| 131 | public: |
| 132 | virtual ~SoftmaxTest() = default; |
| 133 | virtual bool run(int precision) { |
| 134 | // testcase 0 |
| 135 | { |
| 136 | auto input = _Input({1, 4}, NCHW); |
| 137 | input->setName("input_tensor"); |
| 138 | // set input data |
| 139 | const float inpudata[] = {1.0, 2.0, 3.0, 4.0}; |
| 140 | auto inputPtr = input->writeMap<float>(); |
| 141 | memcpy(inputPtr, inpudata, 4 * sizeof(float)); |
| 142 | input->unMap(); |
| 143 | auto output = _Softmax(input); |
| 144 | const std::vector<float> expectedOutput = {0.0320586, 0.0871443, 0.236883, 0.643914}; |
| 145 | auto gotOutput = output->readMap<float>(); |
| 146 | if (!checkVector<float>(gotOutput, expectedOutput.data(), 4, 0.001)) { |
| 147 | MNN_ERROR("SoftmaxTest0 test failed!\n"); |
| 148 | return false; |
| 149 | } |
| 150 | } |
| 151 | // testcase 1 |
| 152 | { |
| 153 | auto input = _Input({2, 4}, NCHW); |
| 154 | input->setName("input_tensor"); |
| 155 | // set input data |
| 156 | const float inpudata[] = {1.0, 2.0, 3.0, 4.0, -1.0, -2.0, -3.0, -4.0}; |
| 157 | auto inputPtr = input->writeMap<float>(); |
| 158 | memcpy(inputPtr, inpudata, 8 * sizeof(float)); |
| 159 | input->unMap(); |
| 160 | auto output = _Softmax(input); |
| 161 | const std::vector<float> expectedOutput = {0.0320586, 0.0871443, 0.236883, 0.643914, |
| 162 | 0.643914, 0.236883, 0.0871443, 0.0320586}; |
| 163 | auto gotOutput = output->readMap<float>(); |
| 164 | if (!checkVector<float>(gotOutput, expectedOutput.data(), 8, 0.001)) { |
| 165 | MNN_ERROR("SoftmaxTest1 test failed!\n"); |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | // testcase 2 |
| 170 | { |
| 171 | auto input = _Input({2, 5}, NCHW); |
| 172 | input->setName("input_tensor"); |
| 173 | // set input data |
| 174 | const float inpudata[] = {1.0, 2.0, 3.0, 4.0, 5.0, -1.0, -2.0, -3.0, -4.0, -5.0}; |
| 175 | auto inputPtr = input->writeMap<float>(); |
| 176 | memcpy(inputPtr, inpudata, 10 * sizeof(float)); |
| 177 | input->unMap(); |
| 178 | auto output = _Softmax(input); |
| 179 | const std::vector<float> expectedOutput = {0.0116558, 0.0316853, 0.0861187, 0.234124, 0.636416, |
| 180 | 0.636416, 0.234124, 0.0861187, 0.0316853, 0.0116558}; |
| 181 | auto gotOutput = output->readMap<float>(); |
| 182 | if (!checkVector<float>(gotOutput, expectedOutput.data(), 10, 0.001)) { |
| 183 | MNN_ERROR("SoftmaxTest2 test failed!\n"); |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | // testcase 3 |
| 188 | { |
| 189 | auto input = _Input({2, 2}, NCHW); |
| 190 | input->setName("input_tensor"); |