Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -2116,12 +2116,86 @@ bool RmsNormOpInferSymbolicShape(
// return true;
// }

// bool SequenceConvOpInferSymbolicShape(pir::Operation *op,
// pir::InferSymbolicShapeContext
// *infer_context) {
// // pass
// return true;
// }
bool SequenceConvOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
// 获取输入张量的符号形状和数据类型
const auto &x_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(0));
const auto &padding_data_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(1));
const auto &filter_shape_or_data =
infer_context->GetShapeOrDataForValue(op->operand_source(2));
const std::vector<symbol::DimExpr> &x_dims = x_shape_or_data.shape();
const std::vector<symbol::DimExpr> &filter_dims =
filter_shape_or_data.shape();

int context_length =
op->attribute<pir::Int32Attribute>("context_length").data();
bool padding_trainable =
op->attribute<pir::BoolAttribute>("padding_trainable").data();
int context_start =
op->attribute<pir::Int32Attribute>("context_start").data();
int context_stride =
op->attribute<pir::Int32Attribute>("context_stride").data();

PADDLE_ENFORCE_EQ(
context_stride,
1,
common::errors::InvalidArgument(
"Currently, SequenceConvOp only supports contextStride=1. But "
"received contextStride = %u.",
context_stride));
PADDLE_ENFORCE_EQ(
x_dims.size() == 2 && filter_dims.size() == 2,
true,
common::errors::InvalidArgument(
"Input(X, Filter) should be 2-D tensor. But received Input(X): "
"input rank %u, input shape [%s]; received Input(Filter): "
"input rank %u, input shape [%s].",
x_dims.size(),
x_dims,
filter_dims.size(),
filter_dims));
auto divisor = symbol::DimExpr(context_length);
infer_context->AddEqualCstr(filter_dims[0], divisor * x_dims[1]);

if (padding_trainable) {
const std::vector<symbol::DimExpr> &padding_dims =
padding_data_shape_or_data.shape();
int up_pad = std::max(0, -context_start);
int down_pad = std::max(0, context_start + context_length - 1);
int total_pad = up_pad + down_pad;
const auto &input_width = x_dims[1];
bool start_equals_zero = context_start == 0;
bool length_equals_one = context_length == 1;
bool start_length = start_equals_zero && length_equals_one;

PADDLE_ENFORCE_EQ(
start_length,
false,
common::errors::InvalidArgument(
"If context_start is 0 and context_length is 1, paddingTrainable "
"should be false."));
PADDLE_ENFORCE_EQ(
padding_dims.size(),
2,
common::errors::InvalidArgument(
"Input(PaddingData) should be 2-D tensor. But received: "
"input rank %u, input shape [%s].",
padding_dims.size(),
padding_dims));
infer_context->AddEqualCstr(padding_dims[0], symbol::DimExpr(total_pad));
infer_context->AddEqualCstr(padding_dims[1], input_width);
}

std::vector<symbol::DimExpr> out_dims = x_dims;
out_dims[1] = filter_dims[1];
infer_context->SetShapeOrDataForValue(
op->result(0),
symbol::ShapeOrDataDimExprs{symbol::TensorShapeOrDataDimExprs(out_dims)});

return true;
}

bool SparseAttentionOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(RoiPool)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(Rnn)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(RoiAlign)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(SpectralNorm)
// OP_DECLARE_INFER_SYMBOLIC_SHAPE(SequenceConv)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(SequenceConv)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(SparseAttention)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Stack)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(SigmoidCrossEntropyWithLogits)
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/ops/yaml/ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4180,7 +4180,7 @@
data_type: x
optional: padding_data
backward: sequence_conv_grad
# interfaces : paddle::dialect::InferSymbolicShapeInterface
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : sequence_mask
args: (Tensor x, Scalar(int) max_len, DataType out_dtype)
Expand Down