C API Documentation

Core Layers

warning: unhandled cursor CursorKind.INCLUSION_DIRECTIVE math.h

void k2c_dense(k2c_tensor *output, const k2c_tensor *input, const k2c_tensor *kernel, const k2c_tensor *bias, k2c_activationType *activation, float *fwork)

Dense (fully connected) Layer.

Parameters
  • output – output tensor.

  • input – input tensor.

  • kernel – kernel tensor.

  • bias – bias tensor.

  • activation – activation function to apply to output.

  • fwork – array of working space, size(fwork) = size(input) + size(kernel)

void k2c_flatten(k2c_tensor *output, const k2c_tensor *input)

Flatten Layer. flattens inputs to ndim=1

Parameters
  • output – output tensor.

  • input – input tensor.

  • kernel – kernel tensor.

void k2c_reshape(k2c_tensor *output, const k2c_tensor *input, const int *newshp, const int newndim)

Reshape Layer. reshapes input to arbitrary output shape, while preserving total number of elements.

Parameters
  • output – output tensor.

  • input – input tensor.

  • newshp – array[newndim] of the desired new shape.

  • newndim – number of dimensions after reshaping.

void k2c_permute_dims(k2c_tensor *output, const k2c_tensor *input, const int *permute)

Permute Layer. permutes the dimensions of the input according to a given pattern.

Parameters
  • output – output tensor.

  • input – input tensor.

  • permute – array[ndim] Permutation pattern. Indexing starts at 0. For instance, (1, 0) permutes the first and second dimension of the input.

void k2c_repeat_vector(k2c_tensor *output, const k2c_tensor *input, const int n)

Repeat Vector Layer. repeats the input n times.

Parameters
  • output – output tensor.

  • input – input tensor.

  • n – repetition factor.

Convolutional Layers

warning: unhandled cursor CursorKind.INCLUSION_DIRECTIVE math.h

void k2c_pad1d(k2c_tensor *output, const k2c_tensor *input, const float fill, const int *pad)

1D (temporal) Padding.

Parameters
  • output – tensor to store padded output data.

  • input – tensor to pad.

  • fill – value to fill in padded areas.

  • pad – array[2] of how many rows to pad. Order is {before dim 1, after dim 1}.

void k2c_pad2d(k2c_tensor *output, const k2c_tensor *input, const float fill, const int *pad)

2D (spatial) Padding.

Parameters
  • output – tensor to store padded output data.

  • input – tensor to pad.

  • fill – value to fill in padded areas.

  • pad – array[4] of how many rows/cols to pad. Order is {before dim 1, after dim 1, before dim 2, after dim 2}.

void k2c_pad3d(k2c_tensor *output, const k2c_tensor *input, const float fill, const int *pad)

3D (spatial or spatio-temporal) Padding.

Parameters
  • output – tensor to store padded output data.

  • input – tensor to pad.

  • fill – value to fill in padded areas.

  • pad – array[6] of how many rows/cols to pad. Order is {before dim 1, after dim 1, before dim 2, after dim 2, before dim 3, after dim 3}.

void k2c_conv1d(k2c_tensor *output, const k2c_tensor *input, const k2c_tensor *kernel, const k2c_tensor *bias, const int stride, const int dilation, k2c_activationType *activation)

1D (temporal) Convolution. Assumes a “channels last” structure.

Parameters
  • output – output tensor.

  • input – input tensor.

  • kernel – kernel tensor.

  • bias – bias tensor.

  • stride – stride length of the convolution.

  • dilation – dilation rate to use for dilated convolution.

  • activation – activation function to apply to output.

void k2c_conv2d(k2c_tensor *output, const k2c_tensor *input, const k2c_tensor *kernel, const k2c_tensor *bias, const int *stride, const int *dilation, k2c_activationType *activation)

2D (spatial) Convolution. Assumes a “channels last” structure.

Parameters
  • output – output tensor.

  • input – input tensor.

  • kernel – kernel tensor.

  • bias – bias tensor.

  • stride – array[2] of stride length of the convolution. Order is {stride dim 1, stride dim 2}.

  • dilation – array[2] dilation rate to use for dilated convolution. Order is {dilation dim 1, dilation dim 2}.

  • activation – activation function to apply to output.

