-
Notifications
You must be signed in to change notification settings - Fork 881
Expand file tree
/
Copy pathop_full_like.cpp
More file actions
74 lines (60 loc) · 1.97 KB
/
op_full_like.cpp
File metadata and controls
74 lines (60 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/kernels/portable/cpu/scalar_utils.h>
#include <executorch/runtime/kernel/kernel_includes.h>
namespace torch {
namespace executor {
namespace native {
using Tensor = executorch::aten::Tensor;
using ScalarType = executorch::aten::ScalarType;
Tensor& full_like_out(
KernelRuntimeContext& ctx,
const Tensor& in,
const Scalar& fill_value,
optional<MemoryFormat> memory_format,
Tensor& out) {
(void)ctx;
if (memory_format.has_value()) {
ET_KERNEL_CHECK_MSG(
ctx,
memory_format.value() == MemoryFormat::Contiguous ||
memory_format.value() == MemoryFormat::Preserve,
InvalidArgument,
out,
"memory_format must be contiguous");
}
ET_KERNEL_CHECK(
ctx, tensors_have_same_dim_order(in, out), InvalidArgument, out);
ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out);
// Resize for dynamic shape
ET_KERNEL_CHECK_MSG(
ctx,
resize_tensor(out, in.sizes()) == Error::Ok,
InvalidArgument,
out,
"Failed to resize output tensor.");
ScalarType val_type = utils::get_scalar_dtype(fill_value);
ScalarType out_type = out.scalar_type();
constexpr auto name = "scalar_tensor.out";
ET_SWITCH_REALB_TYPES(val_type, ctx, name, CTYPE_VAL, [&] {
CTYPE_VAL val;
ET_KERNEL_CHECK(
ctx, utils::extract_scalar(fill_value, &val), InvalidArgument, );
ET_SWITCH_REALHBBF16_TYPES(out_type, ctx, name, CTYPE_OUT, [&] {
CTYPE_OUT val_casted = static_cast<CTYPE_OUT>(val);
auto data_out = out.mutable_data_ptr<CTYPE_OUT>();
for (size_t i = 0; i < out.numel(); ++i) {
data_out[i] = val_casted;
}
});
});
return out;
}
} // namespace native
} // namespace executor
} // namespace torch