From fb53aee50fb8d0b73a200334b4297ac445fd9ddf Mon Sep 17 00:00:00 2001 From: Rifa Ferzana <78103780+Rifa-111@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:47:34 +0000 Subject: [PATCH 1/8] Fix ShiftIntensity safe integer offset overflow Promote integer tensors before adding offsets when safe=True to avoid wraparound before dtype conversion, and cover signed/unsigned cases. Signed-off-by: Rifa Ferzana <78103780+Rifa-111@users.noreply.github.com> --- monai/transforms/intensity/array.py | 5 ++++- tests/transforms/test_shift_intensity.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/monai/transforms/intensity/array.py b/monai/transforms/intensity/array.py index 0421d344923..f93c5f8d3e7 100644 --- a/monai/transforms/intensity/array.py +++ b/monai/transforms/intensity/array.py @@ -252,7 +252,10 @@ def __call__(self, img: NdarrayOrTensor, offset: float | None = None) -> Ndarray img = convert_to_tensor(img, track_meta=get_track_meta()) offset = self.offset if offset is None else offset - out = img + offset + # Promote integer inputs before addition when safe=True. Otherwise integer + # offsets can overflow in the input dtype before safe conversion clamps. + computation_img = img.to(torch.float64) if self.safe and not img.is_floating_point() else img + out = computation_img + offset out, *_ = convert_data_type(data=out, dtype=img.dtype, safe=self.safe) return out diff --git a/tests/transforms/test_shift_intensity.py b/tests/transforms/test_shift_intensity.py index 70a6c7bf799..4b626fc5eb6 100644 --- a/tests/transforms/test_shift_intensity.py +++ b/tests/transforms/test_shift_intensity.py @@ -14,12 +14,32 @@ import unittest import numpy as np +import torch from monai.transforms import ShiftIntensity from tests.test_utils import NumpyImageTestCase2D class TestShiftIntensity(NumpyImageTestCase2D): + def test_safe_integer_offset_clamps_unsigned_image(self): + image = torch.tensor([0, 100, 255], dtype=torch.uint8) + for offset, expected in ( + (1, [1, 101, 255]), + (-1, [0, 99, 254]), + (1.0, [1, 101, 255]), + (-1.0, [0, 99, 254]), + ): + with self.subTest(offset=offset): + result = ShiftIntensity(offset=offset, safe=True)(image) + torch.testing.assert_close(result, torch.tensor(expected, dtype=torch.uint8)) + + def test_safe_integer_offset_clamps_signed_image(self): + image = torch.tensor([-128, 0, 127], dtype=torch.int8) + result = ShiftIntensity(offset=1, safe=True)(image) + torch.testing.assert_close(result, torch.tensor([-127, 1, 127], dtype=torch.int8)) + result = ShiftIntensity(offset=-1, safe=True)(image) + torch.testing.assert_close(result, torch.tensor([-128, -1, 126], dtype=torch.int8)) + def test_value(self): shifter = ShiftIntensity(offset=1.0) result = shifter(self.imt) From 63f1803787c4e85b96ed57f5f1acde067a67b65c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Nov 2026 00:50:50 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/transforms/test_shift_intensity.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/transforms/test_shift_intensity.py b/tests/transforms/test_shift_intensity.py index 4b626fc5eb6..4346f9adc90 100644 --- a/tests/transforms/test_shift_intensity.py +++ b/tests/transforms/test_shift_intensity.py @@ -23,12 +23,7 @@ class TestShiftIntensity(NumpyImageTestCase2D): def test_safe_integer_offset_clamps_unsigned_image(self): image = torch.tensor([0, 100, 255], dtype=torch.uint8) - for offset, expected in ( - (1, [1, 101, 255]), - (-1, [0, 99, 254]), - (1.0, [1, 101, 255]), - (-1.0, [0, 99, 254]), - ): + for offset, expected in ((1, [1, 101, 255]), (-1, [0, 99, 254]), (1.0, [1, 101, 255]), (-1.0, [0, 99, 254])): with self.subTest(offset=offset): result = ShiftIntensity(offset=offset, safe=True)(image) torch.testing.assert_close(result, torch.tensor(expected, dtype=torch.uint8)) From 1c792efd8f549fee2517993f58ad310b0f5de195 Mon Sep 17 00:00:00 2001 From: Rifa Ferzana <78103780+Rifa-111@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:05:50 +0000 Subject: [PATCH 3/8] Fix ShiftIntensity int64 precision for integral offsets --- monai/transforms/intensity/array.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/monai/transforms/intensity/array.py b/monai/transforms/intensity/array.py index ff5462e25ed..2acc70009f8 100644 --- a/monai/transforms/intensity/array.py +++ b/monai/transforms/intensity/array.py @@ -254,11 +254,27 @@ def __call__(self, img: NdarrayOrTensor, offset: float | None = None) -> Ndarray img = convert_to_tensor(img, track_meta=get_track_meta()) offset = self.offset if offset is None else offset - # Promote integer inputs before addition when safe=True. Otherwise integer - # offsets can overflow in the input dtype before safe conversion clamps. - computation_img = img.to(torch.float64) if self.safe and not img.is_floating_point() else img - out = computation_img + offset - out, *_ = convert_data_type(data=out, dtype=img.dtype, safe=self.safe) + if self.safe and not img.is_floating_point() and isinstance(offset, (int, float)) and float(offset).is_integer(): + # Keep integral offsets in integer arithmetic to avoid float64 precision + # loss for int64 values above 2**53. + shift = int(offset) + bounds = torch.iinfo(img.dtype) + if shift >= bounds.max - bounds.min: + out = torch.full_like(img, bounds.max) + elif shift <= bounds.min - bounds.max: + out = torch.full_like(img, bounds.min) + else: + # Clamp before adding so even int64 arithmetic cannot overflow. + work = img.to(torch.int64) + if shift > 0: + work = work.clamp(max=bounds.max - shift) + elif shift < 0: + work = work.clamp(min=bounds.min - shift) + out = (work + shift).to(img.dtype) + else: + computation_img = img.to(torch.float64) if self.safe and not img.is_floating_point() else img + out = computation_img + offset + out, *_ = convert_data_type(data=out, dtype=img.dtype, safe=self.safe) return out From f95b1acd18ecc729dd61aec5faeb0257188e3963 Mon Sep 17 00:00:00 2001 From: Rifa Ferzana <78103780+Rifa-111@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:05:56 +0000 Subject: [PATCH 4/8] Test ShiftIntensity large int64 precision and saturation --- tests/transforms/test_shift_intensity.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/transforms/test_shift_intensity.py b/tests/transforms/test_shift_intensity.py index 4346f9adc90..236fa45dc06 100644 --- a/tests/transforms/test_shift_intensity.py +++ b/tests/transforms/test_shift_intensity.py @@ -35,6 +35,18 @@ def test_safe_integer_offset_clamps_signed_image(self): result = ShiftIntensity(offset=-1, safe=True)(image) torch.testing.assert_close(result, torch.tensor([-128, -1, 126], dtype=torch.int8)) + def test_safe_int64_preserves_large_integer_precision(self): + image = torch.tensor([2**60 + 1, torch.iinfo(torch.int64).max], dtype=torch.int64) + result = ShiftIntensity(offset=1, safe=True)(image) + torch.testing.assert_close( + result, torch.tensor([2**60 + 2, torch.iinfo(torch.int64).max], dtype=torch.int64) + ) + negative = torch.tensor([-(2**60 + 1), torch.iinfo(torch.int64).min], dtype=torch.int64) + result = ShiftIntensity(offset=-1, safe=True)(negative) + torch.testing.assert_close( + result, torch.tensor([-(2**60 + 2), torch.iinfo(torch.int64).min], dtype=torch.int64) + ) + def test_value(self): shifter = ShiftIntensity(offset=1.0) result = shifter(self.imt) From 2926ae6333a7e59c39c04958c1a85f3bd79e5af4 Mon Sep 17 00:00:00 2001 From: Rifa Ferzana <78103780+Rifa-111@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:06:04 +0000 Subject: [PATCH 5/8] Avoid float conversion when checking integral offsets --- monai/transforms/intensity/array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/transforms/intensity/array.py b/monai/transforms/intensity/array.py index 2acc70009f8..c3f139000c0 100644 --- a/monai/transforms/intensity/array.py +++ b/monai/transforms/intensity/array.py @@ -254,7 +254,7 @@ def __call__(self, img: NdarrayOrTensor, offset: float | None = None) -> Ndarray img = convert_to_tensor(img, track_meta=get_track_meta()) offset = self.offset if offset is None else offset - if self.safe and not img.is_floating_point() and isinstance(offset, (int, float)) and float(offset).is_integer(): + if self.safe and not img.is_floating_point() and (isinstance(offset, int) or (isinstance(offset, float) and offset.is_integer())): # Keep integral offsets in integer arithmetic to avoid float64 precision # loss for int64 values above 2**53. shift = int(offset) From 564bb6b51082d485c8186eee819b3e0e8f8e8747 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:07:46 +0000 Subject: [PATCH 6/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/transforms/intensity/array.py | 6 +++++- tests/transforms/test_shift_intensity.py | 4 +--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/monai/transforms/intensity/array.py b/monai/transforms/intensity/array.py index c3f139000c0..dd77ab6563e 100644 --- a/monai/transforms/intensity/array.py +++ b/monai/transforms/intensity/array.py @@ -254,7 +254,11 @@ def __call__(self, img: NdarrayOrTensor, offset: float | None = None) -> Ndarray img = convert_to_tensor(img, track_meta=get_track_meta()) offset = self.offset if offset is None else offset - if self.safe and not img.is_floating_point() and (isinstance(offset, int) or (isinstance(offset, float) and offset.is_integer())): + if ( + self.safe + and not img.is_floating_point() + and (isinstance(offset, int) or (isinstance(offset, float) and offset.is_integer())) + ): # Keep integral offsets in integer arithmetic to avoid float64 precision # loss for int64 values above 2**53. shift = int(offset) diff --git a/tests/transforms/test_shift_intensity.py b/tests/transforms/test_shift_intensity.py index 236fa45dc06..21e1cbe4707 100644 --- a/tests/transforms/test_shift_intensity.py +++ b/tests/transforms/test_shift_intensity.py @@ -38,9 +38,7 @@ def test_safe_integer_offset_clamps_signed_image(self): def test_safe_int64_preserves_large_integer_precision(self): image = torch.tensor([2**60 + 1, torch.iinfo(torch.int64).max], dtype=torch.int64) result = ShiftIntensity(offset=1, safe=True)(image) - torch.testing.assert_close( - result, torch.tensor([2**60 + 2, torch.iinfo(torch.int64).max], dtype=torch.int64) - ) + torch.testing.assert_close(result, torch.tensor([2**60 + 2, torch.iinfo(torch.int64).max], dtype=torch.int64)) negative = torch.tensor([-(2**60 + 1), torch.iinfo(torch.int64).min], dtype=torch.int64) result = ShiftIntensity(offset=-1, safe=True)(negative) torch.testing.assert_close( From 43c339f4c3abb7b1d5641ee47023cad2ed7836d7 Mon Sep 17 00:00:00 2001 From: Rifa Ferzana <78103780+Rifa-111@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:16:22 +0000 Subject: [PATCH 7/8] Handle ShiftIntensity offsets beyond int64 scalar range --- monai/transforms/intensity/array.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/monai/transforms/intensity/array.py b/monai/transforms/intensity/array.py index dd77ab6563e..e5d3382f3cf 100644 --- a/monai/transforms/intensity/array.py +++ b/monai/transforms/intensity/array.py @@ -274,7 +274,15 @@ def __call__(self, img: NdarrayOrTensor, offset: float | None = None) -> Ndarray work = work.clamp(max=bounds.max - shift) elif shift < 0: work = work.clamp(min=bounds.min - shift) - out = (work + shift).to(img.dtype) + # Large offsets may exceed the range of a PyTorch int64 scalar. + # Split them into representable additions after clamping. + int64_bounds = torch.iinfo(torch.int64) + remaining = shift + while remaining: + step = min(remaining, int64_bounds.max) if remaining > 0 else max(remaining, int64_bounds.min) + work = work + step + remaining -= step + out = work.to(img.dtype) else: computation_img = img.to(torch.float64) if self.safe and not img.is_floating_point() else img out = computation_img + offset From e7fa92eb323be74a08f7d44a09aab645b99965f1 Mon Sep 17 00:00:00 2001 From: Rifa Ferzana <78103780+Rifa-111@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:16:36 +0000 Subject: [PATCH 8/8] Test int64 offset beyond scalar range --- tests/transforms/test_shift_intensity.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/transforms/test_shift_intensity.py b/tests/transforms/test_shift_intensity.py index 21e1cbe4707..52afc6ec1f0 100644 --- a/tests/transforms/test_shift_intensity.py +++ b/tests/transforms/test_shift_intensity.py @@ -45,6 +45,11 @@ def test_safe_int64_preserves_large_integer_precision(self): result, torch.tensor([-(2**60 + 2), torch.iinfo(torch.int64).min], dtype=torch.int64) ) + def test_safe_int64_offset_beyond_scalar_range(self): + image = torch.tensor([2**62], dtype=torch.int64) + result = ShiftIntensity(offset=float(-3 * 2**62), safe=True)(image) + torch.testing.assert_close(result, torch.tensor([torch.iinfo(torch.int64).min], dtype=torch.int64)) + def test_value(self): shifter = ShiftIntensity(offset=1.0) result = shifter(self.imt)