-
Notifications
You must be signed in to change notification settings - Fork 99
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
14 changed files
with
269 additions
and
1 deletion.
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
72 changes: 72 additions & 0 deletions
72
enzyme/Enzyme/MLIR/Implementations/ComplexAutoDiffOpInterfaceImpl.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,72 @@ | ||
//===- ComplexAutoDiffOpInterfaceImpl.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 complex dialect. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "Implementations/CoreDialectsAutoDiffImplementations.h" | ||
#include "Interfaces/AutoDiffOpInterface.h" | ||
#include "Interfaces/AutoDiffTypeInterface.h" | ||
#include "Interfaces/GradientUtils.h" | ||
#include "Interfaces/GradientUtilsReverse.h" | ||
|
||
#include "mlir/Dialect/Complex/IR/Complex.h" | ||
#include "mlir/IR/DialectRegistry.h" | ||
#include "mlir/Support/LogicalResult.h" | ||
|
||
using namespace mlir; | ||
using namespace mlir::enzyme; | ||
|
||
namespace { | ||
#include "Implementations/ComplexDerivatives.inc" | ||
|
||
|
||
class ComplexTypeInterface | ||
: public AutoDiffTypeInterface::ExternalModel<ComplexTypeInterface, | ||
ComplexType> { | ||
public: | ||
Value createNullValue(Type self, OpBuilder &builder, Location loc) const { | ||
auto fltType = self.cast<ComplexType>().getElementType().cast<FloatType>(); | ||
mlir::Attribute attrs[2] = { | ||
builder.getFloatAttr(fltType, APFloat(fltType.getFloatSemantics(), 0)), | ||
builder.getFloatAttr(fltType, APFloat(fltType.getFloatSemantics(), 0)) | ||
}; | ||
return builder.create<complex::ConstantOp>( | ||
loc, self, builder.getArrayAttr(attrs)); | ||
} | ||
|
||
Value createAddOp(Type self, OpBuilder &builder, Location loc, Value a, | ||
Value b) const { | ||
return builder.create<complex::AddOp>(loc, a, b); | ||
} | ||
Value createConjOp(Type self, OpBuilder &builder, Location loc, Value a) const { | ||
return builder.create<complex::ConjOp>(loc, a); | ||
} | ||
|
||
Type getShadowType(Type self, unsigned width) const { | ||
assert(width == 1 && "unsupported width != 1"); | ||
return self; | ||
} | ||
|
||
bool isMutable(Type self) const { return false; } | ||
LogicalResult zeroInPlace(Type self, OpBuilder &builder, Location loc, | ||
Value val) const { | ||
return failure(); | ||
} | ||
}; | ||
} // namespace | ||
|
||
void mlir::enzyme::registerComplexDialectAutoDiffInterface( | ||
DialectRegistry ®istry) { | ||
registry.addExtension(+[](MLIRContext *context, complex::ComplexDialect *) { | ||
registerInterfaces(context); | ||
ComplexType::attachInterface<ComplexTypeInterface>(*context); | ||
}); | ||
} |
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,17 @@ | ||
include "Common.td" | ||
|
||
def : MLIRDerivative<"complex", "AddOp", (Op $x, $y), | ||
[ | ||
(DiffeRet), | ||
(DiffeRet), | ||
] | ||
>; | ||
|
||
def CMul : ComplexInst<"MulOp">; | ||
|
||
def : MLIRDerivative<"complex", "MulOp", (Op $x, $y), | ||
[ | ||
(CMul (DiffeRet), $y), | ||
(CMul (DiffeRet), $x) | ||
] | ||
>; |
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
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,21 @@ | ||
// RUN: %eopt --enzyme --canonicalize --remove-unnecessary-enzyme-ops --canonicalize --enzyme-simplify-math --cse %s | FileCheck %s | ||
|
||
module { | ||
func.func @square(%x: complex<f64>) -> complex<f64> { | ||
%next = complex.mul %x, %x : complex<f64> | ||
return %next : complex<f64> | ||
} | ||
|
||
func.func @dsquare(%x: complex<f64>, %dr: complex<f64>) -> complex<f64> { | ||
%r = enzyme.autodiff @square(%x, %dr) { activity=[#enzyme<activity enzyme_active>], ret_activity=[#enzyme<activity enzyme_activenoneed>] } : (complex<f64>, complex<f64>) -> complex<f64> | ||
return %r : complex<f64> | ||
} | ||
} | ||
|
||
// CHECK: func.func private @diffesquare(%arg0: complex<f64>, %arg1: complex<f64>) -> complex<f64> | ||
// CHECK-NEXT: %0 = complex.conj %arg1 : complex<f64> | ||
// CHECK-NEXT: %1 = complex.mul %0, %arg0 : complex<f64> | ||
// CHECK-NEXT: %2 = complex.conj %1 : complex<f64> | ||
// CHECK-NEXT: %3 = complex.add %2, %2 : complex<f64> | ||
// CHECK-NEXT: return %3 : complex<f64> | ||
// CHECK-NEXT: } |
Oops, something went wrong.