Skip to content

[InferMeta] Add input validation for FusedRopeInferMeta#78333

Closed
wanghuancoder wants to merge 5 commits intoPaddlePaddle:developfrom
wanghuancoder:FusedRopeInferMeta_check2
Closed

[InferMeta] Add input validation for FusedRopeInferMeta#78333
wanghuancoder wants to merge 5 commits intoPaddlePaddle:developfrom
wanghuancoder:FusedRopeInferMeta_check2

Conversation

@wanghuancoder
Copy link
Copy Markdown
Contributor

PR Category

Operator Mechanism

PR Types

Improvements

Description

FusedRopeInferMeta 添加全面的输入参数验证:

  1. 基础参数校验

    • head_dim 必须是 2 的倍数
    • batch_size, seq_len, num_heads, head_dim 必须大于 0
  2. 可选输入 k, v 校验

    • 维度必须与 q 相同
  3. 可选输入 sin, cos 校验

    • 必须是 2-D [seq_len, head_dim] 或 4-D [1, seq_len, 1, head_dim]
    • head_dim 必须是 2 的倍数
    • seq_len 和 head_dim 必须与 q 匹配
    • sin 和 cos 必须同时存在或同时不存在
  4. position_ids 校验

    • 必须是 2-D [batch_size, seq_len]

这些验证可以提前捕获潜在的输入错误,提供更友好的错误信息。

是否引起精度变化

…kernels

When processing tensors with zero dimensions (e.g., shape (0, 10) or (16, 0)),
the cross_entropy kernels encounter invalid configuration arguments because:
1. std::log2(0) is undefined behavior, resulting in -inf
2. The computed block/grid dimensions become invalid

This fix adds zero-dimension checks at the entry of affected kernel functions
to avoid launching kernels with invalid configurations.

Files modified:
- paddle/phi/kernels/funcs/cross_entropy.cu
- paddle/phi/kernels/gpu/cross_entropy_kernel.cu
- paddle/phi/kernels/gpu/cross_entropy_grad_kernel.cu
- paddle/phi/kernels/gpu/cross_entropy_bwd_w_downcast.cu
When calling paddle.full_like with int64 max value (9223372036854775807),
the value was incorrectly converted to -9223372036854775808 due to:
1. Converting the value to double (which has only 53 bits of precision)
2. Converting back to int64 (causing overflow)

This fix directly converts the value to the target type without going
through double for integer types, while still using double for inf/nan
checking in floating-point types.
Add comprehensive input validation for FusedRopeInferMeta:
- Validate head_dim is a multiple of 2
- Validate batch_size, seq_len, num_heads, head_dim are greater than 0
- Validate optional inputs k, v have same dims as q
- Validate optional inputs sin, cos have correct shape [seq_len, head_dim] or [1, seq_len, 1, head_dim]
- Validate sin and cos must be both provided or both absent
- Validate position_ids has shape [batch_size, seq_len]
@paddle-bot
Copy link
Copy Markdown

paddle-bot Bot commented Mar 17, 2026

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant