ONE - On-device Neural Engine
Loading...
Searching...
No Matches
package.infer.session.session Class Reference
Collaboration diagram for package.infer.session.session:

Public Member Functions

None __init__ (self, str path, str backends="cpu")
 
Union[List[np.ndarray], Tuple[List[np.ndarray], Dict[str, float]]] infer (self, List[np.ndarray] inputs_array, *bool measure=False)
 
- Public Member Functions inherited from package.common.basesession.BaseSession
 __getattr__ (self, name)
 
List[tensorinfo] get_inputs_tensorinfo (self)
 
List[tensorinfo] get_outputs_tensorinfo (self)
 
 set_inputs (self, size, inputs_array=[])
 

Data Fields

 outputs
 
- Data Fields inherited from package.common.basesession.BaseSession
 session
 
 inputs
 
 outputs
 

Protected Member Functions

None _update_inputs_tensorinfo (self, List[tensorinfo] new_infos)
 
 _time_block (self, Dict[str, float] metrics, str key, bool measure)
 
- Protected Member Functions inherited from package.common.basesession.BaseSession
 _recreate_session (self, backend_session)
 
 _set_outputs (self, size)
 

Protected Attributes

 _prepared
 

Detailed Description

Class for inference using nnfw_session.

Definition at line 12 of file session.py.

Constructor & Destructor Documentation

◆ __init__()

None package.infer.session.session.__init__ (   self,
str  path,
str   backends = "cpu" 
)
Initialize the inference session.

Args:
    path (str): Path to the model file or nnpackage directory.
    backends (str): Backends to use, default is "cpu".

Reimplemented from package.common.basesession.BaseSession.

Definition at line 16 of file session.py.

16 def __init__(self, path: str, backends: str = "cpu") -> None:
17 """
18 Initialize the inference session.
19
20 Args:
21 path (str): Path to the model file or nnpackage directory.
22 backends (str): Backends to use, default is "cpu".
23 """
24 super().__init__(infer.nnfw_session(path, backends))
25 self._prepared: bool = False
26

References package.infer.session.session.__init__(), nnfw::cker::Conv._prepared, nnfw::cker::FusedBatchNorm._prepared, nnfw::cker::Reduce._prepared, nnfw::ruy::Conv._prepared, package.infer.session.session._prepared, and onert::backend::cpu::ops::DepthwiseConvolutionLayer._prepared.

Referenced by package.infer.session.session.__init__().

Member Function Documentation

◆ _time_block()

package.infer.session.session._time_block (   self,
Dict[str, float]  metrics,
str  key,
bool  measure 
)
protected

Definition at line 167 of file session.py.

167 def _time_block(self, metrics: Dict[str, float], key: str, measure: bool):
168 if measure:
169 start = time.perf_counter()
170 yield
171 metrics[key] = (time.perf_counter() - start) * 1000
172 else:
173 yield

Referenced by package.infer.session.session.infer().

◆ _update_inputs_tensorinfo()

None package.infer.session.session._update_inputs_tensorinfo (   self,
List[tensorinfo]  new_infos 
)
protected
Update all input tensors' tensorinfo at once.

Args:
    new_infos (list[tensorinfo]): A list of updated tensorinfo objects for the inputs.

Raises:
    ValueError: If the number of new_infos does not match the session's input size,
                or if any tensorinfo contains a negative dimension.

    OnertError: If the underlying C-API call fails.

Definition at line 134 of file session.py.

134 def _update_inputs_tensorinfo(self, new_infos: List[tensorinfo]) -> None:
135 """
136 Update all input tensors' tensorinfo at once.
137
138 Args:
139 new_infos (list[tensorinfo]): A list of updated tensorinfo objects for the inputs.
140
141 Raises:
142 ValueError: If the number of new_infos does not match the session's input size,
143 or if any tensorinfo contains a negative dimension.
144
145 OnertError: If the underlying C-API call fails.
146 """
147 num_inputs: int = self.session.input_size()
148 if len(new_infos) != num_inputs:
149 raise ValueError(
150 f"Expected {num_inputs} input tensorinfo(s), but got {len(new_infos)}.")
151
152 for i, info in enumerate(new_infos):
153 # Check for any negative dimension in the specified rank
154 if any(d < 0 for d in info.dims[:info.rank]):
155 raise ValueError(
156 f"Input tensorinfo at index {i} contains negative dimension(s): "
157 f"{info.dims[:info.rank]}")
158 try:
159 self.session.set_input_tensorinfo(i, info)
160 except ValueError:
161 # re-raise ValueError directly
162 raise
163 except Exception as e:
164 raise OnertError(f"Failed to update input tensorinfo: {e}") from e
165

References validate_onnx2circle.OnnxRunner.session, onert::api::python::NNFW_SESSION.session, and package.common.basesession.BaseSession.session.

Referenced by package.infer.session.session.infer().

◆ infer()

Union[List[np.ndarray], Tuple[List[np.ndarray], Dict[str, float]]] package.infer.session.session.infer (   self,
List[np.ndarray]  inputs_array,
*bool   measure = False 
)
Run a complete inference cycle:
 - If the session has not been prepared or outputs have not been set, call prepare().
 - Automatically configure input buffers based on the provided numpy arrays.
 - Execute the inference session.
 - Return the output tensors with proper multi-dimensional shapes.

