Premchan369/Q-TensorFormer
2185
1"""2Tests for Information-Value Resource Allocator.3"""4 5import sys6import os7sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))8 9import torch10import pytest11from src.resource_allocator import InformationValueAllocator, AllocationBudget12 13 14def test_resource_allocator_decisions():15 allocator = InformationValueAllocator(info_dim=8, hidden_dim=16)16 17 B, T = 2, 818 z_t = torch.rand(B, T, 8)19 20 decisions, diagnostics = allocator(z_t, preset="balanced")21 22 assert decisions["rank"] in [1, 2, 4, 8]23 assert decisions["attn_mode_idx"].shape == (B, T)24 assert decisions["depth_mode"] in ["skip", "partial", "full"]25 assert decisions["kv_precision"] in ["fp16", "int8", "int4"]26 assert "chosen_rank" in diagnostics27 assert "routing_churn_rate" in diagnostics28 print("✓ test_resource_allocator_decisions passed")29 30 31def test_resource_allocator_hysteresis():32 allocator = InformationValueAllocator(info_dim=8, hidden_dim=16, hysteresis_tau=0.5)33 34 B, T = 1, 435 z_t_1 = torch.full((B, T, 8), 0.2)36 decisions_1, _ = allocator(z_t_1)37 rank_1 = decisions_1["rank"]38 39 # Small perturbation that should NOT break hysteresis threshold40 z_t_2 = torch.full((B, T, 8), 0.22)41 decisions_2, _ = allocator(z_t_2)42 rank_2 = decisions_2["rank"]43 44 assert rank_1 == rank_2, f"Hysteresis should preserve rank on small delta: {rank_1} vs {rank_2}"45 print("✓ test_resource_allocator_hysteresis passed")46 47 48def test_resource_allocator_presets():49 allocator = InformationValueAllocator(info_dim=8)50 z_t = torch.rand(2, 4, 8)51 52 # Edge preset should force classical53 decisions_edge, diag_edge = allocator(z_t, preset="edge")54 assert not decisions_edge["is_quantum_token"].any(), "Edge preset should disable quantum tokens"55 56 # Classical-only preset should force classical57 decisions_class, _ = allocator(z_t, preset="classical_only")58 assert not decisions_class["is_quantum_token"].any(), "Classical-only preset should disable quantum tokens"59 print("✓ test_resource_allocator_presets passed")60 61 62if __name__ == "__main__":63 test_resource_allocator_decisions()64 test_resource_allocator_hysteresis()65 test_resource_allocator_presets()66 print("All Resource Allocator tests passed!")67 