|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +"""Ethos(TM)-N partition parameter tests""" |
| 18 | + |
| 19 | +import pytest |
| 20 | +import tvm |
| 21 | +from tvm import relay |
| 22 | +import numpy as np |
| 23 | + |
| 24 | +from tvm.relay.op.contrib.ethosn import partition_for_ethosn77 |
| 25 | +from tvm.relay.op.contrib.ethosn import partition_for_ethosn78 |
| 26 | +from tvm.testing import requires_ethosn |
| 27 | + |
| 28 | + |
| 29 | +@requires_ethosn |
| 30 | +def test_ethosn78_partition_no_error(): |
| 31 | + a = relay.var("a", shape=[2, 7, 8, 8], dtype="uint8") |
| 32 | + w = relay.const(np.random.uniform(-10, 10, (8, 7, 3, 3)).astype("uint8")) |
| 33 | + res = relay.nn.conv2d(a, w, kernel_size=(3, 3), padding=(1, 1), channels=8, out_dtype="uint8") |
| 34 | + b = relay.var("b", shape=[8], dtype="uint8") |
| 35 | + res = relay.nn.bias_add(res, b, axis=1) |
| 36 | + |
| 37 | + mod = tvm.IRModule.from_expr(res) |
| 38 | + opts = {"variant": "Ethos-N78"} |
| 39 | + partition_for_ethosn78(mod, **opts) |
| 40 | + |
| 41 | + |
| 42 | +@requires_ethosn |
| 43 | +def test_ethosn78_partition_undefined_variant(): |
| 44 | + with pytest.raises( |
| 45 | + ValueError, match=r".*When targeting Ethos\(TM\)-N78, -variant=Ethos-N78 should be set.*" |
| 46 | + ): |
| 47 | + a = relay.var("a", shape=[2, 7, 8, 8], dtype="uint8") |
| 48 | + w = relay.const(np.random.uniform(-10, 10, (8, 7, 3, 3)).astype("uint8")) |
| 49 | + res = relay.nn.conv2d( |
| 50 | + a, w, kernel_size=(3, 3), padding=(1, 1), channels=8, out_dtype="uint8" |
| 51 | + ) |
| 52 | + b = relay.var("b", shape=[8], dtype="uint8") |
| 53 | + res = relay.nn.bias_add(res, b, axis=1) |
| 54 | + |
| 55 | + mod = tvm.IRModule.from_expr(res) |
| 56 | + partition_for_ethosn78(mod) |
| 57 | + |
| 58 | + |
| 59 | +@requires_ethosn |
| 60 | +def test_ethosn78_partition_invalid_variant(): |
| 61 | + with pytest.raises( |
| 62 | + ValueError, match=r".*When targeting Ethos\(TM\)-N78, -variant=Ethos-N78 should be set.*" |
| 63 | + ): |
| 64 | + a = relay.var("a", shape=[2, 7, 8, 8], dtype="uint8") |
| 65 | + w = relay.const(np.random.uniform(-10, 10, (8, 7, 3, 3)).astype("uint8")) |
| 66 | + res = relay.nn.conv2d( |
| 67 | + a, w, kernel_size=(3, 3), padding=(1, 1), channels=8, out_dtype="uint8" |
| 68 | + ) |
| 69 | + b = relay.var("b", shape=[8], dtype="uint8") |
| 70 | + res = relay.nn.bias_add(res, b, axis=1) |
| 71 | + |
| 72 | + mod = tvm.IRModule.from_expr(res) |
| 73 | + opts = {"variant": "Ethos-N"} |
| 74 | + partition_for_ethosn78(mod, **opts) |
| 75 | + |
| 76 | + |
| 77 | +@requires_ethosn |
| 78 | +def test_ethosn78_partition_error(): |
| 79 | + with pytest.raises( |
| 80 | + ValueError, match=r".*When targeting Ethos\(TM\)-N78, -variant=Ethos-N78 should be set.*" |
| 81 | + ): |
| 82 | + a = relay.var("a", shape=[2, 7, 8, 8], dtype="uint8") |
| 83 | + w = relay.const(np.random.uniform(-10, 10, (8, 7, 3, 3)).astype("uint8")) |
| 84 | + res = relay.nn.conv2d( |
| 85 | + a, w, kernel_size=(3, 3), padding=(1, 1), channels=8, out_dtype="uint8" |
| 86 | + ) |
| 87 | + b = relay.var("b", shape=[8], dtype="uint8") |
| 88 | + res = relay.nn.bias_add(res, b, axis=1) |
| 89 | + |
| 90 | + mod = tvm.IRModule.from_expr(res) |
| 91 | + opts = {"variant": "Ethos-N77"} |
| 92 | + partition_for_ethosn78(mod, **opts) |
| 93 | + |
| 94 | + |
| 95 | +@requires_ethosn |
| 96 | +def test_ethosn77_partition_no_error(): |
| 97 | + a = relay.var("a", shape=[2, 7, 8, 8], dtype="uint8") |
| 98 | + w = relay.const(np.random.uniform(-10, 10, (8, 7, 3, 3)).astype("uint8")) |
| 99 | + res = relay.nn.conv2d(a, w, kernel_size=(3, 3), padding=(1, 1), channels=8, out_dtype="uint8") |
| 100 | + b = relay.var("b", shape=[8], dtype="uint8") |
| 101 | + res = relay.nn.bias_add(res, b, axis=1) |
| 102 | + |
| 103 | + mod = tvm.IRModule.from_expr(res) |
| 104 | + partition_for_ethosn77(mod) |
| 105 | + |
| 106 | + |
| 107 | +@requires_ethosn |
| 108 | +def test_ethosn77_partition_error(): |
| 109 | + with pytest.raises( |
| 110 | + ValueError, |
| 111 | + match=r".*Setting tops, ple_ratio or sram_size has no effect when targeting Ethos\(TM\)-N77.*", |
| 112 | + ): |
| 113 | + a = relay.var("a", shape=[2, 7, 8, 8], dtype="uint8") |
| 114 | + w = relay.const(np.random.uniform(-10, 10, (8, 7, 3, 3)).astype("uint8")) |
| 115 | + res = relay.nn.conv2d( |
| 116 | + a, w, kernel_size=(3, 3), padding=(1, 1), channels=8, out_dtype="uint8" |
| 117 | + ) |
| 118 | + b = relay.var("b", shape=[8], dtype="uint8") |
| 119 | + res = relay.nn.bias_add(res, b, axis=1) |
| 120 | + |
| 121 | + mod = tvm.IRModule.from_expr(res) |
| 122 | + opts = {"tops": 4} |
| 123 | + partition_for_ethosn77(mod, **opts) |
0 commit comments