<commit_before>import math
from numbers import Number

import numpy

from chainer import cuda
from chainer import function
from chainer import utils
from chainer import variable

# ------------------------------------------------------------------------------
# Arithmetic
# ------------------------------------------------------------------------------

def _convert_value_to_string(value):
    if isinstance(value, variable.Variable):
        value = value.data

    if isinstance(value, float):
        return str(value)
    elif isinstance(value, (numpy.ndarray, cuda.GPUArray)):
        return 'constant array'
    else:
        raise ValueError(
            'value must be float, ndarray, GPUArray, or Variable')

def _force_type(dtype, value):
    if isinstance(value, (int, float)):
        return dtype.type(value)
    else:
        return value

class Neg(function.Function):

    @property
    def label(self):
        return '__neg__'

    def forward(self, x):
        return utils.force_array(-x[0]),

    def backward(self, x, gy):
        return utils.force_array(-gy[0]),

def neg(x):  # -x
    return Neg()(x)

class Absolute(function.Function):

    def forward(self, x):
        return utils.force_array(abs(x[0])),

    def backward_cpu(self, x, gy):
        return utils.force_array(numpy.sign(x[0]) * gy[0]),

    def backward_gpu(self, x, gy):
        gx0 = cuda.empty_like(x[0])
        cuda.elementwise(
            'float* gx0, const float* x0, const float* gy',
            'gx0[i] = ((x0[i] > 0) - (x0[i] < 0)) * gy[i]',
            'abs_bwd')(gx0, x[0], gy[0])
        return gx0,

def absolute(x):
    return Absolute()(x)

class Add(function.Function):

    @property
    def label(self):
        return '_ + _'

    def forward(self, x):
        y = utils.force_array(x[0] + x[1])
        return y,

    def backward(self, x, gy):
        return gy[0], gy[0]

class AddConstant(function.Function):

    def __init__(self, value):
        self.value = value

    @property
    def label(self):
        return '_ + %s' % _convert_value_to_string(self.value)

    def forward(self, x):
        return utils.force_array(x[0] + _force_type(x[0].dtype, self.value)),

    def backward(self, x, gy):
        return gy[0],

def add(lhs, rhs):  # lhs + rhs
    if isinstance(rhs, variable.Variable):
        return Add()(lhs, rhs)
    return AddConstant(rhs)(lhs)

class Sub(function.Function):

    @property
    def label(self):
        return '_ - _'

    def forward(self, x):
        return utils.force_array(x[0] - x[1]),

    def backward(self, x, gy):
        return gy[0], utils.force_array(-gy[0])

def sub(lhs, rhs):  # lhs - rhs
    if isinstance(rhs, variable.Variable):
        return Sub()(lhs, rhs)
    return AddConstant(-rhs)(lhs)

class SubFromConstant(function.Function):

    def __init__(self, value):
        self.value = value

    @property
    def label(self):
        return '%s - _' % _convert_value_to_string(self.value)

    def forward(self, x):
        return utils.force_array(_force_type(x[0].dtype, self.value) - x[0]),

    def backward(self, x, gy):
        return utils.force_array(-gy[0]),

def rsub(lhs, rhs):  # rhs - lhs
    if isinstance(rhs, variable.Variable):
        return Sub()(rhs, lhs)
    return SubFromConstant(rhs)(lhs)

class Mul(function.Function):

    @property
    def label(self):
        return '_ * _'

    def forward(self, x):
        return utils.force_array(x[0] * x[1]),

    def backward_cpu(self, x, gy):
        return utils.force_array(gy[0] * x[1]), utils.force_array(gy[0] * x[0])

    def backward_gpu(self, x, gy):
        gx0 = cuda.empty_like(x[0])
        gx1 = cuda.empty_like(x[1])
        cuda.elementwise(
            '''
               float* gx0, float* gx1, const float* x0, const float* x1,
               const float* gy
            ''', '''
               gx0[i] = gy[i] * x1[i];
               gx1[i] = gy[i] * x0[i];
            ''', 'mul_bwd')(gx0, gx1, x[0], x[1], gy[0])
        return gx0, gx1

class MulConstant(function.Function):

    def __init__(self, value):
        self.value = value

    @property
    def label(self):
        return '_ * %s' % _convert_value_to_string(self.value)

    def forward(self, x):
        return utils.force_array(_force_type(x[0].dtype, self.value) * x[0]),

    def backward(self, x, gy):
        return utils.force_array(_force_type(gy[0].dtype, self.value) * gy[0]),

def mul(lhs, rhs):  # lhs * rhs
    if isinstance(rhs, variable.Variable):
        return Mul()(lhs, rhs)
    return MulConstant(rhs)(lhs)

class Div(function.Function):

    @property
    def label(self):
        return '_ / _'

    def forward(self, x):
        return utils.force_array(x[0] / x[1]),

    def backward_cpu(self, x, gy):
        gx0 = utils.force_array(gy[0] / x[1])
        return gx0, utils.force_array(-gx0 * x[0] / x[1])

    def backward_gpu(self, x, gy):
        gx0 = cuda.empty_like(x[0])
        gx1 = cuda.empty_like(x[1])
        cuda.elementwise(
            '''
               float* gx0, float* gx1, const float* x0, const float* x1,
               const float* gy
            ''', '''
               gx0[i] = gy[i] / x1[i];
               gx1[i] = -gx0[i] * x0[i] / x1[i];
            ''', 'div_bwd')(gx0, gx1, x[0], x[1], gy[0])
        return gx0, gx1

def div(lhs, rhs):  # lhs / rhs
    if isinstance(rhs, variable.Variable):
        return Div()(lhs, rhs)
    return MulConstant(1. / rhs)(lhs)

class DivFromConstant(function.Function):

    def __init__(self, value):
        self.value = value

    @property
    def label(self):
        return '_ / %s' % _convert_value_to_string(self.value)

    def forward(self, x):
        return utils.force_array(_force_type(x[0].dtype, self.value) / x[0]),

    def backward_cpu(self, x, gy):
        value = _force_type(gy[0].dtype, self.value)
        return utils.force_array(-value * gy[0] / (x[0] ** 2)),

    def backward_gpu(self, x, gy):
        gx = cuda.empty_like(x[0])
        if isinstance(self.value, Number):
            cuda.elementwise(
                '''
                   float* gx, const float* x, const float* gy,
                   const float value
                ''',
                'gx[i] = -value * gy[i] / (x[i] * x[i])',
                'div_from_const_bwd')(gx, x[0], gy[0], self.value)
        else:
            cuda.elementwise(
                '''
                   float* gx, const float* x, const float* gy,
                   const float* value
                ''',
                'gx[i] = -value[i] * gy[i] / (x[i] * x[i])',
                'div_from_const_bwd')(gx, x[0], gy[0], self.value)
        return gx,

def rdiv(lhs, rhs):  # rhs / lhs
    if isinstance(rhs, variable.Variable):
        return Div()(rhs, lhs)
    return DivFromConstant(rhs)(lhs)

class PowVarVar(function.Function):

    @property
    def label(self):
        return '_ ** _'

    def forward_cpu(self, x):
        self.y = utils.force_array(x[0] ** x[1])
        return self.y,

    def forward_gpu(self, x):
        return x[0] ** x[1],

    def backward_cpu(self, x, gy):
        gx0 = utils.force_array(x[1] * (x[0] ** (x[1] - 1)) * gy[0])
        gx1 = utils.force_array(numpy.log(x[0]) * self.y * gy[0])
        return gx0, gx1

    def backward_gpu(self, x, gy):
        gx0 = cuda.empty_like(x[0])
        gx1 = cuda.empty_like(x[1])
        cuda.elementwise(
            '''
               float* gx0, float* gx1, const float* x0, const float* x1,
               const float* gy
            ''', '''
               gx0[i] = x1[i] * __powf(x0[i], x1[i] - 1) * gy[i];
               gx1[i] = __logf(x0[i]) * __powf(x0[i], x1[i]) * gy[i];
            ''', 'pow_var_var_bwd')(gx0, gx1, x[0], x[1], gy[0])
        return gx0, gx1

