Text Generation
Transformers
PyTorch
English
tinymixtral
Mixture of Experts
mixture-of-experts
small-model
pretraining
from-scratch
conversational
custom_code
Instructions to use mikecovlee/tinymixtral-v2.0-beta with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use mikecovlee/tinymixtral-v2.0-beta with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="mikecovlee/tinymixtral-v2.0-beta", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("mikecovlee/tinymixtral-v2.0-beta", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use mikecovlee/tinymixtral-v2.0-beta with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "mikecovlee/tinymixtral-v2.0-beta" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "mikecovlee/tinymixtral-v2.0-beta", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/mikecovlee/tinymixtral-v2.0-beta
- SGLang
How to use mikecovlee/tinymixtral-v2.0-beta 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 "mikecovlee/tinymixtral-v2.0-beta" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "mikecovlee/tinymixtral-v2.0-beta", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'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 "mikecovlee/tinymixtral-v2.0-beta" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "mikecovlee/tinymixtral-v2.0-beta", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use mikecovlee/tinymixtral-v2.0-beta with Docker Model Runner:
docker model run hf.co/mikecovlee/tinymixtral-v2.0-beta
File size: 12,140 Bytes
fd1a388 0f358ac fd1a388 0f358ac fd1a388 0f358ac fd1a388 0f358ac fd1a388 0f358ac fd1a388 | 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 | # Copyright (C) Michael Lee (李登淳) 2026. All rights reserved.
# Open-source under the MIT License. See LICENSE for details.
from dataclasses import dataclass
from typing import Optional
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.checkpoint import checkpoint
from transformers import PreTrainedModel
from .configuration_tinymixtral import TinyMixtralConfig
# ============================================================
# Layers
# ============================================================
class RMSNorm(nn.Module):
def __init__(self, dim: int, eps: float = 1e-6):
super().__init__()
self.weight = nn.Parameter(torch.ones(dim))
self.eps = eps
def forward(self, x):
dtype = x.dtype
x = x.float()
norm = x.pow(2).mean(-1, keepdim=True)
x = x * torch.rsqrt(norm + self.eps)
return (x * self.weight).to(dtype)
class RotaryEmbedding(nn.Module):
def __init__(self, dim, max_position_embeddings=2048, theta=10000.0):
super().__init__()
self.dim = dim
self.max_position_embeddings = max_position_embeddings
self.theta = theta
self._build_cache()
def _build_cache(self):
inv_freq = 1.0 / (self.theta ** (torch.arange(0, self.dim, 2).float() / self.dim))
t = torch.arange(self.max_position_embeddings).float()
freqs = torch.outer(t, 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, x, position_ids):
cos = self.cos_cached[position_ids].unsqueeze(1)
sin = self.sin_cached[position_ids].unsqueeze(1)
x_rot = x.float()
x1, x2 = x_rot.chunk(2, dim=-1)
rotated = torch.cat((-x2, x1), dim=-1)
return (x_rot * cos + rotated * sin).to(x.dtype)
class GQAAttention(nn.Module):
def __init__(self, config):
super().__init__()
self.hidden_size = config.hidden_size
self.num_heads = config.num_attention_heads
self.num_kv_heads = config.num_key_value_heads
self.head_dim = config.head_dim
self.num_groups = self.num_heads // self.num_kv_heads
assert self.num_heads % self.num_kv_heads == 0
self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
self.k_proj = nn.Linear(self.hidden_size, self.num_kv_heads * self.head_dim, bias=False)
self.v_proj = nn.Linear(self.hidden_size, self.num_kv_heads * self.head_dim, bias=False)
self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
self.rotary_emb = RotaryEmbedding(self.head_dim, config.max_position_embeddings, config.rope_theta)
self.attention_dropout = config.attention_dropout
def forward(self, hidden_states, attention_mask=None, position_ids=None):
B, S, _ = hidden_states.shape
q = self.q_proj(hidden_states).view(B, S, self.num_heads, self.head_dim).transpose(1, 2)
k = self.k_proj(hidden_states).view(B, S, self.num_kv_heads, self.head_dim).transpose(1, 2)
v = self.v_proj(hidden_states).view(B, S, self.num_kv_heads, self.head_dim).transpose(1, 2)
if position_ids is None:
position_ids = torch.arange(S, device=hidden_states.device).unsqueeze(0).expand(B, -1)
q, k = self.rotary_emb(q, position_ids), self.rotary_emb(k, position_ids)
if attention_mask is not None:
k_exp = k.unsqueeze(2).expand(-1, -1, self.num_groups, -1, -1).reshape(B, self.num_heads, S, self.head_dim)
v_exp = v.unsqueeze(2).expand(-1, -1, self.num_groups, -1, -1).reshape(B, self.num_heads, S, self.head_dim)
causal = torch.tril(torch.ones(S, S, device=hidden_states.device, dtype=torch.bool))
combined = causal[None, None, :, :] & attention_mask[:, None, None, :]
attn = F.scaled_dot_product_attention(
q, k_exp, v_exp, attn_mask=combined,
dropout_p=self.attention_dropout if self.training else 0.0,
is_causal=False,
)
else:
attn = F.scaled_dot_product_attention(
q, k, v, attn_mask=None,
dropout_p=self.attention_dropout if self.training else 0.0,
is_causal=True,
enable_gqa=True,
)
return self.o_proj(attn.transpose(1, 2).reshape(B, S, -1))
class SparseMoE(nn.Module):
def __init__(self, config):
super().__init__()
self.hidden_size = config.hidden_size
self.num_shared = config.num_shared_experts
self.num_routed = config.num_routed_experts
self.top_k = config.num_experts_per_tok
self.expert_intermediate = config.expert_intermediate_size
self.jitter_noise = config.router_jitter_noise
self.aux_loss_coef = config.router_aux_loss_coef
if self.num_routed > 0:
self.router = nn.Linear(self.hidden_size, self.num_routed, bias=False)
if self.num_shared > 0:
self.shared_gate_proj = nn.Parameter(torch.empty(self.num_shared, self.expert_intermediate, self.hidden_size))
self.shared_up_proj = nn.Parameter(torch.empty(self.num_shared, self.expert_intermediate, self.hidden_size))
self.shared_down_proj = nn.Parameter(torch.empty(self.num_shared, self.hidden_size, self.expert_intermediate))
if self.num_routed > 0:
self.gate_proj = nn.Parameter(torch.empty(self.num_routed, self.expert_intermediate, self.hidden_size))
self.up_proj = nn.Parameter(torch.empty(self.num_routed, self.expert_intermediate, self.hidden_size))
self.down_proj = nn.Parameter(torch.empty(self.num_routed, self.hidden_size, self.expert_intermediate))
self._init_weights()
def _init_weights(self, std=0.02):
for name in ("shared_gate_proj", "shared_up_proj", "shared_down_proj",
"gate_proj", "up_proj", "down_proj"):
param = getattr(self, name, None)
if param is not None:
nn.init.normal_(param, std=std)
def _forward_expert(self, x, gate_w, up_w, down_w):
gate = F.silu(x @ gate_w.T)
up = x @ up_w.T
return (gate * up) @ down_w.T
def forward(self, x):
B, S, D = x.shape
x_flat = x.view(-1, D)
out = torch.zeros(B * S, D, device=x.device, dtype=x.dtype)
# Shared experts: always active
for e in range(self.num_shared):
out += self._forward_expert(x_flat, self.shared_gate_proj[e], self.shared_up_proj[e], self.shared_down_proj[e])
aux = torch.tensor(0.0, device=x.device, dtype=x.dtype)
if self.num_routed == 0:
return out.view(B, S, D), aux
# Routed experts: top-k gating
logits = self.router(x_flat)
if self.training and self.jitter_noise > 0:
logits = logits * (1 + torch.randn_like(logits) * self.jitter_noise)
weights = F.softmax(logits.float(), dim=-1).to(x.dtype)
w_topk, experts = torch.topk(weights, self.top_k, dim=-1)
w_topk = w_topk / w_topk.sum(dim=-1, keepdim=True)
if self.training and self.aux_loss_coef > 0:
with torch.no_grad():
mask = F.one_hot(experts, num_classes=self.num_routed).float()
f_i = mask.mean(dim=(0, 1))
P_i = weights.mean(dim=0)
aux = (f_i.detach() * P_i).sum() * self.num_routed
N = B * S
flat_experts = experts.view(-1)
flat_weights = w_topk.view(-1)
flat_token_idx = torch.arange(N, device=x.device).unsqueeze(1).expand(-1, self.top_k).reshape(-1)
sorted_indices = flat_experts.argsort(stable=True)
sorted_token_idx = flat_token_idx[sorted_indices]
sorted_weights = flat_weights[sorted_indices]
sorted_experts = flat_experts[sorted_indices]
expert_counts = torch.bincount(sorted_experts, minlength=self.num_routed).tolist()
start = 0
for e in range(self.num_routed):
count = expert_counts[e]
if count == 0:
continue
end = start + count
idx = sorted_token_idx[start:end]
w = sorted_weights[start:end]
ts = x_flat[idx]
expert_out = self._forward_expert(ts, self.gate_proj[e], self.up_proj[e], self.down_proj[e])
out.index_add_(0, idx, (expert_out * w.unsqueeze(-1)).to(x.dtype))
start = end
return out.view(B, S, D), aux
class MoETransformerBlock(nn.Module):
def __init__(self, config):
super().__init__()
self.input_layernorm = RMSNorm(config.hidden_size, config.rms_norm_eps)
self.post_attention_layernorm = RMSNorm(config.hidden_size, config.rms_norm_eps)
self.self_attn = GQAAttention(config)
self.moe = SparseMoE(config)
def forward(self, x, attention_mask=None, position_ids=None):
x = x + self.self_attn(self.input_layernorm(x), attention_mask, position_ids)
h, aux = self.moe(self.post_attention_layernorm(x))
return x + h, aux
# ============================================================
# Causal LM
# ============================================================
@dataclass
class CausalLMOutputWithPast:
loss: Optional[torch.Tensor] = None
logits: torch.Tensor = None
class TinyMixtralForCausalLM(PreTrainedModel):
config_class = TinyMixtralConfig
base_model_prefix = "tinymixtral"
supports_gradient_checkpointing = True
_no_split_modules = ["MoETransformerBlock"]
def __init__(self, config):
super().__init__(config)
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size)
self.layers = nn.ModuleList([MoETransformerBlock(config) for _ in range(config.num_hidden_layers)])
self.norm = RMSNorm(config.hidden_size, config.rms_norm_eps)
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
if config.tie_word_embeddings:
self.lm_head.weight = self.embed_tokens.weight
self._use_activation_checkpointing = False
self.post_init()
def _init_weights(self, module):
std = self.config.initializer_range
if isinstance(module, nn.Linear):
module.weight.data.normal_(mean=0.0, std=std)
if module.bias is not None:
module.bias.data.zero_()
elif isinstance(module, nn.Embedding):
module.weight.data.normal_(mean=0.0, std=std)
def gradient_checkpointing_enable(self, gradient_checkpointing_kwargs=None):
self._use_activation_checkpointing = True
def gradient_checkpointing_disable(self):
self._use_activation_checkpointing = False
def forward(self, input_ids, attention_mask=None, labels=None, return_dict=True, **kwargs):
B, S = input_ids.shape
pos = torch.arange(S, device=input_ids.device).unsqueeze(0).expand(B, -1)
cmask = attention_mask.bool() if attention_mask is not None else None
h = self.embed_tokens(input_ids)
total_aux = torch.tensor(0.0, device=input_ids.device, dtype=torch.float32)
for layer in self.layers:
if self._use_activation_checkpointing and self.training:
h, aux = checkpoint(layer, h, cmask, pos, use_reentrant=False)
else:
h, aux = layer(h, cmask, pos)
total_aux = total_aux + aux
logits = self.lm_head(self.norm(h)).float()
loss = None
if labels is not None:
loss = F.cross_entropy(
logits.reshape(-1, logits.size(-1)),
labels.reshape(-1),
ignore_index=-100,
)
loss = loss + self.config.router_aux_loss_coef * (total_aux / len(self.layers))
if not return_dict:
return (loss, logits) if loss is not None else (logits,)
return CausalLMOutputWithPast(loss=loss, logits=logits)
|