Skip to content

add qwen2 classification#1817

Closed
ranzhejiang wants to merge 1 commit into
huggingface:mainfrom
ranzhejiang:zhejiang/qwen2_Classification
Closed

add qwen2 classification#1817
ranzhejiang wants to merge 1 commit into
huggingface:mainfrom
ranzhejiang:zhejiang/qwen2_Classification

Conversation

@ranzhejiang
Copy link
Copy Markdown
Contributor

  • To meet the customer's demands, we need to add the classification function interface of the qwen2 on gaudi
  • you can test it with following code
import torch
import habana_frameworks.torch.core as htcore
from optimum.habana.transformers.modeling_utils import adapt_transformers_to_gaudi
from transformers import AutoTokenizer, AutoModel, AutoModelForCausalLM, AutoModelForSequenceClassification, AutoModelForTokenClassification
adapt_transformers_to_gaudi()

tokenizer = AutoTokenizer.from_pretrained("/mnt/HF_models/qwen2-7b", trust_remote_code=True)
model = AutoModelForSequenceClassification.from_pretrained("/mnt/HF_models/qwen2-7b",num_labels=3, trust_remote_code=True).bfloat16().to('hpu')
print(model)

texts = [
"Today’s exercise makes your body feel very relaxed"
]

label_to_id = {
"positive": 0,
"negative": 1,
"neutral ": 2
}

inputs = tokenizer(texts, padding=True, truncation=True, return_tensors="pt")

with torch.no_grad():
outputs = model(**inputs)

logits = outputs.logits
predictions = torch.argmax(logits, dim=-1)

for text, prediction in zip(texts, predictions):
print(f"Text: {text} -> predicted label: {list(label_to_id.keys())[prediction.item()]}")

@ranzhejiang ranzhejiang requested a review from regisss as a code owner March 5, 2025 06:59
Comment thread optimum/habana/transformers/models/qwen2/modeling_qwen2.py
@Luca-Calabria
Copy link
Copy Markdown
Contributor

LGTM!

@12010486
Copy link
Copy Markdown
Contributor

Ok, thanks @Luca-Calabria. Waiting for @regisss then

@libinta libinta added the run-test Run CI for PRs from external contributors label Mar 26, 2025
@ranzhejiang
Copy link
Copy Markdown
Contributor Author

@regisss Would you please review this PR? Thank you.

@regisss
Copy link
Copy Markdown
Collaborator

regisss commented Apr 15, 2025

@ranzhejiang There are merge conflicts to solve following #1698.
Can you also make sure your code is up-to-date with Transformers v4.49 please?

Can you also add a test for it? It can be the code snippet you posted above 🙂

@ranzhejiang
Copy link
Copy Markdown
Contributor Author

Ok, I will solve it later, and add this demo in example, thanks for review.

@ranzhejiang ranzhejiang force-pushed the zhejiang/qwen2_Classification branch from 97ea0ca to 6dedcbb Compare April 22, 2025 09:24
@Luca-Calabria
Copy link
Copy Markdown
Contributor

Luca-Calabria commented May 14, 2025

@ranzhejiang @regisss any update here?

@regisss
Copy link
Copy Markdown
Collaborator

regisss commented May 14, 2025

My comment above with

Can you also make sure your code is up-to-date with Transformers v4.49 please?
Can you also add a test for it? It can be the code snippet you posted above 🙂

should be addressed

@ranzhejiang
Copy link
Copy Markdown
Contributor Author

ranzhejiang commented May 14, 2025

Sorry for late reply owing to others. For first comment, I have made sure my code is up-to-date with Transformers v4.49, now no confilict, for second comment, I had not find proper tests for this text-classification task example before, so I will add this the code snippet into https://github.com/huggingface/optimum-habana/tree/main/examples/text-classification before 5.23
, then it will give reference example for our customers.

@IlyasMoutawwakil
Copy link
Copy Markdown
Member

IlyasMoutawwakil commented Jun 5, 2025

hi @ranzhejiang ! hope my question is not self evident, but why do we need to implement those custom task classes ? my understanding is that if they all use the same based model QwenModel, which we patch with QaudiQwenModel, the original transformers QwenModelForSequenceClassification will work just fine on gaudi with all optimization in GaudiQwenModel, as it's simply a linear layer on top. My question could be interpreted as : what's Gaudi-specific here ?

def __init__(self, config):
super().__init__(config)
self.num_labels = config.num_labels
self.model = GaudiQwen2Model(config)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my point is that the line in adapt_transformers_to_gaudi: transformers.models.qwen2.modeling_qwen2.Qwen2Model = GaudiQwen2Model already makes these qwen2 models with task heads compatible/optimized for gaudi.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you said make sense, it seems that this plan can not run work well for auto tp when I firstly give this pr, but now it can works well for single card, but I still need confirm what you said in 8-card env.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you said makes sense, I have changed my code and not using GaudiQwen2Model for init, and the new PR is in #2062

@tianyuan211 tianyuan211 mentioned this pull request Jun 17, 2025
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-test Run CI for PRs from external contributors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants