From 75c4a24ca32ac2c07d7e271a5d15ddbc52df1b6c Mon Sep 17 00:00:00 2001 From: zhangbo9674 <82555433+zhangbo9674@users.noreply.github.com> Date: Wed, 6 Sep 2023 14:09:06 +0800 Subject: [PATCH] [IR] Add IrMetaTensor (#56973) * add meta tensor * refine code * fix bug * fix bug --- paddle/fluid/framework/type_info.cc | 2 + .../ir/dialect/op_generator/op_build_gen.py | 24 +++--- .../fluid/ir/dialect/op_generator/op_gen.py | 1 + .../dialect/paddle_dialect/ir/CMakeLists.txt | 2 +- .../paddle_dialect/ir/pd_meta_tensor.cc | 68 ++++++++++++++++ .../paddle_dialect/ir/pd_meta_tensor.h | 80 +++++++++++++++++++ paddle/phi/core/meta_tensor.cc | 3 + paddle/phi/core/utils/type_info.cc | 2 + 8 files changed, 168 insertions(+), 14 deletions(-) create mode 100644 paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.cc create mode 100644 paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h diff --git a/paddle/fluid/framework/type_info.cc b/paddle/fluid/framework/type_info.cc index 442800d035f..cb7dae540d1 100644 --- a/paddle/fluid/framework/type_info.cc +++ b/paddle/fluid/framework/type_info.cc @@ -16,6 +16,7 @@ limitations under the License. */ #include "paddle/fluid/framework/feed_fetch_type.h" #include "paddle/fluid/framework/raw_tensor.h" #include "paddle/fluid/framework/string_array.h" +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h" #include "paddle/fluid/prim/utils/static/desc_tensor.h" #include "paddle/fluid/primitive/type/lazy_tensor.h" @@ -44,5 +45,6 @@ template class TypeInfoTraits; template class TypeInfoTraits; template class TypeInfoTraits; +template class TypeInfoTraits; } // namespace phi diff --git a/paddle/fluid/ir/dialect/op_generator/op_build_gen.py b/paddle/fluid/ir/dialect/op_generator/op_build_gen.py index d9747b47e87..d36c2696483 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_build_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_build_gen.py @@ -286,27 +286,25 @@ def GenBuildOutputs( build_output_str = ' VLOG(4) << "Builder construction outputs";\n' CREATE_INPUT_METATENSOR_TEMPLATE = """ VLOG(4) << "Builder construction dense_{name}"; - phi::DenseTensor dense_{name}(std::make_unique(paddle::platform::CPUPlace()).get(), - phi::DenseTensorMeta(paddle::dialect::TransToPhiDataType({name}.dtype()), - {name}.dims(), - {name}.data_layout(), - {name}.lod(), - {name}.offset())); + paddle::dialect::IrMetaTensor ir_meta_tensor_{name}(paddle::dialect::TransToPhiDataType({name}.dtype()), + {name}.dims(), + {name}.data_layout(), + {name}.lod(), + {name}.offset()); VLOG(4) << "Builder construction meta_{name}"; - phi::MetaTensor meta_{name}(&dense_{name}); + phi::MetaTensor meta_{name}(&ir_meta_tensor_{name}); """ - CREATE_INPUT_VEC_METATENSOR_TEMPLATE = """ std::vector vec_dense_{name}; + CREATE_INPUT_VEC_METATENSOR_TEMPLATE = """ std::vector vec_ir_meta_tensor_{name}; for (size_t i=0; i < static_cast({name}.size()); i++) {{ - vec_dense_{name}.push_back(phi::DenseTensor(std::make_unique(paddle::platform::CPUPlace()).get(), - phi::DenseTensorMeta(paddle::dialect::TransToPhiDataType({name}[i].dyn_cast().dtype()), + vec_ir_meta_tensor_{name}.push_back(paddle::dialect::IrMetaTensor(paddle::dialect::TransToPhiDataType({name}[i].dyn_cast().dtype()), {name}[i].dyn_cast().dims(), {name}[i].dyn_cast().data_layout(), {name}[i].dyn_cast().lod(), - {name}[i].dyn_cast().offset()))); + {name}[i].dyn_cast().offset())); }} std::vector vec_meta_{name}; - for (size_t i=0; i < vec_dense_{name}.size(); i++) {{ - vec_meta_{name}.push_back(phi::MetaTensor(&vec_dense_{name}[i])); + for (size_t i=0; i < vec_ir_meta_tensor_{name}.size(); i++) {{ + vec_meta_{name}.push_back(phi::MetaTensor(&vec_ir_meta_tensor_{name}[i])); }} std::vector meta_{name}; diff --git a/paddle/fluid/ir/dialect/op_generator/op_gen.py b/paddle/fluid/ir/dialect/op_generator/op_gen.py index 1c19874dd9b..7ee65d05058 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_gen.py @@ -101,6 +101,7 @@ CC_FILE_TEMPLATE = """// This file is generated by "paddle/fluid/ir/dialect/op_g #include "{h_file}" #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_type.h" #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_attribute.h" +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h" #include "paddle/ir/core/builtin_attribute.h" #include "paddle/ir/core/builtin_type.h" #include "paddle/ir/core/builtin_op.h" diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt b/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt index cc8d1357bf0..08cc463c34c 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt @@ -183,7 +183,7 @@ add_custom_target(ops_api_gen ALL DEPENDS ${ops_api_source_file}) cc_library( pd_dialect_core - SRCS pd_attribute.cc pd_type.cc + SRCS pd_attribute.cc pd_type.cc pd_meta_tensor.cc DEPS phi pd_interface pd_trait type_info) cc_library( pd_dialect_op diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.cc b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.cc new file mode 100644 index 00000000000..2da7b098a65 --- /dev/null +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.cc @@ -0,0 +1,68 @@ +// Copyright (c) 2023 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/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h" + +#include "paddle/ir/core/enforce.h" + +namespace paddle { +namespace dialect { +IrMetaTensor::IrMetaTensor(phi::DataType dtype, + const phi::DDim& dims, + phi::DataLayout layout, + const LoD& lod, + size_t offset) + : dims_(dims), dtype_(dtype), layout_(layout), lod_(lod), offset_(offset) {} + +IrMetaTensor::IrMetaTensor(const IrMetaTensor& other) { + dims_ = other.dims(); + dtype_ = other.dtype(); + layout_ = other.layout(); + lod_ = other.lod(); + offset_ = other.offset(); +} + +IrMetaTensor& IrMetaTensor::operator=(const IrMetaTensor& other) { + dims_ = other.dims(); + dtype_ = other.dtype(); + layout_ = other.layout(); + lod_ = other.lod(); + offset_ = other.offset(); + return *this; +} + +IrMetaTensor& IrMetaTensor::operator=(IrMetaTensor&& other) noexcept { + dims_ = std::move(other.dims()); + dtype_ = other.dtype(); + layout_ = other.layout(); + lod_ = std::move(other.lod()); + offset_ = other.offset(); + return *this; +} + +int64_t IrMetaTensor::numel() const { return phi::product(dims_); } + +const phi::Place& IrMetaTensor::place() const { + IR_THROW("Don't use IrMetaTensor::place method."); +} + +void* IrMetaTensor::AllocateFrom(phi::Allocator* allocator, + phi::DataType dtype, + size_t requested_size, + bool fake_alloc) { + IR_THROW("Don't use IrMetaTensor::AllocateFrom method."); +} + +} // namespace dialect +} // namespace paddle diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h new file mode 100644 index 00000000000..ffcbd415c36 --- /dev/null +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h @@ -0,0 +1,80 @@ +// Copyright (c) 2023 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 "paddle/phi/core/allocator.h" +#include "paddle/phi/core/tensor_base.h" +#include "paddle/phi/core/tensor_meta.h" + +namespace paddle { +namespace dialect { + +using LoD = std::vector>; + +class IrMetaTensor : public phi::TensorBase, + public phi::TypeInfoTraits { + public: + IrMetaTensor(phi::DataType dtype, + const phi::DDim& dims, + phi::DataLayout layout, + const LoD& lod, + size_t offset = 0); + + IrMetaTensor(IrMetaTensor&& other) = default; + + IrMetaTensor(const IrMetaTensor& other); + + IrMetaTensor& operator=(const IrMetaTensor& other); + + IrMetaTensor& operator=(IrMetaTensor&& other) noexcept; + + virtual ~IrMetaTensor() = default; + + public: + static const char* name() { return "IrMetaTensor"; } + + int64_t numel() const override; + + const phi::DDim& dims() const noexcept override { return dims_; } + + const phi::Place& place() const override; + + phi::DataType dtype() const noexcept override { return dtype_; } + + phi::DataLayout layout() const noexcept override { return layout_; } + + const LoD& lod() const noexcept { return lod_; } + + size_t offset() const noexcept { return offset_; } + + bool valid() const noexcept override { return true; } + + bool initialized() const override { return true; } + + void* AllocateFrom(phi::Allocator* allocator, + phi::DataType dtype, + size_t requested_size = 0, + bool fake_alloc = false) override; + + private: + phi::DDim dims_; + phi::DataType dtype_{phi::DataType::UNDEFINED}; + phi::DataLayout layout_{phi::DataLayout::NCHW}; + LoD lod_; + size_t offset_{0}; +}; + +} // namespace dialect +} // namespace paddle diff --git a/paddle/phi/core/meta_tensor.cc b/paddle/phi/core/meta_tensor.cc index 5ea5a079609..9b9df5c1ff4 100644 --- a/paddle/phi/core/meta_tensor.cc +++ b/paddle/phi/core/meta_tensor.cc @@ -16,6 +16,7 @@ limitations under the License. */ #include "glog/logging.h" +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" #include "paddle/phi/core/enforce.h" @@ -271,6 +272,8 @@ const LoD& MetaTensor::lod() const { return static_cast(tensor_)->non_zero_elements().lod(); } else if (phi::SparseCsrTensor::classof(tensor_)) { return static_cast(tensor_)->non_zero_elements().lod(); + } else if (paddle::dialect::IrMetaTensor::classof(tensor_)) { + return static_cast(tensor_)->lod(); } else { PADDLE_THROW(phi::errors::Unimplemented("Unsupported getting lod of `%s`.", tensor_->type_info().name())); diff --git a/paddle/phi/core/utils/type_info.cc b/paddle/phi/core/utils/type_info.cc index 38e17b57f63..99b134b6e79 100644 --- a/paddle/phi/core/utils/type_info.cc +++ b/paddle/phi/core/utils/type_info.cc @@ -14,6 +14,7 @@ limitations under the License. */ #include +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h" #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/backends/custom/custom_context.h" #include "paddle/phi/backends/gpu/gpu_context.h" @@ -50,6 +51,7 @@ template class TypeInfoTraits; template class TypeInfoTraits; template class TypeInfoTraits; template class TypeInfoTraits; +template class TypeInfoTraits; template class TypeInfoTraits; template class TypeInfoTraits; -- GitLab