Skip to content

Commit 6d0cb9f

Browse files
remove _validate_cvcuda_dtype check
1 parent 3f6f771 commit 6d0cb9f

File tree

2 files changed

+16
-108
lines changed

2 files changed

+16
-108
lines changed

test/test_transforms_v2.py

Lines changed: 16 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6747,63 +6747,28 @@ def test_functional_error(self):
67476747
class TestToCVCUDATensor:
67486748
"""Tests for to_cvcuda_tensor function following patterns from TestToPil"""
67496749

6750-
def test_1_channel_uint8_tensor_to_cvcuda_tensor(self):
6750+
@pytest.mark.parametrize("dtype", [torch.uint8, torch.uint16, torch.float32, torch.float64])
6751+
def test_1_channel_to_cvcuda_tensor(self, dtype):
67516752
# Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6752-
img_data = torch.randint(0, 256, (1, 4, 4), dtype=torch.uint8)
6753-
img_data = img_data.cuda()
6754-
cvcuda_img = F.to_cvcuda_tensor(img_data)
6755-
# Check that the conversion succeeded and format is correct
6756-
assert cvcuda_img is not None
6757-
6758-
def test_1_channel_int16_tensor_to_cvcuda_tensor(self):
6759-
# Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6760-
img_data = torch.randint(0, 256, (1, 4, 4), dtype=torch.int16)
6761-
img_data = img_data.cuda()
6762-
cvcuda_img = F.to_cvcuda_tensor(img_data)
6763-
assert cvcuda_img is not None
6764-
6765-
def test_1_channel_int32_tensor_to_cvcuda_tensor(self):
6766-
# Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6767-
img_data = torch.randint(0, 256, (1, 4, 4), dtype=torch.int32)
6768-
img_data = img_data.cuda()
6769-
cvcuda_img = F.to_cvcuda_tensor(img_data)
6770-
assert cvcuda_img is not None
6771-
6772-
def test_1_channel_float32_tensor_to_cvcuda_tensor(self):
6773-
# Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6774-
img_data = torch.rand(1, 4, 4)
6775-
img_data = img_data.cuda()
6776-
cvcuda_img = F.to_cvcuda_tensor(img_data)
6777-
assert cvcuda_img is not None
6778-
6779-
def test_3_channel_uint8_tensor_to_cvcuda_tensor(self):
6780-
# Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6781-
img_data = torch.randint(0, 256, (3, 4, 4), dtype=torch.uint8)
6753+
if dtype in (torch.uint8, torch.uint16):
6754+
img_data = torch.randint(0, 256, (1, 4, 4), dtype=dtype)
6755+
else:
6756+
img_data = torch.rand(1, 4, 4, dtype=dtype)
67826757
img_data = img_data.cuda()
67836758
cvcuda_img = F.to_cvcuda_tensor(img_data)
67846759
assert cvcuda_img is not None
67856760

6786-
def test_3_channel_float32_tensor_to_cvcuda_tensor(self):
6761+
@pytest.mark.parametrize("dtype", [torch.uint8, torch.uint16, torch.float32, torch.float64])
6762+
def test_3_channel_to_cvcuda_tensor(self, dtype):
67876763
# Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6788-
img_data = torch.rand(3, 4, 4)
6764+
if dtype in (torch.uint8, torch.uint16):
6765+
img_data = torch.randint(0, 256, (3, 4, 4), dtype=dtype)
6766+
else:
6767+
img_data = torch.rand(3, 4, 4, dtype=dtype)
67896768
img_data = img_data.cuda()
67906769
cvcuda_img = F.to_cvcuda_tensor(img_data)
67916770
assert cvcuda_img is not None
67926771

6793-
def test_unsupported_num_channels(self):
6794-
# Test 2-channel image (not supported)
6795-
# Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6796-
img_data = torch.rand(2, 5, 5)
6797-
img_data = img_data.cuda()
6798-
with pytest.raises(ValueError, match="Only 1 and 3 channel images are supported"):
6799-
F.to_cvcuda_tensor(img_data)
6800-
6801-
# Test 4-channel image (not supported)
6802-
img_data = torch.randint(0, 256, (4, 5, 5), dtype=torch.uint8)
6803-
img_data = img_data.cuda()
6804-
with pytest.raises(ValueError, match="Only 1 and 3 channel images are supported"):
6805-
F.to_cvcuda_tensor(img_data)
6806-
68076772
def test_invalid_input_type(self):
68086773
with pytest.raises(TypeError, match=r"pic should be `torch.Tensor`"):
68096774
F.to_cvcuda_tensor("invalid_input")
@@ -6828,32 +6793,12 @@ def test_invalid_dimensions(self):
68286793
img_data = img_data.cuda()
68296794
F.to_cvcuda_tensor(img_data)
68306795

