-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[v1] add init plugin #9716
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
Merged
Merged
[v1] add init plugin #9716
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,32 @@ | ||
| # Copyright 2025 the LlamaFactory team. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from .arg_parser import InputArgument, get_args | ||
| from .arg_utils import ModelClass, SampleBackend | ||
| from .data_args import DataArguments | ||
| from .model_args import ModelArguments | ||
| from .sample_args import SampleArguments | ||
| from .training_args import TrainingArguments | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "DataArguments", | ||
| "InputArgument", | ||
| "ModelArguments", | ||
| "ModelClass", | ||
| "SampleArguments", | ||
| "SampleBackend", | ||
| "TrainingArguments", | ||
| "get_args", | ||
| ] |
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,77 @@ | ||
| # Copyright 2025 the LlamaFactory team. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from abc import ABC, abstractmethod | ||
|
|
||
| from ..config import ModelArguments, SampleArguments, SampleBackend | ||
| from ..utils.types import HFModel, Processor, TorchDataset | ||
|
|
||
|
|
||
| class BaseEngine(ABC): | ||
| @abstractmethod | ||
| def __init__( | ||
| self, | ||
| args: SampleArguments, | ||
| model_args: ModelArguments, | ||
| model: HFModel = None, | ||
| processor: Processor = None, | ||
| ) -> None: | ||
| """Initialize the engine. | ||
| Args: | ||
| args: Sample arguments. | ||
| model_args: Model arguments. | ||
| model: Model. | ||
| processor: Processor. | ||
| """ | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| async def generate(self, messages): | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| async def batch_infer(self, data: TorchDataset) -> None: | ||
| pass | ||
|
|
||
|
|
||
| class HuggingFaceEngine(BaseEngine): | ||
| def __init__( | ||
| self, | ||
| args: SampleArguments, | ||
| model_args: ModelArguments, | ||
| model: HFModel, | ||
| processor: Processor, | ||
| ) -> None: | ||
| self.args = args | ||
|
|
||
|
|
||
| class BaseSampler: | ||
| def __init__( | ||
| self, | ||
| args: SampleArguments, | ||
| model_args: ModelArguments, | ||
| model: HFModel, | ||
| processor: Processor, | ||
| ) -> None: | ||
| if args.sample_backend == SampleBackend.HF: | ||
| self.engine = HuggingFaceEngine(args, model_args, model, processor) | ||
| else: | ||
| raise ValueError(f"Unknown sample backend: {args.sample_backend}") | ||
|
|
||
| async def generate(self, messages): | ||
| return await self.engine.generate(messages) | ||
|
|
||
| async def batch_infer(self, data: TorchDataset) -> None: | ||
| return await self.engine.batch_infer(data) | ||
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
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,43 @@ | ||
| # Copyright 2025 the LlamaFactory team. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| import torch | ||
|
|
||
| from ...accelerator.helper import DeviceType | ||
| from ...accelerator.interface import DistributedInterface | ||
| from ...utils.plugin import BasePlugin | ||
|
|
||
|
|
||
| class InitPlugin(BasePlugin): | ||
| def __call__(self) -> torch.device: | ||
| return super().__call__() | ||
|
|
||
|
|
||
| @InitPlugin("init_on_meta").register | ||
| def init_on_meta() -> torch.device: | ||
| return torch.device(DeviceType.META.value) | ||
|
|
||
|
|
||
| @InitPlugin("init_on_rank0").register | ||
| def init_on_rank0() -> torch.device: | ||
| if DistributedInterface().get_rank() == 0: | ||
| return torch.device(DeviceType.CPU.value) | ||
| else: | ||
| return torch.device(DeviceType.META.value) | ||
|
|
||
|
|
||
| @InitPlugin("init_on_default").register | ||
| def init_on_default() -> torch.device: | ||
| return DistributedInterface().current_accelerator |
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,35 @@ | ||
| # Copyright 2025 the LlamaFactory team. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| from ..config import InputArgument, SampleBackend, get_args | ||
| from ..core.base_sampler import BaseSampler | ||
| from ..core.model_loader import ModelLoader | ||
|
|
||
|
|
||
| def run_chat(args: InputArgument = None): | ||
| data_args, model_args, _, sample_args = get_args(args) | ||
| if sample_args.sample_backend != SampleBackend.HF: | ||
| model_args.init_plugin = {"name": "init_on_meta"} | ||
|
|
||
| model_loader = ModelLoader(model_args) | ||
| sampler = BaseSampler(sample_args, model_args, model_loader.model, model_loader.processor) | ||
| if data_args.dataset is not None: | ||
| sampler.batch_infer() | ||
| else: | ||
| sampler.generate() | ||
hiyouga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| run_chat() | ||
hiyouga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.