-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
264 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC. | ||
* 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. | ||
* ============================================================================= | ||
*/ | ||
|
||
#ifdef __EMSCRIPTEN__ | ||
#include <emscripten.h> | ||
#endif | ||
|
||
#include <algorithm> | ||
#include <limits> | ||
|
||
#include "tfjs-backend-wasm/src/cc/backend.h" | ||
#include "tfjs-backend-wasm/src/cc/pool3d_impl.h" | ||
|
||
namespace tfjs::wasm { | ||
|
||
// We use C-style API to interface with Javascript. | ||
extern "C" { | ||
|
||
#ifdef __EMSCRIPTEN__ | ||
EMSCRIPTEN_KEEPALIVE | ||
#endif | ||
|
||
// REQUIRES: | ||
// - Tensor `x`, `dx` and `dy` must have dtype float32 (checked in tfjs-core) | ||
// - Tensor `x`, `dx` and `dy` must have data format 'NDHWC' (checked in | ||
// tfjs-core) | ||
void MaxPool3DGrad(int x_id, int dy_id, int dx_id, int batch_size, | ||
int channel_size, int in_depth, int in_height, int in_width, | ||
int out_depth, int out_height, int out_width, | ||
int stride_depth, int stride_height, int stride_width, | ||
int dilation_depth, int dilation_height, int dilation_width, | ||
int effective_filter_depth, int effective_filter_height, | ||
int effective_filter_width, int pad_front, int pad_top, | ||
int pad_left, int filter_depth) { | ||
const TensorInfo& x_info = backend::get_tensor_info(x_id); | ||
const TensorInfo& dy_info = backend::get_tensor_info(dy_id); | ||
TensorInfo& dx_info = backend::get_tensor_info_out(dx_id); | ||
NDHWCPool3DInfo pool3d_info{ | ||
.batch_size = batch_size, | ||
.channel_size = channel_size, | ||
.in_depth = in_depth, | ||
.in_height = in_height, | ||
.in_width = in_width, | ||
.out_depth = out_depth, | ||
.out_height = out_height, | ||
.out_width = out_width, | ||
.stride_depth = stride_depth, | ||
.stride_height = stride_height, | ||
.stride_width = stride_width, | ||
.dilation_depth = dilation_depth, | ||
.dilation_height = dilation_height, | ||
.dilation_width = dilation_width, | ||
.effective_filter_depth = effective_filter_depth, | ||
.effective_filter_height = effective_filter_height, | ||
.effective_filter_width = effective_filter_width, | ||
.pad_front = pad_front, | ||
.pad_top = pad_top, | ||
.pad_left = pad_left, | ||
}; | ||
|
||
int* max_positions = new int[pool3d_info.out_size()]; | ||
NDHWCPool3DImpl</*IN=*/float, /*OUT=*/int>( | ||
x_info.f32(), max_positions, pool3d_info, | ||
/*filter_init=*/ | ||
[]() -> std::pair<float, int> { | ||
return {std::numeric_limits<float>::min(), 0}; | ||
}, | ||
/*filter_apply=*/ | ||
[](std::pair<float, int>& data, int x_offset, const float& x_val) { | ||
if (x_val >= data.first) { | ||
data = {x_val, x_offset}; | ||
} | ||
}, | ||
/*filter_aggregate=*/ | ||
[](const std::pair<float, int>& data) { return data.second; }); | ||
|
||
pool3d_info.pad_front = effective_filter_depth - 1 - pad_front; | ||
pool3d_info.pad_top = effective_filter_height - 1 - pad_top; | ||
pool3d_info.pad_left = effective_filter_width - 1 - pad_left; | ||
NDHWCPool3DGradImpl( | ||
dy_info.f32(), dx_info.f32_write(), pool3d_info, | ||
/*pixel_mask=*/ | ||
[&max_positions](int dy_offset, int dx_offset) { | ||
return static_cast<float>(dx_offset == max_positions[dy_offset]); | ||
}); | ||
|
||
delete[] max_positions; | ||
} | ||
|
||
} // extern "C" | ||
} // namespace tfjs::wasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC. | ||
* 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 {backend_util, KernelConfig, KernelFunc, MaxPool3DGrad, MaxPool3DGradAttrs, MaxPool3DGradInputs, TensorInfo} from '@tensorflow/tfjs-core'; | ||
|
||
import {BackendWasm} from '../backend_wasm'; | ||
|
||
let wasmMaxPool3DGrad: ( | ||
xId: number, dyId: number, dxId: number, batchSize: number, | ||
channelSize: number, inDepth: number, inHeight: number, inWidth: number, | ||
outDepth: number, outHeight: number, outWidth: number, strideDepth: number, | ||
strideHeight: number, strideWidth: number, dilationDepth: number, | ||
dilationHeight: number, dilationWidth: number, effectiveFilterDepth: number, | ||
effectiveFilterHeight: number, effectiveFilterWidth: number, | ||
padFront: number, padTop: number, padLeft: number) => void; | ||
|
||
function setup(backend: BackendWasm) { | ||
wasmMaxPool3DGrad = backend.wasm.cwrap('MaxPool3DGrad', null, [ | ||
'number', // xId | ||
'number', // dyId | ||
'number', // dxId | ||
'number', // batchSize | ||
'number', // channelSize | ||
'number', // inDepth | ||
'number', // inHeight | ||
'number', // inWidth | ||
'number', // outDepth | ||
'number', // outHeight | ||
'number', // outWidth | ||
'number', // strideDepth | ||
'number', // strideHeight | ||
'number', // strideWidth | ||
'number', // dilationDepth | ||
'number', // dilationHeight | ||
'number', // dilationWidth | ||
'number', // effectiveFilterDepth | ||
'number', // effectiveFilterHeight | ||
'number', // effectiveFilterWidth | ||
'number', // padFront | ||
'number', // padTop | ||
'number', // padLeft | ||
]); | ||
} | ||
|
||
export function maxPool3DGrad(args: { | ||
inputs: MaxPool3DGradInputs, | ||
attrs: MaxPool3DGradAttrs, | ||
backend: BackendWasm, | ||
}): TensorInfo { | ||
const {inputs, backend, attrs} = args; | ||
const {dy, input} = inputs; | ||
const {filterSize, strides, pad, dimRoundingMode} = attrs; | ||
|
||
const convInfo = backend_util.computePool3DInfo( | ||
input.shape as [number, number, number, number, number], filterSize, | ||
strides, /*dilations=*/1, pad, dimRoundingMode); | ||
const dx = backend.makeOutput(input.shape, input.dtype); | ||
|
||
wasmMaxPool3DGrad( | ||
backend.dataIdMap.get(input.dataId).id, | ||
backend.dataIdMap.get(dy.dataId).id, | ||
backend.dataIdMap.get(dx.dataId).id, | ||
convInfo.batchSize, | ||
// Since Pool3D ops (MaxPool3D and MaxPool3D) support 3D filter only, in | ||
// channels should always equal to out channels. | ||
/*channelSize=*/convInfo.inChannels, | ||
convInfo.inDepth, | ||
convInfo.inHeight, | ||
convInfo.inWidth, | ||
convInfo.outDepth, | ||
convInfo.outHeight, | ||
convInfo.outWidth, | ||
convInfo.strideDepth, | ||
convInfo.strideHeight, | ||
convInfo.strideWidth, | ||
convInfo.dilationDepth, | ||
convInfo.dilationHeight, | ||
convInfo.dilationWidth, | ||
convInfo.effectiveFilterDepth, | ||
convInfo.effectiveFilterHeight, | ||
convInfo.effectiveFilterWidth, | ||
convInfo.padInfo.front, | ||
convInfo.padInfo.top, | ||
convInfo.padInfo.left, | ||
); | ||
return dx; | ||
} | ||
|
||
export const maxPool3DGradConfig: KernelConfig = { | ||
kernelName: MaxPool3DGrad, | ||
backendName: 'wasm', | ||
setupFunc: setup, | ||
kernelFunc: maxPool3DGrad as unknown as KernelFunc | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters