MCPcopy
hub / github.com/OpenPPL/ppq / export_var

Method export_var

ppq/parser/tengine_exporter.py:154–188  ·  view source on GitHub ↗
(self, variable: Variable)

Source from the content-addressed store, hash-verified

152 return op_proto
153
154 def export_var(self, variable: Variable) -> onnx.TensorProto:
155 shape = variable.shape
156 dtype = variable.dtype.value
157
158 if dtype is None:
159 ppq_warning(
160 f"Data type of Variable {variable.name} is not correctly traced, "
161 "ppq will export it as fp32 variable to onnx."
162 )
163 dtype = DataType.FP32.value
164
165 if not variable.is_parameter:
166 tensor_proto = helper.make_tensor_value_info(
167 name=variable.name,
168 # PPQ data type has exact same eunm value with onnx.
169 elem_type=dtype,
170 shape=shape,
171 )
172 else:
173 value = variable.value
174 if isinstance(value, torch.Tensor):
175 if value.numel() == 0:
176 value = []
177 elif value.ndim >= 1:
178 value = convert_any_to_numpy(variable.value).flatten()
179 elif value.ndim == 0:
180 value = [
181 value.item(),
182 ] # it is fine for onnx, cause shape for this value will be []
183 else:
184 value = value # value is python primary type.
185 tensor_proto = helper.make_tensor(
186 name=variable.name, data_type=dtype, dims=shape, vals=value
187 )
188 return tensor_proto
189
190 def export(self, file_path: str, graph: BaseGraph, config_path: str = None):
191 # during export we will remove all boundary operations from graph.

Callers 1

exportMethod · 0.95

Calls 2

ppq_warningFunction · 0.90
convert_any_to_numpyFunction · 0.90

Tested by

no test coverage detected