Skip to content

[ROCm] Fix bfe() bit-field extraction in the quantized CUDA EmbeddingBag kernel (#192571) - #192571

Closed
zidongyang wants to merge 1 commit into
pytorch:mainfrom
zidongyang:export-D115065161
Closed

zidongyang wants to merge 1 commit into
pytorch:mainfrom
zidongyang:export-D115065161

Conversation

@zidongyang

@zidongyang zidongyang commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

bfe() in aten/src/ATen/native/quantized/cuda/EmbeddingBag.cu extracts the
[pos, pos+len) bit field of a packed word and returns it as a float. It is the
inner primitive of dequantize_intx(), so every
quantized::embedding_bag_{byte,4bit}_rowwise_offsets call on a GPU goes
through it.

The #ifdef USE_ROCM branch read:

return *reinterpret_cast<float*>((val >> pos) && ((1u << len) - 1u));

which has two defects:

  • && (logical AND) is used where & (bitwise AND) was intended, so the
    expression collapses to 0 or 1 instead of the requested bit field.
  • the resulting bool is then reinterpreted as a float* and dereferenced,
    i.e. a load from address 0 or 1.

This is not a subtle numerical issue: on an AMD GPU the null load aborts the
process with

Memory access fault by GPU node-2 (Agent handle: 0x...) on address (nil).

so quantized embedding-bag lookups could never have run on ROCm. The line has
been unchanged since the file was added in July 2021.

Use the bitwise AND and convert with __uint2float_rn, which yields the same
value the CUDA branch computes with the bfe.u32 PTX instruction. Both branches
end in that same conversion, so only the bit-field extraction itself stays under
the #ifdef and the conversion is shared between them. NVIDIA codegen and the
CPU implementations are unchanged.

The explanatory comment carried the same && typo and is most likely where the
ROCm expression was copied from; it is corrected and hoisted to describe the
function as a whole rather than just one branch.

Test Plan:
New test test_embedding_bag_rowwise_offsets_cuda in
test/quantization/core/test_quantized_op.py runs
quantized::embedding_bag_byte_rowwise_offsets and
quantized::embedding_bag_4bit_rowwise_offsets on GPU and asserts the output is
finite and agrees with the CPU implementation. Both bit widths are covered
because bfe() is shared by them through dequantize_intx(), with a different
len.

python test/quantization/core/test_quantized_op.py -k test_embedding_bag_rowwise_offsets_cuda

Run on an AMD GPU (ROCm): passes. The whole TestQuantizedEmbeddingOps class
passes as well (7 tests).

As a negative control, reverting just this fix makes the new test abort with the
GPU memory fault quoted in the summary, so the test does pin the behaviour it
claims to.

Differential Revision: D115065161

cc @jeffdaily @sunway513 @jithunnair-amd @pruthvistony @ROCmSupport @jataylo @hongxiayang @naromero77amd @pragupta @jerrymannil @xinyazhang

@zidongyang
zidongyang requested a review from jerryzh168 as a code owner August 8, 2026 00:46
@pytorch-bot

pytorch-bot Bot commented Aug 8, 2026

Copy link
Copy Markdown

This appears to be a diff that was exported from phabricator, but the PR author does not have sufficient permissions to run CI. @zidongyang, please do step 2 of internal wiki to get write access so you do not need to get CI approvals in the future. If you think this is a mistake, please contact the Pytorch Dev Infra team.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 8, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: zidongyang / name: Zidong Yang (74fb38d)

@pytorch-bot pytorch-bot Bot added module: rocm AMD GPU support for Pytorch release notes: quantization release notes category labels Aug 8, 2026
@pytorch-bot

pytorch-bot Bot commented Aug 8, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/192571

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 8cb7340 with merge base 4c5b5ab (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-codesync

meta-codesync Bot commented Aug 8, 2026

Copy link
Copy Markdown

@zidongyang has exported this pull request. If you are a Meta employee, you can view the originating Diff in D115065161.

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Aug 8, 2026
@meta-codesync meta-codesync Bot changed the title [ROCm] Fix bfe() bit-field extraction in the quantized CUDA EmbeddingBag kernel [ROCm] Fix bfe() bit-field extraction in the quantized CUDA EmbeddingBag kernel (#192571) Aug 8, 2026
zidongyang added a commit to zidongyang/pytorch that referenced this pull request Aug 8, 2026
…Bag kernel (pytorch#192571)

Summary:

`bfe()` in aten/src/ATen/native/quantized/cuda/EmbeddingBag.cu extracts the
[pos, pos+len) bit field of a packed word and returns it as a float. It is the
inner primitive of `dequantize_intx()`, so every
`quantized::embedding_bag_{byte,4bit}_rowwise_offsets` call on a GPU goes
through it.

The `#ifdef USE_ROCM` branch read:

    return *reinterpret_cast<float*>((val >> pos) && ((1u << len) - 1u));

which has two defects:

- `&&` (logical AND) is used where `&` (bitwise AND) was intended, so the
  expression collapses to 0 or 1 instead of the requested bit field.
- the resulting `bool` is then reinterpreted as a `float*` and dereferenced,
  i.e. a load from address 0 or 1.

This is not a subtle numerical issue: on an AMD GPU the null load aborts the
process with

    Memory access fault by GPU node-2 (Agent handle: 0x...) on address (nil).

so quantized embedding-bag lookups could never have run on ROCm. The line has
been unchanged since the file was added in July 2021.

Use the bitwise AND and convert with `__uint2float_rn`, which yields the same
value the CUDA branch computes with the `bfe.u32` PTX instruction. Both branches
end in that same conversion, so only the bit-field extraction itself stays under
the `#ifdef` and the conversion is shared between them. NVIDIA codegen and the
CPU implementations are unchanged.

The explanatory comment carried the same `&&` typo and is most likely where the
ROCm expression was copied from; it is corrected and hoisted to describe the
function as a whole rather than just one branch.

Test Plan:
New test `test_embedding_bag_rowwise_offsets_cuda` in
`test/quantization/core/test_quantized_op.py` runs
`quantized::embedding_bag_byte_rowwise_offsets` and
`quantized::embedding_bag_4bit_rowwise_offsets` on GPU and asserts the output is
finite and agrees with the CPU implementation. Both bit widths are covered
because `bfe()` is shared by them through `dequantize_intx()`, with a different
`len`.

    python test/quantization/core/test_quantized_op.py -k test_embedding_bag_rowwise_offsets_cuda

Run on an AMD GPU (ROCm): passes. The whole `TestQuantizedEmbeddingOps` class
passes as well (7 tests).

As a negative control, reverting just this fix makes the new test abort with the
GPU memory fault quoted in the summary, so the test does pin the behaviour it
claims to.

Differential Revision: D115065161
zidongyang added a commit to zidongyang/pytorch that referenced this pull request Aug 8, 2026
…Bag kernel (pytorch#192571)

Summary:

`bfe()` in aten/src/ATen/native/quantized/cuda/EmbeddingBag.cu extracts the
[pos, pos+len) bit field of a packed word and returns it as a float. It is the
inner primitive of `dequantize_intx()`, so every
`quantized::embedding_bag_{byte,4bit}_rowwise_offsets` call on a GPU goes
through it.

The `#ifdef USE_ROCM` branch read:

    return *reinterpret_cast<float*>((val >> pos) && ((1u << len) - 1u));

which has two defects:

- `&&` (logical AND) is used where `&` (bitwise AND) was intended, so the
  expression collapses to 0 or 1 instead of the requested bit field.
- the resulting `bool` is then reinterpreted as a `float*` and dereferenced,
  i.e. a load from address 0 or 1.

This is not a subtle numerical issue: on an AMD GPU the null load aborts the
process with

    Memory access fault by GPU node-2 (Agent handle: 0x...) on address (nil).

so quantized embedding-bag lookups could never have run on ROCm. The line has
been unchanged since the file was added in July 2021.

Use the bitwise AND and convert with `__uint2float_rn`, which yields the same
value the CUDA branch computes with the `bfe.u32` PTX instruction. Both branches
end in that same conversion, so only the bit-field extraction itself stays under
the `#ifdef` and the conversion is shared between them. NVIDIA codegen and the
CPU implementations are unchanged.

The explanatory comment carried the same `&&` typo and is most likely where the
ROCm expression was copied from; it is corrected and hoisted to describe the
function as a whole rather than just one branch.

Test Plan:
New test `test_embedding_bag_rowwise_offsets_cuda` in
`test/quantization/core/test_quantized_op.py` runs
`quantized::embedding_bag_byte_rowwise_offsets` and
`quantized::embedding_bag_4bit_rowwise_offsets` on GPU and asserts the output is
finite and agrees with the CPU implementation. Both bit widths are covered
because `bfe()` is shared by them through `dequantize_intx()`, with a different
`len`.

    python test/quantization/core/test_quantized_op.py -k test_embedding_bag_rowwise_offsets_cuda

Run on an AMD GPU (ROCm): passes. The whole `TestQuantizedEmbeddingOps` class
passes as well (7 tests).

As a negative control, reverting just this fix makes the new test abort with the
GPU memory fault quoted in the summary, so the test does pin the behaviour it
claims to.

Differential Revision: D115065161
…Bag kernel (pytorch#192571)

Summary:
Pull Request resolved: pytorch#192571

`bfe()` in aten/src/ATen/native/quantized/cuda/EmbeddingBag.cu extracts the
[pos, pos+len) bit field of a packed word and returns it as a float. It is the
inner primitive of `dequantize_intx()`, so every
`quantized::embedding_bag_{byte,4bit}_rowwise_offsets` call on a GPU goes
through it.

The `#ifdef USE_ROCM` branch read:

    return *reinterpret_cast<float*>((val >> pos) && ((1u << len) - 1u));

which has two defects:

- `&&` (logical AND) is used where `&` (bitwise AND) was intended, so the
  expression collapses to 0 or 1 instead of the requested bit field.
- the resulting `bool` is then reinterpreted as a `float*` and dereferenced,
  i.e. a load from address 0 or 1.

This is not a subtle numerical issue: on an AMD GPU the null load aborts the
process with

    Memory access fault by GPU node-2 (Agent handle: 0x...) on address (nil).

so quantized embedding-bag lookups could never have run on ROCm. The line has
been unchanged since the file was added in July 2021.

Use the bitwise AND and convert with `__uint2float_rn`, which yields the same
value the CUDA branch computes with the `bfe.u32` PTX instruction. Both branches
end in that same conversion, so only the bit-field extraction itself stays under
the `#ifdef` and the conversion is shared between them. NVIDIA codegen and the
CPU implementations are unchanged.

The explanatory comment carried the same `&&` typo and is most likely where the
ROCm expression was copied from; it is corrected and hoisted to describe the
function as a whole rather than just one branch.

Test Plan:
New test `test_embedding_bag_rowwise_offsets_cuda` in
`test/quantization/core/test_quantized_op.py` runs
`quantized::embedding_bag_byte_rowwise_offsets` and
`quantized::embedding_bag_4bit_rowwise_offsets` on GPU and asserts the output is
finite and agrees with the CPU implementation. Both bit widths are covered
because `bfe()` is shared by them through `dequantize_intx()`, with a different
`len`.

    python test/quantization/core/test_quantized_op.py -k test_embedding_bag_rowwise_offsets_cuda

Run on an AMD GPU (ROCm): passes. The whole `TestQuantizedEmbeddingOps` class
passes as well (7 tests).

As a negative control, reverting just this fix makes the new test abort with the
GPU memory fault quoted in the summary, so the test does pin the behaviour it
claims to.

Differential Revision: D115065161
zidongyang added a commit to zidongyang/pytorch that referenced this pull request Aug 8, 2026
…Bag kernel (pytorch#192571)

Summary:

`bfe()` in aten/src/ATen/native/quantized/cuda/EmbeddingBag.cu extracts the
[pos, pos+len) bit field of a packed word and returns it as a float. It is the
inner primitive of `dequantize_intx()`, so every
`quantized::embedding_bag_{byte,4bit}_rowwise_offsets` call on a GPU goes
through it.

The `#ifdef USE_ROCM` branch read:

    return *reinterpret_cast<float*>((val >> pos) && ((1u << len) - 1u));

which has two defects:

- `&&` (logical AND) is used where `&` (bitwise AND) was intended, so the
  expression collapses to 0 or 1 instead of the requested bit field.
- the resulting `bool` is then reinterpreted as a `float*` and dereferenced,
  i.e. a load from address 0 or 1.

This is not a subtle numerical issue: on an AMD GPU the null load aborts the
process with

    Memory access fault by GPU node-2 (Agent handle: 0x...) on address (nil).

so quantized embedding-bag lookups could never have run on ROCm. The line has
been unchanged since the file was added in July 2021.

Use the bitwise AND and convert with `__uint2float_rn`, which yields the same
value the CUDA branch computes with the `bfe.u32` PTX instruction. Both branches
end in that same conversion, so only the bit-field extraction itself stays under
the `#ifdef` and the conversion is shared between them. NVIDIA codegen and the
CPU implementations are unchanged.

The explanatory comment carried the same `&&` typo and is most likely where the
ROCm expression was copied from; it is corrected and hoisted to describe the
function as a whole rather than just one branch.

Test Plan:
New test `test_embedding_bag_rowwise_offsets_cuda` in
`test/quantization/core/test_quantized_op.py` runs
`quantized::embedding_bag_byte_rowwise_offsets` and
`quantized::embedding_bag_4bit_rowwise_offsets` on GPU and asserts the output is
finite and agrees with the CPU implementation. Both bit widths are covered
because `bfe()` is shared by them through `dequantize_intx()`, with a different
`len`.

    python test/quantization/core/test_quantized_op.py -k test_embedding_bag_rowwise_offsets_cuda

Run on an AMD GPU (ROCm): passes. The whole `TestQuantizedEmbeddingOps` class
passes as well (7 tests).

As a negative control, reverting just this fix makes the new test abort with the
GPU memory fault quoted in the summary, so the test does pin the behaviour it
claims to.

Differential Revision: D115065161
zidongyang added a commit to zidongyang/pytorch that referenced this pull request Aug 8, 2026
…Bag kernel (pytorch#192571)

Summary:
Pull Request resolved: pytorch#192571

`bfe()` in aten/src/ATen/native/quantized/cuda/EmbeddingBag.cu extracts the
[pos, pos+len) bit field of a packed word and returns it as a float. It is the
inner primitive of `dequantize_intx()`, so every
`quantized::embedding_bag_{byte,4bit}_rowwise_offsets` call on a GPU goes
through it.

The `#ifdef USE_ROCM` branch read:

    return *reinterpret_cast<float*>((val >> pos) && ((1u << len) - 1u));

which has two defects:

- `&&` (logical AND) is used where `&` (bitwise AND) was intended, so the
  expression collapses to 0 or 1 instead of the requested bit field.
- the resulting `bool` is then reinterpreted as a `float*` and dereferenced,
  i.e. a load from address 0 or 1.

This is not a subtle numerical issue: on an AMD GPU the null load aborts the
process with

    Memory access fault by GPU node-2 (Agent handle: 0x...) on address (nil).

so quantized embedding-bag lookups could never have run on ROCm. The line has
been unchanged since the file was added in July 2021.

Use the bitwise AND and convert with `__uint2float_rn`, which yields the same
value the CUDA branch computes with the `bfe.u32` PTX instruction. Both branches
end in that same conversion, so only the bit-field extraction itself stays under
the `#ifdef` and the conversion is shared between them. NVIDIA codegen and the
CPU implementations are unchanged.

The explanatory comment carried the same `&&` typo and is most likely where the
ROCm expression was copied from; it is corrected and hoisted to describe the
function as a whole rather than just one branch.

Test Plan:
New test `test_embedding_bag_rowwise_offsets_cuda` in
`test/quantization/core/test_quantized_op.py` runs
`quantized::embedding_bag_byte_rowwise_offsets` and
`quantized::embedding_bag_4bit_rowwise_offsets` on GPU and asserts the output is
finite and agrees with the CPU implementation. Both bit widths are covered
because `bfe()` is shared by them through `dequantize_intx()`, with a different
`len`.

    python test/quantization/core/test_quantized_op.py -k test_embedding_bag_rowwise_offsets_cuda

Run on an AMD GPU (ROCm): passes. The whole `TestQuantizedEmbeddingOps` class
passes as well (7 tests).

As a negative control, reverting just this fix makes the new test abort with the
GPU memory fault quoted in the summary, so the test does pin the behaviour it
claims to.

Differential Revision: D115065161
@meta-codesync

meta-codesync Bot commented Aug 9, 2026

Copy link
Copy Markdown

@pytorchbot merge

(Initiating merge automatically since Phabricator Diff has merged)

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

ankitbhrdwj pushed a commit to ankitbhrdwj/pytorch that referenced this pull request Aug 12, 2026
…Bag kernel (pytorch#192571) (pytorch#192571)

Summary:

`bfe()` in aten/src/ATen/native/quantized/cuda/EmbeddingBag.cu extracts the
[pos, pos+len) bit field of a packed word and returns it as a float. It is the
inner primitive of `dequantize_intx()`, so every
`quantized::embedding_bag_{byte,4bit}_rowwise_offsets` call on a GPU goes
through it.

The `#ifdef USE_ROCM` branch read:

    return *reinterpret_cast<float*>((val >> pos) && ((1u << len) - 1u));

which has two defects:

- `&&` (logical AND) is used where `&` (bitwise AND) was intended, so the
  expression collapses to 0 or 1 instead of the requested bit field.
- the resulting `bool` is then reinterpreted as a `float*` and dereferenced,
  i.e. a load from address 0 or 1.

This is not a subtle numerical issue: on an AMD GPU the null load aborts the
process with

    Memory access fault by GPU node-2 (Agent handle: 0x...) on address (nil).

so quantized embedding-bag lookups could never have run on ROCm. The line has
been unchanged since the file was added in July 2021.

Use the bitwise AND and convert with `__uint2float_rn`, which yields the same
value the CUDA branch computes with the `bfe.u32` PTX instruction. Both branches
end in that same conversion, so only the bit-field extraction itself stays under
the `#ifdef` and the conversion is shared between them. NVIDIA codegen and the
CPU implementations are unchanged.

The explanatory comment carried the same `&&` typo and is most likely where the
ROCm expression was copied from; it is corrected and hoisted to describe the
function as a whole rather than just one branch.

Test Plan:
New test `test_embedding_bag_rowwise_offsets_cuda` in
`test/quantization/core/test_quantized_op.py` runs
`quantized::embedding_bag_byte_rowwise_offsets` and
`quantized::embedding_bag_4bit_rowwise_offsets` on GPU and asserts the output is
finite and agrees with the CPU implementation. Both bit widths are covered
because `bfe()` is shared by them through `dequantize_intx()`, with a different
`len`.

    python test/quantization/core/test_quantized_op.py -k test_embedding_bag_rowwise_offsets_cuda

Run on an AMD GPU (ROCm): passes. The whole `TestQuantizedEmbeddingOps` class
passes as well (7 tests).

As a negative control, reverting just this fix makes the new test abort with the
GPU memory fault quoted in the summary, so the test does pin the behaviour it
claims to.

Differential Revision: D115065161

Pull Request resolved: pytorch#192571
Approved by: https://github.com/Skylion007, https://github.com/vadimkantorov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged meta-exported module: rocm AMD GPU support for Pytorch release notes: quantization release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants