Views
No views yet

| Metric | Initial | Final | Improvement |
|---|---|---|---|
| Loss | 3.760 | 0.772 | ✅ -79.46% |
| Perplexity | 42.85 | 2.16 | ✅ -94.95% |
| Accuracy | ~5% | ~95% | ✅ +90% |
Input: "666 * 618 = "
Output: "411588" ✓
Input: "123 * 456 = "
Output: "56088" ✓
Input: "789 * 321 = "
Output: "253269" ✓pip install torch numpy1import torch
2import os
3
4# Download model
5# model_path = "path/to/rwkv-final.pth"
6
7# Set environment
8os.environ["RWKV_MY_TESTING"] = "x070"
9os.environ["RWKV_CTXLEN"] = "512"
10os.environ["RWKV_HEAD_SIZE"] = "64"
11
12# Load model (simplified - see full usage below)
13model = torch.load("rwkv-final.pth", map_location="cpu")
14print(f"Model loaded: {sum(p.numel() for p in model.values())/1e6:.1f}M parameters")1import os
2import sys
3import torch
4import torch.nn.functional as F
5
6# Setup paths (adjust to your setup)
7sys.path.insert(0, 'path/to/RWKV-LM/finetune')
8
9from src.model import RWKV
10from tokenizer.rwkv_tokenizer import RWKV_TOKENIZER
11
12# Environment setup
13os.environ["RWKV_MY_TESTING"] = "x070"
14os.environ["RWKV_CTXLEN"] = "512"
15os.environ["RWKV_HEAD_SIZE"] = "64"
16os.environ["RWKV_FLOAT_MODE"] = "bf16"
17
18# Model configuration
19class ModelArgs:
20 n_layer = 12
21 n_embd = 768
22 vocab_size = 65536
23 ctx_len = 512
24 head_size = 64
25 dim_att = 768
26 dim_ffn = 2688 # 3.5x of n_embd
27 my_testing = 'x070'
28
29# Initialize model
30args = ModelArgs()
31model = RWKV(args)
32
33# Load weights
34checkpoint = torch.load('rwkv-final.pth', map_location='cpu', weights_only=False)
35model.load_state_dict(checkpoint, strict=False)
36model.eval()
37
38# Initialize tokenizer
39tokenizer = RWKV_TOKENIZER("path/to/rwkv_vocab_v20230424.txt")
40
41# Inference function
42def generate(prompt, max_length=100, temperature=1.0, top_p=0.9):
43 tokens = tokenizer.encode(prompt)
44 state = None
45
46 with torch.no_grad():
47 for i in range(max_length):
48 x = torch.tensor([tokens[-1]], dtype=torch.long)
49 out, state = model.forward(x, state)
50
51 # Sample next token
52 probs = F.softmax(out[0] / temperature, dim=-1)
53
54 # Top-p sampling
55 sorted_probs, sorted_indices = torch.sort(probs, descending=True)
56 cumsum_probs = torch.cumsum(sorted_probs, dim=-1)
57 cutoff_index = torch.searchsorted(cumsum_probs, top_p)
58
59 probs[sorted_indices[cutoff_index + 1:]] = 0
60 probs = probs / probs.sum()
61
62 next_token = torch.multinomial(probs, num_samples=1).item()
63 tokens.append(next_token)
64
65 # Stop if answer complete
66 decoded = tokenizer.decode(tokens)
67 if "</answer>" in decoded:
68 break
69
70 return tokenizer.decode(tokens)
71
72# Example usage
73prompt = "User: Give me the answer of the following equation: 123 * 456 = Assistant: Ok let me think about it.\n<think>"
74
75result = generate(prompt, max_length=200, temperature=0.8)
76print(result)User: Give me the answer of the following equation: 123 * 456 =
Assistant: Ok let me think about it.
<think>
Let me calculate 123 * 456 step by step...
123 * 400 = 49200
123 * 50 = 6150
123 * 6 = 738
Adding them: 49200 + 6150 + 738 = 56088
</think>
<answer>56088</answer><think> and <answer> tags1Hardware:
2 - GPUs: 2x NVIDIA RTX 4090 (24GB VRAM each)
3 - Strategy: DeepSpeed Stage 2
4 - Precision: BFloat16
5
6Hyperparameters:
7 - Learning Rate: 1e-5 → 1e-6 (cosine decay)
8 - Batch Size: 16 (8 per GPU × 2 GPUs)
9 - Epochs: 10
10 - Context Length: 512 tokens
11 - Optimizer: Adam (β1=0.9, β2=0.99, ε=1e-18)
12 - Weight Decay: 0.001
13 - Gradient Clipping: 1.0
14 - Warmup Steps: 10
15 - Gradient Checkpointing: Enabled
16
17Data Augmentation:
18 - Training data duplicated 5x (for better convergence)
19 - Validation data: no duplication| Metric | Value | Description |
|---|---|---|
| Final Loss | 0.772 | Cross-entropy loss on validation set |
| Perplexity | 2.16 | Indicates high confidence in predictions |
| Token Accuracy | ~95% | Percentage of correct digits generated |
| Exact Match | ~90%* | Percentage of completely correct answers |
1@misc{rwkv7-math-multiply-2025,
2 title={RWKV-7 0.1B Fine-tuned for 3-Digit Multiplication},
3 author={Duc Minh},
4 year={2025},
5 howpublished={\url{https://huggingface.co/CommerAI/rwkv-7-goose-arithmetic-multiplication}},
6}1@article{peng2023rwkv,
2 title={RWKV: Reinventing RNNs for the Transformer Era},
3 author={Peng, Bo and others},
4 journal={arXiv preprint arXiv:2305.13048},
5 year={2023}
6}