6831-
def test_float64_tensor_to_cvcuda_tensor(self):
6832-
# Test single channel float64 (F64 format is supported)
6833-
# Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6834-
img_data = torch.rand(1, 4, 4, dtype=torch.float64)
6835-
img_data = img_data.cuda()
6836-
cvcuda_img = F.to_cvcuda_tensor(img_data)
6837-
assert cvcuda_img is not None
6838-
6839-
def test_float64_rgb_not_supported(self):
6840-
# Test 3-channel float64 is NOT supported (no RGBf64 format in CV-CUDA)
6841-
# Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6842-
img_data = torch.rand(3, 4, 4, dtype=torch.float64)
6843-
img_data = img_data.cuda()
6844-
with pytest.raises(TypeError, match=r"Unsupported dtype"):
6845-
F.to_cvcuda_tensor(img_data)
6846-
68476796
@pytest.mark.parametrize("num_channels", [1, 3])
6848-
@pytest.mark.parametrize("dtype", [torch.uint8, torch.int16, torch.int32, torch.float32, torch.float64])
6797+
@pytest.mark.parametrize("dtype", [torch.uint8, torch.uint16, torch.float32, torch.float64])
68496798
def test_round_trip(self, num_channels, dtype):
6850-
# Skip int16/int32/float64 for 3-channel (only supported for single-channel)
6851-
if num_channels == 3 and dtype in (torch.int16, torch.int32, torch.float64):
6852-
pytest.skip(f"{dtype} is only supported for single-channel images")
6853-
68546799
# Setup: Create a tensor in CHW format (PyTorch standard)
68556800
# Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6856-
if dtype in (torch.uint8, torch.int16, torch.int32):
6801+
if dtype in (torch.uint8, torch.uint16):
68576802
original_tensor = torch.randint(0, 256, (num_channels, 4, 4), dtype=dtype)
68586803
else:
68596804
original_tensor = torch.rand(num_channels, 4, 4, dtype=dtype)
@@ -6871,16 +6816,12 @@ def test_round_trip(self, num_channels, dtype):
68716816
torch.testing.assert_close(result_tensor, original_tensor, rtol=0, atol=0)
68726817

68736818
@pytest.mark.parametrize("num_channels", [1, 3])
6874-
@pytest.mark.parametrize("dtype", [torch.uint8, torch.int16, torch.int32, torch.float32, torch.float64])
6819+
@pytest.mark.parametrize("dtype", [torch.uint8, torch.uint16, torch.float32, torch.float64])
68756820
@pytest.mark.parametrize("batch_size", [1, 2, 4])
68766821
def test_round_trip_batched(self, num_channels, dtype, batch_size):
6877-
# Skip int16/int32/float64 for 3-channel (only supported for single-channel)
6878-
if num_channels == 3 and dtype in (torch.int16, torch.int32, torch.float64):
6879-
pytest.skip(f"{dtype} is only supported for single-channel images")
6880-
68816822
# Setup: Create a batched tensor in NCHW format
68826823
# Create tensor on CPU first, then move to CUDA to avoid CUDA context issues
6883-
if dtype in (torch.uint8, torch.int16, torch.int32):
6824+
if dtype in (torch.uint8, torch.uint16):
68846825
original_tensor = torch.randint(0, 256, (batch_size, num_channels, 4, 4), dtype=dtype)
68856826
else:
68866827
original_tensor = torch.rand(batch_size, num_channels, 4, 4, dtype=dtype)

torchvision/transforms/v2/functional/_type_conversion.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,6 @@ def to_image(inpt: Union[torch.Tensor, PIL.Image.Image, np.ndarray]) -> tv_tenso
5353
pil_to_tensor = _F.pil_to_tensor
5454

5555

56-
def _validate_cvcuda_dtype(img_tensor: torch.Tensor) -> None:
57-
"""Validate that tensor dtype and channel count are supported by CV-CUDA.
58-
59-
Args:
60-
img_tensor: Tensor with shape (H, W, C) where C is number of channels.
61-
62-
Raises:
63-
TypeError: If dtype is not supported for the given number of channels.
64-
ValueError: If number of channels is not 1 or 3.
65-
"""
66-
num_channels = img_tensor.shape[2]
67-
dtype = img_tensor.dtype
68-
69-
# Handle single channel images
70-
if num_channels == 1:
71-
if dtype not in (torch.uint8, torch.int16, torch.int32, torch.float32, torch.float64):
72-
raise TypeError(f"Unsupported dtype {dtype} for single channel image")
73-
74-
# Handle 3 channel images (defaults to RGB)
75-
elif num_channels == 3:
76-
if dtype not in (torch.uint8, torch.float32):
77-
# Note: CV-CUDA does not support float64 for RGB images (only F64 for single-channel)
78-
raise TypeError(f"Unsupported dtype {dtype} for 3-channel image")
79-
80-
else:
81-
raise ValueError(f"Only 1 and 3 channel images are supported. Got {num_channels} channels.")
82-
83-
8456
@torch.jit.unused
8557
def to_cvcuda_tensor(pic) -> "cvcuda.Tensor":
8658
"""Convert a torch.Tensor to cvcuda.Tensor. This function does not support torchscript.
@@ -113,14 +85,9 @@ def to_cvcuda_tensor(pic) -> "cvcuda.Tensor":
11385
else:
11486
raise ValueError(f"pic should be 3 or 4 dimensional. Got {pic.ndim} dimensions.")
11587

116-
# At this point, img_tensor is always 4D in NCHW format
11788
# Convert NCHW -> NHWC
11889
img_tensor = img_tensor.permute(0, 2, 3, 1)
11990

120-
# Validate dtype and channel count from the first image
121-
sample_img = img_tensor[0]
122-
_validate_cvcuda_dtype(sample_img)
123-
12491
# Convert to CV-CUDA tensor with NHWC layout
12592
return cvcuda.as_tensor(img_tensor.cuda().contiguous(), cvcuda.TensorLayout.NHWC)
12693

0 commit comments

Comments
 (0)