gussmith23 commented on a change in pull request #5812: URL: https://github.com/apache/incubator-tvm/pull/5812#discussion_r473452201
########## File path: tests/python/unittest/test_custom_datatypes.py ########## @@ -0,0 +1,405 @@ +# 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. +"""Utilities for changing datatypes of models.""" +import tvm +import tvm.topi.testing +import numpy as np +import pytest +from numpy.random import MT19937, RandomState, SeedSequence +from tvm import relay +from tvm.relay.testing.inception_v3 import get_workload as get_inception +from tvm.relay.testing.resnet import get_workload as get_resnet +from tvm.relay.testing.layers import batch_norm_infer +from tvm.relay.testing.mobilenet import get_workload as get_mobilenet +from tvm.target.datatype import register, register_min_func, register_op, create_lower_func, lower_ite, lower_call_pure_extern +from tvm.tir.op import call_pure_extern +from nose.tools import nottest + +# we use a random seed to generate input_data +# to guarantee stable tests +rs = RandomState(MT19937(SeedSequence(123456789))) + +def convert_ndarray(dst_dtype, *args, **kwargs): + """Converts NDArray(s) into the specified datatype""" + def convert(array): + x = relay.var('x', shape=array.shape, dtype=str(array.dtype)) + cast = relay.Function([x], x.astype(dst_dtype)) + with tvm.transform.PassContext(config={"tir.disable_vectorize": True}): + return relay.create_executor('graph').evaluate(cast)(array) + + return (tuple([convert(x) for x in args]), {k: convert(v) for (k, v) in kwargs.items()}) + + +def change_dtype(src, dst, module, params): + module = relay.frontend.ChangeDatatype(src, dst)(module) + module = relay.transform.InferType()(module) + params = dict((p, convert_ndarray(dst, params[p])) for p in params) + return module, params + +def compare(module, input, src_dtype, dst_dtype, rtol, atol, params = {}, target='llvm'): + module = relay.transform.SimplifyInference()(module) Review comment: - [x] @gussmith23 document here that `SimplifyInference` must be used. - [ ] @gussmith23 (more importantly) document elsewhere that `SimplifyInference` must be used. ########## File path: tests/python/unittest/test_custom_datatypes.py ########## @@ -0,0 +1,405 @@ +# 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. +"""Utilities for changing datatypes of models.""" +import tvm +import tvm.topi.testing +import numpy as np +import pytest +from numpy.random import MT19937, RandomState, SeedSequence +from tvm import relay +from tvm.relay.testing.inception_v3 import get_workload as get_inception +from tvm.relay.testing.resnet import get_workload as get_resnet +from tvm.relay.testing.layers import batch_norm_infer +from tvm.relay.testing.mobilenet import get_workload as get_mobilenet +from tvm.target.datatype import register, register_min_func, register_op, create_lower_func, lower_ite, lower_call_pure_extern +from tvm.tir.op import call_pure_extern +from nose.tools import nottest + +# we use a random seed to generate input_data +# to guarantee stable tests +rs = RandomState(MT19937(SeedSequence(123456789))) + +def convert_ndarray(dst_dtype, *args, **kwargs): + """Converts NDArray(s) into the specified datatype""" + def convert(array): + x = relay.var('x', shape=array.shape, dtype=str(array.dtype)) + cast = relay.Function([x], x.astype(dst_dtype)) + with tvm.transform.PassContext(config={"tir.disable_vectorize": True}): + return relay.create_executor('graph').evaluate(cast)(array) + + return (tuple([convert(x) for x in args]), {k: convert(v) for (k, v) in kwargs.items()}) + + +def change_dtype(src, dst, module, params): + module = relay.frontend.ChangeDatatype(src, dst)(module) + module = relay.transform.InferType()(module) + params = dict((p, convert_ndarray(dst, params[p])) for p in params) + return module, params + +def compare(module, input, src_dtype, dst_dtype, rtol, atol, params = {}, target='llvm'): + module = relay.transform.SimplifyInference()(module) Review comment: - [x] @gussmith23 document here that `SimplifyInference` must be used. - [x] @gussmith23 (more importantly) document elsewhere that `SimplifyInference` must be used. ---------------------------------------------------------------- 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: [email protected]
