-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of relay_to_tir target hook
This the first new hook proposed in the Additional Target Hooks RFC, longer term the compilation should move to using `Target` proper but this unblocks our current work whilst illustrating the eventual interface via `Target` in `target_kind.cc` I've encapsulated the hook lookup into a method on `TargetKind` (`GetRegisteredHook`), which will eventually mean that the logic can be compacted to: ``` func->target->kind.GetRegisteredHook() ```
- Loading branch information
Showing
5 changed files
with
166 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""Unit tests for target hooks.""" | ||
import sys | ||
import numpy as np | ||
import pytest | ||
|
||
import tvm | ||
import tvm.relay.testing | ||
import tvm.relay.transform | ||
|
||
from tvm import relay | ||
from utils.external_codegen import ( | ||
set_external_func_attr, | ||
check_aot_executor_result, | ||
check_graph_executor_result, | ||
) | ||
|
||
|
||
def translate_relay_add_to_tir_subtract(ir_module, relay_func): | ||
"""A transform to test Relay -> TIR with""" | ||
ib = tvm.tir.ir_builder.create() | ||
A = tvm.tir.decl_buffer( | ||
dtype=relay_func.params[0].checked_type.dtype, | ||
name=relay_func.params[0].name_hint, | ||
shape=relay_func.params[0].checked_type.shape, | ||
) | ||
B = tvm.tir.decl_buffer( | ||
dtype=relay_func.params[1].checked_type.dtype, | ||
name=relay_func.params[1].name_hint, | ||
shape=relay_func.params[1].checked_type.shape, | ||
) | ||
C = tvm.tir.decl_buffer(dtype=relay_func.ret_type.dtype, shape=relay_func.ret_type.shape) | ||
|
||
Ap = ib.buffer_ptr(A) | ||
Bp = ib.buffer_ptr(B) | ||
Cp = ib.buffer_ptr(C) | ||
|
||
with ib.for_range(0, 8, name="i") as i: | ||
with ib.for_range(0, 8, name="j") as j: | ||
row = i * 8 | ||
Cp[row + j] = Ap[row + j] - Bp[row + j] | ||
|
||
prim_func = tvm.tir.PrimFunc([A, B, C], ib.get()) | ||
|
||
ir_module = tvm.lower(prim_func, name=relay_func.attrs["global_symbol"]) | ||
return ir_module | ||
|
||
|
||
@pytest.mark.parametrize("check_result", [check_graph_executor_result, check_aot_executor_result]) | ||
def test_tir_external_generation(check_result): | ||
tvm.register_func("target.test.tir_lowering", translate_relay_add_to_tir_subtract, True) | ||
|
||
shape = (8, 8) | ||
x_data = np.random.randint(255, size=shape).astype("float32") | ||
y_data = np.random.randint(255, size=shape).astype("float32") | ||
inputs = {"x": x_data, "y": y_data} | ||
|
||
x0 = relay.var("x0", shape=shape, dtype="float32") | ||
y0 = relay.var("y0", shape=shape, dtype="float32") | ||
z = x0 + y0 | ||
f = relay.Function([x0, y0], z) | ||
f = set_external_func_attr(f, "test", "replace_add_with_subtract") | ||
|
||
x = relay.var("x", shape=(8, 8), dtype="float32") | ||
y = relay.var("y", shape=(8, 8), dtype="float32") | ||
call = relay.Call(f, [x, y]) | ||
func = tvm.IRModule.from_expr(call) | ||
|
||
check_result(func, inputs, (8, 8), x_data - y_data) | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(pytest.main([__file__] + sys.argv[1:])) |