From 400207726195eda2b5fde228c9bb5fa073222e19 Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" Date: Fri, 18 Sep 2020 04:25:55 -0700 Subject: [PATCH] [MLIR][KernelGen] Lower `tf.Sinh` to MLHLO PiperOrigin-RevId: 332425724 --- include/mlir-hlo/Dialect/mhlo/IR/chlo_ops.td | 14 +++++++ .../chlo_legalize_to_hlo_patterns.td | 39 +++++++++++++++++++ .../mhlo/transforms/transform_unranked_hlo.cc | 3 +- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/include/mlir-hlo/Dialect/mhlo/IR/chlo_ops.td b/include/mlir-hlo/Dialect/mhlo/IR/chlo_ops.td index 54b40fe..99a6d08 100644 --- a/include/mlir-hlo/Dialect/mhlo/IR/chlo_ops.td +++ b/include/mlir-hlo/Dialect/mhlo/IR/chlo_ops.td @@ -366,6 +366,20 @@ def HLOClient_AcosOp : HLOClient_UnaryElementwiseOp<"acos", [], }]; } +def HLOClient_SinhOp : HLOClient_UnaryElementwiseOp<"sinh", [], + HLO_FpOrComplexTensor> { + let summary = "Sinh operation"; + + let description = [{ + Returns `Sinh(operand)` element-wise. + + $$ + \sinh(x) = (e^x - e^-x) / 2 if |x| < 1 + = e^(x + log(1/2)) - e^(-x + log(1/2)) otherwise. + $$ + }]; +} + def HLOClient_TanOp : HLOClient_UnaryElementwiseOp<"tan", [], HLO_FpOrComplexTensor> { let summary = "Tan operation"; diff --git a/lib/Dialect/mhlo/transforms/chlo_legalize_to_hlo_patterns.td b/lib/Dialect/mhlo/transforms/chlo_legalize_to_hlo_patterns.td index 7b612ff..9d9e6d9 100644 --- a/lib/Dialect/mhlo/transforms/chlo_legalize_to_hlo_patterns.td +++ b/lib/Dialect/mhlo/transforms/chlo_legalize_to_hlo_patterns.td @@ -49,6 +49,45 @@ def : Pat<(HLOClient_AcosOp $input), ), (HLO_ConstantLike<"M_PI"> $input))>; +// Express `sinh` as +// sinh(x) = (e^x - e^-x) / 2 if |x| < 1 +// = e^(x + log(1/2)) - e^(-x + log(1/2)) otherwise. +def : Pat<(HLOClient_SinhOp $input), + (HLO_SelectOp + (HLO_CompareOp + (HLO_AbsOp $input), + (HLO_ConstantLike<"1"> $input), + HLO_COMPARISON_DIRECTION_LT + ), + (HLO_DivOp + (HLO_SubOp + (HLO_ExpOp $input), + (HLO_ExpOp + (HLO_NegOp $input) + ) + ), + (HLO_ConstantLike<"2"> $input) + ), + (HLO_SubOp + (HLO_ExpOp + (HLO_AddOp + $input, + (HLO_LogOp + (HLO_ConstantLike<"0.5"> $input) + ) + ) + ), + (HLO_ExpOp + (HLO_SubOp + (HLO_LogOp + (HLO_ConstantLike<"0.5"> $input) + ), + $input + ) + ) + ) + )>; + // Express tan in MHLO dialect as // tan(x) = sin(x) / cos(x). def : Pat<(HLOClient_TanOp $input), diff --git a/lib/Dialect/mhlo/transforms/transform_unranked_hlo.cc b/lib/Dialect/mhlo/transforms/transform_unranked_hlo.cc index 4a17a5b..0be4c68 100644 --- a/lib/Dialect/mhlo/transforms/transform_unranked_hlo.cc +++ b/lib/Dialect/mhlo/transforms/transform_unranked_hlo.cc @@ -47,7 +47,8 @@ namespace { sep fn(ShiftRightLogicalOp) sep fn(SubOp) // TODO(herhut): Generate these out of op definitions. -#define MAP_CHLO_OPERATION_CWISE_UNARY(fn, sep) fn(TanOp) sep fn(AcosOp) +#define MAP_CHLO_OPERATION_CWISE_UNARY(fn, sep) \ + fn(TanOp) sep fn(AcosOp) sep fn(SinhOp) template inline void AddLegalOpOnRankedTensor(ConversionTarget *target) {