Skip to content

Commit

Permalink
[PROFILER] Theoretical roofline models (apache#11066)
Browse files Browse the repository at this point in the history
`tvm.analysis.roofline_analysis` adds estimated roofline performance to a
profiling report. The roofline model measures how close an operator gets
to best possible memory bandwidth or FLOP/s depending on whether it is
memory or compute bound. This computation uses the runtime of the
operator along with two numbers extracted from the TIR code: bytes of
memory touched and number of floating point operations. Because these
numbers are extracted from TIR, they may not be 100% accurate. The best
possible memory bandwidth and FLOP/s are measured by running small
programs that are memory and compute bound respectively.

For now, this function only works with llvm cpu targets, but it should
be possible to extend to GPU targets.
  • Loading branch information
Tristan Konolige authored and Boblest Sebastian (ETAS-DEV/XPC-Fe1) committed May 27, 2022
1 parent df6eaa2 commit 1a1ac74
Show file tree
Hide file tree
Showing 7 changed files with 563 additions and 26 deletions.
15 changes: 15 additions & 0 deletions include/tvm/runtime/profiling.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,21 @@ class CountNode : public Object {
TVM_DECLARE_FINAL_OBJECT_INFO(CountNode, Object);
};

/* \brief A ratio of two things. */
class RatioNode : public Object {
public:
/* The ratio as a double precision floating point number. */
double ratio;

/* \brief Construct a new ratio.
* \param a The ratio.
*/
explicit RatioNode(double a) : ratio(a) {}

static constexpr const char* _type_key = "runtime.profiling.Ratio";
TVM_DECLARE_FINAL_OBJECT_INFO(RatioNode, Object);
};

/*! \brief String representation of an array of NDArray shapes
* \param shapes Array of NDArrays to get the shapes of.
* \return A textual representation of the shapes. For example: `float32[2], int64[1, 2]`.
Expand Down
47 changes: 47 additions & 0 deletions python/tvm/runtime/profiling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ class Report(Object):
Per-device metrics collected over the entire run.
"""

def __init__(
self, calls: Sequence[Dict[str, Object]], device_metrics: Dict[str, Dict[str, Object]]
):
"""Construct a profiling report from a list of metrics and per-device metrics.
Parameters
----------
calls : Sequence[Dict[str, Object]]
Per function call metrics.
device_metrics : Dict[str, Dict[str, Object]]
Per device metrics.
"""
self.__init_handle_by_constructor__(_ffi_api.Report, calls, device_metrics)

def csv(self):
"""Convert this profiling report into CSV format.
Expand Down Expand Up @@ -150,6 +165,38 @@ def from_json(cls, s):
return _ffi_api.FromJSON(s)


@_ffi.register_object("runtime.profiling.Count")
class Count(Object):
"""A integer count of something"""

def __init__(self, count: int):
self.__init_handle_by_constructor__(_ffi_api.Count, count)


@_ffi.register_object("runtime.profiling.Duration")
class Duration(Object):
"""A duration of something"""

def __init__(self, duration: float):
self.__init_handle_by_constructor__(_ffi_api.Duration, duration)


@_ffi.register_object("runtime.profiling.Percent")
class Percent(Object):
"""A Percent of something"""

def __init__(self, percent: float):
self.__init_handle_by_constructor__(_ffi_api.Percent, percent)


@_ffi.register_object("runtime.profiling.Ratio")
class Ratio(Object):
"""A Ratio of two things"""

def __init__(self, ratio: float):
self.__init_handle_by_constructor__(_ffi_api.Ratio, ratio)


@_ffi.register_object("runtime.profiling.MetricCollector")
class MetricCollector(Object):
"""Interface for user defined profiling metric collection."""
Expand Down
19 changes: 19 additions & 0 deletions python/tvm/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.
"""Utilities operating at a graph/model or other "high" level"""

from .roofline import estimate_peak_bandwidth, estimate_peak_fma_flops, roofline_analysis
Loading

0 comments on commit 1a1ac74

Please sign in to comment.