Alikestocode commited on
Commit
671d7f9
Β·
1 Parent(s): 35d8225

Fix oneshot() API: use Recipe and Dataset objects

Browse files

- Import Recipe from llmcompressor.recipe and Dataset from datasets
- Convert modifiers list to Recipe object using Recipe.from_modifiers()
- Convert calibration_texts to Hugging Face Dataset using Dataset.from_dict()
- Use use_auth_token=True instead of token parameter
- All kwargs now map to ModelArguments, DatasetArguments, RecipeArguments
- Fixes HfArgumentParser 'Some keys are not used' error

Files changed (1) hide show
  1. quantize_to_awq_colab.ipynb +31 -14
quantize_to_awq_colab.ipynb CHANGED
@@ -189,6 +189,8 @@
189
  "\n",
190
  "from transformers import AutoTokenizer\n",
191
  "from huggingface_hub import HfApi, scan_cache_dir, upload_folder\n",
 
 
192
  "import torch\n",
193
  "import shutil\n",
194
  "import gc\n",
@@ -346,27 +348,42 @@
346
  " print(f\" βœ… AWQModifier created successfully\")\n",
347
  " \n",
348
  " # Call oneshot with the modifier\n",
349
- " # Correct API based on llm-compressor documentation:\n",
350
- " # oneshot(model, dataset, recipe, output_dir, max_seq_length, num_calibration_samples)\n",
351
  " print(f\" β†’ Starting quantization process...\")\n",
352
  " \n",
353
  " # Prepare calibration dataset (limit to reasonable size)\n",
354
- " calibration_dataset = calibration_texts[:min(calibration_dataset_size, 128)]\n",
355
  " \n",
356
- " # oneshot() API parameters:\n",
357
- " # - model: model ID or path\n",
358
- " # - dataset: dataset name (string) or list of calibration strings\n",
359
- " # - recipe: list of modifiers\n",
360
- " # - output_dir: output directory\n",
361
- " # - max_seq_length: optional, max sequence length\n",
362
- " # - num_calibration_samples: optional, number of calibration samples\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
363
  " oneshot(\n",
364
  " model=repo_id,\n",
365
- " dataset=calibration_dataset, # List of calibration strings\n",
366
- " recipe=modifiers, # List of modifiers (e.g., [AWQModifier(...)])\n",
367
  " output_dir=temp_output_dir,\n",
368
- " max_seq_length=2048, # Optional: max sequence length\n",
369
- " num_calibration_samples=len(calibration_dataset) # Optional: number of samples\n",
 
 
 
370
  " )\n",
371
  " \n",
372
  " print(f\"βœ… Model quantized to AWQ successfully\")\n",
 
189
  "\n",
190
  "from transformers import AutoTokenizer\n",
191
  "from huggingface_hub import HfApi, scan_cache_dir, upload_folder\n",
192
+ "from datasets import Dataset\n",
193
+ "from llmcompressor.recipe import Recipe\n",
194
  "import torch\n",
195
  "import shutil\n",
196
  "import gc\n",
 
348
  " print(f\" βœ… AWQModifier created successfully\")\n",
349
  " \n",
350
  " # Call oneshot with the modifier\n",
351
+ " # oneshot() uses HfArgumentParser which only understands ModelArguments, DatasetArguments, RecipeArguments\n",
352
+ " # We need to convert modifiers to Recipe and calibration_texts to Dataset\n",
353
  " print(f\" β†’ Starting quantization process...\")\n",
354
  " \n",
355
  " # Prepare calibration dataset (limit to reasonable size)\n",
356
+ " calibration_texts_limited = calibration_texts[:min(calibration_dataset_size, 128)]\n",
357
  " \n",
358
+ " # Convert calibration texts to Hugging Face Dataset\n",
359
+ " # DatasetArguments expects a Dataset object, not a list\n",
360
+ " print(f\" β†’ Creating Hugging Face Dataset from calibration texts...\")\n",
361
+ " calibration_dataset = Dataset.from_dict({\"text\": calibration_texts_limited})\n",
362
+ " print(f\" βœ… Created dataset with {len(calibration_dataset)} samples\")\n",
363
+ " \n",
364
+ " # Convert modifiers list to Recipe object\n",
365
+ " # RecipeArguments expects a Recipe object, not a list of modifiers\n",
366
+ " print(f\" β†’ Converting modifiers to Recipe object...\")\n",
367
+ " recipe = Recipe.from_modifiers(modifiers)\n",
368
+ " print(f\" βœ… Recipe created from modifiers\")\n",
369
+ " \n",
370
+ " # oneshot() API - all kwargs must map to ModelArguments, DatasetArguments, or RecipeArguments\n",
371
+ " # - model: ModelArguments.model\n",
372
+ " # - output_dir: ModelArguments.output_dir\n",
373
+ " # - recipe: RecipeArguments.recipe (Recipe object)\n",
374
+ " # - dataset: DatasetArguments.dataset (Dataset object)\n",
375
+ " # - num_calibration_samples: DatasetArguments.num_calibration_samples\n",
376
+ " # - use_auth_token: ModelArguments.use_auth_token (reads from HF_TOKEN env var)\n",
377
+ " # - trust_remote_code_model: ModelArguments.trust_remote_code_model\n",
378
+ " print(f\" β†’ Calling oneshot() with proper argument structure...\")\n",
379
  " oneshot(\n",
380
  " model=repo_id,\n",
 
 
381
  " output_dir=temp_output_dir,\n",
382
+ " recipe=recipe,\n",
383
+ " dataset=calibration_dataset,\n",
384
+ " num_calibration_samples=min(calibration_dataset_size, len(calibration_dataset)),\n",
385
+ " use_auth_token=True, # Reads from os.environ[\"HF_TOKEN\"]\n",
386
+ " trust_remote_code_model=True\n",
387
  " )\n",
388
  " \n",
389
  " print(f\"βœ… Model quantized to AWQ successfully\")\n",