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 not self.pipeline_stage_manager:
          raise ValueError(
              "set_pipeline_forward method can only be called when pipeline parallel is enabled."
          )
      stage_manager = self.pipeline_stage_manager
      if self.model.__class__.__name__ == "ChatGLMModel":
          module = self.model
      else:
          module = self.model.transformer
  
      layers_per_stage = Policy.distribute_layers(
          module.num_layers, 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: from functools import partial
  from typing import Callable, Dict, List, Union
  import torch.nn
  from torch import Tensor
  import colossalai.shardformer.layer
  from colossalai.shardformer.modeling.chatglm2 import ChatGLMPipelineForwards
  from colossalai.shardformer.modeling.chatglm2_6b.modeling_chatglm import ChatGLMForConditionalGeneration, ChatGLMModel
  from ..modeling.chatglm2 import get_chatglm_sequence_parallel_forward_fn, get_flash_core_attention_forward, get_jit_fused_glm_block_forward
  from ..modeling.jit import get_jit_fused_dropout_add_func
  from .base_policy import ModulePolicyDescription, Policy, SubModuleReplacementDescription
label_desc: Use image node_id 6 for calling the ChatGLMPolicy obj's underlying member method code with example usage: obj.set_pipeline_forward(model_cls, new_forward, policy) without return types