-
Notifications
You must be signed in to change notification settings - Fork 270
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
base: main
Are you sure you want to change the base?
Codable macro #316
Changes from all commits
8ab14a2
b4ed35d
186b791
a4f53fd
4c584d7
6b361f1
7de8684
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ import MLX | |
import MLXLinalg | ||
import MLXNN | ||
|
||
public struct PoolingConfiguration: Codable { | ||
public struct PoolingConfiguration: Codable, Sendable { | ||
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. All of these should be Sendable as well |
||
public let dimension: Int | ||
public let poolingModeClsToken: Bool | ||
public let poolingModeMeanTokens: Bool | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,9 @@ import Foundation | |
import MLX | ||
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 { | ||
|
||
|
@@ -168,63 +169,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 | ||
|
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.
This is how we tell CI to trust the macro package -- it runs in a sandbox but still requires some permission.