1161 const std::string &modelNamespace,
bool sgemmAlreadyDeclared)
1163 auto need = [&](
const char *key) {
return neededHelpers.count(key) > 0; };
1165 const bool im2col = need(
"Im2col");
1166 const bool im2col3d = need(
"Im2col_3d");
1167 const bool col2im = need(
"col2im");
1168 const bool uniBroadcast = need(
"UnidirectionalBroadcast");
1169 const bool convBias = need(
"BroadcastConvBias");
1170 const bool gemm = need(
"Gemm_Call");
1171 const bool relu = need(
"Relu");
1172 const bool fill = need(
"Fill");
1173 const bool copy = need(
"Copy");
1174 const bool readTensor = need(
"ReadTensorFromStream");
1175 const bool safetensorsBlob = need(
"SafetensorsBlob");
1176 const bool readSafetensors = need(
"SafetensorsReader");
1177 const bool inputDims = need(
"InputTensorDims");
1178 const bool dynMemory = need(
"DynamicMemory");
1180 const bool im2colFamily = im2col || im2col3d || col2im;
1181 const bool needConvertLength = uniBroadcast || convBias;
1182 const bool needConvertString = convBias;
1185 std::set<std::string> stdHeaders;
1186 auto addStd = [&](std::initializer_list<const char *> hs) {
1188 stdHeaders.insert(
h);
1191 if (im2colFamily || uniBroadcast || convBias || gemm || fill || copy || dynMemory)
1192 addStd({
"algorithm"});
1193 if (needConvertLength || uniBroadcast || dynMemory)
1194 addStd({
"vector",
"cstddef"});
1195 if (needConvertString || convBias)
1196 addStd({
"sstream",
"string",
"stdexcept"});
1198 addStd({
"string",
"istream",
"stdexcept",
"limits"});
1199 if (safetensorsBlob)
1200 addStd({
"cstddef",
"string"});
1201 if (readSafetensors)
1202 addStd({
"cstdint",
"cstring",
"string",
"string_view",
"unordered_map",
"stdexcept",
"vector"});
1204 addStd({
"array",
"string_view",
"cstddef"});
1206 addStd({
"set",
"unordered_map",
"stdexcept",
"iterator"});
1208 std::string includes;
1209 for (
auto const &
h : stdHeaders)
1210 includes +=
"#include <" +
h +
">\n";
1215 defs +=
"\n// --- Standalone SOFIE inference helper functions ---\n";
1218 if (gemm && !sgemmAlreadyDeclared)
1221 if (needConvertLength)
1222 defs += kConvertShapeToLength;
1223 if (needConvertString)
1224 defs += kConvertShapeToString;
1226 if (im2colFamily || uniBroadcast || convBias) {
1227 defs +=
"\nnamespace UTILITY {\n";
1237 defs += kBroadcastTensor;
1239 defs += kBroadcastConvBias;
1240 defs +=
"} // namespace UTILITY\n";
1252 defs += kReadTensorFromStream;
1253 if (safetensorsBlob)
1254 defs += kSafetensorsBlob;
1255 if (readSafetensors)
1256 defs += kSafetensorsReader;
1262 defs +=
"// --- End of SOFIE inference helper functions ---\n\n";
1275 std::string cladDefs;
1276 if (gemm || copy || fill || relu) {
1277 cladDefs +=
"\nnamespace clad {\nnamespace custom_derivatives {\nnamespace " + modelNamespace +
" {\n";
1284 cladDefs +=
"using ::" + modelNamespace +
"::Gemm_Call;\n";
1285 cladDefs +=
"namespace SOFIE_MODEL_NS = ::" + modelNamespace +
";\n";
1286 cladDefs += kGemmCallPullback;
1287 cladDefs += kGemmCallPushforward;
1290 cladDefs += kCopyPullback;
1291 cladDefs += kCopyPushforward;
1294 cladDefs += kFillPullback;
1295 cladDefs += kFillPushforward;
1298 cladDefs += kReluPullback;
1299 cladDefs += kReluPushforward;
1301 cladDefs +=
"} // namespace " + modelNamespace +
"\n} // namespace custom_derivatives\n} // namespace clad\n";
1304 return HelperFunctionsCode{std::move(includes), std::move(defs), std::move(cladDefs)};
HelperFunctionsCode GenerateHelperFunctionsCode(const std::set< std::string > &neededHelpers, const std::string &modelNamespace, bool sgemmAlreadyDeclared=false)
Return the standalone C++ source of the inference helper functions requested in neededHelpers (see RM...