O editor esquece o cache das arvores quando o usuario edita algo e monta outra instancia para o mesmo bloco da tela. Como os pesos da tensor eram achados pela instancia, cada remontagem comecava com pesos novos: no treino da GAN a perda voltava ao formato do inicio de tempos em tempos. Cada bloco da tela ganha um uid e as instancias levam esse uid em TinyBlocks_Block.origin. tt_param acha o parametro pela origem (sem origem, pela propria instancia) e a instancia nova herda os pesos; a velha, ao morrer, nao leva os da nova. As medias moveis do batch norm passam a ser parametros sem gradiente, para irem junto no "salve os parametros". Testes: test-tensor ganha a heranca pela origem; os donos de mentira dos testes viram blocos zerados (o param_key le owner->origin).
289 lines
9.7 KiB
C
289 lines
9.7 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include "tinyblocks/tinyblocks.h"
|
|
|
|
/* -- Alocação da estrutura base --- */
|
|
TinyBlocks_Block* tb_block_alloc_base(TinyBlocks* rt,
|
|
TinyBlocks_BlockKind kind,
|
|
size_t struct_size,
|
|
uint32_t input_count,
|
|
uint32_t output_count,
|
|
const TinyBlocks_BlockVTable* vtable)
|
|
{
|
|
TinyBlocks_Block* b = tb_alloc(rt, struct_size);
|
|
if (!b) return NULL;
|
|
|
|
b->kind = kind;
|
|
b->vtable = vtable;
|
|
b->id = tb_rt_next_id(rt);
|
|
b->origin = 0;
|
|
b->input_count = input_count;
|
|
b->output_count = output_count;
|
|
b->dirty = true;
|
|
|
|
if (input_count > 0) {
|
|
b->inputs = tb_alloc(rt, input_count * sizeof(TinyBlocks_Connection));
|
|
if (!b->inputs) { tb_free(rt, b); return NULL; }
|
|
}
|
|
if (output_count > 0) {
|
|
b->outputs = tb_alloc(rt, output_count * sizeof(TinyBlocks_Value));
|
|
if (!b->outputs) { tb_free(rt, b->inputs); tb_free(rt, b); return NULL; }
|
|
}
|
|
|
|
tb_rt_register_block(rt, b);
|
|
return b;
|
|
}
|
|
|
|
/* -- Vtable padrão para blocos definidos por TypeBlock --- */
|
|
static void default_eval(TinyBlocks_Block* self, TinyBlocks* rt)
|
|
{
|
|
if (self->type && self->type->eval_fn)
|
|
self->type->eval_fn(self, rt);
|
|
self->dirty = false;
|
|
}
|
|
|
|
static void default_destroy(TinyBlocks_Block* self, TinyBlocks* rt)
|
|
{
|
|
if (self->type && self->type->instance_deinit)
|
|
self->type->instance_deinit(self, rt);
|
|
tb_free(rt, self->userdata);
|
|
}
|
|
|
|
static size_t default_to_string(TinyBlocks_Block* self, TinyBlocks* rt,
|
|
char* buf, size_t buf_size)
|
|
{
|
|
(void)rt;
|
|
const char* tname = self->type ? self->type->type_name : "Block";
|
|
const char* bname = self->name ? self->name : "";
|
|
return (size_t)snprintf(buf, buf_size,
|
|
"[%s#%llu%s%s]",
|
|
tname, (unsigned long long)self->id,
|
|
bname[0] ? " " : "", bname);
|
|
}
|
|
|
|
static TinyBlocks_Block* default_clone(const TinyBlocks_Block* self, TinyBlocks* rt)
|
|
{
|
|
TinyBlocks_Block* c = tb_block_create(rt, self->type);
|
|
if (!c) return NULL;
|
|
if (self->name) c->name = tb_strdup(rt, self->name);
|
|
/* copia conexões */
|
|
memcpy(c->inputs, self->inputs, self->input_count * sizeof(TinyBlocks_Connection));
|
|
/* copia outputs */
|
|
for (uint32_t i = 0; i < self->output_count; ++i)
|
|
c->outputs[i] = tb_value_clone(self->outputs[i]);
|
|
return c;
|
|
}
|
|
|
|
static const TinyBlocks_BlockVTable s_default_vtable = {
|
|
.eval = default_eval,
|
|
.destroy = default_destroy,
|
|
.to_string = default_to_string,
|
|
.clone = default_clone,
|
|
};
|
|
|
|
/* -- Criação via TypeBlock --- */
|
|
TinyBlocks_Block* tb_block_create(TinyBlocks* rt, TinyBlocks_TypeBlock* type)
|
|
{
|
|
if (!type) {
|
|
tinyblocks_set_error(rt, "tb_block_create: type is NULL");
|
|
return NULL;
|
|
}
|
|
|
|
TinyBlocks_Block* b = tb_block_alloc_base(rt,
|
|
TB_KIND_BLOCK,
|
|
sizeof(TinyBlocks_Block),
|
|
type->input_desc_count,
|
|
type->output_desc_count,
|
|
&s_default_vtable);
|
|
if (!b) return NULL;
|
|
|
|
b->type = type;
|
|
|
|
/* Inicializa userdata por instância */
|
|
if (type->instance_userdata_size > 0) {
|
|
b->userdata = tb_alloc(rt, type->instance_userdata_size);
|
|
if (!b->userdata) {
|
|
tb_block_destroy(b, rt);
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
if (type->instance_init && !type->instance_init(b, rt)) {
|
|
tb_block_destroy(b, rt);
|
|
return NULL;
|
|
}
|
|
|
|
return b;
|
|
}
|
|
|
|
/* -- Destruição --- */
|
|
void tb_block_destroy(TinyBlocks_Block* block, TinyBlocks* rt)
|
|
{
|
|
if (!block) return;
|
|
tb_rt_unregister_block(rt, block);
|
|
|
|
if (block->vtable && block->vtable->destroy)
|
|
block->vtable->destroy(block, rt);
|
|
|
|
tb_free(rt, block->name);
|
|
|
|
/* Libera valores de default dos inputs */
|
|
for (uint32_t i = 0; i < block->input_count; ++i) {
|
|
tb_value_free(&block->inputs[i].default_value);
|
|
free(block->inputs[i].text_cache);
|
|
}
|
|
tb_free(rt, block->inputs);
|
|
|
|
for (uint32_t i = 0; i < block->output_count; ++i)
|
|
tb_value_free(&block->outputs[i]);
|
|
tb_free(rt, block->outputs);
|
|
|
|
tb_free(rt, block);
|
|
}
|
|
|
|
/* -- Avaliação --- */
|
|
void tb_block_eval(TinyBlocks_Block* block, TinyBlocks* rt)
|
|
{
|
|
if (!block || !block->vtable || !block->vtable->eval) return;
|
|
|
|
/* Detecta ciclo: bloco já está sendo avaliado nesta chamada recursiva */
|
|
if (block->in_eval) {
|
|
tinyblocks_set_error(rt, "ciclo detectado em '%s' - conexão ignorada",
|
|
block->type ? block->type->type_name : "?");
|
|
return;
|
|
}
|
|
block->in_eval = true;
|
|
|
|
/* Avalia fontes conectadas primeiro. Se uma delas levantar um erro, o
|
|
bloco nao roda: o erro sobe (como uma excecao) ate quem trata. */
|
|
bool had_error = rt->error[0] != '\0';
|
|
if (!(block->type && block->type->lazy_inputs)) {
|
|
for (uint32_t i = 0; i < block->input_count; ++i) {
|
|
TinyBlocks_Connection* conn = &block->inputs[i];
|
|
if (conn->source && conn->source->dirty)
|
|
tb_block_eval(conn->source, rt);
|
|
}
|
|
}
|
|
if (!had_error && rt->error[0]) { block->in_eval = false; return; }
|
|
|
|
block->vtable->eval(block, rt);
|
|
block->in_eval = false;
|
|
}
|
|
|
|
/* -- Clone --- */
|
|
TinyBlocks_Block* tb_block_clone(const TinyBlocks_Block* block, TinyBlocks* rt)
|
|
{
|
|
if (!block || !block->vtable || !block->vtable->clone) return NULL;
|
|
return block->vtable->clone(block, rt);
|
|
}
|
|
|
|
/* -- Representação textual --- */
|
|
size_t tb_block_to_string(TinyBlocks_Block* block, TinyBlocks* rt,
|
|
char* buf, size_t buf_size)
|
|
{
|
|
if (!block || !buf || buf_size == 0) return 0;
|
|
if (block->vtable && block->vtable->to_string)
|
|
return block->vtable->to_string(block, rt, buf, buf_size);
|
|
return (size_t)snprintf(buf, buf_size, "[Block#%llu]",
|
|
(unsigned long long)block->id);
|
|
}
|
|
|
|
/* -- Conexões --- */
|
|
bool tb_block_connect(TinyBlocks_Block* target, uint32_t input_idx,
|
|
TinyBlocks_Block* source, uint32_t output_idx)
|
|
{
|
|
if (!target || input_idx >= target->input_count) return false;
|
|
if (!source || output_idx >= source->output_count) return false;
|
|
|
|
TinyBlocks_Connection* conn = &target->inputs[input_idx];
|
|
conn->source = source;
|
|
conn->output_idx = output_idx;
|
|
target->dirty = true;
|
|
return true;
|
|
}
|
|
|
|
void tb_block_disconnect(TinyBlocks_Block* target, uint32_t input_idx)
|
|
{
|
|
if (!target || input_idx >= target->input_count) return;
|
|
target->inputs[input_idx].source = NULL;
|
|
target->dirty = true;
|
|
}
|
|
|
|
bool tb_block_set_default(TinyBlocks_Block* target, uint32_t input_idx,
|
|
TinyBlocks_Value default_value)
|
|
{
|
|
if (!target || input_idx >= target->input_count) return false;
|
|
tb_value_free(&target->inputs[input_idx].default_value);
|
|
target->inputs[input_idx].default_value = tb_value_clone(default_value);
|
|
return true;
|
|
}
|
|
|
|
/* -- Acesso a valores em eval_fn --- */
|
|
TinyBlocks_Value tb_block_get_input(TinyBlocks_Block* self, TinyBlocks* rt,
|
|
uint32_t input_idx)
|
|
{
|
|
if (!self || input_idx >= self->input_count) return tb_value_none();
|
|
|
|
TinyBlocks_Connection* conn = &self->inputs[input_idx];
|
|
if (conn->source) {
|
|
if (conn->source->dirty) tb_block_eval(conn->source, rt);
|
|
if (conn->output_idx < conn->source->output_count)
|
|
return conn->source->outputs[conn->output_idx];
|
|
}
|
|
return conn->default_value;
|
|
}
|
|
|
|
/* -- Entradas convertidas (polimorficas pela classe do valor) --- */
|
|
const char* tb_block_input_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i)
|
|
{
|
|
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
|
if (v.kind == TB_VALUE_TEXT) return v.as.text ? v.as.text : "";
|
|
if (!self || i >= self->input_count) return "";
|
|
TinyBlocks_Connection* c = &self->inputs[i];
|
|
free(c->text_cache);
|
|
c->text_cache = tb_value_to_string(v);
|
|
return c->text_cache ? c->text_cache : "";
|
|
}
|
|
|
|
double tb_block_input_number(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, double def)
|
|
{
|
|
return tb_value_as_number(tb_block_get_input(self, rt, i), def);
|
|
}
|
|
|
|
int64_t tb_block_input_integer(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, int64_t def)
|
|
{
|
|
return tb_value_as_integer(tb_block_get_input(self, rt, i), def);
|
|
}
|
|
|
|
bool tb_block_input_truthy(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i)
|
|
{
|
|
return tb_value_truthy(tb_block_get_input(self, rt, i));
|
|
}
|
|
|
|
/* Toma ownership de v - o caller NÃO deve liberar v depois desta chamada.
|
|
Se precisar manter v, passe tb_value_clone(v) em vez de v. */
|
|
void tb_block_set_output(TinyBlocks_Block* self, uint32_t output_idx,
|
|
TinyBlocks_Value v)
|
|
{
|
|
if (!self || output_idx >= self->output_count) return;
|
|
tb_value_free(&self->outputs[output_idx]);
|
|
self->outputs[output_idx] = v;
|
|
}
|
|
|
|
/* -- Downcasting seguro --- */
|
|
TinyBlocks_TypeBlock* tb_block_as_type (TinyBlocks_Block* b)
|
|
{ return b && b->kind == TB_KIND_TYPE ? (TinyBlocks_TypeBlock*)b : NULL; }
|
|
|
|
TinyBlocks_LibraryBlock* tb_block_as_library(TinyBlocks_Block* b)
|
|
{ return b && b->kind == TB_KIND_LIBRARY ? (TinyBlocks_LibraryBlock*)b : NULL; }
|
|
|
|
TinyBlocks_TextBlock* tb_block_as_text (TinyBlocks_Block* b)
|
|
{ return b && b->kind == TB_KIND_TEXT ? (TinyBlocks_TextBlock*)b : NULL; }
|
|
|
|
TinyBlocks_IntegerBlock* tb_block_as_integer(TinyBlocks_Block* b)
|
|
{ return b && b->kind == TB_KIND_INTEGER ? (TinyBlocks_IntegerBlock*)b : NULL; }
|
|
|
|
TinyBlocks_RealBlock* tb_block_as_real (TinyBlocks_Block* b)
|
|
{ return b && b->kind == TB_KIND_REAL ? (TinyBlocks_RealBlock*)b : NULL; }
|