Compare commits

..

34 Commits

Author SHA1 Message Date
chenxiaozeng
7c96f95ea6 Add files via upload 2022-05-13 00:01:38 +08:00
chenxiaozeng
0e8603c21c Delete wechat.png 2022-05-13 00:01:15 +08:00
chenxiaozeng
3372a3382a Add files via upload 2022-05-04 22:14:49 +08:00
chenxiaozeng
63f9d88d9e Delete wechat.png 2022-05-04 22:14:29 +08:00
chenxiaozeng
74f46464aa Add files via upload 2022-04-26 15:27:46 +08:00
chenxiaozeng
f7575220b9 Delete wechat.png 2022-04-26 15:26:40 +08:00
chenxiaozeng
e288d7f07c Add files via upload 2022-04-22 16:24:56 +08:00
chenxiaozeng
37644cc512 Delete wechat.png 2022-04-22 16:24:30 +08:00
chenxiaozeng
5c59a81cb7 Add files via upload 2022-04-15 17:41:12 +08:00
chenxiaozeng
10481e9888 Delete wechat.png 2022-04-15 17:40:51 +08:00
chenxiaozeng
097220364b Add files via upload 2022-04-15 17:38:50 +08:00
chenxiaozeng
9cb33e166b Delete wechat.png 2022-04-15 17:38:27 +08:00
chenxiaozeng
dbecb4af2e Add files via upload 2022-04-15 17:35:01 +08:00
chenxiaozeng
12e2db7422 Delete wechat.png 2022-04-15 17:34:25 +08:00
chenxiaozeng
7a6b934547 Delete wechat.png 2022-04-08 22:39:21 +08:00
chenxiaozeng
a9eb71b90a Add files via upload 2022-04-08 22:33:45 +08:00
chenxiaozeng
eeca1a2054 Delete wechat.png 2022-04-08 22:33:04 +08:00
chenxiaozeng
196793bb63 Add files via upload 2022-04-08 22:30:01 +08:00
chenxiaozeng
3427973ee0 Delete wechat.png 2022-04-08 22:29:41 +08:00
chenxiaozeng
69be464be8 Add files via upload 2022-04-08 22:28:30 +08:00
chenxiaozeng
e7a865111b Delete wechat.png 2022-04-08 22:28:09 +08:00
chenxiaozeng
83f7c91f71 Add files via upload 2022-04-01 20:46:09 +08:00
chenxiaozeng
f83cd7394a Delete wechat.png 2022-04-01 20:45:52 +08:00
chenxiaozeng
c4932f8e2d Update group code
Update group code
2022-03-25 22:52:34 +08:00
chenxiaozeng
a4259164bf Update group code 2022-03-25 22:51:07 +08:00
chenxiaozeng
c82e777b30 Delete wechat.png 2022-03-25 22:40:33 +08:00
chenxiaozeng
b070e720a0 Update group code
Update group code
2022-03-25 22:37:47 +08:00
chenxiaozeng
3afa7bd052 add wechat group code
add wechat group code
2022-03-18 20:34:11 +08:00
wawltor
867407d9cf Add the tokeniers version in fastertokenizer (#1462) 2021-12-14 15:38:51 +08:00
wawltor
c982271883 Merge pull request #1447 from Steffy-zxf/up-2.2
[cherrypick-2.2] update paddle verion requires
2021-12-11 14:20:20 +08:00
Steffy-zxf
1bc6c86036 update paddle verion requires 2021-12-11 14:08:47 +08:00
wawltor
34cea03b20 Update README.md 2021-12-11 13:20:08 +08:00
wawltor
4bab6e647d Merge pull request #1446 from wawltor/faster_tokenizer_cp_new
update the document in the FasterTokenizer examples
2021-12-11 13:11:38 +08:00
wawltor
45bb58cfcc Update README.md 2021-12-11 05:09:12 +00:00
6703 changed files with 222217 additions and 975035 deletions

View File

@@ -1,134 +0,0 @@
# Copyright (c) 2020 PaddlePaddle Authors. 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 __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
import argparse
import io
import re
import sys
import os
import datetime
COPYRIGHT = '''Copyright (c) 2022 PaddlePaddle Authors. 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.'''
def _generate_copyright(comment_mark):
copyright=COPYRIGHT.split(os.linesep)
header = copyright[0].rstrip()
p = re.search('(\d{4})', header).group(0)
now = datetime.datetime.now()
header = header.replace(p,str(now.year))
ans=[comment_mark + " " + header + os.linesep]
for idx, line in enumerate(copyright[1:]):
ans.append(comment_mark + " " + line.rstrip() + os.linesep)
return ans
def _get_comment_mark(path):
lang_type=re.compile(r"\.(py|sh)$")
if lang_type.search(path) is not None:
return "#"
lang_type=re.compile(r"\.(h|c|hpp|cc|cpp|cu|go|cuh|proto)$")
if lang_type.search(path) is not None:
return "//"
return None
RE_ENCODE = re.compile(r"^[ \t\v]*#.*?coding[:=]", re.IGNORECASE)
RE_COPYRIGHT = re.compile(r".*Copyright( \(c\))* \d{4}", re.IGNORECASE)
RE_SHEBANG = re.compile(r"^[ \t\v]*#[ \t]?\!")
def _check_copyright(path):
head=[]
try:
with open(path, encoding="utf-8") as f:
head = [next(f) for x in range(4)]
except StopIteration:
pass
for idx, line in enumerate(head):
if RE_COPYRIGHT.search(line) is not None:
return True
return False
def generate_copyright(path, comment_mark):
original_contents = io.open(path, encoding="utf-8").readlines()
head = original_contents[0:4]
insert_line_no=0
for i, line in enumerate(head):
if RE_ENCODE.search(line) or RE_SHEBANG.search(line):
insert_line_no=i+1
copyright = _generate_copyright(comment_mark)
if insert_line_no == 0:
new_contents = copyright
if len(original_contents) > 0 and len(original_contents[0].strip()) != 0:
new_contents.append(os.linesep)
new_contents.extend(original_contents)
else:
new_contents=original_contents[0:insert_line_no]
new_contents.append(os.linesep)
new_contents.extend(copyright)
if len(original_contents) > insert_line_no and len(original_contents[insert_line_no].strip()) != 0:
new_contents.append(os.linesep)
new_contents.extend(original_contents[insert_line_no:])
new_contents="".join(new_contents)
with io.open(path, 'w') as output_file:
output_file.write(new_contents)
def main(argv=None):
parser = argparse.ArgumentParser(
description='Checker for copyright declaration.')
parser.add_argument('filenames', nargs='*', help='Filenames to check')
args = parser.parse_args(argv)
retv = 0
for path in args.filenames:
comment_mark = _get_comment_mark(path)
if comment_mark is None:
print("warning:Unsupported file", path, file=sys.stderr)
continue
if _check_copyright(path):
continue
generate_copyright(path, comment_mark)
if __name__ == '__main__':
exit(main())

View File

@@ -1,7 +0,0 @@
[flake8]
ignore = E203, E402, E501, E731, E741, W503, W605, E722
max-line-length = 119
# E402: module level import not at top of file
per-file-ignores =
__init__.py:F401,F403,E402

View File

@@ -1,89 +0,0 @@
**简体中文**🀄 | [English🌎](./CODE_OF_CONDUCT_en.md)
# 贡献者公约
## 我们的承诺
身为社区成员、贡献者和领袖,我们承诺使社区参与者不受骚扰,无论其年龄、体型、可见或不可见的缺陷、族裔、性征、性别认同和表达、经验水平、教育程度、社会与经济地位、国籍、相貌、种族、种姓、肤色、宗教信仰、性倾向或性取向如何。
我们承诺以有助于建立开放、友善、多样化、包容、健康社区的方式行事和互动。
## 我们的准则
有助于为我们的社区创造积极环境的行为例子包括但不限于:
* 表现出对他人的同情和善意
* 尊重不同的主张、观点和感受
* 提出和大方接受建设性意见
* 承担责任并向受我们错误影响的人道歉
* 注重社区共同诉求,而非个人得失
不当行为例子包括:
* 使用情色化的语言或图像,及性引诱或挑逗
* 嘲弄、侮辱或诋毁性评论,以及人身或政治攻击
* 公开或私下的骚扰行为
* 未经他人明确许可,公布他人的私人信息,如物理或电子邮件地址
* 其他有理由认定为违反职业操守的不当行为
## 责任和权力
社区领袖有责任解释和落实我们所认可的行为准则,并妥善公正地对他们认为不当、威胁、冒犯或有害的任何行为采取纠正措施。
社区领导有权力和责任删除、编辑或拒绝或拒绝与本行为准则不相符的评论(comment)、提交(commits)、代码、维基(wiki)编辑、议题(issues)或其他贡献,并在适当时机知采取措施的理由。
## 适用范围
本行为准则适用于所有社区场合,也适用于在公共场所代表社区时的个人。
代表社区的情形包括使用官方电子邮件地址、通过官方社交媒体帐户发帖或在线上或线下活动中担任指定代表。
## 监督
辱骂、骚扰或其他不可接受的行为可通过 paddlenlp@baidu.com 向负责监督的社区领袖报告。
所有投诉都将得到及时和公平的审查和调查。
所有社区领袖都有义务尊重任何事件报告者的隐私和安全。
## 处理方针
社区领袖将遵循下列社区处理方针来明确他们所认定违反本行为准则的行为的处理方式:
### 1. 纠正
**社区影响**:使用不恰当的语言或其他在社区中被认定为不符合职业道德或不受欢迎的行为。
**处理意见**:由社区领袖发出非公开的书面警告,明确说明违规行为的性质,并解释举止如何不妥。或将要求公开道歉。
### 2. 警告
**社区影响**:单个或一系列违规行为。
**处理意见**:警告并对连续性行为进行处理。在指定时间内,不得与相关人员互动,包括主动与行为准则执行者互动。这包括避免在社区场所和外部渠道中的互动。违反这些条款可能会导致临时或永久封禁。
### 3. 临时封禁
**社区影响**: 严重违反社区准则,包括持续的不当行为。
**处理意见**: 在指定时间内,暂时禁止与社区进行任何形式的互动或公开交流。在此期间,不得与相关人员进行公开或私下互动,包括主动与行为准则执行者互动。违反这些条款可能会导致永久封禁。
### 4. 永久封禁
**社区影响**:行为模式表现出违反社区准则,包括持续的不当行为、骚扰个人或攻击或贬低某个类别的个体。
**处理意见**:永久禁止在社区内进行任何形式的公开互动。
## 参见
本行为准则改编自 [Contributor Covenant][homepage] 2.1 版, 参见 [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]。
社区处理方针灵感来源于 [Mozilla's code of conduct enforcement ladder][Mozilla CoC]。
有关本行为准则的常见问题的答案,参见 [https://www.contributor-covenant.org/faq][FAQ]。
其他语言翻译参见 [https://www.contributor-covenant.org/translations][translations]。
[homepage]: https://www.contributor-covenant.org
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
[Mozilla CoC]: https://github.com/mozilla/diversity
[FAQ]: https://www.contributor-covenant.org/faq
[translations]: https://www.contributor-covenant.org/translations

View File

@@ -1,134 +0,0 @@
[简体中文🀄](./CODE_OF_CONDUCT.md) | **English**🌎
# Contributor Covenant Code of Conduct
## Our Pledge
We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, caste, color, religion, or sexual
identity and orientation.
We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.
## Our Standards
Examples of behavior that contributes to a positive environment for our
community include:
* Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
* Focusing on what is best not just for us as individuals, but for the overall
community
Examples of unacceptable behavior include:
* The use of sexualized language or imagery, and sexual attention or advances of
any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email address,
without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Enforcement Responsibilities
Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.
Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.
## Scope
This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
paddlenlp@baidu.com.
All complaints will be reviewed and investigated promptly and fairly.
All community leaders are obligated to respect the privacy and security of the
reporter of any incident.
## Enforcement Guidelines
Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:
### 1. Correction
**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.
**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.
### 2. Warning
**Community Impact**: A violation through a single incident or series of
actions.
**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or permanent
ban.
### 3. Temporary Ban
**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.
**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.
### 4. Permanent Ban
**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.
**Consequence**: A permanent ban from any sort of public interaction within the
community.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.1, available at
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
Community Impact Guidelines were inspired by
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
For answers to common questions about this code of conduct, see the FAQ at
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
[https://www.contributor-covenant.org/translations][translations].
[homepage]: https://www.contributor-covenant.org
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
[Mozilla CoC]: https://github.com/mozilla/diversity
[FAQ]: https://www.contributor-covenant.org/faq
[translations]: https://www.contributor-covenant.org/translations

View File

@@ -1,154 +0,0 @@
[简体中文🀄](../CONTRIBUTING.md) | **English**🌎
# Contributing to PaddleNLP
We highly welcome and value your contributions to `PaddleNLP`. The first step to start your contribution is to sign the [PaddlePaddle Contributor License Agreement](https://cla-assistant.io/PaddlePaddle/PaddleNLP).
This document explains our workflow and work style:
## Finding out what to work on Workflow
## Development Workflow
PaddleNLP uses the [Git branching model](http://nvie.com/posts/a-successful-git-branching-model/). The following steps guide usual contributions.
#### 1. Fork
Our development community has been growing fastly; it doesn't make sense for everyone to write into the official repo. So, please file Pull Requests from your fork. To make a fork, just head over to the GitHub page and click the ["Fork" button](https://help.github.com/articles/fork-a-repo/).
#### 2. Clone
To make a copy of your fork to your local computers, please run
```bash
git clone https://github.com/<your-github-account>/PaddleNLP
cd PaddleNLP
```
#### 3. Create the local feature branch
For daily works like adding a new feature or fixing a bug, please open your feature branch before coding:
```bash
git checkout -b my-cool-feature
```
#### 4. Set up the development environment
Before you start coding, you need to setup the development environment. We highly recommend doing all your development in a virtual environment such as
[venv](https://docs.python.org/3/library/venv.html) or [conda](https://docs.conda.io/en/latest/). After you setup and activated your virtual environment,
run the following command:
```bash
make install
```
This will setup all the dependencies of `PaddleNLP` as well as the [`pre-commit`](http://pre-commit.com/) tool.
If you are working on the `examples` or `applications` module and require importing from `PaddleNLP`, make sure you install `PaddleNLP` in editable mode.
If `PaddleNLP` is already installed in the virtual environment, remove it with `pip uninstall paddlenlp` before reinstalling it in editable mode with
`pip install -e .`
#### 5. Develop
As you develop your new exciting feature, keep in mind that it should be covered by unit tests. All of our unit tests can be found under the `tests` directory.
You can either modify existing unit test to cover the new feature, or create a new test from scratch.
As you finish up the your code, you should make sure the test suite passes. You can run the tests impacted by your changes like this:
```bash
pytest tests/<test_to_run>.py
```
#### 6. Commit
We utilizes [`pre-commit`](http://pre-commit.com/) (with [black](https://black.readthedocs.io/en/stable/), [isort](https://pycqa.github.io/isort/) and
[flake8](https://flake8.pycqa.org/en/latest/) under the hood) to check the style of code and documentation in every commit. When you run run `git commit`, you will see
something like the following:
```
➜ (my-virtual-env) git commit -m "commiting my cool feature"
black....................................................................Passed
isort....................................................................Passed
flake8...................................................................Passed
check for merge conflicts................................................Passed
check for broken symlinks............................(no files to check)Skipped
detect private key.......................................................Passed
fix end of files.....................................(no files to check)Skipped
trim trailing whitespace.............................(no files to check)Skipped
CRLF end-lines checker...............................(no files to check)Skipped
CRLF end-lines remover...............................(no files to check)Skipped
No-tabs checker......................................(no files to check)Skipped
Tabs remover.........................................(no files to check)Skipped
copyright_checker........................................................Passed
```
But most of the time things don't go so smoothly. When your code or documentation doesn't meet the standard, the `pre-commit` check will fail.
```
➜ (my-virtual-env) git commit -m "commiting my cool feature"
black....................................................................Passed
isort....................................................................Failed
- hook id: isort
- files were modified by this hook
Fixing examples/information_extraction/waybill_ie/run_ernie_crf.py
flake8...................................................................Passed
check for merge conflicts................................................Passed
check for broken symlinks............................(no files to check)Skipped
detect private key.......................................................Passed
fix end of files.....................................(no files to check)Skipped
trim trailing whitespace.............................(no files to check)Skipped
CRLF end-lines checker...............................(no files to check)Skipped
CRLF end-lines remover...............................(no files to check)Skipped
No-tabs checker......................................(no files to check)Skipped
Tabs remover.........................................(no files to check)Skipped
copyright_checker........................................................Passed
```
But **don't panic**!
Our tooling will fix most of the style errors automatically. Some errors will need to be addressed manually. Fortunately, the error messages are straight forward and
the errors are usually simple to fix. After addressing the errors, you can run `git add <files>` and `git commit` again, which will trigger `pre-commit` again.
Once the `pre-commit` checks pass, you are ready to push the code.
[Google][http://google.com/] or [StackOverflow](https://stackoverflow.com/) are great tools to help you understand the code style errors.
Don't worry if you still can't figure it out. You can commit with `git commit -m "style error" --no-verify` and we are happy to help you once you create a Pull Request.
#### 7. Keep pulling
An experienced Git user pulls from the official repo often -- daily or even hourly, so they notice conflicts with others work early, and it's easier to resolve smaller conflicts.
```bash
git remote add upstream https://github.com/PaddlePaddle/PaddleNLP
git pull upstream develop
```
#### 8. Push and file a pull request
You can "push" your local work into your forked repo:
```bash
git push origin my-cool-stuff
```
The push allows you to create a pull request, requesting owners of this [official repo](https://github.com/PaddlePaddle/PaddleNLP) to pull your change into the official one.
To create a pull request, please follow [these steps](https://help.github.com/articles/creating-a-pull-request/).
#### 9. Delete local and remote branches
To keep your local workspace and your fork clean, you might want to remove merged branches:
```bash
git push origin my-cool-stuff
git checkout develop
git pull upstream develop
git branch -d my-cool-stuff
```
## Code Review
- Please feel free to ping your reviewers by @-mentioning the in the Pull Request. Please do this after your pull request passes the CI.
- Please answer reviewers' every comment. If you are to follow the comment, please write "Done"; Otherwise, please start a discussion under the comment.
- If you don't want your reviewers to get overwhelmed by email notifications, you might reply their comments by [in a batch](https://help.github.com/articles/reviewing-proposed-changes-in-a-pull-request/).

15
.github/ISSUE_TEMPLATE/----.md vendored Normal file
View File

@@ -0,0 +1,15 @@
---
name: 问题反馈
about: PaddleNLP问题反馈
title: ''
labels: ''
assignees: ''
---
欢迎您反馈PaddleNLP使用问题,非常感谢您对PaddleNLP的贡献!
在留下您的问题时,辛苦您同步提供如下信息:
- 版本、环境信息
1)PaddleNLP和PaddlePaddle版本:请提供您的PaddleNLP和PaddlePaddle版本号,例如PaddleNLP 2.0.4,PaddlePaddle2.1.1
2)系统环境:请您描述系统类型,例如Linux/Windows/MacOS/,python版本
- 复现信息:如为报错,请给出复现环境、复现步骤

View File

@@ -1,23 +0,0 @@
name: 🐛 Ask Question
description: 请描述您使用PaddleNLP时遇到的问题
title: "[Question]: "
labels:
- question
body:
- type: markdown
attributes:
value: >
#### 你可以在这里提出一个使用/咨询问题,提问之前请确保:
- 1)已经百度/谷歌搜索过你的问题,但是没有找到解答;
- 2)已经在官网查询过[API文档](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/index_cn.html)与[FAQ](https://www.paddlepaddle.org.cn/documentation/docs/zh/faq/index_cn.html),但是没有找到解答;
- 3)已经在[历史issue](https://github.com/PaddlePaddle/Paddle/issues)中搜索过,没有找到同类issue或issue未被解答。
- type: textarea
id: question
attributes:
label: 请提出你的问题
validations:
required: true

View File

@@ -1,45 +0,0 @@
name: 🐛 Bug Report
description: PaddleNLP问题反馈
title: "[Bug]: "
labels: bug
body:
- type: textarea
id: environment
attributes:
label: 软件环境
description: |
请使用以下命令给出您本地Paddle相关包信息
```sh
pip list | grep paddle
```
value: |
- paddlepaddle:
- paddlepaddle-gpu:
- paddlenlp:
render: Markdown
validations:
required: true
- type: checkboxes
id: dumplicated-problem
attributes:
label: 重复问题
description: 是否已在issues中搜索相关问题
options:
- label: I have searched the existing issues
required: true
- type: textarea
id: descripton
attributes:
label: 错误描述
description: 给出错误详细描述,以便能够更好的追踪相关问题
render: Markdown
validations:
required: true
- type: textarea
id: mvp-code
attributes:
label: 稳定复现步骤 & 代码
description: 请给出稳定复现该问题的步骤 & 代码,以便相关人员能够快速定位到具体问题。
validations:
required: true

View File

@@ -1,32 +0,0 @@
name: 🐛 Docs Report
description: PaddleNLP文档反馈
title: "[Docs]: "
labels:
- documentation
body:
- type: textarea
id: environment
attributes:
label: 软件环境
description: |
请使用以下命令给出您本地Paddle相关包信息
```sh
pip list | grep paddle
```
value: |
- paddlepaddle:
- paddlepaddle-gpu:
- paddlenlp:
render: Markdown
validations:
required: true
- type: textarea
id: description
attributes:
label: 详细描述
description: 请详细描述您想要反馈的具体问题
render: Markdown
validations:
required: true

View File

@@ -1,30 +0,0 @@
name: "\U0001F680 Feature request"
description: 请详细描述您所需功能
labels: [ "feature" ]
body:
- type: textarea
id: feature-request
validations:
required: true
attributes:
label: Feature request
description: |
对特性提案的清晰而简明的描述。如果论文和代码存在,请提供链接。
- type: textarea
id: motivation
validations:
required: true
attributes:
label: Motivation
description: |
请概述这项建议的动机。您的特性要求与问题有关吗?
- type: textarea
id: contribution
validations:
required: true
attributes:
label: Your contribution
description: |
Is there any way that you could help, e.g. by submitting a PR? Make sure to read the CONTRIBUTING.MD [readme](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md)

View File

@@ -1,28 +0,0 @@
name: "\U0001F31F 添加新模型"
description: 请为新模型提交一份说明
labels: [ "New model" ]
body:
- type: textarea
id: description-request
validations:
required: true
attributes:
label: 简要描述
description: |
请简要描述模型的类型、解决的问题等。
- type: checkboxes
id: information-tasks
attributes:
label: 是否已开源
options:
- label: 已开源
- label: 未开源
- type: textarea
id: additional-info
attributes:
label: 模型详细信息
description: |
请给出新模型相关信息,如论文地址、现存代码地址等。

View File

@@ -1,23 +0,0 @@
name: 🧩 其他 Others
description: 提出其他问题。
labels: [others]
body:
- type: markdown
attributes:
value: >
#### 你可以在这里提出任何前面几类模板不适用的问题,包括但不限于:优化性建议、框架使用体验反馈、版本兼容性问题、报错信息不清楚等。
- type: textarea
id: others
attributes:
label: 问题描述
validations:
required: true
- type: markdown
attributes:
value: >
感谢你的贡献 🎉!

View File

@@ -1,18 +1,4 @@
<!-- Demo: https://github.com/PaddlePaddle/PaddleNLP/pull/26 -->
#### Before submitting
- [ ] Lint code. If there are lint issues, please format the code first.
```shell
# Install and register `pre-commit` in the project folder
pip install pre-commit && pre-commit install
# Process previous code files separately
pre-commit run --file XXXX.py
```
- [ ] Add test cases into `tests` folder. If there are codecov issues, please add tests cases first.
### PR types
<!-- One of [ New features | Bug fixes | Function optimization | Performance optimization | Breaking changes | Others ] -->

View File

@@ -1,41 +0,0 @@
name: "Check bypass"
description: "A custom action to encapsulate PFCCLab/ci-bypass"
inputs:
github-token:
description: "GitHub token"
required: true
workflow-name:
description: "Workflow name"
required: true
outputs:
can-skip:
description: "Whether the workflow can be skipped."
value: ${{ steps.check-bypass.outputs.can-skip }}
runs:
using: "composite"
steps:
- id: check-bypass
name: Check Bypass
env:
CI_TEAM_MEMBERS: '["tianshuo78520a", "swgu98", "risemeup1", "XieYunshen","luotao1","From00"]'
uses: PFCCLab/ci-bypass@v1
with:
github-token: ${{ inputs.github-token }}
non-pull-request-event-strategy: 'never-skipped'
type: 'composite'
composite-rule: |
{
"any": [
{
"type": "labeled",
"label": ["skip-ci: ${{ inputs.workflow-name }}", "skip-ci: all"],
"username": ${{ env.CI_TEAM_MEMBERS }}
},
{
"type": "commented",
"comment-pattern": [".*/skip-ci ${{ inputs.workflow-name }}.*", ".*/skip-ci all.*"],
"username": ${{ env.CI_TEAM_MEMBERS }}
}
]
}

View File

@@ -1,30 +0,0 @@
name: 'Rerun Workflow'
description: 'Re-run GitHub Actions workflow for a given Pull Request'
inputs:
GITHUB_TOKEN:
description: 'GitHub token with repo scope'
required: true
OWNER:
description: 'Repository owner'
required: true
REPO:
description: 'Repository name'
required: true
PR_ID:
description: 'Pull Request ID'
required: true
JOB_NAME:
description: 'Job name to rerun'
required: true
runs:
using: 'composite'
steps:
- run: bash ./.github/actions/rerun-workflow/rerun.sh
shell: bash
env:
GITHUB_TOKEN: ${{ inputs.GITHUB_TOKEN }}
OWNER: ${{ inputs.OWNER }}
REPO: ${{ inputs.REPO }}
PR_ID: ${{ inputs.PR_ID }}
JOB_NAME: ${{ inputs.JOB_NAME }}

View File

@@ -1,77 +0,0 @@
# Copyright (c) 2025 PaddleNLP Authors. 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.
set -e
COMMIT_SHA=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$OWNER/$REPO/pulls/$PR_ID" | jq -r '.head.sha')
echo "Commit SHA: $COMMIT_SHA"
response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$OWNER/$REPO/actions/runs?head_sha=$COMMIT_SHA&per_page=100")
echo "Response: $response"
run_ids=$(echo "$response" | jq -r '.workflow_runs[].id')
if [ -n "$run_ids" ]; then
echo "Found run_ids for commit $COMMIT_SHA: $run_ids"
for run_id in $run_ids; do
if [ "$JOB_NAME" = "all-failed" ]; then
echo "Rerunning all failed jobs for run_id: $run_id"
rerun_response=$(curl -X POST -s -w "%{http_code}" -o /dev/null \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
"https://api.github.com/repos/$OWNER/$REPO/actions/runs/$run_id/rerun-failed-jobs")
if [ "$rerun_response" -eq 201 ]; then
echo "Successfully requested rerun for all blocked jobs in run_id: $run_id"
else
echo "Failed to request rerun for run_id: $run_id with status code $rerun_response"
fi
else
jobs_response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$OWNER/$REPO/actions/runs/$run_id/jobs")
echo "Jobs Response for run_id $run_id: $jobs_response"
# if [[ "$JOB_NAME" == *"bypass"* ]]; then
block_jobs=$(echo "$jobs_response" | jq -r --arg job_name "$JOB_NAME" \
'.jobs[] | select(.name == $job_name) | .id')
# else
# block_jobs=$(echo "$jobs_response" | jq -r --arg job_name "$JOB_NAME" \
# '.jobs[] | select(.name == $job_name and .conclusion != "success") | .id')
# fi
if [ -n "$block_jobs" ]; then
echo "Found block jobs for run_id $run_id: $block_jobs"
for job_id in $block_jobs; do
echo "Rerunning job_id: $job_id"
curl -X POST -H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$OWNER/$REPO/actions/jobs/$job_id/rerun"
done
else
echo "No block jobs found for run_id $run_id with name $JOB_NAME."
fi
fi
done
else
echo "No matching workflow runs found for commit $COMMIT_SHA."
exit 1
fi

13
.github/codecov.yml vendored
View File

@@ -1,13 +0,0 @@
codecov:
notify:
wait_for_ci: false
coverage:
status:
project:
default:
target: 58% # overall project Coverage
threshold: 1% # Allow the coverage to drop by 1%, and posting a success status.
patch:
default:
target: 80% # lines adjusted Coverage < 80% CI will fail

View File

@@ -1,47 +0,0 @@
name: PaddleFormers PR Check
on:
pull_request:
types: [opened, synchronize]
env:
BRANCH: ${{ github.base_ref }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_ID: ${{ github.event.pull_request.number }}
COMMIT_ID: ${{ github.event.pull_request.head.sha }}
token: ${{ vars.ACTION_GITHUB_TOKEN }}
jobs:
check-approvers:
name: Check approval
runs-on:
group: APPROVAL
steps:
- name: Cleanup
run: |
rm -rf * .[^.]*
- name: Update paddle
run: |
wget -q --no-proxy https://xly-devops.bj.bcebos.com/PaddleTest/PaddleNLP/PaddleNLP-develop.tar.gz --no-check-certificate
tar zxf PaddleNLP-develop.tar.gz --strip-components=1 >/dev/null
rm -rf PaddleNLP-develop.tar.gz >/dev/null
git fetch origin pull/${PR_ID}/head
git checkout -b origin_pr FETCH_HEAD
git remote add upstream https://github.com/PaddlePaddle/PaddleNLP.git
git fetch upstream
git checkout -b test_pr upstream/${BRANCH}
git merge --no-edit origin_pr
git log --pretty=oneline -10
- name: Check bypass
id: check-bypass
uses: ./.github/actions/check-bypass
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
workflow-name: approval
- name: Display Required Approvers
if: steps.check-bypass.outputs.can-skip != 'true'
run: |
cd scripts/ci_approval
bash run_ci_approval_cherry-pick.sh

View File

@@ -1,47 +0,0 @@
name: Approval
on:
pull_request:
types: [opened, synchronize]
env:
BRANCH: ${{ github.base_ref }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_ID: ${{ github.event.pull_request.number }}
COMMIT_ID: ${{ github.event.pull_request.head.sha }}
token: ${{ vars.ACTION_GITHUB_TOKEN }}
jobs:
check-approvers:
name: Check approval
runs-on:
group: APPROVAL
steps:
- name: Cleanup
run: |
rm -rf * .[^.]*
- name: Update paddle
run: |
wget -q --no-proxy https://xly-devops.bj.bcebos.com/PaddleTest/PaddleNLP/PaddleNLP-develop.tar.gz --no-check-certificate
tar zxf PaddleNLP-develop.tar.gz --strip-components=1 >/dev/null
rm -rf PaddleNLP-develop.tar.gz >/dev/null
git fetch origin pull/${PR_ID}/head
git checkout -b origin_pr FETCH_HEAD
git remote add upstream https://github.com/PaddlePaddle/PaddleNLP.git
git fetch upstream
git checkout -b test_pr upstream/${BRANCH}
git merge --no-edit origin_pr
git log --pretty=oneline -10
- name: Check bypass
id: check-bypass
uses: ./.github/actions/check-bypass
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
workflow-name: approval
- name: Display Required Approvers
if: steps.check-bypass.outputs.can-skip != 'true'
run: |
cd scripts/ci_approval
bash -x run_ci_approval.sh

View File

@@ -1,52 +0,0 @@
on:
workflow_call:
inputs:
workflow-name:
required: true
type: string
secrets:
github-token:
required: true
outputs:
can-skip:
description: "Whether the workflow can be skipped."
value: ${{ jobs.check-bypass.outputs.can-skip }}
jobs:
check-bypass:
name: Check bypass
runs-on:
group: APPROVAL
permissions:
contents: read
env:
CI_TEAM_MEMBERS: '["tianshuo78520a", "swgu98", "risemeup1" , "XieYunshen","luotao1","From00"]'
outputs:
can-skip: ${{ steps.check-bypass.outputs.can-skip }}
steps:
- name: Cleanup
run: |
rm -rf * .[^.]*
- id: check-bypass
name: Check Bypass
uses: PFCCLab/ci-bypass@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
non-pull-request-event-strategy: 'never-skipped'
type: 'composite'
composite-rule: |
{
"any": [
{
"type": "labeled",
"label": ["skip-ci: ${{ inputs.workflow-name }}", "skip-ci: all"],
"username": ${{ env.CI_TEAM_MEMBERS }}
},
{
"type": "commented",
"comment-pattern": [".*/skip-ci ${{ inputs.workflow-name }}.*", ".*/skip-ci all.*"],
"username": ${{ env.CI_TEAM_MEMBERS }}
}
]
}

View File

@@ -1,113 +0,0 @@
name: Codestyle Check
on: [push, pull_request]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
env:
PR_ID: ${{ github.event.pull_request.number }}
COMMIT_ID: ${{ github.event.pull_request.head.sha }}
BRANCH: ${{ github.event.pull_request.base.ref }}
TASK: PaddleNLP-CI-Lint-${{ github.event.pull_request.number }}
jobs:
check-bypass:
name: Check bypass
uses: ./.github/workflows/check-bypass.yml
with:
workflow-name: 'lint'
secrets:
github-token: ${{ secrets.GITHUB_TOKEN }}
Lint:
name: Lint
needs: check-bypass
if: ${{ needs.check-bypass.outputs.can-skip != 'true' }}
runs-on: [self-hosted, ernie-cpu]
steps:
- name: Run Container
env:
work_dir: ${{ github.workspace }}
python_version: "3.10"
run: |
container_name=${TASK}-$(date +%Y%m%d-%H%M%S)
echo "container_name=${container_name}" >> ${{ github.env }}
docker_image="iregistry.baidu-int.com/paddlecloud/base-images:paddlecloud-ubuntu20.04-gcc12.2-cuda12.3-cudnn9.0-nccl2.20.3.1-openmpi4.1.5-latest"
docker run -d -t --name ${container_name} --net=host -v /dev/shm:/dev/shm --shm-size=32G \
-v $work_dir/../../..:$work_dir/../../.. \
-v $work_dir:/workspace \
-v /home/.cache/pip:/home/.cache/pip \
-e BRANCH \
-e PR_ID \
-e COMMIT_ID \
-e work_dir \
-e no_proxy \
-e python_version \
-w /workspace ${docker_image}
- name: Download Code
env:
work_dir: ${{ github.workspace }}
run: |
docker exec -t ${container_name} /bin/bash -c '
rm -rf * .[^.]*
echo "Downloading PaddleNLP.tar"
wget -q --no-proxy https://paddle-qa.bj.bcebos.com/CodeSync/develop/PaddleNLP.tar --no-check-certificate
echo "Extracting PaddleNLP.tar"
tar xf PaddleNLP.tar && rm -rf PaddleNLP.tar
source $work_dir/../../../proxy
cd PaddleNLP
git config --global user.name "PaddleCI"
git config --global user.email "paddle_ci@example.com"
git pull
git submodule update --init --recursive --force
if [ -n "${PR_ID}" ]; then
git fetch origin pull/${PR_ID}/head
git checkout -b PR_${PR_ID} FETCH_HEAD
git remote add upstream https://github.com/PaddlePaddle/PaddleNLP.git
git fetch upstream ${BRANCH}
git merge ${BRANCH} --no-edit
git diff --numstat ${BRANCH} -- | awk "{print \$NF}"
else
echo "Not in a pull_request event. Skipping PR-specific operations."
fi
git log --pretty=oneline -10
if ! git show-ref --quiet refs/heads/develop; then \
echo "local develop branch is missing, creating local develop branch that tracks remote develop branch"
git fetch origin develop
git branch develop --track origin/develop
else
echo "local develop branch exist, skipping"
fi
'
- name: Setup Environment
run: |
docker exec -t $container_name /bin/bash -c '
unlink /usr/local/bin/python
ln -sf $(which python${python_version}) /usr/local/bin/python
set -e
python -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
python -m pip config set global.cache-dir "/home/.cache/pip"
source $work_dir/../../../proxy
python -m pip install --upgrade pip
cd /workspace/PaddleNLP && git config --global --add safe.directory $PWD
make install
'
- name: Test
run: |
docker exec -t $container_name /bin/bash -c '
set -e
source $work_dir/../../../proxy
cd /workspace/PaddleNLP
make lint
'
- name: Terminate And Delete the Container
if: always()
run: |
docker rm -f $container_name 2>/dev/null || true

View File

@@ -1,207 +0,0 @@
# Disabled LLM CI workflow - manually enabled when needed
name: LLM CI (DISABLED)
#on:
# pull_request:
# types: [opened, synchronize, reopened]
# branches: [develop]
# schedule:
# - cron: "2 0 * * *"
# workflow_call:
# inputs:
# run_downstream:
# required: true
# type: string
# image_name:
# required: true
# type: string
#concurrency:
# group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
# cancel-in-progress: true
#env:
# PR_ID: ${{ github.event.pull_request.number }}
COMMIT_ID: ${{ github.event.pull_request.head.sha }}
TASK: paddlenlp-CI-${{ github.event.pull_request.number }}-llm
ci_scripts: /workspace/PaddleNLP/scripts/regression
BRANCH: ${{ github.event.pull_request.base.ref }}
AGILE_COMPILE_BRANCH: ${{ github.event.pull_request.base.ref }}
CI_name: llm-ci
no_proxy: "localhost,bj.bcebos.com,su.bcebos.com,bcebos.com,apiin.im.baidu.com,gitee.com,aliyun.com,.baidu.com,.tuna.tsinghua.edu.cn"
HF_ENDPOINT: https://hf-mirror.com
STUDIO_GIT_HOST: http://git.prod.idc-to-cloud.aistudio.baidu-int.com
PPNLP_HOME: /ssd1/paddlenlp
HF_DATASETS_CACHE: /ssd1/paddlenlp/huggingface/datasets
TRANSFORMERS_CACHE: /ssd1/paddlenlp/huggingface
CCACHE_DIR: /home/data/gzcfs/.ccache/gpubox
RUN_DOWNSTREAM: ${{ inputs.run_downstream }}
#defaults:
# run:
# shell: bash
#jobs:
# llm-ci:
# name: llm-ci (DISABLED)
runs-on: [self-hosted, ernie-8gpu]
steps:
- name: Determine Image Name
env:
IMAGE_NAME: ${{ inputs.image_name }}
run: |
if [[ -n "${IMAGE_NAME}" ]]; then
echo "IMAGE_NAME=${IMAGE_NAME}" >> "$GITHUB_ENV"
else
echo "IMAGE_NAME=iregistry.baidu-int.com/paddlecloud/base-images:paddlecloud-ubuntu18.04-gcc8.2-cuda11.8-cudnn8.6-nccl2.15.5-paddlenlp-latest" >> "$GITHUB_ENV"
fi
- name: Run Container
env:
work_dir: ${{ github.workspace }}
CACHE_DIR: /home/data/cfs/.cache
FLAGS_dynamic_static_unified_comm: "True"
python_version: "3.10"
paddle_whl: https://paddle-qa.bj.bcebos.com/paddle-pipeline/Develop-GpuSome-LinuxCentos-Gcc82-Cuda118-Cudnn86-Trt85-Py310-CINN-Compile/latest/paddlepaddle_gpu-0.0.0-cp310-cp310-linux_x86_64.whl
run: |
container_name=${TASK}-$(date +%Y%m%d-%H%M%S)
echo "container_name=${container_name}" >> "$GITHUB_ENV"
if [[ "$RUN_DOWNSTREAM" == "false" ]]; then
echo "Not in a pull_request or test_build event. Skipping..."
else
docker run -d -t --name ${container_name} --net=host -v /dev/shm:/dev/shm --shm-size=32G \
-v $work_dir/../../..:$work_dir/../../.. \
-v $work_dir:/workspace \
-v /home/.cache/pip:/home/.cache/pip \
-v /ssd1/paddlenlp:/ssd1/paddlenlp \
-v /home/data/gzcfs/.ccache/gpubox:/home/data/gzcfs/.ccache/gpubox \
-e BRANCH \
-e AGILE_COMPILE_BRANCH \
-e PR_ID \
-e COMMIT_ID \
-e work_dir \
-e ci_scripts \
-e no_proxy \
-e CI_name \
-e paddle_whl \
-e HF_ENDPOINT \
-e STUDIO_GIT_HOST \
-e PPNLP_HOME \
-e HF_DATASETS_CACHE \
-e TRANSFORMERS_CACHE \
-e CACHE_DIR \
-e FLAGS_dynamic_static_unified_comm \
-e python_version \
-w /workspace --runtime=nvidia $IMAGE_NAME
fi
- name: Download Code
env:
work_dir: ${{ github.workspace }}
run: |
if [[ "$RUN_DOWNSTREAM" == "false" ]]; then
echo "Not in a pull_request or test_build event. Skipping.."
else
docker exec -t $container_name /bin/bash -c '
rm -rf * .[^.]*
echo "Downloading PaddleNLP.tar.gz"
wget -q --no-proxy https://paddle-qa.bj.bcebos.com/CodeSync/develop/PaddleNLP.tar --no-check-certificate
echo "Extracting PaddleNLP.tar.gz"
tar xf PaddleNLP.tar && rm -rf PaddleNLP.tar
source $work_dir/../../../proxy
cd PaddleNLP
git config --global user.name "PaddleCI"
git config --global user.email "paddle_ci@example.com"
git pull
git submodule update --init --recursive --force
if [ -n "${PR_ID}" ]; then
git fetch origin pull/${PR_ID}/head
git checkout -b PR_${PR_ID} FETCH_HEAD
git remote add upstream https://github.com/PaddlePaddle/PaddleNLP.git
git fetch upstream ${BRANCH}
git merge ${BRANCH} --no-edit
git diff --numstat ${BRANCH} -- | awk "{print \$NF}"
else
echo "Not in a pull_request event. Skipping PR-specific operations."
fi
git log --pretty=oneline -10
'
fi
- name: Skip For Bug
run: |
if [[ "$RUN_DOWNSTREAM" == "false" ]]; then
echo "Not in a pull_request or test_build event. Skipping..."
else
docker exec -t $container_name /bin/bash -c '
cd /workspace/PaddleNLP
git revert f2477c07272d04244cd3287d1f21c70482a4a85f --no-edit # 套件PR#10413引入bug-待修复
git revert d74c950e15a35b7a100d1688c89318195cc83bca --no-edit # 框架升级后配合修改,当前因框架bug固定commit适配
'
fi
- name: Test
run: |
if [[ "$RUN_DOWNSTREAM" == "false" ]]; then
echo "Not in a pull_request or test_build event. Skipping..."
else
docker exec -t $container_name /bin/bash -c '
ldconfig
unlink /usr/bin/python3
ln -sf $(which python${python_version}) /usr/bin/python3
pip config set global.cache-dir "/home/.cache/pip"
set -e
source $work_dir/../../../proxy
cd /workspace/PaddleNLP && git config --global --add safe.directory $PWD
echo "Paddle PR#73283 PR#74484 import bug, set paddle_commit=8ae742 to skip "
export paddle_whl=https://paddle-qa.bj.bcebos.com/paddle-pipeline/Develop-GpuSome-LinuxCentos-Gcc82-Cuda118-Cudnn86-Trt85-Py310-CINN-Compile/8ae7423e99b2ea96e410968a0ebb3f1795e37205/paddlepaddle_gpu-0.0.0-cp310-cp310-linux_x86_64.whl
timeout 2h bash scripts/regression/run_ci.sh python${python_version} ${paddle_whl}
'
fi
- name: Upload Allure-reports & Logs
if: always()
env:
home_path: ${{ github.workspace }}/../../..
bos_file: ${{ github.workspace }}/../../../bos/BosClient.py
allure_file: ${{ github.workspace }}/../../../allure-2.19.0/bin/allure
run: |
if [[ "$RUN_DOWNSTREAM" == "false" ]]; then
echo "Not in a pull_request or test_build event. Skipping..."
else
docker exec -t $container_name /bin/bash -c '
unset http_proxy && unset https_proxy
if [ ! -f "${{ env.bos_file }}" ]; then
wget -q --no-proxy -O ${{ env.home_path }}/bos_new.tar.gz https://xly-devops.bj.bcebos.com/home/bos_new.tar.gz --no-check-certificate
mkdir ${{ env.home_path }}/bos
tar xf ${{ env.home_path }}/bos_new.tar.gz -C ${{ env.home_path }}/bos
fi
if [ ! -f "${{ env.allure_file }}" ]; then
wget -q --no-proxy -O ${{ env.home_path }}/allure-2.19.0.zip https://xly-devops.bj.bcebos.com/tools/allure-2.19.0.zip --no-check-certificate
unzip -q ${{ env.home_path }}/allure-2.19.0.zip -d ${{ env.home_path }}/
fi
if [[ "${{ env.RUN_DOWNSTREAM }}" == "" && -n "${PR_ID}" ]]; then
bos_prefix="${PR_ID}/${COMMIT_ID}"
elif [[ "${{ env.RUN_DOWNSTREAM }}" == "true" && -n "${PR_ID}" ]]; then
bos_prefix="${PR_ID}/${COMMIT_ID}/test_build"
else
bos_prefix="schedule/$(date +%Y%m%d)"
fi
cd /workspace/PaddleNLP/model_logs
for FILE in /workspace/PaddleNLP/model_logs/*; do
file=$(basename "$FILE")
python ${{ env.bos_file }} $file paddle-github-action/PR/PaddleNLP/llm/${bos_prefix}/logs
echo "$file: https://paddle-github-action.bj.bcebos.com/PR/PaddleNLP/llm/${bos_prefix}/logs/$file"
done
cd /workspace/PaddleNLP/
${{ env.allure_file }} generate result -o report
tar -czf products.tar.gz report model_logs
python ${{ env.bos_file }} products.tar.gz paddle-github-action/PR/PaddleNLP/llm/${bos_prefix}/logs
echo "products: https://paddle-github-action.bj.bcebos.com/PR/PaddleNLP/llm/${bos_prefix}/logs/products.tar.gz"
'
fi
- name: Terminate And Delete the Container
if: always()
run: |
docker rm -f ${{ env.container_name }} 2>/dev/null || true

View File

@@ -1,30 +0,0 @@
name: Pipelines-Test
on:
push:
paths:
- 'slm/pipelines/*'
pull_request:
paths:
- 'slm/pipelines/*'
jobs:
Pipelines-Test:
name: Pipelines-Test
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
cache: 'pip' # caching pip dependencies
- name: Install dependencies
working-directory: ./slm/pipelines
run: |
python -m pip install --upgrade pip
make install
pip install -r tests/requirements.txt
- name: run the command
working-directory: ./slm/pipelines
run: make test

View File

@@ -1,58 +0,0 @@
name: Release
on:
workflow_dispatch:
inputs:
package:
description: 'Package to be deployed'
required: true
default: 'paddlenlp'
type: choice
options:
- paddlenlp
- paddle-pipelines
- ppdiffusers
jobs:
Release:
name: Release
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v1
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Should Deploy
id: should-deploy
env:
PACKAGE: ${{ inputs.package }}
run: python scripts/should_deploy.py --name $PACKAGE >> $GITHUB_OUTPUT
- name: Check Branch
id: check-branch
run: |
if [[ ${{ github.ref }} =~ ^refs/tags/v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "match=true" >> $GITHUB_OUTPUT
fi # See: https://stackoverflow.com/a/58869470/1123955
- name: Is A Publish Branch
if: steps.check-branch.outputs.match == 'true' && steps.should-deploy.outputs.should_deploy == 'true'
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.paddlenlp }}
PACKAGE: ${{ inputs.package }}
PADDLENLP_STABLE_VERSION: "true"
run: |
make deploy-$PACKAGE
- name: Should Not Deploy To Pypi Server
if: steps.should-deploy.outputs.should_deploy != 'true'
run: echo 'should not deploy pypi server'
- name: Is Not A Publish Branch
if: steps.check-branch.outputs.match != 'true'
run: echo 'Not A Publish Branch'

View File

@@ -1,73 +0,0 @@
name: Re-run
on:
issue_comment:
types: [created]
jobs:
re-run:
if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, '/re-run') && github.event.comment.user.login == github.event.issue.user.login }}
runs-on: [self-hosted, ernie-cpu]
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Rerun all failed jobs
if: ${{ contains(github.event.comment.body, 'all-failed') }}
uses: ./.github/actions/rerun-workflow
with:
PR_ID: ${{ github.event.issue.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
JOB_NAME: 'all-failed'
- name: Rerun LLM
if: ${{ contains(github.event.comment.body, 'LLM') }}
uses: ./.github/actions/rerun-workflow
with:
PR_ID: ${{ github.event.issue.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
JOB_NAME: 'LLM CI / llm-ci'
- name: Unittest GPU
if: ${{ contains(github.event.comment.body, 'Unittest GPU') }}
uses: ./.github/actions/rerun-workflow
with:
PR_ID: ${{ github.event.issue.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
JOB_NAME: 'Unittest GPU CI / unittest-gpu-ci'
- name: Rerun Unittest CPU
if: ${{ contains(github.event.comment.body, 'Unittest CPU') }}
uses: ./.github/actions/rerun-workflow
with:
PR_ID: ${{ github.event.issue.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
JOB_NAME: 'Unittest CPU CI / unit-cpu-ci'
- name: Rerun Distribute (V100)
if: ${{ contains(github.event.comment.body, 'Distribute (V100)') }}
uses: ./.github/actions/rerun-workflow
with:
PR_ID: ${{ github.event.issue.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
JOB_NAME: 'Distribute CI (V100)/ distribute-v100-ci'
- name: Rerun Codestyle Check
if: ${{ contains(github.event.comment.body, 'Codestyle Check') }}
uses: ./.github/actions/rerun-workflow
with:
PR_ID: ${{ github.event.issue.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
JOB_NAME: 'Codestyle Check / Lint'

View File

@@ -1,35 +0,0 @@
name: Stale
on:
# Allow manual run via GitHub web or CLI
workflow_dispatch:
schedule:
# Run daily at midnight UTC
- cron: 0 0 * * *
permissions:
issues: write
pull-requests: write
jobs:
stale:
runs-on: ubuntu-24.04
permissions:
issues: write
pull-requests: write
steps:
- uses: actions/stale@v6.0.1
with:
days-before-issue-stale: 60
days-before-issue-close: 14
stale-issue-label: "stale"
stale-issue-message: "This issue is stale because it has been open for 60 days with no activity. 当前issue 60天内无活动,被标记为stale。"
close-issue-message: "This issue was closed because it has been inactive for 14 days since being marked as stale. 当前issue 被标记为stale已有14天,即将关闭。"
exempt-issue-labels: 'triage,keep'
days-before-pr-stale: 60
days-before-pr-close: -1
stale-pr-label: "stale"
stale-pr-message: "This Pull Request is stale because it has been open for 60 days with no activity. 当前Pull Request 60天内无活动,被标记为stale。"
operations-per-run: 400

View File

@@ -1,120 +0,0 @@
# Disabled unittest CPU workflow - manually enabled when needed
name: Unittest CPU CI (DISABLED)
#on: [pull_request]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
env:
PR_ID: ${{ github.event.pull_request.number }}
COMMIT_ID: ${{ github.event.pull_request.head.sha }}
BRANCH: ${{ github.event.pull_request.base.ref }}
TASK: PaddleNLP-CI-${{ github.event.pull_request.number }}-unittest-cpu
HF_ENDPOINT: https://hf-mirror.com
STUDIO_GIT_HOST: http://git.prod.idc-to-cloud.aistudio.baidu-int.com
PPNLP_HOME: /home/disk1/cache
HF_DATASETS_CACHE: /home/disk1/cache/huggingface/datasets
TRANSFORMERS_CACHE: /home/disk1/cache/huggingface
#jobs:
# unittest-cpu-ci:
# name: unittest-cpu-ci (DISABLED)
runs-on: [self-hosted, ernie-cpu]
permissions:
pull-requests: write
contents: read
id-token: write
steps:
- name: Run Container
env:
work_dir: ${{ github.workspace }}
python_version: "3.10"
run: |
container_name=${TASK}-$(date +%Y%m%d-%H%M%S)
echo "container_name=${container_name}" >> ${{ github.env }}
docker_image="iregistry.baidu-int.com/paddlecloud/base-images:paddlecloud-ubuntu20.04-gcc12.2-cuda12.3-cudnn9.0-nccl2.20.3.1-openmpi4.1.5-latest"
docker run -d -t --name ${container_name} --net=host -v /dev/shm:/dev/shm --shm-size=32G \
-v $work_dir/../../..:$work_dir/../../.. \
-v $work_dir:/workspace \
-v /home/.cache/pip:/home/.cache/pip \
-v /home/disk1/cache:/home/disk1/cache \
-e BRANCH \
-e PR_ID \
-e COMMIT_ID \
-e work_dir \
-e no_proxy \
-e python_version \
-e HF_ENDPOINT \
-e STUDIO_GIT_HOST \
-e PPNLP_HOME \
-e HF_DATASETS_CACHE \
-e TRANSFORMERS_CACHE \
-w /workspace ${docker_image}
- name: Download Code
env:
work_dir: ${{ github.workspace }}
run: |
docker exec -t ${container_name} /bin/bash -c '
rm -rf * .[^.]*
echo "Downloading PaddleNLP.tar"
wget -q --no-proxy https://paddle-qa.bj.bcebos.com/CodeSync/develop/PaddleNLP.tar --no-check-certificate
echo "Extracting PaddleNLP.tar"
tar xf PaddleNLP.tar && rm -rf PaddleNLP.tar
source $work_dir/../../../proxy
cd PaddleNLP
git config --global user.name "PaddleCI"
git config --global user.email "paddle_ci@example.com"
git pull
git submodule update --init --recursive --force
if [ -n "${PR_ID}" ]; then
git fetch origin pull/${PR_ID}/head
git checkout -b PR_${PR_ID} FETCH_HEAD
git remote add upstream https://github.com/PaddlePaddle/PaddleNLP.git
git fetch upstream ${BRANCH}
git merge ${BRANCH} --no-edit
git diff --numstat ${BRANCH} -- | awk "{print \$NF}"
else
echo "Not in a pull_request event. Skipping PR-specific operations."
fi
git log --pretty=oneline -10
'
- name: Setup Environment
run: |
docker exec -t $container_name /bin/bash -c '
unlink /usr/bin/python3
ln -sf $(which python${python_version}) /usr/bin/python3
set -e
python -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
python -m pip config set global.cache-dir "/home/.cache/pip"
source $work_dir/../../../proxy
python -m pip install --upgrade pip
cd /workspace/PaddleNLP && git config --global --add safe.directory $PWD
pip install -r tests/requirements.txt
make install
'
- name: Test
run: |
docker exec -t $container_name /bin/bash -c '
source $work_dir/../../../proxy
cd /workspace/PaddleNLP
set -e
make test
'
# - name: Upload Coverage To Codecov
# uses: codecov/codecov-action@v3
# env:
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
# with:
# token: ${{ secrets.CODECOV_TOKEN }}
# files: ./PaddleNLP/coverage.xml
- name: Terminate And Delete the Container
if: always()
run: |
docker rm -f $container_name 2>/dev/null || true

View File

@@ -1,204 +0,0 @@
# Disabled unittest GPU workflow - manually enabled when needed
name: Unittest GPU CI (DISABLED)
#on:
# pull_request:
# types: [opened, synchronize, reopened]
# branches: [develop]
# schedule:
# - cron: "3 0 * * *"
# workflow_call:
# inputs:
# run_downstream:
# required: true
# type: string
# image_name:
# required: true
# type: string
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
env:
PR_ID: ${{ github.event.pull_request.number }}
COMMIT_ID: ${{ github.event.pull_request.head.sha }}
TASK: paddlenlp-CI-${{ github.event.pull_request.number }}-unit-gpu
ci_scripts: /workspace/PaddleNLP/scripts/unit_test
BRANCH: ${{ github.event.pull_request.base.ref }}
AGILE_COMPILE_BRANCH: ${{ github.event.pull_request.base.ref }}
CI_name: unittest-gpu-ci
no_proxy: "localhost,bj.bcebos.com,su.bcebos.com,bcebos.com,apiin.im.baidu.com,gitee.com,aliyun.com,.baidu.com,.tuna.tsinghua.edu.cn"
HF_ENDPOINT: https://hf-mirror.com
STUDIO_GIT_HOST: http://git.prod.idc-to-cloud.aistudio.baidu-int.com
PPNLP_HOME: /ssd1/paddlenlp
HF_DATASETS_CACHE: /ssd1/paddlenlp/huggingface/datasets
TRANSFORMERS_CACHE: /ssd1/paddlenlp/huggingface
CCACHE_DIR: /home/data/gzcfs/.ccache/gpubox
RUN_DOWNSTREAM: ${{ inputs.run_downstream }}
defaults:
run:
shell: bash
#jobs:
# unittest-gpu-ci:
# name: unittest-gpu-ci (DISABLED)
runs-on: [self-hosted, ernie-8gpu]
steps:
- name: Determine Image Name
env:
IMAGE_NAME: ${{ inputs.image_name }}
run: |
if [[ -n "${IMAGE_NAME}" ]]; then
echo "IMAGE_NAME=${IMAGE_NAME}" >> "$GITHUB_ENV"
else
echo "IMAGE_NAME=iregistry.baidu-int.com/paddlecloud/base-images:paddlecloud-ubuntu18.04-gcc8.2-cuda11.8-cudnn8.6-nccl2.15.5-paddlenlp-latest" >> "$GITHUB_ENV"
fi
- name: Run Container
env:
work_dir: ${{ github.workspace }}
CACHE_DIR: /home/data/cfs/.cache
FLAGS_dynamic_static_unified_comm: "True"
python_version: "3.10"
paddle_whl: https://paddle-qa.bj.bcebos.com/paddle-pipeline/Develop-GpuSome-LinuxCentos-Gcc82-Cuda118-Cudnn86-Trt85-Py310-CINN-Compile/latest/paddlepaddle_gpu-0.0.0-cp310-cp310-linux_x86_64.whl
run: |
container_name=${TASK}-$(date +%Y%m%d-%H%M%S)
echo "container_name=${container_name}" >> "$GITHUB_ENV"
if [[ "$RUN_DOWNSTREAM" == "false" ]]; then
echo "Not in a pull_request or test_build event. Skipping..."
else
docker run -d -t --name ${container_name} --net=host -v /dev/shm:/dev/shm --shm-size=32G \
-v $work_dir/../../..:$work_dir/../../.. \
-v $work_dir:/workspace \
-v /home/.cache/pip:/home/.cache/pip \
-v /ssd1/paddlenlp:/ssd1/paddlenlp \
-v /home/data/gzcfs/.ccache/gpubox:/home/data/gzcfs/.ccache/gpubox \
-e BRANCH \
-e AGILE_COMPILE_BRANCH \
-e PR_ID \
-e COMMIT_ID \
-e work_dir \
-e ci_scripts \
-e no_proxy \
-e CI_name \
-e paddle_whl \
-e HF_ENDPOINT \
-e STUDIO_GIT_HOST \
-e PPNLP_HOME \
-e HF_DATASETS_CACHE \
-e TRANSFORMERS_CACHE \
-e CACHE_DIR \
-e FLAGS_dynamic_static_unified_comm \
-e python_version \
-w /workspace --runtime=nvidia $IMAGE_NAME
fi
- name: Download Code
env:
work_dir: ${{ github.workspace }}
run: |
if [[ "$RUN_DOWNSTREAM" == "false" ]]; then
echo "Not in a pull_request or test_build event. Skipping.."
else
docker exec -t $container_name /bin/bash -c '
rm -rf * .[^.]*
echo "Downloading PaddleNLP.tar.gz"
wget -q --no-proxy https://paddle-qa.bj.bcebos.com/CodeSync/develop/PaddleNLP.tar --no-check-certificate
echo "Extracting PaddleNLP.tar.gz"
tar xf PaddleNLP.tar && rm -rf PaddleNLP.tar
source $work_dir/../../../proxy
cd PaddleNLP
git config --global user.name "PaddleCI"
git config --global user.email "paddle_ci@example.com"
git pull
git submodule update --init --recursive --force
if [ -n "${PR_ID}" ]; then
git fetch origin pull/${PR_ID}/head
git checkout -b PR_${PR_ID} FETCH_HEAD
git remote add upstream https://github.com/PaddlePaddle/PaddleNLP.git
git fetch upstream ${BRANCH}
git merge ${BRANCH} --no-edit
git diff --numstat ${BRANCH} -- | awk "{print \$NF}"
else
echo "Not in a pull_request event. Skipping PR-specific operations."
fi
git log --pretty=oneline -10
'
fi
- name: Skip For Bug
run: |
if [[ "$RUN_DOWNSTREAM" == "false" ]]; then
echo "Not in a pull_request or test_build event. Skipping..."
else
docker exec -t $container_name /bin/bash -c '
cd /workspace/PaddleNLP
echo "no skip for bug"
'
fi
- name: Test
run: |
if [[ "$RUN_DOWNSTREAM" == "false" ]]; then
echo "Not in a pull_request or test_build event. Skipping..."
else
docker exec -t $container_name /bin/bash -c '
ldconfig
unlink /usr/bin/python3
ln -sf $(which python${python_version}) /usr/bin/python3
pip config set global.cache-dir "/home/.cache/pip"
source $work_dir/../../../proxy
set -e
cd /workspace/PaddleNLP && git config --global --add safe.directory $PWD
timeout 50m bash scripts/unit_test/ci_unit.sh ${paddle_whl}
'
fi
- name: Upload Allure-reports & Logs
if: always()
env:
home_path: ${{ github.workspace }}/../../..
bos_file: ${{ github.workspace }}/../../../bos/BosClient.py
allure_file: ${{ github.workspace }}/../../../allure-2.19.0/bin/allure
run: |
if [[ "$RUN_DOWNSTREAM" == "false" ]]; then
echo "Not in a pull_request or test_build event. Skipping..."
else
docker exec -t $container_name /bin/bash -c '
unset http_proxy && unset https_proxy
if [ ! -f "${{ env.bos_file }}" ]; then
wget -q --no-proxy -O ${{ env.home_path }}/bos_new.tar.gz https://xly-devops.bj.bcebos.com/home/bos_new.tar.gz --no-check-certificate
mkdir ${{ env.home_path }}/bos
tar xf ${{ env.home_path }}/bos_new.tar.gz -C ${{ env.home_path }}/bos
fi
if [ ! -f "${{ env.allure_file }}" ]; then
wget -q --no-proxy -O ${{ env.home_path }}/allure-2.19.0.zip https://xly-devops.bj.bcebos.com/tools/allure-2.19.0.zip --no-check-certificate
unzip -q ${{ env.home_path }}/allure-2.19.0.zip -d ${{ env.home_path }}/
fi
if [[ "${{ env.RUN_DOWNSTREAM }}" == "" && -n "${PR_ID}" ]]; then
bos_prefix="${PR_ID}/${COMMIT_ID}"
elif [[ "${{ env.RUN_DOWNSTREAM }}" == "true" && -n "${PR_ID}" ]]; then
bos_prefix="${PR_ID}/${COMMIT_ID}/test_build"
else
bos_prefix="schedule/$(date +%Y%m%d)"
fi
cd /workspace/PaddleNLP/unittest_logs
for FILE in /workspace/PaddleNLP/unittest_logs/*; do
file=$(basename "$FILE")
python ${{ env.bos_file }} $file paddle-github-action/PR/PaddleNLP/unittest-gpu/${bos_prefix}/logs
echo "$file: https://paddle-github-action.bj.bcebos.com/PR/PaddleNLP/unittest-gpu/${bos_prefix}/logs/$file"
done
cd /workspace/PaddleNLP/
${{ env.allure_file }} generate result -o report
tar -czf products.tar.gz report unittest_logs
python ${{ env.bos_file }} products.tar.gz paddle-github-action/PR/PaddleNLP/unittest-gpu/${bos_prefix}/logs
echo "report: https://paddle-github-action.bj.bcebos.com/PR/PaddleNLP/unittest-gpu/${bos_prefix}/logs/products.tar.gz"
'
fi
- name: Terminate And Delete the Container
if: always()
run: |
docker rm -f $container_name 2>/dev/null || true

19
.gitignore vendored
View File

@@ -53,7 +53,6 @@ coverage.xml
# Translations
*.mo
*.pot
*.doctree
# Django stuff:
*.log
@@ -123,21 +122,3 @@ FETCH_HEAD
# vscode
.vscode
./ppdiffusers/ppdiffusers/version.py
# third party
csrc/third_party/
dataset/
output/
# gen codes
autogen/
# cutlass kernel
!csrc/gpu/cutlass_kernels/gemm/collective/builders
#fp8
ops/csrc/fp8/deep_gemm/include/cutlass
ops/csrc/fp8/deep_gemm/include/cute
.ccls-cache

6
.gitmodules vendored
View File

@@ -1,6 +0,0 @@
[submodule "csrc/third_party/cutlass"]
path = csrc/third_party/cutlass
url = https://github.com/NVIDIA/cutlass.git
[submodule "csrc/third_party/nlohmann_json"]
path = csrc/third_party/nlohmann_json
url = https://github.com/nlohmann/json.git

View File

@@ -1,22 +1,11 @@
exclude: 'slm/model_zoo/gpt-3;csrc/third_party'
repos:
# For Python files
- repo: https://github.com/psf/black.git
rev: 22.8.0
- repo: https://github.com/PaddlePaddle/mirrors-yapf.git
sha: 0d79c0c469bab64f7229c9aca2b1186ef47f0e37
hooks:
- id: black
files: \.(py|pyi)$
additional_dependencies: [toml]
- repo: https://github.com/PyCQA/isort
rev: 5.11.5
hooks:
- id: isort
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
- id: yapf
files: \.py$
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
sha: a11d9314b22d8f8c7556443875b731ef05965464
hooks:
- id: check-merge-conflict
- id: check-symlinks
@@ -27,7 +16,7 @@ repos:
- id: trailing-whitespace
files: \.md$
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.1.14
sha: v1.0.1
hooks:
- id: forbid-crlf
files: \.md$
@@ -37,37 +26,3 @@ repos:
files: \.md$
- id: remove-tabs
files: \.md$
- repo: local
hooks:
- id: copyright_checker
name: copyright_checker
entry: python .copyright.hook
language: system
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|xpu|kps|py|sh)$
# For Markdown files
- repo: local
hooks:
- id: add-spaces-between-chinese-and-english
name: Add spaces between Chinese and English characters
entry: python scripts/codestyle/check_spaces.py
language: python
files: \.(md|markdown)$
pass_filenames: true
# For dead links
- repo: local
hooks:
- id: check-dead-links
name: Check dead links
entry: python scripts/codestyle/check_dead_links.py
language: python
files: \.(md|markdown|rst)$
pass_filenames: true
- repo: local
hooks:
- id: clang-format
name: clang-format
description: Format files with ClangFormat.
entry: bash ./tools/codestyle/clang_format.sh -i
language: system
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|xpu|kps)$

View File

@@ -4,10 +4,6 @@
# Required
version: 2
build:
os: "ubuntu-20.04"
tools:
python: "3.10"
submodules:
include: all
@@ -15,7 +11,7 @@ submodules:
# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: docs/zh/conf.py
configuration: docs/conf.py
# Optionally build your docs in additional formats such as PDF
#formats:
@@ -23,5 +19,10 @@ sphinx:
# Optionally set the version of Python and requirements required to build your docs
python:
version: 3.7
install:
- requirements: docs/requirements.txt
- requirements: requirements.txt
- method: setuptools
path: .
system_packages: true

3
.style.yapf Normal file
View File

@@ -0,0 +1,3 @@
[style]
based_on_style = pep8
column_limit = 80

View File

@@ -1,158 +0,0 @@
**简体中文**🀄 | [English🌎](.github/CONTRIBUTING_en.md)
# Contributing to PaddleNLP
我们非常欢迎并希望您对`PaddleNLP`做出开源贡献。在您开始提交您的贡献之前,请先行签署[PaddlePaddle 贡献者许可协议](https://cla-assistant.io/PaddlePaddle/PaddleNLP)。
本文接下来将介绍我们的开发与贡献流程:
## 贡献方式
我们欢迎不同的向`PaddleNLP`做出贡献的方式,例如:
- 修复已知的 Issue
- 提交新的 Issue,例如提出功能需求或者 bug 报告
- 实现新的模型结构
如果您不知道从哪里开始,请查看 Issues 板块中的`Good First Issue`标签。它为您提供一个对初学者友好的已知 Issue 列表,可以降低贡献的门槛,帮助您开始为开源做出贡献。您只需在您想处理的 Issue 中告知我们您想负责此 Issue 即可。
## 开发流程
PaddleNLP 使用 [Git 分支模型](http://nvie.com/posts/a-successful-git-branching-model/)。对于常见的开源贡献,我们有以下的贡献流程:
### 1. Fork
因为 PaddleNLP 的开发社区一直在发展,如果每位贡献者都直接向官方 Repo 提交 commit 将会难以管理。因此,请从您的分支中提交 Pull Requests。建议您通过 GitHub 的[“Fork”按钮](https://help.github.com/articles/fork-a-repo/)来创建您的 Fork 分支。
### 2. Clone
请运行一下命令将您的分支 clone 到本地
```bash
git clone https://github.com/<your-github-account>/PaddleNLP
cd PaddleNLP
```
### 3. 创建本地开发分支
对于添加新功能或修复错误等日常工作,请在开发前创建您的本地开发分支:
```bash
git checkout -b my-cool-feature
```
### 4. 配置开发环境
在开始编码之前,您需要设置开发环境。我们强烈建议您在虚拟环境中进行所有开发,例如[venv](https://docs.python.org/3/library/venv.html)或[conda](https://docs.conda.io/en/latest/)。
请您设置并激活虚拟环境后,运行以下命令:
```bash
make install
```
这将设置 `PaddleNLP` 的所有依赖以及 [`pre-commit`](http://pre-commit.com/) 工具。
如果您需要开发 `examples` 或 `applications` 模块并加载 `PaddleNLP`,请确保以可编辑模式(`-e`)安装 `PaddleNLP`。
如果在虚拟环境中已经安装 `PaddleNLP` ,请使用 `pip uninstall paddlenlp` 将其删除,然后以可编辑模式重新安装它
`pip install -e .`
### 5. 开发
当您开发时,请确保您新增的代码会被单元测试所覆盖。我们所有的单元测试都可以在 `tests` 目录下找到。
您可以修改现有单元测试以覆盖新功能,也可以从头开始创建新测试。
当您完成代码时,您应该确保相关的单元测试可以通过。您可以像这样运行受更改影响的测试:
```bash
pytest tests/<test_to_run>.py
```
### 6. Commit
我们使用 [`pre-commit`](http://pre-commit.com/)工具(包括[black](https://black.readthedocs.io/en/stable/)、[isort](https:/ /pycqa.github.io/isort/) 和
[flake8](https://flake8.pycqa.org/en/latest/))来检查每次提交中的代码和文档的风格。当你运行 `git commit` 时,你会看到
类似于以下内容:
```text
➜ (my-virtual-env) git commit -m "committing my cool feature"
black....................................................................Passed
isort....................................................................Passed
flake8...................................................................Passed
check for merge conflicts................................................Passed
check for broken symlinks............................(no files to check)Skipped
detect private key.......................................................Passed
fix end of files.....................................(no files to check)Skipped
trim trailing whitespace.............................(no files to check)Skipped
CRLF end-lines checker...............................(no files to check)Skipped
CRLF end-lines remover...............................(no files to check)Skipped
No-tabs checker......................................(no files to check)Skipped
Tabs remover.........................................(no files to check)Skipped
copyright_checker........................................................Passed
```
但大多数时候事情并没有那么顺利。当您的代码或文档不符合标准时,`pre-commit` 检查将失败。
```text
➜ (my-virtual-env) git commit -m "committing my cool feature"
black....................................................................Passed
isort....................................................................Failed
- hook id: isort
- files were modified by this hook
Fixing examples/information_extraction/waybill_ie/run_ernie_crf.py
flake8...................................................................Passed
check for merge conflicts................................................Passed
check for broken symlinks............................(no files to check)Skipped
detect private key.......................................................Passed
fix end of files.....................................(no files to check)Skipped
trim trailing whitespace.............................(no files to check)Skipped
CRLF end-lines checker...............................(no files to check)Skipped
CRLF end-lines remover...............................(no files to check)Skipped
No-tabs checker......................................(no files to check)Skipped
Tabs remover.........................................(no files to check)Skipped
copyright_checker........................................................Passed
```
我们的工具将自动修复大部分样式错误,但是有些错误需要手动解决。幸运的是,错误信息一般通俗易懂,很容易修复。
解决错误后,您可以再次运行 `git add <files>` 和 `git commit`,这将再次触发 pre-commit 。
一旦 pre-commit 检查通过,您就可以推送代码了。
[Google](https://google.com/) 或 [StackOverflow](https://stackoverflow.com/) 是帮助您了解代码风格错误的好工具。
如果您仍然无法弄清楚,请不要担心。您可以使用 `git commit -m "style error" --no-verify` 提交,我们很乐意在您创建 Pull Request 后帮助您。
### 7. git pull 与代码冲突
有经验的 Git 用户经常从官方 Repo 中 git pull。因为这样子他们会及早注意到与其他人的代码冲突,并且让代码冲突更容易解决
```bash
git remote add upstream https://github.com/PaddlePaddle/PaddleNLP
git pull upstream develop
```
### 8. git push 与提交 Pull Request
您可以将您的本地开发分支中的工作 push 到您的 fork 的分支中:
```bash
git push origin my-cool-stuff
```
git push 之后,您可以提交 Pull Request,请求[官方 repo](https://github.com/PaddlePaddle/PaddleNLP) 采纳您的开发工作。请您依照[这些步骤](https://help.github.com/articles/creating-a-pull-request/)创建 Pull Request。
### 9. 删除已经合入的本地和远程分支
为了保持您本地的工作区和 fork 分支的干净整洁,建议您在 Pull Request 合入之后删除本地的残余分支:
```bash
git push origin my-cool-stuff
git checkout develop
git pull upstream develop
git branch -d my-cool-stuff
```
## 代码 Review
- 在您的 Pull Request 能够顺利通过本地测试以及 CI 的情况下,您可以在 Pull Request 中 @ 相关的 Reviewer,提醒他们尽快对您的 Pull Request 进行 Review。
- 请处理 Reviewer 的每一条评论。如果您已按照评论修改,请回复“完成”;否则,可以在评论下展开讨论。
- 如果您不希望您的 Reviewer 被电子邮件通知淹没,您可以[批量回复](https://help.github.com/articles/reviewing-proposed-changes-in-a-pull-request/)。

View File

@@ -1,79 +0,0 @@
# Makefile for PaddleNLP
#
# GitHb: https://github.com/PaddlePaddle/PaddleNLP
# Author: Paddle Team https://github.com/PaddlePaddle
#
.PHONY: all
all : lint test
check_dirs := applications examples model_zoo paddlenlp pipelines ppdiffusers scripts tests
# # # # # # # # # # # # # # # Format Block # # # # # # # # # # # # # # #
format:
pre-commit run isort
pre-commit run black
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # Lint Block # # # # # # # # # # # # # # #
.PHONY: lint
lint:
$(eval modified_py_files := $(shell python scripts/get_modified_files.py $(check_dirs)))
@if test -n "$(modified_py_files)"; then \
echo ${modified_py_files}; \
pre-commit run --files ${modified_py_files}; \
else \
echo "No library .py files were modified"; \
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # Test Block # # # # # # # # # # # # # # #
.PHONY: test
test: unit-test
unit-test:
PYTHONPATH=$(shell pwd) pytest -v \
-n auto \
--retries 1 --retry-delay 1 \
--durations 20 \
--cov paddlenlp \
--cov-report xml:coverage.xml
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
.PHONY: install
install:
pip install --pre paddlepaddle -i https://www.paddlepaddle.org.cn/packages/nightly/cpu/
pip install -r requirements-dev.txt
pip install -r requirements.txt
pip install -r paddlenlp/experimental/autonlp/requirements.txt
pre-commit install
.PHONY: deploy-ppdiffusers
deploy-ppdiffusers:
cd ppdiffusers && make install && make
.PHONY: deploy-paddle-pipelines
deploy-paddle-pipelines:
cd pipelines && make install && make
.PHONY: deploy-paddlenlp
deploy-paddlenlp:
# install related package
make install
# build
python3 setup.py sdist bdist_wheel
# upload
twine upload --skip-existing dist/*
.PHONY: regression-all
release:
bash ./scripts/regression/run_release.sh 0 0,1 all
.PHONY: regression-key
key:
bash ./scripts/regression/run_release.sh 0 0,1 p0

527
README.md
View File

@@ -1,298 +1,327 @@
**简体中文**🀄 | [English🌎](./README_en.md)
简体中文 | [English](./README_en.md)
<p align="center">
<img src="https://user-images.githubusercontent.com/1371212/175816733-8ec25eb0-9af3-4380-9218-27c154518258.png" align="middle" width="500" />
<img src="./docs/imgs/paddlenlp.png" width="718" height ="100" />
</p>
------------------------------------------------------------------------------------------
[![PyPI - PaddleNLP Version](https://img.shields.io/pypi/v/paddlenlp.svg?label=pip&logo=PyPI&logoColor=white)](https://pypi.org/project/paddlenlp/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/paddlenlp)](https://pypi.org/project/paddlenlp/)
[![PyPI Status](https://pepy.tech/badge/paddlenlp/month)](https://pepy.tech/project/paddlenlp)
![support os](https://img.shields.io/badge/os-linux%2C%20win%2C%20mac-yellow.svg)
![GitHub](https://img.shields.io/github/license/paddlepaddle/paddlenlp)
<p align="center">
<a href="https://paddlenlp.readthedocs.io/en/latest/?badge=latest"><img src="https://readthedocs.org/projects/paddlenlp/badge/?version=latest">
<a href="https://github.com/PaddlePaddle/PaddleNLP/releases"><img src="https://img.shields.io/github/v/release/PaddlePaddle/PaddleNLP?color=ffa"></a>
<a href=""><img src="https://img.shields.io/badge/python-3.7+-aff.svg"></a>
<a href=""><img src="https://img.shields.io/badge/os-linux%2C%20win%2C%20mac-pink.svg"></a>
<a href="https://github.com/PaddlePaddle/PaddleNLP/graphs/contributors"><img src="https://img.shields.io/github/contributors/PaddlePaddle/PaddleNLP?color=9ea"></a>
<a href="https://github.com/PaddlePaddle/PaddleNLP/commits"><img src="https://img.shields.io/github/commit-activity/m/PaddlePaddle/PaddleNLP?color=3af"></a>
<a href="https://pypi.org/project/paddlenlp/"><img src="https://img.shields.io/pypi/dm/paddlenlp?color=9cf"></a>
<a href="https://github.com/PaddlePaddle/PaddleNLP/issues"><img src="https://img.shields.io/github/issues/PaddlePaddle/PaddleNLP?color=9cc"></a>
<a href="https://github.com/PaddlePaddle/PaddleNLP/stargazers"><img src="https://img.shields.io/github/stars/PaddlePaddle/PaddleNLP?color=ccf"></a>
<a href="./LICENSE"><img src="https://img.shields.io/badge/license-Apache%202-dfd.svg"></a>
</p>
<h4 align="center">
<a href=#特性> 特性 </a> |
<a href=#模型支持> 模型支持 </a> |
<a href=#安装> 安装 </a> |
<a href=#快速开始> 快速开始 </a> |
<a href=#社区交流> 社区交流 </a>
</h4>
**PaddleNLP**是一款基于飞桨深度学习框架的大语言模型(LLM)开发套件,支持在多种硬件上进行高效的大模型训练、无损压缩以及高性能推理。PaddleNLP 具备**简单易用**和**性能极致**的特点,致力于助力开发者实现高效的大模型产业级应用。
<a href="https://trendshift.io/repositories/2246" target="_blank"><img src="https://trendshift.io/api/badge/repositories/2246" alt="PaddlePaddle%2FPaddleNLP | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
## News 📢
* **2025.04.29 PaddleNLP 现已支持 Qwen3 系列模型**: Qwen3 系列模型支持持两种思考模式,预训练约 36 万亿个 token、119 种语言和方言。包括六个 Dense 模型, Qwen3-32B、Qwen3-14B、Qwen3-8B、Qwen3-4B、Qwen3-1.7B 和 Qwen3-0.6B。两个 MoE 模型的权重:Qwen3-235B-A22B,Qwen3-30B-A3B。
* **2025.03.12 [PaddleNLP v3.0 Beta4](https://github.com/PaddlePaddle/PaddleNLP/releases/tag/v3.0.0-beta4)**:全面支持 DeepSeek V3/R1/R1-Distill, 及 QwQ-32B 等热门思考模型。**DeepSeek V3/R1完整版支持 FP8、INT8、4-bit 量化推理,MTP 投机解码**。单机 FP8推理输出超**1000 tokens/s**; 4-bit 推理输出超**2100 tokens/s**! 发布新版推理部署镜像,热门模型[一键部署](https://paddlenlp.readthedocs.io/zh/latest/llm/server/docs/general_model_inference.html)。推理部署[使用文档](https://paddlenlp.readthedocs.io/zh/latest/llm/docs/predict/index.html)全面更新,体验全面提升!自研下一代通用信息抽取模型 PP-UIE [全新发布](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/application/information_extraction),支持8K 长度信息抽取。新增大模型 Embedding 训练,支持 INF-CL 超大 batch size 训练。新增[MergeKit](https://paddlenlp.readthedocs.io/zh/latest/llm/docs/mergekit.html)模型融合工具,缓解对齐代价。低资源训练全面优化,16G 小显存可以流畅训练。
## News <img src="./docs/imgs/news_icon.png" width="40"/>
* [2021-10-12] PaddleNLP 2.1版本已发布!新增开箱即用的NLP任务能力、Prompt Tuning应用示例与生成任务的高性能推理!:tada:更多详细升级信息请查看[Release Note](https://github.com/PaddlePaddle/PaddleNLP/releases/tag/v2.1.0)。
* [2021-09-16][《千言-问题匹配鲁棒性评测》](https://www.datafountain.cn/competitions/516)正式开赛啦🔥🔥🔥,欢迎大家踊跃报名!! [官方基线地址](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/examples/text_matching/question_matching)。
* [2021-08-22][《千言:面向事实一致性的生成评测比赛》](https://aistudio.baidu.com/aistudio/competition/detail/105)正式开赛啦🔥🔥🔥,欢迎大家踊跃报名!! [官方基线地址](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/examples/text_generation/unimo-text)。
* **2025.02.10 PaddleNLP 现已支持 DeepSeek-R1系列模型,[在线使用](https://aistudio.baidu.com/projectdetail/8775758)**:依托全新的 PaddleNLP 3.0套件,DeepSeek-R1系列模型现已全面支持。凭借数据并行、数据分组切分并行、模型并行、流水线并行以及专家并行等一系列先进的分布式训练能力,结合 Paddle 框架独有的列稀疏注意力掩码表示技术——FlashMask 方法,DeepSeek-R1系列模型在训练过程中显著降低了显存消耗,同时取得了卓越的训练性能提升。
## 简介
<details><summary> <b>点击展开</b> </summary><div>
PaddleNLP是飞桨自然语言处理开发库,具备**易用的文本领域API**,**多场景的应用示例**、和**高性能分布式训练**三大特点,旨在提升开发者在文本领域的开发效率,并提供丰富的NLP应用示例。
* **2025.03.17 《DeepSeek-R1满血版单机部署实测》** 🔥🔥🔥 飞桨框架3.0大模型推理部署全面升级,支持多款主流大模型,DeepSeek-R1满血版实现单机部署,吞吐提升一倍!欢迎广大用户开箱体验~现已开启有奖活动:完成 DeepSeek-R1-MTP 单机部署任务、提交高质量测评 blog,即可实时赢取奖金!💰💰💰
报名[地址](https://www.wjx.top/vm/OlzzmbG.aspx#), 活动详情:https://github.com/PaddlePaddle/PaddleNLP/issues/10166 , 参考文档:https://github.com/PaddlePaddle/PaddleNLP/issues/10157 。
- **易用的文本领域API**
- 提供丰富的产业级预置任务能力[Taskflow](./docs/model_zoo/taskflow.md)和全流程的文本领域API:支持丰富中文数据集加载的[Dataset API](https://paddlenlp.readthedocs.io/zh/latest/data_prepare/dataset_list.html);灵活高效地完成数据预处理的[Data API](https://paddlenlp.readthedocs.io/zh/latest/source/paddlenlp.data.html);提供100+预训练模型的[Transformer API](./docs/model_zoo/transformers.rst)等,可大幅提升NLP任务建模的效率。
* **2025.03.06 PaddleNLP 现已支持 Qwen/QwQ-32B 模型**: 其模型参数仅有 32B,但其数学推理、编程能力和通用能力可与具备 671B 参数(其中 37B 被激活)的 DeepSeek-R1 媲美。借助 PaddleNLP 3.0套件,现可实现多种并行策略[微调训练](./llm/README.md)、[高性能推理、低比特量化](./llm/docs/predict/qwen.md)和[服务化部署](./llm/server/README.md)。
- **多场景的应用示例**
- 覆盖从学术到产业级的NLP[应用示例](#多场景的应用示例),涵盖NLP基础技术、NLP系统应用以及相关拓展应用。全面基于飞桨核心框架2.0全新API体系开发,为开发者提供飞桨文本领域的最佳实践。
* **2025.02.20 🔥🔥《PP-UIE 信息抽取智能引擎全新升级》** 强化零样本学习能力,支持极少甚至零标注数据实现高效冷启动与迁移学习,显著降低数据标注成本;具备处理长文本能力,支持 8192 个 Token 长度文档信息抽取,实现跨段落识别关键信息,形成完整理解;提供完整可定制化的训练和推理全流程,训练效率相较于 LLama-Factory 实现了1.8倍的提升。
2月26日(周三)19:00为您深度解析全新 PP-UIE 技术方案及在部署方面的功能、优势与技巧。报名链接:https://www.wjx.top/vm/mBKC6pb.aspx?udsid=606418
* **2024.12.16 [PaddleNLP v3.0 Beta3](https://github.com/PaddlePaddle/PaddleNLP/releases/tag/v3.0.0-beta3)**:大模型功能全新升级,新增了 Llama-3.2、DeepSeekV2模型,升级了 TokenizerFast,快速分词,重构了 SFTTrainer,一键开启 SFT 训练。此外,PaddleNLP 还支持了优化器状态的卸载和重载功能,实现了精细化的重新计算,训练性能提升7%。在 Unified Checkpoint 方面,进一步优化了异步保存逻辑,新增 Checkpoint 压缩功能,可节省78.5%存储空间。
最后,在大模型推理方面,升级 Append Attention,支持了 FP8量化,支持投机解码。
* **2024.12.13 📚《飞桨大模型套件 Unified Checkpoint 技术》**,加速模型存储95%,节省空间78%。支持全分布式策略调整自适应转换,提升模型训练的灵活性与可扩展性。训练-压缩-推理统一存储协议,无需手动转换提升全流程体验。Checkpoint 无损压缩结合异步保存,实现秒级存储并降低模型存储成本。适用于智能制造、指挥交通、医疗健康、金融服务等产业实际场景。12月24日(周二)19:00直播为您详细解读该技术如何优化大模型训练流程。报名链接:https://www.wjx.top/vm/huZkHn9.aspx?udsid=787976
* **2024.11.28 📚《FlashRAG-Paddle | 基于 PaddleNLP 的高效开发与评测 RAG 框架》**,为文本更快更好构建准确嵌入表示、加速推理生成速度。PaddleNLP 支持超大 Batch 嵌入表示学习与多硬件高性能推理,涵盖 INT8/INT4量化技术及多种高效注意力机制优化与 TensorCore 深度优化。内置全环节算子融合技术,使得 FlashRAG 推理性能相比 transformers 动态图提升70%以上,结合检索增强知识输出结果更加准确,带来敏捷高效的使用体验。直播时间:12月3日(周二)19:00。报名链接:https://www.wjx.top/vm/eaBa1vA.aspx?udsid=682361
* **2024.08.08 📚《飞桨产业级大语言模型开发利器 PaddleNLP 3.0 重磅发布》**,训压推全流程贯通,主流模型全覆盖。大模型自动并行,千亿模型训推全流程开箱即用。提供产业级高性能精调与对齐解决方案,压缩推理领先,多硬件适配。覆盖产业级智能助手、内容创作、知识问答、关键信息抽取等应用场景。直播时间:8月22日(周四)19:00。报名链接:https://www.wjx.top/vm/Y2f7FFY.aspx?udsid=143844
* **2024.06.27 [PaddleNLP v3.0 Beta](https://github.com/PaddlePaddle/PaddleNLP/releases/tag/v3.0.0-beta0)**:拥抱大模型,体验全升级。统一大模型套件,实现国产计算芯片全流程接入;全面支持飞桨4D 并行配置、高效精调策略、高效对齐算法、高性能推理等大模型产业级应用流程;自研极致收敛的 RsLoRA+算法、自动扩缩容存储机制 Unified Checkpoint 和通用化支持的 FastFFN、FusedQKV 助力大模型训推;主流模型持续支持更新,提供高效解决方案。
* **2024.04.24 [PaddleNLP v2.8](https://github.com/PaddlePaddle/PaddleNLP/releases/tag/v2.8.0)**:自研极致收敛的 RsLoRA+算法,大幅提升 PEFT 训练收敛速度以及训练效果;引入高性能生成加速到 RLHF PPO 算法,打破 PPO 训练中生成速度瓶颈,PPO 训练性能大幅领先。通用化支持 FastFFN、FusedQKV 等多个大模型训练性能优化方式,大模型训练更快、更稳定。
</div></details>
## 特性
### <a href=#多硬件训推一体> 🔧 多硬件训推一体 </a>
支持英伟达 GPU、昆仑 XPU、昇腾 NPU、燧原 GCU 和海光 DCU 等多个硬件的大模型和自然语言理解模型训练和推理,套件接口支持硬件快速切换,大幅降低硬件切换研发成本。
当前支持的自然语言理解模型:[多硬件自然语言理解模型列表](./docs/zh/model_zoo/model_list_multy_device.md)
### <a href=#高效易用的预训练> 🚀 高效易用的预训练 </a>
支持纯数据并行策略、分组参数切片的数据并行策略、张量模型并行策略和流水线模型并行策略的4D 高性能训练,Trainer 支持分布式策略配置化,降低复杂分布式组合带来的使用成本;
[Unified Checkpoint 大模型存储工具](./llm/docs/unified_checkpoint.md)可以使得训练断点支持机器资源动态扩缩容恢复。此外,异步保存,模型存储可加速95%,Checkpoint 压缩,可节省78.5%存储空间。
### <a href=#高效精调> 🤗 高效精调 </a>
精调算法深度结合零填充数据流和 [FlashMask](./llm/docs/flashmask.md) 高性能算子,降低训练无效数据填充和计算,大幅提升精调训练吞吐。
### <a href=#无损压缩和高性能推理> 🎛️ 无损压缩和高性能推理 </a>
大模型套件高性能推理模块内置动态插入和全环节算子融合策略,极大加快并行推理速度。底层实现细节封装化,实现开箱即用的高性能并行推理能力。
## 文档
更多详细文档, 请访问 [PaddleNLP Documentation](https://paddlenlp.readthedocs.io/).
------------------------------------------------------------------------------------------
## 模型支持
* 模型参数已支持 LLaMA 系列、Baichuan 系列、Bloom 系列、ChatGLM 系列、Gemma 系列、Mistral 系列、OPT 系列和 Qwen 系列,详细列表👉【LLM】模型参数支持列表如下:
| 模型系列 | 模型名称 |
|:-------------------------------------------------------------------------------------------------------:|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [PP-UIE](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/application/information_extraction) | paddlenlp/PP-UIE-0.5B, paddlenlp/PP-UIE-1.5B, paddlenlp/PP-UIE-7B, paddlenlp/PP-UIE-14B |
| [LLaMA](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/llama) | facebook/llama-7b, facebook/llama-13b, facebook/llama-30b, facebook/llama-65b |
| [Llama2](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/llama) | meta-llama/Llama-2-7b, meta-llama/Llama-2-7b-chat, meta-llama/Llama-2-13b, meta-llama/Llama-2-13b-chat, meta-llama/Llama-2-70b, meta-llama/Llama-2-70b-chat |
| [Llama3](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/llama) | meta-llama/Meta-Llama-3-8B, meta-llama/Meta-Llama-3-8B-Instruct, meta-llama/Meta-Llama-3-70B, meta-llama/Meta-Llama-3-70B-Instruct |
| [Llama3.1](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/llama) | meta-llama/Meta-Llama-3.1-8B, meta-llama/Meta-Llama-3.1-8B-Instruct, meta-llama/Meta-Llama-3.1-70B, meta-llama/Meta-Llama-3.1-70B-Instruct, meta-llama/Meta-Llama-3.1-405B, meta-llama/Meta-Llama-3.1-405B-Instruct, meta-llama/Llama-Guard-3-8B |
| [Llama3.2](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/llama) | meta-llama/Llama-3.2-1B, meta-llama/Llama-3.2-1B-Instruct, meta-llama/Llama-3.2-3B, meta-llama/Llama-3.2-3B-Instruct, meta-llama/Llama-Guard-3-1B |
| [Llama3.3](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/llama) | meta-llama/Llama-3.3-70B-Instruct |
| [Baichuan](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/baichuan) | baichuan-inc/Baichuan-7B, baichuan-inc/Baichuan-13B-Base, baichuan-inc/Baichuan-13B-Chat |
| [Baichuan2](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/baichuan) | baichuan-inc/Baichuan2-7B-Base, baichuan-inc/Baichuan2-7B-Chat, baichuan-inc/Baichuan2-13B-Base, baichuan-inc/Baichuan2-13B-Chat |
| [Bloom](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/bloom) | bigscience/bloom-560m, bigscience/bloom-560m-bf16, bigscience/bloom-1b1, bigscience/bloom-3b, bigscience/bloom-7b1, bigscience/bloomz-560m, bigscience/bloomz-1b1, bigscience/bloomz-3b, bigscience/bloomz-7b1-mt, bigscience/bloomz-7b1-p3, bigscience/bloomz-7b1, bellegroup/belle-7b-2m |
| [ChatGLM](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/chatglm/) | THUDM/chatglm-6b, THUDM/chatglm-6b-v1.1 |
| [ChatGLM2](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/chatglm2) | THUDM/chatglm2-6b |
| [ChatGLM3](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/chatglm2) | THUDM/chatglm3-6b |
| [DeepSeekV2](https://github.com/PaddlePaddle/PaddleNLP/blob/develop/llm/config/deepseek-v2) | deepseek-ai/DeepSeek-V2, deepseek-ai/DeepSeek-V2-Chat, deepseek-ai/DeepSeek-V2-Lite, deepseek-ai/DeepSeek-V2-Lite-Chat, deepseek-ai/DeepSeek-Coder-V2-Base, deepseek-ai/DeepSeek-Coder-V2-Instruct, deepseek-ai/DeepSeek-Coder-V2-Lite-Base, deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct |
| [DeepSeekV3](https://github.com/PaddlePaddle/PaddleNLP/blob/develop/llm/config/deepseek-v2) | deepseek-ai/DeepSeek-V3, deepseek-ai/DeepSeek-V3-Base |
| [DeepSeek-R1](https://github.com/PaddlePaddle/PaddleNLP/blob/develop/llm/config/deepseek-v2) | deepseek-ai/DeepSeek-R1, deepseek-ai/DeepSeek-R1-Zero, deepseek-ai/DeepSeek-R1-Distill-Llama-70B, deepseek-ai/DeepSeek-R1-Distill-Llama-8B, deepseek-ai/DeepSeek-R1-Distill-Qwen-14B, deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B, deepseek-ai/DeepSeek-R1-Distill-Qwen-32B, deepseek-ai/DeepSeek-R1-Distill-Qwen-7B |
| [Gemma](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/gemma) | google/gemma-7b, google/gemma-7b-it, google/gemma-2b, google/gemma-2b-it |
| [Mistral](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/mistral) | mistralai/Mistral-7B-Instruct-v0.3, mistralai/Mistral-7B-v0.1 |
| [Mixtral](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/mixtral) | mistralai/Mixtral-8x7B-Instruct-v0.1 |
| [OPT](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/opt) | facebook/opt-125m, facebook/opt-350m, facebook/opt-1.3b, facebook/opt-2.7b, facebook/opt-6.7b, facebook/opt-13b, facebook/opt-30b, facebook/opt-66b, facebook/opt-iml-1.3b, opt-iml-max-1.3b |
| [Qwen](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/qwen/) | qwen/qwen-7b, qwen/qwen-7b-chat, qwen/qwen-14b, qwen/qwen-14b-chat, qwen/qwen-72b, qwen/qwen-72b-chat, |
| [Qwen1.5](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/qwen/) | Qwen/Qwen1.5-0.5B, Qwen/Qwen1.5-0.5B-Chat, Qwen/Qwen1.5-1.8B, Qwen/Qwen1.5-1.8B-Chat, Qwen/Qwen1.5-4B, Qwen/Qwen1.5-4B-Chat, Qwen/Qwen1.5-7B, Qwen/Qwen1.5-7B-Chat, Qwen/Qwen1.5-14B, Qwen/Qwen1.5-14B-Chat, Qwen/Qwen1.5-32B, Qwen/Qwen1.5-32B-Chat, Qwen/Qwen1.5-72B, Qwen/Qwen1.5-72B-Chat, Qwen/Qwen1.5-110B, Qwen/Qwen1.5-110B-Chat, Qwen/Qwen1.5-MoE-A2.7B, Qwen/Qwen1.5-MoE-A2.7B-Chat |
| [Qwen2](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/qwen/) | Qwen/Qwen2-0.5B, Qwen/Qwen2-0.5B-Instruct, Qwen/Qwen2-1.5B, Qwen/Qwen2-1.5B-Instruct, Qwen/Qwen2-7B, Qwen/Qwen2-7B-Instruct, Qwen/Qwen2-72B, Qwen/Qwen2-72B-Instruct, Qwen/Qwen2-57B-A14B, Qwen/Qwen2-57B-A14B-Instruct |
| [Qwen2-Math](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/qwen/) | Qwen/Qwen2-Math-1.5B, Qwen/Qwen2-Math-1.5B-Instruct, Qwen/Qwen2-Math-7B, Qwen/Qwen2-Math-7B-Instruct, Qwen/Qwen2-Math-72B, Qwen/Qwen2-Math-72B-Instruct, Qwen/Qwen2-Math-RM-72B |
| [Qwen2.5](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/qwen/) | Qwen/Qwen2.5-0.5B, Qwen/Qwen2.5-0.5B-Instruct, Qwen/Qwen2.5-1.5B, Qwen/Qwen2.5-1.5B-Instruct, Qwen/Qwen2.5-3B, Qwen/Qwen2.5-3B-Instruct, Qwen/Qwen2.5-7B, Qwen/Qwen2.5-7B-Instruct, Qwen/Qwen2.5-7B-Instruct-1M, Qwen/Qwen2.5-14B, Qwen/Qwen2.5-14B-Instruct, Qwen/Qwen2.5-14B-Instruct-1M, Qwen/Qwen2.5-32B, Qwen/Qwen2.5-32B-Instruct, Qwen/Qwen2.5-72B, Qwen/Qwen2.5-72B-Instruct |
| [Qwen2.5-Math](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/qwen/) | Qwen/Qwen2.5-Math-1.5B, Qwen/Qwen2.5-Math-1.5B-Instruct, Qwen/Qwen2.5-Math-7B, Qwen/Qwen2.5-Math-7B-Instruct, Qwen/Qwen2.5-Math-72B, Qwen/Qwen2.5-Math-72B-Instruct, Qwen/Qwen2.5-Math-RM-72B |
| [Qwen2.5-Coder](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/qwen/) | Qwen/Qwen2.5-Coder-1.5B, Qwen/Qwen2.5-Coder-1.5B-Instruct, Qwen/Qwen2.5-Coder-7B, Qwen/Qwen2.5-Coder-7B-Instruct |
| [Qwen3](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/qwen/) | Qwen/Qwen3-0.6B, Qwen/Qwen3-1.7B, Qwen/Qwen3-4B, Qwen/Qwen3-8B, Qwen/Qwen3-14B, Qwen/Qwen3-32B, Qwen/Qwen3-30B-A3B, Qwen/Qwen3-235B-A22B, Qwen/Qwen3-0.6B-Base, Qwen/Qwen3-1.7B-Base, Qwen/Qwen3-4B-Base, Qwen/Qwen3-8B-Base, Qwen/Qwen3-14B-Base, Qwen/Qwen3-30B-A3B-Base |
| [QwQ](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/qwen/) | Qwen/QwQ-32B, Qwen/QwQ-32B-Preview |
| [Yuan2](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm/config/yuan/) | IEITYuan/Yuan2-2B, IEITYuan/Yuan2-51B, IEITYuan/Yuan2-102B |
* 4D 并行和算子优化已支持 LLaMA 系列、Baichuan 系列、Bloom 系列、ChatGLM 系列、Gemma 系列、Mistral 系列、OPT 系列和 Qwen 系列,【LLM】模型4D 并行和算子支持列表如下:
| 模型名称/并行能力支持 | 数据并行 | 张量模型并行 | | 参数分片并行 | | | 流水线并行 |
|:---------------------:|:--------:|:------------:|:--------:|:------------:|:------:|:------:|:----------:|
| | | 基础能力 | 序列并行 | stage1 | stage2 | stage3 | |
| Llama | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Qwen | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Qwen1.5 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Qwen2 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Mixtral(moe) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 🚧 |
| Mistral | ✅ | ✅ | 🚧 | ✅ | ✅ | ✅ | 🚧 |
| Baichuan | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Baichuan2 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| ChatGLM | ✅ | ✅ | 🚧 | ✅ | ✅ | ✅ | 🚧 |
| ChatGLM2 | ✅ | 🚧 | 🚧 | ✅ | ✅ | ✅ | 🚧 |
| ChatGLM3 | ✅ | 🚧 | 🚧 | ✅ | ✅ | ✅ | 🚧 |
| Bloom | ✅ | ✅ | 🚧 | ✅ | ✅ | ✅ | 🚧 |
| GPT-2/GPT-3 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| OPT | ✅ | ✅ | 🚧 | ✅ | ✅ | ✅ | 🚧 |
| Gemma | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Yuan2 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 🚧 |
* 大模型预训练、精调(包含 SFT、PEFT 技术)、对齐、量化已支持 LLaMA 系列、Baichuan 系列、Bloom 系列、ChatGLM 系列、Mistral 系列、OPT 系列和 Qwen 系列,【LLM】模型预训练、精调、对齐、量化支持列表如下:
| Model | Pretrain | SFT | LoRA | FlashMask | Prefix Tuning | DPO/SimPO/ORPO/KTO | RLHF | Mergekit | Quantization |
|--------------------------------------------|:--------:|:---:|:----:|:---------:|:-------------:|:------------------:|:----:|:--------:|:------------:|
| [Llama](./llm/config/llama) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| [Qwen](./llm/config/qwen) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 🚧 | ✅ | 🚧 |
| [Mixtral](./llm/config/mixtral) | ✅ | ✅ | ✅ | 🚧 | 🚧 | ✅ | 🚧 | ✅ | 🚧 |
| [Mistral](./llm/config/mistral) | ✅ | ✅ | ✅ | 🚧 | ✅ | ✅ | 🚧 | ✅ | 🚧 |
| [Baichuan/Baichuan2](./llm/config/llama) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 🚧 | ✅ | ✅ |
| [ChatGLM-6B](./llm/config/chatglm) | ✅ | ✅ | ✅ | 🚧 | ✅ | 🚧 | 🚧 | ✅ | ✅ |
| [ChatGLM2/ChatGLM3](./llm/config/chatglm2) | ✅ | ✅ | ✅ | 🚧 | ✅ | ✅ | 🚧 | ✅ | ✅ |
| [Bloom](./llm/config/bloom) | ✅ | ✅ | ✅ | 🚧 | ✅ | 🚧 | 🚧 | ✅ | ✅ |
| [GPT-3](./llm/config/gpt-3) | ✅ | ✅ | 🚧 | 🚧 | 🚧 | 🚧 | 🚧 | ✅ | 🚧 |
| [OPT](./llm/config/opt) | ✅ | ✅ | ✅ | 🚧 | 🚧 | 🚧 | 🚧 | ✅ | 🚧 |
| [Gemma](./llm/config/gemma) | ✅ | ✅ | ✅ | 🚧 | 🚧 | ✅ | 🚧 | ✅ | 🚧 |
| [Yuan](./llm/config/yuan) | ✅ | ✅ | ✅ | 🚧 | 🚧 | ✅ | 🚧 | ✅ | 🚧 |
* [大模型推理](./llm/docs/predict/inference.md)已支持 LLaMA 系列、Qwen 系列、DeepSeek 系列、Mistral 系列、ChatGLM 系列、Bloom 系列和 Baichuan 系列,支持 Weight Only INT8及 INT4推理,支持 WAC(权重、激活、Cache KV)进行 INT8、FP8量化的推理,【LLM】模型推理支持列表如下:
| 模型名称/量化类型支持 | FP16/BF16 | WINT8 | WINT4 | INT8-A8W8 | FP8-A8W8 | INT8-A8W8C8 |
|:------------------------------------------:|:---------:|:-----:|:-----:|:---------:|:--------:|:-----------:|
| [LLaMA](./llm/docs/predict/llama.md) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| [Qwen](./llm/docs/predict/qwen.md) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| [DeepSeek](./llm/docs/predict/deepseek.md) | ✅ | ✅ | ✅ | 🚧 | ✅ | 🚧 |
| [Qwen-Moe](./llm/docs/predict/qwen.md) | ✅ | ✅ | ✅ | 🚧 | 🚧 | 🚧 |
| [Mixtral](./llm/docs/predict/mixtral.md) | ✅ | ✅ | ✅ | 🚧 | 🚧 | 🚧 |
| ChatGLM | ✅ | ✅ | ✅ | 🚧 | 🚧 | 🚧 |
| Bloom | ✅ | ✅ | ✅ | 🚧 | 🚧 | 🚧 |
| BaiChuan | ✅ | ✅ | ✅ | ✅ | ✅ | 🚧 |
- **高性能分布式训练**
- 基于飞桨核心框架领先的自动混合精度优化策略,结合分布式Fleet API,支持4D混合并行策略,可高效地完成大规模预训练模型训练。
## 安装
### 环境依赖
* python >= 3.8
* paddlepaddle >= 3.0.0rc1
- python >= 3.6
- paddlepaddle >= 2.2
如果您尚未安装 PaddlePaddle,请参考 [飞桨官网](https://www.paddlepaddle.org.cn/) 进行安装。
### pip 安装
### pip安装
```shell
pip install --upgrade paddlenlp==3.0.0b4
pip install --upgrade paddlenlp
```
或者可通过以下命令安装最新 develop 分支代码:
更多关于PaddlePaddle和PaddleNLP安装的详细教程请查看[Installation](./docs/get_started/installation.rst)。
```shell
pip install --pre --upgrade paddlenlp -f https://www.paddlepaddle.org.cn/whl/paddlenlp.html
```
## 易用的文本领域API
更多关于 PaddlePaddle 和 PaddleNLP 安装的详细教程请查看[Installation](./docs/zh/get_started/installation.rst)。
### Taskflow:开箱即用的产业级NLP能力
------------------------------------------------------------------------------------------
## 快速开始
### 大模型文本生成
PaddleNLP 提供了方便易用的 Auto API,能够快速的加载模型和 Tokenizer。这里以使用 `Qwen/Qwen2-0.5B` 模型做文本生成为例:
Taskflow旨在提供**开箱即用**的NLP预置任务能力,覆盖自然语言理解与生成两大场景,提供**产业级的效果**与**极致的预测性能**。
```python
from paddlenlp.transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-0.5B")
# if using CPU, please change float16 to float32
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-0.5B", dtype="float16")
input_features = tokenizer("你好!请自我介绍一下。", return_tensors="pd")
outputs = model.generate(**input_features, max_new_tokens=128)
print(tokenizer.batch_decode(outputs[0], skip_special_tokens=True))
# ['我是一个AI语言模型,我可以回答各种问题,包括但不限于:天气、新闻、历史、文化、科学、教育、娱乐等。请问您有什么需要了解的吗?']
from paddlenlp import Taskflow
# 中文分词
seg = Taskflow("word_segmentation")
seg("第十四届全运会在西安举办")
>>> ['第十四届', '全运会', '在', '西安', '举办']
# 词性标注
tag = Taskflow("pos_tagging")
tag("第十四届全运会在西安举办")
>>> [('第十四届', 'm'), ('全运会', 'nz'), ('在', 'p'), ('西安', 'LOC'), ('举办', 'v')]
# 命名实体识别
ner = Taskflow("ner")
ner("《孤女》是2010年九州出版社出版的小说,作者是余兼羽")
>>> [('《', 'w'), ('孤女', '作品类_实体'), ('》', 'w'), ('是', '肯定词'), ('2010年', '时间类'), ('九州出版社', '组织机构类'), ('出版', '场景事件'), ('的', '助词'), ('小说', '作品类_概念'), (',', 'w'), ('作者', '人物类_概念'), ('是', '肯定词'), ('余兼羽', '人物类_实体')]
# 句法分析
ddp = Taskflow("dependency_parsing")
ddp("9月9日上午纳达尔在亚瑟·阿什球场击败俄罗斯球员梅德韦杰夫")
>>> [{'word': ['9月9日', '上午', '纳达尔', '在', '亚瑟·阿什球场', '击败', '俄罗斯', '球员', '梅德韦杰夫'], 'head': [2, 6, 6, 5, 6, 0, 8, 9, 6], 'deprel': ['ATT', 'ADV', 'SBV', 'MT', 'ADV', 'HED', 'ATT', 'ATT', 'VOB']}]
# 情感分析
senta = Taskflow("sentiment_analysis")
senta("这个产品用起来真的很流畅,我非常喜欢")
>>> [{'text': '这个产品用起来真的很流畅,我非常喜欢', 'label': 'positive', 'score': 0.9938690066337585}]
```
更多使用方法请参考[Taskflow文档](./docs/model_zoo/taskflow.md)。
### 大模型预训练
### Transformer API: 强大的预训练模型生态底座
```shell
git clone https://github.com/PaddlePaddle/PaddleNLP.git && cd PaddleNLP # 如已clone或下载PaddleNLP可跳过
mkdir -p llm/data && cd llm/data
wget https://bj.bcebos.com/paddlenlp/models/transformers/llama/data/llama_openwebtext_100k.bin
wget https://bj.bcebos.com/paddlenlp/models/transformers/llama/data/llama_openwebtext_100k.idx
cd .. # change folder to PaddleNLP/llm
# 如需使用use_fused_rms_norm=true,需要前往slm/model_zoo/gpt-3/external_ops安装fused_ln
python -u run_pretrain.py ./config/qwen/pretrain_argument_0p5b.json
```
### 大模型 SFT 精调
```shell
git clone https://github.com/PaddlePaddle/PaddleNLP.git && cd PaddleNLP # 如已clone或下载PaddleNLP可跳过
mkdir -p llm/data && cd llm/data
wget https://bj.bcebos.com/paddlenlp/datasets/examples/AdvertiseGen.tar.gz && tar -zxvf AdvertiseGen.tar.gz
cd .. # change folder to PaddleNLP/llm
python -u run_finetune.py ./config/qwen/sft_argument_0p5b.json
```
更多大模型全流程步骤,请参考[飞桨大模型套件](./llm)介绍。
另外我们还提供了快速微调方式, 无需 clone 源代码:
覆盖**30**个网络结构和**100**余个预训练模型参数,既包括百度自研的预训练模型如ERNIE系列, PLATO, SKEP等,也涵盖业界主流的中文预训练模型如BERT,GPT,XLNet,BART等。使用AutoModel可以下载不同网络结构的预训练模型。欢迎开发者加入贡献更多预训练模型!🤗
```python
from paddlenlp.trl import SFTConfig, SFTTrainer
from datasets import load_dataset
from paddlenlp.transformers import *
dataset = load_dataset("ZHUI/alpaca_demo", split="train")
training_args = SFTConfig(output_dir="Qwen/Qwen2.5-0.5B-SFT", device="gpu")
trainer = SFTTrainer(
args=training_args,
model="Qwen/Qwen2.5-0.5B-Instruct",
train_dataset=dataset,
)
trainer.train()
ernie = AutoModel.from_pretrained('ernie-1.0')
ernie_gram = AutoModel.from_pretrained('ernie-gram-zh')
bert = AutoModel.from_pretrained('bert-wwm-chinese')
albert = AutoModel.from_pretrained('albert-chinese-tiny')
roberta = AutoModel.from_pretrained('roberta-wwm-ext')
electra = AutoModel.from_pretrained('chinese-electra-small')
gpt = AutoModelForPretraining.from_pretrained('gpt-cpm-large-cn')
```
更多 PaddleNLP 内容可参考:
对预训练模型应用范式如语义表示、文本分类、句对匹配、序列标注、问答等,提供统一的API体验。
* [精选模型库](./slm/model_zoo),包含优质预训练模型的端到端全流程使用。
* [多场景示例](./slm/examples),了解如何使用 PaddleNLP 解决 NLP 多种技术问题,包含基础技术、系统应用与拓展应用。
* [交互式教程](https://aistudio.baidu.com/aistudio/personalcenter/thirdview/574995),在🆓免费算力平台 AI Studio 上快速学习 PaddleNLP。
```python
import paddle
from paddlenlp.transformers import *
------------------------------------------------------------------------------------------
tokenizer = AutoTokenizer.from_pretrained('ernie-1.0')
text = tokenizer('自然语言处理')
## 社区交流
# 语义表示
model = AutoModel.from_pretrained('ernie-1.0')
sequence_output, pooled_output = model(input_ids=paddle.to_tensor([text['input_ids']]))
# 文本分类 & 句对匹配
model = AutoModelForSequenceClassification.from_pretrained('ernie-1.0')
# 序列标注
model = AutoModelForTokenClassification.from_pretrained('ernie-1.0')
# 问答
model = AutoModelForQuestionAnswering.from_pretrained('ernie-1.0')
```
* 微信扫描二维码并填写问卷,即可加入交流群与众多社区开发者以及官方团队深度交流.
请参考[Transformer API文档](./docs/model_zoo/transformers.rst)查看目前支持的预训练模型结构、参数和详细用法。
### Dataset API: 丰富的中文数据集
Dataset API提供便捷、高效的数据集加载功能;内置[千言数据集](https://www.luge.ai/),提供丰富的面向自然语言理解与生成场景的中文数据集,为NLP研究人员提供一站式的科研体验。
```python
from paddlenlp.datasets import load_dataset
train_ds, dev_ds, test_ds = load_dataset("chnsenticorp", splits=["train", "dev", "test"])
train_ds, dev_ds = load_dataset("lcqmc", splits=["train", "dev"])
```
可参考[Dataset文档](https://paddlenlp.readthedocs.io/zh/latest/data_prepare/dataset_list.html) 查看更多数据集。
### Embedding API: 一键加载预训练词向量
```python
from paddlenlp.embeddings import TokenEmbedding
wordemb = TokenEmbedding("w2v.baidu_encyclopedia.target.word-word.dim300")
print(wordemb.cosine_sim("国王", "王后"))
>>> 0.63395125
wordemb.cosine_sim("艺术", "火车")
>>> 0.14792643
```
内置50+中文词向量,覆盖多种领域语料、如百科、新闻、微博等。更多使用方法请参考[Embedding文档](./docs/model_zoo/embeddings.md)。
### 更多API使用文档
- [Data API](./docs/data.md): 提供便捷高效的文本数据处理功能
- [Metrics API](./docs/metrics.md): 提供NLP任务的评估指标,与飞桨高层API兼容。
更多的API示例与使用说明请查阅[PaddleNLP官方文档](https://paddlenlp.readthedocs.io/)
## 多场景的应用示例
PaddleNLP提供了多粒度、多场景的NLP应用示例,面向动态图模式和全新的API体系开发,更加简单易懂。
涵盖了[NLP基础技术](#nlp-基础技术)、[NLP系统应用](#nlp-系统应用)以及文本相关的[NLP拓展应用](#拓展应用)、与知识库结合的[文本知识关联](./examples/text_to_knowledge)、与图结合的[文本图学习](./examples/text_graph/)等。
### NLP 基础技术
| 任务 | 简介 |
| :------------ | ---- |
| [词向量](./examples/word_embedding/) | 利用`TokenEmbedding API`展示如何快速计算词之间语义距离和词的特征提取。 |
| [词法分析](./examples/lexical_analysis/) | 基于BiGRU-CRF模型实现了分词、词性标注和命名实体识的联合训练任务。 |
| [语言模型](./examples/language_model/) | 覆盖了经典的LSTM类语言模型,和Transformer类的预训练语言模型,如BERT, ERNIE, GPT, XLNet, BART,ELECTRA,BigBird等。 |
| [语义解析](./examples/text_to_sql/):star: | 语义解析Text-to-SQL任务是让机器自动让自然语言问题转换数据库可操作的SQL查询语句,是实现基于数据库自动问答的核心模块。|
#### 句法依存分析 (Dependency Parsing)
| 模型 | 简介 |
| :----- | ------ |
| [Deep Biaffine Parsing](./examples/dependency_parsing/) | 实现经典的Deep Biaffine Parsing句法分析模型,并提供高质量的中文预训练模型可供直接使用。|
#### 文本分类 (Text Classification)
| 模型 | 简介 |
| :----- | ------ |
| [RNN/CNN/GRU/LSTM](./examples/text_classification/rnn) | 实现了经典的RNN, CNN, GRU, LSTM等经典文本分类结构。|
| [BiLSTM-Attention](./examples/text_classification/rnn) | 基于BiLSTM网络结构引入注意力机制提升文本分类效果。 |
| [BERT/ERNIE](./examples/text_classification/pretrained_models) | 提供基于预训练模型的文本分类任务实现,包含训练、预测和推理部署的全流程应用。 |
#### 文本匹配 (Text Matching)
| 模型 | 简介 |
| :--------------- | ---------- |
| [SimCSE](./examples/text_matching/simcse/):star2: | 基于论文[SimCSE: Simple Contrastive Learning of Sentence Embeddings](https://arxiv.org/abs/2104.08821)实现无监督语义匹配模型,无需标注数据仅利用无监督数据也能训练效果出众的语义匹配模型。|
| [ERNIE-Gram w/ R-Drop](./examples/text_matching/question_matching/) | 提供基于ERNIE-Gram预训练模型结合R-Drop策略的问题匹配任在千言数据集上的基线代码。|
| [SimNet](./examples/text_matching/simnet/) | 百度自研的语义匹配框架,使用BOW、CNN、GRNN等核心网络作为表示层,在百度内搜索、推荐等多个应用场景得到广泛易用。|
| [ERNIE](./examples/text_matching/ernie_matching/) | 基于ERNIE使用LCQMC数据完成中文句对匹配任务,提供了Pointwise和Pairwise两种类型学习方式。 |
| [Sentence-BERT](./examples/text_matching/sentence_transformers/) | 提供基于Siamese双塔结构的文本匹配模型[Sentence-BERT](https://arxiv.org/abs/1908.1008)实现,可用于获取文本的向量化表示。 |
| [SimBERT](./examples/text_matching/simbert/) | 提供[SimBERT](https://github.com/ZhuiyiTechnology/simbert)模型实现,用于获取文本的向量化表示。|
#### 文本生成 (Text Generation)
| 模型 | 简介 |
| :------------ | ---------- |
| [Seq2Seq](./examples/text_generation/couplet) | 实现了经典的Seq2Seq with Attention的网络结构,并提供在自动对联的文本生成应用示例。 |
| [VAE-Seq2Seq](./examples/text_generation/vae-seq2seq) | 在Seq2Seq框架基础上,加入VAE结构以实现更加多样化的文本生成。|
| [ERNIE-GEN](./examples/text_generation/ernie-gen) | [ERNIE-GEN](https://arxiv.org/abs/2001.11314)是百度NLP提出的基于多流(multi-flow)机制生成完整语义片段的预训练模型,基于该模型实现了提供了智能写诗的应用示例。|
#### 文本纠错 (Text Correction)
| 模型 | 简介 |
| :------------ | ---------- |
| [ERNIE-CSC](./examples/text_correction/ernie-csc):star1: | [ERNIE-CSC](https://aclanthology.org/2021.findings-acl.198.pdf)是基于ERNIE预训练模型融合了拼音特征的端到端中文拼写纠错模型,在SIGHAN数据集上取得SOTA的效果。|
#### 语义索引 (Semantic Indexing)
提供一套完整的语义索引开发流程,并提供了In-Batch Negative和Hardest Negatives两种策略,开发者可基于该示例实现一个轻量级的语义索引系统,更多信息请查看[语义索引应用示例](./examples/semantic_indexing/)。
#### 信息抽取 (Information Extraction)
| 任务 | 简介 |
| :--------------- | ---- |
| [DuEE](./examples/information_extraction/DuEE/) | 基于[DuEE](https://link.springer.com/chapter/10.1007/978-3-030-60457-8_44)数据集,使用预训练模型的方式提供句子级和篇章级的事件抽取示例。 |
| [DuIE](./examples/information_extraction/DuIE/) | 基于[DuIE](http://tcci.ccf.org.cn/conference/2019/papers/EV10.pdf)数据集,使用预训练模型的方式提供关系抽取示例。 |
| [快递单信息抽取](./examples/information_extraction/waybill_ie/) | 提供BiLSTM+CRF和预训练模型两种方式完成真实的快递单信息抽取案例。 |
### NLP 系统应用
#### 情感分析 (Sentiment Analysis)
| 模型 | 简介 |
| :--------- | ---------- |
| [SKEP](./examples/sentiment_analysis/skep/):star2: | [SKEP](https://arxiv.org/abs/2005.05635)是百度提出的基于情感知识增强的预训练算法,利用无监督挖掘的海量情感知识构建预训练目标,让模型更好理解情感语义,可为各类情感分析任务提供统一且强大的情感语义表示。 |
#### 阅读理解 (Machine Reading Comprehension)
| 任务 | 简介 |
| :------------------- | ---- |
| [SQuAD](./examples/machine_reading_comprehension/SQuAD/) | 提供预训练模型在SQuAD 2.0数据集上微调的应用示例。 |
| [DuReader-yesno](./examples/machine_reading_comprehension/DuReader-yesno/) | 提供预训练模型在**千言数据集DuReader-yesno**上微调的应用示例。 |
| [DuReader-robust](./examples/machine_reading_comprehension/DuReader-robust/) | 提供预训练模型在**千言数据集DuReader-robust**上微调的应用示例。 |
#### 文本翻译 (Text Translation)
| 模型 | 简介 |
| :--------------- | ------- |
| [Seq2Seq-Attn](./examples/machine_translation/seq2seq) | 提供了[Effective Approaches to Attention-based Neural Machine Translation](https://arxiv.org/abs/1508.04025v5)基于注意力机制改进的Seq2Seq经典神经网络机器翻译模型实现。|
| [Transformer](./examples/machine_translation/transformer) | 提供了基于[Attention Is All You Need](https://arxiv.org/abs/1706.03762)论文的Transformer机器翻译实现,包含了完整的训练到推理部署的全流程实现。|
#### 同传翻译 (Simultaneous Translation)
| 模型 | 简介 |
| :---------- | ------- |
| [STACL](./examples/simultaneous_translation/stacl) :star:| [STACL](https://www.aclweb.org/anthology/P19-1289/)是百度自研的基于Prefix-to-Prefix框架的同传翻译模型,结合Wait-k策略可以在保持较高的翻译质量的同时实现任意字级别的翻译延迟,并提供了轻量级同声传译系统搭建教程。|
#### 对话系统 (Dialogue System)
| 模型 | 简介 |
| :---------------- | ------|
| [PLATO-2](./examples/dialogue/plato-2) | [PLATO-2](https://arxiv.org/abs/2006.16779)是百度自研领先的基于课程学习两阶段方式训练的开放域对话预训练模型。|
| [PLATO-mini](./examples/dialogue/unified_transformer):star2: | 基于6层UnifiedTransformer预训练结构,结合海量中文对话语料数据预训练的轻量级**中文**闲聊对话模型。|
### 拓展应用
#### 文本知识关联 (Text to Knowledge)
:star2:[**解语**](./examples/text_to_knowledge/)是由百度知识图谱部开发的文本知识关联框架,覆盖中文全词类的知识库和知识标注工具,能够帮助开发者面对更加多元的应用场景,方便地融合自有知识体系,显著提升中文文本解析和挖掘效果,还可以便捷地利用知识增强机器学习模型效果。
* [TermTree: 中文全词类的知识库](./examples/text_to_knowledge/termtree):star2:
* [WordTag: 中文词类知识标注工具](./examples/text_to_knowledge/wordtag):star2:
* [NPTag: 中文名词短语标注工具](./examples/text_to_knowledge/nptag):star2:
#### 文本图学习 (Text Graph Learning)
| 模型 | 简介 |
| :------------ | ------- |
| [ERNIESage](./examples/text_graph/erniesage)| 基于[飞桨PGL](https://github.com/PaddlePaddle/PGL)图学习框架结合PaddleNLP Transformer API实现的文本与图结构融合的模型。|
#### 模型压缩 (Model Compression)
| 模型 | 简介 |
| :--------------------------------------------------------- | ------------------------------------------------------------ |
| [MiniLMv2](examples/model_compression/minilmv2) :star2: | 基于[MiniLMv2: Multi-Head Self-Attention Relation Distillation for Compressing Pretrained Transformers](https://arxiv.org/abs/2012.15828)论文策略的实现,是一种通用蒸馏方法。本实例以`bert-base-chinese`为教师模型,利用中文数据进行了通用蒸馏。 |
| [TinyBERT](./examples/model_compression/tinybert) | 基于论文[TinyBERT: Distilling BERT for Natural Language Understanding](https://arxiv.org/abs/1909.10351)的实现,提供了通用蒸馏和下游任务蒸馏的脚本。本实例利用开源模型`tinybert-6l-768d-v2`初始化,在GLUE的7个数据集上进行下游任务的蒸馏,最终模型参数量缩小1/2,预测速度提升2倍,同时保证模型精度几乎无损,其中精度可达教师模型`bert-base-uncased`的 98.90%。 |
| [OFA-BERT](./examples/model_compression/ofa/) :star2: | 基于PaddleSlim Once-For-ALL(OFA)策略对BERT在GLUE任务的下游模型进行压缩,在精度无损的情况下可减少33%参数量,达到模型小型化的提速的效果。 |
| [Distill-LSTM](./examples/model_compression/distill_lstm/) | 基于[Distilling Task-Specific Knowledge from BERT into Simple Neural Networks](https://arxiv.org/abs/1903.12136)论文策略的实现,将BERT中英文分类的下游模型知识通过蒸馏的方式迁移至LSTM的小模型结构中,取得比LSTM单独训练更好的效果。 |
#### 小样本学习 (Few-Shot Learning)
| 算法 | 简介 |
| :--------------- | ------- |
| [PET](./examples/few_shot/pet/) | 基于[Exploiting Cloze Questions for Few Shot Text Classification and Natural Language Inference](https://arxiv.org/abs/2001.07676) 论文策略实现, 基于人工知识设计 Prompt, 将下游目标任务转换为完形填空任务来充分挖掘预训练模型中的知识, 显著提升模型效果。|
| [P-Tuning](./examples/few_shot/p-tuning/) |基于[GPT Understands, Too](https://arxiv.org/pdf/2103.10385.pdf) 论文策略实现, 首次提出连续可学习的模板参数,在全参数空间对模板进行连续优化,大幅提升模型稳定性和模型效果。|
| [EFL](./examples/few_shot/efl/) | 基于[Entailment as Few-Shot Learner](https://arxiv.org/abs/2104.14690) 论文策略实现,将下游目标任务转换为蕴含任务降低模型预测空间,显著提升模型效果。|
## 交互式Notebook教程
- [使用Seq2Vec模块进行句子情感分类](https://aistudio.baidu.com/aistudio/projectdetail/1283423)
- [如何通过预训练模型Fine-tune下游任务](https://aistudio.baidu.com/aistudio/projectdetail/1294333)
- [使用BiGRU-CRF模型完成快递单信息抽取](https://aistudio.baidu.com/aistudio/projectdetail/1317771)
- [使用预训练模型ERNIE优化快递单信息抽取](https://aistudio.baidu.com/aistudio/projectdetail/1329361)
- [使用Seq2Seq模型完成自动对联](https://aistudio.baidu.com/aistudio/projectdetail/1321118)
- [使用预训练模型ERNIE-GEN实现智能写诗](https://aistudio.baidu.com/aistudio/projectdetail/1339888)
- [使用TCN网络完成新冠疫情病例数预测](https://aistudio.baidu.com/aistudio/projectdetail/1290873)
更多教程参见[PaddleNLP on AI Studio](https://aistudio.baidu.com/aistudio/personalcenter/thirdview/574995)。
## 社区贡献与技术交流
### 特殊兴趣小组
- 欢迎您加入PaddleNLP的SIG社区,贡献优秀的模型实现、公开数据集、教程与案例等。
### QQ
- 现在就加入PaddleNLP的QQ技术交流群,一起交流NLP技术吧!⬇️
<div align="center">
<img src="https://github.com/user-attachments/assets/3a58cc9f-69c7-4ccb-b6f5-73e966b8051a" width="150" height="150" />
</div>
<img src="./docs/imgs/qq.png" width="200" height="200" />
</div>
## Citation
## 版本更新
如果 PaddleNLP 对您的研究有帮助,欢迎引用
```bibtex
@misc{=paddlenlp,
title={PaddleNLP: An Easy-to-use and High Performance NLP Library},
author={PaddleNLP Contributors},
howpublished = {\url{https://github.com/PaddlePaddle/PaddleNLP}},
year={2021}
}
```
更多版本更新说明请查看[ChangeLog](./docs/changelog.md)
## Acknowledge
我们借鉴了 Hugging Face 的[Transformers](https://github.com/huggingface/transformers)🤗关于预训练模型使用的优秀设计,在此对 Hugging Face 作者及其开源社区表示感谢。
我们借鉴了Hugging Face的[Transformers](https://github.com/huggingface/transformers)🤗关于预训练模型使用的优秀设计,在此对Hugging Face作者及其开源社区表示感谢。
## License
PaddleNLP 遵循[Apache-2.0开源协议](./LICENSE)。
PaddleNLP遵循[Apache-2.0开源协议](./LICENSE)。

View File

@@ -1,167 +1,229 @@
[简体中文🀄](./README.md) | **English🌎**
English | [简体中文](./README.md)
<p align="center">
<img src="https://user-images.githubusercontent.com/1371212/175816733-8ec25eb0-9af3-4380-9218-27c154518258.png" align="middle" width="500" />
<img src="./docs/imgs/paddlenlp.png" width="718" height ="100" />
</p>
------------------------------------------------------------------------------------------
[![PyPI - PaddleNLP Version](https://img.shields.io/pypi/v/paddlenlp.svg?label=pip&logo=PyPI&logoColor=white)](https://pypi.org/project/paddlenlp/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/paddlenlp)](https://pypi.org/project/paddlenlp/)
[![PyPI Status](https://pepy.tech/badge/paddlenlp/month)](https://pepy.tech/project/paddlenlp)
![support os](https://img.shields.io/badge/os-linux%2C%20win%2C%20mac-yellow.svg)
![GitHub](https://img.shields.io/github/license/paddlepaddle/paddlenlp)
<p align="center">
<a href="https://paddlenlp.readthedocs.io/en/latest/?badge=latest"><img src="https://readthedocs.org/projects/paddlenlp/badge/?version=latest">
<a href="https://github.com/PaddlePaddle/PaddleNLP/releases"><img src="https://img.shields.io/github/v/release/PaddlePaddle/PaddleNLP?color=ffa"></a>
<a href=""><img src="https://img.shields.io/badge/python-3.7+-aff.svg"></a>
<a href=""><img src="https://img.shields.io/badge/os-linux%2C%20win%2C%20mac-pink.svg"></a>
<a href="https://github.com/PaddlePaddle/PaddleNLP/graphs/contributors"><img src="https://img.shields.io/github/contributors/PaddlePaddle/PaddleNLP?color=9ea"></a>
<a href="https://github.com/PaddlePaddle/PaddleNLP/commits"><img src="https://img.shields.io/github/commit-activity/m/PaddlePaddle/PaddleNLP?color=3af"></a>
<a href="https://pypi.org/project/paddlenlp/"><img src="https://img.shields.io/pypi/dm/paddlenlp?color=9cf"></a>
<a href="https://github.com/PaddlePaddle/PaddleNLP/issues"><img src="https://img.shields.io/github/issues/PaddlePaddle/PaddleNLP?color=9cc"></a>
<a href="https://github.com/PaddlePaddle/PaddleNLP/stargazers"><img src="https://img.shields.io/github/stars/PaddlePaddle/PaddleNLP?color=ccf"></a>
<a href="./LICENSE"><img src="https://img.shields.io/badge/license-Apache%202-dfd.svg"></a>
</p>
## News <img src="./docs/imgs/news_icon.png" width="40"/>
<h4 align="center">
<a href=#Features> Features </a> |
<a href=#Support-Models> Supported Models </a> |
<a href=#Installation> Installation </a> |
<a href=#Quick-start> Quick Start </a> |
<a href=#community> Community </a>
</h4>
* [2021-10-12] PaddleNLP 2.1 has been officially relealsed! :tada: For more information please refer to [Release Note](https://github.com/PaddlePaddle/PaddleNLP/releases/tag/v2.1.0).
**PaddleNLP** is a Large Language Model (LLM) development suite based on the PaddlePaddle deep learning framework, supporting efficient large model training, lossless compression, and high-performance inference on various hardware devices. With its **simplicity** and **ultimate performance**, PaddleNLP is dedicated to helping developers achieve efficient industrial applications of large models.
## Introduction
## News 📢
**PaddleNLP** is a powerful NLP library with **Awesome** pre-trained Transformer models and easy-to-use interface, supporting wide-range of NLP tasks from research to industrial applications.
* **2024.06.27 [PaddleNLP v3.0 Beta](https://github.com/PaddlePaddle/PaddleNLP/releases/tag/v3.0.0-beta0)**:Embrace large models and experience a complete upgrade. With a unified large model suite, we achieve full-process access to domestically produced computing chips. We fully support industrial-level application processes for large models, such as PaddlePaddle's 4D parallel configuration, efficient fine-tuning strategies, efficient alignment algorithms, and high-performance reasoning. Our developed RsLoRA+ algorithm, full checkpoint storage mechanism Unified Checkpoint, and generalized support for FastFNN and FusedQKV all contribute to the training and inference of large models. We continuously support updates to mainstream models for providing efficient solutions.
* **2024.04.24 [PaddleNLP v2.8](https://github.com/PaddlePaddle/PaddleNLP/releases/tag/v2.8.0)**:Our self-developed RsLoRA+ algorithm with extreme convergence significantly improves the convergence speed and training effectiveness of PEFT training. By introducing high-performance generation acceleration into the RLHF PPO algorithm, we have broken through the generation speed bottleneck in PPO training, achieving a significant lead in PPO training performance. We generally support multiple large model training performance optimization methods such as FastFFN and FusedQKV, making large model training faster and more stable.
* **Easy-to-Use API**
- The API is fully integrated with PaddlePaddle 2.0 high-level API system. It minimizes the number of user actions required for common use cases like data loading, text pre-processing, awesome transfomer models, and fast inference, which enables developer to deal with text problems more productively.
## Features
* **Wide-range NLP Task Support**
- PaddleNLP support NLP task from research to industrial applications, including Lexical Analysis, Text Classification, Text Matching, Text Generation, Information Extraction, Machine Translation, General Dialogue and Question Answering etc.
### <a href=#Integrated training and inference on multiple hardware platforms> 🔧 Integrated training and inference on multiple hardware platforms </a>
Our development suit supports large model training and inference on multiple hardware platforms, including NVIDIA GPUs, Kunlun XPUs, Ascend NPUs, Enflame GCUs, and Hygon DCUs. The toolkit's interface allows for quick hardware switching, significantly reducing research and development costs associated with hardware transitions.
### <a href=Efficient and easy-to-use pre-training> 🚀 Efficient and easy-to-use pre-training </a>
We support 4D high-performance training with data parallelism, sharding parallelism, tensor parallelism, and pipeline parallelism. The Trainer supports configurable distributed strategies, reducing the cost associated with complex distributed combinations. The Unified Checkpoint large model storage format supports dynamic scaling of model parameter distribution during training, thereby reducing the migration cost caused by hardware switching.
### <a href=#Efficient fine-tuning> 🤗 Efficient fine-tuning </a>
The fine-tuning algorithms are deeply integrated with zero-padding data streams and high-performance FlashMask operators, reducing invalid data padding and computation during training, and significantly improving the throughput of fine-tuning training.
### <a href=#Lossless compression and high-performance inference> 🎛️ Lossless compression and high-performance inference </a>
The high-performance inference module of the large model toolkit incorporates dynamic insertion and operator fusion strategies throughout the entire process, greatly accelerating parallel inference speed. The underlying implementation details are encapsulated, enabling out-of-the-box high-performance parallel inference capabilities.
## Documentation
For detailed documentation, visit the [PaddleNLP Documentation](https://paddlenlp.readthedocs.io/).
------------------------------------------------------------------------------------------
## Support Models
Detailed list 👉 [Supported Model List](https://github.com/PaddlePaddle/PaddleNLP/issues/8663)
* **High Performance Distributed Training**
- We provide an industrial level training pipeline for super large-scale Transformer model based on **Auto Mixed Precision** and Fleet distributed training API by PaddlePaddle, which can support customized model pre-training efficiently.
## Installation
### Prerequisites
* python >= 3.8
* paddlepaddle >= 3.0.0b0
* python >= 3.6
* paddlepaddle >= 2.2
### Pip Installation
More information about PaddlePaddle installation please refer to [PaddlePaddle's Website](https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/conda/linux-conda.html).
```shell
pip install --upgrade paddlenlp==3.0.0b3
### Python pip Installation
```
pip install --upgrade paddlenlp
```
or you can install the latest develop branch code with the following command:
## Easy-to-use API
```shell
pip install --pre --upgrade paddlenlp -f https://www.paddlepaddle.org.cn/whl/paddlenlp.html
```
### Taskflow:Off-the-shelf Industial NLP Pre-built Task
More information about PaddlePaddle installation please refer to [PaddlePaddle's Website](https://www.paddlepaddle.org.cn).
------------------------------------------------------------------------------------------
## Quick Start
### Text generation with large language model
PaddleNLP provides a convenient and easy-to-use Auto API, which can quickly load models and Tokenizers. Here, we use the `Qwen/Qwen2-0.5B` large model as an example for text generation:
Taskflow aims to provide **off-the-shelf** NLP pre-built task covering NLU and NLG scenario, in the meanwhile with extreamly fast infernece satisfying industrial applications.
```python
>>> from paddlenlp.transformers import AutoTokenizer, AutoModelForCausalLM
>>> tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-0.5B")
>>> model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-0.5B", dtype="float16")
>>> input_features = tokenizer("你好!请自我介绍一下。", return_tensors="pd")
>>> outputs = model.generate(**input_features, max_length=128)
>>> print(tokenizer.batch_decode(outputs[0], skip_special_tokens=True))
['我是一个AI语言模型,我可以回答各种问题,包括但不限于:天气、新闻、历史、文化、科学、教育、娱乐等。请问您有什么需要了解的吗?']
from paddlenlp import Taskflow
# Chinese Word Segmentation
seg = Taskflow("word_segmentation")
seg("第十四届全运会在西安举办")
>>> ['第十四届', '全运会', '在', '西安', '举办']
# POS Tagging
tag = Taskflow("pos_tagging")
tag("第十四届全运会在西安举办")
>>> [('第十四届', 'm'), ('全运会', 'nz'), ('在', 'p'), ('西安', 'LOC'), ('举办', 'v')]
# Named Entity Recognition
ner = Taskflow("ner")
ner("《孤女》是2010年九州出版社出版的小说,作者是余兼羽")
>>> [('《', 'w'), ('孤女', '作品类_实体'), ('》', 'w'), ('是', '肯定词'), ('2010年', '时间类'), ('九州出版社', '组织机构类'), ('出版', '场景事件'), ('的', '助词'), ('小说', '作品类_概念'), (',', 'w'), ('作者', '人物类_概念'), ('是', '肯定词'), ('余兼羽', '人物类_实体')]
# Dependency Parsing
ddp = Taskflow("dependency_parsing")
ddp("9月9日上午纳达尔在亚瑟·阿什球场击败俄罗斯球员梅德韦杰夫")
>>> [{'word': ['9月9日', '上午', '纳达尔', '在', '亚瑟·阿什球场', '击败', '俄罗斯', '球员', '梅德韦杰夫'], 'head': [2, 6, 6, 5, 6, 0, 8, 9, 6], 'deprel': ['ATT', 'ADV', 'SBV', 'MT', 'ADV', 'HED', 'ATT', 'ATT', 'VOB']}]
# Sentiment Analysis
senta = Taskflow("sentiment_analysis")
senta("这个产品用起来真的很流畅,我非常喜欢")
>>> [{'text': '这个产品用起来真的很流畅,我非常喜欢', 'label': 'positive', 'score': 0.9938690066337585}]
```
### Pre-training for large language model
For more usage please refer to [Taskflow Docs](./docs/model_zoo/taskflow.md)
```shell
git clone https://github.com/PaddlePaddle/PaddleNLP.git && cd PaddleNLP # if cloned or downloaded, can skip this step
mkdir -p llm/data && cd llm/data
wget https://bj.bcebos.com/paddlenlp/models/transformers/llama/data/llama_openwebtext_100k.bin
wget https://bj.bcebos.com/paddlenlp/models/transformers/llama/data/llama_openwebtext_100k.idx
cd .. # change folder to PaddleNLP/llm
python -u -m paddle.distributed.launch --gpus "0,1,2,3,4,5,6,7" run_pretrain.py ./config/llama/pretrain_argument.json
### Transformer API: Awesome Pre-trained Model Ecosystem
We provide **30** network architectures and over **100** pretrained models. Not only includes all the SOTA model like ERNIE, PLATO and SKEP released by Baidu, but also integrates most of the high quality Chinese pretrained model developed by other organizations. Use AutoModel to download pretrained mdoels of different architecture. We welcome all developers to contribute your Transformer models to PaddleNLP! 🤗
```python
from paddlenlp.transformers import *
ernie = AutoModel.from_pretrained('ernie-1.0')
ernie_gram = AutoModel.from_pretrained('ernie-gram-zh')
bert = AutoModel.from_pretrained('bert-wwm-chinese')
albert = AutoModel.from_pretrained('albert-chinese-tiny')
roberta = AutoModel.from_pretrained('roberta-wwm-ext')
electra = AutoModel.from_pretrained('chinese-electra-small')
gpt = AutoModelForPretraining.from_pretrained('gpt-cpm-large-cn')
```
### SFT finetuning forlarge language model
PaddleNLP also provides unified API experience for NLP task like semantic representation, text classification, sentence matching, sequence labeling, question answering, etc.
```shell
git clone https://github.com/PaddlePaddle/PaddleNLP.git && cd PaddleNLP # if cloned or downloaded, can skip this step
mkdir -p llm/data && cd llm/data
wget https://bj.bcebos.com/paddlenlp/datasets/examples/AdvertiseGen.tar.gz && tar -zxvf AdvertiseGen.tar.gz
cd .. # change folder to PaddleNLP/llm
python -u -m paddle.distributed.launch --gpus "0,1,2,3,4,5,6,7" run_finetune.py ./config/llama/sft_argument.json
```python
import paddle
from paddlenlp.transformers import *
tokenizer = AutoTokenizer.from_pretrained('ernie-1.0')
text = tokenizer('natural language understanding')
# Semantic Representation
model = AutoModel.from_pretrained('ernie-1.0')
sequence_output, pooled_output = model(input_ids=paddle.to_tensor([text['input_ids']]))
# Text Classificaiton and Matching
model = AutoModelForSequenceClassification.from_pretrained('ernie-1.0')
# Sequence Labeling
model = AutoModelForTokenClassification.from_pretrained('ernie-1.0')
# Question Answering
model = AutoModelForQuestionAnswering.from_pretrained('ernie-1.0')
```
For more steps in the entire large model process, please refer to the[Large Model Full-Process Suite](./llm).
For more pretrained model usage, please refer to [Transformer API](./docs/model_zoo/transformers.rst)
For more PaddleNLP content, please refer to:
### Dataset API: Abundant Dataset Integration and Quick Loading
* [Model Library](./slm/model_zoo),which includes end-to-end usage of high-quality pre-trained models.
* [Multi-scenario Examples](./slm/examples),to understand how to use PaddleNLP to solve various NLP technical problems, including basic techniques, system applications, and extended applications.
* [Interactive Tutorial](https://aistudio.baidu.com/aistudio/personalcenter/thirdview/574995),to quickly learn PaddleNLP on the free computing platform AI Studio.
```python
from paddlenlp.datasets import load_dataset
------------------------------------------------------------------------------------------
train_ds, dev_ds, test_ds = load_dataset("chnsenticorp", splits=["train", "dev", "test"])
```
For more dataset API usage please refer to [Dataset API](./docs/datasets.md).
### Embedding API: Quick Loading for Word Embedding
```python
from paddlenlp.embeddings import TokenEmbedding
wordemb = TokenEmbedding("fasttext.wiki-news.target.word-word.dim300.en")
wordemb.cosine_sim("king", "queen")
>>> 0.77053076
wordemb.cosine_sim("apple", "rail")
>>> 0.29207364
```
For more `TokenEmbedding` usage, please refer to [Embedding API](./docs/model_zoo/embeddings.md)
### More API Usage
- [Transformer API](./docs/model_zoo/transformers.rst)
- [Data API](./docs/data.md)
- [Dataset API](./docs/datasets.md)
- [Embedding API](./docs/model_zoo/embeddings.md)
- [Metrics API](./docs/metrics.md)
Please find more API Reference from our [readthedocs](https://paddlenlp.readthedocs.io/).
## Wide-range NLP Task Support
PaddleNLP provides rich application examples covering mainstream NLP task to help developers accelerate problem solving.
### NLP Basic Technique
- [Word Embedding](./examples/word_embedding/)
- [Lexical Analysis](./examples/lexical_analysis/)
- [Dependency Parsing](./examples/dependency_parsing/)
- [Language Model](./examples/language_model/)
- [Semantic Parsing (Text to SQL)](./examples/text_to_sql):star:
- [Text Classification](./examples/text_classification/)
- [Text Matching](./examples/text_matching/)
- [Text Generation](./examples/text_generation/)
- [Text Correction](./examples/text_correction/):star:
- [Semantic Indexing](./examples/semantic_indexing/)
- [Information Extraction](./examples/information_extraction/)
### NLP System
- [Sentiment Analysis](./examples/sentiment_analysis/):star2:
- [General Dialogue System](./examples/dialogue/)
- [Machine Translation](./examples/machine_translation/)
- [Simultaneous Translation](././examples/simultaneous_translation/)
- [Machine Reading Comprehension](./examples/machine_reading_comprehension/)
### NLP Extented Applications
- [Few-shot Learning](./examples/few_shot/):star2:
- [Text Knowledge Mining](./examples/text_to_knowledge/):star2:
- [Model Compression](./examples/model_compression/)
- [Text Graph Learning](./examples/text_graph/erniesage/)
- [Time Series Prediction](./examples/time_series/)
## Tutorials
Please refer to our official AI Studio account for more interactive tutorials: [PaddleNLP on AI Studio](https://aistudio.baidu.com/aistudio/personalcenter/thirdview/574995)
* [What's Seq2Vec?](https://aistudio.baidu.com/aistudio/projectdetail/1283423) shows how to use simple API to finish LSTM model and solve sentiment analysis task.
* [Sentiment Analysis with ERNIE](https://aistudio.baidu.com/aistudio/projectdetail/1294333) shows how to exploit the pretrained ERNIE to solve sentiment analysis problem.
* [Waybill Information Extraction with BiGRU-CRF Model](https://aistudio.baidu.com/aistudio/projectdetail/1317771) shows how to make use of Bi-GRU plus CRF to finish information extraction task.
* [Waybill Information Extraction with ERNIE](https://aistudio.baidu.com/aistudio/projectdetail/1329361) shows how to use ERNIE, the Chinese pre-trained model improve information extraction performance.
* [Use TCN Model to predict COVID-19 confirmed cases](https://aistudio.baidu.com/aistudio/projectdetail/1290873)
## Community
### Slack
### Special Interest Group (SIG)
Welcome to join [PaddleNLP SIG](https://iwenjuan.baidu.com/?code=bkypg8) for contribution, eg. Dataset, Models and Toolkit.
### Slack
To connect with other users and contributors, welcome to join our [Slack channel](https://paddlenlp.slack.com/).
### WeChat
Scan the QR code below with your Wechat⬇️. You can access to official technical exchange group. Look forward to your participation.
### QQ
Join our QQ Technical Group for technical exchange right now! ⬇️
<div align="center">
<img src="https://github.com/user-attachments/assets/3a58cc9f-69c7-4ccb-b6f5-73e966b8051a" width="150" height="150" />
<img src="./docs/imgs/qq.png" width="200" height="200" />
</div>
## Citation
## ChangeLog
If you find PaddleNLP useful in your research, please consider citing
```bibtext
@misc{=paddlenlp,
title={PaddleNLP: An Easy-to-use and High Performance NLP Library},
author={PaddleNLP Contributors},
howpublished = {\url{https://github.com/PaddlePaddle/PaddleNLP}},
year={2021}
}
```
For more details about our release, please refer to [ChangeLog](./docs/changelog.md)
## Acknowledge
We have borrowed from Hugging Face's [Transformers](https://github.com/huggingface/transformers)🤗 excellent design on pretrained models usage, and we would like to express our gratitude to the authors of Hugging Face and its open source community.
We have borrowed from Hugging Face's [Transformer](https://github.com/huggingface/transformers)🤗 excellent design on pretrained models usage, and we would like to express our gratitude to the authors of Hugging Face and its open source community.
## License

View File

@@ -0,0 +1,63 @@
import paddle
import paddlenlp.transformers as ppnlp
import torch
import transformers as hgnlp
def compare():
bert = ppnlp.BertModel.from_pretrained('huhuiwen/mengzi-bert-base')
tokenizer = ppnlp.BertTokenizer.from_pretrained('huhuiwen/mengzi-bert-base')
inputs = tokenizer("Welcome to use PaddleNLP!")['input_ids']
pt_inputs = torch.tensor([inputs])
pd_inputs = paddle.to_tensor([inputs])
pt_model = hgnlp.BertForMaskedLM.from_pretrained(
'Langboat/mengzi-bert-base')
pt_model.eval()
with torch.no_grad():
pt_outputs = pt_model(pt_inputs)[0]
pd_model = ppnlp.BertForMaskedLM.from_pretrained(
'huhuiwen/mengzi-bert-base')
pd_model.eval()
with paddle.no_grad():
pd_outputs = torch.from_numpy(pd_model(pd_inputs).numpy())
print(f"huggingface {'mengzi-bert-base'} vs paddle {'mengzi-bert-base'}")
print("mean difference:", (pt_outputs - pd_outputs).abs().mean())
print("max difference:", (pt_outputs - pd_outputs).abs().max())
def compare_fin():
bert = ppnlp.BertModel.from_pretrained('huhuiwen/mengzi-bert-base-fin')
tokenizer = ppnlp.BertTokenizer.from_pretrained(
'huhuiwen/mengzi-bert-base-fin')
inputs = tokenizer("Welcome to use PaddleNLP!")['input_ids']
pt_inputs = torch.tensor([inputs])
pd_inputs = paddle.to_tensor([inputs])
pt_model = hgnlp.BertForMaskedLM.from_pretrained(
'Langboat/mengzi-bert-base-fin')
pt_model.eval()
with torch.no_grad():
pt_outputs = pt_model(pt_inputs)[0]
pd_model = ppnlp.BertForMaskedLM.from_pretrained(
'huhuiwen/mengzi-bert-base-fin')
pd_model.eval()
with paddle.no_grad():
pd_outputs = torch.from_numpy(pd_model(pd_inputs).numpy())
print(
f"huggingface {'mengzi-bert-base-fin'} vs paddle {'mengzi-bert-base-fin'}"
)
print("mean difference:", (pt_outputs - pd_outputs).abs().mean())
print("max difference:", (pt_outputs - pd_outputs).abs().max())
if __name__ == "__main__":
compare()
compare_fin()

View File

@@ -0,0 +1,83 @@
# Copyright (c) 2021 PaddlePaddle Authors. 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 collections import OrderedDict
from paddlenlp.transformers import *
import argparse
import torch
import paddle
huggingface_to_paddle = {
"embeddings.LayerNorm": "embeddings.layer_norm",
"encoder.layer": "encoder.layers",
"attention.self.query": "self_attn.q_proj",
"attention.self.key": "self_attn.k_proj",
"attention.self.value": "self_attn.v_proj",
"attention.output.dense": "self_attn.out_proj",
"intermediate.dense": "linear1",
"output.dense": "linear2",
"attention.output.LayerNorm": "norm1",
"output.LayerNorm": "norm2",
"predictions.decoder.": "predictions.decoder_",
"predictions.transform.dense": "predictions.transform",
"predictions.transform.LayerNorm": "predictions.layer_norm",
}
def convert_pytorch_checkpoint_to_paddle(pytorch_checkpoint_path,
paddle_dump_path):
pytorch_state_dict = torch.load(pytorch_checkpoint_path, map_location="cpu")
paddle_state_dict = OrderedDict()
for k, v in pytorch_state_dict.items():
is_transpose = False
if k[-7:] == ".weight":
if ".embeddings." not in k and ".LayerNorm." not in k:
if v.ndim == 2:
v = v.transpose(0, 1)
is_transpose = True
oldk = k
for huggingface_name, paddle_name in huggingface_to_paddle.items():
k = k.replace(huggingface_name, paddle_name)
if "bert." not in k and "cls." not in k and "classifier" not in k:
k = "bert." + k
print(f"Converting: {oldk} => {k} | is_transpose {is_transpose}")
if v.dtype == torch.float16:
v = torch.tensor(v.data, dtype=torch.float32)
paddle_state_dict[k] = v.data.numpy()
paddle.save(paddle_state_dict, paddle_dump_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--pytorch_checkpoint_path",
default="/mengzi-bert-base/pytorch_model.bin",
type=str,
required=False,
help="Path to the Pytorch checkpoint path.")
parser.add_argument(
"--paddle_dump_path",
default="/mengzi-bert-base-pd/model_state.pdparams",
type=str,
required=False,
help="Path to the output Paddle model.")
args = parser.parse_args()
convert_pytorch_checkpoint_to_paddle(args.pytorch_checkpoint_path,
args.paddle_dump_path)

View File

@@ -0,0 +1,34 @@
# 详细介绍
**介绍**:基于mengzi-bert-base,继续在20G金融语料上训练,
包括金融新闻和研究报告。预训练目标有三个,掩码语言建模 (MLM),句子顺序预测(SOP)
和词性标注(POS)。
**模型结构**: **`BertForMakedLM`**。
**适用下游任务**:**金融领域的自然语言理解类任务**,如:文本分类、实体识别、关系抽取、阅读理解等。
**训练数据**:20G金融语料,包括金融新闻和研究报告。
#使用示例:
```python
from paddlenlp import BertForMaskedLM, BertTokenizer
tokenizer = BertTokenizer.from_pretrained('Langboat/mengzi-bert-base-fin')
model = BertForMaskedLM.from_pretrained('Langboat/mengzi-bert-base-fin')
tokens = ['[CLS]', '[MASK]','是', '商', '品','交','换','的','产','物','。', '[SEP]']
masked_ids = paddle.to_tensor([tokenizer.convert_tokens_to_ids(tokens)])
segment_ids = paddle.to_tensor([[0] * len(tokens)])
outputs = model(masked_ids, token_type_ids=segment_ids)
prediction_scores = outputs
prediction_index = paddle.argmax(prediction_scores[0, 3]).item()
predicted_token = tokenizer.convert_ids_to_tokens([prediction_index])[0]
print(tokens)
#['[CLS]', '[MASK]','是', '商', '品','交','换','的','产','物','。', '[SEP]']
print(predicted_token)
#它
```
# 权重来源
https://huggingface.co/Langboat/mengzi-bert-base-fin

View File

@@ -0,0 +1,6 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/huhuiwen/mengzi-bert-base-fin/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/huhuiwen/mengzi-bert-base-fin/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/huhuiwen/mengzi-bert-base-fin/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/huhuiwen/mengzi-bert-base-fin/vocab.txt"
}

View File

@@ -0,0 +1,34 @@
# 详细介绍
**介绍**:Mengzi-bert-base 是一个轻量级的中文语言BERT模型,
在300G中文语料库继续训练,预训练目标有三个,掩码语言建模 (MLM),句子顺序预测(SOP)
和词性标注(POS)。
**模型结构**: **`BertForMakedLM`**。
**适用下游任务**:**自然语言理解类任务**,如:文本分类、实体识别、关系抽取、阅读理解等。
**训练数据**:300G中文语料库数据。
#使用示例:
```python
from paddlenlp import BertForMaskedLM, BertTokenizer
tokenizer = BertTokenizer.from_pretrained('Langboat/mengzi-bert-base')
model = BertForMaskedLM.from_pretrained('Langboat/mengzi-bert-base')
tokens = ['[CLS]', '我', '的', '[MASK]','很', '可', '爱','。', '[SEP]']
masked_ids = paddle.to_tensor([tokenizer.convert_tokens_to_ids(tokens)])
segment_ids = paddle.to_tensor([[0] * len(tokens)])
outputs = model(masked_ids, token_type_ids=segment_ids)
prediction_scores = outputs
prediction_index = paddle.argmax(prediction_scores[0, 3]).item()
predicted_token = tokenizer.convert_ids_to_tokens([prediction_index])[0]
print(tokens)
#['[CLS]', '我', '的', '[MASK]', '很', '可', '爱', '。', '[SEP]']
print(predicted_token)
#猫
```
# 权重来源
https://huggingface.co/Langboat/mengzi-bert-base

View File

@@ -0,0 +1,6 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/huhuiwen/mengzi-bert-base/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/huhuiwen/mengzi-bert-base/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/huhuiwen/mengzi-bert-base/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/huhuiwen/mengzi-bert-base/vocab.txt"
}

View File

@@ -0,0 +1,64 @@
# BERT base Japanese (character tokenization, whole word masking enabled)
This is a [BERT](https://github.com/google-research/bert) model pretrained on texts in the Japanese language.
This version of the model processes input texts with word-level tokenization based on the IPA dictionary, followed by character-level tokenization.
Additionally, the model is trained with the whole word masking enabled for the masked language modeling (MLM) objective.
The codes for the pretraining are available at [cl-tohoku/bert-japanese](https://github.com/cl-tohoku/bert-japanese/tree/v1.0).
## Model architecture
The model architecture is the same as the original BERT base model; 12 layers, 768 dimensions of hidden states, and 12 attention heads.
## Training Data
The model is trained on Japanese Wikipedia as of September 1, 2019.
To generate the training corpus, [WikiExtractor](https://github.com/attardi/wikiextractor) is used to extract plain texts from a dump file of Wikipedia articles.
The text files used for the training are 2.6GB in size, consisting of approximately 17M sentences.
## Tokenization
The texts are first tokenized by [MeCab](https://taku910.github.io/mecab/) morphological parser with the IPA dictionary and then split into characters.
The vocabulary size is 4000.
## Training
The model is trained with the same configuration as the original BERT; 512 tokens per instance, 256 instances per batch, and 1M training steps.
For the training of the MLM (masked language modeling) objective, we introduced the **Whole Word Masking** in which all of the subword tokens corresponding to a single word (tokenized by MeCab) are masked at once.
## Licenses
The pretrained models are distributed under the terms of the [Creative Commons Attribution-ShareAlike 3.0](https://creativecommons.org/licenses/by-sa/3.0/).
## Acknowledgments
For training models, we used Cloud TPUs provided by [TensorFlow Research Cloud](https://www.tensorflow.org/tfrc/) program.
## Usage
```python
import paddle
from paddlenlp.transformers import BertJapaneseTokenizer, BertForMaskedLM
path = "iverxin/bert-base-japanese-char-whole-word-masking/"
tokenizer = BertJapaneseTokenizer.from_pretrained(path)
model = BertForMaskedLM.from_pretrained(path)
text1 = "こんにちは"
model.eval()
inputs = tokenizer(text1)
inputs = {k: paddle.to_tensor([v]) for (k, v) in inputs.items()}
output = model(**inputs)
print(output.shape)
# [1, 5, 32000]
```
## Weights source
https://huggingface.co/cl-tohoku/bert-base-japanese-char-whole-word-masking

View File

@@ -0,0 +1,6 @@
{
"model_config_file":"https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese-char-whole-word-masking/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese-char-whole-word-masking/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese-char-whole-word-masking/tokenizer_config.pdparams",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese-char-whole-word-masking/vocab.txt"
}

View File

@@ -0,0 +1,60 @@
# BERT base Japanese (character tokenization)
This is a [BERT](https://github.com/google-research/bert) model pretrained on texts in the Japanese language.
This version of the model processes input texts with word-level tokenization based on the IPA dictionary, followed by character-level tokenization.
The codes for the pretraining are available at [cl-tohoku/bert-japanese](https://github.com/cl-tohoku/bert-japanese/tree/v1.0).
## Model architecture
The model architecture is the same as the original BERT base model; 12 layers, 768 dimensions of hidden states, and 12 attention heads.
## Training Data
The model is trained on Japanese Wikipedia as of September 1, 2019.
To generate the training corpus, [WikiExtractor](https://github.com/attardi/wikiextractor) is used to extract plain texts from a dump file of Wikipedia articles.
The text files used for the training are 2.6GB in size, consisting of approximately 17M sentences.
## Tokenization
The texts are first tokenized by [MeCab](https://taku910.github.io/mecab/) morphological parser with the IPA dictionary and then split into characters.
The vocabulary size is 4000.
## Training
The model is trained with the same configuration as the original BERT; 512 tokens per instance, 256 instances per batch, and 1M training steps.
## Licenses
The pretrained models are distributed under the terms of the [Creative Commons Attribution-ShareAlike 3.0](https://creativecommons.org/licenses/by-sa/3.0/).
## Acknowledgments
For training models, we used Cloud TPUs provided by [TensorFlow Research Cloud](https://www.tensorflow.org/tfrc/) program.
## Usage
```python
import paddle
from paddlenlp.transformers import BertJapaneseTokenizer, BertForMaskedLM, MecabTokenizer
path = "iverxin/bert-base-japanese-char/"
tokenizer = BertJapaneseTokenizer.from_pretrained(path)
model = BertForMaskedLM.from_pretrained(path)
text1 = "こんにちは"
text2 = "櫓を飛ばす"
model.eval()
inputs = tokenizer(text1)
inputs = {k: paddle.to_tensor([v]) for (k, v) in inputs.items()}
output = model(**inputs)
print(output.shape)
```
## Weights source
https://huggingface.co/cl-tohoku/bert-base-japanese-char

View File

@@ -0,0 +1,6 @@
{
"model_config_file":"https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese-char/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese-char/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese-char/tokenizer_config.pdparams",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese-char/vocab.txt"
}

View File

@@ -0,0 +1,63 @@
# BERT base Japanese (IPA dictionary, whole word masking enabled)
This is a [BERT](https://github.com/google-research/bert) model pretrained on texts in the Japanese language.
This version of the model processes input texts with word-level tokenization based on the IPA dictionary, followed by the WordPiece subword tokenization.
Additionally, the model is trained with the whole word masking enabled for the masked language modeling (MLM) objective.
The codes for the pretraining are available at [cl-tohoku/bert-japanese](https://github.com/cl-tohoku/bert-japanese/tree/v1.0).
## Model architecture
The model architecture is the same as the original BERT base model; 12 layers, 768 dimensions of hidden states, and 12 attention heads.
## Training Data
The model is trained on Japanese Wikipedia as of September 1, 2019.
To generate the training corpus, [WikiExtractor](https://github.com/attardi/wikiextractor) is used to extract plain texts from a dump file of Wikipedia articles.
The text files used for the training are 2.6GB in size, consisting of approximately 17M sentences.
## Tokenization
The texts are first tokenized by [MeCab](https://taku910.github.io/mecab/) morphological parser with the IPA dictionary and then split into subwords by the WordPiece algorithm.
The vocabulary size is 32000.
## Training
The model is trained with the same configuration as the original BERT; 512 tokens per instance, 256 instances per batch, and 1M training steps.
For the training of the MLM (masked language modeling) objective, we introduced the **Whole Word Masking** in which all of the subword tokens corresponding to a single word (tokenized by MeCab) are masked at once.
## Licenses
The pretrained models are distributed under the terms of the [Creative Commons Attribution-ShareAlike 3.0](https://creativecommons.org/licenses/by-sa/3.0/).
## Acknowledgments
For training models, we used Cloud TPUs provided by [TensorFlow Research Cloud](https://www.tensorflow.org/tfrc/) program.
## Usage
```python
import paddle
from paddlenlp.transformers import BertJapaneseTokenizer, BertForMaskedLM
path = "iverxin/bert-base-japanese-whole-word-masking/"
tokenizer = BertJapaneseTokenizer.from_pretrained(path)
model = BertForMaskedLM.from_pretrained(path)
text1 = "こんにちは"
model.eval()
inputs = tokenizer(text1)
inputs = {k: paddle.to_tensor([v]) for (k, v) in inputs.items()}
output = model(**inputs)
print(output.shape)
```
## Weights source
https://huggingface.co/cl-tohoku/bert-base-japanese-whole-word-masking

View File

@@ -0,0 +1,6 @@
{
"model_config_file":"https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese-whole-word-masking/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese-whole-word-masking/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese-whole-word-masking/tokenizer_config.pdparams",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese-whole-word-masking/vocab.txt"
}

View File

@@ -0,0 +1,59 @@
# BERT base Japanese (IPA dictionary)
This is a [BERT](https://github.com/google-research/bert) model pretrained on texts in the Japanese language.
This version of the model processes input texts with word-level tokenization based on the IPA dictionary, followed by the WordPiece subword tokenization.
The codes for the pretraining are available at [cl-tohoku/bert-japanese](https://github.com/cl-tohoku/bert-japanese/tree/v1.0).
## Model architecture
The model architecture is the same as the original BERT base model; 12 layers, 768 dimensions of hidden states, and 12 attention heads.
## Training Data
The model is trained on Japanese Wikipedia as of September 1, 2019.
To generate the training corpus, [WikiExtractor](https://github.com/attardi/wikiextractor) is used to extract plain texts from a dump file of Wikipedia articles.
The text files used for the training are 2.6GB in size, consisting of approximately 17M sentences.
## Tokenization
The texts are first tokenized by [MeCab](https://taku910.github.io/mecab/) morphological parser with the IPA dictionary and then split into subwords by the WordPiece algorithm.
The vocabulary size is 32000.
## Training
The model is trained with the same configuration as the original BERT; 512 tokens per instance, 256 instances per batch, and 1M training steps.
## Licenses
The pretrained models are distributed under the terms of the [Creative Commons Attribution-ShareAlike 3.0](https://creativecommons.org/licenses/by-sa/3.0/).
## Acknowledgments
For training models, we used Cloud TPUs provided by [TensorFlow Research Cloud](https://www.tensorflow.org/tfrc/) program.
## Usage
```python
import paddle
from paddlenlp.transformers import BertJapaneseTokenizer, BertForMaskedLM
path = "iverxin/bert-base-japanese/"
tokenizer = BertJapaneseTokenizer.from_pretrained(path)
model = BertForMaskedLM.from_pretrained(path)
text1 = "こんにちは"
model.eval()
inputs = tokenizer(text1)
inputs = {k: paddle.to_tensor([v]) for (k, v) in inputs.items()}
output = model(**inputs)
print(output.shape)
```
## Weights source
https://huggingface.co/cl-tohoku/bert-base-japanese

View File

@@ -0,0 +1,6 @@
{
"model_config_file":"https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese/tokenizer_config.pdparams",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/iverxin/bert-base-japanese/vocab.txt"
}

View File

@@ -0,0 +1,663 @@
import paddle
import paddlenlp.transformers as ppnlp
import torch
import transformers as hgnlp
def compare_math():
text = "students apply these new understandings as they reason about and perform decimal [MASK] through the hundredths place."
# ppnlp
path = "junnyu/tbs17-MathBERT"
model = ppnlp.BertForPretraining.from_pretrained(path)
tokenizer = ppnlp.BertTokenizer.from_pretrained(path)
model.eval()
text = "students apply these new understandings as they reason about and perform decimal [MASK] through the hundredths place."
tokens = ["[CLS]"]
text_list = text.split("[MASK]")
for i, t in enumerate(text_list):
tokens.extend(tokenizer.tokenize(t))
if i == len(text_list) - 1:
tokens.extend(["[SEP]"])
else:
tokens.extend(["[MASK]"])
input_ids_list = tokenizer.convert_tokens_to_ids(tokens)
input_ids = paddle.to_tensor([input_ids_list])
with paddle.no_grad():
pd_outputs = model(input_ids)[0][0]
pd_outputs_sentence = "paddle: "
for i, id in enumerate(input_ids_list):
if id == tokenizer.convert_tokens_to_ids(["[MASK]"])[0]:
scores, index = paddle.nn.functional.softmax(pd_outputs[i],
-1).topk(5)
tokens = tokenizer.convert_ids_to_tokens(index.tolist())
outputs = []
for score, tk in zip(scores.tolist(), tokens):
outputs.append(f"{tk}={score}")
pd_outputs_sentence += "[" + "||".join(outputs) + "]" + " "
else:
pd_outputs_sentence += "".join(
tokenizer.convert_ids_to_tokens(
[id], skip_special_tokens=True)) + " "
print(pd_outputs_sentence)
#paddle: students apply these new understanding ##s as they reason about and perform decimal [numbers=0.8327996134757996||##s=0.0865364819765091||operations=0.0313422717154026||placement=0.019931407645344734||places=0.01254698634147644] through the hundred ##ths place .
# transformers
path = "tbs17/MathBERT"
model = hgnlp.BertForPreTraining.from_pretrained(path)
tokenizer = hgnlp.BertTokenizer.from_pretrained(path)
model.eval()
inputs = tokenizer(text, return_tensors="pt")
with torch.no_grad():
pt_outputs = model(**inputs).prediction_logits[0]
pt_outputs_sentence = "pytorch: "
for i, id in enumerate(inputs["input_ids"][0].tolist()):
if id == tokenizer.convert_tokens_to_ids(["[MASK]"])[0]:
scores, index = torch.nn.functional.softmax(pt_outputs[i],
-1).topk(5)
tokens = tokenizer.convert_ids_to_tokens(index.tolist())
outputs = []
for score, tk in zip(scores.tolist(), tokens):
outputs.append(f"{tk}={score}")
pt_outputs_sentence += "[" + "||".join(outputs) + "]" + " "
else:
pt_outputs_sentence += "".join(
tokenizer.convert_ids_to_tokens(
[id], skip_special_tokens=True)) + " "
print(pt_outputs_sentence)
# pytorch: students apply these new understanding ##s as they reason about and perform decimal [numbers=0.8328049778938293||##s=0.0865367129445076||operations=0.03134247660636902||placement=0.019931575283408165||places=0.012546995654702187] through the hundred ##ths place .
def compare_nlptown():
text = "I like you. I love you"
id2label = {
0: "1 star",
1: "2 stars",
2: "3 stars",
3: "4 stars",
4: "5 stars"
}
# ppnlp
path = "junnyu/nlptown-bert-base-multilingual-uncased-sentiment"
model = ppnlp.BertForSequenceClassification.from_pretrained(path)
model.eval()
tokenizer = ppnlp.BertTokenizer.from_pretrained(path)
inputs = {
k: paddle.to_tensor(
v, dtype="int64").unsqueeze(0)
for k, v in tokenizer(text).items()
}
with paddle.no_grad():
score = paddle.nn.functional.softmax(model(**inputs), axis=-1)
for i, s in enumerate(score[0].tolist()):
label = id2label[i]
print(f"{label} score {s}")
# 1 star score 0.0021950288210064173
# 2 stars score 0.0022533712908625603
# 3 stars score 0.015475980937480927
# 4 stars score 0.1935628354549408
# 5 stars score 0.7865128517150879
# transformers
path = "nlptown/bert-base-multilingual-uncased-sentiment"
model = hgnlp.BertForSequenceClassification.from_pretrained(path)
model.eval()
tokenizer = hgnlp.BertTokenizer.from_pretrained(path)
inputs = tokenizer(text, return_tensors="pt")
with torch.no_grad():
score = torch.nn.functional.softmax(model(**inputs).logits, dim=-1)
for i, s in enumerate(score[0].tolist()):
label = id2label[i]
print(f"{label} score {s}")
# 1 star score 0.00219502835534513
# 2 stars score 0.0022533696610480547
# 3 stars score 0.01547597348690033
# 4 stars score 0.19356288015842438
# 5 stars score 0.7865127325057983
def compare_ckiplab_ws():
text = "傅達仁今將執行安樂死,卻突然爆出自己20年前遭緯來體育台封殺,他不懂自己哪裡得罪到電視台。"
id2label = {"0": "B", "1": "I"}
# ppnlp
path = "junnyu/ckiplab-bert-base-chinese-ws"
model = ppnlp.BertForTokenClassification.from_pretrained(path)
model.eval()
tokenizer = ppnlp.BertTokenizer.from_pretrained(path)
tokenized_text = tokenizer.tokenize(text)
inputs = {
k: paddle.to_tensor(
v, dtype="int64").unsqueeze(0)
for k, v in tokenizer(text).items()
}
with paddle.no_grad():
score = paddle.nn.functional.softmax(model(**inputs), axis=-1)
for t, s in zip(tokenized_text, score[0][1:-1]):
index = paddle.argmax(s).item()
label = id2label[str(index)]
print(f"{label} {t} score {s[index].item()}")
# B 傅 score 0.9999865293502808
# I 達 score 0.999922513961792
# I 仁 score 0.9999332427978516
# B 今 score 0.9999370574951172
# B 將 score 0.9983423948287964
# B 執 score 0.9999731779098511
# I 行 score 0.9999544620513916
# B 安 score 0.9999713897705078
# I 樂 score 0.9999532699584961
# I 死 score 0.9998632669448853
# B , score 0.9999871253967285
# B 卻 score 0.9999560117721558
# B 突 score 0.9999818801879883
# I 然 score 0.9999614953994751
# B 爆 score 0.9999759197235107
# I 出 score 0.9994433522224426
# B 自 score 0.9999866485595703
# I 己 score 0.9999630451202393
# B 20 score 0.9999810457229614
# B 年 score 0.9974608421325684
# B 前 score 0.8930220603942871
# B 遭 score 0.9999674558639526
# B 緯 score 0.999970555305481
# I 來 score 0.9999680519104004
# B 體 score 0.9997956156730652
# I 育 score 0.9999778270721436
# I 台 score 0.9980663657188416
# B 封 score 0.999984860420227
# I 殺 score 0.999974250793457
# B , score 0.9999891519546509
# B 他 score 0.999988317489624
# B 不 score 0.9999889135360718
# B 懂 score 0.9997660517692566
# B 自 score 0.9999877214431763
# I 己 score 0.9999549388885498
# B 哪 score 0.9999915361404419
# I 裡 score 0.9980868101119995
# B 得 score 0.9999058246612549
# I 罪 score 0.9916028380393982
# I 到 score 0.8443355560302734
# B 電 score 0.9999363422393799
# I 視 score 0.9999769926071167
# I 台 score 0.999947190284729
# B 。 score 0.9999719858169556
# ppnlp
path = "ckiplab/bert-base-chinese-ws"
model = hgnlp.BertForTokenClassification.from_pretrained(path)
model.eval()
tokenizer = hgnlp.BertTokenizer.from_pretrained(path)
inputs = tokenizer(text, return_tensors="pt")
tokenized_text = tokenizer.tokenize(text)
with torch.no_grad():
score = torch.nn.functional.softmax(model(**inputs).logits, dim=-1)
for t, s in zip(tokenized_text, score[0][1:-1]):
index = torch.argmax(s).item()
label = id2label[str(index)]
print(f"{label} {t} score {s[index].item()}")
# B 傅 score 0.9999865293502808
# I 達 score 0.999922513961792
# I 仁 score 0.9999332427978516
# B 今 score 0.9999370574951172
# B 將 score 0.9983423948287964
# B 執 score 0.9999731779098511
# I 行 score 0.9999544620513916
# B 安 score 0.9999713897705078
# I 樂 score 0.9999532699584961
# I 死 score 0.9998632669448853
# B , score 0.9999871253967285
# B 卻 score 0.9999560117721558
# B 突 score 0.9999818801879883
# I 然 score 0.9999614953994751
# B 爆 score 0.9999759197235107
# I 出 score 0.9994433522224426
# B 自 score 0.9999866485595703
# I 己 score 0.9999630451202393
# B 20 score 0.9999810457229614
# B 年 score 0.9974608421325684
# B 前 score 0.8930219411849976
# B 遭 score 0.9999674558639526
# B 緯 score 0.999970555305481
# I 來 score 0.9999680519104004
# B 體 score 0.9997956156730652
# I 育 score 0.9999778270721436
# I 台 score 0.9980663657188416
# B 封 score 0.999984860420227
# I 殺 score 0.999974250793457
# B , score 0.9999891519546509
# B 他 score 0.999988317489624
# B 不 score 0.9999889135360718
# B 懂 score 0.9997660517692566
# B 自 score 0.9999877214431763
# I 己 score 0.9999549388885498
# B 哪 score 0.9999915361404419
# I 裡 score 0.9980868101119995
# B 得 score 0.9999058246612549
# I 罪 score 0.9916029572486877
# I 到 score 0.8443354964256287
# B 電 score 0.9999363422393799
# I 視 score 0.9999769926071167
# I 台 score 0.999947190284729
# B 。 score 0.9999719858169556
def compare_ckiplab_pos():
text = "傅達仁今將執行安樂死,卻突然爆出自己20年前遭緯來體育台封殺,他不懂自己哪裡得罪到電視台。"
id2label = {
"0": "A",
"1": "Caa",
"2": "Cab",
"3": "Cba",
"4": "Cbb",
"5": "D",
"6": "Da",
"7": "Dfa",
"8": "Dfb",
"9": "Di",
"10": "Dk",
"11": "DM",
"12": "I",
"13": "Na",
"14": "Nb",
"15": "Nc",
"16": "Ncd",
"17": "Nd",
"18": "Nep",
"19": "Neqa",
"20": "Neqb",
"21": "Nes",
"22": "Neu",
"23": "Nf",
"24": "Ng",
"25": "Nh",
"26": "Nv",
"27": "P",
"28": "T",
"29": "VA",
"30": "VAC",
"31": "VB",
"32": "VC",
"33": "VCL",
"34": "VD",
"35": "VF",
"36": "VE",
"37": "VG",
"38": "VH",
"39": "VHC",
"40": "VI",
"41": "VJ",
"42": "VK",
"43": "VL",
"44": "V_2",
"45": "DE",
"46": "SHI",
"47": "FW",
"48": "COLONCATEGORY",
"49": "COMMACATEGORY",
"50": "DASHCATEGORY",
"51": "DOTCATEGORY",
"52": "ETCCATEGORY",
"53": "EXCLAMATIONCATEGORY",
"54": "PARENTHESISCATEGORY",
"55": "PAUSECATEGORY",
"56": "PERIODCATEGORY",
"57": "QUESTIONCATEGORY",
"58": "SEMICOLONCATEGORY",
"59": "SPCHANGECATEGORY"
}
# ppnlp
path = "junnyu/ckiplab-bert-base-chinese-pos"
model = ppnlp.BertForTokenClassification.from_pretrained(path)
model.eval()
tokenizer = ppnlp.BertTokenizer.from_pretrained(path)
tokenized_text = tokenizer.tokenize(text)
inputs = {
k: paddle.to_tensor(
v, dtype="int64").unsqueeze(0)
for k, v in tokenizer(text).items()
}
with paddle.no_grad():
score = paddle.nn.functional.softmax(model(**inputs), axis=-1)
for t, s in zip(tokenized_text, score[0][1:-1]):
index = paddle.argmax(s).item()
label = id2label[str(index)]
print(f"{label} {t} score {s[index].item()}")
# Nb 傅 score 0.9999998807907104
# Nb 達 score 0.9700667858123779
# Na 仁 score 0.9985846281051636
# Nd 今 score 0.9999947547912598
# D 將 score 0.9999957084655762
# VC 執 score 0.9999998807907104
# VC 行 score 0.9951109290122986
# Na 安 score 0.9999996423721313
# Na 樂 score 0.9999638795852661
# VH 死 score 0.9813857674598694
# COMMACATEGORY , score 1.0
# D 卻 score 1.0
# D 突 score 1.0
# Cbb 然 score 0.9989008903503418
# VJ 爆 score 0.9999979734420776
# VC 出 score 0.9965670108795166
# Nh 自 score 1.0
# Nh 己 score 1.0
# Neu 20 score 0.9999995231628418
# Nf 年 score 0.9125530123710632
# Ng 前 score 0.9999992847442627
# P 遭 score 1.0
# Nb 緯 score 0.9999996423721313
# VA 來 score 0.9322434663772583
# Na 體 score 0.9846553802490234
# Nc 育 score 0.729569137096405
# Nc 台 score 0.9999841451644897
# VC 封 score 0.9999997615814209
# VC 殺 score 0.9999991655349731
# COMMACATEGORY , score 1.0
# Nh 他 score 0.9999996423721313
# D 不 score 1.0
# VK 懂 score 1.0
# Nh 自 score 1.0
# Nh 己 score 0.9999978542327881
# Ncd 哪 score 0.9856181740760803
# Ncd 裡 score 0.9999995231628418
# VC 得 score 0.9999988079071045
# Na 罪 score 0.9994786381721497
# VCL 到 score 0.8332439661026001
# Nc 電 score 1.0
# Nc 視 score 0.9999986886978149
# Nc 台 score 0.9973978996276855
# PERIODCATEGORY 。 score 1.0
# transformers
path = "ckiplab/bert-base-chinese-pos"
model = hgnlp.BertForTokenClassification.from_pretrained(path)
model.eval()
tokenizer = hgnlp.BertTokenizer.from_pretrained(path)
inputs = tokenizer(text, return_tensors="pt")
tokenized_text = tokenizer.tokenize(text)
with torch.no_grad():
score = torch.nn.functional.softmax(model(**inputs).logits, dim=-1)
for t, s in zip(tokenized_text, score[0][1:-1]):
index = torch.argmax(s).item()
label = id2label[str(index)]
print(f"{label} {t} score {s[index].item()}")
# Nb 傅 score 0.9999998807907104
# Nb 達 score 0.970066487789154
# Na 仁 score 0.998584508895874
# Nd 今 score 0.9999948740005493
# D 將 score 0.9999957084655762
# VC 執 score 0.9999998807907104
# VC 行 score 0.995110809803009
# Na 安 score 0.9999995231628418
# Na 樂 score 0.9999638795852661
# VH 死 score 0.9813857674598694
# COMMACATEGORY , score 1.0
# D 卻 score 1.0
# D 突 score 1.0
# Cbb 然 score 0.9989006519317627
# VJ 爆 score 0.9999980926513672
# VC 出 score 0.996566891670227
# Nh 自 score 1.0
# Nh 己 score 1.0
# Neu 20 score 0.9999995231628418
# Nf 年 score 0.9125524163246155
# Ng 前 score 0.9999992847442627
# P 遭 score 1.0
# Nb 緯 score 0.9999997615814209
# VA 來 score 0.9322431087493896
# Na 體 score 0.9846551418304443
# Nc 育 score 0.7295736074447632
# Nc 台 score 0.9999841451644897
# VC 封 score 0.9999997615814209
# VC 殺 score 0.9999991655349731
# COMMACATEGORY , score 1.0
# Nh 他 score 0.9999997615814209
# D 不 score 0.9999998807907104
# VK 懂 score 1.0
# Nh 自 score 1.0
# Nh 己 score 0.9999977350234985
# Ncd 哪 score 0.9856180548667908
# Ncd 裡 score 0.9999995231628418
# VC 得 score 0.9999988079071045
# Na 罪 score 0.9994783997535706
# VCL 到 score 0.8332419991493225
# Nc 電 score 1.0
# Nc 視 score 0.9999988079071045
# Nc 台 score 0.9973980188369751
# PERIODCATEGORY 。 score 1.0
def compare_ckiplab_ner():
text = "傅達仁今將執行安樂死,卻突然爆出自己20年前遭緯來體育台封殺,他不懂自己哪裡得罪到電視台。"
id2label = {
"0": "O",
"1": "B-CARDINAL",
"2": "B-DATE",
"3": "B-EVENT",
"4": "B-FAC",
"5": "B-GPE",
"6": "B-LANGUAGE",
"7": "B-LAW",
"8": "B-LOC",
"9": "B-MONEY",
"10": "B-NORP",
"11": "B-ORDINAL",
"12": "B-ORG",
"13": "B-PERCENT",
"14": "B-PERSON",
"15": "B-PRODUCT",
"16": "B-QUANTITY",
"17": "B-TIME",
"18": "B-WORK_OF_ART",
"19": "I-CARDINAL",
"20": "I-DATE",
"21": "I-EVENT",
"22": "I-FAC",
"23": "I-GPE",
"24": "I-LANGUAGE",
"25": "I-LAW",
"26": "I-LOC",
"27": "I-MONEY",
"28": "I-NORP",
"29": "I-ORDINAL",
"30": "I-ORG",
"31": "I-PERCENT",
"32": "I-PERSON",
"33": "I-PRODUCT",
"34": "I-QUANTITY",
"35": "I-TIME",
"36": "I-WORK_OF_ART",
"37": "E-CARDINAL",
"38": "E-DATE",
"39": "E-EVENT",
"40": "E-FAC",
"41": "E-GPE",
"42": "E-LANGUAGE",
"43": "E-LAW",
"44": "E-LOC",
"45": "E-MONEY",
"46": "E-NORP",
"47": "E-ORDINAL",
"48": "E-ORG",
"49": "E-PERCENT",
"50": "E-PERSON",
"51": "E-PRODUCT",
"52": "E-QUANTITY",
"53": "E-TIME",
"54": "E-WORK_OF_ART",
"55": "S-CARDINAL",
"56": "S-DATE",
"57": "S-EVENT",
"58": "S-FAC",
"59": "S-GPE",
"60": "S-LANGUAGE",
"61": "S-LAW",
"62": "S-LOC",
"63": "S-MONEY",
"64": "S-NORP",
"65": "S-ORDINAL",
"66": "S-ORG",
"67": "S-PERCENT",
"68": "S-PERSON",
"69": "S-PRODUCT",
"70": "S-QUANTITY",
"71": "S-TIME",
"72": "S-WORK_OF_ART"
}
# ppnlp
path = "junnyu/ckiplab-bert-base-chinese-ner"
model = ppnlp.BertForTokenClassification.from_pretrained(path)
model.eval()
tokenizer = ppnlp.BertTokenizer.from_pretrained(path)
tokenized_text = tokenizer.tokenize(text)
inputs = {
k: paddle.to_tensor(
v, dtype="int64").unsqueeze(0)
for k, v in tokenizer(text).items()
}
with paddle.no_grad():
score = paddle.nn.functional.softmax(model(**inputs), axis=-1)
for t, s in zip(tokenized_text, score[0][1:-1]):
index = paddle.argmax(s).item()
label = id2label[str(index)]
print(f"{label} {t} score {s[index].item()}")
# B-PERSON 傅 score 0.9999995231628418
# I-PERSON 達 score 0.9999994039535522
# E-PERSON 仁 score 0.9999995231628418
# B-DATE 今 score 0.9991734623908997
# O 將 score 0.9852147698402405
# O 執 score 1.0
# O 行 score 0.9999998807907104
# O 安 score 0.9999996423721313
# O 樂 score 0.9999997615814209
# O 死 score 0.9999997615814209
# O , score 1.0
# O 卻 score 1.0
# O 突 score 1.0
# O 然 score 1.0
# O 爆 score 1.0
# O 出 score 1.0
# O 自 score 1.0
# O 己 score 1.0
# B-DATE 20 score 0.9999992847442627
# E-DATE 年 score 0.9999892711639404
# O 前 score 0.9999995231628418
# O 遭 score 1.0
# B-ORG 緯 score 0.9999990463256836
# I-ORG 來 score 0.9999986886978149
# I-ORG 體 score 0.999998927116394
# I-ORG 育 score 0.9999985694885254
# E-ORG 台 score 0.999998927116394
# O 封 score 1.0
# O 殺 score 1.0
# O , score 1.0
# O 他 score 1.0
# O 不 score 1.0
# O 懂 score 1.0
# O 自 score 1.0
# O 己 score 1.0
# O 哪 score 1.0
# O 裡 score 1.0
# O 得 score 1.0
# O 罪 score 1.0
# O 到 score 1.0
# O 電 score 1.0
# O 視 score 1.0
# O 台 score 1.0
# O 。 score 0.9999960660934448
# transformers
path = "ckiplab/bert-base-chinese-ner"
model = hgnlp.BertForTokenClassification.from_pretrained(path)
model.eval()
tokenizer = hgnlp.BertTokenizer.from_pretrained(path)
inputs = tokenizer(text, return_tensors="pt")
tokenized_text = tokenizer.tokenize(text)
with torch.no_grad():
score = torch.nn.functional.softmax(model(**inputs).logits, dim=-1)
for t, s in zip(tokenized_text, score[0][1:-1]):
index = torch.argmax(s).item()
label = id2label[str(index)]
print(f"{label} {t} score {s[index].item()}")
# B-PERSON 傅 score 0.9999996423721313
# I-PERSON 達 score 0.9999994039535522
# E-PERSON 仁 score 0.9999995231628418
# B-DATE 今 score 0.9991734623908997
# O 將 score 0.9852147698402405
# O 執 score 1.0
# O 行 score 0.9999998807907104
# O 安 score 0.9999997615814209
# O 樂 score 1.0
# O 死 score 0.9999998807907104
# O , score 1.0
# O 卻 score 1.0
# O 突 score 1.0
# O 然 score 1.0
# O 爆 score 1.0
# O 出 score 1.0
# O 自 score 1.0
# O 己 score 1.0
# B-DATE 20 score 0.9999994039535522
# E-DATE 年 score 0.9999892711639404
# O 前 score 0.9999996423721313
# O 遭 score 1.0
# B-ORG 緯 score 0.9999991655349731
# I-ORG 來 score 0.9999986886978149
# I-ORG 體 score 0.999998927116394
# I-ORG 育 score 0.9999984502792358
# E-ORG 台 score 0.999998927116394
# O 封 score 1.0
# O 殺 score 1.0
# O , score 1.0
# O 他 score 1.0
# O 不 score 1.0
# O 懂 score 1.0
# O 自 score 1.0
# O 己 score 1.0
# O 哪 score 1.0
# O 裡 score 1.0
# O 得 score 1.0
# O 罪 score 1.0
# O 到 score 1.0
# O 電 score 1.0
# O 視 score 1.0
# O 台 score 1.0
# O 。 score 0.9999960660934448
if __name__ == "__main__":
compare_ckiplab_ner()
compare_ckiplab_pos()
compare_ckiplab_ws()
compare_math()
compare_nlptown()

View File

@@ -0,0 +1,78 @@
# Copyright (c) 2021 PaddlePaddle Authors. 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 collections import OrderedDict
import argparse
huggingface_to_paddle = {
"embeddings.LayerNorm": "embeddings.layer_norm",
"encoder.layer": "encoder.layers",
"attention.self.query": "self_attn.q_proj",
"attention.self.key": "self_attn.k_proj",
"attention.self.value": "self_attn.v_proj",
"attention.output.dense": "self_attn.out_proj",
"intermediate.dense": "linear1",
"output.dense": "linear2",
"attention.output.LayerNorm": "norm1",
"output.LayerNorm": "norm2",
"predictions.decoder.": "predictions.decoder_",
"predictions.transform.dense": "predictions.transform",
"predictions.transform.LayerNorm": "predictions.layer_norm",
}
def convert_pytorch_checkpoint_to_paddle(pytorch_checkpoint_path,
paddle_dump_path):
import torch
import paddle
pytorch_state_dict = torch.load(pytorch_checkpoint_path, map_location="cpu")
paddle_state_dict = OrderedDict()
for k, v in pytorch_state_dict.items():
is_transpose = False
if k[-7:] == ".weight":
if ".embeddings." not in k and ".LayerNorm." not in k:
if v.ndim == 2:
v = v.transpose(0, 1)
is_transpose = True
oldk = k
for huggingface_name, paddle_name in huggingface_to_paddle.items():
k = k.replace(huggingface_name, paddle_name)
if "bert." not in k and "cls." not in k and "classifier" not in k:
k = "bert." + k
print(f"Converting: {oldk} => {k} | is_transpose {is_transpose}")
paddle_state_dict[k] = v.data.numpy()
paddle.save(paddle_state_dict, paddle_dump_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--pytorch_checkpoint_path",
default="MODEL/ckiplab-bert-base-chinese-ws/pytorch_model.bin",
type=str,
required=False,
help="Path to the Pytorch checkpoint path.")
parser.add_argument(
"--paddle_dump_path",
default="MODEL/ckiplab-bert-base-chinese-ws/model_state.pdparams",
type=str,
required=False,
help="Path to the output Paddle model.")
args = parser.parse_args()
convert_pytorch_checkpoint_to_paddle(args.pytorch_checkpoint_path,
args.paddle_dump_path)

View File

@@ -0,0 +1,159 @@
# 详细介绍
**介绍**:ckiplab-bert-base-chinese-ner 是一个带有token分类头的BERT模型,该模型已经在**命名实体识别任务**上进行了微调。
关于完整使用方法及其他信息,请参考 https://github.com/ckiplab/ckip-transformers 。
**模型结构**: **`BertForTokenClassification`**,带有token分类头的Bert模型。
**适用下游任务**:**命名实体识别**,该权重已经在下游`NER`任务上进行了微调,因此可直接使用。
# 使用示例
```python
import paddle
import paddle.nn.functional as F
from paddlenlp.transformers import BertForTokenClassification, BertTokenizer
path = "junnyu/ckiplab-bert-base-chinese-ner"
model = BertForTokenClassification.from_pretrained(path)
model.eval()
tokenizer = BertTokenizer.from_pretrained(path)
text = "傅達仁今將執行安樂死,卻突然爆出自己20年前遭緯來體育台封殺,他不懂自己哪裡得罪到電視台。"
tokenized_text = tokenizer.tokenize(text)
inputs = {
k: paddle.to_tensor(
v, dtype="int64").unsqueeze(0)
for k, v in tokenizer(text).items()
}
with paddle.no_grad():
score = F.softmax(model(**inputs), axis=-1)
id2label = {
"0": "O",
"1": "B-CARDINAL",
"2": "B-DATE",
"3": "B-EVENT",
"4": "B-FAC",
"5": "B-GPE",
"6": "B-LANGUAGE",
"7": "B-LAW",
"8": "B-LOC",
"9": "B-MONEY",
"10": "B-NORP",
"11": "B-ORDINAL",
"12": "B-ORG",
"13": "B-PERCENT",
"14": "B-PERSON",
"15": "B-PRODUCT",
"16": "B-QUANTITY",
"17": "B-TIME",
"18": "B-WORK_OF_ART",
"19": "I-CARDINAL",
"20": "I-DATE",
"21": "I-EVENT",
"22": "I-FAC",
"23": "I-GPE",
"24": "I-LANGUAGE",
"25": "I-LAW",
"26": "I-LOC",
"27": "I-MONEY",
"28": "I-NORP",
"29": "I-ORDINAL",
"30": "I-ORG",
"31": "I-PERCENT",
"32": "I-PERSON",
"33": "I-PRODUCT",
"34": "I-QUANTITY",
"35": "I-TIME",
"36": "I-WORK_OF_ART",
"37": "E-CARDINAL",
"38": "E-DATE",
"39": "E-EVENT",
"40": "E-FAC",
"41": "E-GPE",
"42": "E-LANGUAGE",
"43": "E-LAW",
"44": "E-LOC",
"45": "E-MONEY",
"46": "E-NORP",
"47": "E-ORDINAL",
"48": "E-ORG",
"49": "E-PERCENT",
"50": "E-PERSON",
"51": "E-PRODUCT",
"52": "E-QUANTITY",
"53": "E-TIME",
"54": "E-WORK_OF_ART",
"55": "S-CARDINAL",
"56": "S-DATE",
"57": "S-EVENT",
"58": "S-FAC",
"59": "S-GPE",
"60": "S-LANGUAGE",
"61": "S-LAW",
"62": "S-LOC",
"63": "S-MONEY",
"64": "S-NORP",
"65": "S-ORDINAL",
"66": "S-ORG",
"67": "S-PERCENT",
"68": "S-PERSON",
"69": "S-PRODUCT",
"70": "S-QUANTITY",
"71": "S-TIME",
"72": "S-WORK_OF_ART"
}
for t, s in zip(tokenized_text, score[0][1:-1]):
index = paddle.argmax(s).item()
label = id2label[str(index)]
print(f"{label} {t} score {s[index].item()}")
# B-PERSON 傅 score 0.9999995231628418
# I-PERSON 達 score 0.9999994039535522
# E-PERSON 仁 score 0.9999995231628418
# B-DATE 今 score 0.9991734623908997
# O 將 score 0.9852147698402405
# O 執 score 1.0
# O 行 score 0.9999998807907104
# O 安 score 0.9999996423721313
# O 樂 score 0.9999997615814209
# O 死 score 0.9999997615814209
# O , score 1.0
# O 卻 score 1.0
# O 突 score 1.0
# O 然 score 1.0
# O 爆 score 1.0
# O 出 score 1.0
# O 自 score 1.0
# O 己 score 1.0
# B-DATE 20 score 0.9999992847442627
# E-DATE 年 score 0.9999892711639404
# O 前 score 0.9999995231628418
# O 遭 score 1.0
# B-ORG 緯 score 0.9999990463256836
# I-ORG 來 score 0.9999986886978149
# I-ORG 體 score 0.999998927116394
# I-ORG 育 score 0.9999985694885254
# E-ORG 台 score 0.999998927116394
# O 封 score 1.0
# O 殺 score 1.0
# O , score 1.0
# O 他 score 1.0
# O 不 score 1.0
# O 懂 score 1.0
# O 自 score 1.0
# O 己 score 1.0
# O 哪 score 1.0
# O 裡 score 1.0
# O 得 score 1.0
# O 罪 score 1.0
# O 到 score 1.0
# O 電 score 1.0
# O 視 score 1.0
# O 台 score 1.0
# O 。 score 0.9999960660934448
```
# 权重来源
https://huggingface.co/ckiplab/bert-base-chinese-ner
这个项目提供了繁体中文版transformer模型(包含ALBERT、BERT、GPT2)及自然语言处理工具(包含分词、词性标注、命名实体识别)。

View File

@@ -0,0 +1,6 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/ckiplab-bert-base-chinese-ner/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/ckiplab-bert-base-chinese-ner/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/ckiplab-bert-base-chinese-ner/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/ckiplab-bert-base-chinese-ner/vocab.txt"
}

View File

@@ -0,0 +1,146 @@
# 详细介绍
**介绍**:ckiplab-bert-base-chinese-pos 是一个带有token分类头的BERT模型,该模型已经在**词性标注任务**上进行了微调。
关于完整使用方法及其他信息,请参考 https://github.com/ckiplab/ckip-transformers 。
**模型结构**: **`BertForTokenClassification`**,带有token分类头的Bert模型。
**适用下游任务**:**词性标注**,该权重已经在下游`POS`任务上进行了微调,因此可直接使用。
# 使用示例
```python
import paddle
import paddle.nn.functional as F
from paddlenlp.transformers import BertForTokenClassification, BertTokenizer
path = "junnyu/ckiplab-bert-base-chinese-pos"
model = BertForTokenClassification.from_pretrained(path)
model.eval()
tokenizer = BertTokenizer.from_pretrained(path)
text = "傅達仁今將執行安樂死,卻突然爆出自己20年前遭緯來體育台封殺,他不懂自己哪裡得罪到電視台。"
tokenized_text = tokenizer.tokenize(text)
inputs = {
k: paddle.to_tensor(
v, dtype="int64").unsqueeze(0)
for k, v in tokenizer(text).items()
}
with paddle.no_grad():
score = F.softmax(model(**inputs), axis=-1)
id2label = {
"0": "A",
"1": "Caa",
"2": "Cab",
"3": "Cba",
"4": "Cbb",
"5": "D",
"6": "Da",
"7": "Dfa",
"8": "Dfb",
"9": "Di",
"10": "Dk",
"11": "DM",
"12": "I",
"13": "Na",
"14": "Nb",
"15": "Nc",
"16": "Ncd",
"17": "Nd",
"18": "Nep",
"19": "Neqa",
"20": "Neqb",
"21": "Nes",
"22": "Neu",
"23": "Nf",
"24": "Ng",
"25": "Nh",
"26": "Nv",
"27": "P",
"28": "T",
"29": "VA",
"30": "VAC",
"31": "VB",
"32": "VC",
"33": "VCL",
"34": "VD",
"35": "VF",
"36": "VE",
"37": "VG",
"38": "VH",
"39": "VHC",
"40": "VI",
"41": "VJ",
"42": "VK",
"43": "VL",
"44": "V_2",
"45": "DE",
"46": "SHI",
"47": "FW",
"48": "COLONCATEGORY",
"49": "COMMACATEGORY",
"50": "DASHCATEGORY",
"51": "DOTCATEGORY",
"52": "ETCCATEGORY",
"53": "EXCLAMATIONCATEGORY",
"54": "PARENTHESISCATEGORY",
"55": "PAUSECATEGORY",
"56": "PERIODCATEGORY",
"57": "QUESTIONCATEGORY",
"58": "SEMICOLONCATEGORY",
"59": "SPCHANGECATEGORY"
}
for t, s in zip(tokenized_text, score[0][1:-1]):
index = paddle.argmax(s).item()
label = id2label[str(index)]
print(f"{label} {t} score {s[index].item()}")
# Nb 傅 score 0.9999998807907104
# Nb 達 score 0.9700667858123779
# Na 仁 score 0.9985846281051636
# Nd 今 score 0.9999947547912598
# D 將 score 0.9999957084655762
# VC 執 score 0.9999998807907104
# VC 行 score 0.9951109290122986
# Na 安 score 0.9999996423721313
# Na 樂 score 0.9999638795852661
# VH 死 score 0.9813857674598694
# COMMACATEGORY , score 1.0
# D 卻 score 1.0
# D 突 score 1.0
# Cbb 然 score 0.9989008903503418
# VJ 爆 score 0.9999979734420776
# VC 出 score 0.9965670108795166
# Nh 自 score 1.0
# Nh 己 score 1.0
# Neu 20 score 0.9999995231628418
# Nf 年 score 0.9125530123710632
# Ng 前 score 0.9999992847442627
# P 遭 score 1.0
# Nb 緯 score 0.9999996423721313
# VA 來 score 0.9322434663772583
# Na 體 score 0.9846553802490234
# Nc 育 score 0.729569137096405
# Nc 台 score 0.9999841451644897
# VC 封 score 0.9999997615814209
# VC 殺 score 0.9999991655349731
# COMMACATEGORY , score 1.0
# Nh 他 score 0.9999996423721313
# D 不 score 1.0
# VK 懂 score 1.0
# Nh 自 score 1.0
# Nh 己 score 0.9999978542327881
# Ncd 哪 score 0.9856181740760803
# Ncd 裡 score 0.9999995231628418
# VC 得 score 0.9999988079071045
# Na 罪 score 0.9994786381721497
# VCL 到 score 0.8332439661026001
# Nc 電 score 1.0
# Nc 視 score 0.9999986886978149
# Nc 台 score 0.9973978996276855
# PERIODCATEGORY 。 score 1.0
```
# 权重来源
https://huggingface.co/ckiplab/bert-base-chinese-pos
这个项目提供了繁体中文版transformer模型(包含ALBERT、BERT、GPT2)及自然语言处理工具(包含分词、词性标注、命名实体识别)。

View File

@@ -0,0 +1,6 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/ckiplab-bert-base-chinese-pos/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/ckiplab-bert-base-chinese-pos/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/ckiplab-bert-base-chinese-pos/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/ckiplab-bert-base-chinese-pos/vocab.txt"
}

View File

@@ -0,0 +1,85 @@
# 详细介绍
**介绍**:ckiplab-bert-base-chinese-ws 是一个带有token分类头的BERT模型,该模型已经在**分词任务**上进行了微调。
关于完整使用方法及其他信息,请参考 https://github.com/ckiplab/ckip-transformers 。
**模型结构**: **`BertForTokenClassification`**,带有token分类头的Bert模型。
**适用下游任务**:**分词**,该权重已经在下游`WS`任务上进行了微调,因此可直接使用。
# 使用示例
```python
import paddle
import paddle.nn.functional as F
from paddlenlp.transformers import BertForTokenClassification, BertTokenizer
path = "junnyu/ckiplab-bert-base-chinese-ws"
model = BertForTokenClassification.from_pretrained(path)
model.eval()
tokenizer = BertTokenizer.from_pretrained(path)
text = "傅達仁今將執行安樂死,卻突然爆出自己20年前遭緯來體育台封殺,他不懂自己哪裡得罪到電視台。"
tokenized_text = tokenizer.tokenize(text)
inputs = {
k: paddle.to_tensor(
v, dtype="int64").unsqueeze(0)
for k, v in tokenizer(text).items()
}
with paddle.no_grad():
score = F.softmax(model(**inputs), axis=-1)
id2label = {"0": "B", "1": "I"}
for t, s in zip(tokenized_text, score[0][1:-1]):
index = paddle.argmax(s).item()
label = id2label[str(index)]
print(f"{label} {t} score {s[index].item()}")
# B 傅 score 0.9999865293502808
# I 達 score 0.999922513961792
# I 仁 score 0.9999332427978516
# B 今 score 0.9999370574951172
# B 將 score 0.9983423948287964
# B 執 score 0.9999731779098511
# I 行 score 0.9999544620513916
# B 安 score 0.9999713897705078
# I 樂 score 0.9999532699584961
# I 死 score 0.9998632669448853
# B , score 0.9999871253967285
# B 卻 score 0.9999560117721558
# B 突 score 0.9999818801879883
# I 然 score 0.9999614953994751
# B 爆 score 0.9999759197235107
# I 出 score 0.9994433522224426
# B 自 score 0.9999866485595703
# I 己 score 0.9999630451202393
# B 20 score 0.9999810457229614
# B 年 score 0.9974608421325684
# B 前 score 0.8930220603942871
# B 遭 score 0.9999674558639526
# B 緯 score 0.999970555305481
# I 來 score 0.9999680519104004
# B 體 score 0.9997956156730652
# I 育 score 0.9999778270721436
# I 台 score 0.9980663657188416
# B 封 score 0.999984860420227
# I 殺 score 0.999974250793457
# B , score 0.9999891519546509
# B 他 score 0.999988317489624
# B 不 score 0.9999889135360718
# B 懂 score 0.9997660517692566
# B 自 score 0.9999877214431763
# I 己 score 0.9999549388885498
# B 哪 score 0.9999915361404419
# I 裡 score 0.9980868101119995
# B 得 score 0.9999058246612549
# I 罪 score 0.9916028380393982
# I 到 score 0.8443355560302734
# B 電 score 0.9999363422393799
# I 視 score 0.9999769926071167
# I 台 score 0.999947190284729
# B 。 score 0.9999719858169556
```
# 权重来源
https://huggingface.co/ckiplab/bert-base-chinese-ws
这个项目提供了繁体中文版transformer模型(包含ALBERT、BERT、GPT2)及自然语言处理工具(包含分词、词性标注、命名实体识别)。

View File

@@ -0,0 +1,6 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/ckiplab-bert-base-chinese-ws/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/ckiplab-bert-base-chinese-ws/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/ckiplab-bert-base-chinese-ws/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/ckiplab-bert-base-chinese-ws/vocab.txt"
}

View File

@@ -0,0 +1,55 @@
# 详细介绍
# DistilGPT2
**介绍**: DistilGPT2 英语语言模型使用 OpenWebTextCorpus(OpenAI 的 WebText 数据集),使用 GPT2 的最小版本的进行了预训练。 该模型有 6 层、768 个维度和 12 个头,总计 82M 参数(相比之下 GPT2 的参数为 124M)。 平均而言,DistilGPT2 比 GPT2 快两倍。
在 WikiText-103 基准测试中,GPT2 在测试集上的困惑度为 16.3,而 DistilGPT2 的困惑度为 21.1(在训练集上进行微调后)。
**模型结构**: **`GPTLMHeadModel`**,GPT模型。
**适用下游任务**:**文本生成**。
# 使用示例
```python
import numpy as np
import paddle
from paddlenlp.transformers import GPTTokenizer, GPTLMHeadModel
from paddlenlp.utils.log import logger
class Demo:
def __init__(self, model_name_or_path="junnyu/distilgpt2", max_predict_len=32):
self.tokenizer = GPTTokenizer.from_pretrained(model_name_or_path)
logger.info("Loading the model parameters, please wait...")
self.max_predict_len = max_predict_len
self.model = GPTLMHeadModel.from_pretrained(
model_name_or_path, eol_token_id=self.tokenizer.eol_token_id
)
self.model.eval()
logger.info("Model loaded.")
@paddle.no_grad()
def predict(self, text="My name is Teven and I am"):
ids = self.tokenizer(text)["input_ids"]
input_ids = paddle.to_tensor(np.array(ids).reshape(1, -1).astype("int64"))
out = self.model.generate(
input_ids,
max_length=self.max_predict_len,
repetition_penalty=1.2,
temperature=0,
)[0][0]
out = [int(x) for x in out.numpy().reshape([-1])]
print(text + self.tokenizer.convert_ids_to_string(out))
demo = Demo(model_name_or_path="junnyu/distilgpt2",max_predict_len=64)
demo.predict(text="My name is Teven and I am")
# My name is Teven and I am a member of the team.
# I have been playing with my friends since we were little, so it was nice to see them play together in our home town on Saturday night! We are very excited about this opportunity for us as well!!<|endoftext|>
```
# 权重来源
https://huggingface.co/distilgpt2

View File

@@ -0,0 +1,7 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/distilgpt2/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/distilgpt2/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/distilgpt2/tokenizer_config.json",
"merges_file":"https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/distilgpt2/merges.txt",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/distilgpt2/vocab.json"
}

View File

@@ -0,0 +1,138 @@
# Copyright (c) 2021 PaddlePaddle Authors. 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.
import paddle
import torch
import numpy as np
import paddlenlp.transformers as ppnlp
import transformers as hgnlp
def compare(a, b):
a = a.cpu().numpy()
b = b.cpu().numpy()
meandif = np.abs(a - b).mean()
maxdif = np.abs(a - b).max()
print("mean dif:", meandif)
print("max dif:", maxdif)
def compare_discriminator(
path="junnyu/hfl-chinese-electra-180g-base-discriminator"):
pdmodel = ppnlp.ElectraDiscriminator.from_pretrained(path)
ptmodel = ppnlp.ElectraForPreTraining.from_pretrained(path).cuda()
tokenizer = ppnlp.ElectraTokenizer.from_pretrained(path)
pdmodel.eval()
ptmodel.eval()
text = "欢迎使用paddlenlp!"
pdinputs = {
k: paddle.to_tensor(
v, dtype="int64").unsqueeze(0)
for k, v in tokenizer(text).items()
}
ptinputs = {
k: torch.tensor(
v, dtype=torch.long).unsqueeze(0).cuda()
for k, v in tokenizer(text).items()
}
with paddle.no_grad():
pd_logits = pdmodel(**pdinputs)
with torch.no_grad():
pt_logits = ptmodel(**ptinputs).logits
compare(pd_logits, pt_logits)
def compare_generator():
text = "本院经审查认为,本案[MASK]民间借贷纠纷申请再审案件,应重点审查二审判决是否存在错误的情形。"
# ppnlp
path = "junnyu/hfl-chinese-legal-electra-small-generator"
model = ppnlp.ElectraForMaskedLM.from_pretrained(path)
tokenizer = ppnlp.ElectraTokenizer.from_pretrained(path)
model.eval()
tokens = ["[CLS]"]
text_list = text.split("[MASK]")
for i, t in enumerate(text_list):
tokens.extend(tokenizer.tokenize(t))
if i == len(text_list) - 1:
tokens.extend(["[SEP]"])
else:
tokens.extend(["[MASK]"])
input_ids_list = tokenizer.convert_tokens_to_ids(tokens)
input_ids = paddle.to_tensor([input_ids_list])
with paddle.no_grad():
pd_outputs = model(input_ids)[0]
pd_outputs_sentence = "paddle: "
for i, id in enumerate(input_ids_list):
if id == tokenizer.convert_tokens_to_ids(["[MASK]"])[0]:
scores, index = paddle.nn.functional.softmax(pd_outputs[i],
-1).topk(5)
tokens = tokenizer.convert_ids_to_tokens(index.tolist())
outputs = []
for score, tk in zip(scores.tolist(), tokens):
outputs.append(f"{tk}={score}")
pd_outputs_sentence += "[" + "||".join(outputs) + "]" + " "
else:
pd_outputs_sentence += "".join(
tokenizer.convert_ids_to_tokens(
[id], skip_special_tokens=True)) + " "
print(pd_outputs_sentence)
# transformers
path = "hfl/chinese-legal-electra-small-generator"
config = hgnlp.ElectraConfig.from_pretrained(path)
config.hidden_size = 64
config.intermediate_size = 256
config.num_attention_heads = 1
model = hgnlp.ElectraForMaskedLM.from_pretrained(path, config=config)
tokenizer = hgnlp.ElectraTokenizer.from_pretrained(path)
model.eval()
inputs = tokenizer(text, return_tensors="pt")
with torch.no_grad():
pt_outputs = model(**inputs).logits[0]
pt_outputs_sentence = "pytorch: "
for i, id in enumerate(inputs["input_ids"][0].tolist()):
if id == tokenizer.convert_tokens_to_ids(["[MASK]"])[0]:
scores, index = torch.nn.functional.softmax(pt_outputs[i],
-1).topk(5)
tokens = tokenizer.convert_ids_to_tokens(index.tolist())
outputs = []
for score, tk in zip(scores.tolist(), tokens):
outputs.append(f"{tk}={score}")
pt_outputs_sentence += "[" + "||".join(outputs) + "]" + " "
else:
pt_outputs_sentence += "".join(
tokenizer.convert_ids_to_tokens(
[id], skip_special_tokens=True)) + " "
print(pt_outputs_sentence)
if __name__ == "__main__":
compare_discriminator(
path="junnyu/hfl-chinese-electra-180g-base-discriminator")
# # mean dif: 3.1698835e-06
# # max dif: 1.335144e-05
compare_discriminator(
path="junnyu/hfl-chinese-electra-180g-small-ex-discriminator")
# mean dif: 3.7930229e-06
# max dif: 1.04904175e-05
compare_generator()
# paddle: 本 院 经 审 查 认 为 , 本 案 [因=0.27444931864738464||经=0.18613006174564362||系=0.09408623725175858||的=0.07536833733320236||就=0.033634234219789505] 民 间 借 贷 纠 纷 申 请 再 审 案 件 , 应 重 点 审 查 二 审 判 决 是 否 存 在 错 误 的 情 形 。
# pytorch: 本 院 经 审 查 认 为 , 本 案 [因=0.2744344472885132||经=0.1861187219619751||系=0.09407979995012283||的=0.07537488639354706||就=0.03363779932260513] 民 间 借 贷 纠 纷 申 请 再 审 案 件 , 应 重 点 审 查 二 审 判 决 是 否 存 在 错 误 的 情 形 。

View File

@@ -0,0 +1,79 @@
# Copyright (c) 2021 PaddlePaddle Authors. 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 collections import OrderedDict
import argparse
huggingface_to_paddle = {
"embeddings.LayerNorm": "embeddings.layer_norm",
"encoder.layer": "encoder.layers",
"attention.self.query.": "self_attn.q_proj.",
"attention.self.key.": "self_attn.k_proj.",
"attention.self.value.": "self_attn.v_proj.",
"attention.output.dense.": "self_attn.out_proj.",
"intermediate.dense": "linear1",
"output.dense": "linear2",
"attention.output.LayerNorm": "norm1",
"output.LayerNorm": "norm2",
"generator_predictions.LayerNorm": "generator_predictions.layer_norm",
"generator_lm_head.bias": "generator_lm_head_bias",
}
skip_weights = ["electra.embeddings.position_ids"]
dont_transpose = ["_embeddings.weight", "LayerNorm."]
def convert_pytorch_checkpoint_to_paddle(pytorch_checkpoint_path,
paddle_dump_path):
import torch
import paddle
pytorch_state_dict = torch.load(pytorch_checkpoint_path, map_location="cpu")
paddle_state_dict = OrderedDict()
for k, v in pytorch_state_dict.items():
if k == "generator_lm_head.weight": continue
is_transpose = False
if k in skip_weights:
continue
if k[-7:] == ".weight":
if not any([w in k for w in dont_transpose]):
if v.ndim == 2:
v = v.transpose(0, 1)
is_transpose = True
oldk = k
for huggingface_name, paddle_name in huggingface_to_paddle.items():
k = k.replace(huggingface_name, paddle_name)
print(f"Converting: {oldk} => {k} | is_transpose {is_transpose}")
paddle_state_dict[k] = v.data.numpy()
paddle.save(paddle_state_dict, paddle_dump_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--pytorch_checkpoint_path",
default=r"MODEL\hfl-chinese-electra-180g-base-discriminator\pytorch_model.bin",
type=str,
required=False,
help="Path to the Pytorch checkpoint path.")
parser.add_argument(
"--paddle_dump_path",
default=r"MODEL\hfl-chinese-electra-180g-base-discriminator\model_state.pdparams",
type=str,
required=False,
help="Path to the output Paddle model.")
args = parser.parse_args()
convert_pytorch_checkpoint_to_paddle(args.pytorch_checkpoint_path,
args.paddle_dump_path)

View File

@@ -0,0 +1,115 @@
# Copyright (c) 2021 PaddlePaddle Authors. 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 paddlenlp.transformers import GPTLMHeadModel as PDGPT2LMHeadModel, GPTTokenizer, BertTokenizer
from transformers.models.gpt2.modeling_gpt2 import GPT2LMHeadModel as PTGPT2LMHeadModel
from paddlenlp.transformers import GPTForTokenClassification, GPTForSequenceClassification, GPTTokenizer
import paddle
import torch
import numpy as np
paddle.set_grad_enabled(False)
torch.set_grad_enabled(False)
def compare(a, b):
a = a.cpu().numpy()
b = b.cpu().numpy()
meandif = np.abs(a - b).mean()
maxdif = np.abs(a - b).max()
print("mean dif:", meandif)
print("max dif:", maxdif)
def compare_lm(path="junnyu/microsoft-DialoGPT-small"):
pdmodel = PDGPT2LMHeadModel.from_pretrained(path)
ptmodel = PTGPT2LMHeadModel.from_pretrained(path).cuda()
if "chinese" in path:
text = "欢迎使用paddlenlp!"
tokenizer = BertTokenizer.from_pretrained(path)
else:
text = "Welcome to paddlenlp!"
tokenizer = GPTTokenizer.from_pretrained(path)
pdmodel.eval()
ptmodel.eval()
pdinputs = {
k: paddle.to_tensor(
v, dtype="int64").unsqueeze(0)
for k, v in tokenizer(
text, return_token_type_ids=False).items()
}
ptinputs = {
k: torch.tensor(
v, dtype=torch.long).unsqueeze(0).cuda()
for k, v in tokenizer(
text, return_token_type_ids=False).items()
}
pd_logits = pdmodel(**pdinputs)
pt_logits = ptmodel(**ptinputs).logits
compare(pd_logits, pt_logits)
def test_GPTForTokenClassification():
tokenizer = GPTTokenizer.from_pretrained("junnyu/distilgpt2")
m = GPTForTokenClassification.from_pretrained("junnyu/distilgpt2")
inputs = tokenizer(
"Welcome to use PaddlePaddle and PaddleNLP!",
return_token_type_ids=False)
inputs = {
k: paddle.to_tensor(
[v], dtype="int64")
for (k, v) in inputs.items()
}
logits = m(**inputs)
print(logits.shape)
def test_GPTForSequenceClassification():
paddle.set_grad_enabled(False)
tokenizer = GPTTokenizer.from_pretrained("junnyu/distilgpt2")
m = GPTForSequenceClassification.from_pretrained("junnyu/distilgpt2")
inputs = tokenizer(
"Welcome to use PaddlePaddle and PaddleNLP!",
return_token_type_ids=False)
inputs = {
k: paddle.to_tensor(
[v], dtype="int64")
for (k, v) in inputs.items()
}
logits = m(**inputs)
print(logits.shape)
if __name__ == "__main__":
# compare_lm(
# path="junnyu/microsoft-DialoGPT-small")
# mean dif: 7.501994e-05
# max dif: 0.00036621094
# compare_lm(
# path="junnyu/distilgpt2")
# mean dif: 7.249901e-06
# max dif: 5.340576e-05
# compare_lm(
# path="junnyu/uer-gpt2-chinese-poem")
# mean dif: 1.0497178e-06
# max dif: 1.335144e-05
# test_GPTForTokenClassification()
# [1, 13, 2]
test_GPTForSequenceClassification()
# [1, 2]

View File

@@ -0,0 +1,99 @@
# Copyright (c) 2021 PaddlePaddle Authors. 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 collections import OrderedDict
import argparse
huggingface_to_paddle = {
"transformer.wte.weight": "gpt.embeddings.word_embeddings.weight",
"transformer.wpe.weight": "gpt.embeddings.position_embeddings.weight",
"transformer.h.": "gpt.decoder.layers.",
".attn.c_proj.": ".self_attn.out_proj.",
".ln_1.": ".norm1.",
".mlp.c_fc.": ".linear1.",
".mlp.c_proj.": ".linear2.",
".ln_2.": ".norm2.",
"transformer.ln_f.": "gpt.decoder.norm.",
"lm_head.weight": "lm_head.decoder_weight"
}
skip_weights = [".attn.bias", "lm_head.weight"]
dont_transpose = [
".wte.weight", ".wpe.weight", ".ln_", ".mlp.c_proj.", ".mlp.c_fc.",
".attn.c_proj.", "lm_head.weight"
]
# 注意,huggingface使用的Conv1D的weight和paddle.nn.Linear中的weight形状一致,因此不需要转置。
# 如果使用了torch.nn.Linear那么就需要转置了!
def convert_pytorch_checkpoint_to_paddle(pytorch_checkpoint_path,
paddle_dump_path):
import torch
import paddle
pytorch_state_dict = torch.load(pytorch_checkpoint_path, map_location="cpu")
paddle_state_dict = OrderedDict()
for k, v in pytorch_state_dict.items():
is_transpose = False
if k in skip_weights:
continue
# c_attn
if ".attn.c_attn." in k:
query_value_key = v.chunk(chunks=3, dim=-1)
for cross_value, new_name in zip(query_value_key, [
".self_attn.q_proj.", ".self_attn.k_proj.",
".self_attn.v_proj."
]):
oldk = k
newk = k.replace("transformer.h.",
"gpt.decoder.layers.").replace(".attn.c_attn.",
new_name)
paddle_state_dict[newk] = cross_value.data.numpy().astype(
"float32")
print(
f"Converting: {oldk} => {newk} | is_transpose {is_transpose}"
)
continue
if k[-7:] == ".weight":
if not any([w in k for w in dont_transpose]):
if v.ndim == 2:
v = v.transpose(0, 1)
is_transpose = True
oldk = k
for huggingface_name, paddle_name in huggingface_to_paddle.items():
k = k.replace(huggingface_name, paddle_name)
print(f"Converting: {oldk} => {k} | is_transpose {is_transpose}")
paddle_state_dict[k] = v.data.numpy().astype("float32")
paddle.save(paddle_state_dict, paddle_dump_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--pytorch_checkpoint_path",
default=r"community\junnyu\microsoft-DialoGPT-small\pytorch_model.bin",
type=str,
required=False,
help="Path to the Pytorch checkpoint path.")
parser.add_argument(
"--paddle_dump_path",
default=r"community\junnyu\microsoft-DialoGPT-small\model_state.pdparams",
type=str,
required=False,
help="Path to the output Paddle model.")
args = parser.parse_args()
convert_pytorch_checkpoint_to_paddle(args.pytorch_checkpoint_path,
args.paddle_dump_path)

View File

@@ -0,0 +1,37 @@
# 详细介绍
**介绍**:该模型是base版本的Electra discriminator模型,并且在180G的中文数据上进行训练。
**模型结构**: **`ElectraDiscriminator`**,带有判别器的中文Electra模型。
**适用下游任务**:**通用下游任务**,如:句子级别分类,token级别分类,抽取式问答等任务。
# 使用示例
```python
import paddle
from paddlenlp.transformers import ElectraDiscriminator, ElectraTokenizer
path = "junnyu/hfl-chinese-electra-180g-base-discriminator"
model = ElectraDiscriminator.from_pretrained(path)
tokenizer = ElectraTokenizer.from_pretrained(path)
model.eval()
text = "欢迎使用paddlenlp!"
inputs = {
k: paddle.to_tensor(
v, dtype="int64").unsqueeze(0)
for k, v in tokenizer(text).items()
}
with paddle.no_grad():
logits = model(**inputs)
print(logits.shape)
```
# 权重来源
https://huggingface.co/hfl/chinese-electra-180g-base-discriminator
谷歌和斯坦福大学发布了一种名为 ELECTRA 的新预训练模型,与 BERT 及其变体相比,该模型具有非常紧凑的模型尺寸和相对具有竞争力的性能。 为进一步加快中文预训练模型的研究,HIT与科大讯飞联合实验室(HFL)发布了基于ELECTRA官方代码的中文ELECTRA模型。 与 BERT 及其变体相比,ELECTRA-small 只需 1/10 的参数就可以在几个 NLP 任务上达到相似甚至更高的分数。
这个项目依赖于官方ELECTRA代码: https://github.com/google-research/electra

View File

@@ -0,0 +1,6 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/hfl-chinese-electra-180g-base-discriminator/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/hfl-chinese-electra-180g-base-discriminator/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/hfl-chinese-electra-180g-base-discriminator/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/hfl-chinese-electra-180g-base-discriminator/vocab.txt"
}

View File

@@ -0,0 +1,36 @@
# 详细介绍
**介绍**:该模型是small版本的Electra discriminator模型,并且在180G的中文数据上进行训练。
**模型结构**: **`ElectraDiscriminator`**,带有判别器的中文Electra模型。
**适用下游任务**:**通用下游任务**,如:句子级别分类,token级别分类,抽取式问答等任务。
# 使用示例
```python
import paddle
from paddlenlp.transformers import ElectraDiscriminator,ElectraTokenizer
path = "junnyu/hfl-chinese-electra-180g-small-ex-discriminator"
model = ElectraDiscriminator.from_pretrained(path)
tokenizer = ElectraTokenizer.from_pretrained(path)
model.eval()
text = "欢迎使用paddlenlp!"
inputs = {
k: paddle.to_tensor(
v, dtype="int64").unsqueeze(0)
for k, v in tokenizer(text).items()
}
with paddle.no_grad():
logits = model(**inputs)
print(logits.shape)
```
# 权重来源
https://huggingface.co/hfl/chinese-electra-180g-small-ex-discriminator
谷歌和斯坦福大学发布了一种名为 ELECTRA 的新预训练模型,与 BERT 及其变体相比,该模型具有非常紧凑的模型尺寸和相对具有竞争力的性能。 为进一步加快中文预训练模型的研究,HIT与科大讯飞联合实验室(HFL)发布了基于ELECTRA官方代码的中文ELECTRA模型。 与 BERT 及其变体相比,ELECTRA-small 只需 1/10 的参数就可以在几个 NLP 任务上达到相似甚至更高的分数。

View File

@@ -0,0 +1,6 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/hfl-chinese-electra-180g-small-ex-discriminator/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/hfl-chinese-electra-180g-small-ex-discriminator/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/hfl-chinese-electra-180g-small-ex-discriminator/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/hfl-chinese-electra-180g-small-ex-discriminator/vocab.txt"
}

View File

@@ -0,0 +1,58 @@
# 详细介绍
**介绍**:该模型是small版本的Electra generator模型,该模型在法律领域数据上进行了预训练。
**模型结构**: **`ElectraGenerator`**,带有生成器的中文Electra模型。
**适用下游任务**:**法律领域的下游任务**,如:法律领域的句子级别分类,法律领域的token级别分类,法律领域的抽取式问答等任务。
(注:生成器的效果不好,通常使用判别器进行下游任务微调)
# 使用示例
```python
import paddle
from paddlenlp.transformers import ElectraGenerator, ElectraTokenizer
text = "本院经审查认为,本案[MASK]民间借贷纠纷申请再审案件,应重点审查二审判决是否存在错误的情形。"
path = "junnyu/hfl-chinese-legal-electra-small-generator"
model = ElectraGenerator.from_pretrained(path)
model.eval()
tokenizer = ElectraTokenizer.from_pretrained(path)
tokens = ["[CLS]"]
text_list = text.split("[MASK]")
for i, t in enumerate(text_list):
tokens.extend(tokenizer.tokenize(t))
if i == len(text_list) - 1:
tokens.extend(["[SEP]"])
else:
tokens.extend(["[MASK]"])
input_ids_list = tokenizer.convert_tokens_to_ids(tokens)
input_ids = paddle.to_tensor([input_ids_list])
with paddle.no_grad():
pd_outputs = model(input_ids)[0]
pd_outputs_sentence = "paddle: "
for i, id in enumerate(input_ids_list):
if id == tokenizer.convert_tokens_to_ids(["[MASK]"])[0]:
scores, index = paddle.nn.functional.softmax(pd_outputs[i],
-1).topk(5)
tokens = tokenizer.convert_ids_to_tokens(index.tolist())
outputs = []
for score, tk in zip(scores.tolist(), tokens):
outputs.append(f"{tk}={score}")
pd_outputs_sentence += "[" + "||".join(outputs) + "]" + " "
else:
pd_outputs_sentence += "".join(
tokenizer.convert_ids_to_tokens(
[id], skip_special_tokens=True)) + " "
print(pd_outputs_sentence)
# paddle: 本 院 经 审 查 认 为 , 本 案 [因=0.27444931864738464||经=0.18613006174564362||系=0.09408623725175858||的=0.07536833733320236||就=0.033634234219789505] 民 间 借 贷 纠 纷 申 请 再 审 案 件 , 应 重 点 审 查 二 审 判 决 是 否 存 在 错 误 的 情 形 。
```
# 权重来源
https://huggingface.co/hfl/chinese-legal-electra-small-generator
谷歌和斯坦福大学发布了一种名为 ELECTRA 的新预训练模型,与 BERT 及其变体相比,该模型具有非常紧凑的模型尺寸和相对具有竞争力的性能。 为进一步加快中文预训练模型的研究,HIT与科大讯飞联合实验室(HFL)发布了基于ELECTRA官方代码的中文ELECTRA模型。 与 BERT 及其变体相比,ELECTRA-small 只需 1/10 的参数就可以在几个 NLP 任务上达到相似甚至更高的分数。
这个项目依赖于官方ELECTRA代码: https://github.com/google-research/electra

View File

@@ -0,0 +1,6 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/hfl-chinese-legal-electra-small-generator/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/hfl-chinese-legal-electra-small-generator/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/hfl-chinese-legal-electra-small-generator/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/hfl-chinese-legal-electra-small-generator/vocab.txt"
}

View File

@@ -0,0 +1,67 @@
# 详细介绍
# microsoft-DialoGPT-small
**介绍**: 最先进的大规模预训练响应生成模型 (DialoGPT)
DialoGPT 是一种用于多轮对话的 SOTA 大规模预训练对话响应生成模型。 人类评估结果表明,DialoGPT 生成的响应与单轮对话图灵测试下的人类响应质量相当。 该模型是在来自 Reddit 讨论的 147M 多轮对话上训练的。
**模型结构**: **`GPTLMHeadModel`**,GPT模型。
**适用下游任务**:**文本生成**。
# 使用示例
```python
import numpy as np
import paddle
from paddlenlp.transformers import GPTTokenizer, GPTLMHeadModel
from paddlenlp.utils.log import logger
class Demo:
def __init__(self, model_name_or_path="junnyu/microsoft-DialoGPT-small", max_predict_len=32):
self.tokenizer = GPTTokenizer.from_pretrained(model_name_or_path)
logger.info("Loading the model parameters, please wait...")
self.max_predict_len = max_predict_len
self.model = GPTLMHeadModel.from_pretrained(
model_name_or_path, eol_token_id=self.tokenizer.eol_token_id
)
self.model.eval()
logger.info("Model loaded.")
@paddle.no_grad()
def predict(self):
# Let's chat for 5 lines
for step in range(5):
# encode the new user input, add the eos_token and return a tensor in Pytorch
ids = self.tokenizer(input(">> User:"))["input_ids"] + [self.tokenizer.eos_token_id]
new_user_input_ids = paddle.to_tensor(np.array(ids).reshape(1, -1).astype("int64"))
# append the new user input tokens to the chat history
bot_input_ids = paddle.concat([chat_history_ids, new_user_input_ids], axis=-1) if step > 0 else new_user_input_ids
# generated a response while limiting the total chat history to 1000 tokens,
chat_history_ids = self.model.generate(bot_input_ids, max_length=self.max_predict_len, pad_token_id=self.tokenizer.eos_token_id,decode_strategy="sampling",top_k=5,)[0]
# pretty print last ouput tokens from bot
print("DialoGPT: {}".format(self.tokenizer.convert_ids_to_string(chat_history_ids[0].tolist()).replace("<|endoftext|>","")))
chat_history_ids = paddle.concat([new_user_input_ids, chat_history_ids], axis=-1)
demo = Demo(model_name_or_path="junnyu/microsoft-DialoGPT-large")
demo.predict()
# >> User: Does money buy happiness?
# DialoGPT: No , but it can buy you a better life .
# >> User: What is the best way to buy happiness ?
# DialoGPT: A job , money , and a better life .
# >> User: This is so difficult !
# DialoGPT: Just get a job , money , and a better life . Then you can buy happiness .
# >> User: Oh, thank you!
# DialoGPT: No problem , friend .
```
# 权重来源
https://huggingface.co/microsoft/DialoGPT-large

View File

@@ -0,0 +1,7 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-large/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-large/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-large/tokenizer_config.json",
"merges_file":"https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-large/merges.txt",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-large/vocab.json"
}

View File

@@ -0,0 +1,67 @@
# 详细介绍
# microsoft-DialoGPT-small
**介绍**: 最先进的大规模预训练响应生成模型 (DialoGPT)
DialoGPT 是一种用于多轮对话的 SOTA 大规模预训练对话响应生成模型。 人类评估结果表明,DialoGPT 生成的响应与单轮对话图灵测试下的人类响应质量相当。 该模型是在来自 Reddit 讨论的 147M 多轮对话上训练的。
**模型结构**: **`GPTLMHeadModel`**,GPT模型。
**适用下游任务**:**文本生成**。
# 使用示例
```python
import numpy as np
import paddle
from paddlenlp.transformers import GPTTokenizer, GPTLMHeadModel
from paddlenlp.utils.log import logger
class Demo:
def __init__(self, model_name_or_path="junnyu/microsoft-DialoGPT-small", max_predict_len=32):
self.tokenizer = GPTTokenizer.from_pretrained(model_name_or_path)
logger.info("Loading the model parameters, please wait...")
self.max_predict_len = max_predict_len
self.model = GPTLMHeadModel.from_pretrained(
model_name_or_path, eol_token_id=self.tokenizer.eol_token_id
)
self.model.eval()
logger.info("Model loaded.")
@paddle.no_grad()
def predict(self):
# Let's chat for 5 lines
for step in range(5):
# encode the new user input, add the eos_token and return a tensor in Pytorch
ids = self.tokenizer(input(">> User:"))["input_ids"] + [self.tokenizer.eos_token_id]
new_user_input_ids = paddle.to_tensor(np.array(ids).reshape(1, -1).astype("int64"))
# append the new user input tokens to the chat history
bot_input_ids = paddle.concat([chat_history_ids, new_user_input_ids], axis=-1) if step > 0 else new_user_input_ids
# generated a response while limiting the total chat history to 1000 tokens,
chat_history_ids = self.model.generate(bot_input_ids, max_length=self.max_predict_len, pad_token_id=self.tokenizer.eos_token_id,decode_strategy="sampling",top_k=5,)[0]
# pretty print last ouput tokens from bot
print("DialoGPT: {}".format(self.tokenizer.convert_ids_to_string(chat_history_ids[0].tolist()).replace("<|endoftext|>","")))
chat_history_ids = paddle.concat([new_user_input_ids, chat_history_ids], axis=-1)
demo = Demo(model_name_or_path="junnyu/microsoft-DialoGPT-medium")
demo.predict()
# >> User: Does money buy happiness?
# DialoGPT: Money buys everything .
# >> User: What is the best way to buy happiness ?
# DialoGPT: I don't know but I've always thought it was money and happiness .
# >> User: This is so difficult !
# DialoGPT: It's not that hard to get happiness , though . It's a lot easier to get money and happiness .
# >> User: Oh, thank you!
# DialoGPT: No problem !
```
# 权重来源
https://huggingface.co/microsoft/DialoGPT-medium

View File

@@ -0,0 +1,7 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-medium/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-medium/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-medium/tokenizer_config.json",
"merges_file":"https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-medium/merges.txt",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-medium/vocab.json"
}

View File

@@ -0,0 +1,69 @@
# 详细介绍
# microsoft-DialoGPT-small
**介绍**: 最先进的大规模预训练响应生成模型 (DialoGPT)
DialoGPT 是一种用于多轮对话的 SOTA 大规模预训练对话响应生成模型。 人类评估结果表明,DialoGPT 生成的响应与单轮对话图灵测试下的人类响应质量相当。 该模型是在来自 Reddit 讨论的 147M 多轮对话上训练的。
**模型结构**: **`GPTLMHeadModel`**,GPT模型。
**适用下游任务**:**文本生成**。
# 使用示例
```python
import numpy as np
import paddle
from paddlenlp.transformers import GPTTokenizer, GPTLMHeadModel
from paddlenlp.utils.log import logger
class Demo:
def __init__(self, model_name_or_path="junnyu/microsoft-DialoGPT-small", max_predict_len=32):
self.tokenizer = GPTTokenizer.from_pretrained(model_name_or_path)
logger.info("Loading the model parameters, please wait...")
self.max_predict_len = max_predict_len
self.model = GPTLMHeadModel.from_pretrained(
model_name_or_path, eol_token_id=self.tokenizer.eol_token_id
)
self.model.eval()
logger.info("Model loaded.")
@paddle.no_grad()
def predict(self):
# Let's chat for 5 lines
for step in range(5):
# encode the new user input, add the eos_token and return a tensor in Pytorch
ids = self.tokenizer(input(">> User:"))["input_ids"] + [self.tokenizer.eos_token_id]
new_user_input_ids = paddle.to_tensor(np.array(ids).reshape(1, -1).astype("int64"))
# append the new user input tokens to the chat history
bot_input_ids = paddle.concat([chat_history_ids, new_user_input_ids], axis=-1) if step > 0 else new_user_input_ids
# generated a response while limiting the total chat history to 1000 tokens,
chat_history_ids = self.model.generate(bot_input_ids, max_length=self.max_predict_len, pad_token_id=self.tokenizer.eos_token_id,decode_strategy="sampling",top_k=5,)[0]
# pretty print last ouput tokens from bot
print("DialoGPT: {}".format(self.tokenizer.convert_ids_to_string(chat_history_ids[0].tolist()).replace("<|endoftext|>","")))
chat_history_ids = paddle.concat([new_user_input_ids, chat_history_ids], axis=-1)
demo = Demo(model_name_or_path="junnyu/microsoft-DialoGPT-small")
demo.predict()
# >> User: Does money buy happiness?
# DialoGPT: Money doesn't buy happiness .
# >> User: What is the best way to buy happiness ?
# DialoGPT: You can get the happiness you need by buying the best things . The best way is to find the best things .
# >> User: This is so difficult !
# DialoGPT: Buy what you want .
# >> User: Oh, thank you!
# DialoGPT: I have a pair of the same shoes that I'm wearing right now . They're amazing !
# >> User: How much is the shoes?
# DialoGPT: I have those same shoes !
```
# 权重来源
https://huggingface.co/microsoft/DialoGPT-small

View File

@@ -0,0 +1,7 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-small/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-small/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-small/tokenizer_config.json",
"merges_file":"https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-small/merges.txt",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/microsoft-DialoGPT-small/vocab.json"
}

View File

@@ -0,0 +1,76 @@
# 详细介绍
**介绍**:nlptown-bert-base-multilingual-uncased-sentiment是一个带有序列分类头的多语言BERT模型,该模型可用于对英语、荷兰语、德语、法语、西班牙语和意大利语这六种语言的商品评论进行情感分析。其中评论的情感标签为1-5之间的星级。
**模型结构**: **`BertForSequenceClassification`**,带有序列分类头的Bert模型。
**适用下游任务**:**情感分类**,该权重已经在下游`Sentiment classification`任务上进行了微调,因此可直接使用。
## 训练数据
以下是用于微调模型的商品评论数量:
| Language | Number of reviews |
| -------- | ----------------- |
| English | 150k |
| Dutch | 80k |
| German | 137k |
| French | 140k |
| Italian | 72k |
| Spanish | 50k |
## 指标
微调后的模型在每种语言的 5,000 条商品评论中获得了以下准确率:
- Accuracy (exact) 完全匹配。
- Accuracy (off-by-1) 是模型预测的评分等级与人工给出的评分等级差值小于等于 1 所占的百分比。
| Language | Accuracy (exact) | Accuracy (off-by-1) |
| -------- | ---------------- | ------------------- |
| English | 67% | 95% |
| Dutch | 57% | 93% |
| German | 61% | 94% |
| French | 59% | 94% |
| Italian | 59% | 95% |
| Spanish | 58% | 95% |
## 联系方式
对于类似模型的问题、反馈和/或请求,请联系 [NLP Town](https://www.nlp.town)。
# 使用示例
```python
import paddle
import paddle.nn.functional as F
from paddlenlp.transformers import BertForSequenceClassification, BertTokenizer
model = BertForSequenceClassification.from_pretrained(path)
model.eval()
path = "junnyu/nlptown-bert-base-multilingual-uncased-sentiment"
tokenizer = BertTokenizer.from_pretrained(path)
text = "I like you. I love you"
inputs = {
k: paddle.to_tensor(
v, dtype="int64").unsqueeze(0)
for k, v in tokenizer(text).items()
}
with paddle.no_grad():
score = F.softmax(model(**inputs), axis=-1)
id2label = {
0: "1 star",
1: "2 stars",
2: "3 stars",
3: "4 stars",
4: "5 stars"
}
for i, s in enumerate(score[0].tolist()):
label = id2label[i]
print(f"{label} score {s}")
# 1 star score 0.0021950288210064173
# 2 stars score 0.0022533712908625603
# 3 stars score 0.015475980937480927
# 4 stars score 0.1935628354549408
# 5 stars score 0.7865128517150879
```
# 权重来源
https://huggingface.co/nlptown/bert-base-multilingual-uncased-sentiment

View File

@@ -0,0 +1,6 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/nlptown-bert-base-multilingual-uncased-sentiment/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/nlptown-bert-base-multilingual-uncased-sentiment/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/nlptown-bert-base-multilingual-uncased-sentiment/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/nlptown-bert-base-multilingual-uncased-sentiment/vocab.txt"
}

View File

@@ -0,0 +1,63 @@
# 详细介绍
**介绍**:tbs17-MathBERT是一个数学领域的BERT模型,它以自监督的方式在大量英语数学语料库数据上进行了预训练。
预训练过程有两个目标:
- 掩码语言建模 (MLM):取一个句子,模型随机掩码输入中 15% 的单词,然后通过模型运行整个掩码句子,并必须预测掩码单词。这与传统的循环神经网络 (RNN) 不同,后者通常一个接一个地看到单词,或者与 GPT 等自回归模型不同,后者在内部掩盖了未来的标记。它允许模型学习句子的双向表示。
- 下一句预测 (NSP):模型在预训练期间连接两个掩码句子作为输入。有时它们对应于原文中相邻的句子,有时不对应。然后该模型必须预测两个句子是否相互跟随。通过这种方式,模型学习了数学语言的内部表示,然后可用于提取对下游任务有用的特征:例如,如果您有一个标记句子的数据集,您可以使用 MathBERT 生成的特征训练标准分类器模型作为输入。
**模型结构**: **`BertForPretraining`**,带有`MLM`和`NSP`任务的Bert模型。
**适用下游任务**:**数学领域相关的任务**,如:与数学领域相关的`句子级别分类`,`token级别分类`,`问答`等。
## 训练数据
- pre-k 到 HS 数学课程(engageNY、Utah Math、Illustrative Math)
- openculture.com 的大学数学书籍
- arxiv 数学论文摘要
# 使用示例
```python
import paddle
import paddle.nn.functional as F
from paddlenlp.transformers import BertForPretraining, BertTokenizer
path = "junnyu/tbs17-MathBERT"
model = BertForPretraining.from_pretrained(path)
tokenizer = BertTokenizer.from_pretrained(path)
model.eval()
text = "students apply these new understandings as they reason about and perform decimal [MASK] through the hundredths place."
tokens = ["[CLS]"]
text_list = text.split("[MASK]")
for i, t in enumerate(text_list):
tokens.extend(tokenizer.tokenize(t))
if i == len(text_list) - 1:
tokens.extend(["[SEP]"])
else:
tokens.extend(["[MASK]"])
input_ids_list = tokenizer.convert_tokens_to_ids(tokens)
input_ids = paddle.to_tensor([input_ids_list])
with paddle.no_grad():
pd_outputs = model(input_ids)[0][0]
pd_outputs_sentence = "paddle: "
for i, id in enumerate(input_ids_list):
if id == tokenizer.convert_tokens_to_ids(["[MASK]"])[0]:
scores, index = F.softmax(pd_outputs[i],
-1).topk(5)
tokens = tokenizer.convert_ids_to_tokens(index.tolist())
outputs = []
for score, tk in zip(scores.tolist(), tokens):
outputs.append(f"{tk}={score}")
pd_outputs_sentence += "[" + "||".join(outputs) + "]" + " "
else:
pd_outputs_sentence += "".join(
tokenizer.convert_ids_to_tokens(
[id], skip_special_tokens=True)) + " "
print(pd_outputs_sentence)
paddle: students apply these new understanding ##s as they reason about and perform decimal [numbers=0.8327996134757996||##s=0.0865364819765091||operations=0.0313422717154026||placement=0.019931407645344734||places=0.01254698634147644] through the hundred ##ths place .
```
# 权重来源
https://huggingface.co/tbs17/MathBERT

View File

@@ -0,0 +1,6 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/tbs17-MathBERT/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/tbs17-MathBERT/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/tbs17-MathBERT/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/tbs17-MathBERT/vocab.txt"
}

View File

@@ -0,0 +1,54 @@
# 详细介绍
# Chinese Poem GPT2 Model
**介绍**: 该模型用于生成中国古诗词。训练数据包含 80 万首中国古诗词,由 chinese-poetry 和 Poetry 项目收集。
**模型结构**: **`GPTLMHeadModel`**,GPT模型。
**适用下游任务**:**诗歌文本生成**。
# 使用示例
```python
import numpy as np
import paddle
from paddlenlp.transformers import BertTokenizer, GPTForGreedyGeneration
from paddlenlp.utils.log import logger
class Demo:
def __init__(self, model_name_or_path="junnyu/uer-gpt2-chinese-poem", max_predict_len=32):
self.tokenizer = BertTokenizer.from_pretrained(model_name_or_path)
logger.info("Loading the model parameters, please wait...")
self.model = GPTForGreedyGeneration.from_pretrained(
model_name_or_path,
max_predict_len=max_predict_len,
eol_token_id=self.tokenizer.pad_token_id,
)
self.model.eval()
logger.info("Model loaded.")
# prediction function
@paddle.no_grad()
def dictation_poetry_cn(self, front):
# don't add [SEP] token.
ids = self.tokenizer(front)["input_ids"][:-1]
input_ids = paddle.to_tensor(np.array(ids).reshape(1, -1).astype("int64"))
out = self.model(input_ids)
out = [int(x) for x in out.numpy().reshape([-1])]
logger.info(
self.tokenizer.convert_tokens_to_string(
self.tokenizer.convert_ids_to_tokens(out)
)
)
demo = Demo(model_name_or_path="junnyu/uer-gpt2-chinese-poem")
demo.dictation_poetry_cn("大漠")
# [CLS] 大 漠 风 沙 暗 , 长 城 日 月 寒 。 汉 兵 天 上 至 , 胡 马 雪 中 看 。 壮 士 心 逾 勇 , 孤 军 气 不 残
```
# 权重来源
https://huggingface.co/uer/gpt2-chinese-poem

View File

@@ -0,0 +1,6 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/uer-gpt2-chinese-poem/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/uer-gpt2-chinese-poem/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/uer-gpt2-chinese-poem/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/junnyu/uer-gpt2-chinese-poem/vocab.txt"
}

View File

@@ -0,0 +1,214 @@
# Copyright (c) 2021 PaddlePaddle Authors. 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 collections import OrderedDict
import argparse
import paddle
import torch
import os
import json
from paddle.utils.download import get_path_from_url
huggingface_to_paddle = {
"embeddings.LayerNorm": "embeddings.layer_norm",
"encoder.layer": "encoder.layers",
"attention.self.query": "self_attn.q_proj",
"attention.self.key": "self_attn.k_proj",
"attention.self.value": "self_attn.v_proj",
"attention.output.dense": "self_attn.out_proj",
"intermediate.dense": "linear1",
"output.dense": "linear2",
"attention.output.LayerNorm": "norm1",
"output.LayerNorm": "norm2",
"qa_outputs": 'classifier',
'lm_head.bias': 'lm_head.decoder.bias'
}
convert_model_name_list = [
"roberta-base",
"roberta-large",
"deepset/roberta-base-squad2",
"uer/roberta-base-finetuned-chinanews-chinese",
"sshleifer/tiny-distilroberta-base",
"uer/roberta-base-finetuned-cluener2020-chinese",
"uer/roberta-base-chinese-extractive-qa",
]
link_template = "https://huggingface.co/{}/resolve/main/pytorch_model.bin"
pretrained_init_configuration = {
"roberta-base": {
"attention_probs_dropout_prob": 0.1,
"layer_norm_eps": 1e-05,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 768,
"initializer_range": 0.02,
"intermediate_size": 3072,
"max_position_embeddings": 514,
"num_attention_heads": 12,
"num_hidden_layers": 12,
"pad_token_id": 1,
"type_vocab_size": 1,
"vocab_size": 50265
},
"roberta-large": {
"attention_probs_dropout_prob": 0.1,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 1024,
"initializer_range": 0.02,
"intermediate_size": 4096,
"max_position_embeddings": 514,
"num_attention_heads": 16,
"num_hidden_layers": 24,
"pad_token_id": 1,
"type_vocab_size": 1,
"layer_norm_eps": 1e-05,
"vocab_size": 50265
},
"deepset/roberta-base-squad2": {
"layer_norm_eps": 1e-05,
"attention_probs_dropout_prob": 0.1,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 768,
"initializer_range": 0.02,
"intermediate_size": 3072,
"max_position_embeddings": 514,
"num_attention_heads": 12,
"num_hidden_layers": 12,
"pad_token_id": 1,
"type_vocab_size": 1,
"vocab_size": 50265
},
"uer/roberta-base-finetuned-chinanews-chinese": {
"attention_probs_dropout_prob": 0.1,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 768,
"initializer_range": 0.02,
"intermediate_size": 3072,
"layer_norm_eps": 1e-12,
"max_position_embeddings": 512,
"num_attention_heads": 12,
"num_hidden_layers": 12,
"pad_token_id": 0,
"type_vocab_size": 2,
"vocab_size": 21128
},
"sshleifer/tiny-distilroberta-base": {
"attention_probs_dropout_prob": 0.1,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 2,
"initializer_range": 0.02,
"intermediate_size": 2,
"layer_norm_eps": 1e-05,
"max_position_embeddings": 514,
"num_attention_heads": 2,
"num_hidden_layers": 2,
"pad_token_id": 1,
"type_vocab_size": 1,
"vocab_size": 50265
},
"uer/roberta-base-finetuned-cluener2020-chinese": {
"attention_probs_dropout_prob": 0.1,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 768,
"initializer_range": 0.02,
"intermediate_size": 3072,
"layer_norm_eps": 1e-12,
"max_position_embeddings": 512,
"num_attention_heads": 12,
"num_hidden_layers": 12,
"pad_token_id": 0,
"type_vocab_size": 2,
"vocab_size": 21128
},
"uer/roberta-base-chinese-extractive-qa": {
"attention_probs_dropout_prob": 0.1,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 768,
"initializer_range": 0.02,
"intermediate_size": 3072,
"layer_norm_eps": 1e-12,
"max_position_embeddings": 512,
"num_attention_heads": 12,
"num_hidden_layers": 12,
"pad_token_id": 0,
"type_vocab_size": 2,
"vocab_size": 21128
}
}
def convert_pytorch_checkpoint_to_paddle(pytorch_src_base_path,
paddle_dump_base_path):
for model_name in convert_model_name_list:
model_state_url = link_template.format(model_name)
paddle_dump_path = os.path.join(paddle_dump_base_path,
model_name.split('/')[-1])
if os.path.exists(
os.path.join(paddle_dump_path, 'model_state.pdparams')):
continue
if not os.path.exists(paddle_dump_path):
os.makedirs(paddle_dump_path)
with open(os.path.join(paddle_dump_path, 'model_config.json'),
'w') as fw:
json.dump(pretrained_init_configuration[model_name], fw)
_ = get_path_from_url(model_state_url, paddle_dump_path)
pytorch_checkpoint_path = os.path.join(paddle_dump_path,
'pytorch_model.bin')
pytorch_state_dict = torch.load(
pytorch_checkpoint_path, map_location="cpu")
paddle_state_dict = OrderedDict()
for k, v in pytorch_state_dict.items():
is_transpose = False
if k[-7:] == ".weight":
if ".embeddings." not in k and ".LayerNorm." not in k:
if v.ndim == 2:
v = v.transpose(0, 1)
is_transpose = True
oldk = k
if k == 'lm_head.bias' and 'lm_head.decoder.bias' in pytorch_state_dict.keys(
):
continue
for huggingface_name, paddle_name in huggingface_to_paddle.items():
k = k.replace(huggingface_name, paddle_name)
if k[:5] == 'bert.':
k = k.replace('bert.', 'roberta.')
print(f"Converting: {oldk} => {k} | is_transpose {is_transpose}")
paddle_state_dict[k] = v.data.numpy()
del pytorch_state_dict
paddle_dump_path = os.path.join(paddle_dump_path,
'model_state.pdparams')
paddle.save(paddle_state_dict, paddle_dump_path)
if __name__ == "__main__":
pytorch_src_base_path = os.path.dirname(os.path.realpath(__file__))
paddle_dump_base_path = pytorch_src_base_path
convert_pytorch_checkpoint_to_paddle(pytorch_src_base_path,
paddle_dump_base_path)

View File

@@ -0,0 +1,108 @@
## deepset/roberta-base-squad2
是一个QA_model,在squad2上训练
模型来源:https://huggingface.co/deepset/roberta-base-squad2
使用示例:
```python
from paddlenlp.transformers import (
RobertaModel, RobertaForMaskedLM, RobertaForQuestionAnswering,
RobertaForSequenceClassification, RobertaForTokenClassification)
from paddlenlp.transformers import RobertaTokenizer
import paddle
import os
import numpy as np
def decode(start, end, topk, max_answer_len, undesired_tokens):
"""
Take the output of any :obj:`ModelForQuestionAnswering` and will generate probabilities for each span to be the
actual answer.
"""
# Ensure we have batch axis
if start.ndim == 1:
start = start[None]
if end.ndim == 1:
end = end[None]
# Compute the score of each tuple(start, end) to be the real answer
outer = np.matmul(np.expand_dims(start, -1), np.expand_dims(end, 1))
# Remove candidate with end < start and end - start > max_answer_len
candidates = np.tril(np.triu(outer), max_answer_len - 1)
# Inspired by Chen & al. (https://github.com/facebookresearch/DrQA)
scores_flat = candidates.flatten()
if topk == 1:
idx_sort = [np.argmax(scores_flat)]
elif len(scores_flat) < topk:
idx_sort = np.argsort(-scores_flat)
else:
idx = np.argpartition(-scores_flat, topk)[0:topk]
idx_sort = idx[np.argsort(-scores_flat[idx])]
starts, ends = np.unravel_index(idx_sort, candidates.shape)[1:]
desired_spans = np.isin(starts, undesired_tokens.nonzero()) & np.isin(
ends, undesired_tokens.nonzero())
starts = starts[desired_spans]
ends = ends[desired_spans]
scores = candidates[0, starts, ends]
return starts, ends, scores
tokenizer = RobertaTokenizer.from_pretrained('deepset_roberta_base_squad2')
questions = ['Where do I live?']
contexts = ['My name is Sarah and I live in London']
token = tokenizer(
questions,
contexts,
stride=128,
max_seq_len=64,
return_attention_mask=True,
return_special_tokens_mask=True)
# print(token)
special_tokens_mask = token[0]['special_tokens_mask']
count = 3
st_idx = 0
for i in special_tokens_mask:
st_idx += 1
if i == 1:
count -= 1
if count == 0:
break
input_ids = token[0]['input_ids']
offset_mapping = token[0]['offset_mapping']
input_ids = paddle.to_tensor(input_ids, dtype='int64').unsqueeze(0)
model = RobertaForQuestionAnswering.from_pretrained(path)
model.eval()
start, end = model(input_ids=input_ids)
start_ = start[0].numpy()
end_ = end[0].numpy()
undesired_tokens = np.ones_like(input_ids[0].numpy())
undesired_tokens[1:st_idx] = 0
undesired_tokens[-1] = 0
# Generate mask
undesired_tokens_mask = undesired_tokens == 0.0
# Make sure non-context indexes in the tensor cannot contribute to the softmax
start_ = np.where(undesired_tokens_mask, -10000.0, start_)
end_ = np.where(undesired_tokens_mask, -10000.0, end_)
start_ = np.exp(start_ - np.log(
np.sum(np.exp(start_), axis=-1, keepdims=True)))
end_ = np.exp(end_ - np.log(np.sum(np.exp(end_), axis=-1, keepdims=True)))
start_idx, end_idx, score = decode(start_, end_, 1, 64, undesired_tokens)
start_idx, end_idx = offset_mapping[start_idx[0]][0], offset_mapping[
end_idx[0]][1]
print("ans: {}".format(contexts[0][start_idx:end_idx]),
'score:{}'.format(score.item()))
'''
ans: London score:0.7772307395935059
'''

View File

@@ -0,0 +1,7 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/deepset_roberta_base_squad2/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/deepset_roberta_base_squad2/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/deepset_roberta_base_squad2/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/deepset_roberta_base_squad2/vocab.json",
"merges_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/deepset_roberta_base_squad2/merges.txt"
}

View File

@@ -0,0 +1,57 @@
## roberta-base
权重来源
https://huggingface.co/roberta-base
在英文数据集上预训练的roberta-base MaskedLM模型
```python
from paddlenlp.transformers import (
RobertaModel, RobertaForMaskedLM, RobertaForQuestionAnswering,
RobertaForSequenceClassification, RobertaForTokenClassification)
from paddlenlp.transformers import RobertaBPETokenizer, RobertaTokenizer
import paddle
import os
import numpy as np
model = RobertaForMaskedLM.from_pretrained('roberta-base')
tokenizer = RobertaBPETokenizer.from_pretrained('roberta-base')
text = ["The man worked as a", "."] #"The man worked as a <mask>."
tokens_list = []
for i in range(2):
tokens_list.append(tokenizer.tokenize(text[i]))
tokens = ['<s>']
tokens.extend(tokens_list[0])
tokens.extend(['<mask>'])
tokens.extend(tokens_list[1])
tokens.extend(['</s>'])
token_ids = tokenizer.convert_tokens_to_ids(tokens)
# print(token_ids)
model.eval()
input_ids = paddle.to_tensor([token_ids])
with paddle.no_grad():
pd_outputs = model(input_ids)
pd_outputs = pd_outputs[0]
pd_outputs_sentence = "paddle: "
for i, id in enumerate(token_ids):
if id == 50264:
scores, index = paddle.nn.functional.softmax(pd_outputs[i],
-1).topk(5)
tokens = tokenizer.convert_ids_to_tokens(index.tolist())
outputs = []
for score, tk in zip(scores.tolist(), tokens):
outputs.append(f"{tk}={score}")
pd_outputs_sentence += "[" + "||".join(outputs) + "]" + " "
else:
pd_outputs_sentence += "".join(
tokenizer.convert_ids_to_tokens(
[id], skip_special_tokens=True)) + " "
print(pd_outputs_sentence)
'''
paddle: The Ġman Ġworked Ġas Ġa [Ġmechanic=0.08702345192432404||Ġwaiter=0.08196478337049484||Ġbutcher=0.07332248240709305||Ġminer=0.046321991831064224||Ġguard=0.040149785578250885] .
'''

View File

@@ -0,0 +1,7 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/roberta_en_base/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/roberta_en_base/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/roberta_en_base/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/roberta_en_base/vocab.json",
"merges_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/roberta_en_base/merges.txt"
}

View File

@@ -0,0 +1,57 @@
## roberta-large
权重来源
https://huggingface.co/roberta-large
在英文数据集上预训练的roberta-base MaskedLM模型
```python
from paddlenlp.transformers import (
RobertaModel, RobertaForMaskedLM, RobertaForQuestionAnswering,
RobertaForSequenceClassification, RobertaForTokenClassification)
from paddlenlp.transformers import RobertaBPETokenizer, RobertaTokenizer
import paddle
import os
import numpy as np
model = RobertaForMaskedLM.from_pretrained('roberta-large')
tokenizer = RobertaBPETokenizer.from_pretrained('roberta-large')
text = ["The man worked as a", "."] #"The man worked as a <mask>."
tokens_list = []
for i in range(2):
tokens_list.append(tokenizer.tokenize(text[i]))
tokens = ['<s>']
tokens.extend(tokens_list[0])
tokens.extend(['<mask>'])
tokens.extend(tokens_list[1])
tokens.extend(['</s>'])
token_ids = tokenizer.convert_tokens_to_ids(tokens)
# print(token_ids)
model.eval()
input_ids = paddle.to_tensor([token_ids])
with paddle.no_grad():
pd_outputs = model(input_ids)
pd_outputs = pd_outputs[0]
pd_outputs_sentence = "paddle: "
for i, id in enumerate(token_ids):
if id == 50264:
scores, index = paddle.nn.functional.softmax(pd_outputs[i],
-1).topk(5)
tokens = tokenizer.convert_ids_to_tokens(index.tolist())
outputs = []
for score, tk in zip(scores.tolist(), tokens):
outputs.append(f"{tk}={score}")
pd_outputs_sentence += "[" + "||".join(outputs) + "]" + " "
else:
pd_outputs_sentence += "".join(
tokenizer.convert_ids_to_tokens(
[id], skip_special_tokens=True)) + " "
print(pd_outputs_sentence)
'''
paddle: The Ġman Ġworked Ġas Ġa [Ġmechanic=0.08260241150856018||Ġdriver=0.05736102908849716||Ġteacher=0.047089140862226486||Ġbartender=0.04641677811741829||Ġwaiter=0.04239342361688614] .
'''

View File

@@ -0,0 +1,7 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/roberta_en_large/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/roberta_en_large/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/roberta_en_large/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/roberta_en_large/vocab.json",
"merges_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/roberta_en_large/merges.txt"
}

View File

@@ -0,0 +1,57 @@
## sshleifer/tiny-distilroberta-base
权重来源
https://huggingface.co/sshleifer/tiny-distilroberta-base
在英文数据集上预训练的tiny-distilroberta-base MaskedLM模型
```python
from paddlenlp.transformers import (
RobertaModel, RobertaForMaskedLM, RobertaForQuestionAnswering,
RobertaForSequenceClassification, RobertaForTokenClassification)
from paddlenlp.transformers import RobertaBPETokenizer, RobertaTokenizer
import paddle
import os
import numpy as np
model = RobertaForMaskedLM.from_pretrained('sshleifer/tiny-distilroberta-base')
tokenizer = RobertaBPETokenizer.from_pretrained('sshleifer/tiny-distilroberta-base')
text = ["The man worked as a", "."] #"The man worked as a <mask>."
tokens_list = []
for i in range(2):
tokens_list.append(tokenizer.tokenize(text[i]))
tokens = ['<s>']
tokens.extend(tokens_list[0])
tokens.extend(['<mask>'])
tokens.extend(tokens_list[1])
tokens.extend(['</s>'])
token_ids = tokenizer.convert_tokens_to_ids(tokens)
# print(token_ids)
model.eval()
input_ids = paddle.to_tensor([token_ids])
with paddle.no_grad():
pd_outputs = model(input_ids)
pd_outputs = pd_outputs[0]
pd_outputs_sentence = "paddle: "
for i, id in enumerate(token_ids):
if id == 50264:
scores, index = paddle.nn.functional.softmax(pd_outputs[i],
-1).topk(5)
tokens = tokenizer.convert_ids_to_tokens(index.tolist())
outputs = []
for score, tk in zip(scores.tolist(), tokens):
outputs.append(f"{tk}={score}")
pd_outputs_sentence += "[" + "||".join(outputs) + "]" + " "
else:
pd_outputs_sentence += "".join(
tokenizer.convert_ids_to_tokens(
[id], skip_special_tokens=True)) + " "
print(pd_outputs_sentence)
'''
paddle: The Ġman Ġworked Ġas Ġa [ĠMaul=2.2346646801452152e-05||ELS=2.2343869204632938e-05||Ġirregular=2.223450610472355e-05||ĠDr=2.2128451746539213e-05||³=2.2114407329354435e-05] .
'''

View File

@@ -0,0 +1,7 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/sshleifer_tiny_distilroberta_base/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/sshleifer_tiny_distilroberta_base/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/sshleifer_tiny_distilroberta_base/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/sshleifer_tiny_distilroberta_base/vocab.json",
"merges_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/sshleifer_tiny_distilroberta_base/merges.txt"
}

View File

@@ -0,0 +1,93 @@
## uer/roberta-base-chinese-extractive-qa
在中文数据集上训练roberta-base-qa model.
权重来源:https://huggingface.co/uer/roberta-base-chinese-extractive-qa
```python
from paddlenlp.transformers import (
RobertaModel, RobertaForMaskedLM, RobertaForQuestionAnswering,
RobertaForSequenceClassification, RobertaForTokenClassification)
from paddlenlp.transformers import RobertaBPETokenizer, RobertaTokenizer
import paddle
import os
import numpy as np
def decode(start, end, topk, max_answer_len, undesired_tokens):
"""
Take the output of any :obj:`ModelForQuestionAnswering` and will generate probabilities for each span to be the
actual answer.
"""
# Ensure we have batch axis
if start.ndim == 1:
start = start[None]
if end.ndim == 1:
end = end[None]
# Compute the score of each tuple(start, end) to be the real answer
outer = np.matmul(np.expand_dims(start, -1), np.expand_dims(end, 1))
# Remove candidate with end < start and end - start > max_answer_len
candidates = np.tril(np.triu(outer), max_answer_len - 1)
# Inspired by Chen & al. (https://github.com/facebookresearch/DrQA)
scores_flat = candidates.flatten()
if topk == 1:
idx_sort = [np.argmax(scores_flat)]
elif len(scores_flat) < topk:
idx_sort = np.argsort(-scores_flat)
else:
idx = np.argpartition(-scores_flat, topk)[0:topk]
idx_sort = idx[np.argsort(-scores_flat[idx])]
starts, ends = np.unravel_index(idx_sort, candidates.shape)[1:]
desired_spans = np.isin(starts, undesired_tokens.nonzero()) & np.isin(
ends, undesired_tokens.nonzero())
starts = starts[desired_spans]
ends = ends[desired_spans]
scores = candidates[0, starts, ends]
return starts, ends, scores
tokenizer = RobertaTokenizer.from_pretrained('uer/roberta-base-chinese-extractive-qa')
questions = ['著名诗歌《假如生活欺骗了你》的作者是']
contexts = [
'普希金从那里学习人民的语言,吸取了许多有益的养料,这一切对普希金后来的创作产生了很大的影响。这两年里,普希金创作了不少优秀的作品,如《囚徒》、《致大海》、《致凯恩》和《假如生活欺骗了你》等几十首抒情诗,叙事诗《努林伯爵》,历史剧《鲍里斯·戈都诺夫》,以及《叶甫盖尼·奥涅金》前六章。'
]
token = tokenizer(questions, contexts, stride=256, max_seq_len=256)
token_type_ids = token[0]["token_type_ids"]
offset_mapping = token[0]["offset_mapping"]
st_idx = len(token_type_ids) - sum(token_type_ids)
model = RobertaForQuestionAnswering.from_pretrained('uer/roberta-base-chinese-extractive-qa')
model.eval()
input_ids = paddle.to_tensor(
token[0]['input_ids'], dtype='int64').unsqueeze(0)
token_type_ids = paddle.to_tensor(
token[0]['token_type_ids'], dtype='int64').unsqueeze(0)
with paddle.no_grad():
start, end = model(input_ids, token_type_ids)
start_ = start[0].numpy()
end_ = end[0].numpy()
undesired_tokens = np.ones_like(input_ids[0].numpy())
undesired_tokens[1:st_idx] = 0
undesired_tokens[-1] = 0
# Generate mask
undesired_tokens_mask = undesired_tokens == 0.0
# Make sure non-context indexes in the tensor cannot contribute to the softmax
start_ = np.where(undesired_tokens_mask, -10000.0, start_)
end_ = np.where(undesired_tokens_mask, -10000.0, end_)
start_ = np.exp(start_ - np.log(
np.sum(np.exp(start_), axis=-1, keepdims=True)))
end_ = np.exp(end_ - np.log(np.sum(np.exp(end_), axis=-1, keepdims=True)))
start_idx, end_idx, score = decode(start_, end_, 1, 64, undesired_tokens)
start_idx, end_idx = offset_mapping[start_idx[0]][0], offset_mapping[
end_idx[0]][1]
print("ans: {}".format(contexts[0][start_idx:end_idx]),
'score:{}'.format(score.item()))
'''
ans: 普希金 score:0.9766426086425781
'''

View File

@@ -0,0 +1,6 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/uer_roberta_base_chinese_extractive_qa/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/uer_roberta_base_chinese_extractive_qa/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/uer_roberta_base_chinese_extractive_qa/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/uer_roberta_base_chinese_extractive_qa/vocab.txt"
}

View File

@@ -0,0 +1,40 @@
## uer/roberta-base-finetuned-chinanews-chinese
在chinanews数据集上微调的新闻分类模型
```python
tokenizer = RobertaTokenizer.from_pretrained('uer/roberta-base-finetuned-chinanews-chinese')
token = tokenizer('北京上个月召开了两会')
# print(token)
config = RobertaModel.pretrained_init_configuration[
'uer/roberta-base-finetuned-chinanews-chinese']
roberta = RobertaModel(**config)
model = RobertaForSequenceClassification(roberta, 7)
model_state = paddle.load(os.path.join(path, "model_state.pdparams"))
model.load_dict(model_state)
model.eval()
input_ids = paddle.to_tensor(token['input_ids'], dtype='int64').unsqueeze(0)
with paddle.no_grad():
output = model(input_ids)
import paddle.nn.functional as F
output = F.softmax(output)
id2label = {
"0": "mainland China politics",
"1": "Hong Kong - Macau politics",
"2": "International news",
"3": "financial news",
"4": "culture",
"5": "entertainment",
"6": "sports"
}
for i in range(7):
print("{}: \t {}".format(id2label[str(i)], output[0][i].item()))
'''
mainland China politics: 0.7211663722991943
Hong Kong - Macau politics: 0.0015174372820183635
International news: 0.00030049943597987294
financial news: 0.1901436597108841
culture: 0.0707709938287735
entertainment: 0.007615766953676939
sports: 0.008485228754580021
'''

View File

@@ -0,0 +1,6 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/uer_roberta_base_finetuned_chinanews_chinese/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/uer_roberta_base_finetuned_chinanews_chinese/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/uer_roberta_base_finetuned_chinanews_chinese/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/uer_roberta_base_finetuned_chinanews_chinese/vocab.txt"
}

View File

@@ -0,0 +1,88 @@
## uer/roberta-base-finetuned-cluener2020-chinese
权重来源:https://huggingface.co/uer/roberta-base-finetuned-cluener2020-chinese
```python
from paddlenlp.transformers import (
RobertaModel, RobertaForMaskedLM, RobertaForQuestionAnswering,
RobertaForSequenceClassification, RobertaForTokenClassification)
from paddlenlp.transformers import RobertaBPETokenizer, RobertaTokenizer
import paddle
import os
import numpy as np
text = '江苏警方通报特斯拉冲进店铺'
tokenizer = RobertaTokenizer.from_pretrained('uer/roberta-base-finetuned-cluener2020-chinese')
token = tokenizer(text)
config = RobertaModel.pretrained_init_configuration[
'uer/roberta-base-finetuned-cluener2020-chinese']
roberta = RobertaModel(**config)
model = RobertaForTokenClassification(roberta, 32)
model_state = paddle.load(os.path.join(path, "model_state.pdparams"))
model.load_dict(model_state)
model.eval()
input_ids = paddle.to_tensor(token['input_ids'], dtype='int64').unsqueeze(0)
with paddle.no_grad():
output = model(input_ids)
import paddle.nn.functional as F
output = F.softmax(output)
id2label = {
"0": "O",
"1": "B-address",
"2": "I-address",
"3": "B-book",
"4": "I-book",
"5": "B-company",
"6": "I-company",
"7": "B-game",
"8": "I-game",
"9": "B-government",
"10": "I-government",
"11": "B-movie",
"12": "I-movie",
"13": "B-name",
"14": "I-name",
"15": "B-organization",
"16": "I-organization",
"17": "B-position",
"18": "I-position",
"19": "B-scene",
"20": "I-scene",
"21": "S-address",
"22": "S-book",
"23": "S-company",
"24": "S-game",
"25": "S-government",
"26": "S-movie",
"27": "S-name",
"28": "S-organization",
"29": "S-position",
"30": "S-scene",
"31": "[PAD]"
}
tokenized_text = tokenizer.tokenize(text)
scores = []
char_cn = []
for t, s in zip(tokenized_text, output[0][1:-1]):
index = paddle.argmax(s).item()
label = id2label[str(index)]
if index != 0:
scores.append(s[index].item())
char_cn.append(t)
print(f"{label} {t} score {s[index].item()}")
print("{}:{}".format("".join(char_cn[:2]), sum(scores[:2]) / 2))
print("{}:{}".format("".join(char_cn[2:]), sum(scores[2:]) / 3))
'''
B-address 江 score 0.6618999242782593
I-address 苏 score 0.5544551610946655
B-company 特 score 0.4227274954319
I-company 斯 score 0.45469844341278076
I-company 拉 score 0.5207833051681519
江苏:0.6081775426864624
特斯拉:0.46606974800427753
'''

View File

@@ -0,0 +1,6 @@
{
"model_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/uer_roberta_base_finetuned_cluener2020_chinese/model_config.json",
"model_state": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/uer_roberta_base_finetuned_cluener2020_chinese/model_state.pdparams",
"tokenizer_config_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/uer_roberta_base_finetuned_cluener2020_chinese/tokenizer_config.json",
"vocab_file": "https://bj.bcebos.com/paddlenlp/models/transformers/community/nosaydomore/uer_roberta_base_finetuned_cluener2020_chinese/vocab.txt"
}

View File

@@ -0,0 +1,12 @@
# Convert tp paddle
1. download checkpoints from huggingface model hub
2. modify model path and run script blow.
```bash
# path1: sshleifertiny-distilbert-base-uncased-finetuned-sst-2-english
# path2: distilbert-base-multilingual-cased
export path1='sshleifertiny-distilbert-base-uncased-finetuned-sst-2-english/pytorch_model.bin'
export path2='distilbert-base-multilingual-cased/pytorch_model.bin'
python convert_to_paddle.py \
--sshleifertiny_model_path $path1 \
--base_model_path $path2
```

View File

@@ -0,0 +1,101 @@
# Copyright (c) 2021 PaddlePaddle Authors. 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 collections import OrderedDict
import argparse
import numpy as np
huggingface_to_paddle1 = {
"embeddings.LayerNorm": "embeddings.layer_norm",
"transformer.layer": "encoder.layers",
"attention.q_lin": "self_attn.q_proj",
"attention.k_lin": "self_attn.k_proj",
"attention.v_lin": "self_attn.v_proj",
"attention.out_lin": "self_attn.out_proj",
"ffn.lin1": "linear1",
"ffn.lin2": "linear2",
"sa_layer_norm": "norm1",
"output_layer_norm": "norm2",
}
huggingface_to_paddle2 = {
"bert": "distilbert",
"embeddings.LayerNorm": "embeddings.layer_norm",
"encoder.layer": "encoder.layers",
"attention.self.query": "self_attn.q_proj",
"attention.self.key": "self_attn.k_proj",
"attention.self.value": "self_attn.v_proj",
"attention.output.dense": "self_attn.out_proj",
"intermediate.dense": "linear1",
"output.dense": "linear2",
"attention.output.LayerNorm": "norm1",
"output.LayerNorm": "norm2",
"predictions.decoder.": "predictions.decoder_",
"predictions.transform.dense": "predictions.transform",
"predictions.transform.LayerNorm": "predictions.layer_norm",
}
def convert_pytorch_checkpoint_to_paddle(pytorch_checkpoint_path,
huggingface_to_paddle):
import torch
import paddle
pytorch_state_dict = torch.load(pytorch_checkpoint_path, map_location="cpu")
paddle_state_dict = OrderedDict()
Total_params = 0
for k, v in pytorch_state_dict.items():
mulValue = np.prod(v.shape)
Total_params += mulValue
is_transpose = False
if k[-7:] == ".weight":
if ".embeddings." not in k and ".LayerNorm." not in k:
if v.ndim == 2:
v = v.transpose(0, 1)
is_transpose = True
oldk = k
for huggingface_name, paddle_name in huggingface_to_paddle.items():
k = k.replace(huggingface_name, paddle_name)
if k.startswith('distilbert.pooler.dense'):
k = k.replace('distilbert.pooler.dense', 'pre_classifier')
print(f"Converting: {oldk} => {k} | is_transpose {is_transpose}")
paddle_state_dict[k] = v.data.numpy()
paddle_dump_path = pytorch_checkpoint_path.replace('pytorch_model.bin',
'model_state.pdparams')
paddle.save(paddle_state_dict, paddle_dump_path)
print(f'Total params: {Total_params}')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--sshleifertiny_model_path",
default="D:\\paddle_models\\sshleifertiny-distilbert-base-uncased-finetuned-sst-2-english\\pytorch_model.bin",
type=str,
required=False,
help="Path to the Pytorch checkpoint path.")
parser.add_argument(
"--base_model_path",
default="D:\\paddle_models\\distilbert-base-multilingual-cased\\pytorch_model.bin",
type=str,
required=False,
help="Path to the Pytorch checkpoint path.")
args = parser.parse_args()
convert_pytorch_checkpoint_to_paddle(args.sshleifertiny_model_path,
huggingface_to_paddle2)
print()
convert_pytorch_checkpoint_to_paddle(args.base_model_path,
huggingface_to_paddle1)

Some files were not shown because too many files have changed in this diff Show More