|
| 1 | +#include <stdint.h> |
| 2 | +#include <string.h> |
| 3 | +#include "shared/runtime/context_manager_helpers.h" |
| 4 | +#include "py/obj.h" |
| 5 | +#include "py/objproperty.h" |
| 6 | +#include "py/runtime.h" |
| 7 | +#include "py/runtime0.h" |
| 8 | +#include "shared-bindings/neutonml/Neuton.h" |
| 9 | +#include "shared-module/neutonml/Neuton.h" |
| 10 | +#include "shared-bindings/util.h" |
| 11 | + |
| 12 | +//| .. currentmodule:: neutonml |
| 13 | +//| |
| 14 | +//| :class:`neuton` -- This module provide the wrapper for a newton machine learning library. |
| 15 | +//| ==================================================================================== |
| 16 | +//| |
| 17 | +//| The class definition. |
| 18 | +//| |
| 19 | +//| .. class:: neuton() |
| 20 | +//| |
| 21 | +//| Create an object. |
| 22 | + |
| 23 | +STATIC mp_obj_t neutonml_neuton_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { |
| 24 | + neutonml_neuton_obj_t *self = m_new_obj(neutonml_neuton_obj_t); |
| 25 | + self->base.type = &neutonml_neuton_type; |
| 26 | + shared_module_neutonml_neuton_construct(self); |
| 27 | + return MP_OBJ_FROM_PTR(self); |
| 28 | +} |
| 29 | + |
| 30 | +//| .. method:: deinit() |
| 31 | +//| |
| 32 | +//| Deinitializes the Meaning and releases any hardware resources for reuse. |
| 33 | +//| |
| 34 | +STATIC mp_obj_t neutonml_neuton_deinit(mp_obj_t self_in) { |
| 35 | + shared_module_neutonml_neuton_deinit(self_in); |
| 36 | + return mp_const_none; |
| 37 | +} |
| 38 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_deinit_obj, neutonml_neuton_deinit); |
| 39 | + |
| 40 | +//| .. method:: __enter__() |
| 41 | +//| |
| 42 | +//| No-op used by Context Managers. |
| 43 | +//| |
| 44 | +// Provided by context manager helper. |
| 45 | + |
| 46 | +//| .. method:: __exit__() |
| 47 | +//| |
| 48 | +//| Automatically deinitializes the hardware when exiting a context. See |
| 49 | +//| :ref:`lifetime-and-contextmanagers` for more info. |
| 50 | +//| |
| 51 | +STATIC mp_obj_t neutonml_neuton_obj___exit__(size_t n_args, const mp_obj_t *args) { |
| 52 | + shared_module_neutonml_neuton_deinit(args[0]); |
| 53 | + return mp_const_none; |
| 54 | +} |
| 55 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(neutonml_neuton___exit___obj, 4, 4, neutonml_neuton_obj___exit__); |
| 56 | + |
| 57 | +STATIC mp_obj_t neutonml_neuton_obj_get_inputs_count(mp_obj_t self_in) { |
| 58 | + return mp_obj_new_int(shared_module_neutonml_neuton_model_inputs_count(self_in)); |
| 59 | +} |
| 60 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_inputs_count_obj, neutonml_neuton_obj_get_inputs_count); |
| 61 | + |
| 62 | +STATIC mp_obj_t neutonml_neuton_obj_set_inputs(mp_obj_t self_in, mp_obj_t inputs) { |
| 63 | + |
| 64 | + size_t len; |
| 65 | + int status = 0; |
| 66 | + mp_obj_t *items; |
| 67 | + mp_obj_get_array(inputs, &len, &items); |
| 68 | + status = shared_module_neutonml_neuton_model_set_inputs(self_in, (input_t *)&items); |
| 69 | + return mp_obj_new_int((mp_int_t)status); |
| 70 | +} |
| 71 | +MP_DEFINE_CONST_FUN_OBJ_2(neutonml_neuton_set_inputs_obj, neutonml_neuton_obj_set_inputs); |
| 72 | + |
| 73 | +STATIC mp_obj_t neutonml_neuton_obj_get_inputs_ptr(mp_obj_t self_in) { |
| 74 | + float *inputs; |
| 75 | + |
| 76 | + inputs = shared_module_neutonml_neuton_model_get_inputs_ptr(self_in); |
| 77 | + return MP_OBJ_FROM_PTR(inputs); |
| 78 | +} |
| 79 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_inputs_ptr_obj, neutonml_neuton_obj_get_inputs_ptr); |
| 80 | + |
| 81 | +STATIC mp_obj_t neutonml_neuton_obj_set_ready_flag(mp_obj_t self_in, mp_obj_t dummy) { |
| 82 | + shared_module_neutonml_neuton_model_set_ready_flag(self_in); |
| 83 | + return dummy; |
| 84 | +} |
| 85 | +MP_DEFINE_CONST_FUN_OBJ_2(neutonml_neuton_set_ready_flag_obj, neutonml_neuton_obj_set_ready_flag); |
| 86 | + |
| 87 | +STATIC mp_obj_t neutonml_neuton_obj_get_ready_flag(mp_obj_t self_in) { |
| 88 | + return mp_const_none; |
| 89 | +} |
| 90 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_ready_flag_obj, neutonml_neuton_obj_get_ready_flag); |
| 91 | +MP_PROPERTY_GETSET(neutonml_neuton_ready_flag_obj, |
| 92 | + (mp_obj_t)&neutonml_neuton_get_ready_flag_obj, |
| 93 | + (mp_obj_t)&neutonml_neuton_set_ready_flag_obj); |
| 94 | + |
| 95 | +STATIC mp_obj_t neutonml_neuton_obj_get_reset_inputs(mp_obj_t self_in) { |
| 96 | + shared_module_neutonml_neuton_model_reset_inputs(self_in); |
| 97 | + return mp_const_none; |
| 98 | +} |
| 99 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_reset_inputs_obj, neutonml_neuton_obj_get_reset_inputs); |
| 100 | + |
| 101 | +STATIC mp_obj_t neutonml_neuton_obj_set_reset_inputs(mp_obj_t self_in) { |
| 102 | + shared_module_neutonml_neuton_model_reset_inputs(self_in); |
| 103 | + return mp_const_none; |
| 104 | +} |
| 105 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_set_reset_inputs_obj, neutonml_neuton_obj_set_reset_inputs); |
| 106 | +MP_PROPERTY_GETSET(neutonml_neuton_reset_inputs_obj, |
| 107 | + (mp_obj_t)&neutonml_neuton_reset_inputs_obj, |
| 108 | + (mp_obj_t)&neutonml_neuton_reset_inputs_obj); |
| 109 | + |
| 110 | +STATIC mp_obj_t |
| 111 | +neutonml_neuton_obj_get_outputs_count(mp_obj_t self_in) { |
| 112 | + return mp_obj_new_int(shared_module_neutonml_neuton_model_outputs_count(self_in)); |
| 113 | +} |
| 114 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_outputs_count_obj, neutonml_neuton_obj_get_outputs_count); |
| 115 | +MP_PROPERTY_GETTER(neutonml_neuton_outputs_count_obj, (mp_obj_t)&neutonml_neuton_get_outputs_count_obj); |
| 116 | + |
| 117 | +STATIC mp_obj_t |
| 118 | +neutonml_neuton_obj_run_inference(mp_obj_t self_in, mp_obj_t index, mp_obj_t outputs) { /* |
| 119 | +uint16_t result; |
| 120 | +
|
| 121 | +STATIC const mp_rom_map_elem_t inference_locals_dict_table[] = { |
| 122 | +{MP_ROM_QSTR(MP_QSTR_result), mp_obj_new_int (result))}, |
| 123 | +{MP_ROM_QSTR(MP_QSTR_reset_reason), MP_ROM_PTR(&mcu_processor_reset_reason_obj)}, |
| 124 | +{MP_ROM_QSTR(MP_QSTR_temperature), MP_ROM_PTR(&mcu_processor_temperature_obj)}, |
| 125 | +{MP_ROM_QSTR(MP_QSTR_uid), MP_ROM_PTR(&mcu_processor_uid_obj)}, |
| 126 | +{MP_ROM_QSTR(MP_QSTR_voltage), MP_ROM_PTR(&mcu_processor_voltage_obj)}, |
| 127 | +}; |
| 128 | +
|
| 129 | +result = shared_module_neutonml_neuton_model_run_inference(self_in, &idx, out); |
| 130 | +*/ |
| 131 | + return mp_const_none; |
| 132 | +} |
| 133 | +MP_DEFINE_CONST_FUN_OBJ_3(neutonml_neuton_run_inference_obj, neutonml_neuton_obj_run_inference); |
| 134 | + |
| 135 | +STATIC mp_obj_t neutonml_neuton_obj_get_task_type(mp_obj_t self_in) { |
| 136 | + return mp_obj_new_int(shared_module_neutonml_neuton_model_task_type(self_in)); |
| 137 | +} |
| 138 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_task_type_obj, neutonml_neuton_obj_get_task_type); |
| 139 | +MP_PROPERTY_GETTER(neutonml_neuton_task_type_obj, (mp_obj_t)&neutonml_neuton_get_task_type_obj); |
| 140 | + |
| 141 | +STATIC mp_obj_t neutonml_neuton_obj_get_quantization_level(mp_obj_t self_in) { |
| 142 | + return mp_obj_new_int(shared_module_neutonml_neuton_model_quantization_level(self_in)); |
| 143 | +} |
| 144 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_quantization_level_obj, neutonml_neuton_obj_get_quantization_level); |
| 145 | +MP_PROPERTY_GETTER(neutonml_neuton_quantization_level_obj, (mp_obj_t)&neutonml_neuton_get_quantization_level_obj); |
| 146 | + |
| 147 | +STATIC mp_obj_t neutonml_neuton_obj_get_float_calculations(mp_obj_t self_in) { |
| 148 | + return mp_obj_new_int(shared_module_neutonml_neuton_model_float_calculations(self_in)); |
| 149 | +} |
| 150 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_float_calculations_obj, neutonml_neuton_obj_get_float_calculations); |
| 151 | +MP_PROPERTY_GETTER(neutonml_neuton_float_calculations_obj, (mp_obj_t)&neutonml_neuton_get_float_calculations_obj); |
| 152 | + |
| 153 | +STATIC mp_obj_t neutonml_neuton_obj_get_neurons_count(mp_obj_t self_in) { |
| 154 | + return mp_obj_new_int(shared_module_neutonml_neuton_model_neurons_count(self_in)); |
| 155 | +} |
| 156 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_neurons_count_obj, neutonml_neuton_obj_get_neurons_count); |
| 157 | +MP_PROPERTY_GETTER(neutonml_neuton_neurons_count_obj, (mp_obj_t)&neutonml_neuton_get_neurons_count_obj); |
| 158 | + |
| 159 | +STATIC mp_obj_t neutonml_neuton_obj_get_weights_count(mp_obj_t self_in) { |
| 160 | + return mp_obj_new_int(shared_module_neutonml_neuton_model_weights_count(self_in)); |
| 161 | +} |
| 162 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_weights_count_obj, neutonml_neuton_obj_get_weights_count); |
| 163 | +MP_PROPERTY_GETTER(neutonml_neuton_weights_count_obj, (mp_obj_t)&neutonml_neuton_get_weights_count_obj); |
| 164 | + |
| 165 | +STATIC mp_obj_t neutonml_neuton_obj_get_inputs_limits_count(mp_obj_t self_in) { |
| 166 | + return mp_obj_new_int(shared_module_neutonml_neuton_model_inputs_limits_count(self_in)); |
| 167 | +} |
| 168 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_inputs_limits_count_obj, neutonml_neuton_obj_get_inputs_limits_count); |
| 169 | +MP_PROPERTY_GETTER(neutonml_neuton_inputs_limits_count_obj, (mp_obj_t)&neutonml_neuton_get_inputs_limits_count_obj); |
| 170 | + |
| 171 | +STATIC mp_obj_t neutonml_neuton_obj_get_window_size(mp_obj_t self_in) { |
| 172 | + return mp_obj_new_int(shared_module_neutonml_neuton_model_window_size(self_in)); |
| 173 | +} |
| 174 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_window_size_obj, neutonml_neuton_obj_get_window_size); |
| 175 | +MP_PROPERTY_GETTER(neutonml_neuton_window_size_obj, (mp_obj_t)&neutonml_neuton_get_window_size_obj); |
| 176 | + |
| 177 | +STATIC mp_obj_t neutonml_neuton_obj_get_ram_usage(mp_obj_t self_in) { |
| 178 | + return mp_obj_new_int(shared_module_neutonml_neuton_model_ram_usage(self_in)); |
| 179 | +} |
| 180 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_ram_usage_obj, neutonml_neuton_obj_get_ram_usage); |
| 181 | +MP_PROPERTY_GETTER(neutonml_neuton_ram_usage_obj, (mp_obj_t)&neutonml_neuton_get_ram_usage_obj); |
| 182 | + |
| 183 | +STATIC mp_obj_t neutonml_neuton_obj_get_size(mp_obj_t self_in) { |
| 184 | + return mp_obj_new_int(shared_module_neutonml_neuton_model_size(self_in)); |
| 185 | +} |
| 186 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_size_obj, neutonml_neuton_obj_get_size); |
| 187 | +MP_PROPERTY_GETTER(neutonml_neuton_size_obj, (mp_obj_t)&neutonml_neuton_get_size_obj); |
| 188 | + |
| 189 | +STATIC mp_obj_t neutonml_neuton_obj_get_size_with_meta(mp_obj_t self_in) { |
| 190 | + return mp_obj_new_int(shared_module_neutonml_neuton_model_size_with_meta(self_in)); |
| 191 | +} |
| 192 | +MP_DEFINE_CONST_FUN_OBJ_1(neutonml_neuton_get_size_with_meta_obj, neutonml_neuton_obj_get_size_with_meta); |
| 193 | +MP_PROPERTY_GETTER(neutonml_neuton_size_with_meta_obj, (mp_obj_t)&neutonml_neuton_get_size_with_meta_obj); |
| 194 | + |
| 195 | +STATIC const mp_rom_map_elem_t neutonml_neuton_locals_dict_table[] = { |
| 196 | + // Methods |
| 197 | + {MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&neutonml_neuton_deinit_obj)}, |
| 198 | + {MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj)}, |
| 199 | + {MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&neutonml_neuton___exit___obj)}, |
| 200 | + {MP_ROM_QSTR(MP_QSTR_get_inputs_count), MP_ROM_PTR(&neutonml_neuton_get_inputs_count_obj)}, |
| 201 | + {MP_ROM_QSTR(MP_QSTR_set_inputs), MP_ROM_PTR(&neutonml_neuton_set_inputs_obj)}, |
| 202 | + {MP_ROM_QSTR(MP_QSTR_set_ready_flag), MP_ROM_PTR(&neutonml_neuton_set_ready_flag_obj)}, |
| 203 | + {MP_ROM_QSTR(MP_QSTR_reset_inputs), MP_ROM_PTR(&neutonml_neuton_reset_inputs_obj)}, |
| 204 | + {MP_ROM_QSTR(MP_QSTR_outputs_count), MP_ROM_PTR(&neutonml_neuton_outputs_count_obj)}, |
| 205 | + {MP_ROM_QSTR(MP_QSTR_inference), MP_ROM_PTR(&neutonml_neuton_run_inference_obj)}, |
| 206 | + {MP_ROM_QSTR(MP_QSTR_task_type), MP_ROM_PTR(&neutonml_neuton_task_type_obj)}, |
| 207 | + {MP_ROM_QSTR(MP_QSTR_quantization_level), MP_ROM_PTR(&neutonml_neuton_quantization_level_obj)}, |
| 208 | + {MP_ROM_QSTR(MP_QSTR_float_calculations), MP_ROM_PTR(&neutonml_neuton_float_calculations_obj)}, |
| 209 | + {MP_ROM_QSTR(MP_QSTR_neurons_count), MP_ROM_PTR(&neutonml_neuton_neurons_count_obj)}, |
| 210 | + {MP_ROM_QSTR(MP_QSTR_weights_count), MP_ROM_PTR(&neutonml_neuton_weights_count_obj)}, |
| 211 | + {MP_ROM_QSTR(MP_QSTR_inputs_limits_count), MP_ROM_PTR(&neutonml_neuton_inputs_limits_count_obj)}, |
| 212 | + {MP_ROM_QSTR(MP_QSTR_window_size), MP_ROM_PTR(&neutonml_neuton_window_size_obj)}, |
| 213 | + {MP_ROM_QSTR(MP_QSTR_ram_usage), MP_ROM_PTR(&neutonml_neuton_ram_usage_obj)}, |
| 214 | + {MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&neutonml_neuton_size_obj)}, |
| 215 | + {MP_ROM_QSTR(MP_QSTR_size_with_meta), MP_ROM_PTR(&neutonml_neuton_size_with_meta_obj)}, |
| 216 | +}; |
| 217 | + |
| 218 | +STATIC MP_DEFINE_CONST_DICT(neutonml_neuton_locals_dict, neutonml_neuton_locals_dict_table); |
| 219 | + |
| 220 | +const mp_obj_type_t neutonml_neuton_type = { |
| 221 | + {&mp_type_type}, |
| 222 | + .name = MP_QSTR_Meaning, |
| 223 | + .make_new = neutonml_neuton_make_new, |
| 224 | + .locals_dict = (mp_obj_dict_t *)&neutonml_neuton_locals_dict, |
| 225 | +}; |
0 commit comments