Aleksei-grovety commented on code in PR #16266:
URL: https://github.com/apache/tvm/pull/16266#discussion_r1521566815
##########
python/tvm/relay/backend/contrib/ethosu/legalize.py:
##########
@@ -135,21 +135,68 @@ def get_lut_from_func(
ofm_scale: float,
ofm_zp: int,
func: Callable[[float], float],
+ dtype,
) -> List[int]:
"""Calculates the values of the lookup table based on the calculation
function"""
- lut_values = list()
- # Only int8 is currently supported
- dtype = np.int8
- qmin, qmax = np.iinfo(dtype).min, np.iinfo(dtype).max
- for x in range(qmin, qmax + 1):
- x_real = ifm_scale * (x - ifm_zp)
- out_real = func(x_real)
- lut_result = int(util.round_away_zero(ofm_zp + out_real / ofm_scale))
- lut_result = min(qmax, max(qmin, lut_result))
- lut_values.append(lut_result)
+ assert dtype in ["int8", "int16"]
- return lut_values
+ if dtype == "int8":
+ lut_values = list()
+ qmin, qmax = np.iinfo(dtype).min, np.iinfo(dtype).max
+ for x in range(qmin, qmax + 1):
+ x_real = ifm_scale * (x - ifm_zp)
+ out_real = func(x_real)
+ lut_result = int(util.round_away_zero(ofm_zp + out_real /
ofm_scale))
+ lut_result = min(qmax, max(qmin, lut_result))
+ lut_values.append(lut_result)
+
+ return lut_values
+ else:
+ # dtype == "int16"
+ input_min = ifm_scale * (np.iinfo(np.int16).min - ifm_zp)
+ input_max = ifm_scale * (np.iinfo(np.int16).max - ifm_zp)
+
+ output_min = ofm_scale * (np.iinfo(np.int16).min - ofm_zp)
+ output_max = ofm_scale * (np.iinfo(np.int16).max - ofm_zp)
+ # Create 16 bit lut following the reference
+ nbr_steps = 512
+ step = (input_max - input_min) / nbr_steps
+ half_step = step / 2
+ output_scaling_inv = (np.iinfo(np.int16).max - np.iinfo(np.int16).min
+ 1) / (
+ output_max - output_min
+ )
+ table_min = np.iinfo(np.int16).min
+ table_max = np.iinfo(np.int16).max
Review Comment:
Done.
--
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]