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

Handle when JAX saves the manual axes on the CallOp of the shmap_body. #92

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

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

#include "llvm/ADT/STLExtras.h"
Expand Down Expand Up @@ -336,5 +337,35 @@ 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;
}

} // namespace sdy
} // namespace mlir
18 changes: 18 additions & 0 deletions shardy/dialect/sdy/ir/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ 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/Support/ErrorHandling.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Threading.h"
Expand Down Expand Up @@ -260,6 +262,22 @@ 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);

} // 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