
    VhB                    8   d Z ddlmZ ddlZddlZddlmZ ddlZddlm	Z	 ddl
mZmZ ddlmZ ddlmZ erdd	lmZ ddd
Z G d de	j*                        Z G d d      Z e       Zej2                  aej6                  dd       Z	 	 d	 	 	 	 	 	 	 	 	 ddZddZy)zRDiagnostic components for TorchScript based ONNX export, i.e. `torch.onnx.export`.    )annotationsN)TYPE_CHECKING)infra)	formattersarif)version)cpp_backtrace)	Generatorc           
        t        j                  | |      j                  d      }g }|D ]V  }|j                  dd      }t        |      dk(  r#|j	                  |d   j                                F|j	                  d       X t        j                  |D cg c],  }t        j                  t        j                  |            . c}      S c c}w )	av  Returns the current C++ call stack.

    This function utilizes `torch.utils.cpp_backtrace` to get the current C++ call stack.
    The returned C++ call stack is a concatenated string of the C++ call stack frames.
    Each frame is separated by a newline character, in the same format of
    r"frame #[0-9]+: (?P<frame_info>.*)". More info at `c10/util/Backtrace.cpp`.

    
:      z<unknown frame>)message)location)frames)
r	   get_cpp_backtracesplitlenappendstripr   Stack
StackFrameLocation)frames_to_skipframes_to_logr   frame_messagesframesegmentsr   s          \/home/dcms/DCMS/lib/python3.12/site-packages/torch/onnx/_internal/diagnostics/_diagnostic.py_cpp_call_stackr!      s     ,,^]KQQRVWFN 5;;sA&x=A!!(1+"3"3"56!!"345 ;; *
 ennW&EF
 
s   1Cc                  \     e Zd ZU dZdZded<   dZded<   ddd	 	 	 	 	 d fd	Zdd
Z xZ	S )TorchScriptOnnxExportDiagnosticzBase class for all export diagnostics.

    This class is used to represent all export diagnostics. It is a subclass of
    infra.Diagnostic, and adds additional methods to add more information to the
    diagnostic.
    Nzinfra.Stack | Nonepython_call_stackcpp_call_stackr   F)r   	cpp_stackc                   t        |   |i | | j                  |      | _        |r| j	                  |      | _        y y )Nr   )super__init__record_python_call_stackr$   record_cpp_call_stackr%   )selfr   r&   argskwargs	__class__s        r    r*   z(TorchScriptOnnxExportDiagnostic.__init__9   sW     	$)&)!%!>!>) "? "
 "&"<"<- #= #D     c                N    t        |      }d|_        | j                  |       |S )z5Records the current C++ call stack in the diagnostic.r(   zC++ call stack)r!   r   
with_stack)r-   r   stacks      r    r,   z5TorchScriptOnnxExportDiagnostic.record_cpp_call_stackI   s$    ~>(r1   )r   intr&   boolreturnNone)r   r5   r7   infra.Stack)
__name__
__module____qualname____doc__r$   __annotations__r%   r*   r,   __classcell__)r0   s   @r    r#   r#   .   sQ     -1)0)-N&-
  	  	 
 r1   r#   c                  x    e Zd ZU dZded<   ded<   ddZedd       Z	 d	 	 	 	 	 	 	 dd	Zd
 Z	ddZ
dddZd Zy)ExportDiagnosticEngineaF  PyTorch ONNX Export diagnostic engine.

    The only purpose of creating this class instead of using `DiagnosticContext` directly
    is to provide a background context for `diagnose` calls inside exporter.

    By design, one `torch.onnx.export` call should initialize one diagnostic context.
    All `diagnose` calls inside exporter should be made in the context of that export.
    However, since diagnostic context is currently being accessed via a global variable,
    there is no guarantee that the context is properly initialized. Therefore, we need
    to provide a default background context to fallback to, otherwise any invocation of
    exporter internals, e.g. unit tests, will fail due to missing diagnostic context.
    This can be removed once the pipeline for context to flow through the exporter is
    established.
    zlist[infra.DiagnosticContext]contextsinfra.DiagnosticContext_background_contextc                f    g | _         t        j                  dt        j                        | _        y )Nz
