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
4 changes: 2 additions & 2 deletions clean/dx12/build_dx12.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
and rcp() (fdiv 1.0, x) is the hardware v_rcp_f32; it is reassembled (dxil_asm.cpp, DXC's IDxcAssembler) and signed by
dxv. Any other float division ('/', which would be an IEEE division) or float remainder is refused: use wg_div.

VGPR check: every Swin / bottleneck kernel built (a name with a swin_ part) is run through RGA (gfx1201) and its
VGPR check: every Swin / bottleneck kernel built (a name with a swin_ or l_swin_ part) is run through RGA (gfx1201) and its
USED_VGPRs compared with its bound in clean/dx12/vgpr_limits.json (the count that keeps its present workgroups per CU);
over the bound, or no bound listed, fails the build. WG_DX12_VGPR_CHECK=0 skips it.
"""
Expand Down Expand Up @@ -201,7 +201,7 @@ def rga_isa(dxil, isa_dir, stem):


def vgpr_checked(name):
return re.search(r"(^|/)swin_", name) is not None
return re.search(r"(^|/|l_)swin_", name) is not None # l_swin_*: Model L's blocks (#89)


def rga_vgprs(dxil):
Expand Down
2 changes: 2 additions & 0 deletions clean/dx12/kernels/fused_swin.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ float norm_r(v8f a[KC])
ss = ss + wg_xor16(ss);
#if SW_C == 64
return wg_rsq((ss + asfloat(0x39000000u)) * asfloat(0x3c800000u)); // (ss + 2^-13) * (1 / 64)
#elif SW_C == 32
return wg_rsq((ss + asfloat(0x39000000u)) * asfloat(0x3d000000u)); // (ss + 2^-13) * (1 / 32) (Model L, #89)
#else
return wg_rsq((ss + asfloat(0x39000000u)) * asfloat(0x3c2aaaabu)); // (ss + 2^-13) * (1 / 96)
#endif
Expand Down
7 changes: 7 additions & 0 deletions clean/dx12/kernels/l_swin.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
// D3D12 port of Model L's C = 32 Swin block (clean/l/kernels/l_swin.hip, l_swin_block_c32_nc of l_swin.co; issue #89):
// swin_block.hlsl instantiated at C = 32, 2 heads, MLP 128 (the same template as the HIP kernel's), with HIP's
// transcendental GELU chain (SW_GELU 0, no table) and HIP's MLP unit cut (HPU 8 = all 8 hidden tiles in one unit).
//
// @wg-kernel l_swin_block_c32_nc co=l_swin.co block=256 wave=32 args=p,p,p,p,u,u,u,u defines=SW_C=32,SW_HEADS=2,SW_MLP=128,SW_ADD=0,SW_SAFE=1,SW_ECL=0,SW_HPU=8,SW_GELU=0,SW_ENTRY_OVERRIDE=l_swin_block_c32_nc
#include "swin_block.hlsl"
2 changes: 2 additions & 0 deletions clean/dx12/kernels/swin_block.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ float norm_r(v8f a[KC])
ss = ss + wg_xor16(ss);
#if SW_C == 64
return wg_rsq((ss + asfloat(0x39000000u)) * asfloat(0x3c800000u)); // (ss + 2^-13) * (1 / 64)
#elif SW_C == 32
return wg_rsq((ss + asfloat(0x39000000u)) * asfloat(0x3d000000u)); // (ss + 2^-13) * (1 / 32) (Model L, #89)
#else
return wg_rsq((ss + asfloat(0x39000000u)) * asfloat(0x3c2aaaabu)); // (ss + 2^-13) * (1 / 96)
#endif
Expand Down
168 changes: 168 additions & 0 deletions clean/dx12/tests/kernel_replay.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// SPDX-License-Identifier: MIT
// Kernel replay on the native D3D12 Device (issue #89): runs single kernels by their launch names on synthetic inputs
// given in files, so that a D3D12 port can be compared byte for byte with its HIP kernel on the same inputs (driver:
// clean/l/runner/l_d3d12_check.py). Uses only the project's Device API (clean/runtime/device.h).
//
// kernel_replay <build dir with dx12/manifest.json> <cases.txt>
//
// cases.txt, one case per block of lines (blank line between cases; paths relative to the cases file):
// case <name>
// module <code object name> [<tag>] (zero or more)
// kernel <launch name> <grid> <block>
// arg buf <file> device buffer with the file's bytes
// arg out <bytes> <file> [fill] device buffer of that size, every byte set to fill (0..255, default 0)
// before the launch, written to <file> afterwards
// arg null null pointer
// arg u32 <value> 32-bit scalar (decimal, may be negative)
// arg bytes <file> a struct passed by value (the file's bytes)
#include "../../runtime/device.h"

#include <cstdint>
#include <cstdio>
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

using namespace wgrt;

namespace {

bool read_file(const std::string& path, std::vector<uint8_t>* out) {
std::ifstream f(path, std::ios::binary);
if (!f) return false;
out->assign(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
return true;
}

bool write_file(const std::string& path, const std::vector<uint8_t>& data) {
std::ofstream f(path, std::ios::binary);
if (!f) return false;
f.write(reinterpret_cast<const char*>(data.data()), (std::streamsize)data.size());
return (bool)f;
}

struct Arg {
std::string kind, file;
size_t bytes = 0;
int64_t value = 0;
DevPtr ptr = 0;
uint32_t u = 0;
std::vector<uint8_t> blob;
};

struct Case {
std::string name, kernel;
unsigned grid = 0, block = 0;
std::vector<std::pair<std::string, std::string>> modules;
std::vector<Arg> args;
};

std::string dir_of(const std::string& path) {
const size_t s = path.find_last_of("/\\");
return s == std::string::npos ? std::string(".") : path.substr(0, s);
}

int run_case(Device* d, Case& c, const std::string& base) {
for (auto& m : c.modules) {
const bool ok = m.second.empty() ? d->load_module(m.first) : d->load_module_tagged(m.first, m.second);
if (!ok) { std::printf("%s: module %s: %s\n", c.name.c_str(), m.first.c_str(), d->err().c_str()); return 1; }
}
std::vector<void*> argv;
std::vector<DevPtr> owned;
for (auto& a : c.args) {
if (a.kind == "buf") {
std::vector<uint8_t> data;
if (!read_file(base + "/" + a.file, &data)) { std::printf("%s: cannot read %s\n", c.name.c_str(), a.file.c_str()); return 1; }
if (!d->alloc(&a.ptr, data.size() ? data.size() : 4) || !d->upload(a.ptr, data.data(), data.size())) {
std::printf("%s: buffer %s: %s\n", c.name.c_str(), a.file.c_str(), d->err().c_str());
return 1;
}
owned.push_back(a.ptr);
argv.push_back(&a.ptr);
} else if (a.kind == "out") {
if (!d->alloc(&a.ptr, a.bytes) || !d->memset(a.ptr, (int)(a.value & 0xff), a.bytes)) {
std::printf("%s: output: %s\n", c.name.c_str(), d->err().c_str());
return 1;
}
owned.push_back(a.ptr);
argv.push_back(&a.ptr);
} else if (a.kind == "null") {
a.ptr = 0;
argv.push_back(&a.ptr);
} else if (a.kind == "u32") {
a.u = (uint32_t)a.value;
argv.push_back(&a.u);
} else if (a.kind == "bytes") {
if (!read_file(base + "/" + a.file, &a.blob)) { std::printf("%s: cannot read %s\n", c.name.c_str(), a.file.c_str()); return 1; }
argv.push_back(a.blob.data());
}
}
if (!d->launch(c.kernel, c.grid, c.block, argv.data())) {
std::printf("%s: launch %s: %s\n", c.name.c_str(), c.kernel.c_str(), d->err().c_str());
return 1;
}
if (!d->sync()) { std::printf("%s: sync: %s\n", c.name.c_str(), d->err().c_str()); return 1; }
for (auto& a : c.args) {
if (a.kind != "out") continue;
std::vector<uint8_t> data(a.bytes);
if (!d->download(data.data(), a.ptr, a.bytes) || !write_file(base + "/" + a.file, data)) {
std::printf("%s: download %s: %s\n", c.name.c_str(), a.file.c_str(), d->err().c_str());
return 1;
}
}
for (DevPtr p : owned) d->free(p);
std::printf("%s: ok (%s)\n", c.name.c_str(), c.kernel.c_str());
return 0;
}

} // namespace

int main(int argc, char** argv) {
if (argc != 3) { std::printf("usage: kernel_replay <build dir> <cases.txt>\n"); return 2; }
std::string err;
auto d = make_d3d12_device(argv[1], &err);
if (!d) { std::printf("device: %s\n", err.c_str()); return 1; }
std::printf("device: %s\n", d->name().c_str());
std::ifstream f(argv[2]);
if (!f) { std::printf("cannot read %s\n", argv[2]); return 2; }
const std::string base = dir_of(argv[2]);
std::vector<Case> cases;
std::string line;
while (std::getline(f, line)) {
if (!line.empty() && line.back() == '\r') line.pop_back();
std::istringstream s(line);
std::string w;
if (!(s >> w)) continue;
if (w == "case") { cases.emplace_back(); s >> cases.back().name; continue; }
if (cases.empty()) { std::printf("line before the first case: %s\n", line.c_str()); return 2; }
Case& c = cases.back();
if (w == "module") {
std::string m, tag;
s >> m >> tag;
c.modules.emplace_back(m, tag);
} else if (w == "kernel") {
s >> c.kernel >> c.grid >> c.block;
} else if (w == "arg") {
Arg a;
s >> a.kind;
if (a.kind == "buf" || a.kind == "bytes") s >> a.file;
else if (a.kind == "out") {
s >> a.bytes >> a.file;
if (!(s >> a.value)) a.value = 0; // the fill byte (a sentinel), default 0
}
else if (a.kind == "u32") s >> a.value;
else if (a.kind != "null") { std::printf("unknown arg kind %s\n", a.kind.c_str()); return 2; }
c.args.push_back(a);
} else {
std::printf("unknown line: %s\n", line.c_str());
return 2;
}
}
int fails = 0;
for (auto& c : cases) fails += run_case(d.get(), c, base);
if (!d->sync()) { std::printf("final sync: %s\n", d->err().c_str()); return 1; }
std::printf("kernel_replay: %d of %zu cases failed\n", fails, cases.size());
return fails ? 1 : 0;
}
1 change: 1 addition & 0 deletions clean/dx12/vgpr_limits.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"_doc": "build_dx12.py VGPR check: the RGA (gfx1201) USED_VGPRs bound of every Swin / bottleneck kernel (a name with a swin_ part). Each bound is the largest count that keeps the kernel's present occupancy: VGPR file 1536 per SIMD lane, granule 8, a 256-lane wave32 workgroup = 4 waves on each of a CU's 2 SIMDs, so 2 workgroups per CU need <= 192, 3 need <= 128 (a 128-lane workgroup = 2 waves per SIMD: 4 per CU need <= 192). A kernel over its bound fails the build (a merge or an edit lost a workgroup per CU: swin_block_c96_add_xe_* at 215 VGPRs ran 1 per CU, +38 us per frame at 1440p Q). A new Swin / bottleneck kernel needs an entry. WG_DX12_VGPR_CHECK=0 skips the check.",
"l_swin_block_c32_nc": 96,
"swin_block_c64_nc": 192,
"swin_block_c96_nc": 192,
"swin_block_c96_add_ex_nc": 192,
Expand Down
Loading
Loading