-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disallow operations on scalar tensors that are no-ops #794
Comments
As an additional data point, both LiteRT and Core ML do not directly support applying their transpose operators to a scalar and implementations based on these frameworks will require workarounds. DirectML only supports this operation because transposition is implemented by adjusting the dimensions and strides of a tensor as it is passed between other nodes, which is a no-op for a scalar tensor. |
We recall that @fdwr had a reason why supporting this was useful for developers, but if so then we should include a more complete discussion of tensor operations on scalars in the specification. Right now there's not much mention that a tensor can have a rank of 0. |
I have no qualms about disallowing this for I don't have easy access to CoreML or TFLiteRT to try, but I'd be surprised if
Yeah, we could add more mention.
...but we could clarify the existing wording "allowed ranks ... are given as an explicit rank (e.g. 1), or N to allow any dimensionality" to explicitly include Reshape reference code# pip freeze torch==1.11.0+cpu
import tensorflow as tf
x = tf.constant(42, dtype=tf.float32)
y = tf.reshape(x, [])
print("value:", y)
print("shape:", y.shape)
# value: 42
# shape: [] # pip freeze torch==1.11.0+cpu
import torch
x = torch.tensor(42, dtype=torch.float32)
print("x value:", x)
print("x shape:", x.shape)
print("x dtype:", x.dtype)
y = x.reshape(())
print("y value:", y)
print("y shape:", y.shape)
print("y dtype:", y.dtype)
# x value: tensor(42.)
# x shape: torch.Size([])
# x dtype: torch.float32
# y value: tensor(42.)
# y shape: torch.Size([])
# y dtype: torch.float32 # pip freeze numpy==1.21.6
import numpy
x = numpy.array(42, dtype=numpy.float32)
y = x.reshape([])
print("value:", y)
print("shape:", y.shape)
print("dtype:", y.dtype)
# value: 42.0
# shape: ()
# dtype: float32 |
I agree with the argument in favor of I'm less sure about other operators. For example What I'd like to see are examples of where silently tolerating scalars is useful for developers. This might end up in a similar place as #391. To start we should go through each case where we allow a rank of N and clarify whether 0 is a valid value of N. |
+1 - if this issue turns into a PR we should include that. |
There are a number of operators, such as
transpose()
andreshape()
which are no-ops when applied to a scalar tensor. In addition there are operators likegather()
which expect indices but a scalar doesn't have indices.While implementations can work around this (typically by treating a scalar as a single-element 1D tensor) it seems better to simply disallow these cases which add complexity without adding any expressivity.
The text was updated successfully, but these errors were encountered: