Repository: Project-MONAI/MONAI

Issue:
integration error: test_box_utils
**Describe the bug**
torch 1.12.0+cu102
```
[2022-06-29T01:24:48.559Z] ======================================================================
[2022-06-29T01:24:48.559Z] ERROR: test_value_12 (tests.test_box_utils.TestCreateBoxList)
[2022-06-29T01:24:48.559Z] ----------------------------------------------------------------------
[2022-06-29T01:24:48.559Z] Traceback (most recent call last):
[2022-06-29T01:24:48.559Z]   File "/usr/local/lib/python3.8/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
[2022-06-29T01:24:48.559Z]     return func(*(a + p.args), **p.kwargs)
[2022-06-29T01:24:48.559Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/test_box_utils.py", line 199, in test_value
[2022-06-29T01:24:48.559Z]     clipped_boxes, keep = clip_boxes_to_image(expected_box_standard, spatial_size, remove_empty=True)
[2022-06-29T01:24:48.559Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/data/box_utils.py", line 1014, in clip_boxes_to_image
[2022-06-29T01:24:48.559Z]     return spatial_crop_boxes(boxes, roi_start=[0] * spatial_dims, roi_end=spatial_size, remove_empty=remove_empty)
[2022-06-29T01:24:48.559Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/data/box_utils.py", line 977, in spatial_crop_boxes
[2022-06-29T01:24:48.559Z]     boxes_t[:, axis].clamp_(min=roi_start_torch[axis], max=roi_end_torch[axis] - TO_REMOVE)
[2022-06-29T01:24:48.559Z] RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument min in method wrapper_clamp__Tensor)
[2022-06-29T01:24:48.559Z] 
[2022-06-29T01:24:48.559Z] ======================================================================
[2022-06-29T01:24:48.559Z] ERROR: test_value_13 (tests.test_box_utils.TestCreateBoxList)
[2022-06-29T01:24:48.559Z] ----------------------------------------------------------------------
[2022-06-29T01:24:48.559Z] Traceback (most recent call last):
[2022-06-29T01:24:48.559Z]   File "/usr/local/lib/python3.8/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
[2022-06-29T01:24:48.559Z]     return func(*(a + p.args), **p.kwargs)
[2022-06-29T01:24:48.559Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/test_box_utils.py", line 199, in test_value
[2022-06-29T01:24:48.559Z]     clipped_boxes, keep = clip_boxes_to_image(expected_box_standard, spatial_size, remove_empty=True)
[2022-06-29T01:24:48.559Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/data/box_utils.py", line 1014, in clip_boxes_to_image
[2022-06-29T01:24:48.559Z]     return spatial_crop_boxes(boxes, roi_start=[0] * spatial_dims, roi_end=spatial_size, remove_empty=remove_empty)
[2022-06-29T01:24:48.559Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/data/box_utils.py", line 977, in spatial_crop_boxes
[2022-06-29T01:24:48.559Z]     boxes_t[:, axis].clamp_(min=roi_start_torch[axis], max=roi_end_torch[axis] - TO_REMOVE)
[2022-06-29T01:24:48.559Z] RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument min in method wrapper_clamp__Tensor)
[2022-06-29T01:24:48.559Z] 
[2022-06-29T01:24:48.559Z] ======================================================================
[2022-06-29T01:24:48.559Z] ERROR: test_value_14 (tests.test_box_utils.TestCreateBoxList)
[2022-06-29T01:24:48.559Z] ----------------------------------------------------------------------
[2022-06-29T01:24:48.559Z] Traceback (most recent call last):
[2022-06-29T01:24:48.560Z]   File "/usr/local/lib/python3.8/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
[2022-06-29T01:24:48.560Z]     return func(*(a + p.args), **p.kwargs)
[2022-06-29T01:24:48.560Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/test_box_utils.py", line 199, in test_value
[2022-06-29T01:24:48.560Z]     clipped_boxes, keep = clip_boxes_to_image(expected_box_standard, spatial_size, remove_empty=True)
[2022-06-29T01:24:48.560Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/data/box_utils.py", line 1014, in clip_boxes_to_image
[2022-06-29T01:24:48.560Z]     return spatial_crop_boxes(boxes, roi_start=[0] * spatial_dims, roi_end=spatial_size, remove_empty=remove_empty)
[2022-06-29T01:24:48.560Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/data/box_utils.py", line 977, in spatial_crop_boxes
[2022-06-29T01:24:48.560Z]     boxes_t[:, axis].clamp_(min=roi_start_torch[axis], max=roi_end_torch[axis] - TO_REMOVE)
[2022-06-29T01:24:48.560Z] RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument min in method wrapper_clamp__Tensor)
[2022-06-29T01:24:48.560Z] 
[2022-06-29T01:24:48.560Z] ======================================================================
[2022-06-29T01:24:48.560Z] ERROR: test_value_15 (tests.test_box_utils.TestCreateBoxList)
[2022-06-29T01:24:48.560Z] ----------------------------------------------------------------------
[2022-06-29T01:24:48.560Z] Traceback (most recent call last):
[2022-06-29T01:24:48.560Z]   File "/usr/local/lib/python3.8/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
[2022-06-29T01:24:48.560Z]     return func(*(a + p.args), **p.kwargs)
[2022-06-29T01:24:48.560Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/test_box_utils.py", line 199, in test_value
[2022-06-29T01:24:48.560Z]     clipped_boxes, keep = clip_boxes_to_image(expected_box_standard, spatial_size, remove_empty=True)
[2022-06-29T01:24:48.560Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/data/box_utils.py", line 1014, in clip_boxes_to_image
[2022-06-29T01:24:48.560Z]     return spatial_crop_boxes(boxes, roi_start=[0] * spatial_dims, roi_end=spatial_size, remove_empty=remove_empty)
[2022-06-29T01:24:48.560Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/data/box_utils.py", line 977, in spatial_crop_boxes
[2022-06-29T01:24:48.560Z]     boxes_t[:, axis].clamp_(min=roi_start_torch[axis], max=roi_end_torch[axis] - TO_REMOVE)
[2022-06-29T01:24:48.560Z] RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument min in method wrapper_clamp__Tensor)
[2022-06-29T01:24:48.560Z] 
[2022-06-29T01:24:48.560Z] ======================================================================
[2022-06-29T01:24:48.560Z] ERROR: test_value_16 (tests.test_box_utils.TestCreateBoxList)
[2022-06-29T01:24:48.560Z] ----------------------------------------------------------------------
[2022-06-29T01:24:48.560Z] Traceback (most recent call last):
[2022-06-29T01:24:48.560Z]   File "/usr/local/lib/python3.8/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
[2022-06-29T01:24:48.560Z]     return func(*(a + p.args), **p.kwargs)
[2022-06-29T01:24:48.560Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/test_box_utils.py", line 199, in test_value
[2022-06-29T01:24:48.560Z]     clipped_boxes, keep = clip_boxes_to_image(expected_box_standard, spatial_size, remove_empty=True)
[2022-06-29T01:24:48.560Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/data/box_utils.py", line 1014, in clip_boxes_to_image
[2022-06-29T01:24:48.560Z]     return spatial_crop_boxes(boxes, roi_start=[0] * spatial_dims, roi_end=spatial_size, remove_empty=remove_empty)
[2022-06-29T01:24:48.560Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/data/box_utils.py", line 977, in spatial_crop_boxes
[2022-06-29T01:24:48.560Z]     boxes_t[:, axis].clamp_(min=roi_start_torch[axis], max=roi_end_torch[axis] - TO_REMOVE)
[2022-06-29T01:24:48.560Z] RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument min in method wrapper_clamp__Tensor)
[2022-06-29T01:24:48.560Z] 
[2022-06-29T01:24:48.560Z] ======================================================================
[2022-06-29T01:24:48.560Z] ERROR: test_value_17 (tests.test_box_utils.TestCreateBoxList)
[2022-06-29T01:24:48.560Z] ----------------------------------------------------------------------
[2022-06-29T01:24:48.560Z] Traceback (most recent call last):
[2022-06-29T01:24:48.560Z]   File "/usr/local/lib/python3.8/dist-packages/parameterized/parameterized.py", line 533, in standalone_func
[2022-06-29T01:24:48.560Z]     return func(*(a + p.args), **p.kwargs)
[2022-06-29T01:24:48.560Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/tests/test_box_utils.py", line 199, in test_value
[2022-06-29T01:24:48.560Z]     clipped_boxes, keep = clip_boxes_to_image(expected_box_standard, spatial_size, remove_empty=True)
[2022-06-29T01:24:48.560Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/data/box_utils.py", line 1014, in clip_boxes_to_image
[2022-06-29T01:24:48.560Z]     return spatial_crop_boxes(boxes, roi_start=[0] * spatial_dims, roi_end=spatial_size, remove_empty=remove_empty)
[2022-06-29T01:24:48.560Z]   File "/home/jenkins/agent/workspace/Monai-pytorch-versions/monai/data/box_utils.py", line 977, in spatial_crop_boxes
[2022-06-29T01:24:48.560Z]     boxes_t[:, axis].clamp_(min=roi_start_torch[axis], max=roi_end_torch[axis] - TO_REMOVE)
[2022-06-29T01:24:48.560Z] RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument min in method wrapper_clamp__Tensor)
[2022-06-29T01:24:48.560Z] 
```


