masklens 0.1.4__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.
Files changed (49) hide show
  1. masklens-0.1.4/.github/ISSUE_TEMPLATE/bug.yml +22 -0
  2. masklens-0.1.4/.github/ISSUE_TEMPLATE/question.yml +11 -0
  3. masklens-0.1.4/.github/workflows/ci.yml +22 -0
  4. masklens-0.1.4/.gitignore +38 -0
  5. masklens-0.1.4/CHANGELOG.md +38 -0
  6. masklens-0.1.4/LICENSE +21 -0
  7. masklens-0.1.4/PKG-INFO +368 -0
  8. masklens-0.1.4/README.ko.md +347 -0
  9. masklens-0.1.4/README.md +349 -0
  10. masklens-0.1.4/SECURITY.md +7 -0
  11. masklens-0.1.4/docs/images/check-fail.ko.png +0 -0
  12. masklens-0.1.4/docs/images/check-fail.png +0 -0
  13. masklens-0.1.4/docs/images/check-parts.ko.png +0 -0
  14. masklens-0.1.4/docs/images/check-parts.png +0 -0
  15. masklens-0.1.4/docs/images/check-truncate.ko.png +0 -0
  16. masklens-0.1.4/docs/images/check-truncate.png +0 -0
  17. masklens-0.1.4/docs/images/show-plain.ko.png +0 -0
  18. masklens-0.1.4/docs/images/show-plain.png +0 -0
  19. masklens-0.1.4/docs/images/show-training.ko.png +0 -0
  20. masklens-0.1.4/docs/images/show-training.png +0 -0
  21. masklens-0.1.4/examples/chat.jsonl +5 -0
  22. masklens-0.1.4/examples/tiny-chatml/tokenizer.json +1026 -0
  23. masklens-0.1.4/examples/tiny-chatml/tokenizer_config.json +12 -0
  24. masklens-0.1.4/examples/tiny-chatml/training.jinja +1 -0
  25. masklens-0.1.4/pyproject.toml +39 -0
  26. masklens-0.1.4/requirements-ci.txt +102 -0
  27. masklens-0.1.4/scripts/make_tiny_tokenizer.py +87 -0
  28. masklens-0.1.4/src/masklens/__init__.py +3 -0
  29. masklens-0.1.4/src/masklens/cli.py +139 -0
  30. masklens-0.1.4/src/masklens/i18n.py +264 -0
  31. masklens-0.1.4/src/masklens/loader.py +99 -0
  32. masklens-0.1.4/src/masklens/parts.py +26 -0
  33. masklens-0.1.4/src/masklens/render.py +192 -0
  34. masklens-0.1.4/src/masklens/report.py +216 -0
  35. masklens-0.1.4/src/masklens/rules.py +122 -0
  36. masklens-0.1.4/src/masklens/truncate.py +23 -0
  37. masklens-0.1.4/tests/conftest.py +65 -0
  38. masklens-0.1.4/tests/fixtures/bad.jsonl +3 -0
  39. masklens-0.1.4/tests/fixtures/drift.jinja +1 -0
  40. masklens-0.1.4/tests/fixtures/leaky.jinja +1 -0
  41. masklens-0.1.4/tests/fixtures/no_eos.jinja +1 -0
  42. masklens-0.1.4/tests/test_ci_workflow.py +43 -0
  43. masklens-0.1.4/tests/test_cli.py +124 -0
  44. masklens-0.1.4/tests/test_i18n.py +74 -0
  45. masklens-0.1.4/tests/test_parts.py +50 -0
  46. masklens-0.1.4/tests/test_real_tokenizers.py +28 -0
  47. masklens-0.1.4/tests/test_rules.py +84 -0
  48. masklens-0.1.4/tests/test_truncate.py +71 -0
  49. masklens-0.1.4/uv.lock +1073 -0
