"tools/vscode:/vscode.git/clone" did not exist on "ba28c08f0c94d39628895d222913d2e09e4c266e"
C.lua 11.8 KB
Newer Older
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
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
-- Copyright 2016-present, Facebook, Inc.
-- All rights reserved.
--
-- This source code is licensed under the license found in the
-- LICENSE file in the root directory of this source tree.

return function (sparseconvnet)
  local ffi = require 'ffi'
  local libpath, ok
  libpath = package.searchpath('libcusparseconvnet', package.cpath)
  if not libpath then
    libpath = package.searchpath('libsparseconvnet', package.cpath)
  end
  assert(libpath)
  local F = ffi.load(libpath)
  --local fc=io.open('header_cpu.h','w')
  --local fg=io.open('header_gpu.h','w')

  local cdef = [[
  void scn_set_THCState(void *state);
  ]]
  ffi.cdef(cdef)
  if cutorch then
    F['scn_set_THCState'](cutorch.getState())
  end

  cdef = [[
28
29
30
31
void scn_ptrCopyA(long *dst, void **src);
void scn_ptrCopyB(void **dst, long *src);
double scn_ruleBookBits();
void scn_2_drawCurve(void **m, THFloatTensor *features, THFloatTensor *stroke);
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
32
33
34
35
36
37
  ]]
  if fc then fc:write(cdef) end
  ffi.cdef(cdef)
  sparseconvnet.ruleBookBits=F['scn_ruleBookBits']()

  cdef = [[
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
double scn_DIMENSION_addSampleFromThresholdedTensor(
  void **m, THFloatTensor *features_, THFloatTensor *tensor_,
  THLongTensor *offset_, THLongTensor *spatialSize_, float threshold);
void scn_DIMENSION_batchAddSample(void **m);
void scn_DIMENSION_createMetadataForDenseToSparse(
  void **m, THLongTensor *spatialSize_, THLongTensor *pad, THLongTensor *nz,
  long batchSize);
void scn_DIMENSION_freeMetadata(void **metadata);
void scn_DIMENSION_generateRuleBooks3s2(void **m);
void scn_DIMENSION_generateRuleBooks2s2(void **m);
void scn_DIMENSION_setInputSpatialSize(void **m, THLongTensor *spatialSize);
void scn_DIMENSION_setInputSpatialLocation(void **m, THFloatTensor *features,
  THLongTensor *location, THFloatTensor *vec, bool overwrite);
void scn_DIMENSION_setInputSpatialLocations(void **m, THFloatTensor *features,
  THLongTensor *locations, THFloatTensor *vecs, bool overwrite);
void scn_DIMENSION_getSpatialLocations(void **m, THLongTensor *spatialSize,
  THLongTensor *locations);
55
56

]]
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
57
58
59
60

  for DIMENSION = 1,10 do
    local def = string.gsub(cdef, 'DIMENSION', DIMENSION)
    ffi.cdef(def)
