jverma-quic commented on code in PR #12340: URL: https://github.com/apache/tvm/pull/12340#discussion_r950287893
########## tests/python/contrib/test_hexagon/test_fixed_point_conversion.py: ########## @@ -0,0 +1,58 @@ +# 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. + +import math +import struct +import numpy as np +import tvm.topi.hexagon.utils as utils + +""" +Test float to fixed-point conversion. We do it by constructing a numpy array with the +wide range of floating-point values. These values are converted into the +fixed-point value using topi.hexagon.utils.get_fixed_point_value. Then, these values are +converted back into float using scale_factor provided by the function. These converted +floating point values are then compared against the original values and an assertion is +raised if they happened to be outside of the expected tolerance. +""" + + +class TestFixedPointConversion: + def test_fixed_point_conversion(self): + # Construct array with wide range of values + fp1 = np.random.uniform(0.00001, 0.0002, size=(10)) + fp2 = np.random.uniform(0.001, 0.02, size=(10)) + fp3 = np.random.uniform(1, 20, size=(10)) + fp4 = np.random.uniform(900, 1000, size=(10)) + fp5 = np.random.uniform(1e9, 1e10, size=(10)) + fp6 = np.random.uniform(2.44885652993e38, 2.54885652993e38, size=(1)) + fp7 = np.random.uniform(1.46711479073e-34, 1.76098837843e-34, size=(1)) + float_arr = np.concatenate((fp1, fp2, fp3, fp4, fp5, fp6, fp7)) + for flp in float_arr: + fxp, rsh = utils.get_fixed_point_value(flp, "int16") + # Compute scale_factor using rsh (rsh is log2 of the scale_factor). While doing this, + # we use IEEE-754 floating-point representation since rsh can be negative or positive. + + scale = ((rsh + 127) & 0xFF) << 23 # Add bias (127) and position it into exponent bits + scale_i = struct.pack("I", scale) # Pack it as integer + scale_f = struct.unpack("f", scale_i) # Unpack as float + + converted_flp = fxp / scale_f[0] Review Comment: That's what I had earlier but I decided not to do it mainly because it's need just for testing and doesn't provide any additional value. I would prefer to keep it that way unless this is a major concern. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
