Skip to content

Studio: keep fine-tuning jobs in order when a batch starts ahead of them - #16832

Merged
danielhanchen merged 6 commits into
unslothai:mainfrom
NilayYadav:fine-tuning-pagination-skip
Nov 6, 2026
Merged

danielhanchen merged 6 commits into
unslothai:mainfrom
NilayYadav:fine-tuning-pagination-skip

Conversation

@NilayYadav

@NilayYadav NilayYadav commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Listing fine-tuning jobs page by page through the OpenAI-compatible API could skip a job. When two compatible jobs started together as a Multi-LoRA batch while an older job kept waiting, the waiting job later started with a newer start time and jumped ahead of the batch in the history. A client that had already paged past the batch never saw it.

Finished runs are now listed by the time the job was queued, falling back to the start time for runs that never went through the queue or predate it. The history keeps the order the jobs were submitted in, however the batches came out.

Fixes #16720

Follow-up on this branch: queue, live and persisted jobs are ordered in one pass by creation time, with jobs created in the same second ordered by when they were queued, then by job ID. Before, those jobs could swap places when a batch moved from running to history, and a client could skip one. History is still read lazily: a short page reads only the rows it needs, even after a long backlog. Regression tests for these cases come from #16835.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T08:24:59.850084Z 2d1fe47 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 57120b9096

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 5, 2026
@danielhanchen danielhanchen self-assigned this Nov 6, 2026
…azily

Order queue, live and persisted jobs in one heap keyed by creation time,
then queue order and public id, so jobs created in the same second keep
their place when a batch moves from live to history. Queue entries share
the heap, so a short page reads only the history rows it needs.

Regression tests ported from unslothai#16835.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 11ae4ab505

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

# place from queue to history. History is newest start first and no job starts before it is
# created, so a buffered row created after the current row's start is final.
def item(created_at: int, public_id: str, seq: int, row: dict[str, Any], is_run: bool):
return (-created_at, -_ALIAS_ORDER.get(public_id, -1), public_id, seq), row, is_run

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Persist the same-second ordering key across restarts

For queued jobs created in the same second, this sort key changes across a Studio restart: _save_alias and _load_aliases persist and restore created_at, but not _ALIAS_ORDER, so the jobs switch from queue order to random-public-ID order. A completed pair can therefore change from [newer, older] to [older, newer]; a client that fetched newer before the restart then gets an empty page for after=newer, silently missing the older persisted run. Persist and reload the tie-breaker, or use a durable ordering key consistently before and after restart.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed in 2d1fe47: same-second ties now break on the public job id alone, which is the same before and after a restart, so no in-memory order is needed.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 2d1fe47407

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit b0072c7 into unslothai:main Nov 6, 2026
danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 6, 2026
Same-second jobs now list by job id (unslothai#16832), so the cursor tests and the
queue order test expect that order instead of queue order.
danielhanchen pushed a commit that referenced this pull request Nov 7, 2026
…restarts (#16835)

Follow-up to #16720, #16832 and #16823.

When several fine-tuning jobs are created within the same second (a script submitting jobs in a loop), `GET /v1/fine_tuning/jobs` broke the `created_at` tie by the random public job id. So `jobs.list(limit=1)` could return an older job instead of the one just submitted, and the order among those jobs had nothing to do with submission order. An earlier revision of this branch fixed that with an in-process counter. That counter was lost when Studio restarted, so a client paging with `after=` across a restart could skip a job (three same-second jobs, page 1 before a restart, page 2 after: `ftjob_qa` never returned).

Jobs now get a nanosecond creation stamp when they are queued (kept strictly increasing, since Windows clocks can tick only every ~16 ms). It is saved with the job id mapping in the alias table, in a new nullable `created_ns` column, and loaded back on start. Same-second jobs are ordered by that stamp, then by public id, so a job keeps its place from queued to running to history, and across restarts. Older alias tables gain the column the first time they are opened. Jobs the API starts directly, without queueing, get a stamp too: it is saved under their usual public id, and their `created_at` still comes from the run. Rows without a stamp (runs started from the Studio UI, jobs from older builds) keep the public id order. Older builds name their columns explicitly when they read and write this table, so a downgrade still works.

Verification:

- `test_a_job_batched_past_keeps_its_place_when_it_starts_between_pages` now also runs across a simulated restart and asserts submission order when jobs share a second. On main, the 3 same-second cells fail and the 3 others pass. Here, all 6 pass.
- New `test_an_alias_table_from_an_older_build_loads_and_gains_the_ns_column`: an alias table from an older build loads, gains the column, and saves new stamps.
- New `test_jobs_created_within_one_clock_tick_list_newest_submitted_first`: with a frozen clock, jobs still list newest submitted first (fails without the strictly increasing stamp).
- New `test_a_job_started_directly_lists_ahead_of_an_older_same_second_job`: a queued job is cancelled and a replacement starts directly in the same second. The replacement lists first, before and after a restart.
- Three tests computed their expected order from the random job id tie break. They now expect newest submitted first.
- `test_openai_fine_tuning.py`: 125 passed. `test_training_api_snippet_contract.py` + `tests/multi_account` (fine-tuning/training selection): 129 passed.
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.

Studio: fine-tuning pagination can skip a job when Multi-LoRA batches start out of order

2 participants