-
Notifications
You must be signed in to change notification settings - Fork 18
Add HPU SFT example #10
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
splotnikv
wants to merge
4
commits into
Red-Hat-AI-Innovation-Team:main
Choose a base branch
from
splotnikv:hpu_ft_pub2
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| #!/usr/bin/env python3 | ||
| sample_name="SFT Training Example for HPU" | ||
| """ | ||
| This script demonstrates how to do SFT training on HPU | ||
| using a single-node, multi-GPU setup with training_hub. | ||
splotnikv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Example usage: | ||
| python sft_hpu_example.py \\ | ||
| --data-path /path/to/data.jsonl \\ | ||
| --ckpt-output-dir /path/to/checkpoints | ||
| """ | ||
splotnikv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import os | ||
| import sys | ||
| import time | ||
| from datetime import datetime | ||
| import argparse | ||
|
|
||
| from training_hub import sft | ||
|
|
||
|
|
||
| def main(): | ||
| #disable HPU backend autoloading | ||
| os.environ['PT_HPU_AUTOLOAD'] = '0' | ||
|
|
||
splotnikv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| parser = argparse.ArgumentParser(description=sample_name) | ||
|
|
||
| # Required parameters | ||
| parser.add_argument('--data-path', required=True, | ||
| help='Path to training data (JSONL format)') | ||
| parser.add_argument('--ckpt-output-dir', required=True, | ||
| help='Directory to save checkpoints') | ||
| parser.add_argument('--model-path', required=True, | ||
| help='Model path or HuggingFace name') | ||
|
|
||
| # Optional overrides | ||
| parser.add_argument('--num-epochs', type=int, default=3, | ||
| help='Number of epochs (default: 3)') | ||
| parser.add_argument('--effective-batch-size', type=int, default=128, | ||
| help='effective batch size') | ||
| parser.add_argument('--max-seq-len', type=int, default=16384, | ||
| help='Maximum sequence length') | ||
| parser.add_argument('--checkpoint-at-epoch', action='store_true', | ||
| help='Store checkpoint after each epoch') | ||
| parser.add_argument('--max-tokens-per-gpu', type=int, default=32768, | ||
| help='Max tokens per GPU') | ||
| parser.add_argument('--nproc-per-node', type=int, default=8, | ||
| help='Number of GPUs') | ||
| parser.add_argument('--torch-compile', action='store_true', default=False, | ||
| help='Enable torch.compile, hpu only') | ||
| parser.add_argument('--num-chunks', type=int, default=1, | ||
| help='Number of chunks to split dataset into for sequential training') | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # sample configuration | ||
| print(f"🚀 {sample_name}") | ||
| print("=" * 50) | ||
| print(f"Model: {args.model_path}") | ||
| print(f"Data: {args.data_path}") | ||
| print(f"Output: {args.ckpt_output_dir}") | ||
| print(f"GPUs: {args.nproc_per_node}") | ||
| print(f"Max tokens per GPU: {args.max_tokens_per_gpu:,}") | ||
| print() | ||
|
|
||
| # Training configuration optimized for Llama 3.1 8B Instruct | ||
| start_time = time.time() | ||
|
|
||
| try: | ||
| result = sft( | ||
splotnikv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Model and data | ||
| model_path=args.model_path, | ||
| data_path=args.data_path, | ||
| ckpt_output_dir=args.ckpt_output_dir, | ||
|
|
||
| # Training parameters | ||
| num_epochs=args.num_epochs, | ||
| effective_batch_size=args.effective_batch_size, | ||
| learning_rate=1e-5, # Lower LR for instruct model | ||
| max_seq_len=args.max_seq_len, | ||
| max_tokens_per_gpu=args.max_tokens_per_gpu, | ||
|
|
||
| # Data processing | ||
| data_output_dir="/dev/shm", # Use RAM disk for speed | ||
| warmup_steps=100, | ||
| save_samples=0, # 0 disables sample-based checkpointing, use epoch-based only | ||
|
|
||
| # Checkpointing | ||
| checkpoint_at_epoch=args.checkpoint_at_epoch, | ||
| accelerate_full_state_at_epoch=False, # Disable for smaller checkpoints (no auto-resumption) | ||
|
|
||
| # Single-node multi-GPU setup | ||
| nproc_per_node=args.nproc_per_node, | ||
| nnodes=1, | ||
| node_rank=0, | ||
| rdzv_id=101, | ||
| rdzv_endpoint="127.0.0.1:29500", | ||
|
|
||
| # HPU specific arguments | ||
| disable_flash_attn = True, | ||
| device = 'hpu', | ||
| torch_compile = args.torch_compile, | ||
| num_chunks = args.num_chunks, | ||
| ) | ||
|
|
||
| end_time = time.time() | ||
| duration = end_time - start_time | ||
|
|
||
| print("=" * 50) | ||
| print("✅ Training completed successfully!") | ||
| print(f"⏱️ Duration: {duration/3600:.2f} hours") | ||
| print(f"📁 Checkpoints: {args.ckpt_output_dir}/hf_format/") | ||
|
|
||
| except Exception as e: | ||
| end_time = time.time() | ||
| duration = end_time - start_time | ||
|
|
||
| print("=" * 50) | ||
| print(f"❌ Training failed after {duration/60:.1f} minutes") | ||
| print(f"Error: {e}") | ||
| print() | ||
| print("💡 Try reducing --max-tokens-per-gpu if you see OOM errors") | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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.