-
Notifications
You must be signed in to change notification settings - Fork 248
[llava 2/n] Support Llava Model Construction #1155
Changes from 31 commits
f4bf00b
2cabbe7
353fafe
728fc46
215331d
23d6504
22fd2a5
fff8647
4b666a7
cc8b4d6
7ec018a
94e56f1
2e3d1dc
cbadc92
43dfdc7
63d76a1
01bb624
319ac86
141fea0
8cd0936
304fece
1eff939
a356897
a190b0f
cbda879
6fbb460
f224da7
83f8501
128566c
f3cbd53
7aab3b4
7ffec73
672915a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,8 +11,12 @@ | |||||||||
| from dataclasses import dataclass | ||||||||||
| from enum import Enum | ||||||||||
| from pathlib import Path | ||||||||||
| from PIL import Image | ||||||||||
| import requests | ||||||||||
| import torchvision | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this torchvision import seems unused |
||||||||||
|
|
||||||||||
| from typing import Any, Callable, Dict, Optional, Union | ||||||||||
| from collections.abc import Hashable | ||||||||||
|
|
||||||||||
| import torch | ||||||||||
| import torch.nn as nn | ||||||||||
|
|
@@ -34,19 +38,147 @@ | |||||||||
|
|
||||||||||
| from torchchat.utils.build_utils import find_multiple, get_precision | ||||||||||
|
|
||||||||||
| from torchtune.models.flamingo import flamingo_decoder, flamingo_vision_encoder | ||||||||||
| from torchtune.models.llama3_1._component_builders import llama3_1 as llama3_1_builder | ||||||||||
| from torchtune.modules.model_fusion import DeepFusionModel | ||||||||||
| from torchtune.models.clip import clip_vision_encoder | ||||||||||
|
||||||||||
|
|
||||||||||
| config_path = Path(f"{str(Path(__file__).parent)}/model_params") | ||||||||||
|
|
||||||||||
|
|
||||||||||
| class QuickGELUActivation(nn.Module): | ||||||||||
| """ | ||||||||||
| Applies GELU approximation that is fast but somewhat inaccurate. See: https://github.com/hendrycks/GELUs | ||||||||||
| """ | ||||||||||
|
|
||||||||||
| def forward(self, input): | ||||||||||
| return input * torch.sigmoid(1.702 * input) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def identity(**kwargs): | ||||||||||
| if len(kwargs) != 1: | ||||||||||
| raise ValueError("Only one argument is expected") | ||||||||||
| return list(kwargs.values())[0] | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @dataclass | ||||||||||
| class ProjectorArgs: | ||||||||||
|
||||||||||
| in_channels: int = 1024 | ||||||||||
| out_channels: int = 4096 | ||||||||||
| activation: nn.Module = nn.GELU() | ||||||||||
|
|
||||||||||
|
|
||||||||||
| class MultiModalProjector(nn.Module): | ||||||||||
| def __init__(self, args: ProjectorArgs): | ||||||||||
| super().__init__() | ||||||||||
|
|
||||||||||
| self.linear_1 = nn.Linear(args.in_channels, args.out_channels, bias=True) | ||||||||||
| self.act = args.activation | ||||||||||
| self.linear_2 = nn.Linear(args.out_channels, args.out_channels, bias=True) | ||||||||||
|
|
||||||||||
| def forward(self, image_features): | ||||||||||
| hidden_states = self.linear_1(image_features) | ||||||||||
| hidden_states = self.act(hidden_states) | ||||||||||
| hidden_states = self.linear_2(hidden_states) | ||||||||||
| return hidden_states | ||||||||||
|
|
||||||||||
|
|
||||||||||
| class ConcateFusion(nn.Module): | ||||||||||
| def __init__( | ||||||||||
| self, | ||||||||||
| encoder: nn.Module, | ||||||||||
| decoder: nn.Module, | ||||||||||
| token_embedding_name="tok_embeddings", | ||||||||||
| mm_proj_in_channels=1024, | ||||||||||
| mm_proj_out_channels=4096, | ||||||||||
| mm_proj_activation=nn.GELU(), | ||||||||||
| ): | ||||||||||
| super().__init__() | ||||||||||
| self.encoder = encoder | ||||||||||
| self.decoder = decoder | ||||||||||
|
|
||||||||||
| # esclate the embedding layer outside decoder llava model need to fuse | ||||||||||
| # the text and image embedding together before passing to decoder. | ||||||||||
| self.tok_embeddings = getattr(self.decoder, token_embedding_name) | ||||||||||
|
|
||||||||||
| # set the embedding layer in decoder to None to jump the embedding layer over in decoder | ||||||||||
| self.decoder.__setattr__(token_embedding_name, None) | ||||||||||
|
|
||||||||||
| self.mm_projector = MultiModalProjector( | ||||||||||
| ProjectorArgs( | ||||||||||
| in_channels=mm_proj_in_channels, | ||||||||||
| out_channels=mm_proj_out_channels, | ||||||||||
| activation=mm_proj_activation, | ||||||||||
| ) | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| def forward( | ||||||||||
| self, | ||||||||||
| tokens: Tensor, | ||||||||||
| *, | ||||||||||
| post_tokens: Optional[Tensor] = None, | ||||||||||
| encoder_input: Optional[Tensor] = None, | ||||||||||
| encoder_mask: Optional[torch.Tensor] = None, | ||||||||||
| input_pos: Optional[torch.Tensor] = None, | ||||||||||
| ) -> Tensor: | ||||||||||
| if encoder_input is not None: | ||||||||||
| encoder_input = encoder_input.view(1, 1, *encoder_input.shape) | ||||||||||
| encoder_output = self.encoder( | ||||||||||
| encoder_input, | ||||||||||
| ) | ||||||||||
|
||||||||||
| encoder_output = self.encoder( | |
| encoder_input, | |
| ) | |
| encoder_output = self.encoder(encoder_input) |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
REturn type
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's really cool to see them working together!
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
match model_type:
case ModelType.TextOnly:
return cls._text_only()
case ModelType.Flamingo:
return cls.flamingo()
...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh YEAH we are in 3.10, it is a good timing to switch to match case statement!
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def _replace_know_params(self, params): | |
| def _replace_known_params(self, params): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stupid grammar issue
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "model_type": "llava", | ||
| "use_tiktoken": true, | ||
| "encoder": { | ||
| "tile_size": 336, | ||
| "patch_size": 14, | ||
| "embed_dim": 1024, | ||
| "num_layers": 24, | ||
| "num_heads": 16, | ||
| "out_indices": [ | ||
| 23 | ||
| ], | ||
| "output_cls_projection": false, | ||
| "max_num_tiles": 1, | ||
| "in_channels": 3, | ||
| "intermediate_act": "QuickGELUActivation()" | ||
| }, | ||
| "decoder": { | ||
| "n_layers": 32, | ||
| "n_heads": 32, | ||
| "dim": 4096, | ||
| "vocab_size": 32064, | ||
| "max_seq_length": 768 | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make sure that this is downloaded in install requirements (it probably already is)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they can be removed rn; they should be sth in the 3/n pr haha