-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Rashuai/gathernd op #170
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
Merged
Merged
Rashuai/gathernd op #170
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
561f62d
define gather_nd op
RandyShuai f3e3e36
add test cases
RandyShuai 75f6673
add test file
RandyShuai 32cda8c
refactor the code and doc
RandyShuai e8a4f52
add test cases
RandyShuai 80ede4a
fix win compile err
RandyShuai 5421bab
fix win compile err
RandyShuai ecf5bb1
adjust indent
RandyShuai 8823a5e
make constructor explicit
RandyShuai c64d1d6
add coment
RandyShuai fbcee5a
remove templates
RandyShuai 99c2d2e
remove wrong def
RandyShuai 9220de9
resolve conflict
RandyShuai 513c063
migrate macros
RandyShuai 4cff030
fix an issue in shape inference
RandyShuai 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 hidden or 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 hidden or 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,114 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include "contrib_ops/cpu/gather_nd.h" | ||
|
|
||
| namespace onnxruntime { | ||
| namespace contrib { | ||
|
|
||
| ONNX_OPERATOR_KERNEL_EX( | ||
| GatherND, | ||
| kMSDomain, | ||
| 1, | ||
| kCpuExecutionProvider, | ||
| KernelDefBuilder() | ||
| .TypeConstraint("T", DataTypeImpl::AllTensorTypes()) | ||
| .TypeConstraint("Tind", {DataTypeImpl::GetTensorType<int32_t>(),DataTypeImpl::GetTensorType<int64_t>()}), | ||
| GatherND); | ||
|
|
||
| template<typename Tind> | ||
| Status GatherNDBase::PrepareForCompute(OpKernelContext* context, Prepare& p) const { | ||
|
|
||
| auto input_tensor = context->Input<Tensor>(0); | ||
| auto indice_tensor = context->Input<Tensor>(1); | ||
| ORT_ENFORCE(input_tensor != nullptr); | ||
| ORT_ENFORCE(indice_tensor != nullptr); | ||
|
|
||
| auto input_shape = input_tensor->Shape(); | ||
| auto indice_shape = indice_tensor->Shape(); | ||
| if (indice_shape.NumDimensions() == 0) { | ||
| return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, | ||
| "indices tensor must has rank larger than 0"); | ||
| } | ||
|
|
||
| auto last_indice_dimension = indice_shape[indice_shape.NumDimensions() - 1]; | ||
| if (last_indice_dimension > static_cast<int64_t>(input_shape.NumDimensions())) { | ||
| return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, | ||
| "last dimension of indices must not be larger than rank of input tensor"); | ||
| } | ||
|
|
||
| std::vector<int64_t> shape(indice_shape.GetDims().begin(), | ||
| indice_shape.GetDims().end() - 1); | ||
| shape.insert(shape.end(), | ||
| input_shape.GetDims().begin() + last_indice_dimension, | ||
| input_shape.GetDims().end()); | ||
| auto output_tensor = context->Output(0,TensorShape(shape)); | ||
| std::vector<int64_t> element_counts(last_indice_dimension, 0LL); // Number of elements for each input dimension | ||
|
|
||
| #pragma omp parallel for | ||
| for (int64_t i = 0; i < last_indice_dimension; ++i) { | ||
| element_counts[i] = input_shape.SizeFromDimension(i + 1); | ||
| } | ||
|
|
||
| int64_t err_indice = 0; | ||
| p.element_bytes = input_tensor->DataType()->Size(); | ||
| p.element_to_copy = input_shape.SizeFromDimension(last_indice_dimension); | ||
| p.bytes_to_copy = p.element_bytes * p.element_to_copy; | ||
| auto indice_offset = static_cast<const Tind*>(context->Input<Tensor>(1)->DataRaw()); | ||
| auto offset_count = indice_shape.Size() / last_indice_dimension; // Times to copy | ||
| p.element_offsets.assign(offset_count, 0LL); | ||
|
|
||
| if (input_tensor->DataType() == DataTypeImpl::GetType<std::string>()) { | ||
| p.input_str_base = static_cast<const std::string*>(input_tensor->DataRaw()); | ||
| p.output_str_base = static_cast<std::string*>(output_tensor->MutableDataRaw()); | ||
| } else { | ||
| p.input_base = static_cast<const uint8_t*>(context->Input<Tensor>(0)->DataRaw()); | ||
| p.output_base = static_cast<uint8_t*>(output_tensor->MutableDataRaw()); | ||
| } | ||
|
|
||
| #pragma omp parallel for | ||
| for (int64_t i = 0; i < offset_count; ++i) { | ||
| for (int64_t j = 0; j < last_indice_dimension; ++j) { | ||
| auto indice = *(indice_offset + i * last_indice_dimension + j); | ||
| if (indice < 0 || indice >= input_shape[j]) { | ||
| err_indice = indice; | ||
| } | ||
| p.element_offsets[i] += indice * element_counts[j]; | ||
| } | ||
| } | ||
| return err_indice == 0 ? Status::OK() : | ||
| ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "invalid indice found, indice = ", err_indice); | ||
| } | ||
|
|
||
| template Status GatherNDBase::PrepareForCompute<int32_t>(OpKernelContext*, Prepare&) const; | ||
| template Status GatherNDBase::PrepareForCompute<int64_t>(OpKernelContext*, Prepare&) const; | ||
|
|
||
| Status GatherND::Compute(OpKernelContext* context) const { | ||
| Prepare p; | ||
| ORT_RETURN_IF_ERROR(context->Input<Tensor>(1)->DataType() == DataTypeImpl::GetType<int32_t>() ? | ||
| PrepareForCompute<int32_t>(context, p) : PrepareForCompute<int64_t>(context, p)); | ||
| return nullptr == p.input_str_base ? GatherNumber(p) : GatherString(p); | ||
| } | ||
|
|
||
| Status GatherND::GatherNumber(const Prepare& p) const { | ||
| #pragma omp parallel for | ||
| for (int64_t i = 0; i < static_cast<int64_t>(p.element_offsets.size()); ++i) { | ||
| memcpy(p.output_base + i * p.bytes_to_copy, | ||
| p.input_base + p.element_offsets[i] * p.element_bytes, | ||
| p.bytes_to_copy); | ||
| } | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status GatherND::GatherString(const Prepare& p) const { | ||
| #pragma omp parallel for | ||
| for (int64_t i = 0; i < static_cast<int64_t>(p.element_offsets.size()); ++i) { | ||
| for (int64_t j = 0; j < static_cast<int64_t>(p.element_to_copy); ++j) { | ||
| p.output_str_base[i * p.element_to_copy + j] = p.input_str_base[p.element_offsets[i] + j]; | ||
| } | ||
| } | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or 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,49 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "core/common/common.h" | ||
| #include "core/framework/op_kernel.h" | ||
|
|
||
| namespace onnxruntime { | ||
| namespace contrib { | ||
|
|
||
| class GatherNDBase | ||
| { | ||
| protected: | ||
| struct Prepare { | ||
| const uint8_t* input_base; | ||
| const std::string* input_str_base; | ||
| uint8_t* output_base; | ||
| std::string* output_str_base; | ||
| uint64_t bytes_to_copy; | ||
| uint64_t element_bytes; | ||
| uint64_t element_to_copy; | ||
| std::vector<uint64_t> element_offsets; | ||
|
|
||
| Prepare(): input_base (nullptr), | ||
| input_str_base (nullptr), | ||
| output_base (nullptr), | ||
| output_str_base (nullptr), | ||
| bytes_to_copy (0), | ||
| element_bytes (0), | ||
| element_to_copy (0), | ||
| element_offsets (0) {} | ||
| }; // struct Prepare | ||
|
|
||
| template<typename Tind> | ||
| Status PrepareForCompute(OpKernelContext* context, Prepare& p) const; | ||
| }; // class GatherNDBase | ||
|
|
||
| class GatherND final : public OpKernel, protected GatherNDBase { | ||
| public: | ||
| explicit GatherND(const OpKernelInfo& info) : OpKernel(info) {} | ||
| Status Compute(OpKernelContext* context) const override; | ||
| private: | ||
| Status GatherNumber(const Prepare& p) const; | ||
| Status GatherString(const Prepare& p) const; | ||
| }; | ||
|
|
||
| } // namespace contrib | ||
| } // namespace onnxruntime |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.