tensorc 0.10.0__tar.gz → 0.12.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.12.0}/PKG-INFO +1 -1
- {tensorc-0.10.0 → tensorc-0.12.0}/pyproject.toml +1 -1
- {tensorc-0.10.0 → tensorc-0.12.0}/tensorc/__init__.py +4 -3
- {tensorc-0.10.0 → tensorc-0.12.0}/tensorc/_tensorc.c +291 -0
- {tensorc-0.10.0 → tensorc-0.12.0}/tensorc/core/include/tensorc/ndtensor.h +56 -0
- {tensorc-0.10.0 → tensorc-0.12.0}/tensorc/core/include/tensorc/tensorc.h +1 -1
- {tensorc-0.10.0 → tensorc-0.12.0}/tensorc/core/ndtensor.c +753 -37
- {tensorc-0.10.0 → tensorc-0.12.0/tensorc.egg-info}/PKG-INFO +1 -1
- {tensorc-0.10.0 → tensorc-0.12.0}/MANIFEST.in +0 -0
- {tensorc-0.10.0 → tensorc-0.12.0}/README.md +0 -0
- {tensorc-0.10.0 → tensorc-0.12.0}/setup.cfg +0 -0
- {tensorc-0.10.0 → tensorc-0.12.0}/setup.py +0 -0
- {tensorc-0.10.0 → tensorc-0.12.0}/sync_core.sh +0 -0
- {tensorc-0.10.0 → tensorc-0.12.0}/tensorc/core/minitf.c +0 -0
- {tensorc-0.10.0 → tensorc-0.12.0}/tensorc/core/minitf.h +0 -0
- {tensorc-0.10.0 → tensorc-0.12.0}/tensorc.egg-info/SOURCES.txt +0 -0
- {tensorc-0.10.0 → tensorc-0.12.0}/tensorc.egg-info/dependency_links.txt +0 -0
- {tensorc-0.10.0 → tensorc-0.12.0}/tensorc.egg-info/top_level.txt +0 -0
|
@@ -5,8 +5,9 @@ extension that wraps it. Tensors keep their parents alive automatically,
|
|
|
5
5
|
so ``backward()`` can be called from any scalar loss.
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
|
-
from ._tensorc import Adam, Conv2DLayer, Tensor
|
|
8
|
+
from ._tensorc import Adam, Conv2DLayer, DenseLayer, LSTMLayer, Tensor
|
|
9
9
|
|
|
10
|
-
__version__ = "0.
|
|
10
|
+
__version__ = "0.12.0"
|
|
11
11
|
|
|
12
|
-
__all__ = ["Tensor", "Conv2DLayer", "
|
|
12
|
+
__all__ = ["Tensor", "Conv2DLayer", "DenseLayer", "LSTMLayer", "Adam",
|
|
13
|
+
"__version__"]
|
|
@@ -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,91 @@ 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
|
+
|
|
334
429
|
static PyMethodDef tensor_methods[] = {
|
|
335
430
|
{"zeros", (PyCFunction)tensor_zeros, METH_VARARGS | METH_KEYWORDS | METH_CLASS,
|
|
336
431
|
"Tensor.zeros(shape, requires_grad=False)"},
|
|
@@ -342,6 +437,9 @@ static PyMethodDef tensor_methods[] = {
|
|
|
342
437
|
{"mul", (PyCFunction)tensor_mul, METH_VARARGS, "Elementwise multiply."},
|
|
343
438
|
{"matmul", (PyCFunction)tensor_matmul, METH_VARARGS, "Matrix multiply."},
|
|
344
439
|
{"relu", (PyCFunction)tensor_relu, METH_NOARGS, "ReLU."},
|
|
440
|
+
{"sigmoid", (PyCFunction)tensor_sigmoid, METH_NOARGS,
|
|
441
|
+
"Sigmoid (numerically stable)."},
|
|
442
|
+
{"tanh", (PyCFunction)tensor_tanh, METH_NOARGS, "Hyperbolic tangent."},
|
|
345
443
|
{"softmax", (PyCFunction)tensor_softmax, METH_NOARGS,
|
|
346
444
|
"Softmax over the last dimension."},
|
|
347
445
|
{"sum", (PyCFunction)tensor_sum, METH_NOARGS, "Sum of all elements."},
|
|
@@ -353,6 +451,17 @@ static PyMethodDef tensor_methods[] = {
|
|
|
353
451
|
{"reshape", (PyCFunction)tensor_reshape, METH_VARARGS, "reshape(shape)"},
|
|
354
452
|
{"conv2d", (PyCFunction)tensor_conv2d, METH_VARARGS | METH_KEYWORDS,
|
|
355
453
|
"conv2d(kernel, stride=(1,1), padding=(0,0))"},
|
|
454
|
+
{"slice_axis", (PyCFunction)tensor_slice_axis, METH_VARARGS,
|
|
455
|
+
"slice_axis(axis, start, length)"},
|
|
456
|
+
{"transpose", (PyCFunction)tensor_transpose, METH_NOARGS,
|
|
457
|
+
"Transposición de matriz [M, N] -> [N, M]."},
|
|
458
|
+
{"layer_norm", (PyCFunction)tensor_layer_norm, METH_NOARGS,
|
|
459
|
+
"LayerNorm sobre la última dimensión (sin afín)."},
|
|
460
|
+
{"concat", (PyCFunction)tensor_concat, METH_VARARGS | METH_KEYWORDS,
|
|
461
|
+
"concat(others, axis=0): others es un Tensor o secuencia de Tensor."},
|
|
462
|
+
{"attention", (PyCFunction)tensor_attention,
|
|
463
|
+
METH_VARARGS | METH_KEYWORDS,
|
|
464
|
+
"attention(key, value, causal=False) sobre [batch, seq, depth]."},
|
|
356
465
|
{NULL, NULL, 0, NULL}
|
|
357
466
|
};
|
|
358
467
|
|
|
@@ -468,6 +577,172 @@ static PyMethodDef module_methods[] = {
|
|
|
468
577
|
{NULL, NULL, 0, NULL}
|
|
469
578
|
};
|
|
470
579
|
|
|
580
|
+
/* --- Capa densa --- */
|
|
581
|
+
typedef struct {
|
|
582
|
+
PyObject_HEAD
|
|
583
|
+
tc_dense_layer *layer;
|
|
584
|
+
} DenseLayerObject;
|
|
585
|
+
|
|
586
|
+
static PyTypeObject DenseLayerType;
|
|
587
|
+
|
|
588
|
+
static void dense_layer_dealloc(DenseLayerObject *self) {
|
|
589
|
+
tc_dense_layer_free(self->layer);
|
|
590
|
+
PyObject_Del(self);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
static PyObject *dense_layer_weight(DenseLayerObject *self, void *closure) {
|
|
594
|
+
(void)closure;
|
|
595
|
+
tc_tensor_nd_retain(tc_dense_layer_weight(self->layer));
|
|
596
|
+
return (PyObject *)tensor_wrap(tc_dense_layer_weight(self->layer));
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
static PyObject *dense_layer_bias(DenseLayerObject *self, void *closure) {
|
|
600
|
+
(void)closure;
|
|
601
|
+
tc_tensor_nd_retain(tc_dense_layer_bias(self->layer));
|
|
602
|
+
return (PyObject *)tensor_wrap(tc_dense_layer_bias(self->layer));
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
static PyObject *dense_layer_forward(DenseLayerObject *self, PyObject *args) {
|
|
606
|
+
PyObject *input_obj;
|
|
607
|
+
if (!PyArg_ParseTuple(args, "O", &input_obj)) return NULL;
|
|
608
|
+
tc_tensor_nd *input = as_tensor(input_obj);
|
|
609
|
+
if (!input) return NULL;
|
|
610
|
+
return (PyObject *)tensor_wrap(
|
|
611
|
+
tc_dense_layer_forward(self->layer, input));
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
static PyObject *dense_layer_new(PyTypeObject *type, PyObject *args,
|
|
615
|
+
PyObject *kwargs) {
|
|
616
|
+
static char *names[] = {"in_features", "out_features", "seed", NULL};
|
|
617
|
+
unsigned long in_features, out_features, seed = 1;
|
|
618
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "kk|k", names, &in_features,
|
|
619
|
+
&out_features, &seed))
|
|
620
|
+
return NULL;
|
|
621
|
+
DenseLayerObject *obj = PyObject_New(DenseLayerObject, &DenseLayerType);
|
|
622
|
+
if (!obj) return NULL;
|
|
623
|
+
obj->layer = tc_dense_layer_create(in_features, out_features,
|
|
624
|
+
(unsigned int)seed);
|
|
625
|
+
if (!obj->layer) {
|
|
626
|
+
PyObject_Del(obj);
|
|
627
|
+
PyErr_SetString(PyExc_RuntimeError,
|
|
628
|
+
"no se pudo crear la capa densa");
|
|
629
|
+
return NULL;
|
|
630
|
+
}
|
|
631
|
+
return (PyObject *)obj;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
static PyMethodDef dense_layer_methods[] = {
|
|
635
|
+
{"forward", (PyCFunction)dense_layer_forward, METH_VARARGS,
|
|
636
|
+
"forward(input) -> matmul + bias"},
|
|
637
|
+
{NULL, NULL, 0, NULL}
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
static PyGetSetDef dense_layer_getset[] = {
|
|
641
|
+
{"weight", (getter)dense_layer_weight, NULL, "Trainable weight tensor.",
|
|
642
|
+
NULL},
|
|
643
|
+
{"bias", (getter)dense_layer_bias, NULL, "Trainable bias tensor.", NULL},
|
|
644
|
+
{NULL, NULL, NULL, NULL, NULL}
|
|
645
|
+
};
|
|
646
|
+
|
|
647
|
+
static PyTypeObject DenseLayerType = {
|
|
648
|
+
PyVarObject_HEAD_INIT(NULL, 0)
|
|
649
|
+
.tp_name = "tensorc.DenseLayer",
|
|
650
|
+
.tp_basicsize = sizeof(DenseLayerObject),
|
|
651
|
+
.tp_dealloc = (destructor)dense_layer_dealloc,
|
|
652
|
+
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
653
|
+
.tp_methods = dense_layer_methods,
|
|
654
|
+
.tp_getset = dense_layer_getset,
|
|
655
|
+
.tp_new = dense_layer_new,
|
|
656
|
+
.tp_doc = "Trainable fully-connected layer.",
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
/* --- Capa LSTM --- */
|
|
660
|
+
typedef struct {
|
|
661
|
+
PyObject_HEAD
|
|
662
|
+
tc_lstm_layer *layer;
|
|
663
|
+
} LSTMLayerObject;
|
|
664
|
+
|
|
665
|
+
static PyTypeObject LSTMLayerType;
|
|
666
|
+
|
|
667
|
+
static void lstm_layer_dealloc(LSTMLayerObject *self) {
|
|
668
|
+
tc_lstm_layer_free(self->layer);
|
|
669
|
+
PyObject_Del(self);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
static PyObject *lstm_layer_weight_ih(LSTMLayerObject *self, void *closure) {
|
|
673
|
+
(void)closure;
|
|
674
|
+
tc_tensor_nd_retain(tc_lstm_layer_weight_ih(self->layer));
|
|
675
|
+
return (PyObject *)tensor_wrap(tc_lstm_layer_weight_ih(self->layer));
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
static PyObject *lstm_layer_weight_hh(LSTMLayerObject *self, void *closure) {
|
|
679
|
+
(void)closure;
|
|
680
|
+
tc_tensor_nd_retain(tc_lstm_layer_weight_hh(self->layer));
|
|
681
|
+
return (PyObject *)tensor_wrap(tc_lstm_layer_weight_hh(self->layer));
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
static PyObject *lstm_layer_bias(LSTMLayerObject *self, void *closure) {
|
|
685
|
+
(void)closure;
|
|
686
|
+
tc_tensor_nd_retain(tc_lstm_layer_bias(self->layer));
|
|
687
|
+
return (PyObject *)tensor_wrap(tc_lstm_layer_bias(self->layer));
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
/* forward(x): x es [seq, batch, in] (time-major); devuelve [seq, batch, hidden]. */
|
|
691
|
+
static PyObject *lstm_layer_forward(LSTMLayerObject *self, PyObject *args) {
|
|
692
|
+
PyObject *input_obj;
|
|
693
|
+
if (!PyArg_ParseTuple(args, "O", &input_obj)) return NULL;
|
|
694
|
+
tc_tensor_nd *input = as_tensor(input_obj);
|
|
695
|
+
if (!input) return NULL;
|
|
696
|
+
return (PyObject *)tensor_wrap(tc_lstm_layer_forward(self->layer, input));
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
static PyObject *lstm_layer_new(PyTypeObject *type, PyObject *args,
|
|
700
|
+
PyObject *kwargs) {
|
|
701
|
+
static char *names[] = {"in_features", "hidden", "seed", NULL};
|
|
702
|
+
unsigned long in_features, hidden, seed = 1;
|
|
703
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "kk|k", names, &in_features,
|
|
704
|
+
&hidden, &seed))
|
|
705
|
+
return NULL;
|
|
706
|
+
LSTMLayerObject *obj = PyObject_New(LSTMLayerObject, &LSTMLayerType);
|
|
707
|
+
if (!obj) return NULL;
|
|
708
|
+
obj->layer = tc_lstm_layer_create(in_features, hidden, (unsigned int)seed);
|
|
709
|
+
if (!obj->layer) {
|
|
710
|
+
PyObject_Del(obj);
|
|
711
|
+
PyErr_SetString(PyExc_RuntimeError, "no se pudo crear la capa LSTM");
|
|
712
|
+
return NULL;
|
|
713
|
+
}
|
|
714
|
+
return (PyObject *)obj;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
static PyMethodDef lstm_layer_methods[] = {
|
|
718
|
+
{"forward", (PyCFunction)lstm_layer_forward, METH_VARARGS,
|
|
719
|
+
"forward(x) con x [seq, batch, in] -> [seq, batch, hidden]"},
|
|
720
|
+
{NULL, NULL, 0, NULL}
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
static PyGetSetDef lstm_layer_getset[] = {
|
|
724
|
+
{"weight_ih", (getter)lstm_layer_weight_ih, NULL,
|
|
725
|
+
"Pesos entrada->puertas [4*hidden, in].", NULL},
|
|
726
|
+
{"weight_hh", (getter)lstm_layer_weight_hh, NULL,
|
|
727
|
+
"Pesos oculta->puertas [4*hidden, hidden].", NULL},
|
|
728
|
+
{"bias", (getter)lstm_layer_bias, NULL, "Sesgo de puertas [1, 4*hidden].",
|
|
729
|
+
NULL},
|
|
730
|
+
{NULL, NULL, NULL, NULL, NULL}
|
|
731
|
+
};
|
|
732
|
+
|
|
733
|
+
static PyTypeObject LSTMLayerType = {
|
|
734
|
+
PyVarObject_HEAD_INIT(NULL, 0)
|
|
735
|
+
.tp_name = "tensorc.LSTMLayer",
|
|
736
|
+
.tp_basicsize = sizeof(LSTMLayerObject),
|
|
737
|
+
.tp_dealloc = (destructor)lstm_layer_dealloc,
|
|
738
|
+
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
739
|
+
.tp_methods = lstm_layer_methods,
|
|
740
|
+
.tp_getset = lstm_layer_getset,
|
|
741
|
+
.tp_new = lstm_layer_new,
|
|
742
|
+
.tp_doc = "LSTM sobre una secuencia completa [seq, batch, in] "
|
|
743
|
+
"(time-major, h0/c0 en cero).",
|
|
744
|
+
};
|
|
745
|
+
|
|
471
746
|
/* --- Optimizador Adam --- */
|
|
472
747
|
typedef struct {
|
|
473
748
|
PyObject_HEAD
|
|
@@ -558,6 +833,8 @@ static struct PyModuleDef tensorc_module = {
|
|
|
558
833
|
PyMODINIT_FUNC PyInit__tensorc(void) {
|
|
559
834
|
if (PyType_Ready(&TensorType) < 0) return NULL;
|
|
560
835
|
if (PyType_Ready(&LayerType) < 0) return NULL;
|
|
836
|
+
if (PyType_Ready(&DenseLayerType) < 0) return NULL;
|
|
837
|
+
if (PyType_Ready(&LSTMLayerType) < 0) return NULL;
|
|
561
838
|
if (PyType_Ready(&OptimizerType) < 0) return NULL;
|
|
562
839
|
PyObject *module = PyModule_Create(&tensorc_module);
|
|
563
840
|
if (!module) return NULL;
|
|
@@ -573,6 +850,20 @@ PyMODINIT_FUNC PyInit__tensorc(void) {
|
|
|
573
850
|
Py_DECREF(module);
|
|
574
851
|
return NULL;
|
|
575
852
|
}
|
|
853
|
+
Py_INCREF(&DenseLayerType);
|
|
854
|
+
if (PyModule_AddObject(module, "DenseLayer",
|
|
855
|
+
(PyObject *)&DenseLayerType) < 0) {
|
|
856
|
+
Py_DECREF(&DenseLayerType);
|
|
857
|
+
Py_DECREF(module);
|
|
858
|
+
return NULL;
|
|
859
|
+
}
|
|
860
|
+
Py_INCREF(&LSTMLayerType);
|
|
861
|
+
if (PyModule_AddObject(module, "LSTMLayer",
|
|
862
|
+
(PyObject *)&LSTMLayerType) < 0) {
|
|
863
|
+
Py_DECREF(&LSTMLayerType);
|
|
864
|
+
Py_DECREF(module);
|
|
865
|
+
return NULL;
|
|
866
|
+
}
|
|
576
867
|
Py_INCREF(&OptimizerType);
|
|
577
868
|
if (PyModule_AddObject(module, "Adam", (PyObject *)&OptimizerType) < 0) {
|
|
578
869
|
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,25 @@ 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
|
+
|
|
93
116
|
/* Runs reverse-mode autodiff from a scalar loss. Returns 1 on success. */
|
|
94
117
|
int tc_tensor_nd_backward(tc_tensor_nd *loss);
|
|
95
118
|
void tc_tensor_nd_print(const tc_tensor_nd *tensor, const char *name);
|
|
@@ -127,6 +150,39 @@ tc_tensor_nd *tc_conv2d_layer_forward(tc_conv2d_layer *layer,
|
|
|
127
150
|
tc_tensor_nd *tc_conv2d_layer_kernel(tc_conv2d_layer *layer);
|
|
128
151
|
tc_tensor_nd *tc_conv2d_layer_bias(tc_conv2d_layer *layer);
|
|
129
152
|
|
|
153
|
+
/*
|
|
154
|
+
* High-level fully-connected layer: weight [in_features, out_features] plus
|
|
155
|
+
* bias [1, out_features], both trainable. The weight uses Xavier uniform
|
|
156
|
+
* initialization scaled by sqrt(6 / (fan_in + fan_out)); the seed makes
|
|
157
|
+
* creation deterministic. The input must be rank 2: [batch, in_features].
|
|
158
|
+
*/
|
|
159
|
+
typedef struct tc_dense_layer tc_dense_layer;
|
|
160
|
+
tc_dense_layer *tc_dense_layer_create(size_t in_features, size_t out_features,
|
|
161
|
+
unsigned int seed);
|
|
162
|
+
void tc_dense_layer_free(tc_dense_layer *layer);
|
|
163
|
+
/* Forward pass: matmul(input, weight) + bias (broadcast over rows). */
|
|
164
|
+
tc_tensor_nd *tc_dense_layer_forward(tc_dense_layer *layer,
|
|
165
|
+
const tc_tensor_nd *input);
|
|
166
|
+
tc_tensor_nd *tc_dense_layer_weight(tc_dense_layer *layer);
|
|
167
|
+
tc_tensor_nd *tc_dense_layer_bias(tc_dense_layer *layer);
|
|
168
|
+
|
|
169
|
+
/*
|
|
170
|
+
* High-level LSTM layer over a full sequence. Input is [seq, batch, in]
|
|
171
|
+
* (time-major); output is [seq, batch, hidden]. h0/c0 start at zero and
|
|
172
|
+
* the same weights are shared across time steps (gradients accumulate).
|
|
173
|
+
* Building the forward unfolds the graph, so the output retains every
|
|
174
|
+
* step: release it only after backward/training use.
|
|
175
|
+
*/
|
|
176
|
+
typedef struct tc_lstm_layer tc_lstm_layer;
|
|
177
|
+
tc_lstm_layer *tc_lstm_layer_create(size_t input_features, size_t hidden,
|
|
178
|
+
unsigned int seed);
|
|
179
|
+
void tc_lstm_layer_free(tc_lstm_layer *layer);
|
|
180
|
+
tc_tensor_nd *tc_lstm_layer_forward(tc_lstm_layer *layer,
|
|
181
|
+
const tc_tensor_nd *input);
|
|
182
|
+
tc_tensor_nd *tc_lstm_layer_weight_ih(tc_lstm_layer *layer);
|
|
183
|
+
tc_tensor_nd *tc_lstm_layer_weight_hh(tc_lstm_layer *layer);
|
|
184
|
+
tc_tensor_nd *tc_lstm_layer_bias(tc_lstm_layer *layer);
|
|
185
|
+
|
|
130
186
|
#ifdef __cplusplus
|
|
131
187
|
}
|
|
132
188
|
#endif
|