Preface
The API was thought bucket
to be an interface rooted in the underlying operation (MXNet doc
-_-| |). From LSTM
looking over, contact with a number of related programs, and then bucketing_module.py
see that part of the next, found that bucket is only an application layer mechanism, the main implementation exists in the module/bucketing_module.py inside. The principle is clear, the realization is concise, makes a mark in this.
Code & Comments
First put some related links, make a preparation.
- MXNet Official documents (\tucao out a document is not easy, also with timeliness ...)
- The Great God's blog elaborated, so pertinent
- Before about LSTM's blog
Given the fact that the great God has been so vivid in this [blog], it is possible to save the province, and then say the details of the gods who have no time to take into account.
In addition, considering that Mxnet's link often shows an unreliable symptom (\kuxia), it boils down to 1
some of the conclusions: 要使用bucket机制,初始化Module时传入的symbol应该是一个函数,这个函数在被调用时将被传入迭代器中的bucket_key参数
.
Go through the sequence of call paths.
On the fit
inside bind
, wait for the init
operation, and later call prepare
the pre-fetched data (if any) to prepare:
# module/bucketing_module.py defPrepare Self, Data_batch):"" " prepares a data batch for forward.Parameters ----------Data_batch:databatch """ # Perform bind if haven ' t done so assert Self. binded and Self. params_initialized Bucket_key=Data_batch.bucket_key Original_bucket_key= Self. _curr_bucket_key Data_shapes=Data_batch.provide_data Label_shapes=Data_batch.provide_label Self. Switch_bucket (Bucket_key, Data_shapes, Label_shapes)# switch back Self. Switch_bucket (Original_bucket_key,None,None)
Clearly, it switch_bucket
is the responsibility to rebind:
# module/bucketing_module.py defSwitch_bucket ( Self, Bucket_key, Data_shapes, label_shapes=None):assert Self. binded,' call bind before switching bucket ' if notBucket_keyinch Self. _buckets:# Check if there is already ...Symbol, Data_names, label_names= Self. _sym_gen (Bucket_key) module=Module (symbol, data_names, label_names, logger= Self. Logger, context= Self. _context, Work_load_list= Self. _work_load_list, Fixed_param_names= Self. _fixed_param_names, State_names= Self. _state_names) Module.bind (Data_shapes, Label_shapes, Self. _curr_module.for_training, Self. _curr_module.inputs_need_grad, Force_rebind=False, Shared_module= Self. _buckets[ Self. _default_bucket_key]) Self. _buckets[bucket_key]=Module Self. _curr_module= Self. _buckets[bucket_key] Self. _curr_bucket_key=Bucket_key
The logic is clear, _curr_module
there are many module, the parameters of these module all point to the same group. If the entry and exit bucket_key
does not appear, bind One and put it into the *_curr_module list, if it already has (including just bind out), switch to that On the module*.
Misc
There are some other related materials that are placed here.
- In the previous blog, it is speculated that the bucket mechanism may be processed for the part to be filled, which is somewhat related to the
io.py
inside DataBatch
pad
variables. In the module/base_module.py , find the reference pad , found and io.py inside the note consistent, only in the time of prediction to use, Be neglected when training.
exmple/rnn/bucketing
There are examples of the use of higher-level interfaces.
Notes on bucket mechanism in mxnet