From d59a5118c4451ed1ac6ed30370321e8b993586e0 Mon Sep 17 00:00:00 2001 From: pytorchbot Date: Thu, 17 Oct 2024 10:55:25 -0700 Subject: [PATCH] Add a null check for `tracer` in the `Module::load_method` (#6319) Add a null check for `event_tracer` param in the `Module::load_method` (#6298) Summary: Pull Request resolved: https://github.com/pytorch/executorch/pull/6298 When testing the [LLM Manual](https://pytorch.org/executorch/0.4/llm/getting-started.html#profiling-and-debugging), I found etdump is not generated :(. It seems to be a bug introduced in D62520386 when a `tracer` parameter was added to `Module::load_method`. It overrides the `event_tracer_.get()`, resulting `tracer` being null and passed to `program_->load_method`, thus etdump is not generated. This diff just adds a check: it `tracer` is not null, use it; otherwise use the tracer get from class member event_tracer_. Reviewed By: tarun292, Gasoonjia, dbort Differential Revision: D64481537 fbshipit-source-id: 86cecbaea2b7293be28d60f4147deb31535fa6ea (cherry picked from commit ad0e5e81c71b1cadaeaaf85cdb9a6d0c78d0f67e) Co-authored-by: Olivia Liu --- extension/module/module.cpp | 6 ++++-- extension/module/module.h | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/extension/module/module.cpp b/extension/module/module.cpp index 58ada0c246..0e708ab0b7 100644 --- a/extension/module/module.cpp +++ b/extension/module/module.cpp @@ -125,7 +125,7 @@ runtime::Result> Module::method_names() { runtime::Error Module::load_method( const std::string& method_name, - torch::executor::EventTracer* tracer) { + torch::executor::EventTracer* event_tracer) { if (!is_method_loaded(method_name)) { ET_CHECK_OK_OR_RETURN_ERROR(load()); @@ -153,7 +153,9 @@ runtime::Error Module::load_method( method_holder.planned_memory.get(), temp_allocator_.get()); method_holder.method = ET_UNWRAP_UNIQUE(program_->load_method( - method_name.c_str(), method_holder.memory_manager.get(), tracer)); + method_name.c_str(), + method_holder.memory_manager.get(), + event_tracer ? event_tracer : this->event_tracer())); method_holder.inputs.resize(method_holder.method->inputs_size()); methods_.emplace(method_name, std::move(method_holder)); } diff --git a/extension/module/module.h b/extension/module/module.h index f7c9b1c8c5..45ed38a7ff 100644 --- a/extension/module/module.h +++ b/extension/module/module.h @@ -133,7 +133,10 @@ class Module { * needed. The loaded method is cached to reuse the next time it's executed. * * @param[in] method_name The name of the method to load. - * @param[in] event_tracer A EventTracer used for tracking and logging events. + * @param[in] event_tracer Per-method event tracer to profile/trace methods + * individually. When not given, the event tracer passed to the Module + * constructor is used. Otherwise, this per-method event tracer takes + * precedence. * * @returns An Error to indicate success or failure. */