1 // Tencent is pleased to support the open source community by making ncnn available.
2 //
3 // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
4 //
5 // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // https://opensource.org/licenses/BSD-3-Clause
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14 
15 #include "layer/softmax.h"
16 #include "testutil.h"
17 
test_softmax(const ncnn::Mat & a,int axis)18 static int test_softmax(const ncnn::Mat& a, int axis)
19 {
20     ncnn::ParamDict pd;
21     pd.set(0, axis); // axis
22     pd.set(1, 1);    // fixbug0
23 
24     std::vector<ncnn::Mat> weights(0);
25 
26     int ret = test_layer<ncnn::Softmax>("Softmax", pd, weights, a);
27     if (ret != 0)
28     {
29         fprintf(stderr, "test_softmax failed a.dims=%d a=(%d %d %d) axis=%d\n", a.dims, a.w, a.h, a.c, axis);
30     }
31 
32     return ret;
33 }
34 
test_softmax_0()35 static int test_softmax_0()
36 {
37     ncnn::Mat a = RandomMat(5, 7, 24);
38     ncnn::Mat b = RandomMat(7, 9, 12);
39     ncnn::Mat c = RandomMat(3, 5, 13);
40 
41     return 0
42            || test_softmax(a, 0)
43            || test_softmax(a, 1)
44            || test_softmax(a, 2)
45            || test_softmax(a, -1)
46            || test_softmax(a, -2)
47            || test_softmax(a, -3)
48 
49            || test_softmax(b, 0)
50            || test_softmax(b, 1)
51            || test_softmax(b, 2)
52            || test_softmax(b, -1)
53            || test_softmax(b, -2)
54            || test_softmax(b, -3)
55 
56            || test_softmax(c, 0)
57            || test_softmax(c, 1)
58            || test_softmax(c, 2)
59            || test_softmax(c, -1)
60            || test_softmax(c, -2)
61            || test_softmax(c, -3);
62 }
63 
test_softmax_1()64 static int test_softmax_1()
65 {
66     ncnn::Mat a = RandomMat(15, 24);
67     ncnn::Mat b = RandomMat(17, 12);
68     ncnn::Mat c = RandomMat(19, 15);
69 
70     return 0
71            || test_softmax(a, 0)
72            || test_softmax(a, 1)
73            || test_softmax(a, -1)
74            || test_softmax(a, -2)
75 
76            || test_softmax(b, 0)
77            || test_softmax(b, 1)
78            || test_softmax(b, -1)
79            || test_softmax(b, -2)
80 
81            || test_softmax(c, 0)
82            || test_softmax(c, 1)
83            || test_softmax(c, -1)
84            || test_softmax(c, -2);
85 }
86 
test_softmax_2()87 static int test_softmax_2()
88 {
89     ncnn::Mat a = RandomMat(128);
90     ncnn::Mat b = RandomMat(124);
91     ncnn::Mat c = RandomMat(127);
92 
93     return 0
94            || test_softmax(a, 0)
95            || test_softmax(a, -1)
96 
97            || test_softmax(b, 0)
98            || test_softmax(b, -1)
99 
100            || test_softmax(c, 0)
101            || test_softmax(c, -1);
102 }
103 
main()104 int main()
105 {
106     SRAND(7767517);
107 
108     return 0
109            || test_softmax_0()
110            || test_softmax_1()
111            || test_softmax_2();
112 }
113