File size: 5,272 Bytes
7e0b12a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# coding=utf-8
# Copyright 2025 The ZhipuAI Inc. team and HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

from transformers.models.glm4v.configuration_glm4v import Glm4vConfig, Glm4vTextConfig, Glm4vVisionConfig


# ==== Quicksviewer2 配置包装 ====
class Quicksviewer2VisionConfig(Glm4vVisionConfig):
    """
    Thin wrapper around :class:`~transformers.Glm4vVisionConfig` with a distinct ``model_type`` so the
    auto configuration registry can differentiate Quicksviewer2 from the GLM-4.1V baseline. The intent
    is to keep behaviour identical while we progressively extend the config with cubing/resampler
    settings.
    """

    model_type = "quicksviewer2"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)


class Quicksviewer2TextConfig(Glm4vTextConfig):
    """
    Text backbone configuration alias for Quicksviewer2. It mirrors the GLM-4.1V implementation and only
    overrides ``model_type`` so downstream registries can register Quicksviewer2 specific heads.
    """

    model_type = "quicksviewer2_text"

    def __init__(self, rope_scaling=None, rope_parameters=None, **kwargs):
        if rope_scaling is None and rope_parameters is None:
            rope_scaling = {"type": "default", "mrope_section": [16, 24, 24]}
        super().__init__(rope_scaling=rope_scaling, rope_parameters=rope_parameters, **kwargs)


class Quicksviewer2Config(Glm4vConfig):
    """
    Composite configuration that binds the vision and text backbones used by Quicksviewer2. This class
    simply reuses :class:`~transformers.Glm4vConfig` while swapping the sub-config registry to the
    Quicksviewer2 wrappers above. Future Quicksviewer2-only options should surface here.
    """

    model_type = "quicksviewer2"
    is_composition = True
    sub_configs = {"vision_config": Quicksviewer2VisionConfig, "text_config": Quicksviewer2TextConfig}

    def __init__(
        self,
        text_config=None,
        vision_config=None,
        bos_token_id=151331,
        image_token_id=151343,
        video_token_id=151344,
        image_start_token_id=151339,
        image_end_token_id=151340,
        video_start_token_id=151341,
        video_end_token_id=151342,
        use_cubing=True,
        cubing_alpha=0.8,
        cubing_fpq=14,
        cubing_temperature=0.5,
        cubing_lr_gumbel_start=1.0,
        cubing_lr_gumbel_end=0.001,
        cubing_use_thumbnail=True,
        thumbnail_num_queries=64,
        resampler_num_queries=64,
        resampler_num_heads=32,
        resampler_max_size=(300, 24, 24),
        reference_fps=10.0,
        video_fps=2.0,
        **kwargs,
    ):
        super().__init__(
            text_config=text_config,
            vision_config=vision_config,
            image_token_id=image_token_id,
            video_token_id=video_token_id,
            image_start_token_id=image_start_token_id,
            image_end_token_id=image_end_token_id,
            video_start_token_id=video_start_token_id,
            video_end_token_id=video_end_token_id,
            **kwargs,
        )

        self.bos_token_id = bos_token_id

        temporal_patch_size = max(int(getattr(self.vision_config, "temporal_patch_size", 2)), 1)

        # ==== Cubing 配置 ====
        self.use_cubing = use_cubing
        self.cubing_alpha = cubing_alpha
        self.cubing_fpq = cubing_fpq
        self.effective_cubing_fpq = cubing_fpq // temporal_patch_size
        self.cubing_temperature = cubing_temperature
        self.cubing_lr_gumbel_start = cubing_lr_gumbel_start
        self.cubing_lr_gumbel_end = cubing_lr_gumbel_end
        self.cubing_use_thumbnail = cubing_use_thumbnail
        self.thumbnail_num_queries = thumbnail_num_queries

        # ==== FPS 配置 ====
        self.reference_fps = float(reference_fps)
        self.video_fps = float(video_fps)
        self.effective_video_fps = self.video_fps / temporal_patch_size

        # ==== Resampler 配置 ====
        self.resampler_num_queries = resampler_num_queries
        self.resampler_num_heads = resampler_num_heads
        self.resampler_max_size = (
            tuple(resampler_max_size) if isinstance(resampler_max_size, (list, tuple)) else resampler_max_size
        )

        # ==== AutoProcessor/AutoConfig 注册 ====
        if not hasattr(self, "processor_class"):
            self.processor_class = "Quicksviewer2Processor"
        auto_map = dict(getattr(self, "auto_map", {}))
        auto_map.setdefault("AutoConfig", "transformers.Quicksviewer2Config")
        auto_map.setdefault("AutoProcessor", "transformers.Quicksviewer2Processor")
        self.auto_map = auto_map


__all__ = ["Quicksviewer2Config", "Quicksviewer2TextConfig", "Quicksviewer2VisionConfig"]