[KongchangAI]
· 2 min read· 1,262 words

TorchCurves: Turning Parametric Curves into Learnable Layers — A New PyTorch Library Bridging KAN and Function Regression

TorchCurves: Turning Parametric Curves into Learnable Layers — A New PyTorch Library Bridging KAN and Function Regression

TorchCurves turns B-spline and Legendre parametric curves into differentiable PyTorch modules, making curves trainable, composable layers.

TorchCurves is a new library in the PyTorch ecosystem that implements parametric curves under B-spline and Legendre polynomial bases as differentiable modules. Its core operation maps a batch of scalar inputs to curve points, differentiable with respect to both inputs and curve coefficients. Curve coefficients can be optimized independently or generated by other networks, and the library offers three usage modes: trainable modules, basis modules, and a functional interface. Target scenarios include continuous feature embedding, Kolmogorov-Arnold Networks, and function regression. Engineering-wise it emphasizes readability and does not write custom hardware kernels; timing experiments give only qualitative conclusions: generality is feasible at some scales but noticeably costly at others. Its positioning is to make curves composable building blocks in the automatic differentiation graph, rather than to replace numerical libraries like SciPy.

In deep learning frameworks, vector-valued parametric curves are usually just visualization tools or data interpolation utilities, rarely treated as trainable first-class citizens. A new library in the PyTorch ecosystem, TorchCurves, aims to change that: it implements parametric curves under B-spline and Legendre polynomial bases as differentiable modules, allowing curve coefficients to be optimized just like neural network weights.

TorchCurves project schematic

Core Operation: From Batches of Scalars to Curve Points

The central operation of TorchCurves is mapping a batch of scalar inputs to points on a curve. The curve is represented by B-spline or Legendre polynomial bases, and the forward computation is differentiable both with respect to the inputs and with respect to the curve coefficients. This means you can embed the curve into a larger model and let gradients propagate from the loss function all the way back to control points or polynomial coefficients.

This design enables two typical usages: one is optimizing curve coefficients directly as standalone parameters; the other is having the coefficients generated by another network, forming a composite structure where "the network outputs a curve, and the curve outputs coordinates." The library itself provides three types of interfaces—trainable modules, reusable basis modules, and a functional interface—covering needs ranging from quick experimentation to modular integration.

B-spline basis is a mathematical tool for constructing smooth curves from piecewise polynomials; the curve is formed as a linear combination of a set of control points (coefficients) and basis functions defined on a knot vector. The local support property of B-splines means that adjusting a single control point only affects the local shape of the curve, making them naturally suited as learnable parameters. The Legendre polynomial basis is a set of orthogonal polynomials defined on the interval [-1, 1], commonly used for function approximation; its orthogonality makes coefficient estimation more stable, but the global support means each coefficient affects the entire curve. Both are implemented as basis modules in TorchCurves, and the core difference lies in the trade-off between locality and globality: B-splines excel at representing piecewise-smooth local features, while the Legendre basis is suited to representing low-order global trends.

Differentiable modules refer to components that follow the PyTorch nn.Module interface and whose forward computation consists entirely of tensors operations that support automatic differentiation. Unlike ordinary modules, the key here is that curve evaluation is simultaneously differentiable with respect to input coordinates and curve coefficients, so gradients can pass through the curve layer back to upstream networks or directly update the coefficients.

Application Scenarios: Continuous Feature Embedding and KAN

The author lists several directions in the project description: continuous feature embedding, Kolmogorov-Arnold Networks (KAN), and function regression. KAN is a recently popularized architecture whose edge functions are essentially learnable univariate curves. TorchCurves abstracts such edge functions into basis modules, providing exactly a replaceable curve-evaluation backend for KAN.

Function regression is more direct: when the target is a continuous function rather than discrete labels, using a parametric curve as the output head is more natural than a fully connected layer. Continuous feature embedding targets tasks where the inputs themselves are continuous coordinates, such as spatial position encoding or implicit neural representations.

According to the project description, these scenarios share the same requirement—curve evaluation must be differentiable, batchable, and freely composable with other PyTorch modules.

