luotingdan commited on
Commit ·
049cfe1
1
Parent(s): ac3d9d9
add model deploy
Browse filesFiles changed (6)
- README.md+88-42
- configuration_step_vl.pyAdded+77
- modeling_step_vl.pyAdded+568
- processing_step3.pyAdded+464
- processor_config.jsonAdded+6
- vision_encoder.pyAdded+468
1234567826 unmodified lines35363738394041424344451 unmodified line47484950515253545556575859606162632 unmodified lines6667686970717273747576777879808182838485868788899091929394959697989910017 unmodified lines11811912012112212312412512635 unmodified lines1621631641651661671684 unmodified lines173174175176---license: apache-2.0base_model:- stepfun-ai/Step3-VL-10B-Base---<div align="center"><div align="center" style="display: flex; justify-content: center; align-items: center;">26 unmodified lines## 📥 Model Zoo| Model Name | Type | Hugging Face | ModelScope ||:-----------|:-----|:------------:|:----------:|| **STEP3-VL-10B-Base** | Base | [🤗 Download](https://huggingface.co/stepfun-ai/Step3-VL-10B-Base) | [🤖 Download](https://modelscope.cn/models/stepfun-ai/Step3-VL-10B-Base) || **STEP3-VL-10B** | Chat | [🤗 Download](https://huggingface.co/stepfun-ai/Step3-VL-10B) | [🤖 Download](https://modelscope.cn/models/stepfun-ai/Step3-VL-10B) |## 📊 Performance1 unmodified line### Comparison with Larger Models (10×–20× Larger)| Benchmark | STEP3-VL-10B (SeRe) | STEP3-VL-10B (PaCoRe) | GLM-4.6V (106B-A12B) | Qwen3-VL (235B-A22B) | Gemini-2.5-Pro | Seed-1.5-VL ||:----------|:-------------------:|:---------------------:|:--------------------:|:--------------------:|:--------------:|:-----------:|| **MMMU** | 78.11 | **80.11** | 75.20 | 78.70 | 83.89 | 79.11 || **MathVista** | 83.97 | **85.50** | 83.51 | 85.10 | 83.88 | 85.60 || **MathVision** | 70.81 | **75.95** | 63.50 | 72.10 | 73.30 | 68.70 || **MMBench (EN)** | 92.05 | 92.38 | 92.75 | 92.70 | **93.19** | 92.11 || **MMStar** | 77.48 | 77.64 | 75.30 | 76.80 | **79.18** | 77.91 || **OCRBench** | 86.75 | **89.00** | 86.20 | 87.30 | 85.90 | 85.20 || **AIME 2025** | 87.66 | **94.43** | 71.88 | 83.59 | 83.96 | 64.06 || **HMMT 2025** | 78.18 | **92.14** | 57.29 | 67.71 | 65.68 | 51.30 || **LiveCodeBench** | 75.77 | **76.43** | 48.71 | 69.45 | 72.01 | 57.10 |<!-- > **Note:** **SeRe** (Sequential Reasoning) uses a max length of 64K tokens; **PaCoRe** (Parallel Coordinated Reasoning) synthesizes 16 SeRe rollouts with a max length of 128K tokens. -->2 unmodified lines> **SeRe (Sequential Reasoning):** The standard inference mode using sequential generation (Chain-of-Thought) with a max length of 64K tokens.>> **PaCoRe (Parallel Coordinated Reasoning):** An advanced mode that scales test-time compute. It aggregates evidence from **16 parallel rollouts** to synthesize a final answer, utilizing a max context length of 128K tokens.>> *Unless otherwise stated, scores below refer to the standard SeRe mode. Higher scores achieved via PaCoRe are explicitly marked.*### Comparison with Open-Source Models (7B–10B)| Category | Benchmark | STEP3-VL-10B | GLM-4.6V-Flash (9B) | Qwen3-VL-Thinking (8B) | InternVL-3.5 (8B) | MiMo-VL-RL-2508 (7B) ||:---------|:----------|:------------:|:-------------------:|:----------------------:|:-----------------:|:--------------------:|| **STEM Reasoning** | MMMU | **78.11** | 71.17 | 73.53 | 71.69 | 71.14 || | MathVision | **70.81** | 54.05 | 59.60 | 52.05 | 59.65 || | MathVista | **83.97** | 82.85 | 78.50 | 76.78 | 79.86 || | PhyX | **59.45** | 52.28 | 57.67 | 50.51 | 56.00 || **Recognition** | MMBench (EN) | **92.05** | 91.04 | 90.55 | 88.20 | 89.91 || | MMStar | **77.48** | 74.26 | 73.58 | 69.83 | 72.93 || | ReMI | **67.29** | 60.75 | 57.17 | 52.65 | 63.13 || **OCR & Document** | OCRBench | **86.75** | 85.97 | 82.85 | 83.70 | 85.40 || | AI2D | **89.35** | 88.93 | 83.32 | 82.34 | 84.96 || **GUI Grounding** | ScreenSpot-V2 | 92.61 | 92.14 | **93.60** | 84.02 | 90.82 || | ScreenSpot-Pro | **51.55** | 45.68 | 46.60 | 15.39 | 34.84 || | OSWorld-G | **59.02** | 54.71 | 56.70 | 31.91 | 50.54 || **Spatial** | BLINK | **66.79** | 64.90 | 62.78 | 55.40 | 62.57 || | All-Angles-Bench | **57.21** | 53.24 | 45.88 | 45.29 | 51.62 || **Code** | HumanEval-V | **66.05** | 29.26 | 26.94 | 24.31 | 31.96 |### Key Capabilities* **STEM Reasoning:** Achieves **94.43%** on AIME 2025 and **75.95%** on MathVision (with PaCoRe), demonstrating exceptional complex reasoning capabilities that outperform models 10×–20× larger.* **Visual Perception:** Records **92.05%** on MMBench and **80.11%** on MMMU, establishing strong general visual understanding and multimodal reasoning.* **GUI & OCR:** Delivers state-of-the-art performance on ScreenSpot-V2 (**92.61%**), ScreenSpot-Pro (**51.55%**), and OCRBench (**86.75%**), optimized for agentic and document understanding tasks.* **Spatial Understanding:** Demonstrates emergent spatial awareness with **66.79%** on BLINK and **57.21%** on All-Angles-Bench, establishing strong potential for embodied intelligence applications.## 🏗️ Architecture & Training17 unmodified lines- **RLHF:** 300 iterations (Task: open-ended generation).- **PaCoRe Training:** 500 iterations (Context length: 64K max sequence).## 🛠️ Quick Start### RequirementsTo run STEP3-VL-10B efficiently, we recommend setting up a Python environment (>=3.10) with **vLLM**:35 unmodified linesprint(f"Output: {outputs[0].outputs[0].text}")```## 📜 CitationIf you find this project useful in your research, please cite our technical report:4 unmodified lines## 📄 LicenseThis project is open-sourced under the [Apache 2.0 License](https://www.google.com/search?q=LICENSE).12345678926 unmodified lines363738394041424344451 unmodified line47484950515253545556575859606162632 unmodified lines6667686970717273747576777879808182838485868788899091929394959697989910017 unmodified lines11811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217335 unmodified lines2092102112122132144 unmodified lines219220221222---license: apache-2.0base_model:- stepfun-ai/Step3-VL-10B-Base---<div align="center"><div align="center" style="display: flex; justify-content: center; align-items: center;">26 unmodified lines## 📥 Model Zoo| Model Name | Type | Hugging Face | ModelScope || :-------------------- | :--- | :----------------------------------------------------------------: | :----------------------------------------------------------------------: || **STEP3-VL-10B-Base** | Base | [🤗 Download](https://huggingface.co/stepfun-ai/Step3-VL-10B-Base) | [🤖 Download](https://modelscope.cn/models/stepfun-ai/Step3-VL-10B-Base) || **STEP3-VL-10B** | Chat | [🤗 Download](https://huggingface.co/stepfun-ai/Step3-VL-10B) | [🤖 Download](https://modelscope.cn/models/stepfun-ai/Step3-VL-10B) |## 📊 Performance1 unmodified line### Comparison with Larger Models (10×–20× Larger)| Benchmark | STEP3-VL-10B (SeRe) | STEP3-VL-10B (PaCoRe) | GLM-4.6V (106B-A12B) | Qwen3-VL (235B-A22B) | Gemini-2.5-Pro | Seed-1.5-VL || :---------------- | :-----------------: | :-------------------: | :------------------: | :------------------: | :------------: | :---------: || **MMMU** | 78.11 | **80.11** | 75.20 | 78.70 | 83.89 | 79.11 || **MathVista** | 83.97 | **85.50** | 83.51 | 85.10 | 83.88 | 85.60 || **MathVision** | 70.81 | **75.95** | 63.50 | 72.10 | 73.30 | 68.70 || **MMBench (EN)** | 92.05 | 92.38 | 92.75 | 92.70 | **93.19** | 92.11 || **MMStar** | 77.48 | 77.64 | 75.30 | 76.80 | **79.18** | 77.91 || **OCRBench** | 86.75 | **89.00** | 86.20 | 87.30 | 85.90 | 85.20 || **AIME 2025** | 87.66 | **94.43** | 71.88 | 83.59 | 83.96 | 64.06 || **HMMT 2025** | 78.18 | **92.14** | 57.29 | 67.71 | 65.68 | 51.30 || **LiveCodeBench** | 75.77 | **76.43** | 48.71 | 69.45 | 72.01 | 57.10 |<!-- > **Note:** **SeRe** (Sequential Reasoning) uses a max length of 64K tokens; **PaCoRe** (Parallel Coordinated Reasoning) synthesizes 16 SeRe rollouts with a max length of 128K tokens. -->2 unmodified lines> **SeRe (Sequential Reasoning):** The standard inference mode using sequential generation (Chain-of-Thought) with a max length of 64K tokens.>> **PaCoRe (Parallel Coordinated Reasoning):** An advanced mode that scales test-time compute. It aggregates evidence from **16 parallel rollouts** to synthesize a final answer, utilizing a max context length of 128K tokens.>> _Unless otherwise stated, scores below refer to the standard SeRe mode. Higher scores achieved via PaCoRe are explicitly marked._### Comparison with Open-Source Models (7B–10B)| Category | Benchmark | STEP3-VL-10B | GLM-4.6V-Flash (9B) | Qwen3-VL-Thinking (8B) | InternVL-3.5 (8B) | MiMo-VL-RL-2508 (7B) || :----------------- | :--------------- | :----------: | :-----------------: | :--------------------: | :---------------: | :------------------: || **STEM Reasoning** | MMMU | **78.11** | 71.17 | 73.53 | 71.69 | 71.14 || | MathVision | **70.81** | 54.05 | 59.60 | 52.05 | 59.65 || | MathVista | **83.97** | 82.85 | 78.50 | 76.78 | 79.86 || | PhyX | **59.45** | 52.28 | 57.67 | 50.51 | 56.00 || **Recognition** | MMBench (EN) | **92.05** | 91.04 | 90.55 | 88.20 | 89.91 || | MMStar | **77.48** | 74.26 | 73.58 | 69.83 | 72.93 || | ReMI | **67.29** | 60.75 | 57.17 | 52.65 | 63.13 || **OCR & Document** | OCRBench | **86.75** | 85.97 | 82.85 | 83.70 | 85.40 || | AI2D | **89.35** | 88.93 | 83.32 | 82.34 | 84.96 || **GUI Grounding** | ScreenSpot-V2 | 92.61 | 92.14 | **93.60** | 84.02 | 90.82 || | ScreenSpot-Pro | **51.55** | 45.68 | 46.60 | 15.39 | 34.84 || | OSWorld-G | **59.02** | 54.71 | 56.70 | 31.91 | 50.54 || **Spatial** | BLINK | **66.79** | 64.90 | 62.78 | 55.40 | 62.57 || | All-Angles-Bench | **57.21** | 53.24 | 45.88 | 45.29 | 51.62 || **Code** | HumanEval-V | **66.05** | 29.26 | 26.94 | 24.31 | 31.96 |### Key Capabilities- **STEM Reasoning:** Achieves **94.43%** on AIME 2025 and **75.95%** on MathVision (with PaCoRe), demonstrating exceptional complex reasoning capabilities that outperform models 10×–20× larger.- **Visual Perception:** Records **92.05%** on MMBench and **80.11%** on MMMU, establishing strong general visual understanding and multimodal reasoning.- **GUI & OCR:** Delivers state-of-the-art performance on ScreenSpot-V2 (**92.61%**), ScreenSpot-Pro (**51.55%**), and OCRBench (**86.75%**), optimized for agentic and document understanding tasks.- **Spatial Understanding:** Demonstrates emergent spatial awareness with **66.79%** on BLINK and **57.21%** on All-Angles-Bench, establishing strong potential for embodied intelligence applications.## 🏗️ Architecture & Training17 unmodified lines- **RLHF:** 300 iterations (Task: open-ended generation).- **PaCoRe Training:** 500 iterations (Context length: 64K max sequence).## 🛠️ Quick Start### Inference with Hugging Face TransformersWe introduce how to use our model at inference stage using transformers library. It is recommended to use python=3.10, torch>=2.1.0, and transformers=4.57.0 as the development environment.We currently only support bf16 inference, and multi-patch for image preprocessing is supported by default. This behavior is aligned with vllm and sglang.```pythonfrom transformers import AutoProcessor, AutoModelForCausalLMkey_mapping = {"^vision_model": "model.vision_model",r"^model(?!\.(language_model|vision_model))": "model.language_model","vit_large_projector": "model.vit_large_projector",}model_path = "stepfun-ai/Step3-VL-10B"processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)messages = [{"role": "user","content": [{"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"},{"type": "text", "text": "What's in this picture?"}]},]model = AutoModelForCausalLM.from_pretrained(model_path,trust_remote_code=True,device_map="auto",torch_dtype="auto",key_mapping=key_mapping).eval()inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True,return_dict=True, return_tensors="pt").to(model.device)generate_ids = model.generate(**inputs, max_new_tokens=1024, do_sample=False)decoded = processor.decode(generate_ids[0, inputs["input_ids"].shape[-1] :], skip_special_tokens=True)print(decoded)```### RequirementsTo run STEP3-VL-10B efficiently, we recommend setting up a Python environment (>=3.10) with **vLLM**:35 unmodified linesprint(f"Output: {outputs[0].outputs[0].text}")```## 📜 CitationIf you find this project useful in your research, please cite our technical report:4 unmodified lines## 📄 LicenseThis project is open-sourced under the [Apache 2.0 License](https://www.google.com/search?q=LICENSE).
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677from typing import Any, Optional, Union
from transformers.configuration_utils import PretrainedConfigfrom transformers import Qwen3Config
class StepRoboticsVisionEncoderConfig(PretrainedConfig):
def __init__( self, width=1536, layers=47, heads=16, num_channels=3, image_size=728, mlp_ratio = 8960/1536, patch_size=14, hidden_act="quick_gelu", layer_norm_eps=1e-5, ues_cls_token=False, use_ln_pre=True, use_ln_post=False, use_abs_posemb=True, use_rope2d=True, ls_init_value=0.1, **kwargs, ): self.width = width self.layers = layers self.heads = heads self.num_channels = num_channels self.patch_size = patch_size self.image_size = image_size self.mlp_ratio = mlp_ratio self.layer_norm_eps = layer_norm_eps self.hidden_act = hidden_act self.ues_cls_token = ues_cls_token self.use_ln_pre = use_ln_pre self.ls_init_value = ls_init_value self.use_ln_post = use_ln_post self.use_abs_posemb = use_abs_posemb self.use_rope2d = use_rope2d super().__init__(**kwargs)
class StepRoboticsConfig(PretrainedConfig): model_type = "step_robotics" architectures = ["StepVLForConditionalGeneration"]
def __init__( self, vision_config: Optional[Union[dict, StepRoboticsVisionEncoderConfig]] = None, text_config: Optional[Union[dict, Qwen3Config]] = None, understand_projector_stride: int = 2, projector_bias: bool = False, image_token_id: int = 151679, **kwargs, ) -> None: if vision_config is None: vision_config = StepRoboticsVisionEncoderConfig() elif isinstance(vision_config, dict): vision_config = StepRoboticsVisionEncoderConfig(**vision_config) self.vision_config = vision_config
if text_config is None: text_config = Qwen3Config() elif isinstance(text_config, dict): text_config = Qwen3Config(**text_config) self.text_config = text_config
self.understand_projector_stride = understand_projector_stride self.projector_bias = projector_bias self.hidden_size = text_config.hidden_size self.image_token_id = image_token_id # Help Auto classes find the correct implementation when saving/loading. super().__init__(**kwargs)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568# Copyright 2025 The LLAMA4 and HuggingFace Inc. team. All rights reserved.### Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.from dataclasses import dataclassfrom typing import Callable, Optional, Tuple, Unionfrom PIL import Image
import torchimport torch.nn as nnimport torch.nn.functional as Ffrom transformers import Qwen3Modelfrom transformers.cache_utils import Cache, DynamicCachefrom transformers.generation import GenerationMixinfrom transformers.modeling_outputs import BaseModelOutputWithPast, ModelOutputfrom transformers.modeling_utils import PreTrainedModelfrom transformers.processing_utils import Unpackfrom transformers.utils import TransformersKwargs, can_return_tuple, logging
from typing import Any, Literal, Optional, TypedDict, Union
from configuration_step_vl import StepRoboticsConfigfrom vision_encoder import StepRoboticsVisionEncoderlogger = logging.get_logger(__name__)
class StepVLImagePixelInputs(TypedDict): type: Literal["pixel_values"] pixel_values: torch.Tensor patch_pixel_values: Optional[torch.Tensor] num_patches: list[int]
class StepVLImageEmbeddingInputs(TypedDict): type: Literal["image_embeds"] image_embeds: torch.Tensor
StepVLImageInputs = Union[StepVLImagePixelInputs, StepVLImageEmbeddingInputs]
@dataclassclass StepVLCausalLMOutputWithPast(ModelOutput): r""" loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided): Language modeling loss (for next-token prediction). logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`): Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). past_key_values (`Cache`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`) Contains pre-computed hidden-states (key and values in the self-attention blocks) that can be used (see `past_key_values` input) to speed up sequential decoding. """
loss: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None logits: torch.FloatTensor = None past_key_values: Optional[list[torch.FloatTensor]] = None hidden_states: Optional[tuple[torch.FloatTensor]] = None attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None
def _flatten_embeddings(embeddings) -> torch.Tensor: """ Recursively flattens and concatenates NestedTensors on all but the last dimension. """
if isinstance(embeddings, torch.Tensor): # Flatten all but the last dimension. return embeddings.flatten(0, -2)
return torch.cat(tuple(_flatten_embeddings(t) for t in embeddings))
def _embedding_count_expression(embeddings) -> str: """ Constructs a debugging representation of the number of embeddings in the NestedTensors. """
if isinstance(embeddings, torch.Tensor): return " x ".join([str(dim) for dim in embeddings.shape[:-1]])
return " + ".join( _embedding_count_expression(inner) for inner in embeddings)
def _merge_multimodal_embeddings( inputs_embeds: torch.Tensor, is_multimodal: torch.Tensor, multimodal_embeddings,) -> torch.Tensor: """ Merge ``multimodal_embeddings`` into ``inputs_embeds`` by overwriting the positions in ``inputs_embeds`` corresponding to placeholder tokens in ``input_ids``. Note: This updates ``inputs_embeds`` in place. """ num_expected_tokens = is_multimodal.sum().item() assert isinstance(num_expected_tokens, int)
flattened = _flatten_embeddings(multimodal_embeddings) if flattened.shape[0] != num_expected_tokens: expr = _embedding_count_expression(multimodal_embeddings) raise ValueError( f"Attempted to assign {expr} = {flattened.shape[0]} " f"multimodal tokens to {num_expected_tokens} placeholders")
is_multimodal = is_multimodal.to(inputs_embeds.device) flattened = flattened.to(inputs_embeds.device) inputs_embeds[is_multimodal] = flattened return inputs_embeds
def merge_multimodal_embeddings( input_ids: torch.Tensor, inputs_embeds: torch.Tensor, multimodal_embeddings, placeholder_token_id: Union[int, list[int]],) -> torch.Tensor: """ Merge ``multimodal_embeddings`` into ``inputs_embeds`` by overwriting the positions in ``inputs_embeds`` corresponding to placeholder tokens in ``input_ids``. ``placeholder_token_id`` can be a list of token ids (e.g, token ids of img_start, img_break, and img_end tokens) when needed: This means the order of these tokens in the ``input_ids`` MUST MATCH the order of their embeddings in ``multimodal_embeddings`` since we need to slice-merge instead of individually scattering. For example, if input_ids is "TTTTTSIIIBIIIBIIIETTT", where - T is text token - S is image start token - I is image embedding token - B is image break token - E is image end token. Then the image embeddings (that correspond to I's) from vision encoder must be padded with embeddings of S, B, and E in the same order of input_ids for a correct embedding merge. Note: This updates ``inputs_embeds`` in place. """ if isinstance(placeholder_token_id, list): placeholder_token_id = torch.tensor(placeholder_token_id, device=input_ids.device) return _merge_multimodal_embeddings( inputs_embeds, torch.isin(input_ids, placeholder_token_id), multimodal_embeddings, )
return _merge_multimodal_embeddings( inputs_embeds, (input_ids == placeholder_token_id), multimodal_embeddings, )
class StepRoboticsPreTrainedModel(PreTrainedModel): # Link this model family to its configuration class so PreTrainedModel.from_pretrained # can load the config instead of failing with a NoneType error. config_class = StepRoboticsConfig supports_gradient_checkpointing = True _skip_keys_device_placement = ["past_key_values"] _supports_flash_attn = False _supports_sdpa = True _supports_flex_attn = True _supports_static_cache = True _supports_attention_backend = True
class StepRoboticsModel(StepRoboticsPreTrainedModel, GenerationMixin): config: StepRoboticsConfig base_model_prefix = "" def __init__(self, config: StepRoboticsConfig): super().__init__(config) self.vision_model = StepRoboticsVisionEncoder(config.vision_config) self.language_model = Qwen3Model(config.text_config) self.vocab_size = config.text_config.vocab_size self.vit_large_projector = nn.Linear( config.vision_config.width * 4, config.text_config.hidden_size, bias=config.projector_bias) self.image_placeholder_token_id = config.image_token_id
# Initialize weights and apply final processing self.post_init()
def get_input_embeddings( self, input_ids: torch.Tensor, multimodal_embeddings = None, ) -> torch.Tensor: input_ids = input_ids.squeeze(0) if multimodal_embeddings is None: inputs_embeds = self.language_model.embed_tokens(input_ids) else: is_text = input_ids != self.config.image_token_id text_ids = input_ids[is_text] text_embeds = self.language_model.embed_tokens(text_ids) inputs_embeds = torch.empty(input_ids.shape[0], text_embeds.shape[-1], dtype=text_embeds.dtype, device=text_embeds.device) inputs_embeds[is_text] = text_embeds inputs_embeds = merge_multimodal_embeddings( input_ids, inputs_embeds, multimodal_embeddings, self.config.image_token_id) inputs_embeds = inputs_embeds.unsqueeze(0) return inputs_embeds
def set_input_embeddings(self, value): return self.language_model.set_input_embeddings(value)
def set_decoder(self, decoder): self.language_model = decoder
def get_decoder(self): return self.language_model def _parse_and_validate_image_input( self, **kwargs: object) -> Optional[StepVLImageInputs]: pixel_values = kwargs.pop("pixel_values", None) patch_pixel_values = kwargs.pop("patch_pixel_values", None) num_patches = kwargs.pop("num_patches", None) image_embeds = kwargs.pop("image_embeds", None)
if pixel_values is None and image_embeds is None: return None
if pixel_values is not None: # pixel_values = flatten_bn(pixel_values, concat=True) if pixel_values.dim() >= 3: pixel_values = pixel_values.view(-1, *pixel_values.shape[-3:]) if patch_pixel_values is not None: # patch_pixel_values = flatten_bn(patch_pixel_values, # concat=True) patch_pixel_values = patch_pixel_values.view( -1, *patch_pixel_values.shape[-3:]) # Handle empty patch_pixel_values by setting to None if patch_pixel_values.shape[0] == 0: patch_pixel_values = None
return StepVLImagePixelInputs( type="pixel_values", pixel_values=pixel_values.to(self.dtype).to(self.device), patch_pixel_values=patch_pixel_values.to(self.dtype).to( self.device) if patch_pixel_values is not None else None, num_patches=num_patches, )
if image_embeds is not None: if image_embeds.dim() == 2 or image_embeds.dim() >= 3: image_embeds = image_embeds.view(-1, image_embeds.shape[-1]) else: raise ValueError( f"Unexpected shape for image_embeds: {image_embeds.shape}")
return StepVLImageEmbeddingInputs( type="image_embeds", image_embeds=image_embeds.to(self.dtype).to(self.device), ) return None def _process_image_features(self, image_features: torch.Tensor) -> torch.Tensor: B, P = image_features.shape[:2] HW = int(P ** 0.5) image_features = image_features.permute(0, 2, 1).view(B, -1, HW, HW) image_features = self.vision_model.vit_downsampler1(image_features) image_features = self.vision_model.vit_downsampler2(image_features)
B, C, HW, HW = image_features.shape image_features = image_features.view(B, -1, HW * HW).permute(0, 2, 1) image_features = self.vit_large_projector(image_features) return image_features
def _get_vision_model_output(self, input_tensor: torch.Tensor) -> torch.Tensor: return self.vision_model(input_tensor) def _get_pooled_vision_model_output( self, input_tensor: torch.Tensor) -> torch.Tensor: return self.vision_model.pool(input_tensor)
def _process_image_input( self, image_input: StepVLImageInputs) -> tuple[torch.Tensor, ...]:
if image_input["type"] == "image_embeds": image_features = image_input["image_embeds"] else: image_features = self._get_vision_model_output( image_input["pixel_values"]) patch_image_features = self._get_vision_model_output( image_input["patch_pixel_values"] ) if image_input["patch_pixel_values"] is not None else None num_patches = image_input["num_patches"]
image_features = self._process_image_features(image_features) patch_image_features = self._process_image_features( patch_image_features) if patch_image_features is not None else None
merged_image_features = [] cur_patch_idx = 0 for i, num_patch in enumerate(num_patches): cur_feature = [] if num_patch > 0: patch_slice = patch_image_features[ cur_patch_idx:cur_patch_idx + num_patch] cur_feature.append(patch_slice.view(-1, patch_slice.shape[-1])) cur_feature.append(image_features[i].view( -1, image_features.shape[-1])) cur_patch_idx += num_patch merged_image_features.append( torch.cat(cur_feature) if len(cur_feature) > 1 else cur_feature[0]) return merged_image_features def get_multimodal_embeddings(self, **kwargs): image_input = self._parse_and_validate_image_input(**kwargs) if image_input is None: return None vision_embeddings = self._process_image_input(image_input) return vision_embeddings
@can_return_tuple def forward( self, input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, images: Optional[list[Image.Image]] = None, **kwargs: Unpack[TransformersKwargs], ) -> Union[tuple, StepVLCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`. Example: ```python >>> from transformers import AutoTokenizer, Llama4ForCausalLM >>> model = Llama4ForCausalLM.from_pretrained("meta-llama4/Llama4-2-7b-hf") >>> tokenizer = AutoTokenizer.from_pretrained("meta-llama4/Llama4-2-7b-hf") >>> prompt = "Hey, are you conscious? Can you talk to me?" >>> inputs = tokenizer(prompt, return_tensors="pt") >>> # Generate >>> generate_ids = model.generate(inputs.input_ids, max_length=30) >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you." ```""" output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions 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_dict if inputs_embeds is None: input_ids = input_ids vision_embeddings = self.get_multimodal_embeddings(**kwargs) inputs_embeds = self.get_input_embeddings(input_ids, vision_embeddings) input_ids = None # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn) outputs = self.language_model( input_ids=None, position_ids=position_ids, attention_mask=attention_mask, past_key_values=past_key_values, inputs_embeds=inputs_embeds, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=True, cache_position=cache_position, **kwargs, )
output = StepVLCausalLMOutputWithPast( last_hidden_state=outputs.last_hidden_state, past_key_values=outputs.past_key_values, attentions=outputs.attentions, ) return output if return_dict else output.to_tuple()
class Step3VL10BForCausalLM(StepRoboticsPreTrainedModel, GenerationMixin): _checkpoint_conversion_mapping = { "^vision_model": "model.vision_model", r"^model(?!\.(language_model|vision_model))": "model.language_model", "^vit_large_projector": "model.vit_large_projector" } _tied_weights_keys = ["lm_head.weight"] config: StepRoboticsConfig
def __init__(self, config: StepRoboticsConfig): super().__init__(config) self.model = StepRoboticsModel(config) self.lm_head = nn.Linear(config.hidden_size, config.text_config.vocab_size, bias=False)
self.post_init() def get_input_embeddings(self): return self.model.get_input_embeddings()
def set_input_embeddings(self, value): self.model.set_input_embeddings(value)
def get_output_embeddings(self): return self.model.get_output_embeddings()
def set_output_embeddings(self, new_embeddings): self.model.set_output_embeddings(new_embeddings)
def set_decoder(self, decoder): self.model.set_decoder(decoder)
def get_decoder(self): return self.model.get_decoder() @property def language_model(self): return self.model.language_model
@property def visual(self): return self.model.visual
def forward( self, input_ids: torch.LongTensor = None, num_patches = None, patch_pixel_values = None, patch_newline_mask = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[Cache] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[TransformersKwargs], ) -> Union[tuple, StepVLCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`. Example: ```python >>> from PIL import Image >>> import requests >>> from transformers import AutoProcessor, LlavaForConditionalGeneration >>> model = LlavaForConditionalGeneration.from_pretrained("llava-hf/llava-1.5-7b-hf") >>> processor = AutoProcessor.from_pretrained("llava-hf/llava-1.5-7b-hf") >>> prompt = "USER: <image>\nWhat's the content of the image? ASSISTANT:" >>> url = "https://www.ilankelman.org/stopsigns/australia.jpg" >>> image = Image.open(requests.get(url, stream=True).raw) >>> inputs = processor(images=image, text=prompt, return_tensors="pt") >>> # Generate >>> generate_ids = model.generate(**inputs, max_new_tokens=15) >>> processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] "USER: \nWhat's the content of the image? ASSISTANT: The image features a busy city street with a stop sign prominently displayed" ```"""
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states )
outputs = self.model( input_ids=input_ids, num_patches = num_patches, patch_pixel_values = patch_pixel_values, patch_newline_mask=patch_newline_mask, position_ids=position_ids, attention_mask=attention_mask, past_key_values=past_key_values, inputs_embeds=inputs_embeds, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, cache_position=cache_position, **kwargs, )
hidden_states = outputs.last_hidden_state logits = self.lm_head(hidden_states)
los = None if labels is not None: loss = self.loss_function(logits=logits, labels=labels, vocab_size=self.config.vocab_size)
return StepVLCausalLMOutputWithPast( logits=logits, )
def prepare_inputs_for_generation( self, input_ids, past_key_values=None, inputs_embeds=None, pixel_values=None, attention_mask=None, cache_position=None, logits_to_keep=None, **kwargs, ): # Overwritten -- in specific circumstances we don't want to forward image inputs to the model
model_inputs = super().prepare_inputs_for_generation( input_ids, past_key_values=past_key_values, inputs_embeds=inputs_embeds, attention_mask=attention_mask, cache_position=cache_position, logits_to_keep=logits_to_keep, **kwargs, )
if cache_position[0] == 0: # If we're in cached decoding stage, pixel values should be None because input ids do not contain special image token anymore # Otherwise we need pixel values to be passed to model model_inputs["pixel_values"] = pixel_values
return model_inputs def _fix_state_dict_key_on_load(self, key: str) -> tuple[str, bool]: if key.startswith("language_model."): return key[len("language_model."):], True return key, False
# Register config/model so Auto classes can instantiate this implementation.from transformers.models.auto.configuration_auto import AutoConfigfrom transformers.models.auto.modeling_auto import AutoModelForCausalLM
AutoConfig.register("step_robotics", StepRoboticsConfig)AutoModelForCausalLM.register(StepRoboticsConfig, Step3VL10BForCausalLM)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464from transformers import BaseImageProcessor, ImageProcessingMixinfrom transformers.processing_utils import ImagesKwargs, MultiModalData, ProcessingKwargs, ProcessorMixin, Unpack, VideosKwargsimport mathfrom typing import Iterable, Optional, Tuple, List, TypedDict, Literal, Union, overload
from PIL import Imageimport torchimport numpy as npimport torchvisionfrom torch import nnfrom torch.nn import functional as F, LayerNormfrom torchvision.transforms.functional import InterpolationModefrom transformers.activations import ACT2FNfrom torchvision import transformsfrom torchvision.transforms.functional import InterpolationModefrom transformers.feature_extraction_utils import BatchFeature, TensorTypefrom transformers.image_utils import ImageInputfrom transformers.processing_utils import ProcessingKwargs, ProcessorMixin, Unpackfrom math import ceilfrom itertools import product
MAX_IMAGE_SIZE: int = 3024
class Step3VLImagePixelInputs(TypedDict): type: Literal["pixel_values"] pixel_values: torch.Tensor patch_pixel_values: Optional[torch.Tensor] num_patches: list[int]
class Step3VLImageEmbeddingInputs(TypedDict): type: Literal["image_embeds"] image_embeds: torch.Tensor
ImageWithPatches = tuple[Image.Image, list[Image.Image], list[int] | None]
class GPUToTensor(torch.nn.Module):
def forward(self, raw_image: Union[np.ndarray, Image.Image]) -> torch.Tensor: if isinstance(raw_image, Image.Image): return transforms.ToTensor()(raw_image) if raw_image.ndim == 2: raw_image = raw_image[:, :, None].repeat(3, -1) if torch.cuda.is_available(): device = torch.device("cuda") else: device = torch.device("cpu") image_tensor = torch.from_numpy(raw_image).to(device) image_tensor = torch.permute(image_tensor, (2, 0, 1)).contiguous() if image_tensor.dtype == torch.uint8: image_tensor = image_tensor.to(torch.float32).div(255) return image_tensor
class Step3VisionProcessor(BaseImageProcessor):
def __init__(self, size, interpolation_mode="bicubic", patch_size=None): mean = [0.48145466, 0.4578275, 0.40821073] std = [0.26862954, 0.26130258, 0.27577711] patch_size = patch_size if patch_size is not None else size
self.transform = transforms.Compose([ GPUToTensor(), transforms.Normalize(mean, std), transforms.Resize( (size, size), interpolation=InterpolationMode.BICUBIC if interpolation_mode == "bicubic" else InterpolationMode.BILINEAR, antialias=True), ])
self.patch_transform = transforms.Compose([ GPUToTensor(), transforms.Normalize(mean, std), transforms.Resize( (patch_size, patch_size), interpolation=InterpolationMode.BICUBIC if interpolation_mode == "bicubic" else InterpolationMode.BILINEAR, antialias=True), ]) if patch_size is not None else None
def __call__(self, image, is_patch=False): if is_patch: return {"pixel_values": self.patch_transform(image).unsqueeze(0)} else: return {"pixel_values": self.transform(image).unsqueeze(0)}
class ImagePatcher: def determine_window_size(self, long: int, short: int) -> int: if long <= 728: return short if long / short > 1.5 else 0 return min(short, 504) if long / short > 4 else 504 def slide_window( self, width: int, height: int, sizes: list[tuple[int, int]], steps: list[tuple[int, int]], img_rate_thr: float = 0.6, ) -> tuple[list[tuple[int, int, int, int]], tuple[int, int]]: assert 1 >= img_rate_thr >= 0, "The `in_rate_thr` should lie in 0~1" windows = [] # Sliding windows. for size, step in zip(sizes, steps): size_w, size_h = size step_w, step_h = step
x_num = 1 if width <= size_w else ceil((width - size_w) / step_w + 1) x_start = [step_w * i for i in range(x_num)] if len(x_start) > 1 and x_start[-1] + size_w > width: x_start[-1] = width - size_w
y_num = 1 if height <= size_h else ceil((height - size_h) / step_h + 1) y_start = [step_h * i for i in range(y_num)] if len(y_start) > 1 and y_start[-1] + size_h > height: y_start[-1] = height - size_h
start = np.array(list(product(y_start, x_start)), dtype=int) start[:, [0, 1]] = start[:, [1, 0]] windows.append(np.concatenate([start, start + size], axis=1)) windows = np.concatenate(windows, axis=0)
return [(int(box[0]), int(box[1]), int(box[2] - box[0]), int(box[3] - box[1])) for box in windows], (x_num, y_num)
def square_pad(self, img: Image.Image) -> Image.Image: w, h = img.size if w == h: return img size = max(w, h) padded = Image.new(img.mode, (size, size), 0) padded.paste(img, (0, 0)) return padded
def get_image_size_for_padding(self, img_width: int, img_height: int) -> tuple[int, int]: ratio = img_width / img_height if min(img_height, img_width) < 32 and (ratio > 4 or ratio < 1 / 4): new_size = max(img_height, img_width) return new_size, new_size return img_width, img_height
def get_image_size_for_preprocess(self, img_width: int, img_height: int) -> tuple[int, int]:
if max(img_height, img_width) > MAX_IMAGE_SIZE: scale_factor = MAX_IMAGE_SIZE / max(img_height, img_width) img_width = int(img_width * scale_factor) img_height = int(img_height * scale_factor) return img_width, img_height
def get_image_size_for_crop(self, img_width: int, img_height: int, window_size: int): w_ratio = img_width / window_size h_ratio = img_height / window_size
if w_ratio < 1: width_new = img_width else: decimal_w = w_ratio - img_width // window_size w_ratio = int(w_ratio) + 1 if decimal_w > 0.2 else int(w_ratio) width_new = window_size * w_ratio if h_ratio < 1: height_new = img_height else: decimal_h = h_ratio - img_height // window_size h_ratio = int(h_ratio) + 1 if decimal_h > 0.2 else int(h_ratio) height_new = window_size * h_ratio return int(width_new), int(height_new)
def patch_crop(self, img: Image.Image, i: int, j: int, th: int, tw: int): target = img.crop((j, i, j + tw, i + th)) return target
def get_num_patches(self, img_width: int, img_height: int) -> tuple[int, int]: img_width, img_height = self.get_image_size_for_padding( img_width, img_height) img_width, img_height = self.get_image_size_for_preprocess( img_width, img_height) window_size = self.determine_window_size(max(img_height, img_width), min(img_height, img_width)) if window_size == 0: return 0, 0 else: img_width, img_height = self.get_image_size_for_crop( img_width, img_height, window_size) center_list, (x_num, y_num) = self.slide_window( img_width, img_height, [(window_size, window_size)], [(window_size, window_size)]) full_rows = (len(center_list) - 1) // x_num + 1 if len(center_list) > 0 and len(center_list) % x_num == 0: full_rows -= 1 return len(center_list), full_rows
def __call__( self, img: Image.Image ) -> tuple[Image.Image, list[Image.Image], list[bool] | None]: img_width, img_height = img.size new_img_width, new_img_height = self.get_image_size_for_padding( img_width, img_height) if new_img_width != img_width or new_img_height != img_height: img = self.square_pad(img) img_width, img_height = img.size
new_img_width, new_img_height = self.get_image_size_for_preprocess( img_width, img_height) img = img.resize((new_img_width, new_img_height), Image.Resampling.BILINEAR) window_size = self.determine_window_size( max(new_img_height, new_img_width), min(new_img_height, new_img_width))
if window_size == 0: return img, [], None else: new_img_width, new_img_height = self.get_image_size_for_crop( new_img_width, new_img_height, window_size) if (new_img_width, new_img_height) != (img_width, img_height): img_for_crop = img.resize((new_img_width, new_img_height), Image.Resampling.BILINEAR) else: img_for_crop = img
patches = [] newlines = [] center_list, (x_num, y_num) = self.slide_window( new_img_width, new_img_height, [(window_size, window_size)], [(window_size, window_size)]) for patch_id, center_lf_point in enumerate(center_list): x, y, patch_w, patch_h = center_lf_point big_patch = self.patch_crop(img_for_crop, y, x, patch_h, patch_w) patches.append(big_patch) if (patch_id + 1) % x_num == 0: newlines.append(patch_id)
if newlines and newlines[-1] == len(patches) - 1: newlines.pop()
return img, patches, [i in newlines for i in range(len(patches))] if len(patches) > 0 else None
class Step3VLProcessor(ProcessorMixin): # Align ProcessorMixin with our custom components. # We only have an image processor (not a feature extractor) plus a tokenizer. attributes = ["tokenizer"] tokenizer_class = "AutoTokenizer"
def __init__( self, tokenizer=None, chat_template=None, **kwargs ) -> None: self.image_size = 728 self.patch_size = 504
self.image_preprocessor = Step3VisionProcessor(self.image_size, "bilinear", self.patch_size)
self.num_image_feature_size = 169 self.num_patch_feature_size = 81 self.image_token = "<im_patch>" self.image_feature_placeholder = (self.image_token * self.num_image_feature_size) self.patch_feature_placeholder = (self.image_token * self.num_patch_feature_size) super().__init__(tokenizer=tokenizer, chat_template=chat_template, **kwargs) self.patcher = ImagePatcher() @property def image_token_id(self) -> int: return self.tokenizer.get_vocab()[self.image_token]
def get_num_image_tokens(self, img_width: int, img_height: int) -> int: num_patches, num_newlines = self.patcher.get_num_patches( img_width, img_height)
return num_patches * ( self.num_patch_feature_size + 2) + self.num_image_feature_size + 2 + num_newlines
def _split_images(self, images: list[Image.Image]) -> list[ImageWithPatches]: result = [] for img in images: result.append(self.patcher(img)) return result
def _convert_images_to_pixel_values( self, images: list[Image.Image], is_patch: bool = False, ) -> list[torch.Tensor]: return [ self.image_preprocessor(img, is_patch=is_patch)["pixel_values"] for img in images ]
def _get_patch_repl( self, num_patches: int, patch_newline_mask: list[bool] | None, ) -> tuple[str, list[int]]: text = "" token_ids = [] for i in range(num_patches): assert len(patch_newline_mask) == num_patches text += f"<patch_start>{self.patch_feature_placeholder}<patch_end>" token_ids.extend( [self.tokenizer.convert_tokens_to_ids("<patch_start>")] + [self.image_token_id] * self.num_patch_feature_size + [self.tokenizer.convert_tokens_to_ids("<patch_end>")]) if patch_newline_mask and patch_newline_mask[i]: text += "<patch_newline>" token_ids.append( self.tokenizer.convert_tokens_to_ids("<patch_newline>")) return text, token_ids
def _get_image_repl( self, num_images: int, ) -> tuple[str, list[int]]: text = f"<im_start>{self.image_feature_placeholder}<im_end>" token_ids = [ self.tokenizer.convert_tokens_to_ids("<im_start>") ] + [self.image_token_id] * self.num_image_feature_size + [ self.tokenizer.convert_tokens_to_ids("<im_end>") ] return text * num_images, token_ids * num_images
def _get_image_repl_features( self, num_images: int, num_patches: int, patch_new_line_idx: Optional[list[bool]], ) -> tuple[str, list[int]]: if num_patches > 0: patch_repl, patch_repl_ids = self._get_patch_repl( num_patches, patch_new_line_idx) else: patch_repl = "" patch_repl_ids = [] image_repl, image_repl_ids = self._get_image_repl(num_images) return patch_repl + image_repl, patch_repl_ids + image_repl_ids
def replace_placeholder(self, text: str, placeholder: str, repls: list[str]) -> str: parts = text.split(placeholder)
if len(parts) - 1 != len(repls): raise ValueError( "The number of placeholders does not match the number of replacements." # noqa: E501 )
result = [parts[0]] for i, repl in enumerate(repls): result.append(repl) result.append(parts[i + 1])
return "".join(result)
def __call__( self, text: Optional[Union[str, list[str]]] = None, images: ImageInput | None = None, return_tensors: Optional[Union[str, TensorType]] = None, **kwargs, ) -> BatchFeature:
if images is not None: images = self.image_preprocessor.fetch_images(images) if text is None: text = [] if not isinstance(text, list): text = [text] if images is None: images = [] elif not isinstance(images, list): images = [images] elif isinstance(images[0], list): images = images[0]
if len(images) == 0: image_inputs = {} text_inputs = self.tokenizer(text) else: splitted_images_data = self._split_images(images) pixel_values_lst = [] patch_pixel_values_lst = [] patch_newline_mask_lst = [] image_repl_str_lst = [] image_repl_ids_lst = [] num_patches = [] for raw_img, img_patches, patch_newline_mask in splitted_images_data: # noqa: E501 pixel_values_lst.extend( self._convert_images_to_pixel_values([raw_img]))
if len(img_patches) > 0: patch_pixel_values_lst.extend( self._convert_images_to_pixel_values(img_patches, is_patch=True)) num_patches.append(len(img_patches))
image_repl_str, image_repl_ids = self._get_image_repl_features( 1, len(img_patches), patch_newline_mask) image_repl_str_lst.append(image_repl_str) image_repl_ids_lst.extend(image_repl_ids)
if patch_newline_mask is not None: patch_newline_mask_lst.extend(patch_newline_mask)
image_inputs = { "pixel_values": torch.cat(pixel_values_lst), "num_patches": num_patches, } if patch_pixel_values_lst: image_inputs["patch_pixel_values"] = torch.cat( patch_pixel_values_lst) if patch_newline_mask_lst: image_inputs["patch_newline_mask"] = torch.tensor( patch_newline_mask_lst, dtype=torch.bool)
text = [ self.replace_placeholder(t, self.image_token, image_repl_str_lst) for t in text ] text_inputs = self.tokenizer(text)
return BatchFeature( { **text_inputs, **image_inputs, }, tensor_type=return_tensors, ) # Copied from transformers.models.clip.processing_clip.CLIPProcessor.batch_decode with CLIP->Gemma def batch_decode(self, *args, **kwargs): """ This method forwards all its arguments to GemmaTokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please refer to the docstring of this method for more information. """ return self.tokenizer.batch_decode(*args, **kwargs)
# Copied from transformers.models.clip.processing_clip.CLIPProcessor.decode with CLIP->Gemma def decode(self, *args, **kwargs): """ This method forwards all its arguments to GemmaTokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to the docstring of this method for more information. """ return self.tokenizer.decode(*args, **kwargs) __all__ = ["Step3VLProcessor"]123456{ "auto_map": { "AutoProcessor": "processing_step3.Step3VLProcessor" }}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468from typing import Literal, Optional, Tuple, Union
import torchimport torch.nn as nnimport torch.nn.functional as Ffrom einops import rearrange, repeatfrom transformers.activations import ACT2FN
from configuration_step_vl import StepRoboticsVisionEncoderConfig
def rotate_half(x: torch.Tensor) -> torch.Tensor: """Rotate last dimension halves (used by RoPE).""" x = rearrange(x, "... (d r) -> ... d r", r=2) x1, x2 = x.unbind(dim=-1) x = torch.stack((-x2, x1), dim=-1) return rearrange(x, "... d r -> ... (d r)")
def apply_rotary_emb(freqs: torch.Tensor, t: torch.Tensor, start_index: int = 0, scale: float = 1.0, seq_dim: int = -2) -> torch.Tensor: """Apply 2D rotary embeddings to queries / keys.""" dtype = t.dtype
if t.ndim == 3: seq_len = t.shape[seq_dim] freqs = freqs[-seq_len:]
rot_dim = freqs.shape[-1] end_index = start_index + rot_dim assert rot_dim <= t.shape[-1], ( f"feature dimension {t.shape[-1]} is too small for rot_dim {rot_dim}")
t_left, t, t_right = ( t[..., :start_index], t[..., start_index:end_index], t[..., end_index:], ) t = (t * freqs.cos() * scale) + (rotate_half(t) * freqs.sin() * scale) out = torch.cat((t_left, t, t_right), dim=-1) return out.type(dtype)
class EncoderRope2D(nn.Module): """Cacheable 2D rotary positional embedding."""
def __init__( self, dim: int, max_grid_height: int, max_grid_width: int, use_cls_token: bool = False, freqs_for: Literal["lang", "pixel", "constant"] = "lang", theta: Union[int, float] = 10000, max_freq: int = 10, num_freqs: int = 1, theta_rescale_factor: float = 1.0, ): super().__init__() self.dim = dim self.max_grid_height = max_grid_height self.max_grid_width = max_grid_width self.use_cls_token = use_cls_token self.theta = theta * theta_rescale_factor**(dim / (dim - 2)) self.freqs_for = freqs_for self.max_freq = max_freq self.num_freqs = num_freqs cache = self._compute_2d_freqs() self.register_buffer("freqs_cache", cache, persistent=False)
def _compute_inv_freq(self, base: Union[int, float], dim: int) -> torch.Tensor: if self.freqs_for == "lang": freqs = 1.0 / (base**( torch.arange(0, dim, 2)[:(dim // 2)].float() / dim)) elif self.freqs_for == "pixel": freqs = torch.linspace(1.0, self.max_freq / 2, dim // 2) * torch.pi elif self.freqs_for == "constant": freqs = torch.ones(self.num_freqs).float() else: raise ValueError(f"Unsupported freqs_for value: {self.freqs_for}") return freqs
def _compute_freqs(self, t: torch.Tensor, inv_freq: torch.Tensor): freqs = torch.einsum("..., f -> ... f", t.type(inv_freq.dtype), inv_freq) freqs = repeat(freqs, "... n -> ... (n r)", r=2) return freqs
def _compute_2d_freqs(self) -> torch.Tensor: grid_h_range = torch.arange(self.max_grid_height, dtype=torch.float) grid_w_range = torch.arange(self.max_grid_width, dtype=torch.float) if self.use_cls_token: grid_h_range += 1 grid_w_range += 1 inv_freq = self._compute_inv_freq(self.theta, self.dim // 2) freqs_h = self._compute_freqs(grid_h_range, inv_freq)[:, None].expand( self.max_grid_height, self.max_grid_width, -1) freqs_w = self._compute_freqs(grid_w_range, inv_freq)[None, :].expand( self.max_grid_height, self.max_grid_width, -1) freqs = torch.cat([freqs_w, freqs_h], dim=-1).reshape( self.max_grid_height * self.max_grid_width, -1) if self.use_cls_token: freqs = torch.cat([torch.zeros(1, freqs.shape[-1]), freqs], dim=0) freqs = freqs[None, None, ...] return freqs
def forward(self, q: torch.Tensor, k: torch.Tensor, grid_hw: tuple[int, int]): # If grid matches cached shape we reuse directly to avoid recomputation. if grid_hw[0] != self.max_grid_height or grid_hw[1] != self.max_grid_width: rows = torch.arange(grid_hw[0], device=q.device).view(-1, 1) cols = torch.arange(grid_hw[1], device=q.device).view(1, -1) positions = (rows * self.max_grid_width + cols).reshape(-1).to( torch.long) if self.use_cls_token: positions = torch.cat( [torch.zeros(1, device=q.device), positions + 1], dim=0) freqs = self.freqs_cache.index_select(2, positions) else: freqs = self.freqs_cache q = apply_rotary_emb(freqs, q) k = apply_rotary_emb(freqs, k) return q, k
class EncoderLayerScale(nn.Module): """Per-channel residual scaling used when ls_init_value is set."""
def __init__(self, dim: int, init_values: float): super().__init__() self.gamma = nn.Parameter(torch.full((dim,), init_values))
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: # (B, L, D) return hidden_states * self.gamma
class EncoderMLP(nn.Module): """Feed-forward network used inside each transformer block."""
def __init__(self, hidden_size: int, intermediate_size: int, hidden_act: str): super().__init__() self.c_fc = nn.Linear(hidden_size, intermediate_size, bias=True) self.act_fn = ACT2FN[hidden_act] self.c_proj = nn.Linear(intermediate_size, hidden_size, bias=True)
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
hidden_states = self.c_proj(self.act_fn(self.c_fc(hidden_states))) return hidden_states
class EncoderVisionAttention(nn.Module): """Multi-head self attention with optional 2D RoPE."""
def __init__( self, hidden_size: int, num_heads: int, max_grid_height: int, max_grid_width: int, use_cls_token: bool = False, use_rope2d: bool = True, rope_theta: Union[int, float] = 10000, rope_max_freq: int = 10, rope_num_freqs: int = 1, rope_theta_rescale_factor: float = 1.0, rope_freqs_for: Literal["lang", "pixel", "constant"] = "lang", ): super().__init__() if hidden_size % num_heads != 0: raise ValueError( f"hidden_size ({hidden_size}) must be divisible by num_heads ({num_heads})." ) self.num_heads = num_heads self.head_dim = hidden_size // num_heads self.scale = self.head_dim**-0.5 self.in_proj_weight = nn.Parameter(torch.zeros(hidden_size * 3, hidden_size)) self.in_proj_bias = nn.Parameter(torch.zeros(hidden_size * 3)) self.out_proj = nn.Linear(hidden_size, hidden_size, bias=True)
self.rope = None if use_rope2d: self.rope = EncoderRope2D( dim=self.head_dim, max_grid_height=max_grid_height, max_grid_width=max_grid_width, use_cls_token=use_cls_token, theta=rope_theta, max_freq=rope_max_freq, num_freqs=rope_num_freqs, theta_rescale_factor=rope_theta_rescale_factor, freqs_for=rope_freqs_for, )
def forward(self, hidden_states: torch.Tensor, grid_hw: tuple[int, int]) -> torch.Tensor: bsz, seq_len, _ = hidden_states.shape qkv = F.linear( hidden_states, self.in_proj_weight, self.in_proj_bias, ) q, k, v = qkv.chunk(3, dim=-1)
q = q.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2) k = k.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2) if self.rope is not None: q, k = self.rope(q, k, grid_hw=grid_hw) v = v.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
attn_output = F.scaled_dot_product_attention( q, k, v, is_causal=False, scale=self.scale) attn_output = attn_output.transpose(1, 2).reshape( bsz, seq_len, self.num_heads * self.head_dim) return self.out_proj(attn_output)
class EncoderVisionBlock(nn.Module): """A single Vision Transformer block (self-attention + MLP)."""
def __init__( self, hidden_size: int, num_heads: int, mlp_ratio: float, hidden_act: str, layer_norm_eps: float, ls_init_value: Optional[float] = None, max_grid_height: Optional[int] = None, max_grid_width: Optional[int] = None, use_cls_token: bool = False, use_rope2d: bool = True, rope_kwargs: Optional[dict] = None, ): super().__init__() rope_kwargs = rope_kwargs or {} self.attn = EncoderVisionAttention( hidden_size, num_heads, max_grid_height=max_grid_height, max_grid_width=max_grid_width, use_cls_token=use_cls_token, use_rope2d=use_rope2d, **rope_kwargs, ) self.ln_1 = nn.LayerNorm(hidden_size, eps=layer_norm_eps) self.ln_2 = nn.LayerNorm(hidden_size, eps=layer_norm_eps)
intermediate = int(hidden_size * mlp_ratio) self.mlp = EncoderMLP(hidden_size, intermediate, hidden_act)
self.ls_1 = EncoderLayerScale(hidden_size, ls_init_value) self.ls_2 = EncoderLayerScale(hidden_size, ls_init_value)
def forward(self, hidden_states: torch.Tensor, grid_hw: tuple[int, int]) -> torch.Tensor: # breakpoint() residual = hidden_states hidden_states = self.ln_1(hidden_states) hidden_states = self.attn(hidden_states, grid_hw=grid_hw) hidden_states = residual + self.ls_1(hidden_states)
residual = hidden_states hidden_states = self.ln_2(hidden_states) hidden_states = self.mlp(hidden_states) hidden_states = residual + self.ls_2(hidden_states) return hidden_states
class EncoderVisionTransformer(nn.Module): """Stack of encoder blocks parameterised by Step35VisionEncoderConfig."""
def __init__( self, embed_dim: int, depth: int, num_heads: int, mlp_ratio: float, hidden_act: str, layer_norm_eps: float, ls_init_value: Optional[float] = None, max_grid_height: Optional[int] = None, max_grid_width: Optional[int] = None, use_cls_token: bool = False, use_rope2d: bool = True, rope_kwargs: Optional[dict] = None, ): super().__init__() self.layers = depth rope_kwargs = rope_kwargs or {} self.resblocks = nn.ModuleList([ EncoderVisionBlock(embed_dim, num_heads, mlp_ratio, hidden_act, layer_norm_eps, max_grid_height=max_grid_height, max_grid_width=max_grid_width, use_cls_token=use_cls_token, use_rope2d=use_rope2d, ls_init_value=ls_init_value, rope_kwargs=rope_kwargs) for _ in range(depth) ])
def forward(self, hidden_states: torch.Tensor, grid_hw: tuple[int, int], layer_idx: int = -1) -> torch.Tensor: stop_idx = (self.layers + layer_idx) % self.layers for idx, block in enumerate(self.resblocks): hidden_states = block(hidden_states, grid_hw=grid_hw) if idx == stop_idx: break return hidden_states
class StepRoboticsVisionEncoder(nn.Module): """ Vision encoder built from StepRoboticsVisionEncoderConfig.
The encoder performs patch embedding followed by a stack of transformer blocks. Only the config fields defined in StepRoboticsVisionEncoderConfig (and StepRoboticVLConfig.vision_config) are expected. """
def __init__(self, config: StepRoboticsVisionEncoderConfig): super().__init__() self.config = config
# Align commonly used attributes so downstream code (e.g. StepRoboticVL) # can access them without extra renaming. self.hidden_size = config.width self.num_heads = config.heads self.num_hidden_layers = config.layers self.patch_size = config.patch_size self.image_size = config.image_size self.use_cls_token = getattr(config, "use_cls_token", False) self.use_rope2d = getattr(config, "use_rope2d", True) self.use_abs_posemb = getattr(config, "use_abs_posemb", True) self.layer_norm_eps = config.layer_norm_eps self.mlp_ratio = getattr(config, "mlp_ratio", 8960 / 1536) self.ls_init_value = getattr(config, "ls_init_value", None) self.hidden_act = config.hidden_act self.use_ln_pre = getattr(config, "use_ln_pre", False) self.use_ln_post = getattr(config, "use_ln_post", True)
# Patch embedding. self.conv1 = nn.Conv2d(in_channels=config.num_channels, out_channels=self.hidden_size, kernel_size=self.patch_size, stride=self.patch_size, bias=False)
self.ln_pre = nn.LayerNorm(self.hidden_size, eps=self.layer_norm_eps) if self.use_ln_pre else nn.Identity() self.ln_post = nn.LayerNorm(self.hidden_size, eps=self.layer_norm_eps) if self.use_ln_post else nn.Identity()
grid_size = self.image_size // self.patch_size self.base_grid = (grid_size, grid_size)
if self.use_cls_token: self.class_embedding = nn.Parameter( torch.randn(self.hidden_size) * (self.hidden_size**-0.5)) else: self.class_embedding = None if self.use_abs_posemb: self.posemb_grid_size = self.image_size // self.patch_size self.positional_embedding = nn.Parameter( (self.hidden_size**-0.5) * torch.randn( int(self.use_cls_token) + self.posemb_grid_size**2, self.hidden_size, ))
self.transformer = EncoderVisionTransformer( embed_dim=self.hidden_size, depth=self.num_hidden_layers, num_heads=self.num_heads, mlp_ratio=self.mlp_ratio, hidden_act=self.hidden_act, layer_norm_eps=self.layer_norm_eps, ls_init_value=self.ls_init_value, max_grid_height=self.base_grid[0], max_grid_width=self.base_grid[1], use_cls_token=self.use_cls_token, use_rope2d=self.use_rope2d, rope_kwargs={ "rope_theta": getattr(config, "rope_theta", 10000), "rope_max_freq": getattr(config, "rope_max_freq", 10), "rope_num_freqs": getattr(config, "rope_num_freqs", 1), "rope_theta_rescale_factor": getattr(config, "rope_theta_rescale_factor", 1.0), "rope_freqs_for": getattr(config, "rope_freqs_for", "lang"), }, ) self.vit_downsampler1 = nn.Conv2d(self.hidden_size, self.hidden_size * 2, kernel_size=3, stride=2, padding=1) self.vit_downsampler2 = nn.Conv2d(self.hidden_size * 2, self.hidden_size * 4, kernel_size=3, stride=2, padding=1)
def sample_abs_posemb(self, grid_h: int, grid_w: int): if self.posemb_grid_size == grid_h and self.posemb_grid_size == grid_w: return self.positional_embedding[None, ...]
pos_embed = self.positional_embedding if self.use_cls_token: cls_token_embed, pos_embed = pos_embed[:1], pos_embed[1:]
pos_embed = (pos_embed.reshape(1, self.posemb_grid_size, self.posemb_grid_size, -1).permute(0, 3, 1, 2).contiguous()) pos_embed = F.interpolate(pos_embed, size=(grid_h, grid_w), mode="bilinear", align_corners=False) pos_embed = pos_embed.permute(0, 2, 3, 1).reshape(-1, self.hidden_size)
if self.use_cls_token: pos_embed = torch.cat([cls_token_embed, pos_embed], dim=0)
return pos_embed[None, ...]
def forward(self, pixel_values: torch.Tensor, layer_idx: int = -1, strip_cls_token: bool = False) -> torch.Tensor: """ Args: pixel_values: Image tensor of shape (B, C, H, W). layer_idx: Negative indices stop after a given block (e.g., -1 uses all blocks). strip_cls_token: If True and cls token is used, remove it from output. """ bsz, _, height, width = pixel_values.shape grid_h, grid_w = height // self.patch_size, width // self.patch_size
hidden_state = self.conv1(pixel_values) # (B, D, Gh, Gw) hidden_state = hidden_state.flatten(2).transpose(1, 2) # (B, Gh*Gw, D)
if self.use_cls_token: cls_token = self.class_embedding.view(1, 1, -1).expand(bsz, -1, -1) hidden_state = torch.cat([cls_token, hidden_state], dim=1)
if self.use_abs_posemb: pos_emb = self.sample_abs_posemb(grid_h, grid_w) hidden_state = hidden_state + pos_emb hidden_state = self.ln_pre(hidden_state) hidden_state = self.transformer(hidden_state, grid_hw=(grid_h, grid_w), layer_idx=layer_idx)
if self.use_ln_post: hidden_state = self.ln_post(hidden_state)
if strip_cls_token and self.use_cls_token: hidden_state = hidden_state[:, 1:, :]
return hidden_stateREADME.md+88-42
configuration_step_vl.pyAdded+77
modeling_step_vl.pyAdded+568
processing_step3.pyAdded+464
processor_config.jsonAdded+6
vision_encoder.pyAdded+468