File size: 3,730 Bytes
62dbcfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
62
63
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from typing import Any

import torch
from torch import Tensor, autocast, nn
from traiNNer.archs import ARCH_REGISTRY, SPANDREL_REGISTRY
from traiNNer.losses.basic_loss import L1Loss

ALL_REGISTRIES = list(ARCH_REGISTRY)
EXCLUDE_BENCHMARK_ARCHS = {
    "dat",
    "hat",
    "swinir",
    "lmlt",
    "vggstylediscriminator",
    "unetdiscriminatorsn",
    "vggfeatureextractor",
}

FILTERED_REGISTRY = [
    (name, arch)
    for name, arch in list(SPANDREL_REGISTRY) + list(ARCH_REGISTRY)
    if name not in EXCLUDE_BENCHMARK_ARCHS
]
# For archs that have extra parameters, list all combinations that need to be benchmarked.
EXTRA_ARCH_PARAMS: dict[str, list[dict[str, Any]]] = {
    k: [] for k, _ in FILTERED_REGISTRY
}
EXTRA_ARCH_PARAMS["realplksr"] = [
    {"upsampler": "dysample"},
    {"upsampler": "pixelshuffle"},
]
# A list of tuples in the format of (name, arch, extra_params).
FILTERED_REGISTRIES_PARAMS = [
    (name, arch, extra_params)
    for (name, arch) in FILTERED_REGISTRY
    for extra_params in (EXTRA_ARCH_PARAMS[name] if EXTRA_ARCH_PARAMS[name] else [{}])
]


def format_extra_params(extra_arch_params: dict[str, Any]) -> str:
    out = ""

    for k, v in extra_arch_params.items():
        if isinstance(v, str):
            out += f"{v} "
        else:
            out += f"{k}={v} "

    return out.strip()


def compare_precision(

    net: nn.Module, input_tensor: Tensor, criterion: nn.Module

) -> tuple[float, float]:
    with torch.inference_mode():
        fp32_output = net(input_tensor)

    fp16_loss = None
    try:
        with autocast(dtype=torch.float16, device_type="cuda"), torch.inference_mode():
            fp16_output = net(input_tensor)
        fp16_loss = criterion(fp16_output.float(), fp32_output).item()
    except Exception as e:
        print(f"Error in FP16 inference: {e}")
        fp16_loss = float("inf")

    bf16_loss = None
    try:
        with autocast(dtype=torch.bfloat16, device_type="cuda"), torch.inference_mode():
            bf16_output = net(input_tensor)
        bf16_loss = criterion(bf16_output.float(), fp32_output).item()
    except Exception as e:
        print(f"Error in BF16 inference: {e}")
        bf16_loss = float("inf")

    return fp16_loss, bf16_loss


if __name__ == "__main__":
    scale = 4
    for name, arch, extra_arch_params in FILTERED_REGISTRIES_PARAMS:
        label = f"{name} {format_extra_params(extra_arch_params)} {scale}x"

        try:
            if name not in {
                "rcan",
                "esrgan",
                "compact",
                "span",
                "dat_2",
                "spanplus",
                "realplksr",
            }:
                continue

            net: nn.Module = arch(scale=scale, **extra_arch_params).eval().to("cuda")
            # net.load_state_dict(
            #     torch.load(
            #         r"DAT_2_x4.pth",
            #         weights_only=True,
            #     )["params"]
            # )

            input_tensor = torch.randn((2, 3, 192, 192), device="cuda")
            criterion = L1Loss(1.0)

            fp16_loss, bf16_loss = compare_precision(net, input_tensor, criterion)
            diff = abs(fp16_loss - bf16_loss)

            if fp16_loss < bf16_loss:
                print(
                    f"{label:>30s}: FP16: {fp16_loss:.6f}; BF16: {bf16_loss:.6f}; diff = {diff}"
                )
            else:
                print(
                    f"{label:>30s}: BF16: {bf16_loss:.6f}; FP16: {fp16_loss:.6f}; diff = {diff}"
                )
        except Exception as e:
            print(f"skip {label}", e)