B
    d*U              (   @   s  d Z ddlZddlmZ ddlmZmZmZ dd Z	dd Z
d	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zeje
ejeejeejeejeejeejeejeej eej!eej"eej#eej$eej%eej&eej'j(eej)j(eej*j(eej+j(eej,j(eiZ-ej.j(eiZ/d#d$ Z0d%d& Z1d'd( Z2d)d* Z3e4ejejejejej"ej!ejej'ej)ej*ej+ej,ejej gZ5G d+d, d,e6Z7d-d. Z8d/d0 Z9d1d2 Z:d3d4 Z;d5d6 Z<d7d8 Z=d9d: Z>d;d< Z?ej@e?gej$e=gej#e=geje<geje<geje<gej"e;geje;geje;gej!e;gej*e;e:gej,e;e:gej'e;e:gej+e;e:gej)e;e:gejAe>geje;geje;giZBd=d> ZCdDd@dAZDdBdC ZEdS )Ez%Utilities to manipulate TensorProtos.    N)
tensor_pb2)dtypescompattensor_shapec             C   s   t j| t jdt j S )N)dtype)npasarrayfloat16viewuint16item)x r   N/var/www/html/venv/lib/python3.7/site-packages/tensorboard/util/tensor_util.pyExtractBitsFromFloat16   s    r   c             C   s   | j dd |D  d S )Nc             S   s   g | ]}t |qS r   )r   ).0r   r   r   r   
<listcomp>   s    z7SlowAppendFloat16ArrayToTensorProto.<locals>.<listcomp>)half_valextend)tensor_protoproto_valuesr   r   r   #SlowAppendFloat16ArrayToTensorProto   s    r   c             C   s   t j| tjjdt j S )N)r   )r   r   r   bfloat16as_numpy_dtyper
   r   r   )r   r   r   r   ExtractBitsFromBFloat16!   s    r   c             C   s   | j dd |D  d S )Nc             S   s   g | ]}t |qS r   )r   )r   r   r   r   r   r   +   s    z8SlowAppendBFloat16ArrayToTensorProto.<locals>.<listcomp>)r   r   )r   r   r   r   r   $SlowAppendBFloat16ArrayToTensorProto)   s    r   c             C   s   | j dd |D  d S )Nc             S   s   g | ]}|  qS r   )r   )r   r   r   r   r   r   0   s    z7SlowAppendFloat32ArrayToTensorProto.<locals>.<listcomp>)	float_valr   )r   r   r   r   r   #SlowAppendFloat32ArrayToTensorProto/   s    r   c             C   s   | j dd |D  d S )Nc             S   s   g | ]}|  qS r   )r   )r   r   r   r   r   r   4   s    z7SlowAppendFloat64ArrayToTensorProto.<locals>.<listcomp>)
