Skip to content

Commit

Permalink
Support Python 3.12 (#4)
Browse files Browse the repository at this point in the history
To support LightFM for Python 3.12, adpoting to Cython 3.0 is needed.
This PR allows the installation of lightfm over Python 3.12.

---------

Co-authored-by: David Davó <[email protected]>
  • Loading branch information
chezou and daviddavo authored Dec 8, 2024
1 parent 294c0d2 commit 33f219a
Show file tree
Hide file tree
Showing 5 changed files with 61,542 additions and 45,657 deletions.
11 changes: 8 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.11"]
python-version: ["3.11", "3.12"]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand All @@ -37,5 +37,10 @@ jobs:
run: |
pip install -e .
- name: Test with pytest
# Windows pytest never succeeded even with previous Cython version with
# Windows fatal exception: access violation
# Just verify pip install -e . works on Windows
# https://github.com/chezou/rectools-lightfm/actions/runs/10412191347/job/28837492302
if: matrix.os != 'windows-latest'
run: |
pytest
26 changes: 13 additions & 13 deletions lightfm/_lightfm_fast.pyx.template
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ cdef struct Pair:
flt val


cdef int reverse_pair_compare(const_void *a, const_void *b) nogil:
cdef int reverse_pair_compare(const_void *a, const_void *b) noexcept nogil:

cdef flt diff

Expand All @@ -122,7 +122,7 @@ cdef int reverse_pair_compare(const_void *a, const_void *b) nogil:
return -1


cdef int int_compare(const_void *a, const_void *b) nogil:
cdef int int_compare(const_void *a, const_void *b) noexcept nogil:

if deref(<int*>a) - deref(<int*>b) > 0:
return 1
Expand All @@ -132,7 +132,7 @@ cdef int int_compare(const_void *a, const_void *b) nogil:
return 0


cdef int flt_compare(const_void *a, const_void *b) nogil:
cdef int flt_compare(const_void *a, const_void *b) noexcept nogil:

if deref(<flt*>a) - deref(<flt*>b) > 0:
return 1
Expand Down Expand Up @@ -165,15 +165,15 @@ cdef class CSRMatrix:
self.rows, self.cols = csr_matrix.shape
self.nnz = len(self.data)

cdef int get_row_start(self, int row) nogil:
cdef int get_row_start(self, int row) noexcept nogil:
"""
Return the pointer to the start of the
data for row.
"""

return self.indptr[row]

cdef int get_row_end(self, int row) nogil:
cdef int get_row_end(self, int row) noexcept nogil:
"""
Return the pointer to the end of the
data for row.
Expand Down Expand Up @@ -267,7 +267,7 @@ cdef inline flt sigmoid(flt v) nogil:
return 1.0 / (1.0 + exp(-v))


cdef inline int in_positives(int item_id, int user_id, CSRMatrix interactions) nogil:
cdef inline int in_positives(int item_id, int user_id, CSRMatrix interactions) noexcept nogil:

cdef int i, start_idx, stop_idx

Expand All @@ -290,7 +290,7 @@ cdef inline void compute_representation(CSRMatrix features,
FastLightFM lightfm,
int row_id,
double scale,
flt *representation) nogil:
flt *representation) noexcept nogil:
"""
Compute latent representation for row_id.
The last element of the representation is the bias.
Expand Down Expand Up @@ -319,7 +319,7 @@ cdef inline void compute_representation(CSRMatrix features,

cdef inline flt compute_prediction_from_repr(flt *user_repr,
flt *item_repr,
int no_components) nogil:
int no_components) noexcept nogil:

cdef int i
cdef flt result
Expand Down Expand Up @@ -403,7 +403,7 @@ cdef inline double update_features(CSRMatrix feature_indices,
double learning_rate,
double alpha,
flt rho,
flt eps) nogil:
flt eps) noexcept nogil:
"""
Update feature vectors.
"""
Expand Down Expand Up @@ -460,7 +460,7 @@ cdef inline void update(double loss,
flt *it_repr,
FastLightFM lightfm,
double item_alpha,
double user_alpha) nogil:
double user_alpha) noexcept nogil:
"""
Apply the gradient step.
"""
Expand Down Expand Up @@ -545,7 +545,7 @@ cdef void warp_update(double loss,
flt *neg_it_repr,
FastLightFM lightfm,
double item_alpha,
double user_alpha) nogil:
double user_alpha) noexcept nogil:
"""
Apply the gradient step.
"""
Expand Down Expand Up @@ -651,7 +651,7 @@ cdef void warp_update(double loss,

cdef void regularize(FastLightFM lightfm,
double item_alpha,
double user_alpha) nogil:
double user_alpha) noexcept nogil:
"""
Apply accumulated L2 regularization to all features.
"""
Expand All @@ -677,7 +677,7 @@ cdef void regularize(FastLightFM lightfm,

cdef void locked_regularize(FastLightFM lightfm,
double item_alpha,
double user_alpha) nogil:
double user_alpha) noexcept nogil:
"""
Apply accumulated L2 regularization to all features. Acquire a lock
to prevent multiple threads from performing this operation.
Expand Down
Loading

0 comments on commit 33f219a

Please sign in to comment.