未验证 提交 6d2b2a40 编写于 作者: W Wilber 提交者: GitHub

[API] Refine tensor api. (#4231)

上级 944eb27b
......@@ -846,6 +846,43 @@ for (int i = 0; i < ShapeProduction(input_tensor->shape()); ++i) {
返回类型:`T*`
### `ShareExternalMemory(data, memory_size, target)`
设置Tensor共享用户数据指针。注意:请保证数据指针在预测过程中处于有效状态。
示例:
```c++
lite_api::CxxConfig config
config.set_model_dir(FLAGS_model_dir);
config.set_valid_places({
Place{TARGET(kX86), PRECISION(kFloat)},
Place{TARGET(kARM), PRECISION(kFloat)},
});
auto predictor = lite_api::CreatePaddlePredictor(config);
auto inputs = predictor->GetInputNames();
auto outputs = predictor->GetOutputNames();
std::vector<float> external_data(100 * 100, 0);
auto input_tensor = predictor->GetInputByName(inputs[0]);
input_tensor->Resize(std::vector<int64_t>({100, 100}));
size_t memory_size = external_data.size() * sizeof(float);
input_tensor->ShareExternalMemory(static_cast<void*>(external_data.data()),
memory_size,
config.valid_places()[0].target);
predictor->Run();
```
参数:
- `data(void*)` - 外部数据指针,请确保在预测过程中数据处于有效状态
- `memory_size(size_t)` - 外部数据所占字节大小
- `target(TargetType)` - 目标设备硬件类型,即数据所处设备类型
返回:`None`
返回类型:`void`
### `SetLoD(lod)`
......@@ -872,3 +909,42 @@ for (int i = 0; i < ShapeProduction(input_tensor->shape()); ++i) {
返回:`Tensor`的LoD信息
返回类型:`std::vector<std::vector<uint64_t>>`
### `precision()`
获取Tensor的精度信息
参数:
- `None`
返回:`Tensor`的precision信息
返回类型:`PrecisionType`
### `SetPrecision(precision)`
设置Tensor的精度信息
参数:
- `precision(PrecisionType)` - Tensor的precision信息
返回:`None`
返回类型:`void`
### `target()`
获取Tensor的数据所处设备信息
参数:
- `None`
返回:`Tensor`的target信息
返回类型:`TargetType`
......@@ -83,6 +83,14 @@ const T *Tensor::data() const {
return ctensor(raw_tensor_)->data<T>();
}
void Tensor::ShareExternalMemory(void *data,
size_t memory_size,
TargetType target) {
auto buf =
std::make_shared<lite::Buffer>(lite::Buffer(data, target, memory_size));
tensor(raw_tensor_)->ResetBuffer(buf, memory_size);
}
template <typename T>
T *Tensor::mutable_data(TargetType type) const {
return tensor(raw_tensor_)->mutable_data<T>(type);
......@@ -93,6 +101,7 @@ template const int8_t *Tensor::data<int8_t>() const;
template const uint8_t *Tensor::data<uint8_t>() const;
template const int64_t *Tensor::data<int64_t>() const;
template const int32_t *Tensor::data<int32_t>() const;
template const void *Tensor::data<void>() const;
template int *Tensor::mutable_data(TargetType type) const;
template float *Tensor::mutable_data(TargetType type) const;
......@@ -199,6 +208,10 @@ PrecisionType Tensor::precision() const {
return precision;
}
void Tensor::SetPrecision(PrecisionType precision) {
tensor(raw_tensor_)->set_precision(precision);
}
lod_t Tensor::lod() const { return ctensor(raw_tensor_)->lod(); }
void Tensor::SetLoD(const lod_t &lod) { tensor(raw_tensor_)->set_lod(lod); }
......
......@@ -49,6 +49,11 @@ struct LITE_API Tensor {
template <typename T>
T* mutable_data(TargetType type = TargetType::kHost) const;
// Share external memory. Note: ensure that the data pointer is in a valid
// state
// during the prediction process.
void ShareExternalMemory(void* data, size_t memory_size, TargetType target);
template <typename T, TargetType type = TargetType::kHost>
void CopyFromCpu(const T* data);
......@@ -58,6 +63,7 @@ struct LITE_API Tensor {
shape_t shape() const;
TargetType target() const;
PrecisionType precision() const;
void SetPrecision(PrecisionType precision);
// LoD of the tensor
lod_t lod() const;
......@@ -153,7 +159,7 @@ class LITE_API ConfigBase {
}
// set Device ID
void set_device_id(int device_id) { device_id_ = device_id; }
const int get_device_id() const { return device_id_; }
int get_device_id() const { return device_id_; }
};
/// CxxConfig is the config for the Full feature predictor.
......
......@@ -68,6 +68,42 @@ TEST(CxxApi, run) {
FLAGS_model_dir + ".opt2.naive", LiteModelType::kNaiveBuffer, true);
}
TEST(CxxApi, share_external_data) {
lite_api::CxxConfig config;
config.set_model_dir(FLAGS_model_dir);
config.set_valid_places({
Place{TARGET(kX86), PRECISION(kFloat)},
Place{TARGET(kARM), PRECISION(kFloat)},
});
auto predictor = lite_api::CreatePaddlePredictor(config);
auto inputs = predictor->GetInputNames();
auto outputs = predictor->GetOutputNames();
std::vector<float> external_data(100 * 100);
for (int i = 0; i < 100 * 100; i++) {
external_data[i] = i;
}
auto input_tensor = predictor->GetInputByName(inputs[0]);
input_tensor->Resize(std::vector<int64_t>({100, 100}));
size_t memory_size = 100 * 100 * sizeof(float);
input_tensor->ShareExternalMemory(static_cast<void*>(external_data.data()),
memory_size,
config.valid_places()[0].target);
predictor->Run();
auto output = predictor->GetTensor(outputs[0]);
auto* out = output->data<float>();
LOG(INFO) << out[0];
LOG(INFO) << out[1];
EXPECT_NEAR(out[0], 50.2132, 1e-3);
EXPECT_NEAR(out[1], -28.8729, 1e-3);
}
// Demo1 for Mobile Devices :Load model from file and run
#ifdef LITE_WITH_LIGHT_WEIGHT_FRAMEWORK
TEST(LightApi, run) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册