Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add oneflow.clone api #9800

Merged
merged 9 commits into from
Jan 30, 2023
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
4 changes: 4 additions & 0 deletions oneflow/core/functional/functional_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3145,3 +3145,7 @@
- name: "mode"
signature: "TensorTuple[values, indices] (Tensor input, Int32 dim=-1, Bool keepdim=False) => Mode"
bind_python: True

- name: "clone"
signature: "Tensor (Tensor input) => Clone"
bind_python: True
6 changes: 6 additions & 0 deletions oneflow/core/functional/impl/array_functor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3918,6 +3918,11 @@ class SortFunctor {
}
};

class CloneFunctor {
public:
Maybe<Tensor> operator()(const std::shared_ptr<Tensor>& input) const { return input->clone(); }
};

} // namespace impl

ONEFLOW_FUNCTION_LIBRARY(m) {
Expand Down Expand Up @@ -4073,6 +4078,7 @@ ONEFLOW_FUNCTION_LIBRARY(m) {
m.add_functor<impl::UniqueWithCountsFunctor>("UniqueWithCounts");
m.add_functor<impl::BaddBmmFunctor>("BaddBmm");
m.add_functor<impl::SortFunctor>("Sort");
m.add_functor<impl::CloneFunctor>("Clone");
};

} // namespace functional
Expand Down
1 change: 1 addition & 0 deletions python/oneflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def is_deprecated(func_or_class):
from oneflow._C import allclose
from oneflow._C import index_add, index_add_
from oneflow._C import sort
from oneflow._C import clone

from oneflow._oneflow_internal import _set_num_threads as set_num_threads

Expand Down
27 changes: 27 additions & 0 deletions python/oneflow/framework/docstr/array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,3 +759,30 @@
tensor([0.0000, 0.2500, 0.2000, 0.0000], dtype=oneflow.float32)
""",
)

add_docstr(
oneflow.clone,
r"""oneflow.clone(input) → Tensor

Returns a copy of input.

The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.clone.html

.. note::
This function is differentiable, so gradients will flow back from the result
of this operation to ``input``. To create a tensor without an autograd relationship
to ``input`` see :meth:`detach`.

Args:
input (oneflow.Tensor): input Tensor to be cloned

For example:
.. code-block:: python

>>> import oneflow as flow
>>> x = flow.Tensor([1, 2, 3])
>>> y = flow.clone(x)
>>> y
tensor([1., 2., 3.], dtype=oneflow.float32)
""",
)
16 changes: 16 additions & 0 deletions python/oneflow/framework/docstr/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2498,3 +2498,19 @@
tensor([1, 1, 1, 1], dtype=oneflow.int32)
""",
)

add_docstr(
oneflow.Tensor.clone,
"""
See :func:`oneflow.clone`

For example:

.. code-block:: python

>>> import oneflow as flow
>>> x = flow.tensor([1, 2, 3])
>>> x.clone()
tensor([1, 2, 3], dtype=oneflow.int64)
""",
)
34 changes: 34 additions & 0 deletions python/oneflow/test/modules/test_clone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Copyright 2020 The OneFlow Authors. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import unittest

import oneflow as flow
import oneflow.unittest

from oneflow.test_utils.automated_test_util import *


@flow.unittest.skip_unless_1n1d()
class TestClone(flow.unittest.TestCase):
@autotest(n=3)
def test_clone_with_random_data(test_case):
x = random_tensor()
y = torch.clone(x)
return y


if __name__ == "__main__":
unittest.main()