@@ -0,0 +1,22 @@
1
+ name: 버그 / Bug
2
+ description: README대로 했는데 다르게 동작해요 / Something does not work as the README says
3
+ labels: [bug]
4
+ body:
5
+ - type: textarea
6
+ id: what
7
+ attributes:
8
+ label: 무엇이 일어났나요? / What happened?
9
+ description: 실행한 명령과 나온 메시지를 그대로 붙여 주세요. / Paste the command and the output.
10
+ validations:
11
+ required: true
12
+ - type: textarea
13
+ id: expected
14
+ attributes:
15
+ label: 기대한 결과 / Expected
16
+ - type: input
17
+ id: env
18
+ attributes:
19
+ label: 환경 / Environment
20
+ placeholder: "macOS 15 (M3) · Ubuntu 24.04 · Windows 11 / Python 3.12 / masklens 0.1.4 / transformers 5.24.0"
21
+ validations:
22
+ required: true
@@ -0,0 +1,11 @@
1
+ name: 질문 / Question
2
+ description: 이해가 안 되는 부분이나 더 알고 싶은 점 / Something unclear or worth explaining
3
+ labels: [question]
4
+ body:
5
+ - type: textarea
6
+ id: question
7
+ attributes:
8
+ label: 질문 / Question
9
+ description: README의 어느 부분(절 제목)에 대한 질문인지 알려 주세요. / Which README section is this about?
10
+ validations:
11
+ required: true
@@ -0,0 +1,22 @@
1
+ # Tests run offline against the tiny tokenizer shipped in the repo (no model downloads).
2
+ name: ci
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ workflow_dispatch:
8
+ permissions:
9
+ contents: read
10
+ jobs:
11
+ test:
12
+ runs-on: ubuntu-latest
13
+ timeout-minutes: 15
14
+ env:
15
+ HF_HUB_OFFLINE: "1"
16
+ steps:
17
+ - uses: actions/checkout@v6
18
+ - uses: actions/setup-python@v6
19
+ with:
20
+ python-version: "3.13"
21
+ - run: python -m pip install -r requirements-ci.txt
22
+ - run: python -m pytest -q
@@ -0,0 +1,38 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ .venv/
5
+ .pytest_cache/
6
+ .ruff_cache/
7
+ .mypy_cache/
8
+ *.egg-info/
9
+
10
+ # Node
11
+ node_modules/
12
+ dist/
13
+ .next/
14
+
15
+ # 실행 산출물·모델·데이터 캐시
16
+ runs/
17
+ outputs/
18
+ .cache/
19
+ *.safetensors
20
+ *.gguf
21
+ *.bin
22
+
23
+ # 환경·비밀
24
+ .env
25
+ .env.*
26
+ !.env.example
27
+
28
+ # OS·에디터
29
+ .DS_Store
30
+ .vscode/
31
+ .idea/
32
+
33
+ # Flutter·Dart
34
+ .dart_tool/
35
+ build/
36
+ .flutter-plugins
37
+ .flutter-plugins-dependencies
38
+ coverage/
@@ -0,0 +1,38 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented in this file.
4
+ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses [Semantic Versioning](https://semver.org/).
5
+
6
+ ## [0.1.4] - 2026-11-23
7
+
8
+ ### Changed
9
+ - CI is a single job on Python 3.13 that installs the pinned `requirements-ci.txt` with pip and runs the offline tests; the scheduled run, the Python matrix, the uv setup and the diagnostic steps were removed.
10
+
11
+ ### Added
12
+ - `requirements-ci.txt` (exported from `uv.lock`) and `tests/test_ci_workflow.py`, which checks the workflow shape and that the pins match the lock file.
13
+
14
+ ## [0.1.3] - 2026-11-23
15
+
16
+ ### Changed
17
+ - CI prints the runner environment and every test outcome (`pytest -rA -v --tb=long`), and lists installed packages when a step fails.
18
+
19
+ ## [0.1.2] - 2026-11-23
20
+
21
+ ### Fixed
22
+ - Tests no longer depend on the terminal width, color settings or locale of the shell that runs them (the `--help` check failed when `COLUMNS` was narrow).
23
+ - CI uses only GitHub-maintained actions on Node 24 (`actions/checkout@v6`, `actions/setup-python@v6`), a pinned uv (`0.13.6`) and `HF_HUB_OFFLINE=1` for every step.
24
+
25
+ ## [0.1.1] - 2026-11-23
26
+
27
+ ### Fixed
28
+ - CI workflow: pinned the Python matrix and the uv setup step.
29
+
30
+ ## [0.1.0] - 2026-11-23
31
+
32
+ ### Added
33
+ - `masklens show`: one example with the trained tokens highlighted (`[[...]]` or ANSI color).
34
+ - `masklens check`: dataset-wide rules (`no_generation_tag`, `empty_mask`, `prompt_leak`, `render_mismatch`, `parts_not_found`, `response_truncated`, `untrained_turn`, `eos_not_trained`, plus the informational `content_not_found`), summary table, `--json` report and exit codes.
35
+ - Two mask methods: `--mode generation` and `--mode parts` (Unsloth-style marker strings).
36
+ - Own truncation with `--max-length` and `--truncation keep_start|keep_end`.
37
+ - English and Korean screens (`--lang`, `MASKLENS_LANG`, locale variables).
38
+ - Practice tokenizer, training template and sample data under `examples/`.
masklens-0.1.4/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 LeeGyeongPil
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.
@@ -0,0 +1,368 @@
1
+ Metadata-Version: 2.5
2
+ Name: masklens
3
+ Version: 0.1.4
4
+ Summary: Preflight check for SFT loss masks: verify which tokens a chat template, tokenizer and labels will actually train on, before you spend GPU time.
5
+ Project-URL: Homepage, https://github.com/LeeGyeongPil/masklens
6
+ Project-URL: Repository, https://github.com/LeeGyeongPil/masklens
7
+ Project-URL: Issues, https://github.com/LeeGyeongPil/masklens/issues
8
+ Author: LeeGyeongPil
9
+ License: MIT
10
+ License-File: LICENSE
11
+ Keywords: chat-template,fine-tuning,lora,loss-mask,sft,transformers
12
+ Classifier: Environment :: Console
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Requires-Python: >=3.11
16
+ Requires-Dist: jinja2>=3.1.6
17
+ Requires-Dist: transformers<6,>=5.24.0
18
+ Description-Content-Type: text/markdown
19
+
20
+ # masklens
21
+
22
+ **English** | [한국어](https://github.com/LeeGyeongPil/masklens/blob/v0.1.4/README.ko.md)
23
+
24
+ Preflight check for SFT loss masks: before you spend GPU time, see which tokens your chat template, tokenizer and labels will actually train on. It is a framework-independent CLI that needs only a tokenizer (no model weights, no GPU), and it works the same whether you train with TRL, Unsloth or your own loop.
25
+
26
+ ![Trained tokens highlighted by masklens show](https://raw.githubusercontent.com/LeeGyeongPil/masklens/v0.1.4/docs/images/show-training.png)
27
+ *`masklens show` wraps every trained token in `[[ ]]`: only the assistant answers and their `<|im_end|>` are trained.*
28
+
29
+ ## Who is this for
30
+ - You fine-tune a chat model with LoRA/SFT and want to know, before the run, that the loss is computed on the answers only.
31
+ - Your trainer printed "all labels are -100" (or the loss never moved) and you want to find out which example or which template line causes it.
32
+ - You edited a chat template by hand (added `{% generation %}`, changed a header) and want proof that it still renders exactly the same text.
33
+ - You truncate with `max_length` and suspect that some answers are cut in half or cut away completely.
34
+
35
+ ## Install
36
+ Python 3.11 or newer and [uv](https://docs.astral.sh/uv/) are required.
37
+
38
+ ```bash
39
+ curl -LsSf https://astral.sh/uv/install.sh | sh # installs uv
40
+ git clone https://github.com/LeeGyeongPil/masklens
41
+ cd masklens
42
+ uv sync
43
+ ```
44
+
45
+ Then run commands with `uv run masklens ...`. On a minimal Linux image you also need `git` and `curl`.
46
+
47
+ The commands below use the **practice tokenizer** in `examples/tiny-chatml/`. It is a tiny tokenizer built by `scripts/make_tiny_tokenizer.py` for demonstration (a ChatML-style vocabulary of 400 entries). It is not the tokenizer of a real model, so token counts differ from a real one. For real work, pass your model's tokenizer folder or Hub ID instead. The tests do not download anything.
48
+
49
+ Check the install with `uv run masklens --version` (prints `masklens 0.1.4`).
50
+
51
+ ## Quick start
52
+ ```bash
53
+ uv run masklens show examples/tiny-chatml examples/chat.jsonl --index 1
54
+ ```
55
+ The output language follows your system locale; add `--lang en` to match the outputs shown here.
56
+
57
+ ```text
58
+ masklens show - example 1
59
+
60
+ <|im_start|>system\n
61
+ You are a helpful assistant.<|im_end|>\n
62
+ <|im_start|>user\n
63
+ What is 2+2?<|im_end|>\n
64
+ <|im_start|>assistant\n
65
+ The answer is 4.<|im_end|>\n
66
+ <|im_start|>user\n
67
+ And 3+3?<|im_end|>\n
68
+ <|im_start|>assistant\n
69
+ The answer is 6.<|im_end|>\n
70
+
71
+
72
+ trained 0/82 tokens
73
+ Trained tokens are wrapped in [[ ]].
74
+ No token is trained in this example.
75
+
76
+ Findings for this example:
77
+ no_generation_tag (error): the template used has no generation block
78
+ empty_mask (error): 0 of 82 kept tokens are trained
79
+ ```
80
+
81
+ Nothing is highlighted: the tokenizer's own template has no `{% generation %}` block, so the "assistant tokens only" mask is empty. Libraries such as transformers do not raise an error in this case; the labels are silently all `-100`.
82
+
83
+ ![show with the original template: nothing highlighted](https://raw.githubusercontent.com/LeeGyeongPil/masklens/v0.1.4/docs/images/show-plain.png)
84
+ *Original template: 0 of 82 tokens are trained.*
85
+
86
+ Now pass a training template that marks the assistant answer:
87
+
88
+ ```bash
89
+ uv run masklens show examples/tiny-chatml examples/chat.jsonl --index 1 --template examples/tiny-chatml/training.jinja
90
+ ```
91
+ ```text
92
+ masklens show - example 1
93
+
94
+ <|im_start|>system\n
95
+ You are a helpful assistant.<|im_end|>\n
96
+ <|im_start|>user\n
97
+ What is 2+2?<|im_end|>\n
98
+ <|im_start|>assistant\n
99
+ [[The answer is 4.<|im_end|>\n
100
+ ]]<|im_start|>user\n
101
+ And 3+3?<|im_end|>\n
102
+ <|im_start|>assistant\n
103
+ [[The answer is 6.<|im_end|>\n
104
+ ]]
105
+
106
+ trained 22/82 tokens
107
+ Trained tokens are wrapped in [[ ]].
108
+ ```
109
+
110
+ Only the two answers and their end token (22 of 82 tokens) are trained. In an interactive terminal the same tokens are shown in color; the `[[ ]]` form is used with `--no-color` or when the output is piped (as in the outputs above).
111
+
112
+ ## Why masks break
113
+ Think of the labels as a marking film laid over an exam sheet: `-100` is a covered spot (ignored by the loss), and every other id is a hole where the model is graded. For chat data you want holes only over the assistant answers. Three things decide where the holes are:
114
+
115
+ ```mermaid
116
+ flowchart LR
117
+ D[messages in JSONL] --> T[chat template]
118
+ T --> R[rendered text]
119
+ R --> K[tokenizer]
120
+ K --> M{mask source}
121
+ M -->|generation block| G[assistant_masks]
122
+ M -->|marker strings| P[parts]
123
+ G --> C[truncate to max_length]
124
+ P --> C
125
+ C --> L[labels: -100 or token id]
126
+ ```
127
+
128
+ - **Template**: the template renders messages to text. With `{% generation %}` ... `{% endgeneration %}` around the answer, transformers can report which tokens came from it. Without the tags, the mask is all zeros.
129
+ - **Marker strings** (the `parts` mode, as in Unsloth's response-only training): instead of tags, you give the text that starts a user turn and the text that starts an assistant turn. A typo means nothing matches.
130
+ - **Truncation**: cutting to `max_length` can remove the end of an answer, or all of it.
131
+
132
+ masklens computes the mask, applies your truncation, and checks the result against rules.
133
+
134
+ ## Usage
135
+ | Command / option | What it does | Example |
136
+ |---|---|---|
137
+ | `masklens show TOKENIZER DATA` | Show one example with trained tokens highlighted | `--index 1` |
138
+ | `masklens check TOKENIZER DATA` | Check every example against the rules | `--limit 100` |
139
+ | `--template FILE` | Use a Jinja chat template file instead of the tokenizer's own | `--template training.jinja` |
140
+ | `--mode generation\|parts` | Mask source: `{% generation %}` blocks or marker strings | `--mode parts` |
141
+ | `--instruction-part`, `--response-part` | Marker strings for `parts` mode (`\n` is turned into a newline) | `--response-part '<\|im_start\|>assistant\n'` |
142
+ | `--max-length N`, `--truncation keep_start\|keep_end` | Truncate like your trainer before judging the mask | `--max-length 48` |
143
+ | `--field NAME` | JSON key that holds the message list (default `messages`) | `--field conversations` |
144
+ | `--index I` | (`show`) Which example to show, counting from 0 | `--index 1` |
145
+ | `--no-color` | (`show`) No ANSI colors, trained tokens only in `[[ ]]` | `--no-color` |
146
+ | `--limit N` | (`check`) Check only the first N examples | `--limit 100` |
147
+ | `--json` | Print a machine-readable report instead of the table | `--json > report.json` |
148
+ | `--strict` | Treat warnings as failures | `--strict` |
149
+ | `--lang en\|ko` | Screen language | `--lang ko` |
150
+
151
+ `TOKENIZER` is anything `AutoTokenizer.from_pretrained` accepts (a folder or a Hub ID). `DATA` is a JSONL file with one `{"messages": [...]}` per line. Exit codes (`check`): `0` no problem, `1` problem found, `2` input error (including a file that is not UTF-8 text; save it as UTF-8). `show` only inspects one example: it exits `0` even when it reports problems, and `2` on input errors.
152
+
153
+ ### Check a dataset with the original template
154
+ ```bash
155
+ uv run masklens check examples/tiny-chatml examples/chat.jsonl
156
+ ```
157
+ ```text
158
+ masklens check
159
+ tokenizer: examples/tiny-chatml
160
+ template: tokenizer's own
161
+ mode: generation
162
+ max_length: none (keep_start)
163
+ examples checked: 5
164
+
165
+ RULE SEVERITY COUNT EXAMPLES
166
+ no_generation_tag error 1 (dataset)
167
+ empty_mask error 5 0,1,2,3,4
168
+
169
+ How to fix:
170
+ no_generation_tag: Template has no {% generation %} block
171
+ -> Pass a training template with --template (wrap the assistant answer in {% generation %}...{% endgeneration %}) or use --mode parts.
172
+ the template used has no generation block
173
+ empty_mask: No trained token left (all labels would be -100)
174
+ -> Raise --max-length, try --truncation keep_end, or fix the template markers.
175
+ e.g. example 0: 0 of 32 kept tokens are trained
176
+
177
+ Result: FAIL (6 error(s), 0 warning(s))
178
+ ```
179
+ This is the "all labels are -100" situation, found before training. The exit code is 1.
180
+
181
+ ![check summary for the original template](https://raw.githubusercontent.com/LeeGyeongPil/masklens/v0.1.4/docs/images/check-fail.png)
182
+ *Summary table plus the fix hints.*
183
+
184
+ With the training template the same check passes:
185
+ ```bash
186
+ uv run masklens check examples/tiny-chatml examples/chat.jsonl --template examples/tiny-chatml/training.jinja
187
+ ```
188
+ ```text
189
+ masklens check
190
+ tokenizer: examples/tiny-chatml
191
+ template: examples/tiny-chatml/training.jinja
192
+ mode: generation
193
+ max_length: none (keep_start)
194
+ examples checked: 5
195
+
196
+ No findings.
197
+
198
+ Result: PASS
199
+ ```
200
+
201
+ ### See what truncation does
202
+ ```bash
203
+ uv run masklens check examples/tiny-chatml examples/chat.jsonl --template examples/tiny-chatml/training.jinja --max-length 48
204
+ ```
205
+ ```text
206
+ masklens check
207
+ tokenizer: examples/tiny-chatml
208
+ template: examples/tiny-chatml/training.jinja
209
+ mode: generation
210
+ max_length: 48 (keep_start)
211
+ examples checked: 5
212
+
213
+ RULE SEVERITY COUNT EXAMPLES
214
+ empty_mask error 2 2,3
215
+ response_truncated warn 3 1,2,3
216
+
217
+ How to fix:
218
+ empty_mask: No trained token left (all labels would be -100)
219
+ -> Raise --max-length, try --truncation keep_end, or fix the template markers.
220
+ e.g. example 2: 0 of 48 kept tokens are trained
221
+ response_truncated: Truncation cut into the assistant answer
222
+ -> Raise --max-length, shorten the prompts, or drop these examples.
223
+ e.g. example 1: 10 answer token(s) cut off
224
+
225
+ Result: FAIL (2 error(s), 3 warning(s))
226
+ ```
227
+ At 48 tokens, examples 2 and 3 lose every answer token and example 1 loses part of an answer. Adding `--truncation keep_end` (keep the last tokens instead of the first) gives `No findings.` for this data.
228
+
229
+ ![truncation findings](https://raw.githubusercontent.com/LeeGyeongPil/masklens/v0.1.4/docs/images/check-truncate.png)
230
+ *`response_truncated` and `empty_mask` with the example numbers.*
231
+
232
+ ### Marker strings (Unsloth-style `parts` mode)
233
+ ```bash
234
+ uv run masklens check examples/tiny-chatml examples/chat.jsonl --mode parts --instruction-part '<|im_start|>user\n' --response-part '<|im_start|>assistant\n'
235
+ ```
236
+ This gives `No findings.` / `Result: PASS`. With a typo (`assistent`) in `--response-part`:
237
+ ```bash
238
+ uv run masklens check examples/tiny-chatml examples/chat.jsonl --mode parts --instruction-part '<|im_start|>user\n' --response-part '<|im_start|>assistent\n'
239
+ ```
240
+ ```text
241
+ RULE SEVERITY COUNT EXAMPLES
242
+ empty_mask error 5 0,1,2,3,4
243
+ parts_not_found error 5 0,1,2,3,4
244
+ ```
245
+ (The header lines and fix hints are shortened here; the full output ends with `Result: FAIL (10 error(s), 0 warning(s))`.)
246
+
247
+ ![parts mode with a wrong marker string](https://raw.githubusercontent.com/LeeGyeongPil/masklens/v0.1.4/docs/images/check-parts.png)
248
+ *A wrong `--response-part` is reported as `parts_not_found`.*
249
+
250
+ ### Rules
251
+ | Rule | Severity | Means | How to fix |
252
+ |---|---|---|---|
253
+ | `no_generation_tag` | error | The template has no `{% generation %}` block (dataset-wide) | Use `--template` with a training template, or `--mode parts` |
254
+ | `empty_mask` | error | An example has no trained token (all `-100`) | Fix markers, raise `--max-length`, try `keep_end` |
255
+ | `prompt_leak` | error | System/user/tool text is trained | Make the generation block cover only the assistant content |
256
+ | `render_mismatch` | error | Your template renders different text than the tokenizer's own (the report gives the first differing character) | Change only the `{% generation %}` markers |
257
+ | `parts_not_found` | error | A marker string was not found in the rendered tokens | Copy the string from `masklens show` output |
258
+ | `response_truncated` | warn | Truncation cut into an answer | Raise `--max-length`, shorten prompts, or drop the example |
259
+ | `untrained_turn` | warn | An assistant turn is not trained at all | Check the markers of that turn |
260
+ | `eos_not_trained` | warn | The end-of-turn token after an answer is not trained | Put the end token inside the generation block so the model learns to stop |
261
+ | `content_not_found` | warn | Message text was not found verbatim (the template rewrites content), so that message is skipped in the cross-check | Informational |
262
+
263
+ `render_mismatch` is checked only when you pass `--template`. A quick demonstration: `--template tests/fixtures/leaky.jinja` reports `prompt_leak` and `eos_not_trained` for all five examples, because that template trains every turn.
264
+
265
+ ## Configuration
266
+ | Setting | Meaning |
267
+ |---|---|
268
+ | `--lang en\|ko` | Screen language. Priority: `--lang`, then `MASKLENS_LANG`, then `LC_ALL`, `LC_MESSAGES`, `LANG`; anything that is not Korean falls back to English. Rule IDs and JSON keys are never translated. |
269
+ | `MASKLENS_LANG` | `en` or `ko` |
270
+ | `HF_HUB_OFFLINE=1` | Standard Hugging Face setting; use it so a local tokenizer folder never touches the network |
271
+ | `MASKLENS_REAL_TOK_DIR` | Optional folder with a real tokenizer for an extra test in the repo; skipped when unset |
272
+
273
+ Korean screen: `uv run masklens check examples/tiny-chatml examples/chat.jsonl --lang ko`.
274
+
275
+ ## How it works
276
+ ```mermaid
277
+ flowchart TD
278
+ A[load JSONL] --> B[render with template]
279
+ B --> C[tokens + assistant_masks or marker spans]
280
+ A --> D[expected mask from message content]
281
+ C --> E[truncate with max_length]
282
+ D --> E
283
+ E --> F[apply rules]
284
+ F --> G[table, JSON, exit code]
285
+ ```
286
+ 1. The messages are rendered with the template and tokenized.
287
+ 2. The mask comes from `{% generation %}` (`assistant_masks`) or from your marker strings.
288
+ 3. Independently, masklens computes an "expected mask" from the character ranges of the assistant `content` (plus the end-of-turn token). A correct training template makes both masks equal token by token; differences become `prompt_leak`, `eos_not_trained`, and so on.
289
+ 4. Truncation is done by masklens itself (`keep_start` or `keep_end`), so the result does not depend on a trainer option.
290
+ 5. Rules turn the final mask into findings, a table, a JSON report and an exit code.
291
+
292
+ ## Writing a training template
293
+ Wrap the assistant branch in `{% generation %}`. The practice template does this (shown on several lines):
294
+ ```jinja
295
+ {%- for message in messages %}
296
+ {%- if message.role == 'assistant' %}
297
+ {{- '<|im_start|>assistant\n' }}
298
+ {%- generation %}{{- message.content + '<|im_end|>' + '\n' }}{%- endgeneration %}
299
+ {%- else %}
300
+ {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
301
+ {%- endif %}
302
+ {%- endfor %}
303
+ ```
304
+ The role header (`<|im_start|>assistant\n`) stays outside the block, so it is not trained; the answer, the end token and the trailing newline are inside. Some templates in the wild also train the header, which is a policy choice, not an error. Run `masklens check ... --template your.jinja` to see what your choice does, and the equality check against the tokenizer's own output (`render_mismatch`) to be sure the text itself did not change.
305
+
306
+ ## Using it with your trainer
307
+ - **TRL `SFTTrainer`** with `assistant_only_loss=True` needs `{% generation %}` markers in the template: use the default `--mode generation`, with the template you give the trainer.
308
+ - **Unsloth** `train_on_responses_only`: use `--mode parts` with the same instruction and response strings.
309
+ - **Your own loop**: pass the same `max_length` and truncation side you use in your collator.
310
+
311
+ ## CI
312
+ `--json` writes a report with `versions`, `mode`, `max_length`, `summary.by_rule` and `findings[]`; the exit code is `0` clean, `1` errors (or warnings with `--strict`), `2` bad input.
313
+ ```bash
314
+ uv run masklens check examples/tiny-chatml examples/chat.jsonl --template examples/tiny-chatml/training.jinja --json
315
+ ```
316
+ ```json
317
+ {
318
+ "versions": {
319
+ "masklens": "0.1.4",
320
+ "transformers": "5.24.0",
321
+ "tokenizers": "0.23.3"
322
+ },
323
+ "tokenizer": "examples/tiny-chatml",
324
+ "template": "examples/tiny-chatml/training.jinja",
325
+ "mode": "generation",
326
+ "max_length": null,
327
+ "truncation": "keep_start",
328
+ "examples": 5,
329
+ "summary": {
330
+ "errors": 0,
331
+ "warnings": 0,
332
+ "by_rule": {},
333
+ "exit_code": 0
334
+ },
335
+ "findings": []
336
+ }
337
+ ```
338
+
339
+ ## When to use it
340
+ - Good fit: a quick, framework-independent check of loss masks on the whole dataset before a run, with the template and truncation you really use.
341
+ - Other tools: to see labels inside a specific trainer pipeline, look at the debug output of [axolotl](https://github.com/axolotl-ai-cloud/axolotl) or the documentation of [TRL](https://huggingface.co/docs/trl/sft_trainer) and [Unsloth](https://unsloth.ai/docs). masklens does not run training.
342
+
343
+ ### Limits
344
+
345
+ It does not rewrite templates, and it handles chat messages only (no tool-call or think-block rules yet). If a template rewrites message content (trimming, removing think blocks), those messages cannot be cross-checked (`content_not_found`). If a message's text is identical to a role name such as `user`, the character range can be wrong. Mask behavior depends on the transformers version (5.24.0 was used here, `>=5.24.0,<6` is allowed). The repo tests use the practice tokenizer; real Qwen2.5 or SmolLM3 tokenizers were not exercised in the test suite.
346
+
347
+ ## Troubleshooting
348
+ | Symptom | Cause | Fix |
349
+ |---|---|---|
350
+ | `no_generation_tag` + `empty_mask` on every example | The template has no `{% generation %}` block | Use `--template` with a training template, or `--mode parts` |
351
+ | `parts_not_found` | Marker string differs from the rendered text (spaces, newline, typo) | Copy it from `masklens show`; in a shell write the newline as `\n` |
352
+ | `unrecognized arguments: --no-color` | `--no-color` exists only for `show` | Drop it for `check` (its output has no colors to turn off) |
353
+ | Hub download errors or slow first run | Remote tokenizer being fetched | Use a local folder and `HF_HUB_OFFLINE=1`; try `--limit N` first |
354
+ | Exit code 2 | Input error (bad JSONL, missing file) | The message names the line number |
355
+
356
+ ## Development
357
+ ```bash
358
+ HF_HUB_OFFLINE=1 uv run pytest -q
359
+ ```
360
+ Without uv, install the pinned test environment and run the same tests (this is what the CI workflow does):
361
+ ```bash
362
+ python -m pip install -r requirements-ci.txt
363
+ HF_HUB_OFFLINE=1 python -m pytest -q
364
+ ```
365
+ Bug reports and suggestions are welcome as issues. Changes are listed in [CHANGELOG.md](https://github.com/LeeGyeongPil/masklens/blob/v0.1.4/CHANGELOG.md).
366
+
367
+ ## License
368
+ MIT. The practice tokenizer in `examples/tiny-chatml/` is generated by this repo's script and contains no third-party model data.