
    BVh                     d    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	 Zd
 Zy)z4Utilities to remove unneeded nodes from a GraphDefs.    N)text_format)attr_value_pb2)	graph_pb2)node_def_pb2)
graph_util)gfilec                 N   |D ]  }d|v st        d| d       |D ch c]  }| }}t        j                         }| j                  D ]  }|j                  |v rj|j                  |j                         t        j                         }d|_        |j                  |_        t        |t              rQ|j                  |j                        }	|j                  d   j                  t        j                  ||	                n2|j                  d   j                  t        j                  |             d|j                  v r+|j                  d   j                  |j                  d          d|j                  v r+|j                  d   j                  |j                  d          |j                  j!                  |g       }|j                  j!                  t#        j$                  |      g        |rt'        d	| d
      t)        j*                  ||      }
|
S c c}w )af  Removes unused nodes from a GraphDef.

  Args:
    input_graph_def: A graph with nodes we want to prune.
    input_node_names: A list of the nodes we use as inputs.
    output_node_names: A list of the output nodes.
    placeholder_type_enum: The AttrValue enum for the placeholder data type, or
        a list that specifies one value per input node name.

  Returns:
    A `GraphDef` with all unnecessary ops removed.

  Raises:
    ValueError: If any element in `input_node_names` refers to a tensor instead
      of an operation.
    KeyError: If any element in `input_node_names` is not found in the graph.
  :zName 'z1' appears to refer to a Tensor, not an Operation.Placeholderdtype)type_output_shapesshapez*The following input nodes were not found: .)
ValueErrorr   GraphDefnodenameremover   NodeDefop
isinstancelistindexattrCopyFromr   	AttrValueextendcopydeepcopyKeyErrorr   extract_sub_graph)input_graph_definput_node_namesoutput_node_namesplaceholder_type_enumr   	not_foundinputs_replaced_graph_defr   placeholder_nodeinput_node_indexoutput_graph_defs              X/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/tools/strip_unused_lib.pystrip_unusedr-      s
   &  %d
d{v &$ $ % %% !11t1)1'002"" Cdyy$$tyy!%--/)"ii	)4	0+11$))<g&//$$*? +" #	$ 	g&//$$*?@	B	TYY	&./88: 		DII	g&//		'0BC$$++-=,>?$$++T]]4-@,AB+C. 
?	{!L
MM112K2CE	= 2s   	H"c                 V   t        j                  |       st        d| z   dz          y|st        d       yt        j                         }|rdnd}t        j
                  | |      5 }	|r |j                  |	j                                n$t        j                  |	j                         |       ddd       t        ||j                  d      |j                  d      |      }
|r?t        j
                  |d	      5 }	|	j                  |
j                                ddd       nCt        j
                  |d
      5 }	|	j                  t        j                  |
             ddd       t        dt        |
j                         z         y# 1 sw Y   xY w# 1 sw Y   7xY w# 1 sw Y   CxY w)z'Removes unused nodes from a graph file.zInput graph file 'z' does not exist!z=You need to supply the name of a node to --output_node_names.rbrN,wbwz%d ops in the final graph.)r   Existsprintr   r   GFileParseFromStringreadr   Merger-   splitwriteSerializeToStringMessageToStringlenr   )input_graphinput_binaryoutput_graphoutput_binaryr$   r%   r&   r#   modefr+   s              r,   strip_unused_from_filesrF   Y   sk   
 
k	"	

,/B
BC		
IJ&&(/3${{;% 3%%affh//2	3 "/"2"8"8"="3"9"9#">"79
 	\4	( 4Agg00234 4 
\3	' =1ggk))*:;<=$s+;+@+@'AAB#3 34 4= =s%   &AF9 F8%FFFF()__doc__r   google.protobufr   tensorflow.core.frameworkr   r   r   tensorflow.python.frameworkr   tensorflow.python.platformr   r-   rF        r,   <module>rN      s+     ;  ' 4 / 2 2 ,9x CrM   