Skip to content

Convert MLX shared variable updates back to numpy arrays - #2378

Open
jessegrabowski wants to merge 4 commits into
pymc-devs:mainfrom
jessegrabowski:mlx-shared-update-filter
Open

Convert MLX shared variable updates back to numpy arrays#2378
jessegrabowski wants to merge 4 commits into
pymc-devs:mainfrom
jessegrabowski:mlx-shared-update-filter

Conversation

@jessegrabowski

Copy link
Copy Markdown
Member

Function.__call__ writes an update straight into the shared variable's storage slot, bypassing the container's filter, so after one MLX call the shared variable holds an mx.array instead of an ndarray. get_value() then returns a device array even with borrow=False, and passing that shared variable to a function compiled for another backend fails outright - a plain numba graph raises a TypingError on it.

JITLinker.output_filter already existed for this and was dead code, called by nothing. It's now applied to the outputs named in fgraph.update_mapping, and MLXLinker overrides it. Values returned to the caller still come back as mx.array; only what gets stored back in a container is converted. Backends that don't override the hook run the same thunk as before.

The value written into a shared variable's container outlives the call and may later be read by a function compiled for another backend, so JITLinker.output_filter is applied to update outputs while returned outputs keep their native backend type.
@jessegrabowski

Copy link
Copy Markdown
Member Author

@juanitorduz tagged you because i was thinking about #2375, wondering if the solution to that wasn't also similar to this, adjustment of the linker behavior in a certain case for a certain backend.

@ricardoV94

ricardoV94 commented Aug 25, 2026

Copy link
Copy Markdown
Member

Check out #2285

You don't always care about cross backend compatibility so it offers an escape. It's also lazy, only done when a different backend would actually read it. Still worrried about overhead though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants