Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
[Opperf] Add array rearrange operators to opperf (#15606)
Browse files Browse the repository at this point in the history
* add array rearrange operators to opperf

* Trigger notification

* 4d tensor, param support

* new line

* add alias logic
  • Loading branch information
ChaiBapchya authored and sandeep-krishnamurthy committed Jul 25, 2019
1 parent 8158ba4 commit b00bb81
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 11 deletions.
5 changes: 0 additions & 5 deletions benchmark/opperf/nd_operations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@
46. linalg_extractdiag
47. sgd_mom_update
48. SequenceLast
50. flip
51. SequenceReverse
52. swapaxes
53. SVMOutput
54. linalg_trsm
55. where
Expand All @@ -82,7 +80,6 @@
63. mp_sgd_mom_update
64. choose_element_0index
65. tile
66. space_to_depth
67. gather_nd
69. SequenceMask
70. reshape_like
Expand Down Expand Up @@ -110,14 +107,12 @@
94. broadcast_like
95. Embedding
96. linalg_makediag
97. transpose
98. linalg_syrk
99. squeeze
101. ROIPooling
102. ftrl_update
103. SliceChannel
104. slice_like
105. depth_to_space
106. linalg_maketrian
108. pad
109. LayerNorm
Expand Down
57 changes: 57 additions & 0 deletions benchmark/opperf/nd_operations/array_rearrange.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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 mxnet as mx
from benchmark.opperf.utils.benchmark_utils import run_op_benchmarks
from benchmark.opperf.utils.op_registry_utils import get_all_rearrange_operators

"""Performance benchmark tests for MXNet NDArray Rearrange Operators.
1. transpose
2. swapaxes
3. flip
4. depth_to_space
5. space_to_depth
"""


def run_rearrange_operators_benchmarks(ctx=mx.cpu(), dtype='float32', warmup=25, runs=100):
"""Runs benchmarks with the given context and precision (dtype) for all the
rearrange operators in MXNet.
Parameters
----------
ctx: mx.ctx
Context to run benchmarks
dtype: str, default 'float32'
Precision to use for benchmarks
warmup: int, default 25
Number of times to run for warmup
runs: int, default 100
Number of runs to capture benchmark results
Returns
-------
Dictionary of results. Key -> Name of the operator, Value -> Benchmark results.
"""
# Fetch all optimizer operators
mx_rearrange_ops = get_all_rearrange_operators()

# Run benchmarks
mx_rearrange_op_results = run_op_benchmarks(mx_rearrange_ops, dtype, ctx, warmup, runs)
return mx_rearrange_op_results
4 changes: 2 additions & 2 deletions benchmark/opperf/nd_operations/binary_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@


def run_mx_binary_broadcast_operators_benchmarks(ctx=mx.cpu(), dtype='float32', warmup=25, runs=100):
"""Runs benchmarks with the given context and precision (dtype)for all the binary
"""Runs benchmarks with the given context and precision (dtype) for all the binary
broadcast operators in MXNet.
Parameters
Expand All @@ -66,7 +66,7 @@ def run_mx_binary_broadcast_operators_benchmarks(ctx=mx.cpu(), dtype='float32',


def run_mx_binary_element_wise_operators_benchmarks(ctx=mx.cpu(), dtype='float32', warmup=25, runs=100):
"""Runs benchmarks with the given context and precision (dtype)for all the binary
"""Runs benchmarks with the given context and precision (dtype) for all the binary
element_wise operators in MXNet.
Parameters
Expand Down
4 changes: 4 additions & 0 deletions benchmark/opperf/opperf.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from benchmark.opperf.nd_operations.nn_conv_operators import run_pooling_operators_benchmarks, \
run_convolution_operators_benchmarks, run_transpose_convolution_operators_benchmarks
from benchmark.opperf.nd_operations.nn_basic_operators import run_nn_basic_operators_benchmarks
from benchmark.opperf.nd_operations.array_rearrange import run_rearrange_operators_benchmarks

from benchmark.opperf.utils.common_utils import merge_map_list, save_to_file
from benchmark.opperf.utils.op_registry_utils import get_operators_with_no_benchmark, \
Expand Down Expand Up @@ -78,6 +79,9 @@ def run_all_mxnet_operator_benchmarks(ctx=mx.cpu(), dtype='float32'):
# Run all Sorting and Searching operations benchmarks with default input values
mxnet_operator_benchmark_results.append(run_sorting_searching_operators_benchmarks(ctx=ctx, dtype=dtype))

# Run all Array Rearrange operations benchmarks with default input values
mxnet_operator_benchmark_results.append(run_rearrange_operators_benchmarks(ctx=ctx, dtype=dtype))

# ************************ MXNET NN OPERATOR BENCHMARKS ****************************

# Run all basic NN operations benchmarks with default input values
Expand Down
15 changes: 14 additions & 1 deletion benchmark/opperf/rules/default_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@
# NOTE: Data used is DEFAULT_DATA
DEFAULT_AXIS = [0]

# For rearrange operators
# NOTE: Data needs to be a 4D tensor for operators like space_to_depth and depth_to_space
# Hence below we append 4d to mark the difference.
# For depth_to_space, dimension 3 needs to be a multiple of 'block' and 1 should be a multiple of `block^2`
DEFAULT_DATA_4d = [(1, 4, 2, 4), (10,25,10,100)]
DEFAULT_DIM_1 = [0, 1, 2, 3]
DEFAULT_DIM_2 = [1, 2, 3, 0]
DEFAULT_BLOCK_SIZE = [2, 5]

# Default Inputs. MXNet Op Param Name to Default Input mapping
DEFAULTS_INPUTS = {"data": DEFAULT_DATA,
"lhs": DEFAULT_LHS,
Expand All @@ -81,7 +90,11 @@
"k_nd": DEFAULT_K_ND,
"p_nd": DEFAULT_P_ND,
"axis_shape": DEFAULT_AXIS_SHAPE,
"axis": DEFAULT_AXIS}
"axis": DEFAULT_AXIS,
"data_4d": DEFAULT_DATA_4d,
"dim1": DEFAULT_DIM_1,
"dim2": DEFAULT_DIM_2,
"block_size": DEFAULT_BLOCK_SIZE}

# These are names of MXNet operator parameters that is of type NDArray.
# We maintain this list to automatically recognize these parameters are to be
Expand Down
22 changes: 22 additions & 0 deletions benchmark/opperf/utils/op_registry_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def prepare_op_inputs(arg_params):
arg_params["params"]["arg_types"]):
if "NDArray" in arg_type and arg_name + "_nd" in DEFAULTS_INPUTS:
arg_values[arg_name] = DEFAULTS_INPUTS[arg_name + "_nd"]
elif "NDArray" in arg_type and arg_name + "_4d" in DEFAULTS_INPUTS:
arg_values[arg_name] = DEFAULTS_INPUTS[arg_name + "_4d"]
elif arg_name in DEFAULTS_INPUTS:
arg_values[arg_name] = DEFAULTS_INPUTS[arg_name]
elif "float" in arg_type and arg_name + "_float" in DEFAULTS_INPUTS:
Expand Down Expand Up @@ -262,6 +264,26 @@ def get_all_sorting_searching_operators():
return sort_search_mx_operators