61
62
63
64
    if fc then
      def=string.gsub(def,'bool','_Bool')
      fc:write(def)
    end
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
65
66
67
68
69
70
  end

  --types CPU float, double;
  --type GPU half, float, double; int_cpu and int_gpu

  cdef = [[
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
void scn_ARCH_REAL_AffineReluTrivialConvolution_updateOutput(
  THTensor *input_features, THTensor *output_features,
  THTensor *affineWeight, THTensor *affineBias, THTensor *convWeight);
void scn_ARCH_REAL_AffineReluTrivialConvolution_backward(
  THTensor *input_features, THTensor *d_input_features,
  THTensor *d_output_features, THTensor *affineWeight,
  THTensor *d_affineWeight, THTensor *affineBias, THTensor *d_affineBias,
  THTensor *convWeight, THTensor *d_convWeight, bool additiveGrad);

// BatchwiseMultiplicativeDropout
void scn_ARCH_REAL_BatchwiseMultiplicativeDropout_updateOutput(
  THTensor *input_features, THTensor *output_features,
  THTensor *noise, long nPlanes, long input_stride, long output_stride,
  float alpha);
void scn_ARCH_REAL_BatchwiseMultiplicativeDropout_updateGradInput(
  THTensor *input_features, THTensor *d_input_features,
  THTensor *d_output_features, THTensor *noise, long nPlanes,
  long input_stride, long output_stride, float alpha);

// BatchNormalization
void scn_ARCH_REAL_BatchNormalization_updateOutput(
  THTensor *input_features, THTensor *output_features,
  THTensor *saveMean, THTensor *saveInvStd, THTensor *runningMean,
  THTensor *runningVar, THTensor *weight, THTensor *bias, REAL eps,
  REAL momentum, bool train, REAL leakiness);
void scn_ARCH_REAL_BatchNormalization_backward(
  THTensor *input_features, THTensor *d_input_features,
  THTensor *output_features, THTensor *d_output_features, THTensor *saveMean,
  THTensor *saveInvStd, THTensor *runningMean, THTensor *runningVar,
  THTensor *weight, THTensor *bias, THTensor *d_weight, THTensor *d_bias,
  REAL leakiness);
// BatchNormalizationInTensor
void scn_ARCH_REAL_BatchNormalizationInTensor_updateOutput(
  THTensor *input_features, THTensor *output_features,
  THTensor *saveMean, THTensor *saveInvStd, THTensor *runningMean,
  THTensor *runningVar, THTensor *weight, THTensor *bias, REAL eps,
  REAL momentum, bool train, REAL leakiness);

// LeakyReLU
void scn_ARCH_REAL_LeakyReLU_updateOutput(
111
  THTensor *input_features, THTensor *output_features,
112
113
114
  float alpha);
void scn_ARCH_REAL_LeakyReLU_updateGradInput(
  THTensor *input_features, THTensor *d_input_features,
115
  THTensor *d_output_features, float alpha);
116
117
118
119
120
121
122
123
124
125
126

// NetworkInNetwork
double scn_ARCH_REAL_NetworkInNetwork_updateOutput(
  THTensor *input_features, THTensor *output_features,
  THTensor *weight, THTensor *bias);
void scn_ARCH_REAL_NetworkInNetwork_updateGradInput(
  THTensor *d_input_features, THTensor *d_output_features,
  THTensor *weight);
void scn_ARCH_REAL_NetworkInNetwork_accGradParameters(
  THTensor *input_features, THTensor *d_output_features,
  THTensor *d_weight, THTensor *d_bias);
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
127
128
129
130
131
132
133
134
135
  ]]

  for _,v in ipairs({{'float', 'THFloatTensor'}, {'double','THDoubleTensor'}}) do
    local def = cdef
    def = string.gsub(def, 'ARCH', 'cpu')
    def = string.gsub(def, 'THITensor', 'void')
    def = string.gsub(def, 'REAL', v[1])
    def = string.gsub(def, 'THTensor', v[2])
    ffi.cdef(def)
136
137
138
139
    if fc then
      def=string.gsub(def,'bool','_Bool')
      fc:write(def)
    end
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
140
141
142
143
  end
  if sparseconvnet.cutorch then
    for k,v in ipairs({
        {'float', 'THCudaTensor'},
144
145
146
        --{'double', 'THCudaDoubleTensor'}
                     })
    do
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
147
148
149
      local def = cdef
      def = string.gsub(def, 'ARCH', 'gpu')
      def = string.gsub(def, 'THITensor', sparseconvnet.ruleBookBits==64 and
150
                          'THCudaLongTensor' or 'THCudaIntTensor')
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
151
152
153
      def = string.gsub(def, 'REAL', v[1])
      def = string.gsub(def, 'THTensor', v[2])
      ffi.cdef(def)
