Prompt48 commited on
Commit
3945af7
·
verified ·
1 Parent(s): dea2756

Upload edit\Qwen3-TTS-test\.venv\Lib\site-packages\accelerate\logging.py with huggingface_hub

Browse files
edit//Qwen3-TTS-test//.venv//Lib//site-packages//accelerate//logging.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2022 The HuggingFace Team. All rights reserved.
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.
14
+
15
+ import functools
16
+ import logging
17
+ import os
18
+ from typing import Optional
19
+
20
+ from .state import PartialState
21
+
22
+
23
+ class MultiProcessAdapter(logging.LoggerAdapter):
24
+ """
25
+ An adapter to assist with logging in multiprocess.
26
+
27
+ `log` takes in an additional `main_process_only` kwarg, which dictates whether it should be called on all processes
28
+ or only the main executed one. Default is `main_process_only=True`.
29
+
30
+ Does not require an `Accelerator` object to be created first.
31
+ """
32
+
33
+ @staticmethod
34
+ def _should_log(main_process_only):
35
+ "Check if log should be performed"
36
+ state = PartialState()
37
+ return not main_process_only or (main_process_only and state.is_main_process)
38
+
39
+ def log(self, level, msg, *args, **kwargs):
40
+ """
41
+ Delegates logger call after checking if we should log.
42
+
43
+ Accepts a new kwarg of `main_process_only`, which will dictate whether it will be logged across all processes
44
+ or only the main executed one. Default is `True` if not passed
45
+
46
+ Also accepts "in_order", which if `True` makes the processes log one by one, in order. This is much easier to
47
+ read, but comes at the cost of sometimes needing to wait for the other processes. Default is `False` to not
48
+ break with the previous behavior.
49
+
50
+ `in_order` is ignored if `main_process_only` is passed.
51
+ """
52
+ if PartialState._shared_state == {}:
53
+ raise RuntimeError(
54
+ "You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility."
55
+ )
56
+ main_process_only = kwargs.pop("main_process_only", True)
57
+ in_order = kwargs.pop("in_order", False)
58
+ # set `stacklevel` to exclude ourself in `Logger.findCaller()` while respecting user's choice
59
+ kwargs.setdefault("stacklevel", 2)
60
+
61
+ if self.isEnabledFor(level):
62
+ if self._should_log(main_process_only):
63
+ msg, kwargs = self.process(msg, kwargs)
64
+ self.logger.log(level, msg, *args, **kwargs)
65
+
66
+ elif in_order:
67
+ state = PartialState()
68
+ for i in range(state.num_processes):
69
+ if i == state.process_index:
70
+ msg, kwargs = self.process(msg, kwargs)
71
+ self.logger.log(level, msg, *args, **kwargs)
72
+ state.wait_for_everyone()
73
+
74
+ @functools.lru_cache(None)
75
+ def warning_once(self, *args, **kwargs):
76
+ """
77
+ This method is identical to `logger.warning()`, but will emit the warning with the same message only once
78
+
79
+ Note: The cache is for the function arguments, so 2 different callers using the same arguments will hit the
80
+ cache. The assumption here is that all warning messages are unique across the code. If they aren't then need to
81
+ switch to another type of cache that includes the caller frame information in the hashing function.
82
+ """
83
+ self.warning(*args, **kwargs)
84
+
85
+
86
+ def get_logger(name: str, log_level: Optional[str] = None):
87
+ """
88
+ Returns a `logging.Logger` for `name` that can handle multiprocessing.
89
+
90
+ If a log should be called on all processes, pass `main_process_only=False` If a log should be called on all
91
+ processes and in order, also pass `in_order=True`
92
+
93
+ Args:
94
+ name (`str`):
95
+ The name for the logger, such as `__file__`
96
+ log_level (`str`, *optional*):
97
+ The log level to use. If not passed, will default to the `LOG_LEVEL` environment variable, or `INFO` if not
98
+
99
+ Example:
100
+
101
+ ```python
102
+ >>> from accelerate.logging import get_logger
103
+ >>> from accelerate import Accelerator
104
+
105
+ >>> logger = get_logger(__name__)
106
+
107
+ >>> accelerator = Accelerator()
108
+ >>> logger.info("My log", main_process_only=False)
109
+ >>> logger.debug("My log", main_process_only=True)
110
+
111
+ >>> logger = get_logger(__name__, log_level="DEBUG")
112
+ >>> logger.info("My log")
113
+ >>> logger.debug("My second log")
114
+
115
+ >>> array = ["a", "b", "c", "d"]
116
+ >>> letter_at_rank = array[accelerator.process_index]
117
+ >>> logger.info(letter_at_rank, in_order=True)
118
+ ```
119
+ """
120
+ if log_level is None:
121
+ log_level = os.environ.get("ACCELERATE_LOG_LEVEL", None)
122
+ logger = logging.getLogger(name)
123
+ if log_level is not None:
124
+ logger.setLevel(log_level.upper())
125
+ logger.root.setLevel(log_level.upper())
126
+ return MultiProcessAdapter(logger, {})