-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy path44_cpt_domain_knowledge.py
More file actions
134 lines (110 loc) · 7.77 KB
/
Copy path44_cpt_domain_knowledge.py
File metadata and controls
134 lines (110 loc) · 7.77 KB
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
"""
Example 44: Continual Pretraining - Domain Knowledge Injection
Inject specialized medical/scientific knowledge into a base model.
CPT on domain-specific text teaches the model new vocabulary and concepts
without requiring labeled instruction-response pairs.
Use Cases:
- Medical/clinical text adaptation
- Legal document understanding
- Financial analysis specialization
- Scientific literature comprehension
NOTE: Uses small inline dataset for demo. For real use, train on large corpora.
"""
from mlx_tune import FastLanguageModel, CPTTrainer, CPTConfig
from datasets import Dataset
def main():
print("=" * 70)
print("Continual Pretraining - Medical Domain Knowledge")
print("=" * 70)
# ========================================================================
# Step 1: Load Base Model
# ========================================================================
print("\n[Step 1] Loading base model...")
model, tokenizer = FastLanguageModel.from_pretrained(
model_name="mlx-community/SmolLM2-360M-Instruct",
max_seq_length=2048,
dtype=None,
load_in_4bit=True,
)
print(f"Model loaded: {model.model_name}")
# ========================================================================
# Step 2: Apply LoRA
# ========================================================================
print("\n[Step 2] Applying LoRA...")
model = FastLanguageModel.get_peft_model(
model,
r=16,
target_modules=[
"q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj",
],
lora_alpha=16,
lora_dropout=0,
bias="none",
)
# ========================================================================
# Step 3: Prepare Medical Text Corpus
# ========================================================================
print("\n[Step 3] Preparing medical text corpus...")
medical_corpus = [
{"text": "Cardiovascular disease (CVD) remains the leading cause of mortality worldwide. Atherosclerosis, characterized by the accumulation of lipids and fibrous elements in arterial walls, is the primary pathological process underlying most cardiovascular events. Risk factors include hypertension, dyslipidemia, diabetes mellitus, smoking, and family history. The pathogenesis involves endothelial dysfunction, inflammatory cell recruitment, foam cell formation, and eventual plaque rupture."},
{"text": "Type 2 diabetes mellitus (T2DM) is a chronic metabolic disorder characterized by insulin resistance and relative insulin deficiency. The condition affects glucose homeostasis through impaired insulin signaling in peripheral tissues. Beta-cell dysfunction progresses over time, leading to inadequate insulin secretion. Management includes lifestyle modifications, metformin as first-line therapy, and potentially GLP-1 receptor agonists or SGLT2 inhibitors for additional glycemic control."},
{"text": "Immunotherapy has revolutionized cancer treatment by harnessing the body's immune system to target malignant cells. Checkpoint inhibitors, such as anti-PD-1 and anti-CTLA-4 antibodies, block inhibitory signals that tumors exploit to evade immune detection. CAR-T cell therapy engineers patient T cells to express chimeric antigen receptors specific to tumor antigens. These approaches have shown remarkable efficacy in melanoma, lung cancer, and hematological malignancies."},
{"text": "Alzheimer's disease is a progressive neurodegenerative disorder characterized by cognitive decline and memory loss. The neuropathological hallmarks include extracellular amyloid-beta plaques and intracellular neurofibrillary tangles composed of hyperphosphorylated tau protein. Neuroinflammation, mediated by activated microglia and astrocytes, contributes to disease progression. Current therapeutic approaches target amyloid clearance, tau aggregation, and neuroinflammatory pathways."},
{"text": "The human microbiome encompasses trillions of microorganisms residing in the gastrointestinal tract, skin, respiratory system, and other body sites. The gut microbiome plays crucial roles in nutrient metabolism, immune system development, and pathogen resistance. Dysbiosis, or microbial imbalance, has been associated with inflammatory bowel disease, obesity, type 2 diabetes, and mental health disorders through the gut-brain axis."},
{"text": "Pharmacogenomics studies how genetic variation affects drug response, enabling personalized medicine approaches. Cytochrome P450 enzymes, particularly CYP2D6 and CYP2C19, exhibit significant polymorphism affecting drug metabolism. Poor metabolizers may experience adverse effects at standard doses, while ultrarapid metabolizers may require higher doses for therapeutic efficacy. Integration of pharmacogenomic data into clinical decision-making improves treatment outcomes and reduces adverse drug reactions."},
{"text": "Epigenetic modifications, including DNA methylation, histone modification, and non-coding RNA regulation, control gene expression without altering the DNA sequence. These modifications are heritable through cell division and can be influenced by environmental factors such as diet, stress, and toxin exposure. Aberrant epigenetic patterns are implicated in cancer, autoimmune diseases, and neurological disorders, making epigenetic therapy a promising therapeutic frontier."},
{"text": "CRISPR-Cas9 gene editing technology has transformed biomedical research and holds promise for treating genetic diseases. The system uses guide RNA to direct the Cas9 nuclease to specific genomic loci, creating double-strand breaks that can be repaired by non-homologous end joining or homology-directed repair. Clinical trials are underway for sickle cell disease, beta-thalassemia, and certain cancers. Delivery challenges and off-target effects remain key areas of ongoing research."},
]
dataset = Dataset.from_list(medical_corpus)
print(f"Dataset: {len(dataset)} medical text documents")
# ========================================================================
# Step 4: Train with CPT
# ========================================================================
print("\n[Step 4] Starting CPT on medical corpus...")
config = CPTConfig(
learning_rate=5e-5,
embedding_learning_rate=1e-5, # 5x smaller for embeddings
include_embeddings=True,
per_device_train_batch_size=1,
gradient_accumulation_steps=4,
warmup_steps=5,
max_steps=30,
logging_steps=5,
output_dir="outputs_cpt_medical",
max_seq_length=2048,
)
trainer = CPTTrainer(
model=model,
tokenizer=tokenizer,
train_dataset=dataset,
args=config,
)
trainer.train()
# ========================================================================
# Step 5: Test Domain Knowledge
# ========================================================================
print("\n[Step 5] Testing medical knowledge...")
FastLanguageModel.for_inference(model)
from mlx_lm import generate
prompts = [
"Atherosclerosis is characterized by",
"Type 2 diabetes mellitus involves",
"CRISPR-Cas9 gene editing works by",
]
for prompt in prompts:
response = generate(
model.model, tokenizer,
prompt=prompt,
max_tokens=80,
verbose=False,
)
print(f"\n Prompt: {prompt}")
print(f" Generated: {response[:150]}")
print("\nMedical domain CPT complete!")
print("\nRecommended pipeline for production:")
print(" 1. CPT on large medical corpus (PubMed, clinical notes)")
print(" 2. SFT on medical Q&A pairs")
print(" 3. Evaluate on medical benchmarks (MedQA, PubMedQA)")
if __name__ == "__main__":
main()