double_valr   )r   r   r   r   r   #SlowAppendFloat64ArrayToTensorProto3   s    r   c             C   s   | j dd |D  d S )Nc             S   s   g | ]}|  qS r   )r   )r   r   r   r   r   r   8   s    z3SlowAppendIntArrayToTensorProto.<locals>.<listcomp>)int_valr   )r   r   r   r   r   SlowAppendIntArrayToTensorProto7   s    r!   c             C   s   | j dd |D  d S )Nc             S   s   g | ]}|  qS r   )r   )r   r   r   r   r   r   <   s    z5SlowAppendInt64ArrayToTensorProto.<locals>.<listcomp>)	int64_valr   )r   r   r   r   r   !SlowAppendInt64ArrayToTensorProto;   s    r#   c             C   s   | j dd |D  d S )Nc             S   s   g | ]}|d    qS )r   )r   )r   r   r   r   r   r   @   s    z4SlowAppendQIntArrayToTensorProto.<locals>.<listcomp>)r    r   )r   r   r   r   r    SlowAppendQIntArrayToTensorProto?   s    r$   c             C   s   | j dd |D  d S )Nc             S   s   g | ]}|  qS r   )r   )r   r   r   r   r   r   D   s    z6SlowAppendUInt32ArrayToTensorProto.<locals>.<listcomp>)Z
uint32_valr   )r   r   r   r   r   "SlowAppendUInt32ArrayToTensorProtoC   s    r%   c             C   s   | j dd |D  d S )Nc             S   s   g | ]}|  qS r   )r   )r   r   r   r   r   r   H   s    z6SlowAppendUInt64ArrayToTensorProto.<locals>.<listcomp>)Z
uint64_valr   )r   r   r   r   r   "SlowAppendUInt64ArrayToTensorProtoG   s    r&   c             C   s   | j dd |D  d S )Nc             S   s&   g | ]}|j |jgD ]}| qqS r   )realimagr   )r   r   vr   r   r   r   M   s    z9SlowAppendComplex64ArrayToTensorProto.<locals>.<listcomp>)scomplex_valr   )r   r   r   r   r   %SlowAppendComplex64ArrayToTensorProtoK   s    r+   c             C   s   | j dd |D  d S )Nc             S   s&   g | ]}|j |jgD ]}| qqS r   )r'   r(   r   )r   r   r)   r   r   r   r   S   s    z:SlowAppendComplex128ArrayToTensorProto.<locals>.<listcomp>)dcomplex_valr   )r   r   r   r   r   &SlowAppendComplex128ArrayToTensorProtoQ   s    r-   c             C   s   | j dd |D  d S )Nc             S   s   g | ]}t |qS r   )r   as_bytes)r   r   r   r   r   r   X   s    z6SlowAppendObjectArrayToTensorProto.<locals>.<listcomp>)
string_valr   )r   r   r   r   r   "SlowAppendObjectArrayToTensorProtoW   s    r0   c             C   s   | j dd |D  d S )Nc             S   s   g | ]}|  qS r   )r   )r   r   r   r   r   r   \   s    z4SlowAppendBoolArrayToTensorProto.<locals>.<listcomp>)bool_valr   )r   r   r   r   r    SlowAppendBoolArrayToTensorProto[   s    r2   c             C   sH   x |   D ]\}}||kr
|S q
W x t  D ]\}}||kr,|S q,W d S )N)itemsBACKUP_DICT)Z
dtype_dictr   keyvalr   r   r   GetFromNumpyDTypeDict|   s    r7   c             C   s&   | j tjks| j tjkrtS tt| S )N)typer   Zstring_Zunicode_r0   r7   _NP_TO_APPEND_FN)r   r   r   r   GetNumpyAppendFn   s    r:   c             C   s6   t | ttfsg S | sdgS t| gt| d  S dS )z9Returns the inferred dense dimensions of a list of lists.r   N)
isinstancelisttuplelen_GetDenseDimensions)Zlist_of_listsr   r   r   r?      s
    r?   c             c   s@   t | ttfr6x,| D ]}xt|D ]
}|V  q"W qW n| V  d S )N)r;   r<   r=   _FlattenToStrings)Znested_stringsinnerZflattened_stringr   r   r   r@      s
    
r@   c               @   s   e Zd Zdd Zdd ZdS )_Messagec             C   s
   || _ d S )N)_message)selfmessager   r   r   __init__   s    z_Message.__init__c             C   s   | j S )N)rC   )rD   r   r   r   __repr__   s    z_Message.__repr__N)__name__
__module____qualname__rF   rG   r   r   r   r   rB      s   rB   c             C   s   x| D ]}|d k	r|S qW d S )Nr   )lr   r   r   r   _FirstNotNone   s    
rL   c             C   s   | d krt dS | S d S )NNone)rB   )r)   r   r   r   _NotNone   s    rN   c             C   sr   t | ttfs| S t | tr2tdd | D s2d S t | tr`tdd | D s`tdd | D S tdd | D S )Nc             s   s   | ]}t |ttfV  qd S )N)r;   r<   r=   )r   r   r   r   r   	<genexpr>   s    z_FilterTuple.<locals>.<genexpr>c             s   s   | ]}t |ttfV  qd S )N)r;   r<   r=   )r   r   r   r   r   rO      s    c             S   s"   g | ]}t |ttfrd n|qS )N)r;   r<   r=   )r   r   r   r   r   r      s    z _FilterTuple.<locals>.<listcomp>c             S   s   g | ]}t |qS r   )_FilterTuple)r   r   r   r   r   r      s    )r;   r<   r=   anyrL   )r)   r   r   r   rP      s    

