forked from pytorch/torchtitan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a ViT Encoder to TorchTitan (pytorch#589)
This is first step to include more models into torchtitan to demonstrate composability of pretrain. Now with llama 3.2 coming and we already have it available in torch tune. We want to bring multi-mode model to torch titan as well. After a deep discussion with the team, we believe the goal of torch titan is to demonstrate distributed training paradigms for different model architectures and each one is quite different. So it makes sense to take the HuggingFace approach when we have each model to own its own definition and code, no inheritance and no common modules will be used. So we create a new folder named "llama_multimodal" and this PR is to add a vision encoder first.
- Loading branch information
Showing
5 changed files
with
1,123 additions
and
0 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,5 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. |
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,73 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import pytest | ||
import torch | ||
from torchtitan.models.llama_multimodal import ModelArgs, VisionEncoder | ||
|
||
from test.multimodal_model.test_utils import fixed_init_model, fixed_init_tensor | ||
|
||
|
||
@pytest.fixture | ||
def model_config(): | ||
return ModelArgs( | ||
dim=32, | ||
num_layers=2, | ||
num_heads=4, | ||
tile_size=49, | ||
patch_size=9, | ||
max_num_tiles=4, | ||
in_channels=3, | ||
return_intermediates=[0, 1], | ||
num_layers_learnable_head=2, | ||
decoder_embed_dim=128, | ||
) | ||
|
||
|
||
class TestMultimodalModelVisionEncoder: | ||
@pytest.fixture(autouse=True) | ||
def setup_class(self, model_config): | ||
self.model_args = model_config | ||
self.batch_size = 1 | ||
self.num_imgs = 2 | ||
self.num_tiles = 4 | ||
self.aspect_ratio = torch.tensor([[1, 3], [2, 2]]).reshape( | ||
self.batch_size, self.num_imgs, 2 | ||
) | ||
image = torch.rand( | ||
( | ||
self.batch_size, | ||
self.num_imgs, | ||
self.num_tiles, | ||
self.model_args.in_channels, | ||
self.model_args.tile_size, | ||
self.model_args.tile_size, | ||
) | ||
) | ||
self.image = fixed_init_tensor(image.shape, min_val=-1, max_val=1) | ||
|
||
def test_llama_mm_vision_encoder(self): | ||
model = VisionEncoder(self.model_args) | ||
fixed_init_model(model, min_val=-1, max_val=1) | ||
# call model | ||
output = model(self.image, self.aspect_ratio) | ||
|
||
# assertion | ||
expected_shape = ( | ||
self.batch_size, | ||
self.num_imgs * self.num_tiles * (model.vit.patches_per_tile + 1), | ||
self.model_args.decoder_embed_dim, | ||
) | ||
assert ( | ||
output.shape == expected_shape | ||
), f"Expected shape {expected_shape}, but got {output.shape}" | ||
|
||
# TODO: Need to ensure numerical stability before doing convergence test. | ||
# output.mean() = 3.994, we need to debug why it is not close to 5.28800, which is | ||
# the test value from the original torch tune test | ||
# assert torch.allclose( | ||
# output.mean(), torch.tensor(5.28800), atol=1e-3, rtol=1e-3 | ||
# ) |
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,58 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import math | ||
|
||
from typing import Optional, Union | ||
|
||
import torch | ||
from torch import nn | ||
|
||
|
||
def fixed_init_tensor( | ||
shape: torch.Size, | ||
min_val: Union[float, int] = 0.0, | ||
max_val: Union[float, int] = 1.0, | ||
nonlinear: bool = False, | ||
dtype: torch.dtype = torch.float, | ||
): | ||
""" | ||
Utility for generating deterministic tensors of a given shape. In general stuff | ||
like torch.ones, torch.eye, etc can result in trivial outputs. This utility | ||
generates a range tensor [min_val, max_val) of a specified dtype, applies | ||
a sine function if nonlinear=True, then reshapes to the appropriate shape. | ||
""" | ||
n_elements = math.prod(shape) | ||
step_size = (max_val - min_val) / n_elements | ||
x = torch.arange(min_val, max_val, step_size, dtype=dtype) | ||
x = x.reshape(shape) | ||
if nonlinear: | ||
return torch.sin(x) | ||
return x | ||
|
||
|
||
@torch.no_grad | ||
def fixed_init_model( | ||
model: nn.Module, | ||
min_val: Union[float, int] = 0.0, | ||
max_val: Union[float, int] = 1.0, | ||
nonlinear: bool = False, | ||
dtype: Optional[torch.dtype] = None, | ||
): | ||
""" | ||
This utility initializes all parameters of a model deterministically using the | ||
function fixed_init_tensor above. See that docstring for details of each parameter. | ||
""" | ||
for _, param in model.named_parameters(): | ||
param.copy_( | ||
fixed_init_tensor( | ||
param.shape, | ||
min_val=min_val, | ||
max_val=max_val, | ||
nonlinear=nonlinear, | ||
dtype=param.dtype if dtype is None else dtype, | ||
) | ||
) |
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,16 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
# | ||
# Llama 2 is licensed under the LLAMA 2 Community License, | ||
# Copyright (c) Meta Platforms, Inc. All Rights Reserved. | ||
|
||
from torchtitan.models.llama_multimodal.model import ModelArgs, VisionEncoder | ||
|
||
__all__ = ["VisionEncoder", "ModelArgs"] | ||
|
||
llama3_2_configs = { | ||
# TODO: add configs for llama3.2 | ||
} |
Oops, something went wrong.