Utilities
seed
Functions:
-
seed_everything–Sets the seed for generating random number in Pytorch, numpy and Python.
seed_everything
seed_everything(seed: int) -> None
Sets the seed for generating random number in Pytorch, numpy and Python.
Parameters:
-
seed(int) –The desired seed.
Notes
- You may also want to set
torch.backends.cudnn.deterministic = Trueandtorch.backends.cudnn.benchmark = Falsefor full determinism on GPU.
Source code in tgm/util/seed.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
logging
Functions:
-
enable_logging–Enable library-wide logging to stdout and optionally to a file.
-
log_gpu–Function decorator to log GPU memory usage during a function call.
-
log_latency–Function decorator to log latency at configurable log level.
-
log_metric–Log a metric with optional epoch and structured JSON output.
-
log_metrics_dict–Log a set of metric with optional epoch and structured JSON output.
enable_logging
enable_logging(
*,
console_log_level: int = INFO,
file_log_level: int = DEBUG,
log_file_path: str | Path | None = None,
) -> None
Enable library-wide logging to stdout and optionally to a file.
Parameters:
-
console_log_level(int, default:INFO) –Logging level for console stream handler (default = logging.INFO).
-
file_log_level(int, default:DEBUG) –Logging level for file handler if configured (default = logging.DEBUG).
-
log_file_path(Optional[str | Path], default:None) –Optional path to a log file.
Source code in tgm/util/logging.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
log_gpu
Function decorator to log GPU memory usage during a function call.
Logs human-readable info at level, and JSON-formatted debug log at DEBUG.
Usage
- @log_gpu # Logs at logging.INFO
- @log_gpu() # Logs at logging.INFO
- @log_gpu(level=logging.DEBUG) # Logs at DEBUG (JSON included)
Source code in tgm/util/logging.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
log_latency
Function decorator to log latency at configurable log level.
Logs human-readable info at level, and JSON-formatted debug log at DEBUG.
Usage
- @log_latency # Logs at logging.INFO
- @log_latency() # Logs at logging.INFO
- @log_latency=level=logging.DEBUG) # Logs at logging.DEBUG (JSON included)
Returns:
-
Any–The output of calling func.
Source code in tgm/util/logging.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
log_metric
log_metric(
metric_name: str,
metric_value: Any,
*,
epoch: int | None = None,
level: int = INFO,
extra: Dict[str, Any] | None = None,
logger: Logger | None = None,
) -> None
Log a metric with optional epoch and structured JSON output.
Logs human-readable info at level, and JSON-formatted debug log at DEBUG.
Parameters:
-
metric_name(str) –Name of the metric to log.
-
metric_value(Any) –Value of the metric to log.
-
epoch(Optional[int], default:None) –Optional epoch number.
-
level(int, default:INFO) –Logging level for human-readable log (default INFO)
-
extra(Dict[str, Any], default:None) –Optional dictionary of extra metadata to include in JSON.
-
logger(Optional[Logger], default:None) –Logger to log to, defaults to tgm.util logger.
Source code in tgm/util/logging.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
log_metrics_dict
log_metrics_dict(
metrics_dict: Dict[str, Any],
*,
epoch: int | None = None,
level: int = INFO,
extra: Dict[str, Any] | None = None,
logger: Logger | None = None,
) -> None
Log a set of metric with optional epoch and structured JSON output.
Logs human-readable info at level, and JSON-formatted debug log at DEBUG.
Note: This is equivalent to calling log_metric for each key-value pair.
Parameters:
-
metrics_dict(Dict[str, Any]) –Dictionary of metric_name: metric_value pairs.
-
epoch(Optional[int], default:None) –Optional epoch number.
-
level(int, default:INFO) –Logging level for human-readable log (default INFO)
-
extra(Dict[str, Any], default:None) –Optional dictionary of extra metadata to include in JSON.
-
logger(Optional[Logger], default:None) –Logger to log to, defaults to tgm.util logger.
Source code in tgm/util/logging.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |