| 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::Random; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Scalar::Util qw/blessed/; | ||
| 22 | use AI::MXNet::NS; | ||
| 23 | use AI::MXNet::Base; | ||
| 24 | use AI::MXNet::NDArray::Base; | ||
| 25 | use AI::MXNet::Function::Parameters; | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | AI::MXNet::Random - Handling of randomization in MXNet. | ||
| 30 | =cut | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | Handling of randomization in MXNet. | ||
| 35 | =cut | ||
| 36 | |||
| 37 | =head2 seed | ||
| 38 | |||
| 39 | Seed the random number generators in mxnet. | ||
| 40 | |||
| 41 | This seed will affect behavior of functions in this module, | ||
| 42 | as well as results from executors that contains Random number | ||
| 43 | such as Dropout operators. | ||
| 44 | |||
| 45 | Parameters | ||
| 46 | ---------- | ||
| 47 | $seed_state : int | ||
| 48 | The random number seed. | ||
| 49 | |||
| 50 | :$ctx : [Str|AI::MXNet::Context] | ||
| 51 | The device context of the generator. The default Str is "all" which means seeding random | ||
| 52 | number generators of all devices. | ||
| 53 | Notes | ||
| 54 | ----- | ||
| 55 | Random number generators in MXNet are device specific. | ||
| 56 | mx->random->seed($seed_state) sets the state of each generator using $seed_state and the | ||
| 57 | device id. Therefore, random numbers generated from different devices can be different | ||
| 58 | even if they are seeded using the same seed. | ||
| 59 | |||
| 60 | To produce identical random number sequences independent of the device id, | ||
| 61 | set optional ctx argument. | ||
| 62 | For example mx->random->seed($seed_state, ctx => mx->gpu(0)) | ||
| 63 | This produces the same sequence of random numbers independent | ||
| 64 | of the device id, but the sequence can be different on different kind of devices as MXNet's | ||
| 65 | random number generators for CPU and GPU use different algorithms. | ||
| 66 | =cut | ||
| 67 | |||
| 68 | method seed(Int $seed_state, Str|AI::MXNet::Context :$ctx='all') | ||
| 69 | { | ||
| 70 | if(not ref $ctx) | ||
| 71 | { | ||
| 72 | confess("ctx argument could be either string 'all' or AI::MXNet::Context") | ||
| 73 | unless $ctx eq 'all'; | ||
| 74 | check_call(AI::MXNetCAPI::RandomSeed($seed_state)); | ||
| 75 | } | ||
| 76 | else | ||
| 77 | { | ||
| 78 | check_call(AI::MXNetCAPI::RandomSeedContext($seed_state, $ctx->device_type_id, $ctx->device_id)); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | sub AUTOLOAD { | ||
| 83 | my $sub = $AI::MXNet::Random::AUTOLOAD; | ||
| 84 | $sub =~ s/.*:://; | ||
| 85 | shift; | ||
| 86 | my %updated; | ||
| 87 | my %defaults = ( | ||
| 88 | ctx => AI::MXNet::Context->current_ctx, | ||
| 89 | shape => 1, | ||
| 90 | out => 1 | ||
| 91 | ); | ||
| 92 | my @args; | ||
| 93 | my @tmp = @_; | ||
| 94 | if($sub eq 'randn') | ||
| 95 | { | ||
| 96 | $sub = 'normal'; | ||
| 97 | my @shape; | ||
| 98 | while(defined $tmp[0] and $tmp[0] =~ /^\d+$/) | ||
| 99 | { | ||
| 100 | push @shape, shift(@tmp); | ||
| 101 | } | ||
| 102 | if(@shape) | ||
| 103 | { | ||
| 104 | push @tmp, (shape => \@shape); | ||
| 105 | } | ||
| 106 | %defaults = (%defaults, loc => 0, scale => 1); | ||
| 107 | } | ||
| 108 | if(ref $tmp[-1] eq 'HASH') | ||
| 109 | { | ||
| 110 | my @kwargs = %{ pop(@tmp) }; | ||
| 111 | push @tmp, @kwargs; | ||
| 112 | } | ||
| 113 | while(@tmp >= 2 and not ref $tmp[-2]) | ||
| 114 | { | ||
| 115 | if(exists $defaults{$tmp[-2]}) | ||
| 116 | { | ||
| 117 | my $v = pop(@tmp); | ||
| 118 | my $k = pop(@tmp); | ||
| 119 | if(defined $v) | ||
| 120 | { | ||
| 121 | $updated{$k} = 1; | ||
| 122 | $defaults{$k} = $v; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | else | ||
| 126 | { | ||
| 127 | unshift @args, pop(@tmp); | ||
| 128 | unshift @args, pop(@tmp); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | unshift @args, @tmp; | ||
| 132 | if(blessed($defaults{out}) and not exists $updated{shape}) | ||
| 133 | { | ||
| 134 | delete $defaults{shape}; | ||
| 135 | } | ||
| 136 | delete $defaults{out} unless blessed $defaults{out}; | ||
| 137 | if($sub eq 'exponential') | ||
| 138 | { | ||
| 139 | my $changed = 0; | ||
| 140 | for my $i (0..@args-1) | ||
| 141 | { | ||
| 142 | if(not ref $args[$i] and $args[$i] eq 'scale') | ||
| 143 | { | ||
| 144 | $args[$i] = 'lam'; | ||
| 145 | $args[$i+1] = 1/$args[$i+1]; | ||
| 146 | $changed = 1; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | $args[0] = 1/$args[0] unless $changed; | ||
| 150 | } | ||
| 151 | if(grep { blessed($_) and $_->isa('AI::MXNet::NDArray') } @args) | ||
| 152 | { | ||
| 153 | if($sub eq 'normal') | ||
| 154 | { | ||
| 155 | my %mapping = qw/loc mu scale sigma/; | ||
| 156 | @args = map { (not ref $_ and exists $mapping{$_}) ? $mapping{$_} : $_ } @args | ||
| 157 | } | ||
| 158 | $sub = "_sample_$sub"; | ||
| 159 | delete $defaults{shape} if not exists $updated{shape}; | ||
| 160 | delete $defaults{ctx}; | ||
| 161 | return AI::MXNet::NDArray->$sub(@args, %defaults); | ||
| 162 | } | ||
| 163 | else | ||
| 164 | { | ||
| 165 | $sub = "_random_$sub"; | ||
| 166 | } | ||
| 167 | return AI::MXNet::NDArray->$sub(@args, %defaults); | ||
| 168 | } | ||
| 169 | |||
| 170 | 1; |