MCPcopy Create free account
hub / github.com/ml-explore/mlx / test_update_modules

Method test_update_modules

python/tests/test_nn.py:295–331  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

293 m.update(updates)
294
295 def test_update_modules(self):
296 m = nn.Sequential(nn.Linear(3, 3), nn.Linear(3, 3))
297
298 # Updating non-existent modules should not be allowed by default
299 with self.assertRaises(ValueError):
300 m = m.update_modules({"values": [0, 1]})
301
302 # Update wrong types
303 with self.assertRaises(ValueError):
304 m = m.update_modules({"layers": [0, 1]})
305
306 class MyModule(nn.Module):
307 def __init__(self):
308 super().__init__()
309 self.test = mx.array(1.0)
310 self.list = [mx.array(1.0), mx.array(2.0)]
311
312 m = MyModule()
313 with self.assertRaises(ValueError):
314 m = m.update_modules({"test": "hi"})
315 with self.assertRaises(ValueError):
316 m = m.update_modules({"list": ["hi"]})
317
318 # Allow updating a strict subset
319 m = nn.Sequential(nn.Linear(3, 3), nn.Linear(3, 3))
320 m.update_modules({"layers": [{}, nn.Linear(3, 4)]})
321 self.assertEqual(m.layers[1].weight.shape, (4, 3))
322
323 # Using leaf_modules in the update should always work
324 class MyModel(nn.Module):
325 def __init__(self):
326 super().__init__()
327 self.stuff = [nn.Linear(2, 2), 0, nn.Linear(2, 2)]
328 self.more_stuff = {"hi": nn.Linear(2, 2), "bye": 0}
329
330 m = MyModel()
331 m.update_modules(m.leaf_modules())
332
333 def test_parameter_deletion(self):
334 m = nn.Linear(32, 32)

Callers

nothing calls this directly

Calls 4

MyModuleClass · 0.85
MyModelClass · 0.85
update_modulesMethod · 0.80
leaf_modulesMethod · 0.80

Tested by

no test coverage detected