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_r371813536
 
 

 ##########
 File path: python/tvm/relay/frontend/pytorch.py
 ##########
 @@ -0,0 +1,1135 @@
+# 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 _convert_data_type(input_type):
+    if input_type == 'double' or input_type == 'torch.float64':
+        return 'float64'
+    elif input_type == 'float' or input_type == 'torch.float32':
+        return 'float32'
+    elif input_type == 'half' or input_type == 'torch.float16':
+        return 'float16'
+    elif input_type == 'long' or input_type == 'torch.int64':
+        return 'int64'
+    elif input_type == 'int' or input_type == 'torch.int32':
+        return 'int32'
+    elif input_type == 'short' or input_type == 'torch.int16':
+        return 'int16'
+    elif input_type == 'char' or input_type == 'torch.int8':
+        return 'int8'
+    elif input_type == 'byte' or input_type == 'torch.uint8':
+        return 'uint8'
+    else:
+        return input_type
+
+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, 
dtype=_convert_data_type(input_types[0]))
+    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, dtype=input_types[0])
+    return _impl
+
+def _get_fill_value(input_types):
+    if input_types[0] == 'int':
+        fill_value = _expr.const(1)
+    elif input_types[0] == 'float':
 
 Review comment:
   this branch is redundant

----------------------------------------------------------------
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