Skip to content

Add Python API for all CLI commands with WorkflowOutput returns#1907

Merged
jambayk merged 22 commits into
mainfrom
copilot/fix-1906
Jun 19, 2025
Merged

Add Python API for all CLI commands with WorkflowOutput returns#1907
jambayk merged 22 commits into
mainfrom
copilot/fix-1906

Conversation

Copilot AI commented Jun 10, 2025

Copy link
Copy Markdown
Contributor

This PR implements a comprehensive Python API for all Olive CLI commands, allowing users to programmatically execute workflows and receive structured WorkflowOutput objects containing ModelOutput instances.

Overview

The new Python API provides 13 functions corresponding to all CLI commands:

Workflow Functions (return WorkflowOutput with ModelOutput instances):

  • auto_opt() - Auto-optimize models for performance
  • finetune() - Fine-tune models using LoRA/QLoRA
  • quantize() - Quantize models for reduced size
  • capture_onnx() - Capture ONNX graphs from PyTorch models
  • generate_adapter() - Generate adapters for ONNX models
  • session_params_tuning() - Tune ONNX Runtime parameters
  • run() - Execute workflows from configuration

Utility Functions (perform operations, return None):

  • configure_qualcomm_sdk() - Configure Qualcomm SDK
  • convert_adapters() - Convert adapter formats
  • extract_adapters() - Extract LoRA adapters
  • generate_cost_model() - Generate cost models for splitting
  • manage_aml_compute() - Manage AzureML compute resources
  • shared_cache() - Manage shared cache operations

Usage Examples

from olive import auto_opt, finetune, extract_adapters

# Auto-optimize a model
result = auto_opt(
    model_path="microsoft/phi-3-mini-4k-instruct",
    device="cpu", 
    precision="int8"
)

# Access the best optimized model
if result.has_output_model():
    best_model = result.get_best_candidate()
    print(f"Model: {best_model.model_path}")
    print(f"Metrics: {best_model.metrics_value}")

# Fine-tune with LoRA
adapter = finetune(
    model_path="microsoft/phi-3-mini-4k-instruct",
    data_name="squad",
    method="lora",
    num_train_epochs=3
)

# Extract adapters (utility function)
extract_adapters(
    model_path="./model_with_lora",
    output_path="./extracted",
    format="onnx_adapter"
)

Key Benefits

  • Same functionality as CLI but with programmatic access
  • Structured returns - WorkflowOutput objects for optimization workflows
  • Python-native parameters instead of command-line arguments
  • Proper error handling and directory management
  • Full workflow traceability through ModelOutput instances

Files Added

  • olive/api/__init__.py - API module exports
  • olive/api/workflow.py - All API function implementations
  • olive/api/README.md - Comprehensive documentation
  • examples/python_api_usage.py - Usage examples
  • test/unit_test/test_python_api.py - Unit tests

Files Modified

  • olive/__init__.py - Export API functions from main module

The API maintains full compatibility with existing CLI functionality while providing a more convenient interface for programmatic use.

Fixes #1906.

Copilot AI and others added 3 commits June 10, 2025 23:50
Co-authored-by: xiaoyu-work <85524621+xiaoyu-work@users.noreply.github.com>
…ation

Co-authored-by: xiaoyu-work <85524621+xiaoyu-work@users.noreply.github.com>
Co-authored-by: xiaoyu-work <85524621+xiaoyu-work@users.noreply.github.com>
Copilot AI changed the title [WIP] [FR]: Create Python API for CLI command Add Python API for all CLI commands with WorkflowOutput returns Jun 10, 2025
Copilot AI requested a review from xiaoyu-work June 10, 2025 23:59
@devang-ml

Copy link
Copy Markdown
Collaborator

We don't need python APIs for following CLIs.

shared_cache,
manage_aml_compute,
configure_qualcomm_sdk

…e_qualcomm_sdk as requested

Co-authored-by: devang-ml <47577486+devang-ml@users.noreply.github.com>

Copilot AI commented Jun 12, 2025

Copy link
Copy Markdown
Contributor Author

We don't need python APIs for following CLIs.

shared_cache,
...

Removed the Python APIs for shared_cache, manage_aml_compute, and configure_qualcomm_sdk as requested. Updated all documentation, tests, and examples to reflect these changes. Commit: 99c5a40

@devang-ml

Copy link
Copy Markdown
Collaborator

Let's add documentation on how to use these APIs.

docs/source/how-to/index.rst documents how to use CLI and how to use .json configuration. Let's update this page to cover ho to use python APIs.

Co-authored-by: devang-ml <47577486+devang-ml@users.noreply.github.com>

Copilot AI commented Jun 12, 2025

Copy link
Copy Markdown
Contributor Author

Let's add documentation on how to use these APIs.

docs/source/how-to/index.rst documents how to use CLI and how to use .json configuration. Let's update this page to cover ho to use python APIs.

Added comprehensive Python API documentation to the docs/source/how-to/index.rst page. Created a new "Working with the Python API" section with detailed documentation for auto_opt(), finetune(), quantize(), run(), and an overview page with usage patterns and examples. The documentation follows the same structure as the CLI documentation with function signatures, parameters, return values, and integration examples. Commit: f7051f7

@xiaoyu-work

Copy link
Copy Markdown
Collaborator

The Python API should be in one single documentation page with different sections.

Comment thread olive/api/workflow.py Fixed
Comment thread olive/api/workflow.py Fixed

@github-advanced-security github-advanced-security AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lintrunner found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

Comment thread olive/api/README.md Outdated
@@ -0,0 +1,117 @@
# Olive Python API

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file, combine this content to documentation you added.

Comment thread examples/python_api_usage.py Outdated
@@ -0,0 +1,159 @@
# -------------------------------------------------------------------------

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file

@xiaoyu-work

Copy link
Copy Markdown
Collaborator

Fix lint issue.

@xiaoyu-work

Copy link
Copy Markdown
Collaborator

The Python API should be in one single documentation page with different sections.

@devang-ml devang-ml marked this pull request as ready for review June 17, 2025 17:22
Comment thread docs/source/how-to/python_api.md Fixed
Comment thread docs/source/how-to/python_api.md Fixed
Comment thread docs/source/how-to/python_api.md Fixed
Comment thread docs/source/how-to/python_api.md Fixed
Comment thread docs/source/how-to/python_api.md Fixed
jambayk
jambayk previously approved these changes Jun 18, 2025
@xiaoyu-work

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@xiaoyu-work

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

Comment thread olive/api/__init__.py Outdated
Comment thread olive/cli/__init__.py Outdated
@xiaoyu-work

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@jambayk jambayk merged commit b3895cf into main Jun 19, 2025
21 checks passed
@jambayk jambayk deleted the copilot/fix-1906 branch June 19, 2025 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FR]: Create Python API for CLI command

5 participants