Spaces:
Runtime error

File size: 9,737 Bytes
2a421b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "tensor.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <time.h>

/* ─────────────────────────────────────────
   LIFECYCLE
   ───────────────────────────────────────── */

Tensor tensor_create(int rows, int cols) {
    Tensor t;
    t.rows = rows;
    t.cols = cols;
    t.data = (float*)malloc(rows * cols * sizeof(float));
    if (!t.data) {
        fprintf(stderr, "[TENSOR] malloc failed for %dx%d\n", rows, cols);
        exit(1);
    }
    return t;
}

Tensor tensor_zeros(int rows, int cols) {
    Tensor t = tensor_create(rows, cols);
    memset(t.data, 0, rows * cols * sizeof(float));
    return t;
}

Tensor tensor_copy(const Tensor* t) {
    Tensor c = tensor_create(t->rows, t->cols);
    memcpy(c.data, t->data, t->rows * t->cols * sizeof(float));
    return c;
}

void tensor_free(Tensor* t) {
    if (t->data) {
        free(t->data);
        t->data = NULL;
    }
    t->rows = 0;
    t->cols = 0;
}

/* ─────────────────────────────────────────
   RANDOM β€” Box-Muller gaussian
   ───────────────────────────────────────── */

static float randn_single(void) {
    static int has_spare = 0;
    static float spare;
    if (has_spare) { has_spare = 0; return spare; }
    float u, v, s;
    do {
        u = ((float)rand() / RAND_MAX) * 2.0f - 1.0f;
        v = ((float)rand() / RAND_MAX) * 2.0f - 1.0f;
        s = u * u + v * v;
    } while (s >= 1.0f || s == 0.0f);
    float mul = sqrtf(-2.0f * logf(s) / s);
    spare = v * mul;
    has_spare = 1;
    return u * mul;
}

void tensor_fill(Tensor* t, float val) {
    int n = t->rows * t->cols;
    for (int i = 0; i < n; i++) t->data[i] = val;
}

void tensor_randn(Tensor* t, float std) {
    int n = t->rows * t->cols;
    for (int i = 0; i < n; i++) t->data[i] = randn_single() * std;
}

void tensor_xavier(Tensor* t) {
    float std = sqrtf(2.0f / (float)(t->rows + t->cols));
    tensor_randn(t, std);
}

/* ─────────────────────────────────────────
   ELEMENTWISE OPS
   ───────────────────────────────────────── */

void tensor_add(Tensor* dst, const Tensor* a, const Tensor* b) {
    int n = a->rows * a->cols;
    for (int i = 0; i < n; i++) dst->data[i] = a->data[i] + b->data[i];
}

void tensor_add_inplace(Tensor* a, const Tensor* b) {
    int n = a->rows * a->cols;
    for (int i = 0; i < n; i++) a->data[i] += b->data[i];
}

void tensor_sub(Tensor* dst, const Tensor* a, const Tensor* b) {
    int n = a->rows * a->cols;
    for (int i = 0; i < n; i++) dst->data[i] = a->data[i] - b->data[i];
}

void tensor_mul_scalar(Tensor* t, float s) {
    int n = t->rows * t->cols;
    for (int i = 0; i < n; i++) t->data[i] *= s;
}

void tensor_add_scalar(Tensor* t, float s) {
    int n = t->rows * t->cols;
    for (int i = 0; i < n; i++) t->data[i] += s;
}

void tensor_elementwise_mul(Tensor* dst, const Tensor* a, const Tensor* b) {
    int n = a->rows * a->cols;
    for (int i = 0; i < n; i++) dst->data[i] = a->data[i] * b->data[i];
}

/* ─────────────────────────────────────────
   MATRIX OPS
   ───────────────────────────────────────── */

/* dst = a @ b   (a: MxK, b: KxN, dst: MxN) */
void tensor_matmul(Tensor* dst, const Tensor* a, const Tensor* b) {
    int M = a->rows, K = a->cols, N = b->cols;
    memset(dst->data, 0, M * N * sizeof(float));
    for (int i = 0; i < M; i++) {
        for (int k = 0; k < K; k++) {
            float aik = a->data[i * K + k];
            for (int j = 0; j < N; j++) {
                dst->data[i * N + j] += aik * b->data[k * N + j];
            }
        }
    }
}

/* dst = a @ b.T   (a: MxK, b: NxK, dst: MxN) */
void tensor_matmul_transB(Tensor* dst, const Tensor* a, const Tensor* b) {
    int M = a->rows, K = a->cols, N = b->rows;
    memset(dst->data, 0, M * N * sizeof(float));
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            float sum = 0.0f;
            for (int k = 0; k < K; k++) {
                sum += a->data[i * K + k] * b->data[j * K + k];
            }
            dst->data[i * N + j] = sum;
        }
    }
}

void tensor_transpose(Tensor* dst, const Tensor* a) {
    for (int i = 0; i < a->rows; i++)
        for (int j = 0; j < a->cols; j++)
            dst->data[j * a->rows + i] = a->data[i * a->cols + j];
}

