tinyblocks-tensor: tensores com gradiente automatico como blocos

Tensores de float (ate 6 eixos) viajam como bloco-handle; cada bloco
produtor e dono de um handle e troca o tensor a cada avaliacao. Contas
com broadcasting, matmul em lote (GEMM com AVX2 escolhido em execucao e
pool de threads), forma, fatias, juncao, indices, reducoes, imagem,
bytes de arquivo e .tbt. Autograd por grafo com contagem de referencias;
parametros por dono e por grupo, salvar/carregar. Outras libraries usam
a tabela TbTensorApi do lib_userdata, sem linkar.

test-tensor confere cada gradiente por diferenca finita. O "escreva"
passa a usar as classes de valor (tensor aparecia como "resultado").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-23 23:03:07 -03:00
1 parent 3dd132d12d
commit 520cb10f08
11 files changed
+2586 -25

No files matched your search

@@ -0,0 +1,78 @@
/*
* TinyBlocks - tensor_internal.h
* Funcoes internas da tinyblocks-tensor, divididas entre tensor.c (nucleo),
* ops.c (operacoes com gradiente) e blocks.c (blocos e a tabela TbTensorApi).
*/
#ifndef TB_TENSOR_INTERNAL_H
#define TB_TENSOR_INTERNAL_H
#include "tb_tensor.h"
/* -- Nucleo (tensor.c) -- */
const char* tt_last_error(void);
void tt_set_error(const char* fmt, ...) __attribute__((format(printf, 1, 2)));
TbTensor* tt_alloc(int nd, const int* shape);
TbTensor* tt_alloc_like(const TbTensor* a);
TbTensor* tt_scalar(float v);
void tt_retain(TbTensor* t);
void tt_release(TbTensor* t);
float* tt_grad(TbTensor* t);
int tt_shape_text(const TbTensor* t, char* out, size_t cap);
void tt_seed(uint32_t seed);
float tt_randu(void);
float tt_randn(void);
void tt_pool_start(void);
void tt_pool_stop(void);
int tt_pool_size(void);
void tt_parallel_for(int n, TbParallelFn fn, void* ctx);
void tt_gemm(bool ta, bool tb, int m, int n, int k, float alpha,
const float* A, int lda, const float* B, int ldb,
float beta, float* C, int ldc);
bool tt_grad_enabled(void);
void tt_set_grad_enabled(bool on);
void tt_link(TbTensor* out, TbBackwardFn bw, TbTensor* const* in, int n,
void* ctx, void (*ctx_free)(void*));
bool tt_backward(TbTensor* root);
TbTensor* tt_param(TinyBlocks_Block* owner, int slot, int nd, const int* shape, int init, float scale);
void tt_params_release(TinyBlocks_Block* owner);
int tt_param_count(void);
TbTensor* tt_param_at(int i);
const char* tt_param_group(int i);
void tt_set_param_group(const char* name);
void tt_params_zero_grad(const char* group); /* NULL ou "" = todos */
size_t tt_params_numel(void);
bool tt_params_save(const char* path);
bool tt_params_load(const char* path);
void tt_params_reset(void);
bool tt_save(const char* path, TbTensor* const* ts, int n);
TbTensor** tt_load(const char* path, int* n);
void tt_mkdirs_for(const char* path);
/* -- Operacoes (ops.c): ref nova ou NULL + erro -- */
enum { TT_ADD, TT_SUB, TT_MUL, TT_DIV };
enum { TT_EXP, TT_LOG, TT_SQRT, TT_ABS };
TbTensor* tt_binary(TbTensor* a, TbTensor* b, int op);
TbTensor* tt_add(TbTensor* a, TbTensor* b);
TbTensor* tt_sub(TbTensor* a, TbTensor* b);
TbTensor* tt_mul(TbTensor* a, TbTensor* b);
TbTensor* tt_div(TbTensor* a, TbTensor* b);
TbTensor* tt_scale(TbTensor* a, float k);
TbTensor* tt_powk(TbTensor* a, float p);
TbTensor* tt_unary(TbTensor* a, int op);
TbTensor* tt_matmul(TbTensor* a, TbTensor* b);
TbTensor* tt_reshape(TbTensor* a, int nd, const int* shape);
TbTensor* tt_transpose(TbTensor* a, int ax1, int ax2);
TbTensor* tt_slice(TbTensor* a, int axis, int from, int to);
TbTensor* tt_concat(TbTensor* a, TbTensor* b, int axis);
TbTensor* tt_sum(TbTensor* a, int axis, bool keep);
TbTensor* tt_mean(TbTensor* a, int axis, bool keep);
TbTensor* tt_max(TbTensor* a, int axis, bool keep);
TbTensor* tt_argmax(TbTensor* a, int axis); /* posições 1..n, sem gradiente */
TbTensor* tt_gather(TbTensor* a, TbTensor* idx); /* linhas idx (1..N) do eixo 0 */
TbTensor* tt_where_eq(TbTensor* a, float v); /* posições (1..n) onde a == v */
TbTensor* tt_flip(TbTensor* a, int axis);
TbTensor* tt_detach(TbTensor* a); /* cópia fora do grafo */
TbTensor* tt_flatten(TbTensor* a); /* [B, resto] */
TbTensor* tt_permutation(int n); /* 1..n embaralhados */
#endif /* TB_TENSOR_INTERNAL_H */