Instructions to use Ilides/cortex-0.3-0.02b-mlx with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use Ilides/cortex-0.3-0.02b-mlx with MLX:
# Make sure mlx-lm is installed # pip install --upgrade mlx-lm # Generate text with mlx-lm from mlx_lm import load, generate model, tokenizer = load("Ilides/cortex-0.3-0.02b-mlx") prompt = "Write a story about Einstein" messages = [{"role": "user", "content": prompt}] prompt = tokenizer.apply_chat_template( messages, add_generation_prompt=True ) text = generate(model, tokenizer, prompt=prompt, verbose=True) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- MLX LM
How to use Ilides/cortex-0.3-0.02b-mlx with MLX LM:
Generate or start a chat session
# Install MLX LM uv tool install mlx-lm # Interactive chat REPL mlx_lm.chat --model "Ilides/cortex-0.3-0.02b-mlx"
Run an OpenAI-compatible server
# Install MLX LM uv tool install mlx-lm # Start the server mlx_lm.server --model "Ilides/cortex-0.3-0.02b-mlx" # Calling the OpenAI-compatible server with curl curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Ilides/cortex-0.3-0.02b-mlx", "messages": [ {"role": "user", "content": "Hello"} ] }' - Atomic Chat
Download model.py from Ilides/cortex-0.3-0.02b-mlx: direct link, hf CLI and curl.
- Browser
- Download file 3.51 kB
-
https://huggingface.co/Ilides/cortex-0.3-0.02b-mlx/resolve/main/model.py
- Command line
-
hf download hf://Ilides/cortex-0.3-0.02b-mlx/model.py
-
curl -L -o model.py https://huggingface.co/Ilides/cortex-0.3-0.02b-mlx/resolve/main/model.py
3.51 kB
| """Cortex-1 en MLX (Apple Silicon) — equivalente a la implementación NumPy/PyTorch. | |
| Misma arquitectura: GPT pre-norm, RMSNorm, atención causal con QKV fusionado, | |
| SwiGLU, embeddings de posición aprendidos, lm_head atado al embedding. | |
| Paridad verificada: max|Δ logits| < 1e-4 vs el modelo NumPy original. | |
| Uso: python generate_mlx.py "La capital de Francia es" | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import os | |
| import mlx.core as mx | |
| import mlx.nn as nn | |
| MODEL_FILE = "weights.safetensors" | |
| class Attention(nn.Module): | |
| def __init__(self, d: int, n_heads: int): | |
| super().__init__() | |
| self.n_heads = n_heads | |
| self.d_head = d // n_heads | |
| self.scale = self.d_head ** -0.5 | |
| self.qkv = nn.Linear(d, 3 * d, bias=False) | |
| self.proj = nn.Linear(d, d, bias=False) | |
| def __call__(self, x: mx.array) -> mx.array: | |
| B, T, d = x.shape | |
| qkv = self.qkv(x).reshape(B, T, 3, self.n_heads, self.d_head) | |
| q, k, v = (qkv[:, :, i].transpose(0, 2, 1, 3) for i in range(3)) | |
| att = (q @ k.transpose(0, 1, 3, 2)) * self.scale # (B,H,T,T) | |
| mask = mx.triu(mx.full((T, T), -mx.inf, dtype=att.dtype), k=1) | |
| att = mx.softmax(att + mask, axis=-1) | |
| y = (att @ v).transpose(0, 2, 1, 3).reshape(B, T, d) | |
| return self.proj(y) | |
| class MLP(nn.Module): | |
| def __init__(self, d: int, d_ff: int): | |
| super().__init__() | |
| self.gate_proj = nn.Linear(d, d_ff, bias=False) | |
| self.up_proj = nn.Linear(d, d_ff, bias=False) | |
| self.down_proj = nn.Linear(d_ff, d, bias=False) | |
| def __call__(self, x: mx.array) -> mx.array: | |
| return self.down_proj(nn.silu(self.gate_proj(x)) * self.up_proj(x)) | |
| class Block(nn.Module): | |
| def __init__(self, d: int, n_heads: int, d_ff: int, eps: float): | |
| super().__init__() | |
| self.input_layernorm = nn.RMSNorm(d, eps=eps) | |
| self.self_attn = Attention(d, n_heads) | |
| self.post_attention_layernorm = nn.RMSNorm(d, eps=eps) | |
| self.mlp = MLP(d, d_ff) | |
| def __call__(self, x): | |
| x = x + self.self_attn(self.input_layernorm(x)) | |
| return x + self.mlp(self.post_attention_layernorm(x)) | |
| class Model(nn.Module): | |
| def __init__(self, cfg: dict): | |
| super().__init__() | |
| d = cfg["hidden_size"] | |
| self.cfg = cfg | |
| self.embed_tokens = nn.Embedding(cfg["vocab_size"], d) | |
| self.embed_positions = nn.Embedding(cfg["max_position_embeddings"], d) | |
| self.layers = [Block(d, cfg["num_attention_heads"], | |
| cfg["intermediate_size"], cfg["rms_norm_eps"]) | |
| for _ in range(cfg["num_hidden_layers"])] | |
| self.norm = nn.RMSNorm(d, eps=cfg["rms_norm_eps"]) | |
| def __call__(self, input_ids: mx.array) -> mx.array: | |
| T = input_ids.shape[1] | |
| h = self.embed_tokens(input_ids) + self.embed_positions(mx.arange(T)) | |
| for layer in self.layers: | |
| h = layer(h) | |
| return self.norm(h) | |
| def logits(self, input_ids: mx.array) -> mx.array: | |
| """(B,T) int -> (B,T,V); lm_head atado = embed_tokens.""" | |
| h = self(input_ids) | |
| w = self.embed_tokens.weight # (V, d) | |
| return h @ w.T | |
| def load(cls, repo: str) -> "Model": | |
| with open(os.path.join(repo, "config.json")) as f: | |
| cfg = json.load(f) | |
| model = cls(cfg) | |
| weights = mx.load(os.path.join(repo, MODEL_FILE)) | |
| model.load_weights(list(weights.items()), strict=True) | |
| return model | |