Skip to content

Commit

Permalink
Update to resolve FutureWarning from torch.load
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Oct 20, 2024
1 parent 40987b5 commit a9535c5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions ext/MathOptAIPythonCallExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function MathOptAI.build_predictor(
end
torch = PythonCall.pyimport("torch")
nn = PythonCall.pyimport("torch.nn")
torch_model = torch.load(predictor.filename)
torch_model = torch.load(predictor.filename; weights_only = false)
return _predictor(nn, torch_model, config)
end

Expand Down Expand Up @@ -134,7 +134,7 @@ function MathOptAI.GrayBox(
hessian::Bool = false,
)
torch = PythonCall.pyimport("torch")
torch_model = torch.load(predictor.filename)
torch_model = torch.load(predictor.filename; weights_only = false)
J = torch.func.jacrev(torch_model)
H = torch.func.hessian(torch_model)
# TODO(odow): I'm not sure if there is a better way to get the output
Expand Down
14 changes: 7 additions & 7 deletions test/test_PythonCall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end

function _evaluate_model(filename, x)
torch = PythonCall.pyimport("torch")
torch_model = torch.load(filename)
torch_model = torch.load(filename; weights_only = false)
input = torch.tensor(x)
return PythonCall.pyconvert(Vector, torch_model(input).detach().numpy())
end
Expand Down Expand Up @@ -299,7 +299,7 @@ function test_model_Tanh_scalar_GrayBox()
)
model = Model(Ipopt.Optimizer)
set_silent(model)
@variable(model, x[1:2])
@variable(model, x[i in 1:2] == i)
ml_model = MathOptAI.PytorchModel(filename)
y, formulation =
MathOptAI.add_predictor(model, ml_model, x; gray_box = true)
Expand Down Expand Up @@ -331,7 +331,7 @@ function test_model_Tanh_scalar_GrayBox_hessian()
)
model = Model(Ipopt.Optimizer)
set_silent(model)
@variable(model, x[1:2])
@variable(model, x[i in 1:2] == i)
ml_model = MathOptAI.PytorchModel(filename)
y, formulation = MathOptAI.add_predictor(
model,
Expand Down Expand Up @@ -369,7 +369,7 @@ function test_model_Tanh_vector_GrayBox()
# Full-space
model = Model(Ipopt.Optimizer)
set_silent(model)
@variable(model, x[1:3])
@variable(model, x[i in 1:3] == i)
ml_model = MathOptAI.PytorchModel(filename)
y, formulation =
MathOptAI.add_predictor(model, ml_model, x; gray_box = true)
Expand All @@ -381,7 +381,7 @@ function test_model_Tanh_vector_GrayBox()
# Reduced-space
model = Model(Ipopt.Optimizer)
set_silent(model)
@variable(model, x[1:3])
@variable(model, x[i in 1:3] == i)
ml_model = MathOptAI.PytorchModel(filename)
y, formulation = MathOptAI.add_predictor(
model,
Expand Down Expand Up @@ -419,7 +419,7 @@ function test_model_Tanh_vector_GrayBox_hessian()
# Full-space
model = Model(Ipopt.Optimizer)
set_silent(model)
@variable(model, x[1:3])
@variable(model, x[i in 1:3] == i)
ml_model = MathOptAI.PytorchModel(filename)
y, formulation = MathOptAI.add_predictor(
model,
Expand All @@ -436,7 +436,7 @@ function test_model_Tanh_vector_GrayBox_hessian()
# Reduced-space
model = Model(Ipopt.Optimizer)
set_silent(model)
@variable(model, x[1:3])
@variable(model, x[i in 1:3] == i)
ml_model = MathOptAI.PytorchModel(filename)
y, formulation = MathOptAI.add_predictor(
model,
Expand Down

0 comments on commit a9535c5

Please sign in to comment.