[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 + +d
[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_r369386924 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369387153 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369398329 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369398329 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369398329 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369398329 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369398329 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369413598 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369414847 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369414847 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369428504 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369430546 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369430546 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369448527 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369539697 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369540508 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369541045 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369540915 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369541404 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369542369 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369543010 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369543379 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369545034 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369546183 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369552116 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369552116 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369560699 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369561507 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369561930 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369552116 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369566503 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369568036 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369539697 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369571350 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369574477 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369579603 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369582532 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369582532 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369582532 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369593441 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369560699 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369598771 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369600861 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369598771 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369603425 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369605072 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369606253 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369606253 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369607713 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369609007 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369609581 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369611489 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369613238 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369611489 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369626019 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r370042351 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r370061548 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r370061548 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r370644342 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r370672126 ## 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
[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_r370672126 ## 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
[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_r370904294 ## 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
[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_r370905930 ## 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
[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_r370906046 ## 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
[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_r370906046 ## 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
[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_r370911013 ## File path: tests/python/frontend/pytorch/test_forward.py ## @@ -0,0 +1,732 @@ +# 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, invalid-name, unused-argument +"""Unit tests for various models and operators""" +from time import time +import os +import sys +from tempfile import TemporaryDirectory +from scipy.stats import t as tdistr +import numpy as np +import torch +import tvm +import torchvision +import single_op + + +from tvm import relay +from tvm.contrib import graph_runtime +#from tvm.relay.testing.config import ctx_list + +sys.setrecursionlimit(1) + +TARGET = 'llvm' +CTX = tvm.cpu() +EXT_ACCEL = None + +model_names = [] +baseline_latencies_map = {} +compiled_latencies_map = {} +speedups_map = {} + +test_repeats = 1 + +def _vectorize(ten): +return ten.reshape(-1) + +def atol(tru, est): +def _atol_elt(tru, est): +return abs(tru - est) +tru = _vectorize(tru) +est = _vectorize(est) +return max([_atol_elt(x, y) for x, y in zip(tru, est)]) + +def rtol(tru, est): +def _rtol_elt(tru, est): +return abs(tru - est) / min(abs(tru), abs(est)) +tru = _vectorize(tru) +est = _vectorize(est) +return max([_rtol_elt(x, y) for x, y in zip(tru, est)]) + +def assert_shapes_match(tru, est): +if tru.shape != est.shape: +msg = "Output shapes {} and {} don't match" +raise AssertionError(msg.format(tru.shape, est.shape)) + +def load_single_op(model_name, input_type=None): +"""Given a model name, returns a single-operator model in eval +mode as well as an example input.""" +input_shape = [1, 3, 224, 224] +if input_type is None or input_type == 'float32': +model = getattr(single_op, model_name)().float().eval() +input_data = torch.rand(input_shape).float() +elif input_type == 'float64': +model = getattr(single_op, model_name)().double().eval() +#input_data = torch.rand(input_shape).double() +#input_data = torch.from_numpy(np.random.random_sample((1, 3, 224, 224))).double() +temp = np.random.random_sample((1, 3, 224, 224)) +input_data = torch.from_numpy(temp) +elif input_type == 'float16': +model = getattr(single_op, model_name)().float().eval() +input_data = torch.rand(input_shape) +elif input_type == 'int32': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).int() +elif input_type == 'int16': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).short() +elif input_type == 'int8': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).char() +elif input_type == 'uint8': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).byte() +return model, input_data + +def load_torchvision(model_name, input_type=None): +"""Given a model name, returns a Torchvision model in eval mode as well +as an example input.""" + +if model_name.startswith('inception'): +height = width = 299 +mean = [0.5, 0.5, 0.5] +std = [0.5, 0.5, 0.5] +else: +height = width = 224 +mean = [0.485, 0.456, 0.406] +std = [0.229, 0.224, 0.225] +input_shape = [1, 3, height, width] + +model = getattr(torchvision.models, model_name)(pretrained=True) +#model = model.float().eval() + +if input_type is None or input_type == 'float32': +model = model.float().eval() +input_data = torch.randn(input_shape).float() +elif input_type == 'float64': +model = model.double().eval() +input_data = torch.randn(input_shape).double() + +for channel in range(3): +input_data[:, channel] -= mean[channel] +input_data[:, channel] /= std[channel] + +return model, input_data + +def load_pretrainedmodels(model_name): +"""Given a model name, returns a pretrainedmodels.pyto
[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_r370911023 ## File path: tests/python/frontend/pytorch/test_forward.py ## @@ -0,0 +1,732 @@ +# 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, invalid-name, unused-argument +"""Unit tests for various models and operators""" +from time import time +import os +import sys +from tempfile import TemporaryDirectory +from scipy.stats import t as tdistr +import numpy as np +import torch +import tvm +import torchvision +import single_op + + +from tvm import relay +from tvm.contrib import graph_runtime +#from tvm.relay.testing.config import ctx_list + +sys.setrecursionlimit(1) + +TARGET = 'llvm' +CTX = tvm.cpu() +EXT_ACCEL = None + +model_names = [] +baseline_latencies_map = {} +compiled_latencies_map = {} +speedups_map = {} + +test_repeats = 1 + +def _vectorize(ten): +return ten.reshape(-1) + +def atol(tru, est): +def _atol_elt(tru, est): +return abs(tru - est) +tru = _vectorize(tru) +est = _vectorize(est) +return max([_atol_elt(x, y) for x, y in zip(tru, est)]) + +def rtol(tru, est): +def _rtol_elt(tru, est): +return abs(tru - est) / min(abs(tru), abs(est)) +tru = _vectorize(tru) +est = _vectorize(est) +return max([_rtol_elt(x, y) for x, y in zip(tru, est)]) + +def assert_shapes_match(tru, est): +if tru.shape != est.shape: +msg = "Output shapes {} and {} don't match" +raise AssertionError(msg.format(tru.shape, est.shape)) + +def load_single_op(model_name, input_type=None): +"""Given a model name, returns a single-operator model in eval +mode as well as an example input.""" +input_shape = [1, 3, 224, 224] +if input_type is None or input_type == 'float32': +model = getattr(single_op, model_name)().float().eval() +input_data = torch.rand(input_shape).float() +elif input_type == 'float64': +model = getattr(single_op, model_name)().double().eval() +#input_data = torch.rand(input_shape).double() +#input_data = torch.from_numpy(np.random.random_sample((1, 3, 224, 224))).double() +temp = np.random.random_sample((1, 3, 224, 224)) +input_data = torch.from_numpy(temp) +elif input_type == 'float16': +model = getattr(single_op, model_name)().float().eval() +input_data = torch.rand(input_shape) +elif input_type == 'int32': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).int() +elif input_type == 'int16': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).short() +elif input_type == 'int8': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).char() +elif input_type == 'uint8': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).byte() +return model, input_data + +def load_torchvision(model_name, input_type=None): +"""Given a model name, returns a Torchvision model in eval mode as well +as an example input.""" + +if model_name.startswith('inception'): +height = width = 299 +mean = [0.5, 0.5, 0.5] +std = [0.5, 0.5, 0.5] +else: +height = width = 224 +mean = [0.485, 0.456, 0.406] +std = [0.229, 0.224, 0.225] +input_shape = [1, 3, height, width] + +model = getattr(torchvision.models, model_name)(pretrained=True) +#model = model.float().eval() + +if input_type is None or input_type == 'float32': +model = model.float().eval() +input_data = torch.randn(input_shape).float() +elif input_type == 'float64': +model = model.double().eval() +input_data = torch.randn(input_shape).double() + +for channel in range(3): +input_data[:, channel] -= mean[channel] +input_data[:, channel] /= std[channel] + +return model, input_data + +def load_pretrainedmodels(model_name): +"""Given a model name, returns a pretrainedmodels.pyto
[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_r370911023 ## File path: tests/python/frontend/pytorch/test_forward.py ## @@ -0,0 +1,732 @@ +# 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, invalid-name, unused-argument +"""Unit tests for various models and operators""" +from time import time +import os +import sys +from tempfile import TemporaryDirectory +from scipy.stats import t as tdistr +import numpy as np +import torch +import tvm +import torchvision +import single_op + + +from tvm import relay +from tvm.contrib import graph_runtime +#from tvm.relay.testing.config import ctx_list + +sys.setrecursionlimit(1) + +TARGET = 'llvm' +CTX = tvm.cpu() +EXT_ACCEL = None + +model_names = [] +baseline_latencies_map = {} +compiled_latencies_map = {} +speedups_map = {} + +test_repeats = 1 + +def _vectorize(ten): +return ten.reshape(-1) + +def atol(tru, est): +def _atol_elt(tru, est): +return abs(tru - est) +tru = _vectorize(tru) +est = _vectorize(est) +return max([_atol_elt(x, y) for x, y in zip(tru, est)]) + +def rtol(tru, est): +def _rtol_elt(tru, est): +return abs(tru - est) / min(abs(tru), abs(est)) +tru = _vectorize(tru) +est = _vectorize(est) +return max([_rtol_elt(x, y) for x, y in zip(tru, est)]) + +def assert_shapes_match(tru, est): +if tru.shape != est.shape: +msg = "Output shapes {} and {} don't match" +raise AssertionError(msg.format(tru.shape, est.shape)) + +def load_single_op(model_name, input_type=None): +"""Given a model name, returns a single-operator model in eval +mode as well as an example input.""" +input_shape = [1, 3, 224, 224] +if input_type is None or input_type == 'float32': +model = getattr(single_op, model_name)().float().eval() +input_data = torch.rand(input_shape).float() +elif input_type == 'float64': +model = getattr(single_op, model_name)().double().eval() +#input_data = torch.rand(input_shape).double() +#input_data = torch.from_numpy(np.random.random_sample((1, 3, 224, 224))).double() +temp = np.random.random_sample((1, 3, 224, 224)) +input_data = torch.from_numpy(temp) +elif input_type == 'float16': +model = getattr(single_op, model_name)().float().eval() +input_data = torch.rand(input_shape) +elif input_type == 'int32': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).int() +elif input_type == 'int16': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).short() +elif input_type == 'int8': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).char() +elif input_type == 'uint8': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).byte() +return model, input_data + +def load_torchvision(model_name, input_type=None): +"""Given a model name, returns a Torchvision model in eval mode as well +as an example input.""" + +if model_name.startswith('inception'): +height = width = 299 +mean = [0.5, 0.5, 0.5] +std = [0.5, 0.5, 0.5] +else: +height = width = 224 +mean = [0.485, 0.456, 0.406] +std = [0.229, 0.224, 0.225] +input_shape = [1, 3, height, width] + +model = getattr(torchvision.models, model_name)(pretrained=True) +#model = model.float().eval() + +if input_type is None or input_type == 'float32': +model = model.float().eval() +input_data = torch.randn(input_shape).float() +elif input_type == 'float64': +model = model.double().eval() +input_data = torch.randn(input_shape).double() + +for channel in range(3): +input_data[:, channel] -= mean[channel] +input_data[:, channel] /= std[channel] + +return model, input_data + +def load_pretrainedmodels(model_name): +"""Given a model name, returns a pretrainedmodels.pyto
[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_r371072180 ## 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
[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_r369430546 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r369428504 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1138 @@ +# 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
[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_r371799109 ## 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
[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_r371812269 ## 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
[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_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
[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_r372107591 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1120 @@ +# 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 torch +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 + +return _op.full(_expr.const(1), shape, dtype=_convert_data_type(input_types[0])) +return _impl + +def _zeros(): +def _impl(inputs, i
[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_r372752325 ## File path: python/tvm/relay/frontend/pytorch.py ## @@ -0,0 +1,1098 @@ +# 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_elemwise_input(inputs[0]) +data1 = _convert_elemwise_input(inputs[1]) + +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 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): Review comment: can we use op.transform.take(...) for this? https://github.com/apache/incubator-tvm/blob/master/python/tvm/relay/op/transform.py#L263 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
[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_r375173253 ## File path: tests/python/frontend/pytorch/test_forward.py ## @@ -0,0 +1,851 @@ +# 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, invalid-name, unused-argument +"""Unit tests for various models and operators""" +from time import time +import os +import sys +from tempfile import TemporaryDirectory +from scipy.stats import t as tdistr +import numpy as np +import torch +from torch.nn import Module +import tvm +import torchvision + +from tvm import relay +from tvm.contrib import graph_runtime +from tvm.relay.testing.config import ctx_list + +sys.setrecursionlimit(1) + +def _vectorize(ten): +return ten.reshape(-1) + +def atol(tru, est): +def _atol_elt(tru, est): +return abs(tru - est) +tru = _vectorize(tru) +est = _vectorize(est) +return max([_atol_elt(x, y) for x, y in zip(tru, est)]) + +def rtol(tru, est): +def _rtol_elt(tru, est): +return abs(tru - est) / min(abs(tru), abs(est)) +tru = _vectorize(tru) +est = _vectorize(est) +return max([_rtol_elt(x, y) for x, y in zip(tru, est)]) + +def assert_shapes_match(tru, est): +if tru.shape != est.shape: +msg = "Output shapes {} and {} don't match" +raise AssertionError(msg.format(tru.shape, est.shape)) + +def load_torchvision(model_name): +"""Given a model name, returns a Torchvision model in eval mode as well +as an example input.""" +with torch.no_grad(): +if model_name.startswith('inception'): +height = width = 299 +mean = [0.5, 0.5, 0.5] +std = [0.5, 0.5, 0.5] +else: +height = width = 224 +mean = [0.485, 0.456, 0.406] +std = [0.229, 0.224, 0.225] +input_shape = [1, 3, height, width] +input_data = torch.randn(input_shape).float() +for channel in range(3): +input_data[:, channel] -= mean[channel] +input_data[:, channel] /= std[channel] +model = getattr(torchvision.models, model_name)(pretrained=True) +model = model.float().eval() +return model, input_data + +def load_pretrainedmodels(model_name): +"""Given a model name, returns a pretrainedmodels.pytorch model in eval +mode as well as an example input.""" +import pretrainedmodels # https://github.com/Cadene/pretrained-models.pytorch +model = getattr(pretrainedmodels, model_name)().float().eval() +input_shape = [1, *model.input_size] +input_data = torch.rand(input_shape).float() * 256 +for channel in range(3): +input_data[:, channel] -= model.mean[channel] +input_data[:, channel] /= model.std[channel] +return model, input_data + +def load_model(model_name): +"""Given a model name, returns a model as well as an example input.""" +if hasattr(torchvision.models, model_name): +return load_torchvision(model_name) +try: +if hasattr(pretrainedmodels, model_name): +return load_pretrainedmodels(model_name) +except ModuleNotFoundError: +raise ModuleNotFoundError('Please install pretrainedmodels.pytorch') +raise RuntimeError('Model not supported') + + +def confidence_interval(mean, stdev, count, alpha=.01): +"""Returns the lower and upper bounds of the confidence interval of a random +variable. Confidence is 1 - alpha (default confidence is 99%).""" +stdval = tdistr.ppf(1 - alpha / 2, count - 1) +lower, upper = mean + np.array([-1, 1]) * stdval * stdev / np.sqrt(count) +return lower, upper + +def measure_latency(model, input_shapes, output_shapes, thresh, dryruns=40): +"""Compute the latency of the given model""" +latencies = [] +count = 0 +while True: +if isinstance(model, torch.nn.Module): +input_data = [torch.rand(shape).float() for shape in input_shapes] +if torch.cuda.is_available(): +input_data = list(map(lambda x: x.cuda(), input_data)) +model = model.cuda() +t_start = time() +model(*inp
[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_r375173153 ## File path: tests/python/frontend/pytorch/test_forward.py ## @@ -0,0 +1,732 @@ +# 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, invalid-name, unused-argument +"""Unit tests for various models and operators""" +from time import time +import os +import sys +from tempfile import TemporaryDirectory +from scipy.stats import t as tdistr +import numpy as np +import torch +import tvm +import torchvision +import single_op + + +from tvm import relay +from tvm.contrib import graph_runtime +#from tvm.relay.testing.config import ctx_list + +sys.setrecursionlimit(1) + +TARGET = 'llvm' +CTX = tvm.cpu() +EXT_ACCEL = None + +model_names = [] +baseline_latencies_map = {} +compiled_latencies_map = {} +speedups_map = {} + +test_repeats = 1 + +def _vectorize(ten): +return ten.reshape(-1) + +def atol(tru, est): +def _atol_elt(tru, est): +return abs(tru - est) +tru = _vectorize(tru) +est = _vectorize(est) +return max([_atol_elt(x, y) for x, y in zip(tru, est)]) + +def rtol(tru, est): +def _rtol_elt(tru, est): +return abs(tru - est) / min(abs(tru), abs(est)) +tru = _vectorize(tru) +est = _vectorize(est) +return max([_rtol_elt(x, y) for x, y in zip(tru, est)]) + +def assert_shapes_match(tru, est): +if tru.shape != est.shape: +msg = "Output shapes {} and {} don't match" +raise AssertionError(msg.format(tru.shape, est.shape)) + +def load_single_op(model_name, input_type=None): +"""Given a model name, returns a single-operator model in eval +mode as well as an example input.""" +input_shape = [1, 3, 224, 224] +if input_type is None or input_type == 'float32': +model = getattr(single_op, model_name)().float().eval() +input_data = torch.rand(input_shape).float() +elif input_type == 'float64': +model = getattr(single_op, model_name)().double().eval() +#input_data = torch.rand(input_shape).double() +#input_data = torch.from_numpy(np.random.random_sample((1, 3, 224, 224))).double() +temp = np.random.random_sample((1, 3, 224, 224)) +input_data = torch.from_numpy(temp) +elif input_type == 'float16': +model = getattr(single_op, model_name)().float().eval() +input_data = torch.rand(input_shape) +elif input_type == 'int32': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).int() +elif input_type == 'int16': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).short() +elif input_type == 'int8': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).char() +elif input_type == 'uint8': +model = getattr(single_op, model_name)().eval() +input_data = torch.randint(0, 10, input_shape).byte() +return model, input_data + +def load_torchvision(model_name, input_type=None): +"""Given a model name, returns a Torchvision model in eval mode as well +as an example input.""" + +if model_name.startswith('inception'): +height = width = 299 +mean = [0.5, 0.5, 0.5] +std = [0.5, 0.5, 0.5] +else: +height = width = 224 +mean = [0.485, 0.456, 0.406] +std = [0.229, 0.224, 0.225] +input_shape = [1, 3, height, width] + +model = getattr(torchvision.models, model_name)(pretrained=True) +#model = model.float().eval() + +if input_type is None or input_type == 'float32': +model = model.float().eval() +input_data = torch.randn(input_shape).float() +elif input_type == 'float64': +model = model.double().eval() +input_data = torch.randn(input_shape).double() + +for channel in range(3): +input_data[:, channel] -= mean[channel] +input_data[:, channel] /= std[channel] + +return model, input_data + +def load_pretrainedmodels(model_name): +"""Given a model name, returns a pretrainedmodels.pyto
[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_r375173992 ## File path: tests/python/frontend/pytorch/test_forward.py ## @@ -0,0 +1,851 @@ +# 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, invalid-name, unused-argument +"""Unit tests for various models and operators""" +from time import time +import os +import sys +from tempfile import TemporaryDirectory +from scipy.stats import t as tdistr +import numpy as np +import torch +from torch.nn import Module +import tvm +import torchvision + +from tvm import relay +from tvm.contrib import graph_runtime +from tvm.relay.testing.config import ctx_list + +sys.setrecursionlimit(1) + +def _vectorize(ten): +return ten.reshape(-1) + +def atol(tru, est): +def _atol_elt(tru, est): +return abs(tru - est) +tru = _vectorize(tru) +est = _vectorize(est) +return max([_atol_elt(x, y) for x, y in zip(tru, est)]) + +def rtol(tru, est): +def _rtol_elt(tru, est): +return abs(tru - est) / min(abs(tru), abs(est)) +tru = _vectorize(tru) +est = _vectorize(est) +return max([_rtol_elt(x, y) for x, y in zip(tru, est)]) + +def assert_shapes_match(tru, est): +if tru.shape != est.shape: +msg = "Output shapes {} and {} don't match" +raise AssertionError(msg.format(tru.shape, est.shape)) + +def load_torchvision(model_name): +"""Given a model name, returns a Torchvision model in eval mode as well +as an example input.""" +with torch.no_grad(): +if model_name.startswith('inception'): +height = width = 299 +mean = [0.5, 0.5, 0.5] +std = [0.5, 0.5, 0.5] +else: +height = width = 224 +mean = [0.485, 0.456, 0.406] +std = [0.229, 0.224, 0.225] +input_shape = [1, 3, height, width] +input_data = torch.randn(input_shape).float() +for channel in range(3): +input_data[:, channel] -= mean[channel] +input_data[:, channel] /= std[channel] +model = getattr(torchvision.models, model_name)(pretrained=True) +model = model.float().eval() +return model, input_data + +def load_pretrainedmodels(model_name): +"""Given a model name, returns a pretrainedmodels.pytorch model in eval +mode as well as an example input.""" +import pretrainedmodels # https://github.com/Cadene/pretrained-models.pytorch +model = getattr(pretrainedmodels, model_name)().float().eval() +input_shape = [1, *model.input_size] +input_data = torch.rand(input_shape).float() * 256 +for channel in range(3): +input_data[:, channel] -= model.mean[channel] +input_data[:, channel] /= model.std[channel] +return model, input_data + +def load_model(model_name): +"""Given a model name, returns a model as well as an example input.""" +if hasattr(torchvision.models, model_name): +return load_torchvision(model_name) +try: +if hasattr(pretrainedmodels, model_name): +return load_pretrainedmodels(model_name) +except ModuleNotFoundError: +raise ModuleNotFoundError('Please install pretrainedmodels.pytorch') +raise RuntimeError('Model not supported') + + +def confidence_interval(mean, stdev, count, alpha=.01): +"""Returns the lower and upper bounds of the confidence interval of a random +variable. Confidence is 1 - alpha (default confidence is 99%).""" +stdval = tdistr.ppf(1 - alpha / 2, count - 1) +lower, upper = mean + np.array([-1, 1]) * stdval * stdev / np.sqrt(count) +return lower, upper + +def measure_latency(model, input_shapes, output_shapes, thresh, dryruns=40): +"""Compute the latency of the given model""" +latencies = [] +count = 0 +while True: +if isinstance(model, torch.nn.Module): +input_data = [torch.rand(shape).float() for shape in input_shapes] +if torch.cuda.is_available(): +input_data = list(map(lambda x: x.cuda(), input_data)) +model = model.cuda() +t_start = time() +model(*inp