-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#sdy Add canonicalization rewrite pattern for ReshardOp.
Add a rewrite pattern to replace a chain of ReshardOp with one ReshardOp. PiperOrigin-RevId: 653379478
- Loading branch information
1 parent
84547f2
commit 4d9ebe6
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* Copyright 2024 The Shardy Authors. | ||
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 "mlir/IR/MLIRContext.h" | ||
#include "mlir/IR/OperationSupport.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "shardy/dialect/sdy/ir/dialect.h" | ||
|
||
namespace mlir { | ||
namespace sdy { | ||
|
||
namespace { | ||
|
||
#include "shardy/dialect/sdy/ir/canonicalization.cc.inc" | ||
|
||
} // namespace | ||
|
||
void ReshardOp::getCanonicalizationPatterns(RewritePatternSet& results, | ||
MLIRContext* context) { | ||
results.add<ReshardOfReshardPattern>(context); | ||
} | ||
|
||
} // namespace sdy | ||
} // namespace mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* Copyright 2024 The Shardy Authors. | ||
|
||
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. | ||
==============================================================================*/ | ||
|
||
#ifndef SDY_CANONICALIZATION | ||
#define SDY_CANONICALIZATION | ||
|
||
include "shardy/dialect/sdy/ir/ops.td" | ||
include "mlir/IR/PatternBase.td" | ||
|
||
def HasOneUse: Constraint<CPred<"$_self.hasOneUse()">, "has one use">; | ||
|
||
def ReshardOfReshardPattern : | ||
Pat<(Sdy_ReshardOp (Sdy_ReshardOp:$inner_reshard $tensor, $inner_attr), $outer_attr), | ||
(Sdy_ReshardOp $tensor, $outer_attr), [(HasOneUse:$inner_reshard)]>; | ||
|
||
#endif // SDY_CANONICALIZATION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// RUN: sdy_opt %s -canonicalize | FileCheck %s | ||
|
||
sdy.mesh @mesh = <"a"=2, "b"=2> | ||
|
||
// CHECK-LABEL: func @reshard_of_reshard_no_other_uses | ||
// CHECK-NEXT: %0 = sdy.reshard %arg0 <@mesh, [{"a", ?}, {?}]> | ||
// CHECK-NEXT: return %0 | ||
func.func @reshard_of_reshard_no_other_uses(%arg0: tensor<8x8xf32>) -> tensor<8x8xf32> { | ||
%0 = sdy.reshard %arg0 <@mesh, [{"a"}, {"b"}]> : tensor<8x8xf32> | ||
%1 = sdy.reshard %0 <@mesh, [{"a", ?}, {?}]> : tensor<8x8xf32> | ||
return %1 : tensor<8x8xf32> | ||
} | ||
|
||
// CHECK-LABEL: func @reshard_of_reshard_with_other_uses | ||
// CHECK-NEXT: %0 = sdy.reshard %arg0 <@mesh, [{"a"}, {"b"}]> | ||
// CHECK-NEXT: %1 = sdy.reshard %0 <@mesh, [{"a", ?}, {?}]> | ||
// CHECK-NEXT: return %0, %1 | ||
func.func @reshard_of_reshard_with_other_uses(%arg0: tensor<8x8xf32>) -> (tensor<8x8xf32>, tensor<8x8xf32>) { | ||
%0 = sdy.reshard %arg0 <@mesh, [{"a"}, {"b"}]> : tensor<8x8xf32> | ||
%1 = sdy.reshard %0 <@mesh, [{"a", ?}, {?}]> : tensor<8x8xf32> | ||
return %0, %1 : tensor<8x8xf32>, tensor<8x8xf32> | ||
} | ||
|
||
// CHECK-LABEL: func @reshard_chain_of_three_no_other_uses | ||
// CHECK-NEXT: %0 = sdy.reshard %arg0 <@mesh, [{?}, {"a", ?}]> | ||
// CHECK-NEXT: return %0 | ||
func.func @reshard_chain_of_three_no_other_uses(%arg0: tensor<8x8xf32>) -> tensor<8x8xf32> { | ||
%0 = sdy.reshard %arg0 <@mesh, [{"a"}, {"b"}]> : tensor<8x8xf32> | ||
%1 = sdy.reshard %0 <@mesh, [{"a", ?}, {?}]> : tensor<8x8xf32> | ||
%2 = sdy.reshard %1 <@mesh, [{?}, {"a", ?}]> : tensor<8x8xf32> | ||
return %2 : tensor<8x8xf32> | ||
} | ||
|