Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure TensorFlow backend Poisson compatibility with other backends #1001

Merged
merged 5 commits into from
Sep 2, 2021

Conversation

matthewfeickert
Copy link
Member

@matthewfeickert matthewfeickert commented Jul 28, 2020

Description

Resolves #997

As TensorFlow Probability v0.11.0+ is needed for PR #817, and as Issue #997 makes it clear that TF/TFP minor releases can be as breaking as normal major releases, require TensorFlow v2.3.0+ in the v2.3.X range and TensorFlow Probability v0.11.0+ in the v0.11.X range.

Ensure compatibility of TensorFlow Poisson's behavior with PyTorch and JAX while Issue #293 is being resolved by using the differentiable TensorFlow operations tf.logical_or and tf.where to allow for checking if the situation in which Poisson(n=0 | lam=0) is being encountered. This provides a working solution for both the TF v2.2.X and TFP V0.10.X API behavior as well as for the TF v2.3.+ and TFP v0.11.0+ API behavior. Also disallow TF v2.3.0 given tensorflow/tensorflow#40789.

Checklist Before Requesting Reviewer

  • Tests are passing
  • "WIP" removed from the title of the pull request
  • Selected an Assignee for the PR to be responsible for the log summary

Before Merging

For the PR Assignees:

  • Summarize commit messages into a comprehensive review of the PR
* Use differentiable TensorFlow operations to determine if Poisson(n=0 | lam=0) is being encountered and ensure a return value compatible with PyTorch and JAX
* Use TensorFlow releases compatible with v2.X with a lower bound of v2.2.1
   - Disallow TensorFlow v2.3.0 for accidental pinning of SciPy
* Use TensorFlow Probability releases compatible with v0.X with a lower bound of v0.10.1
* Use tf.errors.InvalidArgumentError for compatibility across TensorFlow releases

@matthewfeickert matthewfeickert added the build Changes that affect the build system or external dependencies label Jul 28, 2020
@matthewfeickert matthewfeickert self-assigned this Jul 28, 2020
@matthewfeickert matthewfeickert added the docs Documentation related label Jul 28, 2020
@matthewfeickert matthewfeickert marked this pull request as draft July 28, 2020 04:24
@matthewfeickert
Copy link
Member Author

It appears that in TensorFlow Probability v0.11.0 they decided to make Pois(0|0) = 1 instead of nan. This breaks the following test_tensor.py test

pyhf/tests/test_tensor.py

Lines 201 to 206 in 8a2f7f6

# poisson(lambda=0) is not defined, should return NaN
assert tb.tolist(
tb.poisson(tb.astensor([0, 0, 1, 1]), tb.astensor([0, 1, 0, 1]))
) == pytest.approx(
[np.nan, 0.3678794503211975, 0.0, 0.3678794503211975], nan_ok=True
)

as

import pyhf

backends = ["numpy", "pytorch", "tensorflow", "jax"]

for backend in backends:
    pyhf.set_backend(backend, precision="64b")
    tb = pyhf.tensorlib
    poisson_results = tb.tolist(
        tb.poisson(tb.astensor([0, 0, 1, 1]), tb.astensor([0, 1, 0, 1]))
    )
    print(f"{backend} backend gives: {poisson_results}")

gives

