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

Support JAX shard map lowering with Shardy enabled. #91

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
44 changes: 44 additions & 0 deletions shardy/dialect/sdy/ir/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ limitations under the License.

#include <cassert>
#include <cstdint>
#include <optional>
#include <string>

#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -336,5 +338,47 @@ void removeShardingRules(Operation* rootOp) {
});
}

std::optional<StringRef> getCommonMeshName(
ArrayRef<TensorShardingAttr> operandShardings,
ArrayRef<TensorShardingAttr> resultsShardings) {
StringRef meshName;
for (TensorShardingAttr sharding : llvm::concat<const TensorShardingAttr>(
operandShardings, resultsShardings)) {
if (sharding) {
if (meshName.empty()) {
meshName = sharding.getMeshName();
} else if (meshName != sharding.getMeshName()) {
// Found more than one mesh name.
return std::nullopt;
}
}
}

return meshName.empty() ? std::nullopt : std::make_optional(meshName);
}

ManualAxisToOwner getParentManualComputationOps(Operation* op) {
ManualAxisToOwner alreadyManualAxes;
auto parent = op->getParentOfType<ManualComputationOp>();
while (parent) {
for (StringRef axisName : parent.getManualAxes()) {
alreadyManualAxes[axisName] = parent;
}
parent = parent->getParentOfType<ManualComputationOp>();
}
return alreadyManualAxes;
}

llvm::SmallDenseSet<StringRef> getParentManualAxes(Operation* op) {
llvm::SmallDenseSet<StringRef> manualAxes;
auto parent = op->getParentOfType<ManualComputationOp>();
while (parent) {
manualAxes.insert(parent.getManualAxes().begin(),
parent.getManualAxes().end());
parent = parent->getParentOfType<ManualComputationOp>();
}
return manualAxes;
}

} // namespace sdy
} // namespace mlir
23 changes: 23 additions & 0 deletions shardy/dialect/sdy/ir/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ limitations under the License.
#define SHARDY_DIALECT_SDY_IR_UTILS_H_

#include <cstdint>
#include <optional>
#include <string>
#include <utility>

#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Threading.h"
Expand Down Expand Up @@ -260,6 +263,26 @@ void cloneRegionAndConvertTerminatorOp(Region& src, Region& dst) {
cloneRegionAndConvertTerminatorOp<TerminatorOpTy>(src, dst, rewriter);
}

// Returns the common mesh name used by all the `TensorShardingAttr` or
// `std::nullopt` if there is none.
std::optional<StringRef> getCommonMeshName(
ArrayRef<TensorShardingAttr> operandShardings,
ArrayRef<TensorShardingAttr> resultsShardings);

// Mapping between an axis name to the `ManualComputationOp` whose body is
// manual on.
using ManualAxisToOwner = llvm::SmallDenseMap<StringRef, ManualComputationOp>;

// Creates a mapping from axis name to the corresponding `ManualComputationOp`.
//
// ManualComputations op are allowed to be nested within each other, and this
// gives what manual axis was introduced by what `ManualComputationOp`.
ManualAxisToOwner getParentManualComputationOps(Operation* op);

// Gets the axes that are manual from a `ManualComputationOp` that has `op`
// inside.
llvm::SmallDenseSet<StringRef> getParentManualAxes(Operation* op);

} // namespace sdy
} // namespace mlir

Expand Down
20 changes: 0 additions & 20 deletions shardy/dialect/sdy/ir/verifiers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ using func::FuncOp;
using ::llvm::SmallDenseMap;
using ::llvm::SmallDenseSet;

// Mapping between an axis name to the ManualComputationOp whose body is manual
// on.
using ManualAxisToOwner = SmallDenseMap<StringRef, ManualComputationOp>;

using EmitErrorFn = std::function<InFlightDiagnostic(StringRef)>;

EmitErrorFn getEmitErrorFn(Operation* op) {
Expand Down Expand Up @@ -174,22 +170,6 @@ LogicalResult verifySubAxes(ArrayRef<AxisRefAttr> subAxes, StringRef axisName,
return success();
}

// ManualComputations op are allowed to be nested within each other. However,
// they cannot operate on the same manual axes. This function creates a mapping
// from a manual mesh axis name to the corresponding ManualComputationOp that
// operates on it to help with verifying this is the case.
ManualAxisToOwner getParentManualComputationOps(Operation* op) {
ManualAxisToOwner alreadyManualAxes;
auto parent = op->getParentOfType<ManualComputationOp>();
while (parent) {
for (StringRef axisName : parent.getManualAxes()) {
alreadyManualAxes[axisName] = parent;
}
parent = parent->getParentOfType<ManualComputationOp>();
}
return alreadyManualAxes;
}

LogicalResult emitBoundAxisInManualComputationError(EmitErrorFn emitError,
StringRef boundAxis,
Location parentLoc) {
Expand Down
21 changes: 0 additions & 21 deletions shardy/dialect/sdy/transforms/propagation/basic_propagation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,27 +209,6 @@ void updateTensorShardings(
mesh, notifyOpModified);
}

// Returns the common mesh name used by all the `TensorShardingAttr` or
// std::nullopt if there is none.
std::optional<StringRef> getCommonMeshName(
ArrayRef<TensorShardingAttr> operandShardings,
ArrayRef<TensorShardingAttr> resultsShardings) {
StringRef meshName;
for (TensorShardingAttr sharding : llvm::concat<const TensorShardingAttr>(
operandShardings, resultsShardings)) {
if (sharding) {
if (meshName.empty()) {
meshName = sharding.getMeshName();
} else if (meshName != sharding.getMeshName()) {
// Found more than one mesh name.
return std::nullopt;
}
}
}

return meshName.empty() ? std::nullopt : std::make_optional(meshName);
}

// Propagates tensor shardings of the given `operands` and `results` according
// to `shardingRule`.
//
Expand Down
Loading