-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[Web] Improve large tensor loading in wasm runtime #19771
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
Open
MakotoUwu
wants to merge
1
commit into
apache:main
Choose a base branch
from
MakotoUwu:ole-34/apache-tvm-webgpu-runtime-gemma4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| export interface TensorCacheChunk { | ||
| outerCount: number; | ||
| sourceByteOffset: number; | ||
| sourceByteLength: number; | ||
| targetByteOffset: number; | ||
| targetByteLength: number; | ||
| } | ||
|
|
||
| export interface TensorCacheChunkPlan { | ||
| chunks: Array<TensorCacheChunk>; | ||
| } | ||
|
|
||
| // Tensor view offsets are currently marshalled into TVM JS's wasm32 runtime. | ||
| const wasm32AddressSpaceBytes = 0x100000000; | ||
|
|
||
| function greatestCommonDivisor(lhs: number, rhs: number): number { | ||
| while (rhs !== 0) { | ||
| const remainder = lhs % rhs; | ||
| lhs = rhs; | ||
| rhs = remainder; | ||
| } | ||
| return lhs; | ||
| } | ||
|
|
||
| /** | ||
| * Plan outer-dimension chunks whose encoded and decoded sizes stay bounded. | ||
| * | ||
| * Returns undefined when outer-dimension chunking cannot satisfy the size and | ||
| * target-alignment constraints. Callers can then retain the full-record path. | ||
| */ | ||
| export function planTensorCacheChunks( | ||
| shape: Array<number>, | ||
| sourceBytes: number, | ||
| targetBytes: number, | ||
| maxChunkBytes: number, | ||
| targetAlignmentBytes = 1, | ||
| ): TensorCacheChunkPlan | undefined { | ||
| const outerDim = shape[0]; | ||
| if ( | ||
| shape.length === 0 || | ||
| !Number.isSafeInteger(outerDim) || | ||
| outerDim <= 0 || | ||
| !Number.isSafeInteger(sourceBytes) || | ||
| sourceBytes <= 0 || | ||
| !Number.isSafeInteger(targetBytes) || | ||
| targetBytes <= 0 || | ||
| targetBytes >= wasm32AddressSpaceBytes || | ||
| !Number.isSafeInteger(maxChunkBytes) || | ||
| maxChunkBytes <= 0 || | ||
| !Number.isSafeInteger(targetAlignmentBytes) || | ||
| targetAlignmentBytes <= 0 || | ||
| sourceBytes % outerDim !== 0 || | ||
| targetBytes % outerDim !== 0 || | ||
| targetBytes % targetAlignmentBytes !== 0 | ||
| ) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const sourceStrideBytes = sourceBytes / outerDim; | ||
| const targetStrideBytes = targetBytes / outerDim; | ||
| const maxStrideBytes = Math.max(sourceStrideBytes, targetStrideBytes); | ||
| if (maxStrideBytes > maxChunkBytes) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const rowsPerAlignment = | ||
| targetAlignmentBytes / | ||
| greatestCommonDivisor(targetStrideBytes, targetAlignmentBytes); | ||
| const maxOuterCount = Math.floor(maxChunkBytes / maxStrideBytes); | ||
| const chunkOuterCount = | ||
| maxOuterCount - (maxOuterCount % rowsPerAlignment); | ||
| if (chunkOuterCount <= 0) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const chunks = new Array<TensorCacheChunk>(); | ||
| for (let outerOffset = 0; outerOffset < outerDim; outerOffset += chunkOuterCount) { | ||
| const outerCount = Math.min(chunkOuterCount, outerDim - outerOffset); | ||
| chunks.push({ | ||
| outerCount, | ||
| sourceByteOffset: outerOffset * sourceStrideBytes, | ||
| sourceByteLength: outerCount * sourceStrideBytes, | ||
| targetByteOffset: outerOffset * targetStrideBytes, | ||
| targetByteLength: outerCount * targetStrideBytes, | ||
| }); | ||
| } | ||
|
|
||
| return { chunks }; | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.