code: def exception_early_stop_asap(pipe_class):
      """Even the first partitions have finished to process, the partition before
      the failed partition hould be killed as soon as possible.
      """
  
      class ExpectedExceptio(Exception):
          pass
  
      class Pass(nn.Module):
          def forward(self, x):
              return x
  
      counter = 0
  
      class Counter(nn.Module):
          def forward(self, x):
              time.sleep(0.1)
  
              nonlocal counter
              counter += 1
  
              return x
  
      class Raise(nn.Module):
          def forward(self, x):
              raise ExpectedException()
  
      model = nn.Sequential(Pass(), Pass(), Counter(), Raise())
      model = pipe_class(
          model, [1, 1, 1, 1], worker_map=get_worker_map(), chunks=3
      )
  
      with pytest.raises(ExpectedException):
          model(torch.rand(3))
  
      # If the early stop doesn't work, it would be 3 instead.
      assert counter == 2
  
imports: from collections import OrderedDict
  from copy import deepcopy
  import os
  import time
  import pytest
  import torch
  from torch import nn
  from fairscale.fair_dev.testing.testing import get_worker_map, torch_spawn
  from fairscale.internal import torch_version
  from fairscale.nn.model_parallel.initialize import get_pipeline_parallel_group
  from fairscale.nn.pipe import AsyncPipe
  from fairscale.nn.pipe.types import LazyModule
label_desc: Use image node_id 21 for calling a global function with example usage: exception_early_stop_asap(pipe_class) and returns: x, x