Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IR] Program & Parameter & PaddleDialect #53557

Merged
merged 14 commits into from
May 17, 2023
3 changes: 3 additions & 0 deletions paddle/fluid/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ add_subdirectory(pybind)
add_subdirectory(eager)
add_subdirectory(prim)
add_subdirectory(jit)
if(WITH_NEWIR)
add_subdirectory(dialect)
endif()
# NOTE: please add subdirectory inference at last.
add_subdirectory(inference)
9 changes: 9 additions & 0 deletions paddle/fluid/dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set(PD_DIALECT_SOURCE_DIR "${PADDLE_SOURCE_DIR}/paddle/fluid/dialect")
set(PD_DIALECT_BINARY_DIR "${PADDLE_BINARY_DIR}/paddle/fluid/dialect")

file(GLOB PD_DIALECT_SRCS "*.cc")

cc_library(
pd_dialect
SRCS ${PD_DIALECT_SRCS}
DEPS new_ir framework_proto dense_tensor)
97 changes: 97 additions & 0 deletions paddle/fluid/dialect/pd_dialect.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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/dialect/pd_dialect.h"
#include "paddle/fluid/dialect/pd_type.h"
#include "paddle/fluid/dialect/utils.h"
#include "paddle/fluid/framework/convert_utils.h"
#include "paddle/fluid/framework/data_type.h"
#include "paddle/ir/builtin_type.h"
#include "paddle/ir/dialect_interface.h"
#include "paddle/phi/core/dense_tensor.h"

namespace paddle {
namespace dialect {
std::shared_ptr<paddle::framework::Variable>
ParameterConvertInterface::ParameterToVariable(ir::Parameter* parameter) {
if (parameter->type().isa<DenseTensorType>()) {
VLOG(4) << "Convert a DenseTensor Parameter to a variable.";
std::shared_ptr<paddle::framework::Variable> var =
std::make_shared<paddle::framework::Variable>();
phi::DenseTensor* tensor = var->GetMutable<phi::DenseTensor>();
// Init DenseTensor
auto dim = parameter->type().dyn_cast<DenseTensorType>().dim();
phi::DenseTensorMeta meta(
TransToPhiDataType(
parameter->type().dyn_cast<DenseTensorType>().dtype()),
phi::DDim(dim.data(), dim.size()),
TransToPhiDataLayout(
parameter->type().dyn_cast<DenseTensorType>().data_layout()),
parameter->type().dyn_cast<DenseTensorType>().lod(),
parameter->type().dyn_cast<DenseTensorType>().offset());
tensor->set_meta(meta);
paddle::platform::DeviceContext* dev_ctx =
paddle::platform::DeviceContextPool::Instance().Get(
paddle::platform::CPUPlace());
dev_ctx->Alloc(tensor,
TransToPhiDataType(
parameter->type().dyn_cast<DenseTensorType>().dtype()));
memcpy(tensor->data(),
parameter->data(),
tensor->numel() * phi::SizeOf(tensor->dtype()));
return var;
} else {
return nullptr;
}
}

std::unique_ptr<ir::Parameter> ParameterConvertInterface::VariableToParameter(
paddle::framework::Variable* var) {
if (var->IsType<phi::DenseTensor>()) {
phi::DenseTensor* tensor = var->GetMutable<phi::DenseTensor>();
// Get Meta
ir::IrContext* ctx = ir::IrContext::Instance();
ir::Type data_type = TransToIrDataType(tensor->dtype(), ctx);
DenseTensorTypeStorage::Dim dims(tensor->dims().size());
std::copy(tensor->dims().Get(),
tensor->dims().Get() + tensor->dims().size(),
dims.data());
DenseTensorTypeStorage::DataLayout data_layout =
TransToIrDataLayout(tensor->layout());
DenseTensorTypeStorage::LoD lod = tensor->lod();
size_t offset = tensor->meta().offset;
void* data = tensor->data();
ir::Type dense_tensor_type =
DenseTensorType::get(ctx, data_type, dims, data_layout, lod, offset);
return std::make_unique<ir::Parameter>(
data,
tensor->numel() * phi::SizeOf(tensor->dtype()),
dense_tensor_type);
} else {
return nullptr;
}
}

PaddleDialect::PaddleDialect(ir::IrContext* context)
: ir::Dialect(name(), context, ir::TypeId::get<PaddleDialect>()) {
initialize();
}

void PaddleDialect::initialize() {
RegisterTypes<GET_PADDLE_TYPE_LIST>();
RegisterInterfaces<ParameterConvertInterface>();
}

} // namespace dialect
} // namespace paddle
47 changes: 47 additions & 0 deletions paddle/fluid/dialect/pd_dialect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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/fluid/framework/variable.h"
#include "paddle/ir/dialect.h"
#include "paddle/ir/parameter.h"

