code: def apply_mask(sample_weight, mask, dtype, reduction):
      """Applies any mask on predictions to sample weights."""
      if mask is not None:
          mask = ops.cast(mask, dtype=dtype)
          if reduction == "sum_over_batch_size":
              # Valid entries have weight `total/valid`, while invalid ones
              # have 0. When summed over batch, they will be reduced to:
              #
              # mean(loss * sample_weight * total / valid)
              #   = sum(loss * sample_weight * total / valid) / total
              #   = sum(loss * sample_weight) / total * total / valid
              #   = sum(loss * sample_weight) / valid
              total = ops.cast(
                  ops.prod(
                      ops.convert_to_tensor(
                          ops.shape(mask), dtype="int32"
                      )
                  ),
                  dtype,
              )
              valid = ops.sum(mask)  # May be 0!
              mask *= total / (valid + backend.epsilon())
  
          if sample_weight is not None:
              sample_weight = ops.cast(sample_weight, dtype=dtype)
              mask, sample_weight = squeeze_to_same_rank(
                  mask, sample_weight
              )
              sample_weight *= mask
          else:
              sample_weight = mask
      return sample_weight
  
file_path: keras/keras/losses/loss.py
imports: import tree
  from keras import backend
  from keras import ops
  from keras.api_export import keras_export
  from keras.utils.naming import auto_name
label_desc: Use image node_id 5 for calling a global function with example usage: apply_mask(sample_weight, mask, dtype, reduction) and returns: sample_weight