/pytorch/source/caffe2/python/functional.py

https://github.com/ryfeus/lambda-packs · Python · 114 lines · 88 code · 17 blank · 9 comment · 21 complexity · d66dcc87662812ffcb3a57fd7d533791 MD5 · raw file

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