mirror of
https://github.com/rasbt/LLMs-from-scratch.git
synced 2026-04-10 12:33:42 +00:00
Organized setup instructions (#115)
* Organized setup instructions * update tets * link checker action * raise error upon broken link * fix links * fix links * delete duplicated paragraph
This commit is contained in:
committed by
GitHub
parent
05718c6b94
commit
e757091301
60
setup/02_installing-python-libraries/README.md
Normal file
60
setup/02_installing-python-libraries/README.md
Normal file
@@ -0,0 +1,60 @@
|
||||
# Installing Libraries Used In This Book
|
||||
|
||||
This document provides more information on double-checking your installed Python version and packages. (Please see the [../01_optional-python-setup-preferences](../01_optional-python-setup-preferences) folder for more information on installing Python and Python packages.)
|
||||
|
||||
I used the following libraries listed [here](https://github.com/rasbt/LLMs-from-scratch/blob/main/requirements.txt) for this book. Newer versions of these libraries are likely compatible as well. However, if you experience any problems with the code, you can try these library versions as a fallback.
|
||||
|
||||
To install these requirements most conveniently, you can use the `requirements.txt` file in the root directory for this code repository and execute the following command:
|
||||
|
||||
```
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
|
||||
Then, after completing the installation, please check if all the packages are installed and are up to date using
|
||||
|
||||
```
|
||||
python python_environment_check.py
|
||||
```
|
||||
|
||||
<img src="figures/check_1.jpg" width="600px">
|
||||
|
||||
It's also recommended to check the versions in JupyterLab by running the `jupyter_environment_check.ipynb` in this directory, which should ideally give you the same results as above.
|
||||
|
||||
<img src="figures/check_2.jpg" width="500px">
|
||||
|
||||
If you see the following issues, it's likely that your JupyterLab instance is connected to wrong conda environment:
|
||||
|
||||
<img src="figures/jupyter-issues.jpg" width="450px">
|
||||
|
||||
In this case, you may want to use `watermark` to check if you opened the JupyterLab instance in the right conda environment using the `--conda` flag:
|
||||
|
||||
<img src="figures/watermark.jpg" width="350px">
|
||||
|
||||
|
||||
<br>
|
||||
<br>
|
||||
|
||||
|
||||
## Installing PyTorch
|
||||
|
||||
PyTorch can be installed just like any other Python library or package using pip. For example:
|
||||
|
||||
```bash
|
||||
pip install torch==2.0.1
|
||||
```
|
||||
|
||||
However, since PyTorch is a comprehensive library featuring CPU- and GPU-compatible codes, the installation may require additional settings and explanation (see the *A.1.3 Installing PyTorch in the book for more information*).
|
||||
|
||||
It's also highly recommended to consult the installation guide menu on the official PyTorch website at [https://pytorch.org](https://pytorch.org).
|
||||
|
||||
<img src="figures/pytorch-installer.jpg" width="600px">
|
||||
|
||||
|
||||
|
||||
---
|
||||
|
||||
|
||||
|
||||
|
||||
Any questions? Please feel free to reach out in the [Discussion Forum](https://github.com/rasbt/LLMs-from-scratch/discussions).
|
||||
BIN
setup/02_installing-python-libraries/figures/check_1.jpg
Normal file
BIN
setup/02_installing-python-libraries/figures/check_1.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 107 KiB |
BIN
setup/02_installing-python-libraries/figures/check_2.jpg
Normal file
BIN
setup/02_installing-python-libraries/figures/check_2.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 79 KiB |
BIN
setup/02_installing-python-libraries/figures/jupyter-issues.jpg
Normal file
BIN
setup/02_installing-python-libraries/figures/jupyter-issues.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 103 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 94 KiB |
BIN
setup/02_installing-python-libraries/figures/watermark.jpg
Normal file
BIN
setup/02_installing-python-libraries/figures/watermark.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 36 KiB |
@@ -0,0 +1,64 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c31e08b0-f551-4d67-b95e-41f49de3b392",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<font size=\"1\">\n",
|
||||
"Supplementary code for \"Build a Large Language Model From Scratch\": <a href=\"https://www.manning.com/books/build-a-large-language-model-from-scratch\">https://www.manning.com/books/build-a-large-language-model-from-scratch</a> by <a href=\"https://sebastianraschka.com\">Sebastian Raschka</a><br>\n",
|
||||
"Code repository: <a href=\"https://github.com/rasbt/LLMs-from-scratch\">https://github.com/rasbt/LLMs-from-scratch</a>\n",
|
||||
"</font>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "67f6f7ed-b67d-465b-bf6f-a99b0d996930",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[OK] Your Python version is 3.10.12\n",
|
||||
"[OK] numpy 1.26.0\n",
|
||||
"[OK] matplotlib 3.8.2\n",
|
||||
"[OK] jupyterlab 4.0.6\n",
|
||||
"[OK] tensorflow 2.15.0\n",
|
||||
"[OK] torch 2.2.1\n",
|
||||
"[OK] tqdm 4.66.1\n",
|
||||
"[OK] tiktoken 0.5.1\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from python_environment_check import check_packages, get_requirements_dict\n",
|
||||
"\n",
|
||||
"d = get_requirements_dict()\n",
|
||||
"check_packages(d)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
# Copyright (c) Sebastian Raschka under Apache License 2.0 (see LICENSE.txt).
|
||||
# Source for "Build a Large Language Model From Scratch"
|
||||
# - https://www.manning.com/books/build-a-large-language-model-from-scratch
|
||||
# Code: https://github.com/rasbt/LLMs-from-scratch
|
||||
|
||||
from importlib.metadata import PackageNotFoundError, import_module
|
||||
import importlib.metadata
|
||||
from os.path import dirname, join, realpath
|
||||
from packaging.version import parse as version_parse
|
||||
import platform
|
||||
import sys
|
||||
|
||||
if version_parse(platform.python_version()) < version_parse('3.9'):
|
||||
print('[FAIL] We recommend Python 3.9 or newer but'
|
||||
' found version %s' % (sys.version))
|
||||
else:
|
||||
print('[OK] Your Python version is %s' % (platform.python_version()))
|
||||
|
||||
|
||||
def get_packages(pkgs):
|
||||
versions = []
|
||||
for p in pkgs:
|
||||
try:
|
||||
imported = import_module(p)
|
||||
try:
|
||||
version = (getattr(imported, '__version__', None) or
|
||||
getattr(imported, 'version', None) or
|
||||
getattr(imported, 'version_info', None))
|
||||
if version is None:
|
||||
# If common attributes don't exist, use importlib.metadata
|
||||
version = importlib.metadata.version(p)
|
||||
versions.append(version)
|
||||
except PackageNotFoundError:
|
||||
# Handle case where package is not installed
|
||||
versions.append('0.0')
|
||||
except ImportError:
|
||||
# Fallback if importlib.import_module fails for unexpected reasons
|
||||
versions.append('0.0')
|
||||
return versions
|
||||
|
||||
|
||||
def get_requirements_dict():
|
||||
PROJECT_ROOT = dirname(realpath(__file__))
|
||||
PROJECT_ROOT_UP_TWO = dirname(dirname(PROJECT_ROOT))
|
||||
REQUIREMENTS_FILE = join(PROJECT_ROOT_UP_TWO, "requirements.txt")
|
||||
d = {}
|
||||
with open(REQUIREMENTS_FILE) as f:
|
||||
for line in f:
|
||||
if not line.strip():
|
||||
continue
|
||||
line = line.split("#")[0].strip()
|
||||
line = line.split(" ")
|
||||
line = [l.strip() for l in line]
|
||||
d[line[0]] = line[-1]
|
||||
return d
|
||||
|
||||
|
||||
def check_packages(d):
|
||||
versions = get_packages(d.keys())
|
||||
|
||||
for (pkg_name, suggested_ver), actual_ver in zip(d.items(), versions):
|
||||
if actual_ver == 'N/A':
|
||||
continue
|
||||
actual_ver, suggested_ver = version_parse(actual_ver), version_parse(suggested_ver)
|
||||
if actual_ver < suggested_ver:
|
||||
print(f'[FAIL] {pkg_name} {actual_ver}, please upgrade to >= {suggested_ver}')
|
||||
else:
|
||||
print(f'[OK] {pkg_name} {actual_ver}')
|
||||
|
||||
|
||||
def main():
|
||||
d = get_requirements_dict()
|
||||
check_packages(d)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
14
setup/02_installing-python-libraries/tests.py
Normal file
14
setup/02_installing-python-libraries/tests.py
Normal file
@@ -0,0 +1,14 @@
|
||||
# Copyright (c) Sebastian Raschka under Apache License 2.0 (see LICENSE.txt).
|
||||
# Source for "Build a Large Language Model From Scratch"
|
||||
# - https://www.manning.com/books/build-a-large-language-model-from-scratch
|
||||
# Code: https://github.com/rasbt/LLMs-from-scratch
|
||||
|
||||
# File for internal use (unit tests)
|
||||
|
||||
from python_environment_check import main
|
||||
|
||||
|
||||
def test_main(capsys):
|
||||
main()
|
||||
captured = capsys.readouterr()
|
||||
assert "FAIL" not in captured.out
|
||||
Reference in New Issue
Block a user