Skip to content

Commit

Permalink
[RELAY][OP] Operators. pool2d, global_pool2d, batch_flatten, tanh, si…
Browse files Browse the repository at this point in the history
…gmoid, floor, ceil, trunc, abs, negative, multiply, mod, pow, resize (apache#1813)
  • Loading branch information
srkreddy1238 authored and tqchen committed Oct 7, 2018
1 parent 5078e8e commit 4fd3604
Show file tree
Hide file tree
Showing 28 changed files with 1,648 additions and 71 deletions.
59 changes: 59 additions & 0 deletions docs/langref/relay_op.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ This level enables fully connected multi-layer perceptron.
tvm.relay.expand_dims
tvm.relay.concatenate
tvm.relay.nn.softmax
tvm.relay.subtract
tvm.relay.multiply
tvm.relay.divide
tvm.relay.mod
tvm.relay.tanh
tvm.relay.sigmoid


**Level 2: Convolutions**

Expand All @@ -39,10 +46,18 @@ This level enables typical convnet models.
:nosignatures:

tvm.relay.nn.conv2d
tvm.relay.nn.max_pool2d
tvm.relay.nn.avg_pool2d
tvm.relay.nn.global_max_pool2d
tvm.relay.nn.global_avg_pool2d
tvm.relay.nn.upsampling
tvm.relay.nn.batch_flatten


**Level 3: Additional Math And Transform Operators**

This level enables additional math and transform operators.

.. autosummary::
:nosignatures:

Expand All @@ -51,6 +66,13 @@ This level enables typical convnet models.
tvm.relay.reshape
tvm.relay.copy
tvm.relay.transpose
tvm.relay.floor
tvm.relay.ceil
tvm.relay.trunc
tvm.relay.round
tvm.relay.abs
tvm.relay.negative


**Level 4: Broadcast and Reductions**

Expand All @@ -67,9 +89,15 @@ This level enables typical convnet models.
tvm.relay.less_equal
tvm.relay.maximum
tvm.relay.minimum
tvm.relay.pow

**Level 5: Vision/Image Operators**

.. autosummary::
:nosignatures:

tvm.relay.image.resize


Level 1 Definitions
-------------------
Expand All @@ -78,12 +106,38 @@ Level 1 Definitions
.. autofunction:: tvm.relay.exp
.. autofunction:: tvm.relay.sigmoid
.. autofunction:: tvm.relay.add
.. autofunction:: tvm.relay.subtract
.. autofunction:: tvm.relay.multiply
.. autofunction:: tvm.relay.divide
.. autofunction:: tvm.relay.mod
.. autofunction:: tvm.relay.tanh
.. autofunction:: tvm.relay.sigmoid
.. autofunction:: tvm.relay.concatenate
.. autofunction:: tvm.relay.nn.softmax


Level 2 Definitions
-------------------
.. autofunction:: tvm.relay.nn.conv2d
.. autofunction:: tvm.relay.nn.max_pool2d
.. autofunction:: tvm.relay.nn.avg_pool2d
.. autofunction:: tvm.relay.nn.global_max_pool2d
.. autofunction:: tvm.relay.nn.global_avg_pool2d
.. autofunction:: tvm.relay.nn.upsampling
.. autofunction:: tvm.relay.nn.batch_flatten


Level 3 Definitions
-------------------
.. autofunction:: tvm.relay.floor
.. autofunction:: tvm.relay.ceil
.. autofunction:: tvm.relay.trunc
.. autofunction:: tvm.relay.round
.. autofunction:: tvm.relay.abs
.. autofunction:: tvm.relay.negative
.. autofunction:: tvm.relay.reshape
.. autofunction:: tvm.relay.copy
.. autofunction:: tvm.relay.transpose

Level 4 Definitions
-------------------
Expand All @@ -97,3 +151,8 @@ Level 4 Definitions
.. autofunction:: tvm.relay.less_equal
.. autofunction:: tvm.relay.maximum
.. autofunction:: tvm.relay.minimum
.. autofunction:: tvm.relay.pow

Level 5 Definitions
-------------------
.. autofunction:: tvm.relay.image.resize
41 changes: 41 additions & 0 deletions include/tvm/relay/attrs/image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*!
* Copyright (c) 2018 by Contributors
* \file tvm/relay/attrs/image.h
* \brief Auxiliary attributes for image operators.
*/
#ifndef TVM_RELAY_ATTRS_IMAGE_H_
#define TVM_RELAY_ATTRS_IMAGE_H_

#include <tvm/attrs.h>
#include <string>

namespace tvm {
namespace relay {

/*! \brief Attributes used in image resize operator */
struct ResizeAttrs : public tvm::AttrsNode<ResizeAttrs> {
Array<IndexExpr> size;
std::string layout;
std::string method;
bool align_corners;

TVM_DECLARE_ATTRS(ResizeAttrs, "relay.attrs.ResizeAttrs") {
TVM_ATTR_FIELD(size).set_default(NullValue<Array<IndexExpr> >())
.describe("Output Size.");
TVM_ATTR_FIELD(layout).set_default("NCHW")
.describe("Dimension ordering of input data. Can be 'NCHW', 'NHWC', etc."
"'N', 'C', 'H', 'W' stands for batch, channel, height, and width"
"dimensions respectively. Resize is applied on the 'H' and"
"'W' dimensions.");
TVM_ATTR_FIELD(method).set_default("BILINEAR")
.describe("Specify the mode to use for scaling."
"NEAREST_NEIGHBOR - Nearest Neighbor"
"BILINEAR - Bilinear Interpolation");
TVM_ATTR_FIELD(align_corners).set_default(false)
.describe("Should be true to preserve the values at the corner pixels");
}
};

} // namespace relay
} // namespace tvm
#endif // TVM_RELAY_ATTRS_IMAGE_H_
96 changes: 96 additions & 0 deletions include/tvm/relay/attrs/nn.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,102 @@ struct SoftmaxAttrs : public tvm::AttrsNode<SoftmaxAttrs> {
}
};

