Python API Documentation¶
Main¶
-
keras2c.keras2c_main.
k2c
(model, function_name, malloc=False, num_tests=10, verbose=True)¶ Converts keras model to C code and generates test suite
- Parameters
model (keras Model or str) – model to convert or path to saved .h5 file
function_name (str) – name of main function
malloc (bool) – whether to allocate variables on the stack or heap
num_tests (int) – how many tests to generate in the test suite
verbose (bool) – whether to print progress
- Raises
ValueError – if model is not instance of keras.models.Model
- Returns
None
-
keras2c.keras2c_main.
model2c
(model, function_name, malloc=False, verbose=True)¶ Generates C code for model
Writes main function definition to “function_name.c” and a public header with declarations to “function_name.h”
- Parameters
model (keras Model) – model to convert
function_name (str) – name of C function
malloc (bool) – whether to allocate variables on the stack or heap
verbose (bool) – whether to print info to stdout
- Returns
malloc_vars (list) – names of variables loaded at runtime and stored on the heap stateful (bool): whether the model must maintain state between calls
-
keras2c.keras2c_main.
gen_function_reset
(function_name)¶ Writes a reset function for stateful models
Reset function is used to clear internal state of the model
- Parameters
function_name (str) – name of main function
- Returns
signature (str) – delcaration of the reset function function (str): definition of the reset function
-
keras2c.keras2c_main.
gen_function_initialize
(function_name, malloc_vars)¶ Writes an initialize function
Initialize function is used to load variables into memory and do other start up tasks
- Parameters
function_name (str) – name of main function
malloc_vars (dict) – variables to read in
- Returns
signature (str) – delcaration of the initialization function function (str): definition of the initialization function
-
keras2c.keras2c_main.
gen_function_terminate
(function_name, malloc_vars)¶ Writes a terminate function
Terminate function is used to deallocate memory after completion
- Parameters
function_name (str) – name of main function
malloc_vars (dict) – variables to deallocate
- Returns
signature (str) – delcaration of the terminate function function (str): definition of the terminate function
Writing Layers¶
-
class
keras2c.layer2c.
Layers2C
(model, malloc)¶ Creates an object to parse and write layer functions.
- Parameters
model (keras Model) – model to parse
malloc (bool) – Whether to allocate variables on the heap using malloc.
-
write_layers
(verbose=True)¶ Writes layers in the correct graph order.
- Parameters
verbose (bool) – whether to print progress
- Returns
layers (str) – C code for calling layer functions in correct order
Writing Weights¶
-
class
keras2c.weights2c.
Weights2C
(model, function_name, malloc=False)¶ Creates an object to extract and write weights and other model parameters
- Parameters
model (keras Model) – model to parse
function_name (str) – name of the function being generated
malloc (bool) – Whether to allocate variables on the heap using malloc.
-
static
array2c
(array, name, malloc=False)¶ Generates C code for a k2c_tensor array type
- Parameters
array (array-like) – Python array to write
name (str) – name for the C variable
malloc (bool) – whether to allocate on the heap
- Returns
arr (str) – generated code for the array as a k2c_tensor
-
write_weights
(verbose=True)¶ Parses and generates code for model weights and other parameters
- Parameters
verbose (bool) – whether to print progress
- Returns
(tuple) –
tuple containing
stack_vars (str): code for variables allocated on the stack
- malloc_vars (dict): dictionary of name,value pairs for arrays to be
allocated on the heap
- static_vars (str): code fora C struct containing static variables
(eg, states of a stateful RNN)
Checking Model¶
-
keras2c.check_model.
is_valid_c_name
(name)¶ Checks if a name is a valid name for a C variable or function.
- Parameters
name (str) – name to check
- Returns
valid (bool) – ‘True’ if the name is valid, ‘False’ otherwise
-
keras2c.check_model.
name_check
(model)¶ Checks if all layer names in a model are valid C names.
- Parameters
model (keras Model) – model to check
- Returns
valid (bool) – ‘True’ if all names are valid, ‘False’ otherwise log (str): log of invalid names
-
keras2c.check_model.
layers_supported_check
(model)¶ Checks if all layers in the model are supported
- Parameters
model (keras Model) – model to check
- Returns
valid (bool) – ‘True’ if all layers are supported, ‘False’ otherwise log (str): log of unsupported layers
-
keras2c.check_model.
activation_supported_check
(model)¶ Checks if all activation functions in the model are supported
- Parameters
model (keras Model) – model to check
- Returns
valid (bool) – ‘True’ if all activations are supported, ‘False’ otherwise log (str): log of unsupported activation functions
-
keras2c.check_model.
config_supported_check
(model)¶ Checks if all layer features in the model are supported
- Parameters
model (keras Model) – model to check
- Returns
valid (bool) – ‘True’ if all features are supported, ‘False’ otherwise log (str): log of unsupported features
-
keras2c.check_model.
check_model
(model, function_name)¶ Checks if all names are valid and all features are supported
- Parameters
model (keras Model) – model to check
function_name (str) – name of the function being created
- Raises
AssertionError – If model contains invalid names or unsupported features
Graph Parsing¶
-
keras2c.io_parsing.
layer_type
(layer)¶ Gets the type of a layer
- Parameters
layer (keras Layer) – layer you want the type of
- Returns
type (str) – what kind of layer it is. Eg “Dense”, “Conv2D”, “SimpleRNN”
-
keras2c.io_parsing.
get_all_io_names
(model)¶ Gets names of all node names in the model
- Parameters
model (keras Model) – model to parse
- Returns
io (list) – names of all the nodes in the model
-
keras2c.io_parsing.
get_layer_num_io
(layer)¶ Gets the number of inputs and outputs for a layer
- Parameters
layer (keras Layer) – layer you want to parse
- Returns
num_inputs (int) – number of input nodes to the layer num_outputs (int): number of output nodes from the layer
-
keras2c.io_parsing.
get_layer_io_names
(layer)¶ Gets the names of the inputs and outputs of a layer
- Parameters
layer (keras Layer) – layer you want to parse
- Returns
inputs (list) – names of all the input nodes to the layer outputs (list): names of all the output nodes from the layer
-
keras2c.io_parsing.
get_model_io_names
(model)¶ Gets names of the input and output nodes of the model
- Parameters
model (keras Model) – model to parse
- Returns
inputs (list) – names of all the input nodes outputs (list): names of all the output nodes
-
keras2c.io_parsing.
flatten
(x)¶ Flattens a nested list or tuple
- Parameters
x (list or tuple) – nested list or tuple of lists or tuples to flatten
- Returns
x (list) – flattened input
Test Generation¶
-
keras2c.make_test_suite.
make_test_suite
(model, function_name, malloc_vars, num_tests=10, stateful=False, verbose=True, tol=1e-05)¶ Generates code to test the generated C function.
Generates random inputs to the model, and gets the corresponding predictions for them. Writes input/output pairs to a C file, along with code to call the generated C function and compare the true outputs with the outputs from the generated code.
Writes the test function to a file <function_name>_test_suite.c
- Parameters
model (keras Model) – model being converted to C
function_name (str) – name of the neural net function being generated
malloc_vars (dict) – dictionary of names and values of variables allocated on the heap
num_tests (int) – number of tests to generate
stateful (bool) – whether the model contains layers that maintain state between calls.
verbose (bool) – whether to print output
tol (float) – tolerance for passing tests. Tests pass if the maximum error over all elements between the true output and generated code output is less than tol.
- Returns
None