#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <functional>
#include <future>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
#include <vector>

#include <ATen/record_function.h>
#include <torch/csrc/inductor/aoti_package/model_package_loader.h>
#include <torch/csrc/inductor/aoti_runner/model_container_observer.h>
#include <torch/csrc/inductor/aoti_runner/model_container_runner_cpu.h>
#if defined(USE_CUDA)
#include <c10/cuda/CUDACachingAllocator.h>
#include <cuda_runtime.h>
#endif
#if defined(USE_CUDA) || defined(USE_ROCM)
#include <c10/cuda/CUDAGuard.h>
#include <c10/cuda/CUDAStream.h>
#include <torch/csrc/inductor/aoti_runner/model_container_runner_cuda.h>
#endif
#include <torch/script.h>
#include <torch/torch.h>

#define STR_VALUE(x) #x
#define STRINGIZE(x) STR_VALUE(x)

namespace {

// Function to check if test data files exist and are valid
bool testDataFilesExist() {
  std::string bindir = STRINGIZE(CMAKE_CURRENT_BINARY_DIR);
  std::vector<std::string> required_files = {
      "data.pt", "script_data.pt", "script_model_cpu.pt"};
  // compile_model.py only writes CUDA data when a GPU is visible.
  if (torch::cuda::is_available()) {
    required_files.emplace_back("script_model_cuda.pt");
  }

  for (const auto& filename : required_files) {
    std::string filepath = bindir + "/" + filename;
    std::ifstream file(filepath);
    if (!file.good()) {
      return false;
    }
  }
  return true;
}

// Function to ensure test data files are generated at runtime
void ensureTestDataGenerated() {
  static std::once_flag generated_flag;
  std::call_once(generated_flag, []() {
    // Only generate if files don't exist or are placeholders
    if (testDataFilesExist()) {
      return;
    }

    std::string bindir = STRINGIZE(CMAKE_CURRENT_BINARY_DIR);
    // The build directory is not shipped to CI test jobs, so create it here.
    std::filesystem::create_directories(bindir);

    // Calculate path to source directory: build/test_aoti_inference -> build ->
    // pytorch
    std::string pytorch_root = bindir.substr(0, bindir.find_last_of("/"));
    pytorch_root = pytorch_root.substr(0, pytorch_root.find_last_of("/"));
    std::string source_dir = pytorch_root + "/test/cpp/aoti_inference";

    // Generate test data files (data.pt, etc.) by running test.py directly
    std::string test_script = source_dir + "/test.py";
    std::string test_data_cmd =
        "cd \"" + bindir + "\" && python \"" + test_script + "\"";
    std::cout << "Generating test data: " << test_data_cmd << std::endl;
    int result1 = std::system(test_data_cmd.c_str());
    TORCH_CHECK(
        result1 == 0,
        "Test data generation failed with status ",
        result1,
        ": ",
        test_data_cmd);

    // Generate model files (script_*.pt) by running compile_model.py directly
    std::string compile_script = source_dir + "/compile_model.py";
    std::string models_cmd = "cd \"" + bindir + "\" && python \"" +
        compile_script + "\" \"" + AOTI_CUSTOM_CLASS_LIB + "\"";
    std::cout << "Generating model files: " << models_cmd << std::endl;
    int result2 = std::system(models_cmd.c_str());
    TORCH_CHECK(
        result2 == 0,
        "Model generation failed with status ",
        result2,
        ": ",
        models_cmd);
  });
}

const std::unordered_map<std::string, at::Tensor> derefTensorConstantMap(
    torch::inductor::TensorConstantMap tensor_constant_map) {
  std::unordered_map<std::string, at::Tensor> ret;
  for (const auto& pair : tensor_constant_map) {
    ret.emplace(pair.first, *(pair.second));
  }
  return ret;
}

bool compareConstantMap(
    const std::unordered_map<std::string, at::Tensor>& lhs,
    const std::unordered_map<std::string, at::Tensor>& rhs) {
  if (lhs.size() != rhs.size()) {
    return false;
  }

  for (const auto& pair : lhs) {
    auto it = rhs.find(pair.first);
    if (it == rhs.end() || !torch::allclose(pair.second, it->second)) {
      return false;
    }
  }
  return true;
}

void test_aoti(const std::string& device, bool use_runtime_constant_folding) {
  torch::NoGradGuard no_grad;

  std::string data_path =
      (std::filesystem::path(STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "data.pt")
           .string();
  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  std::string suffix = use_runtime_constant_folding
      ? device + "_use_runtime_constant_folding"
      : device;
  std::string path_attr = "model_so_path_" + suffix;
  std::string inputs_attr = "inputs_" + suffix;
  std::string outputs_attr = "outputs_" + suffix;
  const auto& model_so_path = data_loader.attr(path_attr.c_str()).toStringRef();
  const auto& ref_output_tensors =
      data_loader.attr(outputs_attr.c_str()).toTensorList().vec();

  std::unique_ptr<torch::inductor::AOTIModelContainerRunner> runner;
  if (device == "cpu") {
    runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCpu>(
        model_so_path);
#if defined(USE_CUDA) || defined(USE_ROCM)
  } else if (device == "cuda") {
    runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCuda>(
        model_so_path);
#endif
  } else {
    FAIL() << "unsupported device: " << device;
  }
  auto actual_output_tensors =
      runner->run(data_loader.attr(inputs_attr.c_str()).toTensorList().vec());
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));
}

