playa555x commited on
Commit
d8a8a38
·
verified ·
1 Parent(s): a253864

Add notebook

Browse files
Files changed (1) hide show
  1. train_moshi_de_colab.ipynb +262 -0
train_moshi_de_colab.ipynb ADDED
@@ -0,0 +1,262 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "name": "train_moshi_de_colab.ipynb",
7
+ "provenance": [],
8
+ "machine_shape": "hm"
9
+ },
10
+ "kernelspec": {
11
+ "name": "python3",
12
+ "display_name": "Python 3"
13
+ },
14
+ "language_info": {
15
+ "name": "python"
16
+ },
17
+ "accelerator": "GPU"
18
+ },
19
+ "cells": [
20
+ {
21
+ "cell_type": "markdown",
22
+ "metadata": {},
23
+ "source": [
24
+ "# German PersonaPlex — Colab Training\n",
25
+ "\n",
26
+ "LoRA fine-tune von Kyutai Moshi auf deutsche Podcast-Daten (Kuechenstudio, CC-BY).\n",
27
+ "\n",
28
+ "**Voraussetzungen:**\n",
29
+ "- Colab Pro+ (A100 40GB) empfohlen — L4 24GB geht auch mit kleinerer Batch-Size\n",
30
+ "- Runtime: GPU (A100 > L4 > V100)\n",
31
+ "- HuggingFace Token (Secret `HF_TOKEN` einrichten: links im Menue → Key-Icon)\n",
32
+ "\n",
33
+ "**Ablauf:** Runtime → Run all. Training dauert ~6-10h. Checkpoint wird auto auf HF gepusht."
34
+ ]
35
+ },
36
+ {
37
+ "cell_type": "markdown",
38
+ "metadata": {},
39
+ "source": ["## 1. Setup"]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "metadata": {},
44
+ "execution_count": null,
45
+ "outputs": [],
46
+ "source": [
47
+ "# GPU check\n",
48
+ "!nvidia-smi"
49
+ ]
50
+ },
51
+ {
52
+ "cell_type": "code",
53
+ "metadata": {},
54
+ "execution_count": null,
55
+ "outputs": [],
56
+ "source": [
57
+ "# HF Token aus Colab Secrets laden\n",
58
+ "from google.colab import userdata\n",
59
+ "import os\n",
60
+ "os.environ['HF_TOKEN'] = userdata.get('HF_TOKEN')\n",
61
+ "os.environ['HUGGINGFACE_HUB_TOKEN'] = os.environ['HF_TOKEN']\n",
62
+ "print('HF_TOKEN set:', os.environ['HF_TOKEN'][:10] + '...')"
63
+ ]
64
+ },
65
+ {
66
+ "cell_type": "code",
67
+ "metadata": {},
68
+ "execution_count": null,
69
+ "outputs": [],
70
+ "source": [
71
+ "# moshi-finetune repo klonen und installieren\n",
72
+ "!git clone https://github.com/kyutai-labs/moshi-finetune.git\n",
73
+ "%cd moshi-finetune\n",
74
+ "!pip install -q -e ."
75
+ ]
76
+ },
77
+ {
78
+ "cell_type": "code",
79
+ "metadata": {},
80
+ "execution_count": null,
81
+ "outputs": [],
82
+ "source": [
83
+ "!pip install -q huggingface_hub sphn soundfile"
84
+ ]
85
+ },
86
+ {
87
+ "cell_type": "markdown",
88
+ "metadata": {},
89
+ "source": ["## 2. Dataset herunterladen"]
90
+ },
91
+ {
92
+ "cell_type": "code",
93
+ "metadata": {},
94
+ "execution_count": null,
95
+ "outputs": [],
96
+ "source": [
97
+ "from huggingface_hub import snapshot_download\n",
98
+ "\n",
99
+ "DATASET_REPO = 'playa555x/german-personaplex-10h'\n",
100
+ "dataset_path = snapshot_download(DATASET_REPO, repo_type='dataset')\n",
101
+ "print('Dataset at:', dataset_path)\n",
102
+ "\n",
103
+ "!ls -la {dataset_path}/kuechenstudio/ | head"
104
+ ]
105
+ },
106
+ {
107
+ "cell_type": "code",
108
+ "metadata": {},
109
+ "execution_count": null,
110
+ "outputs": [],
111
+ "source": [
112
+ "# Manifest fuer moshi-finetune vorbereiten — absolute Pfade\n",
113
+ "import json\n",
114
+ "from pathlib import Path\n",
115
+ "\n",
116
+ "src = Path(dataset_path) / 'manifest.jsonl'\n",
117
+ "manifest_entries = [json.loads(l) for l in src.read_text().splitlines() if l.strip()]\n",
118
+ "out = Path('/content/train_manifest.jsonl')\n",
119
+ "with out.open('w') as fh:\n",
120
+ " for e in manifest_entries:\n",
121
+ " e['path'] = str(Path(dataset_path) / e['path'])\n",
122
+ " fh.write(json.dumps(e) + '\\n')\n",
123
+ "print('Manifest:', out, '-', len(manifest_entries), 'samples')"
124
+ ]
125
+ },
126
+ {
127
+ "cell_type": "markdown",
128
+ "metadata": {},
129
+ "source": ["## 3. Base Model laden"]
130
+ },
131
+ {
132
+ "cell_type": "code",
133
+ "metadata": {},
134
+ "execution_count": null,
135
+ "outputs": [],
136
+ "source": [
137
+ "BASE_MODEL = 'kyutai/moshiko-pytorch-bf16'\n",
138
+ "model_path = snapshot_download(BASE_MODEL)\n",
139
+ "print('Base model:', model_path)\n",
140
+ "!ls -la {model_path}"
141
+ ]
142
+ },
143
+ {
144
+ "cell_type": "markdown",
145
+ "metadata": {},
146
+ "source": ["## 4. Training Config"]
147
+ },
148
+ {
149
+ "cell_type": "code",
150
+ "metadata": {},
151
+ "execution_count": null,
152
+ "outputs": [],
153
+ "source": [
154
+ "config_yaml = '''\n",
155
+ "# LoRA Fine-Tune Moshi -> Deutsch\n",
156
+ "run_dir: /content/checkpoints/moshi-de-lora-v1\n",
157
+ "\n",
158
+ "data:\n",
159
+ " train_data: /content/train_manifest.jsonl\n",
160
+ " eval_data: /content/train_manifest.jsonl\n",
161
+ " duration_sec: 30\n",
162
+ " shuffle: true\n",
163
+ "\n",
164
+ "model:\n",
165
+ " moshi_paths:\n",
166
+ " hf_repo_id: kyutai/moshiko-pytorch-bf16\n",
167
+ " lora:\n",
168
+ " enable: true\n",
169
+ " rank: 64\n",
170
+ " scaling: 2.0\n",
171
+ "\n",
172
+ "training:\n",
173
+ " batch_size: 2\n",
174
+ " gradient_accumulation: 4\n",
175
+ " optim:\n",
176
+ " lr: 1.0e-4\n",
177
+ " weight_decay: 0.01\n",
178
+ " max_steps: 2000\n",
179
+ " eval_freq: 500\n",
180
+ " save_freq: 500\n",
181
+ " log_freq: 20\n",
182
+ " seed: 42\n",
183
+ "\n",
184
+ "system:\n",
185
+ " mixed_precision: bf16\n",
186
+ " gradient_checkpointing: true\n",
187
+ " num_workers: 2\n",
188
+ "'''\n",
189
+ "\n",
190
+ "config_path = '/content/moshi-de-lora.yaml'\n",
191
+ "with open(config_path, 'w') as fh:\n",
192
+ " fh.write(config_yaml)\n",
193
+ "print('Config written:', config_path)"
194
+ ]
195
+ },
196
+ {
197
+ "cell_type": "markdown",
198
+ "metadata": {},
199
+ "source": ["## 5. Training starten"]
200
+ },
201
+ {
202
+ "cell_type": "code",
203
+ "metadata": {},
204
+ "execution_count": null,
205
+ "outputs": [],
206
+ "source": [
207
+ "# Lizenz fuer Moshi akzeptieren falls noch nicht\n",
208
+ "!huggingface-cli login --token $HF_TOKEN --add-to-git-credential\n",
209
+ "\n",
210
+ "# Training\n",
211
+ "%cd /content/moshi-finetune\n",
212
+ "!torchrun --nproc-per-node=1 -m train /content/moshi-de-lora.yaml 2>&1 | tee /content/train.log"
213
+ ]
214
+ },
215
+ {
216
+ "cell_type": "markdown",
217
+ "metadata": {},
218
+ "source": ["## 6. Checkpoint hochladen"]
219
+ },
220
+ {
221
+ "cell_type": "code",
222
+ "metadata": {},
223
+ "execution_count": null,
224
+ "outputs": [],
225
+ "source": [
226
+ "from huggingface_hub import HfApi, create_repo\n",
227
+ "import os\n",
228
+ "\n",
229
+ "CHECKPOINT_REPO = 'playa555x/moshi-de-lora-v1'\n",
230
+ "api = HfApi(token=os.environ['HF_TOKEN'])\n",
231
+ "create_repo(CHECKPOINT_REPO, private=True, exist_ok=True, token=os.environ['HF_TOKEN'])\n",
232
+ "\n",
233
+ "api.upload_folder(\n",
234
+ " folder_path='/content/checkpoints/moshi-de-lora-v1',\n",
235
+ " repo_id=CHECKPOINT_REPO,\n",
236
+ " commit_message='LoRA fine-tune: Moshi -> Deutsch (Kuechenstudio 10h)',\n",
237
+ ")\n",
238
+ "print(f'Checkpoint uploaded: https://huggingface.co/{CHECKPOINT_REPO}')"
239
+ ]
240
+ },
241
+ {
242
+ "cell_type": "markdown",
243
+ "metadata": {},
244
+ "source": [
245
+ "## 7. Quick-Test: Generate German Audio\n",
246
+ "\n",
247
+ "Nach dem Training: ein deutsches Sample erzeugen zum Anhoeren."
248
+ ]
249
+ },
250
+ {
251
+ "cell_type": "code",
252
+ "metadata": {},
253
+ "execution_count": null,
254
+ "outputs": [],
255
+ "source": [
256
+ "# Simple inference test — erzeuge 10s Audio als Reaktion auf deutschen Prompt\n",
257
+ "# Details je nach moshi-finetune API\n",
258
+ "!python -c \"print('Training complete. Download checkpoint for local inference.')\""
259
+ ]
260
+ }
261
+ ]
262
+ }