MCPcopy Create free account
hub / github.com/InternLM/InternBootcamp / apply_monkey_patch

Function apply_monkey_patch

verl/verl/models/transformers/monkey_patch.py:286–493  ·  view source on GitHub ↗

Apply monkey patch to the models for ulysses sequence parallel, fused kernel, tiled MLP and prefix grouper. In the end of this function forward function of the model is patched for fused kernel. If the model is not supported with fused kernel, please return after patch. Args:

(
    model: PreTrainedModel,
    ulysses_sp_size: int = 1,
    use_remove_padding: bool = True,
    use_fused_kernels: bool = False,
    fused_kernels_backend: str = None,
    use_prefix_grouper: bool = False,
    use_tiled_mlp: bool = False,
    tiled_mlp_shards: int = 4,
)

Source from the content-addressed store, hash-verified

284
285
286def apply_monkey_patch(
287 model: PreTrainedModel,
288 ulysses_sp_size: int = 1,
289 use_remove_padding: bool = True,
290 use_fused_kernels: bool = False,
291 fused_kernels_backend: str = None,
292 use_prefix_grouper: bool = False,
293 use_tiled_mlp: bool = False,
294 tiled_mlp_shards: int = 4,
295):
296 """
297 Apply monkey patch to the models for ulysses sequence parallel, fused kernel, tiled MLP and prefix grouper.
298
299 In the end of this function forward function of the model is patched for fused kernel.
300 If the model is not supported with fused kernel, please return after patch.
301
302 Args:
303 model: The model to apply the monkey patch.
304 ulysses_sp_size: The size of ulysses sequence parallel.
305 use_remove_padding: Whether to use remove padding.
306 use_fused_kernels: Whether to use fused kernels.
307 fused_kernels_backend: The backend to use for fused kernels.
308 use_tiled_mlp: Whether to use TiledMLP for memory-efficient MLP computation.
309 tiled_mlp_shards: Number of shards for TiledMLP (higher = lower memory, slightly slower).
310 """
311
312 # Apply TiledMLP monkey patch for memory-efficient MLP computation
313 if use_tiled_mlp:
314 from verl.models.transformers.tiled_mlp import apply_tiled_mlp_monkey_patch
315
316 model_type = getattr(model.config, "model_type", None)
317 apply_tiled_mlp_monkey_patch(num_shards=tiled_mlp_shards, model_type=model_type)
318 # Apply PrefixGrouper patch if enabled
319 if use_prefix_grouper:
320 apply_prefix_grouper_patch()
321
322 """Replace _flash_attention_forward to _ulysses_flash_attention_forward"""
323 module = sys.modules[model.__module__]
324
325 try:
326 num_attention_heads, num_key_value_heads = model.config.num_attention_heads, model.config.num_key_value_heads
327 except AttributeError:
328 num_attention_heads, num_key_value_heads = (
329 model.config.text_config.num_attention_heads,
330 model.config.text_config.num_key_value_heads,
331 )
332
333 assert num_attention_heads % ulysses_sp_size == 0, (
334 f"num_attention_heads {num_attention_heads} must be divisible by ulysses_sp_size {ulysses_sp_size}"
335 )
336 assert num_key_value_heads % ulysses_sp_size == 0 or ulysses_sp_size % num_key_value_heads == 0, (
337 f"num_key_value_heads {num_key_value_heads} must be divisible by ulysses_sp_size "
338 f"{ulysses_sp_size}or vise versa. Upon ulysses_sp_size % num_key_value_heads == 0,"
339 f"kv heads are repeated to ensure correctness."
340 )
341
342 if is_trl_available():
343 from trl import AutoModelForCausalLMWithValueHead # type: ignore

Callers 7

_hf_casual_fwdFunction · 0.90
_hf_casual_fwd_bwdFunction · 0.90
_build_modelMethod · 0.90
_build_moduleMethod · 0.90

Tested by 2

_hf_casual_fwdFunction · 0.72
_hf_casual_fwd_bwdFunction · 0.72