
    pi                     X   d dl Z d dlZd dlmZ d dlmZmZ d dlZd dlm	c m
Z d dlmZmZmZ d dlmZ d dlmZmZ d dlmZmZ d dlmZmZ  G d	 d
ej6                  j8                        Z edddg      Zdej6                  j8                  dedej>                  j@                  f   fdZ!y)    N)
namedtuple)AnyCallable)	FrameInfofullgraph_captureget_compile_id)argument_names)dynamo_timedget_metrics_context)compile_contextCompileContext)_PyTreeCodeGen_PyTreeInfoc                   <     e Zd Zdededdf fdZdeddfdZ xZS )	ModuleToTracefooin_specreturnNc                 >    t         |           || _        || _        y N)super__init___export_rootr   )selfr   r   	__class__s      a/opt/services/ai/voice_agent/venv/lib/python3.12/site-packages/torch/_dynamo/functional_export.pyr   zModuleToTrace.__init__   s        	flat_argsExportTracerOutputc                     t        j                  || j                        \  }} | j                  |i |}t        j                  |      \  }}t        ||      S r   )pytreetree_unflattenr   r   tree_flattenr   )r   r   argskwargsresout_flatout_specs          r   forwardzModuleToTrace.forward   sU    ,,YEfd00#005(!(H55r   )__name__
__module____qualname__r   r   r)   __classcell__)r   s   @r   r   r      s1    C # $ 
6# 6*> 6r   r   r   r   r(   modr   .c                 `     dt         dt         dt        j                  j                  f fd}|S )z
    This is lower level API that is used for export to capture dynamo level
    torch IR.

    Notable TODOs:
    1. Are we actually gonna run the bytecode?
    2. Need to attach guards
    r$   r%   r   c            
      d	   t        j                  | |f      \  }}t        "|      }t        j                  |j
                        } |j                  | }|j                          d|i|j                  }t        |j
                  j                  j                  |j
                  j                  j                  |t        d      }t        j                  j                   j#                  dd      }	t%        t'        t)        i                   5  t+               5  t-        d      5  |	5  t/        |d      }
|
j0                  j2                  j4                  J |
j0                  j2                  j4                  j6                  }|j8                  }|j:                  }|j<                  }|
j>                  j@                  }i }|D ]O  }||   }tC        |t        j                  jD                  jF                        sJ tI        |      ||jJ                  <   Q tM        |jN                  jP                        D cg c]  }|jR                  dk(  s| }}tU        d	 tM        |jN                  jP                        D              }tI        |      d
kD  r|d
   n|}i }|jN                  jW                  |      5  tY        tI        |            D ]V  }|jN                  j[                  d|       }||v r-|||       |||      j\                  j_                         |_.        |||<   X 	 d d d        g }|D ]l  }||   \  }}|dk(  r!|ja                  |jb                  d
   |          |dk(  r |jJ                  }|ja                  ||          |dk(  s\|ja                  |       n te        |      f|_1        |jg                         D ];  \  }}||   }||   } |ji                  |        |jN                  jk                  |       = d
dl6m7}! |!jq                  |       d d d        d d d        d d d        d d d        ts        tu        tw        || |      |            jN                  _<        |j{                          |S c c}w # 1 sw Y   ZxY w# 1 sw Y   uxY w# 1 sw Y   yxY w# 1 sw Y   }xY w# 1 sw Y   xY w)Nr    )closurelog_graph_in_out_metadataTr   ) _is_export_deprecated_do_not_useplaceholderc              3   @   K   | ]  }|j                   d k(  s|  yw)outputN)op).0ns     r   	<genexpr>zB_dynamo_graph_capture_for_export.<locals>.inner.<locals>.<genexpr>_   s     Q@P!Qs   r   arg_	graph_outinputconstant)_LazyGraphModule)>r!   r#   r   inspect	signaturer)   bindapply_defaults	argumentsr   __func____code____globals__builtinstorch_dynamoconfigpatchr   r   r   r   r
   r   dynamo_outputtracer_outputoutput_graphexport_metadatagraph_input_idx_to_local_sourceoutput_return_typer(   backend_inputgraph_module
isinstancesourceGetItemSourcelenindexlistgraphnodesr8   nextinserting_beforeranger5   metacopyappendr$   tupleitemsreplace_all_uses_with
erase_nodetorch.fx._lazy_graph_moduler@   force_recompiler   r   r	   _codegen	recompile)#r$   r%   flat_inputsr   module_to_tracerB   bound_argumentsf_localsframedynamo_config_ctxoutrQ   graph_inputsrS   r(   r\   graph_input_orderinprW   r:   placeholdersr7   anchorinp_to_nodeinode_newnew_argstypeval	input_idxsrc_idxoldnewr@   r.   s#                                     r   innerz/_dynamo_graph_capture_for_export.<locals>.inner+   sa   %22D&>BW'W5%%o&=&=>	()..+6&&(OI/H/HI##,,55##,,88
 "MM0066'

 N>"+=>??	4!?	4 ,-?	4 	?	4 $EDQC$$22??KKK !!//<<LL  +JJL!0!C!C&//H%%22E 13# I%c*!&%--*>*>*L*LMMM256G2H!&,,/I
 (,EKK,=,='>X!!$$-BWAXLXQT%++*;*;%<QQF(+L(9A(=\!_6FK--f5 .s;/0 .A${{66aSzBH--$%6q%9:(45Fq5I(J(O(O(T(T(V%-KN.. H' ).q1	c;&OOFKKN3$787? #		IOOK	$:;:%OOC() !?,FK/557 ,
"7+!!n))#.&&s+	, E,,U3?	4 ?	4 ?	4 ?	4B  .y$7 
 	] Y. .E?	4 ?	4 ?	4 ?	4 ?	4 ?	4 ?	4 ?	4s   R&R$R'DR	2Q0Q0A#R	.A.Q5A'R	B	R	RRR&0R	5Q?:R	RRRRR#	R&&R/)r   rJ   fxGraphModule)r.   r   s   ` r    _dynamo_graph_capture_for_exportr      s3    aS aC aEHH,@,@ aF Lr   )"rI   rA   collectionsr   typingr   r   rJ   torch.utils._pytreeutils_pytreer!   torch._dynamo.convert_framer   r   r   torch._dynamo.eval_framer	   torch._dynamo.utilsr
   r   torch._guardsr   r   torch.fx.graphr   r   nnModuler   r   r   r   r   r1   r   r   <module>r      s      "    $ $ T T 3 A 9 6
6EHHOO 
6   4{J6OP o	oc588'''(or   