utils¶
- class trojanzoo.utils.lock.Lock[source]¶
A boolean lock class used for contextmanager. It’s used in
KFAC
to avoid auxiliary computation operations.- Example:
>>> from trojanzoo.utils.lock import Lock >>> >>> track = Lock() >>> print(bool(track)) False >>> with track(): >>> print(bool(track)) True >>> print(bool(track)) False >>> track.enable() >>> print(bool(track)) True >>> track.disable() >>> print(bool(track)) False
- trojanzoo.utils.memory.empty_cache(threshold=None)[source]¶
Call
torch.cuda.empty_cache
to empty GPU cache whentorch.cuda.memory_cached()
>
threshold
MB
.- Parameters:
threshold (float) – The cached memory threshold (MB). Defaults to
env['cache_threshold']
.
- trojanzoo.utils.memory.output_memory(device=None, full=False, indent=0, **kwargs)[source]¶
Output memory usage information.
- Parameters:
device (None | str | int | device) – Passed to
torch.cuda.memory_summary
ortorch.cuda.memory_allocated
|torch.cuda.memory_reserved
. Defaults toNone
.full (bool) – Whether to call
torch.cuda.memory_summary
. Otherwise, calltorch.cuda.memory_allocated
andtorch.cuda.memory_reserved
. Defaults toFalse
.indent (int) – The space indent for the entire string. Defaults to
0
.**kwargs – Keyword arguments passed to
torch.cuda.memory_summary
.