0sparsh2 commited on
Commit
ac4bbf5
·
verified ·
1 Parent(s): f4f6f1c

Upload API_USAGE.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. API_USAGE.md +67 -0
API_USAGE.md ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # API & Inference Usage
2
+
3
+ This guide covers how to load the MiniLM 1.58-bit base model and dynamically snap on custom LoRAs for inference.
4
+
5
+ ## Python Inference (PyTorch)
6
+
7
+ Because MiniLM uses custom ternary BitLinear layers, it cannot be loaded via the standard `transformers` AutoModel pipeline. You must use the provided `model.py` and `lora.py` scripts.
8
+
9
+ ### 1. Loading the Base Model
10
+ ```python
11
+ import torch
12
+ from transformers import AutoTokenizer
13
+ from model import BitGPT
14
+
15
+ device = torch.device('mps' if torch.backends.mps.is_available() else 'cpu')
16
+ tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM-135M-Instruct")
17
+
18
+ # Initialize the 12-Layer Tied Architecture
19
+ model = BitGPT(vocab_size=len(tokenizer), embed_dim=256, num_layers=12, num_heads=4, tie_weights=True).to(device)
20
+
21
+ # Load the frozen 1.58-bit Base Weights
22
+ model.load_state_dict(torch.load("minilm_base.pt", map_location=device))
23
+ model.eval()
24
+ ```
25
+
26
+ ### 2. Injecting a "Side-Car" LoRA
27
+ If you want to run a specific task (like Smart Home JSON extraction), you must wrap the Linear layers with the custom `BitLoraLinear` adapter.
28
+
29
+ ```python
30
+ from lora import inject_lora
31
+
32
+ # Wrap the model's layers with LoRA adapters
33
+ model = inject_lora(model, r=8, lora_alpha=16).to(device)
34
+
35
+ # Snap on the custom 1MB weights (strict=False ensures we only overwrite the new LoRA parameters)
36
+ model.load_state_dict(torch.load("lora_smarthome.pt", map_location=device), strict=False)
37
+ model.eval()
38
+ ```
39
+
40
+ ### 3. Generation Loop
41
+ To generate text, format your prompt using `ChatML` standard tags:
42
+
43
+ ```python
44
+ prompt = "Uh, it's freezing in here, can you turn up the heat in the living room?"
45
+ chatml_text = f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
46
+ input_ids = tokenizer.encode(chatml_text, return_tensors="pt").to(device)
47
+
48
+ max_new_tokens = 60
49
+ with torch.no_grad():
50
+ for _ in range(max_new_tokens):
51
+ logits = model(input_ids)
52
+ next_token_logits = logits[:, -1, :]
53
+
54
+ # Greedy decoding
55
+ next_token = torch.argmax(next_token_logits, dim=-1, keepdim=True)
56
+ input_ids = torch.cat([input_ids, next_token], dim=-1)
57
+
58
+ # Stop condition (2 is im_end in ChatML)
59
+ if next_token.item() == tokenizer.eos_token_id or next_token.item() == 2:
60
+ break
61
+
62
+ output_text = tokenizer.decode(input_ids[0])
63
+ final_output = output_text.split("<|im_start|>assistant\n")[-1].replace("<|im_end|>", "").strip()
64
+
65
+ print(final_output)
66
+ # Output: {"device": "thermostat", "action": "increase_temp", "room": "living_room"}
67
+ ```