Skip to content

Commit 129dda7

Browse files
authored
[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
1 parent 4e74ac0 commit 129dda7

37 files changed

+1567
-75
lines changed

fastdeploy/core/fd_scalar.h

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
#pragma once
15+
16+
#include <cstdint>
17+
#include <limits>
18+
19+
#include "fastdeploy/core/fd_type.h"
20+
#include "fastdeploy/core/float16.h"
21+
22+
namespace fastdeploy {
23+
24+
class Scalar {
25+
public:
26+
// Constructor support implicit
27+
Scalar() : Scalar(0) {}
28+
Scalar(double val) : dtype_(FDDataType::FP64) { // NOLINT
29+
data_.f64 = val;
30+
}
31+
32+
Scalar(float val) : dtype_(FDDataType::FP32) { // NOLINT
33+
data_.f32 = val;
34+
}
35+
36+
Scalar(float16 val) : dtype_(FDDataType::FP16) { // NOLINT
37+
data_.f16 = val;
38+
}
39+
40+
Scalar(int64_t val) : dtype_(FDDataType::INT64) { // NOLINT
41+
data_.i64 = val;
42+
}
43+
44+
Scalar(int32_t val) : dtype_(FDDataType::INT32) { // NOLINT
45+
data_.i32 = val;
46+
}
47+
48+
Scalar(int16_t val) : dtype_(FDDataType::INT16) { // NOLINT
49+
data_.i16 = val;
50+
}
51+
52+
Scalar(int8_t val) : dtype_(FDDataType::INT8) { // NOLINT
53+
data_.i8 = val;
54+
}
55+
56+
Scalar(uint8_t val) : dtype_(FDDataType::UINT8) { // NOLINT
57+
data_.ui8 = val;
58+
}
59+
60+
Scalar(bool val) : dtype_(FDDataType::BOOL) { // NOLINT
61+
data_.b = val;
62+
}
63+
64+
// The compatible method for fliud operators,
65+
// and it will be removed in the future.
66+
explicit Scalar(const std::string& str_value) : dtype_(FDDataType::FP64) {
67+
if (str_value == "inf") {
68+
data_.f64 = std::numeric_limits<double>::infinity();
69+
} else if (str_value == "-inf") {
70+
data_.f64 = -std::numeric_limits<double>::infinity();
71+
} else if (str_value == "nan") {
72+
data_.f64 = std::numeric_limits<double>::quiet_NaN();
73+
} else {
74+
data_.f64 = std::stod(str_value);
75+
}
76+
}
77+
78+
template <typename RT> inline RT to() const {
79+
switch (dtype_) {
80+
case FDDataType::FP32:
81+
return static_cast<RT>(data_.f32);
82+
case FDDataType::FP64:
83+
return static_cast<RT>(data_.f64);
84+
case FDDataType::FP16:
85+
return static_cast<RT>(data_.f16);
86+
case FDDataType::INT32:
87+
return static_cast<RT>(data_.i32);
88+
case FDDataType::INT64:
89+
return static_cast<RT>(data_.i64);
90+
case FDDataType::INT16:
91+
return static_cast<RT>(data_.i16);
92+
case FDDataType::INT8:
93+
return static_cast<RT>(data_.i8);
94+
case FDDataType::UINT8:
95+
return static_cast<RT>(data_.ui8);
96+
case FDDataType::BOOL:
97+
return static_cast<RT>(data_.b);
98+
default:
99+
FDASSERT(false, "Invalid enum scalar data type `%s`.",
100+
Str(dtype_).c_str());
101+
}
102+
}
103+
104+
FDDataType dtype() const { return dtype_; }
105+
106+
private:
107+
FDDataType dtype_;
108+
union data {
109+
bool b;
110+
int8_t i8;
111+
int16_t i16;
112+
int32_t i32;
113+
int64_t i64;
114+
uint8_t ui8;
115+
float16 f16;
116+
float f32;
117+
double f64;
118+
} data_;
119+
};
120+
121+
} // namespace fastdeploy

fastdeploy/core/fd_tensor.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414
#include "fastdeploy/core/fd_tensor.h"
15+
#include "fastdeploy/core/fd_scalar.h"
1516
#include "fastdeploy/core/float16.h"
1617
#include "fastdeploy/utils/utils.h"
18+
1719
#include <algorithm>
1820
#include <cstring>
1921
#ifdef WITH_GPU
@@ -344,6 +346,40 @@ void FDTensor::CopyBuffer(void* dst, const void* src, size_t nbytes,
344346
}
345347

346348
FDTensor::FDTensor(const std::string& tensor_name) { name = tensor_name; }
349+
FDTensor::FDTensor(const char* tensor_name) { name = tensor_name; }
350+
351+
FDTensor::FDTensor(const Scalar& scalar) {
352+
Allocate({1}, scalar.dtype());
353+
switch (scalar.dtype()) {
354+
case FDDataType::BOOL:
355+
(reinterpret_cast<bool*>(Data()))[0] = scalar.to<bool>();
356+
break;
357+
case FDDataType::UINT8:
358+
(reinterpret_cast<uint8_t*>(Data()))[0] = scalar.to<uint8_t>();
359+
break;
360+
case FDDataType::INT8:
361+
(reinterpret_cast<int8_t*>(Data()))[0] = scalar.to<int8_t>();
362+
break;
363+
case FDDataType::INT16:
364+
(reinterpret_cast<int16_t*>(Data()))[0] = scalar.to<int16_t>();
365+
break;
366+
case FDDataType::INT32:
367+
(reinterpret_cast<int*>(Data()))[0] = scalar.to<int>();
368+
break;
369+
case FDDataType::INT64:
370+
(reinterpret_cast<int64_t*>(Data()))[0] = scalar.to<int64_t>();
371+
break;
372+
case FDDataType::FP16:
373+
(reinterpret_cast<float16*>(Data()))[0] = scalar.to<float16>();
374+
break;
375+
case FDDataType::FP32:
376+
(reinterpret_cast<float*>(Data()))[0] = scalar.to<float>();
377+
break;
378+
case FDDataType::FP64:
379+
(reinterpret_cast<double*>(Data()))[0] = scalar.to<double>();
380+
break;
381+
}
382+
}
347383

348384
FDTensor::FDTensor(const FDTensor& other)
349385
: shape(other.shape), name(other.name), dtype(other.dtype),

fastdeploy/core/fd_tensor.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
namespace fastdeploy {
2525

26+
struct Scalar;
27+
2628
struct FASTDEPLOY_DECL FDTensor {
2729
// std::vector<int8_t> data;
2830
void* buffer_ = nullptr;
@@ -126,6 +128,8 @@ struct FASTDEPLOY_DECL FDTensor {
126128

127129
FDTensor() {}
128130
explicit FDTensor(const std::string& tensor_name);
131+
explicit FDTensor(const char* tensor_name);
132+
129133
// Deep copy
130134
FDTensor(const FDTensor& other);
131135
// Move constructor
@@ -136,6 +140,9 @@ struct FASTDEPLOY_DECL FDTensor {
136140
// Move assignment
137141
FDTensor& operator=(FDTensor&& other);
138142

143+
// Scalar to FDTensor
144+
explicit FDTensor(const Scalar& scalar);
145+
139146
~FDTensor() { FreeFn(); }
140147

141148
static void CopyBuffer(void* dst, const void* src, size_t nbytes,

fastdeploy/function/cast.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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/function/cast.h"
16+
#include <algorithm>
17+
18+
namespace fastdeploy {
19+
namespace function {
20+
21+
template <typename InT, typename OutT> struct CastOpTransformFunctor {
22+
OutT operator()(InT in) const { return static_cast<OutT>(in); }
23+
};
24+
25+
template <typename InT>
26+
void CastKernel(const FDTensor& x, FDTensor* out, FDDataType output_dtype) {
27+
28+
FD_VISIT_ALL_TYPES(output_dtype, "CastOpTransformFunctor", ([&] {
29+
auto* in_begin = reinterpret_cast<const InT*>(x.Data());
30+
auto* in_end = in_begin + x.Numel();
31+
FDTensor out_tmp;
32+
out_tmp.Allocate(x.Shape(), output_dtype);
33+
auto* out_begin = reinterpret_cast<data_t*>(out_tmp.Data());
34+
std::transform(in_begin, in_end, out_begin,
35+
CastOpTransformFunctor<InT, data_t>());
36+
*out = std::move(out_tmp);
37+
}));
38+
}
39+
40+
void Cast(const FDTensor& x, FDTensor* out, FDDataType output_dtype) {
41+
FD_VISIT_ALL_TYPES(x.dtype, "CastKernel",
42+
([&] { CastKernel<data_t>(x, out, output_dtype); }));
43+
}
44+
45+
} // namespace function
46+
} // namespace fastdeploy

fastdeploy/function/cast.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
#pragma once
16+
17+
#include "fastdeploy/core/fd_tensor.h"
18+
19+
namespace fastdeploy {
20+
namespace function {
21+
22+
/** Cast x to output data type element-wise. Only for float type FDTensor
23+
@param x The input tensor.
24+
@param out The output tensor which stores the result.
25+
@param output_dtype The type of output tensor.
26+
*/
27+
FASTDEPLOY_DECL void Cast(const FDTensor& x, FDTensor* out,
28+
FDDataType output_dtype);
29+
30+
} // namespace function
31+
} // namespace fastdeploy

fastdeploy/function/concat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace function {
2222
/** Excute the concatenate operation for input FDTensor along given axis.
2323
@param x The input tensor.
2424
@param out The output tensor which stores the result.
25-
@param axisi Axis which will be concatenated.
25+
@param axis Axis which will be concatenated.
2626
*/
2727

2828
FASTDEPLOY_DECL void Concat(const std::vector<FDTensor>& x, FDTensor* out,

fastdeploy/function/elementwise.cc

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,21 @@ void Add(const FDTensor& x, const FDTensor& y, FDTensor* out) {
3232
([&] { AddRawKernel<data_t>()(x, y, -1, out); }));
3333
}
3434

35-
FDTensor operator+(const FDTensor& x, const FDTensor& y) {
36-
FDTensor out;
37-
Add(x, y, &out);
38-
return out;
39-
}
40-
4135
void Subtract(const FDTensor& x, const FDTensor& y, FDTensor* out) {
4236
FD_VISIT_ALL_TYPES(x.dtype, "SubtractRawKernel",
4337
([&] { SubtractRawKernel<data_t>()(x, y, -1, out); }));
4438
}
4539

46-
FDTensor operator-(const FDTensor& x, const FDTensor& y) {
47-
FDTensor out;
48-
Subtract(x, y, &out);
49-
return out;
50-
}
51-
5240
void Multiply(const FDTensor& x, const FDTensor& y, FDTensor* out) {
5341
FD_VISIT_ALL_TYPES(x.dtype, "MultiplyRawKernel",
5442
([&] { MultiplyRawKernel<data_t>()(x, y, -1, out); }));
5543
}
5644

57-
FDTensor operator*(const FDTensor& x, const FDTensor& y) {
58-
FDTensor out;
59-
Multiply(x, y, &out);
60-
return out;
61-
}
62-
6345
void Divide(const FDTensor& x, const FDTensor& y, FDTensor* out) {
6446
FD_VISIT_ALL_TYPES(x.dtype, "DivideRawKernel",
6547
([&] { DivideRawKernel<data_t>()(x, y, -1, out); }));
6648
}
6749

68-
FDTensor operator/(const FDTensor& x, const FDTensor& y) {
69-
FDTensor out;
70-
Divide(x, y, &out);
71-
return out;
72-
}
73-
7450
template <typename T> struct MaximumRawKernel {
7551
void operator()(const FDTensor& x, const FDTensor& y, int axis,
7652
FDTensor* out) {
@@ -85,4 +61,29 @@ void Maximum(const FDTensor& x, const FDTensor& y, FDTensor* out) {
8561
}
8662

8763
} // namespace function
64+
65+
FDTensor operator+(const FDTensor& x, const FDTensor& y) {
66+
FDTensor out;
67+
function::Add(x, y, &out);
68+
return out;
69+
}
70+
71+
FDTensor operator-(const FDTensor& x, const FDTensor& y) {
72+
FDTensor out;
73+
function::Subtract(x, y, &out);
74+
return out;
75+
}
76+
77+
FDTensor operator*(const FDTensor& x, const FDTensor& y) {
78+
FDTensor out;
79+
function::Multiply(x, y, &out);
80+
return out;
81+
}
82+
83+
FDTensor operator/(const FDTensor& x, const FDTensor& y) {
84+
FDTensor out;
85+
function::Divide(x, y, &out);
86+
return out;
87+
}
88+
8889
} // namespace fastdeploy

0 commit comments

Comments
 (0)