forked from karpathy/nanoGPT
-
Notifications
You must be signed in to change notification settings - Fork 27
Create model export automation scripts #668
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
klei22
wants to merge
3
commits into
ReaLLMASIC:master
Choose a base branch
from
klei22:create-model-export-automation-scripts
base: master
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
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| if [[ ${1:-} == "" ]]; then | ||
| echo "Usage: $0 <ckpt-path> [pte-path]" | ||
| exit 1 | ||
| fi | ||
|
|
||
| CKPT_PATH=$1 | ||
| PTE_PATH=${2:-} | ||
|
|
||
| if [[ -n "$PTE_PATH" ]]; then | ||
| python -m model_exports.executorch.export_checkpoint --ckpt "$CKPT_PATH" --pte-path "$PTE_PATH" "${@:3}" | ||
| else | ||
| python -m model_exports.executorch.export_checkpoint --ckpt "$CKPT_PATH" "${@:2}" | ||
| fi |
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,7 @@ | ||
| # Hardware profiling targets | ||
|
|
||
| This directory contains automation helpers for running exported ExecuTorch programs on specific devices. | ||
|
|
||
| ## Android | ||
|
|
||
| Use `android/profile_pte.py` to stage a runner and `.pte` file onto an attached device via `adb`, invoke the runner, and parse energy/latency metrics emitted between `EXECUTORCH_METRICS_BEGIN` and `EXECUTORCH_METRICS_END` markers. |
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,139 @@ | ||
| """Utility for profiling ExecuTorch `.pte` programs on Android devices via `adb`.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import json | ||
| import re | ||
| import shlex | ||
| import subprocess | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| METRICS_BEGIN = "EXECUTORCH_METRICS_BEGIN" | ||
| METRICS_END = "EXECUTORCH_METRICS_END" | ||
|
|
||
|
|
||
| @dataclass(slots=True) | ||
| class MetricsSummary: | ||
| phase: str | ||
| tokens: int | ||
| latency_ms: float | ||
| energy_mj: float | ||
|
|
||
| @property | ||
| def latency_per_token_ms(self) -> float: | ||
| return self.latency_ms / max(self.tokens, 1) | ||
|
|
||
| @property | ||
| def energy_per_token_mj(self) -> float: | ||
| return self.energy_mj / max(self.tokens, 1) | ||
|
|
||
|
|
||
| def _adb_cmd(args: list[str], serial: Optional[str] = None, **kwargs: Any) -> subprocess.CompletedProcess[str]: | ||
| base = ["adb"] | ||
| if serial: | ||
| base += ["-s", serial] | ||
| result = subprocess.run(base + args, check=True, capture_output=True, text=True, **kwargs) | ||
| return result | ||
|
|
||
|
|
||
| def _extract_metrics(stdout: str) -> Dict[str, MetricsSummary]: | ||
| pattern = re.compile(rf"{METRICS_BEGIN}(.*?){METRICS_END}", re.DOTALL) | ||
| match = pattern.search(stdout) | ||
| if not match: | ||
| return {} | ||
| payload = match.group(1).strip() | ||
| data = json.loads(payload) | ||
| summaries: Dict[str, MetricsSummary] = {} | ||
| for phase, values in data.items(): | ||
| summaries[phase] = MetricsSummary( | ||
| phase=phase, | ||
| tokens=int(values.get("tokens", 0)), | ||
| latency_ms=float(values.get("latency_ms", 0.0)), | ||
| energy_mj=float(values.get("energy_mj", 0.0)), | ||
| ) | ||
| return summaries | ||
|
|
||
|
|
||
| def _format_summary(summary: MetricsSummary) -> str: | ||
| return ( | ||
| f"{summary.phase}: tokens={summary.tokens} " | ||
| f"latency={summary.latency_ms:.2f}ms (per token {summary.latency_per_token_ms:.2f}ms) " | ||
| f"energy={summary.energy_mj:.3f}mJ (per token {summary.energy_per_token_mj:.3f}mJ)" | ||
| ) | ||
|
|
||
|
|
||
| def profile(args: argparse.Namespace) -> None: | ||
| remote_dir = Path(args.remote_dir) | ||
| remote_dir_str = str(remote_dir) | ||
| remote_runner = remote_dir / Path(args.runner).name | ||
| remote_pte = remote_dir / Path(args.pte).name | ||
|
|
||
| print(f"[INFO] Pushing runner to {remote_runner}") | ||
| _adb_cmd(["push", args.runner, str(remote_runner)], serial=args.serial) | ||
| print(f"[INFO] Pushing PTE to {remote_pte}") | ||
| _adb_cmd(["push", args.pte, str(remote_pte)], serial=args.serial) | ||
|
|
||
| prompt = args.prompt or "Hello world!" | ||
| runner_invocation = ( | ||
| f"cd {shlex.quote(remote_dir_str)} && " | ||
| f"chmod +x {shlex.quote(remote_runner.name)} && " | ||
| f"echo {shlex.quote(prompt)} | " | ||
| f"{shlex.quote('./' + remote_runner.name)}" | ||
| ) | ||
|
|
||
| print(f"[INFO] Launching runner via adb shell: {runner_invocation}") | ||
| result = _adb_cmd(["shell", runner_invocation], serial=args.serial) | ||
| stdout = result.stdout | ||
| if stdout: | ||
| print("[DEVICE OUTPUT]") | ||
| print(stdout) | ||
|
|
||
| summaries = _extract_metrics(stdout) | ||
| if not summaries: | ||
| print( | ||
| "[WARN] No ExecuTorch metrics detected. Ensure the runner prints JSON between " | ||
| f"{METRICS_BEGIN} and {METRICS_END}." | ||
| ) | ||
| return | ||
|
|
||
| print("[INFO] Parsed metrics:") | ||
| for summary in summaries.values(): | ||
| print(" " + _format_summary(summary)) | ||
|
|
||
|
|
||
| def parse_args() -> argparse.Namespace: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("--runner", required=True, help="Path to the compiled ExecuTorch runner binary.") | ||
| parser.add_argument("--pte", required=True, help="Path to the exported ExecuTorch .pte program.") | ||
| parser.add_argument( | ||
| "--remote-dir", | ||
| default="/data/local/tmp/nanogpt", | ||
| help="Directory on the device where artifacts will be staged.", | ||
| ) | ||
| parser.add_argument( | ||
| "--prompt", | ||
| help="Prompt text to feed into the runner. Defaults to 'Hello world!'.", | ||
| ) | ||
| parser.add_argument( | ||
| "--serial", | ||
| help="Optional adb serial number when multiple devices are connected.", | ||
| ) | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| def main() -> None: | ||
| args = parse_args() | ||
| try: | ||
| profile(args) | ||
| except FileNotFoundError as exc: | ||
| print(f"[ERROR] Failed to invoke external tool: {exc}") | ||
| except subprocess.CalledProcessError as exc: | ||
| print("[ERROR] adb command failed:") | ||
| print(exc.stderr) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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,5 @@ | ||
| """ExecuTorch export utilities for nanoGPT checkpoints.""" | ||
|
|
||
| from .exporter import ExportConfig, export_checkpoint_to_pte | ||
|
|
||
| __all__ = ["ExportConfig", "export_checkpoint_to_pte"] |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue as in run_experiments.py:
default=Truewithaction='store_true'is redundant. Remove thedefault=Trueparameter.