MCPcopy Create free account
hub / github.com/OpenDriveLab/ReSim / __init__

Method __init__

sat/sgm/modules/encoders/traj_encoder.py:140–183  ·  view source on GitHub ↗
(self, *, seq_len, dim, out_dim, depth, mlp_dim, heads=8, dim_head = 64, dropout = 0., emb_dropout = 0.,
                 channels = 3,
                 pos_emb="learnable",
                 avoid_first_ln=False,
                 use_all_tokens=False,
                 zero_init=False)

Source from the content-addressed store, hash-verified

138# If traj is all zeros, return all zeros, as we might drop the traj in getitem stage.
139class TrajEncoder(AbstractEmbModel):
140 def __init__(self, *, seq_len, dim, out_dim, depth, mlp_dim, heads=8, dim_head = 64, dropout = 0., emb_dropout = 0.,
141 channels = 3,
142 pos_emb="learnable",
143 avoid_first_ln=False,
144 use_all_tokens=False,
145 zero_init=False):
146 super().__init__()
147 # assert (seq_len % patch_size) == 0
148
149
150 if avoid_first_ln:
151 self.to_patch_embedding = nn.Sequential(
152 nn.Linear(channels, dim),
153 nn.LayerNorm(dim),
154 )
155 else:
156 self.to_patch_embedding = nn.Sequential(
157 nn.LayerNorm(channels), # * Do we need this?
158 nn.Linear(channels, dim),
159 nn.LayerNorm(dim),
160 )
161
162 assert pos_emb in ["learnable", "sine"]
163
164 if pos_emb == "learnable":
165 self.pos_embedding = nn.Parameter(torch.randn(1, seq_len + 1, dim))
166 else:
167 grid_t = np.arange(1 + seq_len, dtype=np.float32) # +1 for cls token
168 sine_pos = get_1d_sincos_pos_embed_from_grid(dim, grid_t)
169 self.pos_embedding = torch.tensor(sine_pos).unsqueeze(0)
170
171 self.cls_token = nn.Parameter(torch.randn(dim))
172 self.dropout = nn.Dropout(emb_dropout)
173
174 self.transformer = Transformer(dim, depth, heads, dim_head, mlp_dim, dropout)
175
176 self.mlp_head = nn.Sequential(
177 nn.LayerNorm(dim),
178 nn.Linear(dim, out_dim)
179 )
180 if zero_init:
181 nn.init.zeros_(self.mlp_head[1].weight)
182 nn.init.zeros_(self.mlp_head[1].bias)
183 self.use_all_tokens = use_all_tokens
184
185 def forward(self, series):
186 series = series.to(torch.float16)

Callers

nothing calls this directly

Calls 3

TransformerClass · 0.85
__init__Method · 0.45

Tested by

no test coverage detected