microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
docs/custom_ops.md
1592lines · modeblame
f12f42a0Nat Kershaw (MSFT)4 years ago | 1 | # Operators |
b3a300d7Xavier Dupré5 years ago | 2 | |
| 3 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 4 | ## Natural language operators |
b3a300d7Xavier Dupré5 years ago | 5 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 6 | ### BertTokenizer |
b3a300d7Xavier Dupré5 years ago | 7 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 8 | <details> |
| 9 | <summary>BertTokenizer details</summary> | |
b3a300d7Xavier Dupré5 years ago | 10 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 11 | BertTokenizer replicates `encode_plus` function of [BertTokenizer (huggingface version )](https://huggingface.co/transformers/_modules/transformers/models/bert/tokenization_bert.html#BertTokenizer). |
b3a300d7Xavier Dupré5 years ago | 12 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 13 | #### Inputs |
b3a300d7Xavier Dupré5 years ago | 14 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 15 | ***text: tensor(string)*** The string tensor for tokenization |
b3a300d7Xavier Dupré5 years ago | 16 | |
| 17 | #### Attributes | |
| 18 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 19 | ***vocab_file: string*** |
b3a300d7Xavier Dupré5 years ago | 20 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 21 | The content of vocab which has same with huggingface. |
b3a300d7Xavier Dupré5 years ago | 22 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 23 | ***do_lower_case: int64_t*** (default is 1, 1 represents True, 0 represents False) |
b3a300d7Xavier Dupré5 years ago | 24 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 25 | Whether or not to lowercase the input when tokenizing. |
b3a300d7Xavier Dupré5 years ago | 26 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 27 | ***do_basic_tokenize: int64_t*** (default is 1, 1 represents True, 0 represents False) |
b3a300d7Xavier Dupré5 years ago | 28 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 29 | Whether or not to do basic tokenization before WordPiece. |
b3a300d7Xavier Dupré5 years ago | 30 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 31 | ***unk_token: string*** |
b3a300d7Xavier Dupré5 years ago | 32 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 33 | The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this |
| 34 | token instead. | |
b3a300d7Xavier Dupré5 years ago | 35 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 36 | ***sep_token: string*** |
b3a300d7Xavier Dupré5 years ago | 37 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 38 | The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for |
| 39 | sequence classification or for a text and a question for question answering. It is also used as the last | |
| 40 | token of a sequence built with special tokens. | |
b3a300d7Xavier Dupré5 years ago | 41 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 42 | ***pad_token: string*** |
b3a300d7Xavier Dupré5 years ago | 43 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 44 | The token used for padding, for example when batching sequences of different lengths. |
4290400eMojimi4 years ago | 45 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 46 | ***cls_token: string*** |
4290400eMojimi4 years ago | 47 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 48 | The classifier token which is used when doing sequence classification (classification of the whole sequence instead of per-token classification). It is the first token of the sequence when built with special tokens. |
4290400eMojimi4 years ago | 49 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 50 | ***mask_token: string*** |
4290400eMojimi4 years ago | 51 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 52 | The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict. |
4290400eMojimi4 years ago | 53 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 54 | ***tokenize_chinese_chars: int64_t*** (default is 1, 1 represents True, 0 represents False) |
4290400eMojimi4 years ago | 55 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 56 | Whether or not to tokenize Chinese characters. |
4290400eMojimi4 years ago | 57 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 58 | ***strip_accents: int64_t*** (default is 1, 1 represents True, 0 represents False) |
4290400eMojimi4 years ago | 59 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 60 | Whether or not to strip all accents. If this option is not specified, then it will be determined by the |
| 61 | value for :obj:`lowercase` (as in the original BERT). | |
4290400eMojimi4 years ago | 62 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 63 | ***tokenize_punctuation: int64_t*** (default is 0, 1 represents True, 0 represents False) |
4290400eMojimi4 years ago | 64 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 65 | Splits punctuation on a piece of text. |
4290400eMojimi4 years ago | 66 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 67 | ***remove_control_chars: int64_t*** (default is 0, 1 represents True, 0 represents False) |
4290400eMojimi4 years ago | 68 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 69 | Remove control chars(such as NUL, BEL) in the text. |
4290400eMojimi4 years ago | 70 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 71 | ***truncation_strategy_name: string*** |
4290400eMojimi4 years ago | 72 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 73 | The name of truncation strategy, it could be `longest_first`, `only_first`, `only_second`, `longest_from_back`. |
4290400eMojimi4 years ago | 74 | |
| 75 | #### Outputs | |
| 76 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 77 | ***input_ids: tensor(int64_t)*** |
4290400eMojimi4 years ago | 78 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 79 | List of token ids. |
4290400eMojimi4 years ago | 80 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 81 | ***token_type_ids: tensor(64_t)*** |
4290400eMojimi4 years ago | 82 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 83 | List of token type ids |
| 84 | | |
| 85 | ***attention_mask: tensor(64_t)*** | |
| 86 | | |
| 87 | List of indices specifying which tokens should b | |
| 88 | e attended to by the model | |
| 89 | | |
| 90 | | |
| 91 | #### Examples | |
4290400eMojimi4 years ago | 92 | |
| 93 | ```python | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 94 | import transformers |
| 95 | | |
2c3d6f79Wenbing Li1 years ago | 96 | bert_cased_tokenizer = transformers.BertTokenizer.from_pretrained('google-bert/bert-base-cased') |
4290400eMojimi4 years ago | 97 | |
| 98 | node = onnx.helper.make_node( | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 99 | 'BertTokenizer', |
| 100 | inputs=['text'], | |
| 101 | outputs=['tokens'], | |
4290400eMojimi4 years ago | 102 | ) |
| 103 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 104 | text = "Hello world louder" |
280ec289Edward Chen3 years ago | 105 | inputs = np.array([text], dtype=object), |
4290400eMojimi4 years ago | 106 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 107 | bert_tokenize_result = bert_cased_tokenizer.tokenize(text) |
| 108 | | |
| 109 | input_ids = np.array(bert_tokenize_result[0]) | |
| 110 | token_type_ids = np.array(bert_tokenize_result[1]) | |
| 111 | attention_mask = np.array(bert_tokenize_result[2]) | |
4290400eMojimi4 years ago | 112 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 113 | expect(node, inputs=[inputs], |
| 114 | outputs=[input_ids, token_type_ids, attention_mask], name='test_bert_tokenizer') | |
| 115 | ``` | |
4290400eMojimi4 years ago | 116 | </details> |
| 117 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 118 | ### BertTokenizerDecoder |
4290400eMojimi4 years ago | 119 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 120 | <details> |
| 121 | <summary>BertTokenizerDecoder details</summary> | |
4bc5c962Xavier Dupré5 years ago | 122 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 123 | BertTokenizerDecoder replicates `decode` function of [BertTokenizer (huggingface version )](https://huggingface.co/transformers/_modules/transformers/models/bert/tokenization_bert.html#BertTokenizer). |
4bc5c962Xavier Dupré5 years ago | 124 | |
| 125 | #### Inputs | |
| 126 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 127 | ***token_ids: tensor(int64)*** |
4bc5c962Xavier Dupré5 years ago | 128 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 129 | List of tokenized input ids. |
4bc5c962Xavier Dupré5 years ago | 130 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 131 | ***indices: tensor(int64)*** |
4bc5c962Xavier Dupré5 years ago | 132 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 133 | List of `[start_position, end_position]` to indicate what segments of input ids should be decoded. This input only enabled when attribute `use_indices`=1. |
4bc5c962Xavier Dupré5 years ago | 134 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 135 | Usually, it is used to decode the slot in the text. |
4bc5c962Xavier Dupré5 years ago | 136 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 137 | #### Attributes |
4bc5c962Xavier Dupré5 years ago | 138 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 139 | ***vocab_file: string*** |
4bc5c962Xavier Dupré5 years ago | 140 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 141 | The content of vocab which has same with huggingface. |
4bc5c962Xavier Dupré5 years ago | 142 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 143 | ***unk_token: string*** |
4bc5c962Xavier Dupré5 years ago | 144 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 145 | The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this |
| 146 | token instead. | |
4bc5c962Xavier Dupré5 years ago | 147 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 148 | ***sep_token: string*** |
4bc5c962Xavier Dupré5 years ago | 149 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 150 | The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for |
| 151 | sequence classification or for a text and a question for question answering. It is also used as the last | |
| 152 | token of a sequence built with special tokens. | |
4bc5c962Xavier Dupré5 years ago | 153 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 154 | ***pad_token: string*** |
4bc5c962Xavier Dupré5 years ago | 155 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 156 | The token used for padding, for example when batching sequences of different lengths. |
4bc5c962Xavier Dupré5 years ago | 157 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 158 | ***cls_token: string*** |
4bc5c962Xavier Dupré5 years ago | 159 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 160 | The classifier token which is used when doing sequence classification (classification of the whole sequence instead of per-token classification). It is the first token of the sequence when built with special tokens. |
4bc5c962Xavier Dupré5 years ago | 161 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 162 | ***mask_token: string*** |
4bc5c962Xavier Dupré5 years ago | 163 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 164 | The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict. |
2378ca11Mojimi5 years ago | 165 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 166 | ***suffix_indicator: string*** |
2378ca11Mojimi5 years ago | 167 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 168 | The suffix indicator. |
2378ca11Mojimi5 years ago | 169 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 170 | ***use_indices: int64_t*** |
2378ca11Mojimi5 years ago | 171 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 172 | Whether use second input. |
2378ca11Mojimi5 years ago | 173 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 174 | ***skip_special_tokens: int64_t*** |
2378ca11Mojimi5 years ago | 175 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 176 | Whether or not to remove special tokens in the decoding. |
2378ca11Mojimi5 years ago | 177 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 178 | ***clean_up_tokenization_spaces: int64_t*** |
2378ca11Mojimi5 years ago | 179 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 180 | Whether or not to clean up the tokenization spaces. |
2378ca11Mojimi5 years ago | 181 | |
| 182 | #### Outputs | |
| 183 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 184 | ***sentences: tensor(int64_t)*** |
2378ca11Mojimi5 years ago | 185 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 186 | The decoded sentences. |
2378ca11Mojimi5 years ago | 187 | |
| 188 | #### Examples | |
| 189 | | |
| 190 | | |
| 191 | ```python | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 192 | import transformers |
| 193 | | |
| 194 | def get_file_content(path): | |
| 195 | with open(path, "rb") as file: | |
| 196 | return file.read() | |
| 197 | | |
2c3d6f79Wenbing Li1 years ago | 198 | bert_cased_tokenizer = transformers.BertTokenizer.from_pretrained('google-bert/bert-base-cased') |
f12f42a0Nat Kershaw (MSFT)4 years ago | 199 | bert_cased_tokenizer.save('.', 'bert') |
| 200 | | |
2378ca11Mojimi5 years ago | 201 | |
| 202 | node = onnx.helper.make_node( | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 203 | 'BertTokenizerDecoder', |
| 204 | inputs=['token_ids'], | |
| 205 | outputs=['sentences'], | |
| 206 | vocab_file=get_file_content("bert-vocab.txt") | |
2378ca11Mojimi5 years ago | 207 | ) |
| 208 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 209 | text = "Hello world louder" |
280ec289Edward Chen3 years ago | 210 | token_ids = np.array([bert_cased_tokenizer.tokenize(text)], dtype=object), |
f12f42a0Nat Kershaw (MSFT)4 years ago | 211 | sentences = np.array(text) |
2378ca11Mojimi5 years ago | 212 | |
| 213 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 214 | expect(node, inputs=[token_ids], |
| 215 | outputs=[sentences], name='test_bert_tokenizer') | |
| 216 | ``` | |
2378ca11Mojimi5 years ago | 217 | </details> |
| 218 | | |
a9a49850Mojimi5 years ago | 219 | |
| 220 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 221 | ### GPT2Tokenizer |
a9a49850Mojimi5 years ago | 222 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 223 | <details> |
| 224 | <summary>GPT2Tokenizer details</summary> | |
4a0f8929Mojimi5 years ago | 225 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 226 | GPT2Tokenizer that performs byte-level bpe tokenization to the input tensor, based on the [hugging face version](https://huggingface.co/transformers/_modules/transformers/tokenization_gpt2.html). |
a9a49850Mojimi5 years ago | 227 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 228 | #### Attributes |
4a0f8929Mojimi5 years ago | 229 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 230 | ***vocab*** |
a9a49850Mojimi5 years ago | 231 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 232 | The **content** of the vocabulary file, its format is same with [hugging face](https://huggingface.co/gpt2/resolve/main/vocab.json). |
4a0f8929Mojimi5 years ago | 233 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 234 | ***merges*** |
a9a49850Mojimi5 years ago | 235 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 236 | The **content** of the merges file, its format is same with [hugging face](https://huggingface.co/gpt2/resolve/main/merges.txt). |
4a0f8929Mojimi5 years ago | 237 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 238 | ***padding_length(optional)*** |
4a0f8929Mojimi5 years ago | 239 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 240 | When the input is a set of query, the tokenized result is ragged tensor, so we need to pad the tensor to tidy tensor and the `padding_length` indicates the strategy of the padding. When the padding_length equals -1, we will pad the tensor to length of longest row. When the padding_length is more than 0, we will pad the tensor to the number of padding_length. |
4a0f8929Mojimi5 years ago | 241 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 242 | The default value of `padding_length` is -1. |
4a0f8929Mojimi5 years ago | 243 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 244 | #### Inputs |
4a0f8929Mojimi5 years ago | 245 | |
| 246 | ***data: tensor(string)*** | |
a9a49850Mojimi5 years ago | 247 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 248 | The string tensor for tokenization |
4a0f8929Mojimi5 years ago | 249 | |
| 250 | #### Outputs | |
| 251 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 252 | ***input_ids: tensor(int64)*** |
a9a49850Mojimi5 years ago | 253 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 254 | The tokenized ids of input |
| 255 | | |
| 256 | ***attention_mask: tensor(int64)*** | |
| 257 | | |
| 258 | A tensor indicates which part of input_ids is padded. | |
4a0f8929Mojimi5 years ago | 259 | |
| 260 | #### Examples | |
| 261 | | |
| 262 | | |
| 263 | ```python | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 264 | def get_file_content(path): |
| 265 | with open(path, "rb") as file: | |
| 266 | return file.read() | |
4a0f8929Mojimi5 years ago | 267 | |
| 268 | node = onnx.helper.make_node( | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 269 | 'GPT2Tokenizer', |
4a0f8929Mojimi5 years ago | 270 | inputs=['x'], |
f12f42a0Nat Kershaw (MSFT)4 years ago | 271 | outputs=['y'], |
| 272 | vocab=get_file_content(vocabulary_file), | |
| 273 | merges=get_file_content(merges_file) | |
4a0f8929Mojimi5 years ago | 274 | ) |
| 275 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 276 | x = ["hey cortana"] |
| 277 | y = np.array([20342, 12794, 2271], dtype=np.int64) | |
4a0f8929Mojimi5 years ago | 278 | |
| 279 | expect(node, inputs=[x], outputs=[y], | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 280 | name='test_gpt2_tokenizer') |
4a0f8929Mojimi5 years ago | 281 | ``` |
| 282 | </details> | |
| 283 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 284 | ### WordpieceTokenizer |
4a0f8929Mojimi5 years ago | 285 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 286 | <details> |
| 287 | <summary>WordpieceTokenizer details</summary> | |
4a0f8929Mojimi5 years ago | 288 | |
| 289 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 290 | WordpieceTokenizer that performs WordPiece tokenization to the input tensor, |
| 291 | based on the [hugging face version](https://huggingface.co/transformers/model_doc/bert.html#WordpieceTokenizer). | |
| 292 | [WordpieceTokenizer](https://github.com/tensorflow/text/blob/master/docs/api_docs/python/text/WordpieceTokenizer.md) | |
| 293 | from *tensorflow_text* can be implemented by a pair of nodes | |
| 294 | *RegexSplitWithOffets* followed by *WordpieceTokenizer*. | |
| 295 | it | |
4a0f8929Mojimi5 years ago | 296 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 297 | #### Attributes |
4a0f8929Mojimi5 years ago | 298 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 299 | ***vocab*** |
4a0f8929Mojimi5 years ago | 300 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 301 | The **content** of the vocabulary file, its format is same with |
| 302 | [hugging face](https://huggingface.co/gpt2/resolve/main/vocab.json). | |
4a0f8929Mojimi5 years ago | 303 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 304 | ***suffix_indicator*** |
4a0f8929Mojimi5 years ago | 305 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 306 | Suffix added to token not in the first position before looking into the vocabulary. |
4a0f8929Mojimi5 years ago | 307 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 308 | ***unk_token*** |
a9a49850Mojimi5 years ago | 309 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 310 | Unknown tokens. Every token not found in the vocabulary is replaced by this one. |
4a0f8929Mojimi5 years ago | 311 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 312 | ***max_input_chars_per_word*** |
a9a49850Mojimi5 years ago | 313 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 314 | Maximum number of characters per token (optional, defaults to 200). |
4a0f8929Mojimi5 years ago | 315 | |
| 316 | #### Inputs | |
| 317 | | |
| 318 | ***data: tensor(string)*** | |
a9a49850Mojimi5 years ago | 319 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 320 | The string tensor for tokenization |
4a0f8929Mojimi5 years ago | 321 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 322 | ***row_indices: tensor(int64)*** Empty or the fndices of every first token of input sentences. |
| 323 | `indices[i+1] - indices[i]` is the number of tokens in input `i`. | |
4a0f8929Mojimi5 years ago | 324 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 325 | [WordpieceTokenizer](https://github.com/tensorflow/text/blob/master/docs/api_docs/python/text/WordpieceTokenizer.md) |
| 326 | includes two steps. The first one splits sentences into words and then splits | |
| 327 | every work into tokens. This operator only implements the second step. | |
| 328 | The first one can be done with operator *StringRegexSplit*. | |
| 329 | This parameter can either be empty or it can be the third output | |
| 330 | of operator *StringRegexSplit*. | |
a9a49850Mojimi5 years ago | 331 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 332 | #### Outputs |
4a0f8929Mojimi5 years ago | 333 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 334 | ***tokens: tensor(string)*** Every token. |
4a0f8929Mojimi5 years ago | 335 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 336 | ***token_indices: tensor(int32)*** Indices of each token. -1 means a token outside the vocabulary. |
| 337 | | |
| 338 | ***row_indices: tensor(int64)*** Indices of every first token of input sentences. | |
| 339 | `indices[i+1] - indices[i]` is the number of tokens in input `i`. | |
| 340 | These are updates row indices given as inputs or new ones if the second input is empty. | |
4a0f8929Mojimi5 years ago | 341 | |
| 342 | #### Examples | |
| 343 | | |
| 344 | | |
| 345 | ```python | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 346 | words = ["want", "##want", |
| 347 | "##ed", "wa", "un", "runn", "##ing"] | |
| 348 | vocab = {w: i + 10 for i, w in enumerate(words)} | |
| 349 | st = json.dumps(vocab) | |
| 350 | nodes = [] | |
| 351 | mkv = helper.make_tensor_value_info | |
| 352 | reg = helper.make_tensor( | |
| 353 | "pattern", onnx_proto.TensorProto.STRING, [1, ], ["(\\s)".encode('ascii')]) | |
| 354 | reg_empty = helper.make_tensor( | |
| 355 | "keep_pattern", onnx_proto.TensorProto.STRING, [0, ], []) | |
4a0f8929Mojimi5 years ago | 356 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 357 | nodes = [ |
| 358 | helper.make_node( | |
| 359 | 'StringRegexSplitWithOffsets, | |
| 360 | inputs=['text', 'pattern', 'keep_pattern'], | |
| 361 | outputs=['words', 'begin_end', 'indices'], | |
| 362 | name='StringRegexPlsitOpName', | |
| 363 | domain='ai.onnx.contrib'), | |
| 364 | helper.make_node( | |
| 365 | 'WordpieceTokenizer', | |
| 366 | inputs=['words', 'indices'], | |
| 367 | outputs=['out0', 'out1', 'out2'], | |
| 368 | name='WordpieceTokenizerOpName', | |
| 369 | domain='ai.onnx.contrib', | |
| 370 | vocab=st.encode('utf-8'), | |
| 371 | suffix_indicator="##", | |
| 372 | unk_token="[UNK]") | |
| 373 | ] | |
| 374 | inputs = [mkv('text', onnx_proto.TensorProto.STRING, [None])] | |
| 375 | graph = helper.make_graph( | |
| 376 | nodes, 'test0', inputs, [ | |
| 377 | mkv('out0', onnx_proto.TensorProto.STRING, [None]), | |
| 378 | mkv('out1', onnx_proto.TensorProto.INT32, [None]), | |
| 379 | mkv('out2', onnx_proto.TensorProto.INT64, [None]), | |
| 380 | mkv('words', onnx_proto.TensorProto.STRING, [None]), | |
| 381 | mkv('indices', onnx_proto.TensorProto.INT64, [None])], | |
| 382 | [reg, reg_empty]) | |
| 383 | model = helper.make_model( | |
| 384 | graph, opset_imports=[helper.make_operatorsetid(domain, 1)]) | |
4a0f8929Mojimi5 years ago | 385 | |
280ec289Edward Chen3 years ago | 386 | text = np.array(["unwanted running", "unwantedX running"], dtype=object) |
f12f42a0Nat Kershaw (MSFT)4 years ago | 387 | tokens = np.array(['un', '##want', '##ed', 'runn', '##ing', 'un', '##want', '##ed', |
| 388 | '[UNK]', 'runn', '##ing'], dtype=object), | |
| 389 | indices = np.array([14, 11, 12, 15, 16, 14, 11, 12, -1, 15, 16], dtype=int32) | |
| 390 | row_indices = np.array([ 0, 5, 11], dtype=int64) | |
4a0f8929Mojimi5 years ago | 391 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 392 | expect(model, inputs=[text], outputs=[tokens, indices, row_indices], |
| 393 | name='test_bert_tokenizer') | |
4a0f8929Mojimi5 years ago | 394 | ``` |
a9a49850Mojimi5 years ago | 395 | |
4a0f8929Mojimi5 years ago | 396 | </details> |
| 397 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 398 | ### SentencepieceTokenizer |
4a0f8929Mojimi5 years ago | 399 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 400 | <details> |
| 401 | <summary>SentencepieceTokenizer details</summary> | |
4a0f8929Mojimi5 years ago | 402 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 403 | SentencepieceTokenizer replicates [SentencepieceTokenizer](https://github.com/tensorflow/text/blob/master/docs/api_docs/python/text/SentencepieceTokenizer.md). |
4a0f8929Mojimi5 years ago | 404 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 405 | #### Inputs |
4a0f8929Mojimi5 years ago | 406 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 407 | ***data: tensor(string)*** The string tensor for tokenization |
4a0f8929Mojimi5 years ago | 408 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 409 | ***nbest_size: tensor(int64)*** A scalar for sampling. nbest_size = {0,1}: No sampling is performed. |
| 410 | (default) nbest_size > 1: samples from the nbest_size results. nbest_size < 0: assuming that | |
| 411 | nbest_size is infinite and samples from the all hypothesis (lattice) using | |
| 412 | forward-filtering-and-backward-sampling algorithm. | |
4a0f8929Mojimi5 years ago | 413 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 414 | ***alpha: tensor(float)*** A scalar for a smoothing parameter. Inverse temperature for probability rescaling. |
4a0f8929Mojimi5 years ago | 415 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 416 | ***reverse: tensor(bool)*** Reverses the tokenized sequence (Default = false) |
4a0f8929Mojimi5 years ago | 417 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 418 | ***add_bos: tensor(bool)*** Add beginning of sentence token to the result (Default = false) |
4a0f8929Mojimi5 years ago | 419 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 420 | ***add_eos: tensor(bool)*** Add end of sentence token to the result (Default = false). |
| 421 | When reverse=True beginning/end of sentence tokens are added after reversing. | |
4a0f8929Mojimi5 years ago | 422 | |
| 423 | #### Attributes | |
| 424 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 425 | ***model: string*** The sentencepiece model serialized proto as stored as a string. |
4a0f8929Mojimi5 years ago | 426 | |
| 427 | #### Outputs | |
| 428 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 429 | ***tokens: tensor(int32)*** Indices of each token. |
a9a49850Mojimi5 years ago | 430 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 431 | ***indices: tensor(int64)*** Indices of every first token of input sentences. |
| 432 | `indices[i+1] - indices[i]` is the number of tokens in input `i`. | |
4a0f8929Mojimi5 years ago | 433 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 434 | Tokenized result of the input |
4a0f8929Mojimi5 years ago | 435 | |
| 436 | #### Examples | |
| 437 | | |
| 438 | | |
| 439 | ```python | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 440 | |
| 441 | url = "https://github.com/microsoft/ort-customops/raw/main/test/data/test_sentencepiece_ops_model__6.txt" | |
| 442 | with urllib.request.urlopen(url) as f: | |
| 443 | content = f.read() | |
| 444 | model = np.array(list(base64.decodebytes(content.encode())), dtype=np.uint8) | |
4a0f8929Mojimi5 years ago | 445 | |
| 446 | node = onnx.helper.make_node( | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 447 | 'SentencepieceTokenizer', |
| 448 | inputs=['inputs', 'nbest_size', 'alpha', 'add_bos', 'add_eos', 'reverse'], | |
| 449 | outputs=['indices', 'output'], | |
| 450 | mapping_file_name='vocabulary.txt', | |
| 451 | unmapping_value="unknown_word", | |
85ffb941Zhipeng Han1 years ago | 452 | model=model, |
| 453 | domain='ai.onnx.contrib' | |
4a0f8929Mojimi5 years ago | 454 | ) |
| 455 | | |
280ec289Edward Chen3 years ago | 456 | inputs = np.array(["Hello world", "Hello world louder"], dtype=object), |
f12f42a0Nat Kershaw (MSFT)4 years ago | 457 | nbest_size = np.array([0], dtype=np.float32), |
| 458 | alpha = np.array([0], dtype=np.float32), | |
| 459 | add_bos = np.array([0], dtype=np.bool_), | |
| 460 | add_eos = np.array([0], dtype=np.bool_), | |
| 461 | reverse = np.array([0], dtype=np.bool_) | |
4a0f8929Mojimi5 years ago | 462 | |
85ffb941Zhipeng Han1 years ago | 463 | tokens = np.array([17486, 1017, 17486, 1017, 155, 21869], dtype=np.int32) |
| 464 | indices = np.array([0, 2, 6], dtype=np.int64) | |
4a0f8929Mojimi5 years ago | 465 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 466 | expect(node, inputs=[inputs, nbest_size, alpha, add_bos, add_eos, reverse], |
| 467 | outputs=[tokens, indices], name='sp') | |
4a0f8929Mojimi5 years ago | 468 | ``` |
| 469 | </details> | |
abdd5b1bMojimi4 years ago | 470 | |
| 471 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 472 | ### BasicTokenizer |
| 473 | | |
| 474 | <details> | |
| 475 | <summary>BasicTokenizer details</summary> | |
abdd5b1bMojimi4 years ago | 476 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 477 | TODO: is this still supported? |
| 478 | | |
| 479 | BasicTokenizer performs basic tokenization to input string tensor, based on [basic tokenizer in BertTokenizer(hugging face version)](https://huggingface.co/transformers/_modules/transformers/models/bert/tokenization_bert.html#BertTokenizer). | |
abdd5b1bMojimi4 years ago | 480 | |
| 481 | #### Inputs | |
| 482 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 483 | ***text: tensor(string)*** The string tensor for tokenization |
abdd5b1bMojimi4 years ago | 484 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 485 | #### Attributes |
abdd5b1bMojimi4 years ago | 486 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 487 | ***do_lower_case: int64_t*** (default is 1, 1 represents True, 0 represents False) |
abdd5b1bMojimi4 years ago | 488 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 489 | Whether or not to lowercase the input when tokenizing. |
abdd5b1bMojimi4 years ago | 490 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 491 | ***tokenize_chinese_chars: int64_t*** (default is 1, 1 represents True, 0 represents False) |
abdd5b1bMojimi4 years ago | 492 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 493 | Whether or not to tokenize Chinese characters. |
abdd5b1bMojimi4 years ago | 494 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 495 | ***strip_accents: int64_t*** (default is 1, 1 represents True, 0 represents False) |
abdd5b1bMojimi4 years ago | 496 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 497 | Whether or not to strip all accents. If this option is not specified, then it will be determined by the |
| 498 | value for :obj:`lowercase` (as in the original BERT). | |
abdd5b1bMojimi4 years ago | 499 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 500 | ***tokenize_punctuation: int64_t*** (default is 0, 1 represents True, 0 represents False) |
abdd5b1bMojimi4 years ago | 501 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 502 | Splits punctuation on a piece of text. |
| 503 | | |
| 504 | ***remove_control_chars: int64_t*** (default is 0, 1 represents True, 0 represents False) | |
| 505 | | |
| 506 | Remove control chars(such as NUL, BEL) in the text. | |
| 507 | | |
| 508 | #### Outputs | |
| 509 | | |
| 510 | ***tokens: tensor(string)*** Tokenized tokens. | |
| 511 | | |
| 512 | #### Examples | |
abdd5b1bMojimi4 years ago | 513 | |
| 514 | ```python | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 515 | import transformers |
| 516 | | |
| 517 | tokenizer = transformers.BasicTokenizer() | |
abdd5b1bMojimi4 years ago | 518 | |
| 519 | node = onnx.helper.make_node( | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 520 | 'BasicTokenizer', |
| 521 | inputs=['text'], | |
| 522 | outputs=['tokens'], | |
abdd5b1bMojimi4 years ago | 523 | ) |
| 524 | | |
280ec289Edward Chen3 years ago | 525 | inputs = np.array([ "Hello world louder"], dtype=object), |
f12f42a0Nat Kershaw (MSFT)4 years ago | 526 | tokens = np.array(tokenizer(inputs), dtype=int32) |
abdd5b1bMojimi4 years ago | 527 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 528 | expect(node, inputs=[inputs], |
| 529 | outputs=[tokens], name='test_basic_tokenizer') | |
abdd5b1bMojimi4 years ago | 530 | ``` |
| 531 | </details> | |
4a0f8929Mojimi5 years ago | 532 | |
| 533 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 534 | ## String operators |
a9a49850Mojimi5 years ago | 535 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 536 | ### StringEqual |
9653f523Mojimi5 years ago | 537 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 538 | <details> |
| 539 | <summary>StringEqual details</summary> | |
9653f523Mojimi5 years ago | 540 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 541 | Compares two strings and returns true if they are equal and false if not. |
a9a49850Mojimi5 years ago | 542 | |
4a0f8929Mojimi5 years ago | 543 | #### Inputs |
| 544 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 545 | ***x: tensor(string)*** |
a9a49850Mojimi5 years ago | 546 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 547 | The first string input |
| 548 | | |
| 549 | ***x: tensor(string)*** | |
| 550 | | |
| 551 | The second string input | |
4a0f8929Mojimi5 years ago | 552 | |
| 553 | #### Outputs | |
| 554 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 555 | ***z: tensor(boolean)*** |
9653f523Mojimi5 years ago | 556 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 557 | String with replacements. |
9653f523Mojimi5 years ago | 558 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 559 | </details> |
a9a49850Mojimi5 years ago | 560 | |
4a0f8929Mojimi5 years ago | 561 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 562 | ### StringHash |
4a0f8929Mojimi5 years ago | 563 | |
| 564 | <details> | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 565 | <summary>StringHash details</summary> |
4a0f8929Mojimi5 years ago | 566 | |
| 567 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 568 | Hashes the input string based on the number of buckets |
4a0f8929Mojimi5 years ago | 569 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 570 | #### Inputs |
4a0f8929Mojimi5 years ago | 571 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 572 | ***input: tensor(string)*** |
4a0f8929Mojimi5 years ago | 573 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 574 | The string to hash |
4a0f8929Mojimi5 years ago | 575 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 576 | ***num_buckets: tensor(int64)*** |
4bc5c962Xavier Dupré5 years ago | 577 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 578 | The number of buckets (must be equal to 1?) |
4a0f8929Mojimi5 years ago | 579 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 580 | #### Outputs |
4bc5c962Xavier Dupré5 years ago | 581 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 582 | ***name: tensor(int64)*** |
4bc5c962Xavier Dupré5 years ago | 583 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 584 | The hash value of the string |
4bc5c962Xavier Dupré5 years ago | 585 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 586 | </details> |
4bc5c962Xavier Dupré5 years ago | 587 | |
| 588 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 589 | ### StringHashFast |
4bc5c962Xavier Dupré5 years ago | 590 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 591 | <details> |
| 592 | <summary>StringHashFast details</summary> | |
4bc5c962Xavier Dupré5 years ago | 593 | |
| 594 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 595 | A faster implementation of StringHash. |
| 596 | | |
| 597 | </details> | |
| 598 | | |
| 599 | | |
| 600 | ### StringJoin | |
| 601 | | |
| 602 | <details> | |
| 603 | <summary>StringJoin details</summary> | |
| 604 | | |
| 605 | | |
| 606 | Join an array of strings | |
4a0f8929Mojimi5 years ago | 607 | |
| 608 | #### Inputs | |
| 609 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 610 | ***input_X: tensor(string)*** |
a9a49850Mojimi5 years ago | 611 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 612 | The input array of strings |
4a0f8929Mojimi5 years ago | 613 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 614 | ***input_sep: tensor(string)*** |
4bc5c962Xavier Dupré5 years ago | 615 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 616 | The string separator for the resulting joing |
4bc5c962Xavier Dupré5 years ago | 617 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 618 | ***input_axis: tensor(int64)*** |
4a0f8929Mojimi5 years ago | 619 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 620 | The axis along which to joing |
4bc5c962Xavier Dupré5 years ago | 621 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 622 | #### Outputs |
a9a49850Mojimi5 years ago | 623 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 624 | ***out: tensor(string)*** |
| 625 | | |
| 626 | The resulting joined string | |
4a0f8929Mojimi5 years ago | 627 | |
| 628 | #### Examples | |
| 629 | | |
| 630 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 631 | ```bash |
4bc5c962Xavier Dupré5 years ago | 632 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 633 | input_X = [["a", "b", "c"], ["aa", "bb", ""]] |
| 634 | input_sep=";" | |
| 635 | input_axis = 1 | |
4bc5c962Xavier Dupré5 years ago | 636 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 637 | out = ["a;b;c", "aa;bb;"] |
| 638 | | |
| 639 | input_axis = 0 | |
| 640 | | |
| 641 | out = ['a;aa', 'b;bb', 'c;'] | |
4bc5c962Xavier Dupré5 years ago | 642 | |
| 643 | | |
4a0f8929Mojimi5 years ago | 644 | </details> |
| 645 | | |
a32f9bc2Xavier Dupré5 years ago | 646 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 647 | ### StringRegexReplace |
| 648 | | |
| 649 | <details> | |
| 650 | <summary>StringRegexReplace details</summary> | |
| 651 | | |
| 652 | | |
| 653 | String replacement based on [Re2-format](https://github.com/google/re2/wiki/Syntax) regular expressions. | |
a32f9bc2Xavier Dupré5 years ago | 654 | |
| 655 | #### Inputs | |
| 656 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 657 | ***text: tensor(string)*** |
a32f9bc2Xavier Dupré5 years ago | 658 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 659 | String tensor to extract slices from. |
a32f9bc2Xavier Dupré5 years ago | 660 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 661 | ***pattern: tensor(string)*** |
a32f9bc2Xavier Dupré5 years ago | 662 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 663 | Pattern of the regular expression. |
a32f9bc2Xavier Dupré5 years ago | 664 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 665 | ***rewrite: tensor(string)*** |
a32f9bc2Xavier Dupré5 years ago | 666 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 667 | Replacement. |
a32f9bc2Xavier Dupré5 years ago | 668 | |
4c201e78Xavier Dupré5 years ago | 669 | #### Attributes |
a32f9bc2Xavier Dupré5 years ago | 670 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 671 | ***global_replace: int64*** (default is 1) |
a32f9bc2Xavier Dupré5 years ago | 672 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 673 | Replace all strings matching the pattern or the first one. |
a32f9bc2Xavier Dupré5 years ago | 674 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 675 | #### Outputs |
a32f9bc2Xavier Dupré5 years ago | 676 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 677 | ***output: tensor(string)*** |
a32f9bc2Xavier Dupré5 years ago | 678 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 679 | String with replacements. |
a32f9bc2Xavier Dupré5 years ago | 680 | |
| 681 | #### Examples | |
| 682 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 683 | ```python |
| 684 | | |
| 685 | node = onnx.helper.make_node( | |
| 686 | 'StringRegexReplace', | |
| 687 | inputs=['text', 'pattern', 'rewrite'], | |
| 688 | outputs=['y'], | |
| 689 | ) | |
| 690 | | |
| 691 | text = np.array([['def myfunc():'], ['def dummy():']]) | |
| 692 | pattern = np.array([r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):']) | |
| 693 | rewrite = np.array([r'static PyObject* py_\1(void) {']) | |
| 694 | y = [['static PyObject* py_myfunc(void) {'], | |
| 695 | ['static PyObject* py_dummy(void) {']] | |
| 696 | | |
| 697 | expect(node, inputs=[text, pattern, rewrite], outputs=[y], | |
| 698 | name='test_string_regex_replace') | |
| 699 | ``` | |
| 700 | | |
| 701 | </details> | |
| 702 | | |
| 703 | ### StringECMARegexReplace | |
| 704 | | |
a32f9bc2Xavier Dupré5 years ago | 705 | <details> |
f12f42a0Nat Kershaw (MSFT)4 years ago | 706 | <summary>StringECMARegexReplace details</summary> |
| 707 | | |
| 708 | String replacement based on [ECMA-format](https://en.cppreference.com/w/cpp/regex/ecmascript) regular expressions. | |
| 709 | | |
| 710 | #### Inputs | |
| 711 | | |
| 712 | ***text: tensor(string)*** | |
| 713 | | |
| 714 | String tensor to extract slices from. | |
| 715 | | |
| 716 | ***pattern: tensor(string)*** | |
| 717 | | |
| 718 | Pattern of the regular expression. | |
| 719 | | |
| 720 | ***rewrite: tensor(string)*** | |
| 721 | | |
| 722 | Replacement. | |
| 723 | | |
| 724 | #### Attributes | |
| 725 | | |
| 726 | ***global_replace: int64*** (default is 1) | |
| 727 | | |
| 728 | Replace all strings matching the pattern or the first one. | |
| 729 | | |
| 730 | | |
| 731 | ***ignore_case: int64*** (default is 0) | |
| 732 | | |
| 733 | Replace | |
| 734 | | |
| 735 | #### Outputs | |
| 736 | | |
| 737 | ***output: tensor(string)*** | |
| 738 | | |
| 739 | String with replacements. | |
| 740 | | |
| 741 | #### Examples | |
| 742 | | |
a32f9bc2Xavier Dupré5 years ago | 743 | |
| 744 | ```python | |
| 745 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 746 | node = onnx.helper.make_node( |
| 747 | 'StringRegexReplace', | |
| 748 | inputs=['text', 'pattern', 'rewrite'], | |
| 749 | outputs=['y'], | |
| 750 | ) | |
| 751 | | |
| 752 | text = np.array([['def myfunc():'], ['def dummy():']]) | |
| 753 | pattern = np.array([r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):']) | |
| 754 | rewrite = np.array([r'static PyObject* py_$1(void) {']) | |
| 755 | y = [['static PyObject* py_myfunc(void) {'], | |
| 756 | ['static PyObject* py_dummy(void) {']] | |
| 757 | | |
| 758 | expect(node, inputs=[text, pattern, rewrite], outputs=[y], | |
| 759 | name='test_string_regex_replace') | |
| 760 | ``` | |
| 761 | | |
| 762 | </details> | |
| 763 | | |
| 764 | | |
| 765 | | |
| 766 | ### StringSplit | |
| 767 | | |
| 768 | TODO | |
| 769 | | |
| 770 | ### StringUpper | |
| 771 | | |
| 772 | TODO | |
| 773 | | |
| 774 | ### StringLower | |
| 775 | | |
| 776 | TODO | |
| 777 | | |
| 778 | ### StringLength | |
| 779 | | |
| 780 | <details> | |
| 781 | <summary>StringECMARegexReplace details</summary> | |
| 782 | | |
| 783 | Get the length of each string element in input tensor. Similar to the function `len("abcde"")` in python. | |
| 784 | | |
| 785 | #### Inputs | |
| 786 | | |
| 787 | ***data: tensor(string)*** | |
| 788 | | |
| 789 | String tensor to get length of its each string element. | |
| 790 | | |
| 791 | #### Outputs | |
| 792 | | |
| 793 | ***output: tensor(int64)*** | |
| 794 | | |
| 795 | Data length tensor. | |
| 796 | | |
| 797 | #### Examples | |
| 798 | | |
| 799 | | |
| 800 | ```python | |
a32f9bc2Xavier Dupré5 years ago | 801 | |
| 802 | node = onnx.helper.make_node( | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 803 | 'StringLength', |
| 804 | inputs=['x'], | |
| 805 | outputs=['y'] | |
a32f9bc2Xavier Dupré5 years ago | 806 | ) |
| 807 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 808 | x = ["abcdef", "hijkl"] |
| 809 | y = np.array([len(x[0]), len(x[1])], dtype=np.int64) | |
a32f9bc2Xavier Dupré5 years ago | 810 | |
| 811 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 812 | expect(node, inputs=[x], outputs=[y], |
| 813 | name='test_string_length') | |
a32f9bc2Xavier Dupré5 years ago | 814 | ``` |
| 815 | </details> | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 816 | |
| 817 | ### StringConcat | |
| 818 | | |
| 819 | <details> | |
| 820 | <summary>StringConcat details</summary> | |
| 821 | | |
| 822 | Concat the corresponding string in the two string tensor. Two input tensors should have the same dimension. | |
| 823 | | |
| 824 | ```python | |
| 825 | output = [] | |
| 826 | shape = input1.shape | |
| 827 | input1 = input1.flatten() | |
| 828 | input2 = input2.flatten() | |
| 829 | for i in range(len(input1)): | |
| 830 | output.append(input1[i] + input2[i]) | |
| 831 | output = np.array(output).reshape(shape) | |
| 832 | ``` | |
| 833 | | |
| 834 | #### Inputs | |
| 835 | | |
| 836 | ***input_1: tensor(string)*** | |
| 837 | | |
| 838 | The first string tensor. | |
| 839 | | |
| 840 | ***input_2: tensor(string)*** | |
| 841 | | |
| 842 | The second string tensor. | |
| 843 | | |
| 844 | | |
| 845 | #### Outputs | |
| 846 | | |
| 847 | ***output: tensor(string)*** | |
| 848 | | |
| 849 | The result. | |
| 850 | | |
| 851 | #### Examples | |
| 852 | | |
| 853 | | |
| 854 | ```python | |
| 855 | | |
| 856 | node = onnx.helper.make_node( | |
| 857 | 'StringConcat', | |
| 858 | inputs=['x', 'y'], | |
| 859 | outputs=['result'], | |
| 860 | ) | |
| 861 | | |
| 862 | x = np.array(["abcd", "efgh"]) | |
| 863 | y = np.array(["wxyz", "stuv"]) | |
| 864 | result = np.array([x[0] + y[0], x[1] + y[1]]) | |
| 865 | | |
| 866 | expect(node, inputs=[x, y], outputs=[result], | |
| 867 | name='test_string_concat') | |
| 868 | ``` | |
| 869 | | |
| 870 | </details> | |
| 871 | | |
| 872 | ### StringRegexSplitWithOffsets | |
| 873 | | |
| 874 | <details> | |
| 875 | <summary>StringRegexSplitWithOffsets details</summary> | |
| 876 | | |
| 877 | Splits string based on regular expressions. | |
| 878 | | |
| 879 | #### Inputs | |
| 880 | | |
| 881 | ***text: tensor(string)*** | |
| 882 | | |
| 883 | String tensor to extract slices from. | |
| 884 | | |
| 885 | ***delim_regex_pattern: tensor(string)*** | |
| 886 | | |
| 887 | Splitting attern of the regular expression. | |
| 888 | | |
| 889 | ***keep_delim_regex_pattern: tensor(string)*** | |
| 890 | | |
| 891 | By default, delimiters are not included in the split string results. Delimiters may be included by specifying a regex pattern keep_delim_regex_pattern. | |
| 892 | | |
| 893 | #### Outputs | |
| 894 | | |
| 895 | ***words: tensor(string)*** Tensor of words. | |
| 896 | | |
| 897 | ***offsets: tensor(int64)*** 2D tensor with 3 columns: | |
| 898 | sentence index, position of the first character, position of the last one (excluded) | |
| 899 | | |
| 900 | ***row_indices: tensor(int64)*** Indices of every first token of input sentences. | |
| 901 | `row_indices[i+1] - row_indices[i]` is the number of tokens in input `i`. | |
| 902 | These are updates row indices given as inputs or new ones if the second input is empty. | |
| 903 | | |
| 904 | | |
| 905 | #### Examples | |
| 906 | | |
| 907 | | |
| 908 | ```python | |
| 909 | | |
| 910 | node = onnx.helper.make_node( | |
| 911 | 'StringRegexSplit', | |
| 912 | inputs=['text', 'pattern', 'rewrite'], | |
| 913 | outputs=['y', 'begin_end', 'indices'], | |
| 914 | ) | |
| 915 | | |
| 916 | text = np.array(["hello there"]) | |
| 917 | pattern = np.array([r'\s']) | |
| 918 | rewrite = np.array([r'\s']) | |
| 919 | y = np.array(["hello", " ", "there"]) | |
| 920 | z1 = np.array([[0, 0, 5], | |
| 921 | [0, 5, 6], | |
| 922 | [0, 6, 11]], dtype=np.int64) | |
| 923 | z2 = np.array([0, 2], dtype=np.int64) | |
| 924 | | |
| 925 | expect(node, inputs=[text, pattern, rewrite], outputs=[y, z1, z2], | |
| 926 | name='test_string_regex_replace') | |
| 927 | ``` | |
| 928 | | |
| 929 | </details> | |
| 930 | | |
| 931 | | |
| 932 | ### StringECMARegexSplitWithOffsets | |
| 933 | | |
| 934 | TODO | |
| 935 | | |
| 936 | ### VectorToString | |
| 937 | | |
| 938 | <details> | |
| 939 | <summary>VectorToString details</summary> | |
| 940 | | |
| 941 | VectorToString is the contrary operation to the `StringToVector` , they share same format of mapping table: | |
| 942 | | |
| 943 | <string>\t<scalar_1>\s<scalar_2>\s<scalar_3>...<scalar_n> | |
| 944 | | |
| 945 | Unmapped vector will output the value of the attribute `unk`. | |
| 946 | | |
| 947 | Example: | |
| 948 | | |
| 949 | *Attributes:* | |
| 950 | | |
| 951 | - `map`: | |
| 952 | ``` | |
| 953 | a 0 0 1 2 | |
| 954 | b 0 1 2 3 | |
| 955 | d 0 1 3 4 | |
| 956 | ``` | |
| 957 | | |
| 958 | - `unk`: "unknown_word" | |
| 959 | | |
| 960 | *Inputs:* | |
| 961 | - data: [[0,0,1,2],[0,1,3,4],[0,0,0,0]] | |
| 962 | | |
| 963 | *Ouputs:* | |
| 964 | - output: ["a", "d", "unknown_word" ] | |
| 965 | | |
| 966 | #### Attributes | |
| 967 | | |
| 968 | ***mapping_file_name*** | |
| 969 | | |
| 970 | the formative mapping table | |
| 971 | | |
| 972 | ***unmapping_value*** | |
| 973 | | |
| 974 | the result returned when a vector aren't found in the map | |
| 975 | | |
| 976 | #### Inputs | |
| 977 | | |
| 978 | ***data: tensor(T)*** | |
| 979 | | |
| 980 | Input tensor | |
| 981 | | |
| 982 | #### Outputs | |
| 983 | | |
| 984 | ***output: tensor(string)*** | |
| 985 | | |
| 986 | The mapping result of the input | |
| 987 | | |
| 988 | #### Type Constraints | |
| 989 | ***T:tensor(uint8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(int8), tensor(int16), tensor(int32), tensor(int64), tensor(bfloat16), tensor(float16), tensor(float), tensor(double), tensor(bool)*** | |
| 990 | | |
| 991 | Constrain input and output types to numerical tensors. | |
| 992 | | |
| 993 | | |
| 994 | #### Examples | |
| 995 | | |
| 996 | | |
| 997 | ```python | |
| 998 | mapping_table = \ | |
| 999 | """ | |
| 1000 | a 0 0 1 2 | |
| 1001 | b 0 1 2 3 | |
| 1002 | d 0 1 3 4 | |
| 1003 | """ | |
| 1004 | | |
| 1005 | node = onnx.helper.make_node( | |
| 1006 | 'VectorToString', | |
| 1007 | inputs=['x'], | |
| 1008 | outputs=['y'], | |
| 1009 | map=mapping_table, | |
| 1010 | unk="unknown_word" | |
| 1011 | ) | |
| 1012 | | |
| 1013 | | |
| 1014 | x = np.array([[0,0,1,2],[0,1,3,4],[0,0,0,0]], type=np.int64) | |
| 1015 | y = ["a", "d", "unknown_word"] | |
| 1016 | | |
| 1017 | | |
| 1018 | expect(node, inputs=[x], outputs=[y], | |
| 1019 | name='test_vector_to_string') | |
| 1020 | ``` | |
| 1021 | </details> | |
| 1022 | | |
| 1023 | | |
| 1024 | ### StringToVector | |
| 1025 | | |
| 1026 | <details> | |
| 1027 | <summary>StringToVector details</summary> | |
| 1028 | | |
| 1029 | StringToVector will map each string element in the input to the corresponding vector according to the mapping file. The mapping file is a utf-8 encoding text file in tsv format: | |
| 1030 | | |
| 1031 | <string>\t<scalar_1>\s<scalar_2>\s<scalar_3>...<scalar_n> | |
| 1032 | | |
| 1033 | Unmapped string will output the value of the attribute `unmapping_value`. | |
a32f9bc2Xavier Dupré5 years ago | 1034 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1035 | Example: |
4a0f8929Mojimi5 years ago | 1036 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1037 | *Attributes:* |
4a0f8929Mojimi5 years ago | 1038 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1039 | - `mapping_file_name`: vocabulary.txt |
| 1040 | ``` | |
| 1041 | a 0 0 1 2 | |
| 1042 | b 0 1 2 3 | |
| 1043 | d 0 1 3 4 | |
| 1044 | ``` | |
| 1045 | | |
| 1046 | - `unmapping_value`: [0 0 0 0] | |
4a0f8929Mojimi5 years ago | 1047 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1048 | *Inputs:* |
| 1049 | - data: ["a", "d", "e"] | |
| 1050 | | |
| 1051 | *Ouputs:* | |
| 1052 | - output: [[0,0,1,2],[0,1,3,4],[0,0,0,0]] | |
4290400eMojimi4 years ago | 1053 | |
| 1054 | #### Attributes | |
| 1055 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1056 | ***mapping_file_name:string*** |
4290400eMojimi4 years ago | 1057 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1058 | The name of your string to vector mapping file. |
4290400eMojimi4 years ago | 1059 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1060 | ***unmapping_value:list(int)*** |
4290400eMojimi4 years ago | 1061 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1062 | Mapping result for unmapped string |
4290400eMojimi4 years ago | 1063 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1064 | #### Inputs |
4290400eMojimi4 years ago | 1065 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1066 | ***data: tensor(string)*** |
4290400eMojimi4 years ago | 1067 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1068 | Input tensor |
4290400eMojimi4 years ago | 1069 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1070 | #### Outputs |
4290400eMojimi4 years ago | 1071 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1072 | ***output: tensor(T)*** |
4290400eMojimi4 years ago | 1073 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1074 | The mapping result of the input |
4a0f8929Mojimi5 years ago | 1075 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1076 | #### Type Constraints |
| 1077 | ***T:tensor(uint8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(int8), tensor(int16), tensor(int32), tensor(int64), tensor(bfloat16), tensor(float16), tensor(float), tensor(double), tensor(bool)*** | |
4a0f8929Mojimi5 years ago | 1078 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1079 | Constrain input and output types to numerical tensors. |
4290400eMojimi4 years ago | 1080 | |
| 1081 | #### Examples | |
| 1082 | | |
| 1083 | | |
| 1084 | ```python | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1085 | # what's in vocabulary.txt |
4290400eMojimi4 years ago | 1086 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1087 | mapping_table = \ |
| 1088 | """ | |
| 1089 | a 0 0 1 2 | |
| 1090 | b 0 1 2 3 | |
| 1091 | d 0 1 3 4 | |
| 1092 | """ | |
4290400eMojimi4 years ago | 1093 | |
| 1094 | node = onnx.helper.make_node( | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1095 | 'StringToVector', |
| 1096 | inputs=['x'], | |
| 1097 | outputs=['y'], | |
| 1098 | mapping_table=mapping_table, | |
| 1099 | unmapping_value=[0,0,0,0] | |
4290400eMojimi4 years ago | 1100 | ) |
| 1101 | | |
| 1102 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1103 | x = ["a", "d", "e"] |
| 1104 | y = np.array([[0,0,1,2],[0,1,3,4],[0,0,0,0]], type=np.int64) | |
| 1105 | | |
| 1106 | | |
| 1107 | expect(node, inputs=[x], outputs=[y], | |
| 1108 | name='test_string_to_vector') | |
4290400eMojimi4 years ago | 1109 | ``` |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1110 | |
4290400eMojimi4 years ago | 1111 | </details> |
| 1112 | | |
| 1113 | | |
| 1114 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1115 | ### StringSlice |
4290400eMojimi4 years ago | 1116 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1117 | <details> |
| 1118 | <summary>StringSlice details</summary> | |
4290400eMojimi4 years ago | 1119 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1120 | Do the slice operation to each string element in input tensor. Similar to string slice in python |
4290400eMojimi4 years ago | 1121 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1122 | ```python |
| 1123 | a = "abcdef" | |
| 1124 | b = a[1:2] | |
| 1125 | c = a[3:1:-1] | |
| 1126 | ``` | |
4290400eMojimi4 years ago | 1127 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1128 | #### Inputs |
4290400eMojimi4 years ago | 1129 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1130 | ***data: tensor(string)*** |
4290400eMojimi4 years ago | 1131 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1132 | String tensor to extract slices from. |
4290400eMojimi4 years ago | 1133 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1134 | ***starts: tensor(int64/int32)*** |
4290400eMojimi4 years ago | 1135 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1136 | The tensor of starting indices of corresponding string in data, which has same dimension of data. |
4290400eMojimi4 years ago | 1137 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1138 | ***ends: tensor(int64/int32)*** |
4290400eMojimi4 years ago | 1139 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1140 | The tensor of ending indices of corresponding string in data, which has same dimension of data. |
4290400eMojimi4 years ago | 1141 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1142 | ***steps(optional): tensor(int64/int32)*** |
4290400eMojimi4 years ago | 1143 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1144 | The tensor of slice step of corresponding string in data, which has same dimension of data.If steps is empty tensor, we will use default value 1 for each string |
4290400eMojimi4 years ago | 1145 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1146 | #### Outputs |
4290400eMojimi4 years ago | 1147 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1148 | ***output: tensor(string)*** |
4290400eMojimi4 years ago | 1149 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1150 | Sliced data tensor. |
4290400eMojimi4 years ago | 1151 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1152 | #### Examples |
4290400eMojimi4 years ago | 1153 | |
| 1154 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1155 | ```python |
4290400eMojimi4 years ago | 1156 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1157 | node = onnx.helper.make_node( |
| 1158 | 'StringSlice', | |
| 1159 | inputs=['x', 'starts', 'ends', 'steps'], | |
| 1160 | outputs=['y'], | |
| 1161 | ) | |
4290400eMojimi4 years ago | 1162 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1163 | x = np.array(["abcdef", "hijkl"]) |
| 1164 | y = np.array([x[0][1:3:1], x[1][3:1:-1]]) | |
| 1165 | starts = np.array([1, 3], dtype=np.int64) | |
| 1166 | ends = np.array([3, 1], dtype=np.int64) | |
| 1167 | axes = np.array([0, 1], dtype=np.int64) | |
| 1168 | steps = np.array([1, 1], dtype=np.int64) | |
4290400eMojimi4 years ago | 1169 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1170 | expect(node, inputs=[x, starts, ends, axes, steps], outputs=[y], |
| 1171 | name='test_string_slice') | |
| 1172 | ``` | |
4290400eMojimi4 years ago | 1173 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1174 | </details> |
4290400eMojimi4 years ago | 1175 | |
| 1176 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1177 | ### MaskedFill |
4290400eMojimi4 years ago | 1178 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1179 | <details> |
| 1180 | <summary>MaskedFill details</summary> | |
4290400eMojimi4 years ago | 1181 | |
| 1182 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1183 | Fills elements of self tensor with value where mask is True. The operator is similar with [`Tensor.masked_fill_`](https://pytorch.org/docs/stable/generated/torch.Tensor.masked_fill_.html#torch.Tensor.masked_fill_) in pytorch. |
4290400eMojimi4 years ago | 1184 | |
| 1185 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1186 | #### Inputs |
4290400eMojimi4 years ago | 1187 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1188 | ***value: tensor(string)*** |
4290400eMojimi4 years ago | 1189 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1190 | The value to fill in with, currently we only support string type and vector&scalar dimension. |
4290400eMojimi4 years ago | 1191 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1192 | ***mask: tensor(bool)*** |
4290400eMojimi4 years ago | 1193 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1194 | The boolean mask, the dimension of mask tensor should be same with value. |
4290400eMojimi4 years ago | 1195 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1196 | #### Outputs |
| 1197 | | |
| 1198 | ***output: tensor(string)*** | |
| 1199 | | |
| 1200 | The filled output of input tensor. | |
a9a49850Mojimi5 years ago | 1201 | |
4a0f8929Mojimi5 years ago | 1202 | |
| 1203 | #### Examples | |
| 1204 | | |
| 1205 | | |
| 1206 | ```python | |
4290400eMojimi4 years ago | 1207 | |
| 1208 | node = onnx.helper.make_node( | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1209 | 'MaskedFill', |
| 1210 | inputs=['value', 'mask'], | |
| 1211 | outputs=['output'] | |
4290400eMojimi4 years ago | 1212 | ) |
| 1213 | | |
| 1214 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1215 | value = np.array(["a", "b", "c", "d"]) |
| 1216 | mask = np.array([True, False, True, False], dtype=bool) | |
| 1217 | output = np.array(["a", "c"]) | |
4290400eMojimi4 years ago | 1218 | |
| 1219 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1220 | expect(node, inputs=[value, mask], outputs=[output], |
| 1221 | name='test_masked_fill') | |
4290400eMojimi4 years ago | 1222 | ``` |
| 1223 | </details> | |
| 1224 | | |
d853d31fRandySheriffH2 years ago | 1225 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1226 | ### StringRaggedTensorToDense |
4290400eMojimi4 years ago | 1227 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1228 | TODO |
4290400eMojimi4 years ago | 1229 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1230 | ### StringMapping |
4290400eMojimi4 years ago | 1231 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1232 | TODO |
4290400eMojimi4 years ago | 1233 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1234 | ## Math operators |
4290400eMojimi4 years ago | 1235 | |
| 1236 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1237 | ### Inverse |
4290400eMojimi4 years ago | 1238 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1239 | TODO |
4290400eMojimi4 years ago | 1240 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1241 | ### NegPos |
4290400eMojimi4 years ago | 1242 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1243 | TODO |
4290400eMojimi4 years ago | 1244 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1245 | ### SegmentExtraction |
4290400eMojimi4 years ago | 1246 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1247 | TODO |
4290400eMojimi4 years ago | 1248 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1249 | ### SegmentSum |
4290400eMojimi4 years ago | 1250 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1251 | TODO |
4290400eMojimi4 years ago | 1252 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1253 | ## Tensor operators |
4290400eMojimi4 years ago | 1254 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1255 | ### RaggedTensorToSparse |
4290400eMojimi4 years ago | 1256 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1257 | TODO |
4290400eMojimi4 years ago | 1258 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1259 | ### RaggedTensorToDense |
4290400eMojimi4 years ago | 1260 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1261 | TODO |
4290400eMojimi4 years ago | 1262 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1263 | ### Template |
4290400eMojimi4 years ago | 1264 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1265 | <details> |
| 1266 | <summary>Template details</summary> | |
4290400eMojimi4 years ago | 1267 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1268 | Description |
4290400eMojimi4 years ago | 1269 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1270 | #### Inputs |
4290400eMojimi4 years ago | 1271 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1272 | ***name: tensor(type)*** |
4290400eMojimi4 years ago | 1273 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1274 | Description |
4290400eMojimi4 years ago | 1275 | |
| 1276 | #### Outputs | |
| 1277 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1278 | ***name: tensor(type)*** |
4290400eMojimi4 years ago | 1279 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1280 | Description |
4290400eMojimi4 years ago | 1281 | |
| 1282 | #### Examples | |
| 1283 | | |
| 1284 | | |
| 1285 | ```python | |
| 1286 | | |
| 1287 | node = onnx.helper.make_node( | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1288 | 'StringRegexReplace', |
| 1289 | inputs=['text', 'pattern', 'rewrite'], | |
| 1290 | outputs=['y'], | |
4290400eMojimi4 years ago | 1291 | ) |
| 1292 | | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1293 | text = np.array([['def myfunc():'], ['def dummy():']]) |
| 1294 | pattern = np.array([r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):']) | |
| 1295 | rewrite = np.array([r'static PyObject* py_\1(void) {']) | |
| 1296 | y = [['static PyObject* py_myfunc(void) {'], | |
| 1297 | ['static PyObject* py_dummy(void) {']] | |
4a0f8929Mojimi5 years ago | 1298 | |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1299 | expect(node, inputs=[text, pattern, rewrite], outputs=[y], |
| 1300 | name='test_string_regex_replace') | |
4a0f8929Mojimi5 years ago | 1301 | ``` |
f12f42a0Nat Kershaw (MSFT)4 years ago | 1302 | |
4a0f8929Mojimi5 years ago | 1303 | </details> |
d853d31fRandySheriffH2 years ago | 1304 | |
| 1305 | | |
| 1306 | ## Azure operators | |
3c22daa4Wenbing Li2 years ago | 1307 | Starting from onnxruntime-extensions 0.12, these Azure operators will be removed from the official onnxruntime-extensions packages. However, they can still be built from source using `cmake -DOCOS_ENABLE_AZURE=ON ...`. |
d853d31fRandySheriffH2 years ago | 1308 | ### OpenAIAudioToText |
| 1309 | | |
| 1310 | <details> | |
| 1311 | <summary>OpenAIAudioToText details</summary> | |
| 1312 | | |
| 1313 | | |
| 1314 | OpenAIAudioToText operator talks to [openAI audio](https://platform.openai.com/docs/api-reference/audio) endpoints. | |
| 1315 | | |
| 1316 | | |
| 1317 | #### Attributes | |
| 1318 | | |
| 1319 | ***model_uri:string*** | |
| 1320 | | |
| 1321 | Endpoint uri, like "https://api.openai.com/v1/audio/transcriptions". | |
| 1322 | | |
| 1323 | ***audio_format:string*** | |
| 1324 | | |
| 1325 | The format of the audio, by default "wav". | |
| 1326 | | |
| 1327 | #### Inputs | |
| 1328 | | |
| 1329 | ***auth_token: tensor(string)*** | |
| 1330 | | |
| 1331 | An access token comes with openAI subscription. | |
| 1332 | | |
| 1333 | ***model_name: tensor(string)*** | |
| 1334 | | |
| 1335 | Model name to send to the endpoint, such as "whisper-1". | |
| 1336 | | |
| 1337 | ***response_format: tensor(string)*** | |
| 1338 | | |
| 1339 | Expected format of the response, either be "text" or "json". | |
| 1340 | | |
| 1341 | ***audio_blob: tensor(uint8)*** | |
| 1342 | | |
| 1343 | A byte array containing raw data from the audio file. | |
| 1344 | | |
| 1345 | #### Outputs | |
| 1346 | | |
| 1347 | ***transcriptions: tensor(string)*** | |
| 1348 | | |
| 1349 | | |
| 1350 | #### Examples | |
| 1351 | | |
4cc985faRandySheriffH2 years ago | 1352 | Note - OpenAIAudioToText operator composes a request based on last part of the input and output names split by "/", |
| 1353 | | |
| 1354 | Meaning for input names, they must be of format: | |
| 1355 | - auth_token: "whatever-name-you-want-to-use" | |
| 1356 | - model_name: ".../.../.../model_name" | |
| 1357 | - response_format: ".../.../.../response_format" | |
| 1358 | - audio_blob: ".../.../.../file" | |
| 1359 | | |
| 1360 | for output name, it must be of format: | |
| 1361 | - transcriptions: ".../.../.../transcriptions" | |
| 1362 | | |
| 1363 | Hence there could be multiple OpenAIAudioToText operators accepting different inputs inside a model, and give varied outputs. | |
| 1364 | | |
| 1365 | Pls find sample code below for a better illustration. | |
| 1366 | | |
d853d31fRandySheriffH2 years ago | 1367 | |
| 1368 | ```python | |
| 1369 | | |
| 1370 | import os | |
| 1371 | import numpy as np | |
| 1372 | | |
| 1373 | from onnx import * | |
| 1374 | from onnxruntime_extensions import PyOrtFunction, util, get_library_path | |
| 1375 | from onnxruntime import * | |
| 1376 | | |
| 1377 | | |
4cc985faRandySheriffH2 years ago | 1378 | openai_model_uri = os.getenv('URI', '') # read uri from env |
| 1379 | openai_auth_token = os.getenv('AUTH', '') # read auto token from env | |
| 1380 | | |
| 1381 | | |
d853d31fRandySheriffH2 years ago | 1382 | def create_openai_audio_model(): |
| 1383 | auth_token = helper.make_tensor_value_info('auth_token', TensorProto.STRING, [1]) | |
4cc985faRandySheriffH2 years ago | 1384 | model = helper.make_tensor_value_info('node_1/model_name', TensorProto.STRING, [1]) |
| 1385 | response_format = helper.make_tensor_value_info('node_1/response_format', TensorProto.STRING, [-1]) | |
| 1386 | file = helper.make_tensor_value_info('node_1/file', TensorProto.UINT8, [-1]) | |
| 1387 | transcriptions = helper.make_tensor_value_info('node_1/transcriptions', TensorProto.STRING, [-1]) | |
d853d31fRandySheriffH2 years ago | 1388 | |
| 1389 | invoker = helper.make_node('OpenAIAudioToText', | |
4cc985faRandySheriffH2 years ago | 1390 | ['auth_token', 'node_1/model_name', 'node_1/response_format', 'node_1/file'], # names must follow the format |
| 1391 | ['node_1/transcriptions'], # names must follow the format | |
d853d31fRandySheriffH2 years ago | 1392 | domain='com.microsoft.extensions', |
| 1393 | name='audio_invoker', | |
4cc985faRandySheriffH2 years ago | 1394 | model_uri=openai_model_uri, |
d853d31fRandySheriffH2 years ago | 1395 | audio_format='wav') |
| 1396 | | |
| 1397 | graph = helper.make_graph([invoker], 'graph', [auth_token, model, response_format, file], [transcriptions]) | |
| 1398 | model = helper.make_model(graph, | |
| 1399 | opset_imports=[helper.make_operatorsetid('com.microsoft.extensions', 1)]) | |
| 1400 | | |
| 1401 | onnx.save(model, 'openai_audio.onnx') | |
| 1402 | | |
| 1403 | | |
| 1404 | create_openai_audio_model() | |
| 1405 | opt = SessionOptions() | |
| 1406 | opt.register_custom_ops_library(get_library_path()) | |
| 1407 | sess = InferenceSession(os.path.join(test_data_dir, "openai_audio.onnx"), | |
| 1408 | opt, providers=["CPUExecutionProvider", "AzureExecutionProvider"]) | |
4cc985faRandySheriffH2 years ago | 1409 | auth_token = np.array([openai_auth_token]) |
d853d31fRandySheriffH2 years ago | 1410 | model = np.array(['whisper-1']) |
| 1411 | response_format = np.array(['text']) | |
| 1412 | | |
| 1413 | with open(os.path.join(test_data_dir, "test16.wav"), "rb") as _f: | |
| 1414 | audio_blob = np.asarray(list(_f.read()), dtype=np.uint8) | |
| 1415 | ort_inputs = { | |
| 1416 | "auth_token": auth_token, | |
4cc985faRandySheriffH2 years ago | 1417 | "node_1/model_name": model, |
| 1418 | "node_1/response_format": response_format, | |
| 1419 | "node_1/file": audio_blob, | |
d853d31fRandySheriffH2 years ago | 1420 | } |
| 1421 | out = sess.run(None, ort_inputs)[0] | |
| 1422 | ``` | |
| 1423 | </details> | |
| 1424 | | |
| 1425 | | |
| 1426 | ### AzureTextToText | |
| 1427 | | |
| 1428 | <details> | |
| 1429 | <summary>AzureTextToText details</summary> | |
| 1430 | | |
| 1431 | | |
| 1432 | AzureTextToText talks to a GPT model hosted by [Azure openAI service](https://learn.microsoft.com/en-us/azure/ai-services/openai/). | |
| 1433 | | |
| 1434 | | |
| 1435 | #### Attributes | |
| 1436 | | |
| 1437 | ***model_uri:string*** | |
| 1438 | | |
| 1439 | Endpoint uri, like "https://myname-aoai-test.openai.azure.com/openai/deployments/mydeploy/chat/completions?api-version=2023-05-15'". | |
| 1440 | | |
| 1441 | #### Inputs | |
| 1442 | | |
| 1443 | ***auth_token: tensor(string)*** | |
| 1444 | | |
| 1445 | An access token comes with Azure openAI subscription. | |
| 1446 | | |
| 1447 | ***chat: tensor(string)*** | |
| 1448 | | |
| 1449 | A json string in requested [format](https://learn.microsoft.com/en-us/azure/ai-services/openai/chatgpt-quickstart?tabs=command-line&pivots=rest-api). | |
| 1450 | | |
| 1451 | #### Outputs | |
| 1452 | | |
| 1453 | ***response_format: tensor(string)*** | |
| 1454 | | |
| 1455 | A json string as response. | |
| 1456 | | |
| 1457 | | |
| 1458 | #### Examples | |
| 1459 | | |
| 1460 | | |
| 1461 | ```python | |
| 1462 | | |
| 1463 | import os | |
| 1464 | import numpy as np | |
| 1465 | | |
| 1466 | from onnx import * | |
| 1467 | from onnxruntime_extensions import PyOrtFunction, util, get_library_path | |
| 1468 | from onnxruntime import * | |
| 1469 | | |
| 1470 | | |
4cc985faRandySheriffH2 years ago | 1471 | azure_model_uri = os.getenv('URI', '') # read uri from env |
| 1472 | azure_auth_token = os.getenv('AUTH', '') # read auto token from env | |
| 1473 | | |
| 1474 | | |
d853d31fRandySheriffH2 years ago | 1475 | def create_azure_chat_model(): |
| 1476 | auth_token = helper.make_tensor_value_info('auth_token', TensorProto.STRING, [-1]) | |
| 1477 | chat = helper.make_tensor_value_info('chat', TensorProto.STRING, [-1]) | |
| 1478 | response = helper.make_tensor_value_info('response', TensorProto.STRING, [-1]) | |
| 1479 | | |
| 1480 | invoker = helper.make_node('AzureTextToText', ['auth_token', 'chat'], ['response'], | |
| 1481 | domain='com.microsoft.extensions', | |
| 1482 | name='chat_invoker', | |
4cc985faRandySheriffH2 years ago | 1483 | model_uri=azure_model_uri) |
d853d31fRandySheriffH2 years ago | 1484 | |
| 1485 | graph = helper.make_graph([invoker], 'graph', [auth_token, chat], [response]) | |
| 1486 | model = helper.make_model(graph, | |
| 1487 | opset_imports=[helper.make_operatorsetid('com.microsoft.extensions', 1)]) | |
| 1488 | | |
| 1489 | onnx.save(model, 'azure_chat.onnx') | |
| 1490 | | |
| 1491 | | |
| 1492 | create_azure_chat_model() | |
| 1493 | opt = SessionOptions() | |
| 1494 | opt.register_custom_ops_library(get_library_path()) | |
| 1495 | sess = InferenceSession(os.path.join(test_data_dir, "azure_chat.onnx"), opt, providers=["CPUExecutionProvider", "AzureExecutionProvider"]) | |
4cc985faRandySheriffH2 years ago | 1496 | auth_token = np.array([azure_auth_token]) |
d853d31fRandySheriffH2 years ago | 1497 | chat = np.array([r'{"messages":[{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Does Azure OpenAI support customer managed keys?"},{"role": "assistant", "content": "Yes, customer managed keys are supported by Azure OpenAI."},{"role": "user", "content": "Do other Azure AI services support this too?"}]}']) |
| 1498 | ort_inputs = { | |
| 1499 | "auth_token": auth_token, | |
| 1500 | "chat": chat, | |
| 1501 | } | |
| 1502 | out = sess.run(None, ort_inputs)[0] | |
| 1503 | ``` | |
| 1504 | </details> | |
| 1505 | | |
| 1506 | | |
| 1507 | ### AzureTritonInvoker | |
| 1508 | | |
| 1509 | <details> | |
| 1510 | <summary>AzureTritonInvoker details</summary> | |
| 1511 | | |
| 1512 | | |
| 1513 | AzureTritonInvoker talks to [Azure Machine Learning triton services](https://learn.microsoft.com/en-us/azure/machine-learning/how-to-deploy-with-triton?view=azureml-api-2&tabs=azure-cli%2Cendpoint). | |
| 1514 | | |
| 1515 | | |
| 1516 | #### Attributes | |
| 1517 | | |
| 1518 | ***model_uri:string*** | |
| 1519 | | |
| 1520 | Endpoint uri, like "'https://endpoint-12345678.westus.inference.ml.azure.com". | |
| 1521 | | |
| 1522 | ***model_name:string*** | |
| 1523 | | |
| 1524 | ***model_version:string*** | |
| 1525 | | |
| 1526 | A version string, like "1", or "2". | |
| 1527 | | |
| 1528 | #### Inputs | |
| 1529 | | |
| 1530 | ***auth_token: tensor(string)*** | |
| 1531 | | |
| 1532 | An access token comes with Azure Machine Learning model deployment. | |
| 1533 | | |
| 1534 | ***inputs: tensor(variadic)*** | |
| 1535 | | |
| 1536 | Tensors of any supported onnx data type. | |
| 1537 | | |
| 1538 | #### Outputs | |
| 1539 | | |
| 1540 | ***outputs: tensor(variadic)*** | |
| 1541 | | |
| 1542 | Tensors of any supported onnx data type. | |
| 1543 | | |
| 1544 | | |
| 1545 | #### Examples | |
| 1546 | | |
| 1547 | | |
| 1548 | ```python | |
| 1549 | | |
| 1550 | import os | |
| 1551 | import numpy as np | |
| 1552 | | |
| 1553 | from onnx import * | |
| 1554 | from onnxruntime_extensions import PyOrtFunction, util, get_library_path | |
| 1555 | from onnxruntime import * | |
| 1556 | | |
| 1557 | | |
4cc985faRandySheriffH2 years ago | 1558 | triton_uri = os.getenv('URI', '') # read uri from env |
| 1559 | triton_auth_token = os.getenv('AUTH', '') # read auto token from env | |
| 1560 | | |
| 1561 | | |
d853d31fRandySheriffH2 years ago | 1562 | def createAddf(): |
| 1563 | auth_token = helper.make_tensor_value_info('auth_token', TensorProto.STRING, [-1]) | |
| 1564 | X = helper.make_tensor_value_info('X', TensorProto.FLOAT, [-1]) | |
| 1565 | Y = helper.make_tensor_value_info('Y', TensorProto.FLOAT, [-1]) | |
| 1566 | Z = helper.make_tensor_value_info('Z', TensorProto.FLOAT, [-1]) | |
| 1567 | invoker = helper.make_node('AzureTritonInvoker', ['auth_token', 'X', 'Y'], ['Z'], | |
| 1568 | domain='com.microsoft.extensions', name='triton_invoker', | |
4cc985faRandySheriffH2 years ago | 1569 | model_uri=triton_uri, |
d853d31fRandySheriffH2 years ago | 1570 | model_name='addf', model_version='1') |
| 1571 | graph = helper.make_graph([invoker], 'graph', [auth_token, X, Y], [Z]) | |
| 1572 | model = helper.make_model(graph, | |
| 1573 | opset_imports=[helper.make_operatorsetid('com.microsoft.extensions', 1)]) | |
| 1574 | save(model, 'triton_addf.onnx') | |
| 1575 | | |
| 1576 | | |
| 1577 | def run_add_f(): | |
| 1578 | opt = SessionOptions() | |
| 1579 | opt.register_custom_ops_library(get_library_path()) | |
| 1580 | sess = InferenceSession(os.path.join(test_data_dir, "triton_addf.onnx"), | |
| 1581 | opt, providers=["CPUExecutionProvider", "AzureExecutionProvider"]) | |
4cc985faRandySheriffH2 years ago | 1582 | auth_token = np.array([triton_auth_token]) |
d853d31fRandySheriffH2 years ago | 1583 | x = np.array([1,2,3,4]).astype(np.float32) |
| 1584 | y = np.array([4,3,2,1]).astype(np.float32) | |
| 1585 | ort_inputs = { | |
| 1586 | "auth_token": auth_token, | |
| 1587 | "X": x, | |
| 1588 | "Y": y | |
| 1589 | } | |
| 1590 | out = sess.run(None, ort_inputs)[0] | |
| 1591 | ``` | |
85ffb941Zhipeng Han1 years ago | 1592 | </details> |