Add sigmoid lowering

Follows existing conventions for activation functions
pull/296/head
dan 2021-08-30 02:03:38 +00:00 committed by Yi Zhang
parent 29e1b2fe89
commit d9df4bfc95
5 changed files with 63 additions and 4 deletions

View File

@ -149,7 +149,6 @@ class ElementwiseFlattenBroadcastModule(torch.nn.Module):
def ElementwiseFlattenBroadcastModule_basic(module, tu: TestUtils): def ElementwiseFlattenBroadcastModule_basic(module, tu: TestUtils):
module.forward(tu.rand(6), tu.rand()) module.forward(tu.rand(6), tu.rand())
# ============================================================================== # ==============================================================================
@ -169,3 +168,24 @@ class ElementwiseReluModule(torch.nn.Module):
@register_test_case(module_factory=lambda: ElementwiseReluModule()) @register_test_case(module_factory=lambda: ElementwiseReluModule())
def ElementwiseReluModule_basic(module, tu: TestUtils): def ElementwiseReluModule_basic(module, tu: TestUtils):
module.forward(tu.rand(4, 2) - 0.5) module.forward(tu.rand(4, 2) - 0.5)
# ==============================================================================
class ElementwiseSigmoidModule(torch.nn.Module):
def __init__(self):
super().__init__()
@export
@annotate_args([
None,
([-1, -1], torch.float32, True),
])
def forward(self, x):
return torch.sigmoid(x)
@register_test_case(module_factory=lambda: ElementwiseSigmoidModule())
def ElementwiseSigmoidModule_basic(module, tu: TestUtils):
module.forward(tu.rand(3, 5))

View File

