class_docstr: Serializer using cloudpickle.
class_docstr_tok: ["Serializer","using","cloudpickle","."]
docstr: Serializes data object and uploads it to S3.
  
  Args:
      obj: object to be serialized and persisted
  Raises:
      SerializationError: when fail to serialize object to bytes.
docstr_tok: ["Serializes","data","object","and","uploads","it","to","S3",".","Args",":","obj",":","object","to","be","serialized","and","persisted","Raises",":","SerializationError",":","when","fail","to","serialize","object","to","bytes","."]
code: def serialize(obj: Any) -> bytes:
      """Serializes data object and uploads it to S3.
  
      Args:
          obj: object to be serialized and persisted
      Raises:
          SerializationError: when fail to serialize object to bytes.
      """
      try:
          io_buffer = io.BytesIO()
          custom_pickler = cloudpickle.CloudPickler(io_buffer)
          dt = pickle.Pickler.dispatch_table.__get__(
              custom_pickler
          )  # pylint: disable=no-member
          new_dt = dt.new_child(dispatch_table)
          pickle.Pickler.dispatch_table.__set__(  # pylint: disable=no-member
              custom_pickler, new_dt
          )
          custom_pickler.dump(obj)
          return io_buffer.getvalue()
      except Exception as e:
          if isinstance(
              e, NotImplementedError
          ) and "Instance of Run type is not allowed to be pickled." in str(
              e
          ):
              raise SerializationError(
                  """You are trying to pass a sagemaker.experiments.run.Run object to
                         a remote function
                         or are trying to access a global sagemaker.experiments.run.Run object
                         from within the function. This is not supported.
                         You must use `load_run` to load an existing Run in the remote function
                         or instantiate a new Run in the function."""
              )
  
          raise SerializationError(
              "Error when serializing object of type [{}]: {}".format(
                  type(obj).__name__, repr(e)
              )
          ) from e
  
imports: from __future__ import absolute_import
  import dataclasses
  import json
  import io
  import sys
  import hmac
  import hashlib
  import pickle
  from typing import Any, Callable, Union
  import cloudpickle
  from tblib import pickling_support
  from sagemaker.remote_function.errors import ServiceError, SerializationError, DeserializationError
  from sagemaker.s3 import S3Downloader, S3Uploader
  from sagemaker.session import Session
  from ._custom_dispatch_table import dispatch_table
label_desc: Use image node_id 1 for calling the CloudpickleSerializer obj's underlying member method code with example usage: obj.serialize(obj) and returns: io_buffer