未验证 提交 f3650201 编写于 作者: Y YuanRisheng 提交者: GitHub

[PHI]Standardise some C++ API (Part5) (#47860)

* standard api

* fix xpu bugs
上级 c374894d
......@@ -193,7 +193,6 @@ void LayerNorm(const std::vector<LayerNormParamType<T>> &scale,
bias_opt,
1e-5,
1,
false,
tensor_y,
tensor_mean,
tensor_variance);
......
......@@ -93,7 +93,7 @@ namespace ops = paddle::operators;
namespace plat = paddle::platform;
DECLARE_INFER_SHAPE_FUNCTOR(tril_triu,
TrilTriuInferShapeFunctor,
PD_INFER_META(phi::TrilInferMeta));
PD_INFER_META(phi::TrilTriuInferMeta));
REGISTER_OPERATOR(tril_triu,
ops::TrilTriuOp,
ops::TrilTriuOpMaker,
......
......@@ -655,8 +655,8 @@
inplace : (out_grad -> x_grad)
- backward_op : hsigmoid_loss_grad
forward : hsigmoid_loss (Tensor x, Tensor w, Tensor label, Tensor path, Tensor code, Tensor bias, int num_classes, bool remote_prefetch, int trainer_id, int64_t[] height_sections, str[] epmap, str[] table_names, bool is_sparse) -> Tensor(out), Tensor(pre_out), Tensor(w_out)
args : (Tensor x, Tensor w, Tensor label, Tensor path, Tensor code, Tensor bias, Tensor pre_out, Tensor out_grad, int num_classes, bool remote_prefetch, int trainer_id, int64_t[] height_sections, str[] epmap, str[] table_names, bool is_sparse)
forward : hsigmoid_loss (Tensor x, Tensor label, Tensor w, Tensor bias, Tensor path, Tensor code, int num_classes, bool remote_prefetch, bool is_sparse) -> Tensor(out), Tensor(pre_out), Tensor(w_out)
args : (Tensor x, Tensor w, Tensor label, Tensor path, Tensor code, Tensor bias, Tensor pre_out, Tensor out_grad, int num_classes, bool remote_prefetch, bool is_sparse)
output : Tensor(x_grad), Tensor(w_grad), Tensor(bias_grad)
infer_meta :
func : GeneralTernaryGradInferMeta
......@@ -764,8 +764,8 @@
func : label_smooth_grad
- backward_op : layer_norm_grad
forward : layer_norm (Tensor x, Tensor scale, Tensor bias, float epsilon, int begin_norm_axis, bool is_test) -> Tensor(out), Tensor(mean), Tensor(variance)
args : (Tensor x, Tensor scale, Tensor bias, Tensor mean, Tensor variance, Tensor out_grad, float epsilon, int begin_norm_axis, bool is_test)
forward : layer_norm (Tensor x, Tensor scale, Tensor bias, float epsilon, int begin_norm_axis) -> Tensor(out), Tensor(mean), Tensor(variance)
args : (Tensor x, Tensor scale, Tensor bias, Tensor mean, Tensor variance, Tensor out_grad, float epsilon, int begin_norm_axis)
output : Tensor(x_grad), Tensor(scale_grad), Tensor(bias_grad)
infer_meta :
func : LayerNormGradInferMeta
......@@ -1830,8 +1830,8 @@
func : triangular_solve_grad
- backward_op : tril_grad
forward : tril(Tensor x, int diagonal, bool lower) -> Tensor(out)
args : (Tensor out_grad, int diagonal, bool lower)
forward : tril(Tensor x, int diagonal) -> Tensor(out)
args : (Tensor out_grad, int diagonal)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
......@@ -1851,6 +1851,16 @@
func : trilinear_interp_grad
data_type : output_grad
- backward_op : triu_grad
forward : triu(Tensor x, int diagonal) -> Tensor(out)
args : (Tensor out_grad, int diagonal)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
param : [out_grad]
kernel :
func : triu_grad
- backward_op : unbind_grad
forward : unbind (Tensor input, int axis) -> Tensor[](out)
args : (Tensor[] out_grad, int axis)
......
......@@ -934,7 +934,7 @@
backward : hardtanh_grad
- op : hsigmoid_loss
args : (Tensor x, Tensor w, Tensor label, Tensor path, Tensor code, Tensor bias, int num_classes, bool remote_prefetch, int trainer_id, int64_t[] height_sections, str[] epmap, str[] table_names, bool is_sparse)
args : (Tensor x, Tensor label, Tensor w, Tensor bias, Tensor path, Tensor code, int num_classes, bool remote_prefetch, bool is_sparse)
output : Tensor(out), Tensor(pre_out), Tensor(w_out)
infer_meta :
func : HSigmoidLossInferMeta
......@@ -1091,7 +1091,7 @@
inplace : (param -> param_out), (moment1 -> moment1_out), (moment2 -> moment2_out), (beta1_pow -> beta1_pow_out), (beta2_pow -> beta2_pow_out), (master_param -> master_param_outs)
- op : layer_norm
args : (Tensor x, Tensor scale, Tensor bias, float epsilon, int begin_norm_axis, bool is_test)
args : (Tensor x, Tensor scale, Tensor bias, float epsilon, int begin_norm_axis)
output : Tensor(out), Tensor(mean), Tensor(variance)
infer_meta :
func : LayerNormInferMeta
......@@ -1294,11 +1294,11 @@
backward : matrix_power_grad
- op : matrix_rank
args : (Tensor x, float tol, bool use_default_tol=true, bool hermitian=false)
args : (Tensor x, float tol, bool hermitian=false, bool use_default_tol=true)
output : Tensor(out)
infer_meta :
func : MatrixRankInferMeta
param : [x, use_default_tol, hermitian]
param : [x, hermitian, use_default_tol]
kernel :
func : matrix_rank
......@@ -2226,7 +2226,7 @@
backward : triangular_solve_grad
- op : tril
args : (Tensor x, int diagonal, bool lower)
args : (Tensor x, int diagonal)
output : Tensor(out)
infer_meta :
func : TrilInferMeta
......@@ -2257,6 +2257,15 @@
data_type : x
backward : trilinear_interp_grad
- op : triu
args : (Tensor x, int diagonal)
output : Tensor(out)
infer_meta :
func : TriuInferMeta
kernel :
func : triu
backward : triu_grad
- op : triu_indices
args : (int row, int col, int offset, DataType dtype, Place place={})
output : Tensor(out)
......
......@@ -1330,17 +1330,13 @@ void GraphSampleNeighborsInferMeta(const MetaTensor& row,
}
void HSigmoidLossInferMeta(const MetaTensor& x,
const MetaTensor& w,
const MetaTensor& label,
const MetaTensor& w,
const MetaTensor& bias,
const MetaTensor& path,
const MetaTensor& code,
const MetaTensor& bias,
int num_classes,
bool remote_prefetch,
int trainer_id,
const std::vector<int64_t>& height_sections,
const std::vector<std::string>& epmap,
const std::vector<std::string>& table_names,
bool is_sparse,
MetaTensor* out,
MetaTensor* pre_out,
......
......@@ -288,17 +288,13 @@ void GraphSampleNeighborsInferMeta(const MetaTensor& row,
MetaTensor* out_eids);
void HSigmoidLossInferMeta(const MetaTensor& x,
const MetaTensor& w,
const MetaTensor& label,
const MetaTensor& w,
const MetaTensor& bias,
const MetaTensor& path,
const MetaTensor& code,
const MetaTensor& bias,
int num_classes,
bool remote_prefetch,
int trainer_id,
const std::vector<int64_t>& height_sections,
const std::vector<std::string>& epmap,
const std::vector<std::string>& table_names,
bool is_sparse,
MetaTensor* out,
MetaTensor* pre_out,
......
......@@ -498,7 +498,6 @@ void LayerNormInferMeta(const MetaTensor& x,
const MetaTensor& bias,
float epsilon,
int begin_norm_axis,
bool is_test,
MetaTensor* out,
MetaTensor* mean,
MetaTensor* variance,
......
......@@ -87,7 +87,6 @@ void LayerNormInferMeta(const MetaTensor& x,
const MetaTensor& bias,
float epsilon,
int begin_norm_axis,
bool is_test,
MetaTensor* out,
MetaTensor* mean,
MetaTensor* variance,
......
......@@ -1882,8 +1882,8 @@ void LUInferMeta(const MetaTensor& x,
}
void MatrixRankInferMeta(const MetaTensor& x,
bool use_default_tol,
bool hermitian,
bool use_default_tol,
MetaTensor* out) {
auto dim_x = x.dims();
PADDLE_ENFORCE_GE(dim_x.size(),
......@@ -4156,10 +4156,10 @@ void UnbindInferMeta(const MetaTensor& x,
}
}
void TrilInferMeta(const MetaTensor& x,
int diagonal,
bool lower,
MetaTensor* out) {
void TrilTriuInferMeta(const MetaTensor& x,
int diagonal,
bool lower,
MetaTensor* out) {
const auto& x_dims = x.dims();
PADDLE_ENFORCE_GE(x_dims.size(),
2,
......@@ -4170,6 +4170,14 @@ void TrilInferMeta(const MetaTensor& x,
out->set_dtype(x.dtype());
}
void TrilInferMeta(const MetaTensor& x, int diagonal, MetaTensor* out) {
TrilTriuInferMeta(x, diagonal, true, out);
}
void TriuInferMeta(const MetaTensor& x, int diagonal, MetaTensor* out) {
TrilTriuInferMeta(x, diagonal, false, out);
}
void UnchangedInferMeta(const MetaTensor& x, MetaTensor* out) {
out->share_meta(x);
}
......
......@@ -264,8 +264,8 @@ void LUInferMeta(const MetaTensor& x,
void MatrixPowerInferMeta(const MetaTensor& x, int n, MetaTensor* out);
void MatrixRankInferMeta(const MetaTensor& x,
bool use_default_tol,
bool hermitian,
bool use_default_tol,
MetaTensor* out);
void MaxOutInferMeta(const MetaTensor& x,
......@@ -601,10 +601,14 @@ void TransposeGradInferMeta(const MetaTensor& x,
const std::vector<int>& axis,
MetaTensor* out);
void TrilInferMeta(const MetaTensor& x,
int diagonal,
bool lower,
MetaTensor* out);
void TrilInferMeta(const MetaTensor& x, int diagonal, MetaTensor* out);
void TriuInferMeta(const MetaTensor& x, int diagonal, MetaTensor* out);
void TrilTriuInferMeta(const MetaTensor& x,
int diagonal,
bool lower,
MetaTensor* out);
void UnbindInferMeta(const MetaTensor& x,
int axis,
......
......@@ -37,10 +37,6 @@ void HSigmoidLossGradKernelImpl(const Context& ctx,
const DenseTensor& out_grad,
int num_classes,
bool remote_prefetch,
int trainer_id,
const std::vector<int64_t>& height_sections,
const std::vector<std::string>& epmap,
const std::vector<std::string>& table_names,
bool is_sparse,
DenseTensor* x_grad,
DenseTensor* w_grad,
......
......@@ -32,10 +32,6 @@ void HSigmoidLossGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int num_classes,
bool remote_prefetch,
int trainer_id,
const std::vector<int64_t>& height_sections,
const std::vector<std::string>& epmap,
const std::vector<std::string>& table_names,
bool is_sparse,
DenseTensor* x_grad,
DenseTensor* w_grad,
......@@ -51,10 +47,6 @@ void HSigmoidLossGradKernel(const Context& ctx,
out_grad,
num_classes,
remote_prefetch,
trainer_id,
height_sections,
epmap,
table_names,
is_sparse,
x_grad,
w_grad,
......
......@@ -30,17 +30,13 @@ namespace math = paddle::operators::math;
template <typename T, typename Context>
void HSigmoidLossKernel(const Context& ctx,
const DenseTensor& x,
const DenseTensor& w,
const DenseTensor& label,
const DenseTensor& w,
const paddle::optional<DenseTensor>& bias,
const paddle::optional<DenseTensor>& path,
const paddle::optional<DenseTensor>& code,
const paddle::optional<DenseTensor>& bias,
int num_classes,
bool remote_prefetch,
int trainer_id,
const std::vector<int64_t>& height_sections,
const std::vector<std::string>& epmap,
const std::vector<std::string>& table_names,
bool is_sparse,
DenseTensor* out,
DenseTensor* pre_out,
......
......@@ -40,7 +40,6 @@ void LayerNormGradKernel(const Context& dev_ctx,
const DenseTensor& out_grad,
float epsilon,
int begin_norm_axis,
bool is_test,
DenseTensor* x_grad,
DenseTensor* scale_grad,
DenseTensor* bias_grad) {
......
......@@ -35,7 +35,6 @@ void LayerNormKernel(const Context& dev_ctx,
const paddle::optional<DenseTensor>& bias_opt,
float epsilon,
int begin_norm_axis,
bool is_test,
DenseTensor* y,
DenseTensor* mean,
DenseTensor* var) {
......
......@@ -24,8 +24,8 @@ template <typename T, typename Context>
void MatrixRankKernel(const Context& dev_ctx,
const DenseTensor& x,
float tol,
bool use_default_tol,
bool hermitian,
bool use_default_tol,
DenseTensor* out) {
DenseTensor atol_tensor;
if (use_default_tol) {
......
......@@ -14,7 +14,29 @@
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/tril_grad_kernel_impl.h"
#include "paddle/phi/kernels/impl/tril_triu_grad_kernel_impl.h"
PD_REGISTER_KERNEL(tril_triu_grad,
CPU,
ALL_LAYOUT,
phi::TrilTriuGradKernel,
bool,
float,
double,
int,
int64_t,
phi::dtype::float16) {}
PD_REGISTER_KERNEL(triu_grad,
CPU,
ALL_LAYOUT,
phi::TriuGradKernel,
bool,
float,
double,
int,
int64_t,
phi::dtype::float16) {}
PD_REGISTER_KERNEL(tril_grad,
CPU,
......
......@@ -14,7 +14,29 @@
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/tril_kernel_impl.h"
#include "paddle/phi/kernels/impl/tril_triu_kernel_impl.h"
PD_REGISTER_KERNEL(tril_triu,
CPU,
ALL_LAYOUT,
phi::TrilTriuKernel,
bool,
float,
double,
int,
int64_t,
phi::dtype::float16) {}
PD_REGISTER_KERNEL(triu,
CPU,
ALL_LAYOUT,
phi::TriuKernel,
bool,
float,
double,
int,
int64_t,
phi::dtype::float16) {}
PD_REGISTER_KERNEL(tril,
CPU,
......
......@@ -31,7 +31,6 @@ void LayerNormGradKernel(const Context &dev_ctx,
const DenseTensor &out_grad,
float epsilon,
int begin_norm_axis,
bool is_test,
DenseTensor *x_grad,
DenseTensor *scale_grad,
DenseTensor *bias_grad) {
......
......@@ -61,7 +61,6 @@ void LayerNormKernel(const Context &dev_ctx,
const paddle::optional<DenseTensor> &bias_opt,
float epsilon,
int begin_norm_axis,
bool is_test,
DenseTensor *y,
DenseTensor *mean,
DenseTensor *var) {
......
......@@ -23,7 +23,7 @@
#include "paddle/phi/kernels/funcs/slice.h"
#include "paddle/phi/kernels/impl/lstsq_kernel_impl.h"
#include "paddle/phi/kernels/impl/qr_kernel_impl.h"
#include "paddle/phi/kernels/impl/tril_kernel_impl.h"
#include "paddle/phi/kernels/impl/tril_triu_kernel_impl.h"
#include "paddle/phi/kernels/lstsq_kernel.h"
#include "paddle/phi/kernels/matmul_kernel.h"
#include "paddle/phi/kernels/transpose_kernel.h"
......@@ -110,7 +110,7 @@ void LstsqKernel(const Context& dev_ctx,
DenseTensor* res_r = new DenseTensor();
res_r->Resize(phi::make_ddim({batch_count, min_mn, min_mn}));
dev_ctx.template Alloc<T>(res_r);
phi::TrilKernel<T>(dev_ctx, slice_r, 0, false, res_r);
phi::TrilTriuKernel<T>(dev_ctx, slice_r, 0, false, res_r);
DenseTensor trans_y = phi::TransposeLast2Dim<T>(dev_ctx, tmp_y);
DenseTensor slice_y =
......@@ -135,7 +135,7 @@ void LstsqKernel(const Context& dev_ctx,
DenseTensor* res_r = new DenseTensor();
res_r->Resize(phi::make_ddim({batch_count, min_mn, min_mn}));
dev_ctx.template Alloc<T>(res_r);
phi::TrilKernel<T>(dev_ctx, slice_r, 0, false, res_r);
phi::TrilTriuKernel<T>(dev_ctx, slice_r, 0, false, res_r);
phi::TriangularSolveKernel<T, Context>(
dev_ctx, *res_r, *new_y, true, true, false, solution);
......
......@@ -27,8 +27,8 @@ template <typename T, typename Context>
void MatrixRankKernel(const Context& dev_ctx,
const DenseTensor& x,
float tol,
bool use_default_tol,
bool hermitian,
bool use_default_tol,
DenseTensor* out) {
DenseTensor atol_tensor;
if (use_default_tol) {
......
......@@ -31,7 +31,7 @@
#include "paddle/phi/kernels/qr_kernel.h"
#include "paddle/phi/kernels/slice_kernel.h"
#include "paddle/phi/kernels/transpose_kernel.h"
#include "paddle/phi/kernels/tril_kernel.h"
#include "paddle/phi/kernels/tril_triu_kernel.h"
namespace phi {
......@@ -103,12 +103,12 @@ void QrKernel(const Context& ctx,
auto trans_qr = TransposeLast2Dim<T, Context>(ctx, qr);
auto sliced_qr = SliceKernel<T, Context>(
ctx, trans_qr, {trans_qr.dims().size() - 2}, {0}, {min_mn}, {1}, {});
auto tmp_r = Tril<T, Context>(ctx, sliced_qr, 0, false);
auto tmp_r = TrilTriu<T, Context>(ctx, sliced_qr, 0, false);
// Transpose 'tmp_r' to retore the original row-major order
phi::Copy(ctx, tmp_r, r->place(), false, r);
} else {
auto trans_qr = TransposeLast2Dim<T, Context>(ctx, qr);
auto tmp_r = Tril<T, Context>(ctx, trans_qr, 0, false);
auto tmp_r = TrilTriu<T, Context>(ctx, trans_qr, 0, false);
// Transpose 'tmp_r' to retore the original row-major order
phi::Copy(ctx, tmp_r, r->place(), false, r);
}
......
......@@ -14,7 +14,7 @@
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/tril_grad_kernel_impl.h"
#include "paddle/phi/kernels/impl/tril_triu_grad_kernel_impl.h"
PD_REGISTER_KERNEL(tril_grad,
GPU,
......@@ -26,3 +26,25 @@ PD_REGISTER_KERNEL(tril_grad,
int,
int64_t,
phi::dtype::float16) {}
PD_REGISTER_KERNEL(triu_grad,
GPU,
ALL_LAYOUT,
phi::TriuGradKernel,
bool,
float,
double,
int,
int64_t,
phi::dtype::float16) {}
PD_REGISTER_KERNEL(tril_triu_grad,
GPU,
ALL_LAYOUT,
phi::TrilTriuGradKernel,
bool,
float,
double,
int,
int64_t,
phi::dtype::float16) {}
......@@ -14,7 +14,29 @@
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/tril_kernel_impl.h"
#include "paddle/phi/kernels/impl/tril_triu_kernel_impl.h"
PD_REGISTER_KERNEL(tril_triu,
GPU,
ALL_LAYOUT,
phi::TrilTriuKernel,
bool,
float,
double,
int,
int64_t,
phi::dtype::float16) {}
PD_REGISTER_KERNEL(triu,
GPU,
ALL_LAYOUT,
phi::TriuKernel,
bool,
float,
double,
int,
int64_t,
phi::dtype::float16) {}
PD_REGISTER_KERNEL(tril,
GPU,
......
......@@ -30,10 +30,6 @@ void HSigmoidLossGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int num_classes,
bool remote_prefetch,
int trainer_id,
const std::vector<int64_t>& height_sections,
const std::vector<std::string>& epmap,
const std::vector<std::string>& table_names,
bool is_sparse,
DenseTensor* x_grad,
DenseTensor* w_grad,
......
......@@ -21,17 +21,13 @@ namespace phi {
template <typename T, typename Context>
void HSigmoidLossKernel(const Context& ctx,
const DenseTensor& x,
const DenseTensor& w,
const DenseTensor& label,
const DenseTensor& w,
const paddle::optional<DenseTensor>& bias,
const paddle::optional<DenseTensor>& path,
const paddle::optional<DenseTensor>& code,
const paddle::optional<DenseTensor>& bias,
int num_classes,
bool remote_prefetch,
int trainer_id,
const std::vector<int64_t>& height_sections,
const std::vector<std::string>& epmap,
const std::vector<std::string>& table_names,
bool is_sparse,
DenseTensor* out,
DenseTensor* pre_out,
......
......@@ -29,7 +29,7 @@
#include "paddle/phi/kernels/slice_kernel.h"
#include "paddle/phi/kernels/transpose_kernel.h"
#include "paddle/phi/kernels/triangular_solve_kernel.h"
#include "paddle/phi/kernels/tril_kernel.h"
#include "paddle/phi/kernels/tril_triu_kernel.h"
namespace phi {
......@@ -116,8 +116,8 @@ void QrGradKernel(const Context& ctx,
DenseTensor M_tmp1 = Subtract<T, Context>(ctx, R_term, Q_term);
// Compute M = (tril(M) + tril(M).mH()) * 0.5 Identity
DenseTensor M_tril_0 = Tril<T, Context>(ctx, M_tmp1, 0, true);
DenseTensor M_tril_1 = Tril<T, Context>(ctx, M_tmp1, -1, true);
DenseTensor M_tril_0 = TrilTriu<T, Context>(ctx, M_tmp1, 0, true);
DenseTensor M_tril_1 = TrilTriu<T, Context>(ctx, M_tmp1, -1, true);
DenseTensor M = Add<T, Context>(
ctx, M_tril_0, TransposeLast2Dim<T, Context>(ctx, M_tril_1));
......
......@@ -16,16 +16,16 @@
#include "paddle/phi/kernels/funcs/for_range.h"
#include "paddle/phi/kernels/funcs/tril_triu_compute.h"
#include "paddle/phi/kernels/tril_grad_kernel.h"
#include "paddle/phi/kernels/tril_triu_grad_kernel.h"
namespace phi {
template <typename T, typename Context>
void TrilGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int diagonal,
bool lower,
DenseTensor* x_grad) {
void TrilTriuGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int diagonal,
bool lower,
DenseTensor* x_grad) {
const auto* dout_data = out_grad.data<T>();
auto* dx_data = ctx.template Alloc<T>(x_grad);
......@@ -40,4 +40,20 @@ void TrilGradKernel(const Context& ctx,
for_range(tril_triu_grad_computer);
}
template <typename T, typename Context>
void TrilGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int diagonal,
DenseTensor* x_grad) {
TrilTriuGradKernel<T, Context>(ctx, out_grad, diagonal, true, x_grad);
}
template <typename T, typename Context>
void TriuGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int diagonal,
DenseTensor* x_grad) {
TrilTriuGradKernel<T, Context>(ctx, out_grad, diagonal, false, x_grad);
}
} // namespace phi
......@@ -16,16 +16,16 @@
#include "paddle/phi/kernels/funcs/for_range.h"
#include "paddle/phi/kernels/funcs/tril_triu_compute.h"
#include "paddle/phi/kernels/tril_kernel.h"
#include "paddle/phi/kernels/tril_triu_kernel.h"
namespace phi {
template <typename T, typename Context>
void TrilKernel(const Context& ctx,
const DenseTensor& x,
int diagonal,
bool lower,
DenseTensor* out) {
void TrilTriuKernel(const Context& ctx,
const DenseTensor& x,
int diagonal,
bool lower,
DenseTensor* out) {
const auto* x_data = x.data<T>();
auto* out_data = ctx.template Alloc<T>(out);
......@@ -39,4 +39,20 @@ void TrilKernel(const Context& ctx,
for_range(tril_triu_computer);
}
template <typename T, typename Context>
void TrilKernel(const Context& ctx,
const DenseTensor& x,
int diagonal,
DenseTensor* out) {
TrilTriuKernel<T, Context>(ctx, x, diagonal, true, out);
}
template <typename T, typename Context>
void TriuKernel(const Context& ctx,
const DenseTensor& x,
int diagonal,
DenseTensor* out) {
TrilTriuKernel<T, Context>(ctx, x, diagonal, false, out);
}
} // namespace phi
......@@ -28,7 +28,6 @@ void LayerNormGradKernel(const Context& ctx,
const DenseTensor& out_grad,
float epsilon,
int begin_norm_axis,
bool is_test,
DenseTensor* x_grad,
DenseTensor* scale_grad,
DenseTensor* bias_grad);
......
......@@ -26,7 +26,6 @@ void LayerNormKernel(const Context& ctx,
const paddle::optional<DenseTensor>& bias,
float epsilon,
int begin_norm_axis,
bool is_test,
DenseTensor* out,
DenseTensor* mean,
DenseTensor* variance);
......
......@@ -22,8 +22,8 @@ template <typename T, typename Context>
void MatrixRankKernel(const Context& dev_ctx,
const DenseTensor& x,
float tol,
bool use_default_tol,
bool hermitian,
bool use_default_tol,
DenseTensor* out);
} // namespace phi
......@@ -47,10 +47,6 @@ void HSigmoidLossGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int num_classes,
bool remote_prefetch,
int trainer_id,
const std::vector<int64_t>& height_sections,
const std::vector<std::string>& epmap,
const std::vector<std::string>& table_names,
bool is_sparse,
DenseTensor* x_grad,
SelectedRows* w_grad,
......@@ -77,10 +73,6 @@ void HSigmoidLossGradKernel(const Context& ctx,
out_grad,
num_classes,
remote_prefetch,
trainer_id,
height_sections,
epmap,
table_names,
is_sparse,
x_grad,
w_grad_value,
......
......@@ -32,10 +32,6 @@ void HSigmoidLossGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int num_classes,
bool remote_prefetch,
int trainer_id,
const std::vector<int64_t>& height_sections,
const std::vector<std::string>& epmap,
const std::vector<std::string>& table_names,
bool is_sparse,
DenseTensor* x_grad,
SelectedRows* w_grad,
......
......@@ -18,11 +18,23 @@
namespace phi {
template <typename T, typename Context>
void TrilTriuGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int diagonal,
bool lower,
DenseTensor* x_grad);
template <typename T, typename Context>
void TrilGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int diagonal,
bool lower,
DenseTensor* x_grad);
template <typename T, typename Context>
void TriuGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int diagonal,
DenseTensor* x_grad);
} // namespace phi
......@@ -19,22 +19,34 @@
namespace phi {
template <typename T, typename Context>
void TrilTriuKernel(const Context& ctx,
const DenseTensor& x,
int diagonal,
bool lower,
DenseTensor* out);
template <typename T, typename Context>
void TrilKernel(const Context& ctx,
const DenseTensor& x,
int diagonal,
bool lower,
DenseTensor* out);
template <typename T, typename Context>
DenseTensor Tril(const Context& ctx,
const DenseTensor& x,
int diagonal,
bool lower) {
void TriuKernel(const Context& ctx,
const DenseTensor& x,
int diagonal,
DenseTensor* out);
template <typename T, typename Context>
DenseTensor TrilTriu(const Context& ctx,
const DenseTensor& x,
int diagonal,
bool lower) {
DenseTensor dense_out;
MetaTensor meta_out(&dense_out);
TrilInferMeta(x, diagonal, lower, &meta_out);
TrilKernel<T, Context>(ctx, x, diagonal, lower, &dense_out);
TrilTriuInferMeta(x, diagonal, lower, &meta_out);
TrilTriuKernel<T, Context>(ctx, x, diagonal, lower, &dense_out);
return dense_out;
}
......
......@@ -29,7 +29,6 @@ void LayerNormGradKernel(const Context& ctx,
const DenseTensor& out_grad,
float epsilon,
int begin_norm_axis,
bool is_test,
DenseTensor* x_grad,
DenseTensor* scale_grad,
DenseTensor* bias_grad) {
......
......@@ -26,7 +26,6 @@ void LayerNormKernel(const Context& ctx,
const paddle::optional<DenseTensor>& bias,
float epsilon,
int begin_norm_axis,
bool is_test,
DenseTensor* out,
DenseTensor* mean,
DenseTensor* variance) {
......
......@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle/phi/kernels/tril_grad_kernel.h"
#include "paddle/phi/kernels/tril_triu_grad_kernel.h"
#include "paddle/phi/backends/xpu/enforce_xpu.h"
#include "paddle/phi/core/kernel_registry.h"
......@@ -20,11 +20,11 @@
namespace phi {
template <typename T, typename Context>
void TrilGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int diagonal,
bool lower,
DenseTensor* x_grad) {
void TrilTriuGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int diagonal,
bool lower,
DenseTensor* x_grad) {
using XPUType = typename XPUTypeTrait<T>::Type;
ctx.template Alloc<T>(x_grad);
auto dy_shape = vectorize<int>(out_grad.dims());
......@@ -46,7 +46,27 @@ void TrilGradKernel(const Context& ctx,
}
}
template <typename T, typename Context>
void TrilGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int diagonal,
DenseTensor* x_grad) {
TrilTriuGradKernel<T, Context>(ctx, out_grad, diagonal, true, x_grad);
}
template <typename T, typename Context>
void TriuGradKernel(const Context& ctx,
const DenseTensor& out_grad,
int diagonal,
DenseTensor* x_grad) {
TrilTriuGradKernel<T, Context>(ctx, out_grad, diagonal, false, x_grad);
}
} // namespace phi
PD_REGISTER_KERNEL(
tril_grad, XPU, ALL_LAYOUT, phi::TrilGradKernel, int, float) {}
PD_REGISTER_KERNEL(
triu_grad, XPU, ALL_LAYOUT, phi::TriuGradKernel, int, float) {}
PD_REGISTER_KERNEL(
tril_triu_grad, XPU, ALL_LAYOUT, phi::TrilTriuGradKernel, int, float) {}
......@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle/phi/kernels/tril_kernel.h"
#include "paddle/phi/kernels/tril_triu_kernel.h"
#include "paddle/phi/backends/xpu/enforce_xpu.h"
#include "paddle/phi/core/kernel_registry.h"
......@@ -20,11 +20,11 @@
namespace phi {
template <typename T, typename Context>
void TrilKernel(const Context& ctx,
const DenseTensor& x,
int diagonal,
bool lower,
DenseTensor* out) {
void TrilTriuKernel(const Context& ctx,
const DenseTensor& x,
int diagonal,
bool lower,
DenseTensor* out) {
using XPUType = typename XPUTypeTrait<T>::Type;
ctx.template Alloc<T>(out);
auto xshape = vectorize<int>(x.dims());
......@@ -46,6 +46,25 @@ void TrilKernel(const Context& ctx,
}
}
template <typename T, typename Context>
void TrilKernel(const Context& ctx,
const DenseTensor& x,
int diagonal,
DenseTensor* out) {
TrilTriuKernel<T, Context>(ctx, x, diagonal, true, out);
}
template <typename T, typename Context>
void TriuKernel(const Context& ctx,
const DenseTensor& x,
int diagonal,
DenseTensor* out) {
TrilTriuKernel<T, Context>(ctx, x, diagonal, false, out);
}
} // namespace phi
PD_REGISTER_KERNEL(
tril_triu, XPU, ALL_LAYOUT, phi::TrilTriuKernel, int, float) {}
PD_REGISTER_KERNEL(tril, XPU, ALL_LAYOUT, phi::TrilKernel, int, float) {}
PD_REGISTER_KERNEL(triu, XPU, ALL_LAYOUT, phi::TriuKernel, int, float) {}
......@@ -19,14 +19,8 @@ namespace phi {
KernelSignature HierarchicalSigmoidOpArgumentMapping(
const ArgumentMappingContext& ctx) {
return KernelSignature("hsigmoid_loss",
{"X", "W", "Label", "PathTable", "PathCode", "Bias"},
{"num_classes",
"remote_prefetch",
"trainer_id",
"height_sections",
"epmap",
"table_names",
"is_sparse"},
{"X", "Label", "W", "Bias", "PathTable", "PathCode"},
{"num_classes", "remote_prefetch", "is_sparse"},
{"Out", "PreOut", "W_Out"});
}
......@@ -42,13 +36,7 @@ KernelSignature HierarchicalSigmoidGradOpArgumentMapping(
"Bias",
"PreOut",
"Out@GRAD"},
{"num_classes",
"remote_prefetch",
"trainer_id",
"height_sections",
"epmap",
"table_names",
"is_sparse"},
{"num_classes", "remote_prefetch", "is_sparse"},
{"X@GRAD", "W@GRAD", "Bias@GRAD"});
} else if (ctx.IsSelectedRowsOutput("W@GRAD")) {
return KernelSignature("hsigmoid_loss_grad_sr",
......@@ -60,13 +48,7 @@ KernelSignature HierarchicalSigmoidGradOpArgumentMapping(
"Bias",
"PreOut",
"Out@GRAD"},
{"num_classes",
"remote_prefetch",
"trainer_id",
"height_sections",
"epmap",
"table_names",
"is_sparse"},
{"num_classes", "remote_prefetch", "is_sparse"},
{"X@GRAD", "W@GRAD", "Bias@GRAD"});
} else {
return KernelSignature("unregistered", {}, {}, {});
......
......@@ -19,7 +19,7 @@ namespace phi {
KernelSignature LayerNormOpArgumentMapping(const ArgumentMappingContext& ctx) {
return KernelSignature("layer_norm",
{"X", "Scale", "Bias"},
{"epsilon", "begin_norm_axis", "is_test"},
{"epsilon", "begin_norm_axis"},
{"Y", "Mean", "Variance"});
}
......@@ -27,7 +27,7 @@ KernelSignature LayerNormGradOpArgumentMapping(
const ArgumentMappingContext& ctx) {
return KernelSignature("layer_norm_grad",
{"X", "Scale", "Bias", "Mean", "Variance", "Y@GRAD"},
{"epsilon", "begin_norm_axis", "is_test"},
{"epsilon", "begin_norm_axis"},
{"X@GRAD", "Scale@GRAD", "Bias@GRAD"});
}
......
......@@ -28,8 +28,8 @@ KernelSignature MatrixRankOpArgumentMapping(const ArgumentMappingContext& ctx) {
{"X"},
{
"tol",
"use_default_tol",
"hermitian",
"use_default_tol",
},
{"Out"});
}
......
......@@ -17,19 +17,16 @@ limitations under the License. */
namespace phi {
KernelSignature TrilTriuOpArgumentMapping(const ArgumentMappingContext& ctx) {
return KernelSignature("tril", {"X"}, {"diagonal", "lower"}, {"Out"});
return KernelSignature("tril_triu", {"X"}, {"diagonal", "lower"}, {"Out"});
}
KernelSignature TrilTriuGradOpArgumentMapping(
const ArgumentMappingContext& ctx) {
return KernelSignature(
"tril_grad", {"Out@GRAD"}, {"diagonal", "lower"}, {"X@GRAD"});
"tril_triu_grad", {"Out@GRAD"}, {"diagonal", "lower"}, {"X@GRAD"});
}
} // namespace phi
PD_REGISTER_BASE_KERNEL_NAME(tril_triu, tril);
PD_REGISTER_BASE_KERNEL_NAME(tril_triu_grad, tril_grad);
PD_REGISTER_ARG_MAPPING_FN(tril_triu, phi::TrilTriuOpArgumentMapping);
PD_REGISTER_ARG_MAPPING_FN(tril_triu_grad, phi::TrilTriuGradOpArgumentMapping);
......@@ -1782,7 +1782,6 @@ class LayerNorm(layers.Layer):
self.bias,
self._epsilon,
self._begin_norm_axis,
False,
)
return dygraph_utils._append_activation_in_dygraph(
pre_act, act=self._act
......
......@@ -170,18 +170,15 @@ def hsigmoidWithCustomTree(
def python_api(
input,
weight,
label,
weight,
bias=None,
path_table=None,
path_code=None,
bias=None,
num_classes=-1,
is_sparse=False,
remote_prefetch=False,
):
assert (
is_sparse == remote_prefetch
), "is_sparse is equal to remote_prefetch in dygraph."
return paddle.nn.functional.hsigmoid_loss(
input,
label,
......
......@@ -26,7 +26,7 @@ SEED = 2049
np.random.seed(SEED)
def matrix_rank_wraper(x, tol=None, use_default_tol=True, hermitian=False):
def matrix_rank_wraper(x, tol=None, hermitian=False, use_default_tol=True):
return paddle.linalg.matrix_rank(x, tol, hermitian)
......
......@@ -1017,17 +1017,13 @@ def hsigmoid_loss(
if in_dygraph_mode():
out, _, _ = _C_ops.hsigmoid_loss(
input,
weight,
label,
weight,
bias,
path_table,
path_code,
bias,
num_classes,
is_sparse,
0,
[],
[],
[],
is_sparse,
)
return out
......
......@@ -385,7 +385,7 @@ def layer_norm(
pre_act,
_,
_,
) = _C_ops.layer_norm(x, weight, bias, epsilon, begin_norm_axis, False)
) = _C_ops.layer_norm(x, weight, bias, epsilon, begin_norm_axis)
return dygraph_utils._append_activation_in_dygraph(pre_act, act=None)
......
......@@ -1183,7 +1183,7 @@ def triu(x, diagonal=0, name=None):
"""
if in_dygraph_mode():
return _C_ops.tril(x, diagonal, False)
return _C_ops.triu(x, diagonal, False)
if _in_legacy_dygraph():
op = getattr(_legacy_C_ops, 'tril_triu')
......
......@@ -1601,7 +1601,7 @@ def matrix_rank(x, tol=None, hermitian=False, name=None):
else:
tol_attr = float(tol)
use_default_tol = False
return _C_ops.matrix_rank(x, tol_attr, use_default_tol, hermitian)
return _C_ops.matrix_rank(x, tol_attr, hermitian, use_default_tol)
if _in_legacy_dygraph():
if tol is None:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册