Views
No views yet
1pip install torch tiktoken huggingface_hub
2python gpt2_slm_instruct_inference.pyask() in your own code1# Import loads the model automatically (one-time download from HuggingFace)
2from gpt2_slm_instruct_inference import ask
3
4# Simple question
5print(ask("What is the capital of France?"))
6print()
7
8# With input context
9print(ask(
10 instruction="Summarize the following text.",
11 input_text="Machine learning enables systems to learn from data rather than being explicitly programmed."
12))
13print()
14
15# Control generation
16print(ask(
17 "Write a short poem about the ocean.",
18 temperature=1.0, # higher = more creative
19 top_k=100, # wider sampling pool
20 max_tokens=150 # longer output
21))
22print()1from huggingface_hub import hf_hub_download
2import torch
3
4model_path = hf_hub_download(
5 repo_id="nishantup/gpt2-slm-instruct",
6 filename="gpt2_slm_instruct.pth"
7)
8
9from gpt2_slm_instruct_inference import GPTModel, BASE_CONFIG
10
11model = GPTModel(BASE_CONFIG)
12model.load_state_dict(torch.load(model_path, map_location="cpu"))
13model.eval()Below is an instruction that describes a task.
### Instruction:
{instruction}
### Response:Below is an instruction that describes a task, paired with further context.
### Instruction:
{instruction}
### Input:
{input}
### Response:| Attribute | Value |
|---|---|
| Parameters | 163.2M |
| Architecture | Raschka GPTModel (12 layers, 12 heads, 768 dim) |
| Context length | 256 tokens |
| Tokenizer | tiktoken GPT-2 BPE (50,257 tokens) |
| Base model | nishantup/nanogpt-slm-124m (gpt_slm_best.pth) |
| Fine-tuning | Supervised (Alpaca format, 1,100 examples, 2 epochs) |
| Framework | PyTorch |
| Feature | This model (Raschka) | nanoGPT variant |
|---|---|---|
| Weights file | gpt2_slm_instruct.pth | nanogpt_slm_instruct.pth |
| Attention | Separate W_query, W_key, W_value | Combined c_attn |
| LayerNorm | scale/shift params | weight/bias params |
| MLP | FeedForward (Sequential) | MLP (c_fc/c_proj) |
| Config | Dict (BASE_CONFIG) | Dataclass (GPTConfig) |
| Weight tying | No | Yes (wte = lm_head) |
| forward() returns | logits | (logits, loss) tuple |
| File | Description |
|---|---|
gpt2_slm_instruct.pth | SFT fine-tuned weights (Raschka GPTModel) |
gpt2_slm_instruct_inference.py | Standalone inference script -- import and call ask() |
config.json | Model configuration |
ask() API Referenceask(instruction, input_text="", max_tokens=256, temperature=0.7, top_k=40)| Parameter | Default | Description |
|---|---|---|
instruction | (required) | The task instruction |
input_text | "" | Optional additional context |
max_tokens | 256 | Maximum tokens to generate |
temperature | 0.7 | 0.0 = greedy, 0.7 = balanced, 1.5 = creative |
top_k | 40 | Top-k filtering (None = no filtering) |
| Variant | Architecture | Repo |
|---|---|---|
| Pretrained base (Raschka) | GPTModel | nishantup/nanogpt-slm-124m (gpt_slm_best.pth) |
| Pretrained base (nanoGPT) | GPT | nishantup/nanogpt-slm-124m (nanogpt_slm_best.pth) |
| Instruct SFT (nanoGPT) | GPT | nishantup/nanogpt-slm-instruct |