gussmith23 commented on a change in pull request #5812: URL: https://github.com/apache/incubator-tvm/pull/5812#discussion_r470707899
########## File path: tests/python/unittest/test_custom_datatypes_change_dtype.py ########## @@ -0,0 +1,553 @@ +# 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 topi.testing +import numpy as np +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.mobilenet import get_workload as get_mobilenet +from tvm.target.datatype import register, register_min_func, register_op, create_lower_func, lower_ite +from nose.tools import nottest + +tgt = "llvm" + + +def convert_ndarray(dst_dtype, array): + """Converts an NDArray into the specified datatype""" + 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) + + +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 setup(): + """Set up tests + + Currently, this registers some custom datatypes using the Bring Your + Own Datatypes framework. + """ + + # To use datatype operations in an external library, you should first load + # the library containing the datatype implementation: + # CDLL("libposit.so", RTLD_GLOBAL) + # In this case, the datatype library we are using is built right into TVM, + # so we do not need to explicitly load any library. + + # You can pick a code for your datatype arbitrarily, as long as it is + # greater than 128 and has not already been chosen. + + register("posit32", 131) + + register_op(create_lower_func("FloatToPosit32es2"), "Cast", "llvm", + "posit32", "float") + register_op(create_lower_func("Posit32es2ToFloat"), "Cast", "llvm", + "float", "posit32") + register_op(create_lower_func("IntToPosit32es2"), "Cast", "llvm", + "posit32", "int") + register_op(create_lower_func("Posit32es2Add"), "Add", "llvm", "posit32") + register_op(create_lower_func("Posit32es2Sub"), "Sub", "llvm", "posit32") + register_op(create_lower_func("FloatToPosit32es2"), "FloatImm", "llvm", + "posit32") + register_op(create_lower_func("Posit32es2Mul"), "Mul", "llvm", "posit32") + register_op(create_lower_func("Posit32es2Div"), "Div", "llvm", "posit32") + register_op(create_lower_func("Posit32es2Max"), "Max", "llvm", "posit32") + register_op(create_lower_func("Posit32es2Sqrt"), + "Call", + "llvm", + "posit32", + intrinsic_name="sqrt") + # TODO(gus) not sure if this will work... Review comment: Great, can you document this in the test that tests different ops? ---------------------------------------------------------------- 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]
