[Functions] Add quantile function (#700)

* Add sort function

* Add isfinite function

* upgrade isinf isnan

* Add Scalar to FDTensor

* Add floor, ceil function

* add cast functions

* Update out_tmp

* Update quantile

* add gather scatter along axis

* finish quantile function

* Add quantile unittest

* refresh code style for test source code

* Add comments

* Add full function

* Add scalar to fd tensor

* Add full unittest

* Add functions headers

* move fdtensor operators to fastdeploy namespace
This commit is contained in:
Jack Zhou
2022-11-28 09:51:40 +08:00
committed by GitHub
parent 4e74ac06fb
commit 129dda7809
37 changed files with 1567 additions and 75 deletions

View File

@@ -0,0 +1,52 @@
// Copyright (c) 2022 PaddlePaddle Authors. 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 "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/function/cast.h"
#include "glog/logging.h"
#include "gtest_utils.h"
#include "gtest/gtest.h"
#include <array>
#include <vector>
namespace fastdeploy {
namespace function {
std::vector<float> CreateTestData() {
// Shape: [2, 3, 4]
std::vector<float> x_data = {
1.8428625, 0.6461913, 0.13740455, 0.11430702, 0.659926, 0.535816,
0.7429162, 0.8456049, -1.21228176, 0.29970083, 0.8621713, 0.40894133,
0.12684688, 2.1566195, -9.42884097, 20.8476526, 0.2458633, 0.669046,
0.87888306, 0.6762589, 0.666453, 0.32523027, 0.4139388, 0.8341406};
return x_data;
}
TEST(fastdeploy, cast) {
CheckShape check_shape;
CheckData check_data;
FDTensor x, y;
auto test_data = CreateTestData();
x.SetExternalData({2, 3, 4}, FDDataType::FP32, test_data.data());
Cast(x, &y, FDDataType::INT32);
std::vector<int> result = {1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0,
0, 2, -9, 20, 0, 0, 0, 0, 0, 0, 0, 0};
check_shape(y.shape, {2, 3, 4});
check_data(reinterpret_cast<const int*>(y.Data()), result.data(),
result.size());
}
} // namespace function
} // namespace fastdeploy

View File

@@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <array>
#include <vector>
#include "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/function/concat.h"
#include "glog/logging.h"
#include "gtest/gtest.h"
#include "gtest_utils.h"
#include "gtest/gtest.h"
#include <array>
#include <vector>
namespace fastdeploy {
namespace function {

View File

@@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/core/float16.h"
#include "gtest_utils.h"
#include "gtest/gtest.h"
#include <array>
#include <cstring>
#include <vector>
#include "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/core/float16.h"
#include "gtest/gtest.h"
#include "gtest_utils.h"
namespace fastdeploy {
@@ -113,7 +113,7 @@ TEST(float16, comparison_cpu) {
TEST(float16, floating) {
// compile time assert.
FDASSERT(std::is_floating_point<float16>::value,
"The float16 support in CPU failed.")
"The float16 support in CPU failed.");
}
TEST(float16, print) {

View File

@@ -0,0 +1,50 @@
// Copyright (c) 2022 PaddlePaddle Authors. 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 "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/function/full.h"
#include "glog/logging.h"
#include "gtest_utils.h"
#include "gtest/gtest.h"
#include <array>
#include <vector>
namespace fastdeploy {
namespace function {
TEST(fastdeploy, full) {
CheckShape check_shape;
CheckData check_data;
FDTensor y;
Full(1, {2, 3, 4}, &y);
std::vector<float> result(24, 1);
check_shape(y.Shape(), {2, 3, 4});
check_data(reinterpret_cast<float*>(y.Data()),
reinterpret_cast<float*>(result.data()), result.size());
}
TEST(fastdeploy, full_like) {
CheckShape check_shape;
CheckData check_data;
FDTensor x, y;
x.Allocate({3, 4}, FDDataType::FP32);
FullLike(x, 0, &y, FDDataType::INT32);
std::vector<int> result(12, 0);
check_shape(y.Shape(), {3, 4});
check_data(reinterpret_cast<int*>(y.Data()),
reinterpret_cast<int*>(result.data()), result.size());
}
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,74 @@
// Copyright (c) 2022 PaddlePaddle Authors. 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 "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/function/gather_scatter_along_axis.h"
#include "glog/logging.h"
#include "gtest_utils.h"
#include "gtest/gtest.h"
#include <array>
#include <vector>
namespace fastdeploy {
namespace function {
std::vector<float> CreateTestData() {
// Shape: [2, 3, 4]
std::vector<float> x_data = {
1.8428625, 0.6461913, 0.13740455, 0.11430702, 0.659926, 0.535816,
0.7429162, 0.8456049, -1.21228176, 0.29970083, 0.8621713, 0.40894133,
0.12684688, 2.1566195, -9.42884097, 20.8476526, 0.2458633, 0.669046,
0.87888306, 0.6762589, 0.666453, 0.32523027, 0.4139388, 0.8341406};
return x_data;
}
TEST(fastdeploy, gather) {
CheckShape check_shape;
CheckData check_data;
FDTensor x, y;
auto test_data = CreateTestData();
x.SetExternalData({2, 3, 4}, FDDataType::FP32, test_data.data());
FDTensor index;
index.Resize({1, 1, 1}, FDDataType::INT32);
reinterpret_cast<int*>(index.Data())[0] = 0;
GatherAlongAxis(x, index, &y, 0);
std::vector<float> result = {1.842862, 0.646191, 0.137405, 0.114307,
0.659926, 0.535816, 0.742916, 0.845605,
-1.212282, 0.299701, 0.862171, 0.408941};
check_shape(y.shape, {1, 3, 4});
check_data(reinterpret_cast<const float*>(y.Data()), result.data(),
result.size());
reinterpret_cast<int*>(index.Data())[0] = 1;
GatherAlongAxis(x, index, &y, 1);
result = {0.659926, 0.535816, 0.742916, 0.845605,
0.245863, 0.669046, 0.878883, 0.676259};
check_shape(y.shape, {2, 1, 4});
check_data(reinterpret_cast<const float*>(y.Data()), result.data(),
result.size());
index.Resize({1, 1, 2});
reinterpret_cast<int*>(index.Data())[0] = 0;
reinterpret_cast<int*>(index.Data())[1] = 2;
GatherAlongAxis(x, index, &y, 2);
result = {1.842862, 0.137405, 0.659926, 0.742916, -1.212282, 0.862171,
0.126847, -9.428841, 0.245863, 0.878883, 0.666453, 0.413939};
check_shape(y.shape, {2, 3, 2});
check_data(reinterpret_cast<const float*>(y.Data()), result.data(),
result.size());
}
} // namespace function
} // namespace fastdeploy

View File

@@ -0,0 +1,79 @@
// Copyright (c) 2022 PaddlePaddle Authors. 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 "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/function/isfinite.h"
#include "glog/logging.h"
#include "gtest_utils.h"
#include "gtest/gtest.h"
#include <array>
#include <cmath>
#include <vector>
namespace fastdeploy {
namespace function {
std::vector<float> CreateTestData() {
// Shape: [2, 3]
std::vector<float> x_data = {0.8428625, NAN, INFINITY,
0.11430702, 0.659926, 0.535816};
return x_data;
}
TEST(fastdeploy, finite) {
CheckShape check_shape;
CheckData check_data;
FDTensor x, y;
auto test_data = CreateTestData();
x.SetExternalData({2, 3}, FDDataType::FP32, test_data.data());
std::array<bool, 6> result = {false, true, false, false, false, false};
IsNan(x, &y);
check_shape(y.shape, {2, 3});
check_data(reinterpret_cast<const bool*>(y.Data()), result.data(),
result.size());
std::vector<int> int_result = {0, 1, 0, 0, 0, 0};
IsNan(x, &y, FDDataType::INT32);
check_shape(y.shape, {2, 3});
check_data(reinterpret_cast<const int*>(y.Data()), int_result.data(),
int_result.size());
result = {false, false, true, false, false, false};
IsInf(x, &y);
check_shape(y.shape, {2, 3});
check_data(reinterpret_cast<const bool*>(y.Data()), result.data(),
result.size());
int_result = {0, 0, 1, 0, 0, 0};
IsInf(x, &y, FDDataType::INT32);
check_shape(y.shape, {2, 3});
check_data(reinterpret_cast<const int*>(y.Data()), int_result.data(),
int_result.size());
result = {true, false, false, true, true, true};
IsFinite(x, &y);
check_shape(y.shape, {2, 3});
check_data(reinterpret_cast<const bool*>(y.Data()), result.data(),
result.size());
int_result = {1, 0, 0, 1, 1, 1};
IsFinite(x, &y, FDDataType::INT32);
check_shape(y.shape, {2, 3});
check_data(reinterpret_cast<const int*>(y.Data()), int_result.data(),
int_result.size());
}
} // namespace function
} // namespace fastdeploy

View File

@@ -71,6 +71,22 @@ TEST(fastdeploy, exp_sqrt_round_log) {
check_data(reinterpret_cast<const float*>(y.Data()), round_result.data(),
round_result.size());
Ceil(x, &y);
std::vector<float> ceil_result = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
check_shape(y.shape, {2, 3, 4});
check_data(reinterpret_cast<const float*>(y.Data()), ceil_result.data(),
ceil_result.size());
Floor(x, &y);
std::vector<float> floor_result = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
check_shape(y.shape, {2, 3, 4});
check_data(reinterpret_cast<const float*>(y.Data()), floor_result.data(),
floor_result.size());
// Test Log function
Log(x, &y);
std::vector<float> log_result = {

View File

@@ -12,14 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <numeric>
#include <vector>
#include "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/function/pad.h"
#include <numeric>
#include <vector>
#include "glog/logging.h"
#include "gtest/gtest.h"
#include "gtest_utils.h"
#include "gtest/gtest.h"
namespace fastdeploy {
namespace function {
@@ -29,12 +29,10 @@ TEST(fastdeploy, pad_2d) {
CheckData check_data;
CheckType check_type;
std::vector<float> inputs = {2, 4, 3,
7, 1, 5};
std::vector<float> expected_result = {2.2, 2.2, 2.2, 2.2, 2.2,
2.2, 2, 4, 3, 2.2,
2.2, 7, 1, 5, 2.2,
2.2, 2.2, 2.2, 2.2, 2.2};
std::vector<float> inputs = {2, 4, 3, 7, 1, 5};
std::vector<float> expected_result = {2.2, 2.2, 2.2, 2.2, 2.2, 2.2, 2,
4, 3, 2.2, 2.2, 7, 1, 5,
2.2, 2.2, 2.2, 2.2, 2.2, 2.2};
input.SetExternalData({2, 3}, FDDataType::FP32, inputs.data());
Pad(input, &output, {1, 1, 1, 1}, 2.2);
@@ -50,12 +48,9 @@ TEST(fastdeploy, pad_2d_int32_t) {
CheckData check_data;
CheckType check_type;
std::vector<int32_t> inputs = {2, 4, 3,
7, 1, 5};
std::vector<int32_t> expected_result = {2, 2, 2, 2, 2,
2, 2, 4, 3, 2,
2, 7, 1, 5, 2,
2, 2, 2, 2, 2};
std::vector<int32_t> inputs = {2, 4, 3, 7, 1, 5};
std::vector<int32_t> expected_result = {2, 2, 2, 2, 2, 2, 2, 4, 3, 2,
2, 7, 1, 5, 2, 2, 2, 2, 2, 2};
input.SetExternalData({2, 3}, FDDataType::INT32, inputs.data());
Pad(input, &output, {1, 1, 1, 1}, 2.2);

View File

@@ -0,0 +1,66 @@
// Copyright (c) 2022 PaddlePaddle Authors. 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 "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/function/quantile.h"
#include "glog/logging.h"
#include "gtest_utils.h"
#include "gtest/gtest.h"
#include <array>
#include <vector>
namespace fastdeploy {
namespace function {
std::vector<float> CreateTestData() {
// Shape: [2, 3, 4]
std::vector<float> x_data = {
1.8428625, 0.6461913, 0.13740455, 0.11430702, 0.659926, 0.535816,
0.7429162, 0.8456049, -1.21228176, 0.29970083, 0.8621713, 0.40894133,
0.12684688, 2.1566195, -9.42884097, 20.8476526, 0.2458633, 0.669046,
0.87888306, 0.6762589, 0.666453, 0.32523027, 0.4139388, 0.8341406};
return x_data;
}
TEST(fastdeploy, quantile) {
CheckShape check_shape;
CheckData check_data;
FDTensor x, y;
auto test_data = CreateTestData();
x.SetExternalData({2, 3, 4}, FDDataType::FP32, test_data.data());
std::vector<float> result = {1.834282, 2.149067, 0.089573, 20.743986,
0.657856, 0.66838, 0.878203, 0.844758,
0.657059, 0.325103, 0.85993, 0.832015};
Quantile(x, {0.995}, {0}, &y);
check_shape(y.shape, {3, 4});
check_data(reinterpret_cast<const float*>(y.Data()), result.data(),
result.size());
result = {1.831033, 0.645088, 0.860979, 0.841238,
0.662247, 2.141744, 0.874234, 20.647517};
Quantile(x, {0.995}, {1}, &y);
check_shape(y.shape, {2, 4});
check_data(reinterpret_cast<const float*>(y.Data()), result.data(),
result.size());
result = {1.824912, 0.844065, 0.855373, 20.567287, 0.875844, 0.831625};
Quantile(x, {0.995}, {2}, &y);
check_shape(y.shape, {2, 3});
check_data(reinterpret_cast<const float*>(y.Data()), result.data(),
result.size());
}
} // namespace function
} // namespace fastdeploy

View File

@@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <array>
#include <vector>
#include "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/function/reduce.h"
#include "glog/logging.h"
#include "gtest/gtest.h"
#include "gtest_utils.h"
#include "gtest/gtest.h"
#include <array>
#include <vector>
namespace fastdeploy {
namespace function {

View File

@@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <vector>
#include "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/function/softmax.h"
#include "glog/logging.h"
#include "gtest/gtest.h"
#include "gtest_utils.h"
#include "gtest/gtest.h"
#include <vector>
namespace fastdeploy {
namespace function {

109
tests/function/test_sort.cc Normal file
View File

@@ -0,0 +1,109 @@
// Copyright (c) 2022 PaddlePaddle Authors. 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 "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/function/sort.h"
#include "glog/logging.h"
#include "gtest_utils.h"
#include "gtest/gtest.h"
#include <array>
#include <vector>
namespace fastdeploy {
namespace function {
std::vector<float> CreateTestData() {
// Shape: [2, 3, 4]
std::vector<float> x_data = {
0.8428625, 0.6461913, 0.13740455, 0.11430702, 0.659926, 0.535816,
0.7429162, 0.8456049, 0.21228176, 0.29970083, 0.8621713, 0.40894133,
0.12684688, 0.1566195, 0.42884097, 0.8476526, 0.2458633, 0.669046,
0.87888306, 0.6762589, 0.666453, 0.32523027, 0.4139388, 0.8341406};
return x_data;
}
TEST(fastdeploy, sort_dim0) {
CheckShape check_shape;
CheckData check_data;
FDTensor x, out, indices;
auto test_data = CreateTestData();
x.SetExternalData({2, 3, 4}, FDDataType::FP32, test_data.data());
Sort(x, &out, &indices, 0);
std::vector<float> out_result = {
0.126847, 0.15662, 0.137405, 0.114307, 0.245863, 0.535816,
0.742916, 0.676259, 0.212282, 0.299701, 0.413939, 0.408941,
0.842862, 0.646191, 0.428841, 0.847653, 0.659926, 0.669046,
0.878883, 0.845605, 0.666453, 0.32523, 0.862171, 0.834141};
std::vector<int64_t> indices_result = {1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1};
check_shape(out.shape, {2, 3, 4});
check_data(reinterpret_cast<const float*>(out.Data()), out_result.data(),
out_result.size());
check_shape(indices.shape, {2, 3, 4});
check_data(reinterpret_cast<const int64_t*>(indices.Data()),
indices_result.data(), indices_result.size());
}
TEST(fastdeploy, sort_dim1) {
CheckShape check_shape;
CheckData check_data;
FDTensor x, out, indices;
auto test_data = CreateTestData();
x.SetExternalData({2, 3, 4}, FDDataType::FP32, test_data.data());
Sort(x, &out, &indices, 1);
std::vector<float> out_result = {
0.212282, 0.299701, 0.137405, 0.114307, 0.659926, 0.535816,
0.742916, 0.408941, 0.842862, 0.646191, 0.862171, 0.845605,
0.126847, 0.15662, 0.413939, 0.676259, 0.245863, 0.32523,
0.428841, 0.834141, 0.666453, 0.669046, 0.878883, 0.847653};
std::vector<int64_t> indices_result = {2, 2, 0, 0, 1, 1, 1, 2, 0, 0, 2, 1,
0, 0, 2, 1, 1, 2, 0, 2, 2, 1, 1, 0};
check_shape(out.shape, {2, 3, 4});
check_data(reinterpret_cast<const float*>(out.Data()), out_result.data(),
out_result.size());
check_shape(indices.shape, {2, 3, 4});
check_data(reinterpret_cast<const int64_t*>(indices.Data()),
indices_result.data(), indices_result.size());
}
TEST(fastdeploy, sort_dim2) {
CheckShape check_shape;
CheckData check_data;
FDTensor x, out, indices;
auto test_data = CreateTestData();
x.SetExternalData({2, 3, 4}, FDDataType::FP32, test_data.data());
Sort(x, &out, &indices, 2);
std::vector<float> out_result = {
0.114307, 0.137405, 0.646191, 0.842862, 0.535816, 0.659926,
0.742916, 0.845605, 0.212282, 0.299701, 0.408941, 0.862171,
0.126847, 0.15662, 0.428841, 0.847653, 0.245863, 0.669046,
0.676259, 0.878883, 0.32523, 0.413939, 0.666453, 0.834141};
std::vector<int64_t> indices_result = {3, 2, 1, 0, 1, 0, 2, 3, 0, 1, 3, 2,
0, 1, 2, 3, 0, 1, 3, 2, 1, 2, 0, 3};
check_shape(out.shape, {2, 3, 4});
check_data(reinterpret_cast<const float*>(out.Data()), out_result.data(),
out_result.size());
check_shape(indices.shape, {2, 3, 4});
check_data(reinterpret_cast<const int64_t*>(indices.Data()),
indices_result.data(), indices_result.size());
}
} // namespace function
} // namespace fastdeploy

View File

@@ -12,14 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <numeric>
#include <vector>
#include "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/function/transpose.h"
#include <numeric>
#include <vector>
#include "glog/logging.h"
#include "gtest/gtest.h"
#include "gtest_utils.h"
#include "gtest/gtest.h"
namespace fastdeploy {
namespace function {