<s> import sys <EOL> import numpy <EOL> from unittest import TestCase <EOL> from nose . plugins . skip import SkipTest <EOL> import theano <EOL> import theano . tensor as tensor <EOL> from theano . tensor . blas_c import CGer <EOL> from theano . tensor . blas_scipy import ScipyGer <EOL> from theano . tensor . blas import Ger <EOL> from theano . tensor . blas_c import CGemv <EOL> from theano . tensor . blas import Gemv <EOL> from theano . tensor . blas_c import check_force_gemv_init <EOL> from theano . tests import unittest_tools <EOL> from theano . tests . unittest_tools import TestOptimizationMixin <EOL> from theano . tensor . tests . test_blas import BaseGemv , TestBlasStrides <EOL> mode_blas_opt = theano . compile . get_default_mode ( ) . including ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestCGer ( TestCase , TestOptimizationMixin ) : <EOL> def setUp ( self , dtype = '<STR_LIT>' ) : <EOL> if theano . config . blas . ldflags == "<STR_LIT>" : <EOL> raise SkipTest ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . dtype = dtype <EOL> self . mode = theano . compile . get_default_mode ( ) . including ( '<STR_LIT>' ) <EOL> self . A = tensor . tensor ( dtype = dtype , broadcastable = ( False , False ) ) <EOL> self . a = tensor . tensor ( dtype = dtype , broadcastable = ( ) ) <EOL> self . x = tensor . tensor ( dtype = dtype , broadcastable = ( False , ) ) <EOL> self . y = tensor . tensor ( dtype = dtype , broadcastable = ( False , ) ) <EOL> self . Aval = numpy . ones ( ( <NUM_LIT:2> , <NUM_LIT:3> ) , dtype = dtype ) <EOL> self . xval = numpy . asarray ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , dtype = dtype ) <EOL> self . yval = numpy . asarray ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , dtype = dtype ) <EOL> def function ( self , inputs , outputs ) : <EOL> return theano . function ( inputs , outputs , <EOL> mode = self . mode , <EOL> ) <EOL> def run_f ( self , f ) : <EOL> f ( self . Aval , self . xval , self . yval ) <EOL> f ( self . Aval [ : : - <NUM_LIT:1> , : : - <NUM_LIT:1> ] , self . xval , self . yval ) <EOL> def b ( self , bval ) : <EOL> return tensor . as_tensor_variable ( numpy . asarray ( bval , dtype = self . dtype ) ) <EOL> def test_eq ( self ) : <EOL> self . assertTrue ( CGer ( True ) == CGer ( True ) ) <EOL> self . assertTrue ( CGer ( False ) == CGer ( False ) ) <EOL> self . assertTrue ( CGer ( False ) != CGer ( True ) ) <EOL> self . assertTrue ( CGer ( True ) != ScipyGer ( True ) ) <EOL> self . assertTrue ( CGer ( False ) != ScipyGer ( False ) ) <EOL> self . assertTrue ( CGer ( True ) != Ger ( True ) ) <EOL> self . assertTrue ( CGer ( False ) != Ger ( False ) ) <EOL> self . assertTrue ( CGer ( False ) is not None ) <EOL> self . assertTrue ( CGer ( True ) is not None ) <EOL> def test_hash ( self ) : <EOL> self . assertTrue ( hash ( CGer ( True ) ) == hash ( CGer ( True ) ) ) <EOL> self . assertTrue ( hash ( CGer ( False ) ) == hash ( CGer ( False ) ) ) <EOL> self . assertTrue ( hash ( CGer ( False ) ) != hash ( CGer ( True ) ) ) <EOL> def test_optimization_pipeline ( self ) : <EOL> f = self . function ( [ self . x , self . y ] , tensor . outer ( self . x , self . y ) ) <EOL> self . assertFunctionContains ( f , CGer ( destructive = True ) ) <EOL> f ( self . xval , self . yval ) <EOL> def test_optimization_pipeline_float ( self ) : <EOL> self . setUp ( '<STR_LIT>' ) <EOL> f = self . function ( [ self . x , self . y ] , tensor . outer ( self . x , self . y ) ) <EOL> self . assertFunctionContains ( f , CGer ( destructive = True ) ) <EOL> f ( self . xval , self . yval ) <EOL> def test_int_fails ( self ) : <EOL> self . setUp ( '<STR_LIT>' ) <EOL> f = self . function ( [ self . x , self . y ] , tensor . outer ( self . x , self . y ) ) <EOL> self . assertFunctionContains0 ( f , CGer ( destructive = True ) ) <EOL> self . assertFunctionContains0 ( f , CGer ( destructive = False ) ) <EOL> def test_A_plus_outer ( self ) : <EOL> f = self . function ( [ self . A , self . x , self . y ] , <EOL> self . A + tensor . outer ( self . x , self . y ) ) <EOL> self . assertFunctionContains ( f , CGer ( destructive = False ) ) <EOL> self . run_f ( f ) <EOL> def test_A_plus_scaled_outer ( self ) : <EOL> f = self . function ( [ self . A , self . x , self . y ] , <EOL> self . A + <NUM_LIT:0.1> * tensor . outer ( self . x , self . y ) ) <EOL> self . assertFunctionContains ( f , CGer ( destructive = False ) ) <EOL> self . run_f ( f ) <EOL> class TestCGemv ( TestCase , TestOptimizationMixin ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self , dtype = '<STR_LIT>' ) : <EOL> if theano . config . blas . ldflags == "<STR_LIT>" : <EOL> raise SkipTest ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . dtype = dtype <EOL> self . mode = theano . compile . get_default_mode ( ) . including ( '<STR_LIT>' ) <EOL> self . A = tensor . tensor ( dtype = dtype , broadcastable = ( False , False ) ) <EOL> self . Aval = numpy . ones ( ( <NUM_LIT:2> , <NUM_LIT:3> ) , dtype = dtype ) <EOL> self . x = tensor . tensor ( dtype = dtype , broadcastable = ( False , ) ) <EOL> self . y = tensor . tensor ( dtype = dtype , broadcastable = ( False , ) ) <EOL> self . xval = numpy . asarray ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , dtype = dtype ) <EOL> self . yval = numpy . asarray ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , dtype = dtype ) <EOL> self . a = tensor . tensor ( dtype = dtype , broadcastable = ( ) ) <EOL> def test_optimizations_vm ( self ) : <EOL> '''<STR_LIT>''' <EOL> f = theano . function ( [ self . x , self . A ] , <EOL> theano . dot ( self . x , self . A ) , <EOL> mode = self . mode ) <EOL> self . assertFunctionContains0 ( f , tensor . dot ) <EOL> self . assertFunctionContains1 ( <EOL> f , <EOL> CGemv ( inplace = True , force_init_beta = True ) <EOL> ) <EOL> assert numpy . allclose ( f ( self . xval , self . Aval ) , <EOL> numpy . dot ( self . xval , self . Aval ) ) <EOL> assert numpy . allclose ( f ( self . xval , self . Aval [ : : - <NUM_LIT:1> , : : - <NUM_LIT:1> ] ) , <EOL> numpy . dot ( self . xval , self . Aval [ : : - <NUM_LIT:1> , : : - <NUM_LIT:1> ] ) ) <EOL> def test_optimizations_mv ( self ) : <EOL> '''<STR_LIT>''' <EOL> f = theano . function ( [ self . A , self . y ] , <EOL> theano . dot ( self . A , self . y ) , <EOL> mode = self . mode ) <EOL> self . assertFunctionContains0 ( f , tensor . dot ) <EOL> self . assertFunctionContains1 ( <EOL> f , <EOL> CGemv ( inplace = True , force_init_beta = True ) <EOL> ) <EOL> assert numpy . allclose ( f ( self . Aval , self . yval ) , <EOL> numpy . dot ( self . Aval , self . yval ) ) <EOL> assert numpy . allclose ( f ( self . Aval [ : : - <NUM_LIT:1> , : : - <NUM_LIT:1> ] , self . yval ) , <EOL> numpy . dot ( self . Aval [ : : - <NUM_LIT:1> , : : - <NUM_LIT:1> ] , self . yval ) ) <EOL> def test_force_gemv_init ( self ) : <EOL> if check_force_gemv_init ( ) : <EOL> sys . stderr . write ( <EOL> "<STR_LIT>" <EOL> + "<STR_LIT>" <EOL> + "<STR_LIT>" <EOL> ) <EOL> def t_gemv1 ( self , m_shp ) : <EOL> '''<STR_LIT>''' <EOL> rng = numpy . random . RandomState ( unittest_tools . fetch_seed ( ) ) <EOL> v1 = theano . shared ( numpy . array ( rng . uniform ( size = ( m_shp [ <NUM_LIT:1> ] , ) ) , <EOL> dtype = '<STR_LIT>' ) ) <EOL> v2_orig = numpy . array ( rng . uniform ( size = ( m_shp [ <NUM_LIT:0> ] , ) ) , dtype = '<STR_LIT>' ) <EOL> v2 = theano . shared ( v2_orig ) <EOL> m = theano . shared ( numpy . array ( rng . uniform ( size = m_shp ) , <EOL> dtype = '<STR_LIT>' ) ) <EOL> f = theano . function ( [ ] , v2 + tensor . dot ( m , v1 ) , <EOL> mode = self . mode ) <EOL> assert numpy . allclose ( f ( ) , <EOL> numpy . dot ( m . get_value ( ) , v1 . get_value ( ) ) + v2_orig ) <EOL> topo = [ n . op for n in f . maker . fgraph . toposort ( ) ] <EOL> assert topo == [ CGemv ( inplace = False ) ] , topo <EOL> g = theano . function ( [ ] , [ ] , <EOL> updates = [ ( v2 , v2 + theano . dot ( m , v1 ) ) ] , <EOL> mode = self . mode ) <EOL> g ( ) <EOL> assert numpy . allclose ( v2 . get_value ( ) , <EOL> numpy . dot ( m . get_value ( ) , v1 . get_value ( ) ) + v2_orig ) <EOL> topo = [ n . op for n in g . maker . fgraph . toposort ( ) ] <EOL> assert topo == [ CGemv ( inplace = True ) ] <EOL> m . set_value ( <EOL> m . get_value ( borrow = True ) [ : : - <NUM_LIT:1> , : : - <NUM_LIT:1> ] , <EOL> borrow = True ) <EOL> v2 . set_value ( v2_orig ) <EOL> assert numpy . allclose ( f ( ) , <EOL> numpy . dot ( m . get_value ( ) , v1 . get_value ( ) ) + v2_orig ) <EOL> g ( ) <EOL> assert numpy . allclose ( v2 . get_value ( ) , <EOL> numpy . dot ( m . get_value ( ) , v1 . get_value ( ) ) + v2_orig ) <EOL> def test_gemv1 ( self ) : <EOL> self . t_gemv1 ( ( <NUM_LIT:3> , <NUM_LIT:2> ) ) <EOL> self . t_gemv1 ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> self . t_gemv1 ( ( <NUM_LIT:0> , <NUM_LIT:2> ) ) <EOL> self . t_gemv1 ( ( <NUM_LIT:3> , <NUM_LIT:1> ) ) <EOL> self . t_gemv1 ( ( <NUM_LIT:3> , <NUM_LIT:0> ) ) <EOL> self . t_gemv1 ( ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> self . t_gemv1 ( ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> self . t_gemv1 ( ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> def test_gemv_dimensions ( self , dtype = '<STR_LIT>' ) : <EOL> alpha = theano . shared ( theano . _asarray ( <NUM_LIT:1.0> , dtype = dtype ) , <EOL> name = '<STR_LIT>' ) <EOL> beta = theano . shared ( theano . _asarray ( <NUM_LIT:1.0> , dtype = dtype ) , <EOL> name = '<STR_LIT>' ) <EOL> z = beta * self . y + alpha * tensor . dot ( self . A , self . x ) <EOL> f = theano . function ( [ self . A , self . x , self . y ] , z , <EOL> mode = self . mode ) <EOL> A_val = numpy . ones ( ( <NUM_LIT:5> , <NUM_LIT:3> ) , dtype = dtype ) <EOL> ones_3 = numpy . ones ( <NUM_LIT:3> , dtype = dtype ) <EOL> ones_4 = numpy . ones ( <NUM_LIT:4> , dtype = dtype ) <EOL> ones_5 = numpy . ones ( <NUM_LIT:5> , dtype = dtype ) <EOL> ones_6 = numpy . ones ( <NUM_LIT:6> , dtype = dtype ) <EOL> f ( A_val , ones_3 , ones_5 ) <EOL> f ( A_val [ : : - <NUM_LIT:1> , : : - <NUM_LIT:1> ] , ones_3 , ones_5 ) <EOL> self . assertRaises ( ValueError , f , A_val , ones_4 , ones_5 ) <EOL> self . assertRaises ( ValueError , f , A_val , ones_3 , ones_6 ) <EOL> self . assertRaises ( ValueError , f , A_val , ones_4 , ones_6 ) <EOL> def test_multiple_inplace ( self ) : <EOL> x = tensor . dmatrix ( '<STR_LIT:x>' ) <EOL> y = tensor . dvector ( '<STR_LIT:y>' ) <EOL> z = tensor . dvector ( '<STR_LIT:z>' ) <EOL> f = theano . function ( [ x , y , z ] , <EOL> [ tensor . dot ( y , x ) , tensor . dot ( z , x ) ] , <EOL> mode = mode_blas_opt ) <EOL> vx = numpy . random . rand ( <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> vy = numpy . random . rand ( <NUM_LIT:3> ) <EOL> vz = numpy . random . rand ( <NUM_LIT:3> ) <EOL> out = f ( vx , vy , vz ) <EOL> assert numpy . allclose ( out [ <NUM_LIT:0> ] , numpy . dot ( vy , vx ) ) <EOL> assert numpy . allclose ( out [ <NUM_LIT:1> ] , numpy . dot ( vz , vx ) ) <EOL> assert len ( [ n for n in f . maker . fgraph . apply_nodes <EOL> if isinstance ( n . op , tensor . AllocEmpty ) ] ) == <NUM_LIT:2> <EOL> class TestCGemvFloat32 ( TestCase , BaseGemv , TestOptimizationMixin ) : <EOL> mode = mode_blas_opt <EOL> dtype = '<STR_LIT>' <EOL> gemv = CGemv ( inplace = False ) <EOL> gemv_inplace = CGemv ( inplace = True ) <EOL> def setUp ( self ) : <EOL> if theano . config . blas . ldflags == "<STR_LIT>" : <EOL> raise SkipTest ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class TestCGemvFloat64 ( TestCase , BaseGemv , TestOptimizationMixin ) : <EOL> mode = mode_blas_opt <EOL> dtype = '<STR_LIT>' <EOL> gemv = CGemv ( inplace = False ) <EOL> gemv_inplace = CGemv ( inplace = True ) <EOL> def setUp ( self ) : <EOL> if theano . config . blas . ldflags == "<STR_LIT>" : <EOL> raise SkipTest ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class TestBlasStridesC ( TestBlasStrides ) : <EOL> mode = mode_blas_opt </s>
