code: def compress_mean(x, dim, compression_factor):
    
    dims = x.shape.dims
    pos = dims.index(dim)
    compressed_dim = mtf.Dimension(dim.name, dim.size // compression_factor)
    compression_factor_dim = mtf.Dimension(
        "compression_factor", compression_factor)
    new_shape = (
        dims[:pos] + [compressed_dim, compression_factor_dim] + dims[pos + 1:])
    x = mtf.reshape(x, new_shape)
    x = mtf.reduce_mean(x, reduced_dim=compression_factor_dim)
    return x
docs: Compress by taking group means.
  
    Args:
      x: a Tensor
      dim: a dimension in x.shape
      compression_factor: an integer
  
    Returns:
      a Tensor