Views
No views yet
1# **qwen3-4b-structured-output-lora-phase2**
2
3This repository provides a LoRA adapter fine-tuned from Qwen/Qwen3-4B-Instruct-2507 using QLoRA (4-bit, bitsandbytes).
4This repository contains LoRA adapter weights (and tokenizer files) only. The base model must be loaded separately.
5
6## **Training Objective**
7
8This adapter is trained to improve structured output accuracy (JSON / YAML / XML / TOML / CSV).
9Loss is applied only to the final assistant output (completion-only loss), while the prompt portion is masked (labels = -100).
10Additionally, intermediate reasoning markers such as `<think>...</think>` and code fences were removed during data preparation to prioritize parseable outputs.
11
12## **Training Configuration**
13
14* Base model: Qwen/Qwen3-4B-Instruct-2507
15* Method: QLoRA (4-bit, bitsandbytes) + PEFT LoRA
16* Training: Phase1 (mixed) → Phase2 (hard-only resume)
17* Epochs: 1 epoch (Phase1) + 1 epoch (Phase2)
18* Learning rate: 5e-05 (Phase2)
19* Packing: disabled (completion-only loss)
20* Framework: transformers + trl + peft
21* LoRA: (see adapter_config.json in this repo)
22
23## **Usage**
24
25```python
26from transformers import AutoModelForCausalLM, AutoTokenizer
27from peft import PeftModel
28import torch
29
30base = "Qwen/Qwen3-4B-Instruct-2507"
31adapter = "your_id/your-repo"
32
33tokenizer = AutoTokenizer.from_pretrained(base, trust_remote_code=True)
34model = AutoModelForCausalLM.from_pretrained(
35 base,
36 torch_dtype=torch.float16,
37 device_map="auto",
38 trust_remote_code=True,
39)
40model = PeftModel.from_pretrained(model, adapter)<think>...</think> やコードフェンスを除去しています。1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel
3import torch
4
5base = "Qwen/Qwen3-4B-Instruct-2507"
6adapter = "your_id/your-repo"
7
8tokenizer = AutoTokenizer.from_pretrained(base, trust_remote_code=True)
9model = AutoModelForCausalLM.from_pretrained(
10 base,
11 torch_dtype=torch.float16,
12 device_map="auto",
13 trust_remote_code=True,
14)
15model = PeftModel.from_pretrained(model, adapter)
---
# B) README.md(マージ済みモデル用:提出URL向け・サンプル準拠)
```md
# **qwen3-4b-structured-output-phase2-merged**
This repository provides a merged FP16 model built from Qwen/Qwen3-4B-Instruct-2507 and a Phase2 LoRA adapter.
This repository contains the full merged model weights, so it can be loaded directly with `from_pretrained()`.
## **Training Objective**
This model is trained to improve structured output accuracy (JSON / YAML / XML / TOML / CSV).
Loss is applied only to the final assistant output (completion-only loss), while the prompt portion is masked (labels = -100).
Data preparation removes `<think>...</think>` and code fences to prioritize strict, parseable outputs.
## **Training Configuration**
* Base model: Qwen/Qwen3-4B-Instruct-2507
* Method: QLoRA SFT (4-bit) → merged into FP16 weights for inference
* Training: Phase1 (mixed) → Phase2 (hard-only resume)
* Epochs: 1 epoch (Phase1) + 1 epoch (Phase2)
* Learning rate: 5e-05 (Phase2)
* Framework: transformers + trl + peft
* Output: merged FP16 model (safe_serialization)
## **Usage**
```python
from transformers import AutoModelForCausalLM, AutoTokenizer
model_id = "your_id/your-repo"
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto",
trust_remote_code=True,
)from_pretrained() で直接ロードできます。<think>...</think> やコードフェンスを除去しています。1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_id = "your_id/your-repo"
4
5tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
6model = AutoModelForCausalLM.from_pretrained(
7 model_id,
8 device_map="auto",
9 trust_remote_code=True,
10)
---
## どっちを使うべき?
- **提出URL用(推奨)**:B(merged)
- **再現・継続学習用**:A(LoRA)
---
必要なら、あなたの実際の学習ログに合わせて README の数値をさらに厳密化できます(例:Phase1/2 の lr、bs、ga、max_len、データ件数を明記)。ただ、今の版でもサンプルの粒度と整合しています。
::contentReference[oaicite:0]{index=0}