import torchfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom peft import PeftModel base = "Qwen/Qwen3-4B-Instruct-2507"tok = AutoTokenizer.from_pretrained(base)model = AutoModelForCausalLM.from_pretrained(base, dtype=torch.bfloat16, device_map="auto")model = PeftModel.from_pretrained(model, "<this-repo>") # adapter_model.safetensors lives here SYSTEM = ("You are an English editor. Fix grammar, word choice and phrasing of the " "given English sentence. Reply with the corrected sentence only, with no " "explanations. If the sentence is already correct, reply with it unchanged.") def correct(en, zh=None): user = (f"Cantonese reference: {zh}\n" if zh else "") + f"English sentence: {en}" prompt = f"<|im_start|>system\n{SYSTEM}<|im_end|>\n<|im_start|>user\n{user}<|im_end|>\n<|im_start|>assistant\n" ids = tok(prompt, return_tensors="pt", add_special_tokens=False).input_ids.to(model.device) out = model.generate(ids, max_new_tokens=60, do_sample=False, eos_token_id=151645, pad_token_id=151643) return tok.decode(out[0, ids.shape[1]:], skip_special_tokens=True).split("\n")[0].strip()
llama.cpp
Merged (LoRA folded into base in our own tinygrad pipeline — no third-party
quants involved) and converted/quantized ourselves:
Table with columns: file, size, note| file | size | note |
|---|
qwen3-4b-english-correction-f16.gguf | 8.0 GB | full precision merge |
qwen3-4b-english-correction-q8_0.gguf | 4.3 GB | Q8_0 self-quantized from our f16 |
SYST='You are an English editor. Fix grammar, word choice and phrasing of the given English sentence. Reply with the corrected sentence only, with no explanations. If the sentence is already correct, reply with it unchanged.'llama-completion -m qwen3-4b-english-correction-q8_0.gguf -e --temp 0 -n 60 \ -p "<|im_start|>system\n$SYST<|im_end|>\n<|im_start|>user\nEnglish sentence: He go to school yesterday.<|im_end|>\n<|im_start|>assistant\n"# -> He went to school yesterday.
Verified greedy-identical to the tinygrad/HF reference outputs (incl. the
Cantonese-context behavior).
Experimental: Qwen3-1.7B variant
Same pipeline, same data, LoRA r=16 applied to Qwen/Qwen3-1.7B (the
hybrid-thinking variant — SFT suppresses the think blocks; outputs are direct):
qwen3-1.7b/adapter_{model.safetensors,config.json} — PEFT adapter (subfolder)
qwen3-1.7b-english-correction-{f16,q8_0}.gguf — merged GGUFs
model = PeftModel.from_pretrained(base, "<this-repo>", subfolder="qwen3-1.7b")
All 14 synthetic eval cases pass; base 1.7B baseline was invalid-by-design
(thinking model burns its budget reasoning; see GitHub JOURNAL). Trained ~25
optimizer steps, best val loss 0.77 (vs 4B's 0.88 on the same data).
Training data (data/)
Derived from the words.hk Cantonese-English dictionary: volunteer-edited English
example sentences (often translationese) were corrected by several teacher models
and partially by humans; each entry pairs an English sentence with its Cantonese
meaning and a corrected target.
data/train.jsonl (66,167 pairs) / data/val.jsonl (1,374) — fields:
en (English input), zh (Cantonese reference, may be null), target
(corrected sentence), source (which teacher/human produced the target),
kind (correction | identity), avg_score (reviewer score of the original).
- Identity pairs (~target == input) teach the model to leave good sentences alone.
- ~15% of training examples omit the Cantonese line so the model also works
without it.
data/sample_review.txt — human-readable samples; data/stats.json — build stats.
Training
- Base: Qwen/Qwen3-4B-Instruct-2507; LoRA r=16, alpha=32 on
q/k/v/o_proj + gate/up/down_proj (all layers).
- Masked completion-only SFT, Qwen chat template, AdamW lr 1.5e-4, bf16→fp16 base.
- Framework: tinygrad (custom Qwen3 implementation + LoRA; validated
token-exact against HF transformers before training).
- ~45 optimizer steps over a 960-example subset (validation loss 2.0 → 0.88).
Small-but-consistent: the task format is narrow and the model follows it
reliably (see eval below).
Evaluation
14 hand-written synthetic cases (grammar, tense, agreement, articles, Chinglish
collocations, translationese, run-ons, already-correct inputs; with and without
Cantonese reference): all pass, including identity behavior on already-correct
sentences. The adapter was also verified to produce identical outputs through
HF transformers+peft and through the original tinygrad inference path.
Limitations
- Trained on a small subset (~200 unique examples seen, heavily repeated);
style coverage beyond the dictionary domain is untested.
- The Cantonese reference is optional but improves translationese handling.
- Trained for single-sentence correction; longer documents are out of scope.