This repository was archived by the owner on Sep 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
some changes to support fine-tuning on Intel GPU #88
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5fb0415
some changes to support fine-tuning on Intel GPU
f89f7c2
update
9ec36bb
update
2f540f2
update
dcf9fb5
upate
d4be3b6
update
0e9932b
update
1b4223c
update
802d16a
update
db06df6
update
0068374
update
ece20fa
update
6dd7b10
update
27b5053
update
0aa6af7
update
8e5de6e
update
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
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
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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| import argparse | ||
| from typing import Any, Dict, Union | ||
|
|
||
| import torch | ||
| import accelerate | ||
| from accelerate.utils import is_xpu_available | ||
|
|
||
|
|
@@ -62,6 +63,14 @@ def get_accelerate_environment_variable(mode: str, config: Union[Dict[str, Any], | |
| return mode_env_vars[mode] | ||
|
|
||
|
|
||
| def convert_dtype(dtype: str) -> torch.dtype: | ||
| supported_dtypes = {"fp16": torch.float16, "bf16": torch.bfloat16, "fp32": torch.float32} | ||
| if dtype in supported_dtypes: | ||
| return supported_dtypes[dtype] | ||
| else: | ||
| raise ValueError(f"only supported torch.dtype list [{supported_dtypes.keys()}]") | ||
|
|
||
|
|
||
| def train_func(config: Dict[str, Any]): | ||
| cwd = config.get("cwd") | ||
| if cwd: | ||
|
|
@@ -79,9 +88,26 @@ def train_func(config: Dict[str, Any]): | |
| ) | ||
| else: | ||
| fsdp_plugin = None | ||
|
|
||
| log_with = "tensorboard" # only support tensorboard as tracker | ||
| output_dir = config["General"]["output_dir"] | ||
| tracking_dir = config["General"]["tracking_dir"] | ||
| accelerator = accelerate.Accelerator( | ||
| gradient_accumulation_steps=gradient_accumulation_steps, fsdp_plugin=fsdp_plugin | ||
| gradient_accumulation_steps=gradient_accumulation_steps, | ||
| fsdp_plugin=fsdp_plugin, | ||
| log_with=log_with, | ||
| project_dir=tracking_dir, | ||
| ) | ||
| epochs = config["Training"]["epochs"] | ||
| tracker_config = { | ||
| "epochs": epochs, | ||
| "learning_rate": config["Training"]["learning_rate"], | ||
| "batch_size": config["Training"]["batch_size"], | ||
| } | ||
| base_model = config["General"]["base_model"] | ||
| dataset_file = config["Dataset"]["train_file"] | ||
| accelerator.init_trackers("fine-tuning", config=tracker_config) | ||
|
|
||
| common.logger.info( | ||
| f"accelerator generate finish, accelerator device type = {accelerator.device}" | ||
| ) | ||
|
|
@@ -92,23 +118,25 @@ def train_func(config: Dict[str, Any]): | |
|
|
||
| datasets = common.dataset.Dataset.registory.get("HuggingfaceDataset")()( | ||
| config={ | ||
| "name": config["Dataset"]["train_file"], | ||
| "name": dataset_file, | ||
| "validation_file": config["Dataset"]["validation_file"], | ||
| "validation_split_percentage": config["Dataset"]["validation_split_percentage"], | ||
| } | ||
| ) | ||
|
|
||
| tokenizer = common.tokenizer.Tokenizer.registory.get("HuggingFaceTokenizer")()( | ||
| config={ | ||
| "name": config["General"]["base_model"], | ||
| "name": base_model, | ||
| "config": config["General"]["config"], | ||
| } | ||
| ) | ||
|
|
||
| model = common.model.Model.registory.get("HuggingFaceModelForCausalLM")()( | ||
| config={ | ||
| "name": config["General"]["base_model"], | ||
| "name": base_model, | ||
| "dtype": convert_dtype(config["Training"]["mixed_precision"]), | ||
| "config": config["General"]["config"], | ||
| "enable_gradient_checkpointing": config["General"]["enable_gradient_checkpointing"], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here. |
||
| "lora_config": config["General"]["lora_config"] | ||
| if config["General"].get("lora_config") | ||
| else None, | ||
|
|
@@ -125,10 +153,10 @@ def train_func(config: Dict[str, Any]): | |
|
|
||
| trainer = common.trainer.Trainer.registory.get("DefaultTrainer")( | ||
| config={ | ||
| "num_train_epochs": config["Training"]["epochs"], | ||
| "num_train_epochs": epochs, | ||
| "max_train_step": config["Training"].get("max_train_steps", None), | ||
| "log_step": 1, | ||
| "output": config["General"]["output_dir"], | ||
| "logging_steps": config["Training"].get("logging_steps", 1), | ||
| "output": output_dir, | ||
| "dataprocesser": { | ||
| "type": "GeneralProcesser", | ||
| "per_device_train_batch_size": config["Training"]["batch_size"], | ||
|
|
@@ -217,14 +245,21 @@ def main(external_config=None): | |
| "FI_PROVIDER": "tcp", | ||
| } | ||
| } | ||
|
|
||
| accelerate_env_vars = get_accelerate_environment_variable(accelerate_mode, config) | ||
| runtime_env["env_vars"].update(accelerate_env_vars) | ||
|
|
||
| if config["General"]["gpt_base_model"] is True: | ||
| runtime_env["pip"] = ["transformers==4.26.0"] | ||
|
|
||
| ray.init(runtime_env=runtime_env) | ||
| import intel_extension_for_pytorch as ipex | ||
|
|
||
| if "xpu" in ipex.__version__: | ||
| num_cpus = ( | ||
| resources_per_worker["CPU"] * num_training_workers + 1 | ||
| ) # additional 1 for head worker | ||
| ray.init(num_cpus=num_cpus, runtime_env=runtime_env) | ||
| else: | ||
| ray.init(runtime_env=runtime_env) | ||
|
|
||
| common.logger.info(f"ray available resources = {ray.available_resources()}") | ||
|
|
||
|
|
||
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
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.
same here.