areusch commented on a change in pull request #8811: URL: https://github.com/apache/tvm/pull/8811#discussion_r712597746
########## File path: python/tvm/relay/backend/contrib/ethosu/tir/utils.py ########## @@ -0,0 +1,222 @@ +# 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. +# pylint: disable=invalid-name +"""Helper utility functions used by the TIR compiler""" +import tvm +from tvm import arith + + +# TODO(@mbaret): Formalise this with a specification Review comment: is there a GH tracking issue for this? same question below. ########## File path: python/tvm/relay/backend/contrib/ethosu/tir/spec.py ########## @@ -0,0 +1,263 @@ +# 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. +"""The TIR serialization specification for Arm(R) Ethos(TM)-U NPU.""" +from typing import Union +from typing import get_type_hints +from inspect import isclass + +import tvm +from tvm.relay.backend.contrib.ethosu import util + + +def create_serial_object(serialized_type, deserialized_elements): + """ + This function will create serialized type that is one of the subclasses + of tvm.relay.backend.contrib.ethosu.tir.spec.SerializableFormat + + Parameters + ---------- + serialized_type : a subclass type of SerializableFormat + + deserialized_elements : list + The list of arguments that needs to packed to create SerializableFormat objects + + Returns + ------- + The constructed object of type serialized_type + """ + + def _create_serial_object(internal_serialized_type, read_element_idx=0): + """The internal function that increments the read_element_idx + when creating nested serial objects""" + arg_len = util.get_arg_count(internal_serialized_type.__init__) - 1 + serial_init_types = get_type_hints(internal_serialized_type.__init__) + serial_init_arg_names = list(serial_init_types.keys()) + serial_init_args = [] + assert arg_len == len(serial_init_arg_names) + for si_arg_name in serial_init_arg_names: + si_arg_type = serial_init_types[si_arg_name] + if isclass(si_arg_type) and issubclass(si_arg_type, SerializableFormat): + sia, read_element_idx = _create_serial_object(si_arg_type, read_element_idx) + serial_init_args.append(sia) + else: + serial_init_args.append(deserialized_elements[read_element_idx]) + read_element_idx += 1 + return internal_serialized_type(*serial_init_args), read_element_idx + + # Just return the primary serial object + return _create_serial_object(serialized_type)[0] + + +class SerializableFormat: + """Base class to retrieve arguments on a predefined ordering""" + + def __iter__(self): + # Note class attribute definition order is preserved - see PEP 520 + for name in self.__dict__: + value = self.__getattribute__(name) + if isinstance(value, SerializableFormat): + yield from list(value) + else: + yield value + + def __getitem__(self, index): + # Note class attribute definition order is preserved - see PEP 520 + name = list(self.__dict__.keys())[index] + return self.__getattribute__(name) + + +class SerialFeatureMap(SerializableFormat): + """Specialization class to retrieve arguments of a Feature Map + (similiar to NpuFeatureMap of Vela) on a predefined ordering""" + + def __init__( + self, Review comment: is it necessary to list all of these things twice? could you somehow leverage NamedTuple and pass `*args` to map positional to name? ########## File path: tests/python/contrib/test_ethosu/test_compiler.py ########## @@ -0,0 +1,48 @@ +# 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. +import pytest + +pytest.importorskip("ethosu.vela") +import tvm +from tvm import relay +from tvm.relay.backend.contrib.ethosu.tir.compiler import lower_to_tir + + +def test_lower_to_tir(): + data = relay.var("data", shape=(1, 1, 1, 1024), dtype="uint8") + weight = relay.var("weight", shape=(1, 1, 1024, 1001), dtype="int8") + p2 = relay.var("p2", shape=(1, 1, 1, 1), dtype="int32") + conv = relay.nn.conv2d( + data, + weight, + kernel_size=(1, 1), + data_layout="NHWC", + kernel_layout="HWIO", + out_dtype="int32", + ) + multiply = relay.multiply(relay.const(-22, dtype="int32"), p2) + tile = relay.tile(multiply, reps=(1, 1, 1, 1001)) + subtract = relay.subtract(conv, tile) + func = subtract + expr = relay.Function(relay.analysis.free_vars(func), func) + mod = tvm.IRModule.from_expr(expr) + mod = relay.transform.InferType()(mod) + lower_to_tir(mod["main"]) + + +if __name__ == "__main__": + pytest.main([__file__]) Review comment: ```suggestion pytest.main([__file__] + sys.argv[1:]) ``` here and in all other test_*.py ########## File path: src/relay/backend/contrib/ethosu/compiler_attrs.cc ########## @@ -0,0 +1,73 @@ +/* + * 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. + */ + +#include <tvm/ir/error.h> +#include <tvm/relay/analysis.h> +#include <tvm/relay/attrs/annotation.h> +#include <tvm/relay/expr.h> +#include <tvm/relay/expr_functor.h> +#include <tvm/relay/transform.h> + +#include <unordered_map> +#include <unordered_set> +#include <utility> +#include <vector> + +#include "../../../op/make_op.h" + +namespace tvm { +namespace relay { +namespace contrib { +namespace ethosu { + +/*! \brief Attributes to store the compiler options for Arm(R) Ethos(TM)-U NPU. */ +struct EthosUCompilerConfigNode : public tvm::AttrsNode<EthosUCompilerConfigNode> { + String accelerator_config; + + TVM_DECLARE_ATTRS(EthosUCompilerConfigNode, "ext.attrs.EthosUCompilerConfigNode") { + TVM_ATTR_FIELD(accelerator_config) + .describe( + "The class of Arm(R) Ethos(TM)-U NPU; possible values = {ethos-u55-32, ethos-u55-64, " Review comment: here it's called "class" but elsewhere it's termed "variant." Which term is appropriate? it would be great to settle on one and update elsewhere as appropriate. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
