Skip to content

[ET-VK][Ops] dequantization op shaders and impl #11768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2025
Merged

Conversation

pytorchbot
Copy link
Collaborator

This PR was created by the merge bot to help merge the original PR into the main branch.
ghstack PR number: #11483 by @ahmtox
^ Please use this as the source of truth for the PR details, comments, and reviews
ghstack PR base: https://github.com/pytorch/executorch/tree/gh/ahmtox/16/base
ghstack PR head: https://github.com/pytorch/executorch/tree/gh/ahmtox/16/head
Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/ahmtox/11/orig
Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/ahmtox/16/orig
@diff-train-skip-merge

morelos added 2 commits June 17, 2025 10:06
Pull Request resolved: #11369

# Operator Description

The quantization operator converts floating-point tensors (fp16/fp32) to lower-precision integer formats (uint8/int8/int32) using affine quantization. This operator supports two quantization modes:

- **Per-tensor quantization**: Uses a single scale and zero_point for the entire tensor
- **Per-token quantization**: Uses different scale and zero_point values for each "token" (typically rows or channels)

The quantization formula is: `quantized_value = clamp(round(input_value / scale) + zero_point, quant_min, quant_max)`

**Example**: For a float value `2.5` with `scale=0.1`, `zero_point=128`, `quant_min=0`, `quant_max=255`:
- `round(2.5 / 0.1) + 128 = round(25) + 128 = 153`
- `clamp(153, 0, 255) = 153` (uint8 output)

The quantization parameters serve these purposes:
- **scale**: Controls the granularity of quantization (smaller scale = finer precision)
- **zero_point**: Maps the floating-point zero to an integer value
- **quant_min/quant_max**: Define the valid range for the quantized output type

# Shader Algorithm Overview

## Texture Storage Implementation (`quantize_texture.glsl`)

The texture-based implementation operates on 3D textures where data is stored in RGBA texel format (4 components per texel):

**Per-tensor Mode**:
Each compute thread processes one texel position. It loads a 4-component texel from the input texture, and applies quantization to each of the 4 components using shared scale/zero_point. It then writes the quantized 4-component result to the output texture. This method is fairly linear.

**Per-token Mode**:
We need to calculate the token index based on the spatial position, it'll differ between various cases like 3D and 2D. For instand we might define the token_idx as `z * dims.y + y` for 3D, or just `y` for 2D cases. We then retrieve the per-token scale/zero_point from the texture storage according to the token_idx. We need to do component indexing based on the texel_idx and token_idx: `texel_idx = token_idx / 4`, along with the component id `comp_idx = token_idx % 4` to get the necessary scale/zero_point. We then apply quantization with the corresponding token-specific parameters to the 4 components of the current texel.

## Buffer Storage Implementation (`quantize_buffer.glsl`)

The buffer-based implementation operates on linear memory buffers with stride-based indexing:

**Per-tensor Mode**:
In this case, each compute thread will process one element at its global position. It converts the 3D position to linear buffer indices using stride calculations `tidx_to_bufi(pos, strides)`. It then loads single scalar values from the input buffer and applies quantization using shared scale/zero_point parameters. We then store the quantized result to the output buffer at the corresponding index.

**Per-token Mode**:
We first calculate the logical tensor position from the linear buffer index through dimension unwrapping. We then determine the token index based on the tensor dimensionality:
   - 4D: `token_idx = w * (z * y) + z * y + y`
   - 3D: `token_idx = z * y + y`
   - 2D: `token_idx = y`
We then directly index into scale/zero_point buffers using token_idx and also apply quantization with the token-specific parameters.

# Performance Considerations / Future Improvements

Current implementation uses default workgroup sizing. Profiling different local workgroup sizes could improve occupancy and cache utilization. Buffer implementation processes one element per thread. Could be optimized to process multiple elements per thread.

NOTE: Currently the only input types supported are **half** (fp16) and **float** (fp32). The only output types supported are **byte** (uint8), **char** (int8), **int** (int32). A future diff plans to implement **double** (fp64) input dtype support.
ghstack-source-id: 291010148
@exported-using-ghexport

