LukeForeverYoung / UReader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: Object of type method is not JSON serializable

XumingYE opened this issue · comments

When I inference this model based on the code in Hugging Face. It shows that TypeError: Object of type method is not JSON serializable
The whole error output is as the follows:

[5](vscode-notebook-cell://ssh-remote%2B10.122.7.49/mnt/raid5/sum/chenlin/FITE/mPLUG-Owl/infer.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4) from PIL import Image
      [7](vscode-notebook-cell://ssh-remote%2B10.122.7.49/mnt/raid5/sum/chenlin/FITE/mPLUG-Owl/infer.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=6) pretrained_ckpt = '/mnt/raid5/sum/MSMO/original/models/mplug-owl-llama-7b'
----> [8](vscode-notebook-cell://ssh-remote%2B10.122.7.49/mnt/raid5/sum/chenlin/FITE/mPLUG-Owl/infer.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7) model = MplugOwlForConditionalGeneration.from_pretrained(
      [9](vscode-notebook-cell://ssh-remote%2B10.122.7.49/mnt/raid5/sum/chenlin/FITE/mPLUG-Owl/infer.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=8)     pretrained_ckpt,
     [10](vscode-notebook-cell://ssh-remote%2B10.122.7.49/mnt/raid5/sum/chenlin/FITE/mPLUG-Owl/infer.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=9)     torch_dtype=torch.bfloat16,
     [11](vscode-notebook-cell://ssh-remote%2B10.122.7.49/mnt/raid5/sum/chenlin/FITE/mPLUG-Owl/infer.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=10) )
     [12](vscode-notebook-cell://ssh-remote%2B10.122.7.49/mnt/raid5/sum/chenlin/FITE/mPLUG-Owl/infer.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=11) image_processor = MplugOwlImageProcessor.from_pretrained(pretrained_ckpt)
     [13](vscode-notebook-cell://ssh-remote%2B10.122.7.49/mnt/raid5/sum/chenlin/FITE/mPLUG-Owl/infer.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=12) tokenizer = MplugOwlTokenizer.from_pretrained(pretrained_ckpt)

File [~/anaconda3/envs/sent_train/lib/python3.8/site-packages/transformers/modeling_utils.py:2325](https://vscode-remote+ssh-002dremote-002b10-002e122-002e7-002e49.vscode-resource.vscode-cdn.net/mnt/raid5/sum/chenlin/FITE/mPLUG-Owl/~/anaconda3/envs/sent_train/lib/python3.8/site-packages/transformers/modeling_utils.py:2325), in PreTrainedModel.from_pretrained(cls, pretrained_model_name_or_path, config, cache_dir, ignore_mismatched_sizes, force_download, local_files_only, token, revision, use_safetensors, *model_args, **kwargs)
   2323 if not isinstance(config, PretrainedConfig):
   2324     config_path = config if config is not None else pretrained_model_name_or_path
-> 2325     config, model_kwargs = cls.config_class.from_pretrained(
   2326         config_path,
   2327         cache_dir=cache_dir,
   2328         return_unused_kwargs=True,
   2329         force_download=force_download,
   2330         resume_download=resume_download,
   2331         proxies=proxies,
   2332         local_files_only=local_files_only,
   2333         token=token,
   2334         revision=revision,
...
    178     """
--> 179     raise TypeError(f'Object of type {o.__class__.__name__} '
    180                     f'is not JSON serializable')

TypeError: Object of type method is not JSON serializable

I use the instruction on the hugging face.
Why did this happen? And Are there any other ways to infer this model? Thanks a lot.

I had this problem, this is how I fixed it. In configuration_mplug_owl.py add the following method to the MPlugOwlConfig class:

    def to_diff_dict(self) -> Dict[str, Any]:
        """
        Removes all attributes from config which correspond to the default config attributes for better readability and
        serializes to a Python dictionary.

        Returns:
            `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance,
        """
        config_dict = self.to_dict()

        # get the default config dict
        default_config_dict = PretrainedConfig().to_dict()

        # get class specific config dict
        class_config_dict = self.__class__().to_dict() if not self.is_composition else {}

        serializable_config_dict = {}

        # only serialize values that differ from the default config
        for key, value in config_dict.items():
            if (
                isinstance(getattr(self, key, None), PretrainedConfig)
                and key in class_config_dict
                and isinstance(class_config_dict[key], dict)
            ):
                # For nested configs we need to clean the diff recursively
                diff = recursive_diff_dict(value, class_config_dict[key], config_obj=getattr(self, key, None))
                if "model_type" in value:
                    # Needs to be set even if it's not in the diff
                    diff["model_type"] = value["model_type"]
                if len(diff) > 0:
                    serializable_config_dict[key] = diff
            elif callable(value):
                # We ignore function parameters
                continue
            elif (
                key not in default_config_dict
                or key == "transformers_version"
                or value != default_config_dict[key]
                or (key in class_config_dict and value != class_config_dict[key])
            ):
                serializable_config_dict[key] = value

        if hasattr(self, "quantization_config"):
            serializable_config_dict["quantization_config"] = (
                self.quantization_config.to_dict()
                if not isinstance(self.quantization_config, dict)
                else self.quantization_config
            )

        self.dict_torch_dtype_to_str(serializable_config_dict)

        if "_flash_attn_2_enabled" in serializable_config_dict:
            del serializable_config_dict["_flash_attn_2_enabled"]

        return serializable_config_dict

I had this problem, this is how I fixed it. In configuration_mplug_owl.py add the following method to the MPlugOwlConfig class:

    def to_diff_dict(self) -> Dict[str, Any]:
        """
        Removes all attributes from config which correspond to the default config attributes for better readability and
        serializes to a Python dictionary.

        Returns:
            `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance,
        """
        config_dict = self.to_dict()

        # get the default config dict
        default_config_dict = PretrainedConfig().to_dict()

        # get class specific config dict
        class_config_dict = self.__class__().to_dict() if not self.is_composition else {}

        serializable_config_dict = {}

        # only serialize values that differ from the default config
        for key, value in config_dict.items():
            if (
                isinstance(getattr(self, key, None), PretrainedConfig)
                and key in class_config_dict
                and isinstance(class_config_dict[key], dict)
            ):
                # For nested configs we need to clean the diff recursively
                diff = recursive_diff_dict(value, class_config_dict[key], config_obj=getattr(self, key, None))
                if "model_type" in value:
                    # Needs to be set even if it's not in the diff
                    diff["model_type"] = value["model_type"]
                if len(diff) > 0:
                    serializable_config_dict[key] = diff
            elif callable(value):
                # We ignore function parameters
                continue
            elif (
                key not in default_config_dict
                or key == "transformers_version"
                or value != default_config_dict[key]
                or (key in class_config_dict and value != class_config_dict[key])
            ):
                serializable_config_dict[key] = value

        if hasattr(self, "quantization_config"):
            serializable_config_dict["quantization_config"] = (
                self.quantization_config.to_dict()
                if not isinstance(self.quantization_config, dict)
                else self.quantization_config
            )

        self.dict_torch_dtype_to_str(serializable_config_dict)

        if "_flash_attn_2_enabled" in serializable_config_dict:
            del serializable_config_dict["_flash_attn_2_enabled"]

        return serializable_config_dict

where is recursive_diff_dict function?

The reason for this error is that your transformers package is not the 4.29.1 version used by the author.
try:
pip uninstall transformers pip install transformers==4.29.1

The method given by the friend above is equivalent to a patch for transformers, so it is better to use the correct version as much as possible.

As for the ‘’recursive_diff_dict‘’ function I also give it here to help people who want to use the patch method:

def recursive_diff_dict(self, dict_a, dict_b, config_obj=None): """ Helper function to recursively take the diff between two nested dictionaries. The resulting diff only contains the values fromdict_athat are different from values indict_b. """ diff = {} default = config_obj.__class__().to_dict() if config_obj is not None else {} for key, value in dict_a.items(): obj_value = getattr(config_obj, str(key), None) if isinstance(obj_value, PretrainedConfig) and key in dict_b and isinstance(dict_b[key], dict): diff_value = self.recursive_diff_dict(value, dict_b[key], config_obj=obj_value) if len(diff_value) > 0: diff[key] = diff_value elif key not in dict_b or value != dict_b[key] or key not in default or value != default[key]: diff[key] = value return diff

Likewise, you need to put this function in class MplugOwlConfig and, where the function is used, add "self." before the function name.