Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / trainer / runtime / logging.py: 56%
41 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-10-25 05:45 +0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-10-25 05:45 +0800
1# Copyright 2025-2026 Bytedance Ltd. and/or its affiliates
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
15"""Trainer-side logging helpers.
17Split out of the former ``auto_models/components/utils/helper.py`` in stage 7
18(05 §10.4); function names and signatures are unchanged.
19"""
21__all__ = [
22 "create_logger",
23 "disable_warning",
24 "enable_third_party_logging",
25 "setup_logging",
26]
28import logging as builtin_logging
29import os
30import sys
31import warnings
33import torch.distributed as dist
34import transformers
37logger = builtin_logging.getLogger(__name__)
40def _is_rank_0() -> bool:
41 """True if global rank 0 (or distributed not initialized)."""
42 if dist.is_initialized():
43 return dist.get_rank() == 0
44 return True
47def setup_logging() -> None:
48 """Setup logging with rank filter."""
49 builtin_logging.basicConfig(
50 format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
51 level=builtin_logging.INFO,
52 )
55def _info_rank0(message: str) -> None:
56 """Log an informational message on global rank zero."""
57 if not dist.is_initialized() or dist.get_rank() == 0:
58 logger.info(message)
61def create_logger(name: str | None = None) -> "builtin_logging._Logger":
62 """
63 Creates a pretty logger for the third-party program.
64 """
65 new_logger = builtin_logging.getLogger(name)
66 formatter = builtin_logging.Formatter(
67 fmt="[%(levelname)s|%(pathname)s:%(lineno)s] %(asctime)s >> %(message)s", datefmt="%m/%d/%Y %H:%M:%S"
68 )
69 handler = builtin_logging.StreamHandler(sys.stdout)
70 handler.setFormatter(formatter)
71 new_logger.addHandler(handler)
72 new_logger.setLevel(builtin_logging.INFO)
73 new_logger.propagate = False
74 return new_logger
77def enable_third_party_logging() -> None:
78 """
79 Enables explicit logger of the third-party libraries.
80 """
81 transformers.logging.set_verbosity_info()
82 transformers.logging.enable_default_handler()
83 transformers.logging.enable_explicit_format()
86def disable_warning() -> None:
87 """
88 Enables warning filter.
89 """
90 # pyiceberg is an optional dependency; import lazily so that importing this
91 # module does not require it.
92 from pyiceberg.metrics import LoggingMetricsReporter # pylint: disable=import-outside-toplevel
94 builtin_logging.basicConfig(level=builtin_logging.ERROR)
95 warnings.simplefilter("ignore")
96 LoggingMetricsReporter()
97 reporter_logger = builtin_logging.getLogger(LoggingMetricsReporter.__name__)
98 # PyIceberg exposes no public logger setter; retain its class-level reporter binding.
99 setattr(LoggingMetricsReporter, "_logger", reporter_logger)
100 reporter_logger.setLevel(builtin_logging.WARNING)
101 reporter_logger.propagate = False
104if os.getenv("DISABLE_WARNINGS", "0").lower() in ["true", "1"]:
105 disable_warning()