AlaBoussoffara commited on
Commit
8a5d5fa
·
1 Parent(s): 1636bb1

updated readme

Browse files
Files changed (2) hide show
  1. README.md +155 -28
  2. README.project.md +0 -155
README.md CHANGED
@@ -1,28 +1,155 @@
1
- ---
2
- title: Mini Transformer Demo
3
- emoji: 🤖
4
- colorFrom: yellow
5
- colorTo: purple
6
- sdk: docker
7
- app_file: app.py
8
- pinned: false
9
- license: mit
10
- ---
11
-
12
- # Mini Transformer Demo
13
-
14
- Chainlit-powered dialogue interface that loads a mini encoder–decoder Transformer from `trained_models/` and serves it via Hugging Face Spaces. If you want the full project write-up, see [README.project.md](README.project.md).
15
-
16
- ## Quickstart
17
- - Install deps: `pip install -r requirements.txt`.
18
- - Launch locally: `chainlit run app.py --host 0.0.0.0 --port 7860`.
19
- - Push to the Space: `git push hf main` (build triggers automatically).
20
- - Visit the Space URL once the build shows as *Running*.
21
-
22
- ## Configuration
23
- - Place checkpoints under `trained_models/<model-name>/...` following the structure described in `README.project.md`.
24
- - Tweak Chainlit defaults with `.chainlit/config.toml`.
25
- - Use environment variables (e.g. `MINI_TRANSFORMER_MODELS`, `MINI_TRANSFORMER_MODEL_NAME`) to preselect models and configs.
26
-
27
- ## Support
28
- Open an issue in this repo for bugs or feature requests. For deployment questions, check the [Spaces docs](https://huggingface.co/docs/hub/spaces-overview).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Mini-Transformer
2
+
3
+ A compact, encoder–decoder Transformer packaged so it can be used both as a Python library and as a ready-to-run demo. The repository mirrors the installable package layout so cloning the repo or `pip install mini-transformer` gives the same structure and tooling.
4
+
5
+ ## Highlights
6
+ - Typed, unit-tested implementation of an encoder–decoder Transformer with reusable building blocks (`mini_transformer/modules`).
7
+ - Hydra configuration system with both packaged defaults (`mini_transformer/conf`) and editable configs in the repo.
8
+ - Ready-made UIs: FastAPI REST server and Chainlit chat interface.
9
+ - CLI convenience commands for inference, serving, UI launch, and Hugging Face model downloads.
10
+ - Makefile shortcuts, notebooks, and Docker dev environment for day-to-day work.
11
+
12
+ ## Installation
13
+ ```bash
14
+ # inside the repository (editable install with optional extras)
15
+ pip install -e .[server,viz]
16
+ ```
17
+ Or install the published package:
18
+ ```bash
19
+ pip install "mini-transformer[server,viz]"
20
+ ```
21
+
22
+ ## Repository Layout
23
+ - `src/mini_transformer/` installable package (model code, CLI, apps, packaged Hydra configs).
24
+ - `configs/` editable Hydra configs for local experiments.
25
+ - `trained_models/` place downloaded or exported checkpoints here (see below).
26
+ - `notebooks/` – exploratory notebooks (`train.ipynb`, `tokenizer.ipynb`, etc.).
27
+ - `tests/` – unit and smoke tests.
28
+ - Supporting files: `pyproject.toml`, `Makefile`, `Dockerfile.dev`, `environment.yml`, etc.
29
+
30
+ ## Training Notebook
31
+ `notebooks/train.ipynb` demonstrates the Hydra-driven training loop used during development.
32
+ - All hyper-parameters come from the composed config (see `configs/train_mode.yaml`); the key knobs live under `trainer.*`.
33
+ - Checkpoint cadence is controlled by `trainer.save_interval` (in optimizer steps) and always saves on epoch boundaries.
34
+ - Dataloader behaviour (workers, pinned memory) can be tuned via `trainer.num_workers` and `trainer.pin_memory`.
35
+ - Gradient accumulation is respected even for partial micro-batch sets, so you can safely mix different batch counts.
36
+ For long-running jobs consider exporting the notebook to a script (`jupyter nbconvert --to script`) or reusing the same logic inside a CLI tool.
37
+
38
+ ## Preparing Models
39
+ Download the demo models hosted on Hugging Face:
40
+ ```bash
41
+ mini-transformer-fetch AlaBoussoffara/transformer_test
42
+ mini-transformer-fetch AlaBoussoffara/transformer_small
43
+ ```
44
+ Use `--name` to customise the local directory and `--force` to refresh an existing download.
45
+ The local folder name defaults to `<author>__<repo>` (e.g. `AlaBoussoffara__transformer_small`). The Hugging Face repo is downloaded as a whole, so if the model files live inside a subfolder (for example `transformer_small/small_model_v1/`), move that entire inner folder—the one that already contains `configs/`, `checkpoints/`, and `tokenizer/`—into `trained_models/` so it becomes your model directory. In practice, after fetching `AlaBoussoffara/transformer_small`, move the `transformer_small/small_model_v1/` directory into `trained_models/` and rename it to the directory name you want to use (for example `AlaBoussoffara__transformer_small/`) so that the final layout is `trained_models/<model-name>/configs/...`.
46
+
47
+ You can also place your own model manually under `trained_models/<model-name>/`:
48
+ ```
49
+ trained_models/
50
+ my-model/
51
+ configs/
52
+ config_inference.yaml
53
+ checkpoints/
54
+ best.pt
55
+ tokenizer/
56
+ tokenizer.json
57
+ ```
58
+ Relative paths in `config_inference.yaml` should stay inside the model folder. Set `MINI_TRANSFORMER_MODELS=/path/to/trained_models` if you store models elsewhere.
59
+
60
+ ## CLI Usage
61
+ Install the optional extras (see Installation) and use the commands below.
62
+
63
+ ```bash
64
+ mini-transformer-infer --model AlaBoussoffara__transformer_small -t "Once upon a time"
65
+ mini-transformer-serve --model AlaBoussoffara__transformer_small --reload
66
+ mini-transformer-ui --model AlaBoussoffara__transformer_small --host 0.0.0.0 --port 8000
67
+ ```
68
+ If you see “Tokenizer file not found”, update the model config or set `MINI_TRANSFORMER_TOKENIZER_PATH` to the correct JSON file.
69
+
70
+ ## Chainlit Demo
71
+ Follow these steps to spin up the bundled Chainlit chat UI for a local inference demo.
72
+
73
+ 1. Install the project with the server extras (or run `make create-env`):
74
+ ```bash
75
+ pip install -e .[server]
76
+ ```
77
+ 2. Download a demo checkpoint (skip if you already have one under `trained_models/`):
78
+ ```bash
79
+ mini-transformer-fetch AlaBoussoffara/transformer_small
80
+ ```
81
+ 3. Launch the Chainlit UI, pointing to the model folder you want to use (defaults to the first available model):
82
+ ```bash
83
+ mini-transformer-ui --model AlaBoussoffara__transformer_small --host 0.0.0.0 --port 8000
84
+ ```
85
+ 4. Open http://localhost:8000 in your browser, send a prompt, and use `/model` in chat to switch between downloaded checkpoints.
86
+ Use `/config temperature=0.7 top_k=50` (or `/config reset`) to tweak generation settings on the fly.
87
+
88
+ ### Environment Variables
89
+ - `MINI_TRANSFORMER_MODELS` – override the models root directory.
90
+ - `MINI_TRANSFORMER_MODEL_NAME` – preselect a model for the server/UI.
91
+ - `MINI_TRANSFORMER_CONFIG_DIR` / `MINI_TRANSFORMER_CONFIG_NAME` – point to custom Hydra configs.
92
+ - `MINI_TRANSFORMER_UI_HOST` / `MINI_TRANSFORMER_UI_PORT` – defaults for Chainlit binding.
93
+ - Optional overrides: `MINI_TRANSFORMER_CHECKPOINT_BEST`, `MINI_TRANSFORMER_TOKENIZER_PATH`, `MINI_TRANSFORMER_OUTPUT_DIR`, etc.
94
+
95
+ ## Docker Inference UI
96
+ Build the lean inference image and launch the Chainlit UI in a container:
97
+ ```bash
98
+ make docker-build-infer
99
+ make docker-run-infer # press Ctrl+C to stop
100
+ ```
101
+ Models stay outside the container under `trained_models/`, mounted at runtime with docker compose.
102
+
103
+ ## Programmatic Inference
104
+ ```python
105
+ from mini_transformer.model_loader import compose_model_config
106
+ from mini_transformer.inference import run_inference
107
+
108
+ cfg = compose_model_config("AlaBoussoffara__transformer_small")
109
+ cfg.input_text = "Hello world"
110
+ print(run_inference(cfg)[0])
111
+ ```
112
+
113
+ ## Development Quickstart
114
+ ```bash
115
+ make create-env
116
+ make lint
117
+ make type
118
+ make test
119
+ pre-commit run --all-files # optional: run all hooks locally
120
+ ```
121
+
122
+ ## Testing & QA
123
+ - Unit tests live under `tests/units/`; run them with `make test` or `python -m pytest`.
124
+ - The suite covers core building blocks (attention math, masking, sampling), CLI flows, and attention-debug utilities—including both pre- and post-layernorm configurations.
125
+ - Add tests alongside new features; keeping coverage high ensures `mini-transformer` behaves the same whether it runs from the repo or as an installed package.
126
+
127
+ ## Makefile Shortcuts
128
+ ```bash
129
+ make help # list common tasks
130
+ make create-env # create/update the conda env and install extras
131
+ make lint # format + lint (ruff + black)
132
+ make lint-check # lint without auto-fixes
133
+ make fmt # format code
134
+ make precommit # run the configured pre-commit hooks (auto-fixes where possible)
135
+ make type # mypy
136
+ make test # pytest
137
+ make cov # pytest with coverage
138
+ make fetch-test # download AlaBoussoffara/transformer_test
139
+ make fetch-small # download AlaBoussoffara/transformer_small
140
+ make infer # quick demo inference
141
+ make serve # run FastAPI server (reload mode)
142
+ make ui # launch Chainlit UI
143
+ make docker-build-dev # build the development container
144
+ make docker-run-dev # open an interactive shell in the development container
145
+ make docker-build-infer # build the inference/UI container
146
+ make docker-run-infer # run the inference service (Ctrl+C to stop)
147
+ ```
148
+
149
+ ## Full Workflow At A Glance
150
+ 1. Fetch a model: `mini-transformer-fetch AlaBoussoffara/transformer_small`
151
+ 2. Run inference: `mini-transformer-infer --model AlaBoussoffara__transformer_small -t "Once upon a time"`
152
+ 3. Launch FastAPI (optional): `mini-transformer-serve --model AlaBoussoffara__transformer_small --reload`
153
+ 4. Start Chainlit UI (optional): `mini-transformer-ui --model AlaBoussoffara__transformer_small --host 0.0.0.0 --port 8000`
154
+
155
+ Environment variables (`MINI_TRANSFORMER_MODELS`, `MINI_TRANSFORMER_MODEL_NAME`, etc.) let you tailor the workflow to your setup.
README.project.md DELETED
@@ -1,155 +0,0 @@
1
- # Mini-Transformer
2
-
3
- A compact, encoder–decoder Transformer packaged so it can be used both as a Python library and as a ready-to-run demo. The repository mirrors the installable package layout so cloning the repo or `pip install mini-transformer` gives the same structure and tooling.
4
-
5
- ## Highlights
6
- - Typed, unit-tested implementation of an encoder–decoder Transformer with reusable building blocks (`mini_transformer/modules`).
7
- - Hydra configuration system with both packaged defaults (`mini_transformer/conf`) and editable configs in the repo.
8
- - Ready-made UIs: FastAPI REST server and Chainlit chat interface.
9
- - CLI convenience commands for inference, serving, UI launch, and Hugging Face model downloads.
10
- - Makefile shortcuts, notebooks, and Docker dev environment for day-to-day work.
11
-
12
- ## Installation
13
- ```bash
14
- # inside the repository (editable install with optional extras)
15
- pip install -e .[server,viz]
16
- ```
17
- Or install the published package:
18
- ```bash
19
- pip install "mini-transformer[server,viz]"
20
- ```
21
-
22
- ## Repository Layout
23
- - `src/mini_transformer/` – installable package (model code, CLI, apps, packaged Hydra configs).
24
- - `configs/` – editable Hydra configs for local experiments.
25
- - `trained_models/` – place downloaded or exported checkpoints here (see below).
26
- - `notebooks/` – exploratory notebooks (`train.ipynb`, `tokenizer.ipynb`, etc.).
27
- - `tests/` – unit and smoke tests.
28
- - Supporting files: `pyproject.toml`, `Makefile`, `Dockerfile.dev`, `environment.yml`, etc.
29
-
30
- ## Training Notebook
31
- `notebooks/train.ipynb` demonstrates the Hydra-driven training loop used during development.
32
- - All hyper-parameters come from the composed config (see `configs/train_mode.yaml`); the key knobs live under `trainer.*`.
33
- - Checkpoint cadence is controlled by `trainer.save_interval` (in optimizer steps) and always saves on epoch boundaries.
34
- - Dataloader behaviour (workers, pinned memory) can be tuned via `trainer.num_workers` and `trainer.pin_memory`.
35
- - Gradient accumulation is respected even for partial micro-batch sets, so you can safely mix different batch counts.
36
- For long-running jobs consider exporting the notebook to a script (`jupyter nbconvert --to script`) or reusing the same logic inside a CLI tool.
37
-
38
- ## Preparing Models
39
- Download the demo models hosted on Hugging Face:
40
- ```bash
41
- mini-transformer-fetch AlaBoussoffara/transformer_test
42
- mini-transformer-fetch AlaBoussoffara/transformer_small
43
- ```
44
- Use `--name` to customise the local directory and `--force` to refresh an existing download.
45
- The local folder name defaults to `<author>__<repo>` (e.g. `AlaBoussoffara__transformer_small`). The Hugging Face repo is downloaded as a whole, so if the model files live inside a subfolder (for example `transformer_small/small_model_v1/`), move that entire inner folder—the one that already contains `configs/`, `checkpoints/`, and `tokenizer/`—into `trained_models/` so it becomes your model directory. In practice, after fetching `AlaBoussoffara/transformer_small`, move the `transformer_small/small_model_v1/` directory into `trained_models/` and rename it to the directory name you want to use (for example `AlaBoussoffara__transformer_small/`) so that the final layout is `trained_models/<model-name>/configs/...`.
46
-
47
- You can also place your own model manually under `trained_models/<model-name>/`:
48
- ```
49
- trained_models/
50
- my-model/
51
- configs/
52
- config_inference.yaml
53
- checkpoints/
54
- best.pt
55
- tokenizer/
56
- tokenizer.json
57
- ```
58
- Relative paths in `config_inference.yaml` should stay inside the model folder. Set `MINI_TRANSFORMER_MODELS=/path/to/trained_models` if you store models elsewhere.
59
-
60
- ## CLI Usage
61
- Install the optional extras (see Installation) and use the commands below.
62
-
63
- ```bash
64
- mini-transformer-infer --model AlaBoussoffara__transformer_small -t "Once upon a time"
65
- mini-transformer-serve --model AlaBoussoffara__transformer_small --reload
66
- mini-transformer-ui --model AlaBoussoffara__transformer_small --host 0.0.0.0 --port 8000
67
- ```
68
- If you see “Tokenizer file not found”, update the model config or set `MINI_TRANSFORMER_TOKENIZER_PATH` to the correct JSON file.
69
-
70
- ## Chainlit Demo
71
- Follow these steps to spin up the bundled Chainlit chat UI for a local inference demo.
72
-
73
- 1. Install the project with the server extras (or run `make create-env`):
74
- ```bash
75
- pip install -e .[server]
76
- ```
77
- 2. Download a demo checkpoint (skip if you already have one under `trained_models/`):
78
- ```bash
79
- mini-transformer-fetch AlaBoussoffara/transformer_small
80
- ```
81
- 3. Launch the Chainlit UI, pointing to the model folder you want to use (defaults to the first available model):
82
- ```bash
83
- mini-transformer-ui --model AlaBoussoffara__transformer_small --host 0.0.0.0 --port 8000
84
- ```
85
- 4. Open http://localhost:8000 in your browser, send a prompt, and use `/model` in chat to switch between downloaded checkpoints.
86
- Use `/config temperature=0.7 top_k=50` (or `/config reset`) to tweak generation settings on the fly.
87
-
88
- ### Environment Variables
89
- - `MINI_TRANSFORMER_MODELS` – override the models root directory.
90
- - `MINI_TRANSFORMER_MODEL_NAME` – preselect a model for the server/UI.
91
- - `MINI_TRANSFORMER_CONFIG_DIR` / `MINI_TRANSFORMER_CONFIG_NAME` – point to custom Hydra configs.
92
- - `MINI_TRANSFORMER_UI_HOST` / `MINI_TRANSFORMER_UI_PORT` – defaults for Chainlit binding.
93
- - Optional overrides: `MINI_TRANSFORMER_CHECKPOINT_BEST`, `MINI_TRANSFORMER_TOKENIZER_PATH`, `MINI_TRANSFORMER_OUTPUT_DIR`, etc.
94
-
95
- ## Docker Inference UI
96
- Build the lean inference image and launch the Chainlit UI in a container:
97
- ```bash
98
- make docker-build-infer
99
- make docker-run-infer # press Ctrl+C to stop
100
- ```
101
- Models stay outside the container under `trained_models/`, mounted at runtime with docker compose.
102
-
103
- ## Programmatic Inference
104
- ```python
105
- from mini_transformer.model_loader import compose_model_config
106
- from mini_transformer.inference import run_inference
107
-
108
- cfg = compose_model_config("AlaBoussoffara__transformer_small")
109
- cfg.input_text = "Hello world"
110
- print(run_inference(cfg)[0])
111
- ```
112
-
113
- ## Development Quickstart
114
- ```bash
115
- make create-env
116
- make lint
117
- make type
118
- make test
119
- pre-commit run --all-files # optional: run all hooks locally
120
- ```
121
-
122
- ## Testing & QA
123
- - Unit tests live under `tests/units/`; run them with `make test` or `python -m pytest`.
124
- - The suite covers core building blocks (attention math, masking, sampling), CLI flows, and attention-debug utilities—including both pre- and post-layernorm configurations.
125
- - Add tests alongside new features; keeping coverage high ensures `mini-transformer` behaves the same whether it runs from the repo or as an installed package.
126
-
127
- ## Makefile Shortcuts
128
- ```bash
129
- make help # list common tasks
130
- make create-env # create/update the conda env and install extras
131
- make lint # format + lint (ruff + black)
132
- make lint-check # lint without auto-fixes
133
- make fmt # format code
134
- make precommit # run the configured pre-commit hooks (auto-fixes where possible)
135
- make type # mypy
136
- make test # pytest
137
- make cov # pytest with coverage
138
- make fetch-test # download AlaBoussoffara/transformer_test
139
- make fetch-small # download AlaBoussoffara/transformer_small
140
- make infer # quick demo inference
141
- make serve # run FastAPI server (reload mode)
142
- make ui # launch Chainlit UI
143
- make docker-build-dev # build the development container
144
- make docker-run-dev # open an interactive shell in the development container
145
- make docker-build-infer # build the inference/UI container
146
- make docker-run-infer # run the inference service (Ctrl+C to stop)
147
- ```
148
-
149
- ## Full Workflow At A Glance
150
- 1. Fetch a model: `mini-transformer-fetch AlaBoussoffara/transformer_small`
151
- 2. Run inference: `mini-transformer-infer --model AlaBoussoffara__transformer_small -t "Once upon a time"`
152
- 3. Launch FastAPI (optional): `mini-transformer-serve --model AlaBoussoffara__transformer_small --reload`
153
- 4. Start Chainlit UI (optional): `mini-transformer-ui --model AlaBoussoffara__transformer_small --host 0.0.0.0 --port 8000`
154
-
155
- Environment variables (`MINI_TRANSFORMER_MODELS`, `MINI_TRANSFORMER_MODEL_NAME`, etc.) let you tailor the workflow to your setup.