Views
No views yet

llms-from-scratch packagellms-from-scratch PyPI package based on the source code in this repository at pkg/llms_from_scratch.pip install llms_from_scratch tokenizers1USE_REASONING_MODEL = True # The "thinking" model
2USE_REASONING_MODEL = False # The base model1MAX_NEW_TOKENS = 150
2TEMPERATURE = 0.
3TOP_K = 11from llms_from_scratch.qwen3 import download_from_huggingface
2
3repo_id = "rasbt/qwen3-from-scratch"
4
5if USE_REASONING_MODEL:
6 filename = "qwen3-0.6B.pth"
7 local_dir = "Qwen3-0.6B"
8else:
9 filename = "qwen3-0.6B-base.pth"
10 local_dir = "Qwen3-0.6B-Base"
11
12download_from_huggingface(
13 repo_id=repo_id,
14 filename=filename,
15 local_dir=local_dir
16)1from pathlib import Path
2import torch
3
4from llms_from_scratch.qwen3 import Qwen3Model, QWEN_CONFIG_06_B
5
6model_file = Path(local_dir) / filename
7
8model = Qwen3Model(QWEN_CONFIG_06_B)
9model.load_state_dict(torch.load(model_file, weights_only=True, map_location="cpu"))
10
11device = (
12 torch.device("cuda") if torch.cuda.is_available() else
13 torch.device("mps") if torch.backends.mps.is_available() else
14 torch.device("cpu")
15)
16model.to(device)1from llms_from_scratch.qwen3 import Qwen3Tokenizer
2
3if USE_REASONING_MODEL:
4 tok_filename = str(Path("Qwen3-0.6B") / "tokenizer.json")
5else:
6 tok_filename = str(Path("Qwen3-0.6B-Base") / "tokenizer-base.json")
7
8tokenizer = Qwen3Tokenizer(
9 tokenizer_file_path=tok_filename,
10 repo_id=repo_id,
11 add_generation_prompt=USE_REASONING_MODEL,
12 add_thinking=USE_REASONING_MODEL
13)1prompt = "Give me a short introduction to large language models."
2input_token_ids = tokenizer.encode(prompt)1from llms_from_scratch.ch05 import generate
2import time
3
4torch.manual_seed(123)
5
6start = time.time()
7
8output_token_ids = generate(
9 model=model,
10 idx=torch.tensor(input_token_ids, device=device).unsqueeze(0),
11 max_new_tokens=150,
12 context_size=QWEN_CONFIG_06_B["context_length"],
13 top_k=1,
14 temperature=0.
15)
16
17total_time = time.time() - start
18print(f"Time: {total_time:.2f} sec")
19print(f"{int(len(output_token_ids[0])/total_time)} tokens/sec")
20
21if torch.cuda.is_available():
22 max_mem_bytes = torch.cuda.max_memory_allocated()
23 max_mem_gb = max_mem_bytes / (1024 ** 3)
24 print(f"Max memory allocated: {max_mem_gb:.2f} GB")
25
26output_text = tokenizer.decode(output_token_ids.squeeze(0).tolist())
27
28print("\n\nOutput text:\n\n", output_text + "...")Time: 6.35 sec
25 tokens/sec
Max memory allocated: 1.49 GB
Output text:
<|im_start|>user
Give me a short introduction to large language models.<|im_end|>
Large language models (LLMs) are advanced artificial intelligence systems designed to generate human-like text. They are trained on vast amounts of text data, allowing them to understand and generate coherent, contextually relevant responses. LLMs are used in a variety of applications, including chatbots, virtual assistants, content generation, and more. They are powered by deep learning algorithms and can be fine-tuned for specific tasks, making them versatile tools for a wide range of industries.<|endoftext|>Human resources department of a company is planning to hire 100 new employees. The company has a budget of $100,000 for the recruitment process. The company has a minimum wage of $10 per hour. The company has a total of...model.to(device)1model = torch.compile(model)
2model.to(device)generate call.generate calls:| Tokens/sec | Memory | |
|---|---|---|
| Qwen3Model | 25 | 1.49 GB |
| Qwen3Model compiled | 101 | 1.99 GB |