Skip to content

Retrofit sentence bert into BertForSeqClassification #3924

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
18 changes: 14 additions & 4 deletions examples/pytorch/quickstart.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from tensorrt_llm import SamplingParams
from tensorrt_llm._torch import LLM

import torch


def main():
prompts = [
Expand All @@ -9,15 +11,23 @@ def main():
"The capital of France is",
"The future of AI is",
]
sampling_params = SamplingParams(max_tokens=32)
sampling_params = SamplingParams(max_tokens=32, return_context_logits=True)

llm = LLM(model='TinyLlama/TinyLlama-1.1B-Chat-v1.0')
model_path = "/code/tensorrt_llm/custom_bert_classifier"
llm = LLM(model=model_path)
outputs = llm.generate(prompts, sampling_params)

tllm_logits = []
for i, output in enumerate(outputs):
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"[{i}] Prompt: {prompt!r}, Generated text: {generated_text!r}")
logits = output.context_logits.cpu()
print(f"[{i}] Prompt: {prompt!r}, logits: {logits}")
tllm_logits += [logits]

# stack logits
tllm_logits = torch.stack(tllm_logits)
print(f"tllm_logits: {tllm_logits}")



if __name__ == '__main__':
Expand Down
24 changes: 8 additions & 16 deletions tensorrt_llm/_torch/models/modeling_bert.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,31 +156,23 @@ class BertPooler(nn.Module):

def __init__(self, config):
super().__init__()
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.activation = nn.Tanh()

def forward(
self,
hidden_states: torch.Tensor,
attn_metadata: AttentionMetadata,
) -> torch.Tensor:

# We "pool" the model by simply taking the hidden state corresponding
# to the first token.
# Apply average pooling over all token embeddings
if attn_metadata is not None:
#NOTE: select the first tokens
offset = attn_metadata.seq_lens_cuda
selected_tokens = torch.cumsum(
attn_metadata.seq_lens_cuda,
dim=0,
dtype=torch.long,
) - offset
hidden_states = hidden_states[selected_tokens]
# Use sequence lengths from attn_metadata to mask padding tokens
seq_lens = attn_metadata.seq_lens_cuda
mask = torch.arange(hidden_states.size(1), device=hidden_states.device).unsqueeze(0) < seq_lens.unsqueeze(1)
hidden_states = hidden_states * mask.unsqueeze(-1) # Mask padding tokens
pooled_output = hidden_states.sum(dim=1) / seq_lens.unsqueeze(1)
else:
# hidden_states: [B, N, H]
hidden_states = hidden_states[:, 0]
pooled_output = self.dense(hidden_states)
pooled_output = self.activation(pooled_output)
# Average pooling over all tokens (assumes no padding)
pooled_output = hidden_states.mean(dim=1)
return pooled_output


Expand Down