ó
àÆ÷Xc           @` s    d  d l  m Z m Z m Z d  d l Z d  d l Z d  d l Z d  d l j Z	 y d  d l
 m Z e Z Wn e k
 r‚ e Z n Xd e j f d „  ƒ  YZ d S(   i    (   t   absolute_importt   print_functiont   divisionN(   t   GpuArrayExceptiont   TestScanCheckpointc           B` sG   e  Z d  „  Z d „  Z d „  Z e j e d ƒ d „  ƒ Z d „  Z	 RS(   c         C` s  t  j d ƒ |  _ t  j d ƒ |  _ t j d d „  d t  j |  j ƒ d |  j d |  j ƒ \ } } t j d d „  d t  j |  j ƒ d |  j d |  j d	 d
 ƒ \ } } | d |  _	 | d |  _
 t  j |  j	 j ƒ  |  j ƒ |  _ t  j |  j
 j ƒ  |  j ƒ |  _ d  S(   Nt   kt   At   fnc         S` s   |  | S(   N(    (   t   prior_resultR   (    (    sN   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_checkpoints.pyt   <lambda>   s    t   outputs_infot   non_sequencest   n_stepsc         S` s   |  | S(   N(    (   R   R   (    (    sN   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_checkpoints.pyR	      s    t   save_every_Nid   iÿÿÿÿ(   t   Tt   iscalarR   t   vectorR   t   theanot   scant	   ones_liket   scan_checkpointst   resultt   result_checkt   gradt   sumt   grad_At   grad_A_check(   t   selfR   t   _R   (    (    sN   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_checkpoints.pyt   setUp   s"    							!c         C` sg   t  j d |  j |  j g d |  j |  j g ƒ } | t d ƒ d ƒ \ } } t j | | ƒ sc t	 ‚ d S(   s!   Test forward computation of A**k.t   inputst   outputsi
   ie   N(
   R   t   functionR   R   R   R   t   ranget   numpyt   allcloset   AssertionError(   R   t   ft   outt	   out_check(    (    sN   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_checkpoints.pyt   test_forward_pass%   s    c         C` sg   t  j d |  j |  j g d |  j |  j g ƒ } | t d ƒ d ƒ \ } } t j | | ƒ sc t	 ‚ d S(   s"   Test gradient computation of A**k.R   R   i
   ie   N(
   R   R    R   R   R   R   R!   R"   R#   R$   (   R   R%   R&   R'   (    (    sN   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_checkpoints.pyt   test_backward_pass,   s    s   Requires pygpu.c         C` sè   d
 t j j j ƒ  k r% t j d ƒ Sd d l m } t j	 d |  j
 |  j g d |  j d | ƒ } t j	 d |  j
 |  j g d |  j d | ƒ } t j j j d
 j } t j | d d t j ƒ} | | d	 ƒ |  j t | | d	 ƒ d
 S(   s/   Test that scan_checkpoint reduces memory usage.s   Requires gpuarray backend.i    (   t   mode_with_gpuR   R   t   modei¸  t   dtypeiè  N(   t   NoneR   t   gpuarrayt   typet   list_contextst   unittestt   SkipTestt   theano.gpuarray.tests.configR*   R    R   R   R   R   t   _context_regt	   free_gmemR"   t   onest   float32t   assertRaisesR   (   R   R*   R%   t   f_checkR5   t   data(    (    sN   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_checkpoints.pyt   test_memory3   s    c         C` s7   |  j  t t j d „  g  i |  j d 6d g d 6ƒ d S(   s8   Test that an error rises if we use taps in outputs_info.c           S` s   d  S(   N(   R-   (    (    (    sN   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_checkpoints.pyR	   G   s    t   initialiþÿÿÿt   tapsN(   R8   t   RuntimeErrorR   R   R   (   R   (    (    sN   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_checkpoints.pyt   test_taps_errorD   s    (
   t   __name__t
   __module__R   R(   R)   R1   t
   skipUnlesst   PYGPU_AVAILABLER;   R?   (    (    (    sN   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_checkpoints.pyR      s
   			(   t
   __future__R    R   R   R"   R1   R   t   theano.tensort   tensorR   t   pygpu.gpuarrayR   t   TrueRC   t   ImportErrort   Falset   TestCaseR   (    (    (    sN   /tmp/pip-build-X4mzal/theano/theano/scan_module/tests/test_scan_checkpoints.pyt   <module>   s   

