
    BVh                     (   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
  G d d	e      Z G d
 de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Ze
j*                  dd       Zd Zy)z)Utilities for using the TensorFlow C API.    N)api_def_pb2)
op_def_pb2)pywrap_tf_session)compat)tf_contextlibc                        e Zd Z fdZ xZS )AlreadyGarbageCollectedErrorc                 8    t         t        |   | d| d       y )Nz	 of type z9 has already been garbage collected and cannot be called.)superr	   __init__)selfnameobj_type	__class__s      V/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/framework/c_api_util.pyr   z%AlreadyGarbageCollectedError.__init__   s+    	
&
 68* 5; < =    )__name__
__module____qualname__r   __classcell__r   s   @r   r	   r	      s    = =r   r	   c                   H    e Zd ZdZg dZd Zej                  d        Zd Z	y)	UniquePtrzDWrapper around single-ownership C-API objects that handles deletion.)_objdeleterr   	type_namec                 `    || _         || _        || _        t        t	        |            | _        y N)r   r   r   strtyper   )r   r   objr   s       r   r   zUniquePtr.__init__*   s*     DIDI
 DLc^DNr   c              #      K   | j                    t        | j                  | j                        | j                    yw)zYields the managed C-API Object, guaranteeing aliveness.

    This is a context manager. Inside the context the C-API object is
    guaranteed to be alive.

    Raises:
      AlreadyGarbageCollectedError: if the object is already deleted.
    N)r   r	   r   r   r   s    r   getzUniquePtr.get6   s0      yy(DNNCC
))Os   <>c                 R    | j                   }|d | _         | j                  |       y y r   )r   r   )r   r!   s     r   __del__zUniquePtr.__del__F   s(    
))C
di
ll3 r   N)
r   r   r   __doc__	__slots__r   
contextlibcontextmanagerr$   r&    r   r   r   r   %   s.    L6)
$  r   r   c                   "    e Zd ZdZdgZd Zd Zy)ScopedTFStatusz/Wrapper around TF_Status that handles deletion.statusc                 6    t        j                         | _        y r   )c_apiTF_NewStatusr.   r#   s    r   r   zScopedTFStatus.__init__R   s    $$&DKr   c                 r    t         1t         j                   t        j                  | j                         y y y r   )r0   TF_DeleteStatusr.   r#   s    r   r&   zScopedTFStatus.__del__U   s/     U22>DKK( ?r   Nr   r   r   r'   r(   r   r&   r+   r   r   r-   r-   M   s    7j)')r   r-   c                   "    e Zd ZdZdgZd Zd Zy)ScopedTFImportGraphDefOptions>Wrapper around TF_ImportGraphDefOptions that handles deletion.optionsc                 6    t        j                         | _        y r   )r0   TF_NewImportGraphDefOptionsr8   r#   s    r   r   z&ScopedTFImportGraphDefOptions.__init__a   s    446DLr   c                 r    t         1t         j                   t        j                  | j                         y y y r   )r0   TF_DeleteImportGraphDefOptionsr8   r#   s    r   r&   z%ScopedTFImportGraphDefOptions.__del__d   0     UAAM**4<<8 Nr   Nr4   r+   r   r   r6   r6   \   s    Fk)79r   r6   c                   "    e Zd ZdZdgZd Zd Zy)ScopedTFImportGraphDefResultsr7   resultsc                     || _         y r   )r@   )r   r@   s     r   r   z&ScopedTFImportGraphDefResults.__init__p   s	    DLr   c                 r    t         1t         j                   t        j                  | j                         y y y r   )r0   TF_DeleteImportGraphDefResultsr@   r#   s    r   r&   z%ScopedTFImportGraphDefResults.__del__s   r=   r   Nr4   r+   r   r   r?   r?   k   s    Fk)9r   r?   c                   "     e Zd ZdZ fdZ xZS )ScopedTFFunctionz1Wrapper around TF_Function that handles deletion.c                 N    t         t        |   ||t        j                         y )N)r   r!   r   )r   rE   r   r0   TF_DeleteFunction)r   funcr   r   s      r   r   zScopedTFFunction.__init__}   s%    	
D*tU%<%< + >r   )r   r   r   r'   r   r   r   s   @r   rE   rE   z   s    9> >r   rE   c                   "    e Zd ZdZdgZd Zd Zy)ScopedTFBufferz8An internal class to help manage the TF_Buffer lifetime.bufferc                 ^    t        j                  t        j                  |            | _        y r   )r0   TF_NewBufferFromStringr   as_bytesrK   )r   
buf_strings     r   r   zScopedTFBuffer.__init__   s    ..vz/JKDKr   c                 B    t        j                  | j                         y r   )r0   TF_DeleteBufferrK   r#   s    r   r&   zScopedTFBuffer.__del__   s    	$++&r   Nr4   r+   r   r   rJ   rJ      s    @j)L'r   rJ   c                   <    e Zd ZdZddgZd Zd Zd Zd Zd Z	d	 Z
y
)	ApiDefMapzWrapper around Tf_ApiDefMap that handles querying and deletion.

  The OpDef protos are also stored in this class so that they could
  be queried by op name.
  _api_def_map_op_per_namec                    t        j                         }t        j                         }	 |j	                  t        j
                  |             t        j                  |      | _        t        j                  |       i | _	        |j                  D ]  }|| j                  |j                  <    y # t        j                  |       w xY wr   )r   OpListr0   TF_GetAllOpListParseFromStringTF_GetBufferTF_NewApiDefMaprT   rQ   rU   opr   )r   op_def_protobufr\   s       r   r   zApiDefMap.__init__   s    $$&L



!C!""5#5#5c#:;//4dC Doo &#%d & C s   >B/ /Cc                 r    t         1t         j                   t        j                  | j                         y y y r   )r0   TF_DeleteApiDefMaprT   r#   s    r   r&   zApiDefMap.__del__   s2     U55At001 Br   c                 X    t        j                  | j                  |t        |             y r   )r0   TF_ApiDefMapPutrT   len)r   texts     r   put_api_defzApiDefMap.put_api_def   s    	$++T3t9=r   c                 *   t        j                         }t        j                  | j                  |t        |            }	 |j                  t        j                  |             t        j                  |       |S # t        j                  |       w xY wr   )	r   ApiDefr0   TF_ApiDefMapGetrT   rc   rY   rZ   rQ   )r   op_nameapi_def_protor^   s       r   get_api_defzApiDefMap.get_api_def   sr    &&(M


 1 17CL
IC!##E$6$6s$;<C  C s    $A; ;Bc                 Z    || j                   v r| j                   |   S t        d| d      )NzNo op_def found for op name .)rU   
ValueError)r   ri   s     r   
get_op_defzApiDefMap.get_op_def   s6    $###w''
3G9A>
??r   c                 6    | j                   j                         S r   )rU   keysr#   s    r   op_nameszApiDefMap.op_names   s    !!##r   N)r   r   r   r'   r(   r   r&   re   rk   ro   rr   r+   r   r   rS   rS      s2     ~.)&2>@
$r   rS   c              #      K   | r)t        j                  t        j                  |             }nt        j                         }	 | t        j
                  |       y# t        j
                  |       w xY ww)a$  Context manager that creates and deletes TF_Buffer.

  Example usage:
    with tf_buffer() as buf:
      # get serialized graph def into buf
      ...
      proto_data = c_api.TF_GetBuffer(buf)
      graph_def.ParseFromString(compat.as_bytes(proto_data))
    # buf has been deleted

    with tf_buffer(some_string) as buf:
      c_api.TF_SomeFunction(buf)
    # buf has been deleted

  Args:
    data: An optional `bytes`, `str`, or `unicode` object. If not None, the
      yielded buffer will contain this data.

  Yields:
    Created TF_Buffer
  N)r0   rM   r   rN   TF_NewBufferrQ   )datar^   s     r   	tf_bufferrv      sX     . 


&
&vt'<
=C



C
I	#E#s   A A7A A7A44A7c                 J    t        j                         }| |_        ||_        |S )zReturns a wrapped TF_Output with specified operation and index.

  Args:
    c_op: wrapped TF_Operation
    index: integer

  Returns:
    Wrapped TF_Output
  )r0   	TF_Outputoperindex)c_oprz   rets      r   	tf_outputr}      s#     	##(#)	*r   r   )r'   r)   tensorflow.core.frameworkr   r   tensorflow.python.clientr   r0   tensorflow.python.utilr   r   	Exceptionr	   objectr   r-   r6   r?   rE   rJ   rS   r*   rv   r}   r+   r   r   <module>r      s     0  1 0 ? ) 0=9 =% %P)V )9F 99F 9>y >	'V 	'.$ .$b  @r   