codingutf8 2022 the realm s and the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license realm model configuration from configurationutils import pretrainedconfig from utils import logging logger logging getloggername realmpretrainedconfigarchivemap googlerealmccnewspretrainedembedder https huggingface cogooglerealmccnewspretrainedembedderresolvemainconfig json googlerealmccnewspretrainedencoder https huggingface cogooglerealmccnewspretrainedencoderresolvemainconfig json googlerealmccnewspretrainedscorer https huggingface cogooglerealmccnewspretrainedscorerresolvemainconfig json googlerealmccnewspretrainedopenqa https huggingface cogooglerealmccnewspretrainedopenqaaresolvemainconfig json googlerealmorqanqopenqa https huggingface cogooglerealmorqanqopenqaresolvemainconfig json googlerealmorqanqreader https huggingface cogooglerealmorqanqreaderresolvemainconfig json googlerealmorqawqopenqa https huggingface cogooglerealmorqawqopenqaresolvemainconfig json googlerealmorqawqreader https huggingface cogooglerealmorqawqreaderresolvemainconfig json see all realm models at https huggingface comodels filterrealm class realmconfigpretrainedconfig r this is the configuration class to store the configuration of 1 realmembedder 2 realmscorer 3 realmknowledgeaugencoder 4 realmretriever 5 realmreader 6 realmforopenqa it is used to instantiate an realm 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 realm googlerealmccnewspretrainedembedderhttps huggingface cogooglerealmccnewspretrainedembedder architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args vocabsize int optional defaults to 30522 vocabulary size of the realm model defines the number of different tokens that can be represented by the inputsids passed when calling realmembedder realmscorer realmknowledgeaugencoder or realmreader hiddensize int optional defaults to 768 dimension of the encoder layers and the pooler layer retrieverprojsize int optional defaults to 128 dimension of the retrieverembedder projection numhiddenlayers int optional defaults to 12 number of hidden layers in the transformer encoder numattentionheads int optional defaults to 12 number of attention heads for each attention layer in the transformer encoder numcandidates int optional defaults to 8 number of candidates inputted to the realmscorer or realmknowledgeaugencoder intermediatesize int optional defaults to 3072 dimension of the intermediate i e feedforward layer in the transformer encoder hiddenact str or function optional defaults to gelunew the nonlinear activation function function or string in the encoder and pooler if string gelu relu selu and gelunew are supported hiddendropoutprob float optional defaults to 0 1 the dropout probabilitiy for all fully connected layers in the embeddings encoder and pooler attentionprobsdropoutprob float optional defaults to 0 1 the dropout ratio for the attention probabilities maxpositionembeddings int optional defaults to 512 the maximum sequence length that this model might ever be used with typically set this to something large just in case e g 512 or 1024 or 2048 typevocabsize int optional defaults to 2 the vocabulary size of the tokentypeids passed when calling realmembedder realmscorer realmknowledgeaugencoder or realmreader initializerrange float optional defaults to 0 02 the standard deviation of the truncatednormalinitializer for initializing all weight matrices layernormeps float optional defaults to 1e12 the epsilon used by the layer normalization layers spanhiddensize int optional defaults to 256 dimension of the reader s spans maxspanwidth int optional defaults to 10 max span width of the reader readerlayernormeps float optional defaults to 1e3 the epsilon used by the reader s layer normalization layers readerbeamsize int optional defaults to 5 beam size of the reader readerseqlen int optional defaults to 28832 maximum sequence length of the reader numblockrecords int optional defaults to 13353718 number of block records searcherbeamsize int optional defaults to 5000 beam size of the searcher note that when eval mode is enabled searcherbeamsize will be the same as readerbeamsize example python from transformers import realmconfig realmembedder initializing a realm realmccnewspretrained style configuration configuration realmconfig initializing a model with random weights from the googlerealmccnewspretrainedembedder style configuration model realmembedderconfiguration accessing the model configuration configuration model config modeltype realm def init self vocabsize30522 hiddensize768 retrieverprojsize128 numhiddenlayers12 numattentionheads12 numcandidates8 intermediatesize3072 hiddenactgelunew hiddendropoutprob0 1 attentionprobsdropoutprob0 1 maxpositionembeddings512 typevocabsize2 initializerrange0 02 layernormeps1e12 spanhiddensize256 maxspanwidth10 readerlayernormeps1e3 readerbeamsize5 readerseqlen320 288 32 numblockrecords13353718 searcherbeamsize5000 padtokenid1 bostokenid0 eostokenid2 kwargs super initpadtokenidpadtokenid bostokenidbostokenid eostokenideostokenid kwargs common config self vocabsize vocabsize self maxpositionembeddings maxpositionembeddings self hiddensize hiddensize self retrieverprojsize retrieverprojsize self numhiddenlayers numhiddenlayers self numattentionheads numattentionheads self numcandidates numcandidates self intermediatesize intermediatesize self hiddenact hiddenact self hiddendropoutprob hiddendropoutprob self attentionprobsdropoutprob attentionprobsdropoutprob self initializerrange initializerrange self typevocabsize typevocabsize self layernormeps layernormeps reader config self spanhiddensize spanhiddensize self maxspanwidth maxspanwidth self readerlayernormeps readerlayernormeps self readerbeamsize readerbeamsize self readerseqlen readerseqlen retrieval config self numblockrecords numblockrecords self searcherbeamsize searcherbeamsize coding utf 8 2022 the realm s and the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license realm model configuration see all realm models at https huggingface co models filter realm this is the configuration class to store the configuration of 1 realmembedder 2 realmscorer 3 realmknowledgeaugencoder 4 realmretriever 5 realmreader 6 realmforopenqa it is used to instantiate an realm 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 realm google realm cc news pretrained embedder https huggingface co google realm cc news pretrained embedder architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args vocab_size int optional defaults to 30522 vocabulary size of the realm model defines the number of different tokens that can be represented by the inputs_ids passed when calling realmembedder realmscorer realmknowledgeaugencoder or realmreader hidden_size int optional defaults to 768 dimension of the encoder layers and the pooler layer retriever_proj_size int optional defaults to 128 dimension of the retriever embedder projection num_hidden_layers int optional defaults to 12 number of hidden layers in the transformer encoder num_attention_heads int optional defaults to 12 number of attention heads for each attention layer in the transformer encoder num_candidates int optional defaults to 8 number of candidates inputted to the realmscorer or realmknowledgeaugencoder intermediate_size int optional defaults to 3072 dimension of the intermediate i e feed forward layer in the transformer encoder hidden_act str or function optional defaults to gelu_new the non linear activation function function or string in the encoder and pooler if string gelu relu selu and gelu_new are supported hidden_dropout_prob float optional defaults to 0 1 the dropout probabilitiy for all fully connected layers in the embeddings encoder and pooler attention_probs_dropout_prob float optional defaults to 0 1 the dropout ratio for the attention probabilities max_position_embeddings int optional defaults to 512 the maximum sequence length that this model might ever be used with typically set this to something large just in case e g 512 or 1024 or 2048 type_vocab_size int optional defaults to 2 the vocabulary size of the token_type_ids passed when calling realmembedder realmscorer realmknowledgeaugencoder or realmreader 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 12 the epsilon used by the layer normalization layers span_hidden_size int optional defaults to 256 dimension of the reader s spans max_span_width int optional defaults to 10 max span width of the reader reader_layer_norm_eps float optional defaults to 1e 3 the epsilon used by the reader s layer normalization layers reader_beam_size int optional defaults to 5 beam size of the reader reader_seq_len int optional defaults to 288 32 maximum sequence length of the reader num_block_records int optional defaults to 13353718 number of block records searcher_beam_size int optional defaults to 5000 beam size of the searcher note that when eval mode is enabled searcher_beam_size will be the same as reader_beam_size example python from transformers import realmconfig realmembedder initializing a realm realm cc news pretrained style configuration configuration realmconfig initializing a model with random weights from the google realm cc news pretrained embedder style configuration model realmembedder configuration accessing the model configuration configuration model config 288 32 common config reader config retrieval config
from ...configuration_utils import PretrainedConfig
from ...utils import logging