Differential Revision: [D75959064](https://our.internmc.facebook.com/intern/diff/D75959064/)
Pull Request resolved: #11483

# Operator Description

The dequantization operator converts lower-precision integer tensors (uint8/int8/int32) back to floating-point formats (fp16/fp32) using affine dequantization. This operator supports two dequantization modes:

- **Per-tensor dequantization**: Uses a single scale and zero_point for the entire tensor
- **Per-token dequantization**: Uses different scale and zero_point values for each "token" (typically rows or channels)

The dequantization formula is: `dequantized_value = (quantized_value - zero_point) * scale`

**Example**: For a quantized uint8 value `153` with `scale=0.1`, `zero_point=128`:
- `(153 - 128) * 0.1 = 25 * 0.1 = 2.5` (float output)

The dequantization parameters serve these purposes:
- **scale**: Controls the granularity of reconstruction (same scale used during quantization)
- **zero_point**: Maps the integer zero representation back to floating-point zero
- **quant_min/quant_max**: Define the valid range that was used during original quantization (for validation)

# Shader Algorithm Overview

## Texture Storage Implementation (`dequantize_texture.glsl`)

The texture-based implementation operates on 3D textures where data is stored in RGBA texel format (4 components per texel):

**Per-tensor Mode**:
Each compute thread processes one texel position. It loads a 4-component integer texel from the input texture, and applies dequantization to each of the 4 components using shared scale/zero_point parameters. It then writes the dequantized 4-component floating-point result to the output texture. This method processes all components uniformly with the same dequantization parameters.

**Per-token Mode**:
We need to calculate the token index based on the spatial position, it'll differ between various cases like 3D and 2D. For instance we might define the token_idx as `z * dims.y + y` for 3D, or just `y` for 2D cases. We then retrieve the per-token scale/zero_point from the texture storage according to the token_idx. We need to do component indexing based on the texel_idx and token_idx: `texel_idx = token_idx / 4`, along with the component id `comp_idx = token_idx % 4` to get the necessary scale/zero_point values. We then apply dequantization with the corresponding token-specific parameters to the 4 components of the current texel, converting each integer component to its floating-point representation.

## Buffer Storage Implementation (`dequantize_buffer.glsl`)

The buffer-based implementation operates on linear memory buffers with stride-based indexing:

**Per-tensor Mode**:
In this case, each compute thread will process one element at its global position. It converts the 3D position to linear buffer indices using stride calculations `tidx_to_bufi(pos, strides)`. It then loads single quantized integer values from the input buffer and applies dequantization using shared scale/zero_point parameters. We then store the dequantized floating-point result to the output buffer at the corresponding index.

**Per-token Mode**:
We first calculate the logical tensor position from the linear buffer index through dimension unwrapping. We then determine the token index based on the tensor dimensionality:
   - 4D: `token_idx = w * (z * y) + z * y + y`
   - 3D: `token_idx = z * y + y`
   - 2D: `token_idx = y`
We then directly index into scale/zero_point buffers using token_idx and apply dequantization with the token-specific parameters, converting the quantized integer value back to its original floating-point representation.

# Performance Considerations / Future Improvements

Current implementation uses default workgroup sizing. Buffer implementation processes one element per thread. Could be optimized to process multiple elements per thread for better throughput.

NOTE: Currently the only input types supported are **byte** (uint8), **char** (int8), **int** (int32). The only output types supported are **half** (fp16) and **float** (fp32). A future diff plans to implement **double** (fp64) output dtype support.
ghstack-source-id: 291010146
@exported-using-ghexport

Differential Revision: [D76267107](https://our.internmc.facebook.com/intern/diff/D76267107/)
@pytorchbot pytorchbot requested a review from SS-JIA as a code owner June 17, 2025 22:03
Copy link

pytorch-bot bot commented Jun 17, 2025

🔗 Helpful Links

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

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

⏳ No Failures, 9 Pending

As of commit 8fe89d6 with merge base 3b1c7fd (image):
💚 Looks good so far! There are no failures yet. 💚

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

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 17, 2025
Base automatically changed from gh/ahmtox/11/orig to main June 17, 2025 22:27
@ahmtox ahmtox self-requested a review June 17, 2025 22:42
@ahmtox ahmtox merged commit 9051d2d into main Jun 17, 2025
95 checks passed
@ahmtox ahmtox deleted the gh/ahmtox/16/orig branch June 17, 2025 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants