Skip to content

Conversation

Cutelemon6
Copy link
Contributor

@Cutelemon6 Cutelemon6 commented May 29, 2025

PR Category

Execute Infrastructure

PR Types

Bug fixes

Description

Fix bug when x1.shape != x2.shape, resultig wrong w1 or w2 which does sumation over the axis that need to broadcast.
A minimum reproducible case compared to torch

a = numpy.array([[1, 2]])
b = numpy.array([[2], [4]])

pa = paddle.to_tensor(a, dtype=paddle.float64)
pb = paddle.to_tensor(b, dtype=paddle.float64)
ta = torch.tensor(a, dtype=torch.float64)
tb = torch.tensor(b, dtype=torch.float64)

pr = paddle.nn.functional.cosine_similarity(pa, pb)
tr = torch.nn.functional.cosine_similarity(ta, tb)
print("paddle: ", pr)
print("torch: ", tr)

output

paddle:  Tensor(shape=[2], dtype=float64, place=Place(gpu:0), stop_gradient=True,
       [1.34164079, 1.34164079])
torch:  tensor([0.9487, 0.9487], dtype=torch.float64)

in this case, paddle implementation

w12 = sum(paddle.multiply(x1, x2), axis=axis)
w1 = sum(paddle.multiply(x1, x1), axis=axis)
w2 = sum(paddle.multiply(x2, x2), axis=axis)
n12 = sqrt(clip(w1 * w2, min=eps * eps))
cos_sim = w12 / n12
return cos_sim

w2 is [4, 16], but should be [8, 32] after broadcast and sumation.

All cases (24 cases in total) have passed both in gpu and cpu environments.

gpu:
cosine_sim-gpu

cpu:
cosine_sim-cpu

Copy link

paddle-bot bot commented May 29, 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.

@paddle-bot paddle-bot bot added the contributor External developers label May 29, 2025
@Cutelemon6 Cutelemon6 changed the title [Accuracy diff No.40-41] fix cosine_similarity accuracy error when x1, x2 need to broadcast [Accuracy diff No.40-41] [BIT] fix cosine_similarity accuracy error when x1, x2 need to broadcast May 29, 2025
@codecov-commenter
Copy link

codecov-commenter commented May 30, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (develop@e9ad101). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             develop    #73014   +/-   ##
===========================================
  Coverage           ?   100.00%           
===========================================
  Files              ?         1           
  Lines              ?         6           
  Branches           ?         0           
===========================================
  Hits               ?         6           
  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.

@lshpku lshpku merged commit 79f3f5f into PaddlePaddle:develop Jun 6, 2025
49 checks passed
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.

3 participants