# Functions¶

In general, several functions are implemented for each primitive supported by MLI library. Each function (implementation of primitive) is designed to deal with specific inputs. Therefore, you must meet the assumptions that functions make. For example, function designed to perform 2D convolution for data of fx8 type must not be used with data of fx16 type.

All assumptions are reflected in function name according to naming convention (see MLI Library Functions Naming Convention and MLI Library Functions Naming - Specialization Details). MLI Library functions have at least one assumption on input data types. Functions with only data-type assumption are referred to as generic functions while functions with additional assumptions referred to as specialized functions or specializations.

Note

A lot of specializations along with generic functions are implemented in convolution and pooling groups for each primitive. Generic functions are typically slower than the specialized ones. Hence, a function without postfix performs switching logic to choose the correct specialized function or a generic function if there is no appropriate specialization. Such ‘switchers’ significantly increase the code size of application and should be used only in development or intentionally. Generic functions have a ‘_generic’ name postfix, and specializations have a descriptive postfix.

## Naming Convention¶

MLI Library function adheres naming convention listed in MLI Library Functions Naming Convention:

mli_<set>_<type>_[layout]_<data_type>_[spec](<in_data>,[config],<out_data>) ;

MLI Library Functions Naming Convention
Field name Field Entries Field Description
set

krn

hlp

Mandatory. Specifies set of functions related to the implementation. For more information, see Library Model
type

conv2d

fully_connected

Mandatory. Specifies particular type of primitive supported by the library
layout

chw

hwc

Optional. Specifies data layout for image-like inputs. For more information, see Data Types
data_type

fx8

fx16

fx8w16d

Mandatory. Specifies the tensor basic element type expected by the function. fx8w16d means weights and bias tensors are 8-bit, while all the others are 16-bit. For more information, see MLI Fixed-Point Data Format
spec   Optional. Reflects additional assumptions of function. For example, if the function can only process convolutions of a 3x3 kernel, this should be reflected in this field (see MLI Library Functions Naming - Specialization Details)
in_data   Mandatory. Input data tensors
config   Optional. Structure of primitive-specific parameters
out_data   Mandatory. Output data tensors

Note

Example:

mli_krn_avepool_hwc_fx8(const mli_tensor *in,
const mli_pool_cfg *cfg,
mli_tensor *out
);


## Specialized Functions¶

Naming convention for the specializations:

MLI Library Functions Naming - Specialization Details
Configuration parameter Naming convention Relevant for
Kernel size

[_knxm]

where n and m are the kernel dimensions example: _k1x1, _k3x3. One of dimension might be left unfixed example _k1xn

convolution group, pooling group
Padding

Where _nopad functions assumes that all padding parameters are zeros, and _krnpad functions assumes smallest padding parameters to achieve same output size (similar to ‘SAME’ padding scheme used in TensorFlow)

convolution group, pooling group
Input channels

[_chn]

where n is the number of channels example _ch1, _ch4

convolution group, pooling group
Stride

[_str[h|w]n]

where n is the stride value, if needed h or w can be used if horizontal stride is different from vertical if omitted, both strides are equal. Example: _str1, _strh2_strw1

convolution group, pooling group
Generalization

[_generic]

If there are a lot of specializations for a primitive, _generic functions can process inputs with any combinations of parameters. Unspecialized functions (without [_spec] field in name) behave as “switches” which analyze inputs and choose suitable specialization. Switch chooses _generic version in case there are no suitable specializations.

convolution group, pooling group

For example, the function name of a 16bit 2d convolution kernel with CHW layout and a kernel size of 3x3 and stride of 1 is: mli_krn_conv2d_chw_fx16_k3x3_str1().