-
Notifications
You must be signed in to change notification settings - Fork 290
Add model package support #2369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xiaoyu-work
wants to merge
13
commits into
main
Choose a base branch
from
xiaoyu/context_binary
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
397923c
Add model package pass and cli
xiaoyu-work 56f1b2e
Update doc
xiaoyu-work e79af68
update docstring
xiaoyu-work 5d8b0d2
Update model target check logic
xiaoyu-work 2bf2b9a
rename cli
xiaoyu-work 8f07785
update logic
xiaoyu-work dcdf280
rm empty doc
xiaoyu-work 50df651
Add base model to model_variants
xiaoyu-work 7e0cdc6
update metadata json
xiaoyu-work e94ee94
Merge branch 'main' into xiaoyu/context_binary
xiaoyu-work 7c22194
update cli
xiaoyu-work c701467
simplify task extract
xiaoyu-work 6984781
update tests
xiaoyu-work File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
| # -------------------------------------------------------------------------- | ||
| import json | ||
| import logging | ||
| from argparse import ArgumentParser | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| from olive.cli.base import ( | ||
| BaseOliveCLICommand, | ||
| add_logging_options, | ||
| add_save_config_file_options, | ||
| add_telemetry_options, | ||
| ) | ||
| from olive.telemetry import action | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class ModelPackageCommand(BaseOliveCLICommand): | ||
| """Merge multiple model outputs into a model package via the ModelPackage pass.""" | ||
|
|
||
| @staticmethod | ||
| def register_subcommand(parser: ArgumentParser): | ||
| sub_parser = parser.add_parser( | ||
| "generate-model-package", | ||
| help="Merge multiple model outputs into a model package with manifest", | ||
| ) | ||
|
|
||
| sub_parser.add_argument( | ||
| "-s", | ||
| "--source", | ||
| type=str, | ||
| action="append", | ||
| required=True, | ||
| help="Source Olive output directory. Can be specified multiple times.", | ||
| ) | ||
|
|
||
| sub_parser.add_argument( | ||
| "-o", | ||
| "--output_path", | ||
| type=str, | ||
| required=True, | ||
| help="Output directory for the merged model package.", | ||
| ) | ||
|
|
||
| sub_parser.add_argument( | ||
| "--model_name", | ||
| type=str, | ||
| default=None, | ||
| help="Model name for the manifest. If not set, derived from the output directory name.", | ||
| ) | ||
|
|
||
| sub_parser.add_argument( | ||
| "--model_version", | ||
| type=str, | ||
| default="1.0", | ||
| help="Model version string for the manifest. Default: 1.0", | ||
| ) | ||
|
|
||
| add_logging_options(sub_parser) | ||
| add_save_config_file_options(sub_parser) | ||
| add_telemetry_options(sub_parser) | ||
| sub_parser.set_defaults(func=ModelPackageCommand) | ||
|
|
||
| def _get_run_config(self, tempdir: str) -> dict[str, Any]: | ||
| sources = self._parse_sources() | ||
|
|
||
| target_models = [] | ||
| target_names = [] | ||
| for target_name, source_path in sources: | ||
| model_config = self._read_model_config(source_path) | ||
| target_models.append(model_config) | ||
| target_names.append(target_name) | ||
|
|
||
| ep, device = self._extract_accelerator_info(target_models) | ||
|
|
||
| return { | ||
| "input_model": { | ||
| "type": "ModelPackageModel", | ||
| "target_models": target_models, | ||
| "target_names": target_names, | ||
| "model_path": tempdir, | ||
| }, | ||
| "systems": { | ||
| "local_system": { | ||
| "type": "LocalSystem", | ||
| "accelerators": [{"device": device, "execution_providers": [ep]}], | ||
| } | ||
| }, | ||
| "passes": { | ||
| "pkg": { | ||
| "type": "ModelPackage", | ||
| "model_name": self.args.model_name, | ||
| "model_version": self.args.model_version, | ||
| } | ||
| }, | ||
| "output_dir": self.args.output_path, | ||
| "host": "local_system", | ||
| "target": "local_system", | ||
| "log_severity_level": self.args.log_level, | ||
| "no_artifacts": True, | ||
| } | ||
|
|
||
| @action | ||
| def run(self): | ||
| return self._run_workflow() | ||
|
|
||
| def _parse_sources(self) -> list[tuple[str, Path]]: | ||
| sources = [] | ||
| for source in self.args.source: | ||
| path = Path(source) | ||
| if not path.is_dir(): | ||
| raise ValueError(f"Source path does not exist or is not a directory: {path}") | ||
|
|
||
| if not (path / "model_config.json").exists(): | ||
| raise ValueError( | ||
| f"No model_config.json found in {path}. " | ||
| "Source must be an Olive output directory with model_config.json." | ||
| ) | ||
|
|
||
| sources.append((path.name, path)) | ||
|
|
||
| if len(sources) < 2: | ||
| raise ValueError("At least two --source directories are required to merge.") | ||
|
|
||
| return sources | ||
|
|
||
| @staticmethod | ||
| def _read_model_config(source_path: Path) -> dict: | ||
| config_path = source_path / "model_config.json" | ||
| with open(config_path) as f: | ||
| return json.load(f) | ||
|
|
||
| @staticmethod | ||
| def _extract_accelerator_info(target_models: list[dict]) -> tuple[str, str]: | ||
| for model_config in target_models: | ||
| attrs = model_config.get("config", {}).get("model_attributes") or {} | ||
| ep = attrs.get("ep", "CPUExecutionProvider") | ||
| device = attrs.get("device", "cpu") | ||
| return ep, device.lower() | ||
| return "CPUExecutionProvider", "cpu" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.