File size: 3,257 Bytes
8e49cd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Configuration of SetCon-LM, the language model on the frozen SetCon encoder (Hugging Face remote code).

Needs transformers >= 5 (Qwen3.5). The SetCon encoder itself also loads with transformers 4.57.
"""
from __future__ import annotations

import os

# The paper's runs used the Triton kernels of flash-linear-attention for the Gated DeltaNet layers of Qwen3.5 and
# disabled its backend dispatch. Must be set before `fla` is imported; an explicit user setting wins.
os.environ.setdefault("FLA_DISABLE_BACKEND_DISPATCH", "1")

from transformers import PretrainedConfig  # noqa: E402
from transformers.models.qwen3_5.configuration_qwen3_5 import Qwen3_5TextConfig  # noqa: E402

# The system prompt of every question during training and in the paper's evaluation.
SYSTEM_PROMPT = ("Continuous tokens from sparse wearable IMUs are provided before this text. Answer the question about "
                 "the observed motion in one or two short sentences.")
# Names of the 22 body sites in the prompt line "Sensors worn at: ...", in the order of processing_setcon.SITE_NAMES.
SITE_TEXT = ("pelvis", "left hip", "left knee", "left ankle", "left foot", "right hip", "right knee", "right ankle",
             "right foot", "spine", "upper spine", "chest", "neck", "head", "left collar", "left shoulder",
             "left elbow", "left wrist", "right collar", "right shoulder", "right elbow", "right wrist")


class SetConLMConfig(PretrainedConfig):
    """Qwen3.5-0.8B (text part, `text_config`) plus the projector from SetCon encoder features to LM inputs.

    encoder_training_seed   the SetCon checkpoint this LM was trained on (1 = repository root, 2 / 3 = seed2/, seed3/)
    encoder_hidden_size     width of the encoder's setup vector and patch tokens (256)
    num_sites, num_patches  the encoder's 22 body sites and 50 patches of 0.2 s
    """

    model_type = "setcon_lm"
    sub_configs = {"text_config": Qwen3_5TextConfig}

    def __init__(
        self,
        text_config=None,
        encoder_training_seed: int = 1,
        encoder_hidden_size: int = 256,
        num_sites: int = 22,
        num_patches: int = 50,
        system_prompt: str = SYSTEM_PROMPT,
        site_text: tuple = SITE_TEXT,
        default_question: str = "What is the person doing?",
        max_new_tokens: int = 48,
        batch_size: int = 32,
        stop_tokens: tuple = ("<|im_end|>", "<|endoftext|>"),
        base_model: str = "Qwen/Qwen3.5-0.8B",
        **kwargs,
    ):
        if isinstance(text_config, dict):
            text_config = Qwen3_5TextConfig(**text_config)
        self.text_config = text_config if text_config is not None else Qwen3_5TextConfig()
        self.encoder_training_seed = int(encoder_training_seed)
        self.encoder_hidden_size = int(encoder_hidden_size)
        self.num_sites = int(num_sites)
        self.num_patches = int(num_patches)
        self.system_prompt = str(system_prompt)
        self.site_text = [str(s) for s in site_text]
        self.default_question = str(default_question)
        self.max_new_tokens = int(max_new_tokens)
        self.batch_size = int(batch_size)
        self.stop_tokens = [str(t) for t in stop_tokens]
        self.base_model = str(base_model)
        super().__init__(**kwargs)