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 to_bytes(self):
      config = {}
      tok_dict = {}
      weights_bytes = {}
      tok_cfg = {}
      trf_cfg = {}
      hf_model = self._hfmodel
      if hf_model.transformer is not None:
          tok_dict = {}
          config = hf_model.transformer.config.to_dict()
          tokenizer = hf_model.tokenizer
          with make_tempdir() as temp_dir:
              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, "wb") as fileh:
                      fileh.write(hf_model.vocab_file_contents)
                  tokenizer.vocab_file = vocab_file_path
              tok_dict["kwargs"] = {"use_fast": tokenizer.is_fast}
              tokenizer.save_pretrained(str(temp_dir.absolute()))
              for x in temp_dir.glob("**/*"):
                  if x.is_file():
                      tok_dict[x.name] = x.read_bytes()
          filelike = BytesIO()
          torch.save(self._model.state_dict(), filelike)
          filelike.seek(0)
          weights_bytes = filelike.getvalue()
      else:
          tok_cfg = hf_model._init_tokenizer_config
          trf_cfg = hf_model._init_transformer_config
      msg = {
          "config": config,
          "state": weights_bytes,
          "tokenizer": tok_dict,
          "_init_tokenizer_config": tok_cfg,
          "_init_transformer_config": trf_cfg,
      }
      return srsly.msgpack_dumps(msg)
  
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 2 for calling the HFShim obj's underlying member method code with example usage: obj.to_bytes() and returns: srsly