Spaces:
Running
Running
some cleaning
Browse files- optimum_neuron_export.py +16 -30
optimum_neuron_export.py
CHANGED
|
@@ -39,7 +39,6 @@ from optimum.neuron import (
|
|
| 39 |
NeuronModelForSeq2SeqLM,
|
| 40 |
)
|
| 41 |
|
| 42 |
-
# Import diffusers pipelines
|
| 43 |
from diffusers import (
|
| 44 |
StableDiffusionPipeline,
|
| 45 |
StableDiffusionImg2ImgPipeline,
|
|
@@ -339,9 +338,9 @@ def export_decoder_model(model_id: str, folder: str, token:str) -> Generator:
|
|
| 339 |
raise
|
| 340 |
|
| 341 |
def export_and_git_add(model_id: str, task_or_pipeline: str, model_type: str, folder: str, token: str, pipeline_name: str = None) -> Any:
|
| 342 |
-
|
| 343 |
-
operations = []
|
| 344 |
|
|
|
|
| 345 |
try:
|
| 346 |
if model_type == "diffusers":
|
| 347 |
export_gen = export_diffusion_model(model_id, pipeline_name, task_or_pipeline, folder, token)
|
|
@@ -372,7 +371,6 @@ def export_and_git_add(model_id: str, task_or_pipeline: str, model_type: str, fo
|
|
| 372 |
readme_path = os.path.join(folder, "README.md")
|
| 373 |
card.save(readme_path)
|
| 374 |
|
| 375 |
-
# Check if README.md is already in operations, if so update, else add
|
| 376 |
readme_op = next((op for op in operations if op.path_in_repo == "README.md"), None)
|
| 377 |
if readme_op:
|
| 378 |
readme_op.path_or_fileobj = readme_path
|
|
@@ -390,6 +388,7 @@ def export_and_git_add(model_id: str, task_or_pipeline: str, model_type: str, fo
|
|
| 390 |
|
| 391 |
def generate_neuron_repo_name(api, original_model_id: str, task_or_pipeline: str, token:str) -> str:
|
| 392 |
"""Generate a name for the Neuron-optimized repository."""
|
|
|
|
| 393 |
requesting_user = api.whoami(token=token)["name"]
|
| 394 |
base_name = original_model_id.replace('/', '-')
|
| 395 |
return f"{requesting_user}/{base_name}-neuron"
|
|
@@ -403,11 +402,9 @@ def create_neuron_repo_and_upload(
|
|
| 403 |
token: str,
|
| 404 |
pipeline_name: str = None,
|
| 405 |
) -> Generator[Union[str, RepoUrl], None, None]:
|
| 406 |
-
"""
|
| 407 |
-
|
| 408 |
-
"""
|
| 409 |
api = HfApi(token=token)
|
| 410 |
-
|
| 411 |
if task_or_pipeline == "auto" and model_type == "transformers":
|
| 412 |
try:
|
| 413 |
task_or_pipeline = TasksManager.infer_task_from_model(original_model_id, token=token)
|
|
@@ -418,7 +415,6 @@ def create_neuron_repo_and_upload(
|
|
| 418 |
neuron_repo_name = generate_neuron_repo_name(api, original_model_id, task_or_pipeline, token)
|
| 419 |
|
| 420 |
try:
|
| 421 |
-
# Create the repository
|
| 422 |
repo_url = create_repo(
|
| 423 |
repo_id=neuron_repo_name,
|
| 424 |
token=token,
|
|
@@ -427,7 +423,6 @@ def create_neuron_repo_and_upload(
|
|
| 427 |
exist_ok=True,
|
| 428 |
)
|
| 429 |
|
| 430 |
-
# Get the appropriate class name for the Python example
|
| 431 |
if model_type == "transformers":
|
| 432 |
model_class = TASK_TO_MODEL_CLASS.get(task_or_pipeline)
|
| 433 |
model_class_name = model_class.__name__ if model_class else "NeuronModel"
|
|
@@ -492,12 +487,10 @@ For the original model, training details, and more information, please visit: [{
|
|
| 492 |
# Update the README in operations
|
| 493 |
readme_op = next((op for op in operations if op.path_in_repo == "README.md"), None)
|
| 494 |
if readme_op:
|
| 495 |
-
# Create a temporary file with the new content
|
| 496 |
with NamedTemporaryFile(mode='w', suffix='.md', delete=False) as f:
|
| 497 |
f.write(neuron_readme_content)
|
| 498 |
readme_op.path_or_fileobj = f.name
|
| 499 |
else:
|
| 500 |
-
# Add new README operation
|
| 501 |
with NamedTemporaryFile(mode='w', suffix='.md', delete=False) as f:
|
| 502 |
f.write(neuron_readme_content)
|
| 503 |
operations.append(CommitOperationAdd(path_in_repo="README.md", path_or_fileobj=f.name))
|
|
@@ -535,11 +528,9 @@ def create_readme_pr_for_original_model(
|
|
| 535 |
requesting_user: str,
|
| 536 |
token: str,
|
| 537 |
) -> Generator[Union[str, CommitInfo], None, None]:
|
| 538 |
-
"""
|
| 539 |
-
|
| 540 |
-
"""
|
| 541 |
api = HfApi(token=token)
|
| 542 |
-
|
| 543 |
yield f"📝 Creating PR to add Neuron repo link in {original_model_id}..."
|
| 544 |
|
| 545 |
try:
|
|
@@ -561,7 +552,6 @@ def create_readme_pr_for_original_model(
|
|
| 561 |
with open(current_readme_path, 'r', encoding='utf-8') as f:
|
| 562 |
readme_content = f.read()
|
| 563 |
except Exception:
|
| 564 |
-
# If README doesn't exist, create a basic one
|
| 565 |
readme_content = f"# {original_model_id}\n\n"
|
| 566 |
|
| 567 |
# Add Neuron optimization section, separated by a horizontal rule
|
|
@@ -626,11 +616,9 @@ def upload_to_custom_repo(
|
|
| 626 |
requesting_user: str,
|
| 627 |
token: str,
|
| 628 |
) -> Generator[Union[str, CommitInfo], None, None]:
|
| 629 |
-
"""
|
| 630 |
-
Uploads neuron files to a custom repository and creates a PR.
|
| 631 |
-
"""
|
| 632 |
-
api = HfApi(token=token)
|
| 633 |
|
|
|
|
| 634 |
try:
|
| 635 |
# Ensure the custom repo exists
|
| 636 |
api.repo_info(repo_id=custom_repo_id, repo_type="model")
|
|
@@ -702,7 +690,7 @@ def convert(
|
|
| 702 |
result_info = {}
|
| 703 |
|
| 704 |
try:
|
| 705 |
-
#
|
| 706 |
export_gen = export_and_git_add(model_id, task_or_pipeline, model_type, export_folder, token=token, pipeline_name=pipeline_name)
|
| 707 |
operations = None
|
| 708 |
for message in export_gen:
|
|
@@ -715,7 +703,7 @@ def convert(
|
|
| 715 |
if not operations:
|
| 716 |
raise Exception("Export process did not produce any files to commit.")
|
| 717 |
|
| 718 |
-
#
|
| 719 |
if pr_options.get("create_cache_pr"):
|
| 720 |
yield "0", f"📤 Creating a Pull Request for the cache repository ..."
|
| 721 |
|
|
@@ -731,7 +719,7 @@ This PR adds newly compiled cache artifacts for the model:
|
|
| 731 |
These files contain precompiled Neuron-optimized representations of the model, allowing faster loading and inference on AWS Neuron hardware.
|
| 732 |
"""
|
| 733 |
|
| 734 |
-
#
|
| 735 |
commit_message = f"Synchronizing local compiler cache of {model_id}"
|
| 736 |
inputs = get_default_inputs(task_or_pipeline, pipeline_name)
|
| 737 |
commit_description = f"""
|
|
@@ -755,7 +743,7 @@ These files contain precompiled Neuron-optimized representations of the model, a
|
|
| 755 |
)
|
| 756 |
|
| 757 |
pr_url = None
|
| 758 |
-
#
|
| 759 |
while True:
|
| 760 |
try:
|
| 761 |
# Get the next status message from your generator
|
|
@@ -766,7 +754,7 @@ These files contain precompiled Neuron-optimized representations of the model, a
|
|
| 766 |
pr_url = e.value
|
| 767 |
break # Exit the loop
|
| 768 |
|
| 769 |
-
#
|
| 770 |
if pr_url:
|
| 771 |
yield "0", f"✅ Successfully captured PR URL."
|
| 772 |
result_info["cache_pr"] = pr_url
|
|
@@ -776,11 +764,10 @@ These files contain precompiled Neuron-optimized representations of the model, a
|
|
| 776 |
except Exception as e:
|
| 777 |
yield "0", f"❌ Failed to create cache PR: {e}"
|
| 778 |
|
| 779 |
-
#
|
| 780 |
if pr_options.get("create_neuron_repo"):
|
| 781 |
yield "0", "🏗️ Creating new Neuron-optimized repository..."
|
| 782 |
neuron_repo_url = None
|
| 783 |
-
# Generate the repo name first so we can use it consistently
|
| 784 |
neuron_repo_name = generate_neuron_repo_name(api, model_id, task_or_pipeline, token)
|
| 785 |
|
| 786 |
repo_creation_gen = create_neuron_repo_and_upload(
|
|
@@ -809,7 +796,7 @@ These files contain precompiled Neuron-optimized representations of the model, a
|
|
| 809 |
if readme_pr:
|
| 810 |
result_info["readme_pr"] = f"https://huggingface.co/{model_id}/discussions/{readme_pr.pr_num}"
|
| 811 |
|
| 812 |
-
#
|
| 813 |
if pr_options.get("create_custom_pr"):
|
| 814 |
custom_repo_id = pr_options["custom_repo_id"]
|
| 815 |
yield "0", f"📤 Creating PR in custom repository: {custom_repo_id}..."
|
|
@@ -827,5 +814,4 @@ These files contain precompiled Neuron-optimized representations of the model, a
|
|
| 827 |
|
| 828 |
except Exception as e:
|
| 829 |
yield "1", f"❌ Conversion failed with a critical error: {e}"
|
| 830 |
-
# Re-raise the exception to be caught by the outer try-except in the Gradio app if needed
|
| 831 |
raise
|
|
|
|
| 39 |
NeuronModelForSeq2SeqLM,
|
| 40 |
)
|
| 41 |
|
|
|
|
| 42 |
from diffusers import (
|
| 43 |
StableDiffusionPipeline,
|
| 44 |
StableDiffusionImg2ImgPipeline,
|
|
|
|
| 338 |
raise
|
| 339 |
|
| 340 |
def export_and_git_add(model_id: str, task_or_pipeline: str, model_type: str, folder: str, token: str, pipeline_name: str = None) -> Any:
|
| 341 |
+
"""Export model and git add it."""
|
|
|
|
| 342 |
|
| 343 |
+
operations = []
|
| 344 |
try:
|
| 345 |
if model_type == "diffusers":
|
| 346 |
export_gen = export_diffusion_model(model_id, pipeline_name, task_or_pipeline, folder, token)
|
|
|
|
| 371 |
readme_path = os.path.join(folder, "README.md")
|
| 372 |
card.save(readme_path)
|
| 373 |
|
|
|
|
| 374 |
readme_op = next((op for op in operations if op.path_in_repo == "README.md"), None)
|
| 375 |
if readme_op:
|
| 376 |
readme_op.path_or_fileobj = readme_path
|
|
|
|
| 388 |
|
| 389 |
def generate_neuron_repo_name(api, original_model_id: str, task_or_pipeline: str, token:str) -> str:
|
| 390 |
"""Generate a name for the Neuron-optimized repository."""
|
| 391 |
+
|
| 392 |
requesting_user = api.whoami(token=token)["name"]
|
| 393 |
base_name = original_model_id.replace('/', '-')
|
| 394 |
return f"{requesting_user}/{base_name}-neuron"
|
|
|
|
| 402 |
token: str,
|
| 403 |
pipeline_name: str = None,
|
| 404 |
) -> Generator[Union[str, RepoUrl], None, None]:
|
| 405 |
+
"""Creates a new repository with Neuron files and uploads them."""
|
| 406 |
+
|
|
|
|
| 407 |
api = HfApi(token=token)
|
|
|
|
| 408 |
if task_or_pipeline == "auto" and model_type == "transformers":
|
| 409 |
try:
|
| 410 |
task_or_pipeline = TasksManager.infer_task_from_model(original_model_id, token=token)
|
|
|
|
| 415 |
neuron_repo_name = generate_neuron_repo_name(api, original_model_id, task_or_pipeline, token)
|
| 416 |
|
| 417 |
try:
|
|
|
|
| 418 |
repo_url = create_repo(
|
| 419 |
repo_id=neuron_repo_name,
|
| 420 |
token=token,
|
|
|
|
| 423 |
exist_ok=True,
|
| 424 |
)
|
| 425 |
|
|
|
|
| 426 |
if model_type == "transformers":
|
| 427 |
model_class = TASK_TO_MODEL_CLASS.get(task_or_pipeline)
|
| 428 |
model_class_name = model_class.__name__ if model_class else "NeuronModel"
|
|
|
|
| 487 |
# Update the README in operations
|
| 488 |
readme_op = next((op for op in operations if op.path_in_repo == "README.md"), None)
|
| 489 |
if readme_op:
|
|
|
|
| 490 |
with NamedTemporaryFile(mode='w', suffix='.md', delete=False) as f:
|
| 491 |
f.write(neuron_readme_content)
|
| 492 |
readme_op.path_or_fileobj = f.name
|
| 493 |
else:
|
|
|
|
| 494 |
with NamedTemporaryFile(mode='w', suffix='.md', delete=False) as f:
|
| 495 |
f.write(neuron_readme_content)
|
| 496 |
operations.append(CommitOperationAdd(path_in_repo="README.md", path_or_fileobj=f.name))
|
|
|
|
| 528 |
requesting_user: str,
|
| 529 |
token: str,
|
| 530 |
) -> Generator[Union[str, CommitInfo], None, None]:
|
| 531 |
+
"""Creates a PR on the original model repository to add a link to the Neuron-optimized version."""
|
| 532 |
+
|
|
|
|
| 533 |
api = HfApi(token=token)
|
|
|
|
| 534 |
yield f"📝 Creating PR to add Neuron repo link in {original_model_id}..."
|
| 535 |
|
| 536 |
try:
|
|
|
|
| 552 |
with open(current_readme_path, 'r', encoding='utf-8') as f:
|
| 553 |
readme_content = f.read()
|
| 554 |
except Exception:
|
|
|
|
| 555 |
readme_content = f"# {original_model_id}\n\n"
|
| 556 |
|
| 557 |
# Add Neuron optimization section, separated by a horizontal rule
|
|
|
|
| 616 |
requesting_user: str,
|
| 617 |
token: str,
|
| 618 |
) -> Generator[Union[str, CommitInfo], None, None]:
|
| 619 |
+
"""Uploads neuron files to a custom repository and creates a PR."""
|
|
|
|
|
|
|
|
|
|
| 620 |
|
| 621 |
+
api = HfApi(token=token)
|
| 622 |
try:
|
| 623 |
# Ensure the custom repo exists
|
| 624 |
api.repo_info(repo_id=custom_repo_id, repo_type="model")
|
|
|
|
| 690 |
result_info = {}
|
| 691 |
|
| 692 |
try:
|
| 693 |
+
# Export Logic
|
| 694 |
export_gen = export_and_git_add(model_id, task_or_pipeline, model_type, export_folder, token=token, pipeline_name=pipeline_name)
|
| 695 |
operations = None
|
| 696 |
for message in export_gen:
|
|
|
|
| 703 |
if not operations:
|
| 704 |
raise Exception("Export process did not produce any files to commit.")
|
| 705 |
|
| 706 |
+
# Cache Handling
|
| 707 |
if pr_options.get("create_cache_pr"):
|
| 708 |
yield "0", f"📤 Creating a Pull Request for the cache repository ..."
|
| 709 |
|
|
|
|
| 719 |
These files contain precompiled Neuron-optimized representations of the model, allowing faster loading and inference on AWS Neuron hardware.
|
| 720 |
"""
|
| 721 |
|
| 722 |
+
# Create an instance of your generator
|
| 723 |
commit_message = f"Synchronizing local compiler cache of {model_id}"
|
| 724 |
inputs = get_default_inputs(task_or_pipeline, pipeline_name)
|
| 725 |
commit_description = f"""
|
|
|
|
| 743 |
)
|
| 744 |
|
| 745 |
pr_url = None
|
| 746 |
+
# Loop to process yielded status messages and capture the final return value
|
| 747 |
while True:
|
| 748 |
try:
|
| 749 |
# Get the next status message from your generator
|
|
|
|
| 754 |
pr_url = e.value
|
| 755 |
break # Exit the loop
|
| 756 |
|
| 757 |
+
# Process the final result
|
| 758 |
if pr_url:
|
| 759 |
yield "0", f"✅ Successfully captured PR URL."
|
| 760 |
result_info["cache_pr"] = pr_url
|
|
|
|
| 764 |
except Exception as e:
|
| 765 |
yield "0", f"❌ Failed to create cache PR: {e}"
|
| 766 |
|
| 767 |
+
# New Repository Creation (Replaces Model PR)
|
| 768 |
if pr_options.get("create_neuron_repo"):
|
| 769 |
yield "0", "🏗️ Creating new Neuron-optimized repository..."
|
| 770 |
neuron_repo_url = None
|
|
|
|
| 771 |
neuron_repo_name = generate_neuron_repo_name(api, model_id, task_or_pipeline, token)
|
| 772 |
|
| 773 |
repo_creation_gen = create_neuron_repo_and_upload(
|
|
|
|
| 796 |
if readme_pr:
|
| 797 |
result_info["readme_pr"] = f"https://huggingface.co/{model_id}/discussions/{readme_pr.pr_num}"
|
| 798 |
|
| 799 |
+
# Custom Repository PR
|
| 800 |
if pr_options.get("create_custom_pr"):
|
| 801 |
custom_repo_id = pr_options["custom_repo_id"]
|
| 802 |
yield "0", f"📤 Creating PR in custom repository: {custom_repo_id}..."
|
|
|
|
| 814 |
|
| 815 |
except Exception as e:
|
| 816 |
yield "1", f"❌ Conversion failed with a critical error: {e}"
|
|
|
|
| 817 |
raise
|