| 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::Monitor; | ||
| 19 | use Mouse; | ||
| 20 | use AI::MXNet::NS; | ||
| 21 | use AI::MXNet::Function::Parameters; | ||
| 22 | use AI::MXNet::Base; | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | AI::MXNet::Monitor - Monitor outputs, weights, and gradients for debugging. | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | Monitor outputs, weights, and gradients for debugging. | ||
| 31 | |||
| 32 | Parameters | ||
| 33 | ---------- | ||
| 34 | interval : Int | ||
| 35 | Number of batches between printing. | ||
| 36 | stat_func : CodeRef | ||
| 37 | a function that computes statistics of tensors. | ||
| 38 | Takes a NDArray and returns a NDArray. defaults to mean | ||
| 39 | absolute value |x|/size(x). | ||
| 40 | pattern : Str | ||
| 41 | A regular expression specifying which tensors to monitor. | ||
| 42 | Only tensors with names that match name_pattern will be included. | ||
| 43 | For example, '.*weight|.*output' will print all weights and outputs; | ||
| 44 | '.*backward.*' will print all gradients. | ||
| 45 | =cut | ||
| 46 | |||
| 47 | has 'interval' => (is => 'ro', isa => 'Int', required => 1); | ||
| 48 | has 'stat_func' => ( | ||
| 49 | is => 'ro', | ||
| 50 | isa => 'CodeRef', | ||
| 51 | default => sub { | ||
| 52 | return sub { | ||
| 53 | # returns |x|/size(x), async execution. | ||
| 54 | my ($x) = @_; | ||
| 55 | return $x->norm/sqrt($x->size); | ||
| 56 | } | ||
| 57 | }, | ||
| 58 | lazy => 1 | ||
| 59 | ); | ||
| 60 | has 'pattern' => (is => 'ro', isa => 'Str', default => '.*'); | ||
| 61 | has '_sort' => (is => 'ro', isa => 'Bool', init_arg => 'sort', default => 0); | ||
| 62 | has [qw/queue exes/] => (is => 'rw', init_arg => undef, default => sub { [] }); | ||
| 63 | has [qw/step activated/] => (is => 'rw', init_arg => undef, default => 0); | ||
| 64 | has 're_pattern' => ( | ||
| 65 | is => 'ro', | ||
| 66 | init_arg => undef, | ||
| 67 | default => sub { | ||
| 68 | my $pattern = shift->pattern; | ||
| 69 | my $re = eval { qr/$pattern/ }; | ||
| 70 | confess("pattern $pattern failed to compile as a regexp $@") | ||
| 71 | if $@; | ||
| 72 | return $re; | ||
| 73 | }, | ||
| 74 | lazy => 1 | ||
| 75 | ); | ||
| 76 | has 'stat_helper' => ( | ||
| 77 | is => 'ro', | ||
| 78 | init_arg => undef, | ||
| 79 | default => sub { | ||
| 80 | my $self = shift; | ||
| 81 | return sub { | ||
| 82 | my ($name, $handle) = @_; | ||
| 83 | return if(not $self->activated or not $name =~ $self->re_pattern); | ||
| 84 | my $array = AI::MXNet::NDArray->_ndarray_cls($handle, 0); | ||
| 85 | push @{ $self->queue }, [$self->step, $name, $self->stat_func->($array)]; | ||
| 86 | } | ||
| 87 | }, | ||
| 88 | lazy => 1 | ||
| 89 | ); | ||
| 90 | |||
| 91 | =head2 install | ||
| 92 | |||
| 93 | install callback to executor. | ||
| 94 | Supports installing to multiple exes. | ||
| 95 | |||
| 96 | Parameters | ||
| 97 | ---------- | ||
| 98 | $exe : AI::MXNet::Executor | ||
| 99 | the Executor (returned by $symbol->bind) to install to. | ||
| 100 | =cut | ||
| 101 | |||
| 102 | method install(AI::MXNet::Executor $exe) | ||
| 103 | { | ||
| 104 | $exe->set_monitor_callback($self->stat_helper); | ||
| 105 | push @{ $self->exes }, $exe; | ||
| 106 | } | ||
| 107 | |||
| 108 | =head2 tic | ||
| 109 | |||
| 110 | start collecting stats for current batch. | ||
| 111 | Call before forward | ||
| 112 | =cut | ||
| 113 | |||
| 114 | method tic() | ||
| 115 | { | ||
| 116 | if ($self->step % $self->interval == 0) | ||
| 117 | { | ||
| 118 | for my $exe (@{ $self->exes }) | ||
| 119 | { | ||
| 120 | $_->wait_to_read for @{ $exe->arg_arrays }; | ||
| 121 | $_->wait_to_read for @{ $exe->aux_arrays }; | ||
| 122 | } | ||
| 123 | $self->queue([]); | ||
| 124 | $self->activated(1); | ||
| 125 | } | ||
| 126 | $self->step($self->step + 1); | ||
| 127 | } | ||
| 128 | |||
| 129 | =head2 toc | ||
| 130 | |||
| 131 | End collecting for current batch and return results. | ||
| 132 | Call after computation of current batch. | ||
| 133 | |||
| 134 | Returns | ||
| 135 | ------- | ||
| 136 | res : array ref of array refs with debug info | ||
| 137 | =cut | ||
| 138 | |||
| 139 | method toc() | ||
| 140 | { | ||
| 141 | return [] unless $self->activated; | ||
| 142 | for my $exe (@{ $self->exes }) | ||
| 143 | { | ||
| 144 | $_->wait_to_read for @{ $exe->arg_arrays }; | ||
| 145 | $_->wait_to_read for @{ $exe->aux_arrays }; | ||
| 146 | } | ||
| 147 | for my $exe (@{ $self->exes }) | ||
| 148 | { | ||
| 149 | for(zip($exe->_symbol->list_arguments, $exe->arg_arrays)) { | ||
| 150 | my ($name, $array) = @$_; | ||
| 151 | push @{ $self->queue }, [$self->step, $name, $self->stat_func->($array)]; | ||
| 152 | } | ||
| 153 | for(zip($exe->_symbol->list_auxiliary_states, $exe->aux_arrays)) { | ||
| 154 | my ($name, $array) = @$_; | ||
| 155 | push @{ $self->queue }, [$self->step, $name, $self->stat_func->($array)]; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | $self->activated(0); | ||
| 159 | my @res; | ||
| 160 | if($self->_sort) | ||
| 161 | { | ||
| 162 | @{ $self->queue } = sort { $a->[1] cmp $b->[1] } @{ $self->queue }; | ||
| 163 | } | ||
| 164 | for my $q (@{ $self->queue }) | ||
| 165 | { | ||
| 166 | my ($n, $k, $v_list) = @{ $q }; | ||
| 167 | if(ref $v_list ne 'ARRAY') | ||
| 168 | { | ||
| 169 | $v_list = [$v_list]; | ||
| 170 | } | ||
| 171 | my $s = ''; | ||
| 172 | for my $v (@{ $v_list }) | ||
| 173 | { | ||
| 174 | confess("the argument must be NDArray") | ||
| 175 | unless blessed($v) and $v->isa('AI::MXNet::NDArray'); | ||
| 176 | if($v->size == 1) | ||
| 177 | { | ||
| 178 | $s .= $v->asscalar . "\t"; | ||
| 179 | } | ||
| 180 | else | ||
| 181 | { | ||
| 182 | $s .= $v->aspdl . "\t"; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | push @res, [$n, $k, $s]; | ||
| 186 | } | ||
| 187 | $self->queue([]); | ||
| 188 | return \@res; | ||
| 189 | } | ||
| 190 | |||
| 191 | =head2 toc_print | ||
| 192 | |||
| 193 | End collecting and print results | ||
| 194 | =cut | ||
| 195 | |||
| 196 | method toc_print() | ||
| 197 | { | ||
| 198 | my $res = $self->toc; | ||
| 199 | for my $r (@{ $res }) | ||
| 200 | { | ||
| 201 | AI::MXNet::Logging->info('Batch: %7d %30s %s', @{ $r }); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | method Monitor(@args) | ||
| 206 | { | ||
| 207 | __PACKAGE__->new(@args % 2 ? ('interval', @args) : @args); | ||
| 208 | } | ||
| 209 | |||
| 210 | 1; |