File size: 9,279 Bytes
7c112bd
 
 
4d6826d
52768f3
4d6826d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
---
license: apache-2.0
---


<p align="center">Clinical + AI β€” An Open-Source Library for Clinical Diagnosis Support</p>

A GPU-based LLM fine-tuning and serving pipeline. This document describes the project's **LLM model composition** from the perspectives of base model, training, data, and inference/serving.

---

## 1. Model Composition Overview

| Category | Details |
|------|------|
| Base model | **Llama 3.1 8B Instruct** |
| Extended pipeline | **Qwen3-32B** multi-phase (SFT β†’ DPO β†’ GRPO β†’ Export) |
| Fine-tuning method | LoRA (PEFT) |
| Deployed models | LoRA adapter / base+LoRA merged model |
| Distribution channel | HuggingFace Hub β€” `the-platforms/MediCPX` |
| Domain | CPX medical (clinical diagnosis) instruction-following |

**Two pipeline tracks**

- **Llama single pipeline** β€” `FineTuningPipeline`: preprocessing β†’ tokenization β†’ LoRA training β†’ save
- **Qwen3 multi-phase pipeline** β€” `MultiPhaseFineTuningPipeline`: SFT β†’ DPO β†’ GRPO β†’ Export

---

## 2. Base Model & Weights

At the end of training, artifacts are produced under a per-run timestamped directory.

```
{output_dir}/{yyyymmddhhmm}/final_model/
β”œβ”€β”€ adapter/          # LoRA adapter (tens of MB) β€” for shared-base serving
β”‚   β”œβ”€β”€ adapter_config.json
β”‚   └── adapter_model.safetensors
β”œβ”€β”€ merged/           # base + LoRA merged model (tens of GB) β€” for standalone deployment
β”‚   β”œβ”€β”€ config.json
β”‚   └── model.safetensors
└── model_info.json   # base model, training config, and path metadata
```

| Format | Size | Loading | Use |
|------|------|------|------|
| `adapter/` | Small | `PeftModel.from_pretrained(base, adapter)` | Experiments & versioning |
| `merged/` | Large | `AutoModelForCausalLM.from_pretrained(merged)` | Dependency-free standalone serving |

**HuggingFace Hub upload** β€” `runs/upload_to_hf.py`

```bash
export HF_TOKEN=hf_xxx
python runs/upload_to_hf.py --folder-path /ai_models/merged --repo-id the-platforms/MediCPX
```

- Training-only state (`optimizer.pt`, `scheduler.pt`, `rng_state*`) is excluded by default
- Details: [`llama/huggingface_upload.md`](./llama/huggingface_upload.md)

---

## 3. Training Configuration

**4-step flow**: preprocessing β†’ tokenization β†’ LoRA training β†’ save

**Key hyperparameters**

| Item | Value |
|------|-----|
| LoRA rank | `r = 8` (Ξ± = r Γ— 2) |
| Learning rate | `2e-4` |
| max_length | `2048` |
| Precision | bf16 (auto) |
| Other | Response-region loss masking + sequence packing |

**Key code paths**

| Category | Path |
|------|------|
| Pipeline package | `app/finetuning/` |
| Llama single pipeline | `app/finetuning/pipeline.py` (`FineTuningPipeline`) |
| Qwen3 multi-phase | `app/finetuning/pipeline.py` (`MultiPhaseFineTuningPipeline`) |
| LoRA trainer | `app/finetuning/training/llama_trainer.py` |
| GRPO trainer (TRL) | `app/finetuning/training/grpo_trainer.py` |
| Reward functions | `app/finetuning/training/rewards/` (`AgentReward`, `SIDomainReward`) |
| LLaMA-Factory integration | `app/finetuning/llamafactory/` |

**Run**

```bash
# Llama LoRA fine-tuning
python runs/run_finetuning.py --model-path /ai_models --data-path /data/output/1105 --epochs 3
# Multi-GPU (DDP)
torchrun --nproc_per_node=2 runs/run_finetuning.py --model-path /ai_models --data-path /data/output/1105
# Qwen3 multi-phase
python runs/run_qwen3_finetuning.py
```

Details: [`llama/process.md`](./llama/process.md) Β· [`qwen3_finetuning_process.md`](./qwen3_finetuning_process.md)

---

## 4. Dataset Configuration

PDF source documents β†’ instruction-response JSON generation (4-Stage).

```
PDFLoader(PyMuPDF4LLM) β†’ MarkdownConverter β†’ InstructionParser(GPT-4) β†’ BatchedJSONWriter
```

**Output schema** (identical to training input)

```json
[{ "instruction": "...", "context": "(optional)", "response": "..." }]
```

**Data composition by training method**

| Method | Format | Fields |
|------|------|------|
| SFT | ShareGPT `messages` | system/user/assistant β€” one gold answer |
| DPO | `sharegpt_dpo` (ranking) | `conversations` + `chosen` + `rejected` |
| GRPO | Prompt + reward function | No gold answers or pairs needed; scored via `RewardFunction.compute()` |

**Code paths**

| Category | Path |
|------|------|
| Preprocessing package | `app/data_handling/data_pre_processing/` |
| Qwen3 data pipeline | `app/data_handling/` (chunker, qa_generation, quality, ragas_eval, trajectory) |
| Entry points | `runs/run_cpx_processing.py`, `runs/run_qwen3_data_pipeline.py` |

```bash
INPUT_PATH=/data/input/cpx.pdf OUTPUT_PATH=/data/output/1105 python runs/run_cpx_processing.py
```

