ó šÄïYc@s~dZddlZddlZddlmZddlmZddlm Z m Z ddl m Z d e fd „ƒYZ dS( s¨A `BucketingModule` implement the `BaseModule` API, and allows multiple symbols to be used depending on the `bucket_key` provided by each different mini-batch of data. iÿÿÿÿNi(tcontext(tUniformi(t BaseModulet_check_input_names(tModuletBucketingModulecBsyeZdZdeejƒdddd„Zd„Ze d„ƒZ e d„ƒZ e d„ƒZ e d„ƒZ e d„ƒZd„Zeeed „Zed ƒddeeed „Zed „Zddd „Zdeeeddd„Zdd„Zddd ed„Zd„Zdd„Zdd„Zd„Zed„Zed„Zd„Z e d„ƒZ!d„Z"RS(!s,This module helps to deal efficiently with varying-length inputs. Parameters ---------- sym_gen : function A function when called with a bucket key, returns a triple ``(symbol, data_names, label_names)``. default_bucket_key : str (or any python object) The key for the default bucket. logger : Logger context : Context or list of Context Defaults to ``mx.cpu()`` work_load_list : list of number Defaults to ``None``, indicating uniform workload. fixed_param_names: list of str Defaults to ``None``, indicating no network parameters are fixed. state_names : list of str States are similar to data and label, but not provided by data iterator. Instead they are initialized to 0 and can be set by set_states() c Csbtt|ƒjd|ƒ|dk s+t‚||_||_||ƒ\}} } | dk rjt| ƒng} | dk rˆt| ƒng} |dk r¦t|ƒng}|dk rÄt|ƒng}t|| dt ƒt|| dt ƒt||dt ƒt||dt ƒ||_ ||_ ||_ ||_i|_d|_d|_t |_dS(Ntloggertdatatlabeltstatet fixed_param(tsuperRt__init__tNonetAssertionErrort_default_bucket_keyt_sym_gentlistRtTruetFalset_fixed_param_namest _state_namest_contextt_work_load_listt_bucketst _curr_modulet_curr_bucket_keyt _params_dirty( tselftsym_gentdefault_bucket_keyRRtwork_load_listtfixed_param_namest state_namestsymbolt data_namest label_names((s=build/bdist.linux-armv7l/egg/mxnet/module/bucketing_module.pyR 8s*         cCs(t|_i|_d|_d|_dS(s+Internal utility function to reset binding.N(RtbindedRR RR(R((s=build/bdist.linux-armv7l/egg/mxnet/module/bucketing_module.pyt _reset_bindVs   cCs6|jr|jjS|j|jƒ\}}}|SdS(s1A list of names for data required by this module.N(R%RR#RR(Rt_R#((s=build/bdist.linux-armv7l/egg/mxnet/module/bucketing_module.pyR#]s  cCs<|jr|jjS|j|jƒ\}}}|jƒSdS(s/A list of names for the outputs of this module.N(R%Rt output_namesRRt list_outputs(RR"R'((s=build/bdist.linux-armv7l/egg/mxnet/module/bucketing_module.pyR(fs  cCs|jst‚|jjS(scGet data shapes. Returns ------- A list of `(name, shape)` pairs. (R%RRt data_shapes(R((s=build/bdist.linux-armv7l/egg/mxnet/module/bucketing_module.pyR*oscCs|jst‚|jjS(s.Get label shapes. Returns ------- A list of `(name, shape)` pairs. The return value could be ``None`` if the module does not need labels, or if the module is not bound for training (in this case, label information is not available). (R%RRt label_shapes(R((s=build/bdist.linux-armv7l/egg/mxnet/module/bucketing_module.pyR+zs cCs|jst‚|jjS(sfGets output shapes. Returns ------- A list of `(name, shape)` pairs. (R%RRt output_shapes(R((s=build/bdist.linux-armv7l/egg/mxnet/module/bucketing_module.pyR,ˆscCsC|jr|jst‚|j|j_|jjƒ}t|_|S(s¸Gets current parameters. Returns ------- `(arg_params, aux_params)` A pair of dictionaries each mapping parameter names to NDArray values. (R%tparams_initializedRRRt get_paramsR(Rtparams((s=build/bdist.linux-armv7l/egg/mxnet/module/bucketing_module.pyR.“s  c Cs”|s2|jddd|d|d|d|ƒdS|jrY| rYtjddd ƒdS|jj||d|d|d |ƒt|_t|_dS( sAssigns parameters and aux state values. Parameters ---------- arg_params : dict Dictionary of name to value (`NDArray`) mapping. aux_params : dict Dictionary of name to value (`NDArray`) mapping. allow_missing : bool If true, params could contain missing values, and the initializer will be called to fill those missing params. force_init : bool If true, will force re-initialize even if already initialized. allow_extra : boolean, optional Whether allow extra parameters that are not needed by symbol. If this is True, no error will be thrown when arg_params or aux_params contain extra parameters that is not needed by the executor. Examples -------- >>> # An example of setting module parameters. >>> sym, arg_params, aux_params = mx.model.load_checkpoint(model_prefix, n_epoch_load) >>> mod.set_params(arg_params=arg_params, aux_params=aux_params) t initializert arg_paramst aux_paramst allow_missingt force_initNsMParameters already initialized and force_init=False. set_params call ignored.t stacklevelit allow_extra( t init_paramsR R-twarningstwarnRt set_paramsRR(RR1R2R3R4R6((s=build/bdist.linux-armv7l/egg/mxnet/module/bucketing_module.pyR:¡s    g{®Gáz„?c Csp|jr| rdS|js)tdƒ‚|jjd|d|d|d|d|d|ƒt|_t|_dS( sŒInitializes parameters. Parameters ---------- initializer : Initializer arg_params : dict Defaults to ``None``. Existing parameters. This has higher priority than `initializer`. aux_params : dict Defaults to ``None``. Existing auxiliary states. This has higher priority than `initializer`. allow_missing : bool Allow missing values in `arg_params` and `aux_params` (if not ``None``). In this case, missing values will be filled with `initializer`. force_init : bool Defaults to ``False``. allow_extra : boolean, optional Whether allow extra parameters that are not needed by symbol. If this is True, no error will be thrown when arg_params or aux_params contain extra parameters that is not needed by the executor. Ns,call bind before initializing the parametersR0R1R2R3R4R6(R-R%RRR7RRR(RR0R1R2R3R4R6((s=build/bdist.linux-armv7l/egg/mxnet/module/bucketing_module.pyR7Ìs   cCs+|jr|jst‚|jjd|ƒS(s´Gets states from all devices. Parameters ---------- merge_multi_context : bool Default is `True`. In the case when data-parallelism is used, the states will be collected from multiple devices. A `True` value indicate that we should merge the collected results so that they look like from a single executor. Returns ------- list of NDArrays or list of list of NDArrays If `merge_multi_context` is ``True``, it is like ``[out1, out2]``. Otherwise, it is like ``[[out1_dev1, out1_dev2], [out2_dev1, out2_dev2]]``. All the output elements are `NDArray`. tmerge_multi_context(R%R-RRt get_states(RR;((s=build/bdist.linux-armv7l/egg/mxnet/module/bucketing_module.pyR<ìscCs/|jr|jst‚|jj||ƒdS(sgSets value for states. Only one of states & values can be specified. Parameters ---------- states : list of list of NDArrays Source states arrays formatted like ``[[state1_dev1, state1_dev2], [state2_dev1, state2_dev2]]``. value : number A single scalar value for all state arrays. N(R%R-RRt set_states(Rtstatestvalue((s=build/bdist.linux-armv7l/egg/mxnet/module/bucketing_module.pyR=s twritecCsH|jr|jƒ\}} n|r1|jƒn|jrN|jjdƒdS|dksftdƒ‚||_||_ t |_|j |j ƒ\} } } t | | | d|jd|jd|jd|jd|jƒ} | j||||d td dd |ƒ| |_|j |_| |j|j <|jrD|j|| ƒndS( sŠBinding for a `BucketingModule` means setting up the buckets and binding the executor for the default bucket key. Executors corresponding to other keys are bound afterwards with `switch_bucket`. Parameters ---------- data_shapes : list of (str, tuple) This should correspond to the symbol for the default bucket. label_shapes : list of (str, tuple) This should correspond to the symbol for the default bucket. for_training : bool Default is ``True``. inputs_need_grad : bool Default is ``False``. force_rebind : bool Default is ``False``. shared_module : BucketingModule Default is ``None``. This value is currently not used. grad_req : str, list of str, dict of str to str Requirement for gradient accumulation. Can be 'write', 'add', or 'null' (default to 'write'). Can be specified globally (str) or for each argument (list, dict). bucket_key : str (or any python object) bucket key for binding. by default use the default_bucket_key sAlready bound, ignoring bind()Ns2shared_module for BucketingModule is not supportedRRRR R!t force_rebindt shared_moduletgrad_req(R-R.R&R%RtwarningR Rt for_trainingtinputs_need_gradRRRRRRRRtbindRRRRR:(RR*R+RERFRARBRCR1R2R"R#R$tmodule((s=build/bdist.linux-armv7l/egg/mxnet/module/bucketing_module.pyRGs.           cCsà|jstdƒ‚||jkrÃ|j|ƒ\}}}t|||d|jd|jd|jd|jd|j ƒ}|j |||j j |j j dtd|j|jƒ||j|s