def c_headers(self): if pygpu.get_default_context().kind == 'opencl': raise MethodNotDefined('cuda only') return [ 'cuda.h', '<numpy_compat.h>', '<gpuarray_helper.h>', '<gpuarray/types.h>' ]
def gpu_kernels(self, node, name): """ This is the method to override. This should return an iterable of Kernel objects that describe the kernels this op will need. """ raise MethodNotDefined('gpu_kernels')
def c_sync(self, name, sub): """ Required: Return C code to pack C types back into a PyObject. The code returned from this function must be templated using "%(name)s", representing the name that the caller wants to call this Variable. The returned code may set "py_%(name)s" to a PyObject* and that PyObject* will be accessible from Python via variable.data. Do not forget to adjust reference counts if "py_%(name)s" is changed from its original value. Parameters ---------- name : WRITEME WRITEME sub : WRITEME WRITEME Raises ------ MethodNotDefined Subclass does not implement this method. """ raise MethodNotDefined("c_sync", type(self), self.__class__.__name__)
def c_declare(self, name, sub, check_input=True): """Required: Return c code to declare variables that will be instantiated by `c_extract`. Example: .. code-block: python return "PyObject ** addr_of_%(name)s;" :param name: the name of the ``PyObject *`` pointer that will the value for this Type :type name: string :param sub: a dictionary of special codes. Most importantly sub['fail']. See CLinker for more info on `sub` and ``fail``. :type sub: dict string -> string :note: It is important to include the `name` inside of variables which are declared here, so that name collisions do not occur in the source file that is generated. :note: The variable called ``name`` is not necessarily defined yet where this code is inserted. This code might be inserted to create class variables for example, whereas the variable ``name`` might only exist inside certain functions in that class. :todo: Why should variable declaration fail? Is it even allowed to? :Exceptions: - `MethodNotDefined`: Subclass does not implement this method """ raise MethodNotDefined()
def c_header_dirs(self): if pygpu.get_default_context().kind == 'opencl': raise MethodNotDefined('cuda only') cuda_root = config.cuda.root res = [os.path.dirname(__file__)] if cuda_root: res.append(os.path.join(cuda_root, 'include')) return res
def f(self, node, name): if tag in self.code_sections: code = self.code_sections[tag] define_macros, undef_macros = self.get_c_macros(node, name) return "\n".join(["", define_macros, code, undef_macros]) else: raise MethodNotDefined("c_" + tag, type(self), type(self).__name__)
def c_init_code(self): """ Get the code section for init_code """ if "init_code" in self.code_sections: return [self.code_sections["init_code"]] else: raise MethodNotDefined("c_init_code", type(self), type(self).__name__)
def c_literal(self, data): """Optional: WRITEME :Parameters: - `data`: WRITEME WRITEME :Exceptions: - `MethodNotDefined`: Subclass does not implement this method """ raise MethodNotDefined("c_literal", type(self), self.__class__.__name__)
def c_element_type(self): """ Optional: Return the name of the primitive C type of items into variables handled by this type. e.g: - For ``TensorType(dtype='int64', ...)``: should return ``"npy_int64"``. - For ``GpuArrayType(dtype='int32', ...)``: should return ``"ga_int"``. """ raise MethodNotDefined("c_element_type", type(self), self.__class__.__name__)
def c_code(self, node, name, inp, out, sub): if self.func_name is not None: assert "code" not in self.code_sections define_macros, undef_macros = self.get_c_macros( node, name, check_input=False ) params = "" if "params" in sub: params = f", {sub['params']}" # Generate the C code return """ %(define_macros)s { if (%(func_name)s(%(func_args)s%(params)s) != 0) { %(fail)s } } %(undef_macros)s """ % dict( func_name=self.func_name, fail=sub["fail"], params=params, func_args=self.format_c_function_args(inp, out), define_macros=define_macros, undef_macros=undef_macros, ) else: if "code" in self.code_sections: op_code = self.code_sections["code"] def_macros, undef_macros = self.get_c_macros(node, name) def_sub, undef_sub = self.get_sub_macros(sub) def_io, undef_io = self.get_io_macros(inp, out) return "\n".join( [ def_macros, def_sub, def_io, op_code, undef_io, undef_sub, undef_macros, ] ) else: raise MethodNotDefined("c_code", type(self), type(self).__name__)
def get_params(self, node): if hasattr(self, "params_type") and isinstance(self.params_type, theano.gof.ParamsType): wrapper = self.params_type if not all(hasattr(self, field) for field in wrapper.fields): # Let's print missing attributes for debugging. not_found = tuple(field for field in wrapper.fields if not hasattr(self, field)) raise AttributeError( f"{type(self).__name__}: missing attributes {not_found} for ParamsType." ) # ParamsType.get_params() will apply filtering to attributes. return self.params_type.get_params(self) raise MethodNotDefined("get_params")
def c_init_code_struct(self, node, name, sub): """ Stitches all the macros and "init_code" together """ if "init_code_struct" in self.code_sections: op_code = self.code_sections["init_code_struct"] def_macros, undef_macros = self.get_c_macros(node, name) def_sub, undef_sub = self.get_sub_macros(sub) return "\n".join( ["", def_macros, def_sub, op_code, undef_sub, undef_macros]) else: raise MethodNotDefined("c_init_code_struct", type(self), type(self).__name__)
def c_literal(self, data): """ Optional: WRITEME Parameters ---------- data : WRITEME WRITEME Raises ------ MethodNotDefined Subclass does not implement this method. """ raise MethodNotDefined("c_literal", type(self), self.__class__.__name__)
def perform(self, node, inputs, output_storage, params=None): """ Required: Calculate the function on the inputs and put the variables in the output storage. Return None. Parameters ---------- node : Apply The symbolic `Apply` node that represents this computation. inputs : Sequence Immutable sequence of non-symbolic/numeric inputs. These are the values of each `Variable` in `node.inputs`. output_storage : list of list List of mutable single-element lists (do not change the length of these lists). Each sub-list corresponds to value of each `Variable` in `node.outputs`. The primary purpose of this method is to set the values of these sub-lists. params : tuple A tuple containing the values of each entry in `__props__`. Notes ----- The `output_storage` list might contain data. If an element of output_storage is not `None`, it has to be of the right type, for instance, for a `TensorVariable`, it has to be a NumPy `ndarray` with the right number of dimensions and the correct dtype. Its shape and stride pattern can be arbitrary. It is not guaranteed that such pre-set values were produced by a previous call to this `Op.perform`; they could've been allocated by another `Op`'s `perform` method. A `Op` is free to reuse `output_storage` as it sees fit, or to discard it and allocate new memory. Raises ------ MethodNotDefined The subclass does not override this method. """ raise MethodNotDefined( "perform", type(self), self.__class__.__name__, "Did you used Theano flags mode=FAST_COMPILE?" " You can use optimizer=fast_compile instead.", )
def c_init(self, name, sub): """Required: Return c code to initialize the variables that were declared by self.c_declare() Example: .. code-block: python return "addr_of_%(name)s = NULL;" :note: The variable called ``name`` is not necessarily defined yet where this code is inserted. This code might be inserted in a class constructor for example, whereas the variable ``name`` might only exist inside certain functions in that class. :todo: Why should variable initialization fail? Is it even allowed to? """ raise MethodNotDefined("c_init", type(self), self.__class__.__name__)
def filter(self, data, strict=False, allow_downcast=None): """Required: Return data or an appropriately wrapped/converted data. Subclass implementation should raise a TypeError exception if the data is not of an acceptable type. If strict is True, the data returned must be the same as the data passed as an argument. If it is False, and allow_downcast is True, filter may cast it to an appropriate type. If allow_downcast is False, filter may only upcast it, not lose precision. If allow_downcast is None (default), the behaviour can be Type-dependent, but for now it means only Python floats can be downcasted, and only to floatX scalars. :Exceptions: - `MethodNotDefined`: subclass doesn't implement this function. """ raise MethodNotDefined("filter", type(self), self.__class__.__name__)
def c_extract(self, name, sub, check_input=True): """ Required: Return c code to extract a PyObject * instance. The code returned from this function must be templated using ``%(name)s``, representing the name that the caller wants to call this `Variable`. The Python object self.data is in a variable called "py_%(name)s" and this code must set the variables declared by c_declare to something representative of py_%(name)s. If the data is improper, set an appropriate exception and insert "%(fail)s". TODO: Point out that template filling (via sub) is now performed by this function. --jpt Parameters ---------- name : str The name of the ``PyObject *`` pointer that will store the value for this Type. sub : dict string -> string A dictionary of special codes. Most importantly sub['fail']. See CLinker for more info on `sub` and ``fail``. Raises ------ MethodNotDefined Subclass does not implement this method. Examples -------- .. code-block: python return "if (py_%(name)s == Py_None)" + \\\ addr_of_%(name)s = &py_%(name)s;" + \\\ "else" + \\\ { PyErr_SetString(PyExc_ValueError, \\\ 'was expecting None'); %(fail)s;}" """ raise MethodNotDefined("c_extract", type(self), self.__class__.__name__)
def c_cleanup(self, name, sub): """Return c code to clean up after `c_extract`. This returns C code that should deallocate whatever `c_extract` allocated or decrease the reference counts. Do not decrease py_%(name)s's reference count. WRITEME :Parameters: - `name`: WRITEME WRITEME - `sub`: WRITEME WRITEME :Exceptions: - `MethodNotDefined`: Subclass does not implement this method """ raise MethodNotDefined()
def c_code_cleanup(self, node, name, inputs, outputs, sub): """ Stitches all the macros and "code_cleanup" together """ if "code_cleanup" in self.code_sections: op_code = self.code_sections["code_cleanup"] def_macros, undef_macros = self.get_c_macros(node, name) def_sub, undef_sub = self.get_sub_macros(sub) def_io, undef_io = self.get_io_macros(inputs, outputs) return "\n".join([ def_macros, def_sub, def_io, op_code, undef_io, undef_sub, undef_macros, ]) else: raise MethodNotDefined("c_code_cleanup", type(self), type(self).__name__)
def c_cleanup(self, name, sub): """ Return C code to clean up after `c_extract`. This returns C code that should deallocate whatever `c_extract` allocated or decrease the reference counts. Do not decrease py_%(name)s's reference count. WRITEME Parameters ---------- name : WRITEME WRITEME sub : WRITEME WRITEME Raises ------ MethodNotDefined Subclass does not implement this method. """ raise MethodNotDefined()
def f(self): if tag in self.code_sections: return self.code_sections[tag] else: raise MethodNotDefined("c_" + tag, type(self), type(self).__name__)
def __hide(*args): raise MethodNotDefined()