|
| 1 | +# Copyright 2025 Bytedance Ltd. and/or its affiliates |
| 2 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | + |
| 17 | +from dataclasses import dataclass |
| 18 | +from typing import Any, Optional |
| 19 | + |
| 20 | +import torch |
| 21 | +import torch.nn as nn |
| 22 | + |
| 23 | +import modelopt.torch.quantization as mtq |
| 24 | +from modelopt.torch.quantization.config import _default_disabled_quantizer_cfg |
| 25 | + |
| 26 | +# --------------------------------------------------------------------------- |
| 27 | +# NVFP4 quantization config |
| 28 | +# --------------------------------------------------------------------------- |
| 29 | + |
| 30 | +NVFP4_WEIGHT_ONLY_CFG = { |
| 31 | + "quant_cfg": { |
| 32 | + "*weight_quantizer": { |
| 33 | + "num_bits": (2, 1), |
| 34 | + "block_sizes": {-1: 16, "type": "dynamic", "scale_bits": (4, 3)}, |
| 35 | + "axis": None, |
| 36 | + "enable": True, |
| 37 | + }, |
| 38 | + "*input_quantizer": {"enable": False}, |
| 39 | + **_default_disabled_quantizer_cfg, |
| 40 | + }, |
| 41 | + "algorithm": "max", |
| 42 | +} |
| 43 | + |
| 44 | +# --------------------------------------------------------------------------- |
| 45 | +# QAT application |
| 46 | +# --------------------------------------------------------------------------- |
| 47 | + |
| 48 | + |
| 49 | +def apply_qat(model: nn.Module, qat_mode: str): |
| 50 | + """Apply Quantization-Aware Training to the model. |
| 51 | +
|
| 52 | + Args: |
| 53 | + model: The Megatron model to apply QAT to. |
| 54 | + qat_mode: QAT mode, now only support "w4a16" for weight-only quantization. |
| 55 | +
|
| 56 | + Returns: |
| 57 | + The quantized model. |
| 58 | + """ |
| 59 | + if qat_mode != "w4a16": |
| 60 | + raise ValueError(f"Only 'w4a16' is supported, got: {qat_mode}") |
| 61 | + |
| 62 | + mtq.quantize(model, NVFP4_WEIGHT_ONLY_CFG) |
| 63 | + return model |
| 64 | + |
| 65 | + |
| 66 | +@dataclass |
| 67 | +class QuantizationMetadata: |
| 68 | + """Metadata for a quantized module.""" |
| 69 | + |
| 70 | + qformat: str |
| 71 | + weight_quantizer: Any |
| 72 | + input_quantizer: Any |
| 73 | + module: torch.nn.Module |
| 74 | + vpp_idx: int |
| 75 | + block_size: int = 16 # Default NVFP4 block size |
| 76 | + # Fields for EP synchronization - store amax values for non-local experts |
| 77 | + weight_amax: Optional[torch.Tensor] = None |
| 78 | + input_amax: Optional[torch.Tensor] = None |
| 79 | + is_local: bool = True # Whether this expert is local to current EP rank |
| 80 | + global_expert_idx: Optional[int] = None # Global expert index for MoE experts |
| 81 | + local_expert_idx: Optional[int] = None # Local expert index on this EP rank |
0 commit comments