
    BVh                          d Z ddlZddl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  edg      Zd	Z ee      Zd
 Zd Zd Zd Zd Zd Z	 	 ddZy)zComputes a header file to be used with SELECTIVE_REGISTRATION.

See the executable wrapper, print_selective_registration_header.py, for more
information.
    N)text_format)	graph_pb2)gfile)
tf_logging)_pywrap_kernel_registryAccumulateNV2s   Flexc                     t               }t        j                  | d      j                         }|st	        d      t        j                  |      }|D ]  \  }}||r|ndf}|j                  |         |S )z7Gets the ops and kernels needed from the ops list file.rzInput file should not be emptyN)setr   GFileread	Exceptionjsonloadsadd)
input_fileopsops_list_strops_listopkernelop_and_kernels          i/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/tools/selective_registration_header_lib.py_get_ops_from_ops_listr   ,   ss    #Z-224,	
4
55ZZ%( jb&6t4MGGM 
*    c                     t               }|j                  t        | j                               | j                  j
                  D ]&  }|j                  t        |j                               ( |S )z:Gets the ops and kernels needed from the tensorflow model.)r   update_get_ops_from_nodedefsnodelibraryfunctionnode_def)	graph_defr   r!   s      r   _get_ops_from_graphdefr$   9   sW    #**#INN34##,, :hJJ%h&7&789:	*r   c                    | j                   sd| _         t        j                  | j                               }t	        | j
                        }|s|t        v r!||rt	        |j                  d            fS dfS t        j                  d|       y)a  Gets the op and kernel needed from the given NodeDef.

  Args:
    node_def: TF NodeDef to get op/kernel information.

  Returns:
    A tuple of (op_name, kernel_name). If the op is not in the allowlist of ops
    without kernel and there is no kernel found, then return None.
  z/cpu:0zutf-8Nz"Warning: no kernel found for op %s)
devicer   TryFindKernelClassSerializeToStringstrr   OPS_WITHOUT_KERNEL_ALLOWLISTdecoder   warning)r"   kernel_classr   s      r   get_ops_from_nodedefr.   C   s     
HO(;;  "$,
8;;"R77\L''01LLtLL;R@r   c                 f    t               }| D ]!  }t        |      }|s|j                  |       # |S )af  Gets the ops and kernels needed from the list of NodeDef.

  If a NodeDef's op is not in the allowlist of ops without kernel and there is
  no kernel found for this NodeDef, then skip that NodeDef and proceed to the
  next one.

  Args:
    node_defs: list of NodeDef's to get op/kernel information.

  Returns:
    A set of (op_name, kernel_name) tuples.
  )r   r.   r   )	node_defsr   r"   r   s       r   r   r   Y   s:     	# h(2M	ggm 
