|
| 1 | +import torch |
| 2 | + |
| 3 | + |
| 4 | +def _to_jsonable(value): |
| 5 | + if isinstance(value, torch.Tensor): |
| 6 | + return value.detach().cpu().tolist() |
| 7 | + return value |
| 8 | + |
| 9 | + |
| 10 | +def _to_tensor(value, dtype=torch.float32): |
| 11 | + if isinstance(value, torch.Tensor): |
| 12 | + return value.detach().cpu().to(dtype) |
| 13 | + return torch.as_tensor(value, dtype=dtype) |
| 14 | + |
| 15 | + |
| 16 | +def _collect_lightllm_kv_scale(scales, zeros, qmin, qmax): |
| 17 | + if isinstance(scales, torch.Tensor) and scales.numel() == 0: |
| 18 | + return None |
| 19 | + |
| 20 | + scales_tensor = _to_tensor(scales) |
| 21 | + zeros_tensor = _to_tensor(zeros, dtype=scales_tensor.dtype) |
| 22 | + qmin_tensor = _to_tensor(qmin, dtype=scales_tensor.dtype) |
| 23 | + qmax_tensor = _to_tensor(qmax, dtype=scales_tensor.dtype) |
| 24 | + min_tensor = (qmin_tensor - zeros_tensor) * scales_tensor |
| 25 | + max_tensor = (qmax_tensor - zeros_tensor) * scales_tensor |
| 26 | + absmax_tensor = torch.maximum(min_tensor.abs(), max_tensor.abs()) |
| 27 | + fp8_qmax = torch.tensor( |
| 28 | + torch.finfo(torch.float8_e4m3fn).max, dtype=absmax_tensor.dtype |
| 29 | + ) |
| 30 | + return absmax_tensor / fp8_qmax |
| 31 | + |
| 32 | + |
| 33 | +def collect_lightllm_kv_calib_json(blockwise_opt): |
| 34 | + if not getattr(blockwise_opt, 'quant_kvcache', False): |
| 35 | + raise ValueError( |
| 36 | + 'save_lightllm_kv_cache_calib requires kvcache quantization.' |
| 37 | + ) |
| 38 | + |
| 39 | + kv_cfg = blockwise_opt.quant_config['kvcache'] |
| 40 | + granularity = kv_cfg.get('granularity') |
| 41 | + if granularity not in ['per_tensor', 'per_head']: |
| 42 | + raise ValueError( |
| 43 | + f'LightLLM calib export only supports per_tensor/per_head, got {granularity}' |
| 44 | + ) |
| 45 | + |
| 46 | + num_layers = blockwise_opt.model.model_config.num_hidden_layers |
| 47 | + num_head = int( |
| 48 | + getattr( |
| 49 | + blockwise_opt.model.model_config, |
| 50 | + 'num_key_value_heads', |
| 51 | + blockwise_opt.model.get_num_attention_heads(), |
| 52 | + ) |
| 53 | + ) |
| 54 | + scales = [] |
| 55 | + for layer_idx in range(num_layers): |
| 56 | + key_scale = _collect_lightllm_kv_scale( |
| 57 | + blockwise_opt.kv_module.k_scales_buffer[layer_idx], |
| 58 | + blockwise_opt.kv_module.k_zeros_buffer[layer_idx], |
| 59 | + blockwise_opt.kv_module.k_qmin_buffer[layer_idx], |
| 60 | + blockwise_opt.kv_module.k_qmax_buffer[layer_idx], |
| 61 | + ) |
| 62 | + value_scale = _collect_lightllm_kv_scale( |
| 63 | + blockwise_opt.kv_module.v_scales_buffer[layer_idx], |
| 64 | + blockwise_opt.kv_module.v_zeros_buffer[layer_idx], |
| 65 | + blockwise_opt.kv_module.v_qmin_buffer[layer_idx], |
| 66 | + blockwise_opt.kv_module.v_qmax_buffer[layer_idx], |
| 67 | + ) |
| 68 | + if key_scale is None or value_scale is None: |
| 69 | + raise ValueError(f'Calibration scale for layer {layer_idx} is empty.') |
| 70 | + |
| 71 | + scale_row = torch.cat([key_scale.reshape(-1), value_scale.reshape(-1)]).tolist() |
| 72 | + scales.append(scale_row) |
| 73 | + |
| 74 | + scale_width = len(scales[0]) if scales else 0 |
| 75 | + if granularity == 'per_tensor' and scale_width != 2: |
| 76 | + raise ValueError(f'per_tensor export expects 2 scales per layer, got {scale_width}') |
| 77 | + if granularity == 'per_head' and scale_width != num_head * 2: |
| 78 | + raise ValueError( |
| 79 | + f'per_head export expects {num_head * 2} scales per layer, got {scale_width}' |
| 80 | + ) |
| 81 | + |
| 82 | + architectures = getattr(blockwise_opt.model.model_config, 'architectures', None) |
| 83 | + if isinstance(architectures, list) and len(architectures) > 0: |
| 84 | + architectures = architectures[0] |
| 85 | + elif architectures is None: |
| 86 | + architectures = blockwise_opt.config.model.type |
| 87 | + |
| 88 | + return { |
| 89 | + 'version': '1.0', |
| 90 | + 'architectures': architectures, |
| 91 | + 'quant_type': granularity, |
| 92 | + 'qmin': float(torch.finfo(torch.float8_e4m3fn).min), |
| 93 | + 'qmax': float(torch.finfo(torch.float8_e4m3fn).max), |
| 94 | + 'num_layers': num_layers, |
| 95 | + 'num_head': num_head, |
| 96 | + 'scales_shape': [num_layers, scale_width], |
| 97 | + 'scales': _to_jsonable(scales), |
| 98 | + } |
0 commit comments