| 29 | class _Functional: |
| 30 | def __getattribute__(self, op_type): |
| 31 | def op_func(*inputs, **args): |
| 32 | ws = Workspace() |
| 33 | schema = OpSchema.get(op_type) |
| 34 | input_prefix = 'input_' |
| 35 | output_prefix = 'output_' |
| 36 | |
| 37 | def get_name_list(prefix, num, max_num): |
| 38 | return [prefix + str(x) for x in range(min(num, max_num))] |
| 39 | |
| 40 | input_names, output_names = [], [] |
| 41 | input_names = get_name_list( |
| 42 | input_prefix, len(inputs), schema.max_input |
| 43 | ) |
| 44 | # verify the length of input name is in range |
| 45 | # of schema |
| 46 | num_input = len(input_names) |
| 47 | if num_input > schema.max_input or num_input < \ |
| 48 | schema.min_input or not schema.num_inputs_allowed(num_input): |
| 49 | raise ValueError( |
| 50 | "Functional C2: Number of inputs not in \ |
| 51 | range: {} - {} or not allowed." |
| 52 | .format(schema.min_input, schema.max_input) |
| 53 | ) |
| 54 | |
| 55 | if 'num_output' in args: |
| 56 | num_output = args['num_output'] |
| 57 | if num_output > schema.max_output or \ |
| 58 | num_output < schema.min_output or \ |
| 59 | not schema.num_outputs_allowed(num_output) or \ |
| 60 | not schema.num_inputs_outputs_allowed(num_input, |
| 61 | num_output): |
| 62 | raise ValueError( |
| 63 | "Functional C2: Number of output \ |
| 64 | not in range: {} - {} or not allowed" |
| 65 | .format(schema.min_output, schema.max_output) |
| 66 | ) |
| 67 | output_names = get_name_list( |
| 68 | output_prefix, num_output, schema.max_output |
| 69 | ) |
| 70 | args.pop('num_output') |
| 71 | calculated = schema.CalculateOutput(num_input) |
| 72 | if not output_names and calculated != -1: |
| 73 | output_names = get_name_list( |
| 74 | output_prefix, calculated, schema.max_output |
| 75 | ) |
| 76 | |
| 77 | if not output_names: |
| 78 | max_output = schema.max_output |
| 79 | # For an op with max_output == inf |
| 80 | # and no Output defined in schema |
| 81 | # user should pass output_size explicitly |
| 82 | if schema.inf == max_output: |
| 83 | raise ValueError( |
| 84 | "For operators with max_output == inf,\ |
| 85 | user should pass num_output explicitly." |
| 86 | ) |
| 87 | output_names = get_name_list( |
| 88 | output_prefix, max_output, max_output |