Factory class for creating BasePyModule instances with Python functions.
| 106 | continue |
| 107 | |
| 108 | class ModuleFactory: |
| 109 | """Factory class for creating BasePyModule instances with Python functions.""" |
| 110 | |
| 111 | def __init__(self, module, pyfunc_methods, original_class): |
| 112 | self.ir_module = module |
| 113 | self.pyfunc_methods = pyfunc_methods |
| 114 | self.original_class = original_class |
| 115 | |
| 116 | def __call__(self, device=None, target=None): |
| 117 | if device is None: |
| 118 | device = cpu(0) |
| 119 | |
| 120 | instance_ir_mod = ir.IRModule() |
| 121 | for global_var, func in self.ir_module.functions_items(): |
| 122 | instance_ir_mod[global_var] = func |
| 123 | |
| 124 | instance = BasePyModule(instance_ir_mod, device, target) |
| 125 | |
| 126 | for method_name in self.pyfunc_methods: |
| 127 | if hasattr(self.original_class, method_name): |
| 128 | method = getattr(self.original_class, method_name) |
| 129 | instance.add_python_function(method_name, method) |
| 130 | |
| 131 | return instance |
| 132 | |
| 133 | def __getattr__(self, name): |
| 134 | if hasattr(self.ir_module, name): |
| 135 | return getattr(self.ir_module, name) |
| 136 | raise AttributeError( |
| 137 | f"'{self.__class__.__name__}' object has no attribute '{name}'" |
| 138 | ) |
| 139 | |
| 140 | factory = ModuleFactory(m, pyfunc_methods, mod) |
| 141 | setattr(factory, "__name__", mod.__name__) |
no outgoing calls
no test coverage detected
searching dependent graphs…