Repository: huggingface/transformers
File: src/transformers/utils/generic.py (Python)

Code before:
                        if getattr(self, "gradient_checkpointing", False) and self.training and arg_value:
                            logger.warning_once(
                                "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`."
                            )
                            arg_value = False
                    elif arg_name == "vision_feature_select_strategy":
                        valid_strategies = ["default", "full"]
                        if arg_value not in valid_strategies:
                            raise ValueError(
                                f"`Unexpected select feature strategy: {arg_value}. "
                                f"Please select from {valid_strategies}."
                            )

                    if arg_index is not None and len(args) > arg_index:
                        args = list(args)
                        args[arg_index] = arg_value
                        args = tuple(args)
                    else:
                        kwargs[arg_name] = arg_value

            return_dict = kwargs.pop("return_dict", None)
            if return_dict is None:
                return_dict = getattr(self.config, "return_dict", True)

            # Maybe temporarily overwrite config value to create the correct mask - kwarg takes precedence
            is_causal = kwargs.get("is_causal", True) and getattr(self.config, "is_causal", True)
            if not is_causal:
                is_causal_in_config = hasattr(self.config, "is_causal")
                if is_causal_in_config:
                    is_causal_original_value = self.config.is_causal
                # Set it to both config and kwargs (it's needed in both, and can come from only 1 of the sources)
                self.config.is_causal = False
                kwargs["is_causal"] = False

            all_args = kwargs.copy()
            if "kwargs" in all_args:
                for k, v in all_args["kwargs"].items():
                    all_args[k] = v

            # _can_record_outputs is None by default
            capture_flags = _CAN_RECORD_REGISTRY.get(str(self.__class__)) or {}  # there is a weak ref for executorch
            recordable_keys = {
                f"output_{k}": all_args.get(
                    f"output_{k}",
                    getattr(
                        self.config,
                        f"output_{k}",
                        all_args.get("output_attentions", getattr(self.config, "output_attentions", False)),
                    ),
                )
                for k in capture_flags

Review comment (suggestion):
If we default to true, it will break all encoder and encoder-decoder models. 

Imo, we should properly add an `is_causal` flag to all models where it's obvious and default to None, i.e. don't do anything

Code after:
                        if getattr(self, "gradient_checkpointing", False) and self.training and arg_value:
                            logger.warning_once(
                                "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`."
                            )
                            arg_value = False
                    elif arg_name == "vision_feature_select_strategy":
                        valid_strategies = ["default", "full"]
                        if arg_value not in valid_strategies:
                            raise ValueError(
                                f"`Unexpected select feature strategy: {arg_value}. "
                                f"Please select from {valid_strategies}."
                            )

                    if arg_index is not None and len(args) > arg_index:
                        args = list(args)
                        args[arg_index] = arg_value
                        args = tuple(args)
                    else:
                        kwargs[arg_name] = arg_value

            return_dict = kwargs.pop("return_dict", None)
            if return_dict is None:
                return_dict = getattr(self.config, "return_dict", True)

            # Maybe temporarily overwrite config value to create the correct mask - kwarg takes precedence
            is_causal = kwargs.get("is_causal")
            if is_causal is None:
                is_causal = getattr(self.config, "is_causal", None)

            if is_causal is not None:
                is_causal_in_config = hasattr(self.config, "is_causal")
                if is_causal_in_config:
                    is_causal_original_value = self.config.is_causal
                # Set it to both config and kwargs (it's needed in both, and can come from only 1 of the sources)
                self.config.is_causal = is_causal
                kwargs["is_causal"] = is_causal

            all_args = kwargs.copy()
            if "kwargs" in all_args:
                for k, v in all_args["kwargs"].items():
                    all_args[k] = v

            # _can_record_outputs is None by default
            capture_flags = _CAN_RECORD_REGISTRY.get(str(self.__class__)) or {}  # there is a weak ref for executorch
            recordable_keys = {
                f"output_{k}": all_args.get(
                    f"output_{k}",
                    getattr(
                        self.config,
                        f"output_{k}",
                        all_args.get("output_attentions", getattr(self.config, "output_attentions", False)),