Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CPU training issue on non-CUDA system #2655

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Callable, Dict, List

import numpy as np
from torch.cuda import is_available as cuda_available

from otx.algorithms.common.adapters.torch.utils import BsSearchAlgo
from otx.algorithms.common.utils.logger import get_logger
Expand Down Expand Up @@ -53,6 +54,10 @@ def adapt_batch_size(train_func: Callable, cfg, datasets: List, validate: bool =
not_increase (bool) : Whether adapting batch size to larger value than default value or not.
"""

if not cuda_available():
logger.warning("Skip Auto-adaptive batch size: CUDA should be available, but it isn't.")
return

def train_func_single_iter(batch_size):
copied_cfg = deepcopy(cfg)
_set_batch_size(copied_cfg, batch_size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ def test_adapt_batch_size(
assert len(mock_train_func.call_args_list[0].kwargs["cfg"].custom_hooks) == 1


def test_adapt_batch_size_no_gpu(mocker, common_cfg, mock_dataset):
# prepare
mock_train_func = mocker.MagicMock()
mock_config = set_mock_cfg_not_action(common_cfg)
mocker.patch.object(automatic_bs, "cuda_available", return_value=False)

# execute
adapt_batch_size(mock_train_func, mock_config, mock_dataset, False, True)

# check train function ins't called.
mock_train_func.assert_not_called()


class TestSubDataset:
@pytest.fixture(autouse=True)
def set_up(self, mocker):
Expand Down