| 1 | # Licensed to the Apache Software Foundation (ASF) under one | ||
| 2 | # or more contributor license agreements. See the NOTICE file | ||
| 3 | # distributed with this work for additional information | ||
| 4 | # regarding copyright ownership. The ASF licenses this file | ||
| 5 | # to you under the Apache License, Version 2.0 (the | ||
| 6 | # "License"); you may not use this file except in compliance | ||
| 7 | # with the License. You may obtain a copy of the License at | ||
| 8 | # | ||
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | # | ||
| 11 | # Unless required by applicable law or agreed to in writing, | ||
| 12 | # software distributed under the License is distributed on an | ||
| 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| 14 | # KIND, either express or implied. See the License for the | ||
| 15 | # specific language governing permissions and limitations | ||
| 16 | # under the License. | ||
| 17 | |||
| 18 | package AI::MXNet::CachedOp; | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | AI::MXNet::CachedOp - A wrapper around CachedOpHandle | ||
| 23 | =cut | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | Internal module, used as a part of AI::MXNet::Gluon::HybridBlock. | ||
| 28 | =cut | ||
| 29 | |||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | use AI::MXNet::Base; | ||
| 33 | use Mouse; | ||
| 34 | use overload '&{}' => sub { my $self = shift; sub { $self->call(@_) } }; | ||
| 35 | |||
| 36 | has 'handle' => (is => 'ro', isa => 'CachedOpHandle', required => 1); | ||
| 37 | around BUILDARGS => sub { | ||
| 38 | my $orig = shift; | ||
| 39 | my $class = shift; | ||
| 40 | my ($sym, $flags) = @_; | ||
| 41 | for my $key (keys %$flags) | ||
| 42 | { | ||
| 43 | $flags->{ $key } = "(" .join(", ", map { defined($_) ? $_ : 'None' } @{ $flags->{ $key } }) .")" | ||
| 44 | if ref $flags->{ $key } eq 'ARRAY'; | ||
| 45 | } | ||
| 46 | my $handle = check_call( | ||
| 47 | AI::MXNetCAPI::CreateCachedOpEx( | ||
| 48 | $sym->handle, | ||
| 49 | scalar(keys %{ $flags//{} }), | ||
| 50 | $flags//{}, | ||
| 51 | ) | ||
| 52 | ); | ||
| 53 | return $class->$orig(handle => $handle); | ||
| 54 | }; | ||
| 55 | |||
| 56 | sub DEMOLISH | ||
| 57 | { | ||
| 58 | check_call(AI::MXNetCAPI::FreeCachedOp(shift->handle)); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub call | ||
| 62 | { | ||
| 63 | my $self = shift; | ||
| 64 | my @args; | ||
| 65 | my %kwargs; | ||
| 66 | if(blessed $_[0] and $_[0]->isa('AI::MXNet::NDArray')) | ||
| 67 | { | ||
| 68 | while(blessed $_[0] and $_[0]->isa('AI::MXNet::NDArray')) | ||
| 69 | { | ||
| 70 | push @args, shift(@_); | ||
| 71 | } | ||
| 72 | %kwargs = @_; | ||
| 73 | } | ||
| 74 | else | ||
| 75 | { | ||
| 76 | %kwargs = @_; | ||
| 77 | } | ||
| 78 | my $out = delete $kwargs{out}; | ||
| 79 | if(%kwargs) | ||
| 80 | { | ||
| 81 | confess( | ||
| 82 | "AI::MXNet::CachedOp::call got unexpected keyword argument(s): ". | ||
| 83 | join(', ', keys %kwargs) | ||
| 84 | ); | ||
| 85 | } | ||
| 86 | my $original_output; | ||
| 87 | if(defined $out) | ||
| 88 | { | ||
| 89 | $original_output = $out; | ||
| 90 | if(blessed($out)) | ||
| 91 | { | ||
| 92 | $out = [$out]; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | else | ||
| 96 | { | ||
| 97 | $out = []; | ||
| 98 | } | ||
| 99 | my ($output, $stypes) = check_call( | ||
| 100 | AI::MXNetCAPI::InvokeCachedOpEx( | ||
| 101 | $self->handle, | ||
| 102 | scalar(@args), | ||
| 103 | [map { $_->handle } @args], | ||
| 104 | [map { $_->handle } @$out] | ||
| 105 | ) | ||
| 106 | ); | ||
| 107 | return $original_output if defined $original_output; | ||
| 108 | if(@$output == 1) | ||
| 109 | { | ||
| 110 | return AI::MXNet::NDArray->_ndarray_cls($output->[0], 1, $stypes->[0]); | ||
| 111 | } | ||
| 112 | else | ||
| 113 | { | ||
| 114 | my $i = 0; | ||
| 115 | return [map { AI::MXNet::NDArray->_ndarray_cls($_, 1, $stypes->[$i++]) } @$output]; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; |