Remove nested directory: BitTransformerLM/tests/test_dashboard.py
Browse files
BitTransformerLM/tests/test_dashboard.py
DELETED
|
@@ -1,90 +0,0 @@
|
|
| 1 |
-
import os, sys; sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
| 2 |
-
|
| 3 |
-
import torch
|
| 4 |
-
import mcp_server as dash
|
| 5 |
-
from mcp_server import app
|
| 6 |
-
from bit_transformer.dashboard_app import ModelManager, MetricDriftWarning
|
| 7 |
-
from bit_transformer import BitTransformerLM
|
| 8 |
-
from bit_transformer.optimization import configure_optimizer
|
| 9 |
-
from bit_transformer.bit_io import text_to_bits
|
| 10 |
-
import time
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def test_exec_endpoint_removed():
|
| 14 |
-
with app.test_client() as client:
|
| 15 |
-
resp = client.post("/exec", json={"code": "print('OK')"})
|
| 16 |
-
assert resp.status_code in (403, 404)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def test_status_endpoint(tmp_path):
|
| 20 |
-
dash.manager = ModelManager(snapshot_dir=tmp_path)
|
| 21 |
-
params = {"d_model": 16, "nhead": 2, "num_layers": 1, "dim_feedforward": 32, "max_seq_len": 8}
|
| 22 |
-
with app.test_client() as client:
|
| 23 |
-
client.post("/init", json=params)
|
| 24 |
-
resp = client.get("/status")
|
| 25 |
-
data = resp.get_json()
|
| 26 |
-
assert data["d_model"] == 16 and data["num_layers"] == 1
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def test_modelmanager_compression(tmp_path):
|
| 30 |
-
mgr = ModelManager(snapshot_dir=tmp_path)
|
| 31 |
-
mgr.model = BitTransformerLM(d_model=16, nhead=2, num_layers=1, dim_feedforward=32, max_seq_len=8)
|
| 32 |
-
mgr.optimizer, mgr.scheduler = configure_optimizer(mgr.model, lr=1e-3, total_steps=1)
|
| 33 |
-
mgr.set_compression(True)
|
| 34 |
-
bits = torch.randint(0, 2, (1, 8), dtype=torch.long)
|
| 35 |
-
loss, ratio = mgr.train_step(bits)
|
| 36 |
-
assert isinstance(loss, float) and 0 <= ratio <= 1.0
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
def test_metric_drift_warning(tmp_path):
|
| 40 |
-
mgr = ModelManager(snapshot_dir=tmp_path, drift_window=2, drift_threshold=0.1)
|
| 41 |
-
tele = {
|
| 42 |
-
"negentropy_logits": torch.tensor([0.0]),
|
| 43 |
-
"lz_complexity_logits": torch.tensor([0.0]),
|
| 44 |
-
"symbiosis_score": torch.tensor([0.0]),
|
| 45 |
-
}
|
| 46 |
-
for _ in range(4):
|
| 47 |
-
mgr._log_metrics(tele)
|
| 48 |
-
tele_drift = {
|
| 49 |
-
"negentropy_logits": torch.tensor([1.0]),
|
| 50 |
-
"lz_complexity_logits": torch.tensor([0.0]),
|
| 51 |
-
"symbiosis_score": torch.tensor([0.0]),
|
| 52 |
-
}
|
| 53 |
-
import pytest
|
| 54 |
-
|
| 55 |
-
with pytest.warns(MetricDriftWarning):
|
| 56 |
-
mgr._log_metrics(tele_drift)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
def test_dashboard_endpoints(tmp_path):
|
| 60 |
-
dash.manager = ModelManager(snapshot_dir=tmp_path)
|
| 61 |
-
params = {"d_model": 16, "nhead": 2, "num_layers": 1, "dim_feedforward": 32, "max_seq_len": 8}
|
| 62 |
-
with app.test_client() as client:
|
| 63 |
-
resp = client.post("/init", json=params)
|
| 64 |
-
assert resp.status_code == 200
|
| 65 |
-
bits = torch.randint(0, 2, (1, 8), dtype=torch.long).tolist()
|
| 66 |
-
train = client.post("/train", json={"bits": bits})
|
| 67 |
-
assert train.status_code == 200
|
| 68 |
-
job_id = train.get_json()["job_id"]
|
| 69 |
-
for _ in range(20):
|
| 70 |
-
job_resp = client.get(f"/job/{job_id}")
|
| 71 |
-
data = job_resp.get_json()
|
| 72 |
-
if data["status"] == "completed":
|
| 73 |
-
assert "loss" in data["result"]
|
| 74 |
-
break
|
| 75 |
-
time.sleep(0.1)
|
| 76 |
-
else:
|
| 77 |
-
assert False, "training job did not complete"
|
| 78 |
-
infer = client.post("/infer", json={"bits": bits})
|
| 79 |
-
assert infer.status_code == 200 and "predicted" in infer.get_json()
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
def test_text_to_bits_and_dataset(tmp_path):
|
| 83 |
-
dash.manager = ModelManager(snapshot_dir=tmp_path)
|
| 84 |
-
with app.test_client() as client:
|
| 85 |
-
resp = client.post("/text_to_bits", json={"text": "hi"})
|
| 86 |
-
assert resp.status_code == 200
|
| 87 |
-
assert resp.get_json()["bits"] == text_to_bits("hi")
|
| 88 |
-
ds = client.get("/dataset?name=wikitext2&split=train&size=1&seq_len=8")
|
| 89 |
-
data = ds.get_json()
|
| 90 |
-
assert len(data["bits"]) == 1 and len(data["bits"][0]) == 8
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|