
    x-jc                        d dl Z d dlZd dlZd dlZd dlZd dlmc mZ	 d dl
mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ dd	lmZ dd
lmZmZmZmZ g Z G d d          ZddZddZd Z d Z!d Z"d Z#d Z$d Z%d Z&dS )    N)core)convert_dtype)switch_to_static_graph)PirTranslatedLayer)TranslatedLayer)layers   )logging_utils)func_to_source_codeparse_arg_and_kwargsparse_varargs_name	type_namec                       e Zd ZdZddZd Zd Zed             Zed             Z	d Z
d	 Zed
             Zed             Zed             Zed             Zed             Zed             ZdS )FunctionSpecz8
    Wrapper class for a function for class method.
    Nc                 H   t          j        |          r+|j        | _        t	          j        |j                  | _        n|| _        d | _        |d | _        d | _	        nC| 
                    |          | _        t          j                            | j                  | _	        t          |          \  | _        | _        t#          |          | _        | j        Gt'          t)          |dd           t*          t,          f          r| xj        |j        j        z  c_        d S d S d S )N__self__)inspectismethod__func___dygraph_functionweakrefrefr   _class_instance_input_spec_flat_input_spec_verify_input_specpaddleutilsflattenr   
_arg_names_default_kwargsr   varargs_name
isinstancegetattrr   r   _input_args_names)selffunction
input_specs      b/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/jit/dy2static/function_spec.py__init__zFunctionSpec.__init__.   s   H%% 	(%-%6D"#*;x/@#A#AD  %-D"#'D #D$(D!!#66zBBD$*L$8$89I$J$JD! 1EX0N0N--.x88(ZHj$//01.
 .
( OOx0BBOOOO	 )(((    c                    t          | j                  t          |          k     rd| j        j         dt          | j                   d| j         dt          |           d| d}|r.t	          j        |d                   r|dz  }t          |          t          |          t          |          }t          t          |          t          | j                            D ]}| j        |         }||v r|
                    ||                    ||= 2|| j        vr&t          d	| j        j         d
| d| d| d	          |
                    | j        |                    t          |          |fS )a  
        Moves kwargs with default value into arguments list to keep `args` contain the same length
        value as function definition.

        For example:

            Given function definition: `def foo(x, a=1, b=2)`,
            when calling it by `foo(23)`, the args is `[23]`, kwargs is `{a=1, b=2}`.
            In this function, it will return args with `[23, 1, 2]`, kwargs with `{}`

        Args:
            args(tuple): tuple of input arguments value of decorated function.
            kwargs(dict): dict of input keyword arguments value of decorated function.

        Return:
            New arguments tuple containing default kwargs value.
        zThe decorated function `z` requires z arguments: , but received z with .r   zP
	Maybe the function has more than one decorator, we don't support this for now.`z()` requires `z,` arguments, but not found in input `args`: z and `kwargs`: )lenr    dygraph_function__name__r   isclassNotImplementedError
ValueErrorlistrangeappendr!   tuple)r&   argskwargs	error_msgiarg_names         r)   unified_args_and_kwargsz$FunctionSpec.unified_args_and_kwargsF   s   $ t#d))++ y43H3Q  y  y^abfbq^r^r  y  y  AE  AP  y  y  ad  ei  aj  aj  y  y  rv  y  y  yI ,Q00 ,qq	))444 +++Dzzs4yy#do"6"677 
	< 
	<Aq)H6!!F8,---8$$4#777$ _D1:  _  _(  _  _  AE  _  _  V\  _  _  _   D0:;;;;T{{F""r+   c                 f   g }g }| j         |rt          | j        j         d| d          t	          |          t	          | j                   k     r4t          dt	          |           dt	          | j                              t          || j                   }n}t          || j                  }d |                                D             }t          t          |
                                          |          fdt          |          D             }t          | j        |          }||fS )a  
        Converts input arguments into InputSpec.

        1. If specific input_spec, use them to construct feed layers.
        2. If input_spec is None, consider all Tensor and Numpy.ndarray as feed layers

        Args:
            args(tuple): tuple of input arguments value of function containing default kwargs value.
            kwargs(dict): kwargs arguments received by **kwargs.

        Return:
            Same nest structure with args and kwargs by replacing value with InputSpec.
        Nz# got unexpected keyword arguments: z;. Cannot trace the function when `input_spec` is specified.zCRequires len(arguments) >= len(input_spec), but received len(args):z < len(InputSpec): c                     g | ]}d |z   S )zkwargs. ).0keys     r)   
