Skip to content

Commit c21bfe4

Browse files
authored
Add PyPI package (#576)
* Add PyPI package * fixes * fixes
1 parent 7757c3d commit c21bfe4

File tree

52 files changed

+2173
-2585
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2173
-2585
lines changed

.github/workflows/basic-tests-linux-uv.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ jobs:
6666
run: |
6767
source .venv/bin/activate
6868
pytest ch02/05_bpe-from-scratch/tests/tests.py
69+
70+
- name: Test Selected Bonus Materials
71+
shell: bash
72+
run: |
73+
source .venv/bin/activate
74+
pytest pkg/llms_from_scratch/tests/

appendix-D/01_main-chapter-code/appendix-D.ipynb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
"\n",
6969
"\n",
7070
"from previous_chapters import GPTModel\n",
71+
"# If the `previous_chapters.py` file is not available locally,\n",
72+
"# you can import it from the `llms-from-scratch` PyPI package.\n",
73+
"# For details, see: https://github.com/rasbt/LLMs-from-scratch/tree/main/pkg\n",
74+
"# E.g.,\n",
75+
"# from llms_from_scratch.ch04 import GPTModel\n",
7176
"\n",
7277
"GPT_CONFIG_124M = {\n",
7378
" \"vocab_size\": 50257, # Vocabulary size\n",
@@ -139,6 +144,9 @@
139144
"outputs": [],
140145
"source": [
141146
"from previous_chapters import create_dataloader_v1\n",
147+
"# Alternatively:\n",
148+
"# from llms_from_scratch.ch02 import create_dataloader_v1\n",
149+
"\n",
142150
"\n",
143151
"# Train/validation ratio\n",
144152
"train_ratio = 0.90\n",
@@ -454,6 +462,9 @@
454462
"outputs": [],
455463
"source": [
456464
"from previous_chapters import calc_loss_batch\n",
465+
"# Alternatively:\n",
466+
"# from llms_from_scratch.ch05 import calc_loss_batch\n",
467+
"\n",
457468
"\n",
458469
"torch.manual_seed(123)\n",
459470
"model = GPTModel(GPT_CONFIG_124M)\n",
@@ -551,6 +562,9 @@
551562
"outputs": [],
552563
"source": [
553564
"from previous_chapters import evaluate_model, generate_and_print_sample\n",
565+
"# Alternatively:\n",
566+
"# from llms_from_scratch.ch05 import evaluate_model, generate_and_print_samplee\n",
567+
"\n",
554568
"\n",
555569
"ORIG_BOOK_VERSION = False\n",
556570
"\n",
@@ -790,6 +804,9 @@
790804
],
791805
"source": [
792806
"from previous_chapters import plot_losses\n",
807+
"# Alternatively:\n",
808+
"# from llms_from_scratch.ch05 import plot_losses\n",
809+
"\n",
793810
"\n",
794811
"epochs_tensor = torch.linspace(1, n_epochs, len(train_losses))\n",
795812
"plot_losses(epochs_tensor, tokens_seen, train_losses, val_losses)\n",
@@ -823,7 +840,7 @@
823840
"name": "python",
824841
"nbconvert_exporter": "python",
825842
"pygments_lexer": "ipython3",
826-
"version": "3.11.4"
843+
"version": "3.10.16"
827844
}
828845
},
829846
"nbformat": 4,

appendix-E/01_main-chapter-code/appendix-E.ipynb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@
198198
" create_balanced_dataset,\n",
199199
" random_split\n",
200200
")\n",
201+
"# If the `previous_chapters.py` file is not available locally,\n",
202+
"# you can import it from the `llms-from-scratch` PyPI package.\n",
203+
"# For details, see: https://github.com/rasbt/LLMs-from-scratch/tree/main/pkg\n",
204+
"# E.g.,\n",
205+
"# from llms_from_scratch.ch06 import (\n",
206+
"# download_and_unzip_spam_data,\n",
207+
"# create_balanced_dataset,\n",
208+
"# random_split\n",
209+
"# )\n",
210+
"\n",
201211
"\n",
202212
"\n",
203213
"url = \"https://archive.ics.uci.edu/static/public/228/sms+spam+collection.zip\"\n",
@@ -409,6 +419,10 @@
409419
"source": [
410420
"from gpt_download import download_and_load_gpt2\n",
411421
"from previous_chapters import GPTModel, load_weights_into_gpt\n",
422+
"# Alternatively:\n",
423+
"# from llms_from_scratch.ch04 import GPTModel\n",
424+
"# from llms_from_scratch.ch05 import load_weights_into_gpt\n",
425+
"\n",
412426
"\n",
413427
"\n",
414428
"CHOOSE_MODEL = \"gpt2-small (124M)\"\n",
@@ -577,6 +591,9 @@
577591
],
578592
"source": [
579593
"from previous_chapters import calc_accuracy_loader\n",
594+
"# Alternatively:\n",
595+
"# from llms_from_scratch.ch06 import calc_accuracy_loader\n",
596+
"\n",
580597
"\n",
581598
"\n",
582599
"torch.manual_seed(123)\n",
@@ -1387,6 +1404,8 @@
13871404
"source": [
13881405
"import time\n",
13891406
"from previous_chapters import train_classifier_simple\n",
1407+
"# Alternatively:\n",
1408+
"# from llms_from_scratch.ch06 import train_classifier_simple\n",
13901409
"\n",
13911410
"\n",
13921411
"start_time = time.time()\n",
@@ -1442,6 +1461,8 @@
14421461
],
14431462
"source": [
14441463
"from previous_chapters import plot_values\n",
1464+
"# Alternatively:\n",
1465+
"# from llms_from_scratch.ch06 import plot_values\n",
14451466
"\n",
14461467
"epochs_tensor = torch.linspace(0, num_epochs, len(train_losses))\n",
14471468
"examples_seen_tensor = torch.linspace(0, examples_seen, len(train_losses))\n",

ch04/01_main-chapter-code/ch04.ipynb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,12 @@
970970
"metadata": {},
971971
"outputs": [],
972972
"source": [
973+
"# If the `previous_chapters.py` file is not available locally,\n",
974+
"# you can import it from the `llms-from-scratch` PyPI package.\n",
975+
"# For details, see: https://github.com/rasbt/LLMs-from-scratch/tree/main/pkg\n",
976+
"# E.g.,\n",
977+
"# from llms_from_scratch.ch03 import MultiHeadAttention\n",
978+
"\n",
973979
"from previous_chapters import MultiHeadAttention\n",
974980
"\n",
975981
"\n",
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Chapter 4: Implementing a GPT Model from Scratch To Generate Text
22

33
- [flops-analysis.ipynb](flops-analysis.ipynb) analyses the floating point operations per second (FLOPS) of the GPT model(s) implemented in the main chapter.
4-
- [previous_chapters.py](previous_chapters.py) is a Python module containing the `GPTModel` code we implemented in chapter 4 and other code implemented in previous chapters, which we import in the analysis notebook.
54
- `requirements-extra.txt` includes additional Python libraries that need to be installed (via `pip install -r requirements-extra.txt`.

ch04/02_performance-analysis/flops-analysis.ipynb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@
127127
"import torch\n",
128128
"from thop import profile\n",
129129
"\n",
130-
"from previous_chapters import GPTModel\n",
130+
"# For installation instructions, see:\n",
131+
"# https://github.com/rasbt/LLMs-from-scratch/tree/main/pkg\n",
132+
"from llms_from_scratch.ch04 import GPTModel\n",
131133
"\n",
132134
"\n",
133135
"BASE_CONFIG = {\n",
@@ -550,7 +552,7 @@
550552
"name": "python",
551553
"nbconvert_exporter": "python",
552554
"pygments_lexer": "ipython3",
553-
"version": "3.11.4"
555+
"version": "3.10.16"
554556
}
555557
},
556558
"nbformat": 4,

ch05/01_main-chapter-code/ch05.ipynb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@
147147
"source": [
148148
"import torch\n",
149149
"from previous_chapters import GPTModel\n",
150+
"# If the `previous_chapters.py` file is not available locally,\n",
151+
"# you can import it from the `llms-from-scratch` PyPI package.\n",
152+
"# For details, see: https://github.com/rasbt/LLMs-from-scratch/tree/main/pkg\n",
153+
"# E.g.,\n",
154+
"# from llms_from_scratch.ch04 import GPTModel\n",
150155
"\n",
151156
"GPT_CONFIG_124M = {\n",
152157
" \"vocab_size\": 50257, # Vocabulary size\n",
@@ -212,6 +217,9 @@
212217
"import tiktoken\n",
213218
"from previous_chapters import generate_text_simple\n",
214219
"\n",
220+
"# Alternatively:\n",
221+
"# from llms_from_scratch.ch04 import generate_text_simple\n",
222+
"\n",
215223
"def text_to_token_ids(text, tokenizer):\n",
216224
" encoded = tokenizer.encode(text, allowed_special={'<|endoftext|>'})\n",
217225
" encoded_tensor = torch.tensor(encoded).unsqueeze(0) # add batch dimension\n",
@@ -924,6 +932,8 @@
924932
"outputs": [],
925933
"source": [
926934
"from previous_chapters import create_dataloader_v1\n",
935+
"# Alternatively:\n",
936+
"# from llms_from_scratch.ch02 import create_dataloader_v1\n",
927937
"\n",
928938
"# Train/validation ratio\n",
929939
"train_ratio = 0.90\n",
@@ -2548,7 +2558,7 @@
25482558
"name": "python",
25492559
"nbconvert_exporter": "python",
25502560
"pygments_lexer": "ipython3",
2551-
"version": "3.12.8"
2561+
"version": "3.10.16"
25522562
}
25532563
},
25542564
"nbformat": 4,

0 commit comments

Comments
 (0)