diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000000..3ab07c55e82 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2025-05-18 - FastDeploy get_tensor PySafeSlice parsing +**Learning:** Checking for `PySafeSlice` by calling `str(type(input))` and looking for a substring (`"PySafeSlice" in str(type(input))`) creates unneeded string formatting overhead in the extremely hot path of `get_tensor` logic since it's frequently called across weight loading and other utils. +**Action:** Replace `str(type(input))` with `type(input).__name__ == "PySafeSlice"` to speed it up. diff --git a/fastdeploy/model_executor/layers/utils.py b/fastdeploy/model_executor/layers/utils.py index f3444173e18..3059cef3da3 100644 --- a/fastdeploy/model_executor/layers/utils.py +++ b/fastdeploy/model_executor/layers/utils.py @@ -332,7 +332,9 @@ def get_tensor(input: Union[paddle.Tensor, np.ndarray, str], model_path=None) -> paddle.Tensor: Returns a PaddlePaddle tensor. """ - if "PySafeSlice" in str(type(input)): + # Optimize type checking: type(input).__name__ avoids the overhead of converting + # the type object to a string for substring matching. + if type(input).__name__ == "PySafeSlice": input = input.get() if isinstance(input, paddle.Tensor):