# In both open-source and fbcode builds, these are generated into
# torch/csrc/{autgrad,jit}/generated.i
GENERATED_CPP = [
    "autograd/generated/Functions.cpp",
    "autograd/generated/VariableType_0.cpp",
    "autograd/generated/VariableType_1.cpp",
    "autograd/generated/VariableType_2.cpp",
    "autograd/generated/VariableType_3.cpp",
    "autograd/generated/VariableType_4.cpp",
    "autograd/generated/TraceType_0.cpp",
    "autograd/generated/TraceType_1.cpp",
    "autograd/generated/TraceType_2.cpp",
    "autograd/generated/TraceType_3.cpp",
    "autograd/generated/TraceType_4.cpp",
    "autograd/generated/ADInplaceOrViewType_0.cpp",
    "autograd/generated/ADInplaceOrViewType_1.cpp",
    "autograd/generated/python_functions.cpp",
    "autograd/generated/python_nn_functions.cpp",
    "autograd/generated/python_fft_functions.cpp",
    "autograd/generated/python_linalg_functions.cpp",
    "autograd/generated/python_special_functions.cpp",
    "autograd/generated/python_torch_functions.cpp",
    "autograd/generated/python_variable_methods.cpp",
]

# NVFuser runtime library
libtorch_nvfuser_runtime_sources = [
    "torch/csrc/jit/codegen/cuda/runtime/block_reduction.cu",
    "torch/csrc/jit/codegen/cuda/runtime/broadcast.cu",
    "torch/csrc/jit/codegen/cuda/runtime/fp16_support.cu",
    "torch/csrc/jit/codegen/cuda/runtime/grid_reduction.cu",
    "torch/csrc/jit/codegen/cuda/runtime/helpers.cu",
    "torch/csrc/jit/codegen/cuda/runtime/random_numbers.cu",
    "torch/csrc/jit/codegen/cuda/runtime/tensor.cu",
    "aten/src/ATen/cuda/detail/PhiloxCudaStateRaw.cuh",
    "aten/src/ATen/cuda/detail/UnpackRaw.cuh",
]

libtorch_nvfuser_generated_headers = ["{}.h".format(name.split("/")[-1].split(".")[0]) for name in libtorch_nvfuser_runtime_sources]

def libtorch_generated_sources(gencode_pattern):
    return [gencode_pattern.format(name) for name in [
        "autograd/generated/Functions.cpp",
        "autograd/generated/VariableType_0.cpp",
        "autograd/generated/VariableType_1.cpp",
        "autograd/generated/VariableType_2.cpp",
        "autograd/generated/VariableType_3.cpp",
        "autograd/generated/VariableType_4.cpp",
        "autograd/generated/TraceType_0.cpp",
        "autograd/generated/TraceType_1.cpp",
        "autograd/generated/TraceType_2.cpp",
        "autograd/generated/TraceType_3.cpp",
        "autograd/generated/TraceType_4.cpp",
        "autograd/generated/ADInplaceOrViewType_0.cpp",
        "autograd/generated/ADInplaceOrViewType_1.cpp",
    ]]

# copied from https://github.com/pytorch/pytorch/blob/f99a693cd9ff7a9b5fdc71357dac66b8192786d3/aten/src/ATen/core/CMakeLists.txt
jit_core_headers = [
    "torch/csrc/utils/memory.h",
    "torch/csrc/WindowsTorchApiMacro.h",
    "torch/csrc/jit/frontend/source_range.h",
    "torch/csrc/jit/serialization/callstack_debug_info_serialization.h",
    "torch/csrc/jit/serialization/source_range_serialization.h",
    "torch/csrc/jit/frontend/lexer.h",
    "torch/csrc/jit/frontend/strtod.h",
    "torch/csrc/jit/frontend/parser_constants.h",
    "torch/csrc/jit/frontend/function_schema_parser.h",
    "torch/csrc/jit/frontend/parse_string_literal.h",
    "torch/csrc/jit/frontend/schema_type_parser.h",
    "torch/csrc/jit/frontend/error_report.h",
    "torch/csrc/jit/frontend/tree.h",
    "torch/custom_class.h",
    "torch/custom_class_detail.h",
    "torch/library.h",
]

jit_core_sources = [
    "torch/csrc/jit/frontend/error_report.cpp",
    "torch/csrc/jit/frontend/function_schema_parser.cpp",
    "torch/csrc/jit/frontend/lexer.cpp",
    "torch/csrc/jit/frontend/schema_type_parser.cpp",
    "torch/csrc/jit/frontend/strtod.cpp",
    "torch/csrc/jit/frontend/source_range.cpp",
]

# copied from https://github.com/pytorch/pytorch/blob/0bde610c14b92d351b968a0228df29e92442b1cc/torch/CMakeLists.txt
# There are some common files used in both internal lite-interpreter and full-jit. Making a separate
# list for the shared files.

core_sources_common = [
    "torch/csrc/autograd/profiler_utils.cpp",
    "torch/csrc/autograd/autograd_meta.cpp",
    "torch/csrc/autograd/forward_grad.cpp",
    "torch/csrc/jit/frontend/edit_distance.cpp",
    "torch/csrc/jit/frontend/string_to_type.cpp",
    "torch/csrc/jit/mobile/type_parser.cpp",
    "torch/csrc/jit/mobile/runtime_compatibility.cpp",
    "torch/csrc/jit/runtime/instruction.cpp",
    "torch/csrc/jit/runtime/jit_exception.cpp",
    "torch/csrc/jit/runtime/operator.cpp",
    "torch/csrc/jit/runtime/print_handler.cpp",
    "torch/csrc/jit/runtime/slice_indices_adjust.cpp",
    "torch/csrc/jit/runtime/register_ops_utils.cpp",
    "torch/csrc/jit/runtime/vararg_functions.cpp",
    "torch/csrc/jit/serialization/import_read.cpp",
    "torch/csrc/jit/serialization/unpickler.cpp",
]

libtorch_sources_common = core_sources_common

# The profilers are not needed in the lite interpreter build.
libtorch_profiler_sources = [
    "torch/csrc/autograd/profiler_legacy.cpp",
    "torch/csrc/autograd/profiler_kineto.cpp",
]

core_trainer_sources = [
    "torch/csrc/autograd/anomaly_mode.cpp",
    "torch/csrc/autograd/autograd.cpp",
    "torch/csrc/autograd/cpp_hook.cpp",
    "torch/csrc/autograd/custom_function.cpp",
    "torch/csrc/autograd/engine.cpp",
    "torch/csrc/autograd/function.cpp",
    "torch/csrc/autograd/function_hook.cpp",
    "torch/csrc/autograd/functions/accumulate_grad.cpp",
    "torch/csrc/autograd/functions/basic_ops.cpp",
    "torch/csrc/autograd/functions/tensor.cpp",
    "torch/csrc/autograd/functions/utils.cpp",
    "torch/csrc/autograd/input_buffer.cpp",
    "torch/csrc/autograd/record_function_ops.cpp",
    "torch/csrc/autograd/saved_variable.cpp",
    "torch/csrc/autograd/variable.cpp",
    "torch/csrc/jit/frontend/name_mangler.cpp",
    "torch/csrc/jit/ir/type_hashing.cpp",
    "torch/csrc/jit/serialization/pickler.cpp",
    "torch/csrc/jit/serialization/type_name_uniquer.cpp",
]

