fix moe shared weights bug

This commit is contained in:
wty-yy
2025-12-30 22:02:31 +08:00
parent 57d70df845
commit 010c5b1700
3 changed files with 31 additions and 64 deletions

View File

@@ -1,6 +1,7 @@
# 20251230 # 20251230
## v0.1.1 ## v0.1.1
1. 给cts算法加入robogauge异步评估 1. 给cts算法加入robogauge异步评估
Fix Bug: 修复MoE中专家使用了共享权重的问题, 换成Conv1D
# 20251221 # 20251221
1. 修改最大地形速度限制, y在所有地形上最大为1.0, z只有平地最大为2.0, x最大为2.0 1. 修改最大地形速度限制, y在所有地形上最大为1.0, z只有平地最大为2.0, x最大为2.0
2. 上调难度9地形难度 (都是moe-cts 100k能通过的难度): 2. 上调难度9地形难度 (都是moe-cts 100k能通过的难度):

View File

@@ -1,33 +1,13 @@
# SPDX-FileCopyrightText: Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause '''
# @File : actor_critic_cts.py
# Redistribution and use in source and binary forms, with or without @Time : 2025/12/30 21:06:08
# modification, are permitted provided that the following conditions are met: @Author : wty-yy
# @Version : 1.0
# 1. Redistributions of source code must retain the above copyright notice, this @Blog : https://wty-yy.github.io/
# list of conditions and the following disclaimer. @Desc : Concurrent Teacher Student Network
# @Refer : CTS https://arxiv.org/abs/2405.10830
# 2. Redistributions in binary form must reproduce the above copyright notice, '''
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Copyright (c) 2021 ETH Zurich, Nikita Rudin
import numpy as np import numpy as np
import torch import torch

View File

@@ -1,33 +1,13 @@
# SPDX-FileCopyrightText: Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause '''
# @File : actor_critic_moe_cts.py
# Redistribution and use in source and binary forms, with or without @Time : 2025/12/30 21:06:46
# modification, are permitted provided that the following conditions are met: @Author : wty-yy
# @Version : 1.0
# 1. Redistributions of source code must retain the above copyright notice, this @Blog : https://wty-yy.github.io/
# list of conditions and the following disclaimer. @Desc : Mixture of Experts Concurrent Teacher Student Network
# @Refer : CTS https://arxiv.org/abs/2405.10830, Switch Transformers https://arxiv.org/abs/2101.03961
# 2. Redistributions in binary form must reproduce the above copyright notice, '''
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Copyright (c) 2021 ETH Zurich, Nikita Rudin
import numpy as np import numpy as np
import torch import torch
@@ -208,7 +188,7 @@ class StudentMoEEncoder(nn.Module):
gating_dim, gating_dim,
hidden_dims=[512, 256], hidden_dims=[512, 256],
expert_num=8, expert_num=8,
expert_hidden_dim=128, expert_hidden_dim=256,
latent_dim=32, latent_dim=32,
activation='elu', activation='elu',
norm_type='l2norm', norm_type='l2norm',
@@ -231,7 +211,12 @@ class StudentMoEEncoder(nn.Module):
nn.Linear(last_dim, expert_num * expert_hidden_dim), nn.Linear(last_dim, expert_num * expert_hidden_dim),
activation activation
) )
self.experts_out = nn.Linear(expert_hidden_dim, latent_dim) self.experts_out = nn.Conv1d(
in_channels=expert_num*expert_hidden_dim,
out_channels=expert_num*latent_dim,
kernel_size=1,
groups=expert_num
)
# Gating network # Gating network
gating_layers = [] gating_layers = []
@@ -248,8 +233,9 @@ class StudentMoEEncoder(nn.Module):
weights = self.gating_network(obs) # (batch, expert_num) weights = self.gating_network(obs) # (batch, expert_num)
shared_features = self.experts_backbone(obs_no_goal) shared_features = self.experts_backbone(obs_no_goal)
expert_hidden = self.experts_hidden(shared_features) expert_hidden = self.experts_hidden(shared_features)
expert_hidden = expert_hidden.view(-1, self.expert_num, expert_hidden.shape[-1] // self.expert_num) expert_hidden = expert_hidden.unsqueeze(-1)
expert_latent = self.experts_out(expert_hidden) # (batch, expert_num, latent_dim) expert_latent_flat = self.experts_out(expert_hidden) # (batch, expert_num * latent_dim, 1)
expert_latent = expert_latent_flat.reshape(-1, self.expert_num, self.latent_dim)
latent = torch.sum(weights.unsqueeze(-1) * expert_latent, dim=1) # (batch, latent_dim) latent = torch.sum(weights.unsqueeze(-1) * expert_latent, dim=1) # (batch, latent_dim)
latent = self.norm_layer(latent) latent = self.norm_layer(latent)
return latent, weights return latent, weights