|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/kernels/optimized/cpu/fft_utils.h> |
| 10 | +#include <executorch/runtime/core/span.h> |
| 11 | + |
| 12 | +namespace torch::executor::native { |
| 13 | +Tensor& opt_fft_c2r_out( |
| 14 | + KernelRuntimeContext& ctx, |
| 15 | + const Tensor& in, |
| 16 | + IntArrayRef dim, |
| 17 | + int64_t normalization, |
| 18 | + int64_t last_dim_size, |
| 19 | + Tensor& out) { |
| 20 | + auto in_sizes = in.sizes(); |
| 21 | + ET_KERNEL_CHECK(ctx, in.dim() <= kTensorDimensionLimit, InvalidArgument, out); |
| 22 | + |
| 23 | + ET_KERNEL_CHECK(ctx, !dim.empty(), InvalidArgument, out); |
| 24 | + ET_KERNEL_CHECK(ctx, last_dim_size >= 1, InvalidArgument, out); |
| 25 | + |
| 26 | + // Determine the output size |
| 27 | + std::array<Tensor::SizesType, kTensorDimensionLimit> out_sizes_storage{}; |
| 28 | + executorch::runtime::Span<Tensor::SizesType> out_sizes( |
| 29 | + out_sizes_storage.data(), in_sizes.size()); |
| 30 | + std::copy(in_sizes.begin(), in_sizes.end(), out_sizes.begin()); |
| 31 | + out_sizes[dim.back()] = last_dim_size; |
| 32 | + |
| 33 | + ET_KERNEL_CHECK( |
| 34 | + ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out); |
| 35 | + |
| 36 | + ET_KERNEL_CHECK_MSG( |
| 37 | + ctx, |
| 38 | + in.scalar_type() == executorch::runtime::toComplexType(out.scalar_type()), |
| 39 | + InvalidArgument, |
| 40 | + out, |
| 41 | + "the input type for _fft_c2r must be the Complex type corresponding to the output type"); |
| 42 | + |
| 43 | + for (auto d : dim) { |
| 44 | + ET_KERNEL_CHECK_MSG( |
| 45 | + ctx, |
| 46 | + d >= 0 && d < in.dim(), |
| 47 | + InvalidArgument, |
| 48 | + out, |
| 49 | + "dims must be in bounds (got %" PRId64 ")", |
| 50 | + d); |
| 51 | + } |
| 52 | + |
| 53 | + ET_KERNEL_CHECK_MSG( |
| 54 | + ctx, |
| 55 | + resize_tensor( |
| 56 | + out, |
| 57 | + executorch::runtime::ArrayRef<Tensor::SizesType>( |
| 58 | + out_sizes.data(), out_sizes.size())) == Error::Ok, |
| 59 | + InvalidArgument, |
| 60 | + out, |
| 61 | + "Failed to resize output tensor (last dim %d).", |
| 62 | + out_sizes[dim.back()]); |
| 63 | + |
| 64 | + pocketfft::shape_t axes(dim.begin(), dim.end()); |
| 65 | + auto out_shape = shape_from_tensor(out); |
| 66 | + // TODO: if arbitrary strides are a possibility, we need to validate |
| 67 | + // these, because pocketfft README says "Strides that lead to |
| 68 | + // multiple accesses of the same memory address are not allowed." |
| 69 | + auto in_stride = stride_from_tensor(in); |
| 70 | + auto out_stride = stride_from_tensor(out); |
| 71 | + // NOTE: as of this writing, upstream PyTorch only supports |
| 72 | + // float/double, so we follow suit. |
| 73 | + ET_SWITCH_FLOAT_TYPES(out.scalar_type(), ctx, "_fft_c2r.out", CTYPE_OUT, [&] { |
| 74 | + auto fct = compute_fct<CTYPE_OUT>(ctx, out, dim, normalization); |
| 75 | + if (!fct) { |
| 76 | + // Check failed, just bail out of the lambda. |
| 77 | + return; |
| 78 | + } |
| 79 | + pocketfft::c2r<CTYPE_OUT>( |
| 80 | + out_shape, |
| 81 | + in_stride, |
| 82 | + out_stride, |
| 83 | + axes, |
| 84 | + false /* forward */, |
| 85 | + tensor_cdata<CTYPE_OUT>(in), |
| 86 | + out.mutable_data_ptr<CTYPE_OUT>(), |
| 87 | + *fct); |
| 88 | + }); |
| 89 | + return out; |
| 90 | +} |
| 91 | +} // namespace torch::executor::native |
0 commit comments