File size: 8,035 Bytes
8a6ca49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import copy
from typing import Any, Dict

from transformers import AutoConfig, PretrainedConfig


class ActionCodecConfig(PretrainedConfig):
    model_type = "action_codec"

    def __init__(
        self,
        embodiment_config: Dict[str, Any] = None,
        n_tokens: int = 16,
        n_quantizers: int = 1,
        z_dim: int = 512,
        vq_type: str = "vq",
        vq_codebook_size: int = 2048,
        vq_commitment_weight: float = 0.25,
        vq_decay: float = 0.99,
        vq_kmeans_init: bool = True,
        vq_threshold_ema_dead_code: int = 2,
        vq_quantizer_dropout: float = 0.25,
        encoder_dim: int = 256,
        encoder_n_layers: int = 6,
        encoder_n_heads: int = 8,
        encoder_add_self_attn: bool = False,
        encoder_add_causal_mask: bool = False,
        encoder_pos_encoding_type: str = "fourier",
        decoder_dim: int = 256,
        decoder_n_layers: int = 6,
        decoder_n_heads: int = 8,
        decoder_add_self_attn: bool = False,
        decoder_add_causal_mask: bool = False,
        decoder_pos_encoding_type: str = "fourier",
        decoder_cls_size: int = 1,
        **kwargs,
    ):
        super().__init__(**kwargs)

        if embodiment_config is None:
            default_config = {
                "franka_libero_20hz": {
                    "action_dim": 7,
                    "freq": 20,
                    "duration": 1,
                    "description": "20Hz 7-dim action for 1s. Delta eef position (xyz), orientation (rpy), and gripper position (1 open/0 close).",
                },
                "widowx_bridge_5hz": {
                    "action_dim": 7,
                    "freq": 5,
                    "duration": 1,
                    "description": "5Hz 7-dim action for 1s. Delta eef position (xyz), orientation (rpy), and gripper position (1 open/0 close).",
                },
                "franka_droid_15hz": {
                    "action_dim": 7,
                    "freq": 15,
                    "duration": 1,
                    "description": "15Hz 7-dim action for 1s. Delta eef position (xyz), orientation (rpy), and gripper position (1 open/0 close).",
                },
            }
            self.embodiment_config = copy.deepcopy(default_config)
        else:
            self.embodiment_config = copy.deepcopy(embodiment_config)

        self.n_tokens = n_tokens
        self.n_quantizers = n_quantizers
        self.z_dim = z_dim

        self.encoder_dim = encoder_dim
        self.encoder_n_layers = encoder_n_layers
        self.encoder_n_heads = encoder_n_heads
        self.encoder_add_self_attn = encoder_add_self_attn
        self.encoder_add_causal_mask = encoder_add_causal_mask
        self.encoder_pos_encoding_type = encoder_pos_encoding_type

        self.decoder_dim = decoder_dim
        self.decoder_n_layers = decoder_n_layers
        self.decoder_n_heads = decoder_n_heads
        self.decoder_add_self_attn = decoder_add_self_attn
        self.decoder_add_causal_mask = decoder_add_causal_mask
        self.decoder_pos_encoding_type = decoder_pos_encoding_type
        self.decoder_cls_size = decoder_cls_size

        self.vq_type = vq_type
        self.vq_codebook_size = vq_codebook_size
        self.vq_commitment_weight = vq_commitment_weight
        self.vq_decay = vq_decay
        self.vq_kmeans_init = vq_kmeans_init
        self.vq_threshold_ema_dead_code = vq_threshold_ema_dead_code
        self.vq_quantizer_dropout = vq_quantizer_dropout