core_sources_full_mobile = [
    "torch/csrc/jit/api/function_impl.cpp",
    "torch/csrc/jit/api/module.cpp",
    "torch/csrc/jit/api/object.cpp",
    "torch/csrc/jit/backends/backend_debug_handler.cpp",
    "torch/csrc/jit/backends/backend_debug_info.cpp",
    "torch/csrc/jit/backends/backend_detail.cpp",
    "torch/csrc/jit/backends/backend_interface.cpp",
    "torch/csrc/jit/backends/backend_resolver.cpp",
    "torch/csrc/jit/codegen/fuser/codegen.cpp",
    "torch/csrc/jit/codegen/fuser/compiler.cpp",
    "torch/csrc/jit/codegen/fuser/executor.cpp",
    "torch/csrc/jit/codegen/fuser/fallback.cpp",
    "torch/csrc/jit/codegen/fuser/interface.cpp",
    "torch/csrc/jit/codegen/fuser/kernel_cache.cpp",
    "torch/csrc/jit/frontend/builtin_functions.cpp",
    "torch/csrc/jit/frontend/versioned_symbols.cpp",
    "torch/csrc/jit/frontend/canonicalize_modified_loop.cpp",
    "torch/csrc/jit/frontend/convert_to_ssa.cpp",
    "torch/csrc/jit/frontend/exit_transforms.cpp",
    "torch/csrc/jit/frontend/inline_loop_condition.cpp",
    "torch/csrc/jit/frontend/ir_emitter.cpp",
    "torch/csrc/jit/frontend/parser.cpp",
    "torch/csrc/jit/frontend/schema_matching.cpp",
    "torch/csrc/jit/frontend/script_type_parser.cpp",
    "torch/csrc/jit/frontend/sugared_value.cpp",
    "torch/csrc/jit/frontend/tracer.cpp",
    "torch/csrc/jit/ir/alias_analysis.cpp",
    "torch/csrc/jit/ir/attributes.cpp",
    "torch/csrc/jit/ir/constants.cpp",
    "torch/csrc/jit/ir/ir.cpp",
    "torch/csrc/jit/ir/irparser.cpp",
    "torch/csrc/jit/ir/node_hashing.cpp",
    "torch/csrc/jit/ir/scope.cpp",
    "torch/csrc/jit/ir/subgraph_matcher.cpp",
    "torch/csrc/jit/jit_log.cpp",
    "torch/csrc/jit/jit_opt_limit.cpp",
    "torch/csrc/jit/passes/annotate_warns.cpp",
    "torch/csrc/jit/passes/bailout_graph.cpp",
    "torch/csrc/jit/passes/batch_mm.cpp",
    "torch/csrc/jit/passes/canonicalize.cpp",
    "torch/csrc/jit/passes/canonicalize_graph_fuser_ops.cpp",
    "torch/csrc/jit/passes/clear_profiling.cpp",
    "torch/csrc/jit/passes/clear_undefinedness.cpp",
    "torch/csrc/jit/passes/common_subexpression_elimination.cpp",
    "torch/csrc/jit/passes/concat_opt.cpp",
    "torch/csrc/jit/passes/constant_pooling.cpp",
    "torch/csrc/jit/passes/constant_propagation.cpp",
    "torch/csrc/jit/passes/restore_mutation.cpp",
    "torch/csrc/jit/passes/create_autodiff_subgraphs.cpp",
    "torch/csrc/jit/passes/dead_code_elimination.cpp",
    "torch/csrc/jit/passes/remove_redundant_profiles.cpp",
    "torch/csrc/jit/passes/remove_exceptions.cpp",
    "torch/csrc/jit/passes/decompose_ops.cpp",
    "torch/csrc/jit/passes/erase_number_types.cpp",
    "torch/csrc/jit/passes/fixup_trace_scope_blocks.cpp",
    "torch/csrc/jit/passes/freeze_module.cpp",
    "torch/csrc/jit/passes/fuse_linear.cpp",
    "torch/csrc/jit/passes/fuse_relu.cpp",
    "torch/csrc/jit/passes/graph_fuser.cpp",
    "torch/csrc/jit/passes/graph_rewrite_helper.cpp",
    "torch/csrc/jit/passes/guard_elimination.cpp",
    "torch/csrc/jit/passes/hoist_conv_packed_params.cpp",
    "torch/csrc/jit/passes/inline_autodiff_subgraphs.cpp",
    "torch/csrc/jit/passes/inline_forked_closures.cpp",
    "torch/csrc/jit/passes/inline_fork_wait.cpp",
    "torch/csrc/jit/passes/inliner.cpp",
    "torch/csrc/jit/passes/inplace_check.cpp",
    "torch/csrc/jit/passes/insert_guards.cpp",
    "torch/csrc/jit/passes/lift_closures.cpp",
    "torch/csrc/jit/passes/liveness.cpp",
    "torch/csrc/jit/passes/loop_unrolling.cpp",
    "torch/csrc/jit/passes/lower_grad_of.cpp",
    "torch/csrc/jit/passes/lower_tuples.cpp",
    "torch/csrc/jit/passes/normalize_ops.cpp",
    "torch/csrc/jit/passes/peephole_list_idioms.cpp",
    "torch/csrc/jit/passes/value_refinement_utils.cpp",
    "torch/csrc/jit/passes/peephole_alias_sensitive.cpp",
    "torch/csrc/jit/passes/pass_manager.cpp",
    "torch/csrc/jit/passes/peephole.cpp",
    "torch/csrc/jit/passes/peephole_non_tensor.cpp",
    "torch/csrc/jit/passes/create_functional_graphs.cpp",
    "torch/csrc/jit/passes/remove_mutation.cpp",
    "torch/csrc/jit/passes/prepack_folding.cpp",
    "torch/csrc/jit/passes/fold_conv_bn.cpp",
    "torch/csrc/jit/passes/frozen_conv_add_relu_fusion.cpp",
    "torch/csrc/jit/passes/frozen_conv_folding.cpp",
    "torch/csrc/jit/passes/frozen_ops_to_mkldnn.cpp",
    "torch/csrc/jit/passes/frozen_graph_optimizations.cpp",
    "torch/csrc/jit/passes/remove_expands.cpp",
    "torch/csrc/jit/passes/remove_dropout.cpp",
    "torch/csrc/jit/passes/requires_grad_analysis.cpp",
    "torch/csrc/jit/passes/shape_analysis.cpp",
    "torch/csrc/jit/passes/integer_value_refinement.cpp",
    "torch/csrc/jit/passes/symbolic_shape_analysis.cpp",
    "torch/csrc/jit/passes/specialize_autogradzero.cpp",
    "torch/csrc/jit/passes/update_differentiable_graph_requires_grad.cpp",
    "torch/csrc/jit/passes/subgraph_rewrite.cpp",
    "torch/csrc/jit/passes/tensorexpr_fuser.cpp",
    "torch/csrc/jit/passes/utils/memory_dag.cpp",
    "torch/csrc/jit/passes/utils/subgraph_utils.cpp",
    "torch/csrc/jit/passes/xnnpack_rewrite.cpp",
    "torch/csrc/jit/passes/vulkan_rewrite.cpp",
    "torch/csrc/jit/passes/metal_rewrite.cpp",
    "torch/csrc/jit/passes/quantization/helper.cpp",
    "torch/csrc/jit/passes/quantization/quantization_type.cpp",
    "torch/csrc/jit/passes/quantization/insert_observers.cpp",
    "torch/csrc/jit/passes/quantization/insert_quant_dequant.cpp",
    "torch/csrc/jit/passes/quantization/dedup_module_uses.cpp",
    "torch/csrc/jit/passes/quantization/finalize.cpp",
    "torch/csrc/jit/passes/quantization/fusion_passes.cpp",
    "torch/csrc/jit/python/update_graph_executor_opt.cpp",
    "torch/csrc/jit/runtime/argument_spec.cpp",
    "torch/csrc/jit/runtime/autodiff.cpp",
    "torch/csrc/jit/runtime/graph_executor.cpp",
    "torch/csrc/jit/runtime/interpreter/frame.cpp",
    "torch/csrc/jit/runtime/interpreter/preprocess_graph.cpp",
    "torch/csrc/jit/runtime/interpreter.cpp",
    "torch/csrc/jit/runtime/logging.cpp",
    "torch/csrc/jit/runtime/profiling_graph_executor_impl.cpp",
    "torch/csrc/jit/runtime/profiling_record.cpp",
    "torch/csrc/jit/runtime/script_profile.cpp",
    "torch/csrc/jit/runtime/symbolic_script.cpp",
    "torch/csrc/jit/runtime/symbolic_shape_registry.cpp",
    "torch/csrc/jit/serialization/callstack_debug_info_serialization.cpp",
    "torch/csrc/jit/serialization/import.cpp",
    "torch/csrc/jit/serialization/import_export_helpers.cpp",
    "torch/csrc/jit/serialization/import_source.cpp",
    "torch/csrc/jit/serialization/pickle.cpp",
    "torch/csrc/jit/serialization/python_print.cpp",
    "torch/csrc/jit/serialization/source_range_serialization.cpp",
    "torch/csrc/jit/tensorexpr/block_codegen.cpp",
    "torch/csrc/jit/tensorexpr/bounds_inference.cpp",
    "torch/csrc/jit/tensorexpr/bounds_overlap.cpp",
    "torch/csrc/jit/tensorexpr/codegen.cpp",
    "torch/csrc/jit/tensorexpr/cpp_codegen.cpp",
    "torch/csrc/jit/tensorexpr/eval.cpp",
    "torch/csrc/jit/tensorexpr/expr.cpp",
    "torch/csrc/jit/tensorexpr/external_functions_registry.cpp",
    "torch/csrc/jit/tensorexpr/hash_provider.cpp",
    "torch/csrc/jit/tensorexpr/intrinsic_symbols.cpp",
    "torch/csrc/jit/tensorexpr/ir.cpp",
    "torch/csrc/jit/tensorexpr/ir_mutator.cpp",
    "torch/csrc/jit/tensorexpr/ir_printer.cpp",
    "torch/csrc/jit/tensorexpr/ir_simplifier.cpp",
    "torch/csrc/jit/tensorexpr/ir_verifier.cpp",
    "torch/csrc/jit/tensorexpr/ir_visitor.cpp",
    "torch/csrc/jit/tensorexpr/kernel.cpp",
    "torch/csrc/jit/tensorexpr/llvm_codegen.cpp",
    "torch/csrc/jit/tensorexpr/llvm_jit.cpp",
    "torch/csrc/jit/tensorexpr/loopnest.cpp",
    "torch/csrc/jit/tensorexpr/mem_arena.cpp",
    "torch/csrc/jit/tensorexpr/mem_dependency_checker.cpp",
    "torch/csrc/jit/tensorexpr/operators/conv2d.cpp",
    "torch/csrc/jit/tensorexpr/reduction.cpp",
    "torch/csrc/jit/tensorexpr/registerizer.cpp",
    "torch/csrc/jit/tensorexpr/tensor.cpp",
    "torch/csrc/jit/tensorexpr/types.cpp",
    "torch/csrc/jit/tensorexpr/unique_name_manager.cpp",
    "torch/csrc/jit/testing/file_check.cpp",
    "torch/csrc/jit/testing/hooks_for_testing.cpp",
    "torch/csrc/utils/tensor_flatten.cpp",
    "torch/csrc/utils/variadic.cpp",
] + libtorch_profiler_sources