/* ─────────────────────────────────────────
   ACTIVATIONS
   ───────────────────────────────────────── */

void tensor_relu(Tensor* dst, const Tensor* src) {
    int n = src->rows * src->cols;
    for (int i = 0; i < n; i++)
        dst->data[i] = src->data[i] > 0.0f ? src->data[i] : 0.0f;
}

void tensor_relu_inplace(Tensor* t) {
    int n = t->rows * t->cols;
    for (int i = 0; i < n; i++)
        if (t->data[i] < 0.0f) t->data[i] = 0.0f;
}

/* GELU approximation: x * 0.5 * (1 + tanh(sqrt(2/pi)*(x + 0.044715*x^3))) */
void tensor_gelu_inplace(Tensor* t) {
    int n = t->rows * t->cols;
    for (int i = 0; i < n; i++) {
        float x = t->data[i];
        float inner = 0.7978845608f * (x + 0.044715f * x * x * x);
        t->data[i] = 0.5f * x * (1.0f + tanhf(inner));
    }
}

void tensor_gelu(Tensor* dst, const Tensor* src) {
    int n = src->rows * src->cols;
    for (int i = 0; i < n; i++) {
        float x = src->data[i];
        float inner = 0.7978845608f * (x + 0.044715f * x * x * x);
        dst->data[i] = 0.5f * x * (1.0f + tanhf(inner));
    }
}

void tensor_softmax_rows(Tensor* t) {
    for (int i = 0; i < t->rows; i++) {
        float* row = t->data + i * t->cols;
        float max_val = row[0];
        for (int j = 1; j < t->cols; j++)
            if (row[j] > max_val) max_val = row[j];
        float sum = 0.0f;
        for (int j = 0; j < t->cols; j++) {
            row[j] = expf(row[j] - max_val);
            sum += row[j];
        }
        for (int j = 0; j < t->cols; j++) row[j] /= sum;
    }
}

void tensor_softmax_inplace(Tensor* t, int len) {
    float max_val = t->data[0];
    for (int i = 1; i < len; i++)
        if (t->data[i] > max_val) max_val = t->data[i];
    float sum = 0.0f;
    for (int i = 0; i < len; i++) {
        t->data[i] = expf(t->data[i] - max_val);
        sum += t->data[i];
    }
    for (int i = 0; i < len; i++) t->data[i] /= sum;
}

/* ─────────────────────────────────────────
   LAYER NORM
   ───────────────────────────────────────── */

void tensor_layernorm(Tensor* dst, const Tensor* src,
                      const Tensor* w, const Tensor* b,
                      int row, float eps) {
    int D = src->cols;
    float* x   = src->data + row * D;
    float* out = dst->data + row * D;

    /* mean */
    float mean = 0.0f;
    for (int i = 0; i < D; i++) mean += x[i];
    mean /= D;

    /* variance */
    float var = 0.0f;
    for (int i = 0; i < D; i++) {
        float diff = x[i] - mean;
        var += diff * diff;
    }
    var /= D;

    float inv_std = 1.0f / sqrtf(var + eps);
    for (int i = 0; i < D; i++)
        out[i] = (x[i] - mean) * inv_std * w->data[i] + b->data[i];
}

/* ─────────────────────────────────────────
   HELPERS
   ───────────────────────────────────────── */

float tensor_get(const Tensor* t, int row, int col) {
    return t->data[row * t->cols + col];
}

void tensor_set(Tensor* t, int row, int col, float val) {
    t->data[row * t->cols + col] = val;
}

void tensor_add_to(Tensor* t, int row, int col, float val) {
    t->data[row * t->cols + col] += val;
}

void tensor_embed(Tensor* dst, const Tensor* embed,
                  const int* token_ids, int n_tokens) {
    int D = embed->cols;
    for (int i = 0; i < n_tokens; i++) {
        int id = token_ids[i];
        memcpy(dst->data + i * D,
               embed->data + id * D,
               D * sizeof(float));
    }
}

void tensor_add_row(Tensor* a, int a_row, const Tensor* b, int b_row) {
    int D = a->cols;
    float* ap = a->data + a_row * D;
    float* bp = b->data + b_row * D;
    for (int i = 0; i < D; i++) ap[i] += bp[i];
}

void tensor_clip(Tensor* t, float min_val, float max_val) {
    int n = t->rows * t->cols;
    for (int i = 0; i < n; i++) {
        if (t->data[i] < min_val) t->data[i] = min_val;
        if (t->data[i] > max_val) t->data[i] = max_val;
    }
}

void tensor_print(const Tensor* t, const char* name, int max_rows) {
    int rows = t->rows < max_rows ? t->rows : max_rows;
    printf("[%s] shape=(%d,%d)\n", name, t->rows, t->cols);
    for (int i = 0; i < rows; i++) {
        printf("  row%d: ", i);
        int cols = t->cols < 8 ? t->cols : 8;
        for (int j = 0; j < cols; j++)
            printf("%.4f ", t->data[i * t->cols + j]);
        if (t->cols > 8) printf("...");
        printf("\n");
    }
}