Rate this Page
โ˜… โ˜… โ˜… โ˜… โ˜…

torch.mps#

Created On: Feb 10, 2023 | Last Updated On: Jun 08, 2025

This package enables an interface for accessing MPS (Metal Performance Shaders) backend in Python. Metal is Appleโ€™s API for programming metal GPU (graphics processor unit). Using MPS means that increased performance can be achieved, by running work on the metal GPU(s). See https://developer.apple.com/documentation/metalperformanceshaders for more details.

device_count

Returns the number of available MPS devices.

synchronize

Waits for all kernels in all streams on a MPS device to complete.

get_rng_state

Returns the random number generator state as a ByteTensor.

set_rng_state

Sets the random number generator state.

manual_seed

Sets the seed for generating random numbers.

seed

Sets the seed for generating random numbers to a random number.

empty_cache

Releases all unoccupied cached memory currently held by the caching allocator so that those can be used in other GPU applications.

set_per_process_memory_fraction

Set memory fraction for limiting process's memory allocation on MPS device.

current_allocated_memory

Returns the current GPU memory occupied by tensors in bytes.

driver_allocated_memory

Returns total GPU memory allocated by Metal driver for the process in bytes.

recommended_max_memory

Returns recommended max Working set size for GPU memory in bytes.

compile_shader

Compiles compute shader from source and allows one to invoke kernels defined there from the comfort of Python runtime Example.

MPS Profiler#

profiler.start

Start OS Signpost tracing from MPS backend.

profiler.stop

Stops generating OS Signpost tracing from MPS backend.

profiler.profile

Context Manager to enabling generating OS Signpost tracing from MPS backend.

profiler.is_capturing_metal

Cheks if metal capture is in progress

profiler.is_metal_capture_enabled

Checks if metal_capture context manager is usable To enable metal capture, set MTL_CAPTURE_ENABLED envvar

profiler.metal_capture

Conext manager that enables capturing of Metal calls into gputrace

MPS Event#

event.Event

Wrapper around an MPS event.