Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #40 from baraline/numba_cache_install
Browse files Browse the repository at this point in the history
Adding numba compilation options and an example of how to set them in examples folder. Should only be useful for some specific use case such as HPC clusters.
  • Loading branch information
baraline authored Mar 11, 2023
2 parents 74379c3 + 07c7119 commit 4913df1
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 43 deletions.
7 changes: 6 additions & 1 deletion convst/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@

__author__ = 'Antoine Guillaume antoine.guillaume45@gmail.com'
__version__ = "0.2.5"
__version__ = "0.2.6"

__all__ = ['transformers', 'classifiers', 'utils', 'interpreters']

__USE_NUMBA_CACHE__ = True
__USE_NUMBA_FASTMATH__ = True
__USE_NUMBA_NOGIL__ = True
__USE_NUMBA_PARALLEL__ = True
53 changes: 30 additions & 23 deletions convst/transformers/_commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
# #
###############################################################################

from convst import (
__USE_NUMBA_CACHE__, __USE_NUMBA_FASTMATH__,
__USE_NUMBA_NOGIL__
)



@njit(
fastmath=True, cache=True, nogil=True
fastmath=__USE_NUMBA_FASTMATH__, cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__
)
def euclidean(x, y):
s = 0
Expand All @@ -19,7 +26,7 @@ def euclidean(x, y):
return sqrt(s)

@njit(
fastmath=True, cache=True, nogil=True
fastmath=__USE_NUMBA_FASTMATH__, cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__
)
def squared_euclidean(x, y):
s = 0
Expand All @@ -28,7 +35,7 @@ def squared_euclidean(x, y):
return s

@njit(
fastmath=True, cache=True, nogil=True
fastmath=__USE_NUMBA_FASTMATH__, cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__
)
def manhattan(x, y):
s = 0
Expand All @@ -43,7 +50,7 @@ def manhattan(x, y):
###############################################################################

@njit(
cache=True, nogil=True
cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__
)
def generate_strides_1D(X, window_size, dilation, use_phase):
if use_phase:
Expand All @@ -52,7 +59,7 @@ def generate_strides_1D(X, window_size, dilation, use_phase):
return _generate_strides_1D(X, window_size, dilation)

@njit(
cache=True, nogil=True
cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__
)

def generate_strides_2D(X, window_size, dilation, use_phase):
Expand All @@ -63,7 +70,7 @@ def generate_strides_2D(X, window_size, dilation, use_phase):


@njit(
cache=True, nogil=True
cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__
)
def _generate_strides_1D(X, window_size, dilation):
"""
Expand Down Expand Up @@ -93,7 +100,7 @@ def _generate_strides_1D(X, window_size, dilation):
return X_new

@njit(
cache=True, nogil=True
cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__
)

def _generate_strides_2D(X, window_size, dilation):
Expand Down Expand Up @@ -127,7 +134,7 @@ def _generate_strides_2D(X, window_size, dilation):


@njit(
cache=True, nogil=True
cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__
)
def _generate_strides_1D_phase(X, window_size, dilation):
"""
Expand Down Expand Up @@ -157,7 +164,7 @@ def _generate_strides_1D_phase(X, window_size, dilation):


@njit(
cache=True, nogil=True
cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__
)
def _generate_strides_2D_phase(X, window_size, dilation):
"""
Expand Down Expand Up @@ -189,7 +196,7 @@ def _generate_strides_2D_phase(X, window_size, dilation):


@njit(
cache=True, nogil=True
cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__
)
def get_subsequence(X, index, length, d, normalize, use_phase):
if use_phase:
Expand All @@ -203,7 +210,7 @@ def get_subsequence(X, index, length, d, normalize, use_phase):


@njit(
cache=True, fastmath=True, nogil=True
cache=__USE_NUMBA_CACHE__, fastmath=__USE_NUMBA_FASTMATH__, nogil=__USE_NUMBA_NOGIL__
)
def _get_subsequence(X, i_start, length, d, normalize):
"""
Expand Down Expand Up @@ -242,7 +249,7 @@ def _get_subsequence(X, i_start, length, d, normalize):


