IQ.Pilot Release Commit @ 461be14
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
"""
|
||||
Copyright © IQ.Lvbs, apart of Project Teal Lvbs, All Rights Reserved, licensed under https://konn3kt.com/tos/
|
||||
"""
|
||||
import hashlib
|
||||
import os
|
||||
|
||||
from iqpilot.selfdrive.iqmodeld import egpu_helpers as eh
|
||||
|
||||
|
||||
def test_fetch_fw_mirrors_and_serves_offline(tmp_path, monkeypatch):
|
||||
from tinygrad import helpers
|
||||
blob = os.urandom(4096)
|
||||
sha = hashlib.sha256(blob).hexdigest()
|
||||
calls = []
|
||||
|
||||
def orig(path, name, sha256):
|
||||
calls.append((path, name))
|
||||
return blob
|
||||
|
||||
monkeypatch.setattr(helpers, "fetch_fw", orig, raising=False)
|
||||
helpers.fetch_fw._iq_patched = False
|
||||
monkeypatch.setattr(eh, "FIRMWARE_MIRROR", str(tmp_path / "mirror"))
|
||||
eh.patch_tinygrad_fetch_fw()
|
||||
assert helpers.fetch_fw("amdgpu", "gc.bin", sha) == blob and calls == [("amdgpu", "gc.bin")]
|
||||
mirrored = tmp_path / "mirror" / "amdgpu" / "gc.bin"
|
||||
assert mirrored.read_bytes() == blob
|
||||
assert helpers.fetch_fw("amdgpu", "gc.bin", sha) == blob and len(calls) == 1
|
||||
mirrored.write_bytes(b"corrupt")
|
||||
assert helpers.fetch_fw("amdgpu", "gc.bin", sha) == blob and len(calls) == 2
|
||||
assert mirrored.read_bytes() == blob
|
||||
34
iqpilot/selfdrive/iqmodeld/tests/test_egpu_host_mock.py
Normal file
34
iqpilot/selfdrive/iqmodeld/tests/test_egpu_host_mock.py
Normal file
@@ -0,0 +1,34 @@
|
||||
"""
|
||||
Copyright © IQ.Lvbs, apart of Project Teal Lvbs, All Rights Reserved, licensed under https://konn3kt.com/tos/
|
||||
"""
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
from iqpilot.selfdrive.iqmodeld.tools.egpu_host_mock import tinygrad_tree
|
||||
|
||||
PROBE = """
|
||||
import os
|
||||
os.environ["JIT_BATCH_SIZE"] = "0"
|
||||
from iqpilot.selfdrive.iqmodeld.tools.egpu_host_mock import activate
|
||||
activate("gfx1200")
|
||||
from tinygrad import Tensor
|
||||
from tinygrad.device import Device
|
||||
from tinygrad.engine.jit import TinyJit
|
||||
dev = Device["AMD"]
|
||||
assert dev.arch == "gfx1200", dev.arch
|
||||
assert type(dev.iface).__name__ == "MOCKUSBIface", type(dev.iface).__name__
|
||||
run = TinyJit(lambda x: (x * 2 + 1).sum(axis=1).realize())
|
||||
for i in range(3):
|
||||
run(Tensor.ones(64, 64, device="AMD") * i)
|
||||
print("MOCK_OK")
|
||||
"""
|
||||
|
||||
|
||||
@pytest.mark.skipif(not os.path.isdir(os.path.join(tinygrad_tree(), "test", "mockgpu")), reason="tinygrad mockgpu tree not checked out")
|
||||
def test_mock_dock_captures_a_jit_without_hardware():
|
||||
out = subprocess.run([sys.executable, "-c", PROBE], capture_output=True, text=True, timeout=600)
|
||||
assert out.returncode == 0, out.stderr[-2000:]
|
||||
assert "MOCK_OK" in out.stdout
|
||||
62
iqpilot/selfdrive/iqmodeld/tests/test_egpu_oob.py
Normal file
62
iqpilot/selfdrive/iqmodeld/tests/test_egpu_oob.py
Normal file
@@ -0,0 +1,62 @@
|
||||
"""
|
||||
Copyright © IQ.Lvbs, apart of Project Teal Lvbs, All Rights Reserved, licensed under https://konn3kt.com/tos/
|
||||
"""
|
||||
import os
|
||||
import pickle
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
os.environ["DEV"] = "CPU"
|
||||
|
||||
from iqpilot.selfdrive.iqmodeld.egpu_policy import dump_oob, is_oob, load_bundle
|
||||
|
||||
|
||||
def _bundle():
|
||||
from tinygrad import Tensor
|
||||
w = Tensor(np.arange(4096, dtype=np.float32).reshape(64, 64), device="CPU").realize()
|
||||
return {"format": 2, "weights": w, "spec": {"a": ((1, 2), "float32")}, "blob": os.urandom(100_000)}
|
||||
|
||||
|
||||
def test_oob_round_trip_matches_plain_pickle(tmp_path):
|
||||
b = _bundle()
|
||||
oob = tmp_path / "b.oob"
|
||||
with open(oob, "wb") as f:
|
||||
dump_oob(b, f)
|
||||
assert is_oob(str(oob))
|
||||
got = load_bundle(str(oob))
|
||||
np.testing.assert_array_equal(got["weights"].numpy(), b["weights"].numpy())
|
||||
assert got["blob"] == b["blob"] and got["spec"] == b["spec"] and got["format"] == 2
|
||||
plain = tmp_path / "b.pkl"
|
||||
with open(plain, "wb") as f:
|
||||
pickle.dump({"x": 1, "blob": b["blob"]}, f, protocol=pickle.HIGHEST_PROTOCOL)
|
||||
assert not is_oob(str(plain))
|
||||
assert load_bundle(str(plain))["blob"] == b["blob"]
|
||||
|
||||
|
||||
def test_memory_guard_raises_when_starved(monkeypatch):
|
||||
from iqpilot.selfdrive.iqmodeld import iqegpumodeld as d
|
||||
monkeypatch.setattr(d, "_mem_available_mb", lambda: 90)
|
||||
monkeypatch.setattr(d, "MEMORY_WAIT_S", 0.0)
|
||||
with pytest.raises(RuntimeError, match="insufficient memory"):
|
||||
d._wait_for_memory(350)
|
||||
monkeypatch.setattr(d, "_mem_available_mb", lambda: 900)
|
||||
d._wait_for_memory(350)
|
||||
|
||||
|
||||
def test_opcode_rewrite_equals_oob_load(tmp_path):
|
||||
from tinygrad import Tensor
|
||||
from iqpilot.selfdrive.iqmodeld.tools.oob_rewrite import rewrite_oob
|
||||
big = Tensor(np.random.default_rng(0).standard_normal((512, 512)).astype(np.float32), device="CPU").realize()
|
||||
small = Tensor(np.arange(16, dtype=np.float32), device="CPU").realize()
|
||||
b = {"format": 2, "w": big, "s": small, "meta": {"k": "v"}, "raw": os.urandom(200_000)}
|
||||
plain = tmp_path / "plain.pkl"
|
||||
with open(plain, "wb") as f:
|
||||
pickle.dump(b, f, protocol=5)
|
||||
oob = tmp_path / "oob.pkl"
|
||||
moved, _ = rewrite_oob(str(plain), str(oob))
|
||||
assert moved >= 2 and is_oob(str(oob))
|
||||
got = load_bundle(str(oob))
|
||||
np.testing.assert_array_equal(got["w"].numpy(), b["w"].numpy())
|
||||
np.testing.assert_array_equal(got["s"].numpy(), b["s"].numpy())
|
||||
assert got["raw"] == b["raw"] and got["meta"] == {"k": "v"}
|
||||
133
iqpilot/selfdrive/iqmodeld/tests/test_model_bundle_downloader.py
Normal file
133
iqpilot/selfdrive/iqmodeld/tests/test_model_bundle_downloader.py
Normal file
@@ -0,0 +1,133 @@
|
||||
"""
|
||||
Copyright © IQ.Lvbs, apart of Project Teal Lvbs, All Rights Reserved, licensed under https://konn3kt.com/tos/
|
||||
"""
|
||||
import hashlib
|
||||
import http.server
|
||||
import os
|
||||
import threading
|
||||
|
||||
import pytest
|
||||
|
||||
from iqpilot.selfdrive.iqmodeld import model_bundle_downloader as dl
|
||||
|
||||
|
||||
class _RangeHandler(http.server.BaseHTTPRequestHandler):
|
||||
store: dict[str, bytes] = {}
|
||||
cut_first: dict[str, int] = {}
|
||||
hits: list[tuple[str, str | None]] = []
|
||||
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
def do_GET(self):
|
||||
oid = self.path.rsplit("/", 1)[-1]
|
||||
data = self.store[oid]
|
||||
rng = self.headers.get("Range")
|
||||
self.hits.append((oid, rng))
|
||||
start = int(rng.split("=")[1].rstrip("-")) if rng else 0
|
||||
body = data[start:]
|
||||
cut = self.cut_first.pop(oid, None)
|
||||
if cut is not None:
|
||||
body = body[:cut]
|
||||
self.send_response(206 if rng else 200)
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
if rng:
|
||||
self.send_header("Content-Range", f"bytes {start}-{start + len(body) - 1}/{len(data)}")
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def server():
|
||||
srv = http.server.ThreadingHTTPServer(("127.0.0.1", 0), _RangeHandler)
|
||||
t = threading.Thread(target=srv.serve_forever, daemon=True)
|
||||
t.start()
|
||||
yield srv
|
||||
srv.shutdown()
|
||||
srv.server_close()
|
||||
|
||||
|
||||
def _objects(parts):
|
||||
return [{"oid": hashlib.sha256(p).hexdigest(), "size": len(p)} for p in parts]
|
||||
|
||||
|
||||
def test_resume_continues_a_cut_part_and_reuses_finished_parts(server, tmp_path, monkeypatch):
|
||||
parts = [os.urandom(300_000), os.urandom(300_000), os.urandom(120_000)]
|
||||
objs = _objects(parts)
|
||||
_RangeHandler.store = {o["oid"]: p for o, p in zip(objs, parts, strict=True)}
|
||||
_RangeHandler.hits = []
|
||||
_RangeHandler.cut_first = {objs[1]["oid"]: 100_000}
|
||||
port = server.server_address[1]
|
||||
monkeypatch.setattr(dl, "_requests_auth", lambda: None)
|
||||
monkeypatch.setattr(dl, "_resolve_oid", lambda session, base, oid, size, auth: (f"http://127.0.0.1:{port}/o/{oid}", {}))
|
||||
monkeypatch.setattr(dl, "MODELS_BASE_URLS", ("http://unused",))
|
||||
monkeypatch.setattr(dl, "STREAM_RETRIES", 3)
|
||||
monkeypatch.setattr(dl, "CHUNK", 64 * 1024)
|
||||
whole = b"".join(parts)
|
||||
dst = str(tmp_path / "model.pkl")
|
||||
out = dl.download_lfs_bundle(objs, dst, hashlib.sha256(whole).hexdigest(), len(whole))
|
||||
with open(dst, "rb") as f:
|
||||
assert out == dst and f.read() == whole
|
||||
assert not os.path.exists(dst + ".parts")
|
||||
ranges = [r for o, r in _RangeHandler.hits if o == objs[1]["oid"]]
|
||||
assert ranges[0] is None and ranges[1] == "bytes=100000-"
|
||||
assert sum(1 for o, _ in _RangeHandler.hits if o == objs[0]["oid"]) == 1
|
||||
|
||||
|
||||
def test_corrupt_finished_part_is_refetched(server, tmp_path, monkeypatch):
|
||||
parts = [os.urandom(200_000), os.urandom(50_000)]
|
||||
objs = _objects(parts)
|
||||
_RangeHandler.store = {o["oid"]: p for o, p in zip(objs, parts, strict=True)}
|
||||
_RangeHandler.hits = []
|
||||
_RangeHandler.cut_first = {}
|
||||
port = server.server_address[1]
|
||||
monkeypatch.setattr(dl, "_requests_auth", lambda: None)
|
||||
monkeypatch.setattr(dl, "_resolve_oid", lambda session, base, oid, size, auth: (f"http://127.0.0.1:{port}/o/{oid}", {}))
|
||||
monkeypatch.setattr(dl, "MODELS_BASE_URLS", ("http://unused",))
|
||||
dst = str(tmp_path / "model.pkl")
|
||||
os.makedirs(dst + ".parts")
|
||||
with open(dl._part_path(dst, objs[0]["oid"]), "wb") as f:
|
||||
f.write(os.urandom(200_000))
|
||||
whole = b"".join(parts)
|
||||
dl.download_lfs_bundle(objs, dst, hashlib.sha256(whole).hexdigest(), len(whole))
|
||||
with open(dst, "rb") as f:
|
||||
assert f.read() == whole
|
||||
|
||||
|
||||
def test_hf_single_file_resumes_after_cut(server, tmp_path, monkeypatch):
|
||||
data = os.urandom(700_000)
|
||||
oid = hashlib.sha256(data).hexdigest()
|
||||
_RangeHandler.store = {oid: data}
|
||||
_RangeHandler.hits = []
|
||||
_RangeHandler.cut_first = {oid: 250_000}
|
||||
port = server.server_address[1]
|
||||
monkeypatch.setattr(dl, "_hf", lambda: ({"Authorization": "Bearer test"}, lambda p: f"http://127.0.0.1:{port}/o/{oid}"))
|
||||
monkeypatch.setattr(dl, "STREAM_RETRIES", 3)
|
||||
monkeypatch.setattr(dl, "CHUNK", 64 * 1024)
|
||||
dst = str(tmp_path / "policy.pkl")
|
||||
out = dl.download_hf_file("egpu/policy/x.pkl", dst, oid, len(data))
|
||||
with open(dst, "rb") as f:
|
||||
assert out == dst and f.read() == data
|
||||
ranges = [r for o, r in _RangeHandler.hits if o == oid]
|
||||
assert ranges[0] is None and ranges[1] == "bytes=250000-"
|
||||
assert not os.path.exists(dst + ".hfpart")
|
||||
|
||||
|
||||
def test_download_onnx_prefers_hf_then_falls_back(tmp_path, monkeypatch):
|
||||
from iqpilot.selfdrive.iqmodeld import egpu_helpers as eh
|
||||
meta = {"key": "m", "sha256": "ab" * 32, "download": {"kind": "comma_lfs", "size": 5}}
|
||||
monkeypatch.setattr(eh, "onnx_cache_path", lambda m: str(tmp_path / "m.onnx"))
|
||||
monkeypatch.setattr("iqpilot.selfdrive.iqmodeld.egpu_model.download_descriptor", lambda m: ("commalfs:" + m["sha256"], 5), raising=False)
|
||||
calls = []
|
||||
import iqpilot.selfdrive.iqmodeld.model_bundle_downloader as dlm
|
||||
monkeypatch.setattr(dlm, "download_hf_file", lambda path, dst, sha, size, progress_cb=None: (calls.append(("hf", path)), open(dst, "wb").close(), dst)[2])
|
||||
monkeypatch.setattr(eh, "resolve_download_url", lambda *a, **k: (calls.append(("lfs",)), "http://unused")[1])
|
||||
out = eh.download_onnx(meta)
|
||||
assert calls == [("hf", "onnx/" + "ab" * 32 + ".onnx")] and out == str(tmp_path / "m.onnx")
|
||||
calls.clear()
|
||||
def boom(*a, **k):
|
||||
calls.append(("hf-fail",)); raise RuntimeError("hf down")
|
||||
monkeypatch.setattr(dlm, "download_hf_file", boom)
|
||||
with pytest.raises(Exception):
|
||||
eh.download_onnx(meta)
|
||||
assert calls[:2] == [("hf-fail",), ("lfs",)]
|
||||
Reference in New Issue
Block a user