
    iD"                         S SK JrJrJrJrJr  S SKrS SKJr  S SK	J
r
  S SKJrJrJr  S SKJr  S SKJr  S SKJr  S S	KJr  S S
KJr   " S S5      r " S S\5      r\ " S S\5      5       rg)    )CallablefinalListOptionalTupleN)OpenvinoBackend)CompileSpec)DelegationSpecPartitionerPartitionResult)tag_constant_data)OperatorSupport)ExportedProgram)CapabilityBasedPartitioner)OperatorSupportBasec                   :    \ rS rSr% 0 r\\\\   4   \	S'   S r
Srg)PatternNode   op_typesc                     0 U l         g )Nr   )selfs    g/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/backends/openvino/partitioner.py__init__PatternNode.__init__    s	        r   N)__name__
__module____qualname____firstlineno__r   dictstrr   list__annotations__r   __static_attributes__ r   r   r   r      s    *,Hd3&',r   r   c            	           \ rS rSrSSS.r   SS\\   S\\   S\\   SS4S jjrS	\R                  R                  S\4S
 jrSrg)OpenvinoOperatorsSupport$   N)z0torch.ops.dim_order_ops._clone_dim_order.defaultz2torch.ops.dim_order_ops._to_dim_order_copy.defaultop_types_to_skipop_names_to_skipenabled_ops_by_namereturnc                 v    Uc
  [        5       nUc
  [        5       nUc
  [        5       nXl        X l        X0l        g)z
Initializes the OpenvinoOperatorsSupport class.

:param op_types_to_skip: A set of operator types to skip during support checking.
:param op_names_to_skip: A set of operator names to skip during support checking.
N)set_op_types_to_skip_op_names_to_skip_enabled_ops_by_name)r   r*   r+   r,   s       r   r   !OpenvinoOperatorsSupport.__init__*   s@     #"u#"u&"%%!1!1$7!r   nodec                 *   UR                   S:w  a  g/ n[        UR                  [        5      (       d  UR                  R                  nO[        UR                  5      nUR
                  U R                  ;   a  g[        U5      R                  U R                  -  nUS:X  a  gS[        U5      -   U;   a  g[        SS[        U5      -   5        X@R                  ;   d  UR
                  U R                  ;   a  [        SU SUR
                   S	35        gg)
z
Checks if a given node is supported by OpenVINO.

:param node: The FX graph node representing an operation.
:return: True if the node is supported, otherwise False.
call_functionFTgetitemz
torch.ops.zOp not supported: z[OpenVINO Backend] The z operator with name 'z' is skipped.)op
isinstancetargetr"   r   namer2   r   _support_dictextended_support_dictprintr0   r1   )r   _r4   optionsop_typesupported_opss         r   is_node_supported*OpenvinoOperatorsSupport.is_node_supportedA   s     77o%$++s++kk**G$++&G99111 G$22T5O5OO 	 i#g,&-7&s7|(CD,,,		T=S=S0S)'2G		{R_` r   )r2   r1   r0   )NNN)r   r   r   r    r=   r   r/   r   torchfxNodeboolrC   r%   r&   r   r   r(   r(   $   sm    <@>B +/*.-1	8"3-8 #3-8 &c]	8
 
8.$ $4 $r   r(   c            	       \   \ rS rSr  SS\\   S\\   S\\   SS4S jjrS\	S\
\\R                  R                     \\\R                  R                   /\4      4   4S	 jrS
\R                  R                   S\\   S\S\4S jrS\R                  R.                  4S jrS\	S\4S jrSrg)OpenvinoPartitionerh   Ncompile_specr*   r+   r-   c                 x    [        [        R                  U5      U l        X l        X0l        [        5       U l        g)a  
Initializes the OpenvinoPartitioner class.

:param compile_spec: A list of compile specifications for OpenVINO.
:param op_types_to_skip: A set of operator types to skip during partitioning.
:param op_names_to_skip: A set of operator names to skip during partitioning.
N)r
   r   r   delegation_specr0   r1   r/   r2   )r   rL   r*   r+   s       r   r   OpenvinoPartitioner.__init__k   s/      .o.F.FU!1!1),!r   epc                 *   [         R                  R                  R                  R                  [         R                  R                  R
                  R                  [         R                  R                  R
                  R                  [         R                  R                  R                  R                  [         R                  R                  R                  R                  [         R                  R                  R                  R                  /nUS4$ )z
Returns a tuple containing a list of operations that should not be decomposed
and an optional function to filter nodes.

:param ep: The exported program.
:return: A tuple consisting of a list of ops to keep and an optional filtering function.
N)	rE   opsatenpixel_shuffledefaultupsample_bilinear2dvecupsample_nearest2dstack)r   rP   ops_not_decomposes      r   ops_to_not_decompose(OpenvinoPartitioner.ops_to_not_decompose}   s     IINN((00IINN..66IINN..22IINN--55IINN--11IINN  ((
 "4((r   r4   patternenabled_opsc                 x   UR                   S:X  a  S[        UR                  R                  5      -   UR                  ;   a  UR
                  nUR                  S[        UR                  R                  5      -      nUc  UR                  U5        g[        U5      [        U5      :w  a  g[        [        U5      5       H!  nU R                  XF   XV   U5      (       a  M!    g   UR                  U5        g gUR                   S:X  a  SUR                  ;   a  ggUR                   S:X  a  SUR                  ;   a  ggg)Nr6   zcall_function:TFget_attrplaceholder)
r8   r"   r:   r   r   all_input_nodesappendlenrangecheck_pattern)r   r4   r]   r^   pt_input_nodespattern_input_opsis          r   rf   !OpenvinoPartitioner.check_pattern   s1    77o%%DKK,@,@(AAgFVFVV!%!5!5$+$4$4)C0D0D,EE%! %,&&t,~&#.?*@@ s>23A--&)+<+?   %	 4
 ""4(! W6  WW
"W---WW% 0 00r   graph_modulec                 "   [         nS UR                  S'   S UR                  S'   [         nU/UR                  S'   [         nU/UR                  S'   [         nUU/UR                  S'   UR                  R                   H  n[	        UR
                  5      S:X  d  M  [	        UR                  R                  5      S:X  d  MC  / nU R                  XeU5      nU(       d  M`  U H(  n	U R                  R                  U	R                  5        M*     M     g )Nr`   ra   z4call_function:aten.bitwise_right_shift.Tensor_Scalarz%call_function:aten.bitwise_and.Scalarz call_function:aten.stack.defaultr6   zaten.stack.default)r   r   graphnodesr"   r8   r:   r   rf   r2   addr;   )
r   rk   
const_nodebitwise_right_shift_nodebitwise_and_node
stack_noder4   r^   pattern_match
pattern_ops
             r   capture_nncf_patterns)OpenvinoPartitioner.capture_nncf_patterns   s    
*.
J'-1
M*#.  L 	!))B	
 'N
!!"IJ !
$C

>?
 !&&,,DDGG/,,-1EE$& $ 2 24[ Q =&1
1155jooF '2 -r   exported_programc                    U R                  UR                  5        [        UR                  [        U R                  U R
                  U R                  5      SS9nUR                  5       n0 nU HA  nUR                   H.  nSUR                   3nXvR                  S'   U R                  XG'   M0     MC     [        U5        [        XS9$ )z
Partitions an exported program into supported and unsupported segments.

:param exported_program: The exported program.
:return: A PartitionResult containing the partitioned graph and delegation tags.
T)allows_single_node_partitiontagdelegation_tag)tagged_exported_programpartition_tags)rv   rk   r   r(   r0   r1   r2   propose_partitionsrn   idmetarN   r   r   )r   rx   partitionerpartition_listr~   	partitionr4   r{   s           r   r   OpenvinoPartitioner.partition   s     	""#3#@#@A0))$&&&&))
 *.
 %779'I!ILL>*.1		*+&*&:&:# ( ( 	*+$4
 	
r   )r2   r1   r0   rN   )NN)r   r   r   r    r   r	   r   r/   r   r   r   rE   _ops
OpOverloadr   rF   rG   rH   r[   typer   r#   rf   GraphModulerv   r   r   r%   r&   r   r   rJ   rJ   h   s     +/*.	/;'/ #3-/ #3-	/
 
/$)) 
tEJJ))*HXuxx}}ot>S5T,UU	V)*HHMM,0,=LP	BG%((2F2F G:
/ 
o 
r   rJ   )typingr   r   r   r   r   rE   'executorch.backends.openvino.preprocessr   'executorch.exir.backend.backend_detailsr	   #executorch.exir.backend.partitionerr
   r   r   executorch.exir.backend.utilsr   0openvino.frontend.pytorch.torchdynamo.op_supportr   torch.export.exported_programr   !torch.fx.passes.infra.partitionerr    torch.fx.passes.operator_supportr   r   r(   rJ   r&   r   r   <module>r      sm    : 9  C ? 
 < : H @ A2 AH E
+ E
 E
r   