void k2c_conv3d(k2c_tensor *output, const k2c_tensor *input, const k2c_tensor *kernel, const k2c_tensor *bias, const int *stride, const int *dilation, k2c_activationType *activation)

3D (spatial or spatio-temporal) Convolution. Assumes a “channels last” structure.

Parameters
  • output – output tensor.

  • input – input tensor.

  • kernel – kernel tensor.

  • bias – bias tensor.

  • stride – array[3] of stride length of the convolution. Order is {stride dim 1, stride dim 2, stride dim 3}.

  • dilation – array[3] dilation rate to use for dilated convolution. Order is {dilation dim 1, dilation dim 2, dilation dim 3}.

  • activation – activation function to apply to output.

void k2c_crop1d(k2c_tensor *output, const k2c_tensor *input, const int *crop)

1D (temporal) Cropping.

Parameters
  • output – tensor to store cropped output data.

  • input – tensor to crop.

  • pad – array[2] of how many rows to crop. Order is {before dim 1, after dim 1}.

void k2c_crop2d(k2c_tensor *output, const k2c_tensor *input, const int *crop)

2D (spatial) Cropping.

Parameters
  • output – tensor to store cropped output data.

  • input – tensor to crop.

  • pad – array[4] of how many rows/cols to crop. Order is {before dim 1, after dim 1, before dim 2, after dim 2}.

void k2c_crop3d(k2c_tensor *output, const k2c_tensor *input, const int *crop)

3D (spatial or spatio-temporal) Cropping.

Parameters
  • output – tensor to store cropped output data.

  • input – tensor to crop.

  • pad – array[6] of how many rows/cols to crop. Order is {before dim 1, after dim 1, before dim 2, after dim 2, before dim 3, after dim 3}.

void k2c_upsampling1d(k2c_tensor *output, const k2c_tensor *input, const int size)

1D (temporal) Upsampling. Repeats each temporal step size times along the time axis.

Parameters
  • output – output tensor.

  • input – input tensor.

  • size – Upsampling factor.

void k2c_upsampling2d(k2c_tensor *output, const k2c_tensor *input, const int *size)

2D (spatial) Upsampling. Repeats the rows and columns of the data by size[0] and size[1] respectively.

Parameters
  • output – output tensor.

  • input – input tensor.

  • size – array[2] of upsampling factors. Order is {upsampling dim 1, upsampling dim 2}.

void k2c_upsampling3d(k2c_tensor *output, const k2c_tensor *input, const int *size)

2D (spatial) Upsampling. Repeats the 1st, 2nd and 3rd dimensions of the data by size[0], size[1] and size[2] respectively.

Parameters
  • output – output tensor.

  • input – input tensor.

  • size – array[3] of upsampling factors. Order is {upsampling dim 1, upsampling dim 2, upsampling dim 3}.

Pooling Layers

warning: unhandled cursor CursorKind.INCLUSION_DIRECTIVE math.h

void k2c_global_max_pooling(k2c_tensor *output, const k2c_tensor *input)

Global max pooling. works for 1D, 2D, or 3D inputs.

Parameters
  • output – output tensor.

  • input – input tensor.

void k2c_global_avg_pooling(k2c_tensor *output, const k2c_tensor *input)

Global average pooling. works for 1D, 2D, or 3D inputs.

Parameters
  • output – output tensor.

  • input – input tensor.

void k2c_maxpool1d(k2c_tensor *output, const k2c_tensor *input, const int pool_size, const int stride)

Max pooling for 1D (temporal) data.

Parameters
  • output – output tensor.

  • input – input tensor.

  • pool_size – size of the max pooling window.

  • stride – factor by which to downscale.

void k2c_maxpool2d(k2c_tensor *output, const k2c_tensor *input, const int *pool_size, const int *stride)

Max pooling for 2D (spatial) data.

Parameters
  • output – output tensor.

  • input – input tensor.

  • pool_size – array[2] size of the max pooling window. Order is {pool size dim 1, pool size dim 2}.

  • stride – array[2] factor by which to downscale. Order is {stride dim 1, stride dim 2}.

