Ejemplo n.º 1
0
 def make_node(self, A):
     ctx_name = infer_context_name(A)
     A = as_gpuarray_variable(A, ctx_name)
     A = gpu_contiguous(A)
     if A.ndim != 2:
         raise LinAlgError("Matrix rank error")
     if A.dtype != "float32":
         raise TypeError("only `float32` is supported for now")
     if self.compute_uv:
         return aesara.Apply(
             self,
             [A],
             # return S, U, VT
             [
                 GpuArrayType(
                     A.dtype, broadcastable=[False], context_name=ctx_name
                 )(),
                 A.type(),
                 A.type(),
             ],
         )
     else:
         return aesara.Apply(
             self,
             [A],
             # return only S
             [GpuArrayType(A.dtype, broadcastable=[False], context_name=ctx_name)()],
         )
Ejemplo n.º 2
0
 def make_node(self, x):
     x = basic.as_tensor_variable(x)
     self_axis = self.axis
     if self_axis is None:
         broadcastable = [False]
     else:
         if self_axis < 0:
             self_axis += len(x.broadcastable)
         if self_axis < 0 or self_axis >= len(x.broadcastable):
             raise RuntimeError(
                 "Unique axis `{}` is outside of input ndim = "
                 "{}.".format(self.axis, len(x.broadcastable)))
         broadcastable = [
             b if axis != self_axis else False
             for axis, b in enumerate(x.broadcastable)
         ]
     outputs = [
         basic.TensorType(broadcastable=broadcastable, dtype=x.dtype)()
     ]
     typ = basic.TensorType(broadcastable=[False], dtype="int64")
     if self.return_index:
         outputs.append(typ())
     if self.return_inverse:
         outputs.append(typ())
     if self.return_counts:
         outputs.append(typ())
     return aesara.Apply(self, [x], outputs)
Ejemplo n.º 3
0
    def make_node(self, inp1, inp2):
        if not cusolver_available:
            raise RuntimeError(
                "CUSOLVER is not available and "
                "GpuCusolverSolve Op can not be constructed."
            )
        if skcuda.__version__ <= "0.5.1":
            warnings.warn(
                "The GpuSolve op requires scikit-cuda > 0.5.1 to work with CUDA 8"
            )
        context_name = infer_context_name(inp1, inp2)

        inp1 = as_gpuarray_variable(inp1, context_name)
        inp2 = as_gpuarray_variable(inp2, context_name)

        inp1 = gpu_contiguous(inp1)
        inp2 = gpu_contiguous(inp2)

        assert inp1.ndim == 2
        assert inp2.ndim == 2
        assert inp1.dtype == inp2.dtype

        return aesara.Apply(
            self,
            [inp1, inp2],
            [
                GpuArrayType(
                    inp1.dtype,
                    broadcastable=inp1.broadcastable,
                    context_name=context_name,
                )()
            ],
        )
Ejemplo n.º 4
0
    def make_node(self, inp1, inp2):
        if not cublas_available:
            raise RuntimeError(
                "CUBLAS is not available and "
                "GpuCublasTriangularSolve Op "
                "can not be constructed."
            )
        context_name = infer_context_name(inp1, inp2)

        inp1 = as_gpuarray_variable(inp1, context_name)
        inp2 = as_gpuarray_variable(inp2, context_name)

        inp1 = gpu_contiguous(inp1)
        inp2 = gpu_contiguous(inp2)

        assert inp1.ndim == 2
        assert inp2.ndim in [1, 2]
        assert inp1.dtype == inp2.dtype

        return aesara.Apply(
            self,
            [inp1, inp2],
            [
                GpuArrayType(
                    inp1.dtype,
                    broadcastable=inp2.broadcastable,
                    context_name=context_name,
                )()
            ],
        )
Ejemplo n.º 5
0
    def make_node(self, inp, s=None):
        # A shape parameter s can be provided as an input. For now this is used to
        # manage odd transform sizes.
        # Later this could be extended to handle padding and trunkation,
        # following numpy's interface. However, cuFFT expects array that match
        # the shape given to the plan, so padding will have to be done in the op.
        # The effect of padding on gradients has yet to be investigated.

        if not skcuda_available:
            raise RuntimeError("skcuda is needed for CuFFTOp")

        if not pygpu_available:
            raise RuntimeError("pygpu is needed for CuFFTOp")

        if not pycuda_available:
            raise RuntimeError("pycuda is needed for CuFFTOp")

        inp = gpu_contiguous(as_gpuarray_variable(inp,
                                                  infer_context_name(inp)))

        # If no shape is provided as input, default to input data shape.
        if s is None:
            s = inp.shape[1:]
        s = tt.as_tensor_variable(s)

        assert inp.dtype == "float32"
        assert s.ndim == 1
        assert s.dtype in tt.integer_dtypes

        return aesara.Apply(self, [inp, s], [self.output_type(inp)()])
