Checks the operator in a very simple fashion by stacking a sum of squares on the top. Inputs: op: the operator to be checked. inputs: the input data in numpy arrays. input_to_check: an index specifying which input blob we should check.
(
self,
op,
inputs,
input_to_check,
outputs_with_grads,
grad_ops=None,
input_device_options=None,
ensure_outputs_are_inferred=False,
)
| 228 | return loss, grad |
| 229 | |
| 230 | def CheckSimple( |
| 231 | self, |
| 232 | op, |
| 233 | inputs, |
| 234 | input_to_check, |
| 235 | outputs_with_grads, |
| 236 | grad_ops=None, |
| 237 | input_device_options=None, |
| 238 | ensure_outputs_are_inferred=False, |
| 239 | ): |
| 240 | """Checks the operator in a very simple fashion by stacking a sum of |
| 241 | squares on the top. |
| 242 | |
| 243 | Inputs: |
| 244 | op: the operator to be checked. |
| 245 | inputs: the input data in numpy arrays. |
| 246 | input_to_check: an index specifying which input blob we should |
| 247 | check. |
| 248 | outputs_with_grads: indices specifying which output blobs will we |
| 249 | need to check gradients with. For these outputs, we will collect a |
| 250 | squared sum and also feed in their gradients. |
| 251 | grad_operator: the gradient operator. If not given, we will get the |
| 252 | gradient operator from the gradient registry. |
| 253 | input_device_options: an optional mapping from input names to |
| 254 | DeviceOptions (to override the default DeviceOption) |
| 255 | ensure_outputs_are_inferred: if set will assert that the gradient output |
| 256 | shapes matches the inferred shapes |
| 257 | Outputs: |
| 258 | boolean: True if it passes, False if it does not pass. |
| 259 | """ |
| 260 | # Entering the checker workspace |
| 261 | old_ws_name = workspace.CurrentWorkspace() |
| 262 | if self._workspace_name != old_ws_name: |
| 263 | workspace.SwitchWorkspace(self._workspace_name, True) |
| 264 | |
| 265 | op.device_option.CopyFrom(self._device_option) |
| 266 | if grad_ops is None: |
| 267 | # TODO(jiayq): use the gradient registration instead of the old |
| 268 | # hack. |
| 269 | grad_ops, g_input = getGradientForOp(op) |
| 270 | |
| 271 | |
| 272 | _input_device_options = input_device_options or \ |
| 273 | core.InferOpBlobDevicesAsDict(op)[0] |
| 274 | # First, feed in the input. |
| 275 | for i, arr in enumerate(inputs): |
| 276 | workspace.FeedBlob( |
| 277 | op.input[i], arr, |
| 278 | _input_device_options.get( |
| 279 | op.input[i], self._device_option)) |
| 280 | |
| 281 | # Get the loss and gradient for the original. |
| 282 | grad_name = g_input[input_to_check] |
| 283 | loss, grad = self.GetLossAndGrad( |
| 284 | op, grad_ops, inputs, op.input, input_to_check, grad_name, |
| 285 | outputs_with_grads, |
| 286 | ) |
| 287 | grad_estimate = np.zeros_like(inputs[input_to_check]) |