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

Function tree_replace

python/src/trees.cpp:219–234  ·  view source on GitHub ↗

Replace all the arrays from the src values with the dst values in the tree

Source from the content-addressed store, hash-verified

217
218// Replace all the arrays from the src values with the dst values in the tree
219void tree_replace(
220 nb::object& tree,
221 const std::vector<mx::array>& src,
222 const std::vector<mx::array>& dst) {
223 std::unordered_map<uintptr_t, mx::array> src_to_dst;
224 for (int i = 0; i < src.size(); ++i) {
225 src_to_dst.insert({src[i].id(), dst[i]});
226 }
227 tree_visit_update(tree, [&](nb::handle node) {
228 auto arr = nb::cast<mx::array>(node);
229 if (auto it = src_to_dst.find(arr.id()); it != src_to_dst.end()) {
230 return nb::cast(it->second);
231 }
232 return nb::cast(arr);
233 });
234}
235
236std::vector<mx::array> tree_flatten(nb::handle tree, bool strict /* = true */) {
237 std::vector<mx::array> flat_tree;

Callers 1

call_implMethod · 0.85

Calls 5

tree_visit_updateFunction · 0.85
sizeMethod · 0.45
insertMethod · 0.45
findMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected