Memory & Device Utilities
Functions for managing device placement, memory-aware transfers, and memory reporting.
torchlinops.utils.get_device
torchlinops.utils.device_ordinal
torchlinops.utils.memory_aware_to
Move the given module to the specified device while being memory aware.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The module to move to the specified device.
TYPE:
|
device
|
The device to which the module should be moved. |
| RETURNS | DESCRIPTION |
|---|---|
Module
|
The module moved to the specified device. |
Source code in src/torchlinops/utils/_device.py
torchlinops.utils.memory_aware_deepcopy
Create a deep copy of the given module while being memory aware.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The module to be deep copied.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Module
|
A deep copy of the module. |
Source code in src/torchlinops/utils/_device.py
torchlinops.utils.ModuleMemoryMap
dataclass
Remembers module and submodule memory layout.
Source code in src/torchlinops/utils/_device.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 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 170 171 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 202 203 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 247 248 249 250 251 | |
register_module
Construct the smallest set of tensors that can be used to hold all the parameters in a module.
Source code in src/torchlinops/utils/_device.py
memory_aware_to
Move a module to a device, without unnecessary memory overhead.
Source code in src/torchlinops/utils/_device.py
memory_aware_deepcopy
Deepcopy a module, without unnecessary memory overhead.
Source code in src/torchlinops/utils/_device.py
torchlinops.utils.MemReporter
A utility class for reporting memory usage of PyTorch tensors by device.
Features: - Tracks tensors in Python scope (excluding C++-managed buffers) - Reports memory in GB (base 1024) or GiB (base 1000) format - Identifies root tensors to avoid double-counting overlapping memory - Supports module-specific analysis or global tensor tracking
| PARAMETER | DESCRIPTION |
|---|---|
format_mode
|
Memory unit format (GB=base1024, GiB=base1000)
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
tensors |
Name-to-tensor mapping for all collected tensors
TYPE:
|
device_map |
Maps devices to tensor names
TYPE:
|
Examples:
>>> reporter = MemReporter(format_mode="GiB")
>>> reporter.report() # Analyze all tracked tensors
>>> reporter.report(module=my_model) # Analyze tensors in a specific module
Notes
- Does not track:
- Tensors allocated in C++ (e.g. backward pass buffers)
- Gradient tensors (.grad attributes)
- Non-contiguous tensors may have inefficient pointer calculations
Source code in src/torchlinops/utils/_device.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 | |
__init__
report
Source code in src/torchlinops/utils/_device.py
torchlinops.utils.cdata
torchlinops.utils.tensor_memory_span
Compute the min and max memory offsets of a tensor.
Supports negative strides (even though PyTorch doesn't.)
Source code in src/torchlinops/utils/_device.py
torchlinops.utils.same_storage
Determine if tensors share the same storage or not