[Web] Support string[] in setPackedFunc() and exceptionally long arrays #16910
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.
There are two changes in this PR.
Change 1: Support
string[]
insetPackedFunc()
Prior to this PR, we cannot pass in
string[]
from typescript to a TVM PackedFunc and need to convert it toTVMArray<TVMString>
(for instance ingetParamsFromCacheByName()
). This may not be the most convenient if the PackedFunc's caller is not internal to tvmjs. Thus, this PR moves the conversion tosetPackedFunc()
instead.Change 2: Support exceptionally long TVM arrays
The second change is dealing with exceptionally long TVM arrays. In cases like passing in a token table, we need to pass in a long
string[]
(in Llama-3's case, of size 128000), leading to JS errorRangeError: Maximum call stack size exceeded
since we treat each string element as an argument, shown inthis.ctx.arrayMake(...inputs)
.This PR sets an empirical call stack limit of 30000 and chunks the array elements in
makeTVMArray()
, converting each chunk to its own TVMArray. Then we concatenate them with the newly implementedruntime.ArrayConcat
that concatenates N TVMArrays.Tested end-to-end in WebLLM.