Skip to content

How to run opertator's python unit test quickly.#4059

Closed
gongweibao wants to merge 3 commits intoPaddlePaddle:developfrom
gongweibao:quicktest
Closed

How to run opertator's python unit test quickly.#4059
gongweibao wants to merge 3 commits intoPaddlePaddle:developfrom
gongweibao:quicktest

Conversation

@gongweibao
Copy link
Contributor

No description provided.

Copy link
Contributor

@lcy-seso lcy-seso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一些过于口语化的地方应该去掉。

`python/paddle/v2/framework/tests` 目录下新增的 `test_*.py` 单元测试会被自动加���工程进行编译。

请注意,**不同于Op的编译测试,运行单元测试测时需要编译整个工程**,并且编译时需要打开`WITH_TESTING`, 即`cmake paddle_dir -DWITH_TESTING=ON`。编译成功后,执行下面的命令来运行单元测试:
我们在调试的时候,其实不需要编译整体的工程(编译整体是在太耗时间了),也可以做到快速单元调试的效果。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“我们在调试的时候,其实不需要编译整体的工程(编译整体是在太耗时间了),也可以做到快速单元调试的效果。” 这句话太过口语了。

我们在调试时,可以不编译整个工程而只对新加入的功能模型进行单元测试以及调试。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我感觉第一次整体编译还是需要的?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实也不需要。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

模型-->模块


请注意,**不同于Op的编译测试,运行单元测试测时需要编译整个工程**,并且编译时需要打开`WITH_TESTING`, 即`cmake paddle_dir -DWITH_TESTING=ON`。编译成功后,执行下面的命令来运行单元测试:
我们在调试的时候,其实不需要编译整体的工程(编译整体是在太耗时间了),也可以做到快速单元调试的效果。
CMake中定义的`target`是可以单���进行编译的。另外,我们在编译的过程中,已经把python需要的依赖打包到了`core.so`中,并且拷贝到了源代码目录下,只需要吧`PYTHONPATH`指向我们的源代码对应的目录,不需要安装就可以进行测试。
Copy link
Contributor

@lcy-seso lcy-seso Sep 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • CMake中定义的target可以单独进行编译。
  • 编译的过程中已经将python所需的依赖打包进core.so中,并且拷贝到源代码目录下,因此,只需要把PYTHONPATH指向我们的新增的源代码对应的目录,无需要安装即可进行测试。

一个小意见,

  • 这个文档未来是可以作为给更多开发者的开发指南,core.so 这个文件在文档里面突然出现,上文从未解释过。这里还是稍作说明,给一个具体的路径。
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

```

或者:
*注意:如果你不是在docker环境中进行开发,`/paddle`的路径需要设置为你开发的实际路径*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果不是在docker环境中进行开发

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


```bash
ctest -R test_mul_op
如果需要运行的是C的程序,一样的道理,例如`gather_test`,执行
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同样道理,如果需要运行的是可执行程序,如gather_test,执行:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


如果需要调试则执行`gdb gather_test`

注意:*如果您用的是docker环境,启动continer的时候需要加入`--security-opt seccomp=unconfined`参数,否则不能进行gdb调试*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果使用的是docker环境,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

`python/paddle/v2/framework/tests` 目录下新增的 `test_*.py` 单元测试会被自动加入工程进行编译。

请注意,**不同于Op的编译测试,运行单元测试测时需要编译整个工程**,并且编译时需要打开`WITH_TESTING`, 即`cmake paddle_dir -DWITH_TESTING=ON`。编译成功后,执行下面的命令来运行单元测试:
我们在调试的时候,其实不需要编译整体的工程(编译整体是在太耗时间了),也可以做到快速单元调试的效果。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原来的那段可以留着么?原来那段虽然可能慢点,但是命令少。
现在这段那么长,小白用户加单测会被吓到的。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我也认为应该保留。大家自己来选择。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

编译整体的工程(编译整体是在太耗时间了):是在-》实在,
另外,不觉得编译整体非常耗时间,paddle的编译速度还是挺快的。有没有时间对比呢?

Copy link
Contributor Author

@gongweibao gongweibao Sep 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我修改elementwise_mul_op.h,然后进行编译:
使用make -j 12进行编译,第一次耗时4m6s,第二次(没有修改任何东西)耗时3m56s。
使用make copy_paddle_bind -j 12,第一次耗时5.6s,第二次(没有修改任何东西)耗时1.2s。

整体make的时间都花在link和生成不需要的文件上,并没有多少时间花在编译上。

- 执行python unit test文件

```
export PYTHONPATH=$PYTHONPATH:/paddle/python
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个目前是不需要的,可以直接

make
ctest -R test_xxx.py 

即可,无需安装,也无需指定环境变量

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

两个commit的名字都是add,可以更详细一点。

-DCMAKE_BUILD_TYPE=Debug
```

*解释*:CMAKE_BUILD_TYPE参数可以让CMake编译的时候加入`-g`参数,以便进行gdb的调试。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里要重新生成cmake么?那这段是不是应该挪到文档的最开头?

  • 对小白用户,这段生成Makefile的太长了,而且都有指定路径,比如cudnn。因为用户会直接复制过来跑。
  • 对高级用户,只要告诉他“CMAKE_BUILD_TYPE参数可以让CMake编译的时候加入-g参数,以便进行gdb的调试”是不是就够了?另外,没看到-g参数在哪里加。
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

`python/paddle/v2/framework/tests` 目录下新增的 `test_*.py` 单元测试会被自动加入工程进行编译。