core_sources_full = core_sources_full_mobile + [
    "torch/csrc/jit/runtime/static/fusion.cpp",
    "torch/csrc/jit/runtime/static/impl.cpp",
    "torch/csrc/jit/runtime/static/ops.cpp",
    "torch/csrc/jit/runtime/static/passes.cpp",
    "torch/csrc/jit/tensorexpr/external_functions.cpp",
    "torch/csrc/jit/tensorexpr/external_functions_codegen.cpp",
]

libtorch_core_sources = sorted(core_sources_common + core_sources_full + core_trainer_sources)

libtorch_distributed_sources = [
    "torch/csrc/distributed/autograd/autograd.cpp",
    "torch/csrc/distributed/autograd/utils.cpp",
    "torch/csrc/distributed/autograd/context/container.cpp",
    "torch/csrc/distributed/autograd/context/context.cpp",
    "torch/csrc/distributed/autograd/engine/dist_engine.cpp",
    "torch/csrc/distributed/autograd/functions/recvrpc_backward.cpp",
    "torch/csrc/distributed/autograd/functions/sendrpc_backward.cpp",
    "torch/csrc/distributed/autograd/rpc_messages/autograd_metadata.cpp",
    "torch/csrc/distributed/autograd/rpc_messages/propagate_gradients_req.cpp",
    "torch/csrc/distributed/autograd/rpc_messages/propagate_gradients_resp.cpp",
    "torch/csrc/distributed/autograd/rpc_messages/cleanup_autograd_context_req.cpp",
    "torch/csrc/distributed/autograd/rpc_messages/cleanup_autograd_context_resp.cpp",
    "torch/csrc/distributed/autograd/rpc_messages/rpc_with_autograd.cpp",
    "torch/csrc/distributed/autograd/rpc_messages/rpc_with_profiling_req.cpp",
    "torch/csrc/distributed/autograd/rpc_messages/rpc_with_profiling_resp.cpp",
    "torch/csrc/distributed/autograd/rpc_messages/rref_backward_req.cpp",
    "torch/csrc/distributed/autograd/rpc_messages/rref_backward_resp.cpp",
    "torch/csrc/distributed/rpc/message.cpp",
    "torch/csrc/distributed/rpc/profiler/remote_profiler_manager.cpp",
    "torch/csrc/distributed/rpc/profiler/server_process_global_profiler.cpp",
    "torch/csrc/distributed/rpc/python_call.cpp",
    "torch/csrc/distributed/rpc/python_remote_call.cpp",
    "torch/csrc/distributed/rpc/python_resp.cpp",
    "torch/csrc/distributed/rpc/request_callback.cpp",
    "torch/csrc/distributed/rpc/request_callback_no_python.cpp",
    "torch/csrc/distributed/rpc/rpc_agent.cpp",
    "torch/csrc/distributed/rpc/rref_context.cpp",
    "torch/csrc/distributed/rpc/rref_impl.cpp",
    "torch/csrc/distributed/rpc/rref_proto.cpp",
    "torch/csrc/distributed/rpc/script_call.cpp",
    "torch/csrc/distributed/rpc/script_remote_call.cpp",
    "torch/csrc/distributed/rpc/script_resp.cpp",
    "torch/csrc/distributed/rpc/torchscript_functions.cpp",
    "torch/csrc/distributed/rpc/types.cpp",
    "torch/csrc/distributed/rpc/utils.cpp",
    "torch/csrc/distributed/rpc/metrics/registry.cpp",
]

jit_sources_full = [
    "torch/csrc/jit/codegen/cuda/interface.cpp",
    "torch/csrc/jit/passes/lower_graph.cpp",
    "torch/csrc/jit/runtime/register_c10_ops.cpp",
    "torch/csrc/jit/runtime/register_prim_ops.cpp",
    "torch/csrc/jit/runtime/register_prim_ops_fulljit.cpp",
    "torch/csrc/jit/runtime/register_special_ops.cpp",
    "torch/csrc/jit/passes/remove_inplace_ops.cpp",
    "torch/csrc/jit/passes/utils/check_alias_annotation.cpp",
]

libtorch_core_jit_sources = sorted(jit_sources_full)

torch_mobile_core = [
    # backend_debug_info.cpp provides
    # __torch__.torch.classes.backend.BackendDebugInfo class
    # This should not be needed eventually.
    # TODO: Remove this dependency
    "torch/csrc/jit/backends/backend_debug_info.cpp",
    "torch/csrc/jit/mobile/function.cpp",
    "torch/csrc/jit/mobile/import.cpp",
    "torch/csrc/jit/mobile/interpreter.cpp",
    "torch/csrc/jit/mobile/model_compatibility.cpp",
    "torch/csrc/jit/mobile/module.cpp",
    "torch/csrc/jit/mobile/observer.cpp",
    "torch/csrc/jit/runtime/register_prim_ops.cpp",
    "torch/csrc/jit/runtime/register_special_ops.cpp",
]

libtorch_lite_eager_symbolication = [
    "torch/csrc/jit/frontend/source_range.cpp",
    "torch/csrc/jit/ir/scope.cpp",
    "torch/csrc/jit/mobile/debug_info.cpp",
    "torch/csrc/jit/serialization/callstack_debug_info_serialization.cpp",
    "torch/csrc/jit/serialization/source_range_serialization.cpp",
    # Later we can split serialization and deserialization logic
    # to have better separation within build and only build relevant parts.
    "torch/csrc/jit/serialization/pickle.cpp",
    "torch/csrc/jit/serialization/pickler.cpp",
    "torch/csrc/jit/serialization/unpickler.cpp",
]

# TODO: core_trainer_sources is not necessary for libtorch lite
libtorch_lite_cmake_sources = sorted(core_trainer_sources + core_sources_common + torch_mobile_core)

libtorch_cmake_sources = libtorch_core_sources + libtorch_core_jit_sources

