Skip to content

Commit

Permalink
修改simd实现
Browse files Browse the repository at this point in the history
  • Loading branch information
zjhellofss committed Jul 26, 2023
1 parent 89237db commit e8bfc18
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 1,854 deletions.
3 changes: 1 addition & 2 deletions bench/bench_exp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

#include <benchmark/benchmark.h>
#include <armadillo>
#include "../source/layer/details/sse_mathfun.hpp"
#include "../source/layer/details/arma_sse.hpp"
#include "data/tensor.hpp"
#include "utils/math/arma_sse.hpp"
static void BM_ExpSimd(benchmark::State& state) {
using namespace kuiper_infer;
uint32_t input_c = state.range(0);
Expand Down
16 changes: 2 additions & 14 deletions include/utils/math/fmath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ inline float exp(float x)
using namespace local;
const ExpVar<>& expVar = C<>::expVar;

#if 1
#if __SSE2__
__m128 x1 = _mm_set_ss(x);

int limit = _mm_cvtss_si32(x1) & 0x7fffffff;
Expand All @@ -429,19 +429,7 @@ inline float exp(float x)
fi.i = ((u + 127) << 23) | expVar.tbl[v];
return (1 + t) * fi.f;
#else
x = std::min(x, expVar.maxX[0]);
x = std::max(x, expVar.minX[0]);
float t = x * expVar.a[0];
const float magic = (1 << 23) + (1 << 22); // to round
t += magic;
fi fi;
fi.f = t;
t = x - (t - magic) * expVar.b[0];
int u = ((fi.i + (127 << expVar.s)) >> expVar.s) << 23;
unsigned int v = fi.i & mask(expVar.s);
fi.i = u | expVar.tbl[v];
return (1 + t) * fi.f;
// return (1 + t) * pow(2, (float)u) * pow(2, (float)v / n);
return std::exp(x);
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//
// Created by fss on 23-7-26.
//
#include "utils/math/arma_sse.hpp"
#include "arma_sse.hpp"
#include <glog/logging.h>

namespace kuiper_infer {
Expand Down Expand Up @@ -66,7 +66,7 @@ void ArmaSigmoid(const arma::fcube& input_data, arma::fcube& output_data) {
if (index < in_size) {
while (index < in_size) {
float value = input_data.at(index);
output_data.at(index) = 1 / (1.f + expf(-value));
output_data.at(index) = 1 / (1.f + fmath::exp(-value));
index += 1;
}
}
Expand Down Expand Up @@ -166,7 +166,7 @@ void ArmaSiLU(const arma::fcube& input_data, arma::fcube& output_data) {
if (j < size) {
while (j < size) {
float value = input_data.at(j);
output_data.at(j) = value / (1.f + expf(-value));
output_data.at(j) = value / (1.f + fmath::exp(-value));
j += 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
// Created by fss on 23-7-26.
//

#ifndef KUIPER_INFER_INCLUDE_MATH_ARMA_SIGMOID
#define KUIPER_INFER_INCLUDE_MATH_ARMA_SIGMOID
#ifndef KUIPER_INFER_INCLUDE_MATH_ARMA_SSE
#define KUIPER_INFER_INCLUDE_MATH_ARMA_SSE
#include <armadillo>
#include "fmath.hpp"
#include "utils/math/fmath.hpp"
namespace kuiper_infer {
namespace math {
void ArmaSigmoid(const arma::fcube& input_data, arma::fcube& output_data);
Expand All @@ -35,4 +35,4 @@ void ArmaReLU(const arma::fcube& input_data, arma::fcube& output_data);
void ArmaSiLU(const arma::fcube& input_data, arma::fcube& output_data);
} // namespace math
} // namespace kuiper_infer
#endif // KUIPER_INFER_INCLUDE_MATH_ARMA_SIGMOID
#endif // KUIPER_INFER_INCLUDE_MATH_ARMA_SSE
53 changes: 0 additions & 53 deletions source/layer/details/platform.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion source/layer/details/relu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

// Created by fss on 22-11-18.
#include "relu.hpp"
#include "arma_sse.hpp"
#include "layer/abstract/layer_factory.hpp"
#include "utils/math/arma_sse.hpp"

namespace kuiper_infer {
InferStatus ReluLayer::Forward(
Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/sigmoid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

#include "sigmoid.hpp"
#include <glog/logging.h>
#include "arma_sse.hpp"
#include "layer/abstract/layer_factory.hpp"
#include "utils/math/arma_sse.hpp"

namespace kuiper_infer {

Expand Down
2 changes: 1 addition & 1 deletion source/layer/details/silu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
// Created by fss on 22-12-25.

#include "silu.hpp"
#include "arma_sse.hpp"
#include "layer/abstract/layer_factory.hpp"
#include "tick.hpp"
#include "utils/math/arma_sse.hpp"

namespace kuiper_infer {

Expand Down
3 changes: 2 additions & 1 deletion source/layer/details/softmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <numeric>
#include "data/tensor_util.hpp"
#include "layer/abstract/layer_factory.hpp"
#include "utils/math/fmath.hpp"
namespace kuiper_infer {
#define POS_INDEX(outer_size, inner_size, axis_size) \
outer_size* axis_sizes* inner_sizes + axis_size* inner_sizes + inner_size;
Expand Down Expand Up @@ -116,7 +117,7 @@ InferStatus SoftmaxLayer::Forward(
for (uint32_t axis_size = 0; axis_size < axis_sizes; ++axis_size) {
uint32_t index = POS_INDEX(outer_size, inner_size, axis_size);
float cur_value = input_values.at(index);
float exp_sub_value = std::exp(cur_value - max_value);
float exp_sub_value = fmath::exp(cur_value - max_value);

sum_value += exp_sub_value;
output_values.at(index) = exp_sub_value;
Expand Down
Loading

0 comments on commit e8bfc18

Please sign in to comment.