Skip to content

[Keras Ops] view_as_complex() and view_as_real() #21221

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

Merged
merged 12 commits into from
May 5, 2025

Conversation

DavidLandup0
Copy link
Contributor

@DavidLandup0 DavidLandup0 commented Apr 29, 2025

Convenience operation, used to convert between complex and real tensors.
Useful for doing operations in architectures like DeepSeek V3/R1 (keras-team/keras-hub#2171).

Equivalent to PyTorch's:

Usage:

view_as_real() converts a complex-type tensor into a real tensor:

>>> from keras import ops
>>> import torch
>>> x=torch.randn(4, dtype=torch.cfloat)
>>> ops.view_as_real(x)
>>> ops.view_as_real(x).numpy()
array([[-0.6658956 , -1.199816  ],
       [ 0.6203052 , -0.647082  ],
       [ 0.6746265 , -0.75243765],
       [ 0.02358519, -0.8950158 ]], dtype=float32)

view_as_complex() does the opposite:

>>> x=torch.randn(4, 2)
>>> x
tensor([[-0.6625, -1.4961],
        [-0.0678, -0.6881],
        [ 0.1599,  0.1223],
        [ 1.8277,  0.1058]])
>>> ops.view_as_complex(x).numpy()
array([-0.6624739 -1.496139j  , -0.06781768-0.6881176j ,
        0.15992023+0.12225848j,  1.8276887 +0.10576057j], dtype=complex64)

/cc @fchollet for review

@codecov-commenter
Copy link

codecov-commenter commented Apr 29, 2025

Codecov Report

Attention: Patch coverage is 73.68421% with 10 lines in your changes missing coverage. Please review.

Project coverage is 82.60%. Comparing base (37eacb0) to head (4ef4b65).
Report is 12 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/ops/math.py 76.47% 8 Missing ⚠️
keras/api/_tf_keras/keras/ops/__init__.py 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21221      +/-   ##
==========================================
- Coverage   82.61%   82.60%   -0.02%     
==========================================
  Files         564      564              
  Lines       54476    54581     +105     
  Branches     8470     8476       +6     
==========================================
+ Hits        45005    45084      +79     
- Misses       7395     7412      +17     
- Partials     2076     2085       +9     
Flag Coverage Δ
keras 82.41% <73.68%> (-0.02%) ⬇️
keras-jax 63.67% <73.68%> (-0.05%) ⬇️
keras-numpy 58.80% <73.68%> (-0.04%) ⬇️
keras-openvino 32.99% <31.57%> (+<0.01%) ⬆️
keras-tensorflow 64.09% <73.68%> (-0.03%) ⬇️
keras-torch 63.75% <73.68%> (-0.05%) ⬇️

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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DavidLandup0
Copy link
Contributor Author

The torch tests don't like the dtype comparisons

AssertionError: torch.float32 != dtype('float32')

Is there a Keras-preferred way to handle this case or should I just find a reasonable way to do the comparison instead?

@fchollet
Copy link
Collaborator

Is there a Keras-preferred way to handle this case or should I just find a reasonable way to do the comparison instead?

Please use standardize_dtype before the comparison

Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

These functions do not appear to be NumPy operations, so at least they should not go in the keras.ops.numpy API (and should not be part of numpy.py).

Is there a standard name we can use? Maybe something in NumPy?

@gbaned gbaned added this to PR Queue May 2, 2025
@github-project-automation github-project-automation bot moved this to Assigned Reviewer in PR Queue May 2, 2025
@DavidLandup0
Copy link
Contributor Author

Strictly speaking, not Numpy operations, but they make use of real() and imag(), which are currently under numpy.py in Keras, hence the position in the PR. Open to putting them anywhere else - perhaps math.py?

Is there a standard name we can use? Maybe something in NumPy?

Numpy offers real() and imag() to extract components of complex arrays, but doesn't have a utility for converting between real and complex. The most standard name I can think of is the PyTorch one, since that's where the utility originated.

We could name it something like as_complex() or as_real(), but a more common name is probably going to be better for dev UX.

@fchollet
Copy link
Collaborator

fchollet commented May 3, 2025

Sounds good, let's move them to math.py.

numpy.py is strictly for actual numpy APIs.

@DavidLandup0
Copy link
Contributor Author

Sounds good, let's move them to math.py.

numpy.py is strictly for actual numpy APIs.

Thanks for weighing in! Moved it to math.py.

@github-project-automation github-project-automation bot moved this from Assigned Reviewer to Approved by Reviewer in PR Queue May 4, 2025
@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels May 4, 2025
)


@keras_export(["keras.ops.view_as_real", "keras.ops.math.view_as_real"])
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is not math namespace -- just export to ops

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops - good point. Accidentally left it in due to the original keras.ops.numpy location.
Removed!

@google-ml-butler google-ml-butler bot removed the ready to pull Ready to be merged into the codebase label May 4, 2025
@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels May 5, 2025
@hertschuh hertschuh merged commit 6ddaefb into keras-team:master May 5, 2025
7 checks passed
@github-project-automation github-project-automation bot moved this from Approved by Reviewer to Merged in PR Queue May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kokoro:force-run ready to pull Ready to be merged into the codebase size:M
Projects
Status: Merged
Development

Successfully merging this pull request may close these issues.

6 participants