cogvlm
mindnlp.transformers.models.cogvlm.configuration_cogvlm.CogVLMConfig
¶
Bases: PretrainedConfig
The CogVLMConfig
class represents the configuration for a CogVLM (Cognitive Vision Language Model) model.
It inherits from the PretrainedConfig
class and provides a set of parameters to customize the
behavior of the CogVLM model.
PARAMETER | DESCRIPTION |
---|---|
`vocab_size` |
The size of the vocabulary. Defaults to 32000.
TYPE:
|
`hidden_size` |
The size of the hidden layers. Defaults to 4096.
TYPE:
|
`intermediate_size` |
The size of the intermediate layers. Defaults to 11008.
TYPE:
|
`num_hidden_layers` |
The number of hidden layers. Defaults to 32.
TYPE:
|
`num_attention_heads` |
The number of attention heads. Defaults to 32.
TYPE:
|
`hidden_act` |
The activation function for the hidden layers. Defaults to 'silu'.
TYPE:
|
`max_position_embeddings` |
The maximum number of position embeddings. Defaults to 2048.
TYPE:
|
`initializer_range` |
The range for the weight initialization. Defaults to 0.02.
TYPE:
|
`rms_norm_eps` |
The epsilon value for the RMS normalization. Defaults to 1e-06.
TYPE:
|
`template_version` |
The template version to use. Defaults to 'chat'.
TYPE:
|
`pad_token_id` |
The token ID for padding. Defaults to 0.
TYPE:
|
`bos_token_id` |
The token ID for the beginning of sentence. Defaults to 1.
TYPE:
|
`eos_token_id` |
The token ID for the end of sentence. Defaults to 2.
TYPE:
|
`tie_word_embeddings` |
Whether to tie the word embeddings. Defaults to False.
TYPE:
|
`use_cache` |
Whether to use cache during model inference. Defaults to True.
TYPE:
|
ATTRIBUTE | DESCRIPTION |
---|---|
`hidden_size` |
The size of the hidden layers.
TYPE:
|
`intermediate_size` |
The size of the intermediate layers.
TYPE:
|
`num_attention_heads` |
The number of attention heads.
TYPE:
|
`max_position_embeddings` |
The maximum number of position embeddings.
TYPE:
|
`rms_norm_eps` |
The epsilon value for the RMS normalization.
TYPE:
|
`initializer_range` |
The range for the weight initialization.
TYPE:
|
`vocab_size` |
The size of the vocabulary.
TYPE:
|
`num_hidden_layers` |
The number of hidden layers.
TYPE:
|
`hidden_act` |
The activation function for the hidden layers.
TYPE:
|
`template_version` |
The template version to use.
TYPE:
|
`use_cache` |
Whether to use cache during model inference.
TYPE:
|
`vision_config` |
The configuration for the vision module.
The
TYPE:
|
Note
This class does not include the actual model architecture, but only the configuration parameters for the CogVLM model.
Source code in mindnlp/transformers/models/cogvlm/configuration_cogvlm.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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 |
|
mindnlp.transformers.models.cogvlm.configuration_cogvlm.CogVLMConfig.__init__(vocab_size=32000, hidden_size=4096, intermediate_size=11008, num_hidden_layers=32, num_attention_heads=32, hidden_act='silu', max_position_embeddings=2048, initializer_range=0.02, rms_norm_eps=1e-06, template_version='chat', pad_token_id=0, bos_token_id=1, eos_token_id=2, tie_word_embeddings=False, use_cache=True, **kwargs)
¶
Initialize CogVLMConfig.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
vocab_size |
The size of the vocabulary. Defaults to 32000.
TYPE:
|
hidden_size |
The size of the hidden layers. Defaults to 4096.
TYPE:
|
intermediate_size |
The size of the intermediate layer in the transformer. Defaults to 11008.
TYPE:
|
num_hidden_layers |
The number of hidden layers in the transformer. Defaults to 32.
TYPE:
|
num_attention_heads |
The number of attention heads in the transformer. Defaults to 32.
TYPE:
|
hidden_act |
The activation function for the hidden layers. Defaults to 'silu'.
TYPE:
|
max_position_embeddings |
The maximum position for positional embeddings. Defaults to 2048.
TYPE:
|
initializer_range |
The range for weight initialization. Defaults to 0.02.
TYPE:
|
rms_norm_eps |
The epsilon value for RMS normalization. Defaults to 1e-06.
TYPE:
|
template_version |
The version of the template. Defaults to 'chat'.
TYPE:
|
pad_token_id |
The id for padding token. Defaults to 0.
TYPE:
|
bos_token_id |
The id for beginning of sequence token. Defaults to 1.
TYPE:
|
eos_token_id |
The id for end of sequence token. Defaults to 2.
TYPE:
|
tie_word_embeddings |
Whether to tie word embeddings. Defaults to False.
TYPE:
|
use_cache |
Whether to use caching. Defaults to True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If vocab_size, hidden_size, intermediate_size, num_hidden_layers, num_attention_heads, max_position_embeddings, pad_token_id, bos_token_id, eos_token_id are not integers. |
ValueError
|
If initializer_range, rms_norm_eps are not floats, or if template_version is not 'base' or 'chat'. |
AssertionError
|
If hidden_act is not a string. |
NotImplementedError
|
If tie_word_embeddings is not a boolean or if use_cache is not a boolean. |
Source code in mindnlp/transformers/models/cogvlm/configuration_cogvlm.py
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 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.MLP
¶
Bases: Module
This class represents a Multi-Layer Perceptron (MLP) neural network model, which is used for various machine learning tasks. The MLP class inherits from the nn.Module class, which is a fundamental building block for creating neural network models.
ATTRIBUTE | DESCRIPTION |
---|---|
hidden_size |
The size of the hidden layer in the MLP.
TYPE:
|
intermediate_size |
The size of the intermediate layer in the MLP.
TYPE:
|
gate_proj |
The dense layer responsible for projecting the input to the intermediate size.
TYPE:
|
up_proj |
The dense layer responsible for projecting the input to the intermediate size.
TYPE:
|
down_proj |
The dense layer responsible for projecting the intermediate size back to the hidden size.
TYPE:
|
act_fn |
The activation function used in the hidden layer of the MLP.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
forward |
Constructs the forward pass of the MLP given an input tensor. |
Example
>>> config = MLPConfig(hidden_size=128, intermediate_size=64, hidden_act="relu")
>>> mlp = MLP(config)
>>> input_tensor = torch.randn(10, 128)
>>> output = mlp.forward(input_tensor)
Note
The MLP class assumes that the ACT2FN dictionary, containing activation functions, is defined in the global scope.
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.MLP.__init__(config)
¶
Initializes an instance of the MLP class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MLP class.
|
config |
An object containing configuration parameters for the MLP.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.MLP.forward(x)
¶
Method to forward a down_proj output based on the given input x.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MLP class.
TYPE:
|
x |
Input tensor of shape (batch_size, features) to be processed.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value directly. The down_proj output is stored in the internal state. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the input x is not of the expected type. |
ValueError
|
If the dimensions of the input x are not compatible with the operations. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.RMSNorm
¶
Bases: Module
This class represents a Root Mean Square Normalization (RMSNorm) layer that can be used in neural networks for feature normalization.
RMSNorm is a technique used to normalize the hidden states of a neural network layer. It calculates the variance of the hidden states and applies normalization based on the root mean square of the variance.
This class inherits from the nn.Module class in the MindSpore library.
ATTRIBUTE | DESCRIPTION |
---|---|
weight |
The weight parameter used for the normalization.
TYPE:
|
variance_epsilon |
A small value added to the variance to avoid division by zero.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes a new instance of the RMSNorm class. Args:
|
forward |
Applies RMSNorm normalization to the given hidden states. Args:
Returns:
|
Note
- The RMSNorm layer assumes that the input hidden states have a shape of (batch_size, hidden_size).
- The RMSNorm layer expects the input hidden states to have a floating-point data type.
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
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 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.RMSNorm.__init__(hidden_size, eps=1e-06)
¶
Initializes a new instance of the RMSNorm class.
PARAMETER | DESCRIPTION |
---|---|
hidden_size |
The size of the hidden layer in the neural network.
TYPE:
|
eps |
The epsilon value used for numerical stability in the calculation of the variance. Defaults to 1e-06.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the hidden_size is not a positive integer. |
TypeError
|
If the eps is not a float. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.RMSNorm.forward(hidden_states)
¶
Constructs an RMSNorm object.
This method applies the RMS normalization technique to the given hidden states.
PARAMETER | DESCRIPTION |
---|---|
self |
The RMSNorm object.
TYPE:
|
hidden_states |
The input hidden states to be normalized. It should have the shape (batch_size, sequence_length, hidden_size). The data type should be convertible to float32.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method modifies the hidden_states tensor in-place. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the input hidden_states tensor is not of type mindspore.Tensor. |
ValueError
|
If the input hidden_states tensor does not have the correct shape. |
ValueError
|
If the input hidden_states tensor data type cannot be converted to float32. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
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 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.VisionExpertMLP
¶
Bases: Module
The VisionExpertMLP class represents a multi-layer perceptron (MLP) model designed for expert processing of vision-related and language-related inputs. This class inherits from the nn.Module module.
ATTRIBUTE | DESCRIPTION |
---|---|
language_mlp |
An instance of the MLP class for processing language-related inputs.
TYPE:
|
vision_mlp |
An instance of the MLP class for processing vision-related inputs.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
forward |
Processes the input hidden states based on the token type IDs to produce the output. |
Detailed Description |
|
The forward method takes the following parameters |
|
The forward method returns |
|
Note
The forward method leverages the get_expert_mask function to obtain vision and language token masks for processing the hidden states.
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.VisionExpertMLP.__init__(config)
¶
Initializes an instance of the VisionExpertMLP class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the VisionExpertMLP class.
|
config |
A configuration object that contains the necessary parameters for the VisionExpertMLP.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.VisionExpertMLP.forward(hidden_states, token_type_ids)
¶
Constructs the expert output by applying vision and language MLPs on the given hidden states.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the VisionExpertMLP class.
|
hidden_states |
A tensor of shape (B, L, D) containing the hidden states. B represents the batch size, L represents the sequence length, and D represents the hidden size.
TYPE:
|
token_type_ids |
A tensor of shape (B, L) containing the token type ids. It identifies whether each token in the sequence belongs to the vision or language modality.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
mindspore.Tensor: A tensor of shape (B, L, D) representing the expert output. The output tensor is forwarded by applying vision MLP on the hidden states of vision tokens and language MLP on the hidden states of language tokens. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.RotaryEmbedding
¶
Bases: Module
The 'RotaryEmbedding' class represents a rotary positional embedding layer in the mindspore.nn framework. This class inherits from the nn.Module class.
ATTRIBUTE | DESCRIPTION |
---|---|
dim |
The dimensionality of the embedding.
TYPE:
|
max_position_embeddings |
The maximum number of positions in the input sequence.
TYPE:
|
base |
The base value used for computing the inverse frequency.
TYPE:
|
inv_freq |
The tensor containing the inverse frequency values.
TYPE:
|
max_seq_len_cached |
The maximum sequence length for which the cos and sin values are cached.
TYPE:
|
cos_cached |
The cached cosine values for the positions.
TYPE:
|
sin_cached |
The cached sine values for the positions.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes a new instance of the 'RotaryEmbedding' class. Args:
Returns:
|
_compute_inv_freq |
Computes the inverse frequency values for the embedding. Returns:
|
_set_cos_sin_cache |
Sets the cosine and sine values cache for the given sequence length and data type. Args:
Returns:
|
forward |
Constructs the rotary embeddings for the given input and sequence length. Args:
Returns:
|
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 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 464 465 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 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.RotaryEmbedding.__init__(dim, max_position_embeddings=2048, base=10000)
¶
Initializes an instance of the RotaryEmbedding class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
TYPE:
|
dim |
The dimensionality of the embeddings.
TYPE:
|
max_position_embeddings |
The maximum number of position embeddings. Default is 2048.
TYPE:
|
base |
The base value used for computing the inverse frequency. Default is 10000.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.RotaryEmbedding.forward(x, seq_len)
¶
This method forwards the rotary embedding for the input sequence.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the RotaryEmbedding class.
TYPE:
|
x |
The input tensor representing the sequence.
|
seq_len |
The length of the input sequence.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value directly. Instead, it updates the internal state of the RotaryEmbedding instance. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.VisionExpertAttention
¶
Bases: Module
This class represents a vision expert attention mechanism used in a neural network model. It is a subclass of nn.Module.
ATTRIBUTE | DESCRIPTION |
---|---|
config |
The configuration object for the attention mechanism.
TYPE:
|
hidden_size |
The size of the hidden state.
TYPE:
|
num_heads |
The number of attention heads.
TYPE:
|
head_dim |
The dimension of each attention head.
TYPE:
|
max_position_embeddings |
The maximum number of position embeddings.
TYPE:
|
rotary_emb |
The rotary embedding layer used for positional encoding.
TYPE:
|
vision_expert_query_key_value |
The dense layer for vision expert query-key-value computation.
TYPE:
|
vision_expert_dense |
The dense layer for vision expert output computation.
TYPE:
|
language_expert_query_key_value |
The dense layer for language expert query-key-value computation.
TYPE:
|
language_expert_dense |
The dense layer for language expert output computation.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the VisionExpertAttention object. |
_swapaxes_for_scores |
Transposes a 3D tensor into a 4D tensor. |
forward |
Constructs the attention mechanism. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
632 633 634 635 636 637 638 639 640 641 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 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.VisionExpertAttention.__init__(config)
¶
Initializes an instance of the VisionExpertAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the class.
TYPE:
|
config |
The configuration object containing various settings for the attention mechanism.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
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 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.VisionExpertAttention.forward(hidden_states, token_type_ids, position_ids, attention_mask=None, past_key_value=None, output_attentions=False, use_cache=False)
¶
Constructs the VisionExpertAttention.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
hidden_states |
The input hidden states. Shape (batch_size, sequence_length, hidden_size).
TYPE:
|
token_type_ids |
The token type ids. Shape (batch_size, sequence_length).
TYPE:
|
position_ids |
The position ids. Shape (batch_size, sequence_length).
TYPE:
|
attention_mask |
The attention mask tensor. Shape (batch_size, sequence_length). Defaults to None.
TYPE:
|
past_key_value |
The past key and value tensors. Defaults to None.
TYPE:
|
output_attentions |
Whether to output attentions. Defaults to False.
TYPE:
|
use_cache |
Whether to use cache. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tuple[mindspore.Tensor, Optional[mindspore.Tensor], Optional[Tuple[mindspore.Tensor]]]: A tuple containing the attention output tensor, |
Optional[Tensor]
|
an optional tensor, and an optional tuple of past key and value tensors. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the shape of the context layer is not (batch_size, num_heads, sequence_length, head_dim). |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
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 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMDecoderLayer
¶
Bases: Module
CogVLMDecoderLayer represents a single layer of the Vision-Language Multimodal Transformer decoder. The layer consists of a vision expert attention module, a vision expert MLP module, and layer normalization modules.
ATTRIBUTE | DESCRIPTION |
---|---|
hidden_size |
The size of the hidden layers in the configuration.
TYPE:
|
self_attn |
The vision expert attention module.
TYPE:
|
mlp |
The vision expert MLP module.
TYPE:
|
input_layernorm |
The layer normalization module for the input.
TYPE:
|
post_attention_layernorm |
The layer normalization module after the attention module.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
forward |
Constructs the decoder layer. |
RETURNS | DESCRIPTION |
---|---|
Tuple[mindspore.Tensor, Optional[Tuple[mindspore.Tensor, mindspore.Tensor]]]: A tuple containing the hidden states of the layer and optionally attention weights and present key value. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMDecoderLayer.__init__(config)
¶
Initialize CogVLMDecoderLayer with given configuration.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of CogVLMDecoderLayer.
TYPE:
|
config |
The configuration object containing the model parameters.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMDecoderLayer.forward(hidden_states, token_type_ids, position_ids, attention_mask=None, past_key_value=None, output_attentions=False, use_cache=False)
¶
CogVLMDecoderLayer.forward method.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CogVLMDecoderLayer class.
|
hidden_states |
The input hidden states tensor.
TYPE:
|
token_type_ids |
The token type ids tensor.
TYPE:
|
position_ids |
The position ids tensor.
TYPE:
|
attention_mask |
An optional tensor for attention mask. Defaults to None.
TYPE:
|
past_key_value |
An optional tuple of past key values. Defaults to None.
TYPE:
|
output_attentions |
A flag to output attentions. Defaults to False.
TYPE:
|
use_cache |
A flag to use cache. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[Tensor, Optional[Tuple[Tensor, Tensor]]]
|
Tuple[mindspore.Tensor, Optional[Tuple[mindspore.Tensor, mindspore.Tensor]]]: A tuple containing the output hidden states tensor and optionally a tuple of present key values. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMPreTrainedModel
¶
Bases: PreTrainedModel
The CogVLMPreTrainedModel
class is a subclass of PreTrainedModel
and represents a pre-trained language model
for cognitive vision and language tasks. This class provides methods for initializing the weights of the
model's neural network cells.
METHOD | DESCRIPTION |
---|---|
`_init_weights |
Initializes the weights of the specified neural network cell.
|
Note
The CogVLMPreTrainedModel
class assumes that the PreTrainedModel
class has been properly implemented and imported.
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMModel
¶
Bases: CogVLMPreTrainedModel
Represents a CogVLM (Cognitive Vision and Language Model) for multimodal learning, combining vision and language information for various NLP and computer vision tasks.
This class inherits from CogVLMPreTrainedModel and implements methods for encoding images and forwarding the model for language and vision processing. It also includes methods for forward pass, getting and setting input embeddings, and preparing attention masks for the decoder.
The CogVLMModel class includes the following methods:
- init: Initializes the CogVLMModel with the provided configuration.
- encode_images: Encodes the input images and returns the image features.
- forward: Constructs the model for language and vision processing and returns the output.
- llm_forward: Performs the forward pass for the CogVLMModel and returns the output.
- get_input_embeddings: Returns the input embeddings for the model.
- set_input_embeddings: Sets the input embeddings for the model.
- _prepare_decoder_attention_mask: Prepares attention masks for the decoder based on the provided inputs.
The CogVLMModel class also includes an init method to initialize the model and handle configuration parameters. Additionally, it inherits methods from the CogVLMPreTrainedModel class.
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMModel.__init__(config)
¶
init(self, config) Initialize the CogVLMModel with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CogVLMModel class.
|
config |
An object containing the configuration parameters for the model, such as pad_token_id, vocab_size, hidden_size, num_hidden_layers, rms_norm_eps, and other relevant settings. It is of type 'config' and is required for initializing the model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMModel.encode_images(images)
¶
Encodes a batch of images into their corresponding image features using the CogVLMModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CogVLMModel class.
TYPE:
|
images |
A list of lists of mindspore.Tensor objects representing the images. Each inner list contains a batch of images. Each image is represented as a mindspore.Tensor object.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: A tensor containing the image features. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMModel.forward(input_ids=None, images=None, token_type_ids=None, attention_mask=None, position_ids=None, past_key_values=None, inputs_embeds=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
take care of image_encode, token_type_ids, position_ids and (attention_mask = None is fine)
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMModel.get_input_embeddings()
¶
This method returns the input embeddings for the CogVLMModel.
PARAMETER | DESCRIPTION |
---|---|
self |
A reference to the current instance of the class CogVLMModel.
|
RETURNS | DESCRIPTION |
---|---|
embed_tokens
|
The method returns the embed_tokens attribute of the CogVLMModel instance. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMModel.llm_forward(input_ids=None, token_type_ids=None, attention_mask=None, position_ids=None, past_key_values=None, inputs_embeds=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
largely copy from llama forward and adapt for cogvlm with token_type_ids
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMModel.set_input_embeddings(value)
¶
Sets the input embeddings for the CogVLMModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CogVLMModel class.
TYPE:
|
value |
The input embeddings to be set.
|
RETURNS | DESCRIPTION |
---|---|
None. |
This method sets the 'embed_tokens' attribute of the CogVLMModel instance to the provided 'value'. The 'embed_tokens' attribute represents the input embeddings used for the model. By setting this attribute, the input embeddings can be customized or updated during runtime.
Note
The 'value' parameter should be compatible with the expected format of the input embeddings. Ensure that the 'value' matches the required shape and data type for the model's input embeddings.
Example
>>> model = CogVLMModel()
>>> embeddings = get_input_embeddings()
>>> model.set_input_embeddings(embeddings)
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMForCausalLM
¶
Bases: CogVLMPreTrainedModel
CogVLMForCausalLM is a class for generating language using a CogVLM (Cognitive Vision Language Model) for causal language modeling. This class inherits from the CogVLMPreTrainedModel and includes methods for forwarding, preparing inputs for generation, updating model keyword arguments for generation, and reordering cache.
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the class with a given configuration. |
get_input_embeddings |
Returns the model's input embeddings. |
set_input_embeddings |
Sets the model's input embeddings to a given value. |
get_output_embeddings |
Returns the model's output embeddings. |
set_output_embeddings |
Sets the model's output embeddings to a given value. |
set_decoder |
Sets the model's decoder to a given value. |
get_decoder |
Returns the model's decoder. |
_prepare_attention_mask_for_generation |
Prepares the attention mask for generation. |
prepare_inputs_for_generation |
Prepares inputs for generation. |
_update_model_kwargs_for_generation |
Updates model keyword arguments for generation. |
_reorder_cache |
Reorders the cache. |
build_conversation_input_ids |
Builds input IDs for a conversation with a given tokenizer, query, history, images, and template version. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMForCausalLM.__init__(config)
¶
Initialize the CogVLMForCausalLM class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CogVLMForCausalLM class.
TYPE:
|
config |
A dictionary containing configuration parameters for the model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
KeyError
|
If the 'config' dictionary does not contain required keys. |
ValueError
|
If the values in the 'config' dictionary are invalid or out of range. |
TypeError
|
If the input parameters are of incorrect types. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMForCausalLM.build_conversation_input_ids(tokenizer, *, query, history=None, images=None, template_version=None)
¶
This method builds conversation input IDs for the CogVLMForCausalLM class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
tokenizer |
The tokenizer used for tokenizing the input. It is required for encoding the input text and images.
TYPE:
|
query |
The query text for the conversation.
TYPE:
|
history |
A list of tuples containing the conversation history, where each tuple represents (user, bot) dialogue turns. Defaults to None.
TYPE:
|
images |
A list of PIL images representing the visual context of the conversation. Defaults to None.
TYPE:
|
template_version |
The version of the conversation template to be used. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the input_ids, token_type_ids, attention_mask, and images. The input_ids are the tokenized input for the conversation, token_type_ids specify the type of each token (language or vision), attention_mask indicates the position of valid tokens, and images represent the processed visual input. |
RAISES | DESCRIPTION |
---|---|
AssertionError
|
If the number of images provided is more than one. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMForCausalLM.forward(input_ids=None, images=None, token_type_ids=None, attention_mask=None, position_ids=None, past_key_values=None, inputs_embeds=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None, labels=None)
¶
Constructs the CogVLMForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the CogVLMForCausalLM class.
|
input_ids |
The input tensor containing sequence tokens. Default is None.
TYPE:
|
images |
The list of image tensors. Default is None.
TYPE:
|
token_type_ids |
The tensor containing token type ids. Default is None.
TYPE:
|
attention_mask |
The tensor containing attention mask. Default is None.
TYPE:
|
position_ids |
The tensor containing position ids. Default is None.
TYPE:
|
past_key_values |
The list of past key values. Default is None.
TYPE:
|
inputs_embeds |
The tensor containing input embeddings. Default is None.
TYPE:
|
use_cache |
Whether to use cache. Default is None.
TYPE:
|
output_attentions |
Whether to output attentions. Default is None.
TYPE:
|
output_hidden_states |
Whether to output hidden states. Default is None.
TYPE:
|
return_dict |
Whether to return a dictionary. Default is None.
TYPE:
|
labels |
The tensor containing labels. Default is None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, CausalLMOutputWithPast]
|
Union[Tuple, CausalLMOutputWithPast]: A tuple or an instance of CausalLMOutputWithPast class containing the following:
|
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMForCausalLM.get_decoder()
¶
Returns the decoder model used for causal language modeling in CogVLMForCausalLM.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CogVLMForCausalLM class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMForCausalLM.get_input_embeddings()
¶
Returns the input embeddings of the CogVLMForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the CogVLMForCausalLM class.
|
RETURNS | DESCRIPTION |
---|---|
None
|
The method retrieves and returns the input embeddings of the model. These embeddings represent the learned representations of the input tokens. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMForCausalLM.get_output_embeddings()
¶
Return the output embeddings from the CogVLMForCausalLM model.
This method takes no additional arguments other than the instance itself.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the CogVLMForCausalLM class.
|
RETURNS | DESCRIPTION |
---|---|
lm_head
|
This method returns the output embeddings of the CogVLMForCausalLM model. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMForCausalLM.prepare_inputs_for_generation(input_ids, token_type_ids, images=None, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs)
¶
Prepare inputs for generation.
This method prepares the inputs for generating text using the CogVLMForCausalLM model. It takes the following parameters:
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CogVLMForCausalLM class.
TYPE:
|
input_ids |
The input tensor containing the token IDs of the input sequence.
TYPE:
|
token_type_ids |
The token type IDs tensor.
TYPE:
|
images |
The tensor containing the image features. Defaults to None.
TYPE:
|
past_key_values |
The tensor containing the past key values for generation. Defaults to None.
TYPE:
|
attention_mask |
The attention mask tensor. Defaults to None.
TYPE:
|
inputs_embeds |
The tensor containing the embedded inputs. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the model inputs. |
RAISES | DESCRIPTION |
---|---|
None
|
This method does not raise any exceptions. |
Note
The input_ids, token_type_ids, and attention_mask tensors should have the same shape and dimensionality. If position_ids are not provided, they are built using the token_type_ids and attention_mask tensors. If past_key_values are provided, the input_ids, token_type_ids, and position_ids tensors are sliced to keep only the last token. The model_inputs dictionary is then forwarded with the relevant tensors.
Example
>>> model = CogVLMForCausalLM()
>>> input_ids = torch.tensor([[1, 2, 3]])
>>> token_type_ids = torch.tensor([[0, 0, 0]])
>>> inputs = model.prepare_inputs_for_generation(input_ids, token_type_ids)
>>> print(inputs)
{'input_ids': tensor([[3]]), 'token_type_ids': tensor([[0]]), 'images': None, 'position_ids': tensor([[2]]), 'past_key_values': None, 'use_cache': None, 'attention_mask': None}
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMForCausalLM.set_decoder(decoder)
¶
Sets the decoder for the CogVLMForCausalLM class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CogVLMForCausalLM class.
TYPE:
|
decoder |
The decoder to be set for the CogVLMForCausalLM instance.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMForCausalLM.set_input_embeddings(value)
¶
Set input embeddings for the CogVLMForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the CogVLMForCausalLM class.
TYPE:
|
value |
The input embeddings to be set for the model. It should be a tensor of shape (vocab_size, embedding_dim).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
This method sets the input embeddings for the CogVLMForCausalLM model. It assigns the input embeddings to the 'embed_tokens' attribute of the model, which is responsible for handling the input embeddings during the model's forward pass.
Note
The input embeddings should be a tensor of shape (vocab_size, embedding_dim), where 'vocab_size' is the size of the vocabulary and 'embedding_dim' is the dimension of the embedding space.
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 |
|
mindnlp.transformers.models.cogvlm.modeling_cogvlm.CogVLMForCausalLM.set_output_embeddings(new_embeddings)
¶
Sets the output embeddings for the CogVLMForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CogVLMForCausalLM class.
TYPE:
|
new_embeddings |
The new embeddings to be set. This parameter can be of any type.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cogvlm/modeling_cogvlm.py
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 |
|
mindnlp.transformers.models.cogvlm.visual.EVA2CLIPModel
¶
Bases: Module
This class represents a model for EVA2CLIP (Embedding Vision and Audio to Clip) task, which combines vision and audio inputs to generate video embeddings. It inherits from nn.Module and contains methods for initializing the model and forwarding the forward pass.
ATTRIBUTE | DESCRIPTION |
---|---|
patch_embedding |
Instance of PatchEmbedding class for extracting image patches.
TYPE:
|
transformer |
Instance of Transformer class for processing image patches.
TYPE:
|
linear_proj |
Instance of GLU class for linear projection.
TYPE:
|
boi |
Beginning of input parameter for the model.
TYPE:
|
eoi |
End of input parameter for the model.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the EVA2CLIPModel with the provided configuration. |
forward |
Constructs the forward pass of the model using the input images. |
Example
>>> model = EVA2CLIPModel(config)
>>> output = model.forward(images)
Source code in mindnlp/transformers/models/cogvlm/visual.py
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 434 435 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 464 465 466 467 468 469 470 471 472 473 474 475 476 |
|
mindnlp.transformers.models.cogvlm.visual.EVA2CLIPModel.__init__(config)
¶
Initializes an instance of the EVA2CLIPModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
A configuration object containing parameters for the model's vision components and hidden size.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/cogvlm/visual.py
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
|
mindnlp.transformers.models.cogvlm.visual.EVA2CLIPModel.forward(images)
¶
Constructs the EVA2CLIP model by processing the input images.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the EVA2CLIPModel class.
|
images |
The input images to be processed. It should be a tensor with dimensions (B, C, H, W), where B represents the batch size, C represents the number of channels, and H and W represent the height and width of the images, respectively.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tensor
|
The processed output tensor. It has dimensions (B, L, D),
TYPE:
|
tensor(B, L, D)
|
where B represents the batch size, L represents the length, and D represents |
tensor(B, L, D)
|
the dimension of the tensor. |
Source code in mindnlp/transformers/models/cogvlm/visual.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
|
mindnlp.transformers.models.cogvlm.visual.TransformerLayer
¶
Bases: Module
transformer layer
Source code in mindnlp/transformers/models/cogvlm/visual.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
|
mindnlp.transformers.models.cogvlm.visual.TransformerLayer.__init__(config)
¶
Initializes a TransformerLayer object with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the TransformerLayer class.
TYPE:
|
config |
The configuration object containing the settings for the TransformerLayer. Expected attributes:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
AttributeError
|
If the required attributes are missing in the config object. |
TypeError
|
If the config object is not of the expected type. |
Source code in mindnlp/transformers/models/cogvlm/visual.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
mindnlp.transformers.models.cogvlm.visual.TransformerLayer.forward(hidden_states)
¶
Constructs the TransformerLayer.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the TransformerLayer class.
TYPE:
|
hidden_states |
The input hidden states to the layer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method modifies the hidden_states in-place. |
Description
This method forwards a TransformerLayer by applying various operations on the input hidden states. It follows the standard Transformer architecture.
The method performs the following steps:
- Apply attention mechanism to the hidden_states using self.attention.
- Apply input layer normalization to the attention output using self.input_layernorm.
- Add the attention output and the input layer normalized attention output.
- Apply multi-layer perceptron (MLP) to the resulting hidden_states using self.mlp.
- Apply post-attention layer normalization to the MLP output using self.post_attention_layernorm.
- Add the hidden_states and the post-attention layer normalized MLP output.
Note that this method modifies the hidden_states in-place and does not return any value.
Source code in mindnlp/transformers/models/cogvlm/visual.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
|