154
155
156
157
      if fg then
        def=string.gsub(def,'bool','_Bool')
        fg:write(def)
      end
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
158
159
160
161
    end
  end

  cdef = [[
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// ActivePooling
void scn_ARCH_REAL_DIMENSIONActivePooling_updateOutput(
  THLongTensor *inputSize, void **m, THTensor *input_features,
  THTensor *output_features, THITensor *rulesBuffer, bool average);
void scn_ARCH_REAL_DIMENSIONActivePooling_updateGradInput(
  THLongTensor *inputSize, void **m,
  THTensor *d_input_features, THTensor *d_output_features,
  THITensor *rulesBuffer, bool average);

// Average Pooling
void scn_ARCH_REAL_DIMENSIONAveragePooling_updateOutput(
  THLongTensor *inputSize, THLongTensor *outputSize,
  THLongTensor *poolSize, THLongTensor *poolStride, void **m,
  THTensor *input_features, THTensor *output_features, long nFeaturesToDrop,
  THITensor *rulesBuffer);
void scn_ARCH_REAL_DIMENSIONAveragePooling_updateGradInput(
  THLongTensor * inputSize, THLongTensor * outputSize,
  THLongTensor * poolSize, THLongTensor * poolStride, void **m,
  THTensor *input_features, THTensor *d_input_features,
  THTensor *d_output_features, long nFeaturesToDrop,
  THITensor *rulesBuffer);

double scn_ARCH_REAL_DIMENSIONConvolution_updateOutput(
  THLongTensor *inputSize, THLongTensor *outputSize,
  THLongTensor *filterSize, THLongTensor *filterStride, void **m,
  THTensor *input_features, THTensor *output_features, THTensor *weight,
  THTensor *bias, long filterVolume, THITensor *rulesBuffer);
void scn_ARCH_REAL_DIMENSIONConvolution_backward(
  THLongTensor *inputSize, THLongTensor *outputSize,
  THLongTensor *filterSize, THLongTensor *filterStride, void **m,
  THTensor *input_features, THTensor *d_input_features,
  THTensor *d_output_features, THTensor *weight, THTensor *d_weight,
  THTensor *d_bias, long filterVolume, THITensor *rulesBuffer);

double scn_ARCH_REAL_DIMENSIONDeconvolution_updateOutput(
  THLongTensor *inputSize, THLongTensor *outputSize,
  THLongTensor *filterSize, THLongTensor *filterStride, void **m,
  THTensor *input_features, THTensor *output_features, THTensor *weight,
  THTensor *bias, long filterVolume, THITensor *rulesBuffer);
void scn_ARCH_REAL_DIMENSIONDeconvolution_backward(
  THLongTensor *inputSize, THLongTensor *outputSize,
  THLongTensor *filterSize, THLongTensor *filterStride, void **m,
  THTensor *input_features, THTensor *d_input_features,
  THTensor *d_output_features, THTensor *weight, THTensor *d_weight,
  THTensor *d_bias, long filterVolume, THITensor *rulesBuffer);

// Max Pooling
void scn_ARCH_REAL_DIMENSIONMaxPooling_updateOutput(
  THLongTensor *inputSize, THLongTensor *outputSize,
  THLongTensor *poolSize, THLongTensor *poolStride, void **m,
  THTensor *input_features, THTensor *output_features, long nFeaturesToDrop,
  THITensor *rulesBuffer);
void scn_ARCH_REAL_DIMENSIONMaxPooling_updateGradInput(
  THLongTensor * inputSize, THLongTensor * outputSize,
  THLongTensor * poolSize, THLongTensor * poolStride, void **m,
  THTensor *input_features, THTensor *d_input_features,
  THTensor *output_features, THTensor *d_output_features,
  long nFeaturesToDrop, THITensor *rulesBuffer);

// SparseToDense
void scn_ARCH_REAL_DIMENSIONSparseToDense_updateOutput(
  THLongTensor *inputSize, void **m, THTensor *input_features,
224
  THTensor *output_features, THITensor *rulesBuffer, long nPlanes);
225
226
227
228
229
void scn_ARCH_REAL_DIMENSIONSparseToDense_updateGradInput(
  THLongTensor *inputSize, void **m, THTensor *input_features,
  THTensor *d_input_features, THTensor *d_output_features,
  THITensor *rulesBuffer);

230
double scn_ARCH_REAL_DIMENSIONSubmanifoldConvolution_updateOutput(
231
232
233
  THLongTensor *inputSize, THLongTensor *filterSize, void **m,
  THTensor *input_features, THTensor *output_features, THTensor *weight,
  THTensor *bias, long filterVolume, THITensor *rulesBuffer);
234
void scn_ARCH_REAL_DIMENSIONSubmanifoldConvolution_backward(
235
236
237
238
  THLongTensor *inputSize, THLongTensor *filterSize, void **m,
  THTensor *input_features, THTensor *d_input_features,
  THTensor *d_output_features, THTensor *weight, THTensor *d_weight,
  THTensor *d_bias, long filterVolume, THITensor *rulesBuffer);
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
239
240
  ]]

