From c2cabcfeeb9673dca89290830657502ec7fc952d Mon Sep 17 00:00:00 2001 From: abhay-codes07 Date: Sun, 12 Jul 2026 14:44:35 +0530 Subject: [PATCH] Fix HyperTile random_divisor never picking the last option torch.randint's high bound is exclusive, so randint(low=0, high=len(ns) - 1) sampled indices 0..len(ns)-2 and never selected the final candidate. With swap_size=2 this always returned the first tile count, silently disabling the node's randomization. Use high=len(ns) so every option is reachable. --- comfy_extras/nodes_hypertile.py | 2 +- .../comfy_extras_test/nodes_hypertile_test.py | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests-unit/comfy_extras_test/nodes_hypertile_test.py diff --git a/comfy_extras/nodes_hypertile.py b/comfy_extras/nodes_hypertile.py index 2a96416be..3df2ed592 100644 --- a/comfy_extras/nodes_hypertile.py +++ b/comfy_extras/nodes_hypertile.py @@ -16,7 +16,7 @@ def random_divisor(value: int, min_value: int, /, max_options: int = 1) -> int: ns = [value // i for i in divisors[:max_options]] # has at least 1 element if len(ns) - 1 > 0: - idx = randint(low=0, high=len(ns) - 1, size=(1,)).item() + idx = randint(low=0, high=len(ns), size=(1,)).item() else: idx = 0 diff --git a/tests-unit/comfy_extras_test/nodes_hypertile_test.py b/tests-unit/comfy_extras_test/nodes_hypertile_test.py new file mode 100644 index 000000000..c3a4a4cef --- /dev/null +++ b/tests-unit/comfy_extras_test/nodes_hypertile_test.py @@ -0,0 +1,22 @@ +from unittest.mock import patch, MagicMock + +import torch + +mock_nodes = MagicMock() +mock_nodes.MAX_RESOLUTION = 16384 +mock_server = MagicMock() + +with patch.dict("sys.modules", {"nodes": mock_nodes, "server": mock_server}): + from comfy_extras.nodes_hypertile import random_divisor + + +class TestRandomDivisor: + def test_all_options_are_reachable(self): + # value=8, min=2, max_options=2 -> candidate tile counts {4, 2}; both must be + # selectable. torch.randint's high is exclusive, so the last option was unreachable. + torch.manual_seed(0) + results = {random_divisor(8, 2, 2) for _ in range(200)} + assert results == {2, 4} + + def test_single_option_is_deterministic(self): + assert random_divisor(8, 8, 1) == 1