Skip to content

site-compare: the PDF book and a GPU site's CPU fallback - #140

Merged
mmcky merged 7 commits into
mainfrom
site-compare-pdf-gpu
Dec 3, 2026
Merged

mmcky merged 7 commits into
mainfrom
site-compare-pdf-gpu

Conversation

@mmcky

@mmcky mmcky commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Two checks that QuantEcon/project-monorepo#26 asks of tools/site-compare and that it lacked: the PDF book, and a GPU site's CPU fallback. This PR also takes in the nits left from #131's review.

pdf: the PDF book (_pdf/*.pdf, when both builds have one), a port of the hand validation's pdf-compare.py.

  • The outline, in order. The book is read by its outline's top two levels: parts and lectures. A section lost, added, typeset twice or moved blocks. A title that comes again (two "Notes") is matched by its occurrence. Keys keep digits, so advanced's "How to Pay for a War: Part 1/2/3" stay three sections. A book with no outline is compared whole, as one section, from its first page.
  • Each section's text, by its letters. Letters of any script count, maths italic and Greek included. Digits, spacing, line breaks and hyphenation are set aside, since pypdf's extraction drops some spaces.
  • What is set aside, in pypdf's real shapes:
    • each page's running header (the first line most pages share: the book's title);
    • its footer, the page's last line ("82 Chapter5. Title" on an even page, "5.9. Section 83" on an odd one), or the same footer glued onto the last line of text, as pypdf can give it (init).block_until_ready()46 Chapter4. Title), only straight after a character that is neither a space nor a digit;
    • code blocks' continuation notes;
    • timing lines, memory addresses and environment paths;
    • a line that is a date and time and nothing else, as nvidia-smi's first line is ("Mon Nov 9 03:15:37 2026"); a date inside prose is still prose;
    • every line of the lecture's executed output, as its HTML pages show it, unless the pages' prose has it too;
    • a chunk moved by a page break, such as a float's caption, each move paired once.
  • Lag. A section's difference is on page _pdf/<book>.pdf#<slug>: the lecture whose h1, less its chapter number, the section's title names. So it can be lag, and an entry can name it. A section with no lecture page, such as LaTeX's own Bibliography, is keyed by its title.
  • Page count. Reported only.
  • pypdf. The PDFs are read with pypdf 7.2.0, which is in the script's header (Python 3.10 or later, pypdf's floor) and in tests.yml. If two PDF books are present and pypdf is missing, the run fails (exit 3) rather than skipping the check.

gpu, with --gpu. For a site that builds on a GPU, these block, and are never lag:

  • A page whose outputs show a CPU fallback:
    • JAX's "falling back to cpu", "No GPU/TPU found", a CUDA-enabled jaxlib missing, or a backend that failed to initialise;
    • NVML failing;
    • nvidia-smi failed or not found, in bash's or zsh's words;
    • "No devices were found".
  • A page whose reference outputs show the GPU while its own show none. The GPU markers are a JAX GPU device in its current or older names, cuda:0, the nvidia-smi table, and "JAX backend: gpu", which back_prop and the status pages print.

A fallback blocks even when the reference fell back too. The shadow workflow (QuantEcon/project-monorepo#80) is to pass --gpu for the matrix's gpu leg.

The shared list gains one entry. The PDF's Bibliography is the merged bibliography typeset by LaTeX, so its months are dropped and its entries corrected there too, as on zreferences.html.

From #131's review:

  • A malformed figure address (http://[) no longer fails the run (exit 3).
  • An address (0x…) must start a token, and a size (1000x800) counts as two values.
  • In a warning's message, these are now values: glued units (kB, GHz, h, μs, s, it/s, 512M) and fractional seconds.
  • The unit pattern's alternatives are pinned by tests.
  • Left for the owner: whether a hyphenated range (1-2) is a value.

From Copilot's review (requested by the maintainer, at fba4703; fixed in 9ff14a6). Copilot made three points, each now fixed with a test:

  • A size ending a sentence in a warning ("figure is 1000x800.") is now values, as a number already was. A dot followed by digits still doesn't end it.
  • The PDF's address rule no longer starts an address straight after a digit, so a size's x ("1000x800") stays text. Copilot proposed WARN_NOISE's token-start guard, but that would keep the addresses pypdf glues to a word ("at0x7f3a2b"). So the guard is a digit, and a full address (8 hex digits or more) glued to a number still goes.
  • Output lines are set aside before difflib aligns a section's lines, so one can't anchor the alignment and split the same prose into a change. The letters are then compared again.

Calibration on intermediate. I compared release publish-2026nov30b with the monorepo's build at 9728988 (site-build run 47031722447), with --gpu and --lag newton_method:

jax (release publish-2026nov11 against the monorepo's build in the same run). This was not a matched pair, so most of its PDF rows are lag. nvidia-smi's date line had raised 11 false defects there and put dates into 5 more sections. With the date rule there are none, and nothing new appears. One more false row came from a footer glued onto a page's last line (newtons_method, whose page break moved); stripping a glued footer removes it, and changes nothing else on jax, intermediate or dp. Intermediate's calibration had missed it only because both its builds ran on a Thursday in November.

dp builds no PDF, and its comparison is unchanged: 0 defects with #137's lists.

Tests. tools/tests/test_site_compare.py has 113 new tests (45 functions, the rest parametrised cases), on PDFs a small writer in the test file makes in pypdf's real shapes:

  • The outline: a section lost, added, typeset twice or moved; a title that comes twice, compared by occurrence (a change in the second is found); only the top two levels as sections; two sections starting on one page; no outline at all, read from its first page.
  • The text:
    • changed prose on its lecture's page, as lag too;
    • a paragraph lost or added;
    • non-ASCII, maths italic and Greek letters;
    • same-titled numbered sections;
    • a lecture typeset twice, or titled with a year, keeping its lecture page.
  • What is set aside:
    • digits and line breaks, however difflib aligns the lines;
    • timings (and it/s), addresses, paths under each environment prefix, and run-date lines (every weekday and month, spaceless, without seconds);
    • footers glued to a page's last line, even and odd;
    • a page break more, with its header, footer and both continuation notes;
    • a moved chunk, paired once each way, and a replaced one that is not a move.
  • What isn't set aside:
    • prose that looks like noise ("Chapter 6 of the book", "has elapsed", "Wall time:", "continued from previous page", a date inside a sentence);
    • a page's last line not in a footer's shape, including "3 Chapter 5 …", "1.2. We use Python 3.10" and prose ending in a number after a space;
    • a glued footer's shape inside a page;
    • a footer-shaped heading inside a page;
    • the book's title as prose, or as the start of a first line;
    • a first line shared by fewer than half the pages;
    • letters glued to a memory address.
  • Executed output:
    • set aside, including a line inside a longer output;
    • but not when either build's page has it as prose, or when it has fewer than eight letters (seven is too few).
  • Also: the page count, the Bibliography's entry, and no pypdf.
  • GPU: each fallback shape and each marker, blocking and never lag; each fallback line reported on its own, and one printed in several cells giving one line.
  • Also in the text: a change of case alone is a change.
  • site-compare: the last review's nits (canonical check, site address spellings, redirect scripts, warning numbers) #131's nits.
  • Copilot's three points: a size ending a warning's sentence; a size in a PDF line, spaced and glued; a full address glued to a number; an output line that would anchor the alignment.

Mutations, run with PYTHONDONTWRITEBYTECODE=1, each mutant first checked to parse. The last round ran 113 across five sets (mine and the reviews'; two appear in two sets), and 107 fail a test. The other six:

  • two let the JAX backend marker match more widely, which on real outputs changes nothing;
  • two change which build's h1 names a lecture's page for lag, or which of two same-titled pages is used;
  • two are the same equivalent mutant: reading the footer from the page's last line rather than the body's.

For Copilot's fixes, the delta review ran 10 mutations and caught 8 (mine ran 7 and caught 5). Two survive:

  • The size's lookahead without its dot-digit clause. It is more lenient only on a token like "2x3.5", which no warning prints.
  • The second early return in section_changes. 9ff14a6's message calls it a shortcut that gives the same empty result, but that is wrong. With the output lines gone, a repeated prose line can anchor the alignment. For example, ["wage", "iswage"] against ["wageis", "wage", <an output line>] gives no change with the return, and a change without it. No test covers it yet; it is the first item below.

The full suite, with CI's packages and pypdf, on the branch with main merged in, gives 864 passed, 1 skipped (the skip is a case-insensitive file-system test on macOS).

Left for a follow-up (the last reviews' nits; none changes a letter read from the four real PDFs):

  • A test for section_changes' second early return (the case above).
  • A PDF test for an address glued to a word with fewer than 8 hex digits ("at0x7f3a2b"), the reason the address guard is a digit.
  • The address rule's 8-hex branch also strips a word starting with a–f letters glued straight after a size whose first dimension ends in 0 ("1000x800faded"), while "1024x768faded" keeps its x. matplotlib writes "with" after a size, so this is recorded, not fixed.
  • The glued-footer rule's limits have no test: its digit exclusion, the odd and even shapes' exact parts, and its $ anchor. The same goes for the date rule's weekday, month and year shapes.
  • A footer glued straight after a whole number with a space before it (x = 45 + 46 Chapter4. Title) isn't stripped. It would block, never hide.
  • A page's last line with a three-part version number ending in a dot (Python 3.10.1. Then it said 5) loses its tail. None occurs in the real PDFs. Requiring a glued footer's page number to be the page's own printed number would close both gaps.
  • The Bibliography and Index pages' own running footers aren't stripped (their section is on the shared list).
  • Two tests could be clearer: the case-only test's expected lines, and the glued-footer test's reference lines.

Not compared.

  • The title page and contents, before the first section: the title page carries the build's date. (A book with no outline is read whole, so its date shows and it blocks; Sphinx always writes an outline.)
  • A number or punctuation that changes only in the PDF (the HTML text check compares numbers in prose).
  • A PDF line that matches the lecture's executed output in either build, unless it is also prose.
  • A traceback that shows only in a lagging lecture's PDF section would be lag, though a strict PDF build (-W) mostly prevents one.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG

mmcky and others added 5 commits December 3, 2026 23:00
…nits)

Two checks QuantEcon/project-monorepo#26 names that the tool lacked:

- pdf: the PDF book (_pdf/*.pdf in both builds), by its outline's parts
  and lectures. A section only in one build blocks. Each section's text is
  compared by its letters alone, less running headers, timings, environment
  paths and every line of the lecture's executed output on its HTML page;
  a chunk moved by a page break is no change. A section's difference is on
  _pdf/<book>.pdf#<slug>, the lecture its title names (from the pages'
  h1), so it can be lag; the page count is reported. Reads PDFs with pypdf
  (7.2.0, in the script's header and in tests.yml); without it, a run with
  two PDF books fails (exit 3) rather than skip the check.
- gpu, with --gpu: a page whose outputs show a CPU fallback (JAX's
  "falling back to cpu", a failed nvidia-smi), or whose reference outputs
  show the GPU and its own none, blocks, and is never lag.

The shared list gains the PDF bibliography's entry (the merged
bibliography's months and corrections, typeset in LaTeX's Bibliography).

From #131's review: a malformed figure address is no failure; an address
starts a token and a size (1000x800) is two values; glued units (kB, GHz,
h, us, s, it/s, M) and fractional seconds are values in a warning.

Intermediate's calibration (publish-2026nov30b against the monorepo's
build at 9728988) with --gpu: the PDF differs only in newton_method (lag)
and the bibliography (expected); output-only sections are set aside; no
fallback. dp builds no PDF, and its comparison is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG
…ders, Unicode)

- Sections are keyed by their title's letters and digits, and a title
  that comes again by its occurrence: advanced's "How to Pay for a War:
  Part 1/2/3" were one key, so Parts 2 and 3 were never compared. The
  outline is compared in order, so a section lost, added, typeset twice or
  moved blocks. A book with no outline is one section.
- The noise rules follow pypdf's real output: the running header is the
  first line most pages share (the book's title), the footer a page's last
  line in its real shapes ("82 Chapter5. Title", "5.9. Section 83"), the
  continuation notes as pypdf gives them; timings anchored, memory
  addresses set aside. The old rules matched only prose ("Chapter 6 of the
  book", "has elapsed").
- Letters of any script: maths italic and Greek are compared.
- Executed output is set aside only where it isn't also the page's prose;
  a moved chunk pairs with one deletion each.
- A page's h1 loses its chapter number before it names a section.
- GPU: NVML failure, zsh's "command not found", older JAX device names.
- requires-python 3.10 (pypdf 7.2.0's floor).
- Tests on pypdf's real shapes, and one for each guard: 39 mutations, all
  caught. Intermediate's calibration is unchanged: newton_method lag, the
  Bibliography expected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG
- Tests for what the review's mutations got past:
  - a change in the second of two same-titled sections, and a lecture
    typeset twice keeping its lecture page;
  - a title starting with a year ("1993 Prospects") keeping its page;
  - maths italic and Greek letters;
  - a chunk replaced in one place and inserted in another (not a move);
  - a page's last line that isn't in a footer's shape;
  - a heading in a footer's shape inside a page;
  - the book's title as prose;
  - a first line that starts with the title;
  - a first line on fewer than half the pages;
  - "Wall time:" and "continued from previous page" inside prose;
  - letters glued to a memory address;
  - a line that is prose in either build's page;
  - an output line of exactly eight letters;
  - a book with no outline read from its first page.
- GPU: back_prop's "JAX backend: gpu" is a marker too.
- 199 tests in test_site_compare.py. 66 mutations (40 of mine, each first
  checked to parse, and the review's 26): 65 caught, and one equivalent
  (footer read from page[-1] rather than body[-1], the same line whenever
  body isn't empty). Intermediate's calibration and dp's comparison are
  unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG
…ards pinned)

- A PDF line that is a date and time and nothing else, as nvidia-smi's
  first line is ("Mon Nov 9 03:15:37 2026"), is set aside: it has too few
  letters for the output rule, so it was compared as prose and blocked on
  every GPU site's run date. On jax's real pair (release publish-2026nov11
  against the monorepo's build) it raised 11 false defects and dated lines
  in 5 more sections; now none. A date inside prose is still prose.
- Tests for the guards real data relies on:
  - an output line inside a multi-line output;
  - only the outline's top two levels being sections;
  - two sections starting on one page;
  - a chunk deleted once and inserted twice;
  - the same letters broken differently (the early return; without it
    difflib can report false lines);
  - a fallback printed in several cells;
  - environment paths under each prefix;
  - "it/s" and both continuation notes;
  - a seven-letter output line;
  - last lines that aren't footers ("3 Chapter 5 …", "1.2. We use Python
    3.10");
  - the GPU sub-alternatives.
  The maths-letters test now goes through letters().
- "JAX backend: gpu" is printed by the status pages as well as back_prop.
- 220 tests. 99 mutations across five sets, each checked to parse: 93
  caught. The other six only make the GPU marker broader or the lag lookup
  choose among same-titled pages, or are equivalent (the footer read from
  page[-1]). Intermediate's calibration and dp's comparison are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG
… last line)

- pypdf can glue a page's running footer onto its last line of text
  ("init).block_until_ready()46 Chapter4. Newton'sMethodviaJAX"). That
  footer wasn't stripped, so where a page break moved between two builds a
  section with no change of text blocked: jax's newtons_method did, against
  its release. A footer-shaped tail is now stripped from a page's last line,
  only straight after a character that is neither a space nor a digit, so
  prose ending in a number is still prose. On jax's real pair the
  newtons_method row goes and nothing else changes; intermediate's
  calibration and dp's comparison are unchanged.
- Tests:
  - glued even and odd footers;
  - a glued footer's shape inside a page, and prose ending in a number
    after a space, both still text;
  - PDF_DATE over every weekday and month, spaceless and without seconds,
    and lines that aren't dates;
  - a run-date case whose letters differ (the Thursday case had none);
  - each fallback line reported on its own;
  - a change of case alone.
  The prose-noise test's decorator sits on it again.
- 227 tests. 113 mutations across five sets, each checked to parse: 107
  caught. The six others widen the GPU marker, choose among same-titled
  pages' h1s for lag, or are the equivalent footer read from page[-1]
  (twice).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG
README.md's tools table conflicted on one row: this branch extends the
site-compare row (the PDF book, --gpu), and #137 added the
consumer-expected row below it, plus a line in the tree. The resolution
keeps both: this branch's site-compare row, then #137's lines unchanged.
Full tools suite on the merged tree: 859 passed, 1 skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG

Copilot AI 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.

🟡 Changes recommended

Active findings in tools/site-compare show that output filtering and size/address normalization can produce false blocking PDF and warning differences from output placement, line wrapping, or numeric-only changes.

3 open issues
What changed in this PR

Extends site-compare with PDF book comparison and optional GPU fallback detection for validating monorepo site builds.

Changes:

  • Compares PDF outlines and section text, with layout and execution noise excluded.
  • Detects CPU fallback with --gpu and improves warning normalization and malformed URL handling.
  • Adds regression tests, PDF dependencies, documentation, and a bibliography exception.
File Description
tools/​tests/​test_site_compare.py Adds PDF, GPU, and normalization regression tests.
tools/​site-compare Implements new checks and normalization fixes.
sites/​compare-expected.yml Allows expected PDF bibliography differences.
README.md Documents PDF and GPU checks.
.github/​workflows/​tests.yml Adds pypdf to test dependencies.

Review effort: Balanced (adaptive)

Comment thread tools/site-compare Outdated
# size, factor, frequency, time or rate unit (512MB, 2.5x, 3GHz, 1.2h, it/s),
# which goes with the number. An address starts a token, and a size
# (1000x800) is two values.
WARN_NOISE = re.compile(OUTPUT_NOISE.pattern + r"|(?<![\w.])0x[0-9a-fA-F]+|(?<![\w.])\d+x\d+(?![\w.])|"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 9ff14a6: the size alternative's lookahead is now (?!\w|.\d), as for a number, so a size may end a sentence ("figure is 1000x800.") and normalises; a dot followed by digits still doesn't end it. Test: the warnings parametrize gains "figure is 1000x800." against "figure is 1024x768.".

Comment thread tools/site-compare Outdated
# is ("Mon Nov 9 03:15:37 2026"): too few letters for the output rule.
PDF_DATE = re.compile(r"^\s*(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"
r"\s*\d{1,2}\s*\d{1,2}:\d\d(?::\d\d)?\s*\d{4}\s*$")
PDF_ADDRESS = re.compile(r"0x[0-9a-fA-F]+")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 9ff14a6, with a different guard: PDF_ADDRESS is now 0x[0-9a-fA-F]{8,}|(?<!\d)0x[0-9a-fA-F]+. WARN_NOISE's token-start guard would stop stripping the addresses pypdf glues to a word ("object at0x7f3a2b" loses its spaces), so the guard is a preceding digit: a size's x (1000x800) stays text, an address after a letter or space still goes, and a full address glued to a number (8 hex digits or more) still goes. Tests: sizes in a PDF line (spaced and glued) and a full address glued to a number give no PDF difference.

Comment thread tools/site-compare
Comment on lines +538 to +542
if "".join(x) == "".join(y):
return []

def output(line):
return len(line) >= 8 and line not in prose and any(line in o for o in outputs)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 9ff14a6: section_changes now sets output lines aside before difflib aligns the lines, then compares the letters again, so an output line can't anchor the alignment. Test: your example gives no change, and a real prose change around the same output line is still reported.

…e's x in the PDF, output lines set aside before alignment)

- WARN_NOISE: a size may end a sentence ("figure is 1000x800."), as a
  number may; a dot followed by digits still doesn't end it, as for a
  number.
- PDF_ADDRESS: an address no longer starts straight after a digit, where
  "0x800" is the end of a size (1000x800) and its x is text; a full
  address (8 hex digits or more) glued to a number still goes. Copilot's
  token-start guard would keep the addresses pypdf glues to a word
  ("at0x7f3a2b"), so the guard is a digit, not a word character.
- section_changes: output lines are set aside before difflib aligns the
  lines, so one can't anchor the alignment and split the same prose into
  a change; the letters are compared again after that.

Tests: 5 new cases (232 in the file). Mutations: 7, parse-checked; 5
caught; 2 survive by design: the size's lookahead without its ".digit"
clause (kept to match the number rule, as Copilot asked) and the second
early return (a shortcut: the loop gives the same empty result).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WT8bMZxoF4m8xKRb8BcHnG
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

☁️ Cloudflare Preview Ready! (intro)

Preview URL: https://pr-140.qe-preview-intro.pages.dev

Commit: 9ff14a6


Build Info

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

☁️ Cloudflare Preview Ready! (jax)

Preview URL: https://pr-140.qe-preview-jax.pages.dev

Commit: 9ff14a6


Build Info

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

☁️ Cloudflare Preview Ready! (advanced)

Preview URL: https://pr-140.qe-preview-advanced.pages.dev

Commit: 9ff14a6


Build Info

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

☁️ Cloudflare Preview Ready! (programming)

Preview URL: https://pr-140.qe-preview-programming.pages.dev

Commit: 9ff14a6


Build Info

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

☁️ Cloudflare Preview Ready! (dp)

Preview URL: https://pr-140.qe-preview-dp.pages.dev

Commit: 9ff14a6


Build Info

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

☁️ Cloudflare Preview Ready! (intermediate)

Preview URL: https://pr-140.qe-preview-intermediate.pages.dev

Commit: 9ff14a6


Build Info

@mmcky
mmcky merged commit a1746c5 into main Dec 3, 2026
24 checks passed
@mmcky
mmcky deleted the site-compare-pdf-gpu branch December 3, 2026 22:12
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