Image-Text-to-Text
Transformers
Safetensors
interns1_pro
text-generation
conversational
custom_code
Eval Results
fp8
Instructions to use internlm/Intern-S1-Pro with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use internlm/Intern-S1-Pro with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="internlm/Intern-S1-Pro", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("internlm/Intern-S1-Pro", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use internlm/Intern-S1-Pro with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "internlm/Intern-S1-Pro" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "internlm/Intern-S1-Pro", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/internlm/Intern-S1-Pro
- SGLang
How to use internlm/Intern-S1-Pro with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "internlm/Intern-S1-Pro" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "internlm/Intern-S1-Pro", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "internlm/Intern-S1-Pro" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "internlm/Intern-S1-Pro", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use internlm/Intern-S1-Pro with Docker Model Runner:
docker model run hf.co/internlm/Intern-S1-Pro
update-deployment-guide
#9
by lvhan - opened
Files changed (14)
- 0092638_seism.npyDeleted-3
- README.md+5-131
- chat_template.jinja-2
- config.json-32
- configuration_interns1_pro.py+2-52
- deployment_guide.md+21-5
- model-time_series-00001-of-00002.safetensorsDeleted-3
- model-time_series-00002-of-00002.safetensorsDeleted-3
- model.safetensors.index.json+2-2
- modeling_interns1_pro.py+4-503
- processing_interns1_pro.py+2-147
- test_inference_ts.pyDeleted-78
- tokenization_interns1.py+6-8
- video_processing_interns1_pro.py+2-2
123version https://git-lfs.github.com/spec/v1oid sha256:c2b94653c6964b630038897a27cb6d276ff866d9ecd1f6419358b9407f0df62esize 7212811 unmodified lines1213141516171842 unmodified lines616263646566673 unmodified lines717273747576170 unmodified lines24724824925025125225332 unmodified lines2862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064079 unmodified lines41741841942042142242342442542642742842943043111 unmodified lines<div> </div>[💻Github Repo](https://github.com/InternLM/Intern-S1) • [🤗Model Collections](https://huggingface.co/collections/internlm/intern-s1-6882e325e8ac1c58ba108aa5) • [📜Technical Report](https://huggingface.co/papers/2603.25040) • [💬Online Chat](https://chat.intern-ai.org.cn/)</div>42 unmodified lines### Serving> [!IMPORTANT]> Running a trillion-parameter model using the native Hugging Face forward method is challenging. We strongly recommend using an LLM inference engine (such as LMDeploy, vLLM, or SGLang) to host Intern-S1-Pro and accessing the model via API.Intern-S1-Pro can be deployed using any of the following LLM inference frameworks:3 unmodified linesDetailed deployment examples for these frameworks are available in the [Model Deployment Guide](./deployment_guide.md).## Advanced Usage170 unmodified lines)```When serving Intern-S1-Pro models, you can dynamically control the thinking mode by adjusting the `enable_thinking` parameter in your requests.```pythonfrom openai import OpenAI32 unmodified linesprint(json.dumps(response.model_dump(), indent=2, ensure_ascii=False))```### Time Series DemoTime series inference is currently only supported in LMDeploy. To get started, download and deploy Intern-S1-Pro with LMDeploy (>=v0.12.1) by following the [Model Deployment Guide](./deployment_guide.md).Below is an example of detecting earthquake events from a time series signal file. Additional data types and functionalities are also supported.```from openai import OpenAIfrom lmdeploy.vl.time_series_utils import encode_time_series_base64openai_api_key = "EMPTY"openai_api_base = "http://0.0.0.0:8000/v1"client = OpenAI(api_key=openai_api_key,base_url=openai_api_base,)model_name = client.models.list().data[0].iddef send_base64(file_path: str, sampling_rate: int = 100):"""base64-encoded time-series data."""# encode_time_series_base64 accepts local file paths and http urls,# encoding time-series data (.npy, .csv, .wav, .mp3, .flac, etc.) into base64 strings.base64_ts = encode_time_series_base64(file_path)messages = [{"role": "user","content": [{"type": "text","text": "Please determine whether an Earthquake event has occurred in the provided time-series data. If so, please specify the starting time point indices of the P-wave and S-wave in the event."},{"type": "time_series_url","time_series_url": {"url": f"data:time_series/npy;base64,{base64_ts}","sampling_rate": sampling_rate},},],}]return client.chat.completions.create(model=model_name,messages=messages,temperature=0,max_tokens=200,)def send_http_url(url: str, sampling_rate: int = 100):"""http(s) url pointing to the time-series data."""messages = [{"role": "user","content": [{"type": "text","text": "Please determine whether an Earthquake event has occurred in the provided time-series data. If so, please specify the starting time point indices of the P-wave and S-wave in the event."},{"type": "time_series_url","time_series_url": {"url": url,"sampling_rate": sampling_rate},},],}]return client.chat.completions.create(model=model_name,messages=messages,temperature=0,max_tokens=200,)def send_file_url(file_path: str, sampling_rate: int = 100):"""file url pointing to the time-series data."""messages = [{"role": "user","content": [{"type": "text","text": "Please determine whether an Earthquake event has occurred in the provided time-series data. If so, please specify the starting time point indices of the P-wave and S-wave in the event."},{"type": "time_series_url","time_series_url": {"url": f"file://{file_path}","sampling_rate": sampling_rate},},],}]return client.chat.completions.create(model=model_name,messages=messages,temperature=0,max_tokens=200,)response = send_base64("./0092638_seism.npy")# response = send_http_url("https://huggingface.co/internlm/Intern-S1-Pro/raw/main/0092638_seism.npy")# response = send_file_url("./0092638_seism.npy")print(response.choices[0].message)```## CitationIf you find this work useful, feel free to give us a cite.9 unmodified linesurl={https://arxiv.org/abs/2508.15763},}``````@misc{zou2026interns1proscientificmultimodalfoundation,title={Intern-S1-Pro: Scientific Multimodal Foundation Model at Trillion Scale},author={Yicheng Zou and Dongsheng Zhu and Lin Zhu and Tong Zhu and Yunhua Zhou and Peiheng Zhou and Xinyu Zhou and Dongzhan Zhou and Zhiwang Zhou and Yuhao Zhou and Bowen Zhou and Zhanping Zhong and Zhijie Zhong and Haiteng Zhao and Penghao Zhao and Xiaomeng Zhao and Zhiyuan Zhao and Yechen Zhang and Jin Zhang and Wenwei Zhang and Hongjie Zhang and Zhuo Zhang and Wenlong Zhang and Bo Zhang and Chao Zhang and Chen Zhang and Yuhang Zang and Fei Yuan and Jiakang Yuan and Jiashuo Yu and Jinhui Yin and Haochen Ye and Qian Yao and Bowen Yang and Danni Yang and Kaichen Yang and Ziang Yan and Jun Xu and Yicheng Xu and Wanghan Xu and Xuenan Xu and Chao Xu and Ruiliang Xu and Shuhao Xing and Long Xing and Xinchen Xie and Ling-I Wu and Zijian Wu and Zhenyu Wu and Lijun Wu and Yue Wu and Jianyu Wu and Wen Wu and Fan Wu and Xilin Wei and Qi Wei and Bingli Wang and Rui Wang and Ziyi Wang and Zun Wang and Yi Wang and Haomin Wang and Yizhou Wang and Lintao Wang and Yiheng Wang and Longjiang Wang and Bin Wang and Jian Tong and Zhongbo Tian and Huanze Tang and Chen Tang and Shixiang Tang and Yu Sun and Qiushi Sun and Xuerui Su and Qisheng Su and Chenlin Su and Demin Song and Jin Shi and Fukai Shang and Yuchen Ren and Pengli Ren and Xiaoye Qu and Yuan Qu and Jiantao Qiu and Yu Qiao and Runyu Peng and Tianshuo Peng and Jiahui Peng and Qizhi Pei and Zhuoshi Pan and Linke Ouyang and Wenchang Ning and Yichuan Ma and Zerun Ma and Ningsheng Ma and Runyuan Ma and Chengqi Lyu and Haijun Lv and Han Lv and Lindong Lu and Kuikun Liu and Jiangning Liu and Yuhong Liu and Kai Liu and Hongwei Liu and Zhoumianze Liu and Mengjie Liu and Ziyu Liu and Wenran Liu and Yang Liu and Liwei Liu and Kaiwen Liu and Junyao Lin and Junming Lin and Tianyang Lin and Dahua Lin and Jianze Liang and Linyang Li and Peiji Li and Zonglin Li and Zehao Li and Pengze Li and Guoyan Li and Lingkai Kong and Linglin Jing and Zhenjiang Jin and Feifei Jiang and Qian Jiang and Junhao Huang and Zixian Huang and Haian Huang and Zhouqi Hua and Han Hu and Linfeng Hou and Yinan He and Conghui He and Tianyao He and Xu Guo and Qipeng Guo and Aijia Guo and Yuzhe Gu and Lixin Gu and Jingyang Gong and Qiming Ge and Jiaye Ge and Songyang Gao and Jianfei Gao and Xinyu Fang and Caihua fan and Yue Fan and Yanhui Duan and Zichen Ding and Shengyuan Ding and Xuanlang Dai and Erfei Cui and Ganqu Cui and Pei Chu and Tao Chu and Guangran Cheng and Yu Cheng and Kai Chen and Yongkang Chen and Chiyu Chen and Guanzhou Chen and Qiaosheng Chen and Sitao Chen and Xin Chen and Haojiong Chen and Yicheng Chen and Weihan Cao and Yuhang Cao and Qinglong Cao and Lei Bai},year={2026},eprint={2603.25040},archivePrefix={arXiv},primaryClass={cs.LG},url={https://arxiv.org/abs/2603.25040},}```11 unmodified lines1213141516171842 unmodified lines616263646566673 unmodified lines7172737475767778170 unmodified lines24925025125225325425532 unmodified lines2882892902912922939 unmodified lines30330430511 unmodified lines<div> </div>[💻Github Repo](https://github.com/InternLM/Intern-S1) • [🤗Model Collections](https://huggingface.co/collections/internlm/intern-s1-6882e325e8ac1c58ba108aa5) • [📜Technical Report](https://arxiv.org/abs/2508.15763) • [💬Online Chat](https://chat.intern-ai.org.cn/)</div>42 unmodified lines### Serving> [!IMPORTANT]> Running a trillion-parameter model using the native Hugging Face forward method is challenging. We strongly recommend using an LLM inference engine (such as LMDeploy, vLLM, or sglang) to host Intern-S1-Pro and accessing the model via API.Intern-S1-Pro can be deployed using any of the following LLM inference frameworks:3 unmodified linesDetailed deployment examples for these frameworks are available in the [Model Deployment Guide](./deployment_guide.md).> Deployment support for the time-series module is under optimization and will be released soon.## Advanced Usage170 unmodified lines)```With serving Intern-S1-Pro models, you can dynamically control the thinking mode by adjusting the `enable_thinking` parameter in your requests.```pythonfrom openai import OpenAI32 unmodified linesprint(json.dumps(response.model_dump(), indent=2, ensure_ascii=False))```## CitationIf you find this work useful, feel free to give us a cite.9 unmodified linesurl={https://arxiv.org/abs/2508.15763},}```
16 unmodified lines171819202122232416 unmodified lines{{- 'Video ' + video_count.value|string + ': <|vision_start|><|video_pad|><|vision_end|>'-}}{%- elif 'text' in item %}{{- item.text }}{%- elif 'time_series' in item or item.type == 'time_series' %}{{- '<|ts|><TS_CONTEXT><|/ts|>'-}}{%- endif %}{%- endfor %}{%- endif %}16 unmodified lines17181920212216 unmodified lines{{- 'Video ' + video_count.value|string + ': <|vision_start|><|video_pad|><|vision_end|>'-}}{%- elif 'text' in item %}{{- item.text }}{%- endif %}{%- endfor %}{%- endif %}
57 unmodified lines5859606162636465666768697071727374757677787980818283848586878889909192939477 unmodified lines17217317417517617717857 unmodified lines},"vision_end_token_id": 151653,"vision_start_token_id": 151652,"ts_config": {"auto_map": {"AutoConfig": "configuration_interns1_pro.InternS1ProTimeSeriesConfig","AutoModel": "modeling_interns1_pro.InternS1ProTimeSeriesModel"},"activation_dropout": 0.0,"activation_function": "gelu","architectures": ["InternS1TimeSeriesModel"],"attention_dropout": 0.0,"d_model": 768,"dropout": 0.0,"dtype": "bfloat16","encoder_attention_heads": 8,"encoder_ffn_dim": 3072,"encoder_layerdrop": 0.0,"encoder_layers": 17,"model_type": "interns1_pro_time_series","max_source_positions": 1500,"num_mel_bins": 80,"out_hidden_size": 4096,"scale_embedding": false,"ts_adapt_in_dim": 256,"ts_adapt_out_dim": 1024,"use_cache": true,"attn_implementation": "eager"},"ts_end_id": 151684,"ts_start_id": 151683,"ts_token_id": 151685,"auto_map": {"AutoConfig": "configuration_interns1_pro.InternS1ProConfig","AutoModel": "modeling_interns1_pro.InternS1ProModel",77 unmodified lines"model.visual.blocks.17.mlp.linear_fc1","model.visual.blocks.4.norm2","model.visual.blocks.17.attn.qkv","model.time_series","model.language_model.layers.83.self_attn.k_norm","model.language_model.layers.47.post_attention_layernorm","model.language_model.layers.59.input_layernorm",57 unmodified lines58596061626377 unmodified lines14114214314414514657 unmodified lines},"vision_end_token_id": 151653,"vision_start_token_id": 151652,"auto_map": {"AutoConfig": "configuration_interns1_pro.InternS1ProConfig","AutoModel": "modeling_interns1_pro.InternS1ProModel",77 unmodified lines"model.visual.blocks.17.mlp.linear_fc1","model.visual.blocks.4.norm2","model.visual.blocks.17.attn.qkv","model.language_model.layers.83.self_attn.k_norm","model.language_model.layers.47.post_attention_layernorm","model.language_model.layers.59.input_layernorm",
14 unmodified lines15161718192021117 unmodified lines1391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981997 unmodified lines20720820921021121221321421521621721821922022122222322422514 unmodified linesfrom transformers.configuration_utils import PretrainedConfigfrom transformers.modeling_rope_utils import rope_config_validationfrom transformers import WhisperConfigclass InternS1ProTextConfig(PretrainedConfig):117 unmodified linesself.num_position_embeddings = num_position_embeddingsself.initializer_range = initializer_rangeclass InternS1ProTimeSeriesConfig(WhisperConfig):model_type = "interns1_pro_time_series"base_config_key = "ts_config"def __init__(self,ts_adapt_in_dim: int=256,ts_adapt_out_dim: int=1024,ts_hidden_dim: int=1024,ts_cnn_channels: list[int]=[1, 32, 64, 128, 128],ts_cnn_kernel_sizes: list[int]=[3, 5, 5, 5],ts_cnn_strides: list[int]=[2, 4, 4, 5],ts_cnn_paddings: list[int]=[1, 2, 2, 2],ts_concat_subsampling_in_channels: int=128,ts_concat_subsampling_concat_size: int=2,use_flash_attn: bool=False,**kwargs):super().__init__(**kwargs)self.ts_cnn_channels = ts_cnn_channelsself.ts_cnn_kernel_sizes = ts_cnn_kernel_sizesself.ts_cnn_strides = ts_cnn_stridesself.ts_cnn_paddings = ts_cnn_paddingsself.ts_concat_subsampling_in_channels = ts_concat_subsampling_in_channelsself.ts_concat_subsampling_concat_size = ts_concat_subsampling_concat_sizeself.ts_adapt_in_dim = ts_adapt_in_dimself.ts_adapt_out_dim = ts_adapt_out_dimself.ts_hidden_dim = ts_hidden_dimself.use_flash_attn = use_flash_attnassert self.ts_adapt_out_dim == self.ts_hidden_dim, "ts_adapt_out_dim should be equal to ts_hidden_dim"assert self.ts_concat_subsampling_in_channels == self.ts_cnn_channels[-1], "ts_concat_subsampling_in_channels should be equal to the out_channel of the last cnn layer"class InternS1ProConfig(PretrainedConfig):model_type = "interns1_pro"sub_configs = {"vision_config": InternS1ProVisionConfig, "text_config": InternS1ProTextConfig, 'ts_config':InternS1ProTimeSeriesConfig}keys_to_ignore_at_inference = ["past_key_values"]def __init__(self,text_config=None,vision_config=None,ts_config=None,image_token_id=151655,video_token_id=151656,vision_start_token_id=151652,vision_end_token_id=151653,ts_token_id=151685,ts_start_id=151683,ts_end_id=151684,tie_word_embeddings=False,**kwargs,):7 unmodified lineselif text_config is None:self.text_config = self.sub_configs["text_config"]()if isinstance(ts_config, dict):self.ts_config = self.sub_configs["ts_config"](**ts_config)elif ts_config is None:self.ts_config = self.sub_configs["ts_config"]()self.image_token_id = image_token_idself.video_token_id = video_token_idself.vision_start_token_id = vision_start_token_idself.vision_end_token_id = vision_end_token_idself.ts_token_id = ts_token_idself.ts_start_id = ts_start_idself.ts_end_id = ts_end_idsuper().__init__(**kwargs, tie_word_embeddings=tie_word_embeddings)__all__ = ["InternS1ProConfig", "InternS1ProTextConfig", "InternS1ProVisionConfig", "InternS1ProTimeSeriesConfig"]14 unmodified lines151617181920117 unmodified lines1381391401411421431441451461471481491501511521531541551561577 unmodified lines16516616716816917017117217317417514 unmodified linesfrom transformers.configuration_utils import PretrainedConfigfrom transformers.modeling_rope_utils import rope_config_validationclass InternS1ProTextConfig(PretrainedConfig):117 unmodified linesself.num_position_embeddings = num_position_embeddingsself.initializer_range = initializer_rangeclass InternS1ProConfig(PretrainedConfig):model_type = "interns1_pro"sub_configs = {"vision_config": InternS1ProVisionConfig, "text_config": InternS1ProTextConfig}keys_to_ignore_at_inference = ["past_key_values"]def __init__(self,text_config=None,vision_config=None,image_token_id=151655,video_token_id=151656,vision_start_token_id=151652,vision_end_token_id=151653,tie_word_embeddings=False,**kwargs,):7 unmodified lineselif text_config is None:self.text_config = self.sub_configs["text_config"]()self.image_token_id = image_token_idself.video_token_id = video_token_idself.vision_start_token_id = vision_start_token_idself.vision_end_token_id = vision_end_token_idsuper().__init__(**kwargs, tie_word_embeddings=tie_word_embeddings)__all__ = ["InternS1ProConfig", "InternS1ProTextConfig", "InternS1ProVisionConfig"]
1234562 unmodified lines910111213141543 unmodified lines5960616263646542 unmodified lines108109110111112113114115# Intern-S1-Pro Deployment GuideThe Intern-S1-Pro release is a 1T parameter model stored in FP8 format. Deployment requires at least **two 8-GPU H200** nodes, with either of the following configurations:- Tensor Parallelism (TP)- Data Parallelism (DP) + Expert Parallelism (EP)2 unmodified lines## LMDeployRequired version `lmdeploy>=0.12.1`- Tensor Parallelism43 unmodified lines## vLLMYou can use the vLLM nightly-built docker image `vllm/vllm-openai:nightly` to deploy. Refer to [using-docker](https://docs.vllm.ai/en/latest/deployment/docker/?h=docker) for more.```bash# node 042 unmodified lines## SGLangYou can use the docker image `lmsysorg/sglang:dev` to deploy. Refer to [using-docker](https://docs.sglang.io/get_started/install.html#method-3-using-docker) for more.- Tensor Parallelism + Expert Parallelism```bash1234562 unmodified lines910111213141543 unmodified lines5960616263646566676869707172737475767778798081828342 unmodified lines126127128129130131# Intern-S1-Pro Deployment GuideThe Intern-S1-Pro release is a 1T parameter model stored in FP8 format. Deployment requires at least two 8-GPU H200 nodes, with either of the following configurations:- Tensor Parallelism (TP)- Data Parallelism (DP) + Expert Parallelism (EP)2 unmodified lines## LMDeployRequired version `lmdeploy>=0.12.0`- Tensor Parallelism43 unmodified lines## vLLM- Tensor Parallelism + Expert Parallelism```bash# start ray on node 0 and node 1# node 0export VLLM_ENGINE_READY_TIMEOUT_S=10000vllm serve internlm/Intern-S1-Pro \--tensor-parallel-size 16 \--enable-expert-parallel \--distributed-executor-backend ray \--max-model-len 65536 \--trust-remote-code \--reasoning-parser deepseek_r1 \--enable-auto-tool-choice \--tool-call-parser hermes```- Data Parallelism + Expert Parallelism```bash# node 042 unmodified lines## SGLang- Tensor Parallelism + Expert Parallelism```bash
123version https://git-lfs.github.com/spec/v1oid sha256:6fab87c45c01a8695f97b5801bee2771ac6e874561ac773983397d958f1e7a00size 291982664123version https://git-lfs.github.com/spec/v1oid sha256:4150fadfb90bd9561c422b37ecc83fd5a30966f1e555bc9305b9fd5d2c914b0dsize 10240128123version https://git-lfs.github.com/spec/v1oid sha256:6aa1acb6e462542ccb55d50c9ba2097df081b6fd69b8ac5aaed1f0b30b14678esize 32236540123version https://git-lfs.github.com/spec/v1oid sha256:7de640c8e6f374c36de64b925b2c107896731ef642283e490e69125ec5c4eac1size 32204741
33 unmodified lines34353637383940414243397 unmodified lines441442443444445446447611 unmodified lines105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150055 unmodified lines155615571558155915601561156215631564156515661567156840 unmodified lines16091610161116121613161416151616161716181619162016211622162349 unmodified lines1673167416751676167716781679168016811 unmodified line16831684168516861687168816891690169116921693169420 unmodified lines171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741132 unmodified lines187418751876187718781879188018812 unmodified lines1884188518861887188818891890189118928 unmodified lines19011902190319041905190619071908190960 unmodified lines19701971197219731974197519761977197840 unmodified lines20192020202120222023202420252026202710 unmodified lines2038203920402041204220432044204520462 unmodified lines204920502051205220532054205520562057137 unmodified lines219521962197219821992200220133 unmodified linesfrom transformers.processing_utils import Unpackfrom transformers.utils import TransformersKwargs, auto_docstring, is_torchdynamo_compilingfrom transformers.utils.generic import OutputRecorder, check_model_inputsfrom .configuration_interns1_pro import InternS1ProConfig, InternS1ProTextConfig, InternS1ProVisionConfig, InternS1ProTimeSeriesConfigfrom transformers.models.whisper.modeling_whisper import WhisperEncoderLayerfrom transformers import WhisperPreTrainedModelimport math@use_kernel_forward_from_hub("RMSNorm")class Qwen3VLMoeTextRMSNorm(nn.Module):397 unmodified linesconfig: InternS1ProConfigbase_model_prefix = "model"supports_gradient_checkpointing = True_no_split_modules = ["InternS1ProMoeTextDecoderLayer", "Qwen3VLMoeVisionBlock",'WhisperEncoderLayer']_skip_keys_device_placement = ["past_key_values"]_supports_flash_attn = True_supports_sdpa = True611 unmodified lines)class InternS1ProTimeSeriesEncoder(WhisperPreTrainedModel):def __init__(self, config: InternS1ProTimeSeriesConfig):super().__init__(config)self.config = configself.dropout = config.dropoutself.layerdrop = config.encoder_layerdropself.embed_dim = config.d_modelself.num_mel_bins = config.num_mel_binsself.padding_idx = config.pad_token_idself.max_source_positions = config.max_source_positionsself.embed_scale = math.sqrt(self.embed_dim) if config.scale_embedding else 1.0self.conv1 = nn.Conv1d(self.num_mel_bins, self.embed_dim, kernel_size=3, padding=1)self.conv2 = nn.Conv1d(self.embed_dim, self.embed_dim, kernel_size=3, stride=2, padding=1)self.embed_positions = nn.Embedding(self.max_source_positions, self.embed_dim)self.layers = nn.ModuleList([WhisperEncoderLayer(config) for _ in range(config.encoder_layers)])self.layer_norm = nn.LayerNorm(config.d_model)self.gradient_checkpointing = Falseself.post_init()self.mask_type = Noneself.chunk_length = Noneself.adapt_in = nn.Linear(config.ts_adapt_in_dim, 80)self.adapt_out = nn.Linear(self.embed_dim, config.ts_adapt_out_dim)def _freeze_parameters(self):for param in self.parameters():param.requires_grad = Falseself._requires_grad = Falsedef get_input_embeddings(self) -> nn.Module:return self.conv1def set_input_embeddings(self, value: nn.Module):self.conv1 = valuedef define_masktype(self, masktype, chunk_length=None):self.mask_type = masktypeself.chunk_length = chunk_lengthdef _make_causal_mask(self,input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0):"""Make causal mask used for bi-directional self-attention."""bsz, tgt_len = input_ids_shapemask = torch.full((tgt_len, tgt_len), torch.finfo(dtype).min, device=device)mask_cond = torch.arange(mask.size(-1), device=device)mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0)mask = mask.to(dtype)if past_key_values_length > 0:mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1)return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length)# Copied from transformers.models.bart.modeling_bart._expand_maskdef _expand_mask(self, mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):"""Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`."""# print(mask.size())bsz, src_len = mask.size()tgt_len = tgt_len if tgt_len is not None else src_lenexpanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype)inverted_mask = 1.0 - expanded_maskreturn inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min)def _prepare_decoder_attention_mask(self, attention_mask, input_shape, inputs_embeds, past_key_values_length):# create causal mask# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]combined_attention_mask = Noneif input_shape[-1] > 1:combined_attention_mask = self._make_causal_mask(input_shape,inputs_embeds.dtype,device=inputs_embeds.device,past_key_values_length=past_key_values_length,)if attention_mask is not None:# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]expanded_attn_mask = self._expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1])combined_attention_mask = (expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask)return combined_attention_maskdef prepare_chunk_attention_mask(self, attention_mask, input_shape, inputs_embeds):block_size = round(self.chunk_length / 4 * 2)matrix_size = input_shape[1]matrix = torch.ones(matrix_size, matrix_size)num_full_blocks = round(matrix_size // block_size)remainder = matrix_size % block_sizefor i in range(num_full_blocks):row_start = i * block_sizecol_start = i * block_sizematrix[row_start:row_start + block_size, col_start:col_start + block_size] = torch.zeros(block_size, block_size)if remainder > 0:last_row_start = num_full_blocks * block_sizelast_col_start = num_full_blocks * block_sizematrix[last_row_start:last_row_start + remainder, last_col_start:last_col_start + remainder] = torch.zeros(remainder, remainder)matrix = matrix * -65504matrix = matrix.unsqueeze(0).unsqueeze(0).repeat(input_shape[0], 1, 1, 1)attention_mask = matrix.to(inputs_embeds.device)return attention_maskdef forward(self,input_features,attention_mask=None,head_mask=None,output_attentions=None,output_hidden_states=None,return_dict=None,):# (N, T, C) -> (T, N, C) -> (N, C, T)input_features = input_features.permute(1, 0, 2)input_features = self.adapt_in(input_features)input_features = input_features.permute(1, 2, 0)output_attentions = output_attentions if output_attentions is not None else self.config.output_attentionsoutput_hidden_states = (output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states)return_dict = return_dict if return_dict is not None else self.config.use_return_dict# (N, C, T) -> (N, C, T//2)inputs_embeds = nn.functional.gelu(self.conv1(input_features))inputs_embeds = nn.functional.gelu(self.conv2(inputs_embeds))# (N, C, T) -> (N, T, C)inputs_embeds = inputs_embeds.permute(0, 2, 1) # torch.Size([1, 100, 768])embed_pos = self.embed_positions.weight # torch.Size([1500, 768])if inputs_embeds.shape[1] > embed_pos.shape[0]:target_len = inputs_embeds.shape[1]padding = [0, 0, 0, target_len-embed_pos.shape[0]]embed_pos = nn.functional.pad(embed_pos, pad=padding, mode='constant', value=0)hidden_states = inputs_embeds[:, :embed_pos.shape[0], :] + embed_poselse:hidden_states = inputs_embeds + embed_pos[:inputs_embeds.shape[1], :]hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)encoder_states = () if output_hidden_states else Noneall_attentions = () if output_attentions else Noneinput_shape = inputs_embeds.size()[:-1]past_key_values_length = 0attention_mask = Noneif self.mask_type == 'chunk':attention_mask = self.prepare_chunk_attention_mask(attention_mask, input_shape, inputs_embeds)else:attention_mask = self._prepare_decoder_attention_mask(attention_mask, input_shape, inputs_embeds, past_key_values_length)if head_mask is not None:assert head_mask.size()[0] == (len(self.layers)), f"The head_mask should be specified for {len(self.layers)} layers, but it is for {head_mask.size()[0]}."for idx, encoder_layer in enumerate(self.layers):if output_hidden_states:encoder_states = encoder_states + (self.layer_norm(hidden_states),)# add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)to_drop = Falseif self.training:dropout_probability = torch.rand([])if dropout_probability < self.layerdrop: # skip the layerto_drop = Trueif to_drop:layer_outputs = (None, None)else:if self.gradient_checkpointing and self.training:def create_custom_forward(module):def custom_forward(*inputs):return module(*inputs, output_attentions)return custom_forwardlayer_outputs = torch.utils.checkpoint.checkpoint(create_custom_forward(encoder_layer),hidden_states,attention_mask,(head_mask[idx] if head_mask is not None else None),)else:layer_outputs = encoder_layer(hidden_states,attention_mask,layer_head_mask=(head_mask[idx] if head_mask is not None else None),output_attentions=output_attentions,)hidden_states = layer_outputs[0]if output_attentions:all_attentions = all_attentions + (layer_outputs[1],)# (N, T, C) -> (T, N, C)hidden_states = hidden_states.permute(1, 0, 2)hidden_states = self.layer_norm(hidden_states)hidden_states = self.adapt_out(hidden_states)# (T, N, C) -> (N, T, C)hidden_states = hidden_states.permute(1, 0, 2)if output_hidden_states:encoder_states = encoder_states + (hidden_states,)if not return_dict:return tuple(v for v in [hidden_states, encoder_states, all_attentions] if v is not None)return ModelOutput(last_hidden_state=hidden_states, hidden_states=encoder_states, attentions=all_attentions)class InternS1ProTimeSeriesConcatSubsampling(nn.Module):def __init__(self, in_channels: int, concat_size: int):super().__init__()self.in_channels = in_channelsself.out_channels = in_channels * concat_sizedef forward(self, ts_signals: torch.Tensor, ts_lens: torch.Tensor):if ts_signals.shape[1] % 2 != 0:ts_signals = ts_signals[:, :-1, :]even_frames = ts_signals[:, ::2, :]odd_frames = ts_signals[:, 1::2, :]ts_signals = torch.cat((even_frames, odd_frames), dim=2)ts_lens = ts_lens // 2return ts_signals, ts_lensclass InternS1ProTimeSeriesFixPositionalEncoding(nn.Module):def __init__(self, d_model, max_len=20000):super().__init__()pe = torch.zeros(max_len, d_model,dtype=torch.float)position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)div_term = torch.exp(torch.arange(0, d_model, 2, dtype=torch.float) * (-math.log(10000.0) / d_model))pe[:, 0::2] = torch.sin(position * div_term)pe[:, 1::2] = torch.cos(position * div_term)pe = pe.unsqueeze(0).transpose(0, 1).to(torch.float32) # (max_len, 1, d_model)self.register_buffer('pe', pe, persistent=True)def forward(self, x):# x: (seq_len, batch_size, d_model)x = x + self.pe[:x.size(0), :]return x.clone()class InternS1ProTimeSeriesMultiChannelAdaptiveSubsampling(nn.Module):def __init__(self, hidden_dim=128, nhead=8,num_encoder_layers = 1):super().__init__()self.conv = nn.Conv1d(in_channels=1, out_channels=hidden_dim, kernel_size=5, stride=1, padding=2)encoder_layers = nn.TransformerEncoderLayer(d_model=hidden_dim, nhead=nhead)self.transformer_encoder = nn.TransformerEncoder(encoder_layers, num_encoder_layers)self.pos_encoder = InternS1ProTimeSeriesFixPositionalEncoding(d_model=hidden_dim)self.subsampling = InternS1ProTimeSeriesConcatSubsampling(128, 2)def forward(self, inputs, input_lens, sr):features, feature_lens = self.forward_patch(inputs, input_lens, sr)outputs = featuresoutput_lens = feature_lensreturn outputs, output_lensdef forward_patch(self, inputs, input_lens, sr):sr = sr.float()strides = torch.floor(160/((1+torch.exp(-sr/100))**6))patch_sizes = strides * 2patched_outputs = []output_lens = []for i in range(len(inputs)):seq = inputs[i] # [seq_len, num_channel]ps = patch_sizes[i].item()st = strides[i].item()le = input_lens[i]output_len = torch.ceil((le - ps) / st) + 1pad_len = ((output_len - 1) * st + ps - le).long().item()if seq.ndim == 1:seq = seq.unsqueeze(-1)seq = nn.functional.pad(seq, (0, 0, 0, pad_len), "constant", 0)assert output_len > 0,(seq.shape, ps,st,le,output_len)output_lens.append(output_len)indices = (torch.arange(0, output_len * st, st).unsqueeze(1) + torch.arange(ps)).long()patched = seq[indices]output = self.forward_encoder(patched) #[num_patch, D]patched_outputs.append(output)outputs = nn.utils.rnn.pad_sequence(patched_outputs, batch_first=True)output_lens = torch.tensor(output_lens).squeeze().to(outputs.device).long()if output_lens.ndim == 0:output_lens = output_lens.unsqueeze(0)outputs, output_lens = self.subsampling(outputs.clone(), output_lens.clone())return outputs, output_lensdef forward_encoder(self, x):num_patch, patch_len, C = x.shape# conv1x = x.reshape(num_patch*C, 1, patch_len) # 每个 channel 当作独立样本送入 conv1x = nn.functional.relu((self.conv(x))) # [B*C, D1, L]x = x.permute(2,0,1) # [L, B*C, D1]x = self.pos_encoder(x) # [L, B*C, D1]x = self.transformer_encoder(x.to(torch.bfloat16))x = x.mean(0)x = x.reshape(num_patch,C,-1)return x.mean(1)class InternS1ProTimeSeriesProjector(nn.Module):def __init__(self, config: InternS1ProTimeSeriesConfig):super().__init__()self.layer_norm = nn.LayerNorm(config.ts_hidden_dim)self.linear_1 = nn.Linear(config.ts_hidden_dim, config.out_hidden_size)self.act = ACT2FN[config.activation_function]self.linear_2 = nn.Linear(config.out_hidden_size, config.out_hidden_size)def forward(self, ts_features):hidden_states = self.layer_norm(ts_features)hidden_states = self.linear_1(hidden_states)hidden_states = self.act(hidden_states)hidden_states = self.linear_2(hidden_states)return hidden_statesclass InternS1ProTimeSeriesModel(InternS1ProPreTrainedModel):main_input_name = 'time_series_signals'_supports_flash_attn_2 = Falseconfig_class = InternS1ProTimeSeriesConfig_no_split_modules = ['WhisperEncoderLayer']def __init__(self, config: InternS1ProTimeSeriesConfig):super().__init__(config)self.config = configself.encoder_embed = InternS1ProTimeSeriesMultiChannelAdaptiveSubsampling()self.encoder = InternS1ProTimeSeriesEncoder(config)self.projector = InternS1ProTimeSeriesProjector(config)def get_input_embeddings(self):return self.encoder_embeddef make_pad_mask(self, lengths: torch.Tensor) -> torch.Tensor:"""Args:lengths:A 1-D tensor containing sentence lengths.max_len:The length of masks.Returns:Return a 2-D bool tensor, where masked positionsare filled with `True` and non-masked positions arefilled with `False`.>>> lengths = torch.tensor([1, 3, 2, 5])>>> make_pad_mask(lengths)tensor([[False, True, True, True, True],[False, False, False, True, True],[False, False, True, True, True],[False, False, False, False, False]])"""assert lengths.ndim == 1, lengths.ndimmax_len = lengths.max()n = lengths.size(0)seq_range = torch.arange(0, max_len, device=lengths.device)expaned_lengths = seq_range.unsqueeze(0).expand(n, max_len)return expaned_lengths >= lengths.unsqueeze(-1)def forward(self,time_series_signals: Optional[torch.FloatTensor] = None,ts_lens: Optional[torch.Tensor] = None,sr: Optional[torch.Tensor] = None,output_hidden_states: Optional[bool] = None,return_dict: Optional[bool] = None,time_series_embeds: Optional[torch.FloatTensor] = None,):output_hidden_states = (output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states)return_dict = return_dict if return_dict is not None else self.config.use_return_dictif time_series_signals is None and time_series_embeds is None:raise ValueError('You have to specify time_series_signals or time_series_embeds')if time_series_embeds is not None and len(time_series_embeds.shape) == 3 and time_series_embeds.shape[-1] == self.config.ts_adapt_in_dim:time_series_embeds = time_series_embedselse:if (isinstance(time_series_signals,list) and len(time_series_signals[0].shape) == 2) \or (isinstance(time_series_signals, torch.Tensor) and len(time_series_signals.shape) == 3):time_series_embeds, ts_lens = self.encoder_embed(time_series_signals, ts_lens, sr)else:raise ValueError(f'wrong time_series_signals size: {time_series_signals[0].shape}')# [B, 64000, 1] -> [B, 200, 256] -> [B, 100, 1024]encoder_outputs = self.encoder(input_features=time_series_embeds,output_hidden_states=output_hidden_states,return_dict=return_dict,)# ts_lens after encoderts_lens = (ts_lens+1) // 2assert torch.all(ts_lens > 0), f"The length of time_series_embeds is so small. ts_lens: {ts_lens}"src_key_padding_mask = self.make_pad_mask(ts_lens)last_hidden_state = encoder_outputs.last_hidden_statets_pad_mask = src_key_padding_maskts_embeds = self.projector(last_hidden_state)return ts_embeds,ts_pad_mask@dataclass@auto_docstring(custom_intro="""55 unmodified lines# Reference: fix gemma3 grad acc #37208accepts_loss_kwargs = Falseconfig: InternS1ProConfig_no_split_modules = ["InternS1ProMoeTextDecoderLayer", "Qwen3VLMoeVisionBlock",'WhisperEncoderLayer']def __init__(self, config):super().__init__(config)self.visual = InternS1ProVisionModel._from_config(config.vision_config)self.language_model = InternS1ProTextModel._from_config(config.text_config)self.time_series = InternS1ProTimeSeriesModel._from_config(config.ts_config)# Initialize weights and apply final processingself.post_init()40 unmodified linessplit_sizes = (image_grid_thw.prod(-1) // self.visual.spatial_merge_size**2).tolist()image_embeds = torch.split(image_embeds, split_sizes)return image_embedsdef get_ts_feature(self, ts_values, ts_lens, sr):ts_embeds, ts_pad_mask = self.time_series(time_series_signals=ts_values,ts_lens=ts_lens,sr=sr,output_hidden_states=False,return_dict=True)return ts_embeds, ts_pad_maskdef get_placeholder_mask(self,49 unmodified linesimage_grid_thw: Optional[torch.LongTensor] = None,video_grid_thw: Optional[torch.LongTensor] = None,cache_position: Optional[torch.LongTensor] = None,ts_values: Union[torch.FloatTensor, list[torch.FloatTensor]] = None,ts_lens: Union[torch.Tensor, list[torch.Tensor]] = None,ts_sr: Union[torch.FloatTensor, list[torch.FloatTensor]] = None,**kwargs: Unpack[TransformersKwargs],) -> Union[tuple, Qwen3VLMoeModelOutputWithPast]:r"""1 unmodified lineThe temporal, height and width of feature shape of each image in LLM.video_grid_thw (`torch.LongTensor` of shape `(num_videos, 3)`, *optional*):The temporal, height and width of feature shape of each video in LLM.ts_values (`torch.FloatTensor` of shape `(batch_size, seq_len, num_channels)`, *optional*):The tensors corresponding to the input time series signals.ts_lens (`torch.Tensor` of shape `(batch_size,)`, *optional*):The valid lengths of each time series signal in the batch.ts_sr (`torch.FloatTensor` of shape `(batch_size,)`, *optional*):The sampling rates of each time series signal in the batch."""if (input_ids is None) ^ (inputs_embeds is not None):raise ValueError("You must specify exactly one of input_ids or inputs_embeds")20 unmodified lines)inputs_embeds = inputs_embeds.masked_scatter(video_mask, video_embeds)if pixel_values is None and pixel_values_videos is None and ts_values is not None:ts_features, ts_pad_mask = self.get_ts_feature(ts_values, ts_lens, ts_sr) # [B, T, C], [B, T]ts_features = ts_features[~ts_pad_mask].to(inputs_embeds.device, inputs_embeds.dtype) # [num_valid_ts_tokens, C]B, N, C = inputs_embeds.shapeinput_ids = input_ids.reshape(B * N)inputs_embeds = inputs_embeds.reshape(B * N, C)# replace ts_token in inputs_embeds and attention_maskts_placeholder = (input_ids == self.config.ts_token_id)n_ts_placeholders = ts_placeholder.sum().item()n_ts_tokens = ts_features.size(0)assert n_ts_placeholders == n_ts_tokens, f"[ERROR]: Mismatch: <TS_CONTEXT> tokens={n_ts_placeholders}, ts_embeds_valid={n_ts_tokens}"try:inputs_embeds[ts_placeholder] = inputs_embeds[ts_placeholder] * 0.0 + ts_featuresexcept Exception as e:print(f'warning: {e}, inputs_embeds[selected].shape={inputs_embeds[ts_placeholder].shape}, ts_embeds_valid.shape={ts_features.shape}')inputs_embeds[ts_placeholder] = inputs_embeds[ts_placeholder] * 0.0 + n_ts_tokens[:n_ts_placeholders]inputs_embeds = inputs_embeds.reshape(B, N, C)# input_ids = input_ids.reshape(B, N)if position_ids is None:batch_size, seq_length = inputs_embeds.shape[:2]if cache_position is not None:132 unmodified linesdef get_image_features(self, pixel_values: torch.FloatTensor, image_grid_thw: Optional[torch.LongTensor] = None):return self.model.get_image_features(pixel_values, image_grid_thw)def get_ts_feature(self, ts_values, ts_lens, sr):return self.model.get_ts_feature(ts_values, ts_lens, sr)# Make modules available through conditional class for BC@propertydef language_model(self):2 unmodified lines@propertydef visual(self):return self.model.visualdef time_series(self):return self.model.time_series@check_model_inputsdef forward(8 unmodified linespixel_values_videos: Optional[torch.FloatTensor] = None,image_grid_thw: Optional[torch.LongTensor] = None,video_grid_thw: Optional[torch.LongTensor] = None,ts_values: Optional[Union[torch.FloatTensor, list[torch.FloatTensor]]] = None,ts_lens: Optional[Union[torch.Tensor, list[torch.Tensor]]] = None,ts_sr: Optional[Union[torch.FloatTensor, list[torch.FloatTensor]]] = None,cache_position: Optional[torch.LongTensor] = None,logits_to_keep: Union[int, torch.Tensor] = 0,**kwargs: Unpack[TransformersKwargs],60 unmodified linespast_key_values=past_key_values,inputs_embeds=inputs_embeds,cache_position=cache_position,ts_values=ts_values,ts_lens=ts_lens,ts_sr=ts_sr,**kwargs,)40 unmodified linespixel_values_videos=None,image_grid_thw=None,video_grid_thw=None,ts_values=None,ts_lens=None,ts_sr=None,**kwargs,):# Overwritten -- in specific circumstances we don't want to forward image inputs to the model10 unmodified linesimage_grid_thw=image_grid_thw,video_grid_thw=video_grid_thw,use_cache=use_cache,ts_values=ts_values,ts_lens=ts_lens,ts_sr=ts_sr,**kwargs,)2 unmodified linesif cache_position[0] != 0:model_inputs["pixel_values"] = Nonemodel_inputs["pixel_values_videos"] = Nonemodel_inputs["ts_values"] = Nonemodel_inputs["ts_lens"] = Nonemodel_inputs["ts_sr"] = Nonereturn model_inputs137 unmodified lines__all__ = ["InternS1ProVisionModel","InternS1ProTimeSeriesModel","InternS1ProForConditionalGeneration","InternS1ProModel","InternS1ProPreTrainedModel",33 unmodified lines3435363738394041397 unmodified lines439440441442443444445611 unmodified lines10571058105910601061106255 unmodified lines11181119112011211122112311241125112611271128112940 unmodified lines11701171117211731174117549 unmodified lines1225122612271228122912301 unmodified line12321233123412351236123720 unmodified lines125812591260126112621263132 unmodified lines1396139713981399140014012 unmodified lines1404140514061407140814098 unmodified lines14181419142014211422142360 unmodified lines14841485148614871488148940 unmodified lines15301531153215331534153510 unmodified lines1546154715481549155015512 unmodified lines155415551556155715581559137 unmodified lines16971698169917001701170233 unmodified linesfrom transformers.processing_utils import Unpackfrom transformers.utils import TransformersKwargs, auto_docstring, is_torchdynamo_compilingfrom transformers.utils.generic import OutputRecorder, check_model_inputsfrom .configuration_interns1_pro import InternS1ProConfig, InternS1ProTextConfig, InternS1ProVisionConfig@use_kernel_forward_from_hub("RMSNorm")class Qwen3VLMoeTextRMSNorm(nn.Module):397 unmodified linesconfig: InternS1ProConfigbase_model_prefix = "model"supports_gradient_checkpointing = True_no_split_modules = ["InternS1ProMoeTextDecoderLayer", "Qwen3VLMoeVisionBlock"]_skip_keys_device_placement = ["past_key_values"]_supports_flash_attn = True_supports_sdpa = True611 unmodified lines)@dataclass@auto_docstring(custom_intro="""55 unmodified lines# Reference: fix gemma3 grad acc #37208accepts_loss_kwargs = Falseconfig: InternS1ProConfig_no_split_modules = ["InternS1ProMoeTextDecoderLayer", "Qwen3VLMoeVisionBlock"]def __init__(self, config):super().__init__(config)self.visual = InternS1ProVisionModel._from_config(config.vision_config)self.language_model = InternS1ProTextModel._from_config(config.text_config)# Initialize weights and apply final processingself.post_init()40 unmodified linessplit_sizes = (image_grid_thw.prod(-1) // self.visual.spatial_merge_size**2).tolist()image_embeds = torch.split(image_embeds, split_sizes)return image_embedsdef get_placeholder_mask(self,49 unmodified linesimage_grid_thw: Optional[torch.LongTensor] = None,video_grid_thw: Optional[torch.LongTensor] = None,cache_position: Optional[torch.LongTensor] = None,**kwargs: Unpack[TransformersKwargs],) -> Union[tuple, Qwen3VLMoeModelOutputWithPast]:r"""1 unmodified lineThe temporal, height and width of feature shape of each image in LLM.video_grid_thw (`torch.LongTensor` of shape `(num_videos, 3)`, *optional*):The temporal, height and width of feature shape of each video in LLM."""if (input_ids is None) ^ (inputs_embeds is not None):raise ValueError("You must specify exactly one of input_ids or inputs_embeds")20 unmodified lines)inputs_embeds = inputs_embeds.masked_scatter(video_mask, video_embeds)if position_ids is None:batch_size, seq_length = inputs_embeds.shape[:2]if cache_position is not None:132 unmodified linesdef get_image_features(self, pixel_values: torch.FloatTensor, image_grid_thw: Optional[torch.LongTensor] = None):return self.model.get_image_features(pixel_values, image_grid_thw)# Make modules available through conditional class for BC@propertydef language_model(self):2 unmodified lines@propertydef visual(self):return self.model.visual@check_model_inputsdef forward(8 unmodified linespixel_values_videos: Optional[torch.FloatTensor] = None,image_grid_thw: Optional[torch.LongTensor] = None,video_grid_thw: Optional[torch.LongTensor] = None,cache_position: Optional[torch.LongTensor] = None,logits_to_keep: Union[int, torch.Tensor] = 0,**kwargs: Unpack[TransformersKwargs],60 unmodified linespast_key_values=past_key_values,inputs_embeds=inputs_embeds,cache_position=cache_position,**kwargs,)40 unmodified linespixel_values_videos=None,image_grid_thw=None,video_grid_thw=None,**kwargs,):# Overwritten -- in specific circumstances we don't want to forward image inputs to the model10 unmodified linesimage_grid_thw=image_grid_thw,video_grid_thw=video_grid_thw,use_cache=use_cache,**kwargs,)2 unmodified linesif cache_position[0] != 0:model_inputs["pixel_values"] = Nonemodel_inputs["pixel_values_videos"] = Nonereturn model_inputs137 unmodified lines__all__ = ["InternS1ProVisionModel","InternS1ProForConditionalGeneration","InternS1ProModel","InternS1ProPreTrainedModel",
17 unmodified lines181920212223243 unmodified lines282930313233347 unmodified lines4243444546474821 unmodified lines707172737475764 unmodified lines818283848586878889909111 unmodified lines10310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323413 unmodified lines24824925025125225325490 unmodified lines3453463473483493503513523533543553563573583593603613623633643653665 unmodified lines37237337437537637737817 unmodified lines# 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 typing import Union,Optionalimport numpy as np3 unmodified linesfrom transformers.tokenization_utils_base import PreTokenizedInput, TextInputfrom transformers.utils import loggingfrom transformers.video_utils import VideoInputimport oslogger = logging.get_logger(__name__)7 unmodified lines"return_mm_token_type_ids": False,},"videos_kwargs": {"return_metadata": True},"time_series_kwargs": {},}21 unmodified linesdef __init__(self, image_processor=None, tokenizer=None, video_processor=None, chat_template=None, **kwargs):self.image_token = "<|image_pad|>" if not hasattr(tokenizer, "image_token") else tokenizer.image_tokenself.video_token = "<|video_pad|>" if not hasattr(tokenizer, "video_token") else tokenizer.video_tokenself.ts_token = "<TS_CONTEXT>" if not hasattr(tokenizer, "ts_token") else tokenizer.ts_tokenself.image_token_id = (tokenizer.image_token_idif getattr(tokenizer, "image_token_id", None)4 unmodified linesif getattr(tokenizer, "video_token_id", None)else tokenizer.convert_tokens_to_ids(self.video_token))self.ts_token_id = (tokenizer.ts_token_idif getattr(tokenizer, "ts_token_id", None)else tokenizer.convert_tokens_to_ids(self.ts_token))super().__init__(image_processor, tokenizer, video_processor, chat_template=chat_template)self.vision_start_token = ("<|vision_start|>" if not hasattr(tokenizer, "vision_start_token") else tokenizer.vision_start_token11 unmodified linesif getattr(tokenizer, "vision_end_token_id", None)else tokenizer.convert_tokens_to_ids(self.vision_end_token))self.ts_start_token = ("<|ts|>" if not hasattr(tokenizer, "ts_start_token") else tokenizer.ts_start_token)self.ts_end_token = ("<|/ts|>" if not hasattr(tokenizer, "ts_end_token") else tokenizer.ts_end_token)self.ts_start_token_id = (tokenizer.ts_start_token_idif getattr(tokenizer, "ts_start_token_id", None)else tokenizer.convert_tokens_to_ids(self.ts_start_token))self.ts_end_token_id = (tokenizer.ts_end_token_idif getattr(tokenizer, "ts_end_token_id", None)else tokenizer.convert_tokens_to_ids(self.ts_end_token))def time_series_preprocessor(self,conversation):if isinstance(conversation, (list, tuple)) and (isinstance(conversation[0], (list, tuple)) or hasattr(conversation[0], "content")):conversations = conversationelse:conversations = [conversation]batch_time_series = []batch_time_series_metadata = []for conversation in conversations:for message in conversation:if message['role'] != "user": continuetime_series_fnames = [content["data"]for content in message["content"]if content.get("type") == "time_series" and "data" in content]time_series_rates = [content.get("sampling_rate", None)for content in message["content"]if content.get("type") == "time_series"]for path, rate in zip(time_series_fnames, time_series_rates):batch_time_series.append(path)batch_time_series_metadata.append(rate)return {"time_series_paths": batch_time_series if batch_time_series else None,"time_series_sampling_rates": batch_time_series_metadata if batch_time_series_metadata else None}def time_series_processor(self,ts_paths: list[str],sampling_rates: list[float],do_normalize=True,do_truncate=True,)-> BatchFeature:assert len(ts_paths)==len(sampling_rates), "ts_paths and sampling_rates must have the same length"ts_values=[]ts_sr=[]ts_lens=[]for idx,ts_path in enumerate(ts_paths):sr=sampling_rates[idx]ext = os.path.splitext(ts_path)[-1].lower()if ext in [".wav",'.mp3','.flac']:try:import soundfile as sfexcept ImportError:raise ImportError("Please install soundfile to process audio files.")ts_input, sr = sf.read(ts_path) # ts_input: np.ndarray, shape [T] or [T, C]elif ext == ".csv":pd = __import__("pandas")df = pd.read_csv(ts_path, header=None)ts_input = df.values # [T, C]elif ext == ".npy":ts_input = np.load(ts_path) # [T, C]else:raise ValueError(f"Unsupported file format: {ext}")# ts_tensor = torch.from_numpy(ts_input).float()if not isinstance(ts_input, np.ndarray):ts_input = np.array(ts_input, dtype=np.float32)if do_normalize:mean = ts_input.mean(axis=0, keepdims=True)std = ts_input.std(axis=0, keepdims=True)ts_input = (ts_input - mean) / (std + 1e-8)if do_truncate and len(ts_input)>240000:ts_input=ts_input[:240000] # truncate to 240k to avoid oomif ts_input.ndim==1:ts_input=ts_input[:, None] #[T,C]ts_len=ts_input.shape[0]if sr is None or sr == 0: # if no sr providedsr = ts_len/4ts_values.append(ts_input)ts_sr.append(sr)ts_lens.append(ts_len)ts_lens = np.array(ts_lens)ts_sr = np.array(ts_sr)num_ts_tokens = self._get_num_ts_tokens(sampling_rates=ts_sr,ts_lens=ts_lens)return BatchFeature(data={"ts_values": ts_values,"ts_sr":ts_sr,"ts_lens":ts_lens,"num_ts_tokens":num_ts_tokens})def _get_num_ts_tokens(self,sampling_rates,ts_lens):strides = np.floor(160/((1+np.exp(-sampling_rates/100))**6))patch_sizes = strides * 2embed_lengths = (np.ceil((ts_lens - patch_sizes) / strides) + 1).astype(np.int64)num_ts_tokens=[(embed_length // 2 + 1) // 2 for embed_length in embed_lengths]return num_ts_tokensdef __call__(self,images: ImageInput = None,text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None,videos: VideoInput = None,time_series_paths: Optional[list[str]]=None,time_series_sampling_rates: Optional[list[float]]=None,**kwargs: Unpack[InternS1ProProcessorKwargs],) -> BatchFeature:"""13 unmodified linesvideos (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`):The image or batch of videos to be prepared. Each video can be a 4D NumPy array or PyTorchtensor, or a nested list of 3D frames. Both channels-first and channels-last formats are supported.time_series_signals (`list[np.ndarray]`, `list[torch.Tensor]`):return_tensors (`str` or [`~utils.TensorType`], *optional*):If set, will return tensors of a particular framework. Acceptable values are:- `'pt'`: Return PyTorch `torch.Tensor` objects.90 unmodified linestext[i] = text[i].replace("<|placeholder|>", self.video_token)time_series_inputs = {}if images is None and videos is None and time_series_paths is not None:assert time_series_sampling_rates is not None, "If time_series_signals is provided, time_series_sampling_rates must also be provided."assert len(time_series_paths) == len(time_series_sampling_rates), "The number of time series signals must match the number of sampling rates."time_series_inputs = self.time_series_processor(ts_paths=time_series_paths, sampling_rates=time_series_sampling_rates)num_ts_tokens = time_series_inputs.pop("num_ts_tokens")assert len(num_ts_tokens) == len(text), "The number of time series signals must match the number of text prompts."for i in range(len(text)):if f"{self.ts_start_token}{self.ts_token}{self.ts_end_token}" in text[i]:ts_placeholder = self.ts_start_token + self.ts_token * num_ts_tokens[i] + self.ts_end_tokentext[i] = text[i].replace(f"{self.ts_start_token}{self.ts_token}{self.ts_end_token}", ts_placeholder, 1)elif self.ts_token in text[i]:text[i] = text[i].replace(self.ts_token, self.ts_token * num_ts_tokens[i])return_tensors = output_kwargs["text_kwargs"].pop("return_tensors", None)return_mm_token_type_ids = output_kwargs["text_kwargs"].pop("return_mm_token_type_ids", None)text_inputs = self.tokenizer(text, **output_kwargs["text_kwargs"])5 unmodified linesmm_token_type_ids[array_ids == self.image_token_id] = 1text_inputs["mm_token_type_ids"] = mm_token_type_ids.tolist()return BatchFeature(data={**text_inputs, **image_inputs, **videos_inputs,**time_series_inputs}, tensor_type=return_tensors)def _get_num_multimodal_tokens(self, image_sizes=None, video_sizes=None, **kwargs):"""17 unmodified lines181920212223243 unmodified lines2829303132337 unmodified lines41424344454621 unmodified lines6869707172734 unmodified lines78798081828311 unmodified lines959697989910010110210310410510613 unmodified lines12012112212312412590 unmodified lines2162172182192202215 unmodified lines22722822923023123223317 unmodified lines# 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 typing import Unionimport numpy as np3 unmodified linesfrom transformers.tokenization_utils_base import PreTokenizedInput, TextInputfrom transformers.utils import loggingfrom transformers.video_utils import VideoInputlogger = logging.get_logger(__name__)7 unmodified lines"return_mm_token_type_ids": False,},"videos_kwargs": {"return_metadata": True},}21 unmodified linesdef __init__(self, image_processor=None, tokenizer=None, video_processor=None, chat_template=None, **kwargs):self.image_token = "<|image_pad|>" if not hasattr(tokenizer, "image_token") else tokenizer.image_tokenself.video_token = "<|video_pad|>" if not hasattr(tokenizer, "video_token") else tokenizer.video_tokenself.image_token_id = (tokenizer.image_token_idif getattr(tokenizer, "image_token_id", None)4 unmodified linesif getattr(tokenizer, "video_token_id", None)else tokenizer.convert_tokens_to_ids(self.video_token))super().__init__(image_processor, tokenizer, video_processor, chat_template=chat_template)self.vision_start_token = ("<|vision_start|>" if not hasattr(tokenizer, "vision_start_token") else tokenizer.vision_start_token11 unmodified linesif getattr(tokenizer, "vision_end_token_id", None)else tokenizer.convert_tokens_to_ids(self.vision_end_token))def __call__(self,images: ImageInput = None,text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None,videos: VideoInput = None,**kwargs: Unpack[InternS1ProProcessorKwargs],) -> BatchFeature:"""13 unmodified linesvideos (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`):The image or batch of videos to be prepared. Each video can be a 4D NumPy array or PyTorchtensor, or a nested list of 3D frames. Both channels-first and channels-last formats are supported.return_tensors (`str` or [`~utils.TensorType`], *optional*):If set, will return tensors of a particular framework. Acceptable values are:- `'pt'`: Return PyTorch `torch.Tensor` objects.90 unmodified linestext[i] = text[i].replace("<|placeholder|>", self.video_token)return_tensors = output_kwargs["text_kwargs"].pop("return_tensors", None)return_mm_token_type_ids = output_kwargs["text_kwargs"].pop("return_mm_token_type_ids", None)text_inputs = self.tokenizer(text, **output_kwargs["text_kwargs"])5 unmodified linesmm_token_type_ids[array_ids == self.image_token_id] = 1text_inputs["mm_token_type_ids"] = mm_token_type_ids.tolist()return BatchFeature(data={**text_inputs, **image_inputs, **videos_inputs}, tensor_type=return_tensors)def _get_num_multimodal_tokens(self, image_sizes=None, video_sizes=None, **kwargs):"""
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778from pathlib import Pathimport torchfrom transformers import AutoConfig, AutoModelForCausalLM, AutoProcessor
model_path = Path(__file__).parent.resolve()print(f"Loading model from: {model_path}")
# 加载模型配置config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)print(f"Model config: {config.model_type}")print(f"Architecture: {config.architectures}")
# 加载处理器(tokenizer + image processor + ts processor)print("\nLoading processor...")processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
# 加载模型(使用 bfloat16 精度和自动设备映射)print("\nLoading model...")model = AutoModelForCausalLM.from_pretrained( model_path, dtype=torch.bfloat16, device_map="auto", # attn_implementation="flash_attention_2", #时序暂不支持flash_attn,load加这行会报错 trust_remote_code=True)
print(f"✓ Model loaded successfully!")print(f"Model type: {type(model).__name__}")print(f"Model device: {model.device}")
# ============================================================================# 测试 3: 时序对话# ============================================================================print("\n" + "=" * 80)print("测试 3: 时序对话")print("=" * 80)
messages = [ { "role": "user", "content": [ {"type": "time_series", "data": "./0092638_seism.npy", "sampling_rate": 100}, {"type": "text", "text": "Please determine whether an Earthquake event has occurred in the provided time-series data. If so, please specify the starting time point indices of the P-wave and S-wave in the event."}, ], }]
time_series_inputs = processor.time_series_preprocessor(messages)multimodal_inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", enable_thinking=False, **time_series_inputs).to(model.device, dtype=torch.bfloat16)
print("\n生成时序回复...")with torch.inference_mode(): multimodal_generated_ids = model.generate( **multimodal_inputs, max_new_tokens=200, do_sample=False, temperature=1.0, )
# 提取生成的 token(去除输入部分)multimodal_generated_ids_trimmed = [ out_ids[len(in_ids):] for in_ids, out_ids in zip(multimodal_inputs.input_ids, multimodal_generated_ids)]
# 解码为文本multimodal_output = processor.batch_decode( multimodal_generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False)
print("\n" + "-" * 80)print("时序输出:")print("-" * 80)print(multimodal_output[0])print("-" * 80)print("\n✅ 时序功能测试完成!")23 unmodified lines2425262728293031323334353637471 unmodified lines50951051151251351451554 unmodified lines570571572573574575576143 unmodified lines72072172272372472523 unmodified linesimport regex as reimport sentencepiece as spmimport transformersfrom transformers.tokenization_utils_base import AddedToken, TextInputfrom transformers.utils import loggingfrom packaging import versionif version.parse(transformers.__version__) >= version.parse("5.0.0"):from transformers.tokenization_python import PreTrainedTokenizerelse:from transformers.tokenization_utils import PreTrainedTokenizerlogger = logging.get_logger(__name__)471 unmodified linespad_token="<|endoftext|>",clean_up_tokenization_spaces=False,split_special_tokens=False,special_tokens_pattern="none",**kwargs,):bos_token = (54 unmodified linespad_token=pad_token,clean_up_tokenization_spaces=clean_up_tokenization_spaces,split_special_tokens=split_special_tokens,special_tokens_pattern="none",**kwargs,)143 unmodified linestext, kwargs = self.prepare_for_tokenization(text, **kwargs)if hasattr(self, "do_lower_case") and self.do_lower_case:# convert non-special tokens to lowercase. Might be super slow as well?escaped_special_toks = [re.escape(s_tok) for s_tok in (self.all_special_tokens)]23 unmodified lines2425262728293031323334471 unmodified lines50650750850951051154 unmodified lines566567568569570571143 unmodified lines71571671771871972072172272323 unmodified linesimport regex as reimport sentencepiece as spmfrom transformers.tokenization_utils_base import AddedToken, TextInputfrom transformers.tokenization_utils import PreTrainedTokenizerfrom transformers.utils import logging# from transformers.utils.import_utils import requireslogger = logging.get_logger(__name__)471 unmodified linespad_token="<|endoftext|>",clean_up_tokenization_spaces=False,split_special_tokens=False,**kwargs,):bos_token = (54 unmodified linespad_token=pad_token,clean_up_tokenization_spaces=clean_up_tokenization_spaces,split_special_tokens=split_special_tokens,**kwargs,)143 unmodified linestext, kwargs = self.prepare_for_tokenization(text, **kwargs)if kwargs:logger.warning(f"Keyword arguments {kwargs} not recognized.")if hasattr(self, "do_lower_case") and self.do_lower_case:# convert non-special tokens to lowercase. Might be super slow as well?escaped_special_toks = [re.escape(s_tok) for s_tok in (self.all_special_tokens)]
20 unmodified lines212223242526272820 unmodified linesfrom transformers.feature_extraction_utils import BatchFeaturefrom transformers.image_utils import ChannelDimension, PILImageResampling, SizeDict, get_image_sizefrom transformers.processing_utils import Unpack, VideosKwargsfrom transformers.utils import TensorType, loggingfrom transformers.video_processing_utils import BaseVideoProcessorfrom transformers.video_utils import VideoMetadata, group_videos_by_shape, reorder_videos20 unmodified lines212223242526272820 unmodified linesfrom transformers.feature_extraction_utils import BatchFeaturefrom transformers.image_utils import ChannelDimension, PILImageResampling, SizeDict, get_image_sizefrom transformers.processing_utils import Unpack, VideosKwargsfrom transformers.utils import TensorType, add_start_docstrings, loggingfrom transformers.video_processing_utils import BASE_VIDEO_PROCESSOR_DOCSTRING, BaseVideoProcessorfrom transformers.video_utils import VideoMetadata, group_videos_by_shape, reorder_videos
0092638_seism.npyDeleted-3
README.md+5-131
chat_template.jinja-2
config.json-32
configuration_interns1_pro.py+2-52
deployment_guide.md+21-5
model-time_series-00001-of-00002.safetensorsDeleted-3
model-time_series-00002-of-00002.safetensorsDeleted-3
model.safetensors.index.json+2-2
modeling_interns1_pro.py+4-503
processing_interns1_pro.py+2-147
test_inference_ts.pyDeleted-78
tokenization_interns1.py+6-8
video_processing_interns1_pro.py+2-2