Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions clean/tests/host_cxx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# SPDX-License-Identifier: MIT
"""Build a small host program from repository sources for a CPU test: the host C++ compiler (CXX, clang++ or g++; on
Windows MSVC cl after them, from PATH or through vcvars64.bat). Raises unittest.SkipTest without a compiler."""
import os
import shutil
import subprocess
import tempfile
import unittest

ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


def _vcvars64():
vswhere = os.path.join(os.environ.get("ProgramFiles(x86)", r"C:\Program Files (x86)"),
"Microsoft Visual Studio", "Installer", "vswhere.exe")
if not os.path.exists(vswhere):
return None
r = subprocess.run([vswhere, "-latest", "-products", "*", "-property", "installationPath"], capture_output=True,
text=True)
vcvars = os.path.join(r.stdout.strip(), "VC", "Auxiliary", "Build", "vcvars64.bat")
return vcvars if r.returncode == 0 and r.stdout.strip() and os.path.exists(vcvars) else None


def build(name, sources, defines=()):
"""sources: paths relative to the repository root; defines: NAME or NAME=VALUE. Returns the program's path."""
tmp = tempfile.mkdtemp(prefix="wg_%s_" % name)
exe = os.path.join(tmp, name + (".exe" if os.name == "nt" else ""))
srcs = [os.path.join(ROOT, s) for s in sources]
cxx = os.environ.get("CXX") or shutil.which("clang++") or shutil.which("g++")
if cxx:
cmd = [cxx, "-std=c++17", "-O1"] + ["-D" + d for d in defines]
cmd += (["-D_CRT_SECURE_NO_WARNINGS"] if os.name == "nt" else []) + srcs + ["-o", exe]
r = subprocess.run(cmd + ([] if os.name == "nt" else ["-ldl", "-pthread"]), capture_output=True, text=True)
elif os.name == "nt" and (shutil.which("cl") or _vcvars64()):
vcvars = None if shutil.which("cl") else _vcvars64()
objdir = os.path.join(tmp, "obj")
os.makedirs(objdir)
cl = ('cl /nologo /std:c++17 /O1 /EHsc /utf-8 /D_CRT_SECURE_NO_WARNINGS %s %s /Fo"%s\\\\" /Fe"%s"'
% (" ".join("/D" + d for d in defines), " ".join('"%s"' % s for s in srcs), objdir, exe))
bat = os.path.join(tmp, "build.bat")
with open(bat, "w") as f:
f.write("@echo off\r\n" + ('call "%s" >nul || exit /b 1\r\n' % vcvars if vcvars else "") +
cl + " || exit /b 1\r\n")
r = subprocess.run(["cmd", "/d", "/c", bat], capture_output=True, text=True)
else:
raise unittest.SkipTest("no host C++ compiler (CXX, clang++, g++ or MSVC cl)")
if r.returncode != 0:
raise RuntimeError("build of %s failed:\n%s%s" % (name, r.stdout, r.stderr))
return exe
146 changes: 146 additions & 0 deletions clean/tests/runtime_frame_trace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// SPDX-License-Identifier: MIT
// The runtime's frame sequence on the CPU, without a GPU: the runtime (clean/runtime/runtime.cpp) runs whole frames on
// a recording device (host memory; launches are recorded, not executed). For clean/tests/test_runtime_frame_trace.py.
//
// runtime_frame_trace <build dir> <model dir> <rw> <rh> <ow> <oh> <frames> [runtime options]
//
// The model is read in the converter's raw layout (model_format 1). Prints the create result, then one line per
// device event of every frame: "load <code object file>[ as <tag>]", "launch <kernel> <grid> <block>"; "ERR <message>"
// on a failure. The code object files are not read: names in the build folder (empty files) select the runtime's
// optional paths as real code objects would. Inputs are zeros (the recording device does not compute), so only the
// sequence is meaningful.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <memory>
#include <string>
#include <vector>

#include "../runtime/device.h"
#include "../runtime/runtime_core.h"
#include "../runtime/wg_runtime.h"

using namespace wgrt;

