badaoui HF Staff commited on
Commit
a2376be
·
verified ·
1 Parent(s): 3e33548

Update optimum_neuron_export.py

Browse files
Files changed (1) hide show
  1. optimum_neuron_export.py +51 -12
optimum_neuron_export.py CHANGED
@@ -149,8 +149,10 @@ DIFFUSION_PIPELINE_MAPPING = {
149
 
150
  def get_default_inputs(task_or_pipeline: str, pipeline_name: str = None) -> Dict[str, int]:
151
  """Get default input shapes based on task type or diffusion pipeline type."""
152
- if task_or_pipeline in ["feature-extraction", "sentence-transformers", "fill-mask", "question-answering", "text-classification", "token-classification","text-generation"]:
153
  return {"batch_size": 1, "sequence_length": 128}
 
 
154
  elif task_or_pipeline == "multiple-choice":
155
  return {"batch_size": 1, "num_choices": 4, "sequence_length": 128}
156
  elif task_or_pipeline == "text2text-generation":
@@ -278,9 +280,7 @@ def export_transformer_model(model_id: str, task: str, folder: str, token: str)
278
  inputs = get_default_inputs(task)
279
  compiler_configs = {"auto_cast": "matmul", "auto_cast_type": "bf16", "instance_type": "inf2"}
280
  yield f"🔧 Using default inputs: {inputs}"
281
-
282
- # Clear any old cache artifacts before export
283
-
284
  try:
285
  # Trigger the export/compilation
286
  model = model_class.from_pretrained(
@@ -315,21 +315,60 @@ def export_transformer_model(model_id: str, task: str, folder: str, token: str)
315
  yield f"❌ Export failed with error: {e}"
316
  raise
317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
  def export_and_git_add(model_id: str, task_or_pipeline: str, model_type: str, folder: str, token: str, pipeline_name: str = None) -> Any:
319
 
320
  operations = []
321
 
322
  try:
323
  if model_type == "diffusers":
324
- # For diffusion models, use the new export function
325
- export_gen = export_diffusion_model(model_id, pipeline_name, task_or_pipeline, folder, token)
326
- for message in export_gen:
327
- yield message
328
  else:
329
- # For transformer models, use the existing function
330
- export_gen = export_transformer_model(model_id, task_or_pipeline, folder, token)
331
- for message in export_gen:
332
- yield message
 
 
 
333
 
334
  # Create operations from exported files
335
  for root, _, files in os.walk(folder):
 
149
 
150
  def get_default_inputs(task_or_pipeline: str, pipeline_name: str = None) -> Dict[str, int]:
151
  """Get default input shapes based on task type or diffusion pipeline type."""
152
+ if task_or_pipeline in ["feature-extraction", "sentence-transformers", "fill-mask", "question-answering", "text-classification", "token-classification"]:
153
  return {"batch_size": 1, "sequence_length": 128}
154
+ elif task_or_pipeline == "text_generation":
155
+ return {"batch_size": 1, "sequence_length": 128, "tensor_parallel_size": 4}
156
  elif task_or_pipeline == "multiple-choice":
157
  return {"batch_size": 1, "num_choices": 4, "sequence_length": 128}
158
  elif task_or_pipeline == "text2text-generation":
 
280
  inputs = get_default_inputs(task)
281
  compiler_configs = {"auto_cast": "matmul", "auto_cast_type": "bf16", "instance_type": "inf2"}
282
  yield f"🔧 Using default inputs: {inputs}"
283
+
 
 
284
  try:
285
  # Trigger the export/compilation
286
  model = model_class.from_pretrained(
 
315
  yield f"❌ Export failed with error: {e}"
316
  raise
317
 
318
+ def export_decoder_model(model_id: str, folder: str, token:str) -> Generator:
319
+ """Export decoder-only models using optimum.neuron"""
320
+
321
+ yield f"📦 Exporting decoder model `{model_id}` ..."
322
+
323
+ export_kwargs = get_default_inputs("text_generation")
324
+ yield f"🔧 Using default export_kwargs: {export_kwargs}"
325
+
326
+ try:
327
+ # Trigger the export/compilation
328
+ neuron_config = NeuronModelForCausalLM.get_neuron_config(model_name_or_path=model_id, **export_kwargs)
329
+ neuron_config.target = "inf2"
330
+ model = NeuronModelForCausalLM.export(
331
+ model_id=model_id, neuron_config=neuron_config,
332
+ )
333
+
334
+ yield "✅ Export/compilation completed successfully."
335
+
336
+ # Find the newly created cache artifacts
337
+ yield "🔍 Locating compiled artifacts in Neuron cache..."
338
+ cache_artifact_dir = find_neuron_cache_artifacts(cache_base_dir)
339
+
340
+ if not cache_artifact_dir:
341
+ raise Exception("❌ Could not find compiled artifacts in Neuron cache")
342
+
343
+ yield f"📂 Found artifacts at: {cache_artifact_dir}"
344
+
345
+ # Copy artifacts from cache to our target folder
346
+ yield f"📋 Copying artifacts to export folder..."
347
+ if os.path.exists(folder):
348
+ shutil.rmtree(folder)
349
+ shutil.copytree(cache_artifact_dir, folder)
350
+
351
+ yield f"✅ Artifacts successfully copied to {folder}"
352
+
353
+ except Exception as e:
354
+ yield f"❌ Export failed with error: {e}"
355
+ raise
356
+
357
  def export_and_git_add(model_id: str, task_or_pipeline: str, model_type: str, folder: str, token: str, pipeline_name: str = None) -> Any:
358
 
359
  operations = []
360
 
361
  try:
362
  if model_type == "diffusers":
363
+ export_gen = export_diffusion_model(model_id, pipeline_name, task_or_pipeline, folder, token)
 
 
 
364
  else:
365
+ if task_or_pipeline == "text_generation":
366
+ export_gen = export_decoder_model(model_id, folder, token)
367
+ else:
368
+ export_gen = export_transformer_model(model_id, task_or_pipeline, folder, token)
369
+
370
+ for message in export_gen:
371
+ yield message
372
 
373
  # Create operations from exported files
374
  for root, _, files in os.walk(folder):