class_docstr: Sampler that samples indices in a way that groups together features of the dataset of roughly the same length while
  keeping a bit of randomness.
class_docstr_tok: ["Sampler","that","samples","indices","in","a","way","that","groups","together","features","of","the","dataset","of","roughly","the","same","length","while","keeping","a","bit","of","randomness","."]
code: def __init__(
      self,
      batch_size: int,
      world_size: int,
      lengths: Optional[List[int]] = None,
      generator=None,
      group_by_modality: bool = False,
  ):
      if lengths is None:
          raise ValueError("Lengths must be provided.")
  
      self.batch_size = batch_size
      self.world_size = world_size
      self.lengths = lengths
      self.generator = generator
      self.group_by_modality = group_by_modality
  
imports: import os
  import torch
  import torch.nn
  from fairscale.optim import OSS
  from torch.utils.data import Sampler
  from transformers import Trainer
  from transformers.pytorch_utils import ALL_LAYERNORM_LAYERS
  from transformers.trainer_pt_utils import get_parameter_names
  from transformers.trainer_utils import ShardedDDPOption, has_length
  from transformers.utils import is_sagemaker_mp_enabled, logging
  from typing import List, Optional, Union, Dict, Any
label_desc: Use image node_id 1 to create a new LengthGroupedSampler object from inherited base classes: Sampler with example: obj = LengthGroupedSampler(batch_size, world_size, lengths, generator, group_by_modality)