From 4e5c0175525b29ea351e63723984e840d2c76d39 Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Mon, 27 Feb 2023 16:19:30 -0500 Subject: [PATCH 1/2] MAINT Use float64 for accumulators in WeightVector --- sklearn/utils/_weight_vector.pxd.tp | 9 +++++---- sklearn/utils/_weight_vector.pyx.tp | 10 +++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/sklearn/utils/_weight_vector.pxd.tp b/sklearn/utils/_weight_vector.pxd.tp index 9d1779373cbf4..f5e3e4af5aefd 100644 --- a/sklearn/utils/_weight_vector.pxd.tp +++ b/sklearn/utils/_weight_vector.pxd.tp @@ -27,11 +27,12 @@ cdef class WeightVector{{name_suffix}}(object): cdef readonly {{c_type}}[::1] aw cdef {{c_type}} *w_data_ptr cdef {{c_type}} *aw_data_ptr - cdef {{c_type}} wscale - cdef {{c_type}} average_a - cdef {{c_type}} average_b + + cdef double wscale + cdef double average_a + cdef double average_b cdef int n_features - cdef {{c_type}} sq_norm + cdef double sq_norm cdef void add(self, {{c_type}} *x_data_ptr, int *x_ind_ptr, int xnnz, {{c_type}} c) noexcept nogil diff --git a/sklearn/utils/_weight_vector.pyx.tp b/sklearn/utils/_weight_vector.pyx.tp index e2d374813a5b4..caa992d4ad64a 100644 --- a/sklearn/utils/_weight_vector.pyx.tp +++ b/sklearn/utils/_weight_vector.pyx.tp @@ -99,8 +99,8 @@ cdef class WeightVector{{name_suffix}}(object): cdef int j cdef int idx cdef {{c_type}} val - cdef {{c_type}} innerprod = 0.0 - cdef {{c_type}} xsqnorm = 0.0 + cdef double innerprod = 0.0 + cdef double xsqnorm = 0.0 # the next two lines save a factor of 2! cdef {{c_type}} wscale = self.wscale @@ -139,8 +139,8 @@ cdef class WeightVector{{name_suffix}}(object): cdef int idx cdef {{c_type}} val cdef {{c_type}} mu = 1.0 / num_iter - cdef {{c_type}} average_a = self.average_a - cdef {{c_type}} wscale = self.wscale + cdef double average_a = self.average_a + cdef double wscale = self.wscale cdef {{c_type}}* aw_data_ptr = self.aw_data_ptr for j in range(xnnz): @@ -174,7 +174,7 @@ cdef class WeightVector{{name_suffix}}(object): """ cdef int j cdef int idx - cdef {{c_type}} innerprod = 0.0 + cdef double innerprod = 0.0 cdef {{c_type}}* w_data_ptr = self.w_data_ptr for j in range(xnnz): idx = x_ind_ptr[j] From 0fffbb68d1ada1eb89a59d8ed0a070a9db095c6f Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Tue, 28 Feb 2023 12:35:46 -0500 Subject: [PATCH 2/2] CLN Address comments --- sklearn/utils/_weight_vector.pyx.tp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sklearn/utils/_weight_vector.pyx.tp b/sklearn/utils/_weight_vector.pyx.tp index caa992d4ad64a..307546fcfb0a2 100644 --- a/sklearn/utils/_weight_vector.pyx.tp +++ b/sklearn/utils/_weight_vector.pyx.tp @@ -98,7 +98,7 @@ cdef class WeightVector{{name_suffix}}(object): """ cdef int j cdef int idx - cdef {{c_type}} val + cdef double val cdef double innerprod = 0.0 cdef double xsqnorm = 0.0 @@ -137,8 +137,8 @@ cdef class WeightVector{{name_suffix}}(object): """ cdef int j cdef int idx - cdef {{c_type}} val - cdef {{c_type}} mu = 1.0 / num_iter + cdef double val + cdef double mu = 1.0 / num_iter cdef double average_a = self.average_a cdef double wscale = self.wscale cdef {{c_type}}* aw_data_ptr = self.aw_data_ptr