59 lines
2.8 KiB
Python
59 lines
2.8 KiB
Python
"""Verify real CUDA kernels and SAM2 propagation without touching user annotations."""
|
|
import argparse
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
import time
|
|
|
|
import cv2
|
|
import numpy as np
|
|
import torch
|
|
from torchvision.ops import nms
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--runtime-root", type=Path, default=Path(__file__).resolve().parents[2])
|
|
runtime_root = parser.parse_args().runtime_root.resolve()
|
|
assert torch.cuda.is_available(), "CUDA is unavailable in this Python runtime"
|
|
boxes = torch.tensor([[0, 0, 20, 20], [1, 1, 19, 19]], dtype=torch.float32, device="cuda")
|
|
scores = torch.tensor([0.9, 0.8], device="cuda")
|
|
assert nms(boxes, scores, 0.5).tolist() == [0], "TorchVision CUDA NMS failed"
|
|
del boxes, scores
|
|
torch.cuda.empty_cache()
|
|
torch.cuda.reset_peak_memory_stats()
|
|
spec = importlib.util.spec_from_file_location("worker", runtime_root / "tools" / "worker.py")
|
|
worker = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(worker)
|
|
messages = []
|
|
worker.emit = lambda message, **kwargs: messages.append(message)
|
|
started = time.perf_counter()
|
|
with tempfile.TemporaryDirectory(prefix="sam2-gpu-check-") as temporary:
|
|
root = Path(temporary)
|
|
frames = root / "frames"
|
|
frames.mkdir()
|
|
for index in range(3):
|
|
image = np.full((192, 256, 3), 210, dtype=np.uint8)
|
|
cv2.rectangle(image, (70 + index * 2, 50), (150 + index * 2, 130), (40, 60, 200), -1)
|
|
assert cv2.imwrite(str(frames / f"{index:06d}.jpg"), image)
|
|
project = root / "project.json"
|
|
worker.save_json(project, {"name": "GPU测试零件", "width": 256, "height": 192,
|
|
"frames": [{"index": i, "image": f"frames/{i:06d}.jpg", "status": "reviewed" if i == 0 else "unlabeled",
|
|
"prompt": i == 0, "box": [70, 50, 151, 131] if i == 0 else None} for i in range(3)]})
|
|
worker.propagate({"project": str(project), "device": "auto"})
|
|
torch.cuda.synchronize()
|
|
result = worker.read_json(project)
|
|
assert result["frames"][0]["status"] == "reviewed"
|
|
assert all(f["status"] == "auto" for f in result["frames"][1:])
|
|
assert any("cuda" in message for message in messages), "SAM2 did not select CUDA"
|
|
assert torch.cuda.max_memory_allocated() > 50_000_000, "SAM2 did not allocate model memory on GPU"
|
|
print(json.dumps({"passed": True, "torch": torch.__version__, "cuda": torch.version.cuda,
|
|
"gpu": torch.cuda.get_device_name(0), "auto_frames": 2,
|
|
"peak_gpu_mb": round(torch.cuda.max_memory_allocated() / 1024 ** 2, 1),
|
|
"seconds": round(time.perf_counter() - started, 2)}, ensure_ascii=True))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|