|
| 1 | +/* Copyright 2017 Google Inc. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +import * as conv_util from '../../src/math/conv_util'; |
| 17 | +import {NDArrayMathCPU} from '../../src/math/math_cpu'; |
| 18 | +import {Array3D} from '../../src/math/ndarray'; |
| 19 | + |
| 20 | +import {BenchmarkTest} from './benchmark'; |
| 21 | + |
| 22 | +const OP_RUNS = 40; |
| 23 | + |
| 24 | +export const MAX_POOL_BENCHMARK_TEST: BenchmarkTest = (size: number) => { |
| 25 | + if (size > 512) { |
| 26 | + return -1; |
| 27 | + } |
| 28 | + const positions = false; |
| 29 | + return testMaxPool(size, positions); |
| 30 | +}; |
| 31 | + |
| 32 | +function testMaxPool(size: number, positions: boolean): number { |
| 33 | + const math = new NDArrayMathCPU(); |
| 34 | + const outputDepth = 1; |
| 35 | + const xShape: [number, number, number] = [size, size, outputDepth]; |
| 36 | + const fieldSize = 11; |
| 37 | + const stride = 1; |
| 38 | + const zeroPad = conv_util.computeDefaultPad(xShape, fieldSize, stride); |
| 39 | + |
| 40 | + const x = Array3D.randUniform(xShape, -1, 1); |
| 41 | + |
| 42 | + const start = performance.now(); |
| 43 | + for (let i = 0; i < OP_RUNS; i++) { |
| 44 | + math.maxPool(x as Array3D, fieldSize, stride, zeroPad); |
| 45 | + } |
| 46 | + const avgTime = (performance.now() - start) / OP_RUNS; |
| 47 | + |
| 48 | + x.dispose(); |
| 49 | + |
| 50 | + return avgTime; |
| 51 | +} |
0 commit comments