Spaces:
Sleeping
Sleeping
Commit ·
6e475f4
1
Parent(s): 2d52135
fixes
Browse files- CONTRIBUTING.md +1 -1
- Makefile +2 -2
- README.md +7 -8
- src/mini_transformer/modules/attention.py +1 -1
- src/mini_transformer/transformer.py +1 -1
- src/mini_transformer/utils.py +1 -1
CONTRIBUTING.md
CHANGED
|
@@ -4,7 +4,7 @@ Thanks for your interest in contributing! Follow these steps to get started.
|
|
| 4 |
|
| 5 |
1. **Create the development environment**
|
| 6 |
```bash
|
| 7 |
-
make
|
| 8 |
```
|
| 9 |
|
| 10 |
2. **Run quality checks** before opening a pull request:
|
|
|
|
| 4 |
|
| 5 |
1. **Create the development environment**
|
| 6 |
```bash
|
| 7 |
+
make create-env
|
| 8 |
```
|
| 9 |
|
| 10 |
2. **Run quality checks** before opening a pull request:
|
Makefile
CHANGED
|
@@ -61,10 +61,10 @@ cov:
|
|
| 61 |
$(CONDA_RUN) pytest --cov=mini_transformer --cov-report=term-missing
|
| 62 |
|
| 63 |
fetch-test:
|
| 64 |
-
$(CONDA_RUN) mini-transformer-fetch AlaBoussoffara/transformer_test --force
|
| 65 |
|
| 66 |
fetch-small:
|
| 67 |
-
$(CONDA_RUN) mini-transformer-fetch AlaBoussoffara/transformer_small --force
|
| 68 |
|
| 69 |
infer:
|
| 70 |
$(CONDA_RUN) mini-transformer-infer --model small_model_v1 -t "translate this sentence."
|
|
|
|
| 61 |
$(CONDA_RUN) pytest --cov=mini_transformer --cov-report=term-missing
|
| 62 |
|
| 63 |
fetch-test:
|
| 64 |
+
$(CONDA_RUN) mini-transformer-fetch AlaBoussoffara/transformer_test --name test_model_v1 --force
|
| 65 |
|
| 66 |
fetch-small:
|
| 67 |
+
$(CONDA_RUN) mini-transformer-fetch AlaBoussoffara/transformer_small --name small_model_v1 --force
|
| 68 |
|
| 69 |
infer:
|
| 70 |
$(CONDA_RUN) mini-transformer-infer --model small_model_v1 -t "translate this sentence."
|
README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
#
|
| 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 |
|
|
@@ -30,12 +30,11 @@ pip install "mini-transformer[server,viz]"
|
|
| 30 |
## Preparing Models
|
| 31 |
Download the demo models hosted on Hugging Face:
|
| 32 |
```bash
|
| 33 |
-
mini-transformer-fetch
|
| 34 |
-
mini-transformer-fetch
|
| 35 |
```
|
| 36 |
Use `--name` to customise the local directory and `--force` to refresh an existing download.
|
| 37 |
-
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/`),
|
| 38 |
-
In other words, after fetching `AlaBoussoffara/transformer_small`, move the `transformer_small/small_model_v1/` directory into `trained_models/AlaBoussoffara__transformer_small/`.
|
| 39 |
|
| 40 |
You can also place your own model manually under `trained_models/<model-name>/`:
|
| 41 |
```
|
|
@@ -69,7 +68,7 @@ Follow these steps to spin up the bundled Chainlit chat UI for a local inference
|
|
| 69 |
```
|
| 70 |
2. Download a demo checkpoint (skip if you already have one under `trained_models/`):
|
| 71 |
```bash
|
| 72 |
-
mini-transformer-fetch
|
| 73 |
```
|
| 74 |
3. Launch the Chainlit UI, pointing to the model folder you want to use (defaults to the first available model):
|
| 75 |
```bash
|
|
@@ -96,7 +95,7 @@ print(run_inference(cfg)[0])
|
|
| 96 |
|
| 97 |
## Development Quickstart
|
| 98 |
```bash
|
| 99 |
-
make
|
| 100 |
make lint
|
| 101 |
make type
|
| 102 |
make test
|
|
@@ -122,7 +121,7 @@ make docker-run # open an interactive shell in the container
|
|
| 122 |
```
|
| 123 |
|
| 124 |
## Full Workflow At A Glance
|
| 125 |
-
1. Fetch a model: `mini-transformer-fetch
|
| 126 |
2. Run inference: `mini-transformer-infer --model AlaBoussoffara__transformer_small -t "Once upon a time"`
|
| 127 |
3. Launch FastAPI (optional): `mini-transformer-serve --model AlaBoussoffara__transformer_small --reload`
|
| 128 |
4. Start Chainlit UI (optional): `mini-transformer-ui --model AlaBoussoffara__transformer_small --host 0.0.0.0 --port 8000`
|
|
|
|
| 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 |
|
|
|
|
| 30 |
## Preparing Models
|
| 31 |
Download the demo models hosted on Hugging Face:
|
| 32 |
```bash
|
| 33 |
+
mini-transformer-fetch AlaBoussoffara/transformer_test
|
| 34 |
+
mini-transformer-fetch AlaBoussoffara/transformer_small
|
| 35 |
```
|
| 36 |
Use `--name` to customise the local directory and `--force` to refresh an existing download.
|
| 37 |
+
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/...`.
|
|
|
|
| 38 |
|
| 39 |
You can also place your own model manually under `trained_models/<model-name>/`:
|
| 40 |
```
|
|
|
|
| 68 |
```
|
| 69 |
2. Download a demo checkpoint (skip if you already have one under `trained_models/`):
|
| 70 |
```bash
|
| 71 |
+
mini-transformer-fetch AlaBoussoffara/transformer_small
|
| 72 |
```
|
| 73 |
3. Launch the Chainlit UI, pointing to the model folder you want to use (defaults to the first available model):
|
| 74 |
```bash
|
|
|
|
| 95 |
|
| 96 |
## Development Quickstart
|
| 97 |
```bash
|
| 98 |
+
make create-env
|
| 99 |
make lint
|
| 100 |
make type
|
| 101 |
make test
|
|
|
|
| 121 |
```
|
| 122 |
|
| 123 |
## Full Workflow At A Glance
|
| 124 |
+
1. Fetch a model: `mini-transformer-fetch AlaBoussoffara/transformer_small`
|
| 125 |
2. Run inference: `mini-transformer-infer --model AlaBoussoffara__transformer_small -t "Once upon a time"`
|
| 126 |
3. Launch FastAPI (optional): `mini-transformer-serve --model AlaBoussoffara__transformer_small --reload`
|
| 127 |
4. Start Chainlit UI (optional): `mini-transformer-ui --model AlaBoussoffara__transformer_small --host 0.0.0.0 --port 8000`
|
src/mini_transformer/modules/attention.py
CHANGED
|
@@ -124,7 +124,7 @@ class MultiHeadAttention(nn.Module):
|
|
| 124 |
k = split_heads(self.key_linear(key), self.num_heads)
|
| 125 |
v = split_heads(self.value_linear(value), self.num_heads)
|
| 126 |
pad_mask = create_qk_padding_mask(q_mask, k_mask)
|
| 127 |
-
combined_mask = combine_masks(
|
| 128 |
|
| 129 |
p = self.dropout_rate if self.training else 0.0
|
| 130 |
attn = cast(Tensor, calculate_attention(q, k, v, combined_mask, dropout_p=p))
|
|
|
|
| 124 |
k = split_heads(self.key_linear(key), self.num_heads)
|
| 125 |
v = split_heads(self.value_linear(value), self.num_heads)
|
| 126 |
pad_mask = create_qk_padding_mask(q_mask, k_mask)
|
| 127 |
+
combined_mask = combine_masks(causal_mask, pad_mask)
|
| 128 |
|
| 129 |
p = self.dropout_rate if self.training else 0.0
|
| 130 |
attn = cast(Tensor, calculate_attention(q, k, v, combined_mask, dropout_p=p))
|
src/mini_transformer/transformer.py
CHANGED
|
@@ -127,7 +127,7 @@ class BasicEncoderDecoderTransformer(nn.Module):
|
|
| 127 |
f"src_ids must be a 2D torch.Tensor of shape (B, S), got shape {tuple(src_ids.shape)}"
|
| 128 |
)
|
| 129 |
if src_ids.dtype != torch.long:
|
| 130 |
-
raise TypeError(f"
|
| 131 |
|
| 132 |
if not isinstance(tgt_ids, Tensor):
|
| 133 |
raise TypeError(f"tgt_ids must be a torch.Tensor, got {type(tgt_ids)}")
|
|
|
|
| 127 |
f"src_ids must be a 2D torch.Tensor of shape (B, S), got shape {tuple(src_ids.shape)}"
|
| 128 |
)
|
| 129 |
if src_ids.dtype != torch.long:
|
| 130 |
+
raise TypeError(f"src_ids must be torch.long (int64), got {src_ids.dtype}")
|
| 131 |
|
| 132 |
if not isinstance(tgt_ids, Tensor):
|
| 133 |
raise TypeError(f"tgt_ids must be a torch.Tensor, got {type(tgt_ids)}")
|
src/mini_transformer/utils.py
CHANGED
|
@@ -511,7 +511,7 @@ def extract_all_attention_maps(
|
|
| 511 |
k = split_heads(self_mha.key_linear(y_in), self_mha.num_heads)
|
| 512 |
v = split_heads(self_mha.value_linear(y_in), self_mha.num_heads)
|
| 513 |
pad = create_qk_padding_mask(tgt_pad_b, tgt_pad_b)
|
| 514 |
-
combined_mask = combine_masks(
|
| 515 |
self_ctx, self_probs = calculate_attention(
|
| 516 |
q, k, v, combined_mask, dropout_p=0.0, return_probs=True
|
| 517 |
)
|
|
|
|
| 511 |
k = split_heads(self_mha.key_linear(y_in), self_mha.num_heads)
|
| 512 |
v = split_heads(self_mha.value_linear(y_in), self_mha.num_heads)
|
| 513 |
pad = create_qk_padding_mask(tgt_pad_b, tgt_pad_b)
|
| 514 |
+
combined_mask = combine_masks(tgt_causal, pad)
|
| 515 |
self_ctx, self_probs = calculate_attention(
|
| 516 |
q, k, v, combined_mask, dropout_p=0.0, return_probs=True
|
| 517 |
)
|