numpy backend gives: [nan, 0.36787944117144233, 0.0, 0.36787944117144233]
pytorch backend gives: [nan, 0.36787944117144233, 0.0, 0.36787944117144233]
tensorflow backend gives: [1.0, 0.3678794411714423, 0.0, 0.3678794411714423]
jax backend gives: [nan, 0.367879441171442, 0.0, 0.367879441171442

So it seems that before this PR can be resolved that Issue #293 will need to get addressed again.

@matthewfeickert
Copy link
Member Author

This is going to be on hold for quite sometime as TensorFlow v2.3.0 has a bug where scipy==1.4.1 was incorrectly introduced somehow. This has been fixed and so the next patch release of TensorFlow should remove all SciPy dependency completely making this a non-issue moving forward.

This will mean that we will need to require

    'tensorflow': [
        'tensorflow~=2.3.1',  # TensorFlow minor releases are as volatile as major
        'tensorflow-probability~=0.11.0',
    ],

and by that time there might be a patch release of TFP out that we might want as well.

@matthewfeickert
Copy link
Member Author

The pinning of scipy==1.4.1 has been fixed in TensorFlow v2.3.1 but we still need to deal with the fact that TensorFlow Probability v0.11.0 has Pois(0|0) = 1 instead of nan.

@matthewfeickert matthewfeickert force-pushed the build/require-TF-v2.3.0-TFP-v0.11.0 branch from dd0bea4 to 86fb51d Compare October 27, 2020 23:58
@matthewfeickert matthewfeickert force-pushed the build/require-TF-v2.3.0-TFP-v0.11.0 branch from 86fb51d to 3c07ce8 Compare September 1, 2021 03:47
@matthewfeickert matthewfeickert changed the title build: Require TensorFlow v2.3.X and TensorFlow Probability v0.11.X fix: Ensure TensorFlow backend Poisson comparability with other backends Sep 1, 2021
@matthewfeickert matthewfeickert added the fix A bug fix label Sep 1, 2021
@matthewfeickert matthewfeickert changed the title fix: Ensure TensorFlow backend Poisson comparability with other backends fix: Ensure TensorFlow backend Poisson compatibility with other backends Sep 1, 2021
@codecov
Copy link

codecov bot commented Sep 1, 2021

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.70%. Comparing base (b7a2c65) to head (f7b2c1a).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1001   +/-   ##
=======================================
  Coverage   97.70%   97.70%           
=======================================
  Files          63       63           
  Lines        4047     4050    +3     
  Branches      576      576           
=======================================
+ Hits         3954     3957    +3     
  Misses         54       54           
  Partials       39       39           
Flag Coverage Δ
contrib 25.43% <16.66%> (+<0.01%) ⬆️
unittests 97.48% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@matthewfeickert matthewfeickert removed the docs Documentation related label Sep 1, 2021
@matthewfeickert matthewfeickert marked this pull request as ready for review September 1, 2021 05:42
@@ -2,6 +2,7 @@
import logging
import tensorflow as tf
import tensorflow_probability as tfp
from numpy import nan
Copy link
Member Author

@matthewfeickert matthewfeickert Sep 1, 2021

Choose a reason for hiding this comment

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

I don't know of any nan equivalent in TensorFlow, as they use np.nan in their test suites.

@matthewfeickert matthewfeickert force-pushed the build/require-TF-v2.3.0-TFP-v0.11.0 branch from 24ad4d9 to f7b2c1a Compare September 2, 2021 12:33
@kratsg kratsg merged commit 5247c16 into master Sep 2, 2021
@kratsg kratsg deleted the build/require-TF-v2.3.0-TFP-v0.11.0 branch September 2, 2021 14:21
matthewfeickert added a commit that referenced this pull request Sep 2, 2021
* Add official support for Python 3.9 and add Python 3.9 to CI
   - Required PR #1001 to get Python 3.9 support of TensorFlow and TensorFlow Probability
* Add Python 3.9 support to PyPI metadata
* Update GitHub Actions workflows to use Python 3.9
   - .github/workflows/release_tests.yml will be updated after pyhf v0.6.3 is released
* Update Dockerfile to use python:3.9-slim-bullseye as base image
lukasheinrich pushed a commit that referenced this pull request Oct 22, 2021
* Effectively reverts most of PR #1001 and PR #280, reapplies most
of PR #277
* Use scipy.special.xlogy in Poisson computation for numpy backend
and use jax.scipy.special.xlogy for jax backend
* Set minimum required PyTorch to v1.10 for API stability
   - c.f. pytorch/pytorch#61511 in torch v1.10.0
* Set minimum required TensorFlow to v2.3.1 and TensorFlow Probability
to v0.11.0
   - tfp v0.11.0 supports zero rate Poisson and requires tensorflow>=2.3.0
* Add note to docs that limit Poisson(n = 0 | lambda -> 0) = 1 is being used
* Update tests to use limit Poisson(n = 0 | lambda -> 0) = 1 result
* Run doctest on only the latest Python release

Co-authored-by: Ruggero Turra <giurrero@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build Changes that affect the build system or external dependencies fix A bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix TensorFlow v2.3.0 and TensorFlow Probability v0.11.0 release incompatibilities
2 participants