Myric commited on
Commit
8ff4d84
·
verified ·
1 Parent(s): 3fbd96f

Upload patch_granite_config.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. patch_granite_config.py +38 -0
patch_granite_config.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Adapt an APEX generate_config.sh output for Granite-4.0-H-Tiny (granitemoehybrid).
3
+
4
+ Hybrid: 36 Mamba-2 layers (ssm_in/out/conv1d) + 4 attention layers (plain MHA) +
5
+ MoE (64 routed + shared expert) on every layer. The stock generator emits
6
+ ffn_*_exps/shexp, attn_q/k/v/output, and ssm_out — but MISSES the Mamba-2
7
+ input/conv projections. We add ssm_in + ssm_conv1d at the mixer precision, and
8
+ drop tensors Granite doesn't have. 1-D SSM state (ssm_a/d/dt/norm) stays F32.
9
+
10
+ --ssm-type pins the Mamba-2 mixer projections (ssm_in/out/conv1d) higher (e.g.
11
+ Q8_0) to test whether protecting the recurrence helps (the hand-roll variant).
12
+ """
13
+ import argparse, re
14
+ DROP = {"attn_gate","attn_qkv","shortconv.in_proj","shortconv.out_proj","ssm_alpha","ssm_beta"}
15
+ ADD = ["ssm_in","ssm_conv1d"] # ssm_out already emitted by the generator
16
+ SSM_ALL = ["ssm_in","ssm_out","ssm_conv1d"]
17
+
18
+ def main():
19
+ ap=argparse.ArgumentParser(); ap.add_argument("infile"); ap.add_argument("outfile")
20
+ ap.add_argument("--ssm-type",default=None,help="override Mamba-2 mixer tensors (e.g. Q8_0)")
21
+ a=ap.parse_args()
22
+ out=[]
23
+ for l in (x.strip() for x in open(a.infile) if x.strip()):
24
+ m=re.match(r"blk\.\d+\.([\w.]+)=",l); tail=m.group(1) if m else ""
25
+ if tail in DROP: continue
26
+ # optionally bump ssm_out (already present) to the mixer override type
27
+ mo_out=re.match(r"(blk\.\d+\.ssm_out)=(\S+)",l)
28
+ if mo_out and a.ssm_type:
29
+ out.append(f"{mo_out.group(1)}={a.ssm_type}"); continue
30
+ out.append(l)
31
+ mo=re.match(r"blk\.(\d+)\.attn_output=(\S+)",l)
32
+ if mo:
33
+ n,t=mo.group(1),mo.group(2)
34
+ st=a.ssm_type or t
35
+ for name in ADD: out.append(f"blk.{n}.{name}={st}")
36
+ open(a.outfile,"w").write("\n".join(out)+"\n")
37
+ print(f"wrote {a.outfile} ({len(out)} lines)")
38
+ if __name__=="__main__": main()