class PowVarConst(function.Function):

    def __init__(self, value):
        self.value = value

    @property
    def label(self):
        return '_ ** %s' % _convert_value_to_string(self.value)

    def forward(self, x):
        return utils.force_array(x[0] ** _force_type(x[0].dtype, self.value)),

    def backward_cpu(self, x, gy):
        val_1 = _force_type(x[0].dtype, self.value - 1)
        gx = _force_type(x[0].dtype, self.value) * (x[0] ** val_1) * gy[0]
        return utils.force_array(gx),

    def backward_gpu(self, x, gy):
        gx = cuda.empty_like(x[0])
        if isinstance(self.value, Number):
            cuda.elementwise(
                '''
                   float* gx, const float* x, const float* gy,
                   const float value
                ''',
                'gx[i] = value * __powf(x[i], value - 1) * gy[i]',
                'pow_var_const_bwd')(gx, x[0], gy[0], self.value)
        else:
            cuda.elementwise(
                '''
                   float* gx, const float* x, const float* gy,
                   const float* value
                ''',
                'gx[i] = value[i] * __powf(x[i], value[i] - 1) * gy[i]',
                'pow_var_const_bwd')(gx, x[0], gy[0], self.value)
        return gx,

def pow(lhs, rhs):  # lhs ** rhs
    if isinstance(rhs, variable.Variable):
        return PowVarVar()(lhs, rhs)
    return PowVarConst(rhs)(lhs)

class PowConstVar(function.Function):

    def __init__(self, value):
        self.value = value

    @property
    def label(self):
        return '%s ** _' % _convert_value_to_string(self.value)

    def forward_cpu(self, x):
        self.y = utils.force_array(_force_type(x[0].dtype, self.value) ** x[0])
        return self.y,

    def forward_gpu(self, x):
        y = cuda.empty_like(x[0])
        if isinstance(self.value, Number):
            cuda.elementwise('float* y, const float* x, const float value',
                             'y[i] = __powf(value, x[i])',
                             'pow_const_var_fwd')(y, x[0], self.value)
        else:
            cuda.elementwise('float* y, const float* x, const float *value',
                             'y[i] = __powf(value[i], x[i])',
                             'pow_const_var_fwd')(y, x[0], self.value)
        return y,

    def backward_cpu(self, x, gy):
        value = _force_type(gy[0].dtype, self.value)
        return utils.force_array(numpy.log(value) * self.y * gy[0]),

    def backward_gpu(self, x, gy):
        gx = cuda.empty_like(x[0])
        if isinstance(self.value, Number):
            logv = math.log(self.value)
            cuda.elementwise(
                '''
                   float* gx, const float* x, const float* gy,
                   const float value, const float logv
                ''',
                'gx[i] = logv * __powf(value, x[i]) * gy[i]',
                'pow_const_var_bwd')(gx, x[0], gy[0], self.value, logv)
        else:
            cuda.elementwise(
                '''
                   float* gx, const float* x, const float* gy,
                   const float* value
                ''',
                'gx[i] = __logf(value[i]) * __powf(value[i], x[i]) * gy[i]',
                'pow_const_var_bwd')(gx, x[0], gy[0], self.value)
        return gx,

def rpow(lhs, rhs):  # rhs ** lhs
    if isinstance(rhs, variable.Variable):
        return PowVarVar()(rhs, lhs)
    return PowConstVar(rhs)(lhs)

def install_variable_arithmetics():
    variable.Variable.__neg__ = neg
    variable.Variable.__abs__ = absolute
    variable.Variable.__add__ = add
    variable.Variable.__radd__ = add
    variable.Variable.__sub__ = sub
    variable.Variable.__rsub__ = rsub
    variable.Variable.__mul__ = mul
    variable.Variable.__rmul__ = mul
    variable.Variable.__div__ = div
    variable.Variable.__truediv__ = div
    variable.Variable.__rdiv__ = rdiv
    variable.Variable.__rtruediv__ = rdiv
    variable.Variable.__pow__ = pow
    variable.Variable.__rpow__ = rpow

# ------------------------------------------------------------------------------
# Special functions
# ------------------------------------------------------------------------------

class Exp(function.Function):

    @property
    def label(self):
        return 'exp'

    def forward_cpu(self, x):
        self.y = utils.force_array(numpy.exp(x[0]))
        return self.y,

    def forward_gpu(self, x):
        self.y = cuda.cumath.exp(x[0])
        return self.y,

    def backward(self, x, gy):
        return utils.force_array(self.y * gy[0]),

def exp(x):
    """Elementwise exponential function."""
    return Exp()(x)

class Log(function.Function):

    @property
    def label(self):
        return 'log'

    def forward_cpu(self, x):
        return utils.force_array(numpy.log(x[0])),

    def forward_gpu(self, x):
        return cuda.cumath.log(x[0]),

    def backward(self, x, gy):
        return utils.force_array(gy[0] / x[0]),

def log(x):
    """Elementwise natural logarithm function."""
    return Log()(x)
<commit_msg>Use isscalar instead of isinstance as int is not a super class of int64<commit_after>import math
from numbers import Number

import numpy

from chainer import cuda
from chainer import function
from chainer import utils
from chainer import variable

# ------------------------------------------------------------------------------
# Arithmetic
# ------------------------------------------------------------------------------

def _convert_value_to_string(value):
    if isinstance(value, variable.Variable):
        value = value.data

    if isinstance(value, float):
        return str(value)
    elif isinstance(value, (numpy.ndarray, cuda.GPUArray)):
        return 'constant array'
    else:
        raise ValueError(
            'value must be float, ndarray, GPUArray, or Variable')

def _force_type(dtype, value):
    if numpy.isscalar(value):
        return dtype.type(value)
    else:
        return value

class Neg(function.Function):

    @property
    def label(self):
        return '__neg__'

    def forward(self, x):
        return utils.force_array(-x[0]),

    def backward(self, x, gy):
        return utils.force_array(-gy[0]),

def neg(x):  # -x
    return Neg()(x)

class Absolute(function.Function):

    def forward(self, x):
        return utils.force_array(abs(x[0])),

    def backward_cpu(self, x, gy):
        return utils.force_array(numpy.sign(x[0]) * gy[0]),

    def backward_gpu(self, x, gy):
        gx0 = cuda.empty_like(x[0])
        cuda.elementwise(
            'float* gx0, const float* x0, const float* gy',
            'gx0[i] = ((x0[i] > 0) - (x0[i] < 0)) * gy[i]',
            'abs_bwd')(gx0, x[0], gy[0])
        return gx0,

def absolute(x):
    return Absolute()(x)

class Add(function.Function):

    @property
    def label(self):
        return '_ + _'

    def forward(self, x):
        y = utils.force_array(x[0] + x[1])
        return y,

    def backward(self, x, gy):
        return gy[0], gy[0]

class AddConstant(function.Function):

    def __init__(self, value):
        self.value = value

    @property
    def label(self):
        return '_ + %s' % _convert_value_to_string(self.value)

    def forward(self, x):
        return utils.force_array(x[0] + _force_type(x[0].dtype, self.value)),

    def backward(self, x, gy):
        return gy[0],

def add(lhs, rhs):  # lhs + rhs
    if isinstance(rhs, variable.Variable):
        return Add()(lhs, rhs)
    return AddConstant(rhs)(lhs)

class Sub(function.Function):

    @property
    def label(self):
        return '_ - _'

    def forward(self, x):
        return utils.force_array(x[0] - x[1]),

    def backward(self, x, gy):
        return gy[0], utils.force_array(-gy[0])

def sub(lhs, rhs):  # lhs - rhs
    if isinstance(rhs, variable.Variable):
        return Sub()(lhs, rhs)
    return AddConstant(-rhs)(lhs)

class SubFromConstant(function.Function):

    def __init__(self, value):
        self.value = value

    @property
    def label(self):
        return '%s - _' % _convert_value_to_string(self.value)

    def forward(self, x):
        return utils.force_array(_force_type(x[0].dtype, self.value) - x[0]),

    def backward(self, x, gy):
        return utils.force_array(-gy[0]),

def rsub(lhs, rhs):  # rhs - lhs
    if isinstance(rhs, variable.Variable):
        return Sub()(rhs, lhs)
    return SubFromConstant(rhs)(lhs)

class Mul(function.Function):

    @property
    def label(self):
        return '_ * _'

    def forward(self, x):
        return utils.force_array(x[0] * x[1]),

    def backward_cpu(self, x, gy):
        return utils.force_array(gy[0] * x[1]), utils.force_array(gy[0] * x[0])

    def backward_gpu(self, x, gy):
        gx0 = cuda.empty_like(x[0])
        gx1 = cuda.empty_like(x[1])
        cuda.elementwise(
            '''
               float* gx0, float* gx1, const float* x0, const float* x1,
               const float* gy
            ''', '''
               gx0[i] = gy[i] * x1[i];
               gx1[i] = gy[i] * x0[i];
            ''', 'mul_bwd')(gx0, gx1, x[0], x[1], gy[0])
        return gx0, gx1

