Fix absent weights in native sparse permutation operators - #6341
Open
eric-gecheng wants to merge 1 commit into
Open
eric-gecheng wants to merge 1 commit into
eric-gecheng wants to merge 1 commit into
Conversation
Keep absent output weights as an empty optional instead of wrapping an undefined Tensor. Apply the same semantics to CUDA 1D/2D and legacy sparse permutations, and to the affected CPU implementations. Add native TorchScript regression coverage without Python autograd registrations, including chained permutations, vector/flat CUDA paths, empty inputs, and defined weights.
Contributor
|
@q10 has imported this pull request. If you are a Meta employee, you can view this in D120923861. |
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.
Sparse permutation operators can return an undefined
Tensorinstead ofNonefor absent weights. When a native TorchScript graph passes that result directly to another permutation, the second operator treats the weights as present and fails. On CUDA, this reproduces as:This affects both nonempty inputs and inputs whose jagged lengths are all zero, including empty-request model warmup. It is not specific to quantized embeddings; chained TorchRec KJT permutations are one way to expose it.
Cause and fix
The affected implementations declare
Tensor permuted_weights, assign it only when input weights are present, and return it as part of a tuple whose third element isstd::optional<Tensor>. Without weights, this constructs an engaged optional containing an undefined tensor rather thanstd::nullopt.Use
std::optional<Tensor>for the local output, matching the existing CPU 2D implementation:permute_sparse_featureson CPU/CUDA and in CPU 1D permutation.Regression coverage
The new test loads native operator libraries in fresh subprocesses without importing FBGEMM's Python registrations. Python autograd wrappers can normalize undefined tensors to
None, masking the bug in ordinary Python tests.It checks optional-weight presence inside TorchScript and feeds each output directly into a second permutation. Coverage includes six public entry points, int32/int64 inputs, nonempty/mixed/all-zero lengths, absent/present weights, preallocated outputs, and both forced CUDA kernel paths. Defined empty weights must remain tensors.
Validation
ufmt diff, Ruff E/F checks, copyright-header checks, andgit diff --checkpass.The full repository test suite and ROCm execution were not run locally.
Related downstream workaround: meta-pytorch/torchrec#4761.