|
| 1 | +# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import argparse |
| 16 | + |
| 17 | +from codegen_utils import ( |
| 18 | + FunctionGeneratorBase, |
| 19 | + GeneratorBase, |
| 20 | +) |
| 21 | + |
| 22 | +IMPORT_TEMPLATE = """ |
| 23 | +import paddle |
| 24 | +from paddle import _C_ops |
| 25 | +from .. import core |
| 26 | +""" |
| 27 | + |
| 28 | +FUNCTION_NAME_TEMPLATE = """ |
| 29 | +def {func_name}(): |
| 30 | +""" |
| 31 | + |
| 32 | +NAME_METHOD_MAPPING_TEMPLATE = """ ('{api_name}',_{api_name})""" |
| 33 | + |
| 34 | +METHODS_MAP_TEMPLATE = """ |
| 35 | +methods_map = [ |
| 36 | +{} |
| 37 | +] |
| 38 | +""" |
| 39 | + |
| 40 | +METHOD_TEMPLATE = """ |
| 41 | +def _{name}(self,*args, **kwargs): |
| 42 | + return _C_ops.{name}(self,*args, **kwargs) |
| 43 | +""" |
| 44 | +SET_METHOD_TEMPLATE = """ |
| 45 | + # set methods for Tensor in dygraph |
| 46 | + local_tensor = core.eager.Tensor |
| 47 | + for method_name, method in methods_map: |
| 48 | + setattr(local_tensor, method_name, method) |
| 49 | +
|
| 50 | +""" |
| 51 | + |
| 52 | + |
| 53 | +class MethodGenerator(FunctionGeneratorBase): |
| 54 | + def __init__(self, forward_api_contents, namespace): |
| 55 | + FunctionGeneratorBase.__init__(self, forward_api_contents, namespace) |
| 56 | + self.need_parse_python_api_args = False |
| 57 | + # Generated Results |
| 58 | + self.Method_str = "" |
| 59 | + |
| 60 | + def GenerateMethod(self, name): |
| 61 | + self.Method_str = METHOD_TEMPLATE.format(name=name) |
| 62 | + |
| 63 | + def run(self): |
| 64 | + # Initialized orig_forward_inputs_list, orig_forward_returns_list, orig_forward_attrs_list |
| 65 | + self.CollectOriginalForwardInfo() |
| 66 | + |
| 67 | + if len(self.python_api_info) > 0: |
| 68 | + self.need_parse_python_api_args = True |
| 69 | + self.ParsePythonAPIInfo() |
| 70 | + for name in self.python_api_names: |
| 71 | + if "Tensor." in name: |
| 72 | + api_name = name.split(".")[-1] |
| 73 | + self.GenerateMethod(api_name) |
| 74 | + self.api_name = api_name |
| 75 | + break |
| 76 | + |
| 77 | + |
| 78 | +class MonkeyPatchTensorMethodsGenerator(GeneratorBase): |
| 79 | + def __init__(self, path): |
| 80 | + # Parent members: |
| 81 | + # self.namespace |
| 82 | + # self.api_yaml_path |
| 83 | + # self.forward_api_list |
| 84 | + GeneratorBase.__init__(self, path) |
| 85 | + |
| 86 | + # Generated Result |
| 87 | + self.MonkeyPatchTensorMethods_str = "" |
| 88 | + |
| 89 | + def GenerateMonkeyPatchTensorMethods(self): |
| 90 | + self.MonkeyPatchTensorMethods_str += IMPORT_TEMPLATE |
| 91 | + |
| 92 | + forward_api_list = self.forward_api_list |
| 93 | + methods_map = [] # [("method_name",method),] |
| 94 | + for forward_api_content in forward_api_list: |
| 95 | + f_generator = MethodGenerator(forward_api_content, None) |
| 96 | + status = f_generator.run() |
| 97 | + method_str = f_generator.Method_str |
| 98 | + if method_str != "": |
| 99 | + methods_map.append( |
| 100 | + NAME_METHOD_MAPPING_TEMPLATE.format( |
| 101 | + api_name=f_generator.api_name |
| 102 | + ) |
| 103 | + ) |
| 104 | + self.MonkeyPatchTensorMethods_str += method_str |
| 105 | + result = ',\n '.join(methods_map) |
| 106 | + self.MonkeyPatchTensorMethods_str += METHODS_MAP_TEMPLATE.format(result) |
| 107 | + self.MonkeyPatchTensorMethods_str += FUNCTION_NAME_TEMPLATE.format( |
| 108 | + func_name="monkey_patch_generated_methods_for_tensor" |
| 109 | + ) |
| 110 | + self.MonkeyPatchTensorMethods_str += SET_METHOD_TEMPLATE |
| 111 | + |
| 112 | + def run(self): |
| 113 | + # Read Yaml file |
| 114 | + self.ParseForwardYamlContents() |
| 115 | + self.GenerateMonkeyPatchTensorMethods() |
| 116 | + |
| 117 | + |
| 118 | +########################## |
| 119 | +# Code Generation Helper # |
| 120 | +########################## |
| 121 | +def ParseArguments(): |
| 122 | + parser = argparse.ArgumentParser( |
| 123 | + description='Eager Code Generator Args Parser for Monkey patch methods ' |
| 124 | + ) |
| 125 | + parser.add_argument('--api_yaml_path', type=str) |
| 126 | + parser.add_argument('--output_path', type=str) |
| 127 | + |
| 128 | + args = parser.parse_args() |
| 129 | + return args |
| 130 | + |
| 131 | + |
| 132 | +def GenerateMonkeyPathFile(filepath, python_c_str): |
| 133 | + with open(filepath, 'w') as f: |
| 134 | + f.write(python_c_str) |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + args = ParseArguments() |
| 139 | + api_yaml_path = args.api_yaml_path |
| 140 | + output_path = args.output_path |
| 141 | + gen = MonkeyPatchTensorMethodsGenerator(api_yaml_path) |
| 142 | + gen.run() |
| 143 | + GenerateMonkeyPathFile(output_path, gen.MonkeyPatchTensorMethods_str) |
0 commit comments