#!/usr/bin/env python3 import argparse, os, pathlib, textwrap from onnx_utils import onnx_io from onnx import TensorProto # ONNX dtype -> (ctype, printf, ONNX_TYPE_*) DTYPES = { TensorProto.FLOAT: ("float", "%g", "ONNX_TYPE_FLOAT"), TensorProto.DOUBLE: ("double", "%g", "ONNX_TYPE_DOUBLE"), TensorProto.INT64: ("int64_t", "%lld","ONNX_TYPE_INT64"), TensorProto.INT32: ("int32_t", "%d", "ONNX_TYPE_INT32"), TensorProto.UINT8: ("uint8_t", "%u", "ONNX_TYPE_UINT8"), TensorProto.INT8: ("int8_t", "%d", "ONNX_TYPE_INT8"), TensorProto.BOOL: ("uint8_t", "%u", "ONNX_TYPE_BOOL"), # stored as byte TensorProto.FLOAT16: ("uint16_t", "%u", "ONNX_TYPE_FLOAT16"), # raw 16-bit TensorProto.BFLOAT16:("uint16_t", "%u", "ONNX_TYPE_BFLOAT16"), } def esc(s): return s.replace("\\","\\\\").replace('"','\\"') def gen_c(inputs, outputs, entry, so_name): in_blocks=[] for i,name,et,shape in inputs: if et not in DTYPES: raise ValueError(f"Unsupported dtype for input '{name}': {et}") cty, pfmt, onnx_ty = DTYPES[et] shp_list = ", ".join(str(d) for d in shape) if shape else "" rank = len(shape) in_blocks.append(textwrap.dedent(f""" // ---- Input {i}: "{esc(name)}" ({cty}) ---- const char *in{i}_csv=NULL, *in{i}_csv_file=NULL, *in{i}_shape_str=NULL; char *in{i}_csv_buf=NULL; // holds file contents if --in{i}-csv-file used int has_in{i}=0; double in{i}_fill=0.0; int in{i}_fill_set=0; for (int ai=1; ai=in{i}_nelem) break; double v=atof(tok); (({cty}*)in{i}_buf)[idx++] = ({cty})v; tok=strtok(NULL,",\\n\\r\\t "); }} free(buf); if(idx!=in{i}_nelem){{fprintf(stderr,"ERROR: CSV provided %lld values, expected %lld.\\n",(long long)idx,in{i}_nelem); if(in{i}_csv_buf) free(in{i}_csv_buf); return 2;}} }} else if (in{i}_fill_set) {{ {cty} vv=({cty})in{i}_fill; for(long long t=0;t.csv" char fname[512]; // simple sanitizer: copy name => replace non [A-Za-z0-9_.-] with '_' char clean[256]; int ci=0; const char *src="{safe}"; for (; src[ci] && ci < 255; ++ci) {{ char ch = src[ci]; int ok = (ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')||(ch>='0'&&ch<='9')||ch=='_'||ch=='-'||ch=='.'; clean[ci] = ok ? ch : '_'; }} clean[ci] = '\\0'; snprintf(fname, sizeof(fname), "%s/output{oi}_%s.csv", save_csv_dir, clean); FILE *csv = fopen(fname, "w"); if (!csv) {{ perror("fopen --save-csv-dir"); }} else {{ OMTensor *t = omTensorListGetOmtByIndex(out_list, {oi}); int64_t rank = omTensorGetRank(t); int64_t const *shape = omTensorGetShape(t); long long numel = 1; for (int64_t k=0;k #include #include #include OMTensorList *{entry}(OMTensorList *inputs); int main(int argc, char **argv) {{ // optional: --save-csv-dir (directory must exist) const char *save_csv_dir = NULL; for (int ai=1; ai