Skip to content

Commit

Permalink
Add a ViT Encoder to TorchTitan (pytorch#589)
Browse files Browse the repository at this point in the history
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
fduwjj authored Oct 16, 2024
1 parent 7c3a1fa commit 3858dc9
Show file tree
Hide file tree
Showing 5 changed files with 1,123 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/multimodal_model/__init__.py
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.
73 changes: 73 additions & 0 deletions test/multimodal_model/test_multimodal_model.py
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
# )
58 changes: 58 additions & 0 deletions test/multimodal_model/test_utils.py
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,
)
)
16 changes: 16 additions & 0 deletions torchtitan/models/llama_multimodal/__init__.py
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
}
Loading

0 comments on commit 3858dc9

Please sign in to comment.