Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def get_input_embeddings(
image_mask = ids_remove_padding == self.model.config.image_token_id
image_token_num = image_mask.sum()

if image_token_num > 0:
if image_token_num > 0 and image_features is not None:

This comment was marked as outdated.

input_embeddings[image_mask] = image_features.cast(self._dtype)
return input_embeddings

Expand Down
72 changes: 72 additions & 0 deletions tests/model_executor/test_paddleocr_vl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
# Copyright (c) 2026 PaddlePaddle Authors. 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.
"""

import unittest
from unittest.mock import MagicMock

import paddle

from fastdeploy.model_executor.models.paddleocr_vl.paddleocr_vl import (
PaddleOCRVLForConditionalGeneration,
)


IMAGE_TOKEN_ID = 101304


class TestPaddleOCRVLForConditionalGeneration(unittest.TestCase):
"""Test PaddleOCRVLForConditionalGeneration class."""

def _make_model(self):
model = PaddleOCRVLForConditionalGeneration.__new__(PaddleOCRVLForConditionalGeneration)
model.model = MagicMock()
model.model.config.image_token_id = IMAGE_TOKEN_ID
model.model.get_input_embeddings = MagicMock(
return_value=paddle.zeros([5, 768], dtype=paddle.float32)
)
model._dtype = paddle.float32
return model

def test_get_input_embeddings_no_crash_when_image_features_none_without_image_tokens(self):
model = self._make_model()
ids = paddle.to_tensor([[1, 2, 3, 4, 5]])
result = model.get_input_embeddings(
ids_remove_padding=ids, image_features=None
)
self.assertIsNotNone(result)

def test_get_input_embeddings_no_crash_when_image_features_none_with_image_tokens(self):
model = self._make_model()
ids = paddle.to_tensor([[1, IMAGE_TOKEN_ID, 3, 4, 5]])
result = model.get_input_embeddings(
ids_remove_padding=ids, image_features=None
)
self.assertIsNotNone(result)

def test_get_input_embeddings_with_image_features_replaces_image_tokens(self):
model = self._make_model()
ids = paddle.to_tensor([[1, IMAGE_TOKEN_ID, 3, 4, 5]])
image_features = paddle.ones([1, 768], dtype=paddle.float32)
result = model.get_input_embeddings(
ids_remove_padding=ids, image_features=image_features
)
self.assertIsNotNone(result)
self.assertEqual(result.shape, [5, 768])
self.assertTrue(paddle.allclose(result[1], paddle.ones([768])))


if __name__ == "__main__":

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 建议 新增测试文件里保留了 if __name__ == "__main__" 入口。

FastDeploy checklist §C 明确把 test/** 中的 if __name__ == "__main__" 作为环境依赖 hack 表层信号。测试应交给 pytest/unittest 发现机制运行,避免直接执行入口造成 CI/本地执行方式分叉。

建议修复方式:删除文件末尾的入口块,仅保留 TestPaddleOCRVLForConditionalGeneration 用例类。

unittest.main()
Loading