- 
                Notifications
    You must be signed in to change notification settings 
- Fork 87
feat(transformers/model): add InternVL #1217
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
      
      
            AndyZhou952
  wants to merge
  33
  commits into
  mindspore-lab:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
AndyZhou952:internvl
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
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 18 commits
      Commits
    
    
            Show all changes
          
          
            33 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      cf69c88
              
                init & auto
              
              
                AndyZhou952 036616b
              
                add back LlavaModel
              
              
                AndyZhou952 112581b
              
                modular
              
              
                AndyZhou952 0d9ea2e
              
                modeling + minor modular update
              
              
                AndyZhou952 c2bbbcc
              
                added example
              
              
                AndyZhou952 dbd00f5
              
                revert changes in modular
              
              
                AndyZhou952 d8e635c
              
                fix
              
              
                AndyZhou952 4215c56
              
                monkey patch to bypass video processing
              
              
                AndyZhou952 4c93bb1
              
                update generate
              
              
                AndyZhou952 6e33ab1
              
                fix runnable
              
              
                AndyZhou952 31e3ed9
              
                update generate ImageProcessor
              
              
                AndyZhou952 6f5649c
              
                (attempt) add _checkpoint_conversion_mapping support
              
              
                AndyZhou952 21e58ff
              
                add & fix
              
              
                AndyZhou952 7325f2d
              
                fix
              
              
                AndyZhou952 76063d7
              
                update example in docstring
              
              
                AndyZhou952 f1b8f5f
              
                test
              
              
                AndyZhou952 3ce4255
              
                fix
              
              
                AndyZhou952 5837a9c
              
                type check
              
              
                AndyZhou952 126b4e6
              
                update
              
              
                AndyZhou952 7ae54e0
              
                attempt fix
              
              
                AndyZhou952 e4bc210
              
                fix
              
              
                AndyZhou952 e128d3c
              
                fix - consistent
              
              
                AndyZhou952 3230683
              
                update
              
              
                AndyZhou952 e1dba7d
              
                Merge branch 'master' into internvl
              
              
                AndyZhou952 ab20bc2
              
                linting
              
              
                AndyZhou952 4429b89
              
                Merge branch 'internvl' of https://github.com/AndyZhou952/mindone int…
              
              
                AndyZhou952 1a8051a
              
                rm redundant
              
              
                AndyZhou952 95e3967
              
                rm redundant
              
              
                AndyZhou952 c60aac4
              
                rm redundant
              
              
                AndyZhou952 4e9f9f2
              
                linting
              
              
                AndyZhou952 00e7eeb
              
                linting
              
              
                AndyZhou952 b557f85
              
                linting
              
              
                AndyZhou952 55db6f3
              
                Merge branch 'mindspore-lab:master' into internvl
              
              
                AndyZhou952 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
          Some comments aren't visible on the classic Files Changed page.
        
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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import time | ||
| import mindspore as ms | ||
|  | ||
| from transformers import InternVLProcessor, GotOcr2ImageProcessor | ||
| from mindone.transformers import InternVLForConditionalGeneration | ||
| from PIL import Image | ||
|  | ||
| MODEL_HUB = "OpenGVLab/InternVL3-1B-hf" | ||
| image = "demo.jpeg" | ||
|  | ||
| # Load processor | ||
| start = time.time() | ||
| processor = InternVLProcessor.from_pretrained(MODEL_HUB) | ||
| # GotOcr2ImageProcessorFast does not support return_tensors="np", use GotOcr2ImageProcessor instead | ||
| image_processor = GotOcr2ImageProcessor.from_pretrained(MODEL_HUB) | ||
| processor.image_processor = image_processor | ||
| print(f"Loaded InternVLProcessor in {time.time()-start:.4f}s") | ||
|  | ||
| # Load model with bfloat16 and eager attention | ||
| start = time.time() | ||
| model = InternVLForConditionalGeneration.from_pretrained( | ||
| MODEL_HUB, | ||
| mindspore_dtype=ms.bfloat16, | ||
| attn_implementation="eager", | ||
| ) | ||
| print(f"Loaded model in {time.time()-start:.4f}s") | ||
|  | ||
| # load image | ||
| image = Image.open(image) | ||
|  | ||
| messages = [ | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| { | ||
| "type": "image", | ||
| "image": image, | ||
| }, | ||
| {"type": "text", "text": "Describe this image."}, | ||
| ], | ||
| } | ||
| ] | ||
| prompt = processor.apply_chat_template(messages, add_generation_prompt=True) | ||
|  | ||
| # Tokenize + encode | ||
| inputs = processor(text=prompt, images=[image], return_tensors="np") | ||
|  | ||
| for k, v in inputs.items(): | ||
| tensor = ms.Tensor(v) | ||
| if tensor.dtype == ms.int64: | ||
| tensor = tensor.astype(ms.int32) | ||
| else: | ||
| tensor = tensor.astype(model.dtype) | ||
| inputs[k] = tensor | ||
|  | ||
| # Generate | ||
| start = time.time() | ||
| generated_ids = model.generate(**inputs, max_new_tokens=500) | ||
| print(f"Inference in {time.time()-start:.4f}s") | ||
|  | ||
| # Decode | ||
| texts = processor.batch_decode(generated_ids, skip_special_tokens=True) | ||
| print(texts) | ||
  
    
      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
    
  
  
    
              
  
    
      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,21 @@ | ||
| # Copyright 2025 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # Licensed 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. | ||
|  | ||
| from .modeling_internvl import ( | ||
| InternVLVisionPreTrainedModel, | ||
| InternVLVisionModel, | ||
| InternVLPreTrainedModel, | ||
| InternVLModel, | ||
| InternVLForConditionalGeneration, | ||
| ) | 
      
      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.