AI Integration Problems May 20, 2026

Unity Sentis 2.0 ShapeInferenceException on Batched ONNX After 2026 Q2 SDK Update - Fix

Fix Unity Sentis 2.0 ShapeInferenceException when loading batched ONNX models after the 2026 Q2 SDK update. Opset 17 export, dynamic batch dimensions, Resize node cleanup, and Sentis.Functional.Compile verification.

By GamineAI Team

Unity Sentis 2.0 ShapeInferenceException on Batched ONNX After 2026 Q2 SDK Update - Fix

Problem: After updating to Unity Sentis 2.0 (2026 Q2 SDK), Worker.Schedule(inputTensor) throws:

ShapeInferenceException: Cannot infer shape for node <Resize> (or Conv / MatMul)

The same ONNX worked in Sentis 1.x or in Python onnxruntime with batch size > 1.

Who is affected now: Teams shipping batched NPC intent classifiers, batch-4 vision encoders, or multi-sample style transfer nets exported from PyTorch 2.4+ in 2026 — Sentis 2.0 tightened supported ONNX opsets and is stricter about undeclared dynamic batch dimensions.

Fastest safe fix: Re-export with opset 17, explicit dynamic_axes on batch dim 0, run Sentis.Functional.Compile on a (1, …) tensor before Schedule, and strip unsupported Resize coordinate modes with onnx-graphsurgeon.

Direct answer

Sentis builds an internal shape graph at import time. If the ONNX omits a dynamic batch annotation, uses opset > 17 features Sentis 2.0 rejects, or leaves a Resize node in tf_crop_and_resize mode, shape inference fails before the first kernel runs. Fix the export — do not patch around with random tensor reshapes in Unity unless you have verified the graph in Netron.

Why this issue spikes in 2026

  1. Sentis 2.0 shipped with the 2026 Q2 AI Inference package line — stricter ONNX validation than 1.x.
  2. PyTorch 2.4+ default exporters emit newer ops and folding patterns.
  3. Tutorials still show batch-1-only exports while gameplay code passes variable batch tensors at runtime.
  4. CI often tests ONNX in Python but never loads the asset in Sentis on the build agent.

Pair with Unity Sentis or ONNX Model Import Failed for import-time errors, Ollama local LLM fallback hang for dialogue stack siblings, and 15 Free LLM-Driven NPC Dialogue resources.

Symptoms and phrases to match

  • Exception on first Worker.Schedule, not at import.
  • Works when you force batch 1; fails when batch 4 or 8.
  • Netron shows opset 18 or dynamic rank without named axis.
  • Resize node highlighted in the Sentis import log.
  • Editor OK, player build fails (different stripping) — still usually export graph.

Root causes (check in this order)

  1. Opset too new — Sentis 2.0 targets ONNX opset ≤ 17 for many graphs.
  2. Missing dynamic batch — exporter froze batch at 1; runtime tensor is N×C×H×W.
  3. Unsupported Resize modecoordinate_transformation_mode not half_pixel / align_corners Sentis supports.
  4. Constant folding removed shape opsShape / Gather chain missing after onnxsim.
  5. Wrong input nameSchedule binds "input" but graph expects "pixel_values".

Fastest safe fix path

Step 1 — Re-export from PyTorch with opset 17 and dynamic batch

import torch

model.eval()
dummy = torch.randn(1, 3, 224, 224)  # adjust to your tensor layout

torch.onnx.export(
    model,
    dummy,
    "npc_intent_v1.onnx",
    opset_version=17,
    input_names=["input"],
    output_names=["logits"],
    dynamic_axes={
        "input": {0: "batch"},
        "logits": {0: "batch"},
    },
)

Rules: stay on opset 17 until Sentis release notes say otherwise; name batch axis explicitly.

Step 2 — Inspect in Netron before Unity import

  • Confirm opset_import shows 17.
  • Search for Resize — note coordinate_transformation_mode.
  • Verify input rank 4 and dim 0 is symbolic or dynamic.

