MCPcopy Create free account
hub / github.com/pytorch/pytorch / _NnapiSerializer

Class _NnapiSerializer

torch/backends/_nnapi/serializer.py:318–2174  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

316
317
318class _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

Callers 2

process_for_nnapiFunction · 0.90
serialize_modelFunction · 0.85

Calls 15

add_getattrMethod · 0.95
add_constant_nodeMethod · 0.95
add_list_constructMethod · 0.95
add_tuple_constructMethod · 0.95
add_unsqueezeMethod · 0.95
add_toMethod · 0.95
_identityMethod · 0.95
add_reshapeMethod · 0.95
add_flattenMethod · 0.95
add_sliceMethod · 0.95
add_sizeMethod · 0.95
add_catMethod · 0.95

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…