Skip to content

Commit f5df0c9

Browse files
t-vifacebook-github-bot
authored andcommitted
Don't end on inplace operators in einsum (#22111)
Summary: Returning the result of an inplace `squeeze_` in `einsum` (which itself is traced) interacts badly with `autograd.Function`. I must admit that I'm not 100% certain whether it should be necessary to change this, but I consider this a good change overall. Fixes: #22072 Pull Request resolved: #22111 Differential Revision: D15974990 Pulled By: soumith fbshipit-source-id: 477e7f23833f02999085f665c175d062e7d32acd
1 parent ede0849 commit f5df0c9

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

aten/src/ATen/native/Linear.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,13 @@ static Tensor sumproduct_pair(const Tensor& left_, const Tensor& right_, IntArra
119119

120120
// finally squeeze summed dimensions if desired
121121
if (! keepdim) {
122-
for (int i = dim-1; i>=0; i--)
123-
if (sum_dims[i])
124-
result.squeeze_(i);
122+
auto sizes = result.sizes().vec();
123+
for (int i = dim-1; i>=0; i--) {
124+
if (sum_dims[i]) {
125+
sizes.erase(sizes.begin() + i);
126+
}
127+
}
128+
result = result.view(sizes);
125129
}
126130
return result;
127131
}
@@ -354,8 +358,12 @@ Tensor einsum(std::string eqn, TensorList tensors) {
354358
result = at::native::sumproduct_pair(result, preprocessed_operands[i], sum_dims, true);
355359
}
356360
// finally, we squeeze out all non-result dimensions
357-
for (int64_t dim = num_total_idxes-1; dim >= num_output_dims; dim--)
358-
result.squeeze_(dim);
361+
auto sizes = result.sizes().vec();
362+
for (int64_t dim = num_total_idxes-1; dim >= num_output_dims; dim--) {
363+
sizes.erase(sizes.begin() + dim);
364+
}
365+
366+
result = result.view(sizes);
359367
return result;
360368
}
361369

0 commit comments

Comments
 (0)