Hi!
This code test max sample in all dataset. Maybe this help with you.
def preallocate_memory_trick(self, model: nn.Module):
    if self.deepspeed:
        return     # finding the longest input_values and labels in the dataset
    # generate this randomly needs to infer dtype of inputs, so...
    input_values = max(self.train_dataset, key=lambda x: len(x['input_values']))['input_values']
    labels = max(self.train_dataset, key=lambda x: len(x['labels']))['labels']
    inputs = {
        "input_values": torch.Tensor(input_values).repeat(self.args.train_batch_size, 1),
        "labels": torch.Tensor(labels).repeat(self.args.train_batch_size, 1)
    }
    self.training_step(model, inputs)
    model.zero_grad()