utils.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from functools import reduce
  2. def maybe_view(tensor, size, check_same_size=True):
  3. if check_same_size and tensor.size() == size:
  4. return tensor
  5. return tensor.contiguous().view(size)
  6. def maybe_unexpand(tensor, old_size, check_same_size=True):
  7. if check_same_size and tensor.size() == old_size:
  8. return tensor
  9. num_unsqueezed = tensor.dim() - len(old_size)
  10. expanded_dims = [dim for dim, (expanded, original)
  11. in enumerate(zip(tensor.size()[num_unsqueezed:], old_size))
  12. if expanded != original]
  13. for _ in range(num_unsqueezed):
  14. tensor = tensor.sum(0, keepdim=False)
  15. for dim in expanded_dims:
  16. tensor = tensor.sum(dim, keepdim=True)
  17. return tensor
  18. # Check whether the op enable broadcasting, and whether it is supported by ONNX.
  19. # If dims1 and dims2 are different, then broadcast is True.
  20. # We always assume the combination of dims1 and dims2 is broadcastable.
  21. # The following types of broadcasting are supported in ONNX:
  22. # 1) Only one element in dims2, such as dims2 = [1, 1]
  23. # 2) dims2 is suffix of dims1, such as dims1 = [2, 3, 4], and dims2 = [3, 4]
  24. # Details can be found here: https://github.com/onnx/onnx/blob/master/docs/Operators.md#Gemm
  25. def check_onnx_broadcast(dims1, dims2):
  26. broadcast = False
  27. supported = True
  28. len1 = len(dims1)
  29. len2 = len(dims2)
  30. numel1 = reduce(lambda x, y: x * y, dims1)
  31. numel2 = reduce(lambda x, y: x * y, dims2)
  32. if len1 < len2:
  33. broadcast = True
  34. if numel2 != 1:
  35. supported = False
  36. elif len1 > len2:
  37. broadcast = True
  38. if numel2 != 1 and dims1[len1 - len2:] != dims2:
  39. supported = False
  40. else:
  41. if dims1 != dims2:
  42. broadcast = True
  43. if numel2 != 1:
  44. supported = False
  45. if not supported:
  46. raise ValueError("Numpy style broadcasting is not supported in ONNX. "
  47. "Input dims are: {}, {}".format(dims1, dims2))
  48. return broadcast