Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Ensure dtype consistency between adaptor and lang_tokens #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions models/rdt_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def __init__(self, *, action_dim, pred_horizon, config,
max_lang_cond_len, img_cond_len, lang_pos_embed_config=None,
img_pos_embed_config=None, dtype=torch.bfloat16):
super(RDTRunner, self).__init__()

self.dtype = dtype

# Create diffusion model
hidden_size = config['rdt']['hidden_size']
self.model = RDT(
Expand All @@ -41,18 +44,21 @@ def __init__(self, *, action_dim, pred_horizon, config,
self.lang_adaptor = self.build_condition_adapter(
config['lang_adaptor'],
in_features=lang_token_dim,
out_features=hidden_size
out_features=hidden_size,
dtype=self.dtype
)
self.img_adaptor = self.build_condition_adapter(
config['img_adaptor'],
in_features=img_token_dim,
out_features=hidden_size
out_features=hidden_size,
dtype=self.dtype
)
# A `state` refers to an action or a proprioception vector
self.state_adaptor = self.build_condition_adapter(
config['state_adaptor'],
in_features=state_token_dim * 2, # state + state mask (indicator)
out_features=hidden_size
out_features=hidden_size,
dtype=self.dtype
)

# Create the noise scheduler
Expand Down Expand Up @@ -83,18 +89,18 @@ def __init__(self, *, action_dim, pred_horizon, config,
[p.numel() for p in self.state_adaptor.parameters()]))

def build_condition_adapter(
self, projector_type, in_features, out_features):
self, projector_type, in_features, out_features, dtype):
projector = None
if projector_type == 'linear':
projector = nn.Linear(in_features, out_features)
projector = nn.Linear(in_features, out_features, dtype=dtype)
else:
mlp_gelu_match = re.match(r'^mlp(\d+)x_gelu$', projector_type)
if mlp_gelu_match:
mlp_depth = int(mlp_gelu_match.group(1))
modules = [nn.Linear(in_features, out_features)]
modules = [nn.Linear(in_features, out_features, dtype=dtype)]
for _ in range(1, mlp_depth):
modules.append(nn.GELU(approximate="tanh"))
modules.append(nn.Linear(out_features, out_features))
modules.append(nn.Linear(out_features, out_features, dtype=dtype))
projector = nn.Sequential(*modules)

if projector is None:
Expand Down