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: stop using distutils (for python 3.12) [MLG-1519] #8667

Merged
merged 1 commit into from
Jan 9, 2024
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 harness/determined/cli/experiment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import base64
import distutils.util
import json
import numbers
import pathlib
Expand Down Expand Up @@ -1269,7 +1268,7 @@ def experiment_id_arg(help: str) -> Arg: # noqa: A002
),
Arg(
"--smaller-is-better",
type=lambda s: bool(distutils.util.strtobool(s)),
type=lambda s: util.strtobool(s),
default=None,
help="The sort order for metrics when using --sort-by. For "
"example, 'accuracy' would require passing '--smaller-is-better false'. If "
Expand Down
3 changes: 1 addition & 2 deletions harness/determined/common/declarative_argparse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import distutils.util
import functools
import itertools
from argparse import SUPPRESS, ArgumentDefaultsHelpFormatter, ArgumentParser, Namespace
Expand Down Expand Up @@ -247,4 +246,4 @@ def description_sort_key(desc: Any) -> str:

def string_to_bool(s: str) -> bool:
"""Converts string values to boolean for flag arguments (e.g. --active=true)"""
return bool(distutils.util.strtobool(s))
return util.strtobool(s)
12 changes: 12 additions & 0 deletions harness/determined/common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,15 @@ def wrapper_deprecated(*args: Any, **kwargs: Any) -> Any:
return cast(U, wrapper_deprecated)

return decorator


def strtobool(val: str) -> bool:
"""
A port of the distutils.util.strtobool function, removed in python 3.12.

The only difference in this function is that any non-falsy value which is a non-empty string is
accepted as a true value. That small difference gives us a small headstart on this todo:

TODO(MLG-1520): we should instead treat any nonempty string as "true".
"""
return bool(val and val.lower() not in ("n", "no", "f", "false", "off", "0"))
7 changes: 6 additions & 1 deletion harness/determined/pytorch/_pytorch_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,12 @@ def train_batch(self, batch, epoch_idx, batch_idx):
# 59.6.0. The bug is that it attempts to import distutils then access distutils.version
# without actually importing distutils.version. We can workaround this by prepopulating
# the distutils.version submodule in the distutils module.
import distutils.version # noqa: F401
#
# Except, starting with python 3.12 distutils isn't available at all.
try:
import distutils.version # noqa: F401
except ImportError:
pass

from torch.utils.tensorboard import SummaryWriter

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,12 @@ def train_batch(self, batch, epoch_idx, batch_idx):
# 59.6.0. The bug is that it attempts to import distutils then access distutils.version
# without actually importing distutils.version. We can workaround this by prepopulating
# the distutils.version submodule in the distutils module.
import distutils.version # noqa: F401
#
# Except, starting with python 3.12 distutils isn't available at all.
try:
import distutils.version # noqa: F401
except ImportError:
pass

from torch.utils.tensorboard import SummaryWriter

Expand Down
6 changes: 5 additions & 1 deletion harness/determined/tensorboard/metric_writers/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
# importing distutils.version. We can workaround this by prepopulating the distutils.version
# submodule in the distutils module.
if version.parse("1.9.0") <= version.parse(torch.__version__) < version.parse("1.11.0"):
import distutils.version # isort:skip # noqa: F401
# Except, starting with python 3.12 distutils isn't available at all.
try:
import distutils.version # isort:skip # noqa: F401
except ImportError:
pass

from torch.utils.tensorboard import SummaryWriter # isort:skip

Expand Down