o
    Qep4                     @   s   d dl Z d dlZddlmZ d dlmZmZmZm	Z	 g Z
G dd de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S )    N   )core)convert_dtypecheck_variable_and_dtype
check_typecheck_dtypec                   @   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   GD:\Projects\ConvertPro\env\Lib\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 )	aq  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=CUDAPlace(0), stop_gradient=True,
                   [[0.0002, 0.8503, 0.0135, ..., 0.9508, 0.2621, 0.6661],
                    [0.9710, 0.2605, 0.9950, ..., 0.4427, 0.9241, 0.9363],
                    [0.0948, 0.3226, 0.9955, ..., 0.1198, 0.0889, 0.9231],
                    ...,
                    [0.7206, 0.0941, 0.5292, ..., 0.4856, 0.1379, 0.0351],
                    [0.1745, 0.5621, 0.3602, ..., 0.2998, 0.4011, 0.1764],
                    [0.0728, 0.7786, 0.0314, ..., 0.2583, 0.1654, 0.0637]])
            '''
    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ridd | d | D }dd | d| d  D }tdd || D S td	d | D S )
Nr      r   c                 S      g | ]}|qS r   r   .0xr   r   r   
<listcomp>s       z_to_summary.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r   r!   t   r"   c                 S      g | ]}t |qS r   _to_summaryr   r   r   r   r!   u       c                 S   r#   r   r$   r   r   r   r   r!   w   r&   )	r   r   npprodshapearraylenZconcatenate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r6tjrdtj| }nt	| | kr,d| }ndtj| }nd| }|t
|krZ|rU| dk rL||S d||d  S ||S |S )Nz	{{:.{}e}}z{:.0f}.z	{{:.{}f}}z{}r    r   )dtyper'   float32Zfloat64Zfloat16r   r   formatr   ceilr+   ljust)np_var	max_widthsigneditem_strr   r   r   _format_itemz   s,   $


r:   c                 C   sH   d}d}t |  D ]}|s|dk rd}t|}t|t|}q
||fS )Nr   FT)listflattenr:   maxr+   )r-   r7   r8   itemr9   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   r:   r   r>   r7   r8   r   r   r!          
z"_format_tensor.<locals>.<listcomp>Nz...c                    r@   r   rA   rB   rC   r   r   r!      rD   r   c                    r@   r   rA   rB   rC   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   indentr7   r8   summaryr   r   r!          c                    rL   rM   rN   r   rP   r   r   r!      rS   c                    rL   rM   rN   r   rP   r   r   r!      rS   ,
)
r   r   r   r+   r)   r:   r=   r;   rangerH   )r-   rR   rQ   r7   r8   r   r   Zitem_lengthlinessvarsr   )rQ   rF   rG   r7   r8   rR   r   rO      s`   

rO   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} |  }t | jdkr>d}nd}| jD ]}||9 }qCd}	|tjkrSd}	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   r   FTrQ   r7   r8   r0   prefixr)   r1   placestop_gradientrQ   data)r+   r   r1   r   VarDescVarTypeBF16valueZ
get_tensorZ_is_initializedastypenumpyr)   r   r   r?   r%   rO   r3   
_place_strrb   )r-   r`   rQ   r1   	_templatetensorr6   sizedimrR   r7   r8   rc   r   r   r   	to_string   sD   




ro   c           	      C   s   | j tjjjkr| d} |  }t| jdkrd}nd}| jD ]}||9 }q d}|t	j
kr0d}tt|\}}t|||||d}|S )Nr2   r   r   FTr^   )r1   r   rd   re   rf   rh   ri   r+   r)   r   r   r?   r%   rO   )	rl   rQ   Z	np_tensorrm   rn   Zsumaryr7   r8   rc   r   r   r   _format_dense_tensor  s&   



rp   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   ra   rb   rQ   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   ra   rb   rQ   crowscolsrr   )r+   Zis_sparse_coorq   rr   rp   r3   r)   r1   rj   rb   rs   rt   )rl   r`   rQ   rk   Zindices_tensorZvalues_tensorZindices_dataZvalues_dataZcrows_tensorZcols_tensorZelements_tensorZ
crows_dataZ	cols_datar   r   r   sparse_tensor_to_string&  sV   
	


ru   c              	   C   sx   t |d }t| j}| jtjjjkrd}d}|  r t| |S | 	 s&dS t
| |}|j|| j|| j| jd| |dS )Nr   r[   r\   r]   r0   r_   )r+   r   r1   r   rd   re   rf   Z	is_sparseru   Z _is_dense_tensor_hold_allocationrp   r3   r)   rj   rb   )rl   r`   rQ   r1   rk   rc   r   r   r   tensor_to_stringO  s$   


rv   )NNNNN)r   F)r   r   F)rZ   )Zpaddleri   r'   Z	frameworkr   Zpaddle.fluid.data_feederr   r   r   r   __all__objectr   r   r   r%   r:   r?   rO   ro   rp   ru   rv   r   r   r   r   <module>   s*   
@


>-
)