Support zero-element graphs (e.g. SDPA with batch size 0) - #404
Draft
Anerudhan wants to merge 1 commit into
Draft
Conversation
Graphs whose non-virtual output tensors are all zero-element (a dimension of size 0) are now detected during validate() and treated as no-ops: all build stages skip backend lowering, get_workspace_size() reports 0, and execute() returns success without launching work. The cuDNN backend does not accept 0-sized tensor dimensions, so previously such graphs failed at tensor creation with a generic CUDNN_STATUS_BAD_PARAM. Graphs that mix zero-element tensors with non-zero-element outputs (e.g. a matmul contracting over a 0-sized dimension) are rejected at validate() with a clear GRAPH_NOT_SUPPORTED message. Adds Graph::is_zero_element_graph() (C++) and pygraph.is_zero_element_graph() (Python), C++ and Python tests, and docs. Fixes NVIDIA#101 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #101
Problem
Building a graph containing a tensor with a 0-sized dimension (e.g. SDPA with
batch_size = 0, as hit by PyTorch's cuDNN SDPA backend in pytorch/pytorch#133780) fails at backend tensor creation with a genericCUDNN_STATUS_BAD_PARAM, because cuDNN backend tensor descriptors do not accept 0-sized dimensions.Solution
Detect zero-element graphs during
validate(), after shape inference has filled in all tensor dims. If the graph references at least one zero-element tensor and every non-virtual output tensor is zero-element, executing the graph could never write an output byte — so the whole graph is a well-defined no-op:build_operation_graph(),create_execution_plans()/create_execution_plan(),check_support(),build_plans()/build_plan_at_index(), andbuild()all succeed while skipping backend lowering entirely.get_workspace_size()reports 0.execute()(all overloads),autotune(), andwarmup()return success without launching work; variant-pack pointers are ignored (zero-element tensors may be null).populate_cuda_graph()leaves the provided CUDA graph empty (valid and instantiable);update_cuda_graph()is a no-op.Graph::is_zero_element_graph()(C++) andpygraph.is_zero_element_graph()(Python) report whether the graph was flagged.Graphs that mix zero-element tensors with non-zero-element outputs (e.g. matmul contracting over a 0-sized dimension, whose output would need zero-filling) are rejected at
validate()with a clearGRAPH_NOT_SUPPORTEDmessage instead of the late backendBAD_PARAM.Guarded limitations (clear errors instead of misbehavior):
serialize(std::vector<uint8_t>&)) errors out — there is no plan to serialize (previously this path would have indexedplans.execution_plans[-1]). JSON structure serialization is unaffected; a graph deserialized from JSON re-detects the no-op in its ownvalidate().override_uids/shapes/strides) on a no-op graph error out rather than silently skipping real work.Testing
test/cpp/validate.cpp([zero_element]): pointwise no-op pipeline (fully deviceless, includingexecutewith a null handle), SDPA with batch 0, and the mixed zero/non-zero rejection. Fulltestsbinary passes: 290 assertions in 27 test cases (cuDNN 9.19.1).test/python/test_zero_element_graph.py: SDPA batch 0 end-to-end via pygraph (mirrors the issue repro), pointwise zero-element, and mixed-graph rejection. Verified on GPU via the pygraph bindings.docs/zero-element-graphs.mddocuments the behavior; linked fromllms.txt.pre-commit(clang-format, black) clean on changed files.🤖 Generated with Claude Code