Skip to content

Commit bbec35f

Browse files
Skylion007pytorchmergebot
authored andcommitted
[BE]: Replace clone detach with detach clone to be more efficient (#144469)
Follow up to #144270 and fix some vulkan code Pull Request resolved: #144469 Approved by: https://github.com/awgu
1 parent 73278e6 commit bbec35f

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

aten/src/ATen/native/vulkan/ops/Random.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static Tensor rand_like(
7070
// numbers from a uniform distribution on the interval [0,1). To match the CPU
7171
// implementation, we simplify the range to [0,1] and tolerate the small
7272
// chance of 1 being sampled.
73-
return input_arg.clone().detach().uniform_(0.0, 1.0);
73+
return input_arg.detach().clone().uniform_(0.0, 1.0);
7474
}
7575

7676
static Tensor& normal_(
@@ -129,7 +129,7 @@ static Tensor randn_like(
129129
const std::optional<c10::MemoryFormat> /* not implemented */) {
130130
// Returns a tensor with the same size as input that is filled with random
131131
// numbers from a normal distribution with mean 0 and standard deviation 1.
132-
return input_arg.clone().detach().normal_(0.0, 1.0);
132+
return input_arg.detach().clone().normal_(0.0, 1.0);
133133
}
134134

135135
TORCH_LIBRARY_IMPL(aten, Vulkan, m) {

test/cpp/lazy/test_lazy_ops_util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void TestBackward(
140140
const torch::Tensor& input = inputs[i];
141141
if (input.defined()) {
142142
torch::Tensor oinput =
143-
input.clone().detach().set_requires_grad(input.requires_grad());
143+
input.detach().clone().set_requires_grad(input.requires_grad());
144144
input_vars.push_back(oinput);
145145

146146
torch::Tensor xinput = CopyToDevice(input, device)

torch/csrc/utils/tensor_new.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,8 +1524,8 @@ Tensor new_tensor(
15241524
if (THPVariable_Check(data)) {
15251525
auto ret = PyErr_WarnEx(
15261526
PyExc_UserWarning,
1527-
"To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() "
1528-
"or sourceTensor.clone().detach().requires_grad_(True), rather than tensor.new_tensor(sourceTensor).",
1527+
"To copy construct from a tensor, it is recommended to use sourceTensor.detach().clone() "
1528+
"or sourceTensor.detach().clone().requires_grad_(True), rather than tensor.new_tensor(sourceTensor).",
15291529
1);
15301530
if (ret != 0)
15311531
throw python_error();

0 commit comments

Comments
 (0)