
    1VhT                        d Z ddlZddlmZ ddlmZmZmZ d Z	d Z
d Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zi ej0                  e
ej2                  eej4                  eej6                  eej8                  eej:                  eej<                  eej>                  eej@                  eejB                  eejD                  eejF                  eejH                  eejJ                  eejL                  eejN                  jP                  eejR                  jP                  eejT                  jP                  eejV                  jP                  eejX                  jP                  eiZ-ej\                  jP                  eiZ/d Z0d Z1d Z2d Z3 e4ej2                  ej4                  ej6                  ej:                  ejD                  ejB                  ej8                  ejN                  ejR                  ejT                  ejV                  ejX                  ej>                  ej@                  g      Z5 G d d      Z6d Z7d Z8d Z9d Z:d Z;d Z<d  Z=d! Z>i ej~                  e>gejH                  e<gejF                  e<gej0                  e;gej2                  e;gej4                  e;gejD                  e:gej6                  e:gej8                  e:gejB                  e:gejT                  e:e9gejX                  e:e9gejN                  e:e9gejV                  e:e9gejR                  e:e9gej                  e=gej<                  e:gej:                  e:giZAd" ZBd%d#ZCd$ ZDy)&z%Utilities to manipulate TensorProtos.    N)
tensor_pb2)dtypescompattensor_shapec                     t        j                  | t         j                        j                  t         j                        j                         S Ndtype)npasarrayfloat16viewuint16itemxs    L/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/util/tensor_util.pyExtractBitsFromFloat16r      s/    ::arzz*//		:??AA    c                 r    | j                   j                  |D cg c]  }t        |       c}       y c c}w N)half_valextendr   tensor_protoproto_valuesr   s      r   #SlowAppendFloat16ArrayToTensorProtor      s-      ,89q		"99   4c                     t        j                  | t        j                  j                        j                  t         j                        j                         S r   )r   r   r   bfloat16as_numpy_dtyper   r   r   r   s    r   ExtractBitsFromBFloat16r"   !   s3    


1FOO::;	bii	r   c                 r    | j                   j                  |D cg c]  }t        |       c}       y c c}w r   )r   r   r"   r   s      r   $SlowAppendBFloat16ArrayToTensorProtor$   )   s-      -9:	 	#::r   c                 |    | j                   j                  |D cg c]  }|j                          c}       y c c}w r   )	float_valr   r   r   s      r   #SlowAppendFloat32ArrayToTensorProtor'   /   *    !!\"B1668"BC"B   9c                 |    | j                   j                  |D cg c]  }|j                          c}       y c c}w r   )
double_valr   r   r   s      r   #SlowAppendFloat64ArrayToTensorProtor,   3   *    ""l#CAFFH#CD#Cr)   c                 |    | j                   j                  |D cg c]  }|j                          c}       y c c}w r   int_valr   r   r   s      r   SlowAppendIntArrayToTensorProtor1   7   s*    < @a @A @r)   c                 |    | j                   j                  |D cg c]  }|j                          c}       y c c}w r   )	int64_valr   r   r   s      r   !SlowAppendInt64ArrayToTensorProtor4   ;   r(   r)   c                     | j                   j                  |D cg c]  }|d   j                          c}       y c c}w )Nr   r/   r   s      r    SlowAppendQIntArrayToTensorProtor6   ?   s.    l C1 CD Cs   <c                 |    | j                   j                  |D cg c]  }|j                          c}       y c c}w r   )
uint32_valr   r   r   s      r   "SlowAppendUInt32ArrayToTensorProtor9   C   r-   r)   c                 |    | j                   j                  |D cg c]  }|j                          c}       y c c}w r   )
uint64_valr   r   r   s      r   "SlowAppendUInt64ArrayToTensorProtor<   G   r-   r)   c           	          | j                   j                  |D cg c]/  }|j                  |j                  fD ]  }|j	                          1 c}}       y c c}}w r   )scomplex_valr   realimagr   r   r   r   vs       r   %SlowAppendComplex64ArrayToTensorProtorC   K   F    $$'Ba!&&!&&1ABABBB   4A
c           	          | j                   j                  |D cg c]/  }|j                  |j                  fD ]  }|j	                          1 c}}       y c c}}w r   )dcomplex_valr   r?   r@   r   rA   s       r   &SlowAppendComplex128ArrayToTensorProtorH   Q   rD   rE   c                     | j                   j                  |D cg c]  }t        j                  |       c}       y c c}w r   )
string_valr   r   as_bytesr   s      r   "SlowAppendObjectArrayToTensorProtorL   W   s-    ""#M1FOOA$6#MN#Ms   >c                 |    | j                   j                  |D cg c]  }|j                          c}       y c c}w r   )bool_valr   r   r   s      r    SlowAppendBoolArrayToTensorProtorO   [   s*      L!Aq!&&(!AB!Ar)   c                     | j                         D ]  \  }}||k(  s|c S  t        j                         D ]  \  }}||k(  s|c S  y r   )itemsBACKUP_DICT)
dtype_dictr
   keyvals       r   GetFromNumpyDTypeDictrV   |   sY    $$& S%<J  %%' S%<J r   c                     | j                   t        j                  k(  s| j                   t        j                  k(  rt        S t        t        |       S r   )typer   bytes_str_rL   rV   _NP_TO_APPEND_FNr	   s    r   GetNumpyAppendFnr\      s7    
 zzRYY%**"711 !1599r   c                 r    t        | t        t        f      sg S | sdgS t        |       gt	        | d         z   S )z9Returns the inferred dense dimensions of a list of lists.r   )
