/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/utils.py (14181B)
# @package utils # Module caffe2.python.utils from caffe2.proto import caffe2_pb2 from future.utils import viewitems from google.protobuf.message import DecodeError, Message from google.protobuf import text_format import sys import collections import copy import functools import numpy as np from six import integer_types, binary_type, text_type, string_types OPTIMIZER_ITERATION_NAME = "optimizer_iteration" ITERATION_MUTEX_NAME = "iteration_mutex" def OpAlmostEqual(op_a, op_b, ignore_fields=None): ''' Two ops are identical except for each field in the `ignore_fields`. ''' ignore_fields = ignore_fields or [] if not isinstance(ignore_fields, list): ignore_fields = [ignore_fields] assert all(isinstance(f, text_type) for f in ignore_fields), ( 'Expect each field is text type, but got {}'.format(ignore_fields)) def clean_op(op): op = copy.deepcopy(op) for field in ignore_fields: if op.HasField(field): op.ClearField(field) return op op_a = clean_op(op_a) op_b = clean_op(op_b) return op_a == op_b or str(op_a) == str(op_b) def CaffeBlobToNumpyArray(blob): if (blob.num != 0): # old style caffe blob. return (np.asarray(blob.data, dtype=np.float32) .reshape(blob.num, blob.channels, blob.height, blob.width)) else: # new style caffe blob. return (np.asarray(blob.data, dtype=np.float32) .reshape(blob.shape.dim)) def Caffe2TensorToNumpyArray(tensor): if tensor.data_type == caffe2_pb2.TensorProto.FLOAT: return np.asarray( tensor.float_data, dtype=np.float32).reshape(tensor.dims) elif tensor.data_type == caffe2_pb2.TensorProto.DOUBLE: return np.asarray( tensor.double_data, dtype=np.float64).reshape(tensor.dims) elif tensor.data_type == caffe2_pb2.TensorProto.INT64: return np.asarray( tensor.int64_data, dtype=np.int64).reshape(tensor.dims) elif tensor.data_type == caffe2_pb2.TensorProto.INT32: return np.asarray( tensor.int32_data, dtype=np.int).reshape(tensor.dims) # pb.INT32=>np.int use int32_data elif tensor.data_type == caffe2_pb2.TensorProto.INT16: return np.asarray( tensor.int32_data, dtype=np.int16).reshape(tensor.dims) # pb.INT16=>np.int16 use int32_data elif tensor.data_type == caffe2_pb2.TensorProto.UINT16: return np.asarray( tensor.int32_data, dtype=np.uint16).reshape(tensor.dims) # pb.UINT16=>np.uint16 use int32_data elif tensor.data_type == caffe2_pb2.TensorProto.INT8: return np.asarray( tensor.int32_data, dtype=np.int8).reshape(tensor.dims) # pb.INT8=>np.int8 use int32_data elif tensor.data_type == caffe2_pb2.TensorProto.UINT8: return np.asarray( tensor.int32_data, dtype=np.uint8).reshape(tensor.dims) # pb.UINT8=>np.uint8 use int32_data else: # TODO: complete the data type: bool, float16, byte, int64, string raise RuntimeError( "Tensor data type not supported yet: " + str(tensor.data_type)) def NumpyArrayToCaffe2Tensor(arr, name=None): tensor = caffe2_pb2.TensorProto() tensor.dims.extend(arr.shape) if name: tensor.name = name if arr.dtype == np.float32: tensor.data_type = caffe2_pb2.TensorProto.FLOAT tensor.float_data.extend(list(arr.flatten().astype(float))) elif arr.dtype == np.float64: tensor.data_type = caffe2_pb2.TensorProto.DOUBLE tensor.double_data.extend(list(arr.flatten().astype(np.float64))) elif arr.dtype == np.int64: tensor.data_type = caffe2_pb2.TensorProto.INT64 tensor.int64_data.extend(list(arr.flatten().astype(np.int64))) elif arr.dtype == np.int or arr.dtype == np.int32: tensor.data_type = caffe2_pb2.TensorProto.INT32 tensor.int32_data.extend(arr.flatten().astype(np.int).tolist()) elif arr.dtype == np.int16: tensor.data_type = caffe2_pb2.TensorProto.INT16 tensor.int32_data.extend(list(arr.flatten().astype(np.int16))) # np.int16=>pb.INT16 use int32_data elif arr.dtype == np.uint16: tensor.data_type = caffe2_pb2.TensorProto.UINT16 tensor.int32_data.extend(list(arr.flatten().astype(np.uint16))) # np.uint16=>pb.UNIT16 use int32_data elif arr.dtype == np.int8: tensor.data_type = caffe2_pb2.TensorProto.INT8 tensor.int32_data.extend(list(arr.flatten().astype(np.int8))) # np.int8=>pb.INT8 use int32_data elif arr.dtype == np.uint8: tensor.data_type = caffe2_pb2.TensorProto.UINT8 tensor.int32_data.extend(list(arr.flatten().astype(np.uint8))) # np.uint8=>pb.UNIT8 use int32_data else: # TODO: complete the data type: bool, float16, byte, string raise RuntimeError( "Numpy data type not supported yet: " + str(arr.dtype)) return tensor def MakeArgument(key, value): """Makes an argument based on the value type.""" argument = caffe2_pb2.Argument() argument.name = key iterable = isinstance(value, collections.abc.Iterable) # Fast tracking common use case where a float32 array of tensor parameters # needs to be serialized. The entire array is guaranteed to have the same # dtype, so no per-element checking necessary and no need to convert each # element separately. if isinstance(value, np.ndarray) and value.dtype.type is np.float32: argument.floats.extend(value.flatten().tolist()) return argument if isinstance(value, np.ndarray): value = value.flatten().tolist() elif isinstance(value, np.generic): # convert numpy scalar to native python type value = np.asscalar(value) if type(value) is float: argument.f = value elif type(value) in integer_types or type(value) is bool: # We make a relaxation that a boolean variable will also be stored as # int. argument.i = value elif isinstance(value, binary_type): argument.s = value elif isinstance(value, text_type): argument.s = value.encode('utf-8') elif isinstance(value, caffe2_pb2.NetDef): argument.n.CopyFrom(value) elif isinstance(value, Message): argument.s = value.SerializeToString() elif iterable and all(type(v) in [float, np.float_] for v in value): argument.floats.extend( v.item() if type(v) is np.float_ else v for v in value ) elif iterable and all( type(v) in integer_types or type(v) in [bool, np.int_] for v in value ): argument.ints.extend( v.item() if type(v) is np.int_ else v for v in value ) elif iterable and all( isinstance(v, binary_type) or isinstance(v, text_type) for v in value ): argument.strings.extend( v.encode('utf-8') if isinstance(v, text_type) else v for v in value ) elif iterable and all(isinstance(v, caffe2_pb2.NetDef) for v in value): argument.nets.extend(value) elif iterable and all(isinstance(v, Message) for v in value): argument.strings.extend(v.SerializeToString() for v in value) else: if iterable: raise ValueError( "Unknown iterable argument type: key={} value={}, value " "type={}[{}]".format( key, value, type(value), set(type(v) for v in value) ) ) else: raise ValueError( "Unknown argument type: key={} value={}, value type={}".format( key, value, type(value) ) ) return argument def TryReadProtoWithClass(cls, s): """Reads a protobuffer with the given proto class. Inputs: cls: a protobuffer class. s: a string of either binary or text protobuffer content. Outputs: proto: the protobuffer of cls Throws: google.protobuf.message.DecodeError: if we cannot decode the message. """ obj = cls() try: text_format.Parse(s, obj) return obj except (text_format.ParseError, UnicodeDecodeError): obj.ParseFromString(s) return obj def GetContentFromProto(obj, function_map): """Gets a specific field from a protocol buffer that matches the given class """ for cls, func in viewitems(function_map): if type(obj) is cls: return func(obj) def GetContentFromProtoString(s, function_map): for cls, func in viewitems(function_map): try: obj = TryReadProtoWithClass(cls, s) return func(obj) except DecodeError: continue else: raise DecodeError("Cannot find a fit protobuffer class.") def ConvertProtoToBinary(proto_class, filename, out_filename): """Convert a text file of the given protobuf class to binary.""" with open(filename) as f: proto = TryReadProtoWithClass(proto_class, f.read()) with open(out_filename, 'w') as fid: fid.write(proto.SerializeToString()) def GetGPUMemoryUsageStats(): """Get GPU memory usage stats from CUDAContext/HIPContext. This requires flag --caffe2_gpu_memory_tracking to be enabled""" from caffe2.python import workspace, core workspace.RunOperatorOnce( core.CreateOperator( "GetGPUMemoryUsage", [], ["____mem____"], device_option=core.DeviceOption(workspace.GpuDeviceType, 0), ), ) b = workspace.FetchBlob("____mem____") return { 'total_by_gpu': b[0, :], 'max_by_gpu': b[1, :], 'total': np.sum(b[0, :]), 'max_total': np.sum(b[1, :]) } def ResetBlobs(blobs): from caffe2.python import workspace, core workspace.RunOperatorOnce( core.CreateOperator( "Free", list(blobs), list(blobs), device_option=core.DeviceOption(caffe2_pb2.CPU), ), ) class DebugMode(object): ''' This class allows to drop you into an interactive debugger if there is an unhandled exception in your python script Example of usage: def main(): # your code here pass if __name__ == '__main__': from caffe2.python.utils import DebugMode DebugMode.run(main) ''' @classmethod def run(cls, func): try: return func() except KeyboardInterrupt: raise except Exception: import pdb print( 'Entering interactive debugger. Type "bt" to print ' 'the full stacktrace. Type "help" to see command listing.') print(sys.exc_info()[1]) print pdb.post_mortem() sys.exit(1) raise def raiseIfNotEqual(a, b, msg): if a != b: raise Exception("{}. {} != {}".format(msg, a, b)) def debug(f): ''' Use this method to decorate your function with DebugMode's functionality Example: @debug def test_foo(self): raise Exception("Bar") ''' @functools.wraps(f) def wrapper(*args, **kwargs): def func(): return f(*args, **kwargs) return DebugMode.run(func) return wrapper def BuildUniqueMutexIter( init_net, net, iter=None, iter_mutex=None, iter_val=0 ): ''' Often, a mutex guarded iteration counter is needed. This function creates a mutex iter in the net uniquely (if the iter already existing, it does nothing) This function returns the iter blob ''' iter = iter if iter is not None else OPTIMIZER_ITERATION_NAME iter_mutex = iter_mutex if iter_mutex is not None else ITERATION_MUTEX_NAME from caffe2.python import core if not init_net.BlobIsDefined(iter): # Add training operators. with core.DeviceScope( core.DeviceOption(caffe2_pb2.CPU, extra_info=["device_type_override:cpu"]) ): iteration = init_net.ConstantFill( [], iter, shape=[1], value=iter_val, dtype=core.DataType.INT64, ) iter_mutex = init_net.CreateMutex([], [iter_mutex]) net.AtomicIter([iter_mutex, iteration], [iteration]) else: iteration = init_net.GetBlobRef(iter) return iteration def EnumClassKeyVals(cls): # cls can only be derived from object assert type(cls) == type # Enum attribute keys are all capitalized and values are strings enum = {} for k in dir(cls): if k == k.upper(): v = getattr(cls, k) if isinstance(v, string_types): assert v not in enum.values(), ( "Failed to resolve {} as Enum: " "duplicate entries {}={}, {}={}".format( cls, k, v, [key for key in enum if enum[key] == v][0], v ) ) enum[k] = v return enum def ArgsToDict(args): """ Convert a list of arguments to a name, value dictionary. Assumes that each argument has a name. Otherwise, the argument is skipped. """ ans = {} for arg in args: if not arg.HasField("name"): continue for d in arg.DESCRIPTOR.fields: if d.name == "name": continue if d.label == d.LABEL_OPTIONAL and arg.HasField(d.name): ans[arg.name] = getattr(arg, d.name) break elif d.label == d.LABEL_REPEATED: list_ = getattr(arg, d.name) if len(list_) > 0: ans[arg.name] = list_ break else: ans[arg.name] = None return ans def NHWC2NCHW(tensor): assert tensor.ndim >= 1 return tensor.transpose((0, tensor.ndim - 1) + tuple(range(1, tensor.ndim - 1))) def NCHW2NHWC(tensor): assert tensor.ndim >= 2 return tensor.transpose((0,) + tuple(range(2, tensor.ndim)) + (1,))