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/portable/cpu/util/functional_util.h>
10 #include <executorch/runtime/kernel/kernel_includes.h>
11 #include <executorch/runtime/platform/assert.h>
12
13 namespace torch {
14 namespace executor {
15 namespace native {
16
17 using exec_aten::Tensor;
18
neg_out(KernelRuntimeContext & ctx,const Tensor & in,Tensor & out)19 Tensor& neg_out(KernelRuntimeContext& ctx, const Tensor& in, Tensor& out) {
20 (void)ctx;
21
22 // Resize for dynamic shape
23 ET_KERNEL_CHECK_MSG(
24 ctx,
25 resize_tensor(out, in.sizes()) == Error::Ok,
26 InvalidArgument,
27 out,
28 "Failed to resize output tensor.");
29
30 ET_KERNEL_CHECK(
31 ctx, tensors_have_same_shape_and_dtype(in, out), InvalidArgument, out);
32
33 ET_KERNEL_CHECK(
34 ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);
35
36 ET_SWITCH_REAL_TYPES(in.scalar_type(), ctx, "neg.out", CTYPE, [&] {
37 apply_unary_map_fn(
38 [](const CTYPE val_in) { return static_cast<CTYPE>(-val_in); },
39 in.const_data_ptr<CTYPE>(),
40 out.mutable_data_ptr<CTYPE>(),
41 in.numel());
42 });
43
44 return out;
45 }
46
47 } // namespace native
48 } // namespace executor
49 } // namespace torch
50