Skip to content

Commit d49abae

Browse files
committed
Add cheaper checks to Tensor::to
1 parent 6b51bca commit d49abae

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

aten/src/ATen/TensorOptions.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,23 @@ inline Tensor to(
257257

258258
inline Tensor Tensor::to(Device device, ScalarType dtype, bool non_blocking)
259259
const {
260+
if (this->device() == device && this->dtype() == dtype) {
261+
return *this;
262+
}
260263
return detail::to(*this, options().device(device).dtype(dtype), non_blocking);
261264
}
262265

263266
inline Tensor Tensor::to(ScalarType dtype, bool non_blocking) const {
267+
if (this->dtype() == dtype) {
268+
return *this;
269+
}
264270
return detail::to(*this, options().dtype(dtype), non_blocking);
265271
}
266272

267273
inline Tensor Tensor::to(Device device, bool non_blocking) const {
274+
if (this->device() == device) {
275+
return *this;
276+
}
268277
return detail::to(*this, options().device(device), non_blocking);
269278
}
270279
} // namespace at

0 commit comments

Comments
 (0)