aien-sovereign-core
Autonomous sovereign intelligence engine and runtime core for NVIDIA DGX Spark
Brought by drakestapleton
aien-sovereign-core / modular / nemotron_h_kvexp / config.py
| 1 | """Config subclass that expands Nemotron-H KV heads to satisfy MAX's GQA kernel.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import math |
| 6 | from dataclasses import dataclass |
| 7 | |
| 8 | from max.dtype import DType |
| 9 | from max.pipelines.architectures.nemotron_h.model_config import ( |
| 10 | NemotronHConfig, |
| 11 | parse_hybrid_pattern, |
| 12 | resolve_attention_head_dim, |
| 13 | ) |
| 14 | from max.pipelines.lib.config.model_config import _select_quantization_encoding |
| 15 | |
| 16 | MAX_NVIDIA_GQA_GROUP = 8 |
| 17 | |
| 18 | |
| 19 | def kv_expansion_factor(huggingface_config) -> int: |
| 20 | """How many times to replicate each KV head so group <= 8.""" |
| 21 | n_heads = int(huggingface_config.num_attention_heads) |
| 22 | n_kv = int(huggingface_config.num_key_value_heads) |
| 23 | if n_kv <= 0: |
| 24 | return 1 |
| 25 | group = n_heads / n_kv |
| 26 | return max(1, math.ceil(group / MAX_NVIDIA_GQA_GROUP)) |
| 27 | |
| 28 | |
| 29 | @dataclass(kw_only=True) |
| 30 | class KvExpandedNemotronHConfig(NemotronHConfig): |
| 31 | """NemotronHConfig with KV-head replication for the MAX MHA kernel.""" |
| 32 | |
| 33 | kv_expansion: int = 1 |
| 34 | |
| 35 | @staticmethod |
| 36 | def construct_kv_params( |
| 37 | huggingface_config, |
| 38 | pipeline_config, |
| 39 | devices, |
| 40 | kv_cache_config, |
| 41 | cache_dtype, |
| 42 | ): |
| 43 | # Mirror the built-in body, but multiply n_kv_heads by E so the cache, |
| 44 | # the attention reshape, and the flash kernel all agree on group <= 8. |
| 45 | expansion = kv_expansion_factor(huggingface_config) |
| 46 | resolved_encoding = _select_quantization_encoding( |
| 47 | pipeline_config.model, NemotronHConfig.DEFAULT_ENCODING |
| 48 | ) |
| 49 | if ( |
| 50 | resolved_encoding == "float8_e4m3fn" |
| 51 | and kv_cache_config.kv_cache_format is None |
| 52 | ): |
| 53 | cache_dtype = DType.float8_e4m3fn |
| 54 | kinds = parse_hybrid_pattern(huggingface_config.hybrid_override_pattern) |
| 55 | num_attention_layers = sum(1 for k in kinds if k == "attention") |
| 56 | return kv_cache_config.to_params( |
| 57 | dtype=cache_dtype, |
| 58 | n_kv_heads=int(huggingface_config.num_key_value_heads) * expansion, |
| 59 | head_dim=resolve_attention_head_dim(huggingface_config), |
| 60 | num_layers=num_attention_layers, |
| 61 | devices=devices, |
| 62 | data_parallel_degree=pipeline_config.model.data_parallel_degree, |
| 63 | ) |
| 64 | |
| 65 | @classmethod |
| 66 | def from_hf( |
| 67 | cls, |
| 68 | pipeline_config, |
| 69 | huggingface_config, |
| 70 | dtype, |
| 71 | kv_params, |
| 72 | devices, |
| 73 | ): |
| 74 | config = super().from_hf( |
| 75 | pipeline_config, huggingface_config, dtype, kv_params, devices |
| 76 | ) |
| 77 | expansion = kv_expansion_factor(huggingface_config) |
| 78 | config.kv_expansion = expansion |
| 79 | config.num_key_value_heads = ( |
| 80 | int(huggingface_config.num_key_value_heads) * expansion |
| 81 | ) |
| 82 | return config |