o
    ?eP                     @   sv   d Z ddl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 G dd	 d	ejZdS )
zTesting utilities.    N)def_function)op_callbacks)ops)	variables)testc                       s   e Zd ZdZ fddZ	d!ddZdd Zd	d
 Z fddZ fddZ	dd Z
dd Zdd Zdd Z fddZ fddZ fddZ fddZdd  Z  ZS )"AutoGraphTestCasea"  Tests specialized for AutoGraph, which run as tf.functions.

  These tests use a staged programming-like approach: most of the test code runs
  as-is inside a tf.function, but the assertions are lifted outside the
  function, and run with the corresponding function values instead.

  For example, the test:

      def test_foo(self):
        baz = bar();
        self.assertEqual(baz, value)

  is equivalent to writing:

      def test_foo(self):
        @tf.function
        def test_fn():
          baz = bar();
          return baz, value

        baz_actual, value_actual = test_fn()
        self.assertEqual(baz_actual, value_actual)

  Only assertions that require evaluation outside the function are lifted
  outside the function scope. The rest execute inline, at function creation
  time.
  c                    s\   t  | }| jD ]"}|tjjsq	t||}t|r+|	|}t
||t|| q	|S N)super__new____dict__
startswithunittestZ
TestLoaderZtestMethodPrefixgetattrcallable_run_as_tf_functionsetattrtypes
MethodType)clsargsobjnamemwrapper	__class__ j/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/autograph/utils/testing.pyr
   :   s   


zAutoGraphTestCase.__new__Nc                 C   s   | j | d S r   )	trace_logappend)selfop_typeZinputsattrsZoutputsZop_namegraphr   r   r   _op_callbackG   s   zAutoGraphTestCase._op_callbackc                    s    fdd}|S )Nc                    s   t jdd fdd}z| } jD ]	}|| j q |}W n    jd ur8 jjt	   Y d S  t
 j|D ]
\\}}}||  q?d S )NF)Z	autographc                     s2   g _ d _g _g _   dd j D } | S )Nc                 S   s   g | ]\}}|qS r   r   ).0_r   r   r   r   
<listcomp>U   s    z^AutoGraphTestCase._run_as_tf_function.<locals>.wrapper.<locals>.fn_wrapper.<locals>.<listcomp>)
assertions	raises_cmgraph_assertionsr   )targets)fnr    r   r   
fn_wrapperN   s   zJAutoGraphTestCase._run_as_tf_function.<locals>.wrapper.<locals>.fn_wrapper)r   functionr*   Zget_concrete_functionr#   evaluater)   __exit__sysexc_infozipr(   )r    r-   Ztensors	assertionZactualsr&   valuesr,   r    r   r   M   s   
	


z6AutoGraphTestCase._run_as_tf_function.<locals>.wrapperr   )r    r,   r   r   r6   r   r   K   s   z%AutoGraphTestCase._run_as_tf_functionc                 C   sb   t    || jvrtj||d| j|< | | j| j W d    n1 s'w   Y  | j| S )N)dtype)r   Z
init_scoper   Variabler/   Zinitializer)r    r   valuer8   r   r   r   variablem   s   


zAutoGraphTestCase.variablec                    s,   t    i | _g | _d | _t| j d S r   )r	   setUpr   r   r)   r   Zadd_op_callbackr$   r7   r   r   r   r<   t   s
   
zAutoGraphTestCase.setUpc                    s&   t | j d | _d | _t   d S r   )r   Zremove_op_callbackr$   r   r   r	   tearDownr7   r   r   r   r=   {   s   zAutoGraphTestCase.tearDownc                    s     fdd}j | d S )Nc                    sr   g }|   jD ]}t|jr|| q|   jjD ]}|jD ]}t|jr/|| q!q	|  d S r   )
Zas_graph_defnoderematchr   r   Zlibraryr.   node_defZ	assertLen)r#   matchesr>   r,   rA   nop_regexr    r   r   r4      s   


z8AutoGraphTestCase.assertGraphContains.<locals>.assertion)r*   r   )r    rE   rD   r4   r   rC   r   assertGraphContains   s   z%AutoGraphTestCase.assertGraphContainsc                 C   s   |  || j d S r   )ZassertInr   )r    r!   r   r   r   assertOpCreated   s   z!AutoGraphTestCase.assertOpCreatedc                 C   s   |  t|t| j@  d S r   )assertEmptysetr   )r    Zop_typesr   r   r   assertOpsNotCreated      z%AutoGraphTestCase.assertOpsNotCreatedc                 C   s   |  | j d S r   )rH   r   r7   r   r   r   assertNoOpsCreated   s   z$AutoGraphTestCase.assertNoOpsCreatedc                       | j t jt|f d S r   )r(   r   r	   assertEquallistr    r   r   r   r   rN      rK   zAutoGraphTestCase.assertEqualc                    rM   r   )r(   r   r	   
assertLessrO   rP   r   r   r   rQ      rK   zAutoGraphTestCase.assertLessc                    rM   r   )r(   r   r	   assertGreaterEqualrO   rP   r   r   r   rR      rK   z$AutoGraphTestCase.assertGreaterEqualc                    rM   r   )r(   r   r	   assertDictEqualrO   rP   r   r   r   rS      rK   z!AutoGraphTestCase.assertDictEqualc                 G   s,   | j d ur	td| j| | _ | j   d S )Nz6cannot use more than one assertRaisesRuntime in a test)r)   
ValueErrorassertRaisesRegex	__enter__rP   r   r   r   assertRaisesRuntime   s   
z%AutoGraphTestCase.assertRaisesRuntime)NN)__name__
__module____qualname____doc__r
   r$   r   r;   r<   r=   rF   rG   rJ   rL   rN   rQ   rR   rS   rW   __classcell__r   r   r   r   r      s$    
"r   )r[   r?   r1   r   r   Ztensorflow.python.eagerr   Ztensorflow.python.frameworkr   r   Ztensorflow.python.opsr   Ztensorflow.python.platformr   ZTestCaser   r   r   r   r   <module>   s   