@njit(
cache=True, fastmath=True, nogil=True
cache=__USE_NUMBA_CACHE__, fastmath=__USE_NUMBA_FASTMATH__, nogil=__USE_NUMBA_NOGIL__
)
def _get_subsequence_phase(X, i_start, length, d, normalize):
"""
Expand Down Expand Up @@ -289,7 +296,7 @@ def _get_subsequence_phase(X, i_start, length, d, normalize):


@njit(
cache=True, nogil=True
cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__
)
def compute_shapelet_dist_vector(
x, values, length, dilation, dist_func, normalize, use_phase
Expand All @@ -315,7 +322,7 @@ def compute_shapelet_dist_vector(


@njit(
cache=True, fastmath=True, nogil=True
cache=__USE_NUMBA_CACHE__, fastmath=__USE_NUMBA_FASTMATH__, nogil=__USE_NUMBA_NOGIL__
)
def _compute_shapelet_dist_vector(x, values, length, dilation, dist_func):
"""
Expand Down Expand Up @@ -351,7 +358,7 @@ def _compute_shapelet_dist_vector(x, values, length, dilation, dist_func):


@njit(
cache=True, fastmath=True, nogil=True
cache=__USE_NUMBA_CACHE__, fastmath=__USE_NUMBA_FASTMATH__, nogil=__USE_NUMBA_NOGIL__
)
def _compute_shapelet_dist_vector_norm(x, values, length, dilation, dist_func):
"""
Expand Down Expand Up @@ -388,7 +395,7 @@ def _compute_shapelet_dist_vector_norm(x, values, length, dilation, dist_func):


@njit(
cache=True, fastmath=True, nogil=True
cache=__USE_NUMBA_CACHE__, fastmath=__USE_NUMBA_FASTMATH__, nogil=__USE_NUMBA_NOGIL__
)
def _compute_shapelet_dist_vector_phase(x, values, length, dilation, dist_func):
"""
Expand Down Expand Up @@ -424,7 +431,7 @@ def _compute_shapelet_dist_vector_phase(x, values, length, dilation, dist_func):


@njit(
cache=True, fastmath=True, nogil=True
cache=__USE_NUMBA_CACHE__, fastmath=__USE_NUMBA_FASTMATH__, nogil=__USE_NUMBA_NOGIL__
)
def _compute_shapelet_dist_vector_norm_phase(x, values, length, dilation, dist_func):
"""
Expand Down Expand Up @@ -462,7 +469,7 @@ def _compute_shapelet_dist_vector_norm_phase(x, values, length, dilation, dist_f


@njit(
cache=True, fastmath=True, nogil=True
cache=__USE_NUMBA_CACHE__, fastmath=__USE_NUMBA_FASTMATH__, nogil=__USE_NUMBA_NOGIL__
)
def apply_one_shapelet_one_sample_univariate(x, values, threshold, dist_func):
"""
Expand Down Expand Up @@ -514,7 +521,7 @@ def apply_one_shapelet_one_sample_univariate(x, values, threshold, dist_func):


@njit(
cache=True, fastmath=True, nogil=True
cache=__USE_NUMBA_CACHE__, fastmath=__USE_NUMBA_FASTMATH__, nogil=__USE_NUMBA_NOGIL__
)
def apply_one_shapelet_one_sample_multivariate(x, values, threshold, dist_func):
"""
Expand Down Expand Up @@ -569,7 +576,7 @@ def apply_one_shapelet_one_sample_multivariate(x, values, threshold, dist_func):


@njit(
cache=True, nogil=True
cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__
)
def _combinations_1d(x,y):
"""
Expand Down Expand Up @@ -608,7 +615,7 @@ def _combinations_1d(x,y):
return combinations

@njit(
cache=True, nogil=True
cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__
)
def prime_up_to(n):
is_p = zeros(n+1, dtype=bool_)
Expand All @@ -618,7 +625,7 @@ def prime_up_to(n):


@njit(
cache=True, nogil=True
cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__
)
def is_prime(n):
if (n % 2 == 0 and n > 2) or n == 0:
Expand All @@ -629,7 +636,7 @@ def is_prime(n):
return True

