Update with corrected usage guide and working model
Browse filesFixed quantized model loading and added comprehensive usage instructions
README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
| 5 |
This is a **blazing-fast, CPU-optimized Small Language Model** that achieves unprecedented speed and efficiency:
|
| 6 |
|
| 7 |
### ⚡ Performance Highlights
|
| 8 |
-
- **
|
| 9 |
- **3.7MB model size** (76.6% smaller than original)
|
| 10 |
- **3.7M parameters** (tiny but powerful)
|
| 11 |
- **Q&A specialized** (learned conversation patterns)
|
|
@@ -29,22 +29,80 @@ This is a **blazing-fast, CPU-optimized Small Language Model** that achieves unp
|
|
| 29 |
|
| 30 |
## Usage
|
| 31 |
|
|
|
|
|
|
|
| 32 |
```python
|
|
|
|
| 33 |
import torch
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
# Load
|
| 37 |
-
|
| 38 |
-
tokenizer
|
| 39 |
|
| 40 |
-
# Generate
|
| 41 |
prompt = "Question: How are you? Answer:"
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
print(response)
|
| 46 |
```
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
## Model Details
|
| 49 |
|
| 50 |
- **Base Model:** Trained on conversational data
|
|
@@ -56,7 +114,7 @@ print(response)
|
|
| 56 |
|
| 57 |
| Model | Speed (tokens/sec) | Size | Training Time |
|
| 58 |
|-------|-------------------|------|---------------|
|
| 59 |
-
|
|
| 60 |
-
| **
|
| 61 |
|
| 62 |
This model represents a breakthrough in CPU-optimized language models, making conversational AI accessible on any device without requiring specialized hardware.
|
|
|
|
| 5 |
This is a **blazing-fast, CPU-optimized Small Language Model** that achieves unprecedented speed and efficiency:
|
| 6 |
|
| 7 |
### ⚡ Performance Highlights
|
| 8 |
+
- **893 tokens/sec** on CPU (fast production speed)
|
| 9 |
- **3.7MB model size** (76.6% smaller than original)
|
| 10 |
- **3.7M parameters** (tiny but powerful)
|
| 11 |
- **Q&A specialized** (learned conversation patterns)
|
|
|
|
| 29 |
|
| 30 |
## Usage
|
| 31 |
|
| 32 |
+
### Quick Start
|
| 33 |
+
|
| 34 |
```python
|
| 35 |
+
from huggingface_hub import hf_hub_download
|
| 36 |
import torch
|
| 37 |
+
import sys
|
| 38 |
+
sys.path.append('src') # Add your model code path
|
| 39 |
+
from model import create_model_from_config
|
| 40 |
+
from tokenizer import BPETokenizer
|
| 41 |
+
from quantize import QuantizedModel
|
| 42 |
+
|
| 43 |
+
# Download model files
|
| 44 |
+
model_path = hf_hub_download(repo_id="Rahulwale12/SLM", filename="pytorch_model.bin")
|
| 45 |
+
config_path = hf_hub_download(repo_id="Rahulwale12/SLM", filename="config.json")
|
| 46 |
+
tokenizer_path = hf_hub_download(repo_id="Rahulwale12/SLM", filename="tokenizer.json")
|
| 47 |
+
|
| 48 |
+
# Load config
|
| 49 |
+
import json
|
| 50 |
+
with open(config_path, 'r') as f:
|
| 51 |
+
config = json.load(f)
|
| 52 |
+
|
| 53 |
+
# Create model
|
| 54 |
+
model_config = {
|
| 55 |
+
'model': {
|
| 56 |
+
'vocab_size': config['vocab_size'],
|
| 57 |
+
'd_model': config['hidden_size'],
|
| 58 |
+
'n_layers': config['num_hidden_layers'],
|
| 59 |
+
'n_heads': config['num_attention_heads'],
|
| 60 |
+
'd_ff': config['intermediate_size'],
|
| 61 |
+
'seq_len': config['max_position_embeddings'],
|
| 62 |
+
'dropout': 0.1,
|
| 63 |
+
'use_rmsnorm': True,
|
| 64 |
+
'use_rotary': True,
|
| 65 |
+
'use_swiglu': True
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
model = create_model_from_config({'model': model_config['model']})
|
| 70 |
+
|
| 71 |
+
# Load quantized weights
|
| 72 |
+
checkpoint = torch.load(model_path, map_location='cpu')
|
| 73 |
+
quantized_model = QuantizedModel(model, checkpoint['quantization_bits'])
|
| 74 |
+
quantized_model.quantized_weights = checkpoint['quantized_weights']
|
| 75 |
+
quantized_model.scales = checkpoint['scales']
|
| 76 |
+
quantized_model.zeros = checkpoint['zeros']
|
| 77 |
+
quantized_model.dequantize_weights()
|
| 78 |
|
| 79 |
+
# Load tokenizer
|
| 80 |
+
tokenizer = BPETokenizer()
|
| 81 |
+
tokenizer.load(tokenizer_path)
|
| 82 |
|
| 83 |
+
# Generate text
|
| 84 |
prompt = "Question: How are you? Answer:"
|
| 85 |
+
input_ids = tokenizer.encode(prompt, add_special_tokens=True)
|
| 86 |
+
input_ids = torch.tensor([input_ids], dtype=torch.long)
|
| 87 |
+
|
| 88 |
+
model.eval()
|
| 89 |
+
with torch.no_grad():
|
| 90 |
+
for _ in range(20):
|
| 91 |
+
logits = model(input_ids)[0, -1, :]
|
| 92 |
+
next_token = torch.argmax(logits, dim=-1).unsqueeze(0)
|
| 93 |
+
input_ids = torch.cat([input_ids, next_token.unsqueeze(0)], dim=1)
|
| 94 |
+
|
| 95 |
+
response = tokenizer.decode(input_ids[0].tolist(), skip_special_tokens=True)
|
| 96 |
print(response)
|
| 97 |
```
|
| 98 |
|
| 99 |
+
### Complete Usage Guide
|
| 100 |
+
|
| 101 |
+
Run the comprehensive usage guide:
|
| 102 |
+
```bash
|
| 103 |
+
python usage_guide.py
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
## Model Details
|
| 107 |
|
| 108 |
- **Base Model:** Trained on conversational data
|
|
|
|
| 114 |
|
| 115 |
| Model | Speed (tokens/sec) | Size | Training Time |
|
| 116 |
|-------|-------------------|------|---------------|
|
| 117 |
+
| Base | 942 | 45.2MB | 28 min |
|
| 118 |
+
| **Fine-tuned** | **893** | **3.7MB** | **2.35 min** |
|
| 119 |
|
| 120 |
This model represents a breakthrough in CPU-optimized language models, making conversational AI accessible on any device without requiring specialized hardware.
|