Prompt48 commited on
Commit
093edb6
·
verified ·
1 Parent(s): fbcc1c2

Upload edit\Qwen3-TTS-test\.venv\Lib\site-packages\torch\mps\__init__.py with huggingface_hub

Browse files
edit//Qwen3-TTS-test//.venv//Lib//site-packages//torch//mps//__init__.py ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # mypy: allow-untyped-defs
2
+ r"""
3
+ This package enables an interface for accessing MPS (Metal Performance Shaders) backend in Python.
4
+ Metal is Apple's API for programming metal GPU (graphics processor unit). Using MPS means that increased
5
+ performance can be achieved, by running work on the metal GPU(s).
6
+ See https://developer.apple.com/documentation/metalperformanceshaders for more details.
7
+ """
8
+ from typing import Union
9
+
10
+ import torch
11
+ from torch import Tensor
12
+
13
+
14
+ _is_in_bad_fork = getattr(torch._C, "_mps_is_in_bad_fork", lambda: False)
15
+ _default_mps_generator: torch._C.Generator = None # type: ignore[assignment]
16
+
17
+
18
+ # local helper function (not public or exported)
19
+ def _get_default_mps_generator() -> torch._C.Generator:
20
+ global _default_mps_generator
21
+ if _default_mps_generator is None:
22
+ _default_mps_generator = torch._C._mps_get_default_generator()
23
+ return _default_mps_generator
24
+
25
+
26
+ def device_count() -> int:
27
+ r"""Returns the number of available MPS devices."""
28
+ return int(torch._C._has_mps and torch._C._mps_is_available())
29
+
30
+
31
+ def synchronize() -> None:
32
+ r"""Waits for all kernels in all streams on a MPS device to complete."""
33
+ return torch._C._mps_deviceSynchronize()
34
+
35
+
36
+ def get_rng_state(device: Union[int, str, torch.device] = "mps") -> Tensor:
37
+ r"""Returns the random number generator state as a ByteTensor.
38
+
39
+ Args:
40
+ device (torch.device or int, optional): The device to return the RNG state of.
41
+ Default: ``'mps'`` (i.e., ``torch.device('mps')``, the current MPS device).
42
+ """
43
+ return _get_default_mps_generator().get_state()
44
+
45
+
46
+ def set_rng_state(
47
+ new_state: Tensor, device: Union[int, str, torch.device] = "mps"
48
+ ) -> None:
49
+ r"""Sets the random number generator state.
50
+
51
+ Args:
52
+ new_state (torch.ByteTensor): The desired state
53
+ device (torch.device or int, optional): The device to set the RNG state.
54
+ Default: ``'mps'`` (i.e., ``torch.device('mps')``, the current MPS device).
55
+ """
56
+ new_state_copy = new_state.clone(memory_format=torch.contiguous_format)
57
+ _get_default_mps_generator().set_state(new_state_copy)
58
+
59
+
60
+ def manual_seed(seed: int) -> None:
61
+ r"""Sets the seed for generating random numbers.
62
+
63
+ Args:
64
+ seed (int): The desired seed.
65
+ """
66
+ # the torch.mps.manual_seed() can be called from the global
67
+ # torch.manual_seed() in torch/random.py. So we need to make
68
+ # sure mps is available (otherwise we just return without
69
+ # erroring out)
70
+ if not torch._C._has_mps:
71
+ return
72
+ seed = int(seed)
73
+ _get_default_mps_generator().manual_seed(seed)
74
+
75
+
76
+ def seed() -> None:
77
+ r"""Sets the seed for generating random numbers to a random number."""
78
+ _get_default_mps_generator().seed()
79
+
80
+
81
+ def empty_cache() -> None:
82
+ r"""Releases all unoccupied cached memory currently held by the caching
83
+ allocator so that those can be used in other GPU applications.
84
+ """
85
+ torch._C._mps_emptyCache()
86
+
87
+
88
+ def set_per_process_memory_fraction(fraction) -> None:
89
+ r"""Set memory fraction for limiting process's memory allocation on MPS device.
90
+ The allowed value equals the fraction multiplied by recommended maximum device memory
91
+ (obtained from Metal API device.recommendedMaxWorkingSetSize).
92
+ If trying to allocate more than the allowed value in a process, it will raise an out of
93
+ memory error in allocator.
94
+
95
+ Args:
96
+ fraction(float): Range: 0~2. Allowed memory equals total_memory * fraction.
97
+
98
+ .. note::
99
+ Passing 0 to fraction means unlimited allocations
100
+ (may cause system failure if out of memory).
101
+ Passing fraction greater than 1.0 allows limits beyond the value
102
+ returned from device.recommendedMaxWorkingSetSize.
103
+ """
104
+
105
+ if not isinstance(fraction, float):
106
+ raise TypeError("Invalid type for fraction argument, must be `float`")
107
+ if fraction < 0 or fraction > 2:
108
+ raise ValueError(f"Invalid fraction value: {fraction}. Allowed range: 0~2")
109
+
110
+ torch._C._mps_setMemoryFraction(fraction)
111
+
112
+
113
+ def current_allocated_memory() -> int:
114
+ r"""Returns the current GPU memory occupied by tensors in bytes.
115
+
116
+ .. note::
117
+ The returned size does not include cached allocations in
118
+ memory pools of MPSAllocator.
119
+ """
120
+ return torch._C._mps_currentAllocatedMemory()
121
+
122
+
123
+ def driver_allocated_memory() -> int:
124
+ r"""Returns total GPU memory allocated by Metal driver for the process in bytes.
125
+
126
+ .. note::
127
+ The returned size includes cached allocations in MPSAllocator pools
128
+ as well as allocations from MPS/MPSGraph frameworks.
129
+ """
130
+ return torch._C._mps_driverAllocatedMemory()
131
+
132
+
133
+ def recommended_max_memory() -> int:
134
+ r"""Returns recommended max Working set size for GPU memory in bytes.
135
+
136
+ .. note::
137
+ Recommended max working set size for Metal.
138
+ returned from device.recommendedMaxWorkingSetSize.
139
+ """
140
+ return torch._C._mps_recommendedMaxMemory()
141
+
142
+
143
+ def _compile_shader(source: str):
144
+ r"""Compiles compute shader from source and allows one to invoke kernels
145
+ defined there from the comfort of Python runtime
146
+ Example::
147
+
148
+ >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_MPS)
149
+ >>> lib = torch.mps._compile_shader(
150
+ ... "kernel void full(device float* out, constant float& val, uint idx [[thread_position_in_grid]]) { out[idx] = val; }"
151
+ ... )
152
+ >>> x = torch.zeros(16, device="mps")
153
+ >>> lib.full(x, 3.14)
154
+ """
155
+ if not hasattr(torch._C, "_mps_compileShader"):
156
+ raise RuntimeError("MPS is not available")
157
+ return torch._C._mps_compileShader(source)
158
+
159
+
160
+ def is_available() -> bool:
161
+ return device_count() > 0
162
+
163
+
164
+ from . import profiler
165
+ from .event import Event
166
+
167
+
168
+ __all__ = [
169
+ "device_count",
170
+ "get_rng_state",
171
+ "manual_seed",
172
+ "seed",
173
+ "set_rng_state",
174
+ "synchronize",
175
+ "empty_cache",
176
+ "set_per_process_memory_fraction",
177
+ "current_allocated_memory",
178
+ "driver_allocated_memory",
179
+ "Event",
180
+ "profiler",
181
+ "recommended_max_memory",
182
+ "is_available",
183
+ ]