xref: /aosp_15_r20/external/armnn/delegate/test/CastTest.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2021, 2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "CastTestHelper.hpp"
7 
8 #include <armnn_delegate.hpp>
9 
10 #include <flatbuffers/flatbuffers.h>
11 #include <schema_generated.h>
12 
13 #include <doctest/doctest.h>
14 
15 namespace armnnDelegate
16 {
17 
CastUint8ToFp32Test(std::vector<armnn::BackendId> & backends)18 void CastUint8ToFp32Test(std::vector<armnn::BackendId>& backends)
19 {
20     std::vector<int32_t> inputShape  {1, 3, 2, 3};
21 
22     std::vector<uint8_t> inputValues { 1, 3, 1, 3, 1, 3, 1, 3, 1,
23                                         3, 1, 3, 1, 2, 1, 3, 1, 3 };
24 
25     std::vector<float> expectedOutputValues { 1.0f, 3.0f, 1.0f, 3.0f, 1.0f, 3.0f, 1.0f, 3.0f, 1.0f,
26                                               3.0f, 1.0f, 3.0f, 1.0f, 2.0f, 1.0f, 3.0f, 1.0f, 3.0f };
27 
28     CastTest<uint8_t, float>(::tflite::TensorType_UINT8,
29                              ::tflite::TensorType_FLOAT32,
30                              backends,
31                              inputShape,
32                              inputValues,
33                              expectedOutputValues);
34 }
35 
CastInt32ToFp32Test(std::vector<armnn::BackendId> & backends)36 void CastInt32ToFp32Test(std::vector<armnn::BackendId>& backends)
37 {
38     std::vector<int32_t> inputShape  {1, 3, 2, 3};
39 
40     std::vector<int32_t> inputValues { -1, -3, -1, -3, -1, -3, -1, -3, 1,
41                                        3, 1, 3, 1, 2, 1, 3, 1, 3 };
42 
43     std::vector<float> expectedOutputValues { -1.0f, -3.0f, -1.0f, -3.0f, -1.0f, -3.0f, -1.0f, -3.0f, 1.0f,
44                                               3.0f, 1.0f, 3.0f, 1.0f, 2.0f, 1.0f, 3.0f, 1.0f, 3.0f };
45 
46     CastTest<int32_t, float>(::tflite::TensorType_INT32,
47                              ::tflite::TensorType_FLOAT32,
48                              backends,
49                              inputShape,
50                              inputValues,
51                              expectedOutputValues);
52 }
53 
54 // CAST Test Suite
55 TEST_SUITE("CAST_CpuRefTests")
56 {
57 
58 TEST_CASE ("CAST_UINT8_TO_FP32_CpuRef_Test")
59 {
60     std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef};
61     CastUint8ToFp32Test(backends);
62 }
63 
64 TEST_CASE ("CAST_INT32_TO_FP32_CpuRef_Test")
65 {
66     std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef};
67     CastInt32ToFp32Test(backends);
68 }
69 
70 }
71 
72 TEST_SUITE("CAST_CpuAccTests")
73 {
74 
75 TEST_CASE ("CAST_INT32_TO_FP32_CpuAcc_Test")
76 {
77     std::vector<armnn::BackendId> backends = {armnn::Compute::CpuAcc};
78     CastInt32ToFp32Test(backends);
79 }
80 
81 }
82 
83 TEST_SUITE("CAST_GpuAccTests")
84 {
85 
86 TEST_CASE ("CAST_INT32_TO_FP32_GpuAcc_Test")
87 {
88     std::vector<armnn::BackendId> backends = {armnn::Compute::GpuAcc};
89     CastInt32ToFp32Test(backends);
90 }
91 
92 }
93 // End of CAST Test Suite
94 
95 } // namespace armnnDelegate