-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_python_integration.py
More file actions
277 lines (220 loc) · 7.66 KB
/
Copy pathtest_python_integration.py
File metadata and controls
277 lines (220 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""
Tests for Python "Ghost" Integration
Validates that the Python API works correctly
Author: Luiz Antonio De Lima Mendonca
Location: Resende, RJ, Brazil
Instagram: @luizinvict
Date: 2026-03-25
"""
import sys
import os
import random
# Add parent directory to path to import aureum
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def test_imports():
"""Tests that all imports work"""
print("Test 1: Imports")
try:
# Direct import from local __init__.py
from frontend.aureum_stdlib import AureumModel, ClassifyResult
from frontend.aureum_ffi import get_kernel
print(" - basic imports: OK")
# Check classes
assert AureumModel is not None
assert ClassifyResult is not None
print(" - Classes: OK")
# Check kernel
kernel = get_kernel()
assert kernel is not None
print(" - Rust Kernel: OK")
print("PASSED\n")
return True
except Exception as e:
print(f"FAILED: {e}\n")
return False
def test_fast_compute():
"""Tests fast_compute()"""
print("Test 2: fast_compute()")
try:
from frontend.aureum_ffi import get_kernel
# Simple test
input_data = [10, 20, 30, 40]
weights = [1, -1, 0, 1]
kernel = get_kernel()
packed = kernel.pack_ternary(weights)
result = kernel.bitnet_infer(input_data, packed, len(input_data))
# Check result
expected = 10 * 1 + 20 * (-1) + 30 * 0 + 40 * 1 # = 30
assert result == expected, f"Expected {expected}, got {result}"
print(f" - Result: {result} (correct)")
print("PASSED\n")
return True
except Exception as e:
print(f"FAILED: {e}\n")
return False
def test_fast_classify():
"""Tests fast_classify()"""
print("Test 3: fast_classify()")
try:
from frontend.aureum_stdlib import classify
# 3 classes, 4 features
input_data = [10, 20, -5, 8]
weights = [
1, 0, -1, 1, # class 0: score = 23
0, 1, 1, 0, # class 1: score = 15
-1, 1, 0, 1, # class 2: score = 18
]
labels = ["cat", "dog", "bird"]
result = classify(input_data, weights, 3, labels=labels)
label = result.label
# Class 0 has highest score
assert label == "cat", f"Expected 'cat', got '{label}'"
print(f" - Classification: {label} (correct)")
print("PASSED\n")
return True
except Exception as e:
print(f"FAILED: {e}\n")
return False
def test_aureum_model():
"""Tests AureumModel"""
print("Test 4: AureumModel")
try:
from frontend.aureum_stdlib import AureumModel
# Create model
model = AureumModel(
input_dim=100,
num_classes=5,
labels=["A", "B", "C", "D", "E"]
)
# Initialize random weights
model.random_weights(seed=42)
# Classify
input_data = [random.randint(-50, 50) for _ in range(100)]
result = model.classify(input_data)
# Check result
assert 0 <= result.class_id < 5
assert result.label in ["A", "B", "C", "D", "E"]
print(f" - Classification: {result.label} (class {result.class_id})")
print(f" - Score: {result.score}")
print("PASSED\n")
return True
except Exception as e:
print(f"FAILED: {e}\n")
return False
def test_embeddings():
"""Tests embedding generation"""
print("Test 5: Embeddings")
try:
from frontend.aureum_stdlib import AureumModel
# Create model
model = AureumModel(
input_dim=256,
embed_dim=64
).random_weights(seed=42)
# Generate embeddings
input_a = [random.randint(-50, 50) for _ in range(256)]
input_b = [random.randint(-50, 50) for _ in range(256)]
emb_a = model.embed(input_a)
emb_b = model.embed(input_b)
# Check dimensions
assert len(emb_a) == 64
assert len(emb_b) == 64
print(f" - Dimension: {len(emb_a)} (correct)")
# Calculate similarity
sim = model.similarity(input_a, input_b)
print(f" - Similarity: {sim.score}")
# Identical embeddings should have high similarity
sim_self = model.similarity(input_a, input_a)
assert sim_self.score > 0, "Self-similarity should be positive"
print(f" - Self-similarity: {sim_self.score} (correct)")
print("PASSED\n")
return True
except Exception as e:
print(f"FAILED: {e}\n")
return False
def test_numpy_compat():
"""Tests NumPy compatibility"""
print("Test 6: NumPy Compatibility")
try:
from frontend.aureum_ffi import get_kernel
# Direct dot product
a = [10, 20, 30]
b = [1, 0, -1]
kernel = get_kernel()
packed = kernel.pack_ternary(b)
result = kernel.bitnet_infer(a, packed, len(a))
expected = 10 * 1 + 20 * 0 + 30 * (-1) # = -20
assert result == expected
print(f" - dot product: {result} (correct)")
print("PASSED\n")
return True
except Exception as e:
print(f"FAILED: {e}\n")
return False
def test_performance():
"""Tests basic performance"""
print("Test 7: Performance")
try:
from frontend.aureum_ffi import get_kernel
import time
# Prepare large data
size = 1000
iterations = 1000
input_data = [random.randint(-100, 100) for _ in range(size)]
weights = [random.choice([-1, 0, 1]) for _ in range(size)]
kernel = get_kernel()
packed = kernel.pack_ternary(weights)
# Benchmark
start = time.time()
for _ in range(iterations):
result = kernel.bitnet_infer(input_data, packed, len(input_data))
elapsed = time.time() - start
ops_per_sec = iterations / elapsed
print(f" - {iterations} operations in {elapsed:.3f}s")
print(f" - {ops_per_sec:.0f} ops/s")
# Should be fast (>100 ops/s)
assert ops_per_sec > 100, f"Performance too low: {ops_per_sec:.0f} ops/s"
print("PASSED\n")
return True
except Exception as e:
print(f"FAILED: {e}\n")
return False
def main():
print("\n" + "=" * 70)
print("PYTHON 'GHOST' INTEGRATION TESTS")
print("=" * 70 + "\n")
tests = [
test_imports,
test_fast_compute,
test_fast_classify,
test_aureum_model,
test_embeddings,
test_numpy_compat,
test_performance,
]
results = []
for test in tests:
try:
passed = test()
results.append(passed)
except Exception as e:
print(f"CRITICAL ERROR: {e}\n")
results.append(False)
# Summary
print("=" * 70)
print("SUMMARY")
print("=" * 70)
passed = sum(results)
total = len(results)
print(f"Tests passed: {passed}/{total}")
if passed == total:
print("\nSTATUS: ALL TESTS PASSED")
print("\nThe Python 'Ghost' integration is working perfectly!")
print("Users can import Aureum as a Python library and")
print("accelerate their bottlenecks without rewriting existing code.")
return 0
else:
print(f"\nSTATUS: {total - passed} TESTS FAILED")
return 1
if __name__ == "__main__":
sys.exit(main())