forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate C Interface API Generation to C++
Using the new name transformations added in apache#9088, the C interface API is now generated in C++ rather than in Python. Follow up PRs will clean up any remaining name transformation inconsistencies. Fixes apache#8792
- Loading branch information
Showing
11 changed files
with
385 additions
and
116 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,132 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/*! | ||
* \file interface_c.cc | ||
* \brief Generates a C interface header for a given modules inputs and outputs | ||
*/ | ||
|
||
#include <tvm/runtime/container/array.h> | ||
#include <tvm/runtime/container/string.h> | ||
#include <tvm/runtime/module.h> | ||
#include <tvm/runtime/packed_func.h> | ||
#include <tvm/runtime/registry.h> | ||
|
||
#include <string> | ||
|
||
#include "../../relay/backend/name_transforms.h" | ||
|
||
namespace tvm { | ||
namespace codegen { | ||
|
||
using runtime::PackedFunc; | ||
using namespace tvm::relay::backend; | ||
|
||
class InterfaceCNode : public runtime::ModuleNode { | ||
public: | ||
InterfaceCNode(std::string module_name, Array<String> inputs, Array<String> outputs) | ||
: module_name_(module_name), inputs_(inputs), outputs_(outputs) {} | ||
const char* type_key() const { return "h"; } | ||
|
||
std::string GetSource(const std::string& format) final { | ||
std::stringstream code; | ||
std::string mangled_module_name = ToCVariableStyle(PrefixGeneratedName({module_name_})); | ||
std::string header_guard_name = ToCConstantStyle(PrefixGeneratedName({module_name_})); | ||
|
||
EmitUpperHeaderGuard(code, header_guard_name); | ||
EmitBrief(code, "Input tensor pointers"); | ||
EmitStruct(code, mangled_module_name, "inputs", inputs_); | ||
EmitBrief(code, "Output tensor pointers"); | ||
EmitStruct(code, mangled_module_name, "outputs", outputs_); | ||
EmitRunFunction(code, mangled_module_name); | ||
EmitLowerHeaderGuard(code, header_guard_name); | ||
|
||
return code.str(); | ||
} | ||
|
||
PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self) final { | ||
return PackedFunc(nullptr); | ||
} | ||
|
||
private: | ||
void EmitUpperHeaderGuard(std::stringstream& code_stream, const std::string& header_guard_name) { | ||
code_stream << "#ifndef " << header_guard_name << "_H_\n" | ||
<< "#define " << header_guard_name << "_H_\n" | ||
<< "#include <stdint.h>\n\n" | ||
<< "#ifdef __cplusplus\n" | ||
<< "extern \"C\" {\n" | ||
<< "#endif\n\n"; | ||
} | ||
|
||
void EmitLowerHeaderGuard(std::stringstream& code_stream, const std::string& header_guard_name) { | ||
code_stream << "\n#ifdef __cplusplus\n" | ||
<< "}\n" | ||
<< "#endif\n\n" | ||
<< "#endif // " << header_guard_name << "_H_\n"; | ||
} | ||
|
||
void EmitBrief(std::stringstream& code_stream, const std::string& description) { | ||
code_stream << "/*!\n" | ||
<< " * \\brief " << description << " for TVM module \"" << module_name_ << "\" \n" | ||
<< " */\n"; | ||
} | ||
|
||
void EmitStruct(std::stringstream& code_stream, const std::string& mangled_module_name, | ||
const std::string& suffix, Array<String> properties) { | ||
code_stream << "struct " << mangled_module_name << "_" << suffix << " {\n"; | ||
|
||
std::vector<std::string> sanitised_properties; | ||
for (const String& property : properties) { | ||
std::string sanitised_property = SanitiseName(property); | ||
ICHECK(std::find(sanitised_properties.begin(), sanitised_properties.end(), | ||
sanitised_property) == sanitised_properties.end()) | ||
<< "Sanitized input tensor name clash" << sanitised_property; | ||
code_stream << " void* " << sanitised_property << ";\n"; | ||
sanitised_properties.push_back(sanitised_property); | ||
} | ||
code_stream << "};\n\n"; | ||
} | ||
|
||
void EmitRunFunction(std::stringstream& code_stream, const std::string& mangled_module_name) { | ||
code_stream << "/*!\n" | ||
<< " * \\brief entrypoint function for TVM module \"" << module_name_ << "\"\n" | ||
<< " * \\param inputs Input tensors for the module \n" | ||
<< " * \\param outputs Output tensors for the module \n" | ||
<< " */\n" | ||
<< "int32_t " << mangled_module_name << "_run(\n" | ||
<< " struct " << mangled_module_name << "_inputs* inputs,\n" | ||
<< " struct " << mangled_module_name << "_outputs* outputs\n" | ||
<< ");\n"; | ||
} | ||
|
||
std::string module_name_; | ||
Array<String> inputs_; | ||
Array<String> outputs_; | ||
}; | ||
|
||
runtime::Module InterfaceCCreate(std::string module_name, Array<String> inputs, | ||
Array<String> outputs) { | ||
auto n = make_object<InterfaceCNode>(module_name, inputs, outputs); | ||
return runtime::Module(n); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("runtime.InterfaceCCreate").set_body_typed(InterfaceCCreate); | ||
|
||
} // namespace codegen | ||
} // namespace tvm |
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
Oops, something went wrong.