
    BVh1                         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 Zd Z G d d      Zy)z6Utilities for managing tf.data user-defined functions.    N)ag_ctx)api)
debug_mode)nest)	structure)context)def_function)function)ops)
script_ops)function_utils)variable_utilsc                 "    t        | t              S )a   Determines whether the caller needs to pack the argument in a tuple.

  If user-defined function returns a list of tensors, `nest.flatten()` and
  `ops.convert_to_tensor()` and would conspire to attempt to stack those tensors
  into a single tensor because the tf.data version of `nest.flatten()` does
  not recurse into lists. Since it is more likely that the list arose from
  returning the result of an operation (such as `tf.numpy_function()`) that
  returns a list of not-necessarily-stackable tensors, we treat the returned
  value as a `tuple` instead. A user wishing to pack the return value into a
  single tensor can use an explicit `tf.stack()` before returning.

  Args:
    arg: argument to check

  Returns:
    Indication of whether the caller needs to pack the argument in a tuple.
  )
isinstancelistargs    ^/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/data/ops/structured_function.py_should_packr   "   s    $ 
C	    c                 $    t        |       t        u S )zDetermines whether the caller needs to unpack the argument from a tuple.

  Args:
    arg: argument to check

  Returns:
    Indication of whether the caller needs to unpack the argument from a tuple.
  )typetupler   s    r   _should_unpackr   7   s     
ce	r   c                   x    e Zd ZdZ	 	 	 	 	 	 	 	 d	dZed        Zed        Zed        Zed        Z	ed        Z
y)
StructuredFunctionWrapperzHA function wrapper that supports structured arguments and return values.Nc                 >    |S|.|||t        d      t        j                  |||       _        n=|||t        d      |j                   _        n||||t        d      | _        | _        |
i }
t              dkD  rj                  dd      dd nd}dj                  |t        j                  |      g      }d	D ]  }|j                  |d      } t        j                          fd
 fd} fd} fd}|	r=|
j                  d|dz   t        t        j                                z   i        ||
      }np|
j                  d|i       |
j                  ddi       t"        j$                  r	 ||
      }n1t'        j(                         rt+        j,                  d        ||
      } |        _        |t1        j2                          z  }||	z  }|r- j.                  j5                  t        j6                                |	st        j6                         j8                  }|rd j.                  j:                  j8                  |k(  r@ j.                  j:                  j<                  rt+        j,                  d|d|dd       yyyyy)a1  Creates a new `StructuredFunctionWrapper` for the given function.

    Args:
      func: A function from a (nested) structure to another (nested) structure.
      transformation_name: Human-readable name of the transformation in which
        this function is being instantiated, for error messages.
      dataset: (Optional.) A `tf.data.Dataset`. If given, the structure of this
        dataset will be assumed as the structure for `func` arguments; otherwise
        `input_classes`, `input_shapes`, and `input_types` must be defined.
      input_classes: (Optional.) A (nested) structure of `type`. If given, this
        argument defines the Python types for `func` arguments.
      input_shapes: (Optional.) A (nested) structure of `tf.TensorShape`. If
        given, this argument defines the shapes and structure for `func`
        arguments.
      input_types: (Optional.) A (nested) structure of `tf.DType`. If given,
        this argument defines the element types and structure for `func`
        arguments.
      input_structure: (Optional.) A `Structure` object. If given, this argument
        defines the element types and structure for `func` arguments.
      add_to_graph: (Optional.) If `True`, the function will be added to the
        default graph, if it exists.
      use_legacy_function: (Optional.) A boolean that determines whether the
        function be created using `tensorflow.python.eager.function.defun`
        (default behavior) or `tensorflow.python.framework.function.Defun`
        (legacy behavior).
      defun_kwargs: (Optional.) A dictionary mapping string argument names to
        values. If supplied, will be passed to `function` as keyword arguments.

    Raises:
      ValueError: If an invalid combination of `dataset`, `input_classes`,
        `input_shapes`, and `input_types` is passed.
    NzsEither `dataset`, `input_structure` or all of `input_classes`, `input_shapes`, and `input_types` must be specified.ztEither `dataset`, `input_structure`, or all of `input_classes`, `input_shapes`, and `input_types` must be specified.   ._ )<>\' c                     t        j                  j                  |       }t        |      s|f} t	        j
                  j                        | }t        j                  |      }t        |      rt        |      }	 t        j                  |      _        |S # t        t        f$ r}t        d d| d      |d}~ww xY w)zDWrapper for passing nested structures to and from tf.data functions.z1Unsupported return value from function passed to z: r   N)r   from_compatible_tensor_list_input_structurer   	autograph
