Text Generation
PEFT
Safetensors
clinical-trial
reinforcement-learning
REINFORCE
lora
qwen3
openenv
hackathon
theme-2
conversational
Instructions to use pratimassaravanan/clinical-qwen3-4b-sft-lora with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use pratimassaravanan/clinical-qwen3-4b-sft-lora with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("unsloth/Qwen3-4B-unsloth-bnb-4bit") model = PeftModel.from_pretrained(base_model, "pratimassaravanan/clinical-qwen3-4b-sft-lora") - Notebooks
- Google Colab
- Kaggle
Update README with real training evidence
Browse files
README.md
CHANGED
|
@@ -1,210 +1,112 @@
|
|
| 1 |
---
|
| 2 |
-
base_model:
|
| 3 |
library_name: peft
|
| 4 |
pipeline_tag: text-generation
|
| 5 |
tags:
|
| 6 |
-
-
|
|
|
|
|
|
|
| 7 |
- lora
|
| 8 |
-
-
|
| 9 |
-
-
|
| 10 |
-
-
|
| 11 |
-
-
|
|
|
|
| 12 |
---
|
| 13 |
|
| 14 |
-
#
|
| 15 |
|
| 16 |
-
|
| 17 |
|
|
|
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
##
|
| 21 |
|
| 22 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
|
|
|
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
- **Funded by [optional]:** [More Information Needed]
|
| 30 |
-
- **Shared by [optional]:** [More Information Needed]
|
| 31 |
-
- **Model type:** [More Information Needed]
|
| 32 |
-
- **Language(s) (NLP):** [More Information Needed]
|
| 33 |
-
- **License:** [More Information Needed]
|
| 34 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
| 35 |
|
| 36 |
-
###
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
- **Paper [optional]:** [More Information Needed]
|
| 42 |
-
- **Demo [optional]:** [More Information Needed]
|
| 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 |
-
### Recommendations
|
| 73 |
-
|
| 74 |
-
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
| 75 |
-
|
| 76 |
-
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
| 77 |
-
|
| 78 |
-
## How to Get Started with the Model
|
| 79 |
-
|
| 80 |
-
Use the code below to get started with the model.
|
| 81 |
-
|
| 82 |
-
[More Information Needed]
|
| 83 |
-
|
| 84 |
-
## Training Details
|
| 85 |
-
|
| 86 |
-
### Training Data
|
| 87 |
-
|
| 88 |
-
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
| 89 |
-
|
| 90 |
-
[More Information Needed]
|
| 91 |
-
|
| 92 |
-
### Training Procedure
|
| 93 |
-
|
| 94 |
-
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
| 95 |
-
|
| 96 |
-
#### Preprocessing [optional]
|
| 97 |
-
|
| 98 |
-
[More Information Needed]
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
#### Training Hyperparameters
|
| 102 |
-
|
| 103 |
-
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 104 |
-
|
| 105 |
-
#### Speeds, Sizes, Times [optional]
|
| 106 |
-
|
| 107 |
-
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 108 |
-
|
| 109 |
-
[More Information Needed]
|
| 110 |
-
|
| 111 |
-
## Evaluation
|
| 112 |
-
|
| 113 |
-
<!-- This section describes the evaluation protocols and provides the results. -->
|
| 114 |
-
|
| 115 |
-
### Testing Data, Factors & Metrics
|
| 116 |
-
|
| 117 |
-
#### Testing Data
|
| 118 |
-
|
| 119 |
-
<!-- This should link to a Dataset Card if possible. -->
|
| 120 |
-
|
| 121 |
-
[More Information Needed]
|
| 122 |
-
|
| 123 |
-
#### Factors
|
| 124 |
-
|
| 125 |
-
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 126 |
-
|
| 127 |
-
[More Information Needed]
|
| 128 |
-
|
| 129 |
-
#### Metrics
|
| 130 |
-
|
| 131 |
-
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 132 |
-
|
| 133 |
-
[More Information Needed]
|
| 134 |
-
|
| 135 |
-
### Results
|
| 136 |
-
|
| 137 |
-
[More Information Needed]
|
| 138 |
-
|
| 139 |
-
#### Summary
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
## Model Examination [optional]
|
| 144 |
-
|
| 145 |
-
<!-- Relevant interpretability work for the model goes here -->
|
| 146 |
-
|
| 147 |
-
[More Information Needed]
|
| 148 |
-
|
| 149 |
-
## Environmental Impact
|
| 150 |
-
|
| 151 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 152 |
-
|
| 153 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 154 |
-
|
| 155 |
-
- **Hardware Type:** [More Information Needed]
|
| 156 |
-
- **Hours used:** [More Information Needed]
|
| 157 |
-
- **Cloud Provider:** [More Information Needed]
|
| 158 |
-
- **Compute Region:** [More Information Needed]
|
| 159 |
-
- **Carbon Emitted:** [More Information Needed]
|
| 160 |
-
|
| 161 |
-
## Technical Specifications [optional]
|
| 162 |
-
|
| 163 |
-
### Model Architecture and Objective
|
| 164 |
-
|
| 165 |
-
[More Information Needed]
|
| 166 |
-
|
| 167 |
-
### Compute Infrastructure
|
| 168 |
-
|
| 169 |
-
[More Information Needed]
|
| 170 |
-
|
| 171 |
-
#### Hardware
|
| 172 |
-
|
| 173 |
-
[More Information Needed]
|
| 174 |
-
|
| 175 |
-
#### Software
|
| 176 |
-
|
| 177 |
-
[More Information Needed]
|
| 178 |
-
|
| 179 |
-
## Citation [optional]
|
| 180 |
-
|
| 181 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 182 |
-
|
| 183 |
-
**BibTeX:**
|
| 184 |
-
|
| 185 |
-
[More Information Needed]
|
| 186 |
-
|
| 187 |
-
**APA:**
|
| 188 |
-
|
| 189 |
-
[More Information Needed]
|
| 190 |
-
|
| 191 |
-
## Glossary [optional]
|
| 192 |
-
|
| 193 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 194 |
-
|
| 195 |
-
[More Information Needed]
|
| 196 |
-
|
| 197 |
-
## More Information [optional]
|
| 198 |
-
|
| 199 |
-
[More Information Needed]
|
| 200 |
-
|
| 201 |
-
## Model Card Authors [optional]
|
| 202 |
-
|
| 203 |
-
[More Information Needed]
|
| 204 |
-
|
| 205 |
-
## Model Card Contact
|
| 206 |
-
|
| 207 |
-
[More Information Needed]
|
| 208 |
-
### Framework versions
|
| 209 |
-
|
| 210 |
-
- PEFT 0.19.1
|
|
|
|
| 1 |
---
|
| 2 |
+
base_model: Qwen/Qwen3-4B
|
| 3 |
library_name: peft
|
| 4 |
pipeline_tag: text-generation
|
| 5 |
tags:
|
| 6 |
+
- clinical-trial
|
| 7 |
+
- reinforcement-learning
|
| 8 |
+
- REINFORCE
|
| 9 |
- lora
|
| 10 |
+
- qwen3
|
| 11 |
+
- openenv
|
| 12 |
+
- hackathon
|
| 13 |
+
- theme-2
|
| 14 |
+
license: mit
|
| 15 |
---
|
| 16 |
|
| 17 |
+
# Clinical Trial Recruitment Agent — Qwen3-4B + RL
|
| 18 |
|
| 19 |
+
A clinical trial recruitment agent trained with **REINFORCE** on a 180-step long-horizon environment. Theme 2 submission for the OpenEnv Hackathon.
|
| 20 |
|
| 21 |
+
## Overview
|
| 22 |
|
| 23 |
+
| Component | Detail |
|
| 24 |
+
|---|---|
|
| 25 |
+
| **Base Model** | Qwen/Qwen3-4B (4-bit quantized) |
|
| 26 |
+
| **Method** | Fresh LoRA (r=16, alpha=32) + REINFORCE policy gradient |
|
| 27 |
+
| **Environment** | Clinical Recruitment Env (8 action types, 37 observation features, 180-step horizon) |
|
| 28 |
+
| **Training GPU** | NVIDIA L40S (48GB) on Lightning AI |
|
| 29 |
+
| **HF Space** | [pratimassaravanan/clinical-recruitment](https://huggingface.co/spaces/pratimassaravanan/clinical-recruitment) |
|
| 30 |
|
| 31 |
+
## Training Pipeline
|
| 32 |
|
| 33 |
+
### Phase 1: SFT (Supervised Fine-Tuning)
|
| 34 |
+
- **Data**: 2,000 heuristic-generated traces (observation → JSON action pairs)
|
| 35 |
+
- **Result**: 100% JSON parse rate but **complete policy collapse** — model only outputs `adjust_strategy`
|
| 36 |
+
- **Loss**: 3.14 → 0.015 (perfect memorization, zero generalization)
|
| 37 |
+
- **Finding**: SFT teaches format but not observation-conditional behavior
|
| 38 |
|
| 39 |
+
### Phase 2: REINFORCE (Policy Gradient RL)
|
| 40 |
+
- **Key fix**: Observation parsing bug (`result.get("observation")` → `result` — API returns flat dict)
|
| 41 |
+
- **Key fix**: Heuristic override prevents degenerate actions when candidates exist
|
| 42 |
+
- **Reward shaping**: +0.20 for allocate, +0.15 for recontact, +0.10 for screen, -0.10 for adjust_strategy when productive actions available
|
| 43 |
|
| 44 |
+
**Debug Trial Results (3 episodes, 15 steps each):**
|
| 45 |
|
| 46 |
+
| Episode | Task | Enrolled | Target | Reward | Action Distribution |
|
| 47 |
+
|---|---|---|---|---|---|
|
| 48 |
+
| 0 | easy_bench | **5** | 80 | 6.61 | screen=6, allocate=9 |
|
| 49 |
+
| 1 | easy_bench | **5** | 80 | 6.61 | screen=6, allocate=9 |
|
| 50 |
+
| 2 | easy_bench | **1** | 80 | 7.41 | screen=14, allocate=1 |
|
| 51 |
|
| 52 |
+
**Improvement over SFT**: 0 → 5 enrolled patients, diverse action distribution vs. 100% adjust_strategy collapse.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
### Phase 3: Full REINFORCE Run (30 episodes)
|
| 55 |
+
- 30 episodes across easy/medium/hard benchmarks
|
| 56 |
+
- Results uploading upon completion
|
| 57 |
|
| 58 |
+
## Key Findings
|
| 59 |
|
| 60 |
+
1. **SFT collapse is fundamental**: Two independent SFT runs (2K and 16K traces) both produced identical policy collapse. More data doesn't help — the model memorizes the most common action without learning observation-conditional behavior.
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
2. **Observation parsing was the root cause of 0 enrollment**: The HF Space API returns a flat dict (not nested under `observation`), causing all candidate lists to appear empty.
|
| 63 |
|
| 64 |
+
3. **Heuristic override + RL reward shaping enables real enrollment**: The combination of a smart fallback (allocate > recontact > screen > adjust) with REINFORCE reward shaping produces agents that actually progress through the recruitment funnel.
|
| 65 |
|
| 66 |
+
4. **Fresh LoRA outperforms SFT LoRA**: Starting from base Qwen3-4B with a fresh LoRA adapter (instead of loading the collapsed SFT adapter) allows the model to explore and learn productive actions.
|
| 67 |
|
| 68 |
+
## Repository Structure
|
| 69 |
|
| 70 |
+
```
|
| 71 |
+
clinical-recruitment-env/
|
| 72 |
+
├── env.py # 180-step clinical trial environment
|
| 73 |
+
├── models.py # Action/Observation Pydantic models
|
| 74 |
+
├── graders.py # Task-specific scoring (easy/medium/hard)
|
| 75 |
+
├── app.py # FastAPI server
|
| 76 |
+
├── openenv_adapter.py # OpenEnv protocol adapter
|
| 77 |
+
├── train.py # SFT training script (local, uses Python API)
|
| 78 |
+
├── _lightning_reinforce.py # REINFORCE v3 (Lightning AI, uses HTTP API)
|
| 79 |
+
├── _reinforce_v4.py # REINFORCE v4 (gentler reward shaping)
|
| 80 |
+
└── _debug_trial.py # Debug trial (3 episodes, full logging)
|
| 81 |
+
```
|
| 82 |
|
| 83 |
+
## Usage
|
| 84 |
|
| 85 |
+
```python
|
| 86 |
+
from peft import PeftModel
|
| 87 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 88 |
|
| 89 |
+
base = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-4B", load_in_4bit=True)
|
| 90 |
+
model = PeftModel.from_pretrained(base, "pratimassaravanan/clinical-qwen3-4b-sft-lora/rl_v3_adapter")
|
| 91 |
+
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-4B")
|
| 92 |
+
```
|
| 93 |
|
| 94 |
+
## Environment Details
|
| 95 |
|
| 96 |
+
- **Actions**: screen_patient, recontact, allocate_to_site, adjust_strategy, plan_next_phase, summarize_and_index, retrieve_relevant_history, stop_recruitment
|
| 97 |
+
- **Observation**: 37 features including patient lists, site performance, funnel metrics, world_type
|
| 98 |
+
- **Reward**: Enrollment (+0.50), screening (+0.30), dropout (-0.35), milestone bonuses, hypothesis accuracy (+0.10)
|
| 99 |
+
- **Tasks**: easy_bench (80 target), medium_bench (100 target), hard_bench (150 target)
|
| 100 |
|
| 101 |
+
## Compute & Cost
|
| 102 |
|
| 103 |
+
| Job | GPU | Duration | Cost |
|
| 104 |
+
|---|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