File size: 8,145 Bytes
638084e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Generate a self-contained Colab notebook that QLoRA-trains an adapter
on the user's selected document chunks.

The user downloads the .ipynb from the EvoLLM UI, opens it in Colab
(free T4 is sufficient for small corpora), clicks 'Run all', and
downloads two files at the end: the LoRA adapter as GGUF and a
manifest.json. They re-upload both into EvoLLM via the 'Import trained
adapter' button, and the new adapter joins the pool.
"""

from __future__ import annotations

import json
import uuid
from datetime import datetime
from pathlib import Path


def _cell(cell_type: str, source: str) -> dict:
    return {
        "cell_type": cell_type,
        "metadata": {},
        "source": source.splitlines(keepends=True),
        **({"execution_count": None, "outputs": []} if cell_type == "code" else {}),
    }


def generate_training_notebook(
    adapter_name: str,
    chunks: list[str],
    source_doc_names: list[str],
    base_model: str = "HuggingFaceTB/SmolLM2-1.7B-Instruct",
    lora_rank: int = 16,
    lora_alpha: int | None = None,
    learning_rate: float = 2e-4,
    num_epochs: int = 3,
    batch_size: int = 2,
    grad_accum: int = 4,
    output_path: str | Path = "evollm_training_notebook.ipynb",
    description: str = "",
) -> Path:
    """Produce a configured .ipynb the user can run on Colab."""
    if lora_alpha is None:
        lora_alpha = lora_rank * 2

    adapter_id = f"user_{uuid.uuid4().hex[:8]}"
    safe_adapter_name = adapter_name.strip() or adapter_id
    created_at = datetime.utcnow().isoformat()

    dataset_rows = [{"text": c} for c in chunks if c and c.strip()]

    manifest = {
        "adapter_id": adapter_id,
        "name": safe_adapter_name,
        "description": description or f"User-trained adapter on {len(source_doc_names)} document(s)",
        "base_model": base_model,
        "source_documents": source_doc_names,
        "lora_rank": lora_rank,
        "lora_alpha": lora_alpha,
        "learning_rate": learning_rate,
        "num_epochs": num_epochs,
        "training_examples": len(dataset_rows),
        "trained_at": created_at,
        "trained_from_knowledge": True,
    }

    intro_md = f"""# EvoLLM β€” Train your own adapter

This notebook produces a **{safe_adapter_name}** LoRA adapter from your
selected documents.

**Source documents**: {", ".join(source_doc_names) or "(none)"}
**Base model**: `{base_model}`
**LoRA rank**: {lora_rank}  (alpha = {lora_alpha})
**Epochs**: {num_epochs}  Β·  **LR**: {learning_rate}  Β·  **Examples**: {len(dataset_rows)}

## How to run

1. **Runtime β†’ Change runtime type β†’ T4 GPU** (or A100 if you have Colab Pro).
2. Click **Runtime β†’ Run all**.
3. When training finishes, you'll get two download links:
   - `{adapter_id}.gguf`  β€” the LoRA adapter in llama.cpp format
   - `{adapter_id}.json`  β€” the manifest
4. Back in EvoLLM, go to the **🧬 Adapter Pool** tab β†’ **πŸ“₯ Import trained adapter** β†’ drop both files.

Approximate runtime on free T4: ~20–60 minutes for {len(dataset_rows)} examples.
"""

    setup_code = """!nvidia-smi
!pip install -q -U \\
    "transformers>=4.46" "peft>=0.13" "trl>=0.12" \\
    "datasets>=3.1" "accelerate>=1.1" "bitsandbytes>=0.44" "sentencepiece"
"""

    config_code = f"""import json, gc, torch
from pathlib import Path

ADAPTER_ID = "{adapter_id}"
ADAPTER_NAME = {json.dumps(safe_adapter_name)}
BASE_MODEL = {json.dumps(base_model)}
LORA_RANK = {lora_rank}
LORA_ALPHA = {lora_alpha}
LEARNING_RATE = {learning_rate}
NUM_EPOCHS = {num_epochs}
BATCH_SIZE = {batch_size}
GRAD_ACCUM = {grad_accum}

OUT_DIR = Path(f"/content/{{ADAPTER_ID}}")
OUT_DIR.mkdir(parents=True, exist_ok=True)
"""

    # Inline the dataset as a JSON list. For small corpora this is fine;
    # very large corpora should switch to a side file, but that's edge case.
    dataset_code = "DATASET_ROWS = " + json.dumps(dataset_rows, ensure_ascii=False, indent=2)

    manifest_code = (
        "MANIFEST = " + json.dumps(manifest, ensure_ascii=False, indent=2)
    )

    train_code = """from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