tf_convert_funcr   convert_variables_to_tensorsr   r   type_spec_from_value_output_structure
ValueError	TypeError)argsnested_argsreter   selftransformation_names       r   wrapper_helperz:StructuredFunctionWrapper.__init__.<locals>.wrapper_helper   s    99


'kK("n4I  V4kBc77<c	c	CjA!*!?!?!D j )$ AK./r#a9 :?@	AAs   ?B C*B==Cc                     t        j                  t        j                  j                        i | fd       fdS )Nc                  N     |  }t        j                  j                  |      S N)r   to_tensor_listr0   )r3   r5   r7   r9   s     r   
wrapped_fnzUStructuredFunctionWrapper.__init__.<locals>.trace_legacy_function.<locals>.wrapped_fn   s(     d#''(>(>DDr   c                       S r<    )r>   s   r   <lambda>zSStructuredFunctionWrapper.__init__.<locals>.trace_legacy_function.<locals>.<lambda>   s    Z r   )r
   Defunr   get_flat_tensor_typesr*   )defun_kwargsr>   r7   r9   s    @r   trace_legacy_functionzAStructuredFunctionWrapper.__init__.<locals>.trace_legacy_function   sC    ~~y66t7L7LM &$&E&E  r   c                 b   fd}| j                  dd      }t        j                  ||t        j                  j
                        d|       }|j                         }fdt        j                  t        j                  j
                        d|       fd       }|j                  S )	Nc                       |  }t        j                  j                  |      }|D cg c]  }t        j                  |       c}S c c}w r<   r   r=   r0   r   convert_to_tensorr3   r5   tr7   r9   s      r   unusedzMStructuredFunctionWrapper.__init__.<locals>.trace_py_function.<locals>.unused   D    d#&&t'='=sC256Q%%a(666   A
	func_namerL   Fpython_functionnameinput_signaturer+   experimental_attributesc                  :   t        j                  j                  |       }t        |      s|f} j                  | }t        |      rt        |      }t        j                  j                  |      }|D cg c]  }t        j                  |       c}S c c}w r<   )r   r)   r*   r   r-   r   r   r=   r0   r   rI   )r3   r4   r5   rK   r7   s       r   py_function_wrapperzZStructuredFunctionWrapper.__init__.<locals>.trace_py_function.<locals>.py_function_wrapper   s    ;;!!4)k*$+djj+&c
#&&t'='=sC256Q%%a(666s   9B)rS   r+   rT   c                  l    t        j                  | t        j                  j                              S r<   )r   eager_py_funcr   rC   r0   )r3   rV   r7   s    r   r>   zQStructuredFunctionWrapper.__init__.<locals>.trace_py_function.<locals>.wrapped_fn   s3     ''++D,B,BCE 	Er   )popr	   Functionr   get_flat_tensor_specsr*   get_concrete_functionr
   )	rD   rL   rO   tf_functionr    r>   rV   r7   r9   s	         @r   trace_py_functionz=StructuredFunctionWrapper.__init__.<locals>.trace_py_function   s    7
 "";9i )) #99## ".k 
