diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0d4bb973ae87bb45ef4386a63c26ed62602f2cee..a60453ff4e3bba6e6cb3b3de915dd69afd3a1ec3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1 +1,157 @@ -./doc/howto/dev/contribute_to_paddle_en.md +# Contribute Code + +We sincerely appreciate your contribution. This document explains our workflow and work style. + +## Workflow + +PaddlePaddle uses this [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/). + +1. Clone + + To make a copy of your fork to your local computers, please run + + ```bash + git clone https://github.com/your-github-account/paddle + cd paddle + ``` + +1. 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-stuff + ``` + +1. Commit + + Before issuing your first `git commit` command, please install [`pre-commit`](http://pre-commit.com/) by running the following commands: + + ```bash + pip install pre-commit + pre-commit install + ``` + + Our pre-commit configuration requires clang-format 3.8 for auto-formating C/C++ code and yapf for Python. + + Once installed, `pre-commit` checks the style of code and documentation in every commit. We will see something like the following when you run `git commit`: + + ``` + ➜ git commit + CRLF end-lines remover...............................(no files to check)Skipped + yapf.................................................(no files to check)Skipped + Check for added large files..............................................Passed + Check for merge conflicts................................................Passed + Check for broken symlinks................................................Passed + Detect Private Key...................................(no files to check)Skipped + Fix End of Files.....................................(no files to check)Skipped + clang-formater.......................................(no files to check)Skipped + [my-cool-stuff c703c041] add test file + 1 file changed, 0 insertions(+), 0 deletions(-) + create mode 100644 233 + ``` + +1. Build and test + + Users can build PaddlePaddle natively on Linux and Mac OS X. But to unify the building environment and to make it easy for debugging, the recommended way is [using Docker](https://github.com/PaddlePaddle/Paddle/blob/develop/doc/howto/dev/build_en.md). + +1. 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/Paddle + git pull upstream develop + ``` + +1. 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/Paddle) 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/). + + If your change is for fixing an issue, please write ["Fixes "](https://help.github.com/articles/closing-issues-using-keywords/) in the description section of your pull request. Github would close the issue when the owners merge your pull request. + + Please remember to specify some reviewers for your pull request. If you don't know who are the right ones, please follow Github's recommendation. + + +1. 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 sending them the URL of your pull request via IM or email. 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"; please give a reason otherwise. + +- 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/). + +- Reduce the unnecessary commits. Some developers commit often. It is recommended to append a sequence of small changes into one commit by running `git commit --amend` instead of `git commit`. + + +## Coding Standard + +### Code Style + +Our C/C++ code follows the [Google style guide](http://google.github.io/styleguide/cppguide.html). + +Our Python code follows the [PEP8 style guide](https://www.python.org/dev/peps/pep-0008/). + +Our build process helps to check the code style. In [`build.sh`](https://github.com/PaddlePaddle/Paddle/blob/b84e8226514b8bb4405c3c28e54aa5077193d179/paddle/scripts/docker/build.sh#L42), the entry point of our [builder Docker image](https://github.com/PaddlePaddle/Paddle/blob/b84e8226514b8bb4405c3c28e54aa5077193d179/Dockerfile#L88), the CMake argument `WITH_STYLE_CHECK` is set to `ON` by default. This flag is on + +Please install pre-commit, which automatically reformat the changes to C/C++ and Python code whenever we run `git commit`. To check the whole codebase, we can run the command `pre-commit run -a`, as in the [`check_style.sh` file](https://github.com/PaddlePaddle/Paddle/blob/b84e8226514b8bb4405c3c28e54aa5077193d179/paddle/scripts/travis/check_style.sh#L30), which is invoked by [our Travis CI configuration](https://github.com/PaddlePaddle/Paddle/blob/b84e8226514b8bb4405c3c28e54aa5077193d179/.travis.yml#L43). + +### Unit Tests + +Please remember to add related unit tests. + +- For C/C++ code, please follow [`google-test` Primer](https://github.com/google/googletest/blob/master/googletest/docs/Primer.md). + +- For Python code, please use [Python's standard `unittest` package](http://pythontesting.net/framework/unittest/unittest-introduction/). + + +### Writing Logs + +We use [glog](https://github.com/google/glog) for logging in our C/C++ code. + +For general information, please use `LOG`. For debug information, please use [`VLOG`](http://htmlpreview.github.io/?https://github.com/google/glog/blob/master/doc/glog.html#verbose). The reason is at [here](https://groups.google.com/a/chromium.org/d/msg/chromium-dev/3NDNd1KzXeY/AZKMMx37fdQJ). + +`VLOG` requires a *verbose level* parameter. For example: + +```c++ +VLOG(3) << "Operator FC is taking " << num_inputs << "inputs." +``` + +When we run a PaddlePaddle application or test, we can specify a verbose threshold. For example: + +```bash +GLOG_vmodule=buddy_allocator=2 \ +GLOG_v=10 \ +python \ +../python/paddle/v2/framework/tests/test_recurrent_op.py +``` + +This will enable VLOG messages generated by `buddy_allocator.{h,cc}` and in the verbose range of 0 to 3, so you will see above example VLOG message, which is in level 3. This suggests that we output overall messages in lower verbose levels, so they display with higher probability. When coding C++, please follow the verbose level convention as follows: + +- verbose level 1: [framework](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/framework) +- verbose level 3: [operators](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/operators) +- verbose level 5: [memory](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/memory), [platform](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/platform) +- verbose level 7: [math](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/math) diff --git a/doc/howto/cross_compiling/cross_compiling_for_raspberry_cn.md b/doc/howto/cross_compiling/cross_compiling_for_raspberry_cn.md index 085b5dda1615a9af918b59870db460fcc5acdcca..026c0c6f3b2a2ca322d063f38e1736a010e1197e 100644 --- a/doc/howto/cross_compiling/cross_compiling_for_raspberry_cn.md +++ b/doc/howto/cross_compiling/cross_compiling_for_raspberry_cn.md @@ -1,39 +1,36 @@ # 构建Raspberry Pi平台上的PaddlePaddle库 -对于Rasspberry Pi系统,用户可通过ssh等方式登录到Raspberry Pi系统上,按照[源码编译PaddlePaddle](http://www.paddlepaddle.org/doc_cn/getstarted/build_and_install/cmake/build_from_source_cn.html)相关文档所述,直接编译Raspberry Pi平台上适用的PaddlePaddle库。 +通常有两个方法来构建基于 Rasspberry Pi 的版本: -用户也可以在自己熟悉的开发平台上,通过交叉编译的方式来编译。这篇文档将以Linux x86-64平台为例,介绍交叉编译Raspberry Pi平台上适用的PaddlePaddle的方法和步骤。 +1. 通过ssh等方式登录到Raspberry Pi系统上来构建。所需的开发工具和第三方库可以参考 [`/Dockerfile`](https://github.com/PaddlePaddle/Paddle/blob/develop/Dockerfile)。 -## 准备交叉编译环境 +1. 另一个方法是交叉编译。这篇文档介绍在 Linux/x64 上交叉编译Raspberry Pi平台上适用的PaddlePaddle的方法和步骤。 -从源码交叉编译PaddlePaddle,用户需要提前准备好交叉编译环境。用户可自行前往[github](https://github.com/raspberrypi/tools)下载Raspberry Pi平台使用的C/C++交叉编译工具链,也可通过以下命令获取: +## 安装交叉编译器 + +克隆下面 Github repo ```bash git clone https://github.com/raspberrypi/tools.git ``` -该github仓库中包含若干个预编译好的、针对不同平台的编译工具。宿主机是Linux x86-64环境,则需选用`arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64`下的作为编译工具,所使用的编译器为arm-linux-gnueabihf-gcc 4.8.3。 - -注意,该编译工具链需要系统glibc支持2.14以上。 +即可在 `./tools/tree/master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64` 目录里找到交叉编译器 arm-linux-gnueabihf-gcc 4.8.3。运行该编译工具链需要一台 Linux x64 机器上以及 2.14版本以上的 glibc。 ## 配置交叉编译参数 -CMake系统对交叉编译提供了支持[cmake-toolchains](https://cmake.org/cmake/help/v3.0/manual/cmake-toolchains.7.html#cross-compiling)。为了简化cmake配置,PaddlePaddle为交叉编译提供了工具链配置文档[cmake/cross_compiling/raspberry_pi.cmake](https://github.com/PaddlePaddle/Paddle/blob/develop/cmake/cross_compiling/raspberry_pi.cmake),以提供一些默认的编译器和编译参数相关配置。 +CMake[支持交叉编译](https://cmake.org/cmake/help/v3.0/manual/cmake-toolchains.7.html#cross-compiling)。PaddlePaddle for Raspberry Pi的配置信息在[cmake/cross_compiling/raspberry_pi.cmake](https://github.com/PaddlePaddle/Paddle/blob/develop/cmake/cross_compiling/raspberry_pi.cmake)。 交叉编译Raspberry Pi版本PaddlePaddle库时,有一些必须配置的参数: -- `CMAKE_SYSTEM_NAME`,CMake编译的目标平台,必须配置为`RPi`。在设置`CMAKE_SYSTEM_NAME=RPi`后,PaddlePaddle的CMake系统才认为在是在交叉编译Raspberry Pi系统的版本,并自动编译宿主机版protoc可执行文件、目标机版protobuf库、以及目标机版OpenBLAS库。 - -Raspberry Pi平台可选配置参数: +- `CMAKE_SYSTEM_NAME`:CMake编译的目标平台,必须配置为`RPi`。在设置`CMAKE_SYSTEM_NAME=RPi`后,PaddlePaddle的CMake系统才认为在是在交叉编译Raspberry Pi系统的版本,并自动编译宿主机版protoc可执行文件、目标机版protobuf库、以及目标机版OpenBLAS库。 -- `RPI_TOOLCHAIN`,编译工具链所在的绝对路径,或者相对于构建目录的相对路径。PaddlePaddle的CMake系统将根据该值自动设置需要使用的交叉编译器;否则,用户需要在cmake时手动设置这些值。无默认值。 -- `RPI_ARM_NEON`,是否使用NEON指令。目前必须设置成`ON`,默认值为`ON`。 +- `RPI_TOOLCHAIN`:编译工具链所在的绝对路径,或者相对于构建目录的相对路径。PaddlePaddle的CMake系统将根据该值自动设置需要使用的交叉编译器;否则,用户需要在cmake时手动设置这些值。无默认值。 -其他配置参数: +- `RPI_ARM_NEON`:是否使用NEON指令。目前必须设置成`ON`,默认值为`ON`。 - `HOST_C/CXX_COMPILER`,宿主机的C/C++编译器。在编译宿主机版protoc可执行文件和目标机版OpenBLAS库时需要用到。默认设置成环境变量`CC`的值;若环境变量`CC`没有设置,则设置成`cc`编译器。 -cmake参数如下; +一个常用的CMake配置如下: ``` cmake -DCMAKE_SYSTEM_NAME=RPi \ @@ -47,7 +44,9 @@ cmake -DCMAKE_SYSTEM_NAME=RPi \ .. ``` -用户还可根据自己的需求设置其他编译参数。比如希望最小化生成的库的大小,可以设置`CMAKE_BUILD_TYPE`为`MinSizeRel`;若希望最快的执行速度,则可设置`CMAKE_BUILD_TYPE`为`Release`。亦可以通过手动设置`CMAKE_C/CXX_FLAGS_MINSIZEREL/RELEASE`来影响PaddlePaddle的编译过程。 +其中`WITH_C_API=ON`表示需要构建推理库。 + +用户还可根据自己的需求设置其他编译参数。比如希望最小化生成的库的大小,可以设置`CMAKE_BUILD_TYPE`为`MinSizeRel`;若希望最快的执行速度,则可设置`CMAKE_BUILD_TYPE`为`Release`。 ## 编译和安装 @@ -60,6 +59,4 @@ make install 注意:如果你曾经在源码目录下编译过其他平台的PaddlePaddle库,请先使用`rm -rf`命令删除`third_party`目录和`build`目录,以确保所有的第三方依赖库和PaddlePaddle代码都是针对新的CMake配置重新编译的。 -执行完安装命令后,由于上一步cmake配置中`WITH_C_API`设置为`ON`,`your/path/to/install`目录中会包含`include`和`lib`目录,其中`include`中包含C-API的头文件,`lib`中包含一个Raspberry Pi版本的库。 - -更多的编译配置见[源码编译PaddlePaddle](http://www.paddlepaddle.org/doc_cn/getstarted/build_and_install/cmake/build_from_source_cn.html)相关文档。 +执行完安装命令后,,`your/path/to/install`目录中会包含`include`和`lib`目录,其中`include`中包含C-API的头文件,`lib`中包含一个Raspberry Pi版本的库。 diff --git a/doc/howto/cross_compiling/cross_compiling_for_raspberry_en.md b/doc/howto/cross_compiling/cross_compiling_for_raspberry_en.md new file mode 100644 index 0000000000000000000000000000000000000000..09ac4733ec98c598dfd62f22beaf838320dc7531 --- /dev/null +++ b/doc/howto/cross_compiling/cross_compiling_for_raspberry_en.md @@ -0,0 +1,62 @@ +# Build PaddlePaddle for Raspberry Pi + +You may use any of the following two approaches to build the inference library of PaddlePaddle for Raspberry Pi: + +1. Build using SSH: Log in to a Raspberry Pi using SSH and build the library. The required development tools and third-party dependencies are listed in here: [`/Dockerfile`](https://github.com/PaddlePaddle/Paddle/blob/develop/Dockerfile). + +1. Cross-compile: We talk about how to cross-compile PaddlePaddle for Raspberry Pi on a Linux/x64 machine, in more detail in this article. + +## The Cross-Compiling Toolchain + +Step 1. Clone the Github repo by running the following command. + +```bash +git clone https://github.com/raspberrypi/tools.git +``` + +Step 2. Use the pre-built cross-compiler found in `./tools/tree/master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64`. To run it on a Linux computer, glibc version >= 2.14 is needed. + +## CMake Arguments + +CMake supports [cross-compiling](https://cmake.org/cmake/help/v3.0/manual/cmake-toolchains.7.html#cross-compiling). All CMake configuration arguments required for the cross-compilation for Raspberry Pi can be found in [`cmake/cross_compiling/raspberry_pi.cmake`](https://github.com/PaddlePaddle/Paddle/blob/develop/cmake/cross_compiling/raspberry_pi.cmake). + +Some important arguments that need to be set: + +- `CMAKE_SYSTEM_NAME`: The target platform. Must be `RPi`. + +- `RPI_TOOLCHAIN`: The absolute path of the cross-compiling toolchain. + +- `RPI_ARM_NEON`: Use ARM NEON Intrinsics. This is a required argument and set default to `ON`. + +- `HOST_C/CXX_COMPILER`: The C/C++ compiler for the host. It is used to build building tools running on the host, for example, protoc. + +A commonly-used CMake configuration is as follows: + +``` +cmake -DCMAKE_SYSTEM_NAME=RPi \ + -DRPI_TOOLCHAIN=your/path/to/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64 \ + -DRPI_ARM_NEON=ON \ + -DCMAKE_INSTALL_PREFIX=your/path/to/install \ + -DWITH_GPU=OFF \ + -DWITH_C_API=ON \ + -DWITH_PYTHON=OFF \ + -DWITH_SWIG_PY=OFF \ + .. +``` + +To build the inference library, please set the argument WITH_API to ON: `WITH_C_API=ON`. + +You can add more arguments. For example, to minimize the size of the generated inference library, you may use `CMAKE_BUILD_TYPE=MinSizeRel`. For performance optimization, you may use `CMAKE_BUILD_TYPE=Release`. + +## Build and Install + +The following commands build the inference library of PaddlePaddle for Raspberry Pi and third-party dependencies. + +```bash +make +make install +``` + + The intermediate files will be stored in `build`. Third-party libraries will be located in `build/third_party`. If you have already built it for other platforms like Android or iOS, you may want to clear these directories by running the command: `rm -rf build`. + +The infernece library will be in `your/path/to/install/lib`, with related header files in `your/path/to/install/include`. diff --git a/doc/howto/dev/contribute_to_paddle_en.md b/doc/howto/dev/contribute_to_paddle_en.md deleted file mode 100644 index 40d1eb62d722244139cc84eb170c190d988f5626..0000000000000000000000000000000000000000 --- a/doc/howto/dev/contribute_to_paddle_en.md +++ /dev/null @@ -1,219 +0,0 @@ -# Contribute Code - -We sincerely appreciate your contributions. You can use fork and pull request -workflow to merge your code. - -## Code Requirements -- Your code comments must be fully documented by - [Doxygen](http://www.stack.nl/~dimitri/doxygen/) style. -- Make sure the compiler option `WITH_STYLE_CHECK` is on and the compiler - passes the code style check. -- All code must have unit test. -- Pass all unit tests. - -The following tutorial guides you into submitting your contibution. - -## [Creating a Fork](https://help.github.com/articles/fork-a-repo/) - -Just head over to the GitHub page and click the "Fork" button. -It's just that simple. - -## Clone - -Clone remote repository. - -```bash -➜ git clone https://github.com/USERNAME/Paddle -➜ cd Paddle -``` - -## Create a local branch - -Paddle is currently using [Git-flow branching model](http://nvie.com/posts/a-successful-git-branching-model/). - -All feature and bug fix development work should be done on a new branch, generally create new branch from `develop` branch . - -```bash -➜ git checkout -b my-cool-stuff -``` - -Before the checkout, you need to keep the current branch directory clean, otherwise the untracked file will be brought to the new branch, which can be inspected by `git status`. - -## Using `pre-commit` hook - -Paddle developers use [pre-commit](http://pre-commit.com/) tool to manage git -pre-commit hooks. It can help us format source codes (cpp, python), check some -basic thing before commit (only one EOL for each file, do not add a huge file -in git). `pre-commit` tests is a part of unit tests in Travis-CI now, every -PR doesn't fit hook can not be merged into Paddle. - -To use [pre-commit](http://pre-commit.com/), you should install it by -`pip install pre-commit`, and currently, Paddle uses `clang-format` to format -c/cpp sources. Please make sure clang-format 3.8+ installed. - -Install and run it as follow: - -```bash -➜ pip install pre-commit -➜ pre-commit install -``` - -When you commit your code, the pre-commit hook will check the local code if there is -anything not suitable to commit, and so on. - -## Start to develop - -In this tutorial, I delete a line in README.md and created a new file. - -We can use `git status` to inspect the changes of current directory, `git diff` to see difference. - -```bash -➜ git status -On branch test -Changes not staged for commit: - (use "git add ..." to update what will be committed) - (use "git checkout -- ..." to discard changes in working directory) - - modified: README.md - -Untracked files: - (use "git add ..." to include in what will be committed) - - test - -no changes added to commit (use "git add" and/or "git commit -a") -``` -## Build and Test - -We package PaddlePaddle's compile environment into a Docker image, called the develop image named `paddle:dev`, it contains all compiling tools that PaddlePaddle needs. - -If you want to build the develop image, just run: - -```bash -➜ docker build -t paddle:dev . -``` - -Then we can use the develop image to build PaddlePaddle source. For example: - -```bash -➜ docker run -v $(pwd):/paddle -e "WITH_GPU=OFF" -e "WITH_AVX=ON" -e "WITH_TEST=ON" paddle:dev -``` - -The above command will compile PaddlePaddle and create a Dockerfile for building production image. All the generated files are in the build directory. "WITH_GPU" controls if the generated production image supports GPU. "WITH_AVX" controls if the generated production image supports AVX. "WITH_TEST" controls if the unit test will be generated. - -Then we can generate the production image by copying the compiled PaddlePaddle program into the image by - -```bash -➜ docker build -t paddle:prod -f build/Dockerfile . -``` - -Run unit test finally: - -```bash -➜ docker run -it -v $(pwd):/paddle paddle:dev bash -c "cd /paddle/build && ctest" -``` - -For more details, you can read [this doc](https://github.com/PaddlePaddle/Paddle/blob/develop/doc/getstarted/build_and_install/docker_install_en.rst). - -## Commit - -Next we cancel the changes to the README.md file and then commit our changes by following command lines: - -```bash -➜ git checkout -- README.md -➜ git status -On branch test -Untracked files: - (use "git add ..." to include in what will be committed) - - test - -nothing added to commit but untracked files present (use "git add" to track) -➜ git add test -``` - -We should write a description of each commit by `git commit` to allow others to know -the changes in these files. - -```bash -➜ git commit -CRLF end-lines remover...............................(no files to check)Skipped -yapf.................................................(no files to check)Skipped -Check for added large files..............................................Passed -Check for merge conflicts................................................Passed -Check for broken symlinks................................................Passed -Detect Private Key...................................(no files to check)Skipped -Fix End of Files.....................................(no files to check)Skipped -clang-formater.......................................(no files to check)Skipped -[my-cool-stuff c703c041] add test file - 1 file changed, 0 insertions(+), 0 deletions(-) - create mode 100644 233 -``` - -## Keeping Fork Up to Date - -Before pull your request, you should sync your code from the latest PaddlePaddle. -To do this, you'll need to add a remote at first: - -```bash -➜ git remote add upstream https://github.com/PaddlePaddle/Paddle -➜ git remote -origin -upstream -``` - -Update your fork with the latest upstream changes: - -```bash -➜ git fetch upstream -➜ git pull upstream develop -``` - -Now, your local master branch is up-to-date with everything modified upstream. - -## Push to GitHub - -```bash -# push to your repository in Github -➜ git push origin my-cool-stuff -``` - -## Create an issue and a Pull Request - -Create an Issue to describe the problem and record its number. - -Go to the page for your fork on GitHub, select your development branch, -and click the `New pull request`. - -screen shot 2017-04-26 at 9 09 28 pm - -Then select the target branch: - -screen shot 2017-04-26 at 9 11 52 pm - -We can add `resolve #Issue number` in PR description to close the issue automatically after the PR is merge. More details in . - -Then wait for review, if there need to modify, refer to the above steps to update the corresponding origin branch. - -## Delete origin branch - -After the PR is merge into the main repository, we can delete the remote branch on the PR page. - -screen shot 2017-04-26 at 9 18 24 pm - -Or just run: - -```bash -➜ git push origin :my-cool-stuff -``` - -## Delete local branch - -Finally, we delete local branch: - -```bash -➜ git checkout develop - -# delete my-cool-stuff branch -➜ git branch -D my-cool-stuff -``` diff --git a/doc/howto/dev/contribute_to_paddle_en.md b/doc/howto/dev/contribute_to_paddle_en.md new file mode 120000 index 0000000000000000000000000000000000000000..c97564d93a7f0a753a23cd97d2467d595bd154ff --- /dev/null +++ b/doc/howto/dev/contribute_to_paddle_en.md @@ -0,0 +1 @@ +../../../CONTRIBUTING.md \ No newline at end of file diff --git a/doc/howto/index_cn.rst b/doc/howto/index_cn.rst index 0608aa30968b0e8474eef330e4d2cc63c9def97d..76d3e0a0092f89005605a23e14e712530112a5ac 100644 --- a/doc/howto/index_cn.rst +++ b/doc/howto/index_cn.rst @@ -21,7 +21,6 @@ dev/build_cn.rst dev/write_docs_cn.rst - dev/contribute_to_paddle_cn.md 模型配置 -------- diff --git a/doc/howto/usage/cluster/cluster_train_cn.md b/doc/howto/usage/cluster/cluster_train_cn.md index 93c5544bcfa911f8bdcdaea39a75b3ab7ef218f8..2e98b3de3fe2284375f87e883ff4bac19255dbeb 100644 --- a/doc/howto/usage/cluster/cluster_train_cn.md +++ b/doc/howto/usage/cluster/cluster_train_cn.md @@ -19,7 +19,7 @@ * [启动集群作业](#启动集群作业-1) * [在Kubernetes集群中提交训练作业](#在kubernetes集群中提交训练作业) -# 概述 +## 概述 本文将介绍如何使用PaddlePaddle在不同的集群框架下完成分布式训练。分布式训练架构如下图所示: @@ -32,7 +32,7 @@ 在使用同步SGD训练神经网络时,PaddlePaddle使用同步屏障(barrier),使梯度的提交和参数的更新按照顺序方式执行。在异步SGD中,则并不会等待所有trainer提交梯度才更新参数,这样极大地提高了计算的并行性:参数服务器之间不相互依赖,并行地接收梯度和更新参数,参数服务器也不会等待计算节点全部都提交梯度之后才开始下一步,计算节点之间也不会相互依赖,并行地执行模型的训练。可以看出,虽然异步SGD方式会提高参数更新并行度, 但是并不能保证参数同步更新,在任意时间某一台参数服务器上保存的参数可能比另一台要更新,与同步SGD相比,梯度会有噪声。 -# 环境准备 +## 环境准备 1. 准备您的计算集群。计算集群通常由一组(几台到几千台规模)的Linux服务器组成。服务器之间可以通过局域网(LAN)联通,每台服务器具有集群中唯一的IP地址(或者可被DNS解析的主机名)。集群中的每台计算机通常被成为一个“节点”。 1. 我们需要在集群的所有节点上安装 PaddlePaddle。 如果要启用GPU,还需要在节点上安装对应的GPU驱动以及CUDA。PaddlePaddle的安装可以参考[build_and_install](https://github.com/PaddlePaddle/Paddle/tree/develop/doc/getstarted/build_and_install)的多种安装方式。我们推荐使用[Docker](https://github.com/PaddlePaddle/Paddle/blob/develop/doc/getstarted/build_and_install/docker_install_cn.rst)安装方式来快速安装PaddlePaddle。 @@ -51,8 +51,8 @@ PaddlePaddle 0.10.0, compiled with 下面以`doc/howto/usage/cluster/src/word2vec`中的代码作为实例,介绍使用PaddlePaddle v2 API完成分布式训练。 -# 启动参数说明 -## 启动参数服务器 +## 启动参数说明 +### 启动参数服务器 执行以下的命令启动一个参数服务器并等待和计算节点的数据交互 ```bash $ paddle pserver --port=7164 --ports_num=1 --ports_num_for_sparse=1 --num_gradient_servers=1 @@ -70,7 +70,7 @@ $ stdbuf -oL /usr/bin/nohup paddle pserver --port=7164 --ports_num=1 --ports_num | ports_num_for_sparse | 必选 | 1 | 用于稀疏类型参数通信的端口个数 | | num_gradient_servers | 必选 | 1 | 当前训练任务pserver总数 | -## 启动计算节点 +### 启动计算节点 执行以下命令启动使用python编写的trainer程序(文件名为任意文件名,如train.py) ```bash $ python train.py @@ -117,7 +117,7 @@ paddle.init( | pservers | 必选 | 127.0.0.1 | 当前训练任务启动的pserver的IP列表,多个IP使用“,”隔开 | -## 准备数据集 +### 准备数据集 参考样例数据准备脚本[prepare.py](https://github.com/PaddlePaddle/Paddle/tree/develop/doc/howto/usage/cluster/src/word2vec/prepare.py),准备训练数据和验证数据集,我们使用paddle.dataset.imikolov数据集,并根据分布式训练并发数(trainer节点个数),在`prepare.py`开头部分指定`SPLIT_COUNT`将数据切分成多份。 @@ -149,7 +149,7 @@ test.txt-00002 对于不同的训练任务,训练数据格式和训练程序的`reader()`会大不相同,所以开发者需要根据自己训练任务的实际场景完成训练数据的分割和`reader()`的编写。 -## 准备训练程序 +### 准备训练程序 我们会对每个训练任务都会在每个节点上创建一个工作空间(workspace),其中包含了用户的训练程序、程序依赖、挂载或下载的训练数据分片。 @@ -184,7 +184,7 @@ test.txt-00002 - `train_data_dir`:包含训练数据的目录,可以是从分布式存储挂载过来的,也可以是在任务启动前下载到本地的。 - `test_data_dir`:包含测试数据集的目录。 -# 使用分布式计算平台或工具 +## 使用分布式计算平台或工具 PaddlePaddle可以使用多种分布式计算平台构建分布式计算任务,包括: - [Kubernetes](http://kubernetes.io) Google开源的容器集群的调度框架,支持大规模集群生产环境的完整集群方案。 @@ -195,12 +195,12 @@ PaddlePaddle可以使用多种分布式计算平台构建分布式计算任务 在使用分布式计算平台进行训练时,任务被调度在集群中时,分布式计算平台通常会通过API或者环境变量提供任务运行需要的参数,比如节点的ID、IP和任务节点个数等。 -## 使用Fabric启动集群作业 +### 使用Fabric启动集群作业 -### 准备一个Linux集群 +#### 准备一个Linux集群 可以在`paddle/scripts/cluster_train_v2/fabric/docker_cluster`目录下,执行`kubectl -f ssh_servers.yaml`启动一个测试集群,并使用`kubectl get po -o wide`获得这些节点的IP地址。 -### 启动集群作业 +#### 启动集群作业 `paddle.py` 提供了自动化脚本来启动不同节点中的所有 PaddlePaddle 集群进程。默认情况下,所有命令行选项可以设置为 `paddle.py` 命令选项并且 `paddle.py` 将透明、自动地将这些选项应用到 PaddlePaddle 底层进程。 @@ -216,10 +216,10 @@ sh run.sh 集群作业将会在几秒后启动。 -### 终止集群作业 +#### 终止集群作业 `paddle.py`能获取`Ctrl + C` SIGINT 信号来自动终止它启动的所有进程。只需中断 `paddle.py` 任务来终止集群作业。如果程序崩溃你也可以手动终止。 -### 检查集群训练结果 +#### 检查集群训练结果 详细信息请检查 $workspace/log 里的日志,每一个节点都有相同的日志结构。 `paddle_trainer.INFO` @@ -234,13 +234,13 @@ sh run.sh `train.log` 提供训练过程的 stderr 和 stdout。训练失败时可以检查错误日志。 -### 检查模型输出 +#### 检查模型输出 运行完成后,模型文件将被写入节点 0 的 `output` 目录中。 工作空间中的 `nodefile` 表示当前集群作业的节点 ID。 -## 在OpenMPI集群中提交训练作业 +### 在OpenMPI集群中提交训练作业 -### 准备OpenMPI集群 +#### 准备OpenMPI集群 执行下面的命令以启动3个节点的OpenMPI集群和一个"head"节点: @@ -252,7 +252,7 @@ kubectl create -f mpi-nodes.yaml 然后可以从head节点ssh无密码登录到OpenMPI的每个节点上。 -### 启动集群作业 +#### 启动集群作业 您可以按照下面的步骤在OpenMPI集群中提交paddle训练任务: @@ -280,6 +280,6 @@ scp train.txt-00002 test.txt-00002 [node3IP]:/home/tutorial mpirun -hostfile machines -n 3 /home/tutorial/start_mpi_train.sh ``` -## 在Kubernetes集群中提交训练作业 +### 在Kubernetes集群中提交训练作业 此部分的使用方法可以参考[here](../k8s/k8s_distributed_cn.md)。 diff --git a/doc/howto/usage/cluster/cluster_train_en.md b/doc/howto/usage/cluster/cluster_train_en.md index 1e8b4d54b9ffa99b3beef35ecaf95bbd0866535f..baa97c0c02ae490fff8587071bd2d4adfb5325e3 100644 --- a/doc/howto/usage/cluster/cluster_train_en.md +++ b/doc/howto/usage/cluster/cluster_train_en.md @@ -19,7 +19,7 @@ * [Launching Cluster Job](#launching-cluster-job-1) * [Cluster Training Using Kubernetes](#cluster-training-using-kubernetes) -# Introduction +## Introduction In this article, we'll explain how to run distributed training jobs with PaddlePaddle on different types of clusters. The diagram below shows the main architecture of a distributed trainning job: @@ -33,7 +33,7 @@ PaddlePaddle can support both synchronize stochastic gradient descent (SGD) and When training with synchronize SGD, PaddlePaddle uses an internal "synchronize barrier" which makes gradients update and parameter download in strict order. On the other hand, asynchronous SGD won't wait for all trainers to finish upload at a single step, this will increase the parallelism of distributed training: parameter servers do not depend on each other, they'll do parameter optimization concurrently. Parameter servers will not wait for trainers, so trainers will also do their work concurrently. But asynchronous SGD will introduce more randomness and noises in the gradient. -# Preparations +## Preparations 1. Prepare your computer cluster. It's normally a bunch of Linux servers connected by LAN. Each server will be assigned a unique IP address. The computers in the cluster can be called "nodes". 2. Install PaddlePaddle on every node. If you are going to take advantage of GPU cards, you'll also need to install proper driver and CUDA libraries. To install PaddlePaddle please read [this build and install](https://github.com/PaddlePaddle/Paddle/tree/develop/doc/getstarted/build_and_install) document. We strongly recommend using [Docker installation](https://github.com/PaddlePaddle/Paddle/blob/develop/doc/getstarted/build_and_install/docker_install_en.rst). @@ -52,9 +52,9 @@ PaddlePaddle 0.10.0rc, compiled with We'll take `doc/howto/usage/cluster/src/word2vec` as an example to introduce distributed training using PaddlePaddle v2 API. -# Command-line arguments +## Command-line arguments -## Starting parameter server +### Starting parameter server Type the below command to start a parameter server which will wait for trainers to connect: @@ -74,7 +74,7 @@ $ stdbuf -oL /usr/bin/nohup paddle pserver --port=7164 --ports_num=1 --ports_num | ports_num_for_sparse | required | 1 | number of ports which serves sparse parameter update | | num_gradient_servers | required | 1 | total number of gradient servers | -## Starting trainer +### Starting trainer Type the command below to start the trainer(name the file whatever you want, like "train.py") ```bash @@ -122,7 +122,7 @@ paddle.init( | trainer_id | required | 0 | ID for every trainer, start from 0 | | pservers | required | 127.0.0.1 | list of IPs of parameter servers, separated by "," | -## Prepare Training Dataset +### Prepare Training Dataset Here's some example code [prepare.py](https://github.com/PaddlePaddle/Paddle/tree/develop/doc/howto/usage/cluster/src/word2vec/prepare.py), it will download public `imikolov` dataset and split it into multiple files according to job parallelism(trainers count). Modify `SPLIT_COUNT` at the begining of `prepare.py` to change the count of output files. @@ -155,7 +155,7 @@ When job started, every trainer needs to get it's own part of data. In some dist Different training jobs may have different data format and `reader()` function, developers may need to write different data prepare scripts and `reader()` functions for their job. -## Prepare Training program +### Prepare Training program We'll create a *workspace* directory on each node, storing your training program, dependencies, mounted or downloaded dataset directory. @@ -191,7 +191,7 @@ Your workspace may looks like: - `train_data_dir`: containing training data. Mount from storage service or copy trainning data to here. - `test_data_dir`: containing testing data. -# Use cluster platforms or cluster management tools +## Use cluster platforms or cluster management tools PaddlePaddle supports running jobs on several platforms including: - [Kubernetes](http://kubernetes.io) open-source system for automating deployment, scaling, and management of containerized applications from Google. @@ -202,13 +202,13 @@ We'll introduce cluster job management on these platforms. The examples can be f These cluster platforms provide API or environment variables for training processes, when the job is dispatched to different nodes. Like node ID, IP or total number of nodes etc. -## Cluster Training Using Fabric +### Cluster Training Using Fabric -### Prepare a Linux cluster +#### Prepare a Linux cluster Run `kubectl -f ssh_servers.yaml` under the directory: `paddle/scripts/cluster_train_v2/fabric/docker_cluster` will launch a demo cluster. Run `kubectl get po -o wide` to get IP addresses of these nodes. -### Launching Cluster Job +#### Launching Cluster Job `paddle.py` provides automatical scripts to start all PaddlePaddle cluster processes in different nodes. By default, all command line options can be set as `paddle.py` command options and `paddle.py` will transparently and automatically set these options to PaddlePaddle lower level processes. `paddle.py`provides two distinguished command option for easy job launching. @@ -224,10 +224,10 @@ sh run.sh The cluster Job will start in several seconds. -### Kill Cluster Job +#### Kill Cluster Job `paddle.py` can capture `Ctrl + C` SIGINT signal to automatically kill all processes launched by it. So just stop `paddle.py` to kill cluster job. You should manually kill the job if the program crashed. -### Check Cluster Training Result +#### Check Cluster Training Result Check log in $workspace/log for details, each node owns same log structure. `paddle_trainer.INFO` @@ -242,13 +242,13 @@ It provides stderr and stdout of parameter server process. Check error log if tr `train.log` It provides stderr and stdout of trainer process. Check error log if training crashes. -### Check Model Output +#### Check Model Output After one pass finished, model files will be written in `output` directory in node 0. `nodefile` in workspace indicates the node id of current cluster job. -## Cluster Training Using OpenMPI +### Cluster Training Using OpenMPI -### Prepare an OpenMPI cluster +#### Prepare an OpenMPI cluster Run the following command to start a 3-node MPI cluster and one "head" node. @@ -260,7 +260,7 @@ kubectl create -f mpi-nodes.yaml Then you can log in to every OpenMPI node using ssh without input any passwords. -### Launching Cluster Job +#### Launching Cluster Job Follow the steps to launch a PaddlePaddle training job in OpenMPI cluster:\ @@ -288,6 +288,6 @@ scp train.txt-00002 test.txt-00002 [node3IP]:/home/tutorial mpirun -hostfile machines -n 3 /home/tutorial/start_mpi_train.sh ``` -## Cluster Training Using Kubernetes +### Cluster Training Using Kubernetes The details can be found [here](../k8s/k8s_cn.md) diff --git a/go/.gitignore b/go/.gitignore index 000e1fd55b63b8e532308b787c2708a6c3e5ac87..398d70ca375ffceccdbfc82a4851a6830ca31264 100644 --- a/go/.gitignore +++ b/go/.gitignore @@ -1,2 +1,3 @@ vendor/ .glide/ +proto/*.go diff --git a/go/glide.lock b/go/glide.lock index ce654d36364f8078a493651d8d8b141532eea26d..d15fc934dbe511389cc92ce95cededa41ba32b4d 100644 --- a/go/glide.lock +++ b/go/glide.lock @@ -1,5 +1,5 @@ -hash: 51d9e2e46d7fd9173ff11ecada40f7b7728756be18d5e2f032535f66465e6e15 -updated: 2017-10-24T15:04:09.987751592-07:00 +hash: 107c058cf5c9163a75d40eef2273a793c36112683c25d72aa8288827fdde3a19 +updated: 2017-10-30T03:46:19.137696069Z imports: - name: github.com/alecthomas/gometalinter version: bae2f1293d092fd8167939d5108d1b025eaef9de diff --git a/go/glide.yaml b/go/glide.yaml index ba253f8bebef0ddab810a8303ab1fbe541defbdf..c5d66694acd0f45de5002391a7953b7491eaf2bc 100644 --- a/go/glide.yaml +++ b/go/glide.yaml @@ -30,3 +30,4 @@ import: version: v2.13 - package: github.com/go-stack/stack version: v1.6.0 +- package: github.com/golang/protobuf diff --git a/go/proto/.gitignore b/go/proto/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5e7d2734cfc60289debf74293817c0a8f572ff32 --- /dev/null +++ b/go/proto/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore diff --git a/go/pserver/CMakeLists.txt b/go/pserver/CMakeLists.txt index 4fe0a8cb021e8dbf443c8f33bfb046e228a2fd8d..9ac05199e7ab76c21275838092c0afbdf2612b77 100644 --- a/go/pserver/CMakeLists.txt +++ b/go/pserver/CMakeLists.txt @@ -13,5 +13,5 @@ # limitations under the License. # if(WITH_TESTING) - go_test(pserver_test DEPS paddle_go_optimizer) + go_test(pserver_test DEPS paddle_go_optimizer gen_proto_go) endif() diff --git a/go/pserver/service.go b/go/pserver/service.go index f703d99a29ae9f5310ef36a7492b729c4c892937..7484ec90b1a3a9e67fa798741a9dfeb580c51f1a 100644 --- a/go/pserver/service.go +++ b/go/pserver/service.go @@ -17,6 +17,7 @@ package pserver import ( "bufio" "bytes" + "encoding/binary" "encoding/gob" "encoding/json" "errors" @@ -26,11 +27,15 @@ import ( "os" "path" "strconv" + "strings" "sync" "time" + "github.com/golang/protobuf/proto" uuid "github.com/satori/go.uuid" + pb "github.com/PaddlePaddle/Paddle/go/proto" + log "github.com/inconshreveable/log15" ) @@ -65,6 +70,46 @@ type Parameter struct { Content []byte } +func float32ToString(b []byte) string { + f := make([]float32, len(b)/4) + buf := bytes.NewReader(b) + err := binary.Read(buf, binary.LittleEndian, &f) + if err != nil { + return "" + } + return fmt.Sprintf("%v", f) +} + +func float32ByteToString(c []byte) string { + var a []byte + var b []byte + if len(c) <= 80 { + a = c + } else { + a = c[0:40] + b = c[len(c)-40:] + } + + var s string + s = float32ToString(a) + + if b == nil { + return s + } + + s = strings.Replace(s, "]", "", -1) + "..." + strings.Replace(float32ToString(b), "[", "", -1) + return s +} + +func (p Parameter) String() string { + if p.ElementType != Float32 { + return fmt.Sprintf("name:%v ElementType:%v", + p.Name, p.ElementType) + } + + return float32ByteToString(p.Content) +} + // ParameterWithConfig contains the parameter and the configuration. type ParameterWithConfig struct { Param Parameter @@ -189,7 +234,9 @@ func (s *Service) InitParam(paramWithConfigs ParameterWithConfig, _ *int) error default: } - // TODO(helin): parse parameter config + c := &pb.OptimizerConfig{} + proto.Unmarshal(paramWithConfigs.Config, c) + log.Debug(fmt.Sprintf("OptimizerConfig:%v", c)) s.mu.Lock() defer s.mu.Unlock() @@ -239,7 +286,8 @@ func (s *Service) SendGrad(g Gradient, _ *int) error { select { case <-s.initialized: default: - log.Warn("received gradient before initialization.", "name", g.Name, "size", len(g.Content), "type", g.ElementType) + log.Warn("received gradient before initialization.", + "name", g.Name, "size", len(g.Content), "type", g.ElementType) return errors.New(Uninitialized) } @@ -248,10 +296,14 @@ func (s *Service) SendGrad(g Gradient, _ *int) error { o, ok := s.optMap[g.Name] if !ok { + log.Warn("received gradient but can't find name.", + "name", g.Name, "size", len(g.Content), "type", g.ElementType) return fmt.Errorf("parameter: %s does not exist", g.Name) } - log.Info("received gradient from trainer, updating gradient.", "name", g.Name, "size", len(g.Content), "type", g.ElementType) + log.Debug(Parameter(g).String()) + log.Info("received gradient from trainer, updating gradient.", + "name", g.Name, "size", len(g.Content), "type", g.ElementType) return o.UpdateParameter(g) } @@ -277,7 +329,7 @@ func (s *Service) GetParam(name string, parameter *Parameter) error { parameter.Name = name parameter.ElementType = opt.elementType parameter.Content = opt.GetWeights() - + log.Debug(parameter.String()) log.Info("sending parameter to the trainer", "name", parameter.Name, "size", len(parameter.Content), "type", parameter.ElementType) return nil } diff --git a/go/pserver/service_test.go b/go/pserver/service_test.go index b6f4566eb78cf797e3738afa5f86f5c4e8090d85..58a743e1fadff9d629f682d660e661013c33ac8a 100644 --- a/go/pserver/service_test.go +++ b/go/pserver/service_test.go @@ -15,6 +15,7 @@ package pserver_test import ( + "fmt" "io/ioutil" "reflect" "sync" @@ -178,3 +179,33 @@ func TestBlockUntilInitialized(t *testing.T) { wg.Wait() } + +func TestGradientString(t *testing.T) { + g := pserver.Parameter{} + g.ElementType = pserver.Float32 + g.Content = []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40} + if g.String() != "[3.3702806e+12 2.142699 3.3702806e+12 2.142699]" { + t.Fatal("get float data error!") + } + + g.Content = []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, + 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40} + if g.String() != "[3.3702806e+12 2.142699 3.3702806e+12 2.142699 3.3702806e+12 2.142699 3.3702806e+12 2.142699 3.3702806e+12 2.142699...3.3702806e+12 2.142699 3.3702806e+12 2.142699 3.3702806e+12 2.142699 3.3702806e+12 2.142699 3.3702806e+12 2.142699]" { + t.Fatal("get float data error!", g.String()) + } + fmt.Println(g) +} diff --git a/paddle/framework/CMakeLists.txt b/paddle/framework/CMakeLists.txt index f69a3cfbf8b195cceb15fc2ce81d8bb9d28e5830..f4fef055daf39e9be0645deaafdad4132fc7e35f 100644 --- a/paddle/framework/CMakeLists.txt +++ b/paddle/framework/CMakeLists.txt @@ -24,9 +24,10 @@ cc_test(program_desc_test SRCS program_desc_test.cc DEPS proto_desc) cc_library(op_proto_maker SRCS op_proto_maker.cc DEPS framework_proto attribute) cc_test(op_proto_maker_test SRCS op_proto_maker_test.cc DEPS op_proto_maker) cc_library(op_info SRCS op_info.cc DEPS attribute framework_proto) -cc_library(operator SRCS operator.cc DEPS op_info device_context tensor scope glog) +cc_library(shape_inference SRCS shape_inference.cc DEPS ddim attribute) +cc_library(operator SRCS operator.cc DEPS op_info device_context tensor scope glog shape_inference) cc_test(operator_test SRCS operator_test.cc DEPS operator op_registry) -cc_library(proto_desc SRCS var_desc.cc op_desc.cc block_desc.cc program_desc.cc DEPS attribute ddim op_info operator glog) +cc_library(proto_desc SRCS var_desc.cc op_desc.cc block_desc.cc program_desc.cc DEPS shape_inference op_info operator glog) cc_library(op_registry SRCS op_registry.cc DEPS op_proto_maker op_info operator glog proto_desc) cc_test(op_registry_test SRCS op_registry_test.cc DEPS op_registry) diff --git a/paddle/framework/data_type.h b/paddle/framework/data_type.h index bafb4fbd480bf2a28e3aa3dc615a310f80cec493..c5ae7b185460c8b0d68ba38bb9db9bd3d3fb14ea 100644 --- a/paddle/framework/data_type.h +++ b/paddle/framework/data_type.h @@ -34,5 +34,25 @@ inline DataType ToDataType(std::type_index type) { } } +template +inline void VisitDataType(DataType type, Visitor visitor) { + switch (type) { + case DataType::FP32: + visitor.template operator()(); + break; + case DataType::FP64: + visitor.template operator()(); + break; + case DataType::INT32: + visitor.template operator()(); + break; + case DataType::INT64: + visitor.template operator()(); + break; + default: + PADDLE_THROW("Not supported"); + } +} + } // namespace framework } // namespace paddle diff --git a/paddle/framework/op_desc.cc b/paddle/framework/op_desc.cc index 133869e7b58dd2082bd6e099351609f7ed37e96a..c2d6f124ad292bf46b4e7e9a1dcc2984aae7fcda 100644 --- a/paddle/framework/op_desc.cc +++ b/paddle/framework/op_desc.cc @@ -16,15 +16,51 @@ limitations under the License. */ #include #include #include +#include "glog/logging.h" #include "paddle/framework/block_desc.h" #include "paddle/framework/operator.h" #include "paddle/framework/program_desc.h" - -#include "glog/logging.h" +#include "paddle/framework/shape_inference.h" namespace paddle { namespace framework { +class OpDescBind; +class BlockDescBind; +class CompileTimeInferShapeContext : public InferShapeContext { + public: + CompileTimeInferShapeContext(const OpDescBind &op, + const BlockDescBind &block); + + bool HasInput(const std::string &name) const override; + + bool HasOutput(const std::string &name) const override; + + bool HasInputs(const std::string &name) const override; + + bool HasOutputs(const std::string &name) const override; + + DDim GetInputDim(const std::string &name) const override; + + void SetOutputDim(const std::string &name, const DDim &dim) override; + + AttrReader Attrs() const override; + + const std::vector &Inputs( + const std::string &name) const override; + + const std::vector &Outputs( + const std::string &name) const override; + + private: + DDim GetDim(const std::string &name) const override; + + void SetDim(const std::string &name, const DDim &dim) override; + + const OpDescBind &op_; + const BlockDescBind &block_; +}; + OpDescBind::OpDescBind(const std::string &type, const VariableNameMap &inputs, const VariableNameMap &outputs, const AttributeMap &attrs) { @@ -288,5 +324,97 @@ void OpDescBind::InferVarType(BlockDescBind *block) const { } } +CompileTimeInferShapeContext::CompileTimeInferShapeContext( + const OpDescBind &op, const BlockDescBind &block) + : op_(op), block_(block) {} + +bool CompileTimeInferShapeContext::HasInput(const std::string &name) const { + const std::vector &input_names = op_.Input(name); + auto length = input_names.size(); + if (length == 0) { + return false; + } + PADDLE_ENFORCE_EQ(length, 1UL, + "Input(%s) should have only one value, " + "but it have %d now", + name, length); + return block_.HasVarRecursive(input_names[0]); +} + +bool CompileTimeInferShapeContext::HasOutput(const std::string &name) const { + const std::vector &output_names = op_.Output(name); + auto length = output_names.size(); + if (length == 0) { + return false; + } + PADDLE_ENFORCE_EQ(length, 1UL, + "Output(%s) should have only one value, " + "but it have %d now", + name, length); + return block_.HasVarRecursive(output_names[0]); +} + +bool CompileTimeInferShapeContext::HasInputs(const std::string &name) const { + const std::vector &input_names = op_.Input(name); + if (input_names.empty()) { + return false; + } + for (auto &input : input_names) { + if (!block_.HasVarRecursive(input)) return false; + } + return true; +} + +bool CompileTimeInferShapeContext::HasOutputs(const std::string &name) const { + const std::vector &output_names = op_.Output(name); + if (output_names.empty()) { + return false; + } + for (auto &output : output_names) { + if (!block_.HasVarRecursive(output)) return false; + } + return true; +} + +DDim CompileTimeInferShapeContext::GetInputDim(const std::string &name) const { + std::vector ddims = GetInputsDim(name); + auto length = ddims.size(); + PADDLE_ENFORCE_EQ(length, 1UL, + "Input(%s) should have 1 value, " + "but it has %d now", + name, length); + return ddims[0]; +} + +void CompileTimeInferShapeContext::SetOutputDim(const std::string &name, + const DDim &dim) { + SetOutputsDim(name, {dim}); +} + +AttrReader CompileTimeInferShapeContext::Attrs() const { + return AttrReader(op_.GetAttrMap()); +} + +const std::vector &CompileTimeInferShapeContext::Inputs( + const std::string &name) const { + return op_.Input(name); +} + +const std::vector &CompileTimeInferShapeContext::Outputs( + const std::string &name) const { + return op_.Output(name); +} + +DDim CompileTimeInferShapeContext::GetDim(const std::string &name) const { + auto var = block_.FindVarRecursive(name); + PADDLE_ENFORCE(var != nullptr, "Cannot find variable %s", name); + return framework::make_ddim(var->Shape()); +} + +void CompileTimeInferShapeContext::SetDim(const std::string &name, + const DDim &dim) { + block_.FindVarRecursive(name)->SetShape(framework::vectorize(dim)); +} + } // namespace framework } // namespace paddle diff --git a/paddle/framework/op_registry.h b/paddle/framework/op_registry.h index ed85c386ec2632604bf5faf0ff9b1a087eb9c276..19a9fc3802a2f2348ad7d50a267615ed70bbc4fe 100644 --- a/paddle/framework/op_registry.h +++ b/paddle/framework/op_registry.h @@ -29,6 +29,7 @@ limitations under the License. */ #include "paddle/framework/op_desc.h" #include "paddle/framework/operator.h" #include "paddle/framework/scope.h" +#include "paddle/framework/shape_inference.h" namespace paddle { namespace framework { @@ -161,6 +162,10 @@ class OpKernelRegistrar : public Registrar { REGISTER_OPERATOR(op_type, op_class, _GradOpDescMaker_##grad_op_type##_, \ op_maker_class); +#define REGISTER_OP_WITH_KERNEL(op_type, ...) \ + REGISTER_OPERATOR(op_type, ::paddle::framework::OperatorWithKernel, \ + ##__VA_ARGS__) + #define REGISTER_OP_WITHOUT_GRADIENT(op_type, op_class, op_maker_class) \ REGISTER_OPERATOR(op_type, op_class, op_maker_class) @@ -223,6 +228,10 @@ class OpKernelRegistrar : public Registrar { USE_OP_ITSELF(op_type); \ USE_OP_DEVICE_KERNEL(op_type, CPU); +#define USE_GPU_ONLY_OP(op_type) \ + USE_OP_ITSELF(op_type); \ + USE_OP_DEVICE_KERNEL(op_type, GPU) + #define USE_OP(op_type) \ USE_OP_ITSELF(op_type); \ USE_OP_KERNEL(op_type) diff --git a/paddle/framework/operator.cc b/paddle/framework/operator.cc index db154e4f76fbec444ae4347523cadd1b6d29d319..222a252dc409bf30d5d6abea95156b41cfcd221a 100644 --- a/paddle/framework/operator.cc +++ b/paddle/framework/operator.cc @@ -15,6 +15,7 @@ limitations under the License. */ #include "paddle/framework/operator.h" #include #include +#include "paddle/framework/shape_inference.h" namespace paddle { namespace framework { @@ -273,5 +274,137 @@ bool OpSupportGPU(const std::string& op_type) { return false; } +class RuntimeInferShapeContext : public InferShapeContext { + public: + RuntimeInferShapeContext(const OperatorBase& op, const Scope& scope) + : op_(op), scope_(scope) {} + + bool HasInput(const std::string& name) const override { + auto& ins = Inputs(name); + size_t length = ins.size(); + if (length == 0) { + return false; + } + PADDLE_ENFORCE_EQ(length, 1UL, "Input %s should have more than one inputs", + name); + auto ipt = ins[0]; + auto* var = ipt == kEmptyVarName ? nullptr : scope_.FindVar(ipt); + return var != nullptr; + } + + bool HasOutput(const std::string& name) const override { + auto& outs = Outputs(name); + size_t length = outs.size(); + if (length == 0) { + return false; + } + PADDLE_ENFORCE_EQ(length, 1UL, "Output %s should have more than one inputs", + name); + auto ipt = outs[0]; + auto* var = ipt == kEmptyVarName ? nullptr : scope_.FindVar(ipt); + return var != nullptr; + } + + bool HasInputs(const std::string& name) const override { + auto inputs = op_.Inputs(name); + if (inputs.empty()) { + return false; + } + for (auto& input : inputs) { + if (scope_.FindVar(input) == nullptr) { + return false; + } + } + return true; + } + + bool HasOutputs(const std::string& name) const override { + auto outputs = op_.Outputs(name); + if (outputs.empty()) { + return false; + } + for (auto& output : outputs) { + if (scope_.FindVar(output) == nullptr) { + return false; + } + } + return true; + } + + DDim GetInputDim(const std::string& name) const override { + return GetDim(op_.Input(name)); + } + + void SetOutputDim(const std::string& name, const DDim& dim) override { + SetDim(op_.Output(name), dim); + } + + AttrReader Attrs() const override { return AttrReader(op_.Attrs()); } + + const std::vector& Inputs( + const std::string& name) const override { + return op_.Inputs(name); + } + + const std::vector& Outputs( + const std::string& name) const override { + return op_.Outputs(name); + } + + private: + DDim GetDim(const std::string& name) const override { + Variable* var = scope_.FindVar(name); + if (var->IsType()) { + return var->Get().dims(); + } else if (var->IsType()) { + return var->Get().GetCompleteDims(); + } else { + PADDLE_THROW("Variable type must be LoDTensor/SelectedRows."); + } + } + + void SetDim(const std::string& name, const DDim& dim) override { + Variable* var = scope_.FindVar(name); + if (var->IsType()) { + var->GetMutable()->Resize(dim); + } else if (var->IsType()) { + var->GetMutable()->set_height(dim[0]); + } else { + PADDLE_THROW("Variable type must be LoDTensor/SelectedRows."); + } + } + + const OperatorBase& op_; + const Scope& scope_; +}; + +void OperatorWithKernel::Run(const Scope& scope, + const platform::DeviceContext& dev_ctx) const { + VLOG(3) << "Running operator " << this->Type(); + RuntimeInferShapeContext infer_shape_ctx(*this, scope); + this->InferShape(&infer_shape_ctx); + + ExecutionContext ctx(*this, scope, dev_ctx); + + // check if op[type] has kernel registered. + auto& all_op_kernels = AllOpKernels(); + auto kernels_iter = all_op_kernels.find(type_); + if (kernels_iter == all_op_kernels.end()) { + PADDLE_THROW( + "There are no kernels which are registered in the %s operator.", type_); + } + + // check if op[type] have kernel for kernel_key + OpKernelMap& kernels = kernels_iter->second; + auto kernel_key = OpKernelKey(IndicateDataType(ctx), dev_ctx); + auto kernel_iter = kernels.find(kernel_key); + + if (kernel_iter == kernels.end()) { + PADDLE_THROW("The operator %s does not support %s", type_, kernel_key); + } + + kernel_iter->second->Compute(ctx); +} + } // namespace framework } // namespace paddle diff --git a/paddle/framework/operator.h b/paddle/framework/operator.h index aa79f16df82ab9d81e093af60b730d9aacd09568..93885fa3028e072bc0bd021ea9287087678f3621 100644 --- a/paddle/framework/operator.h +++ b/paddle/framework/operator.h @@ -29,7 +29,6 @@ limitations under the License. */ #include "paddle/framework/op_info.h" #include "paddle/framework/scope.h" #include "paddle/framework/selected_rows.h" -#include "paddle/framework/shape_inference.h" #include "paddle/framework/tensor.h" #include "paddle/platform/device_context.h" #include "paddle/platform/place.h" @@ -123,7 +122,7 @@ class OperatorBase { protected: std::string type_; // NOTE: in case of OpGrad, inputs_ contains: - // I (Inputs)opear + // I (Inputs) // O (Outputs) // OG (Output Gradients) VariableNameMap inputs_; @@ -288,6 +287,16 @@ class ExecutionContext { return device_context_; } + //! Get actual name vector for this input. + const std::vector& Inputs(const std::string& name) const { + return op_.Inputs(name); + } + + //! Get actual name vector for this output. + const std::vector& Outputs(const std::string& name) const { + return op_.Outputs(name); + } + #ifdef PADDLE_WITH_CUDA const platform::CUDADeviceContext& cuda_device_context() const { PADDLE_ENFORCE(platform::is_gpu_place(device_context_.GetPlace())); @@ -317,226 +326,6 @@ template <> std::vector ExecutionContext::MultiOutput( const std::string& name) const; -class CompileTimeInferShapeContext : public InferShapeContext { - public: - CompileTimeInferShapeContext(const OpDescBind& op, const BlockDescBind& block) - : op_(op), block_(block) {} - - bool HasInput(const std::string& name) const override { - const std::vector& input_names = op_.Input(name); - auto length = input_names.size(); - if (length == 0) { - return false; - } - PADDLE_ENFORCE_EQ(length, 1UL, - "Input(%s) should have only one value, " - "but it have %d now", - name, length); - return block_.HasVarRecursive(input_names[0]); - } - - bool HasOutput(const std::string& name) const override { - const std::vector& output_names = op_.Output(name); - auto length = output_names.size(); - if (length == 0) { - return false; - } - PADDLE_ENFORCE_EQ(length, 1UL, - "Output(%s) should have only one value, " - "but it have %d now", - name, length); - return block_.HasVarRecursive(output_names[0]); - } - - bool HasInputs(const std::string& name) const override { - const std::vector& input_names = op_.Input(name); - if (input_names.empty()) { - return false; - } - for (auto& input : input_names) { - if (!block_.HasVarRecursive(input)) return false; - } - return true; - } - - bool HasOutputs(const std::string& name) const override { - const std::vector& output_names = op_.Output(name); - if (output_names.empty()) { - return false; - } - for (auto& output : output_names) { - if (!block_.HasVarRecursive(output)) return false; - } - return true; - } - - DDim GetInputDim(const std::string& name) const override { - std::vector ddims = GetInputsDim(name); - auto length = ddims.size(); - PADDLE_ENFORCE_EQ(length, 1UL, - "Input(%s) should have 1 value, " - "but it has %d now", - name, length); - return ddims[0]; - } - - void SetInputDim(const std::string& name, const DDim& dim) override { - SetInputsDim(name, {dim}); - } - - DDim GetOutputDim(const std::string& name) const override { - std::vector ddims = GetOutputsDim(name); - auto length = ddims.size(); - PADDLE_ENFORCE_EQ(length, 1UL, - "Output(%s) should have 1 value, " - "but it has %d now", - name, length); - return ddims[0]; - } - - void SetOutputDim(const std::string& name, const DDim& dim) override { - SetOutputsDim(name, {dim}); - } - - AttrReader Attrs() const override { return AttrReader(op_.GetAttrMap()); } - - const std::vector& Inputs( - const std::string& name) const override { - return op_.Input(name); - } - - const std::vector& Outputs( - const std::string& name) const override { - return op_.Output(name); - } - - private: - DDim GetDim(const std::string& name) const override { - auto var = block_.FindVarRecursive(name); - PADDLE_ENFORCE(var != nullptr, "Cannot find variable %s", name); - return framework::make_ddim(var->Shape()); - } - - void SetDim(const std::string& name, const DDim& dim) override { - block_.FindVarRecursive(name)->SetShape(framework::vectorize(dim)); - } - - const OpDescBind& op_; - const BlockDescBind& block_; -}; - -class RuntimeInferShapeContext : public InferShapeContext { - public: - RuntimeInferShapeContext(const OperatorBase& op, const Scope& scope) - : op_(op), scope_(scope) {} - - bool HasInput(const std::string& name) const override { - auto& ins = Inputs(name); - size_t length = ins.size(); - if (length == 0) { - return false; - } - PADDLE_ENFORCE_EQ(length, 1UL, "Input %s should have more than one inputs", - name); - auto ipt = ins[0]; - auto* var = ipt == kEmptyVarName ? nullptr : scope_.FindVar(ipt); - return var != nullptr; - } - - bool HasOutput(const std::string& name) const override { - auto& outs = Outputs(name); - size_t length = outs.size(); - if (length == 0) { - return false; - } - PADDLE_ENFORCE_EQ(length, 1UL, "Output %s should have more than one inputs", - name); - auto ipt = outs[0]; - auto* var = ipt == kEmptyVarName ? nullptr : scope_.FindVar(ipt); - return var != nullptr; - } - - bool HasInputs(const std::string& name) const override { - auto inputs = op_.Inputs(name); - if (inputs.empty()) { - return false; - } - for (auto& input : inputs) { - if (scope_.FindVar(input) == nullptr) { - return false; - } - } - return true; - } - - bool HasOutputs(const std::string& name) const override { - auto outputs = op_.Outputs(name); - if (outputs.empty()) { - return false; - } - for (auto& output : outputs) { - if (scope_.FindVar(output) == nullptr) { - return false; - } - } - return true; - } - - DDim GetInputDim(const std::string& name) const override { - return GetDim(op_.Input(name)); - } - - void SetInputDim(const std::string& name, const DDim& dim) override { - SetDim(op_.Input(name), dim); - } - - DDim GetOutputDim(const std::string& name) const override { - return GetDim(op_.Output(name)); - } - - void SetOutputDim(const std::string& name, const DDim& dim) override { - SetDim(op_.Output(name), dim); - } - - AttrReader Attrs() const override { return AttrReader(op_.Attrs()); } - - const std::vector& Inputs( - const std::string& name) const override { - return op_.Inputs(name); - } - - const std::vector& Outputs( - const std::string& name) const override { - return op_.Outputs(name); - } - - private: - DDim GetDim(const std::string& name) const override { - Variable* var = scope_.FindVar(name); - if (var->IsType()) { - return var->Get().dims(); - } else if (var->IsType()) { - return var->Get().GetCompleteDims(); - } else { - PADDLE_THROW("Variable type must be LoDTensor/SelectedRows."); - } - } - - void SetDim(const std::string& name, const DDim& dim) override { - Variable* var = scope_.FindVar(name); - if (var->IsType()) { - var->GetMutable()->Resize(dim); - } else if (var->IsType()) { - var->GetMutable()->set_height(dim[0]); - } else { - PADDLE_THROW("Variable type must be LoDTensor/SelectedRows."); - } - } - - const OperatorBase& op_; - const Scope& scope_; -}; - class OpKernelBase { public: /** @@ -595,32 +384,7 @@ class OperatorWithKernel : public OperatorBase { : OperatorBase(type, inputs, outputs, attrs) {} void Run(const Scope& scope, - const platform::DeviceContext& dev_ctx) const final { - VLOG(3) << "Running operator " << this->Type(); - RuntimeInferShapeContext infer_shape_ctx(*this, scope); - this->InferShape(&infer_shape_ctx); - - ExecutionContext ctx(*this, scope, dev_ctx); - - // check if op[type] has kernel registered. - auto& all_op_kernels = AllOpKernels(); - auto kernels_iter = all_op_kernels.find(type_); - if (kernels_iter == all_op_kernels.end()) { - PADDLE_THROW("op[%s] has no kernel", type_); - } - - // check if op[type] have kernel for kernel_key - OpKernelMap& kernels = kernels_iter->second; - auto kernel_key = OpKernelKey(IndicateDataType(ctx), dev_ctx); - auto kernel_iter = kernels.find(kernel_key); - - if (kernel_iter == kernels.end()) { - PADDLE_THROW("op[%s] has no kernel with kernel_key[%s]", type_, - kernel_key); - } - - kernel_iter->second->Compute(ctx); - } + const platform::DeviceContext& dev_ctx) const final; static std::unordered_map& AllOpKernels() { @@ -644,6 +408,7 @@ class OperatorWithKernel : public OperatorBase { // indicate kernel DataType by input data. Defaultly all input data must be // same. virtual DataType IndicateDataType(const ExecutionContext& ctx) const { + VLOG(3) << "Default IndicateDataType " << this->Type(); auto& scope = ctx.scope(); int data_type = -1; for (auto& input : this->inputs_) { diff --git a/paddle/framework/shape_inference.cc b/paddle/framework/shape_inference.cc new file mode 100644 index 0000000000000000000000000000000000000000..33a1d0b9b217c5d2a4b0fb63f427529e7988b24e --- /dev/null +++ b/paddle/framework/shape_inference.cc @@ -0,0 +1,54 @@ +/* Copyright (c) 2016 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. */ +#include "paddle/framework/shape_inference.h" + +namespace paddle { +namespace framework { + +std::vector InferShapeContext::GetInputsDim( + const std::string &name) const { + const std::vector &names = Inputs(name); + return GetDims(names); +} + +void InferShapeContext::SetOutputsDim( + const std::string &name, const std::vector &dims) { + auto &names = Outputs(name); + SetDims(names, dims); +} + +void InferShapeContext::ShareLoD(const std::string &in, const std::string &out, + size_t i, size_t j) const {} + +std::vector InferShapeContext::GetDims( + const std::vector &names) const { + std::vector ret; + ret.reserve(names.size()); + std::transform( + names.begin(), names.end(), std::back_inserter(ret), + [this](const std::string &name) { return this->GetDim(name); }); + return ret; +} + +void InferShapeContext::SetDims(const std::vector &names, + const std::vector &dims) { + size_t length = names.size(); + PADDLE_ENFORCE_EQ(length, dims.size()); + for (size_t i = 0; i < length; ++i) { + SetDim(names[i], dims[i]); + } +} + +} // namespace framework +} // namespace paddle diff --git a/paddle/framework/shape_inference.h b/paddle/framework/shape_inference.h index b93f980cf6d279d18388b9637a2ff45d797ca78e..f1f1e44bccd771be81cad7c28efe9b1b885eef6b 100644 --- a/paddle/framework/shape_inference.h +++ b/paddle/framework/shape_inference.h @@ -14,6 +14,7 @@ limitations under the License. */ #pragma once +#include "paddle/framework/attribute.h" #include "paddle/framework/ddim.h" namespace paddle { @@ -21,7 +22,7 @@ namespace framework { class InferShapeContext { public: - virtual ~InferShapeContext() {} + virtual ~InferShapeContext() = default; virtual bool HasInput(const std::string &name) const = 0; virtual bool HasOutput(const std::string &name) const = 0; @@ -29,57 +30,32 @@ class InferShapeContext { virtual bool HasOutputs(const std::string &name) const = 0; virtual framework::DDim GetInputDim(const std::string &name) const = 0; - std::vector GetInputsDim(const std::string &name) const { - const std::vector &names = Inputs(name); - return GetDims(names); - } - virtual void SetInputDim(const std::string &name, - const framework::DDim &dim) = 0; - void SetInputsDim(const std::string &name, - const std::vector &dims) { - auto &names = Inputs(name); - SetDims(names, dims); - } - virtual framework::DDim GetOutputDim(const std::string &name) const = 0; - std::vector GetOutputsDim(const std::string &name) const { - const std::vector &names = Outputs(name); - return GetDims(names); - } + + std::vector GetInputsDim(const std::string &name) const; + virtual void SetOutputDim(const std::string &name, const DDim &dim) = 0; void SetOutputsDim(const std::string &name, - const std::vector &dims) { - auto &names = Outputs(name); - SetDims(names, dims); - } + const std::vector &dims); + virtual AttrReader Attrs() const = 0; virtual const std::vector &Inputs( const std::string &name) const = 0; virtual const std::vector &Outputs( const std::string &name) const = 0; + // TODO(qiao) implement this function void ShareLoD(const std::string &in, const std::string &out, size_t i = 0, - size_t j = 0) const {} + size_t j = 0) const; protected: virtual framework::DDim GetDim(const std::string &name) const = 0; virtual void SetDim(const std::string &name, const framework::DDim &dim) = 0; + std::vector GetDims( - const std::vector &names) const { - std::vector ret; - ret.reserve(names.size()); - std::transform( - names.begin(), names.end(), std::back_inserter(ret), - [this](const std::string &name) { return this->GetDim(name); }); - return ret; - } + const std::vector &names) const; + void SetDims(const std::vector &names, - const std::vector &dims) { - size_t length = names.size(); - PADDLE_ENFORCE_EQ(length, dims.size()); - for (size_t i = 0; i < length; ++i) { - SetDim(names[i], dims[i]); - } - } + const std::vector &dims); }; } // namespace framework diff --git a/paddle/framework/tensor.h b/paddle/framework/tensor.h index 9d2dc6a32bb2d4f6368fd9c7264c55fb9588819c..7b9a5b75e1087a1cc3b6c6c7a6e4dc185c32dd42 100644 --- a/paddle/framework/tensor.h +++ b/paddle/framework/tensor.h @@ -126,11 +126,16 @@ class Tensor { inline Tensor Slice(const int& begin_idx, const int& end_idx) const; platform::Place place() const { - PADDLE_ENFORCE_NOT_NULL(holder_, "Tensor get place() must contains holder"); + PADDLE_ENFORCE_NOT_NULL( + holder_, "Tensor not initialized yet when Tensor::place() is called."); return holder_->place(); } - std::type_index type() const { return holder_->type(); } + std::type_index type() const { + PADDLE_ENFORCE_NOT_NULL( + holder_, "Tensor not initialized yet when Tensor::type() is called."); + return holder_->type(); + } size_t memory_size() const; diff --git a/paddle/gserver/tests/CMakeLists.txt b/paddle/gserver/tests/CMakeLists.txt index 329536afaf6d69676e8c39fdf8b6b8cb87ade5fa..aa94ee406e27c86e6d49b6d2b5327a3f86bcacd6 100644 --- a/paddle/gserver/tests/CMakeLists.txt +++ b/paddle/gserver/tests/CMakeLists.txt @@ -1,24 +1,29 @@ # gserver pacakge unittests -if(NOT MOBILE_INFERENCE) -################### test_ProtoDataProvider ############ - add_unittest_without_exec(test_ProtoDataProvider - test_ProtoDataProvider.cpp) - - # test_ProtoDataProvider will mkdir as same name, - # so if WORKING_DIRECTORY is default directory, then - # mkdir will get error. - add_test(NAME test_ProtoDataProvider - COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_ProtoDataProvider - WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle) -endif() +add_simple_unittest(test_LinearChainCRF) +add_simple_unittest(test_MultinomialSampler) +add_simple_unittest(test_RecurrentLayer) -################# test_LayerGrad ####################### -add_unittest_without_exec(test_LayerGrad - test_LayerGrad.cpp - LayerGradUtil.cpp) -add_test(NAME test_LayerGrad - COMMAND test_LayerGrad) +function(gserver_test TARGET) + add_unittest_without_exec(${TARGET} + ${TARGET}.cpp + LayerGradUtil.cpp) + add_test(NAME ${TARGET} + COMMAND ${TARGET}) +endfunction() + +gserver_test(test_LayerGrad) +gserver_test(test_CRFLayerGrad) +gserver_test(test_CrossEntropyOverBeamGrad) +gserver_test(test_SeqSliceLayerGrad) +gserver_test(test_ActivationGrad) +gserver_test(test_ConvTrans) +gserver_test(test_PriorBox) +gserver_test(test_DetectionOutput) +gserver_test(test_ConvUnify) +gserver_test(test_BatchNorm) +gserver_test(test_KmaxSeqScore) +gserver_test(test_Expand) ########## test_Mkldnn layers and activations ########## if(WITH_MKLDNN) @@ -32,89 +37,6 @@ if(WITH_MKLDNN) WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle) endif() -################ test_CRFLayerGrad #################### -add_unittest_without_exec(test_CRFLayerGrad - test_CRFLayerGrad.cpp - LayerGradUtil.cpp) -add_test(NAME test_CRFLayerGrad - COMMAND test_CRFLayerGrad) - -################ test_CrossEntropyOverBeam #################### -add_unittest_without_exec(test_CrossEntropyOverBeam - test_CrossEntropyOverBeamGrad.cpp - LayerGradUtil.cpp) -add_test(NAME test_CrossEntropyOverBeam - COMMAND test_CrossEntropyOverBeam) - -################ test_SeqSliceLayerGrad #################### -add_unittest_without_exec(test_SeqSliceLayerGrad - test_SeqSliceLayerGrad.cpp - LayerGradUtil.cpp) -add_test(NAME test_SeqSliceLayerGrad - COMMAND test_SeqSliceLayerGrad) - -add_unittest_without_exec(test_ActivationGrad - test_ActivationGrad.cpp - LayerGradUtil.cpp) -add_test(NAME test_ActivationGrad - COMMAND test_ActivationGrad) -################# test_ConvTrans ####################### -add_unittest_without_exec(test_ConvTrans - test_ConvTrans.cpp - LayerGradUtil.cpp) - -add_test(NAME test_ConvTrans - COMMAND test_ConvTrans) -################# test_PriorBox ####################### -add_unittest_without_exec(test_PriorBox - test_PriorBox.cpp - LayerGradUtil.cpp) - -add_test(NAME test_PriorBox - COMMAND test_PriorBox) -################# test_DetectionOutput ####################### -add_unittest_without_exec(test_DetectionOutput - test_DetectionOutput.cpp - LayerGradUtil.cpp) - -add_test(NAME test_DetectionOutput - COMMAND test_DetectionOutput) -################# test_ConvUnify ####################### -add_unittest_without_exec(test_ConvUnify - test_ConvUnify.cpp - LayerGradUtil.cpp) - -add_test(NAME test_ConvUnify - COMMAND test_ConvUnify) -################# test_BatchNorm ####################### -add_unittest_without_exec(test_BatchNorm - test_BatchNorm.cpp - LayerGradUtil.cpp) - -add_test(NAME test_BatchNorm - COMMAND test_BatchNorm) - - -################# test_KmaxSeqScore ####################### -add_unittest_without_exec(test_KmaxSeqScore - test_KmaxSeqScore.cpp - LayerGradUtil.cpp) - -add_test(NAME test_KmaxSeqScore - COMMAND test_KmaxSeqScore) - -if(NOT MOBILE_INFERENCE) -################## test_Evaluator ####################### - add_unittest(test_Evaluator - test_Evaluator.cpp) -endif() - -################ test_LinearChainCRF #################### -add_simple_unittest(test_LinearChainCRF) - -############## test_MultinomialSampler ################### -add_simple_unittest(test_MultinomialSampler) - ############## test_PyDataProvider ######################## if(WITH_PYTHON) add_unittest_without_exec(test_PyDataProvider @@ -125,9 +47,6 @@ if(WITH_PYTHON) WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle) endif() -############### test_RecurrentLayer ####################### -add_simple_unittest(test_RecurrentLayer) - ############### test_WarpCTCLayer ####################### if(NOT WITH_DOUBLE) add_unittest_without_exec(test_WarpCTCLayer @@ -139,19 +58,33 @@ if(NOT WITH_DOUBLE) endif() if(NOT MOBILE_INFERENCE) -############### test_RecurrentGradientMachine ############### - # TODO(yuyang18): There is some bug in test_RecurrentGradientMachine - # I will fix it. - add_unittest_without_exec(test_RecurrentGradientMachine - test_RecurrentGradientMachine.cpp) - add_test(NAME test_RecurrentGradientMachine - COMMAND .set_python_path.sh -d - ${PADDLE_SOURCE_DIR}/python:${PADDLE_SOURCE_DIR}/paddle/gserver/tests - ${CMAKE_CURRENT_BINARY_DIR}/test_RecurrentGradientMachine - WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle) -endif() +################### test_ProtoDataProvider ############ + add_unittest_without_exec(test_ProtoDataProvider + test_ProtoDataProvider.cpp) -if(NOT MOBILE_INFERENCE) + # test_ProtoDataProvider will mkdir as same name, + # so if WORKING_DIRECTORY is default directory, then + # mkdir will get error. + add_test(NAME test_ProtoDataProvider + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_ProtoDataProvider + WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle) + +################## test_Evaluator ####################### + add_unittest(test_Evaluator + test_Evaluator.cpp) + +############### test_RecurrentGradientMachine ############### + # TODO(yuyang18): There is some bug in test_RecurrentGradientMachine + # I will fix it. + add_unittest_without_exec(test_RecurrentGradientMachine + test_RecurrentGradientMachine.cpp) + add_test(NAME test_RecurrentGradientMachine + COMMAND .set_python_path.sh -d + ${PADDLE_SOURCE_DIR}/python:${PADDLE_SOURCE_DIR}/paddle/gserver/tests + ${CMAKE_CURRENT_BINARY_DIR}/test_RecurrentGradientMachine + WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle) + +############### test_NetworkCompare ############### add_unittest_without_exec(test_NetworkCompare test_NetworkCompare.cpp) if(WITH_GPU) diff --git a/paddle/gserver/tests/test_Expand.cpp b/paddle/gserver/tests/test_Expand.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d32bf0152f77bba098daa508fe448784ac013549 --- /dev/null +++ b/paddle/gserver/tests/test_Expand.cpp @@ -0,0 +1,127 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. + +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. */ + +#include +#include +#include + +#include "LayerGradUtil.h" +#include "paddle/testing/TestUtil.h" + +using namespace paddle; // NOLINT +using namespace std; // NOLINT + +// Do one forward pass of expand layer and check to see if its output +// matches the given result.(Test onlyCPU currently.) +void doOneExpandTest(string trans_type, + bool hasSubseq, + bool useGpu, + Argument& input1, + Argument& input2, + Argument& result) { + FLAGS_use_gpu = false; + // Setting up the expand layer + TestConfig config; + config.layerConfig.set_type("expand"); + + auto inputType1 = + trans_type == "non-seq" ? INPUT_DENSE_DIM_DATA : INPUT_SEQUENCE_DATA; + config.inputDefs.push_back({inputType1, "layer0", 1, 0}); + auto inputType2 = + hasSubseq ? INPUT_HASSUB_SEQUENCE_DATA : INPUT_SEQUENCE_DATA; + + config.inputDefs.push_back({inputType2, "layer1", 1, 0}); + config.layerConfig.add_inputs(); + config.layerConfig.add_inputs(); + config.layerConfig.set_trans_type(trans_type); + + // data layer initialize + std::vector dataLayers; + LayerMap layerMap; + vector datas; + initDataLayer( + config, &dataLayers, &datas, &layerMap, "expand", 1, false, useGpu); + dataLayers[0]->getOutput() = input1; + dataLayers[1]->getOutput() = input2; + + // test layer initialize + std::vector parameters; + LayerPtr expandLayer; + initTestLayer(config, &layerMap, ¶meters, &expandLayer); + expandLayer->forward(PASS_GC); + checkMatrixEqual(expandLayer->getOutputValue(), result.value); +} + +TEST(Layer, ExpandLayerFwd) { + bool useGpu = false; + + // Assume batch_size =3 in all cases. + + // CPU case 1. non-seq expand to seq + // input1 = 1,2,3 + // input2 = [4,5],[6],[7,8,9] + // result = [1,1],[2],[3,3,3] + Argument input1, input2, result; + input1.value = Matrix::create(3, 1, false, useGpu); + real input1Data[] = {1, 2, 3}; + input1.value->setData(input1Data); + + input2.value = Matrix::create(6, 1, false, useGpu); + real input2Data[] = {4, 5, 6, 7, 8, 9}; + input2.value->setData(input2Data); + input2.sequenceStartPositions = ICpuGpuVector::create(4, useGpu); + int input2Seq[] = {0, 2, 3, 6}; + input2.sequenceStartPositions->copyFrom(input2Seq, 4, useGpu); + + result.value = Matrix::create(6, 1, false, useGpu); + real resultData[] = {1, 1, 2, 3, 3, 3}; + result.value->setData(resultData); + + doOneExpandTest("non-seq", false, useGpu, input1, input2, result); + + // CPU case 2. non-seq expand to sub-seq + // NOTE: input1.batch_size == input2.sequencelength in this case. + // i.e, input1 expands by input2.sequence + // input1 = 1,2,3 + // input2 = [[4,5]],[[6]],[[7],[8,9]] + // result = [[1,1]],[[2]],[[3],[3,3]] + input2.subSequenceStartPositions = ICpuGpuVector::create(5, useGpu); + int input2SubSeq[] = {0, 2, 3, 4, 6}; + input2.subSequenceStartPositions->copyFrom(input2SubSeq, 5, useGpu); + + doOneExpandTest("non-seq", true, useGpu, input1, input2, result); + + // CPU case 3. seq expand to sub-seq + // input1 = [1,2],[3],[4] + // input2 = [[4,5]],[[6]],[[7],[8,9]] + // result = [[1,1]],[[2]],[[3],[4,4]] + Matrix::resizeOrCreate(input1.value, 4, 1, false, useGpu); + real input1Data_case3[] = {1, 2, 3, 4}; + input1.value->setData(input1Data_case3); + + input1.sequenceStartPositions = ICpuGpuVector::create(4, useGpu); + int input1Seq[] = {0, 2, 3, 4}; + input1.sequenceStartPositions->copyFrom(input1Seq, 4, useGpu); + + real resultData_case3[] = {1, 1, 2, 3, 4, 4}; + result.value->setData(resultData_case3); + + doOneExpandTest("seq", true, useGpu, input1, input2, result); +} + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + initMain(argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/paddle/operators/CMakeLists.txt b/paddle/operators/CMakeLists.txt index c72261710173a0f3af199646d6800bf9d7c27b67..60dc55a32f5f05875e4f3ce77431556e14adc74a 100644 --- a/paddle/operators/CMakeLists.txt +++ b/paddle/operators/CMakeLists.txt @@ -90,6 +90,13 @@ function(op_library TARGET) file(APPEND ${pybind_file} "USE_OP(sigmoid);\n") endif() + # nccl_op contains several operators + if ("${TARGET}" STREQUAL "nccl_op") + set(pybind_flag 1) + # It's enough to just adding one operator to pybind + file(APPEND ${pybind_file} "USE_GPU_ONLY_OP(ncclAllReduce);\n") + endif() + # reduce_op contains several operators if ("${TARGET}" STREQUAL "reduce_op") set(pybind_flag 1) @@ -121,6 +128,7 @@ function(op_library TARGET) endfunction() add_subdirectory(math) +add_subdirectory(nccl) set(DEPS_OPS recurrent_op @@ -130,6 +138,7 @@ set(DEPS_OPS sum_op pool_op pool_with_index_op + nccl_op sequence_conv_op lstm_op) @@ -142,6 +151,9 @@ op_library(softmax_with_cross_entropy_op DEPS cross_entropy softmax) op_library(sum_op DEPS net_op selected_rows_functor) op_library(pool_op DEPS pooling) op_library(pool_with_index_op DEPS pooling) +if(WITH_GPU) +op_library(nccl_op DEPS nccl_common) +endif() op_library(sequence_conv_op DEPS context_project) op_library(lstm_op DEPS sequence2batch lstm_compute) @@ -157,4 +169,8 @@ cc_test(net_op_test SRCS net_op_test.cc DEPS net_op) cc_test(scatter_test SRCS scatter_test.cc DEPS tensor) cc_test(strided_memcpy_test SRCS strided_memcpy_test.cc DEPS tensor paddle_memory) cc_test(dynamic_recurrent_op_test SRCS dynamic_recurrent_op_test.cc DEPS dynamic_recurrent_op recurrent_op tensor_array) + +if(WITH_GPU) + nv_test(nccl_op_test SRCS nccl_op_test.cu DEPS nccl_op gpu_info device_context) +endif() cc_test(save_load_op_test SRCS save_load_op_test.cc DEPS save_op load_op) diff --git a/paddle/operators/accuracy_op.cc b/paddle/operators/accuracy_op.cc index e0a00ecaf04335800eab9e2e5a03628a2ce2ca8d..2a2a1e9cfd680ff983f54e4c12c34fbb5af69ca0 100644 --- a/paddle/operators/accuracy_op.cc +++ b/paddle/operators/accuracy_op.cc @@ -22,22 +22,35 @@ class AccuracyOp : public framework::OperatorWithKernel { using framework::OperatorWithKernel::OperatorWithKernel; void InferShape(framework::InferShapeContext *ctx) const override { - PADDLE_ENFORCE(ctx->HasInput("Inference"), - "Input(Inference) of AccuracyOp should not be null."); + PADDLE_ENFORCE(ctx->HasInput("Out"), + "Input (Out) of accuracy op should not be null."); + PADDLE_ENFORCE(ctx->HasInput("Indices"), + "Input (Indices) of accuracy op should not be null."); PADDLE_ENFORCE(ctx->HasInput("Label"), - "Input(Label) of AccuracyOp should not be null."); + "Input (Label) of accuracy op should not be null."); PADDLE_ENFORCE(ctx->HasOutput("Accuracy"), - "Output(Accuracy) of AccuracyOp should not be null."); + "Output (Accuracy) of AccuracyOp should not be null."); - auto inference_dim = ctx->GetInputDim("Inference"); + auto inference_dim = ctx->GetInputDim("Out"); auto label_dim = ctx->GetInputDim("Label"); + // Assume indices has same shape with infernece, because + // it's the output of topk. - PADDLE_ENFORCE_EQ(label_dim.size(), 1, "label must be a vector"); + PADDLE_ENFORCE_EQ(label_dim.size(), 2, "label's rank must be 2."); + PADDLE_ENFORCE_EQ(label_dim[1], 1, "label's second dimension must be 1"); PADDLE_ENFORCE_EQ(inference_dim[0], label_dim[0], - "inference size must be the same as label size"); + "the inference tensor's num_rows must be" + " the same as label."); ctx->SetOutputDim("Accuracy", {1}); - ctx->ShareLoD("Inference", /*->*/ "Accuracy"); + ctx->ShareLoD("Out", /*->*/ "Accuracy"); + } + + protected: + // IndicateDataType + framework::DataType IndicateDataType( + const framework::ExecutionContext &ctx) const override { + return framework::ToDataType(ctx.Input("Out")->type()); } }; @@ -47,7 +60,8 @@ class AccuracyOpMaker : public framework::OpProtoAndCheckerMaker { framework::OpAttrChecker *op_checker) : OpProtoAndCheckerMaker(proto, op_checker) { // TODO(typhoonzero): support both inference value and indices. - AddInput("Inference", "topk(indices) the network output"); + AddInput("Out", "topk (inferences) the network output"); + AddInput("Indices", "topk (indices) the network output"); AddInput("Label", "Label of the training data"); // TODO(typhoonzero): AddInput("Weight", ... AddOutput("Accuracy", "The accuracy of current batch"); @@ -58,7 +72,7 @@ The accuracy is: .. math:: accuracy = \\frac{NumOfCorrectPredicts}{NumOfAllSamples}) -Both the input `Inference` and `Label` can carry the LoD (Level of Details) +Both the input `Out` and `Label` can carry the LoD (Level of Details) information, or not. But the output only shares the LoD with input `Inference`. )DOC"); } @@ -68,9 +82,10 @@ information, or not. But the output only shares the LoD with input `Inference`. } // namespace paddle namespace ops = paddle::operators; -REGISTER_OP_WITHOUT_GRADIENT(accuracy, ops::AccuracyOp, ops::AccuracyOpMaker); -REGISTER_OP_CPU_KERNEL( - accuracy, ops::AccuracyKernel, - ops::AccuracyKernel, - ops::AccuracyKernel, - ops::AccuracyKernel); +REGISTER_OPERATOR(accuracy, ops::AccuracyOp, ops::AccuracyOpMaker, + paddle::framework::EmptyGradOpMaker); +// FIXME(typhoonzero): types of T is for infernece data. +// label data is always int. +REGISTER_OP_CPU_KERNEL(accuracy, + ops::AccuracyKernel, + ops::AccuracyKernel); diff --git a/paddle/operators/accuracy_op.cu b/paddle/operators/accuracy_op.cu index 54e6ab99dc8c8ff1afbc636e6595cd67fb64eccf..a0483f367e1e3bcf4ad50ee9155f25461c1b4ed2 100644 --- a/paddle/operators/accuracy_op.cu +++ b/paddle/operators/accuracy_op.cu @@ -21,9 +21,10 @@ namespace paddle { namespace operators { using platform::PADDLE_CUDA_NUM_THREADS; -template -__global__ void AccuracyCudaKernel(const int N, const int D, const T* Xdata, - const T* labeldata, float* accuracy) { +template +__global__ void AccuracyCudaKernel(const int N, const int D, + const int64_t* Xdata, + const int64_t* labeldata, float* accuracy) { int count = 0; __shared__ int total[BlockSize]; @@ -52,13 +53,14 @@ class AccuracyOpCUDAKernel : public framework::OpKernel { void Compute(const framework::ExecutionContext& ctx) const override { PADDLE_ENFORCE(platform::is_gpu_place(ctx.GetPlace()), "It must use GPUPlace."); - auto* inference = ctx.Input("Inference"); + auto* inference = ctx.Input("Out"); + auto* indices = ctx.Input("Indices"); auto* label = ctx.Input("Label"); auto* accuracy = ctx.Output("Accuracy"); // FIXME(typhoonzero): only support indices currently // if add support for output values, how to detect the data type? - const T* inference_data = inference->data(); - const T* label_data = label->data(); + const int64_t* indices_data = indices->data(); + const int64_t* label_data = label->data(); float* accuracy_data = accuracy->mutable_data(ctx.GetPlace()); size_t num_samples = inference->dims()[0]; @@ -69,11 +71,11 @@ class AccuracyOpCUDAKernel : public framework::OpKernel { return; } - AccuracyCudaKernel<<< + AccuracyCudaKernel<<< 1, PADDLE_CUDA_NUM_THREADS, 0, reinterpret_cast( ctx.device_context()) - .stream()>>>(num_samples, infer_width, inference_data, label_data, + .stream()>>>(num_samples, infer_width, indices_data, label_data, accuracy_data); } }; @@ -81,7 +83,7 @@ class AccuracyOpCUDAKernel : public framework::OpKernel { } // namespace operators } // namespace paddle +// FIXME(typhoonzero): types of T is for infernece data. +// label data is always int REGISTER_OP_GPU_KERNEL(accuracy, paddle::operators::AccuracyOpCUDAKernel, - paddle::operators::AccuracyOpCUDAKernel, - paddle::operators::AccuracyOpCUDAKernel, - paddle::operators::AccuracyOpCUDAKernel); + paddle::operators::AccuracyOpCUDAKernel); diff --git a/paddle/operators/accuracy_op.h b/paddle/operators/accuracy_op.h index 12c6b9aac8819caedbc02017cee81b37322bb72a..1968b53d19acfddaa514eca6e24d98a298d8d311 100644 --- a/paddle/operators/accuracy_op.h +++ b/paddle/operators/accuracy_op.h @@ -38,14 +38,15 @@ template class AccuracyKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { - auto* inference = ctx.Input("Inference"); + auto* inference = ctx.Input("Out"); + auto* indices = ctx.Input("Indices"); auto* label = ctx.Input("Label"); auto* accuracy = ctx.Output("Accuracy"); float* accuracy_data = accuracy->mutable_data(ctx.GetPlace()); - const T* inference_data = inference->data(); - const T* label_data = label->data(); + const int64_t* indices_data = indices->data(); + const int64_t* label_data = label->data(); size_t num_samples = inference->dims()[0]; size_t class_dim = inference->dims()[1]; @@ -60,7 +61,7 @@ class AccuracyKernel : public framework::OpKernel { for (size_t i = 0; i < num_samples; ++i) { PADDLE_ENFORCE_GE(label_data[i], 0, "label must >= 0"); for (size_t j = 0; j < class_dim; ++j) { - if (inference_data[i * class_dim + j] == label_data[i]) { + if (indices_data[i * class_dim + j] == label_data[i]) { ++num_correct; break; } diff --git a/paddle/operators/activation_op.h b/paddle/operators/activation_op.h index e4c6b2e09cd71f00a2ef73173205b9066c34fcf5..ddd966e26c9abad0d83f8b5c6e3e7d9ad65158a8 100644 --- a/paddle/operators/activation_op.h +++ b/paddle/operators/activation_op.h @@ -547,6 +547,7 @@ struct ELUGradFunctor : public BaseActivationFunctor { } }; +// FIXME(qijun) https://github.com/PaddlePaddle/Paddle/issues/5198 template struct PowFunctor : public BaseActivationFunctor { float factor; diff --git a/paddle/operators/auc_op.cc b/paddle/operators/auc_op.cc index cf3dbc5d10c66cbb344ca8cf8c46432eabef4a07..f5784922af1cafcf4e6873c0e906bbd66ce8ae69 100644 --- a/paddle/operators/auc_op.cc +++ b/paddle/operators/auc_op.cc @@ -23,18 +23,26 @@ class AucOp : public framework::OperatorWithKernel { protected: void InferShape(framework::InferShapeContext *ctx) const override { - PADDLE_ENFORCE(ctx->HasInput("Inference"), - "Input of Inference must be initialized."); + PADDLE_ENFORCE(ctx->HasInput("Out"), "Input of Out must be initialized."); + PADDLE_ENFORCE(ctx->HasInput("Indices"), + "Input of Indices must be initialized."); PADDLE_ENFORCE(ctx->HasInput("Label"), "Input of Label must be initialized."); - auto inference_dim = ctx->GetInputDim("Inference"); - auto label_dim = ctx->GetInputDim("Label"); + auto inference_height = ctx->GetInputDim("Out")[0]; + auto label_height = ctx->GetInputDim("Label")[0]; - PADDLE_ENFORCE_EQ(inference_dim, label_dim, - "inference and label should have same shape"); + PADDLE_ENFORCE_EQ(inference_height, label_height, + "Out and Label should have same height."); ctx->SetOutputDim("AUC", {1}); - ctx->ShareLoD("Inference", /*->*/ "AUC"); + ctx->ShareLoD("Out", /*->*/ "AUC"); + } + + protected: + // IndicateDataType + framework::DataType IndicateDataType( + const framework::ExecutionContext &ctx) const override { + return framework::ToDataType(ctx.Input("Out")->type()); } }; @@ -42,12 +50,18 @@ class AucOpMaker : public framework::OpProtoAndCheckerMaker { public: AucOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker) : OpProtoAndCheckerMaker(proto, op_checker) { - AddInput("Inference", - "A floating point tensor of arbitrary shape and whose values" - "are in the range [0, 1]."); + AddInput("Out", + "A floating point 2D tensor, values are in the range [0, 1]." + "Each row is descend sorted. This input should be the" + "output of topk." + "Typically, this tensor indicates the probability of each label"); + AddInput("Indices", + "An int 2D tensor, indicating the indices of original" + "tensor before sort. Typically, this tensor indicates which label" + "the probability stands for."); AddInput("Label", - "A tensor whose shape matches " - "Inference. Will be cast to bool."); + "A 2D int tensor indicating the label of the training data." + "The height is batch size and width is always 1."); // TODO(typhoonzero): support weight input AddOutput("AUC", "A scalar representing the " diff --git a/paddle/operators/auc_op.h b/paddle/operators/auc_op.h index be6ef29d5f6cff5b9ebdf7d8564b2e2792c3b5cb..e5ac57b038ac32ed35bce35e477ede0cdb5da813 100644 --- a/paddle/operators/auc_op.h +++ b/paddle/operators/auc_op.h @@ -29,7 +29,7 @@ template class AucKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { - auto* inference = ctx.Input("Inference"); + auto* inference = ctx.Input("Out"); auto* label = ctx.Input("Label"); auto* auc = ctx.Output("AUC"); @@ -46,18 +46,11 @@ class AucKernel : public framework::OpKernel { thresholds_list[0] = 0.0f - kEpsilon; thresholds_list[num_thresholds - 1] = 1.0f + kEpsilon; - size_t num_samples = inference->numel(); + size_t batch_size = inference->dims()[0]; + size_t inference_width = inference->dims()[1]; const T* inference_data = inference->data(); - Tensor label_casted; - label_casted.Resize(label->dims()); - bool* label_casted_data = label_casted.mutable_data(ctx.GetPlace()); - - const int* label_data = label->data(); - // cast label_data to bool - for (size_t i = 0; i < num_samples; i++) { - label_casted_data[i] = static_cast(label_data[i]); - } + const int64_t* label_data = label->data(); // Create local tensor for storing the curve: TP, FN, TN, FP // TODO(typhoonzero): use eigen op to caculate these values. @@ -68,23 +61,27 @@ class AucKernel : public framework::OpKernel { true_negative.Resize({num_thresholds}); false_positive.Resize({num_thresholds}); - int* tp_data = true_positive.mutable_data(ctx.GetPlace()); - int* fn_data = false_negative.mutable_data(ctx.GetPlace()); - int* tn_data = true_negative.mutable_data(ctx.GetPlace()); - int* fp_data = false_positive.mutable_data(ctx.GetPlace()); + int64_t* tp_data = true_positive.mutable_data(ctx.GetPlace()); + int64_t* fn_data = false_negative.mutable_data(ctx.GetPlace()); + int64_t* tn_data = true_negative.mutable_data(ctx.GetPlace()); + int64_t* fp_data = false_positive.mutable_data(ctx.GetPlace()); for (int idx_thresh = 0; idx_thresh < num_thresholds; idx_thresh++) { // caculate TP, FN, TN, FP for current thresh - int tp = 0, fn = 0, tn = 0, fp = 0; - for (size_t i = 0; i < num_samples; i++) { - if (label_casted_data[i]) { - if (inference_data[i] >= (thresholds_list[idx_thresh])) { + int64_t tp = 0, fn = 0, tn = 0, fp = 0; + for (size_t i = 0; i < batch_size; i++) { + // NOTE: label_data used as bool, labels >0 will be treated as true. + if (label_data[i]) { + // use first(max) data in each row + if (inference_data[i * inference_width] >= + (thresholds_list[idx_thresh])) { tp++; } else { fn++; } } else { - if (inference_data[i] >= (thresholds_list[idx_thresh])) { + if (inference_data[i * inference_width] >= + (thresholds_list[idx_thresh])) { fp++; } else { tn++; diff --git a/paddle/operators/batch_norm_op.cc b/paddle/operators/batch_norm_op.cc index f7dc990f0db8ae4891ff068fb97899e6d01478da..f2c8be4c54eed9cd0aeb004eeb74a42adc0695f5 100644 --- a/paddle/operators/batch_norm_op.cc +++ b/paddle/operators/batch_norm_op.cc @@ -18,6 +18,7 @@ namespace paddle { namespace operators { using Tensor = framework::Tensor; +using LoDTensor = framework::LoDTensor; template using EigenMatrix = framework::EigenMatrix; @@ -64,6 +65,9 @@ class BatchNormOp : public framework::OperatorWithKernel { (tensor_format == TensorFormat::NCHW ? x_dims[1] : x_dims[x_dims.size() - 1]); + PADDLE_ENFORCE(x_dims.size() >= 3 && x_dims.size() <= 5, + "Input x must have 3 to 5 dimensions."); + PADDLE_ENFORCE_EQ(ctx->GetInputDim("Scale").size(), 1UL); PADDLE_ENFORCE_EQ(ctx->GetInputDim("Scale")[0], C); PADDLE_ENFORCE_EQ(ctx->GetInputDim("Bias").size(), 1UL); @@ -108,10 +112,12 @@ class BatchNormOpMaker : public framework::OpProtoAndCheckerMaker { "Store the global Variance when training"); AddOutput("SavedMean", "Mean of the current mini batch, " - "will apply to output when training"); + "will apply to output when training") + .AsIntermediate(); AddOutput("SavedVariance", "Variance of the current mini batch, " - "will apply to output when training"); + "will apply to output when training") + .AsIntermediate(); AddComment(R"DOC( https://arxiv.org/pdf/1502.03167.pdf @@ -135,7 +141,6 @@ class BatchNormKernel : public framework::OpKernel { const auto *x = ctx.Input("X"); const auto &x_dims = x->dims(); - PADDLE_ENFORCE(x_dims.size() >= 3 && x_dims.size() <= 5, "The Input dim size should be between 3 and 5"); const int N = x_dims[0]; @@ -289,6 +294,25 @@ class BatchNormGradOp : public framework::OperatorWithKernel { ctx->SetOutputDim(framework::GradVarName("Scale"), {C}); ctx->SetOutputDim(framework::GradVarName("Bias"), {C}); } + + framework::DataType IndicateDataType( + const framework::ExecutionContext &ctx) const override { + VLOG(3) << "IndicateDataType " << this->Type(); + const auto *var = ctx.InputVar(framework::GradVarName("Y")); + if (var == nullptr) { + PADDLE_THROW("can't find Y@GRAD"); + } + const Tensor *t = nullptr; + if (var->IsType()) { + t = &var->Get(); + } else if (var->IsType()) { + t = &var->Get(); + } + if (t == nullptr) { + PADDLE_THROW("can't find Y@GRAD"); + } + return framework::ToDataType(t->type()); + } }; template diff --git a/paddle/operators/cast_op.cc b/paddle/operators/cast_op.cc new file mode 100644 index 0000000000000000000000000000000000000000..19187894c3f4803ef241d5e0c159852c0d9687da --- /dev/null +++ b/paddle/operators/cast_op.cc @@ -0,0 +1,73 @@ +/* Copyright (c) 2016 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. */ + +#include "paddle/operators/cast_op.h" +#include "paddle/framework/op_registry.h" + +namespace paddle { +namespace operators { + +class CastOpProtoMaker : public framework::OpProtoAndCheckerMaker { + public: + CastOpProtoMaker(framework::OpProto *proto, + framework::OpAttrChecker *op_checker) + : OpProtoAndCheckerMaker(proto, op_checker) { + AddInput("X", "the input tensor of cast op"); + AddOutput("Out", "the output tensor of cast op"); + AddComment(R"DOC(Cast operator. +cast the input tensor to other data type. +)DOC"); + AddAttr("out_data_type", "output data type"); + AddAttr("in_data_type", "input data type"); + } +}; + +class CastOpInferShape : public framework::InferShapeBase { + public: + void operator()(framework::InferShapeContext *context) const override { + PADDLE_ENFORCE(context->HasInput("X"), "The input of cast op must be set"); + PADDLE_ENFORCE(context->HasOutput("Out"), + "The output of cast op must be set"); + context->SetOutputDim("Out", context->GetInputDim("X")); + context->ShareLoD("X", "Out"); + } +}; + +class CastOpGradMaker : public framework::SingleGradOpDescMaker { + public: + using framework::SingleGradOpDescMaker::SingleGradOpDescMaker; + + protected: + std::unique_ptr Apply() const override { + auto grad = new framework::OpDescBind(); + grad->SetType("cast"); + grad->SetInput("X", OutputGrad("Out")); + grad->SetOutput("Out", InputGrad("X")); + grad->SetAttr("out_data_type", GetAttr("in_data_type")); + grad->SetAttr("in_data_type", GetAttr("out_data_type")); + return std::unique_ptr(grad); + } +}; + +} // namespace operators +} // namespace paddle + +namespace ops = paddle::operators; +using CPU = paddle::platform::CPUPlace; +REGISTER_OP_WITH_KERNEL(cast, ops::CastOpGradMaker, ops::CastOpInferShape, + ops::CastOpProtoMaker); +REGISTER_OP_CPU_KERNEL(cast, ops::CastOpKernel, + ops::CastOpKernel, + ops::CastOpKernel, + ops::CastOpKernel); diff --git a/paddle/operators/cast_op.cu b/paddle/operators/cast_op.cu new file mode 100644 index 0000000000000000000000000000000000000000..fb75ddbabfefd8d00420d8c96f958abcb8fdce62 --- /dev/null +++ b/paddle/operators/cast_op.cu @@ -0,0 +1,22 @@ +/* Copyright (c) 2016 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. */ + +#include "paddle/operators/cast_op.h" + +template +using CastOpKernel = + paddle::operators::CastOpKernel; + +REGISTER_OP_GPU_KERNEL(cast, CastOpKernel, CastOpKernel, + CastOpKernel, CastOpKernel); diff --git a/paddle/operators/cast_op.h b/paddle/operators/cast_op.h new file mode 100644 index 0000000000000000000000000000000000000000..ffdbff7030afedab2efc06479ac86ad70c185f48 --- /dev/null +++ b/paddle/operators/cast_op.h @@ -0,0 +1,64 @@ +/* Copyright (c) 2016 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. */ + +#pragma once + +#include "paddle/framework/data_type.h" +#include "paddle/framework/framework.pb.h" +#include "paddle/framework/op_registry.h" +#include "paddle/platform/transform.h" + +namespace paddle { +namespace operators { + +template +struct CastOpTransformFunctor { + HOSTDEVICE OutT operator()(InT in) const { return static_cast(in); } +}; + +template +struct CastOpFunctor { + const framework::Tensor* in_; + framework::Tensor* out_; + const platform::DeviceContext& ctx_; + CastOpFunctor(const framework::Tensor* in, framework::Tensor* out, + const platform::DeviceContext& ctx) + : in_(in), out_(out), ctx_(ctx) {} + + template + void operator()() const { + auto* in_begin = in_->data(); + auto numel = in_->numel(); + auto* in_end = in_begin + numel; + auto* out_begin = out_->mutable_data(ctx_.GetPlace()); + platform::Transform trans; + trans(ctx_, in_begin, in_end, out_begin, + CastOpTransformFunctor()); + } +}; + +template +class CastOpKernel : public framework::OpKernel { + public: + void Compute(const framework::ExecutionContext& context) const override { + auto* in = context.Input("X"); + auto* out = context.Output("Out"); + framework::VisitDataType( + static_cast(context.Attr("out_data_type")), + CastOpFunctor(in, out, context.device_context())); + } +}; + +} // namespace operators +} // namespace paddle diff --git a/paddle/operators/cross_entropy_op.cu b/paddle/operators/cross_entropy_op.cu index 5f8a6cd5ef6fbb554112085adc6b85ef8e765e86..a523cb6fcec16d309f6bb3baf8549bf14756fd7d 100644 --- a/paddle/operators/cross_entropy_op.cu +++ b/paddle/operators/cross_entropy_op.cu @@ -21,7 +21,7 @@ namespace { template __global__ void CrossEntropyGradientKernel(T* dX, const T* dY, const T* X, - const int* label, const int N, + const int64_t* label, const int N, const int D) { // TOOD(qingqing) define CUDA_1D_KERNEL_LOOP macro in a common file. // CUDA_1D_KERNEL_LOOP(i, N) { @@ -77,8 +77,8 @@ class CrossEntropyGradientOpCUDAKernel : public framework::OpKernel { T* dx_data = dx->mutable_data(ctx.GetPlace()); const T* x_data = x->data(); - int batch_size = x->dims()[0]; - int class_num = x->dims()[1]; + int64_t batch_size = x->dims()[0]; + int64_t class_num = x->dims()[1]; int block = 512; int grid = (batch_size * class_num + block - 1) / block; @@ -93,7 +93,7 @@ class CrossEntropyGradientOpCUDAKernel : public framework::OpKernel { } else { math::SetConstant functor; functor(ctx.device_context(), dx, 0); - auto* label_data = label->data(); + auto* label_data = label->data(); grid = (batch_size + block - 1) / block; CrossEntropyGradientKernel<<< grid, block, 0, reinterpret_cast( diff --git a/paddle/operators/cross_entropy_op.h b/paddle/operators/cross_entropy_op.h index 42f282103b5609e3c987fc4a83113f86532f74d6..37db0a930a6aea0ba333395ca9c5b9d231c07b32 100644 --- a/paddle/operators/cross_entropy_op.h +++ b/paddle/operators/cross_entropy_op.h @@ -54,7 +54,7 @@ class CrossEntropyGradientOpKernel : public framework::OpKernel { Tensor* dx = ctx.Output(framework::GradVarName("X")); T* dx_data = dx->mutable_data(ctx.GetPlace()); - int class_num = x->dims()[1]; + int64_t class_num = x->dims()[1]; if (ctx.Attr("soft_label")) { auto x_mat = EigenMatrix::From(*x); auto dy_mat = EigenMatrix::From(*dy); @@ -62,20 +62,20 @@ class CrossEntropyGradientOpKernel : public framework::OpKernel { auto dx_mat = EigenMatrix::From(*dx); dx_mat.device(ctx.GetEigenDevice()) = - -(lbl_mat * dy_mat.broadcast(Eigen::DSizes(1, class_num)) / - x_mat); + -(lbl_mat * + dy_mat.broadcast(Eigen::DSizes(1, class_num)) / x_mat); } else { - int batch_size = x->dims()[0]; + int64_t batch_size = x->dims()[0]; const T* dy_data = dy->data(); const T* x_data = x->data(); - const int* label_data = label->data(); + const int64_t* label_data = label->data(); math::SetConstant functor; functor(ctx.device_context(), dx, 0); - for (int i = 0; i < batch_size; ++i) { + for (int64_t i = 0; i < batch_size; ++i) { PADDLE_ASSERT(label_data[i] >= 0 || label_data[i] < class_num); - int index = i * class_num + label_data[i]; + int64_t index = i * class_num + label_data[i]; dx_data[index] = -dy_data[i] / x_data[index]; } } diff --git a/paddle/operators/feed_op.cc b/paddle/operators/feed_op.cc index 0f1722a5383c80ff2ede0801d34f22a80fbc6e52..0e5b263eae904d97b61d41691b848e4fa2c17971 100644 --- a/paddle/operators/feed_op.cc +++ b/paddle/operators/feed_op.cc @@ -41,7 +41,7 @@ class FeedOp : public framework::OperatorBase { auto col = Attr("col"); - VLOG(3) << "Feed Var " << feed_var_name << "'s " << col << " column to var" + VLOG(3) << "Feed Var " << feed_var_name << "'s " << col << " column to var " << out_name; auto &feed_list = feed_var->Get(); diff --git a/paddle/operators/lookup_table_op.cc b/paddle/operators/lookup_table_op.cc index ad86a2e5bc23b2b0ea853971cf79dec745e9706a..8fdd42352e5e6857e4bf0e4645f82c8e2fcdc6fd 100644 --- a/paddle/operators/lookup_table_op.cc +++ b/paddle/operators/lookup_table_op.cc @@ -13,6 +13,7 @@ limitations under the License. */ #include "paddle/operators/lookup_table_op.h" +#include "paddle/framework/var_type_inference.h" namespace paddle { namespace operators { @@ -60,6 +61,7 @@ class LookupTableOpMaker : public framework::OpProtoAndCheckerMaker { "Ids must be a column vector with rank = 2." "The 2nd dimension size must be 1"); AddOutput("Out", "The lookup results, which have the same type with W."); + AddAttr("is_sparse", "Sparse update").SetDefault(false); AddComment(R"DOC( This operator is used to perform lookups on the parameter W, then concatenated into a dense tensor. @@ -70,6 +72,15 @@ or not. And the output only shares the LoD with input `Ids`. } }; +class LookupTableOpGradDescMaker + : public framework::DefaultGradOpDescMaker { + using ::paddle::framework::DefaultGradOpDescMaker< + true>::DefaultGradOpDescMaker; + + protected: + virtual std::string GradOpType() const { return "lookup_table_grad"; } +}; + class LookupTableOpGrad : public framework::OperatorWithKernel { public: using framework::OperatorWithKernel::OperatorWithKernel; @@ -86,12 +97,35 @@ class LookupTableOpGrad : public framework::OperatorWithKernel { } }; +class LookupTableOpGradVarTypeInference : public framework::VarTypeInference { + public: + void operator()(const framework::OpDescBind& op_desc, + framework::BlockDescBind* block) const override { + auto out_var_name = op_desc.Output(framework::GradVarName("W")).front(); + auto attr = op_desc.GetAttr("is_sparse"); + bool is_sparse = boost::get(attr); + if (is_sparse) { + VLOG(3) << "lookup_table_grad op " << framework::GradVarName("W") + << " is set to SelectedRows"; + block->Var(out_var_name)->SetType(framework::VarDesc::SELECTED_ROWS); + } else { + VLOG(3) << "lookup_table_grad op " << framework::GradVarName("W") + << " is set to LoDTensor"; + block->Var(out_var_name)->SetType(framework::VarDesc::LOD_TENSOR); + } + } +}; + } // namespace operators } // namespace paddle namespace ops = paddle::operators; -REGISTER_OP(lookup_table, ops::LookupTableOp, ops::LookupTableOpMaker, - lookup_table_grad, ops::LookupTableOpGrad); - -REGISTER_OP_CPU_KERNEL(lookup_table, ops::LookupTableKernel); -REGISTER_OP_CPU_KERNEL(lookup_table_grad, ops::LookupTableGradKernel); +REGISTER_OPERATOR(lookup_table, ops::LookupTableOp, + ops::LookupTableOpGradDescMaker, ops::LookupTableOpMaker); +REGISTER_OPERATOR(lookup_table_grad, ops::LookupTableOpGrad, + ops::LookupTableOpGradVarTypeInference); + +REGISTER_OP_CPU_KERNEL(lookup_table, ops::LookupTableKernel, + ops::LookupTableKernel); +REGISTER_OP_CPU_KERNEL(lookup_table_grad, ops::LookupTableGradKernel, + ops::LookupTableGradKernel); diff --git a/paddle/operators/lookup_table_op.cu b/paddle/operators/lookup_table_op.cu index c3808fa9a8de031fcae3ac0417e8c4330b2f5aad..837b2a1f4c94f201c0ab498671f936aab6c7a811 100644 --- a/paddle/operators/lookup_table_op.cu +++ b/paddle/operators/lookup_table_op.cu @@ -1,11 +1,8 @@ /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. - 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. @@ -14,22 +11,21 @@ #include "paddle/framework/eigen.h" #include "paddle/framework/op_registry.h" +#include "paddle/operators/lookup_table_op.h" #include "paddle/platform/assert.h" #include "paddle/platform/cuda_helper.h" namespace paddle { namespace operators { -using Tensor = framework::Tensor; - template -__global__ void LookupTable(T* output, const T* table, const int32_t* ids, - const int N, const int K, const int D) { +__global__ void LookupTable(T* output, const T* table, const int64_t* ids, + const int64_t N, const int64_t K, const int64_t D) { int idx = threadIdx.x; int idy = blockIdx.x + threadIdx.y * GridDimX; while (idy < K) { - int id = ids[idy]; + int64_t id = ids[idy]; PADDLE_ASSERT(id >= 0); PADDLE_ASSERT(id < N); T* out = output + idy * D; @@ -42,8 +38,9 @@ __global__ void LookupTable(T* output, const T* table, const int32_t* ids, } template -__global__ void LookupTableGrad(T* table, const T* output, const int32_t* ids, - const int N, const int K, const int D) { +__global__ void LookupTableGrad(T* table, const T* output, const int64_t* ids, + const int64_t N, const int64_t K, + const int64_t D) { int idx = threadIdx.x; int idy = blockIdx.x + threadIdx.y * GridDimX; @@ -71,7 +68,7 @@ class LookupTableCUDAKernel : public framework::OpKernel { size_t N = table_t->dims()[0]; size_t D = table_t->dims()[1]; size_t K = ids_t->numel(); - auto ids = ids_t->data(); + auto ids = ids_t->data(); auto table = table_t->data(); auto output = output_t->mutable_data(context.GetPlace()); @@ -88,27 +85,63 @@ template class LookupTableGradCUDAKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { - auto ids_t = context.Input("Ids"); - auto d_output_t = context.Input(framework::GradVarName("Out")); - auto d_table_t = context.Output(framework::GradVarName("W")); - - int N = d_table_t->dims()[0]; - int D = d_table_t->dims()[1]; - int K = ids_t->numel(); - const int32_t* ids = ids_t->data(); - const T* d_output = d_output_t->data(); - T* d_table = d_table_t->mutable_data(context.GetPlace()); - - auto t = framework::EigenVector::Flatten(*d_table_t); - t.device(context.GetEigenDevice()) = - t.constant(static_cast(0)); - - dim3 threads(128, 8); - dim3 grids(8, 1); - LookupTableGrad<<< - grids, threads, 0, reinterpret_cast( + bool is_sparse = context.Attr("is_sparse"); + if (is_sparse) { + auto* ids = context.Input("Ids"); + auto* table = context.Input("W"); + auto* d_output = context.Input(framework::GradVarName("Out")); + auto* d_table = context.Output(framework::GradVarName("W")); + + auto* ids_data = ids->data(); + auto ids_dim = ids->dims(); + + auto stream = reinterpret_cast( + context.device_context()) + .stream(); + // copy GPU memory to CPU pinned memory + framework::Vector new_rows; + new_rows.resize(ids_dim[0]); + auto gpu_place = boost::get(context.GetPlace()); + + memory::Copy(platform::CPUPlace(), new_rows.data(), gpu_place, ids_data, + ids_dim[0] * sizeof(int64_t), stream); + + d_table->set_rows(new_rows); + + auto* d_table_value = d_table->mutable_value(); + d_table_value->Resize({ids_dim[0], table->dims()[1]}); + d_table_value->mutable_data(context.GetPlace()); + + auto* d_table_data = d_table_value->data(); + auto* d_output_data = d_output->data(); + PADDLE_ENFORCE_EQ(d_table_value->dims(), d_output->dims()); + memory::Copy(gpu_place, d_table_data, gpu_place, d_output_data, + d_output->numel(), stream); + + } else { + auto ids_t = context.Input("Ids"); + auto d_output_t = context.Input(framework::GradVarName("Out")); + auto d_table_t = context.Output(framework::GradVarName("W")); + + int N = d_table_t->dims()[0]; + int D = d_table_t->dims()[1]; + int K = ids_t->numel(); + const int64_t* ids = ids_t->data(); + const T* d_output = d_output_t->data(); + T* d_table = d_table_t->mutable_data(context.GetPlace()); + + auto t = framework::EigenVector::Flatten(*d_table_t); + t.device(context.GetEigenDevice()) = + t.constant(static_cast(0)); + + dim3 threads(128, 8); + dim3 grids(8, 1); + LookupTableGrad<<( context.device_context()) .stream()>>>(d_table, d_output, ids, N, K, D); + } } }; @@ -116,6 +149,7 @@ class LookupTableGradCUDAKernel : public framework::OpKernel { } // namespace paddle namespace ops = paddle::operators; -REGISTER_OP_GPU_KERNEL(lookup_table, ops::LookupTableCUDAKernel); -REGISTER_OP_GPU_KERNEL(lookup_table_grad, - ops::LookupTableGradCUDAKernel); +REGISTER_OP_GPU_KERNEL(lookup_table, ops::LookupTableCUDAKernel, + ops::LookupTableCUDAKernel); +REGISTER_OP_GPU_KERNEL(lookup_table_grad, ops::LookupTableGradCUDAKernel, + ops::LookupTableGradCUDAKernel); diff --git a/paddle/operators/lookup_table_op.h b/paddle/operators/lookup_table_op.h index dfead2fc5b25b9be26bb19cd74a3a94daf62cca6..54067cd01d3ef35a050a3c2565ea19cb6520bcec 100644 --- a/paddle/operators/lookup_table_op.h +++ b/paddle/operators/lookup_table_op.h @@ -1,11 +1,8 @@ /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. - 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. @@ -15,12 +12,15 @@ #pragma once #include "paddle/framework/eigen.h" +#include "paddle/framework/lod_tensor.h" #include "paddle/framework/op_registry.h" +#include "paddle/framework/selected_rows.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; +using SelectedRows = framework::SelectedRows; template class LookupTableKernel : public framework::OpKernel { @@ -32,7 +32,7 @@ class LookupTableKernel : public framework::OpKernel { int N = table_t->dims()[0]; int D = table_t->dims()[1]; - auto ids = ids_t->data(); + auto ids = ids_t->data(); auto table = table_t->data(); auto output = output_t->mutable_data(context.GetPlace()); for (int64_t i = 0; i < ids_t->numel(); ++i) { @@ -47,25 +47,55 @@ template class LookupTableGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { - auto ids_t = context.Input("Ids"); - auto d_output_t = context.Input(framework::GradVarName("Out")); - auto d_table_t = context.Output(framework::GradVarName("W")); + bool is_sparse = context.Attr("is_sparse"); + if (is_sparse) { + auto* ids = context.Input("Ids"); + auto* table = context.Input("W"); + auto* d_output = context.Input(framework::GradVarName("Out")); + auto* d_table = context.Output(framework::GradVarName("W")); - int N = d_table_t->dims()[0]; - int D = d_table_t->dims()[1]; - auto ids = ids_t->data(); - const T* d_output = d_output_t->data(); - T* d_table = d_table_t->mutable_data(context.GetPlace()); + auto* ids_data = ids->data(); + auto ids_dim = ids->dims(); - auto t = framework::EigenVector::Flatten(*d_table_t); - t.device(context.GetEigenDevice()) = - t.constant(static_cast(0)); + framework::Vector new_rows; + new_rows.reserve(ids_dim[0]); + for (int64_t i = 0; i < ids_dim[0]; i++) { + new_rows.push_back(ids_data[i]); + } + d_table->set_rows(new_rows); - for (int64_t i = 0; i < ids_t->numel(); ++i) { - PADDLE_ENFORCE_LT(ids[i], N); - PADDLE_ENFORCE_GE(ids[i], 0); - for (int j = 0; j < D; ++j) { - d_table[ids[i] * D + j] += d_output[i * D + j]; + auto* d_table_value = d_table->mutable_value(); + d_table_value->Resize({ids_dim[0], table->dims()[1]}); + d_table_value->mutable_data(context.GetPlace()); + + d_table->set_height(table->dims()[0]); + + auto* d_output_data = d_output->data(); + auto* d_table_data = d_table_value->data(); + + PADDLE_ENFORCE_EQ(d_table_value->dims(), d_output->dims()); + memcpy(d_table_data, d_output_data, sizeof(T) * d_output->numel()); + } else { + auto* ids = context.Input("Ids"); + auto* d_output = context.Input(framework::GradVarName("Out")); + auto* d_table = context.Output(framework::GradVarName("W")); + auto* table = context.Input("W"); + + auto* ids_data = ids->data(); + auto ids_dim = ids->dims(); + + int N = table->dims()[0]; + int D = d_output->dims()[1]; + + auto* d_output_data = d_output->data(); + auto* d_table_data = d_table->mutable_data(context.GetPlace()); + + for (int64_t i = 0; i < ids->numel(); ++i) { + PADDLE_ENFORCE_LT(ids_data[i], N); + PADDLE_ENFORCE_GE(ids_data[i], 0); + for (int j = 0; j < D; ++j) { + d_table_data[ids_data[i] * D + j] = d_output_data[i * D + j]; + } } } } diff --git a/paddle/operators/math/context_project.h b/paddle/operators/math/context_project.h index e37f3a5bf2bd59e46f66aa3a8284e05d79dbc790..e0283360414fbdfb3dae2e94b45c9c8daeed3c74 100644 --- a/paddle/operators/math/context_project.h +++ b/paddle/operators/math/context_project.h @@ -16,36 +16,36 @@ limitations under the License. */ #include "paddle/framework/eigen.h" #include "paddle/framework/lod_tensor.h" -#include "paddle/framework/tensor.h" #include "paddle/operators/math/im2col.h" namespace paddle { namespace operators { namespace math { +using Tensor = framework::Tensor; +using LoDTensor = framework::LoDTensor; template using EigenMatrix = framework::EigenMatrix; + /* - * \brief Context projection concatenate features in adjacent time steps in + * \brief Context projection concatenates features in adjacent time-steps in * a sequence. The i-th row of the output is the concatenation of * context_length rows of the input. The context_length rows are the * consecutive rows from the i+shift_start row. - + * ContextProjectGradFunctor is the inverse process of ContextProjectFunctor. + * * \param in Input data. - * \param Shape The shape of Input data, - * [minibatch, number_of_input_features]. - * \param type A float LoDTensor. + * \param Shape The shape of Input data: + * [mini-batch, input_hidden_size]. * * \param padding_data Padding data. - * \param Shape The shape of Padding data, - * [up_pad + down_pad, number_of_input_features]. - * \param type A float Tensor. + * \param Shape The shape of Padding data: + * [up_pad + down_pad, input_hidden_size]. * * \param col Col data. - * \param Shape The shape of Col data, - * [minibatch, context_length * number_of_input_features]. - * \param type A float Tensor. + * \param Shape The shape of Col data: + * [mini-batch, context_length * input_hidden_size]. * * For a mini-batch of 2 variable lengths sentences, containing 3, and 1 * time-steps: @@ -63,72 +63,170 @@ using EigenMatrix = framework::EigenMatrix; * representation is 2. * * - Case1: - * If context_start is -1 and padding_trainable is false, we use zero to pad - * instead of learned weight to pad, - * and the context_lenth is 3, the output (Out) is: + * If context_start is -1 and padding_trainable is false, we use zero to pad + * instead of learned weight to pad, + * and the context_length is 3, the output (Out) is: * - * Out =[[0, 0, a1, a2, b1, b2; - * a1, a2, b1, b2, c1, c2; - * b1, b2, c1, c2, 0, 0 ] - * [0, 0, d1, d2, 0, 0 ]] + * Out =[[0, 0, a1, a2, b1, b2; + * a1, a2, b1, b2, c1, c2; + * b1, b2, c1, c2, 0, 0 ] + * [0, 0, d1, d2, 0, 0 ]] * * - Case2: - * If context_start is -1 and padding_trainable is true, we use learned weight - * to pad, - * and the context_lenth is 3, the output (Out) is: + * If context_start is -1 and padding_trainable is true, we use learned weight + * to pad, + * and the context_length is 3, the output (Out) is: * - * Out = [[w1, w2, a1, a2, b1, b2; - * a1, a2, b1, b2, c1, c2; - * b1, b2, c1, c2, w3, w4] - * [w1, w2, d1, d2, w3, w4]] + * Out = [[w1, w2, a1, a2, b1, b2; + * a1, a2, b1, b2, c1, c2; + * b1, b2, c1, c2, w3, w4] + * [w1, w2, d1, d2, w3, w4]] * */ template class ContextProjectFunctor { public: - void operator()(const platform::DeviceContext& context, - framework::LoDTensor& in, framework::Tensor& padding_data, - framework::Tensor& col, bool padding_trainable, + void operator()(const platform::DeviceContext& context, const LoDTensor& in, + const Tensor& padding_data, Tensor& col, + bool padding_trainable, int context_start, int context_length, + int context_stride, int up_pad, int down_pad) { + auto lod_level_0 = in.lod()[0]; + + math::Im2ColFunctor im2col_ocf; + + int input_row_begin, input_row_end; + int sequence_height, sequence_width; + sequence_width = in.dims()[1]; + + for (int i = 0; i < static_cast(lod_level_0.size()) - 1; ++i) { + input_row_begin = (context_start > 0) + ? static_cast(lod_level_0[i]) + context_start + : static_cast(lod_level_0[i]); + input_row_end = static_cast(lod_level_0[i + 1]); + + Tensor out_t = col.Slice(static_cast(lod_level_0[i]), + static_cast(lod_level_0[i + 1])); + + sequence_height = static_cast(out_t.dims()[0]); + + if (input_row_begin < input_row_end) { + Tensor in_t = in.Slice(input_row_begin, input_row_end); + + std::vector output_shape( + {sequence_height, 1, 1, context_length, + sequence_width}); // output_height, output_width, + // input_channels, filter_height, filter_width + out_t.Resize(framework::make_ddim(output_shape)); + + std::vector input_shape( + {1, input_row_end - input_row_begin, + sequence_width}); // input_channels, input_height, input_width + in_t.Resize(framework::make_ddim(input_shape)); + + im2col_ocf(context, in_t, out_t, + /*stride_height*/ context_stride, /*stride_width*/ 1, up_pad, + down_pad, 0, 0); + out_t.Resize({sequence_height, context_length * sequence_width}); + } + } + if (padding_trainable) { + for (int i = 0; i < static_cast(lod_level_0.size()) - 1; ++i) { + Tensor out_t = col.Slice(static_cast(lod_level_0[i]), + static_cast(lod_level_0[i + 1])); + + sequence_height = static_cast(out_t.dims()[0]); + + // add up trainable data + out_t.Resize({sequence_height * context_length, sequence_width}); + + if (up_pad > 0) { // add up pad + int padding_rows = std::min( + up_pad, static_cast(lod_level_0[i + 1] - lod_level_0[i])); + + for (int k = 0; k < padding_rows; ++k) { + int padding_size = + k + context_length < up_pad ? context_length : up_pad - k; + Tensor out_t_sub = out_t.Slice(k * context_length, + k * context_length + padding_size); + Tensor w_sub = padding_data.Slice(k, k + padding_size); + auto out_t_sub_e = EigenMatrix::From(out_t_sub); + auto w_sub_e = EigenMatrix::From(w_sub); + out_t_sub_e.device(*context.GetEigenDevice()) = w_sub_e; + } + } + if (down_pad > 0) { // add down pad + int down_pad_begin_row = + std::max(0, + (sequence_height - context_start - context_length) + 1) + + 1; + int padding_begin = std::max(0, context_start - sequence_height); + int padding_size = + sequence_height - context_start >= context_length + ? 1 + : context_length - (sequence_height - context_start); + if (context_start >= sequence_height) padding_size = context_length; + int padding_idx = padding_begin; + for (int t = 0; t + down_pad_begin_row <= sequence_height; + ++t, ++padding_size) { + if (context_start >= sequence_height) padding_size = context_length; + if (padding_size > context_length) { + padding_size = context_length; + padding_idx++; + } + if (padding_begin > 0 || sequence_height == context_start) + padding_idx = padding_begin + t; + + Tensor out_t_sub = out_t.Slice( + (down_pad_begin_row + t) * context_length - padding_size, + (down_pad_begin_row + t) * context_length); + Tensor w_sub = padding_data.Slice( + up_pad + padding_idx, up_pad + padding_idx + padding_size); + auto out_t_sub_e = EigenMatrix::From(out_t_sub); + auto w_sub_e = EigenMatrix::From(w_sub); + out_t_sub_e.device(*context.GetEigenDevice()) = w_sub_e; + } + } + out_t.Resize({sequence_height, context_length * sequence_width}); + } + } + } +}; + +template +class ContextProjectGradFunctor { + public: + void operator()(const platform::DeviceContext& context, LoDTensor& in, + Tensor& padding_data, Tensor& col, bool padding_trainable, int context_start, int context_length, int context_stride, - int up_pad, int down_pad, bool gradient, bool input_grad, - bool pad_grad) { + int up_pad, int down_pad, bool input_grad, bool pad_grad) { auto lod_level_0 = in.lod()[0]; - paddle::operators::math::Im2ColFunctor< - paddle::operators::math::ColFormat::kOCF, Place, float> - im2col_ocf; - paddle::operators::math::Col2ImFunctor< - paddle::operators::math::ColFormat::kOCF, Place, float> - col2im_ocf; + math::Col2ImFunctor col2im_ocf; int input_row_begin, input_row_end; int sequence_height, sequence_width; sequence_width = in.dims()[1]; - input_grad = gradient && input_grad; - pad_grad = gradient && pad_grad; - if (!gradient || input_grad) { + if (input_grad) { for (int i = 0; i < static_cast(lod_level_0.size()) - 1; ++i) { input_row_begin = (context_start > 0) ? static_cast(lod_level_0[i]) + context_start : static_cast(lod_level_0[i]); input_row_end = static_cast(lod_level_0[i + 1]); - framework::Tensor out_t = - col.Slice(static_cast(lod_level_0[i]), - static_cast(lod_level_0[i + 1])); + Tensor out_t = col.Slice(static_cast(lod_level_0[i]), + static_cast(lod_level_0[i + 1])); sequence_height = static_cast(out_t.dims()[0]); if (input_row_begin < input_row_end) { - framework::Tensor in_t = in.Slice(input_row_begin, input_row_end); + Tensor in_t = in.Slice(input_row_begin, input_row_end); std::vector output_shape( {sequence_height, 1, 1, context_length, sequence_width}); // output_height, output_width, // input_channels, filter_height, filter_width - out_t.Resize(framework::make_ddim(output_shape)); std::vector input_shape( @@ -136,53 +234,39 @@ class ContextProjectFunctor { sequence_width}); // input_channels, input_height, input_width in_t.Resize(framework::make_ddim(input_shape)); - if (gradient) { - col2im_ocf(context, in_t, out_t, - /*stride_height*/ context_stride, /*stride_width*/ 1, - up_pad, down_pad, 0, 0); - } else { - im2col_ocf(context, in_t, out_t, - /*stride_height*/ context_stride, /*stride_width*/ 1, - up_pad, down_pad, 0, 0); - } + col2im_ocf(context, in_t, out_t, + /*stride_height*/ context_stride, /*stride_width*/ 1, + up_pad, down_pad, 0, 0); out_t.Resize({sequence_height, context_length * sequence_width}); } } } - if (!gradient || pad_grad) { + if (pad_grad) { if (padding_trainable) { for (int i = 0; i < static_cast(lod_level_0.size()) - 1; ++i) { - framework::Tensor out_t = - col.Slice(static_cast(lod_level_0[i]), - static_cast(lod_level_0[i + 1])); + Tensor out_t = col.Slice(static_cast(lod_level_0[i]), + static_cast(lod_level_0[i + 1])); sequence_height = static_cast(out_t.dims()[0]); - - // add up trainable data out_t.Resize({sequence_height * context_length, sequence_width}); - if (up_pad > 0) { // add up pad + if (up_pad > 0) { int padding_rows = std::min( up_pad, static_cast(lod_level_0[i + 1] - lod_level_0[i])); for (int k = 0; k < padding_rows; ++k) { int padding_size = k + context_length < up_pad ? context_length : up_pad - k; - framework::Tensor out_t_sub = out_t.Slice( - k * context_length, k * context_length + padding_size); - framework::Tensor w_sub = padding_data.Slice(k, k + padding_size); - // in this block, using EigenVector::Flatten is ok too. + Tensor out_t_sub = out_t.Slice(k * context_length, + k * context_length + padding_size); + Tensor w_sub = padding_data.Slice(k, k + padding_size); auto out_t_sub_e = EigenMatrix::From(out_t_sub); auto w_sub_e = EigenMatrix::From(w_sub); - if (gradient) { - w_sub_e.device(*context.GetEigenDevice()) = - w_sub_e + out_t_sub_e; - } else { - out_t_sub_e.device(*context.GetEigenDevice()) = w_sub_e; - } + w_sub_e.device(*context.GetEigenDevice()) = + w_sub_e + out_t_sub_e; } } - if (down_pad > 0) { // add down pad + if (down_pad > 0) { int down_pad_begin_row = std::max( 0, (sequence_height - context_start - context_length) + 1) + @@ -204,19 +288,16 @@ class ContextProjectFunctor { } if (padding_begin > 0 || sequence_height == context_start) padding_idx = padding_begin + t; - framework::Tensor out_t_sub = out_t.Slice( + + Tensor out_t_sub = out_t.Slice( (down_pad_begin_row + t) * context_length - padding_size, (down_pad_begin_row + t) * context_length); - framework::Tensor w_sub = padding_data.Slice( + Tensor w_sub = padding_data.Slice( up_pad + padding_idx, up_pad + padding_idx + padding_size); auto out_t_sub_e = EigenMatrix::From(out_t_sub); auto w_sub_e = EigenMatrix::From(w_sub); - if (gradient) { - w_sub_e.device(*context.GetEigenDevice()) = - w_sub_e + out_t_sub_e; - } else { - out_t_sub_e.device(*context.GetEigenDevice()) = w_sub_e; - } + w_sub_e.device(*context.GetEigenDevice()) = + w_sub_e + out_t_sub_e; } } out_t.Resize({sequence_height, context_length * sequence_width}); diff --git a/paddle/operators/math/cross_entropy.cc b/paddle/operators/math/cross_entropy.cc index cb28add3f01c321797b75230f45f19f8d403387a..cf238a58e0a0b930077b0376a71dc02c5b31efe5 100644 --- a/paddle/operators/math/cross_entropy.cc +++ b/paddle/operators/math/cross_entropy.cc @@ -44,7 +44,7 @@ class CrossEntropyFunctor { const T* prob_data = prob->data(); T* loss_data = out->data(); - const int* label_data = labels->data(); + const int64_t* label_data = labels->data(); for (int i = 0; i < batch_size; ++i) { int index = i * class_num + label_data[i]; loss_data[i] = -math::TolerableValue()(std::log(prob_data[index])); diff --git a/paddle/operators/math/cross_entropy.cu b/paddle/operators/math/cross_entropy.cu index 80db130aa0900553db30ead8f2cd5b850f3df1e5..651c08f740c2991b11c210c9bf012e505adc1835 100644 --- a/paddle/operators/math/cross_entropy.cu +++ b/paddle/operators/math/cross_entropy.cu @@ -20,7 +20,7 @@ namespace math { namespace { template -__global__ void CrossEntropyKernel(T* Y, const T* X, const int* label, +__global__ void CrossEntropyKernel(T* Y, const T* X, const int64_t* label, const int N, const int D) { for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < N; i += blockDim.x * gridDim.x) { @@ -115,7 +115,7 @@ class CrossEntropyFunctor { reinterpret_cast(ctx).stream()>>>( loss_data, prob_data, label_data, class_num); } else { - const int* label_data = labels->data(); + const int64_t* label_data = labels->data(); int block = 512; int grid = (batch_size + block - 1) / block; CrossEntropyKernel<<< diff --git a/paddle/operators/nccl/CMakeLists.txt b/paddle/operators/nccl/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..ce0ddd89bfb0d73e237a6f9a777376624d8ef2d4 --- /dev/null +++ b/paddle/operators/nccl/CMakeLists.txt @@ -0,0 +1,3 @@ +if(WITH_GPU) + nv_library(nccl_common SRCS nccl_gpu_common.cc DEPS device_context operator ) +endif() diff --git a/paddle/operators/nccl/nccl_gpu_common.cc b/paddle/operators/nccl/nccl_gpu_common.cc new file mode 100644 index 0000000000000000000000000000000000000000..6be735e4c731f79684e0bdac3d69a30b328fed84 --- /dev/null +++ b/paddle/operators/nccl/nccl_gpu_common.cc @@ -0,0 +1,17 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. + 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. */ + +#include "paddle/operators/nccl/nccl_gpu_common.h" +#include "paddle/platform/gpu_info.h" + +namespace paddle { +namespace platform {} // namespace platform +} // namespace paddle diff --git a/paddle/operators/nccl/nccl_gpu_common.h b/paddle/operators/nccl/nccl_gpu_common.h new file mode 100644 index 0000000000000000000000000000000000000000..5858cd4839d367bb888b2b98cde2225751391162 --- /dev/null +++ b/paddle/operators/nccl/nccl_gpu_common.h @@ -0,0 +1,63 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. + + 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. */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include "paddle/platform/device_context.h" +#include "paddle/platform/dynload/nccl.h" +#include "paddle/platform/enforce.h" +#include "paddle/platform/macros.h" + +namespace paddle { +namespace platform { + +constexpr int kInvalidGPUId = -1; + +struct Communicator { + std::vector comms_; + std::unordered_map comm_id_map_; + + Communicator() {} + + int GetCommId(int device_id) const { return comm_id_map_.at(device_id); } + + void InitAll(const std::vector& gpus) { + comms_.resize(gpus.size()); + for (size_t i = 0; i < gpus.size(); ++i) { + comm_id_map_[gpus[i]] = i; + } + PADDLE_ENFORCE( + dynload::ncclCommInitAll(comms_.data(), gpus.size(), gpus.data())); + } + + ~Communicator() { + for (size_t i = 0; i < comms_.size(); ++i) { + // FIXME(dzh) : PADDLE_ENFORCE return void + dynload::ncclCommDestroy(comms_[i]); + } + } + + DISABLE_COPY_AND_ASSIGN(Communicator); +}; + +} // namespace platform +} // namespace paddle diff --git a/paddle/operators/nccl_op.cc b/paddle/operators/nccl_op.cc new file mode 100644 index 0000000000000000000000000000000000000000..d39cb2fcf9cc205edf86f8ab1d5e04b5672e00f6 --- /dev/null +++ b/paddle/operators/nccl_op.cc @@ -0,0 +1,206 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. + 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. */ + +#include "paddle/framework/op_registry.h" +#include "paddle/operators/nccl/nccl_gpu_common.h" + +namespace paddle { +namespace operators { + +// NCCLinitOp +class NCCLInitOp : public framework::OperatorBase { + public: + NCCLInitOp(const std::string &type, const framework::VariableNameMap &inputs, + const framework::VariableNameMap &outputs, + const framework::AttributeMap &attrs) + : OperatorBase(type, inputs, outputs, attrs) {} + + void Run(const framework::Scope &scope, + const platform::DeviceContext &dev_ctx) const override { + const auto &name = Output("Communicator"); + PADDLE_ENFORCE_NOT_NULL(scope.FindVar(name), + "Can not find variable '%s' in the scope.", name); + std::vector gpus = Attr>("gpus"); + PADDLE_ENFORCE(!gpus.empty(), "Attr(gpus) should not be empty."); + + if (scope.FindVar(name) == nullptr) { + PADDLE_THROW("Output(Communicator) is needed for ncclInit operator."); + } + + platform::Communicator *comm = + scope.FindVar(name)->GetMutable(); + comm->InitAll(gpus); + } +}; + +class NCCLInitOpMaker : public framework::OpProtoAndCheckerMaker { + public: + NCCLInitOpMaker(framework::OpProto *proto, + framework::OpAttrChecker *op_checker) + : OpProtoAndCheckerMaker(proto, op_checker) { + AddOutput("Communicator", + "Create Communicator for communicating between gpus"); + AddAttr>("gpus", "gpu id lists"); + AddAttr("data_type", "output data type") + .SetDefault(framework::DataType::FP32); + AddComment(R"DOC( + create communicator. + )DOC"); + } +}; + +// AllReduceOp +class NCCLAllReduceOp : public framework::OperatorWithKernel { + public: + using framework::OperatorWithKernel::OperatorWithKernel; + + protected: + void InferShape(framework::InferShapeContext *ctx) const override { + PADDLE_ENFORCE(ctx->HasInput("X"), + " Input(X) of AllReduce op input should not be NULL"); + PADDLE_ENFORCE( + ctx->HasInput("Communicator"), + " Input(Communicator) of AllReduce op input should not be NULL"); + PADDLE_ENFORCE(ctx->HasOutput("Out"), + " Input(X) of AllReduce op input should not be NULL"); + + auto x_dims = ctx->GetInputsDim("X"); + + std::string reduction = ctx->Attrs().Get("reduction"); + PADDLE_ENFORCE((reduction == "ncclSum" || reduction == "ncclProd" || + reduction == "ncclMin" || reduction == "ncclMax"), + "invalid reduction."); + + ctx->SetOutputsDim("Out", x_dims); + ctx->ShareLoD("X", /*->*/ "Out"); + } +}; + +// ReduceOp +class NCCLReduceOp : public framework::OperatorWithKernel { + public: + using framework::OperatorWithKernel::OperatorWithKernel; + + protected: + void InferShape(framework::InferShapeContext *ctx) const override { + PADDLE_ENFORCE(ctx->HasInput("X"), + " Input(X) of Reduce op input should not be NULL"); + PADDLE_ENFORCE( + ctx->HasInput("Communicator"), + " Input(Communicator) of Reduce op input should not be NULL"); + PADDLE_ENFORCE(ctx->HasOutput("Out"), + " Input(X) of Reduce op input should not be NULL"); + + std::string reduction = ctx->Attrs().Get("reduction"); + PADDLE_ENFORCE((reduction == "ncclSum" || reduction == "ncclProd" || + reduction == "ncclMin" || reduction == "ncclMax"), + "invalid reduction."); + + auto x_dims = ctx->GetInputsDim("X"); + ctx->SetOutputsDim("Out", x_dims); + ctx->ShareLoD("X", /*->*/ "Out"); + } +}; + +// BcastOp +class NCCLBcastOp : public framework::OperatorWithKernel { + public: + using framework::OperatorWithKernel::OperatorWithKernel; + + protected: + void InferShape(framework::InferShapeContext *ctx) const override { + PADDLE_ENFORCE(ctx->HasInput("X"), + " Input(X) of Bcast op input should not be NULL"); + PADDLE_ENFORCE(ctx->HasInput("Communicator"), + " Input(Communicator) of Bcast op input should not be NULL"); + PADDLE_ENFORCE(ctx->HasOutput("Out"), + " Output(Out) of Bcast op output should not be NULL"); + + int root = ctx->Attrs().Get("root"); + PADDLE_ENFORCE(root != platform::kInvalidGPUId, "Bcast root must be set."); + + auto x_dims = ctx->GetInputsDim("X"); + ctx->SetOutputsDim("Out", x_dims); + ctx->ShareLoD("X", /*->*/ "Out"); + } +}; + +// AllreduceOp +class NCCLAllReduceOpMaker : public framework::OpProtoAndCheckerMaker { + public: + NCCLAllReduceOpMaker(framework::OpProto *proto, + framework::OpAttrChecker *op_checker) + : OpProtoAndCheckerMaker(proto, op_checker) { + AddInput("X", "The input of AllReduce op"); + AddInput("Communicator", "Communicator for communicating between gpus"); + AddOutput("Out", "The output of AllReduce op"); + AddAttr("reduction", + "{'ncclMin', 'ncclMax', 'ncclProd', 'ncclSum'}.") + .SetDefault("ncclSum"); + AddComment(R"DOC( + AllReduce the input tensors. + )DOC"); + } +}; + +// ReduceOp +class NCCLReduceOpMaker : public framework::OpProtoAndCheckerMaker { + public: + NCCLReduceOpMaker(framework::OpProto *proto, + framework::OpAttrChecker *op_checker) + : OpProtoAndCheckerMaker(proto, op_checker) { + AddInput("X", "The input of Reduce op"); + AddInput("Communicator", "Communicator for communicating between gpus"); + AddOutput("Out", "The output of Reduce op"); + AddAttr("reduction", + "{'ncclMin', 'ncclMax', 'ncclProd', 'ncclSum'}.") + .SetDefault("ncclSum"); + AddAttr("root", + "root gpu of the parameter. if not " + "set(platform::kInvalidGPUId). hashed by name.") + .SetDefault(platform::kInvalidGPUId); + AddComment(R"DOC( + Reduce the tensors)DOC"); + } +}; + +// BcastOp +class NCCLBcastOpMaker : public framework::OpProtoAndCheckerMaker { + public: + NCCLBcastOpMaker(framework::OpProto *proto, + framework::OpAttrChecker *op_checker) + : OpProtoAndCheckerMaker(proto, op_checker) { + AddInput("X", "The input of BcastSend op"); + AddInput("Communicator", "Communicator for communicating between gpus"); + AddOutput("Out", "The output of Bcast"); + AddAttr("root", + "root gpu of the parameter. if not " + "set(platform::kInvalidGPUId). hashed by name.") + .SetDefault(platform::kInvalidGPUId); + AddComment(R"DOC( + Bcast the tensors. + )DOC"); + } +}; + +} // namespace operators +} // namespace paddle + +namespace ops = paddle::operators; +REGISTER_OPERATOR(ncclInit, ops::NCCLInitOp, + paddle::framework::EmptyGradOpMaker, ops::NCCLInitOpMaker); + +REGISTER_OP_WITHOUT_GRADIENT(ncclAllReduce, ops::NCCLAllReduceOp, + ops::NCCLAllReduceOpMaker); +REGISTER_OP_WITHOUT_GRADIENT(ncclBcast, ops::NCCLBcastOp, + ops::NCCLBcastOpMaker); +REGISTER_OP_WITHOUT_GRADIENT(ncclReduce, ops::NCCLReduceOp, + ops::NCCLReduceOpMaker); diff --git a/paddle/operators/nccl_op.cu b/paddle/operators/nccl_op.cu new file mode 100644 index 0000000000000000000000000000000000000000..86dee8ee8e1c1a1041d6bc9fa515d669a9c4e466 --- /dev/null +++ b/paddle/operators/nccl_op.cu @@ -0,0 +1,211 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. +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/licenseshashernless 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. */ + +#include + +#include "paddle/framework/lod_tensor.h" +#include "paddle/framework/op_registry.h" +#include "paddle/operators/nccl/nccl_gpu_common.h" + +namespace paddle { +namespace operators { + +using framework::Tensor; +using platform::Communicator; +using framework::LoDTensor; + +template +class NCCLTypeWrapper; + +template <> +class NCCLTypeWrapper { + public: + static const ncclDataType_t type = ncclFloat; +}; + +template <> +class NCCLTypeWrapper { + public: + static const ncclDataType_t type = ncclDouble; +}; + +template +class NCCLAllReduceKernel : public framework::OpKernel { + public: + void Compute(const framework::ExecutionContext& ctx) const override { + PADDLE_ENFORCE(platform::is_gpu_place(ctx.GetPlace()), + "This kernel only runs on GPU device."); + + auto ins = ctx.MultiInput("X"); + auto outs = ctx.MultiOutput("Out"); + + std::string reduction = ctx.Attr("reduction"); + ncclRedOp_t reduction_op_ = ncclSum; + + if (reduction == "ncclMin") { + reduction_op_ = ncclMin; + } else if (reduction == "ncclMax") { + reduction_op_ = ncclMax; + } else if (reduction == "ncclSum") { + reduction_op_ = ncclSum; + } else if (reduction == "ncclProd") { + reduction_op_ = ncclProd; + } else { + PADDLE_THROW("Invalid reduction. default ncclSum."); + } + + auto* comm = ctx.Input("Communicator"); + + auto stream = reinterpret_cast( + ctx.device_context()) + .stream(); + + // device id + int gpu_id = boost::get(ctx.GetPlace()).GetDeviceId(); + int idx = comm->GetCommId(gpu_id); + + for (size_t i = 0; i < ins.size(); ++i) { + VLOG(1) << "gpu : " + << " invoke allreduce. send " << ins[i]->numel() << " recv " + << outs[i]->numel(); + + PADDLE_ENFORCE(platform::dynload::ncclAllReduce( + ins[i]->data(), outs[i]->mutable_data(ctx.GetPlace()), + outs[i]->numel(), NCCLTypeWrapper::type, reduction_op_, + comm->comms_[idx], stream)); + PADDLE_ENFORCE(cudaStreamSynchronize(stream)); + + VLOG(1) << "gpu : " + << " finished allreduce. send " << ins[i]->numel() << " recv " + << outs[i]->numel(); + } + } +}; + +template +class NCCLReduceKernel : public framework::OpKernel { + public: + void Compute(const framework::ExecutionContext& ctx) const override { + PADDLE_ENFORCE(platform::is_gpu_place(ctx.GetPlace()), + "This kernel only runs on GPU device."); + + auto ins = ctx.MultiInput("X"); // x0, x1, x2 + auto outs = ctx.MultiOutput("Out"); + + std::string reduction = ctx.Attr("reduction"); + ncclRedOp_t reduction_op_ = ncclSum; + + if (reduction == "ncclMin") { + reduction_op_ = ncclMin; + } else if (reduction == "ncclMax") { + reduction_op_ = ncclMax; + } else if (reduction == "ncclSum") { + reduction_op_ = ncclSum; + } else if (reduction == "ncclProd") { + reduction_op_ = ncclProd; + } else { + PADDLE_THROW("Invalid reduction. default ncclSum."); + } + + int root = ctx.Attr("root"); + auto* comm = ctx.Input("Communicator"); + + auto stream = reinterpret_cast( + ctx.device_context()) + .stream(); + // device id + int gpu_id = boost::get(ctx.GetPlace()).GetDeviceId(); + int idx = comm->GetCommId(gpu_id); + + auto ins_names = ctx.Inputs("X"); + std::hash hasher; + for (size_t i = 0; i < ins.size(); ++i) { + if (root == platform::kInvalidGPUId) { + root = hasher(ins_names[i]) % comm->comms_.size(); + } + T* recvbuffer = nullptr; + if (root == gpu_id) { + recvbuffer = outs[i]->mutable_data(ctx.GetPlace()); + } + + VLOG(1) << "gpu : " << gpu_id << " invoke reduce. send " + << ins[i]->numel() << " recv " << outs[i]->numel(); + + PADDLE_ENFORCE(platform::dynload::ncclReduce( + ins[i]->data(), recvbuffer, ins[i]->numel(), + NCCLTypeWrapper::type, reduction_op_, root, comm->comms_[idx], + stream)); + PADDLE_ENFORCE(cudaStreamSynchronize(stream)); + + VLOG(1) << "gpu : " << gpu_id << " finished reduce. send " + << ins[i]->numel() << " recv " << outs[i]->numel(); + } + } +}; + +template +class NCCLBcastKernel : public framework::OpKernel { + public: + void Compute(const framework::ExecutionContext& ctx) const override { + PADDLE_ENFORCE(platform::is_gpu_place(ctx.GetPlace()), + "This kernel only runs on GPU device."); + + int root = ctx.Attr("root"); + + auto* comm = ctx.Input("Communicator"); + + auto stream = reinterpret_cast( + ctx.device_context()) + .stream(); + // device id + int gpu_id = boost::get(ctx.GetPlace()).GetDeviceId(); + int idx = comm->GetCommId(gpu_id); + + if (idx == root) { + auto ins = ctx.MultiInput("X"); + for (size_t i = 0; i < ins.size(); ++i) { + VLOG(1) << "gpu : " << gpu_id << " invoke Bcast. send " + << ins[i]->numel(); + + VLOG(1) << " before ncclBcast"; + PADDLE_ENFORCE(platform::dynload::ncclBcast( + (void*)ins[i]->data(), ins[i]->numel(), NCCLTypeWrapper::type, + root, comm->comms_[idx], stream)); + VLOG(1) << " after ncclBcast"; + PADDLE_ENFORCE(cudaStreamSynchronize(stream)); + + VLOG(1) << "gpu : " << gpu_id << " finished Bcast."; + } + } else { + auto outs = ctx.MultiOutput("Out"); + for (size_t i = 0; i < outs.size(); ++i) { + VLOG(1) << "gpu : " << gpu_id << " invoke Bcast. recv buffer " + << framework::product(outs[i]->dims()); + + PADDLE_ENFORCE(platform::dynload::ncclBcast( + outs[i]->mutable_data(ctx.GetPlace()), outs[i]->numel(), + NCCLTypeWrapper::type, root, comm->comms_[idx], stream)); + PADDLE_ENFORCE(cudaStreamSynchronize(stream)); + + VLOG(1) << "gpu : " << gpu_id << " finished Bcast. recv " + << outs[i]->numel(); + } + } + } +}; + +} // namespace operators +} // namespace paddle + +namespace ops = paddle::operators; +REGISTER_OP_GPU_KERNEL(ncclAllReduce, ops::NCCLAllReduceKernel); +REGISTER_OP_GPU_KERNEL(ncclBcast, ops::NCCLBcastKernel); +REGISTER_OP_GPU_KERNEL(ncclReduce, ops::NCCLReduceKernel); diff --git a/paddle/operators/nccl_op_test.cu b/paddle/operators/nccl_op_test.cu new file mode 100644 index 0000000000000000000000000000000000000000..80c50a28a9e5d560fc693c518b9e62091ddc5724 --- /dev/null +++ b/paddle/operators/nccl_op_test.cu @@ -0,0 +1,307 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. + + 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. */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "paddle/framework/block_desc.h" +#include "paddle/framework/op_desc.h" +#include "paddle/framework/op_registry.h" +#include "paddle/framework/program_desc.h" +#include "paddle/framework/var_desc.h" +#include "paddle/operators/math/math_function.h" +#include "paddle/operators/nccl/nccl_gpu_common.h" +#include "paddle/platform/device_context.h" +#include "paddle/platform/enforce.h" +#include "paddle/platform/gpu_info.h" +#include "paddle/platform/place.h" + +USE_NO_KERNEL_OP(ncclInit); +USE_GPU_ONLY_OP(ncclAllReduce); +USE_GPU_ONLY_OP(ncclReduce); +USE_GPU_ONLY_OP(ncclBcast); + +namespace f = paddle::framework; +namespace p = paddle::platform; + +static std::vector gpu_list; + +// test data amount +const f::DDim kDims = {100, 100}; + +// nccl op common tester, init communicator. +class NCCLTester : public ::testing::Test { + public: + virtual void SetUp() override { + cpu_ctx = new p::CPUDeviceContext(p::CPUPlace()); + for (size_t i = 0; i < gpu_list.size(); ++i) { + p::GPUPlace place(i); + dev_ctxs.emplace_back(new p::CUDADeviceContext(place)); + } + + NCCLInitOp(); + } + + virtual void TearDown() override { + for (auto &device_context : dev_ctxs) { + delete device_context; + } + } + + void NCCLInitOp() { + std::unique_ptr op1(new f::OpDescBind); + + op1->SetType("ncclInit"); + op1->SetOutput("Communicator", {"comm"}); + op1->SetAttr("gpus", {gpu_list}); + + auto *var = g_scope.Var("comm"); + var->GetMutable(); + + auto op = f::OpRegistry::CreateOp(*op1); + VLOG(1) << "invoke NCCLInitOp."; + op->Run(g_scope, *cpu_ctx); + VLOG(1) << "NCCLInitOp finished."; + } + + template + void PerThreadProgram(int gpu_id, const f::OpDescBind &op_desc, + f::Scope *scope) { + std::unique_lock lk(mu); + const f::OpDescBind *op1 = &op_desc; + + p::GPUPlace place(gpu_id); + auto &ctx = dev_ctxs.at(gpu_id); + + auto *send_tensor = scope->Var("st")->GetMutable(); + auto *recv_tensor = scope->Var("rt")->GetMutable(); + + if (!send_tensor->numel()) { + send_tensor->Resize(kDims); + send_tensor->mutable_data(kDims, place); + + std::vector send_vector(f::product(kDims), gpu_id); + send_tensor->CopyFromVector(send_vector, *ctx); + ctx->Wait(); + VLOG(1) << "Send Tensor filled with elements " << send_tensor->numel(); + } + + lk.unlock(); + + PADDLE_ENFORCE(send_tensor->numel() == f::product(kDims), + "Tensor numel not match!"); + + auto op = f::OpRegistry::CreateOp(*op1); + + VLOG(1) << "Device : " << gpu_id << " invoke " << op_desc.Type(); + VLOG(1) << " send_tensor : " << send_tensor->numel() + << " recv_tensor : " << recv_tensor->numel(); + op->Run(*scope, *ctx); + VLOG(1) << "Device : " << gpu_id << " finished " << op_desc.Type(); + } + + public: + std::vector dev_ctxs; + p::DeviceContext *cpu_ctx; + f::Scope g_scope; + std::mutex mu; +}; + +// ncclInitOp with desc +TEST(NCCL, ncclInitOp) { + std::unique_ptr op_desc(new f::OpDescBind); + + op_desc->SetType("ncclInit"); + op_desc->SetOutput("Communicator", {"x1"}); + op_desc->SetAttr("gpus", {gpu_list}); + + f::Scope g_scope; + std::unique_ptr ctx(new p::CPUDeviceContext(p::CPUPlace())); + + auto *var = g_scope.Var("x1"); + var->GetMutable(); + + auto op = f::OpRegistry::CreateOp(*op_desc); + VLOG(1) << "invoke NCCLInitOp."; + op->Run(g_scope, *ctx.get()); + VLOG(1) << "NCCLInitOp finished."; +} + +// ncclAllReduceOp with desc +TEST_F(NCCLTester, ncclAllReduceOp) { + std::unique_ptr op2(new f::OpDescBind); + op2->SetType("ncclAllReduce"); + op2->SetInput("X", {"st"}); + op2->SetInput("Communicator", {"comm"}); + op2->SetOutput("Out", {"rt"}); + + std::vector dev_scopes; + + std::vector ths; + + for (size_t i = 0; i < gpu_list.size(); ++i) { + dev_scopes.emplace_back(&g_scope.NewScope()); + std::thread th(&NCCLTester::PerThreadProgram, this, gpu_list[i], + *op2.get(), dev_scopes[i]); + ths.emplace_back(std::move(th)); + } + + for (size_t i = 0; i < gpu_list.size(); ++i) { + ths[i].join(); + } + + // check results + float result = std::accumulate(gpu_list.begin(), gpu_list.end(), 0); + + for (size_t i = 0; i < dev_scopes.size(); ++i) { + p::CPUPlace cpu_place; + p::GPUPlace gpu_place(gpu_list[i]); + + auto &recv_tensor = dev_scopes[i]->FindVar("rt")->Get(); + auto *rt = recv_tensor.data(); + auto *result_tensor = dev_scopes[i]->Var("ct")->GetMutable(); + result_tensor->Resize(kDims); + auto *ct = result_tensor->mutable_data(cpu_place); + + paddle::memory::Copy( + cpu_place, ct, p::GPUPlace(gpu_list[i]), rt, + recv_tensor.numel() * sizeof(float), + static_cast(dev_ctxs[i])->stream()); + + for (size_t j = 0; j < f::product(kDims); ++j) { + ASSERT_NEAR(ct[j], result, 1e-5); + } + } +} + +// ncclReduceOp with desc +TEST_F(NCCLTester, ncclReduceOp) { + std::unique_ptr op2(new f::OpDescBind); + const int kRoot = 0; + op2->SetType("ncclReduce"); + op2->SetInput("X", {"st"}); + op2->SetInput("Communicator", {"comm"}); + op2->SetOutput("Out", {"rt"}); + op2->SetAttr("root", kRoot); + + std::vector dev_scopes; + + std::vector ths; + + for (size_t i = 0; i < gpu_list.size(); ++i) { + dev_scopes.emplace_back(&g_scope.NewScope()); + std::thread th(&NCCLTester::PerThreadProgram, this, gpu_list[i], + *op2.get(), dev_scopes[i]); + ths.emplace_back(std::move(th)); + } + + for (size_t i = 0; i < gpu_list.size(); ++i) { + ths[i].join(); + } + + // check results on + float result = std::accumulate(gpu_list.begin(), gpu_list.end(), 0); + + p::CPUPlace cpu_place; + p::GPUPlace gpu_place(gpu_list[kRoot]); + + auto &recv_tensor = dev_scopes[kRoot]->FindVar("rt")->Get(); + auto *rt = recv_tensor.data(); + auto *result_tensor = + dev_scopes[kRoot]->Var("ct")->GetMutable(); + result_tensor->Resize(kDims); + auto *ct = result_tensor->mutable_data(cpu_place); + + paddle::memory::Copy( + cpu_place, ct, p::GPUPlace(gpu_list[kRoot]), rt, + recv_tensor.numel() * sizeof(float), + static_cast(dev_ctxs[kRoot])->stream()); + + for (int j = 0; j < f::product(kDims); ++j) { + ASSERT_NEAR(ct[j], result, 1e-5); + } +} + +// ncclBcastOp with desc +TEST_F(NCCLTester, ncclBcastOp) { + std::unique_ptr op2(new f::OpDescBind); + const int kRoot = 5; + op2->SetType("ncclBcast"); + op2->SetInput("X", {"st"}); + op2->SetInput("Communicator", {"comm"}); + op2->SetOutput("Out", {"rt"}); + op2->SetAttr("root", kRoot); + + std::vector dev_scopes; + + std::vector ths; + + for (size_t i = 0; i < gpu_list.size(); ++i) { + dev_scopes.emplace_back(&g_scope.NewScope()); + std::thread th(&NCCLTester::PerThreadProgram, this, gpu_list[i], + *op2.get(), dev_scopes[i]); + ths.emplace_back(std::move(th)); + } + + for (size_t i = 0; i < gpu_list.size(); ++i) { + ths[i].join(); + } + + const int idx = 1; + // check results on + float result = kRoot; + + p::CPUPlace cpu_place; + p::GPUPlace gpu_place(gpu_list[idx]); + + auto &recv_tensor = dev_scopes[idx]->FindVar("rt")->Get(); + auto *rt = recv_tensor.data(); + auto *result_tensor = dev_scopes[idx]->Var("ct")->GetMutable(); + result_tensor->Resize(kDims); + auto *ct = result_tensor->mutable_data(cpu_place); + + paddle::memory::Copy( + cpu_place, ct, p::GPUPlace(gpu_list[idx]), rt, + recv_tensor.numel() * sizeof(float), + static_cast(dev_ctxs[idx])->stream()); + + for (size_t j = 0; j < f::product(kDims); ++j) { + ASSERT_NEAR(ct[j], result, 1e-5); + } +} + +int main(int argc, char **argv) { + const int dev_count = p::GetCUDADeviceCount(); + if (dev_count <= 1) { + LOG(WARNING) + << "Cannot test multi-gpu nccl, because the CUDA device count is " + << dev_count; + return 0; + } + + for (int i = 0; i < dev_count; ++i) { + gpu_list.emplace_back(i); + } + testing::InitGoogleTest(&argc, argv); + + // device context should be release before scope. + // otherwise driver will down. + return RUN_ALL_TESTS(); +} diff --git a/paddle/operators/pool_cudnn_op.cu b/paddle/operators/pool_cudnn_op.cu index bc29be18e76fde19c10c32e0299c395a150d8c40..8d0741dccc1fdae069af55da49f44378e2c4ddf8 100644 --- a/paddle/operators/pool_cudnn_op.cu +++ b/paddle/operators/pool_cudnn_op.cu @@ -43,6 +43,7 @@ class PoolCudnnOpKernel : public framework::OpKernel { std::vector paddings = ctx.Attr>("paddings"); if (ctx.Attr("globalPooling")) { for (size_t i = 0; i < ksize.size(); ++i) { + paddings[i] = 0; ksize[i] = static_cast(input->dims()[i + 2]); } } @@ -97,8 +98,10 @@ class PoolCudnnGradOpKernel : public framework::OpKernel { std::vector paddings = ctx.Attr>("paddings"); if (ctx.Attr("globalPooling")) { - for (size_t i = 0; i < ksize.size(); ++i) + for (size_t i = 0; i < ksize.size(); ++i) { + paddings[i] = 0; ksize[i] = static_cast(input->dims()[i + 2]); + } } const T *input_data = input->data(); diff --git a/paddle/operators/pool_op.cc b/paddle/operators/pool_op.cc index c4ab29e4d5f7c02d97a2185a58fdcd48de822d2d..4d75c11bc8130343e95f75e687529303179caa93 100644 --- a/paddle/operators/pool_op.cc +++ b/paddle/operators/pool_op.cc @@ -39,8 +39,10 @@ void PoolOp::InferShape(framework::InferShapeContext *ctx) const { if (ctx->Attrs().Get("globalPooling")) { ksize.resize(static_cast(in_x_dims.size()) - 2); - for (size_t i = 0; i < ksize.size(); ++i) + for (size_t i = 0; i < ksize.size(); ++i) { + paddings[i] = 0; ksize[i] = static_cast(in_x_dims[i + 2]); + } } PADDLE_ENFORCE(in_x_dims.size() - ksize.size() == 2U, @@ -84,15 +86,16 @@ Pool2dOpMaker::Pool2dOpMaker(framework::OpProto *proto, "(string), pooling type, can be \"max\" for max-pooling " "and \"avg\" for average-pooling.") .InEnum({"max", "avg"}); - AddAttr>( - "ksize", - "(vector ), the pooling window size(height, width) of pooling operator." - "If globalPooling = true, ksize is ignored and need not be " - "specified."); // TODO(Chengduo): Add checker. (Currently, + AddAttr>("ksize", + "(vector ), the pooling window size(height, width) " + "of pooling operator." + "If globalPooling = true, ksize and paddings will " + "be ignored."); // TODO(Chengduo): Add checker. + // (Currently, // TypedAttrChecker don't support vector type.) AddAttr("globalPooling", "(bool default: false), whether to use the global pooling." - "If globalPooling = true, ksize is ignored.") + "If globalPooling = true, ksize and paddings will be ignored.") .SetDefault(false); AddAttr>( "strides", @@ -101,7 +104,8 @@ Pool2dOpMaker::Pool2dOpMaker(framework::OpProto *proto, // TypedAttrChecker don't support vector type.) AddAttr>( "paddings", - "(vector defalut:{0,0}), paddings(height, width) of pooling operator.") + "(vector defalut:{0,0}), paddings(height, width) of pooling operator." + "If globalPooling = true, paddings and ksize will be ignored.") .SetDefault({0, 0}); // TODO(Chengduo): Add checker. (Currently, // TypedAttrChecker don't support vector type.) @@ -145,25 +149,28 @@ Pool3dOpMaker::Pool3dOpMaker(framework::OpProto *proto, "(string), pooling type, can be \"max\" for max-pooling " "and \"avg\" for average-pooling.") .InEnum({"max", "avg"}); - AddAttr>( - "ksize", - "(vector ), the pooling window size(depth, height, width) of pooling " - "operator." - "If globalPooling = true, ksize is ignored and need not be " - "specified."); // TODO(Chengduo): Add checker. (Currently, - // TypedAttrChecker don't support vector type.) + AddAttr>("ksize", + "(vector ), the pooling window size(depth, height, " + "width) of pooling " + "operator." + "If globalPooling = true, ksize and paddings wille " + "be ignored."); // TODO(Chengduo): Add checker. + // (Currently, + // TypedAttrChecker don't support vector type.) AddAttr("globalPooling", "(bool default: false), whether to use the global pooling." - "If globalPooling = true, ksize is ignored.") + "If globalPooling = true, ksize and paddings wille be ignored.") .SetDefault(false); AddAttr>("strides", "(vector, default:{1,1,1}), strides(depth, height, " "width) of pooling operator.") .SetDefault({1, 1, 1}); // TODO(Chengduo): Add checker. (Currently, // TypedAttrChecker don't support vector type.) - AddAttr>("paddings", - "(vector defalut:{0,0,0}), paddings(depth, height, " - "width) of pooling operator.") + AddAttr>( + "paddings", + "(vector defalut:{0,0,0}), paddings(depth, height, " + "width) of pooling operator." + "If globalPooling = true, ksize and paddings wille be ignored.") .SetDefault({0, 0, 0}); // TODO(Chengduo): Add checker. (Currently, // TypedAttrChecker don't support vector type.) diff --git a/paddle/operators/pool_op.h b/paddle/operators/pool_op.h index ba8edc9cf60bcf90204ed11fa4fe1d408ad82d40..d9d445f6a6257b0c8a1959c64c9a878539e10cd4 100644 --- a/paddle/operators/pool_op.h +++ b/paddle/operators/pool_op.h @@ -63,6 +63,7 @@ class PoolKernel : public framework::OpKernel { std::vector paddings = context.Attr>("paddings"); if (context.Attr("globalPooling")) { for (size_t i = 0; i < ksize.size(); ++i) { + paddings[i] = 0; ksize[i] = static_cast(in_x->dims()[i + 2]); } } @@ -103,6 +104,7 @@ class PoolKernel : public framework::OpKernel { paddings, pool_process); } } break; + default: { PADDLE_THROW("Pool op only supports 2D and 3D input."); } } } }; @@ -123,8 +125,10 @@ class PoolGradKernel : public framework::OpKernel { std::vector paddings = context.Attr>("paddings"); if (context.Attr("globalPooling")) { - for (size_t i = 0; i < ksize.size(); ++i) + for (size_t i = 0; i < ksize.size(); ++i) { + paddings[i] = 0; ksize[i] = static_cast(in_x->dims()[i + 2]); + } } if (in_x_grad) { @@ -164,6 +168,7 @@ class PoolGradKernel : public framework::OpKernel { *out_grad, ksize, strides, paddings, pool_process); } } break; + default: { PADDLE_THROW("Pool op only supports 2D and 3D input."); } } } } diff --git a/paddle/operators/pool_with_index_op.cc b/paddle/operators/pool_with_index_op.cc index ea21845751bee523fbbb85f7fdbeea7bcc586997..95e896e7cc33b1aebe78d1af8746a25318048041 100644 --- a/paddle/operators/pool_with_index_op.cc +++ b/paddle/operators/pool_with_index_op.cc @@ -46,8 +46,10 @@ class MaxPoolWithIndexOp : public framework::OperatorWithKernel { if (ctx->Attrs().Get("globalPooling")) { ksize.resize(static_cast(in_x_dims.size()) - 2); - for (size_t i = 0; i < ksize.size(); ++i) + for (size_t i = 0; i < ksize.size(); ++i) { + paddings[i] = 0; ksize[i] = static_cast(in_x_dims[i + 2]); + } } PADDLE_ENFORCE(in_x_dims.size() - ksize.size() == 2U, @@ -87,31 +89,33 @@ class MaxPool2dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker { : OpProtoAndCheckerMaker(proto, op_checker) { AddInput( "X", - "(Tensor) The input tensor of pooling operator. " + "(Tensor), the input tensor of pooling operator. " "The format of input tensor is NCHW. Where N is batch size, C is the " "number of channels, H and W is the height and width of image."); AddOutput("Out", - "(Tensor) The output tensor of pooling operator." + "(Tensor), the output tensor of pooling operator." "The format of output tensor is also NCHW." "Where N is batch size, C is " "the number of channels, H and W is the height and " "width of image."); AddOutput("Mask", - "(Tensor) The Mask tensor of pooling operator." + "(Tensor), the Mask tensor of pooling operator." "The format of output tensor is also NCHW." "Where N is batch size, C is the number of channels, H and W " "is the height and width of image." "The value in it is the index in current feature map"); - AddAttr>( - "ksize", - "(vector ), the pooling window size(height, width) of pooling operator." - "If globalPooling = true, ksize is ignored and need not be " - "specified."); // TODO(Chengduo): Add checker. (Currently, + AddAttr>("ksize", + "(vector ), the pooling window size(height, " + "width) of pooling operator." + "If globalPooling = true, ksize and paddings " + "will be ignored."); // TODO(Chengduo): Add + // checker. (Currently, // TypedAttrChecker don't support vector type.) - AddAttr("globalPooling", - "(bool default: false), whether to use the global pooling." - "If globalPooling = true, ksize is ignored.") + AddAttr( + "globalPooling", + "(bool default: false), whether to use the global pooling." + "If globalPooling = true, ksize and paddings will be ignored.") .SetDefault(false); AddAttr>( "strides", @@ -120,7 +124,8 @@ class MaxPool2dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker { // TypedAttrChecker don't support vector type.) AddAttr>( "paddings", - "(vector defalut:{0,0}), paddings(height, width) of pooling operator.") + "(vector defalut:{0, 0}), paddings(height, width) of pooling operator." + "If globalPooling = true, paddings and will be ignored.") .SetDefault({0, 0}); // TODO(Chengduo): Add checker. (Currently, // TypedAttrChecker don't support vector type.) @@ -153,42 +158,46 @@ class MaxPool3dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker { : OpProtoAndCheckerMaker(proto, op_checker) { AddInput( "X", - "(Tensor) The input tensor of pooling operator. " + "(Tensor), the input tensor of pooling operator. " "The format of input tensor is NCDHW. Where N is batch size, C is " "the number of channels, D, H and W is the depth, height and width of " "image."); AddOutput("Out", - "(Tensor) The output tensor of pooling operator." + "(Tensor), the output tensor of pooling operator." "The format of output tensor is also NCDHW." "Where N is batch size, C is " "the number of channels, D, H and W is the depth, height and " "width of image."); AddOutput("Mask", - "(Tensor) The Mask tensor of pooling operator." + "(Tensor), the Mask tensor of pooling operator." "The format of output tensor is also NCDHW." "Where N is batch size, C is the number of channels, D, H and W " "is the depth, height and width of image." "The value in it is the index in current feature map"); - AddAttr>( - "ksize", - "(vector ), the pooling window size(depth, height, width) of pooling " - "operator." - "If globalPooling = true, ksize is ignored and need not be " - "specified."); // TODO(Chengduo): Add checker. (Currently, + AddAttr>("ksize", + "(vector), the pooling window size(depth, " + "height, width) of pooling " + "operator." + "If globalPooling = true, ksize and paddings " + "will be ignored."); // TODO(Chengduo): Add + // checker. (Currently, // TypedAttrChecker don't support vector type.) - AddAttr("globalPooling", - "(bool default: false), whether to use the global pooling." - "If globalPooling = true, ksize is ignored.") + AddAttr( + "globalPooling", + "(bool default: false), whether to use the global pooling." + "If globalPooling = true, ksize and paddings will be ignored.") .SetDefault(false); AddAttr>("strides", "(vector, default:{1,1,1}), strides(depth, " "height, width) of pooling operator.") .SetDefault({1, 1, 1}); // TODO(Chengduo): Add checker. (Currently, // TypedAttrChecker don't support vector type.) - AddAttr>("paddings", - "(vector defalut:{0,0,0}), paddings(depth, " - "height, width) of pooling operator.") + AddAttr>( + "paddings", + "(vector defalut:{0,0,0}), paddings(depth, " + "height, width) of pooling operator." + "If globalPooling = true, paddings and ksize will be ignored.") .SetDefault({0, 0, 0}); // TODO(Chengduo): Add checker. (Currently, // TypedAttrChecker don't support vector type.) diff --git a/paddle/operators/pool_with_index_op.h b/paddle/operators/pool_with_index_op.h index 01b961ca8295f723bea7335e43ec5ab100dfc65c..48627740435b7d397c5a53491c1f89ba1b603803 100644 --- a/paddle/operators/pool_with_index_op.h +++ b/paddle/operators/pool_with_index_op.h @@ -37,6 +37,7 @@ class MaxPoolWithIndexKernel : public framework::OpKernel { std::vector paddings = context.Attr>("paddings"); if (context.Attr("globalPooling")) { for (size_t i = 0; i < ksize.size(); ++i) { + paddings[i] = 0; ksize[i] = static_cast(in_x->dims()[i + 2]); } } @@ -54,6 +55,7 @@ class MaxPoolWithIndexKernel : public framework::OpKernel { pool3d_forward(context.device_context(), *in_x, *out, *mask, ksize, strides, paddings); } break; + default: { PADDLE_THROW("Pool op only supports 2D and 3D input."); } } } }; @@ -72,6 +74,7 @@ class MaxPoolWithIndexGradKernel : public framework::OpKernel { std::vector paddings = context.Attr>("paddings"); if (context.Attr("globalPooling")) { for (size_t i = 0; i < ksize.size(); ++i) { + paddings[i] = 0; ksize[i] = static_cast(in_x_grad->dims()[i + 2]); } } @@ -95,6 +98,7 @@ class MaxPoolWithIndexGradKernel : public framework::OpKernel { pool3d_backward(context.device_context(), *in_x_grad, *out_grad, *mask, ksize, strides, paddings); } break; + default: { PADDLE_THROW("Pool op only supports 2D and 3D input."); } } } } diff --git a/paddle/operators/reshape_op.cc b/paddle/operators/reshape_op.cc index a8eb8d45eec214842ee756a260127b9d0aacb0f4..eda8226480a66ae1a631391e9335db04604039c5 100644 --- a/paddle/operators/reshape_op.cc +++ b/paddle/operators/reshape_op.cc @@ -34,13 +34,19 @@ class ReshapeOp : public framework::OperatorWithKernel { auto shape = ctx->Attrs().Get>("shape"); PADDLE_ENFORCE(shape.size() > 0, "Attr(shape) shouldn't be empty."); - for (auto dim : shape) { - PADDLE_ENFORCE(dim > 0, "Each dimension of shape must be positive."); + auto x_dims = ctx->GetInputDim("X"); + // TODO(qiao) change batch_size + for (int i = 1; i < shape.size(); ++i) { + PADDLE_ENFORCE(shape[i] > 0, + "Each dimension of shape " + "must be positiv except the first."); + } + if (shape[0] < 0) { + shape[0] = x_dims[0]; } // capacity check int64_t capacity = std::accumulate(shape.begin(), shape.end(), 1, std::multiplies()); - auto x_dims = ctx->GetInputDim("X"); int64_t in_size = framework::product(x_dims); PADDLE_ENFORCE_EQ(capacity, in_size, "The size of Input(X) mismatches with Attr(shape)."); diff --git a/paddle/operators/reshape_op.h b/paddle/operators/reshape_op.h index c89cdf8cab9f209667c5e09b521b8f6e30f202fd..beb951713ae2a9fd83fe7c1a5e97ee8c642158a8 100644 --- a/paddle/operators/reshape_op.h +++ b/paddle/operators/reshape_op.h @@ -26,13 +26,8 @@ class ReshapeKernel : public framework::OpKernel { void Compute(const framework::ExecutionContext& ctx) const { auto* out = ctx.Output("Out"); auto* in = ctx.Input("X"); + auto out_dims = out->dims(); out->mutable_data(ctx.GetPlace()); - - auto shape = ctx.Attr>("shape"); - std::vector shape_int64(shape.size(), 0); - std::transform(shape.begin(), shape.end(), shape_int64.begin(), - [](int a) { return static_cast(a); }); - auto out_dims = framework::make_ddim(shape_int64); out->CopyFrom(*in, ctx.GetPlace(), ctx.device_context()); out->Resize(out_dims); } diff --git a/paddle/operators/seq_expand_op.cc b/paddle/operators/seq_expand_op.cc new file mode 100644 index 0000000000000000000000000000000000000000..08fda9b44564249634f0d1a570e8b2458f88fd41 --- /dev/null +++ b/paddle/operators/seq_expand_op.cc @@ -0,0 +1,153 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. + + 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. */ + +#include "paddle/operators/seq_expand_op.h" + +namespace paddle { +namespace operators { + +using framework::Tensor; + +class SeqExpandOp : public framework::OperatorWithKernel { + public: + using framework::OperatorWithKernel::OperatorWithKernel; + + protected: + void InferShape(framework::InferShapeContext* ctx) const override { + PADDLE_ENFORCE(ctx->HasInput("X")); + PADDLE_ENFORCE(ctx->HasOutput("Out")); + PADDLE_ENFORCE(ctx->HasInput("Y")); + framework::DDim out_dim; + out_dim = ctx->GetInputDim("Y"); + ctx->ShareLoD("Y", "Out"); + ctx->SetOutputDim("Out", out_dim); + } +}; + +class SeqExpandOpMaker : public framework::OpProtoAndCheckerMaker { + public: + SeqExpandOpMaker(framework::OpProto* proto, + framework::OpAttrChecker* op_checker) + : OpProtoAndCheckerMaker(proto, op_checker) { + AddInput("X", + "(Tensor or LoDTensor) The input(X) of this operator can be a " + "LoDTensor or a base Tensor."); + AddInput("Y", + "(LoDTensor)The reference input(Y) of seq_expand op." + "It must be a LoDTensor with k-level(k>0)." + "The input(X) will be expanded according to LOD of input(Y)." + "The element numbers of last level in input(Y) " + "must be equal to dims[0] of input(X)."); + AddOutput("Out", + "(LodTensor)The output of seq_expand op." + "The lod of output will be as same as input(Y)'s lod."); + AddComment(R"DOC( +Expand input(X) according to LOD of input(Y). + +Case 1: + +Given 2-level a LoDTensor input(X) + X.lod = [[0, 2, 3], + [0, 1, 3, 4]] + X.data = [a, b, c, d] + X.dims = [4, 1] +and input(Y) + Y.lod = [[0, 2, 4], + [0, 3, 6, 7, 8]] +with condition len(Y.lod[-1]) -1 == X.dims[0] +then we get 2-level LoDTensor + Out.lod = [[0, 2, 4], + [0, 3, 6, 7, 8]] + Out.data = [a, a, a, b, b, b, c, d] + Out.dims = [8, 1] + +Case 2: + +Given a 0-level LoDTensor input(X) + X.data = [a, b, c] + X.lod = NULL + X.dims = [3, 1] +and input(Y) + Y.lod = [[0, 2, 3, 6]] +with condition len(Y.lod[-1]) -1 == X.dims[0] +then we get 1-level LoDTensor + Out.lod = [[0, 2, 3, 6]] + Out.data = [a, a, b, c, c, c] + Out.dims = [6, 1] + +Case 3: + +Given a 0-level LoDTensor input(X) + X.data = [[a, b], [c, d], [e, f]] + X.lod = NULL + X.dims = [3, 2] +and input(Y) + Y.lod = [[0, 2, 3, 6]] +with condition len(Y.lod[-1]) -1 == X.dims[0] +then we get 1-level LoDTensor + Out.lod = [[0, 2, 3, 6]] + Out.data = [[a,b], [a,b] [c,d], [e, f], [e, f], [e, f]] + Out.dims = [6, 2] + +Case 4: + +Given 2-level a LoDTensor input(X) + X.lod = [[0, 2, 3], + [0, 1, 3, 4]] + X.data = [a, b, c, d] + X.dims = [4, 1] +and input(Y) + Y.lod = [[0, 2, 4], + [0, 3, 6, 6, 8]] +with condition len(Y.lod[-1]) -1 == X.dims[0] +then we get 2-level LoDTensor + Out.lod = [[0, 2, 4], + [0, 3, 6, 6, 8]] + Out.data = [a, a, a, b, b, b, d, d] + Out.dims = [8, 1] + + +)DOC"); + } +}; + +class SeqExpandOpGrad : public framework::OperatorWithKernel { + public: + using framework::OperatorWithKernel::OperatorWithKernel; + + protected: + void InferShape(framework::InferShapeContext* ctx) const override { + PADDLE_ENFORCE(ctx->HasInput("X")); + PADDLE_ENFORCE(ctx->HasInput("Out")); + PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")), + "The input(Out@GRAD) should not be null"); + auto x_dims = ctx->GetInputDim("X"); + auto x_grad_name = framework::GradVarName("X"); + if (ctx->HasOutput(x_grad_name)) { + ctx->SetOutputDim(x_grad_name, x_dims); + } + } +}; + +} // namespace operators +} // namespace paddle + +namespace ops = paddle::operators; +REGISTER_OP(seq_expand, ops::SeqExpandOp, ops::SeqExpandOpMaker, + seq_expand_grad, ops::SeqExpandOpGrad); +REGISTER_OP_CPU_KERNEL(seq_expand, + ops::SeqExpandKernel); +REGISTER_OP_CPU_KERNEL( + seq_expand_grad, + ops::SeqExpandGradKernel); diff --git a/paddle/operators/seq_expand_op.cu b/paddle/operators/seq_expand_op.cu new file mode 100644 index 0000000000000000000000000000000000000000..f1e4b82a76e628c4d9fb83bc93f3dcfd2f98ea5b --- /dev/null +++ b/paddle/operators/seq_expand_op.cu @@ -0,0 +1,23 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. + + 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. */ + +#define EIGEN_USE_GPU +#include "paddle/operators/seq_expand_op.h" + +namespace ops = paddle::operators; +REGISTER_OP_GPU_KERNEL(seq_expand, + ops::SeqExpandKernel); +REGISTER_OP_GPU_KERNEL( + seq_expand_grad, + ops::SeqExpandGradKernel); diff --git a/paddle/operators/seq_expand_op.h b/paddle/operators/seq_expand_op.h new file mode 100644 index 0000000000000000000000000000000000000000..8703105385183c1a0ee1a1b3831228f942c04dda --- /dev/null +++ b/paddle/operators/seq_expand_op.h @@ -0,0 +1,100 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. + + 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. */ + +#pragma once + +#include "paddle/framework/op_registry.h" +#include "paddle/memory/memcpy.h" +#include "unsupported/Eigen/CXX11/Tensor" + +namespace paddle { +namespace operators { + +using LoDTensor = framework::LoDTensor; + +template +class SeqExpandKernel : public framework::OpKernel { + public: + void Compute(const framework::ExecutionContext& context) const override { + auto* x = context.Input("X"); + auto* out = context.Output("Out"); + const T* x_data = x->data(); + auto x_dims = x->dims(); + auto* y = context.Input("Y"); + PADDLE_ENFORCE_EQ(x_dims[0], y->lod().back().size() - 1, + "The size of last lod level in Input(Y)" + "must be equal to dims[0] of Input(X)."); + out->set_lod(y->lod()); + auto place = context.GetEigenDevice(); + size_t element_len = framework::product(x_dims) / x_dims[0]; + T* out_data = out->mutable_data(context.GetPlace()); + auto out_starts = out->lod().back(); + + for (size_t i = 0; i < out_starts.size() - 1; i++) { + int scale = out_starts[i + 1] - out_starts[i]; + Eigen::TensorMap< + Eigen::Tensor> + x_t(x_data, 1, element_len); + Eigen::TensorMap> + out_t(out_data, scale, element_len); + Eigen::array cast({{scale, 1}}); + out_t.device(place) = x_t.broadcast(cast); + x_data += element_len; + out_data += element_len * scale; + } + } +}; + +/* + *Given Grad(Out) + * + * Grad(Out).lod = [[0, 2], + * [0, 3, 6]] + * Grad(Out).data = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] + * Then + * Grad(X).data = [(0.1 + 0.2 + 0.3), (0.4 + 0.5 + 0.6)] + * = [0.6, 1.5] + * Grad(X).lod = Input(X).lod + * + * */ +template +class SeqExpandGradKernel : public framework::OpKernel { + public: + void Compute(const framework::ExecutionContext& context) const override { + auto* d_out = context.Input(framework::GradVarName("Out")); + auto* x = context.Input("X"); + auto* out = context.Input("Out"); + auto* d_x = context.Output(framework::GradVarName("X")); + auto out_last_level = out->lod().back(); + d_x->set_lod(x->lod()); + const T* d_out_data = d_out->data(); + T* d_x_data = d_x->mutable_data(context.GetPlace()); + size_t element_len = d_out->numel() / d_out->dims()[0]; + for (size_t i = 0; i < out_last_level.size() - 1; ++i) { + size_t repeat = out_last_level[i + 1] - out_last_level[i]; + Eigen::TensorMap< + Eigen::Tensor> + d_out_t(d_out_data, static_cast(repeat), element_len); + Eigen::TensorMap> + d_x_t(d_x_data, static_cast(element_len)); + auto place = context.GetEigenDevice(); + d_x_t.device(place) = d_out_t.sum(Eigen::array({{0}})); + d_out_data += (repeat * element_len); + d_x_data += element_len; + } + } +}; + +} // namespace operators +} // namespace paddle diff --git a/paddle/operators/sequence_concat_op.cc b/paddle/operators/sequence_concat_op.cc index 1fce96cdfe20fc3ab33a3cd00e9a03833c9b94f8..46f73e3c279835bbb4bfdd7dede03a5535186b24 100644 --- a/paddle/operators/sequence_concat_op.cc +++ b/paddle/operators/sequence_concat_op.cc @@ -68,12 +68,12 @@ class SequenceConcatOpMaker : public framework::OpProtoAndCheckerMaker { "The level should be less than the level number of inputs.") .SetDefault(0); AddComment(R"DOC( - The sequence_concat operator concatenates multiple LoDTensors. - It only supports sequence (LoD Tensor with level number is 1) + The sequence_concat operator concatenates multiple LoDTensors. + It only supports sequence (LoD Tensor with level number is 1) or a nested sequence (LoD tensor with level number is 2) as its input. - Case1: If the axis is other than 0(here, axis is 1 and level is 1), - each input should have the same LoD information and the LoD + each input should have the same LoD information and the LoD information of the output keeps the same as the input. LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4) @@ -81,7 +81,7 @@ class SequenceConcatOpMaker : public framework::OpProtoAndCheckerMaker { LoD(Out) = {{0,2,4}, {0,1,2,3,4}}; Dims(Out) = (4,7,4) - Case2: - If the axis is 0(here, leve is 0), the inputs are concatenated along + If the axis is 0(here, leve is 0), the inputs are concatenated along time steps, the LoD information of the output need to re-compute. LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4) @@ -94,7 +94,7 @@ class SequenceConcatOpMaker : public framework::OpProtoAndCheckerMaker { LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4) LoD(x1) = {{0,3,5}, {0,1,3,4,5}}; Dims(x1) = (5,3,4) LoD(Out) = {{0,5,9}, {0,2,5,7,9}}; Dims(Out) = (9,3,4) - + NOTE: The levels of all the inputs should be the same. )DOC"); } diff --git a/paddle/operators/sequence_conv_op.cc b/paddle/operators/sequence_conv_op.cc index 139000c561870c3bc49e01cdcb6cf4b787e64577..bdb52265a529f560b4622ee037dcb3160ac90dec 100644 --- a/paddle/operators/sequence_conv_op.cc +++ b/paddle/operators/sequence_conv_op.cc @@ -30,19 +30,20 @@ class SequenceConvOp : public framework::OperatorWithKernel { PADDLE_ENFORCE(ctx->HasOutput("Out"), "Output(Out) of SequenceConvOp should not be null."); - int context_length = ctx->Attrs().Get("context_length"); - bool padding_trainable = ctx->Attrs().Get("padding_trainable"); - int context_start = ctx->Attrs().Get("context_start"); + int context_length = ctx->Attrs().Get("contextLength"); + int context_start = ctx->Attrs().Get("contextStart"); auto in_dims = ctx->GetInputDim("X"); auto filter_dims = ctx->GetInputDim("Filter"); + PADDLE_ENFORCE(ctx->Attrs().Get("contextStride") == 1, + "Currently, SequenceConvOp only supports contextStride=1."); PADDLE_ENFORCE(in_dims.size() == 2 && filter_dims.size() == 2, "Input(X, Filter) should be 2-D tensor."); PADDLE_ENFORCE(filter_dims[0] == context_length * in_dims[1], "Filter's height should be context_length * " - "number_of_input_features ."); + "input_hidden_size ."); - if (padding_trainable) { + if (ctx->Attrs().Get("paddingTrainable")) { PADDLE_ENFORCE( ctx->HasInput("PaddingData"), "Input(PaddingData) of SequenceConvOp should not be null."); @@ -54,7 +55,7 @@ class SequenceConvOp : public framework::OperatorWithKernel { if (context_start == 0 && context_length == 1) { PADDLE_THROW( - "If context_start is 0 and context_length is 1, padding_trainable " + "If context_start is 0 and context_length is 1, paddingTrainable " "should be false."); } PADDLE_ENFORCE(padding_dim.size() == 2, @@ -81,13 +82,14 @@ class SequenceConvGradOp : public framework::OperatorWithKernel { "Gradient of output(Out) should not be null."); PADDLE_ENFORCE(ctx->HasInput("X"), "The input(X) should not be null."); - if (ctx->Attrs().Get("padding_trainable") && + if (ctx->Attrs().Get("paddingTrainable") && ctx->HasOutput(framework::GradVarName("PaddingData"))) { ctx->SetOutputDim(framework::GradVarName("PaddingData"), ctx->GetInputDim("PaddingData")); } if (ctx->HasOutput(framework::GradVarName("X"))) { ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X")); + ctx->ShareLoD(framework::GradVarName("X"), "X"); } if (ctx->HasOutput(framework::GradVarName("Filter"))) { ctx->SetOutputDim(framework::GradVarName("Filter"), @@ -105,54 +107,58 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker { "X", "(LoDTensor) the input(X) is a LodTensor, which support " "variable-time length input sequence. The underlying tensor in " - "this LoDTensor is a matrix with shape (T, D), where, T is the " - "total time steps in this mini-batch, D is the input feature size."); + "this LoDTensor is a matrix with shape (T, N), where, T is the " + "total time steps in this mini-batch, N is the input_hidden_size."); AddInput("PaddingData", "(Tensor, optional) the input(PaddingData) is an optional " "parameter, and it is learnable. " - "This is a tensor with shape (N, D), where N is the " - "top_pad + bottom_pad, D is the input feature size. In order to " + "This is a tensor with shape (P, N), where P is the " + "top_pad + bottom_pad, N is the input_hidden_size. In order to " "ensure the equal length of sequence before and after " "convolution, it is necessary to fill the top and bottom of each " "sequence according to context_length, context_stride and " "context_start") .AsDispensable(); - AddInput("Filter", - "(Tensor) the input(Filter) is an learnable parameter." - "This is a tensor with shape (N, D), where N is the " - "context_length, D is the output feature size."); + AddInput( + "Filter", + "(Tensor) the input(Filter) is an learnable parameter." + "This is a tensor with shape (K, M), where K is the " + "context_length * input_hidden_size, M is the output feature size."); AddOutput( "Out", "(LoDTensor) the output(Out) is a LodTensor, which support " "variable-time length output sequence. The underlying tensor in " - "this LoDTensor is a matrix with shape (T, D), where, T is the " - "total time steps in this mini-batch, D is the output feature size."); + "this LoDTensor is a matrix with shape (T, M), where, T is the " + "total time steps in this mini-batch, M is the output feature size."); - AddAttr("padding_trainable", - "(bool, default false) the padding data of SequenceConvOp " + AddAttr("paddingTrainable", + "(bool, default:false) the padding data of SequenceConvOp " "is trainable or not.") .SetDefault(false); - AddAttr("context_length", - "(int, default 3) the context_length of SequenceConvOp is the " + AddAttr("contextLength", + "(int) the contextLength of SequenceConvOp is the " "height of the convolution kernel.") - .SetDefault(3) .GreaterThan(0); - AddAttr("context_start", - "(int, default 0) the context_start of SequenceConvOp " + AddAttr("contextStart", + "(int, default:0) the contextStart of SequenceConvOp " "represents the beginning of the convolution of the number of " - "rows of sequence, which can be negative.") + "rows of sequence, which can be negative. The negative number " + "means to pad contextStart time-steps of zeros or learnable " + "parameters at the beginning of each instance. The positive " + "number means to skip contextStart time-steps of each " + "instance.") .SetDefault(0); - AddAttr("context_stride", - "(int, default 1) the context_stride of SequenceConvOp " - "represents the step length of convolution. " + AddAttr("contextStride", + "(int, default:1) the contextStride of SequenceConvOp " + "represents the stride length of convolution kernel. " "Currently, SequenceConvOp only supports" - "context_stride=1.") + "contextStride=1.") .SetDefault(1) .GreaterThan(0); AddComment(R"DOC( SequenceConvOp performs convolution operation on features of - context_length time-steps of each instance. + contextLength time-steps of each instance. The convolution operation calculates the output based on the input, filter and strides, paddings parameters. The size of each dimension of the parameters is checked in the infer-shape. In order to ensure the equal diff --git a/paddle/operators/sequence_conv_op.h b/paddle/operators/sequence_conv_op.h index cd8a8d4cea39161029602530cc75532b5f977d01..a57e1752bb8ed4844423f752bf0ad9f8e114486a 100644 --- a/paddle/operators/sequence_conv_op.h +++ b/paddle/operators/sequence_conv_op.h @@ -35,12 +35,11 @@ class SequenceConvKernel : public framework::OpKernel { out->mutable_data(context.GetPlace()); context.ShareLoD("X", "Out"); - int context_start = context.Attr("context_start"); - int context_length = context.Attr("context_length"); - int context_stride = context.Attr("context_stride"); - bool padding_trainable = context.Attr("padding_trainable"); + int context_start = context.Attr("contextStart"); + int context_length = context.Attr("contextLength"); + int context_stride = context.Attr("contextStride"); + bool padding_trainable = context.Attr("paddingTrainable"); - // InferShape by in_lod PADDLE_ENFORCE_EQ(in->lod().size(), 1UL, "Only support one level sequence now."); @@ -51,26 +50,21 @@ class SequenceConvKernel : public framework::OpKernel { int up_pad = std::max(0, -context_start); int down_pad = std::max(0, context_start + context_length - 1); - int sequence_width; - sequence_width = static_cast(in->dims()[1]); + int sequence_width = static_cast(in->dims()[1]); - // Use col_shape in the im2col calculation. framework::DDim col_shape = {in->dims()[0], - sequence_width * context_length}; + context_length * sequence_width}; Tensor col; col.mutable_data(col_shape, context.GetPlace()); - math::SetConstant set_zero; // Because if padding_trainable is false, padding data should be zeros. + math::SetConstant set_zero; set_zero(context.device_context(), &col, static_cast(0)); - paddle::operators::math::ContextProjectFunctor - seq_project_functor; - LoDTensor* input = const_cast(in); - Tensor* pad_data = const_cast(padding_data); + math::ContextProjectFunctor seq_project_functor; - seq_project_functor(context.device_context(), *input, *pad_data, col, + seq_project_functor(context.device_context(), *in, *padding_data, col, padding_trainable, context_start, context_length, - context_stride, up_pad, down_pad, false, false, false); + context_stride, up_pad, down_pad); math::matmul(context.device_context(), col, false, filter, false, static_cast(1.0), out, static_cast(0.0)); @@ -81,18 +75,18 @@ template class SequenceConvGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { - auto* out_g = context.Input(framework::GradVarName("Out")); auto* in_g = context.Output(framework::GradVarName("X")); + auto* out_g = context.Input(framework::GradVarName("Out")); auto* filter_g = context.Output(framework::GradVarName("Filter")); auto* padding_data_g = context.Output(framework::GradVarName("PaddingData")); auto* in = context.Input("X"); auto* filter = context.Input("Filter"); - int context_start = context.Attr("context_start"); - int context_length = context.Attr("context_length"); - int context_stride = context.Attr("context_stride"); - bool padding_trainable = context.Attr("padding_trainable"); + int context_start = context.Attr("contextStart"); + int context_length = context.Attr("contextLength"); + int context_stride = context.Attr("contextStride"); + bool padding_trainable = context.Attr("paddingTrainable"); PADDLE_ENFORCE_EQ(in->lod().size(), 1UL, "Only support one level sequence now."); @@ -115,17 +109,18 @@ class SequenceConvGradKernel : public framework::OpKernel { math::matmul(context.device_context(), *out_g, false, *filter, true, T(1.0), &col, T(1.0)); } - paddle::operators::math::ContextProjectFunctor - seq_project_functor; + math::ContextProjectFunctor seq_project_functor; + math::ContextProjectGradFunctor seq_project_grad_functor; if (in_g) { in_g->mutable_data(context.GetPlace()); in_g->set_lod(in->lod()); set_zero(context.device_context(), in_g, static_cast(0)); - seq_project_functor(context.device_context(), *in_g, *padding_data_g, col, - padding_trainable, context_start, context_length, - context_stride, up_pad, down_pad, true, true, false); + seq_project_grad_functor(context.device_context(), *in_g, *padding_data_g, + col, padding_trainable, context_start, + context_length, context_stride, up_pad, down_pad, + true, false); } if (padding_trainable && padding_data_g) { @@ -133,9 +128,10 @@ class SequenceConvGradKernel : public framework::OpKernel { set_zero(context.device_context(), padding_data_g, static_cast(0)); LoDTensor* input = const_cast(in); - seq_project_functor(context.device_context(), *input, *padding_data_g, - col, padding_trainable, context_start, context_length, - context_stride, up_pad, down_pad, true, false, true); + seq_project_grad_functor(context.device_context(), *input, + *padding_data_g, col, padding_trainable, + context_start, context_length, context_stride, + up_pad, down_pad, false, true); } if (filter_g) { @@ -150,15 +146,9 @@ class SequenceConvGradKernel : public framework::OpKernel { padding_data = context.Input("PaddingData"); } - sequence_width = static_cast(in->dims()[1]); - - LoDTensor* input = const_cast(in); - Tensor* pad_data = const_cast(padding_data); - - seq_project_functor(context.device_context(), *input, *pad_data, col, + seq_project_functor(context.device_context(), *in, *padding_data, col, padding_trainable, context_start, context_length, - context_stride, up_pad, down_pad, false, false, - false); + context_stride, up_pad, down_pad); math::matmul(context.device_context(), col, true, out_grad, false, T(1.0), &filter_grad, T(1.0)); diff --git a/paddle/operators/sgd_op.cc b/paddle/operators/sgd_op.cc index 2acb96d1b4f5903ff6c57b10e7621c8adaf73171..939176c73dc21dc662b1aaf23d8077c6856a5650 100644 --- a/paddle/operators/sgd_op.cc +++ b/paddle/operators/sgd_op.cc @@ -89,11 +89,12 @@ struct SparseSGDFunctor { }; template struct SparseSGDFunctor; +template struct SparseSGDFunctor; } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OP_WITHOUT_GRADIENT(sgd, ops::SGDOp, ops::SGDOpMaker); -REGISTER_OP_CPU_KERNEL(sgd, - ops::SGDOpKernel); +REGISTER_OP_CPU_KERNEL(sgd, ops::SGDOpKernel, + ops::SGDOpKernel); diff --git a/paddle/operators/sgd_op.cu b/paddle/operators/sgd_op.cu index 106f9b746ba6614d8fa68b677c47ec04ed26fb81..2f41c7fc121950926f6e8d842eb629d59738f321 100644 --- a/paddle/operators/sgd_op.cu +++ b/paddle/operators/sgd_op.cu @@ -71,10 +71,11 @@ struct SparseSGDFunctor { }; template struct SparseSGDFunctor; +template struct SparseSGDFunctor; } // namespace operators } // namespace paddle namespace ops = paddle::operators; -REGISTER_OP_GPU_KERNEL(sgd, - ops::SGDOpKernel); +REGISTER_OP_GPU_KERNEL(sgd, ops::SGDOpKernel, + ops::SGDOpKernel); diff --git a/paddle/operators/sum_op.h b/paddle/operators/sum_op.h index a4be6b61b9042056bcf74936dbd35a69a6a87abc..f2f2c67bc395ea245798b537144dd88a816f4a85 100644 --- a/paddle/operators/sum_op.h +++ b/paddle/operators/sum_op.h @@ -35,13 +35,6 @@ class SumKernel : public framework::OpKernel { if (out_var->IsType()) { auto* out = context.Output("Out"); - // Runtime InferShape - for (int i = 0; i < N; i++) { - if (in_vars[i]->IsType()) { - out->Resize(in_vars[i]->Get().dims()); - break; - } - } out->mutable_data(context.GetPlace()); auto result = EigenVector::Flatten(*out); @@ -73,12 +66,10 @@ class SumKernel : public framework::OpKernel { first_dim += in_vars[i]->Get().rows().size(); } auto in_dim = in_vars[0]->Get().value().dims(); - auto in_dim_vec = framework::vectorize(in_dim); in_dim_vec[0] = static_cast(first_dim); out_value->Resize(framework::make_ddim(in_dim_vec)); - out_value->mutable_data(context.GetPlace()); math::SelectedRowsAddTo functor; diff --git a/paddle/operators/top_k_op.cc b/paddle/operators/top_k_op.cc index d5c2c91a5fb0f639ea84d13e27de8271218da54f..ac9257259548dc4223efac239d8362f69366850c 100644 --- a/paddle/operators/top_k_op.cc +++ b/paddle/operators/top_k_op.cc @@ -52,7 +52,11 @@ class TopkOpMaker : public framework::OpProtoAndCheckerMaker { AddOutput("Out", "The output tensor of Topk op"); AddOutput("Indices", "The indices of Topk elements of input"); AddComment( - R"DOC(If the input is a vector (1d tensor), finds the k largest entries in the vector and outputs their values and indices as vectors. Thus values[j] is the j-th largest entry in input, and its index is indices[j]. + R"DOC(If the input is a vector (1d tensor), + finds the k largest entries in the vector + and outputs their values and indices as vectors. + Thus values[j] is the j-th largest entry in input, + and its index is indices[j]. For matrices, computes the top k entries in each row. )DOC"); AddAttr("k", @@ -66,6 +70,7 @@ class TopkOpMaker : public framework::OpProtoAndCheckerMaker { } // namespace paddle namespace ops = paddle::operators; -REGISTER_OP_WITHOUT_GRADIENT(top_k, ops::TopkOp, ops::TopkOpMaker); +REGISTER_OPERATOR(top_k, ops::TopkOp, ops::TopkOpMaker, + paddle::framework::EmptyGradOpMaker); REGISTER_OP_CPU_KERNEL(top_k, ops::TopkKernel); diff --git a/paddle/operators/top_k_op.cu b/paddle/operators/top_k_op.cu index 7be6932f1e301d06e0e232367a38bfa673ff45be..7851c71bbe9fe73402968ce14f6db0df523cd6d3 100644 --- a/paddle/operators/top_k_op.cu +++ b/paddle/operators/top_k_op.cu @@ -23,9 +23,9 @@ using Tensor = framework::Tensor; template struct Pair { __device__ __forceinline__ Pair() {} - __device__ __forceinline__ Pair(T value, int id) : v(value), id(id) {} + __device__ __forceinline__ Pair(T value, int64_t id) : v(value), id(id) {} - __device__ __forceinline__ void set(T value, int id) { + __device__ __forceinline__ void set(T value, int64_t id) { v = value; id = id; } @@ -48,7 +48,7 @@ struct Pair { } T v; - int id; + int64_t id; }; template @@ -197,7 +197,7 @@ __device__ __forceinline__ void ThreadGetTopK(Pair topk[], int& beam, template __device__ __forceinline__ void BlockReduce(Pair* sh_topk, int* maxid, Pair topk[], T** topVal, - int** topIds, int& beam, int& k, + int64_t** topIds, int& beam, int& k, const int tid, const int warp) { while (true) { __syncthreads(); @@ -249,7 +249,7 @@ __device__ __forceinline__ void BlockReduce(Pair* sh_topk, int* maxid, * 4. go to the first setp, until get the topk value. */ template -__global__ void KeMatrixTopK(T* output, int output_stride, int* indices, +__global__ void KeMatrixTopK(T* output, int output_stride, int64_t* indices, const T* src, int lds, int dim, int k) { __shared__ Pair sh_topk[BlockSize]; __shared__ int maxid[BlockSize / 2]; @@ -293,7 +293,7 @@ class TopkOpCUDAKernel : public framework::OpKernel { T* output_data = output->mutable_data(ctx.GetPlace()); // FIXME(typhoonzero): data is always converted to type T? - int* indices_data = indices->mutable_data(ctx.GetPlace()); + int64_t* indices_data = indices->mutable_data(ctx.GetPlace()); size_t input_height = input->dims()[0]; size_t input_width = input->dims()[1]; diff --git a/paddle/operators/top_k_op.h b/paddle/operators/top_k_op.h index 4b248faa120bcfd20e70d288cce2d485d3e6371e..bc8563717a21bd5b3d8fc87f689657990066957b 100644 --- a/paddle/operators/top_k_op.h +++ b/paddle/operators/top_k_op.h @@ -40,7 +40,7 @@ class TopkKernel : public framework::OpKernel { const size_t k = static_cast(ctx.Attr("k")); T* output_data = output->mutable_data(ctx.GetPlace()); - T* indices_data = indices->mutable_data(ctx.GetPlace()); + int64_t* indices_data = indices->mutable_data(ctx.GetPlace()); auto eg_input = EigenMatrix::From(*input); @@ -66,7 +66,7 @@ class TopkKernel : public framework::OpKernel { }); for (size_t j = 0; j < k; j++) { output_data[i * k + j] = vec[j].first; - indices_data[i * k + j] = vec[j].second; + indices_data[i * k + j] = int64_t(vec[j].second); } } } diff --git a/paddle/operators/uniform_random_op.cc b/paddle/operators/uniform_random_op.cc index 39b53948e3cc58ff1d0ab481143b066b1a2fae16..82f9b8fbf1094bde1def83b9a1c464207b7e4669 100644 --- a/paddle/operators/uniform_random_op.cc +++ b/paddle/operators/uniform_random_op.cc @@ -95,4 +95,5 @@ Used to initialize tensor with uniform random generator. REGISTER_OP_WITHOUT_GRADIENT(uniform_random, paddle::operators::UniformRandomOp, paddle::operators::UniformRandomOpMaker); REGISTER_OP_CPU_KERNEL(uniform_random, - paddle::operators::CPUUniformRandomKernel); + paddle::operators::CPUUniformRandomKernel, + paddle::operators::CPUUniformRandomKernel); diff --git a/paddle/operators/uniform_random_op.cu b/paddle/operators/uniform_random_op.cu index 5612ce9eb1c644d6271b4a9bb949f685848e05c0..8b20bb8287807aca673817c503fee6db04b55753 100644 --- a/paddle/operators/uniform_random_op.cu +++ b/paddle/operators/uniform_random_op.cu @@ -64,4 +64,5 @@ class GPUUniformRandomKernel : public framework::OpKernel { } // namespace paddle REGISTER_OP_GPU_KERNEL(uniform_random, - paddle::operators::GPUUniformRandomKernel); + paddle::operators::GPUUniformRandomKernel, + paddle::operators::GPUUniformRandomKernel); diff --git a/paddle/platform/CMakeLists.txt b/paddle/platform/CMakeLists.txt index eb850b658583f2256629d63fdb64248dbf249937..bd86a9fe268c277065cd450f91b544def6c4d32f 100644 --- a/paddle/platform/CMakeLists.txt +++ b/paddle/platform/CMakeLists.txt @@ -9,7 +9,6 @@ cc_test(place_test SRCS place_test.cc DEPS place glog gflags) add_subdirectory(dynload) cc_test(enforce_test SRCS enforce_test.cc DEPS stringpiece) -cc_test(environment_test SRCS environment_test.cc DEPS stringpiece) IF(WITH_GPU) set(GPU_CTX_DEPS dynload_cuda dynamic_loader) diff --git a/paddle/platform/environment.h b/paddle/platform/environment.h deleted file mode 100644 index 4edcce932edc61453cef74f2c4ee0f72496b3677..0000000000000000000000000000000000000000 --- a/paddle/platform/environment.h +++ /dev/null @@ -1,60 +0,0 @@ -/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. - -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. */ - -#pragma once - -#include -#include -#include - -#include "paddle/platform/enforce.h" -#include "paddle/string/piece.h" - -extern char** environ; // for environment variables - -namespace paddle { -namespace platform { - -inline void SetEnvVariable(const std::string& name, const std::string& value) { - PADDLE_ENFORCE_NE(setenv(name.c_str(), value.c_str(), 1), -1, - "Failed to set environment variable %s=%s", name, value); -} - -inline void UnsetEnvVariable(const std::string& name) { - PADDLE_ENFORCE_NE(unsetenv(name.c_str()), -1, - "Failed to unset environment variable %s", name); -} - -inline bool IsEnvVarDefined(const std::string& name) { - return std::getenv(name.c_str()) != nullptr; -} - -inline std::string GetEnvValue(const std::string& name) { - PADDLE_ENFORCE(IsEnvVarDefined(name), - "Tried to access undefined environment variable %s", name); - return std::getenv(name.c_str()); -} - -inline std::vector GetAllEnvVariables() { - std::vector vars; - for (auto var = environ; *var != nullptr; ++var) { - auto tail = string::Index(*var, "="); - auto name = string::SubStr(*var, 0, tail).ToString(); - vars.push_back(name); - } - return vars; -} - -} // namespace platform -} // namespace paddle diff --git a/paddle/platform/environment_test.cc b/paddle/platform/environment_test.cc deleted file mode 100644 index 5f136527215d6a676cfa1a3b08f09dfd3ab24a90..0000000000000000000000000000000000000000 --- a/paddle/platform/environment_test.cc +++ /dev/null @@ -1,54 +0,0 @@ -/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. -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. */ - -#include "paddle/platform/environment.h" - -#include "glog/logging.h" -#include "gtest/gtest.h" - -TEST(ENVIRONMENT, ACCESS) { - namespace platform = paddle::platform; - namespace string = paddle::string; - - platform::SetEnvVariable("PADDLE_USE_ENV", "TRUE"); - - EXPECT_TRUE(platform::IsEnvVarDefined("PADDLE_USE_ENV")); - EXPECT_EQ(platform::GetEnvValue("PADDLE_USE_ENV"), "TRUE"); - - platform::UnsetEnvVariable("PADDLE_USE_ENV"); - EXPECT_FALSE(platform::IsEnvVarDefined("PADDLE_USE_ENV")); - - platform::SetEnvVariable("PADDLE_USE_ENV1", "Hello "); - platform::SetEnvVariable("PADDLE_USE_ENV2", "World, "); - platform::SetEnvVariable("PADDLE_USE_ENV3", "PaddlePaddle!"); - - std::string env_info; - auto vars = platform::GetAllEnvVariables(); - for_each(vars.begin(), vars.end(), [&](const std::string& var) { - env_info += platform::GetEnvValue(var); - }); - - EXPECT_TRUE(string::Contains(env_info, "Hello World, PaddlePaddle!")); - platform::UnsetEnvVariable("PADDLE_USE_ENV1"); - platform::UnsetEnvVariable("PADDLE_USE_ENV2"); - platform::UnsetEnvVariable("PADDLE_USE_ENV3"); - - env_info.clear(); - vars = platform::GetAllEnvVariables(); - for_each(vars.begin(), vars.end(), [&](const std::string& var) { - env_info += platform::GetEnvValue(var); - }); - - EXPECT_FALSE(string::Contains(env_info, "Hello World, PaddlePaddle!")); - EXPECT_FALSE(platform::IsEnvVarDefined("PADDLE_USE_ENV1")); - EXPECT_FALSE(platform::IsEnvVarDefined("PADDLE_USE_ENV2")); - EXPECT_FALSE(platform::IsEnvVarDefined("PADDLE_USE_ENV3")); -} diff --git a/paddle/platform/gpu_info.cc b/paddle/platform/gpu_info.cc index 0cab5ffc5609bbd6fd08c74329d8370fb95f8102..f3455a8733862c91eaece629b6684d446672336c 100644 --- a/paddle/platform/gpu_info.cc +++ b/paddle/platform/gpu_info.cc @@ -17,7 +17,6 @@ limitations under the License. */ #include "gflags/gflags.h" #include "paddle/platform/enforce.h" -#include "paddle/platform/environment.h" DEFINE_double(fraction_of_gpu_memory_to_use, 0.95, "Default use 95% of GPU memory for PaddlePaddle," @@ -75,13 +74,6 @@ size_t GpuMaxChunkSize() { GpuMemoryUsage(available, total); - if (IsEnvVarDefined(kEnvFractionGpuMemoryToUse)) { - auto val = std::stod(GetEnvValue(kEnvFractionGpuMemoryToUse)); - PADDLE_ENFORCE_GT(val, 0.0); - PADDLE_ENFORCE_LE(val, 1.0); - FLAGS_fraction_of_gpu_memory_to_use = val; - } - // Reserving the rest memory for page tables, etc. size_t reserving = (1 - FLAGS_fraction_of_gpu_memory_to_use) * total; diff --git a/paddle/platform/nccl_test.cu b/paddle/platform/nccl_test.cu index ab8b96f7263aed83407866fedf9e529ce0affe3f..c99dae68bef67c58d3efea42fef45e84bb3d9255 100644 --- a/paddle/platform/nccl_test.cu +++ b/paddle/platform/nccl_test.cu @@ -31,9 +31,7 @@ namespace platform { TEST(NCCL, init) { std::vector comms; comms.resize(dev_count); - - auto status = dynload::ncclCommInitAll(comms.data(), dev_count, nullptr); - PADDLE_ENFORCE(status); + PADDLE_ENFORCE(dynload::ncclCommInitAll(comms.data(), dev_count, nullptr)); for (int i = 0; i < dev_count; ++i) { dynload::ncclCommDestroy(comms[i]); } @@ -64,8 +62,7 @@ TEST(NCCL, all_reduce) { std::vector comms; comms.resize(dev_count); VLOG(1) << "Initializing ncclComm"; - auto status = dynload::ncclCommInitAll(comms.data(), dev_count, nullptr); - PADDLE_ENFORCE(status); + PADDLE_ENFORCE(dynload::ncclCommInitAll(comms.data(), dev_count, nullptr)); VLOG(1) << "ncclComm initialized"; VLOG(1) << "Creating thread data"; std::vector>> data; diff --git a/paddle/pybind/pybind.cc b/paddle/pybind/pybind.cc index e9c1d40de185768048159c10ee278ffde12335f7..2a0075356ed1e0f0b3501ac681c5e3a1bf37e2ca 100644 --- a/paddle/pybind/pybind.cc +++ b/paddle/pybind/pybind.cc @@ -14,6 +14,9 @@ limitations under the License. */ #include "paddle/pybind/protobuf.h" +#include // for call_once +#include +#include "gflags/gflags.h" #include "paddle/framework/backward.h" #include "paddle/framework/executor.h" #include "paddle/framework/feed_fetch_method.h" @@ -33,11 +36,34 @@ limitations under the License. */ #include "paddle/pybind/tensor_py.h" #include "paddle/string/to_string.h" +#ifdef PADDLE_WITH_CUDA +#include "paddle/operators/nccl/nccl_gpu_common.h" +#include "paddle/platform/gpu_info.h" +#endif + namespace paddle { namespace pybind { -static size_t UniqueIntegerGenerator() { - static std::atomic generator; - return generator.fetch_add(1); +static size_t UniqueIntegerGenerator(const std::string &prefix) { + static std::unordered_map> generators; + return generators[prefix].fetch_add(1); +} + +std::once_flag gflags_init_flag; + +// TODO(qijun) move init gflags to init.cc +void InitGflags(std::vector &argv) { + std::call_once(gflags_init_flag, [&]() { + int argc = argv.size(); + char **arr = new char *[argv.size()]; + std::string line; + for (size_t i = 0; i < argv.size(); i++) { + arr[i] = &argv[i][0]; + line += argv[i]; + line += ' '; + } + google::ParseCommandLineFlags(&argc, &arr, true); + VLOG(1) << "Init commandline: " << line; + }); } bool IsCompileGPU() { @@ -204,6 +230,13 @@ All parameter, weight, gradient are variables in Paddle. return self.GetMutable(); }, py::return_value_policy::reference) +#ifdef PADDLE_WITH_CUDA + .def("get_communicator", + [](Variable &self) -> platform::Communicator * { + return self.GetMutable(); + }, + py::return_value_policy::reference) +#endif .def("get_net", [](Variable &self) -> operators::NetOp * { return self.GetMutable(); @@ -269,8 +302,11 @@ All parameter, weight, gradient are variables in Paddle. return new paddle::platform::CUDADeviceContext(place); #endif }); - // clang-format on +// clang-format on +#ifdef PADDLE_WITH_CUDA + py::class_(m, "Communicator").def(py::init<>()); +#endif py::class_(m, "GPUPlace") .def(py::init()) .def("__str__", string::to_string); @@ -468,6 +504,7 @@ All parameter, weight, gradient are variables in Paddle. }); m.def("unique_integer", UniqueIntegerGenerator); + m.def("init_gflags", InitGflags); m.def("is_compile_gpu", IsCompileGPU); m.def("set_feed_variable", framework::SetFeedVariable); @@ -479,6 +516,9 @@ All parameter, weight, gradient are variables in Paddle. BindOpDesc(m); m.def("op_support_gpu", OpSupportGPU); +#ifdef PADDLE_WITH_CUDA + m.def("get_cuda_device_count", platform::GetCUDADeviceCount); +#endif return m.ptr(); } diff --git a/paddle/pybind/tensor_py.h b/paddle/pybind/tensor_py.h index 85f9f22733c97ef209e6c25dbcfbac492ac5c746..f278e79af60486bce400f313b80ebbe3971f869b 100644 --- a/paddle/pybind/tensor_py.h +++ b/paddle/pybind/tensor_py.h @@ -85,7 +85,8 @@ struct CastToPyBufferImpl { } // namespace details inline py::buffer_info CastToPyBuffer(framework::Tensor &tensor) { auto buffer_info = - details::CastToPyBufferImpl()(tensor); + details::CastToPyBufferImpl()( + tensor); return buffer_info; } diff --git a/proto/CMakeLists.txt b/proto/CMakeLists.txt index 5d898d860cfc6dc26eaf5a81d8aed6d757ed5831..556bcd1d7e60c27fece43de666e9531ab4203414 100644 --- a/proto/CMakeLists.txt +++ b/proto/CMakeLists.txt @@ -27,3 +27,30 @@ foreach(filename ${proto_filenames}) endforeach() add_custom_target(gen_proto_py ALL DEPENDS ${PROTO_GEN_PY}) + + +if (WITH_GOLANG) + add_custom_target(protoc-gen-go) + add_custom_command(TARGET protoc-gen-go + COMMAND go + ARGS "get" "-u" "github.com/golang/protobuf/protoc-gen-go") + + set(PROTO_GEN_GO) + file(GLOB proto_filenames . OptimizerConfig.proto) + foreach(filename ${proto_filenames}) + message(STATUS ${filename}) + get_filename_component(ABS_FIL ${filename} ABSOLUTE) + get_filename_component(FIL_WE ${filename} NAME_WE) + set(CUR_PROTO_GEN_GO + ${PADDLE_SOURCE_DIR}/paddle/go/proto/${FIL_WE}.pb.go) + set(PROTO_GEN_GO + ${CUR_PROTO_GEN_GO} + ${PROTO_GEN_GO}) + add_custom_command(OUTPUT ${CUR_PROTO_GEN_GO} + COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} + ARGS "--go_out=${PADDLE_SOURCE_DIR}/go/proto" + "-I" ${CMAKE_CURRENT_SOURCE_DIR} ${ABS_FIL} + DEPENDS ${ABS_FIL} protoc protoc-gen-go) + endforeach() + add_custom_target(gen_proto_go ALL DEPENDS ${PROTO_GEN_GO}) +endif() diff --git a/python/paddle/v2/framework/__init__.py b/python/paddle/v2/framework/__init__.py index c942373c667733f8aabe63026998a8915618130a..5df612bf3530c843c16b337f2b8f83445fcf39b5 100644 --- a/python/paddle/v2/framework/__init__.py +++ b/python/paddle/v2/framework/__init__.py @@ -1 +1,11 @@ +import sys +import core __all__ = ['proto'] +argv = [] +if core.is_compile_gpu(): + argv = list(sys.argv) + [ + "--tryfromenv=fraction_of_gpu_memory_to_use,use_pinned_memory" + ] +else: + argv = list(sys.argv) + ["--tryfromenv=use_pinned_memory"] +core.init_gflags(argv) diff --git a/python/paddle/v2/framework/framework.py b/python/paddle/v2/framework/framework.py index 348c393913b3d73f9c9c16580d19a19551f2a57b..f8d2f67410a6c06a1642180d2d62c881ec6bda3d 100644 --- a/python/paddle/v2/framework/framework.py +++ b/python/paddle/v2/framework/framework.py @@ -119,8 +119,9 @@ class Variable(object): @staticmethod def _unique_var_name_(): - uid = core.unique_integer() # unique during whole process. - return "_generated_var_%d" % uid + prefix = "_generated_var" + uid = core.unique_integer(prefix) # unique during whole process. + return "_".join([prefix, str(uid)]) @staticmethod def _convert_np_dtype_to_dtype_(np_dtype): @@ -352,7 +353,10 @@ class Block(object): return {v for k, v in self.vars.iteritems() if isinstance(v, Parameter)} def create_var(self, *args, **kwargs): - return Variable(self, *args, **kwargs) + var = Variable(self, *args, **kwargs) + if 'init_attr' in kwargs: + self._prepend_initialize_ops_(var, kwargs['init_attr']) + return var def has_var(self, name): return name in self.vars diff --git a/python/paddle/v2/framework/layer_helper.py b/python/paddle/v2/framework/layer_helper.py index 1f72c9bc7b0ceda1dd954703fcc10c77a3e5ed25..d96dbe172c22617182e7ebf4aab175c6142352b7 100644 --- a/python/paddle/v2/framework/layer_helper.py +++ b/python/paddle/v2/framework/layer_helper.py @@ -8,7 +8,7 @@ from paddle.v2.framework.framework import Variable, g_program, \ def unique_name(prefix): - uid = core.unique_integer() # unique during whole process. + uid = core.unique_integer(prefix) # unique during whole process. return "_".join([prefix, str(uid)]) diff --git a/python/paddle/v2/framework/layers.py b/python/paddle/v2/framework/layers.py index 4bb763e6d9be39f8f1cc3521767c4f46537db7d4..6451d11e2b68692527addb424b0cd716f23bd77a 100644 --- a/python/paddle/v2/framework/layers.py +++ b/python/paddle/v2/framework/layers.py @@ -5,7 +5,7 @@ import re __all__ = [ 'fc', 'data', 'cross_entropy', 'conv2d', 'pool2d', 'embedding', 'concat', - 'StaticRNN' + 'StaticRNN', 'cast', 'sequence_conv', 'sequence_pool', 'accuracy' ] @@ -61,6 +61,7 @@ def fc(input, def embedding(input, size, data_type='float32', + is_sparse=False, param_attr=None, program=None, init_program=None): @@ -72,7 +73,8 @@ def embedding(input, type='lookup_table', inputs={'Ids': input, 'W': w}, - outputs={'Out': tmp}) + outputs={'Out': tmp}, + attrs={'is_sparse': is_sparse}) return tmp @@ -148,7 +150,7 @@ def _create_op_func_(op_type): outputs[name] = [helper.create_tmp_variable(dtype=dtype)] helper.append_op( type=op_type, inputs=inputs, outputs=outputs, attrs=kwargs) - return out + return helper.append_activation(out) func.__name__ = op_type globals()[op_type] = func @@ -158,7 +160,33 @@ def _create_op_func_(op_type): _create_op_func_('mean') _create_op_func_('mul') +_create_op_func_('elementwise_add') _create_op_func_('dropout') +_create_op_func_('reshape') + + +def cast(x, data_type, program=None): + helper = LayerHelper('cast', **locals()) + out = helper.create_tmp_variable(dtype=data_type) + helper.append_op( + type='cast', + inputs={'X': [x]}, + outputs={'Out': [out]}, + attrs={'in_data_type': x.data_type, + 'out_data_type': out.data_type}) + return out + + +def cast(x, data_type, program=None): + helper = LayerHelper('cast', **locals()) + out = helper.create_tmp_variable(dtype=data_type) + helper.append_op( + type='cast', + inputs={'X': [x]}, + outputs={'Out': [out]}, + attrs={'in_data_type': x.data_type, + 'out_data_type': out.data_type}) + return out def concat(input, axis, program=None, init_program=None): @@ -197,13 +225,73 @@ def square_error_cost(input, label, **kwargs): square_out = helper.create_tmp_variable(dtype=input.data_type) helper.append_op( - type='pow', - inputs={'X': [minus_out]}, - outputs={'Y': [square_out]}, - attrs={'factor': 2.0}) + type='square', inputs={'X': [minus_out]}, outputs={'Y': [square_out]}) return square_out +def accuracy(input, label, k=1, **kwargs): + helper = LayerHelper("accuracy", **kwargs) + topk_out = helper.create_tmp_variable(dtype=input.data_type) + topk_indices = helper.create_tmp_variable(dtype="int64") + helper.append_op( + type="top_k", + inputs={"X": [input]}, + outputs={"Out": [topk_out], + "Indices": [topk_indices]}, + attrs={"k": k}) + acc_out_dtype = kwargs.get("out_dtype", "float32") + acc_out = helper.create_tmp_variable(dtype=acc_out_dtype) + helper.append_op( + type="accuracy", + inputs={ + "Out": [topk_out], + "Indices": [topk_indices], + "Label": [label] + }, + outputs={"Accuracy": [acc_out]}) + return acc_out + + +def sequence_conv(input, + num_filters, + name=None, + filter_size=3, + act=None, + stride=1, + padding=None, + bias_attr=None, + param_attr=None, + program=None, + init_program=None): + # FIXME(dzh) : want to unify the argument of python layer + # function. So we ignore some unecessary attributes. + # such as, padding_trainable, context_start. + + helper = LayerHelper('sequence_conv', **locals()) + dtype = helper.input_dtype() + + filter_shape = [num_filters, filter_size] + filter = helper.create_parameter( + attr=helper.param_attr, shape=filter_shape, dtype=dtype) + pre_bias = helper.create_tmp_variable(dtype) + + helper.append_op( + type='sequence_conv', + inputs={ + 'X': [input], + 'Filter': filter, + }, + outputs={"Out": pre_bias}, + attrs={ + 'context_stride': stride, + 'context_start': 0, + 'context_length': filter_size + }) + + pre_act = helper.append_bias_op(pre_bias) + return helper.append_activation(pre_act) + + def conv2d(input, num_filters, name=None, @@ -256,6 +344,35 @@ def conv2d(input, return helper.append_activation(pre_act) +def sequence_pool(input, + pool_size, + pool_type, + pool_stride=1, + pool_padding=0, + global_pooling=False, + program=None, + init_program=None): + # FIXME(dzh) : want to unify the argument of python layer + # function. So we ignore some unecessary attributes + + ENUM_POOL_TYPE = set(["max", "avg", "sqrt", "last", "first"]) + if pool_type not in ENUM_POOL_TYPE: + raise ValueError("Unknown pool_type: '%s'. It can only be %s.", + str(pool_type), " ".join(ENUM_POOL_TYPE)) + + helper = LayerHelper('sequence_pool', **locals()) + dtype = helper.input_dtype() + pool_out = helper.create_tmp_variable(dtype) + + helper.append_op( + type="sequence_pool", + inputs={"X": [input]}, + outputs={"Out": pool_out}, + attrs={"strategy": pool_type}) + + return pool_out + + def pool2d(input, pool_size, pool_type, @@ -275,7 +392,7 @@ def pool2d(input, if isinstance(pool_padding, int): pool_padding = [pool_padding, pool_padding] - helper = LayerHelper('conv2d', **locals()) + helper = LayerHelper('pool2d', **locals()) dtype = helper.input_dtype() pool_out = helper.create_tmp_variable(dtype) @@ -294,6 +411,96 @@ def pool2d(input, return pool_out +def batch_norm(input, + act=None, + is_test=False, + momentum=0.9, + epsilon=1e05, + param_attr=None, + bias_attr=None, + data_layout='NCHW', + program=None, + init_program=None): + helper = LayerHelper('batch_norm', **locals()) + dtype = helper.input_dtype() + + input_shape = input.shape + if data_layout == 'NCHW': + channel_num = input_shape[1] + else: + if data_layout == 'NHWC': + channel_num = input_shape[-1] + else: + raise ValueError("unsupported data layout:" + data_layout) + + def get_init_attr(value): + if not isinstance(value, float): + raise ValueError("attr value should be a float") + return {'type': 'fill_constant', 'value': value} + + def prepend_init_op(var, init_attr): + assert isinstance(var, Variable) + op_type = init_attr['type'] + init_attr['shape'] = var.shape + init_attr['data_type'] = int(var.data_type) + op = var.block.prepend_op( + type=op_type, inputs=None, outputs={'Out': [var]}, attrs=init_attr) + return op + + def create_persistable_var(dtype, shape, init_attr=None): + name = unique_name(".".join([helper.name, "xxxx"])) + var = init_program.global_block().create_var( + dtype=dtype, shape=shape, name=name, persistable=True) + if 'init_attr' is not None: + prepend_init_op(var, init_attr) + return program.global_block().create_var( + name=name, dtype=dtype, shape=shape, persistable=True) + + param_shape = [channel_num] + + # create parameter + scale = helper.create_parameter( + attr=helper.param_attr, shape=param_shape, dtype=dtype) + bias = helper.create_parameter( + attr=helper.param_attr, shape=param_shape, dtype=dtype) + + # create input + mean = create_persistable_var(dtype, param_shape, get_init_attr(0.0)) + variance = create_persistable_var(dtype, param_shape, get_init_attr(1.0)) + + # create output + # mean and mean_out share the same memory + mean_out = mean + # variance and variance out share the same memory + variance_out = variance + saved_mean = helper.create_tmp_variable(dtype) + saved_variance = helper.create_tmp_variable(dtype) + + batch_norm_out = helper.create_tmp_variable(dtype) + + helper.append_op( + type="batch_norm", + inputs={ + "X": input, + "Scale": scale, + "Bias": bias, + "Mean": mean, + "Variance": variance + }, + outputs={ + "Y": batch_norm_out, + "MeanOut": mean_out, + "VarianceOut": variance_out, + "SavedMean": saved_mean, + "SavedVariance": saved_variance + }, + attrs={"momentum": momentum, + "epsilon": epsilon, + "is_test": is_test}) + + return helper.append_activation(batch_norm_out) + + class BlockGuard(object): """ BlockGuard used to create sub-block in program by using Python `with` diff --git a/python/paddle/v2/framework/nets.py b/python/paddle/v2/framework/nets.py index 8a83ebfb9639f6fae6344b68509a80580881dab0..a9998073e164a223e5d99fc26146ba48027d7a3e 100644 --- a/python/paddle/v2/framework/nets.py +++ b/python/paddle/v2/framework/nets.py @@ -1,12 +1,15 @@ import paddle.v2.framework.layers as layers +__all__ = ["simple_img_conv_pool", "sequence_conv_pool"] + def simple_img_conv_pool(input, - filter_size, num_filters, + filter_size, pool_size, pool_stride, act, + pool_type='max', program=None, init_program=None): conv_out = layers.conv2d( @@ -18,6 +21,100 @@ def simple_img_conv_pool(input, init_program=init_program) pool_out = layers.pool2d( + input=conv_out, + pool_size=pool_size, + pool_type=pool_type, + pool_stride=pool_stride, + program=program, + init_program=init_program) + return pool_out + + +def img_conv_group(input, + conv_num_filter, + pool_size, + conv_padding=1, + conv_filter_size=3, + conv_act=None, + conv_with_batchnorm=False, + conv_batchnorm_drop_rate=None, + pool_stride=1, + pool_type=None, + program=None, + init_program=None): + """ + Image Convolution Group, Used for vgg net. + """ + tmp = input + assert isinstance(conv_num_filter, list) or \ + isinstance(conv_num_filter, tuple) + + def __extend_list__(obj): + if not hasattr(obj, '__len__'): + return [obj] * len(conv_num_filter) + else: + return obj + + conv_padding = __extend_list__(conv_padding) + conv_filter_size = __extend_list__(conv_filter_size) + conv_with_batchnorm = __extend_list__(conv_with_batchnorm) + conv_batchnorm_drop_rate = __extend_list__(conv_batchnorm_drop_rate) + + for i in xrange(len(conv_num_filter)): + local_conv_act = conv_act + if conv_with_batchnorm[i]: + local_conv_act = None + + tmp = layers.conv2d( + input=tmp, + num_filters=conv_num_filter[i], + filter_size=conv_filter_size[i], + padding=conv_padding[i], + act=local_conv_act, + program=program, + init_program=init_program) + + if conv_with_batchnorm[i]: + tmp = layers.batch_norm( + input=tmp, + act=conv_act, + program=program, + init_program=init_program) + drop_rate = conv_batchnorm_drop_rate[i] + if abs(drop_rate) > 1e-5: + tmp = layers.dropout( + x=tmp, + dropout_prob=drop_rate, + program=program, + init_program=init_program) + + pool_out = layers.pool2d( + input=tmp, + pool_size=pool_size, + pool_type=pool_type, + pool_stride=pool_stride, + program=program, + init_program=init_program) + return pool_out + + +def sequence_conv_pool(input, + num_filters, + filter_size, + pool_size, + pool_stride, + act, + program=None, + init_program=None): + conv_out = layers.sequence_conv( + input=input, + num_filters=num_filters, + filter_size=filter_size, + act=act, + program=program, + init_program=init_program) + + pool_out = layers.sequence_pool( input=conv_out, pool_size=pool_size, pool_type='max', diff --git a/python/paddle/v2/framework/tests/op_test.py b/python/paddle/v2/framework/tests/op_test.py index 50360e6e729df2957a5c7fe871100b5a53bd9305..2e6710b5fcfe5a531067498e38a4cb93d3165602 100644 --- a/python/paddle/v2/framework/tests/op_test.py +++ b/python/paddle/v2/framework/tests/op_test.py @@ -281,7 +281,8 @@ class OpTest(unittest.TestCase): type(sub_out)) for sub_out_name, expect in sub_out: idx = find_actual(sub_out_name, fetch_list) - actual_t = np.array(outs[idx]) + actual = outs[idx] + actual_t = np.array(actual) expect_t = expect[0] \ if isinstance(expect, tuple) else expect self.assertTrue( @@ -291,11 +292,12 @@ class OpTest(unittest.TestCase): str(place)) if isinstance(expect, tuple): self.assertListEqual( - actual_t.lod(), expect[1], "Output (" + sub_out_name - + ") has different lod at " + str(place)) + actual.lod(), expect[1], "Output (" + sub_out_name + + ") has different lod at " + str(place)) else: idx = find_actual(out_name, fetch_list) - actual_t = outs[idx] + actual = outs[idx] + actual_t = np.array(actual) expect = self.outputs[out_name] expect_t = expect[0] if isinstance(expect, tuple) else expect self.assertTrue( @@ -303,7 +305,7 @@ class OpTest(unittest.TestCase): actual_t, expect_t, atol=atol), "Output (" + out_name + ") has diff at " + str(place)) if isinstance(expect, tuple): - self.assertListEqual(actual_t.lod(), expect[1], + self.assertListEqual(actual.lod(), expect[1], "Output (" + out_name + ") has different lod at " + str(place)) diff --git a/python/paddle/v2/framework/tests/test_accuracy_op.py b/python/paddle/v2/framework/tests/test_accuracy_op.py index 02be9a02910bee3eae63e12cceaa51cf53591539..6536c297e8e559bf04fe6ef3b0e2dadd1914eb87 100644 --- a/python/paddle/v2/framework/tests/test_accuracy_op.py +++ b/python/paddle/v2/framework/tests/test_accuracy_op.py @@ -7,12 +7,13 @@ class TestAccuracyOp(OpTest): def setUp(self): self.op_type = "accuracy" n = 8192 - infer = np.random.randint(0, 2, (n, 1)).astype("int") - label = np.random.randint(0, 2, (n, )).astype("int") - self.inputs = {'Inference': infer, "Label": label} + infer = np.random.random((n, 1)).astype("float32") + indices = np.random.randint(0, 2, (n, 1)) + label = np.random.randint(0, 2, (n, 1)) + self.inputs = {'Out': infer, 'Indices': indices, "Label": label} num_correct = 0 for rowid in xrange(n): - for ele in infer[rowid]: + for ele in indices[rowid]: if ele == label[rowid]: num_correct += 1 break diff --git a/python/paddle/v2/framework/tests/test_auc_op.py b/python/paddle/v2/framework/tests/test_auc_op.py index 65f679cfccccae41b8924bc68833c1703dd3671d..26ea905d88093605dff820b178996a5724becf82 100644 --- a/python/paddle/v2/framework/tests/test_auc_op.py +++ b/python/paddle/v2/framework/tests/test_auc_op.py @@ -6,10 +6,11 @@ from op_test import OpTest class TestAucOp(OpTest): def setUp(self): self.op_type = "auc" - pred = np.random.random((128)).astype("float32") - labels = np.random.randint(0, 2, (128, )) + pred = np.random.random((128, 2)).astype("float32") + indices = np.random.randint(0, 2, (128, 2)) + labels = np.random.randint(0, 2, (128, 1)) num_thresholds = 200 - self.inputs = {'Inference': pred, 'Label': labels} + self.inputs = {'Out': pred, 'Indices': indices, 'Label': labels} self.attrs = {'curve': 'ROC', 'num_thresholds': num_thresholds} # NOTE: sklearn use a different way to generate thresholds # which will cause the result differs slightly: @@ -31,12 +32,12 @@ class TestAucOp(OpTest): tp, fn, tn, fp = 0, 0, 0, 0 for i, lbl in enumerate(labels): if lbl: - if pred[i] >= thresh: + if pred[i, 0] >= thresh: tp += 1 else: fn += 1 else: - if pred[i] >= thresh: + if pred[i, 0] >= thresh: fp += 1 else: tn += 1 @@ -62,6 +63,5 @@ class TestAucOp(OpTest): self.check_output() -# TODO(typhoonzero): add this back till we fix it -#if __name__ == "__main__": -# unittest.main() +if __name__ == "__main__": + unittest.main() diff --git a/python/paddle/v2/framework/tests/test_cast_op.py b/python/paddle/v2/framework/tests/test_cast_op.py new file mode 100644 index 0000000000000000000000000000000000000000..52ee71a8a4058a1367d9e493e02d8f2469ccfc9f --- /dev/null +++ b/python/paddle/v2/framework/tests/test_cast_op.py @@ -0,0 +1,26 @@ +import op_test +import unittest +import numpy as np +import paddle.v2.framework.core as core + + +class TestCastOp(op_test.OpTest): + def setUp(self): + ipt = np.random.random(size=[10, 10]) + self.inputs = {'X': ipt.astype('float32')} + self.outputs = {'Out': ipt.astype('float64')} + self.attrs = { + 'in_data_type': int(core.DataType.FP32), + 'out_data_type': int(core.DataType.FP64) + } + self.op_type = 'cast' + + def test_check_output(self): + self.check_output() + + def test_grad(self): + self.check_grad(['X'], ['Out']) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/paddle/v2/framework/tests/test_cross_entropy_op.py b/python/paddle/v2/framework/tests/test_cross_entropy_op.py index 8b94539dcdf246959e39f825aafd1876f8af1723..b81af9364d63bc9b242372e71f175ad047d7c240 100644 --- a/python/paddle/v2/framework/tests/test_cross_entropy_op.py +++ b/python/paddle/v2/framework/tests/test_cross_entropy_op.py @@ -14,7 +14,7 @@ class TestCrossEntropyOp1(OpTest): X = randomize_probability(batch_size, class_num, dtype='float64') - label = np.random.randint(0, class_num, (batch_size, 1), dtype="int32") + label = np.random.randint(0, class_num, (batch_size, 1), dtype="int64") cross_entropy = np.asmatrix( [[-np.log(X[i][label[i][0]])] for i in range(X.shape[0])], dtype="float64") @@ -92,5 +92,4 @@ class TestCrossEntropyOp3(OpTest): if __name__ == "__main__": - exit(0) # Gradient operator has bug! unittest.main() diff --git a/python/paddle/v2/framework/tests/test_image_classification_layer.py b/python/paddle/v2/framework/tests/test_image_classification_layer.py new file mode 100644 index 0000000000000000000000000000000000000000..b4eda13552e60f009ec910e3d21e9d77107133a1 --- /dev/null +++ b/python/paddle/v2/framework/tests/test_image_classification_layer.py @@ -0,0 +1,98 @@ +import unittest + +import paddle.v2.framework.layers as layers +import paddle.v2.framework.nets as nets +from paddle.v2.framework.framework import Program + + +def conv_block(input, + num_filter, + groups, + dropouts, + program=None, + init_program=None): + return nets.img_conv_group( + input=input, + pool_size=2, + pool_stride=2, + conv_num_filter=[num_filter] * groups, + conv_filter_size=3, + conv_act='relu', + conv_with_batchnorm=True, + conv_batchnorm_drop_rate=dropouts, + pool_type='max', + program=program, + init_program=init_program) + + +class TestLayer(unittest.TestCase): + def test_batch_norm_layer(self): + program = Program() + init_program = Program() + images = layers.data( + name='pixel', + shape=[3, 48, 48], + data_type='float32', + program=program) + layers.batch_norm( + input=images, program=program, init_program=init_program) + + # print str(program) + + def test_dropout_layer(self): + program = Program() + init_program = Program() + images = layers.data( + name='pixel', + shape=[3, 48, 48], + data_type='float32', + program=program) + layers.dropout( + x=images, + dropout_prob=0.5, + program=program, + init_program=init_program) + + # print str(program) + + def test_img_conv_group(self): + program = Program() + init_program = Program() + + images = layers.data( + name='pixel', + shape=[3, 48, 48], + data_type='float32', + program=program, + init_program=init_program) + conv1 = conv_block(images, 64, 2, [0.3, 0], program, init_program) + conv2 = conv_block(conv1, 256, 3, [0.4, 0.4, 0], program, init_program) + + # print str(program) + + def test_elementwise_add_with_act(self): + program = Program() + init_program = Program() + image1 = layers.data( + name='pixel1', + shape=[3, 48, 48], + data_type='float32', + program=program, + init_program=init_program) + image2 = layers.data( + name='pixel2', + shape=[3, 48, 48], + data_type='float32', + program=program, + init_program=init_program) + out = layers.elementwise_add( + x=image1, + y=image2, + act='relu', + program=program, + init_program=init_program) + # print(program) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/paddle/v2/framework/tests/test_image_classification_train.py b/python/paddle/v2/framework/tests/test_image_classification_train.py new file mode 100644 index 0000000000000000000000000000000000000000..6b6dec4976d510fae7e987ad0276b049bbcb96fa --- /dev/null +++ b/python/paddle/v2/framework/tests/test_image_classification_train.py @@ -0,0 +1,255 @@ +import paddle.v2 as paddle +import paddle.v2.framework.layers as layers +import paddle.v2.framework.nets as nets +import paddle.v2.framework.core as core +import paddle.v2.framework.optimizer as optimizer + +from paddle.v2.framework.framework import Program, g_program +from paddle.v2.framework.executor import Executor + +import numpy as np + + +def resnet_cifar10(input, depth=32, program=None, init_program=None): + def conv_bn_layer(input, + ch_out, + filter_size, + stride, + padding, + act='relu', + program=None, + init_program=None): + tmp = layers.conv2d( + input=input, + filter_size=filter_size, + num_filters=ch_out, + stride=stride, + padding=padding, + act=None, + bias_attr=False, + program=program, + init_program=init_program) + return layers.batch_norm( + input=tmp, act=act, program=program, init_program=init_program) + + def shortcut(input, ch_in, ch_out, stride, program, init_program): + if ch_in != ch_out: + return conv_bn_layer(input, ch_out, 1, stride, 0, None, program, + init_program) + else: + return input + + def basicblock(input, + ch_in, + ch_out, + stride, + program=program, + init_program=init_program): + tmp = conv_bn_layer( + input, + ch_out, + 3, + stride, + 1, + program=program, + init_program=init_program) + tmp = conv_bn_layer( + tmp, + ch_out, + 3, + 1, + 1, + act=None, + program=program, + init_program=init_program) + short = shortcut(input, ch_in, ch_out, stride, program, init_program) + return layers.elementwise_add( + x=tmp, + y=short, + act='relu', + program=program, + init_program=init_program) + + def layer_warp(block_func, input, ch_in, ch_out, count, stride, program, + init_program): + tmp = block_func(input, ch_in, ch_out, stride, program, init_program) + for i in range(1, count): + tmp = block_func(tmp, ch_out, ch_out, 1, program, init_program) + return tmp + + assert (depth - 2) % 6 == 0 + n = (depth - 2) / 6 + conv1 = conv_bn_layer( + input=input, + ch_out=16, + filter_size=3, + stride=1, + padding=1, + program=program, + init_program=init_program) + res1 = layer_warp( + basicblock, + conv1, + 16, + 16, + n, + 1, + program=program, + init_program=init_program) + res2 = layer_warp( + basicblock, + res1, + 16, + 32, + n, + 2, + program=program, + init_program=init_program) + res3 = layer_warp( + basicblock, + res2, + 32, + 64, + n, + 2, + program=program, + init_program=init_program) + pool = layers.pool2d( + input=res3, + pool_size=8, + pool_type='avg', + pool_stride=1, + program=program, + init_program=init_program) + return pool + + +def vgg16_bn_drop(input, program, init_program): + def conv_block(input, + num_filter, + groups, + dropouts, + program=None, + init_program=None): + return nets.img_conv_group( + input=input, + pool_size=2, + pool_stride=2, + conv_num_filter=[num_filter] * groups, + conv_filter_size=3, + conv_act='relu', + conv_with_batchnorm=True, + conv_batchnorm_drop_rate=dropouts, + pool_type='max', + program=program, + init_program=init_program) + + conv1 = conv_block(input, 64, 2, [0.3, 0], program, init_program) + conv2 = conv_block(conv1, 128, 2, [0.4, 0], program, init_program) + conv3 = conv_block(conv2, 256, 3, [0.4, 0.4, 0], program, init_program) + conv4 = conv_block(conv3, 512, 3, [0.4, 0.4, 0], program, init_program) + conv5 = conv_block(conv4, 512, 3, [0.4, 0.4, 0], program, init_program) + + drop = layers.dropout( + x=conv5, dropout_prob=0.5, program=program, init_program=init_program) + fc1 = layers.fc(input=drop, + size=512, + act=None, + program=program, + init_program=init_program) + reshape1 = layers.reshape( + x=fc1, + shape=list(fc1.shape + (1, 1)), + program=program, + init_program=init_program) + bn = layers.batch_norm( + input=reshape1, act='relu', program=program, init_program=init_program) + drop2 = layers.dropout( + x=bn, dropout_prob=0.5, program=program, init_program=init_program) + fc2 = layers.fc(input=drop2, + size=512, + act=None, + program=program, + init_program=init_program) + return fc2 + + +init_program = Program() +program = Program() + +classdim = 10 +data_shape = [3, 32, 32] + +images = layers.data( + name='pixel', shape=data_shape, data_type='float32', program=program) + +label = layers.data( + name='label', + shape=[1], + data_type='int64', + program=program, + init_program=init_program) + +# Add neural network config +# option 1. resnet +net = resnet_cifar10(images, 32, program, init_program) +# option 2. vgg +# net = vgg16_bn_drop(images, program, init_program) + +# print(program) + +predict = layers.fc(input=net, + size=classdim, + act='softmax', + program=program, + init_program=init_program) +cost = layers.cross_entropy( + input=predict, label=label, program=program, init_program=init_program) +avg_cost = layers.mean(x=cost, program=program, init_program=init_program) + +sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.001) +opts = sgd_optimizer.minimize(avg_cost) + +BATCH_SIZE = 128 +PASS_NUM = 1 + +train_reader = paddle.batch( + paddle.reader.shuffle( + paddle.dataset.cifar.train10(), buf_size=128 * 10), + batch_size=BATCH_SIZE) + +place = core.CPUPlace() +exe = Executor(place) + +exe.run(init_program, feed={}, fetch_list=[]) + +for pass_id in range(PASS_NUM): + batch_id = 0 + for data in train_reader(): + img_data = np.array(map(lambda x: x[0].reshape(data_shape), + data)).astype("float32") + y_data = np.array(map(lambda x: x[1], data)).astype("int64") + batch_size = 1 + for i in y_data.shape: + batch_size = batch_size * i + y_data = y_data.reshape([batch_size, 1]) + + tensor_img = core.LoDTensor() + tensor_y = core.LoDTensor() + tensor_img.set(img_data, place) + tensor_y.set(y_data, place) + + outs = exe.run(program, + feed={"pixel": tensor_img, + "label": tensor_y}, + fetch_list=[avg_cost]) + + loss = np.array(outs[0]) + print("pass_id:" + str(pass_id) + " batch_id:" + str(batch_id) + + " loss:" + str(loss)) + batch_id = batch_id + 1 + + if batch_id > 1: + # this model is slow, so if we can train two mini batch, we think it works properly. + exit(0) +exit(1) diff --git a/python/paddle/v2/framework/tests/test_layers.py b/python/paddle/v2/framework/tests/test_layers.py index 54f8a0270de723ac5bfc2843653e6a8d3e66bf8a..5cbe790e3f019f5dcf6b201c4744e7502141ed99 100644 --- a/python/paddle/v2/framework/tests/test_layers.py +++ b/python/paddle/v2/framework/tests/test_layers.py @@ -93,15 +93,15 @@ class TestBook(unittest.TestCase): dict_size = 10000 embed_size = 32 first_word = layers.data( - name='firstw', shape=[1], data_type='int32', program=program) + name='firstw', shape=[1], data_type='int64', program=program) second_word = layers.data( - name='secondw', shape=[1], data_type='int32', program=program) + name='secondw', shape=[1], data_type='int64', program=program) third_word = layers.data( - name='thirdw', shape=[1], data_type='int32', program=program) + name='thirdw', shape=[1], data_type='int64', program=program) forth_word = layers.data( - name='forthw', shape=[1], data_type='int32', program=program) + name='forthw', shape=[1], data_type='int64', program=program) next_word = layers.data( - name='nextw', shape=[1], data_type='int32', program=program) + name='nextw', shape=[1], data_type='int64', program=program) embed_first = layers.embedding( input=first_word, diff --git a/python/paddle/v2/framework/tests/test_lookup_table_op.py b/python/paddle/v2/framework/tests/test_lookup_table_op.py index 2c48f9bf93b939aa631cd54e8fb14b5cba22f2e0..a56a549e69eaf950df39853a63947a8abac930d7 100644 --- a/python/paddle/v2/framework/tests/test_lookup_table_op.py +++ b/python/paddle/v2/framework/tests/test_lookup_table_op.py @@ -7,7 +7,7 @@ class TestLookupTableOp(OpTest): def setUp(self): self.op_type = "lookup_table" table = np.random.random((17, 31)).astype("float32") - ids = np.random.randint(0, 17, 4).astype("int32") + ids = np.random.randint(0, 17, 4).astype("int64") ids_expand = np.expand_dims(ids, axis=1) self.inputs = {'W': table, 'Ids': ids_expand} self.outputs = {'Out': table[ids]} diff --git a/python/paddle/v2/framework/tests/test_lstm_unit_op.py b/python/paddle/v2/framework/tests/test_lstm_unit_op.py index cf0e25f5eb267f6543f10c640a9bef177d6f915c..6bad2e1f7c34c51419424d88b41b809da997eb8f 100644 --- a/python/paddle/v2/framework/tests/test_lstm_unit_op.py +++ b/python/paddle/v2/framework/tests/test_lstm_unit_op.py @@ -34,6 +34,7 @@ class LstmUnitTest(OpTest): self.check_grad(['X', 'C_prev'], ['C', 'H']) -# TODO(gongwb):fix CI error -#if __name__ == "__main__": -# unittest.main() +if __name__ == "__main__": + # FIXME(qijun) https://github.com/PaddlePaddle/Paddle/issues/5185 + exit(0) + unittest.main() diff --git a/python/paddle/v2/framework/tests/test_modified_huber_loss_op.py b/python/paddle/v2/framework/tests/test_modified_huber_loss_op.py index bc8ee369d294af3a431e2bdf14a8646028a90161..33de8ff7219fafa1ddeb9ebd78d77ae4fa240c98 100644 --- a/python/paddle/v2/framework/tests/test_modified_huber_loss_op.py +++ b/python/paddle/v2/framework/tests/test_modified_huber_loss_op.py @@ -45,4 +45,6 @@ class TestModifiedHuberLossOp(OpTest): if __name__ == '__main__': + exit(0) + # FIXME(qijun) https://github.com/PaddlePaddle/Paddle/issues/5184 unittest.main() diff --git a/python/paddle/v2/framework/tests/test_nccl_init_op.py b/python/paddle/v2/framework/tests/test_nccl_init_op.py new file mode 100644 index 0000000000000000000000000000000000000000..054909fdf5517a68c6a07971c65a1d5bdc20d4fa --- /dev/null +++ b/python/paddle/v2/framework/tests/test_nccl_init_op.py @@ -0,0 +1,39 @@ +import unittest, os +import numpy as np +import paddle.v2 as paddle +from paddle.v2.framework.op import Operator +import paddle.v2.framework.core as core +from op_test import OpTest, create_op, set_input + +if not core.is_compile_gpu(): + exit(0) + +gpu_count = core.get_cuda_device_count() + +if gpu_count <= 1: + exit(0) + +g_scope = core.Scope() +g_ctx = core.DeviceContext.create(core.CPUPlace()) + + +class TestNCCLInit(unittest.TestCase): + def test_init(self): + self.op_type = "ncclInit" + self.gpus = range(gpu_count) + + self.inputs = {} + self.attrs = {"gpus": self.gpus} + g_scope.var("Communicator").get_communicator() + self.outputs = {"Communicator": g_scope.find_var("Communicator")} + nccl_init = create_op( + g_scope, + op_type=self.op_type, + inputs=self.inputs, + outputs=self.outputs, + attrs=self.attrs) + nccl_init.run(g_scope, g_ctx) + + +if __name__ == "__main__": + unittest.main() diff --git a/python/paddle/v2/framework/tests/test_pool2d_op.py b/python/paddle/v2/framework/tests/test_pool2d_op.py index f04de8133ad3b747d03500a1498b1516c21479b8..c93469e11994c44ee6fbd1a8828074c1558c08fa 100644 --- a/python/paddle/v2/framework/tests/test_pool2d_op.py +++ b/python/paddle/v2/framework/tests/test_pool2d_op.py @@ -49,9 +49,12 @@ class TestPool2d_Op(OpTest): self.init_test_case() self.init_op_type() self.init_pool_type() + if self.global_pool: + self.paddings = [0 for _ in range(len(self.paddings))] input = np.random.random(self.shape).astype("float32") output = self.pool2D_forward_naive(input, self.ksize, self.strides, - self.paddings, self.global_pool) + self.paddings, + self.global_pool).astype("float32") self.inputs = {'X': input} self.attrs = { diff --git a/python/paddle/v2/framework/tests/test_pool3d_op.py b/python/paddle/v2/framework/tests/test_pool3d_op.py index d62fbee9746c5524cb8c428df584d2b76cf67bc9..416f0df7cd27f58c4c99fb776b84e44005f31639 100644 --- a/python/paddle/v2/framework/tests/test_pool3d_op.py +++ b/python/paddle/v2/framework/tests/test_pool3d_op.py @@ -54,10 +54,13 @@ def avg_pool3D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): class TestPool3d_Op(OpTest): def setUp(self): - self.initTestCase() + self.init_test_case() + if self.global_pool: + self.paddings = [0 for _ in range(len(self.paddings))] input = np.random.random(self.shape).astype("float32") output = self.pool3D_forward_naive(input, self.ksize, self.strides, - self.paddings, self.global_pool) + self.paddings, + self.global_pool).astype("float32") self.inputs = {'X': input} self.attrs = { @@ -77,7 +80,7 @@ class TestPool3d_Op(OpTest): if self.pool_type != "max": self.check_grad(set(['X']), 'Out', max_relative_error=0.07) - def initTestCase(self): + def init_test_case(self): self.global_pool = True self.op_type = "pool3d" self.pool_type = "avg" @@ -89,7 +92,7 @@ class TestPool3d_Op(OpTest): class TestCase1(TestPool3d_Op): - def initTestCase(self): + def init_test_case(self): self.global_pool = False self.op_type = "pool3d" self.pool_type = "avg" @@ -101,7 +104,7 @@ class TestCase1(TestPool3d_Op): class TestCase2(TestPool3d_Op): - def initTestCase(self): + def init_test_case(self): self.global_pool = False self.op_type = "pool3d" self.pool_type = "avg" @@ -113,7 +116,7 @@ class TestCase2(TestPool3d_Op): class TestCase3(TestPool3d_Op): - def initTestCase(self): + def init_test_case(self): self.global_pool = True self.op_type = "pool3d" self.pool_type = "max" @@ -125,7 +128,7 @@ class TestCase3(TestPool3d_Op): class TestCase4(TestPool3d_Op): - def initTestCase(self): + def init_test_case(self): self.global_pool = False self.op_type = "pool3d" self.pool_type = "max" @@ -137,7 +140,7 @@ class TestCase4(TestPool3d_Op): class TestCase5(TestPool3d_Op): - def initTestCase(self): + def init_test_case(self): self.global_pool = False self.op_type = "pool3d" self.pool_type = "max" diff --git a/python/paddle/v2/framework/tests/test_pool_max_op.py b/python/paddle/v2/framework/tests/test_pool_max_op.py index f0f8aa6089c74d31702a6a5d37362099205d96b2..cc1a867761142edea506a24e84ad31bfe6858fb0 100644 --- a/python/paddle/v2/framework/tests/test_pool_max_op.py +++ b/python/paddle/v2/framework/tests/test_pool_max_op.py @@ -3,11 +3,7 @@ import numpy as np from op_test import OpTest -def max_pool3D_forward_naive(x, - ksize, - strides, - paddings=[0, 0, 0], - global_pool=0): +def max_pool3D_forward_naive(x, ksize, strides, paddings, global_pool=0): N, C, D, H, W = x.shape if global_pool == 1: @@ -44,7 +40,7 @@ def max_pool3D_forward_naive(x, return out, mask -def max_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): +def max_pool2D_forward_naive(x, ksize, strides, paddings, global_pool=0): N, C, H, W = x.shape if global_pool == 1: @@ -77,10 +73,14 @@ def max_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): class TestMaxPoolWithIndex_Op(OpTest): def setUp(self): - self.initTestCase() + self.init_test_case() + if self.global_pool: + self.paddings = [0 for _ in range(len(self.paddings))] input = np.random.random(self.shape).astype("float32") output, mask = self.pool_forward_naive(input, self.ksize, self.strides, self.paddings, self.global_pool) + output = output.astype("float32") + mask = mask.astype("float32") self.attrs = { 'strides': self.strides, @@ -98,7 +98,7 @@ class TestMaxPoolWithIndex_Op(OpTest): # def test_check_grad(self): # self.check_grad(set(['X']), ['Out'], max_relative_error=0.07) - def initTestCase(self): + def init_test_case(self): self.global_pool = True self.index = "max_pool3d_with_index" self.op_type = "%s" % self.index @@ -110,7 +110,7 @@ class TestMaxPoolWithIndex_Op(OpTest): class TestCase1(TestMaxPoolWithIndex_Op): - def initTestCase(self): + def init_test_case(self): self.global_pool = True self.op_type = "max_pool3d_with_index" self.pool_forward_naive = max_pool3D_forward_naive @@ -121,7 +121,7 @@ class TestCase1(TestMaxPoolWithIndex_Op): class TestCase2(TestMaxPoolWithIndex_Op): - def initTestCase(self): + def init_test_case(self): self.global_pool = False self.op_type = "max_pool3d_with_index" self.pool_forward_naive = max_pool3D_forward_naive @@ -132,7 +132,7 @@ class TestCase2(TestMaxPoolWithIndex_Op): class TestCase3(TestMaxPoolWithIndex_Op): - def initTestCase(self): + def init_test_case(self): self.global_pool = False self.op_type = "max_pool3d_with_index" self.pool_forward_naive = max_pool3D_forward_naive @@ -143,7 +143,7 @@ class TestCase3(TestMaxPoolWithIndex_Op): class TestCase4(TestMaxPoolWithIndex_Op): - def initTestCase(self): + def init_test_case(self): self.global_pool = True self.op_type = "max_pool3d_with_index" self.pool_forward_naive = max_pool3D_forward_naive @@ -154,7 +154,7 @@ class TestCase4(TestMaxPoolWithIndex_Op): class TestCase5(TestMaxPoolWithIndex_Op): - def initTestCase(self): + def init_test_case(self): self.global_pool = True self.op_type = "max_pool3d_with_index" self.pool_forward_naive = max_pool3D_forward_naive @@ -165,7 +165,7 @@ class TestCase5(TestMaxPoolWithIndex_Op): class TestCase6(TestMaxPoolWithIndex_Op): - def initTestCase(self): + def init_test_case(self): self.global_pool = False self.op_type = "max_pool2d_with_index" self.pool_forward_naive = max_pool2D_forward_naive @@ -176,7 +176,7 @@ class TestCase6(TestMaxPoolWithIndex_Op): class TestCase7(TestMaxPoolWithIndex_Op): - def initTestCase(self): + def init_test_case(self): self.global_pool = False self.op_type = "max_pool2d_with_index" self.pool_forward_naive = max_pool2D_forward_naive @@ -187,7 +187,7 @@ class TestCase7(TestMaxPoolWithIndex_Op): class TestCase8(TestMaxPoolWithIndex_Op): - def initTestCase(self): + def init_test_case(self): self.global_pool = True self.op_type = "max_pool2d_with_index" self.pool_forward_naive = max_pool2D_forward_naive @@ -198,7 +198,7 @@ class TestCase8(TestMaxPoolWithIndex_Op): class TestCase9(TestMaxPoolWithIndex_Op): - def initTestCase(self): + def init_test_case(self): self.global_pool = True self.op_type = "max_pool2d_with_index" self.pool_forward_naive = max_pool2D_forward_naive diff --git a/python/paddle/v2/framework/tests/test_recognize_digits_conv.py b/python/paddle/v2/framework/tests/test_recognize_digits_conv.py index 2b305213df424dd097bf4238aa14320a2f7da45d..92b1d0542619b765cc32c98f59604cfc73d7d6d4 100644 --- a/python/paddle/v2/framework/tests/test_recognize_digits_conv.py +++ b/python/paddle/v2/framework/tests/test_recognize_digits_conv.py @@ -21,7 +21,7 @@ images = layers.data( label = layers.data( name='label', shape=[1], - data_type='int32', + data_type='int64', program=program, init_program=init_program) conv_pool_1 = nets.simple_img_conv_pool( @@ -51,12 +51,14 @@ predict = layers.fc(input=conv_pool_2, cost = layers.cross_entropy( input=predict, label=label, program=program, init_program=init_program) avg_cost = layers.mean(x=cost, program=program) +accuracy = layers.accuracy( + input=predict, label=label, program=program, init_program=init_program) sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.001) opts = sgd_optimizer.minimize(avg_cost) BATCH_SIZE = 50 -PASS_NUM = 1 +PASS_NUM = 3 train_reader = paddle.batch( paddle.reader.shuffle( paddle.dataset.mnist.train(), buf_size=500), @@ -72,7 +74,7 @@ for pass_id in range(PASS_NUM): for data in train_reader(): img_data = np.array(map(lambda x: x[0].reshape([1, 28, 28]), data)).astype("float32") - y_data = np.array(map(lambda x: x[1], data)).astype("int32") + y_data = np.array(map(lambda x: x[1], data)).astype("int64") y_data = y_data.reshape([BATCH_SIZE, 1]) tensor_img = core.LoDTensor() @@ -83,10 +85,11 @@ for pass_id in range(PASS_NUM): outs = exe.run(program, feed={"pixel": tensor_img, "label": tensor_y}, - fetch_list=[avg_cost]) - + fetch_list=[avg_cost, accuracy]) loss = np.array(outs[0]) + acc = np.array(outs[1]) - if loss < 10.0: - exit(0) # if avg cost less than 10.0, we think our code is good. + if loss < 10.0 and acc > 0.9: + # if avg cost less than 10.0 and accuracy is larger than 0.9, we think our code is good. + exit(0) exit(1) diff --git a/python/paddle/v2/framework/tests/test_recognize_digits_mlp.py b/python/paddle/v2/framework/tests/test_recognize_digits_mlp.py index 44a768d5e2274c08c3b90b29b564549c11fb1105..a8a34b2a952c8d374089ab8142b530610b2afe59 100644 --- a/python/paddle/v2/framework/tests/test_recognize_digits_mlp.py +++ b/python/paddle/v2/framework/tests/test_recognize_digits_mlp.py @@ -52,7 +52,7 @@ predict = layers.fc(input=hidden2, label = layers.data( name='y', shape=[1], - data_type='int32', + data_type='int64', program=program, init_program=init_program) @@ -77,7 +77,7 @@ PASS_NUM = 100 for pass_id in range(PASS_NUM): for data in train_reader(): x_data = np.array(map(lambda x: x[0], data)).astype("float32") - y_data = np.array(map(lambda x: x[1], data)).astype("int32") + y_data = np.array(map(lambda x: x[1], data)).astype("int64") y_data = np.expand_dims(y_data, axis=1) tensor_x = core.LoDTensor() diff --git a/python/paddle/v2/framework/tests/test_seq_conv.py b/python/paddle/v2/framework/tests/test_seq_conv.py index f0337c20a9e87fab971f9d9e2a113346feb20957..14edc5f953022ca05f5620c28bd7276d961dd4d0 100644 --- a/python/paddle/v2/framework/tests/test_seq_conv.py +++ b/python/paddle/v2/framework/tests/test_seq_conv.py @@ -45,10 +45,10 @@ class TestSeqProject(OpTest): self.inputs_val_no_f = ['PaddingData', 'X'] self.attrs = { - 'context_start': self.context_start, - 'context_length': self.context_length, - 'padding_trainable': self.padding_trainable, - 'context_stride': self.context_stride + 'contextStart': self.context_start, + 'contextLength': self.context_length, + 'paddingTrainable': self.padding_trainable, + 'contextStride': self.context_stride } out = np.zeros( (self.input_size[0], self.output_represention)).astype('float32') diff --git a/python/paddle/v2/framework/tests/test_seq_expand.py b/python/paddle/v2/framework/tests/test_seq_expand.py new file mode 100644 index 0000000000000000000000000000000000000000..ff17edd04bfd34ab8449a0ae05aacf66632dabc8 --- /dev/null +++ b/python/paddle/v2/framework/tests/test_seq_expand.py @@ -0,0 +1,63 @@ +import unittest +import numpy as np +from op_test import OpTest + + +class TestSeqExpand(OpTest): + def set_data(self): + x_data = np.random.uniform(0.1, 1, [3, 1]).astype('float32') + y_data = np.random.uniform(0.1, 1, [8, 1]).astype('float32') + y_lod = [[0, 1, 4, 8]] + self.inputs = {'X': x_data, 'Y': (y_data, y_lod)} + + def compute(self): + x = self.inputs['X'] + x_data, x_lod = x if type(x) == tuple else (x, None) + n = 1 + x_data.shape[0] if not x_lod else len(x_lod[0]) + y_data, y_lod = self.inputs['Y'] + repeats = [((y_lod[-1][i + 1] - y_lod[-1][i])) + for i in range(len(y_lod[-1]) - 1)] + out = x_data.repeat(repeats, axis=0) + self.outputs = {'Out': out} + + def setUp(self): + self.op_type = 'seq_expand' + self.set_data() + self.compute() + + def test_check_output(self): + self.check_output() + + def test_check_grad(self): + self.check_grad(["X"], "Out") + + +class TestSeqExpandCase1(TestSeqExpand): + def set_data(self): + x_data = np.random.uniform(0.1, 1, [5, 1]).astype('float32') + x_lod = [[0, 2, 5]] + y_data = np.random.uniform(0.1, 1, [13, 1]).astype('float32') + y_lod = [[0, 2, 5], [0, 2, 4, 7, 10, 13]] + self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)} + + +class TestSeqExpandCase2(TestSeqExpand): + def set_data(self): + x_data = np.random.uniform(0.1, 1, [1, 2, 2]).astype('float32') + x_lod = [[0, 1]] + y_data = np.random.uniform(0.1, 1, [2, 2, 2]).astype('float32') + y_lod = [[0, 2]] + self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)} + + +class TestSeqExpandCase3(TestSeqExpand): + def set_data(self): + x_data = np.random.uniform(0.1, 1, [4, 1]).astype('float32') + x_lod = [[0, 1, 2, 3, 4]] + y_data = np.random.uniform(0.1, 1, [6, 1]).astype('float32') + y_lod = [[0, 2, 4, 4, 6]] + self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)} + + +if __name__ == '__main__': + unittest.main() diff --git a/python/paddle/v2/framework/tests/test_top_k_op.py b/python/paddle/v2/framework/tests/test_top_k_op.py index 694f37d612d4c46e673dc894b05a0a446190732c..6e8fbefa6eafa391cdb5e17c882ee74b5bdc6507 100644 --- a/python/paddle/v2/framework/tests/test_top_k_op.py +++ b/python/paddle/v2/framework/tests/test_top_k_op.py @@ -9,7 +9,7 @@ class TestTopkOp(OpTest): k = 1 input = np.random.random((32, 84)).astype("float32") output = np.ndarray((32, k)) - indices = np.ndarray((32, k)) + indices = np.ndarray((32, k)).astype("int64") self.inputs = {'X': input} self.attrs = {'k': k} @@ -32,7 +32,7 @@ class TestTopkOp3d(OpTest): input = np.random.random((32, 2, 84)).astype("float32") input_flat_2d = input.reshape(64, 84) output = np.ndarray((64, k)) - indices = np.ndarray((64, k)).astype("int") + indices = np.ndarray((64, k)).astype("int64") # FIXME: should use 'X': input for a 3d input self.inputs = {'X': input_flat_2d} diff --git a/python/paddle/v2/framework/tests/test_word2vec.py b/python/paddle/v2/framework/tests/test_word2vec.py index f5e61bef0d8c0fafde0cebdb913a08a41559a171..515d30d3e23edf429304d796faa8e17532168e26 100644 --- a/python/paddle/v2/framework/tests/test_word2vec.py +++ b/python/paddle/v2/framework/tests/test_word2vec.py @@ -15,6 +15,7 @@ embed_size = 32 hidden_size = 256 N = 5 batch_size = 32 +is_sparse = True word_dict = paddle.dataset.imikolov.build_dict() dict_size = len(word_dict) @@ -22,31 +23,31 @@ dict_size = len(word_dict) first_word = layers.data( name='firstw', shape=[1], - data_type='int32', + data_type='int64', program=program, init_program=init_program) second_word = layers.data( name='secondw', shape=[1], - data_type='int32', + data_type='int64', program=program, init_program=init_program) third_word = layers.data( name='thirdw', shape=[1], - data_type='int32', + data_type='int64', program=program, init_program=init_program) forth_word = layers.data( name='forthw', shape=[1], - data_type='int32', + data_type='int64', program=program, init_program=init_program) next_word = layers.data( name='nextw', shape=[1], - data_type='int32', + data_type='int64', program=program, init_program=init_program) @@ -54,6 +55,7 @@ embed_first = layers.embedding( input=first_word, size=[dict_size, embed_size], data_type='float32', + is_sparse=is_sparse, param_attr={'name': 'shared_w'}, program=program, init_program=init_program) @@ -61,6 +63,7 @@ embed_second = layers.embedding( input=second_word, size=[dict_size, embed_size], data_type='float32', + is_sparse=is_sparse, param_attr={'name': 'shared_w'}, program=program, init_program=init_program) @@ -69,6 +72,7 @@ embed_third = layers.embedding( input=third_word, size=[dict_size, embed_size], data_type='float32', + is_sparse=is_sparse, param_attr={'name': 'shared_w'}, program=program, init_program=init_program) @@ -76,6 +80,7 @@ embed_forth = layers.embedding( input=forth_word, size=[dict_size, embed_size], data_type='float32', + is_sparse=is_sparse, param_attr={'name': 'shared_w'}, program=program, init_program=init_program) @@ -117,26 +122,26 @@ PASS_NUM = 100 for pass_id in range(PASS_NUM): for data in train_reader(): input_data = [[data_idx[idx] for data_idx in data] for idx in xrange(5)] - input_data = map(lambda x: np.array(x).astype("int32"), input_data) + input_data = map(lambda x: np.array(x).astype("int64"), input_data) input_data = map(lambda x: np.expand_dims(x, axis=1), input_data) first_data = input_data[0] first_tensor = core.LoDTensor() first_tensor.set(first_data, place) - second_data = input_data[0] + second_data = input_data[1] second_tensor = core.LoDTensor() second_tensor.set(second_data, place) - third_data = input_data[0] + third_data = input_data[2] third_tensor = core.LoDTensor() third_tensor.set(third_data, place) - forth_data = input_data[0] + forth_data = input_data[3] forth_tensor = core.LoDTensor() forth_tensor.set(forth_data, place) - next_data = input_data[0] + next_data = input_data[4] next_tensor = core.LoDTensor() next_tensor.set(next_data, place) diff --git a/python/paddle/v2/trainer.py b/python/paddle/v2/trainer.py index b68fd0d5a97a7993ddd0a1d947304fa5428c01b8..db01ab7374eca18b6063dc634da5ef83c4bc9adc 100644 --- a/python/paddle/v2/trainer.py +++ b/python/paddle/v2/trainer.py @@ -205,7 +205,8 @@ class SGD(object): """ Testing method. Will test input data. - :param reader: A reader that reads and yeilds data items. + :param reader: A batch reader that reads and yeilds data items, + it should be a paddle.v2.batch. :type reader: collections.Iterable :param feeding: Feeding is a map of neural network input name and array index that reader returns.