• Home
  • Raw
  • Download

Lines Matching full:guard

61     Guard,
147 A helper class that contains the root guard manager. An instance of this
177 def get_guard_lines(self, guard): argument
178 guard_name = guard.__class__.__name__
179 parts = guard.verbose_code_parts()
205 for guard in mgr.get_leaf_guards():
206 … if isinstance(guard, torch._C._dynamo.guards.NO_TENSOR_ALIASING): # type: ignore[attr-defined]
209 body.writelines(self.get_guard_lines(guard))
213 guard.__class__.__name__,
217 body.writelines(self.get_guard_lines(guard))
252 for guard in self.root.get_epilogue_lambda_guards():
253 body.writelines(self.get_guard_lines(guard))
265 # This should be called when the guard manager is fully populated
277 for guard in mgr.get_leaf_guards():
278 … if isinstance(guard, torch._C._dynamo.guards.NO_TENSOR_ALIASING): # type: ignore[attr-defined]
280 self.code_parts.extend(get_code_parts(guard))
283 self.code_parts.extend(get_code_parts(guard))
380 def get_verbose_code_part(code_part: str, guard: Guard) -> str: argument
382 if guard.user_stack:
383 for fs in reversed(guard.user_stack):
387 elif guard.stack:
388 extra = f" # {format_frame(guard.stack.summary()[-1])}"
394 code_parts: Union[str | List[str]], guard: Guard argument
398 return [get_verbose_code_part(code_part, guard) for code_part in code_parts]
468 # We have to insert a key manager guard here
487 def match_on_id_for_tensor(guard): argument
488 source = guard.originating_source
492 # The ready to eval generated code (possibly multiple parts) for a guard, plus
493 # the original guard object that created it for provenance
497 guard: Guard
534 # Code is python expression strings generated for each guard
543 # Most of the time, we generate Python code in a guard to directly
549 # TENSOR_MATCH guard, we just add another entry to
557 self.tensor_check_guards: List[Guard] = []
570 # Keep track of weak references of objects with ID_MATCH guard. This
575 # Save the guard managers to avoid repeatedly traversing sources.
582 def guard_on_dict_keys_and_ignore_order(self, example_value, guard): argument
583 dict_mgr = self.get_guard_manager(guard)
587 f"added the dict to tx.output.guard_on_key_order for {guard.name}"
591 dict_source = guard.originating_source.name()
594 value_source = GetItemSource(guard.originating_source, index=key)
605 def guard_on_dict_keys_and_order(self, value, guard): argument
607 # ID_MATCH or EQUALS_MATCH guard on the key.
608 dict_mgr = self.get_guard_manager(guard)
612 f"to set the right guard manager enum for {guard.name}"
617 key_source = get_key_index_source(guard.name, idx)
625 # Install ID_MATCH guard
630 f"__check_obj_id({key_source}, {id_val})", guard
634 # Install EQUALS_MATCH guard
636 key, get_verbose_code_parts(f"{key_source} == {key!r}", guard)
674 # Install the key manager and add equals match guard
744 # do not guard on key order for _parameters etc unless the user code
760 # Get __dict__ accessor. No need to guard on dict key order, so use base
761 # Guard Manager
863 # because globals dict is big and we typically guard on a very
879 # Don't do anything here. We guard on global state completely in
1023 # kwdefaults is a dict. No need to guard on dict order.
1105 f"missing guard manager builder {source} - {source.name()}"
1111 def get_guard_manager(self, guard: Guard): argument
1112 return self.get_guard_manager_from_source(guard.originating_source)
1121 # Adds a lambda leaf guard to the root guard manager. It wraps the
1123 # guard.
1146 # guard code.
1151 # string (or stored in the Guard) as being guarded upon. It's important
1152 # to call this before generating some code that makes use of 'guard',
1154 # you reference in the actual guard closure (oops!)
1155 def arg_ref(self, guard: Union[str, Guard]) -> str: argument
1157 if isinstance(guard, str):
1158 name = guard
1160 name = guard.name
1165 log.warning("invalid var name: %s", guard)
1170 def _guard_on_attribute(self, guard: Guard, attr_name: str, guard_fn): argument
1171 attr_source = AttrSource(guard.originating_source, attr_name)
1173 new_guard = Guard(
1174 attr_source, guard_fn, stack=guard.stack, user_stack=guard.user_stack
1179 def HASATTR(self, guard: Guard): argument
1180 source = guard.originating_source
1183 assert isinstance(source, AttrSource), f"invalid source {guard.name}"
1196 guard, [code], provided_guarded_object=self.get(base)
1203 # acts as hasattr guard.
1209 # guard by going through __dict__ attrs.
1227 source=guard.name,
1233 attr, get_verbose_code_parts(code, guard)
1236 self._produce_guard_code(guard, [code])
1238 def NOT_PRESENT_IN_GENERIC_DICT(self, guard: Guard, attr=None) -> None: argument
1240 ref = self.arg_ref(guard)
1241 val = self.get(guard.name)
1244 base_manager = self.get_guard_manager(guard)
1246 mod_dict_source = f"{guard.name}.__dict__"
1255 False, attr, get_verbose_code_parts(code, guard)
1258 def TYPE_MATCH(self, guard: Guard) -> None: argument
1260 t = type(self.get(guard.name))
1262 code = f"___check_type_id({self.arg_ref(guard)}, {obj_id})"
1263 self._set_guard_export_info(guard, [code])
1266 self.get_guard_manager(guard).add_type_match_guard(
1267 obj_id, get_verbose_code_parts(code, guard)
1270 self._produce_guard_code(guard, [code])
1272 def DICT_VERSION(self, guard: Guard): argument
1274 ref = self.arg_ref(guard)
1275 val = self.get(guard.name)
1276 version = dict_version(self.get(guard.name))
1278 self._set_guard_export_info(guard, [code])
1283 self.get_guard_manager(guard).add_dict_version_guard(
1284 val, get_verbose_code_parts(code, guard)
1287 self._produce_guard_code(guard, [code])
1289 def DICT_CONTAINS(self, guard: Guard, key: str, invert: bool): argument
1290 dict_ref = self.arg_ref(guard)
1294 self._set_guard_export_info(guard, [code])
1297 self.get_guard_manager(guard).add_dict_contains_guard(
1298 not invert, key, get_verbose_code_parts(code, guard)
1301 self._produce_guard_code(guard, [code])
1303 def ID_MATCH(self, guard: Guard): argument
1305 if isinstance(guard.originating_source, TypeSource):
1306 # optional optimization to produce cleaner/faster guard code
1308Guard(guard.originating_source.base, GuardBuilder.TYPE_MATCH) # type: ignore[arg-type]
1311 ref = self.arg_ref(guard)
1312 val = self.get(guard.name)
1315 self._set_guard_export_info(guard, [code])
1318 self.get_guard_manager(guard).add_id_match_guard(
1319 id_val, get_verbose_code_parts(code, guard)
1322 self._produce_guard_code(guard, [code])
1326 if isinstance(guard.originating_source, LocalSource):
1331 local_name = guard.originating_source.local_name
1336 def NOT_NONE_MATCH(self, guard: Guard, value=None): argument
1337 ref = self.arg_ref(guard)
1338 val = self.get(guard.name)
1341 self._set_guard_export_info(guard, [code])
1344 self.get_guard_manager(guard).add_not_none_guard(
1345 get_verbose_code_parts(code, guard)
1348 self._produce_guard_code(guard, [code])
1350 def NAME_MATCH(self, guard: Guard): argument
1351 self._guard_on_attribute(guard, "__name__", GuardBuilder.EQUALS_MATCH)
1353 def DATA_PTR_MATCH(self, guard: Guard): argument
1354 # Add a type check. C++ guard has the type check internally, so only
1357 self.TYPE_MATCH(guard)
1359 obj = self.get(guard.name)
1360 code = f"{self.arg_ref(guard)}.data_ptr() == {obj.data_ptr()}"
1361 self._set_guard_export_info(guard, [code])
1364 self.get_guard_manager(guard).add_data_ptr_guard(
1365 obj, get_verbose_code_parts(code, guard)
1368 self._produce_guard_code(guard, [code])
1370 def DUAL_LEVEL(self, guard: Guard): argument
1375 self._set_guard_export_info(guard, [code])
1377 # TODO(anijain2305) - Consider this moving this guard to C++
1385 fn, get_verbose_code_parts(code, guard)
1388 self._produce_guard_code(guard, code)
1390 def FUNCTORCH_STACK_MATCH(self, guard: Guard): argument
1396 self._set_guard_export_info(guard, code)
1399 # TODO(anijain2305) - Consider this moving this guard to C++
1407 fn, get_verbose_code_parts(code, guard)
1410 self._produce_guard_code(guard, code)
1412 def TENSOR_SUBCLASS_METADATA_MATCH(self, guard: Guard): argument
1413 value = self.get(guard.name)
1414 original_metadata = deepcopy(self.get(guard.name).__tensor_flatten__()[1])
1430 self.get_guard_manager(guard).add_lambda_guard(
1431 metadata_checker, get_verbose_code_parts(global_name, guard)
1436 code = [f"{global_name}({self.get(guard.name)})"]
1437 self._produce_guard_code(guard, code)
1439 def EQUALS_MATCH(self, guard: Guard): argument
1440 ref = self.arg_ref(guard)
1441 val = self.get(guard.name)
1503 self.TYPE_MATCH(guard)
1506 self._set_guard_export_info(guard, code)
1509 self.get_guard_manager(guard).add_lambda_guard(
1510 CLOSURE_VARS["__math_isnan"], get_verbose_code_parts(code, guard)
1513 self._produce_guard_code(guard, code)
1518 self.TYPE_MATCH(guard)
1521 self._set_guard_export_info(guard, code)
1524 self.get_guard_manager(guard).add_lambda_guard(
1525 CLOSURE_VARS["__numpy_isnan"], get_verbose_code_parts(code, guard)
1528 self._produce_guard_code(guard, code)
1532 # Construct a debug string to put into the c++ equals match guard.
1539 self.get_guard_manager(guard).add_equals_match_guard(
1540 val, get_verbose_code_parts(code, guard)
1542 self._set_guard_export_info(guard, code)
1551 self.SEQUENCE_LENGTH(guard)
1559 self.TYPE_MATCH(guard)
1572 self._produce_guard_code(guard, code)
1573 self._set_guard_export_info(guard, code)
1575 def CONSTANT_MATCH(self, guard: Guard): argument
1576 val = self.get(guard.name)
1578 self.ID_MATCH(guard)
1580 self.EQUALS_MATCH(guard)
1582 def NN_MODULE(self, guard: Guard): argument
1583 self.ID_MATCH(guard)
1584 val = self.get(guard.name)
1587 self._guard_on_attribute(guard, "training", GuardBuilder.CONSTANT_MATCH)
1589 exc.unimplemented(f"Guard setup for uninitialized class {type(val)}")
1591 def FUNCTION_MATCH(self, guard: Guard): argument
1593 return self.ID_MATCH(guard)
1595 def CLOSURE_MATCH(self, guard: Guard): argument
1597 val = self.get(guard.name)
1600 self._guard_on_attribute(guard, "__code__", GuardBuilder.HASATTR)
1601 self._guard_on_attribute(guard, "__code__", GuardBuilder.FUNCTION_MATCH)
1603 self.FUNCTION_MATCH(guard)
1605 def BUILTIN_MATCH(self, guard: Guard): argument
1606 return self.FUNCTION_MATCH(guard)
1608 def PYMODULE_MATCH(self, guard: Guard): argument
1609 return self.FUNCTION_MATCH(guard)
1611 def SEQUENCE_LENGTH(self, guard): argument
1612 # This guard is used to check lenght of PySequence objects like list,
1614 ref = self.arg_ref(guard)
1615 value = self.get(guard.name)
1620 self.TYPE_MATCH(guard)
1628 self._set_guard_export_info(guard, code)
1631 self.get_guard_manager(guard).add_dict_length_check_guard(
1632 len(value), get_verbose_code_parts(code, guard)
1635 self.get_guard_manager(guard).add_length_check_guard(
1636 len(value), get_verbose_code_parts(code, guard)
1639 self._produce_guard_code(guard, code)
1641 def TUPLE_ITERATOR_LEN(self, guard): argument
1642 ref = self.arg_ref(guard)
1643 value = self.get(guard.name)
1647 # C++ guard already checks the type
1648 self.TYPE_MATCH(guard)
1652 self._set_guard_export_info(guard, code)
1658 self.get_guard_manager(guard).add_tuple_iterator_length_guard(
1659 tuple_iterator_len(value), obj_id, get_verbose_code_parts(code, guard)
1662 self._produce_guard_code(guard, code)
1665 def DUPLICATE_INPUT(self, guard, source_b): argument
1666 ref_a = self.arg_ref(guard)
1670 guard.originating_source
1675 self._set_guard_export_info(guard, code)
1678 # Check that the guard has not been inserted already
1686 self.get_guard_manager(guard),
1688 get_verbose_code_parts(code, guard),
1691 self._produce_guard_code(guard, code)
1693 def DICT_KEYS(self, guard): argument
1694 # Guard on the keys and their order
1695 ref = self.arg_ref(guard)
1696 value = self.get(guard.name)
1699 self.TYPE_MATCH(guard)
1704 local=is_from_local_source(guard.originating_source),
1711 self._set_guard_export_info(guard, code)
1713 if self.requires_key_order_guarding(guard.originating_source):
1714 self.guard_on_dict_keys_and_order(value, guard)
1716 self.guard_on_dict_keys_and_ignore_order(value, guard)
1718 self._produce_guard_code(guard, code)
1720 def WEAKREF_ALIVE(self, guard): argument
1721 code = [f"{self.arg_ref(guard)} is not None"]
1723 self._set_guard_export_info(guard, code)
1725 self.get_guard_manager(guard).add_not_none_guard(
1726 get_verbose_code_parts(code, guard)
1729 self._produce_guard_code(guard, code)
1731 def DICT_CONST_KEYS(self, guard): argument
1733 ref = self.arg_ref(guard)
1734 value = self.get(guard.name)
1739 self.TYPE_MATCH(guard)
1743 self._set_guard_export_info(guard, code)
1746 if self.requires_key_order_guarding(guard.originating_source):
1747 self.guard_on_dict_keys_and_order(value, guard)
1749 self.guard_on_dict_keys_and_ignore_order(value, guard)
1751 self._produce_guard_code(guard, code)
1753 def EMPTY_NN_MODULE_HOOKS_DICT(self, guard): argument
1754 … """Special guard to skip guards on empty hooks. This is controlled by skip_nnmodule_hook_guards"""
1758 self.SEQUENCE_LENGTH(guard)
1760 def OBJECT_MUTATION(self, guard: Guard): argument
1761 mutation_guard.watch(self.get(guard.name), self.check_fn_manager)
1763 def GRAD_MODE(self, guard: Guard): argument
1764 pass # we always guard on this via GlobalStateGuard()
1766 def DETERMINISTIC_ALGORITHMS(self, guard: Guard): argument
1767 pass # we always guard on this via GlobalStateGuard()
1769 def TORCH_FUNCTION_STATE(self, guard: Guard): argument
1770 pass # we always guard on this via GlobalStateGuard()
1772 def FSDP_TRAINING_STATE(self, guard: Guard): argument
1773 pass # we always guard on this via GlobalStateGuard()
1775 def DEFAULT_DEVICE(self, guard: Guard): argument
1776 """Guard on CURRENT_DEVICE per torch.utils._device"""
1777 assert guard.source is GuardSource.GLOBAL
1781 self._set_guard_export_info(guard, code)
1784 self.get_guard_manager(guard).add_default_device_guard(
1785 get_verbose_code_parts(code, guard)
1788 self._produce_guard_code(guard, code)
1790 def SHAPE_ENV(self, guard: Guard): argument
1794 assert guard.name == ""
1851 self._set_guard_export_info(guard, [shape_guard])
1854 # Install all the symbolic guards in one lambda guard. These are run
1860 get_verbose_code_parts(code_parts, guard),
1865 self._produce_guard_code(guard, [shape_guard], shape_env=True)
1867 def TENSOR_MATCH(self, guard: Guard, value=None): argument
1870 if guard.is_fsdp_module():
1875 # will be lifted as inputs and have a TENSOR_MATCH guard.
1879 guard.is_specialized_nn_module()
1880 and not isinstance(guard.originating_source, NumpyTensorSource)
1881 ) or match_on_id_for_tensor(guard):
1882 self.ID_MATCH(guard)
1887 value = value if value is not None else self.get(guard.name)
1890 tensor_name = self.arg_ref(guard)
1914 self.TYPE_MATCH(guard)
1932 self.tensor_check_guards.append(guard)
1935 guard_manager = self.get_guard_manager(guard)
1936 # Keep track of all the tensor guard managers to insert
1942 guard.originating_source
1949 guard,
1987 assert guard.source is not None
1989 value, is_tensor=True, tensor_source=guard.originating_source
1998 self.get_guard_manager(guard).add_dynamic_indices_guard(
1999 dynamic_indices, get_verbose_code_parts(code_part, guard)
2009 self.get_guard_manager(guard).add_no_hasattr_guard(
2011 get_verbose_code_parts(code_part, guard),
2014 self._set_guard_export_info(guard, code)
2016 self._produce_guard_code(guard, code)
2019 def _produce_guard_code(self, guard, code_list, shape_env=False): argument
2022 self.shape_env_code.append(GuardCodeList(code_list, guard))
2024 self.code.append(GuardCodeList(code_list, guard))
2027 def _set_guard_export_info(self, guard, code_list, provided_guarded_object=None): argument
2045 name_valid = guard.name is not None and guard.name != ""
2047 guarded_object = self.get(guard.name) if name_valid else None
2062 guard.set_export_info(
2172 def must_add_nn_module_guards(guard): argument
2176 # Guard for defaults
2177 isinstance(guard.originating_source, DefaultsSource)
2178 # Guard using dict tags if the config flag is set
2181 and guard.create_fn is GuardBuilder.NN_MODULE
2191 # the callable that constitutes the guard. However, there is some
2249 for guard in sorted(guards or [], key=Guard.sort_key):
2252 and guard.is_specialized_nn_module()
2254 # TODO: we could make use of 'DefaultsSource' and offer a .guard.is_defaults() API
2255 and "__defaults__" not in guard.name
2256 and "__kwdefaults__" not in guard.name
2257 and (config.skip_nnmodule_hook_guards or "hooks" not in guard.name)
2261 guard.create(builder)
2265 # Keep track of weak references of objects with ID_MATCH guard. This
2285 # Check that the guard returns True. False means that we will always
2296 raise AssertionError(f"Guard check failed: {reasons}")
2327 # Insert the global_state guard
2339 # Don't report this guard, it's always the same, useless!
2348 def add_code_part(code_part, guard, log_only=False): argument
2349 verbose_code_part = get_verbose_code_part(code_part, guard)
2355 "stack": structured.from_traceback(guard.stack.summary())
2356 if guard.stack
2358 "user_stack": structured.from_traceback(guard.user_stack)
2359 if guard.user_stack
2367 if guard is not None:
2368 if guard.stack:
2369 maybe_stack = f"\nStack:\n{''.join(guard.stack.format())}"
2370 if guard.user_stack:
2372 f"\nUser stack:\n{''.join(guard.user_stack.format())}"
2375 "Guard: %s%s%s",
2389 # If Cpp guard manager is enabled, we don't need to add to
2391 add_code_part(code, gcl.guard, config.enable_cpp_guard_manager)
2439 # Install tensor aliasing guard. TENSOR_MATCH guards are already
2440 # installed for cpp guard manager.
2456 for guard in aotautograd_guards:
2457 if isinstance(guard, DuplicateInputs):
2458 source_a = guard.input_source_a
2459 source_b = guard.input_source_b
2469 raise RuntimeError(f"Unknown GuardEnvExpr: {guard}")
2471 # TODO: the "guard" here is actually just the top level SHAPE_ENV
2475 # Shape env guards are already added for CPP guard manager in
2477 add_code_part(code, gcl.guard, config.enable_cpp_guard_manager)
2501 # Guard manager construction is complete
2507 # Ensure we did not miss to insert a guard in cpp guard manager.
2527 log.exception("Failed to exec guard at line %s.\n%s", ex.lineno, pycode)
2532 … # TODO(whc) maybe '.code_parts' was only kept around for the guard callback? so we don't need both
2601 # Generate the inner body of the guard function.
2602 # i.e. if-chain of the guard expressions.
2611 # Wrap the inner body into the actual guard function.
2612 guard = IndentedBuffer()
2613 guard.writeline("def guard(L):")
2614 with guard.indent():
2615 guard.splice(guard_body)
2616 guard.writeline("return True")
2618 # Wrap the whole guard function into another function
2623 make_guard_fn.splice(guard)
2624 make_guard_fn.writeline("return guard")
2696 # C++ guard manager. We need to fix the issue to remove the comment.
2702 # passed to the leaf guard at construction time. If its a list, we
2703 # walk through this list and find the guard that failed. This is
2705 # installed as a lambda guard and can encompass a long list of code_parts.
2715 # This is not needed for CPP guard because the verbose check is already
2763 … "Failure in guard_fail_fn callback - raising here will cause a NULL Error on guard eval",
2773 Return the list of guard failure reasons using cache_entry.
2797 f"guard {i} failures:\n" + textwrap.indent(reason, "- ")
2803 f"triggered by the following guard failure(s):\n{failures}"
2846 for guard in guard_fn.code_parts:
2848 eval(guard, guard_fn.global_scope, local_scope)
2850 print(f"Malformed guard:\n{guard}")
2872 …# In order to ensure that we do not reuse fn(x, x) for fn(x, y), we create a duplicate input guard.
2888 …# reconcile guards builder scopes in compile_check_fn. This technically means we miss a guard here,
2890 # TODO(voz): Combine local and global guard builders.
2893 # However, this should always be a sound guard to add here.
2903 guards: guard(s) to add
2912 for guard in guards:
2913 assert isinstance(guard, Guard)
2914 add(guard, collect_debug_stack=collect_debug_stack, skip=skip + 1)