HuggingFaceFW/fineweb-edu
Viewer • Updated • 3.5B • 481k • 1.49k
KenExp-Coder-113M is a 113.4M-parameter (~37M active parameters per token) Small Language Model (SLM) built from scratch in PyTorch for code generation, debugging, and long-context code comprehension (24,576-token context window).
Inspired by DeepSeek-V4-Flash architectural and optimization efficiency, it combines Manifold-Constrained Hyper-Connections (mHC), Fine-Grained Mixture-of-Experts (MoE) with auxiliary-loss-free load balancing, 5:1 Grouped-Query Attention (GQA), progressive 24K RoPE context extension, and the Muon orthogonalized momentum optimizer.
| Specification | Value |
|---|---|
| Total Parameters | 113,360,480 (~113.4M) |
| Active Parameters / Token | ~37M (Dense Spine + Shared Expert + Top-2 Routed Experts) |
| Checkpoint Precision & Size | bfloat16 (226.8 MB in model.safetensors) |
| Transformer Layers | 10 Blocks |
Hidden Dimension (d_model) |
640 |
| Attention Mechanism | Grouped-Query Attention (10 Query Heads, 2 KV Heads, head_dim = 64) |
| Residual Topology | 4-Stream mHC (Sinkhorn-Knopp doubly-stochastic stream mixing) |
| Feed-Forward / MoE | 1 Shared SwiGLU Expert (inter_dim = 640) + 8 Routed SwiGLU Experts (inter_dim = 384, top_k = 2) |
| Expert Load Balancing | Auxiliary-loss-free dynamic router bias adjustment |
| Positional Encoding | Rotary Position Embeddings (RoPE, base = 1,000,000.0) |
| Maximum Context Window | 24,576 tokens (24K) |
| Tokenizer & Vocabulary | tiktoken (gpt2 BPE, 50,304 padded vocabulary) |
KenExp-Coder-113M was trained from scratch via a 3-stage curriculum designed for fast convergence and stable 24K context generalization:
| Stage | Objective & Context | Dataset(s) | Volume & Details |
|---|---|---|---|
| Stage 1: Code & Reasoning Pre-training | Base syntax, algorithms, and technical English at 2,048 context (rope_base = 10,000) |
codeparrot/codeparrot-clean (85%) + HuggingFaceFW/fineweb-edu sample-10BT (15%) |
~82M tokens (2,500 steps × 32,768 tokens/step) with cosine LR decay and torch.compile |
| Stage 1.5: 24K Context Extension | Long-range code dependency adaptation at 24,576 context (rope_base = 1,000,000) |
Mixed Python & technical corpus (codeparrot-clean + fineweb-edu) |
~6M long-context tokens (120 steps × 49,152 tokens/step) with activation checkpointing and chunked cross-entropy |
| Stage 2: Agentic Coding & Debugging SFT | Multi-turn bug fixing, step-by-step reasoning, and complete code generation up to 24,576 context | DSFFGFG456/fable-5-coding-and-debugging-traces |
3,000 length-bucketed trajectories trained with final-assistant-only loss masking (-100 on prior turns) to prevent overfitting on cumulative trajectory prefixes |
bfloat16 mixed precision, TF32 tensor cores, selective gradient checkpointing (>4,096 tokens), and 4,096-token chunked cross-entropy computation (reducing 24K logit peak memory from 4.61 GiB to <0.8 GiB)bfloat16 and runs at high token speeds on consumer GPUs or modern CPUs.pip install torch tiktoken safetensors huggingface_hub
import importlib.util
import tiktoken
import torch
import torch.nn.functional as F
from huggingface_hub import hf_hub_download
REPO_ID = "KenSensei/KenExp-Coder-113M"
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# Download and load standalone modeling_kenexp.py architecture
mod_path = hf_hub_download(repo_id=REPO_ID, filename="modeling_kenexp.py")
spec = importlib.util.spec_from_file_location("modeling_kenexp", mod_path)
modeling_kenexp = importlib.util.module_from_spec(spec)
spec.loader.exec_module(modeling_kenexp)
model = modeling_kenexp.load_kenexp_coder(REPO_ID, device=DEVICE)
enc = tiktoken.get_encoding("gpt2")
EOT = enc.eot_token
prompt = "<|user|>\nWrite a Python function to merge overlapping intervals.\n<|assistant|>\n"
ids = enc.encode_ordinary(prompt)
x = torch.tensor([ids], dtype=torch.long, device=DEVICE)
with torch.no_grad():
for _ in range(256):
logits = model(x[:, -24576:])[:, -1, :] / 0.25
v, _ = torch.topk(logits, 40)
logits[logits < v[:, [-1]]] = -float("inf")
next_tok = torch.multinomial(F.softmax(logits, dim=-1), num_samples=1)
x = torch.cat([x, next_tok], dim=1)
if next_tok.item() == EOT:
break
print(enc.decode(x[0, len(ids):].tolist()).replace("<|endoftext|>", ""))