libtorch_extra_sources = libtorch_core_jit_sources + [
    "torch/csrc/autograd/TraceTypeManual.cpp",
    "torch/csrc/autograd/VariableTypeManual.cpp",
    "torch/csrc/autograd/FunctionsManual.cpp",
    "torch/csrc/jit/api/module_save.cpp",
    "torch/csrc/jit/codegen/fuser/cpu/fused_kernel.cpp",
    "torch/csrc/jit/mobile/backport.cpp",
    "torch/csrc/jit/mobile/backport_manager.cpp",
    # To be included for eager symbolication in lite interpreter
    # when it is built in libtorch
    "torch/csrc/jit/mobile/debug_info.cpp",
    "torch/csrc/jit/mobile/function.cpp",
    "torch/csrc/jit/mobile/import.cpp",
    "torch/csrc/jit/mobile/import_data.cpp",
    "torch/csrc/jit/mobile/interpreter.cpp",
    "torch/csrc/jit/mobile/model_compatibility.cpp",
    "torch/csrc/jit/mobile/module.cpp",
    "torch/csrc/jit/mobile/nnc/context.cpp",
    "torch/csrc/jit/mobile/nnc/registry.cpp",
    "torch/csrc/jit/mobile/observer.cpp",
    "torch/csrc/jit/mobile/train/export_data.cpp",
    "torch/csrc/jit/mobile/train/optim/sgd.cpp",
    "torch/csrc/jit/mobile/train/random.cpp",
    "torch/csrc/jit/mobile/train/sequential.cpp",
    "torch/csrc/jit/serialization/onnx.cpp",
    "torch/csrc/jit/serialization/export.cpp",
    "torch/csrc/jit/serialization/export_module.cpp",
    "torch/csrc/jit/serialization/import_legacy.cpp",
    "torch/csrc/utils/byte_order.cpp",
    "torch/csrc/utils/out_types.cpp",
]

def libtorch_sources(gencode_pattern = ":generate-code[{}]"):
    return libtorch_generated_sources(gencode_pattern) + libtorch_core_sources + libtorch_distributed_sources + libtorch_extra_sources

libtorch_cuda_core_sources = [
    "torch/csrc/CudaIPCTypes.cpp",
    "torch/csrc/cuda/comm.cpp",
    "torch/csrc/jit/codegen/fuser/cuda/fused_kernel.cpp",
    "torch/csrc/autograd/profiler_cuda.cpp",
    "torch/csrc/autograd/functions/comm.cpp",
    "torch/csrc/jit/codegen/cuda/arith.cpp",
    "torch/csrc/jit/codegen/cuda/compute_at.cpp",
    "torch/csrc/jit/codegen/cuda/codegen.cpp",
    "torch/csrc/jit/codegen/cuda/dispatch.cpp",
    "torch/csrc/jit/codegen/cuda/expr_evaluator.cpp",
    "torch/csrc/jit/codegen/cuda/executor.cpp",
    "torch/csrc/jit/codegen/cuda/executor_kernel_arg.cpp",
    "torch/csrc/jit/codegen/cuda/executor_launch_params.cpp",
    "torch/csrc/jit/codegen/cuda/executor_utils.cpp",
    "torch/csrc/jit/codegen/cuda/fusion.cpp",
    "torch/csrc/jit/codegen/cuda/graph_fuser.cpp",
    "torch/csrc/jit/codegen/cuda/index_compute.cpp",
    "torch/csrc/jit/codegen/cuda/instrumentation.cpp",
    "torch/csrc/jit/codegen/cuda/ir_base_nodes.cpp",
    "torch/csrc/jit/codegen/cuda/ir_cloner.cpp",
    "torch/csrc/jit/codegen/cuda/ir_graphviz.cpp",
    "torch/csrc/jit/codegen/cuda/ir_nodes.cpp",
    "torch/csrc/jit/codegen/cuda/ir_iostream.cpp",
    "torch/csrc/jit/codegen/cuda/iter_visitor.cpp",
    "torch/csrc/jit/codegen/cuda/kernel.cpp",
    "torch/csrc/jit/codegen/cuda/kernel_cache.cpp",
    "torch/csrc/jit/codegen/cuda/kernel_ir.cpp",
    "torch/csrc/jit/codegen/cuda/kernel_ir_builder.cpp",
    "torch/csrc/jit/codegen/cuda/kernel_ir_printer.cpp",
    "torch/csrc/jit/codegen/cuda/lower_index.cpp",
    "torch/csrc/jit/codegen/cuda/lower_loops.cpp",
    "torch/csrc/jit/codegen/cuda/lower_alias_memory.cpp",
    "torch/csrc/jit/codegen/cuda/lower_insert_syncs.cpp",
    "torch/csrc/jit/codegen/cuda/lower_unroll.cpp",
    "torch/csrc/jit/codegen/cuda/lower_thread_predicate.cpp",
    "torch/csrc/jit/codegen/cuda/lower_utils.cpp",
    "torch/csrc/jit/codegen/cuda/lower_validation.cpp",
    "torch/csrc/jit/codegen/cuda/lower2device.cpp",
    "torch/csrc/jit/codegen/cuda/manager.cpp",
    "torch/csrc/jit/codegen/cuda/mutator.cpp",
    "torch/csrc/jit/codegen/cuda/parser.cpp",
    "torch/csrc/jit/codegen/cuda/partition.cpp",
    "torch/csrc/jit/codegen/cuda/predicate_compute.cpp",
    "torch/csrc/jit/codegen/cuda/register_interface.cpp",
    "torch/csrc/jit/codegen/cuda/scheduler.cpp",
    "torch/csrc/jit/codegen/cuda/shape_inference.cpp",
    "torch/csrc/jit/codegen/cuda/tensor_view.cpp",
    "torch/csrc/jit/codegen/cuda/transform_iter.cpp",
    "torch/csrc/jit/codegen/cuda/transform_replay.cpp",
    "torch/csrc/jit/codegen/cuda/transform_rfactor.cpp",
    "torch/csrc/jit/codegen/cuda/type.cpp",
    "torch/csrc/jit/tensorexpr/cuda_codegen.cpp",
    "torch/csrc/jit/runtime/register_cuda_ops.cpp",
]

libtorch_cuda_sources = libtorch_cuda_core_sources + [
    "torch/csrc/cuda/nccl.cpp",
]

torch_cpp_srcs = [
    "torch/csrc/api/src/cuda.cpp",  # this just forwards stuff, no real CUDA
    "torch/csrc/api/src/data/datasets/mnist.cpp",
    "torch/csrc/api/src/data/samplers/distributed.cpp",
    "torch/csrc/api/src/data/samplers/random.cpp",
    "torch/csrc/api/src/data/samplers/sequential.cpp",
    "torch/csrc/api/src/data/samplers/stream.cpp",
    "torch/csrc/api/src/enum.cpp",
    "torch/csrc/api/src/jit.cpp",
    "torch/csrc/api/src/serialize.cpp",
    "torch/csrc/api/src/nn/init.cpp",
    "torch/csrc/api/src/nn/module.cpp",
    "torch/csrc/api/src/nn/modules/_functions.cpp",
    "torch/csrc/api/src/nn/modules/activation.cpp",
    "torch/csrc/api/src/nn/modules/adaptive.cpp",
    "torch/csrc/api/src/nn/modules/batchnorm.cpp",
    "torch/csrc/api/src/nn/modules/normalization.cpp",
    "torch/csrc/api/src/nn/modules/instancenorm.cpp",
    "torch/csrc/api/src/nn/modules/conv.cpp",
    "torch/csrc/api/src/nn/modules/dropout.cpp",
    "torch/csrc/api/src/nn/modules/distance.cpp",
    "torch/csrc/api/src/nn/modules/embedding.cpp",
    "torch/csrc/api/src/nn/modules/fold.cpp",
    "torch/csrc/api/src/nn/modules/linear.cpp",
    "torch/csrc/api/src/nn/modules/loss.cpp",
    "torch/csrc/api/src/nn/modules/padding.cpp",
    "torch/csrc/api/src/nn/modules/pixelshuffle.cpp",
    "torch/csrc/api/src/nn/modules/pooling.cpp",
    "torch/csrc/api/src/nn/modules/rnn.cpp",
    "torch/csrc/api/src/nn/modules/upsampling.cpp",
    "torch/csrc/api/src/nn/modules/transformer.cpp",
    "torch/csrc/api/src/nn/modules/container/functional.cpp",
    "torch/csrc/api/src/nn/options/activation.cpp",
    "torch/csrc/api/src/nn/options/adaptive.cpp",
    "torch/csrc/api/src/nn/options/batchnorm.cpp",
    "torch/csrc/api/src/nn/options/conv.cpp",
    "torch/csrc/api/src/nn/options/dropout.cpp",
    "torch/csrc/api/src/nn/options/instancenorm.cpp",
    "torch/csrc/api/src/nn/options/linear.cpp",
    "torch/csrc/api/src/nn/options/normalization.cpp",
    "torch/csrc/api/src/nn/options/embedding.cpp",
    "torch/csrc/api/src/nn/options/padding.cpp",
    "torch/csrc/api/src/nn/options/pooling.cpp",
    "torch/csrc/api/src/nn/options/rnn.cpp",
    "torch/csrc/api/src/nn/options/vision.cpp",
    "torch/csrc/api/src/nn/options/transformer.cpp",
    "torch/csrc/api/src/optim/adagrad.cpp",
    "torch/csrc/api/src/optim/adam.cpp",
    "torch/csrc/api/src/optim/adamw.cpp",
    "torch/csrc/api/src/optim/lbfgs.cpp",
    "torch/csrc/api/src/optim/optimizer.cpp",
    "torch/csrc/api/src/optim/rmsprop.cpp",
    "torch/csrc/api/src/optim/serialize.cpp",
    "torch/csrc/api/src/optim/sgd.cpp",
    "torch/csrc/api/src/optim/schedulers/lr_scheduler.cpp",
    "torch/csrc/api/src/optim/schedulers/step_lr.cpp",
    "torch/csrc/api/src/serialize/input-archive.cpp",
    "torch/csrc/api/src/serialize/output-archive.cpp",
    "torch/csrc/utils/crash_handler.cpp",
]

