-
Notifications
You must be signed in to change notification settings - Fork 974
Expand file tree
/
Copy pathQuantize.cpp
More file actions
538 lines (461 loc) · 17.6 KB
/
Quantize.cpp
File metadata and controls
538 lines (461 loc) · 17.6 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/backends/vulkan/runtime/graph/ops/OperatorRegistry.h>
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/ScalarUtils.h>
#include <executorch/backends/vulkan/runtime/graph/ops/utils/ShaderNameUtils.h>
#include <executorch/backends/vulkan/runtime/graph/ops/DynamicDispatchNode.h>
#include <executorch/backends/vulkan/runtime/graph/ops/impl/Common.h>
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h>
namespace vkcompute {
void resize_quantize_output(
ComputeGraph* graph,
const std::vector<ArgGroup>& args,
const std::vector<ValueRef>& extra_args) {
(void)extra_args;
const ValueRef out = args.at(0).refs.at(0);
const ValueRef in = args.at(1).refs.at(0);
graph->virtual_resize(out, graph->sizes_of(in));
}
utils::uvec3 quantize_per_channel_global_wg_size(
ComputeGraph* graph,
const vkapi::ShaderInfo& shader,
const std::vector<ArgGroup>& args,
const std::vector<ValueRef>& resize_args) {
(void)shader;
(void)resize_args;
const ValueRef out = args.at(0).refs.at(0);
utils::uvec3 global_wg_size = graph->create_global_wg_size(out);
return global_wg_size;
}
utils::uvec3 quantize_per_channel_local_wg_size(
ComputeGraph* graph,
const vkapi::ShaderInfo& shader,
const utils::uvec3& global_workgroup_size,
const std::vector<ArgGroup>& args,
const std::vector<ValueRef>& resize_args) {
(void)shader;
(void)args;
(void)resize_args;
const ValueRef input = args.at(1).refs.at(0);
utils::uvec3 local_wg_size =
graph->create_local_wg_size(global_workgroup_size);
// WORKAROUND: The CommandBuffer::dispatch function divides
// global_workgroup_size by local_workgroup_size to get the number of
// workgroups to dispatch. For per-channel quantization along the batch axis,
// we need to ensure that we dispatch the correct number of workgroups in the
// Z dimension to cover all batch-channel combinations.
//
// If local_wg_size[2] > 1, then div_up(global_workgroup_size[2],
// local_wg_size[2]) might reduce the number of workgroups dispatched. To
// ensure we dispatch global_workgroup_size[2] workgroups in the Z dimension,
// we set local_wg_size[2] = 1.
const auto input_sizes = graph->sizes_of(input);
if (global_workgroup_size[2] > 1 && input_sizes[3] > 0) {
local_wg_size[2] = 1;
}
return local_wg_size;
}
void add_quantize_per_tensor_node(
ComputeGraph& graph,
const ValueRef& input,
const ValueRef& scale,
const ValueRef& zero_point,
const ValueRef& quant_min,
const ValueRef& quant_max,
const ValueRef& output) {
std::string kernel_name("quantize_per_tensor");
add_storage_type_suffix(kernel_name, graph.storage_type_of(input));
add_dtype_suffix(kernel_name, graph.dtype_of(input));
add_dtype_suffix(kernel_name, graph.dtype_of(output));
int quant_min_val = static_cast<int>(graph.get_int(quant_min));
int quant_max_val = static_cast<int>(graph.get_int(quant_max));
vkapi::ParamsBindList param_ubos;
std::vector<PushConstantDataInfo> push_constants;
if (graph.is_buffer_storage(input)) {
param_ubos = {
graph.numel_ubo(input),
graph.sizes_ubo(input),
graph.strides_ubo(input),
graph.sizes_ubo(output),
graph.strides_ubo(output)};
} else {
param_ubos = {
graph.logical_limits_ubo(input), graph.logical_limits_ubo(output)};
}
push_constants = {
PushConstantDataInfo(&quant_min_val, sizeof(int)),
PushConstantDataInfo(&quant_max_val, sizeof(int)),
};
vkapi::SpecVarList spec_vars = {
graph.hashed_layout_of(output),
graph.hashed_layout_of(input),
};
graph.execute_nodes().emplace_back(new DynamicDispatchNode(
graph,
VK_KERNEL_FROM_STR(kernel_name),
default_pick_global_wg_size,
default_pick_local_wg_size,
// Inputs and Outputs
{{output, vkapi::kWrite},
{input, vkapi::kRead},
{{scale, zero_point}, vkapi::kRead}},
// Shader param buffers
param_ubos,
// Push Constants
push_constants,
// Specialization Constants
spec_vars,
// Resize Args
{},
// Resizing Logic
resize_quantize_output));
}
void add_quantize_per_token_node(
ComputeGraph& graph,
const ValueRef& input,
const ValueRef& scale,
const ValueRef& zero_point,
const ValueRef& quant_min,
const ValueRef& quant_max,
const ValueRef& output) {
std::string kernel_name("quantize_per_token");
add_storage_type_suffix(kernel_name, graph.storage_type_of(input));
add_dtype_suffix(kernel_name, graph.dtype_of(input));
add_dtype_suffix(kernel_name, graph.dtype_of(output));
int quant_min_val = static_cast<int>(graph.get_int(quant_min));
int quant_max_val = static_cast<int>(graph.get_int(quant_max));
int num_tokens = static_cast<int>(graph.sizes_of(scale)[0]);
vkapi::ParamsBindList param_ubos;
std::vector<PushConstantDataInfo> push_constants;
if (graph.is_buffer_storage(input)) {
param_ubos = {
graph.numel_ubo(input),
graph.sizes_ubo(input),
graph.strides_ubo(input),
graph.sizes_ubo(output),
graph.strides_ubo(output),
};
push_constants = {
PushConstantDataInfo(&num_tokens, sizeof(int)),
PushConstantDataInfo(&quant_min_val, sizeof(int)),
PushConstantDataInfo(&quant_max_val, sizeof(int)),
};
} else {
param_ubos = {
graph.logical_limits_ubo(input),
graph.logical_limits_ubo(output),
};
push_constants = {
PushConstantDataInfo(&num_tokens, sizeof(int)),
PushConstantDataInfo(&quant_min_val, sizeof(int)),
PushConstantDataInfo(&quant_max_val, sizeof(int)),
};
}
vkapi::SpecVarList spec_vars = {
graph.hashed_layout_of(output),
graph.hashed_layout_of(input),
};
graph.execute_nodes().emplace_back(new DynamicDispatchNode(
graph,
VK_KERNEL_FROM_STR(kernel_name),
default_pick_global_wg_size,
default_pick_local_wg_size,
// Inputs and Outputs
{{output, vkapi::kWrite},
{input, vkapi::kRead},
{{scale, zero_point}, vkapi::kRead}},
// Shader param buffers
param_ubos,
// Push Constants
push_constants,
// Specialization Constants
spec_vars,
// Resize Args
{},
// Resizing Logic
resize_quantize_output));
}
void add_quantize_per_channel_node(
ComputeGraph& graph,
const ValueRef& input,
const ValueRef& scale,
const ValueRef& zero_point,
const ValueRef& axis,
const ValueRef& quant_min,
const ValueRef& quant_max,
const ValueRef& output) {
std::string kernel_name("quantize_per_channel");
add_storage_type_suffix(kernel_name, graph.storage_type_of(input));
add_dtype_suffix(kernel_name, graph.dtype_of(input));
add_dtype_suffix(kernel_name, graph.dtype_of(output));
int axis_val = static_cast<int>(graph.get_int(axis));
int quant_min_val = static_cast<int>(graph.get_int(quant_min));
int quant_max_val = static_cast<int>(graph.get_int(quant_max));
// Normalize axis and convert from NCHW to WHCN using utility functions
const auto input_sizes = graph.sizes_of(input);
const int64_t ndim = graph.dim_of(input);
// Normalize axis to handle negative indices
axis_val = normalize(axis_val, ndim);
// Convert from NCHW axis to WHCN axis for shader (vulkan representation)
int axis_whcn = nchw_dim_to_whcn_dim(axis_val, ndim);
int num_channels;
if (axis_val == 0 && ndim == 4 && !graph.is_buffer_storage(input)) {
// For batch dimension quantization in 4D tensors, pass the actual number of
// channels so the shader can correctly unfold the batch-channel folding
num_channels = static_cast<int>(input_sizes[1]); // Channel dimension
} else {
num_channels = static_cast<int>(input_sizes[axis_val]);
}
vkapi::ParamsBindList param_ubos;
std::vector<PushConstantDataInfo> push_constants;
if (graph.is_buffer_storage(input)) {
param_ubos = {
graph.numel_ubo(input),
graph.sizes_ubo(input),
graph.strides_ubo(input),
graph.sizes_ubo(output),
graph.strides_ubo(output),
};
push_constants = {
PushConstantDataInfo(&axis_whcn, sizeof(int)),
PushConstantDataInfo(&num_channels, sizeof(int)),
PushConstantDataInfo(&quant_min_val, sizeof(int)),
PushConstantDataInfo(&quant_max_val, sizeof(int)),
};
} else {
param_ubos = {
graph.logical_limits_ubo(input),
graph.logical_limits_ubo(output),
};
push_constants = {
PushConstantDataInfo(&axis_whcn, sizeof(int)),
PushConstantDataInfo(&num_channels, sizeof(int)),
PushConstantDataInfo(&quant_min_val, sizeof(int)),
PushConstantDataInfo(&quant_max_val, sizeof(int)),
};
}
vkapi::SpecVarList spec_vars = {
graph.hashed_layout_of(output),
graph.hashed_layout_of(input),
};
graph.execute_nodes().emplace_back(new DynamicDispatchNode(
graph,
VK_KERNEL_FROM_STR(kernel_name),
quantize_per_channel_global_wg_size,
quantize_per_channel_local_wg_size,
// Inputs and Outputs
{{output, vkapi::kWrite},
{input, vkapi::kRead},
{{scale, zero_point}, vkapi::kRead}},
// Shader param buffers
param_ubos,
// Push Constants
push_constants,
// Specialization Constants
spec_vars,
// Resize Args
{},
// Resizing Logic
resize_quantize_output));
}
void quantize_per_tensor_impl(
ComputeGraph& graph,
const std::vector<ValueRef>& args) {
int arg_idx = 0;
const ValueRef input = args[arg_idx++];
const ValueRef scale = args[arg_idx++];
const ValueRef zero_point = args[arg_idx++];
const ValueRef quant_min = args[arg_idx++];
const ValueRef quant_max = args[arg_idx++];
const ValueRef dtype = args[arg_idx++]; // Added dtype parameter
const ValueRef output = args[arg_idx++];
// Suppress unused variable warning - dtype is inferred from output
(void)dtype;
// Check tensor types
VK_CHECK_COND(graph.val_is_tensor(input));
VK_CHECK_COND(graph.val_is_tensor(output));
// Verify input is a floating point type
VK_CHECK_COND(
graph.dtype_of(input) == vkapi::kDouble ||
graph.dtype_of(input) == vkapi::kFloat ||
graph.dtype_of(input) == vkapi::kHalf);
add_quantize_per_tensor_node(
graph, input, scale, zero_point, quant_min, quant_max, output);
}
void quantize_per_token_impl(
ComputeGraph& graph,
const std::vector<ValueRef>& args) {
int arg_idx = 0;
const ValueRef input = args[arg_idx++];
const ValueRef scale = args[arg_idx++];
const ValueRef zero_point = args[arg_idx++];
const ValueRef quant_min = args[arg_idx++];
const ValueRef quant_max = args[arg_idx++];
const ValueRef dtype = args[arg_idx++]; // Added dtype parameter
const ValueRef output = args[arg_idx++];
// Suppress unused variable warning - dtype is inferred from output
(void)dtype;
// Check tensor types
VK_CHECK_COND(graph.val_is_tensor(input));
VK_CHECK_COND(graph.val_is_tensor(scale));
VK_CHECK_COND(graph.val_is_tensor(zero_point));
VK_CHECK_COND(graph.val_is_tensor(output));
// Verify input is a floating point type
VK_CHECK_COND(
graph.dtype_of(input) == vkapi::kDouble ||
graph.dtype_of(input) == vkapi::kFloat ||
graph.dtype_of(input) == vkapi::kHalf);
// Check that scale and zero_point have buffer storage and width packing
VK_CHECK_COND(graph.is_buffer_storage(scale));
VK_CHECK_COND(graph.packed_dim_of(scale) == WHCN::kWidthDim);
VK_CHECK_COND(graph.is_buffer_storage(zero_point));
VK_CHECK_COND(graph.packed_dim_of(zero_point) == WHCN::kWidthDim);
// Check that tensors with texture storage have standard axis map
if (!graph.is_buffer_storage(input)) {
VK_CHECK_COND(graph.has_standard_axis_map(input));
}
if (!graph.is_buffer_storage(output)) {
VK_CHECK_COND(graph.has_standard_axis_map(output));
}
// Calculate number of tokens (product of all dimensions except the last one)
int64_t num_tokens = 1;
const auto input_sizes = graph.sizes_of(input);
for (size_t i = 0; i < input_sizes.size() - 1; i++) {
num_tokens *= input_sizes[i];
}
const auto scale_sizes = graph.sizes_of(scale);
const auto zero_point_sizes = graph.sizes_of(zero_point);
// Calculate total number of elements in scale and zero_point tensors
int64_t scale_numel = 1;
for (size_t i = 0; i < scale_sizes.size(); i++) {
scale_numel *= scale_sizes[i];
}
int64_t zero_point_numel = 1;
for (size_t i = 0; i < zero_point_sizes.size(); i++) {
zero_point_numel *= zero_point_sizes[i];
}
// Check that the total number of elements matches num_tokens
// This allows for both 1D tensors (size [num_tokens]) and reshaped tensors
// (size [num_tokens, 1])
VK_CHECK_COND(scale_numel == num_tokens);
VK_CHECK_COND(zero_point_numel == num_tokens);
add_quantize_per_token_node(
graph, input, scale, zero_point, quant_min, quant_max, output);
}
void quantize_per_channel_impl(
ComputeGraph& graph,
const std::vector<ValueRef>& args) {
int arg_idx = 0;
const ValueRef input = args[arg_idx++];
const ValueRef scale = args[arg_idx++];
const ValueRef zero_point = args[arg_idx++];
const ValueRef axis = args[arg_idx++];
const ValueRef quant_min = args[arg_idx++];
const ValueRef quant_max = args[arg_idx++];
const ValueRef dtype = args[arg_idx++]; // Added dtype parameter
const ValueRef output = args[arg_idx++];
// Suppress unused variable warning - dtype is inferred from output
(void)dtype;
// Check tensor types
VK_CHECK_COND(graph.val_is_tensor(input));
VK_CHECK_COND(graph.val_is_tensor(scale));
VK_CHECK_COND(graph.val_is_tensor(zero_point));
VK_CHECK_COND(graph.val_is_tensor(output));
// Verify input is a floating point type
VK_CHECK_COND(
graph.dtype_of(input) == vkapi::kDouble ||
graph.dtype_of(input) == vkapi::kFloat ||
graph.dtype_of(input) == vkapi::kHalf);
// Check that scale and zero_point have buffer storage and width packing
VK_CHECK_COND(graph.is_buffer_storage(scale));
VK_CHECK_COND(graph.packed_dim_of(scale) == WHCN::kWidthDim);
VK_CHECK_COND(graph.is_buffer_storage(zero_point));
VK_CHECK_COND(graph.packed_dim_of(zero_point) == WHCN::kWidthDim);
// Check that tensors with texture storage have standard axis map
if (!graph.is_buffer_storage(input)) {
VK_CHECK_COND(graph.has_standard_axis_map(input));
}
if (!graph.is_buffer_storage(output)) {
VK_CHECK_COND(graph.has_standard_axis_map(output));
}
// Normalize axis
int axis_val = static_cast<int>(graph.get_int(axis));
const auto input_sizes = graph.sizes_of(input);
int64_t ndim = graph.dim_of(input);
if (axis_val < 0) {
axis_val += ndim;
}
// Verify axis is valid
VK_CHECK_COND(axis_val >= 0 && axis_val < ndim);
// Get number of channels along the specified axis
int64_t num_channels = input_sizes[axis_val];
const auto scale_sizes = graph.sizes_of(scale);
const auto zero_point_sizes = graph.sizes_of(zero_point);
// Calculate total number of elements in scale and zero_point tensors
int64_t scale_numel = 1;
for (size_t i = 0; i < scale_sizes.size(); i++) {
scale_numel *= scale_sizes[i];
}
int64_t zero_point_numel = 1;
for (size_t i = 0; i < zero_point_sizes.size(); i++) {
zero_point_numel *= zero_point_sizes[i];
}
// Check that the total number of elements matches num_channels
VK_CHECK_COND(scale_numel == num_channels);
VK_CHECK_COND(zero_point_numel == num_channels);
add_quantize_per_channel_node(
graph, input, scale, zero_point, axis, quant_min, quant_max, output);
}
void quantize_affine_impl(
ComputeGraph& graph,
const std::vector<ValueRef>& args) {
int arg_idx = 0;
const ValueRef input = args[arg_idx++];
const ValueRef block_size =
args[arg_idx++]; // SymInt[] - ignored for per-tensor
const ValueRef scale = args[arg_idx++];
const ValueRef zero_point = args[arg_idx++];
const ValueRef output_dtype = args[arg_idx++];
const ValueRef quant_min = args[arg_idx++];
const ValueRef quant_max = args[arg_idx++];
const ValueRef output = args[arg_idx++];
// Suppress unused variable warnings
(void)output_dtype;
// Check tensor types
VK_CHECK_COND(graph.val_is_tensor(input));
VK_CHECK_COND(graph.val_is_tensor(output));
// Verify input is a floating point type
VK_CHECK_COND(
graph.dtype_of(input) == vkapi::kDouble ||
graph.dtype_of(input) == vkapi::kFloat ||
graph.dtype_of(input) == vkapi::kHalf);
// Check if this is per-tensor quantization (only supported granularity)
// block_size should equal input tensor dimensions for per-tensor quantization
const auto input_sizes = graph.sizes_of(input);
const auto block_size_list = graph.get_int_list(block_size);
VK_CHECK_COND(block_size_list->size() == input_sizes.size());
for (size_t i = 0; i < input_sizes.size(); i++) {
VK_CHECK_COND((*block_size_list)[i] == input_sizes[i]);
}
// Default to per-tensor quantization for TorchAO affine ops
add_quantize_per_tensor_node(
graph, input, scale, zero_point, quant_min, quant_max, output);
}
REGISTER_OPERATORS {
VK_REGISTER_OP(
quantized_decomposed.quantize_per_tensor.tensor,
quantize_per_tensor_impl);
VK_REGISTER_OP(
quantized_decomposed.quantize_per_token.default, quantize_per_token_impl);
VK_REGISTER_OP(
quantized_decomposed.quantize_per_channel.default,
quantize_per_channel_impl);
// TorchAO affine quantization operators
VK_REGISTER_OP(torchao.quantize_affine.default, quantize_affine_impl);
}
} // namespace vkcompute