An opaque data container for large storage in MPS CNN filters.
- iOS 11.0+
- macOS 10.13+
- Mac Catalyst 13.0+Beta
- tvOS 11.0+
- Metal Performance Shaders
Some MPS CNN kernels produce additional information beyond an
MPSImage. These may be pooling indices where the result came from, convolution weights, or other information not contained in the usual
MPSImage result from a
MPSState object typically contains one or more expensive
MTLResource objects such as textures or buffers to store this information. It provides a base class with interfaces for managing this storage. Child classes may add additional functionality specific to their contents.
MPSState objects are temporary. Temporary state objects, for example,
MPSTemporary, are for very short lived storage, perhaps just a few lines of code within the scope of a single
MTLCommand. They are very efficient for storage, as several temporary objects can share the same memory over the course of a command buffer. This can improve both memory usage and time spent in the kernel wiring down memory and such. You may find that some large CNN tasks can not be computed without them, as nontemporary storage would simply take up too much memory.
In exchange, the lifetime of the underlying storage in temporary
MPSState objects needs to be carefully managed. ARC often waits until the end of scope to release objects. Temporary storage often needs to be released sooner than that. Consequently the lifetime of the data in the underlying Metal resources is managed by a
read property. Each time a
MPSCNNKernel reads a temporary
MPSState object the
read is automatically decremented. When it reaches 0, the underlying storage is recycled for use by other MPS temporary objects, and the data is becomes undefined. If you need to consume the data multiple times, you should set the
read to a larger number to prevent the data from becoming undefined. You may set the
read to 0 yourself to return the storage to MPS, if for any reason, you realize that the
MPSState object will no longer be used.
The contents of a temporary
MPSState object are only valid from creation to the time the
read reaches 0. The data is only valid for the
MTLCommand on which it was created. Nontemporary
MPSState objects are valid on any
MTLCommand on the same device until they are released.