-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.c
More file actions
68 lines (54 loc) · 1.05 KB
/
Copy pathtest.c
File metadata and controls
68 lines (54 loc) · 1.05 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
#include <stdio.h>
#include <stdlib.h>
typedef enum {
ADD = 10,
MUL,
MATMUL,
RELU,
SUB,
DIV,
EXP,
LOG
} OpName;
typedef struct Op {
OpType type;
const *char name;
void(*backward)(Arena *A, struct Tensor *self);
} Op;
typedef struct Tensor {
int id;
int *shape;
int *stride;
int ndim;
float *data;
// New parameters
Tensor *grad;
bool requires_grad;
Op *operations; // Added name, and type as well!
Tensor **parents;
int num_parents;
} Tensor;
Tensor *tensor_create_new(Arena *A, int ndim, int *shape) {
Tensor *t = arena_alloc(A, sizeof(Tensor));
t->ndim = ndim;
t->shape = arena_alloc(A, ndim * sizeof(int));
t->stride = arena_alloc(A, ndim * sizeof(int));
// define the shape of Tensor
int total = 1;
for (int i = ndim - 1; i >= 0; i--) {
t->shape[i] = shape[i];
t->stride[i] = total;
total *= shape[i];
}
// For autograd
t->data = arena_alloc(A, total * sizeof(float));
t->parents = NULL;
t->operations = NULL;
t->grad = NULL;
t->num_parents = 0;
return t;
}
int main() {
Tensor *x = tensor_create_new(
return 0;
}