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
14 changes: 10 additions & 4 deletions clean/converter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ data damaged". This is how a further preset that reuses an existing map version

**Preset L** (Model L, issue #89) is such a row: `"preset": "L"` in a map of version 1 or 4. Its maps use the record
kinds of preset M's (no new element format, op or source kind); its parameters are a block of their own in the same
library file as preset M's, found by its own length and hashes like any source. The map file itself
(`data/preset-L.v1.map.json`) comes with the converter data; until it exists, `tests/run_tests.py` covers preset L with a
synthetic library file holding an L block directly before an M block, and maps for both: each converts its own block,
and a file without a preset's block fails with that preset's message. It also converts an **L-shaped** map
library file as preset M's, found by its own length and hashes like any source. Its map,
`data/preset-L.v1.map.json` (map version 1; sources W, D and K), has no `"preset"` field, so the caller names the
preset: `wronggpu-convert --map data\preset-L.v1.map.json --preset L --dll <nvngx_dlss.dll> --out <dir>` (API:
`load_map_bytes(..., "L")`). `--preset` reads a map without the field as that preset when the preset uses the map's
version; a map with the field must name the same preset; anything else is "converter data damaged", and an unknown
`--preset` is a usage error (exit 1). Without `--preset` the preset-L map reads as a version-1 map (preset M's message).
`tests/run_tests.py` checks the real map (validation, the messages, `--preset` mismatches) and converts our own random
data in its exact layout at two placements. It also covers preset L with a synthetic library file holding an L block
directly before an M block, and maps for both: each converts its own block, and a file without a preset's block fails
with that preset's message. It also converts an **L-shaped** map
(`tests/cases.py` `l_shaped_map`): preset M's map layout with the input encoder's output linear 64 x 128, and two new
stages of L's record kinds (two linears and a C = 32 Swin block before `enc0`, a C = 32 Swin block before `dec0`),
filled with our own random data. Its stage names and placement are stand-ins; the real map defines its own. A map of
Expand Down
7 changes: 4 additions & 3 deletions clean/converter/src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,18 @@ Result convert_rest_v5(const Map& map, const std::vector<std::uint8_t>& data, co

} // namespace

Result load_map_bytes(const std::string& bytes, LoadedMap& out, const LogSink& log) {
Result load_map_bytes(const std::string& bytes, LoadedMap& out, const LogSink& log, const std::string& expected_preset) {
out.map = std::make_unique<Map>();
MapError me;
if (!load_map(bytes, *out.map, me)) {
if (!load_map(bytes, *out.map, me, expected_preset)) {
emit(log, "map: " + me.detail);
out.map.reset();
const bool range = me.kind == MapErrorKind::IndexRange;
return fail(range ? kMapDoesNotFit : kMapDamaged, range ? kMsgMapDoesNotFit : kMsgMapDamaged);
}
emit(log, "map sha256 " + to_hex(out.map->file_sha256) + ", " + std::to_string(out.map->stages.size()) +
" stages, model: " + out.map->model);
" stages, model: " + out.map->model +
(expected_preset.empty() ? std::string() : ", preset " + out.map->preset_name));
return Result{};
}

Expand Down
5 changes: 3 additions & 2 deletions clean/converter/src/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ struct LoadedMap {
std::unique_ptr<Map> map;
};

// Parses and validates a map (no DLL is read). On failure the result carries code 20 or 21.
Result load_map_bytes(const std::string& bytes, LoadedMap& out, const LogSink& log);
// Parses and validates a map (no DLL is read). On failure the result carries code 20 or 21. `expected_preset`: the
// preset the caller loads the map for ("" : the map's own field, or its version's preset; map.h load_map).
Result load_map_bytes(const std::string& bytes, LoadedMap& out, const LogSink& log, const std::string& expected_preset = "");

// One converted file, held in memory (nothing is written).
struct Converted {
Expand Down
17 changes: 14 additions & 3 deletions clean/converter/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ void log_line(const std::string& line) { std::cerr << "[wronggpu-convert] " << l

void usage() {
std::cout << "usage: wronggpu-convert --map <map.json> --dll <model dll> [--dll <other> ...]\n"
" --out <dir> [--raw] [--threads N]\n"
" --out <dir> [--raw] [--threads N] [--preset M|K|F|L]\n"
" --dll may be given several times; the files are tried in order: the first that converts\n"
" completely is used (for a map with an optional group, such as preset M's output filter: the\n"
" first that has the group, if any has it). The map names its preset (field \"preset\", or by its\n"
" version: 1 and 4 M, 2 and 3 K, 5 F).\n"
" version: 1 and 4 M, 2 and 3 K, 5 F). --preset names the preset the map is for: a map without the\n"
" field is read as that preset (data/preset-L.v1.map.json: --preset L); a map with it must agree.\n"
" --raw also writes <dir>/raw/<group>/<name>.bin for every tensor.\n";
}

int run(const std::vector<fs::path>& args) {
fs::path map_path, out_dir;
std::vector<fs::path> dlls;
bool raw = false;
std::string preset; // --preset: the preset the map is loaded for ("" : the map's field, or its version's preset)
SearchOptions opt;
for (std::size_t i = 0; i < args.size(); ++i) {
const std::string a = args[i].u8string();
Expand All @@ -59,6 +61,15 @@ int run(const std::vector<fs::path>& args) {
fs::path p;
if (!value(p)) return usage(), kUsage;
dlls.push_back(p);
} else if (a == "--preset") {
fs::path p;
if (!value(p)) return usage(), kUsage;
preset = p.u8string();
if (!find_preset(preset)) {
std::cout << "unknown preset: " << preset << "\n";
usage();
return kUsage;
}
} else if (a == "--raw") {
raw = true;
} else if (a == "--threads") {
Expand Down Expand Up @@ -99,7 +110,7 @@ int run(const std::vector<fs::path>& args) {
}
LoadedMap lm;
log_line("map " + map_path.u8string());
const Result mr = load_map_bytes(map_bytes, lm, log_line);
const Result mr = load_map_bytes(map_bytes, lm, log_line, preset);
if (mr.code != kOk) {
std::cout << mr.message << std::endl;
return mr.code;
Expand Down
23 changes: 23 additions & 0 deletions clean/converter/src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class Loader {
if (!p->allows(version_))
return damaged("map: preset " + pj->text + " does not use map version " + std::to_string(version_));
m.preset_name = p->name;
m.preset_named = true;
} else {
m.preset_name = preset_for_version(version_);
}
Expand Down Expand Up @@ -1397,6 +1398,28 @@ std::string preset_for_version(int version) {
return "";
}

bool load_map(const std::string& file_bytes, Map& out, MapError& err, const std::string& expected_preset) {
if (!load_map(file_bytes, out, err)) return false;
if (expected_preset.empty()) return true;
auto damaged = [&](const std::string& detail) {
err.kind = MapErrorKind::Damaged;
err.detail = detail;
out = Map();
return false;
};
const PresetInfo* p = find_preset(expected_preset);
if (!p) return damaged("map: preset \"" + expected_preset + "\" asked for is not known");
if (out.preset_named) { // the map's own field decides; the caller's name must agree with it
if (out.preset_name != p->name)
return damaged("map: the map names preset " + out.preset_name + ", preset " + expected_preset + " was asked for");
return true;
}
if (!p->allows(out.version))
return damaged("map: preset " + expected_preset + " does not use map version " + std::to_string(out.version));
out.preset_name = p->name;
return true;
}

bool load_map(const std::string& file_bytes, Map& out, MapError& err) {
out = Map();
err = MapError();
Expand Down
6 changes: 6 additions & 0 deletions clean/converter/src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ struct Map {
// The preset, by name ("M", "K", "F"): the map's "preset" field, or preset_for_version(version) without one.
// load_map checks it is a known preset that allows the map's version.
std::string preset_name;
bool preset_named = false; // the map has the field "preset" (else the name came from the version, or the caller)
std::vector<Source> sources; // in file order
std::vector<Array> arrays; // what tensors index
int key_source = -1;
Expand Down Expand Up @@ -275,6 +276,11 @@ struct MapError {

// Parses and fully validates a map, including every index range (source lengths are part of the map).
bool load_map(const std::string& file_bytes, Map& out, MapError& err);
// The same, for a caller that knows which preset it loads the map for (`expected_preset`, e.g. "L"; "" : as above). A map
// without the field "preset" then gets that preset, when it allows the map's version; a map with the field must name
// the same preset. Anything else (an unknown name, a version the preset does not use, another name): damaged. This is
// how a map written before maps named their preset (the delivered preset-L map has no field) is read as its preset.
bool load_map(const std::string& file_bytes, Map& out, MapError& err, const std::string& expected_preset);

// Hex helpers shared with the tests: optional 0x prefix, then 1..max_digits hex digits.
bool parse_hex_u64(const std::string& s, int max_digits, std::uint64_t& out);
Expand Down
48 changes: 48 additions & 0 deletions clean/converter/tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

DEFAULT_MAP = os.path.normpath(os.path.join(HERE, "..", "..", "..", "data", "preset-M.v1.map.json"))
DEFAULT_KMAP = os.path.normpath(os.path.join(HERE, "..", "..", "..", "data", "preset-K.v1.map.json"))
DEFAULT_LMAP = os.path.normpath(os.path.join(HERE, "..", "..", "..", "data", "preset-L.v1.map.json"))
MSG_NO_PRESET_L = "This DLSS file has no preset-L model." # the message's start (its minimum version: api.cpp)
DEFAULT_T5 = os.path.normpath(os.path.join(HERE, "..", "..", "..", "data", "k48-capped-softmax-test.json"))
DEFAULT_T7 = os.path.normpath(os.path.join(HERE, "..", "..", "..", "data", "lz4-block-test.json"))
DTYPE_CODE = {"float32": 0, "uint8": 1, "int32": 2}
Expand Down Expand Up @@ -267,6 +269,47 @@ def test_shipped_map_validates(r, shipped_map):
r.check("real map: nothing written", not os.path.exists(os.path.join(out, "model.wgcm")))


def test_l_real_map(r, lmap):
"""Model L (#89): the delivered preset-L map (data/preset-L.v1.map.json; map version 1, W, D, K; no "preset" field,
so it is loaded with --preset L). It validates; our own random file gives preset L's message (and, without
--preset, preset M's, as for any version-1 map); a mismatching --preset is refused; and its exact layout converts
our own random data at two placements, every tensor of every stage checked against the reference."""
import random
with open(lmap, "rb") as f:
m = json.load(f)
r.check("preset-L map: version 1, sources W, D and K, no \"preset\" field",
m["version"] == 1 and sorted(m["sources"]) == ["D", "K", "W"] and "preset" not in m, (m["version"], sorted(m["sources"])))
rng = random.Random(29)
lw = m["sources"]["W"]["length"]
out = r.path("l-real/out", "x")[:-2]
big = r.write("l-real/nomodel.dll", rng.randbytes(lw + 100_000))
code, so, se = r.run(lmap, [big], out, extra=("--preset", "L"))
r.check("preset-L map --preset L: valid, W absent -> exit 12, preset L's message", code == EXIT["no_preset_m"] and MSG_NO_PRESET_L in so, so + se[-400:])
r.check("...the log names the preset", "preset L" in se, se[-400:])
code, so, se = r.run(lmap, [big], out)
r.check("preset-L map without --preset: read as version 1 (preset M's message)", code == EXIT["no_preset_m"] and MSG["no_preset_m"] in so, so)
for asked in ("K", "F"):
code, so, se = r.run(lmap, [big], out, extra=("--preset", asked))
r.check("preset-L map --preset %s: converter data damaged" % asked, code == EXIT["map_damaged"] and MSG["map_damaged"] in so, so)
code, so, se = r.run(lmap, [big], out, extra=("--preset", "Q"))
r.check("--preset Q (unknown): usage error", code == EXIT["usage"] and "unknown preset" in so, so)
r.check("preset-L map: nothing written", not os.path.exists(os.path.join(out, "model.wgcm")))
for seed, w_off in ((21, 4096), (22, 4096 + 6)):
t0 = time.time()
case = shipped_like_case(lmap, seed=seed, w_offset=w_off)
label = "preset-L layout(seed %d, W at %d)" % (seed, w_off)
dll = r.write("l-real/%d/fake.dll" % seed, case.file)
mp = r.write("l-real/%d/map.json" % seed, case.map)
o = r.path("l-real/%d/out" % seed, "x")[:-2]
code, so, se = r.run(mp, [dll], o, extra=("--preset", "L"))
r.check(label + ": exit 0", code == 0 and SUCCESS in so, so + se[-600:])
with open(mp, "rb") as f:
check_output(r, label, case, o, f.read())
r.check(label + ": offsets", logged_offsets(se) == case.placements, "%s vs %s" % (logged_offsets(se), case.placements))
r.check(label + ": every stage digest matches", ("all %d stage digests match" % len(case.map["stages"])) in se, se[-400:])
print("%s: %.1f s" % (label, time.time() - t0))


def test_preset_l(r):
"""Model L (#89): a map that names preset L ("preset": "L", map version 1) next to a preset-M map, both reading one
synthetic library file in which L's parameter block lies directly before M's. Each map converts its own block, and
Expand Down Expand Up @@ -1087,6 +1130,7 @@ def main():
ap.add_argument("--work", default=os.path.join(HERE, "..", "build", "test-work"))
ap.add_argument("--map", default=DEFAULT_MAP, help="the shipped map (only its layout is used)")
ap.add_argument("--kmap", default=DEFAULT_KMAP, help="the shipped preset-K map (only its layout is used)")
ap.add_argument("--lmap", default=DEFAULT_LMAP, help="the shipped preset-L map (only its layout is used)")
ap.add_argument("--t5", default=DEFAULT_T5, help="spec 48-T5 capped_softmax vectors")
ap.add_argument("--t7", default=DEFAULT_T7, help="spec 08-T7 block decompression vectors")
ap.add_argument("--quick", action="store_true", help="skip the large cases")
Expand All @@ -1113,6 +1157,10 @@ def main():
test_k_shipped_like(r, args.kmap)
else:
print("note: preset-K map not found at %s; skipping its layout tests" % args.kmap)
if os.path.exists(args.lmap):
test_l_real_map(r, args.lmap)
else:
print("note: preset-L map not found at %s; skipping its layout tests" % args.lmap)
if os.path.exists(args.t5):
test_k_t5(r, args.t5)
if not args.quick:
Expand Down
17 changes: 17 additions & 0 deletions clean/converter/tests/unit_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,23 @@ static void test_api_files(const fs::path& work) {
lm1.map->preset() == 'L' && lm1.map->preset_info() == find_preset("L"));
CHECK(load_map_bytes(with_preset(u.map_v4, "4", "\"L\""), lm4, sink).code == kOk && lm4.map && lm4.map->preset_name == "L" &&
lm4.map->stages.size() == m4.map->stages.size());
// the caller's preset (load_map_bytes expected_preset; wronggpu-convert --preset): a map without the field is read
// as that preset when it allows the version; a map with the field must agree
{
LoadedMap h1, h2, h3, h4;
CHECK(load_map_bytes(u.map_v1, h1, sink, "L").code == kOk && h1.map && h1.map->preset_name == "L" && !h1.map->preset_named &&
h1.map->preset_info() == find_preset("L"));
CHECK(load_map_bytes(u.map_v4, h2, sink, "M").code == kOk && h2.map && h2.map->preset_name == "M");
CHECK(load_map_bytes(with_preset(u.map_v1, "1", "\"L\""), h3, sink, "L").code == kOk && h3.map && h3.map->preset_named);
CHECK(load_map_bytes(u.map_v1, h4, sink, "").code == kOk && h4.map && h4.map->preset_name == "M");
for (const char* asked : {"K", "F", "X", "l"}) {
LoadedMap hb;
CHECK(load_map_bytes(u.map_v1, hb, sink, asked).code == kMapDamaged && !hb.map);
}
LoadedMap hm;
CHECK(load_map_bytes(with_preset(u.map_v1, "1", "\"M\""), hm, sink, "L").code == kMapDamaged && !hm.map);
CHECK(logged("the map names preset M, preset L was asked for") && logged("preset \"X\" asked for is not known"));
}
for (const char* bad : {"\"K\"", "\"F\"", "\"X\"", "\"m\"", "\"l\"", "\"\"", "7", "null"}) {
LoadedMap bm;
const Result r = load_map_bytes(with_preset(u.map_v1, "1", bad), bm, sink);
Expand Down
5 changes: 3 additions & 2 deletions data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ only layout information and the digests the converter uses to verify the player'

## Converter maps

`preset-M.v1.map.json` (Model M, map version 4), `preset-F.v1.map.json` (Ray Reconstruction, version 5) and
`preset-K.v1.map.json` (Model K, version 3) tell the converter (`clean/converter`) how to read the network's parameters
`preset-M.v1.map.json` (Model M, map version 4), `preset-F.v1.map.json` (Ray Reconstruction, version 5),
`preset-K.v1.map.json` (Model K, version 3) and `preset-L.v1.map.json` (Model L, version 1; read with the converter's
`--preset L`, since it has no `preset` field) tell the converter (`clean/converter`) how to read the network's parameters
from the player's own DLSS file. Each map holds:

- `format`, `version`, `channel_order`, and a `model` line naming the DLSS product and the first DLSS version the map
Expand Down
Loading
Loading