namespace {

class RecordingDevice : public Device {
public:
std::vector<std::string>* log;
std::map<DevPtr, std::vector<uint8_t>> mem;
DevPtr next = 0x10000;
explicit RecordingDevice(std::vector<std::string>* l) : log(l) {}
std::string name() const override { return "recording device (CPU test)"; }
bool alloc(DevPtr* p, size_t bytes) override {
*p = next;
mem[next].assign(bytes ? bytes : 1, 0);
next += (bytes + 0xFFFF) / 0x10000 * 0x10000 + 0x10000;
return true;
}
void free(DevPtr p) override { mem.erase(p); }
uint8_t* at(DevPtr p, size_t n) {
auto it = mem.upper_bound(p);
if (it == mem.begin()) return nullptr;
--it;
if (p - it->first + n > it->second.size()) return nullptr;
return it->second.data() + (p - it->first);
}
bool upload(DevPtr dst, const void* src, size_t n) override {
uint8_t* d = at(dst, n);
if (d && n) std::memcpy(d, src, n);
return d != nullptr;
}
bool download(void* dst, DevPtr src, size_t n) override {
uint8_t* s = at(src, n);
if (s && n) std::memcpy(dst, s, n);
return s != nullptr;
}
bool copy(DevPtr dst, DevPtr src, size_t n) override {
uint8_t *d = at(dst, n), *s = at(src, n);
if (d && s && n) std::memmove(d, s, n);
return d && s;
}
bool memset(DevPtr dst, int v, size_t n) override {
uint8_t* d = at(dst, n);
if (d && n) std::memset(d, v, n);
return d != nullptr;
}
bool load_module(const std::string& path) override {
const size_t s = path.find_last_of("\\/");
log->push_back("load " + (s == std::string::npos ? path : path.substr(s + 1)));
return true;
}
bool load_module_tagged(const std::string& path, const std::string& tag) override {
if (!load_module(path)) return false;
log->back() += " as " + tag;
return true;
}
bool launch(const std::string& kernel, unsigned grid, unsigned block, void**) override {
log->push_back("launch " + kernel + " " + std::to_string(grid) + " " + std::to_string(block));
return true;
}
bool sync() override { return true; }
bool record(int) override { return true; }
bool elapsed_ms(int, int, float* ms) override { return *ms = 0.0f, true; }
bool reserve_events(int) override { return true; }
int event_done(int) override { return 1; }
bool elapsed_done_ms(int, int, float* ms) override { return *ms = 0.0f, true; }
};

} // namespace

// The device backends are not linked (runtime_create gets the recording device); the runtime still names them.
namespace wgrt {
#if defined(_WIN32)
std::unique_ptr<Device> make_dx12_device(const std::string&, const std::string&, std::string* err) {
return *err = "not linked in this test", nullptr;
}
#endif
std::unique_ptr<Device> make_d3d12_device(const std::string&, std::string* err) {
return *err = "not linked in this test", nullptr;
}
} // namespace wgrt

int main(int argc, char** argv) {
if (argc < 8) return std::printf("ERR usage\n"), 2;
std::vector<std::string> log;
WgRtConfig cfg = WgRtConfig();
cfg.build_dir = argv[1];
cfg.model_dir = argv[2];
cfg.backend = 0;
cfg.model_format = 1; // raw
cfg.max_rw = std::atoi(argv[3]);
cfg.max_rh = std::atoi(argv[4]);
cfg.ow = std::atoi(argv[5]);
cfg.oh = std::atoi(argv[6]);
cfg.depth_inverted = 1;
cfg.auto_exposure = 0;
cfg.runtime_options = argc > 8 ? argv[8] : "";
const int frames = std::atoi(argv[7]);
std::string err;
Runtime* rt = runtime_create(cfg, std::make_unique<RecordingDevice>(&log), &err);
if (!rt) return std::printf("ERR create: %s\n", err.c_str()), 0;
std::printf("create ok model %s\n", runtime_model(rt));
for (const std::string& l : log) std::printf("%s\n", l.c_str());
log.clear();
const size_t px = (size_t)cfg.max_rw * cfg.max_rh;
std::vector<float> color(px * 3, 0.0f), depth(px, 0.0f), motion(px * 2, 0.0f);
for (int f = 0; f < frames; ++f) {
if (wg_rt_upload_inputs(rt, cfg.max_rw, cfg.max_rh, color.data(), depth.data(), motion.data()) != 0)
return std::printf("ERR upload: %s\n", runtime_error(rt).c_str()), 0;
WgRtFrame fr = WgRtFrame();
fr.rw = cfg.max_rw;
fr.rh = cfg.max_rh;
fr.mv_scale[0] = fr.mv_scale[1] = 1.0f;
fr.exposure = 1.0f;
fr.pre_exposure = 1.0f;
if (!runtime_frame(rt, fr, nullptr)) return std::printf("ERR frame %d: %s\n", f, runtime_error(rt).c_str()), 0;
std::printf("frame %d\n", f);
for (const std::string& l : log) std::printf("%s\n", l.c_str());
log.clear();
}
std::printf("kernels %s\n", runtime_kernels(rt));
runtime_destroy(rt);
return 0;
}
132 changes: 132 additions & 0 deletions clean/tests/test_runtime_frame_trace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# SPDX-License-Identifier: MIT
"""The runtime's frame sequence on the CPU (#89): clean/tests/runtime_frame_trace.cpp runs the runtime
(clean/runtime/runtime.cpp) on a recording device, so whole frames run without a GPU and without HIP. Launches are
recorded, not executed: the test checks the sequence (code objects loaded, kernels launched per frame with grid and
block), not pixels. The base for checking other models' frames against Model M's (Model L: Model M's frame with the
encoder and decoder steps replaced).

- a synthetic Model M (converter raw layout): create, the code objects loaded, the launches of four frames;
- a full build's optional code objects (names only) select the default paths: pack12 decoder, dec1 fused with dec0,
wave64 post;
- the same run twice gives the same trace;
- model=L is refused at create for now.

The runtime options of the environment (WG_*) are removed for the runs. Built with the host C++ compiler
(clean/tests/host_cxx.py; skipped without one).

Run: python -m unittest clean.tests.test_runtime_frame_trace
"""
import os
import shutil
import subprocess
import tempfile
import unittest

