GPT NeoX Japanese
Last updated
Last updated
We introduce GPT-NeoX-Japanese, which is an autoregressive language model for Japanese, trained on top of . Japanese is a unique language with its large vocabulary and a combination of hiragana, katakana, and kanji writing scripts. To address this distinct structure of the Japanese language, we use a . We are very grateful to tanreinama for open-sourcing this incredibly helpful tokenizer. Following the recommendations from Googleโs research on , we have removed bias parameters from transformer blocks, achieving better model performance. Please refer in detail.
Development of the model was led by , , , and from . For more information on this model-building activity, please refer .
The generate()
method can be used to generate text using GPT NeoX Japanese model.
Copied
( vocab_size = 32000hidden_size = 2560num_hidden_layers = 32num_attention_heads = 32intermediate_multiple_size = 4hidden_act = 'gelu'rotary_pct = 1.0rotary_emb_base = 10000max_position_embeddings = 2048initializer_range = 0.02layer_norm_eps = 1e-05use_cache = Truebos_token_id = 31996eos_token_id = 31999attention_dropout = 0.1hidden_dropout = 0.0**kwargs )
Parameters
vocab_size (int
, optional, defaults to 32000) โ Vocabulary size of the GPTNeoXJapanese model. Defines the number of different tokens that can be represented by the inputs_ids
passed when calling GPTNeoXJapanese
.
hidden_size (int
, optional, defaults to 2560) โ Dimension of the encoder layers and the pooler layer.
num_hidden_layers (int
, optional, defaults to 32) โ Number of hidden layers in the Transformer encoder.
num_attention_heads (int
, optional, defaults to 32) โ Number of attention heads for each attention layer in the Transformer encoder.
intermediate_multiple_size (int
, optional, defaults to 4) โ Dimension of the โintermediateโ layer in the Transformer encoder is calculated by hidden_size * intermediate_multiple_size.
hidden_act (str
or function
, optional, defaults to "gelu"
) โ The non-linear activation function (function or string) in the encoder and pooler.
rotary_pct (float
, optional, defaults to 1.00) โ percentage of hidden dimensions to allocate to rotary embeddings
rotary_emb_base (int
, optional, defaults to 10000) โ base for computing rotary embeddings frequency
max_position_embeddings (int
, optional, defaults to 2048) โ The maximum sequence length that this model might ever be used with.
initializer_range (float
, optional, defaults to 0.02) โ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
layer_norm_eps (float
, optional, defaults to 1e-5) โ The epsilon used by the layer normalization layers.
use_cache (bool
, optional, defaults to True
) โ Whether or not the model should return the last key/values attentions (not used by all models). Only relevant if config.is_decoder=True
.
attention_dropout (float
, optional, defaults to 0.1) โ The dropout ratio for the attention.
hidden_dropout (float
, optional, defaults to 0.0) โ The dropout ratio for the hidden layer. Example โ
Copied
( vocab_fileemoji_fileunk_token = '<|endoftext|>'pad_token = '<|endoftext|>'bos_token = '<|startoftext|>'eos_token = '<|endoftext|>'do_clean_text = False**kwargs )
Parameters
vocab_file (str
) โ File containing the vocabulary.
emoji_file (str
) โ File containing the emoji.
unk_token (str
, optional, defaults to "<|endoftext|>"
) โ The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this token instead.
pad_token (str
, optional, defaults to "<|endoftext|>"
) โ The token used for padding
bos_token (str
, optional, defaults to "<|startoftext|>"
) โ The beginning of sequence token.
eos_token (str
, optional, defaults to "<|endoftext|>"
) โ The end of sequence token.
do_clean_text (bool
, optional, defaults to False
) โ Whether or not to clean text for URL, EMAIL, TEL, Japanese DATE and Japanese PRICE.
Subword-by-subword segmentation, which is intermediate between byte strings and morphological analysis.
BPEs are created for each Kanji, Hiragana, and Katakana character, and there are no BPEs that cross character types, such as Kanji + Hiragana or Hiragana + Katakana.
All-byte encoding that does not require <unk>.
Independent of UTF codes such as 2-byte and 3-byte characters
Conversion of heterographs to the same token_id
Emoji and Emoticon are grouped into 12 types as special tags.
Example:
Copied
convert_tokens_to_string
( tokens )
Converts a sequence of tokens (string) in a single string.
( config )
Parameters
forward
Parameters
input_ids (torch.LongTensor
of shape (batch_size, sequence_length)
) โ Indices of input sequence tokens in the vocabulary.
attention_mask (torch.FloatTensor
of shape (batch_size, sequence_length)
, optional) โ Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]
:
1 for tokens that are not masked,
0 for tokens that are masked.
token_type_ids (torch.LongTensor
of shape (batch_size, sequence_length)
, optional) โ Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]
:
0 corresponds to a sentence A token,
1 corresponds to a sentence B token.
position_ids (torch.LongTensor
of shape (batch_size, sequence_length)
, optional) โ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, config.max_position_embeddings - 1]
.
head_mask (torch.FloatTensor
of shape (num_heads,)
or (num_layers, num_heads)
, optional) โ Mask to nullify selected heads of the self-attention modules. Mask values selected in [0, 1]
:
1 indicates the head is not masked,
0 indicates the head is masked.
inputs_embeds (torch.FloatTensor
of shape (batch_size, sequence_length, hidden_size)
, optional) โ Optionally, instead of passing input_ids
you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert input_ids indices into associated vectors than the modelโs internal embedding lookup matrix.
output_attentions (bool
, optional) โ Whether or not to return the attentions tensors of all attention layers. See attentions
under returned tensors for more detail.
output_hidden_states (bool
, optional) โ Whether or not to return the hidden states of all layers. See hidden_states
under returned tensors for more detail.
past_key_values (tuple(tuple(torch.FloatTensor))
of length config.n_layers
with each tuple having 4 tensors of shape (batch_size, num_heads, sequence_length - 1, embed_size_per_head)
) โ Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If past_key_values
are used, the user can optionally input only the last decoder_input_ids
(those that donโt have their past key value states given to this model) of shape (batch_size, 1)
instead of all decoder_input_ids
of shape (batch_size, sequence_length)
.
use_cache (bool
, optional) โ If set to True
, past_key_values
key value states are returned and can be used to speed up decoding (see past_key_values
).
Returns
last_hidden_state (torch.FloatTensor
of shape (batch_size, sequence_length, hidden_size)
) โ Sequence of hidden-states at the output of the last layer of the model.
If past_key_values
is used only the last hidden-state of the sequences of shape (batch_size, 1, hidden_size)
is output.
past_key_values (tuple(tuple(torch.FloatTensor))
, optional, returned when use_cache=True
is passed or when config.use_cache=True
) โ Tuple of tuple(torch.FloatTensor)
of length config.n_layers
, with each tuple having 2 tensors of shape (batch_size, num_heads, sequence_length, embed_size_per_head)
) and optionally if config.is_encoder_decoder=True
2 additional tensors of shape (batch_size, num_heads, encoder_sequence_length, embed_size_per_head)
.
Contains pre-computed hidden-states (key and values in the self-attention blocks and optionally if config.is_encoder_decoder=True
in the cross-attention blocks) that can be used (see past_key_values
input) to speed up sequential decoding.
hidden_states (tuple(torch.FloatTensor)
, optional, returned when output_hidden_states=True
is passed or when config.output_hidden_states=True
) โ Tuple of torch.FloatTensor
(one for the output of the embeddings, if the model has an embedding layer, + one for the output of each layer) of shape (batch_size, sequence_length, hidden_size)
.
Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
attentions (tuple(torch.FloatTensor)
, optional, returned when output_attentions=True
is passed or when config.output_attentions=True
) โ Tuple of torch.FloatTensor
(one for each layer) of shape (batch_size, num_heads, sequence_length, sequence_length)
.
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
Although the recipe for forward pass needs to be defined within this function, one should call the Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them.
Example:
Copied
( config )
Parameters
forward
Parameters
input_ids (torch.LongTensor
of shape (batch_size, sequence_length)
) โ Indices of input sequence tokens in the vocabulary.
attention_mask (torch.FloatTensor
of shape (batch_size, sequence_length)
, optional) โ Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]
:
1 for tokens that are not masked,
0 for tokens that are masked.
token_type_ids (torch.LongTensor
of shape (batch_size, sequence_length)
, optional) โ Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]
:
0 corresponds to a sentence A token,
1 corresponds to a sentence B token.
position_ids (torch.LongTensor
of shape (batch_size, sequence_length)
, optional) โ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, config.max_position_embeddings - 1]
.
head_mask (torch.FloatTensor
of shape (num_heads,)
or (num_layers, num_heads)
, optional) โ Mask to nullify selected heads of the self-attention modules. Mask values selected in [0, 1]
:
1 indicates the head is not masked,
0 indicates the head is masked.
inputs_embeds (torch.FloatTensor
of shape (batch_size, sequence_length, hidden_size)
, optional) โ Optionally, instead of passing input_ids
you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert input_ids indices into associated vectors than the modelโs internal embedding lookup matrix.
output_attentions (bool
, optional) โ Whether or not to return the attentions tensors of all attention layers. See attentions
under returned tensors for more detail.
output_hidden_states (bool
, optional) โ Whether or not to return the hidden states of all layers. See hidden_states
under returned tensors for more detail.
past_key_values (tuple(tuple(torch.FloatTensor))
, optional, returned when use_cache=True
is passed or when config.use_cache=True
) โ Tuple of tuple(torch.FloatTensor)
of length config.n_layers
, with each tuple having 2 tensors of shape (batch_size, num_heads, sequence_length, embed_size_per_head)
) and 2 additional tensors of shape (batch_size, num_heads, encoder_sequence_length, embed_size_per_head)
. The two additional tensors are only required when the model is used as a decoder in a Sequence to Sequence model.
Contains pre-computed hidden-states (key and values in the self-attention blocks that can be used (see past_key_values
input) to speed up sequential decoding.
If past_key_values
are used, the user can optionally input only the last decoder_input_ids
(those that donโt have their past key value states given to this model) of shape (batch_size, 1)
instead of all decoder_input_ids
of shape (batch_size, sequence_length)
.
labels (torch.LongTensor
of shape (batch_size, sequence_length)
, optional) โ Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in [-100, 0, ..., config.vocab_size]
(see input_ids
docstring) Tokens with indices set to -100
are ignored (masked), the loss is only computed for the tokens with labels n [0, ..., config.vocab_size]
.
use_cache (bool
, optional) โ If set to True
, past_key_values
key value states are returned and can be used to speed up decoding (see past_key_values
).
Returns
loss (torch.FloatTensor
of shape (1,)
, optional, returned when labels
is provided) โ Language modeling loss (for next-token prediction).
logits (torch.FloatTensor
of shape (batch_size, sequence_length, config.vocab_size)
) โ Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
past_key_values (tuple(tuple(torch.FloatTensor))
, optional, returned when use_cache=True
is passed or when config.use_cache=True
) โ Tuple of tuple(torch.FloatTensor)
of length config.n_layers
, with each tuple having 2 tensors of shape (batch_size, num_heads, sequence_length, embed_size_per_head)
)
Contains pre-computed hidden-states (key and values in the self-attention blocks) that can be used (see past_key_values
input) to speed up sequential decoding.
hidden_states (tuple(torch.FloatTensor)
, optional, returned when output_hidden_states=True
is passed or when config.output_hidden_states=True
) โ Tuple of torch.FloatTensor
(one for the output of the embeddings, if the model has an embedding layer, + one for the output of each layer) of shape (batch_size, sequence_length, hidden_size)
.
Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
attentions (tuple(torch.FloatTensor)
, optional, returned when output_attentions=True
is passed or when config.output_attentions=True
) โ Tuple of torch.FloatTensor
(one for each layer) of shape (batch_size, num_heads, sequence_length, sequence_length)
.
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
Although the recipe for forward pass needs to be defined within this function, one should call the Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them.
Example:
Copied
This is the configuration class to store the configuration of a GPTNeoXModelJapanese
. It is used to instantiate a GPTNeoX 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 GPTNeoXJapanese architecture.
Configuration objects inherit from and can be used to control the model outputs. Read the documentation from for more information. Default configs is set as 2.7B model
This tokenizer inherits from and is based on Japanese special Sub-Word-Encoding that is used in this repository (). Check the repository for details. Japanese has a relatively large vocabulary and there is no separation between words. Furthermore, the language is a combination of hiragana, katakana, and kanji, and variants such as โ1โ and โโ โ are often used. In order to cope with these, this tokenizer has the following features
config () โ Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the method to load the model weights.
The bare GPTNeoXJapanese Model transformer outputting raw hidden-states without any specific head on top. This model is a PyTorch sub-class. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
( input_ids: typing.Optional[torch.LongTensor] = Noneattention_mask: typing.Optional[torch.FloatTensor] = Nonehead_mask: typing.Optional[torch.FloatTensor] = Noneinputs_embeds: typing.Optional[torch.FloatTensor] = Nonepast_key_values: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = Noneuse_cache: typing.Optional[bool] = Noneoutput_attentions: typing.Optional[bool] = Noneoutput_hidden_states: typing.Optional[bool] = Nonereturn_dict: typing.Optional[bool] = None ) โ or tuple(torch.FloatTensor)
Indices can be obtained using .
return_dict (bool
, optional) โ Whether or not to return a instead of a plain tuple.
or tuple(torch.FloatTensor)
A or a tuple of torch.FloatTensor
(if return_dict=False
is passed or when config.return_dict=False
) comprising various elements depending on the configuration () and inputs.
The forward method, overrides the __call__
special method.
config () โ Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the method to load the model weights.
GPTNeoXJapanese Model with a language modeling
head on top for Classifier Model fine-tuning. This model is a PyTorch sub-class. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
( input_ids: typing.Optional[torch.LongTensor] = Noneattention_mask: typing.Optional[torch.FloatTensor] = Noneinputs_embeds: typing.Optional[torch.FloatTensor] = Nonehead_mask: typing.Optional[torch.FloatTensor] = Nonepast_key_values: typing.Optional[typing.Tuple[typing.Tuple[torch.FloatTensor]]] = Nonelabels: typing.Optional[torch.LongTensor] = Noneuse_cache: typing.Optional[bool] = Noneoutput_attentions: typing.Optional[bool] = Noneoutput_hidden_states: typing.Optional[bool] = Nonereturn_dict: typing.Optional[bool] = None ) โ or tuple(torch.FloatTensor)
Indices can be obtained using .
return_dict (bool
, optional) โ Whether or not to return a instead of a plain tuple.
or tuple(torch.FloatTensor)
A or a tuple of torch.FloatTensor
(if return_dict=False
is passed or when config.return_dict=False
) comprising various elements depending on the configuration () and inputs.
The forward method, overrides the __call__
special method.