libtorch_python_cuda_core_sources = [
    "torch/csrc/cuda/Event.cpp",
    "torch/csrc/cuda/Module.cpp",
    "torch/csrc/cuda/python_comm.cpp",
    "torch/csrc/cuda/Storage.cpp",
    "torch/csrc/cuda/Stream.cpp",
    "torch/csrc/cuda/Graph.cpp",
    "torch/csrc/cuda/serialization.cpp",
    "torch/csrc/cuda/shared/cudart.cpp",
    "torch/csrc/cuda/shared/nvtx.cpp",
    "torch/csrc/cuda/utils.cpp",
]

libtorch_python_cuda_sources = libtorch_python_cuda_core_sources + [
    "torch/csrc/cuda/python_nccl.cpp",
    "torch/csrc/cuda/shared/cudnn.cpp",
    "torch/csrc/cuda/Tensor.cpp",
]

libtorch_python_core_sources = [
    "torch/csrc/DataLoader.cpp",
    "torch/csrc/Device.cpp",
    "torch/csrc/Dtype.cpp",
    "torch/csrc/DynamicTypes.cpp",
    "torch/csrc/Exceptions.cpp",
    "torch/csrc/Generator.cpp",
    "torch/csrc/Layout.cpp",
    "torch/csrc/MemoryFormat.cpp",
    "torch/csrc/QScheme.cpp",
    "torch/csrc/Module.cpp",
    "torch/csrc/python_dimname.cpp",
    "torch/csrc/Size.cpp",
    "torch/csrc/Storage.cpp",
    "torch/csrc/Stream.cpp",
    "torch/csrc/TypeInfo.cpp",
    "torch/csrc/api/src/python/init.cpp",
    "torch/csrc/autograd/functions/init.cpp",
    "torch/csrc/autograd/init.cpp",
    "torch/csrc/autograd/python_anomaly_mode.cpp",
    "torch/csrc/autograd/python_cpp_function.cpp",
    "torch/csrc/autograd/python_engine.cpp",
    "torch/csrc/autograd/python_function.cpp",
    "torch/csrc/autograd/python_hook.cpp",
    "torch/csrc/autograd/python_legacy_variable.cpp",
    "torch/csrc/autograd/python_variable.cpp",
    "torch/csrc/autograd/python_variable_indexing.cpp",
    "torch/csrc/jit/backends/backend_init.cpp",
    "torch/csrc/jit/python/init.cpp",
    "torch/csrc/jit/passes/onnx.cpp",
    "torch/csrc/jit/passes/onnx/cast_all_constant_to_floating.cpp",
    "torch/csrc/jit/passes/onnx/eval_peephole.cpp",
    "torch/csrc/jit/passes/onnx/constant_fold.cpp",
    "torch/csrc/jit/passes/onnx/constant_map.cpp",
    "torch/csrc/jit/passes/onnx/eliminate_unused_items.cpp",
    "torch/csrc/jit/passes/onnx/fixup_onnx_controlflow.cpp",
    "torch/csrc/jit/passes/onnx/list_model_parameters.cpp",
    "torch/csrc/jit/passes/onnx/function_substitution.cpp",
    "torch/csrc/jit/passes/onnx/fold_if_node.cpp",
    "torch/csrc/jit/passes/onnx/helper.cpp",
    "torch/csrc/jit/passes/onnx/peephole.cpp",
    "torch/csrc/jit/passes/onnx/preprocess_for_onnx.cpp",
    "torch/csrc/jit/passes/onnx/prepare_division_for_onnx.cpp",
    "torch/csrc/jit/passes/onnx/scalar_type_analysis.cpp",
    "torch/csrc/jit/passes/onnx/unpack_quantized_weights.cpp",
    "torch/csrc/jit/passes/onnx/remove_inplace_ops_for_onnx.cpp",
    "torch/csrc/jit/passes/onnx/shape_type_inference.cpp",
    "torch/csrc/jit/python/pybind_utils.cpp",
    "torch/csrc/jit/passes/onnx/pattern_conversion/common.cpp",
    "torch/csrc/jit/passes/onnx/pattern_conversion/pattern_encapsulation.cpp",
    "torch/csrc/jit/passes/onnx/pattern_conversion/pattern_conversion.cpp",
    "torch/csrc/jit/python/python_arg_flatten.cpp",
    "torch/csrc/jit/python/python_custom_class.cpp",
    "torch/csrc/jit/python/python_dict.cpp",
    "torch/csrc/jit/python/python_interpreter.cpp",
    "torch/csrc/jit/python/python_ir.cpp",
    "torch/csrc/jit/python/python_tracer.cpp",
    "torch/csrc/jit/python/script_init.cpp",
    "torch/csrc/jit/frontend/concrete_module_type.cpp",
    "torch/csrc/jit/frontend/tree_views.cpp",
    "torch/csrc/jit/python/python_sugared_value.cpp",
    "torch/csrc/jit/python/python_tree_views.cpp",
    "torch/csrc/jit/runtime/static/init.cpp",
    "torch/csrc/fx/fx_init.cpp",
    "torch/csrc/jit/tensorexpr/tensorexpr_init.cpp",
    "torch/csrc/multiprocessing/init.cpp",
    "torch/csrc/onnx/init.cpp",
    "torch/csrc/serialization.cpp",
    "torch/csrc/tensor/python_tensor.cpp",
    "torch/csrc/utils/init.cpp",
    "torch/csrc/utils/throughput_benchmark.cpp",
    "torch/csrc/utils.cpp",
    "torch/csrc/utils/cuda_lazy_init.cpp",
    "torch/csrc/utils/invalid_arguments.cpp",
    "torch/csrc/utils/object_ptr.cpp",
    "torch/csrc/utils/python_arg_parser.cpp",
    "torch/csrc/utils/python_dispatch.cpp",
    "torch/csrc/utils/structseq.cpp",
    "torch/csrc/utils/tensor_apply.cpp",
    "torch/csrc/utils/tensor_dtypes.cpp",
    "torch/csrc/utils/tensor_layouts.cpp",
    "torch/csrc/utils/tensor_memoryformats.cpp",
    "torch/csrc/utils/tensor_qschemes.cpp",
    "torch/csrc/utils/tensor_list.cpp",
    "torch/csrc/utils/tensor_new.cpp",
    "torch/csrc/utils/tensor_numpy.cpp",
    "torch/csrc/utils/tensor_types.cpp",
    "torch/csrc/utils/disable_torch_function.cpp",
]

libtorch_python_distributed_core_sources = [
    "torch/lib/c10d/comm.cpp",
    "torch/lib/c10d/default_comm_hooks.cpp",
    "torch/lib/c10d/reducer.cpp",
    "torch/lib/c10d/reducer_cuda.cpp",
    "torch/lib/c10d/logger.cpp",
    "torch/csrc/distributed/c10d/python_comm_hook.cpp",
    "torch/csrc/distributed/c10d/init.cpp",
]

libtorch_python_distributed_sources = libtorch_python_distributed_core_sources + [
    "torch/csrc/distributed/autograd/init.cpp",
    "torch/csrc/distributed/rpc/agent_utils.cpp",
    "torch/csrc/distributed/rpc/init.cpp",
    "torch/csrc/distributed/rpc/process_group_agent.cpp",
    "torch/csrc/distributed/rpc/py_rref.cpp",
    "torch/csrc/distributed/rpc/python_functions.cpp",
    "torch/csrc/distributed/rpc/python_rpc_handler.cpp",
    "torch/csrc/distributed/rpc/request_callback_impl.cpp",
    "torch/csrc/distributed/rpc/tensorpipe_agent.cpp",
    "torch/csrc/distributed/rpc/tensorpipe_utils.cpp",
    "torch/csrc/distributed/rpc/testing/faulty_process_group_agent.cpp",
    "torch/csrc/distributed/rpc/testing/init.cpp",
    "torch/csrc/distributed/rpc/unpickled_python_call.cpp",
    "torch/csrc/distributed/rpc/unpickled_python_remote_call.cpp",
    "torch/csrc/jit/runtime/register_distributed_ops.cpp",
]

