tensor.opt – Tensor Optimizations

class theano.tensor.opt.Assert(msg='Theano Assert failed!')[source]

Implements assertion in a computational graph.

Returns the first parameter if the condition is true, otherwise, triggers AssertionError.


This Op is a debugging feature. It can be removed from the graph because of optimizations, and can hide some possible optimizations to the optimizer. Specifically, removing happens if it can be determined that condition will always be true. Also, the output of the Op must be used in the function computing the graph, but it doesn’t have to be returned.


>>> import theano
>>> T = theano.tensor
>>> x = T.vector('x')
>>> assert_op = T.opt.Assert()
>>> func = theano.function([x], assert_op(x, x.size<2))
class theano.tensor.opt.Canonizer(main, inverse, reciprocal, calculate, use_reciprocal=True)[source]

Simplification tool. The variable is a local_optimizer. It is best used with a TopoOptimizer in in_to_out order.

Usage: Canonizer(main, inverse, reciprocal, calculate)

  • main – A suitable Op class that is commutative, associative and takes one to an arbitrary number of inputs, e.g. add or mul
  • inverse – An Op class such that inverse(main(x, y), y) == x e.g. sub or true_div
  • reciprocal – A function such that main(x, reciprocal(y)) == inverse(x, y) e.g. neg or inv
  • calculate – Function that takes a list of numpy.ndarray instances for the numerator, another list for the denumerator, and calculates inverse(main(*num), main(*denum)). It takes a keyword argument, aslist. If True, the value should be returned as a list of one element, unless the value is such that value = main(). In that case, the return value should be an empty list.


>>> import theano.tensor as T
>>> from theano.tensor.opt import Canonizer
>>> add_canonizer = Canonizer(T.add, T.sub, T.neg, \
...                           lambda n, d: sum(n) - sum(d))
>>> mul_canonizer = Canonizer(T.mul, T.true_div, T.inv, \
...                           lambda n, d: prod(n) / prod(d))

Examples of optimizations mul_canonizer can perform:

x / x -> 1
(x * y) / x -> y
x / y / x -> 1 / y
x / y / z -> x / (y * z)
x / (y / z) -> (x * z) / y
(a / b) * (b / c) * (c / d) -> a / d
(2.0 * x) / (4.0 * y) -> (0.5 * x) / y
2 * x / 2 -> x
x * y * z -> Elemwise(T.mul){x,y,z} #only one pass over the memory.
!-> Elemwise(T.mul){x,Elemwise(T.mul){y,z}}
static get_constant(v)[source]
Returns:A numeric constant if v is a Constant or, well, a numeric constant. If v is a plain Variable, returns None.
Return type:object

This extract two lists, num and denum, such that the input is: self.inverse(self.main(*num), self.main(*denum)). It returns the two lists in a (num, denum) pair.

For example, for main, inverse and reciprocal = *, / and inv(),

input -> returned value (num, denum)
x*y -> ([x, y], [])
inv(x) -> ([], [x])
inv(x) * inv(y) -> ([], [x, y])
x*y/z -> ([x, y], [z])
log(x) / y * (z + x) / y -> ([log(x), z + x], [y, y])
(((a / b) * c) / d) -> ([a, c], [b, d])
a / (b / c) -> ([a, c], [b])
log(x) -> ([log(x)], [])
x**y -> ([x**y], [])
x * y * z -> ([x, y, z], [])
merge_num_denum(num, denum)[source]

Utility function which takes two lists, num and denum, and returns something which is equivalent to inverse(main(*num), main(*denum)), but depends on the length of num and the length of denum (in order to minimize the number of operations).

Let n = len(num) and d = len(denum):

n=0, d=0: neutral element (given by self.calculate([], []))
(for example, this would be 0 if main is addition
and 1 if main is multiplication)
n=1, d=0: num[0]
n=0, d=1: reciprocal(denum[0])
n=1, d=1: inverse(num[0], denum[0])
n=0, d>1: reciprocal(main(*denum))
n>1, d=0: main(*num)
n=1, d>1: inverse(num[0], main(*denum))
n>1, d=1: inverse(main(*num), denum[0])
n>1, d>1: inverse(main(*num), main(*denum))

Given the values of n and d to which they are associated, all of the above are equivalent to: inverse(main(*num), main(*denum))

simplify(num, denum, out_type)[source]

Shorthand for:

self.simplify_constants(*self.simplify_factors(num, denum))
simplify_constants(orig_num, orig_denum, out_type=None)[source]

Find all constants and put them together into a single constant.

