Skip to content

libtorch.py integration#1

Open
ibeuler wants to merge 11 commits into
ferhatpy:mainfrom
ibeuler:main
Open

libtorch.py integration#1
ibeuler wants to merge 11 commits into
ferhatpy:mainfrom
ibeuler:main

Conversation

@ibeuler

@ibeuler ibeuler commented Feb 9, 2026

Copy link
Copy Markdown

added libtorch.py for torch integration.

Copilot AI review requested due to automatic review settings February 9, 2026 17:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds initial Torch/torchquad integration utilities to support evaluating SymPy expressions with Torch and performing repeated numerical integrations efficiently.

Changes:

  • Added src/libtorch.py providing torchify() (SymPy→Torch + optional symbolic change-of-vars) and torchquad_integrate().
  • Updated src/libsympy.py with torch/loguru-related imports/output, fixed get_piecewise signature, and adjusted tensor_to_product TensorProduct detection.
  • Added a baseline .gitignore.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 6 comments.

File Description
src/libtorch.py New SymPy→Torch conversion + torchquad integration helpers.
src/libsympy.py Adds torch/loguru import-time behavior; minor function adjustments.
.gitignore Adds common Python/venv/IDE/log ignores.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/libtorch.py Outdated

Usage
=====
from libphysics.libtorch import torchify, torchquad_integrate

Copilot AI Feb 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage example shows from libphysics.libtorch import ..., but this repo doesn’t appear to be packaged as libphysics (no pyproject.toml/setup.py and src/ has no __init__.py). This makes the example import path likely incorrect for users/tests. Consider updating the example to match how modules are imported in this repo (e.g., via ../src on sys.path as in existing tests) or add package scaffolding.

Suggested change
from libphysics.libtorch import torchify, torchquad_integrate
from libtorch import torchify, torchquad_integrate

Copilot uses AI. Check for mistakes.
Comment thread src/libtorch.py Outdated
Comment on lines +187 to +191
def torchquad_integrate(texpr, params_values=None, method=None, N=21):
"""
Numerically integrate a ``TorchExpr`` using torchquad.

Parameters

Copilot AI Feb 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/libtorch.py adds new integration behavior (change-of-variables for infinite bounds + torchquad integration) but there are no tests exercising it. Since the repo already has Python tests, add coverage for at least: finite bounds, ±oo bounds mapping, and a complex-valued integrand returning correct (re, im).

Copilot uses AI. Check for mistakes.
Comment thread src/libsympy.py Outdated
Comment on lines +53 to +55
from loguru import logger
logger.remove()

Copilot AI Feb 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loguru is imported unconditionally and logger.remove() is executed at import time. This introduces a new runtime dependency (not referenced elsewhere in the repo) and will raise ImportError for users/environments that don’t have loguru installed; additionally, removing all loguru sinks is a global side effect that can break application logging. Prefer the stdlib logging module (or make this optional/guarded) and avoid mutating global logger configuration during import.

Suggested change
from loguru import logger
logger.remove()

Copilot uses AI. Check for mistakes.
Comment thread src/libsympy.py Outdated
Comment on lines +58 to +59
print('torch:', torch.__version__, '| cuda:', torch.version.cuda, '| cuda available:', torch.cuda.is_available(), '| device:', device)
print('torch file:', getattr(torch, '__file__', None))

Copilot AI Feb 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These print(...) statements run on module import and will spam stdout whenever libsympy is imported (including during tests) and can break consumers that rely on clean stdout. If this is needed for debugging, gate it behind an explicit flag / __main__ block, or use a configurable logger; also the second import torch here is redundant.

Suggested change
print('torch:', torch.__version__, '| cuda:', torch.version.cuda, '| cuda available:', torch.cuda.is_available(), '| device:', device)
print('torch file:', getattr(torch, '__file__', None))
logger.debug(
"torch: {} | cuda: {} | cuda available: {} | device: {}",
torch.__version__,
torch.version.cuda,
torch.cuda.is_available(),
device,
)
logger.debug("torch file: {}", getattr(torch, "__file__", None))

Copilot uses AI. Check for mistakes.
Comment thread src/libtorch.py Outdated
if method is None:
method = Simpson()

param_vals = list(params_values) if params_values else []

Copilot AI Feb 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

params_values is checked via truthiness (if params_values), which will raise for inputs like torch.Tensor or numpy.ndarray (their truth value is ambiguous). Use an explicit is not None check instead so callers can pass arrays/tensors safely.

Suggested change
param_vals = list(params_values) if params_values else []
param_vals = list(params_values) if params_values is not None else []

Copilot uses AI. Check for mistakes.
Comment thread src/libtorch.py Outdated
Comment on lines +160 to +163
else:
# finite [a, b]
new_vars.append(v)
domain.append([float(lower), float(upper)])

Copilot AI Feb 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

domain.append([float(lower), float(upper)]) will throw TypeError for finite bounds that are SymPy expressions (e.g., symbolic parameters) and gives a low-signal error. Either validate that finite bounds are numeric (and raise a clear ValueError), or accept SymPy numbers via sympy.N(...)/float(...) only when conversion is safe.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants