Example #1
0
 def test_padded_fail9(self, padded_tensor, padding_value, batch_size,
                       max_shape,
                       last_dim, dtype, device):
     with pytest.raises(ValueError,
                        match='shape_per_tensor should not be None if padding_value is set'):
         testing.check_padded_tensor(padded_tensor, padding_value,
                                     batch_size=batch_size,
                                     max_shape=max_shape, last_dim=last_dim,
                                     dtype=dtype,
                                     device=device)
Example #2
0
 def test_padded_fail8(self, padded_tensor, padding_value, shape_per_tensor,
                       batch_size, max_shape, last_dim, dtype, wrong_device):
     with pytest.raises(TypeError,
                        match='tensor device is cpu, should be cuda'):
         testing.check_padded_tensor(padded_tensor, padding_value,
                                     shape_per_tensor,
                                     batch_size, max_shape, last_dim, dtype,
                                     wrong_device)
     assert not testing.check_padded_tensor(padded_tensor, padding_value,
                                            shape_per_tensor, batch_size,
                                            max_shape,
                                            last_dim, dtype, wrong_device,
                                            throw=False)
Example #3
0
 def test_padded_fail6(self, padded_tensor, padding_value, shape_per_tensor,
                       batch_size, max_shape, wrong_last_dim, dtype, device):
     with pytest.raises(ValueError,
                        match='tensor last_dim is 2, should be 3'):
         testing.check_padded_tensor(padded_tensor, padding_value,
                                     shape_per_tensor,
                                     batch_size, max_shape, wrong_last_dim,
                                     dtype, device)
     assert not testing.check_padded_tensor(padded_tensor, padding_value,
                                            shape_per_tensor, batch_size,
                                            max_shape,
                                            wrong_last_dim, dtype, device,
                                            throw=False)
Example #4
0
 def test_padded_fail7(self, padded_tensor, padding_value, shape_per_tensor,
                       batch_size, max_shape, last_dim, wrong_dtype, device):
     with pytest.raises(TypeError,
                        match='tensor dtype is torch.float32, should be torch.int64'):
         testing.check_padded_tensor(padded_tensor, padding_value,
                                     shape_per_tensor,
                                     batch_size, max_shape, last_dim,
                                     wrong_dtype, device)
     assert not testing.check_padded_tensor(padded_tensor, padding_value,
                                            shape_per_tensor, batch_size,
                                            max_shape,
                                            last_dim, wrong_dtype, device,
                                            throw=False)
Example #5
0
 def test_padded_fail5(self, padded_tensor, padding_value, shape_per_tensor,
                       batch_size, wrong_max_shape, last_dim, dtype, device):
     with pytest.raises(ValueError,
                        match=r'tensor max_shape is torch.Size\(\[4, 4\]\), should be \(4, 4, 4\)'):
         testing.check_padded_tensor(padded_tensor, padding_value,
                                     shape_per_tensor,
                                     batch_size, wrong_max_shape, last_dim,
                                     dtype, device)
     assert not testing.check_padded_tensor(padded_tensor, padding_value,
                                            shape_per_tensor, batch_size,
                                            wrong_max_shape,
                                            last_dim, dtype, device,
                                            throw=False)
Example #6
0
 def test_padded_fail3(self, padded_tensor, padding_value, shape_per_tensor,
                       wrong_batch_size, max_shape, last_dim, dtype, device):
     with pytest.raises(ValueError,
                        match='batch_size is 3, but there are 2 shapes in shape_per_tensor'):
         testing.check_padded_tensor(padded_tensor, padding_value,
                                     shape_per_tensor,
                                     wrong_batch_size, max_shape, last_dim,
                                     dtype, device)
     assert not testing.check_padded_tensor(padded_tensor, padding_value,
                                            shape_per_tensor,
                                            wrong_batch_size, max_shape,
                                            last_dim, dtype, device,
                                            throw=False)
Example #7
0
 def test_padded_fail2(self, padded_tensor, padding_value,
                       wrong_shape_per_tensor,
                       batch_size, max_shape, last_dim, dtype, device):
     with pytest.raises(ValueError,
                        match=r'tensor padding at \(1, 0, 1, 0\) is 0.0, should be -1.0'):
         testing.check_padded_tensor(padded_tensor, padding_value,
                                     wrong_shape_per_tensor,
                                     batch_size, max_shape, last_dim, dtype,
                                     device)
     assert not testing.check_padded_tensor(padded_tensor, padding_value,
                                            wrong_shape_per_tensor,
                                            batch_size, max_shape,
                                            last_dim, dtype, device,
                                            throw=False)
Example #8
0
 def test_padded_fail4(self, padded_tensor, padding_value,
                       wrong_batch_size, max_shape, last_dim, dtype, device):
     with pytest.raises(ValueError,
                        match='tensor batch size is 2, should be 3'):
         testing.check_padded_tensor(padded_tensor, padding_value,
                                     batch_size=wrong_batch_size,
                                     max_shape=max_shape, last_dim=last_dim,
                                     dtype=dtype,
                                     device=device)
     assert not testing.check_padded_tensor(padded_tensor, padding_value,
                                            batch_size=wrong_batch_size,
                                            max_shape=max_shape,
                                            last_dim=last_dim, dtype=dtype,
                                            device=device,
                                            throw=False)
Example #9
0
 def test_padded_success(self, padded_tensor, padding_value,
                         shape_per_tensor,
                         batch_size, max_shape, last_dim, dtype, device):
     assert testing.check_padded_tensor(padded_tensor, padding_value,
                                        shape_per_tensor,
                                        batch_size, max_shape, last_dim,
                                        dtype, device)
Example #10
0
    def test_list_to_padded_to_list(self, tensor_list, batch_size,
                                    padding_value,
                                    shape_per_tensor, max_shape, last_dim,
                                    dtype, device):
        padded_tensor, output_shape_per_tensor = batch.list_to_padded(
            tensor_list, padding_value, max_shape)

        assert torch.equal(output_shape_per_tensor, shape_per_tensor)
        check_padded_tensor(padded_tensor, batch_size=batch_size,
                            shape_per_tensor=shape_per_tensor,
                            padding_value=padding_value, max_shape=max_shape,
                            last_dim=last_dim,
                            dtype=dtype, device=device)
        for i, shape in enumerate(shape_per_tensor):
            assert torch.equal(
                padded_tensor[[i] + [slice(dim) for dim in shape]],
                tensor_list[i])
        output_tensor_list = batch.padded_to_list(padded_tensor,
                                                  shape_per_tensor)
        for output_tensor, expected_tensor in zip(output_tensor_list,
                                                  tensor_list):
            assert torch.equal(output_tensor, expected_tensor)
Example #11
0
 def test_padded_default_success(self, padded_tensor):
     assert testing.check_padded_tensor(padded_tensor)