[Operator Mechanism] Align cosine_similarity with torch2.12 (#79710)
* Align cosine_similarity with torch2.12 * fix * Add accuracy compatible path to cosine_similarity Keep torch's literal order, expand both inputs to the broadcast shape and then take the norm, behind FLAGS_use_accuracy_compatible_kernel. Since paddle.broadcast_to is not a 0-copy view like torch.expand, that path pays an extra O(prod(broadcast_shape)) allocation per input, so the default stays on unsqueeze plus the ||repeat(x, m)|| == sqrt(m) * ||x|| correction. Tests cover the three shape families: equal shapes, mismatched ranks in both dygraph and static graph, and large inputs broadcast along the reduced axis. The peak memory test asserts the no-expansion property and is therefore skipped when the compatible path is enabled. * delete useless flag path in cosine_similarity * Fix the broadcast factor under an unknown reduced axis * Add detection for fp16/bf16 at cpu * follow bots advice promote bf16/fp16 dtype to fp32 in cpu device * Add test for cosine_similarity * Fix the dtype promotion in cpu kernel. Use fp32 as the accumulation_dtype when the common dtype is fp16/bf16 otherwise preserve the precision of the common dtype (in particular, do not demote a float64 input in a mixed reduced/float64 operation) * Add test cases * Fix cosine similarity related tests * Fix p_norm_grad in processing x/0 to support test_zero_vector
O
omoYang committed
f4e6f68d71bf2f3be577f96370e9be4cd9b64415
Parent: 4ac7a75
Committed by GitHub <noreply@github.com>
on 9/10/2026, 3:25:47 AM