Feature Extraction
sentence-transformers
Safetensors
GGUF
English
Chinese
multilingual
qwen3_5
multimodal
embeddings
retrieval
quantization
mixed-precision
w4a8
fp8
int4
svd
mrl
text-embeddings
image-embedding
video-embedding
cross-modal
custom_code
Eval Results (legacy)
Instructions to use ewin-reg/WeMM-Embedding-2B-Quantized with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use ewin-reg/WeMM-Embedding-2B-Quantized with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("ewin-reg/WeMM-Embedding-2B-Quantized", trust_remote_code=True) sentences = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium." ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [3, 3] - Notebooks
- Google Colab
- Kaggle
Release WeMM-Embedding-2B-Quantized: Native Sub-1.3GB SafeTensors with <1.0% degradation
Browse files- .gitattributes +1 -0
- chat_template.jinja +154 -0
- config.json +113 -0
- config_sentence_transformers.json +11 -0
- model.safetensors +3 -0
- modeling_st_wemm.py +70 -0
- modeling_wemm_embedding.py +98 -0
- modules.json +8 -0
- processor_config.json +60 -0
- sentence_bert_config.json +37 -0
- tokenizer.json +3 -0
- tokenizer_config.json +32 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{%- set image_count = namespace(value=0) %}
|
| 2 |
+
{%- set video_count = namespace(value=0) %}
|
| 3 |
+
{%- macro render_content(content, do_vision_count, is_system_content=false) %}
|
| 4 |
+
{%- if content is string %}
|
| 5 |
+
{{- content }}
|
| 6 |
+
{%- elif content is iterable and content is not mapping %}
|
| 7 |
+
{%- for item in content %}
|
| 8 |
+
{%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
|
| 9 |
+
{%- if is_system_content %}
|
| 10 |
+
{{- raise_exception('System message cannot contain images.') }}
|
| 11 |
+
{%- endif %}
|
| 12 |
+
{%- if do_vision_count %}
|
| 13 |
+
{%- set image_count.value = image_count.value + 1 %}
|
| 14 |
+
{%- endif %}
|
| 15 |
+
{%- if add_vision_id %}
|
| 16 |
+
{{- 'Picture ' ~ image_count.value ~ ': ' }}
|
| 17 |
+
{%- endif %}
|
| 18 |
+
{{- '<|vision_start|><|image_pad|><|vision_end|>' }}
|
| 19 |
+
{%- elif 'video' in item or item.type == 'video' %}
|
| 20 |
+
{%- if is_system_content %}
|
| 21 |
+
{{- raise_exception('System message cannot contain videos.') }}
|
| 22 |
+
{%- endif %}
|
| 23 |
+
{%- if do_vision_count %}
|
| 24 |
+
{%- set video_count.value = video_count.value + 1 %}
|
| 25 |
+
{%- endif %}
|
| 26 |
+
{%- if add_vision_id %}
|
| 27 |
+
{{- 'Video ' ~ video_count.value ~ ': ' }}
|
| 28 |
+
{%- endif %}
|
| 29 |
+
{{- '<|vision_start|><|video_pad|><|vision_end|>' }}
|
| 30 |
+
{%- elif 'text' in item %}
|
| 31 |
+
{{- item.text }}
|
| 32 |
+
{%- else %}
|
| 33 |
+
{{- raise_exception('Unexpected item type in content.') }}
|
| 34 |
+
{%- endif %}
|
| 35 |
+
{%- endfor %}
|
| 36 |
+
{%- elif content is none or content is undefined %}
|
| 37 |
+
{{- '' }}
|
| 38 |
+
{%- else %}
|
| 39 |
+
{{- raise_exception('Unexpected content type.') }}
|
| 40 |
+
{%- endif %}
|
| 41 |
+
{%- endmacro %}
|
| 42 |
+
{%- if not messages %}
|
| 43 |
+
{{- raise_exception('No messages provided.') }}
|
| 44 |
+
{%- endif %}
|
| 45 |
+
{%- if tools and tools is iterable and tools is not mapping %}
|
| 46 |
+
{{- '<|im_start|>system\n' }}
|
| 47 |
+
{{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
|
| 48 |
+
{%- for tool in tools %}
|
| 49 |
+
{{- "\n" }}
|
| 50 |
+
{{- tool | tojson }}
|
| 51 |
+
{%- endfor %}
|
| 52 |
+
{{- "\n</tools>" }}
|
| 53 |
+
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
|
| 54 |
+
{%- if messages[0].role == 'system' %}
|
| 55 |
+
{%- set content = render_content(messages[0].content, false, true)|trim %}
|
| 56 |
+
{%- if content %}
|
| 57 |
+
{{- '\n\n' + content }}
|
| 58 |
+
{%- endif %}
|
| 59 |
+
{%- endif %}
|
| 60 |
+
{{- '<|im_end|>\n' }}
|
| 61 |
+
{%- else %}
|
| 62 |
+
{%- if messages[0].role == 'system' %}
|
| 63 |
+
{%- set content = render_content(messages[0].content, false, true)|trim %}
|
| 64 |
+
{{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
|
| 65 |
+
{%- endif %}
|
| 66 |
+
{%- endif %}
|
| 67 |
+
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
|
| 68 |
+
{%- for message in messages[::-1] %}
|
| 69 |
+
{%- set index = (messages|length - 1) - loop.index0 %}
|
| 70 |
+
{%- if ns.multi_step_tool and message.role == "user" %}
|
| 71 |
+
{%- set content = render_content(message.content, false)|trim %}
|
| 72 |
+
{%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
|
| 73 |
+
{%- set ns.multi_step_tool = false %}
|
| 74 |
+
{%- set ns.last_query_index = index %}
|
| 75 |
+
{%- endif %}
|
| 76 |
+
{%- endif %}
|
| 77 |
+
{%- endfor %}
|
| 78 |
+
{%- if ns.multi_step_tool %}
|
| 79 |
+
{{- raise_exception('No user query found in messages.') }}
|
| 80 |
+
{%- endif %}
|
| 81 |
+
{%- for message in messages %}
|
| 82 |
+
{%- set content = render_content(message.content, true)|trim %}
|
| 83 |
+
{%- if message.role == "system" %}
|
| 84 |
+
{%- if not loop.first %}
|
| 85 |
+
{{- raise_exception('System message must be at the beginning.') }}
|
| 86 |
+
{%- endif %}
|
| 87 |
+
{%- elif message.role == "user" %}
|
| 88 |
+
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
|
| 89 |
+
{%- elif message.role == "assistant" %}
|
| 90 |
+
{%- set reasoning_content = '' %}
|
| 91 |
+
{%- if message.reasoning_content is string %}
|
| 92 |
+
{%- set reasoning_content = message.reasoning_content %}
|
| 93 |
+
{%- else %}
|
| 94 |
+
{%- if '</think>' in content %}
|
| 95 |
+
{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
|
| 96 |
+
{%- set content = content.split('</think>')[-1].lstrip('\n') %}
|
| 97 |
+
{%- endif %}
|
| 98 |
+
{%- endif %}
|
| 99 |
+
{%- set reasoning_content = reasoning_content|trim %}
|
| 100 |
+
{%- if loop.index0 > ns.last_query_index %}
|
| 101 |
+
{{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
|
| 102 |
+
{%- else %}
|
| 103 |
+
{{- '<|im_start|>' + message.role + '\n' + content }}
|
| 104 |
+
{%- endif %}
|
| 105 |
+
{%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
|
| 106 |
+
{%- for tool_call in message.tool_calls %}
|
| 107 |
+
{%- if tool_call.function is defined %}
|
| 108 |
+
{%- set tool_call = tool_call.function %}
|
| 109 |
+
{%- endif %}
|
| 110 |
+
{%- if loop.first %}
|
| 111 |
+
{%- if content|trim %}
|
| 112 |
+
{{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 113 |
+
{%- else %}
|
| 114 |
+
{{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 115 |
+
{%- endif %}
|
| 116 |
+
{%- else %}
|
| 117 |
+
{{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 118 |
+
{%- endif %}
|
| 119 |
+
{%- if tool_call.arguments is defined %}
|
| 120 |
+
{%- for args_name, args_value in tool_call.arguments|items %}
|
| 121 |
+
{{- '<parameter=' + args_name + '>\n' }}
|
| 122 |
+
{%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
|
| 123 |
+
{{- args_value }}
|
| 124 |
+
{{- '\n</parameter>\n' }}
|
| 125 |
+
{%- endfor %}
|
| 126 |
+
{%- endif %}
|
| 127 |
+
{{- '</function>\n</tool_call>' }}
|
| 128 |
+
{%- endfor %}
|
| 129 |
+
{%- endif %}
|
| 130 |
+
{{- '<|im_end|>\n' }}
|
| 131 |
+
{%- elif message.role == "tool" %}
|
| 132 |
+
{%- if loop.previtem and loop.previtem.role != "tool" %}
|
| 133 |
+
{{- '<|im_start|>user' }}
|
| 134 |
+
{%- endif %}
|
| 135 |
+
{{- '\n<tool_response>\n' }}
|
| 136 |
+
{{- content }}
|
| 137 |
+
{{- '\n</tool_response>' }}
|
| 138 |
+
{%- if not loop.last and loop.nextitem.role != "tool" %}
|
| 139 |
+
{{- '<|im_end|>\n' }}
|
| 140 |
+
{%- elif loop.last %}
|
| 141 |
+
{{- '<|im_end|>\n' }}
|
| 142 |
+
{%- endif %}
|
| 143 |
+
{%- else %}
|
| 144 |
+
{{- raise_exception('Unexpected message role.') }}
|
| 145 |
+
{%- endif %}
|
| 146 |
+
{%- endfor %}
|
| 147 |
+
{%- if add_generation_prompt %}
|
| 148 |
+
{{- '<|im_start|>assistant\n' }}
|
| 149 |
+
{%- if enable_thinking is defined and enable_thinking is true %}
|
| 150 |
+
{{- '<think>\n' }}
|
| 151 |
+
{%- else %}
|
| 152 |
+
{{- '<think>\n\n</think>\n\n' }}
|
| 153 |
+
{%- endif %}
|
| 154 |
+
{%- endif %}
|
config.json
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"Qwen3_5ForConditionalGeneration"
|
| 4 |
+
],
|
| 5 |
+
"dtype": "bfloat16",
|
| 6 |
+
"image_token_id": 248056,
|
| 7 |
+
"model_type": "qwen3_5",
|
| 8 |
+
"text_config": {
|
| 9 |
+
"attention_bias": false,
|
| 10 |
+
"attention_dropout": 0.0,
|
| 11 |
+
"attn_output_gate": true,
|
| 12 |
+
"bos_token_id": null,
|
| 13 |
+
"dtype": "bfloat16",
|
| 14 |
+
"eos_token_id": 248044,
|
| 15 |
+
"full_attention_interval": 4,
|
| 16 |
+
"head_dim": 256,
|
| 17 |
+
"hidden_act": "silu",
|
| 18 |
+
"hidden_size": 2048,
|
| 19 |
+
"initializer_range": 0.02,
|
| 20 |
+
"intermediate_size": 6144,
|
| 21 |
+
"layer_types": [
|
| 22 |
+
"linear_attention",
|
| 23 |
+
"linear_attention",
|
| 24 |
+
"linear_attention",
|
| 25 |
+
"full_attention",
|
| 26 |
+
"linear_attention",
|
| 27 |
+
"linear_attention",
|
| 28 |
+
"linear_attention",
|
| 29 |
+
"full_attention",
|
| 30 |
+
"linear_attention",
|
| 31 |
+
"linear_attention",
|
| 32 |
+
"linear_attention",
|
| 33 |
+
"full_attention",
|
| 34 |
+
"linear_attention",
|
| 35 |
+
"linear_attention",
|
| 36 |
+
"linear_attention",
|
| 37 |
+
"full_attention",
|
| 38 |
+
"linear_attention",
|
| 39 |
+
"linear_attention",
|
| 40 |
+
"linear_attention",
|
| 41 |
+
"full_attention",
|
| 42 |
+
"linear_attention",
|
| 43 |
+
"linear_attention",
|
| 44 |
+
"linear_attention",
|
| 45 |
+
"full_attention"
|
| 46 |
+
],
|
| 47 |
+
"linear_conv_kernel_dim": 4,
|
| 48 |
+
"linear_key_head_dim": 128,
|
| 49 |
+
"linear_num_key_heads": 16,
|
| 50 |
+
"linear_num_value_heads": 16,
|
| 51 |
+
"linear_value_head_dim": 128,
|
| 52 |
+
"mamba_ssm_dtype": "float32",
|
| 53 |
+
"max_position_embeddings": 262144,
|
| 54 |
+
"mlp_only_layers": [],
|
| 55 |
+
"model_type": "qwen3_5_text",
|
| 56 |
+
"mtp_num_hidden_layers": 1,
|
| 57 |
+
"mtp_use_dedicated_embeddings": false,
|
| 58 |
+
"num_attention_heads": 8,
|
| 59 |
+
"num_hidden_layers": 24,
|
| 60 |
+
"num_key_value_heads": 2,
|
| 61 |
+
"pad_token_id": null,
|
| 62 |
+
"partial_rotary_factor": 0.25,
|
| 63 |
+
"rms_norm_eps": 1e-06,
|
| 64 |
+
"rope_parameters": {
|
| 65 |
+
"mrope_interleaved": true,
|
| 66 |
+
"mrope_section": [
|
| 67 |
+
11,
|
| 68 |
+
11,
|
| 69 |
+
10
|
| 70 |
+
],
|
| 71 |
+
"partial_rotary_factor": 0.25,
|
| 72 |
+
"rope_theta": 10000000,
|
| 73 |
+
"rope_type": "default"
|
| 74 |
+
},
|
| 75 |
+
"tie_word_embeddings": false,
|
| 76 |
+
"use_cache": true,
|
| 77 |
+
"vocab_size": 248078
|
| 78 |
+
},
|
| 79 |
+
"tie_word_embeddings": false,
|
| 80 |
+
"transformers_version": "5.2.0",
|
| 81 |
+
"video_token_id": 248057,
|
| 82 |
+
"vision_config": {
|
| 83 |
+
"deepstack_visual_indexes": [],
|
| 84 |
+
"depth": 24,
|
| 85 |
+
"dtype": "bfloat16",
|
| 86 |
+
"hidden_act": "gelu_pytorch_tanh",
|
| 87 |
+
"hidden_size": 1024,
|
| 88 |
+
"in_channels": 3,
|
| 89 |
+
"initializer_range": 0.02,
|
| 90 |
+
"intermediate_size": 4096,
|
| 91 |
+
"model_type": "qwen3_5",
|
| 92 |
+
"num_heads": 16,
|
| 93 |
+
"num_position_embeddings": 2304,
|
| 94 |
+
"out_hidden_size": 2048,
|
| 95 |
+
"patch_size": 16,
|
| 96 |
+
"spatial_merge_size": 2,
|
| 97 |
+
"temporal_patch_size": 2
|
| 98 |
+
},
|
| 99 |
+
"vision_end_token_id": 248054,
|
| 100 |
+
"vision_start_token_id": 248053,
|
| 101 |
+
"auto_map": {
|
| 102 |
+
"AutoModel": "modeling_wemm_embedding.WeMMEmbedding",
|
| 103 |
+
"AutoModelForCausalLM": "modeling_wemm_embedding.WeMMEmbedding"
|
| 104 |
+
},
|
| 105 |
+
"matryoshka_dimensions": [
|
| 106 |
+
64,
|
| 107 |
+
128,
|
| 108 |
+
256,
|
| 109 |
+
512,
|
| 110 |
+
1024,
|
| 111 |
+
2048
|
| 112 |
+
]
|
| 113 |
+
}
|
config_sentence_transformers.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"__version__": {
|
| 3 |
+
"pytorch": "2.11.0+cu128",
|
| 4 |
+
"sentence_transformers": "5.7.0",
|
| 5 |
+
"transformers": "5.2.0"
|
| 6 |
+
},
|
| 7 |
+
"default_prompt_name": null,
|
| 8 |
+
"model_type": "SentenceTransformer",
|
| 9 |
+
"prompts": {},
|
| 10 |
+
"similarity_fn_name": "cosine"
|
| 11 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6b92c565dd4530b3cecc25eb1147b6e0a6592cd3ac5b734b01f5aa64fc4ccfef
|
| 3 |
+
size 1543511130
|
modeling_st_wemm.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Sentence Transformers module for WeMM-Embedding.
|
| 2 |
+
|
| 3 |
+
Reproduces the `transformers` usage from the model card inside a Sentence Transformers
|
| 4 |
+
pipeline: vision inputs are prepared with `qwen_vl_utils.process_vision_info` and the
|
| 5 |
+
embedding is read from `WeMMEmbedding.embedding`, which pools the `<embedding>` position and
|
| 6 |
+
L2-normalizes. Everything else (batching, prompts, truncation, `encode_query` /
|
| 7 |
+
`encode_document`, similarity) comes from the stock `Transformer` module.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
import inspect
|
| 13 |
+
from typing import Any
|
| 14 |
+
|
| 15 |
+
from sentence_transformers.models import Transformer
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class WeMMTransformer(Transformer):
|
| 19 |
+
"""`Transformer` that prepares images and videos the way the model card's snippet does."""
|
| 20 |
+
|
| 21 |
+
def __init__(self, model_name_or_path: str, **kwargs: Any) -> None:
|
| 22 |
+
super().__init__(model_name_or_path, **kwargs)
|
| 23 |
+
vision_config = getattr(self.config, "vision_config", None)
|
| 24 |
+
self.image_patch_size = int(getattr(vision_config, "patch_size", 16))
|
| 25 |
+
|
| 26 |
+
# `embedding` hands its **kwargs to the inner model, so filtering on its own signature
|
| 27 |
+
# would drop `pixel_values`. Filter on the inner model's parameters instead, plus the
|
| 28 |
+
# processor's input names for anything the model only accepts as **kwargs.
|
| 29 |
+
inner_model = getattr(self.model, "model", self.model)
|
| 30 |
+
signature = set(inspect.signature(inner_model.forward).parameters)
|
| 31 |
+
signature |= set(getattr(self.processor, "model_input_names", ()))
|
| 32 |
+
for modality_params in self.modality_config.values():
|
| 33 |
+
method_name = modality_params["method"]
|
| 34 |
+
if method_name != "forward":
|
| 35 |
+
self._method_signature_cache.setdefault(method_name, signature)
|
| 36 |
+
|
| 37 |
+
def _apply_chat_template(
|
| 38 |
+
self,
|
| 39 |
+
messages: list[list[dict[str, Any]]],
|
| 40 |
+
modality_kwargs: dict[str, dict[str, Any]],
|
| 41 |
+
common_kwargs: dict[str, Any],
|
| 42 |
+
chat_template_kwargs: dict[str, Any],
|
| 43 |
+
) -> dict[str, Any]:
|
| 44 |
+
"""Render the chat template and prepare images / videos exactly as the model card does."""
|
| 45 |
+
from qwen_vl_utils import process_vision_info
|
| 46 |
+
|
| 47 |
+
chat_template_kwargs = {"add_generation_prompt": False, **chat_template_kwargs}
|
| 48 |
+
texts = [
|
| 49 |
+
self.processor.apply_chat_template(conversation, tokenize=False, **chat_template_kwargs)
|
| 50 |
+
for conversation in messages
|
| 51 |
+
]
|
| 52 |
+
images, videos, video_kwargs = process_vision_info(
|
| 53 |
+
[list(conversation) for conversation in messages],
|
| 54 |
+
image_patch_size=self.image_patch_size,
|
| 55 |
+
return_video_kwargs=True,
|
| 56 |
+
return_video_metadata=True,
|
| 57 |
+
)
|
| 58 |
+
if videos is not None:
|
| 59 |
+
videos, video_metadata = (list(part) for part in zip(*videos))
|
| 60 |
+
video_kwargs = {**video_kwargs, "video_metadata": video_metadata}
|
| 61 |
+
|
| 62 |
+
return self.processor(
|
| 63 |
+
text=texts,
|
| 64 |
+
images=images,
|
| 65 |
+
videos=videos,
|
| 66 |
+
text_kwargs=modality_kwargs["text"],
|
| 67 |
+
images_kwargs=modality_kwargs["image"],
|
| 68 |
+
videos_kwargs={**modality_kwargs["video"], **video_kwargs},
|
| 69 |
+
common_kwargs=common_kwargs,
|
| 70 |
+
)
|
modeling_wemm_embedding.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
|
| 4 |
+
class FlatQuantW4A8Linear(nn.Module):
|
| 5 |
+
def __init__(self, in_features, out_features, bias=False, group_size=64):
|
| 6 |
+
super().__init__()
|
| 7 |
+
self.in_features = in_features
|
| 8 |
+
self.out_features = out_features
|
| 9 |
+
self.group_size = group_size
|
| 10 |
+
self.register_buffer("weight", torch.empty((out_features, in_features // 2), dtype=torch.uint8))
|
| 11 |
+
self.register_buffer("weight_scale", torch.empty((out_features, in_features // group_size), dtype=torch.bfloat16))
|
| 12 |
+
if bias:
|
| 13 |
+
self.bias = nn.Parameter(torch.empty(out_features, dtype=torch.bfloat16))
|
| 14 |
+
else:
|
| 15 |
+
self.register_parameter("bias", None)
|
| 16 |
+
|
| 17 |
+
def forward(self, x):
|
| 18 |
+
low = (self.weight & 0x0F).to(torch.int8)
|
| 19 |
+
high = (self.weight >> 4).to(torch.int8)
|
| 20 |
+
low = torch.where(low >= 8, low - 16, low)
|
| 21 |
+
high = torch.where(high >= 8, high - 16, high)
|
| 22 |
+
unpacked = torch.empty(self.out_features, self.in_features, device=x.device, dtype=x.dtype)
|
| 23 |
+
unpacked[:, 0::2] = low.to(x.dtype)
|
| 24 |
+
unpacked[:, 1::2] = high.to(x.dtype)
|
| 25 |
+
scales = self.weight_scale.to(x.dtype).repeat_interleave(self.group_size, dim=1)
|
| 26 |
+
return F.linear(x, unpacked * scales, self.bias.to(x.dtype) if self.bias is not None else None)
|
| 27 |
+
|
| 28 |
+
class FlatQuantFP8Linear(nn.Module):
|
| 29 |
+
def __init__(self, in_features, out_features, bias=False):
|
| 30 |
+
super().__init__()
|
| 31 |
+
self.in_features = in_features
|
| 32 |
+
self.out_features = out_features
|
| 33 |
+
self.register_buffer("weight", torch.empty((out_features, in_features), dtype=torch.float8_e4m3fn))
|
| 34 |
+
self.register_buffer("weight_scale", torch.empty((), dtype=torch.bfloat16))
|
| 35 |
+
if bias:
|
| 36 |
+
self.bias = nn.Parameter(torch.empty(out_features, dtype=torch.bfloat16))
|
| 37 |
+
else:
|
| 38 |
+
self.register_parameter("bias", None)
|
| 39 |
+
|
| 40 |
+
def forward(self, x):
|
| 41 |
+
w_deq = (self.weight.float() * self.weight_scale.float()).to(x.dtype)
|
| 42 |
+
return F.linear(x, w_deq, self.bias.to(x.dtype) if self.bias is not None else None)
|
| 43 |
+
|
| 44 |
+
class FlatQuantW4A8Embedding(nn.Module):
|
| 45 |
+
def __init__(self, num_embeddings, embedding_dim, group_size=64):
|
| 46 |
+
super().__init__()
|
| 47 |
+
self.num_embeddings = num_embeddings
|
| 48 |
+
self.embedding_dim = embedding_dim
|
| 49 |
+
self.group_size = group_size
|
| 50 |
+
self.register_buffer("weight", torch.empty((num_embeddings, embedding_dim // 2), dtype=torch.uint8))
|
| 51 |
+
self.register_buffer("weight_scale", torch.empty((num_embeddings, embedding_dim // group_size), dtype=torch.bfloat16))
|
| 52 |
+
|
| 53 |
+
def forward(self, input_ids):
|
| 54 |
+
w_packed = self.weight[input_ids]
|
| 55 |
+
scales = self.weight_scale[input_ids]
|
| 56 |
+
low = (w_packed & 0x0F).to(torch.int8)
|
| 57 |
+
high = (w_packed >> 4).to(torch.int8)
|
| 58 |
+
low = torch.where(low >= 8, low - 16, low)
|
| 59 |
+
high = torch.where(high >= 8, high - 16, high)
|
| 60 |
+
*bdims, packed_dim = w_packed.shape
|
| 61 |
+
unpacked = torch.empty(*bdims, packed_dim * 2, device=input_ids.device, dtype=torch.bfloat16)
|
| 62 |
+
unpacked[..., 0::2] = low.to(torch.bfloat16)
|
| 63 |
+
unpacked[..., 1::2] = high.to(torch.bfloat16)
|
| 64 |
+
return unpacked * scales.to(torch.bfloat16).repeat_interleave(self.group_size, dim=-1)
|
| 65 |
+
|
| 66 |
+
import torch
|
| 67 |
+
import torch.nn.functional as F
|
| 68 |
+
from transformers import Qwen3_5ForConditionalGeneration
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
class WeMMEmbedding(Qwen3_5ForConditionalGeneration):
|
| 72 |
+
def embedding(self, input_ids=None, attention_mask=None, **kwargs):
|
| 73 |
+
# transformers < 5.15 reuses the rope_deltas cached by the previous multimodal
|
| 74 |
+
# forward for a text-only one, which shifts its position ids.
|
| 75 |
+
self.model.rope_deltas = None
|
| 76 |
+
|
| 77 |
+
outputs = self.model(
|
| 78 |
+
input_ids=input_ids,
|
| 79 |
+
attention_mask=attention_mask,
|
| 80 |
+
**kwargs
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
last_hidden_state = outputs.last_hidden_state
|
| 84 |
+
|
| 85 |
+
if attention_mask is not None:
|
| 86 |
+
eos_positions = attention_mask.sum(dim=1) - 1
|
| 87 |
+
else:
|
| 88 |
+
eos_positions = torch.full((last_hidden_state.shape[0],), last_hidden_state.shape[1] - 1, device=last_hidden_state.device)
|
| 89 |
+
|
| 90 |
+
eos_positions = eos_positions.clamp(min=0)
|
| 91 |
+
|
| 92 |
+
batch_indices = torch.arange(last_hidden_state.size(0), device=last_hidden_state.device)
|
| 93 |
+
|
| 94 |
+
embeddings = last_hidden_state[batch_indices, eos_positions]
|
| 95 |
+
|
| 96 |
+
embeddings = F.normalize(embeddings, dim=-1)
|
| 97 |
+
|
| 98 |
+
return embeddings
|
modules.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"idx": 0,
|
| 4 |
+
"name": "0",
|
| 5 |
+
"path": "",
|
| 6 |
+
"type": "modeling_st_wemm.WeMMTransformer"
|
| 7 |
+
}
|
| 8 |
+
]
|
processor_config.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"image_processor": {
|
| 3 |
+
"do_convert_rgb": true,
|
| 4 |
+
"do_normalize": true,
|
| 5 |
+
"do_rescale": true,
|
| 6 |
+
"do_resize": true,
|
| 7 |
+
"image_mean": [
|
| 8 |
+
0.5,
|
| 9 |
+
0.5,
|
| 10 |
+
0.5
|
| 11 |
+
],
|
| 12 |
+
"image_processor_type": "Qwen2VLImageProcessor",
|
| 13 |
+
"image_std": [
|
| 14 |
+
0.5,
|
| 15 |
+
0.5,
|
| 16 |
+
0.5
|
| 17 |
+
],
|
| 18 |
+
"merge_size": 2,
|
| 19 |
+
"patch_size": 16,
|
| 20 |
+
"resample": 3,
|
| 21 |
+
"rescale_factor": 0.00392156862745098,
|
| 22 |
+
"size": {
|
| 23 |
+
"longest_edge": 16777216,
|
| 24 |
+
"shortest_edge": 65536
|
| 25 |
+
},
|
| 26 |
+
"temporal_patch_size": 2
|
| 27 |
+
},
|
| 28 |
+
"processor_class": "Qwen3VLProcessor",
|
| 29 |
+
"video_processor": {
|
| 30 |
+
"do_convert_rgb": true,
|
| 31 |
+
"do_normalize": true,
|
| 32 |
+
"do_rescale": true,
|
| 33 |
+
"do_resize": true,
|
| 34 |
+
"do_sample_frames": true,
|
| 35 |
+
"fps": 2,
|
| 36 |
+
"image_mean": [
|
| 37 |
+
0.5,
|
| 38 |
+
0.5,
|
| 39 |
+
0.5
|
| 40 |
+
],
|
| 41 |
+
"image_std": [
|
| 42 |
+
0.5,
|
| 43 |
+
0.5,
|
| 44 |
+
0.5
|
| 45 |
+
],
|
| 46 |
+
"max_frames": 768,
|
| 47 |
+
"merge_size": 2,
|
| 48 |
+
"min_frames": 4,
|
| 49 |
+
"patch_size": 16,
|
| 50 |
+
"resample": 3,
|
| 51 |
+
"rescale_factor": 0.00392156862745098,
|
| 52 |
+
"return_metadata": false,
|
| 53 |
+
"size": {
|
| 54 |
+
"longest_edge": 234881024,
|
| 55 |
+
"shortest_edge": 4096
|
| 56 |
+
},
|
| 57 |
+
"temporal_patch_size": 2,
|
| 58 |
+
"video_processor_type": "Qwen3VLVideoProcessor"
|
| 59 |
+
}
|
| 60 |
+
}
|
sentence_bert_config.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"transformer_task": "feature-extraction",
|
| 3 |
+
"modality_config": {
|
| 4 |
+
"text": {
|
| 5 |
+
"method": "embedding",
|
| 6 |
+
"method_output_name": null
|
| 7 |
+
},
|
| 8 |
+
"image": {
|
| 9 |
+
"method": "embedding",
|
| 10 |
+
"method_output_name": null
|
| 11 |
+
},
|
| 12 |
+
"video": {
|
| 13 |
+
"method": "embedding",
|
| 14 |
+
"method_output_name": null
|
| 15 |
+
},
|
| 16 |
+
"image+text": {
|
| 17 |
+
"method": "embedding",
|
| 18 |
+
"method_output_name": null
|
| 19 |
+
},
|
| 20 |
+
"text+video": {
|
| 21 |
+
"method": "embedding",
|
| 22 |
+
"method_output_name": null
|
| 23 |
+
},
|
| 24 |
+
"message": {
|
| 25 |
+
"method": "embedding",
|
| 26 |
+
"method_output_name": null,
|
| 27 |
+
"format": "structured"
|
| 28 |
+
}
|
| 29 |
+
},
|
| 30 |
+
"module_output_name": "sentence_embedding",
|
| 31 |
+
"processing_kwargs": {
|
| 32 |
+
"chat_template": {
|
| 33 |
+
"chat_template": "sentence_transformers"
|
| 34 |
+
}
|
| 35 |
+
},
|
| 36 |
+
"unpad_inputs": false
|
| 37 |
+
}
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:40e444c744512f423da4c8443c47c21e22ff76056ba4e9796a81c04c13a9daf0
|
| 3 |
+
size 19990378
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"audio_bos_token": "<|audio_start|>",
|
| 4 |
+
"audio_eos_token": "<|audio_end|>",
|
| 5 |
+
"audio_token": "<|audio_pad|>",
|
| 6 |
+
"backend": "tokenizers",
|
| 7 |
+
"bos_token": null,
|
| 8 |
+
"clean_up_tokenization_spaces": false,
|
| 9 |
+
"eos_token": "<|endoftext|>",
|
| 10 |
+
"errors": "replace",
|
| 11 |
+
"image_token": "<|image_pad|>",
|
| 12 |
+
"is_local": true,
|
| 13 |
+
"model_max_length": 262144,
|
| 14 |
+
"model_specific_special_tokens": {
|
| 15 |
+
"audio_bos_token": "<|audio_start|>",
|
| 16 |
+
"audio_eos_token": "<|audio_end|>",
|
| 17 |
+
"audio_token": "<|audio_pad|>",
|
| 18 |
+
"image_token": "<|image_pad|>",
|
| 19 |
+
"video_token": "<|video_pad|>",
|
| 20 |
+
"vision_bos_token": "<|vision_start|>",
|
| 21 |
+
"vision_eos_token": "<|vision_end|>"
|
| 22 |
+
},
|
| 23 |
+
"pad_token": "<|endoftext|>",
|
| 24 |
+
"pretokenize_regex": "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?[\\p{L}\\p{M}]+|\\p{N}| ?[^\\s\\p{L}\\p{M}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+",
|
| 25 |
+
"processor_class": "Qwen3VLProcessor",
|
| 26 |
+
"split_special_tokens": false,
|
| 27 |
+
"tokenizer_class": "PreTrainedTokenizerFast",
|
| 28 |
+
"unk_token": null,
|
| 29 |
+
"video_token": "<|video_pad|>",
|
| 30 |
+
"vision_bos_token": "<|vision_start|>",
|
| 31 |
+
"vision_eos_token": "<|vision_end|>"
|
| 32 |
+
}
|