Skip to content

Commit df30f69

Browse files
committed
Provide a python interface for compliation
- Provides a Python interface for compiling NN models via LiteCore in ahead-of-time compilation. Signed-off-by: chong-chen <[email protected]> Signed-off-by: jiseong.oh <[email protected]>
1 parent 9ab662c commit df30f69

File tree

5 files changed

+134
-3
lines changed

5 files changed

+134
-3
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2025 Samsung Electronics Co. LTD
2+
# All rights reserved
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
target_sources(
8+
PyEnnWrapperAdaptor PUBLIC PyEnnWrapperAdaptor.cpp
9+
PyEnnWrapperAdaptor.h
10+
)
11+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2025 Samsung Electronics Co. LTD
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+
10+
#include <executorch/backends/samsung/aot/PyEnnWrapperAdaptor.h>
11+
#include <pybind11/pybind11.h>
12+
13+
namespace torch {
14+
namespace executor {
15+
namespace enn {
16+
PYBIND11_MODULE(PyEnnWrapperAdaptor, m) {
17+
pybind11::class_<PyEnnWrapper, std::shared_ptr<PyEnnWrapper>>(m, "EnnWrapper")
18+
.def(pybind11::init())
19+
.def("Init", &PyEnnWrapper::Init)
20+
.def("IsNodeSupportedByBackend", &PyEnnWrapper::IsNodeSupportedByBackend)
21+
.def(
22+
"Compile",
23+
&PyEnnWrapper::Compile,
24+
"Ahead of time compilation for serialized graph.")
25+
.def("Destroy", &PyEnnWrapper::Destroy, "Release resources.");
26+
}
27+
} // namespace enn
28+
} // namespace executor
29+
} // namespace torch
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2025 Samsung Electronics Co. LTD
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 <include/graphgen_c.h>
12+
#include <include/graphgen_common.h>
13+
#include <pybind11/numpy.h>
14+
#include <pybind11/pybind11.h>
15+
16+
#include <iostream>
17+
#include <memory>
18+
#include <vector>
19+
20+
namespace py = pybind11;
21+
22+
namespace torch {
23+
namespace executor {
24+
namespace enn {
25+
26+
class PyEnnWrapper {
27+
public:
28+
PyEnnWrapper() {}
29+
30+
void Init(const py::bytes& compile_opts) {
31+
graphgen_instance_ = graphgen_create();
32+
}
33+
34+
bool IsNodeSupportedByBackend() {
35+
return False;
36+
}
37+
38+
py::array_t<char> Compile(const py::array_t<char>& model_buffer) {
39+
if (graphgen_instance_ == nullptr) {
40+
ENN_LOG_ERROR("Please call `Init()` first before compile.");
41+
return py::array_t<char>();
42+
}
43+
44+
45+
auto m_buf_info = model_buffer.request();
46+
auto* model_buf_ptr = reinterpret_cast<uint8_t*>(m_buf_info.ptr);
47+
NNCBuffer* nnc_buffer = nullptr;
48+
if (graphgen_generate(
49+
graphgen_instance_, model_buf_ptr, m_buf_info.size, &nnc_buffer) !=
50+
GraphGenResult::SUCCESS) {
51+
ENN_LOG_ERROR("Compile model failed.");
52+
return py::array_t<char>();
53+
}
54+
55+
auto result = py::array_t<char>({nnc_buffer->size}, {sizeof(char)});
56+
auto result_buf = result.request();
57+
memcpy(result_buf.ptr, nnc_buffer->addr, nnc_buffer->size);
58+
59+
graphgen_release_buffer(graphgen_instance_, nnc_buffer);
60+
61+
return result;
62+
}
63+
64+
void Destroy() {
65+
graphgen_release(graphgen_instance_);
66+
graphgen_instance_ = nullptr;
67+
}
68+
69+
~PyEnnWrapper() {
70+
Destroy();
71+
}
72+
73+
private:
74+
// pointer to enn software entry
75+
void* graphgen_instance_ = nullptr;
76+
};
77+
} // namespace enn
78+
} // namespace executor
79+
} // namespace torch

backends/samsung/enn_preprocess.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
from typing import Dict, final, List
99

10+
import executorch.backends.samsung.python.PyEnnWrapperAdaptor as PyEnnWrapper
1011
import torch
1112
from executorch.exir.backend.backend_details import (
1213
BackendDetails,
@@ -26,7 +27,12 @@ def preprocess(
2627
edge_program: ExportedProgram,
2728
compile_specs: List[CompileSpec],
2829
) -> PreprocessResult:
29-
# 1 Converter Init
30+
enn_wrapper = PyEnnWrapper.EnnWrapper()
31+
option_spec = get_compile_spec(
32+
compile_specs, ENN_COMPILE_OPTION_TITLE, required=True
33+
)
34+
enn_wrapper.Init(option_spec.value)
35+
3036
enn_preprocess_passes = PassManager(passes=[])
3137

3238
# 2 make enn graph
@@ -55,4 +61,5 @@ def preprocess(
5561
raise RuntimeError(f"{node.op}" " is not supported in ENN Delegate")
5662

5763
# 4 Compile Graph
64+
enn_wrapper.Destroy()
5865
return PreprocessResult(processed_bytes=0, debug_handle_map={})

backends/samsung/partition/enn_partitioner.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import logging
88
from typing import Any, Callable, Dict, List, Optional, Tuple
99

10+
import executorch.backends.samsung.python.PyEnnWrapperAdaptor as PyEnnWrapper
11+
1012
import torch
1113
from executorch.backends.samsung.enn_preprocess import EnnBackend
1214

@@ -33,7 +35,9 @@ def __init__(
3335
compile_specs: List[CompileSpec],
3436
):
3537
self.edge_program = edge_program
38+
self.enn_wrapper = PyEnnWrapper.EnnWrapper()
3639
option_spec = get_compile_spec(compile_specs, "Exynos compile", required=True)
40+
self.enn_wrapper.Init(option_spec.value)
3741

3842
def is_node_supported(self, _, node: torch.fx.Node) -> bool:
3943
if node.op != "call_function":
@@ -46,10 +50,11 @@ def is_node_supported(self, _, node: torch.fx.Node) -> bool:
4650
]:
4751
return False
4852

49-
return False
53+
supported = self.enn_wrapper.IsNodeSupportedByBackend()
54+
return supported
5055

5156
def __del__(self):
52-
pass
57+
self.enn_wrapper.Destroy()
5358

5459

5560
class EnnPartitioner(Partitioner):

0 commit comments

Comments
 (0)