forked from pytorch/torchtitan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
92 lines (75 loc) · 2.77 KB
/
Copy pathmodel.py
File metadata and controls
92 lines (75 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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.
#
# Copyright (c) Meta Platforms, Inc. All Rights Reserved.
from dataclasses import dataclass
import torch
from torch import nn
from torchtitan.models.common.attention import AttentionMasksType
from torchtitan.models.common.decoder import Decoder, TransformerBlock
from torchtitan.models.utils import get_dense_model_nparams_and_flops
class Llama3TransformerBlock(TransformerBlock):
"""
Llama3 TransformerBlock Module
Args:
layer_id (int): Identifier for the layer.
dim (int): Model dimension.
n_layers (int): Total number of layers.
config (Llama3TransformerBlock.Config): Block configuration.
"""
@dataclass(kw_only=True, slots=True)
class Config(TransformerBlock.Config):
pass
def __init__(self, config: Config):
super().__init__()
self.attention = config.attention.build()
assert config.feed_forward is not None
self.feed_forward = config.feed_forward.build()
self.attention_norm = config.attention_norm.build()
self.ffn_norm = config.ffn_norm.build()
def forward(
self,
x: torch.Tensor,
attention_masks: AttentionMasksType | None,
positions: torch.Tensor | None = None,
):
h = x + self.attention(self.attention_norm(x), attention_masks, positions)
out = h + self.feed_forward(self.ffn_norm(h))
return out
class Llama3Model(Decoder):
"""
Llama3Model Module
Args:
config (Llama3Model.Config): Model configuration.
"""
@dataclass(kw_only=True, slots=True)
class Config(Decoder.Config):
dim: int = 4096
vocab_size: int = 128256
def update_from_config(
self,
*,
config,
**kwargs,
) -> None:
Decoder.Config.update_from_config(self, config=config, **kwargs)
parallelism = config.parallelism
from torchtitan.models.llama3.sharding import set_llama3_sharding_config
set_llama3_sharding_config(
self,
enable_sp=parallelism.enable_sequence_parallel,
)
def get_nparams_and_flops(
self, model: nn.Module, seq_len: int
) -> tuple[int, int]:
return get_dense_model_nparams_and_flops(
model,
n_layers=len(self.layers),
n_heads=self.layers[0].attention.n_heads,
head_dims=2 * (self.dim // self.layers[0].attention.n_heads),
seq_len=seq_len,
enable_weight_tying=self.enable_weight_tying,
)