@njit(
cache=True, fastmath=True, nogil=True
cache=__USE_NUMBA_CACHE__, fastmath=__USE_NUMBA_FASTMATH__, nogil=__USE_NUMBA_NOGIL__
)
def choice_log(n_choice, n_sample):
if n_choice > 1:
Expand Down
10 changes: 8 additions & 2 deletions convst/transformers/_input_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.base import BaseEstimator, TransformerMixin


from convst import (
__USE_NUMBA_CACHE__, __USE_NUMBA_PARALLEL__
)


class c_StandardScaler(StandardScaler):
def fit(self, X, y=None):
self.usefull_atts = np.where(np.std(X, axis=0) != 0)[0]
Expand All @@ -36,15 +42,15 @@ def fit(self, X, y=None):
def transform(self, X):
return super().transform(X[:, self.usefull_atts])

@njit(cache=True)
@njit(cache=__USE_NUMBA_CACHE__)
def z_norm_one_sample(x):
n_features, n_timestamps = x.shape
x_new = np.empty((n_features, n_timestamps))
for i in prange(n_features):
x_new[i] = (x[i] - x[i].mean()) / (x[i].std() + 1e-8)
return x_new

@njit(cache=True, parallel=True)
@njit(cache=__USE_NUMBA_CACHE__, parallel=__USE_NUMBA_PARALLEL__)
def z_norm_all_samples(X):
n_samples, n_features, n_timestamps = X.shape
X_new = np.empty((n_samples, n_features, n_timestamps))
Expand Down
12 changes: 9 additions & 3 deletions convst/transformers/_multivariate_same_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@

from numba import njit, prange

@njit(cache=True, nogil=True)
from convst import (
__USE_NUMBA_CACHE__, __USE_NUMBA_FASTMATH__,
__USE_NUMBA_NOGIL__, __USE_NUMBA_PARALLEL__
)


