← Back to list

How to Connect Perl/PDL with TensorFlow/Keras to Leverage GPU on macOS

Most of today’s Deep Learning ecosystem runs on Python, but many scientists and developers have been working for years with Perl and its…

Marcontk · 2025-08-30 02:39 · 1 claps · 3.3 min read
#perl-programming #deep-learning #gpu-computing #hybrid-work #image-processing
Open on Medium ↗
Wiki topics: RAG · RAG & Retrieval OPS · LLMOps & Inference ML · Machine Learning EDU · Education & Learning 💻 · Programming

How to Connect Perl/PDL with TensorFlow/Keras to Leverage GPU on macOS

Most of today’s Deep Learning ecosystem runs on Python, but many scientists and developers have been working for years with Perl and its powerful numerical ecosystem PDL (Perl Data Language).

Is it possible to keep scientific workflows in Perl while also taking advantage of GPU acceleration that TensorFlow/Keras provides on macOS through Metal?

The answer is yes. To achieve this, I first created a tensorflow-metal environment with Miniconda, and on top of that I built a Perl ⇆ TF/Keras bridge that allows training and inference of neural networks directly from PDL, with execution accelerated on the GPU.

Why a Bridge?

Perl + PDL: excellent for handling large numerical arrays, NetCDF, GMT, and scientific pipelines.

TensorFlow/Keras: direct access to optimized libraries (cuDNN, Metal Performance Shaders on macOS).

Problem: AI::MXNet and AI::TensorFlow::Libtensorflow in Perl currently do not support GPU acceleration on macOS. I had already developed functional code for image classification and autoencoder training directly in Perl, which worked well enough for relatively simple architectures. However, as I started to build deeper and more complex networks, CPU-only training quickly became a bottleneck. The computation times were tolerable at first, but when projecting towards more demanding solutions — such as denoising autoencoders with large datasets or multi-layer convolutional models — the lack of GPU support made the workflow impractical. Since I wanted to continue leveraging my existing Perl ecosystem and resources, I needed a hybrid solution where Perl remains the controller, orchestrating data preparation with PDL::NDArray, while delegating heavy GPU-based operations to Python/TensorFlow, and then bringing the results back into Perl for analysis and further processing.

Solution: use Perl as the “controller,” preparing data in PDL::NDArray, and delegate heavy GPU operations to Python/TensorFlow. Then, bring the results back into Perl.

Minimal Implementation

In Perl (TFMini.pm)

In Python (tfbox.py)

Use Case: Autoencoder on macOS with GPU Metal

  1. Train the autoencoder from Perl, passing data_nd, label_nd to TensorFlow via the bridge.

  2. Training runs on the GPU through Metal (AMD Radeon Pro in my case).

  3. Return the training history as CSV, plot it with GMT or Chart::Plotly from Perl.

  4. Run inference with tf_predict_nchw() to obtain reconstructions directly in PDL.

The screenshot below shows the full workflow in action. TensorFlow detects and uses the Metal GPU backend on macOS (AMD Radeon Pro 555X in my case). Training runs directly on the GPU, with loss and val_loss decreasing over epochs. At the end of training, inference is executed through the Perl ⇆ TF/Keras bridge: the reconstructed tensor is returned to Perl/PDL, and basic metrics (MSE, min, mean, max) are computed natively in PDL. This proves that the bridge is not just theoretical — Perl/PDL can stay as the scientific data engine, while TensorFlow/Keras leverages GPU acceleration on macOS through Metal.

Example console output

Example console output

Advantages of this Approach

• Keep the Perl/PDL ecosystem for scientific I/O (NetCDF, GMT,).

• Delegate GPU acceleration to TensorFlow/Keras on macOS with Metal support.

• Avoid rewriting entire pipelines in Python: Perl controls, Python accelerates.

  • Scalable: can switch backend (GPU, CPU, even remote TPU) without altering the Perl flow.

Conclusion

Although the Deep Learning world revolves around Python, it is still possible to work in Perl and connect both worlds with a simple and effective bridge.

Thus, PDL remains my engine for scientific data, while TensorFlow/Keras handles GPU acceleration on macOS.

This pattern (binary NDArray + JSON → Python → NDArray) opens the door for any legacy Perl code to benefit from modern AI libraries without abandoning the ecosystem.

Disclaimer

This development is not intended to “compete” with Python or with mainstream Deep Learning frameworks. The objective is practical and personal: to maintain my working ecosystem in Perl/PDL (scientific pipeline built on Perl + PDL + NetCDF + GMT) while also taking advantage of GPU acceleration (Metal on macOS) without rewriting everything in Python.

This Perl ⇆ TF/Keras bridge is a compromise solution: it adds a bit of I/O overhead, but it allows those who already have robust pipelines in Perl to access modern GPU training and inference capabilities without rewriting their entire codebase in another language.


메타데이터
post_id
add3e8372d2d
slug
how-to-connect-perl-pdl-with-tensorflow-keras-to-leverage-gpu-on-macos-add3e8372d2d
url
https://medium.com/@marcontk/how-to-connect-perl-pdl-with-tensorflow-keras-to-leverage-gpu-on-macos-add3e8372d2d
canonical_url
https://medium.com/@marcontk/how-to-connect-perl-pdl-with-tensorflow-keras-to-leverage-gpu-on-macos-add3e8372d2d
author_url
https://medium.com/@marcontk
status
ok
fetched_at
2026-06-24 16:30:55