logger = logging.get_logger(__name__)

REALM_PRETRAINED_CONFIG_ARCHIVE_MAP = {
    "google/realm-cc-news-pretrained-embedder": (
        "https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json"
    ),
    "google/realm-cc-news-pretrained-encoder": (
        "https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json"
    ),
    "google/realm-cc-news-pretrained-scorer": (
        "https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json"
    ),
    "google/realm-cc-news-pretrained-openqa": (
        "https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json"
    ),
    "google/realm-orqa-nq-openqa": "https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json",
    "google/realm-orqa-nq-reader": "https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json",
    "google/realm-orqa-wq-openqa": "https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json",
    "google/realm-orqa-wq-reader": "https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json",
    
}


class RealmConfig(PretrainedConfig):
    r

    model_type = "realm"

    def __init__(
        self,
        vocab_size=30522,
        hidden_size=768,
        retriever_proj_size=128,
        num_hidden_layers=12,
        num_attention_heads=12,
        num_candidates=8,
        intermediate_size=3072,
        hidden_act="gelu_new",
        hidden_dropout_prob=0.1,
        attention_probs_dropout_prob=0.1,
        max_position_embeddings=512,
        type_vocab_size=2,
        initializer_range=0.02,
        layer_norm_eps=1e-12,
        span_hidden_size=256,
        max_span_width=10,
        reader_layer_norm_eps=1e-3,
        reader_beam_size=5,
        reader_seq_len=320,  
        num_block_records=13353718,
        searcher_beam_size=5000,
        pad_token_id=1,
        bos_token_id=0,
        eos_token_id=2,
        **kwargs,
    ):
        super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)

        
        self.vocab_size = vocab_size
        self.max_position_embeddings = max_position_embeddings
        self.hidden_size = hidden_size
        self.retriever_proj_size = retriever_proj_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.num_candidates = num_candidates
        self.intermediate_size = intermediate_size
        self.hidden_act = hidden_act
        self.hidden_dropout_prob = hidden_dropout_prob
        self.attention_probs_dropout_prob = attention_probs_dropout_prob
        self.initializer_range = initializer_range
        self.type_vocab_size = type_vocab_size
        self.layer_norm_eps = layer_norm_eps

        
        self.span_hidden_size = span_hidden_size
        self.max_span_width = max_span_width
        self.reader_layer_norm_eps = reader_layer_norm_eps
        self.reader_beam_size = reader_beam_size
        self.reader_seq_len = reader_seq_len

        
        self.num_block_records = num_block_records
        self.searcher_beam_size = searcher_beam_size