1 #define TORCH_ASSERT_NO_OPERATORS
2 #include <ATen/Dispatch_v2.h>
3 #include <ATen/Parallel.h>
4 #include <ATen/cpu/vec/vec.h>
5 #include <ATen/cpu/vec/functional.h>
6 #include <ATen/native/TensorIterator.h>
7 #include <ATen/native/cpu/Loops.h>
8
9 #include <ATen/native/Fill.h>
10 #include <c10/core/Scalar.h>
11
12 namespace at::native {
13 namespace {
14
15
16 template <typename scalar_t>
fill_non_native_type(TensorIterator & iter,const Scalar & value_scalar)17 void fill_non_native_type(TensorIterator& iter, const Scalar& value_scalar) {
18 auto value = value_scalar.to<scalar_t>().x;
19 using H = typename std::make_signed<decltype(value)>::type; // Signed type has more acceleration
20 // Reserve the representation of value. static_cast<H>(value) is implementation defined.
21 H val = *reinterpret_cast<H*>(std::addressof(value));
22 cpu_kernel_vec</*check_dynamic_cast=*/false>(
23 iter,
24 [val]() -> H { return val; },
25 [val]() { return Vectorized<H>(val); });
26 }
27
28 template <>
fill_non_native_type(TensorIterator & iter,const Scalar & value_scalar)29 void fill_non_native_type<c10::complex<at::Half>>(TensorIterator& iter, const Scalar& value_scalar) {
30 static_assert(sizeof(c10::complex<at::Half>) == sizeof(int32_t), "Size of ComplexHalf should be 32-bits");
31 auto value = c10::complex<at::Half>(value_scalar.to<c10::complex<float>>());
32 auto val = *reinterpret_cast<int32_t*>(std::addressof(value));
33 cpu_kernel_vec</*check_dynamic_cast=*/false>(
34 iter,
35 [val]() -> int32_t { return val; },
36 [val]() { return Vectorized<int32_t>(val); });
37 }
38
fill_kernel(TensorIterator & iter,const Scalar & value_scalar)39 void fill_kernel(TensorIterator& iter, const Scalar& value_scalar) {
40 if (iter.dtype() == ScalarType::Half) {
41 fill_non_native_type<at::Half>(iter, value_scalar);
42 } else if (iter.dtype() == ScalarType::BFloat16) {
43 fill_non_native_type<at::BFloat16>(iter, value_scalar);
44 } else if (iter.dtype() == ScalarType::ComplexHalf) {
45 fill_non_native_type<c10::complex<at::Half>>(iter, value_scalar);
46 } else if (iter.dtype() == ScalarType::Float8_e4m3fn) {
47 fill_non_native_type<at::Float8_e4m3fn>(iter, value_scalar);
48 } else if (iter.dtype() == ScalarType::Float8_e5m2) {
49 fill_non_native_type<at::Float8_e5m2>(iter, value_scalar);
50 } else if (iter.dtype() == ScalarType::Float8_e4m3fnuz) {
51 fill_non_native_type<at::Float8_e4m3fnuz>(iter, value_scalar);
52 } else if (iter.dtype() == ScalarType::Float8_e5m2fnuz) {
53 fill_non_native_type<at::Float8_e5m2fnuz>(iter, value_scalar);
54 } else {
55 AT_DISPATCH_V2(
56 iter.dtype(), "fill_cpu", AT_WRAP([&]() {
57 scalar_t value = value_scalar.to<scalar_t>();
58 cpu_kernel_vec(
59 iter,
60 [=]() -> scalar_t { return value; },
61 [=]() { return Vectorized<scalar_t>(value); });
62 }),
63 AT_EXPAND(AT_ALL_TYPES_AND_COMPLEX), kBool, AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES)
64 );
65 }
66 }
67
68 } // namespace
69
70 REGISTER_DISPATCH(fill_stub, &fill_kernel);
71
72 } // namespace at::native
73