评价此页

torch.mps#

创建于:2023年2月10日 | 最后更新于:2025年6月8日

该包提供了访问 Python 中 MPS(Metal Performance Shaders)后端的接口。Metal 是 Apple 的用于编程 Metal GPU(图形处理器单元)的 API。使用 MPS 意味着可以通过在 Metal GPU 上运行工作来获得更高的性能。有关更多详细信息,请参阅 https://developer.apple.com/documentation/metalperformanceshaders

device_count

返回可用的 MPS 设备数量。

synchronize

等待 MPS 设备上的所有流中的所有内核完成。

get_rng_state

以 ByteTensor 形式返回随机数生成器状态。

set_rng_state

设置随机数生成器状态。

manual_seed

设置生成随机数的种子。

seed

将生成随机数的种子设置为一个随机数。

empty_cache

释放缓存分配器当前持有的所有未占用缓存内存,以便其他 GPU 应用程序可以使用这些内存。

set_per_process_memory_fraction

设置内存分数以限制进程在 MPS 设备上的内存分配。

current_allocated_memory

返回张量当前占用的 GPU 内存(以字节为单位)。

driver_allocated_memory

返回 Metal 驱动程序为该进程分配的总 GPU 内存(以字节为单位)。

recommended_max_memory

返回 GPU 内存推荐的最大工作集大小(以字节为单位)。

compile_shader

从源代码编译计算着色器,并允许用户在 Python 运行时从容调用其中定义的内核。示例。

MPS Profiler#

profiler.start

从 MPS 后端启动 OS Signpost 跟踪。

profiler.stop

停止从 MPS 后端生成 OS Signpost 跟踪。

profiler.profile

上下文管理器,用于从 MPS 后端启用 OS Signpost 跟踪的生成。

profiler.is_capturing_metal

检查 Metal 捕获是否正在进行中

profiler.is_metal_capture_enabled

检查 metal_capture 上下文管理器是否可用。要启用 Metal 捕获,请设置 MTL_CAPTURE_ENABLED 环境变量。

profiler.metal_capture

上下文管理器,用于将 Metal 调用捕获到 gputrace 中。

MPS Event#

event.Event

MPS 事件的包装器。