def glob_libtorch_python_sources(gencode_pattern = ":generate-code[{}]"):
    _libtorch_python_sources = [gencode_pattern.format(name) for name in [
        "autograd/generated/python_functions.cpp",
        "autograd/generated/python_nn_functions.cpp",
        "autograd/generated/python_fft_functions.cpp",
        "autograd/generated/python_linalg_functions.cpp",
        "autograd/generated/python_special_functions.cpp",
        "autograd/generated/python_torch_functions.cpp",
        "autograd/generated/python_variable_methods.cpp",
    ]]

    _libtorch_python_sources.extend(libtorch_python_core_sources)
    _libtorch_python_sources.extend(libtorch_python_distributed_sources)

    return _libtorch_python_sources

aten_cpu_source_non_codegen_list = [
    "aten/src/ATen/AccumulateType.cpp",
    "aten/src/ATen/BatchedTensorImpl.cpp",
    "aten/src/ATen/CPUGeneratorImpl.cpp",
    "aten/src/ATen/Context.cpp",
    "aten/src/ATen/DLConvertor.cpp",
    "aten/src/ATen/ExpandUtils.cpp",
    "aten/src/ATen/MemoryOverlap.cpp",
    "aten/src/ATen/NamedTensorUtils.cpp",
    "aten/src/ATen/ParallelCommon.cpp",
    "aten/src/ATen/ParallelNative.cpp",
    "aten/src/ATen/ParallelNativeTBB.cpp",
    "aten/src/ATen/ParallelOpenMP.cpp",
    "aten/src/ATen/ParallelThreadPoolNative.cpp",
    "aten/src/ATen/ScalarOps.cpp",
    "aten/src/ATen/SequenceNumber.cpp",
    "aten/src/ATen/SparseTensorImpl.cpp",
    "aten/src/ATen/SparseCsrTensorImpl.cpp",
    "aten/src/ATen/SparseTensorUtils.cpp",
    "aten/src/ATen/TensorGeometry.cpp",
    "aten/src/ATen/TensorIndexing.cpp",
    "aten/src/ATen/TensorMeta.cpp",
    "aten/src/ATen/TensorNames.cpp",
    "aten/src/ATen/TensorUtils.cpp",
    "aten/src/ATen/ThreadLocalState.cpp",
    "aten/src/ATen/Utils.cpp",
    "aten/src/ATen/Version.cpp",
    "aten/src/ATen/VmapMode.cpp",
    "aten/src/ATen/VmapTransforms.cpp",
    "aten/src/ATen/core/BackendSelectFallbackKernel.cpp",
    "aten/src/ATen/core/DeprecatedTypeProperties.cpp",
    "aten/src/ATen/core/DeprecatedTypePropertiesRegistry.cpp",
    "aten/src/ATen/core/Dict.cpp",
    "aten/src/ATen/core/Dimname.cpp",
    "aten/src/ATen/core/Formatting.cpp",
    "aten/src/ATen/core/Generator.cpp",
    "aten/src/ATen/core/List.cpp",
    "aten/src/ATen/core/NamedTensor.cpp",
    "aten/src/ATen/core/Tensor.cpp",
    "aten/src/ATen/core/VariableFallbackKernel.cpp",
    "aten/src/ATen/core/VariableHooksInterface.cpp",
    "aten/src/ATen/core/Vitals.cpp",
    "aten/src/ATen/core/boxing/KernelFunction.cpp",
    "aten/src/ATen/core/custom_class.cpp",
    "aten/src/ATen/core/dispatch/DispatchKeyExtractor.cpp",
    "aten/src/ATen/core/dispatch/Dispatcher.cpp",
    "aten/src/ATen/core/dispatch/ObservedOperators.cpp",
    "aten/src/ATen/core/dispatch/OperatorEntry.cpp",
    "aten/src/ATen/core/interned_strings.cpp",
    "aten/src/ATen/core/ivalue.cpp",
    "aten/src/ATen/core/library.cpp",
    "aten/src/ATen/core/op_registration/infer_schema.cpp",
    "aten/src/ATen/core/op_registration/op_registration.cpp",
    "aten/src/ATen/core/operator_name.cpp",
    "aten/src/ATen/core/register_symbols.cpp",
    "aten/src/ATen/core/type.cpp",
    "aten/src/ATen/cpu/FlushDenormal.cpp",
    "aten/src/ATen/detail/CPUGuardImpl.cpp",
    "aten/src/ATen/detail/CUDAHooksInterface.cpp",
    "aten/src/ATen/detail/HIPHooksInterface.cpp",
    "aten/src/ATen/metal/Context.cpp",
    "aten/src/ATen/native/AutogradComposite.cpp",
    "aten/src/ATen/native/BatchLinearAlgebraKernel.cpp",
    "aten/src/ATen/native/DispatchStub.cpp",
    "aten/src/ATen/native/UpSample.cpp",
    "aten/src/ATen/native/mkl/LinearAlgebra.cpp",
    "aten/src/ATen/native/mkl/SparseCsrLinearAlgebra.cpp",
    "aten/src/ATen/native/mkl/SpectralOps.cpp",
    "aten/src/ATen/native/mkldnn/BinaryOps.cpp",
    "aten/src/ATen/native/mkldnn/Conv.cpp",
    "aten/src/ATen/native/mkldnn/Copy.cpp",
    "aten/src/ATen/native/mkldnn/IDeepRegistration.cpp",
    "aten/src/ATen/native/mkldnn/Linear.cpp",
    "aten/src/ATen/native/mkldnn/MKLDNNCommon.cpp",
    "aten/src/ATen/native/mkldnn/MKLDNNConversions.cpp",
    "aten/src/ATen/native/mkldnn/MkldnnTensorMath.cpp",
    "aten/src/ATen/native/mkldnn/Normalization.cpp",
    "aten/src/ATen/native/mkldnn/Pooling.cpp",
    "aten/src/ATen/native/mkldnn/Relu.cpp",
    "aten/src/ATen/native/mkldnn/SoftMax.cpp",
    "aten/src/ATen/native/mkldnn/TensorFactories.cpp",
    "aten/src/ATen/native/mkldnn/TensorShape.cpp",
    "aten/src/ATen/native/mkldnn/UnaryOps.cpp",
    "aten/src/ATen/native/mkldnn/Utils.cpp",
    "aten/src/ATen/native/quantized/cpu/init_qnnpack.cpp",
    "aten/src/ATen/record_function.cpp",
    "aten/src/ATen/vulkan/Context.cpp",
]

aten_cpu_source_codegen_list = [
    "aten/src/ATen/native/cpu/AdaptiveAvgPoolKernel.cpp",
]

# When building lite interpreter in OSS, "aten/src/ATen/native/cpu/AdaptiveAvgPoolKernel.cpp" will go through
# codegen process. The codegen version of this file, like Activation.cpp.DEFAULT.cpp, will be included
# in ${cpu_kernel_cpp} in aten/src/ATen/CMakeLists.txt. As a result, in aten/src/ATen/CMakeLists.txt,
# only aten_cpu_source_non_codegen_list need to be added to ${all_cpu_cpp}.
aten_cpu_source_list = sorted(aten_cpu_source_non_codegen_list + aten_cpu_source_codegen_list)

