
    Αi=                         S r SSKrSSKrSSKrSSKJr  / 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S
 jrSS jrg)zFleet Metrics    N)Variablec                    Uc  [         R                  R                  5       nUc$  [         R                  R                  R
                  n[        U [        5      (       a>  [        R                  " UR                  U R                  5      R                  5       5      n OH[        U [        5      (       a3  [        R                  " UR                  U 5      R                  5       5      n [        R                  " U R                  5      n[        R                  " U 5      S-  nUR!                  U S5      nUR#                  U5      nU$ )a	  
distributed sum in fleet

Args:
    input(numpy.array|Variable|string): output of a layer
    scope(Scope): specific scope

Returns:
    global_metric(numpy.array): sum array

Example:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:DISTRIBUTED)
        >>> # in model.py
        >>> input = paddle.cast(some_input, dtype='float32')
        >>> cnt = paddle.sum(input)
        >>> global_cnt = paddle.static.create_global_var(persistable=True, dtype='float32', shape=[], value=0)
        >>> tmp = paddle.add(cnt, global_cnt)
        >>> paddle.assign(tmp, global_cnt)

        >>> # in train.py, after train or infer
        >>> res = np.array(scope.find_var(global_cnt.name).get_tensor())
        >>> print("sum array: ", paddle.distributed.fleet.sum(res))
r   sumpaddlestaticglobal_scopedistributedfleetutil
isinstancer   nparrayfind_varname
get_tensorstrshapecopy
all_reducereshapeinputscoper   	old_shapeoutputs        g/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distributed/fleet/metrics/metric.pyr   r          4 }**,|!!'',,%""

3>>@A	E3		.99;<%IWWU^aF__UE*F^^I&FM    c                    Uc  [         R                  R                  5       nUc$  [         R                  R                  R
                  n[        U [        5      (       a>  [        R                  " UR                  U R                  5      R                  5       5      n OH[        U [        5      (       a3  [        R                  " UR                  U 5      R                  5       5      n [        R                  " U R                  5      n[        R                  " U 5      S-  nUR!                  U S5      nUR#                  U5      nU$ )a  
distributed max in fleet

Args:
    input(numpy.array|Variable|string): output of a layer
    scope(Scope): specific scope

Returns:
    global_metric(numpy.array): max array

Example:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:DISTRIBUTED)
        >>> # in model.py
        >>> input = paddle.cast(some_input, dtype='float32')
        >>> cnt = paddle.sum(input)
        >>> global_cnt = paddle.static.create_global_var(persistable=True, dtype='float32', shape=[], value=0)
        >>> tmp = paddle.maximum(cnt, global_cnt)
        >>> paddle.assign(tmp, global_cnt)

        >>> # in train.py, after train or infer
        >>> res = np.array(scope.find_var(global_cnt.name).get_tensor())
        >>> print("max array: ", paddle.distributed.fleet.max(res))
r   maxr   r   s        r   r!   r!   C   r   r   c                    Uc  [         R                  R                  5       nUc$  [         R                  R                  R
                  n[        U [        5      (       a>  [        R                  " UR                  U R                  5      R                  5       5      n OH[        U [        5      (       a3  [        R                  " UR                  U 5      R                  5       5      n [        R                  " U R                  5      n[        R                  " U 5      S-  nUR!                  U S5      nUR#                  U5      nU$ )a  
distributed min in fleet

Args:
    input(numpy.array|Variable|string): output of a layer
    scope(Scope): specific scope

Returns:
    global_metric(numpy.array): min array

Example:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:DISTRIBUTED)
        >>> # in model.py
        >>> input = paddle.cast(some_input, dtype='float32')
        >>> cnt = paddle.sum(input)
        >>> global_cnt = paddle.static.create_global_var(persistable=True, dtype='float32', shape=[], value=0)
        >>> tmp = paddle.minimum(cnt, global_cnt)
        >>> paddle.assign(tmp, global_cnt)

        >>> # in train.py, after train or infer
        >>> res = np.array(scope.find_var(global_cnt.name).get_tensor())
        >>> print("min array: ", paddle.distributed.fleet.min(res))
r   minr   r   s        r   r#   r#   l   r   r   c                    Uc  [         R                  R                  5       nUc$  [         R                  R                  R
                  n[        U [        5      (       a>  [        R                  " UR                  U R                  5      R                  5       5      n OH[        U [        5      (       a3  [        R                  " UR                  U 5      R                  5       5      n [        U[        5      (       a>  [        R                  " UR                  UR                  5      R                  5       5      nOH[        U[        5      (       a3  [        R                  " UR                  U5      R                  5       5      n[        R                  " U R                  5      nU R                  S5      n [        R                   " U 5      S-  nUR#                  U S5      nUR                  U5      n[        R                  " UR                  5      nUR                  S5      n[        R                   " U5      S-  nUR#                  US5      nUR                  U5      n[%        US   5      nSn	Sn
SnSnSnSn['        U5       HF  nUS-
  U-
  nXS   U   -   nXS   U   -  nXS   U   -   nXS   U   -  nXU-
  X-   -  S-  -  n	Un
UnMH     SnX-  S:X  d  US:X  a  SnU$ XU-  -  nU$ )	aG  
distributed auc in fleet

Args:
    stat_pos(numpy.array|Variable|string): stat_pos in output of paddle.static.auc
    stat_neg(numpy.array|Variable|string): stat_neg in output of paddle.static.auc
    scope(Scope): specific scope

Returns:
    auc_value(float): auc value

Example:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:DISTRIBUTED)
        >>> # in model.py
        >>> similarity_norm = paddle.nn.functional.sigmoid(paddle.clip(output, min=-15.0, max=15.0))
        >>> binary_predict = paddle.concat(
        ...     input=[paddle.subtract(paddle.ceil(similarity_norm), similarity_norm), similarity_norm], axis=1)
        >>> self.auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, stat_neg] =
        ...     paddle.static.auc(input=binary_predict, label=label, curve='ROC', num_thresholds=4096)

        >>> # in train.py, after train or infer
        >>> pos = np.array(scope.find_var(stat_pos.name).get_tensor())
        >>> neg = np.array(scope.find_var(stat_neg.name).get_tensor())
        >>> print("auc: ", paddle.distributed.fleet.auc(pos, neg))
Nr   r   g              g      ?)r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   lenrange)stat_posstat_negr   r   old_pos_shape
global_posold_neg_shape
global_neg
num_bucketareaposnegnew_posnew_negtotal_ins_numiindex	auc_values                     r   aucr:      s   8 }**,|!!'',,(H%%88ENN8==9DDFG	Hc	"	"88ENN84??AB(H%%88ENN8==9DDFG	Hc	"	"88ENN84??ABHHX^^,M#H"Q&J51J##M2J HHX^^,M#H"Q&J51J##M2J Z]#JD
C
CGGM:Q"1e,,Au--1e,,Au--33=1A55  I
yA~!+	  #I&	r   c                 :   Uc  [         R                  R                  5       nUc$  [         R                  R                  R
                  n[        U [        5      (       a>  [        R                  " UR                  U R                  5      R                  5       5      n OH[        U [        5      (       a3  [        R                  " UR                  U 5      R                  5       5      n [        U[        5      (       a>  [        R                  " UR                  UR                  5      R                  5       5      nOH[        U[        5      (       a3  [        R                  " UR                  U5      R                  5       5      n[        R                  " U R                  5      nU R                  S5      n [        R                   " U 5      S-  nUR#                  U S5      nUR                  U5      nUR#                  US5      n[%        US   5      [%        US   5      -  nU$ )a  
distributed mae in fleet

Args:
    abserr(numpy.array|Variable|string): abserr in output of paddle.static.ctr_metric_bundle
    total_ins_num(numpy.array|Variable|string): total variable
    scope(Scope): specific scope

Returns:
    mae(float): mae value

Example:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:DISTRIBUTED)
        >>> # in model.py
        >>> sqrerr, abserr, prob, q, pos, total = paddle.static.ctr_metric_bundle(similarity_norm, paddle.cast(x=label, dtype='float32'))

        >>> # in train.py, after train or infer
        >>> res = np.array(scope.find_var(abserr.name).get_tensor())
        >>> print("mae: ", paddle.distributed.fleet.mae(res, total_ins_num))
r%   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   float)abserrr6   r   r   old_metric_shapeglobal_metricglobal_total_num	mae_values           r   maerC      s   . }**,|!!'',,&(##%..5@@BC	FC	 	 %..0;;=>-**NN=--.99;
 
M3	'	'!>!I!I!KLxx-^^BFGGFOa'MOOFE2M!))*:;M}e<mA&'%0@0C*DDIr   c                 b   Uc  [         R                  R                  5       nUc$  [         R                  R                  R
                  n[        U [        5      (       a>  [        R                  " UR                  U R                  5      R                  5       5      n OH[        U [        5      (       a3  [        R                  " UR                  U 5      R                  5       5      n [        U[        5      (       a>  [        R                  " UR                  UR                  5      R                  5       5      nOH[        U[        5      (       a3  [        R                  " UR                  U5      R                  5       5      n[        R                  " U R                  5      nU R                  S5      n [        R                   " U 5      S-  nUR#                  U S5      nUR                  U5      nUR#                  US5      n[$        R&                  " [)        US   5      [)        US   5      -  5      nU$ )a  
distributed rmse in fleet

Args:
    sqrerr(numpy.array|Variable|string): sqrerr in output of paddle.static.ctr_metric_bundle
    total_ins_num(numpy.array|Variable|string): total variable
    scope(Scope): specific scope

Returns:
    rmse(float): rmse value

Example:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:DISTRIBUTED)
        >>> # in model.py
        >>> sqrerr, abserr, prob, q, pos, total = paddle.static.ctr_metric_bundle(similarity_norm, paddle.cast(x=label, dtype='float32'))

        >>> # in train.py, after train or infer
        >>> res = np.array(scope.find_var(sqrerr.name).get_tensor())
        >>> print("rmse: ", paddle.distributed.fleet.rmse(res, total_ins_num))
r%   r   r   )r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   mathsqrtr=   )sqrerrr6   r   r   r?   r@   rA   
rmse_values           r   rmserI     s   . }**,|!!'',,&(##%..5@@BC	FC	 	 %..0;;=>-**NN=--.99;
 
M3	'	'!>!I!I!KLxx-^^BFGGFOa'MOOFE2M!))*:;M}e<5q!12U;KA;N5OOPJr   c                 :   Uc  [         R                  R                  5       nUc$  [         R                  R                  R
                  n[        U [        5      (       a>  [        R                  " UR                  U R                  5      R                  5       5      n OH[        U [        5      (       a3  [        R                  " UR                  U 5      R                  5       5      n [        U[        5      (       a>  [        R                  " UR                  UR                  5      R                  5       5      nOH[        U[        5      (       a3  [        R                  " UR                  U5      R                  5       5      n[        R                  " U R                  5      nU R                  S5      n [        R                   " U 5      S-  nUR#                  U S5      nUR                  U5      nUR#                  US5      n[%        US   5      [%        US   5      -  nU$ )a  
distributed mse in fleet

Args:
    sqrerr(numpy.array|Variable|string): sqrerr in output of paddle.static.ctr_metric_bundle
    total_ins_num(numpy.array|Variable|string): total variable
    scope(Scope): specific scope

Returns:
    mse(float): mse value

Example:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:DISTRIBUTED)
        >>> # in model.py
        >>> sqrerr, abserr, prob, q, pos, total = paddle.static.ctr_metric_bundle(similarity_norm, paddle.cast(x=label, dtype='float32'))

        >>> # in train.py, after train or infer
        >>> metric = np.array(scope.find_var(sqrerr.name).get_tensor())
        >>> print("mse: ", paddle.distributed.fleet.mse(metric, total_ins_num))
r%   r   r   r<   )rG   r6   r   r   r?   r@   rA   	mse_values           r   mserL   O  s   . }**,|!!'',,&(##%..5@@BC	FC	 	 %..0;;=>-**NN=--.99;
 
M3	'	'!>!I!I!KLxx-^^BFGGFOa'MOOFE2M!))*:;M}e<mA&'%0@0C*DDIr   c                    Uc  [         R                  R                  5       nUc$  [         R                  R                  R
                  n[        U [        5      (       a>  [        R                  " UR                  U R                  5      R                  5       5      n OH[        U [        5      (       a3  [        R                  " UR                  U 5      R                  5       5      n [        U[        5      (       a>  [        R                  " UR                  UR                  5      R                  5       5      nOH[        U[        5      (       a3  [        R                  " UR                  U5      R                  5       5      n[        R                  " U 5      S-  n[        R                  " U5      S-  nUR                  U S5      nUR                  US5      n[!        US   5      [!        US   5      -  $ )a4  
distributed accuracy in fleet

Args:
    correct(numpy.array|Variable|string): correct Variable
    total(numpy.array|Variable): total Variable
    scope(Scope): specific scope

Returns:
    acc(float): accuracy value

Example:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:DISTRIBUTED)
        >>> # in model.py
        >>> correct = paddle.static.create_global_var(dtype='float32', shape=[1], value=0)
        >>> total = paddle.static.create_global_var(dtype='float32', shape=[1], value=0)
        >>> acc = paddle.metric.accuracy(predict, label, k=1, correct=correct, total=total)

        >>> global_correct = paddle.static.create_global_var(persistable=True, dtype='float32', shape=[1], value=0)
        >>> tmp1 = paddle.minimum(correct, global_correct)
        >>> paddle.assign(tmp1, global_correct)

        >>> global_total = paddle.static.create_global_var(persistable=True, dtype='float32', shape=[1], value=0)
        >>> tmp2 = paddle.minimum(total, global_total)
        >>> paddle.assign(tmp2, global_total)

        >>> # in train.py, after train or infer
        >>> correct_num = np.array(scope.find_var(correct.name).get_tensor())
        >>> total_num = np.array(scope.find_var(total.name).get_tensor())
        >>> print("accuracy: ", paddle.distributed.fleet.acc(correct_num, total_num))
r   r   )r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r=   )correcttotalr   r   global_correct_numrA   s         r   accrQ     s^   D }**,|!!'',,'8$$((5>>',,7BBDE	GS	!	!((5>>'2==?@%""

3>>@A	E3		.99;<)A-wwu~)%8ue4#A&'%0@0C*DDDr   )NN)__doc__rE   numpyr   r   paddle.common_ops_importr   __all__r   r!   r#   r:   rC   rI   rL   rQ    r   r   <module>rW      sL        -
&R&R&RQh0f0f/d6Er   