Skip to content

Add support for SafeTensors#5

Open
mtavenrath wants to merge 5 commits intomainfrom
safetensor_support
Open

Add support for SafeTensors#5
mtavenrath wants to merge 5 commits intomainfrom
safetensor_support

Conversation

@mtavenrath
Copy link
Copy Markdown
Contributor

Being able to load SafeTensors allows to share weights between WebNN and Torch to allow file duplication.

@mtavenrath mtavenrath requested a review from tarekziade March 9, 2026 15:34
@mtavenrath mtavenrath removed the request for review from tarekziade March 16, 2026 15:56
@theHamsta
Copy link
Copy Markdown

theHamsta commented Mar 22, 2026

Why not just get the weights via python dict since we're loading weights into memory anyway? then it would also work for a Pytorch state dict and you could share the weights with a pytorch that also loads the same model.

The advantage of this API would be if we DMA or stream to device in future to avoid using RAM (which is probably want we want).

Cargo.toml Outdated
rustnn = { git = "https://github.com/rustnn/rustnn", branch = "main" }
serde_json = "1.0"
webnn-graph = { git = "https://github.com/rustnn/webnn-graph", branch = "main" }
safetensors = "0.4"
Copy link
Copy Markdown

@theHamsta theHamsta Mar 22, 2026

Choose a reason for hiding this comment

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

AI keeps adding old versions. Most recent version is safetensors = "0.7.0"

Copy link
Copy Markdown
Contributor Author

@mtavenrath mtavenrath Mar 22, 2026

Choose a reason for hiding this comment

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

@gedoensmax added this feature to share weights between pytorch and webnn. It is an additional feature, not the only exclusive way to load weights in a fast manner.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fully AI written - just told it to let me provide a safetensor file and read the bytes out of that. I have not looked at how this works at all and focused on the pyhon pieces.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

but what do you mean by sharing? Do you want

  • to have a safetensors file and then either read by pytorch or RustNN
  • or do you want to load safetensors and then use the weights both by pytorch and RustNN in the same execution? for the latter I would just use Python safetensors and accept here a weight dict similar to pytorch

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The first option. I want to be able to load the weights into webNN that are already present in safetensors for pytorch. The idea was to let rust control how to load the weights from dist instead of loading them in python and passing them as dict.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@mtavenrath to make the change to 0.7 since i can not edit this branch on the repo.

Copy link
Copy Markdown

@theHamsta theHamsta left a comment

Choose a reason for hiding this comment

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

Approved when the newest safetensors = "0.7" is used

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.

3 participants