NAME

PDL::IO::Protobuf - Read and write PDL ndarrays from Protocol Buffers

VERSION

Version 0.01

SYNOPSIS

use PDL;
use PDL::IO::Protobuf qw(rprotobuf wprotobuf from_protobuf to_protobuf);
use My::Embedding::Vector;

# Convert a PDL ndarray to a Protobuf message and write to disk
my $vec = float([0.12, -0.45, 0.88, 0.31]);
wprotobuf($vec, 'embedding.pb', 'My::Embedding::Vector', 'values');

# Read a binary .pb file directly into a PDL ndarray
my $loaded = rprotobuf('embedding.pb', 'My::Embedding::Vector', 'values',
    proto_type => 'float',
);

# Convert in-memory Protobuf messages (e.g. from Google::gRPC / Vertex AI)
my $matrix = from_protobuf($msg, 'values',
    proto_type => 'float',
    dims       => [2, 2],
);
my $out_msg = to_protobuf($matrix, 'My::Embedding::Vector', 'values');

DESCRIPTION

PDL::IO::Protobuf bridges PDL (Perl Data Language) ndarrays and Protobuf messages backed by Google's upb C/XS engine (or pure-Perl fallback). It provides idiomatic PDL::IO-style functions ("rprotobuf", "wprotobuf") as well as in-memory converters ("from_protobuf", "to_protobuf") for working with repeated numeric fields such as ML embeddings, sensor time series, and scientific tensors.

FUNCTIONS

rprotobuf($source, $class, $field, %opts)

my $pdl = rprotobuf('data.pb', 'My::Message', 'values', proto_type => 'float');

Reads binary Protocol Buffers wire bytes from a file path or open filehandle $source, parses them with $class->parse($bytes), and converts $field into a PDL ndarray via "from_protobuf".

wprotobuf($pdl, $dest, $class_or_msg, $field, %opts)

wprotobuf($pdl, 'data.pb', 'My::Message', 'values');

Converts $pdl into a Protobuf::Message via "to_protobuf", serializes it with $msg->serialize(), and writes the binary wire bytes to file path or filehandle $dest. Returns the constructed message.

from_protobuf($msg, $field, %opts)

my $pdl = from_protobuf($msg, 'values',
    proto_type => 'float',   # or type => 'float'
    dims       => [128, 64], # optional reshape dimensions
);

Extracts repeated numeric field $field from $msg and returns a PDL ndarray. Supports proto_type (Protobuf scalar type name), type (explicit PDL type name, default 'double'), and optional dims arrayref to reshape the resulting ndarray.

to_protobuf($pdl, $class_or_msg, $field, %opts)

my $msg = to_protobuf($pdl, 'My::Message', 'values',
    extra_fields => { id => 'vec-1' },
);

Flattens $pdl and populates $field on an existing message object or a newly constructed instance of $class_or_msg.

pdl_type_for_proto_type($proto_type)

Returns the corresponding PDL type name for a Protocol Buffers scalar type (e.g. float => float, int32 => long, int64 => longlong, bool => byte).

TYPE MAPPING

  • double => double

  • float => float

  • int32, sint32, sfixed32 => long

  • uint32, fixed32 => ulong

  • int64, sint64, sfixed64 => longlong

  • uint64, fixed64 => ulonglong

  • bool => byte

SEE ALSO

PDL, Protobuf, Protobuf::Message, Protobuf::Arena

AUTHOR

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

LICENSE AND COPYRIGHT

Copyright (c) 2026 Google LLC and contributors.

This program is released under the Apache License, Version 2.0.