Ejemplo n.º 6
0
 def make_node(self, x, v, sorter=None):
     x = basic.as_tensor(x, ndim=1)
     v = basic.as_tensor(v)
     out_type = v.type.clone(dtype="int64")
     if sorter is None:
         return aesara.Apply(self, [x, v], [out_type()])
     else:
         sorter = basic.as_tensor(sorter, ndim=1)
         if (aesara.configdefaults.python_int_bitwidth() == 32
                 and sorter.dtype == "int64"):
             raise TypeError(
                 "numpy.searchsorted with Python 32bit do not support a"
                 " sorter of int64.")
         if sorter.type not in basic.int_vector_types:
             raise TypeError("sorter must be an integer vector",
                             sorter.type)
         return aesara.Apply(self, [x, v, sorter], [out_type()])
 def make_node(self, x, y):
     x = aesara.tensor.as_tensor_variable(x)
     y = aesara.tensor.as_tensor_variable(y)
     outdim = x.ndim
     output = aesara.tensor.TensorType(
         dtype=aesara.scalar.upcast(x.dtype, y.dtype), broadcastable=[False] * outdim
     )()
     return aesara.Apply(self, inputs=[x, y], outputs=[output])
Ejemplo n.º 8
0
 def make_node(self, x):
     # x could be one of a number of types
     # the only thing we require is that the variable have a .ndim,
     # and that the value have a .shape
     if not isinstance(x, aesara.Variable):
         raise TypeError("x must be Variable with ndim attribute", x)
     if x.ndim <= self.i:
         raise TypeError("x has too few dimensions for Shape_i", (x, self.i))
     return aesara.Apply(self, [x], [aesara.tensor.lscalar()])
Ejemplo n.º 9
0
 def make_node(self, input, axis=-1):
     input = aesara.tensor.as_tensor_variable(input)
     axis = aesara.tensor.as_tensor_variable(axis)
     bcast = input.type.broadcastable
     return aesara.Apply(
         self,
         [input, axis],
         [aesara.tensor.TensorType(dtype="int64", broadcastable=bcast)()],
     )
Ejemplo n.º 10
0
    def make_node(self, a, *shape):
        a = basic.as_tensor_variable(a)
        shape = basic.as_tensor_variable(shape, ndim=1)

        shape, bcast = basic.alloc_validate_shape(shape)

        out = type(a.type)(dtype=a.type.dtype, broadcastable=bcast)()

        return aesara.Apply(self, [a] + shape, [out])
Ejemplo n.º 11
0
 def make_node(self, A):
     ctx_name = infer_context_name(A)
     A = as_gpuarray_variable(A, ctx_name)
     A = gpu_contiguous(A)
     if A.ndim != 2:
         raise LinAlgError("Matrix rank error")
     if A.dtype != "float32":
         raise TypeError("only `float32` is supported for now")
     return aesara.Apply(self, [A], [A.type()])
Ejemplo n.º 12
0
    def make_node(self, x):
        x = basic.as_tensor_variable(x)
        out_type = x.type()

        if self.axis is None:
            out_type = aesara.tensor.vector(dtype=x.dtype)  # Flatten
        elif self.axis >= x.ndim or self.axis < -x.ndim:
            raise ValueError("axis(={}) out of bounds".format(self.axis))

        return aesara.Apply(self, [x], [out_type])
Ejemplo n.º 13
0
 def make_node(self, x, y, rcond):
     x = aesara.tensor.as_tensor_variable(x)
     y = aesara.tensor.as_tensor_variable(y)
     rcond = aesara.tensor.as_tensor_variable(rcond)
     return aesara.Apply(
         self,
         [x, y, rcond],
         [
             aesara.tensor.matrix(),
             aesara.tensor.dvector(),
             aesara.tensor.lscalar(),
             aesara.tensor.dvector(),
         ],
     )
Ejemplo n.º 14
0
    def make_node(self, activations, labels, input_lengths):
        context_name = infer_context_name(activations)
        t_activations = as_gpuarray_variable(activations,
                                             context_name=context_name)
        # Ensure activations array is C-contiguous
        t_activations = gpu_contiguous(t_activations)

        # Labels and input lengths are always on the CPU
        t_labels = tt.as_tensor_variable(labels)
        t_input_lengths = tt.as_tensor_variable(input_lengths)

        if t_activations.type.dtype != "float32":
            raise TypeError("activations must use the float32 type.")

        if t_activations.ndim != 3:
            raise ValueError("activations must have 3 dimensions.")

        if t_labels.type.dtype != "int32":
            raise TypeError("labels must use the int32 type.")

        if t_labels.ndim != 2:
            raise ValueError("labels must have 2 dimensions.")

        if t_input_lengths.type.dtype != "int32":
            raise TypeError("input_lengths must use the int32 type.")

        if t_input_lengths.ndim != 1:
            raise ValueError("input_lengths must have 1 dimension.")

        costs = GpuArrayType(dtype="float32",
                             broadcastable=(False, ),
                             context_name=context_name)()
        outputs = [costs]

        if self.compute_grad:
            gradients = GpuArrayType(
                dtype="float32",
                broadcastable=(
                    False,
                    False,
                    False,
                ),
                context_name=context_name,
            )()
            outputs += [gradients]

        return aesara.Apply(self,
                            inputs=[t_activations, t_labels, t_input_lengths],
                            outputs=outputs)
