AI-TensorFlow-Libtensorflow
view release on metacpan or search on metacpan
t/lib/TF_Utils.pm view on Meta::CPAN
package TF_Utils;
use AI::TensorFlow::Libtensorflow;
use AI::TensorFlow::Libtensorflow::Lib;
use AI::TensorFlow::Libtensorflow::DataType qw(FLOAT INT32 INT8);
use Path::Tiny;
use List::Util qw(first);
use PDL::Core ':Internal';
use FFI::Platypus::Buffer;
use Test2::V0;
my $ffi = AI::TensorFlow::Libtensorflow::Lib->ffi;
sub ScalarStringTensor {
my ($str, $status) = @_;
#my $tensor = AI::TensorFlow::Libtensorflow::Tensor->_Allocate(
#AI::TensorFlow::Libtensorflow::DType::STRING,
#\@dims, $ndims,
#$data_size_bytes,
#);
...
}
sub ReadBufferFromFile {
my ($path) = @_;
my $data = path($path)->slurp_raw;
my $buffer = AI::TensorFlow::Libtensorflow::Buffer->NewFromString(
\$data
);
}
sub LoadGraph {
my ($path, $checkpoint_prefix, $status) = @_;
my $buffer = ReadBufferFromFile( $path );
$status //= AI::TensorFlow::Libtensorflow::Status->New;
my $graph = AI::TensorFlow::Libtensorflow::Graph->New;
my $opts = AI::TensorFlow::Libtensorflow::ImportGraphDefOptions->New;
$graph->ImportGraphDef( $buffer, $opts, $status );
if( $status->GetCode != AI::TensorFlow::Libtensorflow::Status::OK ) {
return;
}
if( ! defined $checkpoint_prefix ) {
return $graph;
}
}
sub FloatPDLToTFTensor {
my ($p) = @_;
my $pdl_closure = sub {
my ($pointer, $size, $pdl_addr) = @_;
# noop
};
my $p_dataref = $p->get_dataref;
my $tensor = AI::TensorFlow::Libtensorflow::Tensor->New(
FLOAT, [ $p->dims ], $p_dataref, $pdl_closure
);
$tensor;
}
sub Placeholder {
my ($graph, $status, $name, $dtype) = @_;
$name ||= 'feed';
$dtype ||= INT32;
t/lib/TF_Utils.pm view on Meta::CPAN
$self->_outputs( \@outputs );
$self->_output_values( \@output_values );
}
sub SetTargets {
my ($self, @data) = @_;
$self->_targets( \@data );
}
sub Run {
my ($self, $s) = @_;
if( @{ $self->_inputs } != @{ $self->_input_values } ) {
die "Call SetInputs() before Run()";
}
$self->session->Run(
undef,
$self->_inputs, $self->_input_values,
$self->_outputs, $self->_output_values,
$self->_targets,
undef,
$s
);
}
sub output_tensor { my ($self, $i) = @_; $self->_output_values->[$i] }
}
sub BinaryOpHelper {
my ($op_name, $l, $r,
$graph, $s, $name,
$device, $check) = @_;
$check ||= 1;
my $desc = AI::TensorFlow::Libtensorflow::OperationDescription
->New( $graph, $op_name, $name );
$desc->SetDevice($device) if $device;
$desc->AddInput( $TFOutput->coerce([ $l => 0 ]) );
$desc->AddInput( $TFOutput->coerce([ $r => 0 ]) );
my $op = $desc->FinishOperation($s);
if( $check ) {
TF_Utils::AssertStatusOK($s);
}
return $op;
}
sub MinWithDevice {
my ($l, $r, $graph, $device, $s, $name) = @_;
$name ||= 'min';
return TF_Utils::BinaryOpHelper(
'Min', $l, $r, $graph, $s, $name, $device, 1
)
}
sub RunMinTest {
my (%args) = @_;
my $device = delete $args{device} || "";
my $use_XLA = delete $args{use_XLA} || 0;
my $ctx = Test2::API::context();
my $s = AI::TensorFlow::Libtensorflow::Status->New;
my $graph = AI::TensorFlow::Libtensorflow::Graph->New;
$ctx->note('Make a placeholder operation.');
my $feed = TF_Utils::Placeholder($graph, $s);
TF_Utils::AssertStatusOK($s);
$ctx->note('Make a constant operation with the scalar "0", for axis.');
my $one = TF_Utils::ScalarConst($graph, $s, 'scalar', INT32, 0);
TF_Utils::AssertStatusOK($s);
$ctx->note('Create a session for this graph.');
my $csession = TF_Utils::CSession->new( graph => $graph, status => $s, use_XLA => $use_XLA );
TF_Utils::AssertStatusOK($s);
if( $device ) {
$ctx->note("Setting op Min on device $device");
}
my $min = TF_Utils::MinWithDevice( $feed, $one, $graph, $device, $s );
TF_Utils::AssertStatusOK($s);
$ctx->note('Run the graph.');
$csession->SetInputs( [ $feed, TF_Utils::Int32Tensor([3, 2, 5]) ]);
$csession->SetOutputs($min);
$csession->Run($s);
TF_Utils::AssertStatusOK($s);
is($csession->output_tensor(0), object {
call Type => INT32;
call NumDims => 0; # scalar
call ByteSize => INT32->Size;
call sub {
[ unpack "l*", ${ shift->Data } ];
} => [ 2 ];
}, 'Min( Feed() = [3, 2, 5] )');
$ctx->release;
}
sub GPUDeviceName {
my ($session) = @_;
my $s = AI::TensorFlow::Libtensorflow::Status->New;
my $graph;
if( ! $session ) {
my $opts = AI::TensorFlow::Libtensorflow::SessionOptions->New;
$graph = AI::TensorFlow::Libtensorflow::Graph->New;
$session ||= AI::TensorFlow::Libtensorflow::Session->New($graph, $opts, $s);
}
my $device_list = $session->ListDevices($s);
my $device_idx = first { my $type = $device_list->Type( $_, $s ) eq 'GPU' } 0..$device_list->Count - 1;
return "" unless $device_idx;
return $device_list->Name( $device_idx, $s );
}
sub DumpDevices {
my ($session) = @_;
( run in 0.591 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )