Spaces:
Runtime error
Runtime error
charlescxk commited on
Commit ·
a38a851
1
Parent(s): 2c415b6
update
Browse filesFiles changed (40)
- README.mdDeleted-13
- Upsample/__init__.pyAdded+1
- Upsample/__pycache__/__init__.cpython-38.pycAdded
- Upsample/__pycache__/arch_utils.cpython-38.pycAdded
- Upsample/__pycache__/model.cpython-38.pycAdded
- Upsample/__pycache__/rrdbnet_arch.cpython-38.pycAdded
- Upsample/__pycache__/utils.cpython-38.pycAdded
- Upsample/arch_utils.pyAdded+197
- Upsample/model.pyAdded+93
- Upsample/rrdbnet_arch.pyAdded+121
- Upsample/utils.pyAdded+135
- app.pyAdded+268
- doge.pngAdded
- equation.pngAdded
- janus/__init__.pyAdded+31
- janus/__pycache__/__init__.cpython-38.pycAdded
- janus/models/__init__.pyAdded+28
- janus/models/__pycache__/__init__.cpython-38.pycAdded
- janus/models/__pycache__/clip_encoder.cpython-38.pycAdded
- janus/models/__pycache__/image_processing_vlm.cpython-38.pycAdded
- janus/models/__pycache__/modeling_vlm.cpython-38.pycAdded
- janus/models/__pycache__/processing_vlm.cpython-38.pycAdded
- janus/models/__pycache__/projector.cpython-38.pycAdded
- janus/models/__pycache__/siglip_vit.cpython-38.pycAdded
- janus/models/__pycache__/vq_model.cpython-38.pycAdded
- janus/models/clip_encoder.pyAdded+122
- janus/models/image_processing_vlm.pyAdded+208
- janus/models/modeling_vlm.pyAdded+272
- janus/models/processing_vlm.pyAdded+418
- janus/models/projector.pyAdded+100
- janus/models/siglip_vit.pyAdded+681
- janus/models/vq_model.pyAdded+527
- janus/utils/__init__.pyAdded+18
- janus/utils/__pycache__/__init__.cpython-38.pycAdded
- janus/utils/__pycache__/conversation.cpython-38.pycAdded
- janus/utils/__pycache__/io.cpython-38.pycAdded
- janus/utils/conversation.pyAdded+365
- janus/utils/io.pyAdded+89
- requirements.txtAdded+8
- weights/RealESRGAN_x2.pthAdded+3
12345678910111213---title: Janus Pro 7Bemoji: 🐢colorFrom: purplecolorTo: graysdk: gradiosdk_version: 5.13.1app_file: app.pypinned: falselicense: mit---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference1from .model import RealESRGAN123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197import mathimport torchfrom torch import nn as nnfrom torch.nn import functional as Ffrom torch.nn import init as initfrom torch.nn.modules.batchnorm import _BatchNorm
@torch.no_grad()def default_init_weights(module_list, scale=1, bias_fill=0, **kwargs): """Initialize network weights.
Args: module_list (list[nn.Module] | nn.Module): Modules to be initialized. scale (float): Scale initialized weights, especially for residual blocks. Default: 1. bias_fill (float): The value to fill bias. Default: 0 kwargs (dict): Other arguments for initialization function. """ if not isinstance(module_list, list): module_list = [module_list] for module in module_list: for m in module.modules(): if isinstance(m, nn.Conv2d): init.kaiming_normal_(m.weight, **kwargs) m.weight.data *= scale if m.bias is not None: m.bias.data.fill_(bias_fill) elif isinstance(m, nn.Linear): init.kaiming_normal_(m.weight, **kwargs) m.weight.data *= scale if m.bias is not None: m.bias.data.fill_(bias_fill) elif isinstance(m, _BatchNorm): init.constant_(m.weight, 1) if m.bias is not None: m.bias.data.fill_(bias_fill)
def make_layer(basic_block, num_basic_block, **kwarg): """Make layers by stacking the same blocks.
Args: basic_block (nn.module): nn.module class for basic block. num_basic_block (int): number of blocks.
Returns: nn.Sequential: Stacked blocks in nn.Sequential. """ layers = [] for _ in range(num_basic_block): layers.append(basic_block(**kwarg)) return nn.Sequential(*layers)
class ResidualBlockNoBN(nn.Module): """Residual block without BN.
It has a style of: ---Conv-ReLU-Conv-+- |________________|
Args: num_feat (int): Channel number of intermediate features. Default: 64. res_scale (float): Residual scale. Default: 1. pytorch_init (bool): If set to True, use pytorch default init, otherwise, use default_init_weights. Default: False. """
def __init__(self, num_feat=64, res_scale=1, pytorch_init=False): super(ResidualBlockNoBN, self).__init__() self.res_scale = res_scale self.conv1 = nn.Conv2d(num_feat, num_feat, 3, 1, 1, bias=True) self.conv2 = nn.Conv2d(num_feat, num_feat, 3, 1, 1, bias=True) self.relu = nn.ReLU(inplace=True)
if not pytorch_init: default_init_weights([self.conv1, self.conv2], 0.1)
def forward(self, x): identity = x out = self.conv2(self.relu(self.conv1(x))) return identity + out * self.res_scale
class Upsample(nn.Sequential): """Upsample module.
Args: scale (int): Scale factor. Supported scales: 2^n and 3. num_feat (int): Channel number of intermediate features. """
def __init__(self, scale, num_feat): m = [] if (scale & (scale - 1)) == 0: # scale = 2^n for _ in range(int(math.log(scale, 2))): m.append(nn.Conv2d(num_feat, 4 * num_feat, 3, 1, 1)) m.append(nn.PixelShuffle(2)) elif scale == 3: m.append(nn.Conv2d(num_feat, 9 * num_feat, 3, 1, 1)) m.append(nn.PixelShuffle(3)) else: raise ValueError(f'scale {scale} is not supported. ' 'Supported scales: 2^n and 3.') super(Upsample, self).__init__(*m)
def flow_warp(x, flow, interp_mode='bilinear', padding_mode='zeros', align_corners=True): """Warp an image or feature map with optical flow.
Args: x (Tensor): Tensor with size (n, c, h, w). flow (Tensor): Tensor with size (n, h, w, 2), normal value. interp_mode (str): 'nearest' or 'bilinear'. Default: 'bilinear'. padding_mode (str): 'zeros' or 'border' or 'reflection'. Default: 'zeros'. align_corners (bool): Before pytorch 1.3, the default value is align_corners=True. After pytorch 1.3, the default value is align_corners=False. Here, we use the True as default.
Returns: Tensor: Warped image or feature map. """ assert x.size()[-2:] == flow.size()[1:3] _, _, h, w = x.size() # create mesh grid grid_y, grid_x = torch.meshgrid(torch.arange(0, h).type_as(x), torch.arange(0, w).type_as(x)) grid = torch.stack((grid_x, grid_y), 2).float() # W(x), H(y), 2 grid.requires_grad = False
vgrid = grid + flow # scale grid to [-1,1] vgrid_x = 2.0 * vgrid[:, :, :, 0] / max(w - 1, 1) - 1.0 vgrid_y = 2.0 * vgrid[:, :, :, 1] / max(h - 1, 1) - 1.0 vgrid_scaled = torch.stack((vgrid_x, vgrid_y), dim=3) output = F.grid_sample(x, vgrid_scaled, mode=interp_mode, padding_mode=padding_mode, align_corners=align_corners)
# TODO, what if align_corners=False return output
def resize_flow(flow, size_type, sizes, interp_mode='bilinear', align_corners=False): """Resize a flow according to ratio or shape.
Args: flow (Tensor): Precomputed flow. shape [N, 2, H, W]. size_type (str): 'ratio' or 'shape'. sizes (list[int | float]): the ratio for resizing or the final output shape. 1) The order of ratio should be [ratio_h, ratio_w]. For downsampling, the ratio should be smaller than 1.0 (i.e., ratio < 1.0). For upsampling, the ratio should be larger than 1.0 (i.e., ratio > 1.0). 2) The order of output_size should be [out_h, out_w]. interp_mode (str): The mode of interpolation for resizing. Default: 'bilinear'. align_corners (bool): Whether align corners. Default: False.
Returns: Tensor: Resized flow. """ _, _, flow_h, flow_w = flow.size() if size_type == 'ratio': output_h, output_w = int(flow_h * sizes[0]), int(flow_w * sizes[1]) elif size_type == 'shape': output_h, output_w = sizes[0], sizes[1] else: raise ValueError(f'Size type should be ratio or shape, but got type {size_type}.')
input_flow = flow.clone() ratio_h = output_h / flow_h ratio_w = output_w / flow_w input_flow[:, 0, :, :] *= ratio_w input_flow[:, 1, :, :] *= ratio_h resized_flow = F.interpolate( input=input_flow, size=(output_h, output_w), mode=interp_mode, align_corners=align_corners) return resized_flow
# TODO: may write a cpp filedef pixel_unshuffle(x, scale): """ Pixel unshuffle.
Args: x (Tensor): Input feature with shape (b, c, hh, hw). scale (int): Downsample ratio.
Returns: Tensor: the pixel unshuffled feature. """ b, c, hh, hw = x.size() out_channel = c * (scale**2) assert hh % scale == 0 and hw % scale == 0 h = hh // scale w = hw // scale x_view = x.view(b, c, h, scale, w, scale) return x_view.permute(0, 1, 3, 5, 2, 4).reshape(b, out_channel, h, w)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293import osimport torchfrom torch.nn import functional as Ffrom PIL import Imageimport numpy as npimport cv2from huggingface_hub import hf_hub_url, hf_hub_download
from .rrdbnet_arch import RRDBNetfrom .utils import pad_reflect, split_image_into_overlapping_patches, stich_together, \ unpad_image
HF_MODELS = { 2: dict( repo_id='sberbank-ai/Real-ESRGAN', filename='RealESRGAN_x2.pth', ), 4: dict( repo_id='sberbank-ai/Real-ESRGAN', filename='RealESRGAN_x4.pth', ), 8: dict( repo_id='sberbank-ai/Real-ESRGAN', filename='RealESRGAN_x8.pth', ),}
class RealESRGAN: def __init__(self, device, scale=4): self.device = device self.scale = scale self.model = RRDBNet( num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=scale )
def load_weights(self, model_path, download=True): if not os.path.exists(model_path) and download: assert self.scale in [2, 4, 8], 'You can download models only with scales: 2, 4, 8' config = HF_MODELS[self.scale] cache_dir = os.path.dirname(model_path) local_filename = os.path.basename(model_path) config_file_url = hf_hub_url(repo_id=config['repo_id'], filename=config['filename']) htr = hf_hub_download(repo_id=config['repo_id'], cache_dir=cache_dir, local_dir=cache_dir, filename=config['filename']) print(htr) # cached_download(config_file_url, cache_dir=cache_dir, force_filename=local_filename) print('Weights downloaded to:', os.path.join(cache_dir, local_filename))
loadnet = torch.load(model_path) if 'params' in loadnet: self.model.load_state_dict(loadnet['params'], strict=True) elif 'params_ema' in loadnet: self.model.load_state_dict(loadnet['params_ema'], strict=True) else: self.model.load_state_dict(loadnet, strict=True) self.model.eval() self.model.to(self.device)
# @torch.cuda.amp.autocast() def predict(self, lr_image, batch_size=4, patches_size=192, padding=24, pad_size=15): torch.autocast(device_type=self.device.type) scale = self.scale device = self.device lr_image = np.array(lr_image) lr_image = pad_reflect(lr_image, pad_size)
patches, p_shape = split_image_into_overlapping_patches( lr_image, patch_size=patches_size, padding_size=padding ) img = torch.FloatTensor(patches / 255).permute((0, 3, 1, 2)).to(device).detach()
with torch.no_grad(): res = self.model(img[0:batch_size]) for i in range(batch_size, img.shape[0], batch_size): res = torch.cat((res, self.model(img[i:i + batch_size])), 0)
sr_image = res.permute((0, 2, 3, 1)).cpu().clamp_(0, 1) np_sr_image = sr_image.numpy()
padded_size_scaled = tuple(np.multiply(p_shape[0:2], scale)) + (3,) scaled_image_shape = tuple(np.multiply(lr_image.shape[0:2], scale)) + (3,) np_sr_image = stich_together( np_sr_image, padded_image_shape=padded_size_scaled, target_shape=scaled_image_shape, padding_size=padding * scale ) sr_img = (np_sr_image * 255).astype(np.uint8) sr_img = unpad_image(sr_img, pad_size * scale) sr_img = Image.fromarray(sr_img)
return sr_img123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121import torchfrom torch import nn as nnfrom torch.nn import functional as F
from .arch_utils import default_init_weights, make_layer, pixel_unshuffle
class ResidualDenseBlock(nn.Module): """Residual Dense Block.
Used in RRDB block in ESRGAN.
Args: num_feat (int): Channel number of intermediate features. num_grow_ch (int): Channels for each growth. """
def __init__(self, num_feat=64, num_grow_ch=32): super(ResidualDenseBlock, self).__init__() self.conv1 = nn.Conv2d(num_feat, num_grow_ch, 3, 1, 1) self.conv2 = nn.Conv2d(num_feat + num_grow_ch, num_grow_ch, 3, 1, 1) self.conv3 = nn.Conv2d(num_feat + 2 * num_grow_ch, num_grow_ch, 3, 1, 1) self.conv4 = nn.Conv2d(num_feat + 3 * num_grow_ch, num_grow_ch, 3, 1, 1) self.conv5 = nn.Conv2d(num_feat + 4 * num_grow_ch, num_feat, 3, 1, 1)
self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
# initialization default_init_weights([self.conv1, self.conv2, self.conv3, self.conv4, self.conv5], 0.1)
def forward(self, x): x1 = self.lrelu(self.conv1(x)) x2 = self.lrelu(self.conv2(torch.cat((x, x1), 1))) x3 = self.lrelu(self.conv3(torch.cat((x, x1, x2), 1))) x4 = self.lrelu(self.conv4(torch.cat((x, x1, x2, x3), 1))) x5 = self.conv5(torch.cat((x, x1, x2, x3, x4), 1)) # Emperically, we use 0.2 to scale the residual for better performance return x5 * 0.2 + x
class RRDB(nn.Module): """Residual in Residual Dense Block.
Used in RRDB-Net in ESRGAN.
Args: num_feat (int): Channel number of intermediate features. num_grow_ch (int): Channels for each growth. """
def __init__(self, num_feat, num_grow_ch=32): super(RRDB, self).__init__() self.rdb1 = ResidualDenseBlock(num_feat, num_grow_ch) self.rdb2 = ResidualDenseBlock(num_feat, num_grow_ch) self.rdb3 = ResidualDenseBlock(num_feat, num_grow_ch)
def forward(self, x): out = self.rdb1(x) out = self.rdb2(out) out = self.rdb3(out) # Emperically, we use 0.2 to scale the residual for better performance return out * 0.2 + x
class RRDBNet(nn.Module): """Networks consisting of Residual in Residual Dense Block, which is used in ESRGAN.
ESRGAN: Enhanced Super-Resolution Generative Adversarial Networks.
We extend ESRGAN for scale x2 and scale x1. Note: This is one option for scale 1, scale 2 in RRDBNet. We first employ the pixel-unshuffle (an inverse operation of pixelshuffle to reduce the spatial size and enlarge the channel size before feeding inputs into the main ESRGAN architecture.
Args: num_in_ch (int): Channel number of inputs. num_out_ch (int): Channel number of outputs. num_feat (int): Channel number of intermediate features. Default: 64 num_block (int): Block number in the trunk network. Defaults: 23 num_grow_ch (int): Channels for each growth. Default: 32. """
def __init__(self, num_in_ch, num_out_ch, scale=4, num_feat=64, num_block=23, num_grow_ch=32): super(RRDBNet, self).__init__() self.scale = scale if scale == 2: num_in_ch = num_in_ch * 4 elif scale == 1: num_in_ch = num_in_ch * 16 self.conv_first = nn.Conv2d(num_in_ch, num_feat, 3, 1, 1) self.body = make_layer(RRDB, num_block, num_feat=num_feat, num_grow_ch=num_grow_ch) self.conv_body = nn.Conv2d(num_feat, num_feat, 3, 1, 1) # upsample self.conv_up1 = nn.Conv2d(num_feat, num_feat, 3, 1, 1) self.conv_up2 = nn.Conv2d(num_feat, num_feat, 3, 1, 1) if scale == 8: self.conv_up3 = nn.Conv2d(num_feat, num_feat, 3, 1, 1) self.conv_hr = nn.Conv2d(num_feat, num_feat, 3, 1, 1) self.conv_last = nn.Conv2d(num_feat, num_out_ch, 3, 1, 1)
self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
def forward(self, x): if self.scale == 2: feat = pixel_unshuffle(x, scale=2) elif self.scale == 1: feat = pixel_unshuffle(x, scale=4) else: feat = x feat = self.conv_first(feat) body_feat = self.conv_body(self.body(feat)) feat = feat + body_feat # upsample feat = self.lrelu(self.conv_up1(F.interpolate(feat, scale_factor=2, mode='nearest'))) feat = self.lrelu(self.conv_up2(F.interpolate(feat, scale_factor=2, mode='nearest'))) if self.scale == 8: feat = self.lrelu(self.conv_up3(F.interpolate(feat, scale_factor=2, mode='nearest'))) out = self.conv_last(self.lrelu(self.conv_hr(feat))) return out123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135import numpy as npimport torchfrom PIL import Imageimport osimport io
def pad_reflect(image, pad_size): imsize = image.shape height, width = imsize[:2] new_img = np.zeros([height + pad_size * 2, width + pad_size * 2, imsize[2]]).astype(np.uint8) new_img[pad_size:-pad_size, pad_size:-pad_size, :] = image
new_img[0:pad_size, pad_size:-pad_size, :] = np.flip(image[0:pad_size, :, :], axis=0) # top new_img[-pad_size:, pad_size:-pad_size, :] = np.flip(image[-pad_size:, :, :], axis=0) # bottom new_img[:, 0:pad_size, :] = np.flip(new_img[:, pad_size:pad_size * 2, :], axis=1) # left new_img[:, -pad_size:, :] = np.flip(new_img[:, -pad_size * 2:-pad_size, :], axis=1) # right
return new_img
def unpad_image(image, pad_size): return image[pad_size:-pad_size, pad_size:-pad_size, :]
def process_array(image_array, expand=True): """ Process a 3-dimensional array into a scaled, 4 dimensional batch of size 1. """
image_batch = image_array / 255.0 if expand: image_batch = np.expand_dims(image_batch, axis=0) return image_batch
def process_output(output_tensor): """ Transforms the 4-dimensional output tensor into a suitable image format. """
sr_img = output_tensor.clip(0, 1) * 255 sr_img = np.uint8(sr_img) return sr_img
def pad_patch(image_patch, padding_size, channel_last=True): """ Pads image_patch with with padding_size edge values. """
if channel_last: return np.pad( image_patch, ((padding_size, padding_size), (padding_size, padding_size), (0, 0)), 'edge', ) else: return np.pad( image_patch, ((0, 0), (padding_size, padding_size), (padding_size, padding_size)), 'edge', )
def unpad_patches(image_patches, padding_size): return image_patches[:, padding_size:-padding_size, padding_size:-padding_size, :]
def split_image_into_overlapping_patches(image_array, patch_size, padding_size=2): """ Splits the image into partially overlapping patches. The patches overlap by padding_size pixels. Pads the image twice: - first to have a size multiple of the patch size, - then to have equal padding at the borders. Args: image_array: numpy array of the input image. patch_size: size of the patches from the original image (without padding). padding_size: size of the overlapping area. """
xmax, ymax, _ = image_array.shape x_remainder = xmax % patch_size y_remainder = ymax % patch_size
# modulo here is to avoid extending of patch_size instead of 0 x_extend = (patch_size - x_remainder) % patch_size y_extend = (patch_size - y_remainder) % patch_size
# make sure the image is divisible into regular patches extended_image = np.pad(image_array, ((0, x_extend), (0, y_extend), (0, 0)), 'edge')
# add padding around the image to simplify computations padded_image = pad_patch(extended_image, padding_size, channel_last=True)
xmax, ymax, _ = padded_image.shape patches = []
x_lefts = range(padding_size, xmax - padding_size, patch_size) y_tops = range(padding_size, ymax - padding_size, patch_size)
for x in x_lefts: for y in y_tops: x_left = x - padding_size y_top = y - padding_size x_right = x + patch_size + padding_size y_bottom = y + patch_size + padding_size patch = padded_image[x_left:x_right, y_top:y_bottom, :] patches.append(patch)
return np.array(patches), padded_image.shape
def stich_together(patches, padded_image_shape, target_shape, padding_size=4): """ Reconstruct the image from overlapping patches. After scaling, shapes and padding should be scaled too. Args: patches: patches obtained with split_image_into_overlapping_patches padded_image_shape: shape of the padded image contructed in split_image_into_overlapping_patches target_shape: shape of the final image padding_size: size of the overlapping area. """
xmax, ymax, _ = padded_image_shape patches = unpad_patches(patches, padding_size) patch_size = patches.shape[1] n_patches_per_row = ymax // patch_size
complete_image = np.zeros((xmax, ymax, 3))
row = -1 col = 0 for i in range(len(patches)): if i % n_patches_per_row == 0: row += 1 col = 0 complete_image[ row * patch_size: (row + 1) * patch_size, col * patch_size: (col + 1) * patch_size, : ] = patches[i] col += 1 return complete_image[0: target_shape[0], 0: target_shape[1], :]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268import gradio as grimport torchfrom transformers import AutoConfig, AutoModelForCausalLMfrom janus.models import MultiModalityCausalLM, VLChatProcessorfrom janus.utils.io import load_pil_imagesfrom PIL import Image
import numpy as npimport osimport timefrom Upsample import RealESRGANimport spaces # Import spaces for ZeroGPU compatibility
# Load model and processormodel_path = "deepseek-ai/Janus-Pro-7B"config = AutoConfig.from_pretrained(model_path)language_config = config.language_configlanguage_config._attn_implementation = 'eager'vl_gpt = AutoModelForCausalLM.from_pretrained(model_path, language_config=language_config, trust_remote_code=True)if torch.cuda.is_available(): vl_gpt = vl_gpt.to(torch.bfloat16).cuda()else: vl_gpt = vl_gpt.to(torch.float16)
vl_chat_processor = VLChatProcessor.from_pretrained(model_path)tokenizer = vl_chat_processor.tokenizercuda_device = 'cuda' if torch.cuda.is_available() else 'cpu'
# SR modelsr_model = RealESRGAN(torch.device('cuda' if torch.cuda.is_available() else 'cpu'), scale=2)sr_model.load_weights(f'weights/RealESRGAN_x2.pth', download=False)
@torch.inference_mode()@spaces.GPU(duration=120) # Multimodal Understanding functiondef multimodal_understanding(image, question, seed, top_p, temperature): # Clear CUDA cache before generating torch.cuda.empty_cache() # set seed torch.manual_seed(seed) np.random.seed(seed) torch.cuda.manual_seed(seed) conversation = [ { "role": "<|User|>", "content": f"<image_placeholder>\n{question}", "images": [image], }, {"role": "<|Assistant|>", "content": ""}, ] pil_images = [Image.fromarray(image)] prepare_inputs = vl_chat_processor( conversations=conversation, images=pil_images, force_batchify=True ).to(cuda_device, dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float16) inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs) outputs = vl_gpt.language_model.generate( inputs_embeds=inputs_embeds, attention_mask=prepare_inputs.attention_mask, pad_token_id=tokenizer.eos_token_id, bos_token_id=tokenizer.bos_token_id, eos_token_id=tokenizer.eos_token_id, max_new_tokens=512, do_sample=False if temperature == 0 else True, use_cache=True, temperature=temperature, top_p=top_p, ) answer = tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True) return answer
def generate(input_ids, width, height, temperature: float = 1, parallel_size: int = 5, cfg_weight: float = 5, image_token_num_per_image: int = 576, patch_size: int = 16): # Clear CUDA cache before generating torch.cuda.empty_cache() tokens = torch.zeros((parallel_size * 2, len(input_ids)), dtype=torch.int).to(cuda_device) for i in range(parallel_size * 2): tokens[i, :] = input_ids if i % 2 != 0: tokens[i, 1:-1] = vl_chat_processor.pad_id inputs_embeds = vl_gpt.language_model.get_input_embeddings()(tokens) generated_tokens = torch.zeros((parallel_size, image_token_num_per_image), dtype=torch.int).to(cuda_device)
pkv = None for i in range(image_token_num_per_image): with torch.no_grad(): outputs = vl_gpt.language_model.model(inputs_embeds=inputs_embeds, use_cache=True, past_key_values=pkv) pkv = outputs.past_key_values hidden_states = outputs.last_hidden_state logits = vl_gpt.gen_head(hidden_states[:, -1, :]) logit_cond = logits[0::2, :] logit_uncond = logits[1::2, :] logits = logit_uncond + cfg_weight * (logit_cond - logit_uncond) probs = torch.softmax(logits / temperature, dim=-1) next_token = torch.multinomial(probs, num_samples=1) generated_tokens[:, i] = next_token.squeeze(dim=-1) next_token = torch.cat([next_token.unsqueeze(dim=1), next_token.unsqueeze(dim=1)], dim=1).view(-1)
img_embeds = vl_gpt.prepare_gen_img_embeds(next_token) inputs_embeds = img_embeds.unsqueeze(dim=1)
patches = vl_gpt.gen_vision_model.decode_code(generated_tokens.to(dtype=torch.int), shape=[parallel_size, 8, width // patch_size, height // patch_size])
return generated_tokens.to(dtype=torch.int), patches
def unpack(dec, width, height, parallel_size=5): dec = dec.to(torch.float32).cpu().numpy().transpose(0, 2, 3, 1) dec = np.clip((dec + 1) / 2 * 255, 0, 255)
visual_img = np.zeros((parallel_size, width, height, 3), dtype=np.uint8) visual_img[:, :, :] = dec
return visual_img
@torch.inference_mode()@spaces.GPU(duration=120) # Specify a duration to avoid timeoutdef generate_image(prompt, seed=None, guidance=5, t2i_temperature=1.0): # Clear CUDA cache and avoid tracking gradients torch.cuda.empty_cache() # Set the seed for reproducible results if seed is not None: torch.manual_seed(seed) torch.cuda.manual_seed(seed) np.random.seed(seed) width = 384 height = 384 parallel_size = 5 with torch.no_grad(): messages = [{'role': '<|User|>', 'content': prompt}, {'role': '<|Assistant|>', 'content': ''}] text = vl_chat_processor.apply_sft_template_for_multi_turn_prompts(conversations=messages, sft_format=vl_chat_processor.sft_format, system_prompt='') text = text + vl_chat_processor.image_start_tag input_ids = torch.LongTensor(tokenizer.encode(text)) output, patches = generate(input_ids, width // 16 * 16, height // 16 * 16, cfg_weight=guidance, parallel_size=parallel_size, temperature=t2i_temperature) images = unpack(patches, width // 16 * 16, height // 16 * 16, parallel_size=parallel_size)
# return [Image.fromarray(images[i]).resize((768, 768), Image.LANCZOS) for i in range(parallel_size)] stime = time.time() ret_images = [image_upsample(Image.fromarray(images[i])) for i in range(parallel_size)] print(f'upsample time: {time.time() - stime}') return ret_images
@spaces.GPU(duration=60)def image_upsample(img: Image.Image) -> Image.Image: if img is None: raise Exception("Image not uploaded") width, height = img.size if width >= 5000 or height >= 5000: raise Exception("The image is too large.")
global sr_model result = sr_model.predict(img.convert('RGB')) return result
# Gradio interfacewith gr.Blocks() as demo: gr.Markdown(value="# Multimodal Understanding") with gr.Row(): image_input = gr.Image() with gr.Column(): question_input = gr.Textbox(label="Question") und_seed_input = gr.Number(label="Seed", precision=0, value=42) top_p = gr.Slider(minimum=0, maximum=1, value=0.95, step=0.05, label="top_p") temperature = gr.Slider(minimum=0, maximum=1, value=0.1, step=0.05, label="temperature") understanding_button = gr.Button("Chat") understanding_output = gr.Textbox(label="Response")
examples_inpainting = gr.Examples( label="Multimodal Understanding examples", examples=[ [ "explain this meme", "doge.png", ], [ "Convert the formula into latex code.", "equation.png", ], ], inputs=[question_input, image_input], ) gr.Markdown(value="# Text-to-Image Generation")
with gr.Row(): cfg_weight_input = gr.Slider(minimum=1, maximum=10, value=5, step=0.5, label="CFG Weight") t2i_temperature = gr.Slider(minimum=0, maximum=1, value=1.0, step=0.05, label="temperature")
prompt_input = gr.Textbox(label="Prompt. (Prompt in more detail can help produce better images!)") seed_input = gr.Number(label="Seed (Optional)", precision=0, value=12345)
generation_button = gr.Button("Generate Images")
image_output = gr.Gallery(label="Generated Images", columns=2, rows=2, height=300)
examples_t2i = gr.Examples( label="Text to image generation examples.", examples=[ "Master shifu racoon wearing drip attire as a street gangster.", "The face of a beautiful girl", "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", "A glass of red wine on a reflective surface.", "A cute and adorable baby fox with big brown eyes, autumn leaves in the background enchanting,immortal,fluffy, shiny mane,Petals,fairyism,unreal engine 5 and Octane Render,highly detailed, photorealistic, cinematic, natural colors.", "The image features an intricately designed eye set against a circular backdrop adorned with ornate swirl patterns that evoke both realism and surrealism. At the center of attention is a strikingly vivid blue iris surrounded by delicate veins radiating outward from the pupil to create depth and intensity. The eyelashes are long and dark, casting subtle shadows on the skin around them which appears smooth yet slightly textured as if aged or weathered over time.\n\nAbove the eye, there's a stone-like structure resembling part of classical architecture, adding layers of mystery and timeless elegance to the composition. This architectural element contrasts sharply but harmoniously with the organic curves surrounding it. Below the eye lies another decorative motif reminiscent of baroque artistry, further enhancing the overall sense of eternity encapsulated within each meticulously crafted detail. \n\nOverall, the atmosphere exudes a mysterious aura intertwined seamlessly with elements suggesting timelessness, achieved through the juxtaposition of realistic textures and surreal artistic flourishes. Each component\u2014from the intricate designs framing the eye to the ancient-looking stone piece above\u2014contributes uniquely towards creating a visually captivating tableau imbued with enigmatic allure.", ], inputs=prompt_input, ) understanding_button.click( multimodal_understanding, inputs=[image_input, question_input, und_seed_input, top_p, temperature], outputs=understanding_output ) generation_button.click( fn=generate_image, inputs=[prompt_input, seed_input, cfg_weight_input, t2i_temperature], outputs=image_output )
demo.launch(share=True)12345678910111213141516171819202122232425262728293031# Copyright (c) 2023-2024 DeepSeek.## Permission is hereby granted, free of charge, to any person obtaining a copy of# this software and associated documentation files (the "Software"), to deal in# the Software without restriction, including without limitation the rights to# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of# the Software, and to permit persons to whom the Software is furnished to do so,# subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# check if python version is above 3.10import sys
if sys.version_info >= (3, 10): print("Python version is above 3.10, patching the collections module.") # Monkey patch collections import collections import collections.abc
for type_name in collections.abc.__all__: setattr(collections, type_name, getattr(collections.abc, type_name))12345678910111213141516171819202122232425262728# Copyright (c) 2023-2024 DeepSeek.## Permission is hereby granted, free of charge, to any person obtaining a copy of# this software and associated documentation files (the "Software"), to deal in# the Software without restriction, including without limitation the rights to# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of# the Software, and to permit persons to whom the Software is furnished to do so,# subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from .image_processing_vlm import VLMImageProcessorfrom .modeling_vlm import MultiModalityCausalLMfrom .processing_vlm import VLChatProcessor
__all__ = [ "VLMImageProcessor", "VLChatProcessor", "MultiModalityCausalLM",]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122# Copyright (c) 2023-2024 DeepSeek.## Permission is hereby granted, free of charge, to any person obtaining a copy of# this software and associated documentation files (the "Software"), to deal in# the Software without restriction, including without limitation the rights to# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of# the Software, and to permit persons to whom the Software is furnished to do so,# subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from typing import Dict, List, Literal, Optional, Tuple, Union
import torchimport torch.nn as nnimport torchvision.transformsfrom einops import rearrange
from janus.models.siglip_vit import create_siglip_vit
class CLIPVisionTower(nn.Module): def __init__( self, model_name: str = "siglip_large_patch16_384", image_size: Union[Tuple[int, int], int] = 336, select_feature: str = "patch", select_layer: int = -2, select_layers: list = None, ckpt_path: str = "", pixel_mean: Optional[List[float]] = None, pixel_std: Optional[List[float]] = None, **kwargs, ): super().__init__()
self.model_name = model_name self.select_feature = select_feature self.select_layer = select_layer self.select_layers = select_layers
vision_tower_params = { "model_name": model_name, "image_size": image_size, "ckpt_path": ckpt_path, "select_layer": select_layer, } vision_tower_params.update(kwargs) self.vision_tower, self.forward_kwargs = self.build_vision_tower( vision_tower_params )
if pixel_mean is not None and pixel_std is not None: image_norm = torchvision.transforms.Normalize( mean=pixel_mean, std=pixel_std ) else: image_norm = None
self.image_norm = image_norm
def build_vision_tower(self, vision_tower_params): if self.model_name.startswith("siglip"): self.select_feature = "same" vision_tower = create_siglip_vit(**vision_tower_params) forward_kwargs = dict()
elif self.model_name.startswith("sam"): vision_tower = create_sam_vit(**vision_tower_params) forward_kwargs = dict()
else: # huggingface from transformers import CLIPVisionModel
vision_tower = CLIPVisionModel.from_pretrained(**vision_tower_params) forward_kwargs = dict(output_hidden_states=True)
return vision_tower, forward_kwargs
def feature_select(self, image_forward_outs): if isinstance(image_forward_outs, torch.Tensor): # the output has been the self.select_layer"s features image_features = image_forward_outs else: image_features = image_forward_outs.hidden_states[self.select_layer]
if self.select_feature == "patch": # if the output has cls_token image_features = image_features[:, 1:] elif self.select_feature == "cls_patch": image_features = image_features elif self.select_feature == "same": image_features = image_features
else: raise ValueError(f"Unexpected select feature: {self.select_feature}") return image_features
def forward(self, images): """
Args: images (torch.Tensor): [b, 3, H, W]
Returns: image_features (torch.Tensor): [b, n_patch, d] """
if self.image_norm is not None: images = self.image_norm(images)
image_forward_outs = self.vision_tower(images, **self.forward_kwargs) image_features = self.feature_select(image_forward_outs) return image_features123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208# Copyright (c) 2023-2024 DeepSeek.## Permission is hereby granted, free of charge, to any person obtaining a copy of# this software and associated documentation files (the "Software"), to deal in# the Software without restriction, including without limitation the rights to# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of# the Software, and to permit persons to whom the Software is furnished to do so,# subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from typing import List, Tuple, Union
import numpy as npimport torchimport torchvisionimport torchvision.transforms.functionalfrom PIL import Imagefrom transformers import AutoImageProcessor, PretrainedConfigfrom transformers.image_processing_utils import BaseImageProcessor, BatchFeaturefrom transformers.image_utils import to_numpy_arrayfrom transformers.utils import logging
logger = logging.get_logger(__name__)
ImageType = Union[np.ndarray, torch.Tensor, Image.Image]IMAGENET_MEAN = (0.48145466, 0.4578275, 0.40821073)IMAGENET_STD = (0.26862954, 0.26130258, 0.27577711)IMAGENET_INCEPTION_MEAN = (0.5, 0.5, 0.5)IMAGENET_INCEPTION_STD = (0.5, 0.5, 0.5)
def expand2square(pil_img, background_color): width, height = pil_img.size if width == height: return pil_img elif width > height: result = Image.new(pil_img.mode, (width, width), background_color) result.paste(pil_img, (0, (width - height) // 2)) return result else: result = Image.new(pil_img.mode, (height, height), background_color) result.paste(pil_img, ((height - width) // 2, 0)) return result
class VLMImageProcessorConfig(PretrainedConfig): model_type = "deepseek_vlm" image_size: int min_size: int image_mean: Union[Tuple[float, float, float], List[float]] image_std: Union[Tuple[float, float, float], List[float]] rescale_factor: float do_normalize: bool
def __init__( self, image_size: int, min_size: int = 14, image_mean: Union[Tuple[float, float, float], List[float]] = ( 0.48145466, 0.4578275, 0.40821073, ), image_std: Union[Tuple[float, float, float], List[float]] = ( 0.26862954, 0.26130258, 0.27577711, ), rescale_factor: float = 1.0 / 255.0, do_normalize: bool = True, **kwargs, ): self.image_size = image_size self.min_size = min_size self.image_mean = image_mean self.image_std = image_std self.rescale_factor = rescale_factor self.do_normalize = do_normalize
super().__init__(**kwargs)
class VLMImageProcessor(BaseImageProcessor): model_input_names = ["pixel_values"]
def __init__( self, image_size: int, min_size: int = 14, image_mean: Union[Tuple[float, float, float], List[float]] = ( 0.48145466, 0.4578275, 0.40821073, ), image_std: Union[Tuple[float, float, float], List[float]] = ( 0.26862954, 0.26130258, 0.27577711, ), rescale_factor: float = 1.0 / 255.0, do_normalize: bool = True, **kwargs, ): super().__init__(**kwargs)
self.image_size = image_size self.rescale_factor = rescale_factor self.image_mean = image_mean self.image_std = image_std self.min_size = min_size self.do_normalize = do_normalize
if image_mean is None: self.background_color = (127, 127, 127) else: self.background_color = tuple([int(x * 255) for x in image_mean])
def resize(self, pil_img: Image) -> np.ndarray: """
Args: pil_img (PIL.Image): [H, W, 3] in PIL.Image in RGB
Returns: x (np.ndarray): [3, self.image_size, self.image_size] """
width, height = pil_img.size max_size = max(width, height)
size = [ max(int(height / max_size * self.image_size), self.min_size), max(int(width / max_size * self.image_size), self.min_size), ]
if width <= 0 or height <= 0 or size[0] <= 0 or size[1] <= 0: print(f"orig size = {pil_img.size}, new size = {size}") raise ValueError("Invalid size!")
pil_img = torchvision.transforms.functional.resize( pil_img, size, interpolation=torchvision.transforms.functional.InterpolationMode.BICUBIC, antialias=True, )
pil_img = expand2square(pil_img, self.background_color) x = to_numpy_array(pil_img)
# [H, W, 3] -> [3, H, W] x = np.transpose(x, (2, 0, 1))
return x
def preprocess(self, images, return_tensors: str = "pt", **kwargs) -> BatchFeature: # resize and pad to [self.image_size, self.image_size] # then convert from [H, W, 3] to [3, H, W] images: List[np.ndarray] = [self.resize(image) for image in images]
# resacle from [0, 255] -> [0, 1] images = [ self.rescale( image=image, scale=self.rescale_factor, input_data_format="channels_first", ) for image in images ]
# normalize if self.do_normalize: images = [ self.normalize( image=image, mean=self.image_mean, std=self.image_std, input_data_format="channels_first", ) for image in images ]
data = {"pixel_values": images} return BatchFeature(data=data, tensor_type=return_tensors)
@property def default_shape(self): return [3, self.image_size, self.image_size]
AutoImageProcessor.register(VLMImageProcessorConfig, VLMImageProcessor)
if __name__ == "__main__": image_processor = VLMImageProcessor( image_size=1024, image_mean=IMAGENET_INCEPTION_MEAN, image_std=IMAGENET_INCEPTION_STD, do_normalize=True, )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272# Copyright (c) 2023-2024 DeepSeek.## Permission is hereby granted, free of charge, to any person obtaining a copy of# this software and associated documentation files (the "Software"), to deal in# the Software without restriction, including without limitation the rights to# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of# the Software, and to permit persons to whom the Software is furnished to do so,# subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import torchfrom attrdict import AttrDictfrom einops import rearrangefrom transformers import ( AutoConfig, AutoModelForCausalLM, LlamaConfig, LlamaForCausalLM, PreTrainedModel,)from transformers.configuration_utils import PretrainedConfig
from janus.models.clip_encoder import CLIPVisionTowerfrom janus.models.projector import MlpProjector
class vision_head(torch.nn.Module): def __init__(self, params): super().__init__() self.output_mlp_projector = torch.nn.Linear( params.n_embed, params.image_token_embed ) self.vision_activation = torch.nn.GELU() self.vision_head = torch.nn.Linear( params.image_token_embed, params.image_token_size )
def forward(self, x): x = self.output_mlp_projector(x) x = self.vision_activation(x) x = self.vision_head(x) return x
def model_name_to_cls(cls_name): if "MlpProjector" in cls_name: cls = MlpProjector
elif "CLIPVisionTower" in cls_name: cls = CLIPVisionTower
elif "VQ" in cls_name: from janus.models.vq_model import VQ_models
cls = VQ_models[cls_name] elif "vision_head" in cls_name: cls = vision_head else: raise ValueError(f"class_name {cls_name} is invalid.")
return cls
class VisionConfig(PretrainedConfig): model_type = "vision" cls: str = "" params: AttrDict = {}
def __init__(self, **kwargs): super().__init__(**kwargs)
self.cls = kwargs.get("cls", "") if not isinstance(self.cls, str): self.cls = self.cls.__name__
self.params = AttrDict(kwargs.get("params", {}))
class AlignerConfig(PretrainedConfig): model_type = "aligner" cls: str = "" params: AttrDict = {}
def __init__(self, **kwargs): super().__init__(**kwargs)
self.cls = kwargs.get("cls", "") if not isinstance(self.cls, str): self.cls = self.cls.__name__
self.params = AttrDict(kwargs.get("params", {}))
class GenVisionConfig(PretrainedConfig): model_type = "gen_vision" cls: str = "" params: AttrDict = {}
def __init__(self, **kwargs): super().__init__(**kwargs)
self.cls = kwargs.get("cls", "") if not isinstance(self.cls, str): self.cls = self.cls.__name__
self.params = AttrDict(kwargs.get("params", {}))
class GenAlignerConfig(PretrainedConfig): model_type = "gen_aligner" cls: str = "" params: AttrDict = {}
def __init__(self, **kwargs): super().__init__(**kwargs)
self.cls = kwargs.get("cls", "") if not isinstance(self.cls, str): self.cls = self.cls.__name__
self.params = AttrDict(kwargs.get("params", {}))
class GenHeadConfig(PretrainedConfig): model_type = "gen_head" cls: str = "" params: AttrDict = {}
def __init__(self, **kwargs): super().__init__(**kwargs)
self.cls = kwargs.get("cls", "") if not isinstance(self.cls, str): self.cls = self.cls.__name__
self.params = AttrDict(kwargs.get("params", {}))
class MultiModalityConfig(PretrainedConfig): model_type = "multi_modality" vision_config: VisionConfig aligner_config: AlignerConfig
gen_vision_config: GenVisionConfig gen_aligner_config: GenAlignerConfig gen_head_config: GenHeadConfig
language_config: LlamaConfig
def __init__(self, **kwargs): super().__init__(**kwargs) vision_config = kwargs.get("vision_config", {}) self.vision_config = VisionConfig(**vision_config)
aligner_config = kwargs.get("aligner_config", {}) self.aligner_config = AlignerConfig(**aligner_config)
gen_vision_config = kwargs.get("gen_vision_config", {}) self.gen_vision_config = GenVisionConfig(**gen_vision_config)
gen_aligner_config = kwargs.get("gen_aligner_config", {}) self.gen_aligner_config = GenAlignerConfig(**gen_aligner_config)
gen_head_config = kwargs.get("gen_head_config", {}) self.gen_head_config = GenHeadConfig(**gen_head_config)
language_config = kwargs.get("language_config", {}) if isinstance(language_config, LlamaConfig): self.language_config = language_config else: self.language_config = LlamaConfig(**language_config)
class MultiModalityPreTrainedModel(PreTrainedModel): config_class = MultiModalityConfig base_model_prefix = "multi_modality" _no_split_modules = [] _skip_keys_device_placement = "past_key_values"
class MultiModalityCausalLM(MultiModalityPreTrainedModel): def __init__(self, config: MultiModalityConfig): super().__init__(config)
vision_config = config.vision_config vision_cls = model_name_to_cls(vision_config.cls) self.vision_model = vision_cls(**vision_config.params)
aligner_config = config.aligner_config aligner_cls = model_name_to_cls(aligner_config.cls) self.aligner = aligner_cls(aligner_config.params)
gen_vision_config = config.gen_vision_config gen_vision_cls = model_name_to_cls(gen_vision_config.cls) self.gen_vision_model = gen_vision_cls()
gen_aligner_config = config.gen_aligner_config gen_aligner_cls = model_name_to_cls(gen_aligner_config.cls) self.gen_aligner = gen_aligner_cls(gen_aligner_config.params)
gen_head_config = config.gen_head_config gen_head_cls = model_name_to_cls(gen_head_config.cls) self.gen_head = gen_head_cls(gen_head_config.params)
self.gen_embed = torch.nn.Embedding( gen_vision_config.params.image_token_size, gen_vision_config.params.n_embed )
language_config = config.language_config self.language_model = LlamaForCausalLM(language_config)
def prepare_inputs_embeds( self, input_ids: torch.LongTensor, pixel_values: torch.FloatTensor, images_seq_mask: torch.LongTensor, images_emb_mask: torch.LongTensor, **kwargs, ): """
Args: input_ids (torch.LongTensor): [b, T] pixel_values (torch.FloatTensor): [b, n_images, 3, h, w] images_seq_mask (torch.BoolTensor): [b, T] images_emb_mask (torch.BoolTensor): [b, n_images, n_image_tokens]
assert torch.sum(images_seq_mask) == torch.sum(images_emb_mask)
Returns: input_embeds (torch.Tensor): [b, T, D] """
bs, n = pixel_values.shape[0:2] images = rearrange(pixel_values, "b n c h w -> (b n) c h w") # [b x n, T2, D] images_embeds = self.aligner(self.vision_model(images))
# [b x n, T2, D] -> [b, n x T2, D] images_embeds = rearrange(images_embeds, "(b n) t d -> b (n t) d", b=bs, n=n) # [b, n, T2] -> [b, n x T2] images_emb_mask = rearrange(images_emb_mask, "b n t -> b (n t)")
# [b, T, D] input_ids[input_ids < 0] = 0 # ignore the image embeddings inputs_embeds = self.language_model.get_input_embeddings()(input_ids)
# replace with the image embeddings inputs_embeds[images_seq_mask] = images_embeds[images_emb_mask]
return inputs_embeds
def prepare_gen_img_embeds(self, image_ids: torch.LongTensor): return self.gen_aligner(self.gen_embed(image_ids))
AutoConfig.register("vision", VisionConfig)AutoConfig.register("aligner", AlignerConfig)AutoConfig.register("gen_vision", GenVisionConfig)AutoConfig.register("gen_aligner", GenAlignerConfig)AutoConfig.register("gen_head", GenHeadConfig)AutoConfig.register("multi_modality", MultiModalityConfig)AutoModelForCausalLM.register(MultiModalityConfig, MultiModalityCausalLM)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418# Copyright (c) 2023-2024 DeepSeek.## Permission is hereby granted, free of charge, to any person obtaining a copy of# this software and associated documentation files (the "Software"), to deal in# the Software without restriction, including without limitation the rights to# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of# the Software, and to permit persons to whom the Software is furnished to do so,# subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from dataclasses import dataclassfrom typing import Dict, List
import torchfrom PIL.Image import Imagefrom transformers import LlamaTokenizerFastfrom transformers.processing_utils import ProcessorMixin
from janus.models.image_processing_vlm import VLMImageProcessorfrom janus.utils.conversation import get_conv_template
class DictOutput(object): def keys(self): return self.__dict__.keys()
def __getitem__(self, item): return self.__dict__[item]
def __setitem__(self, key, value): self.__dict__[key] = value
@dataclassclass VLChatProcessorOutput(DictOutput): sft_format: str input_ids: torch.Tensor pixel_values: torch.Tensor num_image_tokens: torch.IntTensor
def __len__(self): return len(self.input_ids)
@dataclassclass BatchedVLChatProcessorOutput(DictOutput): sft_format: List[str] input_ids: torch.Tensor pixel_values: torch.Tensor attention_mask: torch.Tensor images_seq_mask: torch.BoolTensor images_emb_mask: torch.BoolTensor
def to(self, device, dtype=torch.bfloat16): self.input_ids = self.input_ids.to(device) self.attention_mask = self.attention_mask.to(device) self.images_seq_mask = self.images_seq_mask.to(device) self.images_emb_mask = self.images_emb_mask.to(device) self.pixel_values = self.pixel_values.to(device=device, dtype=dtype) return self
class VLChatProcessor(ProcessorMixin): image_processor_class = "AutoImageProcessor" tokenizer_class = ("LlamaTokenizer", "LlamaTokenizerFast")
attributes = ["image_processor", "tokenizer"]
system_prompt = ( "You are a helpful language and vision assistant. " "You are able to understand the visual content that the user provides, " "and assist the user with a variety of tasks using natural language." )
def __init__( self, image_processor: VLMImageProcessor, tokenizer: LlamaTokenizerFast, image_tag: str = "<image_placeholder>", image_start_tag: str = "<begin_of_image>", image_end_tag: str = "<end_of_image>", pad_tag: str = "<|▁pad▁|>", num_image_tokens: int = 576, add_special_token: bool = False, sft_format: str = "deepseek", mask_prompt: bool = True, ignore_id: int = -100, **kwargs, ): self.image_processor = image_processor self.tokenizer = tokenizer
image_id = self.tokenizer.vocab.get(image_tag) if image_id is None: special_tokens = [image_tag] special_tokens_dict = {"additional_special_tokens": special_tokens} self.tokenizer.add_special_tokens(special_tokens_dict) print(f"Add image tag = {image_tag} to the tokenizer")
self.image_tag = image_tag self.image_start_tag = image_start_tag self.image_end_tag = image_end_tag self.pad_tag = pad_tag
self.num_image_tokens = num_image_tokens self.add_special_token = add_special_token self.sft_format = sft_format self.mask_prompt = mask_prompt self.ignore_id = ignore_id
super().__init__( image_processor, tokenizer, image_tag, num_image_tokens, add_special_token, sft_format, mask_prompt, ignore_id, **kwargs, )
def new_chat_template(self): conv = get_conv_template(self.sft_format) conv.set_system_message(self.system_prompt) return conv
def apply_sft_template_for_multi_turn_prompts( self, conversations: List[Dict[str, str]], sft_format: str = "deepseek", system_prompt: str = "", ): """ Applies the SFT template to conversation.
An example of conversation: conversation = [ { "role": "User", "content": "<image_placeholder> is Figure 1.\n<image_placeholder> is Figure 2.\nWhich image is brighter?", "images": [ "./multi-images/attribute_comparison_1.png", "./multi-images/attribute_comparison_2.png" ] }, { "role": "Assistant", "content": "" } ]
Args: conversations (List[Dict]): A conversation with a List of Dict[str, str] text. sft_format (str, optional): The format of the SFT template to use. Defaults to "deepseek". system_prompt (str, optional): The system prompt to use in the SFT template. Defaults to "".
Returns: sft_prompt (str): The formatted text. """
conv = get_conv_template(sft_format) conv.set_system_message(system_prompt) for message in conversations: conv.append_message(message["role"], message["content"].strip()) sft_prompt = conv.get_prompt().strip()
return sft_prompt
@property def image_token(self): return self.image_tag
@property def image_id(self): image_id = self.tokenizer.vocab.get(self.image_tag) return image_id
@property def image_start_id(self): image_start_id = self.tokenizer.vocab.get(self.image_start_tag) return image_start_id
@property def image_end_id(self): image_end_id = self.tokenizer.vocab.get(self.image_end_tag) return image_end_id
@property def image_start_token(self): return self.image_start_tag
@property def image_end_token(self): return self.image_end_tag
@property def pad_id(self): pad_id = self.tokenizer.vocab.get(self.pad_tag) # pad_id = self.tokenizer.pad_token_id # if pad_id is None: # pad_id = self.tokenizer.eos_token_id
return pad_id
def add_image_token( self, image_indices: List[int], input_ids: torch.LongTensor, ): """
Args: image_indices (List[int]): [index_0, index_1, ..., index_j] input_ids (torch.LongTensor): [N]
Returns: input_ids (torch.LongTensor): [N + image tokens] num_image_tokens (torch.IntTensor): [n_images] """
input_slices = []
start = 0 for index in image_indices: if self.add_special_token: end = index + 1 else: end = index
# original text tokens input_slices.append(input_ids[start:end])
# add boi, image tokens, eoi and set the mask as False input_slices.append(self.image_start_id * torch.ones((1), dtype=torch.long)) input_slices.append( self.image_id * torch.ones((self.num_image_tokens,), dtype=torch.long) ) input_slices.append(self.image_end_id * torch.ones((1), dtype=torch.long)) start = index + 1
# the left part input_slices.append(input_ids[start:])
# concat all slices input_ids = torch.cat(input_slices, dim=0) num_image_tokens = torch.IntTensor([self.num_image_tokens] * len(image_indices))
return input_ids, num_image_tokens
def process_one( self, prompt: str = None, conversations: List[Dict[str, str]] = None, images: List[Image] = None, **kwargs, ): """
Args: prompt (str): the formatted prompt; conversations (List[Dict]): conversations with a list of messages; images (List[ImageType]): the list of images; **kwargs:
Returns: outputs (BaseProcessorOutput): the output of the processor, - input_ids (torch.LongTensor): [N + image tokens] - target_ids (torch.LongTensor): [N + image tokens] - images (torch.FloatTensor): [n_images, 3, H, W] - image_id (int): the id of the image token - num_image_tokens (List[int]): the number of image tokens """
assert ( prompt is None or conversations is None ), "prompt and conversations cannot be used at the same time."
if prompt is None: # apply sft format sft_format = self.apply_sft_template_for_multi_turn_prompts( conversations=conversations, sft_format=self.sft_format, system_prompt=self.system_prompt, ) else: sft_format = prompt
# tokenize input_ids = self.tokenizer.encode(sft_format) input_ids = torch.LongTensor(input_ids)
# add image tokens to the input_ids image_token_mask: torch.BoolTensor = input_ids == self.image_id image_indices = image_token_mask.nonzero() input_ids, num_image_tokens = self.add_image_token( image_indices=image_indices, input_ids=input_ids, )
# load images images_outputs = self.image_processor(images, return_tensors="pt")
prepare = VLChatProcessorOutput( sft_format=sft_format, input_ids=input_ids, pixel_values=images_outputs.pixel_values, num_image_tokens=num_image_tokens, )
return prepare
def __call__( self, *, prompt: str = None, conversations: List[Dict[str, str]] = None, images: List[Image] = None, force_batchify: bool = True, **kwargs, ): """
Args: prompt (str): the formatted prompt; conversations (List[Dict]): conversations with a list of messages; images (List[ImageType]): the list of images; force_batchify (bool): force batchify the inputs; **kwargs:
Returns: outputs (BaseProcessorOutput): the output of the processor, - input_ids (torch.LongTensor): [N + image tokens] - images (torch.FloatTensor): [n_images, 3, H, W] - image_id (int): the id of the image token - num_image_tokens (List[int]): the number of image tokens """
prepare = self.process_one( prompt=prompt, conversations=conversations, images=images )
if force_batchify: prepare = self.batchify([prepare])
return prepare
def batchify( self, prepare_list: List[VLChatProcessorOutput] ) -> BatchedVLChatProcessorOutput: """ Preprocesses the inputs for multimodal inference.
Args: prepare_list (List[VLChatProcessorOutput]): A list of VLChatProcessorOutput.
Returns: BatchedVLChatProcessorOutput: A dictionary of the inputs to use for multimodal inference. """
batch_size = len(prepare_list) sft_format = [] n_images = [] seq_lens = [] for prepare in prepare_list: n_images.append(len(prepare.num_image_tokens)) seq_lens.append(len(prepare))
input_token_max_len = max(seq_lens) max_n_images = max(1, max(n_images))
batched_input_ids = torch.full( (batch_size, input_token_max_len), self.pad_id ).long() # FIXME batched_attention_mask = torch.zeros((batch_size, input_token_max_len)).long() batched_pixel_values = torch.zeros( (batch_size, max_n_images, *self.image_processor.default_shape) ).float() batched_images_seq_mask = torch.zeros((batch_size, input_token_max_len)).bool() batched_images_emb_mask = torch.zeros( (batch_size, max_n_images, self.num_image_tokens) ).bool()
for i, prepare in enumerate(prepare_list): input_ids = prepare.input_ids seq_len = len(prepare) n_image = len(prepare.num_image_tokens) # left-padding batched_attention_mask[i, -seq_len:] = 1 batched_input_ids[i, -seq_len:] = torch.LongTensor(input_ids) batched_images_seq_mask[i, -seq_len:] = input_ids == self.image_id
if n_image > 0: batched_pixel_values[i, :n_image] = prepare.pixel_values for j, n_image_tokens in enumerate(prepare.num_image_tokens): batched_images_emb_mask[i, j, :n_image_tokens] = True
sft_format.append(prepare.sft_format)
batched_prepares = BatchedVLChatProcessorOutput( input_ids=batched_input_ids, attention_mask=batched_attention_mask, pixel_values=batched_pixel_values, images_seq_mask=batched_images_seq_mask, images_emb_mask=batched_images_emb_mask, sft_format=sft_format, )
return batched_prepares123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100# Copyright (c) 2023-2024 DeepSeek.## Permission is hereby granted, free of charge, to any person obtaining a copy of# this software and associated documentation files (the "Software"), to deal in# the Software without restriction, including without limitation the rights to# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of# the Software, and to permit persons to whom the Software is furnished to do so,# subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from typing import Tuple, Union
import torchimport torch.nn as nnfrom attrdict import AttrDict
class MlpProjector(nn.Module): def __init__(self, cfg): super().__init__()
self.cfg = cfg
if cfg.projector_type == "identity": modules = nn.Identity()
elif cfg.projector_type == "linear": modules = nn.Linear(cfg.input_dim, cfg.n_embed)
elif cfg.projector_type == "mlp_gelu": mlp_depth = cfg.get("depth", 1) modules = [nn.Linear(cfg.input_dim, cfg.n_embed)] for _ in range(1, mlp_depth): modules.append(nn.GELU()) modules.append(nn.Linear(cfg.n_embed, cfg.n_embed)) modules = nn.Sequential(*modules)
elif cfg.projector_type == "low_high_hybrid_split_mlp_gelu": mlp_depth = cfg.get("depth", 1) self.high_up_proj = nn.Linear(cfg.input_dim, cfg.n_embed // 2) self.low_up_proj = nn.Linear(cfg.input_dim, cfg.n_embed // 2)
modules = [] for _ in range(1, mlp_depth): modules.append(nn.GELU()) modules.append(nn.Linear(cfg.n_embed, cfg.n_embed)) modules = nn.Sequential(*modules)
else: raise ValueError(f"Unknown projector type: {cfg.projector_type}")
self.layers = modules
def forward( self, x_or_tuple: Union[Tuple[torch.Tensor, torch.Tensor], torch.Tensor] ): """
Args: x_or_tuple (Union[Tuple[torch.Tensor, torch.Tensor], torch.Tensor]: if it is a tuple of torch.Tensor, then it comes from the hybrid vision encoder, and x = high_res_x, low_res_x); otherwise it is the feature from the single vision encoder.
Returns: x (torch.Tensor): [b, s, c] """
if isinstance(x_or_tuple, tuple): # self.cfg.projector_type == "low_high_hybrid_split_mlp_gelu": high_x, low_x = x_or_tuple high_x = self.high_up_proj(high_x) low_x = self.low_up_proj(low_x) x = torch.concat([high_x, low_x], dim=-1) else: x = x_or_tuple
return self.layers(x)
if __name__ == "__main__": cfg = AttrDict( input_dim=1024, n_embed=2048, depth=2, projector_type="low_high_hybrid_split_mlp_gelu", ) inputs = (torch.rand(4, 576, 1024), torch.rand(4, 576, 1024))
m = MlpProjector(cfg) out = m(inputs) print(out.shape)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681# Copyright (c) 2023-2024 DeepSeek.## Permission is hereby granted, free of charge, to any person obtaining a copy of# this software and associated documentation files (the "Software"), to deal in# the Software without restriction, including without limitation the rights to# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of# the Software, and to permit persons to whom the Software is furnished to do so,# subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# https://github.com/huggingface/pytorch-image-models/blob/main/timm/models/vision_transformer.pyimport mathimport warningsfrom dataclasses import dataclassfrom functools import partialfrom typing import ( Callable, Dict, Final, List, Literal, Optional, Sequence, Set, Tuple, Type, Union,)
import torchimport torch.nn as nnimport torch.nn.functional as Ffrom timm.layers import ( AttentionPoolLatent, DropPath, LayerType, Mlp, PatchDropout, PatchEmbed, resample_abs_pos_embed,)from timm.models._manipulate import checkpoint_seq, named_apply
def _no_grad_trunc_normal_(tensor, mean, std, a, b): # Cut & paste from PyTorch official master until it's in a few official releases - RW # Method based on https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf def norm_cdf(x): # Computes standard normal cumulative distribution function return (1.0 + math.erf(x / math.sqrt(2.0))) / 2.0
if (mean < a - 2 * std) or (mean > b + 2 * std): warnings.warn( "mean is more than 2 std from [a, b] in nn.init.trunc_normal_. " "The distribution of values may be incorrect.", stacklevel=2, )
with torch.no_grad(): # Values are generated by using a truncated uniform distribution and # then using the inverse CDF for the normal distribution. # Get upper and lower cdf values l = norm_cdf((a - mean) / std) # noqa: E741 u = norm_cdf((b - mean) / std)
# Uniformly fill tensor with values from [l, u], then translate to # [2l-1, 2u-1]. tensor.uniform_(2 * l - 1, 2 * u - 1)
# Use inverse cdf transform for normal distribution to get truncated # standard normal tensor.erfinv_()
# Transform to proper mean, std tensor.mul_(std * math.sqrt(2.0)) tensor.add_(mean)
# Clamp to ensure it's in the proper range tensor.clamp_(min=a, max=b) return tensor
def trunc_normal_(tensor, mean=0.0, std=1.0, a=-2.0, b=2.0): # type: (torch.Tensor, float, float, float, float) -> torch.Tensor r"""The original timm.models.layers.weight_init.trunc_normal_ can not handle bfloat16 yet, here we first convert the tensor to float32, apply the trunc_normal_() in float32, and then convert it back to its original dtype. Fills the input Tensor with values drawn from a truncated normal distribution. The values are effectively drawn from the normal distribution :math:`\mathcal{N}(\text{mean}, \text{std}^2)` with values outside :math:`[a, b]` redrawn until they are within the bounds. The method used for generating the random values works best when :math:`a \leq \text{mean} \leq b`. Args: tensor: an n-dimensional `torch.Tensor` mean: the mean of the normal distribution std: the standard deviation of the normal distribution a: the minimum cutoff value b: the maximum cutoff value Examples: >>> w = torch.empty(3, 5) >>> nn.init.trunc_normal_(w) """
with torch.no_grad(): dtype = tensor.dtype tensor_fp32 = tensor.float() tensor_fp32 = _no_grad_trunc_normal_(tensor_fp32, mean, std, a, b) tensor_dtype = tensor_fp32.to(dtype=dtype) tensor.copy_(tensor_dtype)
def init_weights(self): if self.pos_embed is not None: trunc_normal_(self.pos_embed, std=self.pos_embed.shape[1] ** -0.5) trunc_normal_(self.latent, std=self.latent_dim**-0.5)
def init_weights_vit_timm(module: nn.Module, name: str = "") -> None: """ViT weight initialization, original timm impl (for reproducibility)""" if isinstance(module, nn.Linear): trunc_normal_(module.weight, std=0.02) if module.bias is not None: nn.init.zeros_(module.bias) elif hasattr(module, "init_weights"): module.init_weights()
class Attention(nn.Module): fused_attn: Final[bool]
def __init__( self, dim: int, num_heads: int = 8, qkv_bias: bool = False, qk_norm: bool = False, attn_drop: float = 0.0, proj_drop: float = 0.0, norm_layer: nn.Module = nn.LayerNorm, ) -> None: super().__init__() assert dim % num_heads == 0, "dim should be divisible by num_heads" self.num_heads = num_heads self.head_dim = dim // num_heads self.scale = self.head_dim**-0.5 # self.fused_attn = use_fused_attn() self.fused_attn = True
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) self.q_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity() self.k_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity() self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(dim, dim) self.proj_drop = nn.Dropout(proj_drop) if proj_drop > 0.0 else nn.Identity()
def forward(self, x: torch.Tensor) -> torch.Tensor: B, N, C = x.shape qkv = ( self.qkv(x) .reshape(B, N, 3, self.num_heads, self.head_dim) .permute(2, 0, 3, 1, 4) ) q, k, v = qkv.unbind(0) q, k = self.q_norm(q), self.k_norm(k)
if self.fused_attn: x = F.scaled_dot_product_attention( q, k, v, dropout_p=self.attn_drop.p if self.training else 0.0, ) else: q = q * self.scale attn = q @ k.transpose(-2, -1) attn = attn.softmax(dim=-1) attn = self.attn_drop(attn) x = attn @ v
x = x.transpose(1, 2).reshape(B, N, C) x = self.proj(x) x = self.proj_drop(x) return x
class LayerScale(nn.Module): def __init__( self, dim: int, init_values: float = 1e-5, inplace: bool = False, ) -> None: super().__init__() self.inplace = inplace self.gamma = nn.Parameter(init_values * torch.ones(dim))
def forward(self, x: torch.Tensor) -> torch.Tensor: return x.mul_(self.gamma) if self.inplace else x * self.gamma
class Block(nn.Module): def __init__( self, dim: int, num_heads: int, mlp_ratio: float = 4.0, qkv_bias: bool = False, qk_norm: bool = False, proj_drop: float = 0.0, attn_drop: float = 0.0, init_values: Optional[float] = None, drop_path: float = 0.0, act_layer: nn.Module = nn.GELU, norm_layer: nn.Module = nn.LayerNorm, mlp_layer: nn.Module = Mlp, ) -> None: super().__init__() self.norm1 = norm_layer(dim) self.attn = Attention( dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_norm=qk_norm, attn_drop=attn_drop, proj_drop=proj_drop, norm_layer=norm_layer, ) self.ls1 = ( LayerScale(dim, init_values=init_values) if init_values else nn.Identity() ) self.drop_path1 = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
self.norm2 = norm_layer(dim) self.mlp = mlp_layer( in_features=dim, hidden_features=int(dim * mlp_ratio), act_layer=act_layer, drop=proj_drop, ) self.ls2 = ( LayerScale(dim, init_values=init_values) if init_values else nn.Identity() ) self.drop_path2 = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
def forward(self, x: torch.Tensor) -> torch.Tensor: x = x + self.drop_path1(self.ls1(self.attn(self.norm1(x)))) x = x + self.drop_path2(self.ls2(self.mlp(self.norm2(x)))) return x
class VisionTransformer(nn.Module): """Vision Transformer
A PyTorch impl of : `An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale` - https://arxiv.org/abs/2010.11929 """
dynamic_img_size: Final[bool]
def __init__( self, img_size: Union[int, Tuple[int, int]] = 224, patch_size: Union[int, Tuple[int, int]] = 16, in_chans: int = 3, num_classes: int = 1000, global_pool: Literal["", "avg", "token", "map"] = "token", embed_dim: int = 768, depth: int = 12, num_heads: int = 12, mlp_ratio: float = 4.0, qkv_bias: bool = True, qk_norm: bool = False, init_values: Optional[float] = None, class_token: bool = True, no_embed_class: bool = False, reg_tokens: int = 0, pre_norm: bool = False, fc_norm: Optional[bool] = None, dynamic_img_size: bool = False, dynamic_img_pad: bool = False, drop_rate: float = 0.0, pos_drop_rate: float = 0.0, patch_drop_rate: float = 0.0, proj_drop_rate: float = 0.0, attn_drop_rate: float = 0.0, drop_path_rate: float = 0.0, weight_init: Literal["skip", "jax", "jax_nlhb", "moco", ""] = "", embed_layer: Callable = PatchEmbed, norm_layer: Optional[LayerType] = None, act_layer: Optional[LayerType] = None, block_fn: Type[nn.Module] = Block, mlp_layer: Type[nn.Module] = Mlp, ignore_head: bool = False, ) -> None: """ Args: img_size: Input image size. patch_size: Patch size. in_chans: Number of image input channels. num_classes: Mumber of classes for classification head. global_pool: Type of global pooling for final sequence (default: 'token'). embed_dim: Transformer embedding dimension. depth: Depth of transformer. num_heads: Number of attention heads. mlp_ratio: Ratio of mlp hidden dim to embedding dim. qkv_bias: Enable bias for qkv projections if True. init_values: Layer-scale init values (layer-scale enabled if not None). class_token: Use class token. no_embed_class: Don't include position embeddings for class (or reg) tokens. reg_tokens: Number of register tokens. fc_norm: Pre head norm after pool (instead of before), if None, enabled when global_pool == 'avg'. drop_rate: Head dropout rate. pos_drop_rate: Position embedding dropout rate. attn_drop_rate: Attention dropout rate. drop_path_rate: Stochastic depth rate. weight_init: Weight initialization scheme. embed_layer: Patch embedding layer. norm_layer: Normalization layer. act_layer: MLP activation layer. block_fn: Transformer block layer. """ super().__init__() assert global_pool in ("", "avg", "token", "map") assert class_token or global_pool != "token" use_fc_norm = global_pool == "avg" if fc_norm is None else fc_norm # norm_layer = get_norm_layer(norm_layer) or partial(nn.LayerNorm, eps=1e-6) # act_layer = get_act_layer(act_layer) or nn.GELU norm_layer = partial(nn.LayerNorm, eps=1e-6) act_layer = nn.GELU
self.num_classes = num_classes self.global_pool = global_pool self.num_features = self.embed_dim = ( embed_dim # num_features for consistency with other models ) self.num_prefix_tokens = 1 if class_token else 0 self.num_prefix_tokens += reg_tokens self.num_reg_tokens = reg_tokens self.has_class_token = class_token self.no_embed_class = ( no_embed_class # don't embed prefix positions (includes reg) ) self.dynamic_img_size = dynamic_img_size self.grad_checkpointing = False self.ignore_head = ignore_head
embed_args = {} if dynamic_img_size: # flatten deferred until after pos embed embed_args.update(dict(strict_img_size=False, output_fmt="NHWC")) self.patch_embed = embed_layer( img_size=img_size, patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim, bias=not pre_norm, # disable bias if pre-norm is used (e.g. CLIP) dynamic_img_pad=dynamic_img_pad, **embed_args, ) num_patches = self.patch_embed.num_patches
self.cls_token = ( nn.Parameter(torch.zeros(1, 1, embed_dim)) if class_token else None ) self.reg_token = ( nn.Parameter(torch.zeros(1, reg_tokens, embed_dim)) if reg_tokens else None ) embed_len = ( num_patches if no_embed_class else num_patches + self.num_prefix_tokens ) self.pos_embed = nn.Parameter(torch.randn(1, embed_len, embed_dim) * 0.02) self.pos_drop = nn.Dropout(p=pos_drop_rate) if patch_drop_rate > 0: self.patch_drop = PatchDropout( patch_drop_rate, num_prefix_tokens=self.num_prefix_tokens, ) else: self.patch_drop = nn.Identity() self.norm_pre = norm_layer(embed_dim) if pre_norm else nn.Identity()
dpr = [ x.item() for x in torch.linspace(0, drop_path_rate, depth) ] # stochastic depth decay rule self.blocks = nn.Sequential( *[ block_fn( dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_norm=qk_norm, init_values=init_values, proj_drop=proj_drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[i], norm_layer=norm_layer, act_layer=act_layer, mlp_layer=mlp_layer, ) for i in range(depth) ] ) self.norm = norm_layer(embed_dim) if not use_fc_norm else nn.Identity()
# Classifier Head if global_pool == "map": AttentionPoolLatent.init_weights = init_weights self.attn_pool = AttentionPoolLatent( self.embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, norm_layer=norm_layer, ) else: self.attn_pool = None self.fc_norm = norm_layer(embed_dim) if use_fc_norm else nn.Identity() self.head_drop = nn.Dropout(drop_rate) self.head = ( nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() )
if weight_init != "skip": self.init_weights(weight_init)
def init_weights(self, mode: Literal["jax", "jax_nlhb", "moco", ""] = "") -> None: assert mode in ("jax", "jax_nlhb", "moco", "") # head_bias = -math.log(self.num_classes) if "nlhb" in mode else 0.0 trunc_normal_(self.pos_embed, std=0.02) if self.cls_token is not None: nn.init.normal_(self.cls_token, std=1e-6) named_apply(init_weights_vit_timm, self)
@torch.jit.ignore def no_weight_decay(self) -> Set: return {"pos_embed", "cls_token", "dist_token"}
@torch.jit.ignore def group_matcher(self, coarse: bool = False) -> Dict: return dict( stem=r"^cls_token|pos_embed|patch_embed", # stem and embed blocks=[(r"^blocks\.(\d+)", None), (r"^norm", (99999,))], )
@torch.jit.ignore def set_grad_checkpointing(self, enable: bool = True) -> None: self.grad_checkpointing = enable
@torch.jit.ignore def get_classifier(self) -> nn.Module: return self.head
def reset_classifier(self, num_classes: int, global_pool=None) -> None: self.num_classes = num_classes if global_pool is not None: assert global_pool in ("", "avg", "token", "map") if global_pool == "map" and self.attn_pool is None: assert ( False ), "Cannot currently add attention pooling in reset_classifier()." elif global_pool != "map " and self.attn_pool is not None: self.attn_pool = None # remove attention pooling self.global_pool = global_pool self.head = ( nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() )
def _pos_embed(self, x: torch.Tensor) -> torch.Tensor: if self.dynamic_img_size: B, H, W, C = x.shape pos_embed = resample_abs_pos_embed( self.pos_embed, (H, W), num_prefix_tokens=0 if self.no_embed_class else self.num_prefix_tokens, ) x = x.view(B, -1, C) else: pos_embed = self.pos_embed
to_cat = [] if self.cls_token is not None: to_cat.append(self.cls_token.expand(x.shape[0], -1, -1)) if self.reg_token is not None: to_cat.append(self.reg_token.expand(x.shape[0], -1, -1))
if self.no_embed_class: # deit-3, updated JAX (big vision) # position embedding does not overlap with class token, add then concat x = x + pos_embed if to_cat: x = torch.cat(to_cat + [x], dim=1) else: # original timm, JAX, and deit vit impl # pos_embed has entry for class token, concat then add if to_cat: x = torch.cat(to_cat + [x], dim=1) x = x + pos_embed
return self.pos_drop(x)
def _intermediate_layers( self, x: torch.Tensor, n: Union[int, Sequence] = 1, ) -> List[torch.Tensor]: outputs, num_blocks = [], len(self.blocks) take_indices = set( range(num_blocks - n, num_blocks) if isinstance(n, int) else n )
# forward pass x = self.patch_embed(x) x = self._pos_embed(x) x = self.patch_drop(x) x = self.norm_pre(x) for i, blk in enumerate(self.blocks): x = blk(x) if i in take_indices: outputs.append(x)
return outputs
def get_intermediate_layers( self, x: torch.Tensor, n: Union[int, Sequence] = 1, reshape: bool = False, return_prefix_tokens: bool = False, norm: bool = False, ) -> Tuple[Union[torch.Tensor, Tuple[torch.Tensor]]]: """Intermediate layer accessor (NOTE: This is a WIP experiment). Inspired by DINO / DINOv2 interface """ # take last n blocks if n is an int, if in is a sequence, select by matching indices outputs = self._intermediate_layers(x, n) if norm: outputs = [self.norm(out) for out in outputs] prefix_tokens = [out[:, 0 : self.num_prefix_tokens] for out in outputs] outputs = [out[:, self.num_prefix_tokens :] for out in outputs]
if reshape: grid_size = self.patch_embed.grid_size outputs = [ out.reshape(x.shape[0], grid_size[0], grid_size[1], -1) .permute(0, 3, 1, 2) .contiguous() for out in outputs ]
if return_prefix_tokens: return tuple(zip(outputs, prefix_tokens)) return tuple(outputs)
def forward_features(self, x: torch.Tensor) -> torch.Tensor: x = self.patch_embed(x) x = self._pos_embed(x) x = self.patch_drop(x) x = self.norm_pre(x) if self.grad_checkpointing and not torch.jit.is_scripting(): x = checkpoint_seq(self.blocks, x) else: x = self.blocks(x) x = self.norm(x) return x
def forward_head(self, x: torch.Tensor, pre_logits: bool = False) -> torch.Tensor: if self.attn_pool is not None: x = self.attn_pool(x) elif self.global_pool == "avg": x = x[:, self.num_prefix_tokens :].mean(dim=1) elif self.global_pool: x = x[:, 0] # class token x = self.fc_norm(x) x = self.head_drop(x) return x if pre_logits else self.head(x)
def forward(self, x: torch.Tensor) -> torch.Tensor: x = self.forward_features(x) if not self.ignore_head: x = self.forward_head(x) return x
@dataclassclass SigLIPVisionCfg: width: int = 1152 layers: Union[Tuple[int, int, int, int], int] = 27 heads: int = 16 patch_size: int = 14 image_size: Union[Tuple[int, int], int] = 336 global_pool: str = "map" mlp_ratio: float = 3.7362 class_token: bool = False num_classes: int = 0 use_checkpoint: bool = False
SigLIP_MODEL_CONFIG = { "siglip_so400m_patch14_384": { "image_size": 336, "patch_size": 14, "width": 1152, "layers": 27, "heads": 16, "mlp_ratio": 3.7362, "global_pool": "map", "use_checkpoint": False, }, "siglip_so400m_patch14_224": { "image_size": 224, "patch_size": 14, "width": 1152, "layers": 27, "heads": 16, "mlp_ratio": 3.7362, "global_pool": "map", "use_checkpoint": False, }, "siglip_large_patch16_384": { "image_size": 384, "patch_size": 16, "width": 1024, "layers": 24, "heads": 16, "mlp_ratio": 4, "global_pool": "map", "use_checkpoint": False, },}
def create_siglip_vit( model_name: str = "siglip_so400m_patch14_384", image_size: int = 384, select_layer: int = -1, ckpt_path: str = "", **kwargs,): assert ( model_name in SigLIP_MODEL_CONFIG.keys() ), f"model name should be in {SigLIP_MODEL_CONFIG.keys()}"
vision_cfg = SigLIPVisionCfg(**SigLIP_MODEL_CONFIG[model_name])
if select_layer <= 0: layers = min(vision_cfg.layers, vision_cfg.layers + select_layer + 1) else: layers = min(vision_cfg.layers, select_layer)
model = VisionTransformer( img_size=image_size, patch_size=vision_cfg.patch_size, embed_dim=vision_cfg.width, depth=layers, num_heads=vision_cfg.heads, mlp_ratio=vision_cfg.mlp_ratio, class_token=vision_cfg.class_token, global_pool=vision_cfg.global_pool, ignore_head=kwargs.get("ignore_head", True), weight_init=kwargs.get("weight_init", "skip"), num_classes=0, )
if ckpt_path: state_dict = torch.load(ckpt_path, map_location="cpu")
incompatible_keys = model.load_state_dict(state_dict, strict=False) print( f"SigLIP-ViT restores from {ckpt_path},\n" f"\tincompatible_keys:', {incompatible_keys}." )
return model123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527# Copyright (c) 2023-2024 DeepSeek.## Permission is hereby granted, free of charge, to any person obtaining a copy of# this software and associated documentation files (the "Software"), to deal in# the Software without restriction, including without limitation the rights to# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of# the Software, and to permit persons to whom the Software is furnished to do so,# subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from dataclasses import dataclass, fieldfrom typing import List
import torchimport torch.nn as nnimport torch.nn.functional as F
from functools import partial
@dataclassclass ModelArgs: codebook_size: int = 16384 codebook_embed_dim: int = 8 codebook_l2_norm: bool = True codebook_show_usage: bool = True commit_loss_beta: float = 0.25 entropy_loss_ratio: float = 0.0
encoder_ch_mult: List[int] = field(default_factory=lambda: [1, 1, 2, 2, 4]) decoder_ch_mult: List[int] = field(default_factory=lambda: [1, 1, 2, 2, 4]) z_channels: int = 256 dropout_p: float = 0.0
class Encoder(nn.Module): def __init__( self, in_channels=3, ch=128, ch_mult=(1, 1, 2, 2, 4), num_res_blocks=2, norm_type="group", dropout=0.0, resamp_with_conv=True, z_channels=256, ): super().__init__() self.num_resolutions = len(ch_mult) self.num_res_blocks = num_res_blocks self.conv_in = nn.Conv2d(in_channels, ch, kernel_size=3, stride=1, padding=1)
# downsampling in_ch_mult = (1,) + tuple(ch_mult) self.conv_blocks = nn.ModuleList() for i_level in range(self.num_resolutions): conv_block = nn.Module() # res & attn res_block = nn.ModuleList() attn_block = nn.ModuleList() block_in = ch * in_ch_mult[i_level] block_out = ch * ch_mult[i_level] for _ in range(self.num_res_blocks): res_block.append( ResnetBlock( block_in, block_out, dropout=dropout, norm_type=norm_type ) ) block_in = block_out if i_level == self.num_resolutions - 1: attn_block.append(AttnBlock(block_in, norm_type)) conv_block.res = res_block conv_block.attn = attn_block # downsample if i_level != self.num_resolutions - 1: conv_block.downsample = Downsample(block_in, resamp_with_conv) self.conv_blocks.append(conv_block)
# middle self.mid = nn.ModuleList() self.mid.append( ResnetBlock(block_in, block_in, dropout=dropout, norm_type=norm_type) ) self.mid.append(AttnBlock(block_in, norm_type=norm_type)) self.mid.append( ResnetBlock(block_in, block_in, dropout=dropout, norm_type=norm_type) )
# end self.norm_out = Normalize(block_in, norm_type) self.conv_out = nn.Conv2d( block_in, z_channels, kernel_size=3, stride=1, padding=1 )
def forward(self, x): h = self.conv_in(x) # downsampling for i_level, block in enumerate(self.conv_blocks): for i_block in range(self.num_res_blocks): h = block.res[i_block](h) if len(block.attn) > 0: h = block.attn[i_block](h) if i_level != self.num_resolutions - 1: h = block.downsample(h)
# middle for mid_block in self.mid: h = mid_block(h)
# end h = self.norm_out(h) h = nonlinearity(h) h = self.conv_out(h) return h
class Decoder(nn.Module): def __init__( self, z_channels=256, ch=128, ch_mult=(1, 1, 2, 2, 4), num_res_blocks=2, norm_type="group", dropout=0.0, resamp_with_conv=True, out_channels=3, ): super().__init__() self.num_resolutions = len(ch_mult) self.num_res_blocks = num_res_blocks
block_in = ch * ch_mult[self.num_resolutions - 1] # z to block_in self.conv_in = nn.Conv2d( z_channels, block_in, kernel_size=3, stride=1, padding=1 )
# middle self.mid = nn.ModuleList() self.mid.append( ResnetBlock(block_in, block_in, dropout=dropout, norm_type=norm_type) ) self.mid.append(AttnBlock(block_in, norm_type=norm_type)) self.mid.append( ResnetBlock(block_in, block_in, dropout=dropout, norm_type=norm_type) )
# upsampling self.conv_blocks = nn.ModuleList() for i_level in reversed(range(self.num_resolutions)): conv_block = nn.Module() # res & attn res_block = nn.ModuleList() attn_block = nn.ModuleList() block_out = ch * ch_mult[i_level] for _ in range(self.num_res_blocks + 1): res_block.append( ResnetBlock( block_in, block_out, dropout=dropout, norm_type=norm_type ) ) block_in = block_out if i_level == self.num_resolutions - 1: attn_block.append(AttnBlock(block_in, norm_type)) conv_block.res = res_block conv_block.attn = attn_block # downsample if i_level != 0: conv_block.upsample = Upsample(block_in, resamp_with_conv) self.conv_blocks.append(conv_block)
# end self.norm_out = Normalize(block_in, norm_type) self.conv_out = nn.Conv2d( block_in, out_channels, kernel_size=3, stride=1, padding=1 )
@property def last_layer(self): return self.conv_out.weight
def forward(self, z): # z to block_in h = self.conv_in(z)
# middle for mid_block in self.mid: h = mid_block(h)
# upsampling for i_level, block in enumerate(self.conv_blocks): for i_block in range(self.num_res_blocks + 1): h = block.res[i_block](h) if len(block.attn) > 0: h = block.attn[i_block](h) if i_level != self.num_resolutions - 1: h = block.upsample(h)
# end h = self.norm_out(h) h = nonlinearity(h) h = self.conv_out(h) return h
class VectorQuantizer(nn.Module): def __init__(self, n_e, e_dim, beta, entropy_loss_ratio, l2_norm, show_usage): super().__init__() self.n_e = n_e self.e_dim = e_dim self.beta = beta self.entropy_loss_ratio = entropy_loss_ratio self.l2_norm = l2_norm self.show_usage = show_usage
self.embedding = nn.Embedding(self.n_e, self.e_dim) self.embedding.weight.data.uniform_(-1.0 / self.n_e, 1.0 / self.n_e) if self.l2_norm: self.embedding.weight.data = F.normalize( self.embedding.weight.data, p=2, dim=-1 ) if self.show_usage: self.register_buffer("codebook_used", nn.Parameter(torch.zeros(65536)))
def forward(self, z): # reshape z -> (batch, height, width, channel) and flatten z = torch.einsum("b c h w -> b h w c", z).contiguous() z_flattened = z.view(-1, self.e_dim) # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z
if self.l2_norm: z = F.normalize(z, p=2, dim=-1) z_flattened = F.normalize(z_flattened, p=2, dim=-1) embedding = F.normalize(self.embedding.weight, p=2, dim=-1) else: embedding = self.embedding.weight
d = ( torch.sum(z_flattened**2, dim=1, keepdim=True) + torch.sum(embedding**2, dim=1) - 2 * torch.einsum( "bd,dn->bn", z_flattened, torch.einsum("n d -> d n", embedding) ) )
min_encoding_indices = torch.argmin(d, dim=1) z_q = embedding[min_encoding_indices].view(z.shape) perplexity = None min_encodings = None vq_loss = None commit_loss = None entropy_loss = None
# compute loss for embedding if self.training: vq_loss = torch.mean((z_q - z.detach()) ** 2) commit_loss = self.beta * torch.mean((z_q.detach() - z) ** 2) entropy_loss = self.entropy_loss_ratio * compute_entropy_loss(-d)
# preserve gradients z_q = z + (z_q - z).detach()
# reshape back to match original input shape z_q = torch.einsum("b h w c -> b c h w", z_q)
return ( z_q, (vq_loss, commit_loss, entropy_loss), (perplexity, min_encodings, min_encoding_indices), )
def get_codebook_entry(self, indices, shape=None, channel_first=True): # shape = (batch, channel, height, width) if channel_first else (batch, height, width, channel) if self.l2_norm: embedding = F.normalize(self.embedding.weight, p=2, dim=-1) else: embedding = self.embedding.weight z_q = embedding[indices] # (b*h*w, c)
if shape is not None: if channel_first: z_q = z_q.reshape(shape[0], shape[2], shape[3], shape[1]) # reshape back to match original input shape z_q = z_q.permute(0, 3, 1, 2).contiguous() else: z_q = z_q.view(shape) return z_q
class ResnetBlock(nn.Module): def __init__( self, in_channels, out_channels=None, conv_shortcut=False, dropout=0.0, norm_type="group", ): super().__init__() self.in_channels = in_channels out_channels = in_channels if out_channels is None else out_channels self.out_channels = out_channels self.use_conv_shortcut = conv_shortcut
self.norm1 = Normalize(in_channels, norm_type) self.conv1 = nn.Conv2d( in_channels, out_channels, kernel_size=3, stride=1, padding=1 ) self.norm2 = Normalize(out_channels, norm_type) self.dropout = nn.Dropout(dropout) self.conv2 = nn.Conv2d( out_channels, out_channels, kernel_size=3, stride=1, padding=1 )
if self.in_channels != self.out_channels: if self.use_conv_shortcut: self.conv_shortcut = nn.Conv2d( in_channels, out_channels, kernel_size=3, stride=1, padding=1 ) else: self.nin_shortcut = nn.Conv2d( in_channels, out_channels, kernel_size=1, stride=1, padding=0 )
def forward(self, x): h = x h = self.norm1(h) h = nonlinearity(h) h = self.conv1(h) h = self.norm2(h) h = nonlinearity(h) h = self.dropout(h) h = self.conv2(h)
if self.in_channels != self.out_channels: if self.use_conv_shortcut: x = self.conv_shortcut(x) else: x = self.nin_shortcut(x) return x + h
class AttnBlock(nn.Module): def __init__(self, in_channels, norm_type="group"): super().__init__() self.norm = Normalize(in_channels, norm_type) self.q = nn.Conv2d(in_channels, in_channels, kernel_size=1, stride=1, padding=0) self.k = nn.Conv2d(in_channels, in_channels, kernel_size=1, stride=1, padding=0) self.v = nn.Conv2d(in_channels, in_channels, kernel_size=1, stride=1, padding=0) self.proj_out = nn.Conv2d( in_channels, in_channels, kernel_size=1, stride=1, padding=0 )
def forward(self, x): h_ = x h_ = self.norm(h_) q = self.q(h_) k = self.k(h_) v = self.v(h_)
# compute attention b, c, h, w = q.shape q = q.reshape(b, c, h * w) q = q.permute(0, 2, 1) # b,hw,c k = k.reshape(b, c, h * w) # b,c,hw w_ = torch.bmm(q, k) # b,hw,hw w[b,i,j]=sum_c q[b,i,c]k[b,c,j] w_ = w_ * (int(c) ** (-0.5)) w_ = F.softmax(w_, dim=2)
# attend to values v = v.reshape(b, c, h * w) w_ = w_.permute(0, 2, 1) # b,hw,hw (first hw of k, second of q) h_ = torch.bmm(v, w_) # b, c,hw (hw of q) h_[b,c,j] = sum_i v[b,c,i] w_[b,i,j] h_ = h_.reshape(b, c, h, w)
h_ = self.proj_out(h_)
return x + h_
def nonlinearity(x): # swish return x * torch.sigmoid(x)
def Normalize(in_channels, norm_type="group"): assert norm_type in ["group", "batch"] if norm_type == "group": return nn.GroupNorm( num_groups=32, num_channels=in_channels, eps=1e-6, affine=True ) elif norm_type == "batch": return nn.SyncBatchNorm(in_channels)
class Upsample(nn.Module): def __init__(self, in_channels, with_conv): super().__init__() self.with_conv = with_conv if self.with_conv: self.conv = nn.Conv2d( in_channels, in_channels, kernel_size=3, stride=1, padding=1 )
def forward(self, x): if x.dtype != torch.float32: x = F.interpolate(x.to(torch.float), scale_factor=2.0, mode="nearest").to( torch.bfloat16 ) else: x = F.interpolate(x, scale_factor=2.0, mode="nearest")
if self.with_conv: x = self.conv(x) return x
class Downsample(nn.Module): def __init__(self, in_channels, with_conv): super().__init__() self.with_conv = with_conv if self.with_conv: # no asymmetric padding in torch conv, must do it ourselves self.conv = nn.Conv2d( in_channels, in_channels, kernel_size=3, stride=2, padding=0 )
def forward(self, x): if self.with_conv: pad = (0, 1, 0, 1) x = F.pad(x, pad, mode="constant", value=0) x = self.conv(x) else: x = F.avg_pool2d(x, kernel_size=2, stride=2) return x
def compute_entropy_loss(affinity, loss_type="softmax", temperature=0.01): flat_affinity = affinity.reshape(-1, affinity.shape[-1]) flat_affinity /= temperature probs = F.softmax(flat_affinity, dim=-1) log_probs = F.log_softmax(flat_affinity + 1e-5, dim=-1) if loss_type == "softmax": target_probs = probs else: raise ValueError("Entropy loss {} not supported".format(loss_type)) avg_probs = torch.mean(target_probs, dim=0) avg_entropy = -torch.sum(avg_probs * torch.log(avg_probs + 1e-5)) sample_entropy = -torch.mean(torch.sum(target_probs * log_probs, dim=-1)) loss = sample_entropy - avg_entropy return loss
class VQModel(nn.Module): def __init__(self, config: ModelArgs): super().__init__() self.config = config self.encoder = Encoder( ch_mult=config.encoder_ch_mult, z_channels=config.z_channels, dropout=config.dropout_p, ) self.decoder = Decoder( ch_mult=config.decoder_ch_mult, z_channels=config.z_channels, dropout=config.dropout_p, )
self.quantize = VectorQuantizer( config.codebook_size, config.codebook_embed_dim, config.commit_loss_beta, config.entropy_loss_ratio, config.codebook_l2_norm, config.codebook_show_usage, ) self.quant_conv = nn.Conv2d(config.z_channels, config.codebook_embed_dim, 1) self.post_quant_conv = nn.Conv2d( config.codebook_embed_dim, config.z_channels, 1 )
def encode(self, x): h = self.encoder(x) h = self.quant_conv(h) quant, emb_loss, info = self.quantize(h) return quant, emb_loss, info
def decode(self, quant): quant = self.post_quant_conv(quant) dec = self.decoder(quant) return dec
def decode_code(self, code_b, shape=None, channel_first=True): quant_b = self.quantize.get_codebook_entry(code_b, shape, channel_first) dec = self.decode(quant_b) return dec
def forward(self, input): quant, diff, _ = self.encode(input) dec = self.decode(quant) return dec, diff
################################################################################## VQ Model Configs ##################################################################################def VQ_16(**kwargs): return VQModel( ModelArgs( encoder_ch_mult=[1, 1, 2, 2, 4], decoder_ch_mult=[1, 1, 2, 2, 4], **kwargs ) )
VQ_models = {"VQ-16": VQ_16}123456789101112131415161718# Copyright (c) 2023-2024 DeepSeek.## Permission is hereby granted, free of charge, to any person obtaining a copy of# this software and associated documentation files (the "Software"), to deal in# the Software without restriction, including without limitation the rights to# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of# the Software, and to permit persons to whom the Software is furnished to do so,# subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365# Copyright (c) 2023-2024 DeepSeek.## Permission is hereby granted, free of charge, to any person obtaining a copy of# this software and associated documentation files (the "Software"), to deal in# the Software without restriction, including without limitation the rights to# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of# the Software, and to permit persons to whom the Software is furnished to do so,# subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""From https://github.com/lm-sys/FastChat/blob/main/fastchat/conversation.py"""
import dataclassesfrom enum import IntEnum, autofrom typing import Dict, List
class SeparatorStyle(IntEnum): """Separator styles."""
ADD_COLON_SINGLE = auto() ADD_COLON_TWO = auto() ADD_COLON_SPACE_SINGLE = auto() NO_COLON_SINGLE = auto() NO_COLON_TWO = auto() ADD_NEW_LINE_SINGLE = auto() LLAMA2 = auto() CHATGLM = auto() CHATML = auto() CHATINTERN = auto() DOLLY = auto() RWKV = auto() PHOENIX = auto() ROBIN = auto() DeepSeek = auto() PLAIN = auto() ALIGNMENT = auto()
@dataclasses.dataclassclass Conversation: """A class that manages prompt templates and keeps all conversation history."""
# The name of this template name: str # The template of the system prompt system_template: str = "{system_message}" # The system message system_message: str = "" # The names of two roles roles: List[str] = (("USER", "ASSISTANT"),) # All messages. Each item is (role, message). messages: List[List[str]] = () # The number of few shot examples offset: int = 0 # The separator style and configurations sep_style: SeparatorStyle = SeparatorStyle.ADD_COLON_SINGLE sep: str = "\n" sep2: str = None # Stop criteria (the default one is EOS token) stop_str: str = None # Stops generation if meeting any token in this list stop_token_ids: List[int] = None
def get_prompt(self) -> str: """Get the prompt for generation.""" system_prompt = self.system_template.format(system_message=self.system_message)
if self.sep_style == SeparatorStyle.DeepSeek: seps = [self.sep, self.sep2] if system_prompt == "" or system_prompt is None: ret = "" else: ret = system_prompt + seps[0] for i, (role, message) in enumerate(self.messages): if message: ret += role + ": " + message + seps[i % 2] else: ret += role + ":" return ret elif self.sep_style == SeparatorStyle.LLAMA2: seps = [self.sep, self.sep2] if self.system_message: ret = system_prompt else: ret = "[INST] " for i, (role, message) in enumerate(self.messages): tag = self.roles[i % 2] if message: if type(message) is tuple: # multimodal message message, _ = message if i == 0: ret += message + " " else: ret += tag + " " + message + seps[i % 2] else: ret += tag return ret elif self.sep_style == SeparatorStyle.PLAIN: seps = [self.sep, self.sep2] ret = "" for i, (role, message) in enumerate(self.messages): if message: if type(message) is tuple: message, _, _ = message if i % 2 == 0: ret += message + seps[i % 2] else: ret += message + seps[i % 2] else: ret += "" return ret elif self.sep_style == SeparatorStyle.ALIGNMENT: seps = [self.sep, self.sep2] ret = "" for i, (role, message) in enumerate(self.messages): if message: if type(message) is tuple: message, _, _ = message if i % 2 == 0: ret += "<image>\n" + seps[i % 2] else: ret += message + seps[i % 2] else: ret += "" return ret else: raise ValueError(f"Invalid style: {self.sep_style}")
def get_prompt_for_current_round(self, content=None): """Get current round formatted question prompt during sft training""" if self.sep_style == SeparatorStyle.PLAIN: formatted_question = "<image>\n" elif self.sep_style == SeparatorStyle.DeepSeek: formatted_question = ( f"{self.roles[0]}: " + content.strip() + self.sep + f"{self.roles[1]}:" ) else: raise ValueError(f"Unsupported sep_style: {self.sep_style}") return formatted_question
def set_system_message(self, system_message: str): """Set the system message.""" self.system_message = system_message
def append_message(self, role: str, message: str): """Append a new message.""" self.messages.append([role, message])
def reset_message(self): """Reset a new message.""" self.messages = []
def update_last_message(self, message: str): """Update the last output.
The last message is typically set to be None when constructing the prompt, so we need to update it in-place after getting the response from a model. """ self.messages[-1][1] = message
def to_gradio_chatbot(self): """Convert the conversation to gradio chatbot format.""" ret = [] for i, (role, msg) in enumerate(self.messages[self.offset :]): if i % 2 == 0: ret.append([msg, None]) else: ret[-1][-1] = msg return ret
def to_openai_api_messages(self): """Convert the conversation to OpenAI chat completion format.""" system_prompt = self.system_template.format(system_message=self.system_message) ret = [{"role": "system", "content": system_prompt}]
for i, (_, msg) in enumerate(self.messages[self.offset :]): if i % 2 == 0: ret.append({"role": "user", "content": msg}) else: if msg is not None: ret.append({"role": "assistant", "content": msg}) return ret
def copy(self): return Conversation( name=self.name, system_template=self.system_template, system_message=self.system_message, roles=self.roles, messages=[[x, y] for x, y in self.messages], offset=self.offset, sep_style=self.sep_style, sep=self.sep, sep2=self.sep2, stop_str=self.stop_str, stop_token_ids=self.stop_token_ids, )
def dict(self): return { "template_name": self.name, "system_message": self.system_message, "roles": self.roles, "messages": self.messages, "offset": self.offset, }
# A global registry for all conversation templatesconv_templates: Dict[str, Conversation] = {}
def register_conv_template(template: Conversation, override: bool = False): """Register a new conversation template.""" if not override: assert ( template.name not in conv_templates ), f"{template.name} has been registered."
conv_templates[template.name] = template
def get_conv_template(name: str) -> Conversation: """Get a conversation template.""" return conv_templates[name].copy()
# llava_llama2 templateregister_conv_template( Conversation( name="llava_llama2", system_message="You are a helpful language and vision assistant. " "You are able to understand the visual content that the user provides, " "and assist the user with a variety of tasks using natural language.", system_template="[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n", roles=("[INST]", "[/INST]"), messages=(), offset=0, sep_style=SeparatorStyle.LLAMA2, sep=" ", sep2=" </s><s>", stop_token_ids=[2], ))
# llama2 template# reference: https://github.com/facebookresearch/llama/blob/cfc3fc8c1968d390eb830e65c63865e980873a06/llama/generation.py#L212register_conv_template( Conversation( name="llama-2", system_template="[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n", roles=("[INST]", "[/INST]"), messages=(), offset=0, sep_style=SeparatorStyle.LLAMA2, sep=" ", sep2=" </s><s>", stop_token_ids=[2], ))
# deepseek templateregister_conv_template( Conversation( name="deepseek_old", system_template="{system_message}", # system_message="You are a helpful assistant. Please answer truthfully and write out your " # "thinking step by step to be sure you get the right answer.", system_message="", roles=("User", "Assistant"), messages=(), offset=0, sep_style=SeparatorStyle.DeepSeek, sep="\n\n", sep2="<|end▁of▁sentence|>", stop_token_ids=[100001], stop_str=["User:", "<|end▁of▁sentence|>"], ))register_conv_template( Conversation( name="deepseek", system_template="{system_message}", # system_message="You are a helpful assistant. Please answer truthfully and write out your " # "thinking step by step to be sure you get the right answer.", system_message="", roles=("<|User|>", "<|Assistant|>"), messages=(), offset=0, sep_style=SeparatorStyle.DeepSeek, sep="\n\n", sep2="<|end▁of▁sentence|>", stop_token_ids=[100001], stop_str=["<|User|>", "<|end▁of▁sentence|>"] ))
register_conv_template( Conversation( name="plain", system_template="", system_message="", roles=("", ""), messages=(), offset=0, sep_style=SeparatorStyle.PLAIN, sep="", sep2="", stop_token_ids=[2], stop_str=["</s>"], ))
register_conv_template( Conversation( name="alignment", system_template="", system_message="", roles=("", ""), messages=(), offset=0, sep_style=SeparatorStyle.ALIGNMENT, sep="", sep2="", stop_token_ids=[2], stop_str=["</s>"], ))
if __name__ == "__main__": # print("Llama-2 template:") # conv = get_conv_template("llama-2") # conv.set_system_message("You are a helpful, respectful and honest assistant.") # conv.append_message(conv.roles[0], "Hello!") # conv.append_message(conv.roles[1], "Hi!") # conv.append_message(conv.roles[0], "How are you?") # conv.append_message(conv.roles[1], None) # print(conv.get_prompt())
# print("\n")
print("deepseek template:") conv = get_conv_template("deepseek") conv.append_message(conv.roles[0], "Hello!") conv.append_message(conv.roles[1], "Hi! This is Tony.") conv.append_message(conv.roles[0], "Who are you?") conv.append_message(conv.roles[1], "I am a helpful assistant.") conv.append_message(conv.roles[0], "How are you?") conv.append_message(conv.roles[1], None) print(conv.get_prompt())1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889# Copyright (c) 2023-2024 DeepSeek.## Permission is hereby granted, free of charge, to any person obtaining a copy of# this software and associated documentation files (the "Software"), to deal in# the Software without restriction, including without limitation the rights to# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of# the Software, and to permit persons to whom the Software is furnished to do so,# subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import jsonfrom typing import Dict, List
import PIL.Imageimport torchimport base64import iofrom transformers import AutoModelForCausalLM
from janus.models import MultiModalityCausalLM, VLChatProcessor
def load_pretrained_model(model_path: str): vl_chat_processor: VLChatProcessor = VLChatProcessor.from_pretrained(model_path) tokenizer = vl_chat_processor.tokenizer
vl_gpt: MultiModalityCausalLM = AutoModelForCausalLM.from_pretrained( model_path, trust_remote_code=True ) vl_gpt = vl_gpt.to(torch.bfloat16).cuda().eval()
return tokenizer, vl_chat_processor, vl_gpt
def load_pil_images(conversations: List[Dict[str, str]]) -> List[PIL.Image.Image]: """
Support file path or base64 images.
Args: conversations (List[Dict[str, str]]): the conversations with a list of messages. An example is : [ { "role": "User", "content": "<image_placeholder>\nExtract all information from this image and convert them into markdown format.", "images": ["./examples/table_datasets.png"] }, {"role": "Assistant", "content": ""}, ]
Returns: pil_images (List[PIL.Image.Image]): the list of PIL images.
"""
pil_images = []
for message in conversations: if "images" not in message: continue
for image_data in message["images"]: if image_data.startswith("data:image"): # Image data is in base64 format _, image_data = image_data.split(",", 1) image_bytes = base64.b64decode(image_data) pil_img = PIL.Image.open(io.BytesIO(image_bytes)) else: # Image data is a file path pil_img = PIL.Image.open(image_data) pil_img = pil_img.convert("RGB") pil_images.append(pil_img)
return pil_images
def load_json(filepath): with open(filepath, "r") as f: data = json.load(f) return data12345678acceleratediffusersgradionumpytorchsafetensorstransformersgit+https://github.com/deepseek-ai/Janus123version https://git-lfs.github.com/spec/v1oid sha256:c830d067d54fc767b9543a8432f36d91bc2de313584e8bbfe4ac26a47339e899size 67061725README.mdDeleted-13
Upsample/__init__.pyAdded+1
Upsample/__pycache__/__init__.cpython-38.pycAdded
Upsample/__pycache__/arch_utils.cpython-38.pycAdded
Upsample/__pycache__/model.cpython-38.pycAdded
Upsample/__pycache__/rrdbnet_arch.cpython-38.pycAdded
Upsample/__pycache__/utils.cpython-38.pycAdded
Upsample/arch_utils.pyAdded+197
Upsample/model.pyAdded+93
Upsample/rrdbnet_arch.pyAdded+121
Upsample/utils.pyAdded+135
app.pyAdded+268
doge.pngAdded

equation.pngAdded

janus/__init__.pyAdded+31
janus/__pycache__/__init__.cpython-38.pycAdded
janus/models/__init__.pyAdded+28
janus/models/__pycache__/__init__.cpython-38.pycAdded
janus/models/__pycache__/clip_encoder.cpython-38.pycAdded
janus/models/__pycache__/image_processing_vlm.cpython-38.pycAdded
janus/models/__pycache__/modeling_vlm.cpython-38.pycAdded
janus/models/__pycache__/processing_vlm.cpython-38.pycAdded
janus/models/__pycache__/projector.cpython-38.pycAdded
janus/models/__pycache__/siglip_vit.cpython-38.pycAdded
janus/models/__pycache__/vq_model.cpython-38.pycAdded
janus/models/clip_encoder.pyAdded+122
janus/models/image_processing_vlm.pyAdded+208
janus/models/modeling_vlm.pyAdded+272
janus/models/processing_vlm.pyAdded+418
janus/models/projector.pyAdded+100
janus/models/siglip_vit.pyAdded+681
janus/models/vq_model.pyAdded+527
janus/utils/__init__.pyAdded+18
janus/utils/__pycache__/__init__.cpython-38.pycAdded
janus/utils/__pycache__/conversation.cpython-38.pycAdded
janus/utils/__pycache__/io.cpython-38.pycAdded
janus/utils/conversation.pyAdded+365
janus/utils/io.pyAdded+89
requirements.txtAdded+8
weights/RealESRGAN_x2.pthAdded+3