code: def sample_cached_cholesky(
      posterior: GPyTorchPosterior,
      baseline_L: Tensor,
      q: int,
      base_samples: Tensor,
      sample_shape: torch.Size,
      max_tries: int = 6,
  ) -> Tensor:
      r"""Get posterior samples at the `q` new points from the joint multi-output
      posterior.
  
      Args:
          posterior: The joint posterior is over (X_baseline, X).
          baseline_L: The baseline lower triangular cholesky factor.
          q: The number of new points in X.
          base_samples: The base samples.
          sample_shape: The sample shape.
          max_tries: The number of tries for computing the Cholesky
              decomposition with increasing jitter.
  
  
      Returns:
          A `sample_shape x batch_shape x q x m`-dim tensor of posterior
              samples at the new points.
      """
      # compute bottom left covariance block
      mvn = posterior.distribution
      lazy_covar = (
          extract_batch_covar(mt_mvn=mvn)
          if isinstance(mvn, MultitaskMultivariateNormal)
          else mvn.lazy_covariance_matrix
      )
      # Get the `q` new rows of the batched covariance matrix
      bottom_rows = lazy_covar[..., -q:, :].to_dense()
      # The covariance in block form is:
      # [K(X_baseline, X_baseline), K(X_baseline, X)]
      # [K(X, X_baseline), K(X, X)]
      # bl := K(X, X_baseline)
      # br := K(X, X)
      # Get bottom right block of new covariance
      bl, br = bottom_rows.split([bottom_rows.shape[-1] - q, q], dim=-1)
      # Solve Ax = b
      # where A = K(X_baseline, X_baseline) and b = K(X, X_baseline)^T
      # and bl_chol := x^T
      # bl_chol is the new `(batch_shape) x q x n`-dim bottom left block
      # of the cholesky decomposition
      bl_chol = torch.linalg.solve_triangular(
          baseline_L, bl.transpose(-2, -1), upper=False
      ).transpose(-2, -1)
  
      # Compute the new bottom right block of the Cholesky
      # decomposition via:
      # Cholesky(K(X, X) - bl_chol @ bl_chol^T)
      br_to_chol = br - bl_chol @ bl_chol.transpose(-2, -1)
      # TODO: technically we should make sure that we add a
      # consistent nugget to the cached covariance and the new block
      br_chol = psd_safe_cholesky(br_to_chol, max_tries=max_tries)
      # Create a `(batch_shape) x q x (n+q)`-dim tensor containing the
      # `q` new bottom rows of the Cholesky decomposition
      new_Lq = torch.cat([bl_chol, br_chol], dim=-1)
      mean = posterior.distribution.mean
      base_samples = _reshape_base_samples(
          base_samples=base_samples,
          sample_shape=sample_shape,
          posterior=posterior,
      )
      if not isinstance(
          posterior.distribution, MultitaskMultivariateNormal
      ):
          # add output dim
          mean = mean.unsqueeze(-1)
          # add batch dim corresponding to output dim
          new_Lq = new_Lq.unsqueeze(-3)
      new_mean = mean[..., -q:, :]
      res = (
          new_Lq.matmul(base_samples)
          .add(new_mean.transpose(-1, -2).unsqueeze(-1))
          .permute(
              -1, *range(posterior.distribution.loc.dim() - 1), -2, -3
          )
          .contiguous()
      )
      contains_nans = torch.isnan(res).any()
      contains_infs = torch.isinf(res).any()
      if contains_nans or contains_infs:
          suffix_args = []
          if contains_nans:
              suffix_args.append("nans")
          if contains_infs:
              suffix_args.append("infs")
          suffix = " and ".join(suffix_args)
          raise NanError(f"Samples contain {suffix}.")
      return res
  
file_path: botorch/botorch/utils/low_rank.py
imports: from __future__ import annotations
  import torch
  from botorch.exceptions.errors import BotorchError
  from botorch.posteriors.base_samples import _reshape_base_samples_non_interleaved
  from botorch.posteriors.gpytorch import GPyTorchPosterior
  from gpytorch.distributions.multitask_multivariate_normal import MultitaskMultivariateNormal
  from linear_operator.operators import BlockDiagLinearOperator, LinearOperator
  from linear_operator.utils.cholesky import psd_safe_cholesky
  from linear_operator.utils.errors import NanError
  from torch import Tensor
label_desc: Use image node_id 3 for calling a global function with example usage: sample_cached_cholesky(posterior, baseline_L, q, base_samples, sample_shape, max_tries) and returns: res