Add MarkupLM (#19198)
* First draft * Make basic test work * Fix most tokenizer tests * More improvements * Make more tests pass * Fix more tests * Fix some code quality * Improve truncation * Implement feature extractor * Improve feature extractor and add tests * Improve feature extractor tests * Fix pair_input test partly * Add fast tokenizer * Improve implementation * Fix rebase * Fix rebase * Fix most of the tokenizer tests. * propose solution for fast * add: integration test for fasttokenizer, warning for decode, fix template in slow tokenizer * add: modify markuplmconverter * add: some modify on converter and tokenizerfast * Fix style, copies * Make fixup * Update tokenization_markuplm.py * Update test_tokenization_markuplm.py * Update markuplm related * Improve processor, add integration test * Add processor test file * Improve processor * Improve processor tests * Fix more processor tests * Fix processor tests * Update docstrings * Add Copied from statements * Add more Copied from statements * Add code examples * Improve code examples * Add model to doc tests * Adding dependency check * Add dummy file * Add requires_backends * Add model to toctree * Fix more things, disable dependency check for now * Apply more suggestions * Add soft dependency * Add annotators to tests * Fix style * Remove from_slow=True * Remove print statements * Add sanity check * Fix processor test * Fix processor tests, add more docs * Add doc tests for mdx file * Add more tips * Apply suggestions Co-authored-by: Niels Rogge <nielsrogge@Nielss-MacBook-Pro.local> Co-authored-by: lockon-n <45759388+lockon-n@users.noreply.github.com> Co-authored-by: SaulLu <lucilesaul.com@gmail.com> Co-authored-by: lockon-n <dd098309@126.com>
This commit is contained in:
parent
49d62b0178
commit
f3d2f7a6e0
|
@ -328,6 +328,7 @@ Current number of checkpoints: ![](https://img.shields.io/endpoint?url=https://h
|
|||
1. **[M-CTC-T](https://huggingface.co/docs/transformers/model_doc/mctct)** (from Facebook) released with the paper [Pseudo-Labeling For Massively Multilingual Speech Recognition](https://arxiv.org/abs/2111.00161) by Loren Lugosch, Tatiana Likhomanenko, Gabriel Synnaeve, and Ronan Collobert.
|
||||
1. **[M2M100](https://huggingface.co/docs/transformers/model_doc/m2m_100)** (from Facebook) released with the paper [Beyond English-Centric Multilingual Machine Translation](https://arxiv.org/abs/2010.11125) by Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin.
|
||||
1. **[MarianMT](https://huggingface.co/docs/transformers/model_doc/marian)** Machine translation models trained using [OPUS](http://opus.nlpl.eu/) data by Jörg Tiedemann. The [Marian Framework](https://marian-nmt.github.io/) is being developed by the Microsoft Translator Team.
|
||||
1. **[MarkupLM](https://huggingface.co/docs/transformers/main/model_doc/markuplm)** (from Microsoft Research Asia) released with the paper [MarkupLM: Pre-training of Text and Markup Language for Visually-rich Document Understanding](https://arxiv.org/abs/2110.08518) by Junlong Li, Yiheng Xu, Lei Cui, Furu Wei.
|
||||
1. **[MaskFormer](https://huggingface.co/docs/transformers/model_doc/maskformer)** (from Meta and UIUC) released with the paper [Per-Pixel Classification is Not All You Need for Semantic Segmentation](https://arxiv.org/abs/2107.06278) by Bowen Cheng, Alexander G. Schwing, Alexander Kirillov.
|
||||
1. **[mBART](https://huggingface.co/docs/transformers/model_doc/mbart)** (from Facebook) released with the paper [Multilingual Denoising Pre-training for Neural Machine Translation](https://arxiv.org/abs/2001.08210) by Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer.
|
||||
1. **[mBART-50](https://huggingface.co/docs/transformers/model_doc/mbart)** (from Facebook) released with the paper [Multilingual Translation with Extensible Multilingual Pretraining and Finetuning](https://arxiv.org/abs/2008.00401) by Yuqing Tang, Chau Tran, Xian Li, Peng-Jen Chen, Naman Goyal, Vishrav Chaudhary, Jiatao Gu, Angela Fan.
|
||||
|
|
|
@ -278,6 +278,7 @@ Flax, PyTorch, TensorFlow 설치 페이지에서 이들을 conda로 설치하는
|
|||
1. **[M-CTC-T](https://huggingface.co/docs/transformers/model_doc/mctct)** (from Facebook) released with the paper [Pseudo-Labeling For Massively Multilingual Speech Recognition](https://arxiv.org/abs/2111.00161) by Loren Lugosch, Tatiana Likhomanenko, Gabriel Synnaeve, and Ronan Collobert.
|
||||
1. **[M2M100](https://huggingface.co/docs/transformers/model_doc/m2m_100)** (from Facebook) released with the paper [Beyond English-Centric Multilingual Machine Translation](https://arxiv.org/abs/2010.11125) by Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin.
|
||||
1. **[MarianMT](https://huggingface.co/docs/transformers/model_doc/marian)** Machine translation models trained using [OPUS](http://opus.nlpl.eu/) data by Jörg Tiedemann. The [Marian Framework](https://marian-nmt.github.io/) is being developed by the Microsoft Translator Team.
|
||||
1. **[MarkupLM](https://huggingface.co/docs/transformers/main/model_doc/markuplm)** (from Microsoft Research Asia) released with the paper [MarkupLM: Pre-training of Text and Markup Language for Visually-rich Document Understanding](https://arxiv.org/abs/2110.08518) by Junlong Li, Yiheng Xu, Lei Cui, Furu Wei.
|
||||
1. **[MaskFormer](https://huggingface.co/docs/transformers/model_doc/maskformer)** (from Meta and UIUC) released with the paper [Per-Pixel Classification is Not All You Need for Semantic Segmentation](https://arxiv.org/abs/2107.06278) by Bowen Cheng, Alexander G. Schwing, Alexander Kirillov.
|
||||
1. **[mBART](https://huggingface.co/docs/transformers/model_doc/mbart)** (from Facebook) released with the paper [Multilingual Denoising Pre-training for Neural Machine Translation](https://arxiv.org/abs/2001.08210) by Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer.
|
||||
1. **[mBART-50](https://huggingface.co/docs/transformers/model_doc/mbart)** (from Facebook) released with the paper [Multilingual Translation with Extensible Multilingual Pretraining and Finetuning](https://arxiv.org/abs/2008.00401) by Yuqing Tang, Chau Tran, Xian Li, Peng-Jen Chen, Naman Goyal, Vishrav Chaudhary, Jiatao Gu, Angela Fan.
|
||||
|
|
|
@ -302,7 +302,8 @@ conda install -c huggingface transformers
|
|||
1. **[M-CTC-T](https://huggingface.co/docs/transformers/model_doc/mctct)** (来自 Facebook) 伴随论文 [Pseudo-Labeling For Massively Multilingual Speech Recognition](https://arxiv.org/abs/2111.00161) 由 Loren Lugosch, Tatiana Likhomanenko, Gabriel Synnaeve, and Ronan Collobert 发布。
|
||||
1. **[M2M100](https://huggingface.co/docs/transformers/model_doc/m2m_100)** (来自 Facebook) 伴随论文 [Beyond English-Centric Multilingual Machine Translation](https://arxiv.org/abs/2010.11125) 由 Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin 发布。
|
||||
1. **[MarianMT](https://huggingface.co/docs/transformers/model_doc/marian)** 用 [OPUS](http://opus.nlpl.eu/) 数据训练的机器翻译模型由 Jörg Tiedemann 发布。[Marian Framework](https://marian-nmt.github.io/) 由微软翻译团队开发。
|
||||
1. **[MaskFormer](https://huggingface.co/docs/transformers/model_doc/maskformer)** (from Meta and UIUC) released with the paper [Per-Pixel Classification is Not All You Need for Semantic Segmentation](https://arxiv.org/abs/2107.06278) by Bowen Cheng, Alexander G. Schwing, Alexander Kirillov
|
||||
1. **[MarkupLM](https://huggingface.co/docs/transformers/main/model_doc/markuplm)** (来自 Microsoft Research Asia) 伴随论文 [MarkupLM: Pre-training of Text and Markup Language for Visually-rich Document Understanding](https://arxiv.org/abs/2110.08518) 由 Junlong Li, Yiheng Xu, Lei Cui, Furu Wei 发布。
|
||||
1. **[MaskFormer](https://huggingface.co/docs/transformers/model_doc/maskformer)** (from Meta and UIUC) released with the paper [Per-Pixel Classification is Not All You Need for Semantic Segmentation](https://arxiv.org/abs/2107.06278) by Bowen Cheng, Alexander G. Schwing, Alexander Kirillov >>>>>>> Fix rebase
|
||||
1. **[mBART](https://huggingface.co/docs/transformers/model_doc/mbart)** (来自 Facebook) 伴随论文 [Multilingual Denoising Pre-training for Neural Machine Translation](https://arxiv.org/abs/2001.08210) 由 Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer 发布。
|
||||
1. **[mBART-50](https://huggingface.co/docs/transformers/model_doc/mbart)** (来自 Facebook) 伴随论文 [Multilingual Translation with Extensible Multilingual Pretraining and Finetuning](https://arxiv.org/abs/2008.00401) 由 Yuqing Tang, Chau Tran, Xian Li, Peng-Jen Chen, Naman Goyal, Vishrav Chaudhary, Jiatao Gu, Angela Fan 发布。
|
||||
1. **[Megatron-BERT](https://huggingface.co/docs/transformers/model_doc/megatron-bert)** (来自 NVIDIA) 伴随论文 [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://arxiv.org/abs/1909.08053) 由 Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper and Bryan Catanzaro 发布。
|
||||
|
|
|
@ -314,6 +314,7 @@ conda install -c huggingface transformers
|
|||
1. **[M-CTC-T](https://huggingface.co/docs/transformers/model_doc/mctct)** (from Facebook) released with the paper [Pseudo-Labeling For Massively Multilingual Speech Recognition](https://arxiv.org/abs/2111.00161) by Loren Lugosch, Tatiana Likhomanenko, Gabriel Synnaeve, and Ronan Collobert.
|
||||
1. **[M2M100](https://huggingface.co/docs/transformers/model_doc/m2m_100)** (from Facebook) released with the paper [Beyond English-Centric Multilingual Machine Translation](https://arxiv.org/abs/2010.11125) by Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin.
|
||||
1. **[MarianMT](https://huggingface.co/docs/transformers/model_doc/marian)** Machine translation models trained using [OPUS](http://opus.nlpl.eu/) data by Jörg Tiedemann. The [Marian Framework](https://marian-nmt.github.io/) is being developed by the Microsoft Translator Team.
|
||||
1. **[MarkupLM](https://huggingface.co/docs/transformers/main/model_doc/markuplm)** (from Microsoft Research Asia) released with the paper [MarkupLM: Pre-training of Text and Markup Language for Visually-rich Document Understanding](https://arxiv.org/abs/2110.08518) by Junlong Li, Yiheng Xu, Lei Cui, Furu Wei.
|
||||
1. **[MaskFormer](https://huggingface.co/docs/transformers/model_doc/maskformer)** (from Meta and UIUC) released with the paper [Per-Pixel Classification is Not All You Need for Semantic Segmentation](https://arxiv.org/abs/2107.06278) by Bowen Cheng, Alexander G. Schwing, Alexander Kirillov
|
||||
1. **[mBART](https://huggingface.co/docs/transformers/model_doc/mbart)** (from Facebook) released with the paper [Multilingual Denoising Pre-training for Neural Machine Translation](https://arxiv.org/abs/2001.08210) by Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer.
|
||||
1. **[mBART-50](https://huggingface.co/docs/transformers/model_doc/mbart)** (from Facebook) released with the paper [Multilingual Translation with Extensible Multilingual Pretraining and Finetuning](https://arxiv.org/abs/2008.00401) by Yuqing Tang, Chau Tran, Xian Li, Peng-Jen Chen, Naman Goyal, Vishrav Chaudhary, Jiatao Gu, Angela Fan.
|
||||
|
|
|
@ -279,6 +279,8 @@
|
|||
title: M2M100
|
||||
- local: model_doc/marian
|
||||
title: MarianMT
|
||||
- local: model_doc/markuplm
|
||||
title: MarkupLM
|
||||
- local: model_doc/mbart
|
||||
title: MBart and MBart-50
|
||||
- local: model_doc/megatron-bert
|
||||
|
|
|
@ -118,6 +118,7 @@ The documentation is organized into five sections:
|
|||
1. **[M-CTC-T](model_doc/mctct)** (from Facebook) released with the paper [Pseudo-Labeling For Massively Multilingual Speech Recognition](https://arxiv.org/abs/2111.00161) by Loren Lugosch, Tatiana Likhomanenko, Gabriel Synnaeve, and Ronan Collobert.
|
||||
1. **[M2M100](model_doc/m2m_100)** (from Facebook) released with the paper [Beyond English-Centric Multilingual Machine Translation](https://arxiv.org/abs/2010.11125) by Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin.
|
||||
1. **[MarianMT](model_doc/marian)** Machine translation models trained using [OPUS](http://opus.nlpl.eu/) data by Jörg Tiedemann. The [Marian Framework](https://marian-nmt.github.io/) is being developed by the Microsoft Translator Team.
|
||||
1. **[MarkupLM](model_doc/markuplm)** (from Microsoft Research Asia) released with the paper [MarkupLM: Pre-training of Text and Markup Language for Visually-rich Document Understanding](https://arxiv.org/abs/2110.08518) by Junlong Li, Yiheng Xu, Lei Cui, Furu Wei.
|
||||
1. **[MaskFormer](model_doc/maskformer)** (from Meta and UIUC) released with the paper [Per-Pixel Classification is Not All You Need for Semantic Segmentation](https://arxiv.org/abs/2107.06278) by Bowen Cheng, Alexander G. Schwing, Alexander Kirillov.
|
||||
1. **[mBART](model_doc/mbart)** (from Facebook) released with the paper [Multilingual Denoising Pre-training for Neural Machine Translation](https://arxiv.org/abs/2001.08210) by Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer.
|
||||
1. **[mBART-50](model_doc/mbart)** (from Facebook) released with the paper [Multilingual Translation with Extensible Multilingual Pretraining and Finetuning](https://arxiv.org/abs/2008.00401) by Yuqing Tang, Chau Tran, Xian Li, Peng-Jen Chen, Naman Goyal, Vishrav Chaudhary, Jiatao Gu, Angela Fan.
|
||||
|
@ -264,6 +265,7 @@ Flax), PyTorch, and/or TensorFlow.
|
|||
| M-CTC-T | ❌ | ❌ | ✅ | ❌ | ❌ |
|
||||
| M2M100 | ✅ | ❌ | ✅ | ❌ | ❌ |
|
||||
| Marian | ✅ | ❌ | ✅ | ✅ | ✅ |
|
||||
| MarkupLM | ✅ | ✅ | ✅ | ❌ | ❌ |
|
||||
| MaskFormer | ❌ | ❌ | ✅ | ❌ | ❌ |
|
||||
| mBART | ✅ | ✅ | ✅ | ✅ | ✅ |
|
||||
| Megatron-BERT | ❌ | ❌ | ✅ | ❌ | ❌ |
|
||||
|
|
|
@ -0,0 +1,246 @@
|
|||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
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.
|
||||
-->
|
||||
|
||||
# MarkupLM
|
||||
|
||||
## Overview
|
||||
|
||||
The MarkupLM model was proposed in [MarkupLM: Pre-training of Text and Markup Language for Visually-rich Document
|
||||
Understanding](https://arxiv.org/abs/2110.08518) by Junlong Li, Yiheng Xu, Lei Cui, Furu Wei. MarkupLM is BERT, but
|
||||
applied to HTML pages instead of raw text documents. The model incorporates additional embedding layers to improve
|
||||
performance, similar to [LayoutLM](layoutlm).
|
||||
|
||||
The model can be used for tasks like question answering on web pages or information extraction from web pages. It obtains
|
||||
state-of-the-art results on 2 important benchmarks:
|
||||
- [WebSRC](https://x-lance.github.io/WebSRC/), a dataset for Web-Based Structual Reading Comprehension (a bit like SQuAD but for web pages)
|
||||
- [SWDE](https://www.researchgate.net/publication/221299838_From_one_tree_to_a_forest_a_unified_solution_for_structured_web_data_extraction), a dataset
|
||||
for information extraction from web pages (basically named-entity recogntion on web pages)
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Multimodal pre-training with text, layout, and image has made significant progress for Visually-rich Document
|
||||
Understanding (VrDU), especially the fixed-layout documents such as scanned document images. While, there are still a
|
||||
large number of digital documents where the layout information is not fixed and needs to be interactively and
|
||||
dynamically rendered for visualization, making existing layout-based pre-training approaches not easy to apply. In this
|
||||
paper, we propose MarkupLM for document understanding tasks with markup languages as the backbone such as
|
||||
HTML/XML-based documents, where text and markup information is jointly pre-trained. Experiment results show that the
|
||||
pre-trained MarkupLM significantly outperforms the existing strong baseline models on several document understanding
|
||||
tasks. The pre-trained model and code will be publicly available.*
|
||||
|
||||
Tips:
|
||||
- In addition to `input_ids`, [`~MarkupLMModel.forward`] expects 2 additional inputs, namely `xpath_tags_seq` and `xpath_subs_seq`.
|
||||
These are the XPATH tags and subscripts respectively for each token in the input sequence.
|
||||
- One can use [`MarkupLMProcessor`] to prepare all data for the model. Refer to the [usage guide](#usage-markuplmprocessor) for more info.
|
||||
- Demo notebooks can be found [here](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/MarkupLM).
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/markuplm_architecture.jpg"
|
||||
alt="drawing" width="600"/>
|
||||
|
||||
<small> MarkupLM architecture. Taken from the <a href="https://arxiv.org/abs/2110.08518">original paper.</a> </small>
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr). The original code can be found [here](https://github.com/microsoft/unilm/tree/master/markuplm).
|
||||
|
||||
## Usage: MarkupLMProcessor
|
||||
|
||||
The easiest way to prepare data for the model is to use [`MarkupLMProcessor`], which internally combines a feature extractor
|
||||
([`MarkupLMFeatureExtractor`]) and a tokenizer ([`MarkupLMTokenizer`] or [`MarkupLMTokenizerFast`]). The feature extractor is
|
||||
used to extract all nodes and xpaths from the HTML strings, which are then provided to the tokenizer, which turns them into the
|
||||
token-level inputs of the model (`input_ids` etc.). Note that you can still use the feature extractor and tokenizer separately,
|
||||
if you only want to handle one of the two tasks.
|
||||
|
||||
```python
|
||||
from transformers import MarkupLMFeatureExtractor, MarkupLMTokenizerFast, MarkupLMProcessor
|
||||
|
||||
feature_extractor = MarkupLMFeatureExtractor()
|
||||
tokenizer = MarkupLMTokenizerFast.from_pretrained("microsoft/markuplm-base")
|
||||
processor = MarkupLMProcessor(feature_extractor, tokenizer)
|
||||
```
|
||||
|
||||
In short, one can provide HTML strings (and possibly additional data) to [`MarkupLMProcessor`],
|
||||
and it will create the inputs expected by the model. Internally, the processor first uses
|
||||
[`MarkupLMFeatureExtractor`] to get a list of nodes and corresponding xpaths. The nodes and
|
||||
xpaths are then provided to [`MarkupLMTokenizer`] or [`MarkupLMTokenizerFast`], which converts them
|
||||
to token-level `input_ids`, `attention_mask`, `token_type_ids`, `xpath_subs_seq`, `xpath_tags_seq`.
|
||||
Optionally, one can provide node labels to the processor, which are turned into token-level `labels`.
|
||||
|
||||
[`MarkupLMFeatureExtractor`] uses [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/bs4/doc/), a Python library for
|
||||
pulling data out of HTML and XML files, under the hood. Note that you can still use your own parsing solution of
|
||||
choice, and provide the nodes and xpaths yourself to [`MarkupLMTokenizer`] or [`MarkupLMTokenizerFast`].
|
||||
|
||||
In total, there are 5 use cases that are supported by the processor. Below, we list them all. Note that each of these
|
||||
use cases work for both batched and non-batched inputs (we illustrate them for non-batched inputs).
|
||||
|
||||
**Use case 1: web page classification (training, inference) + token classification (inference), parse_html = True**
|
||||
|
||||
This is the simplest case, in which the processor will use the feature extractor to get all nodes and xpaths from the HTML.
|
||||
|
||||
```python
|
||||
>>> from transformers import MarkupLMProcessor
|
||||
|
||||
>>> processor = MarkupLMProcessor.from_pretrained("microsoft/markuplm-base")
|
||||
|
||||
>>> html_string = """
|
||||
... <!DOCTYPE html>
|
||||
... <html>
|
||||
... <head>
|
||||
... <title>Hello world</title>
|
||||
... </head>
|
||||
... <body>
|
||||
|
||||
... <h1>Welcome</h1>
|
||||
... <p>Here is my website.</p>
|
||||
|
||||
... </body>
|
||||
... </html>"""
|
||||
|
||||
>>> # note that you can also add provide all tokenizer parameters here such as padding, truncation
|
||||
>>> encoding = processor(html_string, return_tensors="pt")
|
||||
>>> print(encoding.keys())
|
||||
dict_keys(['input_ids', 'token_type_ids', 'attention_mask', 'xpath_tags_seq', 'xpath_subs_seq'])
|
||||
```
|
||||
|
||||
**Use case 2: web page classification (training, inference) + token classification (inference), parse_html=False**
|
||||
|
||||
In case one already has obtained all nodes and xpaths, one doesn't need the feature extractor. In that case, one should
|
||||
provide the nodes and corresponding xpaths themselves to the processor, and make sure to set `parse_html` to `False`.
|
||||
|
||||
```python
|
||||
>>> from transformers import MarkupLMProcessor
|
||||
|
||||
>>> processor = MarkupLMProcessor.from_pretrained("microsoft/markuplm-base")
|
||||
>>> processor.parse_html = False
|
||||
|
||||
>>> nodes = ["hello", "world", "how", "are"]
|
||||
>>> xpaths = ["/html/body/div/li[1]/div/span", "/html/body/div/li[1]/div/span", "html/body", "html/body/div"]
|
||||
>>> encoding = processor(nodes=nodes, xpaths=xpaths, return_tensors="pt")
|
||||
>>> print(encoding.keys())
|
||||
dict_keys(['input_ids', 'token_type_ids', 'attention_mask', 'xpath_tags_seq', 'xpath_subs_seq'])
|
||||
```
|
||||
|
||||
**Use case 3: token classification (training), parse_html=False**
|
||||
|
||||
For token classification tasks (such as [SWDE](https://paperswithcode.com/dataset/swde)), one can also provide the
|
||||
corresponding node labels in order to train a model. The processor will then convert these into token-level `labels`.
|
||||
By default, it will only label the first wordpiece of a word, and label the remaining wordpieces with -100, which is the
|
||||
`ignore_index` of PyTorch's CrossEntropyLoss. In case you want all wordpieces of a word to be labeled, you can
|
||||
initialize the tokenizer with `only_label_first_subword` set to `False`.
|
||||
|
||||
```python
|
||||
>>> from transformers import MarkupLMProcessor
|
||||
|
||||
>>> processor = MarkupLMProcessor.from_pretrained("microsoft/markuplm-base")
|
||||
>>> processor.parse_html = False
|
||||
|
||||
>>> nodes = ["hello", "world", "how", "are"]
|
||||
>>> xpaths = ["/html/body/div/li[1]/div/span", "/html/body/div/li[1]/div/span", "html/body", "html/body/div"]
|
||||
>>> node_labels = [1, 2, 2, 1]
|
||||
>>> encoding = processor(nodes=nodes, xpaths=xpaths, node_labels=node_labels, return_tensors="pt")
|
||||
>>> print(encoding.keys())
|
||||
dict_keys(['input_ids', 'token_type_ids', 'attention_mask', 'xpath_tags_seq', 'xpath_subs_seq', 'labels'])
|
||||
```
|
||||
|
||||
**Use case 4: web page question answering (inference), parse_html=True**
|
||||
|
||||
For question answering tasks on web pages, you can provide a question to the processor. By default, the
|
||||
processor will use the feature extractor to get all nodes and xpaths, and create [CLS] question tokens [SEP] word tokens [SEP].
|
||||
|
||||
```python
|
||||
>>> from transformers import MarkupLMProcessor
|
||||
|
||||
>>> processor = MarkupLMProcessor.from_pretrained("microsoft/markuplm-base")
|
||||
|
||||
>>> html_string = """
|
||||
... <!DOCTYPE html>
|
||||
... <html>
|
||||
... <head>
|
||||
... <title>Hello world</title>
|
||||
... </head>
|
||||
... <body>
|
||||
|
||||
... <h1>Welcome</h1>
|
||||
... <p>My name is Niels.</p>
|
||||
|
||||
... </body>
|
||||
... </html>"""
|
||||
|
||||
>>> question = "What's his name?"
|
||||
>>> encoding = processor(html_string, questions=question, return_tensors="pt")
|
||||
>>> print(encoding.keys())
|
||||
dict_keys(['input_ids', 'token_type_ids', 'attention_mask', 'xpath_tags_seq', 'xpath_subs_seq'])
|
||||
```
|
||||
|
||||
**Use case 5: web page question answering (inference), apply_ocr=False**
|
||||
|
||||
For question answering tasks (such as WebSRC), you can provide a question to the processor. If you have extracted
|
||||
all nodes and xpaths yourself, you can provide them directly to the processor. Make sure to set `parse_html` to `False`.
|
||||
|
||||
```python
|
||||
>>> from transformers import MarkupLMProcessor
|
||||
|
||||
>>> processor = MarkupLMProcessor.from_pretrained("microsoft/markuplm-base")
|
||||
>>> processor.parse_html = False
|
||||
|
||||
>>> nodes = ["hello", "world", "how", "are"]
|
||||
>>> xpaths = ["/html/body/div/li[1]/div/span", "/html/body/div/li[1]/div/span", "html/body", "html/body/div"]
|
||||
>>> question = "What's his name?"
|
||||
>>> encoding = processor(nodes=nodes, xpaths=xpaths, questions=question, return_tensors="pt")
|
||||
>>> print(encoding.keys())
|
||||
dict_keys(['input_ids', 'token_type_ids', 'attention_mask', 'xpath_tags_seq', 'xpath_subs_seq'])
|
||||
```
|
||||
|
||||
## MarkupLMConfig
|
||||
|
||||
[[autodoc]] MarkupLMConfig
|
||||
- all
|
||||
|
||||
## MarkupLMFeatureExtractor
|
||||
|
||||
[[autodoc]] MarkupLMFeatureExtractor
|
||||
- __call__
|
||||
|
||||
## MarkupLMTokenizer
|
||||
|
||||
[[autodoc]] MarkupLMTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## MarkupLMTokenizerFast
|
||||
|
||||
[[autodoc]] MarkupLMTokenizerFast
|
||||
- all
|
||||
|
||||
## MarkupLMProcessor
|
||||
|
||||
[[autodoc]] MarkupLMProcessor
|
||||
- __call__
|
||||
|
||||
## MarkupLMModel
|
||||
|
||||
[[autodoc]] MarkupLMModel
|
||||
- forward
|
||||
|
||||
## MarkupLMForSequenceClassification
|
||||
|
||||
[[autodoc]] MarkupLMForSequenceClassification
|
||||
- forward
|
||||
|
||||
## MarkupLMForTokenClassification
|
||||
|
||||
[[autodoc]] MarkupLMForTokenClassification
|
||||
- forward
|
||||
|
||||
## MarkupLMForQuestionAnswering
|
||||
|
||||
[[autodoc]] MarkupLMForQuestionAnswering
|
||||
- forward
|
|
@ -262,6 +262,13 @@ _import_structure = {
|
|||
"models.lxmert": ["LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "LxmertConfig", "LxmertTokenizer"],
|
||||
"models.m2m_100": ["M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP", "M2M100Config"],
|
||||
"models.marian": ["MarianConfig"],
|
||||
"models.markuplm": [
|
||||
"MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP",
|
||||
"MarkupLMConfig",
|
||||
"MarkupLMFeatureExtractor",
|
||||
"MarkupLMProcessor",
|
||||
"MarkupLMTokenizer",
|
||||
],
|
||||
"models.maskformer": ["MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "MaskFormerConfig"],
|
||||
"models.mbart": ["MBartConfig"],
|
||||
"models.mbart50": [],
|
||||
|
@ -570,6 +577,7 @@ else:
|
|||
_import_structure["models.led"].append("LEDTokenizerFast")
|
||||
_import_structure["models.longformer"].append("LongformerTokenizerFast")
|
||||
_import_structure["models.lxmert"].append("LxmertTokenizerFast")
|
||||
_import_structure["models.markuplm"].append("MarkupLMTokenizerFast")
|
||||
_import_structure["models.mbart"].append("MBartTokenizerFast")
|
||||
_import_structure["models.mbart50"].append("MBart50TokenizerFast")
|
||||
_import_structure["models.mobilebert"].append("MobileBertTokenizerFast")
|
||||
|
@ -1488,6 +1496,16 @@ else:
|
|||
"MaskFormerPreTrainedModel",
|
||||
]
|
||||
)
|
||||
_import_structure["models.markuplm"].extend(
|
||||
[
|
||||
"MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST",
|
||||
"MarkupLMForQuestionAnswering",
|
||||
"MarkupLMForSequenceClassification",
|
||||
"MarkupLMForTokenClassification",
|
||||
"MarkupLMModel",
|
||||
"MarkupLMPreTrainedModel",
|
||||
]
|
||||
)
|
||||
_import_structure["models.mbart"].extend(
|
||||
[
|
||||
"MBartForCausalLM",
|
||||
|
@ -3192,6 +3210,13 @@ if TYPE_CHECKING:
|
|||
from .models.lxmert import LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, LxmertConfig, LxmertTokenizer
|
||||
from .models.m2m_100 import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, M2M100Config
|
||||
from .models.marian import MarianConfig
|
||||
from .models.markuplm import (
|
||||
MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||
MarkupLMConfig,
|
||||
MarkupLMFeatureExtractor,
|
||||
MarkupLMProcessor,
|
||||
MarkupLMTokenizer,
|
||||
)
|
||||
from .models.maskformer import MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskFormerConfig
|
||||
from .models.mbart import MBartConfig
|
||||
from .models.mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig, MCTCTProcessor
|
||||
|
@ -3465,6 +3490,7 @@ if TYPE_CHECKING:
|
|||
from .models.led import LEDTokenizerFast
|
||||
from .models.longformer import LongformerTokenizerFast
|
||||
from .models.lxmert import LxmertTokenizerFast
|
||||
from .models.markuplm import MarkupLMTokenizerFast
|
||||
from .models.mbart import MBartTokenizerFast
|
||||
from .models.mbart50 import MBart50TokenizerFast
|
||||
from .models.mobilebert import MobileBertTokenizerFast
|
||||
|
@ -4196,6 +4222,14 @@ if TYPE_CHECKING:
|
|||
M2M100PreTrainedModel,
|
||||
)
|
||||
from .models.marian import MarianForCausalLM, MarianModel, MarianMTModel
|
||||
from .models.markuplm import (
|
||||
MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST,
|
||||
MarkupLMForQuestionAnswering,
|
||||
MarkupLMForSequenceClassification,
|
||||
MarkupLMForTokenClassification,
|
||||
MarkupLMModel,
|
||||
MarkupLMPreTrainedModel,
|
||||
)
|
||||
from .models.maskformer import (
|
||||
MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
|
||||
MaskFormerForInstanceSegmentation,
|
||||
|
|
|
@ -1043,6 +1043,44 @@ class XGLMConverter(SpmConverter):
|
|||
)
|
||||
|
||||
|
||||
class MarkupLMConverter(Converter):
|
||||
def converted(self) -> Tokenizer:
|
||||
ot = self.original_tokenizer
|
||||
vocab = ot.encoder
|
||||
merges = list(ot.bpe_ranks.keys())
|
||||
|
||||
tokenizer = Tokenizer(
|
||||
BPE(
|
||||
vocab=vocab,
|
||||
merges=merges,
|
||||
dropout=None,
|
||||
continuing_subword_prefix="",
|
||||
end_of_word_suffix="",
|
||||
fuse_unk=False,
|
||||
unk_token=self.original_tokenizer.unk_token,
|
||||
)
|
||||
)
|
||||
|
||||
tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=ot.add_prefix_space)
|
||||
tokenizer.decoder = decoders.ByteLevel()
|
||||
|
||||
cls = str(self.original_tokenizer.cls_token)
|
||||
sep = str(self.original_tokenizer.sep_token)
|
||||
cls_token_id = self.original_tokenizer.cls_token_id
|
||||
sep_token_id = self.original_tokenizer.sep_token_id
|
||||
|
||||
tokenizer.post_processor = processors.TemplateProcessing(
|
||||
single=f"{cls} $A {sep}",
|
||||
pair=f"{cls} $A {sep} $B {sep}",
|
||||
special_tokens=[
|
||||
(cls, cls_token_id),
|
||||
(sep, sep_token_id),
|
||||
],
|
||||
)
|
||||
|
||||
return tokenizer
|
||||
|
||||
|
||||
SLOW_TO_FAST_CONVERTERS = {
|
||||
"AlbertTokenizer": AlbertConverter,
|
||||
"BartTokenizer": RobertaConverter,
|
||||
|
@ -1072,6 +1110,7 @@ SLOW_TO_FAST_CONVERTERS = {
|
|||
"LongformerTokenizer": RobertaConverter,
|
||||
"LEDTokenizer": RobertaConverter,
|
||||
"LxmertTokenizer": BertConverter,
|
||||
"MarkupLMTokenizer": MarkupLMConverter,
|
||||
"MBartTokenizer": MBartConverter,
|
||||
"MBart50Tokenizer": MBart50Converter,
|
||||
"MPNetTokenizer": MPNetConverter,
|
||||
|
|
|
@ -79,6 +79,7 @@ from .utils import (
|
|||
has_file,
|
||||
http_user_agent,
|
||||
is_apex_available,
|
||||
is_bs4_available,
|
||||
is_coloredlogs_available,
|
||||
is_datasets_available,
|
||||
is_detectron2_available,
|
||||
|
|
|
@ -88,6 +88,7 @@ from . import (
|
|||
lxmert,
|
||||
m2m_100,
|
||||
marian,
|
||||
markuplm,
|
||||
maskformer,
|
||||
mbart,
|
||||
mbart50,
|
||||
|
|
|
@ -90,6 +90,7 @@ CONFIG_MAPPING_NAMES = OrderedDict(
|
|||
("lxmert", "LxmertConfig"),
|
||||
("m2m_100", "M2M100Config"),
|
||||
("marian", "MarianConfig"),
|
||||
("markuplm", "MarkupLMConfig"),
|
||||
("maskformer", "MaskFormerConfig"),
|
||||
("mbart", "MBartConfig"),
|
||||
("mctct", "MCTCTConfig"),
|
||||
|
@ -221,6 +222,7 @@ CONFIG_ARCHIVE_MAP_MAPPING_NAMES = OrderedDict(
|
|||
("luke", "LUKE_PRETRAINED_CONFIG_ARCHIVE_MAP"),
|
||||
("lxmert", "LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP"),
|
||||
("m2m_100", "M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP"),
|
||||
("markuplm", "MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP"),
|
||||
("maskformer", "MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP"),
|
||||
("mbart", "MBART_PRETRAINED_CONFIG_ARCHIVE_MAP"),
|
||||
("mctct", "MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP"),
|
||||
|
@ -357,6 +359,7 @@ MODEL_NAMES_MAPPING = OrderedDict(
|
|||
("lxmert", "LXMERT"),
|
||||
("m2m_100", "M2M100"),
|
||||
("marian", "Marian"),
|
||||
("markuplm", "MarkupLM"),
|
||||
("maskformer", "MaskFormer"),
|
||||
("mbart", "mBART"),
|
||||
("mbart50", "mBART-50"),
|
||||
|
|
|
@ -89,6 +89,7 @@ MODEL_MAPPING_NAMES = OrderedDict(
|
|||
("lxmert", "LxmertModel"),
|
||||
("m2m_100", "M2M100Model"),
|
||||
("marian", "MarianModel"),
|
||||
("markuplm", "MarkupLMModel"),
|
||||
("maskformer", "MaskFormerModel"),
|
||||
("mbart", "MBartModel"),
|
||||
("mctct", "MCTCTModel"),
|
||||
|
@ -247,6 +248,7 @@ MODEL_WITH_LM_HEAD_MAPPING_NAMES = OrderedDict(
|
|||
("luke", "LukeForMaskedLM"),
|
||||
("m2m_100", "M2M100ForConditionalGeneration"),
|
||||
("marian", "MarianMTModel"),
|
||||
("markuplm", "MarkupLMForMaskedLM"),
|
||||
("megatron-bert", "MegatronBertForCausalLM"),
|
||||
("mobilebert", "MobileBertForMaskedLM"),
|
||||
("mpnet", "MPNetForMaskedLM"),
|
||||
|
@ -530,6 +532,7 @@ MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES = OrderedDict(
|
|||
("led", "LEDForSequenceClassification"),
|
||||
("longformer", "LongformerForSequenceClassification"),
|
||||
("luke", "LukeForSequenceClassification"),
|
||||
("markuplm", "MarkupLMForSequenceClassification"),
|
||||
("mbart", "MBartForSequenceClassification"),
|
||||
("megatron-bert", "MegatronBertForSequenceClassification"),
|
||||
("mobilebert", "MobileBertForSequenceClassification"),
|
||||
|
@ -585,6 +588,7 @@ MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES = OrderedDict(
|
|||
("longformer", "LongformerForQuestionAnswering"),
|
||||
("luke", "LukeForQuestionAnswering"),
|
||||
("lxmert", "LxmertForQuestionAnswering"),
|
||||
("markuplm", "MarkupLMForQuestionAnswering"),
|
||||
("mbart", "MBartForQuestionAnswering"),
|
||||
("megatron-bert", "MegatronBertForQuestionAnswering"),
|
||||
("mobilebert", "MobileBertForQuestionAnswering"),
|
||||
|
@ -654,6 +658,7 @@ MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES = OrderedDict(
|
|||
("layoutlmv3", "LayoutLMv3ForTokenClassification"),
|
||||
("longformer", "LongformerForTokenClassification"),
|
||||
("luke", "LukeForTokenClassification"),
|
||||
("markuplm", "MarkupLMForTokenClassification"),
|
||||
("megatron-bert", "MegatronBertForTokenClassification"),
|
||||
("mobilebert", "MobileBertForTokenClassification"),
|
||||
("mpnet", "MPNetForTokenClassification"),
|
||||
|
|
|
@ -46,6 +46,7 @@ PROCESSOR_MAPPING_NAMES = OrderedDict(
|
|||
("layoutlmv2", "LayoutLMv2Processor"),
|
||||
("layoutlmv3", "LayoutLMv3Processor"),
|
||||
("layoutxlm", "LayoutXLMProcessor"),
|
||||
("markuplm", "MarkupLMProcessor"),
|
||||
("owlvit", "OwlViTProcessor"),
|
||||
("sew", "Wav2Vec2Processor"),
|
||||
("sew-d", "Wav2Vec2Processor"),
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
# flake8: noqa
|
||||
# There's no way to ignore "F401 '...' imported but unused" warnings in this
|
||||
# module, but to preserve other warnings. So, don't check this module at all.
|
||||
|
||||
# Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
#
|
||||
# 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.
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
# rely on isort to merge the imports
|
||||
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available
|
||||
|
||||
|
||||
_import_structure = {
|
||||
"configuration_markuplm": ["MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "MarkupLMConfig"],
|
||||
"feature_extraction_markuplm": ["MarkupLMFeatureExtractor"],
|
||||
"processing_markuplm": ["MarkupLMProcessor"],
|
||||
"tokenization_markuplm": ["MarkupLMTokenizer"],
|
||||
}
|
||||
|
||||
try:
|
||||
if not is_tokenizers_available():
|
||||
raise OptionalDependencyNotAvailable()
|
||||
except OptionalDependencyNotAvailable:
|
||||
pass
|
||||
else:
|
||||
_import_structure["tokenization_markuplm_fast"] = ["MarkupLMTokenizerFast"]
|
||||
|
||||
try:
|
||||
if not is_torch_available():
|
||||
raise OptionalDependencyNotAvailable()
|
||||
except OptionalDependencyNotAvailable:
|
||||
pass
|
||||
else:
|
||||
_import_structure["modeling_markuplm"] = [
|
||||
"MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST",
|
||||
"MarkupLMForQuestionAnswering",
|
||||
"MarkupLMForSequenceClassification",
|
||||
"MarkupLMForTokenClassification",
|
||||
"MarkupLMModel",
|
||||
"MarkupLMPreTrainedModel",
|
||||
]
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .configuration_markuplm import MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP, MarkupLMConfig
|
||||
from .feature_extraction_markuplm import MarkupLMFeatureExtractor
|
||||
from .processing_markuplm import MarkupLMProcessor
|
||||
from .tokenization_markuplm import MarkupLMTokenizer
|
||||
|
||||
try:
|
||||
if not is_tokenizers_available():
|
||||
raise OptionalDependencyNotAvailable()
|
||||
except OptionalDependencyNotAvailable:
|
||||
pass
|
||||
else:
|
||||
from .tokenization_markuplm_fast import MarkupLMTokenizerFast
|
||||
|
||||
try:
|
||||
if not is_torch_available():
|
||||
raise OptionalDependencyNotAvailable()
|
||||
except OptionalDependencyNotAvailable:
|
||||
pass
|
||||
else:
|
||||
from .modeling_markuplm import (
|
||||
MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST,
|
||||
MarkupLMForQuestionAnswering,
|
||||
MarkupLMForSequenceClassification,
|
||||
MarkupLMForTokenClassification,
|
||||
MarkupLMModel,
|
||||
MarkupLMPreTrainedModel,
|
||||
)
|
||||
|
||||
|
||||
else:
|
||||
import sys
|
||||
|
||||
sys.modules[__name__] = _LazyModule(__name__, globals()["__file__"], _import_structure)
|
|
@ -0,0 +1,151 @@
|
|||
# coding=utf-8
|
||||
# Copyright 2021, The Microsoft Research Asia MarkupLM Team authors
|
||||
#
|
||||
# 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.
|
||||
""" MarkupLM model configuration"""
|
||||
|
||||
from transformers.models.roberta.configuration_roberta import RobertaConfig
|
||||
from transformers.utils import logging
|
||||
|
||||
|
||||
logger = logging.get_logger(__name__)
|
||||
|
||||
MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP = {
|
||||
"microsoft/markuplm-base": "https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json",
|
||||
"microsoft/markuplm-large": "https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json",
|
||||
}
|
||||
|
||||
|
||||
class MarkupLMConfig(RobertaConfig):
|
||||
r"""
|
||||
This is the configuration class to store the configuration of a [`MarkupLMModel`]. It is used to instantiate a
|
||||
MarkupLM 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 MarkupLM
|
||||
[microsoft/markuplm-base-uncased](https://huggingface.co/microsoft/markuplm-base-uncased) architecture.
|
||||
|
||||
Configuration objects inherit from [`BertConfig`] and can be used to control the model outputs. Read the
|
||||
documentation from [`BertConfig`] for more information.
|
||||
|
||||
Args:
|
||||
vocab_size (`int`, *optional*, defaults to 30522):
|
||||
Vocabulary size of the MarkupLM model. Defines the different tokens that can be represented by the
|
||||
*inputs_ids* passed to the forward method of [`MarkupLMModel`].
|
||||
hidden_size (`int`, *optional*, defaults to 768):
|
||||
Dimensionality of the encoder layers and the pooler layer.
|
||||
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.
|
||||
intermediate_size (`int`, *optional*, defaults to 3072):
|
||||
Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
|
||||
hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
|
||||
The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
|
||||
`"relu"`, `"silu"` and `"gelu_new"` are supported.
|
||||
hidden_dropout_prob (`float`, *optional*, defaults to 0.1):
|
||||
The dropout probability 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 into [`MarkupLMModel`].
|
||||
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.
|
||||
gradient_checkpointing (`bool`, *optional*, defaults to `False`):
|
||||
If True, use gradient checkpointing to save memory at the expense of slower backward pass.
|
||||
max_tree_id_unit_embeddings (`int`, *optional*, defaults to 1024):
|
||||
The maximum value that the tree id unit embedding might ever use. Typically set this to something large
|
||||
just in case (e.g., 1024).
|
||||
max_xpath_tag_unit_embeddings (`int`, *optional*, defaults to 256):
|
||||
The maximum value that the xpath tag unit embedding might ever use. Typically set this to something large
|
||||
just in case (e.g., 256).
|
||||
max_xpath_subs_unit_embeddings (`int`, *optional*, defaults to 1024):
|
||||
The maximum value that the xpath subscript unit embedding might ever use. Typically set this to something
|
||||
large just in case (e.g., 1024).
|
||||
tag_pad_id (`int`, *optional*, defaults to 216):
|
||||
The id of the padding token in the xpath tags.
|
||||
subs_pad_id (`int`, *optional*, defaults to 1001):
|
||||
The id of the padding token in the xpath subscripts.
|
||||
xpath_tag_unit_hidden_size (`int`, *optional*, defaults to 32):
|
||||
The hidden size of each tree id unit. One complete tree index will have
|
||||
(50*xpath_tag_unit_hidden_size)-dim.
|
||||
max_depth (`int`, *optional*, defaults to 50):
|
||||
The maximum depth in xpath.
|
||||
|
||||
Examples:
|
||||
|
||||
```python
|
||||
>>> from transformers import MarkupLMModel, MarkupLMConfig
|
||||
|
||||
>>> # Initializing a MarkupLM microsoft/markuplm-base style configuration
|
||||
>>> configuration = MarkupLMConfig()
|
||||
|
||||
>>> # Initializing a model from the microsoft/markuplm-base style configuration
|
||||
>>> model = MarkupLMModel(configuration)
|
||||
|
||||
>>> # Accessing the model configuration
|
||||
>>> configuration = model.config
|
||||
```"""
|
||||
model_type = "markuplm"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
vocab_size=30522,
|
||||
hidden_size=768,
|
||||
num_hidden_layers=12,
|
||||
num_attention_heads=12,
|
||||
intermediate_size=3072,
|
||||
hidden_act="gelu",
|
||||
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,
|
||||
pad_token_id=0,
|
||||
gradient_checkpointing=False,
|
||||
max_xpath_tag_unit_embeddings=256,
|
||||
max_xpath_subs_unit_embeddings=1024,
|
||||
tag_pad_id=216,
|
||||
subs_pad_id=1001,
|
||||
xpath_unit_hidden_size=32,
|
||||
max_depth=50,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__(
|
||||
vocab_size=vocab_size,
|
||||
hidden_size=hidden_size,
|
||||
num_hidden_layers=num_hidden_layers,
|
||||
num_attention_heads=num_attention_heads,
|
||||
intermediate_size=intermediate_size,
|
||||
hidden_act=hidden_act,
|
||||
hidden_dropout_prob=hidden_dropout_prob,
|
||||
attention_probs_dropout_prob=attention_probs_dropout_prob,
|
||||
max_position_embeddings=max_position_embeddings,
|
||||
type_vocab_size=type_vocab_size,
|
||||
initializer_range=initializer_range,
|
||||
layer_norm_eps=layer_norm_eps,
|
||||
pad_token_id=pad_token_id,
|
||||
gradient_checkpointing=gradient_checkpointing,
|
||||
**kwargs,
|
||||
)
|
||||
# additional properties
|
||||
self.max_depth = max_depth
|
||||
self.max_xpath_tag_unit_embeddings = max_xpath_tag_unit_embeddings
|
||||
self.max_xpath_subs_unit_embeddings = max_xpath_subs_unit_embeddings
|
||||
self.tag_pad_id = tag_pad_id
|
||||
self.subs_pad_id = subs_pad_id
|
||||
self.xpath_unit_hidden_size = xpath_unit_hidden_size
|
|
@ -0,0 +1,183 @@
|
|||
# coding=utf-8
|
||||
# Copyright 2022 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.
|
||||
"""
|
||||
Feature extractor class for MarkupLM.
|
||||
"""
|
||||
|
||||
import html
|
||||
|
||||
from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin
|
||||
from ...utils import is_bs4_available, logging, requires_backends
|
||||
|
||||
|
||||
if is_bs4_available():
|
||||
import bs4
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
|
||||
logger = logging.get_logger(__name__)
|
||||
|
||||
|
||||
class MarkupLMFeatureExtractor(FeatureExtractionMixin):
|
||||
r"""
|
||||
Constructs a MarkupLM feature extractor. This can be used to get a list of nodes and corresponding xpaths from HTML
|
||||
strings.
|
||||
|
||||
This feature extractor inherits from [`~feature_extraction_utils.PreTrainedFeatureExtractor`] which contains most
|
||||
of the main methods. Users should refer to this superclass for more information regarding those methods.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
requires_backends(self, ["bs4"])
|
||||
super().__init__(**kwargs)
|
||||
|
||||
def xpath_soup(self, element):
|
||||
xpath_tags = []
|
||||
xpath_subscripts = []
|
||||
child = element if element.name else element.parent
|
||||
for parent in child.parents: # type: bs4.element.Tag
|
||||
siblings = parent.find_all(child.name, recursive=False)
|
||||
xpath_tags.append(child.name)
|
||||
xpath_subscripts.append(
|
||||
0 if 1 == len(siblings) else next(i for i, s in enumerate(siblings, 1) if s is child)
|
||||
)
|
||||
child = parent
|
||||
xpath_tags.reverse()
|
||||
xpath_subscripts.reverse()
|
||||
return xpath_tags, xpath_subscripts
|
||||
|
||||
def get_three_from_single(self, html_string):
|
||||
html_code = BeautifulSoup(html_string, "html.parser")
|
||||
|
||||
all_doc_strings = []
|
||||
string2xtag_seq = []
|
||||
string2xsubs_seq = []
|
||||
|
||||
for element in html_code.descendants:
|
||||
if type(element) == bs4.element.NavigableString:
|
||||
if type(element.parent) != bs4.element.Tag:
|
||||
continue
|
||||
|
||||
text_in_this_tag = html.unescape(element).strip()
|
||||
if not text_in_this_tag:
|
||||
continue
|
||||
|
||||
all_doc_strings.append(text_in_this_tag)
|
||||
|
||||
xpath_tags, xpath_subscripts = self.xpath_soup(element)
|
||||
string2xtag_seq.append(xpath_tags)
|
||||
string2xsubs_seq.append(xpath_subscripts)
|
||||
|
||||
if len(all_doc_strings) != len(string2xtag_seq):
|
||||
raise ValueError("Number of doc strings and xtags does not correspond")
|
||||
if len(all_doc_strings) != len(string2xsubs_seq):
|
||||
raise ValueError("Number of doc strings and xsubs does not correspond")
|
||||
|
||||
return all_doc_strings, string2xtag_seq, string2xsubs_seq
|
||||
|
||||
def construct_xpath(self, xpath_tags, xpath_subscripts):
|
||||
xpath = ""
|
||||
for tagname, subs in zip(xpath_tags, xpath_subscripts):
|
||||
xpath += f"/{tagname}"
|
||||
if subs != 0:
|
||||
xpath += f"[{subs}]"
|
||||
return xpath
|
||||
|
||||
def __call__(self, html_strings) -> BatchFeature:
|
||||
"""
|
||||
Main method to prepare for the model one or several HTML strings.
|
||||
|
||||
Args:
|
||||
html_strings (`str`, `List[str]`):
|
||||
The HTML string or batch of HTML strings from which to extract nodes and corresponding xpaths.
|
||||
|
||||
Returns:
|
||||
[`BatchFeature`]: A [`BatchFeature`] with the following fields:
|
||||
|
||||
- **nodes** -- Nodes.
|
||||
- **xpaths** -- Corresponding xpaths.
|
||||
|
||||
Examples:
|
||||
|
||||
```python
|
||||
>>> from transformers import MarkupLMFeatureExtractor
|
||||
|
||||
>>> page_name_1 = "page1.html"
|
||||
>>> page_name_2 = "page2.html"
|
||||
>>> page_name_3 = "page3.html"
|
||||
|
||||
>>> with open(page_name_1) as f:
|
||||
... single_html_string = f.read()
|
||||
|
||||
>>> feature_extractor = MarkupLMFeatureExtractor()
|
||||
|
||||
>>> # single example
|
||||
>>> encoding = feature_extractor(single_html_string)
|
||||
>>> print(encoding.keys())
|
||||
>>> # dict_keys(['nodes', 'xpaths'])
|
||||
|
||||
>>> # batched example
|
||||
|
||||
>>> multi_html_strings = []
|
||||
|
||||
>>> with open(page_name_2) as f:
|
||||
... multi_html_strings.append(f.read())
|
||||
>>> with open(page_name_3) as f:
|
||||
... multi_html_strings.append(f.read())
|
||||
|
||||
>>> encoding = feature_extractor(multi_html_strings)
|
||||
>>> print(encoding.keys())
|
||||
>>> # dict_keys(['nodes', 'xpaths'])
|
||||
```"""
|
||||
|
||||
# Input type checking for clearer error
|
||||
valid_strings = False
|
||||
|
||||
# Check that strings has a valid type
|
||||
if isinstance(html_strings, str):
|
||||
valid_strings = True
|
||||
elif isinstance(html_strings, (list, tuple)):
|
||||
if len(html_strings) == 0 or isinstance(html_strings[0], str):
|
||||
valid_strings = True
|
||||
|
||||
if not valid_strings:
|
||||
raise ValueError(
|
||||
"HTML strings must of type `str`, `List[str]` (batch of examples), "
|
||||
f"but is of type {type(html_strings)}."
|
||||
)
|
||||
|
||||
is_batched = bool(isinstance(html_strings, (list, tuple)) and (isinstance(html_strings[0], str)))
|
||||
|
||||
if not is_batched:
|
||||
html_strings = [html_strings]
|
||||
|
||||
# Get nodes + xpaths
|
||||
nodes = []
|
||||
xpaths = []
|
||||
for html_string in html_strings:
|
||||
all_doc_strings, string2xtag_seq, string2xsubs_seq = self.get_three_from_single(html_string)
|
||||
nodes.append(all_doc_strings)
|
||||
xpath_strings = []
|
||||
for node, tag_list, sub_list in zip(all_doc_strings, string2xtag_seq, string2xsubs_seq):
|
||||
xpath_string = self.construct_xpath(tag_list, sub_list)
|
||||
xpath_strings.append(xpath_string)
|
||||
xpaths.append(xpath_strings)
|
||||
|
||||
# return as Dict
|
||||
data = {"nodes": nodes, "xpaths": xpaths}
|
||||
encoded_inputs = BatchFeature(data=data, tensor_type=None)
|
||||
|
||||
return encoded_inputs
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,140 @@
|
|||
# coding=utf-8
|
||||
# Copyright 2022 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.
|
||||
"""
|
||||
Processor class for MarkupLM.
|
||||
"""
|
||||
from typing import Optional, Union
|
||||
|
||||
from ...file_utils import TensorType
|
||||
from ...processing_utils import ProcessorMixin
|
||||
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, TruncationStrategy
|
||||
|
||||
|
||||
class MarkupLMProcessor(ProcessorMixin):
|
||||
r"""
|
||||
Constructs a MarkupLM processor which combines a MarkupLM feature extractor and a MarkupLM tokenizer into a single
|
||||
processor.
|
||||
|
||||
[`MarkupLMProcessor`] offers all the functionalities you need to prepare data for the model.
|
||||
|
||||
It first uses [`MarkupLMFeatureExtractor`] to extract nodes and corresponding xpaths from one or more HTML strings.
|
||||
Next, these are provided to [`MarkupLMTokenizer`] or [`MarkupLMTokenizerFast`], which turns them into token-level
|
||||
`input_ids`, `attention_mask`, `token_type_ids`, `xpath_tags_seq` and `xpath_subs_seq`.
|
||||
|
||||
Args:
|
||||
feature_extractor (`MarkupLMFeatureExtractor`):
|
||||
An instance of [`MarkupLMFeatureExtractor`]. The feature extractor is a required input.
|
||||
tokenizer (`MarkupLMTokenizer` or `MarkupLMTokenizerFast`):
|
||||
An instance of [`MarkupLMTokenizer`] or [`MarkupLMTokenizerFast`]. The tokenizer is a required input.
|
||||
parse_html (`bool`, *optional*, defaults to `True`):
|
||||
Whether or not to use `MarkupLMFeatureExtractor` to parse HTML strings into nodes and corresponding xpaths.
|
||||
"""
|
||||
feature_extractor_class = "MarkupLMFeatureExtractor"
|
||||
tokenizer_class = ("MarkupLMTokenizer", "MarkupLMTokenizerFast")
|
||||
parse_html = True
|
||||
|
||||
def __call__(
|
||||
self,
|
||||
html_strings=None,
|
||||
nodes=None,
|
||||
xpaths=None,
|
||||
node_labels=None,
|
||||
questions=None,
|
||||
add_special_tokens: bool = True,
|
||||
padding: Union[bool, str, PaddingStrategy] = False,
|
||||
truncation: Union[bool, str, TruncationStrategy] = False,
|
||||
max_length: Optional[int] = None,
|
||||
stride: int = 0,
|
||||
pad_to_multiple_of: Optional[int] = None,
|
||||
return_token_type_ids: Optional[bool] = None,
|
||||
return_attention_mask: Optional[bool] = None,
|
||||
return_overflowing_tokens: bool = False,
|
||||
return_special_tokens_mask: bool = False,
|
||||
return_offsets_mapping: bool = False,
|
||||
return_length: bool = False,
|
||||
verbose: bool = True,
|
||||
return_tensors: Optional[Union[str, TensorType]] = None,
|
||||
**kwargs
|
||||
) -> BatchEncoding:
|
||||
"""
|
||||
This method first forwards the `html_strings` argument to [`~MarkupLMFeatureExtractor.__call__`]. Next, it
|
||||
passes the `nodes` and `xpaths` along with the additional arguments to [`~MarkupLMTokenizer.__call__`] and
|
||||
returns the output.
|
||||
|
||||
Optionally, one can also provide a `text` argument which is passed along as first sequence.
|
||||
|
||||
Please refer to the docstring of the above two methods for more information.
|
||||
"""
|
||||
# first, create nodes and xpaths
|
||||
if self.parse_html:
|
||||
if html_strings is None:
|
||||
raise ValueError("Make sure to pass HTML strings in case `parse_html` is set to `True`")
|
||||
|
||||
if nodes is not None or xpaths is not None or node_labels is not None:
|
||||
raise ValueError(
|
||||
"Please don't pass nodes, xpaths nor node labels in case `parse_html` is set to `True`"
|
||||
)
|
||||
|
||||
features = self.feature_extractor(html_strings)
|
||||
nodes = features["nodes"]
|
||||
xpaths = features["xpaths"]
|
||||
else:
|
||||
if html_strings is not None:
|
||||
raise ValueError("You have passed HTML strings but `parse_html` is set to `False`.")
|
||||
if nodes is None or xpaths is None:
|
||||
raise ValueError("Make sure to pass nodes and xpaths in case `parse_html` is set to `False`")
|
||||
|
||||
# # second, apply the tokenizer
|
||||
if questions is not None and self.parse_html:
|
||||
if isinstance(questions, str):
|
||||
questions = [questions] # add batch dimension (as the feature extractor always adds a batch dimension)
|
||||
|
||||
encoded_inputs = self.tokenizer(
|
||||
text=questions if questions is not None else nodes,
|
||||
text_pair=nodes if questions is not None else None,
|
||||
xpaths=xpaths,
|
||||
node_labels=node_labels,
|
||||
add_special_tokens=add_special_tokens,
|
||||
padding=padding,
|
||||
truncation=truncation,
|
||||
max_length=max_length,
|
||||
stride=stride,
|
||||
pad_to_multiple_of=pad_to_multiple_of,
|
||||
return_token_type_ids=return_token_type_ids,
|
||||
return_attention_mask=return_attention_mask,
|
||||
return_overflowing_tokens=return_overflowing_tokens,
|
||||
return_special_tokens_mask=return_special_tokens_mask,
|
||||
return_offsets_mapping=return_offsets_mapping,
|
||||
return_length=return_length,
|
||||
verbose=verbose,
|
||||
return_tensors=return_tensors,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
return encoded_inputs
|
||||
|
||||
def batch_decode(self, *args, **kwargs):
|
||||
"""
|
||||
This method forwards all its arguments to TrOCRTokenizer's [`~PreTrainedTokenizer.batch_decode`]. Please refer
|
||||
to the docstring of this method for more information.
|
||||
"""
|
||||
return self.tokenizer.batch_decode(*args, **kwargs)
|
||||
|
||||
def decode(self, *args, **kwargs):
|
||||
"""
|
||||
This method forwards all its arguments to TrOCRTokenizer's [`~PreTrainedTokenizer.decode`]. Please refer to the
|
||||
docstring of this method for more information.
|
||||
"""
|
||||
return self.tokenizer.decode(*args, **kwargs)
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,924 @@
|
|||
# coding=utf-8
|
||||
# Copyright 2022 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.
|
||||
"""
|
||||
Fast tokenization class for MarkupLM. It overwrites 2 methods of the slow tokenizer class, namely _batch_encode_plus
|
||||
and _encode_plus, in which the Rust tokenizer is used.
|
||||
"""
|
||||
|
||||
import json
|
||||
from functools import lru_cache
|
||||
from typing import Dict, List, Optional, Tuple, Union
|
||||
|
||||
from tokenizers import pre_tokenizers, processors
|
||||
|
||||
from ...file_utils import PaddingStrategy, TensorType, add_end_docstrings
|
||||
from ...tokenization_utils_base import (
|
||||
ENCODE_KWARGS_DOCSTRING,
|
||||
BatchEncoding,
|
||||
EncodedInput,
|
||||
PreTokenizedInput,
|
||||
TextInput,
|
||||
TextInputPair,
|
||||
TruncationStrategy,
|
||||
)
|
||||
from ...tokenization_utils_fast import PreTrainedTokenizerFast
|
||||
from ...utils import logging
|
||||
from .tokenization_markuplm import MARKUPLM_ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING, MarkupLMTokenizer
|
||||
|
||||
|
||||
logger = logging.get_logger(__name__)
|
||||
|
||||
VOCAB_FILES_NAMES = {"vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_file": "tokenizer.json"}
|
||||
|
||||
PRETRAINED_VOCAB_FILES_MAP = {
|
||||
"vocab_file": {
|
||||
"microsoft/markuplm-base": "https://huggingface.co/microsoft/markuplm-base/resolve/main/vocab.json",
|
||||
"microsoft/markuplm-large": "https://huggingface.co/microsoft/markuplm-large/resolve/main/vocab.json",
|
||||
},
|
||||
"merges_file": {
|
||||
"microsoft/markuplm-base": "https://huggingface.co/microsoft/markuplm-base/resolve/main/merges.txt",
|
||||
"microsoft/markuplm-large": "https://huggingface.co/microsoft/markuplm-large/resolve/main/merges.txt",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES = {
|
||||
"microsoft/markuplm-base": 512,
|
||||
"microsoft/markuplm-large": 512,
|
||||
}
|
||||
|
||||
|
||||
@lru_cache()
|
||||
def bytes_to_unicode():
|
||||
"""
|
||||
Returns list of utf-8 byte and a mapping to unicode strings. We specifically avoids mapping to whitespace/control
|
||||
characters the bpe code barfs on. The reversible bpe codes work on unicode strings. This means you need a large #
|
||||
of unicode characters in your vocab if you want to avoid UNKs. When you're at something like a 10B token dataset
|
||||
you end up needing around 5K for decent coverage. This is a significant percentage of your normal, say, 32K bpe
|
||||
vocab. To avoid that, we want lookup tables between utf-8 bytes and unicode strings.
|
||||
"""
|
||||
bs = (
|
||||
list(range(ord("!"), ord("~") + 1)) + list(range(ord("¡"), ord("¬") + 1)) + list(range(ord("®"), ord("ÿ") + 1))
|
||||
)
|
||||
cs = bs[:]
|
||||
n = 0
|
||||
for b in range(2**8):
|
||||
if b not in bs:
|
||||
bs.append(b)
|
||||
cs.append(2**8 + n)
|
||||
n += 1
|
||||
cs = [chr(n) for n in cs]
|
||||
return dict(zip(bs, cs))
|
||||
|
||||
|
||||
def get_pairs(word):
|
||||
"""
|
||||
Return set of symbol pairs in a word. Word is represented as tuple of symbols (symbols being variable-length
|
||||
strings).
|
||||
"""
|
||||
pairs = set()
|
||||
prev_char = word[0]
|
||||
for char in word[1:]:
|
||||
pairs.add((prev_char, char))
|
||||
prev_char = char
|
||||
return pairs
|
||||
|
||||
|
||||
class MarkupLMTokenizerFast(PreTrainedTokenizerFast):
|
||||
r"""
|
||||
Construct a MarkupLM tokenizer. Based on byte-level Byte-Pair-Encoding (BPE).
|
||||
|
||||
[`MarkupLMTokenizerFast`] can be used to turn HTML strings into to token-level `input_ids`, `attention_mask`,
|
||||
`token_type_ids`, `xpath_tags_seq` and `xpath_tags_seq`. This tokenizer inherits from [`PreTrainedTokenizer`] which
|
||||
contains most of the main methods.
|
||||
|
||||
Users should refer to this superclass for more information regarding those methods.
|
||||
|
||||
Args:
|
||||
vocab_file (`str`):
|
||||
Path to the vocabulary file.
|
||||
merges_file (`str`):
|
||||
Path to the merges file.
|
||||
errors (`str`, *optional*, defaults to `"replace"`):
|
||||
Paradigm to follow when decoding bytes to UTF-8. See
|
||||
[bytes.decode](https://docs.python.org/3/library/stdtypes.html#bytes.decode) for more information.
|
||||
bos_token (`str`, *optional*, defaults to `"<s>"`):
|
||||
The beginning of sequence token that was used during pretraining. Can be used a sequence classifier token.
|
||||
|
||||
<Tip>
|
||||
|
||||
When building a sequence using special tokens, this is not the token that is used for the beginning of
|
||||
sequence. The token used is the `cls_token`.
|
||||
|
||||
</Tip>
|
||||
|
||||
eos_token (`str`, *optional*, defaults to `"</s>"`):
|
||||
The end of sequence token.
|
||||
|
||||
<Tip>
|
||||
|
||||
When building a sequence using special tokens, this is not the token that is used for the end of sequence.
|
||||
The token used is the `sep_token`.
|
||||
|
||||
</Tip>
|
||||
|
||||
sep_token (`str`, *optional*, defaults to `"</s>"`):
|
||||
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for
|
||||
sequence classification or for a text and a question for question answering. It is also used as the last
|
||||
token of a sequence built with special tokens.
|
||||
cls_token (`str`, *optional*, defaults to `"<s>"`):
|
||||
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.
|
||||
unk_token (`str`, *optional*, defaults to `"<unk>"`):
|
||||
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
||||
token instead.
|
||||
pad_token (`str`, *optional*, defaults to `"<pad>"`):
|
||||
The token used for padding, for example when batching sequences of different lengths.
|
||||
mask_token (`str`, *optional*, defaults to `"<mask>"`):
|
||||
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.
|
||||
add_prefix_space (`bool`, *optional*, defaults to `False`):
|
||||
Whether or not to add an initial space to the input. This allows to treat the leading word just as any
|
||||
other word. (RoBERTa tokenizer detect beginning of words by the preceding space).
|
||||
"""
|
||||
|
||||
vocab_files_names = VOCAB_FILES_NAMES
|
||||
pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
|
||||
max_model_input_sizes = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
|
||||
slow_tokenizer_class = MarkupLMTokenizer
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
vocab_file,
|
||||
merges_file,
|
||||
tags_dict,
|
||||
tokenizer_file=None,
|
||||
errors="replace",
|
||||
bos_token="<s>",
|
||||
eos_token="</s>",
|
||||
sep_token="</s>",
|
||||
cls_token="<s>",
|
||||
unk_token="<unk>",
|
||||
pad_token="<pad>",
|
||||
mask_token="<mask>",
|
||||
add_prefix_space=False,
|
||||
max_depth=50,
|
||||
max_width=1000,
|
||||
pad_width=1001,
|
||||
pad_token_label=-100,
|
||||
only_label_first_subword=True,
|
||||
trim_offsets=False,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__(
|
||||
vocab_file=vocab_file,
|
||||
merges_file=merges_file,
|
||||
tags_dict=tags_dict,
|
||||
tokenizer_file=tokenizer_file,
|
||||
errors=errors,
|
||||
bos_token=bos_token,
|
||||
eos_token=eos_token,
|
||||
unk_token=unk_token,
|
||||
sep_token=sep_token,
|
||||
cls_token=cls_token,
|
||||
pad_token=pad_token,
|
||||
mask_token=mask_token,
|
||||
add_prefix_space=add_prefix_space,
|
||||
trim_offsets=trim_offsets,
|
||||
max_depth=max_depth,
|
||||
max_width=max_width,
|
||||
pad_width=pad_width,
|
||||
pad_token_label=pad_token_label,
|
||||
only_label_first_subword=only_label_first_subword,
|
||||
**kwargs,
|
||||
)
|
||||
if trim_offsets:
|
||||
# Not implemented yet, because we need to chain two post processors which is not possible yet
|
||||
# We need to wait for https://github.com/huggingface/tokenizers/pull/1005
|
||||
# With `trim_offsets=False` we don't need to do add `processors.ByteLevel(trim_offsets=False)`
|
||||
# because it's not doing anything
|
||||
raise NotImplementedError(
|
||||
"`trim_offsets=True` is not implemented for MarkupLMTokenizerFast. Please set it to False."
|
||||
)
|
||||
|
||||
self.tags_dict = tags_dict
|
||||
|
||||
pre_tok_state = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__())
|
||||
if pre_tok_state.get("add_prefix_space", add_prefix_space) != add_prefix_space:
|
||||
pre_tok_class = getattr(pre_tokenizers, pre_tok_state.pop("type"))
|
||||
pre_tok_state["add_prefix_space"] = add_prefix_space
|
||||
self.backend_tokenizer.pre_tokenizer = pre_tok_class(**pre_tok_state)
|
||||
|
||||
self.add_prefix_space = add_prefix_space
|
||||
|
||||
tokenizer_component = "post_processor"
|
||||
tokenizer_component_instance = getattr(self.backend_tokenizer, tokenizer_component, None)
|
||||
if tokenizer_component_instance:
|
||||
state = json.loads(tokenizer_component_instance.__getstate__())
|
||||
|
||||
# The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
|
||||
if "sep" in state:
|
||||
state["sep"] = tuple(state["sep"])
|
||||
if "cls" in state:
|
||||
state["cls"] = tuple(state["cls"])
|
||||
|
||||
changes_to_apply = False
|
||||
|
||||
if state.get("add_prefix_space", add_prefix_space) != add_prefix_space:
|
||||
state["add_prefix_space"] = add_prefix_space
|
||||
changes_to_apply = True
|
||||
|
||||
if changes_to_apply:
|
||||
component_class = getattr(processors, state.pop("type"))
|
||||
new_value = component_class(**state)
|
||||
setattr(self.backend_tokenizer, tokenizer_component, new_value)
|
||||
|
||||
# additional properties
|
||||
self.max_depth = max_depth
|
||||
self.max_width = max_width
|
||||
self.pad_width = pad_width
|
||||
self.unk_tag_id = len(self.tags_dict)
|
||||
self.pad_tag_id = self.unk_tag_id + 1
|
||||
self.pad_xpath_tags_seq = [self.pad_tag_id] * self.max_depth
|
||||
self.pad_xpath_subs_seq = [self.pad_width] * self.max_depth
|
||||
self.pad_token_label = pad_token_label
|
||||
self.only_label_first_subword = only_label_first_subword
|
||||
|
||||
def get_xpath_seq(self, xpath):
|
||||
"""
|
||||
Given the xpath expression of one particular node (like "/html/body/div/li[1]/div/span[2]"), return a list of
|
||||
tag IDs and corresponding subscripts, taking into account max depth.
|
||||
"""
|
||||
xpath_tags_list = []
|
||||
xpath_subs_list = []
|
||||
|
||||
xpath_units = xpath.split("/")
|
||||
for unit in xpath_units:
|
||||
if not unit.strip():
|
||||
continue
|
||||
name_subs = unit.strip().split("[")
|
||||
tag_name = name_subs[0]
|
||||
sub = 0 if len(name_subs) == 1 else int(name_subs[1][:-1])
|
||||
xpath_tags_list.append(self.tags_dict.get(tag_name, self.unk_tag_id))
|
||||
xpath_subs_list.append(min(self.max_width, sub))
|
||||
|
||||
xpath_tags_list = xpath_tags_list[: self.max_depth]
|
||||
xpath_subs_list = xpath_tags_list[: self.max_depth]
|
||||
xpath_tags_list += [self.pad_tag_id] * (self.max_depth - len(xpath_tags_list))
|
||||
xpath_subs_list += [self.pad_width] * (self.max_depth - len(xpath_subs_list))
|
||||
|
||||
return xpath_tags_list, xpath_subs_list
|
||||
|
||||
@add_end_docstrings(ENCODE_KWARGS_DOCSTRING, MARKUPLM_ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING)
|
||||
def __call__(
|
||||
self,
|
||||
text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]],
|
||||
text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None,
|
||||
xpaths: Union[List[List[int]], List[List[List[int]]]] = None,
|
||||
node_labels: Optional[Union[List[int], List[List[int]]]] = None,
|
||||
add_special_tokens: bool = True,
|
||||
padding: Union[bool, str, PaddingStrategy] = False,
|
||||
truncation: Union[bool, str, TruncationStrategy] = False,
|
||||
max_length: Optional[int] = None,
|
||||
stride: int = 0,
|
||||
pad_to_multiple_of: Optional[int] = None,
|
||||
return_tensors: Optional[Union[str, TensorType]] = None,
|
||||
return_token_type_ids: Optional[bool] = None,
|
||||
return_attention_mask: Optional[bool] = None,
|
||||
return_overflowing_tokens: bool = False,
|
||||
return_special_tokens_mask: bool = False,
|
||||
return_offsets_mapping: bool = False,
|
||||
return_length: bool = False,
|
||||
verbose: bool = True,
|
||||
**kwargs
|
||||
) -> BatchEncoding:
|
||||
"""
|
||||
Main method to tokenize and prepare for the model one or several sequence(s) or one or several pair(s) of
|
||||
sequences with nodes, xpaths and optional labels.
|
||||
|
||||
Args:
|
||||
text (`str`, `List[str]`, `List[List[str]]`):
|
||||
The sequence or batch of sequences to be encoded. Each sequence can be a string, a list of strings
|
||||
(words of a single example or questions of a batch of examples) or a list of list of strings (batch of
|
||||
words).
|
||||
text_pair (`List[str]`, `List[List[str]]`):
|
||||
The sequence or batch of sequences to be encoded. Each sequence should be a list of strings
|
||||
(pretokenized string).
|
||||
xpaths (`List[List[int]]`, `List[List[List[int]]]`):
|
||||
Node-level xpaths. Each bounding box should be normalized to be on a 0-1000 scale.
|
||||
node_labels (`List[int]`, `List[List[int]]`, *optional*):
|
||||
Node-level integer labels (for token classification tasks).
|
||||
"""
|
||||
# Input type checking for clearer error
|
||||
def _is_valid_text_input(t):
|
||||
if isinstance(t, str):
|
||||
# Strings are fine
|
||||
return True
|
||||
elif isinstance(t, (list, tuple)):
|
||||
# List are fine as long as they are...
|
||||
if len(t) == 0:
|
||||
# ... empty
|
||||
return True
|
||||
elif isinstance(t[0], str):
|
||||
# ... list of strings
|
||||
return True
|
||||
elif isinstance(t[0], (list, tuple)):
|
||||
# ... list with an empty list or with a list of strings
|
||||
return len(t[0]) == 0 or isinstance(t[0][0], str)
|
||||
else:
|
||||
return False
|
||||
else:
|
||||
return False
|
||||
|
||||
if text_pair is not None:
|
||||
# in case text + text_pair are provided, text = questions, text_pair = nodes
|
||||
if not _is_valid_text_input(text):
|
||||
raise ValueError("text input must of type `str` (single example) or `List[str]` (batch of examples). ")
|
||||
if not isinstance(text_pair, (list, tuple)):
|
||||
raise ValueError(
|
||||
"Nodes must be of type `List[str]` (single pretokenized example), "
|
||||
"or `List[List[str]]` (batch of pretokenized examples)."
|
||||
)
|
||||
else:
|
||||
# in case only text is provided => must be nodes
|
||||
if not isinstance(text, (list, tuple)):
|
||||
raise ValueError(
|
||||
"Nodes must be of type `List[str]` (single pretokenized example), "
|
||||
"or `List[List[str]]` (batch of pretokenized examples)."
|
||||
)
|
||||
|
||||
if text_pair is not None:
|
||||
is_batched = isinstance(text, (list, tuple))
|
||||
else:
|
||||
is_batched = isinstance(text, (list, tuple)) and text and isinstance(text[0], (list, tuple))
|
||||
|
||||
nodes = text if text_pair is None else text_pair
|
||||
assert xpaths is not None, "You must provide corresponding xpaths"
|
||||
if is_batched:
|
||||
assert len(nodes) == len(xpaths), "You must provide nodes and xpaths for an equal amount of examples"
|
||||
for nodes_example, xpaths_example in zip(nodes, xpaths):
|
||||
assert len(nodes_example) == len(xpaths_example), "You must provide as many nodes as there are xpaths"
|
||||
else:
|
||||
assert len(nodes) == len(xpaths), "You must provide as many nodes as there are xpaths"
|
||||
|
||||
if is_batched:
|
||||
if text_pair is not None and len(text) != len(text_pair):
|
||||
raise ValueError(
|
||||
f"batch length of `text`: {len(text)} does not match batch length of `text_pair`:"
|
||||
f" {len(text_pair)}."
|
||||
)
|
||||
batch_text_or_text_pairs = list(zip(text, text_pair)) if text_pair is not None else text
|
||||
is_pair = bool(text_pair is not None)
|
||||
return self.batch_encode_plus(
|
||||
batch_text_or_text_pairs=batch_text_or_text_pairs,
|
||||
is_pair=is_pair,
|
||||
xpaths=xpaths,
|
||||
node_labels=node_labels,
|
||||
add_special_tokens=add_special_tokens,
|
||||
padding=padding,
|
||||
truncation=truncation,
|
||||
max_length=max_length,
|
||||
stride=stride,
|
||||
pad_to_multiple_of=pad_to_multiple_of,
|
||||
return_tensors=return_tensors,
|
||||
return_token_type_ids=return_token_type_ids,
|
||||
return_attention_mask=return_attention_mask,
|
||||
return_overflowing_tokens=return_overflowing_tokens,
|
||||
return_special_tokens_mask=return_special_tokens_mask,
|
||||
return_offsets_mapping=return_offsets_mapping,
|
||||
return_length=return_length,
|
||||
verbose=verbose,
|
||||
**kwargs,
|
||||
)
|
||||
else:
|
||||
return self.encode_plus(
|
||||
text=text,
|
||||
text_pair=text_pair,
|
||||
xpaths=xpaths,
|
||||
node_labels=node_labels,
|
||||
add_special_tokens=add_special_tokens,
|
||||
padding=padding,
|
||||
truncation=truncation,
|
||||
max_length=max_length,
|
||||
stride=stride,
|
||||
pad_to_multiple_of=pad_to_multiple_of,
|
||||
return_tensors=return_tensors,
|
||||
return_token_type_ids=return_token_type_ids,
|
||||
return_attention_mask=return_attention_mask,
|
||||
return_overflowing_tokens=return_overflowing_tokens,
|
||||
return_special_tokens_mask=return_special_tokens_mask,
|
||||
return_offsets_mapping=return_offsets_mapping,
|
||||
return_length=return_length,
|
||||
verbose=verbose,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
@add_end_docstrings(ENCODE_KWARGS_DOCSTRING, MARKUPLM_ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING)
|
||||
def batch_encode_plus(
|
||||
self,
|
||||
batch_text_or_text_pairs: Union[
|
||||
List[TextInput],
|
||||
List[TextInputPair],
|
||||
List[PreTokenizedInput],
|
||||
],
|
||||
is_pair: bool = None,
|
||||
xpaths: Optional[List[List[List[int]]]] = None,
|
||||
node_labels: Optional[Union[List[int], List[List[int]]]] = None,
|
||||
add_special_tokens: bool = True,
|
||||
padding: Union[bool, str, PaddingStrategy] = False,
|
||||
truncation: Union[bool, str, TruncationStrategy] = False,
|
||||
max_length: Optional[int] = None,
|
||||
stride: int = 0,
|
||||
pad_to_multiple_of: Optional[int] = None,
|
||||
return_tensors: Optional[Union[str, TensorType]] = None,
|
||||
return_token_type_ids: Optional[bool] = None,
|
||||
return_attention_mask: Optional[bool] = None,
|
||||
return_overflowing_tokens: bool = False,
|
||||
return_special_tokens_mask: bool = False,
|
||||
return_offsets_mapping: bool = False,
|
||||
return_length: bool = False,
|
||||
verbose: bool = True,
|
||||
**kwargs
|
||||
) -> BatchEncoding:
|
||||
# Backward compatibility for 'truncation_strategy', 'pad_to_max_length'
|
||||
padding_strategy, truncation_strategy, max_length, kwargs = self._get_padding_truncation_strategies(
|
||||
padding=padding,
|
||||
truncation=truncation,
|
||||
max_length=max_length,
|
||||
pad_to_multiple_of=pad_to_multiple_of,
|
||||
verbose=verbose,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
return self._batch_encode_plus(
|
||||
batch_text_or_text_pairs=batch_text_or_text_pairs,
|
||||
is_pair=is_pair,
|
||||
xpaths=xpaths,
|
||||
node_labels=node_labels,
|
||||
add_special_tokens=add_special_tokens,
|
||||
padding_strategy=padding_strategy,
|
||||
truncation_strategy=truncation_strategy,
|
||||
max_length=max_length,
|
||||
stride=stride,
|
||||
pad_to_multiple_of=pad_to_multiple_of,
|
||||
return_tensors=return_tensors,
|
||||
return_token_type_ids=return_token_type_ids,
|
||||
return_attention_mask=return_attention_mask,
|
||||
return_overflowing_tokens=return_overflowing_tokens,
|
||||
return_special_tokens_mask=return_special_tokens_mask,
|
||||
return_offsets_mapping=return_offsets_mapping,
|
||||
return_length=return_length,
|
||||
verbose=verbose,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def tokenize(self, text: str, pair: Optional[str] = None, add_special_tokens: bool = False, **kwargs) -> List[str]:
|
||||
batched_input = [(text, pair)] if pair else [text]
|
||||
encodings = self._tokenizer.encode_batch(
|
||||
batched_input, add_special_tokens=add_special_tokens, is_pretokenized=False, **kwargs
|
||||
)
|
||||
|
||||
return encodings[0].tokens
|
||||
|
||||
@add_end_docstrings(ENCODE_KWARGS_DOCSTRING, MARKUPLM_ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING)
|
||||
def encode_plus(
|
||||
self,
|
||||
text: Union[TextInput, PreTokenizedInput],
|
||||
text_pair: Optional[PreTokenizedInput] = None,
|
||||
xpaths: Optional[List[List[int]]] = None,
|
||||
node_labels: Optional[List[int]] = None,
|
||||
add_special_tokens: bool = True,
|
||||
padding: Union[bool, str, PaddingStrategy] = False,
|
||||
truncation: Union[bool, str, TruncationStrategy] = False,
|
||||
max_length: Optional[int] = None,
|
||||
stride: int = 0,
|
||||
pad_to_multiple_of: Optional[int] = None,
|
||||
return_tensors: Optional[Union[str, TensorType]] = None,
|
||||
return_token_type_ids: Optional[bool] = None,
|
||||
return_attention_mask: Optional[bool] = None,
|
||||
return_overflowing_tokens: bool = False,
|
||||
return_special_tokens_mask: bool = False,
|
||||
return_offsets_mapping: bool = False,
|
||||
return_length: bool = False,
|
||||
verbose: bool = True,
|
||||
**kwargs
|
||||
) -> BatchEncoding:
|
||||
"""
|
||||
Tokenize and prepare for the model a sequence or a pair of sequences. .. warning:: This method is deprecated,
|
||||
`__call__` should be used instead.
|
||||
|
||||
Args:
|
||||
text (`str`, `List[str]`, `List[List[str]]`):
|
||||
The first sequence to be encoded. This can be a string, a list of strings or a list of list of strings.
|
||||
text_pair (`List[str]` or `List[int]`, *optional*):
|
||||
Optional second sequence to be encoded. This can be a list of strings (words of a single example) or a
|
||||
list of list of strings (words of a batch of examples).
|
||||
"""
|
||||
|
||||
# Backward compatibility for 'truncation_strategy', 'pad_to_max_length'
|
||||
padding_strategy, truncation_strategy, max_length, kwargs = self._get_padding_truncation_strategies(
|
||||
padding=padding,
|
||||
truncation=truncation,
|
||||
max_length=max_length,
|
||||
pad_to_multiple_of=pad_to_multiple_of,
|
||||
verbose=verbose,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
return self._encode_plus(
|
||||
text=text,
|
||||
xpaths=xpaths,
|
||||
text_pair=text_pair,
|
||||
node_labels=node_labels,
|
||||
add_special_tokens=add_special_tokens,
|
||||
padding_strategy=padding_strategy,
|
||||
truncation_strategy=truncation_strategy,
|
||||
max_length=max_length,
|
||||
stride=stride,
|
||||
pad_to_multiple_of=pad_to_multiple_of,
|
||||
return_tensors=return_tensors,
|
||||
return_token_type_ids=return_token_type_ids,
|
||||
return_attention_mask=return_attention_mask,
|
||||
return_overflowing_tokens=return_overflowing_tokens,
|
||||
return_special_tokens_mask=return_special_tokens_mask,
|
||||
return_offsets_mapping=return_offsets_mapping,
|
||||
return_length=return_length,
|
||||
verbose=verbose,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def _batch_encode_plus(
|
||||
self,
|
||||
batch_text_or_text_pairs: Union[
|
||||
List[TextInput],
|
||||
List[TextInputPair],
|
||||
List[PreTokenizedInput],
|
||||
],
|
||||
is_pair: bool = None,
|
||||
xpaths: Optional[List[List[List[int]]]] = None,
|
||||
node_labels: Optional[List[List[int]]] = None,
|
||||
add_special_tokens: bool = True,
|
||||
padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
|
||||
truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE,
|
||||
max_length: Optional[int] = None,
|
||||
stride: int = 0,
|
||||
pad_to_multiple_of: Optional[int] = None,
|
||||
return_tensors: Optional[str] = None,
|
||||
return_token_type_ids: Optional[bool] = None,
|
||||
return_attention_mask: Optional[bool] = None,
|
||||
return_overflowing_tokens: bool = False,
|
||||
return_special_tokens_mask: bool = False,
|
||||
return_offsets_mapping: bool = False,
|
||||
return_length: bool = False,
|
||||
verbose: bool = True,
|
||||
) -> BatchEncoding:
|
||||
if not isinstance(batch_text_or_text_pairs, list):
|
||||
raise TypeError(f"batch_text_or_text_pairs has to be a list (got {type(batch_text_or_text_pairs)})")
|
||||
|
||||
# Set the truncation and padding strategy and restore the initial configuration
|
||||
self.set_truncation_and_padding(
|
||||
padding_strategy=padding_strategy,
|
||||
truncation_strategy=truncation_strategy,
|
||||
max_length=max_length,
|
||||
stride=stride,
|
||||
pad_to_multiple_of=pad_to_multiple_of,
|
||||
)
|
||||
|
||||
if is_pair:
|
||||
batch_text_or_text_pairs = [([text], text_pair) for text, text_pair in batch_text_or_text_pairs]
|
||||
|
||||
encodings = self._tokenizer.encode_batch(
|
||||
batch_text_or_text_pairs,
|
||||
add_special_tokens=add_special_tokens,
|
||||
is_pretokenized=True, # we set this to True as MarkupLM always expects pretokenized inputs
|
||||
)
|
||||
|
||||
# Convert encoding to dict
|
||||
# `Tokens` is a tuple of (List[Dict[str, List[List[int]]]] or List[Dict[str, 2D-Tensor]],
|
||||
# List[EncodingFast]) with nested dimensions corresponding to batch, overflows, sequence length
|
||||
tokens_and_encodings = [
|
||||
self._convert_encoding(
|
||||
encoding=encoding,
|
||||
return_token_type_ids=return_token_type_ids,
|
||||
return_attention_mask=return_attention_mask,
|
||||
return_overflowing_tokens=return_overflowing_tokens,
|
||||
return_special_tokens_mask=return_special_tokens_mask,
|
||||
return_offsets_mapping=True
|
||||
if node_labels is not None
|
||||
else return_offsets_mapping, # we use offsets to create the labels
|
||||
return_length=return_length,
|
||||
verbose=verbose,
|
||||
)
|
||||
for encoding in encodings
|
||||
]
|
||||
|
||||
# Convert the output to have dict[list] from list[dict] and remove the additional overflows dimension
|
||||
# From (variable) shape (batch, overflows, sequence length) to ~ (batch * overflows, sequence length)
|
||||
# (we say ~ because the number of overflow varies with the example in the batch)
|
||||
#
|
||||
# To match each overflowing sample with the original sample in the batch
|
||||
# we add an overflow_to_sample_mapping array (see below)
|
||||
sanitized_tokens = {}
|
||||
for key in tokens_and_encodings[0][0].keys():
|
||||
stack = [e for item, _ in tokens_and_encodings for e in item[key]]
|
||||
sanitized_tokens[key] = stack
|
||||
sanitized_encodings = [e for _, item in tokens_and_encodings for e in item]
|
||||
|
||||
# If returning overflowing tokens, we need to return a mapping
|
||||
# from the batch idx to the original sample
|
||||
if return_overflowing_tokens:
|
||||
overflow_to_sample_mapping = []
|
||||
for i, (toks, _) in enumerate(tokens_and_encodings):
|
||||
overflow_to_sample_mapping += [i] * len(toks["input_ids"])
|
||||
sanitized_tokens["overflow_to_sample_mapping"] = overflow_to_sample_mapping
|
||||
|
||||
for input_ids in sanitized_tokens["input_ids"]:
|
||||
self._eventual_warn_about_too_long_sequence(input_ids, max_length, verbose)
|
||||
|
||||
# create the token-level xpaths tags and subscripts
|
||||
xpath_tags_seq = []
|
||||
xpath_subs_seq = []
|
||||
for batch_index in range(len(sanitized_tokens["input_ids"])):
|
||||
if return_overflowing_tokens:
|
||||
original_index = sanitized_tokens["overflow_to_sample_mapping"][batch_index]
|
||||
else:
|
||||
original_index = batch_index
|
||||
xpath_tags_seq_example = []
|
||||
xpath_subs_seq_example = []
|
||||
for id, sequence_id, word_id in zip(
|
||||
sanitized_tokens["input_ids"][batch_index],
|
||||
sanitized_encodings[batch_index].sequence_ids,
|
||||
sanitized_encodings[batch_index].word_ids,
|
||||
):
|
||||
if word_id is not None:
|
||||
if is_pair and sequence_id == 0:
|
||||
xpath_tags_seq_example.append(self.pad_xpath_tags_seq)
|
||||
xpath_subs_seq_example.append(self.pad_xpath_subs_seq)
|
||||
else:
|
||||
xpath_tags_list, xpath_subs_list = self.get_xpath_seq(xpaths[original_index][word_id])
|
||||
xpath_tags_seq_example.extend([xpath_tags_list])
|
||||
xpath_subs_seq_example.extend([xpath_subs_list])
|
||||
else:
|
||||
if id in [self.cls_token_id, self.sep_token_id, self.pad_token_id]:
|
||||
xpath_tags_seq_example.append(self.pad_xpath_tags_seq)
|
||||
xpath_subs_seq_example.append(self.pad_xpath_subs_seq)
|
||||
else:
|
||||
raise ValueError("Id not recognized")
|
||||
xpath_tags_seq.append(xpath_tags_seq_example)
|
||||
xpath_subs_seq.append(xpath_subs_seq_example)
|
||||
|
||||
sanitized_tokens["xpath_tags_seq"] = xpath_tags_seq
|
||||
sanitized_tokens["xpath_subs_seq"] = xpath_subs_seq
|
||||
|
||||
# optionally, create the labels
|
||||
if node_labels is not None:
|
||||
labels = []
|
||||
for batch_index in range(len(sanitized_tokens["input_ids"])):
|
||||
if return_overflowing_tokens:
|
||||
original_index = sanitized_tokens["overflow_to_sample_mapping"][batch_index]
|
||||
else:
|
||||
original_index = batch_index
|
||||
labels_example = []
|
||||
for id, offset, word_id in zip(
|
||||
sanitized_tokens["input_ids"][batch_index],
|
||||
sanitized_tokens["offset_mapping"][batch_index],
|
||||
sanitized_encodings[batch_index].word_ids,
|
||||
):
|
||||
if word_id is not None:
|
||||
if self.only_label_first_subword:
|
||||
if offset[0] == 0:
|
||||
# Use the real label id for the first token of the word, and padding ids for the remaining tokens
|
||||
labels_example.append(node_labels[original_index][word_id])
|
||||
else:
|
||||
labels_example.append(self.pad_token_label)
|
||||
else:
|
||||
labels_example.append(node_labels[original_index][word_id])
|
||||
else:
|
||||
labels_example.append(self.pad_token_label)
|
||||
labels.append(labels_example)
|
||||
|
||||
sanitized_tokens["labels"] = labels
|
||||
# finally, remove offsets if the user didn't want them
|
||||
if not return_offsets_mapping:
|
||||
del sanitized_tokens["offset_mapping"]
|
||||
|
||||
return BatchEncoding(sanitized_tokens, sanitized_encodings, tensor_type=return_tensors)
|
||||
|
||||
def _encode_plus(
|
||||
self,
|
||||
text: Union[TextInput, PreTokenizedInput],
|
||||
text_pair: Optional[PreTokenizedInput] = None,
|
||||
xpaths: Optional[List[List[int]]] = None,
|
||||
node_labels: Optional[List[int]] = None,
|
||||
add_special_tokens: bool = True,
|
||||
padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
|
||||
truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE,
|
||||
max_length: Optional[int] = None,
|
||||
stride: int = 0,
|
||||
pad_to_multiple_of: Optional[int] = None,
|
||||
return_tensors: Optional[bool] = None,
|
||||
return_token_type_ids: Optional[bool] = None,
|
||||
return_attention_mask: Optional[bool] = None,
|
||||
return_overflowing_tokens: bool = False,
|
||||
return_special_tokens_mask: bool = False,
|
||||
return_offsets_mapping: bool = False,
|
||||
return_length: bool = False,
|
||||
verbose: bool = True,
|
||||
**kwargs
|
||||
) -> BatchEncoding:
|
||||
# make it a batched input
|
||||
# 2 options:
|
||||
# 1) only text, in case text must be a list of str
|
||||
# 2) text + text_pair, in which case text = str and text_pair a list of str
|
||||
batched_input = [(text, text_pair)] if text_pair else [text]
|
||||
batched_xpaths = [xpaths]
|
||||
batched_node_labels = [node_labels] if node_labels is not None else None
|
||||
batched_output = self._batch_encode_plus(
|
||||
batched_input,
|
||||
is_pair=bool(text_pair is not None),
|
||||
xpaths=batched_xpaths,
|
||||
node_labels=batched_node_labels,
|
||||
add_special_tokens=add_special_tokens,
|
||||
padding_strategy=padding_strategy,
|
||||
truncation_strategy=truncation_strategy,
|
||||
max_length=max_length,
|
||||
stride=stride,
|
||||
pad_to_multiple_of=pad_to_multiple_of,
|
||||
return_tensors=return_tensors,
|
||||
return_token_type_ids=return_token_type_ids,
|
||||
return_attention_mask=return_attention_mask,
|
||||
return_overflowing_tokens=return_overflowing_tokens,
|
||||
return_special_tokens_mask=return_special_tokens_mask,
|
||||
return_offsets_mapping=return_offsets_mapping,
|
||||
return_length=return_length,
|
||||
verbose=verbose,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
# Return tensor is None, then we can remove the leading batch axis
|
||||
# Overflowing tokens are returned as a batch of output so we keep them in this case
|
||||
if return_tensors is None and not return_overflowing_tokens:
|
||||
batched_output = BatchEncoding(
|
||||
{
|
||||
key: value[0] if len(value) > 0 and isinstance(value[0], list) else value
|
||||
for key, value in batched_output.items()
|
||||
},
|
||||
batched_output.encodings,
|
||||
)
|
||||
|
||||
self._eventual_warn_about_too_long_sequence(batched_output["input_ids"], max_length, verbose)
|
||||
|
||||
return batched_output
|
||||
|
||||
def _pad(
|
||||
self,
|
||||
encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding],
|
||||
max_length: Optional[int] = None,
|
||||
padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
|
||||
pad_to_multiple_of: Optional[int] = None,
|
||||
return_attention_mask: Optional[bool] = None,
|
||||
) -> dict:
|
||||
"""
|
||||
Args:
|
||||
Pad encoded inputs (on left/right and up to predefined length or max length in the batch)
|
||||
encoded_inputs:
|
||||
Dictionary of tokenized inputs (`List[int]`) or batch of tokenized inputs (`List[List[int]]`).
|
||||
max_length: maximum length of the returned list and optionally padding length (see below).
|
||||
Will truncate by taking into account the special tokens.
|
||||
padding_strategy: PaddingStrategy to use for padding.
|
||||
- PaddingStrategy.LONGEST Pad to the longest sequence in the batch
|
||||
- PaddingStrategy.MAX_LENGTH: Pad to the max length (default)
|
||||
- PaddingStrategy.DO_NOT_PAD: Do not pad
|
||||
The tokenizer padding sides are defined in self.padding_side:
|
||||
- 'left': pads on the left of the sequences
|
||||
- 'right': pads on the right of the sequences
|
||||
pad_to_multiple_of: (optional) Integer if set will pad the sequence to a multiple of the provided value.
|
||||
This is especially useful to enable the use of Tensor Core on NVIDIA hardware with compute capability
|
||||
>= 7.5 (Volta).
|
||||
return_attention_mask:
|
||||
(optional) Set to False to avoid returning attention mask (default: set to model specifics)
|
||||
"""
|
||||
# Load from model defaults
|
||||
if return_attention_mask is None:
|
||||
return_attention_mask = "attention_mask" in self.model_input_names
|
||||
|
||||
required_input = encoded_inputs[self.model_input_names[0]]
|
||||
|
||||
if padding_strategy == PaddingStrategy.LONGEST:
|
||||
max_length = len(required_input)
|
||||
|
||||
if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
|
||||
max_length = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of
|
||||
|
||||
needs_to_be_padded = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(required_input) != max_length
|
||||
|
||||
# Initialize attention mask if not present.
|
||||
if return_attention_mask and "attention_mask" not in encoded_inputs:
|
||||
encoded_inputs["attention_mask"] = [1] * len(required_input)
|
||||
|
||||
if needs_to_be_padded:
|
||||
difference = max_length - len(required_input)
|
||||
if self.padding_side == "right":
|
||||
if return_attention_mask:
|
||||
encoded_inputs["attention_mask"] = encoded_inputs["attention_mask"] + [0] * difference
|
||||
if "token_type_ids" in encoded_inputs:
|
||||
encoded_inputs["token_type_ids"] = (
|
||||
encoded_inputs["token_type_ids"] + [self.pad_token_type_id] * difference
|
||||
)
|
||||
if "xpath_tags_seq" in encoded_inputs:
|
||||
encoded_inputs["xpath_tags_seq"] = (
|
||||
encoded_inputs["xpath_tags_seq"] + [self.pad_xpath_tags_seq] * difference
|
||||
)
|
||||
if "xpath_subs_seq" in encoded_inputs:
|
||||
encoded_inputs["xpath_subs_seq"] = (
|
||||
encoded_inputs["xpath_subs_seq"] + [self.pad_xpath_subs_seq] * difference
|
||||
)
|
||||
if "labels" in encoded_inputs:
|
||||
encoded_inputs["labels"] = encoded_inputs["labels"] + [self.pad_token_label] * difference
|
||||
if "special_tokens_mask" in encoded_inputs:
|
||||
encoded_inputs["special_tokens_mask"] = encoded_inputs["special_tokens_mask"] + [1] * difference
|
||||
encoded_inputs[self.model_input_names[0]] = required_input + [self.pad_token_id] * difference
|
||||
elif self.padding_side == "left":
|
||||
if return_attention_mask:
|
||||
encoded_inputs["attention_mask"] = [0] * difference + encoded_inputs["attention_mask"]
|
||||
if "token_type_ids" in encoded_inputs:
|
||||
encoded_inputs["token_type_ids"] = [self.pad_token_type_id] * difference + encoded_inputs[
|
||||
"token_type_ids"
|
||||
]
|
||||
if "xpath_tags_seq" in encoded_inputs:
|
||||
encoded_inputs["xpath_tags_seq"] = [self.pad_xpath_tags_seq] * difference + encoded_inputs[
|
||||
"xpath_tags_seq"
|
||||
]
|
||||
if "xpath_subs_seq" in encoded_inputs:
|
||||
encoded_inputs["xpath_subs_seq"] = [self.pad_xpath_subs_seq] * difference + encoded_inputs[
|
||||
"xpath_subs_seq"
|
||||
]
|
||||
if "labels" in encoded_inputs:
|
||||
encoded_inputs["labels"] = [self.pad_token_label] * difference + encoded_inputs["labels"]
|
||||
if "special_tokens_mask" in encoded_inputs:
|
||||
encoded_inputs["special_tokens_mask"] = [1] * difference + encoded_inputs["special_tokens_mask"]
|
||||
encoded_inputs[self.model_input_names[0]] = [self.pad_token_id] * difference + required_input
|
||||
else:
|
||||
raise ValueError("Invalid padding strategy:" + str(self.padding_side))
|
||||
|
||||
return encoded_inputs
|
||||
|
||||
def build_inputs_with_special_tokens(
|
||||
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
||||
) -> List[int]:
|
||||
"""
|
||||
Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
|
||||
adding special tokens. A RoBERTa sequence has the following format:
|
||||
- single sequence: `<s> X </s>`
|
||||
- pair of sequences: `<s> A </s></s> B </s>`
|
||||
Args:
|
||||
token_ids_0 (`List[int]`):
|
||||
List of IDs to which the special tokens will be added.
|
||||
token_ids_1 (`List[int]`, *optional*):
|
||||
Optional second list of IDs for sequence pairs.
|
||||
Returns:
|
||||
`List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
|
||||
"""
|
||||
if token_ids_1 is None:
|
||||
return [self.cls_token_id] + token_ids_0 + [self.sep_token_id]
|
||||
cls = [self.cls_token_id]
|
||||
sep = [self.sep_token_id]
|
||||
return cls + token_ids_0 + sep + token_ids_1 + sep
|
||||
|
||||
def create_token_type_ids_from_sequences(
|
||||
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
||||
) -> List[int]:
|
||||
"""
|
||||
Create a mask from the two sequences passed to be used in a sequence-pair classification task. RoBERTa does not
|
||||
make use of token type ids, therefore a list of zeros is returned.
|
||||
|
||||
Args:
|
||||
token_ids_0 (`List[int]`):
|
||||
List of IDs.
|
||||
token_ids_1 (`List[int]`, *optional*):
|
||||
Optional second list of IDs for sequence pairs.
|
||||
Returns:
|
||||
`List[int]`: List of zeros.
|
||||
"""
|
||||
sep = [self.sep_token_id]
|
||||
cls = [self.cls_token_id]
|
||||
|
||||
if token_ids_1 is None:
|
||||
return len(cls + token_ids_0 + sep) * [0]
|
||||
return len(cls + token_ids_0 + sep + token_ids_1 + sep) * [0]
|
||||
|
||||
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
|
||||
files = self._tokenizer.model.save(save_directory, name=filename_prefix)
|
||||
return tuple(files)
|
|
@ -46,6 +46,7 @@ from .utils import (
|
|||
is_accelerate_available,
|
||||
is_apex_available,
|
||||
is_bitsandbytes_available,
|
||||
is_bs4_available,
|
||||
is_detectron2_available,
|
||||
is_faiss_available,
|
||||
is_flax_available,
|
||||
|
@ -239,6 +240,13 @@ def custom_tokenizers(test_case):
|
|||
return unittest.skipUnless(_run_custom_tokenizers, "test of custom tokenizers")(test_case)
|
||||
|
||||
|
||||
def require_bs4(test_case):
|
||||
"""
|
||||
Decorator marking a test that requires BeautifulSoup4. These tests are skipped when BeautifulSoup4 isn't installed.
|
||||
"""
|
||||
return unittest.skipUnless(is_bs4_available(), "test requires BeautifulSoup4")(test_case)
|
||||
|
||||
|
||||
def require_git_lfs(test_case):
|
||||
"""
|
||||
Decorator marking a test that requires git-lfs.
|
||||
|
|
|
@ -89,6 +89,7 @@ from .import_utils import (
|
|||
is_accelerate_available,
|
||||
is_apex_available,
|
||||
is_bitsandbytes_available,
|
||||
is_bs4_available,
|
||||
is_coloredlogs_available,
|
||||
is_datasets_available,
|
||||
is_detectron2_available,
|
||||
|
|
|
@ -3020,6 +3020,44 @@ class MarianMTModel(metaclass=DummyObject):
|
|||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST = None
|
||||
|
||||
|
||||
class MarkupLMForQuestionAnswering(metaclass=DummyObject):
|
||||
_backends = ["torch"]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
class MarkupLMForSequenceClassification(metaclass=DummyObject):
|
||||
_backends = ["torch"]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
class MarkupLMForTokenClassification(metaclass=DummyObject):
|
||||
_backends = ["torch"]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
class MarkupLMModel(metaclass=DummyObject):
|
||||
_backends = ["torch"]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
class MarkupLMPreTrainedModel(metaclass=DummyObject):
|
||||
_backends = ["torch"]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST = None
|
||||
|
||||
|
||||
|
|
|
@ -234,6 +234,13 @@ class LxmertTokenizerFast(metaclass=DummyObject):
|
|||
requires_backends(self, ["tokenizers"])
|
||||
|
||||
|
||||
class MarkupLMTokenizerFast(metaclass=DummyObject):
|
||||
_backends = ["tokenizers"]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["tokenizers"])
|
||||
|
||||
|
||||
class MBartTokenizerFast(metaclass=DummyObject):
|
||||
_backends = ["tokenizers"]
|
||||
|
||||
|
|
|
@ -386,6 +386,10 @@ def is_torch_fx_available():
|
|||
return _torch_fx_available
|
||||
|
||||
|
||||
def is_bs4_available():
|
||||
return importlib.util.find_spec("bs4") is not None
|
||||
|
||||
|
||||
def is_torch_onnx_dict_inputs_support_available():
|
||||
return _torch_onnx_dict_inputs_support_available
|
||||
|
||||
|
@ -748,6 +752,12 @@ If you really do want to use TensorFlow, please follow the instructions on the
|
|||
installation page https://www.tensorflow.org/install that match your environment.
|
||||
"""
|
||||
|
||||
# docstyle-ignore
|
||||
BS4_IMPORT_ERROR = """
|
||||
{0} requires the Beautiful Soup library but it was not found in your environment. You can install it with pip:
|
||||
`pip install beautifulsoup4`
|
||||
"""
|
||||
|
||||
|
||||
# docstyle-ignore
|
||||
SKLEARN_IMPORT_ERROR = """
|
||||
|
@ -889,6 +899,7 @@ CCL_IMPORT_ERROR = """
|
|||
|
||||
BACKENDS_MAPPING = OrderedDict(
|
||||
[
|
||||
("bs4", (is_bs4_available, BS4_IMPORT_ERROR)),
|
||||
("datasets", (is_datasets_available, DATASETS_IMPORT_ERROR)),
|
||||
("detectron2", (is_detectron2_available, DETECTRON2_IMPORT_ERROR)),
|
||||
("faiss", (is_faiss_available, FAISS_IMPORT_ERROR)),
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
# coding=utf-8
|
||||
# Copyright 2022 HuggingFace Inc.
|
||||
#
|
||||
# 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.
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from transformers.testing_utils import require_bs4
|
||||
from transformers.utils import is_bs4_available
|
||||
|
||||
from ...test_feature_extraction_common import FeatureExtractionSavingTestMixin
|
||||
|
||||
|
||||
if is_bs4_available():
|
||||
from transformers import MarkupLMFeatureExtractor
|
||||
|
||||
|
||||
class MarkupLMFeatureExtractionTester(unittest.TestCase):
|
||||
def __init__(self, parent):
|
||||
self.parent = parent
|
||||
|
||||
def prepare_feat_extract_dict(self):
|
||||
return {}
|
||||
|
||||
|
||||
def get_html_strings():
|
||||
html_string_1 = """<HTML>
|
||||
|
||||
<HEAD>
|
||||
<TITLE>sample document</TITLE>
|
||||
</HEAD>
|
||||
|
||||
<BODY BGCOLOR="FFFFFF">
|
||||
<HR>
|
||||
<a href="http://google.com">Goog</a>
|
||||
<H1>This is one header</H1>
|
||||
<H2>This is a another Header</H2>
|
||||
<P>Travel from
|
||||
<P>
|
||||
<B>SFO to JFK</B>
|
||||
<BR>
|
||||
<B><I>on May 2, 2015 at 2:00 pm. For details go to confirm.com </I></B>
|
||||
<HR>
|
||||
<div style="color:#0000FF">
|
||||
<h3>Traveler <b> name </b> is
|
||||
<p> John Doe </p>
|
||||
</div>"""
|
||||
|
||||
html_string_2 = """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
|
||||
<h1>My First Heading</h1>
|
||||
<p>My first paragraph.</p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
return [html_string_1, html_string_2]
|
||||
|
||||
|
||||
@require_bs4
|
||||
class MarkupLMFeatureExtractionTest(FeatureExtractionSavingTestMixin, unittest.TestCase):
|
||||
feature_extraction_class = MarkupLMFeatureExtractor if is_bs4_available() else None
|
||||
|
||||
def setUp(self):
|
||||
self.feature_extract_tester = MarkupLMFeatureExtractionTester(self)
|
||||
|
||||
@property
|
||||
def feat_extract_dict(self):
|
||||
return self.feature_extract_tester.prepare_feat_extract_dict()
|
||||
|
||||
def test_call(self):
|
||||
# Initialize feature_extractor
|
||||
feature_extractor = self.feature_extraction_class()
|
||||
|
||||
# Test not batched input
|
||||
html_string = get_html_strings()[0]
|
||||
encoding = feature_extractor(html_string)
|
||||
|
||||
# fmt: off
|
||||
expected_nodes = [['sample document', 'Goog', 'This is one header', 'This is a another Header', 'Travel from', 'SFO to JFK', 'on May 2, 2015 at 2:00 pm. For details go to confirm.com', 'Traveler', 'name', 'is', 'John Doe']]
|
||||
expected_xpaths = [['/html/head/title', '/html/body/a', '/html/body/h1', '/html/body/h2', '/html/body/p', '/html/body/p/p/b[1]', '/html/body/p/p/b[2]/i', '/html/body/p/p/div/h3', '/html/body/p/p/div/h3/b', '/html/body/p/p/div/h3', '/html/body/p/p/div/h3/p']]
|
||||
# fmt: on
|
||||
|
||||
self.assertEqual(encoding.nodes, expected_nodes)
|
||||
self.assertEqual(encoding.xpaths, expected_xpaths)
|
||||
|
||||
# Test batched
|
||||
html_strings = get_html_strings()
|
||||
encoding = feature_extractor(html_strings)
|
||||
|
||||
# fmt: off
|
||||
expected_nodes = expected_nodes + [['My First Heading', 'My first paragraph.']]
|
||||
expected_xpaths = expected_xpaths + [['/html/body/h1', '/html/body/p']]
|
||||
|
||||
self.assertEqual(len(encoding.nodes), 2)
|
||||
self.assertEqual(len(encoding.xpaths), 2)
|
||||
|
||||
self.assertEqual(encoding.nodes, expected_nodes)
|
||||
self.assertEqual(encoding.xpaths, expected_xpaths)
|
|
@ -0,0 +1,364 @@
|
|||
# coding=utf-8
|
||||
# Copyright 2022 The Hugging Face 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.
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from transformers import MarkupLMConfig, is_torch_available
|
||||
from transformers.testing_utils import require_torch, slow, torch_device
|
||||
from transformers.utils import cached_property
|
||||
|
||||
from ...test_configuration_common import ConfigTester
|
||||
from ...test_modeling_common import ModelTesterMixin, ids_tensor
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
from transformers import (
|
||||
MarkupLMForQuestionAnswering,
|
||||
MarkupLMForSequenceClassification,
|
||||
MarkupLMForTokenClassification,
|
||||
MarkupLMModel,
|
||||
)
|
||||
|
||||
# TODO check dependencies
|
||||
from transformers import MarkupLMFeatureExtractor, MarkupLMProcessor, MarkupLMTokenizer
|
||||
|
||||
|
||||
class MarkupLMModelTester:
|
||||
"""You can also import this e.g from .test_modeling_markuplm import MarkupLMModelTester"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
parent,
|
||||
batch_size=13,
|
||||
seq_length=7,
|
||||
is_training=True,
|
||||
use_input_mask=True,
|
||||
use_token_type_ids=True,
|
||||
use_labels=True,
|
||||
vocab_size=99,
|
||||
hidden_size=32,
|
||||
num_hidden_layers=5,
|
||||
num_attention_heads=4,
|
||||
intermediate_size=37,
|
||||
hidden_act="gelu",
|
||||
hidden_dropout_prob=0.1,
|
||||
attention_probs_dropout_prob=0.1,
|
||||
max_position_embeddings=512,
|
||||
type_vocab_size=16,
|
||||
type_sequence_label_size=2,
|
||||
initializer_range=0.02,
|
||||
num_labels=3,
|
||||
scope=None,
|
||||
max_xpath_tag_unit_embeddings=20,
|
||||
max_xpath_subs_unit_embeddings=30,
|
||||
tag_pad_id=2,
|
||||
subs_pad_id=2,
|
||||
max_depth=10,
|
||||
):
|
||||
self.parent = parent
|
||||
self.batch_size = batch_size
|
||||
self.seq_length = seq_length
|
||||
self.is_training = is_training
|
||||
self.use_input_mask = use_input_mask
|
||||
self.use_token_type_ids = use_token_type_ids
|
||||
self.use_labels = use_labels
|
||||
self.vocab_size = vocab_size
|
||||
self.hidden_size = hidden_size
|
||||
self.num_hidden_layers = num_hidden_layers
|
||||
self.num_attention_heads = num_attention_heads
|
||||
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.max_position_embeddings = max_position_embeddings
|
||||
self.type_vocab_size = type_vocab_size
|
||||
self.type_sequence_label_size = type_sequence_label_size
|
||||
self.initializer_range = initializer_range
|
||||
self.num_labels = num_labels
|
||||
self.scope = scope
|
||||
self.max_xpath_tag_unit_embeddings = max_xpath_tag_unit_embeddings
|
||||
self.max_xpath_subs_unit_embeddings = max_xpath_subs_unit_embeddings
|
||||
self.tag_pad_id = tag_pad_id
|
||||
self.subs_pad_id = subs_pad_id
|
||||
self.max_depth = max_depth
|
||||
|
||||
def prepare_config_and_inputs(self):
|
||||
input_ids = ids_tensor([self.batch_size, self.seq_length], self.vocab_size)
|
||||
|
||||
xpath_tags_seq = ids_tensor(
|
||||
[self.batch_size, self.seq_length, self.max_depth], self.max_xpath_tag_unit_embeddings
|
||||
)
|
||||
|
||||
xpath_subs_seq = ids_tensor(
|
||||
[self.batch_size, self.seq_length, self.max_depth], self.max_xpath_subs_unit_embeddings
|
||||
)
|
||||
|
||||
input_mask = None
|
||||
if self.use_input_mask:
|
||||
input_mask = ids_tensor([self.batch_size, self.seq_length], vocab_size=2)
|
||||
|
||||
token_type_ids = None
|
||||
if self.use_token_type_ids:
|
||||
token_type_ids = ids_tensor([self.batch_size, self.seq_length], self.type_vocab_size)
|
||||
|
||||
sequence_labels = None
|
||||
token_labels = None
|
||||
if self.use_labels:
|
||||
sequence_labels = ids_tensor([self.batch_size], self.type_sequence_label_size)
|
||||
token_labels = ids_tensor([self.batch_size, self.seq_length], self.num_labels)
|
||||
|
||||
config = self.get_config()
|
||||
|
||||
return (
|
||||
config,
|
||||
input_ids,
|
||||
xpath_tags_seq,
|
||||
xpath_subs_seq,
|
||||
token_type_ids,
|
||||
input_mask,
|
||||
sequence_labels,
|
||||
token_labels,
|
||||
)
|
||||
|
||||
def get_config(self):
|
||||
return MarkupLMConfig(
|
||||
vocab_size=self.vocab_size,
|
||||
hidden_size=self.hidden_size,
|
||||
num_hidden_layers=self.num_hidden_layers,
|
||||
num_attention_heads=self.num_attention_heads,
|
||||
intermediate_size=self.intermediate_size,
|
||||
hidden_act=self.hidden_act,
|
||||
hidden_dropout_prob=self.hidden_dropout_prob,
|
||||
attention_probs_dropout_prob=self.attention_probs_dropout_prob,
|
||||
max_position_embeddings=self.max_position_embeddings,
|
||||
type_vocab_size=self.type_vocab_size,
|
||||
initializer_range=self.initializer_range,
|
||||
max_xpath_tag_unit_embeddings=self.max_xpath_tag_unit_embeddings,
|
||||
max_xpath_subs_unit_embeddings=self.max_xpath_subs_unit_embeddings,
|
||||
tag_pad_id=self.tag_pad_id,
|
||||
subs_pad_id=self.subs_pad_id,
|
||||
max_depth=self.max_depth,
|
||||
)
|
||||
|
||||
def create_and_check_model(
|
||||
self,
|
||||
config,
|
||||
input_ids,
|
||||
xpath_tags_seq,
|
||||
xpath_subs_seq,
|
||||
token_type_ids,
|
||||
input_mask,
|
||||
sequence_labels,
|
||||
token_labels,
|
||||
):
|
||||
model = MarkupLMModel(config=config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
print("Configs:", model.config.tag_pad_id, model.config.subs_pad_id)
|
||||
result = model(input_ids, attention_mask=input_mask, token_type_ids=token_type_ids)
|
||||
result = model(input_ids, token_type_ids=token_type_ids)
|
||||
result = model(input_ids)
|
||||
self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size))
|
||||
self.parent.assertEqual(result.pooler_output.shape, (self.batch_size, self.hidden_size))
|
||||
|
||||
def create_and_check_for_sequence_classification(
|
||||
self,
|
||||
config,
|
||||
input_ids,
|
||||
xpath_tags_seq,
|
||||
xpath_subs_seq,
|
||||
token_type_ids,
|
||||
input_mask,
|
||||
sequence_labels,
|
||||
token_labels,
|
||||
):
|
||||
config.num_labels = self.num_labels
|
||||
model = MarkupLMForSequenceClassification(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
result = model(
|
||||
input_ids,
|
||||
xpath_tags_seq=xpath_tags_seq,
|
||||
xpath_subs_seq=xpath_subs_seq,
|
||||
attention_mask=input_mask,
|
||||
token_type_ids=token_type_ids,
|
||||
labels=sequence_labels,
|
||||
)
|
||||
self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_labels))
|
||||
|
||||
def create_and_check_for_token_classification(
|
||||
self,
|
||||
config,
|
||||
input_ids,
|
||||
xpath_tags_seq,
|
||||
xpath_subs_seq,
|
||||
token_type_ids,
|
||||
input_mask,
|
||||
sequence_labels,
|
||||
token_labels,
|
||||
):
|
||||
config.num_labels = self.num_labels
|
||||
model = MarkupLMForTokenClassification(config=config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
result = model(
|
||||
input_ids,
|
||||
xpath_tags_seq=xpath_tags_seq,
|
||||
xpath_subs_seq=xpath_subs_seq,
|
||||
attention_mask=input_mask,
|
||||
token_type_ids=token_type_ids,
|
||||
labels=token_labels,
|
||||
)
|
||||
self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.num_labels))
|
||||
|
||||
def create_and_check_for_question_answering(
|
||||
self,
|
||||
config,
|
||||
input_ids,
|
||||
xpath_tags_seq,
|
||||
xpath_subs_seq,
|
||||
token_type_ids,
|
||||
input_mask,
|
||||
sequence_labels,
|
||||
token_labels,
|
||||
):
|
||||
model = MarkupLMForQuestionAnswering(config=config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
result = model(
|
||||
input_ids,
|
||||
xpath_tags_seq=xpath_tags_seq,
|
||||
xpath_subs_seq=xpath_subs_seq,
|
||||
attention_mask=input_mask,
|
||||
token_type_ids=token_type_ids,
|
||||
start_positions=sequence_labels,
|
||||
end_positions=sequence_labels,
|
||||
)
|
||||
self.parent.assertEqual(result.start_logits.shape, (self.batch_size, self.seq_length))
|
||||
self.parent.assertEqual(result.end_logits.shape, (self.batch_size, self.seq_length))
|
||||
|
||||
def prepare_config_and_inputs_for_common(self):
|
||||
config_and_inputs = self.prepare_config_and_inputs()
|
||||
(
|
||||
config,
|
||||
input_ids,
|
||||
xpath_tags_seq,
|
||||
xpath_subs_seq,
|
||||
token_type_ids,
|
||||
input_mask,
|
||||
sequence_labels,
|
||||
token_labels,
|
||||
) = config_and_inputs
|
||||
inputs_dict = {
|
||||
"input_ids": input_ids,
|
||||
"xpath_tags_seq": xpath_tags_seq,
|
||||
"xpath_subs_seq": xpath_subs_seq,
|
||||
"token_type_ids": token_type_ids,
|
||||
"attention_mask": input_mask,
|
||||
}
|
||||
return config, inputs_dict
|
||||
|
||||
|
||||
@require_torch
|
||||
class MarkupLMModelTest(ModelTesterMixin, unittest.TestCase):
|
||||
all_model_classes = (
|
||||
(
|
||||
MarkupLMModel,
|
||||
MarkupLMForSequenceClassification,
|
||||
MarkupLMForTokenClassification,
|
||||
MarkupLMForQuestionAnswering,
|
||||
)
|
||||
if is_torch_available()
|
||||
else None
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = MarkupLMModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=MarkupLMConfig, hidden_size=37)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_model(*config_and_inputs)
|
||||
|
||||
def test_for_sequence_classification(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_for_sequence_classification(*config_and_inputs)
|
||||
|
||||
def test_for_token_classification(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_for_token_classification(*config_and_inputs)
|
||||
|
||||
def test_for_question_answering(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_for_question_answering(*config_and_inputs)
|
||||
|
||||
|
||||
def prepare_html_string():
|
||||
html_string = """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Page Title</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>This is a Heading</h1>
|
||||
<p>This is a paragraph.</p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
return html_string
|
||||
|
||||
|
||||
@require_torch
|
||||
class MarkupLMModelIntegrationTest(unittest.TestCase):
|
||||
@cached_property
|
||||
def default_processor(self):
|
||||
# TODO use from_pretrained here
|
||||
feature_extractor = MarkupLMFeatureExtractor()
|
||||
tokenizer = MarkupLMTokenizer.from_pretrained("microsoft/markuplm-base")
|
||||
|
||||
return MarkupLMProcessor(feature_extractor, tokenizer)
|
||||
|
||||
@slow
|
||||
def test_forward_pass_no_head(self):
|
||||
model = MarkupLMModel.from_pretrained("microsoft/markuplm-base").to(torch_device)
|
||||
|
||||
processor = self.default_processor
|
||||
|
||||
inputs = processor(prepare_html_string(), return_tensors="pt")
|
||||
inputs = inputs.to(torch_device)
|
||||
|
||||
# forward pass
|
||||
with torch.no_grad():
|
||||
outputs = model(**inputs)
|
||||
|
||||
# verify the last hidden states
|
||||
expected_shape = torch.Size([1, 14, 768])
|
||||
self.assertEqual(outputs.last_hidden_state.shape, expected_shape)
|
||||
|
||||
expected_slice = torch.tensor(
|
||||
[[0.0267, -0.1289, 0.4930], [-0.2376, -0.0342, 0.2381], [-0.0329, -0.3785, 0.0263]]
|
||||
).to(torch_device)
|
||||
|
||||
self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3], expected_slice, atol=1e-4))
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -3,6 +3,7 @@ docs/source/es/quicktour.mdx
|
|||
docs/source/en/pipeline_tutorial.mdx
|
||||
docs/source/en/autoclass_tutorial.mdx
|
||||
docs/source/en/task_summary.mdx
|
||||
docs/source/en/model_doc/markuplm.mdx
|
||||
docs/source/en/model_doc/speech_to_text.mdx
|
||||
docs/source/en/model_doc/t5.mdx
|
||||
docs/source/en/model_doc/t5v1.1.mdx
|
||||
|
@ -51,6 +52,7 @@ src/transformers/models/longformer/modeling_longformer.py
|
|||
src/transformers/models/longformer/modeling_tf_longformer.py
|
||||
src/transformers/models/longt5/modeling_longt5.py
|
||||
src/transformers/models/marian/modeling_marian.py
|
||||
src/transformers/models/markuplm/modeling_markuplm.py
|
||||
src/transformers/models/mbart/modeling_mbart.py
|
||||
src/transformers/models/mobilebert/modeling_mobilebert.py
|
||||
src/transformers/models/mobilebert/modeling_tf_mobilebert.py
|
||||
|
|
Loading…
Reference in New Issue