code: def _encode_all_memory_utilization_optimized(
      encoder: "TextEncoder",
      labels: Sequence[str],
      batch_size: int,
  ) -> torch.Tensor:
      """
      Encode all labels with the given batch-size.
  
      Wrapped by memory utilization maximizer to automatically reduce the batch size if needed.
  
      :param encoder:
          the encoder
      :param labels:
          the labels to encode
      :param batch_size:
          the batch size to use. Will automatically be reduced if necessary.
  
      :return: shape: `(len(labels), dim)`
          the encoded labels
      """
      return torch.cat(
          [
              encoder(batch)
              for batch in chunked(
                  tqdm(map(str, labels), leave=False), batch_size
              )
          ],
          dim=0,
      )
  
imports: import logging
  import string
  from abc import abstractmethod
  from typing import TYPE_CHECKING, Callable, Optional, Sequence, Union
  import torch
  from class_resolver import ClassResolver, Hint, HintOrType
  from class_resolver.contrib.torch import aggregation_resolver
  from more_itertools import chunked
  from torch import nn
  from torch_max_mem import maximize_memory_utilization
  from tqdm.auto import tqdm
  from ..utils import get_preferred_device, resolve_device, upgrade_to_sequence
label_desc: Use image node_id 1 for calling a global function with example usage: _encode_all_memory_utilization_optimized(encoder, labels, batch_size) and returns: torch