Skip to content

Conversation

@akaashrp
Copy link
Contributor

@akaashrp akaashrp commented Nov 2, 2025

This PR fixes scope issues caused by the different flow for q0f32 models in ArrayDecodeStorage. Currently, for q0f32 models, during the fetchTensorCacheInternal call in runtime.ts, the loop for loading shards from the cache fails due to scope issues. Specifically, the arrayDecodeStorage call causes the following error:

Uncaught (in promise) Error: Must call beginScope to use functions that returns TVM objects
at RuntimeContext.attachToCurrentScope ((index):7792:63)
at Instance.retValueToJS ((index):9247:50)
at RuntimeContext.packedFunc [as arrayDecodeStorage] ((index):9169:38)
at Instance.<anonymous> ((index):8580:38)
at Generator.next (<anonymous>)
at fulfilled ((index):3193:40)

This is likely because arrayDecodeStorage returns a Tensor (cpu_arr) when loading shards for q0f32 models, even though the corresponding function in wasm_runtime.cc is marked as void. Simply wrapping the call in a tvm scope causes the following error:

014dbf9a:0xf3792 Uncaught (in promise) RuntimeError: memory access out of bounds
at 014dbf9a:0xf3792
at TVMObject.dispose ((index):7931:57)
at RuntimeContext.endScope ((index):7802:48)
at Instance.endScope ((index):8286:26)
at Instance.withNewScope ((index):8300:22)
at Instance.<anonymous> ((index):8601:34)
at Generator.next (<anonymous>)
at fulfilled ((index):3214:40)

This suggests that a double-free is occurring and that the return value of arrayDecodeStorage shares the same memory as cpu_arr. The resolution for this is to wrap the arrayDecodeStorage call in a tvm scope and explicitly detach the return value from the scope. We want to skip this detach call when the object is not found in the current scope.

@gemini-code-assist
Copy link

Summary of Changes

Hello @akaashrp, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses and resolves a significant scope management issue affecting the arrayDecodeStorage function, particularly for q0f32 models. The core change involves introducing a new scope for the arrayDecodeStorage operation and explicitly detaching TVMObject instances to ensure proper object lifecycle and prevent potential memory or runtime errors. Additionally, the PR includes routine updates to a number of development dependencies to keep the project's tooling current.

Highlights

  • Scope Issue Fix: Resolved a critical scope management problem within the arrayDecodeStorage function, specifically when handling q0f32 models.
  • Explicit Scope Management: Implemented explicit scope handling for TVMObject results returned by arrayDecodeStorage by wrapping the call in a new scope and detaching relevant objects.
  • Dependency Updates: Updated various web development dependencies, including several Babel packages, @types/node, and browser compatibility tools.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses a scope management issue in arrayDecodeStorage for q0f32 models by wrapping the call in a new scope to handle temporary object disposal. The change correctly identifies that the return value might need to be detached from this temporary scope to prevent it from being disposed. My main feedback is on improving the error handling within the try...catch block to avoid silently ignoring potentially important errors.

// first sync copy to cpu.
this.ctx.arrayDecodeStorage(cpu_arr, new Uint8Array(recSource), rec.format, rec.dtype);
this.withNewScope(() => {
const retValue = this.ctx.arrayDecodeStorage(
Copy link
Member

Choose a reason for hiding this comment

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

would be good to document the rationale here, any better ways than try catch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've documented the rationale in the PR description. If you'd like, I can add a comment in the code too. I've updated the error handling in the catch block to throw when the error is not something related to the object not being found in the current scope. I don't think there's a better way than a try-catch to avoid throwing an error in the aforementioned case - maybe creating an if block that mirrors the if-else conditions in ArrayDecodeStorage (wasm_runtime.cc)?

@tqchen
Copy link
Member

tqchen commented Nov 3, 2025

Ok, reading https://github.com/apache/tvm/blob/main/web/emcc/wasm_runtime.cc#L125, seems the function does not have a return value. Would be important to double check in here on what is going on, maybe related to a bug.

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.

2 participants