Skip to content

Commit

Permalink
Disable building GPU extension if IMPLICIT_DISABLE_CUDA env var is set (
Browse files Browse the repository at this point in the history
#561)

Disable building GPU extension if IMPLICIT_DISABLE_CUDA environment variable is set

Add an option to build implicit without the GPU extension, if the IMPLICIT_DISABLE_CUDA
is set. Closes #446
  • Loading branch information
benfred authored Apr 11, 2022
1 parent fd941d7 commit 7171765
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repos:
- id: isort
additional_dependencies: [toml]
- repo: https://github.com/python/black
rev: 21.12b0
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
Expand Down
4 changes: 2 additions & 2 deletions implicit/ann/annoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def similar_items(
if filter_items is not None:
ids, scores = _filter_items_from_results(itemid, ids, scores, filter_items, N)

return ids, 1 - (scores ** 2) / 2
return ids, 1 - (scores**2) / 2

def recommend(
self,
Expand Down Expand Up @@ -228,7 +228,7 @@ def recommend(
# convert the distances from euclidean to cosine distance,
# and then rescale the cosine distance to go back to inner product
scaling = self.max_norm * np.linalg.norm(query)
scores = scaling * (1 - (scores ** 2) / 2)
scores = scaling * (1 - (scores**2) / 2)
return ids, scores

def similar_users(self, userid, N=10, filter_users=None, users=None):
Expand Down
5 changes: 5 additions & 0 deletions implicit/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ find_package(CUDAToolkit)
if(CUDAToolkit_FOUND)
if (${CUDAToolkit_VERSION} VERSION_LESS "11.0.0")
message("implicit requires CUDA 11.0 or greater for GPU acceleration - found CUDA ${CUDAToolkit_VERSION}")

elseif(DEFINED ENV{IMPLICIT_DISABLE_CUDA})
# disable building the CUDA extension if the IMPLICIT_DISABLE_CUDA environment variable is set
message("Disabling building the GPU extension since IMPLICIT_DISABLE_CUDA env var is set")

else()
enable_language(CUDA)
add_cython_target(_cuda CXX)
Expand Down
2 changes: 1 addition & 1 deletion implicit/gpu/bpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def fit(self, user_items, show_progress=True):
Y,
self.learning_rate,
self.regularization,
rs.randint(2 ** 31),
rs.randint(2**31),
self.verify_negative_samples,
)
progress.update(1)
Expand Down
2 changes: 1 addition & 1 deletion implicit/gpu/matrix_factorization_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def check_random_state(random_state):
"""
if isinstance(random_state, np.random.RandomState):
# we need to convert from numpy random state our internal random state
return implicit.gpu.RandomState(random_state.randint(2 ** 31))
return implicit.gpu.RandomState(random_state.randint(2**31))

# otherwise try to initialize a new one, and let it fail through
# on the numpy side if it doesn't work
Expand Down
2 changes: 1 addition & 1 deletion implicit/nearest_neighbours.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def normalize(X):
"""equivalent to scipy.preprocessing.normalize on sparse matrices
, but lets avoid another dependency just for a small utility function"""
X = coo_matrix(X)
X.data = X.data / sqrt(bincount(X.row, X.data ** 2))[X.row]
X.data = X.data / sqrt(bincount(X.row, X.data**2))[X.row]
return X


Expand Down
2 changes: 1 addition & 1 deletion implicit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def augment_inner_product_matrix(factors):

# add an extra dimension so that the norm of each row is the same
# (max_norm)
extra_dimension = np.sqrt(max_norm ** 2 - norms ** 2)
extra_dimension = np.sqrt(max_norm**2 - norms**2)
return max_norm, np.append(factors, extra_dimension.reshape(norms.shape[0], 1), axis=1)


Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

scikit-build>=0.13.1
pytest
black==21.12b0
black==22.3.0
flake8
isort
codespell
Expand Down

0 comments on commit 7171765

Please sign in to comment.