Sample CodemacOSReviewed 2026-07-21View on Apple Developer

Running a machine learning model on the GPU timeline

At a glance

Item Summary
Purpose Dispatch model inference commands with a machine learning pass in a Metal 4 command buffer.
App architecture Objective-C source; Command-line main hands work to MLMatrixMultiplier, which owns matrix objects and MLMatrixMultiplier encoder before Metal 4 machine-learning pipeline on a command buffer.
Main patterns Command-line composition, Encoder object, GPU timeline integration
Scope High-level review of 11 scanned source files and 12 detected declarations; build assets are omitted.

Project structure

Source bundle/
├── Entry point/
│   └── main.m
├── Matrix multiplier/
│   ├── MLMatrixMultiplier.m
│   ├── MLMatrixMultiplier+PipelineCompilation.h
│   ├── MLMatrixMultiplier+PipelineCompilation.m
│   ├── MLMatrixMultiplier+TensorSetup.h
│   ├── MLMatrixMultiplier+TensorSetup.m
│   └── MLMatrixMultiplier.h
└── Matrix/
    ├── Matrix.m
    └── Matrix+TensorUtilities.h

Structure observations

  • The entry/composition boundary and renderer or operation boundary are separate in the source; MLMatrixMultiplier is the principal feature coordinator.
  • GPU/framework work is encoded through native implementation code rather than a standalone .metal file.
  • The tree above is intentionally pruned to composition, resource, and shader files.

Overall architecture

Reference code

Entry point/main.m:44 — feature handoff or setup anchor

    MLMatrixMultiplier *multiplier = [MLMatrixMultiplier new];

Interpretation

This is the dominant control/data path: platform code composes MLMatrixMultiplier; that object controls matrix objects and MLMatrixMultiplier encoder; GPU-visible work ends in Metal 4 machine-learning pipeline on a command buffer. The arrows summarize responsibility transfer, not a claim that every node directly calls the next.

Ownership and state

Ownership evidence

Entry point/main.m:44main creates for a local scope multiplier

    MLMatrixMultiplier *multiplier = [MLMatrixMultiplier new];
Owner Object or state Relationship Mutation authority
main multiplier / MLMatrixMultiplier Creates for a local scope; the diagram uses composition because construction is source-visible. The declaring scope performs setup and replacement.
MLMatrixMultiplier Feature-specific framework and Metal resources Operation-local calls or stored state; exclusive lifetime is not assumed beyond cited evidence. Feature setup/encoding code controls mutation and command submission.

Class and protocol design

Type Responsibility Depends on or conforms to Source
MLMatrixMultiplier encapsulates the feature operation and its framework/GPU resources. NSObject Matrix multiplier/MLMatrixMultiplier+PipelineCompilation.h:12
Matrix (TensorUtilities) adds tensor-extent element counts and index/coordinate conversion helpers used to allocate and address Metal tensors. Matrix, MTLTensorExtents Matrix/Matrix+TensorUtilities.m:10

No local protocol abstraction is present; the sample uses concrete framework, operation, or model types at this boundary.

Access control

Symbol Access Verified effect Design reason
MLMatrixMultiplier header-visible available to translation units that import the header; this is not Swift public. Keep the usable surface no wider than the collaboration requires. (Matrix multiplier/MLMatrixMultiplier+PipelineCompilation.h:12)
MLMatrixMultiplier implementation details implementation-only native helpers, stored state, or registration code stays out of the imported header contract. Hide native implementation details from importing translation units. (Matrix multiplier/MLMatrixMultiplier.m:1)

This sample does not use Swift private, fileprivate, or public for its native boundary. Objective-C/Objective-C++ use header versus implementation placement, C++ uses access specifiers/linkage, Python uses module conventions, and Metal entry points cross a compiled-library boundary; these are not Swift access levels.

Logic ownership and placement

Logic Owning type or file Why it lives there
Encapsulates the feature operation and its framework/GPU resources MLMatrixMultiplierMatrix multiplier/MLMatrixMultiplier+PipelineCompilation.h:12 The type’s callbacks and stored state align with this responsibility.
Matrix objects and MLMatrixMultiplier encoder MLMatrixMultiplier / Entry point/main.m:44 Keeps Metal/framework setup and encoding out of entry or lifecycle code.
Metal 4 machine-learning pipeline on a command buffer Entry point/main.m:44 This sample encodes the operation through framework/native code rather than a standalone .metal file.

Design patterns

Pattern Source evidence Purpose or tradeoff
Command-line composition Entry point/main.m:44 Makes the sample’s matrix objects and MLMatrixMultiplier encoder an explicit, reviewable boundary.
Encoder object Entry point/main.m:44 Makes the sample’s matrix objects and MLMatrixMultiplier encoder an explicit, reviewable boundary.
GPU timeline integration Entry point/main.m:44 Makes the sample’s matrix objects and MLMatrixMultiplier encoder an explicit, reviewable boundary.

Naming conventions

  • Role suffixes make ownership visible: MLMatrixMultiplier.
  • Method names describe setup or encoding actions: init, loadMetalPackage, configureWithMatrix1, encodeAndRunModelInference, productTensor, data, matrixWithRandomRows.
  • Shader entry points use stage/operation names: no standalone Metal shader entry point in this archive.
  • Names favor concrete domain roles and target-local types; no broad public library namespace is introduced.

Architecture takeaways

  • Keep Command-line main focused on composition; MLMatrixMultiplier is the owner of matrix objects and MLMatrixMultiplier encoder.
  • Treat Metal 4 machine-learning pipeline on a command buffer as a separate execution/compilation boundary with explicit resource and data-layout contracts.
  • The verified ownership edge is mainmultiplier; broader exclusive ownership is not inferred.
  • Access-control rationale follows concrete language boundaries rather than translating every header or shader symbol into Swift terms.

Source map

Source file Architectural role
Entry point/main.m entry point or feature implementation
Matrix multiplier/MLMatrixMultiplier.m MLMatrixMultiplier, MLMatrixMultiplier
Matrix/Matrix.m Matrix, Matrix
Matrix multiplier/MLMatrixMultiplier+PipelineCompilation.h MLMatrixMultiplier
Matrix multiplier/MLMatrixMultiplier+PipelineCompilation.m MLMatrixMultiplier
Matrix multiplier/MLMatrixMultiplier+TensorSetup.h MLMatrixMultiplier
Matrix multiplier/MLMatrixMultiplier+TensorSetup.m MLMatrixMultiplier
Matrix multiplier/MLMatrixMultiplier.h MLMatrixMultiplier
Matrix/Matrix+TensorUtilities.h Matrix