请注意,**不同于Op的编译测试,运行单元测试测时需要编译整个工程**,并且编译时需要打开`WITH_TESTING`, 即`cmake paddle_dir -DWITH_TESTING=ON`。编译成功后,执行下面的命令来运行单元测试:
我们在调试的时候,其实不需要编译整体的工程(编译整体是在太耗时间了),也可以做到快速单元调试的效果。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

第一次整体编译还是需要的,后边如果只改动一小部分代码,编译其实也不算慢。因为如果单独编译core.so的话,其实系统大部分代码还是会编译,和整体编译没有太大区别。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里已经回复:
#4059 (comment)


请注意,**不同于Op的编译测试,运行单元测试测时需要编译整个工程**,并且编译时需要打开`WITH_TESTING`, 即`cmake paddle_dir -DWITH_TESTING=ON`。编译成功后,执行下面的命令来运行单元测试:
我们在调试的时候,其实不需要编译整体的工程(编译整体是在太耗时间了),也可以做到快速单元调试的效果。
CMake中定义的`target`是可以单独进行编译的。另外,我们在编译的过程中,已经把python需要的依赖打包到了`core.so`中,并且拷贝到了源代码目录下,只需要吧`PYTHONPATH`指向我们的源代码对应的目录,不需要安装就可以进行测试。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不推荐使用pythonpath指定源代码目录的方式,因为有些python 库可能不完整,用ctest的话,一定是一个完整的python环境。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

@jacquesqiao jacquesqiao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因为这个pr涉及到单测的一部分东西,感觉不太符合当前的设计,所以希望等完善之后再merge。

@jacquesqiao
Copy link
Member

jacquesqiao commented Sep 13, 2017

现状

目前测试python单测的步骤是比较简单的。以 test_operator.py为例子:

  1. make
  2. ctest -R test_operator

无需用户安装,也无需指定环境变量。

性能

  1. 如果每次单测需要重新make core.so的话,所有涉及到的cpp代码都会重新编译,不会比make快。
  2. 指定环境变量的话,用户多了好几步操作。

我的感觉是这个修改没带来好处,可能还需要再完善下。

@luotao1
Copy link
Contributor

luotao1 commented Sep 13, 2017

修改CMakeList,加入 py_test(test_operator SRCS test_operator.py)

#4044 已经merge了,目前这一步也不需要 @jacquesqiao

@pkuyym
Copy link
Contributor

pkuyym commented Sep 13, 2017

@jacquesqiao
make --> ctest -R test_operator还是比较慢的,很长时间卡在编译wheel上

Copy link
Contributor Author

@gongweibao gongweibao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我修改elementwise_mul_op.h,然后进行编译:
使用make -j 12进行编译,第一次耗时4m6s,第二次(没有修改任何东西)耗时3m56s。
使用make copy_paddle_bind -j 12,第一次耗时5.6s,第二次耗时1.2s。

整体make的时间都花在link和生成不需要的文件上,并没有多少时间花在编译上。

@jacquesqiao

Copy link
Contributor

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pkuyym

很长时间卡在编译wheel上

这个可以作为一个单独的issue提出。

@gongweibao

使用make -j 12进行编译,第一次耗时4m6s,第二次(没有修改任何东西)耗时3m56s。

我是在开发机上测的,没有修改任何东西的时候,耗时1分钟15s,其中大概1分钟花在wheel上面。

使用make copy_paddle_bind -j 12,第一次耗时5.6s,第二次耗时1.2s。

这里笔误,应该是make copy_paddle_pybind

所以是不是运行单元测试只要两步?:

make copy_paddle_pybind -j12
ctest -R test_mul_op

如果只需要加一个make copy_paddle_pybind的命令,可以加在原来的地方吧。 @jacquesqiao


注意:*如果您用的是docker环境,启动continer的时候需要加入`--security-opt seccomp=unconfined`参数,否则不能进行gdb调试*
- 只编译新功能相关的部分
- opertator的python单测依赖的是`/paddle/python/paddle/v2/framework/core.so`, 添加或者修改operator的C源代码后,用`make copy_paddle_pybind`即可更新该文件,不用编译整个工程,即可使用如`ctest -R test_mul_op`运行单测。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opertator笔误。

如果需要pdb调试,如`pdb test_xxx.py`, 可以尝试修改`PYTHONPATH` 如`export PYTHONPATH=$PYTHONPATH:/paddle/python`。
*注意:这种办法由于可能库的不完整导致错误的发生。*
- 调试C单测:
cmake需要增加参数`-DCMAKE_BUILD_TYPE=Debug`,同时如果使用的是docker环境,启动continer的时候需要加入`--security-opt seccomp=unconfined`,然后执行如`gdb gather_test`。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

整段需要再组织下,python单测和C单测所在的目录都不一样。而且文章前面也没说要写C单测,这儿突然说有C单测,有点迷惑。

- 如果需要的是C的可单测程序,如`gather_test`,可以执行`make gather_test`。
- python调试:
如果需要pdb调试,如`pdb test_xxx.py`, 可以尝试修改`PYTHONPATH` 如`export PYTHONPATH=$PYTHONPATH:/paddle/python`。
*注意:这种办法由于可能库的不完整导致错误的发生。*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个pdb调试,如果有错误的发生,是不是不建议推荐给读者呢?

@gongweibao gongweibao closed this Apr 8, 2018
@gongweibao gongweibao deleted the quicktest branch January 17, 2021 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

5 participants