Skip to content

Commit

Permalink
move algorithm.h (#39502)
Browse files Browse the repository at this point in the history
Move paddle/fluid/operators/math/algorithm.h to paddle/pten/kernels/funcs and rename all references to symbols in it.
  • Loading branch information
Feiyu Chan authored Feb 15, 2022
1 parent 2d16d69 commit 7eb9593
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 27 deletions.
2 changes: 1 addition & 1 deletion paddle/fluid/operators/optimizers/adam_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ __global__ void SparseAdamCUDAKernelREG(

for (; id < ndim; id += blockDim.x * gridDim.x) {
auto row_idx =
math::BinarySearch<int64_t>(rows_, row_count, id / row_numel);
pten::funcs::BinarySearch<int64_t>(rows_, row_count, id / row_numel);
if (lazy_mode && row_idx < 0) {
return;
} else {
Expand Down
4 changes: 2 additions & 2 deletions paddle/fluid/operators/optimizers/adam_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ limitations under the License. */
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/threadpool.h"
#include "paddle/fluid/operators/jit/kernels.h"
#include "paddle/fluid/operators/math/algorithm.h"
#include "paddle/fluid/operators/math/selected_rows_functor.h"
#include "paddle/fluid/platform/for_range.h"
#include "paddle/fluid/platform/profiler.h"
#include "paddle/pten/kernels/funcs/algorithm.h"

namespace paddle {
namespace operators {
Expand Down Expand Up @@ -279,7 +279,7 @@ class SparseAdamFunctor<T, GPUAdam, MT> {

inline HOSTDEVICE void operator()(size_t i) const {
auto row_idx =
math::BinarySearch<int64_t>(rows_, row_count_, i / row_numel_);
pten::funcs::BinarySearch<int64_t>(rows_, row_count_, i / row_numel_);
if (lazy_mode_ && row_idx < 0) {
return;
} else {
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/operators/optimizers/adamw_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ __global__ void SparseAdamWCUDAKernelREG(

for (; id < ndim; id += blockDim.x * gridDim.x) {
auto row_idx =
math::BinarySearch<int64_t>(rows_, row_count, id / row_numel);
pten::funcs::BinarySearch<int64_t>(rows_, row_count, id / row_numel);
if (lazy_mode && row_idx < 0) {
return;
} else {
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/operators/optimizers/adamw_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class SparseAdamWFunctor<T, GPUAdamW, MT> {

inline HOSTDEVICE void operator()(size_t i) const {
auto row_idx =
math::BinarySearch<int64_t>(rows_, row_count_, i / row_numel_);
pten::funcs::BinarySearch<int64_t>(rows_, row_count_, i / row_numel_);
if (lazy_mode_ && row_idx < 0) {
return;
} else {
Expand Down
6 changes: 3 additions & 3 deletions paddle/fluid/operators/optimizers/lamb_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ limitations under the License. */
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/memory/buffer.h"
#include "paddle/fluid/operators/amp/fp16_type_traits.h"
#include "paddle/fluid/operators/math/algorithm.h"
#include "paddle/fluid/operators/math/selected_rows_functor.h"
#include "paddle/fluid/operators/math/squared_l2_norm.h"
#include "paddle/fluid/platform/for_range.h"
#include "paddle/pten/kernels/funcs/algorithm.h"
#include "paddle/pten/kernels/funcs/eigen/extensions.h"

namespace paddle {
Expand Down Expand Up @@ -233,7 +233,7 @@ struct SparseLambMomentREGUpdateFunctor {
inline HOSTDEVICE void operator()(size_t i) const {
if (skip_update_ && *skip_update_) return;
auto row_idx =
math::BinarySearch<int64_t>(rows_, row_count_, i / row_numel_);
pten::funcs::BinarySearch<int64_t>(rows_, row_count_, i / row_numel_);
T g = row_idx >= 0 ? grad_[row_idx * row_numel_ + i % row_numel_]
: static_cast<T>(0);
update(i, g);
Expand Down Expand Up @@ -312,7 +312,7 @@ struct SparseLambMomentMENUpdateFunctor {
inline HOSTDEVICE void operator()(size_t i) const {
if (skip_update_ && *skip_update_) return;
auto row_idx =
math::BinarySearch<int64_t>(rows_, row_count_, i / row_numel_);
pten::funcs::BinarySearch<int64_t>(rows_, row_count_, i / row_numel_);
T g = row_idx >= 0 ? grad_[row_idx * row_numel_ + i % row_numel_]
: static_cast<T>(0);
update(i, g);
Expand Down
6 changes: 3 additions & 3 deletions paddle/fluid/operators/optimizers/momentum_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ limitations under the License. */
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/amp/fp16_type_traits.h"
#include "paddle/fluid/operators/math/algorithm.h"
#include "paddle/fluid/operators/math/selected_rows_functor.h"
#include "paddle/fluid/platform/float16.h"
#include "paddle/fluid/platform/for_range.h"
#include "paddle/pten/kernels/funcs/algorithm.h"

namespace paddle {
namespace operators {
Expand Down Expand Up @@ -345,7 +345,7 @@ class SparseMomentumFunctor<T, MT, UseNesterov> {

inline HOSTDEVICE void operator()(size_t i) {
auto row_idx =
math::BinarySearch<int64_t>(rows_, row_height_, i / row_numel_);
pten::funcs::BinarySearch<int64_t>(rows_, row_height_, i / row_numel_);
MT grad =
row_idx >= 0
? static_cast<MT>(grad_[row_idx * row_numel_ + i % row_numel_]) *
Expand Down Expand Up @@ -418,7 +418,7 @@ class SparseMomentumFunctor<T, MT, NoNesterov> {

inline HOSTDEVICE void operator()(size_t i) {
auto row_idx =
math::BinarySearch<int64_t>(rows_, row_height_, i / row_numel_);
pten::funcs::BinarySearch<int64_t>(rows_, row_height_, i / row_numel_);
MT grad =
row_idx >= 0
? static_cast<MT>(grad_[row_idx * row_numel_ + i % row_numel_]) *
Expand Down
5 changes: 3 additions & 2 deletions paddle/fluid/operators/optimizers/rmsprop_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ limitations under the License. */
#include <math.h>
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/algorithm.h"
#include "paddle/fluid/operators/math/selected_rows_functor.h"
#include "paddle/fluid/platform/for_range.h"
#include "paddle/pten/kernels/funcs/algorithm.h"

namespace paddle {
namespace operators {
Expand All @@ -42,7 +42,8 @@ struct SparseRmspropGradFunctor {
row_count_(row_count) {}

HOSTDEVICE inline T operator()(int64_t idx) const {
auto row_idx = math::BinarySearch(rows_, row_count_, idx / row_numel_);
auto row_idx =
pten::funcs::BinarySearch(rows_, row_count_, idx / row_numel_);
return row_idx >= 0 ? grad_[row_idx * row_numel_ + idx % row_numel_] : 0;
}

Expand Down
1 change: 0 additions & 1 deletion paddle/fluid/operators/optimizers/sparse_momentum_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/amp/fp16_type_traits.h"
#include "paddle/fluid/operators/math/algorithm.h"
#include "paddle/fluid/platform/float16.h"
#include "paddle/fluid/platform/for_range.h"

Expand Down
10 changes: 5 additions & 5 deletions paddle/fluid/operators/searchsorted_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#include "paddle/fluid/framework/ddim.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/operators/math/algorithm.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/for_range.h"
#include "paddle/pten/kernels/funcs/algorithm.h"

namespace paddle {
namespace operators {
Expand Down Expand Up @@ -85,11 +85,11 @@ class GpuAndCpuSearchSortedCompute {
out_data_[idx] = seq_size_;
} else {
if (right_) {
out_data_[idx] = static_cast<OutType>(
math::UpperBound<T1, T2>(sequence_ptr, seq_size_, *value_ptr));
out_data_[idx] = static_cast<OutType>(pten::funcs::UpperBound<T1, T2>(
sequence_ptr, seq_size_, *value_ptr));
} else {
out_data_[idx] = static_cast<OutType>(
math::LowerBound<T1, T2>(sequence_ptr, seq_size_, *value_ptr));
out_data_[idx] = static_cast<OutType>(pten::funcs::LowerBound<T1, T2>(
sequence_ptr, seq_size_, *value_ptr));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions paddle/fluid/operators/sequence_ops/sequence_reverse_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

#include <memory>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/algorithm.h"
#include "paddle/fluid/platform/for_range.h"
#include "paddle/pten/kernels/funcs/algorithm.h"

namespace paddle {
namespace operators {
Expand Down Expand Up @@ -94,7 +94,7 @@ struct SequenceReverseFunctor {

HOSTDEVICE void operator()(size_t idx_x) const {
auto row_idx_x = idx_x / row_numel_;
auto lod_idx = math::UpperBound(lod_, lod_count_, row_idx_x);
auto lod_idx = pten::funcs::UpperBound(lod_, lod_count_, row_idx_x);
auto row_idx_y = lod_[lod_idx - 1] + (lod_[lod_idx] - 1 - row_idx_x);
auto idx_y = row_idx_y * row_numel_ + idx_x % row_numel_;
y_[idx_y] = x_[idx_x];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

#include "paddle/pten/core/hostdevice.h"

namespace paddle {
namespace operators {
namespace math {
namespace pten {
namespace funcs {

template <typename T>
HOSTDEVICE inline int64_t BinarySearch(const T *x, int64_t num, const T &val) {
Expand Down Expand Up @@ -85,6 +84,5 @@ HOSTDEVICE inline size_t UpperBound(const T1 *x, size_t num, const T2 &val) {
#endif // @} End Group UpperBound
}

} // namespace math
} // namespace operators
} // namespace paddle
} // namespace funcs
} // namespace pten

0 comments on commit 7eb9593

Please sign in to comment.