Discussion:

Resolving patch:
diff --git a/monai/data/box_utils.py b/monai/data/box_utils.py
--- a/monai/data/box_utils.py
+++ b/monai/data/box_utils.py
@@ -959,25 +959,23 @@ def spatial_crop_boxes(
         - ``keep``, it indicates whether each box in ``boxes`` are kept when ``remove_empty=True``.
     """
 
-    roi_start_torch, *_ = convert_data_type(
-        data=roi_start, output_type=torch.Tensor, dtype=torch.int16, wrap_sequence=True
-    )
-    roi_end_torch, *_ = convert_to_dst_type(src=roi_end, dst=roi_start_torch, wrap_sequence=True)
-    roi_end_torch = torch.maximum(roi_end_torch, roi_start_torch)
-
     # convert numpy to tensor if needed
     boxes_t, *_ = convert_data_type(deepcopy(boxes), torch.Tensor)
 
     # convert to float32 since torch.clamp_ does not support float16
     boxes_t = boxes_t.to(dtype=COMPUTE_DTYPE)
 
+    roi_start_t = convert_to_dst_type(src=roi_start, dst=boxes_t, wrap_sequence=True)[0].to(torch.int16)
+    roi_end_t = convert_to_dst_type(src=roi_end, dst=boxes_t, wrap_sequence=True)[0].to(torch.int16)
+    roi_end_t = torch.maximum(roi_end_t, roi_start_t)
+
     # makes sure the bounding boxes are within the patch
     spatial_dims = get_spatial_dims(boxes=boxes, spatial_size=roi_end)
     for axis in range(0, spatial_dims):
-        boxes_t[:, axis].clamp_(min=roi_start_torch[axis], max=roi_end_torch[axis] - TO_REMOVE)
-        boxes_t[:, axis + spatial_dims].clamp_(min=roi_start_torch[axis], max=roi_end_torch[axis] - TO_REMOVE)
-        boxes_t[:, axis] -= roi_start_torch[axis]
-        boxes_t[:, axis + spatial_dims] -= roi_start_torch[axis]
+        boxes_t[:, axis].clamp_(min=roi_start_t[axis], max=roi_end_t[axis] - TO_REMOVE)
+        boxes_t[:, axis + spatial_dims].clamp_(min=roi_start_t[axis], max=roi_end_t[axis] - TO_REMOVE)
+        boxes_t[:, axis] -= roi_start_t[axis]
+        boxes_t[:, axis + spatial_dims] -= roi_start_t[axis]
 
     # remove the boxes that are actually empty
     if remove_empty:


Test patch: