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

Add nllloss backward #5210

Merged
merged 50 commits into from
Jun 16, 2021
Merged
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
7af6ddb
add argmax test
BBuf May 25, 2021
b354d19
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf May 25, 2021
32200ae
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf May 25, 2021
cbccc2f
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf May 26, 2021
871897a
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf May 27, 2021
c293851
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf May 27, 2021
e8ca65b
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf May 27, 2021
09eaaef
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf May 28, 2021
40f5639
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf May 28, 2021
9e4ca51
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf May 28, 2021
39e6d18
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf May 29, 2021
7e99d9a
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf May 31, 2021
17d2dbe
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf May 31, 2021
e57fe0d
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf May 31, 2021
c3d2b8b
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf May 31, 2021
c480351
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 1, 2021
57dfa4f
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 2, 2021
13aae88
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 2, 2021
ce4e87c
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 2, 2021
6b9f771
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 3, 2021
bf9b9db
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 3, 2021
72a1d52
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 4, 2021
2550d8a
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 4, 2021
47f3591
fix ci error
BBuf Jun 4, 2021
f9ab3d1
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 6, 2021
69d8053
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 6, 2021
bb4da0a
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 6, 2021
6a6cb05
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 7, 2021
4c535c8
fix docstring warning
BBuf Jun 7, 2021
f2c8da7
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 7, 2021
aeb55f1
fix tensor greater and less bug
BBuf Jun 7, 2021
f16eb2b
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 8, 2021
1cbb8de
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 9, 2021
c7b50e4
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 9, 2021
ac39c72
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 10, 2021
50923e7
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 11, 2021
e765c35
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 11, 2021
ed53533
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 11, 2021
6e8b1aa
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 12, 2021
70c2e08
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 15, 2021
fa279db
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 15, 2021
e8ae905
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow
BBuf Jun 16, 2021
a589268
add nllloss backward test
BBuf Jun 16, 2021
4e597fa
add nllloss backward test
BBuf Jun 16, 2021
3929f42
Merge branch 'master' into add_nllloss_backward
BBuf Jun 16, 2021
5a21df6
Merge branch 'master' into add_nllloss_backward
oneflow-ci-bot Jun 16, 2021
3224fcc
Merge branch 'master' into add_nllloss_backward
oneflow-ci-bot Jun 16, 2021
5609c33
Merge branch 'master' into add_nllloss_backward
oneflow-ci-bot Jun 16, 2021
e6670e4
Merge branch 'master' into add_nllloss_backward
oneflow-ci-bot Jun 16, 2021
60a66fc
Merge branch 'master' into add_nllloss_backward
oneflow-ci-bot Jun 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
141 changes: 108 additions & 33 deletions oneflow/python/test/modules/test_nllloss_grad.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ def _test_nllloss_none_backward(test_case, device):
of_out = nll_loss(input, target)
of_out = of_out.sum()
of_out.backward()
print(input.grad.numpy())
np_grad = [
[-0.20000000298023224, 0.0, 0.0],
[0.0, 0.0, -0.20000000298023224],
[0.0, -0.20000000298023224, 0.0],
[0.0, -0.20000000298023224, 0.0],
[-0.20000000298023224, 0.0, 0.0],
]
test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, atol=1e-5, rtol=1e-5))


def _test_nllloss_mean(test_case, device):
Expand All @@ -59,14 +66,26 @@ def _test_nllloss_mean(test_case, device):
]
).astype(np.float32)
y = np.array([0, 2, 1, 1, 0]).astype(np.int)
input = flow.Tensor(x, dtype=flow.float32, device=flow.device(device))
input = flow.Tensor(
x, dtype=flow.float32, device=flow.device(device), requires_grad=True
)

target = flow.Tensor(y, dtype=flow.int64, device=flow.device(device))
target = flow.Tensor(
y, dtype=flow.int64, device=flow.device(device), requires_grad=True
)
nll_loss = flow.nn.NLLLoss(reduction="mean")
nll_loss = nll_loss.to(device)
of_out = nll_loss(input, target)
np_out = nll_loss_1d(input.numpy(), target.numpy(), reduction="mean")
test_case.assertTrue(np.allclose(of_out.numpy(), np_out))
of_out = of_out.sum()
of_out.backward()
np_grad = [
[-0.20000000298023224, 0.0, 0.0],
[0.0, 0.0, -0.20000000298023224],
[0.0, -0.20000000298023224, 0.0],
[0.0, -0.20000000298023224, 0.0],
[-0.20000000298023224, 0.0, 0.0],
]
test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, atol=1e-5, rtol=1e-5))


