forked from zhaozhixu/TensorLight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstress_test.c
More file actions
222 lines (188 loc) · 6.84 KB
/
Copy pathstress_test.c
File metadata and controls
222 lines (188 loc) · 6.84 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
/*
* Stress test with larger input sizes
* Tests memory handling, numerical stability with large tensors
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "tofu_tensor.h"
/* Simple ReLU */
void relu(tofu_tensor* t) {
for (int i = 0; i < t->len; i++) {
float val;
TOFU_TENSOR_DATA_TO(t, i, val, TOFU_FLOAT);
if (val < 0) val = 0;
TOFU_TENSOR_DATA_FROM(t, i, val, TOFU_FLOAT);
}
}
/* Check for NaN/Inf */
int has_invalid_values(tofu_tensor* t) {
for (int i = 0; i < t->len; i++) {
float val;
TOFU_TENSOR_DATA_TO(t, i, val, TOFU_FLOAT);
if (isnan(val) || isinf(val)) {
return 1;
}
}
return 0;
}
/* Initialize tensor with random values */
void init_random(tofu_tensor* t, float scale) {
for (int i = 0; i < t->len; i++) {
float val = ((float)rand() / RAND_MAX - 0.5f) * scale;
TOFU_TENSOR_DATA_FROM(t, i, val, TOFU_FLOAT);
}
}
int main() {
printf("============================================================\n");
printf("Stress Test with Large Input Sizes\n");
printf("============================================================\n\n");
srand(42);
/* Test 1: Large batch size */
printf("Test 1: Large batch processing\n");
const int BATCH_SIZE = 128;
const int INPUT_SIZE = 256;
const int HIDDEN_SIZE = 512;
const int OUTPUT_SIZE = 128;
printf(" Batch: %d samples\n", BATCH_SIZE);
printf(" Input: %d features\n", INPUT_SIZE);
printf(" Hidden: %d neurons\n", HIDDEN_SIZE);
printf(" Output: %d classes\n\n", OUTPUT_SIZE);
/* Create tensors */
printf(" Allocating tensors...\n");
tofu_tensor* X = tofu_tensor_zeros(2, (int[]){BATCH_SIZE, INPUT_SIZE}, TOFU_FLOAT);
tofu_tensor* W1 = tofu_tensor_zeros(2, (int[]){INPUT_SIZE, HIDDEN_SIZE}, TOFU_FLOAT);
tofu_tensor* W2 = tofu_tensor_zeros(2, (int[]){HIDDEN_SIZE, OUTPUT_SIZE}, TOFU_FLOAT);
if (!X || !W1 || !W2) {
fprintf(stderr, " ✗ Failed to allocate tensors\n");
return 1;
}
printf(" ✓ Allocated %.2f MB\n",
(X->len + W1->len + W2->len) * sizeof(float) / 1024.0 / 1024.0);
/* Initialize */
printf(" Initializing with random values...\n");
init_random(X, 2.0f);
init_random(W1, 0.1f);
init_random(W2, 0.1f);
/* Forward pass */
printf(" Forward pass: [%d,%d] @ [%d,%d] -> [%d,%d]\n",
X->dims[0], X->dims[1], W1->dims[0], W1->dims[1],
BATCH_SIZE, HIDDEN_SIZE);
clock_t start = clock();
tofu_tensor* h1 = tofu_tensor_matmul(X, W1, NULL);
clock_t end = clock();
if (!h1) {
fprintf(stderr, " ✗ Layer 1 matmul failed\n");
return 1;
}
double time_ms = (double)(end - start) / CLOCKS_PER_SEC * 1000.0;
printf(" ✓ Layer 1 completed in %.2f ms\n", time_ms);
/* Check for numerical issues */
if (has_invalid_values(h1)) {
printf(" ✗ Found NaN/Inf in layer 1 output\n");
return 1;
}
printf(" ✓ No NaN/Inf detected\n");
relu(h1);
printf(" Forward pass: [%d,%d] @ [%d,%d] -> [%d,%d]\n",
h1->dims[0], h1->dims[1], W2->dims[0], W2->dims[1],
BATCH_SIZE, OUTPUT_SIZE);
start = clock();
tofu_tensor* output = tofu_tensor_matmul(h1, W2, NULL);
end = clock();
if (!output) {
fprintf(stderr, " ✗ Layer 2 matmul failed\n");
return 1;
}
time_ms = (double)(end - start) / CLOCKS_PER_SEC * 1000.0;
printf(" ✓ Layer 2 completed in %.2f ms\n", time_ms);
if (has_invalid_values(output)) {
printf(" ✗ Found NaN/Inf in output\n");
return 1;
}
printf(" ✓ Output is valid\n");
/* Compute statistics */
float sum = 0, min = INFINITY, max = -INFINITY;
for (int i = 0; i < output->len; i++) {
float val;
TOFU_TENSOR_DATA_TO(output, i, val, TOFU_FLOAT);
sum += val;
if (val < min) min = val;
if (val > max) max = val;
}
float mean = sum / output->len;
printf(" Output stats: min=%.6f, max=%.6f, mean=%.6f\n\n", min, max, mean);
/* Cleanup */
tofu_tensor_free_data_too(X);
tofu_tensor_free_data_too(W1);
tofu_tensor_free_data_too(W2);
tofu_tensor_free_data_too(h1);
tofu_tensor_free_data_too(output);
/* Test 2: Very deep network (memory stress) */
printf("Test 2: Very deep network (10 layers)\n");
const int DEPTH = 10;
const int LAYER_SIZE = 64;
printf(" Network: 10 layers, %d neurons each\n", LAYER_SIZE);
printf(" Total parameters: %d\n\n", DEPTH * LAYER_SIZE * LAYER_SIZE);
tofu_tensor* layers[DEPTH + 1];
layers[0] = tofu_tensor_zeros(2, (int[]){1, LAYER_SIZE}, TOFU_FLOAT);
init_random(layers[0], 1.0f);
printf(" Creating %d weight matrices...\n", DEPTH);
tofu_tensor* weights[DEPTH];
for (int i = 0; i < DEPTH; i++) {
weights[i] = tofu_tensor_zeros(2, (int[]){LAYER_SIZE, LAYER_SIZE}, TOFU_FLOAT);
if (!weights[i]) {
fprintf(stderr, " ✗ Failed to allocate weight matrix %d\n", i);
return 1;
}
init_random(weights[i], 0.1f);
}
printf(" ✓ Allocated %.2f MB for weights\n",
DEPTH * LAYER_SIZE * LAYER_SIZE * sizeof(float) / 1024.0 / 1024.0);
printf(" Forward pass through %d layers...\n", DEPTH);
start = clock();
for (int i = 0; i < DEPTH; i++) {
layers[i + 1] = tofu_tensor_matmul(layers[i], weights[i], NULL);
if (!layers[i + 1]) {
fprintf(stderr, " ✗ Layer %d matmul failed\n", i);
return 1;
}
relu(layers[i + 1]);
if (has_invalid_values(layers[i + 1])) {
fprintf(stderr, " ✗ NaN/Inf detected at layer %d\n", i);
return 1;
}
}
end = clock();
time_ms = (double)(end - start) / CLOCKS_PER_SEC * 1000.0;
printf(" ✓ Completed in %.2f ms\n", time_ms);
/* Final output stats */
tofu_tensor* final = layers[DEPTH];
sum = 0;
min = INFINITY;
max = -INFINITY;
for (int i = 0; i < final->len; i++) {
float val;
TOFU_TENSOR_DATA_TO(final, i, val, TOFU_FLOAT);
sum += val;
if (val < min) min = val;
if (val > max) max = val;
}
mean = sum / final->len;
printf(" Final output: min=%.6f, max=%.6f, mean=%.6f\n", min, max, mean);
/* Cleanup */
for (int i = 0; i <= DEPTH; i++) {
tofu_tensor_free_data_too(layers[i]);
}
for (int i = 0; i < DEPTH; i++) {
tofu_tensor_free_data_too(weights[i]);
}
printf("\n============================================================\n");
printf("Stress tests complete!\n");
printf("✓ Large batch processing works\n");
printf("✓ Very deep networks work\n");
printf("✓ No memory or numerical issues detected\n");
printf("============================================================\n");
return 0;
}