<commit_before>Acceptance tests for multiple graphs
====================================

Double backprop with different graphs
-------------------------------------

>>> import xchainer as xc

>>> x = xc.Array((3,), xc.float32, [1, 2, 3]).require_grad('input')
>>> w = xc.Array((3,), xc.float32, [4, 5, 6]).require_grad('weight')
>>> y = x * w
>>> y.is_grad_required('input')
True
>>> y.is_grad_required('weight')
True
>>> y.is_grad_required()  # 'default'
False

>>> xc.backward(y, graph_id='input')
>>> gx = x.get_grad('input')
>>> gx  # == w
array([4., 5., 6.], dtype=float32, graph_ids=['weight'])
>>> w.get_grad('input')
Traceback (most recent call last):
  ...
xchainer.XchainerError: Cannot find ArrayNode for graph: input

>>> z = gx * w  # == w * w
>>> xc.backward(z, graph_id='weight')
>>> w.get_grad('weight')  # == 2 * w
array([ 8., 10., 12.], dtype=float32)
>>> x.get_grad('weight')
Traceback (most recent call last):
  ...
xchainer.XchainerError: Cannot find ArrayNode for graph: weight


Double backprop with single graph
---------------------------------

>>> x = xc.Array((3,), xc.float32, [1, 2, 3]).require_grad()
>>> w = xc.Array((3,), xc.float32, [4, 5, 6]).require_grad()
>>> y = x * w
>>> y.is_grad_required()
True
>>> y.is_grad_required('foo')  # unknown graph name
False

>>> xc.backward(y, enable_double_backprop=True)
>>> gx = x.get_grad()
>>> gx  # == w
array([4., 5., 6.], dtype=float32, graph_ids=['default'])
>>> w.get_grad()  # == x
array([1., 2., 3.], dtype=float32, graph_ids=['default'])

>>> w.clear_grad()
>>> z = gx * w  # == w * w
>>> xc.backward(z)
>>> w.get_grad()  # == 2 * w
array([ 8., 10., 12.], dtype=float32)
>>> x.get_grad()  # the second backprop does not reach here
array([4., 5., 6.], dtype=float32, graph_ids=['default'])
>>> x.get_grad() is gx
True
<commit_msg>Fix clear grad in acceptance test<commit_after>Acceptance tests for multiple graphs
====================================

Double backprop with different graphs
-------------------------------------

>>> import xchainer as xc

>>> x = xc.Array((3,), xc.float32, [1, 2, 3]).require_grad('input')
>>> w = xc.Array((3,), xc.float32, [4, 5, 6]).require_grad('weight')
>>> y = x * w
>>> y.is_grad_required('input')
True
>>> y.is_grad_required('weight')
True
>>> y.is_grad_required()  # 'default'
False

>>> xc.backward(y, graph_id='input')
>>> gx = x.get_grad('input')
>>> gx  # == w
array([4., 5., 6.], dtype=float32, graph_ids=['weight'])
>>> w.get_grad('input')
Traceback (most recent call last):
  ...
xchainer.XchainerError: Cannot find ArrayNode for graph: input

>>> z = gx * w  # == w * w
>>> xc.backward(z, graph_id='weight')
>>> w.get_grad('weight')  # == 2 * w
array([ 8., 10., 12.], dtype=float32)
>>> x.get_grad('weight')
Traceback (most recent call last):
  ...
xchainer.XchainerError: Cannot find ArrayNode for graph: weight


Double backprop with single graph
---------------------------------

>>> x = xc.Array((3,), xc.float32, [1, 2, 3]).require_grad()
>>> w = xc.Array((3,), xc.float32, [4, 5, 6]).require_grad()
>>> y = x * w
>>> y.is_grad_required()
True
>>> y.is_grad_required('foo')  # unknown graph name
False

>>> xc.backward(y, enable_double_backprop=True)
>>> gx = x.get_grad()
>>> gx  # == w
array([4., 5., 6.], dtype=float32, graph_ids=['default'])
>>> w.get_grad()  # == x
array([1., 2., 3.], dtype=float32, graph_ids=['default'])

>>> w.set_grad(None)
>>> z = gx * w  # == w * w
>>> xc.backward(z)
>>> w.get_grad()  # == 2 * w
array([ 8., 10., 12.], dtype=float32)
>>> x.get_grad()  # the second backprop does not reach here
array([4., 5., 6.], dtype=float32, graph_ids=['default'])
>>> x.get_grad() is gx
True

{'fraction_non_alphanumeric': 0.2738225629791895, 'fraction_numerical': 0.03285870755750274, 'mean_word_length': 3.6953727506426737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 183, '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': '4732460', 'n_tokens_mistral': 1436, 'n_tokens_neox': 1351, 'n_words': 339}