Using Op params

The Op params is a facility to pass some runtime parameters to thecode of an op without modifying it. It can enable a single instanceof C code to serve different needs and therefore reduce compilation.

The code enables you to pass a single object, but it can be a structor python object with multiple values if you have more than one valueto pass.

We will first introduce the parts involved in actually using thisfunctionality and then present a simple working example.

The params type

You can either reuse an existing type such as Generic orcreate your own.

Using a python object for your op parameters (Generic) can beannoying to access from C code since you would have to go through thePython-C API for all accesses.

Making a purpose-built class may require more upfront work, but canpay off if you reuse the type for a lot of Ops, by not having to re-doall of the python manipulation.

The params object

The object that you use to store your param values must be hashableand comparable for equality, because it will be stored in a dictionaryat some point. Apart from those requirements it can be anything thatmatches what you have declared as the params type.

Defining a params type

Note

This section is only relevant if you decide to create your own type.

The first thing you need to do is to define a Theano Type for yourparams object. It doesn’t have to be complete type because only thefollowing methods will be used for the type:

Additionaly if you want to use your params with C code, you need thefollowing methods:

You can also define other convenience methods such asc_headers if you need any special things.

Registering the params with your Op

To declare that your Op uses params you have to set the classattribute params_type to an instance of your params Type.

Note

If you want to have multiple parameters you have to bundle thoseinside a single object and use that as the params type.

For example if we decide to use an int as the params the followingwould be appropriate:

  1. class MyOp(Op):
  2. params_type = Generic()

After that you need to define a get_params() method on yourclass with the following signature:

  1. def get_params(self, node)

This method must return a valid object for your Type (an object thatpasses filter()). The node parameter is the Apply node forwhich we want the params. Therefore the params object can depend onthe inputs and outputs of the node.

Note

Due to implementation restrictions, None is not allowed as aparams object and will be taken to mean that the Op doesn’t haveparameters.

Since this will change the expected signature of a few methods, itis strongly discouraged to have your get_params() methodreturn None.

Signature changes from having params

Having declared a params for your Op will affect the expectedsignature of perform(). The new expected signature will have anextra parameter at the end which corresponds to the params object.

Warning

If you do not account for this extra parameter, the code will failat runtime if it tries to run the python version.

Also, for the C code, the sub dictionary will contain an extra entry‘params’ which will map to the variable name of the params object.This is true for all methods that recieve a sub parameter, so thismeans that you can use your params in the c_codeand c_init_code_struct method.

A simple example

This is a simple example which uses a params object to pass a value.This Op will multiply a scalar input by a fixed floating point value.

Since the value in this case is a python float, we chose Generic asthe params type.

  1. from theano import Op
  2. from theano.gof.type import Generic
  3. from theano.scalar import as_scalar
  4.  
  5. class MulOp(Op):
  6. params_type = Generic()
  7. __props__ = ('mul',)
  8.  
  9. def __init__(self, mul):
  10. self.mul = float(mul)
  11.  
  12. def get_params(self, node):
  13. return self.mul
  14.  
  15. def make_node(self, inp):
  16. inp = as_scalar(inp)
  17. return Apply(self, [inp], [inp.type()])
  18.  
  19. def perform(self, node, inputs, output_storage, params):
  20. # Here params is a python float so this is ok
  21. output_storage[0][0] = inputs[0] * params
  22.  
  23. def c_code(self, node, name, inputs, outputs, sub):
  24. return ("%(z)s = %(x)s * PyFloat_AsDouble(%(p)s);" %
  25. dict(z=outputs[0], x=inputs[0], p=sub['params']))

A more complex example

This is a more complex example which actually passes multiple values.It does a linear combination of two values using floating pointweights.

  1. from theano import Op
  2. from theano.gof.type import Generic
  3. from theano.scalar import as_scalar
  4.  
  5. class ab(object):
  6. def __init__(self, alpha, beta):
  7. self.alpha = alpha
  8. self.beta = beta
  9.  
  10. def __hash__(self):
  11. return hash((type(self), self.alpha, self.beta))
  12.  
  13. def __eq__(self, other):
  14. return (type(self) == type(other) and
  15. self.alpha == other.alpha and
  16. self.beta == other.beta)
  17.  
  18.  
  19. class Mix(Op):
  20. params_type = Generic()
  21. __props__ = ('alpha', 'beta')
  22.  
  23. def __init__(self, alpha, beta):
  24. self.alpha = alpha
  25. self.beta = beta
  26.  
  27. def get_params(self, node):
  28. return ab(alpha=self.alpha, beta=self.beta)
  29.  
  30. def make_node(self, x, y):
  31. x = as_scalar(x)
  32. y = as_scalar(y)
  33. return Apply(self, [x, y], [x.type()])
  34.  
  35. def c_support_code_struct(self, node, name):
  36. return """
  37. double alpha_%(name)s;
  38. double beta_%(name)s;
  39. """ % dict(name=name)
  40.  
  41. def c_init_code_struct(self, node, name, sub):
  42. return """{
  43. PyObject *tmp;
  44. tmp = PyObject_GetAttrString(%(p)s, "alpha");
  45. if (tmp == NULL)
  46. %(fail)s
  47. alpha_%(name)s = PyFloat_AsDouble(tmp);
  48. Py_DECREF(%(tmp)s);
  49. if (PyErr_Occurred())
  50. %(fail)s
  51. tmp = PyObject_GetAttrString(%(p)s, "beta");
  52. if (tmp == NULL)
  53. %(fail)s
  54. beta_%(name)s = PyFloat_AsDouble(tmp);
  55. Py_DECREF(tmp);
  56. if (PyErr_Occurred())
  57. %(fail)s
  58. }""" % dict(name=name, p=sub['params'], fail=sub['fail'])
  59.  
  60. def c_code(self, node, name, inputs, outputs, sub):
  61. return """
  62. %(z)s = alpha_%(name)s * %(x)s + beta_%(name)s * %(y)s;
  63. """ % dict(name=name, z=outputs[0], x=inputs[0], y=inputs[1])