gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
1
2
3
4
5
6
7
8
9
10
11
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
/**
 * @license
 * Copyright 2023 Google LLC.
 * 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.
 * =============================================================================
 */
/// <amd-module name="@tensorflow/tfjs-layers/dist/layers/nlp/models/gpt2/gpt2_backbone" />
/**
 *  Base class for Backbone models.
 */
import { serialization } from '@tensorflow/tfjs-core';
import { Embedding } from '../../../embeddings';
import { Backbone } from '../backbone';
export interface GPT2BackboneArgs {
    /**
     * Integer. The size of the token vocabulary.
     */
    vocabularySize: number;
    /**
     * Integer. The number of transformer layers.
     */
    numLayers: number;
    /**
     * Integer. The number of attention heads for each transformer.
     * The hidden size must be divisible by the number of attention heads.
     */
    numHeads: number;
    /**
     * Integer. The size of the transformer encoding and pooler layers.
     */
    hiddenDim: number;
    /**
     * Integer. The output dimension of the first Dense layer in a two-layer
     * feedforward network for each transformer.
     */
    intermediateDim: number;
    /**
     * Float. Dropout probability for the Transformer encoder.
     * Defaults to 0.2.
     */
    dropout?: number;
    /**
     * Integer. The maximum sequence length that this encoder can consume.
     * If `null`, `maxSequenceLength` uses the value from sequence length.
     * This determines the variable shape for positional embeddings.
     * Defaults to 1024.
     */
    maxSequenceLength?: number;
}
/**
 * GPT-2 core network with hyperparameters.
 *
 * This network implements a Transformer-based decoder network,
 * Generative Pretrained Transformer-2 (GPT-2), as described in
 * ["Language Models are Unsupervised Multitask Learners"](https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf).
 * It includes the embedding lookups and transformer layers.
 *
 * The default constructor gives a fully customizable, randomly initialized
 * GPT-2 model with any number of layers, heads, and embedding
 * dimensions. To load preset architectures and weights, use the `fromPreset`
 * constructor.
 *
 * Disclaimer: Pre-trained models are provided on an "as is" basis, without
 * warranties or conditions of any kind. The underlying model is provided by a
 * third party and subject to a separate license, available
 * [here](https://github.com/openai/gpt-2).
 *
 *
 * Example usage:
 * ```js
 * const tokenIds = tf.ones([1, 12]), dtype="int32");
 * const paddingMask = tf.tensor(
 *  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]], 'int32');
 *
 * # Pretrained GPT-2 decoder.
 * model = GPT2Backbone.fromPreset("gpt2_base_en");
 * model.apply(inputData, {paddingMask});
 *
 * # Randomly initialized GPT-2 decoder with custom config.
 * model = kerasNlp.models.GPT2Backbone({
 *     vocabularySize: 50257,
 *     numLayers: 12,
 *     numHeads: 12,
 *     hiddenDim: 768,
 *     intermediateDim: 3072,
 *     maxSequenceLength: 1024,
 * });
 * model.apply(inputData, {paddingMask});
 * ```
 */
export declare class GPT2Backbone extends Backbone {
    /** @nocollapse */
    static className: string;
    private vocabularySize;
    private numLayers;
    private numHeads;
    private hiddenDim;
    private intermediateDim;
    private dropout;
    private maxSequenceLength;
    constructor(args: GPT2BackboneArgs);
    getConfig(): serialization.ConfigDict;
    get tokenEmbedding(): Embedding;
}