Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions exir/passes/executorch_prim_ops_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

import math
import operator
from typing import Dict, Set, Union

Expand All @@ -14,6 +15,8 @@
from torch._ops import OpOverload
from torch.library import Library

# pyre-unsafe


executorch_prims_lib = Library("executorch_prim", "DEF")

Expand Down Expand Up @@ -91,7 +94,13 @@ def neg(a: _SymScalar) -> _SymScalar:
return -a # pyre-ignore


@bind_pattern_to_op(executorch_prims_lib, "trunc.Scalar(Scalar a) -> Scalar")
def trunc(a: _SymScalar) -> _SymScalar:
return math.trunc(a) # pyre-ignore


_PYTHON_SYM_OPS_TO_EXECUTORCH_SYM_OPS: Dict[OpOverload, OpOverload] = {
math.trunc: ops.backend.executorch_prim.trunc.Scalar,
operator.sub: ops.backend.executorch_prim.sub.Scalar,
operator.mul: ops.backend.executorch_prim.mul.Scalar,
operator.add: ops.backend.executorch_prim.add.Scalar,
Expand Down
16 changes: 16 additions & 0 deletions kernels/prim_ops/register_prim_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <executorch/runtime/kernel/kernel_includes.h>
#include <executorch/runtime/kernel/operator_registry.h>

#include <cmath>

using torch::executor::function::et_copy_index;

namespace torch {
Expand Down Expand Up @@ -301,6 +303,20 @@ static Kernel prim_ops[] = {
}
}),

// trunc.Scalar(Scalar a) -> Scalar
Kernel(
"executorch_prim::trunc.Scalar",
[](KernelRuntimeContext& context, EValue** stack) {
(void)context;
EValue& a = *stack[0];
EValue& out = *stack[1];
if (a.isDouble()) {
out = EValue(static_cast<int64_t>(trunc(a.toDouble())));
} else {
ET_CHECK_MSG(false, "%zu", (size_t)a.tag);
}
}),

// executorch_prim::et_copy_index.tensor(tensor, tensor) -> tensor
Kernel("executorch_prim::et_copy_index.tensor", &et_copy_index),
// executorch_prim::et_view.default(Tensor, int[]) -> Tensor
Expand Down
20 changes: 20 additions & 0 deletions kernels/prim_ops/test/prim_ops_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,5 +503,25 @@ TEST_F(RegisterPrimOpsTest, TestETViewEmpty) {
getOpsFn("executorch_prim::et_view.default")(context, bad_stack), "");
}

TEST_F(RegisterPrimOpsTest, TestTrunc) {
std::array<double, 10> inputs = {
0.0, 0.25, 0.5, 0.75, 1.0, 1.75, -0.5, -1.0, -1.5, 9.999999};
std::array<int64_t, 10> expected = {0, 0, 0, 0, 1, 1, 0, -1, -1, 9};

for (auto i = 0; i < inputs.size(); i++) {
EValue values[2];
values[0] = EValue(inputs[i]);
values[1] = EValue(0.0);

EValue* stack[2];
for (size_t j = 0; j < 2; j++) {
stack[j] = &values[j];
}

getOpsFn("executorch_prim::trunc.Scalar")(context, stack);
EXPECT_EQ(stack[1]->toInt(), expected[i]);
}
}

} // namespace executor
} // namespace torch
Loading