<listcomp>z3FunctionSpec.args_to_input_spec.<locals>.<listcomp>   s    DDDs9s?DDDr+   c                 (    i | ]\  }}||         S rB   rB   )rC   idxrD   kwargs_list_with_specs      r)   
<dictcomp>z3FunctionSpec.args_to_input_spec.<locals>.<dictcomp>   s5          C *3/     r+   )r   r5   r1   r2   r0   convert_to_input_spec$_replace_to_input_spec_with_new_namer    keysr6   values	enumeratereplace_spec_empty_name)r&   r:   r;   args_with_speckwargs_with_speckwarg_namesrH   s         @r)   args_to_input_speczFunctionSpec.args_to_input_specp   s    '   ,5  ^  ^Z`  ^  ^  ^   4yy3t/0000  PZ]^bZcZc  P  Px{  }A  }M  yN  yN  P  P  
 349IJJNNAdo N EDfkkmmDDDK$HV]]__%%{% %!        )& 1 1      1O^
 
 ///r+   c           	         ddl m} t          j                            |          }g }g }t          j        |          5  t          |          D ]*\  }}t          |t          j	        j
                  rt          |dd          }	t          j	        j                            |j        pd| |j        t!          |j                            }
|	|
_        ddlm} t          ||          rM ||j        |          }t          j                            |
|j        |          }|                    |           n-|                    |
           n|}
|                    |
           |                    |
           ,	 ddd           n# 1 swxY w Y   t          j                            ||          t          j                            ||          fS )	  
        Constructs feed layer by inputs with InputSpec information for main program.

        Args:
            input_with_spec(tuple): input arguments by replacing argument with InputSpec.
            main_program(Program): main program for inserting feed layer.
        r   to_placementsstop_gradientFfeed_)nameshapedtypeDistributedInputSpecN)/paddle.distributed.auto_parallel.placement_typerW   r   r   r   	ir_staticprogram_guardrN   r#   static	InputSpecr$   inputdatarZ   r[   r   r\   rX   7paddle.distributed.auto_parallel.static.dist_input_specr^   dims_mapping_pir_opsshard_tensormeshr8   pack_sequence_as)r&   input_with_specmain_programrW   flat_input_specfunction_argsprogram_inputsr=   var_specrX   
