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
7 changes: 6 additions & 1 deletion clean/converter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ kinds of preset M's (no new element format, op or source kind); its parameters a
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.
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
versions 1-4 must still have the sources W, D and K: should preset L have no dither table or constants, that rule
becomes per preset.

## Preset F (map version 5, spec 57)

Expand Down
70 changes: 70 additions & 0 deletions clean/converter/tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,76 @@ def layout(with_l, with_m):
return out[0], out[1], l_only, m_only


# ---------------------------------------------------------------- an L-shaped map (Model L, #89)

SQRT32 = "0x40b504f3"


def _swin_block_c32(base):
"""The usual Swin block record set at width C = 32 (2 heads x 32, 16 x 16 position bias, MLP 128), laid out one
after the other from element `base` of W. Returns (tensors, next free element)."""
t, at = [], base

def add(name, shape, op, axes):
nonlocal at
t.append(tensor(name, shape, "float32", "W", op, at, axes))
n = 1
for s in shape:
n *= s
at += n

scaled32 = {"name": "f16_scaled", "mul": SQRT32, "div": SQRT32}
add("gamma1", [32], scaled32, [lin(1)])
add("gamma2", [32], scaled32, [lin(1)])
add("Wqk", [2, 32, 32], {"name": "fp8"}, [lin(1024), lin(32), lin(1)])
add("Wv", [2, 32, 32], {"name": "fp8"}, [lin(1024), lin(32), lin(1)])
add("B", [2, 16, 16], {"name": "f16_scaled", "mul": SQRT8, "div": "0x3f800000"}, [lin(256), lin(16), lin(1)])
add("Wp", [32, 32], {"name": "fp8"}, [lin(32), lin(1)])
add("bp", [32], {"name": "f16"}, [lin(1)])
add("W1", [128, 32], {"name": "fp8"}, [lin(32), lin(1)])
add("b1", [128], {"name": "f16"}, [lin(1)])
add("W2", [32, 128], {"name": "fp8"}, [lin(128), lin(1)])
add("b2", [32], {"name": "f16"}, [lin(1)])
return t, at