Step 3 — Clean Resize nodes (if present)

pip install onnx-graphsurgeon
python -c "
import onnx_graphsurgeon as gs
import onnx
graph = gs.import_onnx(onnx.load('npc_intent_v1.onnx'))
for node in graph.nodes:
    if node.op == 'Resize':
        mode = node.attrs.get('coordinate_transformation_mode', '')
        if mode not in ('half_pixel', 'align_corners', 'asymmetric'):
            node.attrs['coordinate_transformation_mode'] = 'half_pixel'
graph.cleanup().toposort()
onnx.save(gs.export_onnx(graph), 'npc_intent_v1_clean.onnx')
"

Re-import npc_intent_v1_clean.onnx into Unity as ModelAsset.

Step 4 — Compile-check in Sentis before Schedule

using Unity.Sentis;
using UnityEngine;

public class SentisSmokeTest : MonoBehaviour
{
    public ModelAsset modelAsset;

    void Start()
    {
        var model = ModelLoader.Load(modelAsset);
        var inputShape = new TensorShape(1, 3, 224, 224); // smoke batch 1
        using var input = new Tensor<float>(inputShape);
        var graph = Functional.Compile(model, Functional.Input(model, 0));
        var output = graph.Forward(input);
        Debug.Log($"Compile+Forward OK, output shape: {output.shape}");
    }
}

If compile fails, shape inference never reaches Worker — fix ONNX before scheduling.

Step 5 — Worker.Schedule with matching batch

using var worker = new Worker(model, BackendType.GPUCompute);
var shape = new TensorShape(batchSize, 3, 224, 224);
using var tensor = new Tensor<float>(shape);
worker.Schedule(tensor);
worker.FlushSchedule();
var output = worker.PeekOutput() as Tensor<float>;

Do not pass batch 4 if the graph was exported without dynamic axis — re-export instead.

Step 6 — Fallback: single-batch inference path

If hotfix deadline blocks re-export:

  • Loop batch 1 calls in C# (slower but unblocks ship).
  • Gate feature flag sentis_batched_inference = false until ONNX v2 lands.

Document the perf hit in release notes.

Verification checklist

  • [ ] Netron shows opset 17 and dynamic batch on dim 0.
  • [ ] Functional.Compile + Forward succeed in Editor with batch 1.
  • [ ] Worker.Schedule with batch 4 completes in < 50 ms on target GPU (adjust for your model).
  • [ ] Player build loads same ModelAsset without backend mismatch.
  • [ ] CI step imports ONNX with Sentis 2.0 package locked in Packages/manifest.json.

Prevention

  • Pin com.unity.sentis version in manifest; bump only with a re-export checklist.
  • Add CI onnx smoke: load every shipping .onnx with ModelLoader.Load + compile.
  • Ban onnxsim passes that strip Shape ops without Sentis verification.
  • Keep export script in repo beside the .onnx (reproducible graphs).
  • Document max batch in ScriptableObject next to the model reference.

Troubleshooting table

Symptom Likely cause Fix
Fails only batch > 1 No dynamic_axes Re-export PyTorch
Resize in stack trace Bad transform mode graphsurgeon patch
Import OK, schedule fails Opset 18 ops Downgrade to opset 17
GPU fail, CPU works Backend bug on op Try BackendType.CPU isolate
Shape off by 1 on H/W NCHW vs NHWC Fix export dummy layout

Frequently asked questions

Can I fix this only in Unity with Reshape nodes?

Only after Sentis accepts the graph. Usually you must fix ONNX — Unity reshapes cannot repair failed global shape inference.

Is opset 18 ever supported?

Check current Sentis release notes — default safe path in 2026 is 17.

Does this affect Barracuda migrations?

Different package. This article is Sentis 2.0 only.

Batch size 0?

Invalid — guard gameplay code; Sentis expects positive batch.

Related help and resources

Bookmark this page when batched ONNX breaks on the first Schedule after a Sentis 2.0 upgrade — the fix is almost always export discipline, not a random Unity bug.