class MulConstant(function.Function):

    def __init__(self, value):
        self.value = value

    @property
    def label(self):
        return '_ * %s' % _convert_value_to_string(self.value)

    def forward(self, x):
        return utils.force_array(_force_type(x[0].dtype, self.value) * x[0]),

    def backward(self, x, gy):
        return utils.force_array(_force_type(gy[0].dtype, self.value) * gy[0]),

def mul(lhs, rhs):  # lhs * rhs
    if isinstance(rhs, variable.Variable):
        return Mul()(lhs, rhs)
    return MulConstant(rhs)(lhs)

class Div(function.Function):

    @property
    def label(self):
        return '_ / _'

    def forward(self, x):
        return utils.force_array(x[0] / x[1]),

    def backward_cpu(self, x, gy):
        gx0 = utils.force_array(gy[0] / x[1])
        return gx0, utils.force_array(-gx0 * x[0] / x[1])

    def backward_gpu(self, x, gy):
        gx0 = cuda.empty_like(x[0])
        gx1 = cuda.empty_like(x[1])
        cuda.elementwise(
            '''
               float* gx0, float* gx1, const float* x0, const float* x1,
               const float* gy
            ''', '''
               gx0[i] = gy[i] / x1[i];
               gx1[i] = -gx0[i] * x0[i] / x1[i];
            ''', 'div_bwd')(gx0, gx1, x[0], x[1], gy[0])
        return gx0, gx1

def div(lhs, rhs):  # lhs / rhs
    if isinstance(rhs, variable.Variable):
        return Div()(lhs, rhs)
    return MulConstant(1. / rhs)(lhs)

class DivFromConstant(function.Function):

    def __init__(self, value):
        self.value = value

    @property
    def label(self):
        return '_ / %s' % _convert_value_to_string(self.value)

    def forward(self, x):
        return utils.force_array(_force_type(x[0].dtype, self.value) / x[0]),

    def backward_cpu(self, x, gy):
        value = _force_type(gy[0].dtype, self.value)
        return utils.force_array(-value * gy[0] / (x[0] ** 2)),

    def backward_gpu(self, x, gy):
        gx = cuda.empty_like(x[0])
        if isinstance(self.value, Number):
            cuda.elementwise(
                '''
                   float* gx, const float* x, const float* gy,
                   const float value
                ''',
                'gx[i] = -value * gy[i] / (x[i] * x[i])',
                'div_from_const_bwd')(gx, x[0], gy[0], self.value)
        else:
            cuda.elementwise(
                '''
                   float* gx, const float* x, const float* gy,
                   const float* value
                ''',
                'gx[i] = -value[i] * gy[i] / (x[i] * x[i])',
                'div_from_const_bwd')(gx, x[0], gy[0], self.value)
        return gx,

def rdiv(lhs, rhs):  # rhs / lhs
    if isinstance(rhs, variable.Variable):
        return Div()(rhs, lhs)
    return DivFromConstant(rhs)(lhs)

class PowVarVar(function.Function):

    @property
    def label(self):
        return '_ ** _'

    def forward_cpu(self, x):
        self.y = utils.force_array(x[0] ** x[1])
        return self.y,

    def forward_gpu(self, x):
        return x[0] ** x[1],

    def backward_cpu(self, x, gy):
        gx0 = utils.force_array(x[1] * (x[0] ** (x[1] - 1)) * gy[0])
        gx1 = utils.force_array(numpy.log(x[0]) * self.y * gy[0])
        return gx0, gx1

    def backward_gpu(self, x, gy):
        gx0 = cuda.empty_like(x[0])
        gx1 = cuda.empty_like(x[1])
        cuda.elementwise(
            '''
               float* gx0, float* gx1, const float* x0, const float* x1,
               const float* gy
            ''', '''
               gx0[i] = x1[i] * __powf(x0[i], x1[i] - 1) * gy[i];
               gx1[i] = __logf(x0[i]) * __powf(x0[i], x1[i]) * gy[i];
            ''', 'pow_var_var_bwd')(gx0, gx1, x[0], x[1], gy[0])
        return gx0, gx1

class PowVarConst(function.Function):

    def __init__(self, value):
        self.value = value

    @property
    def label(self):
        return '_ ** %s' % _convert_value_to_string(self.value)

    def forward(self, x):
        return utils.force_array(x[0] ** _force_type(x[0].dtype, self.value)),

    def backward_cpu(self, x, gy):
        val_1 = _force_type(x[0].dtype, self.value - 1)
        gx = _force_type(x[0].dtype, self.value) * (x[0] ** val_1) * gy[0]
        return utils.force_array(gx),

    def backward_gpu(self, x, gy):
        gx = cuda.empty_like(x[0])
        if isinstance(self.value, Number):
            cuda.elementwise(
                '''
                   float* gx, const float* x, const float* gy,
                   const float value
                ''',
                'gx[i] = value * __powf(x[i], value - 1) * gy[i]',
                'pow_var_const_bwd')(gx, x[0], gy[0], self.value)
        else:
            cuda.elementwise(
                '''
                   float* gx, const float* x, const float* gy,
                   const float* value
                ''',
                'gx[i] = value[i] * __powf(x[i], value[i] - 1) * gy[i]',
                'pow_var_const_bwd')(gx, x[0], gy[0], self.value)
        return gx,

def pow(lhs, rhs):  # lhs ** rhs
    if isinstance(rhs, variable.Variable):
        return PowVarVar()(lhs, rhs)
    return PowVarConst(rhs)(lhs)

class PowConstVar(function.Function):

    def __init__(self, value):
        self.value = value

    @property
    def label(self):
        return '%s ** _' % _convert_value_to_string(self.value)

    def forward_cpu(self, x):
        self.y = utils.force_array(_force_type(x[0].dtype, self.value) ** x[0])
        return self.y,

    def forward_gpu(self, x):
        y = cuda.empty_like(x[0])
        if isinstance(self.value, Number):
            cuda.elementwise('float* y, const float* x, const float value',
                             'y[i] = __powf(value, x[i])',
                             'pow_const_var_fwd')(y, x[0], self.value)
        else:
            cuda.elementwise('float* y, const float* x, const float *value',
                             'y[i] = __powf(value[i], x[i])',
                             'pow_const_var_fwd')(y, x[0], self.value)
        return y,

    def backward_cpu(self, x, gy):
        value = _force_type(gy[0].dtype, self.value)
        return utils.force_array(numpy.log(value) * self.y * gy[0]),

    def backward_gpu(self, x, gy):
        gx = cuda.empty_like(x[0])
        if isinstance(self.value, Number):
            logv = math.log(self.value)
            cuda.elementwise(
                '''
                   float* gx, const float* x, const float* gy,
                   const float value, const float logv
                ''',
                'gx[i] = logv * __powf(value, x[i]) * gy[i]',
                'pow_const_var_bwd')(gx, x[0], gy[0], self.value, logv)
        else:
            cuda.elementwise(
                '''
                   float* gx, const float* x, const float* gy,
                   const float* value
                ''',
                'gx[i] = __logf(value[i]) * __powf(value[i], x[i]) * gy[i]',
                'pow_const_var_bwd')(gx, x[0], gy[0], self.value)
        return gx,

def rpow(lhs, rhs):  # rhs ** lhs
    if isinstance(rhs, variable.Variable):
        return PowVarVar()(rhs, lhs)
    return PowConstVar(rhs)(lhs)

def install_variable_arithmetics():
    variable.Variable.__neg__ = neg
    variable.Variable.__abs__ = absolute
    variable.Variable.__add__ = add
    variable.Variable.__radd__ = add
    variable.Variable.__sub__ = sub
    variable.Variable.__rsub__ = rsub
    variable.Variable.__mul__ = mul
    variable.Variable.__rmul__ = mul
    variable.Variable.__div__ = div
    variable.Variable.__truediv__ = div
    variable.Variable.__rdiv__ = rdiv
    variable.Variable.__rtruediv__ = rdiv
    variable.Variable.__pow__ = pow
    variable.Variable.__rpow__ = rpow

# ------------------------------------------------------------------------------
# Special functions
# ------------------------------------------------------------------------------

class Exp(function.Function):

    @property
    def label(self):
        return 'exp'

    def forward_cpu(self, x):
        self.y = utils.force_array(numpy.exp(x[0]))
        return self.y,

    def forward_gpu(self, x):
        self.y = cuda.cumath.exp(x[0])
        return self.y,

    def backward(self, x, gy):
        return utils.force_array(self.y * gy[0]),

