Skip to content

use ReerCodable macro to allow for default values#106

Open
davidkoski wants to merge 6 commits intomainfrom
codable-macro
Open

use ReerCodable macro to allow for default values#106
davidkoski wants to merge 6 commits intomainfrom
codable-macro

Conversation

@davidkoski
Copy link
Copy Markdown
Collaborator

Proposed changes

Looking at moving to a Codable macro that makes it easier to integrate with JSON configuration files. Specifically:

  • easy default values
  • easy to override json keys

These are both possible with standard Swift Coding but are unwieldy in the quantity we need here. See:

For an example. This changes:

public struct YourModelConfiguration: Codable, Sendable {
    public let hiddenSize: Int

    // use this pattern for values that need defaults
    public let _layerNormEps: Float?
    public var layerNormEps: Float { _layerNormEps ?? 1e-6 }

    enum CodingKeys: String, CodingKey {
        case hiddenSize = "hidden_size"
        case _layerNormEps = "layer_norm_eps"
    }
}

to this:

@Codable
public struct YourModelConfiguration: Sendable {
    @CodingKey("hidden_size") public var hiddenSize: Int
    @CodingKey("layer_norm_eps") public var layerNormEps: Float = 1e-6
}

The downside is that we are using macros (there is some additional build cost).

There was a previous attempt here: ml-explore/mlx-swift-examples#316. This is roughly the same approach but doesn't attempt to port all the configuration at once. Keeping that up-to-date was too much. Once this is in we can migrate the configuration in chunks.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

@davidkoski davidkoski requested a review from awni February 12, 2026 21:41
import ReerCodable

// port of https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/models/cohere.py
// port of https://github.com/ml-explore/mlx-lm/tree/main/mlx_lm/models/cohere.py
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Once #105 is in, this should be a NOP.

if let mlpBias = try container.decodeIfPresent(Bool.self, forKey: .mlpBias) {
self.mlpBias = mlpBias
}
public func didDecode(from decoder: any Decoder) throws {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is an interesting one because it has some logic to post-process the configuration.

Copy link
Copy Markdown
Member

@angeloskath angeloskath left a comment

Choose a reason for hiding this comment

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

Just 🚀

@davidkoski
Copy link
Copy Markdown
Collaborator Author

This is on hold for now -- I like the approach but am not ready to commit yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants