def _set_weights(
    instance, symbolic_weights, weight_values, name, skip_mismatch=False
):
    """Safely set weights into a model or a layer.

    Args:
        instance: Model or layer instance,
        symbolic_weights: symbolic tensors representing
                        the weights of the variables to load,
        weight_values: values of the weights to load,
        skip_mismatch: Boolean, whether to skip loading of weights
            where there is a mismatch in the shape of the weights,
        name: name used to identify the group.

    Raises:
        ValueError: in case of mismatch between provided
            model/layer and weights.
    """
    for i, weight_value in enumerate(weight_values):
        expected_shape = symbolic_weights[i].shape
        received_shape = weight_value.shape
        if expected_shape != received_shape:
            if skip_mismatch:
                warnings.warn(
                    f"Skipping loading weights for {name}"
                    f"due to mismatch in shape for "
                    f"weight {symbolic_weights[i].path}. "
                    f"Weight expects shape {expected_shape}. "
                    "Received saved weight "
                    f"with shape {received_shape}",
                    stacklevel=2,
                )
                continue
            raise ValueError(
                f"Shape mismatch in {name}"
                f"for weight {symbolic_weights[i].path}. "
                f"Weight expects shape {expected_shape}. "
                "Received saved weight "
                f"with shape {received_shape}"
            )
        symbolic_weights[i].assign(weight_value)

    if hasattr(instance, "finalize_state") and symbolic_weights:
        instance.finalize_state()
Safely set weights into a model or a layer.

    Args:
        instance: Model or layer instance,
        symbolic_weights: symbolic tensors representing
                        the weights of the variables to load,
        weight_values: values of the weights to load,
        skip_mismatch: Boolean, whether to skip loading of weights
            where there is a mismatch in the shape of the weights,
        name: name used to identify the group.

    Raises:
        ValueError: in case of mismatch between provided
            model/layer and weights.
    
keras/src/legacy/saving/legacy_h5_format.py