o
    pi0>                     @  s   d dl mZ d dlZd dlZd dlZd dlmZmZ ddl	m
Z
 g ZG dd dZe Z					d+d,ddZdd Zd-ddZdd Zd.ddZd/ddZdd  Zd!d" Zd/d#d$Zd/d%d&Zd/d'd(Zd/d)d*ZdS )0    )annotationsN)
check_typeconvert_dtype   )corec                   @  s    e Zd ZdZdZdZdZdZdS )PrintOptions   i     P   FN)__name__
__module____qualname__	precision	threshold	edgeitems	linewidthsci_mode r   r   ^/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/tensor/to_string.pyr      s    r   r   
int | Noner   r   r   bool | Noner   returnNonec                 C  s   i }| durt | dtd | t_| |d< |dur&t |dtd |t_||d< |dur8t |dtd |t_||d< |durJt |dtd |t_||d< |dur\t |dtd |t_||d< t	j
di | dS )	a3  Set the printing options for Tensor.

    Args:
        precision (int|None, optional): Number of digits of the floating number, default 8.
        threshold (int|None, optional): Total number of elements printed, default 1000.
        edgeitems (int|None, optional): Number of elements in summary at the beginning and ending of each dimension, default 3.
        sci_mode (bool|None, optional): Format the floating number with scientific notation or not, default False.
        linewidth (int|None, optional): Number of characters each line, default 80.


    Returns:
        None.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> paddle.seed(10)
            >>> a = paddle.rand([10, 20])
            >>> paddle.set_printoptions(4, 100, 3)
            >>> print(a)
            Tensor(shape=[10, 20], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[0.2727, 0.5489, 0.8655, ..., 0.2916, 0.8525, 0.9000],
             [0.3806, 0.8996, 0.0928, ..., 0.9535, 0.8378, 0.6409],
             [0.1484, 0.4038, 0.8294, ..., 0.0148, 0.6520, 0.4250],
             ...,
             [0.3426, 0.1909, 0.7240, ..., 0.4218, 0.2676, 0.5679],
             [0.5561, 0.2081, 0.0676, ..., 0.9778, 0.3302, 0.9559],
             [0.2665, 0.8483, 0.5389, ..., 0.4956, 0.6862, 0.9178]])
    Nr   set_printoptionsr   r   r   r   r   )r   intDEFAULT_PRINT_OPTIONSr   r   r   r   boolr   r   r   )r   r   r   r   r   kwargsr   r   r   r   (   s,   &r   c                 C  s   t j}t| jdkrtg S t| jdkr| S t| jdkr<| jd d| kr:t| d | | d| d  gS | S | jd d| krct| d | }t| d| d  }t	dd || D S t	dd | D S )Nr      r   c                 S     g | ]}t |qS r   _to_summary.0xr   r   r   
<listcomp>z       z_to_summary.<locals>.<listcomp>c                 S  r    r   r!   r#   r   r   r   r&   |   r'   )
r   r   npprodshapearraylenZconcatenateliststack)varr   beginendr   r   r   r"   g   s   
"r"   Fc                 C  s   | j tjks| j tjks| j tjkr7tjr| dtj d}nt| | kr-| dd}n| dtj d}n|  }|t	|krY|rT| dk rK|
|S d|
|d  S |
|S |S )N.ez.0ffr    r   )dtyper(   float32Zfloat64Zfloat16r   r   r   ceilr,   ljust)np_var	max_widthsigneditem_strr   r   r   _format_item   s    

r>   c                 C  sH   d}d}t |  D ]}|s|dk rd}t|}t|t|}q
||fS )Nr   FT)r-   flattenr>   maxr,   )r/   r;   r<   itemr=   r   r   r   _get_max_width   s   rB   c                   s  t j}t j}tjdkrtS tjdkrd }td|  | rYjd d| krYfddt|D dg fddtjd | jd D  nfddtjd D fd	dtdtD }d
d d   dd |D }	d|	 d S rňjd d| krŇ fddt|D dg  fddtjd | jd D  }
n fddtjd D }
ddtjd   d d   |
}	d|	 d S )a\  
    Format a tensor

    Args:
        var(Tensor): The tensor to be formatted.
        summary(bool): Do summary or not. If true, some elements will not be printed, and be replaced with "...".
        indent(int): The indent of each line.
        max_width(int): The max width of each elements in var.
        signed(bool): Print +/- or not.
    r   r   r   c                      g | ]
}t |  qS r   r>   r$   ir;   r<   r/   r   r   r&          z"_format_tensor.<locals>.<listcomp>z...c                   rC   r   rD   rE   rG   r   r   r&      rH   c                   rC   r   rD   rE   rG   r   r   r&      rH   c                   s   g | ]
} ||  qS r   r   rE   )itemsitems_per_liner   r   r&      rH   z,
r5   c                 S  s   g | ]}d  |qS )z, )join)r$   liner   r   r   r&      s    []c                   $   g | ]}t |  d  qS r   _format_tensorrE   indentr;   r<   summaryr/   r   r   r&          c                   rO   rP   rQ   rE   rS   r   r   r&      rV   c                   rO   rP   rQ   rE   rS   r   r   r&      s    ,
)	r   r   r   r,   r*   r>   r@   rangerK   )r/   rU   rT   r;   r<   r   r   Zitem_lengthlinessvarsr   )rT   rI   rJ   r;   r<   rU   r/   r   rR      sX   (rR   Tensorc              	   C  s   t |d }t| j}| jtjkrd}d}|   }| s!dS | jtjkr6| j	 s1tj
  | d} | d}t | jdkrEd}nd}| jD ]}||9 }qJd}	|tjkrZd}	tt|\}
}t||	||
|d	}|j|| j|| j| jd
| |dS )Nr   bfloat16d{prefix}(shape={shape}, dtype={dtype}, place={place}, stop_gradient={stop_gradient},
{indent}{data})Tensor(Not initialized)r7   Fr   TrT   r;   r<   r5   prefixr*   r6   placestop_gradientrT   data)r,   r   r6   paddler^   valueZ
get_tensorZ_is_initializedrd   is_cpu_placedevicesynchronizeastypenumpyr*   r   r   rB   r"   rR   format
_place_strre   )r/   rc   rT   r6   	_templatetensorr:   sizedimrU   r;   r<   rf   r   r   r   	to_string   sD   








