# Recipe: 200-example glossary LoRA

## Source generator

Qwen3.8-27B, 25 sentences per term, temperature 0.8. The exact prompt is in
`scripts/make_synthetic_sources.py`. The generated pool is capped at 25 per term
(300 candidates).

## Teacher prompt

Hy-MT3-30B-A3B, temperature 0, max 512 tokens:

```text
Reference the following translations:
<12 glossary lines>

Section: Glossary practice

Translate the following paragraph into Modern Standard Arabic. Preserve names,
numbers, URLs, and code exactly. Only output the translated paragraph, with no
explanation.

<source sentence>
```

## Filter

Keep a row only when:

- every glossary term present in the source is present in the teacher output,
  using the strict checker (definite/indefinite accepted, synonyms rejected);
- the output has no preamble and does not echo the source;
- the output has an Arabic ratio and length pass;
- `finish_reason` is `stop`.

Result: 293 of 300 teacher outputs kept.

## Split

200 training examples, 20 validation examples, stratified by term. The 20
evaluation paragraphs are never in either split.

## Leakage gate

`scripts/check_leakage.py` checks exact paragraph, exact sentence, shared
12-word shingles and 8-gram containment >= 0.20 against 84 eval texts: the 20
sources, the 30B outputs, the 1.8B Q8 outputs and the 1.8B bf16 outputs. The
gate is a hard precondition in `train_lora.py`; a leak exits 1 before model load.

## LoRA

Rank 8, alpha 16, dropout 0.05, q/k/v/o/gate/up/down projections, 9,699,328
trainable parameters. 200 examples, batch 2, 3 epochs, 300 steps, 155 seconds
on the M3 Ultra MPS. Adapter size 37 MB.

## Evaluation

Same 20 paragraphs, same prompt, 3 runs, greedy, 2,048 new-token cap. Base and
LoRA are bf16 Transformers. Repair uses the 1.8B Q8 draft and calls the 30B only
when the strict checker finds a missing term. 30B direct uses the existing Q8
GGUF outputs. Glossary strict/lenient and COMET-Kiwi are scored for every arm.

## Commands

```bash
# 30B teacher (separate terminal)
llama-server -m Hy-MT3-30B-A3B-Q8_0.gguf -ngl 99 -c 8192 --jinja --port 8081 -np 1

python3 scripts/make_synthetic_sources.py
python3 scripts/translate_teacher.py
<python-with-sacrebleu> scripts/filter_teacher.py
NLT_HY18_MODEL=/path/to/Hy-MT3-1.8B python3 scripts/train_lora.py   --train data/train200.jsonl --val data/val20.jsonl --steps 300 --batch-size 2 --out adapter/full200
NLT_HY18_MODEL=/path/to/Hy-MT3-1.8B python3 scripts/eval_adapter.py   --adapter adapter/full200 --out results/lora_full_bf16.jsonl
<python-with-sacrebleu> scripts/run_repair.py
NLT_COMET_DIR=/path/to/comet/kiwi-int8 <python-with-sacrebleu> scripts/score_arms.py
```