torch.onnx)namer   )rB   r   DiagnosticContexttorch__version__rD   r-   s    r    r*   zExportDiagnosticEngine.__init__d   s(    #(#:#:%%$
 r1   c                    | j                   S N)rD   rJ   s    r    background_contextz)ExportDiagnosticEngine.background_contextk   s    '''r1   Nc                    |t        j                         }t        j                  |||      }| j                  j	                  |       |S )a2  Creates a new diagnostic context.

        Args:
            name: The subject name for the diagnostic context.
            version: The subject version for the diagnostic context.
            options: The options for the diagnostic context.

        Returns:
            A new diagnostic context.
        )r   DiagnosticOptionsrG   rB   r   )r-   rF   r   optionscontexts        r    create_diagnostic_contextz0ExportDiagnosticEngine.create_diagnostic_contexto   sG      ?--/G=B=T=T'7>
 	W%r1   c                    | j                   j                          | j                  j                  j                          y)zClears all diagnostic contexts.N)rB   clearrD   diagnosticsrJ   s    r    rT   zExportDiagnosticEngine.clear   s*      ,,224r1   c                H    t        j                  | j                               S rL   )r   sarif_to_json	sarif_logrJ   s    r    to_jsonzExportDiagnosticEngine.to_json   s    &&t~~'788r1   c                   |r?t        j                  |d      5 }|j                  | j                                ddd       yt        |d      5 }|j                  | j                                ddd       y# 1 sw Y   yxY w# 1 sw Y   yxY w)zDumps the SARIF log to a file.wtNw)gzipopenwriterY   )r-   	file_pathcompressfs       r    dumpzExportDiagnosticEngine.dump   s{    9d+ (q'( ( i% ('( (( (( (s    A7 B7B Bc           	     *   t        j                  t        j                  t        j                  | j
                  D cg c]  }|j                          c}      }|j                  j                  | j                  j                                |S c c}w )N)r   
schema_uriruns)	r   SarifLogsarif_versionSARIF_VERSIONSARIF_SCHEMA_LINKrB   rf   r   rD   )r-   rQ   logs      r    rX   z ExportDiagnosticEngine.sarif_log   sf    nn!//$6615?g'--/?
 	006689
	 @s   B)r7   r8   r7   rC   rL   )rF   strr   rm   rP   zinfra.DiagnosticOptions | Noner7   rC   )r7   rm   )F)r`   rm   ra   r6   r7   r8   )r:   r;   r<   r=   r>   r*   propertyrM   rR   rT   rY   rc   rX    r1   r    rA   rA   Q   sr     ,+00
 ( ( 37	  0	
 
!05
9(r1   rA   c               #     K   t         t        j                  k(  sJ d       t        j                  dt        j
                        a 	 t          t        j                  a y# t        j                  a w xY ww)zCreate a diagnostic context for export.

    This is a workaround for code robustness since diagnostic context is accessed by
    export internals via global variable. See `ExportDiagnosticEngine` for more details.
    z>Export context is already set. Nested export is not supported.ztorch.onnx.exportN)_contextenginerM   rR   rH   rI   ro   r1   r     create_export_diagnostic_contextrs      sd      v000 H0 //H-,,6,,s   AA4A A4A11A4c                X    t        | ||fd|i|}t               j                  |       |S )zCreates a diagnostic and record it in the global diagnostic context.

    This is a wrapper around `context.log` that uses the global diagnostic
    context.
    r   )r#   export_contextrk   )rulelevelr   r   r/   
diagnostics         r    diagnosery      s>     1eW-;?EJ $r1   c                     t         S rL   )rq   ro   r1   r    ru   ru      s    Or1   )r       )r   r5   r   r5   r7   r9   )r7   z.Generator[infra.DiagnosticContext, None, None])Nr   )
rv   z
infra.Rulerw   zinfra.Levelr   z
str | Noner   r5   r7   r#   rl   )r=   
__future__r   
contextlibr]   typingr   rH    torch.onnx._internal.diagnosticsr   &torch.onnx._internal.diagnostics.infrar   r   ,torch.onnx._internal.diagnostics.infra.sarifr   rh   torch.utilsr	   collections.abcr
   r!   
Diagnosticr#   rA   rr   rM   rq   contextmanagerrs   ry   ru   ro   r1   r    <module>r      s    X "      2 C Q % )2 e&6&6  FO Od 
 	!$$ - -2 	
  	 %&r1   