def _test_nllloss_sum(test_case, device):
Expand All @@ -80,98 +99,146 @@ def _test_nllloss_sum(test_case, device):
]
).astype(np.float32)
y = np.array([0, 2, 1, 1, 0]).astype(np.int)
input = flow.Tensor(x, dtype=flow.float32, device=flow.device(device))
input = flow.Tensor(
x, dtype=flow.float32, device=flow.device(device), requires_grad=True
)

target = flow.Tensor(y, dtype=flow.int64, device=flow.device(device))
target = flow.Tensor(
y, dtype=flow.int64, device=flow.device(device), requires_grad=True
)
nll_loss = flow.nn.NLLLoss(reduction="sum")
nll_loss = nll_loss.to(device)
of_out = nll_loss(input, target)
np_out = nll_loss_1d(input.numpy(), target.numpy(), reduction="sum")
test_case.assertTrue(np.allclose(of_out.numpy(), np_out))
of_out = of_out.sum()
of_out.backward()
np_grad = [
[-1.0, 0.0, 0.0],
[0.0, 0.0, -1.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[-1.0, 0.0, 0.0],
]
test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, atol=1e-5, rtol=1e-5))


def _test_nllloss_segmentation_none(test_case, device):
x = np.array(
[[[[0.12, 0.36], [0.22, 0.66]], [[0.13, 0.34], [0.52, -0.96]]]]
).astype(np.float32)
input = flow.Tensor(x, dtype=flow.float32, device=flow.device(device))
input = flow.Tensor(
x, dtype=flow.float32, device=flow.device(device), requires_grad=True
)
y = np.array([[[1, 0], [0, 1]]]).astype(np.int)
target = flow.Tensor(y, dtype=flow.int64, device=flow.device(device))
target = flow.Tensor(
y, dtype=flow.int64, device=flow.device(device), requires_grad=True
)
nll_loss = flow.nn.NLLLoss()
nll_loss = nll_loss.to(device)
of_out = nll_loss(input, target)
np_out = nll_loss_2d(input.numpy(), target.numpy())
test_case.assertTrue(np.allclose(of_out.numpy(), np_out))
of_out = of_out.sum()
of_out.backward()
np_grad = [[[[0.0, -0.25], [-0.25, 0.0]], [[-0.25, 0.0], [0.0, -0.25]]]]
test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, atol=1e-5, rtol=1e-5))


def _test_nllloss_segmentation_mean(test_case, device):
x = np.array(
[[[[0.12, 0.36], [0.22, 0.66]], [[0.13, 0.34], [0.52, -0.96]]]]
).astype(np.float32)
input = flow.Tensor(x, dtype=flow.float32, device=flow.device(device))
input = flow.Tensor(
x, dtype=flow.float32, device=flow.device(device), requires_grad=True
)
y = np.array([[[1, 0], [0, 1]]]).astype(np.int)
target = flow.Tensor(y, dtype=flow.int64, device=flow.device(device))
target = flow.Tensor(
y, dtype=flow.int64, device=flow.device(device), requires_grad=True
)
nll_loss = flow.nn.NLLLoss(reduction="mean")
nll_loss = nll_loss.to(device)
of_out = nll_loss(input, target)
np_out = nll_loss_2d(input.numpy(), target.numpy(), reduction="mean")
test_case.assertTrue(np.allclose(of_out.numpy(), np_out))
of_out = of_out.sum()
of_out.backward()
np_grad = [[[[0.0, -0.25], [-0.25, 0.0]], [[-0.25, 0.0], [0.0, -0.25]]]]
test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, atol=1e-5, rtol=1e-5))


def _test_nllloss_segmentation_sum(test_case, device):
x = np.array(
[[[[0.12, 0.36], [0.22, 0.66]], [[0.13, 0.34], [0.52, -0.96]]]]
).astype(np.float32)
input = flow.Tensor(x, dtype=flow.float32, device=flow.device(device))
input = flow.Tensor(
x, dtype=flow.float32, device=flow.device(device), requires_grad=True
)
y = np.array([[[1, 0], [0, 1]]]).astype(np.int)
target = flow.Tensor(y, dtype=flow.int64, device=flow.device(device))
target = flow.Tensor(
y, dtype=flow.int64, device=flow.device(device), requires_grad=True
)
nll_loss = flow.nn.NLLLoss(reduction="sum")
nll_loss = nll_loss.to(device)
of_out = nll_loss(input, target)
np_out = nll_loss_2d(input.numpy(), target.numpy(), reduction="sum")
test_case.assertTrue(np.allclose(of_out.numpy(), np_out))
of_out = of_out.sum()
of_out.backward()
np_grad = [[[[0.0, -1.0], [-1.0, 0.0]], [[-1.0, 0.0], [0.0, -1.0]]]]
test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, atol=1e-5, rtol=1e-5))


