Python Fine-tuning with C# Inference: A Dual-Track Pipeline

Fine-tune where the ecosystem is, serve where the process already runs, and put a diff between the two so they cannot silently drift.

Language versions [EN] [中文]
Cite

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.

  1. Fine-tune in Python, export to ONNX with a fixed opset and explicit dynamic axes.
  2. Verify: run a frozen set of inputs through both PyTorch and ONNX Runtime, compare.
  3. 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.

Related reading

Writing//13 min read

Building a TIR Compiler: From Patent to Prototype

A rule graph, one intermediate representation, and three backends. What was easy, what leaked, and which abstraction I would not build again.

Writing//14 min read

Enterprise AI Agents in .NET 10: Wiring an Agent to Real Systems

What changes when an agent has to call into a system with an audit log, a permission model and a rate limit, instead of a demo endpoint.

Video//1 min read

One Rule Graph, Three Backends: A TIR Compiler Demo

Lowering the same rule graph to a spreadsheet add-in, a service and an FPGA pipeline, and the four disagreements the conformance suite found.

Patent//5 min read

Lowering a Uniform Rule Graph to Heterogeneous Evaluation Targets

The disclosure behind the TIR compiler, including the width and latency negotiation that the first implementation got wrong.

Cite this entry

Copy the block you need.

BibTeX
@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}
}
RIS
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  - 
APA
Mr Huang (2026). Python Fine-tuning with C# Inference: A Dual-Track Pipeline. selfevo. https://selfevo.dev/en/blog/tech/python-finetuning-csharp-inference