Text Generation
Transformers
Safetensors
English
bananamind2_nano
causal-lm
base-model
bananamind2-nano
adamw
fineweb-edu
optimizer-comparison
custom-code
trust-remote-code
custom_code
Instructions to use Banaxi-Tech/adamw-model-test with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Banaxi-Tech/adamw-model-test with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Banaxi-Tech/adamw-model-test", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("Banaxi-Tech/adamw-model-test", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Banaxi-Tech/adamw-model-test with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Banaxi-Tech/adamw-model-test" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Banaxi-Tech/adamw-model-test", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Banaxi-Tech/adamw-model-test
- SGLang
How to use Banaxi-Tech/adamw-model-test 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 "Banaxi-Tech/adamw-model-test" \ --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": "Banaxi-Tech/adamw-model-test", "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 "Banaxi-Tech/adamw-model-test" \ --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": "Banaxi-Tech/adamw-model-test", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Banaxi-Tech/adamw-model-test with Docker Model Runner:
docker model run hf.co/Banaxi-Tech/adamw-model-test
Upload AdamW FineWeb-Edu checkpoint at 24,999,591,936 tokens
Browse files- README.md +85 -0
- config.json +29 -0
- configuration_bananamind2nano.py +48 -0
- generation_config.json +7 -0
- model.safetensors +3 -0
- modeling_bananamind2nano.py +307 -0
- special_tokens_map.json +6 -0
- tokenizer.json +0 -0
- tokenizer_config.json +8 -0
- training_metadata.json +34 -0
- training_metrics.jsonl +0 -0
README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
library_name: transformers
|
| 6 |
+
pipeline_tag: text-generation
|
| 7 |
+
datasets:
|
| 8 |
+
- HuggingFaceFW/fineweb-edu
|
| 9 |
+
tags:
|
| 10 |
+
- causal-lm
|
| 11 |
+
- base-model
|
| 12 |
+
- bananamind2-nano
|
| 13 |
+
- adamw
|
| 14 |
+
- fineweb-edu
|
| 15 |
+
- optimizer-comparison
|
| 16 |
+
- custom-code
|
| 17 |
+
- trust-remote-code
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
# BananaMind 2 Nano AdamW FineWeb-Edu Test
|
| 21 |
+
|
| 22 |
+
This experimental base model uses the exact **BananaMind 2 Nano** architecture
|
| 23 |
+
and tokenizer. It was trained from scratch with AdamW on only streamed
|
| 24 |
+
FineWeb-Edu data for **24,999,591,936 custom-tokenizer tokens**.
|
| 25 |
+
|
| 26 |
+
## Architecture
|
| 27 |
+
|
| 28 |
+
| Field | Value |
|
| 29 |
+
|---|---:|
|
| 30 |
+
| Parameters | 9,968,128 |
|
| 31 |
+
| Layers | 10 |
|
| 32 |
+
| Hidden size | 256 |
|
| 33 |
+
| Intermediate size | 768 |
|
| 34 |
+
| Query heads | 4 |
|
| 35 |
+
| KV heads | 2 |
|
| 36 |
+
| Head dimension | 64 |
|
| 37 |
+
| Context | 4,096 |
|
| 38 |
+
| Vocabulary | 8,192 |
|
| 39 |
+
| Embeddings | Tied |
|
| 40 |
+
| Attention | GQA, pre-RoPE QK norm |
|
| 41 |
+
| MLP | SwiGLU |
|
| 42 |
+
| Position encoding | RoPE, theta 100,000 |
|
| 43 |
+
|
| 44 |
+
## Training
|
| 45 |
+
|
| 46 |
+
| Field | Value |
|
| 47 |
+
|---|---:|
|
| 48 |
+
| Dataset | `HuggingFaceFW/fineweb-edu` / `sample-100BT` |
|
| 49 |
+
| Dataset revision | `87f09149ef4734204d70ed1d046ddc9ca3f2b8f9` |
|
| 50 |
+
| Data access | Streaming |
|
| 51 |
+
| Optimizer | AdamW |
|
| 52 |
+
| Peak learning rate | 0.003 |
|
| 53 |
+
| Betas | (0.9, 0.95) |
|
| 54 |
+
| Global batch | 132 sequences |
|
| 55 |
+
| Tokens per optimizer step | 540,672 |
|
| 56 |
+
| Optimizer steps | 46,238 |
|
| 57 |
+
| Warmup | 1,750 steps |
|
| 58 |
+
| Schedule | Warmup-stable-decay, final 15% cosine cooldown |
|
| 59 |
+
| Weight decay | 0.1, then 0.01 after 12,000,000,000 tokens |
|
| 60 |
+
| Precision | bfloat16 autocast, float32 master weights |
|
| 61 |
+
| Hardware | 8 x NVIDIA RTX PRO 6000 Blackwell Server Edition |
|
| 62 |
+
| Seed | 1337 |
|
| 63 |
+
|
| 64 |
+
The original Nano effective batch was 12 micro-batches x 11 accumulation
|
| 65 |
+
steps = 132 sequences. This distributed run preserves that exact global batch.
|
| 66 |
+
Ranks receive 16 or 17 sequences and scale their local mean losses so DDP's
|
| 67 |
+
averaged gradient is the true 132-sequence global mean.
|
| 68 |
+
|
| 69 |
+
## Usage
|
| 70 |
+
|
| 71 |
+
```python
|
| 72 |
+
import torch
|
| 73 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 74 |
+
|
| 75 |
+
model_id = "Banaxi-Tech/adamw-model-test"
|
| 76 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 77 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 78 |
+
model_id,
|
| 79 |
+
trust_remote_code=True,
|
| 80 |
+
dtype=torch.bfloat16,
|
| 81 |
+
device_map="auto",
|
| 82 |
+
)
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
This is a base model, not an instruction-tuned chat model.
|
config.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"vocab_size": 8192,
|
| 3 |
+
"hidden_size": 256,
|
| 4 |
+
"num_hidden_layers": 10,
|
| 5 |
+
"num_attention_heads": 4,
|
| 6 |
+
"num_key_value_heads": 2,
|
| 7 |
+
"head_dim": 64,
|
| 8 |
+
"intermediate_size": 768,
|
| 9 |
+
"max_position_embeddings": 4096,
|
| 10 |
+
"rope_theta": 100000.0,
|
| 11 |
+
"rms_norm_eps": 1e-06,
|
| 12 |
+
"tie_word_embeddings": true,
|
| 13 |
+
"model_type": "bananamind2_nano",
|
| 14 |
+
"architectures": [
|
| 15 |
+
"BananaMind2NanoForCausalLM"
|
| 16 |
+
],
|
| 17 |
+
"auto_map": {
|
| 18 |
+
"AutoConfig": "configuration_bananamind2nano.BananaMind2NanoConfig",
|
| 19 |
+
"AutoModelForCausalLM": "modeling_bananamind2nano.BananaMind2NanoForCausalLM"
|
| 20 |
+
},
|
| 21 |
+
"torch_dtype": "float32",
|
| 22 |
+
"bos_token_id": 1,
|
| 23 |
+
"eos_token_id": 2,
|
| 24 |
+
"pad_token_id": 0,
|
| 25 |
+
"unk_token_id": 3,
|
| 26 |
+
"use_cache": true,
|
| 27 |
+
"z_loss_coeff": 0.0,
|
| 28 |
+
"_name_or_path": "Banaxi-Tech/adamw-model-test"
|
| 29 |
+
}
|
configuration_bananamind2nano.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""BananaMind 2 Nano configuration."""
|
| 2 |
+
from transformers import PretrainedConfig
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class BananaMind2NanoConfig(PretrainedConfig):
|
| 6 |
+
model_type = "bananamind2_nano"
|
| 7 |
+
|
| 8 |
+
def __init__(
|
| 9 |
+
self,
|
| 10 |
+
vocab_size=8192,
|
| 11 |
+
hidden_size=256,
|
| 12 |
+
num_hidden_layers=10,
|
| 13 |
+
num_attention_heads=4,
|
| 14 |
+
num_key_value_heads=2,
|
| 15 |
+
head_dim=64,
|
| 16 |
+
intermediate_size=768,
|
| 17 |
+
max_position_embeddings=4096,
|
| 18 |
+
rope_theta=100000.0,
|
| 19 |
+
rms_norm_eps=1e-6,
|
| 20 |
+
tie_word_embeddings=True,
|
| 21 |
+
use_cache=True,
|
| 22 |
+
z_loss_coeff=0.0,
|
| 23 |
+
bos_token_id=1,
|
| 24 |
+
eos_token_id=2,
|
| 25 |
+
pad_token_id=0,
|
| 26 |
+
unk_token_id=3,
|
| 27 |
+
**kwargs,
|
| 28 |
+
):
|
| 29 |
+
self.vocab_size = vocab_size
|
| 30 |
+
self.hidden_size = hidden_size
|
| 31 |
+
self.num_hidden_layers = num_hidden_layers
|
| 32 |
+
self.num_attention_heads = num_attention_heads
|
| 33 |
+
self.num_key_value_heads = num_key_value_heads
|
| 34 |
+
self.head_dim = head_dim
|
| 35 |
+
self.intermediate_size = intermediate_size
|
| 36 |
+
self.max_position_embeddings = max_position_embeddings
|
| 37 |
+
self.rope_theta = rope_theta
|
| 38 |
+
self.rms_norm_eps = rms_norm_eps
|
| 39 |
+
self.use_cache = use_cache
|
| 40 |
+
self.z_loss_coeff = z_loss_coeff
|
| 41 |
+
super().__init__(
|
| 42 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 43 |
+
bos_token_id=bos_token_id,
|
| 44 |
+
eos_token_id=eos_token_id,
|
| 45 |
+
pad_token_id=pad_token_id,
|
| 46 |
+
unk_token_id=unk_token_id,
|
| 47 |
+
**kwargs,
|
| 48 |
+
)
|
generation_config.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token_id": 1,
|
| 3 |
+
"eos_token_id": 2,
|
| 4 |
+
"pad_token_id": 0,
|
| 5 |
+
"use_cache": true,
|
| 6 |
+
"transformers_version": "5.7.0"
|
| 7 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1bafb54fd1d65595edcc46d2d508d85dc36e15cf87a80d6717e4db749f81e33e
|
| 3 |
+
size 48272696
|
modeling_bananamind2nano.py
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""BananaMind 2 Nano implementation for Hugging Face Transformers."""
|
| 2 |
+
import math
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
+
from transformers import PreTrainedModel
|
| 9 |
+
from transformers.cache_utils import Cache, DynamicCache
|
| 10 |
+
from transformers.generation.utils import GenerationMixin
|
| 11 |
+
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 12 |
+
|
| 13 |
+
from .configuration_bananamind2nano import BananaMind2NanoConfig
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class RMSNorm(nn.Module):
|
| 17 |
+
def __init__(self, dim, eps=1e-6):
|
| 18 |
+
super().__init__()
|
| 19 |
+
self.eps = eps
|
| 20 |
+
self.weight = nn.Parameter(torch.ones(dim))
|
| 21 |
+
|
| 22 |
+
def forward(self, x):
|
| 23 |
+
x_float = x.float()
|
| 24 |
+
rms = torch.rsqrt(x_float.pow(2).mean(-1, keepdim=True) + self.eps)
|
| 25 |
+
return (x_float * rms * self.weight.float()).type_as(x)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def build_rope_inv_freq(head_dim, theta=100000.0):
|
| 29 |
+
return 1.0 / (theta ** (torch.arange(0, head_dim, 2, dtype=torch.float32) / head_dim))
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def precompute_freqs_cis(head_dim, seq_len, theta=100000.0):
|
| 33 |
+
freqs = build_rope_inv_freq(head_dim, theta)
|
| 34 |
+
positions = torch.arange(seq_len, dtype=torch.float32)
|
| 35 |
+
freqs = torch.outer(positions, freqs)
|
| 36 |
+
return torch.polar(torch.ones_like(freqs), freqs)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def apply_rotary_emb(q, k, freqs_cis):
|
| 40 |
+
q_complex = torch.view_as_complex(q.float().reshape(*q.shape[:-1], -1, 2))
|
| 41 |
+
k_complex = torch.view_as_complex(k.float().reshape(*k.shape[:-1], -1, 2))
|
| 42 |
+
freqs_cis = freqs_cis.unsqueeze(0).unsqueeze(0)
|
| 43 |
+
q_out = torch.view_as_real(q_complex * freqs_cis).flatten(-2)
|
| 44 |
+
k_out = torch.view_as_real(k_complex * freqs_cis).flatten(-2)
|
| 45 |
+
return q_out.type_as(q), k_out.type_as(k)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class BananaMind2NanoAttention(nn.Module):
|
| 49 |
+
def __init__(self, config, layer_idx):
|
| 50 |
+
super().__init__()
|
| 51 |
+
self.layer_idx = layer_idx
|
| 52 |
+
self.n_head = config.num_attention_heads
|
| 53 |
+
self.n_kv_heads = config.num_key_value_heads
|
| 54 |
+
self.head_dim = config.head_dim
|
| 55 |
+
self.n_rep = self.n_head // self.n_kv_heads
|
| 56 |
+
|
| 57 |
+
self.q_proj = nn.Linear(config.hidden_size, self.n_head * self.head_dim, bias=False)
|
| 58 |
+
self.k_proj = nn.Linear(config.hidden_size, self.n_kv_heads * self.head_dim, bias=False)
|
| 59 |
+
self.v_proj = nn.Linear(config.hidden_size, self.n_kv_heads * self.head_dim, bias=False)
|
| 60 |
+
self.o_proj = nn.Linear(self.n_head * self.head_dim, config.hidden_size, bias=False)
|
| 61 |
+
self.q_norm = RMSNorm(self.head_dim, eps=config.rms_norm_eps)
|
| 62 |
+
self.k_norm = RMSNorm(self.head_dim, eps=config.rms_norm_eps)
|
| 63 |
+
|
| 64 |
+
def forward(
|
| 65 |
+
self,
|
| 66 |
+
x,
|
| 67 |
+
freqs_cis,
|
| 68 |
+
attention_mask=None,
|
| 69 |
+
past_key_values=None,
|
| 70 |
+
use_cache=False,
|
| 71 |
+
):
|
| 72 |
+
batch_size, seq_len, _ = x.shape
|
| 73 |
+
q = self.q_proj(x).view(
|
| 74 |
+
batch_size,
|
| 75 |
+
seq_len,
|
| 76 |
+
self.n_head,
|
| 77 |
+
self.head_dim,
|
| 78 |
+
).transpose(1, 2)
|
| 79 |
+
k = self.k_proj(x).view(
|
| 80 |
+
batch_size,
|
| 81 |
+
seq_len,
|
| 82 |
+
self.n_kv_heads,
|
| 83 |
+
self.head_dim,
|
| 84 |
+
).transpose(1, 2)
|
| 85 |
+
v = self.v_proj(x).view(
|
| 86 |
+
batch_size,
|
| 87 |
+
seq_len,
|
| 88 |
+
self.n_kv_heads,
|
| 89 |
+
self.head_dim,
|
| 90 |
+
).transpose(1, 2)
|
| 91 |
+
|
| 92 |
+
q = self.q_norm(q)
|
| 93 |
+
k = self.k_norm(k)
|
| 94 |
+
q, k = apply_rotary_emb(q, k, freqs_cis)
|
| 95 |
+
|
| 96 |
+
past_length = 0
|
| 97 |
+
if use_cache and past_key_values is not None:
|
| 98 |
+
past_length = past_key_values.get_seq_length(self.layer_idx)
|
| 99 |
+
k, v = past_key_values.update(k, v, self.layer_idx)
|
| 100 |
+
|
| 101 |
+
kv_len = k.size(-2)
|
| 102 |
+
k = k.unsqueeze(2).expand(
|
| 103 |
+
batch_size,
|
| 104 |
+
self.n_kv_heads,
|
| 105 |
+
self.n_rep,
|
| 106 |
+
kv_len,
|
| 107 |
+
self.head_dim,
|
| 108 |
+
).reshape(batch_size, self.n_head, kv_len, self.head_dim)
|
| 109 |
+
v = v.unsqueeze(2).expand(
|
| 110 |
+
batch_size,
|
| 111 |
+
self.n_kv_heads,
|
| 112 |
+
self.n_rep,
|
| 113 |
+
kv_len,
|
| 114 |
+
self.head_dim,
|
| 115 |
+
).reshape(batch_size, self.n_head, kv_len, self.head_dim)
|
| 116 |
+
|
| 117 |
+
attn_mask = None
|
| 118 |
+
is_causal = past_length == 0 and attention_mask is None
|
| 119 |
+
if not is_causal:
|
| 120 |
+
query_positions = past_length + torch.arange(seq_len, device=x.device)
|
| 121 |
+
key_positions = torch.arange(kv_len, device=x.device)
|
| 122 |
+
causal = key_positions.unsqueeze(0) <= query_positions.unsqueeze(1)
|
| 123 |
+
attn_mask = causal[None, None, :, :]
|
| 124 |
+
if attention_mask is not None:
|
| 125 |
+
key_padding = attention_mask.to(torch.bool)
|
| 126 |
+
if key_padding.size(-1) < kv_len:
|
| 127 |
+
cached_padding = torch.ones(
|
| 128 |
+
key_padding.size(0),
|
| 129 |
+
kv_len - key_padding.size(-1),
|
| 130 |
+
dtype=torch.bool,
|
| 131 |
+
device=key_padding.device,
|
| 132 |
+
)
|
| 133 |
+
key_padding = torch.cat((cached_padding, key_padding), dim=-1)
|
| 134 |
+
else:
|
| 135 |
+
key_padding = key_padding[:, -kv_len:]
|
| 136 |
+
attn_mask = attn_mask & key_padding[:, None, None, :]
|
| 137 |
+
is_causal = False
|
| 138 |
+
|
| 139 |
+
y = F.scaled_dot_product_attention(
|
| 140 |
+
q,
|
| 141 |
+
k,
|
| 142 |
+
v,
|
| 143 |
+
attn_mask=attn_mask,
|
| 144 |
+
is_causal=is_causal,
|
| 145 |
+
)
|
| 146 |
+
y = y.transpose(1, 2).contiguous().view(
|
| 147 |
+
batch_size,
|
| 148 |
+
seq_len,
|
| 149 |
+
self.n_head * self.head_dim,
|
| 150 |
+
)
|
| 151 |
+
return self.o_proj(y)
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
class BananaMind2NanoSwiGLUMLP(nn.Module):
|
| 155 |
+
def __init__(self, config):
|
| 156 |
+
super().__init__()
|
| 157 |
+
self.w_gate = nn.Linear(config.hidden_size, config.intermediate_size, bias=False)
|
| 158 |
+
self.w_up = nn.Linear(config.hidden_size, config.intermediate_size, bias=False)
|
| 159 |
+
self.w_down = nn.Linear(config.intermediate_size, config.hidden_size, bias=False)
|
| 160 |
+
|
| 161 |
+
def forward(self, x):
|
| 162 |
+
return self.w_down(F.silu(self.w_gate(x)) * self.w_up(x))
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
class BananaMind2NanoBlock(nn.Module):
|
| 166 |
+
def __init__(self, config, layer_idx):
|
| 167 |
+
super().__init__()
|
| 168 |
+
self.ln_1 = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 169 |
+
self.attn = BananaMind2NanoAttention(config, layer_idx)
|
| 170 |
+
self.ln_2 = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 171 |
+
self.mlp = BananaMind2NanoSwiGLUMLP(config)
|
| 172 |
+
|
| 173 |
+
def forward(
|
| 174 |
+
self,
|
| 175 |
+
x,
|
| 176 |
+
freqs_cis,
|
| 177 |
+
attention_mask=None,
|
| 178 |
+
past_key_values=None,
|
| 179 |
+
use_cache=False,
|
| 180 |
+
):
|
| 181 |
+
x = x + self.attn(
|
| 182 |
+
self.ln_1(x),
|
| 183 |
+
freqs_cis,
|
| 184 |
+
attention_mask=attention_mask,
|
| 185 |
+
past_key_values=past_key_values,
|
| 186 |
+
use_cache=use_cache,
|
| 187 |
+
)
|
| 188 |
+
return x + self.mlp(self.ln_2(x))
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
class BananaMind2NanoPreTrainedModel(PreTrainedModel):
|
| 192 |
+
config_class = BananaMind2NanoConfig
|
| 193 |
+
base_model_prefix = "transformer"
|
| 194 |
+
supports_gradient_checkpointing = False
|
| 195 |
+
|
| 196 |
+
def _init_weights(self, module):
|
| 197 |
+
std = 0.02
|
| 198 |
+
if hasattr(module, "NANOGPT_SCALE_INIT"):
|
| 199 |
+
std *= 2 * self.config.num_hidden_layers ** -0.5
|
| 200 |
+
if isinstance(module, nn.Linear):
|
| 201 |
+
nn.init.normal_(module.weight, mean=0.0, std=std)
|
| 202 |
+
elif isinstance(module, nn.Embedding):
|
| 203 |
+
nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
class BananaMind2NanoForCausalLM(BananaMind2NanoPreTrainedModel, GenerationMixin):
|
| 207 |
+
_tied_weights_keys = {"lm_head.weight": "transformer.wte.weight"}
|
| 208 |
+
|
| 209 |
+
def __init__(self, config):
|
| 210 |
+
super().__init__(config)
|
| 211 |
+
self.config = config
|
| 212 |
+
self.transformer = nn.ModuleDict(
|
| 213 |
+
{
|
| 214 |
+
"wte": nn.Embedding(config.vocab_size, config.hidden_size),
|
| 215 |
+
"h": nn.ModuleList(
|
| 216 |
+
[
|
| 217 |
+
BananaMind2NanoBlock(config, layer_idx)
|
| 218 |
+
for layer_idx in range(config.num_hidden_layers)
|
| 219 |
+
]
|
| 220 |
+
),
|
| 221 |
+
"ln_f": RMSNorm(config.hidden_size, eps=config.rms_norm_eps),
|
| 222 |
+
}
|
| 223 |
+
)
|
| 224 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 225 |
+
if config.tie_word_embeddings:
|
| 226 |
+
self.lm_head.weight = self.transformer["wte"].weight
|
| 227 |
+
self._embd_scale = math.sqrt(config.hidden_size)
|
| 228 |
+
self._freqs_cis_cache = None
|
| 229 |
+
self.post_init()
|
| 230 |
+
|
| 231 |
+
def get_input_embeddings(self):
|
| 232 |
+
return self.transformer["wte"]
|
| 233 |
+
|
| 234 |
+
def set_input_embeddings(self, value):
|
| 235 |
+
self.transformer["wte"] = value
|
| 236 |
+
|
| 237 |
+
def get_output_embeddings(self):
|
| 238 |
+
return self.lm_head
|
| 239 |
+
|
| 240 |
+
def set_output_embeddings(self, new_embeddings):
|
| 241 |
+
self.lm_head = new_embeddings
|
| 242 |
+
|
| 243 |
+
def _get_freqs_cis(self, seq_len, device):
|
| 244 |
+
cache = self._freqs_cis_cache
|
| 245 |
+
if cache is None or cache.device != device or cache.size(0) < seq_len:
|
| 246 |
+
cache = precompute_freqs_cis(
|
| 247 |
+
self.config.head_dim,
|
| 248 |
+
seq_len,
|
| 249 |
+
self.config.rope_theta,
|
| 250 |
+
).to(device)
|
| 251 |
+
self._freqs_cis_cache = cache
|
| 252 |
+
return cache[:seq_len]
|
| 253 |
+
|
| 254 |
+
def forward(
|
| 255 |
+
self,
|
| 256 |
+
input_ids,
|
| 257 |
+
attention_mask=None,
|
| 258 |
+
labels=None,
|
| 259 |
+
past_key_values: Optional[Cache] = None,
|
| 260 |
+
use_cache=None,
|
| 261 |
+
**kwargs,
|
| 262 |
+
):
|
| 263 |
+
_, seq_len = input_ids.shape
|
| 264 |
+
if use_cache is None:
|
| 265 |
+
use_cache = self.config.use_cache and labels is None
|
| 266 |
+
if use_cache and past_key_values is None:
|
| 267 |
+
past_key_values = DynamicCache(config=self.config)
|
| 268 |
+
|
| 269 |
+
past_length = past_key_values.get_seq_length() if use_cache else 0
|
| 270 |
+
total_length = past_length + seq_len
|
| 271 |
+
if total_length > self.config.max_position_embeddings:
|
| 272 |
+
raise ValueError(
|
| 273 |
+
f"Sequence length {total_length} exceeds the configured maximum "
|
| 274 |
+
f"of {self.config.max_position_embeddings}"
|
| 275 |
+
)
|
| 276 |
+
|
| 277 |
+
x = self.transformer["wte"](input_ids) * self._embd_scale
|
| 278 |
+
freqs_cis = self._get_freqs_cis(total_length, input_ids.device)[past_length:]
|
| 279 |
+
|
| 280 |
+
for block in self.transformer["h"]:
|
| 281 |
+
x = block(
|
| 282 |
+
x,
|
| 283 |
+
freqs_cis,
|
| 284 |
+
attention_mask=attention_mask,
|
| 285 |
+
past_key_values=past_key_values,
|
| 286 |
+
use_cache=use_cache,
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
x = self.transformer["ln_f"](x)
|
| 290 |
+
logits = self.lm_head(x)
|
| 291 |
+
|
| 292 |
+
loss = None
|
| 293 |
+
if labels is not None:
|
| 294 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
| 295 |
+
shift_labels = labels[..., 1:].contiguous()
|
| 296 |
+
loss = F.cross_entropy(
|
| 297 |
+
shift_logits.float().reshape(-1, shift_logits.size(-1)),
|
| 298 |
+
shift_labels.reshape(-1),
|
| 299 |
+
)
|
| 300 |
+
if self.config.z_loss_coeff:
|
| 301 |
+
loss = loss + self.config.z_loss_coeff * logits.float().pow(2).mean()
|
| 302 |
+
|
| 303 |
+
return CausalLMOutputWithPast(
|
| 304 |
+
loss=loss,
|
| 305 |
+
logits=logits,
|
| 306 |
+
past_key_values=past_key_values if use_cache else None,
|
| 307 |
+
)
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"pad_token": "<|pad|>",
|
| 3 |
+
"bos_token": "<|bos|>",
|
| 4 |
+
"eos_token": "<|eos|>",
|
| 5 |
+
"unk_token": "<|unk|>"
|
| 6 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"tokenizer_class": "PreTrainedTokenizerFast",
|
| 3 |
+
"model_max_length": 4096,
|
| 4 |
+
"pad_token": "<|pad|>",
|
| 5 |
+
"bos_token": "<|bos|>",
|
| 6 |
+
"eos_token": "<|eos|>",
|
| 7 |
+
"unk_token": "<|unk|>"
|
| 8 |
+
}
|
training_metadata.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"parameters": 9968128,
|
| 3 |
+
"architecture": {
|
| 4 |
+
"vocab_size": 8192,
|
| 5 |
+
"hidden_size": 256,
|
| 6 |
+
"num_hidden_layers": 10,
|
| 7 |
+
"num_attention_heads": 4,
|
| 8 |
+
"num_key_value_heads": 2,
|
| 9 |
+
"head_dim": 64,
|
| 10 |
+
"intermediate_size": 768,
|
| 11 |
+
"max_position_embeddings": 4096,
|
| 12 |
+
"rope_theta": 100000.0,
|
| 13 |
+
"rms_norm_eps": 1e-06,
|
| 14 |
+
"tie_word_embeddings": true
|
| 15 |
+
},
|
| 16 |
+
"dataset_id": "HuggingFaceFW/fineweb-edu",
|
| 17 |
+
"dataset_config": "sample-100BT",
|
| 18 |
+
"dataset_revision": "87f09149ef4734204d70ed1d046ddc9ca3f2b8f9",
|
| 19 |
+
"tokenizer_repo": "BananaMind/BananaMind-2-Nano",
|
| 20 |
+
"tokenizer_revision": "c8564d1bd3f6177221ed7e4f63ae5f281a677a1c",
|
| 21 |
+
"optimizer": "AdamW",
|
| 22 |
+
"peak_lr": 0.003,
|
| 23 |
+
"global_batch": 132,
|
| 24 |
+
"tokens_per_step": 540672,
|
| 25 |
+
"steps": 46238,
|
| 26 |
+
"tokens_seen": 24999591936,
|
| 27 |
+
"target_tokens": 25000000000,
|
| 28 |
+
"world_size": 8,
|
| 29 |
+
"gpu_name": "NVIDIA RTX PRO 6000 Blackwell Server Edition",
|
| 30 |
+
"elapsed_seconds": 3753.9637784957886,
|
| 31 |
+
"average_tokens_per_second": 6659518.687742193,
|
| 32 |
+
"estimated_training_flops": 4640871447860871168,
|
| 33 |
+
"seed": 1337
|
| 34 |
+
}
|
training_metrics.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|