-
Notifications
You must be signed in to change notification settings - Fork 211
feat: add local Qwen3-Omni API and fine-tune regression #318
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
Open
glennko
wants to merge
5
commits into
main
Choose a base branch
from
glenn/qwen3omni
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0c28182
feat: add local Qwen3-Omni API and fine-tune regression
glennko 74ee61d
Merge branch 'main' into glenn/qwen3omni
MarcosRiveraMartinez 93c250c
feat(datasets): add SIFT-50M helpers and Qwen3-Omni scaffold
glennko 3d3e2e6
fix(qwen3-omni): use multimodal loader and correct model
glennko cdd5de9
test(qwen3-omni): update API tests for multimodal loader
glennko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -148,3 +148,6 @@ dmypy.json | |
| .cache/ | ||
|
|
||
| .DS_Store | ||
|
|
||
| AGENTS.md | ||
| CLAUDE.md | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| from datetime import datetime | ||
| from typing import Dict, List, Optional, Sequence | ||
|
|
||
| import torch | ||
| from transformers import AutoModelForCausalLM, AutoTokenizer | ||
|
|
||
| from xturing.model_apis.base import TextGenerationAPI | ||
|
|
||
|
|
||
| class Qwen3OmniTextGenerationAPI(TextGenerationAPI): | ||
| """Text generation API wrapper for running Qwen3-Omni locally via Hugging Face.""" | ||
|
|
||
| config_name = "qwen3_omni" | ||
|
|
||
| def __init__( | ||
| self, | ||
| model_name_or_path: str = "Qwen/Qwen2.5-Omni", | ||
| device: Optional[str] = None, | ||
| tokenizer_kwargs: Optional[Dict] = None, | ||
| model_kwargs: Optional[Dict] = None, | ||
| default_generate_kwargs: Optional[Dict] = None, | ||
| ): | ||
| super().__init__( | ||
| engine=model_name_or_path, | ||
| api_key=None, | ||
| request_batch_size=1, | ||
| ) | ||
| tokenizer_kwargs = tokenizer_kwargs or {} | ||
| model_kwargs = model_kwargs or {} | ||
| self.default_generate_kwargs = default_generate_kwargs or {} | ||
|
|
||
| self.tokenizer = AutoTokenizer.from_pretrained( | ||
| model_name_or_path, trust_remote_code=True, **tokenizer_kwargs | ||
| ) | ||
| self.model = AutoModelForCausalLM.from_pretrained( | ||
| model_name_or_path, trust_remote_code=True, **model_kwargs | ||
| ) | ||
|
|
||
| if device is None: | ||
| device = "cuda" if torch.cuda.is_available() else "cpu" | ||
| self.device = torch.device(device) | ||
| self.model.to(self.device) | ||
| if self.tokenizer.pad_token is None: | ||
| self.tokenizer.pad_token = self.tokenizer.eos_token | ||
| self.tokenizer.pad_token_id = self.tokenizer.eos_token_id | ||
|
|
||
| def _trim_stop_sequences( | ||
| self, text: str, stop_sequences: Optional[Sequence[str]] | ||
| ) -> str: | ||
| if not stop_sequences: | ||
| return text | ||
| cut_index = len(text) | ||
| for stop in stop_sequences: | ||
| if not stop: | ||
| continue | ||
| idx = text.find(stop) | ||
| if idx != -1 and idx < cut_index: | ||
| cut_index = idx | ||
| return text[:cut_index].rstrip() | ||
|
|
||
| def _generate_single( | ||
| self, | ||
| prompt: str, | ||
| max_tokens: int, | ||
| temperature: float, | ||
| top_p: Optional[float], | ||
| stop_sequences: Optional[Sequence[str]], | ||
| n: int, | ||
| generation_overrides: Dict, | ||
| ) -> List[Dict[str, str]]: | ||
| inputs = self.tokenizer( | ||
| prompt, | ||
| return_tensors="pt", | ||
| ) | ||
| inputs = {k: v.to(self.device) for k, v in inputs.items()} | ||
| do_sample = temperature is not None and temperature > 0 | ||
| generate_kwargs = { | ||
| "max_new_tokens": max_tokens, | ||
| "do_sample": do_sample, | ||
| "num_return_sequences": n, | ||
| "eos_token_id": self.tokenizer.eos_token_id, | ||
| "pad_token_id": self.tokenizer.pad_token_id, | ||
| } | ||
| if temperature is not None: | ||
| generate_kwargs["temperature"] = temperature | ||
| if top_p is not None: | ||
| generate_kwargs["top_p"] = top_p | ||
| generate_kwargs.update(self.default_generate_kwargs) | ||
| generate_kwargs.update(generation_overrides) | ||
| outputs = self.model.generate(**inputs, **generate_kwargs) | ||
| if n == 1: | ||
| outputs = outputs.unsqueeze(0) if outputs.dim() == 1 else outputs | ||
| generated_sequences: List[Dict[str, str]] = [] | ||
| prompt_length = inputs["input_ids"].shape[-1] | ||
| for sequence in outputs: | ||
| completion_tokens = sequence[prompt_length:] | ||
| text = self.tokenizer.decode( | ||
| completion_tokens, | ||
| skip_special_tokens=True, | ||
| ).strip() | ||
| text = self._trim_stop_sequences(text, stop_sequences) | ||
| generated_sequences.append( | ||
| { | ||
| "text": text, | ||
| "finish_reason": "stop", | ||
| } | ||
| ) | ||
| return generated_sequences | ||
|
|
||
| def generate_text( | ||
| self, | ||
| prompts, | ||
| max_tokens, | ||
| temperature, | ||
| top_p=None, | ||
| frequency_penalty=None, | ||
| presence_penalty=None, | ||
| stop_sequences=None, | ||
| logprobs=None, | ||
| n=1, | ||
| best_of=1, | ||
| retries=0, | ||
| **generation_overrides, | ||
| ): | ||
| if not isinstance(prompts, list): | ||
| prompts = [prompts] | ||
|
|
||
| results = [] | ||
| for prompt in prompts: | ||
| choices = self._generate_single( | ||
| prompt=prompt, | ||
| max_tokens=max_tokens, | ||
| temperature=temperature, | ||
| top_p=top_p, | ||
| stop_sequences=stop_sequences, | ||
| n=n, | ||
| generation_overrides=generation_overrides, | ||
| ) | ||
| data = { | ||
| "prompt": prompt, | ||
| "response": {"choices": choices}, | ||
| "created_at": str(datetime.now()), | ||
| } | ||
| results.append(data) | ||
|
|
||
| return results | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't look correct. I don't think AutoModelForCausalLM is supported for this model. Just try AutoModelForMultimodalLM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made the suggested changes.