code: def reduce_to_samplewise_values(
      values, sample_weight, reduce_fn, dtype
  ):
      mask = getattr(values, "_keras_mask", None)
      values = ops.cast(values, dtype=dtype)
      if sample_weight is not None:
          sample_weight = ops.cast(sample_weight, dtype=dtype)
          if mask is not None:
              sample_weight = losses.loss.apply_mask(
                  sample_weight, mask, dtype=dtype, reduction="sum"
              )
          # Update dimensions of weights to match with values if possible.
          values, sample_weight = losses.loss.squeeze_to_same_rank(
              values, sample_weight
          )
          # Reduce values to same ndim as weight array
          weight_ndim = len(sample_weight.shape)
          values_ndim = len(values.shape)
          if values_ndim > weight_ndim:
              values = reduce_fn(
                  values, axis=list(range(weight_ndim, values_ndim))
              )
          values = values * sample_weight
          if values_ndim > 1:
              sample_weight = reduce_fn(
                  sample_weight, axis=list(range(1, weight_ndim))
              )
  
      values_ndim = len(values.shape)
      if values_ndim > 1:
          values = reduce_fn(values, axis=list(range(1, values_ndim)))
          return values, sample_weight
      return values, sample_weight
  
imports: from keras import backend
  from keras import initializers
  from keras import losses
  from keras import ops
  from keras.api_export import keras_export
  from keras.metrics.metric import Metric
  from keras.saving import serialization_lib
label_desc: Use image node_id 1 for calling a global function with example usage: reduce_to_samplewise_values(values, sample_weight, reduce_fn, dtype) and returns: values,  sample_weight, values,  sample_weight