*r   c                 `   t               }|D ]  }t        j                  d|       | dk(  r|j                  t	        |            }9t        j                  |d      j                         }| dk(  r t        j                  j                  |      }n/| dk(  sJ t        j                  |t        j                               }|j                  t        |            } |rI|dk7  rD|j                  d      D ]0  }|j                  d      \  }}	||	f}
|
|vs |j                  |
       2 t!        |      S )	z5Gets the ops and kernels needed from the model files.zLoading proto file %sr   rbrawproto	textprotoall,:)r   r   infounionr   r   r   r   r   GraphDef
FromStringr   Parser$   splitr   sorted)proto_fileformatproto_filesdefault_ops_strr   
proto_file	file_datar#   sr   r   r   s              r   get_ops_and_kernelsrE   n   s   # 7jOO+Z8:%II,Z89c J-224I:%$$//	:i,,,##Iy/A/A/CDi
))*95
6C7" E1""3' 773<jb&6lm	c	!	 
r   c                 z  	 t        |       } t        d | D              }g 		fd}t        j                  j	                  t
        j                  d         \  }} |d|z          |d        |d       |r |d        |d        |d	       nd
}|dz  }| D ]  \  }}|	|d|z  z  } |dz  } ||        |d        |d        |d        |d       t        |      D ]  } |d|z           |d        |d        |d        |d        |dd|v rdndz           |d       dj                  	      S )a[  Returns a header for use with tensorflow SELECTIVE_REGISTRATION.

  Args:
    ops_and_kernels: a set of (op_name, kernel_class_name) pairs to include.
    include_all_ops_and_kernels: if True, ops_and_kernels is ignored and all op
      kernels are included.

  Returns:
    the string of the header that should be written as ops_to_register.h.
  c              3   &   K   | ]	  \  }}|  y wN ).0r   _s      r   	<genexpr>z2get_header_from_ops_and_kernels.<locals>.<genexpr>   s     ,52qB,s   c                 (    j                  |        y rH   )append)rD   result_lists    r   rN   z/get_header_from_ops_and_kernels.<locals>.append   s    qr   r   z$// This file was autogenerated by %sz#ifndef OPS_TO_REGISTERz#define OPS_TO_REGISTERz##define SHOULD_REGISTER_OP(op) truez+#define SHOULD_REGISTER_OP_KERNEL(clz) truez(#define SHOULD_REGISTER_OP_GRADIENT truea  
    namespace {
      constexpr const char* skip(const char* x) {
        return (*x) ? (*x == ' ' ? skip(x + 1) : x) : x;
      }

      constexpr bool isequal(const char* x, const char* y) {
        return (*skip(x) && *skip(y))
                   ? (*skip(x) == *skip(y) && isequal(skip(x) + 1, skip(y) + 1))
                   : (!*skip(x) && !*skip(y));
      }

      template<int N>
      struct find_in {
        static constexpr bool f(const char* x, const char* const y[N]) {
          return isequal(x, y[0]) || find_in<N - 1>::f(x, y + 1);
        }
      };

      template<>
      struct find_in<0> {
        static constexpr bool f(const char* x, const char* const y[]) {
          return false;
        }
      };
    }  // end namespace
    z6constexpr const char* kNecessaryOpKernelClasses[] = {
z"%s",
z};z#define SHOULD_REGISTER_OP_KERNEL(clz) (find_in<sizeof(kNecessaryOpKernelClasses) / sizeof(*kNecessaryOpKernelClasses)>::f(clz, kNecessaryOpKernelClasses)) z9constexpr inline bool ShouldRegisterOp(const char op[]) {z  return falsez     || isequal(op, "%s")z  ;}z3#define SHOULD_REGISTER_OP(op) ShouldRegisterOp(op)z$#define SHOULD_REGISTER_OP_GRADIENT SymbolicGradienttruefalsez#endif
)r>   r   ospathr=   sysargvjoin)
ops_and_kernelsinclude_all_ops_and_kernelsr   rN   rK   script_nameliner-   r   rO   s
            @r   get_header_from_ops_and_kernelsr_      st    ?+/,O,,#+ 77==!-.![	/+	=>	"#	"# 
01
89
56D6 	EED* '<		
i,&&d' 	DLD
4L
 ) * 2J
FG
Sk /(2-./
5M
3K
@A
2J
1(C/FW> ? 		;	r   c                 V    t        || |      }|st        d       yt        ||dk(        S )a  Computes a header for use with tensorflow SELECTIVE_REGISTRATION.

  Args:
    graphs: a list of paths to GraphDef files to include.
    proto_fileformat: optional format of proto file, either 'textproto',
      'rawproto' (default) or ops_list. The ops_list is the file contain the
      list of ops in JSON format, Ex: "[["Transpose", "TransposeCpuOp"]]".
    default_ops: optional comma-separated string of operator:kernel pairs to
      always include implementation for. Pass 'all' to have all operators and
      kernels included. Default: 'NoOp:NoOp,_Recv:RecvOp,_Send:SendOp'.

  Returns:
    the string of the header that should be written as ops_to_register.h.
  zError reading graph!   r5   )rE   printr_   )graphsr?   default_opsr[   s       r   
get_headerre      s4    " ((8&+N/		
 !	(+:N	OOr   )r3   z#NoOp:NoOp,_Recv:RecvOp,_Send:SendOp)__doc__r   rV   rX   google.protobufr   tensorflow.core.frameworkr   tensorflow.python.platformr   r   tensorflow.python.utilr   	frozensetr*   FLEX_PREFIXlenFLEX_PREFIX_LENGTHr   r$   r.   r   rE   r_   re   rI   r   r   <module>ro      s{     	 
 ' / , 1 :  ) *   
 % 
,*>R l !+@Pr   