class_docstr: Interface between a HF Pytorch model and a Thinc Model.
class_docstr_tok: ["Interface","between","a","HF","Pytorch","model","and","a","Thinc","Model","."]
code: def from_bytes(self, bytes_data):
      msg = srsly.msgpack_loads(bytes_data)
      config_dict = msg["config"]
      tok_dict = msg["tokenizer"]
      if config_dict:
          with make_tempdir() as temp_dir:
              config_file = temp_dir / "config.json"
              srsly.write_json(config_file, config_dict)
              config = self.config_cls.from_pretrained(config_file)
              tok_kwargs = tok_dict.pop("kwargs", {})
              for x, x_bytes in tok_dict.items():
                  Path(temp_dir / x).write_bytes(x_bytes)
              tokenizer = self.tokenizer_cls.from_pretrained(
                  str(temp_dir.absolute()), **tok_kwargs
              )
              vocab_file_contents = None
              if hasattr(tokenizer, "vocab_file"):
                  vocab_file_name = tokenizer.vocab_files_names[
                      "vocab_file"
                  ]
                  vocab_file_path = str(
                      (temp_dir / vocab_file_name).absolute()
                  )
                  with open(vocab_file_path, "rb") as fileh:
                      vocab_file_contents = fileh.read()
  
          transformer = self.model_cls.from_config(config)
          self._hfmodel = HFObjects(
              tokenizer,
              transformer,
              vocab_file_contents,
              SimpleFrozenDict(),
              SimpleFrozenDict(),
          )
          self._model = transformer
          filelike = BytesIO(msg["state"])
          filelike.seek(0)
          device = get_torch_default_device()
          try:
              self._model.load_state_dict(
                  torch.load(filelike, map_location=device)
              )
          except RuntimeError:
              warn_msg = (
                  "Error loading saved torch state_dict with strict=True, "
                  "likely due to differences between 'transformers' "
                  "versions. Attempting to load with strict=False as a "
                  "fallback...\n\n"
                  "If you see errors or degraded performance, download a "
                  "newer compatible model or retrain your custom model with "
                  "the current 'transformers' and 'spacy-transformers' "
                  "versions. For more details and available updates, run: "
                  "python -m spacy validate"
              )
              warnings.warn(warn_msg)
              filelike.seek(0)
              b = torch.load(filelike, map_location=device)
              self._model.load_state_dict(b, strict=False)
          self._model.to(device)
      else:
          self._hfmodel = HFObjects(
              None,
              None,
              None,
              msg["_init_tokenizer_config"],
              msg["_init_transformer_config"],
          )
      return self
  
file_path: spacy-transformers/spacy_transformers/layers/hf_shim.py
imports: from typing import Any, Dict
  from io import BytesIO
  from pathlib import Path
  import srsly
  import torch
  import warnings
  from thinc.api import get_torch_default_device
  from spacy.util import SimpleFrozenDict
  from ..data_classes import HFObjects
  from ..util import make_tempdir
  from thinc.api import PyTorchGradScaler, PyTorchShim
  from transformers import AutoModel, AutoConfig, AutoTokenizer
label_desc: Use image node_id 3 for calling the HFShim obj's underlying member method code with example usage: obj.from_bytes(bytes_data) and returns: self