From 32de3912884dc2dc1425b61c419bbd30f2adbbbd Mon Sep 17 00:00:00 2001
From: Dominic Symes <dominic.symes@arm.com>
Date: Fri, 3 Sep 2021 11:45:59 +0100
Subject: MAX_POOL2D: Fix padding behaviour

Change-Id: Idcf4b99161ce05e56391fbc9107a19617cb1ad21
Signed-off-by: Dominic Symes <dominic.symes@arm.com>
---
 chapters/tensor_ops.adoc | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

(limited to 'chapters/tensor_ops.adoc')

diff --git a/chapters/tensor_ops.adoc b/chapters/tensor_ops.adoc
index 7334f67..6bf9bf2 100644
--- a/chapters/tensor_ops.adoc
+++ b/chapters/tensor_ops.adoc
@@ -447,7 +447,6 @@ None
 
 [source,c++]
 ----
-pad = flatten([0,0], pad, [0,0]);
 for_each(0 <= n < N, 0 <= oy < H, 0 <= ox < W, 0 <= c < C ) {
     in_t acc = minimum_value<in_t>;
     iy = oy * stride_y - pad_top;
@@ -455,8 +454,11 @@ for_each(0 <= n < N, 0 <= oy < H, 0 <= ox < W, 0 <= c < C ) {
     for_each( 0 <= ky < kernel_y, 0 <= kx < kernel_x ) {
         y = iy + ky;
         x = ix + kx;
-        in_t value = tensor_read<in_t>(input, [N,IH,IW,C], [n,y,x,c], pad);
-        acc = apply_max(acc, value);
+        ERROR_IF(y >= IH + pad_bottom || x >= IW + pad_right);
+        if (y >= 0 && y < IH && x >= 0 && x < IW) {
+            in_t value = tensor_read<in_t>(input, [N,IH,IW,C], [n,y,x,c]);
+            acc = apply_max(acc, value);
+        }
     }
     tensor_write<in_t>(output, [N,H,W,C], [n,oy,ox,c], acc);
 }
-- 
cgit v1.2.1

