Views
No views yet
1from huggingface_hub import hf_hub_download
2import torch
3import sys
4sys.path.append('src') # Add your model code path
5from model import create_model_from_config
6from tokenizer import BPETokenizer
7from quantize import QuantizedModel
8
9# Download model files
10model_path = hf_hub_download(repo_id="Rahulwale12/SLM", filename="pytorch_model.bin")
11config_path = hf_hub_download(repo_id="Rahulwale12/SLM", filename="config.json")
12tokenizer_path = hf_hub_download(repo_id="Rahulwale12/SLM", filename="tokenizer.json")
13
14# Load config
15import json
16with open(config_path, 'r') as f:
17 config = json.load(f)
18
19# Create model
20model_config = {
21 'model': {
22 'vocab_size': config['vocab_size'],
23 'd_model': config['hidden_size'],
24 'n_layers': config['num_hidden_layers'],
25 'n_heads': config['num_attention_heads'],
26 'd_ff': config['intermediate_size'],
27 'seq_len': config['max_position_embeddings'],
28 'dropout': 0.1,
29 'use_rmsnorm': True,
30 'use_rotary': True,
31 'use_swiglu': True
32 }
33}
34
35model = create_model_from_config({'model': model_config['model']})
36
37# Load quantized weights
38checkpoint = torch.load(model_path, map_location='cpu')
39quantized_model = QuantizedModel(model, checkpoint['quantization_bits'])
40quantized_model.quantized_weights = checkpoint['quantized_weights']
41quantized_model.scales = checkpoint['scales']
42quantized_model.zeros = checkpoint['zeros']
43quantized_model.dequantize_weights()
44
45# Load tokenizer
46tokenizer = BPETokenizer()
47tokenizer.load(tokenizer_path)
48
49# Generate text
50prompt = "Question: How are you? Answer:"
51input_ids = tokenizer.encode(prompt, add_special_tokens=True)
52input_ids = torch.tensor([input_ids], dtype=torch.long)
53
54model.eval()
55with torch.no_grad():
56 for _ in range(20):
57 logits = model(input_ids)[0, -1, :]
58 next_token = torch.argmax(logits, dim=-1).unsqueeze(0)
59 input_ids = torch.cat([input_ids, next_token.unsqueeze(0)], dim=1)
60
61response = tokenizer.decode(input_ids[0].tolist(), skip_special_tokens=True)
62print(response)python usage_guide.py| Model | Speed (tokens/sec) | Size | Training Time |
|---|---|---|---|
| Base | 942 | 45.2MB | 28 min |
| Fine-tuned | 893 | 3.7MB | 2.35 min |