<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from paramz . caching import Cache_this <EOL> from . import PSICOMP_RBF <EOL> gpu_code = """<STR_LIT>""" <EOL> class PSICOMP_SSRBF_GPU ( PSICOMP_RBF ) : <EOL> def __init__ ( self , threadnum = <NUM_LIT> , blocknum = <NUM_LIT:15> , GPU_direct = False ) : <EOL> from pycuda . compiler import SourceModule <EOL> import GPy . util . gpu_init <EOL> self . GPU_direct = GPU_direct <EOL> self . gpuCache = None <EOL> self . threadnum = threadnum <EOL> self . blocknum = blocknum <EOL> module = SourceModule ( "<STR_LIT>" + str ( self . threadnum ) + "<STR_LIT:\n>" + gpu_code ) <EOL> self . g_psi1computations = module . get_function ( '<STR_LIT>' ) <EOL> self . g_psi1computations . prepare ( '<STR_LIT>' ) <EOL> self . g_psi2computations = module . get_function ( '<STR_LIT>' ) <EOL> self . g_psi2computations . prepare ( '<STR_LIT>' ) <EOL> self . g_psi1compDer = module . get_function ( '<STR_LIT>' ) <EOL> self . g_psi1compDer . prepare ( '<STR_LIT>' ) <EOL> self . g_psi2compDer = module . get_function ( '<STR_LIT>' ) <EOL> self . g_psi2compDer . prepare ( '<STR_LIT>' ) <EOL> self . g_compDenom = module . get_function ( '<STR_LIT>' ) <EOL> self . g_compDenom . prepare ( '<STR_LIT>' ) <EOL> def __deepcopy__ ( self , memo ) : <EOL> s = PSICOMP_SSRBF_GPU ( threadnum = self . threadnum , blocknum = self . blocknum , GPU_direct = self . GPU_direct ) <EOL> memo [ id ( self ) ] = s <EOL> return s <EOL> def _initGPUCache ( self , N , M , Q ) : <EOL> import pycuda . gpuarray as gpuarray <EOL> if self . gpuCache == None : <EOL> self . gpuCache = { <EOL> '<STR_LIT>' : gpuarray . empty ( ( Q , ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( M , Q ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , Q ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , Q ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , Q ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , M ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( M , M ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , M , M ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , M ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( M , M ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , Q ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , Q ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , Q ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , Q ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( self . blocknum , ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( Q , self . blocknum ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( M , Q ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , Q , self . blocknum ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , Q , self . blocknum ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , Q , self . blocknum ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( Q , ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , Q , ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , Q , ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> '<STR_LIT>' : gpuarray . empty ( ( N , Q , ) , np . float64 , order = '<STR_LIT:F>' ) , <EOL> } <EOL> else : <EOL> assert N == self . gpuCache [ '<STR_LIT>' ] . shape [ <NUM_LIT:0> ] <EOL> assert M == self . gpuCache [ '<STR_LIT>' ] . shape [ <NUM_LIT:0> ] <EOL> assert Q == self . gpuCache [ '<STR_LIT>' ] . shape [ <NUM_LIT:0> ] <EOL> def sync_params ( self , lengthscale , Z , mu , S , gamma ) : <EOL> if len ( lengthscale ) == <NUM_LIT:1> : <EOL> self . gpuCache [ '<STR_LIT>' ] . fill ( lengthscale ) <EOL> else : <EOL> self . gpuCache [ '<STR_LIT>' ] . set ( np . asfortranarray ( lengthscale ) ) <EOL> self . gpuCache [ '<STR_LIT>' ] . set ( np . asfortranarray ( Z ) ) <EOL> self . gpuCache [ '<STR_LIT>' ] . set ( np . asfortranarray ( mu ) ) <EOL> self . gpuCache [ '<STR_LIT>' ] . set ( np . asfortranarray ( S ) ) <EOL> self . gpuCache [ '<STR_LIT>' ] . set ( np . asfortranarray ( gamma ) ) <EOL> N , Q = self . gpuCache [ '<STR_LIT>' ] . shape <EOL> self . g_compDenom . prepared_call ( ( self . blocknum , <NUM_LIT:1> ) , ( self . threadnum , <NUM_LIT:1> , <NUM_LIT:1> ) , self . gpuCache [ '<STR_LIT>' ] . gpudata , self . gpuCache [ '<STR_LIT>' ] . gpudata , self . gpuCache [ '<STR_LIT>' ] . gpudata , self . gpuCache [ '<STR_LIT>' ] . gpudata , self . gpuCache [ '<STR_LIT>' ] . gpudata , self . gpuCache [ '<STR_LIT>' ] . gpudata , self . gpuCache [ '<STR_LIT>' ] . gpudata , np . int32 ( N ) , np . int32 ( Q ) ) <EOL> def reset_derivative ( self ) : <EOL> self . gpuCache [ '<STR_LIT>' ] . fill ( <NUM_LIT:0.> ) <EOL> self . gpuCache [ '<STR_LIT>' ] . fill ( <NUM_LIT:0.> ) <EOL> self . gpuCache [ '<STR_LIT>' ] . fill ( <NUM_LIT:0.> ) <EOL> self . gpuCache [ '<STR_LIT>' ] . fill ( <NUM_LIT:0.> ) <EOL> self . gpuCache [ '<STR_LIT>' ] . fill ( <NUM_LIT:0.> ) <EOL> self . gpuCache [ '<STR_LIT>' ] . fill ( <NUM_LIT:0.> ) <EOL> self . gpuCache [ '<STR_LIT>' ] . fill ( <NUM_LIT:0.> ) <EOL> self . gpuCache [ '<STR_LIT>' ] . fill ( <NUM_LIT:0.> ) <EOL> self . gpuCache [ '<STR_LIT>' ] . fill ( <NUM_LIT:0.> ) <EOL> self . gpuCache [ '<STR_LIT>' ] . fill ( <NUM_LIT:0.> ) <EOL> def get_dimensions ( self , Z , variational_posterior ) : <EOL> return variational_posterior . mean . shape [ <NUM_LIT:0> ] , Z . shape [ <NUM_LIT:0> ] , Z . shape [ <NUM_LIT:1> ] <EOL> @ Cache_this ( limit = <NUM_LIT:3> , ignore_args = ( <NUM_LIT:0> , ) ) <EOL> def psicomputations ( self , kern , Z , variational_posterior , return_psi2_n = False ) : <EOL> """<STR_LIT>""" <EOL> variance , lengthscale = kern . variance , kern . lengthscale <EOL> N , M , Q = self . get_dimensions ( Z , variational_posterior ) <EOL> self . _initGPUCache ( N , M , Q ) <EOL> self . sync_params ( lengthscale , Z , variational_posterior . mean , variational_posterior . variance , variational_posterior . binary_prob ) <EOL> psi1_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> psi2_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> psi2n_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> l_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> Z_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> mu_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> S_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> log_denom1_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> log_denom2_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> log_gamma_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> log_gamma1_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> psi0 = np . empty ( ( N , ) ) <EOL> psi0 [ : ] = variance <EOL> self . g_psi1computations . prepared_call ( ( self . blocknum , <NUM_LIT:1> ) , ( self . threadnum , <NUM_LIT:1> , <NUM_LIT:1> ) , psi1_gpu . gpudata , log_denom1_gpu . gpudata , log_gamma_gpu . gpudata , log_gamma1_gpu . gpudata , np . float64 ( variance ) , l_gpu . gpudata , Z_gpu . gpudata , mu_gpu . gpudata , S_gpu . gpudata , np . int32 ( N ) , np . int32 ( M ) , np . int32 ( Q ) ) <EOL> self . g_psi2computations . prepared_call ( ( self . blocknum , <NUM_LIT:1> ) , ( self . threadnum , <NUM_LIT:1> , <NUM_LIT:1> ) , psi2_gpu . gpudata , psi2n_gpu . gpudata , log_denom2_gpu . gpudata , log_gamma_gpu . gpudata , log_gamma1_gpu . gpudata , np . float64 ( variance ) , l_gpu . gpudata , Z_gpu . gpudata , mu_gpu . gpudata , S_gpu . gpudata , np . int32 ( N ) , np . int32 ( M ) , np . int32 ( Q ) ) <EOL> if self . GPU_direct : <EOL> return psi0 , psi1_gpu , psi2_gpu <EOL> else : <EOL> return psi0 , psi1_gpu . get ( ) , psi2_gpu . get ( ) <EOL> @ Cache_this ( limit = <NUM_LIT:3> , ignore_args = ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> def psiDerivativecomputations ( self , kern , dL_dpsi0 , dL_dpsi1 , dL_dpsi2 , Z , variational_posterior ) : <EOL> variance , lengthscale = kern . variance , kern . lengthscale <EOL> from ... . util . linalg_gpu import sum_axis <EOL> ARD = ( len ( lengthscale ) != <NUM_LIT:1> ) <EOL> N , M , Q = self . get_dimensions ( Z , variational_posterior ) <EOL> psi1_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> psi2n_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> l_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> Z_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> mu_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> S_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> gamma_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> dvar_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> dl_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> dZ_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> dmu_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> dS_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> dgamma_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> grad_l_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> grad_mu_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> grad_S_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> grad_gamma_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> log_denom1_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> log_denom2_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> log_gamma_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> log_gamma1_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> if self . GPU_direct : <EOL> dL_dpsi1_gpu = dL_dpsi1 <EOL> dL_dpsi2_gpu = dL_dpsi2 <EOL> dL_dpsi0_sum = gpuarray . sum ( dL_dpsi0 ) . get ( ) <EOL> else : <EOL> dL_dpsi1_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> dL_dpsi2_gpu = self . gpuCache [ '<STR_LIT>' ] <EOL> dL_dpsi1_gpu . set ( np . asfortranarray ( dL_dpsi1 ) ) <EOL> dL_dpsi2_gpu . set ( np . asfortranarray ( dL_dpsi2 ) ) <EOL> dL_dpsi0_sum = dL_dpsi0 . sum ( ) <EOL> self . reset_derivative ( ) <EOL> self . g_psi1compDer . prepared_call ( ( self . blocknum , <NUM_LIT:1> ) , ( self . threadnum , <NUM_LIT:1> , <NUM_LIT:1> ) , dvar_gpu . gpudata , dl_gpu . gpudata , dZ_gpu . gpudata , dmu_gpu . gpudata , dS_gpu . gpudata , dgamma_gpu . gpudata , dL_dpsi1_gpu . gpudata , psi1_gpu . gpudata , log_denom1_gpu . gpudata , log_gamma_gpu . gpudata , log_gamma1_gpu . gpudata , np . float64 ( variance ) , l_gpu . gpudata , Z_gpu . gpudata , mu_gpu . gpudata , S_gpu . gpudata , gamma_gpu . gpudata , np . int32 ( N ) , np . int32 ( M ) , np . int32 ( Q ) ) <EOL> self . g_psi2compDer . prepared_call ( ( self . blocknum , <NUM_LIT:1> ) , ( self . threadnum , <NUM_LIT:1> , <NUM_LIT:1> ) , dvar_gpu . gpudata , dl_gpu . gpudata , dZ_gpu . gpudata , dmu_gpu . gpudata , dS_gpu . gpudata , dgamma_gpu . gpudata , dL_dpsi2_gpu . gpudata , psi2n_gpu . gpudata , log_denom2_gpu . gpudata , log_gamma_gpu . gpudata , log_gamma1_gpu . gpudata , np . float64 ( variance ) , l_gpu . gpudata , Z_gpu . gpudata , mu_gpu . gpudata , S_gpu . gpudata , gamma_gpu . gpudata , np . int32 ( N ) , np . int32 ( M ) , np . int32 ( Q ) ) <EOL> dL_dvar = dL_dpsi0_sum + gpuarray . sum ( dvar_gpu ) . get ( ) <EOL> sum_axis ( grad_mu_gpu , dmu_gpu , N * Q , self . blocknum ) <EOL> dL_dmu = grad_mu_gpu . get ( ) <EOL> sum_axis ( grad_S_gpu , dS_gpu , N * Q , self . blocknum ) <EOL> dL_dS = grad_S_gpu . get ( ) <EOL> sum_axis ( grad_gamma_gpu , dgamma_gpu , N * Q , self . blocknum ) <EOL> dL_dgamma = grad_gamma_gpu . get ( ) <EOL> dL_dZ = dZ_gpu . get ( ) <EOL> if ARD : <EOL> sum_axis ( grad_l_gpu , dl_gpu , Q , self . blocknum ) <EOL> dL_dlengscale = grad_l_gpu . get ( ) <EOL> else : <EOL> dL_dlengscale = gpuarray . sum ( dl_gpu ) . get ( ) <EOL> return dL_dvar , dL_dlengscale , dL_dZ , dL_dmu , dL_dS , dL_dgamma </s>
