-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathops_stack.c
More file actions
286 lines (264 loc) · 8.68 KB
/
Copy pathops_stack.c
File metadata and controls
286 lines (264 loc) · 8.68 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
#include "parser.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
/* Pops s (shape tensor: 1D, 1 or 2 elements) then a, reshapes a to the dimensions in s.
* Memory layout is unchanged; only shape.row and shape.col are updated.
* Validates dimensions >= 1 and product <= INT_MAX (entry point for user-supplied dims).
* Input: my_stack — the stack.
* Output: TF_OK on success. */
int op_reshape(stack *my_stack){
array_instance *s = stack_pop(my_stack);
if (s == NULL) return TF_ERR_STACK;
if (s->shape.row != 1 || s->shape.col < 1 || s->shape.col > 2){
fprintf(stderr, "error: 'r' requires a 1D shape tensor with 1 or 2 elements\n");
instance_free(s);
return TF_ERR_ARG;
}
array_instance *a = stack_pop(my_stack);
if (a == NULL) { instance_free(s); return TF_ERR_STACK; }
int new_rows = (s->shape.col == 2) ? (int)s->data[0] : 1;
int new_cols = (s->shape.col == 2) ? (int)s->data[1] : (int)s->data[0];
if (new_rows < 1 || new_cols < 1) {
fprintf(stderr, "error: 'r' requires dimensions >= 1\n");
instance_free(s); instance_free(a); return TF_ERR_ARG;
}
int64_t n64 = (int64_t)new_rows * (int64_t)new_cols;
if (n64 > INT_MAX) {
fprintf(stderr, "error: tensor too large\n");
instance_free(s); instance_free(a); return TF_ERR_ARG;
}
if (new_rows * new_cols != a->shape.row * a->shape.col){
fprintf(stderr, "error: reshape incompatible [%d %d] -> [%d %d]\n",
a->shape.row, a->shape.col, new_rows, new_cols);
instance_free(s);
instance_free(a);
return TF_ERR_SHAPE;
}
int ndim = s->shape.col;
instance_free(s);
int n = new_rows * new_cols;
float *new_data = malloc(sizeof(float) * (size_t)n);
if (!new_data) { instance_free(a); return TF_ERR_MEM; }
memcpy(new_data, a->data, sizeof(float) * (size_t)n);
instance_free(a);
shape_t shape = {new_rows, new_cols, ndim};
if (stack_push(my_stack, new_data, shape) != 0) { free(new_data); return TF_ERR_MEM; }
return TF_OK;
}
/* Pops the top tensor and prints it in the format: Tensor(shape=[r c], data=[...]).
* Input: my_stack — the stack.
* Output: TF_OK on success. */
int pop_print(stack *my_stack){
array_instance *last = stack_pop(my_stack);
if (last == NULL) return TF_ERR_STACK;
// perf: hoisted last->shape.row, last->shape.col, last->data — avoids repeated struct dereference in loop
int rows = last->shape.row;
int cols = last->shape.col;
float *data = last->data;
int total = rows * cols;
if (last->shape.ndim == 1)
printf("Tensor(shape=[%d], data=[", cols);
else
printf("Tensor(shape=[%d %d], data=[", rows, cols);
for (int i = 0; i < total; i++){
printf("%f", data[i]);
if (i < total - 1) printf(" ");
}
printf("])\n");
instance_free(last);
return TF_OK;
}
/* Pops the top tensor and prints it row by row for visual debugging.
* Input: my_stack — the stack.
* Output: TF_OK on success. */
int pop_print_as_matrix(stack *my_stack){
array_instance *last = stack_pop(my_stack);
if (last == NULL) return TF_ERR_STACK;
printf("on top of the stack was\n");
// perf: hoisted last->shape.row, last->shape.col, last->data — avoids reload each iteration
int rows = last->shape.row;
int cols = last->shape.col;
float *data = last->data;
/* row-major: elemento (i,j) -> data[i * shape.col + j] */
for (int i = 0; i < rows; i++){
printf("[ ");
for (int j = 0; j < cols; j++){
printf("%f ", data[i * cols + j]);
}
printf("]\n");
}
instance_free(last);
return TF_OK;
}
/* Duplicates the top element of the stack by incrementing its ref_count.
* Does not allocate a new tensor — both stack entries point to the same instance.
* Input: my_stack — the stack. */
int duplicate (stack *my_stack){
array_instance *top = stack_peek(my_stack);
if (top == NULL) return TF_ERR_STACK;
return stack_push_instance(my_stack, top) != 0 ? TF_ERR_MEM : TF_OK;
}
/* Swaps the top two stack items. Works for both tensors and strings.
* Input: my_stack — the stack.
* Output: TF_OK on success. */
int op_switch(stack *my_stack){
stack_item a = stack_pop_item(my_stack);
if (a.type == ITEM_NONE)
return TF_ERR_STACK;
stack_item b = stack_pop_item(my_stack);
if (b.type == ITEM_NONE) {
stack_free_item(a);
return TF_ERR_STACK;
}
int err = stack_push_item(my_stack, a);
stack_free_item(a);
if (err != 0) { stack_free_item(b); return TF_ERR_MEM; }
err = stack_push_item(my_stack, b);
stack_free_item(b);
return err != 0 ? TF_ERR_MEM : TF_OK;
}
/* Copies the second-from-top item to the top: ( a b -- a b a ).
* Input: my_stack — the stack.
* Output: TF_OK on success. */
int over (stack *my_stack){
stack_item a = stack_pop_item(my_stack);
if (a.type == ITEM_NONE)
return TF_ERR_STACK;
stack_item b = stack_pop_item(my_stack);
if (b.type == ITEM_NONE) {
stack_free_item(a);
return TF_ERR_STACK;
}
int err = stack_push_item(my_stack, b);
if (err != 0) {
stack_free_item(b);
stack_free_item(a);
return TF_ERR_MEM;
}
err = stack_push_item(my_stack, a);
stack_free_item(a);
if (err != 0) {
stack_free_item(b);
return TF_ERR_MEM;
}
err = stack_push_item(my_stack, b);
stack_free_item(b);
return err != 0 ? TF_ERR_MEM : TF_OK;
}
/* Pops and discards the top stack item.
* Input: my_stack — the stack.
* Output: TF_OK on success. */
int drop (stack *my_stack){
stack_item a = stack_pop_item(my_stack);
if (a.type == ITEM_NONE)
return TF_ERR_STACK;
stack_free_item(a);
return TF_OK;
}
/* Flattens the top tensor to a 1D row vector in place: shape [r c] -> [1, r*c].
* Input: my_stack — the stack.
* Output: TF_OK on success. */
int ravel(stack *my_stack) {
array_instance *a = stack_pop(my_stack);
if (!a) return TF_ERR_STACK;
if (a->ref_count == 1){
a->shape.col = a->shape.row * a->shape.col;
a->shape.row = 1;
a->shape.ndim = 1;
if (stack_push_instance(my_stack, a) != 0) {
instance_free(a);
return TF_ERR_MEM;
}
instance_free(a);
} else {
int new_col = a->shape.row * a->shape.col;
float *new_data = malloc(sizeof(float) * (size_t)new_col);
if (!new_data) { instance_free(a); return TF_ERR_MEM; }
memcpy(new_data, a->data, (size_t)new_col * sizeof(float));
shape_t shape = {1, new_col, 1};
instance_free(a);
if (stack_push(my_stack, new_data, shape) != 0) {
fprintf(stderr, "error: '_': stack push failed\n");
free(new_data);
return TF_ERR_MEM;
}
}
return TF_OK;
}
/* Pops a tensor and pushes a 1D tensor containing its shape.
* 2D tensor [r c] -> [r c] (1x2); 1D tensor [1 n] -> [n] (1x1).
* Input: my_stack — the stack.
* Output: TF_OK on success. */
int op_shape(stack *my_stack) {
array_instance *a = stack_pop(my_stack);
if (!a) return TF_ERR_STACK;
int is_1d = (a->shape.ndim == 1);
int count = is_1d ? 1 : 2;
float *shape_arr = malloc(sizeof(float) * (size_t)count);
if (!shape_arr) {
instance_free(a);
return TF_ERR_MEM;
}
if (is_1d) {
shape_arr[0] = (float)a->shape.col;
} else {
shape_arr[0] = (float)a->shape.row;
shape_arr[1] = (float)a->shape.col;
}
instance_free(a);
shape_t shape = {1, count, 1};
if (stack_push(my_stack, shape_arr, shape) != 0) {
free(shape_arr);
return TF_ERR_MEM;
}
return TF_OK;
}
/* Pops a value tensor v (top) and a shape tensor s, pushes a new tensor of shape s
* filled by cycling through the elements of v.
* Validates dimensions >= 1 and product <= INT_MAX (entry point for user-supplied dims).
* Input: my_stack — the stack (top: v, then s).
* Output: TF_OK on success. */
int fill(stack *my_stack) {
array_instance *v = stack_pop(my_stack);
if (!v) return TF_ERR_STACK;
array_instance *s = stack_pop(my_stack);
if (!s) { instance_free(v); return TF_ERR_STACK; }
if (s->shape.row != 1 || s->shape.col < 1 || s->shape.col > 2) {
fprintf(stderr, "error: 'f' requires a 1D shape tensor with 1 or 2 elements\n");
instance_free(s); instance_free(v); return TF_ERR_ARG;
}
int row = (s->shape.col == 2) ? (int)s->data[0] : 1;
int col = (s->shape.col == 2) ? (int)s->data[1] : (int)s->data[0];
if (row < 1 || col < 1) {
fprintf(stderr, "error: 'f' requires dimensions >= 1\n");
instance_free(s); instance_free(v); return TF_ERR_ARG;
}
int ndim = s->shape.col;
instance_free(s);
int64_t n64 = (int64_t)row * (int64_t)col;
if (n64 > INT_MAX) {
fprintf(stderr, "error: tensor too large\n");
instance_free(v); return TF_ERR_ARG;
}
int n = (int)n64;
int m = v->shape.row * v->shape.col;
if (m <= 0) {
fprintf(stderr, "error: 'f': value tensor cannot be empty\n");
instance_free(v);
return TF_ERR_ARG;
}
float *new_data = malloc(sizeof(float) * (size_t)n);
if (!new_data) { instance_free(v); return TF_ERR_MEM; }
for (int i = 0; i < n; i++)
new_data[i] = v->data[i % m];
instance_free(v);
shape_t shape = {row, col, ndim};
if (stack_push(my_stack, new_data, shape) != 0) {
free(new_data);
return TF_ERR_MEM;
}
return TF_OK;
}