2019-07-30 17:45:13 +03:00
|
|
|
# Copyright (c) Facebook, Inc. and its affiliates.
|
2017-10-11 17:14:19 +03:00
|
|
|
#
|
2019-07-30 17:45:13 +03:00
|
|
|
# This source code is licensed under the MIT license found in the
|
|
|
|
# LICENSE file in the root directory of this source tree.
|
2017-10-11 17:14:19 +03:00
|
|
|
|
|
|
|
import unittest
|
2020-10-19 04:13:29 +03:00
|
|
|
|
|
|
|
import torch
|
2017-10-11 17:14:19 +03:00
|
|
|
import torch.nn as nn
|
2020-10-19 04:13:29 +03:00
|
|
|
from fairseq.modules import ConvTBC
|
2017-10-11 17:14:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TestConvTBC(unittest.TestCase):
|
|
|
|
def test_convtbc(self):
|
|
|
|
# ksz, in_channels, out_channels
|
|
|
|
conv_tbc = ConvTBC(4, 5, kernel_size=3, padding=1)
|
|
|
|
# out_channels, in_channels, ksz
|
|
|
|
conv1d = nn.Conv1d(4, 5, kernel_size=3, padding=1)
|
|
|
|
|
|
|
|
conv_tbc.weight.data.copy_(conv1d.weight.data.transpose(0, 2))
|
|
|
|
conv_tbc.bias.data.copy_(conv1d.bias.data)
|
|
|
|
|
2018-06-25 19:23:04 +03:00
|
|
|
input_tbc = torch.randn(7, 2, 4, requires_grad=True)
|
|
|
|
input1d = input_tbc.data.transpose(0, 1).transpose(1, 2)
|
|
|
|
input1d.requires_grad = True
|
2017-10-11 17:14:19 +03:00
|
|
|
|
|
|
|
output_tbc = conv_tbc(input_tbc)
|
|
|
|
output1d = conv1d(input1d)
|
|
|
|
|
2020-10-19 04:13:29 +03:00
|
|
|
self.assertAlmostEqual(
|
|
|
|
output_tbc.data.transpose(0, 1).transpose(1, 2), output1d.data
|
|
|
|
)
|
2017-10-25 22:14:11 +03:00
|
|
|
|
2017-10-11 17:14:19 +03:00
|
|
|
grad_tbc = torch.randn(output_tbc.size())
|
|
|
|
grad1d = grad_tbc.transpose(0, 1).transpose(1, 2).contiguous()
|
|
|
|
|
|
|
|
output_tbc.backward(grad_tbc)
|
|
|
|
output1d.backward(grad1d)
|
|
|
|
|
2020-10-19 04:13:29 +03:00
|
|
|
self.assertAlmostEqual(
|
|
|
|
conv_tbc.weight.grad.data.transpose(0, 2), conv1d.weight.grad.data
|
|
|
|
)
|
2017-10-11 17:14:19 +03:00
|
|
|
self.assertAlmostEqual(conv_tbc.bias.grad.data, conv1d.bias.grad.data)
|
2020-10-19 04:13:29 +03:00
|
|
|
self.assertAlmostEqual(
|
|
|
|
input_tbc.grad.data.transpose(0, 1).transpose(1, 2), input1d.grad.data
|
|
|
|
)
|
2017-10-11 17:14:19 +03:00
|
|
|
|
|
|
|
def assertAlmostEqual(self, t1, t2):
|
|
|
|
self.assertEqual(t1.size(), t2.size(), "size mismatch")
|
|
|
|
self.assertLess((t1 - t2).abs().max(), 1e-4)
|
|
|
|
|
|
|
|
|
2020-10-19 04:13:29 +03:00
|
|
|
if __name__ == "__main__":
|
2017-10-11 17:14:19 +03:00
|
|
|
unittest.main()
|