code: def matrix_inv_fallback_pinv(matrix: torch.Tensor) -> torch.Tensor:
      """
      if the matrix A or any matrix in the batch of matrices A is not invertible,
      raise a error message and then switch from `inv` to `pinv`
      https://pytorch.org/docs/stable/generated/torch.linalg.inv.html
      """
      try:
          inv_matrix = torch.linalg.inv(matrix).contiguous()
      except RuntimeError as e:
          logger.warning(
              "Exception raised during matrix inversion, falling back to pseudo-inverse",
              e,
          )
          # switch from `inv` to `pinv`
          # first check if matrix is Hermitian (symmetric matrix)
          matrix_is_hermitian = torch.allclose(
              matrix, matrix.T, atol=1e-4, rtol=1e-4
          )
          # applying hermitian=True saves about 50% computations
          inv_matrix = torch.linalg.pinv(
              matrix,
              hermitian=matrix_is_hermitian,
          ).contiguous()
      return inv_matrix
  
file_path: ReAgent/reagent/models/linear_regression.py
imports: import logging
  from typing import Dict, Optional
  import torch
  from pytorch_lightning.utilities.distributed import ReduceOp, sync_ddp_if_available
  from reagent.models.cb_base_model import UCBBaseModel
label_desc: Use image node_id 1 for calling a global function with example usage: matrix_inv_fallback_pinv(matrix) and returns: inv_matrix