
    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 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 ddlmZ ddlmZ ddlmZ ddlmZ dZd Zd Zej:                  d        Zd Zd Z d Z!d Z"d Z#d Z$	 d'dZ%d Z&d Z'd Z(d Z) ed d!       edd"d#      	 	 	 	 	 d(d$              Z*	 d)d%Z+	 	 	 	 	 	 d*d&Z,y)+z3A utility function for importing TensorFlow graphs.    N)	graph_pb2)tf2)pywrap_tf_session)
c_api_utildevice)errors)function)op_def_registry)ops)tensor)control_flow_util)compat)deprecated_args)	tf_exportTc                 $    | j                  d      S )N^)
startswith)
input_names    T/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/framework/importer.py_IsControlInputr   &   s    			s	##    c                    | j                  d      }t        |      dk(  r	 t        |d         }|d   |fS t        |      dk(  r|d   dfS t        d|  d	t        |      dz
         # t        $ r t        d| d|d    d      w xY w)
a  Parses a tensor name into an operation name and output index.

  This function will canonicalize tensor names as follows:

  * "foo:0"       -> ("foo", 0)
  * "foo:7"       -> ("foo", 7)
  * "foo"         -> ("foo", 0)
  * "foo:bar:baz" -> ValueError

  Args:
    tensor_name: The name of a tensor.

  Returns:
    A tuple containing the operation name, and the output index.

  Raises:
    ValueError: If `tensor_name' cannot be interpreted as the name of a tensor.
  :      zCannot convert zX to a tensor name. Second component of the name following the `:` should be an int. Got .r   zCannot convert 'zN' to a tensor name. Tensor names should not contain more than 1 `:`. Obtained )splitlenint
ValueError)tensor_name
componentsoutput_indexs      r   _ParseTensorNamer%   +   s    &   %*_;A'l
 a=,&&
:!a=!
'} 5KJ!+,. / /  ; 8))3Aq: ; ;;s   A( (Bc              #   v   K   | r#t        j                  |       5  d ddd       yd y# 1 sw Y   yxY ww)z=Applies the given device only if device is not None or empty.N)r   r   r   s    r   _MaybeDevicer'   Q   s8      	F	   
 s   9-969c                 <   t        | t        j                        s*	 | }t        j                         } | j                  |       | S | j
                  D ]0  }t        j                  |j                        }|%t        ||       2 | S # t        $ r t	        d      w xY w)z3Type-checks and possibly canonicalizes `graph_def`.z.Argument `graph_def` must be a GraphDef proto.)

isinstancer   GraphDef	MergeFrom	TypeErrornoder   getop_SetDefaultAttrValues)	graph_defold_graph_defr-   op_defs       r   _ProcessGraphDefParamr4   [   s    	Iy11	2Hm$$&i-(& 
  *""477+f	 	D&)* 
%  HFGGHs   'B Bc                     | i } | S t        | t              s!t        dt        |       j                         t        d | j                         D              s%t        dt        | j                                      | S )z3Type-checks and possibly canonicalizes `input_map`.z4Argument `input_map` must be a dictionary. Obtained c              3   P   K   | ]  }t        |t        j                           y wNr)   r   bytes_or_text_types).0ks     r   	<genexpr>z(_ProcessInputMapParam.<locals>.<genexpr>   s$      M67
1f001M   $&zBAll keys for argument `input_map` must be strings. Obtained keys: )r)   dictr,   type__name__allkeyslist)	input_maps    r   _ProcessInputMapParamrE   y   s    I 
 i&Li1124 5 5 M;D>>;KM M ((,Y^^-=(>'?A B B	r   c                 n    | yt        d | D              st        d|  d      t        d | D              S )z9Type-checks and possibly canonicalizes `return_elements`.Nc              3   P   K   | ]  }t        |t        j                           y wr7   r8   r:   xs     r   r<   z._ProcessReturnElementsParam.<locals>.<genexpr>   s$      J45jF../Jr=   z?Argument `return_elements` must be a list of strings. Obtained r   c              3   F   K   | ]  }t        j                  |        y wr7   )r   as_strrH   s     r   r<   z._ProcessReturnElementsParam.<locals>.<genexpr>   s     9Av}}Q9s   !)rA   r,   tuple)return_elementss    r   _ProcessReturnElementsParamrN      sS    	 J9HJ 
J
   /03 4 4	99	99r   c                 L    |j                   D ]  }| |j                  k(  s|c S  y r7   )attrname)	attr_namer3   attr_defs      r   _FindAttrInOpDefrT      s+    ++ hHMM!o 
r   c                    | j                   D ci c]  }|j                  | }}|j                  D ]  }|j                   |v st        j                  |j                         }|4||j                      }t        |j                        D ]Z  }t        ||      t        ||      }|s|j                  d      s1|j                  |   |j                  k(  sN|j                  |= \  yc c}w )a   Removes unknown default attrs according to `producer_op_list`.

  Removes any unknown attrs in `graph_def` (i.e. attrs that do not appear in
  registered OpDefs) that have a default value in `producer_op_list`.

  Args:
    producer_op_list: OpList proto.
    graph_def: GraphDef proto
  Ndefault_value)
r/   rQ   r-   r   r.   rC   rP   rT   HasFieldrV   )	producer_op_listr1   r/   producer_op_dictr-   r3   producer_op_defkeyrS   s	            r   _RemoveDefaultAttrsr\      s     -=,?,?@bbggrk@@nn dww""""477+f	 	(1o dii #C(0%c?;(8,,_=iin 6 66 		# As   C)c           	      8   t        d |j                         D              sf| dk(  rt        d      t        j                  d      5  |j                         D ci c]  \  }}|t        j                  |       }}}ddd       |S |S c c}}w # 1 sw Y   |S xY w)a  Ensures all input map values are tensors.

  This should be called from inside the import name scope.

  Args:
    name: the `name` argument passed to import_graph_def
    input_map: the `input_map` argument passed to import_graph_def.

  Returns:
    An possibly-updated version of `input_map`.

  Raises:
    ValueError: if input map values cannot be converted due to empty name scope.
  c              3   P   K   | ]  }t        |t        j                           y wr7   )r)   r   Tensor)r:   vs     r   r<   z)_ConvertInputMapValues.<locals>.<genexpr>   s     FaZ6==)Fr=    ztf.import_graph_def() requires a non-empty `name` if `input_map` contains non-Tensor values. Try calling tf.convert_to_tensor() on `input_map` values before calling tf.import_graph_def()._inputsN)rA   valuesr!   r   
name_scopeitemsconvert_to_tensor)rQ   rD   r;   r`   s       r   _ConvertInputMapValuesrg      s     
F93C3C3EF	FrzEF F 
		" N;D??;LM41a1c++A..MiMN	 NN	s   B B	:B	BBc                 @   t        j                  | |       t        j                  | d       t        j                  | |       |j	                         D ]  \  }}t        j                  |      }|j                  d      rJt        j                  |dd       }|j                         j                  }	t        j                  | ||	       vt        |      \  }}
