dim of 0 is a special case, that will make your tensor volume to be 0.
Apparently, in is considered to be “bigger” than 1 in broadcasting. So the resulting dim is 0*1=0
but adding an empty tensor to some tensor, should return some tensor. I mean adding something to nothing should return something, why does it return nothing?
or it should give error, that addition of these two tensors is not possible.
It not actually like adding something to nothing, x = torch.randn(1, 1, 0) is empty indeed, but it has a shape.
Then, for each dimension size, the resulting dimension size is the max of the sizes of x and y along that dimension.
I don’t know where you read about this, but it’s incorrect. To broadcast, two must meet the rules, check Broadcasting semantics — PyTorch 2.1 documentation. For not equal dimensions, the resulting should be the one dimension which is not 1. (Generally the larger one, except for 0 I guess.
Take the broadcasting rules this way, if two dimension is not equal and one of them is 1, repeat the one with 1 dimension to make them match.
So when performing x + y with shape [1, 1, 0] and [4, 1, 1], image that
Along the first dimension, their shapes are not equal and x has dimension 1, so x is repeated 4 times, making it [4, 1, 0] (which is still empty).
Along the second dimension, their shapes are equal, so skip.
Along the third dimension, their shapes are not equal and y has dimension 1, so y is repeated 0 times, making it [4, 1, 0] (and it becomes empty).