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
24 changes: 9 additions & 15 deletions clean/k/runtime/model_k.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cmath>
#include <cstdlib>

#include "../../runtime/model_select.h"
#include "../../runtime/wg_runtime.h"

namespace wgrt {
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion clean/k/runtime/model_k.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions clean/runtime/interop/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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; // <model_dir>\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) {
Expand Down Expand Up @@ -1400,7 +1403,7 @@ class Context {
// model K: <model_dir>\K\model.wgcm (runtime option model_k_dir=<folder> overrides) and the preset-K map
// next to this library or built into it (model_k_map=<file> 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;
Expand Down
85 changes: 85 additions & 0 deletions clean/runtime/model_select.h
Original file line number Diff line number Diff line change
@@ -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=<name>. 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 <cstddef>
#include <string>

#include "../k/runtime/model_k.h" // ModelId

namespace wgrt {

struct ModelInfo {
ModelId id;
const char* name; // "M", "K", "L": the runtime option model=<name> (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=<value> 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
28 changes: 11 additions & 17 deletions clean/runtime/no_model_k.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>

#include "../k/runtime/model_k.h"
#include "model_select.h"
#include "wg_runtime.h"

namespace wgrt {
Expand All @@ -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;
}

Expand Down
12 changes: 7 additions & 5 deletions clean/runtime/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down
94 changes: 94 additions & 0 deletions clean/tests/test_model_select.cpp
Original file line number Diff line number Diff line change
@@ -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=<name> 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 <cstdio>
#include <cstdlib>
#include <string>

#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;
}
Loading
Loading