1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements.  See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership.  The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License.  You may obtain a copy of the License at
8#
9#   http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied.  See the License for the
15# specific language governing permissions and limitations
16# under the License.
17
18"""Performance benchmark tests for MXNet NDArray Unary Operations.
191. Operators are automatically fetched from MXNet operator registry.
202. Default Inputs are generated. See rules/default_params.py. You can override the default values.
21
22Below 54 unary Operators are covered:
23
24['BlockGrad', 'Flatten', 'abs', 'arccos', 'arccosh', 'arcsin', 'arcsinh', 'arctan', 'arctanh',
25'argmax_channel', 'cbrt', 'ceil', 'cos', 'cosh', 'degrees', 'erf', 'erfinv', 'exp', 'expm1', 'fix', 'flatten',
26'floor', 'gamma', 'gammaln', 'identity', 'log', 'log10', 'log1p', 'log2', 'logical_not', 'make_loss', 'negative',
27'ones_like', 'radians', 'rcbrt', 'reciprocal', 'relu', 'rint', 'round', 'rsqrt', 'shuffle', 'sigmoid', 'sign',
28'sin', 'sinh', 'size_array', 'softsign', 'sqrt', 'square', 'stop_gradient', 'tan', 'tanh', 'trunc', 'zeros_like']
29
30"""
31
32import mxnet as mx
33
34from benchmark.opperf.utils.op_registry_utils import get_all_unary_operators
35from benchmark.opperf.utils.benchmark_utils import run_op_benchmarks
36
37from benchmark.opperf.utils.benchmark_utils import run_performance_test
38from benchmark.opperf.utils.common_utils import merge_map_list
39from benchmark.opperf.rules.default_params import MX_OP_MODULE
40
41def run_mx_unary_operators_benchmarks(ctx=mx.cpu(), dtype='float32', profiler='native', warmup=25, runs=100):
42    """Runs benchmarks with the given context and precision (dtype)for all the unary
43    operators in MXNet.
44
45    Parameters
46    ----------
47    ctx: mx.ctx
48        Context to run benchmarks
49    dtype: str, default 'float32'
50        Precision to use for benchmarks
51    profiler: str, default 'native'
52        Type of Profiler to use (native/python)
53    warmup: int, default 25
54        Number of times to run for warmup
55    runs: int, default 100
56        Number of runs to capture benchmark results
57
58    Returns
59    -------
60    Dictionary of results. Key -> Name of the operator, Value -> Benchmark results.
61
62    """
63    # Run amp_multicast as it needs data as positional argument
64    amp_multicast_benchmark = run_performance_test([getattr(MX_OP_MODULE, "amp_multicast")],
65                                                   run_backward=True,
66                                                   dtype=dtype,
67                                                   ctx=ctx,
68                                                   profiler=profiler,
69                                                   inputs=[{"args": [(1024, 1024)],
70                                                            "num_outputs":1},
71                                                           {"args": [(10000, 1)],
72                                                            "num_outputs":1}],
73                                                   warmup=warmup,
74                                                   runs=runs)
75
76    # Fetch all Unary Operators
77    mx_unary_broadcast_ops = get_all_unary_operators()
78
79    # Run benchmarks
80    mx_unary_op_results = run_op_benchmarks(mx_unary_broadcast_ops, dtype, ctx, profiler, warmup, runs)
81    return merge_map_list(amp_multicast_benchmark + [mx_unary_op_results])
82