def exp(x):
    """Elementwise exponential function."""
    return Exp()(x)

class Log(function.Function):

    @property
    def label(self):
        return 'log'

    def forward_cpu(self, x):
        return utils.force_array(numpy.log(x[0])),

    def forward_gpu(self, x):
        return cuda.cumath.log(x[0]),

    def backward(self, x, gy):
        return utils.force_array(gy[0] / x[0]),

def log(x):
    """Elementwise natural logarithm function."""
    return Log()(x)
<|endoftext|>


<commit_before>"""
Module to parse xmind file into test suite and test case objects.
"""
import logging
import re
from xml.etree import ElementTree as  ET
from xml.etree.ElementTree import Element
from zipfile import ZipFile

from .datatype import *

content_xml = "content.xml"
comments_xml = "comments.xml"

def parse_xmind_file(file_path):
    """Extract xmind as zip file then read the content.xml"""
    with ZipFile(file_path) as xmind:
        for f in xmind.namelist():
            if f == content_xml:
                cache[content_xml] = xmind.open(f).read().decode('utf-8')

            if f == comments_xml:
                cache[comments_xml] = xmind.open(f).read().decode('utf-8')

    return parse_xmind_content()

def parse_xmind_content():
    """Main function to read the content xml and return test suite data."""
    xml_root = xmind_content_to_etree(cache[content_xml])
    assert isinstance(xml_root, Element)

    try:
        xml_root_suite = xml_root.find('sheet').find('topic')
        logging.info("Parse topic: {}".format(title_of(xml_root_suite)))
    except:
        logging.error('Cannot find any topic in your xmind!')
        raise

    root_suite = TestSuite()
    root_suite.sub_suites = []
    suite_nodes = children_topics_of(xml_root_suite)

    if suite_nodes is None:
        raise ValueError("Cannot find any test suite in xmind!")

    for node in suite_nodes:
        suite = parse_suite(node)
        root_suite.sub_suites.append(suite)

    return root_suite

def xmind_content_to_etree(content):
    # Remove the default namespace definition (xmlns="http://some/namespace")
    xml_content = re.sub(r'\sxmlns="[^"]+"', '', content, count=1)
    return ET.fromstring(xml_content.encode('utf-8'))

def xmind_xml_to_etree(xml_path):
    with open(xml_path) as f:
        content = f.read()
        return xmind_content_to_etree(content)

def comments_of(node):
    if cache.get(comments_xml, None):
        xml_root = xmind_content_to_etree(cache[comments_xml])
        node_id = node.attrib['id']
        comment = xml_root.find('./comment[@object-id="{}"]'.format(node_id))

        if comment is not None:
            return comment.find('content').text

def title_of(node):
    title = node.find('title')

    if title is not None:
        return title.text

def note_of(topic_node):
    note_node = topic_node.find('notes')

    if note_node is not None:
        note = note_node.find('plain').text
        return note.strip()

def maker_of(topic_node, maker_prefix):
    maker_node = topic_node.find('marker-refs')
    if maker_node is not None:
        for maker in maker_node:
            maker_id = maker.attrib['marker-id']
            if maker_id.startswith(maker_prefix):
                return maker_id

def children_topics_of(topic_node):
    children = topic_node.find('children')

    if children is not None:
        return children.find('./topics[@type="attached"]')

def parse_step(step_node):
    step = TestStep()
    step.action = title_of(step_node)
    expected_node = children_topics_of(step_node)

    if expected_node is not None:
        step.expected = title_of(expected_node[0])

    return step

def parse_steps(steps_node):
    steps = []

    for step_number, step_node in enumerate(steps_node, 1):
        step = parse_step(step_node)
        step.number = step_number
        steps.append(step)

    return steps

def parse_testcase(testcase_node):
    testcase = TestCase()
    testcase.name = title_of(testcase_node)
    testcase.summary = note_of(testcase_node)
    testcase.importance = maker_of(testcase_node, 'priority')
    testcase.preconditions = comments_of(testcase_node)
    steps_node = children_topics_of(testcase_node)

    if steps_node is not None:
        testcase.steps = parse_steps(steps_node)

    return testcase

def parse_suite(suite_node):
    suite = TestSuite()
    suite.name = title_of(suite_node)
    suite.details = note_of(suite_node)
    suite.testcase_list = []
    testcase_nodes = children_topics_of(suite_node)

    if testcase_nodes is not None:
        for node in testcase_nodes:
            testcase = parse_testcase(node)
            suite.testcase_list.append(testcase)

    return suite
<commit_msg>improve code quality.<commit_after>"""
Module to parse xmind file into test suite and test case objects.
"""
import logging
import re
from xml.etree import ElementTree as  ET
from xml.etree.ElementTree import Element
from zipfile import ZipFile

from .datatype import *

content_xml = "content.xml"
comments_xml = "comments.xml"

def parse_xmind_file(file_path):
    """Extract xmind as zip file then read the content.xml"""
    with ZipFile(file_path) as xmind:
        for f in xmind.namelist():
            for key in [content_xml, comments_xml]:
                if f == key:
                    cache[key] = xmind.open(f).read().decode('utf-8')

    return parse_xmind_content()

def parse_xmind_content():
    """Main function to read the content xml and return test suite data."""
    xml_root = xmind_content_to_etree(cache[content_xml])
    assert isinstance(xml_root, Element)

    try:
        xml_root_suite = xml_root.find('sheet').find('topic')
        logging.info("Parse topic: {}".format(title_of(xml_root_suite)))
    except:
        logging.error('Cannot find any topic in your xmind!')
        raise

    root_suite = TestSuite()
    root_suite.sub_suites = []
    suite_nodes = children_topics_of(xml_root_suite)

    if suite_nodes is None:
        raise ValueError("Cannot find any test suite in xmind!")

    for node in suite_nodes:
        suite = parse_suite(node)
        root_suite.sub_suites.append(suite)

    return root_suite

def xmind_content_to_etree(content):
    # Remove the default namespace definition (xmlns="http://some/namespace")
    xml_content = re.sub(r'\sxmlns="[^"]+"', '', content, count=1)
    return ET.fromstring(xml_content.encode('utf-8'))

def xmind_xml_to_etree(xml_path):
    with open(xml_path) as f:
        content = f.read()
        return xmind_content_to_etree(content)

def comments_of(node):
    if cache.get(comments_xml, None):
        xml_root = xmind_content_to_etree(cache[comments_xml])
        node_id = node.attrib['id']
        comment = xml_root.find('./comment[@object-id="{}"]'.format(node_id))

        if comment is not None:
            return comment.find('content').text

def title_of(node):
    title = node.find('title')

    if title is not None:
        return title.text

def note_of(topic_node):
    note_node = topic_node.find('notes')

    if note_node is not None:
        note = note_node.find('plain').text
        return note.strip()

def maker_of(topic_node, maker_prefix):
    maker_node = topic_node.find('marker-refs')
    if maker_node is not None:
        for maker in maker_node:
            maker_id = maker.attrib['marker-id']
            if maker_id.startswith(maker_prefix):
                return maker_id

def children_topics_of(topic_node):
    children = topic_node.find('children')

    if children is not None:
        return children.find('./topics[@type="attached"]')

def parse_step(step_node):
    step = TestStep()
    step.action = title_of(step_node)
    expected_node = children_topics_of(step_node)

    if expected_node is not None:
        step.expected = title_of(expected_node[0])

    return step

def parse_steps(steps_node):
    steps = []

    for step_number, step_node in enumerate(steps_node, 1):
        step = parse_step(step_node)
        step.number = step_number
        steps.append(step)

    return steps

def parse_testcase(testcase_node):
    testcase = TestCase()
    testcase.name = title_of(testcase_node)
    testcase.summary = note_of(testcase_node)
    testcase.importance = maker_of(testcase_node, 'priority')
    testcase.preconditions = comments_of(testcase_node)
    steps_node = children_topics_of(testcase_node)

    if steps_node is not None:
        testcase.steps = parse_steps(steps_node)

    return testcase

def parse_suite(suite_node):
    suite = TestSuite()
    suite.name = title_of(suite_node)
    suite.details = note_of(suite_node)
    suite.testcase_list = []
    testcase_nodes = children_topics_of(suite_node)

    if testcase_nodes is not None:
        for node in testcase_nodes:
            testcase = parse_testcase(node)
            suite.testcase_list.append(testcase)

    return suite
<|endoftext|>


<commit_before>#!/usr/bin/env python

from pysblgnt import morphgnt_rows

