<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , print_function , division <EOL> import inspect <EOL> import logging <EOL> import numpy <EOL> import os <EOL> import re <EOL> import sys <EOL> import warnings <EOL> import theano <EOL> from theano import config <EOL> import theano . gof . cc <EOL> from six import itervalues <EOL> from theano . gof import graph <EOL> from theano . gof import utils <EOL> from theano . gof . cmodule import GCC_compiler <EOL> from theano . gof . fg import FunctionGraph <EOL> __authors__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> __contact__ = "<STR_LIT>" <EOL> __docformat__ = "<STR_LIT>" <EOL> class CLinkerObject ( object ) : <EOL> """<STR_LIT>""" <EOL> def c_headers ( self ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( <EOL> "<STR_LIT>" , type ( self ) , self . __class__ . __name__ ) <EOL> def c_header_dirs ( self ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( <EOL> "<STR_LIT>" , <EOL> type ( self ) , <EOL> self . __class__ . __name__ ) <EOL> def c_libraries ( self ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( <EOL> "<STR_LIT>" , type ( self ) , self . __class__ . __name__ ) <EOL> def c_lib_dirs ( self ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( <EOL> "<STR_LIT>" , type ( self ) , self . __class__ . __name__ ) <EOL> def c_support_code ( self ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( <EOL> "<STR_LIT>" , <EOL> type ( self ) , <EOL> self . __class__ . __name__ ) <EOL> def c_code_cache_version ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( ) <EOL> def c_compile_args ( self ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( <EOL> "<STR_LIT>" , <EOL> type ( self ) , <EOL> self . __class__ . __name__ ) <EOL> def c_no_compile_args ( self ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( <EOL> "<STR_LIT>" , <EOL> type ( self ) , <EOL> self . __class__ . __name__ ) <EOL> def c_init_code ( self ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( "<STR_LIT>" , type ( self ) , <EOL> self . __class__ . __name__ ) <EOL> class CLinkerOp ( CLinkerObject ) : <EOL> """<STR_LIT>""" <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( '<STR_LIT>' % self . __class__ . __name__ ) <EOL> def c_code_cache_version_apply ( self , node ) : <EOL> """<STR_LIT>""" <EOL> return self . c_code_cache_version ( ) <EOL> def c_code_cleanup ( self , node , name , inputs , outputs , sub ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( '<STR_LIT>' % <EOL> self . __class__ . __name__ ) <EOL> def c_support_code_apply ( self , node , name ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( "<STR_LIT>" , <EOL> type ( self ) , self . __class__ . __name__ ) <EOL> def c_init_code_apply ( self , node , name ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( "<STR_LIT>" , type ( self ) , <EOL> self . __class__ . __name__ ) <EOL> def c_init_code_struct ( self , node , name , sub ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( "<STR_LIT>" , type ( self ) , <EOL> self . __class__ . __name__ ) <EOL> def c_support_code_struct ( self , node , name ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( "<STR_LIT>" , <EOL> type ( self ) , self . __class__ . __name__ ) <EOL> def c_cleanup_code_struct ( self , node , name ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( "<STR_LIT>" , type ( self ) , <EOL> self . __class__ . __name__ ) <EOL> class PureOp ( object ) : <EOL> """<STR_LIT>""" <EOL> default_output = None <EOL> """<STR_LIT>""" <EOL> def make_node ( self , * inputs ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( <EOL> "<STR_LIT>" , type ( self ) , self . __class__ . __name__ ) <EOL> @ classmethod <EOL> def _get_test_value ( cls , v ) : <EOL> """<STR_LIT>""" <EOL> from theano . compile . sharedvalue import SharedVariable <EOL> if isinstance ( v , graph . Constant ) : <EOL> return v . value <EOL> elif isinstance ( v , SharedVariable ) : <EOL> return v . get_value ( borrow = True , return_internal_type = True ) <EOL> elif isinstance ( v , graph . Variable ) and hasattr ( v . tag , '<STR_LIT>' ) : <EOL> try : <EOL> ret = v . type . filter ( v . tag . test_value ) <EOL> except Exception as e : <EOL> detailed_err_msg = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> detailed_err_msg += utils . get_variable_trace_string ( v ) <EOL> detailed_err_msg += ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> args = ( detailed_err_msg , ) + tuple ( str ( arg ) for arg in e . args ) <EOL> e . args = ( "<STR_LIT:\n>" . join ( args ) , ) <EOL> raise <EOL> return ret <EOL> detailed_err_msg = utils . get_variable_trace_string ( v ) <EOL> raise AttributeError ( '<STR_LIT>' % ( v , detailed_err_msg ) ) <EOL> def __call__ ( self , * inputs , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return_list = kwargs . pop ( '<STR_LIT>' , False ) <EOL> node = self . make_node ( * inputs , ** kwargs ) <EOL> if config . compute_test_value != '<STR_LIT>' : <EOL> run_perform = True <EOL> storage_map = { } <EOL> compute_map = { } <EOL> for i , ins in enumerate ( node . inputs ) : <EOL> try : <EOL> storage_map [ ins ] = [ self . _get_test_value ( ins ) ] <EOL> compute_map [ ins ] = [ True ] <EOL> except AttributeError : <EOL> if config . compute_test_value == '<STR_LIT>' : <EOL> warnings . warn ( <EOL> '<STR_LIT>' % <EOL> ( i , ins , node ) , stacklevel = <NUM_LIT:2> ) <EOL> run_perform = False <EOL> elif config . compute_test_value == '<STR_LIT>' : <EOL> detailed_err_msg = utils . get_variable_trace_string ( ins ) <EOL> raise ValueError ( <EOL> '<STR_LIT>' % <EOL> ( i , ins , node , detailed_err_msg ) ) <EOL> elif config . compute_test_value == '<STR_LIT:ignore>' : <EOL> run_perform = False <EOL> elif config . compute_test_value == '<STR_LIT>' : <EOL> import pdb <EOL> pdb . post_mortem ( sys . exc_info ( ) [ <NUM_LIT:2> ] ) <EOL> else : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % <EOL> config . compute_test_value ) <EOL> if run_perform : <EOL> destroyed_inputs_idx = set ( ) <EOL> if getattr ( node . op , '<STR_LIT>' , None ) : <EOL> for i_pos_list in itervalues ( node . op . destroy_map ) : <EOL> destroyed_inputs_idx . update ( i_pos_list ) <EOL> for inp_idx in destroyed_inputs_idx : <EOL> inp = node . inputs [ inp_idx ] <EOL> storage_map [ inp ] = [ storage_map [ inp ] [ <NUM_LIT:0> ] . copy ( ) ] <EOL> for o in node . outputs : <EOL> storage_map [ o ] = [ None ] <EOL> compute_map [ o ] = [ False ] <EOL> thunk = node . op . make_thunk ( node , storage_map , compute_map , <EOL> no_recycling = [ ] ) <EOL> thunk . inputs = [ storage_map [ v ] for v in node . inputs ] <EOL> thunk . outputs = [ storage_map [ v ] for v in node . outputs ] <EOL> required = thunk ( ) <EOL> assert not required <EOL> for output in node . outputs : <EOL> assert compute_map [ output ] [ <EOL> <NUM_LIT:0> ] , ( output , storage_map [ output ] [ <NUM_LIT:0> ] ) <EOL> output . tag . test_value = storage_map [ output ] [ <NUM_LIT:0> ] <EOL> if self . default_output is not None : <EOL> rval = node . outputs [ self . default_output ] <EOL> if return_list : <EOL> rval = [ rval ] <EOL> return rval <EOL> else : <EOL> if return_list : <EOL> return list ( node . outputs ) <EOL> elif len ( node . outputs ) == <NUM_LIT:1> : <EOL> return node . outputs [ <NUM_LIT:0> ] <EOL> else : <EOL> return node . outputs <EOL> def __ne__ ( self , other ) : <EOL> return not ( self == other ) <EOL> add_tag_trace = staticmethod ( utils . add_tag_trace ) <EOL> def R_op ( self , inputs , eval_points ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( self , self . __class__ . __name__ ) ) <EOL> def perform ( self , node , inputs , output_storage , params = None ) : <EOL> """<STR_LIT>""" <EOL> raise utils . MethodNotDefined ( <EOL> "<STR_LIT>" , type ( self ) , self . __class__ . __name__ , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def do_constant_folding ( self , node ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> class Op ( utils . object2 , PureOp , CLinkerOp ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> obj = object . __new__ ( cls ) <EOL> if not hasattr ( obj , '<STR_LIT>' ) : <EOL> obj . _op_use_c_code = theano . config . cxx <EOL> return obj <EOL> def __init__ ( self , use_c_code = theano . config . cxx ) : <EOL> self . _op_use_c_code = use_c_code <EOL> def _props ( self ) : <EOL> return tuple ( getattr ( self , a ) for a in self . __props__ ) <EOL> def _props_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( [ ( a , getattr ( self , a ) ) <EOL> for a in self . __props__ ] ) <EOL> def __hash__ ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return hash ( ( type ( self ) , self . _props ( ) ) ) <EOL> else : <EOL> return super ( Op , self ) . __hash__ ( ) <EOL> def __str__ ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> if len ( self . __props__ ) == <NUM_LIT:0> : <EOL> return "<STR_LIT:%s>" % ( self . __class__ . __name__ , ) <EOL> else : <EOL> return "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , <EOL> "<STR_LIT:U+002CU+0020>" . join ( "<STR_LIT>" % ( p , getattr ( self , p ) ) <EOL> for p in self . __props__ ) ) <EOL> else : <EOL> return super ( Op , self ) . __str__ ( ) <EOL> def __eq__ ( self , other ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return ( type ( self ) == type ( other ) and self . _props ( ) == <EOL> other . _props ( ) ) <EOL> else : <EOL> return NotImplemented <EOL> def prepare_node ( self , node , storage_map , compute_map ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def make_c_thunk ( self , node , storage_map , compute_map , no_recycling ) : <EOL> """<STR_LIT>""" <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> node_input_storage = [ storage_map [ r ] for r in node . inputs ] <EOL> node_output_storage = [ storage_map [ r ] for r in node . outputs ] <EOL> if not getattr ( self , '<STR_LIT>' , False ) : <EOL> def is_f16 ( t ) : <EOL> return getattr ( t , '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' <EOL> if ( any ( is_f16 ( i . type ) for i in node . inputs ) or <EOL> any ( is_f16 ( o . type ) for o in node . outputs ) ) : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( self , ) ) <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> e = FunctionGraph ( node . inputs , node . outputs ) <EOL> e_no_recycling = [ new_o <EOL> for ( new_o , old_o ) in zip ( e . outputs , node . outputs ) <EOL> if old_o in no_recycling ] <EOL> cl = theano . gof . cc . CLinker ( ) . accept ( e , <EOL> no_recycling = e_no_recycling ) <EOL> logger . debug ( '<STR_LIT>' ) <EOL> outputs = cl . make_thunk ( input_storage = node_input_storage , <EOL> output_storage = node_output_storage ) <EOL> fill_storage , node_input_filters , node_output_filters = outputs <EOL> def rval ( ) : <EOL> fill_storage ( ) <EOL> for o in node . outputs : <EOL> compute_map [ o ] [ <NUM_LIT:0> ] = True <EOL> rval . cthunk = fill_storage . cthunk <EOL> rval . inputs = node_input_storage <EOL> rval . outputs = node_output_storage <EOL> rval . lazy = False <EOL> return rval <EOL> def make_py_thunk ( self , node , storage_map , compute_map , no_recycling , <EOL> debug = False ) : <EOL> """<STR_LIT>""" <EOL> node_input_storage = [ storage_map [ r ] for r in node . inputs ] <EOL> node_output_storage = [ storage_map [ r ] for r in node . outputs ] <EOL> if debug : <EOL> p = node . op . debug_perform <EOL> else : <EOL> p = node . op . perform <EOL> params = node . run_params ( ) <EOL> if params is graph . NoParams : <EOL> def rval ( p = p , i = node_input_storage , o = node_output_storage , n = node ) : <EOL> r = p ( n , [ x [ <NUM_LIT:0> ] for x in i ] , o ) <EOL> for o in node . outputs : <EOL> compute_map [ o ] [ <NUM_LIT:0> ] = True <EOL> return r <EOL> else : <EOL> params_val = node . params_type . filter ( params ) <EOL> def rval ( p = p , i = node_input_storage , o = node_output_storage , n = node , <EOL> params = params_val ) : <EOL> r = p ( n , [ x [ <NUM_LIT:0> ] for x in i ] , o , params ) <EOL> for o in node . outputs : <EOL> compute_map [ o ] [ <NUM_LIT:0> ] = True <EOL> return r <EOL> rval . inputs = node_input_storage <EOL> rval . outputs = node_output_storage <EOL> rval . perform = p <EOL> rval . lazy = False <EOL> return rval <EOL> def make_thunk ( self , node , storage_map , compute_map , no_recycling ) : <EOL> """<STR_LIT>""" <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> new_node = self . prepare_node ( node , storage_map = storage_map , <EOL> compute_map = compute_map ) <EOL> if new_node is not None : <EOL> node = new_node <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( self ) , <EOL> stacklevel = <NUM_LIT:2> ) <EOL> if getattr ( self , '<STR_LIT>' , theano . config . cxx ) : <EOL> try : <EOL> return self . make_c_thunk ( node , storage_map , compute_map , <EOL> no_recycling ) <EOL> except ( NotImplementedError , utils . MethodNotDefined ) : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> return self . make_py_thunk ( node , storage_map , compute_map , no_recycling ) <EOL> def make_node ( self , * inputs ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> if len ( inputs ) != len ( self . itypes ) : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> ( len ( self . itypes ) , len ( inputs ) ) ) <EOL> if not all ( inp . type == it for inp , it in zip ( inputs , self . itypes ) ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % <EOL> ( str ( [ inp . type for inp in inputs ] ) , str ( self . itypes ) ) ) <EOL> return theano . Apply ( self , inputs , [ o ( ) for o in self . otypes ] ) <EOL> def get_test_value ( v ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( v , graph . Variable ) : <EOL> v_var = theano . tensor . as_tensor_variable ( v ) <EOL> else : <EOL> v_var = v <EOL> return PureOp . _get_test_value ( v_var ) <EOL> def missing_test_message ( msg ) : <EOL> """<STR_LIT>""" <EOL> action = config . compute_test_value <EOL> if action == '<STR_LIT>' : <EOL> raise AttributeError ( msg ) <EOL> elif action == '<STR_LIT>' : <EOL> warnings . warn ( msg , stacklevel = <NUM_LIT:2> ) <EOL> else : <EOL> assert action in [ '<STR_LIT:ignore>' , '<STR_LIT>' ] <EOL> def debug_error_message ( msg ) : <EOL> """<STR_LIT>""" <EOL> action = config . compute_test_value <EOL> assert action != '<STR_LIT>' <EOL> if action in [ '<STR_LIT>' , '<STR_LIT:ignore>' ] : <EOL> raise ValueError ( msg ) <EOL> else : <EOL> assert action == '<STR_LIT>' <EOL> warnings . warn ( msg , stacklevel = <NUM_LIT:2> ) <EOL> def debug_assert ( condition , msg = None ) : <EOL> if msg is None : <EOL> msg = '<STR_LIT>' <EOL> if not condition : <EOL> action = config . compute_test_value <EOL> if action in [ '<STR_LIT>' , '<STR_LIT:ignore>' ] : <EOL> raise AssertionError ( msg ) <EOL> else : <EOL> assert action == '<STR_LIT>' <EOL> warnings . warn ( msg , stacklevel = <NUM_LIT:2> ) <EOL> def get_debug_values ( * args ) : <EOL> """<STR_LIT>""" <EOL> if config . compute_test_value == '<STR_LIT>' : <EOL> return [ ] <EOL> rval = [ ] <EOL> for i , arg in enumerate ( args ) : <EOL> try : <EOL> rval . append ( get_test_value ( arg ) ) <EOL> except AttributeError : <EOL> if hasattr ( arg , '<STR_LIT:name>' ) and arg . name is not None : <EOL> missing_test_message ( "<STR_LIT>" + str ( i ) + "<STR_LIT>" + arg . name + <EOL> "<STR_LIT>" ) <EOL> else : <EOL> missing_test_message ( "<STR_LIT>" + str ( i ) + <EOL> "<STR_LIT>" ) <EOL> return [ ] <EOL> if len ( rval ) == <NUM_LIT:1> : <EOL> return rval <EOL> return [ tuple ( rval ) ] <EOL> ops_with_inner_function = { } <EOL> """<STR_LIT>""" <EOL> class OpenMPOp ( Op ) : <EOL> """<STR_LIT>""" <EOL> gxx_support_openmp = None <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , openmp = None ) : <EOL> if openmp is None : <EOL> openmp = theano . config . openmp <EOL> self . openmp = openmp <EOL> def __setstate__ ( self , d ) : <EOL> self . __dict__ . update ( d ) <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . openmp = False <EOL> def c_compile_args ( self ) : <EOL> self . update_self_openmp ( ) <EOL> if self . openmp : <EOL> return [ '<STR_LIT>' ] <EOL> return [ ] <EOL> def c_headers ( self ) : <EOL> self . update_self_openmp ( ) <EOL> if self . openmp : <EOL> return [ "<STR_LIT>" ] <EOL> return [ ] <EOL> @ staticmethod <EOL> def test_gxx_support ( ) : <EOL> code = """<STR_LIT>""" <EOL> default_openmp = GCC_compiler . try_compile_tmp ( <EOL> src_code = code , <EOL> tmp_prefix = '<STR_LIT>' , <EOL> flags = [ '<STR_LIT>' ] , <EOL> try_run = False ) <EOL> return default_openmp <EOL> def update_self_openmp ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . openmp : <EOL> if OpenMPOp . gxx_support_openmp is None : <EOL> OpenMPOp . gxx_support_openmp = OpenMPOp . test_gxx_support ( ) <EOL> if not OpenMPOp . gxx_support_openmp : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> stacklevel = <NUM_LIT:3> ) <EOL> if OpenMPOp . gxx_support_openmp is False : <EOL> self . openmp = False <EOL> theano . config . openmp = False <EOL> def prepare_node ( self , node , storage_map , <EOL> compute_map ) : <EOL> self . update_self_openmp ( ) <EOL> def simple_meth ( tag ) : <EOL> def f ( self ) : <EOL> if tag in self . code_sections : <EOL> return self . code_sections [ tag ] <EOL> else : <EOL> raise utils . MethodNotDefined ( <EOL> '<STR_LIT>' + tag , type ( self ) , type ( self ) . __name__ ) <EOL> f . __name__ = '<STR_LIT>' + tag <EOL> return f <EOL> def apply_meth ( tag ) : <EOL> def f ( self , node , name ) : <EOL> if tag in self . code_sections : <EOL> code = self . code_sections [ tag ] <EOL> define_macros , undef_macros = self . get_c_macros ( node , name ) <EOL> return os . linesep . join ( [ '<STR_LIT>' , define_macros , code , <EOL> undef_macros ] ) <EOL> else : <EOL> raise utils . MethodNotDefined ( <EOL> '<STR_LIT>' + tag , type ( self ) , type ( self ) . __name__ ) <EOL> f . __name__ = '<STR_LIT>' + tag <EOL> return f <EOL> class COp ( Op ) : <EOL> """<STR_LIT>""" <EOL> section_re = re . compile ( r'<STR_LIT>' , re . MULTILINE ) <EOL> backward_re = re . compile ( <EOL> r'<STR_LIT>' , <EOL> re . MULTILINE ) <EOL> SECTIONS = set ( [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:code>' , '<STR_LIT>' ] ) <EOL> @ classmethod <EOL> def get_path ( cls , f ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . isabs ( f ) : <EOL> class_file = inspect . getfile ( cls ) <EOL> class_dir = os . path . dirname ( class_file ) <EOL> f = os . path . realpath ( os . path . join ( class_dir , f ) ) <EOL> return f <EOL> def __init__ ( self , func_files , func_name = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( func_files , list ) : <EOL> func_files = [ func_files ] <EOL> self . func_files = [ self . get_path ( f ) for f in func_files ] <EOL> self . func_name = func_name <EOL> self . load_c_code ( ) <EOL> if len ( self . code_sections ) == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if self . func_name is not None : <EOL> if '<STR_LIT>' in self . code_sections : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if '<STR_LIT>' in self . code_sections : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def load_c_code ( self ) : <EOL> self . func_codes = [ ] <EOL> for func_file in self . func_files : <EOL> with open ( func_file , '<STR_LIT:r>' ) as f : <EOL> self . func_codes . append ( f . read ( ) ) <EOL> old_markers_present = False <EOL> new_markers_present = False <EOL> for code in self . func_codes : <EOL> if self . backward_re . search ( code ) : <EOL> old_markers_present = True <EOL> if self . section_re . search ( code ) : <EOL> new_markers_present = True <EOL> if old_markers_present and new_markers_present : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . code_sections = dict ( ) <EOL> for i , code in enumerate ( self . func_codes ) : <EOL> if self . backward_re . search ( code ) : <EOL> split = self . backward_re . split ( code ) <EOL> n = <NUM_LIT:1> <EOL> while n < len ( split ) : <EOL> if split [ n ] == '<STR_LIT>' : <EOL> self . code_sections [ '<STR_LIT>' ] = split [ n + <NUM_LIT:1> ] <EOL> elif split [ n ] == '<STR_LIT>' : <EOL> self . code_sections [ '<STR_LIT>' ] = split [ n + <NUM_LIT:1> ] <EOL> n += <NUM_LIT:2> <EOL> continue <EOL> elif self . section_re . search ( code ) : <EOL> split = self . section_re . split ( code ) <EOL> if split [ <NUM_LIT:0> ] . strip ( ) != '<STR_LIT>' : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( self . func_files [ i ] , split [ <NUM_LIT:0> ] ) ) <EOL> n = <NUM_LIT:1> <EOL> while n < len ( split ) : <EOL> if split [ n ] not in self . SECTIONS : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % <EOL> ( self . func_files [ i ] , split [ n ] ) ) <EOL> if split [ n ] not in self . code_sections : <EOL> self . code_sections [ split [ n ] ] = "<STR_LIT>" <EOL> self . code_sections [ split [ n ] ] += split [ n + <NUM_LIT:1> ] <EOL> n += <NUM_LIT:2> <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT:%s>" % self . func_files [ i ] ) <EOL> def get_op_params ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ ] <EOL> def c_code_cache_version ( self ) : <EOL> return hash ( tuple ( self . func_codes ) ) <EOL> def c_init_code ( self ) : <EOL> if '<STR_LIT>' in self . code_sections : <EOL> return [ self . code_sections [ '<STR_LIT>' ] ] <EOL> else : <EOL> raise utils . MethodNotDefined ( <EOL> '<STR_LIT>' , type ( self ) , type ( self ) . __name__ ) <EOL> c_init_code_apply = apply_meth ( '<STR_LIT>' ) <EOL> c_support_code = simple_meth ( '<STR_LIT>' ) <EOL> c_support_code_apply = apply_meth ( '<STR_LIT>' ) <EOL> c_support_code_struct = apply_meth ( '<STR_LIT>' ) <EOL> c_cleanup_code_struct = apply_meth ( '<STR_LIT>' ) <EOL> def format_c_function_args ( self , inp , out ) : <EOL> return "<STR_LIT:U+002CU+0020>" . join ( list ( inp ) + [ "<STR_LIT>" % o for o in out ] ) <EOL> def get_c_macros ( self , node , name , check_input = None ) : <EOL> define_template = "<STR_LIT>" <EOL> undef_template = "<STR_LIT>" <EOL> define_macros = [ ] <EOL> undef_macros = [ ] <EOL> if check_input is None : <EOL> check_input = getattr ( self , '<STR_LIT>' , True ) <EOL> if check_input : <EOL> variables = node . inputs + node . outputs <EOL> variable_names = ( [ "<STR_LIT>" % i for i in range ( len ( node . inputs ) ) ] + <EOL> [ "<STR_LIT>" % i for i in range ( len ( node . outputs ) ) ] ) <EOL> for i , v in enumerate ( variables ) : <EOL> if not hasattr ( v , '<STR_LIT>' ) : <EOL> continue <EOL> vname = variable_names [ i ] <EOL> macro_name = "<STR_LIT>" + vname <EOL> macro_value = "<STR_LIT>" + v . dtype <EOL> define_macros . append ( <EOL> define_template % <EOL> ( macro_name , macro_value ) ) <EOL> undef_macros . append ( undef_template % macro_name ) <EOL> d = numpy . dtype ( v . dtype ) <EOL> macro_name = "<STR_LIT>" + vname <EOL> macro_value = d . num <EOL> define_macros . append ( <EOL> define_template % <EOL> ( macro_name , macro_value ) ) <EOL> undef_macros . append ( undef_template % macro_name ) <EOL> macro_name = "<STR_LIT>" + vname <EOL> macro_value = d . itemsize <EOL> define_macros . append ( <EOL> define_template % <EOL> ( macro_name , macro_value ) ) <EOL> undef_macros . append ( undef_template % macro_name ) <EOL> define_macros . append ( define_template % ( "<STR_LIT>" , <EOL> "<STR_LIT>" % name ) ) <EOL> undef_macros . append ( undef_template % "<STR_LIT>" ) <EOL> for n , v in self . get_op_params ( ) : <EOL> define_macros . append ( define_template % ( n , v ) ) <EOL> undef_macros . append ( undef_template % ( n , ) ) <EOL> return os . linesep . join ( define_macros ) , os . linesep . join ( undef_macros ) <EOL> def _lquote_macro ( self , txt ) : <EOL> res = [ ] <EOL> spl = txt . split ( '<STR_LIT:\n>' ) <EOL> for l in spl [ : - <NUM_LIT:1> ] : <EOL> res . append ( l + '<STR_LIT>' ) <EOL> res . append ( spl [ - <NUM_LIT:1> ] ) <EOL> return os . linesep . join ( res ) <EOL> def get_sub_macros ( self , sub ) : <EOL> define_macros = [ ] <EOL> undef_macros = [ ] <EOL> define_macros . append ( "<STR_LIT>" % ( <EOL> self . _lquote_macro ( sub [ '<STR_LIT>' ] ) , ) ) <EOL> undef_macros . append ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in sub : <EOL> define_macros . append ( "<STR_LIT>" % ( sub [ '<STR_LIT>' ] , ) ) <EOL> undef_macros . append ( "<STR_LIT>" ) <EOL> return os . linesep . join ( define_macros ) , os . linesep . join ( undef_macros ) <EOL> def get_io_macros ( self , inputs , outputs ) : <EOL> define_macros = [ ] <EOL> undef_macros = [ ] <EOL> for i , inp in enumerate ( inputs ) : <EOL> define_macros . append ( "<STR_LIT>" ( i , inp ) ) <EOL> undef_macros . append ( "<STR_LIT>" , ( i , ) ) <EOL> for i , out in enumerate ( outputs ) : <EOL> define_macros . append ( "<STR_LIT>" ( i , inp ) ) <EOL> undef_macros . append ( "<STR_LIT>" , ( i , ) ) <EOL> def c_init_code_struct ( self , node , name , sub ) : <EOL> if '<STR_LIT>' in self . code_sections : <EOL> op_code = self . code_sections [ '<STR_LIT>' ] <EOL> def_macros , undef_macros = self . get_c_macros ( node , name ) <EOL> def_sub , undef_sub = self . get_sub_macros ( sub ) <EOL> return os . linesep . join ( [ '<STR_LIT>' , def_macros , def_sub , <EOL> op_code , <EOL> undef_sub , undef_macros ] ) <EOL> else : <EOL> raise utils . MethodNotDefined ( <EOL> '<STR_LIT>' , type ( self ) , type ( self ) . __name__ ) <EOL> def c_code ( self , node , name , inp , out , sub ) : <EOL> if self . func_name is not None : <EOL> assert '<STR_LIT:code>' not in self . code_sections <EOL> define_macros , undef_macros = self . get_c_macros ( node , name , <EOL> check_input = False ) <EOL> params = "<STR_LIT>" <EOL> if '<STR_LIT>' in sub : <EOL> params = "<STR_LIT>" % ( sub [ '<STR_LIT>' ] , ) <EOL> return """<STR_LIT>""" % dict ( func_name = self . func_name , <EOL> fail = sub [ '<STR_LIT>' ] , params = params , <EOL> func_args = self . format_c_function_args ( inp , out ) , <EOL> define_macros = define_macros , <EOL> undef_macros = undef_macros ) <EOL> else : <EOL> if '<STR_LIT:code>' in self . code_sections : <EOL> op_code = self . code_sections [ '<STR_LIT:code>' ] <EOL> def_macros , undef_macros = self . get_c_macros ( node , name ) <EOL> def_sub , undef_sub = self . get_sub_macros ( sub ) <EOL> def_io , undef_io = self . get_io_macros ( inp , out ) <EOL> return os . linesep . join ( [ def_macros , def_sub , def_io , <EOL> op_code , <EOL> undef_io , undef_sub , undef_macros ] ) <EOL> else : <EOL> raise utils . MethodNotDefined ( <EOL> '<STR_LIT>' , type ( self ) , type ( self ) . __name__ ) <EOL> def c_code_cleanup ( self , node , name , inputs , outputs , sub ) : <EOL> if '<STR_LIT>' in self . code_sections : <EOL> op_code = self . code_sections [ '<STR_LIT>' ] <EOL> def_macros , undef_macros = self . get_c_macros ( node , name ) <EOL> def_sub , undef_sub = self . get_sub_macros ( sub ) <EOL> def_io , undef_io = self . get_io_macros ( inputs , outputs ) <EOL> return os . linesep . join ( [ def_macros , def_sub , def_io , <EOL> op_code , <EOL> undef_io , undef_sub , undef_macros ] ) <EOL> else : <EOL> raise utils . MethodNotDefined ( <EOL> '<STR_LIT>' , type ( self ) , type ( self ) . __name__ ) </s>
