Caffe2 - Python API
A deep learning, cross platform ML framework
fc.py
1 ## @package fc
2 # Module caffe2.python.helpers.fc
3 from __future__ import absolute_import
4 from __future__ import division
5 from __future__ import print_function
6 from __future__ import unicode_literals
7 
8 from caffe2.python import core
9 from caffe2.python.modeling import initializers
10 from caffe2.python.modeling.parameter_info import ParameterTags
11 
12 
13 def _FC_or_packed_FC(
14  model, op_call, blob_in, blob_out, dim_in, dim_out, weight_init=None,
15  bias_init=None, WeightInitializer=None, BiasInitializer=None,
16  enable_tensor_core=False, float16_compute=False, **kwargs
17 ):
18  WeightInitializer = initializers.update_initializer(
19  WeightInitializer, weight_init, ("XavierFill", {})
20  )
21  BiasInitializer = initializers.update_initializer(
22  BiasInitializer, bias_init, ("ConstantFill", {})
23  )
24  if not model.init_params:
25  WeightInitializer = initializers.ExternalInitializer()
26  BiasInitializer = initializers.ExternalInitializer()
27 
28  blob_out = blob_out or model.net.NextName()
29  bias_tags = [ParameterTags.BIAS]
30  if 'freeze_bias' in kwargs:
31  bias_tags.append(ParameterTags.COMPUTED_PARAM)
32 
33  weight = model.create_param(
34  param_name=blob_out + '_w',
35  shape=[dim_out, dim_in],
36  initializer=WeightInitializer,
37  tags=ParameterTags.WEIGHT
38  )
39  bias = model.create_param(
40  param_name=blob_out + '_b',
41  shape=[dim_out, ],
42  initializer=BiasInitializer,
43  tags=bias_tags
44  )
45 
46  # enable TensorCore by setting appropriate engine
47  if enable_tensor_core:
48  kwargs['engine'] = 'TENSORCORE'
49 
50  # Enable float 16 compute kernel (relevant for CUDA)
51  if float16_compute:
52  kwargs['float16_compute'] = True
53 
54  return op_call([blob_in, weight, bias], blob_out, **kwargs)
55 
56 
57 def fc(model, *args, **kwargs):
58  return _FC_or_packed_FC(model, model.net.FC, *args, **kwargs)
59 
60 
61 def packed_fc(model, *args, **kwargs):
62  return _FC_or_packed_FC(model, model.net.PackedFC, *args, **kwargs)
63 
64 
65 def fc_decomp(
66  model, blob_in, blob_out, dim_in, dim_out,
67  rank_approx=5, weight_init=None, bias_init=None,
68  WeightInitializer=None, BiasInitializer=None, **kwargs
69 ):
70  """FC_Decomp version
71  Here we assume that the rank of original input is bigger than 5.
72  """
73  WeightInitializer = initializers.update_initializer(
74  WeightInitializer, weight_init, ("XavierFill", {})
75  )
76  BiasInitializer = initializers.update_initializer(
77  BiasInitializer, bias_init, ("ConstantFill", {})
78  )
79  blob_out = blob_out or model.net.NextName()
80  u = model.create_param(
81  param_name=blob_out + '_u',
82  shape=[dim_out, rank_approx],
83  initializer=WeightInitializer,
84  )
85  v = model.create_param(
86  param_name=blob_out + '_v',
87  shape=[dim_in, rank_approx],
88  initializer=WeightInitializer,
89  )
90  bias = model.create_param(
91  param_name=blob_out + '_b',
92  shape=[dim_out, ],
93  initializer=BiasInitializer,
94  )
95  return model.net.FC_Decomp([blob_in, u, v, bias], blob_out, **kwargs)
96 
97 
98 def fc_prune(
99  model, blob_in, blob_out, dim_in, dim_out,
100  weight_init=None, bias_init=None, mask_init=None,
101  threshold=0.00001, need_compress_rate=False,
102  comp_lb=0.05,
103  **kwargs
104 ):
105  """FC_Prune version
106  Runnable so far. Great!:)
107  """
108  weight_init = weight_init if weight_init else ('XavierFill', {})
109  bias_init = bias_init if bias_init else ('ConstantFill', {})
110  mask_init = mask_init if mask_init else ('ConstantFill', {})
111  blob_out = blob_out or model.net.NextName()
112  compress_rate = blob_out + '_compress_rate'
113  if model.init_params:
114  compress_lb = model.param_init_net.ConstantFill(
115  [],
116  blob_out + '_lb',
117  shape=[1],
118  value=comp_lb
119  )
120  weight = model.param_init_net.__getattr__(weight_init[0])(
121  [],
122  blob_out + '_w',
123  shape=[dim_out, dim_in],
124  **weight_init[1]
125  )
126  mask = model.param_init_net.ConstantFill(
127  [],
128  blob_out + '_m',
129  shape=[dim_out, dim_in],
130  value=1.0
131  )
132  ag_dw = model.param_init_net.__getattr__(mask_init[0])(
133  [],
134  blob_out + '_ag_dw',
135  shape=[dim_out, dim_in],
136  **mask_init[1]
137  )
138  bias = model.param_init_net.__getattr__(bias_init[0])(
139  [],
140  blob_out + '_b',
141  shape=[dim_out, ],
142  **bias_init[1]
143  )
144  mask_seq = model.param_init_net.__getattr__(mask_init[0])(
145  [],
146  blob_out + '_mask_seq',
147  shape=[dim_out, dim_in],
148  **mask_init[1]
149  )
150  thres = model.param_init_net.ConstantFill(
151  [],
152  blob_out + '_thres',
153  shape=[1],
154  value=threshold
155  )
156  else:
157  compress_lb = core.ScopedBlobReference(
158  blob_out + '_lb', model.param_init_net)
159  weight = core.ScopedBlobReference(
160  blob_out + '_w', model.param_init_net)
161  bias = core.ScopedBlobReference(
162  blob_out + '_b', model.param_init_net)
163  mask = core.ScopedBlobReference(
164  blob_out + '_m', model.param_init_net)
165  ag_dw = core.ScopedBlobReference(
166  blob_out + '_ag_dw', model.param_init_net)
167  mask_seq = core.ScopedBlobReference(
168  blob_out + '_mask_seq', model.param_init_net)
169  thres = core.ScopedBlobReference(
170  blob_out + '_thres', model.param_init_net)
171 
172  model.AddParameter(weight)
173  model.AddParameter(bias)
174  if need_compress_rate:
175  return model.net.FC_Prune([blob_in, weight, mask, bias, ag_dw, mask_seq,
176  thres, compress_lb],
177  [blob_out, compress_rate], **kwargs)
178  else:
179  return model.net.FC_Prune([blob_in, weight, mask,
180  bias, ag_dw, mask_seq,
181  thres, compress_lb],
182  blob_out, **kwargs)
183 
184 
185 def fc_sparse(
186  model, blob_in, blob_out, w_csr, iw, jw, bias,
187  **kwargs
188 ):
189  """FC_Sparse: Only takes in alocated weights"""
190  if not (w_csr and iw and jw and bias):
191  print("Warning...")
192  model.AddParameter(w_csr)
193  model.AddParameter(iw)
194  model.AddParameter(jw)
195  model.AddParameter(bias)
196  return model.net.FC_Sparse([blob_in, w_csr, iw, jw, bias],
197  blob_out, **kwargs)
Module caffe2.python.helpers.fc.