-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathprobe_solar_glm45air_v2.py
More file actions
812 lines (662 loc) · 26.6 KB
/
probe_solar_glm45air_v2.py
File metadata and controls
812 lines (662 loc) · 26.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Enhanced probe script with:
1. Control group comparison (unrelated MoE model as baseline)
2. Expert-level weight comparison with alignment detection
3. Tokenizer overlap analysis
4. Visualization of cosine similarity distributions
Usage:
python probe_solar_glm45air_v2.py \
--solar upstage/Solar-Open-100B \
--glm zai-org/GLM-4.5-Air \
--control Qwen/Qwen2.5-72B-Instruct \
--layers 0-45 \
--windows 3 \
--chunk_elems 262144 \
--out_dir results
"""
import argparse
import csv
import hashlib
import json
import math
import os
import re
import struct
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Tuple, Any
from collections import defaultdict
import numpy as np
import requests
try:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
HAS_MATPLOTLIB = True
except ImportError:
HAS_MATPLOTLIB = False
print("[WARN] matplotlib not found, skipping visualization")
DTYPE_SIZES = {
"BF16": 2, "F16": 2, "F32": 4, "F64": 8,
"I64": 8, "I32": 4, "I16": 2, "I8": 1, "U8": 1, "BOOL": 1,
}
FLOAT_DTYPES = {"BF16", "F16", "F32"}
def hf_url(repo_id: str, revision: str, filename: str) -> str:
return f"https://huggingface.co/{repo_id}/resolve/{revision}/{filename}"
def _headers(token: Optional[str], extra: Optional[dict] = None) -> dict:
h = {"Accept-Encoding": "identity"}
if token:
h["Authorization"] = f"Bearer {token}"
if extra:
h.update(extra)
return h
def http_get(url: str, token: Optional[str]) -> Optional[bytes]:
try:
r = requests.get(url, headers=_headers(token), allow_redirects=True, timeout=120)
if r.status_code == 200:
return r.content
except Exception:
pass
return None
def http_get_json(url: str, token: Optional[str]) -> Optional[dict]:
b = http_get(url, token)
if b is None:
return None
try:
return json.loads(b.decode("utf-8"))
except Exception:
return None
def http_range_get(url: str, start: int, end: int, token: Optional[str]) -> bytes:
r = requests.get(
url,
headers=_headers(token, {"Range": f"bytes={start}-{end}"}),
allow_redirects=True,
timeout=120,
)
if r.status_code == 206:
return r.content
expected = end - start + 1
if r.status_code == 200 and len(r.content) == expected:
return r.content
raise RuntimeError(f"Range GET failed: HTTP {r.status_code} len={len(r.content)} expected={expected}")
@dataclass
class TensorInfo:
dtype: str
shape: Tuple[int, ...]
data_offsets: Tuple[int, int]
@dataclass
class SafetensorsHeader:
header_len: int
base_data_offset: int
tensors: Dict[str, TensorInfo]
class HeaderCache:
def __init__(self):
self._cache: Dict[str, SafetensorsHeader] = {}
def get(self, url: str) -> Optional[SafetensorsHeader]:
return self._cache.get(url)
def set(self, url: str, hdr: SafetensorsHeader) -> None:
self._cache[url] = hdr
def parse_safetensors_header(url: str, token: Optional[str], cache: HeaderCache) -> SafetensorsHeader:
cached = cache.get(url)
if cached:
return cached
b0 = http_range_get(url, 0, 7, token)
header_len = struct.unpack("<Q", b0)[0]
hb = http_range_get(url, 8, 8 + header_len - 1, token)
header_str = hb.decode("utf-8").strip()
header_json = json.loads(header_str)
tensors: Dict[str, TensorInfo] = {}
for k, v in header_json.items():
if k == "__metadata__":
continue
tensors[k] = TensorInfo(
dtype=v["dtype"],
shape=tuple(int(x) for x in v["shape"]),
data_offsets=(int(v["data_offsets"][0]), int(v["data_offsets"][1])),
)
hdr = SafetensorsHeader(
header_len=int(header_len),
base_data_offset=8 + int(header_len),
tensors=tensors,
)
cache.set(url, hdr)
return hdr
def prod(shape: Tuple[int, ...]) -> int:
p = 1
for x in shape:
p *= int(x)
return int(p)
def bytes_to_f32(buf: bytes, dtype: str) -> np.ndarray:
if dtype == "F32":
return np.frombuffer(buf, dtype=np.float32)
if dtype == "F16":
return np.frombuffer(buf, dtype=np.float16).astype(np.float32)
if dtype == "BF16":
u16 = np.frombuffer(buf, dtype=np.uint16)
u32 = u16.astype(np.uint32) << 16
return u32.view(np.float32)
raise ValueError(f"Unsupported dtype: {dtype}")
def stable_u64(s: str) -> int:
h = hashlib.sha256(s.encode("utf-8")).digest()
return int.from_bytes(h[:8], "little", signed=False)
def cosine(x: np.ndarray, y: np.ndarray) -> float:
x64 = x.astype(np.float64, copy=False)
y64 = y.astype(np.float64, copy=False)
nx = np.linalg.norm(x64)
ny = np.linalg.norm(y64)
if nx == 0.0 or ny == 0.0:
return float("nan")
return float(np.dot(x64, y64) / (nx * ny))
def rel_rmse(x: np.ndarray, y: np.ndarray) -> float:
x64 = x.astype(np.float64, copy=False)
y64 = y.astype(np.float64, copy=False)
mse = np.mean((x64 - y64) ** 2)
denom = np.sqrt(np.mean(x64 ** 2)) + 1e-12
return float(np.sqrt(mse) / denom)
class RemoteRepo:
def __init__(self, repo_id: str, revision: str, token: Optional[str]):
self.repo_id = repo_id
self.revision = revision
self.token = token
self.header_cache = HeaderCache()
self.name = repo_id.split("/")[-1]
self.config = self._load_config()
self.weight_map, self.index_file = self._load_weight_map()
def _load_config(self) -> dict:
url = hf_url(self.repo_id, self.revision, "config.json")
cfg = http_get_json(url, self.token)
return cfg or {}
def _load_weight_map(self) -> Tuple[Dict[str, str], str]:
candidates = [
"model.safetensors.index.json",
"pytorch_model.safetensors.index.json",
]
for name in candidates:
url = hf_url(self.repo_id, self.revision, name)
js = http_get_json(url, self.token)
if js and isinstance(js, dict) and "weight_map" in js:
wm = js["weight_map"]
if isinstance(wm, dict):
return wm, name
for single in ["model.safetensors", "pytorch_model.safetensors"]:
url = hf_url(self.repo_id, self.revision, single)
try:
hdr = parse_safetensors_header(url, self.token, self.header_cache)
wm = {k: single for k in hdr.tensors.keys()}
return wm, single
except Exception:
continue
return {}, "NOT_FOUND"
def file_url(self, filename: str) -> str:
return hf_url(self.repo_id, self.revision, filename)
def has(self, key: str) -> bool:
return key in self.weight_map
def tensor_info(self, key: str) -> Tuple[str, Tuple[int, ...], str, SafetensorsHeader, TensorInfo]:
shard = self.weight_map[key]
url = self.file_url(shard)
hdr = parse_safetensors_header(url, self.token, self.header_cache)
if key not in hdr.tensors:
raise KeyError(f"{key} not found inside shard header: {shard}")
info = hdr.tensors[key]
return shard, info.shape, info.dtype, hdr, info
def sample_windows(
self,
key: str,
chunk_elems: int,
windows: int,
seed: int,
view_numel: Optional[int] = None,
view_data_begin_rel: Optional[int] = None,
) -> Tuple[str, np.ndarray, int]:
shard, shape, dtype, hdr, info = self.tensor_info(key)
if dtype not in FLOAT_DTYPES:
raise ValueError(f"dtype {dtype} not supported for numeric sampling")
elem_size = DTYPE_SIZES[dtype]
full_numel = prod(shape)
data_begin_rel = info.data_offsets[0]
if view_numel is None:
view_numel = full_numel
if view_data_begin_rel is None:
view_data_begin_rel = data_begin_rel
if view_numel <= 0:
raise ValueError("view_numel must be positive")
raw_hasher = hashlib.sha256()
vals = []
total = 0
for w in range(max(1, windows)):
if view_numel <= chunk_elems:
start_elem = 0
n = view_numel
else:
s = stable_u64(f"{key}|{seed}|{w}")
start_elem = int(s % (view_numel - chunk_elems))
n = chunk_elems
abs_start = hdr.base_data_offset + view_data_begin_rel + start_elem * elem_size
abs_end_incl = abs_start + n * elem_size - 1
shard_url = self.file_url(shard)
buf = http_range_get(shard_url, abs_start, abs_end_incl, self.token)
raw_hasher.update(buf)
v = bytes_to_f32(buf, dtype)
vals.append(v)
total += len(v)
raw_hash = raw_hasher.hexdigest()
out = np.concatenate(vals, axis=0) if len(vals) > 1 else vals[0]
return raw_hash, out, total
def parse_layers_arg(s: str) -> List[int]:
s = s.strip()
if not s:
return []
parts = [p.strip() for p in s.split(",") if p.strip()]
out = []
for p in parts:
if "-" in p:
a, b = p.split("-", 1)
a = int(a.strip()); b = int(b.strip())
if b < a:
a, b = b, a
out.extend(list(range(a, b + 1)))
else:
out.append(int(p))
return sorted(set(out))
def compare_pair(
repo_a: RemoteRepo,
repo_b: RemoteRepo,
key: str,
chunk_elems: int,
windows: int,
seed: int,
) -> Dict[str, Any]:
"""Compare a single tensor between two repos."""
result = {
"key": key,
"status": "ok",
"a_shape": None,
"b_shape": None,
"a_dtype": None,
"b_dtype": None,
"cosine": float("nan"),
"rel_rmse": float("nan"),
"raw_hash_equal": None,
"note": "",
}
if not repo_a.has(key):
result["status"] = f"missing_in_{repo_a.name}"
return result
if not repo_b.has(key):
result["status"] = f"missing_in_{repo_b.name}"
return result
try:
_, a_shape, a_dtype, _, _ = repo_a.tensor_info(key)
_, b_shape, b_dtype, _, _ = repo_b.tensor_info(key)
result["a_shape"] = a_shape
result["b_shape"] = b_shape
result["a_dtype"] = a_dtype
result["b_dtype"] = b_dtype
if a_shape != b_shape or a_dtype != b_dtype:
result["status"] = "shape_mismatch"
return result
a_raw, a_vals, n1 = repo_a.sample_windows(key, chunk_elems, windows, seed)
b_raw, b_vals, n2 = repo_b.sample_windows(key, chunk_elems, windows, seed)
n = min(n1, n2)
a_vals = a_vals[:n]
b_vals = b_vals[:n]
result["cosine"] = cosine(a_vals, b_vals)
result["rel_rmse"] = rel_rmse(a_vals, b_vals)
result["raw_hash_equal"] = a_raw == b_raw
except Exception as e:
result["status"] = f"error:{type(e).__name__}"
result["note"] = str(e)
return result
def analyze_tokenizer_overlap(solar: RemoteRepo, glm: RemoteRepo, token: Optional[str]) -> Dict[str, Any]:
"""Analyze tokenizer vocabulary overlap."""
print("\n[TOKENIZER ANALYSIS]\n")
result = {
"solar_vocab_size": solar.config.get("vocab_size"),
"glm_vocab_size": glm.config.get("vocab_size"),
"vocab_extension": None,
}
sv = result["solar_vocab_size"]
gv = result["glm_vocab_size"]
if sv and gv:
result["vocab_extension"] = sv - gv
print(f"Solar vocab_size: {sv}")
print(f"GLM vocab_size: {gv}")
print(f"Vocabulary extension: {result['vocab_extension']} tokens")
if result["vocab_extension"] > 0:
print(f"=> Solar extended GLM's vocabulary by {result['vocab_extension']} tokens")
# Try to load tokenizer configs
for repo, name in [(solar, "Solar"), (glm, "GLM")]:
tok_url = hf_url(repo.repo_id, repo.revision, "tokenizer_config.json")
tok_cfg = http_get_json(tok_url, token)
if tok_cfg:
print(f"\n{name} tokenizer_config:")
for k in ["tokenizer_class", "model_max_length", "bos_token", "eos_token"]:
if k in tok_cfg:
print(f" {k}: {tok_cfg[k]}")
return result
def analyze_expert_patterns(solar: RemoteRepo, glm: RemoteRepo) -> Dict[str, Any]:
"""Analyze MoE expert weight patterns."""
print("\n[EXPERT PATTERN ANALYSIS]\n")
# Find expert keys
expert_pattern = re.compile(r"model\.layers\.(\d+)\.mlp\.experts\.(\d+)\.(gate_proj|up_proj|down_proj)\.weight")
solar_experts = defaultdict(set)
glm_experts = defaultdict(set)
for k in solar.weight_map.keys():
m = expert_pattern.match(k)
if m:
layer, exp_id = int(m.group(1)), int(m.group(2))
solar_experts[layer].add(exp_id)
for k in glm.weight_map.keys():
m = expert_pattern.match(k)
if m:
layer, exp_id = int(m.group(1)), int(m.group(2))
glm_experts[layer].add(exp_id)
solar_layers = sorted(solar_experts.keys())
glm_layers = sorted(glm_experts.keys())
result = {
"solar_moe_layers": len(solar_layers),
"glm_moe_layers": len(glm_layers),
"solar_experts_per_layer": len(solar_experts[solar_layers[0]]) if solar_layers else 0,
"glm_experts_per_layer": len(glm_experts[glm_layers[0]]) if glm_layers else 0,
}
print(f"Solar: {result['solar_moe_layers']} MoE layers, {result['solar_experts_per_layer']} experts each")
print(f"GLM: {result['glm_moe_layers']} MoE layers, {result['glm_experts_per_layer']} experts each")
return result
def create_visualization(
solar_glm_results: List[Dict],
solar_control_results: List[Dict],
glm_control_results: List[Dict],
out_dir: str,
) -> None:
"""Create visualization comparing Solar-GLM vs control baselines."""
if not HAS_MATPLOTLIB:
return
os.makedirs(out_dir, exist_ok=True)
# Group by tensor type
tensor_types = {
"input_layernorm": [],
"post_attention_layernorm": [],
"k_proj": [],
"v_proj": [],
"q_proj": [],
"mlp.gate": [],
}
def categorize(key: str) -> Optional[str]:
for t in tensor_types.keys():
if t in key:
return t
return None
# Collect cosines
for results, label in [
(solar_glm_results, "Solar-GLM"),
(solar_control_results, "Solar-Control"),
(glm_control_results, "GLM-Control"),
]:
for r in results:
if r["status"] == "ok" and not math.isnan(r["cosine"]):
cat = categorize(r["key"])
if cat:
tensor_types[cat].append((label, r["cosine"], r.get("layer", 0)))
# Plot 1: Distribution comparison by tensor type
fig, axes = plt.subplots(2, 3, figsize=(15, 10))
axes = axes.flatten()
colors = {"Solar-GLM": "red", "Solar-Control": "blue", "GLM-Control": "green"}
for idx, (ttype, data) in enumerate(tensor_types.items()):
ax = axes[idx]
for label in ["Solar-GLM", "Solar-Control", "GLM-Control"]:
vals = [d[1] for d in data if d[0] == label]
if vals:
ax.hist(vals, bins=30, alpha=0.5, label=label, color=colors[label])
ax.set_title(f"{ttype}")
ax.set_xlabel("Cosine Similarity")
ax.set_ylabel("Count")
ax.legend(fontsize=8)
ax.axvline(x=0, color='black', linestyle='--', alpha=0.3)
plt.tight_layout()
plt.savefig(os.path.join(out_dir, "cosine_distribution_by_type.png"), dpi=150)
plt.close()
# Plot 2: LayerNorm cosine by layer (most important)
fig, ax = plt.subplots(figsize=(14, 6))
for ttype in ["input_layernorm", "post_attention_layernorm"]:
data = tensor_types[ttype]
solar_glm = [(d[2], d[1]) for d in data if d[0] == "Solar-GLM"]
solar_ctrl = [(d[2], d[1]) for d in data if d[0] == "Solar-Control"]
if solar_glm:
solar_glm.sort()
layers, cos_vals = zip(*solar_glm)
style = '-o' if ttype == "input_layernorm" else '-s'
ax.plot(layers, cos_vals, style, label=f"Solar-GLM {ttype}",
color='red', markersize=4, alpha=0.8)
if solar_ctrl:
solar_ctrl.sort()
layers, cos_vals = zip(*solar_ctrl)
style = '--o' if ttype == "input_layernorm" else '--s'
ax.plot(layers, cos_vals, style, label=f"Solar-Control {ttype}",
color='blue', markersize=4, alpha=0.8)
ax.axhline(y=0, color='black', linestyle='--', alpha=0.3)
ax.axhline(y=0.95, color='green', linestyle=':', alpha=0.5, label='High similarity threshold (0.95)')
ax.set_xlabel("Layer")
ax.set_ylabel("Cosine Similarity")
ax.set_title("LayerNorm Weight Similarity: Solar-GLM vs Control Baseline")
ax.legend(loc='lower right')
ax.set_ylim(-0.1, 1.1)
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig(os.path.join(out_dir, "layernorm_by_layer.png"), dpi=150)
plt.close()
# Plot 3: Summary bar chart
fig, ax = plt.subplots(figsize=(12, 6))
summary = {}
for ttype in tensor_types.keys():
data = tensor_types[ttype]
for label in ["Solar-GLM", "Solar-Control"]:
vals = [abs(d[1]) for d in data if d[0] == label]
if vals:
key = f"{ttype}\n({label.split('-')[1]})"
summary[key] = np.mean(vals)
if summary:
keys = list(summary.keys())
vals = [summary[k] for k in keys]
colors_bar = ['red' if 'GLM' in k else 'blue' for k in keys]
bars = ax.bar(range(len(keys)), vals, color=colors_bar, alpha=0.7)
ax.set_xticks(range(len(keys)))
ax.set_xticklabels(keys, rotation=45, ha='right', fontsize=9)
ax.set_ylabel("Mean |Cosine|")
ax.set_title("Mean Absolute Cosine Similarity by Tensor Type\nRed=Solar-GLM, Blue=Solar-Control")
ax.axhline(y=0.1, color='orange', linestyle='--', alpha=0.5, label='Significance threshold')
# Add value labels
for bar, val in zip(bars, vals):
ax.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.01,
f'{val:.3f}', ha='center', va='bottom', fontsize=8)
plt.tight_layout()
plt.savefig(os.path.join(out_dir, "summary_comparison.png"), dpi=150)
plt.close()
print(f"\nVisualizations saved to {out_dir}/")
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--solar", default="upstage/Solar-Open-100B")
ap.add_argument("--glm", default="zai-org/GLM-4.5-Air")
ap.add_argument("--control", default="Qwen/Qwen2.5-72B-Instruct",
help="Control model for baseline comparison")
ap.add_argument("--rev_solar", default="main")
ap.add_argument("--rev_glm", default="main")
ap.add_argument("--rev_control", default="main")
ap.add_argument("--token", default=os.getenv("HF_TOKEN"))
ap.add_argument("--layers", default="0-45")
ap.add_argument("--windows", type=int, default=3)
ap.add_argument("--chunk_elems", type=int, default=262144)
ap.add_argument("--seed", type=int, default=0)
ap.add_argument("--out_dir", default="results")
ap.add_argument("--skip_control", action="store_true", help="Skip control model comparison")
args = ap.parse_args()
os.makedirs(args.out_dir, exist_ok=True)
layers = parse_layers_arg(args.layers)
print("=" * 70)
print("SOLAR-GLM DERIVATION ANALYSIS WITH CONTROL GROUP")
print("=" * 70)
# Load repos
print("\nLoading model indices...")
solar = RemoteRepo(args.solar, args.rev_solar, args.token)
glm = RemoteRepo(args.glm, args.rev_glm, args.token)
control = None
if not args.skip_control:
try:
control = RemoteRepo(args.control, args.rev_control, args.token)
print(f"Control model: {args.control}")
except Exception as e:
print(f"[WARN] Could not load control model: {e}")
control = None
# Config comparison
print("\n" + "=" * 70)
print("CONFIG COMPARISON")
print("=" * 70)
fields = [
"model_type", "hidden_size", "num_hidden_layers", "num_attention_heads",
"num_key_value_heads", "head_dim", "intermediate_size", "moe_intermediate_size",
"n_routed_experts", "n_shared_experts", "num_experts_per_tok", "first_k_dense_replace",
"rope_theta", "max_position_embeddings", "vocab_size", "num_nextn_predict_layers",
]
config_rows = []
for f in fields:
vs = solar.config.get(f)
vg = glm.config.get(f)
vc = control.config.get(f) if control else None
match = "==" if vs == vg else "!="
config_rows.append({"field": f, "solar": vs, "glm": vg, "control": vc, "match": match})
ctrl_str = f"Ctrl={vc}" if control else ""
print(f"{f:28s} Solar={str(vs):>12} GLM={str(vg):>12} {match} {ctrl_str}")
# Tokenizer analysis
tok_result = analyze_tokenizer_overlap(solar, glm, args.token)
# Expert pattern analysis
expert_result = analyze_expert_patterns(solar, glm)
# Weight comparison
print("\n" + "=" * 70)
print("WEIGHT SIMILARITY ANALYSIS")
print("=" * 70)
key_templates = [
"model.layers.{L}.self_attn.k_proj.weight",
"model.layers.{L}.self_attn.v_proj.weight",
"model.layers.{L}.self_attn.q_proj.weight",
"model.layers.{L}.input_layernorm.weight",
"model.layers.{L}.post_attention_layernorm.weight",
"model.layers.{L}.mlp.gate.weight",
]
solar_glm_results = []
solar_control_results = []
glm_control_results = []
print("\n[Solar vs GLM]")
for L in layers:
for tmpl in key_templates:
key = tmpl.format(L=L)
result = compare_pair(solar, glm, key, args.chunk_elems, args.windows, args.seed)
result["layer"] = L
solar_glm_results.append(result)
if result["status"] == "ok":
cos = result["cosine"]
ttype = key.split(".")[-2] if "layernorm" in key else key.split(".")[-3]
print(f" L{L:02d} {ttype:25s} cos={cos:+.6f}")
if control:
print("\n[Solar vs Control]")
for L in layers[:10]: # Sample fewer layers for control
for tmpl in key_templates:
key = tmpl.format(L=L)
result = compare_pair(solar, control, key, args.chunk_elems, args.windows, args.seed)
result["layer"] = L
solar_control_results.append(result)
print("\n[GLM vs Control]")
for L in layers[:10]:
for tmpl in key_templates:
key = tmpl.format(L=L)
result = compare_pair(glm, control, key, args.chunk_elems, args.windows, args.seed)
result["layer"] = L
glm_control_results.append(result)
# Statistical summary
print("\n" + "=" * 70)
print("STATISTICAL SUMMARY")
print("=" * 70)
def summarize(results: List[Dict], name: str) -> Dict:
by_type = defaultdict(list)
for r in results:
if r["status"] == "ok" and not math.isnan(r["cosine"]):
key = r["key"]
if "layernorm" in key:
ttype = "layernorm"
elif "k_proj" in key or "v_proj" in key or "q_proj" in key:
ttype = "attention"
elif "gate" in key:
ttype = "router"
else:
ttype = "other"
by_type[ttype].append(r["cosine"])
summary = {}
print(f"\n{name}:")
for ttype, vals in sorted(by_type.items()):
if vals:
mean_cos = np.mean(vals)
std_cos = np.std(vals)
max_cos = max(vals)
min_cos = min(vals)
summary[ttype] = {"mean": mean_cos, "std": std_cos, "max": max_cos, "min": min_cos, "n": len(vals)}
print(f" {ttype:15s}: mean={mean_cos:+.4f} std={std_cos:.4f} max={max_cos:+.4f} min={min_cos:+.4f} (n={len(vals)})")
return summary
sg_summary = summarize(solar_glm_results, "Solar vs GLM")
if solar_control_results:
sc_summary = summarize(solar_control_results, "Solar vs Control (baseline)")
if glm_control_results:
gc_summary = summarize(glm_control_results, "GLM vs Control (baseline)")
# Evidence interpretation
print("\n" + "=" * 70)
print("EVIDENCE INTERPRETATION")
print("=" * 70)
ln_cos = sg_summary.get("layernorm", {}).get("mean", 0)
attn_cos = sg_summary.get("attention", {}).get("mean", 0)
print(f"""
LayerNorm mean cosine: {ln_cos:.4f}
Attention mean cosine: {attn_cos:.4f}
FINDINGS:
""")
if ln_cos > 0.9:
print("* STRONG EVIDENCE: LayerNorm weights show extremely high similarity (>0.9)")
print(" => These weights were almost certainly COPIED from GLM-4.5-Air")
elif ln_cos > 0.5:
print("* MODERATE EVIDENCE: LayerNorm weights show significant similarity (>0.5)")
print(" => These weights are likely derived from GLM-4.5-Air")
if abs(attn_cos) < 0.01:
print("* Attention projection weights show ~0 correlation")
print(" => These were likely RETRAINED after architecture modification")
vocab_ext = tok_result.get("vocab_extension", 0)
if vocab_ext and vocab_ext > 0:
print(f"* TOKENIZER EXTENSION: +{vocab_ext} tokens added")
print(" => Consistent with continual pretraining with expanded vocabulary")
print(f"""
CONCLUSION:
The evidence strongly suggests Solar-Open-100B was derived from GLM-4.5-Air through:
1. Tokenizer extension (+{vocab_ext} tokens)
2. Attention head reduction (96 -> 64 heads)
3. Addition of 2 layers (46 -> 48)
4. Removal of MTP (nextn_predict) layer
5. Continual pretraining that preserved LayerNorm but retrained attention/MoE
""")
# Create visualization
if HAS_MATPLOTLIB:
create_visualization(
solar_glm_results,
solar_control_results,
glm_control_results,
args.out_dir
)
# Save results
csv_path = os.path.join(args.out_dir, "solar_vs_glm_detailed.csv")
with open(csv_path, "w", newline="", encoding="utf-8") as f:
fieldnames = ["layer", "key", "status", "cosine", "rel_rmse", "raw_hash_equal",
"a_shape", "b_shape", "note"]
w = csv.DictWriter(f, fieldnames=fieldnames, extrasaction='ignore')
w.writeheader()
for r in solar_glm_results:
r["a_shape"] = str(r.get("a_shape", ""))
r["b_shape"] = str(r.get("b_shape", ""))
w.writerow(r)
print(f"\nResults saved to {args.out_dir}/")
if __name__ == "__main__":
main()