MCPcopy Create free account
hub / github.com/pytorch/pytorch / _RemoveLegacyPad

Function _RemoveLegacyPad

caffe2/python/caffe_translator.py:110–162  ·  view source on GitHub ↗
(net, net_params, input_dims)

Source from the content-addressed store, hash-verified

108
109
110def _RemoveLegacyPad(net, net_params, input_dims):
111 legacy_pad_ops = []
112 for i in range(len(net.op)):
113 op_def = net.op[i]
114 if re.match(r'^(Conv|ConvTranspose|MaxPool|AveragePool)(\dD)?$',
115 op_def.type):
116 for arg in op_def.arg:
117 if arg.name == 'legacy_pad':
118 legacy_pad_ops.append(i)
119 break
120 if legacy_pad_ops:
121 n, c, h, w = input_dims
122 dummy_input = np.random.randn(n, c, h, w).astype(np.float32)
123 dim_map = _GetLegacyDims(net, net_params, dummy_input, legacy_pad_ops)
124
125 # Running with the legacy pad argument removed
126 # compare the dimensions and adjust pad argument when necessary
127 ws = workspace.C.Workspace()
128
129 external_input = net.op[0].input[0]
130 ws.create_blob(external_input).feed_blob(dummy_input)
131 for param in net_params.protos:
132 ws.create_blob(param.name) \
133 .feed_blob(utils.Caffe2TensorToNumpyArray(param))
134
135 for i in range(len(net.op)):
136 op_def = net.op[i]
137 if i in legacy_pad_ops:
138 arg_map = {}
139 for arg in op_def.arg:
140 arg_map[arg.name] = arg
141 pads = _GetLegacyPadArgs(op_def, arg_map)
142 # remove legacy pad arg
143 for j in range(len(op_def.arg)):
144 arg = op_def.arg[j]
145 if arg.name == 'legacy_pad':
146 del op_def.arg[j]
147 break
148 output = op_def.output[0]
149 # use a new name to avoid the interference with inplace
150 nonlegacy_output = output + '_nonlegacy'
151 op_def.output[0] = nonlegacy_output
152 ws._run_operator(op_def.SerializeToString())
153 blob_nonlegacy = ws.fetch_blob(nonlegacy_output)
154 # reset output name
155 op_def.output[0] = output
156
157 dim1 = dim_map[i]
158 dim2 = blob_nonlegacy.shape
159 _AdjustDims(op_def, arg_map, pads, dim1, dim2)
160
161 ws._run_operator(op_def.SerializeToString())
162 return net
163
164
165def _GetBlobDimMap(net, net_params, dummy_input):

Callers 1

TranslateModelMethod · 0.85

Calls 8

_GetLegacyDimsFunction · 0.85
_GetLegacyPadArgsFunction · 0.85
_AdjustDimsFunction · 0.85
astypeMethod · 0.80
rangeFunction · 0.50
matchMethod · 0.45
appendMethod · 0.45
randnMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…