Skip to content

Commit 7ad13f6

Browse files
Fix CPU MLX shim: drop duplicate Module.children/__contains__ (#938)
#934 added a structure-preserving Module.children() (non-Module list/dict items map to {}) plus a vars-based __contains__, along with the smoke test that pins that behavior. #935 landed minutes later and re-added its own children() and __contains__ without seeing #934's, so the second definitions silently shadowed the first: children() returned raw containers and __contains__ resolved via getattr (which fires @Property descriptors). That broke test_module_children_preserves_direct_child_tree. Remove the duplicate definitions and keep #934's versions. The full MLX suite, including the output-head resolution paths #935 targeted, stays green. Co-authored-by: danielhanchen <unslothai@gmail.com>
1 parent 118c8c9 commit 7ad13f6

1 file changed

Lines changed: 0 additions & 26 deletions

File tree

tests/mlx_simulation/mlx_nn_stub.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -143,32 +143,6 @@ def named_modules(self, prefix=""):
143143
sub_prefix = f"{prefix}.{attr_name}.{k}" if prefix else f"{attr_name}.{k}"
144144
yield from item.named_modules(sub_prefix)
145145

146-
def __contains__(self, key):
147-
"""MLX's Module derives from dict, so ``"bias" in module`` is valid on
148-
any module and checks the parameter/child entry. Mirror that here by
149-
answering for tensor- or Module-valued attributes; subclasses whose
150-
parameters live on a wrapped torch module (Linear) override this."""
151-
value = getattr(self, key, None)
152-
return isinstance(value, (torch.Tensor, Module))
153-
154-
def children(self):
155-
"""Direct sub-structure keyed by attribute name, mirroring MLX's
156-
``Module.children()``: Module-valued attributes map to the Module,
157-
list/dict containers of Modules map to the container itself (MLX
158-
returns the nested structure; consumers type-check the values, so
159-
containers fall through their exact-type filters unchanged)."""
160-
out = {}
161-
for attr_name, attr_val in vars(self).items():
162-
if isinstance(attr_val, Module):
163-
out[attr_name] = attr_val
164-
elif isinstance(attr_val, (list, tuple)):
165-
if any(isinstance(item, Module) for item in attr_val):
166-
out[attr_name] = attr_val
167-
elif isinstance(attr_val, dict):
168-
if any(isinstance(item, Module) for item in attr_val.values()):
169-
out[attr_name] = attr_val
170-
return out
171-
172146
def freeze(self, *, recurse=True, keys=None):
173147
return self
174148

0 commit comments

Comments
 (0)