CuDNN version of LSTM for GPUs. input_blob Blob containing the input. Will need to be available when param_init_net is run, because the sequence lengths and batch sizes will be inferred from the size of this bl
(model, input_blob, initial_states, dim_in, dim_out,
scope, recurrent_params=None, input_params=None,
num_layers=1, return_params=False)
| 1704 | |
| 1705 | |
| 1706 | def cudnn_LSTM(model, input_blob, initial_states, dim_in, dim_out, |
| 1707 | scope, recurrent_params=None, input_params=None, |
| 1708 | num_layers=1, return_params=False): |
| 1709 | ''' |
| 1710 | CuDNN version of LSTM for GPUs. |
| 1711 | input_blob Blob containing the input. Will need to be available |
| 1712 | when param_init_net is run, because the sequence lengths |
| 1713 | and batch sizes will be inferred from the size of this |
| 1714 | blob. |
| 1715 | initial_states tuple of (hidden_init, cell_init) blobs |
| 1716 | dim_in input dimensions |
| 1717 | dim_out output/hidden dimension |
| 1718 | scope namescope to apply |
| 1719 | recurrent_params dict of blobs containing values for recurrent |
| 1720 | gate weights, biases (if None, use random init values) |
| 1721 | See GetLSTMParamNames() for format. |
| 1722 | input_params dict of blobs containing values for input |
| 1723 | gate weights, biases (if None, use random init values) |
| 1724 | See GetLSTMParamNames() for format. |
| 1725 | num_layers number of LSTM layers |
| 1726 | return_params if True, returns (param_extract_net, param_mapping) |
| 1727 | where param_extract_net is a net that when run, will |
| 1728 | populate the blobs specified in param_mapping with the |
| 1729 | current gate weights and biases (input/recurrent). |
| 1730 | Useful for assigning the values back to non-cuDNN |
| 1731 | LSTM. |
| 1732 | ''' |
| 1733 | with core.NameScope(scope): |
| 1734 | weight_params = GetLSTMParamNames()['weights'] |
| 1735 | bias_params = GetLSTMParamNames()['biases'] |
| 1736 | |
| 1737 | input_weight_size = dim_out * dim_in |
| 1738 | upper_layer_input_weight_size = dim_out * dim_out |
| 1739 | recurrent_weight_size = dim_out * dim_out |
| 1740 | input_bias_size = dim_out |
| 1741 | recurrent_bias_size = dim_out |
| 1742 | |
| 1743 | def init(layer, pname, input_type): |
| 1744 | input_weight_size_for_layer = input_weight_size if layer == 0 else \ |
| 1745 | upper_layer_input_weight_size |
| 1746 | if pname in weight_params: |
| 1747 | sz = input_weight_size_for_layer if input_type == 'input' \ |
| 1748 | else recurrent_weight_size |
| 1749 | elif pname in bias_params: |
| 1750 | sz = input_bias_size if input_type == 'input' \ |
| 1751 | else recurrent_bias_size |
| 1752 | else: |
| 1753 | assert False, "unknown parameter type {}".format(pname) |
| 1754 | return model.param_init_net.UniformFill( |
| 1755 | [], |
| 1756 | "lstm_init_{}_{}_{}".format(input_type, pname, layer), |
| 1757 | shape=[sz]) |
| 1758 | |
| 1759 | # Multiply by 4 since we have 4 gates per LSTM unit |
| 1760 | first_layer_sz = input_weight_size + recurrent_weight_size + \ |
| 1761 | input_bias_size + recurrent_bias_size |
| 1762 | upper_layer_sz = upper_layer_input_weight_size + \ |
| 1763 | recurrent_weight_size + input_bias_size + \ |
nothing calls this directly
no test coverage detected
searching dependent graphs…