o
    #j6                     @   s   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Z
d	d
 ZdddZdd ZdddZd ddZdd Zd ddZd ddZd ddZdS )!    N)
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   X/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/tensor/to_string.pyr      s    r   c                 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 )	a  Set the printing options for Tensor.

    Args:
        precision (int, optional): Number of digits of the floating number, default 8.
        threshold (int, optional): Total number of elements printed, default 1000.
        edgeitems (int, optional): Number of elements in summary at the beginning and ending of each dimension, default 3.
        sci_mode (bool, optional): Format the floating number with scientific notation or not, default False.
        linewidth (int, 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>u       z_to_summary.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r   r!   w   r"   )
r   r   npprodshapearraylenZconcatenateliststack)varr   beginendr   r   r   r   b   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_itemz   s    

r9   c                 C   sH   d}d}t |  D ]}|s|dk rd}t|}t|t|}q
||fS )Nr   FT)r(   flattenr9   maxr'   )r*   r6   r7   itemr8   r   r   r   _get_max_width   s   r=   c                    s  t j}t j}t| jdkrt| S t| jdkrd }|  | tdrZ| jd d| krZfddt| d| D dg fddt| d	| d D  nfd
dt| 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| d| D dg  fdd| d	| d D  }
n fdd| D }
dddt| j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   r9   r   r<   r6   r7   r   r   r!          
z"_format_tensor.<locals>.<listcomp>Nz...c                    r>   r   r?   r@   rA   r   r   r!      rB   r   c                    r>   r   r?   r@   rA   r   r   r!      s    c                    s   g | ]
} ||  qS r   r   )r   i)itemsitems_per_liner   r   r!      s    z,
r0   c                 S   s   g | ]}d  |qS )z, )join)r   liner   r   r   r!      s    []c                        g | ]}t | d  qS r   _format_tensorr   indentr6   r7   summaryr   r   r!          c                    rJ   rK   rL   r   rN   r   r   r!      rQ   c                    rJ   rK   rL   r   rN   r   r   r!      rQ   ,
)
r   r   r   r'   r%   r9   r;   r(   rangerF   )r*   rP   rO   r6   r7   r   r   Zitem_lengthlinessvarsr   )rO   rD   rE   r6   r7   rP   r   rM      sf   

"rM   Tensorc              	   C   s   t |d }t| j}| jtjjjkrd}d}|   }|	 s#dS | jtjjjkr0| 
d} | d}t | jdkr?d}nd}| jD ]}||9 }qDd}	|tjkrTd}	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)r2   Fr   TrO   r6   r7   r0   prefixr%   r1   placestop_gradientrO   data)r'   r   r1   r   VarDescVarTypeBF16valueZ
get_tensorZ_is_initializedastypenumpyr%   r   r   r=   r   rM   format
_place_strr`   )r*   r^   rO   r1   	_templatetensorr5   sizedimrP   r6   r7   ra   r   r   r   	to_string   s@   






rn   c           	      C   s   | j tjjjkr| d} | d}t| jdkrd}nd}| jD ]}||9 }q!d}|t	j
kr1d}tt|\}}t|||||d}|S )Nr2   Fr   r   Tr\   )r1   r   rb   rc   rd   rf   rg   r'   r%   r   r   r=   r   rM   )	rk   rO   Z	np_tensorrl   rm   Zsumaryr6   r7   ra   r   r   r   _format_dense_tensor  s    





ro   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=r0   )r^   r%   r1   r_   r`   rO   indicesvaluesz{prefix}(shape={shape}, dtype={dtype}, place={place}, stop_gradient={stop_gradient}, 
{indent}{crows}, 
{indent}{cols}, 
{indent}{values})zcrows=zcols=)	r^   r%   r1   r_   r`   rO   crowscolsrq   )r'   Zis_sparse_coorp   rq   ro   rh   r%   r1   ri   r`   rr   rs   )rk   r^   rO   rj   Zindices_tensorZvalues_tensorZindices_dataZvalues_dataZcrows_tensorZcols_tensorZelements_tensorZ
crows_dataZ	cols_datar   r   r   sparse_tensor_to_string4  sZ   rt   c                 C   s   t |d }t| j}| jtjjjkrd}|  s,d}|j|| j	|| j
| j| j| jdS t |d }t| |}d}|j|| j	|| j
| j| j| jd| |d	S )Nr   rY   z{prefix}(shape={shape}, dtype={dtype}, place={place}, stop_gradient={stop_gradient}, process_mesh={process_mesh}, placements={placements}, GlobalDenseTensor Not initialized))r^   r%   r1   r_   r`   process_mesh
placementsz{prefix}(shape={shape}, dtype={dtype}, place={place}, stop_gradient={stop_gradient}, process_mesh={process_mesh}, placements={placements}, GlobalDenseTensor=
{indent}{data})r0   )	r^   r%   r1   r_   r`   ru   rv   rO   ra   )r'   r   r1   r   rb   rc   rd    _is_dense_tensor_hold_allocationrh   r%   ri   r`   ru   Z_placements_strro   rk   r^   rO   r1   rj   ra   r   r   r   dist_tensor_to_stringf  s:   


ry   c              	   C   s   t |d }t| j}| jtjjjkrd}d}|  r t| |S | 	 r)t
| |S |  s/dS t| |}|j|| j|| j| jd| |dS )Nr   rY   rZ   r[   r0   r]   )r'   r   r1   r   rb   rc   rd   Z	is_sparsert   Zis_distry   rw   ro   rh   r%   ri   r`   rx   r   r   r   tensor_to_string  s*   



rz   )NNNNN)r   F)r   r   F)rX   )rg   r#   Zpaddle.base.data_feederr   r   Z	frameworkr   __all__r   r   r   r   r9   r=   rM   rn   ro   rt   ry   rz   r   r   r   r   <module>   s*   
?


K-

2$