Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] clarify instructions to load PythonCall #163

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions docs/src/manual/PyTorch.md
Original file line number Diff line number Diff line change
@@ -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
@@ -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");
@@ -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");
@@ -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");
@@ -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");
29 changes: 20 additions & 9 deletions docs/src/tutorials/pytorch.jl
Original file line number Diff line number Diff line change
@@ -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
@@ -46,6 +56,7 @@ using Test
import Ipopt
import MathOptAI
import Plots
import PythonCall

# ## Training a model

10 changes: 9 additions & 1 deletion src/predictors/PytorchModel.jl
Original file line number Diff line number Diff line change
@@ -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");
```