def l_shaped_map(m_map):
"""An L-shaped map made from a preset-M map's layout (the dict of data/preset-M.v1.map.json): every M stage as it is,
the input encoder's output linear 64 x 128 (M: 64 x 80), and two new stages: `enc0_pre` (linears 20 -> 32 and
32 -> 32 with biases, then a C = 32 Swin block) before enc0, and `dec0_pre` (a C = 32 Swin block) before dec0.
Named "preset": "L", map version 1 (no source F: whether L has the output filter group is open). The new records
are appended after M's block in W, so W is longer; the stage names, order and placement are our own stand-ins
(the real preset-L map defines its own), only the record kinds and shapes follow L's description."""
m = copy.deepcopy(m_map)
m["preset"] = "L"
m["version"] = 1
m["model"] = "synthetic L-shaped layout (made from the preset-M map's layout)"
m["sources"].pop("F", None)
m["stages"] = [st for st in m["stages"] if not any(t["src"] == "F" for t in st["tensors"])]
at = (m["sources"]["W"]["length"] // 2 + 31) // 32 * 32 # the first free element after M's block
enc0 = next(st for st in m["stages"] if st["stage"] == "enc0")
w = next(t for t in enc0["tensors"] if t["name"] == "W")
w.update(shape=[64, 128], base=at, axes=[lin(128), lin(1)])
at += 64 * 128
pre = [tensor("L0_W", [32, 20], "float32", "W", {"name": "fp8"}, at, [lin(20), lin(1)])]
at += 32 * 20
pre.append(tensor("L0_b", [32], "float32", "W", {"name": "f16"}, at, [lin(1)]))
at += 32
pre.append(tensor("L1_W", [32, 32], "float32", "W", {"name": "fp8"}, at, [lin(32), lin(1)]))
at += 32 * 32
pre.append(tensor("L1_b", [32], "float32", "W", {"name": "f16"}, at, [lin(1)]))
at += 32
block, at = _swin_block_c32(at)
pre += block
dec_block, at = _swin_block_c32(at)
i = next(n for n, st in enumerate(m["stages"]) if st["stage"] == "enc0")
m["stages"].insert(i, {"stage": "enc0_pre", "digest": "", "tensors": pre})
i = next(n for n, st in enumerate(m["stages"]) if st["stage"] == "dec0")
m["stages"].insert(i, {"stage": "dec0_pre", "digest": "", "tensors": dec_block})
m["sources"]["W"]["length"] = 2 * at
return m


# ---------------------------------------------------------------- source F (spec 08 sections 4.3, 4.4; map version 4)

def f_elements(rng, n=5):
Expand Down
40 changes: 40 additions & 0 deletions clean/converter/tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,45 @@ def test_preset_l(r):
r.check("\"preset\": %r in a version-1 map: converter data damaged" % bad, code == EXIT["map_damaged"] and MSG["map_damaged"] in so, so)


def test_l_shaped(r, m_map_path):
"""Model L (#89): an L-shaped map made from the shipped preset-M map's layout (cases.l_shaped_map: the encoder's
output linear 64 x 128, a stage with two linears and a C = 32 Swin block before enc0, a C = 32 Swin block before
dec0; "preset": "L"), filled with our own random data like the shipped-like cases. Every tensor of every stage is
checked against the reference, at two placements of the block; a file without the block gives preset L's message."""
with open(m_map_path, "rb") as f:
lmap = cases.l_shaped_map(json.load(f))
mp_in = r.write("l-shaped/layout.json", lmap)
names = [st["stage"] for st in lmap["stages"]]
r.check("L-shaped map: enc0_pre before enc0, dec0_pre before dec0, no filter group",
names.index("enc0_pre") + 1 == names.index("enc0") and names.index("dec0_pre") + 1 == names.index("dec0") and "filter" not in names, names)
for seed, w_off in ((7, 4096), (8, 4096 + 2)):
t0 = time.time()
case = shipped_like_case(mp_in, seed=seed, w_offset=w_off)
label = "L-shaped(seed %d, W at %d)" % (seed, w_off)
dll = r.write("l-shaped/%d/fake.dll" % seed, case.file)
mp = r.write("l-shaped/%d/map.json" % seed, case.map)
out = r.path("l-shaped/%d/out" % seed, "x")[:-2]
code, so, se = r.run(mp, [dll], out)
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, out, 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:])
enc0 = dict((t[0], t) for t in case.expected["enc0"])
r.check(label + ": the encoder's output linear is 64 x 128 (float32)", len(enc0["W"][1]) == 64 * 128 * 4)
r.check(label + ": the C = 32 blocks have their 11 records",
[t[0] for t in case.expected["dec0_pre"]] == ["gamma1", "gamma2", "Wqk", "Wv", "B", "Wp", "bp", "W1", "b1", "W2", "b2"] and
len(case.expected["enc0_pre"]) == 15)
print("%s: %.1f s" % (label, time.time() - t0))
# the same layout against a file without its block: preset L's message (exit 12)
case = shipped_like_case(mp_in, seed=9)
mp = r.write("l-shaped/9/map.json", case.map)
other = shipped_like_case(mp_in, seed=10) # another random block of the same layout: not this map's
code, so, se = r.run(mp, [r.write("l-shaped/9/other.dll", other.file)], r.path("l-shaped/9/out", "x")[:-2])
r.check("L-shaped map, a file without its block: exit 12 with preset L's message",
code == EXIT["no_preset_m"] and "This DLSS file has no preset-L model." in so, "code %d, out %r" % (code, so))


def mutate(m, fn):
m2 = copy.deepcopy(m)
fn(m2)
Expand Down Expand Up @@ -1065,6 +1104,7 @@ def main():
test_shipped_map_validates(r, args.map)
if not args.quick:
test_shipped_like(r, args.map)
test_l_shaped(r, args.map)
else:
print("note: shipped map not found at %s; skipping the layout tests" % args.map)
if os.path.exists(args.kmap):
Expand Down
Loading