Skip to content

Commit

Permalink
r437: removed the instant mode
Browse files Browse the repository at this point in the history
It is rarely useful...
  • Loading branch information
lh3 committed Feb 22, 2017
1 parent 2230b1a commit 0d3a096
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 49 deletions.
2 changes: 1 addition & 1 deletion examples/vae.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "kann.h"
#include "kann_extra/kann_data.h"

#define const_scalar(x) kad_leaf0(KAD_CONST, 0, (x))
#define const_scalar(x) kann_leaf0(KAD_F_CONSTANT, (x))

static kann_t *model_gen(int n_in, int n_hidden, int n_code)
{
Expand Down
12 changes: 11 additions & 1 deletion kann.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,16 @@ kann_t *kann_load(const char *fn)
*** @@LAYER: layers and model generation ***
**********************************************/

kad_node_t *kann_leaf0(uint8_t flag, float x)
{
kad_node_t *p;
p = (kad_node_t*)calloc(1, sizeof(kad_node_t));
p->n_d = 0;
p->x = (float*)calloc(1, sizeof(float));
*p->x = x, p->flag = flag;
return p;
}

kad_node_t *kann_new_weight(int n_row, int n_col)
{
kad_node_t *w;
Expand Down Expand Up @@ -361,7 +371,7 @@ kad_node_t *kann_layer_linear(kad_node_t *in, int n1)
kad_node_t *kann_layer_dropout(kad_node_t *t, float r)
{
kad_node_t *x[2];
x[0] = t, x[1] = kad_dropout(t, kad_leaf0(KAD_CONST, 0, r));
x[0] = t, x[1] = kad_dropout(t, kann_leaf0(KAD_F_CONSTANT, r));
return kad_switch(2, x);
}

Expand Down
3 changes: 2 additions & 1 deletion kann.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#ifndef KANN_H
#define KANN_H

#define KANN_VERSION "r436"
#define KANN_VERSION "r437"

#define KANN_F_IN 0x1 // input
#define KANN_F_OUT 0x2 // output
Expand Down Expand Up @@ -194,6 +194,7 @@ kad_node_t *kann_layer_conv1d(kad_node_t *in, int n_flt, int k_size, int stride,
kad_node_t *kann_layer_max2d(kad_node_t *in, int k_rows, int k_cols, int stride, int pad);
kad_node_t *kann_layer_cost(kad_node_t *t, int n_out, int cost_type);

kad_node_t *kann_leaf0(uint8_t flag, float x);
kad_node_t *kann_new_weight(int n_row, int n_col);
kad_node_t *kann_new_bias(int n);
kad_node_t *kann_new_weight_conv2d(int n_out_channel, int n_in_channel, int k_row, int k_col);
Expand Down
47 changes: 6 additions & 41 deletions kautodiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static inline kad_node_t *kad_new_core(int n_d, int op, int n_child)
return s;
}

kad_node_t *kad_vleaf(kad_leaftype_t type, int is_dyn, float *x, float *g, int n_d, va_list ap)
static inline kad_node_t *kad_vleaf(uint8_t flag, float *x, float *g, int n_d, va_list ap)
{
int i;
kad_node_t *p;
Expand All @@ -36,61 +36,37 @@ kad_node_t *kad_vleaf(kad_leaftype_t type, int is_dyn, float *x, float *g, int n
p->n_d = n_d;
for (i = 0; i < n_d; ++i)
p->d[i] = va_arg(ap, int32_t);
p->x = x, p->g = g, p->flag = is_dyn? KAD_F_INST_FOR : 0;
if (type == KAD_VAR) p->flag |= KAD_F_WITH_PD;
else if (type == KAD_CONST) p->flag |= KAD_F_CONSTANT;
return p;
}

kad_node_t *kad_leaf(kad_leaftype_t type, int is_dyn, float *x, float *g, int n_d, ...)
{
kad_node_t *p;
va_list ap;
va_start(ap, n_d);
p = kad_vleaf(type, is_dyn, x, g, n_d, ap);
va_end(ap);
return p;
}

kad_node_t *kad_leaf0(kad_leaftype_t type, int is_dyn, float x)
{
kad_node_t *p;
p = (kad_node_t*)calloc(1, sizeof(kad_node_t));
p->n_d = 0;
p->x = (float*)calloc(1, sizeof(float));
*p->x = x, p->flag = is_dyn? KAD_F_INST_FOR : 0;
if (type == KAD_VAR) p->flag |= KAD_F_WITH_PD;
else if (type == KAD_CONST) p->flag |= KAD_F_CONSTANT;
p->x = x, p->g = g, p->flag = flag;
return p;
}

kad_node_t *kad_const(float *x, int n_d, ...)
{
kad_node_t *p;
va_list ap;
va_start(ap, n_d); p = kad_vleaf(KAD_CONST, 0, x, 0, n_d, ap); va_end(ap);
va_start(ap, n_d); p = kad_vleaf(KAD_F_CONSTANT, x, 0, n_d, ap); va_end(ap);
return p;
}

kad_node_t *kad_feed(int n_d, ...)
{
kad_node_t *p;
va_list ap;
va_start(ap, n_d); p = kad_vleaf(KAD_FEED, 0, 0, 0, n_d, ap); va_end(ap);
va_start(ap, n_d); p = kad_vleaf(0, 0, 0, n_d, ap); va_end(ap);
return p;
}

kad_node_t *kad_var(float *x, float *g, int n_d, ...)
{
kad_node_t *p;
va_list ap;
va_start(ap, n_d); p = kad_vleaf(KAD_VAR, 0, x, g, n_d, ap); va_end(ap);
va_start(ap, n_d); p = kad_vleaf(KAD_F_WITH_PD, x, g, n_d, ap); va_end(ap);
return p;
}

static inline kad_node_t *kad_finalize_node(kad_node_t *s) // a helper function
{
int i, n;
int i;
if (kad_op_list[s->op](s, KAD_SYNC_DIM) < 0) { // check dimension
if (s->ptr) free(s->ptr);
free(s->child); free(s);
Expand All @@ -100,17 +76,6 @@ static inline kad_node_t *kad_finalize_node(kad_node_t *s) // a helper function
if (kad_is_back(s->child[i]))
break;
if (i < s->n_child) s->flag |= KAD_F_WITH_PD;
for (i = 0; i < s->n_child; ++i)
if ((s->child[i]->flag & KAD_F_INST_FOR) == 0)
break;
if (i == s->n_child) { // forward computation right away
n = kad_len(s);
s->x = (float*)calloc(n, sizeof(float));
if (kad_is_back(s)) s->g = (float*)calloc(n, sizeof(float));
kad_op_list[s->op](s, KAD_ALLOC);
kad_op_list[s->op](s, KAD_FORWARD);
s->flag |= KAD_F_INST_FOR;
}
return s;
}

Expand Down
6 changes: 1 addition & 5 deletions kautodiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#ifndef KANN_AUTODIFF_H
#define KANN_AUTODIFF_H

#define KAD_VERSION "r436"
#define KAD_VERSION "r437"

#include <stdio.h>
#include <stdint.h>
Expand All @@ -50,7 +50,6 @@ typedef enum {
#define KAD_F_WITH_PD 0x1 // PD = partial derivative
#define KAD_F_CONSTANT 0x2
#define KAD_F_POOLING 0x4
#define KAD_F_INST_FOR 0x8
#define KAD_F_SHARE_RNG 0x10 // with this flag on, different time step shares the same RNG status after unroll

#define kad_is_back(p) ((p)->flag & KAD_F_WITH_PD)
Expand Down Expand Up @@ -156,9 +155,6 @@ kad_node_t *kad_var(float *x, float *g, int n_d, ...); // a variable; gradients
kad_node_t *kad_const(float *x, int n_d, ...); // a constant; no gradients computed; not unrolled
kad_node_t *kad_feed(int n_d, ...); // an input/output; no gradients computed; unrolled

kad_node_t *kad_leaf(kad_leaftype_t type, int is_dyn, float *x, float *g, int n_d, ...);
kad_node_t *kad_leaf0(kad_leaftype_t type, int is_dyn, float x);

// operators taking two operands
kad_node_t *kad_add(kad_node_t *x, kad_node_t *y); // f(x,y) = x + y (generalized element-wise addition; f[i*n+j]=x[i*n+j]+y[j], n=kad_len(y), 0<j<n, 0<i<kad_len(x)/n)
kad_node_t *kad_sub(kad_node_t *x, kad_node_t *y); // f(x,y) = x - y (generalized element-wise subtraction)
Expand Down

0 comments on commit 0d3a096

Please sign in to comment.