File size: 8,604 Bytes
f24563f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
"""
Configuration for LLM training.
"""

from dataclasses import dataclass, field
from typing import Optional, List, Dict, Any, Union
import jax.numpy as jnp

from model.llm import LLMConfig


@dataclass
class TrainingConfig:
    """
    Configuration for training.

    Attributes:
        output_dir: Output directory
        model_config: Model configuration

        # Training parameters
        learning_rate: Learning rate
        weight_decay: Weight decay
        warmup_steps: Number of warmup steps
        max_steps: Maximum number of training steps
        batch_size: Batch size per device
        gradient_accumulation_steps: Number of steps to accumulate gradients
        max_grad_norm: Maximum gradient norm for clipping

        # Optimizer parameters
        optimizer: Optimizer type
        adam_beta1: Beta1 for Adam optimizer
        adam_beta2: Beta2 for Adam optimizer
        adam_epsilon: Epsilon for Adam optimizer

        # Logging parameters
        logging_steps: Number of steps between logging
        save_steps: Number of steps between checkpoints
        eval_steps: Number of steps between evaluations

        # Dataset parameters
        train_file: Path to training file
        eval_file: Path to evaluation file
        max_seq_length: Maximum sequence length

        # Tokenizer parameters
        tokenizer_file: Path to tokenizer file

        # Parallelism parameters
        parallelism_type: Type of parallelism
        tensor_parallel_size: Number of tensor parallel devices
        pipeline_parallel_size: Number of pipeline parallel devices

        # Performance optimization parameters
        use_flash_attention: Whether to use flash attention for efficiency
        use_gradient_checkpointing: Whether to use gradient checkpointing to save memory

        # Long context support parameters
        use_rope_scaling: Whether to use RoPE scaling for longer contexts
        rope_scaling_factor: Scaling factor for RoPE frequencies

        # Reasoning capabilities parameters
        use_reasoning_layer: Whether to use additional reasoning layers
        num_reasoning_layers: Number of additional reasoning layers
        reasoning_intermediate_size: Hidden dimension in reasoning feed-forward network

        # Miscellaneous parameters
        seed: Random seed
        dtype: Data type for computations
        mixed_precision: Whether to use mixed precision training
    """
    # Output directory
    output_dir: str = "output"

    # Model configuration
    model_config: LLMConfig = field(default_factory=LLMConfig)

    # Training parameters
    learning_rate: float = 3e-4
    weight_decay: float = 0.01
    warmup_steps: int = 1000
    max_steps: int = 100000
    batch_size: int = 32
    gradient_accumulation_steps: int = 1
    max_grad_norm: float = 1.0

    # Optimizer parameters
    optimizer: str = "adamw"  # "adamw", "lion"
    adam_beta1: float = 0.9
    adam_beta2: float = 0.999
    adam_epsilon: float = 1e-8

    # Logging parameters
    logging_steps: int = 100
    save_steps: int = 1000
    eval_steps: int = 1000

    # Dataset parameters
    train_file: str = ""
    eval_file: str = ""
    max_seq_length: int = 32768  # Increased to support longer contexts

    # Tokenizer parameters
    tokenizer_file: str = ""

    # Parallelism parameters
    parallelism_type: str = "data"  # "data", "tensor", "pipeline"
    tensor_parallel_size: int = 1
    pipeline_parallel_size: int = 1

    # Performance optimization parameters
    use_flash_attention: bool = True  # Use flash attention for efficiency
    use_gradient_checkpointing: bool = True  # Use gradient checkpointing to save memory

    # Long context support parameters
    use_rope_scaling: bool = True  # Use RoPE scaling for longer contexts
    rope_scaling_factor: float = 0.5  # Scaling factor for RoPE frequencies

    # Reasoning capabilities parameters
    use_reasoning_layer: bool = True  # Use additional reasoning layers
    num_reasoning_layers: int = 2  # Number of additional reasoning layers
    reasoning_intermediate_size: int = 16384  # Hidden dimension in reasoning feed-forward network

    # Miscellaneous parameters
    seed: int = 42
    dtype: jnp.dtype = jnp.bfloat16
    mixed_precision: bool = True


