keras/src/layers/layer.py/Layer/_set_mask_metadata
class Layer:    def _set_mask_metadata(self, inputs, outputs, previous_mask):
        flat_outputs = tree.flatten(outputs)

        mask_already_computed = all(
            getattr(x, "_keras_mask", None) is not None for x in flat_outputs
        )
        if mask_already_computed:
            return

        output_masks = self.compute_mask(inputs, previous_mask)
        if output_masks is None:
            return

        flat_masks = tree.flatten(output_masks)
        for tensor, mask in zip(flat_outputs, flat_masks):
            if getattr(tensor, "_keras_mask", None) is None:
                try:
                    # Numpy backend does not support masking.
                    if backend.backend() == "numpy":
                        warnings.warn(
                            "The NumPy backend does not support masking at this"
                            "time. Masks will be ignored."
                        )
                    tensor._keras_mask = mask
                except AttributeError:
                    # It's a C type.
                    pass