#include #include #include #include #include #include namespace at { int _crash_if_asan(int arg) { // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays) volatile char x[3]; x[arg] = 0; return x[0]; } namespace detail { template Tensor tensor_cpu(ArrayRef values, const TensorOptions& options) { auto result = at::empty(values.size(), options); AT_ASSERT(result.is_contiguous()); AT_DISPATCH_ALL_TYPES_AND_COMPLEX(result.scalar_type(), "tensor_cpu", [&] { std::copy( values.begin(), values.end(), result.template data_ptr()); }); return result; } template Tensor tensor_backend(ArrayRef values, const TensorOptions& options) { auto cpu_tensor = tensor_cpu(values, options.device(DeviceType::CPU)); return cpu_tensor.to(options.device()); } template Tensor tensor_complex_cpu(ArrayRef values, const TensorOptions& options) { auto result = at::empty(values.size(), options); AT_ASSERT(result.is_contiguous()); AT_DISPATCH_COMPLEX_TYPES(result.scalar_type(), "tensor_cpu", [&] { std::copy( values.begin(), values.end(), result.template data_ptr()); }); return result; } template Tensor tensor_complex_backend( ArrayRef values, const TensorOptions& options) { auto cpu_tensor = tensor_complex_cpu(values, options.device(DeviceType::CPU)); return cpu_tensor.to(options.device()); } } // namespace detail #define TENSOR(T, _1) \ Tensor tensor(ArrayRef values, const TensorOptions& options) { \ if (options.device().type() != c10::DeviceType::CPU) { \ return at::detail::tensor_backend(values, options); \ } else { \ return at::detail::tensor_cpu(values, options); \ } \ } AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, TENSOR) #undef TENSOR #define TENSOR(T, _1) \ Tensor tensor(ArrayRef values, const TensorOptions& options) { \ if (options.device().type() != c10::DeviceType::CPU) { \ return at::detail::tensor_complex_backend(values, options); \ } else { \ return at::detail::tensor_complex_cpu(values, options); \ } \ } AT_FORALL_COMPLEX_TYPES(TENSOR) #undef TENSOR } // namespace at