-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kye
committed
Dec 17, 2023
1 parent
2c89b26
commit cbb33a9
Showing
5 changed files
with
61 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,54 @@ | ||
import torch | ||
import functools | ||
import logging | ||
import torch | ||
import functools | ||
import logging | ||
|
||
# Logging initialization | ||
logging.basicConfig( | ||
level=logging.INFO, | ||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' | ||
level=logging.INFO, | ||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", | ||
) | ||
|
||
|
||
# Main function | ||
def track_cuda_memory_usage(func): | ||
"""Track CUDA memory usage of a function. | ||
Args: | ||
func (function): The function to be tracked. | ||
Returns: | ||
function: The wrapped function. | ||
Example: | ||
>>> @track_cuda_memory_usage | ||
>>> def train(): | ||
>>> pass | ||
>>> train() | ||
""" | ||
|
||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs): | ||
if not torch.cuda.is_available(): | ||
logging.warning("CUDA is not available, skip tracking memory usage") | ||
return func(*args, **kwargs) | ||
|
||
torch.cuda.synchronize() | ||
before_memory = torch.cuda.memory_allocated() | ||
|
||
try: | ||
result = func(*args, **kwargs) | ||
except Exception as error: | ||
logging.error(f"Error occurs when running {func.__name__}: {error}") | ||
raise | ||
|
||
finally: | ||
torch.cuda.synchronize() | ||
after_memory = torch.cuda.memory_allocated() | ||
memory_diff = after_memory - before_memory | ||
logging.info(f"Memory usage of {func.__name__}: {memory_diff} bytes") | ||
|
||
logging.info( | ||
f"Memory usage of {func.__name__}: {memory_diff} bytes" | ||
) | ||
|
||
return result | ||
return wrapper | ||
|
||
return wrapper |