def get_all_rearrange_operators():
"""Gets all array rearrange operators registered with MXNet.
Returns
-------
{"operator_name": {"has_backward", "nd_op_handle", "params"}}
"""
rearrange_ops = ['transpose','swapaxes','flip','depth_to_space','space_to_depth']

# Get all mxnet operators
mx_operators = _get_all_mxnet_operators()

# Filter for Array Rearrange operators
rearrange_mx_operators = {}
for op_name, op_params in mx_operators.items():
if op_name in rearrange_ops and op_name not in unique_ops:
rearrange_mx_operators[op_name] = mx_operators[op_name]
return rearrange_mx_operators


def get_operators_with_no_benchmark(operators_with_benchmark):
"""Gets all MXNet operators with not benchmark.
Expand Down
20 changes: 17 additions & 3 deletions benchmark/opperf/utils/profiler_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,28 @@ def _get_memory_profile(memory_profile_results):

def _get_operator_profile(operator_name, operator_profile_results):
operator_profile = {}

# alias map : dictionary of the form {"alias" : "registered_name"}
# allows to retrieve alias operator profile from the profiler results
# TODO handling - "identity" : "_copy"
alias_map = {"broadcast_plus" : "broadcast_add", "broadcast_minus" : "broadcast_sub", "flatten" : "Flatten", "max_axis" : "max",
"swapaxes" : "SwapAxis", "flip" : "reverse", "reshape" : "Reshape", "crop" : "slice", "sum_axis" : "sum", "min_axis" : "min"}

op_name = None

if operator_name in alias_map:
op_name = alias_map[operator_name]
else:
op_name = operator_name

for line in operator_profile_results:
if operator_name in line or operator_name[:3] + " " in line:
if op_name in line or op_name[:3] + " " in line:
operation = line.split()[0]
operation_avg_time = float(line.split()[-1])
if "_backward" in operation:
operator_profile["avg_time" + operation] = operation_avg_time
operator_profile["avg_time_backward_" + operator_name] = operation_avg_time
else:
operator_profile["avg_time_forward_" + operation] = operation_avg_time
operator_profile["avg_time_forward_" + operator_name] = operation_avg_time

return operator_profile

Expand Down

0 comments on commit b00bb81

Please sign in to comment.