onnxruntime
808ba88b - Wire max-shape inference into workspace estimation (#31613)

Commit
63 days ago
Wire max-shape inference into workspace estimation (#31613) ### Description This PR wires estimation-only input-shape hints into the two-level workspace-estimation flow described in #29775. It adds the `session.max_shape_override` session option: ```text input_ids:[8,4096];attention_mask:[8,4096] ``` ORT validates the configured graph-input shapes before cloning, applies them to a disposable shadow graph, runs recursive shape inference, and captures concrete intermediate `NodeArg` shapes without changing runtime input constraints or executable-graph metadata. The resolved shapes are consumed at: - **Level 1:** CUDA `MatMulNBits` workspace estimation during `GetCapability()`. - **Level 2:** `DeclareWorkspaceRequirements()` on constructed kernels during `FinalizeSessionState()`. Static graph shapes remain usable at Level 2 without configuring an override. Nodes whose inputs remain unresolved retain the existing dynamic allocation/estimation fallback. The shadow workflow: - Shares existing initializer `OrtValue`s instead of copying large weights. - Preserves model-local functions and dynamically generated schemas. - Captures main-graph and control-flow-subgraph shapes under separate graph identities. - Matches source and shadow control-flow nodes by stable identity instead of topological position. - Reuses results when the graph is unchanged and rebuilds them after layout, fusion, or partitioning mutations. - Converts shadow-model construction and resolution exceptions to `Status`. Invalid overrides fail with explicit errors, including unknown inputs, rank mismatches, static-dimension conflicts, inconsistent symbolic dimensions, empty entries, and malformed dimension lists. Minimal builds warn and ignore the option rather than failing session initialization. The inferred downstream shapes are estimation hints, not guaranteed upper bounds: operator shape transformations are not necessarily monotonic. Future preallocation must retain runtime bounds checks and allocation fallbacks. Level-2 declarations are currently observational. Workspace offset planning and preallocation remain follow-up work. ### Motivation and Context Dynamic transformer models commonly retain symbolic batch and sequence dimensions, preventing workspace requirements from being calculated before execution. Deployment shape hints make partition-time budgeting and post-kernel workspace declaration possible while preserving dynamic runtime behavior. The isolated shadow graph prevents optimizers and runtime validation from treating those hints as the model's static shape contract. This is part of the constrained-environment memory roadmap tracked by #29775 and documented in `docs/annotated_partitioning/future_directions_constrained_env.md`. Kernels that do not implement `DeclareWorkspaceRequirements()` retain the default no-op and continue using their existing scratch-allocation path. ### Testing - Built the Debug `onnxruntime_test_all` target on Windows. - Ran all 20 `MaxShapeOverride.*` tests. - Added coverage for parsing, malformed separators, zero extents, shape propagation, canonical-graph isolation, invalid overrides, and stable matching of parallel control-flow subgraphs. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 1ceed57f-bf2a-45c7-a865-05bc6c4a598f Copilot-Session: 3915a111-4266-46e9-9922-3f26d253bbf8
Author
Parents
Loading