-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_interp_mem_format.py
163 lines (113 loc) · 6.56 KB
/
check_interp_mem_format.py
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
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
import torch
print(torch.__version__)
def t1(x, mode, aa):
do_squeeze = False
if x.dim() == 3:
do_squeeze = True
x = x[None, ...]
out = torch.nn.functional.interpolate(
x, size=224, mode=mode, antialias=aa
)
if do_squeeze:
out = out[0, ...]
return out
def t2(x, mode):
do_squeeze = False
if x.dim() == 4:
do_squeeze = True
x = x[None, ...]
out = torch.nn.functional.interpolate(
x, size=224, mode=mode
)
if do_squeeze:
out = out[0, ...]
return out
c_t1 = torch.compile(t1)
# for aa in [False, True]:
for aa in [True, False]:
for mode in ["bilinear", "nearest", "nearest-exact", "bicubic"]:
if "nearest" in mode and aa:
continue
for dtype in [torch.uint8, torch.float32, torch.float64]:
if mode == "bicubic" and dtype == torch.uint8:
continue
for mf in [torch.channels_last, torch.contiguous_format]:
for device in ["cpu", "cuda"]:
if device == "cuda" and not torch.cuda.is_available():
continue
if device == "cuda" and dtype == torch.uint8:
continue
print("-", aa, mode, mf, device, dtype)
x = torch.randint(0, 256, size=(1, 3, 256, 256), dtype=dtype, device=device).contiguous(memory_format=mf)
y_ref = t1(x, mode, aa)
_ = c_t1(x, mode, aa)
input_mem_format = "CL" if x.is_contiguous(memory_format=torch.channels_last) else "CF"
if input_mem_format == "CF":
assert x.is_contiguous(memory_format=torch.contiguous_format)
output_mem_format = "CL" if y_ref.is_contiguous(memory_format=torch.channels_last) else "CF"
if output_mem_format == "CF":
assert y_ref.is_contiguous(memory_format=torch.contiguous_format)
# assert input_mem_format == output_mem_format, f"1 {mf}, {device}, {dtype}: {output_mem_format} != {input_mem_format}"
if input_mem_format != output_mem_format:
print(f"1 {mf}, {device}, {dtype}: {output_mem_format} != {input_mem_format}\n")
x = x[0, ...]
x = x[None, ...]
# # Restride x
# shape = x.shape
# strides = list(x.stride())
# strides[0] = shape[1] * shape[2] * shape[3]
# x = x.as_strided(shape, strides)
y = t1(x, mode, aa)
input_mem_format = "CL" if x.is_contiguous(memory_format=torch.channels_last) else "CF"
if input_mem_format == "CF":
assert x.is_contiguous(memory_format=torch.contiguous_format)
output_mem_format = "CL" if y.is_contiguous(memory_format=torch.channels_last) else "CF"
if output_mem_format == "CF":
assert y.is_contiguous(memory_format=torch.contiguous_format)
# assert input_mem_format == output_mem_format, f"2 {mf}, {device}, {dtype}: {output_mem_format} != {input_mem_format}"
if input_mem_format != output_mem_format:
print(f"2 {mf}, {device}, {dtype}: {output_mem_format} != {input_mem_format}\n")
# torch.testing.assert_close(y_ref, y)
y = c_t1(x, mode, aa)
input_mem_format = "CL" if x.is_contiguous(memory_format=torch.channels_last) else "CF"
if input_mem_format == "CF":
assert x.is_contiguous(memory_format=torch.contiguous_format)
output_mem_format = "CL" if y.is_contiguous(memory_format=torch.channels_last) else "CF"
if output_mem_format == "CF":
assert y.is_contiguous(memory_format=torch.contiguous_format)
# assert input_mem_format == output_mem_format, f"2 {mf}, {device}, {dtype}: {output_mem_format} != {input_mem_format}"
if input_mem_format != output_mem_format:
print(f"3 {mf}, {device}, {dtype}: {output_mem_format} != {input_mem_format}\n")
for mode in ["trilinear", "nearest", "nearest-exact", ]:
for dtype in [torch.float32, torch.float64]:
for mf in [torch.channels_last_3d, torch.contiguous_format]:
for device in ["cpu", "cuda"]:
if device == "cuda" and not torch.cuda.is_available():
continue
if device == "cuda" and dtype == torch.uint8:
continue
print("-", mode, mf, device, dtype)
x = torch.randint(0, 256, size=(1, 2, 3, 256, 256), dtype=dtype, device=device).contiguous(memory_format=mf)
y_ref = t2(x, mode)
input_mem_format = "CL" if x.is_contiguous(memory_format=torch.channels_last_3d) else "CF"
if input_mem_format == "CF":
assert x.is_contiguous(memory_format=torch.contiguous_format)
output_mem_format = "CL" if y_ref.is_contiguous(memory_format=torch.channels_last_3d) else "CF"
if output_mem_format == "CF":
assert y_ref.is_contiguous(memory_format=torch.contiguous_format)
# assert input_mem_format == output_mem_format, f"1 {mf}, {device}, {dtype}: {output_mem_format} != {input_mem_format}"
if input_mem_format != output_mem_format:
print(f"1 {mf}, {device}, {dtype}: {output_mem_format} != {input_mem_format}\n")
x = x[0, ...]
x = x[None, ...]
y = t2(x, mode)
input_mem_format = "CL" if x.is_contiguous(memory_format=torch.channels_last_3d) else "CF"
if input_mem_format == "CF":
assert x.is_contiguous(memory_format=torch.contiguous_format)
output_mem_format = "CL" if y.is_contiguous(memory_format=torch.channels_last_3d) else "CF"
if output_mem_format == "CF":
assert y.is_contiguous(memory_format=torch.contiguous_format)
# assert input_mem_format == output_mem_format, f"2 {mf}, {device}, {dtype}: {output_mem_format} != {input_mem_format}"
if input_mem_format != output_mem_format:
print(f"2 {mf}, {device}, {dtype}: {output_mem_format} != {input_mem_format}\n")
# torch.testing.assert_close(y_ref, y)