| 316 | |
| 317 | |
| 318 | class _NnapiSerializer: |
| 319 | def __init__(self, config, use_int16_for_qint16=False): |
| 320 | self.operands = [] |
| 321 | self.values = [] |
| 322 | self.operations = [] |
| 323 | self.value_data = [] |
| 324 | self.operation_args = [] |
| 325 | self.inputs = [] |
| 326 | self.outputs = [] |
| 327 | self.flexible_shape_computation_lines = [] |
| 328 | |
| 329 | self.modules = {} |
| 330 | self.constants = {} |
| 331 | self.tensor_sequences = {} |
| 332 | self.jitval_operand_map = {} |
| 333 | self.cached_immediates = {} |
| 334 | self.used_weights = [] |
| 335 | self.weight_offset = 0 |
| 336 | self.use_int16_for_qint16 = use_int16_for_qint16 |
| 337 | |
| 338 | if config is None: |
| 339 | config = {} |
| 340 | |
| 341 | def get_next_operand_id(self): |
| 342 | return len(self.operands) |
| 343 | |
| 344 | # Add a tensor operand corresponding to a JIT Value. |
| 345 | # Returns the NNAPI operand ID. Can be looked up later with |
| 346 | # get_tensor_operand_by_jitval. |
| 347 | def add_tensor_operand(self, jitval, oper): |
| 348 | assert isinstance(oper, Operand) |
| 349 | if jitval in self.jitval_operand_map: |
| 350 | raise Exception(f"Duplicate tensor: {jitval!r}") |
| 351 | |
| 352 | operand_id = self.get_next_operand_id() |
| 353 | self.operands.append(oper) |
| 354 | self.jitval_operand_map[jitval] = operand_id |
| 355 | return operand_id |
| 356 | |
| 357 | # Add a tensor operand that does not correspond to a JIT Value. |
| 358 | # Useful for cases where multiple NNAPI operands are required |
| 359 | # to implement one JIT IR node. Returns the NNAPI operand ID. |
| 360 | def add_anonymous_tensor_operand(self, oper): |
| 361 | assert isinstance(oper, Operand) |
| 362 | operand_id = self.get_next_operand_id() |
| 363 | self.operands.append(oper) |
| 364 | return operand_id |
| 365 | |
| 366 | def torch_tensor_to_operand(self, tensor, dim_order): |
| 367 | dtype = str(tensor.dtype).replace("torch.", "") |
| 368 | scale = 0.0 |
| 369 | zero_point = 0 |
| 370 | if dtype == "float32": |
| 371 | op_type = NNAPI_OperandCode.TENSOR_FLOAT32 |
| 372 | elif dtype == "int32": |
| 373 | op_type = NNAPI_OperandCode.TENSOR_INT32 |
| 374 | elif dtype == "quint8": |
| 375 | op_type = NNAPI_OperandCode.TENSOR_QUANT8_ASYMM |
no test coverage detected
searching dependent graphs…