| 1 | package AI::TensorFlow::Libtensorflow::Lib; | ||
| 2 | # ABSTRACT: Private class for AI::TensorFlow::Libtensorflow | ||
| 3 | $AI::TensorFlow::Libtensorflow::Lib::VERSION = '0.0.7'; | ||
| 4 | 43 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 43 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 43 | ▶ | use feature qw(state); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 43 | ▶ | use FFI::CheckLib 0.28 qw( find_lib_or_die ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Alien::Libtensorflow; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use FFI::Platypus; | ||
| 11 | use AI::TensorFlow::Libtensorflow::Lib::FFIType::Variant::PackableArrayRef; | ||
| 12 | use AI::TensorFlow::Libtensorflow::Lib::FFIType::Variant::PackableMaybeArrayRef; | ||
| 13 | use AI::TensorFlow::Libtensorflow::Lib::FFIType::TFPtrSizeScalar; | ||
| 14 | |||
| 15 | use base 'Exporter::Tiny'; | ||
| 16 | our @EXPORT_OK = qw(arg); | ||
| 17 | |||
| 18 | sub lib { | ||
| 19 | $ENV{AI_TENSORFLOW_LIBTENSORFLOW_LIB_DLL} | ||
| 20 | // find_lib_or_die( | ||
| 21 | lib => 'tensorflow', | ||
| 22 | symbol => ['TF_Version'], | ||
| 23 | alien => ['Alien::Libtensorflow'] ); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub ffi { | ||
| 27 | state $ffi; | ||
| 28 | $ffi ||= do { | ||
| 29 | my $ffi = FFI::Platypus->new( api => 2 ); | ||
| 30 | $ffi->lib( __PACKAGE__->lib ); | ||
| 31 | |||
| 32 | $ffi->load_custom_type('::PointerSizeBuffer' => 'tf_config_proto_buffer'); | ||
| 33 | $ffi->load_custom_type('::PointerSizeBuffer' => 'tf_tensor_shape_proto_buffer'); | ||
| 34 | $ffi->load_custom_type('::PointerSizeBuffer' => 'tf_attr_value_proto_buffer'); | ||
| 35 | |||
| 36 | $ffi->load_custom_type('AI::TensorFlow::Libtensorflow::Lib::FFIType::TFPtrSizeScalar' | ||
| 37 | => 'tf_text_buffer'); | ||
| 38 | |||
| 39 | $ffi->load_custom_type( PackableMaybeArrayRef( 'DimsBuffer', pack_type => 'q' ) | ||
| 40 | => 'tf_dims_buffer' | ||
| 41 | ); | ||
| 42 | |||
| 43 | |||
| 44 | $ffi->type('object(AI::TensorFlow::Libtensorflow::SessionOptions)' => 'TF_SessionOptions'); | ||
| 45 | |||
| 46 | $ffi->type('object(AI::TensorFlow::Libtensorflow::Graph)' => 'TF_Graph'); | ||
| 47 | |||
| 48 | $ffi->type('object(AI::TensorFlow::Libtensorflow::OperationDescription)' | ||
| 49 | => 'TF_OperationDescription'); | ||
| 50 | |||
| 51 | $ffi->load_custom_type('::PtrObject', 'TF_Operation' => 'AI::TensorFlow::Libtensorflow::Operation'); | ||
| 52 | |||
| 53 | $ffi->type('opaque' => 'TF_Function'); | ||
| 54 | |||
| 55 | $ffi->type('opaque' => 'TF_FunctionOptions'); | ||
| 56 | |||
| 57 | $ffi->type('object(AI::TensorFlow::Libtensorflow::ImportGraphDefOptions)' => 'TF_ImportGraphDefOptions'); | ||
| 58 | |||
| 59 | $ffi->type('object(AI::TensorFlow::Libtensorflow::ImportGraphDefResults)' => 'TF_ImportGraphDefResults'); | ||
| 60 | |||
| 61 | $ffi->type('object(AI::TensorFlow::Libtensorflow::Session)' => 'TF_Session'); | ||
| 62 | |||
| 63 | $ffi->type('opaque' => 'TF_DeprecatedSession'); | ||
| 64 | |||
| 65 | $ffi->type('object(AI::TensorFlow::Libtensorflow::DeviceList)' => 'TF_DeviceList'); | ||
| 66 | |||
| 67 | $ffi->type('object(AI::TensorFlow::Libtensorflow::TFLibrary)' => 'TF_Library'); | ||
| 68 | |||
| 69 | $ffi->type('object(AI::TensorFlow::Libtensorflow::ApiDefMap)' => 'TF_ApiDefMap'); | ||
| 70 | |||
| 71 | $ffi->type('opaque' => 'TF_Server'); | ||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | $ffi->type('opaque' => 'TF_CheckpointReader'); | ||
| 76 | |||
| 77 | $ffi->type('opaque' => 'TF_AttrBuilder'); | ||
| 78 | |||
| 79 | $ffi->type('opaque' => 'TF_ShapeAndType'); | ||
| 80 | |||
| 81 | $ffi->type('opaque' => 'TF_ShapeAndTypeList'); | ||
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | $ffi->type('opaque' => 'TF_WritableFileHandle'); | ||
| 86 | |||
| 87 | $ffi->type('opaque' => 'TF_StringStream'); | ||
| 88 | |||
| 89 | $ffi->type('opaque' => 'TF_Thread'); | ||
| 90 | |||
| 91 | |||
| 92 | $ffi->type('opaque' => 'TF_KernelBuilder'); | ||
| 93 | |||
| 94 | $ffi->type('opaque' => 'TF_OpKernelConstruction'); | ||
| 95 | |||
| 96 | $ffi->type('opaque' => 'TF_OpKernelContext'); | ||
| 97 | |||
| 98 | |||
| 99 | $ffi->type('opaque' => 'TF_VariableInputLockHolder'); | ||
| 100 | |||
| 101 | $ffi->type('opaque' => 'TF_CoordinationServiceAgent'); | ||
| 102 | |||
| 103 | |||
| 104 | $ffi->type('opaque' => 'TF_Shape'); | ||
| 105 | |||
| 106 | |||
| 107 | $ffi->type('object(AI::TensorFlow::Libtensorflow::Status)' => 'TF_Status'); | ||
| 108 | |||
| 109 | |||
| 110 | $ffi->load_custom_type('::PtrObject', 'TF_Tensor' => 'AI::TensorFlow::Libtensorflow::Tensor'); | ||
| 111 | |||
| 112 | |||
| 113 | $ffi->load_custom_type('::PtrObject', 'TF_TString' => 'AI::TensorFlow::Libtensorflow::TString'); | ||
| 114 | |||
| 115 | |||
| 116 | $ffi->type('object(AI::TensorFlow::Libtensorflow::Eager::ContextOptions)', 'TFE_ContextOptions'); | ||
| 117 | |||
| 118 | $ffi->type('object(AI::TensorFlow::Libtensorflow::Eager::Context)', 'TFE_Context'); | ||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | ## Callbacks for deallocation | ||
| 123 | # For TF_Buffer | ||
| 124 | $ffi->type('(opaque,size_t)->void' => 'data_deallocator_t'); | ||
| 125 | # For TF_Tensor | ||
| 126 | $ffi->type('(opaque,size_t,opaque)->void' => 'tensor_deallocator_t'); | ||
| 127 | |||
| 128 | $ffi; | ||
| 129 | }; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub mangler_default { | ||
| 133 | my $target = (caller)[0]; | ||
| 134 | my $prefix = 'TF'; | ||
| 135 | if( $target =~ /::Eager::/ ) { | ||
| 136 | $prefix = 'TFE'; | ||
| 137 | } | ||
| 138 | sub { | ||
| 139 | my ($name) = @_; | ||
| 140 | "${prefix}_$name"; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | sub mangler_for_object { | ||
| 145 | my ($class, $object_name) = @_; | ||
| 146 | sub { | ||
| 147 | my ($name) = @_; | ||
| 148 | |||
| 149 | # constructor and destructors | ||
| 150 | return "TF_New${object_name}" if $name eq 'New'; | ||
| 151 | return "TF_Delete${object_name}" if $name eq 'Delete'; | ||
| 152 | |||
| 153 | return "TF_${object_name}$name"; | ||
| 154 | }; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub arg(@) { | ||
| 158 | my $arg = AI::TensorFlow::Libtensorflow::Lib::_Arg->new( | ||
| 159 | type => shift, | ||
| 160 | id => shift, | ||
| 161 | ); | ||
| 162 | return $arg, @_; | ||
| 163 | } | ||
| 164 | |||
| 165 | # from FFI::Platypus::Type::StringArray | ||
| 166 | use constant _pointer_incantation => | ||
| 167 | $^O eq 'MSWin32' && do { require Config; $Config::Config{archname} =~ /MSWin32-x64/ } | ||
| 168 | ? 'Q' | ||
| 169 | : 'L!'; | ||
| 170 | use constant _size_of_pointer => FFI::Platypus->new( api => 2 )->sizeof('opaque'); | ||
| 171 | use constant _pointer_buffer => "P" . _size_of_pointer; | ||
| 172 | |||
| 173 | package # hide from PAUSE | ||
| 174 | AI::TensorFlow::Libtensorflow::Lib::_Arg { | ||
| 175 | |||
| 176 | use Class::Tiny qw(type id); | ||
| 177 | |||
| 178 | use overload | ||
| 179 | q{""} => 'stringify', | ||
| 180 | eq => 'eq'; | ||
| 181 | |||
| 182 | sub stringify { $_[0]->type } | ||
| 183 | |||
| 184 | sub eq { | ||
| 185 | my ($self, $other, $swap) = @_; | ||
| 186 | "$self" eq "$other"; | ||
| 187 | } | ||
| 188 | |||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | 1; | ||
| 194 | |||
| 195 | __END__ | ||