241
  for _,v in ipairs({{'float', 'THFloatTensor'}, {'double', 'THDoubleTensor'}}) do
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
242
243
244
245
246
247
248
249
    for DIMENSION = 1,10 do
      local def = cdef
      def = string.gsub(def, 'ARCH', 'cpu')
      def = string.gsub(def, '_DIMENSION', DIMENSION)
      def = string.gsub(def, 'THITensor', 'void')
      def = string.gsub(def, 'REAL', v[1])
      def = string.gsub(def, 'THTensor', v[2])
      ffi.cdef(def)
250
251
252
253
      if fc then
        def=string.gsub(def,'bool','_Bool')
        fc:write(def)
      end
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
254
255
256
257
258
    end
  end
  if sparseconvnet.cutorch then
    for k,v in ipairs({
        {'float', 'THCudaTensor'},
259
260
        --{'double', 'THCudaDoubleTensor'}
    }) do
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
261
262
263
264
265
      for DIMENSION = 1,10 do
        local def = cdef
        def = string.gsub(def, 'ARCH', 'gpu')
        def = string.gsub(def, '_DIMENSION', DIMENSION)
        def = string.gsub(def, 'THITensor', sparseconvnet.ruleBookBits==64 and
266
                            'THCudaLongTensor' or 'THCudaIntTensor')
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
267
268
269
        def = string.gsub(def, 'REAL', v[1])
        def = string.gsub(def, 'THTensor', v[2])
        ffi.cdef(def)
270
271
272
273
        if fg then
          def=string.gsub(def,'bool','_Bool')
          fg:write(def)
        end
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
      end
    end
  end
  if fc then
    fc:close()
    fg:close()
  end
  sparseconvnet.C = {}
  local C = sparseconvnet.C

  local typeTable={}
  typeTable['torch.FloatTensor'] = 'cpu_float'
  typeTable['torch.DoubleTensor'] = 'cpu_double'
  typeTable['torch.CudaHalfTensor'] = 'gpu_half' --todo
  typeTable['torch.CudaTensor'] = 'gpu_float'
  typeTable['torch.CudaDoubleTensor'] = 'gpu_double'

  function C.fn(name)
    return F['scn_' .. name]
  end
  function C.typedFn(type,name)
    return F['scn_' .. typeTable[type] .. '_' .. name]
  end
  function C.dimensionFn(dimension,name)
    return F['scn_' .. dimension .. '_' .. name]
  end
  function C.dimTypedFn(dimension,type,name)
    return F['scn_' .. typeTable[type] .. dimension .. name]
  end

  function C.copyFfiPtrToLong(dst,src)
    F['scn_ptrCopyA'](dst:data(), src)
  end
  function C.copyLongToFfiPtr(dst,src)
    F['scn_ptrCopyB'](dst, src:data())
  end
end