14
14
#include < executorch/runtime/core/exec_aten/util/tensor_util.h>
15
15
#include < numeric>
16
16
17
+ #ifdef EXECUTORCH_ENABLE_EVENT_TRACER
18
+ #include < executorch/devtools/etdump/etdump_flatcc.h>
19
+ #endif
20
+
17
21
#define THROW_JS_ERROR (errorType, message, ...) \
18
22
({ \
19
23
char msg_buf[256 ]; \
@@ -51,10 +55,15 @@ using executorch::aten::Tensor;
51
55
using ::executorch::extension::BufferDataLoader;
52
56
using ::executorch::runtime::Error;
53
57
using ::executorch::runtime::EValue;
58
+ using ::executorch::runtime::EventTracer;
54
59
using ::executorch::runtime::Result;
55
60
using ::executorch::runtime::Tag;
56
61
using ::executorch::runtime::TensorInfo;
57
62
63
+ #ifdef EXECUTORCH_ENABLE_EVENT_TRACER
64
+ using executorch::etdump::ETDumpGen;
65
+ #endif
66
+
58
67
namespace executorch {
59
68
namespace extension {
60
69
namespace wasm {
@@ -495,6 +504,35 @@ struct ET_EXPERIMENTAL JsMethodMeta {
495
504
}
496
505
};
497
506
507
+ /* *
508
+ * EXPERIMENTAL: Wrapper around ETDumpResult for JavaScript.
509
+ */
510
+ #ifdef EXECUTORCH_ENABLE_EVENT_TRACER
511
+ class ET_EXPERIMENTAL JsETDumpResult final {
512
+ public:
513
+ JsETDumpResult () = delete ;
514
+ JsETDumpResult (const JsETDumpResult&) = delete ;
515
+ JsETDumpResult& operator =(const JsETDumpResult&) = delete ;
516
+ JsETDumpResult (JsETDumpResult&&) = default ;
517
+ JsETDumpResult& operator =(JsETDumpResult&&) = default ;
518
+
519
+ explicit JsETDumpResult (uint8_t * buffer, size_t size)
520
+ : buffer_(buffer), size_(size) {}
521
+
522
+ ~JsETDumpResult () {
523
+ free (buffer_);
524
+ }
525
+
526
+ val get_buffer () const {
527
+ return val (typed_memory_view (size_, buffer_));
528
+ }
529
+
530
+ private:
531
+ uint8_t * buffer_;
532
+ size_t size_;
533
+ };
534
+ #endif
535
+
498
536
/* *
499
537
* EXPERIMENTAL: Wrapper around extension/Module for JavaScript.
500
538
*/
@@ -518,17 +556,32 @@ class ET_EXPERIMENTAL JsModule final {
518
556
val memory_view = val (typed_memory_view (length, buffer.data ()));
519
557
memory_view.call <void >(" set" , data);
520
558
auto loader = std::make_unique<BufferDataLoader>(buffer.data (), length);
559
+
560
+ #ifdef EXECUTORCH_ENABLE_EVENT_TRACER
561
+ std::unique_ptr<EventTracer> etdump_gen = std::make_unique<ETDumpGen>();
562
+ #else
563
+ std::unique_ptr<EventTracer> etdump_gen = nullptr ;
564
+ #endif
521
565
return std::make_unique<JsModule>(
522
- std::move (buffer), std::make_unique<Module>(std::move (loader)));
566
+ std::move (buffer),
567
+ std::make_unique<Module>(
568
+ std::move (loader), nullptr , nullptr , std::move (etdump_gen)));
523
569
}
524
570
525
571
static std::unique_ptr<JsModule> load (val data) {
526
572
if (data.isNull () || data.isUndefined ()) {
527
573
THROW_JS_ERROR (TypeError, " Data cannot be null or undefined" );
528
574
}
529
575
if (data.isString ()) {
530
- return std::make_unique<JsModule>(
531
- std::make_unique<Module>(data.as <std::string>()));
576
+ #ifdef EXECUTORCH_ENABLE_EVENT_TRACER
577
+ std::unique_ptr<EventTracer> etdump_gen = std::make_unique<ETDumpGen>();
578
+ #else
579
+ std::unique_ptr<EventTracer> etdump_gen = nullptr ;
580
+ #endif
581
+ return std::make_unique<JsModule>(std::make_unique<Module>(
582
+ data.as <std::string>(),
583
+ Module::LoadMode::File,
584
+ std::move (etdump_gen)));
532
585
} else if (data.instanceof (val::global (" Uint8Array" ))) {
533
586
return load_from_uint8_array (data);
534
587
} else if (data.instanceof (val::global (" ArrayBuffer" ))) {
@@ -569,6 +622,18 @@ class ET_EXPERIMENTAL JsModule final {
569
622
return JsMethodMeta::from_method_meta (res.get ());
570
623
}
571
624
625
+ #ifdef EXECUTORCH_ENABLE_EVENT_TRACER
626
+ std::unique_ptr<JsETDumpResult> etdump () {
627
+ ETDumpGen* etdump_gen = dynamic_cast <ETDumpGen*>(module_->event_tracer ());
628
+ if (etdump_gen == nullptr ) {
629
+ return nullptr ;
630
+ }
631
+ auto etdump_data = etdump_gen->get_etdump_data ();
632
+ return std::make_unique<JsETDumpResult>(
633
+ static_cast <uint8_t *>(etdump_data.buf ), etdump_data.size );
634
+ }
635
+ #endif
636
+
572
637
val_array<val> execute (const std::string& method, val js_inputs) {
573
638
std::vector<EValue> inputs;
574
639
if (js_inputs.isArray ()) {
@@ -613,11 +678,19 @@ EMSCRIPTEN_BINDINGS(WasmBindings) {
613
678
#define JS_DECLARE_TAG (NAME ) .value(#NAME, Tag::NAME)
614
679
EXECUTORCH_FORALL_TAGS (JS_DECLARE_TAG);
615
680
681
+ #ifdef EXECUTORCH_ENABLE_EVENT_TRACER
682
+ class_<JsETDumpResult>(" ETDumpResult" )
683
+ .property (" buffer" , &JsETDumpResult::get_buffer);
684
+ #endif
685
+
616
686
class_<JsModule>(" Module" )
617
687
.class_function (" load" , &JsModule::load)
618
688
.function (" getMethods" , &JsModule::get_methods)
619
689
.function (" loadMethod" , &JsModule::load_method)
620
690
.function (" getMethodMeta" , &JsModule::get_method_meta)
691
+ #ifdef EXECUTORCH_ENABLE_EVENT_TRACER
692
+ .function (" etdump" , &JsModule::etdump)
693
+ #endif
621
694
.function (" execute" , &JsModule::execute)
622
695
.function (" forward" , &JsModule::forward);
623
696
class_<JsTensor>(" Tensor" )
0 commit comments