Skip to content

Fix ZeroDivisionError in SFT during record_train_metrics#3581

Merged
copybara-service[bot] merged 1 commit intomainfrom
igorts/sft-timing-fix
Apr 10, 2026
Merged

Fix ZeroDivisionError in SFT during record_train_metrics#3581
copybara-service[bot] merged 1 commit intomainfrom
igorts/sft-timing-fix

Conversation

@igorts-git
Copy link
Copy Markdown
Collaborator

@igorts-git igorts-git commented Apr 6, 2026

Description

This PR fixes a ZeroDivisionError that occurs in the MaxText MetricLogger during Tunix-based SFT runs.

The Problem

As the result of Tunix PR1289. The Tunix library's internal training loop now hard-codes the step time to zero when calling the on_train_step_end hook.
The MaxText SFT hook passes the zero value to the metric_logger.
The MetricLogger divides FLOPS and tokens values by the passed zero step_time.

The Fix

This PR modifies SFTTrainingHooks in src/maxtext/trainers/post_train/sft/hooks.py to independently track time intervals in between calls to on_train_step_end and using this time interval for reporting step time metrics.

FIXES: b/497665859

Tests

Manual invocation of SFT training with and without the fix with Tunix installed from main/HEAD.

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 6, 2026

Codecov Report

❌ Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/maxtext/trainers/post_train/sft/hooks.py 83.33% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@igorts-git igorts-git force-pushed the igorts/sft-timing-fix branch from be0941c to 3c3bfff Compare April 6, 2026 20:45
Copy link
Copy Markdown
Collaborator

@richjames0 richjames0 left a comment

Choose a reason for hiding this comment

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

lgtm

train_step: int,
train_loss: float,
step_time: float,
step_time: float = 0.0, # No longer provided. See https://github.com/google/tunix/pull/1289.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can also modify the hooks interface in Tunix and remove step_time. Can be done as a follow up.

@copybara-service copybara-service bot merged commit 58e2c7e into main Apr 10, 2026
56 checks passed
@copybara-service copybara-service bot deleted the igorts/sft-timing-fix branch April 10, 2026 20:22
copybara-service bot pushed a commit to google/tunix that referenced this pull request Apr 10, 2026
…_step_end` function.

MaxText we change this flag to optional in AI-Hypercomputer/maxtext#3581. This will allow MaxText to be compatible with older versions of Tunix. Eventually we will delete it there as well.

PiperOrigin-RevId: 897791378
copybara-service bot pushed a commit to google/tunix that referenced this pull request Apr 10, 2026
…_step_end` function.

In MaxText we changed this flag to optional in AI-Hypercomputer/maxtext#3581. This will allow MaxText to be compatible with older versions of Tunix. Eventually we will delete it there as well.

PiperOrigin-RevId: 897791378
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants