Text Generation
Transformers
Safetensors
taonet
trust-remote-code
sentencepiece
custom-architecture
custom_code
Instructions to use TaoTern/TaoNet-mini-A2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use TaoTern/TaoNet-mini-A2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="TaoTern/TaoNet-mini-A2", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("TaoTern/TaoNet-mini-A2", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use TaoTern/TaoNet-mini-A2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "TaoTern/TaoNet-mini-A2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "TaoTern/TaoNet-mini-A2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/TaoTern/TaoNet-mini-A2
- SGLang
How to use TaoTern/TaoNet-mini-A2 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 "TaoTern/TaoNet-mini-A2" \ --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": "TaoTern/TaoNet-mini-A2", "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 "TaoTern/TaoNet-mini-A2" \ --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": "TaoTern/TaoNet-mini-A2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use TaoTern/TaoNet-mini-A2 with Docker Model Runner:
docker model run hf.co/TaoTern/TaoNet-mini-A2
Upload folder using huggingface_hub
Browse files- __pycache__/configuration_taonet.cpython-312.pyc +0 -0
- __pycache__/embeddings.cpython-312.pyc +0 -0
- __pycache__/mla_components.cpython-312.pyc +0 -0
- __pycache__/modeling_taonet.cpython-312.pyc +0 -0
- __pycache__/taonet_model.cpython-312.pyc +0 -0
- __pycache__/tokenization_taonet.cpython-312.pyc +0 -0
- checkpoints/sft/final_model.pt +3 -0
- configs/pretrain.yaml +138 -0
- configs/pretrain_gamma.yaml +116 -0
- configs/rl_dpo.yaml +60 -0
- configs/sft.yaml +92 -0
- configs/tokenizer.yaml +35 -0
- configs/vlm.yaml +86 -0
- configs/vlm_sft.yaml +86 -0
- configs/yarn4k.yaml +75 -0
- configs/yarn8k.yaml +75 -0
- configs/yarn_pretrain.yaml +181 -0
- tokenization_taonet.py +111 -0
__pycache__/configuration_taonet.cpython-312.pyc
ADDED
|
Binary file (2.55 kB). View file
|
|
|
__pycache__/embeddings.cpython-312.pyc
ADDED
|
Binary file (1.63 kB). View file
|
|
|
__pycache__/mla_components.cpython-312.pyc
ADDED
|
Binary file (10.4 kB). View file
|
|
|
__pycache__/modeling_taonet.cpython-312.pyc
ADDED
|
Binary file (5.11 kB). View file
|
|
|
__pycache__/taonet_model.cpython-312.pyc
ADDED
|
Binary file (8.75 kB). View file
|
|
|
__pycache__/tokenization_taonet.cpython-312.pyc
ADDED
|
Binary file (6.85 kB). View file
|
|
|
checkpoints/sft/final_model.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dcbace558f0fea7a597de65aec13d6bfb2db23926c02178b638b75febe5bf7a2
|
| 3 |
+
size 2578583807
|
configs/pretrain.yaml
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# TaoNet T2 Configuration for Pretraining
|
| 2 |
+
# DeepSeek MLA + RoPE with Hybrid Muon+AdamW Optimizer
|
| 3 |
+
# Full BF16 precision (no quantization)
|
| 4 |
+
|
| 5 |
+
# ============================================================================
|
| 6 |
+
# Model Architecture - TaoNet (DeepSeek MLA + RoPE)
|
| 7 |
+
# ============================================================================
|
| 8 |
+
model:
|
| 9 |
+
architecture_type: taonet
|
| 10 |
+
vocab_size: 8192
|
| 11 |
+
hidden_dim: 1024
|
| 12 |
+
num_layers: 16
|
| 13 |
+
num_heads: 8
|
| 14 |
+
max_seq_length: 1024
|
| 15 |
+
|
| 16 |
+
# TaoNet-specific: Multi-head Latent Attention (MLA) compression
|
| 17 |
+
d_latent_kv: 768
|
| 18 |
+
|
| 19 |
+
# RoPE (Rotary Position Embedding) dimension per head
|
| 20 |
+
# With hidden_dim=1024 and num_heads=8, head_dim = 128
|
| 21 |
+
d_rope: 128
|
| 22 |
+
|
| 23 |
+
# Feed-forward intermediate dimension
|
| 24 |
+
hidden_dim_ff: 3072
|
| 25 |
+
|
| 26 |
+
# Dropout rate (low for stability with large models)
|
| 27 |
+
dropout: 0.02
|
| 28 |
+
|
| 29 |
+
# Grouped Query Attention (1 = standard MLA, >1 = GQA)
|
| 30 |
+
gqa_groups: 1
|
| 31 |
+
|
| 32 |
+
# Optional: Use factorized embedding for parameter efficiency
|
| 33 |
+
# vocab (8192) → rank (96) → hidden (1024)
|
| 34 |
+
use_factorized_embedding: true
|
| 35 |
+
d_embed_rank: 96
|
| 36 |
+
|
| 37 |
+
# Weight initialization standard deviation
|
| 38 |
+
init_std: 0.02
|
| 39 |
+
|
| 40 |
+
# ============================================================================
|
| 41 |
+
# Dataset Configuration - Local JSONL
|
| 42 |
+
# ============================================================================
|
| 43 |
+
dataset:
|
| 44 |
+
local: true
|
| 45 |
+
jsonl_path: /home/student/Data/TaoData/pretrain.jsonl
|
| 46 |
+
text_field: text
|
| 47 |
+
max_samples: 6700000
|
| 48 |
+
samples_per_chunk: 1000
|
| 49 |
+
|
| 50 |
+
# Tokenizer configuration
|
| 51 |
+
tokenizer_type: sentencepiece
|
| 52 |
+
tokenizer_path: tokenizer/tokenizer.model
|
| 53 |
+
tokenizer_threads: 4
|
| 54 |
+
|
| 55 |
+
# ============================================================================
|
| 56 |
+
# Training Hyperparameters
|
| 57 |
+
# ============================================================================
|
| 58 |
+
batch_size: 8
|
| 59 |
+
num_epochs: 1 # Set to 10 for full training
|
| 60 |
+
gradient_accumulation_steps: 32 # Effective batch: 8 × 32 = 256
|
| 61 |
+
|
| 62 |
+
# Maximum gradient norm for clipping (prevents ternary instability)
|
| 63 |
+
max_grad_norm: 1.0
|
| 64 |
+
|
| 65 |
+
# ============================================================================
|
| 66 |
+
# Optimizer - Hybrid Muon + AdamW
|
| 67 |
+
# ============================================================================
|
| 68 |
+
# Strategy:
|
| 69 |
+
# - Muon: For 2D Linear weight matrices (orthogonal/SVD-based optimization)
|
| 70 |
+
# - 2D weights: learning_rate (3e-3)
|
| 71 |
+
# - AdamW: For 1D parameters (biases, norms, embeddings)
|
| 72 |
+
# - 1D params: adamw_lr (3e-4) = 1/10 × learning_rate
|
| 73 |
+
|
| 74 |
+
optimizer:
|
| 75 |
+
optimizer_type: hybrid_muon_adamw
|
| 76 |
+
|
| 77 |
+
# Learning rate for Muon (2D weight matrices)
|
| 78 |
+
learning_rate: 3e-3
|
| 79 |
+
|
| 80 |
+
# Learning rate for AdamW (1D parameters)
|
| 81 |
+
# Typically 1/10 of learning_rate to prevent over-updating 1D params
|
| 82 |
+
adamw_lr: 3e-4
|
| 83 |
+
|
| 84 |
+
# L2 regularization (weight decay)
|
| 85 |
+
weight_decay: 0.01
|
| 86 |
+
|
| 87 |
+
# Adam betas
|
| 88 |
+
betas: [0.9, 0.999]
|
| 89 |
+
|
| 90 |
+
# Epsilon for numerical stability
|
| 91 |
+
eps: 1e-8
|
| 92 |
+
|
| 93 |
+
# ============================================================================
|
| 94 |
+
# Learning Rate Scheduler - 3-Phase Cosine with Warmup
|
| 95 |
+
# ============================================================================
|
| 96 |
+
# Phases:
|
| 97 |
+
# 1. Warmup: 0 → 1.0 (300 steps, ~1.4% of training)
|
| 98 |
+
# 2. Steady: 1.0 (constant for 5% of training)
|
| 99 |
+
# 3. Decay: 1.0 → 0.1 (cosine decay for remaining 95%)
|
| 100 |
+
|
| 101 |
+
scheduler:
|
| 102 |
+
scheduler_type: cosineWarmup
|
| 103 |
+
warmup_steps: 300
|
| 104 |
+
warmup_ratio: 0.0 # Ignored if warmup_steps > 0
|
| 105 |
+
steady_ratio: 0.05 # 5% of total training steps at peak LR
|
| 106 |
+
min_lr_ratio: 0.1 # Decay to 10% of peak LR
|
| 107 |
+
num_cycles: 0.5 # For compatibility (not used in 3-phase schedule)
|
| 108 |
+
|
| 109 |
+
# ============================================================================
|
| 110 |
+
# Data Type and Device
|
| 111 |
+
# ============================================================================
|
| 112 |
+
dtype: bfloat16 # Use BF16 for better convergence with large models
|
| 113 |
+
device: cuda # Use GPU for training
|
| 114 |
+
|
| 115 |
+
# ============================================================================
|
| 116 |
+
# Checkpointing and Validation
|
| 117 |
+
# ============================================================================
|
| 118 |
+
checkpoint_dir: checkpoints/test
|
| 119 |
+
save_every_steps: 81920
|
| 120 |
+
save_best_model: true
|
| 121 |
+
keep_last_n_checkpoints: 3
|
| 122 |
+
|
| 123 |
+
# Validation
|
| 124 |
+
eval_every_steps: 8192
|
| 125 |
+
eval_samples: 8000
|
| 126 |
+
|
| 127 |
+
# ============================================================================
|
| 128 |
+
# Logging
|
| 129 |
+
# ============================================================================
|
| 130 |
+
log_every_steps: 50
|
| 131 |
+
aim_repo: .aim
|
| 132 |
+
|
| 133 |
+
# ============================================================================
|
| 134 |
+
# Miscellaneous
|
| 135 |
+
# ============================================================================
|
| 136 |
+
seed: 42
|
| 137 |
+
num_workers: 0
|
| 138 |
+
pin_memory: true
|
configs/pretrain_gamma.yaml
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# GammaNet Configuration for Pretraining
|
| 2 |
+
# Gamma Space Model blocks in the TaoTrain causal LM shell
|
| 3 |
+
|
| 4 |
+
# ============================================================================
|
| 5 |
+
# Model Architecture - GammaNet (Gamma Space Model)
|
| 6 |
+
# ============================================================================
|
| 7 |
+
model:
|
| 8 |
+
architecture_type: gamma_net
|
| 9 |
+
vocab_size: 8192
|
| 10 |
+
hidden_dim: 768
|
| 11 |
+
num_layers: 10
|
| 12 |
+
num_heads: 8
|
| 13 |
+
max_seq_length: 1024
|
| 14 |
+
|
| 15 |
+
# Shared LM shell feed-forward / embedding controls
|
| 16 |
+
hidden_dim_ff: 2048
|
| 17 |
+
dropout: 0.02
|
| 18 |
+
use_factorized_embedding: false
|
| 19 |
+
d_embed_rank: 96
|
| 20 |
+
|
| 21 |
+
# GammaSpaceBlock-specific settings
|
| 22 |
+
gamma_hidden_dim: 256
|
| 23 |
+
gamma_dt_min: 1e-3
|
| 24 |
+
gamma_dt_max: 1e-1
|
| 25 |
+
gamma_dt_init: 1e-2
|
| 26 |
+
gamma_discretization: bilinear
|
| 27 |
+
gamma_prenorm: true
|
| 28 |
+
gamma_residual_scale: 1.0
|
| 29 |
+
gamma_activation: gelu
|
| 30 |
+
gamma_gate: true
|
| 31 |
+
gamma_use_D: true
|
| 32 |
+
gamma_kernel_mode: auto
|
| 33 |
+
gamma_kernel_threshold: 64
|
| 34 |
+
gamma_use_output_linear: true
|
| 35 |
+
gamma_gate_bias: 2.0
|
| 36 |
+
gamma_input_gate: true
|
| 37 |
+
gamma_input_gate_bias: 2.0
|
| 38 |
+
gamma_layer_scale_init: 0.1
|
| 39 |
+
|
| 40 |
+
# Weight initialization standard deviation for TaoTrain LM shell
|
| 41 |
+
init_std: 0.02
|
| 42 |
+
|
| 43 |
+
# ============================================================================
|
| 44 |
+
# Dataset Configuration - Local JSONL
|
| 45 |
+
# ============================================================================
|
| 46 |
+
dataset:
|
| 47 |
+
local: true
|
| 48 |
+
jsonl_path: /home/student/Data/TaoData/pretrain.jsonl
|
| 49 |
+
text_field: text
|
| 50 |
+
max_samples: 1000000
|
| 51 |
+
samples_per_chunk: 1000
|
| 52 |
+
|
| 53 |
+
# Tokenizer configuration
|
| 54 |
+
tokenizer_type: sentencepiece
|
| 55 |
+
tokenizer_path: tokenizer/tokenizer.model
|
| 56 |
+
tokenizer_threads: 4
|
| 57 |
+
|
| 58 |
+
# ============================================================================
|
| 59 |
+
# Training Hyperparameters
|
| 60 |
+
# ============================================================================
|
| 61 |
+
batch_size: 32
|
| 62 |
+
num_epochs: 2
|
| 63 |
+
gradient_accumulation_steps: 8
|
| 64 |
+
max_grad_norm: 1.0
|
| 65 |
+
|
| 66 |
+
# ============================================================================
|
| 67 |
+
# Optimizer - Hybrid Muon + AdamW
|
| 68 |
+
# ============================================================================
|
| 69 |
+
optimizer:
|
| 70 |
+
optimizer_type: hybrid_muon_adamw
|
| 71 |
+
learning_rate: 5e-3
|
| 72 |
+
adamw_lr: 5e-4
|
| 73 |
+
weight_decay: 0.01
|
| 74 |
+
betas: [0.9, 0.999]
|
| 75 |
+
eps: 1e-8
|
| 76 |
+
|
| 77 |
+
# ============================================================================
|
| 78 |
+
# Learning Rate Scheduler - 3-Phase Cosine with Warmup
|
| 79 |
+
# ============================================================================
|
| 80 |
+
scheduler:
|
| 81 |
+
scheduler_type: cosineWarmup
|
| 82 |
+
warmup_steps: 300
|
| 83 |
+
warmup_ratio: 0.0
|
| 84 |
+
steady_ratio: 0.05
|
| 85 |
+
min_lr_ratio: 0.1
|
| 86 |
+
num_cycles: 0.5
|
| 87 |
+
|
| 88 |
+
# ============================================================================
|
| 89 |
+
# Data Type and Device
|
| 90 |
+
# ============================================================================
|
| 91 |
+
dtype: bfloat16
|
| 92 |
+
device: cuda
|
| 93 |
+
|
| 94 |
+
# ============================================================================
|
| 95 |
+
# Checkpointing and Validation
|
| 96 |
+
# ============================================================================
|
| 97 |
+
checkpoint_dir: checkpoints/pretrain_gamma
|
| 98 |
+
save_every_steps: 81920
|
| 99 |
+
save_best_model: true
|
| 100 |
+
keep_last_n_checkpoints: 3
|
| 101 |
+
|
| 102 |
+
eval_every_steps: 8192
|
| 103 |
+
eval_samples: 8000
|
| 104 |
+
|
| 105 |
+
# ============================================================================
|
| 106 |
+
# Logging
|
| 107 |
+
# ============================================================================
|
| 108 |
+
log_every_steps: 50
|
| 109 |
+
aim_repo: .aim
|
| 110 |
+
|
| 111 |
+
# ============================================================================
|
| 112 |
+
# Miscellaneous
|
| 113 |
+
# ============================================================================
|
| 114 |
+
seed: 42
|
| 115 |
+
num_workers: 0
|
| 116 |
+
pin_memory: true
|
configs/rl_dpo.yaml
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Example configuration for RL training (RL stage assumes you have a reward model)
|
| 2 |
+
|
| 3 |
+
model:
|
| 4 |
+
architecture_type: transformer
|
| 5 |
+
vocab_size: 50257
|
| 6 |
+
hidden_dim: 256
|
| 7 |
+
num_layers: 8
|
| 8 |
+
num_heads: 8
|
| 9 |
+
dropout: 0.1
|
| 10 |
+
max_seq_length: 512
|
| 11 |
+
init_std: 0.02
|
| 12 |
+
|
| 13 |
+
dataset:
|
| 14 |
+
dataset_name: allenai/real_toxicity_prompts
|
| 15 |
+
split: train
|
| 16 |
+
prompt_column: text
|
| 17 |
+
max_samples: 2000
|
| 18 |
+
cache_dir: .cache/datasets
|
| 19 |
+
tokenizer_threads: 1 # Number of background threads for tokenization (1-32 recommended)
|
| 20 |
+
|
| 21 |
+
batch_size: 4
|
| 22 |
+
num_epochs: 1
|
| 23 |
+
gradient_accumulation_steps: 8
|
| 24 |
+
max_grad_norm: 0.5
|
| 25 |
+
|
| 26 |
+
optimizer:
|
| 27 |
+
optimizer_type: adamw
|
| 28 |
+
learning_rate: 1e-5
|
| 29 |
+
weight_decay: 0.0
|
| 30 |
+
|
| 31 |
+
scheduler:
|
| 32 |
+
scheduler_type: linearWarmup
|
| 33 |
+
warmup_steps: 50
|
| 34 |
+
|
| 35 |
+
dtype: bfloat16
|
| 36 |
+
device: cuda
|
| 37 |
+
|
| 38 |
+
checkpoint_dir: checkpoints/rl
|
| 39 |
+
save_every_steps: 100
|
| 40 |
+
save_best_model: false
|
| 41 |
+
keep_last_n_checkpoints: 2
|
| 42 |
+
|
| 43 |
+
eval_every_steps: 100
|
| 44 |
+
eval_samples: 100
|
| 45 |
+
|
| 46 |
+
log_every_steps: 10
|
| 47 |
+
aim_repo: .aim
|
| 48 |
+
|
| 49 |
+
# RL-specific settings
|
| 50 |
+
rl_method: ppo # or "dpo"
|
| 51 |
+
reward_model_path: checkpoints/reward_model.pt # Path to your reward model
|
| 52 |
+
ppo_epochs: 4
|
| 53 |
+
ppo_clip_ratio: 0.2
|
| 54 |
+
entropy_coeff: 0.01
|
| 55 |
+
value_loss_coeff: 1.0
|
| 56 |
+
generation_max_length: 256
|
| 57 |
+
|
| 58 |
+
seed: 42
|
| 59 |
+
num_workers: 0
|
| 60 |
+
pin_memory: true
|
configs/sft.yaml
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Example configuration for supervised fine-tuning
|
| 2 |
+
# Uses TaoNet (MLA+RoPE) architecture loaded from pretrained checkpoint
|
| 3 |
+
|
| 4 |
+
# ============================================================================
|
| 5 |
+
# Model Architecture - TaoNet (DeepSeek MLA + RoPE)
|
| 6 |
+
# ============================================================================
|
| 7 |
+
model:
|
| 8 |
+
architecture_type: taonet
|
| 9 |
+
vocab_size: 8192
|
| 10 |
+
hidden_dim: 1024
|
| 11 |
+
num_layers: 16
|
| 12 |
+
num_heads: 8
|
| 13 |
+
max_seq_length: 1024
|
| 14 |
+
|
| 15 |
+
# TaoNet-specific: Multi-head Latent Attention (MLA) compression
|
| 16 |
+
d_latent_kv: 768
|
| 17 |
+
|
| 18 |
+
# RoPE (Rotary Position Embedding) dimension per head
|
| 19 |
+
# With hidden_dim=1024 and num_heads=8, head_dim = 128
|
| 20 |
+
d_rope: 128
|
| 21 |
+
|
| 22 |
+
# Feed-forward intermediate dimension
|
| 23 |
+
hidden_dim_ff: 3072
|
| 24 |
+
|
| 25 |
+
# Dropout rate (low for stability with large models)
|
| 26 |
+
dropout: 0.02
|
| 27 |
+
|
| 28 |
+
# Grouped Query Attention (1 = standard MLA, >1 = GQA)
|
| 29 |
+
gqa_groups: 1
|
| 30 |
+
|
| 31 |
+
# Optional: Use factorized embedding for parameter efficiency
|
| 32 |
+
# vocab (8192) → rank (96) → hidden (1024)
|
| 33 |
+
use_factorized_embedding: true
|
| 34 |
+
d_embed_rank: 96
|
| 35 |
+
|
| 36 |
+
# Weight initialization standard deviation
|
| 37 |
+
init_std: 0.02
|
| 38 |
+
|
| 39 |
+
dataset:
|
| 40 |
+
split: train
|
| 41 |
+
instruction_column: input
|
| 42 |
+
response_column: output
|
| 43 |
+
|
| 44 |
+
local: true
|
| 45 |
+
jsonl_path: /home/student/Data/TaoData/sft.jsonl
|
| 46 |
+
samples_per_chunk: 1000
|
| 47 |
+
max_samples: 160000
|
| 48 |
+
cache_dir: .cache/datasets
|
| 49 |
+
instruction_template: "{instruction}\n{response}"
|
| 50 |
+
|
| 51 |
+
# Tokenizer configuration
|
| 52 |
+
tokenizer_type: sentencepiece
|
| 53 |
+
tokenizer_path: tokenizer/tokenizer.model
|
| 54 |
+
tokenizer_threads: 4
|
| 55 |
+
|
| 56 |
+
# SFT-specific configuration (these fields are in SFTConfig)
|
| 57 |
+
checkpoint_path: "checkpoints/yarn8k/best_model.pt"
|
| 58 |
+
user_token: "<user>"
|
| 59 |
+
assistant_token: "<assistant>"
|
| 60 |
+
response_loss_only: true
|
| 61 |
+
|
| 62 |
+
batch_size: 8
|
| 63 |
+
num_epochs: 1
|
| 64 |
+
gradient_accumulation_steps: 4
|
| 65 |
+
max_grad_norm: 1.0
|
| 66 |
+
|
| 67 |
+
optimizer:
|
| 68 |
+
optimizer_type: adamw
|
| 69 |
+
learning_rate: 5e-5 # Lower LR for fine-tuning (vs 5e-4 pretrain base, 5e-3 Muon)
|
| 70 |
+
weight_decay: 0.01
|
| 71 |
+
|
| 72 |
+
scheduler:
|
| 73 |
+
scheduler_type: linearWarmup
|
| 74 |
+
warmup_steps: 500 # Less aggressive warmup for fine-tuning
|
| 75 |
+
|
| 76 |
+
dtype: bfloat16
|
| 77 |
+
device: cuda
|
| 78 |
+
|
| 79 |
+
checkpoint_dir: checkpoints/sft
|
| 80 |
+
save_every_steps: 81920
|
| 81 |
+
save_best_model: true
|
| 82 |
+
keep_last_n_checkpoints: 2
|
| 83 |
+
|
| 84 |
+
eval_every_steps: 8192
|
| 85 |
+
eval_samples: 200
|
| 86 |
+
|
| 87 |
+
log_every_steps: 10
|
| 88 |
+
aim_repo: .aim
|
| 89 |
+
|
| 90 |
+
seed: 42
|
| 91 |
+
num_workers: 0
|
| 92 |
+
pin_memory: true
|
configs/tokenizer.yaml
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Example configuration for training a SentencePiece tokenizer from JSONL data
|
| 2 |
+
|
| 3 |
+
# Dataset source - JSONL file
|
| 4 |
+
jsonl_path: /home/student/Data/TaoData/pretrain.jsonl
|
| 5 |
+
text_field: text # Field name in JSON for text data
|
| 6 |
+
|
| 7 |
+
# Tokenizer training parameters
|
| 8 |
+
vocab_size: 8192 # Keep aligned with pretrain/sft model vocab_size
|
| 9 |
+
model_type: unigram # SentencePiece model type: unigram, bpe, char, word
|
| 10 |
+
character_coverage: 0.9995
|
| 11 |
+
|
| 12 |
+
# Output configuration
|
| 13 |
+
output_dir: tokenizer
|
| 14 |
+
tokenizer_prefix: tokenizer
|
| 15 |
+
|
| 16 |
+
# Custom special tokens
|
| 17 |
+
# Built-in tokens are managed by SentencePiece and resolved at runtime.
|
| 18 |
+
# Entries here are registered as user-defined symbols and should encode as
|
| 19 |
+
# single tokens, but SentencePiece does not guarantee their exact IDs.
|
| 20 |
+
# Note: Use \n for newline token, \t for tab, etc.
|
| 21 |
+
special_tokens:
|
| 22 |
+
- "\n" # Newline token - quoted to preserve literal \n in YAML
|
| 23 |
+
- <think> # Special token for chain-of-thought reasoning
|
| 24 |
+
- <user> # User message token
|
| 25 |
+
- <assistant> # Assistant message token
|
| 26 |
+
- <image> # Image token for multimodal models
|
| 27 |
+
|
| 28 |
+
# Data sampling (optional)
|
| 29 |
+
# Set to a number to train on only the first N samples from the JSONL file
|
| 30 |
+
# Useful for quick testing or sub-sampling large datasets
|
| 31 |
+
# Omit or set to null to use entire file
|
| 32 |
+
max_samples: 1000000
|
| 33 |
+
|
| 34 |
+
# Optional metadata
|
| 35 |
+
tokenizer_name: tokenizer
|
configs/vlm.yaml
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Example configuration for multimodal vision-language connector training
|
| 2 |
+
# Intended training path: pretrain.yaml -> sft.yaml -> vlm.yaml
|
| 3 |
+
|
| 4 |
+
model:
|
| 5 |
+
architecture_type: multimodal_wrapper
|
| 6 |
+
llm_architecture_type: taonet
|
| 7 |
+
vocab_size: 8192
|
| 8 |
+
hidden_dim: 768
|
| 9 |
+
num_layers: 10
|
| 10 |
+
num_heads: 8
|
| 11 |
+
max_seq_length: 1024
|
| 12 |
+
d_latent_kv: 512
|
| 13 |
+
d_rope: 64
|
| 14 |
+
hidden_dim_ff: 2048
|
| 15 |
+
dropout: 0.02
|
| 16 |
+
gqa_groups: 1
|
| 17 |
+
use_factorized_embedding: false
|
| 18 |
+
d_embed_rank: 96
|
| 19 |
+
init_std: 0.02
|
| 20 |
+
|
| 21 |
+
vision_encoder_type: cnn
|
| 22 |
+
vision_output_dim: 256
|
| 23 |
+
image_size: 224
|
| 24 |
+
vision_prefix_tokens: 10
|
| 25 |
+
image_token: <image>
|
| 26 |
+
cnn_channels: [32, 64, 128]
|
| 27 |
+
cnn_kernel_size: 3
|
| 28 |
+
|
| 29 |
+
dataset:
|
| 30 |
+
local: true
|
| 31 |
+
jsonl_path: /home/student/Data/TaoData/vision_pretrain.jsonl
|
| 32 |
+
image_path_column: image
|
| 33 |
+
image_path_aliases: [image, image_path, image_file, file_name]
|
| 34 |
+
caption_prompt: Describe the image.
|
| 35 |
+
samples_per_chunk: 1000
|
| 36 |
+
max_samples: 160000
|
| 37 |
+
cache_dir: .cache/datasets
|
| 38 |
+
|
| 39 |
+
tokenizer_type: sentencepiece
|
| 40 |
+
tokenizer_path: tokenizer/tokenizer.model
|
| 41 |
+
tokenizer_threads: 4
|
| 42 |
+
|
| 43 |
+
checkpoint_path: checkpoints/sft/final_model.pt
|
| 44 |
+
user_token: <user>
|
| 45 |
+
assistant_token: <assistant>
|
| 46 |
+
response_loss_only: true
|
| 47 |
+
|
| 48 |
+
freeze_llm: true
|
| 49 |
+
unfreeze_last_n_layers: 2
|
| 50 |
+
vision_learning_rate: 1e-4
|
| 51 |
+
llm_learning_rate: 5e-5
|
| 52 |
+
vision_prefix_tokens: 10
|
| 53 |
+
image_token: <image>
|
| 54 |
+
image_size: 224
|
| 55 |
+
|
| 56 |
+
batch_size: 8
|
| 57 |
+
num_epochs: 1
|
| 58 |
+
gradient_accumulation_steps: 4
|
| 59 |
+
max_grad_norm: 1.0
|
| 60 |
+
|
| 61 |
+
optimizer:
|
| 62 |
+
optimizer_type: adamw
|
| 63 |
+
learning_rate: 1e-4
|
| 64 |
+
weight_decay: 0.01
|
| 65 |
+
|
| 66 |
+
scheduler:
|
| 67 |
+
scheduler_type: linearWarmup
|
| 68 |
+
warmup_steps: 500
|
| 69 |
+
|
| 70 |
+
dtype: bfloat16
|
| 71 |
+
device: cuda
|
| 72 |
+
|
| 73 |
+
checkpoint_dir: checkpoints/vlm
|
| 74 |
+
save_every_steps: 81920
|
| 75 |
+
save_best_model: true
|
| 76 |
+
keep_last_n_checkpoints: 2
|
| 77 |
+
|
| 78 |
+
eval_every_steps: 8192
|
| 79 |
+
eval_samples: 200
|
| 80 |
+
|
| 81 |
+
log_every_steps: 10
|
| 82 |
+
aim_repo: .aim
|
| 83 |
+
|
| 84 |
+
seed: 42
|
| 85 |
+
num_workers: 0
|
| 86 |
+
pin_memory: true
|
configs/vlm_sft.yaml
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Example configuration for end-to-end multimodal supervised fine-tuning
|
| 2 |
+
# Intended training path: pretrain.yaml -> sft.yaml -> vlm.yaml -> vlm_sft.yaml
|
| 3 |
+
|
| 4 |
+
model:
|
| 5 |
+
architecture_type: multimodal_wrapper
|
| 6 |
+
llm_architecture_type: taonet
|
| 7 |
+
vocab_size: 8192
|
| 8 |
+
hidden_dim: 768
|
| 9 |
+
num_layers: 10
|
| 10 |
+
num_heads: 8
|
| 11 |
+
max_seq_length: 1024
|
| 12 |
+
d_latent_kv: 512
|
| 13 |
+
d_rope: 64
|
| 14 |
+
hidden_dim_ff: 2048
|
| 15 |
+
dropout: 0.02
|
| 16 |
+
gqa_groups: 1
|
| 17 |
+
use_factorized_embedding: false
|
| 18 |
+
d_embed_rank: 96
|
| 19 |
+
init_std: 0.02
|
| 20 |
+
|
| 21 |
+
vision_encoder_type: cnn
|
| 22 |
+
vision_output_dim: 256
|
| 23 |
+
image_size: 224
|
| 24 |
+
vision_prefix_tokens: 10
|
| 25 |
+
image_token: <image>
|
| 26 |
+
cnn_channels: [32, 64, 128]
|
| 27 |
+
cnn_kernel_size: 3
|
| 28 |
+
|
| 29 |
+
dataset:
|
| 30 |
+
local: true
|
| 31 |
+
jsonl_path: /home/student/Data/TaoData/vision_sft.jsonl
|
| 32 |
+
image_path_column: image
|
| 33 |
+
image_path_aliases: [images, image_path, image_file, file_name]
|
| 34 |
+
caption_prompt: Describe the image.
|
| 35 |
+
samples_per_chunk: 1000
|
| 36 |
+
max_samples: 160000
|
| 37 |
+
cache_dir: .cache/datasets
|
| 38 |
+
|
| 39 |
+
tokenizer_type: sentencepiece
|
| 40 |
+
tokenizer_path: tokenizer/tokenizer.model
|
| 41 |
+
tokenizer_threads: 4
|
| 42 |
+
|
| 43 |
+
checkpoint_path: checkpoints/vlm/final_model.pt
|
| 44 |
+
user_token: <user>
|
| 45 |
+
assistant_token: <assistant>
|
| 46 |
+
response_loss_only: true
|
| 47 |
+
|
| 48 |
+
freeze_llm: false
|
| 49 |
+
unfreeze_last_n_layers: 0
|
| 50 |
+
vision_learning_rate: 5e-5
|
| 51 |
+
llm_learning_rate: 5e-5
|
| 52 |
+
vision_prefix_tokens: 10
|
| 53 |
+
image_token: <image>
|
| 54 |
+
image_size: 224
|
| 55 |
+
|
| 56 |
+
batch_size: 8
|
| 57 |
+
num_epochs: 1
|
| 58 |
+
gradient_accumulation_steps: 4
|
| 59 |
+
max_grad_norm: 1.0
|
| 60 |
+
|
| 61 |
+
optimizer:
|
| 62 |
+
optimizer_type: adamw
|
| 63 |
+
learning_rate: 5e-5
|
| 64 |
+
weight_decay: 0.01
|
| 65 |
+
|
| 66 |
+
scheduler:
|
| 67 |
+
scheduler_type: linearWarmup
|
| 68 |
+
warmup_steps: 500
|
| 69 |
+
|
| 70 |
+
dtype: bfloat16
|
| 71 |
+
device: cuda
|
| 72 |
+
|
| 73 |
+
checkpoint_dir: checkpoints/vlm_sft
|
| 74 |
+
save_every_steps: 81920
|
| 75 |
+
save_best_model: true
|
| 76 |
+
keep_last_n_checkpoints: 2
|
| 77 |
+
|
| 78 |
+
eval_every_steps: 8192
|
| 79 |
+
eval_samples: 200
|
| 80 |
+
|
| 81 |
+
log_every_steps: 10
|
| 82 |
+
aim_repo: .aim
|
| 83 |
+
|
| 84 |
+
seed: 42
|
| 85 |
+
num_workers: 0
|
| 86 |
+
pin_memory: true
|
configs/yarn4k.yaml
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# TaoNet 200M Configuration for YaRN Continued Pretraining (1k -> 4k)
|
| 2 |
+
|
| 3 |
+
model:
|
| 4 |
+
architecture_type: taonet
|
| 5 |
+
vocab_size: 8192
|
| 6 |
+
hidden_dim: 1024
|
| 7 |
+
num_layers: 16
|
| 8 |
+
num_heads: 8
|
| 9 |
+
max_seq_length: 4096
|
| 10 |
+
|
| 11 |
+
d_latent_kv: 768
|
| 12 |
+
d_rope: 128
|
| 13 |
+
hidden_dim_ff: 3072
|
| 14 |
+
dropout: 0.02
|
| 15 |
+
gqa_groups: 1
|
| 16 |
+
use_factorized_embedding: true
|
| 17 |
+
d_embed_rank: 96
|
| 18 |
+
init_std: 0.02
|
| 19 |
+
|
| 20 |
+
rope_scale: 40.0
|
| 21 |
+
yarn_enabled: true
|
| 22 |
+
yarn_original_max_seq_length: 1024
|
| 23 |
+
yarn_alpha: 1.0
|
| 24 |
+
|
| 25 |
+
dataset:
|
| 26 |
+
local: true
|
| 27 |
+
jsonl_path: /home/student/Data/TaoData/pretrain.jsonl
|
| 28 |
+
text_field: text
|
| 29 |
+
max_samples: 300000
|
| 30 |
+
samples_per_chunk: 1000
|
| 31 |
+
|
| 32 |
+
tokenizer_type: sentencepiece
|
| 33 |
+
tokenizer_path: tokenizer/tokenizer.model
|
| 34 |
+
tokenizer_threads: 4
|
| 35 |
+
|
| 36 |
+
checkpoint_path: checkpoints/pretrain/final_model.pt
|
| 37 |
+
|
| 38 |
+
batch_size: 2
|
| 39 |
+
num_epochs: 1
|
| 40 |
+
gradient_accumulation_steps: 64
|
| 41 |
+
max_grad_norm: 1.0
|
| 42 |
+
|
| 43 |
+
optimizer:
|
| 44 |
+
optimizer_type: hybrid_muon_adamw
|
| 45 |
+
learning_rate: 1.5e-3
|
| 46 |
+
adamw_lr: 1.5e-4
|
| 47 |
+
weight_decay: 0.01
|
| 48 |
+
betas: [0.9, 0.999]
|
| 49 |
+
eps: 1e-8
|
| 50 |
+
|
| 51 |
+
scheduler:
|
| 52 |
+
scheduler_type: cosineWarmup
|
| 53 |
+
warmup_steps: 300
|
| 54 |
+
warmup_ratio: 0.0
|
| 55 |
+
steady_ratio: 0.05
|
| 56 |
+
min_lr_ratio: 0.1
|
| 57 |
+
num_cycles: 0.5
|
| 58 |
+
|
| 59 |
+
dtype: bfloat16
|
| 60 |
+
device: cuda
|
| 61 |
+
|
| 62 |
+
checkpoint_dir: checkpoints/yarn4k
|
| 63 |
+
save_every_steps: 4096
|
| 64 |
+
save_best_model: true
|
| 65 |
+
keep_last_n_checkpoints: 3
|
| 66 |
+
|
| 67 |
+
eval_every_steps: 4096
|
| 68 |
+
eval_samples: 4000
|
| 69 |
+
|
| 70 |
+
log_every_steps: 50
|
| 71 |
+
aim_repo: .aim
|
| 72 |
+
|
| 73 |
+
seed: 42
|
| 74 |
+
num_workers: 0
|
| 75 |
+
pin_memory: true
|
configs/yarn8k.yaml
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# TaoNet 200M Configuration for YaRN Continued Pretraining (4k -> 8k)
|
| 2 |
+
|
| 3 |
+
model:
|
| 4 |
+
architecture_type: taonet
|
| 5 |
+
vocab_size: 8192
|
| 6 |
+
hidden_dim: 1024
|
| 7 |
+
num_layers: 16
|
| 8 |
+
num_heads: 8
|
| 9 |
+
max_seq_length: 8192
|
| 10 |
+
|
| 11 |
+
d_latent_kv: 768
|
| 12 |
+
d_rope: 128
|
| 13 |
+
hidden_dim_ff: 3072
|
| 14 |
+
dropout: 0.02
|
| 15 |
+
gqa_groups: 1
|
| 16 |
+
use_factorized_embedding: true
|
| 17 |
+
d_embed_rank: 96
|
| 18 |
+
init_std: 0.02
|
| 19 |
+
|
| 20 |
+
rope_scale: 40.0
|
| 21 |
+
yarn_enabled: true
|
| 22 |
+
yarn_original_max_seq_length: 4096
|
| 23 |
+
yarn_alpha: 1.0
|
| 24 |
+
|
| 25 |
+
dataset:
|
| 26 |
+
local: true
|
| 27 |
+
jsonl_path: /home/student/Data/TaoData/pretrain.jsonl
|
| 28 |
+
text_field: text
|
| 29 |
+
max_samples: 800000
|
| 30 |
+
samples_per_chunk: 1000
|
| 31 |
+
|
| 32 |
+
tokenizer_type: sentencepiece
|
| 33 |
+
tokenizer_path: tokenizer/tokenizer.model
|
| 34 |
+
tokenizer_threads: 4
|
| 35 |
+
|
| 36 |
+
checkpoint_path: checkpoints/yarn4k/best_model.pt
|
| 37 |
+
|
| 38 |
+
batch_size: 2
|
| 39 |
+
num_epochs: 1
|
| 40 |
+
gradient_accumulation_steps: 32
|
| 41 |
+
max_grad_norm: 1.0
|
| 42 |
+
|
| 43 |
+
optimizer:
|
| 44 |
+
optimizer_type: hybrid_muon_adamw
|
| 45 |
+
learning_rate: 7.5e-4
|
| 46 |
+
adamw_lr: 7.5e-5
|
| 47 |
+
weight_decay: 0.01
|
| 48 |
+
betas: [0.9, 0.999]
|
| 49 |
+
eps: 1e-8
|
| 50 |
+
|
| 51 |
+
scheduler:
|
| 52 |
+
scheduler_type: cosineWarmup
|
| 53 |
+
warmup_steps: 300
|
| 54 |
+
warmup_ratio: 0.0
|
| 55 |
+
steady_ratio: 0.05
|
| 56 |
+
min_lr_ratio: 0.1
|
| 57 |
+
num_cycles: 0.5
|
| 58 |
+
|
| 59 |
+
dtype: bfloat16
|
| 60 |
+
device: cuda
|
| 61 |
+
|
| 62 |
+
checkpoint_dir: checkpoints/yarn8k
|
| 63 |
+
save_every_steps: 2048
|
| 64 |
+
save_best_model: true
|
| 65 |
+
keep_last_n_checkpoints: 3
|
| 66 |
+
|
| 67 |
+
eval_every_steps: 2048
|
| 68 |
+
eval_samples: 2000
|
| 69 |
+
|
| 70 |
+
log_every_steps: 50
|
| 71 |
+
aim_repo: .aim
|
| 72 |
+
|
| 73 |
+
seed: 42
|
| 74 |
+
num_workers: 0
|
| 75 |
+
pin_memory: true
|
configs/yarn_pretrain.yaml
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# TaoNet T2 Configuration for YaRN Continued Pretraining
|
| 2 |
+
# Extended Context: 1024 → 8192 tokens with frequency interpolation
|
| 3 |
+
# Built on DeepSeek MLA + RoPE with Hybrid Muon+AdamW Optimizer
|
| 4 |
+
# Full BF16 precision (no quantization)
|
| 5 |
+
|
| 6 |
+
# ============================================================================
|
| 7 |
+
# Model Architecture - TaoNet (DeepSeek MLA + RoPE) with YaRN Extension
|
| 8 |
+
# ============================================================================
|
| 9 |
+
model:
|
| 10 |
+
architecture_type: taonet
|
| 11 |
+
vocab_size: 8192
|
| 12 |
+
hidden_dim: 512
|
| 13 |
+
num_layers: 12
|
| 14 |
+
num_heads: 8
|
| 15 |
+
max_seq_length: 8192 # Extended from 1024 → 8192 (8x longer context)
|
| 16 |
+
|
| 17 |
+
# TaoNet-specific: Multi-head Latent Attention (MLA) compression
|
| 18 |
+
# KV dimension reduced from 512 to 384 (25% memory savings)
|
| 19 |
+
d_latent_kv: 384
|
| 20 |
+
|
| 21 |
+
# RoPE (Rotary Position Embedding) dimension per head
|
| 22 |
+
# Default would be 512 / 8 = 64
|
| 23 |
+
d_rope: 64
|
| 24 |
+
|
| 25 |
+
# Feed-forward intermediate dimension
|
| 26 |
+
# Default would be 4 * 512 = 2048
|
| 27 |
+
hidden_dim_ff: 1024
|
| 28 |
+
|
| 29 |
+
# Dropout rate (low for stability)
|
| 30 |
+
dropout: 0.02
|
| 31 |
+
|
| 32 |
+
# Grouped Query Attention (1 = standard MLA, >1 = GQA)
|
| 33 |
+
gqa_groups: 1
|
| 34 |
+
|
| 35 |
+
# Optional: Use factorized embedding for parameter efficiency
|
| 36 |
+
use_factorized_embedding: false
|
| 37 |
+
d_embed_rank: 96
|
| 38 |
+
|
| 39 |
+
# Weight initialization standard deviation
|
| 40 |
+
init_std: 0.02
|
| 41 |
+
|
| 42 |
+
# ========================================================================
|
| 43 |
+
# YaRN (Yet another RoPE eXtension) Configuration
|
| 44 |
+
# ========================================================================
|
| 45 |
+
# Enables frequency interpolation to extend context length from 1024 → 8192
|
| 46 |
+
# The model learns to "pack" RoPE frequencies into the new longer context during training.
|
| 47 |
+
|
| 48 |
+
# RoPE base scale factor (explicit, previously hardcoded to 40)
|
| 49 |
+
rope_scale: 40.0
|
| 50 |
+
|
| 51 |
+
# Enable YaRN frequency interpolation
|
| 52 |
+
yarn_enabled: true
|
| 53 |
+
|
| 54 |
+
# Interpolation smoothness parameter
|
| 55 |
+
# - 1.0 (default): Smooth, gradual interpolation—safer for learning extended context
|
| 56 |
+
# - 0.5: Aggressive interpolation—faster context expansion, higher risk
|
| 57 |
+
# - 2.0: Conservative interpolation—safer but slower adaptation
|
| 58 |
+
# Recommendation: Start with 1.0; tune in follow-up runs if convergence issues
|
| 59 |
+
yarn_alpha: 1.0
|
| 60 |
+
|
| 61 |
+
# ============================================================================
|
| 62 |
+
# Dataset Configuration - Local JSONL (Same as Pretrain)
|
| 63 |
+
# ============================================================================
|
| 64 |
+
dataset:
|
| 65 |
+
local: true
|
| 66 |
+
jsonl_path: /home/student/Data/TaoData/output.jsonl
|
| 67 |
+
text_field: text
|
| 68 |
+
max_samples: 50000 # Reduced from 1M → 50k for quick YaRN adaptation
|
| 69 |
+
samples_per_chunk: 1000
|
| 70 |
+
|
| 71 |
+
# Tokenizer configuration (unchanged)
|
| 72 |
+
tokenizer_type: sentencepiece
|
| 73 |
+
tokenizer_path: tokenizer/tokenizer.model
|
| 74 |
+
tokenizer_threads: 4
|
| 75 |
+
|
| 76 |
+
# ============================================================================
|
| 77 |
+
# Training Hyperparameters - Conservative for Context Extension
|
| 78 |
+
# ============================================================================
|
| 79 |
+
# Strategy: Lower learning rates + smaller batch to prevent catastrophic forgetting
|
| 80 |
+
# while the model learns to use 8x longer context.
|
| 81 |
+
|
| 82 |
+
batch_size: 16 # Reduced from 32 (8192 tokens/seq is memory-intensive)
|
| 83 |
+
num_epochs: 1 # 50k samples / effective_batch=256 ≈ 200 updates (1 epoch sufficient for warm-start)
|
| 84 |
+
|
| 85 |
+
# Gradient accumulation to maintain effective batch size of ~256
|
| 86 |
+
# Effective batch = batch_size × gradient_accumulation_steps = 16 × 16 = 256
|
| 87 |
+
gradient_accumulation_steps: 16
|
| 88 |
+
|
| 89 |
+
# Maximum gradient norm for clipping
|
| 90 |
+
max_grad_norm: 1.0
|
| 91 |
+
|
| 92 |
+
# ============================================================================
|
| 93 |
+
# Optimizer - Hybrid Muon + AdamW (Conservative LR for Stability)
|
| 94 |
+
# ============================================================================
|
| 95 |
+
# Strategy: Use 1/2 of pretrain learning rates to:
|
| 96 |
+
# 1. Avoid catastrophic forgetting of learned features
|
| 97 |
+
# 2. Allow smooth adaptation to YaRN-scaled RoPE frequencies
|
| 98 |
+
# 3. Give the model time to learn how to use extended context
|
| 99 |
+
|
| 100 |
+
optimizer:
|
| 101 |
+
optimizer_type: hybrid_muon_adamw
|
| 102 |
+
|
| 103 |
+
# Learning rate for Muon (2D weight matrices)
|
| 104 |
+
# Reduced: 5e-3 → 2.5e-3 (50% of pretrain)
|
| 105 |
+
learning_rate: 2.5e-3
|
| 106 |
+
|
| 107 |
+
# Learning rate for AdamW (1D parameters)
|
| 108 |
+
# Reduced: 5e-4 → 1.25e-4 (25% of pretrain, maintains 1/10 ratio)
|
| 109 |
+
adamw_lr: 1.25e-4
|
| 110 |
+
|
| 111 |
+
# L2 regularization (weight decay)
|
| 112 |
+
weight_decay: 0.01
|
| 113 |
+
|
| 114 |
+
# Adam betas (unchanged)
|
| 115 |
+
betas: [0.9, 0.999]
|
| 116 |
+
|
| 117 |
+
# Epsilon for numerical stability
|
| 118 |
+
eps: 1e-8
|
| 119 |
+
|
| 120 |
+
# ============================================================================
|
| 121 |
+
# Learning Rate Scheduler - 3-Phase Cosine with Warmup (Same as Pretrain)
|
| 122 |
+
# ============================================================================
|
| 123 |
+
# Phases:
|
| 124 |
+
# 1. Warmup: 0 → 1.0 (300 steps, ~1.4% of training)
|
| 125 |
+
# 2. Steady: 1.0 (constant for 5% of training steps at peak LR)
|
| 126 |
+
# 3. Decay: 1.0 → 0.1 (cosine decay for remaining ~95%)
|
| 127 |
+
|
| 128 |
+
scheduler:
|
| 129 |
+
scheduler_type: cosineWarmup
|
| 130 |
+
warmup_steps: 300
|
| 131 |
+
warmup_ratio: 0.0 # Ignored if warmup_steps > 0
|
| 132 |
+
steady_ratio: 0.05 # 5% of total training steps at peak LR
|
| 133 |
+
min_lr_ratio: 0.1 # Decay to 10% of peak LR
|
| 134 |
+
num_cycles: 0.5 # For compatibility (not used in 3-phase schedule)
|
| 135 |
+
|
| 136 |
+
# ============================================================================
|
| 137 |
+
# Data Type and Device
|
| 138 |
+
# ============================================================================
|
| 139 |
+
dtype: bfloat16 # Use BF16 for better convergence with extended context
|
| 140 |
+
device: cuda # Use GPU for training
|
| 141 |
+
|
| 142 |
+
# ============================================================================
|
| 143 |
+
# Checkpointing and Validation
|
| 144 |
+
# ============================================================================
|
| 145 |
+
# Load pretrained checkpoint and continue training
|
| 146 |
+
checkpoint_path: checkpoints/pretrain_taonet/best_model.pt
|
| 147 |
+
checkpoint_dir: checkpoints/yarn_taonet
|
| 148 |
+
save_every_steps: 512 # More frequent saves for 50k samples (200 updates total)
|
| 149 |
+
save_best_model: true
|
| 150 |
+
keep_last_n_checkpoints: 3
|
| 151 |
+
|
| 152 |
+
# Validation every 512 steps (10% of 50k samples)
|
| 153 |
+
eval_every_steps: 512
|
| 154 |
+
eval_samples: 2500 # Reduced from 8000
|
| 155 |
+
|
| 156 |
+
# ============================================================================
|
| 157 |
+
# Logging
|
| 158 |
+
# ============================================================================
|
| 159 |
+
log_every_steps: 50 # Log every 50 updates
|
| 160 |
+
aim_repo: .aim
|
| 161 |
+
|
| 162 |
+
# ============================================================================
|
| 163 |
+
# Miscellaneous
|
| 164 |
+
# ============================================================================
|
| 165 |
+
seed: 42
|
| 166 |
+
num_workers: 0
|
| 167 |
+
pin_memory: true
|
| 168 |
+
|
| 169 |
+
# ============================================================================
|
| 170 |
+
# YaRN Performance Notes
|
| 171 |
+
# ============================================================================
|
| 172 |
+
# Expected memory usage: ~1.5x of pretrain (8x longer seq, half batch)
|
| 173 |
+
# Expected training time: ~50-100 steps/min on H100 (depends on setup)
|
| 174 |
+
# Expected convergence: Loss should decrease over 50k samples; monitor perplexity on 8192-length sequences
|
| 175 |
+
#
|
| 176 |
+
# Tuning recommendations for iterative improvements:
|
| 177 |
+
# 1. If loss is unstable: Reduce learning_rate further (1.25e-3)
|
| 178 |
+
# 2. If loss plateaus quickly: Increase max_samples (100k-200k)
|
| 179 |
+
# 3. If memory OOM: Reduce batch_size to 8 (maintain grad_accum at 16)
|
| 180 |
+
# 4. To speed context expansion: Reduce yarn_alpha to 0.5 (more aggressive)
|
| 181 |
+
# 5. For safer training: Increase yarn_alpha to 2.0 (more conservative)
|
tokenization_taonet.py
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
| 5 |
import shutil
|
| 6 |
|
| 7 |
from transformers import PreTrainedTokenizer
|
|
@@ -43,6 +44,7 @@ class TaoNetTokenizer(PreTrainedTokenizer):
|
|
| 43 |
str(token): int(token_id) for token, token_id in metadata.get("special_tokens", {}).items()
|
| 44 |
}
|
| 45 |
configured_special_tokens = [str(token) for token in metadata.get("configured_special_tokens", [])]
|
|
|
|
| 46 |
self.id_to_special_token = {
|
| 47 |
int(token_id): str(token) for token, token_id in self.special_token_ids.items()
|
| 48 |
}
|
|
@@ -74,6 +76,35 @@ class TaoNetTokenizer(PreTrainedTokenizer):
|
|
| 74 |
def _tokenize(self, text):
|
| 75 |
return list(self.sp_model.encode(text, out_type=str))
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
def _convert_token_to_id(self, token):
|
| 78 |
if token in self.special_token_ids:
|
| 79 |
return self.special_token_ids[token]
|
|
@@ -99,6 +130,86 @@ class TaoNetTokenizer(PreTrainedTokenizer):
|
|
| 99 |
return list(token_ids_0)
|
| 100 |
return list(token_ids_0) + list(token_ids_1)
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
def save_vocabulary(self, save_directory, filename_prefix=None):
|
| 103 |
if not os.path.isdir(save_directory):
|
| 104 |
raise ValueError(f"Vocabulary path should be a directory, got: {save_directory}")
|
|
|
|
| 2 |
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
+
import re
|
| 6 |
import shutil
|
| 7 |
|
| 8 |
from transformers import PreTrainedTokenizer
|
|
|
|
| 44 |
str(token): int(token_id) for token, token_id in metadata.get("special_tokens", {}).items()
|
| 45 |
}
|
| 46 |
configured_special_tokens = [str(token) for token in metadata.get("configured_special_tokens", [])]
|
| 47 |
+
self.configured_special_tokens = list(configured_special_tokens)
|
| 48 |
self.id_to_special_token = {
|
| 49 |
int(token_id): str(token) for token, token_id in self.special_token_ids.items()
|
| 50 |
}
|
|
|
|
| 76 |
def _tokenize(self, text):
|
| 77 |
return list(self.sp_model.encode(text, out_type=str))
|
| 78 |
|
| 79 |
+
def get_special_token_id(self, token):
|
| 80 |
+
return self.special_token_ids.get(token)
|
| 81 |
+
|
| 82 |
+
def _encode_with_registered_special_tokens(self, text):
|
| 83 |
+
if not text:
|
| 84 |
+
return []
|
| 85 |
+
|
| 86 |
+
special_tokens = [
|
| 87 |
+
token
|
| 88 |
+
for token in self.configured_special_tokens
|
| 89 |
+
if token and token in text
|
| 90 |
+
]
|
| 91 |
+
if not special_tokens:
|
| 92 |
+
return list(self.sp_model.encode(text, out_type=int))
|
| 93 |
+
|
| 94 |
+
pattern = "(" + "|".join(re.escape(token) for token in sorted(special_tokens, key=len, reverse=True)) + ")"
|
| 95 |
+
parts = re.split(pattern, text)
|
| 96 |
+
|
| 97 |
+
encoded = []
|
| 98 |
+
for part in parts:
|
| 99 |
+
if not part:
|
| 100 |
+
continue
|
| 101 |
+
special_token_id = self.special_token_ids.get(part)
|
| 102 |
+
if special_token_id is not None:
|
| 103 |
+
encoded.append(int(special_token_id))
|
| 104 |
+
else:
|
| 105 |
+
encoded.extend(self.sp_model.encode(part, out_type=int))
|
| 106 |
+
return encoded
|
| 107 |
+
|
| 108 |
def _convert_token_to_id(self, token):
|
| 109 |
if token in self.special_token_ids:
|
| 110 |
return self.special_token_ids[token]
|
|
|
|
| 130 |
return list(token_ids_0)
|
| 131 |
return list(token_ids_0) + list(token_ids_1)
|
| 132 |
|
| 133 |
+
def encode(self, text, return_tensors=None, **kwargs):
|
| 134 |
+
import torch
|
| 135 |
+
|
| 136 |
+
add_special_tokens = kwargs.pop("add_special_tokens", True)
|
| 137 |
+
del add_special_tokens
|
| 138 |
+
|
| 139 |
+
input_ids = self._encode_with_registered_special_tokens(text)
|
| 140 |
+
if return_tensors == "pt":
|
| 141 |
+
return torch.tensor(input_ids, dtype=torch.long).unsqueeze(0)
|
| 142 |
+
return input_ids
|
| 143 |
+
|
| 144 |
+
def __call__(self, text, return_tensors=None, **kwargs):
|
| 145 |
+
import torch
|
| 146 |
+
|
| 147 |
+
add_special_tokens = kwargs.pop("add_special_tokens", True)
|
| 148 |
+
del add_special_tokens
|
| 149 |
+
|
| 150 |
+
is_single = isinstance(text, str)
|
| 151 |
+
texts = [text] if is_single else list(text)
|
| 152 |
+
encoded_batch = [self._encode_with_registered_special_tokens(item) for item in texts]
|
| 153 |
+
|
| 154 |
+
padding = kwargs.pop("padding", False)
|
| 155 |
+
truncation = kwargs.pop("truncation", False)
|
| 156 |
+
max_length = kwargs.pop("max_length", None)
|
| 157 |
+
return_attention_mask = kwargs.pop("return_attention_mask", True)
|
| 158 |
+
|
| 159 |
+
if truncation and max_length is not None:
|
| 160 |
+
encoded_batch = [ids[:max_length] for ids in encoded_batch]
|
| 161 |
+
|
| 162 |
+
if padding == "max_length" and max_length is None:
|
| 163 |
+
raise ValueError("max_length must be set when padding='max_length'")
|
| 164 |
+
if padding:
|
| 165 |
+
target_length = max_length if max_length is not None else max(len(ids) for ids in encoded_batch)
|
| 166 |
+
padded_batch = []
|
| 167 |
+
attention_masks = []
|
| 168 |
+
for ids in encoded_batch:
|
| 169 |
+
trimmed = ids[:target_length]
|
| 170 |
+
pad_len = target_length - len(trimmed)
|
| 171 |
+
padded_batch.append(trimmed + [self.pad_token_id] * pad_len)
|
| 172 |
+
attention_masks.append([1] * len(trimmed) + [0] * pad_len)
|
| 173 |
+
else:
|
| 174 |
+
padded_batch = encoded_batch
|
| 175 |
+
attention_masks = [[1] * len(ids) for ids in encoded_batch]
|
| 176 |
+
|
| 177 |
+
if return_tensors == "pt":
|
| 178 |
+
if not padding and len({len(ids) for ids in padded_batch}) > 1:
|
| 179 |
+
raise ValueError("Batch elements must have the same length when return_tensors='pt' without padding")
|
| 180 |
+
input_ids = torch.tensor(padded_batch, dtype=torch.long)
|
| 181 |
+
result = {"input_ids": input_ids}
|
| 182 |
+
if return_attention_mask:
|
| 183 |
+
result["attention_mask"] = torch.tensor(attention_masks, dtype=torch.long)
|
| 184 |
+
if is_single:
|
| 185 |
+
result = {key: value for key, value in result.items()}
|
| 186 |
+
return result
|
| 187 |
+
|
| 188 |
+
result = {"input_ids": padded_batch[0] if is_single else padded_batch}
|
| 189 |
+
if return_attention_mask:
|
| 190 |
+
result["attention_mask"] = attention_masks[0] if is_single else attention_masks
|
| 191 |
+
return result
|
| 192 |
+
|
| 193 |
+
def build_chat_inputs(self, prompt, return_tensors=None):
|
| 194 |
+
import torch
|
| 195 |
+
|
| 196 |
+
user_token_id = self.special_token_ids["<user>"]
|
| 197 |
+
assistant_token_id = self.special_token_ids["<assistant>"]
|
| 198 |
+
prompt_ids = self._encode_with_registered_special_tokens(prompt)
|
| 199 |
+
input_ids = [user_token_id, *prompt_ids, assistant_token_id]
|
| 200 |
+
attention_mask = [1] * len(input_ids)
|
| 201 |
+
|
| 202 |
+
if return_tensors == "pt":
|
| 203 |
+
return {
|
| 204 |
+
"input_ids": torch.tensor(input_ids, dtype=torch.long).unsqueeze(0),
|
| 205 |
+
"attention_mask": torch.tensor(attention_mask, dtype=torch.long).unsqueeze(0),
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
return {
|
| 209 |
+
"input_ids": input_ids,
|
| 210 |
+
"attention_mask": attention_mask,
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
def save_vocabulary(self, save_directory, filename_prefix=None):
|
| 214 |
if not os.path.isdir(save_directory):
|
| 215 |
raise ValueError(f"Vocabulary path should be a directory, got: {save_directory}")
|