from trl import SFTTrainer, SFTConfig
from datasets import Dataset

bnb = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_use_double_quant=True,
)

print(f"Loading base: {BASE_MODEL}")
tok = AutoTokenizer.from_pretrained(BASE_MODEL)
if tok.pad_token is None:
    tok.pad_token = tok.eos_token

model = AutoModelForCausalLM.from_pretrained(
    BASE_MODEL, quantization_config=bnb, device_map="auto", torch_dtype=torch.bfloat16,
)
model = prepare_model_for_kbit_training(model)

peft_cfg = LoraConfig(
    r=LORA_RANK, lora_alpha=LORA_ALPHA, lora_dropout=0.05,
    bias="none", task_type="CAUSAL_LM",
    target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
)
model = get_peft_model(model, peft_cfg)
model.print_trainable_parameters()

ds = Dataset.from_list(DATASET_ROWS)

cfg = SFTConfig(
    output_dir=str(OUT_DIR),
    num_train_epochs=NUM_EPOCHS,
    per_device_train_batch_size=BATCH_SIZE,
    gradient_accumulation_steps=GRAD_ACCUM,
    learning_rate=LEARNING_RATE,
    bf16=True,
    logging_steps=10,
    save_strategy="epoch",
    save_total_limit=1,
    report_to="none",
    max_seq_length=1024,
    warmup_ratio=0.03,
    dataset_text_field="text",
)

trainer = SFTTrainer(model=model, tokenizer=tok, train_dataset=ds, args=cfg)
trainer.train()
trainer.save_model(str(OUT_DIR))
print(f"\\nAdapter saved to {OUT_DIR}")

del model, trainer; gc.collect(); torch.cuda.empty_cache()
"""

    convert_code = """# Convert the LoRA adapter to GGUF for llama.cpp
!apt-get install -y -qq cmake build-essential
!git clone --depth 1 https://github.com/ggerganov/llama.cpp /content/llama.cpp 2>/dev/null || echo 'already cloned'
!pip install -q -r /content/llama.cpp/requirements/requirements-convert_lora_to_gguf.txt

GGUF_PATH = OUT_DIR / f"{ADAPTER_ID}.gguf"
!python /content/llama.cpp/convert_lora_to_gguf.py {OUT_DIR} --base {BASE_MODEL} --outfile {GGUF_PATH}

print(f"\\nGGUF adapter at: {GGUF_PATH}")
print(f"Size: {GGUF_PATH.stat().st_size / 1024 / 1024:.1f} MB")
"""

    package_code = """# Save manifest and prepare downloads
import shutil

manifest_path = OUT_DIR / f"{ADAPTER_ID}.json"
manifest_path.write_text(json.dumps(MANIFEST, ensure_ascii=False, indent=2))

# Stage the two files at /content for easy download
shutil.copy(GGUF_PATH, f"/content/{ADAPTER_ID}.gguf")
shutil.copy(manifest_path, f"/content/{ADAPTER_ID}.json")

print("\\n" + "=" * 60)
print("READY TO DOWNLOAD")
print("=" * 60)
print(f"  /content/{ADAPTER_ID}.gguf")
print(f"  /content/{ADAPTER_ID}.json")
print()
print("In the Colab Files panel (left side), right-click each file β†’ Download.")
print("Then in EvoLLM: 🧬 Adapter Pool tab β†’ πŸ“₯ Import trained adapter β†’ drop both files.")
"""

    notebook = {
        "cells": [
            _cell("markdown", intro_md),
            _cell("markdown", "## 0. Setup"),
            _cell("code", setup_code),
            _cell("markdown", "## 1. Configuration & dataset"),
            _cell("code", config_code),
            _cell("code", dataset_code),
            _cell("code", manifest_code),
            _cell("markdown", "## 2. Train"),
            _cell("code", train_code),
            _cell("markdown", "## 3. Convert to GGUF"),
            _cell("code", convert_code),
            _cell("markdown", "## 4. Package for EvoLLM"),
            _cell("code", package_code),
        ],
        "metadata": {
            "kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"},
            "language_info": {"name": "python", "version": "3.10"},
            "accelerator": "GPU",
            "colab": {"provenance": [], "gpuType": "T4"},
        },
        "nbformat": 4,
        "nbformat_minor": 4,
    }

    output_path = Path(output_path)
    output_path.parent.mkdir(parents=True, exist_ok=True)
    output_path.write_text(json.dumps(notebook, ensure_ascii=False, indent=1))
    return output_path