File: resnet_memory_profiler.py

package info (click to toggle)
pytorch-cuda 2.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 161,620 kB
  • sloc: python: 1,278,832; cpp: 900,322; ansic: 82,710; asm: 7,754; java: 3,363; sh: 2,811; javascript: 2,443; makefile: 597; ruby: 195; xml: 84; objc: 68
file content (27 lines) | stat: -rw-r--r-- 769 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import torchvision.models as models

import torch
import torch.autograd.profiler as profiler


for with_cuda in [False, True]:
    model = models.resnet18()
    inputs = torch.randn(5, 3, 224, 224)
    sort_key = "self_cpu_memory_usage"
    if with_cuda and torch.cuda.is_available():
        model = model.cuda()
        inputs = inputs.cuda()
        sort_key = "self_cuda_memory_usage"
        print("Profiling CUDA Resnet model")
    else:
        print("Profiling CPU Resnet model")

    with profiler.profile(profile_memory=True, record_shapes=True) as prof:
        with profiler.record_function("root"):
            model(inputs)

    print(
        prof.key_averages(group_by_input_shape=True).table(
            sort_by=sort_key, row_limit=-1
        )
    )