The sensible path is to train where the libraries are and infer where the process already runs. The risk is drift: two runtimes interpreting the same model file slightly differently, and nobody noticing until a user does.
The pipeline
Three stages, one artefact between each.
- Fine-tune in Python, export to ONNX with a fixed opset and explicit dynamic axes.
- Verify: run a frozen set of inputs through both PyTorch and ONNX Runtime, compare.
- Serve from C# with ONNX Runtime, and re-run the same verification in CI on every build.
torch.onnx.export(
model,
dummy_inputs,
"model.onnx",
opset_version=18,
input_names=["input_ids", "attention_mask"],
output_names=["logits"],
dynamic_axes={"input_ids": {0: "batch", 1: "seq"}, "attention_mask": {0: "batch", 1: "seq"}},
)
The verification that earns its keep
The parity check is a small fixed corpus of inputs with expected logits stored alongside. Both runtimes must be within a tolerance, and the tolerance is chosen for the decision the logits feed, not for float precision.
var expected = ParityCorpus.Load("parity/corpus.json");
foreach (var sample in expected)
{
var actual = _session.Run(sample.Inputs);
var delta = MaxAbsDifference(sample.Logits, actual);
Assert.True(delta <= sample.Tolerance, $"{sample.Id}: delta {delta:E3}");
}
This has caught: an opset mismatch after a library upgrade, a tokenizer revision that silently changed padding, and one genuine ONNX Runtime kernel difference on a specific sequence length. None of the three would have been visible from accuracy metrics.
Quantisation is a decision, not a step
Post-training quantisation to INT8 is where the two tracks diverge most, because the runtime’s kernels and the calibration data both matter. The rule I use: quantise, then re-run the parity corpus, then look at the distribution of errors, not the mean.
A mean absolute difference of 0.002 is uninteresting if 1 in 4000 inputs is off by 2.4.
Sizing the deployment
The C# side carries a fixed memory arena and no allocator on the inference path. On the edge board this is the difference between a stable 90-second window and a slow climb into thermal throttling. That story is in the on-device entry.
What I would tell someone starting this
Pick the parity corpus before you pick the model. It is the only artefact in the pipeline that tells you when you have broken something, and it costs an afternoon to build.
Cite this entry
Copy the block you need.
@misc{huang2026pythonfinetuningwi,
author = {Mr Huang},
title = {Python Fine-tuning with C# Inference: A Dual-Track Pipeline},
year = {2026},
url = {https://selfevo.dev/en/blog/tech/python-finetuning-csharp-inference},
note = {selfevo, blog}
} TY - GEN
AU - Mr Huang
TI - Python Fine-tuning with C# Inference: A Dual-Track Pipeline
PY - 2026
UR - https://selfevo.dev/en/blog/tech/python-finetuning-csharp-inference
PB - selfevo
ER - Mr Huang (2026). Python Fine-tuning with C# Inference: A Dual-Track Pipeline. selfevo. https://selfevo.dev/en/blog/tech/python-finetuning-csharp-inference