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

Support vanilla lists when the default backend is non-numpy #1654

Open
kratsg opened this issue Oct 19, 2021 · 2 comments
Open

Support vanilla lists when the default backend is non-numpy #1654

kratsg opened this issue Oct 19, 2021 · 2 comments
Labels
feat/enhancement New feature or request help wanted Extra attention is needed / contributions welcome

Comments

@kratsg
Copy link
Contributor

kratsg commented Oct 19, 2021

(Make builders diffable)

#1646 will pull out a bug that we somewhat have in our code about assumptions of the default backend... we'll need to fix this.

This is just revealing an underlying feature we never actually supported

>>> import json
>>> ws = pyhf.Workspace(json.load(open('mysigfit_brZ_100_brH_0_brW_0_bre_33_brm_33_brt_34_mass_100.json')))
>>> pdf = ws.model()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/kratsg/.pyenv/versions/pyhf-dev/lib/python3.8/site-packages/pyhf/workspace.py", line 425, in model
    return Model(modelspec, **config_kwargs)
  File "/Users/kratsg/.pyenv/versions/pyhf-dev/lib/python3.8/site-packages/pyhf/pdf.py", line 632, in __init__
    modifiers, _nominal_rates = _nominal_and_modifiers_from_spec(
  File "/Users/kratsg/.pyenv/versions/pyhf-dev/lib/python3.8/site-packages/pyhf/pdf.py", line 129, in _nominal_and_modifiers_from_spec
    nominal_rates = nominal.finalize()
  File "/Users/kratsg/.pyenv/versions/pyhf-dev/lib/python3.8/site-packages/pyhf/pdf.py", line 69, in finalize
    [
  File "/Users/kratsg/.pyenv/versions/pyhf-dev/lib/python3.8/site-packages/pyhf/pdf.py", line 70, in <listcomp>
    pyhf.default_backend.concatenate(self.mega_samples[sample]['nom'])
  File "/Users/kratsg/.pyenv/versions/pyhf-dev/lib/python3.8/site-packages/pyhf/tensor/jax_backend.py", line 298, in concatenate
    return jnp.concatenate(sequence, axis=axis)
  File "/Users/kratsg/.pyenv/versions/pyhf-dev/lib/python3.8/site-packages/jax/_src/numpy/lax_numpy.py", line 3382, in concatenate
    _check_arraylike("concatenate", *arrays)
  File "/Users/kratsg/.pyenv/versions/pyhf-dev/lib/python3.8/site-packages/jax/_src/numpy/lax_numpy.py", line 560, in _check_arraylike
    raise TypeError(msg.format(fun_name, type(arg), pos))
TypeError: concatenate requires ndarray or scalar arguments, got <class 'list'> at position 0.

Originally posted by @kratsg in #1646 (comment)

@matthewfeickert
Copy link
Member

@kratsg @phinate can you comment a bit more on the specifics of what is causing this problem and/or create a public workspace that will fail for this?

If I take the workspaces from Discussion #1695

import json

import pyhf

if __name__ == "__main__":

    with open("NormalMeasurement_combined.json") as read_file:
        workspace_json = json.load(read_file)

    backends = ["numpy", "jax", "pytorch", "tensorflow"]
    for backend in backends:
        print(f"\n{backend}")
        pyhf.set_backend(backend)
        workspace = pyhf.Workspace(workspace_json)
        model = workspace.model()
        assert model is not None

those are fine, so can you summarize in the Issue what the problems are that you're encountering in Issue #1646 and PR #1655.

@matthewfeickert matthewfeickert added the help wanted Extra attention is needed / contributions welcome label Nov 12, 2021
@phinate
Copy link
Contributor

phinate commented Nov 12, 2021

@kratsg @phinate can you comment a bit more on the specifics of what is causing this problem and/or create a public workspace that will fail for this?

If I take the workspaces from Discussion #1695

import json

import pyhf

if __name__ == "__main__":

    with open("NormalMeasurement_combined.json") as read_file:
        workspace_json = json.load(read_file)

    backends = ["numpy", "jax", "pytorch", "tensorflow"]
    for backend in backends:
        print(f"\n{backend}")
        pyhf.set_backend(backend)
        workspace = pyhf.Workspace(workspace_json)
        model = workspace.model()
        assert model is not None

those are fine, so can you summarize in the Issue what the problems are that you're encountering in Issue #1646 and PR #1655.

No problem -- the issue arises when using the new pyhf.set_backend(..., default=True) arg. @kratsg enabled this functionality in #1646, but this exposed issues where using e.g. jax as the default backend had unforseen consequences that result from differing behaviour between jax and numpy.

The specific problem I found was to do with tensorlib.concatenate -- details are also in #1655, but the bottom line is that jax.numpy.concatenate:

  • does not support lists within the iterable of arrays
  • does not support jagged concatenation

Code examples:

import numpy as npnp.concatenate([[4,5], [3]])
> array([4, 5, 3])

import jax.numpy as jnpjnp.concatenate(jnp.array([[4,5], [3]]))
> /home/jovyan/.local/lib/python3.8/site-packages/jax/_src/numpy/lax_numpy.py:476: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  arr = np.array(obj, dtype=dtype, **kwargs)

... (leaving out long stack trace)

TypeError: JAX only supports number and bool dtypes, got dtype object in array

This happens in pyhf possibly in multiple places, but the main occurence I found was when using tensorlib.stitch.
It's only possible to replicate the numpy behaviour here through iteratively casting to arrays within the iterable in jax_backend.py, but I also don't remember if this fixed the problem or maybe introduced some other pathology when I tried it...

Hope this makes sense! @lukasheinrich was also encountering these things while working on #1676, so he may also have comments or a resolution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat/enhancement New feature or request help wanted Extra attention is needed / contributions welcome
Projects
None yet
Development

No branches or pull requests

3 participants