o
    2hP                     @   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unittest
TestLoadertestMethodPrefixgetattrcallable_run_as_tf_functionsetattrtypes
MethodType)clsargsobjnamemwrapper	__class__ d/var/www/html/chatgem/venv/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_typeinputsattrsoutputs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)	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   functionr0   get_concrete_functionr(   evaluater/   __exit__sysexc_infozipr.   )r"   r3   tensors	assertionactualsr,   valuesr2   r"   r   r   M   s   
	


z6AutoGraphTestCase._run_as_tf_function.<locals>.wrapperr   )r"   r2   r   r   r?   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   
init_scoper   Variabler6   initializer)r"   r   valuerA   r   r   r   variablem   s   


zAutoGraphTestCase.variablec                    s,   t    i | _g | _d | _t| j d S r   )r	   setUpr   r    r/   r   add_op_callbackr)   r@   r   r   r   rG   t   s
   
zAutoGraphTestCase.setUpc                    s&   t | j d | _d | _t   d S r   )r   remove_op_callbackr)   r    r   r	   tearDownr@   r   r   r   rJ   {   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   )
as_graph_defnoderematchr   r!   libraryr4   node_def	assertLen)r(   matchesrL   r2   rP   nop_regexr"   r   r   r<      s   


z8AutoGraphTestCase.assertGraphContains.<locals>.assertion)r0   r!   )r"   rU   rT   r<   r   rS   r   assertGraphContains   s   z%AutoGraphTestCase.assertGraphContainsc                 C   s   |  || j d S r   )assertInr    )r"   r#   r   r   r   assertOpCreated   s   z!AutoGraphTestCase.assertOpCreatedc                 C   s   |  t|t| j@  d S r   )assertEmptysetr    )r"   op_typesr   r   r   assertOpsNotCreated      z%AutoGraphTestCase.assertOpsNotCreatedc                 C   s   |  | j d S r   )rY   r    r@   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   r`      r]   zAutoGraphTestCase.assertEqualc                    r_   r   )r.   r!   r	   
assertLessra   rb   r   r   r   rc      r]   zAutoGraphTestCase.assertLessc                    r_   r   )r.   r!   r	   assertGreaterEqualra   rb   r   r   r   rd      r]   z$AutoGraphTestCase.assertGreaterEqualc                    r_   r   )r.   r!   r	   assertDictEqualra   rb   r   r   r   re      r]   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__rb   r   r   r   assertRaisesRuntime   s   
z%AutoGraphTestCase.assertRaisesRuntime)NN)__name__
__module____qualname____doc__r
   r)   r   rF   rG   rJ   rV   rX   r\   r^   r`   rc   rd   re   ri   __classcell__r   r   r   r   r      s$    
"r   )rm   rM   r8   r   r   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   tensorflow.python.opsr   tensorflow.python.platformr   TestCaser   r   r   r   r   <module>   s   