/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/crf.py (13250B)
## @package crf # Module caffe2.python.crf import numpy as np from caffe2.python import brew, core, model_helper, recurrent """ Due to a limitation in ReccurentNetworkOp, this layer only supports batch_size=1 In order to support batch_size > 1, we will have to implement the CRFUnit and its gradient in C++ and handle the different batches there. """ class CRFWithLoss(object): def __init__(self, model, num_classes, transitions_blob=None): self.model = model self.num_classes = num_classes self.num_classes_padded = num_classes + 2 # After adding BOS and EOS if not transitions_blob: transitions_blob = self.model.param_init_net.UniformFill( [], [core.ScopedBlobReference("crf_transitions")], shape=[self.num_classes_padded, self.num_classes_padded], min=-1.0, max=1.0, ) self.transitions = transitions_blob self.model.params.append(self.transitions) def crf_loss(self, predictions, labels, seq_lengths=None): # Since the transitions matrix is a shared parameter, need to # take a snapshot of it at the beginning since it can be updated # in between the operators that uses it when doing parallel updates transitions_snapshot = self.model.net.Copy( self.transitions, core.ScopedBlobReference("transitions_snapshot") ) # Compute best path unary score from the logits path_unary_score = self._gather_entries_sum( predictions, labels, self.num_classes ) # Append BOS and EOS entries to the predictions and labels predictions = CRFWithLoss.pad_predictions( predictions, self.model.param_init_net, self.model.net, self.num_classes ) labels = CRFWithLoss.pad_labels( labels, self.model.param_init_net, self.model.net, self.num_classes ) # Compute best path binary scores from the transitions matrix path_binary_score = self._path_binary_scores( labels, transitions_snapshot, seq_lengths ) path_total_score = self.model.net.Add( [path_binary_score, path_unary_score], core.ScopedBlobReference("path_total"), ) # Compute all paths score zero_index = self.model.param_init_net.ConstantFill([], shape=[1], value=0) initial_state = self.model.net.Gather( [predictions, zero_index], core.ScopedBlobReference("rnn_initial"), dense_gradient=True, ) input_data, _ = self.model.net.RemovePadding( [predictions], padding_width=1, end_padding_width=0, outputs=2 ) input_data = self.model.net.ExpandDims( [input_data], core.ScopedBlobReference("rnn_input_data"), dims=[1] ) # Due to a bug in RecurrentNetworkGradientOp, we need to copy the # transitions blob before sending it to the recurrent network transitions_copy = self.model.net.Copy( transitions_snapshot, core.ScopedBlobReference("transitions_copy") ) all_paths_scores = self._crf_forward( input_data, initial_state, transitions_copy ) loss = self.model.net.Sub( [all_paths_scores, path_total_score], core.ScopedBlobReference("crf_loss") ) return loss def _path_binary_scores(self, labels, transitions, seq_lengths=None): column_ids, _ = self.model.net.RemovePadding( [labels], outputs=2, padding_width=1, end_padding_width=0 ) row_ids, _ = self.model.net.RemovePadding( [labels], outputs=2, padding_width=0, end_padding_width=1 ) # Since there is no multi-dimensional gather, I flatten the matrix to # a 1-d vector and transform the ids to (row_ids * num_columns + # column_ids) and do gather in 1-d num_columns_blob = self.model.net.ConstantFill( [row_ids], value=self.num_classes_padded ) flattened_ids = self.model.net.Mul([row_ids, num_columns_blob]) flattened_ids = self.model.net.Add([flattened_ids, column_ids]) flattened_transitions = self.model.net.FlattenToVec([transitions]) entries = self.model.net.Gather( [flattened_transitions, flattened_ids], dense_gradient=True ) return self.model.ReduceFrontSum(entries) def _gather_entries_sum(self, in_data, indices, index_size): indices = self.model.net.Cast([indices], to="int64") index_size_blob = self.model.param_init_net.ConstantFill( [], shape=[1], value=index_size ) query_one_hot = self.model.net.OneHot([indices, index_size_blob]) flattend_query = self.model.net.FlattenToVec(query_one_hot) flattend_data = self.model.net.FlattenToVec(in_data) query_scores = self.model.net.DotProduct([flattend_query, flattend_data]) final_sum = self.model.net.ReduceFrontSum([query_scores]) return final_sum def _crf_forward( self, input_blob, initial_state, transitions_copy, seq_lengths=None ): # Build the RNN net and get the last timestep output out_last = self.build_crf_net(input_blob, initial_state, transitions_copy) out_last, _ = self.model.net.Reshape( [out_last], outputs=2, shape=(self.num_classes_padded,) ) zero_segment_id = self.model.param_init_net.ConstantFill( [], value=0, shape=[self.num_classes_padded], dtype=core.DataType.INT32 ) # Compute the accumulated total score of all the paths accum_score = self.model.net.SortedSegmentRangeLogSumExp( [out_last, zero_segment_id] ) accum_score, _ = self.model.net.Reshape(accum_score, outputs=2, shape=()) return accum_score def build_crf_net(self, input_blob, initial_state, transitions): """ Adds the crf_net recurrent operator to the model. model: model_helper.ModelHelper object new operators would be added to input_blob: the input sequence in a format T x N x D where T is sequence size, N - batch size and D - input dimension ##Only supports batch-size 1## seq_lengths: blob containing sequence lengths (unused) """ scope = "crf_net" def s(name): "" # We have to manually scope due to our internal/external blob # relationships. return "{}/{}".format(str(scope), str(name)) step_model = model_helper.ModelHelper(name="crf_step", param_model=self.model) input_t, cell_t_prev, _ = step_model.net.AddExternalInputs( core.ScopedBlobReference("input_t"), core.ScopedBlobReference("cell_t_prev"), transitions, ) zero_segment_id = step_model.param_init_net.ConstantFill( [], [s("zero_segment_id")], value=0, shape=[self.num_classes_padded], dtype=core.DataType.INT32, ) # A hack to bypass model cloning for test step_model.param_init_net.AddExternalOutput(zero_segment_id) """ the CRF step """ # Do tile prev_transpose = brew.transpose( step_model, cell_t_prev, [s("prev_transpose")], axes=(0, 2, 1) ) prev_tiled = step_model.net.Tile( prev_transpose, [s("prev_tiled")], tiles=self.num_classes_padded, axis=2 ) input_t_tiled = step_model.net.Tile( input_t, [s("input_t_tiled")], tiles=self.num_classes_padded, axis=1 ) input_with_prev = step_model.net.Add( [prev_tiled, input_t_tiled], [s("input_with_prev")] ) all_with_transitions = step_model.net.Add( [input_with_prev, transitions], [s("prev_with_transitions")], broadcast=1, use_grad_hack=1, ) all_with_transitions_reshaped, _ = step_model.net.Reshape( all_with_transitions, [s("all_with_transitions_reshaped"), s("all_with_transitions_orig")], shape=(self.num_classes_padded, self.num_classes_padded), ) cell_t = step_model.net.SortedSegmentRangeLogSumExp( [all_with_transitions_reshaped, zero_segment_id], [s("cell_t")] ) step_model.net.AddExternalOutputs(cell_t) """ recurrent network """ cell_input_blob = initial_state out_all, out_last = recurrent.recurrent_net( net=self.model.net, cell_net=step_model.net, inputs=[(input_t, input_blob)], initial_cell_inputs=[(cell_t_prev, cell_input_blob)], links={cell_t_prev: cell_t}, scope=scope, outputs_with_grads=(1,), ) return out_last def update_predictions(self, classes): def crf_update_predictions_op(inputs, outputs): # This operator will compute the best path of classes by performing # Viterbi decoding and then updates the predictions to make the tag # On the best path has the highest score among the others predictions = inputs[0].data transitions = inputs[1].data predictions = inputs[0].data predictions_shape = inputs[0].shape outputs[0].reshape(predictions_shape) trellis = np.zeros(predictions_shape) backpointers = np.zeros(predictions_shape, dtype=np.int32) trellis[0] = predictions[0] for t in range(1, predictions_shape[0]): v = np.expand_dims(trellis[t - 1], 1) + transitions trellis[t] = predictions[t] + np.max(v, 0) backpointers[t] = np.argmax(v, 0) viterbi = [np.argmax(trellis[-1])] for bp in reversed(backpointers[1:]): viterbi.append(bp[viterbi[-1]]) viterbi.reverse() new_predictions = np.zeros(predictions_shape) old_bests = [] for i, w_predictions in enumerate(predictions): # Get the current tag with the maximum score new_predictions[i] = predictions[i] old_best = np.argmax(w_predictions) old_bests.append(old_best) # Swap the scores of the current best tag and the tag on the # Viterbi path w_predictions[viterbi[i]], w_predictions[old_best] = ( w_predictions[old_best], w_predictions[viterbi[i]], ) new_predictions[i] = w_predictions # Remove the BOS and EOS entries from the predictions matrix orig_predictions = new_predictions[1:-1, 0:-2] outputs[0].reshape(orig_predictions.shape) outputs[0].data[...] = orig_predictions padded_classes = CRFWithLoss.pad_predictions( classes, self.model.param_init_net, self.model.net, self.num_classes ) new_classes = self.model.net.Python(crf_update_predictions_op)( [padded_classes, self.transitions], core.ScopedBlobReference("post_crf_classes"), ) return new_classes @staticmethod def pad_labels(labels, init_net, net, num_classes): bos_i = num_classes eos_i = num_classes + 1 bos_i_b = init_net.ConstantFill([], shape=[1], value=bos_i) eos_i_b = init_net.ConstantFill([], shape=[1], value=eos_i) labels = net.Cast([labels], to="int64") padded_labels, _ = net.Concat([bos_i_b, labels, eos_i_b], axis=0, outputs=2) return padded_labels @staticmethod def pad_predictions(predictions, init_net, net, num_classes): # This function will introduce two labels for beginning of sequence # And end of sequence, it will make the necessary udpates to the # the predictions blob low_score = -1000.0 # An arbitray very low number b_scores = np.array([[low_score] * num_classes + [0, low_score]]).astype( np.float32 ) e_scores = np.array([[low_score] * num_classes + [low_score, 0]]).astype( np.float32 ) b_scores = init_net.GivenTensorFill( [], "b_scores", shape=[1, num_classes + 2], values=b_scores ) e_scores = init_net.GivenTensorFill( [], "e_scores", shape=[1, num_classes + 2], values=e_scores ) zero_index = net.ConstantFill([], shape=[1], value=0) length = net.Gather([net.Shape([predictions]), zero_index]) length = net.Cast(length, to="int32") t_range = net.LengthsRangeFill(length) padding = net.ConstantFill([t_range], value=low_score) padding = net.ExpandDims(padding, dims=[1]) padded_predictions, _ = net.Concat( [predictions, padding, padding], outputs=2, axis=1 ) padded_predictions_concat, _ = net.Concat( [b_scores, padded_predictions, e_scores], outputs=2, axis=0 ) return padded_predictions_concat