Details: [`llama/data_preprocessing.md`](./llama/data_preprocessing.md)

---

## 5. Inference & Serving

| Category | Path | Description |
|------|------|------|
| Lightweight inference server | `server.py` | Single-model startup; `/generate`, `/stream`, `/health` |
| Full pipeline API | `main.py` | Integrated model load/training/inference; OpenAI-compatible chat |
| vLLM serving | `app/serving/vllm_config.py` | For Qwen3 Agent; Hermes tool parser |
| Model load helper | `runs/load_llama_3_1_8b.py` | Llama 3.1 8B load verification |

```bash
# Lightweight inference server
MODEL_DIR=/path/to/model USE_4BIT=1 uvicorn server:app --host 0.0.0.0 --port 8080
# vLLM (with tool calling)
./scripts/serve_qwen3_vllm.sh
```

**OpenAI-compatible REST API** β€” two entry points

`main.py` β€” Full Pipeline API (`:8000`)

| Category | Endpoints |
|------|-----------|
| Health | `GET /health` |
| Model | `POST /api/v1/models/load`, `GET /api/v1/models/{id}/verify` |
| Preprocess | `POST /api/v1/preprocessing/run` |
| Tokenize | `POST /api/v1/tokenization/tokenize` |
| Training | `POST /api/v1/training/start`, `GET /api/v1/training/{job_id}/status` |
| Multi-Phase | `POST /api/v1/training/multi-phase/start` |
| Data Pipeline | `POST /api/v1/data-pipeline/generate-qa` |
| Inference | `POST /api/v1/inference/load`, `GET /api/v1/inference/models` |
| Chat (OpenAI-compatible) | `POST /api/v1/chat/completions`, `POST /v1/chat/completions` |

`server.py` β€” Inference API (`:8080`): `POST /generate`, `POST /stream` (SSE), `GET /health`

```bash
./scripts/api_server.sh start    # start main.py
```

Request examples: [`API_SAMPLE.md`](./API_SAMPLE.md)

---

## 6. Evaluation

| Category | Path |
|------|------|
| Agent evaluation | `runs/eval_agent.py` |
| Domain evaluation | `runs/eval_domain.py` |
| Model evaluation | `runs/eval_model.py` |
| NLG surface metrics (BLEU/ROUGE/METEOR/BERTScore) | `runs/eval_nlg.py` |
| RAGAS evaluation | `runs/eval_ragas.py` |
| Model comparison | `runs/eval_compare.py` |

Details: [`evaluation.md`](./evaluation.md) Β· [`llama/evaluation_usage.md`](./llama/evaluation_usage.md) Β· [`llama/evaluation_runbook.md`](./llama/evaluation_runbook.md)

---

## 7. Demo

| Category | Path | Description |
|------|------|------|
| Medical chat loop demo | `scripts/medical_chat_loop.sh` | Calls the chat API in parallel with N random medical questions (repeats periodically) |
| Question set | `scripts/medical_questions.txt` | Question pool for demo input |
| System prompt | `scripts/medical_system_prompt.txt` | System prompt for the demo |
| Frontend integration guide | [`frontend_request_cancel_guide.md`](./frontend_request_cancel_guide.md) | Request/cancel integration |

```bash
# After starting the inference server
API_URL=http://localhost:8000 MODEL=MediCPX ./scripts/medical_chat_loop.sh
```

---

## Appendix β€” Deliverables Inventory

| # | Item | Key Location | Form |
|---|------|-----------|------|
| 1 | Model weights | `final_model/`, HF Hub `the-platforms/MediCPX` | LoRA adapter / merged model |
| 2 | Training code | `app/finetuning/`, `runs/run_finetuning.py` | Python package + entry point |
| 3 | Inference code | `server.py`, `main.py`, `app/serving/` | FastAPI + vLLM |
| 4 | Dataset | `app/data_handling/`, `runs/run_cpx_processing.py` | PDF β†’ instruction JSON |
| 5 | Demo | `scripts/medical_chat_loop.sh` | CLI chat demo |
| 6 | API | `main.py` (Full) / `server.py` (Inference) | OpenAI-compatible REST |
| 7 | Technical docs | `docs/` | Markdown + papers/patents |

**Technical documentation**

| Category | Document |
|------|------|
| Project overview | [`README.md`](./README.md), [`QUICKSTART.md`](./QUICKSTART.md) |
| End-to-end summary | [`llama/end_to_end_summary.md`](./llama/end_to_end_summary.md) |
| Data preprocessing | [`llama/data_preprocessing.md`](./llama/data_preprocessing.md) |
| Training walkthrough | [`llama/process.md`](./llama/process.md) |
| Qwen3 multi-phase | [`qwen3_finetuning_process.md`](./qwen3_finetuning_process.md) |
| Evaluation | [`evaluation.md`](./evaluation.md), [`llama/evaluation_usage.md`](./llama/evaluation_usage.md), [`llama/evaluation_runbook.md`](./llama/evaluation_runbook.md) |
| Improvement history | [`llama/improvements.md`](./llama/improvements.md) |
| Research & papers | [`llama/research.md`](./llama/research.md), [`llama/papers.md`](./llama/papers.md) |
| HF upload | [`llama/huggingface_upload.md`](./llama/huggingface_upload.md) |
| Deployment | [`deployments/README.md`](./deployments/README.md) (Docker, K3s) |
| Patent/paper drafts | `docs/paper/` (CPX medical-domain IMRaD, patent application drafts & figures) |