|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import pytest |
| 8 | +import torch |
| 9 | +from tests.test_utils import assert_expected, init_weights_with_constant |
| 10 | +from torch import nn, Tensor |
| 11 | +from torchmultimodal.models.coca.multimodal_decoder import CoCaMultimodalDecoder |
| 12 | + |
| 13 | + |
| 14 | +class TestCoCaMultimodalDecoder: |
| 15 | + @pytest.fixture |
| 16 | + def batch_size(self): |
| 17 | + return 2 |
| 18 | + |
| 19 | + @pytest.fixture |
| 20 | + def input_seq_len(self): |
| 21 | + return 5 |
| 22 | + |
| 23 | + @pytest.fixture |
| 24 | + def num_image_positions(self): |
| 25 | + return 10 |
| 26 | + |
| 27 | + @pytest.fixture |
| 28 | + def text_embedding_dim(self): |
| 29 | + return 4 |
| 30 | + |
| 31 | + @pytest.fixture |
| 32 | + def multimodal_decoder(self, input_seq_len, batch_size, text_embedding_dim): |
| 33 | + decoder = CoCaMultimodalDecoder( |
| 34 | + input_seq_len=input_seq_len, |
| 35 | + text_embedding_dim=text_embedding_dim, |
| 36 | + n_layer=2, |
| 37 | + n_head=2, |
| 38 | + dim_feedforward=4 * text_embedding_dim, |
| 39 | + output_dim=3, |
| 40 | + final_layer_norm_eps=1e-5, |
| 41 | + ) |
| 42 | + init_weights_with_constant(decoder) |
| 43 | + |
| 44 | + # Custom init final MLP layer weight, final LN, and text projection |
| 45 | + decoder.transformer_decoder.layer[1].feedforward.model[2].weight = nn.Parameter( |
| 46 | + torch.arange( |
| 47 | + decoder.transformer_decoder.layer[1] |
| 48 | + .feedforward.model[2] |
| 49 | + .weight.numel(), |
| 50 | + dtype=torch.float, |
| 51 | + ).reshape( |
| 52 | + decoder.transformer_decoder.layer[1].feedforward.model[2].weight.shape |
| 53 | + ) |
| 54 | + ) |
| 55 | + decoder.output_projection.weight = nn.Parameter( |
| 56 | + torch.arange(decoder.output_projection.weight.numel(), dtype=torch.float) |
| 57 | + .reshape(decoder.output_projection.weight.T.shape) |
| 58 | + .T |
| 59 | + ) |
| 60 | + decoder.transformer_decoder.final_layer_norm.weight = nn.Parameter( |
| 61 | + torch.arange( |
| 62 | + decoder.transformer_decoder.final_layer_norm.weight.numel(), |
| 63 | + dtype=torch.float, |
| 64 | + ) |
| 65 | + ) |
| 66 | + decoder.eval() |
| 67 | + return decoder |
| 68 | + |
| 69 | + @pytest.fixture |
| 70 | + def text_inputs(self, batch_size, input_seq_len, text_embedding_dim): |
| 71 | + return torch.arange(0.0, 1.0, 1.0 / 40).reshape( |
| 72 | + batch_size, input_seq_len, text_embedding_dim |
| 73 | + ) |
| 74 | + |
| 75 | + @pytest.fixture |
| 76 | + def image_inputs(self, batch_size, num_image_positions, text_embedding_dim): |
| 77 | + return torch.arange(10.0, 20.0, 1.0 / 8).reshape( |
| 78 | + batch_size, num_image_positions, text_embedding_dim |
| 79 | + ) |
| 80 | + |
| 81 | + @pytest.fixture |
| 82 | + def expected(self): |
| 83 | + return Tensor( |
| 84 | + [ |
| 85 | + [ |
| 86 | + [58.2492, 66.7214, 75.1935], |
| 87 | + [58.2492, 66.7214, 75.1935], |
| 88 | + [58.2492, 66.7214, 75.1935], |
| 89 | + [58.2492, 66.7214, 75.1935], |
| 90 | + [58.2492, 66.7214, 75.1935], |
| 91 | + ], |
| 92 | + [ |
| 93 | + [58.2492, 66.7214, 75.1935], |
| 94 | + [58.2492, 66.7214, 75.1935], |
| 95 | + [58.2492, 66.7214, 75.1935], |
| 96 | + [58.2492, 66.7214, 75.1935], |
| 97 | + [58.2492, 66.7214, 75.1935], |
| 98 | + ], |
| 99 | + ] |
| 100 | + ) |
| 101 | + |
| 102 | + def test_coca_multimodal_decoder( |
| 103 | + self, text_inputs, image_inputs, multimodal_decoder, expected |
| 104 | + ): |
| 105 | + actual = multimodal_decoder(text_inputs, image_inputs) |
| 106 | + assert_expected(actual, expected, rtol=0, atol=1e-4) |
| 107 | + |
| 108 | + def test_scripting(self, text_inputs, image_inputs, multimodal_decoder): |
| 109 | + scripted_multimodal_decoder = torch.jit.script(multimodal_decoder) |
| 110 | + assert_expected( |
| 111 | + scripted_multimodal_decoder(text_inputs, image_inputs), |
| 112 | + multimodal_decoder(text_inputs, image_inputs), |
| 113 | + rtol=0, |
| 114 | + atol=1e-4, |
| 115 | + ) |
0 commit comments