Skip to content

Commit

Permalink
Merge pull request #5525 from toslunar/use-cupy
Browse files Browse the repository at this point in the history
Use `cupy.linalg.det` in `F.det`
  • Loading branch information
kmaehashi committed Nov 16, 2018
2 parents faec814 + 529b56a commit d3da91c
Showing 1 changed file with 4 additions and 47 deletions.
51 changes: 4 additions & 47 deletions chainer/functions/math/det.py
@@ -1,49 +1,11 @@
import numpy

import chainer
from chainer.backends import cuda
from chainer import function_node
import chainer.functions
from chainer.functions.math import matmul
from chainer import utils
from chainer.utils import precision
from chainer.utils import type_check


def _det_gpu(b):
# We do a batched LU decomposition on the GPU to compute
# and compute the determinant by multiplying the diagonal.
# Change the shape of the array to be size=1 minibatch if necessary.
# Also copy the matrix as the elments will be modified in-place.
a = matmul._as_batch_mat(b).copy()
n = a.shape[1]
n_matrices = len(a)
# Pivot array
p = cuda.cupy.zeros((n_matrices, n), dtype='int32')
# Output array
# These arrays hold information on the execution success
# or if the matrix was singular.
info = cuda.cupy.zeros(n_matrices, dtype=numpy.intp)
ap = matmul._mat_ptrs(a)
_, lda = matmul._get_ld(a)
if b.dtype == numpy.float32:
cuda.cublas.sgetrfBatched(cuda.Device().cublas_handle, n, ap.data.ptr,
lda, p.data.ptr, info.data.ptr, n_matrices)
elif b.dtype == numpy.float64:
cuda.cublas.dgetrfBatched(cuda.Device().cublas_handle, n, ap.data.ptr,
lda, p.data.ptr, info.data.ptr, n_matrices)
else:
assert False
det = cuda.cupy.prod(a.diagonal(axis1=1, axis2=2), axis=1)
# The determinant is equal to the product of the diagonal entries
# of `a` where the sign of `a` is flipped depending on whether
# the pivot array is equal to its index.
rng = cuda.cupy.arange(1, n + 1, dtype='int32')
parity = cuda.cupy.sum(p != rng, axis=1) % 2
sign = 1. - 2. * parity.astype(b.dtype, copy=False)
return det * sign, info


class BatchDet(function_node.FunctionNode):

@property
Expand All @@ -61,17 +23,12 @@ def check_type_forward(self, in_types):
type_check.expect(a_type.shape[-1] == a_type.shape[-2])

@precision._fp16_mixed_precision_helper
def forward_cpu(self, x):
self.retain_inputs((0,))
self.retain_outputs((0,))
detx = utils.force_array(numpy.linalg.det(x[0]))
return detx,

@precision._fp16_mixed_precision_helper
def forward_gpu(self, x):
def forward(self, inputs):
self.retain_inputs((0,))
self.retain_outputs((0,))
detx, _ = _det_gpu(x[0])
x, = inputs
xp = cuda.get_array_module(x)
detx = xp.linalg.det(x)
return detx,

def backward(self, indexes, gy):
Expand Down

0 comments on commit d3da91c

Please sign in to comment.