tensorc 0.10.0__tar.gz → 0.13.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {tensorc-0.10.0/tensorc.egg-info → tensorc-0.13.0}/PKG-INFO +1 -1
- {tensorc-0.10.0 → tensorc-0.13.0}/pyproject.toml +1 -1
- tensorc-0.13.0/tensorc/__init__.py +29 -0
- {tensorc-0.10.0 → tensorc-0.13.0}/tensorc/_tensorc.c +448 -0
- {tensorc-0.10.0 → tensorc-0.13.0}/tensorc/core/include/tensorc/ndtensor.h +89 -0
- {tensorc-0.10.0 → tensorc-0.13.0}/tensorc/core/include/tensorc/tensorc.h +1 -1
- tensorc-0.13.0/tensorc/core/ndtensor.c +2103 -0
- {tensorc-0.10.0 → tensorc-0.13.0/tensorc.egg-info}/PKG-INFO +1 -1
- tensorc-0.10.0/tensorc/__init__.py +0 -12
- tensorc-0.10.0/tensorc/core/ndtensor.c +0 -1078
- {tensorc-0.10.0 → tensorc-0.13.0}/MANIFEST.in +0 -0
- {tensorc-0.10.0 → tensorc-0.13.0}/README.md +0 -0
- {tensorc-0.10.0 → tensorc-0.13.0}/setup.cfg +0 -0
- {tensorc-0.10.0 → tensorc-0.13.0}/setup.py +0 -0
- {tensorc-0.10.0 → tensorc-0.13.0}/sync_core.sh +0 -0
- {tensorc-0.10.0 → tensorc-0.13.0}/tensorc/core/minitf.c +0 -0
- {tensorc-0.10.0 → tensorc-0.13.0}/tensorc/core/minitf.h +0 -0
- {tensorc-0.10.0 → tensorc-0.13.0}/tensorc.egg-info/SOURCES.txt +0 -0
- {tensorc-0.10.0 → tensorc-0.13.0}/tensorc.egg-info/dependency_links.txt +0 -0
- {tensorc-0.10.0 → tensorc-0.13.0}/tensorc.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""TensorC: neural network framework written in C11, with autograd.
|
|
2
|
+
|
|
3
|
+
The numerical core is a C library; this package is a thin CPython
|
|
4
|
+
extension that wraps it. Tensors keep their parents alive automatically,
|
|
5
|
+
so ``backward()`` can be called from any scalar loss.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from ._tensorc import (
|
|
9
|
+
Adam,
|
|
10
|
+
Conv2DLayer,
|
|
11
|
+
DenseLayer,
|
|
12
|
+
LSTMLayer,
|
|
13
|
+
Tensor,
|
|
14
|
+
TransformerBlock,
|
|
15
|
+
sinusoidal_positions,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
__version__ = "0.13.0"
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"Tensor",
|
|
22
|
+
"Conv2DLayer",
|
|
23
|
+
"DenseLayer",
|
|
24
|
+
"LSTMLayer",
|
|
25
|
+
"TransformerBlock",
|
|
26
|
+
"Adam",
|
|
27
|
+
"sinusoidal_positions",
|
|
28
|
+
"__version__",
|
|
29
|
+
]
|
|
@@ -281,6 +281,16 @@ static PyObject *tensor_relu(TensorObject *self, PyObject *args) {
|
|
|
281
281
|
return (PyObject *)tensor_wrap(tc_tensor_nd_relu(self->tensor));
|
|
282
282
|
}
|
|
283
283
|
|
|
284
|
+
static PyObject *tensor_sigmoid(TensorObject *self, PyObject *args) {
|
|
285
|
+
(void)args;
|
|
286
|
+
return (PyObject *)tensor_wrap(tc_tensor_nd_sigmoid(self->tensor));
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
static PyObject *tensor_tanh(TensorObject *self, PyObject *args) {
|
|
290
|
+
(void)args;
|
|
291
|
+
return (PyObject *)tensor_wrap(tc_tensor_nd_tanh(self->tensor));
|
|
292
|
+
}
|
|
293
|
+
|
|
284
294
|
static PyObject *tensor_softmax(TensorObject *self, PyObject *args) {
|
|
285
295
|
(void)args;
|
|
286
296
|
return (PyObject *)tensor_wrap(tc_tensor_nd_softmax(self->tensor));
|
|
@@ -331,6 +341,134 @@ static PyObject *tensor_conv2d(TensorObject *self, PyObject *args,
|
|
|
331
341
|
tc_tensor_nd_conv2d_ex(self->tensor, kernel, options));
|
|
332
342
|
}
|
|
333
343
|
|
|
344
|
+
static PyObject *tensor_slice_axis(TensorObject *self, PyObject *args) {
|
|
345
|
+
size_t axis, start, length;
|
|
346
|
+
if (!PyArg_ParseTuple(args, "nnn", &axis, &start, &length)) return NULL;
|
|
347
|
+
return (PyObject *)tensor_wrap(
|
|
348
|
+
tc_tensor_nd_slice_axis(self->tensor, axis, start, length));
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
static PyObject *tensor_transpose(TensorObject *self, PyObject *args) {
|
|
352
|
+
(void)args;
|
|
353
|
+
return (PyObject *)tensor_wrap(tc_tensor_nd_transpose(self->tensor));
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
static PyObject *tensor_layer_norm(TensorObject *self, PyObject *args) {
|
|
357
|
+
(void)args;
|
|
358
|
+
return (PyObject *)tensor_wrap(tc_tensor_nd_layer_norm(self->tensor));
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/* concat(others, axis=0): others es un Tensor o una secuencia de Tensor.
|
|
362
|
+
self se concatena primero, seguido de los elementos de others. */
|
|
363
|
+
static PyObject *tensor_concat(TensorObject *self, PyObject *args,
|
|
364
|
+
PyObject *kwargs) {
|
|
365
|
+
static char *names[] = {"others", "axis", NULL};
|
|
366
|
+
PyObject *others_obj;
|
|
367
|
+
size_t axis = 0;
|
|
368
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|n", names, &others_obj,
|
|
369
|
+
&axis))
|
|
370
|
+
return NULL;
|
|
371
|
+
|
|
372
|
+
int is_single = PyObject_TypeCheck(others_obj, &TensorType);
|
|
373
|
+
PyObject *seq = is_single ? NULL
|
|
374
|
+
: PySequence_Fast(others_obj,
|
|
375
|
+
"others debe ser un Tensor o una secuencia");
|
|
376
|
+
const Py_ssize_t extra = is_single ? 1
|
|
377
|
+
: PySequence_Fast_GET_SIZE(seq);
|
|
378
|
+
if (!is_single && extra < 0) {
|
|
379
|
+
Py_DECREF(seq);
|
|
380
|
+
return NULL;
|
|
381
|
+
}
|
|
382
|
+
tc_tensor_nd **items = (tc_tensor_nd **)PyMem_Malloc(
|
|
383
|
+
(size_t)(extra + 1) * sizeof(*items));
|
|
384
|
+
if (!items) {
|
|
385
|
+
Py_XDECREF(seq);
|
|
386
|
+
return PyErr_NoMemory();
|
|
387
|
+
}
|
|
388
|
+
items[0] = self->tensor;
|
|
389
|
+
if (is_single) {
|
|
390
|
+
items[1] = as_tensor(others_obj);
|
|
391
|
+
if (!items[1]) {
|
|
392
|
+
PyMem_Free(items);
|
|
393
|
+
return NULL;
|
|
394
|
+
}
|
|
395
|
+
} else {
|
|
396
|
+
for (Py_ssize_t i = 0; i < extra; ++i) {
|
|
397
|
+
items[i + 1] = as_tensor(PySequence_Fast_GET_ITEM(seq, i));
|
|
398
|
+
if (!items[i + 1]) {
|
|
399
|
+
PyMem_Free(items);
|
|
400
|
+
Py_DECREF(seq);
|
|
401
|
+
return NULL;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
tc_tensor_nd *result =
|
|
406
|
+
tc_tensor_nd_concat((const tc_tensor_nd *const *)items,
|
|
407
|
+
(size_t)(extra + 1), axis);
|
|
408
|
+
PyMem_Free(items);
|
|
409
|
+
Py_XDECREF(seq);
|
|
410
|
+
return (PyObject *)tensor_wrap(result);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
static PyObject *tensor_attention(TensorObject *self, PyObject *args,
|
|
414
|
+
PyObject *kwargs) {
|
|
415
|
+
static char *names[] = {"key", "value", "causal", NULL};
|
|
416
|
+
PyObject *key_obj, *value_obj;
|
|
417
|
+
int causal = 0;
|
|
418
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|p", names, &key_obj,
|
|
419
|
+
&value_obj, &causal))
|
|
420
|
+
return NULL;
|
|
421
|
+
tc_tensor_nd *key = as_tensor(key_obj);
|
|
422
|
+
if (!key) return NULL;
|
|
423
|
+
tc_tensor_nd *value = as_tensor(value_obj);
|
|
424
|
+
if (!value) return NULL;
|
|
425
|
+
return (PyObject *)tensor_wrap(
|
|
426
|
+
tc_tensor_nd_attention(self->tensor, key, value, causal));
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
static PyObject *tensor_permute(TensorObject *self, PyObject *args) {
|
|
430
|
+
PyObject *axes_obj;
|
|
431
|
+
if (!PyArg_ParseTuple(args, "O", &axes_obj)) return NULL;
|
|
432
|
+
PyObject *seq = PySequence_Fast(axes_obj, "axes debe ser una secuencia");
|
|
433
|
+
if (!seq) return NULL;
|
|
434
|
+
const Py_ssize_t n = PySequence_Fast_GET_SIZE(seq);
|
|
435
|
+
size_t axes[8];
|
|
436
|
+
if (n < 1 || n > 8) {
|
|
437
|
+
Py_DECREF(seq);
|
|
438
|
+
PyErr_SetString(PyExc_ValueError, "axes debe tener entre 1 y 8 ejes");
|
|
439
|
+
return NULL;
|
|
440
|
+
}
|
|
441
|
+
for (Py_ssize_t i = 0; i < n; ++i) {
|
|
442
|
+
const long a = PyLong_AsLong(PySequence_Fast_GET_ITEM(seq, i));
|
|
443
|
+
if (a < 0 || PyErr_Occurred()) {
|
|
444
|
+
Py_DECREF(seq);
|
|
445
|
+
PyErr_SetString(PyExc_ValueError, "ejes inválidos en axes");
|
|
446
|
+
return NULL;
|
|
447
|
+
}
|
|
448
|
+
axes[i] = (size_t)a;
|
|
449
|
+
}
|
|
450
|
+
Py_DECREF(seq);
|
|
451
|
+
return (PyObject *)tensor_wrap(
|
|
452
|
+
tc_tensor_nd_permute(self->tensor, axes, (size_t)n));
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
static PyObject *tensor_multihead_attention(TensorObject *self, PyObject *args,
|
|
456
|
+
PyObject *kwargs) {
|
|
457
|
+
static char *names[] = {"key", "value", "heads", "causal", NULL};
|
|
458
|
+
PyObject *key_obj, *value_obj;
|
|
459
|
+
unsigned long heads;
|
|
460
|
+
int causal = 0;
|
|
461
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOk|p", names, &key_obj,
|
|
462
|
+
&value_obj, &heads, &causal))
|
|
463
|
+
return NULL;
|
|
464
|
+
tc_tensor_nd *key = as_tensor(key_obj);
|
|
465
|
+
if (!key) return NULL;
|
|
466
|
+
tc_tensor_nd *value = as_tensor(value_obj);
|
|
467
|
+
if (!value) return NULL;
|
|
468
|
+
return (PyObject *)tensor_wrap(tc_tensor_nd_multihead_attention(
|
|
469
|
+
self->tensor, key, value, (size_t)heads, causal));
|
|
470
|
+
}
|
|
471
|
+
|
|
334
472
|
static PyMethodDef tensor_methods[] = {
|
|
335
473
|
{"zeros", (PyCFunction)tensor_zeros, METH_VARARGS | METH_KEYWORDS | METH_CLASS,
|
|
336
474
|
"Tensor.zeros(shape, requires_grad=False)"},
|
|
@@ -342,6 +480,9 @@ static PyMethodDef tensor_methods[] = {
|
|
|
342
480
|
{"mul", (PyCFunction)tensor_mul, METH_VARARGS, "Elementwise multiply."},
|
|
343
481
|
{"matmul", (PyCFunction)tensor_matmul, METH_VARARGS, "Matrix multiply."},
|
|
344
482
|
{"relu", (PyCFunction)tensor_relu, METH_NOARGS, "ReLU."},
|
|
483
|
+
{"sigmoid", (PyCFunction)tensor_sigmoid, METH_NOARGS,
|
|
484
|
+
"Sigmoid (numerically stable)."},
|
|
485
|
+
{"tanh", (PyCFunction)tensor_tanh, METH_NOARGS, "Hyperbolic tangent."},
|
|
345
486
|
{"softmax", (PyCFunction)tensor_softmax, METH_NOARGS,
|
|
346
487
|
"Softmax over the last dimension."},
|
|
347
488
|
{"sum", (PyCFunction)tensor_sum, METH_NOARGS, "Sum of all elements."},
|
|
@@ -353,6 +494,22 @@ static PyMethodDef tensor_methods[] = {
|
|
|
353
494
|
{"reshape", (PyCFunction)tensor_reshape, METH_VARARGS, "reshape(shape)"},
|
|
354
495
|
{"conv2d", (PyCFunction)tensor_conv2d, METH_VARARGS | METH_KEYWORDS,
|
|
355
496
|
"conv2d(kernel, stride=(1,1), padding=(0,0))"},
|
|
497
|
+
{"slice_axis", (PyCFunction)tensor_slice_axis, METH_VARARGS,
|
|
498
|
+
"slice_axis(axis, start, length)"},
|
|
499
|
+
{"transpose", (PyCFunction)tensor_transpose, METH_NOARGS,
|
|
500
|
+
"Transposición de matriz [M, N] -> [N, M]."},
|
|
501
|
+
{"layer_norm", (PyCFunction)tensor_layer_norm, METH_NOARGS,
|
|
502
|
+
"LayerNorm sobre la última dimensión (sin afín)."},
|
|
503
|
+
{"concat", (PyCFunction)tensor_concat, METH_VARARGS | METH_KEYWORDS,
|
|
504
|
+
"concat(others, axis=0): others es un Tensor o secuencia de Tensor."},
|
|
505
|
+
{"attention", (PyCFunction)tensor_attention,
|
|
506
|
+
METH_VARARGS | METH_KEYWORDS,
|
|
507
|
+
"attention(key, value, causal=False) sobre [batch, seq, depth]."},
|
|
508
|
+
{"permute", (PyCFunction)tensor_permute, METH_VARARGS,
|
|
509
|
+
"permute(axes): out[i] = in[axes[i]] por dimensión."},
|
|
510
|
+
{"multihead_attention", (PyCFunction)tensor_multihead_attention,
|
|
511
|
+
METH_VARARGS | METH_KEYWORDS,
|
|
512
|
+
"multihead_attention(key, value, heads, causal=False) sobre [batch, seq, depth]."},
|
|
356
513
|
{NULL, NULL, 0, NULL}
|
|
357
514
|
};
|
|
358
515
|
|
|
@@ -464,10 +621,277 @@ static PyTypeObject LayerType = {
|
|
|
464
621
|
.tp_doc = "Trainable 2D convolution layer.",
|
|
465
622
|
};
|
|
466
623
|
|
|
624
|
+
/* Definida más abajo; declaración anticipada para la tabla de métodos. */
|
|
625
|
+
static PyObject *module_sinusoidal_positions(PyObject *self, PyObject *args);
|
|
626
|
+
|
|
467
627
|
static PyMethodDef module_methods[] = {
|
|
628
|
+
{"sinusoidal_positions", (PyCFunction)module_sinusoidal_positions,
|
|
629
|
+
METH_VARARGS,
|
|
630
|
+
"sinusoidal_positions(seq, depth) -> codificación posicional [seq, depth]."},
|
|
631
|
+
{NULL, NULL, 0, NULL}
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
/* --- Capa densa --- */
|
|
635
|
+
typedef struct {
|
|
636
|
+
PyObject_HEAD
|
|
637
|
+
tc_dense_layer *layer;
|
|
638
|
+
} DenseLayerObject;
|
|
639
|
+
|
|
640
|
+
static PyTypeObject DenseLayerType;
|
|
641
|
+
|
|
642
|
+
static void dense_layer_dealloc(DenseLayerObject *self) {
|
|
643
|
+
tc_dense_layer_free(self->layer);
|
|
644
|
+
PyObject_Del(self);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
static PyObject *dense_layer_weight(DenseLayerObject *self, void *closure) {
|
|
648
|
+
(void)closure;
|
|
649
|
+
tc_tensor_nd_retain(tc_dense_layer_weight(self->layer));
|
|
650
|
+
return (PyObject *)tensor_wrap(tc_dense_layer_weight(self->layer));
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
static PyObject *dense_layer_bias(DenseLayerObject *self, void *closure) {
|
|
654
|
+
(void)closure;
|
|
655
|
+
tc_tensor_nd_retain(tc_dense_layer_bias(self->layer));
|
|
656
|
+
return (PyObject *)tensor_wrap(tc_dense_layer_bias(self->layer));
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
static PyObject *dense_layer_forward(DenseLayerObject *self, PyObject *args) {
|
|
660
|
+
PyObject *input_obj;
|
|
661
|
+
if (!PyArg_ParseTuple(args, "O", &input_obj)) return NULL;
|
|
662
|
+
tc_tensor_nd *input = as_tensor(input_obj);
|
|
663
|
+
if (!input) return NULL;
|
|
664
|
+
return (PyObject *)tensor_wrap(
|
|
665
|
+
tc_dense_layer_forward(self->layer, input));
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
static PyObject *dense_layer_new(PyTypeObject *type, PyObject *args,
|
|
669
|
+
PyObject *kwargs) {
|
|
670
|
+
static char *names[] = {"in_features", "out_features", "seed", NULL};
|
|
671
|
+
unsigned long in_features, out_features, seed = 1;
|
|
672
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "kk|k", names, &in_features,
|
|
673
|
+
&out_features, &seed))
|
|
674
|
+
return NULL;
|
|
675
|
+
DenseLayerObject *obj = PyObject_New(DenseLayerObject, &DenseLayerType);
|
|
676
|
+
if (!obj) return NULL;
|
|
677
|
+
obj->layer = tc_dense_layer_create(in_features, out_features,
|
|
678
|
+
(unsigned int)seed);
|
|
679
|
+
if (!obj->layer) {
|
|
680
|
+
PyObject_Del(obj);
|
|
681
|
+
PyErr_SetString(PyExc_RuntimeError,
|
|
682
|
+
"no se pudo crear la capa densa");
|
|
683
|
+
return NULL;
|
|
684
|
+
}
|
|
685
|
+
return (PyObject *)obj;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
static PyMethodDef dense_layer_methods[] = {
|
|
689
|
+
{"forward", (PyCFunction)dense_layer_forward, METH_VARARGS,
|
|
690
|
+
"forward(input) -> matmul + bias"},
|
|
691
|
+
{NULL, NULL, 0, NULL}
|
|
692
|
+
};
|
|
693
|
+
|
|
694
|
+
static PyGetSetDef dense_layer_getset[] = {
|
|
695
|
+
{"weight", (getter)dense_layer_weight, NULL, "Trainable weight tensor.",
|
|
696
|
+
NULL},
|
|
697
|
+
{"bias", (getter)dense_layer_bias, NULL, "Trainable bias tensor.", NULL},
|
|
698
|
+
{NULL, NULL, NULL, NULL, NULL}
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
static PyTypeObject DenseLayerType = {
|
|
702
|
+
PyVarObject_HEAD_INIT(NULL, 0)
|
|
703
|
+
.tp_name = "tensorc.DenseLayer",
|
|
704
|
+
.tp_basicsize = sizeof(DenseLayerObject),
|
|
705
|
+
.tp_dealloc = (destructor)dense_layer_dealloc,
|
|
706
|
+
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
707
|
+
.tp_methods = dense_layer_methods,
|
|
708
|
+
.tp_getset = dense_layer_getset,
|
|
709
|
+
.tp_new = dense_layer_new,
|
|
710
|
+
.tp_doc = "Trainable fully-connected layer.",
|
|
711
|
+
};
|
|
712
|
+
|
|
713
|
+
/* --- Capa LSTM --- */
|
|
714
|
+
typedef struct {
|
|
715
|
+
PyObject_HEAD
|
|
716
|
+
tc_lstm_layer *layer;
|
|
717
|
+
} LSTMLayerObject;
|
|
718
|
+
|
|
719
|
+
static PyTypeObject LSTMLayerType;
|
|
720
|
+
|
|
721
|
+
static void lstm_layer_dealloc(LSTMLayerObject *self) {
|
|
722
|
+
tc_lstm_layer_free(self->layer);
|
|
723
|
+
PyObject_Del(self);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
static PyObject *lstm_layer_weight_ih(LSTMLayerObject *self, void *closure) {
|
|
727
|
+
(void)closure;
|
|
728
|
+
tc_tensor_nd_retain(tc_lstm_layer_weight_ih(self->layer));
|
|
729
|
+
return (PyObject *)tensor_wrap(tc_lstm_layer_weight_ih(self->layer));
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
static PyObject *lstm_layer_weight_hh(LSTMLayerObject *self, void *closure) {
|
|
733
|
+
(void)closure;
|
|
734
|
+
tc_tensor_nd_retain(tc_lstm_layer_weight_hh(self->layer));
|
|
735
|
+
return (PyObject *)tensor_wrap(tc_lstm_layer_weight_hh(self->layer));
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
static PyObject *lstm_layer_bias(LSTMLayerObject *self, void *closure) {
|
|
739
|
+
(void)closure;
|
|
740
|
+
tc_tensor_nd_retain(tc_lstm_layer_bias(self->layer));
|
|
741
|
+
return (PyObject *)tensor_wrap(tc_lstm_layer_bias(self->layer));
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/* forward(x): x es [seq, batch, in] (time-major); devuelve [seq, batch, hidden]. */
|
|
745
|
+
static PyObject *lstm_layer_forward(LSTMLayerObject *self, PyObject *args) {
|
|
746
|
+
PyObject *input_obj;
|
|
747
|
+
if (!PyArg_ParseTuple(args, "O", &input_obj)) return NULL;
|
|
748
|
+
tc_tensor_nd *input = as_tensor(input_obj);
|
|
749
|
+
if (!input) return NULL;
|
|
750
|
+
return (PyObject *)tensor_wrap(tc_lstm_layer_forward(self->layer, input));
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
static PyObject *lstm_layer_new(PyTypeObject *type, PyObject *args,
|
|
754
|
+
PyObject *kwargs) {
|
|
755
|
+
static char *names[] = {"in_features", "hidden", "seed", NULL};
|
|
756
|
+
unsigned long in_features, hidden, seed = 1;
|
|
757
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "kk|k", names, &in_features,
|
|
758
|
+
&hidden, &seed))
|
|
759
|
+
return NULL;
|
|
760
|
+
LSTMLayerObject *obj = PyObject_New(LSTMLayerObject, &LSTMLayerType);
|
|
761
|
+
if (!obj) return NULL;
|
|
762
|
+
obj->layer = tc_lstm_layer_create(in_features, hidden, (unsigned int)seed);
|
|
763
|
+
if (!obj->layer) {
|
|
764
|
+
PyObject_Del(obj);
|
|
765
|
+
PyErr_SetString(PyExc_RuntimeError, "no se pudo crear la capa LSTM");
|
|
766
|
+
return NULL;
|
|
767
|
+
}
|
|
768
|
+
return (PyObject *)obj;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
static PyMethodDef lstm_layer_methods[] = {
|
|
772
|
+
{"forward", (PyCFunction)lstm_layer_forward, METH_VARARGS,
|
|
773
|
+
"forward(x) con x [seq, batch, in] -> [seq, batch, hidden]"},
|
|
774
|
+
{NULL, NULL, 0, NULL}
|
|
775
|
+
};
|
|
776
|
+
|
|
777
|
+
static PyGetSetDef lstm_layer_getset[] = {
|
|
778
|
+
{"weight_ih", (getter)lstm_layer_weight_ih, NULL,
|
|
779
|
+
"Pesos entrada->puertas [4*hidden, in].", NULL},
|
|
780
|
+
{"weight_hh", (getter)lstm_layer_weight_hh, NULL,
|
|
781
|
+
"Pesos oculta->puertas [4*hidden, hidden].", NULL},
|
|
782
|
+
{"bias", (getter)lstm_layer_bias, NULL, "Sesgo de puertas [1, 4*hidden].",
|
|
783
|
+
NULL},
|
|
784
|
+
{NULL, NULL, NULL, NULL, NULL}
|
|
785
|
+
};
|
|
786
|
+
|
|
787
|
+
static PyTypeObject LSTMLayerType = {
|
|
788
|
+
PyVarObject_HEAD_INIT(NULL, 0)
|
|
789
|
+
.tp_name = "tensorc.LSTMLayer",
|
|
790
|
+
.tp_basicsize = sizeof(LSTMLayerObject),
|
|
791
|
+
.tp_dealloc = (destructor)lstm_layer_dealloc,
|
|
792
|
+
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
793
|
+
.tp_methods = lstm_layer_methods,
|
|
794
|
+
.tp_getset = lstm_layer_getset,
|
|
795
|
+
.tp_new = lstm_layer_new,
|
|
796
|
+
.tp_doc = "LSTM sobre una secuencia completa [seq, batch, in] "
|
|
797
|
+
"(time-major, h0/c0 en cero).",
|
|
798
|
+
};
|
|
799
|
+
|
|
800
|
+
/* --- Capa Transformer (bloque pre-norm) --- */
|
|
801
|
+
typedef struct {
|
|
802
|
+
PyObject_HEAD
|
|
803
|
+
tc_transformer_block *block;
|
|
804
|
+
} TransformerBlockObject;
|
|
805
|
+
|
|
806
|
+
static PyTypeObject TransformerBlockType;
|
|
807
|
+
|
|
808
|
+
static void transformer_block_dealloc(TransformerBlockObject *self) {
|
|
809
|
+
tc_transformer_block_free(self->block);
|
|
810
|
+
PyObject_Del(self);
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/* forward(x): x es [batch, seq, model_dim]; devuelve la misma forma. */
|
|
814
|
+
static PyObject *transformer_block_forward(TransformerBlockObject *self,
|
|
815
|
+
PyObject *args) {
|
|
816
|
+
PyObject *input_obj;
|
|
817
|
+
if (!PyArg_ParseTuple(args, "O", &input_obj)) return NULL;
|
|
818
|
+
tc_tensor_nd *input = as_tensor(input_obj);
|
|
819
|
+
if (!input) return NULL;
|
|
820
|
+
return (PyObject *)tensor_wrap(
|
|
821
|
+
tc_transformer_block_forward(self->block, input));
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/* params: lista de los 8 tensores entrenables (para Adam). */
|
|
825
|
+
static PyObject *transformer_block_params(TransformerBlockObject *self,
|
|
826
|
+
void *closure) {
|
|
827
|
+
(void)closure;
|
|
828
|
+
const size_t count = tc_transformer_block_param_count(self->block);
|
|
829
|
+
PyObject *list = PyList_New((Py_ssize_t)count);
|
|
830
|
+
if (!list) return NULL;
|
|
831
|
+
for (size_t i = 0; i < count; ++i) {
|
|
832
|
+
tc_tensor_nd_retain(tc_transformer_block_param(self->block, i));
|
|
833
|
+
PyList_SET_ITEM(list, (Py_ssize_t)i,
|
|
834
|
+
(PyObject *)tensor_wrap(
|
|
835
|
+
tc_transformer_block_param(self->block, i)));
|
|
836
|
+
}
|
|
837
|
+
return list;
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
static PyObject *transformer_block_new(PyTypeObject *type, PyObject *args,
|
|
841
|
+
PyObject *kwargs) {
|
|
842
|
+
static char *names[] = {"model_dim", "ff_dim", "heads", "seed", NULL};
|
|
843
|
+
unsigned long model_dim, ff_dim, heads, seed = 1;
|
|
844
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "kkk|k", names, &model_dim,
|
|
845
|
+
&ff_dim, &heads, &seed))
|
|
846
|
+
return NULL;
|
|
847
|
+
TransformerBlockObject *obj =
|
|
848
|
+
PyObject_New(TransformerBlockObject, &TransformerBlockType);
|
|
849
|
+
if (!obj) return NULL;
|
|
850
|
+
obj->block = tc_transformer_block_create(model_dim, ff_dim, heads,
|
|
851
|
+
(unsigned int)seed);
|
|
852
|
+
if (!obj->block) {
|
|
853
|
+
PyObject_Del(obj);
|
|
854
|
+
PyErr_SetString(PyExc_RuntimeError,
|
|
855
|
+
"no se pudo crear el bloque transformer "
|
|
856
|
+
"(model_dim debe ser divisible entre heads)");
|
|
857
|
+
return NULL;
|
|
858
|
+
}
|
|
859
|
+
return (PyObject *)obj;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
static PyMethodDef transformer_block_methods[] = {
|
|
863
|
+
{"forward", (PyCFunction)transformer_block_forward, METH_VARARGS,
|
|
864
|
+
"forward(x) con x [batch, seq, model_dim] -> misma forma"},
|
|
468
865
|
{NULL, NULL, 0, NULL}
|
|
469
866
|
};
|
|
470
867
|
|
|
868
|
+
static PyGetSetDef transformer_block_getset[] = {
|
|
869
|
+
{"params", (getter)transformer_block_params, NULL,
|
|
870
|
+
"Lista de los 8 tensores entrenables (para Adam).", NULL},
|
|
871
|
+
{NULL, NULL, NULL, NULL, NULL}
|
|
872
|
+
};
|
|
873
|
+
|
|
874
|
+
static PyTypeObject TransformerBlockType = {
|
|
875
|
+
PyVarObject_HEAD_INIT(NULL, 0)
|
|
876
|
+
.tp_name = "tensorc.TransformerBlock",
|
|
877
|
+
.tp_basicsize = sizeof(TransformerBlockObject),
|
|
878
|
+
.tp_dealloc = (destructor)transformer_block_dealloc,
|
|
879
|
+
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
880
|
+
.tp_methods = transformer_block_methods,
|
|
881
|
+
.tp_getset = transformer_block_getset,
|
|
882
|
+
.tp_new = transformer_block_new,
|
|
883
|
+
.tp_doc = "Bloque Transformer pre-norm (MHA + residual + FFN + residual).",
|
|
884
|
+
};
|
|
885
|
+
|
|
886
|
+
/* Codificación posicional sinusoidal [seq, depth]. */
|
|
887
|
+
static PyObject *module_sinusoidal_positions(PyObject *self, PyObject *args) {
|
|
888
|
+
(void)self;
|
|
889
|
+
unsigned long seq, depth;
|
|
890
|
+
if (!PyArg_ParseTuple(args, "kk", &seq, &depth)) return NULL;
|
|
891
|
+
return (PyObject *)tensor_wrap(
|
|
892
|
+
tc_tensor_nd_sinusoidal_positions((size_t)seq, (size_t)depth));
|
|
893
|
+
}
|
|
894
|
+
|
|
471
895
|
/* --- Optimizador Adam --- */
|
|
472
896
|
typedef struct {
|
|
473
897
|
PyObject_HEAD
|
|
@@ -558,6 +982,9 @@ static struct PyModuleDef tensorc_module = {
|
|
|
558
982
|
PyMODINIT_FUNC PyInit__tensorc(void) {
|
|
559
983
|
if (PyType_Ready(&TensorType) < 0) return NULL;
|
|
560
984
|
if (PyType_Ready(&LayerType) < 0) return NULL;
|
|
985
|
+
if (PyType_Ready(&DenseLayerType) < 0) return NULL;
|
|
986
|
+
if (PyType_Ready(&LSTMLayerType) < 0) return NULL;
|
|
987
|
+
if (PyType_Ready(&TransformerBlockType) < 0) return NULL;
|
|
561
988
|
if (PyType_Ready(&OptimizerType) < 0) return NULL;
|
|
562
989
|
PyObject *module = PyModule_Create(&tensorc_module);
|
|
563
990
|
if (!module) return NULL;
|
|
@@ -573,6 +1000,27 @@ PyMODINIT_FUNC PyInit__tensorc(void) {
|
|
|
573
1000
|
Py_DECREF(module);
|
|
574
1001
|
return NULL;
|
|
575
1002
|
}
|
|
1003
|
+
Py_INCREF(&DenseLayerType);
|
|
1004
|
+
if (PyModule_AddObject(module, "DenseLayer",
|
|
1005
|
+
(PyObject *)&DenseLayerType) < 0) {
|
|
1006
|
+
Py_DECREF(&DenseLayerType);
|
|
1007
|
+
Py_DECREF(module);
|
|
1008
|
+
return NULL;
|
|
1009
|
+
}
|
|
1010
|
+
Py_INCREF(&LSTMLayerType);
|
|
1011
|
+
if (PyModule_AddObject(module, "LSTMLayer",
|
|
1012
|
+
(PyObject *)&LSTMLayerType) < 0) {
|
|
1013
|
+
Py_DECREF(&LSTMLayerType);
|
|
1014
|
+
Py_DECREF(module);
|
|
1015
|
+
return NULL;
|
|
1016
|
+
}
|
|
1017
|
+
Py_INCREF(&TransformerBlockType);
|
|
1018
|
+
if (PyModule_AddObject(module, "TransformerBlock",
|
|
1019
|
+
(PyObject *)&TransformerBlockType) < 0) {
|
|
1020
|
+
Py_DECREF(&TransformerBlockType);
|
|
1021
|
+
Py_DECREF(module);
|
|
1022
|
+
return NULL;
|
|
1023
|
+
}
|
|
576
1024
|
Py_INCREF(&OptimizerType);
|
|
577
1025
|
if (PyModule_AddObject(module, "Adam", (PyObject *)&OptimizerType) < 0) {
|
|
578
1026
|
Py_DECREF(&OptimizerType);
|
|
@@ -68,6 +68,10 @@ tc_tensor_nd *tc_tensor_nd_conv2d_ex(const tc_tensor_nd *input,
|
|
|
68
68
|
const tc_tensor_nd *kernel,
|
|
69
69
|
tc_conv2d_options options);
|
|
70
70
|
tc_tensor_nd *tc_tensor_nd_relu(const tc_tensor_nd *input);
|
|
71
|
+
/* Elementwise sigmoid: 1 / (1 + exp(-x)), numerically stable. */
|
|
72
|
+
tc_tensor_nd *tc_tensor_nd_sigmoid(const tc_tensor_nd *input);
|
|
73
|
+
/* Elementwise hyperbolic tangent. */
|
|
74
|
+
tc_tensor_nd *tc_tensor_nd_tanh(const tc_tensor_nd *input);
|
|
71
75
|
/* Softmax over the last dimension (one distribution per row). */
|
|
72
76
|
tc_tensor_nd *tc_tensor_nd_softmax(const tc_tensor_nd *input);
|
|
73
77
|
/*
|
|
@@ -90,6 +94,39 @@ tc_tensor_nd *tc_tensor_nd_sum(const tc_tensor_nd *input);
|
|
|
90
94
|
tc_tensor_nd *tc_tensor_nd_mse_loss(const tc_tensor_nd *prediction,
|
|
91
95
|
const tc_tensor_nd *target);
|
|
92
96
|
|
|
97
|
+
/* Slice contiguo a lo largo de un eje (start + length <= shape[axis]). */
|
|
98
|
+
tc_tensor_nd *tc_tensor_nd_slice_axis(const tc_tensor_nd *input, size_t axis,
|
|
99
|
+
size_t start, size_t length);
|
|
100
|
+
/* Concatena N tensores en un eje; todas las demás dimensiones deben coincidir. */
|
|
101
|
+
tc_tensor_nd *tc_tensor_nd_concat(const tc_tensor_nd *const *inputs,
|
|
102
|
+
size_t count, size_t axis);
|
|
103
|
+
/* LayerNorm sobre la última dimensión (sin afín; componer con mul/add). */
|
|
104
|
+
tc_tensor_nd *tc_tensor_nd_layer_norm(const tc_tensor_nd *input);
|
|
105
|
+
/* Transposición de matriz [M, N] -> [N, M]. */
|
|
106
|
+
tc_tensor_nd *tc_tensor_nd_transpose(const tc_tensor_nd *input);
|
|
107
|
+
/*
|
|
108
|
+
* Atención single-head: softmax((Q Kᵀ)/sqrt(depth) + máscara) V sobre
|
|
109
|
+
* tensores [batch, seq, depth]. causal=1 enmascara posiciones futuras.
|
|
110
|
+
*/
|
|
111
|
+
tc_tensor_nd *tc_tensor_nd_attention(const tc_tensor_nd *query,
|
|
112
|
+
const tc_tensor_nd *key,
|
|
113
|
+
const tc_tensor_nd *value,
|
|
114
|
+
int causal);
|
|
115
|
+
/* Permutación general de ejes: out[i] = in[axes[i]] por dimensión. */
|
|
116
|
+
tc_tensor_nd *tc_tensor_nd_permute(const tc_tensor_nd *input,
|
|
117
|
+
const size_t *axes, size_t ndim);
|
|
118
|
+
/*
|
|
119
|
+
* Atención multi-cabeza sobre [batch, seq, depth] con `heads` cabezas
|
|
120
|
+
* (depth debe ser divisible entre heads). Compone permute + reshape +
|
|
121
|
+
* atención single-head + concat, así el autograd fluye sin nodos nuevos.
|
|
122
|
+
*/
|
|
123
|
+
tc_tensor_nd *tc_tensor_nd_multihead_attention(const tc_tensor_nd *query,
|
|
124
|
+
const tc_tensor_nd *key,
|
|
125
|
+
const tc_tensor_nd *value,
|
|
126
|
+
size_t heads, int causal);
|
|
127
|
+
/* Codificación posicional sinusoidal [seq, depth] (hoja constante). */
|
|
128
|
+
tc_tensor_nd *tc_tensor_nd_sinusoidal_positions(size_t seq, size_t depth);
|
|
129
|
+
|
|
93
130
|
/* Runs reverse-mode autodiff from a scalar loss. Returns 1 on success. */
|
|
94
131
|
int tc_tensor_nd_backward(tc_tensor_nd *loss);
|
|
95
132
|
void tc_tensor_nd_print(const tc_tensor_nd *tensor, const char *name);
|
|
@@ -127,6 +164,58 @@ tc_tensor_nd *tc_conv2d_layer_forward(tc_conv2d_layer *layer,
|
|
|
127
164
|
tc_tensor_nd *tc_conv2d_layer_kernel(tc_conv2d_layer *layer);
|
|
128
165
|
tc_tensor_nd *tc_conv2d_layer_bias(tc_conv2d_layer *layer);
|
|
129
166
|
|
|
167
|
+
/*
|
|
168
|
+
* High-level fully-connected layer: weight [in_features, out_features] plus
|
|
169
|
+
* bias [1, out_features], both trainable. The weight uses Xavier uniform
|
|
170
|
+
* initialization scaled by sqrt(6 / (fan_in + fan_out)); the seed makes
|
|
171
|
+
* creation deterministic. The input must be rank 2: [batch, in_features].
|
|
172
|
+
*/
|
|
173
|
+
typedef struct tc_dense_layer tc_dense_layer;
|
|
174
|
+
tc_dense_layer *tc_dense_layer_create(size_t in_features, size_t out_features,
|
|
175
|
+
unsigned int seed);
|
|
176
|
+
void tc_dense_layer_free(tc_dense_layer *layer);
|
|
177
|
+
/* Forward pass: matmul(input, weight) + bias (broadcast over rows). */
|
|
178
|
+
tc_tensor_nd *tc_dense_layer_forward(tc_dense_layer *layer,
|
|
179
|
+
const tc_tensor_nd *input);
|
|
180
|
+
tc_tensor_nd *tc_dense_layer_weight(tc_dense_layer *layer);
|
|
181
|
+
tc_tensor_nd *tc_dense_layer_bias(tc_dense_layer *layer);
|
|
182
|
+
|
|
183
|
+
/*
|
|
184
|
+
* High-level LSTM layer over a full sequence. Input is [seq, batch, in]
|
|
185
|
+
* (time-major); output is [seq, batch, hidden]. h0/c0 start at zero and
|
|
186
|
+
* the same weights are shared across time steps (gradients accumulate).
|
|
187
|
+
* Building the forward unfolds the graph, so the output retains every
|
|
188
|
+
* step: release it only after backward/training use.
|
|
189
|
+
*/
|
|
190
|
+
typedef struct tc_lstm_layer tc_lstm_layer;
|
|
191
|
+
tc_lstm_layer *tc_lstm_layer_create(size_t input_features, size_t hidden,
|
|
192
|
+
unsigned int seed);
|
|
193
|
+
void tc_lstm_layer_free(tc_lstm_layer *layer);
|
|
194
|
+
tc_tensor_nd *tc_lstm_layer_forward(tc_lstm_layer *layer,
|
|
195
|
+
const tc_tensor_nd *input);
|
|
196
|
+
tc_tensor_nd *tc_lstm_layer_weight_ih(tc_lstm_layer *layer);
|
|
197
|
+
tc_tensor_nd *tc_lstm_layer_weight_hh(tc_lstm_layer *layer);
|
|
198
|
+
tc_tensor_nd *tc_lstm_layer_bias(tc_lstm_layer *layer);
|
|
199
|
+
|
|
200
|
+
/*
|
|
201
|
+
* Bloque Transformer pre-norm: atención multi-cabeza con residuales y
|
|
202
|
+
* FFN (ReLU) sobre [batch, seq, model_dim] -> misma forma. Pesos:
|
|
203
|
+
* W_q/W_k/W_v/W_o [model_dim, model_dim], W1 [model_dim, ff_dim],
|
|
204
|
+
* b1 [1, ff_dim], W2 [ff_dim, model_dim], b2 [1, model_dim], Xavier
|
|
205
|
+
* determinista. Los gradientes se acumulan en los 8 parámetros.
|
|
206
|
+
*/
|
|
207
|
+
typedef struct tc_transformer_block tc_transformer_block;
|
|
208
|
+
tc_transformer_block *tc_transformer_block_create(size_t model_dim,
|
|
209
|
+
size_t ff_dim,
|
|
210
|
+
size_t heads,
|
|
211
|
+
unsigned int seed);
|
|
212
|
+
void tc_transformer_block_free(tc_transformer_block *block);
|
|
213
|
+
tc_tensor_nd *tc_transformer_block_forward(tc_transformer_block *block,
|
|
214
|
+
const tc_tensor_nd *input);
|
|
215
|
+
size_t tc_transformer_block_param_count(const tc_transformer_block *block);
|
|
216
|
+
tc_tensor_nd *tc_transformer_block_param(tc_transformer_block *block,
|
|
217
|
+
size_t index);
|
|
218
|
+
|
|
130
219
|
#ifdef __cplusplus
|
|
131
220
|
}
|
|
132
221
|
#endif
|