test_utils.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import sys
  2. import numpy as np
  3. def print_test_debug_info(testname, items_dict):
  4. filename = "debug_operator_onnxifi_" + testname + ".txt"
  5. np.set_printoptions(threshold=sys.maxsize)
  6. with open(filename, 'w') as f:
  7. for key, value in items_dict.items():
  8. print(key, value)
  9. f.write("{}\n".format(key))
  10. f.write("{}\n".format(value))
  11. def print_net(net):
  12. for i in net.external_input:
  13. print("Input: {}".format(i))
  14. for i in net.external_output:
  15. print("Output: {}".format(i))
  16. for op in net.op:
  17. print("Op {}".format(op.type))
  18. for x in op.input:
  19. print(" input: {}".format(x))
  20. for y in op.output:
  21. print(" output: {}".format(y))
  22. def _sigmoid(x):
  23. return 1. / (1. + np.exp(np.float64(-x)))
  24. def _tanh(x):
  25. return np.tanh(np.float64(x))
  26. def _swish(x):
  27. return np.float64(x) * _sigmoid(x)
  28. def _gelu_by_sigmoid(x):
  29. return np.float64(x) / (1. + np.exp(np.float64(x) * 1.702))
  30. def _acc_func(opname, x):
  31. if opname == "Swish":
  32. return _swish(x)
  33. elif opname == "Sigmoid":
  34. return _sigmoid(x)
  35. elif opname == "Tanh":
  36. return _tanh(x)
  37. elif opname == "Gelu":
  38. return _gelu_by_sigmoid(x)
  39. else:
  40. return x
  41. def _get_ulp16(x):
  42. abs_x = np.abs(x)
  43. mask = (abs_x > 2.**(-14))
  44. abs_x = mask * abs_x + (1 - mask) * 2.**(-14)
  45. k = np.floor(np.log2(abs_x))
  46. return 2.**(k - 10)
  47. def compute_ulp_error(opname, xvec, y_nnpi):
  48. y_acc = _acc_func(opname, np.float64(xvec))
  49. scale = 1. / _get_ulp16(y_acc)
  50. return (y_nnpi - y_acc) * scale