tensorflow / tensorflow

An Open Source Machine Learning Framework for Everyone
https://tensorflow.org
Apache License 2.0
186.39k stars 74.31k forks source link

AutoGraph error: OP_REQUIRES failed at strided_slice_op.cc:266 #75269

Closed richardwhitehead closed 1 month ago

richardwhitehead commented 2 months ago

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

No

Source

binary

TensorFlow version

2.16.1

Custom code

Yes

OS platform and distribution

Linux Ubuntu 20.04

Mobile device

No response

Python version

3.10.12

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

The following code works and produces the expected output when run in eager mode, but when using AutoGraph it produces bizarre error messages. The line numbers reported are not the actual location of the errors.

Replacing the contents of _safe_lookup_pixel so it always returns zeros makes the error go away, though of course the output is not then correct.

My installation is using docker image based on the NVIDIA image, details below.

Standalone code to reproduce the issue

import keras
import tensorflow as tf
import numpy as np

class UnAveragePooling2D(keras.layers.Layer):

    def __init__(self, kernel, *, strides, name = 'co1', dtype = None):
        super().__init__(trainable=True, name=name, dtype=dtype)
        self.kernel = kernel
        self.strides = strides

    def build(self, input_shape):
        pass

    def call(self, inputs):
        inputs_shape = tf.shape(inputs)

        # the averaged image had a minimum size of source.shape * strides, but it could have been larger if its size
        # was not a multiple of stride. For now, assume it was a multiple of stride.
        dest_shape = (inputs_shape[0], inputs.shape[1] * self.strides, inputs.shape[2] * self.strides, inputs_shape[3])
        dest = tf.zeros(dest_shape)

        # width of destination invalid edges that need to be filled, again assuming that source size was a multiple of stride.
        dest_start_invalid = (self.strides // 2, self.strides // 2)
        dest_end_invalid = (self.strides // 2, self.strides // 2)

        strides_f = tf.cast(self.strides, tf.float32)
        dest_start_invalid_f = (tf.cast(dest_start_invalid[0], tf.float32), tf.cast(dest_start_invalid[1], tf.float32))
        max_source_f = (tf.cast(inputs_shape[1] - 1, tf.float32), tf.cast(inputs_shape[2] - 1, tf.float32))

        # biniliear interpolation with distorted edges
        for dest_r in range(dest_shape[1]):
            for dest_c in range(dest_shape[2]):
                source_r = self._dest_to_rource(dest_r, strides_f, dest_start_invalid_f[0], max_source_f[0])
                source_c = self._dest_to_rource(dest_c, strides_f, dest_start_invalid_f[1], max_source_f[1])
                value = self._bilinear_interpolate(inputs, source_r, source_c)
                dest = self._assign_pixel_batch_values(dest, dest_r, dest_c, value)

        # # nearest edge fill
        # first_valid_row = tf.reshape(dest[:, dest_start_invalid[0], :, :], (dest_shape[0], 1, dest_shape[2], dest_shape[3]))
        # last_valid_row = tf.reshape(dest[:, -dest_end_invalid[0] - 1, :, :], (dest_shape[0], 1, dest_shape[2], dest_shape[3]))
        # start_rows = tf.tile(first_valid_row, (1, dest_start_invalid[0], 1, 1))
        # middle_rows = dest[:, dest_start_invalid[0]:-dest_end_invalid[0], :, :]
        # end_rows = tf.tile(last_valid_row, (1, dest_end_invalid[0], 1, 1))

        # dest = tf.concat([start_rows, middle_rows, end_rows], axis=1)
        # first_valid_col = tf.reshape(dest[:, :, dest_start_invalid[1], :], (dest_shape[0], dest_shape[1], 1, dest_shape[3]))
        # last_valid_col = tf.reshape(dest[:, :, -dest_end_invalid[1] - 1, :], (dest_shape[0], dest_shape[1], 1, dest_shape[3]))
        # start_cols = tf.tile(first_valid_col, (1, 1, dest_start_invalid[1], 1))
        # middle_cols = dest[:, :, dest_start_invalid[1]:-dest_end_invalid[1], :]
        # end_cols = tf.tile(last_valid_col, (1, 1, dest_end_invalid[1], 1))
        # dest = tf.concat([start_cols, middle_cols, end_cols], axis=2)

        return dest

    @tf.function
    def _dest_to_rource(self, dest, stride, dest_start_invalid, max_source):
        """Given a destination pixel row or column position, work out the source
        pixel location from which the value should be interpolated."""

        if dest < dest_start_invalid + stride - 0.5:
            return (dest - dest_start_invalid) / (stride - 0.5)
        elif dest > dest_start_invalid + (max_source - 1.0) * stride - 0.5:
            return ((dest - dest_start_invalid + 0.5) - (max_source - 1.0) * stride) / (stride - 0.5) + max_source - 1.0
        else:
            return (dest - dest_start_invalid + 0.5) / stride

    @tf.function
    def _bilinear_interpolate(self, source, r, c):
        """Given a batch of source images, interpolate each from the four pixels surrounding point r,c.
        Near the edge, fade to black.
        """
        # Algorithm (ignoring edges):
        # 1. Round r,c down to get top-right source pixel r0, c0
        # 2. Subtract r0, c0 from r, c get 0..1 proportions of a pixel fr, fc
        # 3. The four pixels to be sampled are at p00=[r0, c0], p01=[r0,c0+1], p10=[r0+1, c0], and p11=[r0+1, c0+1]
        # 4. For each channel, calculate a linear sum of the four pixels, as follows:
        # 5. result =   p00.(1-fr).(1-fc)
        #             + p01.(1-fr).fc
        #             + p10.fr.(1-fc)
        #             + p11.fr.fc
        # To cope with edges and implement fade-to-black: if any of p00, p01, p10 or p11 would be outside the source
        # image then use black instead
        r0, c0 =  tf.cast(tf.floor(r), tf.int32), tf.cast(tf.floor(c), tf.int32)
        fr, fc = r - tf.cast(r0, tf.float32), c - tf.cast(c0, tf.float32)
        p00 = self._safe_lookup_pixel(source, r0, c0)
        p01 = self._safe_lookup_pixel(source, r0, c0+1)
        p10 = self._safe_lookup_pixel(source, r0+1, c0)
        p11 = self._safe_lookup_pixel(source, r0+1, c0+1)
        return p00*(1-fr)*(1-fc) + p01*(1-fr)*fc + p10*fr*(1-fc) + p11*fr*fc

    @tf.function
    def _safe_lookup_pixel(self, source, r, c):
        """If x,y is a valid index into the source images then return the
        batch of pixels at that location, otherwise return a batch of black pixels."""

        source_shape = tf.shape(source)
        if 0 <= r < source_shape[1] and 0 <= c < source_shape[2]:
            return source[:, r, c, :]
        else:
            return tf.zeros((source_shape[0], source_shape[3]))

    @staticmethod
    @tf.function
    def _assign_pixel_batch_values(tensor, r, c, value):
        """Given a 4d tensor, replace the batch of pixels at location r,c with the given batch of pixels.
        Equivalent to tensor[:, r, c, :] = value
        """

        # There has got to be an easier way!
        tensor_shape = tensor.shape
        ret = tensor[:, :r, :, :]  # rows before the update
        new_row = tf.reshape(tensor[:, r, :c, :], (tensor_shape[0], 1, c, tensor_shape[3])) # columns before the update, on the update row
        value = tf.reshape(value, (tensor_shape[0], 1, 1, tensor_shape[3]))
        new_row = tf.concat([new_row, value], axis=2)   # append the new value
        last_cols = tf.reshape(tensor[:, r, c+1:, :], (tensor_shape[0], 1, tensor_shape[2] - c - 1, tensor_shape[3]))
        new_row = tf.concat([new_row, last_cols], axis=2) # columns after the update, on the update row
        ret = tf.concat([ret, new_row], axis=1)  # the now row with the update in it
        ret = tf.concat([ret, tensor[:, r+1:, :, :]], axis=1) # rows after the update
        return ret

TEST_SIZE = 16

data = [list(range(TEST_SIZE))] * TEST_SIZE
data = tf.convert_to_tensor(data, np.float32)
data = tf.reshape(data, (1, TEST_SIZE, TEST_SIZE, 1))

model = keras.models.Sequential()
model.add(keras.layers.AveragePooling2D((6, 6), padding='same', strides=4))
model.add(UnAveragePooling2D((6, 6), strides=4))
model.compile(run_eagerly=False)

expanded = model.predict(data)
print(expanded[0,:,:,0])

Dockerfile:

FROM nvcr.io/nvidia/tensorflow:24.06-tf2-py3 AS base_image
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
    wget \
    build-essential \
    cmake \
    git \
    unzip \
    pkg-config
WORKDIR /environment
RUN pip install --upgrade pip
RUN pip install -U numpy protobuf tensorflow-datasets tensorflow_graphics keras scikit-learn scikit-image pandas seaborn jupyter keras-tuner openml opencv-python typing-extensions pydot graphviz data-science-types adjustText
RUN apt-get update && apt-get install -y \
    graphviz
EXPOSE 8888
ENTRYPOINT ["jupyter", "notebook", "--no-browser","--ip=0.0.0.0"]

### Relevant log output

```shell
2024-09-06 17:35:56.430840: W tensorflow/core/framework/op_kernel.cc:1839] OP_REQUIRES failed at strided_slice_op.cc:266 : INVALID_ARGUMENT: slice index 4 of dimension 1 out of bounds.

Stack trace for op definition: 
File "usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
File "usr/lib/python3.10/runpy.py", line 86, in _run_code
File "usr/local/lib/python3.10/dist-packages/ipykernel_launcher.py", line 18, in <module>
File "usr/local/lib/python3.10/dist-packages/traitlets/config/application.py", line 1043, in launch_instance
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py", line 739, in start
File "usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py", line 205, in start
File "usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
File "usr/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
File "usr/lib/python3.10/asyncio/events.py", line 80, in _run
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 545, in dispatch_queue
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 534, in process_one
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 437, in dispatch_shell
File "usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 362, in execute_request
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 778, in execute_request
File "usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 449, in do_execute
File "usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 549, in run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3051, in run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3106, in _run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3311, in run_cell_async
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3493, in run_ast_nodes
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
File "tmp/ipykernel_2107/74085429.py", line 137, in <module>
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 508, in predict
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 208, in one_step_on_data_distributed
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 198, in one_step_on_data
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 96, in predict_step
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 807, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 1315, in _maybe_build
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 225, in build_wrapper
File "usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py", line 183, in build
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 882, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 46, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 156, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 58, in symbolic_call
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 1047, in compute_output_spec
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 78, in compute_output_spec
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/core.py", line 200, in compute_output_spec
File "tmp/ipykernel_2107/74085429.py", line 38, in call
File "tmp/ipykernel_2107/3701247216.py", line 88, in _bilinear_interpolate
File "tmp/ipykernel_2107/2303768048.py", line 100, in _safe_lookup_pixel
File "tmp/ipykernel_2107/2303768048.py", line 101, in _safe_lookup_pixel

2024-09-06 17:35:56.430922: W tensorflow/core/framework/op_kernel.cc:1839] OP_REQUIRES failed at if_op.cc:269 : INVALID_ARGUMENT: slice index 4 of dimension 1 out of bounds.

Stack trace for op definition: 
File "usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
File "usr/lib/python3.10/runpy.py", line 86, in _run_code
File "usr/local/lib/python3.10/dist-packages/ipykernel_launcher.py", line 18, in <module>
File "usr/local/lib/python3.10/dist-packages/traitlets/config/application.py", line 1043, in launch_instance
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py", line 739, in start
File "usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py", line 205, in start
File "usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
File "usr/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
File "usr/lib/python3.10/asyncio/events.py", line 80, in _run
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 545, in dispatch_queue
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 534, in process_one
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 437, in dispatch_shell
File "usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 362, in execute_request
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 778, in execute_request
File "usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 449, in do_execute
File "usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 549, in run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3051, in run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3106, in _run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3311, in run_cell_async
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3493, in run_ast_nodes
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
File "tmp/ipykernel_2107/74085429.py", line 137, in <module>
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 508, in predict
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 208, in one_step_on_data_distributed
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 198, in one_step_on_data
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 96, in predict_step
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 807, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 1315, in _maybe_build
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 225, in build_wrapper
File "usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py", line 183, in build
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 882, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 46, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 156, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 58, in symbolic_call
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 1047, in compute_output_spec
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 78, in compute_output_spec
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/core.py", line 200, in compute_output_spec
File "tmp/ipykernel_2107/74085429.py", line 38, in call
File "tmp/ipykernel_2107/3701247216.py", line 88, in _bilinear_interpolate
File "tmp/ipykernel_2107/2303768048.py", line 100, in _safe_lookup_pixel
File "tmp/ipykernel_2107/2303768048.py", line 101, in _safe_lookup_pixel

     [[{{node cond_2/strided_slice}}]]
    tf2xla conversion failed while converting cond_2_true_603071_const_1002[]. Run with TF_DUMP_GRAPH_PREFIX=/path/to/dump/dir and --vmodule=xla_compiler=2 to obtain a dump of the compiled functions.

Stack trace for op definition: 
File "usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
File "usr/lib/python3.10/runpy.py", line 86, in _run_code
File "usr/local/lib/python3.10/dist-packages/ipykernel_launcher.py", line 18, in <module>
File "usr/local/lib/python3.10/dist-packages/traitlets/config/application.py", line 1043, in launch_instance
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py", line 739, in start
File "usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py", line 205, in start
File "usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
File "usr/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
File "usr/lib/python3.10/asyncio/events.py", line 80, in _run
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 545, in dispatch_queue
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 534, in process_one
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 437, in dispatch_shell
File "usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 362, in execute_request
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 778, in execute_request
File "usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 449, in do_execute
File "usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 549, in run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3051, in run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3106, in _run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3311, in run_cell_async
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3493, in run_ast_nodes
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
File "tmp/ipykernel_2107/74085429.py", line 137, in <module>
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 508, in predict
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 208, in one_step_on_data_distributed
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 198, in one_step_on_data
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 96, in predict_step
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 807, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 1315, in _maybe_build
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 225, in build_wrapper
File "usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py", line 183, in build
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 882, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 46, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 156, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 58, in symbolic_call
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 1047, in compute_output_spec
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 78, in compute_output_spec
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/core.py", line 200, in compute_output_spec
File "tmp/ipykernel_2107/74085429.py", line 38, in call
File "tmp/ipykernel_2107/3701247216.py", line 88, in _bilinear_interpolate
File "tmp/ipykernel_2107/2303768048.py", line 100, in _safe_lookup_pixel

2024-09-06 17:35:56.593132: W tensorflow/core/framework/op_kernel.cc:1839] OP_REQUIRES failed at xla_ops.cc:580 : INVALID_ARGUMENT: slice index 4 of dimension 1 out of bounds.

Stack trace for op definition: 
File "usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
File "usr/lib/python3.10/runpy.py", line 86, in _run_code
File "usr/local/lib/python3.10/dist-packages/ipykernel_launcher.py", line 18, in <module>
File "usr/local/lib/python3.10/dist-packages/traitlets/config/application.py", line 1043, in launch_instance
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py", line 739, in start
File "usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py", line 205, in start
File "usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
File "usr/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
File "usr/lib/python3.10/asyncio/events.py", line 80, in _run
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 545, in dispatch_queue
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 534, in process_one
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 437, in dispatch_shell
File "usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 362, in execute_request
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 778, in execute_request
File "usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 449, in do_execute
File "usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 549, in run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3051, in run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3106, in _run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3311, in run_cell_async
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3493, in run_ast_nodes
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
File "tmp/ipykernel_2107/74085429.py", line 137, in <module>
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 508, in predict
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 208, in one_step_on_data_distributed
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 198, in one_step_on_data
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 96, in predict_step
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 807, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 1315, in _maybe_build
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 225, in build_wrapper
File "usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py", line 183, in build
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 882, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 46, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 156, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 58, in symbolic_call
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 1047, in compute_output_spec
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 78, in compute_output_spec
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/core.py", line 200, in compute_output_spec
File "tmp/ipykernel_2107/74085429.py", line 38, in call
File "tmp/ipykernel_2107/3701247216.py", line 88, in _bilinear_interpolate
File "tmp/ipykernel_2107/2303768048.py", line 100, in _safe_lookup_pixel
File "tmp/ipykernel_2107/2303768048.py", line 101, in _safe_lookup_pixel

     [[{{node cond_2/strided_slice}}]]
    tf2xla conversion failed while converting cond_2_true_603071_const_1002[]. Run with TF_DUMP_GRAPH_PREFIX=/path/to/dump/dir and --vmodule=xla_compiler=2 to obtain a dump of the compiled functions.

Stack trace for op definition: 
File "usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
File "usr/lib/python3.10/runpy.py", line 86, in _run_code
File "usr/local/lib/python3.10/dist-packages/ipykernel_launcher.py", line 18, in <module>
File "usr/local/lib/python3.10/dist-packages/traitlets/config/application.py", line 1043, in launch_instance
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py", line 739, in start
File "usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py", line 205, in start
File "usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
File "usr/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
File "usr/lib/python3.10/asyncio/events.py", line 80, in _run
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 545, in dispatch_queue
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 534, in process_one
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 437, in dispatch_shell
File "usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 362, in execute_request
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 778, in execute_request
File "usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 449, in do_execute
File "usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 549, in run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3051, in run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3106, in _run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3311, in run_cell_async
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3493, in run_ast_nodes
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
File "tmp/ipykernel_2107/74085429.py", line 137, in <module>
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 508, in predict
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 208, in one_step_on_data_distributed
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 198, in one_step_on_data
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 96, in predict_step
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 807, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 1315, in _maybe_build
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 225, in build_wrapper
File "usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py", line 183, in build
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 882, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 46, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 156, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 58, in symbolic_call
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 1047, in compute_output_spec
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 78, in compute_output_spec
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/core.py", line 200, in compute_output_spec
File "tmp/ipykernel_2107/74085429.py", line 38, in call
File "tmp/ipykernel_2107/3701247216.py", line 88, in _bilinear_interpolate
File "tmp/ipykernel_2107/2303768048.py", line 100, in _safe_lookup_pixel

     [[sequential_75_1/co1_1/PartitionedCall_1002/PartitionedCall_2/cond_2]]
    tf2xla conversion failed while converting __inference_one_step_on_data_615470[]. Run with TF_DUMP_GRAPH_PREFIX=/path/to/dump/dir and --vmodule=xla_compiler=2 to obtain a dump of the compiled functions.
2024-09-06 17:35:56.593189: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: INVALID_ARGUMENT: slice index 4 of dimension 1 out of bounds.

Stack trace for op definition: 
File "usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
File "usr/lib/python3.10/runpy.py", line 86, in _run_code
File "usr/local/lib/python3.10/dist-packages/ipykernel_launcher.py", line 18, in <module>
File "usr/local/lib/python3.10/dist-packages/traitlets/config/application.py", line 1043, in launch_instance
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py", line 739, in start
File "usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py", line 205, in start
File "usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
File "usr/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
File "usr/lib/python3.10/asyncio/events.py", line 80, in _run
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 545, in dispatch_queue
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 534, in process_one
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 437, in dispatch_shell
File "usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 362, in execute_request
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 778, in execute_request
File "usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 449, in do_execute
File "usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 549, in run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3051, in run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3106, in _run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3311, in run_cell_async
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3493, in run_ast_nodes
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
File "tmp/ipykernel_2107/74085429.py", line 137, in <module>
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 508, in predict
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 208, in one_step_on_data_distributed
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 198, in one_step_on_data
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 96, in predict_step
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 807, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 1315, in _maybe_build
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 225, in build_wrapper
File "usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py", line 183, in build
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 882, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 46, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 156, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 58, in symbolic_call
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 1047, in compute_output_spec
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 78, in compute_output_spec
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/core.py", line 200, in compute_output_spec
File "tmp/ipykernel_2107/74085429.py", line 38, in call
File "tmp/ipykernel_2107/3701247216.py", line 88, in _bilinear_interpolate
File "tmp/ipykernel_2107/2303768048.py", line 100, in _safe_lookup_pixel
File "tmp/ipykernel_2107/2303768048.py", line 101, in _safe_lookup_pixel

     [[{{node cond_2/strided_slice}}]]
    tf2xla conversion failed while converting cond_2_true_603071_const_1002[]. Run with TF_DUMP_GRAPH_PREFIX=/path/to/dump/dir and --vmodule=xla_compiler=2 to obtain a dump of the compiled functions.

Stack trace for op definition: 
File "usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
File "usr/lib/python3.10/runpy.py", line 86, in _run_code
File "usr/local/lib/python3.10/dist-packages/ipykernel_launcher.py", line 18, in <module>
File "usr/local/lib/python3.10/dist-packages/traitlets/config/application.py", line 1043, in launch_instance
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py", line 739, in start
File "usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py", line 205, in start
File "usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
File "usr/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
File "usr/lib/python3.10/asyncio/events.py", line 80, in _run
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 545, in dispatch_queue
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 534, in process_one
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 437, in dispatch_shell
File "usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 362, in execute_request
File "usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 778, in execute_request
File "usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 449, in do_execute
File "usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 549, in run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3051, in run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3106, in _run_cell
File "usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3311, in run_cell_async
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3493, in run_ast_nodes
File "usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
File "tmp/ipykernel_2107/74085429.py", line 137, in <module>
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 508, in predict
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 208, in one_step_on_data_distributed
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 198, in one_step_on_data
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/trainer.py", line 96, in predict_step
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 807, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 1315, in _maybe_build
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 225, in build_wrapper
File "usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py", line 183, in build
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 882, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 117, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 46, in __call__
File "usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 156, in error_handler
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 58, in symbolic_call
File "usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 1047, in compute_output_spec
File "usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py", line 78, in compute_output_spec
File "usr/local/lib/python3.10/dist-packages/keras/src/backend/tensorflow/core.py", line 200, in compute_output_spec
File "tmp/ipykernel_2107/74085429.py", line 38, in call
File "tmp/ipykernel_2107/3701247216.py", line 88, in _bilinear_interpolate
File "tmp/ipykernel_2107/2303768048.py", line 100, in _safe_lookup_pixel

     [[sequential_75_1/co1_1/PartitionedCall_1002/PartitionedCall_2/cond_2]]
    tf2xla conversion failed while converting __inference_one_step_on_data_615470[]. Run with TF_DUMP_GRAPH_PREFIX=/path/to/dump/dir and --vmodule=xla_compiler=2 to obtain a dump of the compiled functions.
     [[PartitionedCall]]
richardwhitehead commented 2 months ago

Here is a different (and simpler) version of the code. This version works in AutoGraph mode in TensorFlow 2.13.1 on Python 3.8.10, but produces the above error when run in TensorFlow 2.16.1 on Python 3.10.12.

import keras
import tensorflow as tf
import numpy as np

class UnAveragePooling2D(keras.layers.Layer):

    def __init__(self, kernel, *, strides, name = 'co1', dtype = None):
        super().__init__(trainable=True, name=name, dtype=dtype)
        self.kernel = kernel
        self.strides = strides

    def build(self, input_shape):
        pass

    def call(self, inputs):
        inputs_shape = tf.shape(inputs)

        # the averaged image had a minimum size of source.shape * strides, but it could have been larger if its size
        # was not a multiple of stride. For now, assume it was a multiple of stride.
        dest_shape = (inputs_shape[0], inputs.shape[1] * self.strides, inputs.shape[2] * self.strides, inputs_shape[3])
        #dest = tf.zeros(dest_shape)

        # width of destination invalid edges that need to be filled, again assuming that source size was a multiple of stride.
        dest_start_invalid = (self.strides // 2, self.strides // 2)
        dest_end_invalid = (self.strides // 2, self.strides // 2)

        strides_f = tf.cast(self.strides, tf.float32)
        dest_start_invalid_f = (tf.cast(dest_start_invalid[0], tf.float32), tf.cast(dest_start_invalid[1], tf.float32))
        max_source_f = (tf.cast(inputs.shape[1] - 1, tf.float32), tf.cast(inputs.shape[2] - 1, tf.float32))

        # biniliear interpolation with distorted edges
        dest = tf.zeros((dest_shape[0], 0, dest_shape[2], dest_shape[3]))
        for dest_r in range(dest_shape[1]):
            dest_r_f = tf.cast(dest_r, tf.float32)
            row = tf.zeros((dest_shape[0], 1, 0, dest_shape[3]))
            for dest_c in range(dest_shape[2]):
                dest_c_f = tf.cast(dest_c, tf.float32)
                source_r = self._dest_to_rource(dest_r_f, strides_f, dest_start_invalid_f[0], max_source_f[0])
                source_c = self._dest_to_rource(dest_c_f, strides_f, dest_start_invalid_f[1], max_source_f[1])
                value = self._bilinear_interpolate(inputs, source_r, source_c)
                #dest = self._assign_pixel_batch_values(dest, dest_r, dest_c, value)
                value = tf.reshape(value, (inputs_shape[0], 1, 1, inputs_shape[3]))
                row = tf.concat([row, value], axis=2)
            dest = tf.concat([dest, row], axis=1)

        return dest

    @tf.function
    def _dest_to_rource(self, dest, stride, dest_start_invalid, max_source):
        """Given a destination pixel row or column position, work out the source
        pixel location from which the value should be interpolated."""

        if dest < dest_start_invalid + stride - 0.5:
            return (dest - dest_start_invalid) / (stride - 0.5)
        elif dest > dest_start_invalid + (max_source - 1.0) * stride - 0.5:
            return ((dest - dest_start_invalid + 0.5) - (max_source - 1.0) * stride) / (stride - 0.5) + max_source - 1.0
        else:
            return (dest - dest_start_invalid + 0.5) / stride

    @tf.function
    def _bilinear_interpolate(self, source, r, c):
        """Given a batch of source images, interpolate each from the four pixels surrounding point r,c.
        Near the edge, fade to black.
        """
        # Algorithm (ignoring edges):
        # 1. Round r,c down to get top-right source pixel r0, c0
        # 2. Subtract r0, c0 from r, c get 0..1 proportions of a pixel fr, fc
        # 3. The four pixels to be sampled are at p00=[r0, c0], p01=[r0,c0+1], p10=[r0+1, c0], and p11=[r0+1, c0+1]
        # 4. For each channel, calculate a linear sum of the four pixels, as follows:
        # 5. result =   p00.(1-fr).(1-fc)
        #             + p01.(1-fr).fc
        #             + p10.fr.(1-fc)
        #             + p11.fr.fc
        # To cope with edges and implement fade-to-black: if any of p00, p01, p10 or p11 would be outside the source
        # image then use black instead
        r0, c0 =  tf.cast(tf.floor(r), tf.int32), tf.cast(tf.floor(c), tf.int32)
        fr, fc = r - tf.cast(r0, tf.float32), c - tf.cast(c0, tf.float32)
        p00 = self._safe_lookup_pixel(source, r0, c0)
        p01 = self._safe_lookup_pixel(source, r0, c0+1)
        p10 = self._safe_lookup_pixel(source, r0+1, c0)
        p11 = self._safe_lookup_pixel(source, r0+1, c0+1)
        return p00*(1-fr)*(1-fc) + p01*(1-fr)*fc + p10*fr*(1-fc) + p11*fr*fc

    @tf.function
    def _safe_lookup_pixel(self, source, r, c):
        """If x,y is a valid index into the source images then return the
        batch of pixels at that location, otherwise return a batch of black pixels."""

        source_shape = tf.shape(source)
        if 0 <= r < source.shape[1] and 0 <= c < source.shape[2]:
            return source[:, r, c, :]
        else:
            return tf.zeros((source_shape[0], source_shape[3]))

TEST_SIZE = 16

data = [list(range(TEST_SIZE))] * TEST_SIZE
data = tf.convert_to_tensor(data, np.float32)
data = tf.reshape(data, (1, TEST_SIZE, TEST_SIZE, 1))

model = keras.models.Sequential()
model.add(keras.Input(shape=(TEST_SIZE, TEST_SIZE, 1), batch_size=1))
model.add(keras.layers.AveragePooling2D((6, 6), padding='same', strides=4))
model.add(UnAveragePooling2D((6, 6), strides=4))
model.compile(run_eagerly=False)

expanded = model.predict(data)
print(expanded[0,:,:,0])
tilakrayal commented 1 month ago

@richardwhitehead, I tried to execute the code on the latest tensorflow v2.17 and it was executed without any issues. Kindly find the gist of it here and try to update to 2.17. Thank you!

richardwhitehead commented 1 month ago

Thank you very much for investigating this.

I tried updating my docker container to the latest version provided by NVIDIA (24.09-tf2-py3) but that still seems to be TensorFlow v2.16.1.

Having lost many days fighting version incompatibilities in the past, and given that Nvidia has chosen not to upgrade yet, I am unwilling to try updating TensorFlow and will wait until Nvidia release a container image with a newer version.

I have no doubt that what you say is true and so please feel free to close the issue.

Many thanks,

Richard

From: tilakrayal @.> Sent: 25 September 2024 15:45 To: tensorflow/tensorflow @.> Cc: richardwhitehead @.>; Mention @.> Subject: Re: [tensorflow/tensorflow] AutoGraph error: OP_REQUIRES failed at strided_slice_op.cc:266 (Issue #75269)

@richardwhitehead https://github.com/richardwhitehead , I tried to execute the code on the latest tensorflow v2.17 and it was executed without any issues. Kindly find the gist of it here https://colab.research.google.com/gist/tilakrayal/08d84020487a9abf51ada4c68c8528e2/untitled2127.ipynb and try to update to 2.17. Thank you!

— Reply to this email directly, view it on GitHub https://github.com/tensorflow/tensorflow/issues/75269#issuecomment-2374305020 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AFPFJLO55UDEAFUZKEUQPRLZYLD7HAVCNFSM6AAAAABNZAI4P2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNZUGMYDKMBSGA . You are receiving this because you were mentioned. https://github.com/notifications/beacon/AFPFJLI33D7PIOQGCMFSAGDZYLD7HA5CNFSM6AAAAABNZAI4P2WGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTUNQUCPY.gif Message ID: @. @.> >

tilakrayal commented 1 month ago

@richardwhitehead, Glad the issue was resolved. Could you please feel free to move this issue to closed status. Thank you!

richardwhitehead commented 1 month ago

Fixed in v2.17

google-ml-butler[bot] commented 1 month ago

Are you satisfied with the resolution of your issue? Yes No