rwkv
mindnlp.transformers.models.rwkv.modeling_rwkv
¶
MindSpore RWKV model.
mindnlp.transformers.models.rwkv.modeling_rwkv.RwkvCausalLMOutput
dataclass
¶
Bases: ModelOutput
Base class for causal language model (or autoregressive) outputs.
| PARAMETER | DESCRIPTION |
|---|---|
loss
|
Language modeling loss (for next-token prediction).
TYPE:
|
logits
|
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
TYPE:
|
state
|
The state of the model at the last time step. Can be used in a forward method with the next
TYPE:
|
hidden_states
|
Tuple of Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
TYPE:
|
attentions
|
Tuple of Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
TYPE:
|
Source code in mindnlp\transformers\models\rwkv\modeling_rwkv.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | |
mindnlp.transformers.models.rwkv.modeling_rwkv.RwkvForCausalLM
¶
Bases: RwkvPreTrainedModel
Source code in mindnlp\transformers\models\rwkv\modeling_rwkv.py
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 | |
mindnlp.transformers.models.rwkv.modeling_rwkv.RwkvForCausalLM.forward(input_ids=None, attention_mask=None, inputs_embeds=None, state=None, labels=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
labels (mindspore.Tensor of shape (batch_size, sequence_length), optional):
Labels for language modeling. Note that the labels are shifted inside the model, i.e. you can set
labels = input_ids Indices are selected in [-100, 0, ..., config.vocab_size] All labels set to -100
are ignored (masked), the loss is only computed for labels in [0, ..., config.vocab_size]
Source code in mindnlp\transformers\models\rwkv\modeling_rwkv.py
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 | |
mindnlp.transformers.models.rwkv.modeling_rwkv.RwkvLinearAttention
¶
Bases: Cell
RWKV linear attention
Source code in mindnlp\transformers\models\rwkv\modeling_rwkv.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
mindnlp.transformers.models.rwkv.modeling_rwkv.RwkvLinearAttention.__init__(config)
¶
Initializes an instance of the RwkvLinearAttention class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the RwkvLinearAttention class.
TYPE:
|
config
|
The configuration object containing the context length parameter. It is used to set the maximum sequence length and load CUDA kernels. Must have the attribute 'context_length' specifying the context length.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the 'config' object does not have the 'context_length' attribute. |
RuntimeError
|
If there is an issue loading the CUDA kernels. |
Source code in mindnlp\transformers\models\rwkv\modeling_rwkv.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
mindnlp.transformers.models.rwkv.modeling_rwkv.RwkvLinearAttention.bprop(w, u, k, v, s, return_state, y, gy)
¶
bporp for wkv
Source code in mindnlp\transformers\models\rwkv\modeling_rwkv.py
165 166 167 168 169 170 171 172 173 174 175 | |
mindnlp.transformers.models.rwkv.modeling_rwkv.RwkvLinearAttention.construct(time_decay, time_first, key, value, state=None, return_state=False)
¶
Constructs the linear attention mechanism for the RwkvLinearAttention class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the RwkvLinearAttention class.
|
time_decay
|
The time decay factor for the attention mechanism.
TYPE:
|
time_first
|
The time first factor for the attention mechanism.
TYPE:
|
key
|
The input tensor representing the keys for the attention mechanism. The shape of the tensor should be (batch_size, seq_len, hidden_size).
TYPE:
|
value
|
The input tensor representing the values for the attention mechanism. The shape of the tensor should be (batch_size, seq_len, hidden_size).
TYPE:
|
state
|
The optional input tensor representing the state for the attention mechanism. It has a default value of None. The shape of the tensor should be (batch_size, hidden_size, 3).
TYPE:
|
return_state
|
A flag indicating whether to return the state. It has a default value of False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Tuple[Tensor, Tensor]: A tuple containing the output tensor of the attention mechanism |
|
|
and the state tensor if return_state is True. The output tensor represents the result of |
|
|
the attention mechanism. |
|
|
The state tensor represents the updated state of the attention mechanism if return_state is True. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the sequence length is greater than the maximum sequence length allowed by the model. |
ValueError
|
If the product of batch size and hidden size is not a round multiple of the minimum of the hidden size and 32. |
Source code in mindnlp\transformers\models\rwkv\modeling_rwkv.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
mindnlp.transformers.models.rwkv.modeling_rwkv.RwkvOutput
dataclass
¶
Bases: ModelOutput
Class for the RWKV model outputs.
| PARAMETER | DESCRIPTION |
|---|---|
last_hidden_state
|
Sequence of hidden-states at the output of the last layer of the model.
TYPE:
|
state
|
The state of the model at the last time step. Can be used in a forward method with the next
TYPE:
|
hidden_states
|
Tuple of Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
TYPE:
|
attentions
|
Tuple of Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
TYPE:
|
Source code in mindnlp\transformers\models\rwkv\modeling_rwkv.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 | |
mindnlp.transformers.models.rwkv.modeling_rwkv.RwkvPreTrainedModel
¶
Bases: PreTrainedModel
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models.
Source code in mindnlp\transformers\models\rwkv\modeling_rwkv.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
mindnlp.transformers.models.rwkv.modeling_rwkv.load_wkv_cuda_kernel(func_name, context_length)
¶
load wkv cuda kernel
Source code in mindnlp\transformers\models\rwkv\modeling_rwkv.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
mindnlp.transformers.models.rwkv.configuration_rwkv
¶
RWKV configuration
mindnlp.transformers.models.rwkv.configuration_rwkv.RwkvConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [RwkvModel]. It is used to instantiate a RWKV
model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
defaults will yield a similar configuration to that of the RWVK-4
RWKV/rwkv-4-169m-pile architecture.
Configuration objects inherit from [PretrainedConfig] and can be used to control the model outputs. Read the
documentation from [PretrainedConfig] for more information.
| PARAMETER | DESCRIPTION |
|---|---|
vocab_size
|
Vocabulary size of the RWKV model. Defines the number of different tokens that can be represented by the
TYPE:
|
context_length
|
The maximum sequence length that this model can be be used with in a single forward (using it in RNN mode lets use any sequence length).
TYPE:
|
hidden_size
|
Dimensionality of the embeddings and hidden states.
TYPE:
|
num_hidden_layers
|
Number of hidden layers in the model.
TYPE:
|
attention_hidden_size
|
Dimensionality of the attention hidden states. Will default to
TYPE:
|
intermediate_size
|
Dimensionality of the inner feed-forward layers. Will default to 4 times
TYPE:
|
layer_norm_eps
|
The epsilon to use in the layer normalization layers.
TYPE:
|
bos_token_id
|
The id of the beginning of sentence token in the vocabulary. Defaults to 0 as RWKV uses the same tokenizer as GPTNeoX.
TYPE:
|
eos_token_id
|
The id of the end of sentence token in the vocabulary. Defaults to 0 as RWKV uses the same tokenizer as GPTNeoX.
TYPE:
|
rescale_every
|
At inference, the hidden states (and weights of the correponding output layers) are divided by 2 every
TYPE:
|
tie_word_embeddings
|
Whether or not to tie the word embeddings with the input token embeddings.
TYPE:
|
use_cache
|
Whether or not the model should return the last state.
TYPE:
|
Example
>>> from transformers import RwkvConfig, RwkvModel
...
>>> # Initializing a Rwkv configuration
>>> configuration = RwkvConfig()
...
>>> # Initializing a model (with random weights) from the configuration
>>> model = RwkvModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp\transformers\models\rwkv\configuration_rwkv.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
mindnlp.transformers.models.rwkv.configuration_rwkv.RwkvConfig.__init__(vocab_size=50277, context_length=1024, hidden_size=4096, num_hidden_layers=32, attention_hidden_size=None, intermediate_size=None, layer_norm_epsilon=1e-05, bos_token_id=0, eos_token_id=0, rescale_every=6, tie_word_embeddings=False, use_cache=True, **kwargs)
¶
Initializes an instance of RwkvConfig.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance itself.
|
vocab_size
|
The size of the vocabulary. Default is 50277.
TYPE:
|
context_length
|
The length of the context. Default is 1024.
TYPE:
|
hidden_size
|
The size of the hidden layers. Default is 4096.
TYPE:
|
num_hidden_layers
|
The number of hidden layers. Default is 32.
TYPE:
|
attention_hidden_size
|
The size of the attention hidden layer. Defaults to hidden_size if not provided.
TYPE:
|
intermediate_size
|
The size of the intermediate layer. Defaults to 4 times hidden_size if not provided.
TYPE:
|
layer_norm_epsilon
|
The epsilon value for layer normalization. Default is 1e-05.
TYPE:
|
bos_token_id
|
The beginning of sentence token id. Default is 0.
TYPE:
|
eos_token_id
|
The end of sentence token id. Default is 0.
TYPE:
|
rescale_every
|
The frequency of rescaling. Default is 6.
TYPE:
|
tie_word_embeddings
|
Whether to tie word embeddings. Default is False.
TYPE:
|
use_cache
|
Whether to use cache. Default is True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the provided vocab_size, context_length, hidden_size, num_hidden_layers, attention_hidden_size, intermediate_size, layer_norm_epsilon, bos_token_id, eos_token_id, or rescale_every is not a positive integer. |
TypeError
|
If any of the provided parameters has an unexpected type. |
Source code in mindnlp\transformers\models\rwkv\configuration_rwkv.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |