From 0539cb8d52c7d4568d3ce541f663058b80d49156 Mon Sep 17 00:00:00 2001 From: Mateusz Bencer Date: Wed, 20 Nov 2024 16:51:12 +0100 Subject: [PATCH] added negative tests --- ...st.cc => ModelTensorMemorySharing.test.cc} | 0 .../one_op_tests/Reshape.test.cc | 89 +++++++++++++++++++ .../one_op_tests/Squeeze.test.cc | 55 ++++++++++++ 3 files changed, 144 insertions(+) rename tests/nnfw_api/src/GenModelTests/{MemorySharingModels.test.cc => ModelTensorMemorySharing.test.cc} (100%) create mode 100644 tests/nnfw_api/src/GenModelTests/one_op_tests/Reshape.test.cc create mode 100644 tests/nnfw_api/src/GenModelTests/one_op_tests/Squeeze.test.cc diff --git a/tests/nnfw_api/src/GenModelTests/MemorySharingModels.test.cc b/tests/nnfw_api/src/GenModelTests/ModelTensorMemorySharing.test.cc similarity index 100% rename from tests/nnfw_api/src/GenModelTests/MemorySharingModels.test.cc rename to tests/nnfw_api/src/GenModelTests/ModelTensorMemorySharing.test.cc diff --git a/tests/nnfw_api/src/GenModelTests/one_op_tests/Reshape.test.cc b/tests/nnfw_api/src/GenModelTests/one_op_tests/Reshape.test.cc new file mode 100644 index 00000000000..2c70ba0f283 --- /dev/null +++ b/tests/nnfw_api/src/GenModelTests/one_op_tests/Reshape.test.cc @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. 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 "GenModelTest.h" + +TEST_F(GenModelTest, OneOp_neg_Reshape_invalid_target_shape) +{ + CircleGen cgen; + const auto f32 = circle::TensorType::TensorType_FLOAT32; + + const std::vector new_shape_data{1, 5}; + const uint32_t new_shape_buf = cgen.addBuffer(new_shape_data); + const int new_shape = cgen.addTensor({{2}, f32, new_shape_buf}); + const int input = cgen.addTensor({{4}, f32}); + const int out = cgen.addTensor({{1, 5}, f32}); + + cgen.addOperatorReshape({{input, new_shape}, {out}}, &new_shape_data); + cgen.setInputsAndOutputs({input}, {out}); + + _context = std::make_unique(cgen.finish()); + _context->addTestCase(uniformTCD({{1, 2, 3, 4}}, {{1, 2, 3, 4}})); + _context->setBackends({"cpu", "gpu_cl"}); + + _context->expectFailCompile(); + + SUCCEED(); +} + +TEST_F(GenModelTest, OneOp_neg_Reshape_invalid_target_dyn_shape) +{ + CircleGen cgen; + const auto f32 = circle::TensorType::TensorType_FLOAT32; + const auto i32 = circle::TensorType::TensorType_INT32; + + const std::vector in_data{1.f, 2.f, 3.f, 4.f}; + const uint32_t input_buf = cgen.addBuffer(in_data); + const int input = cgen.addTensor({{4}, f32, input_buf}); + const int new_shape = cgen.addTensor({{2}, i32}); + const int out = cgen.addTensor({{}, f32}); // unspecified shape + + const CircleGen::Shape empty_new_shape; + cgen.addOperatorReshape({{input, new_shape}, {out}}, &empty_new_shape); + cgen.setInputsAndOutputs({new_shape}, {out}); + + _context = std::make_unique(cgen.finish()); + _context->addTestCase( + TestCaseData{}.addInput(std::vector{1, 5}).addOutput(in_data).expectFailRun()); + _context->output_sizes(0, sizeof(float) * in_data.size()); + _context->setBackends({"cpu", "gpu_cl"}); + + SUCCEED(); +} + +TEST_F(GenModelTest, OneOp_neg_Reshape_invalid_target_dyn_type) +{ + CircleGen cgen; + const auto f32 = circle::TensorType::TensorType_FLOAT32; + + const std::vector in_data{1.f, 2.f, 3.f, 4.f}; + const uint32_t input_buf = cgen.addBuffer(in_data); + const int input = cgen.addTensor({{4}, f32, input_buf}); + const int new_shape = cgen.addTensor({{2}, f32}); + const int out = cgen.addTensor({{}, f32}); // unspecified shape + + const CircleGen::Shape empty_new_shape; + cgen.addOperatorReshape({{input, new_shape}, {out}}, &empty_new_shape); + cgen.setInputsAndOutputs({new_shape}, {out}); + + _context = std::make_unique(cgen.finish()); + _context->addTestCase( + TestCaseData{}.addInput(std::vector{2, 2}).addOutput(in_data).expectFailRun()); + _context->output_sizes(0, sizeof(float) * in_data.size()); + _context->setBackends({"cpu", "gpu_cl"}); + + SUCCEED(); +} diff --git a/tests/nnfw_api/src/GenModelTests/one_op_tests/Squeeze.test.cc b/tests/nnfw_api/src/GenModelTests/one_op_tests/Squeeze.test.cc new file mode 100644 index 00000000000..cb99c1338c6 --- /dev/null +++ b/tests/nnfw_api/src/GenModelTests/one_op_tests/Squeeze.test.cc @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. 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 "GenModelTest.h" + +TEST_F(GenModelTest, OneOp_neg_Squeeze_invalid_dims) +{ + CircleGen cgen; + const std::vector squeeze_dims{0, 1}; // 1 dim here is incorrect + int input = cgen.addTensor({{1, 2, 1, 2}, circle::TensorType::TensorType_FLOAT32}); + int squeeze_out = cgen.addTensor({{2, 2}, circle::TensorType::TensorType_FLOAT32}); + + cgen.addOperatorSqueeze({{input}, {squeeze_out}}, squeeze_dims); + cgen.setInputsAndOutputs({input}, {squeeze_out}); + + _context = std::make_unique(cgen.finish()); + _context->addTestCase(uniformTCD({{1, 2, 3, 4}}, {{1, 2, 3, 4}})); + _context->setBackends({"cpu", "gpu_cl"}); + + _context->expectFailCompile(); + + SUCCEED(); +} + +TEST_F(GenModelTest, OneOp_neg_Squeeze_out_of_rank_dims) +{ + CircleGen cgen; + const std::vector squeeze_dims{0, 4}; // 4 dim here is incorrect + int input = cgen.addTensor({{1, 2, 1, 2}, circle::TensorType::TensorType_FLOAT32}); + int squeeze_out = cgen.addTensor({{2, 2}, circle::TensorType::TensorType_FLOAT32}); + + cgen.addOperatorSqueeze({{input}, {squeeze_out}}, squeeze_dims); + cgen.setInputsAndOutputs({input}, {squeeze_out}); + + _context = std::make_unique(cgen.finish()); + _context->addTestCase(uniformTCD({{1, 2, 3, 4}}, {{1, 2, 3, 4}})); + _context->setBackends({"cpu", "gpu_cl"}); + + _context->expectFailCompile(); + + SUCCEED(); +}