Skip to content

Conversation

@zrr1999
Copy link
Member

@zrr1999 zrr1999 commented Nov 5, 2025

PR Category

Operator Mechanism

PR Types

Performance

Description

受影响的 API:

  • paddle.nn.functional.interpolate ,新增对齐 case 3900/4769 个,现存精度问题case 620 个。

修改内容

  • 规范 get_cubic_upsample_coefficients 重命名为 GetCubicUpsampleCoefficients,保持与其他函数命名风格一致。
  • 添加 AreaPixelComputeScale 函数,提高代码复用性,并统一计算逻辑。AreaPixelComputeScale 部分与原本替代掉的逻辑不同,当 output_size > 1 时,原本逻辑是直接设置 ratio = 0,现在是根据align_corners的状态判断,为True时与原本逻辑一致,为 False时不进行该条件的判断。
  • 移除 CubicConvolution2、CubicConvolution1和GetCubicUpsampleCoefficients 中不必要的static_cast。
  • ratio_x 参数计算时使用与MT一致的数据类型,同时参数传递过程中使用double避免精度损失。
  • 增加 InterpAntialiasKernel,使用抗锯齿算法,目前仅支持bilinear,bicubic算法。
  • 修复单测问题,增加新单测

TODO

  • 由于目前 Paddle 插值算法不同的 mode 共用同一个前处理,而nearest的T可能是整数,所以将
    funcs::AreaPixelComputeScale 统一写成了funcs::AreaPixelComputeScale,后续需要修改成funcs::AreaPixelComputeScale
  • InferSymbolicShapeInterface 暂时注释,后续添加单测后再加回来。

注意

torch 的 AreaPixelComputeScale 实现有一些问题,

template <typename T>
HOSTDEVICE inline T AreaPixelComputeScale(int64_t input_size,
                                          int64_t output_size,
                                          bool align_corners,
                                          const T scale) {
  if (align_corners) {
    if (output_size > 1) {
      return static_cast<T>(input_size - 1) / (output_size - 1);
    } else {
      return static_cast<T>(0);
    }
  } else {
    return (scale > 0.) ? static_cast<T>(1.0) / scale
                        : (static_cast<T>(input_size) / output_size);
  }
}

上述 torch 等价实现中,当align_corners=False,scale=0, output_size=0 时,会出现x/0的情况,所以优化为下述实现(在合法情况下结果一致)。

template <typename T>
inline T AreaPixelComputeScale(int64_t input_size,
                               int64_t output_size,
                               bool align_corners,
                               const T scale) {
  if (align_corners) {
    if (output_size > 1) {
      return static_cast<T>(input_size - 1) / (output_size - 1);
    }
  } else {
    if (scale > 0.) {
      return static_cast<T>(1.0) / scale;
    }
    if (output_size > 0) {
      return static_cast<T>(input_size) / output_size;
    }
  }
  return static_cast<T>(0);
}

Pcard-67164

@paddle-bot
Copy link

paddle-bot bot commented Nov 5, 2025

你的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.

@zrr1999 zrr1999 changed the title Acc/interpolate [Precision Depth Alignment] align paddle.nn.functional.interpolate forward Nov 5, 2025
@zrr1999 zrr1999 force-pushed the acc/interpolate branch 3 times, most recently from d1823e5 to 6253096 Compare November 13, 2025 03:07
@zrr1999 zrr1999 requested a review from Copilot November 13, 2025 08:11
Copy link
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 pull request aligns the precision and behavior of paddle.nn.functional.interpolate forward pass with reference implementations. The changes focus on improving numerical precision, adding anti-aliasing support for bilinear and bicubic interpolation modes, and refactoring the codebase for better maintainability.

Key Changes:

  • Added anti-aliasing support for bilinear and bicubic interpolation modes with new interp_antialias kernel
  • Unified ratio computation logic using new AreaPixelComputeScale and AreaPixelComputeSourceIndex helper functions
  • Changed ratio parameters from float to double throughout the codebase to improve precision
  • Renamed get_cubic_upsample_coefficients to GetCubicUpsampleCoefficients for naming consistency

