Skip to content

Commit

Permalink
Attempt to fix data type-related bugs on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
shz9 committed Dec 4, 2024
1 parent c18e3b4 commit 692c921
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 62 deletions.
26 changes: 22 additions & 4 deletions viprs/model/vi/e_step.pxd
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
from cython cimport floating, integral
from cython cimport floating
cimport numpy as cnp

# --------------------------------------------------
# Define fused data types:

ctypedef fused indptr_type:
cnp.int32_t
cnp.int64_t

ctypedef fused noncomplex_numeric:
cnp.int8_t
cnp.int16_t
cnp.int32_t
cnp.int64_t
cnp.float32_t
cnp.float64_t

# --------------------------------------------------

cpdef void e_step(int[::1] ld_left_bound,
integral[::1] ld_indptr,
indptr_type[::1] ld_indptr,
floating[::1] ld_data,
floating[::1] std_beta,
floating[::1] var_gamma,
Expand All @@ -19,7 +37,7 @@ cpdef void e_step(int[::1] ld_left_bound,


cpdef void e_step_mixture(int[::1] ld_left_bound,
integral[::1] ld_indptr,
indptr_type[::1] ld_indptr,
floating[::1] ld_data,
floating[::1] std_beta,
floating[:, ::1] var_gamma,
Expand All @@ -37,7 +55,7 @@ cpdef void e_step_mixture(int[::1] ld_left_bound,


cpdef void e_step_grid(int[::1] ld_left_bound,
integral[::1] ld_indptr,
indptr_type[::1] ld_indptr,
floating[::1] ld_data,
floating[::1] std_beta,
floating[::1, :] var_gamma,
Expand Down
22 changes: 11 additions & 11 deletions viprs/model/vi/e_step.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ from .e_step_cpp cimport cpp_blas_axpy, cpp_blas_dot
cimport cython
import numpy as np
cimport numpy as np
from cython cimport floating, integral
from cython cimport floating


# A safe way to get the number of the thread currently executing the code:
Expand Down Expand Up @@ -40,7 +40,7 @@ cdef extern from *:
@cython.exceptval(check=False)
cpdef void update_q_factor_matrix(int[:] active_model_idx,
int[::1] ld_left_bound,
integral[::1] ld_indptr,
indptr_type[::1] ld_indptr,
floating[::1] ld_data,
floating[::1, :] eta,
floating[::1, :] q,
Expand All @@ -59,7 +59,7 @@ cpdef void update_q_factor_matrix(int[:] active_model_idx,

cdef:
int j, m, m_idx, start, end, c_size = eta.shape[0], num_models = active_model_idx.shape[0]
integral ld_start, ld_end
indptr_type ld_start, ld_end

for j in prange(c_size, nogil=True, schedule='static', num_threads=threads):

Expand All @@ -82,7 +82,7 @@ cpdef void update_q_factor_matrix(int[:] active_model_idx,
@cython.cdivision(True)
@cython.exceptval(check=False)
cpdef void update_q_factor(int[::1] ld_left_bound,
integral[::1] ld_indptr,
indptr_type[::1] ld_indptr,
floating[::1] ld_data,
floating[::1] eta,
floating[::1] q,
Expand All @@ -100,7 +100,7 @@ cpdef void update_q_factor(int[::1] ld_left_bound,

cdef:
int j, start, end, c_size = eta.shape[0]
integral ld_start, ld_end;
indptr_type ld_start, ld_end;

for j in prange(c_size, nogil=True, schedule='static', num_threads=threads):

Expand All @@ -121,7 +121,7 @@ cpdef void update_q_factor(int[::1] ld_left_bound,
@cython.cdivision(True)
@cython.exceptval(check=False)
cpdef void e_step(int[::1] ld_left_bound,
integral[::1] ld_indptr,
indptr_type[::1] ld_indptr,
floating[::1] ld_data,
floating[::1] std_beta,
floating[::1] var_gamma,
Expand Down Expand Up @@ -159,7 +159,7 @@ cpdef void e_step(int[::1] ld_left_bound,

cdef:
int j, start, end, c_size = var_mu.shape[0]
long ld_start, ld_end
indptr_type ld_start, ld_end
floating u_j

for j in prange(c_size, nogil=True, schedule='static', num_threads=threads):
Expand Down Expand Up @@ -209,7 +209,7 @@ cpdef void e_step(int[::1] ld_left_bound,
@cython.cdivision(True)
@cython.exceptval(check=False)
cpdef void e_step_mixture(int[::1] ld_left_bound,
integral[::1] ld_indptr,
indptr_type[::1] ld_indptr,
floating[::1] ld_data,
floating[::1] std_beta,
floating[:, ::1] var_gamma,
Expand Down Expand Up @@ -250,7 +250,7 @@ cpdef void e_step_mixture(int[::1] ld_left_bound,
cdef:
int j, start, end, thread_offset, c_size = var_mu.shape[0], k, K = var_mu.shape[1]
int u_size = threads * (K + 1)
long ld_start, ld_end
indptr_type ld_start, ld_end
floating mu_beta_j
floating[::1] u_j

Expand Down Expand Up @@ -314,7 +314,7 @@ cpdef void e_step_mixture(int[::1] ld_left_bound,
@cython.cdivision(True)
@cython.exceptval(check=False)
cpdef void e_step_grid(int[::1] ld_left_bound,
integral[::1] ld_indptr,
indptr_type[::1] ld_indptr,
floating[::1] ld_data,
floating[::1] std_beta,
floating[::1, :] var_gamma,
Expand Down Expand Up @@ -354,7 +354,7 @@ cpdef void e_step_grid(int[::1] ld_left_bound,

cdef:
int start, end, j, c_size = var_mu.shape[0], m_idx, m, num_models = active_model_idx.shape[0]
long ld_start, ld_end
indptr_type ld_start, ld_end
floating u_j

for j in prange(c_size, nogil=True, schedule='static', num_threads=threads):
Expand Down
30 changes: 19 additions & 11 deletions viprs/model/vi/e_step_cpp.pxd
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
from cython cimport floating, integral
cimport numpy as np
from cython cimport floating
cimport numpy as cnp

# --------------------------------------------------
# Define fused data types:

ctypedef fused indptr_type:
cnp.int32_t
cnp.int64_t

ctypedef fused noncomplex_numeric:
np.int8_t
np.int16_t
np.int32_t
np.int64_t
np.float32_t
np.float64_t
cnp.int8_t
cnp.int16_t
cnp.int32_t
cnp.int64_t
cnp.float32_t
cnp.float64_t

# --------------------------------------------------

cdef void cpp_blas_axpy(floating[::1] v1, floating[::1] v2, floating alpha) noexcept nogil
cdef floating cpp_blas_dot(floating[::1] v1, floating[::1] v2) noexcept nogil


cpdef void cpp_e_step(int[::1] ld_left_bound,
integral[::1] ld_indptr,
indptr_type[::1] ld_indptr,
noncomplex_numeric[::1] ld_data,
floating[::1] std_beta,
floating[::1] var_gamma,
Expand All @@ -33,7 +41,7 @@ cpdef void cpp_e_step(int[::1] ld_left_bound,


cpdef void cpp_e_step_mixture(int[::1] ld_left_bound,
integral[::1] ld_indptr,
indptr_type[::1] ld_indptr,
noncomplex_numeric[::1] ld_data,
floating[::1] std_beta,
floating[:, ::1] var_gamma,
Expand All @@ -51,7 +59,7 @@ cpdef void cpp_e_step_mixture(int[::1] ld_left_bound,
bint low_memory) noexcept nogil

cpdef void cpp_e_step_grid(int[::1] ld_left_bound,
integral[::1] ld_indptr,
indptr_type[::1] ld_indptr,
noncomplex_numeric[::1] ld_data,
floating[::1] std_beta,
floating[::1, :] var_gamma,
Expand Down
72 changes: 36 additions & 36 deletions viprs/model/vi/e_step_cpp.pyx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# distutils: language = c++
# sources: model/vi/e_step.hpp

from cython cimport floating, integral
from cython cimport floating


cdef extern from "e_step.hpp" nogil:
Expand All @@ -13,42 +13,42 @@ cdef extern from "e_step.hpp" nogil:
T blas_dot[T](T* x, T* y, int size) noexcept nogil

void e_step[T, U, I](int c_size,
int* ld_left_bound,
I* ld_indptr,
U* ld_data,
T* std_beta,
T* var_gamma,
T* var_mu,
T* eta,
T* q,
T* eta_diff,
T* u_logs,
T* half_var_tau,
T* mu_mult,
int* ld_left_bound,
I* ld_indptr,
U* ld_data,
T* std_beta,
T* var_gamma,
T* var_mu,
T* eta,
T* q,
T* eta_diff,
T* u_logs,
T* half_var_tau,
T* mu_mult,
T dq_scale,
int threads,
bint use_blas,
bint low_memory) noexcept nogil
int threads,
bint use_blas,
bint low_memory) noexcept nogil

void e_step_mixture[T, U, I](int c_size,
int K,
int* ld_left_bound,
I* ld_indptr,
U* ld_data,
T* std_beta,
T* var_gamma,
T* var_mu,
T* eta,
T* q,
T* eta_diff,
T* log_null_pi,
T* u_logs,
T* half_var_tau,
T* mu_mult,
int K,
int* ld_left_bound,
I* ld_indptr,
U* ld_data,
T* std_beta,
T* var_gamma,
T* var_mu,
T* eta,
T* q,
T* eta_diff,
T* log_null_pi,
T* u_logs,
T* half_var_tau,
T* mu_mult,
T dq_scale,
int threads,
bint use_blas,
bint low_memory) noexcept nogil
int threads,
bint use_blas,
bint low_memory) noexcept nogil

void e_step_grid[T, U, I](int c_size,
int n_active_models,
Expand Down Expand Up @@ -92,7 +92,7 @@ cdef floating cpp_blas_dot(floating[::1] v1, floating[::1] v2) noexcept nogil:


cpdef void cpp_e_step(int[::1] ld_left_bound,
integral[::1] ld_indptr,
indptr_type[::1] ld_indptr,
noncomplex_numeric[::1] ld_data,
floating[::1] std_beta,
floating[::1] var_gamma,
Expand Down Expand Up @@ -128,7 +128,7 @@ cpdef void cpp_e_step(int[::1] ld_left_bound,


cpdef void cpp_e_step_mixture(int[::1] ld_left_bound,
integral[::1] ld_indptr,
indptr_type[::1] ld_indptr,
noncomplex_numeric[::1] ld_data,
floating[::1] std_beta,
floating[:, ::1] var_gamma,
Expand Down Expand Up @@ -166,7 +166,7 @@ cpdef void cpp_e_step_mixture(int[::1] ld_left_bound,
low_memory)

cpdef void cpp_e_step_grid(int[::1] ld_left_bound,
integral[::1] ld_indptr,
indptr_type[::1] ld_indptr,
noncomplex_numeric[::1] ld_data,
floating[::1] std_beta,
floating[::1, :] var_gamma,
Expand Down

0 comments on commit 692c921

Please sign in to comment.