Kolmogorov-Arnold Networks (KAN) are a neural network architecture based on the Kolmogorov-Arnold representation theorem: the theorem states that a multivariate continuous function can be represented as a composition of finitely many univariate continuous functions and addition. KAN replaces the weights on each "edge" with learnable univariate functions (typically parameterized by splines), and nodes only perform summation. This differs from MLPs, which place nonlinearity at the nodes; KAN places nonlinearity on the edges, so the evaluation quality and differentiability of edge functions directly determine model capability. The basis modules provided by TorchCurves can serve precisely as the evaluation backend for these edge functions, allowing researchers to train KAN without hand-deriving basis function derivatives.

Function regression differs from ordinary regression in that the target is an entire continuous function rather than discrete labels. When using a parametric curve as the output head, the model directly outputs curve coefficients and then evaluates at arbitrary coordinates, naturally supporting variable-resolution outputs and supervision over a continuous domain.

Implementation Trade-offs: Readability First, No Custom Kernels

The engineering choices of TorchCurves are noteworthy. It combines classical curve evaluation algorithms with vectorized PyTorch operations, explicitly emphasizing readability and maintainability, and does not write custom hardware kernels for performance. This positioning makes the library easier to review and modify, but it also means additional overhead at extreme scales.

The author conducted two sets of timing experiments: one comparing curve evaluation against NumPy and SciPy, and another comparing KAN training against EfficientKAN across different problem scales. The conclusions are qualitative—this generality remains computationally feasible at some scales, while at others the overhead is quite noticeable. The original text does not provide specific speedup ratios or absolute runtimes, so the safer interpretation is: TorchCurves targets scenarios where flexibility and differentiability come first, rather than production inference chasing maximum throughput.

Custom hardware kernels usually refer to low-level operators written with CUDA, Triton, or C++ extensions that can bypass PyTorch's generic dispatch overhead and perform fusion and parallel optimization for specific computation patterns. Scientific computing libraries pursuing extreme performance often write dedicated kernels for regular computations like curve evaluation, but the cost is reduced portability and code that is harder to review. TorchCurves chooses pure vectorized PyTorch operations, meaning it runs consistently on CPU/GPU and across different backends, but every evaluation goes through generic operator dispatch, making overhead more noticeable in small-batch or frequently-called scenarios. This also explains the qualitative conclusion from the timing experiments: "feasible at some scales, noticeably costly at others."

Ecosystem Position and Usage Recommendations

In the PyTorch ecosystem, differentiable curve libraries are uncommon. SciPy's spline tools are mature but not differentiable, while NumPy is suitable for numerical computation but does not participate in automatic differentiation. TorchCurves fills the abstraction layer of "curves as layers," exposing the mathematical structure of B-spline and Legendre bases directly as modules.

For researchers wanting to try KAN or function regression, this library offers a low-barrier path: no need to derive basis function derivatives yourself, and no need to hand-write batched evaluation. For engineering teams focused on performance, however, it is advisable to first benchmark on your own problem scale before deciding whether to adopt it. The project is open source on GitHub at github.com/alexshtf/torchcurves.

A reasonable judgment is: the value of TorchCurves lies not in replacing existing numerical libraries, but in bringing parametric curves into the automatic differentiation graph, making "curves" composable building blocks in model design.

One-line Summary

TorchCurves turns B-spline and Legendre parametric curves into differentiable PyTorch modules, making curves trainable, composable layers.

Paragraph Summary

TorchCurves is a new library in the PyTorch ecosystem that implements parametric curves under B-spline and Legendre polynomial bases as differentiable modules. Its core operation maps a batch of scalar inputs to curve points, differentiable with respect to both inputs and curve coefficients. Curve coefficients can be optimized independently or generated by other networks, and the library offers three usage modes: trainable modules, basis modules, and a functional interface. Target scenarios include continuous feature embedding, Kolmogorov-Arnold Networks, and function regression. Engineering-wise it emphasizes readability and does not write custom hardware kernels; timing experiments give only qualitative conclusions: generality is feasible at some scales but noticeably costly at others. Its positioning is to make curves composable building blocks in the automatic differentiation graph, rather than to replace numerical libraries like SciPy.

#TorchCurves#PyTorch#differentiable curves#B-spline#Legendre polynomials#Kolmogorov-Arnold Networks#KAN#function regression#continuous feature embedding#automatic differentiation
Share:

Related articles