Skip to content

Add metadata to exported functions#3682

Open
Ritesh102000 wants to merge 1 commit into
ml-explore:mainfrom
Ritesh102000:export-function-metadata
Open

Add metadata to exported functions#3682
Ritesh102000 wants to merge 1 commit into
ml-explore:mainfrom
Ritesh102000:export-function-metadata

Conversation

@Ritesh102000

@Ritesh102000 Ritesh102000 commented Jun 14, 2026

Copy link
Copy Markdown

Proposed changes

This adds support for saving metadata along with an exported function, as requested in #2410.

  • mx.export_function(..., metadata={...}) and mx.exporter(..., metadata={...}) store a dict[str, str] in the .mlxfn header.
  • mx.import_function(file, return_metadata=True) returns the imported function together with its metadata.
  • The metadata is also accessible from C++ through ImportedFunction::metadata().

I kept the metadata as a string-to-string dictionary to match the existing save_safetensors / load metadata API. The issue mentioned possibly allowing Union[str, int, float] values — happy to widen it if you'd prefer, but I went with string keys/values for consistency. Passing metadata together with a callback raises, since it is only written when exporting to a file.

Closes #2410

Checklist

Add an optional string-to-string `metadata` dictionary to
`export_function`/`exporter`, serialized into the `.mlxfn` header, and a
`return_metadata` flag on `import_function` to read it back. This mirrors
the existing `save_safetensors`/`load` metadata convention. The metadata
is also exposed in C++ via `ImportedFunction::metadata()`.

Closes ml-explore#2410
Comment thread mlx/export.cpp
auto function_count = deserialize<int>(is);
ftable->shapeless = deserialize<bool>(is);
auto metadata_pairs =
deserialize<std::vector<std::pair<std::string, std::string>>>(is);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen when importing functions exported from previous versions without metadata support? (There is no need to be compatible but I'm curious about the behavior)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature request] Support saving meta data to mlxfn files and reading them back

2 participants