Standardmäßig ist der Prompt ausgewählt, der zuerst die Quelle prüft. Sie können zu einem direkten Befehl wechseln oder eine lokale Kopie herunterladen.
Quelldateien prüfen
Lesen Sie SKILL.md und alle von SkillsMP angezeigten Begleitdateien, bevor Sie sich für eine Installation entscheiden.
Mit Codex oder Claude installieren Kopieren Sie diesen Prompt, fügen Sie ihn in Codex, Claude oder einen anderen Assistant ein und lassen Sie die Skill-Seite prüfen und installieren.
Ein direkter Befehl überspringt den Prüf-Prompt. Prüfen Sie die Quelle, bevor Sie ihn ausführen.
MLX Swift is Apple's high-performance machine learning framework designed specifically for Apple Silicon. It provides NumPy-like array operations with lazy evaluation, automatic differentiation, and unified CPU/GPU memory.
import MLX
// Create arrayslet a =MLXArray([1, 2, 3, 4])
let b =MLXArray(0..<12, [3, 4]) // Shape [3, 4]let c =MLXArray.zeros([2, 3])
let d =MLXArray.ones([4, 4], dtype: .float32)
// Random arrays (use MLXRandom namespace or free functions)let uniform =MLXRandom.uniform(0.0..<1.0, [3, 3])
let normal =MLXRandom.normal([100])
let a =MLXArray([1.0, 2.0, 3.0])
let b =MLXArray([4.0, 5.0, 6.0])
// Arithmetic (lazy - not computed until eval)let sum = a + b
let product = a * b
let matmul = a.matmul(b.T)
// Force evaluation
eval(sum, product)
// or
sum.eval()
let a =MLXArray(0..<12, [3, 4])
// Single element
a[0, 1]
// Slicing
a[0...] // All rows
a[..<2] // First 2 rows
a[1..., 2...] // From row 1, column 2 onwards// Advanced indexing
a[.ellipsis, 0] // First column of all dimensions
a[.newAxis, .ellipsis] // Add dimension at front
Shape Manipulation
let a =MLXArray(0..<12, [3, 4])
a.reshaped([4, 3])
a.reshaped(-1, 6) // Infer first dimension
a.T// Transpose
a.transposed(1, 0) // Explicit transpose
a.squeezed() // Remove size-1 dimensions
a.expandedDimensions(axis: 0)
See transforms.md for automatic differentiation details.
Gradient Computation
// Simple gradientlet gradFn = grad { x in
sum(x * x)
}
let g = gradFn(MLXArray([1.0, 2.0, 3.0]))
// Value and gradient togetherlet (value, gradient) = valueAndGrad { x in
sum(x * x)
}(MLXArray([1.0, 2.0, 3.0]))
// Model gradients - valueAndGrad returns a function, call it to get resultslet lossAndGradFn = valueAndGrad(model: model) { model in
model(input)
}
let (loss, grads) = lossAndGradFn(model)
// Common optimizerslet sgd =SGD(learningRate: 0.01, momentum: 0.9)
let adam =Adam(learningRate: 0.001, betas: (0.9, 0.999))
let adamw =AdamW(learningRate: 0.001, weightDecay: 0.01)
// Training step
optimizer.update(model: model, gradients: grads)
eval(model, optimizer)
Compilation for Performance
// Compile a pure array function for faster executionlet compiledOp = compile { (a: MLXArray, b: MLXArray) -> MLXArrayinlet x = a + b
return sum(x * x)
}
// Use compiled versionlet output = compiledOp(arrayA, arrayB)
// Note: compile() works best with pure MLXArray functions.// For models, call model methods directly (they can use internal compilation).
Quaternary Workflow: Wired Memory Coordination
See wired-memory.md for full policy, hysteresis, and admission guidance.
import MLX
let policy =WiredSumPolicy()
// Reservation: participates in admission but does not keep the wired limit high while idle.let weightsTicket = policy.ticket(size: weightsBytes, kind: .reservation)
_=await weightsTicket.start()
// Active work: raises limit while inference runs.let inferenceTicket = policy.ticket(size: kvCacheBytes, kind: .active)
tryawait inferenceTicket.withWiredLimit {
// run model inference
}
_=await weightsTicket.end()
Best Practices
DO
Use lazy evaluation: MLX arrays are computed lazily. Call eval() strategically to control memory and compute.
Batch eval calls: eval(a, b, c) is more efficient than separate calls.
Use @ModuleInfo for all module properties to enable quantization and updates.
Use actors for concurrent code: Encapsulate MLX state within actors for thread safety.
Use namespaced functions: MLXRandom.uniform(), FFT.fft(), Linalg.inv().
Use ticket-based wired memory coordination: Prefer WiredMemoryTicket.withWiredLimit and WiredMemoryManager.shared.
DON'T
Don't share MLXArrays across tasks: MLXArray is NOT Sendable by design.
Don't use deprecated module imports: Use import MLX not import MLXRandom.
Don't forget to eval(): Unevaluated arrays can accumulate large compute graphs.
Don't mutate arrays directly: Use operations that return new arrays.
Don't call deprecated wired-limit APIs: Avoid GPU.withWiredLimit(...) and Memory.withWiredLimit(...).
Deprecated Patterns
If you see...
Use instead...
import MLXRandom
import MLX then MLXRandom.uniform() or free function uniform()
import MLXFFT
import MLX then FFT.fft()
import MLXLinalg
import MLX then Linalg.inv()
GPU.activeMemory
Memory.activeMemory
GPU.withWiredLimit(...)
WiredMemoryTicket(...).withWiredLimit { ... } via WiredMemoryManager