-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
403 lines (288 loc) · 9.46 KB
/
Copy pathtests.py
File metadata and controls
403 lines (288 loc) · 9.46 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
"""
Tests for Omega Tensor library
"""
import sys
import os
# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from omega_tensor import Tensor, nn, optim
import numpy as np
def test_tensor_creation():
"""Test tensor creation"""
print("Testing tensor creation...")
# From list
t1 = Tensor([1, 2, 3])
assert t1.shape == (3,)
assert np.allclose(t1.data, [1, 2, 3])
# From numpy array
t2 = Tensor(np.array([[1, 2], [3, 4]]))
assert t2.shape == (2, 2)
# Static constructors
t3 = Tensor.zeros(2, 3)
assert t3.shape == (2, 3)
assert np.allclose(t3.data, 0)
t4 = Tensor.ones(3, 3)
assert t4.shape == (3, 3)
assert np.allclose(t4.data, 1)
print("✓ Tensor creation tests passed")
def test_basic_operations():
"""Test basic arithmetic operations"""
print("Testing basic operations...")
x = Tensor([1, 2, 3])
y = Tensor([4, 5, 6])
# Addition
z = x + y
assert np.allclose(z.data, [5, 7, 9])
# Subtraction
z = x - y
assert np.allclose(z.data, [-3, -3, -3])
# Multiplication
z = x * y
assert np.allclose(z.data, [4, 10, 18])
# Division
z = x / Tensor([2, 2, 2])
assert np.allclose(z.data, [0.5, 1, 1.5])
# Power
z = x ** 2
assert np.allclose(z.data, [1, 4, 9])
print("✓ Basic operations tests passed")
def test_matrix_multiplication():
"""Test matrix multiplication"""
print("Testing matrix multiplication...")
A = Tensor([[1, 2], [3, 4]])
B = Tensor([[5, 6], [7, 8]])
C = A @ B
expected = np.array([[19, 22], [43, 50]])
assert np.allclose(C.data, expected)
print("✓ Matrix multiplication tests passed")
def test_gradients():
"""Test gradient computation"""
print("Testing gradients...")
# Simple gradient
x = Tensor([2.0], requires_grad=True)
y = x ** 2
y.backward()
# dy/dx = 2x = 4
assert np.allclose(x.grad, [4.0])
# Chain rule
x = Tensor([3.0], requires_grad=True)
y = Tensor([2.0], requires_grad=True)
z = x * y
w = z + x
w.backward()
# dw/dx = y + 1 = 3
assert np.allclose(x.grad, [3.0])
# dw/dy = x = 3
assert np.allclose(y.grad, [3.0])
print("✓ Gradient tests passed")
def test_activation_functions():
"""Test activation functions and their gradients"""
print("Testing activation functions...")
x = Tensor([-1.0, 0.0, 1.0], requires_grad=True)
# ReLU
y = x.relu()
assert np.allclose(y.data, [0.0, 0.0, 1.0])
# Sigmoid
x2 = Tensor([0.0], requires_grad=True)
y = x2.sigmoid()
assert np.allclose(y.data, [0.5], atol=1e-6)
# Tanh
x3 = Tensor([0.0], requires_grad=True)
y = x3.tanh()
assert np.allclose(y.data, [0.0], atol=1e-6)
print("✓ Activation function tests passed")
def test_reshape_transpose():
"""Test reshape and transpose operations"""
print("Testing reshape and transpose...")
x = Tensor([[1, 2, 3], [4, 5, 6]])
# Reshape
y = x.reshape(3, 2)
assert y.shape == (3, 2)
# Transpose
y = x.transpose()
assert y.shape == (3, 2)
assert np.allclose(y.data, [[1, 4], [2, 5], [3, 6]])
print("✓ Reshape and transpose tests passed")
def test_reduction_operations():
"""Test reduction operations"""
print("Testing reduction operations...")
x = Tensor([[1, 2, 3], [4, 5, 6]], requires_grad=True)
# Sum
y = x.sum()
assert np.allclose(y.data, 21)
# Sum with axis
y = x.sum(axis=0)
assert np.allclose(y.data, [5, 7, 9])
# Mean
y = x.mean()
assert np.allclose(y.data, 3.5)
print("✓ Reduction operation tests passed")
def test_broadcasting():
"""Test broadcasting in operations"""
print("Testing broadcasting...")
x = Tensor([[1, 2, 3]], requires_grad=True) # (1, 3)
y = Tensor([10], requires_grad=True) # (1,)
z = x + y
assert z.shape == (1, 3)
assert np.allclose(z.data, [[11, 12, 13]])
print("✓ Broadcasting tests passed")
def test_linear_layer():
"""Test linear layer"""
print("Testing linear layer...")
layer = nn.Linear(5, 3)
x = Tensor.randn(2, 5)
y = layer(x)
assert y.shape == (2, 3)
# Test backward
loss = y.sum()
loss.backward()
assert layer.weight.grad is not None
assert layer.bias.grad is not None
print("✓ Linear layer tests passed")
def test_sequential_model():
"""Test sequential model"""
print("Testing sequential model...")
model = nn.Sequential(
nn.Linear(10, 20),
nn.ReLU(),
nn.Linear(20, 5)
)
x = Tensor.randn(3, 10)
y = model(x)
assert y.shape == (3, 5)
print("✓ Sequential model tests passed")
def test_optimizers():
"""Test optimizer functionality"""
print("Testing optimizers...")
# Create a simple parameter
x = Tensor([5.0], requires_grad=True)
# Test SGD
optimizer = optim.SGD([x], lr=0.1)
loss = (x - 2) ** 2
optimizer.zero_grad()
loss.backward()
old_value = x.data.copy()
optimizer.step()
# Check that parameter was updated
assert not np.allclose(x.data, old_value)
# Test Adam
x = Tensor([5.0], requires_grad=True)
optimizer = optim.Adam([x], lr=0.1)
loss = (x - 2) ** 2
optimizer.zero_grad()
loss.backward()
optimizer.step()
print("✓ Optimizer tests passed")
def test_decentralized_storage():
"""Test decentralized tensor storage"""
print("Testing decentralized storage...")
initial_count = len(Tensor._tensor_registry)
t1 = Tensor([1, 2, 3])
t2 = Tensor([4, 5, 6])
# Check that tensors are in registry
assert t1.id in Tensor._tensor_registry
assert t2.id in Tensor._tensor_registry
# Check that registry grew
assert len(Tensor._tensor_registry) == initial_count + 2
print("✓ Decentralized storage tests passed")
def test_next_gen_models():
"""Test revolutionary next-gen models"""
print("Testing next-gen models...")
from omega_tensor.revolutionary_models import NextGenTransformer, SpaceTimeEmbedding
# Test SpaceTimeEmbedding
emb = SpaceTimeEmbedding(10, 4, max_len=10)
# Using numpy array directly for indices as now supported/handled
x = Tensor(np.array([[0, 1, 2]]))
y = emb(x)
assert y.shape == (1, 3, 4)
y.sum().backward()
assert emb.embedding.weight.grad is not None
assert emb.time_factor.grad is not None
# Test NextGenTransformer
model = NextGenTransformer(
num_embeddings=20,
embed_dim=8,
num_heads=2,
num_layers=1,
feedforward_dim=16,
num_classes=2
)
x = Tensor(np.zeros((2, 5))) # Batch 2, Seq 5
out = model(x)
assert out.shape == (2, 2)
loss = out.sum()
loss.backward()
assert model.classifier.weight.grad is not None
print("✓ Next-gen models tests passed")
def test_jarvis():
"""Test JARVIS AI assistant"""
print("Testing JARVIS AI assistant...")
from omega_tensor import JARVIS, JARVISBrain
# --- JARVISBrain ---
brain = JARVISBrain()
# tokenize produces correct length
tokens = JARVISBrain.tokenize("hello jarvis")
assert len(tokens) == 128
# keyword boost is non-zero for recognised keywords
boost = brain._keyword_boost("hello")
assert boost[brain.INTENTS.index("greeting")] > 0.0
# classify returns a valid intent string
intent = brain.classify("hello jarvis")
assert intent in brain.INTENTS
# All well-known keywords map to the expected intent
keyword_cases = [
("hello there", "greeting"),
("goodbye", "farewell"),
("what time is it now", "time"),
("run diagnostics", "system"),
("calculate 2 plus 2", "compute"),
("help me", "help"),
]
for phrase, expected_intent in keyword_cases:
result = brain.classify(phrase)
assert result == expected_intent, (
f"classify('{phrase}') → '{result}', expected '{expected_intent}'"
)
# --- JARVIS ---
j = JARVIS(owner="Tester", verbose=False)
# Empty input
reply = j.respond("")
assert "Tester" in reply
# Non-empty input produces a non-empty string
reply = j.respond("hello jarvis")
assert isinstance(reply, str) and len(reply) > 0
# Round-robin: two calls with same intent return different canned responses
j2 = JARVIS(verbose=False)
r1 = j2._pick_response("status")
r2 = j2._pick_response("status")
assert r1 != r2
# Time-placeholder rendering
rendered = JARVIS._render_time_tokens("Time is {time}.")
assert "{time}" not in rendered
print("✓ JARVIS tests passed")
def run_all_tests():
"""Run all tests"""
print("=" * 60)
print("Running Omega Tensor Tests")
print("=" * 60)
print()
test_tensor_creation()
test_basic_operations()
test_matrix_multiplication()
test_gradients()
test_activation_functions()
test_reshape_transpose()
test_reduction_operations()
test_broadcasting()
test_linear_layer()
test_sequential_model()
test_optimizers()
test_decentralized_storage()
test_next_gen_models()
test_jarvis()
print()
print("=" * 60)
print("All tests passed! ✓")
print("=" * 60)
if __name__ == "__main__":
run_all_tests()