nx_lora_pool_test.nx source
↩ module page · 140 lines · 6814 B
1// nx_lora_pool_test.nx -- smoke for H9 Multi-LoRA adapter pool.
2
3import "nx_syscalls.nx"
4import "nx_lora_pool.nx"
5
6func main() -> i64 {
7 // Backing tensors are opaque to this primitive; allocate any
8 // non-null pointers to stand in for them.
9 let a_data: *i64 = (sys_mmap(64)) as *i64
10 let b_data: *i64 = (sys_mmap(64)) as *i64
11
12 // ----- 1. Pool construction -----
13 let p: *NxLoraPool = nx_lora_pool_new(8)
14 if nx_lora_pool_is_valid(p) != 1 { return 1 }
15 if nx_lora_n_loaded(p) != 0 { return 2 }
16
17 // ----- 2. Register 3 adapters -----
18 let s0: i64 = nx_lora_register(p, 1001, NX_LORA_RANK_8, 32, a_data, b_data, 65536)
19 if s0 != 0 { return 3 }
20 if nx_lora_n_loaded(p) != 1 { return 4 }
21
22 let s1: i64 = nx_lora_register(p, 1002, NX_LORA_RANK_16, 32, a_data, b_data, 131072)
23 if s1 != 1 { return 5 }
24
25 let s2: i64 = nx_lora_register(p, 1003, NX_LORA_RANK_4, 24, a_data, b_data, 32768)
26 if s2 != 2 { return 6 }
27 if nx_lora_n_loaded(p) != 3 { return 7 }
28
29 // ----- 3. Lookup -----
30 let a1: *NxLoraAdapter = nx_lora_lookup(p, 1001)
31 if nx_lora_adapter_is_valid(a1) != 1 { return 8 }
32 if nx_lora_adapter_id(a1) != 1001 { return 9 }
33 if nx_lora_adapter_rank(a1) != NX_LORA_RANK_8 { return 10 }
34 if nx_lora_adapter_n_layers(a1) != 32 { return 11 }
35 if nx_lora_adapter_alpha_q16(a1) != 65536 { return 12 }
36
37 let a2: *NxLoraAdapter = nx_lora_lookup(p, 1002)
38 if nx_lora_adapter_rank(a2) != NX_LORA_RANK_16 { return 13 }
39
40 let a_miss: *NxLoraAdapter = nx_lora_lookup(p, 9999)
41 if (a_miss as i64) != 0 { return 14 }
42
43 // ----- 4. Acquire / refcount -----
44 if nx_lora_refcount(p, 1001) != 0 { return 15 }
45 if nx_lora_acquire(p, 1001) != 1 { return 16 }
46 if nx_lora_acquire(p, 1001) != 2 { return 17 }
47 if nx_lora_acquire(p, 1001) != 3 { return 18 }
48 if nx_lora_refcount(p, 1001) != 3 { return 19 }
49
50 // ----- 5. Release -----
51 if nx_lora_release(p, 1001) != 2 { return 20 }
52 if nx_lora_release(p, 1001) != 1 { return 21 }
53 if nx_lora_release(p, 1001) != 0 { return 22 }
54 if nx_lora_refcount(p, 1001) != 0 { return 23 }
55
56 // Release-below-zero refused.
57 if nx_lora_release(p, 1001) != (0 - NX_LORA_BAD_INPUT) { return 24 }
58
59 // ----- 6. Evict refuses while in use -----
60 if nx_lora_acquire(p, 1002) != 1 { return 25 }
61 if nx_lora_evict(p, 1002) != (0 - NX_LORA_IN_USE) { return 26 }
62 if nx_lora_n_loaded(p) != 3 { return 27 }
63 if nx_lora_release(p, 1002) != 0 { return 28 }
64
65 // ----- 7. Evict succeeds when refcount==0 -----
66 if nx_lora_evict(p, 1002) != NX_LORA_OK { return 29 }
67 if nx_lora_n_loaded(p) != 2 { return 30 }
68 // Subsequent lookup misses.
69 if (nx_lora_lookup(p, 1002) as i64) != 0 { return 31 }
70 // The other two adapters survive the compaction.
71 if (nx_lora_lookup(p, 1001) as i64) == 0 { return 32 }
72 if (nx_lora_lookup(p, 1003) as i64) == 0 { return 33 }
73
74 // ----- 8. Duplicate register -----
75 if nx_lora_register(p, 1001, NX_LORA_RANK_8, 32, a_data, b_data, 65536) != (0 - NX_LORA_DUPLICATE) { return 34 }
76
77 // ----- 9. Bad rank rejected -----
78 if nx_lora_register(p, 2001, 3, 32, a_data, b_data, 65536) != (0 - NX_LORA_BAD_RANK) { return 35 }
79 if nx_lora_register(p, 2001, 5, 32, a_data, b_data, 65536) != (0 - NX_LORA_BAD_RANK) { return 36 }
80 if nx_lora_register(p, 2001, 128, 32, a_data, b_data, 65536) != (0 - NX_LORA_BAD_RANK) { return 37 }
81
82 // ----- 10. Bad n_layers / alpha / null data -----
83 if nx_lora_register(p, 2001, NX_LORA_RANK_8, 0, a_data, b_data, 65536) != (0 - NX_LORA_BAD_INPUT) { return 38 }
84 if nx_lora_register(p, 2001, NX_LORA_RANK_8, -1, a_data, b_data, 65536) != (0 - NX_LORA_BAD_INPUT) { return 39 }
85 if nx_lora_register(p, 2001, NX_LORA_RANK_8, 32, a_data, b_data, -1) != (0 - NX_LORA_BAD_INPUT) { return 40 }
86 let null_p: *i64 = (0 as i64) as *i64
87 if nx_lora_register(p, 2001, NX_LORA_RANK_8, 32, null_p, b_data, 65536) != (0 - NX_LORA_BAD_INPUT) { return 41 }
88 if nx_lora_register(p, 2001, NX_LORA_RANK_8, 32, a_data, null_p, 65536) != (0 - NX_LORA_BAD_INPUT) { return 42 }
89
90 // ----- 11. NOT_FOUND verdicts on missing adapters -----
91 if nx_lora_acquire(p, 9999) != (0 - NX_LORA_NOT_FOUND) { return 43 }
92 if nx_lora_release(p, 9999) != (0 - NX_LORA_NOT_FOUND) { return 44 }
93 if nx_lora_evict(p, 9999) != (0 - NX_LORA_NOT_FOUND) { return 45 }
94
95 // ----- 12. Fill the pool to FULL -----
96 let p_small: *NxLoraPool = nx_lora_pool_new(2)
97 if nx_lora_register(p_small, 1, NX_LORA_RANK_4, 8, a_data, b_data, 65536) != 0 { return 46 }
98 if nx_lora_register(p_small, 2, NX_LORA_RANK_4, 8, a_data, b_data, 65536) != 1 { return 47 }
99 if nx_lora_register(p_small, 3, NX_LORA_RANK_4, 8, a_data, b_data, 65536) != (0 - NX_LORA_FULL) { return 48 }
100
101 // Evict a slot then fill again.
102 if nx_lora_evict(p_small, 1) != NX_LORA_OK { return 49 }
103 if nx_lora_register(p_small, 3, NX_LORA_RANK_4, 8, a_data, b_data, 65536) != 1 { return 50 }
104
105 // ----- 13. Bad pool_new inputs -----
106 if (nx_lora_pool_new(0) as i64) != 0 { return 51 }
107 if (nx_lora_pool_new(-1) as i64) != 0 { return 52 }
108 if (nx_lora_pool_new(NX_LORA_POOL_MAX_ADAPTERS + 1) as i64) != 0 { return 53 }
109
110 // ----- 14. Tamper on canary -----
111 let tamper_p: *NxLoraPool = nx_lora_pool_new(4)
112 tamper_p.canary_post = 0xDEADBEEF
113 if nx_lora_pool_is_valid(tamper_p) != 0 { return 54 }
114 if nx_lora_register(tamper_p, 1, NX_LORA_RANK_8, 8, a_data, b_data, 65536) != (0 - NX_LORA_TAMPER) { return 55 }
115 if nx_lora_acquire(tamper_p, 1) != (0 - NX_LORA_TAMPER) { return 56 }
116 if nx_lora_release(tamper_p, 1) != (0 - NX_LORA_TAMPER) { return 57 }
117 if nx_lora_evict(tamper_p, 1) != (0 - NX_LORA_TAMPER) { return 58 }
118 if nx_lora_n_loaded(tamper_p) != -1 { return 59 }
119 if (nx_lora_lookup(tamper_p, 1) as i64) != 0 { return 60 }
120
121 // Tamper on adapter struct (canary_post corrupted in-place).
122 let p2: *NxLoraPool = nx_lora_pool_new(4)
123 nx_lora_register(p2, 7, NX_LORA_RANK_32, 32, a_data, b_data, 65536)
124 let a7: *NxLoraAdapter = nx_lora_lookup(p2, 7)
125 a7.canary_post = 0xDEADBEEF
126 if nx_lora_adapter_is_valid(a7) != 0 { return 61 }
127 if nx_lora_acquire(p2, 7) != (0 - NX_LORA_TAMPER) { return 62 }
128
129 // ----- 15. Sealed-enum gates -----
130 if nx_lora_rank_is_valid(NX_LORA_RANK_4) != 1 { return 63 }
131 if nx_lora_rank_is_valid(NX_LORA_RANK_64) != 1 { return 64 }
132 if nx_lora_rank_is_valid(0) != 0 { return 65 }
133 if nx_lora_rank_is_valid(7) != 0 { return 66 }
134 if nx_lora_verdict_is_valid(NX_LORA_OK) != 1 { return 67 }
135 if nx_lora_verdict_is_valid(NX_LORA_TAMPER) != 1 { return 68 }
136 if nx_lora_verdict_is_valid(-1) != 0 { return 69 }
137 if nx_lora_verdict_is_valid(NX_LORA_N_VERDICTS) != 0 { return 70 }
138
139 return 0
140}