diff --git a/paddle/fluid/framework/type_info.cc b/paddle/fluid/framework/type_info.cc index 442800d035f55bd002fa297cc7b45159748445c4..cb7dae540d119f5d3146eb49dfdda4e82811cb5c 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 d9747b47e8747ca61e08dde3f017424c7dc8640d..d36c269648315928aaad1f0841ea8999e443971e 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 1c19874dd9bb7913a44cf709ae1078aed8578eda..7ee65d050581b39eaca03d5cbc9e3da471a360d8 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 cc8d1357bf070a37b0d05f4dcd7a43d0d479b798..08cc463c34c9bd30966995e075a61623ff686a9e 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 0000000000000000000000000000000000000000..2da7b098a6556ee6f83070f2ecd0a695ea5132ae --- /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 0000000000000000000000000000000000000000..ffcbd415c368a973f0aef9e99afa508f928746c1 --- /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 5ea5a0796092381c6ae66641dea49c8c65503002..9b9df5c1ff4aa5c0b52a8cc97ae66cd3e45584b8 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 38e17b57f633da8f4be4498edb7914f30851b79c..99b134b6e7960f565ae07178829de4f65145c263 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;