File size: 12,658 Bytes
aa3b580
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""
MolmoAct configuration
"""

from typing import Tuple, Optional, Dict, Any

from transformers import PretrainedConfig
from transformers.modeling_rope_utils import rope_config_validation
from transformers.utils import logging

logger = logging.get_logger(__name__)


class MolmoActVitConfig(PretrainedConfig):
    r"""
    This is the configuration class to store the configuration of a [`MolmoActVisionTransformer`].
    It is used to instantiate a `MolmoActVisionTransformer` according to the specified arguments,
    defining the model architecture.

    Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
    documentation from [`PretrainedConfig`] for more information.

    Example:
    ```python
    >>> from transformers import MolmoActVitConfig, MolmoActVisionTransformer

    >>> # Initializing a MolmoActVitConfig
    >>> configuration = MolmoActVitConfig()

    >>> # Initializing a MolmoActVisionTransformer (with random weights)
    >>> model = MolmoActVisionTransformer(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```"""

    model_type = "molmoact_vit"

    def __init__(
        self,
        hidden_size: int = 1152,
        intermediate_size: int = 4304,
        num_hidden_layers: int = 27,
        num_attention_heads: int = 16,
        num_key_value_heads: int = 16,
        head_dim: int = 72,
        hidden_act: str = "gelu_pytorch_tanh",
        layer_norm_eps: float = 1e-6,
        image_default_input_size: Tuple[int, int] = (378, 378),
        image_patch_size: int = 14,
        image_num_pos: int = 577,
        attention_dropout: float = 0.0,
        residual_dropout: float = 0.0,
        initializer_range: float = 0.02,
        float32_attention: bool = True,
        use_cls_token: bool = False,      # True for OpenCLIP
        patch_bias: bool = True,          # False for OpenCLIP
        pre_layernorm: bool = False,      # True for OpenCLIP
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.hidden_size = hidden_size
        self.intermediate_size = intermediate_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.num_key_value_heads = num_key_value_heads
        self.head_dim = head_dim
        self.hidden_act = hidden_act
        self.layer_norm_eps = layer_norm_eps
        self.image_default_input_size = image_default_input_size
        self.image_patch_size = image_patch_size
        self.image_num_pos = image_num_pos
        self.attention_dropout = attention_dropout
        self.residual_dropout = residual_dropout
        self.initializer_range = initializer_range
        self.float32_attention = float32_attention
        self.use_cls_token = use_cls_token
        self.patch_bias = patch_bias
        self.pre_layernorm = pre_layernorm

    @property
    def image_num_patch(self):
        h, w = self.image_default_input_size
        return h // self.image_patch_size, w // self.image_patch_size


class MolmoActAdapterConfig(PretrainedConfig):
    r"""
    This is the configuration class to store the configuration of MolmoActAdapter. With MolmoActVitConfig,
    It is used to instantiate an MolmoActVisionBackbone according to the specified arguments,
    defining the model architecture.

    Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
    documentation from [`PretrainedConfig`] for more information.

    Example:

    ```python
    >>> from transformers import MolmoActVitConfig, MolmoActAdapterConfig, MolmoActVisionBackbone

    >>> # Initializing a MolmoActVitConfig and a MolmoActAdapterConfig
    >>> vit_config = MolmoActVitConfig()
    >>> adapter_config = MolmoPoolingConfig()

    >>> # Initializing a MolmoActVisionBackbone (with random weights)
    >>> model = MolmoActVisionBackbone(vit_config, adapter_config)

    >>> # Accessing the model configuration
    >>> vit_configuration = model.vit_config
    >>> adapter_configuration = model.adapter_config
    ```"""

    def __init__(
        self,
        vit_layers: Tuple = (-3, -9),
        hidden_size: int = 1152,
        num_attention_heads: int = 16,
        num_key_value_heads: int = 16,
        head_dim: int = 72,
        float32_attention: bool = True,
        attention_dropout: float = 0.0,
        residual_dropout: float = 0.0,
        hidden_act: str = "silu",
        intermediate_size: int = 18944,
        text_hidden_size: int = 3584,
        image_feature_dropout: float = 0.0,
        initializer_range: float = 0.02,
        # pooling_mode: str = "indices",            # "indices" (SigLIP) or "2x2_attention" (OpenCLIP)
        image_padding_embed: Optional[str] = None,  # e.g. "pad_and_partial_pad"
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.vit_layers = vit_layers
        self.hidden_size = hidden_size
        self.num_attention_heads = num_attention_heads
        self.num_key_value_heads = num_key_value_heads
        self.head_dim = head_dim
        self.float32_attention = float32_attention
        self.attention_dropout = attention_dropout
        self.residual_dropout = residual_dropout
        self.hidden_act = hidden_act
        self.intermediate_size = intermediate_size
        self.text_hidden_size = text_hidden_size
        self.image_feature_dropout = image_feature_dropout
        self.initializer_range = initializer_range
        # self.pooling_mode = pooling_mode
        self.image_padding_embed = image_padding_embed


class MolmoActLlmConfig(PretrainedConfig):
    r"""
    This is the configuration class to store the configuration of a [`MolmoActLlm`]. It is used to instantiate a
    `MolmoActLlm` according to the specified arguments, defining the model architecture.

    Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
    documentation from [`PretrainedConfig`] for more information.

    Example:
    ```python
    >>> from transformers import MolmoActLlmConfig, MolmoActLlm

    >>> # Initializing a MolmoActLlmConfig
    >>> configuration = MolmoActLlmConfig()

    >>> # Initializing a MolmoActLlm (with random weights)
    >>> model = MolmoActLlm(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```"""

    model_type = "molmoact_llm"
    keys_to_ignore_at_inference = ["past_key_values"]
    base_model_tp_plan = {
        "blocks.*.self_attn.att_proj": "colwise",
        "blocks.*.self_attn.attn_out": "rowwise",
        "blocks.*.mlp.ff_proj": "colwise",
        "blocks.*.mlp.ff_out": "rowwise",
    }
    base_model_pp_plan = {
        "wte": (["input_ids"], ["inputs_embeds"]),
        "blocks": (["hidden_states", "attention_mask"], ["hidden_states"]),
        "ln_f": (["hidden_states"], ["hidden_states"]),
    }

    def __init__(
        self,
        hidden_size: int = 3584,
        num_attention_heads: int = 28,
        num_key_value_heads: Optional[int] = 4,
        head_dim: int = 128,
        vocab_size: int = 152064,
        additional_vocab_size: int = 128,
        qkv_bias: bool = True,
        num_hidden_layers: int = 48,
        intermediate_size: int = 18944,
        hidden_act: str = "silu",
        embedding_dropout: float=0.0,
        attention_dropout: float=0.0,
        residual_dropout: float = 0.0,
        max_position_embeddings: int = 4096,
        rope_theta: float = 1000000.0,
        rope_scaling: Dict[str, Any] = None,
        use_qk_norm: bool = False,
        qk_norm_type: str = "olmo",
        layer_norm_eps: int = 1e-6,
        norm_after: bool = False,
        initializer_range: float = 0.02,
        use_cache=True,
        tie_word_embeddings=False,
        **kwargs,
    ):
        super().__init__(
            tie_word_embeddings=tie_word_embeddings,
            **kwargs
        )
        self.hidden_size = hidden_size
        self.num_attention_heads = num_attention_heads
        if num_key_value_heads is None:
            num_key_value_heads = num_attention_heads
        self.num_key_value_heads = num_key_value_heads
        self.head_dim = head_dim
        self.vocab_size = vocab_size
        self.additional_vocab_size = additional_vocab_size
        self.qkv_bias = qkv_bias
        self.num_hidden_layers = num_hidden_layers
        self.intermediate_size = intermediate_size
        self.hidden_act = hidden_act
        self.embedding_dropout = embedding_dropout
        self.attention_dropout = attention_dropout
        self.residual_dropout = residual_dropout
        self.max_position_embeddings = max_position_embeddings
        self.rope_theta = rope_theta
        self.rope_scaling = rope_scaling
        self.use_qk_norm = use_qk_norm
        self.qk_norm_type = qk_norm_type
        self.layer_norm_eps = layer_norm_eps
        self.norm_after = norm_after
        self.initializer_range = initializer_range
        self.use_cache = use_cache

        # Validate the correctness of rotary position embeddings parameters
        rope_config_validation(self)


class MolmoActConfig(PretrainedConfig):
    r"""
    This is the configuration class to store the configuration of a [`MolmoActForActionReasoning`].
    It is used to instantiate an MolmoAct model according to the specified arguments, defining the model architecture.

    Example:

    ```python
    >>> from transformers import MolmoActConfig, MolmoActVitConfig, MolmoActAdapterConfig, MolmoActLlmConfig

    >>> # Initializing a MolmoActVitConfig
    >>> vit_config = MolmoActVitConfig()

    >>> # Initializing a MolmoActAdapterConfig
    >>> adapter_config = MolmoActAdapterConfig()

    >>> # Initializing a MolmoActLlmConfig
    >>> llm_config = MolmoActLlmConfig()

    >>> # Initializing a MolmoActConfig
    >>> configuration = MolmoActConfig(vit_config, adapter_config, llm_config, image_patch_id=152069)

    >>> # Initializing a model
    >>> model = MolmoActForActionReasoning(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```"""

    model_type = "molmoact"
    sub_configs = {
        "llm_config": MolmoActLlmConfig,
        "vit_config": MolmoActVitConfig,
        "adapter_config": MolmoActAdapterConfig,
    }

    def __init__(
        self,
        vit_config: MolmoActVitConfig = None,
        adapter_config: MolmoActAdapterConfig = None,
        llm_config: MolmoActLlmConfig = None,
        image_patch_id: int = None,
        initializer_range: float = 0.02,
        n_action_bins: int = 256,
        norm_stats: dict = {},
        **kwargs,
    ):
        super().__init__(**kwargs)
        if vit_config is None:
            self.vit_config = MolmoActVitConfig()
        elif isinstance(vit_config, dict):
            self.vit_config = MolmoActVitConfig(**vit_config)
        else:
            self.vit_config = vit_config
        if adapter_config is None:
            self.adapter_config = MolmoActAdapterConfig()
        elif isinstance(adapter_config, dict):
            self.adapter_config = MolmoActAdapterConfig(**adapter_config)
        else:
            self.adapter_config = adapter_config
        if llm_config is None:
            self.llm_config = MolmoActLlmConfig()
        elif isinstance(llm_config, dict):
            self.llm_config = MolmoActLlmConfig(**llm_config)
        else:
            self.llm_config = llm_config
        self.image_patch_id = image_patch_id
        self.initializer_range = initializer_range

        self.n_action_bins = n_action_bins
        self.norm_stats = norm_stats

    @property
    def image_num_patch(self):
        assert self.vit_config is not None
        return self.vit_config.image_num_patch
    
    @property
    def num_attention_heads(self):
        return self.llm_config.num_attention_heads
    
    @property
    def num_key_value_heads(self):
        return self.llm_config.num_key_value_heads

    @property
    def head_dim(self):
        return self.llm_config.head_dim

    @property
    def num_hidden_layers(self):
        return self.llm_config.num_hidden_layers
    
    @property
    def hidden_size(self):
        return self.llm_config.hidden_size
    
    @property
    def vocab_size(self):
        return self.llm_config.vocab_size
    
    @property
    def max_position_embeddings(self):
        return self.llm_config.max_position_embeddings


MolmoActVitConfig.register_for_auto_class()
MolmoActAdapterConfig.register_for_auto_class()
MolmoActLlmConfig.register_for_auto_class()
MolmoActConfig.register_for_auto_class()