Skip to content

Commit

Permalink
pip test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
masteryi-0018 committed Jan 22, 2025
1 parent fe1be21 commit 6f9a372
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ MODULE*

# py
__pycache__
*.egg-info

# model
*.gynn
Expand Down
6 changes: 3 additions & 3 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
file(GLOB BIND_SRC "*.cc")
pybind11_add_module(mininn MODULE ${BIND_SRC})
target_link_libraries(mininn PRIVATE runtime kernel operator parser graph utils)
pybind11_add_module(mininn_capi MODULE ${BIND_SRC})
target_link_libraries(mininn_capi PRIVATE runtime kernel operator parser graph utils)

message(STATUS "Source files for mininn library: ${BIND_SRC}")
message(STATUS "Source files for mininn_capi library: ${BIND_SRC}")
8 changes: 4 additions & 4 deletions python/mininn/predictor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import mininn
from mininn import mininn_capi

class Predictor():
def __init__(self, filename):
graph = mininn.Graph()
mininn.load_model(filename, graph)
self.Predictor = mininn.Predictor(graph)
graph = mininn_capi.Graph()
mininn_capi.load_model(filename, graph)
self.Predictor = mininn_capi.Predictor(graph)

def get_input(self):
inputs = self.Predictor.get_input_tensors()
Expand Down
2 changes: 1 addition & 1 deletion python/pybind11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void bind_class(py::module_ &m) {
.def("get_output_tensors", &Predictor::get_output_tensors);
}

PYBIND11_MODULE(mininn, m) {
PYBIND11_MODULE(mininn_capi, m) {
bind_function(m);
bind_enum(m);
bind_class(m);
Expand Down
48 changes: 30 additions & 18 deletions python/setup.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
from setuptools import setup, find_packages, Extension
from setuptools import setup, find_packages
from setuptools.command.build_py import build_py

import os
import shutil

VERSION = '0.0.1'
DESCRIPTION = 'My first Python package'
LONG_DESCRIPTION = 'My first Python package with a slightly longer description'
DESCRIPTION = 'python interface of MiniNN'
LONG_DESCRIPTION = 'Build a deep learning inference framework from scratch'

ext_modules = [
Extension(
'mininn',
["mininn.so"],
),
]
class CustomBuildCommand(build_py):
"""Custom build step to copy CMake-built .so file into the package."""
def run(self):
cmake_output = os.path.abspath("../build/python/mininn_capi.cpython-311-x86_64-linux-gnu.so")
target_dir = os.path.join(self.build_lib, "mininn")

if not os.path.exists(target_dir):
os.makedirs(target_dir)

if os.path.exists(cmake_output):
shutil.copyfile(cmake_output, os.path.join(target_dir, "mininn_capi.so"))
else:
raise FileNotFoundError(f"{cmake_output} does not exist. Build with CMake first.")

super().run()


# 配置
setup(
name="mininn",
version=VERSION,
author="masteryi-0018",
author_email="<[email protected]>",
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
packages=find_packages(),
install_requires=[],
name = "mininn",
version = VERSION,
author = "masteryi-0018",
author_email = "<[email protected]>",
description = DESCRIPTION,
long_description = LONG_DESCRIPTION,
packages = find_packages(),
cmdclass={"build_py": CustomBuildCommand},
)
6 changes: 3 additions & 3 deletions python/test/pip_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def main():

outputs = my_predictor.get_output()
c = outputs[0]
print(c[0], c.get_shape())
print(c.get_data()[0], c.get_shape())

output_size = c.get_size()
golden = [3.0] * output_size
assert c == golden
assert c.get_data() == golden
return


if __name__ == "__main__":
main()
main()
10 changes: 5 additions & 5 deletions python/test/pybind_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import mininn
import mininn_capi

def main():
# Tensor
tensor = mininn.Tensor()
tensor = mininn_capi.Tensor()
tensor.set_shape([1, 3, 224, 224])
print(tensor.get_shape())
print(tensor.get_size())
Expand All @@ -11,8 +11,8 @@ def main():

# parser
filename = "../../convertor/mininn_test.gynn"
graph = mininn.Graph()
mininn.load_model(filename, graph)
graph = mininn_capi.Graph()
mininn_capi.load_model(filename, graph)

# graph
node_num = len(graph.get_nodes())
Expand All @@ -26,7 +26,7 @@ def main():
print(graph.get_outputs())

# predictor
predictor = mininn.Predictor(graph)
predictor = mininn_capi.Predictor(graph)
input = predictor.get_input_tensors()
output = predictor.get_output_tensors()
for i in range(len(input)):
Expand Down

0 comments on commit 6f9a372

Please sign in to comment.