-
Notifications
You must be signed in to change notification settings - Fork 790
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
tensorsplit_op #7258
Merged
Merged
tensorsplit_op #7258
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b9c5bb8
first
lcylcy 41595ed
hsplit,vsplit
lcylcy b9f34a9
revise
lcylcy ef0770b
confict
lcylcy 970edcd
confict
lcylcy b41b8b2
docs
lcylcy 7bce539
formatted
lcylcy a946188
Merge branch 'master' into lcy_tensorsplit
lcylcy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,9 @@ oneflow | |
diag, | ||
diagonal, | ||
movedim, | ||
tensor_split, | ||
hsplit, | ||
vsplit, | ||
as_strided, | ||
div, | ||
dot, | ||
|
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 |
---|---|---|
|
@@ -1842,6 +1842,137 @@ class MovedimIntFunctor { | |
} | ||
}; | ||
|
||
class TensorSplitVecFunctor { | ||
public: | ||
TensorSplitVecFunctor() = default; | ||
Maybe<TensorTuple> operator()(const std::shared_ptr<one::Tensor>& input, | ||
const std::vector<int32_t>& indices_or_sections, | ||
const int32_t& dim) const { | ||
int32_t ndim = input->ndim(); | ||
CHECK_OR_RETURN((dim >= -ndim) && (dim < ndim)) | ||
<< "Dimension out of range (expected to be in range of [" << -ndim << "," << ndim - 1 | ||
<< "], but got " << dim << ")"; | ||
int32_t pos_dim = dim >= 0 ? dim : dim + ndim; | ||
|
||
std::vector<int64_t> start(ndim, 0); | ||
std::vector<int64_t> stop(ndim); | ||
std::vector<int64_t> step(ndim, 1); | ||
for (int32_t i = 0; i < ndim; i++) { stop[i] = input->dim(i); } | ||
|
||
int32_t num_indices = indices_or_sections.size(); | ||
TensorTuple output(num_indices + 1); | ||
for (int32_t i = 0; i < num_indices; i++) { | ||
int32_t end_idx = indices_or_sections[i]; | ||
stop[pos_dim] = end_idx; | ||
output[i] = JUST(Slice(input, start, stop, step)); | ||
start[pos_dim] = end_idx; | ||
} | ||
stop[pos_dim] = input->shape()->At(ndim - 1); | ||
output[num_indices] = JUST(Slice(input, start, stop, step)); | ||
|
||
return output; | ||
} | ||
}; | ||
|
||
class TensorSplitIntFunctor { | ||
public: | ||
TensorSplitIntFunctor() = default; | ||
Maybe<TensorTuple> operator()(const std::shared_ptr<one::Tensor>& input, | ||
const int32_t& indices_or_sections, const int32_t& dim) const { | ||
int32_t ndim = input->ndim(); | ||
CHECK_OR_RETURN((dim >= -ndim) && (dim < ndim)) | ||
<< "Dimension out of range (expected to be in range of [" << -ndim << "," << ndim - 1 | ||
<< "], but got " << dim << ")"; | ||
CHECK_OR_RETURN(indices_or_sections > 0) | ||
<< "number of sections must be larger than 0, got ," << indices_or_sections << ");"; | ||
int32_t pos_dim = dim >= 0 ? dim : dim + ndim; | ||
|
||
const auto dim_size = input->dim(pos_dim); | ||
int64_t min_split_size = dim_size / indices_or_sections; | ||
int64_t num_splits_one_extra = dim_size % indices_or_sections; | ||
|
||
std::vector<int64_t> start(ndim, 0); | ||
std::vector<int64_t> stop(ndim); | ||
std::vector<int64_t> step(ndim, 1); | ||
for (int32_t i = 0; i < ndim; i++) { stop[i] = input->dim(i); } | ||
stop[pos_dim] = 0; | ||
|
||
TensorTuple output(indices_or_sections); | ||
for (int32_t i = 0; i < indices_or_sections; i++) { | ||
int64_t split_size = (i < num_splits_one_extra) ? (min_split_size + 1) : min_split_size; | ||
stop[pos_dim] += split_size; | ||
output[i] = JUST(Slice(input, start, stop, step)); | ||
start[pos_dim] += split_size; | ||
} | ||
|
||
return output; | ||
} | ||
}; | ||
|
||
class HsplitIntFunctor { | ||
public: | ||
HsplitIntFunctor() = default; | ||
Maybe<TensorTuple> operator()(const std::shared_ptr<one::Tensor>& input, | ||
const int32_t& indices_or_sections) const { | ||
int32_t ndim = input->ndim(); | ||
CHECK_OR_RETURN(ndim >= 1) | ||
<< "torch.hsplit requires a tensor with at least 1 dimension, but got a tensor with " | ||
<< ndim << " dimensions!"; | ||
CHECK_OR_RETURN(indices_or_sections > 0) << "indices_or_sections must greater than 0"; | ||
int32_t dim = (ndim == 1) ? 0 : 1; | ||
CHECK_OR_RETURN(input->dim(dim) % indices_or_sections == 0) | ||
<< "torch.hsplit attempted to split along dimension " << dim | ||
<< ", but the size of the dimension " << input->shape()->At(dim) | ||
<< " is not divisible by the split_size " << indices_or_sections << "!"; | ||
return TensorSplitInt(input, indices_or_sections, dim); | ||
} | ||
}; | ||
|
||
class HsplitVecFunctor { | ||
public: | ||
HsplitVecFunctor() = default; | ||
Maybe<TensorTuple> operator()(const std::shared_ptr<one::Tensor>& input, | ||
const std::vector<int32_t>& indices_or_sections) const { | ||
int32_t ndim = input->ndim(); | ||
CHECK_OR_RETURN(ndim >= 1) | ||
<< "torch.hsplit requires a tensor with at least 1 dimension, but got a tensor with " | ||
<< ndim << " dimensions!"; | ||
int32_t dim = (ndim == 1) ? 0 : 1; | ||
return TensorSplitVec(input, indices_or_sections, dim); | ||
} | ||
}; | ||
|
||
class VsplitIntFunctor { | ||
public: | ||
VsplitIntFunctor() = default; | ||
Maybe<TensorTuple> operator()(const std::shared_ptr<one::Tensor>& input, | ||
const int32_t& indices_or_sections) const { | ||
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. |
||
int32_t ndim = input->ndim(); | ||
CHECK_OR_RETURN(ndim >= 2) | ||
<< "torch.vsplit requires a tensor with at least 2 dimension, but got a tensor with " | ||
<< ndim << " dimensions!"; | ||
CHECK_OR_RETURN(indices_or_sections > 0) << "indices_or_sections must greater than 0"; | ||
CHECK_OR_RETURN(input->dim(0) % indices_or_sections == 0) | ||
<< "torch.vsplit attempted to split along dimension " << 0 | ||
<< ", but the size of the dimension " << input->dim(0) | ||
<< " is not divisible by the split_size " << indices_or_sections << "!"; | ||
return TensorSplitInt(input, indices_or_sections, 0); | ||
} | ||
}; | ||
|
||
class VsplitVecFunctor { | ||
public: | ||
VsplitVecFunctor() = default; | ||
Maybe<TensorTuple> operator()(const std::shared_ptr<one::Tensor>& input, | ||
const std::vector<int32_t>& indices_or_sections) const { | ||
int32_t ndim = input->shape()->NumAxes(); | ||
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. consider |
||
CHECK_OR_RETURN(ndim >= 2) | ||
<< "torch.vsplit requires a tensor with at least 1 dimension, but got a tensor with " | ||
<< ndim << " dimensions!"; | ||
return TensorSplitVec(input, indices_or_sections, 0); | ||
} | ||
}; | ||
|
||
class ErfinvFunctor { | ||
public: | ||
ErfinvFunctor() { op_ = CHECK_JUST(one::OpBuilder("erfinv").Input("x").Output("y").Build()); } | ||
|
@@ -2012,6 +2143,12 @@ ONEFLOW_FUNCTION_LIBRARY(m) { | |
m.add_functor<DotFunctor>("Dot"); | ||
m.add_functor<MovedimVecFunctor>("MovedimVec"); | ||
m.add_functor<MovedimIntFunctor>("MovedimInt"); | ||
m.add_functor<TensorSplitVecFunctor>("TensorSplitVec"); | ||
m.add_functor<TensorSplitIntFunctor>("TensorSplitInt"); | ||
m.add_functor<HsplitIntFunctor>("HsplitInt"); | ||
m.add_functor<HsplitVecFunctor>("HsplitVec"); | ||
m.add_functor<VsplitIntFunctor>("VsplitInt"); | ||
m.add_functor<VsplitVecFunctor>("VsplitVec"); | ||
m.add_functor<ErfinvFunctor>("Erfinv"); | ||
m.add_functor<ErfinvInplaceFunctor>("ErfinvInplace"); | ||
m.add_functor<CumsumFunctor>("Cumsum"); | ||
|
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,54 @@ | ||
""" | ||
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 | ||
from oneflow.test_utils.automated_test_util import * | ||
import oneflow as flow | ||
import oneflow.unittest | ||
|
||
|
||
class TestHsplitVec(flow.unittest.TestCase): | ||
@autotest(check_graph=False) | ||
def test_flow_hsplit_vec(test_case): | ||
device = random_device() | ||
x = random_pytorch_tensor( | ||
ndim=4, | ||
dim1=random(3, 6), | ||
dim2=random(3, 6), | ||
dim3=random(3, 6), | ||
dim4=random(3, 6), | ||
).to(device) | ||
z = torch.hsplit(x, (1, 2)) | ||
return z[0] | ||
|
||
|
||
class TestHsplitInt(flow.unittest.TestCase): | ||
@autotest(check_graph=False) | ||
def test_flow_hsplit_int(test_case): | ||
device = random_device() | ||
x = random_pytorch_tensor( | ||
ndim=4, | ||
dim1=random(3, 6), | ||
dim2=random(3, 6), | ||
dim3=random(3, 6), | ||
dim4=random(3, 6), | ||
).to(device) | ||
split = random(1, 3).to(int) | ||
z = torch.hsplit(x, split) | ||
return z[0] | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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.
同上:https://github.com/Oneflow-Inc/oneflow/pull/7258/files#r790376430