docstr: If under pipeline parallel setting, replacing the original forward method of huggingface
  to customized forward method, and add this changing to policy.
docstr_tok: ["If","under","pipeline","parallel","setting",",","replacing","the","original","forward","method","of","huggingface","to","customized","forward","method",",","and","add","this","changing","to","policy","."]
code: def set_pipeline_forward(
      self, model_cls: nn.Module, new_forward: Callable, policy: Dict
  ) -> None:
      """If under pipeline parallel setting, replacing the original forward method of huggingface
      to customized forward method, and add this changing to policy."""
      if self.pipeline_stage_manager:
          stage_manager = self.pipeline_stage_manager
          if self.model.__class__.__name__ == "FalconModel":
              module = self.model
          else:
              module = self.model.transformer
  
          layers_per_stage = Policy.distribute_layers(
              len(module.h), stage_manager.num_stages
          )
          stage_index = Policy.get_stage_index(
              layers_per_stage, stage_manager.stage
          )
          method_replacement = {
              "forward": partial(
                  new_forward,
                  stage_manager=stage_manager,
                  stage_index=stage_index,
                  shard_config=self.shard_config,
              )
          }
          self.append_or_create_method_replacement(
              description=method_replacement,
              policy=policy,
              target_key=model_cls,
          )
  
imports: import warnings
  from functools import partial
  from typing import Callable, Dict, List
  from torch import Tensor, nn
  from torch.nn import Module
  import colossalai.shardformer.layer
  from ..modeling.falcon import FalconPipelineForwards, build_falcon_alibi_tensor_fn, get_falcon_flash_attention_forward, get_tp_falcon_decoder_layer_forward
  from .base_policy import ModulePolicyDescription, Policy, SubModuleReplacementDescription
label_desc: Use image node_id 6 for calling the FalconPolicy obj's underlying member method code with example usage: obj.set_pipeline_forward(model_cls, new_forward, policy) without return types