code: def _load_state(
      cls: Union[
          Type["pl.LightningModule"], Type["pl.LightningDataModule"]
      ],
      checkpoint: Dict[str, Any],
      strict: Optional[bool] = None,
      **cls_kwargs_new: Any,
  ) -> Union["pl.LightningModule", "pl.LightningDataModule"]:
      cls_spec = inspect.getfullargspec(cls.__init__)
      cls_init_args_name = inspect.signature(
          cls.__init__
      ).parameters.keys()
  
      self_var, args_var, kwargs_var = parse_class_init_keys(cls)
      drop_names = [n for n in (self_var, args_var, kwargs_var) if n]
      cls_init_args_name = list(
          filter(lambda n: n not in drop_names, cls_init_args_name)
      )
  
      cls_kwargs_loaded = {}
      # pass in the values we saved automatically
      if cls.CHECKPOINT_HYPER_PARAMS_KEY in checkpoint:
          if issubclass(cls, pl.LightningModule):
              # TODO: make this a migration:
              # 1. (backward compatibility) Try to restore model hparams from checkpoint using old/past keys
              for _old_hparam_key in CHECKPOINT_PAST_HPARAMS_KEYS:
                  cls_kwargs_loaded.update(
                      checkpoint.get(_old_hparam_key, {})
                  )
  
          # 2. Try to restore model hparams from checkpoint using the new key
          cls_kwargs_loaded.update(
              checkpoint.get(cls.CHECKPOINT_HYPER_PARAMS_KEY, {})
          )
  
          # 3. Ensure that `cls_kwargs_old` has the right type, back compatibility between dict and Namespace
          cls_kwargs_loaded = _convert_loaded_hparams(
              cls_kwargs_loaded,
              checkpoint.get(cls.CHECKPOINT_HYPER_PARAMS_TYPE),
          )
  
          # 4. Update cls_kwargs_new with cls_kwargs_old, such that new has higher priority
          args_name = checkpoint.get(cls.CHECKPOINT_HYPER_PARAMS_NAME)
          if args_name and args_name in cls_init_args_name:
              cls_kwargs_loaded = {args_name: cls_kwargs_loaded}
  
      _cls_kwargs = {}
      _cls_kwargs.update(cls_kwargs_loaded)
      _cls_kwargs.update(cls_kwargs_new)
  
      if not cls_spec.varkw:
          # filter kwargs according to class init unless it allows any argument via kwargs
          _cls_kwargs = {
              k: v
              for k, v in _cls_kwargs.items()
              if k in cls_init_args_name
          }
  
      obj = cls(**_cls_kwargs)
  
      if isinstance(obj, pl.LightningModule):
          if is_overridden("configure_model", obj):
              obj.configure_model()
  
          # give model a chance to load something
          obj.on_load_checkpoint(checkpoint)
  
      if isinstance(obj, pl.LightningDataModule):
          if obj.__class__.__qualname__ in checkpoint:
              obj.load_state_dict(
                  checkpoint[obj.__class__.__qualname__]
              )
          return obj
  
      # load the state_dict on the model automatically
      assert strict is not None
      keys = obj.load_state_dict(
          checkpoint["state_dict"], strict=strict
      )
  
      if not strict:
          if keys.missing_keys:
              rank_zero_warn(
                  f"Found keys that are in the model state dict but not in the checkpoint: {keys.missing_keys}"
              )
          if keys.unexpected_keys:
              rank_zero_warn(
                  f"Found keys that are not in the model state dict but in the checkpoint: {keys.unexpected_keys}"
              )
  
      return obj
  
imports: import ast
  import contextlib
  import csv
  import inspect
  import logging
  import os
  from argparse import Namespace
  from copy import deepcopy
  from enum import Enum
  from pathlib import Path
  from typing import IO, TYPE_CHECKING, Any, Callable, Dict, Optional, Type, Union
  from warnings import warn
  import torch
  import yaml
  from lightning_utilities.core.apply_func import apply_to_collection
  import lightning.pytorch
  from lightning.fabric.utilities.cloud_io import _is_dir, get_filesystem
  from lightning.fabric.utilities.cloud_io import _load
  from lightning.fabric.utilities.data import AttributeDict
  from lightning.fabric.utilities.types import _MAP_LOCATION_TYPE, _PATH
  from lightning.pytorch.accelerators import CUDAAccelerator, MPSAccelerator, XLAAccelerator
  from lightning.pytorch.utilities.imports import _OMEGACONF_AVAILABLE
  from lightning.pytorch.utilities.migration import pl_legacy_patch
  from lightning.pytorch.utilities.migration.utils import _pl_migrate_checkpoint
  from lightning.pytorch.utilities.model_helpers import is_overridden
  from lightning.pytorch.utilities.parsing import parse_class_init_keys
  from lightning.pytorch.utilities.rank_zero import rank_zero_warn
label_desc: Use image node_id 3 for calling a global function with example usage: _load_state(cls, checkpoint, strict) and returns: obj, obj