-
Notifications
You must be signed in to change notification settings - Fork 4.1k
First Draft EyeLike CPU OP9 #121
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
Changes from 1 commit
Commits
Show all changes
4 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 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,78 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include "core/providers/cpu/tensor/eye_like.h" | ||
| #include "core/framework/tensorprotoutils.h" | ||
|
|
||
| using namespace ::onnxruntime::common; | ||
|
|
||
| namespace onnxruntime { | ||
|
|
||
| ONNX_CPU_OPERATOR_KERNEL( | ||
| EyeLike, | ||
| 9, | ||
| KernelDefBuilder().TypeConstraint("T1", | ||
| std::vector<MLDataType>{ | ||
| DataTypeImpl::GetTensorType<float>(), | ||
| DataTypeImpl::GetTensorType<int64_t>(), | ||
| DataTypeImpl::GetTensorType<uint64_t>(), | ||
| }) | ||
| .TypeConstraint("T2", | ||
| std::vector<MLDataType>{ | ||
| DataTypeImpl::GetTensorType<float>(), | ||
| DataTypeImpl::GetTensorType<uint64_t>(), | ||
| DataTypeImpl::GetTensorType<int64_t>(), | ||
| }), | ||
| EyeLike); | ||
|
|
||
| Status EyeLike::Compute(OpKernelContext* context) const { | ||
| const Tensor* T1 = context->Input<Tensor>(0); | ||
| ONNXRUNTIME_ENFORCE(T1 != nullptr); | ||
|
|
||
| auto output_tensor_dtype = has_dtype ? static_cast<onnx::TensorProto::DataType>(dtype_) : utils::GetTensorProtoType(*T1); | ||
| switch (output_tensor_dtype) { | ||
| case onnx::TensorProto_DataType_FLOAT: | ||
| return ComputeImpl<float>(context); | ||
| case onnx::TensorProto_DataType_INT64: | ||
| return ComputeImpl<int64_t>(context); | ||
| case onnx::TensorProto_DataType_UINT64: | ||
| return ComputeImpl<uint64_t>(context); | ||
| default: | ||
| ONNXRUNTIME_THROW("Unsupported 'dtype' value: ", output_tensor_dtype); | ||
| } | ||
| } | ||
|
|
||
| template <typename T> | ||
| Status EyeLike::ComputeImpl(OpKernelContext* context) const { | ||
| const Tensor* T1 = context->Input<Tensor>(0); | ||
| const std::vector<int64_t>& input_dims = T1->Shape().GetDims(); | ||
| if (input_dims.size() != 2) { | ||
| return Status(ONNXRUNTIME, INVALID_ARGUMENT, "EyeLike : Input tensor dimension is not 2"); | ||
| } | ||
|
|
||
| // set output tensor shape same as input tensor and set all values to zero | ||
| auto* T2 = context->Output(0, input_dims); | ||
| auto* data = T2->MutableData<T>(); | ||
| T zero_value = static_cast<T>(0); | ||
| auto out = gsl::make_span(data, T2->Shape().Size()); | ||
| std::for_each(out.begin(), out.end(), [&zero_value](T& v) { v = zero_value; }); | ||
|
askhade marked this conversation as resolved.
Outdated
|
||
|
|
||
| int64_t diag_start = 0; | ||
| int64_t diag_end = 0; | ||
| if (k_ >= 0) { | ||
| diag_start = k_; | ||
| diag_end = (input_dims[1] - k_) * (input_dims[1]); | ||
| } else { | ||
| diag_start = (-k_) * input_dims[1]; | ||
| diag_end = diag_start + (input_dims[0] + k_) * input_dims[1]; | ||
| } | ||
|
|
||
| T one_value = static_cast<T>(1); | ||
| for (auto i = diag_start; i < diag_end; i += input_dims[1] + 1) { | ||
| data[i] = one_value; | ||
| } | ||
|
|
||
| return Status::OK(); | ||
| } | ||
|
|
||
| } // 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // 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 { | ||
|
|
||
| class EyeLike final : public OpKernel { | ||
| public: | ||
| EyeLike(const OpKernelInfo& info) : OpKernel(info) { | ||
| if (!info.GetAttr("k", &k_).IsOK()) { | ||
| ONNXRUNTIME_THROW("Missing 'k' attribute value"); | ||
|
Contributor
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. Looks like 'k' is optional based on the spec in which case we don't have to throw. #Resolved |
||
| } | ||
|
|
||
| has_dtype = info.GetAttr("dtype", &dtype_).IsOK(); | ||
| } | ||
|
|
||
| Status Compute(OpKernelContext* context) const override; | ||
|
|
||
| private: | ||
| template <typename T> | ||
| Status ComputeImpl(OpKernelContext* context) const; | ||
|
|
||
| bool has_dtype; | ||
|
askhade marked this conversation as resolved.
Outdated
|
||
| int64_t dtype_; | ||
| int64_t k_; | ||
| }; | ||
|
|
||
| } //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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include "gtest/gtest.h" | ||
| #include "test/providers/provider_test_utils.h" | ||
|
|
||
| namespace onnxruntime { | ||
| namespace test { | ||
|
|
||
| TEST(EyeLikeOpTest, EyeLikeDefault) { | ||
| OpTester test("EyeLike", 9); | ||
| test.AddInput<float>("T1", {3, 2}, {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}); | ||
| test.AddOutput<float>("T2", {3, 2}, {1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f}); | ||
| test.Run(); | ||
| } | ||
|
|
||
| TEST(EyeLikeOpTest, EyeLike_DifferentDtype) { | ||
| OpTester test("EyeLike", 9); | ||
| test.AddAttribute("dtype", int64_t(7)); | ||
| test.AddInput<float>("T1", {3, 3}, {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}); | ||
| test.AddOutput<int64_t>("T2", {3, 3}, {1, 0, 0, 0, 1, 0, 0, 0, 1}); | ||
| test.Run(); | ||
| } | ||
|
|
||
| TEST(EyeLikeOpTest, EyeLike_K_EgdeCase_1) { | ||
| OpTester test("EyeLike", 9); | ||
| test.AddInput<int64_t>("T1", {3, 2}, {0, 0, 0, 0, 0, 0}); | ||
| int64_t kVal = 3; | ||
| test.AddAttribute("k", kVal); | ||
| test.AddAttribute("dtype", int64_t(7)); | ||
| test.AddOutput<int64_t>("T2", {3, 2}, {0, 0, 0, 0, 0, 0}); | ||
| test.Run(); | ||
| } | ||
|
|
||
| TEST(EyeLikeOpTest, EyeLike_K_EgdeCase_2) { | ||
| OpTester test("EyeLike", 9); | ||
| test.AddInput<int64_t>("T1", {3, 2}, {0, 0, 0, 0, 0, 0}); | ||
| int64_t kVal = -3; | ||
| test.AddAttribute("k", kVal); | ||
| test.AddOutput<int64_t>("T2", {3, 2}, {0, 0, 0, 0, 0, 0}); | ||
| test.Run(); | ||
| } | ||
|
|
||
| TEST(EyeLikeOpTest, EyeLike_UpperDiagonal) { | ||
| OpTester test("EyeLike", 9); | ||
| test.AddInput<float>("T1", {3, 4}, {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}); | ||
| int64_t kVal = 2; | ||
| test.AddAttribute("k", kVal); | ||
| test.AddOutput<float>("T2", {3, 4}, {0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f}); | ||
| test.Run(); | ||
| } | ||
|
|
||
| TEST(EyeLikeOpTest, EyeLike_UpperrDiagonal2) { | ||
| OpTester test("EyeLike", 9); | ||
| test.AddInput<float>("T1", {3, 2}, {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}); | ||
| int64_t kVal = 1; | ||
| test.AddAttribute("k", kVal); | ||
| test.AddOutput<float>("T2", {3, 2}, {0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f}); | ||
| test.Run(); | ||
| } | ||
|
|
||
| TEST(EyeLikeOpTest, EyeLike_LowerDiagonal) { | ||
| OpTester test("EyeLike", 9); | ||
| test.AddInput<float>("T1", {3, 2}, {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}); | ||
| int64_t kVal = -1; | ||
| test.AddAttribute("k", kVal); | ||
| test.AddAttribute("dtype", int64_t(1)); | ||
| test.AddOutput<float>("T2", {3, 2}, {0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f}); | ||
| test.Run(); | ||
| } | ||
|
|
||
| TEST(EyeLikeOpTest, EyeLike_LowerDiagonal2) { | ||
| OpTester test("EyeLike", 9); | ||
| test.AddInput<float>("T1", {3, 4}, {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}); | ||
| int64_t kVal = -2; | ||
| test.AddAttribute("k", kVal); | ||
| test.AddAttribute("dtype", int64_t(1)); | ||
| test.AddOutput<float>("T2", {3, 4}, {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f}); | ||
| test.Run(); | ||
| } | ||
|
|
||
| } // namespace test | ||
| } // namespace onnxruntime |
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.
this is the entire list of supported types as per ONNX spec... should I add all of them?
tensor(float16), tensor(float), tensor(double), tensor(int8), tensor(int16), tensor(int32), tensor(int64), tensor(uint8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(bool)
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.
we could add the types step by step based on requirement. Is there any models require other types?
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.
I don't know if any model actually needs this operator right now. I am adding this as part of OP9 support. Do we maintain this info somewhere? How do I check?
Uh oh!
There was an error while loading. Please reload this page.
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.
By default, we start with tensor(float) and add other types if need be.