forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AvgPool3DGrad.ts
112 lines (100 loc) · 4.47 KB
/
AvgPool3DGrad.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import {AvgPool3DGrad, AvgPool3DGradAttrs, AvgPool3DGradInputs, backend_util, buffer, KernelConfig, KernelFunc, Rank, TensorInfo} from '@tensorflow/tfjs-core';
import {MathBackendCPU} from '../backend_cpu';
import {assertNotComplex} from '../cpu_util';
export function avgPool3DGrad(args: {
inputs: AvgPool3DGradInputs,
backend: MathBackendCPU,
attrs: AvgPool3DGradAttrs
}): TensorInfo {
const {inputs, backend, attrs} = args;
const {dy, input} = inputs;
const {filterSize, strides, pad, dimRoundingMode} = attrs;
assertNotComplex([dy, input], 'avgPool3DGrad');
const convInfo = backend_util.computePool3DInfo(
input.shape as [number, number, number, number, number], filterSize,
strides, 1 /* dilations */, pad, dimRoundingMode);
const strideDepth = convInfo.strideDepth;
const strideHeight = convInfo.strideHeight;
const strideWidth = convInfo.strideWidth;
const filterDepth = convInfo.filterDepth;
const filterHeight = convInfo.filterHeight;
const filterWidth = convInfo.filterWidth;
const dilationDepth = convInfo.dilationDepth;
const dilationHeight = convInfo.dilationHeight;
const dilationWidth = convInfo.dilationWidth;
const effectiveFilterDepth = convInfo.effectiveFilterDepth;
const effectiveFilterHeight = convInfo.effectiveFilterHeight;
const effectiveFilterWidth = convInfo.effectiveFilterWidth;
const padFront = effectiveFilterDepth - 1 - convInfo.padInfo.front;
const padLeft = effectiveFilterWidth - 1 - convInfo.padInfo.left;
const padTop = effectiveFilterHeight - 1 - convInfo.padInfo.top;
const dx = buffer(input.shape, 'float32');
const avgMultiplier = 1 / (filterDepth * filterHeight * filterWidth);
const dyBuf = backend.bufferSync<Rank, 'float32'>(dy);
for (let batch = 0; batch < convInfo.batchSize; ++batch) {
for (let channel = 0; channel < convInfo.inChannels; ++channel) {
for (let dxDepth = 0; dxDepth < convInfo.inDepth; ++dxDepth) {
for (let dxRow = 0; dxRow < convInfo.inHeight; ++dxRow) {
for (let dxCol = 0; dxCol < convInfo.inWidth; ++dxCol) {
// Shader code begins.
const dyDepthCorner = dxDepth - padFront;
const dyRowCorner = dxRow - padTop;
const dyColCorner = dxCol - padLeft;
let dotProd = 0;
for (let wDepth = 0; wDepth < effectiveFilterDepth;
wDepth += dilationDepth) {
const dyDepth = (dyDepthCorner + wDepth) / strideDepth;
if (dyDepth < 0 || dyDepth >= convInfo.outDepth ||
Math.floor(dyDepth) !== dyDepth) {
continue;
}
for (let wRow = 0; wRow < effectiveFilterHeight;
wRow += dilationHeight) {
const dyRow = (dyRowCorner + wRow) / strideHeight;
if (dyRow < 0 || dyRow >= convInfo.outHeight ||
Math.floor(dyRow) !== dyRow) {
continue;
}
for (let wCol = 0; wCol < effectiveFilterWidth;
wCol += dilationWidth) {
const dyCol = (dyColCorner + wCol) / strideWidth;
if (dyCol < 0 || dyCol >= convInfo.outWidth ||
Math.floor(dyCol) !== dyCol) {
continue;
}
const pixel =
dyBuf.get(batch, dyDepth, dyRow, dyCol, channel);
dotProd += pixel;
}
}
}
dx.set(
dotProd * avgMultiplier, batch, dxDepth, dxRow, dxCol, channel);
}
}
}
}
}
return backend.makeTensorInfo(dx.shape, dx.dtype, dx.values);
}
export const avgPool3DGradConfig: KernelConfig = {
kernelName: AvgPool3DGrad,
backendName: 'cpu',
kernelFunc: avgPool3DGrad as unknown as KernelFunc
};