Ejemplo n.º 15
0
    def make_node(self, x, repeats):
        x = basic.as_tensor_variable(x)
        repeats = basic.as_tensor_variable(repeats)

        if repeats.dtype not in basic.integer_dtypes:
            raise TypeError("repeats.dtype must be an integer.")

        # Some dtypes are not supported by numpy's implementation of repeat.
        # Until another one is available, we should fail at graph construction
        # time, not wait for execution.
        ptr_bitwidth = aesara.configdefaults.local_bitwidth()
        if ptr_bitwidth == 64:
            numpy_unsupported_dtypes = ("uint64", )
        if ptr_bitwidth == 32:
            numpy_unsupported_dtypes = ("uint32", "int64", "uint64")

        if repeats.dtype in numpy_unsupported_dtypes:
            raise TypeError(
                ("dtypes %s are not supported by numpy.repeat "
                 "for the 'repeats' parameter, " %
                 str(numpy_unsupported_dtypes)),
                repeats.dtype,
            )

        if self.axis is None:
            broadcastable = [False]
        else:
            try:
                const_reps = basic.get_scalar_constant_value(repeats)
            except basic.NotScalarConstantError:
                const_reps = None
            if const_reps == 1:
                broadcastable = x.broadcastable
            else:
                broadcastable = list(x.broadcastable)
                broadcastable[self.axis] = False

        out_type = aesara.tensor.TensorType(x.dtype, broadcastable)

        return aesara.Apply(self, [x, repeats], [out_type()])
Ejemplo n.º 16
0
    def make_node(self, inp, kth):
        inp = aesara.tensor.as_tensor_variable(inp)
        ndim = inp.ndim
        if ndim == 0:
            raise ValueError("Cannot take scalar as input")
        if not -ndim <= self.axis < ndim:
            raise IndexError(
                '"axis" parameter out of range,'
                " expected integer within [%d, %d]" % (-ndim, ndim - 1)
            )

        kth = aesara.tensor.as_tensor_variable(kth)
        _check_tensor_is_scalar(kth)
        bcast = inp.type.broadcastable
        outs = []
        if self.return_values:
            outs.append(inp.type())
        if self.return_indices:
            outs.append(
                aesara.tensor.TensorType(dtype=self.idx_dtype, broadcastable=bcast)()
            )
        return aesara.Apply(self, [inp, kth], outs)
Ejemplo n.º 17
0
    def make_node(self, inp):
        if not cusolver_available:
            raise RuntimeError(
                "CUSOLVER is not available and "
                "GpuCholesky Op can not be constructed."
            )
        if skcuda.__version__ <= "0.5.1":
            warnings.warn(
                "The GpuCholesky op requires scikit-cuda > " "0.5.1 to work with CUDA 8"
            )
        if not pygpu_available:
            raise RuntimeError(
                "Missing pygpu or triu/tril functions." "Install or update libgpuarray."
            )
        context_name = infer_context_name(inp)

        inp = as_gpuarray_variable(inp, context_name)

        inp = gpu_contiguous(inp)

        assert inp.ndim == 2

        return aesara.Apply(self, [inp], [inp.type()])
Ejemplo n.º 18
0
 def make_node(self, input, axis=-1):
     input = aesara.tensor.as_tensor_variable(input)
     axis = aesara.tensor.as_tensor_variable(axis)
     out_type = input.type()
     return aesara.Apply(self, [input, axis], [out_type])
Ejemplo n.º 19
0
 def make_node(self, inp):
     inp = cuda.basic_ops.gpu_contiguous(
         cuda.basic_ops.as_cuda_ndarray_variable(inp))
     assert inp.dtype == "float32"
     return aesara.Apply(self, [inp], [inp.type()])
Ejemplo n.º 20
0
 def make_node(self, x):
     return aesara.Apply(self,
                         inputs=[x],
                         outputs=[x.type(),
                                  aesara.tensor.scalar()])
Ejemplo n.º 21
0
 def make_node(self, x):
     x = aesara.tensor.as_tensor_variable(x)
     return aesara.Apply(self, [x], [x.type()])
Ejemplo n.º 22
0
 def make_node(self, f, g):
     return aesara.Apply(self,
                         inputs=[f, g],
                         outputs=[aesara.tensor.scalar()])
Ejemplo n.º 23
0
 def make_node(self, x):
     return aesara.Apply(self, [x], [x.type()])