#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
// !!! This is a file automatically generated by hipify!!!
#pragma once

#include <c10/core/Allocator.h>
#include <c10/hip/HIPStream.h>

#include <iostream>
#include <optional>

// CUDA Graphs utils used by c10 and aten.
// aten/cuda/CUDAGraphsUtils.cuh adds utils used by aten only.

namespace c10::cuda {

// RAII guard for "hipStreamCaptureMode", a thread-local value
// that controls the error-checking strictness of a capture.
struct C10_CUDA_API CUDAStreamCaptureModeGuard {
  CUDAStreamCaptureModeGuard(hipStreamCaptureMode desired)
      : strictness_(desired) {
    C10_CUDA_CHECK(hipThreadExchangeStreamCaptureMode(&strictness_));
  }
  CUDAStreamCaptureModeGuard(const CUDAStreamCaptureModeGuard&) = delete;
  CUDAStreamCaptureModeGuard(CUDAStreamCaptureModeGuard&&) = delete;
  CUDAStreamCaptureModeGuard& operator=(const CUDAStreamCaptureModeGuard&) =
      delete;
  CUDAStreamCaptureModeGuard& operator=(CUDAStreamCaptureModeGuard&&) = delete;
  ~CUDAStreamCaptureModeGuard() {
    C10_CUDA_CHECK_WARN(hipThreadExchangeStreamCaptureMode(&strictness_));
  }

 private:
  hipStreamCaptureMode strictness_;
};

// Protects against enum hipStreamCaptureStatus implementation changes.
// Some compilers seem not to like static_assert without the messages.
static_assert(
    int(hipStreamCaptureStatus::hipStreamCaptureStatusNone) == 0,
    "unexpected int(hipStreamCaptureStatusNone) value");
static_assert(
    int(hipStreamCaptureStatus::hipStreamCaptureStatusActive) == 1,
    "unexpected int(hipStreamCaptureStatusActive) value");
static_assert(
    int(hipStreamCaptureStatus::hipStreamCaptureStatusInvalidated) == 2,
    "unexpected int(hipStreamCaptureStatusInvalidated) value");

enum class CaptureStatus : int {
  None = int(hipStreamCaptureStatus::hipStreamCaptureStatusNone),
  Active = int(hipStreamCaptureStatus::hipStreamCaptureStatusActive),
  Invalidated = int(hipStreamCaptureStatus::hipStreamCaptureStatusInvalidated)
};

inline std::ostream& operator<<(std::ostream& os, CaptureStatus status) {
  switch (status) {
    case CaptureStatus::None:
      os << "hipStreamCaptureStatusNone";
      break;
    case CaptureStatus::Active:
      os << "hipStreamCaptureStatusActive";
      break;
    case CaptureStatus::Invalidated:
      os << "hipStreamCaptureStatusInvalidated";
      break;
    default:
      TORCH_INTERNAL_ASSERT(
          false, "Unknown CUDA graph CaptureStatus", int(status));
  }
  return os;
}

// Use this version where you're sure a CUDA context exists already.
inline CaptureStatus currentStreamCaptureStatusMayInitCtx() {
  hipStreamCaptureStatus status{hipStreamCaptureStatusNone};
  C10_CUDA_CHECK(
      hipStreamIsCapturing(c10::cuda::getCurrentCUDAStream(), &status));
  return CaptureStatus(status);
}

inline CaptureStatus captureStatusMayInitCtx(hipStream_t stream) {
  hipStreamCaptureStatus status{hipStreamCaptureStatusNone};
  C10_CUDA_CHECK(hipStreamIsCapturing(stream, &status));
  return CaptureStatus(status);
}

inline bool isStreamCapturingMayInitCtx(hipStream_t stream) {
  return captureStatusMayInitCtx(stream) == CaptureStatus::Active;
}

inline std::optional<CaptureId_t> currentStreamCaptureIdMayInitCtx() {
  hipStreamCaptureStatus status{};
  CaptureId_t capture_id = 0;
  C10_CUDA_CHECK(hipStreamGetCaptureInfo(
      c10::cuda::getCurrentCUDAStream(), &status, &capture_id));
  if (status == hipStreamCaptureStatus::hipStreamCaptureStatusActive) {
    return capture_id;
  }
  return std::nullopt;
}

inline std::optional<CaptureId_t> captureIdMayInitCtx(hipStream_t stream) {
  hipStreamCaptureStatus status{};
  CaptureId_t capture_id = 0;
  C10_CUDA_CHECK(hipStreamGetCaptureInfo(stream, &status, &capture_id));
  if (status == hipStreamCaptureStatus::hipStreamCaptureStatusActive) {
    return capture_id;
  }
  return std::nullopt;
}

} // namespace c10::cuda

#else
#error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
#endif  // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
