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
1 change: 1 addition & 0 deletions shardy/dialect/sdy/transforms/export/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ cc_library(
":explicit_reshards_util",
":passes_inc",
"//shardy/common:file_utils",
"//shardy/common:logging",
"//shardy/dialect/sdy/ir:axis_list_ref",
"//shardy/dialect/sdy/ir:dialect",
"//shardy/dialect/sdy/transforms/common:op_properties",
Expand Down
44 changes: 30 additions & 14 deletions shardy/dialect/sdy/transforms/export/insert_explicit_reshards.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ limitations under the License.
#include "mlir/IR/Value.h"
#include "mlir/Pass/Pass.h" // IWYU pragma: keep
#include "mlir/Support/LLVM.h"
#include "shardy/common/logging.h"
#include "shardy/dialect/sdy/ir/dialect.h"
#include "shardy/dialect/sdy/ir/enums.h"
#include "shardy/dialect/sdy/ir/utils.h"
Expand Down Expand Up @@ -319,6 +320,34 @@ std::optional<Mesh> getMesh(ArrayRef<TensorShardingAttr> inShardings,
/*defaultMesh=*/Mesh(meshAttr, *meshName));
}

void insertAllReduceOnOpIfUnreducedToReplicated(
Operation* op, IRRewriter& rewriter, const SymbolTable& symbolTable) {
if (op->getResults().empty()) {
auto operandHasUnreducedAxes = [&](OpOperand& operand) {
TensorShardingAttr sharding = getSharding(operand.get());
return sharding && !sharding.getUnreducedAxes().empty();
};
SDY_CHECK(!llvm::any_of(op->getOpOperands(), operandHasUnreducedAxes))
<< "Some operands has unreduced axes but the operation has no "
"results. ";
return;
}

// For each operand that has unreduced axes, insert an all-reduce if
// any of the unreduced axes isn't unreduced in the target sharding.
//
// We assume all results of an op should have the same unreduced axes,
// so we look at the first result.
rewriter.setInsertionPoint(op);
for (OpOperand& operand : op->getOpOperands()) {
if (TensorShardingAttr inSharding = getSharding(operand.get())) {
insertAllReduceIfUnreducedToReplicated(operand, inSharding,
getSharding(op->getResult(0)),
symbolTable, rewriter);
}
}
}

struct InsertExplicitReshardsPass
: public impl::InsertExplicitReshardsPassBase<InsertExplicitReshardsPass> {
using InsertExplicitReshardsPassBase::InsertExplicitReshardsPassBase;
Expand Down Expand Up @@ -361,20 +390,7 @@ struct InsertExplicitReshardsPass
return;
}

// For each operand that has unreduced axes, insert an all-reduce if
// any of the unreduced axes isn't unreduced in the target sharding.
//
// We assume all results of an op should have the same unreduced axes,
// so we look at the first result.
TensorShardingAttr outSharding =
op->getResults().empty() ? nullptr : getSharding(op->getResult(0));
rewriter.setInsertionPoint(op);
for (OpOperand& operand : op->getOpOperands()) {
if (TensorShardingAttr inSharding = getSharding(operand.get())) {
insertAllReduceIfUnreducedToReplicated(
operand, inSharding, outSharding, symbolTable, rewriter);
}
}
insertAllReduceOnOpIfUnreducedToReplicated(op, rewriter, symbolTable);

// NOTE: Creating a sharding rule requires data flow edges are present.
OpShardingRuleAttr shardingRule =
Expand Down
Loading