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

[Tune] Add find_free_port Tune util #16098

Merged
merged 1 commit into from
May 27, 2021
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
3 changes: 1 addition & 2 deletions python/ray/tune/integration/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
from ray.tune.result import RESULT_DUPLICATE
from ray.tune.function_runner import wrap_function
from ray.tune.resources import Resources
from ray.util.sgd.utils import find_free_port
from ray.util.placement_group import remove_placement_group
from ray.tune.utils.trainable import PlacementGroupUtil, TrainableUtil
from ray.tune.utils.util import detect_checkpoint_function
from ray.tune.utils import detect_checkpoint_function, find_free_port
from typing import Callable, Dict, Type, Optional

logger = logging.getLogger(__name__)
Expand Down
14 changes: 7 additions & 7 deletions python/ray/tune/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from ray.tune.utils.util import (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When do/don't we add references in this file? For example, the atomic_save function is not defined here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a convention for this either, I think atomic_save was probably just forgotten to be included. @richardliaw?

deep_update, date_str, flatten_dict, get_pinned_object, merge_dicts,
pin_in_object_store, unflattened_lookup, UtilMonitor,
deep_update, date_str, find_free_port, flatten_dict, get_pinned_object,
merge_dicts, pin_in_object_store, unflattened_lookup, UtilMonitor,
validate_save_restore, warn_if_slow, diagnose_serialization,
detect_checkpoint_function, detect_reporter, detect_config_single,
wait_for_gpu)

__all__ = [
"deep_update", "date_str", "flatten_dict", "get_pinned_object",
"merge_dicts", "pin_in_object_store", "unflattened_lookup", "UtilMonitor",
"validate_save_restore", "warn_if_slow", "diagnose_serialization",
"detect_checkpoint_function", "detect_reporter", "detect_config_single",
"wait_for_gpu"
"deep_update", "date_str", "find_free_port", "flatten_dict",
"get_pinned_object", "merge_dicts", "pin_in_object_store",
"unflattened_lookup", "UtilMonitor", "validate_save_restore",
"warn_if_slow", "diagnose_serialization", "detect_checkpoint_function",
"detect_reporter", "detect_config_single", "wait_for_gpu"
]
10 changes: 10 additions & 0 deletions python/ray/tune/utils/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import socket
from contextlib import closing
from typing import Dict, List, Union
import copy
import json
Expand Down Expand Up @@ -484,6 +486,14 @@ def atomic_save(state: Dict, checkpoint_dir: str, file_name: str,
os.replace(tmp_search_ckpt_path, os.path.join(checkpoint_dir, file_name))


def find_free_port():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a convention for where new functions should be, e.g. end of file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No convention; usually locally grouped by similarity

"""Finds a free port on the current node."""
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(("", 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return s.getsockname()[1]


def load_newest_checkpoint(dirpath: str, ckpt_pattern: str) -> dict:
"""Returns the most recently modified checkpoint.

Expand Down