Skip to content

Add SFT trainer and sft task #284

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
wants to merge 44 commits into
base: main
Choose a base branch
from
Open

Add SFT trainer and sft task #284

wants to merge 44 commits into from

Conversation

jialei777
Copy link
Collaborator

@jialei777 jialei777 commented Jun 5, 2025

Enable SFT, specifically:

  • add sfttrainer and the associated unit test
  • update dataset a bit for sft task
  • update readme and e2e test

GSM8k training:

[2025-06-06 16:57:39,483][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Num replicas: 1
[2025-06-06 16:57:39,484][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Starting training
[2025-06-06 16:57:39,484][torchprime.torch_xla_models.trainer.base_trainer][INFO] -     Max step: 100
[2025-06-06 16:57:39,484][torchprime.torch_xla_models.trainer.base_trainer][INFO] -     Global batch size: 64
[2025-06-06 16:59:53,697][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.0000, step: 0, loss: 1.2016, lr: 4.00e-06, trace time: 132096.80 ms
[2025-06-06 17:02:07,179][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.0431, step: 5, loss: 1.5330, lr: 2.40e-05, trace time: 1018.20 ms
[2025-06-06 17:02:12,027][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.0862, step: 10, loss: 3.3264, lr: 3.96e-05, trace time: 734.19 ms
[2025-06-06 17:02:16,878][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.1293, step: 15, loss: 1.5569, lr: 3.73e-05, trace time: 1108.31 ms
[2025-06-06 17:02:21,727][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.1724, step: 20, loss: 0.7388, lr: 3.51e-05, trace time: 740.09 ms
[2025-06-06 17:02:26,580][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.2155, step: 25, loss: 0.6763, lr: 3.29e-05, trace time: 736.51 ms
[2025-06-06 17:02:31,460][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.2586, step: 30, loss: 0.5993, lr: 3.07e-05, trace time: 735.40 ms
[2025-06-06 17:02:36,287][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.3017, step: 35, loss: 0.5973, lr: 2.84e-05, trace time: 735.46 ms
[2025-06-06 17:02:41,140][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.3448, step: 40, loss: 0.5832, lr: 2.62e-05, trace time: 735.81 ms
[2025-06-06 17:02:45,992][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.3879, step: 45, loss: 0.5754, lr: 2.40e-05, trace time: 739.57 ms
[2025-06-06 17:02:50,846][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.4310, step: 50, loss: 0.5350, lr: 2.18e-05, trace time: 736.22 ms
[2025-06-06 17:02:55,701][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.4741, step: 55, loss: 0.5491, lr: 1.96e-05, trace time: 1111.88 ms
[2025-06-06 17:03:00,554][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.5172, step: 60, loss: 0.4939, lr: 1.73e-05, trace time: 738.59 ms
[2025-06-06 17:03:05,407][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.5603, step: 65, loss: 0.5829, lr: 1.51e-05, trace time: 736.64 ms
[2025-06-06 17:03:10,261][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.6034, step: 70, loss: 0.5455, lr: 1.29e-05, trace time: 741.68 ms
[2025-06-06 17:03:15,116][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.6466, step: 75, loss: 0.5600, lr: 1.07e-05, trace time: 746.14 ms
[2025-06-06 17:03:19,972][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.6897, step: 80, loss: 0.5516, lr: 8.44e-06, trace time: 741.00 ms
[2025-06-06 17:03:24,826][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.7328, step: 85, loss: 0.5162, lr: 6.22e-06, trace time: 742.01 ms
[2025-06-06 17:03:29,685][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.7759, step: 90, loss: 0.5052, lr: 4.00e-06, trace time: 742.07 ms
[2025-06-06 17:03:34,540][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Epoch: 0.8190, step: 95, loss: 0.5012, lr: 1.78e-06, trace time: 740.57 ms
[2025-06-06 17:03:38,423][torchprime.torch_xla_models.trainer.base_trainer][INFO] - Finished training run
[2025-06-06 17:03:38,424][torchprime.torch_xla_models.trainer.base_trainer][INFO] - ***** train metrics *****

@jialei777 jialei777 marked this pull request as ready for review June 6, 2025 04:41
@tengyifei
Copy link
Collaborator

Hmm, very strange. Latest SFT run (https://github.com/AI-Hypercomputer/torchprime/actions/runs/15501248395/job/43649488239?pr=284) is taking forever to finish. Looks like it is compiling many graphs. In the past I've seen this happening when there are unexpected transfers from the TPU to the CPU (e.g. printing or calling .cpu()).

@jialei777
Copy link
Collaborator Author

jialei777 commented Jun 7, 2025

@tengyifei Error is due to the export model in the end (so align with transfers from the TPU to the CPU as you mentioned). I don't know why, seems it worked with tpu vm. Any idea on how to debug this?

@yaoshiang
Copy link
Collaborator

I get a lot of notifications for this PR and it's also getting large. Can you work on smaller PRs that chain towards a bigger change, and, ask for more intermediate reviews? with unit tests, it's pretty easy to introduce small PRs that are 100 lines of code. https://google.github.io/eng-practices/review/developer/small-cls.html

@yaoshiang
Copy link
Collaborator

Please create a PR when ready for review, and make the PRs small - 100 lines of code is a good target. A PR that lives for days with 18 commits (not addressing comments) is getting too large.

@yaoshiang yaoshiang closed this Jun 10, 2025
@jialei777 jialei777 reopened this Jun 10, 2025
@jialei777 jialei777 marked this pull request as draft June 10, 2025 03:05
@jialei777 jialei777 marked this pull request as ready for review June 10, 2025 21:55
@jialei777 jialei777 linked an issue Jun 11, 2025 that may be closed by this pull request
@@ -66,9 +69,13 @@ def analyze_step_duration_from_pb(xspace: XSpace) -> float:

# Confirm we have exactly one unique event name
if len(unique_names) > 1:
raise ValueError(f"Ambiguous event names found in XSpace: {unique_names}")
logger.warning(
f"Multiple event names found in XSpace: {unique_names}.\n"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this workaround still required after #302?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, sometime it still recompile. I assume it is the same root cause as #260. Do we want to just let it fail?

xm.mark_step()
xm.wait_device_ops()

# Ensure a torch.distributed PG exists (once per host)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we link to the SPMD distributed checkpoint docs that mentions such a requirement?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it has nothing to do with SPMD, but because we are using torch.distributed.checkpoint for saving then we need to have a torch.distributed process group. That is my understanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add SFT support
3 participants