def _pack_trimmed_examples(
        self,
        trimmed_examples: list[dict[str, torch.Tensor]],
        sizes: list[int],
    ) -> Optional[dict[str, torch.Tensor]]:
        """Packs trimmed examples into fixed-size bins and repads them.

        Args:
            trimmed_examples (List[Dict[str, torch.Tensor]]): A list of trimmed examples.
            sizes (List[int]): The sizes of the trimmed examples.

        Returns:
            Optional[Dict[str, torch.Tensor]]: A batch of repadded examples
                ready for processing. If the collator is in profiling mode,
                returns None.
        """
        # Apply our CS 101 bin packing algorithm.
        packed_examples, n_packed_tokens, n_total_tokens, leftover_bins = self._first_fit_bin_packing(
            sizes=sizes,
            examples=trimmed_examples,
            num_bins=self.out_size,
            max_bin_size=self.max_seq_len,
            existing_bins=self._leftover_bins,
        )
        self.n_packed_tokens += n_packed_tokens
        self.n_total_tokens += n_total_tokens
        self.n_packed_examples += self.out_size
        self._leftover_bins = leftover_bins[:self.max_leftover_bins_to_keep]

        if self._is_profiling:
            return None
        # Re-pad to max_seq_len and batch
        batch = self._convert_to_batch(packed_examples)
        return batch
Packs trimmed examples into fixed-size bins and repads them.

        Args:
            trimmed_examples (List[Dict[str, torch.Tensor]]): A list of trimmed examples.
            sizes (List[int]): The sizes of the trimmed examples.

        Returns:
            Optional[Dict[str, torch.Tensor]]: A batch of repadded examples
                ready for processing. If the collator is in profiling mode,
                returns None.
        
llmfoundry/data/packing.py