Asuka0630 commented on code in PR #18609:
URL: https://github.com/apache/tvm/pull/18609#discussion_r2647028593


##########
python/tvm/topi/nn/batch_norm.py:
##########
@@ -111,26 +111,22 @@ 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
+        )
+        data_var_rs = topi.reshape(data_var, shape)
+        out = (data - data_mean_rs) / topi.math.sqrt(data_var_rs + epsilon)
+    else:
         moving_mean_rs = topi.reshape(moving_mean, shape)
         moving_var_rs = topi.reshape(moving_var, shape)
-
         out = (data - moving_mean_rs) / topi.math.sqrt(moving_var_rs + epsilon)

Review Comment:
   I don't think there will be any errors at runtime here, If I'm not 
misunderstanding Python. Then I need to add `data_mean = None` and `data_var = 
None` outside to avoid this warning. Is my understanding correct? :)



-- 
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