Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ Status GatherOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const
return Status::OK();
}

bool GatherOpBuilder::HasSupportedInputsImpl(const Node& node, const OpBuilderInputParams& /*input_params*/,
bool GatherOpBuilder::HasSupportedInputsImpl(const Node& node, const OpBuilderInputParams& input_params,
const logging::Logger& logger) const {
int32_t input_type;
if (!GetType(*node.InputDefs()[0], input_type, logger))
return false;

if (input_type != ONNX_NAMESPACE::TensorProto_DataType_FLOAT &&
(input_type != ONNX_NAMESPACE::TensorProto_DataType_FLOAT16 ||
!input_params.create_mlprogram || input_params.coreml_version < 6) &&
Comment thread
edgchen1 marked this conversation as resolved.
input_type != ONNX_NAMESPACE::TensorProto_DataType_INT64) {
LOGS(logger, VERBOSE) << "[" << node.OpType()
<< "] Input type: [" << input_type
Expand Down
116 changes: 61 additions & 55 deletions onnxruntime/test/providers/cpu/tensor/gather_op_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,62 @@
#include "gtest/gtest.h"
#include "test/providers/provider_test_utils.h"
#include "test/util/include/default_providers.h"
#include "test/common/tensor_op_test_utils.h"

namespace onnxruntime {
namespace test {

// Some of the tests can't run on TensorrtExecutionProvider because of unsupported data types.
// Those tests will fallback to other EPs

TEST(GatherOpTest, Gather_axis0) {
template <typename T>
class GatherOpTest : public ::testing::Test {
};

using GatherOpTestTypes = ::testing::Types<float, MLFloat16>;
TYPED_TEST_SUITE(GatherOpTest, GatherOpTestTypes);

TYPED_TEST(GatherOpTest, Gather_axis0) {
// To test for NNAPI EP, we need the indices to be initializers
auto run_test = [](bool indices_is_initializer) {
OpTester test("Gather");
test.AddAttribute<int64_t>("axis", 0LL);
test.AddInput<float>("data", {2, 3, 4},
{0.0f, 0.1f, 0.2f, 0.3f,
1.0f, 1.1f, 1.2f, 1.3f,
2.0f, 2.1f, 2.2f, 2.3f,
10.0f, 10.1f, 10.2f, 10.3f,
11.0f, 11.1f, 11.2f, 11.3f,
12.0f, 12.1f, 12.2f, 12.3f});
test.AddInput<TypeParam>("data", {2, 3, 4},
GetTypedArray<TypeParam>({0.0f, 0.1f, 0.2f, 0.3f,
1.0f, 1.1f, 1.2f, 1.3f,
2.0f, 2.1f, 2.2f, 2.3f,
10.0f, 10.1f, 10.2f, 10.3f,
11.0f, 11.1f, 11.2f, 11.3f,
12.0f, 12.1f, 12.2f, 12.3f}));
test.AddInput<int64_t>("indices", {1}, {1LL}, indices_is_initializer);
test.AddOutput<float>("output", {1, 3, 4},
{10.0f, 10.1f, 10.2f, 10.3f,
11.0f, 11.1f, 11.2f, 11.3f,
12.0f, 12.1f, 12.2f, 12.3f});
test.AddOutput<TypeParam>("output", {1, 3, 4},
GetTypedArray<TypeParam>({10.0f, 10.1f, 10.2f, 10.3f,
11.0f, 11.1f, 11.2f, 11.3f,
12.0f, 12.1f, 12.2f, 12.3f}));
test.Run();
};

run_test(false);
run_test(true);
}

TEST(GatherOpTest, Gather_negative_axis) {
TYPED_TEST(GatherOpTest, Gather_negative_axis) {
// To test for NNAPI EP, we need the indices to be initializers
auto run_test = [](bool indices_is_initializer) {
OpTester test("Gather");
test.AddAttribute<int64_t>("axis", -3LL);
test.AddInput<float>("data", {2, 3, 4},
{0.0f, 0.1f, 0.2f, 0.3f,
1.0f, 1.1f, 1.2f, 1.3f,
2.0f, 2.1f, 2.2f, 2.3f,
10.0f, 10.1f, 10.2f, 10.3f,
11.0f, 11.1f, 11.2f, 11.3f,
12.0f, 12.1f, 12.2f, 12.3f});
test.AddInput<TypeParam>("data", {2, 3, 4},
GetTypedArray<TypeParam>({0.0f, 0.1f, 0.2f, 0.3f,
1.0f, 1.1f, 1.2f, 1.3f,
2.0f, 2.1f, 2.2f, 2.3f,
10.0f, 10.1f, 10.2f, 10.3f,
11.0f, 11.1f, 11.2f, 11.3f,
12.0f, 12.1f, 12.2f, 12.3f}));
test.AddInput<int64_t>("indices", {1}, {1LL}, indices_is_initializer);
test.AddOutput<float>("output", {1, 3, 4},
{10.0f, 10.1f, 10.2f, 10.3f,
11.0f, 11.1f, 11.2f, 11.3f,
12.0f, 12.1f, 12.2f, 12.3f});
test.AddOutput<TypeParam>("output", {1, 3, 4},
GetTypedArray<TypeParam>({10.0f, 10.1f, 10.2f, 10.3f,
11.0f, 11.1f, 11.2f, 11.3f,
12.0f, 12.1f, 12.2f, 12.3f}));
test.Run();
};

Expand Down Expand Up @@ -206,65 +214,63 @@ TEST(GatherOpTest, Gather_axis0_indices2d) {
run_test(true);
}

TEST(GatherOpTest, Gather_axis1_indices2d) {
TYPED_TEST(GatherOpTest, Gather_axis1_indices2d) {
// To test for NNAPI EP, we need the indices to be initializers
auto run_test = [](bool indices_is_initializer) {
OpTester test("Gather");
test.AddAttribute<int64_t>("axis", 1LL);
test.AddInput<float>("data", {3, 3},
{0.0f, 0.1f, 0.2f,
1.0f, 1.1f, 1.2f,
2.0f, 2.1f, 2.2f});
test.AddInput<TypeParam>("data", {3, 3},
GetTypedArray<TypeParam>({0.0f, 0.1f, 0.2f,
1.0f, 1.1f, 1.2f,
2.0f, 2.1f, 2.2f}));
test.AddInput<int64_t>("indices", {2LL, 2LL},
{1LL, 0LL,
2LL, 1LL},
indices_is_initializer);
test.AddOutput<float>("output", {3, 2, 2},
{0.1f, 0.0f, 0.2f, 0.1f,
1.1f, 1.0f, 1.2f, 1.1f,
2.1f, 2.0f, 2.2f, 2.1f});
test.AddOutput<TypeParam>("output", {3, 2, 2},
GetTypedArray<TypeParam>({0.1f, 0.0f, 0.2f, 0.1f,
1.1f, 1.0f, 1.2f, 1.1f,
2.1f, 2.0f, 2.2f, 2.1f}));
test.Run();
};

run_test(false);
run_test(true);
}

TEST(GatherOpTest, Gather_axis0_indicesInt32) {
TYPED_TEST(GatherOpTest, Gather_axis0_indicesInt32) {
// NNAPI EP only supports float input data for now,
// the following two test cases cover int32_t indices with float input other than int64_t type for Nnapi
OpTester test("Gather");
test.AddAttribute<int64_t>("axis", 0LL);
test.AddInput<float>("data", {2, 3, 4},
{0.0f, 0.1f, 0.2f, 0.3f,
1.0f, 1.1f, 1.2f, 1.3f,
2.0f, 2.1f, 2.2f, 2.3f,
10.0f, 10.1f, 10.2f, 10.3f,
11.0f, 11.1f, 11.2f, 11.3f,
12.0f, 12.1f, 12.2f, 12.3f});
test.AddInput<TypeParam>("data", {2, 3, 4},
GetTypedArray<TypeParam>({0.0f, 0.1f, 0.2f, 0.3f,
1.0f, 1.1f, 1.2f, 1.3f,
2.0f, 2.1f, 2.2f, 2.3f,
10.0f, 10.1f, 10.2f, 10.3f,
11.0f, 11.1f, 11.2f, 11.3f,
12.0f, 12.1f, 12.2f, 12.3f}));
test.AddInput<int32_t>("indices", {1}, {1});
test.AddOutput<float>("output", {1, 3, 4},
{10.0f, 10.1f, 10.2f, 10.3f,
11.0f, 11.1f, 11.2f, 11.3f,
12.0f, 12.1f, 12.2f, 12.3f});

test.AddOutput<TypeParam>("output", {1, 3, 4},
GetTypedArray<TypeParam>({10.0f, 10.1f, 10.2f, 10.3f,
11.0f, 11.1f, 11.2f, 11.3f,
12.0f, 12.1f, 12.2f, 12.3f}));
test.Run();
}

TEST(GatherOpTest, Gather_axis0_indices2dInt32) {
TYPED_TEST(GatherOpTest, Gather_axis0_indices2dInt32) {
OpTester test("Gather");
test.AddAttribute<int64_t>("axis", 0LL);
test.AddInput<float>("data", {3, 3},
{0.0f, 0.1f, 0.2f,
1.0f, 1.1f, 1.2f,
2.0f, 2.1f, 2.2f});
test.AddInput<TypeParam>("data", {3, 3},
GetTypedArray<TypeParam>({0.0f, 0.1f, 0.2f,
1.0f, 1.1f, 1.2f,
2.0f, 2.1f, 2.2f}));
test.AddInput<int32_t>("indices", {2, 2},
{1, 0,
2, 1});
test.AddOutput<float>("output", {2, 2, 3},
{1.0f, 1.1f, 1.2f, 0.0f, 0.1f, 0.2f,
2.0f, 2.1f, 2.2f, 1.0f, 1.1f, 1.2f});

test.AddOutput<TypeParam>("output", {2, 2, 3},
GetTypedArray<TypeParam>({1.0f, 1.1f, 1.2f, 0.0f, 0.1f, 0.2f,
2.0f, 2.1f, 2.2f, 1.0f, 1.1f, 1.2f}));
test.Run();
}

Expand Down
Loading