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

Skip to content

Commit c7238c8

Browse files
jeremiedbbglemaitre
authored andcommitted
MNT Centralize common cython compiler directives (scikit-learn#21512)
1 parent f3724ab commit c7238c8

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

0 commit comments

Comments
 (0)
0