Skip to content

[ROCm] Fix GPU memory fault in batched_unary_embeddings backward - #6261

Closed
avbokovoy wants to merge 5 commits into
pytorch:mainfrom
ROCm:abokovoi/fix-batched-unary-embeddings-backward-test
Closed

avbokovoy wants to merge 5 commits into
pytorch:mainfrom
ROCm:abokovoi/fix-batched-unary-embeddings-backward-test

Conversation

@avbokovoy

Copy link
Copy Markdown
Contributor

Problem

batched_unary_embeddings_test.py::test_gpu crashed with a GPU memory access fault on ROCm. The permute sub-test passed
offsets of length 8 (7 segments) for a T=2 module, violating the op's offsets.numel() == T*B+1 contract. Forward silently truncated
B=3 and dropped the trailing segment; backward walked all 7 segments, deriving an out-of-range table id t=2 that indexed
grad_weight out of bounds.

Fix

  • Correct the test offsets to [0..8] (9 entries -> T*B+1 for B=4).
  • Add host-side TORCH_CHECK guards on the offsets length in forward (CPU/CUDA/HIP) and backward (CUDA/HIP), so malformed input fails loudly instead of faulting/corrupting grads.

@meta-codesync

meta-codesync Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

@q10 has imported this pull request. If you are a Meta employee, you can view this in D118492251.

@sylvesterkaczmarek sylvesterkaczmarek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The eager paths now reject malformed offsets, but batched_unary_embeddings_forward_meta still floor-divides (offsets.sym_numel() - 1) / T without enforcing the new T > 0 / divisibility invariant. FakeTensor/export can therefore accept an input shape that eager now rejects and report a misleading B. Could the Meta implementation mirror the new validation?

@pytorch-bot

pytorch-bot Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@avbokovoy

Copy link
Copy Markdown
Contributor Author

The eager paths now reject malformed offsets, but batched_unary_embeddings_forward_meta still floor-divides (offsets.sym_numel() - 1) / T without enforcing the new T > 0 / divisibility invariant. FakeTensor/export can therefore accept an input shape that eager now rejects and report a misleading B. Could the Meta implementation mirror the new validation?

Sure, done in df219f4. Added dedicated test in 99737ef

q10 added a commit to q10/FBGEMM that referenced this pull request Sep 17, 2026
Summary:
Pull Request resolved: pytorch#6318

X-link: https://github.com/facebookresearch/FBGEMM/pull/3192

NOTE: No linked task. Please associate a task with this diff.

## Motivation

`batched_unary_embeddings_test.py::test_gpu` was disabled on ROCm after its permute case triggered a GPU memory access fault. The failure came from malformed test input, but the operator accepted that input instead of reporting an error, allowing it to reach the backward kernel and access memory out of bounds.

## Root cause

The operator uses two offset tensors for different purposes. `table_offsets` separates the embedding tables in the concatenated weight tensor and determines the number of tables, `T`. `offsets` separates the input bags for every `(table, batch)` pair, so it must contain exactly `T * B + 1` entries.

The failing test created a module with `T = 2` and intended `B = 4`, but supplied only 8 offsets instead of 9. The forward path calculated `B` using integer division: `(8 - 1) / 2 = 3`. This silently discarded the incomplete final table row. The backward path still processed all seven encoded segments; with the truncated `B`, the final segment decoded to table index `t = 2`, outside the valid table range `[0, 2)`, and wrote beyond `grad_weight`. ROCm surfaced that invalid access as a memory fault.

## Fix

- Correct the test to provide offsets `[0, ..., 8]`, giving the required 9 entries for `T = 2` and `B = 4`.
- Validate positive dimensions and the `T * B + 1` offsets contract before deriving `B` in the CPU and CUDA forward paths. Mirror the contract in the Meta implementation so eager execution and tracing reject the same malformed shapes.
- Validate tensor sizes before narrowing them to the existing 32-bit kernel parameters, while keeping flattened address calculations in 64-bit arithmetic where the output can exceed `2^32` elements.
- Preserve CUDA backward compatibility with both `[N, sum_E]` and `[N, sum_E, 1]` weights, with a focused gradient regression for the 2-D layout.
- Re-enable the GPU test on ROCm and add CPU/GPU invalid-input, dynamic-shape, Meta, and large-grid regression coverage.

Imported from [facebookresearch/FBGEMM#6261](pytorch#6261), authored by Andrey Bokovoy.

Reviewed By: spcyppt, yvonne-lab

Differential Revision: D118492251
meta-codesync Bot pushed a commit that referenced this pull request Sep 17, 2026
Summary:
Pull Request resolved: #6318

X-link: https://github.com/facebookresearch/FBGEMM/pull/3192

NOTE: No linked task. Please associate a task with this diff.

## Motivation

`batched_unary_embeddings_test.py::test_gpu` was disabled on ROCm after its permute case triggered a GPU memory access fault. The failure came from malformed test input, but the operator accepted that input instead of reporting an error, allowing it to reach the backward kernel and access memory out of bounds.

## Root cause

The operator uses two offset tensors for different purposes. `table_offsets` separates the embedding tables in the concatenated weight tensor and determines the number of tables, `T`. `offsets` separates the input bags for every `(table, batch)` pair, so it must contain exactly `T * B + 1` entries.

The failing test created a module with `T = 2` and intended `B = 4`, but supplied only 8 offsets instead of 9. The forward path calculated `B` using integer division: `(8 - 1) / 2 = 3`. This silently discarded the incomplete final table row. The backward path still processed all seven encoded segments; with the truncated `B`, the final segment decoded to table index `t = 2`, outside the valid table range `[0, 2)`, and wrote beyond `grad_weight`. ROCm surfaced that invalid access as a memory fault.

## Fix

- Correct the test to provide offsets `[0, ..., 8]`, giving the required 9 entries for `T = 2` and `B = 4`.
- Validate positive dimensions and the `T * B + 1` offsets contract before deriving `B` in the CPU and CUDA forward paths. Mirror the contract in the Meta implementation so eager execution and tracing reject the same malformed shapes.
- Validate tensor sizes before narrowing them to the existing 32-bit kernel parameters, while keeping flattened address calculations in 64-bit arithmetic where the output can exceed `2^32` elements.
- Preserve CUDA backward compatibility with both `[N, sum_E]` and `[N, sum_E, 1]` weights, with a focused gradient regression for the 2-D layout.
- Re-enable the GPU test on ROCm and add CPU/GPU invalid-input, dynamic-shape, Meta, and large-grid regression coverage.

Imported from [facebookresearch/FBGEMM#6261](#6261), authored by Andrey Bokovoy.

Reviewed By: spcyppt, yvonne-lab

Differential Revision: D118492251

fbshipit-source-id: c00c9d4cb5a06af93f8d724f427272d2be685eb5
@q10

q10 commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

Closed in 4f8c2ef

@q10 q10 closed this Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants