Text Generation
Transformers
Safetensors
English
bananaall
novi
novi-micro
causal-lm
from-scratch
custom_code
Instructions to use Novi-AI/Novi-Micro-Base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Novi-AI/Novi-Micro-Base with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Novi-AI/Novi-Micro-Base", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("Novi-AI/Novi-Micro-Base", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Novi-AI/Novi-Micro-Base with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Novi-AI/Novi-Micro-Base" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Novi-AI/Novi-Micro-Base", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Novi-AI/Novi-Micro-Base
- SGLang
How to use Novi-AI/Novi-Micro-Base 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 "Novi-AI/Novi-Micro-Base" \ --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": "Novi-AI/Novi-Micro-Base", "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 "Novi-AI/Novi-Micro-Base" \ --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": "Novi-AI/Novi-Micro-Base", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Novi-AI/Novi-Micro-Base with Docker Model Runner:
docker model run hf.co/Novi-AI/Novi-Micro-Base
Upload 9 files
Browse files- config.json +30 -0
- configuration_novimicro.py +27 -0
- dataset_tokens.json +6 -0
- generation_config.json +12 -0
- model.safetensors +3 -0
- modeling_novimicro.py +172 -0
- tokenizer.json +0 -0
- tokenizer_config.json +9 -0
- training_args.bin +3 -0
config.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architecture_style": "bananamind2",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"BananaAllForCausalLM"
|
| 5 |
+
],
|
| 6 |
+
"auto_map": {
|
| 7 |
+
"AutoConfig": "configuration_bananaall.BananaAllConfig",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_bananaall.BananaAllForCausalLM"
|
| 9 |
+
},
|
| 10 |
+
"bos_token_id": 1,
|
| 11 |
+
"dtype": "float32",
|
| 12 |
+
"eos_token_id": 2,
|
| 13 |
+
"head_dim": 40,
|
| 14 |
+
"hidden_size": 160,
|
| 15 |
+
"intermediate_size": 864,
|
| 16 |
+
"lft": false,
|
| 17 |
+
"max_position_embeddings": 4096,
|
| 18 |
+
"model_type": "bananaall",
|
| 19 |
+
"num_attention_heads": 4,
|
| 20 |
+
"num_hidden_layers": 9,
|
| 21 |
+
"num_key_value_heads": 2,
|
| 22 |
+
"pad_token_id": 0,
|
| 23 |
+
"rms_norm_eps": 1e-06,
|
| 24 |
+
"rope_theta": 100000.0,
|
| 25 |
+
"ternary": false,
|
| 26 |
+
"tie_word_embeddings": true,
|
| 27 |
+
"transformers_version": "5.14.1",
|
| 28 |
+
"use_cache": false,
|
| 29 |
+
"vocab_size": 3840
|
| 30 |
+
}
|
configuration_novimicro.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class BananaAllConfig(PretrainedConfig):
|
| 5 |
+
model_type = "bananaall"
|
| 6 |
+
|
| 7 |
+
def __init__(self, vocab_size=8192, hidden_size=384, num_hidden_layers=14,
|
| 8 |
+
num_attention_heads=6, num_key_value_heads=2, head_dim=64,
|
| 9 |
+
intermediate_size=1024, max_position_embeddings=4096,
|
| 10 |
+
rope_theta=100000.0, rms_norm_eps=1e-6, architecture_style="bananamind2",
|
| 11 |
+
lft=False, ternary=False, **kwargs):
|
| 12 |
+
kwargs.setdefault("tie_word_embeddings", True)
|
| 13 |
+
super().__init__(**kwargs)
|
| 14 |
+
self.vocab_size = vocab_size
|
| 15 |
+
self.hidden_size = hidden_size
|
| 16 |
+
self.num_hidden_layers = num_hidden_layers
|
| 17 |
+
self.num_attention_heads = num_attention_heads
|
| 18 |
+
self.num_key_value_heads = num_key_value_heads
|
| 19 |
+
self.head_dim = head_dim
|
| 20 |
+
self.intermediate_size = intermediate_size
|
| 21 |
+
self.max_position_embeddings = max_position_embeddings
|
| 22 |
+
self.rope_theta = rope_theta
|
| 23 |
+
self.rms_norm_eps = rms_norm_eps
|
| 24 |
+
self.architecture_style = architecture_style
|
| 25 |
+
self.lft = lft
|
| 26 |
+
self.ternary = ternary
|
| 27 |
+
self.use_cache = False
|
dataset_tokens.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"dataset": "epfml/FineWeb-HQ",
|
| 4 |
+
"tokens": 1000000000
|
| 5 |
+
}
|
| 6 |
+
]
|
generation_config.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 1,
|
| 4 |
+
"eos_token_id": [
|
| 5 |
+
2
|
| 6 |
+
],
|
| 7 |
+
"output_attentions": false,
|
| 8 |
+
"output_hidden_states": false,
|
| 9 |
+
"pad_token_id": 0,
|
| 10 |
+
"transformers_version": "5.14.1",
|
| 11 |
+
"use_cache": false
|
| 12 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2d49880f3a4fd633a297a770f2d3a5e542d290f98167e744a740cb787eb01765
|
| 3 |
+
size 22634608
|
modeling_novimicro.py
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""BananaMind 2 style decoder with optional LFT or ternary fake quantization.
|
| 2 |
+
|
| 3 |
+
The model uses pre-RMSNorm, RoPE, grouped-query attention, SwiGLU and tied
|
| 4 |
+
embeddings. BananaMind 2 mode also applies QK norm. LFT changes only routing.
|
| 5 |
+
Ternary mode uses floating-point master weights and fake-quantized projections.
|
| 6 |
+
"""
|
| 7 |
+
import math
|
| 8 |
+
import torch
|
| 9 |
+
from torch import nn
|
| 10 |
+
from torch.nn import functional as F
|
| 11 |
+
from transformers import PreTrainedModel
|
| 12 |
+
from transformers.generation import GenerationMixin
|
| 13 |
+
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
from .configuration_bananaall import BananaAllConfig
|
| 17 |
+
except ImportError:
|
| 18 |
+
from configuration_bananaall import BananaAllConfig
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class RMSNorm(nn.Module):
|
| 22 |
+
def __init__(self, size, eps=1e-6):
|
| 23 |
+
super().__init__()
|
| 24 |
+
self.weight = nn.Parameter(torch.ones(size))
|
| 25 |
+
self.eps = eps
|
| 26 |
+
|
| 27 |
+
def forward(self, x):
|
| 28 |
+
y = x.float()
|
| 29 |
+
return (y * torch.rsqrt(y.square().mean(-1, keepdim=True) + self.eps) * self.weight.float()).to(x.dtype)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class TernaryLinear(nn.Linear):
|
| 33 |
+
"""W1.58A8 fake quantization with straight-through gradients.
|
| 34 |
+
|
| 35 |
+
The master weights remain floating point for optimization and checkpoints.
|
| 36 |
+
This layer does not pack ternary weights or use a low-bit inference kernel.
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
def forward(self, x):
|
| 40 |
+
weights = self.weight.float()
|
| 41 |
+
weight_scale = weights.detach().abs().mean().clamp_min(1e-6)
|
| 42 |
+
quantized_weights = (weights / weight_scale).round().clamp(-1, 1) * weight_scale
|
| 43 |
+
fake_weights = self.weight + (quantized_weights.to(self.weight.dtype) - self.weight).detach()
|
| 44 |
+
|
| 45 |
+
activations = x.float()
|
| 46 |
+
activation_scale = activations.detach().abs().amax(dim=-1, keepdim=True).clamp_min(1e-6) / 127
|
| 47 |
+
quantized_activations = (activations / activation_scale).round().clamp(-127, 127) * activation_scale
|
| 48 |
+
fake_activations = x + (quantized_activations.to(x.dtype) - x).detach()
|
| 49 |
+
return F.linear(fake_activations, fake_weights, self.bias)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def apply_rope(x, theta, position_ids):
|
| 53 |
+
dim = x.shape[-1]
|
| 54 |
+
inv = 1.0 / (theta ** (torch.arange(0, dim, 2, device=x.device, dtype=torch.float32) / dim))
|
| 55 |
+
angles = position_ids.float().unsqueeze(-1) * inv
|
| 56 |
+
cos = angles.cos().unsqueeze(1).to(x.dtype)
|
| 57 |
+
sin = angles.sin().unsqueeze(1).to(x.dtype)
|
| 58 |
+
even, odd = x[..., ::2], x[..., 1::2]
|
| 59 |
+
return torch.stack((even * cos - odd * sin, even * sin + odd * cos), dim=-1).flatten(-2)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class Attention(nn.Module):
|
| 63 |
+
def __init__(self, config):
|
| 64 |
+
super().__init__()
|
| 65 |
+
h, d, kv = config.num_attention_heads, config.head_dim, config.num_key_value_heads
|
| 66 |
+
self.h, self.d, self.kv, self.theta = h, d, kv, config.rope_theta
|
| 67 |
+
linear = TernaryLinear if config.ternary else nn.Linear
|
| 68 |
+
self.q_proj = linear(config.hidden_size, h * d, bias=False)
|
| 69 |
+
self.k_proj = linear(config.hidden_size, kv * d, bias=False)
|
| 70 |
+
self.v_proj = linear(config.hidden_size, kv * d, bias=False)
|
| 71 |
+
self.o_proj = linear(h * d, config.hidden_size, bias=False)
|
| 72 |
+
self.q_norm = RMSNorm(d, config.rms_norm_eps) if config.architecture_style == "bananamind2" else nn.Identity()
|
| 73 |
+
self.k_norm = RMSNorm(d, config.rms_norm_eps) if config.architecture_style == "bananamind2" else nn.Identity()
|
| 74 |
+
|
| 75 |
+
def forward(self, x, attention_mask=None):
|
| 76 |
+
b, t, _ = x.shape
|
| 77 |
+
q = self.q_norm(self.q_proj(x).view(b, t, self.h, self.d).transpose(1, 2))
|
| 78 |
+
k = self.k_norm(self.k_proj(x).view(b, t, self.kv, self.d).transpose(1, 2))
|
| 79 |
+
v = self.v_proj(x).view(b, t, self.kv, self.d).transpose(1, 2)
|
| 80 |
+
positions = torch.arange(t, device=x.device).unsqueeze(0)
|
| 81 |
+
q, k = apply_rope(q, self.theta, positions), apply_rope(k, self.theta, positions)
|
| 82 |
+
k = k.repeat_interleave(self.h // self.kv, dim=1)
|
| 83 |
+
v = v.repeat_interleave(self.h // self.kv, dim=1)
|
| 84 |
+
if attention_mask is None:
|
| 85 |
+
y = F.scaled_dot_product_attention(q, k, v, is_causal=True)
|
| 86 |
+
else:
|
| 87 |
+
causal = torch.ones(t, t, device=x.device, dtype=torch.bool).tril()
|
| 88 |
+
mask = causal[None, None] & attention_mask[:, None, None, :].bool()
|
| 89 |
+
y = F.scaled_dot_product_attention(q, k, v, attn_mask=mask)
|
| 90 |
+
return self.o_proj(y.transpose(1, 2).contiguous().view(b, t, self.h * self.d))
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
class Block(nn.Module):
|
| 94 |
+
def __init__(self, config):
|
| 95 |
+
super().__init__()
|
| 96 |
+
self.norm1 = RMSNorm(config.hidden_size, config.rms_norm_eps)
|
| 97 |
+
self.attn = Attention(config)
|
| 98 |
+
self.norm2 = RMSNorm(config.hidden_size, config.rms_norm_eps)
|
| 99 |
+
linear = TernaryLinear if config.ternary else nn.Linear
|
| 100 |
+
self.gate_proj = linear(config.hidden_size, config.intermediate_size, bias=False)
|
| 101 |
+
self.up_proj = linear(config.hidden_size, config.intermediate_size, bias=False)
|
| 102 |
+
self.down_proj = linear(config.intermediate_size, config.hidden_size, bias=False)
|
| 103 |
+
|
| 104 |
+
def forward(self, x, attention_mask=None):
|
| 105 |
+
x = x + self.attn(self.norm1(x), attention_mask)
|
| 106 |
+
z = self.norm2(x)
|
| 107 |
+
return x + self.down_proj(F.silu(self.gate_proj(z)) * self.up_proj(z))
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
class BananaAllForCausalLM(PreTrainedModel, GenerationMixin):
|
| 111 |
+
config_class = BananaAllConfig
|
| 112 |
+
base_model_prefix = "model"
|
| 113 |
+
_supports_sdpa = True
|
| 114 |
+
# forward() returns a mean loss per microbatch and ignores **kwargs.
|
| 115 |
+
# Tell Trainer to divide it by the gradient-accumulation count.
|
| 116 |
+
accepts_loss_kwargs = False
|
| 117 |
+
|
| 118 |
+
def __init__(self, config):
|
| 119 |
+
super().__init__(config)
|
| 120 |
+
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size)
|
| 121 |
+
self.layers = nn.ModuleList([Block(config) for _ in range(config.num_hidden_layers)])
|
| 122 |
+
self.norm = RMSNorm(config.hidden_size, config.rms_norm_eps)
|
| 123 |
+
self.lm_head = (TernaryLinear if config.ternary else nn.Linear)(config.hidden_size, config.vocab_size, bias=False)
|
| 124 |
+
self.post_init()
|
| 125 |
+
self.tie_weights()
|
| 126 |
+
|
| 127 |
+
def get_input_embeddings(self):
|
| 128 |
+
return self.embed_tokens
|
| 129 |
+
|
| 130 |
+
def set_input_embeddings(self, value):
|
| 131 |
+
self.embed_tokens = value
|
| 132 |
+
|
| 133 |
+
def get_output_embeddings(self):
|
| 134 |
+
return self.lm_head
|
| 135 |
+
|
| 136 |
+
def set_output_embeddings(self, value):
|
| 137 |
+
self.lm_head = value
|
| 138 |
+
|
| 139 |
+
def forward(self, input_ids=None, attention_mask=None, labels=None, **kwargs):
|
| 140 |
+
x = self.embed_tokens(input_ids)
|
| 141 |
+
if self.config.lft and len(self.layers) > 1:
|
| 142 |
+
x = self.layers[0](x, attention_mask)
|
| 143 |
+
for i in range(1, len(self.layers)):
|
| 144 |
+
x = self.layers[i](x, attention_mask)
|
| 145 |
+
if i < len(self.layers) - 1:
|
| 146 |
+
x = self.layers[i - 1](x, attention_mask)
|
| 147 |
+
x = self.layers[i](x, attention_mask)
|
| 148 |
+
else:
|
| 149 |
+
for layer in self.layers:
|
| 150 |
+
x = layer(x, attention_mask)
|
| 151 |
+
logits = self.lm_head(self.norm(x))
|
| 152 |
+
loss = None
|
| 153 |
+
if labels is not None:
|
| 154 |
+
shifted_logits = logits[:, :-1, :].contiguous().float()
|
| 155 |
+
shifted_labels = labels[:, 1:].contiguous()
|
| 156 |
+
loss = F.cross_entropy(shifted_logits.view(-1, shifted_logits.size(-1)), shifted_labels.view(-1), ignore_index=-100)
|
| 157 |
+
return CausalLMOutputWithPast(loss=loss, logits=logits, past_key_values=None)
|
| 158 |
+
|
| 159 |
+
def prepare_inputs_for_generation(self, input_ids, attention_mask=None, **kwargs):
|
| 160 |
+
return {"input_ids": input_ids, "attention_mask": attention_mask}
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def register():
|
| 164 |
+
from transformers import AutoConfig, AutoModelForCausalLM
|
| 165 |
+
try:
|
| 166 |
+
AutoConfig.register("bananaall", BananaAllConfig)
|
| 167 |
+
except ValueError:
|
| 168 |
+
pass
|
| 169 |
+
try:
|
| 170 |
+
AutoModelForCausalLM.register(BananaAllConfig, BananaAllForCausalLM)
|
| 171 |
+
except ValueError:
|
| 172 |
+
pass
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"backend": "tokenizers",
|
| 3 |
+
"bos_token": "[BOS]",
|
| 4 |
+
"eos_token": "[EOS]",
|
| 5 |
+
"model_max_length": 2048,
|
| 6 |
+
"pad_token": "[PAD]",
|
| 7 |
+
"tokenizer_class": "TokenizersBackend",
|
| 8 |
+
"unk_token": "[UNK]"
|
| 9 |
+
}
|
training_args.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b2b83efc3b4aacda4749a21a5d8119c1f2900aff097df50646a039e98da988de
|
| 3 |
+
size 5201
|