AI-MXNet
view release on metacpan or search on metacpan
lib/AI/MXNet/Symbol.pm view on Meta::CPAN
return (
[ map { DTYPE_MX_TO_STR->{ $_ } } @{ $arg_type }],
[ map { DTYPE_MX_TO_STR->{ $_ } } @{ $out_type }],
[ map { DTYPE_MX_TO_STR->{ $_ } } @{ $aux_type }]
);
}
else
{
return (undef, undef, undef);
}
}
=head2 infer_shape
Infer the shape of outputs and arguments of given known shapes of arguments.
User can either pass in the known shapes in positional way or keyword argument way.
Tuple of Nones is returned if there is not enough information passed in.
An error will be raised if there is inconsistency found in the known shapes passed in.
Parameters
----------
*args :
Provide shape of arguments in a positional way.
Unknown shape can be marked as undef
**kwargs :
Provide keyword arguments of known shapes.
Returns
-------
arg_shapes : array ref of Shape or undef
List of shapes of arguments.
The order is in the same order as list_arguments()
out_shapes : array ref of Shape or undef
List of shapes of outputs.
The order is in the same order as list_outputs()
aux_shapes : array ref of Shape or undef
List of shapes of outputs.
The order is in the same order as list_auxiliary()
=cut
method infer_shape(Maybe[Str|Shape] @args)
{
my @res = $self->_infer_shape_impl(0, @args);
if(not defined $res[1])
{
my ($arg_shapes) = $self->_infer_shape_impl(1, @args);
my $arg_names = $self->list_arguments;
my @unknowns;
zip(sub {
my ($name, $shape) = @_;
if(not ref $shape or not @$shape or not product(@$shape))
{
if(@unknowns >= 10)
{
$unknowns[10] = '...';
}
else
{
my @shape = eval { @$shape };
push @unknowns, "$name @shape";
}
}
}, $arg_names, $arg_shapes);
AI::MXNet::Logging->warning(
"Cannot decide shape for the following arguments "
."(0s in shape means unknown dimensions). "
."Consider providing them as input:\n\t"
."\n\t"
.join(", ", @unknowns)
);
}
return @res;
}
=head2 infer_shape_partial
Partially infer the shape. The same as infer_shape, except that the partial
results can be returned.
=cut
method infer_shape_partial(Maybe[Str|Shape] @args)
{
$self->_infer_shape_impl(1, @args)
}
# The actual implementation for calling shape inference API.
method _infer_shape_impl(Maybe[Str|Shape] @args)
{
my $partial = shift(@args);
my ($positional_arguments, $kwargs, $kwargs_order) = _parse_arguments("Shape", @args);
my $sdata = [];
my $indptr = [0];
my $keys = [];
if(@{ $positional_arguments })
{
for my $shape (grep { defined } @{ $positional_arguments })
{
push @{ $sdata }, @{ $shape };
push @{ $indptr }, scalar(@{ $sdata });
}
}
{
for my $k (@{ $kwargs_order })
{
push @{ $keys }, $k;
push @{ $sdata }, @{ $kwargs->{ $k } };
push @{ $indptr }, scalar(@{ $sdata });
}
}
my $infer_func = $partial ? \&AI::MXNetCAPI::SymbolInferShapePartial : \&AI::MXNetCAPI::SymbolInferShape;
my ($arg_shapes, $out_shapes, $aux_shapes, $complete) = check_call(
$infer_func->(
$self->handle,
scalar(@{ $indptr }) - 1,
$keys,
$indptr,
$sdata,
)
);
lib/AI/MXNet/Symbol.pm view on Meta::CPAN
my @provided_grad_req_names;
if(defined $grad_req)
{
if(not ref $grad_req)
{
push @provided_grad_req_types, $grad_req;
}
elsif(ref $grad_req eq 'ARRAY')
{
assert((@{ $grad_req } != 0), 'grad_req in simple_bind cannot be an empty list');
@provided_grad_req_types = @{ $grad_req };
$provided_req_type_list_len = @provided_grad_req_types;
}
elsif(ref $grad_req eq 'HASH')
{
assert((keys %{ $grad_req } != 0), 'grad_req in simple_bind cannot be an empty hash');
while(my ($k, $v) = each %{ $grad_req })
{
push @provided_grad_req_names, $k;
push @provided_grad_req_types, $v;
}
$provided_req_type_list_len = @provided_grad_req_types;
}
}
my $num_ctx_map_keys = 0;
my @ctx_map_keys;
my @ctx_map_dev_types;
my @ctx_map_dev_ids;
if(defined $group2ctx)
{
while(my ($k, $v) = each %{ $group2ctx })
{
push @ctx_map_keys, $k;
push @ctx_map_dev_types, $v->device_type_id;
push @ctx_map_dev_ids, $v->device_id;
}
$num_ctx_map_keys = @ctx_map_keys;
}
my @shared_arg_name_list;
if(defined $shared_arg_names)
{
@shared_arg_name_list = @{ $shared_arg_names };
}
my %shared_data;
if(defined $shared_buffer)
{
while(my ($k, $v) = each %{ $shared_buffer })
{
$shared_data{$k} = $v->handle;
}
}
my $shared_exec_handle = defined $shared_exec ? $shared_exec->handle : undef;
my (
$updated_shared_data,
$in_arg_handles,
$arg_grad_handles,
$aux_state_handles,
$exe_handle
);
eval {
($updated_shared_data, $in_arg_handles, $arg_grad_handles, $aux_state_handles, $exe_handle)
=
check_call(
AI::MXNetCAPI::ExecutorSimpleBind(
$self->handle,
$ctx->device_type_id,
$ctx->device_id,
$num_ctx_map_keys,
\@ctx_map_keys,
\@ctx_map_dev_types,
\@ctx_map_dev_ids,
$provided_req_type_list_len,
\@provided_grad_req_names,
\@provided_grad_req_types,
scalar(@provided_arg_shape_names),
\@provided_arg_shape_names,
\@provided_arg_shape_data,
\@provided_arg_shape_idx,
$num_provided_arg_types,
\@provided_arg_type_names,
\@provided_arg_type_data,
scalar(@shared_arg_name_list),
\@shared_arg_name_list,
defined $shared_buffer ? \%shared_data : undef,
$shared_exec_handle
)
);
};
if($@)
{
confess(
"simple_bind failed: Error: $@; Arguments: ".
Data::Dumper->new(
[$shapes//{}]
)->Purity(1)->Deepcopy(1)->Terse(1)->Dump
);
}
if(defined $shared_buffer)
{
while(my ($k, $v) = each %{ $updated_shared_data })
{
$shared_buffer->{$k} = AI::MXNet::NDArray->new(handle => $v);
}
}
my @arg_arrays = map { AI::MXNet::NDArray->new(handle => $_) } @{ $in_arg_handles };
my @grad_arrays = map { defined $_ ? AI::MXNet::NDArray->new(handle => $_) : undef } @{ $arg_grad_handles };
my @aux_arrays = map { AI::MXNet::NDArray->new(handle => $_) } @{ $aux_state_handles };
my $executor = AI::MXNet::Executor->new(
handle => $exe_handle,
symbol => $self,
ctx => $ctx,
grad_req => $grad_req,
group2ctx => $group2ctx
);
$executor->arg_arrays(\@arg_arrays);
$executor->grad_arrays(\@grad_arrays);
$executor->aux_arrays(\@aux_arrays);
return $executor;
}
lib/AI/MXNet/Symbol.pm view on Meta::CPAN
}
else
{
for my $name (@{ $listed_arguments })
{
if(exists $grad_req->{ $name })
{
push @{ $req_array }, $req_map->{ $grad_req->{ $name } };
}
else
{
push @{ $req_array }, 0;
}
}
}
my $ctx_map_keys = [];
my $ctx_map_dev_types = [];
my $ctx_map_dev_ids = [];
if(defined $group2ctx)
{
while(my ($key, $val) = each %{ $group2ctx })
{
push @{ $ctx_map_keys } , $key;
push @{ $ctx_map_dev_types }, $val->device_type_id;
push @{ $ctx_map_dev_ids }, $val->device_id;
}
}
my $shared_handle = $shared_exec->handle if $shared_exec;
my $handle = check_call(AI::MXNetCAPI::ExecutorBindEX(
$self->handle,
$ctx->device_type_id,
$ctx->device_id,
scalar(@{ $ctx_map_keys }),
$ctx_map_keys,
$ctx_map_dev_types,
$ctx_map_dev_ids,
scalar(@{ $args }),
$args_handle,
$args_grad_handle,
$req_array,
scalar(@{ $aux_states }),
$aux_args_handle,
$shared_handle
)
);
my $executor = AI::MXNet::Executor->new(
handle => $handle,
symbol => $self,
ctx => $ctx,
grad_req => $grad_req,
group2ctx => $group2ctx
);
$executor->arg_arrays($args);
$executor->grad_arrays($args_grad);
$executor->aux_arrays($aux_states);
return $executor;
}
=head2 eval
Evaluate a symbol given arguments
The `eval` method combines a call to `bind` (which returns an executor)
with a call to `forward` (executor method).
For the common use case, where you might repeatedly evaluate with same arguments,
eval is slow.
In that case, you should call `bind` once and then repeatedly call forward.
Eval allows simpler syntax for less cumbersome introspection.
Parameters
----------
:$ctx : Context
The device context the generated executor to run on.
Optional, defaults to cpu(0)
:$args array ref of NDArray or hash ref of NDArray
- If the type is an array ref of NDArray, the position is in the same order of list_arguments.
- If the type is a hash of str to NDArray, then it maps the name of the argument
to the corresponding NDArray.
- In either case, all arguments must be provided.
Returns
----------
result : an array ref of NDArrays corresponding to the values
taken by each symbol when evaluated on given args.
When called on a single symbol (not a group),
the result will be an array ref with one element.
Examples:
my $result = $symbol->eval(ctx => mx->gpu, args => {data => mx->nd->ones([5,5])});
my $result = $symbol->eval(args => {data => mx->nd->ones([5,5])});
=cut
method eval(:$ctx=AI::MXNet::Context->cpu, HashRef[AI::MXNet::NDArray]|ArrayRef[AI::MXNet::NDArray] :$args)
{
return $self->bind(ctx => $ctx, args => $args)->forward;
}
=head2 grad
Get the autodiff of current symbol.
This function can only be used if current symbol is a loss function.
Parameters
----------
$wrt : Array of String
keyword arguments of the symbol that the gradients are taken.
Returns
-------
grad : AI::MXNet::Symbol
A gradient Symbol with returns to be the corresponding gradients.
=cut
method grad(ArrayRef[Str] $wrt)
{
my $handle = check_call(AI::MXNetCAPI::SymbolGrad(
$self->handle,
scalar(@$wrt),
$wrt
)
);
return __PACKAGE__->new(handle => $handle);
}
=head2 Variable
Create a symbolic variable with specified name.
Parameters
----------
name : str
Name of the variable.
attr : hash ref of string -> string
Additional attributes to set on the variable.
shape : array ref of positive integers
Optionally, one can specify the shape of a variable. This will be used during
shape inference. If user specified a different shape for this variable using
keyword argument when calling shape inference, this shape information will be ignored.
lr_mult : float
Specify learning rate muliplier for this variable.
wd_mult : float
Specify weight decay muliplier for this variable.
dtype : Dtype
Similar to shape, we can specify dtype for this variable.
init : initializer (mx->init->*)
Specify initializer for this variable to override the default initializer
kwargs : hash ref
other additional attribute variables
Returns
-------
variable : Symbol
The created variable symbol.
=cut
( run in 0.865 second using v1.01-cache-2.11-cpan-acf6aa7dc9e )