void k2c_avgpool1d(k2c_tensor *output, const k2c_tensor *input, const int pool_size, const int stride)

Average pooling for 1D (temporal) data.

Parameters
  • output – output tensor.

  • input – input tensor.

  • pool_size – size of the average pooling window.

  • stride – factor by which to downscale.

void k2c_avgpool2d(k2c_tensor *output, const k2c_tensor *input, const int *pool_size, const int *stride)

Average pooling for 2D (spatial) data.

Parameters
  • output – output tensor.

  • input – input tensor.

  • pool_size – array[2] size of the average pooling window. Order is {pool size dim 1, pool size dim 2}.

  • stride – array[2] factor by which to downscale. Order is {stride dim 1, stride dim 2}.

Recurrent Layers

warning: unhandled cursor CursorKind.INCLUSION_DIRECTIVE math.h

void k2c_lstmcell(float *state, const float *input, const k2c_tensor *kernel, const k2c_tensor *recurrent_kernel, const k2c_tensor *bias, float *fwork, k2c_activationType *recurrent_activation, k2c_activationType *output_activation)

Cell for the LSTM layer. “units” is the dimension of the output space

Parameters
  • state – array[2*units] recurrent state.

  • input – array of input data.

  • kernel – kernel tensor.

  • recurrent_kernel – recurrent kernel tensor

  • bias – bias tensor.

  • fwork – array[8*units] working storage.

  • recurrent_activation – activation function to apply to internal state.

  • output_activation – activation function to apply to output.

void k2c_lstm(k2c_tensor *output, const k2c_tensor *input, float *state, const k2c_tensor *kernel, const k2c_tensor *recurrent_kernel, const k2c_tensor *bias, float *fwork, const int go_backwards, const int return_sequences, k2c_activationType *recurrent_activation, k2c_activationType *output_activation)

Long Short-Term Memory (LSTM) layer. “units” is the dimension of the output space

Parameters
  • output – output tensor.

  • input – input tensor.

  • state – array[2*units] recurrent state.

  • kernel – kernel tensor.

  • recurrent_kernel – recurrent kernel tensor

  • bias – bias tensor.

  • fwork – array[8*units] working storage.

  • go_backwards – whether to process input sequences forwards (1) or backwards (0).

  • return_sequences – whether to return the last output in the output sequence (0), or the full sequence (1).

  • recurrent_activation – activation function to apply to internal state.

  • output_activation – activation function to apply to output.

void k2c_simpleRNNcell(float *state, const float *input, const k2c_tensor *kernel, const k2c_tensor *recurrent_kernel, const k2c_tensor *bias, float *fwork, k2c_activationType *output_activation)

Cell for the RNN layer. “units” is the dimension of the output space

Parameters
  • state – array[units] recurrent state.

  • input – array of input data.

  • kernel – kernel tensor.

  • recurrent_kernel – recurrent kernel tensor

  • bias – bias tensor.

  • fwork – array[2*units] working storage.

  • output_activation – activation function to apply to output.

void k2c_simpleRNN(k2c_tensor *output, const k2c_tensor *input, float *state, const k2c_tensor *kernel, const k2c_tensor *recurrent_kernel, const k2c_tensor *bias, float *fwork, const int go_backwards, const int return_sequences, k2c_activationType *output_activation)

Fully-connected RNN where the output is to be fed back to input. “units” is the dimension of the output space

Parameters
  • output – output tensor.

  • input – input tensor.

  • state – array[units] recurrent state.

  • kernel – kernel tensor.

  • recurrent_kernel – recurrent kernel tensor

  • bias – bias tensor.

  • fwork – array[2*units] working storage.

  • go_backwards – whether to process input sequences forwards (1) or backwards (0).

  • return_sequences – whether to return the last output in the output sequence (0), or the full sequence (1).

  • output_activation – activation function to apply to output.

void k2c_grucell(float *state, const float *input, const k2c_tensor *kernel, const k2c_tensor *recurrent_kernel, const k2c_tensor *bias, float *fwork, const int reset_after, k2c_activationType *recurrent_activation, k2c_activationType *output_activation)

