v0.1.5 prev1; Add DualMoE

This commit is contained in:
wty-yy
2026-01-07 00:21:59 +08:00
parent 05e1e81d64
commit e4aa714eab
10 changed files with 487 additions and 12 deletions

View File

@@ -77,7 +77,8 @@ class _TorchPolicyExporter(torch.nn.Module):
self.forward = self.forward_cts
if hasattr(policy, "student_moe_encoder"):
self.student_moe_encoder = copy.deepcopy(policy.student_moe_encoder).cpu()
self.obs_no_goal_mask = copy.deepcopy(policy.obs_no_goal_mask).cpu()
if hasattr(policy, "obs_no_goal_mask"):
self.obs_no_goal_mask = copy.deepcopy(policy.obs_no_goal_mask).cpu()
self.history_length = policy.history.shape[1]
self.history = torch.zeros([1, policy.history.shape[1], policy.history.shape[2]], device='cpu')
self.forward = self.forward_moe_cts
@@ -98,6 +99,8 @@ class _TorchPolicyExporter(torch.nn.Module):
self.rnn = copy.deepcopy(policy.memory_s.rnn)
else:
raise ValueError("Policy does not have an actor/student module.")
if hasattr(policy, "student_moe_encoder") and hasattr(policy, "actor_moe"):
self.forward = self.forward_dual_moe_cts
# set up recurrent network
if self.is_recurrent:
self.rnn.cpu()
@@ -155,6 +158,14 @@ class _TorchPolicyExporter(torch.nn.Module):
mean, weights = self.actor(x)
return mean, (weights, latent)
def forward_dual_moe_cts(self, x): # x is single observations
x = self.normalizer(x)
self.history = torch.cat([self.history[:, 1:], x.unsqueeze(1)], dim=1)
latent, student_weights = self.student_moe_encoder(self.history.flatten(1))
x = torch.cat([latent, x], dim=1)
mean, actor_weights = self.actor(x)
return mean, (student_weights, actor_weights, latent)
@torch.jit.export
def reset(self):
if hasattr(self, 'history'):