optimize serving_score loops.#24000
Conversation
There was a problem hiding this comment.
Code Review
The pull request optimizes two loops by using list slicing, which is a good improvement for conciseness and performance. However, this change introduced a critical issue by removing a None check on the embeddings list. This could lead to runtime errors. I've added a review comment with a suggested fix to re-introduce the check in a Pythonic way.
There was a problem hiding this comment.
This optimization is a good idea, but it removes the crucial assert ... is not None check. The embeddings list is of type list[Optional[PoolingRequestOutput]] and can contain None if an embedding generation fails. Removing the check can lead to a runtime AttributeError in _cosine_similarity when accessing emb.outputs.data.
I suggest re-introducing the assertion in a more Pythonic way that keeps the slicing optimization.
Note that static type checkers might still complain about the types of emb_texts_1 and emb_texts_2 after this change. You may need to add a # type: ignore or use typing.cast if you are using a strict type checking setup.
| emb_texts_1 = embeddings[:len(texts_1)] | |
| emb_texts_2 = embeddings[len(texts_1):] | |
| emb_texts_1, emb_texts_2 = embeddings[:len(texts_1)], embeddings[len(texts_1):] | |
| assert all(e is not None for e in emb_texts_1) and all(e is not None for e in emb_texts_2), "Embedding generation failed, resulting in None values." |
fcb3aef to
74c9b14
Compare
Signed-off-by: zhanluxianshen <zhanluxianshen@163.com>
74c9b14 to
d883a5c
Compare
|
This pull request has been automatically marked as stale because it has not had any activity within 90 days. It will be automatically closed if no further activity occurs within 30 days. Leave a comment if you feel this pull request should remain open. Thank you! |
Purpose
Test Plan
Test Result
Essential Elements of an Effective PR Description Checklist
supported_models.mdandexamplesfor a new model.