MCPcopy Create free account
hub / github.com/pytorch/pytorch / init_then_script

Method init_then_script

torch/jit/_script.py:301–330  ·  view source on GitHub ↗
(self, *args, **kwargs)

Source from the content-addressed store, hash-verified

299
300 @functools.wraps(original_init)
301 def init_then_script(self, *args, **kwargs):
302 num_methods = len(cls._methods)
303 original_init(self, *args, **kwargs)
304 added_methods_in_init = len(cls._methods) > num_methods
305
306 if type(self) == cls:
307
308 def make_stubs(module):
309 cls = type(module)
310 if hasattr(cls, "_methods"):
311 return [v for k, v in sorted(cls._methods.items())]
312 else:
313 return infer_methods_to_compile(module)
314
315 self.__dict__[
316 "_actual_script_module"
317 ] = torch.jit._recursive.create_script_module(
318 self, make_stubs, share_types=not added_methods_in_init
319 )
320
321 # Delete the Python attributes that now shadow the ScriptModule
322 # ones, so that __getattr__ and __setattr__ will properly find
323 # the scripted versions.
324 concrete_type = self._actual_script_module._concrete_type
325 for name in concrete_type.get_attributes():
326 delattr(self, name)
327 for name, _ in concrete_type.get_modules():
328 delattr(self, name)
329 for name in ("_parameters", "_buffers", "_modules"):
330 delattr(self, name)
331
332 cls.__init__ = init_then_script # type: ignore[misc]
333 super().__init__(name, bases, attrs)

Callers

nothing calls this directly

Calls 2

get_attributesMethod · 0.80
get_modulesMethod · 0.45

Tested by

no test coverage detected