# Same as ${aten_cpu_source_codegen_list}, this list will go through aten codegen, and be included in
# ${cpu_kernel_cpp} in aten/src/ATen/CMakeLists.txt.
aten_native_source_codegen_list = [
    "aten/src/ATen/native/cpu/Activation.cpp",
    "aten/src/ATen/native/cpu/AvgPoolKernel.cpp",
    "aten/src/ATen/native/cpu/BinaryOpsKernel.cpp",
    "aten/src/ATen/native/cpu/BlasKernel.cpp",
    "aten/src/ATen/native/cpu/CatKernel.cpp",
    "aten/src/ATen/native/cpu/ComplexKernel.cpp",
    "aten/src/ATen/native/cpu/CopyKernel.cpp",
    "aten/src/ATen/native/cpu/CrossKernel.cpp",
    "aten/src/ATen/native/cpu/DepthwiseConvKernel.cpp",
    "aten/src/ATen/native/cpu/DistanceOpsKernel.cpp",
    "aten/src/ATen/native/cpu/FillKernel.cpp",
    "aten/src/ATen/native/cpu/FunctionOfAMatrixUtilsKernel.cpp",
    "aten/src/ATen/native/cpu/GridSamplerKernel.cpp",
    "aten/src/ATen/native/cpu/IndexKernel.cpp",
    "aten/src/ATen/native/cpu/LerpKernel.cpp",
    "aten/src/ATen/native/cpu/LinearAlgebraKernel.cpp",
    "aten/src/ATen/native/cpu/MaxPooling.cpp",
    "aten/src/ATen/native/cpu/MaxPoolKernel.cpp",
    "aten/src/ATen/native/cpu/MultinomialKernel.cpp",
    "aten/src/ATen/native/cpu/PointwiseOpsKernel.cpp",
    "aten/src/ATen/native/cpu/PowKernel.cpp",
    "aten/src/ATen/native/cpu/RangeFactoriesKernel.cpp",
    "aten/src/ATen/native/cpu/ReduceAllOpsKernel.cpp",
    "aten/src/ATen/native/cpu/ReduceOpsKernel.cpp",
    "aten/src/ATen/native/cpu/RenormKernel.cpp",
    "aten/src/ATen/native/cpu/ScatterGatherKernel.cpp",
    "aten/src/ATen/native/cpu/SoftMaxKernel.cpp",
    "aten/src/ATen/native/cpu/SortingKernel.cpp",
    "aten/src/ATen/native/cpu/StackKernel.cpp",
    "aten/src/ATen/native/cpu/SumKernel.cpp",
    "aten/src/ATen/native/cpu/TensorCompareKernel.cpp",
    "aten/src/ATen/native/cpu/UnaryOpsKernel.cpp",
    "aten/src/ATen/native/cpu/Unfold2d.cpp",
    "aten/src/ATen/native/cpu/UnfoldBackwardKernel.cpp",
    "aten/src/ATen/native/cpu/UpSampleKernel.cpp",
    "aten/src/ATen/native/cpu/UpSampleMoreKernel.cpp",
    "aten/src/ATen/native/cpu/batch_norm_kernel.cpp",
    "aten/src/ATen/native/cpu/group_norm_kernel.cpp",
    "aten/src/ATen/native/cpu/layer_norm_kernel.cpp",
    "aten/src/ATen/native/quantized/cpu/kernels/QuantizedOpKernels.cpp",
]

