| 38 | { |
| 39 | |
| 40 | py::array numpyArray(const Tensor *tensor) |
| 41 | { |
| 42 | assert(tensor != nullptr); // FIX_CALLER_UNLESS |
| 43 | |
| 44 | const auto tensor_shape = tensor->shape(); |
| 45 | |
| 46 | uint32_t size = 1; |
| 47 | std::vector<uint32_t> shape(tensor_shape.num_dims()); |
| 48 | for (int i = 0; i < tensor_shape.num_dims(); i++) |
| 49 | { |
| 50 | THROW_UNLESS(tensor_shape.dim(i) >= 0, "Negative dimension detected in " + tensor->name()); |
| 51 | |
| 52 | shape[i] = tensor_shape.dim(i); |
| 53 | size *= shape[i]; |
| 54 | } |
| 55 | |
| 56 | if (size == 0) |
| 57 | return py::none(); |
| 58 | |
| 59 | switch (tensor->element_type()) |
| 60 | { |
| 61 | case loco::DataType::FLOAT32: |
| 62 | return py::array_t<float, py::array::c_style>(shape, tensor->data<float>()); |
| 63 | case loco::DataType::S16: |
| 64 | return py::array_t<int16_t, py::array::c_style>(shape, tensor->data<int16_t>()); |
| 65 | case loco::DataType::S32: |
| 66 | return py::array_t<int32_t, py::array::c_style>(shape, tensor->data<int32_t>()); |
| 67 | case loco::DataType::S64: |
| 68 | return py::array_t<int64_t, py::array::c_style>(shape, tensor->data<int64_t>()); |
| 69 | case loco::DataType::U8: |
| 70 | return py::array_t<uint8_t, py::array::c_style>(shape, tensor->data<uint8_t>()); |
| 71 | default: |
| 72 | throw std::runtime_error("Unsupported data type"); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | py::dict quantparam(const Tensor *tensor) |
| 77 | { |