MCPcopy Create free account
hub / github.com/pytorch/executorch / keep

Function keep

exir/program/_program.py:1095–1158  ·  view source on GitHub ↗
(op)

Source from the content-addressed store, hash-verified

1093
1094 # To address https://github.com/pytorch/executorch/issues/8781
1095 def keep(op):
1096 # Explicit allow list
1097 allow_list = []
1098 try:
1099 # Ops in torch.ops.quant are not always loaded, so we use try/except
1100 # Aliases output, but we need to allow it for XNNPACK
1101 allow_list.append(torch.ops.torchao.choose_qparams_affine.default)
1102 except:
1103 pass
1104
1105 if op in allow_list:
1106 return True
1107
1108 schema = op._schema
1109 native_schema = _pybind_schema_to_native_schema(schema)
1110 if native_schema is None:
1111 log_warning(
1112 f"Torchgen is not able to parse the schema of {op._schema}. This is not fatal."
1113 )
1114 else:
1115 if native_schema.is_mutable:
1116 log_warning(
1117 f"Op {op} was requested for preservation by partitioner. This request is ignored because it is mutable."
1118 )
1119 return False
1120
1121 if native_schema.aliased_return_names() != [None]:
1122 log_warning(
1123 f"Op {op} was requested for preservation by partitioner. This request is ignored because it aliases output."
1124 )
1125 return False
1126
1127 # Fallback: torchgen does not detect alias annotations on ops
1128 # returning lists of aliased tensors (e.g. split.Tensor returns
1129 # Tensor(a)[]). Check op._schema.returns directly.
1130 for ret in schema.returns:
1131 if ret.alias_info is not None:
1132 log_warning(
1133 f"Op {op} was requested for preservation by partitioner. This request is ignored because it aliases output."
1134 )
1135 return False
1136
1137 # Explicit block list of ops that don't work if asked for
1138 # preservation
1139 if op in [
1140 # Hits infinte recursion error when op is in
1141 # EDGE_DO_NOT_DECOMP namespace
1142 torch.ops.aten._to_copy.default,
1143 # scalar to tensor type promotion does not work on ops
1144 # in EDGE_DO_NOT_DECOMP namespace
1145 torch.ops.aten.mul.Tensor,
1146 torch.ops.aten.add.Tensor,
1147 torch.ops.aten.sub.Tensor,
1148 torch.ops.aten.div.Tensor,
1149 torch.ops.aten.item.default,
1150 torch.ops.aten._local_scalar_dense.default,
1151 torch.ops.aten.unbind.int,
1152 torch.ops.aten.split_with_sizes.default,

Callers

nothing calls this directly

Calls 3

log_warningFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected