class_docstr: Quantize weights while preserving clusters.
docstr: Extracts the cluster centroids and cluster indices.
  
  Extracts cluster centroids and cluster indices from the pretrained
  clustered model when the input layer is clustered.
  
  Args:
    name: Name of weights in layer.
    layer: Quantization wrapped keras layer.
  Returns:
    A dictionary of the initial values of the
    cluster centroids, cluster indices, original weights,
    the pretrained flag for marking the first training
    epoch, and weight name.
code: def _build_clusters(self, name, layer):
      """Extracts the cluster centroids and cluster indices.
  
      Extracts cluster centroids and cluster indices from the pretrained
      clustered model when the input layer is clustered.
  
      Args:
        name: Name of weights in layer.
        layer: Quantization wrapped keras layer.
      Returns:
        A dictionary of the initial values of the
        cluster centroids, cluster indices, original weights,
        the pretrained flag for marking the first training
        epoch, and weight name.
      """
      result = {}
      weights = getattr(layer.layer, name)
      if self.preserve_sparsity and not tf.reduce_any(weights == 0):
          self.preserve_sparsity = False
          logging.warning(
              "Input layer does not contain zero weights, so apply CQAT instead."
          )
      centroids_mask = None
  
      # Detects whether layer is convolutional and is clustered per channel
      data_format = getattr(layer.layer, "data_format", None)
      (
          centroids,
          num_centroids,
          lookup,
          cluster_per_channel,
      ) = get_centroids(layer, weights, data_format)
  
      if self.preserve_sparsity:
          sparsity_mask = tf.math.divide_no_nan(weights, weights)
          zero_idx = tf.argmin(tf.abs(centroids), axis=-1)
          centroids_mask = 1.0 - tf.one_hot(zero_idx, num_centroids)
          result = {SPARSITY_MASK: sparsity_mask}
  
      # Prepare clustering variables for the Keras graph when clusters
      # exist, assuming we do not use number_of_clusters larger than 1024
      if num_centroids > 1024:
          warnings.warn(
              f"No clustering performed on layer {layer.name}.\n"
              f"Too many centroids to cluster."
          )
          return result
      # If not enough clusters, we do not preserve clustering
      elif num_centroids <= 1:
          warnings.warn(
              f"No clustering performed on layer {layer.name}.\n"
              f"Perhaps too many clusters requested for this layer?"
          )
          return result
      else:
          clst_centroids_tf = layer.add_weight(
              CLUSTER_CENTROIDS,
              shape=centroids.shape,
              initializer=tf.keras.initializers.Constant(
                  value=K.batch_get_value([centroids])[0]
              ),
              dtype=centroids.dtype,
              trainable=True,
          )
  
          ori_weights_tf = layer.add_weight(
              ORIGINAL_WEIGHTS,
              shape=weights.shape,
              initializer=tf.keras.initializers.Constant(
                  value=K.batch_get_value([weights])[0]
              ),
              dtype=weights.dtype,
              trainable=True,
          )
  
          # Get clustering implementation according to layer type
          clustering_impl_cls = clustering_registry.ClusteringLookupRegistry().get_clustering_impl(
              layer.layer, name, cluster_per_channel=cluster_per_channel
          )
          clustering_impl = clustering_impl_cls(
              clst_centroids_tf,
              cluster_config.GradientAggregation.SUM,
              data_format,
          )
  
          pulling_indices = tf.dtypes.cast(
              clustering_impl.get_pulling_indices(ori_weights_tf),
              lookup.dtype,
          )
  
          pulling_indices_tf = layer.add_weight(
              PULLING_INDICES,
              shape=lookup.shape,
              initializer=tf.keras.initializers.Constant(
                  value=K.batch_get_value([pulling_indices])[0]
              ),
              dtype=lookup.dtype,
              trainable=False,
          )
  
          result_clst = {
              CLUSTER_CENTROIDS: clst_centroids_tf,
              PULLING_INDICES: pulling_indices_tf,
              ORIGINAL_WEIGHTS: ori_weights_tf,
              WEIGHT_NAME: name,
              CLUSTERING_IMPL: clustering_impl,
              CENTROIDS_MASK: centroids_mask,
          }
          result.update(result_clst)
          return result
  
imports: import logging
  import warnings
  import tensorflow
  from tensorflow_model_optimization.python.core.clustering.keras import cluster_config
  from tensorflow_model_optimization.python.core.clustering.keras import clustering_registry
  from tensorflow_model_optimization.python.core.quantization.keras import quant_ops
  from tensorflow_model_optimization.python.core.quantization.keras import quantizers
  from tensorflow_model_optimization.python.core.quantization.keras.default_8bit import default_8bit_quantize_registry
  from tensorflow_model_optimization.python.core.quantization.keras.default_8bit import default_8bit_quantizers
label_desc: Use image node_id 2 for calling the ClusterPreserveDefaultWeightsQuantizer obj's underlying member method code with example usage: obj._build_clusters(name, layer) and returns: result, result, result