I have not done much ensembling so I don’t know all the nuances of the best implementation. One simple way of how I would go about implementing this, is to first store the output of the two models (after softmax, so I can iterate faster). Then do a random search from 0 to 1, until I find a good weight value.
How I would go about implementing it. You can use a smaller dataset to get the value of weight, as you goal is to generalize, not to memorize the training set.
model1_out = # torch.tensor of shape [num_images, num_classes]
model2_out = # torch.tensor of shape [num_images, num_classes]

low = 0.0
high = 1.0
check_vals = low + torch.rand(10)*(high-low)

for val in check_vals:
    out = model1_out*val + model2_out*(1-val)
    # compute your metric
    
# change the values of `low`, `high`, depending upon the best value
# that you got for your metric, until you are satisfied.