def pos_parse(row):
    pos = {
        "RA": "article",
        "A-": "adjective",
        "N-": "noun",
        "C-": "conjunction",
        "RP": "personal pronoun",
        "RR": "relative pronoun",
        "V-": "verb",
        "P-": "preposition",
        "D-": "adverb",
        "RD": "demonstrative",
        "RI": "interogative/indefinite pronoun",
        "X-": "particle",
        "I-": "interjection",
    }[row["ccat-pos"]]
    person, tense, voice, mood, case, number, gender, degree = row["ccat-parse"]
    parse = []
    if pos == "verb":
        if mood == "P":
            parse.append({
                "P": "present",
                "A": "aorist",
                "X": "perfect",
                "F": "future",
            }[tense])
            parse.append({
                "A": "active",
                "M": "middle",
                "P": "passive",
            }[voice])
            parse.append("participle")
            parse.append({
                "N": "nominative",
                "A": "accusative",
                "G": "genitive",
                "D": "dative",
                "V": "vocative",
                "-": "",
            }[case])
            parse.append({
                "S": "singular",
                "P": "plural",
                "-": "",
            }[number])
            parse.append({
                "M": "masculine",
                "F": "feminine",
                "N": "neuter",
                "-": "",
            }[gender])
        elif mood == "N":
            parse.append({
                "P": "present",
                "A": "aorist",
                "X": "perfect",
                "F": "future",
            }[tense])
            parse.append({
                "A": "active",
                "M": "middle",
                "P": "passive",
            }[voice])
            parse.append("infinitive")
        else:
            parse.append({
                "P": "present",
                "F": "future",
                "A": "aorist",
                "X": "perfect",
                "Y": "pluperfect",
                "I": "imperfect",
            }[tense])
            parse.append({
                "A": "active",
                "M": "middle",
                "P": "passive",
            }[voice])
            parse.append({
                "I": "indicative",
                "S": "subjunctive",
                "D": "imperative",
                "O": "optative",
            }[mood])
            parse.append({
                "1": "1st person",
                "2": "2nd person",
                "3": "3rd person",
            }[person])
            parse.append({
                "S": "singular",
                "P": "plural",
                "-": "",
            }[number])
    else:
        parse.append({
            "N": "nominative",
            "A": "accusative",
            "G": "genitive",
            "D": "dative",
            "V": "vocative",
            "-": "",
        }[case])
        parse.append({
            "S": "singular",
            "P": "plural",
            "-": "",
        }[number])
        parse.append({
            "M": "masculine",
            "F": "feminine",
            "N": "neuter",
            "-": "",
        }[gender])
        parse.append({
            "C": "comparative",
            "S": "superlative",
            "-": "",
        }[degree])
    parse = " ".join(parse).strip()

    return pos, parse

def generate(book_title, book_num, output_filename, chapter_count):

    with open(output_filename, "w") as output:

        print(f"""
        <!doctype html>

        <title>{book_title}</title>
        <meta charset="utf-8">
        <link rel="stylesheet" href="sblgnt.css">

MorphGNT SBLGNT

{book_title}

        """, file=output)

        for chapter in range(1, chapter_count + 1):
            print(f"""
{chapter}
""", file=output)

        print(f"""

{book_title}

        """, file=output)

        last_verse = 0
        last_chapter = 0

        for row in morphgnt_rows(book_num):
            pos, parse = pos_parse(row)
            chapter = int(row["bcv"][2:4])
            verse = int(row["bcv"][4:])
            cv = f"{chapter}-{verse}"
            if chapter != last_chapter:
                if last_chapter:
                    print("""""", file=output)
                    print("""
""", file=output)
                print(f"""
""", file=output)
                last_chapter = chapter
                last_verse = 0
            if verse != last_verse:
                if last_verse:
                    print("""""", file=output)
                print(f"""{verse}""", file=output)
                last_verse = verse
                print(f"""""", file=output)
            text, form, lemma = row["text"], row["norm"], row["lemma"]
            print(f"""{text}""", file=output)

        print("""

        """, file=output)

    assert chapter == chapter_count, chapter

generate("Matthew", 1, "matthew.html", 28)
generate("Mark", 2, "mark.html", 16)
generate("Luke", 3, "luke.html", 24)
generate("John", 4, "john.html", 21)
generate("Acts", 5, "acts.html", 28)
generate("Romans", 6, "romans.html", 16)
generate("1 Corinthians", 7, "1corinthians.html", 16)
generate("2 Corinthians", 8, "2corinthians.html", 13)
generate("Galatians", 9, "galatians.html", 6)
generate("Ephesians", 10, "ephesians.html", 6)
generate("Philippians", 11, "philippians.html", 4)
generate("Colossians", 12, "colossians.html", 4)
generate("1 Thessalonians", 13, "1thessalonians.html", 5)
generate("2 Thessalonians", 14, "2thessalonians.html", 3)
generate("1 Timothy", 15, "1timothy.html", 6)
generate("2 Timothy", 16, "2timothy.html", 4)
generate("Titus", 17, "titus.html", 3)
generate("Philemon", 18, "philemon.html", 1)
generate("Hebrews", 19, "hebrews.html", 13)
generate("James", 20, "james.html", 5)
generate("1 Peter", 21, "1peter.html", 5)
generate("2 Peter", 22, "2peter.html", 3)
generate("1 John", 23, "1john.html", 5)
generate("2 John", 24, "2john.html", 1)
generate("3 John", 25, "3john.html", 1)
generate("Jude", 26, "jude.html", 1)
generate("Revelation", 27, "revelation.html", 22)
<commit_msg>write output to output/ directory<commit_after>#!/usr/bin/env python

import os
import shutil

from pysblgnt import morphgnt_rows

def pos_parse(row):
    pos = {
        "RA": "article",
        "A-": "adjective",
        "N-": "noun",
        "C-": "conjunction",
        "RP": "personal pronoun",
        "RR": "relative pronoun",
        "V-": "verb",
        "P-": "preposition",
        "D-": "adverb",
        "RD": "demonstrative",
        "RI": "interogative/indefinite pronoun",
        "X-": "particle",
        "I-": "interjection",
    }[row["ccat-pos"]]
    person, tense, voice, mood, case, number, gender, degree = row["ccat-parse"]
    parse = []
    if pos == "verb":
        if mood == "P":
            parse.append({
                "P": "present",
                "A": "aorist",
                "X": "perfect",
                "F": "future",
            }[tense])
            parse.append({
                "A": "active",
                "M": "middle",
                "P": "passive",
            }[voice])
            parse.append("participle")
            parse.append({
                "N": "nominative",
                "A": "accusative",
                "G": "genitive",
                "D": "dative",
                "V": "vocative",
                "-": "",
            }[case])
            parse.append({
                "S": "singular",
                "P": "plural",
                "-": "",
            }[number])
            parse.append({
                "M": "masculine",
                "F": "feminine",
                "N": "neuter",
                "-": "",
            }[gender])
        elif mood == "N":
            parse.append({
                "P": "present",
                "A": "aorist",
                "X": "perfect",
                "F": "future",
            }[tense])
            parse.append({
                "A": "active",
                "M": "middle",
                "P": "passive",
            }[voice])
            parse.append("infinitive")
        else:
            parse.append({
                "P": "present",
                "F": "future",
                "A": "aorist",
                "X": "perfect",
                "Y": "pluperfect",
                "I": "imperfect",
            }[tense])
            parse.append({
                "A": "active",
                "M": "middle",
                "P": "passive",
            }[voice])
            parse.append({
                "I": "indicative",
                "S": "subjunctive",
                "D": "imperative",
                "O": "optative",
            }[mood])
            parse.append({
                "1": "1st person",
                "2": "2nd person",
                "3": "3rd person",
            }[person])
            parse.append({
                "S": "singular",
                "P": "plural",
                "-": "",
            }[number])
    else:
        parse.append({
            "N": "nominative",
            "A": "accusative",
            "G": "genitive",
            "D": "dative",
            "V": "vocative",
            "-": "",
        }[case])
        parse.append({
            "S": "singular",
            "P": "plural",
            "-": "",
        }[number])
        parse.append({
            "M": "masculine",
            "F": "feminine",
            "N": "neuter",
            "-": "",
        }[gender])
        parse.append({
            "C": "comparative",
            "S": "superlative",
            "-": "",
        }[degree])
    parse = " ".join(parse).strip()

    return pos, parse