void test_aoti_script(const std::string& device) {
  torch::NoGradGuard no_grad;

  std::string script_model = "script_model_" + device + ".pt";
  std::string model_path =
      (std::filesystem::path(
           STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / script_model.c_str())
           .string();
  torch::jit::script::Module model = torch::jit::load(model_path);

  std::string sample_data_path =
      (std::filesystem::path(
           STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "script_data.pt")
           .string();
  torch::jit::script::Module sample_data = torch::jit::load(sample_data_path);
  std::string inputs_attr = "inputs_" + device;
  std::string outputs_attr = "outputs_" + device;
  const auto& inputs = sample_data.attr(inputs_attr.c_str()).toList().vec();
  const auto& ref_output_tensors =
      sample_data.attr(outputs_attr.c_str()).toTensorVector();
  auto outputs = model.forward(inputs).toTuple()->elements();
  ASSERT_EQ(outputs.size(), ref_output_tensors.size());
  for (size_t i = 0; i < ref_output_tensors.size(); i++) {
    ASSERT_TRUE(torch::allclose(outputs[i].toTensor(), ref_output_tensors[i]));
  }
}

void test_aoti_package_loader(
    const std::string& device,
    bool use_runtime_constant_folding) {
  torch::NoGradGuard no_grad;

  std::string data_path =
      (std::filesystem::path(STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "data.pt")
           .string();
  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  std::string suffix = use_runtime_constant_folding
      ? device + "_use_runtime_constant_folding"
      : device;
  std::string path_attr = "pt2_package_path_" + suffix;
  std::string inputs_attr = "inputs_" + suffix;
  std::string outputs_attr = "outputs_" + suffix;
  const auto& pt2_package_path =
      data_loader.attr(path_attr.c_str()).toStringRef();
  const auto& ref_output_tensors =
      data_loader.attr(outputs_attr.c_str()).toTensorList().vec();

  torch::inductor::AOTIModelPackageLoader runner(pt2_package_path);
  auto actual_output_tensors =
      runner.run(data_loader.attr(inputs_attr.c_str()).toTensorList().vec());
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));
}

void test_aoti_package_loader_multi_gpu(
    const std::string& device,
    bool use_runtime_constant_folding) {
  torch::NoGradGuard no_grad;
  // Ensure that this test will reset the default CUDA device on exit.
  torch::DeviceGuard device_guard(c10::Device("cuda"));

  std::string data_path =
      (std::filesystem::path(STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "data.pt")
           .string();
  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  std::string suffix = use_runtime_constant_folding
      ? device + "_use_runtime_constant_folding"
      : device;
  std::string path_attr = "pt2_package_path_" + suffix;
  std::string inputs_attr = "inputs_" + suffix;
  std::string outputs_attr = "outputs_" + suffix;
  const auto& pt2_package_path =
      data_loader.attr(path_attr.c_str()).toStringRef();
  const auto& ref_output_tensors =
      data_loader.attr(outputs_attr.c_str()).toTensorList().vec();

  // For all available CUDA devices: Load PT2 package on this device, run
  // inference, and validate results
  auto input_tensors =
      data_loader.attr(inputs_attr.c_str()).toTensorList().vec();
  for (int i = 0; i < torch::cuda::device_count(); i++) {
    auto options = torch::TensorOptions().device(torch::kCUDA, i);
    torch::inductor::AOTIModelPackageLoader runner(
        pt2_package_path, "model", false, 1, i);
    std::vector<torch::Tensor> input_tensors_on_device;
    for (auto input_tensor : input_tensors) {
      input_tensors_on_device.push_back(input_tensor.clone().to(options));
    }
    // Run loaded PT2 package on device
    auto actual_output_tensors = runner.run(input_tensors_on_device);
    ASSERT_TRUE(torch::allclose(
        ref_output_tensors[0].cpu(), actual_output_tensors[0].cpu()));
  }
}

void test_aoti_constants_update(
    const std::string& device,
    bool use_runtime_constant_folding) {
  torch::NoGradGuard no_grad;

  std::string data_path =
      (std::filesystem::path(STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "data.pt")
           .string();

  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  std::string suffix = use_runtime_constant_folding
      ? device + "_use_runtime_constant_folding"
      : device;
  std::string path_attr = "model_so_path_" + suffix;
  std::string inputs_attr = "inputs_" + suffix;
  std::string outputs_attr = "outputs_" + suffix;
  std::string weights_attr = "w_pre_" + suffix;
  std::string add_attr = "w_add_" + suffix;
  const auto& model_so_path = data_loader.attr(path_attr.c_str()).toStringRef();
  auto input_tensors =
      data_loader.attr(inputs_attr.c_str()).toTensorList().vec();
  const auto& ref_output_tensors =
      data_loader.attr(outputs_attr.c_str()).toTensorList().vec();

  const auto& weight_tensors =
      data_loader.attr(weights_attr.c_str()).toTensor();
  const auto& add_tensors = data_loader.attr(add_attr.c_str()).toTensor();

  // The random constants stay on CPU; update_constant_buffer_from_cpu
  // auto-copies them to the container's device.
  torch::inductor::TensorConstantMap missing_map, rand_map, real_map;
  missing_map.emplace("L__self___w_pre", new at::Tensor(at::randn({4, 4})));
  rand_map.emplace("L__self___w_pre", new at::Tensor(at::randn({10})));
  rand_map.emplace("L__self___w_add", new at::Tensor(at::randn({10})));
  real_map.emplace("L__self___w_pre", new at::Tensor(weight_tensors));
  real_map.emplace("L__self___w_add", new at::Tensor(add_tensors));

  std::unique_ptr<torch::inductor::AOTIModelContainerRunner> runner;
  if (device == "cpu") {
    runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCpu>(
        model_so_path);
#if defined(USE_CUDA) || defined(USE_ROCM)
  } else if (device == "cuda") {
    runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCuda>(
        model_so_path);
#endif
  } else {
    FAIL() << "unsupported device: " << device;
  }
  // By default, buffer #1 get loaded with burned in weights. Correct results.
  auto actual_output_tensors = runner->run(input_tensors);
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  // Update with missing map which should throw.
  // Somehow EXPECT_THROW doesn't work here when running tests in a row, but
  // works when running AotInductorTest.RuntimeUpdateConstantsCuda individually.
  try {
    runner->update_constant_buffer_from_cpu(missing_map, false, true);
  } catch (const std::exception& e) {
    EXPECT_THAT(e.what(), ::testing::HasSubstr("Cannot find constants"));
  }

  // Update random weight to buffer #1.
  runner->update_constant_buffer_from_cpu(missing_map, false, false);
  actual_output_tensors = runner->run(input_tensors);
  ASSERT_FALSE(
      torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  // Update with real map.
  runner->update_constant_buffer(real_map, false, false);
  actual_output_tensors = runner->run(input_tensors);
  if (use_runtime_constant_folding) {
    runner->run_const_fold(/* use_inactive = */ false);
  }
  actual_output_tensors = runner->run(input_tensors);
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  // Update with full random map.
  runner->update_constant_buffer_from_cpu(rand_map, false, false);
  if (use_runtime_constant_folding) {
    runner->run_const_fold(/* use_inactive = */ false);
  }
  actual_output_tensors = runner->run(input_tensors);
  ASSERT_FALSE(
      torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  for (auto& pair : missing_map) {
    delete pair.second;
  }
  for (auto& pair : rand_map) {
    delete pair.second;
  }
  for (auto& pair : real_map) {
    delete pair.second;
  }
}

void test_aoti_extract_constants_map(const std::string& device) {
  torch::NoGradGuard no_grad;

  std::string data_path =
      (std::filesystem::path(STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "data.pt")
           .string();

  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  std::string path_attr = "model_so_path_" + device;
  std::string inputs_attr = "inputs_" + device;
  std::string outputs_attr = "outputs_" + device;
  std::string weights_attr = "w_pre_" + device;
  std::string add_attr = "w_add_" + device;
  const auto& model_so_path = data_loader.attr(path_attr.c_str()).toStringRef();
  auto input_tensors =
      data_loader.attr(inputs_attr.c_str()).toTensorList().vec();
  const auto& ref_output_tensors =
      data_loader.attr(outputs_attr.c_str()).toTensorList().vec();

  const auto& weight_tensors =
      data_loader.attr(weights_attr.c_str()).toTensor();
  const auto& add_tensors = data_loader.attr(add_attr.c_str()).toTensor();

  torch::inductor::TensorConstantMap rand_map, real_map;
  at::Tensor rand_pre, rand_add;
  at::Tensor w_pre, w_add;
  at::DeviceType device_type = device == "cuda" ? at::kCUDA : at::kCPU;
  rand_pre = at::randn({4, 4}).to(device_type);
  rand_add = at::randn({4, 4}).to(device_type);
  w_pre = at::Tensor(weight_tensors).to(device_type);
  w_add = at::Tensor(add_tensors).to(device_type);

  rand_map.emplace("L__self___w_pre", &rand_pre);
  rand_map.emplace("L__self___w_add", &rand_add);
  real_map.emplace("L__self___w_pre", &w_pre);
  real_map.emplace("L__self___w_add", &w_add);

  std::unique_ptr<torch::inductor::AOTIModelContainerRunner> runner;
  if (device == "cpu") {
    runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCpu>(
        model_so_path);
#if defined(USE_CUDA) || defined(USE_ROCM)
  } else if (device == "cuda") {
    runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCuda>(
        model_so_path);
#endif
  } else {
    FAIL() << "unsupported device: " << device;
  }

  // By default, buffer #1 get loaded with burned in weights. Correct results.
  auto actual_output_tensors = runner->run(input_tensors);
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  // We update the weights to buffer #2 and activate it. This should still
  // produce correct result, as it's the real constant map.
  runner->update_inactive_constant_buffer(real_map);
  auto extracted_inactive_weight =
      runner->extract_constants_map(/* use_inactive = */ true);
  auto extracted_active_weight =
      runner->extract_constants_map(/* use_inactive = */ false);
  auto cmp_real_map = derefTensorConstantMap(real_map);
  auto cmp_rand_map = derefTensorConstantMap(rand_map);
  ASSERT_TRUE(compareConstantMap(extracted_active_weight, cmp_real_map));
  ASSERT_TRUE(compareConstantMap(extracted_inactive_weight, cmp_real_map));

  // We update random weights to buffer #1. But do not swap in the weight yet.
  runner->update_inactive_constant_buffer(rand_map);
  extracted_inactive_weight =
      runner->extract_constants_map(/* use_inactive = */ true);
  ASSERT_TRUE(compareConstantMap(extracted_inactive_weight, cmp_rand_map));

  // We swap and activate the weight to buffer #1.
  // Active weight now should be the new weight, while inactive should be the
  // previous one.
  runner->swap_constant_buffer();
  extracted_inactive_weight =
      runner->extract_constants_map(/* use_inactive = */ true);
  extracted_active_weight =
      runner->extract_constants_map(/* use_inactive = */ false);
  ASSERT_TRUE(compareConstantMap(extracted_active_weight, cmp_rand_map));
  ASSERT_TRUE(compareConstantMap(extracted_inactive_weight, cmp_real_map));
}

void test_aoti_double_buffering(
    const std::string& device,
    bool use_runtime_constant_folding) {
  torch::NoGradGuard no_grad;

  std::string data_path =
      (std::filesystem::path(STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "data.pt")
           .string();

  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  std::string suffix = use_runtime_constant_folding
      ? device + "_use_runtime_constant_folding"
      : device;
  std::string path_attr = "model_so_path_" + suffix;
  std::string inputs_attr = "inputs_" + suffix;
  std::string outputs_attr = "outputs_" + suffix;
  std::string weights_attr = "w_pre_" + suffix;
  std::string add_attr = "w_add_" + suffix;
  const auto& model_so_path = data_loader.attr(path_attr.c_str()).toStringRef();
  auto input_tensors =
      data_loader.attr(inputs_attr.c_str()).toTensorList().vec();
  const auto& ref_output_tensors =
      data_loader.attr(outputs_attr.c_str()).toTensorList().vec();

  const auto& weight_tensors =
      data_loader.attr(weights_attr.c_str()).toTensor();
  const auto& add_tensors = data_loader.attr(add_attr.c_str()).toTensor();

  // The random constants stay on CPU; update_constant_buffer_from_cpu
  // auto-copies them to the container's device.
  torch::inductor::TensorConstantMap rand_map, real_map;
  rand_map.emplace("L__self___w_pre", new at::Tensor(at::randn({4, 4})));
  rand_map.emplace("L__self___w_add", new at::Tensor(at::randn({4, 4})));
  real_map.emplace("L__self___w_pre", new at::Tensor(weight_tensors));
  real_map.emplace("L__self___w_add", new at::Tensor(add_tensors));

  std::unique_ptr<torch::inductor::AOTIModelContainerRunner> runner;
  if (device == "cpu") {
    runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCpu>(
        model_so_path);
#if defined(USE_CUDA) || defined(USE_ROCM)
  } else if (device == "cuda") {
    runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCuda>(
        model_so_path);
#endif
  } else {
    FAIL() << "unsupported device: " << device;
  }
  // By default, buffer #1 get loaded with burned in weights. Correct results.
  auto actual_output_tensors = runner->run(input_tensors);
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  // We update the weights to buffer #2 and activate it. This should still
  // produce correct result, as it's the real constant map.
  runner->update_inactive_constant_buffer(real_map);
  if (use_runtime_constant_folding) {
    runner->run_const_fold(/* use_inactive = */ true);
  }
  runner->swap_constant_buffer();
  actual_output_tensors = runner->run(input_tensors);
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  // We update random weights to buffer #1. But do not swap in the weight yet.
  runner->update_constant_buffer_from_cpu(
      rand_map, /* use_inactive = */ true, /* check_full_update = */ true);
  if (use_runtime_constant_folding) {
    runner->run_const_fold(/* use_inactive = */ true);
  }
  actual_output_tensors = runner->run(input_tensors);
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  // We swap and activate the weight to buffer #1. This is random weight and
  // should produce incorrect results.
  runner->swap_constant_buffer();
  actual_output_tensors = runner->run(input_tensors);
  ASSERT_FALSE(
      torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  // Swap back to buffer #2 which is the real constants.
  runner->swap_constant_buffer();
  actual_output_tensors = runner->run(input_tensors);
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  for (auto& pair : rand_map) {
    delete pair.second;
  }
  for (auto& pair : real_map) {
    delete pair.second;
  }
}

#if defined(USE_CUDA) || defined(USE_ROCM)
void test_aoti_double_buffering_with_tensor_constants() {
  torch::NoGradGuard no_grad;

  std::string data_path = (std::filesystem::path(
                               STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) /
                               "data_with_tensor_constants.pt")
                               .string();

  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  std::string path_attr = "model_so_path";
  std::string inputs_attr = "inputs";
  std::string w_attr = "w";
  std::string outputs_attr = "outputs";
  const auto& model_so_path = data_loader.attr(path_attr.c_str()).toStringRef();
  auto input_tensors =
      data_loader.attr(inputs_attr.c_str()).toTensorList().vec();
  const auto& w_tensors = data_loader.attr(w_attr.c_str()).toTensor();
  const auto& ref_output_tensors =
      data_loader.attr(outputs_attr.c_str()).toTensorList().vec();

  torch::inductor::TensorConstantMap real_map;
  real_map.emplace("L__self___w", new at::Tensor(w_tensors));

  std::unique_ptr<torch::inductor::AOTIModelContainerRunner> runner;
  runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCuda>(
      model_so_path.c_str());

  // By default, buffer #1 get loaded with burned in weights. Correct results.
  auto actual_output_tensors = runner->run(input_tensors);
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  // We update the weights to buffer #2 and activate it. This should still
  // produce correct result, since we would have copied the tensor_constants.
  runner->update_inactive_constant_buffer(real_map);
  runner->swap_constant_buffer();
  actual_output_tensors = runner->run(input_tensors);
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  for (auto& pair : real_map) {
    delete pair.second;
  }
}

void test_aoti_user_managed_buffer() {
  torch::NoGradGuard no_grad;

  std::string data_path =
      (std::filesystem::path(
           STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "large_data.pt")
           .string();

  // Memory information variable
  size_t DATASIZE = 128 * 1024 * 1024; // We have 128MB of weight data.

  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  std::string path_attr = "model_so_path";
  std::string inputs_attr = "inputs";
  std::string outputs_attr = "outputs";
  std::string weights_attr = "w_pre";
  std::string add_attr = "w_add";
  const auto& model_so_path = data_loader.attr(path_attr.c_str()).toStringRef();
  auto input_tensors =
      data_loader.attr(inputs_attr.c_str()).toTensorList().vec();
  const auto& ref_output_tensors =
      data_loader.attr(outputs_attr.c_str()).toTensorList().vec();

  const auto& weight_tensors =
      data_loader.attr(weights_attr.c_str()).toTensor();
  const auto& add_tensors = data_loader.attr(add_attr.c_str()).toTensor();

  torch::inductor::TensorConstantMap rand_map, real_map;
  at::Tensor rand_pre, rand_add;
  at::Tensor w_pre, w_add;
  rand_pre = at::randn({4096, 4096}).contiguous().to(at::kCUDA);
  rand_add = at::randn({4096, 4096}).contiguous().to(at::kCUDA);
  w_pre = at::Tensor(weight_tensors).contiguous().to(at::kCUDA);
  w_add = at::Tensor(add_tensors).contiguous().to(at::kCUDA);

  rand_map.emplace("L__self___w_pre", &rand_pre);
  rand_map.emplace("L__self___w_add", &rand_add);
  real_map.emplace("L__self___w_pre", &w_pre);
  real_map.emplace("L__self___w_add", &w_add);

  std::unique_ptr<torch::inductor::AOTIModelContainerRunner> runner;
  runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCuda>(
      model_so_path);

  // We extract the memory information starting from here.
  int device_idx = -1;
  cudaError_t cudaStatus;
  cudaStatus = cudaGetDevice(&device_idx);
  c10::cuda::CUDACachingAllocator::DeviceStats stats =
      c10::cuda::CUDACachingAllocator::getDeviceStats(device_idx);
  size_t initTorchReserved = stats.reserved_bytes[0].current;
  size_t torchReserved = stats.reserved_bytes[0].current;
  if (cudaStatus != cudaSuccess || device_idx == -1) {
    TORCH_CHECK(false, "cudaGetDevice failed!");
  }
  // This should contain one set of weight (128MB) loaded from .so
  size_t initMemory = 0;
  size_t totalMemory = 0;
  size_t preFreeMemory = 0;
  cudaStatus = cudaMemGetInfo(&preFreeMemory, &totalMemory);
  if (cudaStatus != cudaSuccess) {
    TORCH_CHECK(false, "cudaMemGetInfo failed!");
  }
  // At this point, no memory should be consumed since we freed them all.
  runner->swap_constant_buffer();
  runner->free_inactive_constant_buffer();
  runner->swap_constant_buffer();
  cudaStatus = cudaMemGetInfo(&initMemory, &totalMemory);
  if (cudaStatus != cudaSuccess) {
    TORCH_CHECK(false, "cudaMemGetInfo failed!");
  }
  ASSERT_EQ(initMemory - DATASIZE, preFreeMemory);

  // We update the active buffer, but with user_managed = True. This shouldn't
  // add any memory consumption.
  runner->update_constant_buffer(
      real_map,
      /*use_inactive = */ false,
      /*validate_full_updates = */ true,
      /*user_managed = */ true);
  size_t updateMemory = 0;
  cudaStatus = cudaMemGetInfo(&updateMemory, &totalMemory);
  if (cudaStatus != cudaSuccess) {
    TORCH_CHECK(false, "cudaMemGetInfo failed!");
  }
  ASSERT_EQ(initMemory, updateMemory);

  // Make sure the output is correct with user managed buffer.
  auto actual_output_tensors = runner->run(input_tensors);
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  // Update with rand_map and extract the output of rand_map.
  // We let user_managed = false for rand_map, this should increase memory
  // consumption.
  cudaStatus = cudaMemGetInfo(&initMemory, &totalMemory);
  if (cudaStatus != cudaSuccess) {
    TORCH_CHECK(false, "cudaMemGetInfo failed!");
  }
  runner->update_constant_buffer(
      rand_map,
      /*use_inactive = */ true,
      /*validate_full_updates = */ true,
      /*user_managed = */ false);
  cudaStatus = cudaMemGetInfo(&updateMemory, &totalMemory);
  if (cudaStatus != cudaSuccess) {
    TORCH_CHECK(false, "cudaMemGetInfo failed!");
  }
  ASSERT_EQ(initMemory - DATASIZE, updateMemory);

  runner->swap_constant_buffer();
  auto ref_rand_output_tensors = runner->run(input_tensors);
  ASSERT_FALSE(
      torch::allclose(ref_output_tensors[0], ref_rand_output_tensors[0]));

  // Free everything.
  runner->free_inactive_constant_buffer();
  runner->swap_constant_buffer();
  runner->free_inactive_constant_buffer();

  // Set buffer #1 user_managed, and #2 not user managed, and compare the
  // underlying data
  runner->update_constant_buffer(
      real_map,
      /*use_inactive = */ false,
      /*validate_full_updates = */ true,
      /*user_managed = */ false);
  runner->update_constant_buffer(
      real_map,
      /*use_inactive = */ true,
      /*validate_full_updates = */ true,
      /*user_managed = */ true);

  auto extracted_active_weight =
      runner->extract_constants_map(/* use_inactive = */ false);
  auto extracted_inactive_weight =
      runner->extract_constants_map(/* use_inactive = */ true);
  auto cmp_real_map = derefTensorConstantMap(real_map);
  // Value-wise all weights are equal
  ASSERT_TRUE(compareConstantMap(extracted_active_weight, cmp_real_map));
  ASSERT_TRUE(compareConstantMap(extracted_inactive_weight, cmp_real_map));
  // Only when user_managed has the same underlying if set to true.
  ASSERT_FALSE(
      extracted_active_weight["L__self___w_pre"].data_ptr() ==
      cmp_real_map["L__self___w_pre"].data_ptr());
  ASSERT_TRUE(
      extracted_inactive_weight["L__self___w_pre"].data_ptr() ==
      cmp_real_map["L__self___w_pre"].data_ptr());

  // From non user_managed
  actual_output_tensors = runner->run(input_tensors);
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  // From user_managed
  runner->swap_constant_buffer();
  actual_output_tensors = runner->run(input_tensors);
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));

  // We modify the buffer by the data's pointer outside of container.
  cudaMemcpy(
      real_map["L__self___w_add"]->data_ptr(),
      rand_map["L__self___w_add"]->data_ptr(),
      4096 * 4096 * sizeof(float),
      cudaMemcpyDeviceToDevice);
  cudaMemcpy(
      real_map["L__self___w_pre"]->data_ptr(),
      rand_map["L__self___w_pre"]->data_ptr(),
      4096 * 4096 * sizeof(float),
      cudaMemcpyDeviceToDevice);

  // We should get the result of the rand output.
  actual_output_tensors = runner->run(input_tensors);
  ASSERT_TRUE(
      torch::allclose(ref_rand_output_tensors[0], actual_output_tensors[0]));
}

void test_aoti_free_buffer(bool use_runtime_constant_folding) {
  torch::NoGradGuard no_grad;

  std::string data_path =
      (std::filesystem::path(
           STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "large_data.pt")
           .string();

  // Memory information variable
  size_t DATASIZE = 128 * 1024 * 1024; // We have 128MB of weight data.
  size_t FOLDEDDATASIZE = use_runtime_constant_folding
      ? 64 * 1024 * 1024
      : 0; // We have 64MB of folded data.

  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  std::string path_attr = "model_so_path";
  if (use_runtime_constant_folding) {
    path_attr += std::string("_use_runtime_constant_folding");
  }
  std::string inputs_attr = "inputs";
  std::string outputs_attr = "outputs";
  std::string weights_attr = "w_pre";
  std::string add_attr = "w_add";
  const auto& model_so_path = data_loader.attr(path_attr.c_str()).toStringRef();
  auto input_tensors =
      data_loader.attr(inputs_attr.c_str()).toTensorList().vec();
  const auto& ref_output_tensors =
      data_loader.attr(outputs_attr.c_str()).toTensorList().vec();

  const auto& weight_tensors =
      data_loader.attr(weights_attr.c_str()).toTensor();
  const auto& add_tensors = data_loader.attr(add_attr.c_str()).toTensor();

  torch::inductor::TensorConstantMap rand_map, real_map;
  rand_map.emplace(
      "L__self___w_pre", new at::Tensor(at::randn({4096, 4096}).to(at::kCUDA)));
  rand_map.emplace(
      "L__self___w_add", new at::Tensor(at::randn({4096, 4096}).to(at::kCUDA)));
  real_map.emplace("L__self___w_pre", new at::Tensor(weight_tensors));
  real_map.emplace("L__self___w_add", new at::Tensor(add_tensors));

  std::unique_ptr<torch::inductor::AOTIModelContainerRunner> runner;
  runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCuda>(
      model_so_path);

  // We extract the memory information starting from here.
  int device_idx = -1;
  cudaError_t cudaStatus;
  cudaStatus = cudaGetDevice(&device_idx);
  if (cudaStatus != cudaSuccess || device_idx == -1) {
    TORCH_CHECK(false, "cudaGetDevice failed!");
  }
  c10::cuda::CUDACachingAllocator::DeviceStats stats =
      c10::cuda::CUDACachingAllocator::getDeviceStats(device_idx);
  size_t initTorchActive = stats.active_bytes[0].current;
  size_t initTorchReserved = stats.reserved_bytes[0].current;
  // This should contain one set of weight (128MB) loaded from .so
  size_t torchActive1, torchActive2;
  size_t torchReserved1, torchReserved2;
  size_t initMemory = 0;
  size_t totalMemory = 0;
  cudaStatus = cudaMemGetInfo(&initMemory, &totalMemory);
  if (cudaStatus != cudaSuccess) {
    TORCH_CHECK(false, "cudaMemGetInfo failed!");
  }

  // We update inactive buffer, this should create one copy (128MB) at buffer #2
  runner->update_inactive_constant_buffer(real_map);
  size_t updateMemory2 = 0;
  cudaStatus = cudaMemGetInfo(&updateMemory2, &totalMemory);
  if (cudaStatus != cudaSuccess) {
    TORCH_CHECK(false, "cudaMemGetInfo failed!");
  }
  ASSERT_EQ(initMemory - DATASIZE, updateMemory2);

  // Call run, this should run const_fold and create the folded constant in #2
  // (64MB).
  if (use_runtime_constant_folding) {
    runner->run_const_fold(/* use_inactive = */ true);
    stats = c10::cuda::CUDACachingAllocator::getDeviceStats(device_idx);
    torchActive1 = stats.active_bytes[0].current;
    torchReserved1 = stats.reserved_bytes[0].current;
    size_t constFoldMemory = 0;
    cudaStatus = cudaMemGetInfo(&constFoldMemory, &totalMemory);
    if (cudaStatus != cudaSuccess) {
      TORCH_CHECK(false, "cudaMemGetInfo failed!");
    }
    ASSERT_EQ(
        initMemory - DATASIZE - (torchReserved1 - initTorchReserved),
        constFoldMemory);
    ASSERT_EQ(torchActive1 - initTorchActive, FOLDEDDATASIZE);
  }

  // We swap and free the inactive buffer. (Use #2 and free #1)
  // Note that buffer #1 does not include folded-const
  stats = c10::cuda::CUDACachingAllocator::getDeviceStats(device_idx);
  torchActive1 = stats.active_bytes[0].current;
  torchReserved1 = stats.reserved_bytes[0].current;
  runner->swap_constant_buffer();
  runner->free_inactive_constant_buffer();
  stats = c10::cuda::CUDACachingAllocator::getDeviceStats(device_idx);
  torchActive2 = stats.active_bytes[0].current;
  torchReserved2 = stats.reserved_bytes[0].current;
  size_t postFreeMemory = 0;
  cudaStatus = cudaMemGetInfo(&postFreeMemory, &totalMemory);
  if (cudaStatus != cudaSuccess) {
    TORCH_CHECK(false, "cudaMemGetInfo failed!");
  }
  // We should only have one set of buffer (#2), available memory should equal
  // initial memory minus the folded constants.
  ASSERT_EQ(initMemory - (torchReserved2 - initTorchReserved), postFreeMemory);
  // Buffer #1 does not include folded-consts
  ASSERT_EQ(torchActive2 - torchActive1, 0);

  // We update random weights to buffer #1 and run const fold.
  // We will have 2 full set of data plus 2 set of const-folded data.
  runner->update_inactive_constant_buffer(rand_map);
  runner->run_const_fold(/* use_inactive = */ true);
  stats = c10::cuda::CUDACachingAllocator::getDeviceStats(device_idx);
  torchActive1 = stats.active_bytes[0].current;
  torchReserved1 = stats.reserved_bytes[0].current;
  size_t updateMemory1 = 0;
  cudaStatus = cudaMemGetInfo(&updateMemory1, &totalMemory);
  if (cudaStatus != cudaSuccess) {
    TORCH_CHECK(false, "cudaMemGetInfo failed!");
  }
  ASSERT_EQ(
      initMemory - DATASIZE - (torchReserved1 - initTorchReserved),
      updateMemory1);
  ASSERT_EQ(torchActive1 - initTorchActive, 2 * FOLDEDDATASIZE);

  // We directly free the buffer #1. This would free the DATASIZE weight.
  // If folded constant exists, it will not directly free the cudaMalloc, but
  // decrease the active buffer in CachingAllocator instead.
  stats = c10::cuda::CUDACachingAllocator::getDeviceStats(device_idx);
  torchActive1 = stats.active_bytes[0].current;
  runner->free_inactive_constant_buffer();
  cudaStatus = cudaMemGetInfo(&updateMemory1, &totalMemory);
  if (cudaStatus != cudaSuccess) {
    TORCH_CHECK(false, "cudaMemGetInfo failed!");
  }
  stats = c10::cuda::CUDACachingAllocator::getDeviceStats(device_idx);
  torchActive2 = stats.active_bytes[0].current;
  torchReserved2 = stats.reserved_bytes[0].current;
  ASSERT_EQ(initMemory - (torchReserved2 - initTorchReserved), updateMemory1);
  ASSERT_EQ(FOLDEDDATASIZE, torchActive1 - torchActive2);

  // Free buffer #1 again, since #1 is freed, nothing should change.
  stats = c10::cuda::CUDACachingAllocator::getDeviceStats(device_idx);
  torchActive1 = stats.active_bytes[0].current;
  runner->free_inactive_constant_buffer();
  stats = c10::cuda::CUDACachingAllocator::getDeviceStats(device_idx);
  torchActive2 = stats.active_bytes[0].current;
  cudaStatus = cudaMemGetInfo(&updateMemory1, &totalMemory);
  if (cudaStatus != cudaSuccess) {
    TORCH_CHECK(false, "cudaMemGetInfo failed!");
  }
  ASSERT_EQ(initMemory - (torchReserved2 - initTorchReserved), updateMemory1);
  ASSERT_EQ(torchActive1 - torchActive2, 0);

  // Swap and free #2, no data should exist in memory now.
  // However, the folded constants might still occupies the CUDA memory in
  // CachedAllocator.
  stats = c10::cuda::CUDACachingAllocator::getDeviceStats(device_idx);
  torchActive1 = stats.active_bytes[0].current;
  torchReserved1 = stats.reserved_bytes[0].current;
  runner->swap_constant_buffer();
  runner->free_inactive_constant_buffer();
  stats = c10::cuda::CUDACachingAllocator::getDeviceStats(device_idx);
  torchActive2 = stats.active_bytes[0].current;
  torchReserved2 = stats.reserved_bytes[0].current;
  cudaStatus = cudaMemGetInfo(&updateMemory1, &totalMemory);
  if (cudaStatus != cudaSuccess) {
    TORCH_CHECK(false, "cudaMemGetInfo failed!");
  }

  ASSERT_EQ(
      initMemory + DATASIZE - (torchReserved2 - initTorchReserved),
      updateMemory1);
  ASSERT_EQ(FOLDEDDATASIZE, torchActive1 - torchActive2);
  ASSERT_EQ(0, torchActive2 - initTorchActive);

  for (auto& pair : rand_map) {
    delete pair.second;
  }
  for (auto& pair : real_map) {
    delete pair.second;
  }
}

void test_cuda_alloc_test() {
  torch::NoGradGuard no_grad;

  std::string data_path =
      (std::filesystem::path(
           STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "cuda_alloc_data.pt")
           .string();
  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  std::string path_attr = "model_so_path";
  std::string inputs_attr = "inputs";
  std::string outputs_attr = "outputs";
  const auto& model_so_path = data_loader.attr(path_attr.c_str()).toStringRef();
  const auto& ref_output_tensors =
      data_loader.attr(outputs_attr.c_str()).toTensorList().vec();

  size_t DATASIZE = 128 * 1024 * 1024; // We have 128MB of weight data.

  int device_idx = -1;
  cudaError_t cudaStatus;
  cudaStatus = cudaGetDevice(&device_idx);
  if (cudaStatus != cudaSuccess || device_idx == -1) {
    TORCH_CHECK(false, "cudaGetDevice failed!");
  }

  c10::cuda::CUDACachingAllocator::emptyCache();
  c10::cuda::CUDACachingAllocator::DeviceStats stats =
      c10::cuda::CUDACachingAllocator::getDeviceStats(device_idx);
  size_t initTorchActive = stats.allocated_bytes[0].current;
  auto runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCuda>(
      model_so_path);
  stats = c10::cuda::CUDACachingAllocator::getDeviceStats(device_idx);
  size_t torchActive = stats.allocated_bytes[0].current;

  ASSERT_EQ(initTorchActive + DATASIZE, torchActive);

  auto actual_output_tensors =
      runner->run(data_loader.attr(inputs_attr.c_str()).toTensorList().vec());
  ASSERT_TRUE(torch::allclose(ref_output_tensors[0], actual_output_tensors[0]));
}

#ifdef USE_CUDA
class ThreadPool {
 private:
  struct Task {
    int id;
    std::vector<torch::Tensor> inputs;
  };

  std::vector<std::thread> workers;
  std::vector<c10::cuda::CUDAStream> cuda_streams;
  std::queue<Task> tasks;
  std::mutex queue_mutex;
  std::condition_variable condition;
  std::condition_variable completion_condition;
  std::atomic<int> active_tasks{0};
  std::atomic<bool> stop;

 public:
  ThreadPool(size_t num_threads) : stop(false) {
    // Create CUDA streams
    cuda_streams.reserve(num_threads);
    for (size_t i = 0; i < num_threads; ++i) {
      cuda_streams.push_back(c10::cuda::getStreamFromPool());
    }

    // Create worker threads
    for (size_t i = 0; i < num_threads; ++i) {
      workers.emplace_back([this, i] {
        while (true) {
          Task task;
          {
            std::unique_lock<std::mutex> lock(this->queue_mutex);
            this->condition.wait(
                lock, [this] { return this->stop || !this->tasks.empty(); });

            if (this->stop && this->tasks.empty()) {
              return;
            }

            task = std::move(this->tasks.front());
            this->tasks.pop();
          }

          // Process the task with this thread's CUDA stream
          process_function(task.id, task.inputs, this->cuda_streams[i]);

          // Mark task as completed
          {
            std::unique_lock<std::mutex> lock(this->queue_mutex);
            active_tasks--;
            if (active_tasks == 0 && this->tasks.empty()) {
              completion_condition.notify_all();
            }
          }
        }
      });
    }
  }

  // Updated processing function for vector of tensors and CUDA stream
  std::function<
      void(int, const std::vector<torch::Tensor>&, c10::cuda::CUDAStream&)>
      process_function;

  // Enqueue task with vector of tensors as input
  void enqueue(int i, std::vector<torch::Tensor> inputs) {
    {
      std::unique_lock<std::mutex> lock(queue_mutex);
      tasks.push({i, std::move(inputs)});
      active_tasks++;
    }
    condition.notify_one();
  }

  // Wait for all tasks to complete
  void wait() {
    std::unique_lock<std::mutex> lock(queue_mutex);
    completion_condition.wait(
        lock, [this] { return active_tasks == 0 && tasks.empty(); });
  }

  ~ThreadPool() {
    {
      std::unique_lock<std::mutex> lock(queue_mutex);
      stop = true;
    }

    condition.notify_all();
    for (std::thread& worker : workers) {
      worker.join();
    }
  }
};

void test_multi_cuda_streams(const std::string& device) {
  c10::InferenceMode mode;
  std::string data_path =
      (std::filesystem::path(STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "data.pt")
           .string();
  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  std::string path_attr = "pt2_package_path_" + device;
  std::string inputs_attr = "inputs_" + device;
  std::string outputs_attr = "outputs_" + device;
  const auto& pt2_package_path =
      data_loader.attr(path_attr.c_str()).toStringRef();
  const auto& ref_output_tensors =
      data_loader.attr(outputs_attr.c_str()).toTensorList().vec();
  auto inputs = data_loader.attr(inputs_attr.c_str()).toTensorList().vec();

  constexpr int N = 16;
  constexpr int num_threads = 4;
  std::vector<std::vector<torch::Tensor>> all_outputs(N);
  // Create thread pool with desired number of threads
  torch::inductor::AOTIModelPackageLoader loader(
      pt2_package_path, "model", false, num_threads);
  ThreadPool pool(num_threads);
  std::mutex results_mutex;

  // Set the processing function
  pool.process_function = [&](int i,
                              const std::vector<torch::Tensor>& inputs,
                              c10::cuda::CUDAStream& stream) {
    // Run inference with the task-specific input
    std::vector<torch::Tensor> outputs = loader.run(inputs, stream.stream());
    // Store results safely
    {
      std::lock_guard<std::mutex> lock(results_mutex);
      all_outputs[i] = outputs;
    }
  };
  // Enqueue all tasks
  for (int i = 0; i < N; i++) {
    pool.enqueue(i, inputs);
  }
  // Wait for all tasks to complete
  pool.wait();

  for (int i = 0; i < N; i++) {
    ASSERT_TRUE(torch::allclose(ref_output_tensors[0], all_outputs[i][0]));
  }
}
#endif // USE_CUDA

void test_cuda_stream_affinity_bindings(const std::string& device) {
  c10::InferenceMode mode;
  const std::string data_path =
      (std::filesystem::path(STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "data.pt")
           .string();
  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  const auto& pt2_package_path =
      data_loader.attr(("pt2_package_path_" + device).c_str()).toStringRef();
  const auto ref_output =
      data_loader.attr(("outputs_" + device).c_str()).toTensorList().get(0);
  const auto inputs =
      data_loader.attr(("inputs_" + device).c_str()).toTensorList().vec();
  const auto first_stream = c10::cuda::getStreamFromPool();
  const auto second_stream = c10::cuda::getStreamFromPool();
  ASSERT_NE(first_stream.stream(), second_stream.stream());

  auto run_on_stream = [&](torch::inductor::AOTIModelPackageLoader& loader,
                           const c10::cuda::CUDAStream& stream) {
    c10::cuda::CUDAStreamGuard stream_guard(stream);
    const auto outputs = loader.run(inputs, stream.stream());
    stream.synchronize();
    ASSERT_TRUE(torch::allclose(ref_output, outputs[0]));
  };

  torch::inductor::AOTIModelPackageLoader default_loader(
      pt2_package_path, "model", false, 2);
  run_on_stream(default_loader, first_stream);
  EXPECT_EQ(
      AOTI_STREAM_AFFINITY_DISABLED,
      default_loader.get_runner()->get_stream_affinity_model_index_for_testing(
          first_stream.stream()));

  torch::inductor::AOTIModelPackageLoader affinity_loader(
      pt2_package_path,
      "model",
      false,
      2,
      -1,
      /*use_stream_affinity=*/true);
  EXPECT_EQ(
      AOTI_STREAM_AFFINITY_UNBOUND,
      affinity_loader.get_runner()->get_stream_affinity_model_index_for_testing(
          first_stream.stream()));

  run_on_stream(affinity_loader, first_stream);
  const auto first_model =
      affinity_loader.get_runner()->get_stream_affinity_model_index_for_testing(
          first_stream.stream());
  ASSERT_GE(first_model, 0);
  try {
    affinity_loader.get_runner()->set_use_stream_affinity(true);
    FAIL() << "Expected affinity reconfiguration after a run to be rejected";
  } catch (const c10::Error& error) {
    EXPECT_THAT(
        error.what(),
        ::testing::HasSubstr(
            "Stream affinity can only be reconfigured while the model pool is "
            "quiescent"));
  }
  run_on_stream(affinity_loader, first_stream);
  EXPECT_EQ(
      first_model,
      affinity_loader.get_runner()->get_stream_affinity_model_index_for_testing(
          first_stream.stream()));

  run_on_stream(affinity_loader, second_stream);
  const auto second_model =
      affinity_loader.get_runner()->get_stream_affinity_model_index_for_testing(
          second_stream.stream());
  ASSERT_GE(second_model, 0);
  EXPECT_NE(first_model, second_model);

  torch::inductor::AOTIModelPackageLoader oversubscribed_loader(
      pt2_package_path,
      "model",
      false,
      1,
      -1,
      /*use_stream_affinity=*/true);
  run_on_stream(oversubscribed_loader, first_stream);
  const auto only_model =
      oversubscribed_loader.get_runner()
          ->get_stream_affinity_model_index_for_testing(first_stream.stream());
  ASSERT_GE(only_model, 0);
  run_on_stream(oversubscribed_loader, second_stream);
  EXPECT_EQ(
      AOTI_STREAM_AFFINITY_UNBOUND,
      oversubscribed_loader.get_runner()
          ->get_stream_affinity_model_index_for_testing(first_stream.stream()));
  EXPECT_EQ(
      only_model,
      oversubscribed_loader.get_runner()
          ->get_stream_affinity_model_index_for_testing(
              second_stream.stream()));
}

void test_concurrent_same_cuda_stream_affinity(const std::string& device) {
  c10::InferenceMode mode;
  const std::string data_path =
      (std::filesystem::path(STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "data.pt")
           .string();
  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  const auto& pt2_package_path =
      data_loader.attr(("pt2_package_path_" + device).c_str()).toStringRef();
  const auto ref_output =
      data_loader.attr(("outputs_" + device).c_str()).toTensorList().get(0);
  const auto inputs =
      data_loader.attr(("inputs_" + device).c_str()).toTensorList().vec();

  constexpr int num_threads = 2;
  torch::inductor::AOTIModelPackageLoader loader(
      pt2_package_path,
      "model",
      false,
      num_threads,
      -1,
      /*use_stream_affinity=*/true);
  const auto stream = c10::cuda::getStreamFromPool();
  std::atomic<int> ready{0};
  std::vector<std::vector<torch::Tensor>> all_outputs(num_threads);
  std::vector<std::thread> threads;
  for (int i = 0; i < num_threads; ++i) {
    threads.emplace_back([&, i]() {
      c10::cuda::CUDAStreamGuard stream_guard(stream);
      ready.fetch_add(1);
      while (ready.load() < num_threads) {
        std::this_thread::yield();
      }
      all_outputs[i] = loader.run(inputs, stream.stream());
    });
  }
  for (auto& thread : threads) {
    thread.join();
  }
  stream.synchronize();

  for (const auto& outputs : all_outputs) {
    ASSERT_TRUE(torch::allclose(ref_output, outputs[0]));
  }
  EXPECT_GE(
      loader.get_runner()->get_stream_affinity_model_index_for_testing(
          stream.stream()),
      0);
}
#endif // USE_CUDA || USE_ROCM

void test_stream_affinity_rejected_for_cpu() {
  const std::string data_path =
      (std::filesystem::path(STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "data.pt")
           .string();
  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  const auto& pt2_package_path =
      data_loader.attr("pt2_package_path_cpu").toStringRef();

  try {
    torch::inductor::AOTIModelPackageLoader loader(
        pt2_package_path,
        "model",
        false,
        1,
        -1,
        /*use_stream_affinity=*/true);
    FAIL() << "Expected CPU stream affinity to be rejected";
  } catch (const c10::Error& error) {
    EXPECT_THAT(
        error.what(),
        ::testing::HasSubstr(
            "Stream affinity is only supported for device runtimes"));
  }
}

void test_stream_affinity_rejected_with_single_threaded() {
  try {
    torch::inductor::AOTIModelPackageLoader loader(
        "does_not_exist.pt2",
        "model",
        true,
        1,
        -1,
        /*use_stream_affinity=*/true);
    FAIL() << "Expected single-threaded stream affinity to be rejected";
  } catch (const c10::Error& error) {
    EXPECT_THAT(
        error.what(),
        ::testing::HasSubstr("use_stream_affinity cannot be enabled when "
                             "run_single_threaded is true"));
  }
}

void test_stream_affinity_rejected_by_single_threaded_runner() {
  const std::string data_path =
      (std::filesystem::path(STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "data.pt")
           .string();
  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  const auto& pt2_package_path =
      data_loader.attr("pt2_package_path_cpu").toStringRef();

  try {
    torch::inductor::AOTIModelPackageLoader loader(
        pt2_package_path, "model", true, 1, -1);
    loader.get_runner()->set_use_stream_affinity(true);
    FAIL() << "Expected the single-threaded runner to reject stream affinity";
  } catch (const c10::Error& error) {
    EXPECT_THAT(
        error.what(),
        ::testing::HasSubstr("use_stream_affinity cannot be enabled when "
                             "run_single_threaded is true"));
  }
}

#if defined(USE_CUDA) || defined(USE_ROCM)
void test_concurrent_run_with_const_fold(const std::string& device) {
  torch::NoGradGuard no_grad;

  std::string data_path =
      (std::filesystem::path(STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "data.pt")
           .string();

  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  std::string suffix = device + "_use_runtime_constant_folding";
  const auto& model_so_path =
      data_loader.attr(("model_so_path_" + suffix).c_str()).toStringRef();
  auto input_tensors =
      data_loader.attr(("inputs_" + suffix).c_str()).toTensorList().vec();
  const auto& ref_output_tensors =
      data_loader.attr(("outputs_" + suffix).c_str()).toTensorList().vec();

  // num_models=1 forces all threads to contend for the single model instance.
  std::unique_ptr<torch::inductor::AOTIModelContainerRunner> runner;
  if (device == "cuda") {
    runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCuda>(
        model_so_path, /* num_models = */ 1);
  } else {
    FAIL() << "unsupported device: " << device;
  }

  constexpr int num_threads = 4;
  constexpr int num_iters = 4;
  std::atomic<int> ready{0};
  std::atomic<bool> failed{false};

  std::vector<std::thread> threads;
  for (int t = 0; t < num_threads; t++) {
    threads.emplace_back([&]() {
      ready.fetch_add(1);
      while (ready.load() < num_threads) {
      }
      for (int i = 0; i < num_iters; i++) {
        auto outputs = runner->run(input_tensors);
        if (!torch::allclose(ref_output_tensors[0], outputs[0])) {
          failed.store(true);
        }
      }
    });
  }

  for (auto& t : threads) {
    t.join();
  }

  ASSERT_FALSE(failed.load())
      << "One or more threads produced incorrect output";
}

// S638065 regression: with runtime constant folding, update_constant_buffer
// copies the new weights into the (inactive) constant buffer on the default
// stream (async device-to-device cudaMemcpy), while run_const_fold reads those
// constants on a separate stream. On ROCm the default stream is not implicitly
// ordered with other streams, so without a barrier the fold can read
// not-yet-copied weights and bake stale values into the folded constants.
// This exercises that exact edge: fold on a dedicated pool stream, then check
// the folded result against an independently computed reference. Each iteration
// uses fresh random weights so a stale read produces a detectably wrong output.
// The race is timing-dependent (the copy usually wins), hence the loop; a clean
// run depends on the cudaStreamSynchronize(0) at the end of
// update_constant_buffer (model_container.h). On NVIDIA the legacy default
// stream implicitly serializes, so this passes regardless.
void test_aoti_const_fold_separate_stream(
    bool use_stream_affinity,
    int num_iters) {
  torch::NoGradGuard no_grad;

  // Use the large (size=4096) model so the per-weight D2D copy is big enough to
  // still be in flight when the fold starts reading.
  std::string data_path =
      (std::filesystem::path(
           STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "large_data.pt")
           .string();
  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  const auto& model_so_path =
      data_loader.attr("model_so_path_use_runtime_constant_folding")
          .toStringRef();
  auto input_tensors = data_loader.attr("inputs").toTensorList().vec();
  const auto& x = input_tensors[0];
  const int64_t size = x.size(1);

  auto runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCuda>(
      model_so_path, use_stream_affinity ? 2 : 1);
  if (use_stream_affinity) {
    runner->set_use_stream_affinity(true);
  }

  // Dedicated non-blocking pool stream for folding, distinct from the default
  // stream that update_constant_buffer copies the weights on.
  c10::cuda::CUDAStream fold_stream = c10::cuda::getStreamFromPool();

  for (int i = 0; i < num_iters; ++i) {
    at::Tensor w_pre = at::randn({size, size}, x.options());
    at::Tensor w_add = at::randn({size, size}, x.options());
    // Independent reference, mirroring Net.forward in test.py.
    at::Tensor expected =
        at::matmul(x, at::relu(at::transpose(w_pre, 0, 1)) + w_add);

    torch::inductor::TensorConstantMap weight_map;
    weight_map.emplace("L__self___w_pre", &w_pre);
    weight_map.emplace("L__self___w_add", &w_add);

    // Copy weights into the inactive buffer (default stream), then fold on a
    // separate stream. We deliberately do NOT synchronize between these two:
    // ordering the copy before the fold is exactly what the fix must guarantee.
    runner->update_inactive_constant_buffer(weight_map);
    runner->run_const_fold(
        /* use_inactive = */ true,
        reinterpret_cast<AOTInductorStreamHandle>(fold_stream.stream()));
    if (use_stream_affinity) {
      ASSERT_GE(
          runner->get_stream_affinity_model_index_for_testing(
              fold_stream.stream()),
          0);
    }
    // Isolate the copy->fold edge (under test) from the fold->inference edge by
    // finishing the fold before swapping the freshly folded buffer in.
    fold_stream.synchronize();
    runner->swap_constant_buffer();

    std::vector<at::Tensor> actual;
    if (use_stream_affinity) {
      actual = runner->run(input_tensors, fold_stream.stream());
      fold_stream.synchronize();
    } else {
      actual = runner->run(input_tensors);
    }
    ASSERT_TRUE(torch::allclose(
        expected, actual[0], /* rtol = */ 1e-2, /* atol = */ 1e-2))
        << "iter " << i
        << ": folded constants reflect stale weights (S638065); the const-fold "
           "read the constant buffer before the default-stream weight copy "
           "completed";
  }
}

// run() holds the container's mutex shared, including while it waits for the
// GPU to finish with the only model instance, so the run() threads below keep
// the mutex continuously held. swap_constant_buffer() needs it exclusively.
void test_swap_constant_buffer_with_concurrent_runs() {
  torch::NoGradGuard no_grad;

  std::string data_path =
      (std::filesystem::path(
           STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "large_data.pt")
           .string();
  torch::jit::script::Module data_loader = torch::jit::load(data_path);
  const auto& model_so_path = data_loader.attr("model_so_path").toStringRef();
  auto input_tensors = data_loader.attr("inputs").toTensorList().vec();
  auto w_pre = data_loader.attr("w_pre").toTensor();
  auto w_add = data_loader.attr("w_add").toTensor();
  torch::inductor::TensorConstantMap real_map;
  real_map.emplace("L__self___w_pre", &w_pre);
  real_map.emplace("L__self___w_add", &w_add);

  auto runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCuda>(
      model_so_path, /* num_models = */ 1);
  runner->update_inactive_constant_buffer(real_map);

  const auto device_index = input_tensors[0].device().index();
  constexpr int num_threads = 4;
  std::atomic<int> num_warmed_up{0};
  std::atomic<bool> stop{false};
  std::vector<std::thread> threads;
  for (int t = 0; t < num_threads; t++) {
    threads.emplace_back([&]() {
      // The model loads and launches kernels through the CUDA driver API,
      // which needs a current context; a new thread has none until a runtime
      // call such as cudaSetDevice makes the primary context current.
      c10::cuda::set_device(device_index, /* force = */ true);
      runner->run(input_tensors);
      num_warmed_up.fetch_add(1);
      while (!stop.load()) {
        runner->run(input_tensors);
      }
    });
  }
  // Swap only once every thread has finished a run(), so that one-time setup
  // is done and the threads are contending for the model.
  while (num_warmed_up.load() < num_threads) {
    std::this_thread::yield();
  }

  auto swapped =
      std::async(std::launch::async, [&]() { runner->swap_constant_buffer(); });
  const bool swapped_while_running =
      swapped.wait_for(std::chrono::seconds(10)) == std::future_status::ready;
  stop.store(true);
  for (auto& t : threads) {
    t.join();
  }
  swapped.get();
  ASSERT_TRUE(swapped_while_running)
      << "swap_constant_buffer() waited for run() calls to stop";
}
#endif // USE_CUDA || USE_ROCM

// RECORD_USER_SCOPE callbacks are raw function pointers and cannot capture, so
// the names they see are collected here.
std::vector<std::string>& recordedScopeNames() {
  static std::vector<std::string> names;
  return names;
}

// Records the (event, ctx, succeeded) sequence an observer is handed, so a test
// can assert begin/end pairing and the failure signal.
class RecordingObserver : public torch::inductor::AOTIModelContainerObserver {
 public:
  struct Record {
    torch::inductor::AOTIContainerEvent event;
    size_t num_constants;
    bool use_inactive;
    bool succeeded;
  };

  void on_begin(
      torch::inductor::AOTIContainerEvent event,
      const torch::inductor::AOTIObserverContext& ctx) override {
    begins.push_back({event, ctx.num_constants, ctx.use_inactive, true});
    ++depth;
  }

  void on_end(
      torch::inductor::AOTIContainerEvent event,
      const torch::inductor::AOTIObserverContext& ctx,
      bool succeeded) override {
    ends.push_back({event, ctx.num_constants, ctx.use_inactive, succeeded});
    --depth;
  }

  std::vector<Record> begins;
  std::vector<Record> ends;
  int depth = 0;
};

void test_aoti_observer(const std::string& device) {
  torch::NoGradGuard no_grad;

  std::string data_path =
      (std::filesystem::path(STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "data.pt")
           .string();
  torch::jit::script::Module data_loader = torch::jit::load(data_path);

  std::string path_attr = "model_so_path_" + device;
  std::string inputs_attr = "inputs_" + device;
  std::string weights_attr = "w_pre_" + device;
  std::string add_attr = "w_add_" + device;
  const auto& model_so_path = data_loader.attr(path_attr.c_str()).toStringRef();
  auto input_tensors =
      data_loader.attr(inputs_attr.c_str()).toTensorList().vec();
  const auto& weight_tensors =
      data_loader.attr(weights_attr.c_str()).toTensor();
  const auto& add_tensors = data_loader.attr(add_attr.c_str()).toTensor();

  std::unique_ptr<torch::inductor::AOTIModelContainerRunner> runner;
  if (device == "cpu") {
    runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCpu>(
        model_so_path);
#if defined(USE_CUDA) || defined(USE_ROCM)
  } else if (device == "cuda") {
    runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCuda>(
        model_so_path);
#endif
  } else {
    FAIL() << "unsupported device: " << device;
  }

  // A null observer is accepted and never consumes the attach-once slot.
  runner->set_observer(nullptr);

  auto observer = std::make_shared<RecordingObserver>();
  runner->set_observer(observer);
  // Attaching a second observer is a usage error, not a silent swap: the
  // lifetime argument for the raw pointer on the hot path depends on the
  // owning shared_ptr never being replaced.
  EXPECT_THROW(
      runner->set_observer(std::make_shared<RecordingObserver>()), c10::Error);

  // Inference reports exactly one begin/end pair, and does not nest.
  runner->run(input_tensors);
  ASSERT_EQ(observer->begins.size(), 1);
  ASSERT_EQ(observer->ends.size(), 1);
  EXPECT_EQ(
      observer->begins[0].event,
      torch::inductor::AOTIContainerEvent::kInference);
  EXPECT_EQ(
      observer->ends[0].event, torch::inductor::AOTIContainerEvent::kInference);
  EXPECT_TRUE(observer->ends[0].succeeded);
  EXPECT_EQ(observer->depth, 0);

  // A successful constants update reports how many constants moved and which
  // buffer they went to.
  torch::inductor::TensorConstantMap real_map;
  real_map.emplace("L__self___w_pre", new at::Tensor(weight_tensors));
  real_map.emplace("L__self___w_add", new at::Tensor(add_tensors));
  runner->update_constant_buffer(
      real_map, /* use_inactive = */ false, /* check_full_update = */ false);
  ASSERT_FALSE(observer->ends.empty());
  EXPECT_EQ(
      observer->ends.back().event,
      torch::inductor::AOTIContainerEvent::kUpdateConstantBuffer);
  EXPECT_EQ(observer->ends.back().num_constants, real_map.size());
  EXPECT_FALSE(observer->ends.back().use_inactive);
  EXPECT_TRUE(observer->ends.back().succeeded);

  // A failing operation still reports on_end, with succeeded == false. Without
  // that flag a failed call would land in the observer's latency distribution
  // as if it had completed normally.
  //
  // Omitting a constant doesn't fail here (assert_all_constants warns and
  // skips TensorConstants); a wrong-device one does, on any build.
  const size_t ends_before = observer->ends.size();
  torch::inductor::TensorConstantMap wrong_device_map;
  wrong_device_map.emplace(
      "L__self___w_pre",
      new at::Tensor(at::randn({4, 4}, at::TensorOptions().device(at::kMeta))));
  try {
    runner->update_constant_buffer(
        wrong_device_map,
        /* use_inactive = */ false,
        /* check_full_update = */ true);
    ADD_FAILURE() << "expected a constant on the wrong device to throw";
  } catch (const std::exception&) {
  }
  ASSERT_EQ(observer->ends.size(), ends_before + 1);
  EXPECT_EQ(
      observer->ends.back().event,
      torch::inductor::AOTIContainerEvent::kUpdateConstantBuffer);
  EXPECT_FALSE(observer->ends.back().succeeded);

  // Every begin was paired with an end, including the one that threw.
  EXPECT_EQ(observer->begins.size(), observer->ends.size());
  EXPECT_EQ(observer->depth, 0);

  for (auto& pair : real_map) {
    delete pair.second;
  }
  for (auto& pair : wrong_device_map) {
    delete pair.second;
  }
}

// The observer is not the only way to see these events: the same call sites are
// bracketed with RECORD_USER_SCOPE, so a profiler (or any RecordFunction
// callback) picks them up without attaching an observer at all.
void test_aoti_record_function(const std::string& device) {
  torch::NoGradGuard no_grad;

  std::string data_path =
      (std::filesystem::path(STRINGIZE(CMAKE_CURRENT_BINARY_DIR)) / "data.pt")
           .string();
  torch::jit::script::Module data_loader = torch::jit::load(data_path);

  std::string path_attr = "model_so_path_" + device;
  std::string inputs_attr = "inputs_" + device;
  const auto& model_so_path = data_loader.attr(path_attr.c_str()).toStringRef();
  auto input_tensors =
      data_loader.attr(inputs_attr.c_str()).toTensorList().vec();

  std::unique_ptr<torch::inductor::AOTIModelContainerRunner> runner;
  if (device == "cpu") {
    runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCpu>(
        model_so_path);
#if defined(USE_CUDA) || defined(USE_ROCM)
  } else if (device == "cuda") {
    runner = std::make_unique<torch::inductor::AOTIModelContainerRunnerCuda>(
        model_so_path);
#endif
  } else {
    FAIL() << "unsupported device: " << device;
  }

  recordedScopeNames().clear();
  auto handle = at::addThreadLocalCallback(
      at::RecordFunctionCallback(
          [](const at::RecordFunction& fn)
              -> std::unique_ptr<at::ObserverContext> {
            recordedScopeNames().emplace_back(fn.name());
            return nullptr;
          })
          .scopes({at::RecordScope::USER_SCOPE}));

  // No observer attached: RecordFunction is the only consumer here.
  runner->run(input_tensors);
  runner->swap_constant_buffer();

  at::removeCallback(handle);

  // Deliberately not EXPECT_THAT/IsSupersetOf: test_aoti_inference links
  // gtest_main but not gmock, and the unordered-container matchers are not
  // header-only (HasSubstr, used elsewhere in this file, is).
  const auto& names = recordedScopeNames();
  const auto sawScope = [&names](const char* name) {
    return std::find(names.begin(), names.end(), name) != names.end();
  };
  EXPECT_TRUE(sawScope("AOTIModelContainerRunner::run"));
  EXPECT_TRUE(sawScope("AOTIModelContainerRunner::swap_constant_buffer"));

  // Swap back: the buffer above was never populated and fails for reasons
  // unrelated to callbacks.
  runner->swap_constant_buffer();

  // Callbacks are torn down again, so later tests are unaffected.
  const size_t recorded = recordedScopeNames().size();
  runner->run(input_tensors);
  EXPECT_EQ(recordedScopeNames().size(), recorded);
}

} // namespace

namespace torch::aot_inductor {

// Test fixture that ensures test data is generated once for all tests
class AotInductorTest : public ::testing::Test {
 public:
  // This runs once before all tests in this test suite
  static void SetUpTestSuite() {
    ensureTestDataGenerated();
  }
};

TEST_F(AotInductorTest, BasicTestCpu) {
  test_aoti("cpu", false);
}

TEST_F(AotInductorTest, BasicScriptTestCpu) {
  test_aoti_script("cpu");
}

TEST_F(AotInductorTest, BasicPackageLoaderTestCpu) {
  test_aoti_package_loader("cpu", false);
}

TEST_F(AotInductorTest, StreamAffinityRejectedForCpu) {
  test_stream_affinity_rejected_for_cpu();
}

TEST_F(AotInductorTest, StreamAffinityRejectedWithSingleThreaded) {
  test_stream_affinity_rejected_with_single_threaded();
}

TEST_F(AotInductorTest, StreamAffinityRejectedBySingleThreadedRunner) {
  test_stream_affinity_rejected_by_single_threaded_runner();
}

TEST_F(AotInductorTest, ExtractConstantsMapCpu) {
  test_aoti_extract_constants_map("cpu");
}

#ifdef USE_CUDA
TEST_F(AotInductorTest, BasicTestCuda) {
  test_aoti("cuda", true);
  test_aoti("cuda", false);
}

TEST_F(AotInductorTest, BasicScriptTestCuda) {
  test_aoti_script("cuda");
}

TEST_F(AotInductorTest, BasicPackageLoaderTestCuda) {
  test_aoti_package_loader("cuda", false);
}

TEST_F(AotInductorTest, BasicPackageLoaderTestMultiGpuCuda) {
  test_aoti_package_loader_multi_gpu("cuda", false);
}

TEST_F(AotInductorTest, UpdateUserManagedConstantsCuda) {
  test_aoti_user_managed_buffer();
}

TEST_F(AotInductorTest, RuntimeUpdateConstantsCuda) {
  test_aoti_constants_update("cuda", true);
}

TEST_F(AotInductorTest, UpdateConstantsCuda) {
  test_aoti_constants_update("cuda", false);
}

TEST_F(AotInductorTest, ExtractConstantsMapCuda) {
  test_aoti_extract_constants_map("cuda");
}

TEST_F(AotInductorTest, RuntimeUpdateInactiveConstantsCuda) {
  test_aoti_double_buffering("cuda", true);
}

TEST_F(AotInductorTest, UpdateInactiveConstantsCuda) {
  test_aoti_double_buffering("cuda", false);
}

TEST_F(AotInductorTest, UpdateInactiveConstantsWithTensorConstantsCuda) {
  test_aoti_double_buffering_with_tensor_constants();
}

TEST_F(AotInductorTest, FreeInactiveConstantBufferCuda) {
  test_aoti_free_buffer(false);
}

TEST_F(AotInductorTest, FreeInactiveConstantBufferRuntimeConstantFoldingCuda) {
  test_aoti_free_buffer(true);
}

TEST_F(AotInductorTest, MultiStreamTestCuda) {
  test_multi_cuda_streams("cuda");
}

TEST_F(AotInductorTest, CudaAllocTestCuda) {
  test_cuda_alloc_test();
}

TEST_F(AotInductorTest, ConcurrentRunConstFoldCuda) {
  test_concurrent_run_with_const_fold("cuda");
}
#endif

// Registered for ROCm as well as CUDA: the S638065 race only manifests on AMD
// (on NVIDIA the legacy default stream implicitly orders with the fold stream).
#if defined(USE_CUDA) || defined(USE_ROCM)
TEST_F(AotInductorTest, MultiStreamAffinityTestCuda) {
  test_cuda_stream_affinity_bindings("cuda");
}

TEST_F(AotInductorTest, ConcurrentSameStreamAffinityTestCuda) {
  test_concurrent_same_cuda_stream_affinity("cuda");
}

TEST_F(AotInductorTest, ConstFoldSeparateStreamCuda) {
  test_aoti_const_fold_separate_stream(false, 100);
}

TEST_F(AotInductorTest, ConstFoldSeparateStreamWithAffinityCuda) {
  test_aoti_const_fold_separate_stream(true, 100);
}

TEST_F(AotInductorTest, SwapConstantBufferWithConcurrentRunsCuda) {
  test_swap_constant_buffer_with_concurrent_runs();
}
#endif // USE_CUDA || USE_ROCM

TEST_F(AotInductorTest, ObserverCpu) {
  test_aoti_observer("cpu");
}

TEST_F(AotInductorTest, RecordFunctionCpu) {
  test_aoti_record_function("cpu");
}

#if defined(USE_CUDA) || defined(USE_ROCM)
TEST_F(AotInductorTest, ObserverCuda) {
  test_aoti_observer("cuda");
}

TEST_F(AotInductorTest, RecordFunctionCuda) {
  test_aoti_record_function("cuda");
}
#endif // USE_CUDA || USE_ROCM

} // namespace torch::aot_inductor