|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import unittest |
| 10 | +from contextlib import contextmanager |
| 11 | + |
| 12 | +import torch |
| 13 | +from executorch.backends.cpu.partitioner import CPUPartitioner |
| 14 | +from executorch.exir import to_edge_transform_and_lower |
| 15 | +from executorch.extension.pybindings.portable_lib import ( |
| 16 | + _load_for_executorch_from_buffer, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class CPURuntimeTest(unittest.TestCase): |
| 21 | + @contextmanager |
| 22 | + def _lower(self, model, inputs): |
| 23 | + edge = to_edge_transform_and_lower( |
| 24 | + torch.export.export(model, inputs), partitioner=[CPUPartitioner()] |
| 25 | + ) |
| 26 | + program = edge.to_executorch() |
| 27 | + plan = program.executorch_program.execution_plan[0] |
| 28 | + self.assertEqual([delegate.id for delegate in plan.delegates], ["CpuBackend"]) |
| 29 | + self.assertEqual(plan.operators, []) |
| 30 | + # The runtime borrows the serialized program buffer. |
| 31 | + yield _load_for_executorch_from_buffer(program.buffer) |
| 32 | + |
| 33 | + def test_integer_inputs_and_outputs_preserve_precision(self): |
| 34 | + class Add(torch.nn.Module): |
| 35 | + def forward(self, x, y): |
| 36 | + return x + y |
| 37 | + |
| 38 | + for dtype, values in ( |
| 39 | + (torch.uint8, [0, 128, 250]), |
| 40 | + (torch.int8, [-100, 0, 100]), |
| 41 | + (torch.int16, [-30000, 0, 30000]), |
| 42 | + (torch.int32, [-(2**24 + 1), 0, 2**24 + 1]), |
| 43 | + (torch.int64, [-(2**40 + 3), 0, 2**40 + 3]), |
| 44 | + ): |
| 45 | + with self.subTest(dtype=dtype): |
| 46 | + model = Add() |
| 47 | + x = torch.tensor(values, dtype=dtype) |
| 48 | + y = torch.ones_like(x) |
| 49 | + with self._lower(model, (x, y)) as runtime: |
| 50 | + for inputs in ((x, y), (y, x)): |
| 51 | + actual = runtime.forward(inputs)[0] |
| 52 | + torch.testing.assert_close( |
| 53 | + actual, model(*inputs), rtol=0, atol=0 |
| 54 | + ) |
| 55 | + |
| 56 | + def test_integer_indices_and_mixed_outputs(self): |
| 57 | + class Select(torch.nn.Module): |
| 58 | + def forward(self, x, indices): |
| 59 | + selected = torch.index_select(x, 1, indices) |
| 60 | + positive = selected > 0 |
| 61 | + return ( |
| 62 | + torch.where(positive, selected, -selected), |
| 63 | + selected.argmax(dim=1), |
| 64 | + positive, |
| 65 | + ) |
| 66 | + |
| 67 | + model = Select() |
| 68 | + x = torch.tensor([[-3.0, 5.0, 2.0], [7.0, -2.0, -4.0]]) |
| 69 | + for dtype in (torch.int32, torch.int64): |
| 70 | + with self.subTest(dtype=dtype): |
| 71 | + indices = torch.tensor([2, 0], dtype=dtype) |
| 72 | + with self._lower(model, (x, indices)) as runtime: |
| 73 | + for current in (indices, indices.flip(0)): |
| 74 | + actual = runtime.forward((x, current)) |
| 75 | + for output, expected in zip( |
| 76 | + actual, model(x, current), strict=True |
| 77 | + ): |
| 78 | + torch.testing.assert_close(output, expected, rtol=0, atol=0) |
| 79 | + |
| 80 | + def test_boolean_inputs_and_integer_constants(self): |
| 81 | + class MaskedAdd(torch.nn.Module): |
| 82 | + def __init__(self): |
| 83 | + super().__init__() |
| 84 | + self.register_buffer("offset", torch.tensor([2**40 + 3, -7])) |
| 85 | + |
| 86 | + def forward(self, x, mask): |
| 87 | + return torch.where(mask, x + self.offset, x), torch.logical_not(mask) |
| 88 | + |
| 89 | + model = MaskedAdd() |
| 90 | + x = torch.tensor([2**40 + 1, -(2**40 + 5)]) |
| 91 | + mask = torch.tensor([True, False]) |
| 92 | + with self._lower(model, (x, mask)) as runtime: |
| 93 | + for current in (mask, ~mask): |
| 94 | + actual = runtime.forward((x, current)) |
| 95 | + for output, expected in zip(actual, model(x, current), strict=True): |
| 96 | + torch.testing.assert_close(output, expected, rtol=0, atol=0) |
| 97 | + |
| 98 | + def test_optional_integer_index_lists(self): |
| 99 | + class Index(torch.nn.Module): |
| 100 | + def __init__(self, accumulate): |
| 101 | + super().__init__() |
| 102 | + self.accumulate = accumulate |
| 103 | + |
| 104 | + def forward(self, x, indices, values): |
| 105 | + updated = torch.index_put(x, (indices,), values, self.accumulate) |
| 106 | + return updated, updated[indices], updated[:, indices] |
| 107 | + |
| 108 | + x = torch.arange(9, dtype=torch.float32).reshape(3, 3) |
| 109 | + indices = torch.tensor([0, 2]) |
| 110 | + values = torch.tensor([[10.0, 20.0, 30.0], [-1.0, -2.0, -3.0]]) |
| 111 | + inputs = (x, indices, values) |
| 112 | + for accumulate in (False, True): |
| 113 | + with self.subTest(accumulate=accumulate): |
| 114 | + model = Index(accumulate) |
| 115 | + with self._lower(model, inputs) as runtime: |
| 116 | + actual = runtime.forward(inputs) |
| 117 | + for output, expected in zip(actual, model(*inputs), strict=True): |
| 118 | + torch.testing.assert_close(output, expected, rtol=0, atol=0) |
0 commit comments