rP   c             C   s>   t | ttfr tdd | D S t | tjtjfr6d S t| S )Nc             S   s   g | ]}t |qS r   )
_FilterInt)r   r   r   r   r   r      s    z_FilterInt.<locals>.<listcomp>)	r;   r<   r=   rL   r   Zintegral_typesr   Z	DimensionrN   )r)   r   r   r   rR      s    rR   c             C   s8   t | ttfr tdd | D S t | tjr0d S t| S )Nc             S   s   g | ]}t |qS r   )_FilterFloat)r   r   r   r   r   r      s    z _FilterFloat.<locals>.<listcomp>)r;   r<   r=   rL   r   Z
real_typesrN   )r)   r   r   r   rS      s    rS   c             C   s8   t | ttfr tdd | D S t | tjr0d S t| S )Nc             S   s   g | ]}t |qS r   )_FilterComplex)r   r   r   r   r   r      s    z"_FilterComplex.<locals>.<listcomp>)r;   r<   r=   rL   r   Zcomplex_typesrN   )r)   r   r   r   rT      s    rT   c             C   s<   t | ttfr tdd | D S t | tjr0d S t| S d S )Nc             S   s   g | ]}t |qS r   )
_FilterStr)r   r   r   r   r   r      s    z_FilterStr.<locals>.<listcomp>)r;   r<   r=   rL   r   Zbytes_or_text_typesrN   )r)   r   r   r   rU      s
    rU   c             C   s6   t | ttfr tdd | D S t | tr.d S t| S )Nc             S   s   g | ]}t |qS r   )_FilterBool)r   r   r   r   r   r      s    z_FilterBool.<locals>.<listcomp>)r;   r<   r=   rL   boolrN   )r)   r   r   r   rV      s    rV   c                s^   |d ks|t krd S t |}t fdd|D }|d k	rZtd|jt|t|jf d S )Nc                s   g | ]}| qS r   r   )r   fn)valuesr   r   r     s    z&_Assertconvertible.<locals>.<listcomp>z)Expected %s, got %s of type '%s' instead.)_TF_TO_IS_OKgetrL   	TypeErrornamereprr8   rH   )rY   r   Zfn_listZmismatchr   )rY   r   _Assertconvertible  s    
r_   Fc             C   s  t | tjr| S |rt|}|tjtjtjtjtj	gk}t | t
jt
jfrf|r^| |j}n| }n2tt| ddst t| ddtrt
j| |d}|} n| dkrtd|r|jr|j}nd}|dk	rt
j|t
jddkrt
j||d}nNt| | t
j| |d}t|jt| kr<|s<td| t|jt| f |jt
jkrb|dkrb|t
j}n6|jt
jkr|dkr|t
j }t
!||r|}t|j}|dkrt"d|j |r|}|dk	rt#|d	r|j$|j$krt"d
||j| f |dkr|j}d}	|j%}
nrdd |D }t
j|t
jd}
|
|j%k}	|rn|jt&|ksnt"dt&||jf |j%|
krtd|
|j%f tj|j't()|* d}|	r|t+kr|
dkr|j%|j, dkrtd|- |_.|S |tj/krXt | t
jsXt0| }ydd |D }W n* t"k
rF   t"dt1| | f Y nX |j23| |S |4 }t5|j}|dkrt"d|j6 ||| |S )a  Create a TensorProto.

    Args:
      values:         Values to put in the TensorProto.
      dtype:          Optional tensor_pb2 DataType value.
      shape:          List of integers representing the dimensions of tensor.
      verify_shape:   Boolean that enables verification of a shape of values.

    Returns:
      A `TensorProto`. Depending on the type, it may contain data in the
      "tensor_content" attribute, which is not directly useful to Python programs.
      To access the values you should convert the proto back to a numpy ndarray
      with `tensor_util.MakeNdarray(proto)`.

      If `values` is a `TensorProto`, it is immediately returned; `dtype` and
      `shape` are ignored.

    Raises:
      TypeError:  if unsupported types are provided.
      ValueError: if arguments have inappropriate values or if verify_shape is
       True and shape of values is not equals to a shape from the argument.

    make_tensor_proto accepts "values" of a python scalar, a python list, a
    numpy ndarray, or a numpy scalar.

    If "values" is a python scalar or a python list, make_tensor_proto
    first convert it to numpy ndarray. If dtype is None, the
    conversion tries its best to infer the right numpy data
    type. Otherwise, the resulting numpy array has a convertible data
    type with the given dtype.

    In either case above, the numpy ndarray (either the caller provided
    or the auto converted) must have the convertible type with dtype.

    make_tensor_proto then converts the numpy array to a tensor proto.

    If "shape" is None, the resulting tensor proto represents the numpy
    array precisely.

    Otherwise, "shape" specifies the tensor's shape and the numpy array
    can not have more elements than what "shape" specifies.
    Z	__array__NZ__array_interface__)r   zNone values not supported.r   zBArgument must be a dense tensor: %s - got shape %s, but wanted %s.zUnrecognized data type: %s
base_dtypez+Inconvertible types: %s vs. %s. Value is %sTc             S   s   g | ]}t |qS r   )int)r   dimr   r   r   r     s    z%make_tensor_proto.<locals>.<listcomp>z$Expected Tensor's shape: %s, got %s.z>Too many elements provided. Needed at most %d, but received %d)r   r      l        z>Cannot create a tensor proto whose content is larger than 2GB.c             S   s   g | ]}t |qS r   )r   r.   )r   r   r   r   r   r     s    zkFailed to convert object of type %s to Tensor. Contents: %s. Consider casting elements to a supported type.z-Element type not supported in TensorProto: %s)7r;   r   ZTensorProtor   as_dtypeqint8quint8qint16quint16qint32r   ZndarrayZgenericZastyper   callablegetattrdictr   
ValueErrorZis_numpy_compatibleprodint64emptyr_   arrayr<   shaper?   r   float64float32int32Zarray_equalr\   hasattrr`   sizer=   Zas_datatype_enumr   Zas_shapeZas_proto_TENSOR_CONTENT_TYPESitemsizetobytestensor_contentstringr@   r8   r/   r   Zravelr:   r]   )rY   r   rr   Zverify_shapeZis_quantizedZnparrayZnp_dtZdowncasted_arrayZnumpy_dtypeZis_same_sizeZ
shape_sizer   r   Z
str_valuesZ	append_fnr   r   r   make_tensor_proto$  s    +












r}   c             C   s  dd | j jD }tj|tjd}t| j}|j}| j	rTtj
| j	|d |S |tjksh|tjkrt| jdkrtj| jd tjd}|j|_t|||S tj| jtjd}|j|_||S n@|tjkr&t| jdkrttj| jd |d||S tj| j|d|S n|tjkr~t| jdkrdttj| jd |d||S tj| j|d|S n|tjtjtjtjtjtjtjtjtj tj!g
krt| j"dkrttj| j"d |d||S tj| j"|d|S n|tjkrTt| j#dkr:ttj| j#d |d||S tj| j#|d|S n|tj$krt| j%dkrttj| j%d |d||S tjdd | j%D |d|S nX|tj&kr0t'| j(}t| j(dkr
ttjt)| j(d | j(d |d||S tjdd t*||D |d|S n|tj+krt'| j,}t| j,dkrttjt)| j,d | j,d |d||S tjd	d t*||D |d|S nd|tj-kr t| j.dkrttj| j.d |d||S tj| j.|d|S nt/d
| j dS )a  Create a numpy ndarray from a tensor.

    Create a numpy ndarray with the same shape and data as the tensor.

    Args:
      tensor: A TensorProto.

    Returns:
      A numpy array with the tensor contents.

    Raises:
      TypeError: if tensor has unsupported type.
    c             S   s   g | ]
}|j qS r   )rw   )r   dr   r   r   r     s    z make_ndarray.<locals>.<listcomp>)r   rc   r   c             S   s   g | ]}|qS r   r   )r   r   r   r   r   r   E  s       c             S   s   g | ]}t |d  |d qS )r   rc   )complex)r   r   r   r   r   r   S  s    c             S   s   g | ]}t |d  |d qS )r   rc   )r   )r   r   r   r   r   r   a  s    zUnsupported tensor type: %sN)0r   rb   r   rn   ro   r   rd   r   r   r{   Z
frombuffercopyZreshaper	   r   r>   r   rq   r   repeatZfromiterrt   r   rs   r   ru   uint8int16int8ri   rf   re   rg   rh   r    r"   r|   r/   	complex64iterr*   r   zip
complex128r,   rW   r1   r\   )Ztensorrr   Znum_elementsZtensor_dtyper   tmpitr   r   r   make_ndarray  s    


r   )NNF)F__doc__numpyr   Ztensorboard.compat.protor   Z"tensorboard.compat.tensorflow_stubr   r   r   r   r   r   r   r   r   r!   r#   r$   r%   r&   r+   r-   r0   r2   r	   rt   rs   ru   ro   r   r   Zuint32Zuint64r   r   r   r   Zobject_Zbool_re   r   rf   rg   rh   ri   r9   r   r4   r7   r:   r?   r@   	frozensetrx   objectrB   rL   rN   rP   rR   rS   rT   rU   rV   rW   r|   rZ   r_   r}   r   r   r   r   r   <module>   s   

	

	





 P