-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrafts.py
175 lines (121 loc) · 3.13 KB
/
drafts.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
169
170
171
172
173
from socket import AI_DEFAULT
def get_element(nested_list, indices):
element = nested_list
for index in indices:
element = element[index]
return element
def set_element(nested_list, indices, to):
element = nested_list
for i, index in enumerate(indices):
if i == len(indices)-1:
element[index] = to
return
element = element[index]
my_list = [[1, 2], [3, 4]]
indices = [0, 1]
element = get_element(my_list, indices)
def add(l, ashape, r, bshape, out, outshape):
stack = []
def getIdx(shape):
return [min(dim-1, idx) for dim, idx in zip(shape, stack)]
def nested_loop(depth: int):
if depth == 3:
res = get_element(l, getIdx(ashape))\
+ get_element(r, getIdx(bshape))
set_element(
out,
stack,
res
)
return
for i in range(outshape[depth]):
stack.append(i)
nested_loop(depth+1)
stack.pop()
nested_loop(0)
def add2(l, r, out, dirs):
stack = []
def getIndices() -> tuple[list[int], list[int]]:
l = []
r = []
for broadcastdir, idx in zip(dirs, stack):
match broadcastdir:
case 'ltr':
l.append(0)
r.append(idx)
case 'rtl':
l.append(idx)
r.append(0)
case None:
l.append(idx)
r.append(idx)
case _:
raise Rc<Tensor>ueError(f'huh?, got {broadcastdir=}')
return l, r
def nested_loop(depth: int):
if depth == 3:
l_idx, r_idx = getIndices()
res = None
try:
res = get_element(l, l_idx) + get_element(r, r_idx)
except:
breakpoint()
set_element(
out,
stack,
res
)
return
for i in range(outshape[depth]):
stack.append(i)
nested_loop(depth+1)
stack.pop()
nested_loop(0)
l = [[[1],
[2],
[3],
[1]],
[[5],
[3],
[9],
[2]]]
lshape = [2, 4, 1]
r = [[[1, 2, 5],
[1, 2, 2],
[2, 2, 5],
[7, 2, 3]]]
rshape = [1, 4, 3]
empty = [[[None, None, None],
[None, None, None],
[None, None, None],
[None, None, None]],
[[None, None, None],
[None, None, None],
[None, None, None],
[None, None, None]]]
outshape = [2, 4, 3]
out = copy.deepcopy(empty)
out2 = copy.deepcopy(empty)
add(l, lshape, r, rshape, out, outshape)
print(out)
broadcast_dirs = ['rtl', None, 'ltr']
add2(l, r, out2, broadcast_dirs)
print(out2)
# l = [
# [1, 2, 2],
# ]
# a_shape = [1, 3]
# r = [
# [1, 1, 1],
# [1, 1, 1],
# [1, 1, 1],
# [1, 1, 1],
# ]
# b_shape = [4, 3]
# bc_dir = ['right', None]
# out = []
# out_shape = []
# for l_d, r_d in zip(a_shape, b_shape):
# out_shape.append(max(l_d, r_d))
# print(out_shape)
# for dir in bc_dir: