Download src_main.py from gamer729/cognithread-ssm: direct link, hf CLI and curl.
- Browser
- Download file 5.33 kB
-
https://huggingface.co/gamer729/cognithread-ssm/resolve/main/src_main.py
- Command line
-
hf download hf://gamer729/cognithread-ssm/src_main.py
-
curl -L -o src_main.py https://huggingface.co/gamer729/cognithread-ssm/resolve/main/src_main.py
5.33 kB
| ############################################################################### | |
| # MAIN: Build, Train, Export | |
| ############################################################################### | |
| def build_variant(name, d_model, d_state, n_heuristics, d_z, T_diff, expand): | |
| print(f"\n--- Building variant: {name} (d_model={d_model}, d_state={d_state}) ---") | |
| key = jax.random.key(42) | |
| rngs = nn.Rngs(key) | |
| model = CogniThreadModel(d_model, d_state, n_heuristics, d_z, T_diff, | |
| expand=expand, rngs=rngs) | |
| # Quick forward pass test | |
| x = jax.random.normal(jax.random.key(1), (2, 8, d_model)) | |
| y_final, var_y, w_f, mu = model(x, key=jax.random.key(2)) | |
| print(f" Forward: y_final={y_final.shape}, var={var_y.shape}, w_f={w_f.shape}") | |
| print(f" w_f=[{float(jnp.min(w_f)):.4f}, {float(jnp.max(w_f)):.4f}]") | |
| print(f" var=[{float(jnp.min(var_y)):.6f}, {float(jnp.max(var_y)):.6f}]") | |
| return model | |
| def export_gguf(model, name, output_dir): | |
| """Export model to GGUF with wave-scalar tensor encoding.""" | |
| params = extract_params(model) | |
| filepath = os.path.join(output_dir, f"{name}.gguf") | |
| writer = GGUFWriter(filepath, arch="cognithread-ssm") | |
| # Metadata | |
| writer.add_string("general.architecture", "cognithread-ssm") | |
| writer.add_string("general.name", name) | |
| writer.add_uint32("cognithread.d_model", model.d_model) | |
| writer.add_uint32("cognithread.d_state", model.d_state) | |
| writer.add_uint32("cognithread.d_z", model.d_z) | |
| writer.add_string("cognithread.encoding", "wave_scalar_thermal_analog") | |
| writer.add_string("cognithread.encoding.description", | |
| "5-parameter wave encoding per scalar: amplitude(A), phase_shift(phi), " | |
| "frequency(f), period(T=1/f), fractal_dim(D). " | |
| "v(t) = A*sin(2*pi*f*t+phi)*fractal_mod(t,D). " | |
| "No bit precision (8/16/quantized) — pure wave-scalar radionics-like analog representation.") | |
| writer.add_uint32("cognithread.wave_scalar.n_harmonics", 8) | |
| # Find wave-scalar parameter groups | |
| # WaveScalar params end with .amplitude, .phase_shift, .frequency, .fractal_dim | |
| ws_groups = {} | |
| regular_params = {} | |
| for pname, pdata in params.items(): | |
| is_ws = False | |
| for suffix in ['amplitude', 'phase_shift', 'frequency', 'fractal_dim']: | |
| if pname.endswith('.' + suffix): | |
| base = pname[:-len(suffix) - 1] | |
| if base not in ws_groups: | |
| ws_groups[base] = {} | |
| ws_groups[base][suffix] = pdata | |
| is_ws = True | |
| break | |
| if not is_ws: | |
| regular_params[pname] = pdata | |
| # Write regular (non-wave-scalar) tensors | |
| for pname, pdata in regular_params.items(): | |
| writer.add_f32_tensor(pname, pdata) | |
| # Write wave-scalar tensors (5 param arrays + combined interleaved) | |
| ws_count = 0 | |
| for base, parts in ws_groups.items(): | |
| if all(s in parts for s in ['amplitude', 'phase_shift', 'frequency', 'fractal_dim']): | |
| writer.add_wave_scalar_tensor( | |
| base, parts['amplitude'], parts['phase_shift'], | |
| parts['frequency'], parts['fractal_dim']) | |
| ws_count += 1 | |
| else: | |
| # Partial group — write individually | |
| for suf, data in parts.items(): | |
| writer.add_f32_tensor(f"{base}.{suf}", data) | |
| writer.write() | |
| print(f" Wave-scalar groups: {ws_count}") | |
| return filepath | |
| def verify_gguf(filepath): | |
| """Verify GGUF file header is valid.""" | |
| with open(filepath, 'rb') as f: | |
| magic = struct.unpack('<I', f.read(4))[0] | |
| ver = struct.unpack('<I', f.read(4))[0] | |
| nt = struct.unpack('<Q', f.read(8))[0] | |
| nk = struct.unpack('<Q', f.read(8))[0] | |
| assert magic == GGUF_MAGIC, f"Bad magic: 0x{magic:08X}" | |
| assert ver == GGUF_VERSION, f"Bad version: {ver}" | |
| print(f" Verified: magic=0x{magic:08X} ver={ver} tensors={nt} kvs={nk}") | |
| return True | |
| def main(): | |
| output_dir = "/tmp/cognithread_output" | |
| os.makedirs(output_dir, exist_ok=True) | |
| # Define 3 variants | |
| variants = [ | |
| ("cognithread-small", 64, 8, 32, 16, 5, 2), | |
| ("cognithread-medium", 128, 16, 64, 32, 10, 2), | |
| ("cognithread-large", 256, 32, 128, 64, 10, 2), | |
| ] | |
| results = [] | |
| for name, d_model, d_state, n_heur, d_z, T_diff, expand in variants: | |
| # Build model | |
| model = build_variant(name, d_model, d_state, n_heur, d_z, T_diff, expand) | |
| # Train on PILE with wave-scalar computing | |
| model, losses = train_model(model, d_model, n_steps=30, lr=1e-3) | |
| # Export to GGUF with wave-scalar encoding | |
| filepath = export_gguf(model, name, output_dir) | |
| # Verify | |
| verify_gguf(filepath) | |
| results.append((name, filepath, losses[-1])) | |
| print("\n" + "=" * 60) | |
| print("=== COGNITHREAD SSM — EXPORT COMPLETE ===") | |
| print("=" * 60) | |
| for name, filepath, final_loss in results: | |
| sz = os.path.getsize(filepath) | |
| print(f" {name}: {sz:,} bytes, final_loss={final_loss:.4f}") | |
| print(f" GGUF: {filepath}") | |
| print("\nAll 3 variants exported with wave-scalar thermal-analog encoding.") | |
| print("No bit precision (8/16/quantized) — pure wave-scalar radionics representation.") | |
| if __name__ == "__main__": | |
| main() | |