Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream weights to the GPU when loading a model #7994

Merged
merged 16 commits into from
Nov 28, 2023

Conversation

mattsoulanille
Copy link
Member

When downloading model weight data, slice it into weight tensors and push them to the GPU eagerly. This avoids storing an extra copy of the weights on CPU, allowing for larger models (1.3B to possibly ~6.7B or larger) to be loaded without causing a V8 OOM crash.

When streaming the weights, check CPU_HANDOFF_SIZE_THRESHOLD or WEBGPU_CPU_HANDOFF_SIZE_THRESHOLD to determine whether the weight should be sent to GPU or remain on CPU.

This feature is guarded by the streamWeights option in LoadOptions. Since most of TFJS's graph model saving relies on the CPU copy of the model, model saving is disabled when the model was streamed (i.e. it will throw an error since the weights ArrayBuffer is missing).

To see the logs from the Cloud Build CI, please join either our discussion or announcement mailing list.

return fetchURLs;
}

// private get loadOptions(): LoadOptions {
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove?

* Whether to stream the model directly to the backend or cache all its
* weights on CPU. Useful for large models.
*/
streamWeights?: boolean;
Copy link
Collaborator

Choose a reason for hiding this comment

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

this flag is the same name as the function streamWeights?: () => ReadableStream<ArrayBuffer>?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but they're part of different interfaces. One is on the ModelArtifacts and is the actual function that, when called, will start streaming the weights. It's internal, and users shouldn't really need to access it.

The other is on the LoadOptions interface, and it's used to configure whether the model loader should stream the weights or load them normally. It's exposed to the user.

I can rename the one on ModelArtifacts to something like startWeightsStream or streamWeightsData.

Copy link
Collaborator

@Linchenn Linchenn left a comment

Choose a reason for hiding this comment

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

Thanks Matt!

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.

None yet

3 participants