code: def test_random_sampler(setup_cluster):
      import torch
  
      data = mt.random.rand(1000, 32, dtype="f4")
      labels = mt.random.randint(0, 2, (1000, 10), dtype="f4")
  
      train_dataset = MarsDataset(data, labels)
  
      # test __init__()
      with pytest.raises(ValueError) as e:
          train_sampler = RandomSampler(train_dataset, replacement=1)
      exec_msg = e.value.args[0]
      assert (
          exec_msg
          == "replacement should be a boolean value, but got replacement=1"
      )
  
      with pytest.raises(ValueError) as e:
          train_sampler = RandomSampler(train_dataset, num_samples=900)
      exec_msg = e.value.args[0]
      assert (
          exec_msg
          == "With replacement=False, num_samples should not "
          + "be specified, since a random permute will be performed."
      )
  
      with pytest.raises(ValueError) as e:
          train_sampler = RandomSampler(
              train_dataset, replacement=True, num_samples=-1
          )
      exec_msg = e.value.args[0]
      assert (
          exec_msg
          == "num_samples should be a positive integer value, but got num_samples=-1"
      )
  
      train_sampler = RandomSampler(train_dataset)
  
      # test __len__ num_samples()
      assert len(train_sampler) == 1000
      assert train_sampler.num_samples == 1000
  
      # test __iter__
      g_cpu = torch.Generator()
      g_cpu.manual_seed(2147483647)
  
      train_sampler = RandomSampler(train_dataset, generator=g_cpu)
      assert len(train_sampler) == 1000
      train_loader = torch.utils.data.DataLoader(
          dataset=train_dataset, batch_size=32, sampler=train_sampler
      )
      for _, (batch_data, batch_labels) in enumerate(train_loader):
          assert len(batch_data[0]) == 32
          assert len(batch_labels[0]) == 10
  
      train_sampler = RandomSampler(
          train_dataset, replacement=True, num_samples=900
      )
      assert len(train_sampler) == 900
      train_loader = torch.utils.data.DataLoader(
          dataset=train_dataset, batch_size=32, sampler=train_sampler
      )
      for _, (batch_data, batch_labels) in enumerate(train_loader):
          assert len(batch_data[0]) == 32
          assert len(batch_labels[0]) == 10
  
      # torch train
      model = torch.nn.Sequential(
          torch.nn.Linear(32, 64),
          torch.nn.ReLU(),
          torch.nn.Linear(64, 64),
          torch.nn.ReLU(),
          torch.nn.Linear(64, 10),
          torch.nn.Softmax(dim=1),
      )
  
      optimizer = torch.optim.SGD(
          model.parameters(), lr=0.01, momentum=0.5
      )
      criterion = torch.nn.BCELoss()
      for _ in range(2):
          # 2 epochs
          for _, (batch_data, batch_labels) in enumerate(train_loader):
              outputs = model(batch_data)
              loss = criterion(outputs.squeeze(), batch_labels)
              optimizer.zero_grad()
              loss.backward()
              optimizer.step()
  
file_path: mars/mars/learn/contrib/pytorch/tests/test_dataset.py
imports: import pytest
  import os
  from .....None import tensor
  from .....None import dataframe
  from .....utils import lazy_import
  from ..None import MarsDataset, RandomSampler, SequentialSampler, SubsetRandomSampler, DistributedSampler, run_pytorch_script
label_desc: Use image node_id 3 for calling a global function with example usage: test_random_sampler(setup_cluster) without return types