class_docstr: A :class:`~ray.train.Checkpoint` with Torch-specific functionality.
class_docstr_tok: ["A",":","class",":","`","~ray.train.Checkpoint","`","with","Torch-specific","functionality","."]
docstr: Create a :class:`~ray.train.Checkpoint` that stores a Torch model.
  
  .. note::
  
      PyTorch recommends storing state dictionaries. To create a
      :class:`TorchCheckpoint` from a state dictionary, call
      :meth:`~ray.train.torch.TorchCheckpoint.from_state_dict`. To learn more
      about state dictionaries, read
      `Saving and Loading Models <https://pytorch.org/tutorials/beginner/saving_loading_models.html#what-is-a-state-dict>`_. # noqa: E501
  
  Args:
      model: The Torch model to store in the checkpoint.
      preprocessor: A fitted preprocessor to be applied before inference.
  
  Returns:
      A :class:`TorchCheckpoint` containing the specified model.
  
  Examples:
  
      .. testcode::
  
          from ray.train.torch import TorchCheckpoint
          import torch
  
          # Create model identity and send a random tensor to it
          model = torch.nn.Identity()
          input = torch.randn(2, 2)
          output = model(input)
  
          # Create a checkpoint
          checkpoint = TorchCheckpoint.from_model(model)
          print(checkpoint)
  
      .. testoutput::
          :hide:
  
          ...
docstr_tok: ["Create","a",":","class",":","`","~ray.train.Checkpoint","`","that","stores","a","Torch","model",".","..","note",":",":","PyTorch","recommends","storing","state","dictionaries",".","To","create","a",":","class",":","`","TorchCheckpoint","`","from","a","state","dictionary",",","call",":","meth",":","`","~ray.train.torch.TorchCheckpoint.from_state_dict","`",".","To","learn","more","about","state","dictionaries",",","read","`","Saving","and","Loading","Models","<","https",":","\/\/pytorch.org\/tutorials\/beginner\/saving_loading_models.html","#","what-is-a-state-dict",">","`","_",".","#","noqa",":","E501","Args",":","model",":","The","Torch","model","to","store","in","the","checkpoint",".","preprocessor",":","A","fitted","preprocessor","to","be","applied","before","inference",".","Returns",":","A",":","class",":","`","TorchCheckpoint","`","containing","the","specified","model",".","Examples",":","..","testcode",":",":","from","ray.train.torch","import","TorchCheckpoint","import","torch","#","Create","model","identity","and","send","a","random","tensor","to","it","model","=","torch.nn.Identity","(",")","input","=","torch.randn","(","2",",","2",")","output","=","model","(","input",")","#","Create","a","checkpoint","checkpoint","=","TorchCheckpoint.from_model","(","model",")","print","(","checkpoint",")","..","testoutput",":",":",":","hide",":","..."]
code: def from_model(
      cls,
      model: torch.nn.Module,
      *,
      preprocessor: Optional["Preprocessor"] = None,
  ) -> "TorchCheckpoint":
      """Create a :class:`~ray.train.Checkpoint` that stores a Torch model.
  
      .. note::
  
          PyTorch recommends storing state dictionaries. To create a
          :class:`TorchCheckpoint` from a state dictionary, call
          :meth:`~ray.train.torch.TorchCheckpoint.from_state_dict`. To learn more
          about state dictionaries, read
          `Saving and Loading Models <https://pytorch.org/tutorials/beginner/saving_loading_models.html#what-is-a-state-dict>`_. # noqa: E501
  
      Args:
          model: The Torch model to store in the checkpoint.
          preprocessor: A fitted preprocessor to be applied before inference.
  
      Returns:
          A :class:`TorchCheckpoint` containing the specified model.
  
      Examples:
  
          .. testcode::
  
              from ray.train.torch import TorchCheckpoint
              import torch
  
              # Create model identity and send a random tensor to it
              model = torch.nn.Identity()
              input = torch.randn(2, 2)
              output = model(input)
  
              # Create a checkpoint
              checkpoint = TorchCheckpoint.from_model(model)
              print(checkpoint)
  
          .. testoutput::
              :hide:
  
              ...
      """
      tempdir = tempfile.mkdtemp()
  
      model_path = os.path.join(tempdir, cls.MODEL_FILENAME)
      torch.save(model, model_path)
  
      checkpoint = cls.from_directory(tempdir)
      if preprocessor:
          checkpoint.set_preprocessor(preprocessor)
      return checkpoint
  
file_path: ray/python/ray/train/torch/torch_checkpoint.py
imports: import os
  import tempfile
  import warnings
  from typing import TYPE_CHECKING, Any, Dict, Optional
  import torch
  from ray.air._internal.torch_utils import consume_prefix_in_state_dict_if_present_not_in_place, load_torch_model
  from ray.train._internal.framework_checkpoint import FrameworkCheckpoint
  from ray.util.annotations import PublicAPI
label_desc: Use image node_id 2 for calling the TorchCheckpoint obj's underlying member method code with example usage: obj.from_model(cls, model) and returns: checkpoint