Model reference · open weights
LaVIT is an open-weight image model from rain1011. LaVIT-7B-v1 (BF16) weighs 1.9 GB; the smallest configuration that runs it is RTX 3060 12 GB.
What it is
| Released by | rain1011 |
|---|---|
| Type | Image models |
| Task | Text→image |
| Runs with | diffusers |
| Released | 2023-10-16 |
| Popularity | 11 downloads / month |
| Weights | 1.9 GB (LaVIT-7B-v1 (BF16), file size) |
| Licence | Open, with conditions |
What it runs on
Weights 1.9 GB (file size) · its biggest part 1.6 GB · working memory for one 1024×1024 image about 5.0 GB · overhead about 537 MB.
| Card | One 1024×1024 image | Counted memory |
|---|---|---|
| RTX 3060 12 GB | fits | 11.6 GB |
| RTX 4060 Ti 16 GB | fits | 15.4 GB |
| RTX 3090 24 GB | fits | 23.4 GB |
| RTX 4090 24 GB | fits | 23.4 GB |
| RTX 5090 32 GB | fits | 31.0 GB |
| L40S 48 GB | fits | 44.0 GB |
| A100 80 GB | fits | 78.2 GB |
| H100 80 GB | fits | 78.1 GB |
| RTX PRO 6000 Blackwell 96 GB | fits | 93.8 GB |
| DGX Spark (GB10) 128 GB unified | fits | 107 GB |
| H200 141 GB | fits | 138 GB |
| B200 180 GB | fits | 176 GB |
Estimates, not measurements: the weights are the build's file size; one 1024×1024 image needs about 5 GB of working memory (larger images more); "encoders offloaded" means only the biggest part is on the card at once — diffusers' model offload, or ComfyUI unloading the text encoder. diffusers can also place a pipeline's parts on separate cards (device_map) — not estimated here. Counted memory is 92 % of what CUDA reports for the card.
From the model card
The inference code of LaVIT can be found in here.
[arXiv] [BibTeX]
2023.10.17 🚀🚀🚀 We release the pre-trained weight for LaVIT on the HuggingFace and provide the inference code of using it for both multi-modal understanding and generation.2023.10.31 🌟🌟🌟 We update the high-resolution pixel decoder in LaVIT, which supports to generate high resolution (1024 * 1024 pixels), muliple aspect ratios (1:1, 4:3, 3:2, 16:9 ...) and high aesthetics images. The quality of generated images have been improved significantly.The code for this repo is tested with PyTorch 1.13.1 and CUDA 11.7. You should first install and configure the Pytorch Environment (including torch and torchvision) can then install the requirements with the following commands:
git clone https://github.com/jy0205/LaVIT.git
cd LaVIT
pip install -r requirements.txt
use_xformers=True in build_model function to save the GPU memory and speed up inference.We release the LaVIT weight that is built upon Llama-2-7B as the large language model.
Note: Due to the license restrictions of Llama1, we cannot publish its weights. Thus, we release the weight of LaVIT based on the Llama2.
The pre-trained weight of LaVIT can be found on the huggingface from here, which will take around 22GB of disk space. LaVIT achieves state-of-the-arts performance on various multi-modal downstream tasks. The detailed quantitive results are shown as follows:
LaVIT can serve as a multi-modal generalist to perform both multi-modal comprehension and generation. Below, we provide some examples. Only a few lines of code are needed to use LaVIT for inference. We also provide the detailed examples in the following jupyter notebooks for learning how to interact with LaVIT.
understanding.ipynb : examples for multi-modal understandingtext2image_synthesis.ipynb: examples for the text-to-image generation.multimodal_synthesis.ipynb: examples for image synthesis with multi-modal prompts.import os
import random
import torch
import torch.nn as nn
from models import build_model
from PIL import Image
seed = 1234
random.seed(seed)
torch.manual_seed(seed)
# The local directory you save the LaVIT pre-trained weight,
# it will automatically download the checkpoint from huggingface
model_path = '/path/LaVIT_weight'
# Using BFloat16 during inference
model_dtype = 'bf16' # Or set to fp16 to enable float16 inference
# Inference using GPU-0
device_id = 0
torch.cuda.set_device(device_id)
device = torch.device('cuda')
# Building LaVIT for understanding and load its weight from huggingface
model = build_model(model_path=model_path, model_dtype=model_dtype,
device_id=device_id, use_xformers=False, understanding=True)
model = model.to(device)
# Image Captioning
image_path = 'demo/caption_image.jpg'
caption = model.generate({"image": image_path})[0]
print(caption)
# an old photo of a horse and buggy in front of a building
# Visual Question Answering
image_path = 'demo/qa_image.jpg'
question = "What's that drink in the glass?"
answer = model.predict_answers({"image": image_path, "text_input": question}, max_len=10)[0]
print("The answer is: ", answer)
# The answer is: orange juice
For the Image generation, the Classifier-Free Guidance scale is important. A larger scale will encourage the model to generate samples highly related to the input prompt while sacrificing the image quality. We set guidance_scale_for_llm=4.0 by default, you can increase this scale (e.g., 5.0 or 6.0) to encourage the generated image to follow the semantics of given prompts. Besides, you can modify the ratio to enable to generate the images with different aspect ratios.
import os
import torch
import random
import torch.nn as nn
from models import build_model
from PIL import Image
seed = 1234
random.seed(seed)
torch.manual_seed(seed)
# The local directory you save the LaVIT pre-trained weight,
# it will automatically download the checkpoint from huggingface
model_path = '/path/LaVIT_weight'
# Using BFloat16 during inference
model_dtype = 'bf16' # Or set to fp16 to enable float16 inference
# Inference using GPU-0
device_id = 0
torch.cuda.set_device(device_id)
device = torch.device('cuda')
torch_dtype = torch.bfloat16 if model_dtype=="bf16" else torch.float16
# Building LaVIT for Generation and load the weight from huggingface
# You can set `use_xformers=True` if have installed xformers to save GPU mempry and speed up
model = build_model(model_path=model_path, model_dtype=model_dtype, device_id=device_id,
use_xformers=False, understanding=False, load_tokenizer=False)
model = model.to(device)
# Text-to-Image Generation
prompt = "a sculpture of a duck made of wool"
# LaVIT support 6 different image aspect ratios
ratio_dict = {
'1:1' : (1024, 1024),
'4:3' : (896, 1152),
'3:2' : (832, 1216),
'16:9' : (768, 1344),
'2:3' : (1216, 832),
'3:4' : (1152, 896),
}
# The image aspect ratio you want to generate
ratio = '1:1'
height, width = ratio_dict[ratio]
with torch.cuda.amp.autocast(enabled=True, dtype=torch_dtype):
images = model.generate_image(prompt, width=width, height=height,
num_return_images=1, guidance_scale_for_llm=4.0, num_inference_steps=50)
images[0].save("output/i2t_output.jpg")
The batch evaluation code with multiple GPUs on the ado
Quoted from the model card on Hugging Face — the full card is behind the Hugging Face link above.
How it works
Running it yourself
Rent a machine by the hour — ComfyUI is installed on it. Open ComfyUI through the tunnel, load the workflow from the model's card on Hugging Face, and choose this file in its Load VAE node.
# on your rented machine (the ssh line is on its page in the console)
# get REPO FILE FOLDER: one file into /workspace/models/FOLDER, where ComfyUI loads it from
get() { hf download "$1" "$2" --local-dir /workspace/hf-files && mkdir -p "/workspace/models/$3" && mv "/workspace/hf-files/$2" "/workspace/models/$3/$4"; }
# the model (638 MB)
get rain1011/LaVIT-7B-v1 highres_pixel_decoding/vae/diffusion_pytorch_model.safetensors vae
get rain1011/LaVIT-7B-v1 pixel_decoding/vae/diffusion_pytorch_model.safetensors vae
start-comfyui
# on your computer, in a second terminal: ComfyUI in your browser at http://localhost:8188
# HOST and PORT are your machine's, from its page in the console
ssh -L 8188:localhost:8188 dev@HOST -p PORT