Skip to content

Commit

Permalink
[phi] move cpu_vec (#39714)
Browse files Browse the repository at this point in the history
move cpu_vec.h to phi/kernels/funcs.
  • Loading branch information
Feiyu Chan authored Mar 4, 2022
1 parent 880dec0 commit 70540b2
Show file tree
Hide file tree
Showing 7 changed files with 756 additions and 64 deletions.
18 changes: 9 additions & 9 deletions paddle/fluid/operators/attention_lstm_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ limitations under the License. */

#include "paddle/fluid/operators/attention_lstm_op.h"
#include <string>
#include "paddle/fluid/operators/math/cpu_vec.h"
#include "paddle/fluid/operators/math/fc.h"
#include "paddle/fluid/platform/cpu_info.h"
#include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/phi/kernels/funcs/cpu_vec.h"

namespace paddle {
namespace operators {
Expand Down Expand Up @@ -269,10 +269,10 @@ use lstm_x_t as input and compute as standard LSTM.
template <typename T>
inline void bias_relu(const int n, const T* x, const T* bias, T* y) {
if (bias) {
math::vec_add_bias<T, platform::avx>(n, *bias, x, y);
math::vec_relu<T, platform::avx>(n, y, y);
phi::funcs::vec_add_bias<T, platform::avx>(n, *bias, x, y);
phi::funcs::vec_relu<T, platform::avx>(n, y, y);
} else {
math::vec_relu<T, platform::avx>(n, x, y);
phi::funcs::vec_relu<T, platform::avx>(n, x, y);
}
}

Expand All @@ -283,14 +283,14 @@ inline void vec_softmax(const int n, const T* x, T* y) {
for (int i = 1; i < n; ++i) {
scalar = scalar < x[i] ? x[i] : scalar;
}
math::vec_add_bias<T, platform::avx>(n, -scalar, x, y); // sub
math::vec_exp<T>(n, y, y); // exp
phi::funcs::vec_add_bias<T, platform::avx>(n, -scalar, x, y); // sub
phi::funcs::vec_exp<T>(n, y, y); // exp
// sum
scalar = T(0);
for (int i = 0; i < n; ++i) {
scalar += y[i];
}
math::vec_scal<T>(n, static_cast<T>(1) / scalar, y); // scale
phi::funcs::vec_scal<T>(n, static_cast<T>(1) / scalar, y); // scale
}

template <typename T>
Expand Down Expand Up @@ -344,12 +344,12 @@ class AttentionLSTMKernel : public framework::OpKernel<T> {
auto& act_cell_str = ctx.Attr<std::string>("cell_activation");
auto& act_cand_str = ctx.Attr<std::string>("candidate_activation");
if (platform::MayIUse(platform::avx)) {
math::VecActivations<T, platform::avx> act_functor;
phi::funcs::VecActivations<T, platform::avx> act_functor;
act_gate = act_functor(act_gate_str);
act_cell = act_functor(act_cell_str);
act_cand = act_functor(act_cand_str);
} else {
math::VecActivations<T, platform::isa_any> act_functor;
phi::funcs::VecActivations<T, platform::isa_any> act_functor;
act_gate = act_functor(act_gate_str);
act_cell = act_functor(act_cell_str);
act_cand = act_functor(act_cand_str);
Expand Down
6 changes: 3 additions & 3 deletions paddle/fluid/operators/fused/fused_embedding_fc_lstm_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ limitations under the License. */

#include "paddle/fluid/operators/fused/fused_embedding_fc_lstm_op.h"
#include <string>
#include "paddle/fluid/operators/math/cpu_vec.h"
#include "paddle/fluid/platform/cpu_info.h"
#include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/phi/kernels/funcs/cpu_vec.h"
#include "paddle/phi/kernels/funcs/sequence2batch.h"

namespace paddle {
Expand Down Expand Up @@ -243,12 +243,12 @@ class FusedEmbeddingFCLSTMKernel : public framework::OpKernel<T> {
auto& act_cell_str = ctx.Attr<std::string>("cell_activation"); \
auto& act_cand_str = ctx.Attr<std::string>("candidate_activation"); \
if (platform::MayIUse(platform::avx)) { \
math::VecActivations<T, platform::avx> act_functor; \
phi::funcs::VecActivations<T, platform::avx> act_functor; \
act_gate = act_functor(act_gate_str); \
act_cell = act_functor(act_cell_str); \
act_cand = act_functor(act_cand_str); \
} else { \
math::VecActivations<T, platform::isa_any> act_functor; \
phi::funcs::VecActivations<T, platform::isa_any> act_functor; \
act_gate = act_functor(act_gate_str); \
act_cell = act_functor(act_cell_str); \
act_cand = act_functor(act_cand_str); \
Expand Down
6 changes: 3 additions & 3 deletions paddle/fluid/operators/fused/fusion_seqexpand_concat_fc_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ limitations under the License. */

#include "paddle/fluid/operators/fused/fusion_seqexpand_concat_fc_op.h"
#include <string>
#include "paddle/fluid/operators/math/cpu_vec.h"
#include "paddle/fluid/operators/math/fc.h"
#include "paddle/fluid/platform/cpu_info.h"
#include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/phi/kernels/funcs/cpu_vec.h"

namespace paddle {
namespace operators {
Expand Down Expand Up @@ -196,10 +196,10 @@ class FusionSeqExpandConcatFCOpKernel : public framework::OpKernel<T> {
std::function<void(const int, const T*, T*)> fc_act;
auto& fc_act_str = ctx.Attr<std::string>("fc_activation");
if (platform::MayIUse(platform::avx)) {
math::VecActivations<T, platform::avx> act_functor;
phi::funcs::VecActivations<T, platform::avx> act_functor;
fc_act = act_functor(fc_act_str);
} else {
math::VecActivations<T, platform::isa_any> act_functor;
phi::funcs::VecActivations<T, platform::isa_any> act_functor;
fc_act = act_functor(fc_act_str);
}

Expand Down
1 change: 0 additions & 1 deletion paddle/fluid/operators/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ if(WITH_GPU AND (NOT WITH_ROCM))
endif()
endif()

cc_test(cpu_vec_test SRCS cpu_vec_test.cc DEPS blas cpu_info)
if(WITH_TESTING AND TEST im2col_test)
set_tests_properties(im2col_test PROPERTIES TIMEOUT 120)
endif()
Loading

0 comments on commit 70540b2

Please sign in to comment.