utils
C++ Converter Utilities
This module provides utility functions to convert numpy arrays and binary files to C++ format.
Functions:
-
array_dump
–Generate C array of values from flattened numpy array.
-
xxd_c_dump
–Generate C array of values from binary file.
Functions
array_dump
array_dump(data: npt.NDArray, dst_path: os.PathLike, var_name: str = 'test_stimulus', var_dtype: str | None = None, row_len: int = 12, is_header: bool = False)
Generate C array of values from flattened numpy array.
Parameters:
-
data
(NDArray
) –Data array
-
dst_path
(PathLike
) –C file destination path
-
var_name
(str
, default:'test_stimulus'
) –C variable name. Defaults to "test_stimulus".
-
var_dtype
(str | None
, default:None
) –C variable type. Defaults to None.
-
row_len
(int
, default:12
) –Elements to write per row. Defaults to 12.
-
is_header
(bool
, default:False
) –Write as header or source C file. Defaults to source.
Example:
data = np.random.rand(10, 10).astype(np.float32)
dst_path = "test_stimulus.h"
nse.converters.cpp.array_dump(
data,
dst_path,
var_name="test_stimulus",
var_dtype="float32_t",
row_len=12,
is_header=True
)
Source code in neuralspot_edge/converters/cpp/utils.py
xxd_c_dump
xxd_c_dump(src_path: os.PathLike, dst_path: os.PathLike, var_name: str = 'model', chunk_len: int = 12, is_header: bool = False)
Generate C like char array of hex values from binary source. Equivalent to xxd -i src_path > dst_path
but with added features to provide # columns and variable name.
Parameters:
-
src_path
(PathLike
) –Binary file source path
-
dst_path
(PathLike
) –C file destination path
-
var_name
(str
, default:'model'
) –C variable name. Defaults to 'model'.
-
chunk_len
(int
, default:12
) –Number of elements per row. Defaults to 12.
-
is_header
(bool
, default:False
) –Write as header or source C file. Defaults to source.
Example:
src_path = "model.tflite"
dst_path = "model.h"
nse.converters.cpp.xxd_c_dump(
src_path,
dst_path,
var_name="model",
chunk_len=12,
is_header=True
)