/* Copyright (c) 2021 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. */ #include "paddle/pten/api/lib/utils/tensor_utils.h" #include #include #include "paddle/pten/core/compat_utils.h" namespace paddle { namespace experimental { template void SetLoD(DstLoD* dst, const SrcLoD& src) { dst->reserve(src.size()); dst->clear(); for (auto&& v : src) { dst->emplace_back(v); } } std::unique_ptr MakePtenDenseTensor( const paddle::framework::Tensor& src) { return std::make_unique(src); } pten::Scalar MakePtenScalar(const paddle::framework::Tensor& src) { PADDLE_ENFORCE_EQ(src.numel(), 1, paddle::platform::errors::InvalidArgument( "The Scalar only supports Tensor with 1 element, " "but now Tensor has %d element.", src.numel())); switch (src.type()) { case paddle::framework::proto::VarType::FP32: return {src.template data()[0]}; case paddle::framework::proto::VarType::FP64: return {src.template data()[0]}; case paddle::framework::proto::VarType::FP16: return {src.template data()[0]}; case paddle::framework::proto::VarType::BF16: return {src.template data()[0]}; case paddle::framework::proto::VarType::INT32: return {src.template data()[0]}; case paddle::framework::proto::VarType::INT64: return {src.template data()[0]}; case paddle::framework::proto::VarType::INT16: return {src.template data()[0]}; case paddle::framework::proto::VarType::INT8: return {src.template data()[0]}; case paddle::framework::proto::VarType::UINT8: return {src.template data()[0]}; case paddle::framework::proto::VarType::BOOL: return {src.template data()[0]}; case paddle::framework::proto::VarType::COMPLEX64: return {src.template data()[0]}; case paddle::framework::proto::VarType::COMPLEX128: return {src.template data()[0]}; default: PADDLE_THROW(paddle::platform::errors::InvalidArgument( "Data type error. Don't support casting a %d LoDTensor to Scalar.", src.type())); } } pten::Scalar MakePtenScalarFromVar(const framework::Variable& variable) { auto expected_place = pten::TransToFluidPlace(pten::Backend::CPU); if (variable.IsType()) { const auto& tensor = variable.Get(); if (!platform::is_same_place(tensor.place(), expected_place)) { framework::LoDTensor tmp_tensor; framework::TensorCopySync(tensor, expected_place, &tmp_tensor); return MakePtenScalar(tmp_tensor); } else { return MakePtenScalar(tensor); } } else { PADDLE_THROW(platform::errors::Unimplemented( "Unsupport casting input `%s` type to Scalar when call pt " "kernel.", framework::ToTypeName(variable.Type()))); } } pten::ScalarArray MakePtenScalarArray(const paddle::framework::Tensor& src) { if (src.type() == paddle::framework::proto::VarType::INT64) { return {src.data(), src.numel()}; } else if (src.type() == paddle::framework::proto::VarType::INT32) { return {src.data(), src.numel()}; } else { PADDLE_THROW(paddle::platform::errors::InvalidArgument( "Data type error. When cast a LoDTensor to ScalarArray, " "the data type of LoDTensor must be int32 or int64, " "but now data type is %s.", src.type())); } } pten::ScalarArray MakePtenScalarArrayFromVar( const framework::Variable& variable) { auto expected_place = pten::TransToFluidPlace(pten::Backend::CPU); if (variable.IsType()) { const auto& tensor = variable.Get(); if (!platform::is_same_place(tensor.place(), expected_place)) { framework::LoDTensor tmp_tensor; framework::TensorCopySync(tensor, expected_place, &tmp_tensor); return MakePtenScalarArray(tmp_tensor); } else { return MakePtenScalarArray(tensor); } } else { PADDLE_THROW(platform::errors::Unimplemented( "Unsupport casting input `%s` type to ScalarArray when call pt " "kernel.", framework::ToTypeName(variable.Type()))); } } pten::ScalarArray MakePtenScalarArrayFromVarList( const std::vector& variable_list) { if (variable_list.size() == 0) { return pten::ScalarArray(); } auto expected_place = pten::TransToFluidPlace(pten::Backend::CPU); paddle::framework::proto::VarType::Type data_type; auto* first_var = variable_list.front(); if (first_var->IsType()) { const auto& tensor = first_var->Get(); data_type = tensor.type(); } else { PADDLE_THROW(platform::errors::Unimplemented( "Unsupport casting input `%s` type to VectorTensor when call pt " "kernel.", framework::ToTypeName(first_var->Type()))); } std::vector vector_data; vector_data.reserve(variable_list.size()); if (data_type == paddle::framework::proto::VarType::INT64) { for (auto* var : variable_list) { if (var->IsType()) { const auto& tensor = var->Get(); if (!platform::is_same_place(tensor.place(), expected_place)) { framework::LoDTensor tmp_tensor; framework::TensorCopySync(tensor, expected_place, &tmp_tensor); vector_data.push_back(*tmp_tensor.data()); } else { vector_data.push_back(*tensor.data()); } } else { PADDLE_THROW(platform::errors::Unimplemented( "Unsupport casting input `%s` type to VectorTensor when call pt " "kernel.", framework::ToTypeName(var->Type()))); } } } else if (data_type == paddle::framework::proto::VarType::INT32) { for (auto* var : variable_list) { if (var->IsType()) { const auto& tensor = var->Get(); if (!platform::is_same_place(tensor.place(), expected_place)) { framework::LoDTensor tmp_tensor; framework::TensorCopySync(tensor, expected_place, &tmp_tensor); vector_data.push_back(*tmp_tensor.data()); } else { vector_data.push_back(*tensor.data()); } } else { PADDLE_THROW(platform::errors::Unimplemented( "Unsupport casting input `%s` type to VectorTensor when call pt " "kernel.", framework::ToTypeName(var->Type()))); } } } else { PADDLE_THROW(paddle::platform::errors::InvalidArgument( "Data type error. When cast a LoDTensor to VectorTensor, " "the data type of LoDTensor must be int32 or int64, " "but now data type is %s.", data_type)); } return {vector_data}; } void ResetTensorByArgDef(pten::DenseTensor* dst, const pten::TensorArgDef& arg_def) { VLOG(5) << "ResetTensor by TensorArgDef."; auto* meta = pten::CompatibleDenseTensorUtils::GetMutableMeta(dst); meta->dtype = arg_def.dtype; meta->layout = arg_def.layout; } } // namespace experimental } // namespace paddle