Skip to content

Commit

Permalink
Move slice to phi (#40736)
Browse files Browse the repository at this point in the history
* move slice to pten

* merge develop; test=develop

* fix slice bug;

* update

* update

* fix error

* update

* fix bug

* polish code

* polish code

* polish code

* try to fix windows bug

* add gpu compile flag;

* try to fix

* remov template;

* polish code;

* fix npu bug;

* fix npu bug

* fix npu bug; test=develop

* fix slice bug;

* remove no need dep
  • Loading branch information
phlrain authored Mar 27, 2022
1 parent 0ad2e19 commit b8236b7
Show file tree
Hide file tree
Showing 23 changed files with 1,019 additions and 418 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ PD_DECLARE_KERNEL(sum, GPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(sum_grad, GPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(max_raw, GPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(sgd, GPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(slice, GPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(slice_grad, GPU, ALL_LAYOUT);

DECLARE_double(eager_delete_tensor_gb);

Expand Down
17 changes: 10 additions & 7 deletions paddle/fluid/operators/lu_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ void SetValueCompute(const framework::ExecutionContext& ctx,
auto dtype = framework::TransToProtoVarType(in->dtype());

auto in_dims = in->dims();
CheckAndUpdateSliceAttrs<int64_t>(in_dims, axes, starts, ends, &steps);
auto slice_dims = GetSliceDims(in_dims, axes, *starts, *ends, &steps);
auto decrease_slice_dims = GetDecreasedDims(slice_dims, decrease_axes);
phi::funcs::CheckAndUpdateSliceAttrs<int64_t>(in_dims, axes, starts, ends,
&steps);
auto slice_dims =
phi::funcs::GetSliceDims(in_dims, axes, *starts, *ends, &steps);
auto decrease_slice_dims =
phi::funcs::GetDecreasedDims(slice_dims, decrease_axes);

auto slice_dims_for_assign = decrease_slice_dims;
if (!none_axes.empty()) {
Expand Down Expand Up @@ -282,10 +285,10 @@ void SliceCompute(const framework::ExecutionContext& ctx,
}
}

CheckAndUpdateSliceAttrs(in_dims, axes, &starts, &ends);
slice_dims =
GetSliceDims<int64_t>(in_dims, axes, starts, ends, nullptr, nullptr);
out_dims = GetDecreasedDims(slice_dims, decrease_axis);
phi::funcs::CheckAndUpdateSliceAttrs(in_dims, axes, &starts, &ends);
slice_dims = phi::funcs::GetSliceDims<int64_t>(in_dims, axes, starts, ends,
nullptr, nullptr);
out_dims = phi::funcs::GetDecreasedDims(slice_dims, decrease_axis);

// 2.2 Get output
auto offsets = Eigen::DSizes<Eigen::DenseIndex, D>();
Expand Down
4 changes: 3 additions & 1 deletion paddle/fluid/operators/set_value_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/tensor_util.h"
#include "paddle/fluid/operators/assign_value_op.h"
#include "paddle/fluid/operators/slice_utils.h"
#include "paddle/fluid/operators/eigen/eigen_function.h"
#include "paddle/fluid/operators/elementwise/elementwise_op_function.h"
#include "paddle/fluid/operators/utils.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/phi/kernels/funcs/slice_utils.h"

namespace paddle {
namespace operators {
Expand Down
10 changes: 7 additions & 3 deletions paddle/fluid/operators/set_value_op_npu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ limitations under the License. */
#include "paddle/fluid/operators/set_value_op.h"
#include "paddle/fluid/platform/device/npu/npu_op_runner.h"

#include "paddle/phi/kernels/funcs/slice_utils.h"

namespace paddle {
namespace operators {

Expand Down Expand Up @@ -51,9 +53,11 @@ class SetValueNPUKernel : public framework::OpKernel<T> {
}

auto in_dims = in->dims();
CheckAndUpdateSliceAttrs(in_dims, axes, &starts, &ends, &steps);
auto slice_dims = GetSliceDims(in_dims, axes, starts, ends, &steps);
auto decrease_slice_dims = GetDecreasedDims(slice_dims, decrease_axes);
phi::funcs::CheckAndUpdateSliceAttrs(in_dims, axes, &starts, &ends, &steps);
auto slice_dims =
phi::funcs::GetSliceDims(in_dims, axes, starts, ends, &steps);
auto decrease_slice_dims =
phi::funcs::GetDecreasedDims(slice_dims, decrease_axes);

auto slice_dims_for_assign = decrease_slice_dims;
if (!none_axes.empty()) {
Expand Down
15 changes: 9 additions & 6 deletions paddle/fluid/operators/slice_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License. */
#include <memory>
#include <string>
#include <vector>
#include "paddle/phi/kernels/funcs/slice_utils.h"

namespace paddle {
namespace operators {
Expand Down Expand Up @@ -101,15 +102,17 @@ class SliceOp : public framework::OperatorWithKernel {
"The size of ends must be equal to the size of axes."));
}

CheckAndUpdateSliceAttrs<int>(in_dims, axes, &starts, &ends, nullptr,
&infer_flags);
phi::funcs::CheckAndUpdateSliceAttrs<int>(in_dims, axes, &starts, &ends,
nullptr, &infer_flags);

auto slice_dims =
GetSliceDims<int>(in_dims, axes, starts, ends, nullptr, &infer_flags);
auto slice_dims = phi::funcs::GetSliceDims<int>(in_dims, axes, starts, ends,
nullptr, &infer_flags);
if (ctx->IsRuntime()) {
out_dims = GetDecreasedDims<int>(slice_dims, decrease_axis, &infer_flags);
out_dims = phi::funcs::GetDecreasedDims<int>(slice_dims, decrease_axis,
&infer_flags);
} else {
out_dims = GetDecreasedDims<int>(slice_dims, decrease_axis, nullptr);
out_dims =
phi::funcs::GetDecreasedDims<int>(slice_dims, decrease_axis, nullptr);
}

ctx->SetOutputDim("Out", out_dims);
Expand Down
Loading

0 comments on commit b8236b7

Please sign in to comment.