-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#sdy Add sdy-round-trip-shard-map-export Pass.
PiperOrigin-RevId: 674303254
- Loading branch information
1 parent
bff7fd7
commit 1135d3c
Showing
11 changed files
with
349 additions
and
10 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
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
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
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
127 changes: 127 additions & 0 deletions
127
xla/service/spmd/shardy/sdy_round_trip/shard_map_export.cc
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,127 @@ | ||
/* Copyright 2024 The OpenXLA 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 "xla/service/spmd/shardy/sdy_round_trip/shard_map_export.h" | ||
|
||
#include <memory> | ||
|
||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Support/ErrorHandling.h" | ||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/IR/AffineMap.h" | ||
#include "mlir/IR/Attributes.h" | ||
#include "mlir/IR/Builders.h" | ||
#include "mlir/IR/BuiltinAttributes.h" | ||
#include "mlir/IR/BuiltinOps.h" | ||
#include "mlir/IR/Diagnostics.h" | ||
#include "mlir/IR/DialectRegistry.h" | ||
#include "mlir/IR/MLIRContext.h" | ||
#include "mlir/IR/Operation.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/IR/SymbolTable.h" | ||
#include "mlir/IR/Value.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Pass/PassManager.h" | ||
#include "mlir/Pass/PassRegistry.h" | ||
#include "mlir/Support/LLVM.h" | ||
#include "mlir/Support/TypeID.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
#include "shardy/dialect/sdy/ir/dialect.h" | ||
#include "shardy/dialect/sdy/ir/utils.h" | ||
#include "stablehlo/dialect/StablehloOps.h" | ||
#include "xla/service/spmd/shardy/constants.h" | ||
#include "xla/service/spmd/shardy/utils.h" | ||
|
||
namespace xla { | ||
namespace sdy { | ||
|
||
namespace { | ||
|
||
using ::mlir::MLIRContext; | ||
using ::mlir::ModuleOp; | ||
using ::mlir::StringRef; | ||
using ::mlir::func::FuncOp; | ||
|
||
namespace stablehlo = ::mlir::stablehlo; | ||
namespace sdy = ::mlir::sdy; | ||
|
||
class SdyRoundTripShardMapExportPass | ||
: public mlir::PassWrapper<SdyRoundTripShardMapExportPass, | ||
mlir::OperationPass<ModuleOp>> { | ||
public: | ||
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(SdyRoundTripShardMapExportPass) | ||
|
||
void runOnOperation() final { | ||
ModuleOp moduleOp = getOperation(); | ||
MLIRContext* context = moduleOp.getContext(); | ||
mlir::SymbolTableCollection symbolTableCollection; | ||
mlir::SymbolTable& symbolTable = | ||
symbolTableCollection.getSymbolTable(moduleOp); | ||
auto rewriter = mlir::IRRewriter(context); | ||
moduleOp->walk([&](sdy::ManualComputationOp manualComputation) { | ||
rewriter.setInsertionPointToEnd(&moduleOp.getRegion().front()); | ||
auto funcOp = rewriter.create<FuncOp>( | ||
manualComputation.getLoc(), kManualComputationBodyFuncName, | ||
rewriter.getFunctionType( | ||
manualComputation.getBody().getArgumentTypes(), | ||
sdy::getBodyTerminatorOpOperandTypes(manualComputation))); | ||
sdy::inlineRegionAndConvertTerminatorOp<mlir::func::ReturnOp>( | ||
manualComputation.getBody(), funcOp.getBody()); | ||
mlir::StringAttr funcName = symbolTable.insert(funcOp); | ||
|
||
rewriter.setInsertionPoint(manualComputation); | ||
auto customCallOp = rewriter.create<stablehlo::CustomCallOp>( | ||
manualComputation.getLoc(), manualComputation.getResultTypes(), | ||
manualComputation->getOperands()); | ||
customCallOp.setCallTargetName(kManualComputationCustomCallTargetName); | ||
customCallOp.setCalledComputationsAttr( | ||
rewriter.getArrayAttr(mlir::FlatSymbolRefAttr::get(funcName))); | ||
addFrontendAttribute(customCallOp, kInShardings, | ||
manualComputation.getInShardings()); | ||
addFrontendAttribute(customCallOp, kOutShardings, | ||
manualComputation.getOutShardings()); | ||
addFrontendAttribute(customCallOp, kManualAxes, | ||
manualComputation.getManualAxesAttr()); | ||
rewriter.replaceOp(manualComputation, customCallOp->getResults()); | ||
}); | ||
} | ||
|
||
StringRef getArgument() const override { | ||
return "xla-sdy-round-trip-shard-map-export"; | ||
} | ||
|
||
StringRef getDescription() const override { | ||
return "Converts the body of a ManualComputationOp to a separate function " | ||
"with a CustomCallOp of the same name referring to it. The " | ||
"CustomCallOp saves the in/out shardings and manual axes as " | ||
"frontend attrs for HLO round tripping."; | ||
} | ||
void getDependentDialects(mlir::DialectRegistry& registry) const final { | ||
registry.insert<stablehlo::StablehloDialect>(); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
void registerSdyRoundTripShardMapExportPass() { | ||
mlir::registerPass(createSdyRoundTripShardMapExportPass); | ||
} | ||
|
||
std::unique_ptr<mlir::Pass> createSdyRoundTripShardMapExportPass() { | ||
return std::make_unique<SdyRoundTripShardMapExportPass>(); | ||
} | ||
|
||
} // namespace sdy | ||
} // namespace xla |
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 OpenXLA 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 XLA_SERVICE_SPMD_SHARDY_SDY_ROUND_TRIP_SHARD_MAP_EXPORT_H_ | ||
#define XLA_SERVICE_SPMD_SHARDY_SDY_ROUND_TRIP_SHARD_MAP_EXPORT_H_ | ||
|
||
#include <memory> | ||
|
||
#include "mlir/Pass/Pass.h" | ||
|
||
namespace xla { | ||
namespace sdy { | ||
|
||
// Creates the pass that converts `ManualComputationOps` to a separate function | ||
// and `CustomCallOp` for round tripping between HLO. | ||
std::unique_ptr<mlir::Pass> createSdyRoundTripShardMapExportPass(); | ||
|
||
// Registers the xla-sdy-round-trip-shard-map-export pass. | ||
void registerSdyRoundTripShardMapExportPass(); | ||
|
||
} // namespace sdy | ||
} // namespace xla | ||
|
||
#endif // XLA_SERVICE_SPMD_SHARDY_SDY_ROUND_TRIP_SHARD_MAP_EXPORT_H_ |
2 changes: 1 addition & 1 deletion
2
...ce/spmd/shardy/test/shard_map_export.mlir → ...est/mhlo_round_trip_shard_map_export.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
Oops, something went wrong.