Run InternVLA synthetic policy with Radeon Cosmos handoff
This commit is contained in:
1 parent
b796c787ec
commit
9df5ac8836
21 files changed
+2020
-16
No files matched your search
@@ -0,0 +1,93 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Export the unchanged InternVLA Cosmos encoder as a fixed-shape graph."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
import types
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def sha256(path: Path) -> str:
|
||||
digest = hashlib.sha256()
|
||||
with path.open("rb") as source:
|
||||
for block in iter(lambda: source.read(8 << 20), b""):
|
||||
digest.update(block)
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--encoder", type=Path, required=True)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--report", type=Path, required=True)
|
||||
parser.add_argument("--batch-size", type=int, default=6)
|
||||
args = parser.parse_args()
|
||||
|
||||
import torch
|
||||
|
||||
source_dir = Path(__file__).resolve().parents[3] / "vllm_omni" / "diffusion" / "models" / "internvla_a1"
|
||||
package = types.ModuleType("internvla_cosmos_export")
|
||||
package.__path__ = [str(source_dir)]
|
||||
sys.modules[package.__name__] = package
|
||||
spec = importlib.util.spec_from_file_location(f"{package.__name__}.model_cosmos", source_dir / "model_cosmos.py")
|
||||
if spec is None or spec.loader is None:
|
||||
raise RuntimeError("cannot load pinned Cosmos source")
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
sys.modules[spec.name] = module
|
||||
spec.loader.exec_module(module)
|
||||
encoder_path = args.encoder.resolve(strict=True)
|
||||
encoder_model = module.load_cosmos_component(encoder_path, component="encoder", device="cpu")
|
||||
|
||||
class LatentOnly(torch.nn.Module):
|
||||
def __init__(self, inner: torch.nn.Module) -> None:
|
||||
super().__init__()
|
||||
self.inner = inner
|
||||
|
||||
def forward(self, pixels: torch.Tensor) -> torch.Tensor:
|
||||
result = self.inner(pixels)
|
||||
return result if isinstance(result, torch.Tensor) else result[0]
|
||||
|
||||
model = LatentOnly(encoder_model).eval()
|
||||
image = torch.linspace(-1, 1, args.batch_size * 3 * 256 * 256, dtype=torch.float32).view(
|
||||
args.batch_size, 3, 256, 256
|
||||
)
|
||||
started = time.perf_counter()
|
||||
with torch.no_grad():
|
||||
reference = model(image)
|
||||
exported = torch.export.export(model, (image,), strict=False)
|
||||
actual = exported.module()(image)
|
||||
export_s = time.perf_counter() - started
|
||||
reference_tensor = reference
|
||||
actual_tensor = actual
|
||||
if not torch.allclose(reference_tensor, actual_tensor, atol=1e-5, rtol=1e-5):
|
||||
raise RuntimeError("exported CPU graph does not match source module")
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
torch.export.save(exported, args.output)
|
||||
report = {
|
||||
"status": "passed",
|
||||
"scope": "fixed-shape Cosmos CI8x8 encoder graph; no InternVLA action-policy claim",
|
||||
"encoder_sha256": sha256(encoder_path),
|
||||
"source_sha256": {name: sha256(source_dir / name) for name in ("model_cosmos.py", "cosmos_ci_torch.py")},
|
||||
"torch": torch.__version__,
|
||||
"input_shape": list(image.shape),
|
||||
"input_dtype": str(image.dtype),
|
||||
"output_shape": list(reference_tensor.shape),
|
||||
"max_abs_export_error": float((reference_tensor - actual_tensor).abs().max()),
|
||||
"export_s": export_s,
|
||||
"artifact_sha256": sha256(args.output),
|
||||
"artifact_bytes": args.output.stat().st_size,
|
||||
"artifact_path": str(args.output.resolve()),
|
||||
}
|
||||
args.report.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.report.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
|
||||
print(json.dumps(report, indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -54,11 +54,13 @@ def main() -> None:
|
||||
parser.add_argument("--encoder", type=Path, required=True)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--dml-index", type=int, default=1)
|
||||
parser.add_argument("--batch-size", type=int, default=1)
|
||||
parser.add_argument("--model-dtype", choices=("float32", "bfloat16"), default="float32")
|
||||
parser.add_argument("--warmups", type=int, default=1)
|
||||
parser.add_argument("--repeats", type=int, default=20)
|
||||
args = parser.parse_args()
|
||||
if args.warmups < 0 or args.repeats < 1:
|
||||
parser.error("invalid warmup or measured count")
|
||||
if args.warmups < 0 or args.repeats < 1 or args.batch_size < 1:
|
||||
parser.error("invalid batch size, warmup or measured count")
|
||||
|
||||
import numpy as np
|
||||
import psutil
|
||||
@@ -83,8 +85,8 @@ def main() -> None:
|
||||
"device_name": device_name,
|
||||
"torch": torch.__version__,
|
||||
"torch_directml": __import__("importlib.metadata", fromlist=["version"]).version("torch-directml"),
|
||||
"input_shape": [1, 3, 256, 256],
|
||||
"input_dtype": "float32",
|
||||
"input_shape": [args.batch_size, 3, 256, 256],
|
||||
"input_dtype": args.model_dtype,
|
||||
"warmups": args.warmups,
|
||||
"repeats": args.repeats,
|
||||
}
|
||||
@@ -94,9 +96,12 @@ def main() -> None:
|
||||
image[:, 0, 48:208, 48:208] = 1
|
||||
image[:, 1, 48:208, 48:208] = -1
|
||||
image[:, 2, :, :] = torch.linspace(-1, 1, 256)[None, None, :]
|
||||
image = image.repeat(args.batch_size, 1, 1, 1)
|
||||
model_dtype = getattr(torch, args.model_dtype)
|
||||
image = image.to(model_dtype)
|
||||
|
||||
started = time.perf_counter()
|
||||
cpu_model = source.load_cosmos_component(encoder, component="encoder", device="cpu")
|
||||
cpu_model = source.load_cosmos_component(encoder, component="encoder", device="cpu").to(model_dtype)
|
||||
report["cpu_load_s"] = time.perf_counter() - started
|
||||
with safe_open(str(encoder), framework="pt", device="cpu") as checkpoint:
|
||||
checkpoint_keys = set(checkpoint.keys())
|
||||
@@ -125,7 +130,7 @@ def main() -> None:
|
||||
set(report["checkpoint_unexpected_keys"]) != set(checkpoint_buffers)):
|
||||
raise RuntimeError("Cosmos encoder checkpoint/state keys do not match")
|
||||
started = time.perf_counter()
|
||||
dml_model = source.load_cosmos_component(encoder, component="encoder", device="cpu").to(dml)
|
||||
dml_model = source.load_cosmos_component(encoder, component="encoder", device="cpu").to(dtype=model_dtype, device=dml)
|
||||
report["dml_load_and_transfer_s"] = time.perf_counter() - started
|
||||
if {parameter.device.type for parameter in cpu_model.parameters()} != {"cpu"}:
|
||||
raise RuntimeError("CPU reference encoder placement differs")
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare complete InternVLA actions on CPU with a Radeon Cosmos handoff.
|
||||
|
||||
The policy and action sampler remain on CPU. Only its Cosmos encoder is
|
||||
replaced by Omni's existing external torch-DirectML graph worker. The test
|
||||
uses synthetic observations and does not establish robot-task quality.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import time
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def sha256(path: Path) -> str:
|
||||
digest = hashlib.sha256()
|
||||
with path.open("rb") as source:
|
||||
for block in iter(lambda: source.read(8 << 20), b""):
|
||||
digest.update(block)
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def rank(values: list[float], fraction: float) -> float:
|
||||
return sorted(values)[math.ceil(len(values) * fraction) - 1]
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--model-dir", type=Path, required=True)
|
||||
parser.add_argument("--cosmos-dir", type=Path, required=True)
|
||||
parser.add_argument("--processor-dir", type=Path, required=True)
|
||||
parser.add_argument("--graph", type=Path, required=True)
|
||||
parser.add_argument("--export-report", type=Path, required=True)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--warmups", type=int, default=1)
|
||||
parser.add_argument("--repeats", type=int, default=20)
|
||||
args = parser.parse_args()
|
||||
if args.warmups < 0 or args.repeats < 1:
|
||||
parser.error("warmups must be nonnegative and repeats must be positive")
|
||||
|
||||
os.environ["INTERNVLA_A1_COSMOS_DIR"] = str(args.cosmos_dir.resolve(strict=True))
|
||||
os.environ["INTERNVLA_A1_PROCESSOR_DIR"] = str(args.processor_dir.resolve(strict=True))
|
||||
os.environ["HF_HUB_OFFLINE"] = "1"
|
||||
|
||||
import numpy as np
|
||||
import psutil
|
||||
import torch
|
||||
|
||||
from vllm_omni.diffusion.data import OmniDiffusionConfig
|
||||
from vllm_omni.diffusion.registry import initialize_model
|
||||
from vllm_omni.diffusion.request import OmniDiffusionRequest
|
||||
from vllm_omni.diffusion.worker.request_batch import DiffusionRequestBatch
|
||||
from vllm_omni.edge.local.external.client import ExternalWorker
|
||||
from vllm_omni.edge.local.external.launch import ROUTE_TORCH_DML, resolve
|
||||
from vllm_omni.inputs.data import OmniDiffusionSamplingParams
|
||||
|
||||
graph = args.graph.resolve(strict=True)
|
||||
export_report = json.loads(args.export_report.read_text(encoding="utf-8"))
|
||||
if export_report["artifact_sha256"] != sha256(graph):
|
||||
raise RuntimeError("exported graph hash differs from the pinned manifest")
|
||||
if export_report["input_shape"] != [6, 3, 256, 256] or export_report["input_dtype"] != "torch.float32":
|
||||
raise RuntimeError("wrong Cosmos export shape or precision")
|
||||
model_dir = args.model_dir.resolve(strict=True)
|
||||
checkpoint = model_dir / "model.safetensors"
|
||||
if not checkpoint.is_file():
|
||||
raise FileNotFoundError(checkpoint)
|
||||
report = {
|
||||
"scope": "InternVLA real Place_Markpen checkpoint, synthetic zero camera/state observations and zero diffusion noise; CPU BF16 policy with FP32 Radeon Cosmos encoder; no robot-task quality claim",
|
||||
"model_checkpoint_sha256": sha256(checkpoint),
|
||||
"graph_sha256": export_report["artifact_sha256"],
|
||||
"graph_export": export_report,
|
||||
"policy_torch": torch.__version__,
|
||||
"warmups": args.warmups,
|
||||
"repeats": args.repeats,
|
||||
"status": "running",
|
||||
}
|
||||
worker = None
|
||||
try:
|
||||
config = OmniDiffusionConfig(
|
||||
model=str(model_dir),
|
||||
model_class_name="InternVLAA1Pipeline",
|
||||
dtype=torch.bfloat16,
|
||||
custom_pipeline_args={
|
||||
"device": "cpu", "dtype": "bfloat16", "compile_model": False,
|
||||
"enable_regional_compile": False, "enable_warmup": False,
|
||||
"strict_load": True, "processor_model_name": str(args.processor_dir.resolve()),
|
||||
},
|
||||
)
|
||||
started = time.perf_counter()
|
||||
pipeline = initialize_model(config)
|
||||
report["policy_load_s"] = time.perf_counter() - started
|
||||
if pipeline.runtime_mode() != "real_checkpoint_loaded":
|
||||
raise RuntimeError(f"unexpected policy mode: {pipeline.runtime_mode()}")
|
||||
if {p.device.type for p in pipeline.policy.parameters()} != {"cpu"}:
|
||||
raise RuntimeError("policy did not load wholly on CPU")
|
||||
report["policy_parameters_device"] = "cpu"
|
||||
inputs = pipeline._build_fake_batch_inputs()
|
||||
noise = torch.zeros((1, pipeline.config.chunk_size, pipeline.config.max_action_dim), dtype=torch.float32)
|
||||
|
||||
route = resolve(ROUTE_TORCH_DML)
|
||||
if not route.available:
|
||||
raise RuntimeError(f"DirectML route unavailable: {route.reason}")
|
||||
worker = ExternalWorker(route)
|
||||
started = time.perf_counter()
|
||||
hello = worker.start()
|
||||
load_report = worker.load(graph, example_inputs={"pixels": np.zeros((6, 3, 256, 256), dtype=np.float32)})
|
||||
report["worker_start_and_graph_load_s"] = time.perf_counter() - started
|
||||
report["worker_hello"] = {key: hello.get(key) for key in ("torch", "torch_directml", "available_providers", "executable")}
|
||||
report["worker_load"] = load_report.to_dict()
|
||||
if (load_report.device_name != "AMD Radeon(TM) 890M Graphics" or
|
||||
load_report.fraction_on_target != 1.0 or
|
||||
load_report.outputs[0]["shape"] != [6, 16, 32, 32]):
|
||||
raise RuntimeError("Radeon placement or output shape not verified")
|
||||
|
||||
class ExternalCosmosEncoder(torch.nn.Module):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.calls = 0
|
||||
self.timings = []
|
||||
|
||||
def forward(self, pixels: torch.Tensor) -> torch.Tensor:
|
||||
if pixels.device.type != "cpu" or tuple(pixels.shape) != (6, 3, 256, 256):
|
||||
raise RuntimeError(f"unexpected encoder input: {pixels.device}, {tuple(pixels.shape)}")
|
||||
# DirectML BF16 aborts this runtime; the FP32 graph is an
|
||||
# explicit artifact change, then latents return as policy BF16.
|
||||
array = pixels.detach().float().contiguous().numpy()
|
||||
output, timing = worker.run({"pixels": array})
|
||||
self.calls += 1
|
||||
self.timings.append(timing.to_dict())
|
||||
latent = output["out_0"]
|
||||
if latent.shape != (6, 16, 32, 32) or not np.isfinite(latent).all():
|
||||
raise RuntimeError("bad DirectML Cosmos latent")
|
||||
return torch.from_numpy(latent).to(pixels.dtype)
|
||||
|
||||
external_encoder = ExternalCosmosEncoder()
|
||||
cosmos = pipeline.policy.model.cosmos
|
||||
cpu_encoder = cosmos._enc_model
|
||||
|
||||
def actions(encoder: torch.nn.Module) -> np.ndarray:
|
||||
cosmos._enc_model = encoder
|
||||
result = pipeline.forward(DiffusionRequestBatch(requests=[
|
||||
OmniDiffusionRequest(
|
||||
prompt="",
|
||||
sampling_params=OmniDiffusionSamplingParams(extra_args={
|
||||
"batch_inputs": inputs, "noise": noise, "decode_image": False,
|
||||
}),
|
||||
request_id="internvla-hybrid-synthetic-observation",
|
||||
)
|
||||
]))
|
||||
if result.error:
|
||||
raise RuntimeError(result.error)
|
||||
tensor = result.output["payload"]["actions"]
|
||||
if tensor.device.type != "cpu" or tuple(tensor.shape) != (1, 50, 32):
|
||||
raise RuntimeError(f"bad policy action shape/device: {tensor.shape} {tensor.device}")
|
||||
if not torch.isfinite(tensor).all():
|
||||
raise RuntimeError("policy actions nonfinite")
|
||||
return tensor.detach().float().cpu().contiguous().numpy()
|
||||
|
||||
cpu_wall = []
|
||||
hybrid_wall = []
|
||||
errors = []
|
||||
reference = actual = None
|
||||
for index in range(args.warmups + args.repeats):
|
||||
started = time.perf_counter()
|
||||
reference = actions(cpu_encoder)
|
||||
cpu_s = time.perf_counter() - started
|
||||
started = time.perf_counter()
|
||||
actual = actions(external_encoder)
|
||||
hybrid_s = time.perf_counter() - started
|
||||
if index >= args.warmups:
|
||||
cpu_wall.append(cpu_s)
|
||||
hybrid_wall.append(hybrid_s)
|
||||
errors.append({
|
||||
"max_abs": float(np.max(np.abs(reference - actual))),
|
||||
"relative_l2": float(np.linalg.norm(reference - actual) / np.linalg.norm(reference)),
|
||||
"cosine": float(np.dot(reference.ravel(), actual.ravel()) / (
|
||||
np.linalg.norm(reference) * np.linalg.norm(actual)
|
||||
)),
|
||||
})
|
||||
assert reference is not None and actual is not None
|
||||
cosmos._enc_model = cpu_encoder
|
||||
report.update({
|
||||
"status": "passed",
|
||||
"action_shape": list(actual.shape),
|
||||
"cpu_action_sha256_last": hashlib.sha256(reference.tobytes()).hexdigest(),
|
||||
"hybrid_action_sha256_last": hashlib.sha256(actual.tobytes()).hexdigest(),
|
||||
"cpu_wall_s": cpu_wall,
|
||||
"hybrid_wall_s": hybrid_wall,
|
||||
"per_request_action_error": errors,
|
||||
"max_action_abs_error": max(e["max_abs"] for e in errors),
|
||||
"max_action_relative_l2_error": max(e["relative_l2"] for e in errors),
|
||||
"min_action_cosine": min(e["cosine"] for e in errors),
|
||||
"cpu_p50_wall_s": rank(cpu_wall, .5),
|
||||
"cpu_p95_wall_s": rank(cpu_wall, .95),
|
||||
"hybrid_p50_wall_s": rank(hybrid_wall, .5),
|
||||
"hybrid_p95_wall_s": rank(hybrid_wall, .95),
|
||||
"external_encoder_calls": external_encoder.calls,
|
||||
"external_encoder_timings": external_encoder.timings,
|
||||
"worker_stats": worker.stats(),
|
||||
"parent_rss_bytes_after": psutil.Process().memory_info().rss,
|
||||
"wsl_available_bytes_after": psutil.virtual_memory().available,
|
||||
})
|
||||
except BaseException as exc:
|
||||
report["status"] = "failed"
|
||||
report["error"] = f"{type(exc).__name__}: {exc}"
|
||||
report["traceback"] = traceback.format_exc()
|
||||
raise
|
||||
finally:
|
||||
if worker is not None:
|
||||
worker.close()
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.output.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
|
||||
print(json.dumps({key: value for key, value in report.items() if key not in (
|
||||
"cpu_wall_s", "hybrid_wall_s", "per_request_action_error", "external_encoder_timings"
|
||||
)}, indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user