|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD 3-Clause license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import types |
| 8 | +from dataclasses import dataclass |
| 9 | +from typing import Optional |
| 10 | + |
| 11 | +import torch |
| 12 | + |
| 13 | +import torchao |
| 14 | +from torchao.core.config import AOBaseConfig |
| 15 | +from torchao.prototype.mx_formats import ( |
| 16 | + MXGemmKernelChoice, |
| 17 | +) |
| 18 | +from torchao.prototype.mx_formats.config import ( |
| 19 | + _validate_elem_dtype, |
| 20 | + _validate_gemm_kernel_choice, |
| 21 | +) |
| 22 | +from torchao.prototype.mx_formats.mx_tensor import MXTensor |
| 23 | +from torchao.quantization.quant_api import to_linear_activation_quantized |
| 24 | +from torchao.quantization.transform_module import ( |
| 25 | + register_quantize_module_handler, |
| 26 | +) |
| 27 | +from torchao.utils import is_sm_at_least_100 |
| 28 | + |
| 29 | + |
| 30 | +@dataclass |
| 31 | +class MXFPConfig(AOBaseConfig): |
| 32 | + block_size: int = 32 |
| 33 | + |
| 34 | + # Dtypes for Input and Weights |
| 35 | + activation_dtype: torch.dtype = torch.float8_e4m3fn |
| 36 | + weight_dtype: torch.dtype = torch.float8_e4m3fn |
| 37 | + |
| 38 | + # Which kernel to run for mm |
| 39 | + gemm_kernel_choice: MXGemmKernelChoice = MXGemmKernelChoice.CUBLAS |
| 40 | + |
| 41 | + # Set some magic perf settings |
| 42 | + set_inductor_config: bool = True |
| 43 | + |
| 44 | + def __post_init__(self): |
| 45 | + assert self.activation_dtype == self.weight_dtype, ( |
| 46 | + "For now - we only support matching input/weight dtypes." |
| 47 | + ) |
| 48 | + _validate_elem_dtype(self.activation_dtype) |
| 49 | + _validate_elem_dtype(self.weight_dtype) |
| 50 | + _validate_gemm_kernel_choice( |
| 51 | + self.gemm_kernel_choice, self.block_size, self.weight_dtype |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +def _linear_extra_repr(self): |
| 56 | + return f"in_features={self.weight.shape[1]}, out_features={self.weight.shape[0]}, weight={repr(self.weight)}" |
| 57 | + |
| 58 | + |
| 59 | +def _input_activation_quant_func_mxfp( |
| 60 | + x: torch.Tensor, |
| 61 | + activation_dtype: torch.dtype, |
| 62 | + block_size: int, |
| 63 | + scale: Optional[torch.Tensor] = None, |
| 64 | +): |
| 65 | + """ """ |
| 66 | + |
| 67 | + # TODO scale for static quant |
| 68 | + |
| 69 | + activation = MXTensor.to_mx( |
| 70 | + x, |
| 71 | + activation_dtype, |
| 72 | + block_size=block_size, |
| 73 | + gemm_kernel_choice=None, # Get from weight |
| 74 | + pack_fp6=False, # TODO |
| 75 | + ) |
| 76 | + return activation |
| 77 | + |
| 78 | + |
| 79 | +@register_quantize_module_handler(MXFPConfig) |
| 80 | +def _mx_inference_linear_transform(module: torch.nn.Module, config: MXFPConfig): |
| 81 | + # TODO Sm120 has slightly more restrictive reqs |
| 82 | + # TODO handle AMD |
| 83 | + assert is_sm_at_least_100(), "MXFP is only supported on sm100 machiens for now" |
| 84 | + if config.set_inductor_config: |
| 85 | + torchao.quantization.utils.recommended_inductor_config_setter() |
| 86 | + |
| 87 | + activation_dtype = config.activation_dtype |
| 88 | + weight_dtype = config.weight_dtype |
| 89 | + weight = module.weight |
| 90 | + |
| 91 | + assert weight.dtype == torch.bfloat16, ( |
| 92 | + f"Only supporting bf16 out dtype for now, got {weight.dtype}" |
| 93 | + ) |
| 94 | + |
| 95 | + # Convert weight to MX Tensor |
| 96 | + quantized_weight = MXTensor.to_mx( |
| 97 | + weight, |
| 98 | + weight_dtype, |
| 99 | + block_size=config.block_size, |
| 100 | + gemm_kernel_choice=config.gemm_kernel_choice, |
| 101 | + pack_fp6=False, # TODO |
| 102 | + ) |
| 103 | + |
| 104 | + input_quant_func = _input_activation_quant_func_mxfp |
| 105 | + input_quant_kwargs = { |
| 106 | + "block_size": config.block_size, |
| 107 | + "activation_dtype": activation_dtype, |
| 108 | + "scale": None, |
| 109 | + } |
| 110 | + |
| 111 | + quantized_weight = to_linear_activation_quantized( |
| 112 | + quantized_weight, input_quant_func, quant_kwargs=input_quant_kwargs |
| 113 | + ) |
| 114 | + |
| 115 | + module.weight = torch.nn.Parameter(quantized_weight, requires_grad=False) |
| 116 | + module.extra_repr = types.MethodType(_linear_extra_repr, module) |
| 117 | + return module |
0 commit comments