Fix unstable output of periodic sequence due to precision error. #35311
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.
A fix following #35300.
See the explanation below (copied from the inline comments):
In
ImpulseSeqGenRestrictionProvider, the total number of counts (i.e.total_outputs) is computed byceil((end - start) / interval)(beam/sdks/python/apache_beam/transforms/periodicsequence.py
Line 45 in facdc4e
endisstart + duration(beam/sdks/python/apache_beam/ml/ts/util.py
Line 172 in facdc4e
Due to precision error of arithmetic operations, even if
durationis set tolen(self._data) * interval,(end - start) / intervalcould be a little bit smaller or bigger thanlen(self._data).In case of being bigger,
total_outputswould belen(self._data) + 1, as theceil()operation is used, which is NOT what we want.Assuming that the precision error is no bigger than 1%, by subtracting a small amount, we ensure that the result after
ceil()is stable even if a certain degree of precision error is present.