+
+
-a	7 #99##%".	0
E0
E
 ---r   c                     fd}| j                  dd      }t        j                  ||t        j                  j
                        d|       }|j                  S )Nc                       |  }t        j                  j                  |      }|D cg c]  }t        j                  |       c}S c c}w r<   rH   rJ   s      r   r>   zQStructuredFunctionWrapper.__init__.<locals>.trace_tf_function.<locals>.wrapped_fn   rM   rN   rO   r>   FrP   )rY   r	   rZ   r   r[   r*   r\   )rD   r>   rO   r]   r7   r9   s       r   trace_tf_functionz=StructuredFunctionWrapper.__init__.<locals>.trace_tf_function   s_    7
 "";=i ))$#99## ".k ...r   rO   _tf_data_functionTzEven though the `tf.config.experimental_run_functions_eagerly` option is set, this option does not apply to tf.data functions. To force eager execution of tf.data functions, please use `tf.data.experimental.enable_debug_mode()`.zSeed z4 from outer graph might be getting used by function z, if the random op has not been provided any seed. Explicitly set the seed in the function if this is not the intended behavior.   )
stacklevel)r1   r   convert_legacy_structurer*   element_specr-   lenreplacejoinr   get_func_nameautograph_ctxcontrol_status_ctxupdatestrr   uidr   
DEBUG_MODEr	   functions_run_eagerlywarningswarn	_functionr   executing_eagerlyadd_to_graphget_default_graphseedgraph
_seed_used)r7   funcr8   datasetinput_classesinput_shapesinput_typesinput_structurerv   use_legacy_functionrD   readable_transformation_namerO   symbolrE   r^   ra   
fn_factoryouter_graph_seedr   r9   s   ` `                @@r   __init__z"StructuredFunctionWrapper.__init__F   s   X 	 L$8K<O 0 1 1 !* B B}!6 %,*># 0 1 1 !( 4 4o-"7"{': . / 	/ .dDJl 12Q6 $7#>#>S$2$<> ! 	%		%	%d	+	-.I
 - 0##FB/i0 --/F$ +.Z/( ;	C#cggi.(HIJ(6j;	23.56			&|4
--/
--<=
 '|4
\DN11333L ''L
nn!!#"7"7"9:..055	dnn2277;KK>>**
--  ,  + L	 r   c                     | j                   S r<   )r0   r7   s    r   output_structurez*StructuredFunctionWrapper.output_structure  s    !!!r   c                 D    t        j                  d | j                        S )Nc                 "    | j                         S r<   )_to_legacy_output_classescomponent_specs    r   rA   z:StructuredFunctionWrapper.output_classes.<locals>.<lambda>$  s    ~GGI r   r   map_structurer0   r   s    r   output_classesz(StructuredFunctionWrapper.output_classes!  s!    I   r   c                 D    t        j                  d | j                        S )Nc                 "    | j                         S r<   )_to_legacy_output_shapesr   s    r   rA   z9StructuredFunctionWrapper.output_shapes.<locals>.<lambda>*  s    ~FFH r   r   r   s    r   output_shapesz'StructuredFunctionWrapper.output_shapes'  s!    H   r   c                 D    t        j                  d | j                        S )Nc                 "    | j                         S r<   )_to_legacy_output_typesr   s    r   rA   z8StructuredFunctionWrapper.output_types.<locals>.<lambda>0  s    ~EEG r   r   r   s    r   output_typesz&StructuredFunctionWrapper.output_types-  s!    G   r   c                     | j                   S r<   )rt   r   s    r   r
   z"StructuredFunctionWrapper.function3  s    >>r   )NNNNNTFN)__name__
__module____qualname____doc__r   propertyr   r   r   r   r
   r@   r   r   r   r   C   s    P
 ! # #( Un " "    
    
    
  r   r   )r   rr    tensorflow.python.autograph.corer   rk    tensorflow.python.autograph.implr   r+   tensorflow.python.data.opsr   tensorflow.python.data.utilr   r   tensorflow.python.eagerr   r	   tensorflow.python.frameworkr
   r   tensorflow.python.opsr   tensorflow.python.utilr   r   r   r   r   r@   r   r   <module>r      sG    =  D = 1 , 1 + 0 0 + , 1 1*	r rr   