code: def test_grad_mode(grad_mode):
      def detect_grad_enabled():
          x = torch.rand(1, requires_grad=torch.is_grad_enabled())
          return Batch(x)
  
      with torch.set_grad_enabled(grad_mode):
          with spawn_workers([torch.device("cpu")]) as (
              in_queues,
              out_queues,
          ):
              task = Task(
                  CPUStream, compute=detect_grad_enabled, finalize=None
              )
              in_queues[0].put(task)
  
              ok, (_, batch) = out_queues[0].get()
  
              assert ok
              assert batch[0].requires_grad == grad_mode
  
file_path: pytorch/test/distributed/pipeline/sync/test_worker.py
imports: import threading
  import pytest
  import torch
  from torch.distributed.pipeline.sync.microbatch import Batch
  from torch.distributed.pipeline.sync.stream import CPUStream
  from torch.distributed.pipeline.sync.worker import Task, spawn_workers
  from torch.testing._internal.common_utils import run_tests
label_desc: Use image node_id 4 for calling a global function with example usage: test_grad_mode(grad_mode) and returns: Batch