/usr/local/lib64/python3.6/site-packages/caffe2/python
NameSizeModeActions
docs/-0755rm
examples/-0755rm
fakelowp/-0755rm
helpers/-0755rm
ideep/-0755rm
layers/-0755rm
mint/-0755rm
mkl/-0755rm
modeling/-0755rm
models/-0755rm
onnx/-0755rm
operator_test/-0755rm
predictor/-0755rm
rnn/-0755rm
serialized_test/-0755rm
test/-0755rm
trt/-0755rm
__pycache__/-0755rm
allcompare_test.py22550644editdlrm
attention.py123590644editdlrm
benchmark_generator.py49120644editdlrm
binarysize.py55210644editdlrm
brew.py47620644editdlrm
brew_test.py117390644editdlrm
build.py1530644editdlrm
cached_reader.py43940644editdlrm
caffe2_pybind11_state.cpython-36m-x86_64-linux-gnu.so482997120755editdlrm
caffe2_pybind11_state_gpu.cpython-36m-x86_64-linux-gnu.so490481440755editdlrm
caffe_translator.py352270644editdlrm
caffe_translator_test.py35530644editdlrm
checkpoint.py321010644editdlrm
checkpoint_test.py134050644editdlrm
cnn.py76260644editdlrm
context.py28410644editdlrm
context_test.py17920644editdlrm
control.py193090644editdlrm
control_ops_grad.py288930644editdlrm
control_ops_grad_test.py17520644editdlrm
control_ops_util.py108630644editdlrm
control_test.py122760644editdlrm
convert.py550644editdlrm
convert_test.py2010644editdlrm
convnet_benchmarks.py205330644editdlrm
convnet_benchmarks_test.py8390644editdlrm
core.py1194000644editdlrm
core_gradients_test.py380220644editdlrm
core_test.py476830644editdlrm
crf.py132500644editdlrm
crf_predict.py11590644editdlrm
crf_viterbi_test.py16630644editdlrm
dataio.py235320644editdlrm
dataio_test.py175750644editdlrm
dataset.py128860644editdlrm
data_parallel_model.py831000644editdlrm
data_parallel_model_test.py561450644editdlrm
data_workers.py159410644editdlrm
data_workers_test.py65610644editdlrm
db_file_reader.py66080644editdlrm
db_test.py11100644editdlrm
device_checker.py51570644editdlrm
dyndep.py15330644editdlrm
embedding_generation_benchmark.py52560644editdlrm
experiment_util.py36250644editdlrm
extension_loader.py7440644editdlrm
fakefp16_transform_lib.py3220644editdlrm
filler_test.py7480644editdlrm
functional.py44150644editdlrm
functional_test.py42040644editdlrm
fused_8bit_rowwise_conversion_ops_test.py39450644editdlrm
gradient_checker.py153770644editdlrm
gradient_check_test.py207290644editdlrm
gru_cell.py51290644editdlrm
hip_test_util.py4050644editdlrm
hsm_util.py22590644editdlrm
hypothesis_test.py1057620644editdlrm
hypothesis_test_util.py268530644editdlrm
ideep_test_util.py9980644editdlrm
layers_test.py929310644editdlrm
layer_model_helper.py293400644editdlrm
layer_model_instantiator.py39350644editdlrm
layer_parameter_sharing_test.py91480644editdlrm
layer_test_util.py48750644editdlrm
lazy.py2770644editdlrm
lazy_dyndep.py25620644editdlrm
lazy_dyndep_test.py39140644editdlrm
lengths_reducer_fused_8bit_rowwise_ops_test.py75750644editdlrm
lengths_reducer_rowwise_8bit_ops_test.py57100644editdlrm
lstm_benchmark.py106490644editdlrm
memonger.py340410644editdlrm
memonger_test.py369100644editdlrm
mkl_test_util.py11420644editdlrm
model_device_test.py47770644editdlrm
model_helper.py234920644editdlrm
model_helper_test.py23360644editdlrm
modifier_context.py17720644editdlrm
muji.py81310644editdlrm
muji_test.py30580644editdlrm
net_builder.py276790644editdlrm
net_builder_test.py113820644editdlrm
net_drawer.py142640644editdlrm
net_printer.py127040644editdlrm
net_printer_test.py31900644editdlrm
nomnigraph.py42160644editdlrm
nomnigraph_test.py154270644editdlrm
nomnigraph_transformations.py37870644editdlrm
nomnigraph_transformations_test.py57670644editdlrm
normalizer.py14110644editdlrm
normalizer_context.py10070644editdlrm
normalizer_test.py4870644editdlrm
numa_benchmark.py22300644editdlrm
numa_test.py16630644editdlrm
observer_test.py53160644editdlrm
operator_fp_exceptions_test.py12480644editdlrm
optimizer.py788130644editdlrm
optimizer_context.py14620644editdlrm
optimizer_test.py307050644editdlrm
optimizer_test_util.py91870644editdlrm
parallelize_bmuf_distributed_test.py99080644editdlrm
parallel_workers.py76820644editdlrm
parallel_workers_test.py35010644editdlrm
pipeline.py172830644editdlrm
pipeline_test.py25420644editdlrm
predictor_constants.py1980644editdlrm
python_op_test.py91690644editdlrm
queue_util.py44590644editdlrm
record_queue.py44530644editdlrm
recurrent.py132970644editdlrm
regularizer.py211200644editdlrm
regularizer_context.py10130644editdlrm
regularizer_test.py102660644editdlrm
rnn_cell.py682330644editdlrm
schema.py456210644editdlrm
schema_test.py157540644editdlrm
scope.py36230644editdlrm
scope_test.py52490644editdlrm
session.py76420644editdlrm
session_test.py20780644editdlrm
sparse_to_dense_mask_test.py65650644editdlrm
sparse_to_dense_test.py35560644editdlrm
task.py242740644editdlrm
task_test.py8700644editdlrm
test_util.py35240644editdlrm
text_file_reader.py19900644editdlrm
timeout_guard.py40540644editdlrm
toy_regression_test.py28220644editdlrm
transformations.py18320644editdlrm
transformations_test.py119600644editdlrm
tt_core.py93490644editdlrm
tt_core_test.py25180644editdlrm
utils.py141810644editdlrm
utils_test.py13990644editdlrm
visualize.py63150644editdlrm
workspace.py252630644editdlrm
workspace_test.py348440644editdlrm
_import_c_extension.py22500644editdlrm
__init__.py39250644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/caffe2/python/gradient_checker.py (15377B)
## @package gradient_checker # Module caffe2.python.gradient_checker import os import numpy as np from caffe2.python import core, workspace, net_drawer from caffe2.proto import caffe2_pb2 def getGradientForOp(op): return core.GradientRegistry.GetGradientForOp( op, [s + '_grad' for s in op.output]) def _get_grad_blob(grad_map, input_to_check): grad_blob = grad_map[input_to_check] if isinstance(grad_blob, core.BlobReference): return workspace.blobs[grad_blob] # If grad_blob is not a single blob, it should be a gradient slice. # To make it comparable with the estimiated gradient which is dense, # we need to first convert grad_blob to dense gradient. assert isinstance(grad_blob, core.GradientSlice) dense_grad = 'tmp_dense_grad' sparse_to_dense_op = core.CreateOperator( 'SparseToDense', [grad_blob.indices, grad_blob.values, input_to_check], dense_grad, ) workspace.RunOperatorOnce(sparse_to_dense_op) return workspace.blobs[dense_grad] def _get_grad(net, outputs, outputs_with_grad, input_values, inputs_with_grads): grad_net = net.Clone(net.Name() + "_copy") grad_map = grad_net.AddGradientOperators(outputs_with_grad) for name, value in (input_values or {}).items(): workspace.blobs[name] = value for input_to_check in inputs_with_grads: assert input_to_check in grad_map, ( '{} has no gradient, cannot check net gradient.'.format( input_to_check)) assert str(input_to_check) in workspace.blobs workspace.RunNetOnce(grad_net) forward_results = [(output, workspace.blobs[output]) for output in outputs] grads = {input_to_check: _get_grad_blob(grad_map, input_to_check) for input_to_check in inputs_with_grads} return forward_results, grads, grad_net def _assert_close(value1, value2, threshold, err_msg=''): np.testing.assert_allclose( value1, value2, atol=threshold, rtol=threshold, err_msg=err_msg, ) delta = np.abs(value1 - value2).flatten() return np.mean(delta), max(delta) class NetGradientChecker(object): @staticmethod def CompareNets(nets, outputs, outputs_with_grad_ids, inputs_with_grads, input_values=None, threshold=0.0000001, print_net_images=False): def _get_output_with_grad_names(net_outputs): return [net_outputs[i] for i in outputs_with_grad_ids] if print_net_images: for i, net in enumerate(nets): png = net_drawer.GetPydotGraph(net).create_png() with open("caffe2_net_forward_" + str(i) + net.Name() + ".png", 'wb') \ as f: f.write(png) results = [ _get_grad(net, net_outputs, _get_output_with_grad_names(net_outputs), input_values, inputs_with_grads) for net, net_outputs in zip(nets, outputs) ] if print_net_images: _, _, backward_nets = zip(*results) for i, net in enumerate(backward_nets): png = net_drawer.GetPydotGraph(net).create_png() with open("caffe2_net_" + str(i) + net.Name() + ".png", 'wb') \ as f: f.write(png) first_net_results, first_net_grads, _ = results[0] for net_results, net_grads, _ in results[1:]: assert len(net_results) == len(first_net_results) for idx, ((blob1, blob_value1), (blob2, blob_value2)) in enumerate( zip(first_net_results, net_results)): _assert_close( blob_value1, blob_value2, threshold, err_msg="Different forward pass results for output id {}. " "Corresponding output blobs: {} and {}".format( idx, blob1, blob2)) assert net_grads.keys() == first_net_grads.keys() for blob, blob_grad_value in net_grads.items(): _assert_close( first_net_grads[blob], blob_grad_value, threshold, err_msg="Different gradients for input {}".format(blob)) @staticmethod def Check(net, outputs_with_grad, input_values, input_to_check, step_size=0.0001, threshold=0.05, print_net=True): net_results, net_grads, full_net = _get_grad( net, [], outputs_with_grad, input_values, [input_to_check]) analytic_grad = net_grads[input_to_check] def GetLoss(new_value): workspace.blobs[input_to_check] = new_value workspace.RunNetOnce(full_net) return sum([ workspace.blobs[output] for output in outputs_with_grad ]).sum() def GetValue(dim, delta): input_value = input_values[input_to_check].copy() input_value.flat[dim] += delta return input_value grad_estimate = np.zeros_like(input_values[input_to_check]) for dim in range(input_values[input_to_check].size): pos_loss = GetLoss(GetValue(dim, step_size)) neg_loss = GetLoss(GetValue(dim, -step_size)) grad_estimate.flat[dim] = (pos_loss - neg_loss) / step_size / 2 err_msg = "Error in gradient check for net_copy {}".format( net.Name()) if print_net: err_msg += ": {}".format(net.Proto()) return _assert_close(analytic_grad, grad_estimate, threshold, err_msg) class GradientChecker: """A gradient checker in Python. This is not the most efficient way to check gradients, as the Python interface will involve a lot of copies back and forth operations. Use at your own risk. """ def __init__( self, stepsize, threshold, device_option=None, workspace_name="gradient_check", input_device_options=None, ): self._stepsize = stepsize self._threshold = threshold self._device_option = device_option or caffe2_pb2.DeviceOption() self._workspace_name = workspace_name if input_device_options is None: self._input_device_options = {} else: self._input_device_options = input_device_options def GetLossAndGrad( self, op, grad_ops, inputs, input_names, input_to_check, grad_name, outputs_with_grads ): for i in range(len(inputs)): workspace.FeedBlob(input_names[i], inputs[i], self._input_device_options.get( input_names[i], self._device_option)) x = inputs[input_to_check] # Run. workspace.RunOperatorOnce(op) loss = 0. # Get Loss and feed in the gradients, run gradient ops. for idx in outputs_with_grads: name = op.output[idx] arr = workspace.FetchBlob(name) loss += (arr**2).sum() workspace.FeedBlob(name + '_grad', arr, self._device_option) loss /= 2. # Run gradient ops workspace.RunOperatorsOnce(grad_ops) # Get gradients if isinstance(grad_name, core.GradientSlice): workspace.FeedBlob('zeros', np.zeros_like(x, dtype=np.float32)) workspace.FeedBlob('ones', np.ones(1, dtype=np.float32)) gv_cpu_op = core.CreateOperator( 'EnsureCPUOutput', grad_name.values, grad_name.values + '_cpu', device_option=self._device_option ) gi_cpu_op = core.CreateOperator( 'EnsureCPUOutput', grad_name.indices, grad_name.indices + '_cpu', device_option=self._device_option ) sparse_to_dense_op = core.CreateOperator( 'ScatterWeightedSum', [ 'zeros', 'ones', grad_name.indices + '_cpu', grad_name.values + '_cpu', 'ones' ], 'zeros', ) workspace.RunOperatorOnce(gv_cpu_op) workspace.RunOperatorOnce(gi_cpu_op) workspace.RunOperatorOnce(sparse_to_dense_op) grad = workspace.FetchBlob('zeros') else: grad = workspace.FetchBlob(grad_name) return loss, grad def CheckSimple( self, op, inputs, input_to_check, outputs_with_grads, grad_ops=None, input_device_options=None, ensure_outputs_are_inferred=False, ): """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. outputs_with_grads: indices specifying which output blobs will we need to check gradients with. For these outputs, we will collect a squared sum and also feed in their gradients. grad_operator: the gradient operator. If not given, we will get the gradient operator from the gradient registry. input_device_options: an optional mapping from input names to DeviceOptions (to override the default DeviceOption) ensure_outputs_are_inferred: if set will assert that the gradient output shapes matches the inferred shapes Outputs: boolean: True if it passes, False if it does not pass. """ # Entering the checker workspace old_ws_name = workspace.CurrentWorkspace() if self._workspace_name != old_ws_name: workspace.SwitchWorkspace(self._workspace_name, True) op.device_option.CopyFrom(self._device_option) if grad_ops is None: # TODO(jiayq): use the gradient registration instead of the old # hack. grad_ops, g_input = getGradientForOp(op) _input_device_options = input_device_options or \ core.InferOpBlobDevicesAsDict(op)[0] # First, feed in the input. for i, arr in enumerate(inputs): workspace.FeedBlob( op.input[i], arr, _input_device_options.get( op.input[i], self._device_option)) # Get the loss and gradient for the original. grad_name = g_input[input_to_check] loss, grad = self.GetLossAndGrad( op, grad_ops, inputs, op.input, input_to_check, grad_name, outputs_with_grads, ) grad_estimate = np.zeros_like(inputs[input_to_check]) if grad_estimate.shape != grad.shape: raise Exception( "Mismatched gradient shapes: estimated ({}), grad ({})".format( grad_estimate.shape, grad.shape)) if ensure_outputs_are_inferred: self._assertInferTensorChecks(op, grad_ops) full_grad_check = os.getenv('CAFFE2_FULL_GRAD_CHECK') == '1' dims_to_check = inputs[input_to_check].size for current_dim in range(dims_to_check): # Grad check is very expensive (as it involves running the op from # scratch for each of the input tensor elements). Thus, let's # run it by default only on a small subset of dimensions. Here we # apply very scientific approach: the first and the last 3 elements # of each tensor. Pass CAFFE2_FULL_GRAD_CHECK=1 env var to enable # the full check if not full_grad_check and current_dim >= 3 and \ current_dim + 3 < dims_to_check: grad_estimate.flat[current_dim] = grad.flat[current_dim] continue # Positive gradient inputs[input_to_check].flat[current_dim] += self._stepsize pos_loss, _ = self.GetLossAndGrad( op, grad_ops, inputs, op.input, input_to_check, grad_name, outputs_with_grads ) # Negative gradient inputs[input_to_check].flat[current_dim] -= self._stepsize * 2 neg_loss, _ = self.GetLossAndGrad( op, grad_ops, inputs, op.input, input_to_check, grad_name, outputs_with_grads ) # Recover the value inputs[input_to_check].flat[current_dim] += self._stepsize grad_estimate.flat[current_dim] = ( pos_loss - neg_loss) / self._stepsize / 2 # Now, check correctness fail_mat = ~np.isclose( grad, grad_estimate, atol=self._threshold, rtol=self._threshold) if np.any(fail_mat): idx = np.flatnonzero(fail_mat) print('Failed. [idx, grad, grad_estimate] are:') print(np.vstack([idx, grad.flat[idx], grad_estimate.flat[idx]]).T) ret = False else: ret = True # After finishing, cleaning up things. if self._workspace_name != old_ws_name: # We reset the workspace to make sure everything intermediate is # cleaned up. Note that there is no need to delete a workspace - # when empty it takes a very limited amount of memory. workspace.ResetWorkspace() workspace.SwitchWorkspace(old_ws_name) return ret, grad, grad_estimate def _assertInferTensorChecks(self, op, grad_ops): tmp_net = caffe2_pb2.NetDef() tmp_net.op.extend([op]) tmp_net.op.extend(grad_ops) inferred_shapes, inferred_types = workspace.InferShapesAndTypes( [tmp_net], nets_proto=True, ) outputs = set() for grad_op in grad_ops: outputs.update(grad_op.output) for output in outputs: if output not in inferred_shapes: raise Exception( "expected output {} to be inferred".format(output)) blob = workspace.FetchBlob(output) correct_shape = list(blob.shape) inferred_shape = list(inferred_shapes[output]) if correct_shape != inferred_shape: raise Exception( "Mismatched inferred shape: want({}), got({})".format( correct_shape, inferred_shape)) if type(blob) is np.ndarray: if blob.dtype == np.dtype('float64'): correct_type = caffe2_pb2.TensorProto.DOUBLE elif blob.dtype == np.dtype('float32'): correct_type = caffe2_pb2.TensorProto.FLOAT elif blob.dtype == np.dtype('int32'): correct_type = caffe2_pb2.TensorProto.INT32 elif blob.dtype == np.dtype('int64'): correct_type = caffe2_pb2.TensorProto.INT64 else: correct_type = "unknown {}".format(np.dtype) else: correct_type = str(type(blob)) inferred_type = inferred_types[output] if correct_type != inferred_type: raise Exception( "Mismatched inferred type: want({}), got({})".format( correct_type, inferred_type))