File size: 3,608 Bytes
1b74191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# /Monet/sitecustomize.py
# -----------------------------------------------------------------------
# This patch is only for RL codes (vllm==0.8.5), not for SFT and inference.
# So use `MONET_RL_PATCH=1` only in the RL script.
# -----------------------------------------------------------------------

import os, sys, importlib, inspect

print(f"[sitecustomize] imported from {__file__}", file=sys.stderr)

def patch_qwen_monet():
    # Import official and Monet implementations
    import transformers.models.qwen2_5_vl.modeling_qwen2_5_vl as q_official
    import monet_models.transformers.monet_modeling_qwen2_5_vl as q_monet
    print("[Monet RL patch] replacing...")
    off_cls = q_official.Qwen2_5_VLForConditionalGeneration
    mon_cls = q_monet.Qwen2_5_VLForConditionalGeneration
    q_official.QWEN2_5_VL_ATTENTION_CLASSES["flash_attention_2"] = q_monet.QWEN2_5_VL_ATTENTION_CLASSES["flash_attention_2"]
    q_official.QWEN2_5_VL_ATTENTION_CLASSES["sdpa"] = q_monet.QWEN2_5_VL_ATTENTION_CLASSES["sdpa"]

    # Debug: check signatures before patch
    try:
        print(
            "[Monet RL patch] official forward sig before:",
            inspect.signature(off_cls.forward),
            file=sys.stderr,
        )
        print(
            "[Monet RL patch] monet    forward sig:",
            inspect.signature(mon_cls.forward),
            file=sys.stderr,
        )
    except Exception:
        pass

    # In-place monkey patch: copy methods from Monet class to official class
    off_cls.forward = mon_cls.forward
    q_official.Qwen2_5_VLModel.forward = q_monet.Qwen2_5_VLModel.forward
    q_official.Qwen2_5_VLFlashAttention2.forward = q_monet.Qwen2_5_VLFlashAttention2.forward
    q_official.Qwen2_5_VLSdpaAttention.forward = q_monet.Qwen2_5_VLSdpaAttention.forward
    q_official.Qwen2_5_VLDecoderLayer.forward = q_monet.Qwen2_5_VLDecoderLayer.forward
    #off_cls.__init__ = mon_cls.__init__

    # Debug: check signature after patch
    try:
        print(
            "[Monet RL patch] official forward sig after:",
            inspect.signature(off_cls.forward),
            file=sys.stderr,
        )
        print(
            "[Monet RL patch] q_official.Qwen2_5_VLModel.forward sig after:",
            inspect.signature(q_official.Qwen2_5_VLModel.forward),
            file=sys.stderr,
        )
        print(
            "[Monet RL patch] q_official.Qwen2_5_VLFlashAttention2.forward sig after:",
            inspect.signature(q_official.Qwen2_5_VLFlashAttention2.forward),
            file=sys.stderr,
        )
    except Exception:
        pass

    print(
        "[Monet RL patch] Patched methods of Qwen2_5_VLForConditionalGeneration in-place",
        file=sys.stderr,
    )


def patch():
    print("[sitecustomize] patch() called", file=sys.stderr)

    os.environ["VLLM_USE_V1"] = "1"
    os.environ["VLLM_NO_USAGE_STATS"] = "1"

    workspace = os.path.abspath(".")
    old_path = os.environ.get("PYTHONPATH", "")
    os.environ["PYTHONPATH"] = f"{workspace}:{old_path}" if old_path else workspace
    os.environ["LATENT_START_ID"] = "151666"
    os.environ["LATENT_END_ID"] = "151667"
    os.environ["AVT_LATENT_HOOK_BIN"] = "1"
    sys.modules["vllm.v1.worker.gpu_model_runner"] = importlib.import_module(
        "monet_models.vllm.monet_gpu_model_runner"
    )
    patch_qwen_monet()
    '''sys.modules[
        "transformers.models.qwen2_5_vl.modeling_qwen2_5_vl"
    ] = importlib.import_module(
        "monet_models.transformers.monet_modeling_qwen2_5_vl"
    )'''

    print("[Monet RL patch] vllm & transformers patched", file=sys.stderr)

patch()