|
| 1 | +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "fastdeploy/core/fd_tensor.h" |
| 16 | +#include "fastdeploy/function/cumprod.h" |
| 17 | +#include "glog/logging.h" |
| 18 | +#include "gtest_utils.h" |
| 19 | +#include "gtest/gtest.h" |
| 20 | +#include <array> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +namespace fastdeploy { |
| 24 | +namespace function { |
| 25 | + |
| 26 | +std::vector<float> CreateTestData() { |
| 27 | + // Shape: [2, 3, 4] |
| 28 | + std::vector<float> x_data = { |
| 29 | + 0.8428625, 0.6461913, 0.13740455, 0.11430702, 0.659926, 0.535816, |
| 30 | + 0.7429162, 0.8456049, 0.21228176, 0.29970083, 0.8621713, 0.40894133, |
| 31 | + 0.12684688, 0.1566195, 0.42884097, 0.8476526, 0.2458633, 0.669046, |
| 32 | + 0.87888306, 0.6762589, 0.666453, 0.32523027, 0.4139388, 0.8341406}; |
| 33 | + return x_data; |
| 34 | +} |
| 35 | + |
| 36 | +TEST(fastdeploy, cumprod) { |
| 37 | + CheckShape check_shape; |
| 38 | + CheckData check_data; |
| 39 | + FDTensor x, y; |
| 40 | + auto test_data = CreateTestData(); |
| 41 | + x.SetExternalData({2, 3, 4}, FDDataType::FP32, test_data.data()); |
| 42 | + |
| 43 | + std::vector<float> result = {0.842862, 0.646191, 0.137405, 0.114307, 0.659926, |
| 44 | + 0.535816, 0.742916, 0.845605, 0.212282, 0.299701, |
| 45 | + 0.862171, 0.408941, 0.106914, 0.101206, 0.058925, |
| 46 | + 0.096893, 0.162252, 0.358486, 0.652937, 0.571848, |
| 47 | + 0.141476, 0.097472, 0.356886, 0.341115}; |
| 48 | + Cumprod(x, &y, 0); |
| 49 | + check_shape(y.shape, {2, 3, 4}); |
| 50 | + check_data(reinterpret_cast<const float*>(y.Data()), result.data(), |
| 51 | + result.size()); |
| 52 | + |
| 53 | + result = {0.842862, 0.646191, 0.137405, 0.114307, 0.556227, 0.34624, |
| 54 | + 0.10208, 0.096659, 0.118077, 0.103768, 0.088011, 0.039528, |
| 55 | + 0.126847, 0.15662, 0.428841, 0.847653, 0.031187, 0.104786, |
| 56 | + 0.376901, 0.573233, 0.020785, 0.034079, 0.156014, 0.478157}; |
| 57 | + Cumprod(x, &y, 1); |
| 58 | + check_shape(y.shape, {2, 3, 4}); |
| 59 | + check_data(reinterpret_cast<const float*>(y.Data()), result.data(), |
| 60 | + result.size()); |
| 61 | + |
| 62 | + result = {0.842862, 0.54465, 0.074837, 0.008554, 0.659926, 0.353599, |
| 63 | + 0.262694, 0.222136, 0.212282, 0.063621, 0.054852, 0.022431, |
| 64 | + 0.126847, 0.019867, 0.00852, 0.007222, 0.245863, 0.164494, |
| 65 | + 0.144571, 0.097767, 0.666453, 0.216751, 0.089722, 0.07484}; |
| 66 | + Cumprod(x, &y, 2); |
| 67 | + check_shape(y.shape, {2, 3, 4}); |
| 68 | + check_data(reinterpret_cast<const float*>(y.Data()), result.data(), |
| 69 | + result.size()); |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace function |
| 73 | +} // namespace fastdeploy |
0 commit comments