forked from Samsung/ONE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasesession.py
More file actions
194 lines (163 loc) · 6.33 KB
/
basesession.py
File metadata and controls
194 lines (163 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from typing import List
import numpy as np
from ..native.libnnfw_api_pybind import infer, tensorinfo
from ..native.libnnfw_api_pybind.exception import OnertError
def num_elems(tensor_info):
"""Get the total number of elements in nnfw_tensorinfo.dims."""
n = 1
for x in range(tensor_info.rank):
n *= tensor_info.dims[x]
return n
class BaseSession:
"""
Base class providing common functionality for inference and training sessions.
"""
def __init__(self, backend_session=None):
"""
Initialize the BaseSession with a backend session.
Args:
backend_session: A backend-specific session object (e.g., nnfw_session).
"""
self.session = backend_session
self.inputs = []
self.outputs = []
def __getattr__(self, name):
"""
Delegate attribute access to the bound NNFW_SESSION instance.
Args:
name (str): The name of the attribute or method to access.
Returns:
The attribute or method from the bound NNFW_SESSION instance.
"""
if name in self.__dict__:
# First, try to get the attribute from the instance's own dictionary
return self.__dict__[name]
elif hasattr(self.session, name):
# If not found, delegate to the session object
return getattr(self.session, name)
else:
raise AttributeError(
f"'{type(self).__name__}' object has no attribute '{name}'")
def _recreate_session(self, backend_session):
"""
Protected method to recreate the session.
Subclasses can override this method to provide custom session recreation logic.
"""
if self.session is not None:
del self.session # Clean up the existing session
self.session = backend_session
def get_inputs_tensorinfo(self) -> List[tensorinfo]:
"""
Retrieve tensorinfo for all input tensors.
Raises:
OnertError: If the underlying C‑API call fails.
Returns:
list[tensorinfo]: A list of tensorinfo objects for each input.
"""
num_inputs: int = self.session.input_size()
infos: List[tensorinfo] = []
for i in range(num_inputs):
try:
infos.append(self.session.input_tensorinfo(i))
except ValueError:
raise
except Exception as e:
raise OnertError(f"Failed to get input tensorinfo #{i}: {e}") from e
return infos
def get_outputs_tensorinfo(self) -> List[tensorinfo]:
"""
Retrieve tensorinfo for all output tensors.
Raises:
OnertError: If the underlying C‑API call fails.
Returns:
list[tensorinfo]: A list of tensorinfo objects for each output.
"""
num_outputs: int = self.session.output_size()
infos: List[tensorinfo] = []
for i in range(num_outputs):
try:
infos.append(self.session.output_tensorinfo(i))
except ValueError:
raise
except Exception as e:
raise OnertError(f"Failed to get output tensorinfo #{i}: {e}") from e
return infos
def set_inputs(self, size, inputs_array=[]):
"""
Set the input tensors for the session.
Args:
size (int): Number of input tensors.
inputs_array (list): List of numpy arrays for the input data.
Raises:
ValueError: If session uninitialized.
OnertError: If any C‑API call fails.
"""
if self.session is None:
raise ValueError(
"Session is not initialized with a model. Please compile with a model before setting inputs."
)
self.inputs = []
for i in range(size):
try:
input_tensorinfo = self.session.input_tensorinfo(i)
except ValueError:
raise
except Exception as e:
raise OnertError(f"Failed to get input tensorinfo #{i}: {e}") from e
if len(inputs_array) > i:
input_array = np.array(inputs_array[i], dtype=input_tensorinfo.dtype)
else:
print(
f"Model's input size is {size}, but given inputs_array size is {len(inputs_array)}.\n{i}-th index input is replaced by an array filled with 0."
)
input_array = np.zeros((num_elems(input_tensorinfo)),
dtype=input_tensorinfo.dtype)
try:
self.session.set_input(i, input_array)
except ValueError:
raise
except Exception as e:
raise OnertError(f"Failed to set input #{i}: {e}") from e
self.inputs.append(input_array)
def set_outputs(self, size):
"""
Set the output tensors for the session.
Args:
size (int): Number of output tensors.
Raises:
ValueError: If session uninitialized.
OnertError: If any C‑API call fails.
"""
if self.session is None:
raise ValueError(
"Session is not initialized with a model. Please compile a model before setting outputs."
)
self.outputs = []
for i in range(size):
try:
output_tensorinfo = self.session.output_tensorinfo(i)
except ValueError:
raise
except Exception as e:
raise OnertError(f"Failed to get output tensorinfo #{i}: {e}") from e
output_array = np.zeros((num_elems(output_tensorinfo)),
dtype=output_tensorinfo.dtype)
try:
self.session.set_output(i, output_array)
except ValueError:
raise
except Exception as e:
raise OnertError(f"Failed to set output #{i}: {e}") from e
self.outputs.append(output_array)
def tensorinfo():
"""
Shortcut to create a fresh tensorinfo instance.
Raises:
OnertError: If the C‑API call fails.
"""
try:
return infer.nnfw_tensorinfo()
except OnertError:
raise
except Exception as e:
raise OnertError(f"Failed to create tensorinfo: {e}") from e