
    i+                         S SK r S SKJs  Js  Jr  S SKrS SKJrJr  S SK	J
r  S SKJrJr  S SKJrJrJrJr  S SKJr  S SKJr  S SKJr   " S	 S
\R4                  R6                  5      r " S S\ R:                  5      rg)    N)EdgeCompileConfigto_edge)ops)create_submodule_from_nodesLoweredBackendModule)BackendDelegateBackendDelegateDataReferenceDataLocationDelegateCall!register_additional_test_aten_ops)export)	FileCheckc                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )WrapperModule   c                 .   > [         TU ]  5         Xl        g N)super__init__fn)selfr   	__class__s     b/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/exir/tests/test_delegate.pyr   WrapperModule.__init__   s        c                 &    U R                   " U0 UD6$ r   r   )r   argskwargss      r   forwardWrapperModule.forward"   s    ww'''r   r   __name__
__module____qualname____firstlineno__r   r!   __static_attributes____classcell__r   s   @r   r   r      s    ( (r   r   c                   d    \ rS rSr\SS j5       rSS jrSS jrSS jrSS jr	SS jr
SS	 jrS
rg)TestDelegate&   Nc                     [        5         g r   r   )clss    r   
setUpClassTestDelegate.setUpClass'   s    )+r   c                   ^ S[         R                  S[         R                  S[         R                  4S jn[         R                  " SS5      [         R                  " SS5      4n[        [	        [        U5      USS95      n[        UR                  5       S	S
/ 5      mS[         R                  S[         R                  S[         R                  4U4S jjnU" U6 n[	        [        U5      USS9n[        5       R                  S5      R                  S5      R                  UR                  R                  5        U R                  [         R                  " XVR                  5       " U6 5      5        g )Nxyreturnc                 
    X-   $ r    )r3   r4   s     r   g*TestDelegate.test_call_delegate.<locals>.g,   s	    5Lr         TstrictBackendWithCompilerDemos   mooc                 X   > [         R                  R                  R                  TX5      $ r   )torchr   higher_orderexecutorch_call_delegate)r3   r4   lowered_modules     r   f*TestDelegate.test_call_delegate.<locals>.f5   s!    99))BB>STXXr   lowered_module_0/torch.ops.higher_order.executorch_call_delegate)r@   Tensoronesr   r   r   r   exported_programr   checkrungraph_modulecode
assertTrueallclosemodule)r   r8   inputs	edge_ir_mrD   orig_resgmrC   s          @r   test_call_delegateTestDelegate.test_call_delegate+   s   	 	%,, 	5<< 	 **Q"EJJq!$45F=#3VDIJ	/C&&(*CVR0
	Y 	Y%,, 	Y5<< 	Y f:M!$fT:,-33=	

#boo""
#xf1EFGr   c                     [         R                  " 5       n[        [        XR	                  5       SS9[        SS9S9R                  5       nUR                  5       R                  nUR                  R                  n[        5       R                  S5      R                  S5      R                  UR                  5        U R                  [         R"                  R$                  R&                  R(                  UR*                  R,                   Vs1 s H  oUR.                  iM     sn;  5        UR*                  R,                   GH%  nUR0                  S:X  d  M  UR.                  [2        R4                  R6                  R8                  :X  d  MJ  U R;                  UR<                  S	   R0                  S
5        [?        X5R<                  S	   R.                  5      nU R;                  UR@                  URB                  R@                  5        U R;                  URD                  URB                  RD                  5        U R;                  URF                  URB                  RF                  5        GM(     URH                  S	   RJ                  S	   nU R;                  URL                  S5        URN                  nU R;                  URP                  [R        RT                  5        U RW                  URX                  [[        UR\                  5      5        U R;                  UR\                  URX                     R^                  S5        U R                  [a        URH                  S	   Rb                  S	   Rd                  S	   Rf                  [h        5      5        gs  snf )zDCheck if we have patched a lowered module correctly (for delegation)Tr<   F)_check_ir_validity)compile_configrF   rG   call_functionr   get_attrbackend_demos   basic_module_addN)5modelsCompositeDelegateModuler   r   get_random_inputsr   to_executorchrJ   rM   _emitter_outputprogramr   rK   rL   rN   rO   exir_opsedgeatenadddefaultgraphnodestargetopr@   r   rA   rB   assertEqualr   getattr_backend_idrC   _processed_bytes_compile_specsexecution_plan	delegatesid	processedlocationr
   INLINE
assertLessindexlenbackend_delegate_datadata
isinstancechainsinstructions
instr_argsr   )	r   m	exec_progrM   rc   nodeget_attr_backenddelegateru   s	            r   test_to_backendTestDelegate.test_to_backend?   s    **,1))+D9,F
 -/ 	 !113@@++33 	,-33=	

