Exemplo n.º 1
0
    def test_acceptor_from_tensor(self):
        fsa_tensor = torch.tensor(
            [[0, 1, 2, _k2._float_as_int(-1.2)],
             [0, 2, 10, _k2._float_as_int(-2.2)],
             [1, 6, -1, _k2._float_as_int(-3.2)],
             [1, 3, 3, _k2._float_as_int(-4.2)],
             [2, 6, -1, _k2._float_as_int(-5.2)],
             [2, 4, 2, _k2._float_as_int(-6.2)],
             [3, 6, -1, _k2._float_as_int(-7.2)],
             [5, 0, 1, _k2._float_as_int(-8.2)]],
            dtype=torch.int32)

        fsa = k2.Fsa(fsa_tensor)

        expected_str = '''
            0 1 2 -1.2
            0 2 10 -2.2
            1 6 -1 -3.2
            1 3 3 -4.2
            2 6 -1 -5.2
            2 4 2 -6.2
            3 6 -1 -7.2
            5 0 1 -8.2
            6
        '''
        assert _remove_leading_spaces(expected_str) == _remove_leading_spaces(
            fsa.to_str())

        arcs = fsa.arcs
        assert isinstance(arcs, torch.Tensor)
        assert arcs.dtype == torch.int32
        assert arcs.device.type == 'cpu'
        assert arcs.shape == (8, 3), 'there should be 8 arcs'
        assert torch.allclose(arcs[0],
                              torch.tensor([0, 1, 2], dtype=torch.int32))

        assert torch.allclose(
            fsa.weights,
            torch.tensor([-1.2, -2.2, -3.2, -4.2, -5.2, -6.2, -7.2, -8.2],
                         dtype=torch.float32))

        fsa = fsa.to('cuda')
        arcs[0][0] += 10
        assert arcs[0][0] == 10, 'arcs should still be accessible'

        arcs = fsa.arcs
        assert arcs.dtype == torch.int32
        assert arcs.device.type == 'cuda'
        assert arcs.device.index == 0
        assert arcs.shape == (8, 3), 'there should be 8 arcs'
        assert torch.allclose(
            arcs[1],
            torch.tensor([0, 2, 10], dtype=torch.int32, device=arcs.device))
Exemplo n.º 2
0
    def test_cpu_arc_array1_from_tensor(self):
        gt_tensor = torch.tensor(
            [[1, 2, 3, _k2._float_as_int(1.5)],
             [10, 20, 30, _k2._float_as_int(-2.5)]],
            dtype=torch.int32).contiguous()
        arc_array = k2.Array(gt_tensor, True)

        arc0 = arc_array.data.get(0)
        assert arc0.src_state == 1
        assert arc0.dest_state == 2
        assert arc0.symbol == 3
        assert arc0.score == 1.5

        actual_tensor = arc_array.tensor()
        assert torch.allclose(gt_tensor, actual_tensor)
        gt_tensor[0] += 10  # also change actual_tensor

        assert torch.allclose(gt_tensor, actual_tensor)
        assert arc_array.data.get(0).src_state == 11
        del gt_tensor, actual_tensor

        # arc_array is still accessible
        assert arc_array.data.get(0).src_state == 11
Exemplo n.º 3
0
    def test_acceptor_from_tensor(self):
        fsa_tensor = torch.tensor(
            [[0, 1, 2, _k2._float_as_int(-1.2)],
             [0, 2, 10, _k2._float_as_int(-2.2)],
             [1, 6, -1, _k2._float_as_int(-3.2)],
             [1, 3, 3, _k2._float_as_int(-4.2)],
             [2, 6, -1, _k2._float_as_int(-5.2)],
             [2, 4, 2, _k2._float_as_int(-6.2)],
             [3, 6, -1, _k2._float_as_int(-7.2)],
             [5, 0, 1, _k2._float_as_int(-8.2)]],
            dtype=torch.int32)

        fsa = k2.Fsa(fsa_tensor)

        expected_str = '''
            0 1 2 -1.2
            0 2 10 -2.2
            1 6 -1 -3.2
            1 3 3 -4.2
            2 6 -1 -5.2
            2 4 2 -6.2
            3 6 -1 -7.2
            5 0 1 -8.2
            6
        '''
        assert _remove_leading_spaces(expected_str) == _remove_leading_spaces(
            k2.to_str(fsa))

        arcs = fsa.arcs.values()[:, :-1]
        assert isinstance(arcs, torch.Tensor)
        assert arcs.dtype == torch.int32
        assert arcs.device.type == 'cpu'
        assert arcs.shape == (8, 3), 'there should be 8 arcs'
        assert torch.allclose(arcs[0],
                              torch.tensor([0, 1, 2], dtype=torch.int32))

        assert torch.allclose(
            fsa.scores,
            torch.tensor([-1.2, -2.2, -3.2, -4.2, -5.2, -6.2, -7.2, -8.2],
                         dtype=torch.float32))

        fsa.scores *= -1

        assert torch.allclose(
            fsa.scores,
            torch.tensor([1.2, 2.2, 3.2, 4.2, 5.2, 6.2, 7.2, 8.2],
                         dtype=torch.float32))
Exemplo n.º 4
0
    def test_cuda_arc_array1_to_tensor(self):
        device_id = 0
        _arc_array = _k2.get_cuda_arc_array1(device_id)
        arc_array = k2.Array(_arc_array)
        tensor = arc_array.tensor()
        assert tensor.ndim == 2
        assert tensor.shape == (2, 4)
        assert tensor.device.type == 'cuda'
        assert tensor.device.index == device_id
        assert tensor[0][0] == 1
        assert tensor[0][3] == _k2._float_as_int(_arc_array.get(0).score)

        tensor[0][0] = 10  # also change _arc_array
        assert _arc_array.get(0).src_state == 10

        del arc_array, _arc_array
        tensor[0][0] += 10
        assert tensor[0][0] == 20, 'tensor should still be accessible'
Exemplo n.º 5
0
    def test_transducer_from_tensor(self):
        devices = [torch.device('cpu')]
        if torch.cuda.is_available():
            devices.append(torch.device('cuda', 0))

        for device in devices:
            fsa_tensor = torch.tensor(
                [[0, 1, 2, _k2._float_as_int(-1.2)],
                 [0, 2, 10, _k2._float_as_int(-2.2)],
                 [1, 6, -1, _k2._float_as_int(-4.2)],
                 [1, 3, 3, _k2._float_as_int(-3.2)],
                 [2, 6, -1, _k2._float_as_int(-5.2)],
                 [2, 4, 2, _k2._float_as_int(-6.2)],
                 [3, 6, -1, _k2._float_as_int(-7.2)],
                 [5, 0, 1, _k2._float_as_int(-8.2)]],
                dtype=torch.int32).to(device)
            aux_labels_tensor = torch.tensor([22, 100, 16, 33, 26, 22, 36, 50],
                                             dtype=torch.int32).to(device)
            fsa = k2.Fsa(fsa_tensor, aux_labels_tensor)
            assert fsa.aux_labels.dtype == torch.int32
            assert fsa.aux_labels.device.type == device.type
            assert torch.allclose(
                fsa.aux_labels,
                torch.tensor([22, 100, 16, 33, 26, 22, 36, 50],
                             dtype=torch.int32).to(device))

            expected_str = '''
                0 1 2 22 -1.2
                0 2 10 100 -2.2
                1 6 -1 16 -4.2
                1 3 3 33 -3.2
                2 6 -1 26 -5.2
                2 4 2 22 -6.2
                3 6 -1 36 -7.2
                5 0 1 50 -8.2
                6
            '''
            assert _remove_leading_spaces(
                expected_str) == _remove_leading_spaces(k2.to_str(fsa))
Exemplo n.º 6
0
    def test_transducer_from_tensor(self):
        device_id = 0
        device = torch.device('cuda', device_id)
        fsa_tensor = torch.tensor(
            [[0, 1, 2, _k2._float_as_int(-1.2)],
             [0, 2, 10, _k2._float_as_int(-2.2)],
             [1, 6, -1, _k2._float_as_int(-4.2)],
             [1, 3, 3, _k2._float_as_int(-3.2)],
             [2, 6, -1, _k2._float_as_int(-5.2)],
             [2, 4, 2, _k2._float_as_int(-6.2)],
             [3, 6, -1, _k2._float_as_int(-7.2)],
             [5, 0, 1, _k2._float_as_int(-8.2)]],
            dtype=torch.int32).to(device)
        aux_labels_tensor = torch.tensor([22, 100, 16, 33, 26, 22, 36, 50],
                                         dtype=torch.int32).to(device)
        fsa = k2.Fsa(fsa_tensor, aux_labels_tensor)
        assert fsa.aux_labels.dtype == torch.int32
        assert fsa.aux_labels.device.type == 'cuda'
        assert torch.allclose(
            fsa.aux_labels,
            torch.tensor([22, 100, 16, 33, 26, 22, 36, 50],
                         dtype=torch.int32).to(device))

        expected_str = '''
            0 1 2 22 -1.2
            0 2 10 100 -2.2
            1 6 -1 16 -4.2
            1 3 3 33 -3.2
            2 6 -1 26 -5.2
            2 4 2 22 -6.2
            3 6 -1 36 -7.2
            5 0 1 50 -8.2
            6
        '''
        assert _remove_leading_spaces(expected_str) == _remove_leading_spaces(
            fsa.to('cpu').to_str())  # String IO supported on CPU only