ONNX export leverage TorchIR to convert PyTorch model to ONNX graph. TorchIR is generated by trace or script, so you will still have the same limitation if you use trace to export to ONNX.
What the problem of using script to capture control flow and loop?