#l
  	MM""**,8,>,>,D,DE,DDKK,DEF	

 !&&,,D?*KK599#9#9#R#RR   1*=#*<19L9L#M   $00!2B2B2N2N   $55q7G7G7X7X   $33Q5E5E5T5T -( %,$:$:1$=$G$G$Jn52:2D2D	++\-@-@A	W-J-J)KL)))//:??AT	

 	&&q)003@@CNN	
C Fs   N
c                     [        S SS/ 5      nU R                  [        5         SUl        S S S 5        g ! , (       d  f       g = f)N r   123)r   assertRaisesAttributeError
backend_id)r   delegs     r   test_cannot_assign_attr$TestDelegate.test_cannot_assign_attr}   s4    $T2sB7~.$E /..s	   5
Ac                    [         R                  " SS5      [         R                  " SS5      4n " S S[         R                  R                  5      nU" 5       " U6 n[	        [        U" 5       USS95      nUR                  5       R                  n/ nUR                  R                   H  nUR                  S:X  d  M  UR                  [        R                  R                  R                  R                   [        R                  R                  R"                  R                   1;   d  M  UR%                  U5        M     ['        XVS5      u  pUR)                  5         UR)                  5         UR                  R                   H  nUR                  S	:X  d  M  U R+                  [-        UR.                  5      S5        U R1                  [3        UR.                  S
   [4        5      5        U R+                  [-        UR.                  S
   5      S5        M     UR                  5       R7                  5       " U6 n	U R1                  [         R8                  " X5      5        g)a  
Original graph:
    add_tensor = add(x, y)
    mul_tensor = mul(add_tensor, y)
    sub_tensor = sub(mul_tensor, y)
    div_tensor = div(sub_tensor, y)
    return [div_tensor]

Partitioned graph:
    add_tensor = add(x, y)
    mul_tensor = mul(add_tensor, y)
    return [mul_tensor]  # Output is pytree.flatten-ed

Final graph:
    partitioned_res = partitioned_graph(x, y)
    getitem_0 = partitioned_res[0]
    sub_tensor = sub(getitem_0, y)
    div_tensor = div(sub_tensor, y)
    return [div_tensor]
r:   r;   c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )?TestDelegate.test_create_submodule_single_return.<locals>.Model   c                 "   > [         TU ]  5         g r   r   r   r   r   s    r   r   HTestDelegate.test_create_submodule_single_return.<locals>.Model.__init__        "r   c                 &    X-   nX-  nX-
  nX-  nU$ r   r7   r   r3   r4   s      r   r!   GTestDelegate.test_create_submodule_single_return.<locals>.Model.forward   #    EEEEr   r7   r#   r*   s   @r   Modelr          # r   r   Tr<   r[   tagoutputr   Nr@   randnnnModuler   r   rJ   rM   ri   rj   rl   rk   rd   re   rf   rg   rH   mulappendr   	recompilerm   rz   r   rO   r}   listrQ   rP   
r   rR   r   rT   progrU   	node_listr   sub_gmnew_ress
             r   #test_create_submodule_single_return0TestDelegate.test_create_submodule_single_return   s   * ++a#U[[A%67		EHHOO 		 7F#vegvd;<""$11	HHNNDww/)dkk""&&--""&&--> /   & # 32%H
LL&&Dww("  TYY3
499Q< >?  TYYq\!2A6	 ' '')002F;w9:r   c                    [         R                  " SS5      [         R                  " SS5      4n " S S[         R                  R                  5      nU" 5       " U6 n[	        [        U" 5       USS95      nUR                  5       R                  n/ nUR                  R                   H  nUR                  S:X  d  M  UR                  [        R                  R                  R                  R                   [        R                  R                  R"                  R                   1;   d  M  UR%                  U5        M     ['        XVS5      u  pUR)                  5         UR)                  5         UR                  R                   H  nUR                  S	:X  d  M  U R+                  [-        UR.                  5      S5        U R1                  [3        UR.                  S
   [4        5      5        U R+                  [-        UR.                  S
   5      S5        M     UR                  5       R7                  5       " U6 n	U R1                  [         R8                  " X5      5        g)a*  
Original graph:
    add_tensor = add(x, y)
    mul_tensor = mul(add_tensor, y)
    sub_tensor = sub(add_tensor, mul_tensor)
    div_tensor = div(sub_tensor, mul_tensor)
    return [div_tensor]

Partitioned graph:
    add_tensor = add(x, y)
    mul_tensor = mul(add_tensor, y)
    return [add_tensor, mul_tensor]

Final graph:
    partitioned_res = partitioned_graph(x, y)
    getitem_0 = partitioned_res[0]
    getitem_1 = partitioned_res[1]
    sub_tensor = sub(getitem_0, getitem_1)
    div_tensor = div(sub_tensor, getitem_1)
    return [div_tensor]
r:   r;   c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )ATestDelegate.test_create_submodule_multiple_return.<locals>.Model   c                 "   > [         TU ]  5         g r   r   r   s    r   r   JTestDelegate.test_create_submodule_multiple_return.<locals>.Model.__init__   r   r   c                 &    X-   nX-  nX-
  nX-  nU$ r   r7   r   s      r   r!   ITestDelegate.test_create_submodule_multiple_return.<locals>.Model.forward   r   r   r7   r#   r*   s   @r   r   r      r   r   r   Tr<   r[   r   r   r      Nr   r   s
             r   %test_create_submodule_multiple_return2TestDelegate.test_create_submodule_multiple_return   s   , ++a#U[[A%67		EHHOO 		 7F#vegvd;<""$11	HHNNDww/)dkk""&&--""&&--> /   & # 32%H
LL&&Dww("  TYY3
499Q< >?  TYYq\!2A6	 ' '')002F;w9:r   c                    [         R                  " S5      [         R                  " S5      4n " S S[         R                  R                  5      nU" 5       " U6 n[	        [        U" 5       USS95      nUR                  5       R                  n/ nUR                  R                   H  nUR                  S:X  d  M  UR                  [        R                  R                  R                  R                   :X  d>  UR                  [        R                  R                  R"                  R$                  :X  d  M  UR'                  U5        M     [)        XVS5      u  pUR                  R                   H  nUR                  S	:X  d  M  U R+                  [-        UR.                  5      S
5        U R1                  [3        UR.                  S   [4        5      5        U R+                  [-        UR.                  S   5      S5        M     UR                  5       R7                  5       " U6 n	U R1                  [         R8                  " X5      5        g)a  
Original graph:
    split_tensor = split(x, 5)
    getitem_0 = split_tensor[0]
    sub_tensor = sub(getitem_0, y)
    div_tensor = div(sub_tensor, y)
    return [div_tensor]

Partitioned graph:
    split_tensor = split(x, 5)
    getitem_0 = split_tensor[0]
    getitem_1 = split_tensor[1]
    return [getitem_0, getitem_1]  # List output is "opened"

Final graph:
    partitioned_res = partitioned_graph(x, y)
    getitem_0 = partitioned_res[0]
    sub_tensor = sub(getitem_0, y)
    div_tensor = div(sub_tensor, y)
    return [div_tensor]

      c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )=TestDelegate.test_create_submodule_list_return.<locals>.Modeli  c                 "   > [         TU ]  5         g r   r   r   s    r   r   FTestDelegate.test_create_submodule_list_return.<locals>.Model.__init__  r   r   c                 L    [         R                  " US5      nUS   U-
  nX-  nU$ )Nr   r   )r@   splitr   s      r   r!   ETestDelegate.test_create_submodule_list_return.<locals>.Model.forward  s+    KK1%aD1HEr   r7   r#   r*   s   @r   r   r     s    # r   r   Tr<   r[   r   r   r:   r   r   N)r@   r   r   r   r   r   rJ   rM   ri   rj   rl   rk   rd   re   rf   split_with_sizes_copyrh   
split_copyrH   r   r   rm   rz   r   rO   r}   r   rQ   rP   r   s
             r   !test_create_submodule_list_return.TestDelegate.test_create_submodule_list_return   s   , ++b/5;;q>2	EHHOO 	 7F#vegvd;<""$11	HHNND
 ww/)x}}11GGOOO;;(--"4"4"?"?"F"FF  & # 32%HLL&&Dww("  TYY3
499Q< >?  TYYq\!2A6	 ' '')002F;w9:r   r7   )r5   N)r$   r%   r&   r'   classmethodr0   rV   r   r   r   r   r   r(   r7   r   r   r,   r,   &   s6    , ,H(<
|%
9;v:;x;;r   r,   )unittestexecutorch.exir.tests.modelsexirtestsr^   r@   executorch.exirr   r   executorch.exir.dialects._opsr   rd   &executorch.exir.lowered_backend_moduler   r   executorch.exir.schemar   r	   r
   r   executorch.exir.tests.commonr   torch.exportr   torch.testingr   r   r   r   TestCaser,   r7   r   r   <module>r      s\     - -  6 9  K  #(EHHOO (N;8$$ N;r   