| 1 | package AI::TensorFlow::Libtensorflow::Operation; | ||
| 2 | # ABSTRACT: An operation | ||
| 3 | $AI::TensorFlow::Libtensorflow::Operation::VERSION = '0.0.7'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use namespace::autoclean; | ||
| 7 | use AI::TensorFlow::Libtensorflow::Lib qw(arg); | ||
| 8 | use AI::TensorFlow::Libtensorflow::Output; | ||
| 9 | use AI::TensorFlow::Libtensorflow::Input; | ||
| 10 | |||
| 11 | my $ffi = AI::TensorFlow::Libtensorflow::Lib->ffi; | ||
| 12 | $ffi->mangler(AI::TensorFlow::Libtensorflow::Lib->mangler_default); | ||
| 13 | |||
| 14 | use FFI::C::ArrayDef; | ||
| 15 | my $adef = FFI::C::ArrayDef->new( | ||
| 16 | $ffi, | ||
| 17 | name => 'TF_Operation_array', | ||
| 18 | members => [ | ||
| 19 | FFI::C::StructDef->new( | ||
| 20 | $ffi, | ||
| 21 | members => [ | ||
| 22 | p => 'opaque' | ||
| 23 | ] | ||
| 24 | ) | ||
| 25 | ], | ||
| 26 | ); | ||
| 27 | sub _adef { $adef; } | ||
| 28 | sub _as_array { | ||
| 29 | my $class = shift; | ||
| 30 | my $array = $class->_adef->create(0 + @_); | ||
| 31 | for my $idx (0..@_-1) { | ||
| 32 | next unless defined $_[$idx]; | ||
| 33 | $array->[$idx]->p($ffi->cast('TF_Operation', 'opaque', $_[$idx])); | ||
| 34 | } | ||
| 35 | $array; | ||
| 36 | } | ||
| 37 | sub _from_array { | ||
| 38 | my ($class, $array) = @_; | ||
| 39 | [ | ||
| 40 | map { | ||
| 41 | $ffi->cast('opaque', 'TF_Operation', $array->[$_]->p); | ||
| 42 | } 0..$array->count-1 | ||
| 43 | ] | ||
| 44 | } | ||
| 45 | |||
| 46 | $ffi->attach( [ 'OperationName' => 'Name' ], [ | ||
| 47 | arg 'TF_Operation' => 'oper', | ||
| 48 | ] => 'string'); | ||
| 49 | |||
| 50 | $ffi->attach( [ 'OperationOpType' => 'OpType' ], [ | ||
| 51 | arg 'TF_Operation' => 'oper', | ||
| 52 | ] => 'string'); | ||
| 53 | |||
| 54 | $ffi->attach( [ 'OperationDevice' => 'Device' ], [ | ||
| 55 | arg 'TF_Operation' => 'oper', | ||
| 56 | ] => 'string'); | ||
| 57 | |||
| 58 | $ffi->attach( [ 'OperationNumOutputs' => 'NumOutputs' ], [ | ||
| 59 | arg 'TF_Operation' => 'oper', | ||
| 60 | ] => 'int'); | ||
| 61 | |||
| 62 | $ffi->attach( [ 'OperationOutputType' => 'OutputType' ] => [ | ||
| 63 | arg 'TF_Output' => 'oper_out', | ||
| 64 | ] => 'TF_DataType' => sub { | ||
| 65 | my ($xs, $self, $output) = @_; | ||
| 66 | # TODO coerce from LibtfPartialOutput here | ||
| 67 | $xs->($output); | ||
| 68 | } ); | ||
| 69 | |||
| 70 | $ffi->attach( [ 'OperationNumInputs' => 'NumInputs' ] => [ | ||
| 71 | arg 'TF_Operation' => 'oper', | ||
| 72 | ] => 'int' ); | ||
| 73 | |||
| 74 | $ffi->attach( [ 'OperationInputType' => 'InputType' ] => [ | ||
| 75 | arg 'TF_Input' => 'oper_in', | ||
| 76 | ] => 'TF_DataType' => sub { | ||
| 77 | my ($xs, $self, $input) = @_; | ||
| 78 | # TODO coerce from LibtfPartialInput here | ||
| 79 | $xs->($input); | ||
| 80 | }); | ||
| 81 | |||
| 82 | $ffi->attach( [ 'OperationNumControlInputs' => 'NumControlInputs' ] => [ | ||
| 83 | arg 'TF_Operation' => 'oper', | ||
| 84 | ] => 'int' ); | ||
| 85 | |||
| 86 | $ffi->attach( [ 'OperationNumControlOutputs' => 'NumControlOutputs' ] => [ | ||
| 87 | arg 'TF_Operation' => 'oper', | ||
| 88 | ] => 'int' ); | ||
| 89 | |||
| 90 | $ffi->attach( [ OperationOutputListLength => 'OutputListLength' ] => [ | ||
| 91 | arg 'TF_Operation' => 'oper', | ||
| 92 | arg 'string' => 'arg_name', | ||
| 93 | arg 'TF_Status' => 'status', | ||
| 94 | ] => 'int'); | ||
| 95 | |||
| 96 | $ffi->attach( [ 'OperationInputListLength' => 'InputListLength' ] => [ | ||
| 97 | arg 'TF_Operation' => 'oper', | ||
| 98 | arg 'string' => 'arg_name', | ||
| 99 | arg 'TF_Status' => 'status', | ||
| 100 | ] => 'int' ); | ||
| 101 | |||
| 102 | $ffi->attach( [ 'OperationInput' => 'Input' ] => [ | ||
| 103 | arg 'TF_Input' => 'oper_in', | ||
| 104 | ] => 'TF_Output' => sub { | ||
| 105 | my ($xs, $self, $input) = @_; | ||
| 106 | # TODO coerce from LibtfPartialInput here | ||
| 107 | $xs->($input); | ||
| 108 | }); | ||
| 109 | |||
| 110 | $ffi->attach( [ 'OperationAllInputs' => 'AllInputs' ] => [ | ||
| 111 | arg 'TF_Operation' => 'oper', | ||
| 112 | # TODO make OutputArray | ||
| 113 | arg 'TF_Output_struct_array' => 'inputs', | ||
| 114 | arg 'int' => 'max_inputs', | ||
| 115 | ] => 'void' => sub { | ||
| 116 | my ($xs, $oper) = @_; | ||
| 117 | my $max_inputs = $oper->NumInputs; | ||
| 118 | return [] if $max_inputs == 0; | ||
| 119 | my $inputs = AI::TensorFlow::Libtensorflow::Output->_adef->create(0 + $max_inputs); | ||
| 120 | $xs->($oper, $inputs, $max_inputs); | ||
| 121 | return AI::TensorFlow::Libtensorflow::Output->_from_array($inputs); | ||
| 122 | }); | ||
| 123 | |||
| 124 | $ffi->attach( [ 'OperationGetControlInputs' => 'GetControlInputs' ] => [ | ||
| 125 | arg 'TF_Operation' => 'oper', | ||
| 126 | arg 'TF_Operation_array' => 'control_inputs', | ||
| 127 | arg 'int' => 'max_control_inputs', | ||
| 128 | ] => 'void' => sub { | ||
| 129 | my ($xs, $oper) = @_; | ||
| 130 | my $max_inputs = $oper->NumControlInputs; | ||
| 131 | return [] if $max_inputs == 0; | ||
| 132 | my $inputs = AI::TensorFlow::Libtensorflow::Operation->_adef->create(0 + $max_inputs); | ||
| 133 | $xs->($oper, $inputs, $max_inputs); | ||
| 134 | return AI::TensorFlow::Libtensorflow::Operation->_from_array($inputs); | ||
| 135 | }); | ||
| 136 | |||
| 137 | $ffi->attach( [ 'OperationGetControlOutputs' => 'GetControlOutputs' ] => [ | ||
| 138 | arg 'TF_Operation' => 'oper', | ||
| 139 | arg 'TF_Operation_array' => 'control_outputs', | ||
| 140 | arg 'int' => 'max_control_outputs', | ||
| 141 | ] => 'void' => sub { | ||
| 142 | my ($xs, $oper) = @_; | ||
| 143 | my $max_outputs = $oper->NumControlOutputs; | ||
| 144 | return [] if $max_outputs == 0; | ||
| 145 | my $outputs = AI::TensorFlow::Libtensorflow::Operation->_adef->create(0 + $max_outputs); | ||
| 146 | $xs->($oper, $outputs, $max_outputs); | ||
| 147 | return AI::TensorFlow::Libtensorflow::Operation->_from_array($outputs); | ||
| 148 | }); | ||
| 149 | |||
| 150 | $ffi->attach( [ 'OperationOutputNumConsumers' => 'OutputNumConsumers' ] => [ | ||
| 151 | arg 'TF_Output' => 'oper_out', | ||
| 152 | ], 'int' => sub { | ||
| 153 | my ($xs, $self, $output) = @_; | ||
| 154 | # TODO coerce from LibtfPartialOutput here | ||
| 155 | $xs->($output); | ||
| 156 | }); | ||
| 157 | |||
| 158 | $ffi->attach( [ 'OperationOutputConsumers' => 'OutputConsumers' ] => [ | ||
| 159 | # TODO simplify API | ||
| 160 | arg 'TF_Output' => 'oper_out', | ||
| 161 | arg 'TF_Input_struct_array' => 'consumers', | ||
| 162 | arg 'int' => 'max_consumers', | ||
| 163 | ] => 'int' => sub { | ||
| 164 | my ($xs, $self, $output) = @_; | ||
| 165 | my $max_consumers = $self->OutputNumConsumers( $output ); | ||
| 166 | my $consumers = AI::TensorFlow::Libtensorflow::Input->_adef->create( $max_consumers ); | ||
| 167 | my $count = $xs->($output, $consumers, $max_consumers); | ||
| 168 | return AI::TensorFlow::Libtensorflow::Input->_from_array( $consumers ); | ||
| 169 | }); | ||
| 170 | |||
| 171 | sub _data_printer { | ||
| 172 | my ($self, $ddp) = @_; | ||
| 173 | |||
| 174 | my %data = ( | ||
| 175 | Name => $self->Name, | ||
| 176 | OpType => $self->OpType, | ||
| 177 | NumInputs => $self->NumInputs, | ||
| 178 | NumOutputs => $self->NumOutputs, | ||
| 179 | ); | ||
| 180 | |||
| 181 | return sprintf('%s %s', | ||
| 182 | $ddp->maybe_colorize(ref $self, 'class' ), | ||
| 183 | $ddp->parse(\%data) ); | ||
| 184 | } | ||
| 185 | |||
| 186 | 1; | ||
| 187 | |||
| 188 | __END__ |