-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ghstack-poisoned]
- Loading branch information
Showing
7 changed files
with
498 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass(frozen=True) | ||
class TextConfig: | ||
dim: int = 2048 | ||
n_layers: int = 24 | ||
vocab_size: int = 51200 | ||
max_context: int = 2048 | ||
n_heads: int = 32 | ||
|
||
|
||
@dataclass(frozen=True) | ||
class VisionConfig: | ||
enc_dim: int = 1152 | ||
enc_patch_size: int = 14 | ||
enc_n_layers: int = 27 | ||
enc_ff_dim: int = 4304 | ||
enc_n_heads: int = 16 | ||
crop_size: int = 378 | ||
in_channels: int = 3 | ||
|
||
|
||
@dataclass(frozen=True) | ||
class RegionConfig: | ||
dim: int = 2048 | ||
coord_feat_dim: int = 256 | ||
coord_out_dim: int = 1024 | ||
size_feat_dim: int = 512 | ||
size_out_dim: int = 2048 | ||
|
||
|
||
@dataclass | ||
class MoondreamConfig: | ||
text: TextConfig = TextConfig() | ||
vision: VisionConfig = VisionConfig() | ||
region: RegionConfig = RegionConfig() | ||
|
||
@classmethod | ||
def from_dict(cls, config_dict: dict): | ||
text_config = TextConfig(**config_dict.get("text", {})) | ||
vision_config = VisionConfig(**config_dict.get("vision", {})) | ||
region_config = RegionConfig(**config_dict.get("region", {})) | ||
return cls(text=text_config, vision=vision_config, region=region_config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
from .config import MoondreamConfig | ||
|
||
|
||
class MoondreamModel(nn.Module): | ||
def __init__(self, config: MoondreamConfig, dtype=torch.float16): | ||
super().__init__() | ||
self.config = config | ||
|
||
# Vision Model | ||
patch_dim = ( | ||
config.vision.enc_patch_size | ||
* config.vision.enc_patch_size | ||
* config.vision.in_channels | ||
) | ||
grid_size = config.vision.crop_size // config.vision.enc_patch_size | ||
num_patches = grid_size * grid_size | ||
|
||
self.vision = nn.ModuleDict( | ||
{ | ||
"patch_emb": nn.Linear(patch_dim, config.vision.enc_dim, dtype=dtype), | ||
"blocks": nn.ModuleList( | ||
[ | ||
nn.ModuleDict( | ||
{ | ||
"ln1": nn.LayerNorm(config.vision.enc_dim, dtype=dtype), | ||
"attn": nn.ModuleDict( | ||
{ | ||
"qkv": nn.Linear( | ||
config.vision.enc_dim, | ||
3 * config.vision.enc_dim, | ||
dtype=dtype, | ||
), | ||
"proj": nn.Linear( | ||
config.vision.enc_dim, | ||
config.vision.enc_dim, | ||
dtype=dtype, | ||
), | ||
} | ||
), | ||
"ln2": nn.LayerNorm(config.vision.enc_dim, dtype=dtype), | ||
"mlp": nn.ModuleDict( | ||
{ | ||
"fc1": nn.Linear( | ||
config.vision.enc_dim, | ||
config.vision.enc_ff_dim, | ||
dtype=dtype, | ||
), | ||
"fc2": nn.Linear( | ||
config.vision.enc_ff_dim, | ||
config.vision.enc_dim, | ||
dtype=dtype, | ||
), | ||
} | ||
), | ||
} | ||
) | ||
for _ in range(config.vision.enc_n_layers) | ||
] | ||
), | ||
"post_ln": nn.LayerNorm(config.vision.enc_dim, dtype=dtype), | ||
"proj_mlp": nn.ModuleDict( | ||
{ | ||
"fc1": nn.Linear( | ||
config.vision.enc_dim * 2, config.text.dim * 4, dtype=dtype | ||
), | ||
"fc2": nn.Linear( | ||
config.text.dim * 4, config.text.dim, dtype=dtype | ||
), | ||
} | ||
), | ||
} | ||
) | ||
self.vision.pos_emb = nn.Parameter( | ||
torch.zeros(1, num_patches, config.vision.enc_dim, dtype=dtype) | ||
) | ||
|
||
# Text Model | ||
self.text = nn.ModuleDict( | ||
{ | ||
"blocks": nn.ModuleList( | ||
[ | ||
nn.ModuleDict( | ||
{ | ||
"ln": nn.LayerNorm(config.text.dim, dtype=dtype), | ||
"attn": nn.ModuleDict( | ||
{ | ||
"qkv": nn.Linear( | ||
config.text.dim, | ||
3 * config.text.dim, | ||
dtype=dtype, | ||
), | ||
"proj": nn.Linear( | ||
config.text.dim, | ||
config.text.dim, | ||
dtype=dtype, | ||
), | ||
} | ||
), | ||
"mlp": nn.ModuleDict( | ||
{ | ||
"fc1": nn.Linear( | ||
config.text.dim, | ||
4 * config.text.dim, | ||
dtype=dtype, | ||
), | ||
"fc2": nn.Linear( | ||
4 * config.text.dim, | ||
config.text.dim, | ||
dtype=dtype, | ||
), | ||
} | ||
), | ||
} | ||
) | ||
for _ in range(config.text.n_layers) | ||
] | ||
), | ||
"post_ln": nn.LayerNorm(config.text.dim, dtype=dtype), | ||
"lm_head": nn.Linear( | ||
config.text.dim, config.text.vocab_size, dtype=dtype | ||
), | ||
} | ||
) | ||
self.text.wte = nn.Parameter( | ||
torch.empty(config.text.vocab_size, config.text.dim, dtype=dtype) | ||
) | ||
|
||
# Region Model | ||
self.region = nn.ModuleDict( | ||
{ | ||
"coord_encoder": nn.Linear( | ||
config.region.coord_feat_dim, config.region.dim, dtype=dtype | ||
), | ||
"coord_decoder": nn.ModuleDict( | ||
{ | ||
"fc1": nn.Linear( | ||
config.region.dim, config.region.dim * 4, dtype=dtype | ||
), | ||
"fc2": nn.Linear( | ||
config.region.dim * 4, | ||
config.region.coord_out_dim, | ||
dtype=dtype, | ||
), | ||
} | ||
), | ||
"size_encoder": nn.Linear( | ||
config.region.size_feat_dim, config.region.dim, dtype=dtype | ||
), | ||
"size_decoder": nn.ModuleDict( | ||
{ | ||
"fc1": nn.Linear( | ||
config.region.dim, config.region.dim * 4, dtype=dtype | ||
), | ||
"fc2": nn.Linear( | ||
config.region.dim * 4, | ||
config.region.size_out_dim, | ||
dtype=dtype, | ||
), | ||
} | ||
), | ||
} | ||
) | ||
self.region.coord_features = nn.Parameter( | ||
torch.empty(config.region.coord_feat_dim // 2, 1, dtype=dtype).T | ||
) | ||
self.region.size_features = nn.Parameter( | ||
torch.empty(config.region.size_feat_dim // 2, 2, dtype=dtype).T | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.