Views
No views yet
This is an RKNN-compatible version of the distilbert/distilbert-base-cased-distilled-squad model. It has been optimized for Rockchip NPUs using the rk-transformers library.
| Model File | Optimization Level | Quantization | File Size |
|---|---|---|---|
| model.rknn | 0 | float16 | 128.1 MB |
rk-transformers with inference dependencies to use this model:pip install rk-transformers[inference]1from rktransformers import RKModelForQuestionAnswering
2from transformers import AutoTokenizer
3
4tokenizer = AutoTokenizer.from_pretrained("rk-transformers/distilbert-base-cased-distilled-squad")
5model = RKModelForQuestionAnswering.from_pretrained(
6 "rk-transformers/distilbert-base-cased-distilled-squad",
7 platform="rk3588",
8 core_mask="auto",
9)
10
11question, text = "Who was Jim Henson?", "Jim Henson was a nice puppet"
12inputs = tokenizer(question, text, return_tensors="np")
13outputs = model(**inputs)
14start_logits = outputs.start_logits
15end_logits = outputs.end_logits
16print(start_logits.shape)
17print(end_logits.shape)1>>> from transformers import pipeline
2>>> question_answerer = pipeline("question-answering", model='distilbert-base-cased-distilled-squad')
3
4>>> context = r"""
5... Extractive Question Answering is the task of extracting an answer from a text given a question. An example of a
6... question answering dataset is the SQuAD dataset, which is entirely based on that task. If you would like to fine-tune
7... a model on a SQuAD task, you may leverage the examples/pytorch/question-answering/run_squad.py script.
8... """
9
10>>> result = question_answerer(question="What is a good example of a question answering dataset?", context=context)
11>>> print(
12... f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}"
13...)
14
15Answer: 'SQuAD dataset', score: 0.5152, start: 147, end: 1601from transformers import DistilBertTokenizer, DistilBertModel
2import torch
3tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-cased-distilled-squad')
4model = DistilBertModel.from_pretrained('distilbert-base-cased-distilled-squad')
5
6question, text = "Who was Jim Henson?", "Jim Henson was a nice puppet"
7
8inputs = tokenizer(question, text, return_tensors="pt")
9with torch.no_grad():
10 outputs = model(**inputs)
11
12print(outputs)1from transformers import DistilBertTokenizer, TFDistilBertForQuestionAnswering
2import tensorflow as tf
3
4tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-cased-distilled-squad")
5model = TFDistilBertForQuestionAnswering.from_pretrained("distilbert-base-cased-distilled-squad")
6
7question, text = "Who was Jim Henson?", "Jim Henson was a nice puppet"
8
9inputs = tokenizer(question, text, return_tensors="tf")
10outputs = model(**inputs)
11
12answer_start_index = int(tf.math.argmax(outputs.start_logits, axis=-1)[0])
13answer_end_index = int(tf.math.argmax(outputs.end_logits, axis=-1)[0])
14
15predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1]
16tokenizer.decode(predict_answer_tokens)1>>> from transformers import pipeline
2>>> question_answerer = pipeline("question-answering", model='distilbert-base-cased-distilled-squad')
3
4>>> context = r"""
5... Alice is sitting on the bench. Bob is sitting next to her.
6... """
7
8>>> result = question_answerer(question="Who is the CEO?", context=context)
9>>> print(
10... f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}"
11...)
12
13Answer: 'Bob', score: 0.7527, start: 32, end: 35DistilBERT pretrained on the same data as BERT, which is BookCorpus, a dataset consisting of 11,038 unpublished books and English Wikipedia (excluding lists, tables and headers).
This model reaches a F1 score of 87.1 on the [SQuAD v1.1] dev set (for comparison, BERT bert-base-cased version reaches a F1 score of 88.7).
1@inproceedings{sanh2019distilbert,
2 title={DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter},
3 author={Sanh, Victor and Debut, Lysandre and Chaumond, Julien and Wolf, Thomas},
4 booktitle={NeurIPS EMC^2 Workshop},
5 year={2019}
6}