rt   c                 C  s"   t jddd}| d|@ dS )Nl      Zuint32)r6   r7   )r(   r+   view)	np_tensormaskr   r   r   mask_xpu_bf16_tensor   s   rx   c                 C  s   | j }|tjks|tjjjks|tjjjks|tjjjkr,| j	
 s'tj  | d} | d}t rLtdd urL|tjksH|tjjjkrLt|}|  tjk}tt|\}}t|||||d}|S )Nr7   FZXPU_PADDLE_MASK_BF16_PRINTra   )r6   rg   r^   r   ZVarDescZVarTypeZBF16Z
FP8_E4M3FNZFP8_E5M2rd   ri   rj   rk   rl   rm   Zis_compiled_with_xpuosgetenvrx   Znumelr   r   rB   r"   rR   )rq   rT   r6   rv   rU   r;   r<   rf   r   r   r   _format_dense_tensor&  s(   





r{   c              
   C  sN   t |d }|  r%d}t| |}|j|| j|| j| jd| ||  dS d S )Nr   zq{prefix}(shape={shape}, dtype={dtype}, place={place}, stop_gradient={stop_gradient}, rows={rows},
{indent}{data})r5   )rc   r*   r6   rd   re   rT   rf   rows)r,   is_selected_rowsr{   rn   r*   ro   re   r|   )rq   r6   rc   rT   rp   rf   r   r   r   selected_rows_tensor_to_stringE  s   
r~   c                 C  s   t |d }|  r=d}|  }|  }dt||t d  }dt||t d  }|j|| j| j| j| j	d| ||dS d}| 
 }|  }	|  }
dt||t d  }d	t|	|t d	  }dt|
|t d  }|j|| j| j| j| j	d| |||d
	S )Nr   z{{prefix}(shape={shape}, dtype={dtype}, place={place}, stop_gradient={stop_gradient}, 
{indent}{indices}, 
{indent}{values})zindices=zvalues=r5   )rc   r*   r6   rd   re   rT   indicesvaluesz{prefix}(shape={shape}, dtype={dtype}, place={place}, stop_gradient={stop_gradient}, 
{indent}{crows}, 
{indent}{cols}, 
{indent}{values})zcrows=zcols=)	rc   r*   r6   rd   re   rT   crowscolsr   )r,   Zis_sparse_coor   r   r{   rn   r*   r6   ro   re   r   r   )rq   rc   rT   rp   Zindices_tensorZvalues_tensorZindices_dataZvalues_dataZcrows_tensorZcols_tensorZelements_tensorZ
crows_dataZ	cols_datar   r   r   sparse_tensor_to_stringV  sZ   r   c           	        s   t |d }t| j}| jtjkrd}|  s*d}|j|| j|| j| j	| j
| jdS t |d }ddlm m}  fddt| j
jD }|| | j
|}t||}d	}|j|| j|| j| j	| j
| jd
| |d	S )Nr   r^   z{prefix}(shape={shape}, dtype={dtype}, place={place}, stop_gradient={stop_gradient}, process_mesh={process_mesh}, placements={placements}, GlobalDenseTensor Not initialized))rc   r*   r6   rd   re   process_mesh
placementsr   )	Replicatereshardc                   s   g | ]}  qS r   r   )r$   _r   r   r   r&     s    z)dist_tensor_to_string.<locals>.<listcomp>z{prefix}(shape={shape}, dtype={dtype}, place={place}, stop_gradient={stop_gradient}, process_mesh={process_mesh}, placements={placements}, GlobalDenseTensor=
{indent}{data})r5   )	rc   r*   r6   rd   re   r   r   rT   rf   )r,   r   r6   rg   r^    _is_dense_tensor_hold_allocationrn   r*   ro   re   r   Z_placements_strZpaddle.distributedr   r   rY   ndimr{   )	rq   rc   rT   r6   rp   r   r   Zglobal_tensorrf   r   r   r   dist_tensor_to_string  s@   


r   c              	   C  s   t |d }t| j}| jtjkrd}d}|  rt| |S |  r(t| ||S | 	 r1t
| |S |  s7dS t| |}|j|| j|| j| jd| |dS )Nr   r^   r_   r`   r5   rb   )r,   r   r6   rg   r^   Z	is_sparser   r}   r~   Zis_distr   r   r{   rn   r*   ro   re   )rq   rc   rT   r6   rp   rf   r   r   r   tensor_to_string  s.   



r   )NNNNN)r   r   r   r   r   r   r   r   r   r   r   r   )r   F)r   r   F)r]   )
__future__r   ry   rm   r(   rg   Zpaddle.base.data_feederr   r   Z	frameworkr   __all__r   r   r   r"   r>   rB   rR   rt   rx   r{   r~   r   r   r   r   r   r   r   <module>   s4   ?


J/


2,