Description
The purpose of this issue is to determine how, or if, broadcasting behavior should be specified for linalg.cross
.
Overview
At the moment, linalg.cross
is specified to only compute the cross product between two arrays x
and y
having the same shape. Meaning, the current linalg.cross
behavior is specified to not support broadcasting.
The origin of the specified behavior comes from PyTorch (<= v1.10.x) and TensorFlow which required (at the time of specification) input arrays of the same shape. Not supporting broadcasting was thus the lowest common denominator across array libraries.
TensorFlow has not changed its behavior; PyTorch has.
NumPy, in its np.cross
API, supports a limited form of broadcasting, as well as 2
element vectors. The broadcasting is limited as the computation dimension must have 2
or 3
elements and is not allowed to broadcast. E.g.,
In [1]: x = np.random.randn(4,3)
In [2]: y = np.random.randn(1,3)
In [3]: z = np.random.randn(4,1)
In [3]: np.cross(x,y)
Out[3]:
array([[ 0.04541174, 0.07097804, -0.53846464],
[ 0.01457143, -0.74247471, -0.1795357 ],
[ 2.4163107 , -0.72177202, -5.98228811],
[ 1.14862403, -1.86202792, 0.32601926]])
In [4]: np.cross(x,z)
ValueError: incompatible dimensions for cross product
(dimension must be 2 or 3)
In [5]: np.add(x,z)
Out[5]:
array([[ 1.66159175, 0.92220278, 0.93708491],
[ 0.26326781, -0.37688777, 1.26610177],
[ 1.34535177, 1.13112439, 0.38157179],
[-1.78861678, -1.34595513, -0.08110636]])
Even though x
and z
are broadcast compatible, np.cross
raises an exception.
Starting in PyTorch v1.11.0, linalg.cross
supports broadcasting. From the docs,
Also supports batches of vectors, for which it computes the product along the dimension dim. In this case, the output has the same batch dimensions as the inputs broadcast to a common shape.
If after broadcasting input.size(dim) != 3 or other.size(dim) != 3.
Thus implying full broadcast support.
Questions
- Should the spec add support for broadcasting to
linalg.cross
? If so, full broadcasting or NumPy-style (i.e., must have matching dimension sizes, similar to howvecdot
works)? - Should the spec add support for size
2
vectors similar to NumPy?