diff --git a/clean/k/runtime/model_k.cpp b/clean/k/runtime/model_k.cpp index 7bed76e6..bc879c40 100644 --- a/clean/k/runtime/model_k.cpp +++ b/clean/k/runtime/model_k.cpp @@ -6,6 +6,7 @@ #include #include +#include "../../runtime/model_select.h" #include "../../runtime/wg_runtime.h" namespace wgrt { @@ -123,21 +124,14 @@ bool model_from_options(const WgRtConfig& c, ModelId* model, std::string* err) { const char* env = std::getenv("WG_RUNTIME_OPTIONS"); const std::string text = std::string(c.runtime_options ? c.runtime_options : "") + ";" + (env ? env : ""); *model = ModelId::M; - for (size_t a = 0; a <= text.size();) { - size_t b = text.find_first_of(";,", a); - if (b == std::string::npos) b = text.size(); - const std::string kv = text.substr(a, b - a); - a = b + 1; - if (kv.compare(0, 6, "model=") != 0) continue; - const std::string v = kv.substr(6); - if (v == "M" || v == "m") { - *model = ModelId::M; - } else if (v == "K" || v == "k") { - *model = ModelId::K; - } else { - *err = "unknown model \"" + v + "\" in the runtime options (known: M, K)"; - return false; - } + std::string bad; + if (!last_model_option(text, model, &bad)) { + *err = "unknown model \"" + bad + "\" in the runtime options (known: M, K)"; + return false; + } + if (*model == ModelId::L) { // #89: recognised, not part of the runtime yet + *err = model_not_built_yet(ModelId::L); + return false; } return true; } diff --git a/clean/k/runtime/model_k.h b/clean/k/runtime/model_k.h index 632f0914..0396c083 100644 --- a/clean/k/runtime/model_k.h +++ b/clean/k/runtime/model_k.h @@ -17,7 +17,9 @@ struct WgRtConfig; namespace wgrt { -enum class ModelId { M, K }; +// The models of the runtime (clean/runtime/model_select.h: their table and the option parser). L: recognised and +// refused until its network exists (#89). +enum class ModelId { M, K, L }; struct KStageInfo { const char* name; diff --git a/clean/runtime/interop/backend.cpp b/clean/runtime/interop/backend.cpp index 956b56e6..1e172968 100644 --- a/clean/runtime/interop/backend.cpp +++ b/clean/runtime/interop/backend.cpp @@ -33,6 +33,7 @@ #include "../runtime_core.h" #include "../embedded.h" #include "../../k/runtime/model_k.h" +#include "../model_select.h" #include "convert.h" #include "executor.h" #include "executor_select.h" @@ -45,8 +46,10 @@ namespace wgi { namespace { -const char kMapName[] = "preset-M.v1.map.json"; -const char kKMapName[] = "preset-K.v1.map.json"; // model K (runtime option model=K) +// the converter maps and model K's folder, from the model table (clean/runtime/model_select.h) +const char* const kMapName = wgrt::model_info(wgrt::ModelId::M).map_file; // preset-M.v1.map.json +const char* const kKMapName = wgrt::model_info(wgrt::ModelId::K).map_file; // model K (runtime option model=K) +const char* const kKSubdir = wgrt::model_info(wgrt::ModelId::K).model_subdir; // \K // Value of `key` in a "key=value;..." string (the last one wins), "" if absent. std::string option_value(const std::string& text, const std::string& key) { @@ -1400,7 +1403,7 @@ class Context { // model K: \K\model.wgcm (runtime option model_k_dir= overrides) and the preset-K map // next to this library or built into it (model_k_map= overrides); the runtime loads and checks both std::string kdir = option_value(o.runtime, "model_k_dir"); - if (kdir.empty()) kdir = model_dir_ + "\\K"; + if (kdir.empty()) kdir = model_dir_ + "\\" + kKSubdir; model_path_ = kdir + "\\model.wgcm"; if (!exists(model_path_)) return *err = "model K: no converted model at " + model_path_ + " (convert the preset-K model there)", false; diff --git a/clean/runtime/model_select.h b/clean/runtime/model_select.h new file mode 100644 index 00000000..fcbe7389 --- /dev/null +++ b/clean/runtime/model_select.h @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: MIT +// Model selection shared by the runtime, the D3D12 interop and the builds with and without model K (#89): the model +// table (name, converter map, folder under the model directory, native D3D12 support) and the parser of the runtime +// option model=. Model M is the default and the first row; a model's row says what it needs from its +// surroundings, so the callers do not compare against one model by hand. Header only: every build that compiles +// clean/k/runtime/model_k.cpp or clean/runtime/no_model_k.cpp gets it without a new source file. +// +// Model L has a row so that model=L is recognised and refused by name ("not part of this build yet") until its +// network exists; the runtime never selects it today. +#pragma once + +#include +#include + +#include "../k/runtime/model_k.h" // ModelId + +namespace wgrt { + +struct ModelInfo { + ModelId id; + const char* name; // "M", "K", "L": the runtime option model= (either case) and runtime_model() + const char* map_file; // the converter map of the model's preset, next to the runtime library + const char* model_subdir; // the model's folder under the model directory ("" = the model directory itself) + bool native_d3d12; // runs on the native D3D12 backend (backend 3) +}; + +constexpr ModelInfo kModelTable[] = { + {ModelId::M, "M", "preset-M.v1.map.json", "", true}, + {ModelId::K, "K", "preset-K.v1.map.json", "K", false}, + {ModelId::L, "L", "preset-L.v1.map.json", "L", false}, +}; +constexpr size_t kModelCount = sizeof kModelTable / sizeof kModelTable[0]; + +inline const ModelInfo& model_info(ModelId id) { + for (const ModelInfo& m : kModelTable) + if (m.id == id) return m; + return kModelTable[0]; +} + +// A model name in either case ("M", "m", "K", ...). False: no such model. +inline bool model_from_name(const std::string& v, ModelId* id) { + for (const ModelInfo& m : kModelTable) { + const char c = m.name[0]; + if (v.size() == 1 && (v[0] == c || v[0] == c - 'A' + 'a')) { + *id = m.id; + return true; + } + } + return false; +} + +// The last model= of `text` (options separated by ';' or ','; no model= leaves *model as it is). False at the +// first unknown value (*bad = that value). +inline bool last_model_option(const std::string& text, ModelId* model, std::string* bad) { + for (size_t a = 0; a <= text.size();) { + size_t b = text.find_first_of(";,", a); + if (b == std::string::npos) b = text.size(); + const std::string kv = text.substr(a, b - a); + a = b + 1; + if (kv.compare(0, 6, "model=") != 0) continue; + const std::string v = kv.substr(6); + if (!model_from_name(v, model)) { + *bad = v; + return false; + } + } + return true; +} + +// The refusal of model=L until its network is part of the runtime (one place for both builds). +inline std::string model_not_built_yet(ModelId id) { + return std::string("Model ") + model_info(id).name + " is not part of this build yet (runtime option model=" + + model_info(id).name + "); remove the option to use Model M"; +} + +// "Model M" (or "Models M, K" ...): the models the native D3D12 backend runs, for its messages. +inline std::string native_d3d12_models() { + std::string names; + int n = 0; + for (const ModelInfo& m : kModelTable) + if (m.native_d3d12) names += std::string(n++ ? ", " : "") + m.name; + return std::string(n > 1 ? "Models " : "Model ") + names; +} + +} // namespace wgrt diff --git a/clean/runtime/no_model_k.cpp b/clean/runtime/no_model_k.cpp index 6774a9ce..ded375a9 100644 --- a/clean/runtime/no_model_k.cpp +++ b/clean/runtime/no_model_k.cpp @@ -9,6 +9,7 @@ #include #include "../k/runtime/model_k.h" +#include "model_select.h" #include "wg_runtime.h" namespace wgrt { @@ -18,27 +19,20 @@ bool model_from_options(const WgRtConfig& c, ModelId* model, std::string* err) { const char* env = std::getenv("WG_RUNTIME_OPTIONS"); const std::string text = std::string(c.runtime_options ? c.runtime_options : "") + ";" + (env ? env : ""); *model = ModelId::M; - bool k = false; - for (size_t a = 0; a <= text.size();) { - size_t b = text.find_first_of(";,", a); - if (b == std::string::npos) b = text.size(); - const std::string kv = text.substr(a, b - a); - a = b + 1; - if (kv.compare(0, 6, "model=") != 0) continue; - const std::string v = kv.substr(6); - if (v == "M" || v == "m") { - k = false; - } else if (v == "K" || v == "k") { - k = true; - } else { - *err = "unknown model \"" + v + "\" in the runtime options (known: M)"; - return false; - } + ModelId last = ModelId::M; + std::string bad; + if (!last_model_option(text, &last, &bad)) { + *err = "unknown model \"" + bad + "\" in the runtime options (known: M)"; + return false; } - if (k) { + if (last == ModelId::K) { *err = "Model K is not part of this build (runtime option model=K); remove the option to use Model M"; return false; } + if (last == ModelId::L) { // #89: recognised, not part of the runtime yet + *err = model_not_built_yet(ModelId::L); + return false; + } return true; } diff --git a/clean/runtime/runtime.cpp b/clean/runtime/runtime.cpp index 947011c4..6e3f007d 100644 --- a/clean/runtime/runtime.cpp +++ b/clean/runtime/runtime.cpp @@ -20,6 +20,7 @@ #include "embedded.h" #include "fpconv.h" #include "model.h" +#include "model_select.h" #include "runtime_core.h" #include "wg_runtime.h" @@ -483,9 +484,10 @@ struct Runtime { ModelId model_id = ModelId::M; // runtime option model=K (clean/k/runtime/model_k.h); default model M if (std::string merr; !model_from_options(c, &model_id, &merr)) return fail(merr); if (c.backend < 0 || c.backend > 3) return fail("unknown runtime backend"); - if (native_direct_io && (!external_io || c.backend != 3 || model_id != ModelId::M)) - return fail("native direct resources require external_io, backend 3 and Model M"); - if (c.backend == 3 && model_id != ModelId::M) return fail("native D3D12 supports Model M only"); + const bool native_ok = model_info(model_id).native_d3d12; // model_select.h (Model M only today) + if (native_direct_io && (!external_io || c.backend != 3 || !native_ok)) + return fail("native direct resources require external_io, backend 3 and " + native_d3d12_models()); + if (c.backend == 3 && !native_ok) return fail("native D3D12 supports " + native_d3d12_models() + " only"); d3d12_native = c.backend == 3; if (c.backend == 3 && external && !native_direct_io) return fail("native D3D12 external I/O requires the private direct-resource route"); @@ -2237,9 +2239,9 @@ const char* const* runtime_timing_labels(Runtime* rt) { #endif } #ifdef WG_NO_MODEL_K -const char* runtime_model(Runtime*) { return "M"; } +const char* runtime_model(Runtime*) { return model_info(ModelId::M).name; } #else -const char* runtime_model(Runtime* rt) { return rt->k ? "K" : "M"; } +const char* runtime_model(Runtime* rt) { return model_info(rt->k ? ModelId::K : ModelId::M).name; } #endif void runtime_recon_size(Runtime* rt, int* dw, int* dh) { *dw = rt->dw; diff --git a/clean/tests/test_model_select.cpp b/clean/tests/test_model_select.cpp new file mode 100644 index 00000000..e92dbf88 --- /dev/null +++ b/clean/tests/test_model_select.cpp @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: MIT +// The runtime's model selection (clean/runtime/model_select.h, model_from_options of clean/k/runtime/model_k.cpp and +// clean/runtime/no_model_k.cpp): the model table, and the runtime option model= with the exact messages the +// runtime gave before the table existed (#89). Built twice by clean/tests/test_model_select.py: with model K +// (model_k.cpp) and with -DWG_NO_MODEL_K (no_model_k.cpp). Prints one line per failed check; exit code = failures. +#include +#include +#include + +#include "../k/runtime/model_k.h" +#include "../runtime/model_select.h" +#include "../runtime/wg_runtime.h" + +namespace wgrt { +bool model_from_options(const WgRtConfig& c, ModelId* model, std::string* err); +} + +static int g_fail = 0; +#define EXPECT(c) \ + do { \ + if (!(c)) { \ + ++g_fail; \ + std::printf("FAILED line %d: %s\n", __LINE__, #c); \ + } \ + } while (0) + +// model_from_options for one options string: (ok, model, message) +static bool select(const char* opts, wgrt::ModelId* m, std::string* err) { + WgRtConfig c = WgRtConfig(); + c.runtime_options = opts; + err->clear(); + *m = wgrt::ModelId::K; // overwritten by every call + return wgrt::model_from_options(c, m, err); +} + +int main() { + using wgrt::ModelId; +#ifdef _WIN32 + _putenv("WG_RUNTIME_OPTIONS="); +#else + unsetenv("WG_RUNTIME_OPTIONS"); +#endif + // ---- the table: Model M first, with the names the runtime and the interop used before + EXPECT(wgrt::kModelTable[0].id == ModelId::M); + const wgrt::ModelInfo& m = wgrt::model_info(ModelId::M); + const wgrt::ModelInfo& k = wgrt::model_info(ModelId::K); + const wgrt::ModelInfo& l = wgrt::model_info(ModelId::L); + EXPECT(std::string(m.name) == "M" && std::string(m.map_file) == "preset-M.v1.map.json" && + std::string(m.model_subdir).empty() && m.native_d3d12); + EXPECT(std::string(k.name) == "K" && std::string(k.map_file) == "preset-K.v1.map.json" && + std::string(k.model_subdir) == "K" && !k.native_d3d12); + EXPECT(std::string(l.name) == "L" && std::string(l.map_file) == "preset-L.v1.map.json" && + std::string(l.model_subdir) == "L" && !l.native_d3d12); + EXPECT(wgrt::native_d3d12_models() == "Model M"); // "native D3D12 supports Model M only", as before + ModelId id = ModelId::K; + EXPECT(wgrt::model_from_name("m", &id) && id == ModelId::M); + EXPECT(wgrt::model_from_name("L", &id) && id == ModelId::L); + EXPECT(!wgrt::model_from_name("MM", &id) && !wgrt::model_from_name("", &id) && !wgrt::model_from_name("x", &id)); + + // ---- the option, Model M (both builds): the default and model=M select Model M + std::string err; + EXPECT(select(nullptr, &id, &err) && id == ModelId::M && err.empty()); + EXPECT(select("", &id, &err) && id == ModelId::M); + EXPECT(select("model=M", &id, &err) && id == ModelId::M); + EXPECT(select("model=m;network=fused", &id, &err) && id == ModelId::M); + EXPECT(select("a=1,model=K;model=M", &id, &err) && id == ModelId::M); // the last one decides + EXPECT(select("modelx=K", &id, &err) && id == ModelId::M); // not the model option +#ifndef WG_NO_MODEL_K + EXPECT(select("model=K", &id, &err) && id == ModelId::K); + EXPECT(select("model=k", &id, &err) && id == ModelId::K); + EXPECT(!select("model=X", &id, &err) && err == "unknown model \"X\" in the runtime options (known: M, K)"); + EXPECT(!select("model=", &id, &err) && err == "unknown model \"\" in the runtime options (known: M, K)"); +#else + EXPECT(!select("model=K", &id, &err) && + err == "Model K is not part of this build (runtime option model=K); remove the option to use Model M"); + EXPECT(!select("model=X", &id, &err) && err == "unknown model \"X\" in the runtime options (known: M)"); + EXPECT(!select("model=", &id, &err) && err == "unknown model \"\" in the runtime options (known: M)"); +#endif + // ---- model=L: recognised and refused by name until its network exists (#89) + const std::string not_yet = "Model L is not part of this build yet (runtime option model=L); remove the option to " + "use Model M"; + EXPECT(!select("model=L", &id, &err) && err == not_yet); + EXPECT(!select("model=l", &id, &err) && err == not_yet); + EXPECT(select("model=L;model=M", &id, &err) && id == ModelId::M); // the last one decides, as for K + + std::printf("test_model_select (%s): %d failure(s)\n", +#ifdef WG_NO_MODEL_K + "no model K", +#else + "with model K", +#endif + g_fail); + return g_fail; +} diff --git a/clean/tests/test_model_select.py b/clean/tests/test_model_select.py new file mode 100644 index 00000000..49b194fb --- /dev/null +++ b/clean/tests/test_model_select.py @@ -0,0 +1,74 @@ +# SPDX-License-Identifier: MIT +"""The runtime's model selection (#89): clean/runtime/model_select.h and model_from_options in both runtime builds, +with model K (clean/k/runtime/model_k.cpp) and without (clean/runtime/no_model_k.cpp, -DWG_NO_MODEL_K). + +No GPU: clean/tests/test_model_select.cpp is built twice with the host C++ compiler (CXX, clang++ or g++; on Windows +MSVC cl after them, from PATH or through vcvars64.bat; skipped without one) and run. It checks the model table and the +exact messages of the runtime option model= (Model M and model K as before; model=L refused by name). + +Run: python -m unittest clean.tests.test_model_select +""" +import os +import shutil +import subprocess +import tempfile +import unittest + +ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +TEST = os.path.join("clean", "tests", "test_model_select.cpp") +BUILDS = {"with model K": ([TEST, os.path.join("clean", "k", "runtime", "model_k.cpp")], []), + "no model K": ([TEST, os.path.join("clean", "runtime", "no_model_k.cpp")], ["WG_NO_MODEL_K"])} + + +def _vcvars64(): + """vcvars64.bat of the newest Visual Studio (vswhere), or None.""" + 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(exe, sources, defines): + cxx = os.environ.get("CXX") or shutil.which("clang++") or shutil.which("g++") + if cxx: + cmd = [cxx, "-std=c++17", "-O1"] + (["-D_CRT_SECURE_NO_WARNINGS"] if os.name == "nt" else []) + cmd += ["-D" + d for d in defines] + [os.path.join(ROOT, s) for s in sources] + ["-o", exe] + return subprocess.run(cmd, capture_output=True, text=True) + vcvars = None if shutil.which("cl") else _vcvars64() + if os.name != "nt" or not (shutil.which("cl") or vcvars): + raise unittest.SkipTest("no host C++ compiler (CXX, clang++, g++ or MSVC cl)") + objdir = os.path.join(os.path.dirname(exe), "obj_" + os.path.splitext(os.path.basename(exe))[0]) + os.makedirs(objdir, exist_ok=True) + 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"' % os.path.join(ROOT, s) for s in sources), objdir, + exe)) + bat = os.path.join(os.path.dirname(exe), os.path.basename(exe) + ".bat") + with open(bat, "w") as f: + f.write("@echo off\r\n") + if vcvars: + f.write('call "%s" >nul || exit /b 1\r\n' % vcvars) + f.write(cl + " || exit /b 1\r\n") + return subprocess.run(["cmd", "/d", "/c", bat], capture_output=True, text=True) + + +class ModelSelect(unittest.TestCase): + def test_both_builds(self): + tmp = tempfile.mkdtemp(prefix="wg_ms_") + for i, (label, (sources, defines)) in enumerate(BUILDS.items()): + with self.subTest(build=label): + exe = os.path.join(tmp, "test_model_select_%d%s" % (i, ".exe" if os.name == "nt" else "")) + r = _build(exe, sources, defines) + self.assertEqual(r.returncode, 0, "build failed:\n" + r.stdout + r.stderr) + env = dict(os.environ) + env.pop("WG_RUNTIME_OPTIONS", None) + r = subprocess.run([exe], capture_output=True, text=True, env=env) + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + self.assertIn("test_model_select (%s): 0 failure(s)" % label, r.stdout) + + +if __name__ == "__main__": + unittest.main()