
    ϑit                     >    S SK r S SKJr  S SKJrJr  S rS rSS jrg)    N)
check_type)Variablein_pir_modec                     [         R                  " 5       (       a/  [        U [         R                  5      (       d  [	        SU  S35      eg[        X[        [         R                  R                  4U5        g)z.Check whether the input is tensor or variable.zThe input: z must be tensor.N)	paddlein_dynamic_mode
isinstanceTensor
ValueErrorr   r   pirValue)inputnameop_names      j/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/incubate/optimizer/functional/utils.pycheck_input_typer      sY    %//{5'1ABCC 0 	56::+;+; <gF    c                 `  ^^^ [         R                  " [         R                  " X R                  5       5      5      mS m[         R                  " 5       (       a/  T(       d  T" 5          [         R
                  R                  U 5        g[        5       (       a  [         R                  R                  R                  R                  TSSSS9  [         R
                  R                  U 5      n[         R                  " [         R                  " UR                  5       S:  5      [         R                  " UR!                  5       S:H  5      5      n[         R                  R                  R                  R                  USSSS9  gS nU" [         R                  R#                  5       SS	S
/S9mUUU4S jn[         R                  R                  R%                  TSU5        [         R&                  " S5        [         R
                  R                  U 5      n[         R                  " UR                  5       S:  5      =(       a'    [         R                  " UR!                  5       S:H  5      n[         R                  R                  R%                  USU5        g! [         a  nT" 5          SnAgSnAff = f)a  Check whether the specified initial_inverse_hessian_estimate is symmetric and positive definite.
    Raise errors when precondition not met.

Note:
    In static graph can not raise error directly, so use py_func make raise_func as a op,
    and use paddle.static.nn.cond to decide if put the op in net.
    cholesky is the fast way to check positive definition, but in static graph can not catch
    exception to raise value error, so use eigvals rather than cholesky in static graph.
c                      [        S5      e)NiThe initial_inverse_hessian_estimate should be symmetric and positive definite, but the specified is not.)r    r   r   
raise_func:check_initial_inverse_hessian_estimate.<locals>.raise_func)   s    w
 	
r   N
   r   )r   g        c                 >    U R                  5       R                  XUS9$ )Nr   dtypeshape)current_block
create_var)programr   r   r   s       r   create_tmp_var>check_initial_inverse_hessian_estimate.<locals>.create_tmp_varI   s(    ((*55e 6  r   outputfloat32r   c                  X   > [         R                  R                  R                  TT TS9  g )N)funcxout)r   staticnnpy_func)is_symmetricout_varr   s   r   false_fn8check_initial_inverse_hessian_estimate.<locals>.false_fnU   s'    MM$$<W % r   cpu)r   allequaltr   linalgcholeskyRuntimeErrorr   r+   r,   control_flowAsserteigvalsbitwise_andrealimagdefault_main_programcond
set_device)	H0errorr;   is_positiver"   r0   r.   r/   r   s	         @@@r   &check_initial_inverse_hessian_estimaterE      s    ::fll2ttv67L

 L	MM""2& 
%%,,|	 	- 	
 --''+((JJw||~+,fjj39N.O
 	%%,,|	 	- 	
	
 !MM..0$	
	
 	lD(;% --''+jj#!56 
6::LLNc!<
 	k4:]  	LL	s   (J 
J-J((J-c                 6   UR                  5       nSUl        U " U5      n[        R                  " 5       (       a  [        R                  " U/U/SS9S   nO%[        R
                  R                  U/U/5      S   nUR                  5       UR                  5       4$ )zCompute function value and gradient of f at x.

Args:
    f (Callable): the objective function.
    x (Tensor): the input tensor.
Returns:
    value: a tensor that holds the function value.
    gradient: a tensor that holds the function gradients.
F)create_graphr   )detachstop_gradientr   r   gradr+   	gradients)fr)   vvaluegradients        r   _value_and_gradientrP   d   s     	

AAOaDE;;w%@C==**E7QC8;<<>8??,,,r   )N)	r   paddle.base.data_feederr   paddle.base.frameworkr   r   r   rE   rP   r   r   r   <module>rS      s"     . 7GD;N-r   