MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle-Lite / BindLiteTensor

Function BindLiteTensor

lite/api/python/pybind/pybind.cc:317–374  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

315}
316
317void BindLiteTensor(py::module *m) {
318 auto data_size_func = [](const std::vector<int64_t> &shape) -> int64_t {
319 int64_t res = 1;
320 for (size_t i = 0; i < shape.size(); i++) {
321 res *= shape[i];
322 }
323 return res;
324 };
325
326 py::class_<Tensor> tensor(*m, "Tensor");
327
328 tensor.def("resize", &Tensor::Resize)
329 .def("numpy", [](Tensor &self) { return TensorToPyArray(self); })
330 .def("shape", &Tensor::shape)
331 .def("target", &Tensor::target)
332 .def("precision", &Tensor::precision)
333 .def("lod", &Tensor::lod)
334 .def("set_lod", &Tensor::SetLoD)
335 .def("from_numpy",
336 SetTensorFromPyArray,
337 py::arg("array"),
338 py::arg("place") = TargetType::kHost);
339
340#define DO_GETTER_ONCE(data_type__, name__) \
341 tensor.def(#name__, [=](Tensor &self) -> std::vector<data_type__> { \
342 std::vector<data_type__> data; \
343 auto shape = self.shape(); \
344 int64_t num = data_size_func(shape); \
345 data.resize(num); \
346 self.CopyToCpu<data_type__>(data.data()); \
347 return data; \
348 });
349
350#define DO_SETTER_ONCE(data_type__, name__) \
351 tensor.def( \
352 #name__, \
353 [](Tensor &self, \
354 const std::vector<data_type__> &data, \
355 TargetType type = TargetType::kHost) { \
356 if (type == TargetType::kHost || type == TargetType::kARM) { \
357 self.CopyFromCpu<data_type__, TargetType::kHost>(data.data()); \
358 } \
359 }, \
360 py::arg("data"), \
361 py::arg("type") = TargetType::kHost);
362
363#define DATA_GETTER_SETTER_ONCE(data_type__, name__) \
364 DO_SETTER_ONCE(data_type__, set_##name__##_data) \
365 DO_GETTER_ONCE(data_type__, name__##_data)
366
367 DATA_GETTER_SETTER_ONCE(int8_t, int8);
368 DATA_GETTER_SETTER_ONCE(uint8_t, uint8);
369 DATA_GETTER_SETTER_ONCE(int32_t, int32);
370 DATA_GETTER_SETTER_ONCE(float, float);
371#undef DO_GETTER_ONCE
372#undef DO_SETTER_ONCE
373#undef DATA_GETTER_SETTER_ONCE
374}

Callers 1

BindLiteApiFunction · 0.85

Calls 2

TensorToPyArrayFunction · 0.85
sizeMethod · 0.45

Tested by

no test coverage detected