Skip to content

Split large SCCs in parallel mode - #22541

Closed
ilevkivskyi wants to merge 4 commits into
python:masterfrom
ilevkivskyi:split-sccs
Closed

ilevkivskyi wants to merge 4 commits into
python:masterfrom
ilevkivskyi:split-sccs

Conversation

@ilevkivskyi

Copy link
Copy Markdown
Member

This makes libraries like torch ~2x faster (tested with -n8), and self-check is up to 10% faster (again for -n8, with fewer workers gains are smaller).

Basic idea and technical side are quite straightforward. However, details of the sharding and scheduling logic are quite tricky. This is partially because I made a mistake when measuring interface vs implementation time: right now ~30-40% of time is spent in loading+interface. Most of it is spent in semantic analysis. This makes splitting semantic analysis into two phases imperative, if we want to see full gains from this PR.

Here are some important details of the PR.

First, we need to be sure that splitting will be effective, this means all of these must be true:

  • SCC must have large absolute size to compensate for extra communication overhead.
  • We should be able to split SCC into roughly equal shards.
  • SCC should not occupy more workers than needed (compared to other work ready in queue).

Second, we should split eagerly. It is quite common that a large bottleneck SCC arrives in the queue while there is only one free worker. The other will become free soon, so if we don't split, they all will be idling.

Third (the consequence of above), we should minimize interface penalty in case where we have split an SCC, but it was actually not needed. For example, if some other worker soon unblocked a big chunk of parallelizable work. For this purpose I introduce affinity for workers processing sharded SCCs. When a worker becomes free:

  • If it previously processed a shard of some SCC, we prioritize shards of this same SCC.
  • If it didn't process any of the currently sharded SCCs, we prioritize the other SCCs in queue.
  • Only if queue is empty we schedule shards on unfamiliar workers.

Note I also did some refactoring in worker code, since serve() function grew too large. I didn't do much parameter tuning. I think we should merge this PR, then try splitting the semantic analysis, and then I will do some parameter tuning.

cc @JukkaL

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@JukkaL

JukkaL commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Here's a possible bug reported by claude:

Bug: a worker can re-analyse an SCC it already loaded from cache

What happens:

  1. An SCC X is split into shards.
  2. Once shard 0 sends back X's interface, X counts as done and its dependents become ready.
  3. A worker W with no affinity to X checks a dependent y, which loads X's modules from cache into W.
  4. W frees up while the queue is empty and a shard of X is still pending, so it takes that shard (mypy/build.py:1532).
  5. That runs the full interface path, and load_states now resets state.tree = None (mypy/build_worker/worker.py:362). X is parsed and analysed again from source, which creates new TypeInfos.
  6. y was already checked and still points at the cache-loaded TypeInfos. Any later SCC W checks that uses both X and y sees two copies of the same classes.

Reproduced. I used an instrumented copy and added sleeps to force the ordering on one worker: y → X shard 2 → z. Here z imports m0 (in X) and y. Sequential mode reports:

z.py:14: error: Argument 3 to "f" has incompatible type "Box[Sub]"; expected "Box[C]"

The sharded run drops that error (due to extra Any types), the same way three times in a row. On that worker, y's Sub base is no longer m0.C, and y.box's class is no longer m0.Box. The same project is correct with sharding off (max_shards_override=1) and under every other schedule I ran. Here it was a missing error, but split class identity can also produce spurious errors, and it lasts for the rest of the build on that worker.

The ordering needs no unusual timing in a real build. It happens whenever dependents get dispatched while the affinity workers are still checking a large SCC's implementations, which is exactly the case sharding targets.

Possible fix: don't hand a pending shard of X to a worker that has already processed anything depending on X. The coordinator can track the SCCs sent to each worker and check them with the existing is_transitive_scc_dep (mypy/build.py:1660). Leaving such a shard to an affinity worker costs only some parallelism.

@JukkaL

JukkaL commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Another potential bug reported by codex:

[P1] Repartitioning a cycle can skip a module’s body checks. The worker recomputes shard membership on every request from mutable size hints. In a two-worker incremental run with a non-ASCII --shadow-file, reparsing changed one module’s hint from byte length to character count. The same worker then checked ring6 twice and skipped ring4, where master reported an incompatible return type. Computing shard membership once from the original size hints would keep assignments stable

@ilevkivskyi

Copy link
Copy Markdown
Member Author

@JukkaL You know, I am not doing this for work, I am doing this to interact with people. Anyway, I guess my time contributing to mypy is over now. I may fork it, or maybe I will find something else.

@JukkaL

JukkaL commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

You are right, this was rude of me, and I'm sorry. I shouldn't have copy pasted coding agent output as response for your PR. I'm actually very excited about this PR, but I will be very busy for a few days at least, and I wanted to make some forward progress with code review ASAP. Instead, I should have just left comment saying that I'm busy and will get back to this later or similar instead of outsourcing my code review to an agent. I promise to stop doing this in the future, and I hope that you can reconsider your decision.

@ilevkivskyi

Copy link
Copy Markdown
Member Author

What you have done is literally like replying to someone's question with a Google link. Anyway, good luck!

@ilevkivskyi
ilevkivskyi deleted the split-sccs branch November 29, 2026 23:36
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.

2 participants