/*! \brief Attributes for max pool operator */
struct MaxPool2DAttrs : public tvm::AttrsNode<MaxPool2DAttrs> {
Array<IndexExpr> pool_size;
Array<IndexExpr> strides;
Array<IndexExpr> padding;
std::string layout;
bool ceil_mode;

TVM_DECLARE_ATTRS(MaxPool2DAttrs, "relay.attrs.MaxPool2DAttrs") {
TVM_ATTR_FIELD(pool_size)
.describe("Size of the pooling windows.");
TVM_ATTR_FIELD(strides).set_default(Array<IndexExpr>({1, 1}))
.describe("Specifies the strides of the convolution.");
TVM_ATTR_FIELD(padding).set_default(Array<IndexExpr>({0, 0}))
.describe("If padding is non-zero, then the input is implicitly zero-padded"
"Padding support both symmetric and asymmetric as"
"one int : same padding used on all sides"
"two int : bottom, right will use same padding as top, left"
"four int : padding width in the order of (top, left, bottom, right)");
TVM_ATTR_FIELD(layout).set_default("NCHW")
.describe("Dimension ordering of data and weight. Can be 'NCHW', 'NHWC', etc."
"'N', 'C', 'H', 'W' stands for batch, channel, height, and width"
"dimensions respectively. Convolution is applied on the 'H' and"
"'W' dimensions.");
TVM_ATTR_FIELD(ceil_mode).set_default(false)
.describe("When true, will use ceil instead of floor to compute the output shape.");
}
};

/*! \brief Attributes for avg pool operator */
struct AvgPool2DAttrs : public tvm::AttrsNode<AvgPool2DAttrs> {
Array<IndexExpr> pool_size;
Array<IndexExpr> strides;
Array<IndexExpr> padding;
std::string layout;
bool ceil_mode;
bool count_include_pad;

TVM_DECLARE_ATTRS(AvgPool2DAttrs, "relay.attrs.AvgPool2DAttrs") {
TVM_ATTR_FIELD(pool_size)
.describe("Size of the pooling windows.");
TVM_ATTR_FIELD(strides).set_default(Array<IndexExpr>({1, 1}))
.describe("Specifies the strides of the convolution.");
TVM_ATTR_FIELD(padding).set_default(Array<IndexExpr>({0, 0}))
.describe("If padding is non-zero, then the input is implicitly zero-padded"
"Padding support both symmetric and asymmetric as"
"one int : same padding used on all sides"
"two int : bottom, right will use same padding as top, left"
"four int : padding width in the order of (top, left, bottom, right)");
TVM_ATTR_FIELD(layout).set_default("NCHW")
.describe("Dimension ordering of data and weight. Can be 'NCHW', 'NHWC', etc."
"'N', 'C', 'H', 'W' stands for batch, channel, height, and width"
"dimensions respectively. Convolution is applied on the 'H' and"
"'W' dimensions.");
TVM_ATTR_FIELD(ceil_mode).set_default(false)
.describe("When true, will use ceil instead of floor to compute the output shape.");
TVM_ATTR_FIELD(count_include_pad).set_default(false)
.describe("When true, will include padding to compute the average");
}
};

/*! \brief Attributes for global pool operator */
struct GlobalPool2DAttrs : public tvm::AttrsNode<GlobalPool2DAttrs> {
std::string layout;

TVM_DECLARE_ATTRS(GlobalPool2DAttrs, "relay.attrs.GlobalPool2DAttrs") {
TVM_ATTR_FIELD(layout).set_default("NCHW")
.describe("Dimension ordering of data and weight. Can be 'NCHW', 'NHWC', etc."
"'N', 'C', 'H', 'W' stands for batch, channel, height, and width"
"dimensions respectively. Convolution is applied on the 'H' and"
"'W' dimensions.");
}
};

/*! \brief Attributes for upsampling operator */
struct UpSamplingAttrs : public tvm::AttrsNode<UpSamplingAttrs> {
int scale;
std::string layout;
std::string method;

TVM_DECLARE_ATTRS(UpSamplingAttrs, "relay.attrs.UpSamplingAttrs") {
TVM_ATTR_FIELD(scale)
.describe("Should be true to preserve the values at the corner pixels");
TVM_ATTR_FIELD(layout).set_default("NCHW")
.describe("Dimension ordering of input data. Can be 'NCHW', 'NHWC', etc."
"'N', 'C', 'H', 'W' stands for batch, channel, height, and width"
"dimensions respectively. Upsampling is applied on the 'H' and"
"'W' dimensions.");
TVM_ATTR_FIELD(method).set_default("NEAREST_NEIGHBOR")
.describe("Specify the mode to use for scaling."
"NEAREST_NEIGHBOR - Nearest Neighbor"
"BILINEAR - Bilinear Interpolation");
}
};


} // namespace relay
} // namespace tvm
#endif // TVM_RELAY_ATTRS_NN_H_
17 changes: 17 additions & 0 deletions include/tvm/relay/attrs/vision.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*!
* Copyright (c) 2018 by Contributors
* \file tvm/relay/attrs/vision.h
* \brief Auxiliary attributes for vision operators.
*/
#ifndef TVM_RELAY_ATTRS_VISION_H_
#define TVM_RELAY_ATTRS_VISION_H_

#include <tvm/attrs.h>
#include <string>

namespace tvm {
namespace relay {

} // namespace relay
} // namespace tvm
#endif // TVM_RELAY_ATTRS_VISION_H_
6 changes: 4 additions & 2 deletions python/tvm/relay/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pylint: disable=wildcard-import
# pylint: disable=wildcard-import, redefined-builtin
"""The Relay IR namespace containing the IR definition and compiler."""
from . import base
from . import ty
Expand All @@ -10,8 +10,10 @@
# Root operators
from .op import Op
from .op.tensor import *
from . import nn
from .op.transform import *
from . import nn
from . import vision
from . import image

# Span
Span = base.Span
Expand Down
4 changes: 4 additions & 0 deletions python/tvm/relay/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# pylint: disable=wildcard-import, unused-import, unused-wildcard-import
"""Image nets related operators."""
# Re-export in a specific file name so that autodoc can pick it up
from .op.image import *
7 changes: 4 additions & 3 deletions python/tvm/relay/op/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#pylint: disable=wildcard-import
#pylint: disable=wildcard-import, redefined-builtin
"""Relay core operators."""
# operator defs
from .op import get, register, Op

# Operators
from .tensor import *
from . import nn
from .transform import *

from . import nn
from . import image
from . import vision

# operator registry
from . import _tensor
Expand Down
4 changes: 4 additions & 0 deletions python/tvm/relay/op/image/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# pylint: disable=wildcard-import
"""Image network related operators."""
from __future__ import absolute_import as _abs
from .image import *
4 changes: 4 additions & 0 deletions python/tvm/relay/op/image/_make.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""Constructor APIs"""
from ...._ffi.function import _init_api

_init_api("relay.op.image._make", __name__)
42 changes: 42 additions & 0 deletions python/tvm/relay/op/image/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Image operations."""
from __future__ import absolute_import as _abs
from . import _make

def resize(data,
size,
layout="NCHW",
method="BILINEAR",
align_corners=False):
"""Image resize operator.
This operator takes data as input and does 2D scaling to the given scale factor.
In the default case, where the data_layout is `NCHW`
with data of shape (n, c, h, w)
out will have a shape (n, c, size[0], size[1])
method indicates the algorithm to be used while calculating ghe out value
and method can be one of ("BILINEAR", "NEAREST_NEIGHBOR")
Parameters
----------
data : relay.Expr
The input data to the operator.
size: Tuple of Expr
The out size to which the image will be resized.
layout : str, optional
Layout of the input.
method : str, optional
Scale method to used [NEAREST_NEIGHBOR, BILINEAR].
align_corners : int, optional
Should be true to preserve the values at the corner pixels
Returns
-------
result: relay.Expr
The resized result.
"""
return _make.resize(data, size, layout, method, align_corners)
Loading

0 comments on commit 4fd3604

Please sign in to comment.