Skip to content

GH-3702: Fix the HyperbolicDistance Lorentz product - #3703

Open
Afloat16 wants to merge 3 commits into
flairNLP:masterfrom
Afloat16:fix-hyperbolic-lorentz-product
Open

Afloat16 wants to merge 3 commits into
flairNLP:masterfrom
Afloat16:fix-hyperbolic-lorentz-product

Conversation

@Afloat16

@Afloat16 Afloat16 commented Oct 1, 2026

Copy link
Copy Markdown

Fixes #3702

Change

HyperbolicDistance creates the time coordinate separately from the input spatial embeddings. Its Lorentz product must therefore use every input feature for the spatial dot product. This change removes the [:, 1:] slices and corrects the forward docstring. The existing arccosh numerical floor is preserved.

Regression coverage

The new native pytest cases cover:

  • An explicit projected Lorentz-product reference in 1, 2, and 7 dimensions, with float32 and float64
  • Identical nonzero embeddings at the existing numerical floor
  • The nearest-prototype counterexample from the issue
  • Invariance under spatial-coordinate permutations
  • Gradient agreement with the reference, finite gradients, symmetry, and finite-difference gradcheck

Verification

Linux CPU, Python 3.12.14, PyTorch 2.14.1+cpu, transformers 4.49.0, pytest 8.1.1, black 24.2.0, ruff 0.7.4, mypy 1.15.0.

  • pytest tests/test_nn_distance.py --force-cpu -o addopts='': 14 failed / 2 passed before the fix; 16 passed after the fix, no skips
  • Black and Ruff checks on both changed files: passed
  • git diff --check: passed
  • A real PrototypicalDecoder(distance_function="hyperbolic") smoke check selects the identical prototype in float32 and float64; cross-entropy backpropagation produces finite input/prototype gradients
  • pytest tests/test_nn_distance.py --force-cpu: 19 passed / 1 failed. The failure is the mypy session status from nine errors in unchanged files
  • pytest flair: 254 passed / 5 failed, with the same nine mypy errors in flair/samplers.py, flair/datasets/text_image.py, flair/nn/model.py, and flair/trainers/trainer.py. Repeating this command on untouched master produced identical failure details

The full basic/integration suite and GPU checks were not run locally.

@Afloat16
Afloat16 marked this pull request as ready for review October 14, 2026 13:40
@Afloat16

Copy link
Copy Markdown
Author

Investigated the failing CI run. Collection stops in the unchanged tests/test_visual.py: Matplotlib 3.9.4 calls pyparsing 3.3.3's deprecated oneOf, and the existing warnings-as-errors policy turns that into a collection error. That run did not execute the hyperbolic-distance regressions.

I reproduced the same failure with an import-only matplotlib.pyplot check using Matplotlib 3.9.4, pyparsing 3.3.3 and NumPy 2.0.2 on Python 3.12.14, without importing Flair. Changing only pyparsing to 3.2.5 makes that import check pass. The visual test, plotting module, warning configuration, dependency files and CI workflow are byte-identical between this PR head (ac101ff) and current master (d4ea377). This identifies a baseline dependency compatibility failure; the full CI suite remains unverified.

Constrain pyparsing below 3.3 in Python 3.9 development environments because Matplotlib 3.9 still uses parser aliases deprecated in pyparsing 3.3. Preserve warnings-as-errors and the existing test suite.
@Afloat16

Afloat16 commented Nov 5, 2026

Copy link
Copy Markdown
Author

The current head 4982de78 includes the Python <3.10 pyparsing<3.3 constraint for the Matplotlib collection issue discussed above. The new CI run 41878946237 is now waiting for maintainer approval (action_required); it has not executed the checks for this revision.

Could a maintainer approve this run so the updated branch can receive the required validation? Thank you.

@plonerma

plonerma commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

@alanakbik : Could you review these issues?

  1. The failing test cases seem unrelated to the changes in this PR?
  2. Please review which dependency flair should rely on in the future: the change introduced here (pyparsing<3.3; python_version < "3.10") is unrelated to the issue in the hyperbolic distance module.

Edit: Apart from this, the changes and test-cases look good to me.

@Afloat16

Afloat16 commented Nov 5, 2026

Copy link
Copy Markdown
Author

Thanks @plonerma. I have removed the development dependency override in 18473db. requirements-dev.txt now matches master exactly, and this PR contains only the Lorentz-product fix and tests/test_nn_distance.py.

I also checked the completed CI run on 4982de7:

  • All 16 new numerical/gradient regression tests passed.
  • Mypy, Black, and Ruff passed for both changed files.
  • The overall run finished with 710 passed, 121 skipped, 66 failed, and 36 errors. The reported failures include model/tokenizer loading, the NILC download endpoint returning HTTP 404, and hf_xet.download_files() deprecation warnings treated as errors. Neither changed file appears in the failure summary.

The mathematical implementation and test file are byte-for-byte unchanged from that run. The dependency override was only a workaround for the earlier Python 3.9/Matplotlib collection failure; its removal keeps that dependency-policy decision separate from #3702. The next run may therefore encounter that collection issue again, but this PR no longer changes the project's dependency constraints.

@Afloat16

Afloat16 commented Nov 5, 2026

Copy link
Copy Markdown
Author

Thanks @plonerma. The current head 18473dbe has removed the dependency override; the PR now contains only flair/nn/distance/hyperbolic.py and tests/test_nn_distance.py.

The CI run for this revision is waiting for maintainer approval (action_required) and has not executed its checks. Could you approve the run so the checks for this revision can execute? Thank you.

This branch has not been deployed

No deployments
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.

HyperbolicDistance drops the first spatial coordinate from the Lorentz product

3 participants