layer_scale.py 823 B

12345678910111213141516171819202122232425262728
  1. # Copyright (c) Meta Platforms, Inc. and affiliates.
  2. # All rights reserved.
  3. #
  4. # This source code is licensed under the license found in the
  5. # LICENSE file in the root directory of this source tree.
  6. # Modified from: https://github.com/huggingface/pytorch-image-models/blob/main/timm/models/vision_transformer.py#L103-L110
  7. from typing import Union
  8. import torch
  9. from torch import Tensor
  10. from torch import nn
  11. class LayerScale(nn.Module):
  12. def __init__(
  13. self,
  14. dim: int,
  15. init_values: Union[float, Tensor] = 1e-5,
  16. inplace: bool = False,
  17. ) -> None:
  18. super().__init__()
  19. self.inplace = inplace
  20. self.gamma = nn.Parameter(init_values * torch.ones(dim))
  21. def forward(self, x: Tensor) -> Tensor:
  22. return x.mul_(self.gamma) if self.inplace else x * self.gamma