MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / quantize

Method quantize

op_builder/hpu/fp_quantizer.py:64–82  ·  view source on GitHub ↗
(cls, out, val, scale, group_size, stochastic_rounding, q_bits, q_mantisa_bits)

Source from the content-addressed store, hash-verified

62
63 @classmethod
64 def quantize(cls, out, val, scale, group_size, stochastic_rounding, q_bits, q_mantisa_bits):
65 assert q_bits == 8, "Quantize on HPU only supports quantization to FP8"
66 assert q_mantisa_bits == 3, "Quantize on HPU only supports q_mantissa_bits = 3"
67 assert out.dtype.is_floating_point, "Quantization on HPU is only to float dtypes"
68
69 num_groups, group_size = out.shape
70
71 # Reshape the tensor
72 val_reshaped = val.view(num_groups, group_size).float()
73 # Calculate the scale
74 max_vals = val_reshaped.abs().max(dim=1, keepdim=True)[0]
75 q_range = torch.finfo(out.dtype).max
76 tmp_scale = q_range / max_vals
77 scale.copy_(tmp_scale)
78 # Copy quantized
79 quant, _ = torch.ops.hpu.cast_to_fp8_v2(val_reshaped, scale, stochastic_rounding, dtype=out.dtype)
80 out.copy_(quant)
81
82 return out
83
84 @classmethod
85 def get_scales(cls, out, num_groups):

Callers

nothing calls this directly

Calls 1

copy_Method · 0.45

Tested by

no test coverage detected