def _test_nllloss_bert_none(test_case, device):
x = np.array([[[0.12, 0.36, 0.22, 0.66], [0.13, 0.34, 0.52, -0.96]]]).astype(
np.float32
)
input = flow.Tensor(x, dtype=flow.float32, device=flow.device(device))
input = flow.Tensor(
x, dtype=flow.float32, device=flow.device(device), requires_grad=True
)
y = np.array([[1, 0, 0, 1]]).astype(np.int)
target = flow.Tensor(y, dtype=flow.int64, device=flow.device(device))
target = flow.Tensor(
y, dtype=flow.int64, device=flow.device(device), requires_grad=True
)
nll_loss = flow.nn.NLLLoss()
nll_loss = nll_loss.to(device)
of_out = nll_loss(input, target)
np_out = nll_loss_bert(input.numpy(), target.numpy())
test_case.assertTrue(np.allclose(of_out.numpy(), np_out))
of_out = of_out.sum()
of_out.backward()
np_grad = [[[0.0, -0.25, -0.25, 0.0], [-0.25, 0.0, 0.0, -0.25]]]
test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, atol=1e-5, rtol=1e-5))


def _test_nllloss_bert_mean(test_case, device):
x = np.array([[[0.12, 0.36, 0.22, 0.66], [0.13, 0.34, 0.52, -0.96]]]).astype(
np.float32
)
input = flow.Tensor(x, dtype=flow.float32, device=flow.device(device))
input = flow.Tensor(
x, dtype=flow.float32, device=flow.device(device), requires_grad=True
)
y = np.array([[1, 0, 0, 1]]).astype(np.int)
target = flow.Tensor(y, dtype=flow.int64, device=flow.device(device))
target = flow.Tensor(
y, dtype=flow.int64, device=flow.device(device), requires_grad=True
)
nll_loss = flow.nn.NLLLoss(reduction="mean")
nll_loss = nll_loss.to(device)
of_out = nll_loss(input, target)
np_out = nll_loss_bert(input.numpy(), target.numpy(), reduction="mean")
test_case.assertTrue(np.allclose(of_out.numpy(), np_out))
of_out = of_out.sum()
of_out.backward()
np_grad = [[[0.0, -0.25, -0.25, 0.0], [-0.25, 0.0, 0.0, -0.25]]]
test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, atol=1e-5, rtol=1e-5))


def _test_nllloss_bert_sum(test_case, device):
x = np.array([[[0.12, 0.36, 0.22, 0.66], [0.13, 0.34, 0.52, -0.96]]]).astype(
np.float32
)
input = flow.Tensor(x, dtype=flow.float32, device=flow.device(device))
input = flow.Tensor(
x, dtype=flow.float32, device=flow.device(device), requires_grad=True
)
y = np.array([[1, 0, 0, 1]]).astype(np.int)
target = flow.Tensor(y, dtype=flow.int64, device=flow.device(device))
target = flow.Tensor(
y, dtype=flow.int64, device=flow.device(device), requires_grad=True
)
nll_loss = flow.nn.NLLLoss(reduction="sum")
nll_loss = nll_loss.to(device)
of_out = nll_loss(input, target)
np_out = nll_loss_bert(input.numpy(), target.numpy(), reduction="sum")
test_case.assertTrue(np.allclose(of_out.numpy(), np_out))
of_out = of_out.sum()
of_out.backward()
np_grad = [[[0.0, -1.0, -1.0, 0.0], [-1.0, 0.0, 0.0, -1.0]]]
test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, atol=1e-5, rtol=1e-5))


@unittest.skipIf(
Expand All @@ -183,6 +250,14 @@ def test_nllloss(test_case):
arg_dict = OrderedDict()
arg_dict["test_fun"] = [
_test_nllloss_none_backward,
_test_nllloss_mean,
_test_nllloss_sum,
_test_nllloss_segmentation_none,
_test_nllloss_segmentation_mean,
_test_nllloss_segmentation_sum,
_test_nllloss_bert_none,
_test_nllloss_bert_mean,
_test_nllloss_bert_sum,
]
arg_dict["device"] = ["cpu", "cuda"]
for arg in GenArgList(arg_dict):
Expand Down