L i$VddlZddlZddlZddlZddlZGddej Zy)NceZdZdZdej j ddfdZdejde de fdZ d Z d Zd Zy) AttributeTypeIsSupportedCheckeraCheck the ``__init__`` method of a given ``nn.Module``. It ensures that all instance-level attributes can be properly initialized. Specifically, we do type inference based on attribute values...even if the attribute in question has already been typed using Python3-style annotations or ``torch.jit.annotate``. This means that setting an instance-level attribute to ``[]`` (for ``List``), ``{}`` for ``Dict``), or ``None`` (for ``Optional``) isn't enough information for us to properly initialize that attribute. An object of this class can walk a given ``nn.Module``'s AST and determine if it meets our requirements or not. Known limitations 1. We can only check the AST nodes for certain constructs; we can't ``eval`` arbitrary expressions. This means that function calls, class instantiations, and complex expressions that resolve to one of the "empty" values specified above will NOT be flagged as problematic. 2. We match on string literals, so if the user decides to use a non-standard import (e.g. `from typing import List as foo`), we won't catch it. Example: .. code-block:: python class M(torch.nn.Module): def fn(self): return [] def __init__(self) -> None: super().__init__() self.x: List[int] = [] def forward(self, x: List[int]): self.x = x return 1 The above code will pass the ``AttributeTypeIsSupportedChecker`` check since we have a function call in ``__init__``. However, it will still fail later with the ``RuntimeError`` "Tried to set nonexistent attribute: x. Did you forget to initialize it in __init__()?". Args: nn_module - The instance of ``torch.nn.Module`` whose ``__init__`` method we wish to check nn_modulereturnNctj|jj}d}dj |j dDcgc] }||r |c}}t jtj|}t|jj|_ d|_|j|ycc}w)Ncl|j}|jdxr|jd S)N#z# type:)strip startswith)lines V/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/jit/_check.pyis_useless_commentzAAttributeTypeIsSupportedChecker.check..is_useless_commentAs-::r*r)dictrr+The TorchScript type system doesn't support instance-level annotations on empty non-base types in `__init__`. Instead, either 1) use a type annotation in the class body, or 2) wrap the type in `torch.jit.Attribute`.) targetr/idr5r4r annotationr0warningswarnr!r& containersr's r visit_AnnAssignz/AttributeTypeIsSupportedChecker.visit_AnnAssigns  {{  ##v-. ;;  t;; ; B  $$''z9:??((++'' H=   1 M   4    s##C"C CC C! C!c||jry |jjjjdk7s<|jjjdk7s|jjdk7r|j |na|jjjjdk7s#|jjjdk7r|j |t|jdk7ryt|jdtjsyhd} |jdjj}||vry|j|jd|sytjd y#t $r|j |YwxYw#t $rYywxYw) aDetermine if a Call node is 'torch.jit.annotate' in __init__. Visit a Call node in an ``nn.Module``'s ``__init__`` method and determine if it's ``torch.jit.annotate``. If so, see if it conforms to our attribute annotation rules. Ntorchjitannotater>r*r)r+r:)rfuncr/r<r4r6r5lenargsr,r Subscriptr0r>r?r@s r visit_Callz*AttributeTypeIsSupportedChecker.visit_Callsg  ( (  % %%((G399??''5099>>Z/""4( %%((E1TYY__5I5IZ5W""4( tyy>Q  $))A, 6 2  yy|)),,H : % '' ! h?   1 C %   t $ %.   s$CF1#F/F,+F,/ F;:F;)__name__ __module__ __qualname____doc__rDnnModuler%rASTstrboolr0r7rBrMrr rr sQ0duxx4.342..4 l? rr)rrrr>rD NodeVisitorrrWrr rYs&  o cooo r