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

Add some verbosity in torch-sys:build.rs for results of command PYTHON_PRINT_PYTORCH_DETAILS if it has a non-empty stderr. fixes #843 #845

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions torch-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ See the readme for more details:
https://github.com/LaurentMazare/tch-rs/blob/main/README.md
";

const NO_PYTORCH_DURING_BUILD_TIME_ERROR_MESSAGE : &str = r"
LIBTORCH_USE_PYTORCH=1 is detected, but PyTorch Python package has not been found during build time of torch-sys:
- Check if the PyTorch package is in build dependencies. Example for 'pyproject.toml' : [build-system] requires = ['setuptools', 'torch'].
- Check if the Python interpreter 'python' (for virtual env.) or 'python3' (for others) has the torch library installed.

It seems like that the Rust code uses the wrong Python interpreter, without torch installed on it.
";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LinkType {
Dynamic,
Expand Down Expand Up @@ -209,6 +217,19 @@ impl SystemInfo {
.arg(PYTHON_PRINT_PYTORCH_DETAILS)
.output()
.with_context(|| format!("error running {python_interpreter:?}"))?;

//Check for output.stderr emptiness and try to detect if it's related to torch module's missing
let output_stderr = std::str::from_utf8(&output.stderr)?;
if output_stderr.len()>0 {
if output_stderr.contains("ModuleNotFoundError: No module named 'torch'")
{
anyhow::bail!(NO_PYTORCH_DURING_BUILD_TIME_ERROR_MESSAGE);
}
println!(
"cargo:warning={}",
format!("The command output to retrieve PyTorch details during build time has a non empty stderr field : {output_stderr}")
);
}
let mut cxx11_abi = None;
for line in String::from_utf8_lossy(&output.stdout).lines() {
if let Some(version) = line.strip_prefix("LIBTORCH_VERSION: ") {
Expand Down