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

const prop for log and logp1 #218

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
56 changes: 54 additions & 2 deletions src/enzyme_ad/jax/Passes/EnzymeHLOOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2209,6 +2209,57 @@ struct DynamicUpdateSliceConstProp final
}
};

struct LogConstProp final : OpRewritePattern<mlir::stablehlo::LogOp> {
using OpRewritePattern::OpRewritePattern;

LogicalResult matchAndRewrite(mlir::stablehlo::LogOp op,
PatternRewriter &rewriter) const override {
// return if not constant
DenseElementsAttr inputAttr;
if (!matchPattern(op.getOperand(), m_Constant(&inputAttr)))
return failure();

// get the resultType
auto resultType = op.getType().cast<ShapedType>();

// Convert constant to tensor, compute log, then convert back to attribute
stablehlo::Tensor inputTen = mlir::stablehlo::constantOp(inputAttr);
vimarsh6739 marked this conversation as resolved.
Show resolved Hide resolved
auto out = mlir::stablehlo::logOp(inputTen, resultType);

// Replace with new constant op containing the computed result
rewriter.replaceOpWithNewOp<stablehlo::ConstantOp>(op, resultType,
fromTensor(out));

return success();
}
};

struct LogPlusConstProp final : OpRewritePattern<mlir::stablehlo::Log1pOp> {

using OpRewritePattern::OpRewritePattern;

LogicalResult matchAndRewrite(mlir::stablehlo::Log1pOp op,
PatternRewriter &rewriter) const override {
// return if not constant
DenseElementsAttr inputAttr;
if (!matchPattern(op.getOperand(), m_Constant(&inputAttr)))
return failure();

// get the resultType
auto resultType = op.getType().cast<ShapedType>();

// Convert constant to tensor, compute log, then convert back to attribute
stablehlo::Tensor inputTen = mlir::stablehlo::constantOp(inputAttr);
auto out = mlir::stablehlo::log1pOp(inputTen, resultType);

// Replace with new constant op containing the computed result
rewriter.replaceOpWithNewOp<stablehlo::ConstantOp>(op, resultType,
fromTensor(out));

return success();
}
};

struct ConcatConstProp final
: OpRewritePattern<mlir::stablehlo::ConcatenateOp> {
using OpRewritePattern::OpRewritePattern;
Expand Down Expand Up @@ -6963,8 +7014,9 @@ struct EnzymeHLOOptPass : public EnzymeHLOOptPassBase<EnzymeHLOOptPass> {
patterns.add<ConvertConcat, DynamicUpdateToConcat, SliceOfDynamicUpdate,
SliceElementwise, SliceReshapeElementwise, SlicePad,
SliceReshapePad, DotReshapeDot, ConcatConstProp,
DynamicUpdateSliceConstProp, ConcatFuse, ConcatToBroadcast,
PadPad, PadReshapePad, ConcatPushBinop<stablehlo::AddOp>,
DynamicUpdateSliceConstProp, LogConstProp, LogPlusConstProp,
ConcatFuse, ConcatToBroadcast, PadPad, PadReshapePad,
ConcatPushBinop<stablehlo::AddOp>,
ConcatPushBinop<stablehlo::MulOp>, ScatterToDynamicUpdateSlice,
ReduceConcat, ConcatSlice, SliceConcat, SliceReshapeConcat,
BinBroadcastSplat<stablehlo::AddOp>,
Expand Down
9 changes: 9 additions & 0 deletions src/enzyme_ad/jax/TransformOps/TransformOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ def ApplyGatherSimplifyPatterns : EnzymeHLOPatternOp<
"gather_simplify"> {
let patterns = ["GatherSimplify"];
}
def ApplyLogConstProp : EnzymeHLOPatternOp<
"log_const_prop">{
let patterns = ["LogConstProp"];
}

def ApplyLogPlusConstProp : EnzymeHLOPatternOp<
"log_plus_one_const_prop">{
let patterns = ["LogPlusConstProp"];
}

// regular benefit
def ApplyConvertConcatPatterns : EnzymeHLOPatternOp<
Expand Down
2 changes: 2 additions & 0 deletions src/enzyme_ad/jax/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ def hlo_opts():
dot_reshape_dot<1>;
concat_const_prop<1>;
dynamic_update_slice_const_prop<1>;
log_const_prop<1>;
log_plus_one_const_prop<1>;
concat_fuse<1>;
pad_reshape_pad<1>;
pad_pad<1>;
Expand Down
30 changes: 30 additions & 0 deletions test/lit_tests/log_const_prop.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: enzymexlamlir-opt %s --pass-pipeline="builtin.module(enzyme-hlo-opt)" | FileCheck %s

module {
func.func @log_f32() -> tensor<2xf32> {
%arg = stablehlo.constant dense<[1.000000e+00,2.000000e+00]> : tensor<2xf32>
%result = stablehlo.log %arg : tensor<2xf32>
func.return %result : tensor<2xf32>
}


func.func @log_plus_one_op_test_f64() -> tensor<5xf64> {
%operand = stablehlo.constant dense<[0.0, -0.999, 7.0, 6.38905621, 15.0]> : tensor<5xf64>
%result = stablehlo.log_plus_one %operand : tensor<5xf64>
func.return %result : tensor<5xf64>
}


}


// CHECK: func.func @log_f32() -> tensor<2xf32> {
// CHECK-NEXT: %cst = stablehlo.constant dense<[0.000000e+00, 0.693147182]> : tensor<2xf32>
// CHECK-NEXT: return %cst : tensor<2xf32>
// CHECK-NEXT: }


// CHECK: func.func @log_plus_one_op_test_f64() -> tensor<5xf64> {
// CHECK-NEXT: %cst = stablehlo.constant dense<[0.000000e+00, -6.9077552789821359, 2.0794415416798357, 2.0000000150316017, 2.7725887222397811]> : tensor<5xf64>
// CHECK-NEXT: return %cst : tensor<5xf64>
// CHECK-NEXT: }
Loading