8000 MNT Centralize common cython compiler directives (#21512) · scikit-learn/scikit-learn@1f8825c · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f8825c

Browse files
authored
MNT Centralize common cython compiler directives (#21512)
1 parent 8cfbc38 commit 1f8825c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+14
-198
lines changed

sklearn/_build_utils/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@ def cythonize_extensions(top_path, config):
7676
compile_time_env={
7777
"SKLEARN_OPENMP_PARALLELISM_ENABLED": sklearn._OPENMP_SUPPORTED
7878
},
79-
compiler_directives={"language_level": 3},
79+
compiler_directives={
80+
"language_level": 3,
81+
"boundscheck": False,
82+
"wraparound": False,
83+
"initializedcheck": False,
84+
"nonecheck": False,
85+
"cdivision": True,
86+
},
8087
)
8188

8289

sklearn/_isotonic.pyx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
# Uses the pool adjacent violators algorithm (PAVA), with the
44
# enhancement of searching for the longest decreasing subsequence to
55
# pool at each step.
6-
#
7-
# cython: boundscheck=False, wraparound=False, cdivision=True
86

97
import numpy as np
108
cimport numpy as np

sklearn/cluster/_dbscan_inner.pyx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Fast inner loop for DBSCAN.
22
# Author: Lars Buitinck
33
# License: 3-clause BSD
4-
#
5-
# cython: boundscheck=False, wraparound=False
64

75
cimport cython
86
from libcpp.vector cimport vector

sklearn/cluster/_hierarchical_fast.pyx

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ ctypedef np.float64_t DOUBLE
88
ctypedef np.npy_intp INTP
99
ctypedef np.int8_t INT8
1010

11-
# Numpy must be initialized. When using numpy from C or Cython you must
12-
# _always_ do that, or you will have segfaults
13-
1411
np.import_array()
1512

1613
from ..metrics._dist_metrics cimport DistanceMetric
@@ -32,9 +29,6 @@ from numpy.math cimport INFINITY
3229
###############################################################################
3330
# Utilities for computing the ward momentum
3431

35-
@cython.boundscheck(False)
36-
@cython.wraparound(False)
37-
@cython.cdivision(True)
3832
def compute_ward_dist(np.ndarray[DOUBLE, ndim=1, mode='c'] m_1,
3933
np.ndarray[DOUBLE, ndim=2, mode='c'] m_2,
4034
np.ndarray[INTP, ndim=1, mode='c'] coord_row,
@@ -101,8 +95,6 @@ def _hc_get_descendent(INTP node, children, INTP n_leaves):
10195
return descendent
10296

10397

104-
@cython.boundscheck(False)
105-
@cython.wraparound(False)
10698
def hc_get_heads(np.ndarray[INTP, ndim=1] parents, copy=True):
10799
"""Returns the heads of the forest, as defined by parents.
108100
@@ -135,8 +127,6 @@ def hc_get_heads(np.ndarray[INTP, ndim=1] parents, copy=True):
135127
return parents
136128

137129

138-
@cython.boundscheck(False)
139-
@cython.wraparound(False)
140130
def _get_parents(nodes, heads, np.ndarray[INTP, ndim=1] parents,
141131
np.ndarray[INT8, ndim=1, mode='c'] not_visited):
142132
"""Returns the heads of the given nodes, as defined by parents.
@@ -176,8 +166,6 @@ def _get_parents(nodes, heads, np.ndarray[INTP, ndim=1] parents,
176166
# as keys and edge weights as values.
177167

178168

179-
@cython.boundscheck(False)
180-
@cython.wraparound(False)
181169
def max_merge(IntFloatDict a, IntFloatDict b,
182170
np.ndarray[ITYPE_t, ndim=1] mask,
183171
ITYPE_t n_a, ITYPE_t n_b):
@@ -231,8 +219,6 @@ def max_merge(IntFloatDict a, IntFloatDict b,
231219
return out_obj
232220

233221

234-
@cython.boundscheck(False)
235-
@cython.wraparound(False)
236222
def average_merge(IntFloatDict a, IntFloatDict b,
237223
np.ndarray[ITYPE_t, ndim=1] mask,
238224
ITYPE_t n_a, ITYPE_t n_b):
@@ -302,7 +288,6 @@ cdef class WeightedEdge:
302288
self.a = a
303289
self.b = b
304290

305-
@cython.nonecheck(False)
306291
def __richcmp__(self, WeightedEdge other, int op):
307292
"""Cython-specific comparison method.
308293
@@ -348,8 +333,6 @@ cdef class UnionFind(object):
348333
self.size = np.hstack((np.ones(N, dtype=ITYPE),
349334
np.zeros(N - 1, dtype=ITYPE)))
350335

351-
@cython.boundscheck(False)
352-
@cython.nonecheck(False)
353336
cdef void union(self, ITYPE_t m, ITYPE_t n):
354337
self.parent[m] = self.next_label
355338
self.parent[n] = self.next_label
@@ -358,8 +341,7 @@ cdef class UnionFind(object):
358341

359342
return
360343

361-
@cython.boundscheck(False)
362-
@cython.nonecheck(False)
344+
@cython.wraparound(True)
363345
cdef ITYPE_t fast_find(self, ITYPE_t n):
364346
cdef ITYPE_t p
365347
p = n
@@ -371,8 +353,7 @@ cdef class UnionFind(object):
371353
p, self.parent[p] = self.parent[p], n
372354
return n
373355

374-
@cython.boundscheck(False)
375-
@cython.nonecheck(False)
356+
376357
cpdef np.ndarray[DTYPE_t, ndim=2] _single_linkage_label(
377358
np.ndarray[DTYPE_t, ndim=2] L):
378359
"""
@@ -423,6 +404,7 @@ cpdef np.ndarray[DTYPE_t, ndim=2] _single_linkage_label(
423404
return result_arr
424405

425406

407+
@cython.wraparound(True)
426408
def single_linkage_label(L):
427409
"""
428410
Convert an linkage array or MST to a tree by labelling clusters at merges.
@@ -452,8 +434,6 @@ def single_linkage_label(L):
452434

453435

454436
# Implements MST-LINKAGE-CORE from https://arxiv.org/abs/1109.2378
455-
@cython.boundscheck(False)
456-
@cython.nonecheck(False)
457437
def mst_linkage_core(
458438
const DTYPE_t [:, ::1] raw_data,
459439
DistanceMetric dist_metric):

sklearn/cluster/_k_means_common.pxd

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# cython: language_level=3
2-
3-
41
from cython cimport floating
52
cimport numpy as np
63

sklearn/cluster/_k_means_common.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# cython: profile=True, boundscheck=False, wraparound=False, cdivision=True
2-
# Profiling is enabled by default as the overhead does not seem to be
3-
# measurable on this specific use case.
4-
51
# Author: Peter Prettenhofer <peter.prettenhofer@gmail.com>
62
# Olivier Grisel <olivier.grisel@ensta.org>
73
# Lars Buitinck

sklearn/cluster/_k_means_elkan.pyx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# cython: profile=True, boundscheck=False, wraparound=False, cdivision=True
2-
#
31
# Author: Andreas Mueller
42
#
53
# Licence: BSD 3 clause

sklearn/cluster/_k_means_lloyd.pyx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# cython: profile=True, boundscheck=False, wraparound=False, cdivision=True
2-
#
31
# Licence: BSD 3 clause
42

53
# TODO: We still need to use ndarrays instead of typed memoryviews when using

sklearn/cluster/_k_means_minibatch.pyx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# cython: profile=True, boundscheck=False, wraparound=False, cdivision=True
2-
31
# TODO: We still need to use ndarrays instead of typed memoryviews when using
42
# fused types and when the array may be read-only (for instance when it's
53
# provided by the user). This will be fixed in cython >= 0.3.

sklearn/datasets/_svmlight_format_fast.pyx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
# Lars Buitinck
55
# Olivier Grisel <olivier.grisel@ensta.org>
66
# License: BSD 3 clause
7-
#
8-
# cython: boundscheck=False, wraparound=False
97

108
import array
119
from cpython cimport array

sklearn/decomposition/_cdnmf_fast.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# cython: cdivision=True
2-
# cython: boundscheck=False
3-
# cython: wraparound=False
4-
51
# Author: Mathieu Blondel, Tom Dupre la Tour
62
# License: BSD 3 clause
73

sklearn/decomposition/_online_lda_fast.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#
2-
# cython: boundscheck=False, wraparound=False
3-
41
cimport cython
52
cimport numpy as np
63
import numpy as np
@@ -91,7 +88,6 @@ def _dirichlet_expectation_2d(np.ndarray[ndim=2, dtype=np.float64_t] arr):
9188
#
9289
# After: J. Bernardo (1976). Algorithm AS 103: Psi (Digamma) Function.
9390
# https://www.uv.es/~bernardo/1976AppStatist.pdf
94-
@cython.cdivision(True)
9591
cdef double psi(double x) nogil:
9692
if x <= 1e-6:
9793
# psi(x) = -EULER - 1/x + O(x)

sklearn/ensemble/_gradient_boosting.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# cython: cdivision=True
2-
# cython: boundscheck=False
3-
# cython: wraparound=False
4-
#
51
# Author: Peter Prettenhofer
62
#
73
# License: BSD 3 clause

sklearn/ensemble/_hist_gradient_boosting/_binning.pyx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# cython: cdivision=True
2-
# cython: boundscheck=False
3-
# cython: wraparound=False
4-
# cython: nonecheck=False
5-
# cython: language_level=3
6-
71
# Author: Nicolas Hug
82

93
cimport cython

sklearn/ensemble/_hist_gradient_boosting/_bitset.pxd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# cython: language_level=3
21
from .common cimport X_BINNED_DTYPE_C
32
from .common cimport BITSET_DTYPE_C
43
from .common cimport BITSET_INNER_DTYPE_C

sklearn/ensemble/_hist_gradient_boosting/_bitset.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# cython: cdivision=True
2-
# cython: boundscheck=False
3-
# cython: wraparound=False
4-
# cython: language_level=3
51
from .common cimport BITSET_INNER_DTYPE_C
62
from .common cimport BITSET_DTYPE_C
73
from .common cimport X_DTYPE_C

sklearn/ensemble/_hist_gradient_boosting/_gradient_boosting.pyx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
# cython: cdivision=True
2-
# cython: boundscheck=False
3-
# cython: wraparound=False
4-
# cython: language_level=3
5-
61
# Author: Nicolas Hug
72

83
cimport cython

sklearn/ensemble/_hist_gradient_boosting/_loss.pyx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
# cython: cdivision=True
2-
# cython: boundscheck=False
3-
# cython: wraparound=False
4-
# cython: language_level=3
5-
61
# Author: Nicolas Hug
72

83
cimport cython

sklearn/ensemble/_hist_gradient_boosting/_predictor.pyx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
# cython: cdivision=True
2-
# cython: boundscheck=False
3-
# cython: wraparound=False
4-
# cython: language_level=3
5-
61
# Author: Nicolas Hug
72

83
cimport cython

sklearn/ensemble/_hist_gradient_boosting/common.pxd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# cython: language_level=3
21
import numpy as np
32
cimport numpy as np
43

sklearn/ensemble/_hist_gradient_boosting/histogram.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# cython: cdivision=True
2-
# cython: boundscheck=False
3-
# cython: wraparound=False
4-
# cython: language_level=3
51
"""This module contains routines for building histograms."""
62

73
# Author: Nicolas Hug

sklearn/ensemble/_hist_gradient_boosting/splitting.pyx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
# cython: cdivision=True
2-
# cython: boundscheck=False
3-
# cython: wraparound=False
4-
# cython: language_level=3
5-
61
"""This module contains routines and data structures to:
72
83
- Find the best possible split of a node. For a given node, a split is
@@ -791,7 +786,6 @@ cdef class Splitter:
791786
split_info.sum_gradient_right, split_info.sum_hessian_right,
792787
lower_bound, upper_bound, self.l2_regularizat B41A ion)
793788

794-
@cython.initializedcheck(False)
795789
cdef void _find_best_bin_to_split_category(
796790
self,
797791
unsigned int feature_idx,

sklearn/ensemble/_hist_gradient_boosting/utils.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# cython: cdivision=True
2-
# cython: boundscheck=False
3-
# cython: wraparound=False
4-
# cython: language_level=3
51
"""This module contains utility routines."""
62
# Author: Nicolas Hug
73

sklearn/feature_extraction/_hashing_fast.pyx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Author: Lars Buitinck
22
# License: BSD 3 clause
3-
#
4-
# cython: boundscheck=False, cdivision=True
53

64
import sys
75
import array
@@ -92,7 +90,7 @@ def transform(raw_X, Py_ssize_t n_features, dtype,
9290
indices_a = np.frombuffer(indices, dtype=np.int32)
9391
indptr_a = np.frombuffer(indptr, dtype=indices_np_dtype)
9492

95-
if indptr[-1] > np.iinfo(np.int32).max: # = 2**31 - 1
93+
if indptr[len(indptr) - 1] > np.iinfo(np.int32).max: # = 2**31 - 1
9694
# both indices and indptr have the same dtype in CSR arrays
9795
indices_a = indices_a.astype(np.int64, copy=False)
9896
else:

sklearn/linear_model/_cd_fast.pyx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
# Manoj Kumar <manojkumarsivaraj334@gmail.com>
66
#
77
# License: BSD 3 clause
8-
#
9-
# cython: boundscheck=False, wraparound=False, cdivision=True
108

119
from libc.math cimport fabs
1210
cimport numpy as np

sklearn/linear_model/_sag_fast.pyx.tp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ dtypes = [('64', 'double', 'np.float64'),
2727

2828
#------------------------------------------------------------------------------
2929

30-
# cython: cdivision=True
31-
# cython: boundscheck=False
32-
# cython: wraparound=False
33-
#
3430
# Authors: Danny Sullivan <dbsullivan23@gmail.com>
3531
# Tom Dupre la Tour <tom.dupre-la-tour@m4x.org>
3632
# Arthur Mensch <arthur.mensch@m4x.org

sklearn/linear_model/_sgd_fast.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# cython: cdivision=True
2-
# cython: boundscheck=False
3-
# cython: wraparound=False
4-
#
51
# Author: Peter Prettenhofer <peter.prettenhofer@gmail.com>
62
# Mathieu Blondel (partial_fit support)
73
# Rob Zinkov (passive-aggressive)

sklearn/manifold/_barnes_hut_tsne.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# cython: boundscheck=False
2-
# cython: wraparound=False
3-
# cython: cdivision=True
4-
#
51
# Author: Christopher Moody <chrisemoody@gmail.com>
62
# Author: Nick Travers <nickt@squareup.com>
73
# Implementation by Chris Moody & Nick Travers

sklearn/manifold/_utils.pyx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# cython: boundscheck=False
2-
31
from libc cimport math
42
cimport cython
53
import numpy as np

sklearn/metrics/_dist_metrics.pxd

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
# cython: boundscheck=False
2-
# cython: cdivision=True
3-
# cython: initializedcheck=False
4-
# cython: wraparound=False
5-
61
cimport numpy as np
72
from libc.math cimport sqrt, exp
83

sklearn/metrics/_dist_metrics.pyx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
# cython: boundscheck=False
2-
# cython: cdivision=True
3-
# cython: initializedcheck=False
4-
# cython: wraparound=False
5-
61
# By Jake Vanderplas (2013) <jakevdp@cs.washington.edu>
72
# written for the scikit-learn project
83
# License: BSD

sklearn/metrics/_pairwise_fast.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#cython: boundscheck=False
2-
#cython: cdivision=True
3-
#cython: wraparound=False
4-
#
51
# Author: Andreas Mueller <amueller@ais.uni-bonn.de>
62
# Lars Buitinck
73
# Paolo Toccaceli

sklearn/metrics/cluster/_expected_mutual_info_fast.pyx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# cython: cdivision=True
2-
# cython: boundscheck=False
3-
# cython: wraparound=False
4-
#
51
# Authors: Robert Layton <robertlayton@gmail.com>
62
# Corey Lynch <coreylynch9@gmail.com>
73
# License: BSD 3 clause

0 commit comments

Comments
 (0)
0