Add float16 and bfloat16 support in sinusoidal encodings. #82
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, the transformers in
depthchargeshould not be trained with float16 or bfloat16 precision, which has become semi-standard in transformer training.The issue is two-fold:
self.sin_termandself.cos_termhave large numbers which cannot be accurately represented with lower precision, distorting the positional encodings (PositionalEncoder(1024).sin_term.max()returnstensor(15915.4941)), and the mz resolution of MS experiments can not be faithfully represented using bfloat16 and float16 inputs (i.e.,mz_arrayinputs toSpectrumTransformerEncoder, see #76 (comment)).This PR does the following:
FloatEncoder'slearnable_wavelengths=False, it fixesself.sin_termandself.cos_termtofloat32precision.FloatEncoderis passed lower thanfloat32precision values, it raises a warning.PositionalEncoder, where this line would otherwise give inaccurate positions for large sequences in combination with bfloat16 model dtypes.