feat(ops): Add keras.ops.numpy.rot90 operation (#20723) by harshaljanjani · Pull Request #20745 · keras-team/keras (original) (raw)

Bumps the github-actions group with 2 updates: actions/upload-artifact and github/codeql-action.

Updates actions/upload-artifact from 4.4.3 to 4.5.0

Updates github/codeql-action from 3.27.5 to 3.28.0


updated-dependencies:

Signed-off-by: dependabot[bot] support@github.com Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>


Co-authored-by: François Chollet francois.chollet@gmail.com

Add tf2onnx requirements

The window_length parameter in tf.keras.ops.istft requires tf.int32 dtype, but this isn't documented. This can cause unexpected ValueError when using tf.int64 and tf.int16

Here is the Example case:

import tensorflow as tf

input_dict = {
    'stfts': tf.constant([[-0.87817144+1.14583987j, -0.32066484+0.25565411j]], dtype=tf.complex128),
    'frame_length': tf.constant(256, dtype=tf.int16),
    'frame_step': tf.constant(5120,dtype=tf.int64)
}
result = tf.signal.inverse_stft(**input_dict)
print(result)

The code throws the following error:

ValueError: window_length: Tensor conversion requested dtype int32 for Tensor with dtype int64

The dtype / DTypePolicy is applied to all float variables.

by simply using ops.shape(x) instead of x.shape.

The same tests exist at:

The goal is to isolate the use of onnxruntime to a single file, onnx_test.py.

Signed-off-by: Kazantsev, Roman roman.kazantsev@intel.com


Signed-off-by: Kazantsev, Roman roman.kazantsev@intel.com

Multiple Example Title has removed in metrics.MeanIoU method

In particular for functional models.

Adds a new operation to rotate tensors by 90 degrees in the specified plane:

Add implementations of rot90() for multiple backend frameworks:

Move rot90 operation to numpy.py files in backend implementations since it's a numpy op (https://numpy.org/doc/stable/reference/generated/numpy.rot90.html). Now exported as both keras.ops.rot90 and keras.ops.numpy.rot90.

Resolved the 'Invalid dtype: object' error by explicitly using to avoid naming conflicts with the custom function.

Replace tf.experimental.numpy.rot90 with core TF ops for XLA compatibility. Use convert_to_tensor for input handling.

The DTypePolicy test produces lower precision results.

Not all of their symbols are exported.

Added ragged option to KerasTensor, InputLayer and convert_to_tensor. The logic is the same as for sparse tensors.

Fixes https://github.com/keras-team/keras/issues/20731

This new implementation is in line with what was done in Keras 2. It tracks all TrackableResources, and lookup tables and hashmaps are subclasses of TrackableResource.

This allows users to attach preprocessing functions that are not solely based on Keras preprocessing layers.

Some tools don't like the mix of code with and without type hints.

Refactor to use backend specific gradient functions in tests and merges logic into single function

This PR addresses review feedback to fix implementation and to move tests to using named_parameters rather than individual functions.

Adds axis param and fixes logic for per channel function

All floats will now follow the global dtype policy unless a specific dtype policy is passed to the layer.

The issue arose from improper handling of out-of-bound coordinates, causing invalid indexing when using dtype='uint8' with TensorFlow backend.

Changes made:

Tests for uint8 and float32 now succeed, and the logic for nearest fill_mode and manual casting is also fixed.

Fixes #20608

Was able to still reproduce the error, the PyTorch backend had inconsistent behavior between static shape inference and dynamic execution for pooling operations, particularly with 'same' padding and non-unit strides, figured that the root cause was by incorrect padding calculation logic that didn't properly handle asymmetric padding cases.

Key changes:

This fixes the issue where MaxPooling2D with 'same' padding would produce different shapes between compute_output_shape() and actual execution (e.g. (1,5,2,2) vs (1,5,2,1)).

Rebased on top of Sachin's September 2024 PR to incorporate latest keras:master changes.


Co-authored-by: sachin prasad sachinprasad@google.com

The history is only used in model.fit, no need to create it for evaluate and predict. The history is attached to the model and therefore lives for as long as the model is around.

The executor used in CallbackList was never shut down, causing it to keep a thread around, which in turn had thread locals that were leaked.


Co-authored-by: François Chollet francois.chollet@gmail.com

Previously, BinaryAccuracy would return incorrect results when given boolean inputs in JAX backend, and would raise errors in TensorFlow backend. This was because the metric expects numerical values (floats/integers) but wasn't properly handling boolean array inputs.

Fix by casting y_true and y_pred to floatx() in MeanMetricWrapper.update_state(). This ensures consistent behavior across backends and proper handling of boolean inputs.

The error message produced when using, for example, a tensorflow math operation in a layer referenced a nonexistent keras.operations namespace (which makes fixing the issue a lot more difficult for newcomers, given that they will encounter it while following examples from the book Deep Learning with Python, 2nd edition). The correct name of the implied namespace is keras.ops.

this change will allow users to customize what happens in the step function while being able to use existing metrics update logic without needing to duplicate it

Co-authored-by: Zoe Kendall zkendall@google.com

Update masked moments calculation to properly account for broadcast dimensions when summing mask weights.

Added test to verify broadcast mask handling produces zero-centered outputs.

Avoid repeated type checks and conversions during forward pass.

Fixes https://github.com/keras-team/keras/issues/20806

This a workaround for an incompatibility between 3.6 and 3.7 introduced by serialization bug fix https://github.com/keras-team/keras/pull/20406

Error with torch 2.6:

ImportError: /opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/_XLAC.cpython-39-x86_64-linux-gnu.so: undefined symbol: _ZN5torch8autograd12VariableInfoC1ERKN2at6TensorE

/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/torch_xla/__init__.py:20: Impo

This is needed to make it compatible with the pinned version of torch we're using.

Note that torch-xla 2.6 doesn't support GPU https://pypi.org/project/torch-xla/2.6.0/ GPU support will be coming back with 2.7.

The is_tf_dataset() function in data adapters now recognizes DistributedDatasetsFromFunction as a valid TensorFlow dataset type. This allows for properly handling distributed datasets created via strategy.distribute_datasets_from_function()

Bumps the github-actions group with 2 updates: actions/upload-artifact and github/codeql-action.

Updates actions/upload-artifact from 4.5.0 to 4.6.0

Updates github/codeql-action from 3.28.0 to 3.28.8


updated-dependencies:

Signed-off-by: dependabot[bot] support@github.com Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

This mechanism is required for open-source community who will provide PRs for each operation. In order to validate PR with the concrete operation support, they should remove the corresponding line.

Signed-off-by: Kazantsev, Roman roman.kazantsev@intel.com

Signed-off-by: Kazantsev, Roman roman.kazantsev@intel.com

Signed-off-by: Kazantsev, Roman roman.kazantsev@intel.com


Signed-off-by: Kazantsev, Roman roman.kazantsev@intel.com

On the Jax backend we were not using donate_argnums during predict. This works when a model is mostly trainable, but when a model is mostly or all frozen, this will result in 2x the memory jump (which is why we use donate_argnums for fit and evaluate).

This change adds donate_argnums to the predict function to avoid the memory spike. But because this means all incoming state (including the trainable variables) will be deleted by jax, this means we need to sync the trainable variables state much like in fit and evaluate. An alternative would be to change the predict_step signature (so we could only donate non-trainable variables), but this would be a breaking change and confusing.

The state sharding is leaked at the end of fit, evaluate and predict. The values are not reused if fit, evaluate and predict is called again.

Clarify that Conv2D operations may exceed the documented 1e-7 precision difference across backends

Document that large convolutions can show notable variations due to accumulated floating-point operations


Co-authored-by: François Chollet francois.chollet@gmail.com

Signed-off-by: Kazantsev, Roman roman.kazantsev@intel.com

Previously, the output of HashedCrossing would always have None batch size as a result of the underlying Tensorflow tf.sparse.cross_hashed.

The previous reshaping logic in HashedCrossing would fix the last dimension (expected to be 1) but not the batch dimension.

When handling a ragged intermediate tensor, the padding code would still be executed even though Ragged.to_tensor already pads correctly. Changed control flow to skip padding.

When handling a dense intermediate tensor, the padding is applied from the dynamic shape. Added set_shape to apply the static output_sequence_length.

Fix shape handling in TF rot90 to correctly swap height/width dimensions based on k rotations.

Refactor test suite to use parameterized test cases and cover edge conditions more thoroughly.

Previously, track would only work with Layers or Models unless the backend was TensorFlow. It would raise an error on JAX for instance.

It is now possible to export saved models with a mix of Keras models and TensorFlow native preprocessing involving resources even with the JAX backend.

Fixes #20878. TensorBoard isn't able to report the correct step because this optimizer doesn't forward the iterations property.

It is not necessary to decorate excluded openvino tests with other backends.

Tests should only write files in a temp folder.

Recognize /placer paths as remote locations, allowing users to save Keras models directly to Placer paths.

Signed-off-by: Kazantsev, Roman roman.kazantsev@intel.com

Co-authored-by: Mohamed I. Hammad ibraaaa@gmail.com

This is a follow up to https://github.com/keras-team/keras/pull/20782 and a replacement for https://github.com/keras-team/keras/pull/20782

We cannot cast y_pred and y_true to the expected output dtype in MeanMetricWrapper. Some metrics expect integers (indices or IDs for instance) and fail if y_pred and y_true are provided as floats.

It is the responsibility of the metric function to cast as needed.

In this case, the correct approach in BinaryAccuracy is to use the regular type promotion rules to ensure that the comparison between y_pred and threshold is done without losing precision. ops.greater already does the type promotion correctly. Previously, threshold was incorrectly cast to the y_pred dtype, which in this case would lower its precision.

With JAX 0.5.1, jax2tf exports XLA that is not compatible with TensorFlow 2.18, making the saved_model_export.py tests fail.

Since Tensorflow 2.19 is not out yet, we pin JAX to 0.5.0 for now.

Bumps the github-actions group with 3 updates: ossf/scorecard-action, actions/upload-artifact and github/codeql-action.

Updates ossf/scorecard-action from 2.4.0 to 2.4.1

Updates actions/upload-artifact from 4.6.0 to 4.6.1

Updates github/codeql-action from 3.28.8 to 3.28.10


updated-dependencies:

Signed-off-by: dependabot[bot] support@github.com Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

Signed-off-by: Lim, Kuan Xian kuan.xian.lim@intel.com

Signed-off-by: Lim, Kuan Xian kuan.xian.lim@intel.com

Signed-off-by: Lim, Kuan Xian kuan.xian.lim@intel.com

Signed-off-by: Lim, Kuan Xian kuan.xian.lim@intel.com

Signed-off-by: Lim, Kuan Xian kuan.xian.lim@intel.com


Signed-off-by: Lim, Kuan Xian kuan.xian.lim@intel.com


Co-authored-by: François Chollet francois.chollet@gmail.com

Previously, serialization / deserialization would fail if:

The fix consists in adding a from_config to handle bin_boundaries separately. This is because at initial creation, bin_boundaries and num_bins cannot be both set, but when restoring the layer after adapt, they are both set.

Tightened the error checking:

Also removed init_bin_boundaries as the value was never used and its presence can be inferred.

The values are cached. Changed the code to access these directly instead of calling the convertion functions every time.

Made the following renames so that these functions can be used in backend agnostic code:

We can still use it for the resize op, but we shouldn't fail to import without jax installed.

removes unnecessary imports

Fixes #20955

Signed-off-by: Kazantsev, Roman roman.kazantsev@intel.com


Co-authored-by: Saif Mohammed ssaifmohammed04@gmail.com

rebased

fix: hardcode dtype int32 when weights=none

Signed-off-by: 11happy soni5happy@gmail.com

fix: use np.expand_dims

Signed-off-by: 11happy soni5happy@gmail.com

remove unecessary headers

Signed-off-by: 11happy soni5happy@gmail.com

style: reformat numpy_test.py

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com


Signed-off-by: 11happy soni5happy@gmail.com

The mask is named array in NumpyMask.

Previously, if no wheel was found, the [-1] subscript would fail, preventing the if not whl_path clause from outputting the error message.

Starting with 2.1, the first argument of np.reshape is positional only.

Removed keyword a and for consistency did the same with other backends.

when second argument is a constant int.

Previously, a convert_to_tensor was applied to the second argument, making it an int32 or int64. The result dtype would take into account this dtype, which could upgrade the dtype of the result.

The expectation is that if the second argument is a constant, the result dtype is the same as the first argument. This is already supported correctly by all underlying backend implementations.

Signed-off-by: Kazantsev, Roman roman.kazantsev@intel.com

The following will break as reimporting Keras will try to re-register the Tensorflow list/dict wrappers. Presumably anything that forced an actual reimport of keras would trigger the same crash.

import keras

keras.config.set_backend("tensorflow")

And use on_read synchronization for Metric variables.


Co-authored-by: François Chollet francois.chollet@gmail.com

Modified DDP check to use isinstance rather than type().name for robustness. Fixed additional whitepace

Fixes https://github.com/keras-team/keras/issues/20835

Also changed multi-input tests to exercise model.export() and its signature inference logic.

The _save_model method combined the logic to determine if the checkpoint should be saved, and the logic to create the paths and save the checkpoint.

This commit separates the check the determine whether the checkpoint should be saved from the I/O logic, and in doing so resolves two bugs in the current implementation:

  1. Host directory is created for every for save iteration, regardless of whether the model will be saved or not. For example, when save_freq == 'epoch' and save_best_only == True, a folder is created for every epoch, even though the model is only saved when the monitored condition is satisfied. This results in a large number of empty folders and makes it difficult to identify the most recently saved checkpoint.

With this commit, the directory to save the model or model weights is only created when necessary.

  1. If save_best_only=True, and the monitored value is an np.ndarray or backend tensor, then it falls back to save_best_only=False and saves the model. However, in this scenario, it save saves the whole model without regard to the value of self.save_weights_only

This commit uses consistent save logic that always checks the value of self.save_weights_only.

The functions that go through remat() should actually be called, if not, remat is not really applied.

JAX will soon fail when jnp.array is called with None, so this test will be broken under newer JAX versions if kept as is.

When constructing a Functional model with a global dtype policy, a spurious Cast operation would appear in the graph before each layer. This cast is part of the layer __call__ method and should not appear separately.

Bumps the github-actions group with 2 updates: actions/upload-artifact and github/codeql-action.

Updates actions/upload-artifact from 4.6.1 to 4.6.2

Updates github/codeql-action from 3.28.10 to 3.28.13


updated-dependencies:

Signed-off-by: dependabot[bot] support@github.com Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

Clarify parameter name

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com


Signed-off-by: 11happy soni5happy@gmail.com

Adds an alpha scaling parameter to LoRA layers, defaulting to rank for backward compatibility.

Updated docstrings to enclose parameters like 'alpha' and 'rank' in backticks as requested in PR review.

Fixes https://github.com/keras-team/keras/issues/21069

This flag no longer does anything in jax.

Adds the expected/actual output shapes/dtypes in the failure message.

Also greatly simplifies the code by using keras.tree.

Previously, only 2D Tensorflow ragged tensors were supported. This adds support for any rank.

Also added tests for ragged tensors with GeneratorDataAdapter.

Adds support for indices indices in the form of a tf.RaggedTensor to the Embedding layer by adding support to ops.take. The output is also ragged.

Also:

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com


Signed-off-by: 11happy soni5happy@gmail.com

The loss needs to not have any non-compilable op.

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com


Signed-off-by: 11happy soni5happy@gmail.com

ops.zeros_like is in particular useful for creating a mask of the populated values in the sparse tensor.

The jax types like jax.float32 have a string representation of

<class 'jax.numpy.float32'>

so with the previous code, would be "standardized" as float32'> (trailing quote and angle bracket), which is an invalid type. But, the JAX dtypes do have a __name__ property, so should be properly detected if we switch the order around.

Kept the old jax.numpy string version in place in case that worked with older versions of JAX.

Updates the requirements on tensorflow-cpu, tensorflow, torch, torch-xla and tensorflow[and-cuda] to permit the latest version.

Updates tensorflow-cpu to 2.18.1

Updates tensorflow to 2.18.1

Updates torch from 2.5.1+cu121 to 2.6.0

Updates torch-xla from 2.5.1 to 2.6.0

Updates tensorflow[and-cuda] to 2.18.1


updated-dependencies:

Signed-off-by: dependabot[bot] support@github.com Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

The ragged attribute exists only with KerasTensors.

Minor fix of a unit tests that was using the same local variable for two nested loops.

Without the need to also override compute_output_spec.

If explicitly overwriting a variable._layout, we want to keep this layout in any future calls. This allows auxiliary variables (e.g. optimizer gradients, momentums) to use the same explicit layout.

If overwriting, the gradient represents the desired final value of the variable, so if we did scale it, we're changing that value.

For folks who are used to the old format, this will print instructions. And for people like me, saves needing to remember SKIP=api-gen pre-commit run --all-files When I just want the formatter. api_gen.py is too slow to run every time.

Unlike jax/torch/tensorflow which all vie for a certain cuda, I don't think openvino has trouble co-installing.

And without the basic requirements.txt will not give a working dev environment. You can't run pre-commit without openvino installed.

This reverts commit 2b880fa3a3c47650fdbd32ebc98005fa1949e887.

We also want it for cuda installations.

They have been broken for a month

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com

Signed-off-by: 11happy soni5happy@gmail.com


Signed-off-by: 11happy soni5happy@gmail.com

The outer LossScaleOptimizer ignores the inner's loss-scale factor when scaling the loss. When computing unscaled gradients, we therefore need to eliminate the inner's loss scale factor, otherwise the gradients get incorrectly scaled.

updated the requires_trainable_backend decorator to use in operator for checking backend values.

Modified compile() API Code.

If variable.overwrite_with_gradient == True, then the only optimizer variable ever used for that variable is base_optimizer._accumulated_gradients. All other optimizer variables are unused. This can be extremely wasteful if the training variables are large, for example in the case of large embedding tables that span multiple hosts/devices.

Added a convenience function in the base optimizer add_optimizer_variables(...) that loops through the variable list and automatically adds a variable only if appropriate. If a variable would otherwise be unused, a None is inserted into the list. This is needed to keep optimizer._get_variable_index() consistent. Updated all built-in optimizers to use this.

NOTE: if a custom optimizer that exists out in the wild still does create unused optimizer variables, the optimizer should still work - it will just be wasteful. IOW this should not be a breaking change.

Bumps the github-actions group with 1 update: [github/codeql-ac…