Cell for the GRU layer. “units” is the dimension of the output space

Parameters
  • state – array[units] recurrent state.

  • input – array of input data.

  • kernel – kernel tensor.

  • recurrent_kernel – recurrent kernel tensor

  • bias – bias tensor.

  • fwork – array[6*units] working storage.

  • reset_after – whether to apply the reset gate before (0) or after (1) the matrix multiplication.

  • recurrent_activation – activation function to apply to internal state.

  • output_activation – activation function to apply to output.

void k2c_gru(k2c_tensor *output, const k2c_tensor *input, float *state, const k2c_tensor *kernel, const k2c_tensor *recurrent_kernel, const k2c_tensor *bias, float *fwork, const int reset_after, const int go_backwards, const int return_sequences, k2c_activationType *recurrent_activation, k2c_activationType *output_activation)

Gated Recurrent Unit. “units” is the dimension of the output space

Parameters
  • output – output tensor.

  • input – input tensor.

  • state – array[units] recurrent state.

  • kernel – kernel tensor.

  • recurrent_kernel – recurrent kernel tensor

  • bias – bias tensor.

  • fwork – array[6*units] working storage.

  • reset_after – whether to apply the reset gate before (0) or after (1) the matrix multiplication.

  • go_backwards – whether to process input sequences forwards (1) or backwards (0).

  • return_sequences – whether to return the last output in the output sequence (0), or the full sequence (1).

  • recurrent_activation – activation function to apply to internal state.

  • output_activation – activation function to apply to output.

Embedding Layers

warning: unhandled cursor CursorKind.INCLUSION_DIRECTIVE math.h

void k2c_embedding(k2c_tensor *outputs, const k2c_tensor *inputs, const k2c_tensor *kernel)

Embedding Layer. turns positive integers (indexes) into dense vectors of fixed size. eg. [[4], [20]] -> [[0.25, 0.1], [0.6, -0.2]]

Parameters
  • output – output tensor.

  • input – input tensor.

  • kernel – kernel mapping integers to vectors.

Merge Layers

warning: unhandled cursor CursorKind.INCLUSION_DIRECTIVE string.h

void k2c_add(k2c_tensor *output, const int num_tensors, ...)

Element-wise sum of several tensors.

Parameters
  • output – output tensor.

  • num_tensors – number of tensors being summed.

  • ... – variadic. Tensors to be summed.

void k2c_subtract(k2c_tensor *output, const int num_tensors, const k2c_tensor *tensor1, const k2c_tensor *tensor2)

Element-wise difference of two tensors.

Parameters
  • output – output tensor.

  • num_tensors – number of tensors being summed. Not used but kept for a consistent API with other merge layers.

  • tensor1 – first input tensor.

  • tensor2 – second input tensor.

void k2c_multiply(k2c_tensor *output, const int num_tensors, ...)

Element-wise product of several tensors.

Parameters
  • output – output tensor.

  • num_tensors – number of tensors being multiplied.

  • ... – variadic. Tensors to be multiplied.

void k2c_average(k2c_tensor *output, const int num_tensors, ...)

Element-wise average of several tensors.

Parameters
  • output – output tensor.

  • num_tensors – number of tensors being averaged.

  • ... – variadic. Tensors to be averaged.

void k2c_max(k2c_tensor *output, const int num_tensors, ...)

Element-wise maximum of several tensors.

Parameters
  • output – output tensor.

  • num_tensors – number of tensors over which to take max.

  • ... – variadic. Tensors to take the max of.

void k2c_min(k2c_tensor *output, const int num_tensors, ...)

Element-wise minimum of several tensors.

Parameters
  • output – output tensor.

  • num_tensors – number of tensors over which to take min.

  • ... – variadic. Tensors to take the min of.

void k2c_concatenate(k2c_tensor *output, const int axis, const int num_tensors, ...)

Concatenation of several tensors.

Parameters
  • output – output tensor.

  • axis – axis along which to concatenate.

  • num_tensors – number of tensors being concatenated.

  • ... – variadic. Tensors to concatenate.

