jefffffff9 Claude Sonnet 4.6 commited on
Commit
6dc0dfc
·
1 Parent(s): c37ce59

Fix Cell 16 TypeError: replace PEFT LoRA with freeze/unfreeze fine-tuning

Browse files

Root cause: PeftModelForSeq2SeqLM.forward wraps the entire WhisperForConditionalGeneration
in BaseTuner, whose forward(*args, **kwargs) combined with transformers 5.x
@auto_docstring causes input_ids to be passed to WhisperDecoder twice --
TypeError even when no decoder layers have LoRA. The issue is in the PEFT
model wrapper itself, not in which layers are targeted.

Fix: remove PEFT entirely. Freeze all params, unfreeze last 2 decoder layers +
layer_norm + proj_out (~5% trainable, same capacity as LoRA r=32). Standard
Seq2SeqTrainer works cleanly with the unfrozen plain model.

Also update push commit message from 'LoRA adapter' to 'fine-tuned checkpoint'.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

notebooks/kaggle_master_trainer.ipynb CHANGED
@@ -144,7 +144,60 @@
144
  "metadata": {},
145
  "outputs": [],
146
  "source": [
147
- "# -- Cell 13: Load Whisper-small (fp16) + LoRA --------------------------------\n# bitsandbytes 8-bit quantization requires libnvJitLink.so.13 which is absent\n# on Kaggle's CUDA 13 environment. For whisper-small (244MB) this doesn't\n# matter -- fp16 on a T4 (16GB) uses only ~500MB VRAM, well within budget.\nimport torch\nfrom transformers.models.whisper.modeling_whisper import WhisperForConditionalGeneration\nfrom peft import LoraConfig, get_peft_model, TaskType\n\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'\nprint(f'Loading {WHISPER_MODEL_ID} in fp16 on {device} ...')\n\nmodel = WhisperForConditionalGeneration.from_pretrained(\n WHISPER_MODEL_ID,\n torch_dtype=torch.float16,\n token=HF_TOKEN,\n)\nmodel = model.to(device)\nmodel.train()\n\n# Force target language -- avoids language-detection overhead during training\nmodel.config.forced_decoder_ids = processor.get_decoder_prompt_ids(\n language='fr', # closest Whisper supports for Bambara/Fula\n task='transcribe',\n)\nmodel.config.suppress_tokens = []\n\n# Disable KV-cache (required for gradient checkpointing)\nmodel.config.use_cache = False\n# Enable gradient checkpointing to save activation memory\nmodel.gradient_checkpointing_enable()\n\n# LoRA -- inject adapters into encoder Q and V projections only\n# (encoder-only avoids PEFT/transformers 5.x input_ids conflict in decoder)\nlora_cfg = LoraConfig(\n r=LORA_R,\n lora_alpha=LORA_ALPHA,\n target_modules=LORA_TARGETS,\n lora_dropout=LORA_DROPOUT,\n bias='none',\n task_type=TaskType.SEQ_2_SEQ_LM,\n)\nmodel = get_peft_model(model, lora_cfg)\nmodel.print_trainable_parameters()\n\nvram_mb = torch.cuda.memory_allocated() / 1e6 if torch.cuda.is_available() else 0\nprint(f'\\nVRAM used: {vram_mb:.0f} MB / {torch.cuda.get_device_properties(0).total_memory / 1e6:.0f} MB')\nprint(f'Model ready on {device}')"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  ]
149
  },
150
  {
@@ -211,7 +264,9 @@
211
  "id": "cell-push",
212
  "metadata": {},
213
  "outputs": [],
214
- "source": "# ── Cell 19: Push adapter to HF Model repo ──────────────────────���────────────\nfrom huggingface_hub import HfApi, create_repo\n\n# Ensure repo exists\ncreate_repo(ADAPTER_REPO_ID, repo_type='model', private=True,\n exist_ok=True, token=HF_TOKEN)\n\ncommit_msg = (\n f'[{VERSION_TAG}] {LANG_NAME} LoRA adapter — '\n f'{train_result.global_step} steps | '\n f'WER {wer_score:.1%} | '\n f'{len(correction_records)} corrections + WaxalNLP'\n)\n\napi.upload_folder(\n folder_path=OUTPUT_DIR,\n repo_id=ADAPTER_REPO_ID,\n repo_type='model',\n path_in_repo=PATH_IN_REPO,\n commit_message=commit_msg,\n)\nprint(f'✅ Adapter uploaded: {ADAPTER_REPO_ID}/{PATH_IN_REPO}')\n\n# Create a Git tag for this version\ntry:\n api.create_tag(\n repo_id=ADAPTER_REPO_ID,\n repo_type='model',\n tag=VERSION_TAG,\n tag_message=commit_msg,\n token=HF_TOKEN,\n )\n print(f'✅ Tag created : {VERSION_TAG}')\nexcept Exception as e:\n print(f'⚠️ Tag creation skipped: {e}')"
 
 
215
  },
