Skip to content

Better cuBLAS handle management - #1389

Merged
ksivaman merged 7 commits into
NVIDIA:mainfrom
ptrendx:pr_cublas_handle_mgmt
Mar 17, 2025
Merged

ksivaman merged 7 commits into
NVIDIA:mainfrom
ptrendx:pr_cublas_handle_mgmt

Conversation

@ptrendx

@ptrendx ptrendx commented Jan 2, 2025 •

Copy link
Copy Markdown
Member

Description

Create only 1 (per thread) cublasLtHandle in order to not leak memory. We do not want to actually destroy the handle, since it would incur an implicit cudaDeviceSynchronize call (see cuBLAS docs).

Fixes #1372

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactor

Changes

  • Introduced a singleton manager for cuBLAS handles similar to the one used for cuDNN handles.

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Signed-off-by: Przemek Tredak <ptredak@nvidia.com>

@ksivaman ksivaman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@shenzhenghai

Copy link
Copy Markdown

May I have a question about the commit? If there is multiple devices invoke cublas_gemm(), it seems always use the same cublasLt handle?
The document mentioned "one cuBLASLt handle should be created for each device".

@shenzhenghai

Copy link
Copy Markdown

May I have a question about the commit? If there is multiple devices invoke cublas_gemm(), it seems always use the same cublasLt handle? The document mentioned "one cuBLASLt handle should be created for each device".

@ptrendx @ksivaman any comment on my concern? Thanks.

@ptrendx

ptrendx commented Jan 6, 2025

Copy link
Copy Markdown
Member Author

@shenzhenghai Yeah, if your usage is to have multiple devices per thread then you are correct - it would use the same handle, which is wrong. I don't necessarily like the fact that fixing this will require calling cudaGetDevice before every cuBLAS call - this adds overhead and the most typical usage by the DL Frameworks nowadays is 1 GPU per process. But I agree that the functional correctness is the most important - will submit a fix (also will fix the cuDNN handle management, since it has similar problem).

ptrendx and others added 2 commits January 6, 2025 14:50
Signed-off-by: Przemek Tredak <ptredak@nvidia.com>
@ptrendx

ptrendx commented Jan 6, 2025

Copy link
Copy Markdown
Member Author

/te-ci

Signed-off-by: Przemek Tredak <ptredak@nvidia.com>
@ptrendx

ptrendx commented Jan 7, 2025

Copy link
Copy Markdown
Member Author

/te-ci

@timmoon10 timmoon10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@ptrendx

ptrendx commented Jan 7, 2025

Copy link
Copy Markdown
Member Author

/te-ci

1 similar comment
@ksivaman

Copy link
Copy Markdown
Member

/te-ci

@ksivaman
ksivaman force-pushed the pr_cublas_handle_mgmt branch from f1a6527 to a8299ab Compare March 17, 2025 16:45
@ksivaman

Copy link
Copy Markdown
Member

/te-ci

@ksivaman
ksivaman merged commit 7ddc593 into NVIDIA:main Mar 17, 2025
lhb8125 pushed a commit to lhb8125/TransformerEngine that referenced this pull request Apr 8, 2025
* Do not create multiple cublas handle

Signed-off-by: Przemek Tredak <ptredak@nvidia.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix for multiple GPUs per thread

Signed-off-by: Przemek Tredak <ptredak@nvidia.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix multithreaded execution

Signed-off-by: Przemek Tredak <ptredak@nvidia.com>

* Fix from conlfict

Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com>

---------

Signed-off-by: Przemek Tredak <ptredak@nvidia.com>
Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com>
Co-authored-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com>
@ksivaman ksivaman mentioned this pull request May 21, 2025
6 of 13 tasks
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.

Should cublasLtHandle_t be Destroyed?

4 participants