Skip to content

Commit d0c49c6

Browse files
authored
Default TensorRT transformer trunks to NHWC (#1218)
1 parent 6763960 commit d0c49c6

3 files changed

Lines changed: 12 additions & 8 deletions

File tree

‎cpp/configs/gtp_example.cfg‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ searchFactorWhenWinningThreshold = 0.95
410410
# ------------------------------
411411
# These only apply when using the TENSORRT version of KataGo.
412412

413+
# Transformer trunks use NHWC by default. Set this to false only after benchmarking
414+
# the particular GPU and TensorRT version.
415+
# trtTransformerNHWC = true
416+
413417
# For one GPU: optionally uncomment this option and change if the GPU to
414418
# use is not device 0.
415419
# trtDeviceToUse = 0

‎cpp/neuralnet/onnxmodelbuilder.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ using namespace std;
1515
namespace {
1616

1717
// Builder that accumulates ONNX nodes and initializers into a single GraphProto, handing back
18-
// tensor names as it goes. Tensors are float32; the trunk runs NCHW by
19-
// default, or channel-last NHWC when transformerNHWC is set (see buildBlockStack / buildConv).
18+
// tensor names as it goes. Tensors are float32; transformerNHWC selects a channel-last NHWC trunk
19+
// instead of NCHW (see buildBlockStack / buildConv).
2020
struct Builder {
2121
onnx::GraphProto* graph;
2222
int nnXLen;

‎cpp/neuralnet/trtbackend.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ ComputeContext* NeuralNet::createComputeContext(
9494
ConfigParser& cfg) {
9595
(void)gpuIdxs;
9696
(void)logger;
97-
(void)loadedModel;
9897

9998
ComputeContext* context = new ComputeContext();
10099
context->nnXLen = nnXLen;
@@ -105,11 +104,12 @@ ComputeContext* NeuralNet::createComputeContext(
105104
// nvonnxparser (the default). trtDisableOnnx=true falls back to the hand-built ModelParser, which
106105
// supports convnets only (transformer models will error in createComputeHandle).
107106
context->useOnnx = !(cfg.contains("trtDisableOnnx") ? cfg.getBool("trtDisableOnnx") : false);
108-
// ONNX transformer emitter layout. Default is NCHW (genuine channel-major attention/FFN): benchmarks
109-
// show it matches or slightly beats the NHWC bubble path at the saturated throughput operating point
110-
// KataGo runs at, and it's the simpler graph. trtTransformerNHWC=true opts into the NHWC path (whole
111-
// trunk channel-last with NCHW<->NHWC conversions around it), which wins on single-stream latency.
112-
context->transformerNHWC = cfg.contains("trtTransformerNHWC") ? cfg.getBool("trtTransformerNHWC") : false;
107+
// Transformer trunks default to NHWC, which benchmarks at least as fast as NCHW across tested GPUs
108+
// and TensorRT versions. Explicit false remains available for hardware-specific tuning. Normalize
109+
// convnets to false since the ONNX builder ignores this setting for models without transformers.
110+
context->transformerNHWC =
111+
(cfg.contains("trtTransformerNHWC") ? cfg.getBool("trtTransformerNHWC") : true) &&
112+
NeuralNet::getModelDesc(loadedModel).hasAnyTransformerBlocks();
113113
// Debugging: if set, the ONNX-emitter path dumps the emitted ONNX model and the built engine's
114114
// per-layer info (precision/format/tactic, via a detailed-profiling build + IEngineInspector) into
115115
// this directory. Files are disambiguated by board size, FP16/FP32, and exact/max NN-length so the

0 commit comments

Comments
 (0)