Normalization Layers

warning: unhandled cursor CursorKind.INCLUSION_DIRECTIVE math.h

void k2c_batch_norm(k2c_tensor *outputs, const k2c_tensor *inputs, const k2c_tensor *mean, const k2c_tensor *stdev, const k2c_tensor *gamma, const k2c_tensor *beta, const int axis)

Batch normalization layer. applies a transformation that maintains the mean activation close to 0 and the activation standard deviation close to 1.

Parameters
  • outputs – output tensor.

  • inputs – input tensor.

  • mean – tensor of mean values.

  • stdev – tensor of standard deviation values.

  • gamma – tensor of gamma (scale) values.

  • beta – tensor of beta (offset) values.

  • axis – axis to be normalized.

Activations

warning: unhandled cursor CursorKind.INCLUSION_DIRECTIVE math.h

void k2c_linear_func(float *x, const int size)
Linear activation function.

y=x

Parameters
  • x – array of input values. Gets overwritten by output.

  • size – length of input array.

void k2c_exponential_func(float *x, const int size)
Exponential activation function.

y = exp(x)

Parameters
  • x – array of input values. Gets overwritten by output.

  • size – length of input array.

void k2c_relu_func(float *x, const int size)
ReLU activation function.

y = max(x,0)

Parameters
  • x – array of input values. Gets overwritten by output.

  • size – length of input array.

void k2c_hard_sigmoid_func(float *x, const int size)
ReLU activation function.
y = {1 if x> 2.5}

{0.2*x+0.5 if -2.5<x< 2.5} {0 if x<-2.5}

Parameters
  • x – array of input values. Gets overwritten by output.

  • size – length of input array.

void k2c_tanh_func(float *x, const int size)
Tanh activation function.

y = tanh(x)

Parameters
  • x – array of input values. Gets overwritten by output.

  • size – length of input array.

void k2c_sigmoid_func(float *x, const int size)
Sigmoid activation function.

y = 1/(1+exp(-x))

Parameters
  • x – array of input values. Gets overwritten by output.

  • size – length of input array.

void k2c_softmax_func(float *x, const int size)
Soft max activation function.

z[i] = exp(x[i]-max(x)) y = z/sum(z)

Parameters
  • x – array of input values. Gets overwritten by output.

  • size – length of input array.

void k2c_softplus_func(float *x, const int size)
Soft plus activation function.

y = ln(1+exp(x))

Parameters
  • x – array of input values. Gets overwritten by output.

  • size – length of input array.

void k2c_softsign_func(float *x, const int size)
Soft sign activation function.

y = x/(1+|x|)

Parameters
  • x – array of input values. Gets overwritten by output.

  • size – length of input array.

void k2c_LeakyReLU(float *x, const int size, const float alpha)

Leaky version of a Rectified Linear Unit. It allows a small gradient when the unit is not active:

y = {alpha*x if x < 0}

{x if x >= 0}

Parameters
  • x – array of input values. Gets overwritten by output.

  • size – length of input array.

  • alpha – slope of negative portion of activation curve.

void k2c_PReLU(float *x, const int size, const float *alpha)

Parametric Rectified Linear Unit. It allows a small gradient when the unit is not active:

y = {alpha*x if x < 0}

{x if x >= 0}

Where alpha is a learned array with the same shape as x.

Parameters
  • x – array of input values. Gets overwritten by output.

  • size – length of input array.

  • alpha – slope of negative portion of activation curve for each unit.

void k2c_ELU(float *x, const int size, const float alpha)
Exponential Linear Unit activation (ELU).
y = {alpha*(exp(x) - 1) if x < 0}

{x if x >= 0}

Parameters
  • x – array of input values. Gets overwritten by output.

  • size – length of input array.

  • alpha – slope of negative portion of activation curve.

void k2c_ThresholdedReLU(float *x, const int size, const float theta)
Thresholded Rectified Linear Unit.

y = {x if x > theta}

{0 if x <= theta}

Parameters
  • x – array of input values. Gets overwritten by output.

  • size – length of input array.

  • theta – threshold for activation.