def generate(book_title, book_num, output_filename, chapter_count):

    with open(output_filename, "w") as output:

        print(f"""
        <!doctype html>

        <title>{book_title}</title>
        <meta charset="utf-8">
        <link rel="stylesheet" href="sblgnt.css">

MorphGNT SBLGNT

{book_title}

        """, file=output)

        for chapter in range(1, chapter_count + 1):
            print(f"""
{chapter}
""", file=output)

        print(f"""

{book_title}

        """, file=output)

        last_verse = 0
        last_chapter = 0

        for row in morphgnt_rows(book_num):
            pos, parse = pos_parse(row)
            chapter = int(row["bcv"][2:4])
            verse = int(row["bcv"][4:])
            cv = f"{chapter}-{verse}"
            if chapter != last_chapter:
                if last_chapter:
                    print("""""", file=output)
                    print("""
""", file=output)
                print(f"""
""", file=output)
                last_chapter = chapter
                last_verse = 0
            if verse != last_verse:
                if last_verse:
                    print("""""", file=output)
                print(f"""{verse}""", file=output)
                last_verse = verse
                print(f"""""", file=output)
            text, form, lemma = row["text"], row["norm"], row["lemma"]
            print(f"""{text}""", file=output)

        print("""

        """, file=output)

    assert chapter == chapter_count, chapter
    print(f"wrote {output_filename}")

if not os.path.exists("output"):
    os.makedirs("output")
    print("created directory")

generate("Matthew", 1, "output/matthew.html", 28)
generate("Mark", 2, "output/mark.html", 16)
generate("Luke", 3, "output/luke.html", 24)
generate("John", 4, "output/john.html", 21)
generate("Acts", 5, "output/acts.html", 28)
generate("Romans", 6, "output/romans.html", 16)
generate("1 Corinthians", 7, "output/1corinthians.html", 16)
generate("2 Corinthians", 8, "output/2corinthians.html", 13)
generate("Galatians", 9, "output/galatians.html", 6)
generate("Ephesians", 10, "output/ephesians.html", 6)
generate("Philippians", 11, "output/philippians.html", 4)
generate("Colossians", 12, "output/colossians.html", 4)
generate("1 Thessalonians", 13, "output/1thessalonians.html", 5)
generate("2 Thessalonians", 14, "output/2thessalonians.html", 3)
generate("1 Timothy", 15, "output/1timothy.html", 6)
generate("2 Timothy", 16, "output/2timothy.html", 4)
generate("Titus", 17, "output/titus.html", 3)
generate("Philemon", 18, "output/philemon.html", 1)
generate("Hebrews", 19, "output/hebrews.html", 13)
generate("James", 20, "output/james.html", 5)
generate("1 Peter", 21, "output/1peter.html", 5)
generate("2 Peter", 22, "output/2peter.html", 3)
generate("1 John", 23, "output/1john.html", 5)
generate("2 John", 24, "output/2john.html", 1)
generate("3 John", 25, "output/3john.html", 1)
generate("Jude", 26, "output/jude.html", 1)
generate("Revelation", 27, "output/revelation.html", 22)

shutil.copy("index.html", "output/index.html")
<|endoftext|>


<commit_before>import collections

import numpy
import six

from chainer import cuda
from chainer import function
from chainer.utils import type_check

_args = 'float* y, float* x, int cdimy, int cdimx, int rdim, int coffset'
_preamble = '''
#define COPY(statement) \
    int l   = i / (rdim * cdimy);  \
    int c   = i / rdim % cdimy + coffset;  \
    int r   = i % rdim;  \
    int idx = r + rdim * (c + cdimx * l);  \
    statement;
'''