@ -436,6 +436,7 @@ def emit_aten_ops(torch_ir_dir: str, registry: Registry):
for key in [ for key in [
"aten::tanh : (Tensor) -> (Tensor)", "aten::tanh : (Tensor) -> (Tensor)",
"aten::relu : (Tensor) -> (Tensor)", "aten::relu : (Tensor) -> (Tensor)",
"aten::sigmoid : (Tensor) -> (Tensor)",
"aten::sin : (Tensor) -> (Tensor)", "aten::sin : (Tensor) -> (Tensor)",
"aten::exp : (Tensor) -> (Tensor)", "aten::exp : (Tensor) -> (Tensor)",
"aten::cos : (Tensor) -> (Tensor)", "aten::cos : (Tensor) -> (Tensor)",

View File

@ -71,6 +71,34 @@ def Torch_AtenRelu_Op : Torch_Op<"aten.relu_", [
let assemblyFormat = "$self attr-dict `:` type($self) `->` type($result)"; let assemblyFormat = "$self attr-dict `:` type($self) `->` type($result)";
} }
def Torch_AtenSigmoidOp : Torch_Op<"aten.sigmoid", [
AllowsTypeRefinement,
HasValueSemantics
]> {
let summary = "Generated op for `aten::sigmoid : (Tensor) -> (Tensor)`";
let arguments = (ins
AnyTorchTensorType:$self
);
let results = (outs
AnyTorchTensorType:$result
);
let assemblyFormat = "$self attr-dict `:` type($self) `->` type($result)";
}
def Torch_AtenSigmoid_Op : Torch_Op<"aten.sigmoid_", [
IsTrailingUnderscoreInplaceVariant,
AllowsTypeRefinement
]> {
let summary = "Generated op for `aten::sigmoid_ : (Tensor) -> (Tensor)`";
let arguments = (ins
AnyTorchTensorType:$self
);
let results = (outs
AnyTorchTensorType:$result
);
let assemblyFormat = "$self attr-dict `:` type($self) `->` type($result)";
}
def Torch_AtenSinOp : Torch_Op<"aten.sin", [ def Torch_AtenSinOp : Torch_Op<"aten.sin", [
AllowsTypeRefinement, AllowsTypeRefinement,
HasValueSemantics HasValueSemantics

View File

@ -654,6 +654,14 @@ static Value createLinalgPayloadCalculationForElementwiseOp(
ArrayRef<Value> operands) { ArrayRef<Value> operands) {
if (isa<AtenTanhOp>(op)) if (isa<AtenTanhOp>(op))
return b.create<math::TanhOp>(loc, payloadArgs[0]); return b.create<math::TanhOp>(loc, payloadArgs[0]);
if (isa<AtenSigmoidOp>(op)){
Type elementType = payloadArgs[0].getType();
auto one = b.create<ConstantOp>(loc, FloatAttr::get(elementType, 1));
auto negate = b.create<NegFOp>(loc, payloadArgs[0]);
auto exp = b.create<math::ExpOp>(loc, negate);
auto added = b.create<AddFOp>(loc, exp, one);
return b.create<DivFOp>(loc, one, added);
}
if (auto relu = dyn_cast<AtenReluOp>(op)) { if (auto relu = dyn_cast<AtenReluOp>(op)) {
if (!relu.getType() if (!relu.getType()
.cast<ValueTensorType>() .cast<ValueTensorType>()
@ -775,7 +783,8 @@ struct ConvertElementwiseOp : ConversionPattern {
matchAndRewrite(Operation *op, ArrayRef<Value> operands, matchAndRewrite(Operation *op, ArrayRef<Value> operands,
ConversionPatternRewriter &rewriter) const override { ConversionPatternRewriter &rewriter) const override {
if (!isa<AtenTanhOp, AtenReluOp, AtenAddTensorOp, AtenMulTensorOp, if (!isa<AtenTanhOp, AtenReluOp, AtenAddTensorOp, AtenMulTensorOp,
AtenDivTensorOp, AtenSubTensorOp, AtenLerpTensorOp>(op)) AtenDivTensorOp, AtenSubTensorOp, AtenLerpTensorOp,
AtenSigmoidOp>(op))
return rewriter.notifyMatchFailure(op, "not a supported elementwise op"); return rewriter.notifyMatchFailure(op, "not a supported elementwise op");
if (failed(verifyLinalgCompatibleTypes(op, rewriter))) if (failed(verifyLinalgCompatibleTypes(op, rewriter)))
@ -1137,7 +1146,8 @@ public:
patterns.add<ConvertAtenBatchNormOp>(typeConverter, context); patterns.add<ConvertAtenBatchNormOp>(typeConverter, context);
target target
.addIllegalOp<AtenTanhOp, AtenReluOp, AtenAddTensorOp, AtenMulTensorOp, .addIllegalOp<AtenTanhOp, AtenReluOp, AtenAddTensorOp, AtenMulTensorOp,
AtenDivTensorOp, AtenSubTensorOp, AtenLerpTensorOp>(); AtenDivTensorOp, AtenSubTensorOp, AtenLerpTensorOp,
AtenSigmoidOp>();
patterns.add<ConvertElementwiseOp>(typeConverter, context); patterns.add<ConvertElementwiseOp>(typeConverter, context);
target.addIllegalOp<AtenUnsqueezeOp>(); target.addIllegalOp<AtenUnsqueezeOp>();
patterns.add<ConvertAtenUnsqueezeOp>(typeConverter, context); patterns.add<ConvertAtenUnsqueezeOp>(typeConverter, context);

View File

@ -175,7 +175,7 @@ public:
AtenSubScalarOp, AtenMulScalarOp, AtenDivScalarOp, AtenFmodScalarOp, AtenSubScalarOp, AtenMulScalarOp, AtenDivScalarOp, AtenFmodScalarOp,
AtenFloorDivideScalarOp, AtenEqScalarOp, AtenGeScalarOp, AtenFloorDivideScalarOp, AtenEqScalarOp, AtenGeScalarOp,
AtenNeScalarOp, AtenBitwiseNotOp, AtenToDtypeOp, AtenExpOp, AtenNeScalarOp, AtenBitwiseNotOp, AtenToDtypeOp, AtenExpOp,
AtenSinOp, AtenCosOp, DerefineOp>(op)) { AtenSinOp, AtenCosOp, AtenSigmoidOp, DerefineOp>(op)) {
return getLatticeElement(op->getResult(0)).join(*operands[0]); return getLatticeElement(op->getResult(0)).join(*operands[0]);
} }