Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ set(MLX_C_BUILD_EXAMPLES OFF)
FetchContent_Declare(
mlx-c
GIT_REPOSITORY "https://github.com/ml-explore/mlx-c.git"
GIT_TAG "19d3c9c180dddf51be441b55e7f454861899bc93")
GIT_TAG "v0.8.0")
FetchContent_MakeAvailable(mlx-c)

# swift-numerics
Expand Down
2 changes: 1 addition & 1 deletion Source/Cmlx/mlx-c
Submodule mlx-c updated 2 files
+1 −1 CMakeLists.txt
+1 −1 docs/src/conf.py
41 changes: 29 additions & 12 deletions Source/MLXNN/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -509,20 +509,37 @@ open class Module {
modulePath: [String] = []
) throws -> Self {
_checkNotSealed()
try update(parameters: parameters, verify: verify, path: path, modulePath: modulePath) {
m, k, a, v in
a._updateInternal(v)
}
try update(
parameters: parameters, verify: verify, path: path, modulePath: modulePath,
mutate: nil)
return self
}

/// Applies `parameters` to this module and recurses into its children.
///
/// With a `nil` `mutate`, arrays update in place and children recurse through the
/// open `update(parameters:verify:path:modulePath:)` so subclass overrides run.
/// A custom `mutate` (see `materialize()`) recurses directly and skips overrides.
func update(
parameters: ModuleParameters, verify: VerifyUpdate, path: [String] = [],
modulePath: [String] = [],
mutate: (Module, String, MLXArray, MLXArray) -> Void
mutate: ((Module, String, MLXArray, MLXArray) -> Void)?
) throws {
let modulePath = modulePath + [describeType(self)]

func updateChild(
_ module: Module, _ parameters: ModuleParameters, path: [String]
) throws {
if let mutate {
try module.update(
parameters: parameters, verify: verify, path: path,
modulePath: modulePath, mutate: mutate)
} else {
try module.update(
parameters: parameters, verify: verify, path: path, modulePath: modulePath)
}
}

func apply(
key: String, path: [String], _ item: ModuleItem, _ value: NestedItem<String, MLXArray>
) throws {
Expand All @@ -542,7 +559,11 @@ open class Module {
path: path, modules: modulePath, expectedShape: p.shape,
actualShape: newArray.shape)
}
mutate(self, key, p, newArray)
if let mutate {
mutate(self, key, p, newArray)
} else {
p._updateInternal(newArray)
}

case (.value(.parameters), .none):
if Self.parameterIsValid(key) {
Expand Down Expand Up @@ -586,14 +607,10 @@ open class Module {
}

case (.value(.module(let module)), .dictionary(let values)):
try module.update(
parameters: NestedDictionary(values: values), verify: verify, path: path,
modulePath: modulePath, mutate: mutate)
try updateChild(module, NestedDictionary(values: values), path: path)

case (.value(.module(let module)), .none):
try module.update(
parameters: NestedDictionary(), verify: verify, path: path,
modulePath: modulePath, mutate: mutate)
try updateChild(module, NestedDictionary(), path: path)

case (.none, .none), (.value(.none), .none), (.value(.other(_)), .none):
break
Expand Down
34 changes: 34 additions & 0 deletions Tests/MLXTests/ModuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,40 @@ class ModuleTests: XCTestCase {
XCTAssertEqual(collect["interior2.children.1"], "Linear")
}

func testUpdateParametersCallsChildOverride() throws {
// a child's update(parameters:verify:path:modulePath:) override runs
// when the update starts at an ancestor
class RecordingLinear: Linear {
var updateCount = 0

override func update(
parameters: ModuleParameters, verify: VerifyUpdate, path: [String] = [],
modulePath: [String] = []
) throws -> Self {
updateCount += 1
return try super.update(
parameters: parameters, verify: verify, path: path, modulePath: modulePath)
}
}

class Parent: Module {
let child = RecordingLinear(2, 2)
let list = [RecordingLinear(2, 2)]
}

let m = Parent()
let weight = MLXArray.ones([2, 2])
m.update(
parameters: .unflattened([
"child.weight": weight, "list.0.weight": weight,
]))

XCTAssertEqual(m.child.updateCount, 1)
XCTAssertEqual(m.list[0].updateCount, 1)
assertEqual(m.child.weight, weight)
assertEqual(m.list[0].weight, weight)
}

func testUpdateModuleSameType() throws {
// set a single module with same type
let m = newStructureModule()
Expand Down
Loading