提交 954c3816 authored 作者: Frédéric Bastien's avatar Frédéric Bastien

Merge pull request #4150 from abergeron/blas_beta_python

Make the python code for Gemv also check for the need to initialize the output with beta=0
...@@ -183,6 +183,24 @@ except ImportError as e: ...@@ -183,6 +183,24 @@ except ImportError as e:
str(e)) str(e))
# If check_init_y() == True we need to initialize y when beta == 0.
def check_init_y():
if check_init_y._result is None:
if not have_fblas:
check_init_y._result = False
y = float('NaN') * numpy.ones((2,))
x = numpy.ones((2,))
A = numpy.ones((2, 2))
gemv = _blas_gemv_fns[y.dtype]
gemv(1.0, A.T, x, 0.0, y, overwrite_y=True, trans=True)
check_init_y._result = numpy.isnan(y).any()
return check_init_y._result
check_init_y._result = None
class Gemv(Op): class Gemv(Op):
""" """
expression is beta * y + alpha * A x expression is beta * y + alpha * A x
...@@ -236,6 +254,9 @@ class Gemv(Op): ...@@ -236,6 +254,9 @@ class Gemv(Op):
'(beta * y + alpha * dot(A, x)). y: %s, A: %s, x: %s ' '(beta * y + alpha * dot(A, x)). y: %s, A: %s, x: %s '
% (y.shape, A.shape, x.shape)) % (y.shape, A.shape, x.shape))
if beta == 0 and check_init_y():
y.fill(0)
# Here I suppose that A is in c order. If we don't make it # Here I suppose that A is in c order. If we don't make it
# explicitly as fortran order, scipy 0.7.2 seam to create # explicitly as fortran order, scipy 0.7.2 seam to create
# a copy in fortran order instead of just reshaping it # a copy in fortran order instead of just reshaping it
...@@ -250,10 +271,11 @@ class Gemv(Op): ...@@ -250,10 +271,11 @@ class Gemv(Op):
out = numpy.dot(A, x) out = numpy.dot(A, x)
if alpha != 1: if alpha != 1:
out *= alpha out *= alpha
if beta != 1: if beta != 0:
out += beta * y if beta != 1:
else: out += beta * y
out += y else:
out += y
out_storage[0][0] = numpy.asarray(out, dtype=y.dtype) out_storage[0][0] = numpy.asarray(out, dtype=y.dtype)
def infer_shape(self, node, input_shapes): def infer_shape(self, node, input_shapes):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论