-
Notifications
You must be signed in to change notification settings - Fork 657
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
Feat empty op #5659
Merged
Merged
Feat empty op #5659
Changes from 4 commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
c94183d
fix bugs in shareing EagerBlobObject::blob_desc_.shape and EagerBlobO…
lixinqi 631ed38
Merge branch 'master' into refactor_eager_blob_object_shape
lixinqi 75be6a9
feat(EmptyOp): add flow.empty
wyg1997 f355238
docs(EmptyOp): add doctest and refine document
wyg1997 275d4b7
docs(EmptyOp): refine document
wyg1997 7631138
refactor(Tensor): Tensor constructor use empty_op
wyg1997 55e5c83
refactor(Tensor): remove useless code
wyg1997 8221bc6
Merge remote-tracking branch 'origin/master' into feat-add_empty_op
wyg1997 fd72499
feat(EmptyOp): support construct in given device and add
wyg1997 6512197
feat(EmptyOp): support unpacked tuple shape
wyg1997 a86be56
Merge remote-tracking branch 'origin/master' into refactor_eager_blob…
wyg1997 a5dd716
refine array functor code
wyg1997 52e3966
Merge remote-tracking branch 'origin/master' into feat-add_empty_op
wyg1997 f4faf11
Merge remote-tracking branch 'origin/refactor_eager_blob_object_shape…
wyg1997 a2d2147
docs(EmptyOp): update empty op document
wyg1997 fdcaa11
refine code
wyg1997 38c3827
docs(EmptyOp): add test and document for consistent empty op
wyg1997 a341960
update document
wyg1997 d9a1dd6
Merge branch 'master' into feat-add_empty_op
wyg1997 b52f857
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 87a02af
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot a5f1e73
fix merge bugs
wyg1997 143d471
fix(*): fix infer distribution
wyg1997 dd09067
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 6a1df24
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot d00b380
test(EmptyOp): fix ConsistentEmptyOp CPU_ONLY test bug
wyg1997 9d9fccf
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 7429b74
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot b9dea29
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot e4e80db
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 911e765
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot e210383
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 3b2d05c
fix(*): init shape when InitBlob
wyg1997 2223c0b
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 25717bc
fix(*): Constant and Empty Op use broadcast sbp
wyg1997 a790dfc
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 3560c43
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 31184f7
fix(indexing): replace MakeTensor with functional::Empty
wyg1997 5478eff
fix(*): fix compile bug
wyg1997 476afdc
refine code
wyg1997 a88ed9b
fix(nnGraph): make eager tensor
wyg1997 6367fb3
Merge branch 'master' into feat-add_empty_op
wyg1997 b9faece
auto format by CI
oneflow-ci-bot 0da4d90
fix(Stride): infer stride before initializing shape
wyg1997 66b571e
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot a75bdfb
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 2fbd207
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 30d74d6
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 8713b41
Merge branch 'master' into feat-add_empty_op
wyg1997 8caeb3f
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 76a932e
Merge branch 'master' into feat-add_empty_op
wyg1997 cdb16ad
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot ae9d3a7
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 3701588
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 90e4ad1
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot fa7f007
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 6373845
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot d898283
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot 43ce019
Merge branch 'master' into feat-add_empty_op
oneflow-ci-bot File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,30 @@ Maybe<EagerMirroredTensorImpl*> TensorImpl4Tensor(const std::shared_ptr<Tensor>& | |
return tensor->mut_eager_mirrored_tensor_impl(); | ||
} | ||
|
||
class MutMirroredTensorMeta : public TensorMeta { | ||
public: | ||
MutMirroredTensorMeta() : TensorMeta(std::make_shared<const Shape>(), kInvalidDataType) {} | ||
MutMirroredTensorMeta(const MutMirroredTensorMeta&) = default; | ||
MutMirroredTensorMeta(MutMirroredTensorMeta&&) = default; | ||
~MutMirroredTensorMeta() override = default; | ||
}; | ||
|
||
std::vector<TensorMeta*>* ThreadLocalDefaultOutputMutTensorMetas(int64_t size) { | ||
static thread_local std::vector<MutMirroredTensorMeta> struct_vec; | ||
static thread_local std::vector<TensorMeta*> ptr_vec; | ||
struct_vec.resize(size); | ||
ptr_vec.resize(size); | ||
if (size == 1) { | ||
ptr_vec.at(0) = &struct_vec.at(0); // unfold loop | ||
} else if (size == 2) { | ||
ptr_vec.at(0) = &struct_vec.at(0); // unfold loop | ||
ptr_vec.at(1) = &struct_vec.at(1); // unfold loop | ||
} else { | ||
for (int i = 0; i < size; ++i) { ptr_vec.at(i) = &struct_vec.at(i); } | ||
} | ||
return &ptr_vec; | ||
} | ||
|
||
} // namespace | ||
|
||
Maybe<void> NaiveInterpret(const UserOpExpr& user_op_expr, const TensorTuple& inputs, | ||
|
@@ -61,12 +85,16 @@ Maybe<void> NaiveInterpret(const UserOpExpr& user_op_expr, const TensorTuple& in | |
} | ||
std::shared_ptr<EagerBlobObjectList> output_eager_blob_objects = | ||
std::make_shared<EagerBlobObjectList>(outputs->size()); | ||
auto* output_tensor_metas = ThreadLocalDefaultOutputMutTensorMetas(outputs->size()); | ||
for (int i = 0; i < outputs->size(); i++) { | ||
if (!outputs->at(i)) { | ||
outputs->at(i) = | ||
const auto& tensor_impl = | ||
std::make_shared<MirroredTensor>(std::make_shared<EagerMirroredTensorImpl>()); | ||
} | ||
if (JUST(outputs->at(i)->has_eager_blob_object())) { | ||
outputs->at(i) = tensor_impl; | ||
output_tensor_metas->at(i) = tensor_impl->mut_tensor_meta(); | ||
} else { | ||
bool has_eager_blob_object = JUST(outputs->at(i)->has_eager_blob_object()); | ||
CHECK_OR_RETURN(has_eager_blob_object); | ||
output_eager_blob_objects->at(i) = JUST(outputs->at(i)->eager_blob_object()); | ||
} | ||
} | ||
|
@@ -101,14 +129,21 @@ Maybe<void> NaiveInterpret(const UserOpExpr& user_op_expr, const TensorTuple& in | |
return CHECK_JUST(TensorImpl4Tensor(inputs.at(i)))->mut_tensor_meta(); | ||
}, | ||
[&](int32_t i) -> TensorMeta* { | ||
return CHECK_JUST(TensorImpl4Tensor(outputs->at(i)))->mut_tensor_meta(); | ||
// using thread_local TensorMeta pointer if inplace. | ||
// using tensor_impl TensorMeta pointer if not inplace. | ||
return output_tensor_metas->at(i); | ||
Comment on lines
+141
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 非inplace时正常 infer 到 thread_local TensorMeta 中,inplace 时 infer 到实际的 tensor_impl 中 |
||
})); | ||
|
||
for (int i = 0; i < output_eager_blob_objects->size(); i++) { | ||
auto* tensor_impl = JUST(TensorImpl4Tensor(outputs->at(i))); | ||
if (!output_eager_blob_objects->at(i)) { | ||
auto* tensor_impl = JUST(TensorImpl4Tensor(outputs->at(i))); | ||
JUST(tensor_impl->InitEagerBlobObject(JUST(outputs->at(i)->device())->mem_case())); | ||
output_eager_blob_objects->at(i) = JUST(tensor_impl->eager_blob_object()); | ||
} else { | ||
// output i is inplaced. | ||
// check thread_local TensorMeta and tensor_impl TensorMeta. | ||
CHECK_OR_RETURN(tensor_impl->tensor_meta()->shape() == output_tensor_metas->at(i)->shape()); | ||
CHECK_OR_RETURN(tensor_impl->tensor_meta()->dtype() == output_tensor_metas->at(i)->dtype()); | ||
Comment on lines
+152
to
+156
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如果是inplace则直接检察infer的结果 |
||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
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. | ||
""" | ||
|
||
from typing import Optional, Union | ||
|
||
import oneflow as flow | ||
|
||
from oneflow.nn.common_types import _size_any_t | ||
from oneflow.nn.modules.utils import _single | ||
|
||
|
||
def empty_op( | ||
size: Union[_size_any_t, flow.Size], | ||
dtype: Optional[flow.dtype] = None, | ||
device: Union[flow.device, str] = None, | ||
requires_grad: bool = False, | ||
): | ||
""" | ||
Returns a tensor filled with uninitialized data. | ||
The shape of the tensor is defined by the variable argument ``size``. | ||
|
||
Args: | ||
size (an integer or tuple of integer values) – Defining the shape of the output tensor. Can be | ||
a variable number of arguments or a collection like a list or tuple. | ||
dtype (flow.dtype, optional) - The desired data type of returned tensor. Default: ``flow.float32``. | ||
wyg1997 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
device (torch.device, optional) – The desired device of returned tensor. Default: if None, uses the | ||
current device for the default tensor type | ||
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False. | ||
|
||
For example: | ||
|
||
.. code-block:: python | ||
|
||
>>> import oneflow as flow | ||
>>> y = flow.empty(5) | ||
>>> y.shape | ||
flow.Size([5]) | ||
|
||
""" | ||
assert size is not None, "shape must not be None" | ||
assert isinstance( | ||
size, (int, tuple, flow.Size) | ||
), "shape should be int or tuple of int or flow.Size" | ||
|
||
shape = _single(size) | ||
if dtype is None: | ||
dtype = flow.float32 | ||
if device is None: | ||
device = flow.device("cpu") | ||
|
||
tensor = flow.F.empty(shape=shape, dtype=dtype) | ||
tensor = tensor.to(device=device) | ||
tensor.requires_grad_(requires_grad) | ||
return tensor | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod(raise_on_error=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revert之前的改动,不共享shape的内存