Text Generation
Transformers
Safetensors
code
ivme_coder
language-model
transformer
rope
swiglu
muon
from-scratch
tiny
small
decoder-only
python
custom_code
Instructions to use IvmeLabs/Ivme-Coder-v1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use IvmeLabs/Ivme-Coder-v1 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="IvmeLabs/Ivme-Coder-v1", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("IvmeLabs/Ivme-Coder-v1", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use IvmeLabs/Ivme-Coder-v1 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "IvmeLabs/Ivme-Coder-v1" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "IvmeLabs/Ivme-Coder-v1", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/IvmeLabs/Ivme-Coder-v1
- SGLang
How to use IvmeLabs/Ivme-Coder-v1 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 "IvmeLabs/Ivme-Coder-v1" \ --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": "IvmeLabs/Ivme-Coder-v1", "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 "IvmeLabs/Ivme-Coder-v1" \ --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": "IvmeLabs/Ivme-Coder-v1", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use IvmeLabs/Ivme-Coder-v1 with Docker Model Runner:
docker model run hf.co/IvmeLabs/Ivme-Coder-v1
Upload Ivme-Coder-v1 (Otter 1): safetensors + custom modeling code
Browse files- README.md +80 -0
- ckpt_final.pt +3 -0
- config.json +20 -0
- model.safetensors +3 -0
- model/model.py +179 -0
- modeling_ivme_coder.py +179 -0
- special_tokens_map.json +5 -0
- tokenizer.json +0 -0
- tokenizer_config.json +7 -0
README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- code
|
| 5 |
+
tags:
|
| 6 |
+
- language-model
|
| 7 |
+
- transformer
|
| 8 |
+
- rope
|
| 9 |
+
- swiglu
|
| 10 |
+
- muon
|
| 11 |
+
- from-scratch
|
| 12 |
+
- tiny
|
| 13 |
+
- small
|
| 14 |
+
- decoder-only
|
| 15 |
+
- code
|
| 16 |
+
- python
|
| 17 |
+
datasets:
|
| 18 |
+
- bigcode/starcoderdata
|
| 19 |
+
pipeline_tag: text-generation
|
| 20 |
+
library_name: transformers
|
| 21 |
+
---
|
| 22 |
+
|
| 23 |
+
# Ivme-Coder-v1 (codename: Otter 1)
|
| 24 |
+
|
| 25 |
+
A ~50M parameter, from-scratch, decoder-only Python code model. First model in
|
| 26 |
+
IvmeLabs' genre-specific ("animal") line, as distinct from the general-purpose
|
| 27 |
+
("fruit") line represented by Ivme-Conversate.
|
| 28 |
+
|
| 29 |
+
## Model Details
|
| 30 |
+
|
| 31 |
+
| Parameter | Value |
|
| 32 |
+
|---|---|
|
| 33 |
+
| Architecture | Decoder-only transformer, dense |
|
| 34 |
+
| Parameters | ~50M |
|
| 35 |
+
| Layers | 12 |
|
| 36 |
+
| Hidden dim | 512 |
|
| 37 |
+
| Attention heads | 8, full attention |
|
| 38 |
+
| Context length | 1024 tokens |
|
| 39 |
+
| Vocab size | 16000 (custom BPE, trained on Python code) |
|
| 40 |
+
| Positional encoding | RoPE (θ=10000) |
|
| 41 |
+
| Normalization | RMSNorm (pre-norm) |
|
| 42 |
+
| Embeddings | Tied input/output |
|
| 43 |
+
| Optimizer | Muon (body) + AdamW (embeddings/norms) |
|
| 44 |
+
|
| 45 |
+
## Training
|
| 46 |
+
|
| 47 |
+
- **Data**: `bigcode/starcoderdata`, Python config, filtered (length/quality heuristics), ~2.5B tokens
|
| 48 |
+
- **Schedule**: Warmup-Stable-Decay (WSD), 12,715 steps
|
| 49 |
+
- **Final weights**: EMA (β=0.999) of the training trajectory
|
| 50 |
+
|
| 51 |
+
## Limitations
|
| 52 |
+
|
| 53 |
+
- Base model only, not instruction tuned - completes code, does not follow instructions
|
| 54 |
+
- Learns Python syntax and idiom well; does **not** reliably produce logically
|
| 55 |
+
correct or semantically meaningful code. Expect plausible-looking but often
|
| 56 |
+
incorrect implementations (e.g. wrong recursive logic, off-topic docstrings).
|
| 57 |
+
- 1024 token context window
|
| 58 |
+
- Python only
|
| 59 |
+
|
| 60 |
+
## Inference
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
import torch
|
| 64 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 65 |
+
|
| 66 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 67 |
+
"IvmeLabs/Ivme-Coder-v1", trust_remote_code=True, dtype=torch.bfloat16,
|
| 68 |
+
)
|
| 69 |
+
tokenizer = AutoTokenizer.from_pretrained("IvmeLabs/Ivme-Coder-v1", trust_remote_code=True)
|
| 70 |
+
model.eval()
|
| 71 |
+
|
| 72 |
+
inputs = tokenizer("def fibonacci(n):", return_tensors="pt")
|
| 73 |
+
out = model.generate(
|
| 74 |
+
**inputs, max_new_tokens=200, do_sample=True,
|
| 75 |
+
temperature=0.8, top_k=50, pad_token_id=tokenizer.pad_token_id,
|
| 76 |
+
)
|
| 77 |
+
print(tokenizer.decode(out[0], skip_special_tokens=True))
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
`trust_remote_code=True` is required (custom architecture: RoPE + SwiGLU + RMSNorm dense decoder).
|
ckpt_final.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2ea5172c14fd5efd044ecaf7172ecf358fbbf5ace4c86d71e36841628fef42f5
|
| 3 |
+
size 584958519
|
config.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_type": "ivme_coder",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"IvmeCoderV1ForCausalLM"
|
| 5 |
+
],
|
| 6 |
+
"vocab_size": 16000,
|
| 7 |
+
"d_model": 512,
|
| 8 |
+
"n_layer": 12,
|
| 9 |
+
"n_head": 8,
|
| 10 |
+
"context_len": 1024,
|
| 11 |
+
"ffn_mult": 2.6666666666666665,
|
| 12 |
+
"rope_theta": 10000.0,
|
| 13 |
+
"tie_word_embeddings": true,
|
| 14 |
+
"auto_map": {
|
| 15 |
+
"AutoConfig": "modeling_ivme_coder.IvmeCoderConfig",
|
| 16 |
+
"AutoModelForCausalLM": "modeling_ivme_coder.IvmeCoderV1ForCausalLM"
|
| 17 |
+
},
|
| 18 |
+
"torch_dtype": "bfloat16",
|
| 19 |
+
"transformers_version": "4.x"
|
| 20 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c4dd525d2983b01268b1eb7dfc2186386aae1ed83e9b4285106d8c5b159e81a3
|
| 3 |
+
size 92038912
|
model/model.py
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Ivme-Coder-v1 (codename Otter 1) modeling file.
|
| 3 |
+
RoPE + SwiGLU + RMSNorm (pre-norm) decoder-only transformer, tied embeddings.
|
| 4 |
+
Load with: AutoModelForCausalLM.from_pretrained(repo_id, trust_remote_code=True)
|
| 5 |
+
"""
|
| 6 |
+
import torch
|
| 7 |
+
import torch.nn as nn
|
| 8 |
+
import torch.nn.functional as F
|
| 9 |
+
from transformers import PretrainedConfig, PreTrainedModel
|
| 10 |
+
from transformers.modeling_outputs import CausalLMOutput
|
| 11 |
+
from transformers.generation import GenerationMixin
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class IvmeCoderConfig(PretrainedConfig):
|
| 15 |
+
model_type = "ivme_coder"
|
| 16 |
+
|
| 17 |
+
def __init__(
|
| 18 |
+
self,
|
| 19 |
+
vocab_size=16000,
|
| 20 |
+
d_model=512,
|
| 21 |
+
n_layer=12,
|
| 22 |
+
n_head=8,
|
| 23 |
+
context_len=1024,
|
| 24 |
+
ffn_mult=8/3,
|
| 25 |
+
rope_theta=10000.0,
|
| 26 |
+
tie_word_embeddings=True,
|
| 27 |
+
**kwargs,
|
| 28 |
+
):
|
| 29 |
+
self.vocab_size = vocab_size
|
| 30 |
+
self.d_model = d_model
|
| 31 |
+
self.n_layer = n_layer
|
| 32 |
+
self.n_head = n_head
|
| 33 |
+
self.context_len = context_len
|
| 34 |
+
self.ffn_mult = ffn_mult
|
| 35 |
+
self.rope_theta = rope_theta
|
| 36 |
+
# Standard HF attribute names, aliased to our own names. Recent `transformers`
|
| 37 |
+
# generation/caching code (DynamicCache etc.) reads these directly off the
|
| 38 |
+
# config regardless of architecture, even for trust_remote_code models.
|
| 39 |
+
self.num_hidden_layers = n_layer
|
| 40 |
+
self.num_attention_heads = n_head
|
| 41 |
+
self.hidden_size = d_model
|
| 42 |
+
self.use_cache = False # this architecture does not implement KV caching -
|
| 43 |
+
# forward() always recomputes the full sequence, see note below
|
| 44 |
+
super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
class RMSNorm(nn.Module):
|
| 48 |
+
def __init__(self, dim, eps=1e-6):
|
| 49 |
+
super().__init__()
|
| 50 |
+
self.weight = nn.Parameter(torch.ones(dim))
|
| 51 |
+
self.eps = eps
|
| 52 |
+
|
| 53 |
+
def forward(self, x):
|
| 54 |
+
norm = x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
|
| 55 |
+
return norm * self.weight
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def precompute_rope(dim, max_len, theta=10000.0, device="cpu"):
|
| 59 |
+
inv_freq = 1.0 / (theta ** (torch.arange(0, dim, 2, device=device).float() / dim))
|
| 60 |
+
t = torch.arange(max_len, device=device).float()
|
| 61 |
+
freqs = torch.outer(t, inv_freq)
|
| 62 |
+
return torch.cos(freqs), torch.sin(freqs)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def apply_rope(x, cos, sin):
|
| 66 |
+
T = x.size(2)
|
| 67 |
+
cos = cos[:T].unsqueeze(0).unsqueeze(0)
|
| 68 |
+
sin = sin[:T].unsqueeze(0).unsqueeze(0)
|
| 69 |
+
x1, x2 = x[..., 0::2], x[..., 1::2]
|
| 70 |
+
rot1 = x1 * cos - x2 * sin
|
| 71 |
+
rot2 = x1 * sin + x2 * cos
|
| 72 |
+
return torch.stack([rot1, rot2], dim=-1).flatten(-2)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
class CausalSelfAttention(nn.Module):
|
| 76 |
+
def __init__(self, d_model, n_head):
|
| 77 |
+
super().__init__()
|
| 78 |
+
self.n_head = n_head
|
| 79 |
+
self.d_head = d_model // n_head
|
| 80 |
+
self.qkv = nn.Linear(d_model, 3 * d_model, bias=False)
|
| 81 |
+
self.proj = nn.Linear(d_model, d_model, bias=False)
|
| 82 |
+
|
| 83 |
+
def forward(self, x, cos, sin):
|
| 84 |
+
B, T, C = x.shape
|
| 85 |
+
qkv = self.qkv(x)
|
| 86 |
+
q, k, v = qkv.split(C, dim=2)
|
| 87 |
+
q = q.view(B, T, self.n_head, self.d_head).transpose(1, 2)
|
| 88 |
+
k = k.view(B, T, self.n_head, self.d_head).transpose(1, 2)
|
| 89 |
+
v = v.view(B, T, self.n_head, self.d_head).transpose(1, 2)
|
| 90 |
+
q = apply_rope(q, cos, sin)
|
| 91 |
+
k = apply_rope(k, cos, sin)
|
| 92 |
+
out = F.scaled_dot_product_attention(q, k, v, is_causal=True)
|
| 93 |
+
out = out.transpose(1, 2).contiguous().view(B, T, C)
|
| 94 |
+
return self.proj(out)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
class SwiGLU(nn.Module):
|
| 98 |
+
def __init__(self, d_model, mult=8/3):
|
| 99 |
+
super().__init__()
|
| 100 |
+
hidden = int(d_model * mult)
|
| 101 |
+
hidden = (hidden + 7) // 8 * 8
|
| 102 |
+
self.w1 = nn.Linear(d_model, hidden, bias=False)
|
| 103 |
+
self.w2 = nn.Linear(d_model, hidden, bias=False)
|
| 104 |
+
self.w3 = nn.Linear(hidden, d_model, bias=False)
|
| 105 |
+
|
| 106 |
+
def forward(self, x):
|
| 107 |
+
return self.w3(F.silu(self.w1(x)) * self.w2(x))
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
class Block(nn.Module):
|
| 111 |
+
def __init__(self, d_model, n_head, ffn_mult):
|
| 112 |
+
super().__init__()
|
| 113 |
+
self.ln1 = RMSNorm(d_model)
|
| 114 |
+
self.attn = CausalSelfAttention(d_model, n_head)
|
| 115 |
+
self.ln2 = RMSNorm(d_model)
|
| 116 |
+
self.mlp = SwiGLU(d_model, ffn_mult)
|
| 117 |
+
|
| 118 |
+
def forward(self, x, cos, sin):
|
| 119 |
+
x = x + self.attn(self.ln1(x), cos, sin)
|
| 120 |
+
x = x + self.mlp(self.ln2(x))
|
| 121 |
+
return x
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
class IvmeCoderV1ForCausalLM(PreTrainedModel, GenerationMixin):
|
| 125 |
+
config_class = IvmeCoderConfig
|
| 126 |
+
|
| 127 |
+
def __init__(self, config):
|
| 128 |
+
super().__init__(config)
|
| 129 |
+
self.context_len = config.context_len
|
| 130 |
+
self.tok_emb = nn.Embedding(config.vocab_size, config.d_model)
|
| 131 |
+
self.blocks = nn.ModuleList(
|
| 132 |
+
[Block(config.d_model, config.n_head, config.ffn_mult) for _ in range(config.n_layer)]
|
| 133 |
+
)
|
| 134 |
+
self.ln_f = RMSNorm(config.d_model)
|
| 135 |
+
self.head = nn.Linear(config.d_model, config.vocab_size, bias=False)
|
| 136 |
+
self.head.weight = self.tok_emb.weight # tied embeddings
|
| 137 |
+
|
| 138 |
+
d_head = config.d_model // config.n_head
|
| 139 |
+
cos, sin = precompute_rope(d_head, config.context_len, theta=config.rope_theta)
|
| 140 |
+
self.register_buffer("rope_cos", cos, persistent=False)
|
| 141 |
+
self.register_buffer("rope_sin", sin, persistent=False)
|
| 142 |
+
|
| 143 |
+
self.post_init()
|
| 144 |
+
|
| 145 |
+
def _init_weights(self, module):
|
| 146 |
+
if isinstance(module, nn.Linear):
|
| 147 |
+
nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 148 |
+
elif isinstance(module, nn.Embedding):
|
| 149 |
+
nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 150 |
+
|
| 151 |
+
def get_input_embeddings(self):
|
| 152 |
+
return self.tok_emb
|
| 153 |
+
|
| 154 |
+
def set_input_embeddings(self, value):
|
| 155 |
+
self.tok_emb = value
|
| 156 |
+
|
| 157 |
+
def forward(self, input_ids, labels=None, use_cache=False, past_key_values=None,
|
| 158 |
+
attention_mask=None, **kwargs):
|
| 159 |
+
# attention_mask is accepted for API compatibility with tokenizer output /
|
| 160 |
+
# generate()'s kwarg validation, but intentionally unused: this model only
|
| 161 |
+
# supports left-padding for batched generation (see model card), and single-
|
| 162 |
+
# sequence causal generation (the common case) needs no mask at all.
|
| 163 |
+
x = self.tok_emb(input_ids)
|
| 164 |
+
for block in self.blocks:
|
| 165 |
+
x = block(x, self.rope_cos, self.rope_sin)
|
| 166 |
+
x = self.ln_f(x)
|
| 167 |
+
logits = self.head(x)
|
| 168 |
+
loss = None
|
| 169 |
+
if labels is not None:
|
| 170 |
+
loss = F.cross_entropy(logits.view(-1, logits.size(-1)), labels.view(-1))
|
| 171 |
+
return CausalLMOutput(loss=loss, logits=logits)
|
| 172 |
+
|
| 173 |
+
def prepare_inputs_for_generation(self, input_ids, **kwargs):
|
| 174 |
+
# This architecture has no KV cache implementation - every generation step
|
| 175 |
+
# recomputes attention over the full (truncated) sequence from scratch.
|
| 176 |
+
# This is correct but slower than a cached model; fine at 50M params / 1024 ctx.
|
| 177 |
+
if input_ids.size(1) > self.context_len:
|
| 178 |
+
input_ids = input_ids[:, -self.context_len:]
|
| 179 |
+
return {"input_ids": input_ids, "use_cache": False}
|
modeling_ivme_coder.py
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Ivme-Coder-v1 (codename Otter 1) modeling file.
|
| 3 |
+
RoPE + SwiGLU + RMSNorm (pre-norm) decoder-only transformer, tied embeddings.
|
| 4 |
+
Load with: AutoModelForCausalLM.from_pretrained(repo_id, trust_remote_code=True)
|
| 5 |
+
"""
|
| 6 |
+
import torch
|
| 7 |
+
import torch.nn as nn
|
| 8 |
+
import torch.nn.functional as F
|
| 9 |
+
from transformers import PretrainedConfig, PreTrainedModel
|
| 10 |
+
from transformers.modeling_outputs import CausalLMOutput
|
| 11 |
+
from transformers.generation import GenerationMixin
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class IvmeCoderConfig(PretrainedConfig):
|
| 15 |
+
model_type = "ivme_coder"
|
| 16 |
+
|
| 17 |
+
def __init__(
|
| 18 |
+
self,
|
| 19 |
+
vocab_size=16000,
|
| 20 |
+
d_model=512,
|
| 21 |
+
n_layer=12,
|
| 22 |
+
n_head=8,
|
| 23 |
+
context_len=1024,
|
| 24 |
+
ffn_mult=8/3,
|
| 25 |
+
rope_theta=10000.0,
|
| 26 |
+
tie_word_embeddings=True,
|
| 27 |
+
**kwargs,
|
| 28 |
+
):
|
| 29 |
+
self.vocab_size = vocab_size
|
| 30 |
+
self.d_model = d_model
|
| 31 |
+
self.n_layer = n_layer
|
| 32 |
+
self.n_head = n_head
|
| 33 |
+
self.context_len = context_len
|
| 34 |
+
self.ffn_mult = ffn_mult
|
| 35 |
+
self.rope_theta = rope_theta
|
| 36 |
+
# Standard HF attribute names, aliased to our own names. Recent `transformers`
|
| 37 |
+
# generation/caching code (DynamicCache etc.) reads these directly off the
|
| 38 |
+
# config regardless of architecture, even for trust_remote_code models.
|
| 39 |
+
self.num_hidden_layers = n_layer
|
| 40 |
+
self.num_attention_heads = n_head
|
| 41 |
+
self.hidden_size = d_model
|
| 42 |
+
self.use_cache = False # this architecture does not implement KV caching -
|
| 43 |
+
# forward() always recomputes the full sequence, see note below
|
| 44 |
+
super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
class RMSNorm(nn.Module):
|
| 48 |
+
def __init__(self, dim, eps=1e-6):
|
| 49 |
+
super().__init__()
|
| 50 |
+
self.weight = nn.Parameter(torch.ones(dim))
|
| 51 |
+
self.eps = eps
|
| 52 |
+
|
| 53 |
+
def forward(self, x):
|
| 54 |
+
norm = x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
|
| 55 |
+
return norm * self.weight
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def precompute_rope(dim, max_len, theta=10000.0, device="cpu"):
|
| 59 |
+
inv_freq = 1.0 / (theta ** (torch.arange(0, dim, 2, device=device).float() / dim))
|
| 60 |
+
t = torch.arange(max_len, device=device).float()
|
| 61 |
+
freqs = torch.outer(t, inv_freq)
|
| 62 |
+
return torch.cos(freqs), torch.sin(freqs)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def apply_rope(x, cos, sin):
|
| 66 |
+
T = x.size(2)
|
| 67 |
+
cos = cos[:T].unsqueeze(0).unsqueeze(0)
|
| 68 |
+
sin = sin[:T].unsqueeze(0).unsqueeze(0)
|
| 69 |
+
x1, x2 = x[..., 0::2], x[..., 1::2]
|
| 70 |
+
rot1 = x1 * cos - x2 * sin
|
| 71 |
+
rot2 = x1 * sin + x2 * cos
|
| 72 |
+
return torch.stack([rot1, rot2], dim=-1).flatten(-2)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
class CausalSelfAttention(nn.Module):
|
| 76 |
+
def __init__(self, d_model, n_head):
|
| 77 |
+
super().__init__()
|
| 78 |
+
self.n_head = n_head
|
| 79 |
+
self.d_head = d_model // n_head
|
| 80 |
+
self.qkv = nn.Linear(d_model, 3 * d_model, bias=False)
|
| 81 |
+
self.proj = nn.Linear(d_model, d_model, bias=False)
|
| 82 |
+
|
| 83 |
+
def forward(self, x, cos, sin):
|
| 84 |
+
B, T, C = x.shape
|
| 85 |
+
qkv = self.qkv(x)
|
| 86 |
+
q, k, v = qkv.split(C, dim=2)
|
| 87 |
+
q = q.view(B, T, self.n_head, self.d_head).transpose(1, 2)
|
| 88 |
+
k = k.view(B, T, self.n_head, self.d_head).transpose(1, 2)
|
| 89 |
+
v = v.view(B, T, self.n_head, self.d_head).transpose(1, 2)
|
| 90 |
+
q = apply_rope(q, cos, sin)
|
| 91 |
+
k = apply_rope(k, cos, sin)
|
| 92 |
+
out = F.scaled_dot_product_attention(q, k, v, is_causal=True)
|
| 93 |
+
out = out.transpose(1, 2).contiguous().view(B, T, C)
|
| 94 |
+
return self.proj(out)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
class SwiGLU(nn.Module):
|
| 98 |
+
def __init__(self, d_model, mult=8/3):
|
| 99 |
+
super().__init__()
|
| 100 |
+
hidden = int(d_model * mult)
|
| 101 |
+
hidden = (hidden + 7) // 8 * 8
|
| 102 |
+
self.w1 = nn.Linear(d_model, hidden, bias=False)
|
| 103 |
+
self.w2 = nn.Linear(d_model, hidden, bias=False)
|
| 104 |
+
self.w3 = nn.Linear(hidden, d_model, bias=False)
|
| 105 |
+
|
| 106 |
+
def forward(self, x):
|
| 107 |
+
return self.w3(F.silu(self.w1(x)) * self.w2(x))
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
class Block(nn.Module):
|
| 111 |
+
def __init__(self, d_model, n_head, ffn_mult):
|
| 112 |
+
super().__init__()
|
| 113 |
+
self.ln1 = RMSNorm(d_model)
|
| 114 |
+
self.attn = CausalSelfAttention(d_model, n_head)
|
| 115 |
+
self.ln2 = RMSNorm(d_model)
|
| 116 |
+
self.mlp = SwiGLU(d_model, ffn_mult)
|
| 117 |
+
|
| 118 |
+
def forward(self, x, cos, sin):
|
| 119 |
+
x = x + self.attn(self.ln1(x), cos, sin)
|
| 120 |
+
x = x + self.mlp(self.ln2(x))
|
| 121 |
+
return x
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
class IvmeCoderV1ForCausalLM(PreTrainedModel, GenerationMixin):
|
| 125 |
+
config_class = IvmeCoderConfig
|
| 126 |
+
|
| 127 |
+
def __init__(self, config):
|
| 128 |
+
super().__init__(config)
|
| 129 |
+
self.context_len = config.context_len
|
| 130 |
+
self.tok_emb = nn.Embedding(config.vocab_size, config.d_model)
|
| 131 |
+
self.blocks = nn.ModuleList(
|
| 132 |
+
[Block(config.d_model, config.n_head, config.ffn_mult) for _ in range(config.n_layer)]
|
| 133 |
+
)
|
| 134 |
+
self.ln_f = RMSNorm(config.d_model)
|
| 135 |
+
self.head = nn.Linear(config.d_model, config.vocab_size, bias=False)
|
| 136 |
+
self.head.weight = self.tok_emb.weight # tied embeddings
|
| 137 |
+
|
| 138 |
+
d_head = config.d_model // config.n_head
|
| 139 |
+
cos, sin = precompute_rope(d_head, config.context_len, theta=config.rope_theta)
|
| 140 |
+
self.register_buffer("rope_cos", cos, persistent=False)
|
| 141 |
+
self.register_buffer("rope_sin", sin, persistent=False)
|
| 142 |
+
|
| 143 |
+
self.post_init()
|
| 144 |
+
|
| 145 |
+
def _init_weights(self, module):
|
| 146 |
+
if isinstance(module, nn.Linear):
|
| 147 |
+
nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 148 |
+
elif isinstance(module, nn.Embedding):
|
| 149 |
+
nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 150 |
+
|
| 151 |
+
def get_input_embeddings(self):
|
| 152 |
+
return self.tok_emb
|
| 153 |
+
|
| 154 |
+
def set_input_embeddings(self, value):
|
| 155 |
+
self.tok_emb = value
|
| 156 |
+
|
| 157 |
+
def forward(self, input_ids, labels=None, use_cache=False, past_key_values=None,
|
| 158 |
+
attention_mask=None, **kwargs):
|
| 159 |
+
# attention_mask is accepted for API compatibility with tokenizer output /
|
| 160 |
+
# generate()'s kwarg validation, but intentionally unused: this model only
|
| 161 |
+
# supports left-padding for batched generation (see model card), and single-
|
| 162 |
+
# sequence causal generation (the common case) needs no mask at all.
|
| 163 |
+
x = self.tok_emb(input_ids)
|
| 164 |
+
for block in self.blocks:
|
| 165 |
+
x = block(x, self.rope_cos, self.rope_sin)
|
| 166 |
+
x = self.ln_f(x)
|
| 167 |
+
logits = self.head(x)
|
| 168 |
+
loss = None
|
| 169 |
+
if labels is not None:
|
| 170 |
+
loss = F.cross_entropy(logits.view(-1, logits.size(-1)), labels.view(-1))
|
| 171 |
+
return CausalLMOutput(loss=loss, logits=logits)
|
| 172 |
+
|
| 173 |
+
def prepare_inputs_for_generation(self, input_ids, **kwargs):
|
| 174 |
+
# This architecture has no KV cache implementation - every generation step
|
| 175 |
+
# recomputes attention over the full (truncated) sequence from scratch.
|
| 176 |
+
# This is correct but slower than a cached model; fine at 50M params / 1024 ctx.
|
| 177 |
+
if input_ids.size(1) > self.context_len:
|
| 178 |
+
input_ids = input_ids[:, -self.context_len:]
|
| 179 |
+
return {"input_ids": input_ids, "use_cache": False}
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": "<|endoftext|>",
|
| 3 |
+
"eos_token": "<|endoftext|>",
|
| 4 |
+
"pad_token": "<|pad|>"
|
| 5 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"tokenizer_class": "PreTrainedTokenizerFast",
|
| 3 |
+
"model_max_length": 1024,
|
| 4 |
+
"bos_token": "<|endoftext|>",
|
| 5 |
+
"eos_token": "<|endoftext|>",
|
| 6 |
+
"pad_token": "<|pad|>"
|
| 7 |
+
}
|