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

Add inputs_embeds as alternative for input_ids #158

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
10 changes: 6 additions & 4 deletions mamba_ssm/models/mixer_seq_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ def allocate_inference_cache(self, batch_size, max_seqlen, dtype=None, **kwargs)
for i, layer in enumerate(self.layers)
}

def forward(self, input_ids, inference_params=None):
hidden_states = self.embedding(input_ids)
def forward(self, input_ids, inference_params=None, inputs_embeds=None):
if input_ids is not None and inputs_embeds is not None:
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
hidden_states = self.embedding(input_ids) if inputs_embeds is None else inputs_embeds
residual = None
for layer in self.layers:
hidden_states, residual = layer(
Expand Down Expand Up @@ -225,12 +227,12 @@ def tie_weights(self):
def allocate_inference_cache(self, batch_size, max_seqlen, dtype=None, **kwargs):
return self.backbone.allocate_inference_cache(batch_size, max_seqlen, dtype=dtype, **kwargs)

def forward(self, input_ids, position_ids=None, inference_params=None, num_last_tokens=0):
def forward(self, input_ids, position_ids=None, inference_params=None, num_last_tokens=0, inputs_embeds=None):
"""
"position_ids" is just to be compatible with Transformer generation. We don't use it.
num_last_tokens: if > 0, only return the logits for the last n tokens
"""
hidden_states = self.backbone(input_ids, inference_params=inference_params)
hidden_states = self.backbone(input_ids, inference_params=inference_params, inputs_embeds=inputs_embeds)
if num_last_tokens > 0:
hidden_states = hidden_states[:, -num_last_tokens:]
lm_logits = self.lm_head(hidden_states)
Expand Down