| 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::Symbol::NameManager; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Mouse; | ||
| 22 | use AI::MXNet::Function::Parameters; | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | AI::MXNet::Symbol::NameManager - Automated symbol naming. | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | NameManager that does an automatic naming. | ||
| 31 | |||
| 32 | A user can also inherit this object to change the naming behavior. | ||
| 33 | =cut | ||
| 34 | |||
| 35 | has 'counter' => ( | ||
| 36 | is => 'ro', | ||
| 37 | isa => 'HashRef', | ||
| 38 | default => sub { +{} } | ||
| 39 | ); | ||
| 40 | |||
| 41 | our $current; | ||
| 42 | |||
| 43 | =head2 get | ||
| 44 | |||
| 45 | Get the canonical name for a symbol. | ||
| 46 | |||
| 47 | This is default implementation. | ||
| 48 | When user specified a name, | ||
| 49 | the user specified name will be used. | ||
| 50 | |||
| 51 | When user did not, we will automatically generate a | ||
| 52 | name based on hint string. | ||
| 53 | |||
| 54 | Parameters | ||
| 55 | ---------- | ||
| 56 | name : str or undef | ||
| 57 | The name the user has specified. | ||
| 58 | |||
| 59 | hint : str | ||
| 60 | A hint string, which can be used to generate name. | ||
| 61 | |||
| 62 | Returns | ||
| 63 | ------- | ||
| 64 | full_name : str | ||
| 65 | A canonical name for the symbol. | ||
| 66 | =cut | ||
| 67 | |||
| 68 | method get(Maybe[Str] $name, Str $hint) | ||
| 69 | { | ||
| 70 | return $name if $name; | ||
| 71 | if(not exists $self->counter->{ $hint }) | ||
| 72 | { | ||
| 73 | $self->counter->{ $hint } = 0; | ||
| 74 | } | ||
| 75 | $name = sprintf("%s%d", $hint, $self->counter->{ $hint }); | ||
| 76 | $self->counter->{ $hint }++; | ||
| 77 | return $name; | ||
| 78 | } | ||
| 79 | |||
| 80 | method current() | ||
| 81 | { | ||
| 82 | $AI::MXNet::NameManager; | ||
| 83 | } | ||
| 84 | |||
| 85 | method set_current(AI::MXNet::Symbol::NameManager $new) | ||
| 86 | { | ||
| 87 | $AI::MXNet::NameManager = $new; | ||
| 88 | } | ||
| 89 | |||
| 90 | package AI::MXNet::Symbol::Prefix; | ||
| 91 | use Mouse; | ||
| 92 | |||
| 93 | =head1 NAME | ||
| 94 | |||
| 95 | AI::MXNet::Symbol::Prefix | ||
| 96 | =cut | ||
| 97 | |||
| 98 | extends 'AI::MXNet::Symbol::NameManager'; | ||
| 99 | |||
| 100 | =head1 DESCRIPTION | ||
| 101 | |||
| 102 | A name manager that always attaches a prefix to all names. | ||
| 103 | =cut | ||
| 104 | |||
| 105 | has prefix => ( | ||
| 106 | is => 'ro', | ||
| 107 | isa => 'Str', | ||
| 108 | required => 1 | ||
| 109 | ); | ||
| 110 | |||
| 111 | method get(Maybe[Str] $name, Str $hint) | ||
| 112 | { | ||
| 113 | $name = $self->SUPER::get($name, $hint); | ||
| 114 | return $self->prefix . $name; | ||
| 115 | } | ||
| 116 | |||
| 117 | 1; |