commit 0a847cba9d6a906f16866cef55abc3d08591d384
Author: vik <vikhyatk@gmail.com>
Date: Wed Jan 31 12:31:10 2024 -0800
bugfix
commit de750a63ec3546c6f2dccde2d90902f314ac1cf8
Author: vik <vikhyatk@gmail.com>
Date: Tue Jan 30 18:33:40 2024 -0800
clean up inference interface a bit
commit 2ee0cdad0c6c7e981bfe21fb5160319fe328bbd1
Author: vik <vikhyatk@gmail.com>
Date: Tue Jan 30 17:20:12 2024 -0800
load tokenizer from HF
commit 4c981021959e440b3d3d33a6cb4c7324349587e4
Author: vik <vikhyatk@gmail.com>
Date: Tue Jan 30 17:07:57 2024 -0800
bugfix
commit d5fa2f95fe1799e5191defa339e06bdfcc079bb8
Author: vik <vikhyatk@gmail.com>
Date: Tue Jan 30 15:45:51 2024 -0800
stop using torch.jit.script for the vision encoder
the interface is a little awkward right now, will be fixed shortly
commit cc252f3f5fc54a4d1ea4a41c1849bd458e5eb515
Merge: 0ce7485 d310e37
Author: vik <vikhyatk@gmail.com>
Date: Tue Jan 30 13:33:47 2024 -0800
Merge pull request #34 from eltociear/patch-1
Update README.md
commit 0ce7485481f664ed90e90d0e257856e61cfcdc44
Merge: 3f4815b 2329525
Author: vik <vikhyatk@gmail.com>
Date: Tue Jan 30 13:32:49 2024 -0800
Merge branch 'mazzzystar-main'
commit 232952569a3b2c46d19d0a3bee25032982f8c68d
Author: vik <vikhyatk@gmail.com>
Date: Tue Jan 30 13:32:36 2024 -0800
add missing newline
commit d310e3739743fee903dd1afd52c572c2b9a23370
Author: Ikko Eltociear Ashimine <eltociear@gmail.com>
Date: Wed Jan 31 00:13:48 2024 +0900
Update README.md
Huggingface -> Hugging Face
commit 53b57032078233856fbca8e19c4e4bd5b7c69586
Merge: aad2073 3f4815b
Author: Ke Fang <myfancoo@qq.com>
Date: Tue Jan 30 17:19:20 2024 +0800
Merge branch 'main' into main
commit aad20735513fceac5437a94d7308b55b95dcb216
Author: mazzzystar <2680461921@qq.com>
Date: Tue Jan 30 17:16:29 2024 +0800
change from Thread -> Queue to get the full response.
commit 6d497b787af56d56a3c307b1745936bcdc7ea9e9
Author: mazzzystar <2680461921@qq.com>
Date: Tue Jan 30 17:11:18 2024 +0800
resolve conflict for stream response.
commit e51173058648ad46875275aed8d6f8a7b595294d
Author: mazzzystar <2680461921@qq.com>
Date: Tue Jan 30 17:02:53 2024 +0800
resolve conflict for stream response.
commit 3f4815bd86aabb18724d74ef024adeff6c53914e
Author: vik <vikhyatk@gmail.com>
Date: Tue Jan 30 00:50:30 2024 -0800
bring back interactive mode text streaming
commit 948fa7ff49d990fa477b70c5768ad6a793edaf38
Author: mazzzystar <2680461921@qq.com>
Date: Tue Jan 30 16:49:07 2024 +0800
support for multi-round chat.
commit 4a2fb46a17ca333a5f53e3787e01bd727058395f
Author: vik <vikhyatk@gmail.com>
Date: Mon Jan 29 21:50:18 2024 -0800
simplify resize/cropping in the vision encoder
commit c206a5d4fd1afe0129f2ff6c0aa03bdbd372fddd
Author: vik <vikhyatk@gmail.com>
Date: Mon Jan 29 21:45:03 2024 -0800
detect and use appropriate device/dtype
commit 38af98596e59f2a6c25c6b52b2bd5a672dab4144
Merge: 15d5dd7 70c5358
Author: vik <vikhyatk@gmail.com>
Date: Mon Jan 29 03:43:58 2024 -0800
Merge pull request #31 from markusheimerl/main
Refactored to reduce code size
commit 70c5358887477991d91241fd66b597a3afc44a33
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 09:40:45 2024 +0000
Refactor code to improve readability and maintainability
commit 12f61ce90786c8d5664cbbd0d8b9701033b241ce
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 09:39:59 2024 +0000
Refactor gradio_demo.py and vision_encoder.py
commit 11619b7ca7c920fda02625f53a78c0a13c31c945
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 09:34:17 2024 +0000
Refactor PhiForCausalLM class in modeling_phi.py
commit 2bf4eb998166a326b6a244f7a92f1a853a0e7817
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 09:25:19 2024 +0000
Refactor PhiForCausalLM class in modeling_phi.py
commit 78f2edd4dd7ede9004b17829b834dc4634de7f59
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 09:23:59 2024 +0000
Remove unused imports and variables
commit e6252a05dfad4eb265089147cce6a8cc9404e0bc
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 09:22:49 2024 +0000
Refactor PhiModel forward method
commit 83362add27945409abdd1e1e37dfe30ff674202c
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 09:22:10 2024 +0000
Refactor return statement in modeling_phi.py
commit aea6dcc4e19a7ccc4d793e410377d712e11e46e6
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 09:18:51 2024 +0000
Refactor PhiPreTrainedModel's input handling
commit f08caef1b82c8c6805d1500fdf55c4b7690113d8
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 09:16:06 2024 +0000
Add MHA and ParallelBlock classes
commit 6be42cd2682038e1497ed7c8da1fa22049ec6b33
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 09:14:02 2024 +0000
Simplified CausalLMHead implementation
commit 863f13265e12ff50f361aea874e26083bb8afc7f
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 09:13:00 2024 +0000
Refactor ParallelBlock class in modeling_phi.py
commit 2d3d197546240e14a9454a1765d10a46b6ad1472
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 09:01:25 2024 +0000
Refactor ParallelBlock class in modeling_phi.py
commit 77e19808f4e0b1f9d2e8fb072f7c9257ad351206
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:58:30 2024 +0000
Refactor inner_cross_attn function call in MHA class
commit f7e4b670fbf2decc26a7f1d9241e1beeaa45f7ff
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:57:40 2024 +0000
Refactor MHA forward method
commit d3ed301d3a292d6f9bed26af0babcb4a0c296abd
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:55:53 2024 +0000
Update MHA class with rotary embeddings
commit 0afdd4054a1d691b41c4c0e52addf332af4810c3
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:54:17 2024 +0000
Refactor self-attention forward pass in MHA class
commit 1a8dde4b28eeae5c9dd62a00f1402a8d8bf399ff
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:53:19 2024 +0000
Refactor MHA initialization and simplify RotaryEmbedding usage
commit 7ce1f8c45a021361e87e36cb1c1c9e28e3714439
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:48:16 2024 +0000
Refactor _update_kv_cache function to improve memory usage
commit 70bd853cb7df4b49adc161a60d1ff7749601dbd7
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:47:30 2024 +0000
Refactor _find_mha_dims function signature
commit 3b9598ed5a6a1849c2b2b7b413ff03ec64809237
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:42:05 2024 +0000
Refactor forward method in CrossAttention class
commit 0607a683e8bc00812f72c37858fb1622d7310556
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:37:54 2024 +0000
Add Flash Attention module
commit 30fc465d1a056510167c3be05b3f7455e2f6e9e3
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:32:16 2024 +0000
Add Flash Attention module
commit 66427ecb22d3f2d2cab0ce7dc3ffeeb919fd99ca
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:29:05 2024 +0000
Refactor SelfAttention class in modeling_phi.py
commit 107f9e317fcbd8f3313fa5e00f01241f9e308339
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:26:29 2024 +0000
Refactor MLP module in modeling_phi.py
commit bcdfaa6f9821fe5b8ddd28bad792029e8baab709
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:23:05 2024 +0000
Refactor forward method in RotaryEmbedding class
commit 3f0b043d55a157217fd4a9dd86284c79f7602ef0
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:16:58 2024 +0000
Refactor RotaryEmbedding class to improve precision and performance
commit fc71d3338c3188301f087a38fd9e14319cc74f15
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:13:58 2024 +0000
Update import statements and remove unused code
commit 1be11477b5c9ad1fbd01f023737ad3801acf756e
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:12:09 2024 +0000
Refactor RotaryEmbedding class to use non-trainable buffers
commit 89dde7df1a9237d0838d1577fa0cba3d5172bd20
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:05:15 2024 +0000
Refactor rotary embedding functions in modeling_phi.py
commit 085ee6b292bc21221cda4b36615d5fcfebb3e7df
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 08:02:29 2024 +0000
Refactor rotary embedding functions
commit 025c8d454e15355ef7e1137c69d1c28305a6be63
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 07:59:09 2024 +0000
Refactor rotary embedding calculation in modeling_phi.py
commit 873b39219c1cbb2906e17fc4e92ca8da66bf800e
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 07:58:12 2024 +0000
Refactor _apply_rotary_emb_kv function to improve readability and performance
commit 32382c2eaa988e714fcfc3b5526890a946d9a776
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 07:54:45 2024 +0000
Refactor rotary embedding function in modeling_phi.py
commit 3226f9f55c0c93ba7f0138bcf4532f1a687cb3f7
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 07:44:18 2024 +0000
Refactor InferenceParams and Embedding classes
commit d5b825d3c92bd8422dfe6d266f9a3c5e1c7f4c4c
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 07:24:26 2024 +0000
Refactor PhiConfig class in configuration_phi.py
commit 7a4d6c34fa23fd7878c03349e38200bb6fed0d06
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 07:16:25 2024 +0000
Refactor text_model.py and vision_encoder.py
commit ddf4a06c7171c9ee0a345fbd77b5bbe9c3cf6487
Author: Markus Heimerl <149831926+markusheimerl@users.noreply.github.com>
Date: Mon Jan 29 07:11:37 2024 +0000
Refactor code and remove unused imports
151 lines
3.9 KiB
Python
151 lines
3.9 KiB
Python
import torch
|
|
from torch import nn
|
|
from PIL import Image
|
|
from einops import rearrange
|
|
from torchvision.transforms.v2 import (
|
|
Compose,
|
|
Resize,
|
|
InterpolationMode,
|
|
ToImage,
|
|
ToDtype,
|
|
Normalize,
|
|
)
|
|
import timm
|
|
|
|
|
|
class VisualHolder(nn.Module):
|
|
def __init__(self, model):
|
|
super().__init__()
|
|
self.visual = model
|
|
|
|
def forward(self, x):
|
|
return self.visual(x)
|
|
|
|
|
|
class ModelHolder(nn.Module):
|
|
def __init__(self, model):
|
|
super().__init__()
|
|
self.model = model
|
|
|
|
def forward(self, x):
|
|
return self.model(x)
|
|
|
|
|
|
class LinearPatchEmbedding(nn.Module):
|
|
def __init__(self, conv):
|
|
super().__init__()
|
|
self.linear = nn.Linear(588, 1152)
|
|
self.linear.weight.data = conv.weight.data.view(1152, -1)
|
|
if conv.bias is not None:
|
|
self.linear.bias.data = conv.bias.data
|
|
|
|
def forward(self, x):
|
|
return self.linear(x)
|
|
|
|
|
|
class MLP(nn.Module):
|
|
def __init__(
|
|
self,
|
|
in_features: int,
|
|
hidden_features: int = None,
|
|
out_features: int = None,
|
|
act_layer: nn.Module = nn.GELU,
|
|
) -> None:
|
|
super().__init__()
|
|
out_features = out_features or in_features
|
|
hidden_features = hidden_features or in_features
|
|
self.fc1 = nn.Linear(in_features, hidden_features)
|
|
self.act = act_layer()
|
|
self.fc2 = nn.Linear(hidden_features, out_features)
|
|
|
|
torch.nn.init.kaiming_normal_(
|
|
self.fc1.weight, mode="fan_in", nonlinearity="relu"
|
|
)
|
|
torch.nn.init.kaiming_normal_(
|
|
self.fc2.weight, mode="fan_in", nonlinearity="relu"
|
|
)
|
|
|
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
x = self.fc1(x)
|
|
x = self.act(x)
|
|
x = self.fc2(x)
|
|
return x
|
|
|
|
|
|
class VisionProjection(nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
image_embedding_dim = 1152
|
|
model_dim = 2048
|
|
hidden_dim = model_dim * 4
|
|
|
|
self.mlp1 = MLP(image_embedding_dim, hidden_dim, model_dim)
|
|
self.mlp2 = MLP(model_dim, hidden_dim, model_dim)
|
|
self.ln = nn.LayerNorm(model_dim)
|
|
|
|
@property
|
|
def device(self):
|
|
return self.mlp1.fc1.weight.device
|
|
|
|
def forward(self, x):
|
|
x = self.mlp1(x)
|
|
x = self.ln(x)
|
|
x = x + self.mlp2(x)
|
|
return x
|
|
|
|
|
|
class VisionTower(nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
self.encoder = ModelHolder(
|
|
VisualHolder(timm.create_model("vit_so400m_patch14_siglip_384"))
|
|
)
|
|
self.encoder.model.visual.patch_embed = LinearPatchEmbedding(
|
|
self.encoder.model.visual.patch_embed.proj
|
|
)
|
|
self.encoder.model.visual.attn_pool = nn.Identity()
|
|
|
|
self.projection = VisionProjection()
|
|
|
|
def forward(self, x):
|
|
x = self.encoder(x)
|
|
x = self.projection(x)
|
|
return x
|
|
|
|
|
|
class VisionEncoder(nn.Module):
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
|
|
self.model = VisionTower()
|
|
self.preprocess = Compose(
|
|
[
|
|
Resize(size=(378, 378), interpolation=InterpolationMode.BICUBIC),
|
|
ToImage(),
|
|
ToDtype(torch.float32, scale=True),
|
|
Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
|
|
]
|
|
)
|
|
|
|
@property
|
|
def device(self):
|
|
return self.model.projection.mlp1.fc1.weight.device
|
|
|
|
@property
|
|
def dtype(self):
|
|
return self.model.projection.mlp1.fc1.weight.dtype
|
|
|
|
def __call__(self, image: Image) -> torch.Tensor:
|
|
with torch.no_grad():
|
|
image_vec = (
|
|
self.preprocess(image.convert("RGB"))
|
|
.unsqueeze(0)
|
|
.to(self.device, dtype=self.dtype)
|
|
)
|
|
image_vec = rearrange(
|
|
image_vec, "b c (h p1) (w p2) -> b (h w) (c p1 p2)", p1=14, p2=14
|
|
)
|
|
return self.model(image_vec)
|