From 411bae3a35b3b16d102a5bdaa2768c9cd2859095 Mon Sep 17 00:00:00 2001 From: Linchenn Date: Wed, 30 Nov 2022 23:06:30 -0800 Subject: [PATCH 01/14] fix webgl --- tfjs-backend-webgl/src/pool_gpu.ts | 7 +++-- tfjs-core/src/ops/avg_pool_3d_test.ts | 30 +++++++++++++++++++ tfjs-core/src/ops/conv_util.ts | 43 ++++++++++++++++++--------- 3 files changed, 64 insertions(+), 16 deletions(-) diff --git a/tfjs-backend-webgl/src/pool_gpu.ts b/tfjs-backend-webgl/src/pool_gpu.ts index 4ae4ae45d8..8f084ad7fb 100644 --- a/tfjs-backend-webgl/src/pool_gpu.ts +++ b/tfjs-backend-webgl/src/pool_gpu.ts @@ -342,7 +342,10 @@ export class Pool3DProgram implements GPGPUProgram { let returnValue = `${poolType}(${poolType}(${poolType}(` + 'minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])'; if (poolType === 'avg') { - returnValue = `avgValue / count`; + // Use `max(count, 1.0)` instead of `count` in case count === 0.0. + // If count === 0.0, `avgValue` is always 0.0 and we change `count`'s + // value to avoid dividing zero. + returnValue = `avgValue / max(count, 1.0)`; } const filterWidthNearestVec4 = Math.floor(filterWidth / 4) * 4; @@ -448,8 +451,8 @@ export class Pool3DProgram implements GPGPUProgram { ${updateSnippet} } } - setOutput(${returnValue}); } + setOutput(${returnValue}); } `; } diff --git a/tfjs-core/src/ops/avg_pool_3d_test.ts b/tfjs-core/src/ops/avg_pool_3d_test.ts index 308eca2504..74d6d87fed 100644 --- a/tfjs-core/src/ops/avg_pool_3d_test.ts +++ b/tfjs-core/src/ops/avg_pool_3d_test.ts @@ -29,6 +29,15 @@ describeWithFlags('avgPool3d', ALL_ENVS, () => { expectArraysClose(await result.data(), [4.5]); }); + it('x=[2,2,2,1] f=[1,2,2] s=1 p=valid', async () => { + const x = tf.tensor4d([1, 2, 3, 4, 5, 6, 7, 8], [2, 2, 2, 1]); + + const result = tf.avgPool3d(x, [1, 2, 2], 1, 'valid'); + + expect(result.shape).toEqual([2, 1, 1, 1]); + expectArraysClose(await result.data(), [2.5, 6.5]); + }); + it('x=[1,1,1,1,1] f=[1,1,1] s=1 [0] => [0]', async () => { const x = tf.tensor5d([0], [1, 1, 1, 1, 1]); @@ -150,6 +159,27 @@ describeWithFlags('avgPool3d', ALL_ENVS, () => { expectArraysClose(await result.data(), expected); }); + it('x=[1,1,1,1,1] f=[1,1,3] s=1 p=valid', async () => { + // Output tensor would have a dimension of zero, if a certain filter's + // dimension is larger than the input's. + const x = tf.tensor5d([1], [1, 1, 1, 1, 1]); + const expected: number[] = []; + const result = tf.avgPool3d(x, [1, 1, 3], 1, 'valid'); + + expect(result.shape).toEqual([1, 1, 1, 0, 1]); + expectArraysClose(await result.data(), expected); + }); + + it('x=[1,1,1,4,1] f=[1,1,1] s=[1,1,2] p=0', async () => { + // Works if the padding is a number. + const x = tf.ones([1, 1, 1, 4, 1]) as tf.Tensor5D; + const expected = [1, 1]; + const result = tf.avgPool3d(x, [1, 1, 1], [1, 1, 2], 0); + + expect(result.shape).toEqual([1, 1, 1, 2, 1]); + expectArraysClose(await result.data(), expected); + }); + it('throws when x is not rank 5', async () => { // tslint:disable-next-line:no-any const x: any = tf.tensor1d([1]); diff --git a/tfjs-core/src/ops/conv_util.ts b/tfjs-core/src/ops/conv_util.ts index 3be66a02ca..ce316f17f9 100644 --- a/tfjs-core/src/ops/conv_util.ts +++ b/tfjs-core/src/ops/conv_util.ts @@ -365,22 +365,32 @@ function computeOutputShape2D( } function computeOutputShape4D( - inShape: [number, number, number, number], fieldSize: number, - outChannels: number, stride: number, zeroPad?: number, + inShape: [number, number, number, number], + fieldSize: [number, number, number], outChannels: number, + stride: [number, number, number], zeroPad?: number, roundingMode?: 'floor'|'round'|'ceil'): [number, number, number, number] { if (zeroPad == null) { - zeroPad = computeDefaultPad(inShape, fieldSize, stride); + zeroPad = computeDefaultPad(inShape, fieldSize[0], stride[0]); } const inputDepth = inShape[0]; const inputRows = inShape[1]; const inputCols = inShape[2]; - const outputDepths = - round((inputDepth - fieldSize + 2 * zeroPad) / stride + 1, roundingMode); - const outputRows = - round((inputRows - fieldSize + 2 * zeroPad) / stride + 1, roundingMode); - const outputCols = - round((inputCols - fieldSize + 2 * zeroPad) / stride + 1, roundingMode); + const outputDepths = Math.max( + 0, + round( + (inputDepth - fieldSize[0] + 2 * zeroPad + 1) / stride[0], + roundingMode)); + const outputRows = Math.max( + 0, + round( + (inputRows - fieldSize[1] + 2 * zeroPad + 1) / stride[1], + roundingMode)); + const outputCols = Math.max( + 0, + round( + (inputCols - fieldSize[2] + 2 * zeroPad + 1) / stride[2], + roundingMode)); return [outputDepths, outputRows, outputCols, outChannels]; } @@ -508,8 +518,9 @@ function get3DPadAndOutInfo( type: padType }; const outShape = computeOutputShape4D( - [inDepth, inHeight, inWidth, 1], filterDepth, 1, strideDepth, pad, - roundingMode); + [inDepth, inHeight, inWidth, 1], + [filterDepth, filterHeight, filterWidth], 1, + [strideDepth, strideHeight, strideWidth], pad, roundingMode); outDepth = outShape[0]; outHeight = outShape[1]; outWidth = outShape[2]; @@ -539,9 +550,13 @@ function get3DPadAndOutInfo( back: 0, type: 'VALID' }; - outDepth = Math.ceil((inDepth - filterDepth + 1) / strideDepth); - outHeight = Math.ceil((inHeight - filterHeight + 1) / strideHeight); - outWidth = Math.ceil((inWidth - filterWidth + 1) / strideWidth); + const outShape = computeOutputShape4D( + [inDepth, inHeight, inWidth, 1], + [filterDepth, filterHeight, filterWidth], 1, + [strideDepth, strideHeight, strideWidth], 0, 'ceil'); + outDepth = outShape[0]; + outHeight = outShape[1]; + outWidth = outShape[2]; } else { throw Error(`Unknown padding parameter: ${pad}`); } From 385c1a663d46083ca14e02c17327e9f7f27274ab Mon Sep 17 00:00:00 2001 From: Linchenn Date: Wed, 30 Nov 2022 23:24:20 -0800 Subject: [PATCH 02/14] rename --- tfjs-core/src/ops/conv_util.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tfjs-core/src/ops/conv_util.ts b/tfjs-core/src/ops/conv_util.ts index ce316f17f9..784ef9625d 100644 --- a/tfjs-core/src/ops/conv_util.ts +++ b/tfjs-core/src/ops/conv_util.ts @@ -366,11 +366,11 @@ function computeOutputShape2D( function computeOutputShape4D( inShape: [number, number, number, number], - fieldSize: [number, number, number], outChannels: number, - stride: [number, number, number], zeroPad?: number, + filterShape: [number, number, number], outChannels: number, + strides: [number, number, number], zeroPad?: number, roundingMode?: 'floor'|'round'|'ceil'): [number, number, number, number] { if (zeroPad == null) { - zeroPad = computeDefaultPad(inShape, fieldSize[0], stride[0]); + zeroPad = computeDefaultPad(inShape, filterShape[0], strides[0]); } const inputDepth = inShape[0]; const inputRows = inShape[1]; @@ -379,17 +379,17 @@ function computeOutputShape4D( const outputDepths = Math.max( 0, round( - (inputDepth - fieldSize[0] + 2 * zeroPad + 1) / stride[0], + (inputDepth - filterShape[0] + 2 * zeroPad + 1) / strides[0], roundingMode)); const outputRows = Math.max( 0, round( - (inputRows - fieldSize[1] + 2 * zeroPad + 1) / stride[1], + (inputRows - filterShape[1] + 2 * zeroPad + 1) / strides[1], roundingMode)); const outputCols = Math.max( 0, round( - (inputCols - fieldSize[2] + 2 * zeroPad + 1) / stride[2], + (inputCols - filterShape[2] + 2 * zeroPad + 1) / strides[2], roundingMode)); return [outputDepths, outputRows, outputCols, outChannels]; From 010dfbc57b5c77498659eb8ef990c7ec39ab096c Mon Sep 17 00:00:00 2001 From: Linchenn Date: Thu, 1 Dec 2022 11:12:47 -0800 Subject: [PATCH 03/14] add strides check --- tfjs-backend-cpu/src/utils/pool_utils.ts | 5 +++-- tfjs-core/src/ops/avg_pool_3d.ts | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/tfjs-backend-cpu/src/utils/pool_utils.ts b/tfjs-backend-cpu/src/utils/pool_utils.ts index f3d497b966..4730026e20 100644 --- a/tfjs-backend-cpu/src/utils/pool_utils.ts +++ b/tfjs-backend-cpu/src/utils/pool_utils.ts @@ -245,8 +245,9 @@ export function pool3d( } } const outputOffset = outputColOffset + channel; - outputVals[outputOffset] = - poolType === 'avg' ? avgValue / count : minMaxValue; + outputVals[outputOffset] = poolType === 'avg' ? + avgValue / Math.max(count, 1) : + minMaxValue; } } } diff --git a/tfjs-core/src/ops/avg_pool_3d.ts b/tfjs-core/src/ops/avg_pool_3d.ts index 7c35a09a64..0606ee5590 100644 --- a/tfjs-core/src/ops/avg_pool_3d.ts +++ b/tfjs-core/src/ops/avg_pool_3d.ts @@ -24,8 +24,8 @@ import {convertToTensor} from '../tensor_util_env'; import {TensorLike} from '../types'; import * as util from '../util'; -import {checkPadOnDimRoundingMode} from './conv_util'; import {cast} from './cast'; +import {checkPadOnDimRoundingMode} from './conv_util'; import {op} from './operation'; import {reshape} from './reshape'; @@ -86,6 +86,11 @@ function avgPool3d_( dataFormat === 'NDHWC', () => `Error in avgPool3d: Only NDHWC is currently supported, ` + `but got dataFormat of ${dataFormat}`); + util.assert( + (typeof strides === 'number' && strides > 0) || + (typeof strides === 'object' && strides[0] > 0 && strides[1] > 0 && + strides[2] > 0), + () => `Error in avgPool3d: Stride must be > 0, but got '${strides}'`); checkPadOnDimRoundingMode('avgPool3d', pad, dimRoundingMode); const inputs: AvgPool3DInputs = {x: x5D}; const attrs: From dbc15e666707a50f155932e18713e50275381e2f Mon Sep 17 00:00:00 2001 From: Linchenn Date: Thu, 1 Dec 2022 11:58:36 -0800 Subject: [PATCH 04/14] Update conv_util.ts --- tfjs-core/src/ops/conv_util.ts | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/tfjs-core/src/ops/conv_util.ts b/tfjs-core/src/ops/conv_util.ts index 784ef9625d..91973d64e3 100644 --- a/tfjs-core/src/ops/conv_util.ts +++ b/tfjs-core/src/ops/conv_util.ts @@ -372,27 +372,16 @@ function computeOutputShape4D( if (zeroPad == null) { zeroPad = computeDefaultPad(inShape, filterShape[0], strides[0]); } - const inputDepth = inShape[0]; - const inputRows = inShape[1]; - const inputCols = inShape[2]; - - const outputDepths = Math.max( - 0, - round( - (inputDepth - filterShape[0] + 2 * zeroPad + 1) / strides[0], - roundingMode)); - const outputRows = Math.max( - 0, - round( - (inputRows - filterShape[1] + 2 * zeroPad + 1) / strides[1], - roundingMode)); - const outputCols = Math.max( - 0, - round( - (inputCols - filterShape[2] + 2 * zeroPad + 1) / strides[2], - roundingMode)); - - return [outputDepths, outputRows, outputCols, outChannels]; + const outShape: [number, number, number, number] = [0, 0, 0, outChannels]; + for (let index = 0; index < 3; index++) { + if (inShape[index] + 2 * zeroPad >= filterShape[index]) { + outShape[index] = round( + (inShape[index] - filterShape[index] + 2 * zeroPad) / strides[index] + + 1, + roundingMode) + } + } + return outShape; } export function computeDefaultPad( From 1822a8382270c447af29af425acbf0b8cc4773ff Mon Sep 17 00:00:00 2001 From: Linchenn Date: Thu, 1 Dec 2022 12:06:10 -0800 Subject: [PATCH 05/14] reduce valid --- tfjs-core/src/ops/conv_util.ts | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/tfjs-core/src/ops/conv_util.ts b/tfjs-core/src/ops/conv_util.ts index 91973d64e3..63c6a4b587 100644 --- a/tfjs-core/src/ops/conv_util.ts +++ b/tfjs-core/src/ops/conv_util.ts @@ -495,6 +495,10 @@ function get3DPadAndOutInfo( let outHeight: number; let outWidth: number; + if (pad === 'valid') { + pad = 0; + } + if (typeof pad === 'number') { const padType = (pad === 0) ? 'VALID' : 'NUMBER'; padInfo = { @@ -529,23 +533,6 @@ function get3DPadAndOutInfo( const right = padAlongWidth - left; padInfo = {top, bottom, left, right, front, back, type: 'SAME'}; - } else if (pad === 'valid') { - padInfo = { - top: 0, - bottom: 0, - left: 0, - right: 0, - front: 0, - back: 0, - type: 'VALID' - }; - const outShape = computeOutputShape4D( - [inDepth, inHeight, inWidth, 1], - [filterDepth, filterHeight, filterWidth], 1, - [strideDepth, strideHeight, strideWidth], 0, 'ceil'); - outDepth = outShape[0]; - outHeight = outShape[1]; - outWidth = outShape[2]; } else { throw Error(`Unknown padding parameter: ${pad}`); } From e4afabff6eff6450b9ccae17dbf494b9a8aff4ab Mon Sep 17 00:00:00 2001 From: Linchenn Date: Thu, 1 Dec 2022 12:21:12 -0800 Subject: [PATCH 06/14] lint --- tfjs-core/src/ops/conv_util.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tfjs-core/src/ops/conv_util.ts b/tfjs-core/src/ops/conv_util.ts index 63c6a4b587..93ef7efccf 100644 --- a/tfjs-core/src/ops/conv_util.ts +++ b/tfjs-core/src/ops/conv_util.ts @@ -378,7 +378,7 @@ function computeOutputShape4D( outShape[index] = round( (inShape[index] - filterShape[index] + 2 * zeroPad) / strides[index] + 1, - roundingMode) + roundingMode); } } return outShape; From 02b63da62aabd11b8ba954b21b28bb3da0b5f5eb Mon Sep 17 00:00:00 2001 From: Linchenn Date: Thu, 1 Dec 2022 13:38:42 -0800 Subject: [PATCH 07/14] reduce --- tfjs-core/src/ops/avg_pool_3d_test.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/tfjs-core/src/ops/avg_pool_3d_test.ts b/tfjs-core/src/ops/avg_pool_3d_test.ts index 74d6d87fed..ec83d67596 100644 --- a/tfjs-core/src/ops/avg_pool_3d_test.ts +++ b/tfjs-core/src/ops/avg_pool_3d_test.ts @@ -159,17 +159,6 @@ describeWithFlags('avgPool3d', ALL_ENVS, () => { expectArraysClose(await result.data(), expected); }); - it('x=[1,1,1,1,1] f=[1,1,3] s=1 p=valid', async () => { - // Output tensor would have a dimension of zero, if a certain filter's - // dimension is larger than the input's. - const x = tf.tensor5d([1], [1, 1, 1, 1, 1]); - const expected: number[] = []; - const result = tf.avgPool3d(x, [1, 1, 3], 1, 'valid'); - - expect(result.shape).toEqual([1, 1, 1, 0, 1]); - expectArraysClose(await result.data(), expected); - }); - it('x=[1,1,1,4,1] f=[1,1,1] s=[1,1,2] p=0', async () => { // Works if the padding is a number. const x = tf.ones([1, 1, 1, 4, 1]) as tf.Tensor5D; From f70417c85f63ea2ccc6788e00fde314a8cf5d1a3 Mon Sep 17 00:00:00 2001 From: Linchenn Date: Thu, 1 Dec 2022 14:44:48 -0800 Subject: [PATCH 08/14] add tests --- tfjs-core/src/ops/avg_pool_3d_test.ts | 11 +++++++++++ tfjs-node/src/run_tests.ts | 2 ++ 2 files changed, 13 insertions(+) diff --git a/tfjs-core/src/ops/avg_pool_3d_test.ts b/tfjs-core/src/ops/avg_pool_3d_test.ts index ec83d67596..74d6d87fed 100644 --- a/tfjs-core/src/ops/avg_pool_3d_test.ts +++ b/tfjs-core/src/ops/avg_pool_3d_test.ts @@ -159,6 +159,17 @@ describeWithFlags('avgPool3d', ALL_ENVS, () => { expectArraysClose(await result.data(), expected); }); + it('x=[1,1,1,1,1] f=[1,1,3] s=1 p=valid', async () => { + // Output tensor would have a dimension of zero, if a certain filter's + // dimension is larger than the input's. + const x = tf.tensor5d([1], [1, 1, 1, 1, 1]); + const expected: number[] = []; + const result = tf.avgPool3d(x, [1, 1, 3], 1, 'valid'); + + expect(result.shape).toEqual([1, 1, 1, 0, 1]); + expectArraysClose(await result.data(), expected); + }); + it('x=[1,1,1,4,1] f=[1,1,1] s=[1,1,2] p=0', async () => { // Works if the padding is a number. const x = tf.ones([1, 1, 1, 4, 1]) as tf.Tensor5D; diff --git a/tfjs-node/src/run_tests.ts b/tfjs-node/src/run_tests.ts index b4ca9560d6..b7d7e61fd8 100644 --- a/tfjs-node/src/run_tests.ts +++ b/tfjs-node/src/run_tests.ts @@ -84,6 +84,8 @@ const IGNORE_LIST: string[] = [ 'avgPool test-tensorflow {} gradient x=[3,3,1] f=[3,3] s=1 p=explicit', // tslint:disable-next-line:max-line-length 'avgPool3d test-tensorflow {} x=[1,2,2,2,1] f=[2,2,2] s=1 p=1 roundingMode=floor', + // https://github.com/tensorflow/tensorflow/issues/58758 + 'avgPool3d test-tensorflow {} x=[1,1,1,1,1] f=[1,1,3] s=1 p=valid', // Node backend which uses TF 2.4.0 doesn't support explicit padding 'maxPool test-tensorflow {} x=[3,3,1] f=[3,3] s=1 p=explicit', 'maxPoolBackprop test-tensorflow {} gradient x=[3,3,1] f=3 s=1 p=explicit', From f9036e3fde429900a796f87dccb163a35f36ba71 Mon Sep 17 00:00:00 2001 From: Linchenn Date: Thu, 1 Dec 2022 18:06:54 -0800 Subject: [PATCH 09/14] isArray --- tfjs-core/src/ops/avg_pool_3d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tfjs-core/src/ops/avg_pool_3d.ts b/tfjs-core/src/ops/avg_pool_3d.ts index 0606ee5590..3c79d75d6d 100644 --- a/tfjs-core/src/ops/avg_pool_3d.ts +++ b/tfjs-core/src/ops/avg_pool_3d.ts @@ -88,7 +88,7 @@ function avgPool3d_( `but got dataFormat of ${dataFormat}`); util.assert( (typeof strides === 'number' && strides > 0) || - (typeof strides === 'object' && strides[0] > 0 && strides[1] > 0 && + (Array.isArray(strides) && strides[0] > 0 && strides[1] > 0 && strides[2] > 0), () => `Error in avgPool3d: Stride must be > 0, but got '${strides}'`); checkPadOnDimRoundingMode('avgPool3d', pad, dimRoundingMode); From 5cba16bf44627c488b79ab6c278bb99630eff46c Mon Sep 17 00:00:00 2001 From: Linchenn Date: Fri, 2 Dec 2022 14:35:08 -0800 Subject: [PATCH 10/14] Update pool_gpu.ts --- tfjs-backend-webgl/src/pool_gpu.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tfjs-backend-webgl/src/pool_gpu.ts b/tfjs-backend-webgl/src/pool_gpu.ts index 8f084ad7fb..f3ea8e4d76 100644 --- a/tfjs-backend-webgl/src/pool_gpu.ts +++ b/tfjs-backend-webgl/src/pool_gpu.ts @@ -121,7 +121,7 @@ export class Pool2DProgram implements GPGPUProgram { let returnValue = `${poolType}(${poolType}(${poolType}(` + 'minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])'; if (poolType === 'avg') { - returnValue = `avgValue / count`; + returnValue = `avgValue / max(count, 1.0)`; } const filterWidthNearestVec4 = Math.floor(filterWidth / 4) * 4; From 0bec23ef0f5ffcb2c745be6fd837e7ed36925e4f Mon Sep 17 00:00:00 2001 From: Linchenn Date: Mon, 5 Dec 2022 11:50:29 -0800 Subject: [PATCH 11/14] Update pool2d_webgpu.ts --- tfjs-backend-webgpu/src/pool2d_webgpu.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tfjs-backend-webgpu/src/pool2d_webgpu.ts b/tfjs-backend-webgpu/src/pool2d_webgpu.ts index b87cf319c3..ff31a01daa 100644 --- a/tfjs-backend-webgpu/src/pool2d_webgpu.ts +++ b/tfjs-backend-webgpu/src/pool2d_webgpu.ts @@ -53,7 +53,7 @@ export class Pool2DProgram implements WebGPUProgram { let returnValue = `resultValue`; if (this.poolType === 'avg') { - returnValue = `resultValue / count`; + returnValue = `resultValue / max(count, 1.0)`; } const userCode = ` From 58108f1cf8183f951f5a0107e12daaef36c55226 Mon Sep 17 00:00:00 2001 From: Linchenn Date: Mon, 5 Dec 2022 12:01:35 -0800 Subject: [PATCH 12/14] Update avg_pool_3d_test.ts --- tfjs-core/src/ops/avg_pool_3d_test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tfjs-core/src/ops/avg_pool_3d_test.ts b/tfjs-core/src/ops/avg_pool_3d_test.ts index 74d6d87fed..6e8f13f893 100644 --- a/tfjs-core/src/ops/avg_pool_3d_test.ts +++ b/tfjs-core/src/ops/avg_pool_3d_test.ts @@ -180,6 +180,16 @@ describeWithFlags('avgPool3d', ALL_ENVS, () => { expectArraysClose(await result.data(), expected); }); + fit('x=[1,1,1,2,1] f=[1,1,2] s=[1,1,2] p=3', async () => { + // Works if the padding is larger than filter size. + const x = tf.ones([1, 1, 1, 2, 1]) as tf.Tensor5D; + const expected = [1, 1]; + const result = tf.avgPool3d(x, [1, 1, 2], [1, 1, 1], 3); + + expect(result.shape).toEqual([1, 1, 1, 2, 1]); + expectArraysClose(await result.data(), expected); + }); + it('throws when x is not rank 5', async () => { // tslint:disable-next-line:no-any const x: any = tf.tensor1d([1]); From 1b478d6c08030f3917fa2040cf7e980fdc2a8c69 Mon Sep 17 00:00:00 2001 From: Linchenn Date: Mon, 5 Dec 2022 12:25:15 -0800 Subject: [PATCH 13/14] Update avg_pool_3d_test.ts --- tfjs-core/src/ops/avg_pool_3d_test.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tfjs-core/src/ops/avg_pool_3d_test.ts b/tfjs-core/src/ops/avg_pool_3d_test.ts index 6e8f13f893..5f2527b76b 100644 --- a/tfjs-core/src/ops/avg_pool_3d_test.ts +++ b/tfjs-core/src/ops/avg_pool_3d_test.ts @@ -180,13 +180,17 @@ describeWithFlags('avgPool3d', ALL_ENVS, () => { expectArraysClose(await result.data(), expected); }); - fit('x=[1,1,1,2,1] f=[1,1,2] s=[1,1,2] p=3', async () => { + it('x=[1,1,1,1,1] f=[2,2,2] s=[1,1,1] p=2', async () => { // Works if the padding is larger than filter size. - const x = tf.ones([1, 1, 1, 2, 1]) as tf.Tensor5D; - const expected = [1, 1]; - const result = tf.avgPool3d(x, [1, 1, 2], [1, 1, 1], 3); + const x = tf.ones([1, 1, 1, 1, 1]) as tf.Tensor5D; + const expected = [ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + ]; + const result = tf.avgPool3d(x, [2, 2, 2], [1, 1, 1], 2); - expect(result.shape).toEqual([1, 1, 1, 2, 1]); + expect(result.shape).toEqual([1, 4, 4, 4, 1]); expectArraysClose(await result.data(), expected); }); From 60c9379d54ae16e7921b7f6313fe2c92280bdcc2 Mon Sep 17 00:00:00 2001 From: Linchenn Date: Mon, 5 Dec 2022 14:07:23 -0800 Subject: [PATCH 14/14] skip tests for node --- tfjs-core/src/ops/avg_pool_3d_test.ts | 4 ++-- tfjs-node/src/run_tests.ts | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tfjs-core/src/ops/avg_pool_3d_test.ts b/tfjs-core/src/ops/avg_pool_3d_test.ts index 5f2527b76b..7c2c12cbe8 100644 --- a/tfjs-core/src/ops/avg_pool_3d_test.ts +++ b/tfjs-core/src/ops/avg_pool_3d_test.ts @@ -180,7 +180,7 @@ describeWithFlags('avgPool3d', ALL_ENVS, () => { expectArraysClose(await result.data(), expected); }); - it('x=[1,1,1,1,1] f=[2,2,2] s=[1,1,1] p=2', async () => { + it('x=[1,1,1,1,1] f=[2,2,2] s=1 p=2', async () => { // Works if the padding is larger than filter size. const x = tf.ones([1, 1, 1, 1, 1]) as tf.Tensor5D; const expected = [ @@ -188,7 +188,7 @@ describeWithFlags('avgPool3d', ALL_ENVS, () => { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]; - const result = tf.avgPool3d(x, [2, 2, 2], [1, 1, 1], 2); + const result = tf.avgPool3d(x, [2, 2, 2], 1, 2); expect(result.shape).toEqual([1, 4, 4, 4, 1]); expectArraysClose(await result.data(), expected); diff --git a/tfjs-node/src/run_tests.ts b/tfjs-node/src/run_tests.ts index b7d7e61fd8..42c32b358d 100644 --- a/tfjs-node/src/run_tests.ts +++ b/tfjs-node/src/run_tests.ts @@ -86,6 +86,8 @@ const IGNORE_LIST: string[] = [ 'avgPool3d test-tensorflow {} x=[1,2,2,2,1] f=[2,2,2] s=1 p=1 roundingMode=floor', // https://github.com/tensorflow/tensorflow/issues/58758 'avgPool3d test-tensorflow {} x=[1,1,1,1,1] f=[1,1,3] s=1 p=valid', + // Node backend which uses TF 2.11.0 doesn't support number padding + 'avgPool3d test-tensorflow {} x=[1,1,1,1,1] f=[2,2,2] s=1 p=2', // Node backend which uses TF 2.4.0 doesn't support explicit padding 'maxPool test-tensorflow {} x=[3,3,1] f=[3,3] s=1 p=explicit', 'maxPoolBackprop test-tensorflow {} gradient x=[3,3,1] f=3 s=1 p=explicit',