Commit e44a3d1
morelos
[ET-VK][Ops] choose_qparams op shaders and impl
Pull Request resolved: #11557
# Operator Description
The choose_qparams operator computes optimal quantization parameters (scale and zero_point) from floating-point input tensors. This operator analyzes the statistical distribution of input data to determine the best quantization mapping for subsequent quantization operations. It supports two computation modes:
- **Per-tensor quantization**: Computes a single scale and zero_point for the entire tensor based on global min/max values
- **Per-token quantization**: Computes separate scale and zero_point values for each "token" (typically rows or channels) based on per-token min/max values
The parameter calculation formulas are:
- `scale = (data_max - data_min) / (quant_max - quant_min)`
- `zero_point = clamp(round(quant_min - data_min/scale), quant_min, quant_max)`
**Example**: For input data with `min=-2.5`, `max=7.3`, `quant_min=0`, `quant_max=255`:
- `scale = (7.3 - (-2.5)) / (255 - 0) = 9.8 / 255 = 0.0384`
- `zero_point = clamp(round(0 - (-2.5)/0.0384), 0, 255) = clamp(65, 0, 255) = 65`
The quantization parameters serve these purposes:
- **scale**: Determines the precision/granularity of the quantization mapping
- **zero_point**: Ensures that floating-point zero maps to an exact integer value
- **quant_min/quant_max**: Define the target quantization range (e.g., 0-255 for uint8)
# Shader Algorithm Overview
## Texture Storage Implementation (`choose_qparams_texture.glsl`)
The texture-based implementation uses a parallel reduction algorithms to efficiently compute min/max values across 3D textures with RGBA texel format:
**Per-tensor Mode**:
Each compute thread processes multiple texels using strided access patterns across the entire tensor. For each texel, it converts linear indices to 3D coordinates using `z = idx/(x*y), y = (idx%(x*y))/x, x = idx%x`, then loads 4-component texel data. The implementation validates each component against padding boundaries by calculating `valid_elements = min(4, remaining_elements)` to avoid processing padded data. Thread-local min/max reduction processes valid components while filtering NaN and infinity values. The algorithm then performs intra-workgroup reduction using shared memory arrays `shared_min[NWORKERS]` and `shared_max[NWORKERS]`. A tree reduction pattern halves the stride iteratively: `stride = workgroup_size/2; stride > 0; stride >>= 1`, combining results from `shared_min[local_id + stride]` with proper infinity handling. Finally, the master thread (local_id == 0, group_id == 0) computes the final scale and zero_point using the `calculate_scale_and_zero_point()` function and writes results to output textures.
**Per-token Mode**:
This mode implements a more complex multi-workgroup coordination strategy where each workgroup processes multiple tokens. The algorithm calculates `tokens_per_workgroup = (num_tokens + total_workgroups - 1) / total_workgroups` to distribute work evenly. For each assigned token, it determines the texel range using `token_start_texel = token_id * texels_per_token` and processes texels within that range using strided access `texel_idx = token_start_texel + local_id; texel_idx < token_end_texel; texel_idx += workgroup_size`. The same padding validation and component processing logic applies, but scoped to the current token's data. After thread-local reduction, it performs the same tree reduction pattern within the workgroup. The master thread computes token-specific scale/zero_point and converts the linear token_id back to 3D output coordinates using `out_z = token_id/(x*y), out_y = (token_id%(x*y))/x, out_x = token_id%x` for writing results. Workgroup synchronization via `barrier()` ensures proper coordination between token processing iterations.
## Buffer Storage Implementation (`choose_qparams_buffer.glsl`)
The buffer-based implementation operates on linear memory with simpler indexing but maintains the same parallel reduction strategy:
**Per-tensor Mode**:
Each compute thread processes multiple elements using strided access across the entire linear buffer: `for (i = global_id; i < total_elements; i += total_threads)`. Direct buffer access `t_in[i]` loads scalar values with NaN/infinity filtering. Thread-local min/max reduction accumulates valid values. The same shared memory tree reduction pattern applies: threads store results in `shared_min[local_id]` and `shared_max[local_id]`, then perform logarithmic reduction with stride halving. The master thread (local_id == 0) computes final parameters and directly writes to output buffers: `t_scale[0] = scale_val; t_zero_point[0] = zero_point_val`.
**Per-token Mode**:
This mode distributes tokens across workgroups using `tokens_per_workgroup = (num_tokens + total_workgroups - 1) / total_workgroups` for load balancing. Each workgroup processes its assigned token range `[start_token, end_token)`. For each token, it calculates the linear element range: `token_start = token_id * token_size; token_end = token_start + token_size`. Threads process elements within the token using strided access: `for (i = token_start + local_id; i < token_end; i += workgroup_size)`. The same thread-local reduction and shared memory tree reduction patterns apply, but scoped to the current token's data. The master thread computes token-specific parameters and writes directly to the output arrays: `t_scale[token_id] = scale_val; t_zero_point[token_id] = zero_point_val`. Workgroup synchronization ensures proper coordination between token processing iterations.
# Performance Considerations / Future Improvements
Current implementation uses a parallel reduction algorithms with shared memory optimization, but several areas offer improvement opportunities:
- The tree reduction pattern achieves O(log N) complexity within workgroups, but the current implementation uses fixed 64-thread workgroups. Dynamic workgroup sizing based on tensor dimensions could improve occupancy.
- Fixed 64-thread workgroups match the NWORKERS constant, but profiling different sizes (32, 128, 256) could reveal better performance characteristics for different tensor sizes and GPU architectures.
NOTE: Currently the only input type supported is **float** (fp32). The output types are **float** for scale and **int** for zero_point. ghstack-source-id: 290041468
@exported-using-ghexport
ghstack-source-id: 291010147
Differential Revision: [D76436933](https://our.internmc.facebook.com/intern/diff/D76436933/)1 parent 8fe89d6 commit e44a3d1
File tree
7 files changed
+1213
-0
lines changed- backends/vulkan
- runtime/graph/ops
- glsl
- impl
- test/op_tests
7 files changed
+1213
-0
lines changedLines changed: 70 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
Lines changed: 278 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
0 commit comments