Kye Gomez commited on
Commit
c258cdc
·
1 Parent(s): 7916266

example .py

Browse files
Files changed (2) hide show
  1. example.py +48 -0
  2. open_mythos/main.py +0 -63
example.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from open_mythos.main import OpenMythos, MythosConfig
3
+
4
+
5
+ attn_type = "mla" # or "gqa"
6
+
7
+ base = {
8
+ "vocab_size": 1000,
9
+ "dim": 256,
10
+ "n_heads": 8,
11
+ "max_seq_len": 128,
12
+ "max_loop_iters": 4,
13
+ "prelude_layers": 1,
14
+ "coda_layers": 1,
15
+ "n_experts": 8,
16
+ "n_shared_experts": 1,
17
+ "n_experts_per_tok": 2,
18
+ "expert_dim": 64,
19
+ "lora_rank": 8,
20
+ "attn_type": attn_type,
21
+ }
22
+
23
+ if attn_type == "gqa":
24
+ cfg = MythosConfig(**base, n_kv_heads=2)
25
+ else:
26
+ cfg = MythosConfig(
27
+ **base,
28
+ n_kv_heads=8,
29
+ kv_lora_rank=32,
30
+ q_lora_rank=64,
31
+ qk_rope_head_dim=16,
32
+ qk_nope_head_dim=16,
33
+ v_head_dim=16,
34
+ )
35
+
36
+ model = OpenMythos(cfg)
37
+ total = sum(p.numel() for p in model.parameters())
38
+ print(f"\n[{attn_type.upper()}] Parameters: {total:,}")
39
+
40
+ ids = torch.randint(0, cfg.vocab_size, (2, 16))
41
+ logits = model(ids, n_loops=4)
42
+ print(f"[{attn_type.upper()}] Logits shape: {logits.shape}")
43
+
44
+ out = model.generate(ids, max_new_tokens=8, n_loops=8)
45
+ print(f"[{attn_type.upper()}] Generated shape: {out.shape}")
46
+
47
+ A = model.recurrent.injection.get_A()
48
+ print(f"[{attn_type.upper()}] Spectral radius ρ(A) max: {A.max().item():.4f} (must be < 1)")
open_mythos/main.py CHANGED
@@ -11,7 +11,6 @@ import torch
11
  import torch.nn as nn
12
  import torch.nn.functional as F
13
 
14
-
15
  # ---------------------------------------------------------------------------
16
  # Config
17
  # ---------------------------------------------------------------------------
@@ -1014,65 +1013,3 @@ class OpenMythos(nn.Module):
1014
  input_ids = torch.cat([input_ids, next_tok], dim=1)
1015
  return input_ids
1016
 
1017
-
1018
- # ---------------------------------------------------------------------------
1019
- # Quick smoke test
1020
- # ---------------------------------------------------------------------------
1021
-
1022
-
1023
- def _smoke(attn_type: str) -> None:
1024
- """
1025
- Instantiate a small OpenMythos model with the given attention type, run a
1026
- forward pass and a short generation, and verify the LTI spectral radius.
1027
-
1028
- Args:
1029
- attn_type -- "gqa" or "mla"
1030
- """
1031
- base = dict(
1032
- vocab_size=1000,
1033
- dim=256,
1034
- n_heads=8,
1035
- max_seq_len=128,
1036
- max_loop_iters=4,
1037
- prelude_layers=1,
1038
- coda_layers=1,
1039
- n_experts=8,
1040
- n_shared_experts=1,
1041
- n_experts_per_tok=2,
1042
- expert_dim=64,
1043
- lora_rank=8,
1044
- attn_type=attn_type,
1045
- )
1046
- if attn_type == "gqa":
1047
- cfg = MythosConfig(**base, n_kv_heads=2)
1048
- else:
1049
- cfg = MythosConfig(
1050
- **base,
1051
- n_kv_heads=8, # unused by MLA but field must be valid
1052
- kv_lora_rank=32,
1053
- q_lora_rank=64,
1054
- qk_rope_head_dim=16,
1055
- qk_nope_head_dim=16,
1056
- v_head_dim=16,
1057
- )
1058
-
1059
- model = OpenMythos(cfg)
1060
- total = sum(p.numel() for p in model.parameters())
1061
- print(f"\n[{attn_type.upper()}] Parameters: {total:,}")
1062
-
1063
- ids = torch.randint(0, cfg.vocab_size, (2, 16))
1064
- logits = model(ids, n_loops=4)
1065
- print(f"[{attn_type.upper()}] Logits shape: {logits.shape}")
1066
-
1067
- out = model.generate(ids, max_new_tokens=8, n_loops=8)
1068
- print(f"[{attn_type.upper()}] Generated shape: {out.shape}")
1069
-
1070
- A = model.recurrent.injection.get_A()
1071
- print(
1072
- f"[{attn_type.upper()}] Spectral radius ρ(A) max: {A.max().item():.4f} (must be < 1)"
1073
- )
1074
-
1075
-
1076
- if __name__ == "__main__":
1077
- _smoke("gqa")
1078
- _smoke("mla")
 
11
  import torch.nn as nn
12
  import torch.nn.functional as F
13
 
 
14
  # ---------------------------------------------------------------------------
15
  # Config
16
  # ---------------------------------------------------------------------------
 
1013
  input_ids = torch.cat([input_ids, next_tok], dim=1)
1014
  return input_ids
1015