Repository navigation
js: dispose the graph model output after squeezing it - #1467
Open
Dev-next-gen wants to merge 1 commit into
Open
Dev-next-gen wants to merge 1 commit into
Dev-next-gen wants to merge 1 commit into
Conversation
`Model.predict` squeezes the tensor returned by `executeAsync` and disposes the squeezed tensor, but never the tensor it was squeezed from. `squeeze` returns a new tensor that shares the data of its input, so the original output stays registered in the tfjs engine and its data is never released: `tf.memory().numTensors` grows by one on every prediction, for the lifetime of the `Magika` instance. Dispose it explicitly and add a regression test asserting that repeated `identifyBytes` calls leave the tensor count unchanged.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While reading
js/src/model.tsI noticed thatModel.predictdisposesmodelInput,maxScoreIndexTensorandmodelOutput, but not the tensor thatexecuteAsyncactually returned.tf.squeezebuilds a new tensor over the same data, so the original output stays registered in the tfjs engine and its data bucket keeps a reference. One tensor is leaked per prediction, for the lifetime of theMagikainstance.I checked it on
standard_v3_3with the Node binding, callingidentifyBytesrepeatedly on the same small buffer and readingtf.memory().numTensorsafter a warm-up call:With
tf.dispose(rawOutput)added, the same loop stays flat at the baseline. This matters most for the long-lived cases: the website demo keeps oneMagikaaround for the whole page, andmagika-jsscans every path given on the command line with a single instance, so on WebGL this is retained GPU memory that only goes away when the tab does.The reads of
rawScoreswere already done before the existingdispose()calls, so the new one changes nothing for the returned prediction;tf.disposeis used rather than.dispose()so theTensor | Tensor[]return type ofexecuteAsyncstays handled the way the currentas anycast leaves it.I also added a regression test to
js/test/magika.test.ts. It fails onmain(Expected: 75, Received: 80) and passes with the fix. The full suite is green: 9823 tests, 4 suites, Node 22,yarn test.AI tools used