Text Generation
Transformers
Safetensors
corm
mixture-of-experts
Mixture of Experts
custom_code
🇪🇺 Region: EU
Run this model with HF Jobs
Run ilsp/CoRM-182M-top1 on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs and TRL to fine-tune https://huggingface.co/ilsp/CoRM-182M-top1. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/ilsp/CoRM-182M-top1 on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Evaluate on a benchmark
Use HF Jobs to evaluate https://huggingface.co/ilsp/CoRM-182M-top1. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
Quantize for lower memory use
Use HF Jobs to create a 4-bit version of https://huggingface.co/ilsp/CoRM-182M-top1. Push it to a new model repo, then reload it from there and report its memory footprint. Run `hf jobs --help` first to learn the CLI.
Instructions to use ilsp/CoRM-182M-top1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ilsp/CoRM-182M-top1 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="ilsp/CoRM-182M-top1", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("ilsp/CoRM-182M-top1", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use ilsp/CoRM-182M-top1 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "ilsp/CoRM-182M-top1" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ilsp/CoRM-182M-top1", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/ilsp/CoRM-182M-top1
- SGLang
How to use ilsp/CoRM-182M-top1 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "ilsp/CoRM-182M-top1" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ilsp/CoRM-182M-top1", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "ilsp/CoRM-182M-top1" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ilsp/CoRM-182M-top1", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use ilsp/CoRM-182M-top1 with Docker Model Runner:
docker model run hf.co/ilsp/CoRM-182M-top1
Upload modeling_corm.py with huggingface_hub
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329import mathimport torchimport torch.nn as nnimport torch.nn.functional as Ffrom typing import Optional, Tuple, Type
from transformers import PreTrainedModel, GenerationMixinfrom transformers.modeling_outputs import CausalLMOutputWithPastfrom .configuration_corm import CoRMConfig
# -------------------------# Small building blocks# -------------------------class RMSNorm(nn.Module): def __init__(self, hidden_size, eps): super().__init__() self.weight = nn.Parameter(torch.ones(hidden_size)) self.eps = eps
def forward(self, x): var = x.pow(2).mean(-1, keepdim=True) x = x * torch.rsqrt(var + self.eps) return self.weight * x
def rotate_half(x): x1, x2 = x.chunk(2, dim=-1) return torch.cat([-x2, x1], dim=-1)
def apply_rope(q, k, cos, sin): cos = cos[None, None, :, :] sin = sin[None, None, :, :] q = (q * cos) + (rotate_half(q) * sin) k = (k * cos) + (rotate_half(k) * sin) return q, k
class RotaryEmbedding(nn.Module): def __init__(self, dim, max_seq_len, base): super().__init__() inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim)) self.register_buffer("inv_freq", inv_freq, persistent=False) t = torch.arange(max_seq_len).float() freqs = torch.outer(t, self.inv_freq) emb = torch.cat([freqs, freqs], dim=-1) self.register_buffer("cos_cached", emb.cos(), persistent=False) self.register_buffer("sin_cached", emb.sin(), persistent=False)
def forward(self, seq_len, device, dtype): cos = self.cos_cached[:seq_len].to(device=device, dtype=dtype) sin = self.sin_cached[:seq_len].to(device=device, dtype=dtype) return cos, sin
# -------------------------# CoRM Router# -------------------------class CoRM(nn.Module): """ CoRM: Contrastive Routing Mixture-of-Experts router.
Routing logit for expert e: gap[e] = [Q_e(x) - Q_e(null)] · K(x) / sqrt(d2)
Weights: key: [d2, H] shared key projection (no bias) expert_queries: [E*d2, H] per-expert query projection (no bias) global_null_mean: buffer [H] EMA of LN'd hidden states (not saved in DCP → zeros) routing_temperature: [1] """
def __init__(self, config: CoRMConfig): super().__init__() H = config.hidden_size E = config.num_local_experts d2 = config.expert_attn_size
self.num_experts = E self.expert_attn_size = d2 self.top_k = config.num_experts_per_tok
self.key = nn.Linear(H, d2, bias=False) self.expert_queries = nn.Linear(H, E * d2, bias=False)
# EMA null state — zero-initialised; updated during warmup (train mode) self.register_buffer('global_null_mean', torch.zeros(H)) self.register_buffer('ema_initialized', torch.tensor(False)) self.momentum = getattr(config, 'router_background_momentum', 0.01) self.use_l2_normalize = getattr(config, 'moe_router_use_l2_normalize', True)
self.routing_temperature = nn.Parameter( torch.tensor(config.routing_temperature_init) )
def gating_logits(self, x: torch.Tensor) -> torch.Tensor: B, T, H = x.shape N = B * T E = self.num_experts d2 = self.expert_attn_size
real = x.reshape(N, H) real_n = F.layer_norm(real, (H,), eps=1e-5)
# Update EMA in train mode (mirrors Megatron router exactly) if self.training: with torch.no_grad(): batch_mean = real_n.mean(dim=0).detach() if not self.ema_initialized.item(): self.global_null_mean.copy_(batch_mean) self.ema_initialized.fill_(True) else: self.global_null_mean.mul_(1 - self.momentum).add_( self.momentum * batch_mean )
K = self.key(real_n) # [N, d2] if self.use_l2_normalize: K = F.normalize(K, p=2, dim=-1)
Q_real = self.expert_queries(real_n).view(N, E, d2) # [N, E, d2] if self.use_l2_normalize: Q_real = F.normalize(Q_real, p=2, dim=-1)
# Null queries: EMA is already in post-LN space, don't re-layernorm Q_null = self.expert_queries(self.global_null_mean).view(E, d2) # [E, d2] if self.use_l2_normalize: Q_null = F.normalize(Q_null, p=2, dim=-1)
scale = 1.0 / math.sqrt(d2) attn_real = (Q_real * K.unsqueeze(1)).sum(-1) * scale # [N, E] attn_null = (Q_null.unsqueeze(0) * K.unsqueeze(1)).sum(-1) * scale # [N, E]
gap = attn_real - attn_null
temp = torch.clamp(self.routing_temperature, min=0.1, max=10.0) router_logits = torch.clamp(gap * temp, min=-10.0, max=10.0)
return router_logits.view(B, T, E)
def topk_routing(self, logits: torch.Tensor): if self.top_k == 1: probs = torch.softmax(logits, dim=-1, dtype=torch.float32) routing_weights, selected_experts = torch.topk(probs, self.top_k, dim=-1) else: topk_logits, selected_experts = torch.topk(logits, self.top_k, dim=-1) routing_weights = torch.softmax(topk_logits, dim=-1, dtype=torch.float32) return selected_experts, routing_weights
# Only "corm" is supported — the only router type ever exported through the# HF conversion pipeline.ROUTER_REGISTRY = { "corm": CoRM,}
def get_router_class(router_type: str) -> Type[nn.Module]: """ Get router class by type string.
Args: router_type: Must be "corm".
Returns: Router class
Raises: ValueError: If router_type is not recognized """ if router_type not in ROUTER_REGISTRY: available = ", ".join(ROUTER_REGISTRY.keys()) raise ValueError( f"Unknown router type '{router_type}'. " f"Available types: {available}" ) return ROUTER_REGISTRY[router_type]
# -------------------------# Expert MLP (Mixtral-like)# -------------------------class SwiGLUExpert(nn.Module): def __init__(self, H, I): super().__init__() self.w1 = nn.Linear(H, I, bias=False) # gate_proj self.w3 = nn.Linear(H, I, bias=False) # up_proj self.w2 = nn.Linear(I, H, bias=False) # down_proj
def forward(self, x): return self.w2(F.silu(self.w1(x)) * self.w3(x))
class CoRMMoE(nn.Module): """MoE layer using the CoRM router (no shared expert)."""
def __init__(self, config: CoRMConfig): super().__init__()
router_class = get_router_class(config.router_type) self.router = router_class(config)
H = config.hidden_size I = config.intermediate_size E = config.num_local_experts self.experts = nn.ModuleList([SwiGLUExpert(H, I) for _ in range(E)]) self.top_k = config.num_experts_per_tok
def forward(self, x): B, T, H = x.shape logits = self.router.gating_logits(x) selected_experts, routing_weights = self.router.topk_routing(logits)
y = torch.zeros_like(x)
for k_idx in range(self.top_k): expert_indices = selected_experts[:, :, k_idx] weights = routing_weights[:, :, k_idx]
for e, expert in enumerate(self.experts): mask = (expert_indices == e) if mask.any(): xe = x[mask] ye = expert(xe) y[mask] += ye * weights[mask].unsqueeze(-1).to(ye.dtype)
return y
# -------------------------# Transformer layer# -------------------------class SelfAttention(nn.Module): def __init__(self, config: CoRMConfig): super().__init__() H = config.hidden_size nh = config.num_attention_heads nk = config.num_key_value_heads self.head_dim = H // nh self.nh = nh self.nk = nk
self.q_proj = nn.Linear(H, H, bias=False) self.k_proj = nn.Linear(H, nk * self.head_dim, bias=False) self.v_proj = nn.Linear(H, nk * self.head_dim, bias=False) self.o_proj = nn.Linear(H, H, bias=False)
self.rope = RotaryEmbedding(self.head_dim, config.max_position_embeddings, config.rope_theta)
def forward(self, x, attn_mask=None): B, T, H = x.shape q = self.q_proj(x).view(B, T, self.nh, self.head_dim).transpose(1, 2) k = self.k_proj(x).view(B, T, self.nk, self.head_dim).transpose(1, 2) v = self.v_proj(x).view(B, T, self.nk, self.head_dim).transpose(1, 2)
# repeat kv to match query heads rep = self.nh // self.nk k = k.repeat_interleave(rep, dim=1) v = v.repeat_interleave(rep, dim=1)
cos, sin = self.rope(T, x.device, x.dtype) q, k = apply_rope(q, k, cos, sin)
# causal mask scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.head_dim) causal = torch.triu(torch.ones(T, T, device=x.device, dtype=torch.bool), diagonal=1) scores = scores.masked_fill(causal, float("-inf"))
p = torch.softmax(scores, dim=-1, dtype=torch.float32).to(x.dtype) out = torch.matmul(p, v) out = out.transpose(1, 2).contiguous().view(B, T, H) return self.o_proj(out)
class Block(nn.Module): def __init__(self, config: CoRMConfig): super().__init__() self.input_layernorm = RMSNorm(config.hidden_size, config.rms_norm_eps) self.self_attn = SelfAttention(config) self.post_attention_layernorm = RMSNorm(config.hidden_size, config.rms_norm_eps) self.moe = CoRMMoE(config)
def forward(self, x): x = x + self.self_attn(self.input_layernorm(x)) x = x + self.moe(self.post_attention_layernorm(x)) return x
class CoRMForCausalLM(PreTrainedModel, GenerationMixin): config_class = CoRMConfig main_input_name = "input_ids"
def __init__(self, config: CoRMConfig): super().__init__(config) self.model = nn.Module() self.model.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size) self.model.layers = nn.ModuleList([Block(config) for _ in range(config.num_hidden_layers)]) self.model.norm = RMSNorm(config.hidden_size, config.rms_norm_eps) self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
self.post_init()
def forward( self, input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, labels: Optional[torch.LongTensor] = None, **kwargs, ): x = self.model.embed_tokens(input_ids)
for layer in self.model.layers: x = layer(x)
x = self.model.norm(x) logits = self.lm_head(x)
loss = None if labels is not None: shift_logits = logits[:, :-1].contiguous() shift_labels = labels[:, 1:].contiguous() loss = F.cross_entropy( shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1), ignore_index=-100, )
return CausalLMOutputWithPast( loss=loss, logits=logits, past_key_values=None, )modeling_corm.pyAdded+329