Parametros seguem o bloco do editor (origin), nao a instancia remontada
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).
This commit is contained in:
1 parent
422c3fb6cb
commit
a26d329521
12 files changed
+68
-22
No files matched your search
@@ -137,16 +137,16 @@ static void eval_batchnorm(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
if (x->nd < 2) A->set_error("batch norm precisa de [lote, canais, ...]");
|
||||
else {
|
||||
int C = x->shape[1];
|
||||
Layer* L = self->userdata;
|
||||
if (!L->buf[0] || (int)L->buf[0]->n != C) { /* medias moveis (nao treinam) */
|
||||
A->release(L->buf[0]); A->release(L->buf[1]);
|
||||
L->buf[0] = A->alloc(1, &C);
|
||||
L->buf[1] = A->alloc(1, &C);
|
||||
for (int i = 0; L->buf[1] && i < C; i++) L->buf[1]->d[i] = 1;
|
||||
}
|
||||
TbTensor* g = P(self, 0, 1, &C, TB_INIT_ONES, 1);
|
||||
TbTensor* b = P(self, 1, 1, &C, TB_INIT_ZEROS, 0);
|
||||
if (g && b && L->buf[0] && L->buf[1]) r = ai_batchnorm(x, g, b, L->buf[0], L->buf[1], g_ai_train, 0.1f);
|
||||
/* medias moveis: parametros sem gradiente (os otimizadores pulam),
|
||||
para irem junto no "salve os parametros" e sobreviverem quando o
|
||||
editor remonta o bloco */
|
||||
TbTensor* mean = P(self, 2, 1, &C, TB_INIT_ZEROS, 0);
|
||||
TbTensor* var = P(self, 3, 1, &C, TB_INIT_ONES, 1);
|
||||
if (mean) mean->req = false;
|
||||
if (var) var->req = false;
|
||||
if (g && b && mean && var) r = ai_batchnorm(x, g, b, mean, var, g_ai_train, 0.1f);
|
||||
}
|
||||
rel(&x, 1); done(self, rt, r);
|
||||
}
|
||||
|
||||
@@ -539,6 +539,7 @@ typedef struct {
|
||||
typedef struct {
|
||||
bool used;
|
||||
bool dragging;
|
||||
uint64_t uid; /* identidade do bloco (vai no origin das instâncias) */
|
||||
TinyBlocks_TypeBlock* type;
|
||||
char type_name[64];
|
||||
float x, y; /* posição (só para o topo de uma pilha) */
|
||||
|
||||
@@ -377,11 +377,18 @@ void blk_release(Tab* T, int i) {
|
||||
T->free_ids[T->free_n++] = i;
|
||||
}
|
||||
|
||||
/* Identidade de cada bloco que nasce na tela. Vai no "origin" das
|
||||
instancias do runtime: quando o cache das arvores e esvaziado (uma edicao,
|
||||
um clique) a instancia nova herda o estado preso a ela, como os
|
||||
parametros de uma camada da tinyblocks-tensor. */
|
||||
static uint64_t s_blk_uid;
|
||||
|
||||
static int blk_alloc(Tab* T) {
|
||||
if (T->free_n > 0) {
|
||||
int i = T->free_ids[--T->free_n];
|
||||
blk_slot_clear(T, i);
|
||||
T->ws[i].used = true;
|
||||
T->ws[i].uid = ++s_blk_uid;
|
||||
return i;
|
||||
}
|
||||
if (T->ws_n >= MAX_WS) return -1;
|
||||
@@ -389,6 +396,7 @@ static int blk_alloc(Tab* T) {
|
||||
if (!blk_reserve(T, i)) return -1;
|
||||
blk_slot_clear(T, i);
|
||||
T->ws[i].used = true;
|
||||
T->ws[i].uid = ++s_blk_uid;
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -1291,6 +1299,7 @@ TinyBlocks_Block* build_rt(Ed* ed, Tab* T, int bi) {
|
||||
if (ed->run_n >= MAX_RUN_BLOCKS) { run_abort(ed, "limite de blocos em execução atingido"); return NULL; }
|
||||
TinyBlocks_Block* r = tb_block_create(ed->rt, t);
|
||||
if (!r) return NULL;
|
||||
r->origin = b->uid;
|
||||
run_track(ed, r);
|
||||
if (T == ed->live_tab) b->live_rt = r;
|
||||
|
||||
@@ -3060,6 +3069,7 @@ static void bp_tab_load(Ed* ed, const TbBpTab* B, Tab* T) {
|
||||
if (!blk_reserve(T, i)) break;
|
||||
EdBlock* b = &T->ws[i];
|
||||
b->used = true;
|
||||
b->uid = ++s_blk_uid;
|
||||
snprintf(b->type_name, sizeof(b->type_name), "%s", o->type);
|
||||
b->type = b->type_name[0] ? tb_type_block_find(ed->rt, b->type_name) : NULL;
|
||||
b->x = o->x; b->y = o->y;
|
||||
|
||||
@@ -393,7 +393,14 @@ bool tt_backward(TbTensor* root) {
|
||||
}
|
||||
|
||||
/* == Parametros ======================================================= */
|
||||
typedef struct { TinyBlocks_Block* owner; int slot; TbTensor* t; char group[32]; } Param;
|
||||
/* Um parametro e achado pela origem do bloco dono (owner->origin, estavel
|
||||
quando o editor remonta a arvore: a instancia nova herda os pesos) ou,
|
||||
sem origem, pela propria instancia. */
|
||||
typedef struct { TinyBlocks_Block* owner; uint64_t key; int slot; TbTensor* t; char group[32]; } Param;
|
||||
|
||||
static uint64_t param_key(const TinyBlocks_Block* owner) {
|
||||
return owner && owner->origin ? owner->origin : (uint64_t)(uintptr_t)owner;
|
||||
}
|
||||
static char s_group[32]; /* grupo dos parametros que nascerem agora */
|
||||
static Param* s_params;
|
||||
static int s_param_n, s_param_cap;
|
||||
@@ -414,8 +421,10 @@ static void param_fill(TbTensor* t, int init, float scale) {
|
||||
}
|
||||
|
||||
TbTensor* tt_param(TinyBlocks_Block* owner, int slot, int nd, const int* shape, int init, float scale) {
|
||||
uint64_t key = param_key(owner);
|
||||
for (int i = 0; i < s_param_n; i++)
|
||||
if (s_params[i].owner == owner && s_params[i].slot == slot) {
|
||||
if (s_params[i].key == key && s_params[i].slot == slot) {
|
||||
s_params[i].owner = owner; /* instancia remontada: herda */
|
||||
TbTensor* t = s_params[i].t;
|
||||
bool same = t->nd == nd;
|
||||
for (int k = 0; same && k < nd; k++) same = t->shape[k] == shape[k];
|
||||
@@ -439,7 +448,7 @@ TbTensor* tt_param(TinyBlocks_Block* owner, int slot, int nd, const int* shape,
|
||||
if (!p) { tt_release(t); tt_set_error("sem memória"); return NULL; }
|
||||
s_params = p; s_param_cap = nc;
|
||||
}
|
||||
s_params[s_param_n] = (Param){ owner, slot, t, "" };
|
||||
s_params[s_param_n] = (Param){ owner, key, slot, t, "" };
|
||||
snprintf(s_params[s_param_n].group, sizeof(s_params[s_param_n].group), "%s", s_group);
|
||||
s_param_n++;
|
||||
return t;
|
||||
|
||||
Reference in new issue
Block a user