t        j                  |      }|j                         }t        j                  | ||
|        |xs g D ]j  }d|v r;t        |      \  }}t        j                  |      }t        j                  | ||       Bt        j                  | t        j                  |             l t        j                  | |       y)z1Populates the TF_ImportGraphDefOptions `options`.Tr   r   Nr   )c_api!TF_ImportGraphDefOptionsSetPrefix(TF_ImportGraphDefOptionsSetUniquifyNames.TF_ImportGraphDefOptionsSetPropagateDeviceSpecre   r   rK   r   _as_tf_outputoper.TF_ImportGraphDefOptionsRemapControlDependencyr%   'TF_ImportGraphDefOptionsAddInputMapping'TF_ImportGraphDefOptionsAddReturnOutput*TF_ImportGraphDefOptionsAddReturnOperation8TF_ImportGraphDefOptionsSetValidateColocationConstraints)optionsprefixrD   rM   validate_colocation_constraintspropagate_device_spec	input_src	input_dstsrc_namedst_opsrc_idx
dst_outputrQ   op_nameindexs                  r    _PopulateTFImportGraphDefOptionsr      su   
 ))'6:00$?66w7LN (oo/ @ii(IC y}-h&&(--f::
8V% +95hx(h**,j33GXw4>@@ # Ld
d{'-ngug&g33GWeL66w7=}}T7JLL @@.0r   c           	         i }| j                  d      D ]W  }|j                  }|j                  d       t        |      }|r|||<   3t	        |      5  | j                  |       ddd       Y |j                         D ]k  \  }}d}|D ]K  }	 | j                  |      }	|	j                  s"t        j                   j#                  |	j                        } n |s[|j                  |       m y# 1 sw Y   xY w# t        $ rG t        j                         st        j                  |       rY t        d| d|j                         w xY w)z3Processes the newly-added TF_Operations in `graph`.F)compute_devicesra   NzSpecified colocation to an op: z+ that does not exist during import for op: )_add_new_tf_operationsr   _set_device_GetColocationNamesr'   _apply_device_functionsre   _get_operation_by_nameKeyErrorr   enabledr   EnableControlFlowV2r!   rQ   pydev
DeviceSpecfrom_string)
graphcolocation_pairsnew_oporiginal_devicecolocation_namesr/   coloc_op_listcoloc_devicecoloc_op_namecoloc_ops
             r   _ProcessNewOpsr      sc    ,,U,C .fmmO
r*62!1v ( .%%f-. .." ,113 #b-L ' L//> 
''33HOOD nn\"5#. .*  L;;=-AA%H
:=/ JAAC	K L 	LLs   C.C:.C7	:2E
.E
c                    g }	 | j                  d      }|D ]X  }t        j                  |      }|j	                  d      s*|t        d      d }|| j                  k7  sH|j                  |       Z |S # t        $ r Y yw xY w)z<Returns names of the ops that `op` should be colocated with._classNzloc:@)get_attrr!   r   rK   r   r   rQ   append)r/   r   class_valuesvalcolocation_node_names        r   r   r   -  s    ;;x(L  6c
--
C
~~g W/		( 456 
 
 
s   A4 4	B ?B c                    t        j                  |      }t        j                  |      }g }d}d}| D ]W  }d|v r)|j                  |j	                  ||                |dz  }0|j                  |j                  ||                |dz  }Y |S )a  Returns the requested return elements from results.

  Args:
    requested_return_elements: list of strings of operation and tensor names
    graph: Graph
    results: wrapped TF_ImportGraphDefResults

  Returns:
    list of `Operation` and/or `Tensor` objects
  r   r   r   )ri   %TF_ImportGraphDefResultsReturnOutputs(TF_ImportGraphDefResultsReturnOperationsr   _get_tensor_by_tf_output_get_operation_by_tf_operation)	requested_return_elementsr   resultsreturn_outputsreturn_operscombined_return_elementsoutputs_idx	opers_idxrQ   s	            r   _GatherReturnElementsr   >  s     >>wG.??H,+)' d
