Skip to content

Commit

Permalink
[docs] clarify instructions to load PythonCall (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Nov 7, 2024
1 parent 42017ae commit 08b8173
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
32 changes: 21 additions & 11 deletions docs/src/manual/PyTorch.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,37 @@ torch.save(model, "saved_pytorch_model.pt")
MathOptAI uses [PythonCall.jl](https://github.com/JuliaPy/PythonCall.jl) to call
from Julia into Python.

See [CondaPkg.jl](https://github.com/JuliaPy/CondaPkg.jl) for more control over
how to link Julia to an existing Python environment. For example, if you have an
existing Python installation (with PyTorch installed), and it is available in
the current Conda environment, set:
To use [`PytorchModel`](@ref) your code must load the `PythonCall` package:
```julia
import PythonCall
```

PythonCall uses [CondaPkg.jl](https://github.com/JuliaPy/CondaPkg.jl) to manage
Python dependencies. See [CondaPkg.jl](https://github.com/JuliaPy/CondaPkg.jl)
for more control over how to link Julia to an existing Python environment. For
example, if you have an existing Python installation (with PyTorch installed),
and it is available in the current Conda environment, do:

```julia
ENV["JULIA_CONDAPKG_BACKEND"] = "Current"
import PythonCall
```

before importing PythonCall.jl. If the Python installation can be found on the
path and it is not in a Conda environment, set:
If the Python installation can be found on the path and it is not in a Conda
environment, do:

```julia
ENV["JULIA_CONDAPKG_BACKEND"] = "Null"
import PythonCall
```

If `python` is not on your path, you may additionally need to set
`JULIA_PYTHONCALL_EXE`, for example, to:
`JULIA_PYTHONCALL_EXE`, for example, do:

```julia
ENV["JULIA_PYTHONCALL_EXE"] = "python3"
ENV["JULIA_CONDAPKG_BACKEND"] = "Null"
import PythonCall
```

## Basic example
Expand All @@ -67,7 +77,7 @@ Use [`MathOptAI.add_predictor`](@ref) to embed a PyTorch model into a JuMP
model:

```@repl
using JuMP, MathOptAI
using JuMP, MathOptAI, PythonCall
model = Model();
@variable(model, x[1:1]);
predictor = MathOptAI.PytorchModel("saved_pytorch_model.pt");
Expand All @@ -81,7 +91,7 @@ formulation
Use the `reduced_space = true` keyword to formulate a reduced-space model:

```@repl
using JuMP, MathOptAI
using JuMP, MathOptAI, PythonCall
model = Model();
@variable(model, x[1:1]);
predictor = MathOptAI.PytorchModel("saved_pytorch_model.pt");
Expand All @@ -96,7 +106,7 @@ formulation
Use the `gray_box = true` keyword to embed the network as a nonlinear operator:

```@repl
using JuMP, MathOptAI
using JuMP, MathOptAI, PythonCall
model = Model();
@variable(model, x[1:1]);
predictor = MathOptAI.PytorchModel("saved_pytorch_model.pt");
Expand All @@ -112,7 +122,7 @@ Pass a dictionary to the `config` keyword that maps the `Symbol` name of each
PyTorch layer to a MathOptAI predictor:

```@repl
using JuMP, MathOptAI
using JuMP, MathOptAI, PythonCall
model = Model();
@variable(model, x[1:1]);
predictor = MathOptAI.PytorchModel("saved_pytorch_model.pt");
Expand Down
29 changes: 20 additions & 9 deletions docs/src/tutorials/pytorch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,40 @@

# ## Python integration

# MathOptAI uses [PythonCall.jl](https://github.com/JuliaPy/PythonCall.jl)
# to call from Julia into Python.
# MathOptAI uses [PythonCall.jl](https://github.com/JuliaPy/PythonCall.jl) to call
# from Julia into Python.

# To use [`PytorchModel`](@ref) your code must load the `PythonCall` package:
# ```julia
# import PythonCall
# ```
#
# See [CondaPkg.jl](https://github.com/JuliaPy/CondaPkg.jl) for more control
# over how to link Julia to an existing Python environment. For example, if you
# have an existing Python installation (with PyTorch installed), and it is
# available in the current Conda environment, set:
# PythonCall uses [CondaPkg.jl](https://github.com/JuliaPy/CondaPkg.jl) to manage
# Python dependencies. See [CondaPkg.jl](https://github.com/JuliaPy/CondaPkg.jl)
# for more control over how to link Julia to an existing Python environment. For
# example, if you have an existing Python installation (with PyTorch installed),
# and it is available in the current Conda environment, do:
#
# ```julia
# ENV["JULIA_CONDAPKG_BACKEND"] = "Current"
# import PythonCall
# ```
#
# before importing PythonCall.jl. If the Python installation can be found on
# the path and it is not in a Conda environment, set:
# If the Python installation can be found on the path and it is not in a Conda
# environment, do:
#
# ```julia
# ENV["JULIA_CONDAPKG_BACKEND"] = "Null"
# import PythonCall
# ```
#
# If `python` is not on your path, you may additionally need to set
# `JULIA_PYTHONCALL_EXE`, for example, to:
# `JULIA_PYTHONCALL_EXE`, for example, do:
#
# ```julia
# ENV["JULIA_PYTHONCALL_EXE"] = "python3"
# ENV["JULIA_CONDAPKG_BACKEND"] = "Null"
# import PythonCall
# ```

# ## Required packages
Expand All @@ -46,6 +56,7 @@ using Test
import Ipopt
import MathOptAI
import Plots
import PythonCall

# ## Training a model

Expand Down
10 changes: 9 additions & 1 deletion src/predictors/PytorchModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ A wrapper struct for loading a PyTorch model.
The only supported file extension is `.pt`, where the `.pt` file has been
created using `torch.save(model, filename)`.
!!! warning
To use [`PytorchModel`](@ref), your code must load the `PythonCall` package:
```julia
import PythonCall
```
## Example
```jldoctest
julia> using PythonCall, MathOptAI
julia> using MathOptAI
julia> using PythonCall # This line is important!
julia> predictor = PytorchModel("model.pt");
```
Expand Down

0 comments on commit 08b8173

Please sign in to comment.