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

+27 -7
View File
@@ -84,6 +84,12 @@ CAT_LIBS := events control errors input operators number string variables vector
CAT_A := $(foreach l,$(CAT_LIBS),$(LIBDIR)/tinyblocks-$(l)/build/libtinyblocks-$(l).a)
CAT_LDFLAGS := $(foreach l,$(CAT_LIBS),-ltinyblocks-$(l))
CAT_INCS := $(foreach l,$(CAT_LIBS),-I$(LIBDIR)/tinyblocks-$(l)/include)
# Libraries que o editor registra como as de categoria, mas com varios
# fontes e o Makefile proprio (o harness nao as compila junto)
APP_LIBS := tensor
APP_A := $(foreach l,$(APP_LIBS),$(LIBDIR)/tinyblocks-$(l)/build/libtinyblocks-$(l).a)
APP_INCS := $(foreach l,$(APP_LIBS),-I$(LIBDIR)/tinyblocks-$(l)/include)
APP_LDLIBS := -lpthread
ED_A := $(ED_DIR)/build/libtinyblocks-editor.a
GX_A := $(GX_DIR)/build/libtinyblocks-graphics.a
SD_A := $(SD_DIR)/build/libtinyblocks-sdl2.a
@@ -105,8 +111,8 @@ LIB_LDFLAGS := -L$(ED_DIR)/build -L$(GX_DIR)/build -L$(SD_DIR)/build \
editor run-editor run-sdl2 mylib vector customappearance graphics sdl2-backend test-editor \
appdir appimage run-appimage icon \
test-toml test-plugin test-blueprint test-a11y test-mcp test-mcp-server test-base64 test-curves test-meta test-ppm test-bmp test-png test-image test-vg test-svg test-jpg \
test-syntax test-os test-value test-vars test-scratchblocks test-input test-stdlib test-regex test-markdown test-opentype \
category-libs category-sos libs \
test-tensor test-syntax test-os test-value test-vars test-scratchblocks test-input test-stdlib test-regex test-markdown test-opentype \
category-libs category-sos libs app-libs \
lib-events lib-control lib-errors lib-input lib-operators lib-number lib-string lib-variables lib-vector lib-appearance lib-pen lib-fs lib-os \
lib-environ \
lib-editor lib-graphics lib-sdl2 lib-python lib-mcp lib-toml \
@@ -167,6 +173,9 @@ lib-$(1):
$(MAKE) -C $(LIBDIR)/tinyblocks-$(1)
endef
$(foreach l,$(CAT_LIBS),$(eval $(call CAT_LIB_TARGET,$(l))))
$(foreach l,$(APP_LIBS),$(eval $(call CAT_LIB_TARGET,$(l))))
app-libs: $(foreach l,$(APP_LIBS),lib-$(l)) ## Compila tensor e as outras libraries do app (.a/.so)
lib-editor: lib-a11y lib-mcp-server
$(MAKE) -C $(ED_DIR)
@@ -252,7 +261,7 @@ lib-python:
lib-mcp: lib-python
$(MAKE) -C $(MC_DIR)
libs: static lib-events lib-control lib-errors lib-operators lib-variables \
libs: static app-libs lib-events lib-control lib-errors lib-operators lib-variables \
lib-appearance lib-pen lib-fs lib-os lib-input lib-number lib-string lib-vector lib-environ lib-graphics lib-sdl2 lib-toml \
lib-json lib-xml lib-csv lib-blueprint lib-base64 lib-curves lib-ppm lib-bmp lib-png lib-image lib-vg lib-svg lib-jpg lib-syntax lib-scratchblocks lib-regex lib-markdown lib-opentype lib-api lib-libman lib-a11y lib-mcp-server lib-editor
@echo "Libraries OK: category + graphics + sdl2 + toml + json + xml + csv + blueprint + base64 + curves + ppm + bmp + png + image + vg + svg + jpg + syntax + regex + markdown + opentype + api + libman + a11y + editor"
@@ -294,13 +303,13 @@ EDITOR_HDRS := $(ED_DIR)/include/tb_stage_widget.h $(ED_DIR)/include/tb_editor.h
$(BUILD)/editor: examples/editor/main.c static libs
$(CC) $(CFLAGS_REL) \
-I$(ED_DIR)/include -I$(GFX_INC) $(CAT_INCS) \
-I$(ED_DIR)/include -I$(GFX_INC) $(CAT_INCS) $(APP_INCS) \
-I$(FS_DIR)/include -I$(EN_DIR)/include -I$(TOML_DIR)/include \
-I$(JSON_DIR)/include -I$(XML_DIR)/include -I$(CSV_DIR)/include -I$(BP_DIR)/include \
-I$(API_DIR)/include -I$(LIBM_DIR)/include -I$(SYN_DIR)/include -I$(A11Y_DIR)/include -I$(MCPS_DIR)/include \
$< -o $@ \
-Wl,--start-group $(ED_A) $(A11Y_A) $(MCPS_A) $(GX_A) $(SD_A) $(TOML_A) $(JSON_A) $(XML_A) $(CSV_A) $(BP_A) $(SB_A) $(API_A) $(LIBM_A) $(SYN_A) $(CAT_A) $(LIB_A) -Wl,--end-group \
$(SDL2_LIBS) $(SDL2TTF_LIBS) -ldl -lm -rdynamic \
-Wl,--start-group $(ED_A) $(A11Y_A) $(MCPS_A) $(GX_A) $(SD_A) $(TOML_A) $(JSON_A) $(XML_A) $(CSV_A) $(BP_A) $(SB_A) $(API_A) $(LIBM_A) $(SYN_A) $(CAT_A) $(APP_A) $(LIB_A) -Wl,--end-group \
$(SDL2_LIBS) $(SDL2TTF_LIBS) $(APP_LDLIBS) -ldl -lm -rdynamic \
'-Wl,-rpath,$$ORIGIN'
editor: static libs $(BUILD)/editor ## Compila o editor visual (estilo Scratch)
@@ -311,7 +320,7 @@ run-editor: editor ## Compila e abre o editor visual
# ── Empacotamento AppImage ──────────────────────────────────────────────
# Libraries com blocos (categoria) que vao empacotadas como extensoes.
APPIMAGE_LIBS := $(CAT_LIBS) toml json xml csv blueprint base64 curves ppm bmp png image vg svg jpg syntax regex markdown opentype
APPIMAGE_LIBS := $(CAT_LIBS) $(APP_LIBS) toml json xml csv blueprint base64 curves ppm bmp png image vg svg jpg syntax regex markdown opentype
APP_NAME := TinyBlocks
APP_VER := 0.1.0
@@ -596,6 +605,17 @@ $(BUILD)/meta_test: tests/meta_test.c $(OP_DIR)/src/tb_operators.c static
test-meta: $(BUILD)/meta_test ## Roda o teste dos metadados de blocos (ASan/UBSan)
./$(BUILD)/meta_test
# -- Teste da library tensor (valores e gradientes por diferenca finita) ---
TENSOR_DIR := $(LIBDIR)/tinyblocks-tensor
TENSOR_SRCS := $(TENSOR_DIR)/src/tensor.c $(TENSOR_DIR)/src/ops.c $(TENSOR_DIR)/src/blocks.c
$(BUILD)/tensor_test: tests/tensor_test.c $(TENSOR_SRCS) $(TENSOR_DIR)/include/tb_tensor.h $(TENSOR_DIR)/src/tensor_internal.h static
@mkdir -p $(BUILD)
$(CC) $(CFLAGS_DBG) -I$(TENSOR_DIR)/include $(TENSOR_SRCS) tests/tensor_test.c -o $@ \
-Wl,--start-group $(LIB_A) -Wl,--end-group -lm -lpthread
test-tensor: $(BUILD)/tensor_test ## Roda o teste da tensor (ASan/UBSan)
./$(BUILD)/tensor_test
# -- Teste da library curves ---
$(BUILD)/curves_test: tests/curves_test.c $(CV_DIR)/src/tb_curves.c static
@mkdir -p $(BUILD)