-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_arange_dtype.py
51 lines (34 loc) · 1.28 KB
/
check_arange_dtype.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
# TORCH_COMPILE_DEBUG=1 python check_arange_dtype.py
# TORCH_LOGS=+output_code python check_arange_dtype.py
import torch
def func_a(t, out_size):
_, _, h, w = t.shape
j = torch.arange(out_size[0], device=t.device, dtype=torch.float32)
y_f32 = (h / out_size[0]) * j
y_f32 = y_f32.unsqueeze(-1)
y = y_f32.to(torch.long)
i = torch.arange(out_size[1], device=t.device, dtype=torch.float32)
x_f32 = (w / out_size[1]) * i
x = x_f32.to(torch.long)
output = t[..., y, x]
return output
def func_b(t, out_size):
_, _, h, w = t.shape
j = torch.arange(out_size[0], device=t.device).to(dtype=torch.float32)
y_f32 = (h / out_size[0]) * j
y_f32 = y_f32.unsqueeze(-1)
y = y_f32.to(torch.long)
i = torch.arange(out_size[1], device=t.device).to(dtype=torch.float32)
x_f32 = (w / out_size[1]) * i
x = x_f32.to(torch.long)
output = t[..., y, x]
return output
cfunc_a = torch.compile(func_a, dynamic=True, fullgraph=True)
cfunc_b = torch.compile(func_b, dynamic=True, fullgraph=True)
device = "cuda"
isize = (20, 30)
a = torch.arange(2 * 3 * isize[0] * isize[1], device=device).reshape(2, 3, *isize)
out_size = (11, 12)
output_a = cfunc_a(a, out_size)
output_b = cfunc_b(a, out_size)
torch.testing.assert_close(output_a, output_b)