-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
241 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,7 @@ XLA_SHA256 = "" | |
# ) | ||
|
||
local_repository( | ||
name = "xlae", | ||
name = "xla", | ||
path = "./xla" | ||
) | ||
|
||
|
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
class InactiveOp<string dialect_, string opName_> { | ||
string dialect = dialect_; | ||
string opName = opName_; | ||
} | ||
|
||
class AllocationOp<string dialect_, string opName_> { | ||
string dialect = dialect_; | ||
string opName = opName_; | ||
} | ||
|
||
class ControlFlowOp<string dialect_, string opName_, string impl_> { | ||
string dialect = dialect_; | ||
string opName = opName_; | ||
string impl = impl_; | ||
} | ||
|
||
class MemoryIdentityOp<string dialect_, string opName_, list<int> ptrargs_, list<int> storedargs_ = []> { | ||
string dialect = dialect_; | ||
string opName = opName_; | ||
list<int> ptrargs = ptrargs_; | ||
list<int> storedargs = storedargs_; | ||
} | ||
|
||
class ReadOnlyIdentityOp<string dialect_, string opName_, list<int> ptrargs_> : MemoryIdentityOp<dialect_, opName_, ptrargs_>; | ||
|
||
class BranchOp<string dialect_, string opName_> { | ||
string dialect = dialect_; | ||
string opName = opName_; | ||
} | ||
|
||
class RegionTerminatorOp<string dialect_, string opName_> { | ||
string dialect = dialect_; | ||
string opName = opName_; | ||
} | ||
|
||
class MLIRDerivative<string dialect_, string opName_, dag patternToMatch, list<dag> resultOps> { | ||
string dialect = dialect_; | ||
string opName = opName_; | ||
dag PatternToMatch = patternToMatch; | ||
list<dag> ArgDerivatives = resultOps; | ||
} | ||
|
||
class Operation<bit usesPrimal_, bit usesShadow_, bit usesCustom_=0> { | ||
bit usesPrimal = usesPrimal_; | ||
bit usesShadow = usesShadow_; | ||
bit usesCustom = usesCustom_; | ||
} | ||
|
||
class DiffeRetIndex<list<int> indices_> { | ||
list<int> indices = indices_; | ||
} | ||
def DiffeRet : DiffeRetIndex<[-1]>; | ||
|
||
class Inst<string mnemonic, string dialect_> : Operation</*primal*/1, /*shadow*/0> { | ||
string name = mnemonic; | ||
string dialect = dialect_; | ||
} | ||
|
||
def Op { | ||
} | ||
|
||
class MHLOInst<string m> : Inst<m, "mhlo">; | ||
|
||
def Add : MHLOInst<"mhlo::AddOp">; | ||
def Sub : MHLOInst<"mhlo::SubOp">; | ||
def Neg : MHLOInst<"mhlo::NegOp">; | ||
def Mul : MHLOInst<"mhlo::MulOp">; | ||
def Div : MHLOInst<"mhlo::DivOp">; | ||
def Rem : MHLOInst<"mhlo::RemOp">; |
74 changes: 74 additions & 0 deletions
74
src/enzyme_ad/jax/Implementations/MHLOAutoDiffOpInterfaceImpl.cpp
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,74 @@ | ||
//===- ArithAutoDiffOpInterfaceImpl.cpp - Interface external model --------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file contains the external model implementation of the automatic | ||
// differentiation op interfaces for the upstream MLIR arithmetic dialect. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "Enzyme/MLIR/Implementations/CoreDialectsAutoDiffImplementations.h" | ||
#include "Enzyme/MLIR/Interfaces/AutoDiffOpInterface.h" | ||
#include "Enzyme/MLIR/Interfaces/GradientUtils.h" | ||
#include "Enzyme/MLIR/Interfaces/GradientUtilsReverse.h" | ||
#include "mlir/IR/DialectRegistry.h" | ||
#include "mlir/Support/LogicalResult.h" | ||
|
||
#include "Dialect/Ops.h" | ||
#include "mlir/IR/TypeSupport.h" | ||
|
||
#include "xla/mlir_hlo/mhlo/IR/hlo_ops.h" | ||
|
||
#include "src/enzyme_ad/jax/Implementations/XLADerivatives.h" | ||
|
||
using namespace mlir; | ||
using namespace mlir::enzyme; | ||
|
||
namespace { | ||
#include "src/enzyme_ad/jax/Implementations/MHLODerivatives.inc" | ||
|
||
#ifndef DISABLE_MHLO_TENSOR_INTERFACE | ||
class MHLOTensorTypeInterface | ||
: public AutoDiffTypeInterface::ExternalModel<MHLOTensorTypeInterface, | ||
TensorType> { | ||
public: | ||
Value createNullValue(Type self, OpBuilder &builder, Location loc) const { | ||
auto tenType = self.cast<TensorType>(); | ||
auto attr = DenseElementsAttr::get(tenType, 0); | ||
return builder.create<mhlo::ConstantOp>(loc, tenType, attr); | ||
} | ||
|
||
Value createAddOp(Type self, OpBuilder &builder, Location loc, Value a, | ||
Value b) const { | ||
return builder.create<mhlo::AddOp>(loc, a, b); | ||
} | ||
|
||
Type getShadowType(Type self, unsigned width) const { | ||
assert(width == 1 && "unsupported width != 1"); | ||
return self; | ||
} | ||
|
||
bool requiresShadow(Type self) const { return false; } | ||
LogicalResult zeroInPlace(Type self, OpBuilder &builder, Location loc, | ||
Value val) const { | ||
return failure(); | ||
} | ||
}; | ||
#endif | ||
} // namespace | ||
|
||
void mlir::enzyme::registerMHLODialectAutoDiffInterface( | ||
DialectRegistry ®istry) { | ||
registry.addExtension(+[](MLIRContext *context, mhlo::MhloDialect *) { | ||
registerInterfaces(context); | ||
|
||
#ifndef DISABLE_MHLO_TENSOR_INTERFACE | ||
UnrankedTensorType::attachInterface<MHLOTensorTypeInterface>(*context); | ||
RankedTensorType::attachInterface<MHLOTensorTypeInterface>(*context); | ||
#endif | ||
}); | ||
} |
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,8 @@ | ||
include "Common.td" | ||
|
||
def : MLIRDerivative<"mhlo", "AddOp", (Op $x, $y), | ||
[ | ||
(DiffeRet), | ||
(DiffeRet), | ||
] | ||
>; |
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,11 @@ | ||
#include "mlir/IR/DialectRegistry.h" | ||
|
||
namespace mlir { | ||
namespace enzyme { | ||
void registerMHLODialectAutoDiffInterface(mlir::DialectRegistry ®istry); | ||
|
||
static inline void registerXLAAutoDiffInterfaces(mlir::DialectRegistry ®istry) { | ||
registerMHLODialectAutoDiffInterface(registry); | ||
} | ||
} | ||
} |
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