@@ -182,9 +182,6 @@ def __init__(self, config):
182182 self .model_type = config .model_type # act based on rtgs ('reward_conditioned') or not ('naive')
183183 self .ct = 0
184184
185- # input embedding stem
186- # self.tok_emb = nn.Embedding(config.vocab_size, config.n_embd)
187-
188185 # pos embedding
189186 self .pos_emb = nn .Parameter (torch .zeros (1 , config .block_size + 1 , config .n_embd ))
190187 self .global_pos_emb = nn .Parameter (torch .zeros (1 , config .max_timestep + 1 , config .n_embd ))
@@ -196,23 +193,6 @@ def __init__(self, config):
196193 # normalization
197194 self .ln_f = nn .LayerNorm (config .n_embd )
198195
199- # action prediction head
200- # if config.linear_rtg:
201- # self.reward_conditioned_head = nn.Linear(config.n_embd * 2, config.vocab_size, bias=False) # predict action conditioned on rtg
202- # else:
203- # self.reward_conditioned_head = nn.Sequential(
204- # nn.Linear(config.n_embd * 2, 512),
205- # nn.ReLU(),
206- # nn.Linear(512, config.vocab_size),
207- # )
208- # self.naive_head = nn.Linear(config.n_embd, config.vocab_size, bias=False) # predict action with state embedding
209- # # forward prediction head
210- # self.forward_pred_head = nn.Linear(config.n_embd * 2, config.n_embd, bias=True)
211- # # inverse prediction head
212- # self.inverse_pred_head = nn.Linear(config.n_embd * 2, config.vocab_size, bias=False)
213- # # reward prediction head
214- # self.reward_pred_head = nn.Linear(config.n_embd * 2, 1, bias=False)
215-
216196 # rtg-based action prediction head
217197 self .reward_conditioned_head = build_mlp (config .n_embd * 2 , config .vocab_size , config .rtg_layers , bias = False )
218198 # naive action prediction head (for behavior cloning)
@@ -228,33 +208,29 @@ def __init__(self, config):
228208
229209 self .apply (self ._init_weights )
230210
231- if hasattr (config , "vector_obs" ) and config .vector_obs :
232- self .state_encoder = nn .Sequential (nn .Linear (config .obs_dim , config .n_embd ), nn .Tanh ())
233- self .target_state_encoder = nn .Sequential (nn .Linear (config .obs_dim , config .n_embd ), nn .Tanh ())
234- else :
235- self .state_encoder = nn .Sequential (
236- nn .Conv2d (self .config .channels , 32 , 8 , stride = 4 , padding = 0 ),
237- nn .ReLU (),
238- nn .Conv2d (32 , 64 , 4 , stride = 2 , padding = 0 ),
239- nn .ReLU (),
240- nn .Conv2d (64 , 64 , 3 , stride = 1 , padding = 0 ),
241- nn .ReLU (),
242- nn .Flatten (),
243- nn .Linear (3136 , config .n_embd ),
244- nn .Tanh (),
245- )
211+ self .state_encoder = nn .Sequential (
212+ nn .Conv2d (self .config .channels , 32 , 8 , stride = 4 , padding = 0 ),
213+ nn .ReLU (),
214+ nn .Conv2d (32 , 64 , 4 , stride = 2 , padding = 0 ),
215+ nn .ReLU (),
216+ nn .Conv2d (64 , 64 , 3 , stride = 1 , padding = 0 ),
217+ nn .ReLU (),
218+ nn .Flatten (),
219+ nn .Linear (3136 , config .n_embd ),
220+ nn .Tanh (),
221+ )
246222
247- self .target_state_encoder = nn .Sequential (
248- nn .Conv2d (self .config .channels , 32 , 8 , stride = 4 , padding = 0 ),
249- nn .ReLU (),
250- nn .Conv2d (32 , 64 , 4 , stride = 2 , padding = 0 ),
251- nn .ReLU (),
252- nn .Conv2d (64 , 64 , 3 , stride = 1 , padding = 0 ),
253- nn .ReLU (),
254- nn .Flatten (),
255- nn .Linear (3136 , config .n_embd ),
256- nn .Tanh (),
257- )
223+ self .target_state_encoder = nn .Sequential (
224+ nn .Conv2d (self .config .channels , 32 , 8 , stride = 4 , padding = 0 ),
225+ nn .ReLU (),
226+ nn .Conv2d (32 , 64 , 4 , stride = 2 , padding = 0 ),
227+ nn .ReLU (),
228+ nn .Conv2d (64 , 64 , 3 , stride = 1 , padding = 0 ),
229+ nn .ReLU (),
230+ nn .Flatten (),
231+ nn .Linear (3136 , config .n_embd ),
232+ nn .Tanh (),
233+ )
258234 self .target_state_encoder .load_state_dict (self .state_encoder .state_dict ())
259235
260236 # rtg encoder
@@ -320,15 +296,11 @@ def forward(
320296
321297 is_testing = (actions is None ) or (actions .shape [1 ] != states .shape [1 ])
322298
323- # (batch * context_length, n_embd)
324- if hasattr (self .config , "vector_obs" ) and self .config .vector_obs :
325- state_embeddings = self .state_encoder (states )
326- else :
327- state_embeddings = self .state_encoder (
328- states .reshape (- 1 , self .config .channels , 84 , 84 ).type (torch .float32 ).contiguous ()
329- )
330- # (batch, context_length, n_embd)
331- state_embeddings = state_embeddings .reshape (states .shape [0 ], states .shape [1 ], self .config .n_embd )
299+ state_embeddings = self .state_encoder (
300+ states .reshape (- 1 , self .config .channels , 84 , 84 ).type (torch .float32 ).contiguous ()
301+ )
302+ # (batch, context_length, n_embd)
303+ state_embeddings = state_embeddings .reshape (states .shape [0 ], states .shape [1 ], self .config .n_embd )
332304
333305 if actions is not None :
334306 if self .config .cont_action :
@@ -369,11 +341,6 @@ def forward(
369341 state_output = x # for completeness
370342 action_output = None
371343
372- # print("token_embeddings", token_embeddings.size())
373- # print("final_embeddings", final_embeddings.size())
374- # print("state_output", state_output.size())
375- # print("action_output", action_output.size())
376-
377344 ## act
378345 rtg_action_logits , naive_action_logits = None , None
379346 ## compute losses
@@ -407,15 +374,12 @@ def forward(
407374 raise NotImplementedError ()
408375
409376 if pred_forward :
410- if hasattr (self .config , "vector_obs" ) and self .config .vector_obs :
411- next_state_embeddings = self .state_encoder (states ).detach ()
412- else :
413- next_state_embeddings = self .target_state_encoder (
414- states .reshape (- 1 , self .config .channels , 84 , 84 ).type (torch .float32 ).contiguous ()
415- ).detach () # (batch, context_length, n_embd)
416- next_state_embeddings = next_state_embeddings .reshape (
417- states .shape [0 ], states .shape [1 ], self .config .n_embd
418- )
377+ next_state_embeddings = self .target_state_encoder (
378+ states .reshape (- 1 , self .config .channels , 84 , 84 ).type (torch .float32 ).contiguous ()
379+ ).detach () # (batch, context_length, n_embd)
380+ next_state_embeddings = next_state_embeddings .reshape (
381+ states .shape [0 ], states .shape [1 ], self .config .n_embd
382+ )
419383 next_state_embeddings = next_state_embeddings [:, 1 :, :] # (batch, context_length-1, n_embd)
420384 forward_pred = self .forward_pred_head (
421385 torch .cat ((state_output [:, :- 1 , :], action_output [:, : - 1 + int (is_testing ), :]), dim = 2 )
@@ -459,14 +423,6 @@ def forward(
459423 rand_mask_obs_idx = np .random .choice (list (range (1 , actions .shape [1 ] - 1 )), mask_obs_size , replace = False )
460424 for j in range (mask_obs_size ):
461425 masked_token [:, 2 * rand_mask_obs_idx [j ], :] = - 1
462- # batch_size = states.shape[0]
463- # all_global_pos_emb = torch.repeat_interleave(
464- # self.global_pos_emb, batch_size, dim=0
465- # ) # batch_size, traj_length, n_embd
466- # position_embeddings = (
467- # torch.gather(all_global_pos_emb, 1, torch.repeat_interleave(timesteps, self.config.n_embd, dim=-1))
468- # + self.pos_emb[:, : token_embeddings.shape[1], :]
469- # )
470426
471427 final_masked_embeddings = self .drop (masked_token + position_embeddings )
472428
@@ -494,15 +450,11 @@ def get_embeddings(self, states, actions, timesteps):
494450 actions = None
495451 is_testing = (actions is None ) or (actions .shape [1 ] != states .shape [1 ])
496452
497- # (batch * context_length, n_embd)
498- if hasattr (self .config , "vector_obs" ) and self .config .vector_obs :
499- state_embeddings = self .state_encoder (states )
500- else :
501- state_embeddings = self .state_encoder (
502- states .reshape (- 1 , self .config .channels , 84 , 84 ).type (torch .float32 ).contiguous ()
503- )
504- # (batch, context_length, n_embd)
505- state_embeddings = state_embeddings .reshape (states .shape [0 ], states .shape [1 ], self .config .n_embd )
453+ state_embeddings = self .state_encoder (
454+ states .reshape (- 1 , self .config .channels , 84 , 84 ).type (torch .float32 ).contiguous ()
455+ )
456+ # (batch, context_length, n_embd)
457+ state_embeddings = state_embeddings .reshape (states .shape [0 ], states .shape [1 ], self .config .n_embd )
506458
507459 if actions is not None :
508460 if self .config .cont_action :
0 commit comments