inital ai hub implementation
This commit is contained in:
@@ -21,6 +21,24 @@ def upload_file(
|
||||
return f"s3://{bucket}/{s3_key}"
|
||||
|
||||
|
||||
def download_file(
|
||||
region: str,
|
||||
profile: str,
|
||||
s3_uri: str,
|
||||
local_path: str,
|
||||
) -> str:
|
||||
if not s3_uri.startswith("s3://"):
|
||||
raise ValueError(f"Expected S3 URI, got: {s3_uri}")
|
||||
bucket_key = s3_uri.removeprefix("s3://")
|
||||
bucket, _, key = bucket_key.partition("/")
|
||||
if not bucket or not key:
|
||||
raise ValueError(f"Expected S3 URI with bucket and key, got: {s3_uri}")
|
||||
dest = Path(local_path)
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
_client(region, profile).download_file(bucket, key, str(dest))
|
||||
return str(dest)
|
||||
|
||||
|
||||
def upload_dir(
|
||||
region: str,
|
||||
profile: str,
|
||||
|
||||
@@ -121,6 +121,16 @@ def get_training_job_status(session: Boto3SessionKwargs, job_name: str) -> Train
|
||||
)
|
||||
|
||||
|
||||
def get_model_artifacts(region: str, profile: str, job_name: str) -> str:
|
||||
resp = boto3.Session(profile_name=profile, region_name=region).client("sagemaker").describe_training_job(
|
||||
TrainingJobName=job_name
|
||||
)
|
||||
artifact = resp.get("ModelArtifacts", {}).get("S3ModelArtifacts")
|
||||
if not artifact:
|
||||
raise RuntimeError(f"Training job '{job_name}' does not have model artifacts yet.")
|
||||
return str(artifact)
|
||||
|
||||
|
||||
def list_training_jobs(
|
||||
session: Boto3SessionKwargs,
|
||||
max_results: int = 10,
|
||||
|
||||
374
src/commands/ai_hub.py
Normal file
374
src/commands/ai_hub.py
Normal file
@@ -0,0 +1,374 @@
|
||||
from collections.abc import Mapping, Sequence
|
||||
from datetime import datetime
|
||||
from enum import StrEnum
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import typer
|
||||
|
||||
from src import state as state_ops
|
||||
from src.commands.utils import CONFIG_OPT, CONSOLE, load_cfg
|
||||
from src.config import Config
|
||||
from src.qualcomm import aihub_jobs
|
||||
from src.qualcomm.artifacts import resolve_onnx
|
||||
|
||||
app = typer.Typer(help="Quantize, compile, validate, profile, and download models with Qualcomm AI Hub")
|
||||
|
||||
_RUNTIME_EXTENSIONS = {
|
||||
"tflite": "tflite",
|
||||
"qnn_context_binary": "bin",
|
||||
"onnx": "onnx",
|
||||
}
|
||||
|
||||
|
||||
class UploadStep(StrEnum):
|
||||
quantize = "quantize"
|
||||
compile = "compile"
|
||||
validate = "validate"
|
||||
profile = "profile"
|
||||
|
||||
|
||||
def _input_specs(cfg: Config) -> dict[str, tuple[tuple[int, ...], str]]:
|
||||
specs = {name: (tuple(shape), dtype) for name, (shape, dtype) in cfg.aihub.input_specs.items()}
|
||||
if not specs:
|
||||
CONSOLE.print("[red]aihub.input_specs must define at least one input.[/red]")
|
||||
raise typer.Exit(1)
|
||||
return specs
|
||||
|
||||
|
||||
def _load_inputs(
|
||||
input_file: Path,
|
||||
specs: Mapping[str, tuple[Sequence[int], str]],
|
||||
input_name: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
import numpy as np
|
||||
|
||||
if not input_file.exists():
|
||||
raise FileNotFoundError(f"File not found: {input_file}")
|
||||
|
||||
if input_file.suffix == ".npz":
|
||||
loaded = np.load(input_file)
|
||||
missing = set(specs) - set(loaded.files)
|
||||
if missing:
|
||||
raise ValueError(f"Missing input(s) in NPZ: {', '.join(sorted(missing))}")
|
||||
return {name: loaded[name] for name in specs}
|
||||
|
||||
if input_file.suffix == ".npy":
|
||||
if input_name is None:
|
||||
if len(specs) != 1:
|
||||
raise ValueError("--input-name is required when config has multiple inputs")
|
||||
input_name = next(iter(specs))
|
||||
if input_name not in specs:
|
||||
raise ValueError(f"Input name '{input_name}' is not defined in aihub.input_specs")
|
||||
return {input_name: np.load(input_file)}
|
||||
|
||||
raise ValueError("Input file must be .npz or .npy")
|
||||
|
||||
|
||||
def _load_calibration(path: Path, specs: Mapping[str, tuple[Sequence[int], str]]) -> dict[str, Any]:
|
||||
import numpy as np
|
||||
|
||||
if path.is_file():
|
||||
return _load_inputs(path, specs)
|
||||
|
||||
if not path.is_dir():
|
||||
raise FileNotFoundError(f"Calibration path not found: {path}")
|
||||
|
||||
if len(specs) != 1:
|
||||
raise ValueError("Directory calibration data is supported only for single-input models.")
|
||||
input_name = next(iter(specs))
|
||||
samples = [np.load(p) for p in sorted(path.glob("*.npy"))]
|
||||
if not samples:
|
||||
raise ValueError(f"No .npy calibration samples found in {path}")
|
||||
return {input_name: samples}
|
||||
|
||||
|
||||
def _job_name(cfg: Config, operation: str) -> str | None:
|
||||
if not cfg.aihub.job_name:
|
||||
return None
|
||||
return f"{cfg.aihub.job_name}-{operation}"
|
||||
|
||||
|
||||
def _model_id_or_state(config_path: str, model_id: str | None, *, quantized: bool = False) -> str:
|
||||
st = state_ops.store(config_path)
|
||||
resolved = model_id or (st.get_last_quantized_model_id() if quantized else st.get_last_compiled_model_id())
|
||||
if not resolved:
|
||||
source = "quantized" if quantized else "compiled"
|
||||
CONSOLE.print(f"[red]No {source} model found. Pass --model-id or run the previous AI Hub step first.[/red]")
|
||||
raise typer.Exit(1)
|
||||
return resolved
|
||||
|
||||
|
||||
def _quantize_step(
|
||||
cfg: Config,
|
||||
config_path: str,
|
||||
calibration_path: Path,
|
||||
from_job: str | None,
|
||||
model_s3_uri: str | None,
|
||||
onnx_path: str | None,
|
||||
) -> str:
|
||||
st = state_ops.store(config_path)
|
||||
specs = _input_specs(cfg)
|
||||
try:
|
||||
resolved = resolve_onnx(
|
||||
cfg=cfg,
|
||||
output_dir=cfg.aihub.output_dir,
|
||||
from_job=from_job,
|
||||
model_s3_uri=model_s3_uri or st.get_last_model_artifact(),
|
||||
onnx_path=onnx_path,
|
||||
last_training_job=st.get_last_training_job(),
|
||||
)
|
||||
calibration_data = _load_calibration(calibration_path, specs)
|
||||
except (FileNotFoundError, ValueError) as e:
|
||||
CONSOLE.print(f"[red]{e}[/red]")
|
||||
raise typer.Exit(1)
|
||||
|
||||
try:
|
||||
result = aihub_jobs.submit_quantize_job(
|
||||
resolved.onnx_path,
|
||||
calibration_data,
|
||||
cfg.aihub.quantize_options,
|
||||
job_name=_job_name(cfg, "quantize"),
|
||||
model_name=cfg.aihub.model_name,
|
||||
)
|
||||
except Exception as e:
|
||||
CONSOLE.print(f"[red]AI Hub quantize failed: {e}[/red]")
|
||||
raise typer.Exit(1)
|
||||
|
||||
st.update(
|
||||
last_model_artifact=resolved.model_artifact,
|
||||
last_quantize_job_id=result["job_id"],
|
||||
last_quantized_model_id=result["model_id"],
|
||||
)
|
||||
CONSOLE.print(f"[green]✓[/green] Quantize job: [bold]{result['job_id']}[/bold]")
|
||||
CONSOLE.print(f"[green]✓[/green] Quantized model: [bold]{result['model_id']}[/bold]")
|
||||
return str(result["model_id"])
|
||||
|
||||
|
||||
def _compile_step(
|
||||
cfg: Config,
|
||||
config_path: str,
|
||||
model_id: str | None,
|
||||
from_job: str | None,
|
||||
model_s3_uri: str | None,
|
||||
onnx_path: str | None,
|
||||
*,
|
||||
prefer_quantized: bool,
|
||||
) -> str:
|
||||
st = state_ops.store(config_path)
|
||||
specs = _input_specs(cfg)
|
||||
|
||||
model: Any
|
||||
model_artifact: str | None = None
|
||||
has_explicit_source = bool(from_job or model_s3_uri or onnx_path)
|
||||
if model_id:
|
||||
model = model_id
|
||||
elif prefer_quantized and not has_explicit_source and st.get_last_quantized_model_id():
|
||||
model = st.get_last_quantized_model_id()
|
||||
else:
|
||||
try:
|
||||
resolved = resolve_onnx(
|
||||
cfg=cfg,
|
||||
output_dir=cfg.aihub.output_dir,
|
||||
from_job=from_job,
|
||||
model_s3_uri=model_s3_uri,
|
||||
onnx_path=onnx_path,
|
||||
last_training_job=st.get_last_training_job(),
|
||||
)
|
||||
except (FileNotFoundError, ValueError) as e:
|
||||
CONSOLE.print(f"[red]{e}[/red]")
|
||||
raise typer.Exit(1)
|
||||
model = resolved.onnx_path
|
||||
model_artifact = resolved.model_artifact
|
||||
|
||||
try:
|
||||
result = aihub_jobs.submit_compile_job(
|
||||
model=model,
|
||||
device_name=cfg.aihub.device,
|
||||
input_specs=specs,
|
||||
target_runtime=cfg.aihub.target_runtime,
|
||||
options=cfg.aihub.compile_options,
|
||||
job_name=_job_name(cfg, "compile"),
|
||||
model_name=cfg.aihub.model_name if isinstance(model, Path) else None,
|
||||
)
|
||||
except Exception as e:
|
||||
CONSOLE.print(f"[red]AI Hub compile failed: {e}[/red]")
|
||||
raise typer.Exit(1)
|
||||
|
||||
updates: dict[str, Any] = {
|
||||
"last_compile_job_id": result["job_id"],
|
||||
"last_compiled_model_id": result["model_id"],
|
||||
}
|
||||
if model_artifact:
|
||||
updates["last_model_artifact"] = model_artifact
|
||||
st.update(**updates)
|
||||
CONSOLE.print(f"[green]✓[/green] Compile job: [bold]{result['job_id']}[/bold]")
|
||||
CONSOLE.print(f"[green]✓[/green] Compiled model: [bold]{result['model_id']}[/bold]")
|
||||
return str(result["model_id"])
|
||||
|
||||
|
||||
def _validate_step(
|
||||
cfg: Config,
|
||||
config_path: str,
|
||||
input_file: Path,
|
||||
model_id: str | None,
|
||||
input_name: str | None,
|
||||
) -> str:
|
||||
specs = _input_specs(cfg)
|
||||
resolved_model_id = _model_id_or_state(config_path, model_id)
|
||||
try:
|
||||
inputs = _load_inputs(input_file, specs, input_name)
|
||||
except (FileNotFoundError, ValueError) as e:
|
||||
CONSOLE.print(f"[red]{e}[/red]")
|
||||
raise typer.Exit(1)
|
||||
|
||||
run = datetime.now().strftime("%Y%m%d-%H%M%S")
|
||||
out_dir = Path(cfg.aihub.output_dir) / run / "validation"
|
||||
try:
|
||||
result = aihub_jobs.submit_inference_job(
|
||||
resolved_model_id,
|
||||
cfg.aihub.device,
|
||||
inputs,
|
||||
out_dir,
|
||||
job_name=_job_name(cfg, "validate"),
|
||||
)
|
||||
except Exception as e:
|
||||
CONSOLE.print(f"[red]AI Hub inference failed: {e}[/red]")
|
||||
raise typer.Exit(1)
|
||||
|
||||
state_ops.store(config_path).update(last_inference_job_id=result["job_id"])
|
||||
CONSOLE.print(f"[green]✓[/green] Inference job: [bold]{result['job_id']}[/bold]")
|
||||
outputs = result.get("outputs")
|
||||
if isinstance(outputs, dict):
|
||||
for name, value in outputs.items():
|
||||
CONSOLE.print(f" {name}: shape={getattr(value, 'shape', '?')}")
|
||||
CONSOLE.print(f"Outputs: [cyan]{out_dir}[/cyan]")
|
||||
return str(result["job_id"])
|
||||
|
||||
|
||||
def _profile_step(cfg: Config, config_path: str, model_id: str | None) -> str:
|
||||
resolved_model_id = _model_id_or_state(config_path, model_id)
|
||||
try:
|
||||
result = aihub_jobs.submit_profile_job(
|
||||
resolved_model_id,
|
||||
cfg.aihub.device,
|
||||
cfg.aihub.profile_options,
|
||||
job_name=_job_name(cfg, "profile"),
|
||||
)
|
||||
except Exception as e:
|
||||
CONSOLE.print(f"[red]AI Hub profile failed: {e}[/red]")
|
||||
raise typer.Exit(1)
|
||||
state_ops.store(config_path).update(last_profile_job_id=result["job_id"])
|
||||
CONSOLE.print(f"[green]✓[/green] Profile job: [bold]{result['job_id']}[/bold]")
|
||||
return str(result["job_id"])
|
||||
|
||||
|
||||
@app.command()
|
||||
def quantize(
|
||||
calibration_path: Path = typer.Argument(..., help="Calibration .npz file or directory of .npy samples"),
|
||||
from_job: str | None = typer.Option(None, "--from-job", help="Training job name whose model artifact should quantize"),
|
||||
model_s3_uri: str | None = typer.Option(None, "--model-s3-uri", help="S3 URI of model.tar.gz to quantize"),
|
||||
onnx_path: str | None = typer.Option(
|
||||
None, "--onnx-path", help="Local ONNX path or ONNX path inside extracted artifact"
|
||||
),
|
||||
config: str = CONFIG_OPT,
|
||||
) -> None:
|
||||
"""Quantize an ONNX model to INT8."""
|
||||
cfg = load_cfg(config)
|
||||
_quantize_step(cfg, config, calibration_path, from_job, model_s3_uri, onnx_path)
|
||||
|
||||
|
||||
@app.command()
|
||||
def compile(
|
||||
model_id: str | None = typer.Option(None, "--model-id", help="AI Hub model ID to compile"),
|
||||
from_job: str | None = typer.Option(None, "--from-job", help="Training job name whose model artifact should compile"),
|
||||
model_s3_uri: str | None = typer.Option(None, "--model-s3-uri", help="S3 URI of model.tar.gz to compile"),
|
||||
onnx_path: str | None = typer.Option(
|
||||
None, "--onnx-path", help="Local ONNX path or ONNX path inside extracted artifact"
|
||||
),
|
||||
config: str = CONFIG_OPT,
|
||||
) -> None:
|
||||
"""Compile a model for the configured Qualcomm AI Hub target."""
|
||||
cfg = load_cfg(config)
|
||||
_compile_step(cfg, config, model_id, from_job, model_s3_uri, onnx_path, prefer_quantized=True)
|
||||
|
||||
|
||||
@app.command()
|
||||
def validate(
|
||||
input_file: Path = typer.Argument(..., help="NumPy .npz or .npy inputs to run on device"),
|
||||
model_id: str | None = typer.Option(None, "--model-id", help="AI Hub compiled model ID"),
|
||||
input_name: str | None = typer.Option(None, "--input-name", help="Input name for .npy files"),
|
||||
config: str = CONFIG_OPT,
|
||||
) -> None:
|
||||
"""Run an AI Hub inference job using sample inputs."""
|
||||
cfg = load_cfg(config)
|
||||
_validate_step(cfg, config, input_file, model_id, input_name)
|
||||
|
||||
|
||||
@app.command()
|
||||
def profile(
|
||||
model_id: str | None = typer.Option(None, "--model-id", help="AI Hub compiled model ID"),
|
||||
config: str = CONFIG_OPT,
|
||||
) -> None:
|
||||
"""Profile a compiled model on the configured AI Hub device."""
|
||||
cfg = load_cfg(config)
|
||||
_profile_step(cfg, config, model_id)
|
||||
|
||||
|
||||
@app.command()
|
||||
def upload(
|
||||
calibration_path: Path = typer.Argument(..., help="Calibration .npz file or directory of .npy samples"),
|
||||
input_file: Path = typer.Argument(..., help="Validation .npz or .npy inputs to run on device"),
|
||||
from_step: UploadStep = typer.Option(UploadStep.quantize, "--from-step", help="Resume from this Workbench step"),
|
||||
from_job: str | None = typer.Option(None, "--from-job", help="Training job name whose model artifact should upload"),
|
||||
model_s3_uri: str | None = typer.Option(None, "--model-s3-uri", help="S3 URI of model.tar.gz to upload"),
|
||||
onnx_path: str | None = typer.Option(
|
||||
None, "--onnx-path", help="Local ONNX path or ONNX path inside extracted artifact"
|
||||
),
|
||||
input_name: str | None = typer.Option(None, "--input-name", help="Input name for .npy validation files"),
|
||||
config: str = CONFIG_OPT,
|
||||
) -> None:
|
||||
"""Run the four Workbench upload steps: quantize, compile, validate, and profile."""
|
||||
cfg = load_cfg(config)
|
||||
steps = [UploadStep.quantize, UploadStep.compile, UploadStep.validate, UploadStep.profile]
|
||||
selected = steps[steps.index(from_step) :]
|
||||
|
||||
quantized_model_id: str | None = None
|
||||
compiled_model_id: str | None = None
|
||||
if UploadStep.quantize in selected:
|
||||
quantized_model_id = _quantize_step(cfg, config, calibration_path, from_job, model_s3_uri, onnx_path)
|
||||
if UploadStep.compile in selected:
|
||||
compiled_model_id = _compile_step(
|
||||
cfg,
|
||||
config,
|
||||
model_id=quantized_model_id,
|
||||
from_job=from_job,
|
||||
model_s3_uri=model_s3_uri,
|
||||
onnx_path=onnx_path,
|
||||
prefer_quantized=True,
|
||||
)
|
||||
if UploadStep.validate in selected:
|
||||
_validate_step(cfg, config, input_file, compiled_model_id, input_name)
|
||||
if UploadStep.profile in selected:
|
||||
_profile_step(cfg, config, compiled_model_id)
|
||||
|
||||
|
||||
@app.command()
|
||||
def download(
|
||||
model_id: str | None = typer.Option(None, "--model-id", help="AI Hub compiled model ID"),
|
||||
output: Path | None = typer.Option(None, "--output", "-o", help="Destination file path"),
|
||||
config: str = CONFIG_OPT,
|
||||
) -> None:
|
||||
"""Download the last compiled deployable artifact from AI Hub."""
|
||||
cfg = load_cfg(config)
|
||||
resolved_model_id = _model_id_or_state(config, model_id)
|
||||
ext = _RUNTIME_EXTENSIONS.get(cfg.aihub.target_runtime, cfg.aihub.target_runtime)
|
||||
dest = output or (Path(cfg.aihub.output_dir) / f"model.{ext}")
|
||||
|
||||
try:
|
||||
written = aihub_jobs.download_model(resolved_model_id, dest)
|
||||
except Exception as e:
|
||||
CONSOLE.print(f"[red]AI Hub download failed: {e}[/red]")
|
||||
raise typer.Exit(1)
|
||||
state_ops.store(config).update(last_downloaded_model=written)
|
||||
CONSOLE.print(f"[green]✓[/green] Downloaded model: [cyan]{written}[/cyan]")
|
||||
@@ -80,6 +80,18 @@ class SageMakerConfig(BaseModel):
|
||||
training: TrainingConfig = Field(default_factory=TrainingConfig)
|
||||
|
||||
|
||||
class AIHubConfig(BaseModel):
|
||||
device: str = "Samsung Galaxy S25 (Family)"
|
||||
target_runtime: str = "tflite"
|
||||
input_specs: dict[str, tuple[list[int], str]] = Field(default_factory=dict)
|
||||
job_name: str | None = None
|
||||
model_name: str | None = None
|
||||
compile_options: str | None = None
|
||||
profile_options: str | None = None
|
||||
quantize_options: str | None = None
|
||||
output_dir: str = "build/qai-hub"
|
||||
|
||||
|
||||
class MlflowConfig(BaseModel):
|
||||
mode: MlflowMode = MlflowMode.disabled
|
||||
tracking_server_name: str | None = None
|
||||
@@ -104,6 +116,7 @@ class Config(BaseModel):
|
||||
aws: AwsConfig = Field(default_factory=AwsConfig)
|
||||
s3: S3Config = Field(default_factory=S3Config)
|
||||
sagemaker: SageMakerConfig = Field(default_factory=SageMakerConfig)
|
||||
aihub: AIHubConfig = Field(default_factory=AIHubConfig)
|
||||
mlflow: MlflowConfig = Field(default_factory=MlflowConfig)
|
||||
|
||||
@property
|
||||
|
||||
@@ -7,7 +7,7 @@ from rich.console import Console
|
||||
from rich.progress import BarColumn, Progress, SpinnerColumn, TaskProgressColumn, TextColumn
|
||||
|
||||
from src.aws import s3 as s3_ops
|
||||
from src.commands import infra, train
|
||||
from src.commands import ai_hub, infra, train
|
||||
from src.commands.utils import CONFIG_OPT, load_cfg
|
||||
from src.config import GENERATED_STACK_PREFIX, Config, InfraConfig, S3Config
|
||||
|
||||
@@ -17,6 +17,7 @@ app = typer.Typer(
|
||||
)
|
||||
app.add_typer(infra.app, name="infra")
|
||||
app.add_typer(train.app, name="train")
|
||||
app.add_typer(ai_hub.app, name="ai-hub")
|
||||
|
||||
console = Console()
|
||||
|
||||
|
||||
1
src/qualcomm/__init__.py
Normal file
1
src/qualcomm/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
144
src/qualcomm/aihub_jobs.py
Normal file
144
src/qualcomm/aihub_jobs.py
Normal file
@@ -0,0 +1,144 @@
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _hub() -> Any:
|
||||
import qai_hub as hub
|
||||
|
||||
return hub
|
||||
|
||||
|
||||
def _id(obj: Any) -> str:
|
||||
for attr in ("model_id", "job_id", "id"):
|
||||
value = getattr(obj, attr, None)
|
||||
if value:
|
||||
return str(value)
|
||||
return str(obj)
|
||||
|
||||
|
||||
def _target_model(job: Any) -> Any:
|
||||
if hasattr(job, "get_target_model"):
|
||||
return job.get_target_model()
|
||||
model = getattr(job, "target_model", None)
|
||||
if model is not None:
|
||||
return model
|
||||
return job
|
||||
|
||||
|
||||
def get_model(model_id: str) -> Any:
|
||||
return _hub().get_model(model_id)
|
||||
|
||||
|
||||
def _dataset_entries(inputs: dict[str, Any]) -> dict[str, list[Any]]:
|
||||
return {name: value if isinstance(value, list) else [value] for name, value in inputs.items()}
|
||||
|
||||
|
||||
def submit_compile_job(
|
||||
model: Any,
|
||||
device_name: str,
|
||||
input_specs: dict[str, tuple[tuple[int, ...], str]],
|
||||
target_runtime: str,
|
||||
options: str | None = None,
|
||||
job_name: str | None = None,
|
||||
model_name: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
hub = _hub()
|
||||
compile_options = f"--target_runtime {target_runtime}"
|
||||
if options:
|
||||
compile_options = f"{compile_options} {options}"
|
||||
|
||||
model_arg = model
|
||||
if isinstance(model, Path):
|
||||
model_arg = str(model)
|
||||
elif isinstance(model, str):
|
||||
candidate = Path(model)
|
||||
model_arg = model if candidate.exists() or candidate.suffix else get_model(model)
|
||||
|
||||
if model_name and isinstance(model_arg, str) and Path(model_arg).exists():
|
||||
model_arg = hub.upload_model(model_arg, name=model_name)
|
||||
|
||||
job = hub.submit_compile_job(
|
||||
model=model_arg,
|
||||
device=hub.Device(device_name),
|
||||
name=job_name,
|
||||
input_specs=input_specs,
|
||||
options=compile_options,
|
||||
)
|
||||
target_model = _target_model(job)
|
||||
if target_model is None:
|
||||
raise RuntimeError(f"Compile job {_id(job)} did not produce a target model.")
|
||||
return {"job": job, "job_id": _id(job), "model": target_model, "model_id": _id(target_model)}
|
||||
|
||||
|
||||
def submit_inference_job(
|
||||
model_id: str,
|
||||
device_name: str,
|
||||
inputs: dict[str, Any],
|
||||
output_dir: str | Path,
|
||||
job_name: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
hub = _hub()
|
||||
job = hub.submit_inference_job(
|
||||
model=get_model(model_id),
|
||||
device=hub.Device(device_name),
|
||||
inputs=_dataset_entries(inputs),
|
||||
name=job_name,
|
||||
)
|
||||
out = Path(output_dir)
|
||||
out.mkdir(parents=True, exist_ok=True)
|
||||
data = job.download_output_data(str(out))
|
||||
return {"job": job, "job_id": _id(job), "outputs": data}
|
||||
|
||||
|
||||
def submit_profile_job(
|
||||
model_id: str,
|
||||
device_name: str,
|
||||
options: str | None = None,
|
||||
job_name: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
hub = _hub()
|
||||
job = hub.submit_profile_job(
|
||||
model=get_model(model_id),
|
||||
device=hub.Device(device_name),
|
||||
name=job_name,
|
||||
options=options or "",
|
||||
)
|
||||
return {"job": job, "job_id": _id(job)}
|
||||
|
||||
|
||||
def submit_quantize_job(
|
||||
model: str | Path,
|
||||
calibration_data: dict[str, Any],
|
||||
options: str | None = None,
|
||||
job_name: str | None = None,
|
||||
model_name: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
hub = _hub()
|
||||
model_arg = str(model)
|
||||
if model_name and Path(model_arg).exists():
|
||||
model_arg = hub.upload_model(model_arg, name=model_name)
|
||||
job = hub.submit_quantize_job(
|
||||
model=model_arg,
|
||||
calibration_data=_dataset_entries(calibration_data),
|
||||
weights_dtype=hub.QuantizeDtype.INT8,
|
||||
activations_dtype=hub.QuantizeDtype.INT8,
|
||||
name=job_name,
|
||||
options=options or "",
|
||||
)
|
||||
target_model = _target_model(job)
|
||||
if target_model is None:
|
||||
raise RuntimeError(f"Quantize job {_id(job)} did not produce a target model.")
|
||||
return {"job": job, "job_id": _id(job), "model": target_model, "model_id": _id(target_model)}
|
||||
|
||||
|
||||
def download_model(model_id: str, output_path: str | Path) -> str:
|
||||
dest = Path(output_path)
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
model = get_model(model_id)
|
||||
if hasattr(model, "download"):
|
||||
result = model.download(str(dest))
|
||||
return str(result or dest)
|
||||
if hasattr(model, "download_model"):
|
||||
result = model.download_model(str(dest))
|
||||
return str(result or dest)
|
||||
raise RuntimeError("AI Hub model object does not expose a download method.")
|
||||
83
src/qualcomm/artifacts.py
Normal file
83
src/qualcomm/artifacts.py
Normal file
@@ -0,0 +1,83 @@
|
||||
import tarfile
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from src.aws import s3 as s3_ops
|
||||
from src.aws import sagemaker as sm_ops
|
||||
from src.config import Config
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ResolvedOnnx:
|
||||
onnx_path: Path
|
||||
model_artifact: str | None
|
||||
run_name: str
|
||||
|
||||
|
||||
def _safe_extract(tar: tarfile.TarFile, dest: Path) -> None:
|
||||
dest_root = dest.resolve()
|
||||
for member in tar.getmembers():
|
||||
target = (dest / member.name).resolve()
|
||||
if dest_root != target and dest_root not in target.parents:
|
||||
raise ValueError(f"Unsafe tar member path: {member.name}")
|
||||
tar.extractall(dest, filter="data")
|
||||
|
||||
|
||||
def _find_onnx(root: Path, explicit: str | None = None) -> Path:
|
||||
if explicit:
|
||||
p = Path(explicit)
|
||||
if not p.is_absolute():
|
||||
p = root / p
|
||||
if not p.exists():
|
||||
raise FileNotFoundError(f"ONNX file not found: {p}")
|
||||
return p
|
||||
|
||||
matches = sorted(root.rglob("model.onnx"))
|
||||
if not matches:
|
||||
matches = sorted(root.rglob("*.onnx"))
|
||||
if not matches:
|
||||
raise FileNotFoundError(f"No ONNX file found under {root}")
|
||||
if len(matches) > 1:
|
||||
joined = ", ".join(str(p.relative_to(root)) for p in matches)
|
||||
raise ValueError(f"Multiple ONNX files found ({joined}). Pass --onnx-path.")
|
||||
return matches[0]
|
||||
|
||||
|
||||
def resolve_onnx(
|
||||
cfg: Config,
|
||||
output_dir: str,
|
||||
from_job: str | None = None,
|
||||
model_s3_uri: str | None = None,
|
||||
onnx_path: str | None = None,
|
||||
last_training_job: str | None = None,
|
||||
) -> ResolvedOnnx:
|
||||
if onnx_path:
|
||||
path = Path(onnx_path)
|
||||
if path.exists():
|
||||
return ResolvedOnnx(onnx_path=path, model_artifact=None, run_name=path.stem)
|
||||
|
||||
job = from_job or last_training_job
|
||||
artifact = model_s3_uri
|
||||
if not artifact:
|
||||
if not job:
|
||||
raise ValueError("No model source found. Pass --onnx-path, --model-s3-uri, --from-job, or run training first.")
|
||||
artifact = sm_ops.get_model_artifacts(cfg.aws.region, cfg.aws.profile, job)
|
||||
|
||||
run_name = job or Path(artifact).name.removesuffix(".tar.gz").replace("/", "-")
|
||||
root = Path(output_dir) / run_name / "source"
|
||||
tar_path = root / "model.tar.gz"
|
||||
s3_ops.download_file(cfg.aws.region, cfg.aws.profile, artifact, str(tar_path))
|
||||
|
||||
extract_dir = root / "extracted"
|
||||
extract_dir.mkdir(parents=True, exist_ok=True)
|
||||
try:
|
||||
with tarfile.open(tar_path, "r:gz") as tar:
|
||||
_safe_extract(tar, extract_dir)
|
||||
except tarfile.TarError as e:
|
||||
raise ValueError(f"Invalid model tarball: {tar_path}") from e
|
||||
|
||||
return ResolvedOnnx(
|
||||
onnx_path=_find_onnx(extract_dir, onnx_path),
|
||||
model_artifact=artifact,
|
||||
run_name=run_name,
|
||||
)
|
||||
16
src/state.py
16
src/state.py
@@ -33,6 +33,22 @@ class CliStateStore:
|
||||
value = self.get("last_training_job")
|
||||
return str(value) if value else None
|
||||
|
||||
def get_last_model_artifact(self) -> str | None:
|
||||
value = self.get("last_model_artifact")
|
||||
return str(value) if value else None
|
||||
|
||||
def get_last_quantized_model_id(self) -> str | None:
|
||||
value = self.get("last_quantized_model_id")
|
||||
return str(value) if value else None
|
||||
|
||||
def get_last_compiled_model_id(self) -> str | None:
|
||||
value = self.get("last_compiled_model_id")
|
||||
return str(value) if value else None
|
||||
|
||||
def get_last_downloaded_model(self) -> str | None:
|
||||
value = self.get("last_downloaded_model")
|
||||
return str(value) if value else None
|
||||
|
||||
def set_last_training_job(self, job_name: str) -> None:
|
||||
self.update(last_training_job=job_name)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user