You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2020/01/09 08:00:12 UTC

[GitHub] [incubator-tvm] masahi commented on a change in pull request #4497: [WIP] [Relay] Add a PyTorch to Relay Parser

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

 ##########
 File path: python/tvm/relay/frontend/pytorch.py
 ##########
 @@ -0,0 +1,1104 @@
+# 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):
+        data0 = convert_input(inputs[0])
+        data1 = convert_input(inputs[1])
+
+        if not isinstance(data0, (_expr.Call, _expr.TupleGetItem, _expr.Var)):
+            temp = data0
+            data0 = data1
+            data1 = temp
+
+        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.Call, _expr.TupleGetItem, _expr.Var)):
+            data = [data]
+
+        return _op.tensor.concatenate(data, int(axis))
+    return _impl
+
+def _slice():
+    def _impl(inputs, input_types):
+        data = inputs[0]
+        strides = []
+
+        inferred_shape = _infer_shape(data)
+        end = []
+        for infer in inferred_shape:
+            end.append(int(infer))
+        if isinstance(data, _expr.Var):
+            end = _infer_shape(data)
+            end = list(end)
+
+        begin = [0]*len(end)
+        dim = int(inputs[1])
+        begin[dim] = int(inputs[2])
+
+        if inputs[3].isdigit():
+            end[dim] = min(end[dim], int(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]
+        inferred_shape = _infer_shape(data)
+        end = []
+
+        for infer in inferred_shape:
+            end.append(int(infer))
+
+        begin = [0]*len(end)
+        dim = int(inputs[1])
+        index = int(inputs[2])
+
+        end[dim] = index+1
+        begin[dim] = index
+
+        strides = [1]*len(end)
+
+        sym = _op.transform.strided_slice(data, begin, end, strides)
+        axis = [dim]
+
+        return _op.transform.squeeze(sym, axis)
+    return _impl
+
+def _ones():
+    def _impl(inputs, input_types):
+        if isinstance(inputs[0], _expr.Var):
+            shape = _infer_shape(inputs[0])
+        elif isinstance(inputs[0], (_expr.Call, _expr.TupleGetItem)):
+            shape = _infer_shape(inputs[0])
+        else:
+            shape = inputs[0].shape
+
+        fill_value = _get_fill_value(input_types)
+
+        return get_relay_op('full')(fill_value, shape)
+    return _impl
+
+def _zeros():
+    def _impl(inputs, input_types):
+        if isinstance(inputs[0], _expr.Var):
+            shape = _infer_shape(inputs[0])
+        elif isinstance(inputs[0], (_expr.Call, _expr.TupleGetItem)):
+            shape = _infer_shape(inputs[0])
+        else:
+            shape = inputs[0].shape
+
+        fill_value = _get_fill_value(input_types)
+
+        return _op.full(fill_value, shape)
+    return _impl
+
+def _get_fill_value(input_types):
+    if input_types[0] == 'int':
+        fill_value = _expr.const(1)
+    elif input_types[0] == 'float':
+        fill_value = _expr.const(1.0)
+    else:
+        fill_value = _expr.const(1)
+
+    return fill_value
+
+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
+
+        use_bias = False
+        if isinstance(inputs[2], _expr.Var):
+            use_bias = True
+
+            data = inputs[0]
+            weight = inputs[1]
+            bias = inputs[2]
+
+            if isinstance(weight, (_expr.Call, _expr.Var, _expr.TupleGetItem)):
+                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]
+
+            strides = inputs[3]
+            padding = inputs[4]
+            dilation = inputs[5]
+
+            kernel_size = weight_shape[2:]
+
+        else:
+            data = inputs[0]
+            weight = inputs[1]
+            bias = inputs[2]
+
+            if isinstance(weight, (_expr.Call, _expr.Var, _expr.TupleGetItem)):
+                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]
+
+            strides = inputs[3]
+            padding = inputs[4]
+            dilation = inputs[5]
+
+            kernel_size = weight_shape[2:]
+
+        if isinstance(strides, _expr.Var):
+            strides = _infer_shape(strides)
+
+        if isinstance(padding, _expr.Var):
+            padding = _infer_shape(padding)
+
+        if isinstance(dilation, _expr.Var):
+            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.Var) and isinstance(inputs[2], _expr.Var):
+            scale = center = True
+            weight = inputs[1]
+            beta = inputs[2]
+        else:
+            scale = center = False
+
+        if scale:
+            gamma = weight
+        else:
+            if data_type == 'double':
+                gamma = _expr.const(np.ones([int(channels[1])]).astype('float64'))
+            elif data_type == 'float':
+                gamma = _expr.const(np.ones([int(channels[1])]).astype('float32'))
+            elif data_type == 'half':
+                gamma = _expr.const(np.ones([int(channels[1])]).astype('float16'))
+            elif data_type == 'long':
+                gamma = _expr.const(np.ones([int(channels[1])]).astype('int64'))
+            elif data_type == 'int':
+                gamma = _expr.const(np.ones([int(channels[1])]).astype('int32'))
+            elif data_type == 'short':
+                gamma = _expr.const(np.ones([int(channels[1])]).astype('int16'))
+            elif data_type == 'char':
+                gamma = _expr.const(np.ones([int(channels[1])]).astype('int8'))
+            elif data_type == 'byte':
+                gamma = _expr.const(np.ones([int(channels[1])]).astype('uint8'))
+
+        if center:
+            beta = beta
+        else:
+            if data_type == 'double':
+                beta = _expr.const(np.zeros([int(channels[1])]).astype('float64'))
+            elif data_type == 'float':
+                beta = _expr.const(np.zeros([int(channels[1])]).astype('float32'))
+            elif data_type == 'half':
+                beta = _expr.const(np.zeros([int(channels[1])]).astype('float16'))
+            elif data_type == 'long':
+                beta = _expr.const(np.zeros([int(channels[1])]).astype('int64'))
+            elif data_type == 'int':
+                beta = _expr.const(np.zeros([int(channels[1])]).astype('int32'))
+            elif data_type == 'short':
+                beta = _expr.const(np.zeros([int(channels[1])]).astype('int16'))
+            elif data_type == 'char':
+                beta = _expr.const(np.zeros([int(channels[1])]).astype('int8'))
+            elif data_type == 'byte':
+                beta = _expr.const(np.zeros([int(channels[1])]).astype('uint8'))
+
+        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.Var):
+            ndims = len(_infer_shape(data))
+        elif isinstance(data, (_expr.Call, _expr.TupleGetItem)):
+            ndims = _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.Var):
+                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.Var):
+            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.Var, _expr.Call, _expr.TupleGetItem)):
+            if data_type == 'double':
+                alpha = _expr.const(np.float64(alpha), dtype='float64')
+            elif data_type == 'float':
+                alpha = _expr.const(np.float32(alpha), dtype='float32')
+            elif data_type == 'half':
+                alpha = _expr.const(np.float16(alpha), dtype='float16')
+            elif data_type == 'long':
+                alpha = _expr.const(np.int64(alpha), dtype='int64')
+            elif data_type == 'int':
+                alpha = _expr.const(np.int32(alpha), dtype='int32')
+            elif data_type == 'short':
+                alpha = _expr.const(np.int16(alpha), dtype='int16')
+            elif data_type == 'char':
+                alpha = _expr.const(np.int8(alpha), dtype='int8')
+            elif data_type == 'byte':
+                alpha = _expr.const(np.uint8(alpha), dtype='uint8')
+            data *= alpha
+
+        if not isinstance(beta, (_expr.Var, _expr.Call, _expr.TupleGetItem)):
+            if data_type == 'double':
+                beta = _expr.const(np.foat64(beta), dtype='float64')
+            elif data_type == 'float':
+                beta = _expr.const(np.float32(beta), dtype='float32')
+            elif data_type == 'half':
+                beta = _expr.const(np.float16(beta), dtype='float16')
+            elif data_type == 'long':
+                beta = _expr.const(np.int64(beta), dtype='int64')
+            elif data_type == 'int':
+                beta = _expr.const(np.int32(beta), dtype='int32')
+            elif data_type == 'short':
+                beta = _expr.const(np.int16(beta), dtype='int16')
+            elif data_type == 'char':
+                beta = _expr.const(np.int8(beta), dtype='int8')
+            elif data_type == 'byte':
+                beta = _expr.const(np.uint8(beta), dtype='uint8')
+            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])
+        if isinstance(inputs[0], _expr.Var):
+            shape = _infer_shape(inputs[0])
+        else:
+            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.Var):
+            inferred_shape = _infer_shape(data)
+        elif isinstance(data, (_expr.Call, _expr.TupleGetItem)):
+            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.Var):
+            shape = _infer_shape(data_in)
+        elif isinstance(data_in, (_expr.Call, _expr.TupleGetItem)):
+            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.Call):
+            return inputs[0]
+        return int(inputs[0])
+    return _impl
+
+def _listunpack():
+    def _impl(inputs, input_types):
+        return inputs[0]
+    return _impl
+
+def _to():
 
 Review comment:
   rename it to identity since aten::detach (and possibly others) is also identity.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services