"""Training and inference logic. Keep this stable — notebooks and deployment/ both depend on these interfaces."""