@dataclass
class ModelSizeConfig:
    """
    Configuration for different model sizes.

    Attributes:
        name: Model size name
        hidden_size: Hidden dimension
        num_hidden_layers: Number of transformer layers
        num_query_heads: Number of query heads
        num_kv_heads: Number of key-value heads
        intermediate_size: Hidden dimension in feed-forward network
        max_position_embeddings: Maximum sequence length
        vocab_size: Size of vocabulary
        reasoning_intermediate_size: Hidden dimension in reasoning feed-forward network
        num_reasoning_layers: Number of additional reasoning layers
    """
    name: str
    hidden_size: int
    num_hidden_layers: int
    num_query_heads: int
    num_kv_heads: int
    intermediate_size: int
    max_position_embeddings: int = 131072  # Increased to support 128K token contexts
    vocab_size: int = 32000
    reasoning_intermediate_size: int = 16384  # Hidden dimension for reasoning layers
    num_reasoning_layers: int = 2  # Number of reasoning layers


# Model size configurations
MODEL_SIZES = {
    "7b": ModelSizeConfig(
        name="7b",
        hidden_size=4096,
        num_hidden_layers=32,
        num_query_heads=32,
        num_kv_heads=8,
        intermediate_size=11008,
        max_position_embeddings=131072,  # Increased to support 128K token contexts
        vocab_size=32000,
        reasoning_intermediate_size=16384,
        num_reasoning_layers=1
    ),
    "13b": ModelSizeConfig(
        name="13b",
        hidden_size=5120,
        num_hidden_layers=40,
        num_query_heads=40,
        num_kv_heads=10,
        intermediate_size=13824,
        max_position_embeddings=131072,  # Increased to support 128K token contexts
        vocab_size=32000,
        reasoning_intermediate_size=20480,
        num_reasoning_layers=1
    ),
    "70b": ModelSizeConfig(
        name="70b",
        hidden_size=8192,
        num_hidden_layers=80,
        num_query_heads=64,
        num_kv_heads=8,
        intermediate_size=28672,
        max_position_embeddings=131072,  # Increased to support 128K token contexts
        vocab_size=32000,
        reasoning_intermediate_size=32768,
        num_reasoning_layers=2
    ),
    "175b": ModelSizeConfig(
        name="175b",
        hidden_size=12288,
        num_hidden_layers=96,
        num_query_heads=96,
        num_kv_heads=12,
        intermediate_size=49152,
        max_position_embeddings=131072,  # Increased to support 128K token contexts
        vocab_size=32000,
        reasoning_intermediate_size=49152,
        num_reasoning_layers=2
    ),
    "600b": ModelSizeConfig(
        name="600b",
        hidden_size=18432,
        num_hidden_layers=128,
        num_query_heads=128,
        num_kv_heads=16,
        intermediate_size=73728,
        max_position_embeddings=131072,  # Increased to support 128K token contexts
        vocab_size=32000,
        reasoning_intermediate_size=73728,
        num_reasoning_layers=3
    )
}


def get_model_config(model_size: str) -> LLMConfig:
    """
    Get model configuration for a specific model size.

    Args:
        model_size: Model size name

    Returns:
        Model configuration
    """
    if model_size not in MODEL_SIZES:
        raise ValueError(f"Model size {model_size} not supported")

    size_config = MODEL_SIZES[model_size]

    return LLMConfig(
        vocab_size=size_config.vocab_size,
        hidden_size=size_config.hidden_size,
        num_hidden_layers=size_config.num_hidden_layers,
        num_query_heads=size_config.num_query_heads,
        num_kv_heads=size_config.num_kv_heads,
        intermediate_size=size_config.intermediate_size,
        max_position_embeddings=size_config.max_position_embeddings,

        # Performance optimizations
        use_flash_attention=True,
        use_gradient_checkpointing=True,

        # Long context support for 128K tokens
        use_rope_scaling=True,
        rope_scaling_factor=0.25,  # More aggressive scaling for 128K tokens
        rope_theta=1000000.0,  # Increased base for better long-context performance

        # Reasoning capabilities
        use_reasoning_layer=True,
        num_reasoning_layers=size_config.num_reasoning_layers,
        reasoning_intermediate_size=size_config.reasoning_intermediate_size
    )