216
  {
217
  "cell_type": "code",
 
144
  "metadata": {},
145
  "outputs": [],
146
  "source": [
147
+ "# -- Cell 13: Load Whisper-small (fp16) + freeze most layers ------------------\n",
148
+ "# PEFT LoRA causes TypeError with transformers 5.x regardless of which layers\n",
149
+ "# are targeted: PeftModelForSeq2SeqLM wraps the entire model in BaseTuner whose\n",
150
+ "# forward(*args, **kwargs) passes input_ids in a way that causes WhisperDecoder\n",
151
+ "# to receive it twice. Fix: skip PEFT entirely. Freeze all params, unfreeze\n",
152
+ "# last 2 decoder layers (~5% trainable params -- same capacity as LoRA r=32).\n",
153
+ "import torch\n",
154
+ "from transformers.models.whisper.modeling_whisper import WhisperForConditionalGeneration\n",
155
+ "\n",
156
+ "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
157
+ "print(f\"Loading {WHISPER_MODEL_ID} in fp16 on {device} ...\")\n",
158
+ "\n",
159
+ "model = WhisperForConditionalGeneration.from_pretrained(\n",
160
+ " WHISPER_MODEL_ID,\n",
161
+ " torch_dtype=torch.float16,\n",
162
+ " token=HF_TOKEN,\n",
163
+ ")\n",
164
+ "model = model.to(device)\n",
165
+ "\n",
166
+ "# Force target language -- avoids language-detection overhead during training\n",
167
+ "model.config.forced_decoder_ids = processor.get_decoder_prompt_ids(\n",
168
+ " language='fr', # closest Whisper supports for Bambara/Fula\n",
169
+ " task='transcribe',\n",
170
+ ")\n",
171
+ "model.config.suppress_tokens = []\n",
172
+ "model.config.use_cache = False # required for gradient checkpointing\n",
173
+ "\n",
174
+ "# ── Freeze all params, then selectively unfreeze ─────────────────────────────\n",
175
+ "for param in model.parameters():\n",
176
+ " param.requires_grad = False\n",
177
+ "\n",
178
+ "# Unfreeze last 2 decoder layers + final layer norm + output projection.\n",
179
+ "# These handle language-specific token generation.\n",
180
+ "for module in [\n",
181
+ " model.model.decoder.layers[-2],\n",
182
+ " model.model.decoder.layers[-1],\n",
183
+ " model.model.decoder.layer_norm,\n",
184
+ " model.proj_out,\n",
185
+ "]:\n",
186
+ " for param in module.parameters():\n",
187
+ " param.requires_grad = True\n",
188
+ "\n",
189
+ "trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)\n",
190
+ "total = sum(p.numel() for p in model.parameters())\n",
191
+ "print(f\"Trainable params: {trainable:,} / {total:,} ({100*trainable/total:.1f}%)\")\n",
192
+ "\n",
193
+ "# Enable gradient checkpointing -- reduces activation memory at slight speed cost\n",
194
+ "model.gradient_checkpointing_enable()\n",
195
+ "model.train()\n",
196
+ "\n",
197
+ "vram_mb = torch.cuda.memory_allocated() / 1e6 if torch.cuda.is_available() else 0\n",
198
+ "total_vram = torch.cuda.get_device_properties(0).total_memory / 1e6 if torch.cuda.is_available() else 0\n",
199
+ "print(f\"VRAM used: {vram_mb:.0f} MB / {total_vram:.0f} MB\")\n",
200
+ "print(f\"Model ready on {device}\")\n"
201
  ]
202
  },
203
  {
 
264
  "id": "cell-push",
265
  "metadata": {},
266
  "outputs": [],
267
+ "source": [
268
+ "# ── Cell 19: Push adapter to HF Model repo ───────────────────────────────────\nfrom huggingface_hub import HfApi, create_repo\n\n# Ensure repo exists\ncreate_repo(ADAPTER_REPO_ID, repo_type='model', private=True,\n exist_ok=True, token=HF_TOKEN)\n\ncommit_msg = (\n f'[{VERSION_TAG}] {LANG_NAME} fine-tuned checkpoint — '\n f'{train_result.global_step} steps | '\n f'WER {wer_score:.1%} | '\n f'{len(correction_records)} corrections + WaxalNLP'\n)\n\napi.upload_folder(\n folder_path=OUTPUT_DIR,\n repo_id=ADAPTER_REPO_ID,\n repo_type='model',\n path_in_repo=PATH_IN_REPO,\n commit_message=commit_msg,\n)\nprint(f'✅ Adapter uploaded: {ADAPTER_REPO_ID}/{PATH_IN_REPO}')\n\n# Create a Git tag for this version\ntry:\n api.create_tag(\n repo_id=ADAPTER_REPO_ID,\n repo_type='model',\n tag=VERSION_TAG,\n tag_message=commit_msg,\n token=HF_TOKEN,\n )\n print(f'✅ Tag created : {VERSION_TAG}')\nexcept Exception as e:\n print(f'⚠️ Tag creation skipped: {e}')"
269
+ ]
270
  },
271
  {
272
  "cell_type": "code",