Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions paddle/fluid/pir/dialect/distributed/ir/dist_interface.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in next pr.

//
// 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/pir/dialect/distributed/ir/dist_interface.h"

namespace paddle::dialect {} // namespace paddle::dialect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
namespace paddle::dialect {} // namespace paddle::dialect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in next pr.


IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::DistTypeInterface)
53 changes: 53 additions & 0 deletions paddle/fluid/pir/dialect/distributed/ir/dist_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2024 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/pir/include/core/cast_utils.h"
#include "paddle/pir/include/core/dll_decl.h"
#include "paddle/pir/include/core/type.h"

namespace paddle {
namespace dialect {

class IR_API DistTypeInterface
: public pir::TypeInterfaceBase<DistTypeInterface> {
public:
struct Concept {
/// Defined these methods with the interface.
explicit Concept(pir::Type (*local_type)(pir::Type))
: local_type(local_type) {}
pir::Type (*local_type)(pir::Type);
};

template <class ConcreteType>
struct Model : public Concept {
static Type local_type(Type type) {
return pir::cast<ConcreteType>(type).local_type();
}
Model() : Concept(local_type) {}
};

DistTypeInterface(pir::Type type, Concept *impl)
: pir::TypeInterfaceBase<DistTypeInterface>(type), impl_(impl) {}

pir::Type local_type() { return impl_->local_type(*this); }

private:
Concept *impl_;
};

} // namespace dialect
} // namespace paddle

IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::DistTypeInterface)
10 changes: 10 additions & 0 deletions paddle/fluid/pir/dialect/distributed/ir/dist_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "paddle/fluid/pir/dialect/distributed/ir/dist_type.h"
#include "paddle/fluid/pir/dialect/distributed/ir/type_storage.h"
#include "paddle/pir/include/core/ir_context.h"

namespace paddle {
namespace dialect {
Expand Down Expand Up @@ -57,6 +58,15 @@ common::DDim InferLocalDDim(const common::DDim& global_ddim,
return local_ddim;
}

auto DistDenseTensorType::local_type() const -> Type {
return pir::DenseTensorType::get(pir::IrContext::Instance(),
dtype(),
local_ddim(),
data_layout(),
lod(),
offset());
}

} // namespace dialect
} // namespace paddle

Expand Down
8 changes: 7 additions & 1 deletion paddle/fluid/pir/dialect/distributed/ir/dist_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include "paddle/fluid/pir/dialect/distributed/ir/dist_attribute.h"
#include "paddle/fluid/pir/dialect/distributed/ir/dist_interface.h"
#include "paddle/pir/include/core/builtin_type.h"
#include "paddle/pir/include/core/type.h"

Expand All @@ -29,18 +30,23 @@ class DistDenseTensorType
: public pir::Type::TypeBase<DistDenseTensorType,
pir::Type,
DistDenseTensorTypeStorage,
pir::WrapTypeInterface> {
pir::WrapTypeInterface,
DistTypeInterface> {
public:
using Base::Base;
using LoD = pir::DenseTensorTypeStorage::LoD;

pir::DenseTensorType dense_tensor_type() const;
TensorDistAttribute tensor_dist_attr() const;
const common::DDim& global_ddim() const { return dense_tensor_type().dims(); }
const common::DDim& local_ddim() const;
Type dtype() const { return dense_tensor_type().dtype(); }
DataLayout data_layout() const { return dense_tensor_type().data_layout(); }
const LoD& lod() const { return dense_tensor_type().lod(); }
size_t offset() const { return dense_tensor_type().offset(); }

Type prim_type() { return dense_tensor_type(); }
Type local_type() const;

ProcessMeshAttribute process_mesh_attr() const {
return tensor_dist_attr().process_mesh_attr();
Expand Down
48 changes: 48 additions & 0 deletions test/cpp/pir/distributed/dist_dialect_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "paddle/fluid/pir/dialect/distributed/ir/dist_attribute.h"
#include "paddle/fluid/pir/dialect/distributed/ir/dist_dialect.h"
#include "paddle/fluid/pir/dialect/distributed/ir/dist_interface.h"
#include "paddle/fluid/pir/dialect/distributed/ir/dist_op.h"
#include "paddle/fluid/pir/dialect/distributed/ir/dist_type.h"
#include "paddle/fluid/pir/dialect/distributed/transforms/mix_to_dist_pass.h"
Expand Down Expand Up @@ -167,6 +168,53 @@ TEST(dist_dense_tensor_type_test, warp_type_interface) {
dense_tensor_type);
}

TEST(dist_dense_tensor_type_test, dist_interface) {
pir::IrContext* ctx = pir::IrContext::Instance();
ctx->GetOrRegisterDialect<DistDialect>();
ctx->GetOrRegisterDialect<OperatorDialect>();
std::vector<int64_t> mesh_shape = {2, 3};
std::vector<int64_t> process_ids = {0, 1, 2, 3, 4, 5};
std::vector<std::string> dim_names = {"x", "y"};
phi::distributed::ProcessMesh process_mesh(
mesh_shape, process_ids, dim_names);
auto mesh_attr = ProcessMeshAttribute::get(ctx, process_mesh);

std::vector<int64_t> dims_mapping = {0, -1};
paddle::flat_hash_map<int64_t, phi::ReduceType> partial_status{
{1, phi::ReduceType::kRedSum}};
// construct a TensorDistAttribute.
auto tensor_dist_attr =
TensorDistAttribute::get(ctx, mesh_attr, dims_mapping, partial_status);

pir::Type fp32_dtype = pir::Float32Type::get(ctx);
common::DDim dims = {4, 8};
common::DDim local_dims = {2, 8};
common::DataLayout data_layout = common::DataLayout::NCHW;
pir::LoD lod = {{0, 1, 2}};
size_t offset = 0;
pir::DenseTensorType dense_tensor_type = pir::DenseTensorType::get(
ctx, fp32_dtype, dims, data_layout, lod, offset);

pir::Type dist_densor_type =
DistDenseTensorType::get(ctx, dense_tensor_type, tensor_dist_attr);

EXPECT_TRUE(dist_densor_type.isa<pir::DenseTensorType>());
EXPECT_EQ(dist_densor_type.dyn_cast<pir::DenseTensorType>(),
dense_tensor_type);

// test local cast
auto local_dense_tensor_type = dist_densor_type.dyn_cast<DistTypeInterface>()
.local_type()
.dyn_cast<pir::DenseTensorType>();
EXPECT_TRUE(local_dense_tensor_type.isa<pir::DenseTensorType>());
EXPECT_FALSE(local_dense_tensor_type.isa<DistDenseTensorType>());
EXPECT_EQ(local_dense_tensor_type.dtype().isa<pir::Float32Type>(), true);
EXPECT_EQ(local_dense_tensor_type.dims(), local_dims);
EXPECT_EQ(local_dense_tensor_type.data_layout(), data_layout);
EXPECT_EQ(local_dense_tensor_type.lod(), lod);
EXPECT_EQ(local_dense_tensor_type.offset(), offset);
}

TEST(operation_dist_attr_test, base) {
pir::IrContext* ctx = pir::IrContext::Instance();
ctx->GetOrRegisterDialect<DistDialect>();
Expand Down