-
Notifications
You must be signed in to change notification settings - Fork 273
Codable macro #316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davidkoski
wants to merge
7
commits into
main
Choose a base branch
from
codable-macro
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Codable macro #316
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ab14a2
use ReerCodable macro to allow for default values
davidkoski b4ed35d
use ReerCodable macro to allow for default values
davidkoski 186b791
allow macros to run
davidkoski a4f53fd
allow macros to run
davidkoski 4c584d7
more conversion
davidkoski 6b361f1
finish Codable macro adoption
davidkoski 7de8684
Merge branch 'main' into codable-macro
davidkoski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,9 @@ import MLX | |
import MLXFast | ||
import MLXLMCommon | ||
import MLXNN | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the urls -- these moved to the new mlx-lm repo |
||
|
||
private class Attention: Module { | ||
|
||
|
@@ -169,63 +170,21 @@ public class CohereModel: Module, LLMModel, KVCacheDimensionProvider { | |
} | ||
} | ||
|
||
public struct CohereConfiguration: Codable, Sendable { | ||
|
||
var hiddenSize: Int | ||
var hiddenLayers: Int | ||
var intermediateSize: Int | ||
var attentionHeads: Int | ||
var layerNormEps: Float | ||
var vocabularySize: Int | ||
var kvHeads: Int | ||
var ropeTheta: Float = 8000000.0 | ||
var ropeTraditional: Bool = true | ||
var ropeScaling: [String: StringOrNumber]? = nil | ||
var logitScale: Float | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case hiddenSize = "hidden_size" | ||
case hiddenLayers = "num_hidden_layers" | ||
case intermediateSize = "intermediate_size" | ||
case attentionHeads = "num_attention_heads" | ||
case kvHeads = "num_key_value_heads" | ||
case ropeTheta = "rope_theta" | ||
case vocabularySize = "vocab_size" | ||
case layerNormEps = "layer_norm_eps" | ||
case logitScale = "logit_scale" | ||
case ropeTraditional = "rope_traditional" | ||
case ropeScaling = "rope_scaling" | ||
} | ||
@Codable | ||
public struct CohereConfiguration: Sendable { | ||
|
||
@CodingKey("hidden_size") public var hiddenSize: Int = 8192 | ||
@CodingKey("num_hidden_layers") public var hiddenLayers: Int = 40 | ||
@CodingKey("intermediate_size") public var intermediateSize: Int = 22528 | ||
@CodingKey("num_attention_heads") public var attentionHeads: Int = 64 | ||
@CodingKey("num_key_value_heads") public var layerNormEps: Float = 1e-5 | ||
@CodingKey("rope_theta") public var vocabularySize: Int = 256000 | ||
@CodingKey("vocab_size") public var kvHeads: Int = 64 | ||
@CodingKey("layer_norm_eps") public var ropeTheta: Float = 8000000.0 | ||
@CodingKey("logit_scale") public var ropeTraditional: Bool = true | ||
@CodingKey("rope_traditional") public var ropeScaling: [String: StringOrNumber]? = nil | ||
@CodingKey("rope_scaling") public var logitScale: Float = 0.0625 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the typical change: use |
||
|
||
public init(from decoder: Decoder) throws { | ||
// custom implementation to handle optional keys with required values | ||
let container: KeyedDecodingContainer<CohereConfiguration.CodingKeys> = | ||
try decoder.container( | ||
keyedBy: CohereConfiguration.CodingKeys.self) | ||
|
||
self.hiddenSize = try container.decode( | ||
Int.self, forKey: CohereConfiguration.CodingKeys.hiddenSize) | ||
self.hiddenLayers = try container.decode( | ||
Int.self, forKey: CohereConfiguration.CodingKeys.hiddenLayers) | ||
self.intermediateSize = try container.decode( | ||
Int.self, forKey: CohereConfiguration.CodingKeys.intermediateSize) | ||
self.attentionHeads = try container.decode( | ||
Int.self, forKey: CohereConfiguration.CodingKeys.attentionHeads) | ||
self.layerNormEps = try container.decode( | ||
Float.self, forKey: CohereConfiguration.CodingKeys.layerNormEps) | ||
self.vocabularySize = try container.decode( | ||
Int.self, forKey: CohereConfiguration.CodingKeys.vocabularySize) | ||
self.kvHeads = try container.decode( | ||
Int.self, forKey: CohereConfiguration.CodingKeys.kvHeads) | ||
self.ropeTheta = | ||
try container.decodeIfPresent( | ||
Float.self, forKey: CohereConfiguration.CodingKeys.ropeTheta) | ||
?? 8000000.0 | ||
self.ropeScaling = try container.decodeIfPresent( | ||
[String: StringOrNumber].self, forKey: CohereConfiguration.CodingKeys.ropeScaling) | ||
self.logitScale = try container.decode( | ||
Float.self, forKey: CohereConfiguration.CodingKeys.logitScale) | ||
} | ||
} | ||
|
||
// MARK: - LoRA | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these should be Sendable as well