alexwong commented on a change in pull request #4497: [Relay] Add a PyTorch to 
Relay Parser
URL: https://github.com/apache/incubator-tvm/pull/4497#discussion_r375549738
 
 

 ##########
 File path: python/tvm/relay/frontend/pytorch.py
 ##########
 @@ -0,0 +1,1023 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# pylint: disable=import-self, too-many-lines, len-as-condition, 
no-else-return, unused-variable, too-many-nested-blocks
+# pylint: disable=consider-iterating-dictionary, invalid-name, 
unused-argument, unused-variable, broad-except
+"""PT: PyTorch frontend."""
+import numpy as np
+
+import tvm
+
+from .. import analysis as _analysis
+from .. import expr as _expr
+from .. import module as _module
+from .. import op as _op
+from .common import get_relay_op
+from .common import infer_shape as _infer_shape
+
+__all__ = ["from_pytorch"]
+
+# operator implementation
+def _elemwise(name):
+    def _impl(inputs, input_types):
+        # TODO: Figure out a better way to get typing to work for tensor + 
scalar
+        type0 = input_types[0]
+        if isinstance(inputs[1], _expr.Expr):
+            type0 = input_types[1]
+
+        type1 = input_types[1]
+        if isinstance(inputs[0], _expr.Expr):
+            type1 = input_types[0]
+
+        data0 = _convert_elemwise_input(inputs[0], type0)
+        data1 = _convert_elemwise_input(inputs[1], type1)
+
+        return get_relay_op(name)(data0, data1)
+    return _impl
+
+def _unsqueeze():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        axis = inputs[1]
+
+        return _op.transform.expand_dims(data, int(axis), 1)
+    return _impl
+
+def _concatenate():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        axis = inputs[1]
+
+        if isinstance(data, _expr.Expr):
+            data = [data]
+
+        return _op.tensor.concatenate(data, int(axis))
+    return _impl
+
+def _slice():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        strides = []
+
+        if isinstance(data, _expr.Expr):
+            inferred_shape = _infer_shape(data)
+            end = []
+            for infer in inferred_shape:
+                end.append(int(infer))
+            if isinstance(data, _expr.Var):
+                end = inferred_shape
+                end = list(end)
+        else:
+            end = data.shape
+
+        begin = [0]*len(end)
+        dim = int(inputs[1])
+        begin[dim] = int(inputs[2])
+
+        if isinstance(inputs[3], str) and inputs[3].isdigit():
+            end[dim] = min(end[dim], int(inputs[3]))
+        else:
+            end[dim] = inputs[3]
+
+        strides.append(int(inputs[4]))
+        return _op.transform.strided_slice(data, begin, end, strides)
+    return _impl
+
+def _select():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        dim = int(inputs[1])
+        index = int(inputs[2])
+
+        return _op.transform.take(data, _expr.const(index, dtype="int32"), 
axis=dim)
+    return _impl
+
+def _ones():
+    def _impl(inputs, input_types):
+        if isinstance(inputs[0], _expr.Expr):
+            shape = _infer_shape(inputs[0])
+        else:
+            shape = inputs[0].shape
+
+        return _op.full(_expr.const(1), shape, 
dtype=_convert_data_type(input_types[0]))
+    return _impl
+
+def _zeros():
+    def _impl(inputs, input_types):
+        if isinstance(inputs[0], _expr.Expr):
+            shape = _infer_shape(inputs[0])
+        else:
+            shape = inputs[0].shape
+
+        return _op.full(_expr.const(0), shape, 
dtype=_convert_data_type(input_types[0]))
+    return _impl
+
+def _relu():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        return _op.nn.relu(data)
+    return _impl
+
+def _adaptive_avg_2d():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        output_size = _infer_shape(inputs[1])
+
+        return _op.contrib.contrib.adaptive_avg_pool2d(
+            data,
+            output_size=output_size)
+    return _impl
+
+def _adaptive_max_2d():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        output_size = _infer_shape(inputs[1])
+
+        return _op.contrib.contrib.adaptive_max_pool2d(
+            data,
+            output_size=output_size)
+    return _impl
+
+def _maxpool_2d():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+
+        pool_size = _infer_shape(inputs[1])
+        strides = _infer_shape(inputs[2])
+        padding = _infer_shape(inputs[3])
+
+        ceil_mode = int(inputs[5])
+
+        return _op.nn.max_pool2d(data, pool_size, strides, padding, "NCHW", 
ceil_mode)
+    return _impl
+
+def _hardtanh():
+    def _impl(inputs, input_types):
+        a = inputs[0]
+        tanh_min = float(inputs[1])
+        tanh_max = float(inputs[2])
+        return _op.tensor.clip(a, tanh_min, tanh_max)
+    return _impl
+
+def _convolution():
+    def _impl(inputs, input_types):
+        # Use transpose or normal
+        use_transpose = False
+        if inputs[6] == "1":
+            use_transpose = True
+
+        data = inputs[0]
+        weight = inputs[1]
+        bias = inputs[2]
+        strides = inputs[3]
+        padding = inputs[4]
+        dilation = inputs[5]
+
+        if isinstance(weight, _expr.Expr):
+            inferred_shape = _infer_shape(weight)
+            weight_shape = []
+            for infer in inferred_shape:
+                weight_shape.append(infer)
+        else:
+            weight_shape = weight.shape
+        channels = weight_shape[0]
+
+        kernel_size = weight_shape[2:]
+
+        use_bias = False
+        if isinstance(bias, _expr.Expr):
+            use_bias = True
+
+        if isinstance(strides, _expr.Expr):
+            strides = _infer_shape(strides)
+
+        if isinstance(padding, _expr.Expr):
+            padding = _infer_shape(padding)
+
+        if isinstance(dilation, _expr.Expr):
+            dilation = _infer_shape(dilation)
+
+        groups = int(inputs[8])
+
+        if use_transpose:
+            conv_out = _op.nn.conv2d_transpose(data,
+                                               weight,
+                                               strides=strides,
+                                               padding=padding,
+                                               dilation=dilation,
+                                               groups=groups,
+                                               channels=channels,
+                                               kernel_size=kernel_size,
+                                               data_layout="NCHW",
+                                               kernel_layout="OIHW",
+                                               out_layout="",
+                                               out_dtype="")
+        else:
+            conv_out = _op.nn.conv2d(data,
+                                     weight,
+                                     strides=strides,
+                                     padding=padding,
+                                     dilation=dilation,
+                                     groups=groups,
+                                     channels=channels,
+                                     kernel_size=kernel_size,
+                                     data_layout="NCHW",
+                                     kernel_layout="OIHW",
+                                     out_layout="",
+                                     out_dtype="")
+
+        if use_bias:
+            return _op.nn.bias_add(conv_out, bias)
+        else:
+            return conv_out
+    return _impl
+
+def _softmax():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        axis = inputs[1]
+        if isinstance(axis, str):
+            axis = int(axis)
+
+        return _op.nn.softmax(data, axis=axis)
+    return _impl
+
+def _threshold():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        return _op.nn.relu(data)
+    return _impl
+
+def _contiguous():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        return _op.tensor.copy(data)
+    return _impl
+
+def _batch_norm():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        data_type = input_types[0]
+
+        channels = _infer_shape(data)
+
+        if isinstance(inputs[1], _expr.Expr) and isinstance(inputs[2], 
_expr.Expr):
+            scale = center = True
+            weight = inputs[1]
+            beta = inputs[2]
+        else:
+            scale = center = False
+
+        if scale:
+            gamma = weight
+        else:
+            gamma = _create_typed_const(np.ones([int(channels[1])]), data_type)
+
+        if center:
+            beta = beta
+        else:
+            beta = _create_typed_const(np.zeros([int(channels[1])]), data_type)
+
+        moving_mean = inputs[3]
+        moving_var = inputs[4]
+        epsilon = float(inputs[7])
+
+        center = center
+        scale = scale
+
+        return _op.nn.batch_norm(data,
+                                 gamma,
+                                 beta,
+                                 moving_mean,
+                                 moving_var,
+                                 axis=1,
+                                 epsilon=epsilon,
+                                 center=center,
+                                 scale=scale)[0]
+    return _impl
+
+def _transpose():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+
+        if isinstance(data, _expr.Expr):
+            ndims = len(_infer_shape(data))
+        else:
+            ndims = data.shape
+
+        if isinstance(data, tvm.ndarray.NDArray):
+            ndims = len(data.shape)
+        axes = list(range(ndims))
+
+        num_inputs = len(inputs)
+
+        if num_inputs == 1:
+            if ndims >= 2:
+                axes[-1] = ndims - 2
+                axes[-2] = ndims - 1
+            if not isinstance(data, _expr.Expr):
+                data = _expr.const(data)
+
+        elif num_inputs == 3:
+            parse = lambda i: ndims * (i < 0) + i
+            src, dst = [parse(int(inputs[i])) for i in [1, 2]]
+            axes[src] = dst
+            axes[dst] = src
+        else:
+            axes = inputs[1]
+        return _op.transform.transpose(data, axes)
+    return _impl
+
+def _flatten():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        return _op.nn.batch_flatten(data)
+    return _impl
+
+def _dense():
+    def _impl(inputs, input_types):
+        use_bias = False
+
+        if isinstance(inputs[0], _expr.Expr):
+            use_bias = True
+
+        data = inputs[1]
+        data_type = input_types[1]
+        weight = inputs[2]
+
+        beta = inputs[3]
+        alpha = inputs[4]
+
+        if not isinstance(alpha, _expr.Expr):
+            alpha = _create_typed_const(alpha, data_type)
+            data *= alpha
+
+        if not isinstance(beta, _expr.Expr):
+            beta = _create_typed_const(beta, data_type)
+            weight *= beta
+
+        weight_out = _op.transform.transpose(weight, axes=[1, 0])
+
+        units = _infer_shape(weight_out)[0]
+        dense_out = _op.nn.dense(data, weight_out, units=units)
+
+        if use_bias:
+            bias = inputs[0]
+            return _op.nn.bias_add(dense_out, bias)
+        else:
+            return dense_out
+    return _impl
+
+def _size():
+    def _impl(inputs, input_types):
+        axis = int(inputs[1])
+        shape = _infer_shape(inputs[0])
+        return shape[axis]
+    return _impl
+
+def _numtotensor():
+    def _impl(inputs, input_types):
+        val = inputs[0]
+        dtype = type(val)
+
+        if isinstance(val, tvm.expr.IntImm):
+            val = val.__int__()
+            dtype = int
+
+        arr = val * np.ones([]).astype(dtype)
+        return arr
+    return _impl
+
+def _view():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+
+        if len(inputs) == 3:
+            new_shape = [inputs[1], _infer_shape(inputs[2])[0]]
+        else:
+            if isinstance(inputs[1], list):
+                new_shape = inputs[1]
+            else:
+                new_shape = _infer_shape(inputs[1])
+
+        return _op.transform.reshape(data, new_shape)
+    return _impl
+
+def _clone():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        return _op.tensor.copy(data)
+    return _impl
+
+def _log_softmax():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        axis = int(inputs[1])
+        return _op.nn.log_softmax(data, axis)
+    return _impl
+
+def _sigmoid():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        return _op.tensor.sigmoid(data)
+    return _impl
+
+def _avg_pool2d():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+
+        pool_size = _infer_shape(inputs[1])
+        strides = _infer_shape(inputs[2])
+        padding = _infer_shape(inputs[3])
+
+        ceil_mode = int(inputs[4])
+        count_include_pad = int(inputs[5])
+
+        return _op.nn.avg_pool2d(data,
+                                 pool_size=pool_size,
+                                 strides=strides,
+                                 padding=padding,
+                                 ceil_mode=ceil_mode,
+                                 count_include_pad=count_include_pad)
+    return _impl
+
+def _dropout():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        rate = float(inputs[1])
+
+        return _op.nn.dropout(data, rate)
+    return _impl
+
+def _reduce(name):
+    def _impl(inputs, attrs, params):
+        data = inputs[0]
+        return get_relay_op(name)(data)
+    return _impl
+
+def _mean():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        axis = _infer_shape(inputs[1])
+
+        keepdims = int(inputs[2])
+        exclude = int(inputs[3])
+
+        return _op.mean(data, axis, keepdims, exclude)
+    return _impl
+
+def _chunk():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+
+        num_chunks = int(inputs[1])
+        axis = int(inputs[2])
+
+        if isinstance(data, _expr.Expr):
+            inferred_shape = _infer_shape(data)
+
+        shape = []
+        for infer in inferred_shape:
+            shape.append(infer)
+
+        dim = int(shape[axis])
+
+        if dim % num_chunks:
+            unif_size = int(dim / (num_chunks - 1))
+        else:
+            unif_size = int(dim / num_chunks)
+
+        chunks = []
+        for i in range(0, dim, unif_size):
+            begin = [0] * len(shape)
+            end = shape[:]
+            begin[axis] = i
+            end[axis] = i + unif_size
+            stride = [1] * len(shape)
+
+            chunk_out = _op.transform.strided_slice(data, begin, end, stride)
+            chunks.append(chunk_out)
+
+
+        if dim % num_chunks:
+            begin = [0] * len(shape)
+            end = shape[:]
+            begin[axis] = unif_size * (num_chunks - 1)
+            end[axis] = dim
+            stride = [1] * len(shape)
+
+            chunk_out = _op.transform.strided_slice(data, begin, end, stride)
+            chunks.append(chunk_out)
+
+        return chunks
+    return _impl
+
+def _matmul():
+    def _impl(inputs, input_types):
+        data0 = inputs[0]
+        data1 = inputs[1]
+        data1_t = _op.transpose(data1, axes=(1, 0))
+
+        return _op.nn.dense(data0, data1_t)
+    return _impl
+
+def _expand():
+    def _impl(inputs, input_types):
+        data_in = inputs[0]
+        if isinstance(data_in, _expr.Expr):
+            shape = _infer_shape(data_in)
+
+        ndims = len(shape)
+        sizes = _infer_shape(inputs[1])
+        out = inputs[0]
+
+        for i in range(ndims):
+            if sizes[i] in {-1, shape[i]}:
+                continue
+            data = list()
+            for temp in range(sizes[i]):
+                data.append(out)
+            call = _op.tensor.concatenate(data, i)
+
+        return call
+    return _impl
+
+def _int():
+    def _impl(inputs, input_types):
+        if isinstance(inputs[0], _expr.Expr):
+            return inputs[0]
+        return int(inputs[0])
+    return _impl
+
+def _identity():
+    def _impl(inputs, input_types):
+        return inputs[0]
+    return _impl
+
+def _none():
+    def _impl(inputs, input_types):
+        return None
+    return _impl
+
+def _pad():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        padding = inputs[1]
+        pad_width = list(zip(padding, padding))
+        pad_value = inputs[2]
+        return _op.nn.pad(data, pad_width, pad_value)
+    return _impl
+
+def _sqrt():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        return _op.tensor.sqrt(data)
+    return _impl
+
+# Helper functions for operator implementation
+
+def _convert_data_type(input_type):
+    if input_type in ["double", "torch.float64"]:
+        return "float64"
+    elif input_type in ["float", "torch.float32"]:
+        return "float32"
+    elif input_type in ["half", "torch.float16"]:
+        return "float16"
+    elif input_type in ["long", "torch.int64"]:
+        return "int64"
+    elif input_type in ["int", "torch.int32"]:
+        return "int32"
+    elif input_type in ["short", "torch.int16"]:
+        return "int16"
+    elif input_type in ["char", "torch.int8"]:
+        return "int8"
+    elif input_type in ["byte", "torch.uint8"]:
+        return "uint8"
+    else:
+        assert "data_type {} is not handled yet" % (data_type)
+        return "float32"
+
+def _create_typed_const(data, data_type):
+    dtype = _convert_data_type(data_type)
+
+    if dtype == "float64":
+        typed_data = _expr.const(np.float64(data), dtype=dtype)
+    elif dtype == "float32":
+        typed_data = _expr.const(np.float32(data), dtype=dtype)
+    elif dtype == "float16":
+        typed_data = _expr.const(np.float16(data), dtype=dtype)
+    elif dtype == "int64":
+        typed_data = _expr.const(np.int64(data), dtype=dtype)
+    elif dtype == "int32":
+        typed_data = _expr.const(np.int32(data), dtype=dtype)
+    elif dtype == "int16":
+        typed_data = _expr.const(np.int16(data), dtype=dtype)
+    elif dtype == "int8":
+        typed_data = _expr.const(np.int8(data), dtype=dtype)
+    elif dtype == "uint8":
+        typed_data = _expr.const(np.uint8(data), dtype=dtype)
+    else:
+        assert "data_type {} is not handled yet" % (data_type)
+        return _expr.const(np.float32(data), dtype="float32")
+
+    return typed_data
+
+# TODO: Fix typing
+def _convert_elemwise_input(data, input_type):
+    import torch
+    if isinstance(data, torch.Tensor):
+        return _expr.const(data.item(), dtype=_convert_data_type(input_type))
+    elif not isinstance(data, _expr.Expr):
+        return _expr.const(int(data), dtype=_convert_data_type(input_type))
+    else:
+        return data
+
+# Operator mappings
+
+_convert_map = {
+    "aten::device"                          : _none(),
+    "aten::add"                             : _elemwise("add"),
+    "aten::add_"                            : _elemwise("add"),
+    "aten::sub"                             : _elemwise("subtract"),
+    "aten::sub_"                            : _elemwise("subtract"),
+    "aten::max"                             : _elemwise("maximum"),
+    "aten::min"                             : _elemwise("minimum"),
+    "aten::mul"                             : _elemwise("multiply"),
+    "aten::mul_"                            : _elemwise("multiply"),
+    "aten::pow"                             : _elemwise("power"),
+    "aten::div"                             : _elemwise("divide"),
+    "aten::div_"                            : _elemwise("divide"),
+    "aten::ones"                            : _ones(),
+    "aten::zeros"                           : _zeros(),
+    "aten::to"                              : _identity(),
+    "aten::unsqueeze"                       : _unsqueeze(),
+    "aten::cat"                             : _concatenate(),
+    "aten::slice"                           : _slice(),
+    "aten::select"                          : _select(),
+    "aten::relu"                            : _relu(),
+    "aten::relu_"                           : _relu(),
+    "aten::adaptive_avg_pool2d"             : _adaptive_avg_2d(),
+    "aten::adaptive_max_pool2d"             : _adaptive_max_2d(),
+    "aten::max_pool2d"                      : _maxpool_2d(),
+    "aten::max_pool2d_with_indices"         : _maxpool_2d(),
+    "aten::hardtanh"                        : _hardtanh(),
+    "aten::hardtanh_"                       : _hardtanh(),
+    "aten::_convolution"                    : _convolution(),
+    "aten::softmax"                         : _softmax(),
+    "aten::threshold"                       : _threshold(),
+    "aten::threshold_"                      : _threshold(),
+    "aten::contiguous"                      : _contiguous(),
+    "aten::batch_norm"                      : _batch_norm(),
+    "aten::transpose"                       : _transpose(),
+    "aten::transpose_"                      : _transpose(),
+    "aten::t"                               : _transpose(),
+    "aten::flatten"                         : _flatten(),
+    "aten::addmm"                           : _dense(),
+    "aten::size"                            : _size(),
+    "aten::view"                            : _view(),
+    "aten::clone"                           : _clone(),
+    "aten::log_softmax"                     : _log_softmax(),
+    "aten::sigmoid"                         : _sigmoid(),
+    "aten::avg_pool2d"                      : _avg_pool2d(),
+    "aten::dropout"                         : _dropout(),
+    "aten::dropout_"                        : _dropout(),
+    "aten::mean"                            : _mean(),
+    "aten::chunk"                           : _chunk(),
+    "aten::matmul"                          : _matmul(),
+    "aten::expand"                          : _expand(),
+    "aten::Int"                             : _int(),
+    "prim::NumToTensor"                     : _numtotensor(),
+    "prim::ListUnpack"                      : _identity(),
+    "aten::constant_pad_nd"                 : _pad(),
+    "aten::permute"                         : _transpose(),
+    "aten::sum"                             : _reduce("sum"),
+    "aten::prod"                            : _reduce("prod"),
+    "aten::sqrt"                            : _sqrt()
+}
+
+# Internal graph for parsing
+
+class Graph(object):
+    """ A helper class for parsing PyTorch model to Relay graph."""
+
+    def __init__(self, script_module, input_shapes):
+
+        self._script_module = script_module
+        self._graph = script_module.graph.copy()
+
+        # TODO: Temporary fix to remove prim::CallMethod node introduced in PT 
1.4
+        import torch
+        if torch.__version__ != "1.2.0":
 
 Review comment:
   Reminder to change to <1.4 once 
https://github.com/apache/incubator-tvm/pull/4756 is merged or just remove.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to