Skip to content

[Cpp API Compatibility] Fix arange default dtype#78552

Merged
SigureMo merged 3 commits intoPaddlePaddle:developfrom
youge325:fix-arange-default-dtype
Apr 2, 2026
Merged

[Cpp API Compatibility] Fix arange default dtype#78552
SigureMo merged 3 commits intoPaddlePaddle:developfrom
youge325:fix-arange-default-dtype

Conversation

@youge325
Copy link
Copy Markdown
Contributor

@youge325 youge325 commented Apr 1, 2026

PR Category

Execute Infrastructure

PR Types

Bug fixes

Description

拆分自 #78484

修复 at::arange 在不指定 dtype 时创建的 tensor 数据类型不是 kLong 的错误,用于 DeepGEMM 的对齐。

变更详情

问题描述

调用方式 PyTorch 推断类型 Paddle 历史行为
arange(5) kLong (int64) kFloat
arange(2, 7) kLong kFloat
arange(1, 10, 2) kLong kFloat

修复内容 (ATen/ops/arange.h)

实现与 PyTorch 一致的类型推断逻辑:

// 当不指定 dtype 时:
// - 整数输入(如 5, 2, 7, 1, 10, 2)→ 推断为 kLong
// - 浮点输入(如 5.0, 0.0, 1.0, 0.1)→ 推断为当前默认浮点 dtype

关键修改:

  • 添加 is_integral 类型判断
  • 整数输入默认推断为 kLong (int64)
  • 浮点输入继续跟随当前默认浮点 dtype

回归测试补充 (test/cpp/compat/ATen_factory_default_dtype_test.cc)

  • ArangeNoDtypeInt:整数输入的 dtype 推断
  • ArangeNoDtypeFloat:浮点输入的 dtype 推断
  • 覆盖直接调用和 dtype=nullopt 两种路径

对齐效果

测试用例 修复前 Paddle 修复后 Paddle PyTorch
NoDtypeWithEndInt scalar_type=6 (kFloat) scalar_type=4 (kLong) scalar_type=4 (kLong)

相关文档

是否引起精度变化

Copilot AI review requested due to automatic review settings April 1, 2026 11:15
@paddle-bot
Copy link
Copy Markdown

paddle-bot bot commented Apr 1, 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.

@paddle-bot paddle-bot bot added the contributor External developers label Apr 1, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes ATen compat arange dtype inference to better match PyTorch behavior, especially when dtype is omitted, and expands C++ compatibility tests to cover the updated overloads and defaults.

Changes:

  • Update compat at::arange to default to kLong when inputs are integral and dtype is omitted, while keeping current default dtype for floating inputs.
  • Refactor arange overloads to funnel through a common (start, end, step, options) implementation.
  • Extend C++ tests to cover more arange overloads, pinned-memory behavior, and default-dtype expectations.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
paddle/phi/api/include/compat/ATen/ops/arange.h Adjusts dtype resolution for omitted dtype and consolidates overload implementations.
test/cpp/compat/ATen_pin_memory_creation_test.cc Adds coverage for pinned-memory behavior across additional arange overloads.
test/cpp/compat/ATen_factory_default_dtype_test.cc Adds tests ensuring integral arange defaults to kLong and floating arange follows current default dtype.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 78 to 95
auto dense = paddle::experimental::arange(
paddle::experimental::full(
{}, start.to<double>(), phi::DataType::FLOAT64),
paddle::experimental::full(
{}, end.to<double>(), phi::DataType::FLOAT64),
paddle::experimental::full({}, 1, phi::DataType::FLOAT64),
compat::_PD_AtenScalarTypeToPhiDataType(options.dtype()),
paddle::experimental::full(
{}, step.to<double>(), phi::DataType::FLOAT64),
compat::_PD_AtenScalarTypeToPhiDataType(dtype),
phi::CPUPlace());
return dense.copy_to(pinned_place, /*blocking=*/true);
}
return paddle::experimental::arange(
paddle::experimental::full(
{}, start.to<double>(), phi::DataType::FLOAT64),
paddle::experimental::full({}, end.to<double>(), phi::DataType::FLOAT64),
paddle::experimental::full({}, 1, phi::DataType::FLOAT64),
compat::_PD_AtenScalarTypeToPhiDataType(options.dtype()),
paddle::experimental::full({}, step.to<double>(), phi::DataType::FLOAT64),
compat::_PD_AtenScalarTypeToPhiDataType(dtype),
options._PD_GetPlace());
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

Now that omitted dtype resolves to kLong for integral inputs, the implementation still materializes start/end/step as FLOAT64 scalars via to<double>(). This loses integer precision for values outside the exact double range (e.g., >2^53) and can produce incorrect sequences compared to an int64 arange. Consider constructing start/end/step using an integral scalar tensor type when the resolved dtype is integral (or otherwise avoid the double round-trip) so large int64 ranges remain exact.

Copilot uses AI. Check for mistakes.
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (develop@638c7c3). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             develop    #78552   +/-   ##
===========================================
  Coverage           ?   100.00%           
===========================================
  Files              ?         1           
  Lines              ?        42           
  Branches           ?         0           
===========================================
  Hits               ?        42           
  Misses             ?         0           
  Partials           ?         0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@youge325
Copy link
Copy Markdown
Contributor Author

youge325 commented Apr 2, 2026

/re-run all-failed

1 similar comment
@youge325
Copy link
Copy Markdown
Contributor Author

youge325 commented Apr 2, 2026

/re-run all-failed

@youge325
Copy link
Copy Markdown
Contributor Author

youge325 commented Apr 2, 2026

/re-run all-failed

@SigureMo SigureMo closed this Apr 2, 2026
@SigureMo SigureMo reopened this Apr 2, 2026
@SigureMo SigureMo closed this Apr 2, 2026
@SigureMo SigureMo reopened this Apr 2, 2026
@SigureMo
Copy link
Copy Markdown
Member

SigureMo commented Apr 2, 2026

@ShigureNyako 这个怎么不再 review 了?

Copy link
Copy Markdown
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

LGTMeow 🐾

@SigureMo SigureMo merged commit 81c4b8b into PaddlePaddle:develop Apr 2, 2026
108 of 110 checks passed
@youge325 youge325 deleted the fix-arange-default-dtype branch April 3, 2026 01:39
liuhao2638 pushed a commit to liuhao2638/Paddle that referenced this pull request Apr 7, 2026
YuhanXu pushed a commit to YuhanXu/Paddle that referenced this pull request Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants