libtorch.py integration#1
Conversation
There was a problem hiding this comment.
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.pyprovidingtorchify()(SymPy→Torch + optional symbolic change-of-vars) andtorchquad_integrate(). - Updated
src/libsympy.pywith torch/loguru-related imports/output, fixedget_piecewisesignature, and adjustedtensor_to_productTensorProduct 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.
|
|
||
| Usage | ||
| ===== | ||
| from libphysics.libtorch import torchify, torchquad_integrate |
There was a problem hiding this comment.
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.
| from libphysics.libtorch import torchify, torchquad_integrate | |
| from libtorch import torchify, torchquad_integrate |
| def torchquad_integrate(texpr, params_values=None, method=None, N=21): | ||
| """ | ||
| Numerically integrate a ``TorchExpr`` using torchquad. | ||
|
|
||
| Parameters |
There was a problem hiding this comment.
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).
| from loguru import logger | ||
| logger.remove() | ||
|
|
There was a problem hiding this comment.
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.
| from loguru import logger | |
| logger.remove() |
| print('torch:', torch.__version__, '| cuda:', torch.version.cuda, '| cuda available:', torch.cuda.is_available(), '| device:', device) | ||
| print('torch file:', getattr(torch, '__file__', None)) |
There was a problem hiding this comment.
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.
| 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)) |
| if method is None: | ||
| method = Simpson() | ||
|
|
||
| param_vals = list(params_values) if params_values else [] |
There was a problem hiding this comment.
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.
| param_vals = list(params_values) if params_values else [] | |
| param_vals = list(params_values) if params_values is not None else [] |
| else: | ||
| # finite [a, b] | ||
| new_vars.append(v) | ||
| domain.append([float(lower), float(upper)]) |
There was a problem hiding this comment.
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.
added libtorch.py for torch integration.