This method supports dynamic shape modification:
 - The input shapes can be adjusted dynamically.

Args:
    inputs_array (list[np.ndarray]): List of numpy arrays representing the input data.
    measure (bool): If True, measure prepare/io/run latencies (ms).

Returns:
    list[np.ndarray]: A list containing the output numpy arrays.
    OR
    (outputs, metrics): Tuple where metrics is a dict with keys
        'prepare_time_ms', 'io_time_ms', 'run_time_ms'

Definition at line 27 of file session.py.

32 ) -> Union[List[np.ndarray], Tuple[List[np.ndarray], Dict[str, float]]]:
33 """
34 Run a complete inference cycle:
35 - If the session has not been prepared or outputs have not been set, call prepare().
36 - Automatically configure input buffers based on the provided numpy arrays.
37 - Execute the inference session.
38 - Return the output tensors with proper multi-dimensional shapes.
39
40 This method supports dynamic shape modification:
41 - The input shapes can be adjusted dynamically.
42
43 Args:
44 inputs_array (list[np.ndarray]): List of numpy arrays representing the input data.
45 measure (bool): If True, measure prepare/io/run latencies (ms).
46
47 Returns:
48 list[np.ndarray]: A list containing the output numpy arrays.
49 OR
50 (outputs, metrics): Tuple where metrics is a dict with keys
51 'prepare_time_ms', 'io_time_ms', 'run_time_ms'
52 """
53 metrics: Dict[str, float] = {}
54
55 # Verify that the number of provided inputs matches the session's expected input count.
56 expected_input_size: int = self.session.input_size()
57 if len(inputs_array) != expected_input_size:
58 raise ValueError(
59 f"Expected {expected_input_size} input(s), but received {len(inputs_array)}."
60 )
61
62 # Check if the session is prepared. If not, call prepare() once.
63 if not self._prepared:
64 try:
65 with self._time_block(metrics, 'prepare_time_ms', measure):
66 # On first call, fix any -1 dims to real input shapes and validate
67 original_infos = self.get_inputs_tensorinfo()
68 fixed_infos = []
69 for idx, info in enumerate(original_infos):
70 input_shape = inputs_array[idx].shape
71 new_dims = []
72 static_dim_changed = False
73 # only the first `info.rank` entries matter
74 for j, d in enumerate(info.dims[:info.rank]):
75 if d == -1:
76 # replace dynamic dim with actual incoming shape
77 new_dims.append(input_shape[j])
78 elif d == input_shape[j]:
79 # static dim must match the provided array
80 new_dims.append(d)
81 else:
82 static_dim_changed = True
83
84 if static_dim_changed:
85 warnings.warn(
86 f"infer() called with input {idx}'s shape={input_shape}, "
87 f"which differs from model's expected shape={tuple(info.dims)}. "
88 "Ensure this is intended.", UserWarning)
89
90 info.dims = new_dims
91 fixed_infos.append(info)
92
93 # Update tensorinfo to optimize using it
94 self._update_inputs_tensorinfo(fixed_infos)
95
96 self.session.set_prepare_config(
97 prepare_config.ENABLE_INTERNAL_OUTPUT_ALLOC)
98 self.session.prepare()
99 self._prepared = True
100 except ValueError:
101 raise
102 except Exception as e:
103 raise OnertError(f"Session preparation failed: {e}") from e
104
105 # Configure input buffers using the current session's input size and provided data.
106 try:
107 with self._time_block(metrics, 'input_time_ms', measure):
108 self.set_inputs(expected_input_size, inputs_array)
109 except ValueError:
110 raise
111 except Exception as e:
112 raise OnertError(f"Failed to bind inputs: {e}") from e
113
114 # Execute the inference.
115 try:
116 with self._time_block(metrics, 'run_time_ms', measure):
117 self.session.run()
118 except ValueError:
119 raise
120 except Exception as e:
121 raise OnertError(f"Inference execution failed: {e}") from e
122
123 try:
124 with self._time_block(metrics, 'output_time_ms', measure):
125 self._set_outputs(self.session.output_size())
126 except ValueError:
127 raise
128 except Exception as e:
129 raise OnertError(f"Failed to bind outputs: {e}") from e
130
131 # Return the output buffers.
132 return (self.outputs, metrics) if measure else self.outputs
133
void run(std::ofstream &os, const circle::Model *model)

References nnfw::cker::Conv._prepared, nnfw::cker::FusedBatchNorm._prepared, nnfw::cker::Reduce._prepared, nnfw::ruy::Conv._prepared, package.infer.session.session._prepared, onert::backend::cpu::ops::DepthwiseConvolutionLayer._prepared, package.infer.session.session._time_block(), package.infer.session.session._update_inputs_tensorinfo(), package.common.basesession.BaseSession.get_inputs_tensorinfo(), validate_onnx2circle.OnnxRunner.session, onert::api::python::NNFW_SESSION.session, and package.common.basesession.BaseSession.session.

Field Documentation

◆ _prepared

package.infer.session.session._prepared
protected

◆ outputs

package.infer.session.session.outputs

The documentation for this class was generated from the following file: