Skip to content

Conversation

@gdewael
Copy link
Contributor

@gdewael gdewael commented Dec 22, 2025

Currently, the transformers in depthcharge should not be trained with float16 or bfloat16 precision, which has become semi-standard in transformer training.
The issue is two-fold: self.sin_term and self.cos_term have large numbers which cannot be accurately represented with lower precision, distorting the positional encodings (PositionalEncoder(1024).sin_term.max() returns tensor(15915.4941)), and the mz resolution of MS experiments can not be faithfully represented using bfloat16 and float16 inputs (i.e., mz_array inputs to SpectrumTransformerEncoder, see #76 (comment)).

This PR does the following:

  • When FloatEncoder's learnable_wavelengths=False, it fixes self.sin_term and self.cos_term to float32 precision.
  • When FloatEncoder is passed lower than float32 precision values, it raises a warning.
  • It introduces a dummy buffer to convert the sinusoidal encodings to the proper model precision after they have been computed.
  • A similar procedure is followed for PositionalEncoder, where this line would otherwise give inaccurate positions for large sequences in combination with bfloat16 model dtypes.

@codecov
Copy link

codecov bot commented Dec 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.69%. Comparing base (5a1afb1) to head (caf27e8).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #82      +/-   ##
==========================================
+ Coverage   96.62%   96.69%   +0.06%     
==========================================
  Files          24       24              
  Lines        1008     1029      +21     
==========================================
+ Hits          974      995      +21     
  Misses         34       34              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant