Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge with master #22

Merged
merged 1,566 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
1566 commits
Select commit Hold shift click to select a range
dc1dbf3
Added chainer.grad features to chainerx.grad
Jun 24, 2019
16ed562
add Variable.mean
yuyu2172 Jul 2, 2019
49b86e7
gsl::span -> absl::Span
hvy Jul 2, 2019
61bbf80
Fix a flake8 error
ktns Jul 2, 2019
fa2cd8c
Merge pull request #7663 from kmaehashi/fix-flake8-error
toslunar Jul 2, 2019
0bdd077
Merge pull request #7592 from niboshi/optimizer-hook-refactor
mergify[bot] Jul 2, 2019
8b511ba
Merge remote-tracking branch 'upstream/master' into opt
shu65 Jul 2, 2019
3a6e9ad
Merge pull request #7671 from hvy/gsl-to-absl
mergify[bot] Jul 2, 2019
8777e90
Merge pull request #7667 from niboshi/curly-braces
mergify[bot] Jul 2, 2019
1999ed7
Merge remote-tracking branch 'origin/master' into replace-optional
niboshi Jul 2, 2019
47e2b54
Merge pull request #7646 from niboshi/replace-optional
hvy Jul 2, 2019
39c0b0a
Add TODOs to handle zero norm
niboshi Jul 3, 2019
a3bc84e
Merge pull request #7561 from asi1024/chainerx-ellipsis
Jul 3, 2019
957d205
Add chainerx.softplus
aksub99 Jul 3, 2019
4c5be66
Added code to check the device being used in tests
Jun 18, 2019
0b18308
remove grad_inputs from chainerx.grad
Jul 3, 2019
5519e2a
Merge remote-tracking branch 'origin/master' into cleanup-cmake
niboshi Jul 2, 2019
9e8da7b
merge with master
Jul 3, 2019
ac47a0d
Move list(APPEND CUDA_NVCC_FLAGS -std=c 14)
niboshi Jul 3, 2019
5324c95
Use CMAKE_CXX_STANDARD for CUDA_NVCC_FLAGS -std=c 14
niboshi Jul 3, 2019
17a413c
fix absl optional
Jul 3, 2019
b845099
Merge pull request #6492 from Crissman/docinstall
niboshi Jul 3, 2019
d8a9062
same code for all backends
Jul 3, 2019
9d2067b
remove grad inputs from chainerx.grad tests
Jul 3, 2019
6fef53f
Merge pull request #7665 from chainer/kuenishi-patch-1
mergify[bot] Jul 3, 2019
98c8b25
Merge pull request #7647 from niboshi/cleanup-cmake
take-cheeze Jul 3, 2019
c1947e6
Improve comment
niboshi Jul 3, 2019
6d0fd18
Fix TODO syntax
Jul 3, 2019
92b9144
Fix typo
gwtnb Jul 3, 2019
d3b7eae
Improve shapes in group normalization tests
toslunar Jul 3, 2019
5e366dd
Stop retrying tests
toslunar Jul 3, 2019
d0e5b37
Merge pull request #7548 from emcastillo/fix_initializers_device_id
niboshi Jul 3, 2019
abaec9a
Update chainer/datasets/pickle_dataset.py
zaltoprofen Jul 3, 2019
5f87669
Fix import
zaltoprofen Jul 3, 2019
09252fb
Add some comments
zaltoprofen Jul 3, 2019
43a240f
Add unit-test
zaltoprofen Jul 3, 2019
16c103c
added chainerx support on initializers
Jul 3, 2019
ad9b76d
Merge pull request #7682 from gwtnb/typo
kmaehashi Jul 3, 2019
0f6cb2d
Merge pull request #7661 from toslunar/fix-amsgrad-vhat
niboshi Jul 3, 2019
d4057c5
Fix test
zaltoprofen Jul 3, 2019
fa25643
Merge pull request #7518 from asi1024/build-info
niboshi Jul 3, 2019
c791818
Merge pull request #7669 from emcastillo/chx_gradient_lars
asi1024 Jul 3, 2019
fb1bcbb
address comment
kshitij12345 Jul 3, 2019
3e05fe0
support ideep `mdarray`
kshitij12345 Jul 3, 2019
0da9c46
use only one fuction for test
kshitij12345 Jul 3, 2019
0724419
Comment tests for deprecated communicators
keisukefukuda Jul 3, 2019
45a36a7
refactored tests for chainer.grad to use inject backends
Jul 4, 2019
8df0457
Merge pull request #7687 from emcastillo/initializers
mergify[bot] Jul 4, 2019
441a5d3
Fix with using device on grad impl
Jul 4, 2019
0e7e961
Merge branch 'master' into function_ptr_type
Jul 4, 2019
4fbc1f5
Fixes an error on chainer.grad for multiple devices
Jul 4, 2019
d5f9a84
delete tests in test_variable.py
yuyu2172 Jul 4, 2019
88daf9c
add test in test_average
yuyu2172 Jul 4, 2019
d89eee6
Merge pull request #7692 from emcastillo/fix_grad_device
mergify[bot] Jul 4, 2019
2367b1c
Merge pull request #7641 from niboshi/gradient_clipping
hvy Jul 4, 2019
0d65b03
Fix Adam FP16 overflow on gpu kernels
Jul 4, 2019
9ee61a7
Merge branch 'master' into ecastill_chainerx_grad
Jul 4, 2019
7884304
Merge pull request #7202 from toslunar/brn-eps
niboshi Jul 4, 2019
222c8c4
Merge pull request #7657 from kshitij12345/lasso_chainerx
Jul 4, 2019
5b0dfe7
Removed deprecated communicators (Hierarchical, TwoDimensional, Singl…
keisukefukuda Jul 4, 2019
57c8e85
Merge pull request #7648 from niboshi/device-hashable
okapies Jul 4, 2019
9dbfa4e
Fixes spectral normalization chainerx conversion
Jul 4, 2019
6376f4f
Merge pull request #7670 from yuyu2172/variable-mean
Jul 4, 2019
4891d4b
Removed all documents, tests that use or depend on the removed commun…
keisukefukuda Jul 4, 2019
32c8d4a
Fix import
zaltoprofen Jul 4, 2019
6224551
ident error
Jul 4, 2019
29ae557
Merge pull request #7666 from niboshi/else-after-return
Crissman Jul 4, 2019
a742062
Merge pull request #7339 from sky58/addBinaryShift
mergify[bot] Jul 4, 2019
a482347
Fix tests
aksub99 Jul 4, 2019
94e88e8
Merge pull request #7684 from toslunar/fix-test-gn
mergify[bot] Jul 4, 2019
d3fb079
Merge pull request #7537 from kshitij12345/add-kwarg-device
mergify[bot] Jul 4, 2019
d800217
Merge pull request #7582 from hvy/chx-loss-followup
niboshi Jul 4, 2019
c47a378
Merge pull request #7698 from emcastillo/spectral
niboshi Jul 4, 2019
237e984
address comment
kshitij12345 Jul 4, 2019
f5f0cc6
save memory allocation using kwarg `out`.
kshitij12345 Jul 4, 2019
1145baf
re-add the TODO comment
kshitij12345 Jul 4, 2019
0d878f3
update test for gradient noise hook
kshitij12345 Jul 4, 2019
5928909
Update chainer/training/triggers/interval_trigger.py
ktns Jul 5, 2019
504ee11
Use `str.format()` also in the unittest
ktns Jul 5, 2019
d7d2a95
Use `assert` instead of `unittest.TestCase.assertEqual`
ktns Jul 5, 2019
daded08
format fix
Jul 4, 2019
a5dc604
Added a common test base class with lapack check
IvanYashchuk Jul 5, 2019
f2ecdfa
Added failing float16 dtype case to tests
IvanYashchuk Jul 5, 2019
242ffdb
Make failing float16 dtype case tests separate
IvanYashchuk Jul 5, 2019
38a07de
Fixed solve implementation for two-dimensional right hand side (b)
IvanYashchuk Jul 5, 2019
5fbb854
Added possible shapes of b to solve docs
IvanYashchuk Jul 5, 2019
40a6663
Merge pull request #7664 from ktns/interval_trigger_str
okapies Jul 5, 2019
66fa51f
Now everything compiles correctly if lapack is not available
IvanYashchuk Jul 5, 2019
d43235a
assert mock.call with noise.mock_calls
kshitij12345 Jul 5, 2019
eedb9dc
Add negative parametrizations
aksub99 Jul 5, 2019
c682b11
Merge pull request #7658 from prabhatnagarajan/expand_dims_docs
toslunar Jul 5, 2019
c8e47f7
Merge pull request #7632 from knorth55/concat-int
toslunar Jul 5, 2019
4b89876
Use `six.integer_types` for axis checks
toslunar Jul 5, 2019
3756b4c
Fix #5514 again
toslunar Jul 5, 2019
5d387c1
test for chainerx backend in weight decay
kshitij12345 Jul 5, 2019
5098955
Small fix for unused dtype variable
IvanYashchuk Jul 6, 2019
0200108
Merge branch 'master' into ivan-add-solve
IvanYashchuk Jul 8, 2019
e900b79
Relax atol/rol of chx erf float16 test
beam2d Jul 8, 2019
b642b6b
address comments
kshitij12345 Jul 8, 2019
b50ac6b
Merge pull request #7721 from beam2d/chx-erf-flaky
mergify[bot] Jul 8, 2019
505298d
pep8 fix
keisukefukuda Jul 8, 2019
03ae62d
Fix flaky chainerx.huber_loss
asi1024 Jul 8, 2019
a34d3ee
Merge pull request #7656 from kshitij12345/gradient_hard_clipping
asi1024 Jul 8, 2019
a2eaa67
Create links.rnn
himkt Jul 8, 2019
8c39ff5
Create functions.rnn
himkt Jul 8, 2019
0f9dff8
Adopt changes of links and functions
himkt Jul 8, 2019
9fcb6a1
Fix order of imports
himkt Jul 8, 2019
b859738
Merge pull request #4822 from kmaehashi/fix-mutable
mergify[bot] Jul 9, 2019
88c23a8
Merge pull request #7713 from toslunar/py2-axis
okapies Jul 9, 2019
139e91a
Fix offset in chainerx::Flip
asi1024 Jul 9, 2019
c05a1fb
Rename dummy variable
zaltoprofen Jul 9, 2019
332fd55
Reverse input array for non-contiguous tests
asi1024 Jul 9, 2019
34ccdc9
Remove dodge_nondifferentiable = True
asi1024 Jul 8, 2019
11c1ae5
Merge pull request #7723 from asi1024/flakey-huber-loss
mergify[bot] Jul 9, 2019
0b7be1d
Merge pull request #7625 from zaltoprofen/fix-pickle-dataset
niboshi Jul 9, 2019
a79cb9f
Modify beta range
aksub99 Jul 9, 2019
87d72e4
NOLINT added for loop
Jul 9, 2019
0ae69a7
Merge pull request #7679 from aksub99/Add_softplus
mergify[bot] Jul 9, 2019
8bc8783
Test invalid mixtures of arrays
Jul 4, 2019
ab23f02
Fix reporter for multi-thread use
niboshi Jul 9, 2019
41001b2
copy `Sum` function node
toslunar Jul 9, 2019
f81194d
Fix Parameter.dtype for uninitialized parameter
niboshi Jul 9, 2019
e7a1a9d
fixed loop
Jul 10, 2019
7630c44
Merge pull request #7520 from emcastillo/function_ptr_type
niboshi Jul 10, 2019
28d707e
Check if _build_info exists before import
asi1024 Jul 9, 2019
d14d454
Missing back conversions
Jul 10, 2019
b21a06f
Fix UpdateRule.use_fp32_update for uninitialized parameter
niboshi Jul 9, 2019
496fa2f
Fixes to method scope and comments
Jul 10, 2019
26ba569
Merge pull request #7464 from emcastillo/ecastill_chainerx_grad
mergify[bot] Jul 10, 2019
34655ef
Merge pull request #7486 from emcastillo/ecastill_multiproc
mergify[bot] Jul 10, 2019
61f1ff2
Merge pull request #7727 from asi1024/fix-flip
Jul 10, 2019
b899db4
Merge pull request #7715 from toslunar/ignore-protobuf-warn-py37
Jul 10, 2019
0b2a755
Add native implementation
aksub99 Jun 20, 2019
5568dc3
Modify native implementation and move code to indexing.cc
aksub99 Jun 23, 2019
3f8ae4b
Overload *= operator for cuda implementation
aksub99 Jun 23, 2019
ec6983b
Move files from reduction to indexing
aksub99 Jun 23, 2019
464e74b
Fix error in loop
aksub99 Jun 24, 2019
28a5a09
Remove *= operator overload
aksub99 Jun 24, 2019
1e87646
Add cumsum to kernels
aksub99 Jun 24, 2019
618d838
Add basic version of cuda implementation
aksub99 Jun 24, 2019
af7bd3b
Fix formatting
aksub99 Jun 24, 2019
156d529
Fix cuda implementation error
aksub99 Jun 25, 2019
2ffb1e3
throw NotImplementedError in cuda implementation and move implementat…
aksub99 Jul 1, 2019
c32b16a
Comment arguments
aksub99 Jul 1, 2019
f11871b
Add tests
aksub99 Jul 2, 2019
cf69ecc
Fix chainerx._testing error and modify native implementation
aksub99 Jul 4, 2019
0c2db29
Fix implementation errors
aksub99 Jul 4, 2019
fa619d4
Modify tests and native implementation
aksub99 Jul 4, 2019
f31a625
Make suggested changes
aksub99 Jul 5, 2019
2e3e343
Modify dtypes
aksub99 Jul 5, 2019
a2e0e89
Remove it_axis and redundant CopyIndexs
aksub99 Jul 6, 2019
349c285
Fix dtype mismatch
aksub99 Jul 6, 2019
b334524
Add support for axis=None
aksub99 Jul 8, 2019
81b65e9
Add missing header
aksub99 Jul 8, 2019
1eb9fbb
Remove redundant copy
aksub99 Jul 8, 2019
bb7c55a
Add backward implementation
aksub99 Jul 8, 2019
9451983
Merge remote-tracking branch 'chainer/master' into noncontiguous-test…
asi1024 Jul 10, 2019
f8c37e0
Fix eps in Contrastive.backward
asi1024 Jul 10, 2019
f8be3bb
Merge pull request #7697 from keisukefukuda/inactivate-deprecated-com…
belldandyxtq Jul 10, 2019
7cf898b
Merge remote-tracking branch 'upstream/master' into opt
shu65 Jul 10, 2019
1fd86cf
Make suggested changes
aksub99 Jul 10, 2019
ec53c38
Add TODO
aksub99 Jul 10, 2019
52efe38
Fix braces, clang-tidy
IvanYashchuk Jul 10, 2019
c25bf90
Fix formatting
aksub99 Jul 11, 2019
61eb325
Make changes
aksub99 Jul 11, 2019
420eee5
Merge pull request #7735 from niboshi/uninitialized-param-dtype
mergify[bot] Jul 11, 2019
6ba6fa8
Fix to avoid the case of square_sum(x1 - x2, axis=1).axis(1) has a va…
asi1024 Jul 10, 2019
575df22
Merge pull request #7736 from niboshi/optimizer-use-fp32-update
Jul 11, 2019
ed37746
Require CUDA if CHAINERX_BUILD_CUDA is set
niboshi Jul 11, 2019
a48e2e8
Modify backward
aksub99 Jul 11, 2019
baac2ce
Add strict mode to scatter_dataset as well as scatter_index
kuenishi May 29, 2019
0b199ec
Merge pull request #7716 from kshitij12345/chainerx-test-weight-decay
mergify[bot] Jul 12, 2019
c5baf01
Remove unnecessary if block
aksub99 Jul 12, 2019
23941e8
Merge pull request #7709 from kshitij12345/test-gradient-noise
Jul 12, 2019
a33253b
Implement `Mean` function node
toslunar Jul 12, 2019
dc456a1
Test if `F.average` overflows
toslunar Jul 12, 2019
d98e3a2
ChainerX fallback
toslunar Jul 12, 2019
88a6cfd
Require new NumPy
toslunar Jul 12, 2019
6664bf5
Update comment
asi1024 Jul 12, 2019
314548c
Merge pull request #7747 from asi1024/flaky-contrastive2
mergify[bot] Jul 12, 2019
ab8406c
Fix test condition and tolerance
toslunar Jul 12, 2019
73a9685
Fix test of `chx.reshape`
toslunar Jul 12, 2019
eabb66e
Merge pull request #7731 from niboshi/reporter-multithread
mergify[bot] Jul 12, 2019
e5f31fd
Merge pull request #7728 from asi1024/noncontiguous-test-slice
mergify[bot] Jul 14, 2019
e33cc03
Merge pull request #7745 from asi1024/flaky-contrastive
Jul 15, 2019
0d9fba9
Do not use 'else' after 'return'
IvanYashchuk Jul 15, 2019
d44b308
Use int64_t instead of int for shapes and dims
IvanYashchuk Jul 15, 2019
a0764fc
Use auto with static_cast
IvanYashchuk Jul 15, 2019
a83f22a
Modified parametrization of tests
IvanYashchuk Jul 15, 2019
9bfe757
Merge pull request #1 from toslunar/weight_standardization
hitsgub Jul 15, 2019
588cad5
Merge pull request #7752 from niboshi/cuda-not-found
Jul 16, 2019
dd19549
Fix style
toslunar Jul 16, 2019
564a647
warn users for Python 2
kmaehashi Jul 16, 2019
ed0e521
add upgrade guide
kmaehashi Jul 16, 2019
f04d13a
add note to installation guide
kmaehashi Jul 16, 2019
7d4c843
Enhanced error message
asi1024 Jul 16, 2019
f03d5ba
Refactor dummy links in optimizer_hooks_tests
niboshi Jul 12, 2019
a0350b6
Fix tolerance
toslunar Jul 16, 2019
713483c
Do not repeat accepted dtype errors
IvanYashchuk Jul 16, 2019
65ad96e
fix "zero division" in resize image
meokz Jul 16, 2019
0491eac
Add chainer.functions.rnn into packages
Jul 16, 2019
ee78ff1
Merge pull request #6678 from hitsgub/weight_standardization
toslunar Jul 16, 2019
5318742
Avoid to conflict names of test files
himkt Jul 16, 2019
5677ed4
Replace links.connection with links.rnn
himkt Jul 16, 2019
e0c6303
Merge pull request #6414 from niboshi/type-check-bool
mergify[bot] Jul 16, 2019
5547824
Merge pull request #6237 from knorth55/fix-roi-max-pooling-2d
mitmul Jul 17, 2019
54c631f
Revert "Allow bool evaluation in `type_check`"
mitmul Jul 17, 2019
26c7b20
Merge pull request #7766 from kmaehashi/drop-py2
niboshi Jul 17, 2019
6eb84e3
Merge pull request #7772 from chainer/revert-6414-type-check-bool
niboshi Jul 17, 2019
746d2d0
Added a comment for Travis CI steps why LAPACK is installed only for …
IvanYashchuk Jul 17, 2019
2f23791
Removed unnecessary dynamic cast
IvanYashchuk Jul 17, 2019
40f2549
Fixed typo
IvanYashchuk Jul 17, 2019
2af8b55
Add separate failing tests for fp16, parametrize suported dtype using…
IvanYashchuk Jul 17, 2019
b7d869a
Add a note to docs about supported dtypes
IvanYashchuk Jul 17, 2019
6d2cbab
Fix flake8 errors
niboshi Jul 18, 2019
40f2aef
Fix imports
niboshi Jul 18, 2019
c06e421
Add Mixed16 tests to multi-node chain List
belldandyxtq Jun 28, 2019
d451cd8
Bump version numbers to v7.0.0b2
asi1024 Jul 18, 2019
f676508
Add mixed16 tests to point_to_point communications
belldandyxtq Jun 28, 2019
aa947d1
Merge pull request #7776 from chainer/v7.0.0b2
hvy Jul 18, 2019
9184bfd
Fixed wrong typecasting
Jul 18, 2019
6bac92f
Revert "Warn users for Python 2"
kmaehashi Jul 18, 2019
815e829
Skip dtype failing tests when lapack is not available
IvanYashchuk Jul 18, 2019
bdda9d6
use cuDNN by default
crcrpar Jun 5, 2019
ee67bb2
change should_use_cudnn to >=auto
crcrpar Jul 18, 2019
7c38f19
Merge pull request #7778 from chainer/revert-7766-drop-py2
niboshi Jul 18, 2019
2a443f3
Do not hack
toslunar Jul 18, 2019
e651568
Removed print
Jul 18, 2019
721986e
Merge pull request #7725 from himkt/unify-rnn
Jul 18, 2019
1eed627
revert changes to optimizer tests
Jul 18, 2019
d734995
Merge pull request #7762 from toslunar/fix-test-chx-reshape-copied
mergify[bot] Jul 18, 2019
ba13fd1
Remove unused variable
asi1024 Jul 18, 2019
b4e65e1
Enhanced error message
asi1024 Jul 18, 2019
227778f
Add TODO
aksub99 Jul 18, 2019
02d1f0c
Merge pull request #7694 from emcastillo/fix_adam_test
niboshi Jul 19, 2019
493a768
Merge pull request #7558 from aksub99/Add_cumsum
asi1024 Jul 19, 2019
586e76e
Merge pull request #7327 from kuenishi/strict-scatter
keisukefukuda Jul 19, 2019
f8be8c7
Merge pull request #7760 from niboshi/refactor-optimizer-hook-test
Jul 19, 2019
400c07d
Merge pull request #7600 from shu65/opt
keisukefukuda Jul 19, 2019
8c5ffba
Merge pull request #7630 from belldandyxtq/mixed16_tests_for_multi_no…
kuenishi Jul 19, 2019
728d941
Merge pull request #7474 from IvanYashchuk/ivan-add-solve
hvy Jul 19, 2019
8088b08
Merge pull request #7758 from toslunar/fix-average-overflow
Jul 20, 2019
8a6116a
Merge pull request #7769 from meokz/fix-image-resize
takagi Jul 22, 2019
457a27a
Merge pull request #7637 from belldandyxtq/mixed16_tests_for_p2p
kuenishi Jul 22, 2019
3b03f9a
Merge pull request #7185 from crcrpar/use-cudnn-dropout
niboshi Jul 22, 2019
7df046a
Merge pull request #6835 from ishanrai05/bug-1
mergify[bot] Jul 22, 2019
8a04a86
Merge pull request #7738 from asi1024/check-build-info
mergify[bot] Jul 23, 2019
c9fb6ba
update test
Hakuyume Jul 23, 2019
a5ea170
add example
Hakuyume Jul 23, 2019
bb9a999
Merge pull request #7361 from Hakuyume/tabular-from-data
beam2d Jul 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added chainer.grad features to chainerx.grad
  • Loading branch information
Emilio Castillo committed Jul 2, 2019
commit dc1dbf3e98ea2ec26cc9cd6c3f3ffef463078385
43 changes: 20 additions & 23 deletions chainer/function_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,39 1011,36 @@ def grad(outputs, inputs, grad_outputs=None, grad_inputs=None, set_grad=False,

# Check if all the inputs are chainerx arrays and if so
# Relies in chainerx.grad function
chx_inputs = sum(map(lambda x: x._has_chainerx_array
if x is not None else False, inputs))
if chx_inputs == len(inputs):
# Need to access the arrays to invoke the chainer grad function
if grad_inputs is not None:
grad_inputs_chx = list(map(lambda x: x._data[0], grad_inputs))
else:
grad_inputs_chx = []
if grad_outputs is not None:
grad_outs_chx = list(map(lambda x: x._data[0], grad_outputs))
else:
grad_outs_chx = []
if retain_grad:
raise ValueError(
'retain_grad is not supported on chainerx.grad interface')
n_chx_inputs = sum([False if x is None else x._has_chainerx_array
for x in inputs])
if n_chx_inputs == len(inputs):
if loss_scale is not None:
raise ValueError(
'loss_scale is not supported on chainerx.grad interface')

outputs_chx = list(map(lambda x: x._data[0], outputs))
inputs_chx = list(map(lambda x: x._data[0], inputs))
# Need to access the arrays to invoke the chainer grad function
if grad_inputs:
grad_inputs_chx = [x._data[0] for x in grad_inputs]
else:
grad_inputs_chx = []
if grad_outputs:
grad_outputs_chx = [x._data[0] for x in grad_outputs]
else:
grad_outputs_chx = []
outputs_chx = [x._data[0] for x in outputs]
inputs_chx = [x._data[0] for x in inputs]
grads = chainerx.grad(outputs_chx, inputs_chx,
backprop_id=None,
enable_double_backprop=enable_double_backprop,
set_grad=set_grad,
retain_grad=retain_grad,
grad_inputs=grad_inputs_chx,
grad_outputs=grad_outs_chx)
ret_vars = [variable.Variable(g, requires_grad=False) for g in grads]
if set_grad:
for x, g in zip(inputs, ret_vars):
x.grad_var = g
grad_outputs=grad_outputs_chx)
ret_vars = [variable.Variable(g,
requires_grad=g.is_backprop_required())
for g in grads]
return ret_vars
elif chx_inputs > 0:
elif n_chx_inputs > 0:
raise TypeError(
'Mixing chainerx and non-chainerx variables is not allowed')

Expand Down
40 changes: 26 additions & 14 deletions chainerx_cc/chainerx/backward.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 207,14 @@ class BackwardImpl {
const std::vector<ConstArrayRef>& outputs,
const BackpropId& backprop_id,
DoubleBackpropOption double_backprop,
std::unordered_map<ArrayNode*, internal::GradRef> array_node_grad_map)
std::unordered_map<ArrayNode*, internal::GradRef> array_node_grad_map,
bool retain_grad = false)
: inputs_{inputs},
outputs_{outputs},
backprop_id_{backprop_id},
double_backprop_{double_backprop},
array_node_grad_map_{std::move(array_node_grad_map)} {
array_node_grad_map_{std::move(array_node_grad_map)},
retain_grad_{retain_grad} {
if (!outputs_.empty()) {
// Collect output array nodes (can be nullptr).
output_array_nodes_.reserve(outputs.size());
Expand Down Expand Up @@ -396,7 398,11 @@ class BackwardImpl {
if (output_array_node != nullptr) {
std::shared_ptr<ArrayBody> body = output_array_node->weak_body().lock();
if (body != nullptr && !body->IsGradRequired(backprop_id_)) {
body->ClearGrad(backprop_id_);
if (retain_grad_) {
internal::ArrayBody::RequireGrad(body, backprop_id_);
} else {
body->ClearGrad(backprop_id_);
}
}
}
}
Expand Down Expand Up @@ -567,6 573,9 @@ class BackwardImpl {
// Represents the subgraph required for backprop in case any inputs are specified.
// Specifically, stores for an op nodes a vector of boolean flags whether an input at that index is included in the subgraph.
std::unordered_map<OpNode*, std::vector<uint8_t>> input_required_flags_;

// To mark if grads of intermediate nodes in the graph should be kept
bool retain_grad_;
};

} // namespace
Expand All @@ -586,56 595,59 @@ void Backward(
BackwardImpl{{}, outputs, actual_backprop_id, double_backprop}.Run();
}


std::vector<nonstd::optional<Array>> Grad(
const std::vector<ConstArrayRef>& outputs,
const std::vector<ConstArrayRef>& inputs,
const nonstd::optional<BackpropId>& backprop_id,
DoubleBackpropOption double_backprop,
bool set_grad,
bool retain_grad,
const std::vector<ConstArrayRef>& grad_inputs,
const std::vector<ConstArrayRef>& grad_outputs){

const std::vector<ConstArrayRef>& grad_outputs) {
if (inputs.empty()) {
return {};
}
if (outputs.empty()) {
return std::vector<nonstd::optional<Array>>(inputs.size(), nonstd::nullopt);
}

CHAINERX_ASSERT(!grad_inputs.size() || (inputs.size() == grad_inputs.size()));
CHAINERX_ASSERT(!grad_outputs.size() || (outputs.size() == grad_outputs.size()));
if (!grad_inputs.empty() && inputs.size() != grad_inputs.size()) {
throw GradientError{"Grad inputs and inputs must have the same size"};
}
if (!grad_outputs.empty() && outputs.size() != grad_outputs.size()) {
throw GradientError{"Grad_outputs and outputs must have the same size"};
}

std::vector<nonstd::optional<Array>> input_grads;
std::vector<nonstd::optional<Array>> output_grads;
input_grads.reserve(inputs.size());
output_grads.reserve(outputs.size());

BackpropId actual_backprop_id = internal::GetArrayBackpropId(outputs.front().get(), backprop_id);

// Initialize the grad map with newly created gradient arrays of the inputs.
// The existing gradients of the inputs are thus not modified.
std::unordered_map<ArrayNode*, internal::GradRef> array_node_grad_map;
for (size_t i=0; i<inputs.size();i ) {
for (size_t i = 0; i < inputs.size(); i ) {
const std::shared_ptr<ArrayBody>& array_body = internal::GetArrayBody(inputs[i]);
if (const std::shared_ptr<ArrayNode>& input_array_node = array_body->GetArrayNode(actual_backprop_id)) {
input_grads.emplace_back(grad_inputs.size()? nonstd::optional<Array>{grad_inputs[i]} : nonstd::optional<Array>{});
input_grads.emplace_back(!grad_inputs.empty() ? nonstd::optional<Array>{grad_inputs[i]} : nonstd::optional<Array>{});
array_node_grad_map.emplace(input_array_node.get(), internal::GradRef{&input_grads.back()});
} else {
input_grads.emplace_back(nonstd::nullopt);
}
}

// Push initial output grads, Run assigns to 1 in other case
for (size_t i=0; i<grad_outputs.size();i ) {
// Push initial output grads, Run assigns to 1 in other case
for (size_t i = 0; i < grad_outputs.size(); i ) {
const std::shared_ptr<ArrayBody>& array_body = internal::GetArrayBody(outputs[i]);
if (const std::shared_ptr<ArrayNode>& array_node = array_body->GetArrayNode(actual_backprop_id)) {
output_grads.emplace_back(grad_outputs[i]);
array_node_grad_map.emplace(array_node.get(), internal::GradRef{&output_grads.back()});
}
}

BackwardImpl{inputs, outputs, actual_backprop_id, double_backprop, std::move(array_node_grad_map)}.Run();
BackwardImpl{inputs, outputs, actual_backprop_id, double_backprop, std::move(array_node_grad_map), retain_grad}.Run();

size_t i = 0;
// input_grads may contain unmodified array bodies (nullptr) for arrays that are not included in the graph.
Expand Down
1 change: 1 addition & 0 deletions chainerx_cc/chainerx/backward.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 51,7 @@ std::vector<nonstd::optional<Array>> Grad(
const nonstd::optional<BackpropId>& backprop_id = nonstd::nullopt,
DoubleBackpropOption double_backprop = DoubleBackpropOption::kDisable,
bool set_grad = false,
bool retain_grad = false,
const std::vector<ConstArrayRef>& grad_inputs = {},
const std::vector<ConstArrayRef>& grad_outputs = {});

Expand Down
5 changes: 4 additions & 1 deletion chainerx_cc/chainerx/python/backward.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 64,9 @@ void InitChainerxBackward(pybind11::module& m) {
const nonstd::optional<BackpropId>& backprop_id,
bool enable_double_backprop,
bool set_grad,
bool retain_grad,
const std::vector<ArrayBodyPtr>& grad_inputs,
const std::vector<ArrayBodyPtr>& grad_outputs){
const std::vector<ArrayBodyPtr>& grad_outputs) {
std::vector<Array> output_arrays = ConvertToArrays(outputs);
std::vector<Array> input_arrays = ConvertToArrays(inputs);

Expand All @@ -79,6 80,7 @@ void InitChainerxBackward(pybind11::module& m) {
backprop_id,
double_backprop,
set_grad,
retain_grad,
std::vector<ConstArrayRef>{grad_input_arrays.begin(), grad_input_arrays.end()},
std::vector<ConstArrayRef>{grad_output_arrays.begin(), grad_output_arrays.end()});
return internal::MoveArrayBodies(std::move(grads));
Expand All @@ -88,6 90,7 @@ void InitChainerxBackward(pybind11::module& m) {
"backprop_id"_a = nullptr,
"enable_double_backprop"_a = false,
"set_grad"_a = false,
"retain_grad"_a = false,
"grad_inputs"_a = std::vector<ArrayBodyPtr>{},
"grad_outputs"_a = std::vector<ArrayBodyPtr>{});
}
Expand Down
44 changes: 42 additions & 2 deletions tests/chainer_tests/test_function_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 883,6 @@ def _init_attrs(self, names):
for name in names:
v = chainer.Variable(
numpy.random.randint(-4, 6, self.shape).astype('f'), name=name)
if self.extend_graph_x:
v *= 1.
ret.append(v)
setattr(self, name, v)
return ret
Expand Down Expand Up @@ -920,6 918,11 @@ def use_gpu(self):
if isinstance(value, chainer.Variable):
value.to_gpu()

def use_chainerx(self, device='native:1'):
for value in six.itervalues(self.__dict__):
if isinstance(value, chainer.Variable):
value.to_device(backend.get_device(device))

def forward(self):
raise NotImplementedError

Expand All @@ -943,6 946,13 @@ def check_grad(self):
ys = [getattr(self, name) for name in self.y_names]
if self.extend_graph_y:
self._ys = [v * 1. for v in ys]

# graph_x extension should be done here
# to avoid chainer/chainerx mixed graph
if self.extend_graph_x:
for v in self.xs:
v *= 1.

gxs = chainer.grad(ys, self.xs, self.gys, self.gxs,
loss_scale=self.loss_scale)

Expand All @@ -960,14 970,29 @@ def check_grad(self):
self._print_variables('gxs (expected)', expected)
raise

def chainerx_grad(self, device):
if self.loss_scale:
pytest.skip('Unsupported configurations')
self.use_chainerx(device)
self.check_grad()

def test_grad_cpu(self):
self.check_grad()

@attr.chainerx
def test_grad_chainerx_cpu(self):
self.chainerx_grad('native:1')

@attr.gpu
def test_grad_gpu(self):
self.use_gpu()
self.check_grad()

@attr.chainerx
@attr.gpu
def test_grad_chainerx_gpu(self):
self.chainerx_grad('cuda:0')

def check_double_grad(self):
self.forward()
ys = [getattr(self, name) for name in self.y_names]
Expand All @@ -992,11 1017,26 @@ def check_double_grad(self):
def test_double_grad_cpu(self):
self.check_double_grad()

def double_chainerx_grad(self, device):
if self.loss_scale:
pytest.skip('Unsupported configurations')
self.use_chainerx(device)
self.check_double_grad()

@attr.chainerx
def test_double_grad_chainerx_cpu(self):
self.double_chainerx_grad('native:1')

@attr.gpu
def test_double_grad_gpu(self):
self.use_gpu()
self.check_double_grad()

@attr.chainerx
@attr.gpu
def test_double_grad_chainerx_gpu(self):
self.double_chainerx_grad('cuda:0')


@testing.parameterize(*testing.product({
'loss_scale': [None, 1, 10],
Expand Down
Loading