class SplitAxis(function.Function):

    """Function that splits multiple arrays towards the specified axis."""

    def __init__(self, indices_or_sections, axis):
        if not isinstance(indices_or_sections, (int, collections.Iterable)):
            raise TypeError('indices_or_sections must be integer or 1-D array')
        self.indices_or_sections = indices_or_sections
        self.axis = axis

    def check_type_forward(self, in_types):
        type_check.expect(in_types.size() == 1)
        type_check.expect(in_types[0].ndim >= self.axis)

        if isinstance(self.indices_or_sections, collections.Iterable):
            max_index = type_check.Variable(
                self.indices_or_sections[-1], 'max_index')
            type_check.expect(in_types[0].shape[self.axis] >= max_index)
        else:
            sections = type_check.Variable(
                self.indices_or_sections, 'sections')
            type_check.expect(in_types[0].shape[self.axis] % sections == 0)

    def check_type_backward(self, in_types, out_types):
        if isinstance(self.indices_or_sections, collections.Iterable):
            num_indices = type_check.Variable(
                len(self.indices_or_sections), 'len(indices)')
            output_size = num_indices + 1
        else:
            output_size = type_check.Variable(
                self.indices_or_sections, 'sections')

        type_check.expect(out_types.size() == output_size.eval())

        for out_type in out_types:
            type_check.expect(
                out_type.ndim == in_types[0].ndim,
                out_type.dtype == out_types[0].dtype
            )

        for out_type in out_types:
            for i in six.moves.range(in_types[0].ndim.eval()):
                if i == self.axis:
                    continue
                type_check.expect(out_type.shape[i] == in_types[0].shape[i])

        total_output_dim = sum(
            out_type.shape[self.axis] for out_type in out_types)
        type_check.expect(
            total_output_dim == in_types[0].shape[self.axis]
        )

    def forward_cpu(self, x):
        if isinstance(self.indices_or_sections, collections.Iterable):
            cdimx = x[0].shape[self.axis]
            ind = list(self.indices_or_sections)
            ind.append(cdimx)
            prev_i = 0
            for i in ind:
                cdimy = max(0, min(i, cdimx) - prev_i)
                if cdimy == 0:
                    raise ValueError('Not support if shape contains 0')
                prev_i = i
        return tuple(numpy.split(x[0], self.indices_or_sections, self.axis))

    def forward_gpu(self, x):
        xshape = x[0].shape
        self.cdimx = xshape[self.axis]
        self.rdim = numpy.prod(xshape[self.axis + 1:])

        if isinstance(self.indices_or_sections, collections.Iterable):
            ind = list(self.indices_or_sections)
            ind.append(self.cdimx)
        else:
            sec = self.indices_or_sections
            if self.cdimx % sec:
                raise ValueError(
                    'array split does not result in an equal division')
            ind = numpy.arange(1, sec + 1) * (self.cdimx // sec)
        ys = []
        kernel = cuda.elementwise(
            _args, 'COPY(y[i] = x[idx])', 'split_fwd', preamble=_preamble)
        prev_i = 0
        for i in ind:
            cdimy = max(0, min(i, self.cdimx) - prev_i)
            s = list(xshape)
            s[self.axis] = cdimy
            y = cuda.empty(s, dtype=x[0].dtype)
            if cdimy == 0:
                raise ValueError('Not support if shape contains 0')
            kernel(y, x[0], cdimy, self.cdimx, self.rdim, prev_i)
            prev_i = i
            ys.append(y)
        return tuple(ys)

    def backward_cpu(self, x, gys):
        return numpy.concatenate(gys, axis=self.axis),

    def backward_gpu(self, x, gys):
        gx = cuda.empty_like(x[0])
        coffset = 0
        kernel = cuda.elementwise(
            _args, 'COPY(x[idx] = y[i])', 'split_bwd', preamble=_preamble)
        for gy in gys:
            cdimy = gy.shape[self.axis]
            if cdimy != 0:
                kernel(gy, gx, cdimy, self.cdimx, self.rdim, coffset)
            coffset += cdimy
        return gx,

def split_axis(x, indices_or_sections, axis):
    """Splits given variables along an axis.

    Args:
        x (tuple of Variables): Variables to be split.
        indices_or_sections (int or 1-D array): If this argument is an integer,
            N, the array will be divided into N equal arrays along axis.
            If it is a 1-D array of sorted integers, it
            indicates the positions where the array is split.
        axis (int): Axis that the input array is split along.

    Returns:
        ``tuple`` or ``Variable``: Tuple of :class:`~chainer.Variable` objects
             if the number of outputs is more than 1 or
             :class:`~chainer.Variable` otherwise.

    .. note::
        This function raises ``ValueError`` if at least
        one of the outputs is splitted to zero-size
        (i.e. `axis`-th value of its shape is zero).

    """
    return SplitAxis(indices_or_sections, axis)(x)
<commit_msg>Fix condition on shape of input type of SplitAxis<commit_after>import collections

import numpy
import six

from chainer import cuda
from chainer import function
from chainer.utils import type_check

_args = 'float* y, float* x, int cdimy, int cdimx, int rdim, int coffset'
_preamble = '''
#define COPY(statement) \
    int l   = i / (rdim * cdimy);  \
    int c   = i / rdim % cdimy + coffset;  \
    int r   = i % rdim;  \
    int idx = r + rdim * (c + cdimx * l);  \
    statement;
'''

class SplitAxis(function.Function):

    """Function that splits multiple arrays towards the specified axis."""

    def __init__(self, indices_or_sections, axis):
        if not isinstance(indices_or_sections, (int, collections.Iterable)):
            raise TypeError('indices_or_sections must be integer or 1-D array')
        self.indices_or_sections = indices_or_sections
        self.axis = axis

    def check_type_forward(self, in_types):
        type_check.expect(in_types.size() == 1)
        type_check.expect(in_types[0].ndim >= self.axis)

        if isinstance(self.indices_or_sections, collections.Iterable):
            max_index = type_check.Variable(
                self.indices_or_sections[-1], 'max_index')
            type_check.expect(in_types[0].shape[self.axis] > max_index)
        else:
            sections = type_check.Variable(
                self.indices_or_sections, 'sections')
            type_check.expect(in_types[0].shape[self.axis] % sections == 0)

    def check_type_backward(self, in_types, out_types):
        if isinstance(self.indices_or_sections, collections.Iterable):
            num_indices = type_check.Variable(
                len(self.indices_or_sections), 'len(indices)')
            output_size = num_indices + 1
        else:
            output_size = type_check.Variable(
                self.indices_or_sections, 'sections')

        type_check.expect(out_types.size() == output_size.eval())

        for out_type in out_types:
            type_check.expect(
                out_type.ndim == in_types[0].ndim,
                out_type.dtype == out_types[0].dtype
            )

        for out_type in out_types:
            for i in six.moves.range(in_types[0].ndim.eval()):
                if i == self.axis:
                    continue
                type_check.expect(out_type.shape[i] == in_types[0].shape[i])

        total_output_dim = sum(
            out_type.shape[self.axis] for out_type in out_types)
        type_check.expect(
            total_output_dim == in_types[0].shape[self.axis]
        )

    def forward_cpu(self, x):
        if isinstance(self.indices_or_sections, collections.Iterable):
            cdimx = x[0].shape[self.axis]
            ind = list(self.indices_or_sections)
            ind.append(cdimx)
            prev_i = 0
            for i in ind:
                cdimy = max(0, min(i, cdimx) - prev_i)
                if cdimy == 0:
                    raise ValueError('Not support if shape contains 0')
                prev_i = i
        return tuple(numpy.split(x[0], self.indices_or_sections, self.axis))

    def forward_gpu(self, x):
        xshape = x[0].shape
        self.cdimx = xshape[self.axis]
        self.rdim = numpy.prod(xshape[self.axis + 1:])

        if isinstance(self.indices_or_sections, collections.Iterable):
            ind = list(self.indices_or_sections)
            ind.append(self.cdimx)
        else:
            sec = self.indices_or_sections
            if self.cdimx % sec:
                raise ValueError(
                    'array split does not result in an equal division')
            ind = numpy.arange(1, sec + 1) * (self.cdimx // sec)
        ys = []
        kernel = cuda.elementwise(
            _args, 'COPY(y[i] = x[idx])', 'split_fwd', preamble=_preamble)
        prev_i = 0
        for i in ind:
            cdimy = max(0, min(i, self.cdimx) - prev_i)
            s = list(xshape)
            s[self.axis] = cdimy
            y = cuda.empty(s, dtype=x[0].dtype)
            if cdimy == 0:
                raise ValueError('Not support if shape contains 0')
            kernel(y, x[0], cdimy, self.cdimx, self.rdim, prev_i)
            prev_i = i
            ys.append(y)
        return tuple(ys)

    def backward_cpu(self, x, gys):
        return numpy.concatenate(gys, axis=self.axis),

    def backward_gpu(self, x, gys):
        gx = cuda.empty_like(x[0])
        coffset = 0
        kernel = cuda.elementwise(
            _args, 'COPY(x[idx] = y[i])', 'split_bwd', preamble=_preamble)
        for gy in gys:
            cdimy = gy.shape[self.axis]
            if cdimy != 0:
                kernel(gy, gx, cdimy, self.cdimx, self.rdim, coffset)
            coffset += cdimy
        return gx,

def split_axis(x, indices_or_sections, axis):
    """Splits given variables along an axis.

    Args:
        x (tuple of Variables): Variables to be split.
        indices_or_sections (int or 1-D array): If this argument is an integer,
            N, the array will be divided into N equal arrays along axis.
            If it is a 1-D array of sorted integers, it
            indicates the positions where the array is split.
        axis (int): Axis that the input array is split along.

    Returns:
        ``tuple`` or ``Variable``: Tuple of :class:`~chainer.Variable` objects
             if the number of outputs is more than 1 or
             :class:`~chainer.Variable` otherwise.

    .. note::
        This function raises ``ValueError`` if at least
        one of the outputs is splitted to zero-size
        (i.e. `axis`-th value of its shape is zero).

    """
    return SplitAxis(indices_or_sections, axis)(x)
<|endoftext|>


<commit_before># This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See LICENSE for more details.
#
# Copyright (c) 2020 ScyllaDB

from contextlib import contextmanager, ExitStack

from sdcm.sct_events import Severity
from sdcm.sct_events.base import LogEvent
from sdcm.sct_events.filters import DbEventsFilter, EventsSeverityChangerFilter, EventsFilter
from sdcm.sct_events.loaders import YcsbStressEvent
from sdcm.sct_events.database import DatabaseLogEvent
from sdcm.sct_events.monitors import PrometheusAlertManagerEvent

@contextmanager
def ignore_alternator_client_errors():
    with ExitStack() as stack:
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=PrometheusAlertManagerEvent,
            regex=".*YCSBTooManyErrors.*",
            extra_time_to_expiration=60,
        ))
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=PrometheusAlertManagerEvent,
            regex=".*YCSBTooManyVerifyErrors.*",
            extra_time_to_expiration=60,
        ))
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=YcsbStressEvent,
            regex=r".*Cannot achieve consistency level.*",
            extra_time_to_expiration=30,
        ))
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=YcsbStressEvent,
            regex=r".*Operation timed out.*",
            extra_time_to_expiration=30,
        ))
        yield

@contextmanager
def ignore_operation_errors():
    with ExitStack() as stack:
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=LogEvent,
            regex=r".*Operation timed out.*",
            extra_time_to_expiration=30,
        ))
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=LogEvent,
            regex=r".*Operation failed for system.paxos.*",
            extra_time_to_expiration=30,
        ))
        yield

@contextmanager
def ignore_upgrade_schema_errors():
    with ExitStack() as stack:
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.DATABASE_ERROR,
            line="Failed to load schema",
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.SCHEMA_FAILURE,
            line="Failed to load schema",
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.DATABASE_ERROR,
            line="Failed to pull schema",
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.RUNTIME_ERROR,
            line="Failed to load schema",
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.DATABASE_ERROR,
            line="Could not retrieve CDC streams with timestamp",
        ))
        # This error message occurs during version rating only for the Drain operating system.
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.DATABASE_ERROR,
            line="cql_server - exception while processing connection: seastar::nested_exception "
                 "(seastar::nested_exception)",
        ))
        yield

@contextmanager
def ignore_no_space_errors(node):
    with ExitStack() as stack:
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.NO_SPACE_ERROR,
            node=node,
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.BACKTRACE,
            line="No space left on device",
            node=node,
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.DATABASE_ERROR,
            line="No space left on device",
            node=node,
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.FILESYSTEM_ERROR,
            line="No space left on device",
            node=node,
        ))
        yield

@contextmanager
def ignore_mutation_write_errors():
    with ExitStack() as stack:
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=LogEvent,
            regex=r".*mutation_write_*",
            extra_time_to_expiration=30
        ))
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=LogEvent,
            regex=r".*Operation timed out for system.paxos.*",
            extra_time_to_expiration=30,
        ))
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=LogEvent,
            regex=r".*Operation failed for system.paxos.*",
            extra_time_to_expiration=30,
        ))
        yield

@contextmanager
def ignore_ycsb_connection_refused():
    with EventsFilter(event_class=YcsbStressEvent, regex='.*Unable to execute HTTP request: Connection refused.*'):
        yield

@contextmanager
def ignore_scrub_invalid_errors():
    with ExitStack() as stack:
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.DATABASE_ERROR,
            line="Skipping invalid clustering row fragment",
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.DATABASE_ERROR,
            line="Skipping invalid partition",
        ))
        yield
