Repository navigation
Implement the portable kernel for _embedding_bag.out - #23871
yenhao-huang wants to merge 6 commits into
Conversation
`_embedding_bag.out` had no portable kernel and was not listed for ATen mode
either.
All three modes are covered: sum, mean and max, with per-sample weights (sum
only, as ATen requires), `include_last_offset`, and `padding_idx` rows skipped
and left out of the bag counts. Indices and offsets may be Long or Int.
`scale_grad_by_freq` and `sparse` only matter to the backward pass and are
ignored.
The three auxiliary outputs exist for the backward pass, and the ATen CPU
kernel fills only what each mode needs, so this matches it rather than
inventing a shape: `offset2bag` is empty for sum and one entry per index
otherwise, `bag_size` stays zero for sum and holds the per-bag counts
otherwise, and `max_indices` is {num_bags, dim} holding the row each feature's
maximum came from for max, and a copy of `bag_size` for mean.
Tests compare every mode, per-sample weights, padding_idx, include_last_offset
and an empty bag against torch.ops.aten._embedding_bag, for all four outputs.
The portable suite is 1648 passed, 0 failed.
Fixes pytorch#11323
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/23871
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 9810885 with merge base 0486667 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@claude please review |
|
Claude finished @JakeStevens's task in 1m 19s —— View job Summary: One thing blocks this PR: the kernel never bounds-checks Detailed reviewPR Review: #23871SummaryThis PR adds a portable Security
Code Quality
Testing
RecommendationRequest Changes: the missing index bounds check is a memory-safety bug. The other items can be fixed in the same pass. |
…61016 Fix embedding bag validation and low-precision accumulation
Move offset and index validation into check_offsets_and_indices, fill offset2bag and bag_size in one pass over each bag, and keep the main loop to output and max index computation. Use promoteTypes and is_reduced_floating_point_v instead of hand-written equivalents. Outputs are unchanged except that bag_size and max_indices are now also filled when out is empty. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…61016 Simplify embedding bag kernel structure
|
@JakeSteven thanks for triggering the review. All points are addressed. S1. Indices read without a bounds check
C1. Offsets aren't validated
C2. Mixed index dtypes are accepted
C3. Half/BF16 lose precision
T1. Most supported inputs aren't tested
T2. No failure tests
All 27 |
Summary
Fixes #11323
ExecuTorch has standalone embedding and reduction kernels, but no portable kernel for
_embedding_bag.out. This adds a kernel that looks up embedding rows and reduces multiple bags in one operator call, supporting sum, mean, and max modes.The implementation supports per-sample weights for sum mode,
include_last_offset,padding_idx, and Int or Long indices and offsets. It also produces the auxiliary outputs in the shapes and forms expected by the ATen CPU operator.Algorithm
File changes
kernels/portable/cpu/op_embedding_bag.cpp: implements the portable kernel.kernels/aten/functions.yamlandkernels/portable/functions.yaml: declare and register_embedding_bag.out.kernels/test/op_embedding_bag_test.cpp: adds cases for sum, mean, max, weighted sum, padding, final offset, empty bags, and invalid per-sample weights.kernels/test/CMakeLists.txt,kernels/test/targets.bzl, andshim_et/xplat/executorch/kernels/portable/op_registration_util.bzl: include the kernel and tests in the build targets.Test plan
Ran the targeted portable kernel tests on Linux:
Result:
portable_kernels_testpassed; CTest reported 1/1 test target passed, 0 failed.cc @larryliu0820 @manuelcandales @JakeStevens @nil-is-all