From 207d6829c96dff804ed6922c76f488a3016d4d17 Mon Sep 17 00:00:00 2001 From: lewuathe Date: Wed, 6 Sep 2017 22:51:35 +0900 Subject: [PATCH] Comparison max pool benchmark between CPU and GPU --- demos/benchmarks/math-benchmark-run-groups.ts | 12 +++-- demos/benchmarks/max_pool_cpu_benchmark.ts | 51 +++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 demos/benchmarks/max_pool_cpu_benchmark.ts diff --git a/demos/benchmarks/math-benchmark-run-groups.ts b/demos/benchmarks/math-benchmark-run-groups.ts index 1c4fe6efe9..8cd6024521 100644 --- a/demos/benchmarks/math-benchmark-run-groups.ts +++ b/demos/benchmarks/math-benchmark-run-groups.ts @@ -19,6 +19,7 @@ import * as conv_transpose_gpu_benchmark from './conv_transpose_gpu_benchmark'; import * as logsumexp_cpu_benchmark from './logsumexp_cpu_benchmark'; import * as logsumexp_gpu_benchmark from './logsumexp_gpu_benchmark'; import * as max_pool_gpu_benchmark from './max_pool_gpu_benchmark'; +import * as max_pool_cpu_benchmark from './max_pool_cpu_benchmark'; import * as mulmat_cpu_benchmark from './mulmat_cpu_benchmark'; import * as mulmat_gpu_benchmark from './mulmat_gpu_benchmark'; @@ -55,14 +56,17 @@ export const BENCHMARK_RUN_GROUPS: BenchmarkRunGroup[] = [ 'd1=1, d2=1, f=11, s=1', conv_transpose_gpu_benchmark.BENCHMARK_TEST)], }, { - name: 'Max pool (GPU)', + name: 'Max pool (CPU vs GPU): d1=1, d2=1, f=11, s=1', min: 0, max: 1024, stepSize: 64, stepToSizeTransformation: (step: number) => Math.max(1, step), - benchmarkRuns: [new BenchmarkRun( - 'd1=1, d2=1, f=11, s=1', - max_pool_gpu_benchmark.MAX_POOL_BENCHMARK_TEST)], + benchmarkRuns: [ + new BenchmarkRun('max_pool_gpu', + max_pool_gpu_benchmark.MAX_POOL_BENCHMARK_TEST), + new BenchmarkRun('max_pool_cpu', + max_pool_cpu_benchmark.MAX_POOL_BENCHMARK_TEST) + ], }, { name: 'LogSumExp (CPU vs GPU): input [size, size]', diff --git a/demos/benchmarks/max_pool_cpu_benchmark.ts b/demos/benchmarks/max_pool_cpu_benchmark.ts new file mode 100644 index 0000000000..9b115c5fbb --- /dev/null +++ b/demos/benchmarks/max_pool_cpu_benchmark.ts @@ -0,0 +1,51 @@ +/* Copyright 2017 Google Inc. 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 * as conv_util from '../../src/math/conv_util'; +import {NDArrayMathCPU} from '../../src/math/math_cpu'; +import {Array3D} from '../../src/math/ndarray'; + +import {BenchmarkTest} from './benchmark'; + +const OP_RUNS = 40; + +export const MAX_POOL_BENCHMARK_TEST: BenchmarkTest = (size: number) => { + if (size > 512) { + return -1; + } + const positions = false; + return testMaxPool(size, positions); +}; + +function testMaxPool(size: number, positions: boolean): number { + const math = new NDArrayMathCPU(); + const outputDepth = 1; + const xShape: [number, number, number] = [size, size, outputDepth]; + const fieldSize = 11; + const stride = 1; + const zeroPad = conv_util.computeDefaultPad(xShape, fieldSize, stride); + + const x = Array3D.randUniform(xShape, -1, 1); + + const start = performance.now(); + for (let i = 0; i < OP_RUNS; i++) { + math.maxPool(x as Array3D, fieldSize, stride, zeroPad); + } + const avgTime = (performance.now() - start) / OP_RUNS; + + x.dispose(); + + return avgTime; +}