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

Replace std::bind with std::bind_front for PropagationDirectionAlongFactor. #363

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
56 changes: 28 additions & 28 deletions shardy/dialect/sdy/transforms/propagation/basic_propagation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,7 @@ LogicalResult propagateFuncResults(FuncOp funcOp,
// Treat the sharding data flow b/w the `funcOp` terminator and func
// result attrs as an identity op. Create an equivalent sharding rule.
createIdentityShardingRule(tensorType),
std::bind(propagateAny, funcOp, std::placeholders::_1),
factorPropagation,
std::bind_front(propagateAny, funcOp), factorPropagation,
/*conservativePropagation=*/false, funcOp, symbolTable,
/*rewriter=*/nullptr, shardingGroupMap);
}
Expand Down Expand Up @@ -420,13 +419,13 @@ class PropagateRegisteredOp : public RewritePattern {
explicit PropagateRegisteredOp(
MLIRContext* context, const SymbolTable& symbolTable,
GetDirectionToPropagateFn getDirectionToPropagate,
bool conservativePropagation, const FactorPropagation& factorPropagation,
const FactorPropagation& factorPropagation, bool conservativePropagation,
const ShardingGroupMap& shardingGroupMap)
: RewritePattern(MatchAnyOpTypeTag(), /*benefit=*/1, context),
symbolTable(symbolTable),
getDirectionToPropagate(getDirectionToPropagate),
conservativePropagation(conservativePropagation),
factorPropagation(factorPropagation),
conservativePropagation(conservativePropagation),
shardingGroupMap(shardingGroupMap) {}

LogicalResult matchAndRewrite(Operation* op,
Expand All @@ -441,7 +440,7 @@ class PropagateRegisteredOp : public RewritePattern {
}

PropagationDirectionAlongFactor directionAlongFactor =
std::bind(getDirectionToPropagate, op, std::placeholders::_1);
std::bind_front(getDirectionToPropagate, op);
return propagateTensorShardings(op->getOperands(), op->getResults(),
shardingRule, op, symbolTable, rewriter,
directionAlongFactor, factorPropagation,
Expand All @@ -451,8 +450,8 @@ class PropagateRegisteredOp : public RewritePattern {
private:
const SymbolTable& symbolTable;
GetDirectionToPropagateFn getDirectionToPropagate;
bool conservativePropagation;
const FactorPropagation& factorPropagation;
bool conservativePropagation;
const ShardingGroupMap& shardingGroupMap;
};

Expand All @@ -462,48 +461,46 @@ class PropagateRegisteredOp : public RewritePattern {
// The `sdy.data_flow_edge` holds the updateable sharding of all targets.
class PropagateDataFlowEdgeOp : public OpRewritePattern<DataFlowEdgeOp> {
public:
explicit PropagateDataFlowEdgeOp(MLIRContext* context,
const SymbolTable& symbolTable,
const FactorPropagation& factorPropagation,
const ShardingGroupMap& shardingGroupMap)
explicit PropagateDataFlowEdgeOp(
MLIRContext* context, const SymbolTable& symbolTable,
GetDirectionToPropagateFn getDirectionToPropagate,
const FactorPropagation& factorPropagation,
const ShardingGroupMap& shardingGroupMap)
: OpRewritePattern<DataFlowEdgeOp>(context),
symbolTable(symbolTable),
getDirectionToPropagate(getDirectionToPropagate),
factorPropagation(factorPropagation),
shardingGroupMap(shardingGroupMap) {}

LogicalResult matchAndRewrite(DataFlowEdgeOp dataFlowEdgeOp,
PatternRewriter& rewriter) const override {
SmallVector<Value> sources = dataFlowEdgeOp.getSources();
// The sharding of `dataFlowEdgeOp.getResult()` is the sharding of all
// targets.

SmallVector<TensorShardingAttr> operandShardingRef = getShardings(sources);
TensorShardingAttr resultsShardingRef =
dataFlowEdgeOp.transformTargetSharding(
dataFlowEdgeOp.getShardingAttr(),
DataFlowShardingTransformType::kBeforeEdgePropagation);

PropagationTensorParams operandsParams = PropagationTensorParams(
/*tensors=*/sources,
/*shardings=*/operandShardingRef,
/*setShardingCallback=*/
[&sources](TensorShardingAttr sharding, int64_t index) {
setSharding(sources[index], sharding);
});

Value result = dataFlowEdgeOp.getResult();
// The sharding of `result` is the sharding of all targets.
TensorShardingAttr resultsShardingRef =
dataFlowEdgeOp.transformTargetSharding(
dataFlowEdgeOp.getShardingAttr(),
DataFlowShardingTransformType::kBeforeEdgePropagation);
PropagationTensorParams resultsParams = PropagationTensorParams(
/*tensors=*/result,
/*shardings=*/resultsShardingRef,
/*setShardingCallback=*/
[&dataFlowEdgeOp](TensorShardingAttr sharding, int64_t _) {
[&dataFlowEdgeOp](TensorShardingAttr sharding, int64_t) {
dataFlowEdgeOp.setShardingAttr(dataFlowEdgeOp.transformTargetSharding(
sharding, DataFlowShardingTransformType::kAfterEdgePropagation));
});

// TODO(b/394390827). We may pass getDirectionToPropagate so we can decide
// to change the priority of data flow ops.
PropagationDirectionAlongFactor directionAlongFactor =
std::bind(propagateAny, dataFlowEdgeOp, std::placeholders::_1);
std::bind_front(getDirectionToPropagate, dataFlowEdgeOp);
return propagateTensorShardings(
operandsParams, resultsParams,
createIdentityShardingRule(cast<ShapedType>(dataFlowEdgeOp.getType()),
Expand All @@ -515,6 +512,7 @@ class PropagateDataFlowEdgeOp : public OpRewritePattern<DataFlowEdgeOp> {

private:
const SymbolTable& symbolTable;
GetDirectionToPropagateFn getDirectionToPropagate;
const FactorPropagation& factorPropagation;
const ShardingGroupMap& shardingGroupMap;
};
Expand Down Expand Up @@ -605,14 +603,16 @@ LogicalResult BasicPropagationPassImpl::propagate(
}
MLIRContext* context = moduleOp.getContext();
RewritePatternSet patterns(context);
patterns.add<PropagateDataFlowEdgeOp, PropagatePropagationBarrier>(
patterns.add<PropagatePropagationBarrier>(
context, symbolTable, factorPropagation, shardingGroupMap);
patterns.add<PropagateDataFlowEdgeOp>(context, symbolTable,
getDirectionToPropagate,
factorPropagation, shardingGroupMap);
patterns.add<PropagateRegisteredOp>(
context, symbolTable, getDirectionToPropagate, conservativePropagation,
factorPropagation, shardingGroupMap);
// Note that we only need a single iteration (and another to confirm
// convergence), since we make sure ops whose sharding changes are
// added back to the worklist.
context, symbolTable, getDirectionToPropagate, factorPropagation,
conservativePropagation, shardingGroupMap);
// We only need a single iteration (and another to confirm convergence), since
// we make sure ops whose sharding changes are added back to the worklist.
GreedyRewriteConfig config;
config.useTopDownTraversal = true;
config.enableRegionSimplification = mlir::GreedySimplifyRegionLevel::Disabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ using GetDirectionToPropagateFnPtr = PropagationDirection (*)(Operation*,

PropagationDirection isPassThrough(Operation* op, int64_t) {
if (isElementwise(op) ||
isa<stablehlo::ReshapeOp, stablehlo::TransposeOp>(op)) {
isa<stablehlo::ReshapeOp, stablehlo::TransposeOp, DataFlowEdgeOp>(op)) {
return PropagationDirection::BOTH;
}
if (isa<stablehlo::DynamicSliceOp, stablehlo::DynamicUpdateSliceOp>(op)) {
Expand Down
Loading