# This aten native source file list will not go through aten codegen process
aten_native_source_non_codegen_list = [
    "aten/src/ATen/native/ao_sparse/library.cpp",
    "aten/src/ATen/native/ao_sparse/quantized/cpu/fbgemm_utils.cpp",
    "aten/src/ATen/native/ao_sparse/quantized/cpu/qlinear.cpp",
    "aten/src/ATen/native/ao_sparse/quantized/cpu/qlinear_dynamic.cpp",
    "aten/src/ATen/native/ao_sparse/quantized/cpu/qlinear_prepack.cpp",
    "aten/src/ATen/native/ao_sparse/quantized/cpu/qlinear_unpack.cpp",
    "aten/src/ATen/native/quantized/cpu/fbgemm_utils.cpp",
    "aten/src/ATen/native/quantized/cpu/int_repr_quant.cpp",
    "aten/src/ATen/native/quantized/cpu/make_per_tensor_quantized_tensor.cpp",
    "aten/src/ATen/native/quantized/cpu/q_adaavgpool.cpp",
    "aten/src/ATen/native/quantized/cpu/q_avgpool.cpp",
    "aten/src/ATen/native/quantized/cpu/q_avgpool3d.cpp",
    "aten/src/ATen/native/quantized/cpu/qadd.cpp",
    "aten/src/ATen/native/quantized/cpu/qbatch_norm.cpp",
    "aten/src/ATen/native/quantized/cpu/qchannel_shuffle.cpp",
    "aten/src/ATen/native/quantized/cpu/qclamp.cpp",
    "aten/src/ATen/native/quantized/cpu/qconcat.cpp",
    "aten/src/ATen/native/quantized/cpu/qconv.cpp",
    "aten/src/ATen/native/quantized/cpu/qconv_prepack.cpp",
    "aten/src/ATen/native/quantized/cpu/qconv_unpack.cpp",
    "aten/src/ATen/native/quantized/cpu/qelu.cpp",
    "aten/src/ATen/native/quantized/cpu/qembeddingbag.cpp",
    "aten/src/ATen/native/quantized/cpu/qembeddingbag_prepack.cpp",
    "aten/src/ATen/native/quantized/cpu/qembeddingbag_unpack.cpp",
    "aten/src/ATen/native/quantized/cpu/qhardsigmoid.cpp",
    "aten/src/ATen/native/quantized/cpu/qhardswish.cpp",
    "aten/src/ATen/native/quantized/cpu/qlinear.cpp",
    "aten/src/ATen/native/quantized/cpu/qlinear_dynamic.cpp",
    "aten/src/ATen/native/quantized/cpu/qlinear_prepack.cpp",
    "aten/src/ATen/native/quantized/cpu/qlinear_unpack.cpp",
    "aten/src/ATen/native/quantized/cpu/qmul.cpp",
    "aten/src/ATen/native/quantized/cpu/qnormalization.cpp",
    "aten/src/ATen/native/quantized/cpu/qpool.cpp",
    "aten/src/ATen/native/quantized/cpu/qreduction.cpp",
    "aten/src/ATen/native/quantized/cpu/qrelu.cpp",
    "aten/src/ATen/native/quantized/cpu/qsigmoid.cpp",
    "aten/src/ATen/native/quantized/cpu/qsort.cpp",
    "aten/src/ATen/native/quantized/cpu/qtanh.cpp",
    "aten/src/ATen/native/quantized/cpu/qthreshold.cpp",
    "aten/src/ATen/native/quantized/cpu/qupsample_bilinear2d.cpp",
    "aten/src/ATen/native/quantized/cpu/qupsample_nearest2d.cpp",
    "aten/src/ATen/native/quantized/cpu/qupsample_nearest3d.cpp",
    "aten/src/ATen/native/quantized/cpu/tensor_operators.cpp",
    "aten/src/ATen/native/quantized/Copy.cpp",
    "aten/src/ATen/native/quantized/QTensor.cpp",
    "aten/src/ATen/native/quantized/TensorCompare.cpp",
    "aten/src/ATen/native/quantized/TensorFactories.cpp",
    "aten/src/ATen/native/quantized/affine_quantizer.cpp",
    "aten/src/ATen/native/quantized/affine_quantizer_base.cpp",
    "aten/src/ATen/native/quantized/fake_quant_per_channel_affine.cpp",
    "aten/src/ATen/native/quantized/fake_quant_per_tensor_affine.cpp",
    "aten/src/ATen/native/quantized/library.cpp",
    "aten/src/ATen/quantized/QTensorImpl.cpp",
    "aten/src/ATen/quantized/Quantizer.cpp",
    "aten/src/ATen/native/Activation.cpp",
    "aten/src/ATen/native/AdaptiveAveragePooling.cpp",
    "aten/src/ATen/native/AdaptiveAveragePooling3d.cpp",
    "aten/src/ATen/native/AdaptiveMaxPooling2d.cpp",
    "aten/src/ATen/native/AdaptiveMaxPooling3d.cpp",
    "aten/src/ATen/native/AffineGridGenerator.cpp",
    "aten/src/ATen/native/AveragePool2d.cpp",
    "aten/src/ATen/native/AveragePool3d.cpp",
    "aten/src/ATen/native/BatchLinearAlgebra.cpp",
    "aten/src/ATen/native/Batching.cpp",
    "aten/src/ATen/native/BinaryOps.cpp",
    "aten/src/ATen/native/Blas.cpp",
    "aten/src/ATen/native/BlasKernel.cpp",
    "aten/src/ATen/native/Bucketization.cpp",
    "aten/src/ATen/native/CPUBlas.cpp",
    "aten/src/ATen/native/ChanelShuffle.cpp",
    "aten/src/ATen/native/Col2Im.cpp",
    "aten/src/ATen/native/ConstantPadNd.cpp",
    "aten/src/ATen/native/Convolution.cpp",
    "aten/src/ATen/native/ConvolutionMM2d.cpp",
    "aten/src/ATen/native/ConvolutionMM3d.cpp",
    "aten/src/ATen/native/ConvolutionTBC.cpp",
    "aten/src/ATen/native/Copy.cpp",
    "aten/src/ATen/native/Correlation.cpp",
    "aten/src/ATen/native/Cross.cpp",
    "aten/src/ATen/native/DilatedMaxPool2d.cpp",
    "aten/src/ATen/native/DilatedMaxPool3d.cpp",
    # Referenced by both native and ATen/Version.cpp. Does not reference to other native symbols
    # "aten/src/ATen/native/DispatchStub.cpp",
    # "aten/src/ATen/native/quantized/cpu/init_qnnpack.cpp",
    "aten/src/ATen/native/Distance.cpp",
    "aten/src/ATen/native/Distributions.cpp",
    "aten/src/ATen/native/Dropout.cpp",
    "aten/src/ATen/native/Embedding.cpp",
    "aten/src/ATen/native/EmbeddingBag.cpp",
    "aten/src/ATen/native/Fill.cpp",
    "aten/src/ATen/native/ForeachOpsKernels.cpp",
    "aten/src/ATen/native/FractionalMaxPool2d.cpp",
    "aten/src/ATen/native/FractionalMaxPool3d.cpp",
    "aten/src/ATen/native/FunctionOfAMatrixUtils.cpp",
    "aten/src/ATen/native/GatedLinearUnit.cpp",
    "aten/src/ATen/native/GridSampler.cpp",
    "aten/src/ATen/native/Im2Col.cpp",
    "aten/src/ATen/native/IndexingUtils.cpp",
    "aten/src/ATen/native/Integration.cpp",
    "aten/src/ATen/native/Itertools.cpp",
    "aten/src/ATen/native/LegacyBridge.cpp",
    "aten/src/ATen/native/LegacyNNDefinitions.cpp",
    "aten/src/ATen/native/Lerp.cpp",
    "aten/src/ATen/native/Linear.cpp",
    "aten/src/ATen/native/LinearAlgebra.cpp",
    "aten/src/ATen/native/Loss.cpp",
    "aten/src/ATen/native/LossCTC.cpp",
    "aten/src/ATen/native/LossMultiLabelMargin.cpp",
    "aten/src/ATen/native/LossMultiMargin.cpp",
    "aten/src/ATen/native/LossNLL.cpp",
    "aten/src/ATen/native/LossNLL2d.cpp",
    "aten/src/ATen/native/MaxPooling.cpp",
    "aten/src/ATen/native/MaxUnpooling.cpp",
    "aten/src/ATen/native/Memory.cpp",
    "aten/src/ATen/native/MetaTensor.cpp",
    "aten/src/ATen/native/NNPACK.cpp",
    "aten/src/ATen/native/NaiveConvolutionTranspose2d.cpp",
    "aten/src/ATen/native/NaiveConvolutionTranspose3d.cpp",
    "aten/src/ATen/native/NaiveDilatedConvolution.cpp",
    "aten/src/ATen/native/NamedTensor.cpp",
    "aten/src/ATen/native/Normalization.cpp",
    "aten/src/ATen/native/Onehot.cpp",
    "aten/src/ATen/native/PackedSequence.cpp",
    "aten/src/ATen/native/PixelShuffle.cpp",
    "aten/src/ATen/native/PointwiseOps.cpp",
    "aten/src/ATen/native/Pooling.cpp",
    "aten/src/ATen/native/Pow.cpp",
    "aten/src/ATen/native/QuantizedLinear.cpp",
    "aten/src/ATen/native/RNN.cpp",
    "aten/src/ATen/native/RangeFactories.cpp",
    "aten/src/ATen/native/ReduceAllOps.cpp",
    "aten/src/ATen/native/ReduceOps.cpp",
    "aten/src/ATen/native/ReflectionPad.cpp",
    "aten/src/ATen/native/Repeat.cpp",
    "aten/src/ATen/native/ReplicationPadding.cpp",
    "aten/src/ATen/native/Resize.cpp",
    "aten/src/ATen/native/RowwisePrune.cpp",
    "aten/src/ATen/native/SegmentReduce.cpp",
    "aten/src/ATen/native/Scalar.cpp",
    "aten/src/ATen/native/SobolEngineOps.cpp",
    "aten/src/ATen/native/SobolEngineOpsUtils.cpp",
    "aten/src/ATen/native/SoftMax.cpp",
    "aten/src/ATen/native/Sorting.cpp",
    "aten/src/ATen/native/SpectralOps.cpp",
    "aten/src/ATen/native/SummaryOps.cpp",
    "aten/src/ATen/native/TensorAdvancedIndexing.cpp",
    "aten/src/ATen/native/TensorCompare.cpp",
    "aten/src/ATen/native/TensorConversions.cpp",
    "aten/src/ATen/native/TensorFactories.cpp",
    "aten/src/ATen/native/TensorIteratorReduce.cpp",
    "aten/src/ATen/native/TensorProperties.cpp",
    "aten/src/ATen/native/TensorShape.cpp",
    "aten/src/ATen/native/TensorTransformations.cpp",
    "aten/src/ATen/native/TestOps.cpp",
    "aten/src/ATen/native/TriangularOps.cpp",
    "aten/src/ATen/native/TypeProperties.cpp",
    "aten/src/ATen/native/UnaryOps.cpp",
    "aten/src/ATen/native/Unfold2d.cpp",
    "aten/src/ATen/native/Unfold3d.cpp",
    "aten/src/ATen/native/UnfoldBackward.cpp",
    "aten/src/ATen/native/Unique.cpp",
    # Low-level functions that can be directly referenced
    # "aten/src/ATen/native/UpSample.cpp",
    "aten/src/ATen/native/UpSampleBicubic2d.cpp",
    "aten/src/ATen/native/UpSampleBilinear2d.cpp",
    "aten/src/ATen/native/UpSampleLinear1d.cpp",
    "aten/src/ATen/native/UpSampleNearest1d.cpp",
    "aten/src/ATen/native/UpSampleNearest2d.cpp",
    "aten/src/ATen/native/UpSampleNearest3d.cpp",
    "aten/src/ATen/native/UpSampleTrilinear3d.cpp",
    "aten/src/ATen/native/VariableMethodStubs.cpp",
    "aten/src/ATen/native/WeightNorm.cpp",
    "aten/src/ATen/native/group_norm.cpp",
    "aten/src/ATen/native/layer_norm.cpp",
    "aten/src/ATen/native/sparse/ParamUtils.cpp",
    "aten/src/ATen/native/sparse/SoftMax.cpp",
    "aten/src/ATen/native/sparse/SparseMatMul.cpp",
    "aten/src/ATen/native/sparse/SparseTensor.cpp",
    "aten/src/ATen/native/sparse/SparseCsrTensor.cpp",
    "aten/src/ATen/native/sparse/SparseTensorMath.cpp",
    "aten/src/ATen/native/sparse/SparseCsrTensorMath.cpp",
    "aten/src/TH/THAllocator.cpp",
    "aten/src/TH/THBlas.cpp",
    "aten/src/TH/THGeneral.cpp",
    "aten/src/TH/THStorageFunctions.cpp",
    "aten/src/TH/THTensor.cpp",
    "aten/src/TH/THTensorMoreMath.cpp",
    "aten/src/ATen/native/utils/Factory.cpp",
    "aten/src/ATen/native/xnnpack/Activation.cpp",
    "aten/src/ATen/native/xnnpack/ChannelShuffle.cpp",
    "aten/src/ATen/native/xnnpack/Convolution.cpp",
    "aten/src/ATen/native/xnnpack/AveragePooling.cpp",
    "aten/src/ATen/native/xnnpack/Init.cpp",
    "aten/src/ATen/native/xnnpack/Linear.cpp",
    "aten/src/ATen/native/xnnpack/MaxPooling.cpp",
    "aten/src/ATen/native/xnnpack/OpContext.cpp",
    "aten/src/ATen/native/xnnpack/RegisterOpContextClass.cpp",
    "aten/src/ATen/native/xnnpack/Shim.cpp",
    # Files not in native, but depends on native symbols
    # "aten/src/ATen/TensorIndexing.cpp",
    "aten/src/ATen/TensorIterator.cpp",
    "aten/src/ATen/LegacyTHFunctionsCPU.cpp",
    "aten/src/ATen/nnapi/nnapi_bind.cpp",
    "aten/src/ATen/nnapi/nnapi_wrapper.cpp",
    "aten/src/ATen/nnapi/nnapi_model_loader.cpp",
]

# 1. Files in ATen/native with a few exceptions
# TODO: move the exceptions to proper locations
# 2. The whole aten native source list includes the list with and without aten codegen process.
aten_native_source_list = sorted(aten_native_source_non_codegen_list + aten_native_source_codegen_list)
