How would I, for a unit test, load an instance of a Qwen2VL model from pretrained…
- User
How would I, for a unit test, load an instance of a Qwen2VL model from pretrained weights and call the forward pass one time?
Assistant
You can load an instance of a Qwen2VL model from pretrained weights and call the forward pass one time using the following code:
pythonfrom sglang.srt.model_loader.loader import get_model from sglang.srt.model_loader.loader import LoadConfig from sglang.srt.configs.device_config import DeviceConfig from sglang.srt.configs.model_config import ModelConfig from sglang.srt.configs.load_config import LoadFormat from transformers import AutoConfig # Load the model config from the transformers library hf_config = AutoConfig.from_pretrained("Qwen/Qwen2-1.5B") # Define the model config model_config = ModelConfig( model_path="Qwen/Qwen2-1.5B", hf_config=hf_config, dtype=torch.float16, is_generation=True, is_multimodal=True, ) # Define the load config load_config = LoadConfig( load_format=LoadFormat.SAFETENSORS, download_dir="./models", ) # Define the device config device_config = DeviceConfig("cuda") # Load the model model = get_model(model_config, load_config, device_config) # Move the model to the desired device model.to(device_config.device) # Create a dummy input input_ids = torch.randint(0, model.config.vocab_size, (1, 128), device=device_config.device) positions = torch.arange(128, device=device_config.device) # Call the forward pass from sglang.srt.model_executor.forward_batch_info import ForwardBatch forward_batch = ForwardBatch(input_ids, positions) output = model(input_ids=input_ids, positions=positions, forward_batch=forward_batch)Note that this code assumes that you have the
transformerslibrary installed and that you have downloaded the pretrained weights for the Qwen2VL model. You may need to modify the and variables to match your specific use case. Additionally, this code only calls the forward pass one time with a dummy input; you will likely need to modify the input and the forward pass call to match your specific use case.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.