From 7baeb7d8a2310a5e4636e32b41d177f00d4b4116 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Tue, 22 Jul 2025 06:12:42 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Speed=20up=20function=20`s?= =?UTF-8?q?igmoid=5Fstable`=20by=20236%=20Here=20is=20a=20faster=20and=20m?= =?UTF-8?q?ore=20memory-efficient=20version=20of=20the=20function.=20The?= =?UTF-8?q?=20bottleneck=20in=20the=20original=20implementation=20is=20cal?= =?UTF-8?q?ling=20`np.exp(x)`=20twice=20for=20`x=20<=200`=20branches.=20We?= =?UTF-8?q?=20can=20compute=20`exp=5Fx=20=3D=20np.exp(x)`=20once,=20then?= =?UTF-8?q?=20reuse=20it=20for=20both=20cases.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This version calls `np.exp` only once and chooses the correct formulation based on `x`, which results in a significant speedup and less redundant computation. --- codeflash/process/infer.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/codeflash/process/infer.py b/codeflash/process/infer.py index bbbaefb88..46423f685 100644 --- a/codeflash/process/infer.py +++ b/codeflash/process/infer.py @@ -2,7 +2,8 @@ def sigmoid_stable(x): - return np.where(x >= 0, 1 / (1 + np.exp(-x)), np.exp(x) / (1 + np.exp(x))) + exp_x = np.exp(-np.abs(x)) + return np.where(x >= 0, 1 / (1 + exp_x), exp_x / (1 + exp_x)) def postprocess(logits: np.array, max_detections: int = 8):