feed_valuer^   
placementsdist_feed_values                 r)   pir_to_static_inputs_with_specz+FunctionSpec.pir_to_static_inputs_with_spec   sH   	
 	
 	
 	
 	
 	
 !,..?? $\22 	2 	2(99 2 28h(?@@ 5$+Hou$M$MM!'!4!9!9%]9kakk&n+HN;; ": " "J
 0=J,      "(,@AA 	9%2]$18& &
 +1/*F*F&z+ + &,,_====%,,Z8888!)J!((444%%j1111;2	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2@ |,,]
 
<((.IIJ 	Js   D<FFFc           	         t           j                            |          }g }|                                }t	          |          D ]\  }}t          |t           j        j                  rt          |dd          }|	                    |j
        pd| |j        |j        dd|          }	ddlm}
 ddlm} t          ||
          rdd	lm}  |            } ||	          }|j        |j        _        |j        |j        _        |j                            d
           |j                            d           |                    |           n|}	|                    |	           !t           j                            ||          S )rU   rX   FrY   T)rZ   r[   r\   is_dataneed_check_feedrX   r   r]   )DistributedTensor)get_default_distributed_contextprocess_meshrg   )r   r   r   global_blockrN   r#   rb   rc   r$   
create_varrZ   r[   r\   rf   r^   3paddle.distributed.auto_parallel.static.dist_tensorry   4paddle.distributed.auto_parallel.static.dist_contextrz   rj   	dist_attrr{   rg   mark_annotatedadd_dist_tensor_for_programr8   rk   )r&   rl   rm   rn   inputsblockr=   rq   rX   
feed_layerr^   ry   rz   default_dist_ctxdist_tensors                  r)   to_static_inputs_with_specz'FunctionSpec.to_static_inputs_with_spec   s    !,..??))++$_55 #	& #	&KAx(FM$;<<  & '/5 I I"--!5+!++".". $)"/ .  
           h(<== N      (G'F'H'H$"3"3J"?"?K9AK)69A9NK)6)88HHH)88HHH$@@MMM%
MM*%%%%|,,_fEEEr+   c                     t          |t          t          f          s t          dt	          |           d          t          |          S )zJ
        Verifies the `input_spec` and its element type is valid.
        zBThe type(input_spec) should be one of (tuple, list), but received r.   )r#   r9   r6   	TypeErrorr   )r&   r(   s     r)   r   zFunctionSpec._verify_input_spec  sT     *udm44 	mU^_iUjUjmmm   Z   r+   c                 ~    d                     | j        j        d                    | j                  | j                  S )Nz function: {}({}), input_spec: {},)formatr1   r2   joinr    r   r&   s    r)   __repr__zFunctionSpec.__repr__  s:    188!*HHT_%%
 
 	
r+   c                     | j         d S |                                  t          d          |                                  S )NzFThe instance of class has been deleted, please re-create the instance.)r   RuntimeErrorr   s    r)   class_instancezFunctionSpec.class_instance$  sK    '4!!)X   ##%%%r+   c                 \    | j         | j                            | j                   S | j        S N)r   r   __get__r   s    r)   r1   zFunctionSpec.dygraph_function.  s.    *)11$2EFFF))r+   c                     | j         S r   )r    r   s    r)   	args_namezFunctionSpec.args_name5  s
    r+   c                     | j         S r   )r   r   s    r)   r(   zFunctionSpec.input_spec9  s    r+   c                     | j         S r   )r   r   s    r)   rn   zFunctionSpec.flat_input_spec=  s    $$r+   c                 *    t          | j                  S r   )r   r1   r   s    r)   codezFunctionSpec.codeA  s    "4#8999r+   r   )r2   
__module____qualname____doc__r*   r?   rS   r   ru   r   r   r   propertyr   r1   r   r(   rn   r   rB   r+   r)   r   r   )   sV        C C C C0(# (# (#T60 60 60p 3J 3J 3Jj 1F 1F 1Ff	! 	! 	!
 
 
 & & X& * * X*   X     X  % % X% : : X: : :r+   r   Tc                 <   t          j                    }| t          | t          j                  rM|rC|                                 }d |D             }t          j        t          ||                    }n'| j        }nt          dt          |                      |S )z
    Returns parameters of decorated layers. If set `include_sublayer` True,
    the parameters created in sub layers will be added.
    Nc                     g | ]	}|j         
S rB   rZ   )rC   ps     r)   rE   z"get_parameters.<locals>.<listcomp>P  s    000A000r+   :Type of `layer_instance` should be nn.Layer, but received )
collectionsOrderedDictr#   r   Layer
parameterszip_parametersr   r   )layer_instanceinclude_sublayerparamsnamess       r)   get_parametersr   F  s    
 $&&F!nfl33 
	 4'224400000$0UF1C1CDD'3hYWeMfMfhh   Mr+   c                 <   t          j                    }| t          | t          j                  rM|rC|                                 }d |D             }t          j        t          ||                    }n'| j        }nt          dt          |                      |S )z
    Returns Variable buffers of decorated layers. If set `include_sublayer` True,
    the Variable buffers created in sub layers will be added.
    Nc                     g | ]	}|j         
S rB   r   )rC   buffers     r)   rE   zget_buffers.<locals>.<listcomp>f  s    ;;;;;;r+   r   )
r   r   r#   r   r   buffersr   _buffersr   r   )r   r   r   r   s       r)   get_buffersr   \  s    
 %''G!nfl33 
	 2(0022;;7;;;%1#eW2E2EFF(1hYWeMfMfhh   Nr+   c           	      L   ddl m} ddlm} g }t	          t
          j                            |                     D ]\  }}t          |t          j
                  r-t
          j        j                            |          }d|_        net          |t          j        j                  r]|j        }|                                r|                    |          }n$t
          j        j                            |          }||_        nt          |t
          j        j        j        t
          j        j        f          r|j        }|                                rb|                                j        } ||                                j        |          } ||j        |j        |j        |||j                   }n0t
          j                            |j        |j        |j                  }||_        |!                    |           t
          j        "                    | |          }|S )Nr   rV   r]   T)r\   rZ   rj   rs   local_shape)#r_   rW   rf   r^   rN   r   r   r   r#   npndarrayrb   rc   
from_numpyrX   r   eagerTensoris_distfrom_dtensorfrom_tensorbase	frameworkVariablepirValuer   r{   rg   r[   r\   rZ   _local_shaper8   rk   )	r:   rW   r^   rP   rG   	input_varrX   rj   rs   s	            r)   _replace_value_with_input_specr   q  s-              N#FL$8$8$>$>?? ") ")Yi,, 	4/::9EEI&*I##	4:#455 	4%3M  "" K0==iHH		"M3??	JJ	&3I##-6
8HI
 
 	4 &3M  ""  **,,9*]''))6 
 10O#/") ) 6  		 #M33OY_in 	 '4I#i((((\224HHNr+   c                    t          |           t          |          k    sJ t          t          t          |          dz
                      }g }t          t          | |                    D ]\  }\  }}d}t          t          j                            |                    D ]\  }}	t          |	t          j	                  r,t          j
        j                            |	          }
d|
_        nt          |	t          j        j                  r3|	j        }t          j
        j                            |	          }
||
_        net          |	t          j        j        j                  r?|	j        }t          j
                            |	j        |	j        |	j                  }
||
_        n|	}
t          |	t          j	        t          j        j        t          j        j        j        f          r5dt          |                              |           d| d| |
_        |dz  }|                    |
           t          j                            | |          }|S )Nr	   r   Tz_jst.r.   )r0   strrN   r   r   r   r   r#   r   r   rb   rc   r   rX   r   r   r   r   r   r   r   r[   r\   rZ   zfillr8   rk   )r:   	arg_namesorder_digitrP   orderargname_prefixindexrG   origin_inputr   rX   s               r)   rK   rK     s!   t99I&&&&c#i..1,--..KN%.s4/C/C%D%D - -!![!*6<+?+?+D+D!E!E 	- 	-C,
33 )"M3>>|LL	*.	''L$**;<< ) , :"M3??MM	*7	''L&+*?*HII ) , :"M33 &(:L<M 	 +8	''(	JJ%K)2  	 "_U)9)9+)F)F!^!^!^!^W\!^!^	
!!),,,,7	-8 \224HHNr+   c                 ~    d fd	}t          t          t          f          rg } | d           t                    D ]0\  }}t	           |         |          }|                    |           1t                     t                    k    rj t                    d         D ]R}t          |t          j        j	        t          j        f          r%t          j        dt          |           d           S|                     t                    d                    |S t          t                     rNi } | d                                            D ](\  }}	|v rt	          |	|                   ||<   #|	||<   )|S t          t$          j        j                  r	 t+           g          d         }
t          |
t$          j        j                  st-          d  d	          j        |
_        t1          |
          rj        |
_        nt          j        d
 d|
 d           |
S S )aD  
    Replaces tensor in structured `inputs` by InputSpec in `input_spec`.

    Args:
        inputs(list|dict): nested structure list or dict.
        input_spec(list|dict): same nested structure list or dict as inputs.


    Return:
        Same structure with inputs by replacing the element with specified InputSpec.
    Fc                 J   t          |           t          |          ur0t          dt          |           dt          |            d          |rOt          |           t          |          k     r1t          dt                     dt                               d S d S )Nztype(input) should be r-   r.   zBRequires len(inputs) >= len(input_spec), but received len(inputs):z < len(input_spec):)typer   r0   r5   )rd   speccheck_lengthr(   r   s      r)   check_type_and_lenz1convert_to_input_spec.<locals>.check_type_and_len  s    ;;d4jj((RdRRDKKRRR    	CJJT22 GUXY_U`U`  G  Gux  zD  vE  vE  G  G  	 	22r+   TNzThe inputs contain `z` without specifying InputSpec, its shape and dtype will be treated immutable. Please specific InputSpec information in `@to_static` if you expect them as mutable inputs.r   z1Give input spec into a non-tensorable arguments `z`.z;input spec is not compatible with real inputs. input_spec: z , real_spec:  )F)r#   r9   r6   rN   rJ   r8   r0   r   r   r   r   r   r
   warnr   extenddictitemsr   rb   rc   r   r   rZ   spec_greaterr[   )r   r(   r   rl   r=   r   out_spec
rest_inputrZ   rd   	real_specs   ``         r)   rJ   rJ     s          *udm,, 16:t444 ,, 	- 	-GAt,VAY==H""8,,,, v;;Z(($S__%6%67  
j4:+<bj*IJJ !&vy/D/D v v v   	vc*oo&7&78999	J	%	% 6:t444!<<>> 	. 	.KD%z!!(=:d+) )%% ).%%	J 7	8	8 T2F8<<Q?	)V]%<== 	NFNNN   $	
I.. 	(.IOOtjtthqttt    r+   c                     t          |          }| dt          |                   }t          |          D ] \  }}||         }t          ||          ||<   !|S )ao  
    Adds default name according to argument name from decorated function
    if without specifying InputSpec.name

    The naming rule are as followed:
        1. If InputSpec.name is not None, do nothing.
        2. If each argument `x` corresponds to an InputSpec, using the argument name like `x`
        3. If the arguments `inputs` corresponds to a list(InputSpec), using name like `inputs_0`, `inputs_1`
        4. If the arguments `input_dic` corresponds to a dict(InputSpec), using key as name.

    For example:

        # case 1: foo(x, y)
        foo = to_static(foo, input_spec=[InputSpec([None, 10]), InputSpec([None])])
        print([in_var.name for in_var in foo.inputs])  # [x, y]

        # case 2: foo(inputs) where inputs is a list
        foo = to_static(foo, input_spec=[[InputSpec([None, 10]), InputSpec([None])]])
        print([in_var.name for in_var in foo.inputs])  # [inputs_0, inputs_1]

        # case 3: foo(inputs) where inputs is a dict
        foo = to_static(foo, input_spec=[{'x': InputSpec([None, 10]), 'y': InputSpec([None])}])
        print([in_var.name for in_var in foo.inputs])  # [x, y]
    N)r6   r0   rN   _replace_spec_name)r   rl   candidate_arg_namesr=   r>   r(   s         r)   rO   rO     so    2 ?++O#$:c/&:&:$:; !455 F F8$Q'
/*EEr+   c                    t          |t          j        j                  r|j        | |_        |S t          |t
          t          f          rCg }t          |          D ]/\  }}|  d| }|                    t          ||                     0|S t          |t                    r1i }|                                D ]\  }}t          ||          ||<   |S |S )zL
    Replaces InputSpec.name with given `name` while not specifying it.
    N_)r#   r   rb   rc   rZ   r6   r9   rN   r8   r   r   r   )rZ   r(   processed_specsr=   r   new_namerD   s          r)   r   r   5  s     *fm566 ?""JO	Ju	.	.  ,, 	G 	GGAt}}}}H""#5h#E#EFFFF	J	%	% #))++ 	A 	AIC#5c4#@#@OC  r+   c                    d t           j                            |           D             }|d t           j                            |          D             z  }di cfdfd|D             }t          |          S )a  
    Generator hash spec with args/kwargs InputSpec names.
    Consider the following InputSpecs with same shape/dtype except for name:
      1. [InputSpec([3,3], 'float32', 'x'), InputSpec([3,3], 'float32', 'x')]
      2. [InputSpec([3,3], 'float32', 'x'), InputSpec([3,3], 'float32', 'y')]
    Under @to_static, we should generate two different program not just one, because
    the former has one input ('x'), but the latter has two input ('x', 'y').
    c                 Z    g | ](}t          |t          j        j                  !|j        )S rB   r#   r   rb   rc   rZ   rC   r   s     r)   rE   z$_hash_spec_names.<locals>.<listcomp>U  s?       dFM344	  r+   c                 Z    g | ](}t          |t          j        j                  !|j        )S rB   r   r   s     r)   rE   z$_hash_spec_names.<locals>.<listcomp>Z  s?       dFM344	  r+   r   c                 0    | vr
| <   dz  |          S )Nr	   rB   )rZ   r=   name_idss    r)   to_idxz _hash_spec_names.<locals>.to_idxa  s*    xHTNFA~r+   c                 &    g | ]} |          S rB   rB   )rC   rZ   r   s     r)   rE   z$_hash_spec_names.<locals>.<listcomp>h  s!    111dVVD\\111r+   )r   r   r   r9   )
args_specskwargs_specs
spec_namesvaluer=   r   r   s       @@@r)   _hash_spec_namesr   L  s     L((44  J
   L((66   J
 RKAx      2111j111E<<r+   c                 4    d } || j         |j                   S )Nc                     t          |           t          |          k    rdS t          | |          D ]\  }}|dk    r	||k    r dS dS )NFT)r0   r   )first_shapesecond_shapefirst_nsecond_ns       r)   _shape_greaterz$spec_greater.<locals>._shape_greatern  s`    {s<00005!$[,!?!? 	 	GX"}}H!4!4uutr+   )r[   )firstotherr   s      r)   r   r   m  s)       >%+u{333r+   )T)'r   r   r   numpyr   r   paddle.pir.corer   r   r`   paddle.basepaddle.base.data_feederr   paddle.base.dygraph.baser   paddle.jit.pir_translated_layerr   paddle.jit.translated_layerr   paddle.nn.layerr    r
   r   r   r   r   r   __all__r   r   r   r   rK   rJ   rO   r   r   r   rB   r+   r)   <module>r     s              # # # # # # # # #       1 1 1 1 1 1 ; ; ; ; ; ; > > > > > > 7 7 7 7 7 7 " " " " " "                  Z: Z: Z: Z: Z: Z: Z: Z:z   ,   *- - -`# # #LH H HV     F  .  B	4 	4 	4 	4 	4r+   