gemini-code-assist[bot] commented on code in PR #18609:
URL: https://github.com/apache/tvm/pull/18609#discussion_r2647127309


##########
python/tvm/topi/nn/batch_norm.py:
##########
@@ -111,36 +111,29 @@ def batch_norm(
     shape = [1] * len(data.shape)
     shape[axis] = data.shape[axis]
 
-    reduce_axes = list(range(len(data.shape)))
-    reduce_axes.remove(axis)
-    shape_prod = reduce(lambda x, y: x * y, [data.shape[ax] for ax in 
reduce_axes], 1)
-
-    data_mean = topi.sum(data, axis=reduce_axes) / shape_prod
-    data_mean_rs = topi.reshape(data_mean, shape)
-    data_var = (
-        topi.sum((data - data_mean_rs) * (data - data_mean_rs), 
axis=reduce_axes) / shape_prod
-    )
-    data_var_rs = topi.reshape(data_var, shape)
-
     if training:
+        reduce_axes = list(range(len(data.shape)))
+        reduce_axes.remove(axis)
+        shape_prod = reduce(lambda x, y: x * y, [data.shape[ax] for ax in 
reduce_axes], 1)
+        data_mean = topi.sum(data, axis=reduce_axes) / shape_prod
+        data_mean_rs = topi.reshape(data_mean, shape)
+        data_var = (
+            topi.sum((data - data_mean_rs) * (data - data_mean_rs), 
axis=reduce_axes) / shape_prod
+        )

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The expression `(data - data_mean_rs)` is computed twice here. While the 
compiler might optimize this, it's better to explicitly compute the difference 
once and reuse it. This improves readability and ensures efficiency.
   
   You could refactor this part like so:
   ```python
   diff = data - data_mean_rs
   data_var = topi.sum(diff * diff, axis=reduce_axes) / shape_prod
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to