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 Binary Operations - covers both broadcast and element_wise.
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 20 binary broadcast Operators are covered:
23
24['broadcast_add', 'broadcast_div', 'broadcast_equal', 'broadcast_greater', 'broadcast_greater_equal',
25'broadcast_hypot', 'broadcast_lesser', 'broadcast_lesser_equal', 'broadcast_logical_and',
26'broadcast_logical_or', 'broadcast_logical_xor', 'broadcast_maximum', 'broadcast_minimum',
27'broadcast_minus', 'broadcast_mod', 'broadcast_mul', 'broadcast_not_equal', 'broadcast_plus',
28'broadcast_power', 'broadcast_sub']
29
30Below 4 binary element_wise Operators are covered:
31['elemwise_add', 'elemwise_mul', 'elemwise_sub', 'elemwise_div']
32
33"""
34import mxnet as mx
35
36from benchmark.opperf.utils.benchmark_utils import run_op_benchmarks
37from benchmark.opperf.utils.op_registry_utils import get_all_broadcast_binary_operators, \
38    get_all_elemen_wise_binary_operators, get_all_misc_binary_operators
39
40
41def run_mx_binary_misc_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 miscellaneous
43    binary 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    warmup: int, default 25
52        Number of times to run for warmup
53    runs: int, default 100
54        Number of runs to capture benchmark results
55
56    Returns
57    -------
58    Dictionary of results. Key -> Name of the operator, Value -> Benchmark results.
59
60    """
61    # Fetch all Miscellaneous Binary Operators
62    mx_binary_misc_ops = get_all_misc_binary_operators()
63    # Run benchmarks
64    mx_binary_op_results = run_op_benchmarks(mx_binary_misc_ops, dtype, ctx, profiler, warmup, runs)
65    return mx_binary_op_results
66
67
68def run_mx_binary_broadcast_operators_benchmarks(ctx=mx.cpu(), dtype='float32', profiler='native', warmup=25, runs=100):
69    """Runs benchmarks with the given context and precision (dtype) for all the binary
70    broadcast operators in MXNet.
71
72    Parameters
73    ----------
74    ctx: mx.ctx
75        Context to run benchmarks
76    dtype: str, default 'float32'
77        Precision to use for benchmarks
78    profiler: str, default 'native'
79        Type of Profiler to use (native/python)
80    warmup: int, default 25
81        Number of times to run for warmup
82    runs: int, default 100
83        Number of runs to capture benchmark results
84
85    Returns
86    -------
87    Dictionary of results. Key -> Name of the operator, Value -> Benchmark results.
88
89    """
90    # Fetch all Binary Broadcast Operators
91    mx_binary_broadcast_ops = get_all_broadcast_binary_operators()
92    # Run benchmarks
93    mx_binary_op_results = run_op_benchmarks(mx_binary_broadcast_ops, dtype, ctx, profiler, warmup, runs)
94    return mx_binary_op_results
95
96
97def run_mx_binary_element_wise_operators_benchmarks(ctx=mx.cpu(), dtype='float32', profiler='native', warmup=25, runs=100):
98    """Runs benchmarks with the given context and precision (dtype) for all the binary
99    element_wise operators in MXNet.
100
101    Parameters
102    ----------
103    ctx: mx.ctx
104        Context to run benchmarks
105    dtype: str, default 'float32'
106        Precision to use for benchmarks
107    profiler: str, default 'native'
108        Type of Profiler to use (native/python)
109    warmup: int, default 10
110        Number of times to run for warmup
111    runs: int, default 50
112        Number of runs to capture benchmark results
113
114    Returns
115    -------
116    Dictionary of results. Key -> Name of the operator, Value -> Benchmark results.
117
118    """
119    # Fetch all Binary Element_wise Operators
120    mx_binary_element_wise_ops = get_all_elemen_wise_binary_operators()
121    # Run benchmarks
122    mx_binary_op_results = run_op_benchmarks(mx_binary_element_wise_ops, dtype, ctx, profiler, warmup, runs)
123    return mx_binary_op_results
124