Skip to content

Commit 4d8c10a

Browse files
authored
fix cos_sim, test=develop (#25017) (#25222)
1 parent ff55993 commit 4d8c10a

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

paddle/fluid/operators/math/cos_sim_functor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct CosSimFunctor {
3535
inline HOSTDEVICE void operator()(size_t row_id) const {
3636
auto* x = x_ + cols_ * row_id;
3737
T xx = 0, xy = 0, yy = 0;
38+
T eps = 1e-8;
3839
if (same_row) {
3940
auto* y = y_ + cols_ * row_id;
4041
T tep_x, tep_y;
@@ -45,6 +46,8 @@ struct CosSimFunctor {
4546
yy += tep_y * tep_y;
4647
xy += tep_x * tep_y;
4748
}
49+
xx = xx > eps ? xx : eps;
50+
yy = yy > eps ? yy : eps;
4851
xx = sqrt(xx);
4952
yy = sqrt(yy);
5053
y_norm_[row_id] = yy;
@@ -59,6 +62,8 @@ struct CosSimFunctor {
5962
yy += tep_y * tep_y;
6063
xy += tep_x * tep_y;
6164
}
65+
xx = xx > eps ? xx : eps;
66+
yy = yy > eps ? yy : eps;
6267
xx = sqrt(xx);
6368
yy = sqrt(yy);
6469
if (row_id == 0) y_norm_[0] = yy;

0 commit comments

Comments
 (0)