-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Stream weights to the GPU when loading a model #7994
Conversation
bf9c6fa
to
af89cf0
Compare
5c93720
to
ed74c0e
Compare
tfjs-core/src/io/http.ts
Outdated
return fetchURLs; | ||
} | ||
|
||
// private get loadOptions(): LoadOptions { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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>
?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Matt!
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
orWEBGPU_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 inLoadOptions
. 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.