|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <executorch/runtime/core/event_tracer.h> |
| 12 | + |
| 13 | +/** |
| 14 | + * @file |
| 15 | + * |
| 16 | + * This file contains the hooks that can be used by runtime delegate backend |
| 17 | + * authors to log profiling and debugging events from backend code. In order to |
| 18 | + * use these hooks delegate authors would have needed to generate a delegate |
| 19 | + * debug identifier mapping using the DelegateMappingBuilder library present in |
| 20 | + * executorch/exir/backend/utils.py. The delegate debug identifiers generated by |
| 21 | + * that library are the ones that need to be passed to these hooks to log |
| 22 | + * events. Using any other identifiers will cause post-processing of the events |
| 23 | + * data to not properly link back to the nodes in the original lowered graph. |
| 24 | + * |
| 25 | + * The benefit of defining these hooks is that we can easily control whether or |
| 26 | + * not we want to compile in the EventTracer code based on the status of the |
| 27 | + * ET_EVENT_TRACER_ENABLED flag. |
| 28 | + */ |
| 29 | + |
| 30 | +namespace torch { |
| 31 | +namespace executor { |
| 32 | + |
| 33 | +/** |
| 34 | + * Start the profiling of a delegate event. Similar to start_profiling it will |
| 35 | + * return an instance of EventTracerEntry that contains the details of this |
| 36 | + * event. Can be left in production code as these hooks compile conditionally. |
| 37 | + * |
| 38 | + * @param[in] event_tracer The event tracer instance that is doing the logging. |
| 39 | + * @param[in] name Human readable name for the delegate event. This name has |
| 40 | + * to be the same name that was passed in during the Debug delegate mapping |
| 41 | + * generation in the export/ahead-of-time process. If indices and not names |
| 42 | + * are used by this delegate to identify ops executed in the backend then |
| 43 | + * nullptr can be passed in. Users calling this interface do not need to keep |
| 44 | + * the memory pointed to by this pointer around. The string must be copied over |
| 45 | + * into internal memory during this call. |
| 46 | + * @param[in] delegate_debug_id The id of the delegate event. If string |
| 47 | + * based names are used by this delegate to identify ops executed in the |
| 48 | + * backend then kUnsetDebugHandle should be passed in here. |
| 49 | + */ |
| 50 | +inline EventTracerEntry event_tracer_start_profiling_delegate( |
| 51 | + EventTracer* event_tracer, |
| 52 | + const char* name, |
| 53 | + DebugHandle delegate_debug_id) { |
| 54 | +#ifdef ET_EVENT_TRACER_ENABLED |
| 55 | + if (event_tracer) { |
| 56 | + return event_tracer->start_profiling_delegate(name, delegate_debug_id); |
| 57 | + } |
| 58 | +#else //! ET_EVENT_TRACER_ENABLED |
| 59 | + (void)name; |
| 60 | + (void)delegate_debug_id; |
| 61 | +#endif |
| 62 | + // There is no active tracer; this value will be ignored. |
| 63 | + return EventTracerEntry(); |
| 64 | +} |
| 65 | +/** |
| 66 | + * Signal the end of the delegate profiling event contained in |
| 67 | + * event_tracer_entry. Users also have the option to log some some free-from |
| 68 | + * string based metadata along with this. Can be left in production code as |
| 69 | + * these hooks compile conditionally. |
| 70 | + * |
| 71 | + * @param[in] event_tracer The event tracer instance that is doing the logging. |
| 72 | + * @param[in] event_tracer_entry The EventTracerEntry returned by a call to |
| 73 | + * start_profiling_delegate(). |
| 74 | + * @param[in] metadata Free-form metadata associated with the delegate event. |
| 75 | + * Users calling this interface do not need to keep the memory pointed to by |
| 76 | + * this pointer around. The string must be copied over into internal memory |
| 77 | + * during this call. The input string must also be null terminated. |
| 78 | + */ |
| 79 | +inline void event_tracer_end_profiling_delegate( |
| 80 | + EventTracer* event_tracer, |
| 81 | + EventTracerEntry event_tracer_entry, |
| 82 | + const char* metadata = nullptr) { |
| 83 | +#ifdef ET_EVENT_TRACER_ENABLED |
| 84 | + if (event_tracer) { |
| 85 | + event_tracer->end_profiling_delegate(event_tracer_entry, metadata); |
| 86 | + } |
| 87 | +#else //! ET_EVENT_TRACER_ENABLED |
| 88 | + (void)event_tracer_entry; |
| 89 | + (void)metadata; |
| 90 | +#endif |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Some delegates get access to the profiling details only after the complete |
| 95 | + * graph has been executed. This interface is to support such use cases. It |
| 96 | + * can be called in a loop etc. to log any number of profiling events that are |
| 97 | + * part of this delegate. Can be left in production code as these hooks |
| 98 | + * compile conditionally. |
| 99 | + * |
| 100 | + * @param[in] event_tracer The event tracer instance that is doing the logging. |
| 101 | + * @param[in] name Human readable name for the delegate event. This name has |
| 102 | + * to be the same name that was passed in during the Debug delegate mapping |
| 103 | + * generation in the export/ahead-of-time process. If indices and not names |
| 104 | + * are used by this delegate to identify ops executed in the backend then |
| 105 | + * nullptr can be passed in. Users calling this interface do not need to keep |
| 106 | + * the memory pointed to by this pointer around. The string must |
| 107 | + * be copied over into internal memory during this call. |
| 108 | + * @param[in] delegate_debug_id The id of the delegate event. If string |
| 109 | + * based names are used by this delegate to identify ops executed in the |
| 110 | + * backend then -1 should be passed in here. |
| 111 | + * @param[in] start_time The timestamp when the delegate event started. |
| 112 | + * @param[in] end_time The timestamp when the delegate event finished. |
| 113 | + * @param[in] metadata Any extra data relevant to the execution that the user |
| 114 | + * wants to log along with this event. Pointer to metadata doesn't need to be |
| 115 | + * valid after the call to this function. Users calling this interface do not |
| 116 | + * need to keep the memory pointed to by this pointer around. The string must |
| 117 | + * be copied over into internal memory during this call. The input string must |
| 118 | + * also be a null terminated. |
| 119 | + */ |
| 120 | +inline void event_tracer_log_profiling_delegate( |
| 121 | + EventTracer* event_tracer, |
| 122 | + const char* name, |
| 123 | + DebugHandle delegate_debug_id, |
| 124 | + et_timestamp_t start_time, |
| 125 | + et_timestamp_t end_time, |
| 126 | + const char* metadata = nullptr) { |
| 127 | +#ifdef ET_EVENT_TRACER_ENABLED |
| 128 | + if (event_tracer) { |
| 129 | + event_tracer->log_profiling_delegate( |
| 130 | + name, delegate_debug_id, start_time, end_time, metadata); |
| 131 | + } |
| 132 | +#else //! ET_EVENT_TRACER_ENABLED |
| 133 | + (void)name; |
| 134 | + (void)delegate_debug_id; |
| 135 | + (void)start_time; |
| 136 | + (void)end_time; |
| 137 | + (void)metadata; |
| 138 | +#endif |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace executor |
| 142 | +} // namespace torch |
0 commit comments