from clean.runner import synthetic_model as SM
from clean.tests import host_cxx

SOURCES = [os.path.join("clean", "tests", "runtime_frame_trace.cpp")]
SOURCES += [os.path.join("clean", "runtime", s) for s in ("device.cpp", "embedded.cpp", "model.cpp", "runtime.cpp",
"no_model_k.cpp")]
SOURCES += [os.path.join("clean", "converter", "src", s) for s in ("sha256.cpp", "json.cpp")]
_EXE = None
_MODEL = None


def trace_exe():
global _EXE
if _EXE is None:
_EXE = host_cxx.build("runtime_frame_trace", SOURCES, ["WG_NO_MODEL_K"])
return _EXE


def model_dir():
global _MODEL
if _MODEL is None:
d = tempfile.mkdtemp(prefix="wg_ft_")
SM.write_raw(d, SM.synthetic_tensors(1))
_MODEL = d
return _MODEL


# the optional code objects of a full build (clean/runner/build.py) that select the runtime's default paths
SHIPPING = ("decoder__pack12.co", "dec1_dec0__pack12.co", "post_w64.co")


def run_trace(rw=64, rh=64, ow=96, oh=96, frames=3, options="", build_files=()):
"""The program's output lines. The build folder holds empty files of the names in build_files (the recording
device reads no code object; the runtime chooses its paths by the names present)."""
build = tempfile.mkdtemp(prefix="wg_ftb_")
for n in build_files:
open(os.path.join(build, n), "wb").close()
env = {k: v for k, v in os.environ.items() if not k.upper().startswith("WG_")}
try:
r = subprocess.run([trace_exe(), build, model_dir(), str(rw), str(rh), str(ow), str(oh), str(frames), options],
capture_output=True, text=True, env=env, timeout=600)
finally:
shutil.rmtree(build, ignore_errors=True)
if r.returncode != 0:
raise AssertionError("runtime_frame_trace failed (exit %d)\n%s%s" % (r.returncode, r.stdout, r.stderr))
return r.stdout.splitlines()


def parse(lines):
"""(create line, [loaded code objects], [[(kernel, grid, block)] per frame], kernels line)."""
loads, frames, kernels = [], [], None
for ln in lines[1:]:
if ln.startswith("load "):
loads.append(ln[5:])
elif ln.startswith("frame "):
frames.append([])
elif ln.startswith("launch "):
k, g, b = ln[7:].split(" ")
frames[-1].append((k, int(g), int(b)))
elif ln.startswith("kernels "):
kernels = dict(kv.split("=", 1) for kv in ln[8:].split(" ") if "=" in kv)
else:
raise AssertionError("unexpected line: " + ln)
return lines[0], loads, frames, kernels


class ModelM(unittest.TestCase):
def test_frames(self):
create, loads, frames, kernels = parse(run_trace(frames=4))
self.assertEqual(create, "create ok model M")
for key in ("swin", "enc0", "dec0"):
self.assertIn(kernels[key], loads)
self.assertEqual(len(frames), 4)
for f in frames:
names = [k for k, _, _ in f]
self.assertGreater(len(names), 10)
self.assertTrue(all(g > 0 and b > 0 for _, g, b in f))
self.assertEqual(names[0], "exposure_pair_diet") # exposure first, then the encoder
self.assertIn(kernels["post"], names)
# the window offsets alternate from frame to frame (grids of the Swin stages change), so the launches repeat
# every second frame
self.assertNotEqual(frames[0], frames[1])
self.assertEqual(frames[0], frames[2])
self.assertEqual(frames[1], frames[3])

def test_shipping_build(self):
"""With the optional code objects of a full build: the pack12 decoder, dec1 fused with dec0, wave64 post."""
create, loads, frames, kernels = parse(run_trace(frames=2, build_files=SHIPPING))
self.assertEqual(create, "create ok model M")
for want in ("decoder__pack12.co as d0p12", "dec1_dec0__pack12.co as d1p12", "post_w64.co as w64"):
self.assertIn(want, loads)
for f in frames:
names = [k for k, _, _ in f]
self.assertIn("d1p12/swin_block_c64_add_dec0_nc", names)
self.assertNotIn("coef_decoder", names)
self.assertTrue(any(n.startswith("w64/") for n in names))

def test_repeatable(self):
self.assertEqual(run_trace(rw=80, rh=48, ow=120, oh=72), run_trace(rw=80, rh=48, ow=120, oh=72))

def test_model_l_refused(self):
lines = run_trace(options="model=L")
self.assertEqual(len(lines), 1)
self.assertTrue(lines[0].startswith("ERR create: ") and "Model L" in lines[0], lines[0])


if __name__ == "__main__":
unittest.main()
Loading