d{%%

(
()D
EGQk%%

.
.|I/F
GI1ni 
"!r   c                 .   | j                   |j                  k(  sJ |j                  D ]k  }|j                  }|j                  d      s!| j                  |   }||j	                  d      D| j                  |   j                  |j                         m y)z>Set any default attr values in `node_def` that aren't present.rV   Nvalue)r/   rQ   rP   rW   
WhichOneofCopyFromrV   )node_defr3   rS   r[   r   s        r   r0   r0   [  s    		##	#++ <h
--C)mmC e	%**73;c##H$:$:;<r   zgraph_util.import_graph_defimport_graph_defzfPlease file an issue at https://github.com/tensorflow/tensorflow/issues if you depend on this feature.op_dictc                 $    ~t        | ||||      S )a  Imports the graph from `graph_def` into the current default `Graph`.

  This function provides a way to import a serialized TensorFlow
  [`GraphDef`](https://www.tensorflow.org/code/tensorflow/core/framework/graph.proto)
  protocol buffer, and extract individual objects in the `GraphDef` as
  `tf.Tensor` and `tf.Operation` objects. Once extracted,
  these objects are placed into the current default `Graph`. See
  `tf.Graph.as_graph_def` for a way to create a `GraphDef`
  proto.

  Args:
    graph_def: A `GraphDef` proto containing operations to be imported into
      the default graph.
    input_map: A dictionary mapping input names (as strings) in `graph_def`
      to `Tensor` objects. The values of the named input tensors in the
      imported graph will be re-mapped to the respective `Tensor` values.
    return_elements: A list of strings containing operation names in
      `graph_def` that will be returned as `Operation` objects; and/or
      tensor names in `graph_def` that will be returned as `Tensor` objects.
    name: (Optional.) A prefix that will be prepended to the names in
      `graph_def`. Note that this does not apply to imported function names.
      Defaults to `"import"`.
    op_dict: (Optional.) Deprecated, do not use.
    producer_op_list: (Optional.) An `OpList` proto with the (possibly stripped)
      list of `OpDef`s used by the producer of the graph. If provided,
      unrecognized attrs for ops in `graph_def` that have their default value
      according to `producer_op_list` will be removed. This will allow some more
      `GraphDef`s produced by later binaries to be accepted by earlier binaries.

  Returns:
    A list of `Operation` and/or `Tensor` objects from the imported graph,
    corresponding to the names in `return_elements`,
    and None if `returns_elements` is None.

  Raises:
    TypeError: If `graph_def` is not a `GraphDef` proto,
      `input_map` is not a dictionary mapping strings to `Tensor` objects,
      or `return_elements` is not a list of strings.
    ValueError: If `input_map`, or `return_elements` contains names that
      do not appear in `graph_def`, or `graph_def` is not well-formed (e.g.
      it refers to an unknown tensor).
  )rD   rM   rQ   rX   _import_graph_def_internal)r1   rD   rM   rQ   r   rX   s         r   r   r   f  s$    h 	#%'
) )r   c                      t        | d||      S )zCLike import_graph_def but does not validate colocation constraints.F)rv   rQ   rw   r   )r1   rQ   rw   s      r   import_graph_def_for_functionr     s     
$&+1	
3 3r   c                     t        |       } t        |      }t        |      }|t        ||        t	        j
                         }t	        j                  |d|j                               5 }|r|j                  d      sJ |dd }	nd}	t        ||      }ddd       t        j                         }