Reviewed Changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
test/xpu/test_bilinear_interp_v2_op_xpu.py Updated ratio computation logic to match new implementation
test/legacy_test/test_trilinear_interp_v2_op.py Fixed compute_ratio function and corrected test parameters
test/legacy_test/test_nn_functional_interpolate.py Removed anti-aliasing unsupported test (now supported)
test/legacy_test/test_bilinear_interp_v2_op.py Added anti-aliasing tests and updated reference implementation
test/legacy_test/test_bicubic_interp_v2_op.py Added anti-aliasing tests and updated reference implementation
python/paddle/nn/functional/common.py Removed anti-aliasing restriction and added new operator integration
paddle/phi/ops/yaml/ops.yaml Added interp_antialias operator definition
paddle/phi/ops/yaml/backward.yaml Added interp_antialias_grad backward operator definition
paddle/phi/kernels/gpu/interpolate_kernel.cu Implemented anti-aliasing kernels and improved precision with double
paddle/phi/kernels/gpu/interpolate_grad_kernel.cu Updated gradient kernels to use double and fixed type casting
paddle/phi/kernels/funcs/interpolate_function.h Added new helper functions and renamed existing functions
paddle/phi/kernels/cpu/interpolate_kernel.cc Updated CPU kernels to use new helper functions
paddle/phi/kernels/cpu/interpolate_grad_kernel.cc Updated CPU gradient kernels to use new helper functions
paddle/fluid/pir/dialect/operator/interface/infer_symbolic_shape/multiary_infer_sym.h Added InterpAntialias symbolic shape inference declaration
paddle/fluid/pir/dialect/operator/interface/infer_symbolic_shape/multiary_infer_sym.cc Added InterpAntialias symbolic shape inference implementation

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

@codecov-commenter
Copy link

codecov-commenter commented Nov 13, 2025

Codecov Report

❌ Patch coverage is 38.29787% with 29 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@3569629). Learn more about missing BASE report.

Files with missing lines Patch % Lines
paddle/phi/kernels/cpu/interpolate_grad_kernel.cc 0.00% 16 Missing ⚠️
paddle/phi/kernels/funcs/interpolate_function.h 28.57% 10 Missing ⚠️
paddle/phi/kernels/cpu/interpolate_kernel.cc 70.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #76246   +/-   ##
==========================================
  Coverage           ?   38.29%           
==========================================
  Files              ?        4           
  Lines              ?       47           
  Branches           ?        0           
==========================================
  Hits               ?       18           
  Misses             ?       29           
  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.

Copy link
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.

Copilot reviewed 15 out of 15 changed files in this pull request and generated 8 comments.


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

Copy link
Contributor

@zhangting2020 zhangting2020 left a comment

Choose a reason for hiding this comment

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

LGTM for [Distribute-stable-test / Test

Copy link
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

@zrr1999 zrr1999 merged commit 1def310 into PaddlePaddle:develop Nov 20, 2025
213 of 239 checks passed
@zrr1999 zrr1999 deleted the acc/interpolate branch November 20, 2025 13:52
zrr1999 added a commit to zrr1999/Paddle that referenced this pull request Nov 25, 2025
…rward (PaddlePaddle#76246)

* add antialias

* fix acc

* impl antialias

* fix common

* use new kernel

* fix

* fix

* use AreaPixelComputeScale for cpu grad

* fix large tensor issue

* fix cov and test

* fix AreaPixelComputeScale

* unified PreCalculatorForLinearInterpInputIndex

* fix test

* disable test_weight_decay

* fix
zrr1999 added a commit that referenced this pull request Nov 26, 2025
…75968) (#76588)

* [Precision Depth Alignment] align paddle.lerp forward (#75968)

* fix lerp forward

* fix

* fix decomp

* fix comp

* fix

* fix lerp comp

* fix comp

* fix decomp

* fix test

* Modify the interpolate API and add 'antialias' argument (#76217)

* [Precision Depth Alignment] align paddle.nn.functional.interpolate forward (#76246)

* add antialias

* fix acc

* impl antialias

* fix common

* use new kernel

* fix

* fix

* use AreaPixelComputeScale for cpu grad

* fix large tensor issue

* fix cov and test

* fix AreaPixelComputeScale

* unified PreCalculatorForLinearInterpInputIndex

* fix test

* disable test_weight_decay

* fix

* empty

---------

Co-authored-by: Haze188 灏喆 <genghaozhe@baidu.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants