/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #pragma once #include #include #include namespace paddle { namespace operators { template inline std::vector GetDataFromTensor(const framework::Tensor* x) { auto* data = x->data(); framework::Tensor cpu_attr_tensor; if (platform::is_gpu_place(x->place())) { TensorCopySync(*x, platform::CPUPlace(), &cpu_attr_tensor); data = cpu_attr_tensor.data(); } auto vec_data = std::vector(data, data + x->numel()); return vec_data; } template inline std::vector GetDataFromTensorList( const std::vector& list_tensor) { std::vector vec_new_data; for (size_t i = 0; i < list_tensor.size(); ++i) { auto tensor = list_tensor[i]; PADDLE_ENFORCE_EQ( tensor->dims(), framework::make_ddim({1}), "ShapeError: If the element type is Tensor, " "the element's shape must be [1]. But received the element's shape " "is [%s]", tensor->dims()); if (platform::is_gpu_place(tensor->place())) { framework::Tensor temp; TensorCopySync(*tensor, platform::CPUPlace(), &temp); vec_new_data.push_back((*temp.data())); } else { vec_new_data.push_back((*tensor->data())); } } return vec_new_data; } } // namespace operators } // namespace paddle