def _cluster_weights_1d(
        cls,
        block_weight: _torch.Tensor,
        block_importance: _torch.Tensor,
        config: KMeansConfig,
    ) -> _Tuple[_torch.Tensor, _torch.Tensor]:
        """
        Cluster weights such that each centroid is a 1d scalar, i.e., cluster_dim == 1.
        """
        num_clusters = 2**config.n_bits

        block_weight_flatten = block_weight.flatten().numpy()
        if block_importance is not None:
            block_importance_flatten = block_importance.flatten().numpy()
            kmeans_results = _kmeans1d.cluster(
                block_weight_flatten,
                num_clusters,
                weights=block_importance_flatten,
            )
        elif (block_weight_flatten.dtype == _np.float16) or (
            config.enable_fast_kmeans_mode
            and _np.max(block_weight_flatten) <= _np.finfo(_np.float16).max
            and _np.min(block_weight_flatten) >= _np.finfo(_np.float16).min
        ):
            # With fp16 values we often have a reduced amount of unique values
            # and performing weighted kmeans becomes much faster

            # Add rounding before computing unique values to further reduce clustered weight size
            if config.enable_fast_kmeans_mode:
                # Cast fp32 -> fp16
                if block_weight_flatten.dtype != _np.float16:
                    block_weight_flatten = block_weight_flatten.astype(_np.float16)

                # Rounding
                scale = 10**config.rounding_precision
                block_weight_flatten = (
                    _np.round(block_weight_flatten.astype(_np.float32) * scale) / scale
                )

            # To speed up parallel kmeans, use numpy.unique instead of
            # torch.unique in multiprocessing setting.
            values, indices, counts = _np.unique(
                block_weight_flatten,
                return_inverse=True,
                return_counts=True,
            )
            num_clusters = min(len(values), num_clusters)
            kmeans_results = _kmeans1d.cluster(values, num_clusters, weights=counts)
            # Expand clusters according to np.unique indices
            # XXX: kmeans_results is a namedtuple, which is why we use this constructor
            kmeans_results = type(kmeans_results)(
                clusters=_np.array(kmeans_results.clusters)[indices].tolist(),
                centroids=kmeans_results.centroids,
            )
        else:
            kmeans_results = _kmeans1d.cluster(block_weight_flatten, num_clusters)

        # First create numpy array from list and then tensor from numpy array.
        # This is much faster than creating tensor from list.
        centroids = _torch.from_numpy(_np.array(kmeans_results.centroids))
        clusters = _torch.from_numpy(_np.array(kmeans_results.clusters))
        return centroids, clusters

        Cluster weights such that each centroid is a 1d scalar, i.e., cluster_dim == 1.
        
coremltools/optimize/torch/_utils/k_means.py