|
| 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 | +# pylint: disable=missing-docstring, |
| 18 | + |
| 19 | +from typing import Any |
| 20 | + |
| 21 | +from tvm import relax |
| 22 | + |
| 23 | +from ...ir_builder import relax as R |
| 24 | +from ...ir_builder.base import IRBuilder |
| 25 | +from .._core import Parser, dispatch, doc |
| 26 | + |
| 27 | + |
| 28 | +def bind_assign_value(self: Parser, node: doc.expr, var_name: str, value: Any) -> Any: |
| 29 | + # pylint: disable=unused-argument |
| 30 | + if isinstance(value, relax.Expr): |
| 31 | + var = R.emit(value) |
| 32 | + IRBuilder.name(var_name, var) |
| 33 | + return var |
| 34 | + else: |
| 35 | + raise TypeError(f"Unsupported type {type(value)} in assignment") |
| 36 | + |
| 37 | + |
| 38 | +@dispatch.register(token="relax", type_name="FunctionDef") |
| 39 | +def visit_function_def(self: Parser, node: doc.FunctionDef) -> None: |
| 40 | + with self.var_table.with_frame(): |
| 41 | + with R.function(): |
| 42 | + R.func_name(node.name) |
| 43 | + if node.returns is not None: |
| 44 | + R.func_ret_type(self.eval_expr(node.returns).type) |
| 45 | + with self.with_dispatch_token("relax"): |
| 46 | + self.visit(node.args) |
| 47 | + self.visit_body(node.body) |
| 48 | + |
| 49 | + |
| 50 | +@dispatch.register(token="relax", type_name="Expr") |
| 51 | +def visit_expr_stmt(self: Parser, node: doc.FunctionDef) -> None: |
| 52 | + self.eval_expr(node.value) |
| 53 | + |
| 54 | + |
| 55 | +@dispatch.register(token="relax", type_name="arguments") |
| 56 | +def visit_arguments(self: Parser, node: doc.arguments) -> None: |
| 57 | + arg: doc.arg |
| 58 | + for arg in node.args: |
| 59 | + if arg.annotation is None: |
| 60 | + self.report_error(arg, "Type annotation is required for function parameters.") |
| 61 | + param_type = self.visit_tvm_annotation(arg.annotation) |
| 62 | + param = R.arg(arg.arg, param_type) |
| 63 | + |
| 64 | + self.var_table.add(arg.arg, param) |
| 65 | + |
| 66 | + |
| 67 | +@dispatch.register(token="relax", type_name="tvm_annotation") |
| 68 | +def visit_tvm_annotation(self: Parser, node: doc.expr): |
| 69 | + annotation = self.eval_expr(node) |
| 70 | + if callable(annotation): |
| 71 | + annotation = annotation() |
| 72 | + return annotation |
| 73 | + |
| 74 | + |
| 75 | +@dispatch.register(token="relax", type_name="Assign") |
| 76 | +def visit_assign(self: Parser, node: doc.Assign) -> None: |
| 77 | + if len(node.targets) != 1: |
| 78 | + self.report_error(node, "Consequential assignments like 'a = b = c' are not supported.") |
| 79 | + lhs = node.targets[0] |
| 80 | + rhs = self.eval_expr(node.value) |
| 81 | + self.eval_assign(target=lhs, source=rhs, bind_value=bind_assign_value) |
| 82 | + |
| 83 | + |
| 84 | +@dispatch.register(token="relax", type_name="Return") |
| 85 | +def visit_return(self: Parser, node: doc.Assign) -> None: |
| 86 | + value = self.eval_expr(node.value) |
| 87 | + |
| 88 | + if isinstance(value, relax.Expr): |
| 89 | + R.func_ret_value(value) |
| 90 | + else: |
| 91 | + self.report_error(node, f"Unsupported return value type {type(value)}.") |
0 commit comments