class ActionCodecConfigOld(PretrainedConfig):
    model_type = "action_codec"

    def __init__(
        self,
        horizon: int = 20,
        action_dim: int = 7,
        action_encoding: str = "independent_v2",
        horizon_patch_size: int = 1,
        encoder_class: str = "action_codec.modules.perceiver.PerceiverEncoder",
        decoder_class: str = "action_codec.modules.perceiver.PerceiverDecoder",
        vq_class: str = "vector_quantize_pytorch.VectorQuantize",
        encoder_kwargs: Dict[str, Any] = None,
        decoder_kwargs: Dict[str, Any] = None,
        vq_kwargs: Dict[str, Any] = None,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.horizon = horizon
        self.action_dim = action_dim
        self.action_encoding = action_encoding
        self.horizon_patch_size = horizon_patch_size
        self.encoder_class = encoder_class
        self.decoder_class = decoder_class
        self.vq_class = vq_class
        self.encoder_kwargs = (
            dict(encoder_kwargs)
            if encoder_kwargs is not None
            else {
                "dim": 384,
                "in_len": horizon,
                "out_len": 16,
                "num_layers": 12,
                "num_heads": 4,
                "output_round": -1.0,
            }
        )
        self.decoder_kwargs = (
            dict(decoder_kwargs)
            if decoder_kwargs is not None
            else {
                "dim": 384,
                "in_len": 16,
                "out_len": horizon,
                "num_layers": 12,
                "num_heads": 4,
            }
        )
        self.vq_kwargs = (
            dict(vq_kwargs)
            if vq_kwargs is not None
            else {
                "dim": 512,
                "codebook_size": 2048,
                "kmeans_init": True,
                "kmeans_iters": 10,
                "decay": 0.99,
                "commitment_weight": 0.25,
                "rotation_trick": False,
                "threshold_ema_dead_code": 2,
                "use_cosine_sim": False,
                "codebook_diversity_loss_weight": 0.0,
            }
        )


class BPEActionCodecConfig(PretrainedConfig):
    model_type = "bpe_action_codec"

    def __init__(
        self,
        horizon: int = 20,
        action_dim: int = 7,
        action_encoding: str = "independent_v2",
        horizon_patch_size: int = 1,
        encoder_class: str = "action_codec.modules.perceiver.PerceiverEncoder",
        decoder_class: str = "action_codec.modules.perceiver.PerceiverDecoder",
        vq_class: str = "vector_quantize_pytorch.VectorQuantize",
        encoder_kwargs: Dict[str, Any] = None,
        decoder_kwargs: Dict[str, Any] = None,
        vq_kwargs: Dict[str, Any] = None,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.horizon = horizon
        self.action_dim = action_dim
        self.action_encoding = action_encoding
        self.horizon_patch_size = horizon_patch_size
        self.encoder_class = encoder_class
        self.decoder_class = decoder_class
        self.vq_class = vq_class
        self.encoder_kwargs = (
            dict(encoder_kwargs)
            if encoder_kwargs is not None
            else {
                "dim": 384,
                "in_len": horizon,
                "out_len": 16,
                "num_layers": 12,
                "num_heads": 4,
                "output_round": -1.0,
            }
        )
        self.decoder_kwargs = (
            dict(decoder_kwargs)
            if decoder_kwargs is not None
            else {
                "dim": 384,
                "in_len": 16,
                "out_len": horizon,
                "num_layers": 12,
                "num_heads": 4,
            }
        )
        self.vq_kwargs = (
            dict(vq_kwargs)
            if vq_kwargs is not None
            else {
                "dim": 512,
                "codebook_size": 2048,
                "kmeans_init": True,
                "kmeans_iters": 10,
                "decay": 0.99,
                "commitment_weight": 0.25,
                "rotation_trick": False,
                "threshold_ema_dead_code": 2,
                "use_cosine_sim": False,
                "codebook_diversity_loss_weight": 0.0,
            }
        )


AutoConfig.register("action_codec", ActionCodecConfig)
AutoConfig.register("bpe_action_codec", BPEActionCodecConfig)

ActionCodecConfig.register_for_auto_class()

__all__ = ["ActionCodecConfig", "BPEActionCodecConfig"]