-
Notifications
You must be signed in to change notification settings - Fork 610
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 split shape utility #3062
Merged
Merged
Add split shape utility #3062
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. 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. | ||
|
||
#ifndef DALI_KERNELS_COMMON_SPLIT_SHAPE_H_ | ||
#define DALI_KERNELS_COMMON_SPLIT_SHAPE_H_ | ||
|
||
namespace dali { | ||
namespace kernels { | ||
|
||
/** | ||
* @brief Utility to divide a bigger shape into smaller blocks, given a desired minimum number | ||
* of blocks and a minimum practical block size. | ||
* The algorithm starts splitting from the outermost dimension until either the number of | ||
* blocks reaches the desired minimum, or until the remaining volume is under a given threshold. | ||
* @remarks The algorithm makes an effort to keep a good balance of block sizes, which might result in | ||
* a higher number of blocks than the minimum requested. | ||
* @param split_factor Output argument used to represent split factors for each dimension. | ||
* @param in_shape Input shape | ||
* @param min_nblocks Desired minimum number of blocks | ||
* @param min_sz Minimum practical block size | ||
*/ | ||
template <typename SplitFactor, typename Shape> | ||
void split_shape(SplitFactor& split_factor, const Shape& in_shape, int min_nblocks = 8, | ||
int min_sz = (16 << 10)) { | ||
int ndim = dali::size(in_shape); | ||
assert(static_cast<int>(dali::size(split_factor)) == ndim); | ||
for (int d = 0; d < ndim; d++) | ||
split_factor[d] = 1; | ||
|
||
int64_t vol = volume(in_shape); | ||
for (int d = 0, nblocks = 1; d < ndim && nblocks < min_nblocks && vol > min_sz; d++) { | ||
int n = in_shape[d]; | ||
int &b = split_factor[d]; | ||
auto remaining = div_ceil(min_nblocks, nblocks); | ||
constexpr int kThreshold = 4; | ||
// ``* kThreshold`` to keep balance of block sizes, | ||
// only dividing by ``remaining`` when the number is small. | ||
if (remaining * kThreshold < n) { | ||
b = remaining; | ||
nblocks *= b; | ||
assert(nblocks >= min_nblocks); | ||
break; | ||
} | ||
|
||
b = n; | ||
nblocks *= b; | ||
vol = div_ceil(vol, b); | ||
} | ||
} | ||
|
||
} // namespace kernels | ||
} // namespace dali | ||
|
||
#endif // DALI_KERNELS_COMMON_SPLIT_SHAPE_H_ |
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,79 @@ | ||
// Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. 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. | ||
|
||
#include <gtest/gtest.h> | ||
#include <vector> | ||
#include "dali/core/tensor_shape.h" | ||
#include "dali/kernels/common/split_shape.h" | ||
|
||
namespace dali { | ||
namespace kernels { | ||
|
||
TEST(split_shape, basic_test) { | ||
TensorShape<> sh(10, 10, 10); | ||
std::vector<int> split_factor = {1, 1, 1}; | ||
|
||
split_shape(split_factor, sh, 3, 1000); // minimum volume is equal to the input volume, no split | ||
ASSERT_EQ(split_factor[0], 1); | ||
ASSERT_EQ(split_factor[1], 1); | ||
ASSERT_EQ(split_factor[2], 1); | ||
|
||
split_shape(split_factor, sh, 10000, 1); // requested more than possible | ||
ASSERT_EQ(split_factor[0], 10); | ||
ASSERT_EQ(split_factor[1], 10); | ||
ASSERT_EQ(split_factor[2], 10); | ||
|
||
split_shape(split_factor, sh, 10, 10); // split across first dimension only | ||
ASSERT_EQ(split_factor[0], 10); | ||
ASSERT_EQ(split_factor[1], 1); | ||
ASSERT_EQ(split_factor[2], 1); | ||
|
||
split_shape(split_factor, sh, 3, 10); // split across first dimension only | ||
ASSERT_EQ(split_factor[0], 10); // Due to 3 * 4 > 10 | ||
ASSERT_EQ(split_factor[1], 1); | ||
ASSERT_EQ(split_factor[2], 1); | ||
|
||
split_shape(split_factor, sh, 2, 10); // split across first dimension only | ||
ASSERT_EQ(split_factor[0], 2); | ||
ASSERT_EQ(split_factor[1], 1); | ||
ASSERT_EQ(split_factor[2], 1); | ||
|
||
split_shape(split_factor, sh, 20, 10); // split across first and second dimensions | ||
ASSERT_EQ(split_factor[0], 10); | ||
ASSERT_EQ(split_factor[1], 2); | ||
ASSERT_EQ(split_factor[2], 1); | ||
|
||
split_shape(split_factor, sh, 16, 1); // split across first and second dimensions | ||
ASSERT_EQ(split_factor[0], 10); | ||
ASSERT_EQ(split_factor[1], 2); | ||
ASSERT_EQ(split_factor[2], 1); | ||
|
||
split_shape(split_factor, sh, 19, 1); // split across first and second dimensions | ||
ASSERT_EQ(split_factor[0], 10); | ||
ASSERT_EQ(split_factor[1], 2); // due to 2 * 4 < 10 | ||
ASSERT_EQ(split_factor[2], 1); | ||
|
||
split_shape(split_factor, sh, 24, 1); // split across first and second dimensions | ||
ASSERT_EQ(split_factor[0], 10); | ||
ASSERT_EQ(split_factor[1], 10); // due to 3 * 4 > 10 | ||
ASSERT_EQ(split_factor[2], 1); | ||
|
||
split_shape(split_factor, sh, 1000, 10); // split until the volume is small enough | ||
ASSERT_EQ(split_factor[0], 10); | ||
ASSERT_EQ(split_factor[1], 10); | ||
ASSERT_EQ(split_factor[2], 1); | ||
} | ||
|
||
} // namespace kernels | ||
} // namespace dali |
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.
If this is intended for threading, then there are several issues:
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.
My idea is that the caller will specify a suitable value for min_nblocks (a multiple of the number of threads)