-
Notifications
You must be signed in to change notification settings - Fork 24
/
poolonetoone.go
204 lines (189 loc) · 5.19 KB
/
poolonetoone.go
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package paths
import "cogentcore.org/lab/tensor"
// PoolOneToOne implements one-to-one connectivity between pools within layers.
// Pools are the outer-most two dimensions of a 4D layer shape.
// If either layer does not have pools, then if the number of individual
// units matches the number of pools in the other layer, those are connected one-to-one
// otherwise each pool connects to the entire set of other units.
// If neither is 4D, then it is equivalent to OneToOne.
type PoolOneToOne struct {
// number of recv pools to connect (0 for entire number of pools in recv layer)
NPools int
// starting pool index for sending connections
SendStart int
// starting pool index for recv connections
RecvStart int
}
func NewPoolOneToOne() *PoolOneToOne {
return &PoolOneToOne{}
}
func (ot *PoolOneToOne) Name() string {
return "PoolOneToOne"
}
func (ot *PoolOneToOne) Connect(send, recv *tensor.Shape, same bool) (sendn, recvn *tensor.Int32, cons *tensor.Bool) {
switch {
case send.NumDims() == 4 && recv.NumDims() == 4:
return ot.ConnectPools(send, recv, same)
case send.NumDims() == 2 && recv.NumDims() == 4:
return ot.ConnectRecvPool(send, recv, same)
case send.NumDims() == 4 && recv.NumDims() == 2:
return ot.ConnectSendPool(send, recv, same)
case send.NumDims() == 2 && recv.NumDims() == 2:
return ot.ConnectOneToOne(send, recv, same)
}
return
}
// ConnectPools is when both recv and send have pools
func (ot *PoolOneToOne) ConnectPools(send, recv *tensor.Shape, same bool) (sendn, recvn *tensor.Int32, cons *tensor.Bool) {
sendn, recvn, cons = NewTensors(send, recv)
sNtot := send.Len()
// rNtot := recv.Len()
sNp := send.DimSize(0) * send.DimSize(1)
rNp := recv.DimSize(0) * recv.DimSize(1)
sNu := send.DimSize(2) * send.DimSize(3)
rNu := recv.DimSize(2) * recv.DimSize(3)
rnv := recvn.Values
snv := sendn.Values
npl := rNp
if ot.NPools > 0 {
npl = min(ot.NPools, rNp)
}
for i := 0; i < npl; i++ {
rpi := ot.RecvStart + i
spi := ot.SendStart + i
if rpi >= rNp || spi >= sNp {
break
}
for rui := 0; rui < rNu; rui++ {
ri := rpi*rNu + rui
for sui := 0; sui < sNu; sui++ {
si := spi*sNu + sui
off := ri*sNtot + si
cons.Values.Set(true, off)
rnv[ri] = int32(sNu)
snv[si] = int32(rNu)
}
}
}
return
}
// ConnectRecvPool is when recv has pools but send doesn't
func (ot *PoolOneToOne) ConnectRecvPool(send, recv *tensor.Shape, same bool) (sendn, recvn *tensor.Int32, cons *tensor.Bool) {
sendn, recvn, cons = NewTensors(send, recv)
sNtot := send.Len()
rNp := recv.DimSize(0) * recv.DimSize(1)
rNu := recv.DimSize(2) * recv.DimSize(3)
rnv := recvn.Values
snv := sendn.Values
npl := rNp
if ot.NPools > 0 {
npl = min(ot.NPools, rNp)
}
if sNtot == rNp { // one-to-one
for i := 0; i < npl; i++ {
rpi := ot.RecvStart + i
si := ot.SendStart + i
if rpi >= rNp || si >= sNtot {
break
}
for rui := 0; rui < rNu; rui++ {
ri := rpi*rNu + rui
off := ri*sNtot + si
cons.Values.Set(true, off)
rnv[ri] = int32(1)
snv[si] = int32(rNu)
}
}
} else { // full
for i := 0; i < npl; i++ {
rpi := ot.RecvStart + i
if rpi >= rNp {
break
}
for rui := 0; rui < rNu; rui++ {
ri := rpi*rNu + rui
for si := 0; si < sNtot; si++ {
off := ri*sNtot + si
cons.Values.Set(true, off)
rnv[ri] = int32(sNtot)
snv[si] = int32(npl * rNu)
}
}
}
}
return
}
// ConnectSendPool is when send has pools but recv doesn't
func (ot *PoolOneToOne) ConnectSendPool(send, recv *tensor.Shape, same bool) (sendn, recvn *tensor.Int32, cons *tensor.Bool) {
sendn, recvn, cons = NewTensors(send, recv)
sNtot := send.Len()
rNtot := recv.Len()
sNp := send.DimSize(0) * send.DimSize(1)
sNu := send.DimSize(2) * send.DimSize(3)
rnv := recvn.Values
snv := sendn.Values
npl := sNp
if ot.NPools > 0 {
npl = min(ot.NPools, sNp)
}
if rNtot == sNp { // one-to-one
for i := 0; i < npl; i++ {
spi := ot.SendStart + i
ri := ot.RecvStart + i
if ri >= rNtot || spi >= sNp {
break
}
for sui := 0; sui < sNu; sui++ {
si := spi*sNu + sui
off := ri*sNtot + si
cons.Values.Set(true, off)
rnv[ri] = int32(sNu)
snv[si] = int32(1)
}
}
} else { // full
for i := 0; i < npl; i++ {
spi := ot.SendStart + i
if spi >= sNp {
break
}
for ri := 0; ri < rNtot; ri++ {
for sui := 0; sui < sNu; sui++ {
si := spi*sNu + sui
off := ri*sNtot + si
cons.Values.Set(true, off)
rnv[ri] = int32(npl * sNu)
snv[si] = int32(rNtot)
}
}
}
}
return
}
// copy of OneToOne.Connect
func (ot *PoolOneToOne) ConnectOneToOne(send, recv *tensor.Shape, same bool) (sendn, recvn *tensor.Int32, cons *tensor.Bool) {
sendn, recvn, cons = NewTensors(send, recv)
sNtot := send.Len()
rNtot := recv.Len()
rnv := recvn.Values
snv := sendn.Values
npl := rNtot
if ot.NPools > 0 {
npl = min(ot.NPools, rNtot)
}
for i := 0; i < npl; i++ {
ri := ot.RecvStart + i
si := ot.SendStart + i
if ri >= rNtot || si >= sNtot {
break
}
off := ri*sNtot + si
cons.Values.Set(true, off)
rnv[ri] = 1
snv[si] = 1
}
return
}