|
j                  }t        |	||||       |j                         5  t        rGt!        j"                  t%        j&                  | j)                                     }t         j*                  }n| }t         j,                  }	 |j.                  j1                         5 } ||||      }ddd       t        j2                        }	 t        rt!        j<                  |       	 t?        |       ddd       | j@                  rM| j@                  jB                  r7tC        jD                  | j@                        }|D ]  }|jG                  |        t!        jH                  jJ                        }|rB|D cg c]  }t%        jL                  |       }}djO                  |      }t9        d| d      |ytQ        |||jJ                        S # 1 sw Y   xY w# 1 sw Y   2xY w# t4        j6                  $ r}t9        t;        |            d}~ww xY w# t        rt!        j<                  |       w w xY w# 1 sw Y   JxY wc c}w )	ag	  Imports the graph from `graph_def` into the current default `Graph`.

  This function provides a way to import a serialized TensorFlow
  [`GraphDef`](https://www.tensorflow.org/code/tensorflow/core/framework/graph.proto)
  protocol buffer, and extract individual objects in the `GraphDef` as
  `tf.Tensor` and `tf.Operation` objects. Once extracted,
  these objects are placed into the current default `Graph`. See
  `tf.Graph.as_graph_def` for a way to create a `GraphDef`
  proto.

  Args:
    graph_def: A `GraphDef` proto containing operations to be imported into the
      default graph.
    input_map: A dictionary mapping input names (as strings) in `graph_def` to
      `Tensor` objects. The values of the named input tensors in the imported
      graph will be re-mapped to the respective `Tensor` values.
    return_elements: A list of strings containing operation names in `graph_def`
      that will be returned as `Operation` objects; and/or tensor names in
      `graph_def` that will be returned as `Tensor` objects.
    validate_colocation_constraints: Whether to validate colocation constraints.
    name: (Optional.) A prefix that will be prepended to the names in
      `graph_def`. Note that this does not apply to imported function names.
      Defaults to `"import"`.
    producer_op_list: (Optional.) An `OpList` proto with the (possibly stripped)
      list of `OpDef`s used by the producer of the graph. If provided,
      unrecognized attrs for ops in `graph_def` that have their default value
      according to `producer_op_list` will be removed. This will allow some more
      `GraphDef`s produced by later binaries to be accepted by earlier binaries.
    propagate_device_spec: Whether to propagate assigned device information
      when importing a graph from a GraphDef into the current default `Graph`.

  Returns:
    A list of `Operation` and/or `Tensor` objects from the imported graph,
    corresponding to the names in `return_elements`,
    and None if `returns_elements` is None.

  Raises:
    TypeError: If `graph_def` is not a `GraphDef` proto,
      `input_map` is not a dictionary mapping strings to `Tensor` objects,
      or `return_elements` is not a list of strings.
    ValueError: If `input_map`, or `return_elements` contains names that
      do not appear in `graph_def`, or `graph_def` is not well-formed (e.g.
      it refers to an unknown tensor).
  Nimport/ra   z, z;Attempted to map inputs that were not found in graph_def: []))r4   rE   rN   r\   r   get_default_graphrd   rc   endswithrg   r   ScopedTFImportGraphDefOptionsrt   r   _mutation_lockis_ossri   TF_NewBufferFromStringr   as_bytesSerializeToString!TF_GraphImportGraphDefWithResults0TF_GraphImportGraphDefWithResultsNoSerialization_c_graphr.   ScopedTFImportGraphDefResultsr	   InvalidArgumentErrorr!   strTF_DeleteBufferr   libraryr
   from_libraryadd_to_graph:TF_ImportGraphDefResultsMissingUnusedInputMappings_wrapperr   rK   joinr   )r1   rD   rM   rv   rQ   rX   rw   r   scoperu   scoped_optionsrt   graph_def_inputgraph_import_graphdefc_graphr   e	functionsfmissing_unused_input_keyssmissing_keyss                         r   r   r     s   h $I.)#I.)/@/!()4



!%
~~dHi&6&6&89 	8U^^C   Sbzff 'tY7I	8 ;;=.""'"7FI#B#8:   44
//)557
8o $EE!o

@
@ 	/>> K7''JK88Ag
 
o. 5A D 9,,55%%i&7&78I nnU FF
//  ";!a! ! 9967L
	>	   %IIY	8 	8@K K && s1v 
o. 
'   Z!sg   )*JA K.<JJ!J?'K.,K;JJ	JK
1KK

KK++K..K8)F)NNNNN)NF)NNTNNF)-__doc__
contextlibtensorflow.core.frameworkr   tensorflow.pythonr   tensorflow.python.clientr   ri   tensorflow.python.frameworkr   r   r   r	   r
   r   r   r   tensorflow.python.opsr   tensorflow.python.utilr   "tensorflow.python.util.deprecationr    tensorflow.python.util.tf_exportr   r   r   r%   contextmanagerr'   r4   rE   rN   rT   r\   rg   r   r   r   r   r0   r   r   r    r   r   <module>r      s   :  / ! ? 2 7 . 0 7 + . 3 ) > 6 
$
#/L 
 
<:B: <A!0H1#h"":< (*<= %&/1  $%)!&*6)1 >6)t 163 $(	IJr   