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 / tests / test_kvexp.py

97 lines · 3732 bytes Raw Blame History
1"""Unit tests for KV-expanded Nemotron-H architecture adapter."""
2
3import unittest
4from types import SimpleNamespace
5import numpy as np
6
7from nemotron_h_kvexp.config import kv_expansion_factor, MAX_NVIDIA_GQA_GROUP
8from nemotron_h_kvexp.weight_adapters import (
9 _bf16_to_f32,
10 _f32_to_bf16,
11 _delta,
12)
13
14
15class TestKvExpansion(unittest.TestCase):
16 def test_expansion_factor_nemotron_30b(self):
17 """Nemotron-30B: 32 query heads, 2 KV heads -> group 16 -> expansion 2."""
18 cfg = SimpleNamespace(num_attention_heads=32, num_key_value_heads=2)
19 factor = kv_expansion_factor(cfg)
20 self.assertEqual(factor, 2)
21 effective_kv = cfg.num_key_value_heads * factor
22 effective_group = cfg.num_attention_heads / effective_kv
23 self.assertLessEqual(effective_group, MAX_NVIDIA_GQA_GROUP)
24
25 def test_expansion_factor_standard_gqa(self):
26 """Standard GQA: 32 query heads, 4 KV heads -> group 8 -> expansion 1."""
27 cfg = SimpleNamespace(num_attention_heads=32, num_key_value_heads=4)
28 factor = kv_expansion_factor(cfg)
29 self.assertEqual(factor, 1)
30
31 def test_expansion_factor_large_group(self):
32 """Hypothetical model: 64 query heads, 2 KV heads -> group 32 -> expansion 4."""
33 cfg = SimpleNamespace(num_attention_heads=64, num_key_value_heads=2)
34 factor = kv_expansion_factor(cfg)
35 self.assertEqual(factor, 4)
36 effective_kv = cfg.num_key_value_heads * factor
37 effective_group = cfg.num_attention_heads / effective_kv
38 self.assertLessEqual(effective_group, MAX_NVIDIA_GQA_GROUP)
39
40 def test_expansion_factor_zero_kv(self):
41 """Edge case: zero or negative KV heads returns 1."""
42 cfg = SimpleNamespace(num_attention_heads=32, num_key_value_heads=0)
43 self.assertEqual(kv_expansion_factor(cfg), 1)
44
45
46class TestBf16Conversions(unittest.TestCase):
47 def test_bf16_f32_roundtrip(self):
48 """Verify bfloat16 to float32 bit-shift roundtrip."""
49 original = np.array([0.0, 1.0, -1.0, 2.5, 0.125], dtype=np.float32)
50 u16 = _f32_to_bf16(original)
51 recovered = _bf16_to_f32(u16)
52 np.testing.assert_allclose(recovered, original, rtol=1e-3, atol=1e-3)
53
54
55class TestLoraDelta(unittest.TestCase):
56 def test_delta_computation(self):
57 """Verify LoRA low-rank delta calculation: scale * (B @ A)."""
58 r = 4
59 in_dim = 16
60 out_dim = 32
61 scale = 2.0
62
63 A = np.random.randn(r, in_dim).astype(np.float32)
64 B = np.random.randn(out_dim, r).astype(np.float32)
65
66 expected = scale * (B @ A)
67 actual = _delta(A, B, scale)
68
69 self.assertEqual(actual.shape, (out_dim, in_dim))
70 np.testing.assert_allclose(actual, expected, rtol=1e-6)
71
72
73class TestWeightReplication(unittest.TestCase):
74 def test_kv_replication_math(self):
75 """Verify manual KV tensor replication semantics along head dimension."""
76 n_kv = 2
77 head_dim = 64
78 hidden = 128
79 expansion = 2
80
81 # Create synthetic KV projection weight
82 k = np.arange(n_kv * head_dim * hidden, dtype=np.float32).reshape(n_kv, head_dim, hidden)
83
84 # Replicate
85 k_rep = np.repeat(k, expansion, axis=0)
86 self.assertEqual(k_rep.shape, (n_kv * expansion, head_dim, hidden))
87
88 # Verify first and second copies match the original first head
89 np.testing.assert_array_equal(k_rep[0], k[0])
90 np.testing.assert_array_equal(k_rep[1], k[0])
91 # Verify third and fourth copies match the original second head
92 np.testing.assert_array_equal(k_rep[2], k[1])
93 np.testing.assert_array_equal(k_rep[3], k[1])
94
95
96if __name__ == "__main__":
97 unittest.main()