- 
                Notifications
    
You must be signed in to change notification settings  - Fork 3.7k
 
[Web] Fix arrayDecodeStorage scope issue for q0f32 models #18415
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
base: main
Are you sure you want to change the base?
Conversation
          Summary of ChangesHello @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  Highlights
 Using Gemini Code AssistThe 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  
 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  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
  | 
    
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.
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( | 
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.
would be good to document the rationale here, any better ways than try catch?
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.
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)?
| 
           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.  | 
    
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:
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:
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.