interplane.Get Omega
Read in your language

aien-yardsticks

aien-dev/aien-yardsticks

Brought by drakestapleton

branch main Branches Tags History

aien-yardsticks / yardsticks / modular-nemotron-h-kvexp / config.py

87 lines · 3038 bytes Raw Blame History
1"""Config subclass that expands Nemotron-H KV heads to satisfy MAX's GQA kernel."""
2
3from __future__ import annotations
4
5import math
6from dataclasses import dataclass
7
8from max.dtype import DType
9from max.pipelines.architectures.nemotron_h.model_config import (
10 NemotronHConfig,
11 parse_hybrid_pattern,
12 resolve_attention_head_dim,
13)
14from max.pipelines.lib.config.model_config import _select_quantization_encoding
15
16MAX_NVIDIA_GQA_GROUP = 8
17
18
19def 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)
30class KvExpandedNemotronHConfig(NemotronHConfig):
31 """NemotronHConfig with KV-head replication for the MAX MHA kernel."""
32
33 SUPPORTED_ENCODINGS = NemotronHConfig.SUPPORTED_ENCODINGS | {"float4_e2m1fnx2"}
34 kv_expansion: int = 1
35
36 @staticmethod
37 def construct_kv_params(
38 huggingface_config,
39 pipeline_config,
40 devices,
41 kv_cache_config,
42 cache_dtype,
43 ):
44 # Mirror the built-in body, but multiply n_kv_heads by E so the cache,
45 # the attention reshape, and the flash kernel all agree on group <= 8.
46 expansion = kv_expansion_factor(huggingface_config)
47 resolved_encoding = _select_quantization_encoding(
48 pipeline_config.model, NemotronHConfig.DEFAULT_ENCODING
49 )
50 if (
51 resolved_encoding in ("float8_e4m3fn", "float4_e2m1fnx2")
52 and kv_cache_config.kv_cache_format is None
53 ):
54 cache_dtype = DType.float8_e4m3fn
55 kinds = parse_hybrid_pattern(huggingface_config.hybrid_override_pattern)
56 num_attention_layers = sum(1 for k in kinds if k == "attention")
57 return kv_cache_config.to_params(
58 dtype=cache_dtype,
59 n_kv_heads=int(huggingface_config.num_key_value_heads) * expansion,
60 head_dim=resolve_attention_head_dim(huggingface_config),
61 num_layers=num_attention_layers,
62 devices=devices,
63 data_parallel_degree=pipeline_config.model.data_parallel_degree,
64 )
65
66 @classmethod
67 def from_hf(
68 cls,
69 pipeline_config,
70 huggingface_config,
71 dtype,
72 kv_params,
73 devices,
74 ):
75 if dtype in (DType.float8_e4m3fn, DType.uint8):
76 dtype = DType.bfloat16
77 config = super().from_hf(
78 pipeline_config, huggingface_config, dtype, kv_params, devices
79 )
80 if config.dtype in (DType.float8_e4m3fn, DType.uint8):
81 config.dtype = DType.bfloat16
82 expansion = kv_expansion_factor(huggingface_config)
83 config.kv_expansion = expansion
84 config.num_key_value_heads = (
85 int(huggingface_config.num_key_value_heads) * expansion
86 )
87 return config