/caffe2/python/functional.py

https://github.com/pytorch/pytorch
Python | 114 lines | 84 code | 21 blank | 9 comment | 21 complexity | 9ab6e12f50c1b8d54257d26f1e7eafe0 MD5 | raw file
  1. from caffe2.python import core, workspace
  2. from caffe2.proto import caffe2_pb2
  3. from caffe2.python.onnx.workspace import Workspace
  4. from collections import namedtuple
  5. from six import string_types
  6. OpSchema = workspace.C.OpSchema
  7. def namedtupledict(typename, field_names, *args, **kwargs):
  8. field_names_map = {n: i for i, n in enumerate(field_names)}
  9. # Some output names are invalid python identifier, e.g. "0"
  10. kwargs.setdefault('rename', True)
  11. data = namedtuple(typename, field_names, *args, **kwargs)
  12. def getitem(self, key):
  13. if isinstance(key, string_types):
  14. key = field_names_map[key]
  15. return super(type(self), self).__getitem__(key)
  16. data.__getitem__ = getitem
  17. return data
  18. class _Functional(object):
  19. def __getattribute__(self, op_type):
  20. def op_func(*inputs, **args):
  21. ws = Workspace()
  22. schema = OpSchema.get(op_type)
  23. input_prefix = 'input_'
  24. output_prefix = 'output_'
  25. def get_name_list(prefix, num, max_num):
  26. return [prefix + str(x) for x in range(min(num, max_num))]
  27. input_names, output_names = [], []
  28. input_names = get_name_list(
  29. input_prefix, len(inputs), schema.max_input
  30. )
  31. # verify the length of input name is in range
  32. # of schema
  33. num_input = len(input_names)
  34. if num_input > schema.max_input or num_input < \
  35. schema.min_input or not schema.num_inputs_allowed(num_input):
  36. raise ValueError(
  37. "Functional C2: Number of inputs not in \
  38. range: {} - {} or not allowed."
  39. .format(schema.min_input, schema.max_input)
  40. )
  41. if 'num_output' in args:
  42. num_output = args['num_output']
  43. if num_output > schema.max_output or \
  44. num_output < schema.min_output or \
  45. not schema.num_outputs_allowed(num_output) or \
  46. not schema.num_inputs_outputs_allowed(num_input,
  47. num_output):
  48. raise ValueError(
  49. "Functional C2: Number of output \
  50. not in range: {} - {} or not allowed"
  51. .format(schema.min_output, schema.max_output)
  52. )
  53. output_names = get_name_list(
  54. output_prefix, num_output, schema.max_output
  55. )
  56. args.pop('num_output')
  57. calculated = schema.CalculateOutput(num_input)
  58. if not output_names and calculated != -1:
  59. output_names = get_name_list(
  60. output_prefix, calculated, schema.max_output
  61. )
  62. if not output_names:
  63. max_output = schema.max_output
  64. # For an op with max_output == inf
  65. # and no Output defined in schema
  66. # user should pass output_size explicitly
  67. if schema.inf == max_output:
  68. raise ValueError(
  69. "For operators with max_output == inf,\
  70. user should pass num_output explicitly."
  71. )
  72. output_names = get_name_list(
  73. output_prefix, max_output, max_output
  74. )
  75. # There could be input-output inplace enforcement; replace the
  76. # output names with input ones if such enforcements exist
  77. for i in range(len(input_names)):
  78. for j in range(len(output_names)):
  79. if schema.inplace_enforced(i, j):
  80. output_names[j] = input_names[i]
  81. op = core.CreateOperator(
  82. op_type, input_names, output_names, **args
  83. )
  84. device_option = args.get('device_option', core.DeviceOption(caffe2_pb2.CPU))
  85. with core.DeviceScope(device_option):
  86. for i, input_blob in enumerate(inputs):
  87. ws.FeedBlob(input_names[i], input_blob)
  88. # RunOperator
  89. ws.RunOperatorOnce(op)
  90. output_values = [ws.FetchBlob(x) for x in output_names]
  91. return namedtupledict('output', output_names)(*output_values)
  92. return op_func
  93. Functional = _Functional()