Finds all constants in orig_num and orig_denum (using get_constant) and puts them together into a single constant. The constant is inserted as the first element of the numerator. If the constant is the neutral element, it is removed from the numerator.


Let main be multiplication:

[2, 3, x], [] -> [6, x], []
[x, y, 2], [4, z] -> [0.5, x, y], [z]
[x, 2, y], [z, 2] -> [x, y], [z]
simplify_factors(num, denum)[source]

For any Variable r which is both in num and denum, removes it from both lists. Modifies the lists inplace. Returns the modified lists. For example:

[x], [x] -> [], []
[x, y], [x] -> [y], []
[a, b], [c, d] -> [a, b], [c, d]
class theano.tensor.opt.FusionOptimizer(local_optimizer)[source]

Graph optimizer for Fusion of elemwise operations.

class theano.tensor.opt.InplaceElemwiseOptimizer(OP)[source]

We parametrise it to make it work for Elemwise and GpuElemwise op.


Usage: InplaceElemwiseOptimizer(op).optimize(fgraph)

Attempts to replace all Broadcast ops by versions of them that operate inplace. It operates greedily: for each Broadcast Op that is encountered, for each output, tries each input to see if it can operate inplace on that input. If so, makes the change and go to the next output or Broadcast Op.


x + y + z -> x += y += z

(x + y) * (x * y) -> (x += y) *= (x * y) or (x + y) *= (x *= y)

class theano.tensor.opt.MakeVector(dtype='int64')[source]

Concatenate a number of scalars together into a vector.

This is a simple version of stack() that introduces far less cruft into the graph. Should work with 0 inputs. The constant_folding optimization will remove it.

class theano.tensor.opt.ShapeFeature[source]

Graph optimizer for removing all calls to shape().

This optimizer replaces all Shapes and Subtensors of Shapes with Shape_i and MakeVector Ops.

This optimizer has several goals:

  1. to ‘lift’ Shapes to as close to the inputs as possible.
  2. to infer the shape of every node in the graph in terms of the input shapes.
  3. remove all fills (T.second, T.fill) from the graph

Lifting shapes as close to the inputs as possible is important for canonicalization because it is very bad form to have to compute something just to know how big it will be. Firstly, it is a waste of time to compute such outputs. But it is important to get rid of these outputs as early as possible in the compilation process because the extra computations make it appear as if many internal graph nodes have multiple clients. Many optimizations refuse to work on nodes with multiple clients.

Lifting is done by using an <Op>.infer_shape function if one is present, or else using a conservative default. An Op that supports shape-lifting should define a infer_shape(self, node, input_shapes) function. The argument input_shapes is a tuple of tuples... there is an interior tuple for each input to the node. The tuple has as many elements as dimensions. The element in position i of tuple j represents the i’th shape component of the j’th input. The function should return a tuple of tuples. One output tuple for each node.output. Again, the i’th element of the j’th output tuple represents the output[j].shape[i] of the function. If an output is not a TensorType, then None should be returned instead of a tuple for that output.

For example the infer_shape for a matrix-matrix product would accept input_shapes=((x0,x1), (y0,y1)) and return ((x0, y1),).

Inferring the shape of internal nodes in the graph is important for doing size-driven optimizations. If we know how big various intermediate results will be, we can estimate the cost of many Ops accurately, and generate c-code that is specific [e.g. unrolled] to particular sizes.

In cases where you cannot figure out the shape, raise a ShapeError.


Right now there is only the ConvOp that could really take advantage of this shape inference, but it is worth it even just for the ConvOp. All that’s necessary to do shape inference is 1) to mark shared inputs as having a particular shape, either via a .tag or some similar hacking; and 2) to add an optional In() argument to promise that inputs will have a certain shape (or even to have certain shapes in certain dimensions). We can’t automatically infer the shape of shared variables as they can change of shape during the execution by default. (NOT IMPLEMENTED YET, BUT IS IN TRAC)

Using Shape information in Optimizations

To use this shape information in OPTIMIZATIONS, use the shape_of dictionary.

For example:

    shape_of = node.fgraph.shape_feature.shape_of
except AttributeError:
    # This can happen when the mode doesn't include the ShapeFeature.

shape_of_output_zero = shape_of[node.output[0]]

The shape_of_output_zero symbol will contain a tuple, whose elements are either integers or symbolic integers.

TODO: check to see if the symbols are necessarily non-constant... or are integer literals sometimes Theano constants?? That would be confusing.

default_infer_shape(node, i_shapes)[source]

Return a list of shape tuple or None for the outputs of node.

This function is used for Ops that don’t implement infer_shape. Ops that do implement infer_shape should use the i_shapes parameter, but this default implementation ignores it.

get_shape(var, idx)[source]

Optimization can call this to get the current shape_i

It is better to call this then use directly shape_of[var][idx] as this method should update shape_of if needed.

TODO: Up to now, we don’t update it in all cases. Update in all cases.


Register r’s shape in the shape_of dictionary.

same_shape(x, y, dim_x=None, dim_y=None)[source]

Return True if we are able to assert that x and y have the same shape.

dim_x and dim_y are optional. If used, they should be an index to compare only 1 dimension of x and y.

set_shape(r, s, override=False)[source]

Assign the shape s to previously un-shaped variable r.

  • r (a variable) –
  • s (None or a tuple of symbolic integers) –
  • override (If False, it mean r is a new object in the fgraph.) – If True, it mean r is already in the fgraph and we want to override its shape.
set_shape_i(r, i, s_i)[source]

Replace element i of shape_of[r] by s_i

shape_ir(i, r)[source]

Return symbolic r.shape[i] for tensor variable r, int i.


Return a tuple of symbolic shape vars for tensor variable r.

unpack(s_i, var)[source]

Return a symbolic integer scalar for the shape element s_i.

The s_i argument was produced by the infer_shape() of an Op subclass.

var: the variable that correspond to s_i. This is just for error reporting.

update_shape(r, other_r)[source]

Replace shape of r by shape of other_r.

If, on some dimensions, the shape of other_r is not informative, keep the shape of r on those dimensions.

class theano.tensor.opt.ShapeOptimizer[source]

Optimizer that serves to add ShapeFeature as an fgraph feature.

class theano.tensor.opt.UnShapeOptimizer[source]

Optimizer remove ShapeFeature as an fgraph feature.


Apply as many times as required the optimization local_useless_rebroadcast and local_rebroadcast_lift.

Parameters:rval (a Variable) –
Return type:A Variable (the same if no optimization can be applied)
theano.tensor.opt.broadcast_like(value, template, fgraph, dtype=None)[source]

Return a Variable with the same shape and dtype as the template, filled by broadcasting value through it. value will be cast as necessary.

theano.tensor.opt.check_for_x_over_absX(numerators, denominators)[source]

Convert x/abs(x) into sign(x).

theano.tensor.opt.encompasses_broadcastable(b1, b2)[source]
  • b1 – The broadcastable attribute of a tensor type.
  • b2 – The broadcastable attribute of a tensor type.

True if the broadcastable patterns b1 and b2 are such that b2 is broadcasted to b1’s shape and not the opposite.

Return type:



Used by erf/erfc opt to track less frequent op.


Used by erf/erfc opt to track less frequent op.

theano.tensor.opt.is_inverse_pair(node_op, prev_op, inv_pair)[source]

Given two consecutive operations, check if they are the provided pair of inverse functions.


Fuse consecutive add or mul in one such node with more inputs.

It is better to fuse add/mul that way then in a Composite node as this make the inner graph of the Composite smaller. This allow to put more computation in a Composite before hitting the max recusion limit when pickling Composite.


As part of specialization, we fuse two consecutive elemwise Ops of the same shape.

For mixed dtype, we let the Composite op do the cast. It lets the C compiler do the cast. The number of dimensions is validated at call time by theano itself.

theano.tensor.opt.local_elemwise_fusion_op(OP, max_input_fct=<function <lambda>>, maker=None)[source]

We parametrize it to make it work for Elemwise and GpuElemwise op.

  • OP – GpuElemwise or Elemwise class (the one that we want to fuse)
  • max_input_fct

    A function that returns the maximum number of inputs that this elemwise can take (useful for GpuElemwise). GPU kernel currently has a limit of 256 bytes for the size of all parameters passed to it. As currently we pass many information only by parameter, we must limit how many ops we fuse together to avoid busting that 256 limit.

    On the CPU we limit to 32 input variables since that is the maximum numpy support.

theano.tensor.opt.merge_two_slices(slice1, len1, slice2, len2)[source]

This function merges two slices into a single slice. The code works on the assumption that:

  1. slice1 is actually a slice and not an index, while slice2 can be just an index.
  2. the two slices have been applied consecutively on the same tensor

The output slice is not in canonical form, but actually just a slice that can be applied to a tensor to produce the same output as applying the two consecutive slices. len1 is the length of the tensor before applying the first slice, while len2 is the length after applying the first slice.

theano.tensor.opt.scalarconsts_rest(inputs, elemwise=True, only_process_constants=False)[source]

Partition a list of variables into two kinds: scalar constants, and the rest.