Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 64 additions & 32 deletions Source/MLXOptimizers/Optimizers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,16 @@ public struct TupleState: Updatable {
/// - <doc:MLXOptimizers>
open class SGD: OptimizerBaseArrayState {

var learningRate: Float
var momentum: Float = 0
var weightDecay: Float = 0
var dampening: Float = 0
var nesterov = false
/// The learning rate
public var learningRate: Float

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes look good to me, but I wonder about changing non-MLXArray values during the run and interaction with grad()

@awni will this work ok?

I realized there was a scheduler piece that happened on the python side and we don't have an equivalent here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be fine to change non-mlx arrays. The main (and I think only time) you need to be careful there is when using mx.compile. In Python we detect if constants (non arrays) change and recompile, but I don't think we do it in Swift yet.

In Python everything is public by default so this kind of mimics that. I would say its ok to do it here. We do have schedulers in Python which is the typical way for updating a learning rate (and other variables). To the extent that the interface has to change for the schedulers that may be something to watch out for (e.g. if we add schedulers will we break the API that this exposes?).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To the extent that the interface has to change for the schedulers that may be something to watch out for (e.g. if we add schedulers will we break the API that this exposes?).

Possibly, but maybe not. It looks like the optimizer base gets a step so we could do something like this:

public var step: MLXArray

public var learningRate: Float {
    get {
        if let scheduledLearningRate {
            return scheduledLearningRate(step)
        } else {
            return _learningRate
        }
    }
    set {
        if scheduledLearningRate != nil {
            fatalError("cannot set learningRate with scheduler")
        }
        _learningRate = newValue
     }
}

private var _learningRate: Float = 0

public var scheduledLearningRate: ((MLXArray) -> MLXArray)?

That would keep the same API while adding an optional scheduled parameter.

/// Momentum strength
public var momentum: Float = 0
/// Weight decay (L2 penalty)
public var weightDecay: Float = 0
/// Dampening for momentum
public var dampening: Float = 0
/// Enables Nesterov momentum
public var nesterov = false

/// Initialize the SGD optimizer.
///
Expand Down Expand Up @@ -184,9 +189,12 @@ open class SGD: OptimizerBaseArrayState {
/// - <doc:MLXOptimizers>
open class RMSprop: OptimizerBaseArrayState {

var learningRate: Float
var alpha: Float = 0.99
var eps: Float = 1e-8
/// The learning rate
public var learningRate: Float
/// The smoothing constant
public var alpha: Float = 0.99
/// The epsilon added to the denominator to improve numerical stability
public var eps: Float = 1e-8

/// Initialize the optimizer.
/// - Parameters:
Expand Down Expand Up @@ -220,8 +228,10 @@ open class RMSprop: OptimizerBaseArrayState {
/// - <doc:MLXOptimizers>
open class AdaGrad: OptimizerBaseArrayState {

var learningRate: Float
var eps: Float = 1e-8
/// The learning rate
public var learningRate: Float
/// The epsilon added to the denominator to improve numerical stability
public var eps: Float = 1e-8

/// Initialize the optimizer.
/// - Parameters:
Expand Down Expand Up @@ -252,9 +262,12 @@ open class AdaGrad: OptimizerBaseArrayState {
/// - <doc:MLXOptimizers>
open class AdaDelta: OptimizerBase<TupleState> {

var learningRate: Float
var rho: Float = 0.99
var eps: Float = 1e-6
/// The learning rate
public var learningRate: Float
/// The coefficient used for computing a running average of squared gradients
public var rho: Float = 0.99
/// The epsilon added to the denominator to improve numerical stability
public var eps: Float = 1e-6

/// Initialize the optimizer.
/// - Parameters:
Expand Down Expand Up @@ -298,9 +311,12 @@ open class AdaDelta: OptimizerBase<TupleState> {
/// - <doc:MLXOptimizers>
open class Adam: OptimizerBase<TupleState> {

var learningRate: Float
var betas: (Float, Float) = (0.9, 0.999)
var eps: Float = 1e-8
/// The learning rate
public var learningRate: Float
/// The coefficients used for computing running averages of the gradient and its square
public var betas: (Float, Float) = (0.9, 0.999)
/// The epsilon added to the denominator to improve numerical stability
public var eps: Float = 1e-8

/// Initialize the optimizer.
/// - Parameters:
Expand Down Expand Up @@ -343,7 +359,8 @@ open class Adam: OptimizerBase<TupleState> {
/// - <doc:MLXOptimizers>
open class AdamW: Adam {

var weightDecay: Float = 0.01
/// The weight decay
public var weightDecay: Float = 0.01

/// Initialize the optimizer.
/// - Parameters:
Expand Down Expand Up @@ -379,9 +396,12 @@ open class AdamW: Adam {
/// - <doc:MLXOptimizers>
open class Adamax: OptimizerBase<TupleState> {

var learningRate: Float
var betas: (Float, Float) = (0.9, 0.999)
var eps: Float = 1e-8
/// The learning rate
public var learningRate: Float
/// The coefficients used for computing running averages of the gradient and its square
public var betas: (Float, Float) = (0.9, 0.999)
/// The epsilon added to the denominator to improve numerical stability
public var eps: Float = 1e-8

/// Initialize the optimizer.
/// - Parameters:
Expand Down Expand Up @@ -427,9 +447,12 @@ open class Adamax: OptimizerBase<TupleState> {
/// - <doc:MLXOptimizers>
open class Lion: OptimizerBaseArrayState {

var learningRate: Float
var betas: (Float, Float) = (0.9, 0.999)
var weightDecay: Float = 0.0
/// The learning rate
public var learningRate: Float
/// The coefficients used for computing running averages of the gradient and its square
public var betas: (Float, Float) = (0.9, 0.999)
/// The weight decay
public var weightDecay: Float = 0.0

/// Initialize the optimizer.
/// - Parameters:
Expand Down Expand Up @@ -470,15 +493,24 @@ open class Lion: OptimizerBaseArrayState {
/// - <doc:MLXOptimizers>
open class Adafactor: OptimizerBase<Adafactor.State> {

var learningRate: Float? = nil
var eps: (Float, Float) = (1e-30, 1e-3)
var clipThreshold: Float = 1
var decayRate: Float = -0.8
var beta1: Float? = nil
var weightDecay: Float = 0
var scaleParameter = true
var relativeStep = true
var warmupInit = false
/// The learning rate
public var learningRate: Float? = nil
/// The first term is added to the square of the gradients to improve numerical stability
public var eps: (Float, Float) = (1e-30, 1e-3)
/// Clips the unscaled update
public var clipThreshold: Float = 1
/// Coefficient for the running average of the squared gradient
public var decayRate: Float = -0.8
/// If set then the first moment will be used
public var beta1: Float? = nil
/// The weight decay
public var weightDecay: Float = 0
/// If `true` the `learningRate` will be scaled by `max(eps.0, RMS(parameter))`
public var scaleParameter = true
/// If `true` the `learningRate` will be ignored and the relative step size will be computed
public var relativeStep = true
/// If `true` the relative step size will be calculated by the current step
public var warmupInit = false

public struct State: Updatable {
var step = MLXArray(0)
Expand Down