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
Tensor indexing #3195
Merged
Merged
Tensor indexing #3195
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
218c1ba
CPU tensor indexing.
mzient 039fd7a
Add python wrapper for slicing.
mzient 48e3326
* Add GPU subscript operator
mzient 24e4c9c
Reduce max. non-collapsible dims to 16.
mzient 771da06
Fix output layout calculation.
mzient 6269261
Enable tensor arguments.
mzient 887cf40
Add tests. Fix ExpandDims for 0D input and nonempty new_axis_names.
mzient 6f28e43
Add cpu-only and variable batch size tests.
mzient bb15eb9
Fix behaviour with swapped ends. Fix a bug in TensorVector - type not…
mzient 0ac474e
Fix out-of-range error message. Add tests for out-of-range and incons…
mzient 847f53e
Add missing file with tests.
mzient f4146fa
Fix review issues.
mzient 1d8f677
Prevent iteration over DataNode.
mzient 86ba294
Review issues.
mzient 601935e
Add a hack to suppress DataNode being detected as an instance of Iter…
mzient d0e3369
Add check for the number of indices. Fix some review issues.
mzient 2391452
Add more tests.
mzient 981923a
Add comments.
mzient 2726dd1
Fix Python LGTM.
mzient f9eab5a
* Add message check to error tests.
mzient 6e878c8
Remove print.
mzient d93320a
Fix trailin full-range handling.
mzient a6d6e43
Add comments.
mzient 13961ae
Add subscript_dim_check to cpu_only and variable_batch_size tests.
mzient 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// 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 "dali/operators/generic/slice/subscript.h" | ||
#include "dali/kernels/common/type_erasure.h" | ||
#include "dali/kernels/slice/slice_cpu.h" | ||
|
||
namespace dali { | ||
|
||
#define INDEX_ARGS(idx) \ | ||
AddOptionalArg<int>("at_" #idx, "Position index", nullptr, true) \ | ||
.AddOptionalArg<int>("lo_" #idx, "Range start", nullptr, true) \ | ||
.AddOptionalArg<int>("hi_" #idx, "Range end", nullptr, true) \ | ||
.AddOptionalArg<int>("step_" #idx, "Range step", nullptr, true) | ||
|
||
|
||
DALI_SCHEMA(TensorSubscript) | ||
.MakeDocHidden() | ||
.DocStr(R"(Applies NumPy-like indexing to a batch of tensors.)") | ||
.NumInput(1) | ||
.NumOutput(1) | ||
.AddOptionalArg<int>("num_subscripts", | ||
"Number of subscripts supplied, including full-range - used for validation only.", nullptr) | ||
.INDEX_ARGS(0) | ||
.INDEX_ARGS(1) | ||
.INDEX_ARGS(2) | ||
.INDEX_ARGS(3) | ||
.INDEX_ARGS(4) | ||
.INDEX_ARGS(5) | ||
.INDEX_ARGS(6) | ||
.INDEX_ARGS(7) | ||
.INDEX_ARGS(8) | ||
.INDEX_ARGS(9) | ||
.INDEX_ARGS(10) | ||
.INDEX_ARGS(11) | ||
.INDEX_ARGS(12) | ||
.INDEX_ARGS(13) | ||
.INDEX_ARGS(14) | ||
.INDEX_ARGS(15) | ||
.INDEX_ARGS(16) | ||
.INDEX_ARGS(17) | ||
.INDEX_ARGS(18) | ||
.INDEX_ARGS(19) | ||
.INDEX_ARGS(20) | ||
.INDEX_ARGS(21) | ||
.INDEX_ARGS(22) | ||
.INDEX_ARGS(23) | ||
.INDEX_ARGS(24) | ||
.INDEX_ARGS(25) | ||
.INDEX_ARGS(26) | ||
.INDEX_ARGS(27) | ||
.INDEX_ARGS(28) | ||
.INDEX_ARGS(29) | ||
.INDEX_ARGS(30) | ||
.INDEX_ARGS(31); | ||
|
||
template <> | ||
template <int ndim, int element_size> | ||
void TensorSubscript<CPUBackend>::RunTyped(HostWorkspace &ws) { | ||
auto &input = ws.template InputRef<CPUBackend>(0); | ||
auto &output = ws.template OutputRef<CPUBackend>(0); | ||
int N = input.ntensor(); | ||
using T = kernels::type_of_size<element_size>; | ||
ThreadPool &tp = ws.GetThreadPool(); | ||
|
||
kernels::SliceCPU<T, T, ndim> K; | ||
TensorView<StorageCPU, const T, ndim> tv_in; | ||
TensorView<StorageCPU, T, ndim> tv_out; | ||
|
||
kernels::KernelContext ctx; | ||
for (int i = 0; i < N; i++) { | ||
tv_in.shape = simplified_in_shape_[i]; | ||
tv_in.data = static_cast<const T*>(input.raw_tensor(i)); | ||
tv_out.shape = simplified_out_shape_[i]; | ||
tv_out.data = static_cast<T*>(output.raw_mutable_tensor(i)); | ||
kernels::SliceArgs<T, ndim> args; | ||
args.anchor = simplified_anchor_[i].to_static<ndim>(); | ||
args.shape = tv_out.shape; | ||
K.Schedule(ctx, tv_out, tv_in, args, tp); | ||
} | ||
tp.RunAll(); | ||
} | ||
|
||
DALI_REGISTER_OPERATOR(TensorSubscript, TensorSubscript<CPUBackend>, CPU); | ||
|
||
DALI_SCHEMA(SubscriptDimCheck) | ||
.MakeDocHidden() | ||
.DocStr(R"(Checks that the input has at least `num_subscripts` dimensions. | ||
|
||
This operator is used internally when all indices are empty (:) and just verifieis | ||
that the input has sufficient number of dimensions and passes through the input.)") | ||
.NumInput(1) | ||
.NumOutput(1) | ||
.PassThrough({{0, 0}}) | ||
.AddArg("num_subscripts", | ||
"Number of subscripts supplied, which is the minimum required in the input.", DALI_INT32); | ||
|
||
|
||
template <typename Backend> | ||
struct SubscriptDimCheck : public Operator<Backend> { | ||
explicit SubscriptDimCheck(const OpSpec &spec) : Operator<Backend>(spec) { | ||
num_subscripts_ = spec.GetArgument<int>("num_subscripts"); | ||
} | ||
|
||
bool SetupImpl(vector<OutputDesc> &desc, const workspace_t<Backend> &ws) override { | ||
return false; | ||
} | ||
|
||
void RunImpl(workspace_t<Backend> &ws) override { | ||
auto &in = ws.template InputRef<Backend>(0); | ||
DALI_ENFORCE(num_subscripts_ <= in.sample_dim(), make_string("Too many indices (", | ||
num_subscripts_, ") for a ", in.sample_dim(), "-D tensor.")); | ||
auto &out = ws.template OutputRef<Backend>(0); | ||
out.ShareData(&in); | ||
} | ||
|
||
int num_subscripts_ = 0; | ||
}; | ||
|
||
DALI_REGISTER_OPERATOR(SubscriptDimCheck, SubscriptDimCheck<CPUBackend>, CPU); | ||
DALI_REGISTER_OPERATOR(SubscriptDimCheck, SubscriptDimCheck<GPUBackend>, GPU); | ||
|
||
} // namespace dali |
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,63 @@ | ||
// 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 <vector> | ||
#include "dali/operators/generic/slice/subscript.h" | ||
#include "dali/kernels/common/type_erasure.h" | ||
#include "dali/kernels/slice/slice_gpu.cuh" | ||
|
||
namespace dali { | ||
|
||
template <> | ||
template <int ndim, int element_size> | ||
void TensorSubscript<GPUBackend>::RunTyped(DeviceWorkspace &ws) { | ||
auto &input = ws.template InputRef<GPUBackend>(0); | ||
auto &output = ws.template OutputRef<GPUBackend>(0); | ||
int N = input.ntensor(); | ||
using T = kernels::type_of_size<element_size>; | ||
using Kernel = kernels::SliceGPU<T, T, ndim>; | ||
kmgr_.Resize<Kernel>(1, 1); | ||
|
||
struct Ctx { | ||
TensorListView<StorageGPU, const T, ndim> tmp_in; | ||
TensorListView<StorageGPU, T, ndim> tmp_out; | ||
vector<kernels::SliceArgs<T, ndim>> args; | ||
}; | ||
Ctx *ctx = any_cast<Ctx>(&ctx_); | ||
if (!ctx) { | ||
ctx_ = Ctx(); | ||
ctx = &any_cast<Ctx&>(ctx_); | ||
} | ||
|
||
ctx->tmp_in.resize(N); | ||
ctx->tmp_out.resize(N); | ||
ctx->args.resize(N); | ||
ctx->tmp_in.shape = simplified_in_shape_.to_static<ndim>(); | ||
ctx->tmp_out.shape = simplified_out_shape_.to_static<ndim>(); | ||
for (int i = 0; i < N; i++) { | ||
ctx->tmp_in.data[i] = static_cast<const T *>(input.raw_tensor(i)); | ||
ctx->tmp_out.data[i] = static_cast<T *>(output.raw_mutable_tensor(i)); | ||
ctx->args[i].shape = ctx->tmp_out.shape[i]; | ||
ctx->args[i].anchor = simplified_anchor_[i]; | ||
} | ||
|
||
kernels::KernelContext kctx; | ||
kctx.gpu.stream = ws.stream(); | ||
kmgr_.Setup<Kernel>(0, kctx, ctx->tmp_in, ctx->args); | ||
kmgr_.Run<Kernel>(0, 0, kctx, ctx->tmp_out, ctx->tmp_in, ctx->args); | ||
} | ||
|
||
DALI_REGISTER_OPERATOR(TensorSubscript, TensorSubscript<GPUBackend>, GPU); | ||
|
||
} // namespace dali |
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.
Is having one ctx for all kernels here ok?
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.
Yeah, it's completely stateless. As you can see, I'm not running setup nor providing a scratchpad.