| 1 | package AI::TensorFlow::Libtensorflow::Tensor; | ||
| 2 | # ABSTRACT: A multi-dimensional array of elements of a single data type | ||
| 3 | $AI::TensorFlow::Libtensorflow::Tensor::VERSION = '0.0.7'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use namespace::autoclean; | ||
| 7 | use AI::TensorFlow::Libtensorflow::Lib qw(arg); | ||
| 8 | use FFI::Platypus::Closure; | ||
| 9 | use FFI::Platypus::Buffer qw(window); | ||
| 10 | use List::Util qw(product); | ||
| 11 | |||
| 12 | my $ffi = AI::TensorFlow::Libtensorflow::Lib->ffi; | ||
| 13 | $ffi->mangler(AI::TensorFlow::Libtensorflow::Lib->mangler_default); | ||
| 14 | |||
| 15 | $ffi->load_custom_type('AI::TensorFlow::Libtensorflow::Lib::FFIType::TFPtrSizeScalarRef' | ||
| 16 | => 'tf_tensor_buffer' | ||
| 17 | ); | ||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | $ffi->attach( [ 'NewTensor' => 'New' ] => | ||
| 22 | [ | ||
| 23 | arg 'TF_DataType' => 'dtype', | ||
| 24 | |||
| 25 | # const int64_t* dims, int num_dims | ||
| 26 | arg 'tf_dims_buffer' => [ qw(dims num_dims) ], | ||
| 27 | |||
| 28 | # void* data, size_t len | ||
| 29 | arg 'tf_tensor_buffer' => [ qw(data len) ], | ||
| 30 | |||
| 31 | arg 'opaque' => 'deallocator', # tensor_deallocator_t (deallocator) | ||
| 32 | arg 'opaque' => 'deallocator_arg', | ||
| 33 | ], | ||
| 34 | => 'TF_Tensor' => sub { | ||
| 35 | my ($xs, $class, | ||
| 36 | $dtype, $dims, $data, | ||
| 37 | $deallocator, $deallocator_arg, | ||
| 38 | ) = @_; | ||
| 39 | my $deallocator_closure = $ffi->closure( $deallocator ); | ||
| 40 | $deallocator_closure->sticky; | ||
| 41 | my $deallocator_ptr = $ffi->cast( | ||
| 42 | 'tensor_deallocator_t', 'opaque', | ||
| 43 | $deallocator_closure ); | ||
| 44 | |||
| 45 | my $obj = $xs->( | ||
| 46 | $dtype, | ||
| 47 | $dims, | ||
| 48 | $data, | ||
| 49 | $deallocator_ptr, $deallocator_arg, | ||
| 50 | ); | ||
| 51 | |||
| 52 | # Return early if no TF_Tensor created | ||
| 53 | # TODO should this throw an exception instead? | ||
| 54 | return unless $obj; | ||
| 55 | |||
| 56 | $obj->{_deallocator_closure} = $deallocator_closure; | ||
| 57 | |||
| 58 | $obj; | ||
| 59 | }); | ||
| 60 | |||
| 61 | |||
| 62 | # C: TF_AllocateTensor | ||
| 63 | # | ||
| 64 | # Constructor | ||
| 65 | $ffi->attach( [ 'AllocateTensor', 'Allocate' ], | ||
| 66 | [ | ||
| 67 | arg 'TF_DataType' => 'dtype', | ||
| 68 | arg 'tf_dims_buffer' => [ qw(dims num_dims) ], | ||
| 69 | arg 'size_t' => 'len', | ||
| 70 | ], | ||
| 71 | => 'TF_Tensor' => sub { | ||
| 72 | my ($xs, $class, @rest) = @_; | ||
| 73 | my ($dtype, $dims, $len) = @rest; | ||
| 74 | if( ! defined $len ) { | ||
| 75 | $len = product($dtype->Size, @$dims); | ||
| 76 | } | ||
| 77 | my $obj = $xs->($dtype, $dims, $len); | ||
| 78 | } | ||
| 79 | ); | ||
| 80 | |||
| 81 | $ffi->attach( [ 'DeleteTensor' => 'DESTROY' ], | ||
| 82 | [ arg 'TF_Tensor' => 't' ] | ||
| 83 | => 'void' | ||
| 84 | => sub { | ||
| 85 | my ($xs, $self) = @_; | ||
| 86 | $xs->($self); | ||
| 87 | if( exists $self->{_deallocator_closure} ) { | ||
| 88 | $self->{_deallocator_closure}->unstick; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | ); | ||
| 92 | |||
| 93 | $ffi->attach( [ 'TensorData' => 'Data' ], | ||
| 94 | [ arg 'TF_Tensor' => 'self' ], | ||
| 95 | => 'opaque' | ||
| 96 | => sub { | ||
| 97 | my ($xs, @rest) = @_; | ||
| 98 | my ($self) = @rest; | ||
| 99 | my $data_p = $xs->(@rest); | ||
| 100 | window(my $buffer, $data_p, $self->ByteSize); | ||
| 101 | \$buffer; | ||
| 102 | } | ||
| 103 | ); | ||
| 104 | |||
| 105 | $ffi->attach( [ 'TensorByteSize' => 'ByteSize' ], | ||
| 106 | [ arg 'TF_Tensor' => 'self' ], | ||
| 107 | => 'size_t' | ||
| 108 | ); | ||
| 109 | |||
| 110 | $ffi->attach( [ 'TensorType' => 'Type' ], | ||
| 111 | [ arg 'TF_Tensor' => 'self' ], | ||
| 112 | => 'TF_DataType' | ||
| 113 | ); | ||
| 114 | |||
| 115 | $ffi->attach( [ 'NumDims' => 'NumDims' ], | ||
| 116 | [ arg 'TF_Tensor' => 'self' ], | ||
| 117 | => 'int', | ||
| 118 | ); | ||
| 119 | |||
| 120 | $ffi->attach( [ 'TensorElementCount' => 'ElementCount' ] => | ||
| 121 | [ arg 'TF_Tensor' => 'self' ] | ||
| 122 | => 'int64_t' | ||
| 123 | ); | ||
| 124 | |||
| 125 | $ffi->attach( [ 'Dim' => 'Dim' ], | ||
| 126 | [ | ||
| 127 | arg 'TF_Tensor' => 't', | ||
| 128 | arg 'int' => 'dim_index', | ||
| 129 | ], | ||
| 130 | => 'int64_t', | ||
| 131 | ); | ||
| 132 | |||
| 133 | $ffi->attach( [ 'TensorMaybeMove' => 'MaybeMove' ] => | ||
| 134 | [ arg 'TF_Tensor' => 'self' ], | ||
| 135 | => 'TF_Tensor', | ||
| 136 | ); | ||
| 137 | |||
| 138 | $ffi->attach( ['TensorIsAligned' => 'IsAligned'] => [ | ||
| 139 | arg TF_Tensor => 't' | ||
| 140 | ] => 'bool' ); | ||
| 141 | |||
| 142 | eval {# TF v2.10.0 | ||
| 143 | $ffi->attach( [ 'SetShape' => 'SetShape' ] => | ||
| 144 | [ | ||
| 145 | arg 'TF_Tensor' => 'self', | ||
| 146 | arg 'tf_dims_buffer' => [ qw(dims num_dims) ], | ||
| 147 | ] | ||
| 148 | => 'void' | ||
| 149 | ); | ||
| 150 | }; | ||
| 151 | |||
| 152 | $ffi->attach( [ 'TensorBitcastFrom' => 'BitcastFrom' ] => [ | ||
| 153 | arg TF_Tensor => 'from', | ||
| 154 | arg TF_DataType => 'type', | ||
| 155 | arg TF_Tensor => 'to', | ||
| 156 | arg 'tf_dims_buffer' => [ qw(new_dims num_new_dims) ], | ||
| 157 | arg TF_Status => 'status', | ||
| 158 | ] => 'void' ); | ||
| 159 | |||
| 160 | #### Array helpers #### | ||
| 161 | use FFI::C::ArrayDef; | ||
| 162 | use FFI::C::StructDef; | ||
| 163 | my $adef = FFI::C::ArrayDef->new( | ||
| 164 | $ffi, | ||
| 165 | name => 'TF_Tensor_array', | ||
| 166 | members => [ | ||
| 167 | FFI::C::StructDef->new( | ||
| 168 | $ffi, | ||
| 169 | members => [ | ||
| 170 | p => 'opaque' | ||
| 171 | ] | ||
| 172 | ) | ||
| 173 | ], | ||
| 174 | ); | ||
| 175 | sub _adef { | ||
| 176 | $adef; | ||
| 177 | } | ||
| 178 | sub _as_array { | ||
| 179 | my $class = shift; | ||
| 180 | my $array = $class->_adef->create(0 + @_); | ||
| 181 | for my $idx (0..@_-1) { | ||
| 182 | next unless defined $_[$idx]; | ||
| 183 | $array->[$idx]->p($ffi->cast('TF_Tensor', 'opaque', $_[$idx])); | ||
| 184 | } | ||
| 185 | $array; | ||
| 186 | } | ||
| 187 | sub _from_array { | ||
| 188 | my ($class, $array) = @_; | ||
| 189 | return [ | ||
| 190 | map { | ||
| 191 | $ffi->cast( | ||
| 192 | 'opaque', | ||
| 193 | 'TF_Tensor', | ||
| 194 | $array->[$_]->p) | ||
| 195 | } 0.. $array->count - 1 | ||
| 196 | ] | ||
| 197 | } | ||
| 198 | |||
| 199 | #### Data::Printer #### | ||
| 200 | sub _data_printer { | ||
| 201 | my ($self, $ddp) = @_; | ||
| 202 | |||
| 203 | my @data = ( | ||
| 204 | [ Type => $ddp->maybe_colorize( $self->Type, 'class' ), ], | ||
| 205 | [ Dims => sprintf "%s %s %s", | ||
| 206 | $ddp->maybe_colorize('[', 'brackets'), | ||
| 207 | join(" ", | ||
| 208 | map $ddp->maybe_colorize( $self->Dim($_), 'number' ), | ||
| 209 | 0..$self->NumDims-1), | ||
| 210 | $ddp->maybe_colorize(']', 'brackets'), | ||
| 211 | ], | ||
| 212 | [ NumDims => $ddp->maybe_colorize( $self->NumDims, 'number' ), ], | ||
| 213 | [ ElementCount => $ddp->maybe_colorize( $self->ElementCount, 'number' ), ], | ||
| 214 | ); | ||
| 215 | |||
| 216 | my $output; | ||
| 217 | |||
| 218 | $output .= $ddp->maybe_colorize(ref $self, 'class' ); | ||
| 219 | $output .= ' ' . $ddp->maybe_colorize('{', 'brackets'); | ||
| 220 | $ddp->indent; | ||
| 221 | for my $item (@data) { | ||
| 222 | $output .= $ddp->newline; | ||
| 223 | $output .= join " ", | ||
| 224 | $ddp->maybe_colorize(sprintf("%-15s", $item->[0]), 'hash'), | ||
| 225 | $item->[1]; | ||
| 226 | } | ||
| 227 | $ddp->outdent; | ||
| 228 | $output .= $ddp->newline; | ||
| 229 | $output .= $ddp->maybe_colorize('}', 'brackets'); | ||
| 230 | |||
| 231 | return $output; | ||
| 232 | } | ||
| 233 | |||
| 234 | 1; | ||
| 235 | |||
| 236 | __END__ |