-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
168 lines (133 loc) · 4.92 KB
/
handlers.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
164
165
166
167
168
branch = "├── "
trunk = "│ "
end = "└── "
# TODO: custom handler args
# TODO: Compress the handlers
def default_handler(self, thing, indent, is_last, prefix):
# Boilerplate printing
dtype_str = ""
if hasattr(thing, "dtype"):
dtype_str = f" (dtype: {thing.dtype})"
new_prefix = prefix + (" " if is_last else trunk)
infix = end if is_last else branch
# type-level printing
self.lines.append(
f"{prefix}{infix}{type(thing).__name__}{dtype_str}"
if indent
else f"{type(thing).__name__}{dtype_str}"
) # continuation
if isinstance(thing, str):
return
elif hasattr(thing, "__getitem__"):
items = list(thing)
for i, item in enumerate(items):
self.traverse(item, indent + 1, i == len(items) - 1, new_prefix)
# TODO: Empty list
def handle_list(self, thing, indent, is_last, prefix):
dtype_str = f" ({len(thing)})"
new_prefix = prefix + (" " if is_last else trunk)
infix = end if is_last else branch
self.lines.append(
f"{prefix}{infix}{type(thing).__name__}{dtype_str}"
if indent
else f"{type(thing).__name__}{dtype_str}"
)
if thing:
self.traverse(thing[0], indent + 1, True, new_prefix)
def handle_tensor(self, thing, indent, is_last, prefix):
dtype_str = f" (dtype: {thing.dtype})"
new_prefix = prefix + (" " if is_last else trunk)
infix = end if is_last else branch
# Print the current object type
self.lines.append(
f"{prefix}{infix}{type(thing).__name__}{dtype_str}"
if indent
else f"{type(thing).__name__}{dtype_str}"
)
self.lines.append(f"{new_prefix}{trunk}(device: {thing.device})")
if thing.dim() == 0:
self.lines.append(f"{new_prefix}{end}{thing.item()}")
else:
for i, dim in enumerate(thing.shape):
infix = end if i == len(thing.shape) - 1 else branch
dim_str = f"dim_{i} ({dim})"
self.lines.append(f"{new_prefix}{infix}{dim_str}")
def handle_linear(self, thing, indent, is_last, prefix):
new_prefix = prefix + (" " if is_last else trunk)
infix = end if is_last else branch
bias = f" (bias: {True if thing.bias is not None else False})"
# Print the current object type
self.lines.append(
f"{prefix}{infix}{type(thing).__name__}{bias}"
if indent
else f"{type(thing).__name__}{bias}"
)
self.lines.append(f"{new_prefix}{branch}rows ({thing.in_features})")
self.lines.append(f"{new_prefix}{end}cols ({thing.out_features})")
def handle_subset(self, thing, indent, is_last, prefix):
dtype_str = f": {len(thing)}"
new_prefix = prefix + (" " if is_last else trunk)
infix = end if is_last else branch
# Print the current object type
self.lines.append(
f"{prefix}{infix}{type(thing).__name__}{dtype_str}"
if indent
else f"{type(thing).__name__}{dtype_str}"
)
if len(thing) != 0:
self.traverse(thing[0], indent + 1, True, new_prefix)
def handle_dataloader(self, thing, indent, is_last, prefix):
dtype_str = f" ({len(thing)})"
new_prefix = prefix + (" " if is_last else trunk)
infix = end if is_last else branch
# Print the current object type
self.lines.append(
f"{prefix}{infix}{type(thing).__name__}{dtype_str}"
if indent
else f"{type(thing).__name__}{dtype_str}"
)
if len(thing) != 0:
for d in thing:
data = d
break
self.traverse(data, indent + 1, True, new_prefix)
else:
pass
def handle_int64(self, thing, indent, is_last, prefix):
dtype_str = ""
# Print the current object type
self.lines.append(
f"{prefix}{end}{type(thing).__name__}{dtype_str}: {thing}"
if indent
else f"{type(thing).__name__}{dtype_str}: {thing}"
)
def handle_ndarray(self, thing, indent, is_last, prefix):
dtype_str = f" (dtype: {thing.dtype})"
new_prefix = prefix + (" " if is_last else trunk)
infix = end if is_last else branch
# Print the current object type
self.lines.append(
f"{prefix}{infix}{type(thing).__name__}{dtype_str}"
if indent
else f"{type(thing).__name__}{dtype_str}"
)
if thing.shape == ():
self.lines.append(f"{new_prefix}{end}{thing.item()}")
else:
for i, dim in enumerate(thing.shape):
infix = end if i == len(thing.shape) - 1 else branch
dim_str = f"dim_{i} ({dim})"
self.lines.append(f"{new_prefix}{infix}{dim_str}")
# lowercase type
handler_storage = {
"std": [("default", default_handler), ("list", handle_list)],
"torch": [
("tensor", handle_tensor),
("linear", handle_linear),
("subset", handle_subset),
("dataloader", handle_dataloader),
],
"numpy": [("ndarray", handle_ndarray), ("int64", handle_int64)],
}
def check_storage(name):
return handler_storage.get(name, None)