Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions paddle/fluid/pybind/eager_method.cc
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,24 @@ static PyObject* tensor_method_copy_(TensorObject* self,
EAGER_CATCH_AND_THROW_RETURN_NULL
}

static PyObject* tensor_method_clone(TensorObject* self,
PyObject* args,
PyObject* kwargs) {
EAGER_TRY

PADDLE_ENFORCE_EQ(
self->tensor.initialized(),
true,
paddle::platform::errors::InvalidArgument(
"We can only support initialized tensor in clone, however we got "
"uninitialized tensor %s, please check your code.",
self->tensor.name()));

auto out = assign_ad_func(self->tensor);
return ToPyObject(out);
EAGER_CATCH_AND_THROW_RETURN_NULL
}

static PyObject* tensor_retain_grads(TensorObject* self,
PyObject* args,
PyObject* kwargs) {
Expand Down Expand Up @@ -1841,6 +1859,10 @@ PyMethodDef variable_methods[] = {
(PyCFunction)(void (*)(void))tensor_method_copy_,
METH_VARARGS | METH_KEYWORDS,
NULL},
{"clone",
(PyCFunction)(void (*)(void))tensor_method_clone,
METH_VARARGS | METH_KEYWORDS,
NULL},
{"reconstruct_from_",
(PyCFunction)(void (*)(void))tensor_method_reconstruct_from_,
METH_VARARGS | METH_KEYWORDS,
Expand Down
12 changes: 0 additions & 12 deletions python/paddle/fluid/dygraph/varbase_patch_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,17 +815,6 @@ def _set_grad_ivar(self, value):
raise TypeError(
"_set_grad_ivar is only supported for Parameter Tensor")

@framework.dygraph_only
def clone(self):
if in_dygraph_mode():
return _C_ops.assign(self)

if _in_legacy_dygraph():
output = core.VarBase()
else:
output = core.eager.Tensor()
return _legacy_C_ops.assign(self, output)

@framework.dygraph_only
def value(self):
return self
Expand Down Expand Up @@ -1009,7 +998,6 @@ def to_sparse_coo(self, sparse_dim):
if framework._in_eager_mode_:
setattr(core.eager.Tensor, "_grad_ivar", _grad_ivar)
setattr(core.eager.Tensor, "_set_grad_ivar", _set_grad_ivar)
setattr(core.eager.Tensor, "clone", clone)
setattr(core.eager.Tensor, "value", value)
setattr(core.eager.Tensor, "cpu", cpu)
setattr(core.eager.Tensor, "cuda", cuda)
Expand Down