<commit_msg>fix(upgrade): don't ignore CDC error in upgrade<commit_after># This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See LICENSE for more details.
#
# Copyright (c) 2020 ScyllaDB

from contextlib import contextmanager, ExitStack

from sdcm.sct_events import Severity
from sdcm.sct_events.base import LogEvent
from sdcm.sct_events.filters import DbEventsFilter, EventsSeverityChangerFilter, EventsFilter
from sdcm.sct_events.loaders import YcsbStressEvent
from sdcm.sct_events.database import DatabaseLogEvent
from sdcm.sct_events.monitors import PrometheusAlertManagerEvent

@contextmanager
def ignore_alternator_client_errors():
    with ExitStack() as stack:
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=PrometheusAlertManagerEvent,
            regex=".*YCSBTooManyErrors.*",
            extra_time_to_expiration=60,
        ))
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=PrometheusAlertManagerEvent,
            regex=".*YCSBTooManyVerifyErrors.*",
            extra_time_to_expiration=60,
        ))
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=YcsbStressEvent,
            regex=r".*Cannot achieve consistency level.*",
            extra_time_to_expiration=30,
        ))
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=YcsbStressEvent,
            regex=r".*Operation timed out.*",
            extra_time_to_expiration=30,
        ))
        yield

@contextmanager
def ignore_operation_errors():
    with ExitStack() as stack:
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=LogEvent,
            regex=r".*Operation timed out.*",
            extra_time_to_expiration=30,
        ))
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=LogEvent,
            regex=r".*Operation failed for system.paxos.*",
            extra_time_to_expiration=30,
        ))
        yield

@contextmanager
def ignore_upgrade_schema_errors():
    with ExitStack() as stack:
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.DATABASE_ERROR,
            line="Failed to load schema",
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.SCHEMA_FAILURE,
            line="Failed to load schema",
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.DATABASE_ERROR,
            line="Failed to pull schema",
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.RUNTIME_ERROR,
            line="Failed to load schema",
        ))
        # This error message occurs during version rating only for the Drain operating system.
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.DATABASE_ERROR,
            line="cql_server - exception while processing connection: seastar::nested_exception "
                 "(seastar::nested_exception)",
        ))
        yield

@contextmanager
def ignore_no_space_errors(node):
    with ExitStack() as stack:
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.NO_SPACE_ERROR,
            node=node,
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.BACKTRACE,
            line="No space left on device",
            node=node,
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.DATABASE_ERROR,
            line="No space left on device",
            node=node,
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.FILESYSTEM_ERROR,
            line="No space left on device",
            node=node,
        ))
        yield

@contextmanager
def ignore_mutation_write_errors():
    with ExitStack() as stack:
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=LogEvent,
            regex=r".*mutation_write_*",
            extra_time_to_expiration=30
        ))
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=LogEvent,
            regex=r".*Operation timed out for system.paxos.*",
            extra_time_to_expiration=30,
        ))
        stack.enter_context(EventsSeverityChangerFilter(
            new_severity=Severity.WARNING,
            event_class=LogEvent,
            regex=r".*Operation failed for system.paxos.*",
            extra_time_to_expiration=30,
        ))
        yield

@contextmanager
def ignore_ycsb_connection_refused():
    with EventsFilter(event_class=YcsbStressEvent, regex='.*Unable to execute HTTP request: Connection refused.*'):
        yield

@contextmanager
def ignore_scrub_invalid_errors():
    with ExitStack() as stack:
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.DATABASE_ERROR,
            line="Skipping invalid clustering row fragment",
        ))
        stack.enter_context(DbEventsFilter(
            db_event=DatabaseLogEvent.DATABASE_ERROR,
            line="Skipping invalid partition",
        ))
        yield
<|endoftext|>


<commit_before>import chainer
import chainer.functions as F
from chainer.initializers import LeCunUniform
import chainer.links as L
import numpy

from chainerrl.initializers import VarianceScalingConstant

class FactorizedNoisyLinear(chainer.Chain):
    """Linear layer in Factorized Noisy Network

    Args:
        mu_link (L.Linear): Linear link that computes mean of output.
        sigma_scale (float): The hyperparameter sigma_0 in the original paper.
            Scaling factor of the initial weights of noise-scaling parameters.
    """

    def __init__(self, mu_link, sigma_scale=0.4):
        super(FactorizedNoisyLinear, self).__init__()
        self.out_size = mu_link.out_size
        self.nobias = not ('/b' in [name for name, _ in mu_link.namedparams()])

        W_data = mu_link.W.data
        in_size = None if W_data is None else W_data.shape[1]

        with self.init_scope():
            self.mu = L.Linear(in_size, self.out_size, self.nobias,
                               initialW=LeCunUniform(1 / numpy.sqrt(3)))

            self.sigma = L.Linear(in_size, self.out_size, self.nobias,
                                  initialW=VarianceScalingConstant(
                                      sigma_scale),
                                  initial_bias=VarianceScalingConstant(sigma_scale, fan='out'))

        device_id = self.mu._device_id
        if device_id is not None:
            self.to_gpu(device_id)

    def _eps(self, shape, dtype):
        xp = self.xp
        r = xp.random.standard_normal(shape).astype(dtype)

        # apply the function f
        return xp.copysign(xp.sqrt(xp.abs(r)), r)

    def __call__(self, x):
        if self.mu.W.data is None:
            self.mu.W.initialize((self.out_size, numpy.prod(x.shape[1:])))
        if self.sigma.W.data is None:
            self.sigma.W.initialize((self.out_size, numpy.prod(x.shape[1:])))
        if self.sigma.b.data is None:
            self.sigma.b.initialize((self.out_size))

        # use info of sigma.W to avoid strange error messages
        dtype = self.sigma.W.dtype
        out_size, in_size = self.sigma.W.shape

        eps_x = self._eps(in_size, dtype)
        eps_y = self._eps(out_size, dtype)
        W = self.mu.W + self.sigma.W * self.xp.outer(eps_y, eps_x)
        if self.nobias:
            return F.linear(x, W)
        else:
            b = self.mu.b + self.sigma.b * eps_y
            return F.linear(x, W, b)
<commit_msg>nobias fix<commit_after>import chainer
import chainer.functions as F
from chainer.initializers import LeCunUniform
import chainer.links as L
import numpy

from chainerrl.initializers import VarianceScalingConstant

class FactorizedNoisyLinear(chainer.Chain):
    """Linear layer in Factorized Noisy Network

    Args:
        mu_link (L.Linear): Linear link that computes mean of output.
        sigma_scale (float): The hyperparameter sigma_0 in the original paper.
            Scaling factor of the initial weights of noise-scaling parameters.
    """

    def __init__(self, mu_link, sigma_scale=0.4):
        super(FactorizedNoisyLinear, self).__init__()
        self.out_size = mu_link.out_size
        self.nobias = not ('/b' in [name for name, _ in mu_link.namedparams()])

        W_data = mu_link.W.data
        in_size = None if W_data is None else W_data.shape[1]

        with self.init_scope():
            self.mu = L.Linear(in_size, self.out_size, self.nobias,
                               initialW=LeCunUniform(1 / numpy.sqrt(3)))

            self.sigma = L.Linear(in_size, self.out_size, self.nobias,
                                  initialW=VarianceScalingConstant(
                                      sigma_scale),
                                  initial_bias=VarianceScalingConstant(sigma_scale, fan='out'))

        device_id = self.mu._device_id
        if device_id is not None:
            self.to_gpu(device_id)

    def _eps(self, shape, dtype):
        xp = self.xp
        r = xp.random.standard_normal(shape).astype(dtype)

        # apply the function f
        return xp.copysign(xp.sqrt(xp.abs(r)), r)

    def __call__(self, x):
        if self.mu.W.data is None:
            self.mu.W.initialize((self.out_size, numpy.prod(x.shape[1:])))
        if self.sigma.W.data is None:
            self.sigma.W.initialize((self.out_size, numpy.prod(x.shape[1:])))
        if self.sigma.b and self.sigma.b.data is None:
            self.sigma.b.initialize((self.out_size))

        # use info of sigma.W to avoid strange error messages
        dtype = self.sigma.W.dtype
        out_size, in_size = self.sigma.W.shape

        eps_x = self._eps(in_size, dtype)
        eps_y = self._eps(out_size, dtype)
        W = self.mu.W + self.sigma.W * self.xp.outer(eps_y, eps_x)
        if self.nobias:
            return F.linear(x, W)
        else:
            b = self.mu.b + self.sigma.b * eps_y
            return F.linear(x, W, b)
<|endoftext|>