void k2c_ReLU(float *x, const int size, const float max_value, const float alpha, const float theta)
Rectified Linear Unit activation function.
y = {max_value if x >= max_value}

{x if theta <= x < max_value} {alpha*(x-theta) if x < theta}

Parameters
  • x – array of input values. Gets overwritten by output.

  • size – length of input array.

  • max_value – maximum value for activated x.

  • alpha – slope of negative portion of activation curve.

  • theta – threshold for activation.

Data Types

warning: unhandled cursor CursorKind.INCLUSION_DIRECTIVE stdlib.h

K2C_MAX_NDIM

Rank of largest keras2c tensors. mostly used to ensure a standard size for the tensor.shape array.

struct k2c_tensor

tensor type for keras2c.

float *array

Pointer to array of tensor values flattened in row major order.

int ndim

Rank of the tensor (number of dimensions).

int numel

Number of elements in the tensor.

type k2c_tensor

Array, size of the tensor in each dimension.

Helper Functions

warning: unhandled cursor CursorKind.INCLUSION_DIRECTIVE math.h

void k2c_matmul(float *C, const float *A, const float *B, const int outrows, const int outcols, const int innerdim)

Just your basic 1d matrix multipication. computes C = A*B assumes A,B,C are all 1d arrays of matrices stored in row major order.

Parameters
  • C – output array.

  • A – input array 1.

  • B – input array 2.

  • outrows – number of rows of C and A.

  • outcols – number of cols of C and B.

  • innderdim – number of cols of A and rows of B

void k2c_affine_matmul(float *C, const float *A, const float *B, const float *d, const int outrows, const int outcols, const int innerdim)

Affine matrix multiplication. computes C = A*B + d, where d is a vector that is added to each row of A*B assumes A,B,C are all 1d arrays of matrices stored in row major order

Parameters
  • C – output array.

  • A – input array 1.

  • B – input array 2.

  • d – input array 3.

  • outrows – number of rows of C and A.

  • outcols – number of cols of C, B and d.

  • innderdim – number of cols of A and rows of B

int k2c_sub2idx()

Converts subscripts to linear indices in row major order.

Parameters
  • sub – array[ndim] subscript to convert.

  • shape – array[ndim] shape of array being indexed.

  • ndim – number of dimensions of array being indexed.

Returns

linear index in row major order.

void k2c_idx2sub(const int idx, int *sub, const int *shape, const int ndim)

Converts linear indices to subscripts in row major order.

Parameters
  • idx – linear index in row major order.

  • sub – array[ndim] output subscript.

  • shape – array[ndim] shape of array being indexed.

  • ndim – number of dimensions of array being indexed.

void k2c_dot(k2c_tensor *C, const k2c_tensor *A, const k2c_tensor *B, const int *axesA, const int *axesB, const int naxes, const int normalize, float *fwork)

Dot product (tensor contraction) between 2 tensors. C=A*B

Parameters
  • C – output tensor.

  • A – input tensor 1.

  • B – input tensor 2.

  • axesA – array[naxes] of axes of A being contracted.

  • axesB – array[naxes] of axes of B being contracted.

  • naxes – number of axes being contracted from each input.

  • normalize – (0,1) whether to L2-normalize samples along the dot product axis before taking the dot product. If set to 1, then the output of the dot product is the cosine proximity between the two samples.

  • fwork – array of working space, size(fwork) = size(A) + size(B)

void k2c_bias_add(k2c_tensor *A, const k2c_tensor *b)

Adds bias vector b to tensor A. assumes b is a rank 1 tensor that is added to the last dimension of A.

Parameters
  • A – input tensor. Overwritten with outputs.

  • b – bias tensor.

void k2c_flip(k2c_tensor *A, const int axis)

Flips a tensor along specified axis. overwrites input with flipped output.

Parameters
  • A – input tensor. Overwritten with outputs.

  • axis – axis along which to flip

float *k2c_read_array(const char *filename, const int array_size)

Reads array from csv file.

Parameters
  • filename – file to read from. Assumed comma separated ascii text.

  • array_size – how many values to read from the file.

Returns

pointer to allocated array.