Register the Python layer for an Operator. Args: op_type: The name of the operator to be created. This function takes in the operator type (sigmoid, mean , average etc) and creates the operator functionality.
(op_type: str)
| 52 | |
| 53 | |
| 54 | def generate_layer_fn(op_type: str): |
| 55 | """Register the Python layer for an Operator. |
| 56 | |
| 57 | Args: |
| 58 | op_type: The name of the operator to be created. |
| 59 | |
| 60 | This function takes in the operator type (sigmoid, mean , average etc) and |
| 61 | creates the operator functionality. |
| 62 | |
| 63 | """ |
| 64 | op_proto = OpProtoHolder.instance().get_op_proto(op_type) |
| 65 | not_intermediate_outputs = [ |
| 66 | output for output in op_proto.outputs if not output.intermediate |
| 67 | ] |
| 68 | intermediate_outputs = [ |
| 69 | output for output in op_proto.outputs if output.intermediate |
| 70 | ] |
| 71 | |
| 72 | if len(not_intermediate_outputs) != 1: |
| 73 | raise ValueError( |
| 74 | "Only one non intermediate output operator can be" |
| 75 | f"automatically generated. {op_type}" |
| 76 | ) |
| 77 | |
| 78 | if not_intermediate_outputs[0].duplicable: |
| 79 | raise ValueError( |
| 80 | "Only non duplicable op can be automatically generated." |
| 81 | ) |
| 82 | |
| 83 | for output in intermediate_outputs: |
| 84 | if output.duplicable: |
| 85 | raise ValueError( |
| 86 | "The op can be automatically generated only when " |
| 87 | "all intermediate ops are not duplicable." |
| 88 | ) |
| 89 | |
| 90 | o_name = not_intermediate_outputs[0].name |
| 91 | intermediate_output_names = [output.name for output in intermediate_outputs] |
| 92 | |
| 93 | def infer_and_check_dtype(op_proto, *args, **kwargs): |
| 94 | """ |
| 95 | This function performs the sanity check for dtype and |
| 96 | instance type. |
| 97 | """ |
| 98 | dtype = None |
| 99 | for ipt in op_proto.inputs: |
| 100 | name = _convert_(ipt.name) |
| 101 | val = kwargs.pop(name, []) |
| 102 | if not isinstance(val, list) and not isinstance(val, tuple): |
| 103 | val = [val] |
| 104 | if len(val) == 0: |
| 105 | if len(args) == 0: |
| 106 | continue |
| 107 | val = [args[0]] |
| 108 | args = args[1:] |
| 109 | |
| 110 | for each in val: |
| 111 | if not isinstance(each, Variable): |