Skip to content

Commit 5ae42f8

Browse files
committed
Humansl support in onnx trt path
1 parent 978acec commit 5ae42f8

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

cpp/neuralnet/onnxmodelbuilder.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,21 @@ struct Builder {
326326
return addNode("Add", {maskName, c}, uniq(nameBase + "/gpmaskshift"), nameBase + "/gpmaskshift");
327327
}
328328

329+
// SGF metadata encoder (HumanSL nets, metaEncoderVersion > 0): a small MLP over the [N,metaC,1,1]
330+
// metadata input producing a [N,trunkC,1,1] bias that gets added into the trunk's initial bias
331+
// alongside the global-input matmul. Mirrors SGFMetadataEncoder::apply in eigenbackend.cpp and
332+
// ModelParser::buildSGFMetadataEncoder in trtbackend.cpp. All ops are NC11 channel matmuls/biases,
333+
// so this is layout-independent (it runs before any NCHW->NHWC trunk conversion).
334+
string buildSGFMetadataEncoder(const string& input, const SGFMetadataEncoderDesc& desc) {
335+
string x = buildMatMul(input, desc.mul1);
336+
x = buildMatBias(x, desc.bias1);
337+
x = buildActivation(x, desc.act1);
338+
x = buildMatMul(x, desc.mul2);
339+
x = buildMatBias(x, desc.bias2);
340+
x = buildActivation(x, desc.act2);
341+
return buildMatMul(x, desc.mul3);
342+
}
343+
329344
// ---- Residual block builders ----
330345
// useNHWC: input and output are channel-last [N,H,W,C], and the block's internals run NHWC. The
331346
// elementwise BN/activation/mask ops and 1x1 convs are layout-free; spatial convs (k>1) bubble to
@@ -814,9 +829,6 @@ Result build(
814829
bool transformerNHWC,
815830
Logger* logger
816831
) {
817-
if(desc.metaEncoderVersion > 0)
818-
throw StringError("OnnxModelBuilder: SGF metadata encoder not yet supported");
819-
820832
if(logger != NULL)
821833
logger->write("Building internal onnx model, requireExactNNLen=" + Global::boolToString(requireExactNNLen) + " transformerNHWC=" + Global::boolToString(transformerNHWC));
822834

@@ -875,6 +887,12 @@ Result build(
875887
addInput("InputMask", 1);
876888
addInput("InputSpatial", numInputChannels);
877889
addInputNC11("InputGlobal", numInputGlobalChannels);
890+
// HumanSL-style nets additionally take a per-row SGF metadata vector. Only declare the input when
891+
// the model actually has an encoder - an unused graph input would just be dead weight (and the
892+
// backend only allocates/binds an InputMeta buffer when numInputMetaChannels > 0).
893+
bool hasMetaEncoder = desc.metaEncoderVersion > 0;
894+
if(hasMetaEncoder)
895+
addInputNC11("InputMeta", desc.numInputMetaChannels);
878896

879897
// ---- Mask-derived features ----
880898
if(!requireExactNNLen) {
@@ -934,6 +952,12 @@ Result build(
934952
string initialConv = b.buildConv("InputSpatial", trunk.initialConv, false);
935953
string initialMatMul = b.buildMatMul("InputGlobal", trunk.initialMatMul);
936954
string cur = b.elementwise("Add", initialConv, initialMatMul, trunk.name + "/initbias");
955+
if(hasMetaEncoder) {
956+
testAssert(trunk.metaEncoderVersion > 0);
957+
testAssert(trunk.sgfMetadataEncoder.mul3.outChannels == trunk.initialMatMul.outChannels);
958+
string initialMeta = b.buildSGFMetadataEncoder("InputMeta", trunk.sgfMetadataEncoder);
959+
cur = b.elementwise("Add", cur, initialMeta, trunk.name + "/initmetabias");
960+
}
937961

938962
// When transformerNHWC, run the entire trunk block stack channel-last: one NCHW->NHWC conversion
939963
// here and one NHWC->NCHW conversion before the trunk tip. Every block (convnet/gpool/nbt/

cpp/neuralnet/trtbackend.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ struct ModelParser {
200200

201201
// Bump this when between katago versions we want to forcibly drop old timing caches and plan caches.
202202
// Bumped 7->8 for the TensorRT ONNX overhaul (ONNX emitter as default path, NHWC trunk, FP32 pinning).
203-
static constexpr int tuneSalt = 8;
203+
// Bumped 8->9 for SGF metadata encoder support on the ONNX path, and to discard caches potentially
204+
// polluted by the concurrent-engine-build bug fixed in "Serialize TensorRT engine builds across GPU
205+
// threads" (#1225).
206+
static constexpr int tuneSalt = 9;
204207

205208
unique_ptr<TRTModel> build(
206209
unique_ptr<INetworkDefinition> net,
@@ -1309,6 +1312,8 @@ struct ComputeHandle {
13091312
setProfile("InputMask", Dims4(1, 1, ctx->nnYLen, ctx->nnXLen), Dims4(maxBatchSize, 1, ctx->nnYLen, ctx->nnXLen));
13101313
setProfile("InputSpatial", Dims4(1, desc.numInputChannels, ctx->nnYLen, ctx->nnXLen), Dims4(maxBatchSize, desc.numInputChannels, ctx->nnYLen, ctx->nnXLen));
13111314
setProfile("InputGlobal", Dims4(1, desc.numInputGlobalChannels, 1, 1), Dims4(maxBatchSize, desc.numInputGlobalChannels, 1, 1));
1315+
if(desc.metaEncoderVersion > 0)
1316+
setProfile("InputMeta", Dims4(1, desc.numInputMetaChannels, 1, 1), Dims4(maxBatchSize, desc.numInputMetaChannels, 1, 1));
13121317

13131318
model = make_unique<TRTModel>();
13141319
model->nnXLen = ctx->nnXLen;
@@ -1324,9 +1329,10 @@ struct ComputeHandle {
13241329
// and the "nhwc" field distinguishes the NHWC vs NCHW trunk layout (different layer signatures),
13251330
// so the two layouts don't share a timing-cache file full of mutual misses.
13261331
string tuneDesc = Global::strprintf(
1327-
"\"onnxsalt\"(%d)\"nhwc\"(%d)\"model\"(%d,%d,%d)",
1332+
"\"onnxsalt\"(%d)\"nhwc\"(%d)\"model\"(%d,%d,%d,%d,%d)",
13281333
ModelParser::tuneSalt, ctx->transformerNHWC ? 1 : 0,
1329-
desc.modelVersion, desc.numInputChannels, desc.numInputGlobalChannels);
1334+
desc.modelVersion, desc.numInputChannels, desc.numInputGlobalChannels,
1335+
desc.metaEncoderVersion, desc.numInputMetaChannels);
13301336
SHA2::get256(tuneDesc.c_str(), model->tuneHash);
13311337
}
13321338
else {

0 commit comments

Comments
 (0)