Just accidentally stumbled on the right function torch.jit.is_tracing().
My linter says it’s not exported, so I’m not sure whether it’s safe to use but it says the same for nn.Parameter so there is probably nothing to worry about. Linters never liked Pytorch anyway.