NAME

PDL::Torch - LibTorch (PyTorch C++) GPU/CPU tensor acceleration for PDL

SYNOPSIS

use PDL;
use PDL::Torch qw(pdl2tensor tensor2pdl cuda_available);

my $device = cuda_available() ? 'cuda' : 'cpu';

my $a = pdl2tensor(double([[1, 2], [3, 4]]), $device);
my $b = pdl2tensor(double([[5, 6], [7, 8]]), $device);

my $c = ($a + $b) x $a;   # elementwise add + matrix multiply on GPU/CPU
my $out = $c->pdl();      # transfer back to PDL ndarray

DESCRIPTION

PDL::Torch binds PDL ndarrays directly to LibTorch (PyTorch's C++ ATen tensor engine) via C99 XS and a C++17 bridge, enabling hardware-accelerated tensor operations on CPU, CUDA, ROCm, and Apple Metal (MPS).

Because Perl uses deterministic reference counting, GPU VRAM held by PDL::Torch::Tensor objects is released immediately when the Perl scalar goes out of scope.

FUNCTIONS

pdl2tensor

Convert a PDL ndarray to a PDL::Torch::Tensor on the requested device.

$tensor = pdl2tensor($pdl);
$tensor = pdl2tensor($pdl, 'cuda');
my $t = pdl2tensor(float([1, 2, 3, 4]), 'cpu');

tensor2pdl

Convert a PDL::Torch::Tensor (from CPU or GPU) back into a PDL ndarray.

$pdl = tensor2pdl($tensor);
my $p = tensor2pdl($t);

cuda_available

Returns true (1) if LibTorch detects a usable CUDA GPU, or 0 otherwise.

$has_cuda = cuda_available();

pdl2torch_type

Map a PDL type name (e.g. double, cfloat) to its LibTorch dtype name.

$torch_dtype = pdl2torch_type($pdl_type);

torch2pdl_type

Map a LibTorch dtype name (e.g. float64, complex64) to its PDL type name.

$pdl_type = torch2pdl_type($torch_dtype);

SEE ALSO

PDL::Torch::Tensor, PDL, PDL::IO::Arrow, PDL::IO::Protobuf

AUTHOR

C.J. Collier <cjac@google.com>

LICENSE

Licensed under the Apache License, Version 2.0.