isinstancelisttuplelen_GetDenseDimensions)list_of_listss    r   rb   rb      s=    mdE]3	s
M"#&9-:J&KKKr   c              #   x   K   t        | t        t        f      r| D ]  }t        |      D ]  }|   y |  y wr   )r^   r_   r`   _FlattenToStrings)nested_stringsinnerflattened_strings      r   re   re      sF     .4-0# 	'E$5e$< ' &&'	' s   8:c                       e Zd Zd Zd Zy)_Messagec                     || _         y r   _message)selfmessages     r   __init__z_Message.__init__   s	    r   c                     | j                   S r   rl   )rn   s    r   __repr__z_Message.__repr__   s    }}r   N)__name__
__module____qualname__rp   rr    r   r   rj   rj      s     r   rj   c                     | D ]  }||c S  y r   rv   )lr   s     r   _FirstNotNonery      s      =H r   c                      | t        d      S | S )NNone)rj   )rB   s    r   _NotNoner|      s    yr   c           
      v   t        | t        t        f      s| S t        | t              rt        d | D              sy t        | t              rCt        d | D              s1t	        | D cg c]  }t        |t        t        f      rd n| c}      S t	        | D cg c]  }t        |       c}      S c c}w c c}w )Nc              3   H   K   | ]  }t        |t        t        f        y wr   r^   r_   r`   .0r   s     r   	<genexpr>z_FilterTuple.<locals>.<genexpr>        ;A:a$/;    "c              3   H   K   | ]  }t        |t        t        f        y wr   r   r   s     r   r   z_FilterTuple.<locals>.<genexpr>   r   r   )r^   r_   r`   anyry   _FilterTuplerB   r   s     r   r   r      s    a$'!U;;;!T;;; FGHAe}51<H  15a,q/566 I5s   '!B1B6c                     t        | t        t        f      r"t        | D cg c]  }t	        |       c}      S t        | t
        j                  t        j                  f      rd S t        |       S c c}w r   )
r^   r_   r`   ry   
_FilterIntr   integral_typesr   	Dimensionr|   r   s     r   r   r      sd    !dE]#Q7jm788 a&//1G1GHI 	 a[ 8s   A0c                     t        | t        t        f      r"t        | D cg c]  }t	        |       c}      S t        | t
        j                        rd S t        |       S c c}w r   )r^   r_   r`   ry   _FilterFloatr   
real_typesr|   r   s     r   r   r      sM    !dE]#q9!l1o9::a!2!234D!D :   A c                     t        | t        t        f      r"t        | D cg c]  }t	        |       c}      S t        | t
        j                        rd S t        |       S c c}w r   )r^   r_   r`   ry   _FilterComplexr   complex_typesr|   r   s     r   r   r      sN    !dE]#;AnQ/;<<a!5!564GHQKG <r   c                     t        | t        t        f      r"t        | D cg c]  }t	        |       c}      S t        | t
        j                        ry t        |       S c c}w r   )r^   r_   r`   ry   
_FilterStrr   bytes_or_text_typesr|   r   s     r   r   r      sL    !dE]#Q7jm788!V//0{	 8s   Ac                     t        | t        t        f      r"t        | D cg c]  }t	        |       c}      S t        | t
              rd S t        |       S c c}w r   )r^   r_   r`   ry   _FilterBoolboolr|   r   s     r   r   r      sG    !dE]#a8k!n899a&47HQK7 9s   Ac           
         ||t         vry t         j                  |      }t        |D cg c]
  } ||        c}      }|;t        d|j                  dt        |      dt        |      j                  d      y c c}w )Nz	Expected , got z
 of type 'z
' instead.)_TF_TO_IS_OKgetry   	TypeErrornamereprrX   rs   )valuesr
   fn_listfnmismatchs        r   _Assertconvertibler     sv    }\1u%G7;Rbj;<Hzz4>4>+B+BD
 	
  <s   A>c           
      
   t        | t        j                        r| S |rt        j                  |      }|t        j
                  t        j                  t        j                  t        j                  t        j                  fv }t        | t        j                  t        j                  f      r#|r| j                  |j                        }n| }nt        t!        | dd            st        t!        | dd      t"              rt        j$                  | |      }|} nx| t'        d      |r|j(                  r|j                  }nd}|@t        j*                  |t        j,                        dk(  rt        j.                  ||      }nwt1        | |       t        j2                  | |      }t5        |j6                        t9        |       k7  r3|s1t'        d| dt5        |j6                        d	t9        |       d
      |j:                  t        j<                  k(  r"| |j                  t        j>                        }nV|j:                  t        j,                  k(  r9|7|j                  t        j@                        }t        jB                  ||      r|}t        j                  |j:                        }|tE        d|j:                  z        |r|}|CtG        |d      r|jH                  |jH                  k7  rtE        d|d|j:                  d|       ||j6                  }d}	|jJ                  }
n|D cg c]  }tM        |       }}t        j*                  |t        j,                        }
|
|jJ                  k(  }	|r=|j6                  tO        |      k(  s%tE        dtO        |      d|j6                  d
      |jJ                  |
kD  rt'        d|
|jJ                  fz        t        j                  |jP                  tS        jT                  |      jW                               }|	rK|tX        v rC|
dkD  r>|jJ                  |jZ                  z  dk\  rt'        d      |j]                         |_/        |S |t        j`                  k(  ret        | t        j                        sKtc        |       }	 |D cg c]  }te        jf                  |       }}|jj                  jm                  |       |S |jo                         }tq        |j:                        }|tE        d|jr                  z         |||       |S c c}w c c}w # tD        $ r tE        dti        |       d| d      w xY w)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.
    	__array__N__array_interface__r	   zNone values not supported.r   z!Argument must be a dense tensor: z - got shape z, but wanted .zUnrecognized data type: %s
base_dtypezInconvertible types: z vs. z. Value is TzExpected Tensor's shape: r   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.z!Failed to convert object of type z to Tensor. Contents: z0. Consider casting elements to a supported type.z-Element type not supported in TensorProto: %s):r^   r   TensorProtor   as_dtypeqint8quint8qint16quint16qint32r   ndarraygenericastyper!   callablegetattrdictr   
ValueErroris_numpy_compatibleprodint64emptyr   arrayr_   shaperb   r
   float64float32int32array_equalr   hasattrr   sizeintr`   as_datatype_enumr   as_shapeas_proto_TENSOR_CONTENT_TYPESitemsizetobytestensor_contentstringre   r   rK   rX   rJ   r   ravelr\   r   )r   r
   r   verify_shapeis_quantizednparraynp_dtdowncasted_arraynumpy_dtypeis_same_size
shape_sizedimr   r   r   
str_values	append_fns                    r   make_tensor_protor   $  s   V &*001& L &2::rzz23mmE$8$89GG	'&+t4	5-t4d:
 **V51 >9:: U..((EE bhh!?1!DhhuE2Gvu-hhvU3G W]]#':6'BB$  tGMM24G4OQ  MMRZZ'U]nnRZZ0Gmmrxx'U]&~~bhh7~~.8* //'--0K4w}}DEE E<({555gmmV-
 	
 }\\
%*+cS++WWU"((3
!W\\1==E%L0U|W]]4 
 <<*$Pw||,- 
 ))**!**51::<L
 '<<a<<'***w7P  '.oo&7# fmm#Jvrzz,J(0	6BC&//!,CJC 	&&z2 ==?L !3!34I;k>N>NN
 	
 lL)G ,^ D 	 &*&\6; 	s$   7T8/U 3T=U =U %U'c                 4   | j                   j                  D cg c]  }|j                   }}t        j                  |t        j
                        }t        j                  | j                        }|j                  }| j                  r>t        j                  | j                  |      j                         j                  |      S |t        j                  k(  s|t        j                  k(  rt!        | j"                        dk(  rht        j$                  | j"                  d   t        j&                        }|j                  |_        t        j(                  ||      j                  |      S t        j*                  | j"                  t        j&                        }|j                  |_        |j                  |      S |t        j,                  k(  rt!        | j.                        dk(  rGt        j(                  t        j$                  | j.                  d   |      |      j                  |      S t        j*                  | j.                  |      j                  |      S |t        j0                  k(  rt!        | j2                        dk(  rGt        j(                  t        j$                  | j2                  d   |      |      j                  |      S t        j*                  | j2                  |      j                  |      S |t        j4                  t        j6                  t        j&                  t        j8                  t        j:                  t        j<                  t        j>                  t        j@                  t        jB                  t        jD                  f
v rt!        | jF                        dk(  rGt        j(                  t        j$                  | jF                  d   |      |      j                  |      S t        j*                  | jF                  |      j                  |      S |t        j
                  k(  rt!        | jH                        dk(  rGt        j(                  t        j$                  | jH                  d   |      |      j                  |      S t        j*                  | jH                  |      j                  |      S |t        jJ                  k(  rt!        | jL                        dk(  rGt        j(                  t        j$                  | jL                  d   |      |      j                  |      S t        j$                  tO        | jL                        |      j                  |      S |t        jP                  k(  rtS        | jT                        }t!        | jT                        dk(  r^t        j(                  t        j$                  tW        | jT                  d   | jT                  d         |      |      j                  |      S t        j$                  tY        ||      D cg c]  }tW        |d   |d          c}|      j                  |      S |t        jZ                  k(  rtS        | j\                        }t!        | j\                        dk(  r^t        j(                  t        j$                  tW        | j\                  d   | j\                  d         |      |      j                  |      S t        j$                  tY        ||      D cg c]  }tW        |d   |d          c}|      j                  |      S |t        j^                  k(  rt!        | j`                        dk(  rGt        j(                  t        j$                  | j`                  d   |      |      j                  |      S t        j*                  | j`                  |      j                  |      S tc        d| j                  z        c c}w c c}w c c}w )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.
    r	   r   r      zUnsupported tensor type: %s)2r   r   r   r   r   r   r   r   r
   r!   r   
frombuffercopyreshaper   r    ra   r   r   r   repeatfromiterr   r&   r   r+   r   uint8int16int8r   r   r   r   r   r0   r3   r   rJ   r_   	complex64iterr>   complexzip
complex128rG   r   rN   r   )	tensordr   num_elementstensor_dtyper
   tmpitr   s	            r   make_ndarrayr     s    $00445QVV5E57751L??6<<0L''EMM&//u=TVWU^	

 
	'<6??+J v1$((6??1-RYY?C$33CI99S,/77>>++fooRYY?C$33CI;;u%%		'v A%99))!,E:Lgen ;;v//u=EEeLL		'v  !Q&99**1-U;\gen ;;v00>FFuMM	 
 v~~!#99*%8,gen ;;v~~U;CCEJJ		%v A%99))!,E:Lgen ;;v//u=EEeLL		&v  !Q&99**1-U;\gen 88D!2!235AII%PP	))	)&%%&v""#q(99F//2F4G4G4JK  gen 88.1"bk:1qt$:%gen 
**	*&%%&v""#q(99F//2F4G4G4JK  gen 88.1"bk:1qt$:%gen 
	$v1$99+59<gen ;;ve<DDUKK5DEEQ 6` ; ;s   `4` `)NNF)E__doc__numpyr   tensorboard.compat.protor   "tensorboard.compat.tensorflow_stubr   r   r   r   r   r"   r$   r'   r,   r1   r4   r6   r9   r<   rC   rH   rL   rO   r   r   r   r   r   r   r   uint32uint64r   r   r   r   object_bool_r   r!   r   r   r   r   r[   r    rR   rV   r\   rb   re   	frozensetr   rj   ry   r|   r   r   r   r   r   r   r   r   r   r   r   r   rv   r   r   <module>r      s   ,  / K KBDEBDEEEOCJJ3JJ3 JJ3 HH-	
 HH/ HH- II. II1 II1 GG, HH- LL7 MM9 JJ2 HH.  LL!A!" MM  "B#$ MM  "B
NN!!#C
MM  "B) 2 OO""$H
:L " ( 7EH8
KK+
' ~& NN\N	
 NN\N NN\N LL:, LL:, LL:, KK* MMJ- MMJ- LL:|, NNZ. MMJ-  MMJ<!" MMJ<#$ LL:,%,

L^vFr   