| 140 | } |
| 141 | |
| 142 | PyObject* CalibrationWrapper::SetTensor(int index, PyObject* value) { |
| 143 | TFLITE_PY_ENSURE_VALID_INTERPRETER(); |
| 144 | |
| 145 | std::unique_ptr<PyObject, PyDecrefDeleter> array_safe( |
| 146 | PyArray_FromAny(value, nullptr, 0, 0, NPY_ARRAY_CARRAY, nullptr)); |
| 147 | if (!array_safe) { |
| 148 | PyErr_SetString(PyExc_ValueError, |
| 149 | "Failed to convert value into readable tensor."); |
| 150 | return nullptr; |
| 151 | } |
| 152 | |
| 153 | PyArrayObject* array = reinterpret_cast<PyArrayObject*>(array_safe.get()); |
| 154 | const TfLiteTensor* tensor = interpreter_->tensor(index); |
| 155 | |
| 156 | if (python_utils::TfLiteTypeFromPyArray(array) != tensor->type) { |
| 157 | PyErr_Format(PyExc_ValueError, |
| 158 | "Cannot set tensor:" |
| 159 | " Got tensor of type %s" |
| 160 | " but expected type %s for input %d, name: %s ", |
| 161 | TfLiteTypeGetName(python_utils::TfLiteTypeFromPyArray(array)), |
| 162 | TfLiteTypeGetName(tensor->type), index, tensor->name); |
| 163 | return nullptr; |
| 164 | } |
| 165 | |
| 166 | if (PyArray_NDIM(array) != tensor->dims->size) { |
| 167 | PyErr_SetString(PyExc_ValueError, "Cannot set tensor: Dimension mismatch"); |
| 168 | return nullptr; |
| 169 | } |
| 170 | |
| 171 | for (int j = 0; j < PyArray_NDIM(array); j++) { |
| 172 | if (tensor->dims->data[j] != PyArray_SHAPE(array)[j]) { |
| 173 | PyErr_SetString(PyExc_ValueError, |
| 174 | "Cannot set tensor: Dimension mismatch"); |
| 175 | return nullptr; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | size_t size = PyArray_NBYTES(array); |
| 180 | if (size != tensor->bytes) { |
| 181 | PyErr_Format(PyExc_ValueError, |
| 182 | "numpy array had %zu bytes but expected %zu bytes.", size, |
| 183 | tensor->bytes); |
| 184 | return nullptr; |
| 185 | } |
| 186 | memcpy(tensor->data.raw, PyArray_DATA(array), size); |
| 187 | Py_RETURN_NONE; |
| 188 | } |
| 189 | |
| 190 | PyObject* CalibrationWrapper::QuantizeModel(int input_py_type, |
| 191 | int output_py_type, |
no test coverage detected