Views
No views yet
gpt2-medium model.transformers and torch libraries. No custom or proprietary layers are required to perform inference.1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from huggingface_hub import hf_hub_download
4
5# 1. Download the quantized weights
6weights_path = hf_hub_download(repo_id="dadni/GPT2-Medium-2Bit-Demo", filename="gpt2-2bit-matmulfree.pt")
7
8# 2. Load the base architecture and tokenizer
9model_id = "gpt2-medium"
10model = AutoModelForCausalLM.from_pretrained(model_id)
11tokenizer = AutoTokenizer.from_pretrained(model_id)
12
13# 3. Load the 2-bit weights into the model
14state_dict = torch.load(weights_path, map_location="cpu")
15model.load_state_dict(state_dict)
16
17model.eval()
18
19# 4. Generate Text
20prompt = "The true secret to human intelligence is"
21input_ids = tokenizer.encode(prompt, return_tensors="pt")
22
23with torch.no_grad():
24 output_ids = model.generate(
25 input_ids,
26 max_new_tokens=100,
27 do_sample=True,
28 top_p=0.9,
29 temperature=0.8,
30 repetition_penalty=1.3,
31 pad_token_id=tokenizer.eos_token_id
32 )
33
34print(tokenizer.decode(output_ids[0], skip_special_tokens=True))