namespace paddle {
namespace dialect {
class ParameterConvertInterface
: public ir::DialectInterface::Base<ParameterConvertInterface> {
public:
explicit ParameterConvertInterface(ir::Dialect* dialect) : Base(dialect) {}

// NOTE(zhangbo): Only support new a CPU Variable.
std::shared_ptr<paddle::framework::Variable> ParameterToVariable(
ir::Parameter* parameter);

std::unique_ptr<ir::Parameter> VariableToParameter(
paddle::framework::Variable* var);
};

class PaddleDialect : public ir::Dialect {
public:
explicit PaddleDialect(ir::IrContext* context);

static const char* name() { return "pd"; }

private:
void initialize();
};

} // namespace dialect
} // namespace paddle
19 changes: 12 additions & 7 deletions paddle/ir/builtin_type.cc → paddle/fluid/dialect/pd_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,28 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/ir/builtin_type.h"
#include "paddle/fluid/dialect/pd_type.h"

namespace ir {
namespace paddle {
namespace dialect {
const ir::Type& DenseTensorType::dtype() const { return storage()->dtype_; }

const ir::DenseTensorTypeStorage::Dim& DenseTensorType::dim() const {
const paddle::dialect::DenseTensorTypeStorage::Dim& DenseTensorType::dim()
const {
return storage()->dims_;
}

const ir::DenseTensorTypeStorage::DataLayout& DenseTensorType::data_layout()
const {
const paddle::dialect::DenseTensorTypeStorage::DataLayout&
DenseTensorType::data_layout() const {
return storage()->layout_;
}

const ir::DenseTensorTypeStorage::LoD& DenseTensorType::lod() const {
const paddle::dialect::DenseTensorTypeStorage::LoD& DenseTensorType::lod()
const {
return storage()->lod_;
}

const size_t& DenseTensorType::offset() const { return storage()->offset_; }
} // namespace ir

} // namespace dialect
} // namespace paddle
46 changes: 46 additions & 0 deletions paddle/fluid/dialect/pd_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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/fluid/dialect/pd_type_storage.h"
#include "paddle/ir/type.h"

namespace paddle {
namespace dialect {
#define GET_PADDLE_TYPE_LIST paddle::dialect::DenseTensorType

///
/// \brief Define built-in parameteric types.
///
class DenseTensorType : public ir::Type {
public:
using Type::Type;

DECLARE_TYPE_UTILITY_FUNCTOR(DenseTensorType, DenseTensorTypeStorage);

const ir::Type &dtype() const;

const paddle::dialect::DenseTensorTypeStorage::Dim &dim() const;

const paddle::dialect::DenseTensorTypeStorage::DataLayout &data_layout()
const;

const paddle::dialect::DenseTensorTypeStorage::LoD &lod() const;

const size_t &offset() const;
};

} // namespace dialect
} // namespace paddle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ struct hash<std::vector<T>> {

} // namespace std

namespace ir {
namespace paddle {
namespace dialect {
///
/// \brief Define Parameteric TypeStorage for DenseTensorType.
///
Expand Down Expand Up @@ -151,4 +152,5 @@ struct DenseTensorTypeStorage : public ir::TypeStorage {
size_t offset_;
};

} // namespace ir
} // namespace dialect
} // namespace paddle
Loading