|
14 | 14 | # limitations under the License.
|
15 | 15 | """ OpenAI ImageGPT configuration"""
|
16 | 16 |
|
| 17 | +from collections import OrderedDict |
| 18 | +from typing import TYPE_CHECKING, Any, Mapping, Optional |
| 19 | + |
17 | 20 | from ...configuration_utils import PretrainedConfig
|
| 21 | +from ...onnx import OnnxConfig |
18 | 22 | from ...utils import logging
|
19 | 23 |
|
20 | 24 |
|
| 25 | +if TYPE_CHECKING: |
| 26 | + from ... import FeatureExtractionMixin, TensorType |
| 27 | + |
21 | 28 | logger = logging.get_logger(__name__)
|
22 | 29 |
|
23 | 30 | IMAGEGPT_PRETRAINED_CONFIG_ARCHIVE_MAP = {
|
@@ -140,3 +147,56 @@ def __init__(
|
140 | 147 | self.tie_word_embeddings = tie_word_embeddings
|
141 | 148 |
|
142 | 149 | super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)
|
| 150 | + |
| 151 | + |
| 152 | +class ImageGPTOnnxConfig(OnnxConfig): |
| 153 | + @property |
| 154 | + def inputs(self) -> Mapping[str, Mapping[int, str]]: |
| 155 | + return OrderedDict( |
| 156 | + [ |
| 157 | + ("input_ids", {0: "batch", 1: "sequence"}), |
| 158 | + ] |
| 159 | + ) |
| 160 | + |
| 161 | + def generate_dummy_inputs( |
| 162 | + self, |
| 163 | + preprocessor: "FeatureExtractionMixin", |
| 164 | + batch_size: int = 1, |
| 165 | + seq_length: int = -1, |
| 166 | + is_pair: bool = False, |
| 167 | + framework: Optional["TensorType"] = None, |
| 168 | + num_channels: int = 3, |
| 169 | + image_width: int = 32, |
| 170 | + image_height: int = 32, |
| 171 | + ) -> Mapping[str, Any]: |
| 172 | + """ |
| 173 | + Generate inputs to provide to the ONNX exporter for the specific framework |
| 174 | +
|
| 175 | + Args: |
| 176 | + preprocessor ([`PreTrainedTokenizerBase`] or [`FeatureExtractionMixin`]): |
| 177 | + The preprocessor associated with this model configuration. |
| 178 | + batch_size (`int`, *optional*, defaults to -1): |
| 179 | + The batch size to export the model for (-1 means dynamic axis). |
| 180 | + num_choices (`int`, *optional*, defaults to -1): |
| 181 | + The number of candidate answers provided for multiple choice task (-1 means dynamic axis). |
| 182 | + seq_length (`int`, *optional*, defaults to -1): |
| 183 | + The sequence length to export the model for (-1 means dynamic axis). |
| 184 | + is_pair (`bool`, *optional*, defaults to `False`): |
| 185 | + Indicate if the input is a pair (sentence 1, sentence 2) |
| 186 | + framework (`TensorType`, *optional*, defaults to `None`): |
| 187 | + The framework (PyTorch or TensorFlow) that the tokenizer will generate tensors for. |
| 188 | + num_channels (`int`, *optional*, defaults to 3): |
| 189 | + The number of channels of the generated images. |
| 190 | + image_width (`int`, *optional*, defaults to 40): |
| 191 | + The width of the generated images. |
| 192 | + image_height (`int`, *optional*, defaults to 40): |
| 193 | + The height of the generated images. |
| 194 | +
|
| 195 | + Returns: |
| 196 | + Mapping[str, Tensor] holding the kwargs to provide to the model's forward function |
| 197 | + """ |
| 198 | + |
| 199 | + input_image = self._generate_dummy_images(batch_size, num_channels, image_height, image_width) |
| 200 | + inputs = dict(preprocessor(input_image, framework)) |
| 201 | + |
| 202 | + return inputs |
0 commit comments