Views
No views yet
Qwen/Qwen3-4B-Instruct-2507 to compose
regular expressions — stacked lookaheads, backreferences, quantified groups,
exclusion classes — rather than recall canonical ones.| tier | what it tests | before | after |
|---|---|---|---|
| hard | composition (lookaheads, backrefs, counting) | 0.250 | 0.417 |
| seed | canonical patterns (postcode, IPv4, email) | 0.333 | 0.167 |
1from peft import PeftModel
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4base = "Qwen/Qwen3-4B-Instruct-2507"
5tok = AutoTokenizer.from_pretrained(base)
6model = AutoModelForCausalLM.from_pretrained(base, device_map="auto")
7model = PeftModel.from_pretrained(model, "wowsocool123/qwen3-4b-regex-lora")
8
9prompt = """Write a Python regular expression for this task.
10
11Task: Match a string of 8 to 16 characters with no whitespace, containing at \
12least one lowercase letter, at least one uppercase letter, and at least one digit.
13
14The pattern is applied with re.fullmatch (it must match the ENTIRE string).
15
16Reply with ONLY the regular expression pattern. No explanation, no code fences,
17no quotes, no re.compile() call. Just the raw pattern."""
18
19text = tok.apply_chat_template([{"role": "user", "content": prompt}],
20 tokenize=False, add_generation_prompt=True)
21out = model.generate(**tok(text, return_tensors="pt").to(model.device),
22 max_new_tokens=256, do_sample=False)
23print(tok.decode(out[0], skip_special_tokens=True))re.fullmatch or re.search note, a few example strings,
then the "reply with ONLY the pattern" instruction. Straying far from it will
degrade output.<think>\n\n</think> block.
Qwen3's chat template injects a reasoning block into the final assistant turn,
so all training targets carried one and the model learned to reproduce it.
Strip it before using the output:1import re
2pattern = re.sub(r"<think>.*?</think>", "", raw, flags=re.DOTALL).strip()| Method | QLoRA (4-bit NF4 base, LoRA adapter) |
| Rank / alpha | 16 / 32 |
| Target modules | q, k, v, o, gate, up, down |
| Learning rate | 2e-4, linear schedule |
| Epochs | 3 |
| Effective batch | 8 (2 × 4 grad accum) |
| Loss | responses only (prompt masked) |
| Hardware | Colab T4, ~5 min |
adapter_config.json records the base as
unsloth/qwen3-4b-instruct-2507-unsloth-bnb-4bit — Unsloth's pre-quantised
mirror of Qwen/Qwen3-4B-Instruct-2507, which is what was actually trained
against. It loads fine on either.