@njit(cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__)
def M_SL_init_random_shapelet_params(
n_shapelets, shapelet_sizes, n_timestamps, p_norm, max_channels, prime_scheme
):
Expand Down Expand Up @@ -96,7 +102,7 @@ def M_SL_init_random_shapelet_params(

return values, lengths, dilations, threshold, normalize, n_channels, channel_ids

@njit(cache=True, parallel=True, nogil=True)
@njit(cache=__USE_NUMBA_CACHE__, parallel=__USE_NUMBA_PARALLEL__, nogil=__USE_NUMBA_NOGIL__)
def M_SL_generate_shapelet(
X, y, n_shapelets, shapelet_sizes, r_seed, p_norm, p_min, p_max, alpha,
dist_func, use_phase, max_channels, prime_scheme
Expand Down Expand Up @@ -273,7 +279,7 @@ def M_SL_generate_shapelet(
channel_ids[:a2]
)

@njit(cache=True, parallel=True, fastmath=True, nogil=True)
@njit(cache=__USE_NUMBA_CACHE__, parallel=__USE_NUMBA_PARALLEL__, fastmath=__USE_NUMBA_FASTMATH__, nogil=__USE_NUMBA_NOGIL__)
def M_SL_apply_all_shapelets(
X, shapelets, dist_func, use_phase
):
Expand Down
12 changes: 9 additions & 3 deletions convst/transformers/_multivariate_variable_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@

from numba import njit, prange

@njit(cache=True, nogil=True)
from convst import (
__USE_NUMBA_CACHE__, __USE_NUMBA_FASTMATH__,
__USE_NUMBA_NOGIL__, __USE_NUMBA_PARALLEL__
)


@njit(cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__)
def M_VL_init_random_shapelet_params(
n_shapelets, shapelet_sizes, n_timestamps, p_norm, max_channels, prime_scheme
):
Expand Down Expand Up @@ -96,7 +102,7 @@ def M_VL_init_random_shapelet_params(

return values, lengths, dilations, threshold, normalize, n_channels, channel_ids

@njit(cache=True, parallel=True, nogil=True)
@njit(cache=__USE_NUMBA_CACHE__, parallel=__USE_NUMBA_PARALLEL__, nogil=__USE_NUMBA_NOGIL__)
def M_VL_generate_shapelet(
X, y, n_shapelets, shapelet_sizes, r_seed, p_norm, p_min, p_max, alpha,
dist_func, use_phase, max_channels, min_len, X_len, prime_scheme
Expand Down Expand Up @@ -302,7 +308,7 @@ def M_VL_generate_shapelet(
channel_ids[:a2]
)

@njit(cache=True, parallel=True, fastmath=True, nogil=True)
@njit(cache=__USE_NUMBA_CACHE__, parallel=__USE_NUMBA_PARALLEL__, fastmath=__USE_NUMBA_FASTMATH__, nogil=__USE_NUMBA_NOGIL__)
def M_VL_apply_all_shapelets(
X, shapelets, dist_func, use_phase, X_len
):
Expand Down
11 changes: 8 additions & 3 deletions convst/transformers/_univariate_same_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

from numba import njit, prange

@njit(cache=True, nogil=True)
from convst import (
__USE_NUMBA_CACHE__, __USE_NUMBA_FASTMATH__,
__USE_NUMBA_NOGIL__, __USE_NUMBA_PARALLEL__
)

@njit(cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__)
def U_SL_init_random_shapelet_params(
n_shapelets, shapelet_sizes, n_timestamps, p_norm, prime_scheme
):
Expand Down Expand Up @@ -81,7 +86,7 @@ def U_SL_init_random_shapelet_params(

return values, lengths, dilations, threshold, normalize

@njit(cache=True, parallel=True, nogil=True)
@njit(cache=__USE_NUMBA_CACHE__, parallel=__USE_NUMBA_PARALLEL__, nogil=__USE_NUMBA_NOGIL__)
def U_SL_generate_shapelet(
X, y, n_shapelets, shapelet_sizes, r_seed, p_norm, p_min, p_max, alpha,
dist_func, use_phase, prime_scheme
Expand Down Expand Up @@ -227,7 +232,7 @@ def U_SL_generate_shapelet(
)


@njit(cache=True, parallel=True, fastmath=True, nogil=True)
@njit(cache=__USE_NUMBA_CACHE__, parallel=__USE_NUMBA_PARALLEL__, fastmath=__USE_NUMBA_FASTMATH__, nogil=__USE_NUMBA_NOGIL__)
def U_SL_apply_all_shapelets(
X, shapelets, dist_func, use_phase
):
Expand Down
11 changes: 7 additions & 4 deletions convst/transformers/_univariate_variable_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

from numba import njit, prange

# TODO : check if numba could support Tuple of variable length numpy arrays as input
from convst import (
__USE_NUMBA_CACHE__, __USE_NUMBA_FASTMATH__,
__USE_NUMBA_NOGIL__, __USE_NUMBA_PARALLEL__
)

@njit(cache=True, nogil=True)
@njit(cache=__USE_NUMBA_CACHE__, nogil=__USE_NUMBA_NOGIL__)
def U_VL_init_random_shapelet_params(
n_shapelets, shapelet_sizes, n_timestamps, p_norm, prime_scheme
):
Expand Down Expand Up @@ -87,7 +90,7 @@ def U_VL_init_random_shapelet_params(
return values, lengths, dilations, threshold, normalize


@njit(cache=True, parallel=True, nogil=True)
@njit(cache=__USE_NUMBA_CACHE__, parallel=__USE_NUMBA_PARALLEL__, nogil=__USE_NUMBA_NOGIL__)
def U_VL_generate_shapelet(
X, y, n_shapelets, shapelet_sizes, r_seed, p_norm, p_min, p_max, alpha,
dist_func, use_phase, min_len, X_len, prime_scheme
Expand Down Expand Up @@ -259,7 +262,7 @@ def U_VL_generate_shapelet(
)


@njit(cache=True, parallel=True, fastmath=True, nogil=True)
@njit(cache=__USE_NUMBA_CACHE__, parallel=__USE_NUMBA_PARALLEL__, fastmath=__USE_NUMBA_FASTMATH__, nogil=__USE_NUMBA_NOGIL__)
def U_VL_apply_all_shapelets(
X, shapelets, dist_func, use_phase, X_len
):
Expand Down
Loading

0 comments on commit 4913df1

Please sign in to comment.