U
    <cG                     @   s   d Z ddlZddlmZmZmZmZ ddlZddlm	Z	m
Z
 edddZG dd de
jZG d	d
 d
e
jZG dd de
jZe Zejaejdd ZdS )z.Diagnostic components for PyTorch ONNX export.    N)AnyOptionalTupleTypeVar)_rulesinfra_ExportDiagnosticExportDiagnostic)boundc                       s|   e Zd ZdZejejeee	df  dd fddZ
eeddd	Zeedd
dZeedddZeedddZ  ZS )r	   zBase 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.
    .N)rulelevelmessage_argsreturnc                    s   t  j|||f| d S N)super__init__)selfr   r   r   kwargs	__class__ P/tmp/pip-unpacked-wheel-gikjz4vx/torch/onnx/_internal/diagnostics/_diagnostic.pyr      s    zExportDiagnostic.__init__)r   r   c                 C   s
   t  | S r   NotImplementedErrorr   r   r   r   with_cpp_stack   s    zExportDiagnostic.with_cpp_stackc                 C   s
   t  | S r   r   r   r   r   r   with_python_stack$   s    z"ExportDiagnostic.with_python_stackc                 C   s
   t  | S r   r   r   r   r   r   with_model_source_location*   s    z+ExportDiagnostic.with_model_source_locationc                 C   s
   t  | S r   r   r   r   r   r   with_export_source_location2   s    z,ExportDiagnostic.with_export_source_location)__name__
__module____qualname____doc__r   ZRuleZLevelr   r   r   r   r   r   r   r   r   __classcell__r   r   r   r   r	      s   			c                       s&   e Zd ZdZdd fddZ  ZS )ExportDiagnosticToolzBase class for all export diagnostic tools.

    This class is used to represent all export diagnostic tools. It is a subclass
    of infra.DiagnosticTool.
    Nr   c                    s   t  jdtjtjtd d S )Nztorch.onnx.export)nameversionrulesZdiagnostic_type)r   r   torch__version__r   r(   r	   r   r   r   r   r   B   s    zExportDiagnosticTool.__init__)r   r    r!   r"   r   r#   r   r   r   r   r$   ;   s   r$   c                       s^   e Zd ZU dZejed< dd fddZeejdddZ	 fd	d
Z
 fddZ  ZS )ExportDiagnosticEngineaA  PyTorch ONNX Export diagnostic engine.

    The only purpose of creating this class instead of using the base class 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.
    _background_contextNr%   c                    s    t    tjt d d| _d S )N)options)r   r   r   DiagnosticContextr$   r,   r   r   r   r   r   ]   s
    
 zExportDiagnosticEngine.__init__c                 C   s   | j S r   )r,   r   r   r   r   background_contextc   s    z)ExportDiagnosticEngine.background_contextc                    s   t    | jj  d S r   )r   clearr,   Z_diagnosticsr   r   r   r   r0   g   s    
zExportDiagnosticEngine.clearc                    s    t   }|j| j  |S r   )r   	sarif_logrunsappendr,   Zsarif)r   logr   r   r   r1   k   s    
z ExportDiagnosticEngine.sarif_log)r   r    r!   r"   r   r.   __annotations__r   propertyr/   r0   r1   r#   r   r   r   r   r+   K   s   

r+   c                   c   s$   t t az
tV  W 5 t jaX dS )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.
    N)engineZcreate_diagnostic_contextr$   contextr/   r   r   r   r    create_export_diagnostic_contextu   s    
r9   )r"   
contextlibtypingr   r   r   r   r)   Z torch.onnx._internal.diagnosticsr   r   r   Z
Diagnosticr	   ZDiagnosticToolr$   ZDiagnosticEnginer+   r7   r/   r8   contextmanagerr9   r   r   r   r   <module>   s   .&