areusch commented on a change in pull request #8280: URL: https://github.com/apache/tvm/pull/8280#discussion_r678611615
########## File path: python/tvm/micro/interface_api.py ########## @@ -0,0 +1,81 @@ +# 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. + +"""Defines functions for generating a C interface header""" + +import os + +from tvm.relay.backend.utils import mangle_module_name + + +def _emit_brief(header_file, model_name, description): + header_file.write("/*!\n") + header_file.write(f" * \\brief TVM {model_name} model {description} \n") + header_file.write(" */\n") + + +def generate_c_interface_header(model_name, inputs, outputs, output_path): + """Generates a C interface header for a given models inputs and outputs + + Parameters + ---------- + model_name : str + Name of the model to be used in defining structs and naming the header + inputs : list[str] + List of model input names to be placed in generated structs + outputs : list[str] + List of model output names to be placed in generated structs + output_path : str + Path to the output folder to generate the header into + """ + + mangled_name = mangle_module_name(model_name) + metadata_header = os.path.join(output_path, f"{mangled_name}.h") + with open(metadata_header, "w") as header_file: + _emit_brief(header_file, model_name, "input tensors") + header_file.write(f"struct {mangled_name}_inputs {{\n") + for input_name in inputs: + header_file.write(f" void* {input_name};\n") + header_file.write("};\n\n") + + _emit_brief(header_file, model_name, "output tensors") + header_file.write(f"struct {mangled_name}_outputs {{\n") + for output_name in outputs: + header_file.write(f" void* {output_name};\n") + header_file.write("};\n\n") + + _emit_brief(header_file, model_name, "memory blocks") + header_file.write(f"struct {mangled_name}_memory {{\n") + header_file.write("};\n\n") + + _emit_brief(header_file, model_name, "device configurations") Review comment: per the discuss forum--i think we should leave out devices and introduce it when we have a use case ready to merge. i don't want to get it wrong on first try. ########## File path: python/tvm/micro/interface_api.py ########## @@ -0,0 +1,81 @@ +# 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. + +"""Defines functions for generating a C interface header""" + +import os + +from tvm.relay.backend.utils import mangle_module_name + + +def _emit_brief(header_file, model_name, description): + header_file.write("/*!\n") + header_file.write(f" * \\brief TVM {model_name} model {description} \n") + header_file.write(" */\n") + + +def generate_c_interface_header(model_name, inputs, outputs, output_path): + """Generates a C interface header for a given models inputs and outputs + + Parameters + ---------- + model_name : str + Name of the model to be used in defining structs and naming the header + inputs : list[str] + List of model input names to be placed in generated structs + outputs : list[str] + List of model output names to be placed in generated structs + output_path : str + Path to the output folder to generate the header into + """ + + mangled_name = mangle_module_name(model_name) + metadata_header = os.path.join(output_path, f"{mangled_name}.h") + with open(metadata_header, "w") as header_file: + _emit_brief(header_file, model_name, "input tensors") + header_file.write(f"struct {mangled_name}_inputs {{\n") Review comment: could you add struct-level \brief documentation here? ########## File path: python/tvm/micro/interface_api.py ########## @@ -0,0 +1,81 @@ +# 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. + +"""Defines functions for generating a C interface header""" + +import os + +from tvm.relay.backend.utils import mangle_module_name + + +def _emit_brief(header_file, model_name, description): + header_file.write("/*!\n") + header_file.write(f" * \\brief TVM {model_name} model {description} \n") Review comment: perhaps we could favor something explicitly identifying the model_name, since that may not make sense to the user: ``` \\brief {description} for TVM module "{module_name}" ``` ########## File path: python/tvm/micro/interface_api.py ########## @@ -0,0 +1,81 @@ +# 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. + +"""Defines functions for generating a C interface header""" + +import os + +from tvm.relay.backend.utils import mangle_module_name + + +def _emit_brief(header_file, model_name, description): + header_file.write("/*!\n") + header_file.write(f" * \\brief TVM {model_name} model {description} \n") + header_file.write(" */\n") + + +def generate_c_interface_header(model_name, inputs, outputs, output_path): + """Generates a C interface header for a given models inputs and outputs + + Parameters + ---------- + model_name : str + Name of the model to be used in defining structs and naming the header + inputs : list[str] + List of model input names to be placed in generated structs + outputs : list[str] + List of model output names to be placed in generated structs + output_path : str + Path to the output folder to generate the header into + """ + + mangled_name = mangle_module_name(model_name) Review comment: let's not implicitly rename model_name == module_name inside this interface; either refer to it as module_name throughout or do it in one place e.g. at the top level. i guess if we do the latter, that would suggest to rename mangle_module_name to something more specific to codegen e.g. mangle_c_function_name ########## File path: python/tvm/micro/interface_api.py ########## @@ -0,0 +1,81 @@ +# 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. + +"""Defines functions for generating a C interface header""" + +import os + +from tvm.relay.backend.utils import mangle_module_name + + +def _emit_brief(header_file, model_name, description): + header_file.write("/*!\n") + header_file.write(f" * \\brief TVM {model_name} model {description} \n") + header_file.write(" */\n") + + +def generate_c_interface_header(model_name, inputs, outputs, output_path): Review comment: not required for this PR, but i'd like to add this information as MLF metadata in either `metadata.json` or e.g. `executor-config/aot/metadata.json` cc @guberti -- 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]
