<commit_before>import pytest

import xchainer


def _check_backward_unary(fprop, requires_grad=True):
    x = xchainer.Array((3,), xchainer.float32, [1., 2., 1.])
    if requires_grad:
        x.require_grad()

    xchainer.check_backward(
        fprop,
        (x,),
        (xchainer.Array((3,), xchainer.float32, [0., -2., 1.]),),
        (xchainer.full((3,), 1e-3, xchainer.float32),),
    )


def test_correct_backward_unary():
    _check_backward_unary(lambda xs: (xs[0] * xs[0],))


def test_no_grad_required():
    _check_backward_unary(lambda xs: (xs[0] * xs[0],), False)


@pytest.mark.xfail
def test_incorrect_backward_unary():
    def fprop(xs):
        x, = xs
        return (x * x).as_constant() + x,
    _check_backward_unary(fprop)


def _check_backward_binary(fprop):
    xchainer.check_backward(
        fprop,
        (xchainer.Array((3,), xchainer.float32, [1., -2., 1.]).require_grad(),
         xchainer.Array((3,), xchainer.float32, [0., 1., 2.]).require_grad()),
        (xchainer.Array((3,), xchainer.float32, [1., -2., 3.]),),
        (xchainer.full((3,), 1e-3, xchainer.float32), xchainer.full((3,), 1e-3, xchainer.float32)),
    )


def test_correct_backward_binary():
    _check_backward_binary(lambda xs: (xs[0] * xs[1],))


@pytest.mark.xfail
def test_incorrect_backward_binary():
    def fprop(xs):
        x, y = xs
        return (x * y).as_constant() + x + y,
    _check_backward_binary(fprop)<commit_msg>Use pytest.raises instead of xfail<commit_after>import pytest

import xchainer


def _check_backward_unary(fprop, requires_grad=True):
    x = xchainer.Array((3,), xchainer.float32, [1., 2., 1.])
    if requires_grad:
        x.require_grad()

    xchainer.check_backward(
        fprop,
        (x,),
        (xchainer.Array((3,), xchainer.float32, [0., -2., 1.]),),
        (xchainer.full((3,), 1e-3, xchainer.float32),),
    )


def test_correct_backward_unary():
    _check_backward_unary(lambda xs: (xs[0] * xs[0],))


def test_no_grad_required():
    _check_backward_unary(lambda xs: (xs[0] * xs[0],), False)


def test_incorrect_backward_unary():
    def fprop(xs):
        x, = xs
        return (x * x).as_constant() + x,
    with pytest.raises(xchainer.GradientCheckError):
        _check_backward_unary(fprop)


def _check_backward_binary(fprop):
    xchainer.check_backward(
        fprop,
        (xchainer.Array((3,), xchainer.float32, [1., -2., 1.]).require_grad(),
         xchainer.Array((3,), xchainer.float32, [0., 1., 2.]).require_grad()),
        (xchainer.Array((3,), xchainer.float32, [1., -2., 3.]),),
        (xchainer.full((3,), 1e-3, xchainer.float32), xchainer.full((3,), 1e-3, xchainer.float32)),
    )


def test_correct_backward_binary():
    _check_backward_binary(lambda xs: (xs[0] * xs[1],))


def test_incorrect_backward_binary():
    def fprop(xs):
        x, y = xs
        return (x * y).as_constant() + x + y,
    with pytest.raises(xchainer.GradientCheckError):
        _check_backward_binary(fprop)
{'fraction_non_alphanumeric': 0.17141898691714189, 'fraction_numerical': 0.03421670580342167, 'mean_word_length': 3.26, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2877684', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1183, 'n_words': 199}