| 23 | } |
| 24 | |
| 25 | at::Tensor quantize(torch::Tensor& out, |
| 26 | torch::Tensor& val, |
| 27 | int group_size, |
| 28 | int stochastic_rounding, |
| 29 | int q_bits, |
| 30 | int q_mantisa_bits) |
| 31 | { |
| 32 | int total_elems = at::numel(val); |
| 33 | TORCH_CHECK(group_size > 0, "group_size must be positive"); |
| 34 | TORCH_CHECK(group_size % 8 == 0, "group_size must be divisible by 8"); |
| 35 | TORCH_CHECK(total_elems % group_size == 0, |
| 36 | "tensor element count must be divisible by group_size"); |
| 37 | float q_range = q_bits == 8 ? (q_mantisa_bits == 3 ? 480.0 : 114688.0) : // fp8 ranges |
| 38 | (q_bits == 12 ? 510.0 : // fp12 range |
| 39 | (q_bits == 6 ? 28.0 : // fp6 range |
| 40 | 6.0)); // fp4 range (using power 2); TODO (Reza): add the power-4 |
| 41 | // in case accuracy is not matching! |
| 42 | int num_groups = total_elems / group_size; |
| 43 | // Empty tensors have no output storage and require no kernel launch. |
| 44 | if (num_groups == 0) { return out; } |
| 45 | |
| 46 | DISPATCH_QUANTIZE(kHalf, __half, 23, 8); |
| 47 | #ifdef BF16_AVAILABLE |
| 48 | DISPATCH_QUANTIZE(kBFloat16, __nv_bfloat16, 23, 8); |
| 49 | #endif |
| 50 | |
| 51 | return out; |
| 52 | } |
| 53 | |
| 54 | #define DISPATCH_DEQUANTIZE(T_TYPE, C_TYPE, mantisa) \ |
| 55 | if (val.options().dtype() == torch::T_TYPE) { \ |
nothing calls this directly
no outgoing calls
no test coverage detected