Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move env var checks into patch_needed() function #22

Merged
merged 1 commit into from
Aug 17, 2022

Conversation

gmarkall
Copy link
Contributor

With this change, the patch_needed() function can also be used from other libraries without them needing to reimplement the logic and env vars to decide whether to check the driver and runtime versions.

I also updated the message about not patching the Numba codegen - the reason for not patching the codegen is not always the driver version (sometimes it's the environment variable) and the reason should be apparent from previous log lines, so it now only states that the codegen is not being patched.

Some of my experiments/checks with this change (similar to those I did for #20):

$ PTXCOMPILER_APPLY_NUMBA_CODEGEN_PATCH=1 NUMBA_CUDA_LOG_LEVEL=DEBUG python -c "from ptxcompiler.patch import patch_numba_codegen_if_needed; patch_numba_codegen_if_needed()"
== CUDA (ptxcompiler) [2612] DEBUG -- PTXCOMPILER_APPLY_NUMBA_CODEGEN_PATCH=1
== CUDA (ptxcompiler) [2612] DEBUG -- Patching Numba codegen for forward compatibility

$ PTXCOMPILER_APPLY_NUMBA_CODEGEN_PATCH=0 NUMBA_CUDA_LOG_LEVEL=DEBUG python -c "from ptxcompiler.patch import patch_numba_codegen_if_needed; patch_numba_codegen_if_needed()"
== CUDA (ptxcompiler) [297] DEBUG -- PTXCOMPILER_APPLY_NUMBA_CODEGEN_PATCH=0
== CUDA (ptxcompiler) [709] DEBUG -- CUDA Driver version 11.7
== CUDA (ptxcompiler) [709] DEBUG -- CUDA Runtime version 11.6
== CUDA (ptxcompiler) [709] DEBUG -- Not patching Numba codegen

$ PTXCOMPILER_CHECK_NUMBA_CODEGEN_PATCH_NEEDED=1 PTXCOMPILER_APPLY_NUMBA_CODEGEN_PATCH=0 NUMBA_CUDA_LOG_LEVEL=DEBUG python -c "from ptxcompiler.patch import patch_numba_codegen_if_needed; patch_numba_codegen_if_needed()"
== CUDA (ptxcompiler) [295] DEBUG -- PTXCOMPILER_APPLY_NUMBA_CODEGEN_PATCH=0
== CUDA (ptxcompiler) [295] DEBUG -- PTXCOMPILER_CHECK_NUMBA_CODEGEN_PATCH_NEEDED=1
== CUDA (ptxcompiler) [671] DEBUG -- CUDA Driver version 11.7
== CUDA (ptxcompiler) [671] DEBUG -- CUDA Runtime version 11.6
== CUDA (ptxcompiler) [671] DEBUG -- Not patching Numba codegen

$ PTXCOMPILER_CHECK_NUMBA_CODEGEN_PATCH_NEEDED=0 PTXCOMPILER_APPLY_NUMBA_CODEGEN_PATCH=0 NUMBA_CUDA_LOG_LEVEL=DEBUG python -c "from ptxcompiler.patch import patch_numba_codegen_if_needed; patch_numba_codegen_if_needed()"
== CUDA (ptxcompiler) [294] DEBUG -- PTXCOMPILER_APPLY_NUMBA_CODEGEN_PATCH=0
== CUDA (ptxcompiler) [294] DEBUG -- PTXCOMPILER_CHECK_NUMBA_CODEGEN_PATCH_NEEDED=0
== CUDA (ptxcompiler) [295] DEBUG -- Not patching Numba codegen

$ PTXCOMPILER_CHECK_NUMBA_CODEGEN_PATCH_NEEDED=0 PTXCOMPILER_APPLY_NUMBA_CODEGEN_PATCH=1 NUMBA_CUDA_LOG_LEVEL=DEBUG python -c "from ptxcompiler.patch import patch_numba_codegen_if_needed; patch_numba_codegen_if_needed()"
== CUDA (ptxcompiler) [293] DEBUG -- PTXCOMPILER_APPLY_NUMBA_CODEGEN_PATCH=1
== CUDA (ptxcompiler) [293] DEBUG -- Patching Numba codegen for forward compatibility

$ PTXCOMPILER_CHECK_NUMBA_CODEGEN_PATCH_NEEDED=0 NUMBA_CUDA_LOG_LEVEL=DEBUG python -c "from ptxcompiler.patch import patch_numba_codegen_if_needed; patch_numba_codegen_if_needed()"
== CUDA (ptxcompiler) [294] DEBUG -- PTXCOMPILER_CHECK_NUMBA_CODEGEN_PATCH_NEEDED=0
== CUDA (ptxcompiler) [295] DEBUG -- Not patching Numba codegen

$ PTXCOMPILER_CHECK_NUMBA_CODEGEN_PATCH_NEEDED=1 NUMBA_CUDA_LOG_LEVEL=DEBUG python -c "from ptxcompiler.patch import patch_numba_codegen_if_needed; patch_numba_codegen_if_needed()"
== CUDA (ptxcompiler) [292] DEBUG -- PTXCOMPILER_CHECK_NUMBA_CODEGEN_PATCH_NEEDED=1
== CUDA (ptxcompiler) [669] DEBUG -- CUDA Driver version 11.7
== CUDA (ptxcompiler) [669] DEBUG -- CUDA Runtime version 11.6
== CUDA (ptxcompiler) [669] DEBUG -- Not patching Numba codegen

$ NUMBA_CUDA_LOG_LEVEL=DEBUG python -c "from ptxcompiler.patch import patch_numba_codegen_if_needed; patch_numba_codegen_if_needed()"
== CUDA (ptxcompiler) [670] DEBUG -- CUDA Driver version 11.7
== CUDA (ptxcompiler) [670] DEBUG -- CUDA Runtime version 11.6
== CUDA (ptxcompiler) [670] DEBUG -- Not patching Numba codegen

With this change, the `patch_needed()` function can also be used from
other libraries without them needing to reimplement the logic and env
vars to decide whether to check the driver and runtime versions.
@gmarkall gmarkall requested a review from wence- August 15, 2022 09:12
Copy link
Contributor

@wence- wence- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the logic would be easier to read with my suggested changes (less double negation and tracking values over distance), but otherwise looks fine.

Comment on lines +113 to +121
if apply is not None:
logger.debug(f"PTXCOMPILER_APPLY_NUMBA_CODEGEN_PATCH={apply}")
try:
apply = int(apply)
except ValueError:
apply = False

if apply:
return True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:

Suggested change
if apply is not None:
logger.debug(f"PTXCOMPILER_APPLY_NUMBA_CODEGEN_PATCH={apply}")
try:
apply = int(apply)
except ValueError:
apply = False
if apply:
return True
if apply is not None:
logger.debug(f"PTXCOMPILER_APPLY_NUMBA_CODEGEN_PATCH={apply}")
try:
return int(apply)
except ValueError:
return False

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this changes the behaviour so that the check won't be performed if the user sets PTXCOMPILER_APPLY_NUMBA_CODEGEN_PATCH to 0. Do we want to change this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, I see I changed the logic here too. I think we should leave as is. So APPLY=1 is a hard "definitely apply" and APPLY=0 is a soft "don't apply": first check if CHECK=1

Comment on lines +126 to +136
if check is not None:
logger.debug(f"PTXCOMPILER_CHECK_NUMBA_CODEGEN_PATCH_NEEDED={check}")
try:
check = int(check)
except ValueError:
check = False
else:
check = True

if not check:
return False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if check is not None:
logger.debug(f"PTXCOMPILER_CHECK_NUMBA_CODEGEN_PATCH_NEEDED={check}")
try:
check = int(check)
except ValueError:
check = False
else:
check = True
if not check:
return False
if check is not None:
logger.debug(f"PTXCOMPILER_CHECK_NUMBA_CODEGEN_PATCH_NEEDED={check}")
try:
return int(check)
except ValueError:
return False

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user sets PTXCOMPILER_CHECK_NUMBA_CODEGEN_PATCH_NEEDED to 1, won't this end up always applying the patch instead of checking if the patch is needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, yes.

@gmarkall
Copy link
Contributor Author

@wence- Many thanks for the review - comments / suggestions replied to.

Copy link
Contributor

@bdice bdice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic seems fine to me.

@gmarkall
Copy link
Contributor Author

Many thanks all!

@gmarkall gmarkall merged commit 87c009e into rapidsai:main Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants