25namespace py = pybind11;
52 if (std::strcmp(layout,
"NCHW") == 0)
54 else if (std::strcmp(layout,
"NHWC") == 0)
56 else if (std::strcmp(layout,
"NONE") == 0)
59 throw NnfwError(std::string(
"Unknown layout type: '") + layout +
"'");
64 if (std::strcmp(type,
"float32") == 0)
66 else if (std::strcmp(type,
"int32") == 0)
68 else if (std::strcmp(type,
"bool") == 0)
69 return NNFW_TYPE::NNFW_TYPE_TENSOR_UINT8;
70 else if (std::strcmp(type,
"bool") == 0)
71 return NNFW_TYPE::NNFW_TYPE_TENSOR_BOOL;
72 else if (std::strcmp(type,
"int64") == 0)
73 return NNFW_TYPE::NNFW_TYPE_TENSOR_INT64;
74 else if (std::strcmp(type,
"int8") == 0)
75 return NNFW_TYPE::NNFW_TYPE_TENSOR_QUANT8_ASYMM_SIGNED;
76 else if (std::strcmp(type,
"int16") == 0)
77 return NNFW_TYPE::NNFW_TYPE_TENSOR_QUANT16_SYMM_SIGNED;
79 throw NnfwError(std::string(
"Cannot convert string to NNFW_TYPE: '") + type +
"'");
90 case NNFW_TYPE::NNFW_TYPE_TENSOR_QUANT8_ASYMM:
91 case NNFW_TYPE::NNFW_TYPE_TENSOR_UINT8:
93 case NNFW_TYPE::NNFW_TYPE_TENSOR_BOOL:
95 case NNFW_TYPE::NNFW_TYPE_TENSOR_INT64:
97 case NNFW_TYPE::NNFW_TYPE_TENSOR_QUANT8_ASYMM_SIGNED:
99 case NNFW_TYPE::NNFW_TYPE_TENSOR_QUANT16_SYMM_SIGNED:
102 throw NnfwError(std::string(
"Cannot convert NNFW_TYPE enum to string (value=") +
103 std::to_string(
static_cast<int>(type)) +
")");
110 for (int32_t i = 0; i < tensor_info->
rank; ++i)
112 n *= tensor_info->
dims[i];
120 for (int32_t i = 0; i < tensor_info.
rank; ++i)
122 dims_list.append(tensor_info.
dims[i]);
129 tensor_info.
rank = py::len(array);
130 for (int32_t i = 0; i < tensor_info.
rank; ++i)
132 tensor_info.
dims[i] = py::cast<int32_t>(array[i]);
138 this->session =
nullptr;
154 this->session =
nullptr;
224 const void *out_buffer =
nullptr;
228 size_t num_elements = 1;
229 std::vector<ssize_t> shape;
230 shape.reserve(out_info.
rank);
231 for (
int i = 0; i < out_info.
rank; ++i)
233 shape.push_back(
static_cast<ssize_t
>(out_info.
dims[i]));
234 num_elements *=
static_cast<size_t>(out_info.
dims[i]);
238 auto np = py::module_::import(
"numpy");
239 py::dtype dt = np.attr(
"dtype")(py::str(
getStringType(out_info.
dtype))).cast<py::dtype>();
240 size_t itemsize = dt.attr(
"itemsize").cast<
size_t>();
242 py::array arr(dt, shape);
243 std::memcpy(arr.mutable_data(), out_buffer, num_elements * itemsize);
244 arr.attr(
"flags").attr(
"writeable") =
false;
288 const char *c_str_path = path.cast<std::string>().c_str();
294 const char *c_str_path = path.cast<std::string>().c_str();
300 const char *c_str_path = path.cast<std::string>().c_str();
nnfw_train_info train_get_traininfo()
void train_export_circle(const py::str &path)
py::array get_output(uint32_t index)
tensorinfo input_tensorinfo(uint32_t index)
void train_import_checkpoint(const py::str &path)
void set_input_tensorinfo(uint32_t index, const tensorinfo *tensor_info)
void train(bool update_weights)
void set_input_layout(uint32_t index, const char *layout)
float train_get_loss(uint32_t index)
void train_set_traininfo(const nnfw_train_info *info)
void set_prepare_config(NNFW_PREPARE_CONFIG config)
tensorinfo output_tensorinfo(uint32_t index)
NNFW_SESSION(const char *package_file_path, const char *backends)
void train_export_checkpoint(const py::str &path)
volatile const char info[]
void set_dims(tensorinfo &tensor_info, const py::list &array)
Set nnfw_tensorinfo->dims.
const char * getStringType(NNFW_TYPE type)
NNFW_TYPE getType(const char *type="")
void ensure_status(NNFW_STATUS status)
Handle errors with NNFW_STATUS in API functions.
NNFW_LAYOUT getLayout(const char *layout="")
uint64_t num_elems(const nnfw_tensorinfo *tensor_info)
Get the total number of elements in nnfw_tensorinfo->dims.
py::list get_dims(const tensorinfo &tensor_info)
Get nnfw_tensorinfo->dims.
NNFW_STATUS nnfw_await(nnfw_session *session)
Wait for asynchronous run to finish.
NNFW_STATUS nnfw_run_async(nnfw_session *session)
Run inference asynchronously.
NNFW_STATUS nnfw_output_tensorinfo(nnfw_session *session, uint32_t index, nnfw_tensorinfo *tensor_info)
Get i-th output tensor info.
NNFW_STATUS nnfw_set_available_backends(nnfw_session *session, const char *backends)
Set available backends.
NNFW_STATUS nnfw_input_tensorinfo(nnfw_session *session, uint32_t index, nnfw_tensorinfo *tensor_info)
Get i-th input tensor info.
NNFW_STATUS nnfw_output_size(nnfw_session *session, uint32_t *number)
Get the number of outputs.
NNFW_STATUS nnfw_input_size(nnfw_session *session, uint32_t *number)
Get the number of inputs.
NNFW_STATUS nnfw_set_input_tensorinfo(nnfw_session *session, uint32_t index, const nnfw_tensorinfo *tensor_info)
Set input model's tensor info for resizing.
NNFW_STATUS nnfw_run(nnfw_session *session)
Run inference.
NNFW_STATUS nnfw_prepare(nnfw_session *session)
Prepare session to be ready for inference.
NNFW_LAYOUT
Data format of a tensor.
@ NNFW_LAYOUT_CHANNELS_LAST
@ NNFW_LAYOUT_CHANNELS_FIRST
NNFW_STATUS nnfw_set_input_layout(nnfw_session *session, uint32_t index, NNFW_LAYOUT layout)
Set the layout of an input.
NNFW_STATUS nnfw_set_prepare_config(nnfw_session *session, NNFW_PREPARE_CONFIG key, const char *value)
Set prepare configuration.
NNFW_STATUS nnfw_train_get_traininfo(nnfw_session *session, nnfw_train_info *info)
Get training information.
NNFW_PREPARE_CONFIG
Configuration key for prepare (compile and schedule)
NNFW_STATUS nnfw_get_output(nnfw_session *session, uint32_t index, nnfw_tensorinfo *out_info, const void **out_buffer)
Python-binding-only API to retrieve a read-only output buffer and its tensor info.
NNFW_STATUS nnfw_create_session(nnfw_session **session)
Create a new session instance.
NNFW_STATUS
Result values returned from a call to an API function.
@ NNFW_STATUS_INVALID_STATE
@ NNFW_STATUS_UNEXPECTED_NULL
@ NNFW_STATUS_DEPRECATED_API
@ NNFW_STATUS_INSUFFICIENT_OUTPUT_SIZE
@ NNFW_STATUS_OUT_OF_MEMORY
NNFW_STATUS nnfw_close_session(nnfw_session *session)
Close a session instance.
NNFW_STATUS nnfw_train_get_loss(nnfw_session *session, uint32_t index, float *loss)
Get loss value for expected output.
NNFW_STATUS nnfw_train_export_checkpoint(nnfw_session *session, const char *path)
NNFW_STATUS nnfw_load_model_from_file(nnfw_session *session, const char *package_file_path)
Load model from nnpackage file or directory.
NNFW_STATUS nnfw_train_prepare(nnfw_session *session)
Prepare session to be ready for training.
NNFW_STATUS nnfw_train(nnfw_session *session, bool update_weights)
Train the model.
NNFW_STATUS nnfw_train_set_traininfo(nnfw_session *session, const nnfw_train_info *info)
Set training information.
NNFW_STATUS nnfw_train_import_checkpoint(nnfw_session *session, const char *path)
@ NNFW_TYPE_TENSOR_FLOAT32
#define NNFW_MAX_RANK
Maximum rank expressible with nnfw.
NNFW_STATUS nnfw_train_export_circle(nnfw_session *session, const char *path)
Export current training model into circle model.
tensor info describes the type and shape of tensors
int32_t dims[NNFW_MAX_RANK]
Training information to prepare training.
tensor info describes the type and shape of tensors
int32_t dims[NNFW_MAX_RANK]