| 1 | package AI::TensorFlow::Libtensorflow::Input; | ||
| 2 | # ABSTRACT: Input of operation as (operation, index) pair | ||
| 3 | $AI::TensorFlow::Libtensorflow::Input::VERSION = '0.0.7'; | ||
| 4 | # See L<AI::TensorFlow::Libtensorflow::Output> for similar. | ||
| 5 | # In fact, they are mostly the same, but keeping the classes separate for now | ||
| 6 | # in case the upstream API changes. | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use namespace::autoclean; | ||
| 11 | use FFI::Platypus::Record; | ||
| 12 | use AI::TensorFlow::Libtensorflow::Lib::FFIType::Variant::RecordArrayRef; | ||
| 13 | |||
| 14 | use AI::TensorFlow::Libtensorflow::Lib; | ||
| 15 | my $ffi = AI::TensorFlow::Libtensorflow::Lib->ffi; | ||
| 16 | $ffi->mangler(AI::TensorFlow::Libtensorflow::Lib->mangler_default); | ||
| 17 | |||
| 18 | record_layout_1($ffi, | ||
| 19 | 'opaque' => '_oper', # 8 (on 64-bit) | ||
| 20 | 'int' => '_index', # 4 | ||
| 21 | |||
| 22 | $ffi->sizeof('opaque') == 8 ? ( | ||
| 23 | 'char[4]' => ':', | ||
| 24 | ) : (), | ||
| 25 | ); | ||
| 26 | $ffi->type('record(AI::TensorFlow::Libtensorflow::Input)', 'TF_Input'); | ||
| 27 | |||
| 28 | sub New { | ||
| 29 | my ($class, $args) = @_; | ||
| 30 | |||
| 31 | my $record = $class->new({ | ||
| 32 | _oper => $ffi->cast( 'TF_Operation', 'opaque', delete $args->{oper} ), | ||
| 33 | _index => delete $args->{index}, | ||
| 34 | }); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub oper { $ffi->cast('opaque', 'TF_Operation', $_[0]->_oper ) } | ||
| 38 | sub index { $_[0]->_index } | ||
| 39 | |||
| 40 | use FFI::C::ArrayDef; | ||
| 41 | use FFI::C::StructDef; | ||
| 42 | my $sdef = FFI::C::StructDef->new($ffi, | ||
| 43 | name => 'TF_Input_struct', | ||
| 44 | members => [ | ||
| 45 | _oper => 'opaque', | ||
| 46 | _index => 'int', | ||
| 47 | __ignore => 'char[4]', | ||
| 48 | ], | ||
| 49 | ); | ||
| 50 | my $adef = FFI::C::ArrayDef->new($ffi, | ||
| 51 | name => 'TF_Input_struct_array', | ||
| 52 | members => [ 'TF_Input_struct' ] | ||
| 53 | ); | ||
| 54 | sub _adef { $adef; } | ||
| 55 | sub _as_array { | ||
| 56 | my $class = shift; | ||
| 57 | my $output = $class->_adef->create(0 + @_); | ||
| 58 | for my $idx (0..@_-1) { | ||
| 59 | next unless defined $_[$idx]; | ||
| 60 | $class->_copy_to_other( $_[$idx], $output->[$idx] ); | ||
| 61 | } | ||
| 62 | $output; | ||
| 63 | } | ||
| 64 | sub _from_array { | ||
| 65 | my ($class, $array) = @_; | ||
| 66 | [ | ||
| 67 | map { | ||
| 68 | my $record = $class->new; | ||
| 69 | $class->_copy_to_other($array->[$_], $record); | ||
| 70 | $record; | ||
| 71 | } 0..$array->count-1 | ||
| 72 | ] | ||
| 73 | } | ||
| 74 | sub _copy_to_other { | ||
| 75 | my ($class, $this, $that) = @_; | ||
| 76 | $that->_oper ($this->_oper); | ||
| 77 | $that->_index($this->_index); | ||
| 78 | } | ||
| 79 | |||
| 80 | $ffi->load_custom_type( | ||
| 81 | RecordArrayRef( 'InputArrayPtr', | ||
| 82 | record_module => __PACKAGE__, with_size => 0, | ||
| 83 | ), | ||
| 84 | => 'TF_Input_array'); | ||
| 85 | $ffi->load_custom_type( | ||
| 86 | RecordArrayRef( 'InputArrayPtrSz', | ||
| 87 | record_module => __PACKAGE__, with_size => 1, | ||
| 88 | ), | ||
| 89 | => 'TF_Input_array_sz'); | ||
| 90 | |||
| 91 | 1; | ||
| 92 | |||
| 93 | __END__ |