-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#sdy support JAX callbacks through the Shardy XLA round-trip pipeline.
PiperOrigin-RevId: 705875941
- Loading branch information
1 parent
d83a315
commit e076af4
Showing
17 changed files
with
485 additions
and
9 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
120 changes: 120 additions & 0 deletions
120
xla/service/spmd/shardy/mhlo_round_trip/export_callback_custom_calls.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,120 @@ | ||
/* 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/mhlo_round_trip/export_callback_custom_calls.h" | ||
|
||
#include <memory> | ||
|
||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Support/Casting.h" | ||
#include "mlir/IR/BuiltinOps.h" | ||
#include "mlir/IR/BuiltinTypes.h" | ||
#include "mlir/IR/DialectRegistry.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Pass/PassRegistry.h" | ||
#include "mlir/Support/TypeID.h" | ||
#include "shardy/dialect/sdy/ir/dialect.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::ModuleOp; | ||
using ::mlir::OperationPass; | ||
using ::mlir::PassWrapper; | ||
using ::mlir::StringRef; | ||
|
||
using ::mlir::stablehlo::CustomCallOp; | ||
|
||
// Attempts to replace the `CustomCallOp` with a tuple version of it, and a | ||
// `GetTupleElementOp` that gets the first element of the tuple. | ||
// | ||
// This only happens if the op has a single result and the result type is not | ||
// a tuple. | ||
void replaceCallbackWithTupleVersion(CustomCallOp customCall, | ||
mlir::IRRewriter& rewriter) { | ||
if (customCall.getNumResults() != 1 || | ||
isa<mlir::TupleType>(customCall->getResultTypes().front())) { | ||
return; | ||
} | ||
CustomCallOp tupleCustomCall = cloneCustomCallWithNewResultTypes( | ||
customCall, | ||
mlir::TupleType::get(customCall->getContext(), | ||
{customCall->getResultTypes()}), | ||
rewriter); | ||
auto getTupleElement = rewriter.create<mlir::stablehlo::GetTupleElementOp>( | ||
customCall.getLoc(), customCall->getResultTypes().front(), | ||
tupleCustomCall.getResult(0), rewriter.getI32IntegerAttr(0)); | ||
getTupleElement->setAttr(kXlaShardingAttr, | ||
customCall->getAttr(kXlaShardingAttr)); | ||
rewriter.replaceOp(customCall, getTupleElement); | ||
} | ||
|
||
class MhloRoundTripExportCallbackCustomCallsPass | ||
: public PassWrapper<MhloRoundTripExportCallbackCustomCallsPass, | ||
OperationPass<ModuleOp>> { | ||
public: | ||
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID( | ||
MhloRoundTripExportCallbackCustomCallsPass) | ||
|
||
void runOnOperation() final { | ||
getOperation().walk([&](CustomCallOp customCall) { | ||
if (!isPythonCallbackCustomCall(customCall)) { | ||
return; | ||
} | ||
mlir::IRRewriter rewriter(customCall); | ||
if (!customCall->use_empty()) { | ||
replaceCallbackWithTupleVersion(customCall, rewriter); | ||
return; | ||
} | ||
CustomCallOp newCustomCall = cloneCustomCallWithNewResultTypes( | ||
customCall, mlir::TypeRange(), rewriter); | ||
newCustomCall.setResultLayoutsAttr(rewriter.getArrayAttr({})); | ||
rewriter.eraseOp(customCall); | ||
return; | ||
}); | ||
} | ||
|
||
StringRef getArgument() const override { | ||
return "xla-sdy-mhlo-round-trip-export-callback-custom-calls"; | ||
} | ||
|
||
StringRef getDescription() const override { | ||
return "Converts the `CustomCallOp`s for host callbacks in XLA into the " | ||
"pattern that the XLA compiler recognizes."; | ||
} | ||
|
||
void getDependentDialects(mlir::DialectRegistry& registry) const final { | ||
registry.insert<mlir::sdy::SdyDialect>(); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
std::unique_ptr<mlir::Pass> createMhloRoundTripExportCallbackCustomCallsPass() { | ||
return std::make_unique<MhloRoundTripExportCallbackCustomCallsPass>(); | ||
} | ||
|
||
void registerMhloRoundTripExportCallbackCustomCallsPass() { | ||
mlir::registerPass(createMhloRoundTripExportCallbackCustomCallsPass); | ||
} | ||
|
||
} // namespace sdy | ||
} // namespace xla |
42 changes: 42 additions & 0 deletions
42
xla/service/spmd/shardy/mhlo_round_trip/export_callback_custom_calls.h
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,42 @@ | ||
/* 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_MHLO_ROUND_TRIP_EXPORT_CALLBACK_CUSTOM_CALLS_H_ | ||
#define XLA_SERVICE_SPMD_SHARDY_MHLO_ROUND_TRIP_EXPORT_CALLBACK_CUSTOM_CALLS_H_ | ||
|
||
#include <memory> | ||
|
||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Pass/PassRegistry.h" | ||
|
||
namespace xla { | ||
namespace sdy { | ||
|
||
// Creates a pass that converts the `CustomCallOp`s for host callbacks in XLA | ||
// into the pattern that the XLA compiler recognizes. | ||
// | ||
// The rest of the XLA pipeline expects host callback custom calls to either be | ||
// a tuple with a get_tuple_element or no results (which we changed due to | ||
// shardy shardings expecting at least one result, and needing to attach a | ||
// maximal sharding to the callbacks). | ||
std::unique_ptr<mlir::Pass> createMhloRoundTripExportCallbackCustomCallsPass(); | ||
|
||
// Registers the xla-sdy-mhlo-round-trip-export-callback-custom-calls pass. | ||
void registerMhloRoundTripExportCallbackCustomCallsPass(); | ||
|
||
} // namespace sdy | ||
} // namespace xla | ||
|
||
#endif // XLA_SERVICE_SPMD_SHARDY_MHLO_ROUND_TRIP_EXPORT_CALLBACK_CUSTOM_CALLS_H_ |
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
Oops, something went wrong.