Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion paddle/fluid/pir/dialect/operator/ir/update_ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
support_tensor : [start, end, step]

- op : sequence_mask
args: (Tensor x, Scalar(int) max_len, int out_dtype)
args: (Tensor x, Scalar(int) max_len, DataType out_dtype)
output: Tensor(y)
infer_meta:
func: SequenceMaskScalarInferMeta
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/api/yaml/legacy_ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@
backward : rrelu_grad

- op : sequence_mask
args: (Tensor x, Scalar(int) max_len, int out_dtype)
args: (Tensor x, Scalar(int) max_len, DataType out_dtype)
output: Tensor(y)
infer_meta:
func: SequenceMaskScalarInferMeta
Expand Down
5 changes: 2 additions & 3 deletions paddle/phi/infermeta/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2835,7 +2835,7 @@ void SearchsortedInferMeta(const MetaTensor& sorted_sequence,
void SequenceMaskInferMeta(const MetaTensor& x,
const MetaTensor& max_len_tensor,
int maxlen,
int out_dtype,
DataType out_dtype,
MetaTensor* y) {
auto dim = common::vectorize<int>(x.dims());

Expand All @@ -2846,8 +2846,7 @@ void SequenceMaskInferMeta(const MetaTensor& x,
}

y->set_dims(common::make_ddim(dim));
auto out_phi_dtype = phi::TransToPhiDataType(out_dtype);
y->set_dtype(out_phi_dtype);
y->set_dtype(out_dtype);
}

void SoftmaxMaskFuseInferMeta(const MetaTensor& x,
Expand Down
3 changes: 2 additions & 1 deletion paddle/phi/infermeta/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License. */

#pragma once

#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/meta_tensor.h"
Expand Down Expand Up @@ -452,7 +453,7 @@ void SearchsortedInferMeta(const MetaTensor& sorted_sequence,
void SequenceMaskInferMeta(const MetaTensor& x,
const MetaTensor& max_len_tensor,
int maxlen,
int out_dtype,
DataType out_dtype,
MetaTensor* y);

void SoftmaxMaskFuseInferMeta(const MetaTensor& x,
Expand Down
5 changes: 2 additions & 3 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4018,14 +4018,13 @@ void SplitWithNumInferMeta(const MetaTensor& x,

void SequenceMaskScalarInferMeta(const MetaTensor& x,
const Scalar& max_len,
int out_dtype,
DataType out_dtype,
MetaTensor* y) {
auto dim = phi::vectorize<int>(x.dims());
int maxlen = max_len.to<int>();
dim.push_back(maxlen > 0 ? maxlen : -1);
y->set_dims(phi::make_ddim(dim));
auto out_phi_dtype = phi::TransToPhiDataType(out_dtype);
y->set_dtype(out_phi_dtype);
y->set_dtype(out_dtype);
}

void SquaredL2NormInferMeta(const MetaTensor& x, MetaTensor* out) {
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/infermeta/unary.h
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ void RReluGradInferMeta(const MetaTensor& out_grad,

void SequenceMaskScalarInferMeta(const MetaTensor& x,
const Scalar& max_len,
int out_dtype,
DataType out_dtype,
MetaTensor* y);

void SetValueInferMeta(const MetaTensor& x, MetaTensor* out);
Expand Down
6 changes: 3 additions & 3 deletions paddle/phi/kernels/impl/sequence_mask_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ template <typename T, typename Context>
void SequenceMaskScalarKernel(const Context& ctx,
const DenseTensor& x,
const Scalar& max_len,
int out_dtype,
DataType out_dtype,
DenseTensor* y) {
int maxlen = max_len.to<int>();
auto* x_data = x.data<T>();
Expand All @@ -58,7 +58,7 @@ void SequenceMaskScalarKernel(const Context& ctx,
y->Resize(common::make_ddim(y_dim));
}

phi::VisitDataType(phi::TransToPhiDataType(out_dtype),
phi::VisitDataType(out_dtype,
phi::funcs::SequenceMaskFunctor<Context, T>(
ctx, x_data, y, x_numel * maxlen, maxlen));
}
Expand All @@ -68,7 +68,7 @@ void SequenceMaskKernel(const Context& ctx,
const DenseTensor& x,
const paddle::optional<DenseTensor>& max_len_tensor,
int maxlen,
int out_dtype,
DataType out_dtype,
DenseTensor* y) {
if (max_len_tensor) {
bool is_gpu_place = ctx.GetPlace().GetType() == phi::AllocationType::GPU;
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/sequence_mask_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void SequenceMaskKernel(const Context& ctx,
const DenseTensor& x,
const paddle::optional<DenseTensor>& max_len_tensor,
int maxlen,
int out_dtype,
DataType out_dtype,
DenseTensor* y);

} // namespace phi
13 changes: 9 additions & 4 deletions python/paddle/nn/functional/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@


from paddle import _C_ops, tensor
from paddle.pir.core import vartype_to_datatype
from paddle.utils import deprecated

from ...base.data_feeder import check_type, check_variable_and_dtype
Expand All @@ -25,6 +26,7 @@
convert_np_dtype_to_dtype_,
core,
in_dynamic_or_pir_mode,
in_pir_mode,
)

__all__ = []
Expand Down Expand Up @@ -103,10 +105,13 @@ def sequence_mask(x, maxlen=None, dtype='int64', name=None):
if in_dynamic_or_pir_mode():
if not isinstance(dtype, (core.VarDesc.VarType, core.DataType)):
dtype = convert_np_dtype_to_dtype_(dtype)
if maxlen is not None:
out = _C_ops.sequence_mask(x, maxlen, dtype)
out.stop_gradient = True
return out
if maxlen is None:
maxlen = -1
if in_pir_mode() and isinstance(dtype, core.VarDesc.VarType):
dtype = vartype_to_datatype[dtype]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

与震哥对齐,这个 API 最好不要加这个兼容逻辑,已经发现 OpTest 存在的问题,问题先记录了,这个 PR 暂时 delay 处理

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

依赖已解,继续推进本 PR

out = _C_ops.sequence_mask(x, maxlen, dtype)
out.stop_gradient = True
return out

helper = LayerHelper('sequence_mask', **locals())
out = helper.create_variable_for_type_inference(dtype=dtype)
Expand Down