Views
No views yet
pip install datasets huggingface_hub matplotlib transformers torch --quiet1from huggingface_hub import hf_hub_download
2hf_hub_download(repo_id="Aananda-giri/LLAMA3-Nepali", filename="parameters_300m/model_pg_398000_steps.pth", local_dir="./")1from transformers import PreTrainedTokenizerFast
2
3tokenizer = PreTrainedTokenizerFast.from_pretrained("Aananda-giri/LLAMA3-Nepali")
4tokenizer.save_pretrained("NepaliBPE")1import requests
2res=requests.get(r"https://raw.githubusercontent.com/Aananda-giri/LLAMA3-Nepali/main/4.%20inference/2_inference/previous_chapters.py")
3with open('previous_chapters.py', 'w') as f:
4 f.write(res.text)1import torch
2from previous_chapters import Llama3Model, ChatFormat, Tokenizer, generate_and_print_sample
3
4# Initialize tokenizer
5_tokenizer = Tokenizer("NepaliBPE/tokenizer.json")
6chat_tokenizer = ChatFormat(_tokenizer)
7
8# Define model configuration
9LLAMA32_CONFIG = {
10 "vocab_size": 50006,
11 "context_length": 512,
12 "emb_dim": 1320,
13 "n_heads": 20,
14 "n_layers": 10,
15 "hidden_dim": 5280,
16 "n_kv_groups": 5,
17 "rope_base": 500_000.0,
18 "dtype": torch.bfloat16,
19 "rope_freq": {
20 "factor": 32.0,
21 "low_freq_factor": 1.0,
22 "high_freq_factor": 4.0,
23 "original_context_length": 8192,
24 }
25}
26
27# Adjust RoPE Scaling
28old_context_length = 131_072
29new_context_length = LLAMA32_CONFIG["context_length"]
30LLAMA32_CONFIG["rope_base"] *= new_context_length / old_context_length
31
32# Load Model
33model = Llama3Model(LLAMA32_CONFIG)
34model.eval()
35
36# Optimize model if PyTorch 2.0 is available
37if torch.__version__ >= "2.0":
38 model = torch.compile(model)1# Move model to device
2device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
3model.to(device)
4print(f'device: {device}')
5
6# Load checkpoint
7latest_model_checkpoint = "parameters_300m/model_pg_398000_steps.pth"
8checkpoint = torch.load(latest_model_checkpoint, map_location=device, weights_only=False)
9model.load_state_dict(checkpoint["model_state_dict"])1# Generate text sample
2generate_and_print_sample(
3 PROMPT="रामले भात",
4 tokenizer=_tokenizer,
5 chat_tokenizer=chat_tokenizer,
6 model=model,
7 device=device,
8 context_length=LLAMA32_CONFIG["context_length"]
9)1from previous_chapters import generate_chat_optimized
2import time
3
4start_time = time.time()
5output_text = generate_chat_optimized(
6 prompt="रामले भात",
7 tokenizer=tokenizer,
8 chat_tokenizer=chat_tokenizer,
9 model=model,
10 max_new_tokens=20,
11 context_size=512,
12 device=device,
13 temperature=0.3,
14 top_k=5,
15 top_p=None,
16 eos_id=None,
17 repetition_penalty=1.2,
18 penalize_len_below=10,
19 batch_size=1 # Added parameter
20)
21
22print(f"time:{time.time() - start_time}\n output_text: {output_text}")