- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit.lua
More file actions
Latest commit
186 lines (174 loc) · 6.17 KB
/
Copy pathinit.lua
File metadata and controls
186 lines (174 loc) · 6.17 KB
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
require'nn'
require'math'
require'libspaghetti'
localSpaghetti, parent=torch.class('nn.Spaghetti', 'nn.Module')
-- Note: NEVER change conSrc or conDst once the module is created,
-- due to the optimizations
functionSpaghetti:__init(conSrc, conDst, dimDst, do_not_reset)
parent.__init(self)
assert(dimDst:size(1) ==conDst:size(2))
self.nCon=conSrc:size(1)
self.conSrc=conSrc:long()
self.conDst=conDst:long()
self.weight=torch.Tensor(self.nCon)
self.gradWeight=torch.Tensor():resizeAs(self.weight)
self.output=torch.Tensor(dimDst)
self.gradInput=torch.Tensor()
iflibspaghetti.spaghetti_blas() ==0then
self.conSrcC=torch.LongTensor(self.nCon) --these 4 arrays are 0 based
self.conDstC=torch.LongTensor(self.nCon)
self.conGOC=torch.LongTensor(self.nCon)
self.conGIC=torch.LongTensor(self.nCon)
self.currentSrcStride=torch.LongStorage{}
self.currentDstStride=torch.LongStorage{}
self.currentGOStride=torch.LongStorage{}
self.currentGIStride=torch.LongStorage{}
localdummyStrideSrc=torch.Tensor(self.conSrc:max(1):storage()):stride()
localdummyStrideDst=torch.Tensor(self.conDst:max(1):storage()):stride()
self:recomputeContiguous(self.conSrc, self.conSrcC,
dummyStrideSrc, self.currentSrcStride)
self:recomputeContiguous(self.conDst, self.conDstC,
dummyStrideDst, self.currentDstStride)
localsortedSrc, orderSrc=self.conSrcC:sort()
localn=sortedSrc:size(1)
self.nChunks=4
self.nChunks2=4
localcuts= {0}
fori=1,(self.nChunks-1) do
localcut0=math.min(math.floor(n/self.nChunks)*i,n-1)
localcut1=cut0+1
while (cut1<n) and (sortedSrc[cut1] ==sortedSrc[cut0]) do
cut1=cut1+1
end
cut1=cut1-1
if (cut1~=cuts[#cuts]) and (cut1<n-1) then
table.insert(cuts, cut1)
end
end
table.insert(cuts, n)
self.chunks=cuts
self.chunksB= {}
self.order=torch.LongTensor(n)
foriChunk=1,(#cuts-1) do
localnewchunk= {}
locali0=cuts[iChunk]
locali1=cuts[iChunk+1]
localn2=i1-i0
localtosort=torch.Tensor(n2)
fori=0,(n2-1) do
tosort[i+1] =self.conDstC[orderSrc[i0+i+1]]
end
localsortedDst, orderDst=tosort:sort()
fori=0,(n2-1) do
self.order[i0+i+1] =orderSrc[i0+orderDst[i+1]]
end
table.insert(newchunk, i0)
fori=1,(self.nChunks2-1) do
localcut0=math.min(math.floor(n2/self.nChunks2)*i,n2-1)
ifcut0>0then
localcut1=cut0+1
while (cut1<n2) and (sortedDst[cut1] ==sortedDst[cut0]) do
cut1=cut1+1
end
cut1=cut1-1
if (cut1~=cuts[#cuts]) and (cut1<n2-1) then
table.insert(newchunk, i0+cut1)
end
end
end
while#newchunk<=self.nChunks2do
table.insert(newchunk, i1)
end
table.insert(self.chunksB, newchunk)
end
table.insert(self.chunks, n)
localconSrcTmp=torch.LongTensor(self.conSrc:size(1), self.conSrc:size(2))
localconDstTmp=torch.LongTensor(self.conDst:size(1), self.conDst:size(2))
fori=1,ndo
conSrcTmp[i] =self.conSrc[self.order[i] ]
conDstTmp[i] =self.conDst[self.order[i] ]
--conSrcTmp[self.order[i] ] = self.conSrc[i]
--conDstTmp[self.order[i] ] = self.conDst[i]
end
self.conSrc=conSrcTmp
self.conDst=conDstTmp
self.currentSrcStride=torch.LongStorage{}
self.currentDstStride=torch.LongStorage{}
self.chunks=torch.LongTensor(self.chunks)
self.chunks:resize(1,self.chunks:size(1))
self.chunksB=torch.LongTensor(self.chunksB)
else
self.conSrc:add(-1) -- Careful : 0-based, only if blas
self.conDst:add(-1)
end
ifnotdo_not_resetthen
self:reset()
end
end
functionSpaghetti:copyWeights(w)
iflibspaghetti.spaghetti_blas() ==0then
fori=1,w:size(1) do
self.weight[i] =w[self.order[i] ]
--self.weight[self.order[i] ] = w[i]
end
else
self.weight:copy(w)
end
end
functionSpaghetti:recomputeContiguous(nonContig, contig, stride, curStride)
localfunctionstrideEq(a,b)
ifa:size(1) ~=b:size(1) then
returnfalse
end
returntorch.Tensor(a:totable()):eq(torch.Tensor(b:totable())):sum() ==a:size(1)
end
ifnotstrideEq(stride, curStride) then
contig:zero()
fori=1,nonContig:size(2) do
contig:add(stride[i], nonContig[{{},i}]-1)
end
curStride:resize(stride:size()):copy(stride)
end
end
functionSpaghetti:reset(stdv)
stdv=stdvor1
self.weight:apply(function() returntorch.uniform(-stdv, stdv) end)
end
functionSpaghetti:updateOutput(input)
assert(input:nDimension() ==self.conSrc:size(2))
iflibspaghetti.spaghetti_blas() ==0then
self:recomputeContiguous(self.conSrc, self.conSrcC,
input:stride(), self.currentSrcStride)
self:recomputeContiguous(self.conDst, self.conDstC,
self.output:stride(), self.currentDstStride)
end
libspaghetti.spaghetti_updateOutput(input, self.conSrc, self.conDst,
self.weight, self.output, self.chunks)
returnself.output
end
functionSpaghetti:updateGradInput(input, gradOutput)
self.gradInput:resizeAs(input)
iflibspaghetti.spaghetti_blas() ==0then
self:recomputeContiguous(self.conDst, self.conGOC,
gradOutput:stride(), self.currentGOStride)
self:recomputeContiguous(self.conSrc, self.conGIC,
self.gradInput:stride(), self.currentGIStride)
end
libspaghetti.spaghetti_updateOutput(gradOutput, self.conGOC, self.conGIC,
self.weight, self.gradInput, self.chunksB)
returnself.gradInput
end
functionSpaghetti:accGradParameters(input, gradOutput, scale)
scale=scaleor1
iflibspaghetti.spaghetti_blas() ==0then
self:recomputeContiguous(self.conSrc, self.conSrcC,
input:stride(), self.currentSrcStride)
self:recomputeContiguous(self.conDst, self.conDstC,
self.output:stride(), self.currentDstStride)
end
libspaghetti.spaghetti_accGradParameters(input, self.conSrcC, self.conDstC,
gradOutput, scale, self.gradWeight)
end
functionSpaghetti:decayParameters(decay)
self.weight:add(-decay, self.weight)
end