cs-lab 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cs_lab-1.0.0/.github/workflows/pytest.yml +47 -0
- cs_lab-1.0.0/.github/workflows/release.yml +50 -0
- cs_lab-1.0.0/.gitignore +16 -0
- cs_lab-1.0.0/.pre-commit-config.yaml +36 -0
- cs_lab-1.0.0/.python-version +1 -0
- cs_lab-1.0.0/LICENSE +21 -0
- cs_lab-1.0.0/PKG-INFO +374 -0
- cs_lab-1.0.0/README.md +336 -0
- cs_lab-1.0.0/cliff.toml +68 -0
- cs_lab-1.0.0/cs_lab/__init__.py +146 -0
- cs_lab-1.0.0/cs_lab/__main__.py +5 -0
- cs_lab-1.0.0/cs_lab/_version.py +24 -0
- cs_lab-1.0.0/cs_lab/cli.py +325 -0
- cs_lab-1.0.0/cs_lab/config.py +192 -0
- cs_lab-1.0.0/cs_lab/datamaestro.py +33 -0
- cs_lab-1.0.0/cs_lab/hf.py +351 -0
- cs_lab-1.0.0/cs_lab/machine.py +328 -0
- cs_lab-1.0.0/cs_lab/notebook.py +21 -0
- cs_lab-1.0.0/cs_lab/profile.py +276 -0
- cs_lab-1.0.0/cs_lab/pyterrier.py +29 -0
- cs_lab-1.0.0/cs_lab/resources.py +211 -0
- cs_lab-1.0.0/cs_lab/scan.py +772 -0
- cs_lab-1.0.0/cs_lab/web.py +180 -0
- cs_lab-1.0.0/pyproject.toml +91 -0
- cs_lab-1.0.0/release-notes.md +12 -0
- cs_lab-1.0.0/tests/test_cli.py +160 -0
- cs_lab-1.0.0/tests/test_config.py +119 -0
- cs_lab-1.0.0/tests/test_hardware.py +279 -0
- cs_lab-1.0.0/tests/test_hf.py +240 -0
- cs_lab-1.0.0/tests/test_profile.py +188 -0
- cs_lab-1.0.0/tests/test_resources.py +108 -0
- cs_lab-1.0.0/tests/test_scan.py +237 -0
- cs_lab-1.0.0/tests/test_scan_profiles.py +186 -0
- cs_lab-1.0.0/tests/test_web.py +158 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
name: Python package
|
|
2
|
+
|
|
3
|
+
concurrency:
|
|
4
|
+
group: ${{ github.workflow }}-${{ github.ref }}
|
|
5
|
+
cancel-in-progress: true
|
|
6
|
+
|
|
7
|
+
on:
|
|
8
|
+
push:
|
|
9
|
+
branches: [main]
|
|
10
|
+
pull_request:
|
|
11
|
+
branches: [main]
|
|
12
|
+
|
|
13
|
+
jobs:
|
|
14
|
+
build:
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
strategy:
|
|
17
|
+
matrix:
|
|
18
|
+
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
|
19
|
+
|
|
20
|
+
steps:
|
|
21
|
+
- uses: actions/checkout@v4
|
|
22
|
+
with:
|
|
23
|
+
fetch-depth: 0 # Needed for hatch-vcs to determine version
|
|
24
|
+
|
|
25
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
26
|
+
uses: actions/setup-python@v5
|
|
27
|
+
with:
|
|
28
|
+
python-version: ${{ matrix.python-version }}
|
|
29
|
+
|
|
30
|
+
- name: Install uv
|
|
31
|
+
uses: astral-sh/setup-uv@v5
|
|
32
|
+
with:
|
|
33
|
+
enable-cache: true
|
|
34
|
+
|
|
35
|
+
- name: Install dependencies
|
|
36
|
+
run: |
|
|
37
|
+
uv sync --extra dev
|
|
38
|
+
|
|
39
|
+
- name: Lint with ruff
|
|
40
|
+
run: |
|
|
41
|
+
uv run ruff check cs_lab tests
|
|
42
|
+
uv run ruff format --check cs_lab tests
|
|
43
|
+
|
|
44
|
+
- name: Test with pytest
|
|
45
|
+
timeout-minutes: 10
|
|
46
|
+
run: |
|
|
47
|
+
uv run pytest
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Build and publish the package to PyPI when a GitHub release is created.
|
|
2
|
+
# Uses PyPI trusted publishing (OIDC): no API token needed.
|
|
3
|
+
|
|
4
|
+
name: Upload Python Package
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
release:
|
|
8
|
+
types: [created]
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
deploy:
|
|
12
|
+
permissions:
|
|
13
|
+
contents: write # Required to update release notes
|
|
14
|
+
id-token: write # Required for PyPI trusted publishing
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- uses: actions/checkout@v4
|
|
19
|
+
with:
|
|
20
|
+
fetch-depth: 0 # Needed for hatch-vcs to determine version
|
|
21
|
+
|
|
22
|
+
- name: Set up Python
|
|
23
|
+
uses: actions/setup-python@v5
|
|
24
|
+
with:
|
|
25
|
+
python-version: "3.x"
|
|
26
|
+
|
|
27
|
+
- name: Install hatch
|
|
28
|
+
run: |
|
|
29
|
+
python -m pip install --upgrade pip
|
|
30
|
+
pip install hatch
|
|
31
|
+
|
|
32
|
+
- name: Generate changelog for this release
|
|
33
|
+
uses: orhun/git-cliff-action@v4
|
|
34
|
+
with:
|
|
35
|
+
config: cliff.toml
|
|
36
|
+
args: --latest --strip header
|
|
37
|
+
env:
|
|
38
|
+
OUTPUT: release-notes.md
|
|
39
|
+
|
|
40
|
+
- name: Update release notes
|
|
41
|
+
uses: softprops/action-gh-release@v2
|
|
42
|
+
with:
|
|
43
|
+
body_path: release-notes.md
|
|
44
|
+
|
|
45
|
+
- name: Build python package
|
|
46
|
+
run: |
|
|
47
|
+
hatch build
|
|
48
|
+
|
|
49
|
+
- name: Publish package to PyPI
|
|
50
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
cs_lab-1.0.0/.gitignore
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
default_install_hook_types:
|
|
2
|
+
- pre-commit
|
|
3
|
+
- commit-msg
|
|
4
|
+
|
|
5
|
+
repos:
|
|
6
|
+
- hooks:
|
|
7
|
+
- id: check-yaml
|
|
8
|
+
- id: check-toml
|
|
9
|
+
- id: end-of-file-fixer
|
|
10
|
+
- id: trailing-whitespace
|
|
11
|
+
repo: https://github.com/pre-commit/pre-commit-hooks
|
|
12
|
+
rev: v6.0.0
|
|
13
|
+
- hooks:
|
|
14
|
+
- id: python-check-blanket-noqa
|
|
15
|
+
- id: python-no-eval
|
|
16
|
+
repo: https://github.com/pre-commit/pygrep-hooks
|
|
17
|
+
rev: v1.10.0
|
|
18
|
+
- hooks:
|
|
19
|
+
- id: no-fixme-todo
|
|
20
|
+
name: No FIXME or TODO markers
|
|
21
|
+
entry: '(FIXME:|TODO:)'
|
|
22
|
+
language: pygrep
|
|
23
|
+
types: [python]
|
|
24
|
+
repo: local
|
|
25
|
+
- hooks:
|
|
26
|
+
- id: ruff
|
|
27
|
+
args: [--fix]
|
|
28
|
+
- id: ruff-format
|
|
29
|
+
repo: https://github.com/astral-sh/ruff-pre-commit
|
|
30
|
+
rev: v0.14.10
|
|
31
|
+
- hooks:
|
|
32
|
+
- id: conventional-pre-commit
|
|
33
|
+
stages:
|
|
34
|
+
- commit-msg
|
|
35
|
+
repo: https://github.com/compilerla/conventional-pre-commit
|
|
36
|
+
rev: v4.3.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.12
|
cs_lab-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Benjamin Piwowarski
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
cs_lab-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: cs-lab
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: The runtime a teaching notebook needs: a compute-profile ladder, hardware detection, and course resources from a shared local cache (falling back to the Hub)
|
|
5
|
+
Project-URL: Homepage, https://github.com/bpiwowar/cs-lab
|
|
6
|
+
Project-URL: Repository, https://github.com/bpiwowar/cs-lab
|
|
7
|
+
Author-email: Benjamin Piwowarski <benjamin@piwowarski.fr>
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: cache,hardware,huggingface,notebook,profile,teaching
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Education
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Education
|
|
22
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Provides-Extra: datamaestro
|
|
25
|
+
Requires-Dist: datamaestro>=1.5; extra == 'datamaestro'
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: pre-commit>=3.5; extra == 'dev'
|
|
28
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
29
|
+
Requires-Dist: ruff>=0.8; extra == 'dev'
|
|
30
|
+
Provides-Extra: hf
|
|
31
|
+
Requires-Dist: datasets>=2.7; extra == 'hf'
|
|
32
|
+
Requires-Dist: transformers>=4.30; extra == 'hf'
|
|
33
|
+
Provides-Extra: pyterrier
|
|
34
|
+
Requires-Dist: python-terrier>=0.10; extra == 'pyterrier'
|
|
35
|
+
Provides-Extra: widgets
|
|
36
|
+
Requires-Dist: ipywidgets>=8; extra == 'widgets'
|
|
37
|
+
Description-Content-Type: text/markdown
|
|
38
|
+
|
|
39
|
+
# cs-lab
|
|
40
|
+
|
|
41
|
+
**What a teaching notebook needs from the machine it runs on: how much compute
|
|
42
|
+
to spend, what the hardware offers, and where the models come from.**
|
|
43
|
+
|
|
44
|
+
A practical hands the same file to a laptop, a lab machine and a Colab GPU. It
|
|
45
|
+
has to answer three questions, and `cs-lab` answers one each:
|
|
46
|
+
|
|
47
|
+
| | | |
|
|
48
|
+
|---|---|---|
|
|
49
|
+
| How much work should I do? | [`Profile`](#compute-profiles), a ladder the course declares | `NOTEBOOK_PROFILE` |
|
|
50
|
+
| What does this machine offer? | [`hardware()`](#the-machine-underneath) — device, dtype, which libraries work | `NOTEBOOK_BACKEND` |
|
|
51
|
+
| Where do the weights come from? | [the loaders](#supported-libraries) — a shared cache, the Hub on a miss | `CS_LAB_CACHE_PATH` |
|
|
52
|
+
|
|
53
|
+
They are one package because a notebook asks all three in the same breath
|
|
54
|
+
(`hardware(Profile)`, then `load_hf_model(Profile.pick(...), dtype=hw.dtype)`)
|
|
55
|
+
and because a student installs them together. It has **no required
|
|
56
|
+
dependency**: `torch`, `transformers`, `datasets`, `pyterrier` and
|
|
57
|
+
`datamaestro` are imported only by the functions that use them.
|
|
58
|
+
|
|
59
|
+
Building the notebooks themselves is a separate concern, and a separate
|
|
60
|
+
package:
|
|
61
|
+
[jupytext-notebook-helper](https://github.com/bpiwowar/jupytext-notebook-helper),
|
|
62
|
+
which students never install.
|
|
63
|
+
|
|
64
|
+
> Renamed from **cached-hub**, which said only what the third row does.
|
|
65
|
+
> Imports change from `cached_hub` to `cs_lab`; the CLI is `cs-lab`, with the
|
|
66
|
+
> cache commands under a `cache` group (`cached-hub list` -> `cs-lab cache
|
|
67
|
+
> list`), leaving room for the rest of what the package now covers; `CACHED_HUB_PATH` / `CACHED_HUB_ENFORCE` are still read, so a
|
|
68
|
+
> classroom machine that exports them keeps working.
|
|
69
|
+
|
|
70
|
+
## Why a shared cache
|
|
71
|
+
|
|
72
|
+
In a classroom, every student pulling `gpt2`, `SmolLM2` and `imdb` from the Hub
|
|
73
|
+
at the same minute is slow, fragile, and sometimes impossible (no home directory
|
|
74
|
+
quota, shaky proxy, offline lab). The usual answer is a shared read-only
|
|
75
|
+
directory pre-filled by the instructor. `cs-lab` makes that directory a
|
|
76
|
+
first-class thing:
|
|
77
|
+
|
|
78
|
+
- notebook code calls `load_hf_model("gpt2")` and gets the cached copy when it
|
|
79
|
+
exists, the Hub otherwise, with a one-line note saying which;
|
|
80
|
+
- the instructor declares the resources once, and `cs-lab cache download` fills
|
|
81
|
+
the cache (each shared resource once, optional ones on demand);
|
|
82
|
+
- an *enforce* mode turns any cache miss into an error, so you can check that
|
|
83
|
+
every notebook runs fully from the cache before the session.
|
|
84
|
+
|
|
85
|
+
## Install
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
pip install cs-lab # loaders only (bring your own transformers/datasets)
|
|
89
|
+
pip install "cs-lab[hf]" # + transformers, datasets
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Supported libraries
|
|
93
|
+
|
|
94
|
+
| Library | cs-lab function | Equivalent to | Cached under `$CS_LAB_CACHE_PATH` | Pre-download with |
|
|
95
|
+
|------------------------|------------------------------------|-------------------------------------------------|----------------------------------------|--------------------------------|
|
|
96
|
+
| transformers | `load_hf_model(id, cls=AutoModel, **kw)` | `cls.from_pretrained(id, **kw)` | `huggingface/models/<id>/` | `make_hf_model_resource` |
|
|
97
|
+
| transformers | `load_hf_tokenizer(id, cls=AutoTokenizer, **kw)`| `cls.from_pretrained(id, **kw)` | `huggingface/tokenizers/<id>/` | `make_hf_tokenizer_resource` |
|
|
98
|
+
| transformers | `load_hf_processor(id, cls=AutoProcessor, **kw)`| `cls.from_pretrained(id, **kw)` | `huggingface/processors/<id>/` | `make_hf_processor_resource` |
|
|
99
|
+
| datasets | `load_hf_dataset(id, name=None, split=None, **kw)` | `datasets.load_dataset(id, name, split=split, **kw)` | `huggingface/datasets/<id>[-<name>]/<split>/` | `make_hf_dataset_resource` |
|
|
100
|
+
| transformers | `HFModel(id, tok_cls, model_cls, **kw)` | lazy `.tokenizer` / `.model` via the two loaders above | as above | model + tokenizer resources |
|
|
101
|
+
| pyterrier / ir-datasets| (use `pt.get_dataset` directly) | `pt.get_dataset(id)` | pyterrier's own home (`PYTERRIER_HOME`, `IR_DATASETS_HOME`) | `make_pyterrier_dataset_resource` |
|
|
102
|
+
| datamaestro | (use `datamaestro.prepare_dataset` directly) | `prepare_dataset(id)` | datamaestro's own store (`DATAMAESTRO_DIR`) | `make_datamaestro_resource` |
|
|
103
|
+
|
|
104
|
+
The loaders differ from their equivalents in one way only: with
|
|
105
|
+
`CS_LAB_CACHE_PATH` set, they first look for the resource in the cache layout
|
|
106
|
+
above, log where it came from, and on a miss forward to the equivalent call
|
|
107
|
+
with `cache_dir=$CS_LAB_CACHE_PATH/huggingface` added (unless you passed one), or
|
|
108
|
+
raise `CacheMissError` in enforce mode. PyTerrier and datamaestro manage their
|
|
109
|
+
own caches, so there is no loader for them: `cs-lab` only declares them as
|
|
110
|
+
resources so that `cs-lab cache download` fetches everything a course needs in
|
|
111
|
+
one go, and you keep calling those libraries as usual.
|
|
112
|
+
|
|
113
|
+
## In notebooks
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
from cs_lab import load_hf_model, load_hf_tokenizer, load_hf_dataset, HFModel
|
|
117
|
+
from transformers import AutoModelForCausalLM
|
|
118
|
+
|
|
119
|
+
tokenizer = load_hf_tokenizer("HuggingFaceTB/SmolLM2-1.7B-Instruct")
|
|
120
|
+
model = load_hf_model(
|
|
121
|
+
"HuggingFaceTB/SmolLM2-1.7B-Instruct", AutoModelForCausalLM, device_map="auto"
|
|
122
|
+
)
|
|
123
|
+
train = load_hf_dataset("imdb", split="train")
|
|
124
|
+
sst2 = load_hf_dataset("glue", name="sst2") # DatasetDict of the cached splits
|
|
125
|
+
|
|
126
|
+
hf = HFModel("gpt2") # lazy: nothing is loaded yet
|
|
127
|
+
hf.tokenizer, hf.model # AutoTokenizer / AutoModel, loaded on first access
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Each loader checks the local cache first, then falls back to the Hub with a
|
|
131
|
+
warning. Extra keyword arguments go to `from_pretrained` / `load_dataset`.
|
|
132
|
+
|
|
133
|
+
## Configuration
|
|
134
|
+
|
|
135
|
+
| Variable | Effect |
|
|
136
|
+
|----------------------|------------------------------------------------------------------------|
|
|
137
|
+
| `CS_LAB_CACHE_PATH` | Root of the shared cache. Unset: the library does nothing (see below). |
|
|
138
|
+
| `CS_LAB_CACHE_ENFORCE` | If set (any value), a cache miss raises `CacheMissError` instead of falling back. |
|
|
139
|
+
|
|
140
|
+
Layout under the root (`org/name` becomes `org-name`):
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
$CS_LAB_CACHE_PATH/huggingface/models/<id>/ model.save_pretrained()
|
|
144
|
+
$CS_LAB_CACHE_PATH/huggingface/tokenizers/<id>/
|
|
145
|
+
$CS_LAB_CACHE_PATH/huggingface/processors/<id>/
|
|
146
|
+
$CS_LAB_CACHE_PATH/huggingface/datasets/<id>[-<name>]/<split>/ dataset.save_to_disk()
|
|
147
|
+
$CS_LAB_CACHE_PATH/huggingface/ HF cache_dir used for fallbacks
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
A directory is used only when it contains the marker `.downloaded.ok`, written
|
|
151
|
+
after a successful download, so a half-copied model is never picked up.
|
|
152
|
+
|
|
153
|
+
**Without `CS_LAB_CACHE_PATH`, `cs-lab` adds no caching of its own.**
|
|
154
|
+
`load_hf_model("gpt2", cls, **kw)` is then exactly `cls.from_pretrained("gpt2", **kw)`,
|
|
155
|
+
and `load_hf_dataset(...)` exactly `datasets.load_dataset(...)`: the usual
|
|
156
|
+
HuggingFace cache (`~/.cache/huggingface`, `HF_HOME`) applies as it always does,
|
|
157
|
+
and `cs-lab cache download` merely warms it. Notebooks can therefore import from
|
|
158
|
+
`cs_lab` unconditionally and run unchanged on a laptop or on Colab; only the
|
|
159
|
+
classroom machines set the variable.
|
|
160
|
+
|
|
161
|
+
## Declaring and downloading resources
|
|
162
|
+
|
|
163
|
+
A course lists what it needs as `{section: [resources]}`:
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
# mycourse/resources.py
|
|
167
|
+
from cs_lab import (
|
|
168
|
+
make_hf_model_resource,
|
|
169
|
+
make_hf_tokenizer_resource,
|
|
170
|
+
make_hf_processor_resource,
|
|
171
|
+
make_hf_dataset_resource,
|
|
172
|
+
make_pyterrier_dataset_resource,
|
|
173
|
+
make_datamaestro_resource,
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
RESOURCES = {
|
|
177
|
+
"practical1": [
|
|
178
|
+
make_hf_model_resource("gpt2", model_class="GPT2LMHeadModel"),
|
|
179
|
+
make_hf_tokenizer_resource("gpt2", tokenizer_class="GPT2Tokenizer"),
|
|
180
|
+
make_hf_dataset_resource("imdb", ["train", "test"]),
|
|
181
|
+
],
|
|
182
|
+
"practical2": [
|
|
183
|
+
make_hf_model_resource(
|
|
184
|
+
"Qwen/Qwen2.5-7B-Instruct",
|
|
185
|
+
model_class="AutoModelForCausalLM",
|
|
186
|
+
optional=True,
|
|
187
|
+
),
|
|
188
|
+
make_pyterrier_dataset_resource(
|
|
189
|
+
"irds:lotte/technology/dev/search", "LoTTE technology"
|
|
190
|
+
),
|
|
191
|
+
],
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
then, on the machine that hosts the cache:
|
|
196
|
+
|
|
197
|
+
```sh
|
|
198
|
+
export CS_LAB_CACHE_PATH=/shared/cache
|
|
199
|
+
cs-lab cache info
|
|
200
|
+
cs-lab cache list --from mycourse.resources:RESOURCES
|
|
201
|
+
cs-lab cache download --from mycourse.resources:RESOURCES # everything but optional
|
|
202
|
+
cs-lab cache download --from mycourse.resources:RESOURCES --section practical2 --optional
|
|
203
|
+
cs-lab cache download --from mycourse.resources:RESOURCES --key gpt2
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
`--from MODULE:ATTR` imports `MODULE` and reads `ATTR` from it: a
|
|
207
|
+
`{section: [resources]}` mapping, or a zero-argument callable returning one
|
|
208
|
+
(dotted attributes such as `plugin.Course.resources` are followed). A `.py`
|
|
209
|
+
path works too and needs nothing on `sys.path`, which is the easy way to reach
|
|
210
|
+
a declaration that lives in a course's `src/`:
|
|
211
|
+
|
|
212
|
+
```sh
|
|
213
|
+
cs-lab cache list --from src/mycourse/resources.py # reads RESOURCES
|
|
214
|
+
cs-lab cache download --from src/mycourse/resources.py:get_resources
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
`RESOURCES` above is only a naming convention. The option can be repeated. Resources are
|
|
218
|
+
identified by `(type, key)`, so a model shared by several practicals is
|
|
219
|
+
downloaded once. `HF_HUB_OFFLINE` is lifted for the duration of a download.
|
|
220
|
+
|
|
221
|
+
The same helpers are available from Python (`download_resources`,
|
|
222
|
+
`select_resources`, `format_resources`, `merge_resources`), and any object with
|
|
223
|
+
`resource_type`, `key`, `description`, `optional` and `download()` is a valid
|
|
224
|
+
resource (`FunctionalResource` wraps a plain function).
|
|
225
|
+
|
|
226
|
+
## Compute profiles
|
|
227
|
+
|
|
228
|
+
How much compute a notebook should spend is a choice a reader makes — a smoke
|
|
229
|
+
test on a laptop, a full run on an A100 — and it is separate from what the
|
|
230
|
+
machine offers (CUDA or MPS, which dtype, whether `bitsandbytes` imports).
|
|
231
|
+
`Profile` covers the first question only.
|
|
232
|
+
|
|
233
|
+
The ladder is course material, not library material, so each course declares
|
|
234
|
+
its own rungs by subclassing:
|
|
235
|
+
|
|
236
|
+
```python
|
|
237
|
+
from cs_lab import Profile as BaseProfile
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
class Profile(BaseProfile):
|
|
241
|
+
FAST_TEST = 0
|
|
242
|
+
SMALL = 1
|
|
243
|
+
LOW_GPU = 2
|
|
244
|
+
HIGH_GPU = 3
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
Notebooks then size themselves with `pick`:
|
|
248
|
+
|
|
249
|
+
```python
|
|
250
|
+
MODEL_NAME = Profile.pick(
|
|
251
|
+
fast_test="HuggingFaceTB/SmolLM2-135M-Instruct",
|
|
252
|
+
small="Qwen/Qwen2.5-0.5B-Instruct",
|
|
253
|
+
low_gpu="Qwen/Qwen2.5-1.5B-Instruct",
|
|
254
|
+
)
|
|
255
|
+
n_queries = Profile.pick(200, fast_test=8, small=40)
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
`pick` resolves when it is called, never at import, so changing profile and
|
|
259
|
+
re-running a cell does what it looks like it does. A rung with no value of its
|
|
260
|
+
own takes the nearest one below it — above, if there is nothing below — and a
|
|
261
|
+
positional default stands for every rung left unspecified. Rungs are ordered,
|
|
262
|
+
so `Profile.current() >= Profile.LOW_GPU` is the way to gate a section.
|
|
263
|
+
|
|
264
|
+
`NOTEBOOK_PROFILE` gives the starting rung by name (`fast-test`, `FAST_TEST`
|
|
265
|
+
and `fast gpu` are all read the same way); `Profile.set(...)` still wins
|
|
266
|
+
afterwards, and `Profile.select()` shows an `ipywidgets` chooser in a notebook.
|
|
267
|
+
Set `NOTEBOOK_PROFILE_WIDGET=0` to suppress it. Detecting the machine is
|
|
268
|
+
somebody else's job: whoever does it calls `Profile.set_detected(...)`, or a
|
|
269
|
+
course overrides `detect(hardware)` to map its own hardware to a rung.
|
|
270
|
+
|
|
271
|
+
Keeping `Profile` here, rather than in each course, is what lets `scan` read a
|
|
272
|
+
ladder without importing anything: it finds the `class X(Profile)` statement,
|
|
273
|
+
learns the rung names and their order from it, and resolves every `X.pick(...)`
|
|
274
|
+
call into the models it may load. Of those, the largest rung is required — it
|
|
275
|
+
is what the ladder resolves to when nothing selects a profile — and the smaller
|
|
276
|
+
ones come out `optional=True`.
|
|
277
|
+
|
|
278
|
+
## The machine underneath
|
|
279
|
+
|
|
280
|
+
`Profile` says how much work to do; `hardware()` says what to do it on. The two
|
|
281
|
+
kept being confused — a notebook writing `device.type == "cuda"` to mean "is
|
|
282
|
+
this machine big" when it meant "does `bitsandbytes` exist here", and an Apple
|
|
283
|
+
Silicon laptop with 128 GB of unified memory coming out on the wrong side of
|
|
284
|
+
both.
|
|
285
|
+
|
|
286
|
+
```python
|
|
287
|
+
from cs_lab import hardware
|
|
288
|
+
from mycourse.profiles import Profile
|
|
289
|
+
|
|
290
|
+
hw = hardware(Profile) # in a notebook: also shows the profile chooser
|
|
291
|
+
model = load_hf_model(MODEL, AutoModelForCausalLM, dtype=hw.dtype).to(hw.device)
|
|
292
|
+
|
|
293
|
+
if hw.has_vllm: # the library imports *and* the backend supports it
|
|
294
|
+
...
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
- `hw.device` / `hw.backend` (`cuda` | `mps` | `cpu`) / `hw.total_memory_gb`.
|
|
298
|
+
**MPS counts as a GPU.**
|
|
299
|
+
- `hw.dtype` for inference (bf16 on CUDA, fp16 on MPS, fp32 on CPU) and
|
|
300
|
+
`hw.train_dtype` for training (bf16 on CUDA, fp32 elsewhere — LoRA in pure
|
|
301
|
+
fp16, without bf16's range, is unstable).
|
|
302
|
+
- `hw.has_bitsandbytes` / `hw.has_vllm` / `hw.has_flash_attention` /
|
|
303
|
+
`hw.supports_bf16`: each is *the library imports* **and** *the backend
|
|
304
|
+
supports it*.
|
|
305
|
+
- `hw.synchronize()` / `hw.empty_cache()` / `hw.memory_used_gb()`, so a
|
|
306
|
+
notebook stops writing the per-device branches by hand.
|
|
307
|
+
- `NOTEBOOK_BACKEND=cpu` forces a backend, to reproduce a CPU-only run on a
|
|
308
|
+
machine that has an accelerator.
|
|
309
|
+
|
|
310
|
+
Handing the ladder to `hardware()` is what connects the two: its `detect()`
|
|
311
|
+
maps this machine to a rung, recorded as the default — `NOTEBOOK_PROFILE` and
|
|
312
|
+
the chooser still win. In a notebook it also prints where things stand:
|
|
313
|
+
|
|
314
|
+
```
|
|
315
|
+
Profile: FAST_TEST — tiny budgets
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
`add_state_note(callable)` adds to that line. That is how
|
|
319
|
+
jupytext-notebook-helper appends `· images: off` when an author runs a source
|
|
320
|
+
with figures turned off; a student's notebook, which does not have that
|
|
321
|
+
package, simply shows the profile.
|
|
322
|
+
|
|
323
|
+
`torch` is imported lazily, so a course that installs this package for its
|
|
324
|
+
cache alone never pays for it.
|
|
325
|
+
|
|
326
|
+
## Keeping the declaration honest
|
|
327
|
+
|
|
328
|
+
The declaration is written by hand, so it drifts: a notebook gains a model, an
|
|
329
|
+
old one stops being loaded, and the classroom cache is wrong on the morning it
|
|
330
|
+
matters. `scan` reads the loader calls back out of the sources, and `check`
|
|
331
|
+
compares them with the declaration:
|
|
332
|
+
|
|
333
|
+
```sh
|
|
334
|
+
cs-lab cache scan sources/ # what the sources load
|
|
335
|
+
cs-lab cache scan sources/ --emit practical2 # a declaration skeleton to fill in
|
|
336
|
+
cs-lab cache check sources/ --declaration mycourse/resources.py # exit 1 on drift
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
A section is a source file name (`sources/02-generation.py` -> `02-generation`),
|
|
340
|
+
which is what `--section` takes. Both commands accept files or directories
|
|
341
|
+
(a directory contributes its top-level `*.py`, `_`-prefixed excluded), and
|
|
342
|
+
`--search-path DIR` lets an imported helper module be scanned as part of the
|
|
343
|
+
file that imports it — for course code split between a notebook and a library.
|
|
344
|
+
|
|
345
|
+
What the scan understands: `load_hf_model` / `load_hf_tokenizer` /
|
|
346
|
+
`load_hf_processor` / `load_hf_dataset` / `HFModel` / `pt.get_dataset` /
|
|
347
|
+
`prepare_dataset`, with module-level string constants resolved
|
|
348
|
+
(`MODEL = "gpt2"` … `load_hf_model(MODEL, …)`). A constant rebound under a
|
|
349
|
+
guard — `if test_mode:` by default, `--guard NAME` for another one — becomes an
|
|
350
|
+
`optional=True` resource, since a small stand-in used while testing has no
|
|
351
|
+
business filling a classroom cache. A profile ladder (below) says the same
|
|
352
|
+
thing in one expression and is read the same way. Plain `load_dataset` and
|
|
353
|
+
`Class.from_pretrained` calls are reported as *bypasses*: they do not go through
|
|
354
|
+
the cache, so `check` never asks for them to be declared.
|
|
355
|
+
|
|
356
|
+
`check` reports as **errors** anything loaded but not declared, or declared but
|
|
357
|
+
never loaded, and as **warnings** a class or `optional` mismatch. Descriptions
|
|
358
|
+
and dataset *splits* are left alone: code that loads every split says nothing
|
|
359
|
+
about their names.
|
|
360
|
+
|
|
361
|
+
In a Makefile:
|
|
362
|
+
|
|
363
|
+
```make
|
|
364
|
+
check-resources:
|
|
365
|
+
cs-lab cache check sources/ --declaration mycourse/resources.py
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
## Checking a cache before class
|
|
369
|
+
|
|
370
|
+
```sh
|
|
371
|
+
CS_LAB_CACHE_PATH=/shared/cache CS_LAB_CACHE_ENFORCE=1 python practical1.py
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
fails at the first resource that would have gone to the Hub.
|