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

Change dtype repr for Numpy 1.24 #5991

Merged
merged 6 commits into from
Apr 3, 2023

Conversation

bnavigator
Copy link
Contributor

@bnavigator bnavigator commented Jan 28, 2023

This is a step towards #5967. It uses the direct repr string from numpy itself dtype('typename') but with the prefix np., which should work for Python-builtins like 'int','float','bool' as well as numpy-specific types.

With this patch, the rpm package for python-cirq 1.1.0 on openSUSE Tumbleweed passes. We need it because we patched numba with numba/numba#8620 and have NumPy 1.24 already.

One part still missing from #5967, which is irrelevant for the openSUSE package, are the json validations. We have been disabling those with pytest parameter -k "not (test_json_test_data_coverage or test_json_and_repr_data)" because they don't like our installation prefix.

@bnavigator bnavigator requested review from a team, vtomole and cduck as code owners January 28, 2023 21:43
@CirqBot CirqBot added the size: M 50< lines changed <250 label Jan 28, 2023
@pavoljuhas pavoljuhas added the triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on label Feb 15, 2023
pavoljuhas added a commit to pavoljuhas/Cirq that referenced this pull request Mar 9, 2023
Temporarily upgrade to numpy-1.24.2 to enable CI testing of quantumlib#5991.
Also add an intentionally failing CI check to ensure this commit
is not accidentally merged.

Please revert this commit before merging the pull-request.
pavoljuhas added a commit to pavoljuhas/Cirq that referenced this pull request Mar 10, 2023
Temporarily upgrade to numpy-1.24.2 to enable CI testing of quantumlib#5991.

Restrict CI-tests to Python 3.8 only which permits installation of
numpy-1.24.2 and numba.  Note numba is not yet compatible with
numpy-1.24 and its import raises SystemError exception
SystemError: initialization of _internal failed without raising an exception

Here we also add intentionally failing CI check to ensure this commit
is not accidentally merged to master.

Please revert this commit before merging the pull-request.
@pavoljuhas
Copy link
Collaborator

Thank you @bnavigator for contributing this.

Could you please take care of the lint / formatting CI errors above and the remaining np.bool instances in the .repr files?

I'd suggest to cherry-pick or re-apply 5d0e6d9 from my temporary PR #6028, which converts numpy deprecation warnings to test errors. Once this is passing the CI and merged, we can be fairly sure old attributes won't be reintroduced.

Were there any other test failures you saw in the test_json* tests with numpy-1.24 and patched numba?

Copy link
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

Thank you for contributing this. Please see my suggestions in #5991 (comment)

@bnavigator bnavigator force-pushed the core-np1.24 branch 2 times, most recently from e7433ab to b84d1bd Compare March 18, 2023 17:48
Copy link
Collaborator

@tanujkhattar tanujkhattar left a comment

Choose a reason for hiding this comment

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

LGTM % one comment

@@ -1 +1 @@
cirq_google.EngineResult(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[True, True, False, True, False]], [[False, True, True, False, False]], [[True, False, True, False, True]]], dtype=np.bool)}, job_id='my_job_id', job_finished_time=datetime.datetime(2022, 4, 1, 8, 23, 45, tzinfo=datetime.timezone.utc))
cirq_google.EngineResult(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[True, True, False, True, False]], [[False, True, True, False, False]], [[True, False, True, False, True]]], dtype=bool)}, job_id='my_job_id', job_finished_time=datetime.datetime(2022, 4, 1, 8, 23, 45, tzinfo=datetime.timezone.utc))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This repr is not what gets generated if I call repr(engine) where engine is the corresponding object, right?

For example, this is what I get when I run repr(engine) using your PR, where dtype=np.dtype('bool') instead of dtype=bool.

>>> repr(engine)
cirq_google.EngineResult(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[True, True, False, True, False]], [[False, True, True, False, False]], [[True, False, True, False, True]]], dtype=np.dtype('bool'))}, job_id='my_job_id', job_finished_time=datetime.datetime(2022, 4, 1, 8, 23, 45, tzinfo=datetime.timezone.utc))

In general, all the .repr files should contain the output of calling repr(val) for each val. Since we are changing the repr generation, we'd need to update every object that is impacted by this change. i.e. Let Object be a class s.t. repr(object) used to contain np.int and now contains np.dtype(int). We need to ensure that Object.repr is updated to reflect the new output of repr(object).

bnavigator and others added 2 commits April 3, 2023 19:09
Also fail when attributes are used in eval expressions.

This prepares for upgrade to NumPy 1.24 which removes deprecated names.
Copy link
Collaborator

@tanujkhattar tanujkhattar left a comment

Choose a reason for hiding this comment

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

Can you just push new commits on top and update the PR instead of force pushing ? the force-push seems to be deleting the history of changes done in previous commits.

@@ -1 +1 @@
[cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[True, True, False, True, False]], [[False, True, True, False, False]], [[True, False, True, False, True]]], dtype=bool)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[1, 1, 0, 1, 0]], [[0, 1, 1, 0, 0]], [[1, 0, 1, 0, 1]]], dtype=np.uint8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.int8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.int16)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.int32)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.int64)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.uint8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.uint16)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.uint32)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.uint64)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[1, 1, 0, 1, 0]], [[0, 1, 1, 0, 0]], [[1, 0, 1, 0, 1]]], dtype=np.uint8), 'n': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.int64)})]
[cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[True, True, False, True, False]], [[False, True, True, False, False]], [[True, False, True, False, True]]], dtype=np.dtype('bool'))}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[1, 1, 0, 1, 0]], [[0, 1, 1, 0, 0]], [[1, 0, 1, 0, 1]]], dtype=np.uint8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.int8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.int16)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.int32)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.int64)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.uint8)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.uint16)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.uint32)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.uint64)}), cirq.ResultDict(params=cirq.ParamResolver({sympy.Symbol('a'): 0.5}), records={'m': np.array([[[1, 1, 0, 1, 0]], [[0, 1, 1, 0, 0]], [[1, 0, 1, 0, 1]]], dtype=np.uint8), 'n': np.array([[[0, 1, 2]], [[3, 4, 5]], [[6, 7, 8]]], dtype=np.int64)})]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Were these regenerated using repr(object) ? It looks like dtype=np.uint64 should also be dtype=np.dtype('uint32') instead ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do it yourself

@bnavigator
Copy link
Contributor Author

Can you just push new commits on top and update the PR instead of force pushing ? the force-push seems to be deleting the history of changes done in previous commits.

I rebased the only relevant changes on top of the current master. This is a perfectly legit pattern in git. All the relevant history is still there.

Side note: Do you have any idea how annoying it is to have to change a courtesy contribution from 2 months ago because you add more and more demands?

@tanujkhattar
Copy link
Collaborator

I understand, this doesn't need to be blocking so I've opened a separate issue to track this change. #6049

Thank you for the patience and the courtesy contribution! We can merge once the CI passes.

@tanujkhattar tanujkhattar merged commit 663d404 into quantumlib:master Apr 3, 2023
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
* Change numpy dtype repr

* Enforce deprecation of np attributes deprecated in NumPy 1.20

Also fail when attributes are used in eval expressions.

This prepares for upgrade to NumPy 1.24 which removes deprecated names.

* update repr test data for numpy dtypes

* break string not found by black

* fix dumb typo

---------

Co-authored-by: Pavol Juhas <[email protected]>
Co-authored-by: Tanuj Khattar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: M 50< lines changed <250 triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants