Skip to content

Commit

Permalink
ort backend support output mutable data (PaddlePaddle#44724)
Browse files Browse the repository at this point in the history
  • Loading branch information
heliqi committed Aug 1, 2022
1 parent fb31506 commit add2a54
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions paddle/fluid/inference/api/details/zero_copy_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ void Tensor::ReshapeStrings(const size_t &shape) {

template <typename T>
T *Tensor::mutable_data(PlaceType place) {
#ifdef PADDLE_WITH_ONNXRUNTIME
if (is_ort_tensor_) {
return ORTGetMutableData<T>();
}
#endif
EAGER_GET_TENSOR(paddle::framework::LoDTensor);
PADDLE_ENFORCE_GT(
tensor->numel(),
Expand Down Expand Up @@ -670,6 +675,17 @@ void Tensor::SetOrtBinding(const std::shared_ptr<Ort::IoBinding> binding) {
binding_ = binding;
}

template <typename T>
T *Tensor::ORTGetMutableData() {
auto binding = binding_.lock();
PADDLE_ENFORCE_NOT_NULL(binding,
paddle::platform::errors::PreconditionNotMet(
"output tensor [%s] no binding ptr", name_));
std::vector<Ort::Value> outputs = binding->GetOutputValues();
Ort::Value &value = outputs[idx_];
return value.GetTensorMutableData<T>();
}

template <typename T>
void Tensor::ORTCopyToCpu(T *data) const {
auto binding = binding_.lock();
Expand Down
3 changes: 3 additions & 0 deletions paddle/fluid/inference/api/paddle_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ class PD_INFER_DECL Tensor {

void SetOrtBinding(const std::shared_ptr<Ort::IoBinding> binding);

template <typename T>
T* ORTGetMutableData();

template <typename T>
void ORTCopyFromCpu(const T* data);

Expand Down

0 comments on commit add2a54

Please sign in to comment.