From 1f121e7c655a5a9651585358c6764f9c64868fcc Mon Sep 17 00:00:00 2001 From: furnace <34057289+windstamp@users.noreply.github.com> Date: Fri, 6 May 2022 14:09:47 +0800 Subject: [PATCH 1/2] Add custom device en docs (#4706) * Add custom device en docs * Update index_en.rst * Update index_en.rst * Fix _cn to _en, and ##### to short * optimieze Co-authored-by: Chen Long <1300851984@qq.com> --- .../custom_device_example_cn.md | 1 - .../custom_device_example_en.md | 461 ++++++++++++++++++ .../custom_kernel_docs/context_api_en.md | 150 ++++++ .../custom_kernel_docs/cpp_api_en.rst | 20 + .../custom_kernel_docs/exception_api_en.md | 62 +++ .../custom_kernel_docs/kernel_declare_en.md | 83 ++++ .../custom_kernel_docs/register_api_en.md | 62 +++ .../custom_kernel_docs/tensor_api_en.md | 190 ++++++++ .../custom_device_docs/custom_kernel_en.rst | 19 + .../custom_device_docs/custom_runtime_en.rst | 144 ++++++ .../custom_device_docs/device_api_en.md | 185 +++++++ .../custom_device_docs/event_api_en.md | 93 ++++ .../custom_device_docs/index_en.rst | 19 + .../custom_device_docs/memory_api_en.md | 459 +++++++++++++++++ .../runtime_data_type_en.md | 131 +++++ .../custom_device_docs/stream_api_en.md | 115 +++++ docs/dev_guides/index_en.rst | 11 +- docs/index_en.rst | 1 + 18 files changed, 2198 insertions(+), 8 deletions(-) create mode 100644 docs/dev_guides/custom_device_docs/custom_device_example_en.md create mode 100644 docs/dev_guides/custom_device_docs/custom_kernel_docs/context_api_en.md create mode 100644 docs/dev_guides/custom_device_docs/custom_kernel_docs/cpp_api_en.rst create mode 100644 docs/dev_guides/custom_device_docs/custom_kernel_docs/exception_api_en.md create mode 100644 docs/dev_guides/custom_device_docs/custom_kernel_docs/kernel_declare_en.md create mode 100644 docs/dev_guides/custom_device_docs/custom_kernel_docs/register_api_en.md create mode 100644 docs/dev_guides/custom_device_docs/custom_kernel_docs/tensor_api_en.md create mode 100644 docs/dev_guides/custom_device_docs/custom_kernel_en.rst create mode 100644 docs/dev_guides/custom_device_docs/custom_runtime_en.rst create mode 100644 docs/dev_guides/custom_device_docs/device_api_en.md create mode 100644 docs/dev_guides/custom_device_docs/event_api_en.md create mode 100644 docs/dev_guides/custom_device_docs/index_en.rst create mode 100644 docs/dev_guides/custom_device_docs/memory_api_en.md create mode 100644 docs/dev_guides/custom_device_docs/runtime_data_type_en.md create mode 100644 docs/dev_guides/custom_device_docs/stream_api_en.md diff --git a/docs/dev_guides/custom_device_docs/custom_device_example_cn.md b/docs/dev_guides/custom_device_docs/custom_device_example_cn.md index 72e79a74f58..9d064bd3702 100644 --- a/docs/dev_guides/custom_device_docs/custom_device_example_cn.md +++ b/docs/dev_guides/custom_device_docs/custom_device_example_cn.md @@ -5,7 +5,6 @@ > 注意: > - 请确保已经正确安装了[飞桨develop](https://github.com/PaddlePaddle/Paddle)最新版本 > - 当前仅支持 `Linux`平台,示例中使用X86_64平台 -> - 支持飞桨已通过头文件开放函数式声明的Kernel自定义编码与注册 ## 第一步:实现自定义 Runtime diff --git a/docs/dev_guides/custom_device_docs/custom_device_example_en.md b/docs/dev_guides/custom_device_docs/custom_device_example_en.md new file mode 100644 index 00000000000..864d2b036aa --- /dev/null +++ b/docs/dev_guides/custom_device_docs/custom_device_example_en.md @@ -0,0 +1,461 @@ +# CustomDevice Example + +In this section we will walk through the steps required to extend a fake hardware backend for PaddlePaddle by implementing a fake device named CustomCPU. + +> Note: +> - Please make sure that you have correctly installed the latest version of [Paddle develop](https://github.com/PaddlePaddle/Paddle). +> - Only `Linux` is supported + +## Step One: Implement Custom Runtime + +**InitPlugin** + +As a custom runtime entry function, InitPlugin is required to be implemented by the plug-in. The parameter in InitPlugin should also be checked, device information should be filled in, and the runtime API should be registered. In the initialization, PaddlePaddle loads the plug-in and invokes InitPlugin to initialize it, and register runtime (The whole process can be done automatically by the framework, only if the dynamic-link library is in site-packages/paddle-plugins/ or the designated directory of the enviornment variable of CUSTOM_DEVICE_ROOT). + +Example: + +```c++ +#include "paddle/phi/backends/device_ext.h" + +void InitPlugin(CustomRuntimeParams *params) { + // Check compatibility of the version and fill in the information of the custom runtime version used by the plug-in. + PADDLE_CUSTOM_RUNTIME_CHECK_VERSION(params); + + // Fill in the basic runtime information + params->device_type = "CustomCPU"; + params->sub_device_type = "V1"; + + // Register the Runtime API + params->interface->set_device = set_device; + params->interface->get_device = get_device; + params->interface->create_stream = create_stream; + params->interface->destroy_stream = destroy_stream; + params->interface->create_event = create_event; + params->interface->destroy_event = destroy_event; + params->interface->record_event = record_event; + params->interface->synchronize_device = sync_device; + params->interface->synchronize_stream = sync_stream; + params->interface->synchronize_event = sync_event; + params->interface->stream_wait_event = stream_wait_event; + params->interface->memory_copy_h2d = memory_copy; + params->interface->memory_copy_d2d = memory_copy; + params->interface->memory_copy_d2h = memory_copy; + params->interface->device_memory_allocate = allocate; + params->interface->device_memory_deallocate = deallocate; + params->interface->get_device_count = get_device_count; + params->interface->get_device_list = get_device_list; + params->interface->device_memory_stats = memstats; + params->interface->device_min_chunk_size = get_min_chunk_size; +} +``` + +The plug-in should first check the parameters of InitPlugin, and the framework should set the size to an optimal value and sent it to InitPlugin. For types of CustomRuntimeParams and C_DeviceInterface, please refer to[device_ext.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/backends/device_ext.h). + +Then, the plug-in should fill in its basic information and version number, which can be helpful for PaddlePaddle to manage the plug-in and check the version compatibility. + +- params->size and params->interface.size : In the following custom runtime versions, the size and the interface will rank the first and the second respectively in all types of CustomRuntimeParams. +- params->version : Information of the plug-in version is filled in. The definition of the version number can be found in [device_ext.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/backends/device_ext.h). And PaddlePaddle checks the version compatibility in the registration of custom runtime. +- params->device_type : the appellation of the device backend. If there is another plug-in with the same name, the runtime will not be registered. +- params->sub_device_type : the appellation of the sub-type of the device backend + +Finally, some callback APIs in params->interface should be filled by the plug-in (At least the required APIs should be implemented, or the runtime will not be registered otherwise). Thus, the custom runtime can be initialized. For details of the APIS, please refer to [Custom Runtime Document](./custom_runtime_en.html)。 + +```c++ +static size_t global_total_mem_size = 1 * 1024 * 1024 * 1024UL; +static size_t global_free_mem_size = global_total_mem_size; + +C_Status set_device(const C_Device device) { + return C_SUCCESS; +} + +C_Status get_device(const C_Device device) { + device->id = 0; + return C_SUCCESS; +} + +C_Status get_device_count(size_t *count) { + *count = 1; + return C_SUCCESS; +} + +C_Status get_device_list(size_t *device) { + *device = 0; + return C_SUCCESS; +} + +C_Status memory_copy(const C_Device device, void *dst, const void *src, size_t size) { + memcpy(dst, src, size); + return C_SUCCESS; +} + +C_Status allocate(const C_Device device, void **ptr, size_t size) { + if (size > global_free_mem_size) { + return C_FAILED; + } + global_free_mem_size -= size; + *ptr = malloc(size); + return C_SUCCESS; +} + +C_Status deallocate(const C_Device device, void *ptr, size_t size) { + if (!ptr) { + return C_FAILED; + } + global_free_mem_size += size; + free(ptr); + return C_SUCCESS; +} + +C_Status create_stream(const C_Device device, C_Stream *stream) { + stream = nullptr; + return C_SUCCESS; +} + +C_Status destroy_stream(const C_Device device, C_Stream stream) { + return C_SUCCESS; +} + +C_Status create_event(const C_Device device, C_Event *event) { + return C_SUCCESS; +} + +C_Status record_event(const C_Device device, C_Stream stream, C_Event event) { + return C_SUCCESS; +} + +C_Status destroy_event(const C_Device device, C_Event event) { + return C_SUCCESS; +} + +C_Status sync_device(const C_Device device) { + return C_SUCCESS; +} + +C_Status sync_stream(const C_Device device, C_Stream stream) { + return C_SUCCESS; +} + +C_Status sync_event(const C_Device device, C_Event event) { + return C_SUCCESS; +} + +C_Status stream_wait_event(const C_Device device, C_Stream stream, C_Event event) { + return C_SUCCESS; +} + +C_Status memstats(const C_Device device, size_t *total_memory, size_t *free_memory) { + *total_memory = global_total_mem_size; + *free_memory = global_free_mem_size + return C_SUCCESS; +} + +C_Status get_min_chunk_size(const C_Device device, size_t *size) { + *size = 1; + return C_SUCCESS; +} +``` + +## Step Two:Add Custom Kernel + +Taking the add as an example, this part will introduce how to implement a kernel and make it registered. + +Example: + +### 1. Determine the Kernel Statement + +Find the kernel statement of the header file `math_kernel.h` released by PaddlePaddle: + +```c++ +// Add the kernel function +// Model parameters: T - Data type +// Context - Device context +// Parameters: dev_ctx - Context object +// x - DenseTensor object +// y - DenseTensor object +// out - DenseTensor point +// Return: None +template +void AddKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + DenseTensor* out); + +``` + +### 2.Kernel Implementation and Registration + +```c++ +// add_kernel.cc + +#include "paddle/phi/extension.h" // the header file on which the custom kernel depends + +namespace custom_cpu { + +// Kernel Implementation +template +void AddKernel(const Context& dev_ctx, + const phi::DenseTensor& x, + const phi::DenseTensor& y, + phi::DenseTensor* out) { + // Use Alloc API of dev_ctx to allocate storage of the template parameter T for the output parameter--out. + dev_ctx.template Alloc(out); + // Use numel API of DenseTensor to acquire the number of Tensor elements. + auto numel = x.numel(); + // Use data API of DenseTensor to acquire the data pointer of the template parameter T of the input parameter--x. + auto x_data = x.data(); + // Use data API of DenseTensor to acquire the data pointer of the template parameter T of the input parameter--y. + auto y_data = y.data(); + // Use data API of DenseTensor to acquire the data pointer of the template parameter T of the output parameter--out. + auto out_data = out->data(); + // Get the computing logic done + for (auto i = 0; i < numel; ++i) { + out_data[i] = x_data[i] + y_data[i]; + } +} + +} // namespace custom_cpu + +// In the global namespace, use the macro of registration to register the kernel. +// Register AddKernel of CustomCPU +// Parameters: add - Kernel name +// CustomCPU - Backend name +// ALL_LAYOUT - Memory layout +// custom_cpu::AddKernel - Name of the kernel function +// int - Data type name +// int64_t - Data type name +// float - Data type name +// double - Data type name +// phi::dtype::float16 - Data type name +PD_REGISTER_PLUGIN_KERNEL(add, + CustomCPU, + ALL_LAYOUT, + custom_cpu::AddKernel, + int, + int64_t, + float, + double, + phi::dtype::float16){} +``` + +## Step Three:Compile and Install + +### CMake Compilation + +**Edit CMakeLists.txt** + +``` +cmake_minimum_required(VERSION 3.10) + +project(paddle-custom_cpu CXX C) + +set(PLUGIN_NAME "paddle_custom_cpu") +set(PLUGIN_VERSION "0.0.1") + +set(PADDLE_PLUGIN_DIR "/opt/conda/lib/python3.7/site-packages/paddle-plugins/") +set(PADDLE_INC_DIR "/opt/conda/lib/python3.7/site-packages/paddle/include/") +set(PADDLE_LIB_DIR "/opt/conda/lib/python3.7/site-packages/paddle/fluid/") + +############ Third-party dependencies +set(BOOST_INC_DIR "/path/to/Paddle/build/third_party/boost/src/extern_boost") +set(GFLAGS_INC_DIR "/path/to/Paddle/build/third_party/install/gflags/include") +set(GLOG_INC_DIR "/path/to/Paddle/build/third_party/install/glog/include") +set(THREAD_INC_DIR "/path/to/Paddle/build/third_party/threadpool/src/extern_threadpool") +set(THIRD_PARTY_INC_DIR ${BOOST_INC_DIR} ${GFLAGS_INC_DIR} ${GLOG_INC_DIR} ${THREAD_INC_DIR}) + +include_directories(${PADDLE_INC_DIR} ${THIRD_PARTY_INC_DIR}) +link_directories(${PADDLE_LIB_DIR}) + +add_definitions(-DPADDLE_WITH_CUSTOM_DEVICE) # for out CustomContext temporarily +add_definitions(-DPADDLE_WITH_CUSTOM_KERNEL) # for out fluid seperate temporarily + +############ Compile plug-ins +add_library(${PLUGIN_NAME} SHARED runtime.cc add_kernel.cc) +target_link_libraries(${PLUGIN_NAME} PRIVATE :core_avx.so) # special name + +############ Assembly plug-ins +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in + ${CMAKE_CURRENT_BINARY_DIR}/setup.py) + +add_custom_command(TARGET ${PLUGIN_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E remove -f ${CMAKE_CURRENT_BINARY_DIR}/python/ + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/python/ + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/python/paddle-plugins/ + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/lib${PLUGIN_NAME}.so ${CMAKE_CURRENT_BINARY_DIR}/python/paddle-plugins/ + COMMENT "Creating plugin dirrectories------>>>" +) + +add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/python/.timestamp + COMMAND python3 ${CMAKE_CURRENT_BINARY_DIR}/setup.py bdist_wheel + DEPENDS ${PLUGIN_NAME} + COMMENT "Packing whl packages------>>>" +) + +add_custom_target(python_package ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/python/.timestamp) +``` + +**Edit setup.py.in** + +CMake generates setup.py according to setup.py.in,and uses setuptools to encapsulate plug-ins into a wheel package. + +``` +from setuptools import setup, Distribution + +packages = [] +package_data = {} + +class BinaryDistribution(Distribution): + def has_ext_modules(self): + return True + +setup( + name = '@CMAKE_PROJECT_NAME@', + version='@PLUGIN_VERSION@', + description='Paddle CustomCPU plugin', + long_description='', + long_description_content_type="text/markdown", + author_email="Paddle-better@baidu.com", + maintainer="PaddlePaddle", + maintainer_email="Paddle-better@baidu.com", + project_urls={}, + license='Apache Software License', + packages= [ + 'paddle-plugins', + ], + include_package_data=True, + package_data = { + '': ['*.so', '*.h', '*.py', '*.hpp'], + }, + package_dir = { + '': 'python', + }, + zip_safe=False, + distclass=BinaryDistribution, + entry_points={ + 'console_scripts': [ + ] + }, + classifiers=[ + ], + keywords='Paddle CustomCPU plugin', +) +``` + +Compile plug-ins by following the command: + +```bash +$ mkdir build +$ cd build +$ cmake .. -DWITH_KERNELS=ON +$ make +``` + +After the compilation, make a wheel package under build/dist. + +### Setuptools Compilation + +**Edit setup.py** + +setuptools can be used to compile plug-ins and directly package them. + +``` +from setuptools import setup, Distribution, Extension +from setuptools.command.build_ext import build_ext +import os +import shutil + +packages = [] +package_data = {} + +class BinaryDistribution(Distribution): + def has_ext_modules(self): + return True + +for pkg_dir in ['build/python/paddle-plugins/']: + if os.path.exists(pkg_dir): + shutil.rmtree(pkg_dir) + os.makedirs(pkg_dir) + +ext_modules = [Extension(name='paddle-plugins.libpaddle_custom_cpu', + sources=['runtime.cc', 'add_kernel.cc'], + include_dirs=['/opt/conda/lib/python3.7/site-packages/paddle/include/'], + library_dirs=['/opt/conda/lib/python3.7/site-packages/paddle/fluid/'], + libraries=['core_avx.so'])] + +setup( + name='paddle-custom_cpu', + version='0.0.1', + description='Paddle CustomCPU plugin', + long_description='', + long_description_content_type="text/markdown", + author_email="Paddle-better@baidu.com", + maintainer="PaddlePaddle", + maintainer_email="Paddle-better@baidu.com", + project_urls={}, + license='Apache Software License', + ext_modules=ext_modules, + packages=[ + 'paddle-plugins', + ], + include_package_data=True, + package_data={ + '': ['*.so', '*.h', '*.py', '*.hpp'], + }, + package_dir={ + '': 'build/python', + }, + zip_safe=False, + distclass=BinaryDistribution, + entry_points={ + 'console_scripts': [ + ] + }, + classifiers=[ + ], + keywords='Paddle CustomCPU plugin', +) +``` + +Compile plug-ins by running the command: + +``` +$ python setup.py bdist_wheel +``` + +After the compilation, make a wheel package under the directory of dist. + +### Pip Installation + +Use pip to install a wheel package. + +``` +$ pip install build/dist/paddle_custom_cpu-0.0.1-cp37-cp37m-linux_aarch64.whl +``` + +## Step Four:Load and Use + +After installing plug-ins to their designated paths (site-packages/paddle-plugins), we can use the device backend of CustomCPU of PaddlePaddle to execute computation. + +First, check the custom devices of PaddlePaddle currently registered. + +``` +>>> paddle.device.get_all_custom_device_type() +['CustomCPU'] +``` + +Then, set the device backend to be used. + +``` +>>> paddle.set_device('CustomCPU') +``` + +Finally, use the new backend for computing tasks. + +``` +>>> x = paddle.to_tensor([1]) +>>> x +Tensor(shape=[1], dtype=int64, place=Place(CustomCPU:0), stop_gradient=True, + [1]) +>>> x + x +Tensor(shape=[1], dtype=int64, place=Place(CustomCPU:0), stop_gradient=True, + [2]) +``` diff --git a/docs/dev_guides/custom_device_docs/custom_kernel_docs/context_api_en.md b/docs/dev_guides/custom_device_docs/custom_kernel_docs/context_api_en.md new file mode 100644 index 00000000000..bff84489435 --- /dev/null +++ b/docs/dev_guides/custom_device_docs/custom_kernel_docs/context_api_en.md @@ -0,0 +1,150 @@ +# Context APIs + +## CustomContext +`CustomContext` is the acutal parameter of the template parameter Context of the custom kernel function. For details, please refer to [custom_context.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/backends/custom/custom_context.h). + +```c++ + // Constructor + // Parameter:place - CustomPlace object + // Return:None + explicit CustomContext(const CustomPlace&); + + // Destructor + virtual ~CustomContext(); + + // Get the contextual place in the device + // Parameter:None + // Return:place - Place object + const Place& GetPlace() const override; + + // Get the contextual stream in the device + // Parameter:None + // Return:stream - void* pointer + void* stream() const; + + // Wait for the completion of operations on the stream + // Parameter:None + // Return:None + void Wait() const override; +``` + +## DeviceContext +`CustomContext` originates from `DeviceContextp`,please refer to [device_context.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/device_context.h) + +```c++ + // No-Parameter constructor + DeviceContext(); + + // Copy constructor + DeviceContext(const DeviceContext&); + + // Move constructor + DeviceContext(DeviceContext&&); + + // Move assignment operator + DeviceContext& operator=(DeviceContext&&); + + // Destructor + virtual ~DeviceContext(); + + // Set device allocator + // Parameter:Allocator pointer + // Return:None + void SetAllocator(const Allocator*); + + // Set host allocator + // Parameter:Allocator pointer + // Return:None + void SetHostAllocator(const Allocator*); + + // Set zero-size allocator + // Parameter:Allocator pointer + // Return:None + void SetZeroAllocator(const Allocator*); + + // Get Allocator + // Parameter:None + // Return:Allocator object + const Allocator& GetAllocator() const; + + // Get Host allocator + // Parameter:None + // Return:Allocator object + const Allocator& GetHostAllocator() const; + + // Get zero-size allocator + // Parameter:None + // Return:Allocator object + const Allocator& GetZeroAllocator() const; + + // Allocate the device memory for Tensor + // Parameter: TensorBase pointer + // dtype - DataType variable + // requested_size - size_t variable with the default value of 0 + // Return:data pointer - void* pointer + void* Alloc(TensorBase*, DataType dtype, size_t requested_size = 0) const; + + // Allocate device memory for Tensor + // Template Parameter:T - data type + // Parameter:TensorBase pointer + // requested_size - size_t variable, 0 by default + // Return:data pointer - T* pointer + template + T* Alloc(TensorBase* tensor, size_t requested_size = 0) const; + + // Allocate host memory for Tensor + // Parameter:TensorBase pointer + // dtype - DataType variable + // requested_size - size_t variable, 0 by default + // Return:data pointer - void* pointer + void* HostAlloc(TensorBase* tensor, + DataType dtype, + size_t requested_size = 0) const; + + // Allocate host storage for Tensor + // Template Parameter:T - data type + // Parameter:TensorBase pointer + // requested_size - size_t variable, 0 by default + // Return:data pointer - T* data pointer + template + T* HostAlloc(TensorBase* tensor, size_t requested_size = 0) const; + + // Get the contextual information of the place, and implement sub interfaces + // Parameter:None + // Return:place - Place object + virtual const Place& GetPlace() const = 0; + + // Wait for the completion of operations on the stream, and implement sub interfaces + // Parameter:None + // Return:None + virtual void Wait() const {} + + // Set the random number generator + // Parameter:Generator pointer + // Return:None + void SetGenerator(Generator*); + + // Get the random number generator + // Parameter:None + // Return:Generator pointer + Generator* GetGenerator() const; + + // Set the Host random number generator + // Parameter:Generator pointer + // Return:None + void SetHostGenerator(Generator*); + + // Get the Host random number generator + // Parameter:None + // Return:Generator pointer + Generator* GetHostGenerator() const; + +``` + +## Relevant Information + +- `Place` and `CustomPlace`:please refer to [place.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/common/place.h) +- `Allocation` and `Allocator`:please refer to [allocator.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/allocator.h) +- `TensorBase`:please refer to [tensor_base.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/tensor_base.h) +- `DataType`:please refer to [data_type.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/common/data_type.h) +- `Generator`:please refer to [generator.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/generator.h) diff --git a/docs/dev_guides/custom_device_docs/custom_kernel_docs/cpp_api_en.rst b/docs/dev_guides/custom_device_docs/custom_kernel_docs/cpp_api_en.rst new file mode 100644 index 00000000000..55bfaf8e652 --- /dev/null +++ b/docs/dev_guides/custom_device_docs/custom_kernel_docs/cpp_api_en.rst @@ -0,0 +1,20 @@ +############################# +Kernel Implementation APIs +############################# + +The custom kernel-function implementation mainly depends on two parts: 1.APIs released by PaddlePaddle, including the context API, the tensor API, and the exception API; 2. APIs of the device encapsulation library. And the C++ API of PaddlePaddle has been released by the header file. + + +- `Context API <./context_api_en.html>`_ : about the C++ API of the device context +- `Tensor API <./tensor_api_en.html>`_ : about the C++ API of Tensor +- `Exception API <./exception_api_en.html>`_ : about the C++ API of exception handling + + +Note:There are abundant C++ API of PaddlePaddle. Three APIs will be introduced here and related classes and documents listed in corresponding websites are provided for developers. + +.. toctree:: + :hidden: + + context_api_en.md + tensor_api_en.md + exception_api_en.md diff --git a/docs/dev_guides/custom_device_docs/custom_kernel_docs/exception_api_en.md b/docs/dev_guides/custom_device_docs/custom_kernel_docs/exception_api_en.md new file mode 100644 index 00000000000..c1ded39d75c --- /dev/null +++ b/docs/dev_guides/custom_device_docs/custom_kernel_docs/exception_api_en.md @@ -0,0 +1,62 @@ +# Exception API + + +## PADDLE_ENFORCE + +How to use: + +```c++ + PADDLE_ENFORCE_{TYPE}(cond_a, // Condition A + cond_b, // Condition B, optional based on the TYPE + phi::errors::{ERR_TYPE}("{ERR_MSG}")); +``` + +There are different conditions according to `TYPE`: + +| Exception Macro | Basis | Error | +|---|---|---| +| PADDLE_ENFORCE_EQ | cond_a == cond_b | Raise ERR_TYPE exception and report ERR_MSG | +| PADDLE_ENFORCE_NE | cond_a != cond_b | Raise ERR_TYPE exception and report ERR_MSG | +| PADDLE_ENFORCE_GT | cond_a > cond_b | Raise ERR_TYPE exception and report ERR_MSG | +| PADDLE_ENFORCE_GE | cond_a >= cond_b | Raise ERR_TYPE exception and report ERR_MSG | +| PADDLE_ENFORCE_LT | cond_a < cond_b | Raise ERR_TYPE exception and report ERR_MSG | +| PADDLE_ENFORCE_LE | cond_a <= cond_b | Raise ERR_TYPE exception and report ERR_MSG | +| PADDLE_ENFORCE_NOT_NULL | cond_a != nullptr | Raise ERR_TYPE exception and report ERR_MSG | + +`ERR_TYPE` supports: + +| Type | +|---| +| InvalidArgument | +| NotFound | +| OutOfRange | +| AlreadyExists | +| ResourceExhausted | +| PreconditionNotMet | +| PermissionDenied | +| ExecutionTimeout | +| Unimplemented | +| Unavailable | +| Fatal | +| External | + +`ERR_MSG` is a C-style string C, supporting variable-length arguments. + +Example: + +```c++ +// If num_col_dims >= 2 && num_col_dims <= src.size() is not true, report the InvalidArgument exception. +// Print relevant tips +PADDLE_ENFORCE_EQ( + (num_col_dims >= 2 && num_col_dims <= src.size()), + true, + phi::errors::InvalidArgument("The num_col_dims should be inside [2, %d] " + "in flatten_to_3d, but received %d.", + src.size(), + num_col_dims)); +``` + +## Relevant Information + +- `PADDLE_ENFORCE`:please refer to [enforce.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/enforce.h) +- `errors`:please refer to [errors.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/errors.h) diff --git a/docs/dev_guides/custom_device_docs/custom_kernel_docs/kernel_declare_en.md b/docs/dev_guides/custom_device_docs/custom_kernel_docs/kernel_declare_en.md new file mode 100644 index 00000000000..c262e5af953 --- /dev/null +++ b/docs/dev_guides/custom_device_docs/custom_kernel_docs/kernel_declare_en.md @@ -0,0 +1,83 @@ +# Kernel Function Declaration + +PaddlePaddle has released the kernel declaration through the header file, and the framework is uniform both inside and outside. + +Custom kernel editing should be based on a specific kernel function declaration. The header file is under `include/paddle/phi/kernels/`. + +The format of the declaration is as follows: + +```c++ +template +void KernelNameKernel(const Context& dev_ctx, + InputTensor(s), + Attribute(s), + OutTensor(s)); +``` + +Agreement: + +1. Template Parameter:It is fixed in format. The data type of the first parameter is `T`,and that of the second is `Context`. +2. Return:`void` is the pattern. +3. Naming:Camel case: kernel name + "Kernel",such as `SoftmaxKernel` +4. Parameter:Context parameter, InputTensor,Attribute,and OutTensor, all arranged in order: +- Context Parameter:It belongs to `const Context&`. + - `CustomContext` corresponding with the custom kernel. You can refer to [custom_context.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/backends/custom/custom_context.h) +- InputTensor:Number >=0,and the types include: + - `const DenseTensor&` Please refer to [dense_tensor.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/dense_tensor.h) + - `const SelectedRows&` Please refer to [selected_rows.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/selected_rows.h) + - `const SparseCooTensor&` Please refer to [sparse_coo_tensor.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/sparse_coo_tensor.h) + - `const SparseCsrTensor&` Please refer to [sparse_csr_tensor.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/sparse_csr_tensor.h) + - `const std::vector&` + - `const std::vector&` + - `const std::vector&` +- Attribute:Number >=0,and the types include: + - `bool` + - `float` + - `double` + - `int` + - `int64_t` + - `phi::dtype::float16` Please refer to [float16.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/common/float16.h) + - `const Scalar&` Please refer to [scalar.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/common/scalar.h) + - `DataType` Please refer to [data_type.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/common/data_type.h) + - `DataLayout` Please refer to [layout.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/common/layout.h) + - `Place` Please refer to [place.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/common/place.h) + - `const std::vector&` + - `const ScalarArray&` Please refer to [scalar_array.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/common/scalar_array.h) + - `const std::vector&` + - `const std::string&` + - `const std::vector&` + - `const std::vector&` + - `const std::vector&` + - `const std::vector&` +- OutTensor:Number >0,and the types include: + - `DenseTensor*` + - `SelectedRows*` + - `SparseCooTensor*` + - `SparseCsrTensor*` + - `std::vector` + - `std::vector` + - `std::vector` + +For example,when the kernel function of `softmax` is in `softmax_kernel.h`: + +```c++ +// Softmax +// Template Parameter: T - data type +// Context - the device context +// Parameter: dev_ctx - object of the Context +// x - DenseTensor object +// axis - int type +// dtype - DataType type +// out - DenseTensor pointer +// Return: None +template +void SoftmaxKernel(const Context& dev_ctx, + const DenseTensor& x, + int axis, + DataType dtype, + DenseTensor* out); +``` + +> Note: +> 1. The kernel function declaration is the basis of the registration and the framework invocation of the custom kernel. It is released by the framework and required to be observed. +> 2. The kernel function declaration cannot perfectly match the header file. You can find the declaration you need by searching the name of the function. diff --git a/docs/dev_guides/custom_device_docs/custom_kernel_docs/register_api_en.md b/docs/dev_guides/custom_device_docs/custom_kernel_docs/register_api_en.md new file mode 100644 index 00000000000..163f1b91964 --- /dev/null +++ b/docs/dev_guides/custom_device_docs/custom_kernel_docs/register_api_en.md @@ -0,0 +1,62 @@ +# Kernel Registration API + +The registration macro of PaddlePaddle helps to register the custom kernel,which can be called by the PaddlePaddle framework. + +The registration macro should be put in a global space. + +For the basic format of the registration macro, please refer to [kernel_registry.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/kernel_registry.h) + +```c++ +/** PD_REGISTER_PLUGIN_KERNEL + * + * Used to register kernels for plug-in backends. + * Support user-defined backend such as 'Ascend910'. + */ +PD_REGISTER_PLUGIN_KERNEL(kernel_name, backend, layout, meta_kernel_fn, ...)) {} +``` + +Explanation: + +- Name of the macro:`PD_REGISTER_PLUGIN_KERNEL` +- First parameter:kernel_name,which is the same both inside and outside. You can refer to registration names of the same kernel functions of CPU, such as `softmax`. +- Second parameter:backend,which can be customized. But its name must be the same as that of the custom runtime, such as `Ascend910`. +- Third parameter:layout,the enumeration of `DataLayout`. For the setting, please refer to [layout.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/common/layout.h) +- Fourth parameter:meta_kernel_fn,the name of a kernel function. Here, the template parameter is not included, such as `my_namespace::SoftmaxKernel`. +- Variable-length data type parameter: includes basic C++ data types or types defined by PaddlePaddle like `phi::dtype::float16`、`phi::dtype::bfloat16`、`phi::dtype::complex`. You can refer to [data_type.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/common/data_type.h) +- End:the function body. You can set the kernel if necessary. If not, keep `{}`. + +>Explanation: the declaration corresponding to the end function body: +>```c++ +>// Kernel Parameter Definition +>// Parameter: kernel_key - KernelKey object +>// kernel - Kernel pointer +>// Return: None +>void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( +> const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel); +>``` +> You can use the parameters `kernel_key` and `kernel` in the function body,and customize the kernel in its registration. + +Take the registration of the CustomCPU backend kernel of `softmax` as an example: + +```c++ +// The registration of the CustomCPU backend kernel of `softmax` +// Global naming space +// Parameter: softmax - Kernel name +// CustomCPU - Backend name +// ALL_LAYOUT - Storage layout +// custom_cpu::SoftmaxKernel - Kernel function name +// float - name of the data type +// double - name of the data type +// phi::dtype::float16 - name of the data type +PD_REGISTER_PLUGIN_KERNEL(softmax, + CustomCPU, + ALL_LAYOUT, + custom_cpu::SoftmaxKernel, + float, + double, + phi::dtype::float16) {} +``` + +> Note: +> 1. When the backend is registered through the custom runtime, the backend parameter must be the same as its name. +> 2. Except the requirement of the end function body of the registration macro,keep the empty function body. You can refer to other backends within the PaddlePaddle framework. diff --git a/docs/dev_guides/custom_device_docs/custom_kernel_docs/tensor_api_en.md b/docs/dev_guides/custom_device_docs/custom_kernel_docs/tensor_api_en.md new file mode 100644 index 00000000000..0cebe64df32 --- /dev/null +++ b/docs/dev_guides/custom_device_docs/custom_kernel_docs/tensor_api_en.md @@ -0,0 +1,190 @@ +# Tensor APIs + +There are many kinds of tensors released by PaddlePaddle, and their base class is `TensorBase`, and here will take the commonly-used API `DenseTensor` as an example. For the `TensorBase` and other tensors, please refer to the link at the end of this text. + +## DenseTensor + +All element data of `DenseTensor` are stored in contiguous memory, and you can refer to [dense_tensor.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/dense_tensor.h). + +```c++ + // Construct the DenseTensor and allocate memory + // Parameter:a - pointer type of the Allocator + // meta - DenseTensorMeta object + // Return:None + DenseTensor(Allocator* a, const DenseTensorMeta& meta); + + // Construct the DenseTensor and allocate memory + // Parameter:a - pointer type of the Allocator + // meta - DenseTensorMeta moving object + // Return:None + DenseTensor(Allocator* a, DenseTensorMeta&& meta); + + // Construct the DenseTensor and allocate memory + // Parameter:holder - shared pointer of Allocation + // meta - DenseTensorMeta moving object + // Return:None + DenseTensor(const std::shared_ptr& holder, + const DenseTensorMeta& meta); + + // Move Constructor + // Parameter:other - DenseTensor moving object + // Return:None + DenseTensor(DenseTensor&& other) = default; + + // Copy Constructor + // Parameter:other - DenseTensor object + // Return:None + DenseTensor(const DenseTensor& other); + + // Assignment + // Parameter:other - DenseTensor object + // Return:DenseTensor object + DenseTensor& operator=(const DenseTensor& other); + + // Move Assignment + // Parameter:other - DenseTensor object + // Return:DenseTensor object + DenseTensor& operator=(DenseTensor&& other); + + // No-Parameter Constructor + DenseTensor(); + + // Destructor + virtual ~DenseTensor() = default; + + // Get the type name,static function + // Parameter:None + // Return:string pointer + static const char* name(); + + // Acquire the number of elements of the tensor + // Parameter:None + // Return:int64_t categorical variable + int64_t numel() const override; + + // Acquire the dims of tbe tensor + // Parameter:None + // Return:DDim object + const DDim& dims() const noexcept override; + + // Acquire the lod of the tensor + // Parameter:None + // Return:LoD object + const LoD& lod() const noexcept; + + // Acquire the data type of the Tensor + // Parameter:None + // Return: DataType categorical variable + DataType dtype() const noexcept override; + + // Acquire the memory layout of the tensor + // Parameter:None + // Return:DataLayout categorical variable + DataLayout layout() const noexcept override; + + // Acquire the place of the tensor + // Parameter:None + // Return:Place categorical variable + const Place& place() const override; + + // Acquire the meta of the tensor + // Parameter:None + // Return:DenseTensorMeta object + const DenseTensorMeta& meta() const noexcept; + + // Set the meta of the tensor + // Parameter:meta - DenseTensorMeta move object + // Return:None + void set_meta(DenseTensorMeta&& meta); + + // Set the meta of the tensor + // Parameter:meta - DenseTensorMeta object + // Return:None + void set_meta(const DenseTensorMeta& meta); + + // Check whether the meta of the tensor is valid + // Parameter:None + // Return:bool categorical variable + bool valid() const noexcept override; + + // Check wether the tensor is initialized + // Parameter:None + // Return:bool categorical variable + bool initialized() const override; + + // Allocate memory for the tensor + // Parameter:allocator - Allocator pointer type + // dtype - DataType variable + // requested_size - size_t categorical variable + // Return:void* pointer + void* AllocateFrom(Allocator* allocator, + DataType dtype, + size_t requested_size = 0) override; + + // Check whether memory is shared with other tensors + // Parameter:b - DenseTensor object + // Return:bool categorical variable + bool IsSharedWith(const DenseTensor& b) const; + + // Modify the dims of the tensor and allocate memory + // Parameter:dims - DDim object + // Return:None + void ResizeAndAllocate(const DDim& dims); + + // Modify the dims of the tensor + // Parameter:dims - DDim object + // Return:DenseTensor object + DenseTensor& Resize(const DDim& dims); + + // Reset the LoD of the tensor + // Parameter:lod - LoD object + // Return:None + void ResetLoD(const LoD& lod); + + // Acquire the memory size of the tensor + // Parameter:None + // Return:size_t categorical variable + size_t capacity() const; + + // Acquire the unchangeable data pointer of the tensor + // Template parameter:T - data type + // Parameter:None + // Return:the unchangeable T data pointer + template + const T* data() const; + + // Acquire the unchangeable data pointer of the tensor + // Parameter:None + // Return:the unchangeable void data pointer + const void* data() const; + + // Acquire the revisable data pointer of the tensor + // Template parameter:T - data type + // Parameter:None + // Return:the revisable T data pointer + template + T* data(); + + // Acquire the revisable data pointer of the tensor + // Parameter:None + // Return:the revisable void data pointer + void* data(); +``` + +## Other Tensors + +- `TensorBase`:Please refer to [tensor_base.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/tensor_base.h) +- `SelectedRows`:Please refer to [selected_rows.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/selected_rows.h) +- `SparseCooTensor`:Please refer to [sparse_coo_tensor.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/sparse_coo_tensor.h) +- `SparseCsrTensor`:Please refer to [sparse_csr_tensor.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/sparse_csr_tensor.h) + + +## Relevant Information + +- `Allocation` and `Allocator`:Please refer to [allocator.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/allocator.h) +- `DenseTensorMeta`:Please refer to [tensor_meta.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/tensor_meta.h) +- `DDim`:Please refer to [ddim.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/ddim.h) +- `LoD`:Please refer to [lod_utils.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/core/lod_utils.h) +- `DataType`:Please refer to [data_type.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/common/data_type.h) +- `DataLayout`:Please refer to [layout.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/common/layout.h) +- `Place`:Please refer to [place.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/common/place.h) diff --git a/docs/dev_guides/custom_device_docs/custom_kernel_en.rst b/docs/dev_guides/custom_device_docs/custom_kernel_en.rst new file mode 100644 index 00000000000..5a64ac842f2 --- /dev/null +++ b/docs/dev_guides/custom_device_docs/custom_kernel_en.rst @@ -0,0 +1,19 @@ +#################### +Custom Kernel +#################### + +The custom kernel is the implementation of corresponding operators of the kernel function (or kernel). The PaddlePaddle framework provides the custom kernel for the external device registered by the custom runtime, achieving the compiling, registration, and automatic loading of the kernel independent of the framework. +The implementation of the custom kernel is based on the public kernel statement of PaddlePaddle, and public C++ API and register macro. + + +- `Kernel function statement <./custom_kernel_docs/kernel_declare_en.html>`_ : to introduce the kernel statement of PaddlePaddle +- `Kernel implementation API <./custom_kernel_docs/cpp_api_en.html>`_ : to introduce the C++ API required in the implementation of the custom function. +- `Kernel register API <./custom_kernel_docs/register_api_en.html>`_ : to introduce the register macro of the custom kernel. + + +.. toctree:: + :hidden: + + custom_kernel_docs/kernel_declare_en.md + custom_kernel_docs/cpp_api_en.rst + custom_kernel_docs/register_api_en.md diff --git a/docs/dev_guides/custom_device_docs/custom_runtime_en.rst b/docs/dev_guides/custom_device_docs/custom_runtime_en.rst new file mode 100644 index 00000000000..c036565be6d --- /dev/null +++ b/docs/dev_guides/custom_device_docs/custom_runtime_en.rst @@ -0,0 +1,144 @@ +############################# +Custom Runtime +############################# + +Custom Runtime offers a new method to register the runtime of new devices via plug-ins. Responsible for the management of PaddlePaddle devices and Runtime/Driver API, DeviceManager provides a uniform API for the framework to invoke device capabilities, offers a series of APIs to register Custom Runtime, and ensure that the binary system is compatible through C API. The APIs can be found in `device_ext.h `_ . Developers can add custom runtime for PaddlePaddle only by implementing these APIs. + +- `Data type <./runtime_data_type_en.html>`_ : to introduce definitions of data types of custom runtime. +- `Device API <./device_api_en.html>`_ : to introduce definitions and functions of Device APIs. +- `Memory API <./memory_api_en.html>`_ : to introduce definitions and functions of Memory APIs. +- `Stream API <./stream_api_en.html>`_ : to introduce definitions and functions of Stream APIs. +- `Event API <./event_api_en.html>`_ : to introduce definitions and functions of Event APIs. + + +Device APIs +############ + ++------------------------+----------------------------------------+ +| API | Function | ++========================+========================================+ +| initialize | To initialize the device backend | ++------------------------+----------------------------------------+ +| finalize | To de-initialize the device backend | ++------------------------+----------------------------------------+ +| init_device | To initialize the designated device | ++------------------------+----------------------------------------+ +| deinit_device | To de-initialize the designated device | ++------------------------+----------------------------------------+ +| set_device | To set the current device | ++------------------------+----------------------------------------+ +| get_device | To get the current device | ++------------------------+----------------------------------------+ +| synchronize_device | To synchronize the desginated device | ++------------------------+----------------------------------------+ +| get_device_count | To count available devices | ++------------------------+----------------------------------------+ +| get_device_list | To get the list of available devices | ++------------------------+----------------------------------------+ +| get_compute_capability | To get computing capability of devices | ++------------------------+----------------------------------------+ +| get_runtime_version | To get the runtime version | ++------------------------+----------------------------------------+ +| get_driver_version | To get the driver version | ++------------------------+----------------------------------------+ + + +Memory APIs +############ + ++---------------------------+-------------------------------------------------------------------+ +| API | Function | ++===========================+===================================================================+ +| device_memory_allocate | To allocate the device memory | ++---------------------------+-------------------------------------------------------------------+ +| device_memory_deallocate | To deallocate the device memory | ++---------------------------+-------------------------------------------------------------------+ +| host_memory_allocate | To allocate pinned host memory | ++---------------------------+-------------------------------------------------------------------+ +| host_memory_deallocate | To deallocate pinned host memory | ++---------------------------+-------------------------------------------------------------------+ +| unified_memory_allocate | To allocated unified memory | ++---------------------------+-------------------------------------------------------------------+ +| unified_memory_deallocate | To deallocate unified memory | ++---------------------------+-------------------------------------------------------------------+ +| memory_copy_h2d | To copy synchronous memory from host to device | ++---------------------------+-------------------------------------------------------------------+ +| memory_copy_d2h | To copy synchronous memory from device to host | ++---------------------------+-------------------------------------------------------------------+ +| memory_copy_d2d | To copy synchronous memory in the device | ++---------------------------+-------------------------------------------------------------------+ +| memory_copy_p2d | To copy synchronous memory between devices | ++---------------------------+-------------------------------------------------------------------+ +| async_memory_copy_h2d | To copy asynchronous memory from host to device | ++---------------------------+-------------------------------------------------------------------+ +| async_memory_copy_d2h | To copy asynchronous memory from device to host | ++---------------------------+-------------------------------------------------------------------+ +| async_memory_copy_d2d | To copy asynchronous memory in the device | ++---------------------------+-------------------------------------------------------------------+ +| async_memory_copy_p2d | To copy asynchronous memory between devices | ++---------------------------+-------------------------------------------------------------------+ +| device_memory_set | To fill the device memory | ++---------------------------+-------------------------------------------------------------------+ +| device_memory_stats | To measure device memory utilization | ++---------------------------+-------------------------------------------------------------------+ +| device_min_chunk_size | To check the minimum size of device memory chunks | ++---------------------------+-------------------------------------------------------------------+ +| device_max_chunk_size | To check the maximum size of device memory chunks | ++---------------------------+-------------------------------------------------------------------+ +| device_max_alloc_size | To check the maximum size of allocatable device memory | ++---------------------------+-------------------------------------------------------------------+ +| device_extra_padding_size | To check the extra padding size of device memory | ++---------------------------+-------------------------------------------------------------------+ +| device_init_alloc_size | To check the size of allocated device memory after initialization | ++---------------------------+-------------------------------------------------------------------+ +| device_realloc_size | To check the size of reallocated device memory | ++---------------------------+-------------------------------------------------------------------+ + + +Stream APIs +############ + ++---------------------+-------------------------------------------------------------------+ +| API | Function | ++=====================+===================================================================+ +| create_stream | To create a stream object | ++---------------------+-------------------------------------------------------------------+ +| destroy_stream | To destroy a stream object | ++---------------------+-------------------------------------------------------------------+ +| query_stream | To query whether all the tasks on the stream are done | ++---------------------+-------------------------------------------------------------------+ +| synchronize_stream | To synchronize the stream and wait for the completion of all tasks| ++---------------------+-------------------------------------------------------------------+ +| stream_add_callback | To add a host and call it back on the stream | ++---------------------+-------------------------------------------------------------------+ +| stream_wait_event | To wait for the completion of an event on the stream | ++---------------------+-------------------------------------------------------------------+ + + +Event APIs +############ + ++-------------------+---------------------------------------------------------+ +| API | Function | ++===================+=========================================================+ +| create_event | To create an event | ++-------------------+---------------------------------------------------------+ +| destroy_event | To destroy an event | ++-------------------+---------------------------------------------------------+ +| record_event | To record an event on the stream | ++-------------------+---------------------------------------------------------+ +| query_event | To query whether the event is done | ++-------------------+---------------------------------------------------------+ +| synchronize_event | To synchronize the event and wait for its completion | ++-------------------+---------------------------------------------------------+ + + +.. toctree:: + :hidden: + + runtime_data_type_en.md + device_api_en.md + memory_api_en.md + stream_api_en.md + event_api_en.md + diff --git a/docs/dev_guides/custom_device_docs/device_api_en.md b/docs/dev_guides/custom_device_docs/device_api_en.md new file mode 100644 index 00000000000..8ee2f32416a --- /dev/null +++ b/docs/dev_guides/custom_device_docs/device_api_en.md @@ -0,0 +1,185 @@ +# Device APIs + +## initialize 【optional】 + +### Definition + +```c++ +C_Status (*initialize)() +``` + +### Description + +It initializes the device backend, such as the runtime or the driver. During the device registration, it is the first to be invoked. But if the API is not implemented, it will not be invoked. + +## finalize 【optional】 + +### Definition + +```c++ +C_Status (*finalize)() +``` + +### Description + +It deinitializes the device backend. For example, the deinitialization is performed during the exit of the runtime or the driver. The API is invoked till the end of the exit. But if it is not implemented, it will not be invoked. + +## init_device 【optional】 + +### Definition + +```c++ +C_Status (*init_device)(const C_Device device) +``` + +### Description + +It initializes the designated device and initializes all available devices during the plug-in registration. If not implemented, the API will not be invoked, and it is invoked only after initialization. + +### Parameter + +device - the device needed to be initialized。 + +## deinit_device 【optional】 + +### Definition + +```c++ +C_Status (*deinit_device)(const C_Device device) +``` + +### Description + +It finalizes the designated device, and deallocate resources allocated to all devices. The API is inovked during the exit. If not implemented, it will not be inovked and it is invoked before finalization. + +### Parameter + +device - the device needed to be finalized + +### Definition + +## set_device 【required】 + +```c++ +C_Status (*set_device)(const C_Device device) +``` + +### Description + +It sets the current device, where following tasks are executed. + +### Parameter + +device - the device needed to be set + +## get_device 【required】 + +### Definition + +```c++ +C_Status (*get_device)(const C_Device device) +``` + +### Description + +It acquires the current device + +### Parameter + +device - to store the current device + +## synchronize_device 【required】 + +### Definition + +```c++ +C_Status (*synchronize_device)(const C_Device device) +``` + +### Description + +It synchronizes the device and waits for the completion of tasks on the device. + +### Parameter + +device - the device required to be synchronized + +## get_device_count 【required】 + +### Definition + +```c++ +C_Status (*get_device_count)(size_t* count) +``` + +### Description + +It counts available devices. + +### Parameter + +count - the number of available devices in storage + +## get_device_list 【required】 + +### Definition + +```c++ +C_Status (*get_device_list)(size_t* devices) +``` + +### Description + +It acquires the number list of all currently available devices. + +### Parameter + +devices - numbers of available devices in storage + +## get_compute_capability 【required】 + +### Definition + +```c++ +C_Status (*get_compute_capability)(size_t* compute_capability) +``` + +### Description + +It gets the computing capability of the device. + +### Parameter + +compute_capability - the computing capability of the stored device + +## get_runtime_version 【required】 + +### Definition + +```c++ +C_Status (*get_runtime_version)(size_t* version) +``` + +### Description + +It acquires the runtime version. + +### Parameter + +version - the runtime version in storage + +## get_driver_version 【required】 + +### Definition + +```c++ +C_Status (*get_driver_version)(size_t* version) +``` + +### Description + +It gets the driver version. + +### Parameter + +version - the version of the stored driver diff --git a/docs/dev_guides/custom_device_docs/event_api_en.md b/docs/dev_guides/custom_device_docs/event_api_en.md new file mode 100644 index 00000000000..887bedb04b2 --- /dev/null +++ b/docs/dev_guides/custom_device_docs/event_api_en.md @@ -0,0 +1,93 @@ +# Event APIs + +## create_event 【required】 + +### Definition + +```c++ +C_Status (*create_event)(const C_Device device, C_Event* event) +``` + +### Description + +It creates an event, which is used to synchronize tasks of different streams within the framework. When the device does not support asynchronous execution, empty implementation of the API is required. + +### Paremeter + +device - the device to be used + +event - the created event in storage + +## destroy_event 【required】 + +### Definition + +```c++ +C_Status (*destroy_event)(const C_Device device, C_Event event) +``` + +### Description + +It destroys an event. When the device does not support asynchronous execution, the API requires an empty implementation. + +### Parameter + +device - the device to be used + +event - the event needed to be destroyed + +## record_event 【required】 + +### Definition + +```c++ +C_Status (*record_event)(const C_Device device, C_Stream stream, C_Event event) +``` + +### Description + +It records the event on the stream. When the device does not support asynchronous execution, empty implementation of the API is required. + +### Parameter + +device - the device to be used + +stream - the stream where the event is recorded + +event - the recorded event + +## query_event 【optional】 + +### Definition + +```c++ +C_Status (*query_event)(const C_Device device, C_Event event) +``` + +### Description + +It queries whether the event is complete. If not implemented, PaddlePaddle will use synchronize_event instead. + +### Parameter + +device - the device to be used + +event - the event to be queried + +## synchronize_event 【required】 + +### Definition + +```c++ +C_Status (*synchronize_event)(const C_Device device, C_Event event) +``` + +### Description + +It synchronizes the event and waits for its completion. When the device does not support asynchronous execution, empty implementation of the API is required. + +### Parameter + +device - the device to be used + +event - the event required to be synchronized diff --git a/docs/dev_guides/custom_device_docs/index_en.rst b/docs/dev_guides/custom_device_docs/index_en.rst new file mode 100644 index 00000000000..49fca8ddf80 --- /dev/null +++ b/docs/dev_guides/custom_device_docs/index_en.rst @@ -0,0 +1,19 @@ +############################# +Custom Device Support +############################# + +The custom device function decouples the framework from the device and makes it available to extend the backend of the PaddlePaddle device via plug-ins. In this way, developers can make a plug-in for PaddlePaddle only by implementing the standard API and compiling it into a dynamic-link library, instead of by modifying the code of PaddlePaddle. Now it is easier to develop hardware backends for PaddlePaddle. + +The custom device function is composed of custom runtime and custom kernel. With the two modules, users can connect new custom devices to PaddlePaddle according to their own needs. + +- `Custom Runtime <./custom_runtime_en.html>`_ : Introduction of custom runtime of the PaddlePaddle framework +- `Custom Kernel <./custom_kernel_en.html>`_ : Introduction of custom kernel of the PaddlePaddle framework +- `CustomDevice Example <./custom_device_example_en.html>`_ : The tutorial of add a new custom device to PaddlePaddle + +.. toctree:: + :hidden: + + + custom_runtime_en.rst + custom_kernel_en.rst + custom_device_example_en.md diff --git a/docs/dev_guides/custom_device_docs/memory_api_en.md b/docs/dev_guides/custom_device_docs/memory_api_en.md new file mode 100644 index 00000000000..876d0cba3aa --- /dev/null +++ b/docs/dev_guides/custom_device_docs/memory_api_en.md @@ -0,0 +1,459 @@ +# Memory APIs + +## device_memory_allocate 【required】 + +### Definition + +```c++ +C_Status (*device_memory_allocate)(const C_Device device, void** ptr, size_t size) +``` + +### Description + +It allocates the device memory. + +### Parameter + +device - the device to be used + +ptr - the address of the allocated device memory + +size - the size of the device memory needed to be allocated (in byte) + +## device_memory_deallocate 【required】 + +### Definition + +```c++ +C_Status (*device_memory_deallocate)(const C_Device device, void* ptr, size_t size) +``` + +### Description + +It deallocates the device storage. + +### Parameter + +device - the device to be used + +ptr - the address of the device memory needed to be deallocated + +size - the size of the device memory needed to be deallocated (in byte) + +## host_memory_allocate 【optional】 + +### Definition + +```c++ +C_Status (*host_memory_allocate)(const C_Device device, void** ptr, size_t size) +``` + +### Description + +It allocates pinned host memory. + +### Parameter + +device - the device to be used + +ptr - the address of allocated host memory + +size - the size of memory needed to be allocated (in byte) + +## host_memory_deallocate 【optional】 + +### Definition + +```c++ +C_Status (*host_memory_deallocate)(const C_Device device, void* ptr, size_t size) +``` + +### Description + +It deallocates the pinned host memory. + +### Parameter + +device - the device to be used + +ptr - the address of host memory needed to be deallocated + +size - the size of memory needed to be deallocated (in byte) + +## unified_memory_allocate 【optional】 + +### Definition + +```c++ +C_Status (*unified_memory_allocate)(const C_Device device, void** ptr, size_t size) +``` + +### Description + +It allocates unified memory. + +### Parameter + +device - the device to be used + +ptr - unified memory address + +size - the size of memory needed to be allocated (in byte) + +## unified_memory_deallocate 【optional】 + +### Definition + +```c++ +C_Status (*unified_memory_deallocate)(const C_Device device, void** ptr, size_t size) +``` + +### Description + +It deallocates unified memory. + +### Parameter + +device - the device to be used + +ptr - the address of unified memory needed to be deallocated + +size - the size of memory needed to be deallocated (in byte) + +## memory_copy_h2d 【required】 + +### Definition + +```c++ +C_Status (*memory_copy_h2d)(const C_Device device, void* dst, const void* src, size_t size) +``` + +### Description + +It copies synchronous memory from the host to the device. + +### Parameter + +device - the device to be used + +dst - the address of destination device memory + +src - the address of the source host memory + +size - the size of memory needed to be copied (in byte) + +## memory_copy_d2h 【required】 + +### Definition + +```c++ +C_Status (*memory_copy_d2h)(const C_Device device, void* dst, const void* src, size_t size) +``` + +### Description + +It copies synchronous memory from the device to the host. + +### Parameter + +device - the device to be used + +dst - the address of the destination host memory + +src - the address of the source device memory + +size - the size of memory needed to be copied (in byte) + +## memory_copy_d2d 【required】 + +### Definition + +```c++ +C_Status (*memory_copy_d2d)(const C_Device device, void* dst, const void* src, size_t size) +``` + +### Description + +It copies synchronous memory in the device. + +### Parameter + +device - the device to be used + +dst - the address of the destination device memroy + +src - the address of the source device memory + +size - the size of memory needed to be copied (in byte) + +## memory_copy_p2p 【optional】 + +### Definition + +```c++ +C_Status (*memory_copy_p2p)(const C_Device dst_device, const C_Device src_device, void* dst, const void* src, size_t size) +``` + +### Description + +It copies synchronous memory between devices. + +### Parameter + +dst_device - the destination device + +src_device - the source device + +dst - the address of destination device memory + +src - the address of source device memory + +size - the size of memory needed to be copied (in byte) + +## async_memory_copy_h2d 【optional】 + +### Definition + +```c++ +C_Status (*async_memory_copy_h2d)(const C_Device device, C_Stream stream, void* dst, const void* src, size_t size) +``` + +### Description + +It copies asynchronous memory from the host to the device. If it is not implemented, PaddlePaddle will be replace it with a synchronous API. + +### Parameter + +device - the device to be used + +stream - it is executed on that stream. + +dst - the address of destination device memory + +src - the address of source host memory + +size - the size of memory neeeded to be copied (in byte) + +## async_memory_copy_d2h 【optional】 + +### Definition + +```c++ +C_Status (*async_memory_copy_d2h)(const C_Device device, C_Stream stream, void* dst, const void* src, size_t size) +``` + +### Description + +It copies asynchronous memory from device to host. If it is not implemented, PaddlePaddle will replace it with a synchronous API. + +### Parameter + +device - the device to be used + +stream - It is executed on the stream. + +dst - the address of destination host + +src - the address of source device + +size - the size of memory needed to be copied + +## async_memory_copy_d2d 【optional】 + +### Definition + +```c++ +C_Status (*async_memory_copy_d2d)(const C_Device device, C_Stream stream, void* dst, const void* src, size_t size) +``` + +### Description + +It copies asynchronous memory in the device. If it is not implemented, PaddlePaddle will replace it with a synchronous API. + +### Parameter + +device - the device to be used + +stream - the stream to be used + +dst - the address of destination device memory + +src - the address of source device memory + +size - the size of memory needed to be copied (in byte) + +## async_memory_copy_p2p 【optional】 + +### Definition + +```c++ +C_Status (*async_memory_copy_p2p)(const C_Device dst_device, const C_Device src_device, C_Stream stream, void* dst, const void* src, size_t size) +``` + +### Description + +It copies asynchronous memory between devices. If it is not implemented, PaddlePaddle will replace it with a synchronous API. + +### Parameter + +dst_device - the destination device + +src_device - the source device + +stream - the stream to be used + +dst - the address of destination device memory + +src - the address of source device memory + +size - the size of memory needed to be copied (in byte) + +## device_memory_set 【optional】 + +### Definition + +```c++ +C_Status (*device_memory_set)(const C_Device device, void* ptr, unsigned char value, size_t size) +``` + +### Description + +It uses the value to pad the memory of a device. If it is not implemented, PaddlePaddle will take its place with memory_copy_h2d. + +### Parameter + +device - the device to be used + +ptr - the address of the padding + +value - padded value + +size - padding size (in byte) + +## device_memory_stats 【required】 + +### Definition + +```c++ +C_Status (*device_memory_stats)(const C_Device device, size_t* total_memory, size_t* free_memory) +``` + +### Description + +It counts the memory using condition. + +### Parameter + +device - the device to be used + +total_memory - total memory (in byte) + +free_memory - free memory (in byte) + +## device_min_chunk_size 【required】 + +### Definition + +```c++ +C_Status (*device_min_chunk_size)(C_Device device, size_t* size) +``` + +### Description + +It checks the minimum size of device memory chunks (in byte). In order not to call the device API to frequently apply for/ deallocate memory, PaddlePaddle manages the device memory. When there is an application, memory will be first allocated from the managed memory. When there is an application for memory whose size is "size", the size of the allocated memory is size + extra_padding_size and it will be aligned with min_chunk_size, the minimum size of memory chunks. + +### Parameter + +device - the device to be used + +size - the size of the minimum chunk (in byte) + +## device_max_chunk_size 【optional】 + +### Definition + +```c++ +C_Status (*device_max_chunk_size)(C_Device device, size_t* size) +``` + +### Description + +The size of the memory allocated from that managed by PaddlePaddle is no more than the maximum size of device memory chunks (in byte). Otherwise, the device API will be invoked for allocation. If this API is not implemented, the size of the memory is device_max_alloc_size, the maximum size of allocatable device memory. + +### Parameter + +device - the device to be used + +size - the size of the maximum chunk (in byte) + +## device_max_alloc_size 【optional】 + +### Definition + +```c++ +C_Status (*device_max_alloc_size)(C_Device device, size_t* size) +``` + +### Description + +It checks the maximum size (in byte) of allocatable device memory. If it is not implemented, the memory size will be equal to that of the current available memory. + +### Parameter + +device - the device to be used + +size - the maximum size of allocatable memory (in byte) + +## device_extra_padding_size 【optional】 + +### Definition + +```c++ +C_Status (*device_extra_padding_size)(C_Device device, size_t* size) +``` + +### Description + +It allocates the extra padding size of device memory. If it is not implemented, the size will be set to 0 by default. In order not to call the device API to frequently apply for/ deallocate memory, PaddlePaddle manages the device memory. When there is an application, memory will be first allocated from the managed memory. When there is an application for memory whose size is "size", the size of the allocated memory is size + extra_padding_size and it will be aligned with min_chunk_size, the minimum size of memory chunks. + +### Parameter + +device - the device to be used + +size - the extra padding size (in byte) + +## device_init_alloc_size 【optional】 + +### Definition + +```c++ +C_Status (*device_init_alloc_size)(const C_Device device, size_t* size) +``` + +### Description + +It checks the size of allocated device memory (in byte) after initialization. If it is not implemented, the size will be equal to device_max_alloc_size, the maximum size of allocatable device memory. + +### Parameter + +device - the device to be used + +size - the size of first allocated memory (in byte) + +## device_realloc_size 【optional】 + +### Definition + +```c++ +C_Status (*device_realloc_size)(const C_Device device, size_t* size) +``` + +### Description + +It checks the size of reallocated device memory (in byte). If it is not implemented, the memory size will be equal to device_max_alloc_size, the maximum size of allocatable device memory. + +### Parameter + +device - the device to be used + +size - the size of reallocated memory (in byte) diff --git a/docs/dev_guides/custom_device_docs/runtime_data_type_en.md b/docs/dev_guides/custom_device_docs/runtime_data_type_en.md new file mode 100644 index 00000000000..d9c1e708385 --- /dev/null +++ b/docs/dev_guides/custom_device_docs/runtime_data_type_en.md @@ -0,0 +1,131 @@ +# Data Type + +## C_Status + +### Definition + +```c++ +typedef enum { + C_SUCCESS = 0, + C_WARNING, + C_FAILED, + C_ERROR, + C_INTERNAL_ERROR +} C_Status; +``` + +### Description + +C_SUCCESS - The returned value when the execution of the function is a success + +C_WARNING - The returned value when the performance of the funtion falls short of expectations. For example, the asynchronous API is actually synchronous. + +C_FAILED - Resources runs out or the request fails. + +C_ERROR - Parameter error, incorrect usage, or not initialized. + +C_INTERNAL_ERROR - Plug-in internal error + +## C_Device + +### Definition + +```c++ +typedef struct C_Device_st { int id; } * C_Device; +``` + +### Description + +It describes a device. + +## C_Stream + +### Definition + +```c++ +typedef struct C_Stream_st* C_Stream; +``` + +### Description + +It describes a stream, which is used to execute asynchronous tasks within the framework. In the stream, tasks are executed in order. + +## C_Event + +### Definition + +```c++ +typedef struct C_Event_st* C_Event; +``` + +### Description + +It describes an event, which is used to synchronize tasks from different streams within the framework. + +## C_Callback + +### Definition + +```c++ +typedef void (*C_Callback)(C_Device device, + C_Stream stream, + void* user_data, + C_Status* status); +``` + +### Description + +It is the callback function offered by the host and has four parameters: device, stream, user data, and returned value. + +## CustomRuntimeParams + +### Definition + +```c++ +struct CustomRuntimeParams { + size_t size; + C_DeviceInterface* interface; + CustomRuntimeVersion version; + char* device_type; + char* sub_device_type; + char reserved[32]; +}; +``` + +### Description + +They are function parameters of InitPlugin. + +size - the size of CustomRuntimeParams. The size of the framework and the plug-in may be different. You need to first check the size of the plug-in and ensure that memory access does not cross the boundary. It is feasible to use the macro of PADDLE_CUSTOM_RUNTIME_CHECK_VERSION in the check. + +interface - the device callback interface. It is necessary for the plug-in to implement essential APIs and fill the parameter in to finish registration. + +version - the custom runtime version defined in the device_ext.h, which is used to check the version compatibility by the framework. + +device_type - the appellation of the device type, used by the framework to distinguish devices and exposed to the user layer to specify the hardware back end, such as "CustomCPU". + +sub_device_type - the appellation of the sub-device type, used to interpret the plug-in version, such as "V1.0". + +## CustomRuntimeVersion + +### Definition + +```c++ +struct CustomRuntimeVersion { + size_t major, minor, patch; +}; +``` + +### Description + +It is the custom runtime version used by the plug-in. It is used to check the version compatibility by the framework and can be filled up by the macro of PADDLE_CUSTOM_RUNTIME_CHECK_VERSION. + +## C_DeviceInterface + +### Definition + +For detailed definitions of the types of C_DeviceInterface, please refer to [device_ext.h](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/backends/device_ext.h). + +### Description + +It collects the custom runtime callback APIs. diff --git a/docs/dev_guides/custom_device_docs/stream_api_en.md b/docs/dev_guides/custom_device_docs/stream_api_en.md new file mode 100644 index 00000000000..d0be594acdf --- /dev/null +++ b/docs/dev_guides/custom_device_docs/stream_api_en.md @@ -0,0 +1,115 @@ +# Stream APIs + +## create_stream 【required】 + +### Definition + +```c++ +C_Status (*create_stream)(const C_Device device, C_Stream* stream) +``` + +### Description + +It creats a stream, which is used to execute asynchronous tasks within the framework. In the stream, tasks are done in order. When the device does not support asynchronous execution, the API is required to be implemented with an empty method. + +### Parameter + +device - the device to be used + +stream - the created stream + +## destroy_stream 【required】 + +### Definition + +```c++ +C_Status (*destroy_stream)(const C_Device device, C_Stream stream) +``` + +### Description + +It destroys a stream. When the device does not support asynchronous execution, the API needs to be implemented with an empty method. + +### Parameter + +device - the device to be used + +stream - the stream required to be deallocated + +## query_stream 【optional】 + +### Definition + +```c++ +C_Status (*query_stream)(const C_Device device, C_Stream stream) +``` + +### Description + +It queries whether the tasks on the stream are done. If not implemented, it will be replaced with synchronize_stream by PaddlePaddle. + +### Parameter + +device - the device to be used + +stream - the stream required to be queried. + +## synchronize_stream 【required】 + +### Definition + +```c++ +C_Status (*synchronize_stream)(const C_Device device, C_Stream stream) +``` + +### Description + +It synchronizes the stream and waits for the completion of all tasks on the stream. When the device does not support asynchronous execution, the API is required to be implemented with an empty method. + +### Parameter + +device - the device to be used + +stream - the stream needed to be synchronized + +## stream_add_callback 【optional】 + +### Definition + +```c++ +C_Status (*stream_add_callback)(const C_Device device, C_Stream stream, C_Callback callback, void* user_data) +``` + +### Description + +It adds a host callback function to the stream. + +### Parameter + +device - the device to be used + +stream - the stream where the callback function is added + +callback - the callback function + +user_data - parameters of the function + +## stream_wait_event 【required】 + +### Definition + +```c++ +C_Status (*stream_wait_event)(const C_Device device, C_Stream stream, C_Event event) +``` + +### Description + +It waits for the completion of an event on the stream. When the device does not support asynchronous execution, the API is required to be implemented with an empty method. + +### Parameter + +device - the device to be used + +stream - the stream waited for + +event - the event waited for diff --git a/docs/dev_guides/index_en.rst b/docs/dev_guides/index_en.rst index 22108b5b757..6bc18c7ae8f 100644 --- a/docs/dev_guides/index_en.rst +++ b/docs/dev_guides/index_en.rst @@ -1,18 +1,15 @@ -######## +############################# Contribution Guidelines -######## +############################# We very much welcome you to participate in the construction of the paddle. The following content is dedicated to explaining all the ways to join the paddle, and try to help you contribute to the paddle smoothly. Similarly, if you feel that this document is missing, or that the description is unclear, we also welcome you to contribute to this series of documents. -- `Overview <./Overview_en.html>`_ : Contribution guidelines overview. +- `custom_device_docs <./custom_device_docs/index_en.html>`_ : Contribution guidelines overview. .. toctree:: :hidden: - Overview_en.md - local_dev_guide_en.md - submit_pr_guide_en.md - code_review_en.md + custom_device_docs/index_en.rst diff --git a/docs/index_en.rst b/docs/index_en.rst index 21bd05d332d..6d864cbd80d 100644 --- a/docs/index_en.rst +++ b/docs/index_en.rst @@ -7,4 +7,5 @@ install/index_en.rst guides/index_en.rst api/index_en.rst + dev_guides/index_en.rst release_note_en.md From 39bdd88bf6eef0861991b4b83c9461663ae7a3a6 Mon Sep 17 00:00:00 2001 From: Chen Long <1300851984@qq.com> Date: Fri, 13 May 2022 17:04:29 +0800 Subject: [PATCH 2/2] Update guides index (#4747) * Update release_note_cn.md * Update release_note_en.md * Update release_note_en.md * Update release_note_cn.md * Update release_note_cn.md * Update release_note_en.md * update guides index * update guides index * update guides index * fix api docs bugs test=document_fix * update release note --- docs/dev_guides/index_cn.rst | 2 + docs/dev_guides/index_en.rst | 3 + .../kernel_primitive_api/add_example_cn.md | 0 .../kernel_primitive_api/add_example_en.md | 0 .../api_description_cn.rst | 0 .../api_description_en.rst | 0 .../kernel_primitive_api/compute_api_cn.md | 0 .../kernel_primitive_api/compute_api_en.md | 0 .../kernel_primitive_api/example_cn.rst | 0 .../kernel_primitive_api/example_en.rst | 0 .../kernel_primitive_api/functor_api_cn.md | 0 .../kernel_primitive_api/functor_api_en.md | 0 .../images/compute_reduce.png | Bin .../images/example_add.png | Bin .../images/example_reduce.png | Bin .../images/io_read_data.png | Bin .../images/io_read_data_broadcast.png | Bin .../images/io_read_data_broadcast_stride.png | Bin .../images/io_read_data_reduce.png | Bin .../images/io_read_data_stride.png | Bin .../images/io_write_data.png | Bin .../images/io_write_data_stride.png | Bin .../kernel_primitive_api/index_cn.rst | 0 .../kernel_primitive_api/index_en.rst | 0 .../kernel_primitive_api/io_api_cn.md | 0 .../kernel_primitive_api/io_api_en.md | 0 .../kernel_primitive_api/reduce_example_cn.md | 0 .../kernel_primitive_api/reduce_example_en.md | 0 .../basic_concept/index_cn.rst | 23 - .../basic_concept/index_en.rst | 20 - .../01_paddle2.0_introduction/index_cn.rst | 18 - .../01_paddle2.0_introduction/index_en.rst | 16 - .../02_paddle2.0_develop/06_device_cn.ipynb | 405 ------------ docs/guides/02_paddle2.0_develop/index_cn.rst | 31 - docs/guides/03_VisualDL/index_cn.rst | 15 - docs/guides/03_VisualDL/index_en.rst | 8 - docs/guides/07_new_op/index_en.rst | 21 - docs/guides/08_api_mapping/index_cn.rst | 16 - .../autograd_cn.rst | 10 +- .../customize_cn.ipynb} | 0 .../gradient_clip_cn.rst | 0 .../gradient_clip_en.rst | 0 .../images}/autograd_image_3-1.png | Bin .../images}/autograd_image_4-1.png | Bin .../images}/autograd_image_4-2.png | Bin .../images}/autograd_image_4-3.png | Bin .../images}/autograd_image_4-4.png | Bin docs/guides/advanced/index_cn.rst | 22 + docs/guides/advanced/index_en.rst | 14 + .../layer_and_model_cn.md | 0 .../layer_and_model_en.md | 0 .../model_to_onnx_cn.rst} | 4 +- .../{03_VisualDL => advanced}/visualdl_cn.md | 0 .../{03_VisualDL => advanced}/visualdl_en.md | 0 .../visualdl_usage_cn.md | 2 +- .../visualdl_usage_en.md | 2 +- .../data_load_cn.ipynb} | 435 ++++++------ .../data_preprocessing_cn.ipynb} | 0 .../images/Axis_2.0.png | Bin .../images/Tensor_2.0.png | Bin .../images/Tensor_broadcast.png | Bin .../images/data_pipeline.png | Bin .../images/data_preprocessing.png | Bin .../images/lenet.png | Bin .../images/mnist.png | Bin .../images/model.png | Bin .../images/model_develop_flow.png | Bin .../images/paddle_jit_save_load_2.1.png | Bin .../images/paddle_save_load_2.1.png | Bin docs/guides/beginner/index_cn.rst | 28 + docs/guides/beginner/index_en.rst | 12 + .../model_cn.ipynb} | 0 .../model_save_load_cn.rst} | 0 .../quick_start_cn.ipynb} | 495 +++++++------- .../tensor_cn.md} | 0 .../tensor_en.md} | 0 .../train_eval_predict_cn.ipynb} | 622 +++++++++--------- .../hardware_info_cn.md | 0 .../index_cn.rst | 0 .../ipu_docs/index_cn.rst | 0 .../ipu_docs/infer_example_cn.md | 0 .../ipu_docs/paddle_install_cn.md | 0 .../ipu_docs/train_example_cn.md | 0 .../npu_docs/index_cn.rst | 0 .../npu_docs/paddle_install_cn.md | 0 .../npu_docs/train_example_cn.md | 0 .../rocm_docs/index_cn.rst | 0 .../rocm_docs/infer_example_cn.md | 0 .../rocm_docs/paddle_install_cn.md | 0 .../rocm_docs/paddle_rocm_cn.md | 0 .../rocm_docs/train_example_cn.md | 0 .../xpu_docs/index_cn.rst | 0 .../xpu_docs/inference_install_example_cn.md | 0 .../xpu_docs/paddle_2.0_xpu2_cn.md | 0 .../xpu_docs/paddle_2.0_xpu_cn.md | 0 .../xpu_docs/paddle_install_cn.md | 0 .../xpu_docs/paddle_install_xpu2_cn.md | 0 .../xpu_docs/train_example_cn.md | 0 .../xpu_docs/train_example_xpu2_cn.md | 0 docs/guides/index_cn.rst | 38 +- docs/guides/index_en.rst | 22 +- .../images/inference_ecosystem.png | Bin .../index_cn.rst | 0 .../index_en.rst | 3 - .../inference/images/inference.png | Bin .../inference/images/paddlepaddle.png | Bin .../inference/images/wechat.png | Bin .../inference/inference_cn.md | 0 .../mobile/mobile_index_cn.md | 0 .../paddleslim/paddle_slim_cn.md | 0 .../paddleslim/paddle_slim_en.rst | 0 .../basic_usage_cn.md | 0 .../basic_usage_en.md | 0 .../case_analysis_cn.md | 0 .../debugging_cn.md | 0 .../debugging_en.md | 0 .../grammar_list_cn.md | 0 .../grammar_list_en.md | 0 .../images/c++_error_log.png | Bin .../images/convert_cond.png | Bin .../images/dy2stat_error_log.png | Bin .../images/dygraph_export.png | Bin .../images/dygraph_to_static.png | Bin .../images/original_error_log.png | Bin .../images/pdb_cmd.png | Bin .../images/pdb_cmd_en.png | Bin .../images/revise_suggestion.png | Bin .../images/slice.png | Bin .../images/static_export.png | Bin .../images/to_static_export.png | Bin .../images/to_static_train.png | Bin .../index_cn.rst | 0 .../index_en.rst | 8 - .../principle_cn.md | 0 docs/guides/model_convert/index_cn.rst | 21 + docs/guides/model_convert/index_en.rst | 12 + .../load_old_format_model_cn.rst | 0 .../migration_cn.rst | 0 .../paddle_api_mapping_cn.rst | 0 .../pytorch_api_mapping_cn.md | 0 .../update_cn.md | 0 .../update_en.md | 0 .../guides/{07_new_op => new_op}/index_cn.rst | 6 +- .../{07_new_op => new_op}/new_custom_op_cn.md | 0 .../{07_new_op => new_op}/new_python_op_cn.md | 0 .../amp_cn.md | 0 .../amp_en.md | 0 .../guides/performance_improving/index_cn.rst | 9 +- .../guides/performance_improving/index_en.rst | 10 +- docs/release_note_cn.md | 269 ++++++-- docs/release_note_en.md | 257 ++++++-- 151 files changed, 1346 insertions(+), 1533 deletions(-) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/add_example_cn.md (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/add_example_en.md (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/api_description_cn.rst (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/api_description_en.rst (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/compute_api_cn.md (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/compute_api_en.md (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/example_cn.rst (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/example_en.rst (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/functor_api_cn.md (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/functor_api_en.md (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/images/compute_reduce.png (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/images/example_add.png (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/images/example_reduce.png (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/images/io_read_data.png (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/images/io_read_data_broadcast.png (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/images/io_read_data_broadcast_stride.png (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/images/io_read_data_reduce.png (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/images/io_read_data_stride.png (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/images/io_write_data.png (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/images/io_write_data_stride.png (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/index_cn.rst (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/index_en.rst (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/io_api_cn.md (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/io_api_en.md (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/reduce_example_cn.md (100%) rename docs/{guides/07_new_op => dev_guides}/kernel_primitive_api/reduce_example_en.md (100%) delete mode 100644 docs/guides/01_paddle2.0_introduction/basic_concept/index_cn.rst delete mode 100644 docs/guides/01_paddle2.0_introduction/basic_concept/index_en.rst delete mode 100644 docs/guides/01_paddle2.0_introduction/index_cn.rst delete mode 100644 docs/guides/01_paddle2.0_introduction/index_en.rst delete mode 100644 docs/guides/02_paddle2.0_develop/06_device_cn.ipynb delete mode 100644 docs/guides/02_paddle2.0_develop/index_cn.rst delete mode 100644 docs/guides/03_VisualDL/index_cn.rst delete mode 100644 docs/guides/03_VisualDL/index_en.rst delete mode 100644 docs/guides/07_new_op/index_en.rst delete mode 100644 docs/guides/08_api_mapping/index_cn.rst rename docs/guides/{01_paddle2.0_introduction/basic_concept => advanced}/autograd_cn.rst (97%) rename docs/guides/{02_paddle2.0_develop/07_customize_cn.ipynb => advanced/customize_cn.ipynb} (100%) rename docs/guides/{01_paddle2.0_introduction/basic_concept => advanced}/gradient_clip_cn.rst (100%) rename docs/guides/{01_paddle2.0_introduction/basic_concept => advanced}/gradient_clip_en.rst (100%) rename docs/guides/{01_paddle2.0_introduction/basic_concept/autograd_image => advanced/images}/autograd_image_3-1.png (100%) rename docs/guides/{01_paddle2.0_introduction/basic_concept/autograd_image => advanced/images}/autograd_image_4-1.png (100%) rename docs/guides/{01_paddle2.0_introduction/basic_concept/autograd_image => advanced/images}/autograd_image_4-2.png (100%) rename docs/guides/{01_paddle2.0_introduction/basic_concept/autograd_image => advanced/images}/autograd_image_4-3.png (100%) rename docs/guides/{01_paddle2.0_introduction/basic_concept/autograd_image => advanced/images}/autograd_image_4-4.png (100%) create mode 100644 docs/guides/advanced/index_cn.rst create mode 100644 docs/guides/advanced/index_en.rst rename docs/guides/{01_paddle2.0_introduction/basic_concept => advanced}/layer_and_model_cn.md (100%) rename docs/guides/{01_paddle2.0_introduction/basic_concept => advanced}/layer_and_model_en.md (100%) rename docs/guides/{02_paddle2.0_develop/09_model_to_onnx_cn.rst => advanced/model_to_onnx_cn.rst} (99%) rename docs/guides/{03_VisualDL => advanced}/visualdl_cn.md (100%) rename docs/guides/{03_VisualDL => advanced}/visualdl_en.md (100%) rename docs/guides/{03_VisualDL => advanced}/visualdl_usage_cn.md (99%) rename docs/guides/{03_VisualDL => advanced}/visualdl_usage_en.md (99%) rename docs/guides/{02_paddle2.0_develop/02_data_load_cn.ipynb => beginner/data_load_cn.ipynb} (95%) rename docs/guides/{02_paddle2.0_develop/03_data_preprocessing_cn.ipynb => beginner/data_preprocessing_cn.ipynb} (100%) rename docs/guides/{01_paddle2.0_introduction/basic_concept => beginner}/images/Axis_2.0.png (100%) rename docs/guides/{01_paddle2.0_introduction/basic_concept => beginner}/images/Tensor_2.0.png (100%) rename docs/guides/{01_paddle2.0_introduction/basic_concept => beginner}/images/Tensor_broadcast.png (100%) rename docs/guides/{02_paddle2.0_develop => beginner}/images/data_pipeline.png (100%) rename docs/guides/{02_paddle2.0_develop => beginner}/images/data_preprocessing.png (100%) rename docs/guides/{02_paddle2.0_develop => beginner}/images/lenet.png (100%) rename docs/guides/{02_paddle2.0_develop => beginner}/images/mnist.png (100%) rename docs/guides/{02_paddle2.0_develop => beginner}/images/model.png (100%) rename docs/guides/{02_paddle2.0_develop => beginner}/images/model_develop_flow.png (100%) rename docs/guides/{02_paddle2.0_develop => beginner}/images/paddle_jit_save_load_2.1.png (100%) rename docs/guides/{02_paddle2.0_develop => beginner}/images/paddle_save_load_2.1.png (100%) create mode 100644 docs/guides/beginner/index_cn.rst create mode 100644 docs/guides/beginner/index_en.rst rename docs/guides/{02_paddle2.0_develop/04_model_cn.ipynb => beginner/model_cn.ipynb} (100%) rename docs/guides/{02_paddle2.0_develop/08_model_save_load_cn.rst => beginner/model_save_load_cn.rst} (100%) rename docs/guides/{02_paddle2.0_develop/01_quick_start_cn.ipynb => beginner/quick_start_cn.ipynb} (94%) rename docs/guides/{01_paddle2.0_introduction/basic_concept/tensor_introduction_cn.md => beginner/tensor_cn.md} (100%) rename docs/guides/{01_paddle2.0_introduction/basic_concept/tensor_introduction_en.md => beginner/tensor_en.md} (100%) rename docs/guides/{02_paddle2.0_develop/05_train_eval_predict_cn.ipynb => beginner/train_eval_predict_cn.ipynb} (94%) rename docs/guides/{09_hardware_support => hardware_support}/hardware_info_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/index_cn.rst (100%) rename docs/guides/{09_hardware_support => hardware_support}/ipu_docs/index_cn.rst (100%) rename docs/guides/{09_hardware_support => hardware_support}/ipu_docs/infer_example_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/ipu_docs/paddle_install_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/ipu_docs/train_example_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/npu_docs/index_cn.rst (100%) rename docs/guides/{09_hardware_support => hardware_support}/npu_docs/paddle_install_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/npu_docs/train_example_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/rocm_docs/index_cn.rst (100%) rename docs/guides/{09_hardware_support => hardware_support}/rocm_docs/infer_example_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/rocm_docs/paddle_install_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/rocm_docs/paddle_rocm_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/rocm_docs/train_example_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/xpu_docs/index_cn.rst (100%) rename docs/guides/{09_hardware_support => hardware_support}/xpu_docs/inference_install_example_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/xpu_docs/paddle_2.0_xpu2_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/xpu_docs/paddle_2.0_xpu_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/xpu_docs/paddle_install_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/xpu_docs/paddle_install_xpu2_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/xpu_docs/train_example_cn.md (100%) rename docs/guides/{09_hardware_support => hardware_support}/xpu_docs/train_example_xpu2_cn.md (100%) rename docs/guides/{05_inference_deployment => infer}/images/inference_ecosystem.png (100%) rename docs/guides/{05_inference_deployment => infer}/index_cn.rst (100%) rename docs/guides/{05_inference_deployment => infer}/index_en.rst (60%) rename docs/guides/{05_inference_deployment => infer}/inference/images/inference.png (100%) rename docs/guides/{05_inference_deployment => infer}/inference/images/paddlepaddle.png (100%) rename docs/guides/{05_inference_deployment => infer}/inference/images/wechat.png (100%) rename docs/guides/{05_inference_deployment => infer}/inference/inference_cn.md (100%) rename docs/guides/{05_inference_deployment => infer}/mobile/mobile_index_cn.md (100%) rename docs/guides/{05_inference_deployment => infer}/paddleslim/paddle_slim_cn.md (100%) rename docs/guides/{05_inference_deployment => infer}/paddleslim/paddle_slim_en.rst (100%) rename docs/guides/{04_dygraph_to_static => jit}/basic_usage_cn.md (100%) rename docs/guides/{04_dygraph_to_static => jit}/basic_usage_en.md (100%) rename docs/guides/{04_dygraph_to_static => jit}/case_analysis_cn.md (100%) rename docs/guides/{04_dygraph_to_static => jit}/debugging_cn.md (100%) rename docs/guides/{04_dygraph_to_static => jit}/debugging_en.md (100%) rename docs/guides/{04_dygraph_to_static => jit}/grammar_list_cn.md (100%) rename docs/guides/{04_dygraph_to_static => jit}/grammar_list_en.md (100%) rename docs/guides/{04_dygraph_to_static => jit}/images/c++_error_log.png (100%) rename docs/guides/{04_dygraph_to_static => jit}/images/convert_cond.png (100%) rename docs/guides/{04_dygraph_to_static => jit}/images/dy2stat_error_log.png (100%) rename docs/guides/{04_dygraph_to_static => jit}/images/dygraph_export.png (100%) rename docs/guides/{04_dygraph_to_static => jit}/images/dygraph_to_static.png (100%) rename docs/guides/{04_dygraph_to_static => jit}/images/original_error_log.png (100%) rename docs/guides/{04_dygraph_to_static => jit}/images/pdb_cmd.png (100%) rename docs/guides/{04_dygraph_to_static => jit}/images/pdb_cmd_en.png (100%) rename docs/guides/{04_dygraph_to_static => jit}/images/revise_suggestion.png (100%) rename docs/guides/{04_dygraph_to_static => jit}/images/slice.png (100%) rename docs/guides/{04_dygraph_to_static => jit}/images/static_export.png (100%) rename docs/guides/{04_dygraph_to_static => jit}/images/to_static_export.png (100%) rename docs/guides/{04_dygraph_to_static => jit}/images/to_static_train.png (100%) rename docs/guides/{04_dygraph_to_static => jit}/index_cn.rst (100%) rename docs/guides/{04_dygraph_to_static => jit}/index_en.rst (80%) rename docs/guides/{04_dygraph_to_static => jit}/principle_cn.md (100%) create mode 100644 docs/guides/model_convert/index_cn.rst create mode 100644 docs/guides/model_convert/index_en.rst rename docs/guides/{01_paddle2.0_introduction => model_convert}/load_old_format_model_cn.rst (100%) rename docs/guides/{01_paddle2.0_introduction => model_convert}/migration_cn.rst (100%) rename docs/guides/{08_api_mapping => model_convert}/paddle_api_mapping_cn.rst (100%) rename docs/guides/{08_api_mapping => model_convert}/pytorch_api_mapping_cn.md (100%) rename docs/guides/{01_paddle2.0_introduction => model_convert}/update_cn.md (100%) rename docs/guides/{01_paddle2.0_introduction => model_convert}/update_en.md (100%) rename docs/guides/{07_new_op => new_op}/index_cn.rst (61%) rename docs/guides/{07_new_op => new_op}/new_custom_op_cn.md (100%) rename docs/guides/{07_new_op => new_op}/new_python_op_cn.md (100%) rename docs/guides/{01_paddle2.0_introduction/basic_concept => performance_improving}/amp_cn.md (100%) rename docs/guides/{01_paddle2.0_introduction/basic_concept => performance_improving}/amp_en.md (100%) diff --git a/docs/dev_guides/index_cn.rst b/docs/dev_guides/index_cn.rst index 7ada18508ad..766a84f57ea 100644 --- a/docs/dev_guides/index_cn.rst +++ b/docs/dev_guides/index_cn.rst @@ -11,6 +11,7 @@ - `Git 操作指南 <./git_guides/index_cn.html>`_ : Git 操作相关说明与Paddle CI 手册。 - `编译安装 `_ : 如何从源码编译安装Paddle。 - `API开发指南 <./api_contributing_guides/api_contributing_guides_cn.html>`_ : API开发相关说明。 +- `Kernel Primitives API <./kernel_primitive_api/index_cn.html>`_ : 介绍 PaddlePaddle 为加快算子开发提供的 Block 级 CUDA 函数。 - `曙光开发指南 <./sugon/index_cn.html>`_ : 曙光开发相关说明。 - `自定义新硬件接入指南 <./custom_device_docs/index_cn.html>`_: 介绍如何通过自定义硬件功能为飞桨接入新硬件后端。 - `文档贡献指南 <./docs_contributing_guides_cn.html>`_ : 飞桨文档贡献指南。 @@ -22,6 +23,7 @@ style_guides_cn.md git_guides/index_cn.rst api_contributing_guides/api_contributing_guides_cn.rst + kernel_primitive_api/index_cn.rst sugon/index_cn.rst custom_device_docs/index_cn.rst docs_contributing_guides_cn.md diff --git a/docs/dev_guides/index_en.rst b/docs/dev_guides/index_en.rst index 6bc18c7ae8f..a85b8b295a3 100644 --- a/docs/dev_guides/index_en.rst +++ b/docs/dev_guides/index_en.rst @@ -6,10 +6,13 @@ We very much welcome you to participate in the construction of the paddle. The f Similarly, if you feel that this document is missing, or that the description is unclear, we also welcome you to contribute to this series of documents. +- `Kernel Primitives API <./kernel_primitive_api/index_cn.html>`_ : Introduce the block-level CUDA functions provided by PaddlePaddle to speed up operator development. - `custom_device_docs <./custom_device_docs/index_en.html>`_ : Contribution guidelines overview. .. toctree:: :hidden: + + kernel_primitive_api/index_en.rst custom_device_docs/index_en.rst diff --git a/docs/guides/07_new_op/kernel_primitive_api/add_example_cn.md b/docs/dev_guides/kernel_primitive_api/add_example_cn.md similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/add_example_cn.md rename to docs/dev_guides/kernel_primitive_api/add_example_cn.md diff --git a/docs/guides/07_new_op/kernel_primitive_api/add_example_en.md b/docs/dev_guides/kernel_primitive_api/add_example_en.md similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/add_example_en.md rename to docs/dev_guides/kernel_primitive_api/add_example_en.md diff --git a/docs/guides/07_new_op/kernel_primitive_api/api_description_cn.rst b/docs/dev_guides/kernel_primitive_api/api_description_cn.rst similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/api_description_cn.rst rename to docs/dev_guides/kernel_primitive_api/api_description_cn.rst diff --git a/docs/guides/07_new_op/kernel_primitive_api/api_description_en.rst b/docs/dev_guides/kernel_primitive_api/api_description_en.rst similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/api_description_en.rst rename to docs/dev_guides/kernel_primitive_api/api_description_en.rst diff --git a/docs/guides/07_new_op/kernel_primitive_api/compute_api_cn.md b/docs/dev_guides/kernel_primitive_api/compute_api_cn.md similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/compute_api_cn.md rename to docs/dev_guides/kernel_primitive_api/compute_api_cn.md diff --git a/docs/guides/07_new_op/kernel_primitive_api/compute_api_en.md b/docs/dev_guides/kernel_primitive_api/compute_api_en.md similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/compute_api_en.md rename to docs/dev_guides/kernel_primitive_api/compute_api_en.md diff --git a/docs/guides/07_new_op/kernel_primitive_api/example_cn.rst b/docs/dev_guides/kernel_primitive_api/example_cn.rst similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/example_cn.rst rename to docs/dev_guides/kernel_primitive_api/example_cn.rst diff --git a/docs/guides/07_new_op/kernel_primitive_api/example_en.rst b/docs/dev_guides/kernel_primitive_api/example_en.rst similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/example_en.rst rename to docs/dev_guides/kernel_primitive_api/example_en.rst diff --git a/docs/guides/07_new_op/kernel_primitive_api/functor_api_cn.md b/docs/dev_guides/kernel_primitive_api/functor_api_cn.md similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/functor_api_cn.md rename to docs/dev_guides/kernel_primitive_api/functor_api_cn.md diff --git a/docs/guides/07_new_op/kernel_primitive_api/functor_api_en.md b/docs/dev_guides/kernel_primitive_api/functor_api_en.md similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/functor_api_en.md rename to docs/dev_guides/kernel_primitive_api/functor_api_en.md diff --git a/docs/guides/07_new_op/kernel_primitive_api/images/compute_reduce.png b/docs/dev_guides/kernel_primitive_api/images/compute_reduce.png similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/images/compute_reduce.png rename to docs/dev_guides/kernel_primitive_api/images/compute_reduce.png diff --git a/docs/guides/07_new_op/kernel_primitive_api/images/example_add.png b/docs/dev_guides/kernel_primitive_api/images/example_add.png similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/images/example_add.png rename to docs/dev_guides/kernel_primitive_api/images/example_add.png diff --git a/docs/guides/07_new_op/kernel_primitive_api/images/example_reduce.png b/docs/dev_guides/kernel_primitive_api/images/example_reduce.png similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/images/example_reduce.png rename to docs/dev_guides/kernel_primitive_api/images/example_reduce.png diff --git a/docs/guides/07_new_op/kernel_primitive_api/images/io_read_data.png b/docs/dev_guides/kernel_primitive_api/images/io_read_data.png similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/images/io_read_data.png rename to docs/dev_guides/kernel_primitive_api/images/io_read_data.png diff --git a/docs/guides/07_new_op/kernel_primitive_api/images/io_read_data_broadcast.png b/docs/dev_guides/kernel_primitive_api/images/io_read_data_broadcast.png similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/images/io_read_data_broadcast.png rename to docs/dev_guides/kernel_primitive_api/images/io_read_data_broadcast.png diff --git a/docs/guides/07_new_op/kernel_primitive_api/images/io_read_data_broadcast_stride.png b/docs/dev_guides/kernel_primitive_api/images/io_read_data_broadcast_stride.png similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/images/io_read_data_broadcast_stride.png rename to docs/dev_guides/kernel_primitive_api/images/io_read_data_broadcast_stride.png diff --git a/docs/guides/07_new_op/kernel_primitive_api/images/io_read_data_reduce.png b/docs/dev_guides/kernel_primitive_api/images/io_read_data_reduce.png similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/images/io_read_data_reduce.png rename to docs/dev_guides/kernel_primitive_api/images/io_read_data_reduce.png diff --git a/docs/guides/07_new_op/kernel_primitive_api/images/io_read_data_stride.png b/docs/dev_guides/kernel_primitive_api/images/io_read_data_stride.png similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/images/io_read_data_stride.png rename to docs/dev_guides/kernel_primitive_api/images/io_read_data_stride.png diff --git a/docs/guides/07_new_op/kernel_primitive_api/images/io_write_data.png b/docs/dev_guides/kernel_primitive_api/images/io_write_data.png similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/images/io_write_data.png rename to docs/dev_guides/kernel_primitive_api/images/io_write_data.png diff --git a/docs/guides/07_new_op/kernel_primitive_api/images/io_write_data_stride.png b/docs/dev_guides/kernel_primitive_api/images/io_write_data_stride.png similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/images/io_write_data_stride.png rename to docs/dev_guides/kernel_primitive_api/images/io_write_data_stride.png diff --git a/docs/guides/07_new_op/kernel_primitive_api/index_cn.rst b/docs/dev_guides/kernel_primitive_api/index_cn.rst similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/index_cn.rst rename to docs/dev_guides/kernel_primitive_api/index_cn.rst diff --git a/docs/guides/07_new_op/kernel_primitive_api/index_en.rst b/docs/dev_guides/kernel_primitive_api/index_en.rst similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/index_en.rst rename to docs/dev_guides/kernel_primitive_api/index_en.rst diff --git a/docs/guides/07_new_op/kernel_primitive_api/io_api_cn.md b/docs/dev_guides/kernel_primitive_api/io_api_cn.md similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/io_api_cn.md rename to docs/dev_guides/kernel_primitive_api/io_api_cn.md diff --git a/docs/guides/07_new_op/kernel_primitive_api/io_api_en.md b/docs/dev_guides/kernel_primitive_api/io_api_en.md similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/io_api_en.md rename to docs/dev_guides/kernel_primitive_api/io_api_en.md diff --git a/docs/guides/07_new_op/kernel_primitive_api/reduce_example_cn.md b/docs/dev_guides/kernel_primitive_api/reduce_example_cn.md similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/reduce_example_cn.md rename to docs/dev_guides/kernel_primitive_api/reduce_example_cn.md diff --git a/docs/guides/07_new_op/kernel_primitive_api/reduce_example_en.md b/docs/dev_guides/kernel_primitive_api/reduce_example_en.md similarity index 100% rename from docs/guides/07_new_op/kernel_primitive_api/reduce_example_en.md rename to docs/dev_guides/kernel_primitive_api/reduce_example_en.md diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/index_cn.rst b/docs/guides/01_paddle2.0_introduction/basic_concept/index_cn.rst deleted file mode 100644 index fce226d57f3..00000000000 --- a/docs/guides/01_paddle2.0_introduction/basic_concept/index_cn.rst +++ /dev/null @@ -1,23 +0,0 @@ -################### -基本概念 -################### - -你可以这里学习飞桨的基本概念: - -- `Tensor概念介绍 <./tensor_introduction_cn.html>`_ : 飞桨中数据的表示方式,Tensor概念介绍。 -- `层与模型 <./layer_and_model_cn.html>`_ : 飞桨中层与模型概念介绍。 -- `广播介绍 <./broadcasting_cn.html>`_ : 飞桨中广播概念的介绍。 -- `自动微分 <./autograd_cn.html>`_ : 飞桨中自动微分原理介绍。 -- `自动混合精度训练 <./amp_cn.html>`_ : 飞桨中自动混合精度功能介绍。 -- `梯度裁剪 <./gradient_clip_cn.html>`_ : 飞桨中梯度裁剪的方法介绍。 - -.. toctree:: - :hidden: - - tensor_introduction_cn.md - layer_and_model_cn.md - broadcasting_cn.rst - autograd_cn.rst - amp_cn.md - gradient_clip_cn.rst - \ No newline at end of file diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/index_en.rst b/docs/guides/01_paddle2.0_introduction/basic_concept/index_en.rst deleted file mode 100644 index f4d661af097..00000000000 --- a/docs/guides/01_paddle2.0_introduction/basic_concept/index_en.rst +++ /dev/null @@ -1,20 +0,0 @@ -######################## -Basic Concept -######################## - -You can start with studying basic concept of PaddlePaddle: - -- `Introduction to Tensor `_ : Introduction of Tensor, which is the representation of data in Paddle. -- `Models and Layers `_ : Introduction to models and layers in Paddle. -- `Broadcasting <./broadcasting_en.html>`_ : Introduction of broadcasting. -- `Automatic Mixed Precision Training <./amp_en.html>`_ : Introduction of Automatic Mixed Precision Training with Paddle. -- `Gradient Clip <./gradient_clip_en.html>`_ : Introduction of gradient clip methods in Paddle. - -.. toctree:: - :hidden: - - tensor_introduction_en.md - layer_and_model_en.md - broadcasting_en.rst - amp_en.md - gradient_clip_en.rst diff --git a/docs/guides/01_paddle2.0_introduction/index_cn.rst b/docs/guides/01_paddle2.0_introduction/index_cn.rst deleted file mode 100644 index 5618005a3c1..00000000000 --- a/docs/guides/01_paddle2.0_introduction/index_cn.rst +++ /dev/null @@ -1,18 +0,0 @@ -############### -整体介绍 -############### - -您可以通过下面的内容,了解更多飞桨框架2.0的内容: - -- `基本概念 <./basic_concept/index_cn.html>`_ : 飞桨框架2.0 基本概念的介绍。 -- `升级指南 <./update_cn.html>`_: 介绍飞桨框架2.0 的主要变化和如何升级到最新版飞桨。 -- `版本迁移工具 <./migration_cn.html>`_: 介绍飞桨框架版本转换工具的使用。 -- `兼容载入旧格式模型 <./load_old_format_model.html>`_: 介绍飞桨框架如何在2.x版本加载1.x版本保存的模型。 - -.. toctree:: - :hidden: - - basic_concept/index_cn.rst - update_cn.md - migration_cn.rst - load_old_format_model.rst diff --git a/docs/guides/01_paddle2.0_introduction/index_en.rst b/docs/guides/01_paddle2.0_introduction/index_en.rst deleted file mode 100644 index 68b81d51d8d..00000000000 --- a/docs/guides/01_paddle2.0_introduction/index_en.rst +++ /dev/null @@ -1,16 +0,0 @@ -##################### -Introduction -##################### - -Introduction of Paddle 2. - -For more information, you can view these pages: - -- `basic concept <./basic_concept/index_en.html>`_ : introduction of PaddlePaddle 2.0 basic concept. -- `update <./update_en.html>`_ : update guides for paddle 2.0. - -.. toctree:: - :hidden: - - basic_concept/index_en.rst - update_en.md diff --git a/docs/guides/02_paddle2.0_develop/06_device_cn.ipynb b/docs/guides/02_paddle2.0_develop/06_device_cn.ipynb deleted file mode 100644 index 569173abb51..00000000000 --- a/docs/guides/02_paddle2.0_develop/06_device_cn.ipynb +++ /dev/null @@ -1,405 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "cd536a25", - "metadata": {}, - "source": [ - "# 单机多卡训练\n", - "\n", - "随着深度学习的发展,模型和数据集越来越大,有时单张显卡无法满足训练任务的显存要求,或者单卡训练用时太久,影响训练速度,这些情况下需要用到多卡训练的方式。飞桨框架 2.0 增加 [paddle.distributed.spawn](../api/paddle/distributed/spawn_cn.html) 函数来启动单机多卡训练,同时原有的 [paddle.distributed.launch](../api/paddle/distributed/launch_cn.html) 的方式依然保留。\n", - "\n", - "## 一、launch启动\n", - "\n", - "### 1.1 高层API场景\n", - "\n", - "当调用 [paddle.Model](../api/paddle/Model_cn.html) 高层API来实现训练时,想要启动单机多卡训练非常简单,代码不需要做任何修改,只需要在启动时增加一下参数 `-m paddle.distributed.launch` 。\n", - "以MNIST为例,使用高层API的训练代码如下:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5a2702a8", - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "import paddle\n", - "import numpy as np\n", - "from paddle.vision.transforms import ToTensor\n", - "\n", - "# 加载训练数据集和测试数据集\n", - "train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=ToTensor())\n", - "test_dataset = paddle.vision.datasets.MNIST(mode='test', transform=ToTensor())\n", - "\n", - "# 使用 Sequential 模型组网\n", - "mnist = paddle.nn.Sequential(\n", - " paddle.nn.Flatten(1, -1), \n", - " paddle.nn.Linear(784, 512), \n", - " paddle.nn.ReLU(), \n", - " paddle.nn.Dropout(0.2), \n", - " paddle.nn.Linear(512, 10)\n", - ")\n", - "\n", - "# 使用 paddle.Model 封装模型\n", - "model = paddle.Model(mnist)\n", - "\n", - "# 使用 Model.prepare 配置训练准备参数\n", - "model.prepare(optimizer=paddle.optimizer.Adam(parameters=model.parameters()), \n", - " loss=paddle.nn.CrossEntropyLoss(), \n", - " metrics=paddle.metric.Accuracy())\n", - "\n", - "# 使用 Model.fit 训练模型\n", - "model.fit(train_dataset, \n", - " epochs=5, \n", - " batch_size=64,\n", - " verbose=1)" - ] - }, - { - "cell_type": "markdown", - "id": "847c28b9", - "metadata": {}, - "source": [ - "将上述代码保存为train.py,使用高层API启动多卡训练的命令如下:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3db288b6", - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "# 单机单卡启动,默认使用第0号卡\n", - "! python train.py\n", - "# 单机多卡启动,默认使用当前可见的所有卡\n", - "! python -m paddle.distributed.launch train.py\n", - "# 单机多卡启动,设置当前使用的第0号和第1号卡\n", - "! python -m paddle.distributed.launch --gpus='0,1' train.py\n", - "# 单机多卡启动,设置当前使用第0号和第1号卡\n", - "! export CUDA_VISIBLE_DEVICES=0,1\n", - "! python -m paddle.distributed.launch train.py" - ] - }, - { - "cell_type": "markdown", - "id": "3fcc2fdc-795d-400b-8f40-a69d88595558", - "metadata": {}, - "source": [ - "这里补充一段介绍这个方式启动后发生了什么?任务怎么分配到不同卡上的\n", - "另外针对这里应该会有常见的问题定位流程,补充一下介绍,有FAQ可以补一下到FAQ的链接。\n", - "\n", - "(待补充)" - ] - }, - { - "cell_type": "markdown", - "id": "17552b14", - "metadata": {}, - "source": [ - "### 1.2 基础API场景\n", - "\n", - "如果使用基础API实现现训练,想要启动单机多卡训练,需要对单机单卡的代码进行3处修改,具体如下:\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d4ebc36a", - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "import paddle\n", - "from paddle.vision.transforms import ToTensor\n", - "# 第1处改动,导入分布式训练所需的包\n", - "import paddle.distributed as dist\n", - "# 加载数据集\n", - "train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=ToTensor())\n", - "test_dataset = paddle.vision.datasets.MNIST(mode='test', transform=ToTensor())\n", - "# 第2处改动,初始化并行环境\n", - "dist.init_parallel_env()\n", - "\n", - "# 定义网络结构\n", - "mnist = paddle.nn.Sequential(\n", - " paddle.nn.Flatten(1, -1),\n", - " paddle.nn.Linear(784, 512),\n", - " paddle.nn.ReLU(),\n", - " paddle.nn.Dropout(0.2),\n", - " paddle.nn.Linear(512, 10)\n", - ")\n", - "# 用 DataLoader 实现数据加载\n", - "train_loader = paddle.io.DataLoader(train_dataset, batch_size=32, shuffle=True)\n", - "\n", - "# 第3处改动,增加paddle.DataParallel封装\n", - "mnist = paddle.DataParallel(mnist)\n", - "mnist.train()\n", - "# 设置迭代次数\n", - "epochs = 5\n", - "# 设置优化器\n", - "optim = paddle.optimizer.Adam(parameters=mnist.parameters())\n", - "for epoch in range(epochs):\n", - " for batch_id, data in enumerate(train_loader()):\n", - " x_data = data[0] # 训练数据\n", - " y_data = data[1] # 训练数据标签\n", - " predicts = mnist(x_data) # 预测结果\n", - " # 计算损失 等价于 prepare 中loss的设置\n", - " loss = paddle.nn.functional.cross_entropy(predicts, y_data)\n", - " # 计算准确率 等价于 prepare 中metrics的设置\n", - " acc = paddle.metric.accuracy(predicts, y_data)\n", - " # 下面的反向传播、打印训练信息、更新参数、梯度清零都被封装到 Model.fit() 中\n", - " # 反向传播\n", - " loss.backward()\n", - " if (batch_id+1) % 1800 == 0:\n", - " print(\"epoch: {}, batch_id: {}, loss is: {}, acc is: {}\".format(epoch, batch_id, loss.numpy(), acc.numpy()))\n", - " # 更新参数\n", - " optim.step()\n", - " # 梯度清零\n", - " optim.clear_grad()" - ] - }, - { - "cell_type": "markdown", - "id": "abaa5a5e", - "metadata": {}, - "source": [ - "修改完后保存文件为train.py,然后使用跟高层API相同的启动方式即可。\n", - "\n", - "补充:\n", - "\n", - "这里基础API实现的效果和高层一模一样吗?完全没有差异?有没有基础API可以更灵活应用的场景?为什么高层不用补额外的配置代码?\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "56786ff8", - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "# 单机多卡启动,默认使用当前可见的所有卡\n", - "! python -m paddle.distributed.launch train.py\n", - "# 单机多卡启动,设置当前使用的第0号和第1号卡\n", - "! python -m paddle.distributed.launch --gpus '0,1' train.py\n", - "# 单机多卡启动,设置当前使用第0号和第1号卡\n", - "! export CUDA_VISIBLE_DEVICES=0,1\n", - "! python -m paddle.distributed.launch train.py" - ] - }, - { - "cell_type": "markdown", - "id": "f5045368", - "metadata": {}, - "source": [ - "## 二、spawn启动\n", - "\n", - " `launch` 方式启动训练,以文件为单位启动多进程,需要用户在启动时调用 `paddle.distributed.launch` ,对于进程的管理要求较高。飞桨框架2.0版本增加了 `spawn` 启动方式,可以更好地控制进程,在日志打印、训练退出时更友好。\n", - " \n", - "(补充“对进程的管理要求较高”、“可以更好地控制进程,在日志打印、训练退出时更友好”这几句话的理解)" - ] - }, - { - "cell_type": "markdown", - "id": "199558fc-9c58-4ff5-8e9e-1bbddab9662d", - "metadata": {}, - "source": [ - "### 2.1 高层API场景\n", - "\n", - "使用 `spawn` 方式启动多卡训练时,需要先将训练的过程封装成一个函数,将超参数设为该函数的参数传入训练流程中。代码如下所示:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0ed54b0d-dcbb-4c52-b2aa-63bae432d79a", - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "import paddle\n", - "import numpy as np\n", - "from paddle.vision.transforms import ToTensor\n", - "# 高层API场景使用spwan方式时,需要导入paddle.distributed包\n", - "import paddle.distributed as dist\n", - "\n", - "def train():\n", - " # 加载训练数据集和测试数据集\n", - " train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=ToTensor())\n", - " test_dataset = paddle.vision.datasets.MNIST(mode='test', transform=ToTensor())\n", - "\n", - " # 使用 Sequential 模型组网\n", - " mnist = paddle.nn.Sequential(\n", - " paddle.nn.Flatten(1, -1), \n", - " paddle.nn.Linear(784, 512), \n", - " paddle.nn.ReLU(), \n", - " paddle.nn.Dropout(0.2), \n", - " paddle.nn.Linear(512, 10)\n", - " )\n", - "\n", - " # 使用 paddle.Model 封装模型\n", - " model = paddle.Model(mnist)\n", - "\n", - " # 使用 Model.prepare 配置训练准备参数\n", - " model.prepare(optimizer=paddle.optimizer.Adam(parameters=model.parameters()), \n", - " loss=paddle.nn.CrossEntropyLoss(), \n", - " metrics=paddle.metric.Accuracy())\n", - "\n", - " # 使用 Model.fit 训练模型\n", - " model.fit(train_dataset, \n", - " epochs=5, \n", - " batch_size=64,\n", - " verbose=1)\n", - "\n", - "\n", - "# 传入训练函数,指定进程数并指定当前使用的卡号\n", - "# (这里我测试使用多卡会报错,只能单卡跑)\n", - "if __name__ == '__main__':\n", - " dist.spawn(train, nprocs=1, gpus='0')" - ] - }, - { - "cell_type": "markdown", - "id": "c2ae28fc-5084-4393-a622-cf60d5e9df00", - "metadata": {}, - "source": [ - "### 2.2 基础API场景\n", - "\n", - "与高层API场景类似,使用 `spawn` 方式启动多卡训练时,需要先将训练的过程封装成一个函数,将超参数设为该函数的参数传入训练流程中。同时,也需要与 `paddle.distributed.launch` 过程类似,进行三处改动:导入分布式包、初始化并行环境和将模型封装。具体代码如下:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4cbcdcdb", - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "from __future__ import print_function\n", - "\n", - "import paddle\n", - "import paddle.nn as nn\n", - "import paddle.optimizer as opt\n", - "# 第1处改动,导入分布式训练所需的包\n", - "import paddle.distributed as dist\n", - "\n", - "def train(print_result=False):\n", - " # 加载数据集\n", - " train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=ToTensor())\n", - " test_dataset = paddle.vision.datasets.MNIST(mode='test', transform=ToTensor())\n", - " # 第2处改动,初始化并行环境\n", - " dist.init_parallel_env()\n", - "\n", - " # 定义网络结构\n", - " mnist = paddle.nn.Sequential(\n", - " paddle.nn.Flatten(1, -1),\n", - " paddle.nn.Linear(784, 512),\n", - " paddle.nn.ReLU(),\n", - " paddle.nn.Dropout(0.2),\n", - " paddle.nn.Linear(512, 10)\n", - " )\n", - " # 用 DataLoader 实现数据加载\n", - " train_loader = paddle.io.DataLoader(train_dataset, batch_size=32, shuffle=True)\n", - "\n", - " # 第3处改动,增加paddle.DataParallel封装\n", - " mnist = paddle.DataParallel(mnist)\n", - " mnist.train()\n", - " # 设置迭代次数\n", - " epochs = 5\n", - " # 设置优化器\n", - " optim = paddle.optimizer.Adam(parameters=mnist.parameters())\n", - " for epoch in range(epochs):\n", - " for batch_id, data in enumerate(train_loader()):\n", - " x_data = data[0] # 训练数据\n", - " y_data = data[1] # 训练数据标签\n", - " predicts = mnist(x_data) # 预测结果\n", - " # 计算损失 等价于 prepare 中loss的设置\n", - " loss = paddle.nn.functional.cross_entropy(predicts, y_data)\n", - " # 计算准确率 等价于 prepare 中metrics的设置\n", - " acc = paddle.metric.accuracy(predicts, y_data)\n", - " # 下面的反向传播、打印训练信息、更新参数、梯度清零都被封装到 Model.fit() 中\n", - " # 反向传播\n", - " loss.backward()\n", - " if (batch_id+1) % 1800 == 0 and print_reslut:\n", - " print(\"epoch: {}, batch_id: {}, loss is: {}, acc is: {}\".format(epoch, batch_id, loss.numpy(), acc.numpy()))\n", - " # 更新参数\n", - " optim.step()\n", - " # 梯度清零\n", - " optim.clear_grad()\n", - "\n", - "# 传入训练函数、参数、指定进程数并指定当前使用的卡号\n", - "if __name__ == '__main__':\n", - " dist.spawn(train, args=(True,), nprocs=2, gpus='4,5')" - ] - }, - { - "cell_type": "raw", - "id": "ea540e08", - "metadata": {}, - "source": [ - "上述代码在本地运行结果如下:\n", - "init nccl context nranks: 2 local rank: 0 gpu id: 4 ring id: 0\n", - "init nccl context nranks: 2 local rank: 1 gpu id: 5 ring id: 0\n", - "Please NOTE: device: 5, GPU Compute Capability: 7.0, Driver API Version: 10.2, Runtime API Version: 10.2\n", - "Please NOTE: device: 4, GPU Compute Capability: 7.0, Driver API Version: 10.2, Runtime API Version: 10.2\n", - "device: 4, cuDNN Version: 7.6.\n", - "device: 5, cuDNN Version: 7.6.\n", - "loss: [2.041318]\n", - "loss: [4.749344]" - ] - }, - { - "cell_type": "markdown", - "id": "1e47c613", - "metadata": {}, - "source": [ - "调用 [paddle.distributed.spawn](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/distributed/spawn_cn.html) 来启动多卡训练时,可根据需要设置参数:\n", - "* func:由 spawn 方法启动的进程所调用的目标函数。\n", - "* args:传入目标函数 func 的参数。\n", - "* nprocs:启动进程的数目。当仅需要使用部分可见的GPU设备进行训练时,可设置该参数指定GPU数。例如:当前机器有8张GPU卡 {0,1,2,3,4,5,6,7},此时会使用前两张卡 {0,1};或者当前机器通过配置环境变量 CUDA_VISIBLE_DEVICES=4,5,6,7,仅使4张GPU卡可见,此时会使用可见的前两张卡 {4,5}。若不设置该参数,默认使用所有可见的GPU设备训练。\n", - "* gpus:指定训练使用的GPU ID。例如 gpus='4,5' 可指定使用第4号卡和第5号卡。若不设置该参数,默认使用GPU ID序号较小的GPU。" - ] - }, - { - "cell_type": "markdown", - "id": "a132576e-7021-4fcc-a56d-d5ecb4bccebd", - "metadata": {}, - "source": [ - "# 三、总结\n", - "\n", - "待补充" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "py35-paddle1.2.0" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.7.4" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/docs/guides/02_paddle2.0_develop/index_cn.rst b/docs/guides/02_paddle2.0_develop/index_cn.rst deleted file mode 100644 index a85c778bf83..00000000000 --- a/docs/guides/02_paddle2.0_develop/index_cn.rst +++ /dev/null @@ -1,31 +0,0 @@ -################### -模型开发 -################### - -本部分将介绍飞桨框架2.0的开发流程。 - -为了快速上手飞桨框架2.0,你可以参考 `10分钟快速上手飞桨 <./01_quick_start_cn.html>`_ ; - -当完成了快速上手的任务后,下面这些模块会阐述如何用飞桨框架2.0,实现深度学习过程中的每一步。具体包括: - -- `数据集定义与加载 <./02_data_load_cn.html>`_ : 飞桨框架数据加载的方式,主要为\ ``paddle.io.Dataset + paddle.io.DataLoader``\ ,以及飞桨内置数据集的介绍。 -- `数据预处理 <./03_data_preprocessing_cn.html>`_ : 飞桨框架数据预处理的方法,主要是\ ``paddle.vision.transform.*``\ 。 -- `模型组网 <./04_model_cn.html>`_ : 飞桨框架组网API的介绍,主要是\ ``paddle.nn.*``\ ,然后是飞桨框架组网方式的介绍,即 Sequential 的组网与 SubClass 的组网。 -- `训练与预测 <./05_train_eval_predict_cn.html>`_ : 飞桨框架训练与预测的方法,有两种方式,一种是使用高层API\ ``paddle.Model``\ 封装模型,然后调用\ ``model.fit()、model.evaluate()、model.predict()``\ 完成模型的训练与预测;另一种是用基础API完成模型的训练与预测,也就是对高层API的拆解。 -- `单机多卡训练 <./06_device_cn.html>`_ : 飞桨框架在单机单卡、单机多卡的场景下完成模型的训练与预测。 -- `自定义指标 <./07_customize_cn.html>`_ : 飞桨框架自定义指标的方法,主要包含自定义Loss、自定义Metric与自定义Callback。 -- `模型的加载与保存 <./08_model_save_load_cn.html>`_ : 飞桨框架模型的加载与保存体系介绍。 -- `模型转ONNX协议 <./09_model_to_onnx_cn.html>`_ : 飞桨框架模型转换为ONNX格式介绍。 - -.. toctree:: - :hidden: - - 01_quick_start_cn.ipynb - 02_data_load_cn.ipynb - 03_data_preprocessing_cn.ipynb - 04_model_cn.ipynb - 05_train_eval_predict_cn.rst - 06_device_cn.rst - 07_customize_cn.rst - 08_model_save_load_cn.rst - 09_model_to_onnx_cn.rst diff --git a/docs/guides/03_VisualDL/index_cn.rst b/docs/guides/03_VisualDL/index_cn.rst deleted file mode 100644 index d6c6a98d570..00000000000 --- a/docs/guides/03_VisualDL/index_cn.rst +++ /dev/null @@ -1,15 +0,0 @@ -.. PaddlePaddle Fluid documentation master file, created by - sphinx-quickstart on Thu Jun 7 17:04:53 2018. - You can adapt this file completely to your liking, but it should at least - contain the root `toctree` directive. - -############## -VisualDL 工具 -############## - -.. toctree:: - :maxdepth: 1 - - - visualdl_cn.md - visualdl_usage_cn.md diff --git a/docs/guides/03_VisualDL/index_en.rst b/docs/guides/03_VisualDL/index_en.rst deleted file mode 100644 index 840edeb8e5e..00000000000 --- a/docs/guides/03_VisualDL/index_en.rst +++ /dev/null @@ -1,8 +0,0 @@ -VisualDL Tools -========================== - -.. toctree:: - :maxdepth: 1 - - visualdl_en.md - visualdl_usage_en.md diff --git a/docs/guides/07_new_op/index_en.rst b/docs/guides/07_new_op/index_en.rst deleted file mode 100644 index 5fe0b2c77c8..00000000000 --- a/docs/guides/07_new_op/index_en.rst +++ /dev/null @@ -1,21 +0,0 @@ -################### -Write New Operators -################### - -This section will guide you on how to use the custom operator mechanism of Paddle, including the following two categories: - -1. C++ operator: The writing method is relatively simple, does not involve the internal concept of the framework, does not need to recompile the paddle framework, and is used as an external module. -2. Python operator: use Python to implement forward and backward methods, then used in network. - -- `Custom C++ Operator <./new_custom_op_cn.html>`_ - -- `Custom Python Operator <./new_python_op_cn.html>`_ - -- `Kernel Primitives API <./kernel_primitive_api/index_en.html>`_ : Introduce the block-level CUDA functions provided by PaddlePaddle to speed up operator development. - -.. toctree:: - :hidden: - - new_op_en.md - op_notes_en.md - kernel_primitive_api/index_en.rst diff --git a/docs/guides/08_api_mapping/index_cn.rst b/docs/guides/08_api_mapping/index_cn.rst deleted file mode 100644 index 674633db5c0..00000000000 --- a/docs/guides/08_api_mapping/index_cn.rst +++ /dev/null @@ -1,16 +0,0 @@ -.. _cn_guides_others_information: - -########## -算子映射 -########## - -你可以通过以下内容,了解飞桨算子映射信息: - -- `Paddle API映射表 <./paddle_api_mapping_cn.html>`_ : 说明 Paddle 1.8 版本与 Paddle 2.0 API对应关系。 -- `PyTorch API映射表 <./pytorch_api_mapping_cn.html>`_ : 说明 PyTorch 1.8 版本与 Paddle 2.0 API对应关系。 - -.. toctree:: - :hidden: - - paddle_api_mapping_cn.rst - pytorch_api_mapping_cn.rst diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/autograd_cn.rst b/docs/guides/advanced/autograd_cn.rst similarity index 97% rename from docs/guides/01_paddle2.0_introduction/basic_concept/autograd_cn.rst rename to docs/guides/advanced/autograd_cn.rst index fcf36e1d774..1bce9208dba 100644 --- a/docs/guides/01_paddle2.0_introduction/basic_concept/autograd_cn.rst +++ b/docs/guides/advanced/autograd_cn.rst @@ -136,7 +136,7 @@ PaddlePaddle的神经网络核心是自动微分,本篇文章主要为你介 假设上面创建的\ ``x``\ 和\ ``y``\ 分别是神经网络中的参数,\ ``z``\ 为神经网络的损失值\ ``loss``\ 。 -.. image:: autograd_image/autograd_image_3-1.png +.. image:: images/autograd_image_3-1.png 对z调用\ ``backward()``\ ,飞桨即可以自动计算\ ``x``\ 和\ ``y``\ 的梯度,并且将他们存进\ ``grad``\ 属性中。 @@ -211,7 +211,7 @@ PaddlePaddle的神经网络核心是自动微分,本篇文章主要为你介 飞桨的自动微分是通过\ ``trace``\ 的方式,记录\ ``前向OP``\ 的执行,并自动创建\ ``反向var``\ 和添加相应的\ ``反向OP``\ ,然后来实现反向梯度计算的。 -.. image:: autograd_image/autograd_image_4-1.png +.. image:: images/autograd_image_4-1.png 下面本文用一些的例子,来模拟这个过程。 @@ -237,7 +237,7 @@ PaddlePaddle的神经网络核心是自动微分,本篇文章主要为你介 在上面代码中\ ``c.backward()``\ 执行前,你可以理解整个计算图是这样的: -.. image:: autograd_image/autograd_image_4-2.png +.. image:: images/autograd_image_4-2.png 当创建\ ``Tensor``\ ,\ ``Tensor``\ 的\ ``stop_grad=False``\ 时,会自动为此\ ``Tensor``\ 创建一个\ ``反向Tensor``\ 。在此例子中,a的反向Tensor就是\ ``a_grad``\ 。在\ ``a_grad``\ 中,会记录他的反向OP,因为a没有作为任何反向op的输入,所以它的\ ``grad_op``\ 为\ ``None``\ 。 @@ -253,7 +253,7 @@ PaddlePaddle的神经网络核心是自动微分,本篇文章主要为你介 调用\ ``backward()``\ 后,正式开始进行反向传播过程,开始自动计算微分。 -.. image:: autograd_image/autograd_image_4-3.png +.. image:: images/autograd_image_4-3.png 例子二:用一个稍微复杂一点的例子让你深入了解这个过程。 @@ -284,7 +284,7 @@ PaddlePaddle的神经网络核心是自动微分,本篇文章主要为你介 该例子的正向和反向图构建过程即: -.. image:: autograd_image/autograd_image_4-4.png +.. image:: images/autograd_image_4-4.png diff --git a/docs/guides/02_paddle2.0_develop/07_customize_cn.ipynb b/docs/guides/advanced/customize_cn.ipynb similarity index 100% rename from docs/guides/02_paddle2.0_develop/07_customize_cn.ipynb rename to docs/guides/advanced/customize_cn.ipynb diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/gradient_clip_cn.rst b/docs/guides/advanced/gradient_clip_cn.rst similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/gradient_clip_cn.rst rename to docs/guides/advanced/gradient_clip_cn.rst diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/gradient_clip_en.rst b/docs/guides/advanced/gradient_clip_en.rst similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/gradient_clip_en.rst rename to docs/guides/advanced/gradient_clip_en.rst diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/autograd_image/autograd_image_3-1.png b/docs/guides/advanced/images/autograd_image_3-1.png similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/autograd_image/autograd_image_3-1.png rename to docs/guides/advanced/images/autograd_image_3-1.png diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/autograd_image/autograd_image_4-1.png b/docs/guides/advanced/images/autograd_image_4-1.png similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/autograd_image/autograd_image_4-1.png rename to docs/guides/advanced/images/autograd_image_4-1.png diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/autograd_image/autograd_image_4-2.png b/docs/guides/advanced/images/autograd_image_4-2.png similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/autograd_image/autograd_image_4-2.png rename to docs/guides/advanced/images/autograd_image_4-2.png diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/autograd_image/autograd_image_4-3.png b/docs/guides/advanced/images/autograd_image_4-3.png similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/autograd_image/autograd_image_4-3.png rename to docs/guides/advanced/images/autograd_image_4-3.png diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/autograd_image/autograd_image_4-4.png b/docs/guides/advanced/images/autograd_image_4-4.png similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/autograd_image/autograd_image_4-4.png rename to docs/guides/advanced/images/autograd_image_4-4.png diff --git a/docs/guides/advanced/index_cn.rst b/docs/guides/advanced/index_cn.rst new file mode 100644 index 00000000000..f32e09b1a6c --- /dev/null +++ b/docs/guides/advanced/index_cn.rst @@ -0,0 +1,22 @@ +################### +模型开发更多用法 +################### + + +- `模型可视化 <./visualdl_usage_cn.html>`_ +- `自动微分 <./autograd_cn.html>`_ +- `层与模型 <./layer_and_model_cn.html>`_ +- `自定义Loss、Metric 及 Callback <./customize_cn.html>`_ +- `梯度裁剪 <./gradient_clip_cn.html>`_ +- `模型导出ONNX协议 <./model_to_onnx_cn.html>`_ + +.. toctree:: + :hidden: + + visualdl_usage_cn.md + autograd_cn.rst + layer_and_model_cn.md + customize_cn.ipynb + gradient_clip_cn.rst + model_to_onnx_cn.rst + \ No newline at end of file diff --git a/docs/guides/advanced/index_en.rst b/docs/guides/advanced/index_en.rst new file mode 100644 index 00000000000..95abcbc139c --- /dev/null +++ b/docs/guides/advanced/index_en.rst @@ -0,0 +1,14 @@ +################################## +More Uses for Model Development +################################## + +- `Model Visualization <./visualdl_usage_en.html>`_ +- `Model and Layer <./layer_and_model_en.html>`_ +- `Gradient Clip <./gradient_clip_en.html>`_ + +.. toctree:: + :hidden: + + visualdl_usage_cn.md + layer_and_model_cn.md + gradient_clip_cn.rst diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/layer_and_model_cn.md b/docs/guides/advanced/layer_and_model_cn.md similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/layer_and_model_cn.md rename to docs/guides/advanced/layer_and_model_cn.md diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/layer_and_model_en.md b/docs/guides/advanced/layer_and_model_en.md similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/layer_and_model_en.md rename to docs/guides/advanced/layer_and_model_en.md diff --git a/docs/guides/02_paddle2.0_develop/09_model_to_onnx_cn.rst b/docs/guides/advanced/model_to_onnx_cn.rst similarity index 99% rename from docs/guides/02_paddle2.0_develop/09_model_to_onnx_cn.rst rename to docs/guides/advanced/model_to_onnx_cn.rst index d76bbc182f8..27de200f6a7 100755 --- a/docs/guides/02_paddle2.0_develop/09_model_to_onnx_cn.rst +++ b/docs/guides/advanced/model_to_onnx_cn.rst @@ -1,8 +1,8 @@ .. _cn_model_to_onnx: -############# +################ 模型导出ONNX协议 -############# +################ 一、简介 ################## diff --git a/docs/guides/03_VisualDL/visualdl_cn.md b/docs/guides/advanced/visualdl_cn.md similarity index 100% rename from docs/guides/03_VisualDL/visualdl_cn.md rename to docs/guides/advanced/visualdl_cn.md diff --git a/docs/guides/03_VisualDL/visualdl_en.md b/docs/guides/advanced/visualdl_en.md similarity index 100% rename from docs/guides/03_VisualDL/visualdl_en.md rename to docs/guides/advanced/visualdl_en.md diff --git a/docs/guides/03_VisualDL/visualdl_usage_cn.md b/docs/guides/advanced/visualdl_usage_cn.md similarity index 99% rename from docs/guides/03_VisualDL/visualdl_usage_cn.md rename to docs/guides/advanced/visualdl_usage_cn.md index f01aa9ffdfc..e54eef5605f 100644 --- a/docs/guides/03_VisualDL/visualdl_usage_cn.md +++ b/docs/guides/advanced/visualdl_usage_cn.md @@ -1,4 +1,4 @@ -# VisualDL 使用指南 +# 模型可视化 ### 概述 diff --git a/docs/guides/03_VisualDL/visualdl_usage_en.md b/docs/guides/advanced/visualdl_usage_en.md similarity index 99% rename from docs/guides/03_VisualDL/visualdl_usage_en.md rename to docs/guides/advanced/visualdl_usage_en.md index 6510c99f186..93d98b7bc00 100755 --- a/docs/guides/03_VisualDL/visualdl_usage_en.md +++ b/docs/guides/advanced/visualdl_usage_en.md @@ -1,4 +1,4 @@ -# VisualDL user guide +# Model Visualization ## Overview diff --git a/docs/guides/02_paddle2.0_develop/02_data_load_cn.ipynb b/docs/guides/beginner/data_load_cn.ipynb similarity index 95% rename from docs/guides/02_paddle2.0_develop/02_data_load_cn.ipynb rename to docs/guides/beginner/data_load_cn.ipynb index 12f1dbbb586..13c316d1409 100644 --- a/docs/guides/02_paddle2.0_develop/02_data_load_cn.ipynb +++ b/docs/guides/beginner/data_load_cn.ipynb @@ -2,8 +2,6 @@ "cells": [ { "cell_type": "markdown", - "id": "6baddc28", - "metadata": {}, "source": [ "# 数据集定义与加载\n", "\n", @@ -19,84 +17,62 @@ "\n", "\n", "本文以图像数据集为例介绍,文本数据集可参考 [NLP 应用实践](../../practices/nlp/index_cn.html)。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "0e6cbad6-1346-47d3-8bd1-e28cbb55c8ad", - "metadata": {}, "source": [ "## 一、定义数据集\n", "\n", "### 1.1 直接加载内置数据集\n", "\n", "飞桨框架在 [paddle.vision.datasets](../../api/paddle/vision/Overview_cn.html#api) 和 [paddle.text](../..//api/paddle/text/Overview_cn.html#api) 目录下内置了一些经典数据集可直接调用,通过以下代码可查看飞桨框架中的内置数据集。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 1, - "id": "4cc7b788-00c6-4e1b-a6ab-49317c861aa5", - "metadata": { - "execution": { - "iopub.execute_input": "2022-01-10T02:42:53.325579Z", - "iopub.status.busy": "2022-01-10T02:42:53.325030Z", - "iopub.status.idle": "2022-01-10T02:42:54.698658Z", - "shell.execute_reply": "2022-01-10T02:42:54.697869Z", - "shell.execute_reply.started": "2022-01-10T02:42:53.325539Z" - }, - "scrolled": true - }, + "source": [ + "import paddle\n", + "print('计算机视觉(CV)相关数据集:', paddle.vision.datasets.__all__)\n", + "print('自然语言处理(NLP)相关数据集:', paddle.text.__all__)" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "计算机视觉(CV)相关数据集: ['DatasetFolder', 'ImageFolder', 'MNIST', 'FashionMNIST', 'Flowers', 'Cifar10', 'Cifar100', 'VOC2012']\n", "自然语言处理(NLP)相关数据集: ['Conll05st', 'Imdb', 'Imikolov', 'Movielens', 'UCIHousing', 'WMT14', 'WMT16', 'ViterbiDecoder', 'viterbi_decode']\n" ] } ], - "source": [ - "import paddle\n", - "print('计算机视觉(CV)相关数据集:', paddle.vision.datasets.__all__)\n", - "print('自然语言处理(NLP)相关数据集:', paddle.text.__all__)" - ] + "metadata": { + "execution": { + "iopub.execute_input": "2022-01-10T02:42:53.325579Z", + "iopub.status.busy": "2022-01-10T02:42:53.325030Z", + "iopub.status.idle": "2022-01-10T02:42:54.698658Z", + "shell.execute_reply": "2022-01-10T02:42:54.697869Z", + "shell.execute_reply.started": "2022-01-10T02:42:53.325539Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "9235d4f3-9e6f-4926-b003-da4eed882631", - "metadata": {}, "source": [ "从打印结果可以看到飞桨内置了 CV 领域的 MNIST、FashionMNIST、Flowers、Cifar10、Cifar100、VOC2012 数据集,以及 NLP 领域的 Conll05st、Imdb、Imikolov、Movielens、UCIHousing、WMT14、WMT16 数据集。\n", "\n", "\n", "以 [MNIST](../../api/paddle/vision/datasets/MNIST_cn.html) 数据集为例,加载内置数据集的代码示例如下所示。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 2, - "id": "5ddfd1f0-b188-4407-a331-7f7f622b805c", - "metadata": { - "execution": { - "iopub.execute_input": "2022-01-10T02:42:58.403305Z", - "iopub.status.busy": "2022-01-10T02:42:58.402126Z", - "iopub.status.idle": "2022-01-10T02:43:07.498070Z", - "shell.execute_reply": "2022-01-10T02:43:07.497331Z", - "shell.execute_reply.started": "2022-01-10T02:42:58.403262Z" - }, - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "train images: 60000 , test images: 10000\n" - ] - } - ], "source": [ "from paddle.vision.transforms import Normalize\n", "\n", @@ -106,84 +82,97 @@ "train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=transform)\n", "test_dataset = paddle.vision.datasets.MNIST(mode='test', transform=transform)\n", "print('train images: ',len(train_dataset),', test images: ',len(test_dataset))" - ] + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "train images: 60000 , test images: 10000\n" + ] + } + ], + "metadata": { + "execution": { + "iopub.execute_input": "2022-01-10T02:42:58.403305Z", + "iopub.status.busy": "2022-01-10T02:42:58.402126Z", + "iopub.status.idle": "2022-01-10T02:43:07.498070Z", + "shell.execute_reply": "2022-01-10T02:43:07.497331Z", + "shell.execute_reply.started": "2022-01-10T02:42:58.403262Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "29fbad59-3234-41c6-82e6-da194972666a", - "metadata": {}, "source": [ "内置的 [MNIST](../../api/paddle/vision/datasets/MNIST_cn.html) 数据集已经划分好了训练集和测试集,通过 `mode` 字段传入 `'train'` 或 `'test'` 来区分。\n", "\n", "另外可通过 `transform` 字段传入一些对图像进行变换的操作,飞桨在 [paddle.vision.transforms](../..api/paddle/vision/Overview_cn.html#about-transforms) 下提供了一些常用的图像变换操作,如对图像进行中心裁剪、水平翻转图像和对图像进行归一化等。这里在初始化 MNIST 数据集时传入了 `Normalize` 变换对图像进行归一化,对图像进行归一化可以加快模型训练的收敛速度。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "79102100-e52e-42d6-9b17-48cdb9b59991", - "metadata": {}, "source": [ "完成数据集初始化之后,可以使用下面的代码直接对数据集进行迭代读取。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 3, - "id": "d1bbf911-41a1-452a-80f8-b19c4aec2939", - "metadata": { - "execution": { - "iopub.execute_input": "2022-01-10T02:50:30.296150Z", - "iopub.status.busy": "2022-01-10T02:50:30.294929Z", - "iopub.status.idle": "2022-01-10T02:50:30.465409Z", - "shell.execute_reply": "2022-01-10T02:50:30.464593Z", - "shell.execute_reply.started": "2022-01-10T02:50:30.296089Z" - }, - "scrolled": true - }, + "source": [ + "from matplotlib import pyplot as plt\n", + "\n", + "for data in train_dataset:\n", + " image, label = data\n", + " print('shape of image: ',image.shape)\n", + " plt.title(str(label))\n", + " plt.imshow(image[0]) \n", + " break" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "shape of image: (1, 28, 28)\n" ] }, { + "output_type": "display_data", "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAEICAYAAACZA4KlAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAPW0lEQVR4nO3de4xc9XnG8eeJvZjYmMSOg+sQFzvglGsx6cqAsIAqCiUoEqAqECuKHErqNMFJaFwJSi+QilRulRARSpFMcTEV9wSEVdEk1IpwogaXhRowEG7GNDbGxmzBXH1Zv/1jx9Fidn67zJy5eN/vR1rtzHnPmfNq7GfPmfmdmZ8jQgDGvg90ugEA7UHYgSQIO5AEYQeSIOxAEoQdSIKwA0kQ9uRsh+03bX93lOt/p7Z+2B7f6v5QHXNRTW62Q9KciHh2n2VvSdr7n+O2iPjKkPosSc9L6omI3W1sF03gLzPqOX7oHwDs/ziNB5Ig7Khnte2XbN9VO23Hfo6wYzinSZol6UhJL0r6d96M2/8RdrxHRKyOiJ0R8aqkb0maLemoznaFZhF2jEZIcqebQHM4NcO72D5GU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G5Juiohja8v+UVJ/RCyt/aGcEhGXdElvV0h6o9PTeNdmK5oxdJpxSedI+rI6+NwV+jpPbXjeOnFknyfp2YhYHxE7Jd0m6ewO9NH1ImK1pP59Fp8taUXt9goN/mdpuzq9dYWI2BwRD9duvy5p7zTjHX3uCn21RSfCfqik3wy5v1HdNd97SPqZ7YdsL+p0M8OYHhGba7dfkjS9k80MY8RpvNtpn2nGu+a5a2T682bxBt17zY+IT0n6rKSLaqerXSkGX4N109jpqKbxbpdhphn/rU4+d41Of96sToR9k6SZQ+5/vLasK0TEptrvrZLuVvdNRb1l7wy6td9bO9zPb3XTNN7DTTOuLnjuOjn9eSfC/qCkObZn2z5A0hckrexAH+9he1LtjRPZniTpDHXfVNQrJS2s3V4o6Z4O9vIu3TKNd71pxtXh567j059HRNt/JJ2lwXfkn5P0V53ooU5fn5D0SO3n8U73JulWDZ7W7dLgexsXSvqIpFWSnpH0n5KmdlFv/ybpMUmPajBYMzrU23wNnqI/Kmlt7eesTj93hb7a8rxxuSyQBG/QAUkQdiAJwg4kQdiBJAg7kARhB5Ig7EAS/w9pgMSoTFggTAAAAABJRU5ErkJggg==" }, "metadata": { "needs_background": "light" - }, - "output_type": "display_data" + } } ], - "source": [ - "from matplotlib import pyplot as plt\n", - "\n", - "for data in train_dataset:\n", - " image, label = data\n", - " print('shape of image: ',image.shape)\n", - " plt.title(str(label))\n", - " plt.imshow(image[0]) \n", - " break" - ] + "metadata": { + "execution": { + "iopub.execute_input": "2022-01-10T02:50:30.296150Z", + "iopub.status.busy": "2022-01-10T02:50:30.294929Z", + "iopub.status.idle": "2022-01-10T02:50:30.465409Z", + "shell.execute_reply": "2022-01-10T02:50:30.464593Z", + "shell.execute_reply.started": "2022-01-10T02:50:30.296089Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "95b8738b-35a3-4748-8aa6-624e17bed362", - "metadata": {}, "source": [ "### 1.2 使用 paddle.io.Dataset 自定义数据集" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "ab2f3fb4", - "metadata": {}, "source": [ "在实际的场景中,一般需要使用自有的数据来定义数据集,这时可以通过 [paddle.io.Dataset](../../api/paddle/io/Dataset_cn.html#dataset) 基类来实现自定义数据集。\n", "\n", @@ -195,12 +184,18 @@ "\n", "下面介绍下载 MNIST 原始数据集文件后,用 `paddle.io.Dataset` 定义数据集的代码示例。\n", "\n" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "id": "f9487da0", + "source": [ + "# 下载原始的 MNIST 数据集并解压\n", + "! wget https://paddle-imagenet-models-name.bj.bcebos.com/data/mnist.tar\n", + "! tar -xf mnist.tar" + ], + "outputs": [], "metadata": { "execution": { "iopub.execute_input": "2022-01-28T05:34:50.747072Z", @@ -210,37 +205,11 @@ "shell.execute_reply.started": "2022-01-28T05:34:50.747033Z" }, "scrolled": true - }, - "outputs": [], - "source": [ - "# 下载原始的 MNIST 数据集并解压\n", - "! wget https://paddle-imagenet-models-name.bj.bcebos.com/data/mnist.tar\n", - "! tar -xf mnist.tar" - ] + } }, { "cell_type": "code", "execution_count": 4, - "id": "1d26950f", - "metadata": { - "execution": { - "iopub.execute_input": "2022-01-28T05:37:13.849337Z", - "iopub.status.busy": "2022-01-28T05:37:13.848816Z", - "iopub.status.idle": "2022-01-28T05:37:13.868808Z", - "shell.execute_reply": "2022-01-28T05:37:13.867867Z", - "shell.execute_reply.started": "2022-01-28T05:37:13.849276Z" - }, - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "train_custom_dataset images: 60000 test_custom_dataset images: 10000\n" - ] - } - ], "source": [ "import os\n", "import cv2\n", @@ -295,12 +264,29 @@ "train_custom_dataset = MyDataset('mnist/train','mnist/train/label.txt', transform)\n", "test_custom_dataset = MyDataset('mnist/val','mnist/val/label.txt', transform)\n", "print('train_custom_dataset images: ',len(train_custom_dataset), 'test_custom_dataset images: ',len(test_custom_dataset))" - ] + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "train_custom_dataset images: 60000 test_custom_dataset images: 10000\n" + ] + } + ], + "metadata": { + "execution": { + "iopub.execute_input": "2022-01-28T05:37:13.849337Z", + "iopub.status.busy": "2022-01-28T05:37:13.848816Z", + "iopub.status.idle": "2022-01-28T05:37:13.868808Z", + "shell.execute_reply": "2022-01-28T05:37:13.867867Z", + "shell.execute_reply.started": "2022-01-28T05:37:13.849276Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "0e705d33", - "metadata": {}, "source": [ "在上面的代码中,自定义了一个数据集类 `MyDataset`,`MyDataset` 继承自 `paddle.io.Dataset` 基类 ,并且实现了 `__init__`,`__getitem__` 和 `__len__` 三个函数。\n", "* 在 `__init__` 函数中完成了对标签文件的读取和解析,并将所有的图像路径 `image_path` 和对应的标签 `label` 存放到一个列表 `data_list` 中。\n", @@ -310,76 +296,62 @@ "\n", "\n", "\n", - "另外,在 `__init__` 函数和 `__getitem__` 函数中还可实现一些数据预处理操作,如对图像的翻转、裁剪、归一化等操作,最终返回处理好的单条数据(样本数据、对应的标签),该操作可增加图像数据多样性,对增强模型的泛化能力带来帮助。飞桨框架在 [paddle.vision.transforms](../..api/paddle/vision/Overview_cn.html#about-transforms) 下内置了几十种图像数据处理方法,详细使用方法可参考 [数据预处理](03_data_preprocessing_cn.html) 章节。\n", + "另外,在 `__init__` 函数和 `__getitem__` 函数中还可实现一些数据预处理操作,如对图像的翻转、裁剪、归一化等操作,最终返回处理好的单条数据(样本数据、对应的标签),该操作可增加图像数据多样性,对增强模型的泛化能力带来帮助。飞桨框架在 [paddle.vision.transforms](../..api/paddle/vision/Overview_cn.html#about-transforms) 下内置了几十种图像数据处理方法,详细使用方法可参考 [数据预处理](data_preprocessing_cn.html) 章节。\n", "\n", "和内置数据集类似,可以使用下面的代码直接对自定义数据集进行迭代读取。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 5, - "id": "9d1570a3", - "metadata": { - "scrolled": true - }, + "source": [ + "for data in train_custom_dataset:\n", + " image, label = data\n", + " print('shape of image: ',image.shape)\n", + " plt.title(str(label))\n", + " plt.imshow(image[0]) \n", + " break" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "shape of image: (1, 28, 28)\n" ] }, { + "output_type": "display_data", "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAEICAYAAACZA4KlAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAUp0lEQVR4nO3de3Cc1XkG8OfZlXyTLXxFKODEhjEY06QmCOM2hJIyIcC0GDpTJrRh3A6paIFMmOGPAm0nZEKntJOQMhQyKJhgWm6ZBopJDQE8JQwtOAjqYIO5GLDBQpYwvskXWdrdt39oTQXoe4/Yb3e/dc7zm9FY2ne/3eOVHn2rffecQzODiPzmy2U9ABGpD4VdJBIKu0gkFHaRSCjsIpFQ2EUiobCLREJhlzGRfIrkIMm95Y/Xsh6TpKOwi+dKM5ta/jgh68FIOgq7SCQUdvH8A8ntJP+b5JlZD0bSod4bL2MheRqAVwAMAfg6gH8BsNjM3sx0YFIxhV3GheRjAP7TzG7JeixSGT2Nl/EyAMx6EFI5hV0+geR0kl8jOYlkE8k/BXAGgMeyHptUrinrAUhDagZwA4CFAIoAXgVwgZm9numoJBX9zS4SCT2NF4mEwi4SCYVdJBIKu0gk6vpq/AROtEloqedd/r9ghzh0BeeFTL3GWRv0vycM1K1USj622f/Rt+GCW29Ug9iHITs45gOTKuwkzwFwM4A8gDvM7Ebv+pPQgtN4Vpo7rPzQfN6/QqheLCaWrJDxD0aKxwVpuzGh+05x+2ye4NcnNLv10r59ibWm2W3usYVtfW69Ua21NYm1ip/Gk8wDuBXAuQAWAbiY5KJKb09EaivN3+xLAGwys7fMbAjA/QCWVWdYIlJtacJ+NIB3R329tXzZR5DsJNlNsnsYB1PcnYikUfNX482s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PbySwDsLL8+UqM/LDUXcLYGoKZ9ZrZi+XPBwAc2mY808fOGVddZBH2owG8O+rrrWis/d4NwOMkXyDZmfVgxtBmZr3lz7cBSF7nKxvBbbzr6WPbjDfMY1fJ9udp6QW6TzrdzL4I4FwAV5SfrjYkG/kbrJF6p+Paxrtexthm/ENZPnaVbn+eVhZh7wEwd9TXx5Qvawhm1lP+tx/AQ2i8raj7Du2gW/63P+PxfKiRtvEea5txNMBjl+X251mE/XkAC0jOJzkBwNcBrMpgHJ9AsqX8wglItgA4G423FfUqAMvLny8H8HCGY/mIRtnGO2mbcWT82GW+/bmZ1f0DwHkYeUX+TQB/k8UYEsZ1LIBflz9eznpsAO7DyNO6YYy8tnEpgFkA1gB4A8CTAGY20Nj+FcB6AC9hJFjtGY3tdIw8RX8JwLryx3lZP3bOuOryuOntsiKR0At0IpFQ2EUiobCLREJhF4mEwi4SCYVdJBIKu0gk/g81xM9ks5Ld8AAAAABJRU5ErkJggg==" }, "metadata": { "needs_background": "light" - }, - "output_type": "display_data" + } } ], - "source": [ - "for data in train_custom_dataset:\n", - " image, label = data\n", - " print('shape of image: ',image.shape)\n", - " plt.title(str(label))\n", - " plt.imshow(image[0]) \n", - " break" - ] + "metadata": { + "scrolled": true + } }, { "cell_type": "markdown", - "id": "de3fd19b", - "metadata": {}, "source": [ "## 二、迭代读取数据集\n", "\n", "### 2.1 使用 paddle.io.DataLoader 定义数据读取器\n", "\n", "通过前面介绍的直接迭代读取 Dataset 的方式虽然可实现对数据集的访问,但是这种访问方式只能单线程进行并且还需要手动分批次(batch)。在飞桨框架中,推荐使用 [paddle.io.DataLoader](../../api/paddle/io/DataLoader_cn.html#dataloader) API 对数据集进行多进程的读取,并且可自动完成划分 batch 的工作。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 7, - "id": "c3ad4116", - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "batch_id: 0, 训练数据shape: [64, 1, 28, 28], 标签数据shape: [64]\n" - ] - } - ], "source": [ "# 定义并初始化数据读取器\n", "train_loader = paddle.io.DataLoader(train_custom_dataset, batch_size=64, shuffle=True, num_workers=1, drop_last=True)\n", @@ -389,12 +361,22 @@ " images, labels = data\n", " print(\"batch_id: {}, 训练数据shape: {}, 标签数据shape: {}\".format(batch_id, images.shape, labels.shape))\n", " break" - ] + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "batch_id: 0, 训练数据shape: [64, 1, 28, 28], 标签数据shape: [64]\n" + ] + } + ], + "metadata": { + "scrolled": true + } }, { "cell_type": "markdown", - "id": "ae21b353", - "metadata": {}, "source": [ "通过上述方法,初始化了一个数据读取器 `train_loader`,用于加载训练数据集 `custom_dataset`。在数据读取器中几个常用的字段如下:\n", "\n", @@ -404,18 +386,17 @@ "* `num_workers`:**同步/异步读取数据**,通过 `num_workers` 来设置加载数据的子进程个数,num_workers的值设为大于0时,即开启多进程方式异步加载数据,可提升数据读取速度。\n", "\n", "\n", - "定义好数据读取器之后,便可用 for 循环方便地迭代读取批次数据,用于模型训练了。值得注意的是,如果使用高层 API 的 [paddle.Model.fit](../../api/paddle/Model_cn.html#fit-train-data-none-eval-data-none-batch-size-1-epochs-1-eval-freq-1-log-freq-10-save-dir-none-save-freq-1-verbose-2-drop-last-false-shuffle-true-num-workers-0-callbacks-none) 读取数据集进行训练,则只需定义数据集 Dataset 即可,不需要再单独定义 DataLoader,因为 paddle.Model.fit 中实际已经封装了一部分 DataLoader 的功能,详细可参考 [模型训练、评估与推理](05_train_eval_predict_cn.html) 章节。\n", + "定义好数据读取器之后,便可用 for 循环方便地迭代读取批次数据,用于模型训练了。值得注意的是,如果使用高层 API 的 [paddle.Model.fit](../../api/paddle/Model_cn.html#fit-train-data-none-eval-data-none-batch-size-1-epochs-1-eval-freq-1-log-freq-10-save-dir-none-save-freq-1-verbose-2-drop-last-false-shuffle-true-num-workers-0-callbacks-none) 读取数据集进行训练,则只需定义数据集 Dataset 即可,不需要再单独定义 DataLoader,因为 paddle.Model.fit 中实际已经封装了一部分 DataLoader 的功能,详细可参考 [模型训练、评估与推理](train_eval_predict_cn.html) 章节。\n", "\n", "\n", "\n", "> 注:\n", "> DataLoader 实际上是通过批采样器 BatchSampler 产生的批次索引列表,并根据索引取得 Dataset 中的对应样本数据,以实现批次数据的加载。DataLoader 中定义了采样的批次大小、顺序等信息,对应字段包括 `batch_size`、`shuffle`、`drop_last`。这三个字段也可以用一个 `batch_sampler` 字段代替,并在 `batch_sampler` 中传入自定义的批采样器实例。以上两种方式二选一即可,可实现相同的效果。下面小节中介绍后一种自定义采样器的使用方法,该用法可以更灵活地定义采样规则。\n" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "4c387f37", - "metadata": {}, "source": [ "### 2.2 (可选)自定义采样器\n", "\n", @@ -427,27 +408,12 @@ "下面通过两段示例代码,介绍采样器的用法。\n", "\n", "首先,以 BatchSampler 为例,介绍在 DataLoader 中使用 BatchSampler 获取采样数据的方法。\n" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 8, - "id": "477c89ef", - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "BatchSampler 每轮迭代返回一个索引列表\n", - "[53486, 39208, 42267, 46762, 33087, 54705, 55986, 20736]\n", - "在 DataLoader 中使用 BatchSampler,返回索引对应的一组样本和标签数据 \n", - "batch_id: 0, 训练数据shape: [8, 1, 28, 28], 标签数据shape: [8]\n" - ] - } - ], "source": [ "from paddle.io import BatchSampler\n", "\n", @@ -467,62 +433,36 @@ " images, labels = data\n", " print(\"batch_id: {}, 训练数据shape: {}, 标签数据shape: {}\".format(batch_id, images.shape, labels.shape))\n", " break" - ] + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "BatchSampler 每轮迭代返回一个索引列表\n", + "[53486, 39208, 42267, 46762, 33087, 54705, 55986, 20736]\n", + "在 DataLoader 中使用 BatchSampler,返回索引对应的一组样本和标签数据 \n", + "batch_id: 0, 训练数据shape: [8, 1, 28, 28], 标签数据shape: [8]\n" + ] + } + ], + "metadata": { + "scrolled": true + } }, { "cell_type": "markdown", - "id": "a503bc9a", - "metadata": {}, "source": [ "以上示例代码中,定义了一个批采样器实例 `bs`,每轮迭代会返回一个 `batch_size` 大小的索引列表(示例中一轮迭代返回 8 个索引值),数据读取器 `train_loader` 通过 `batch_sampler=bs` 字段传入批采样器,即可根据这些索引获取对应的一组样本数据。另外可以看到,`batch_size`、`shuffle`、`drop_last`这三个参数只在 BatchSampler 中设定。\n", "\n", "\n", "下面再通过一段代码示例,对比几个不同采样器的采样行为。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 9, - "id": "7d4c4622-b19a-47ad-a603-fc53c44650fa", - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "-----------------顺序采样----------------\n", - "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", - "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n", - "[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]\n", - "[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]\n", - "[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]\n", - "[50, 51, 52, 53, 54, 55, 56, 57, 58, 59]\n", - "[60, 61, 62, 63, 64, 65, 66, 67, 68, 69]\n", - "[70, 71, 72, 73, 74, 75, 76, 77, 78, 79]\n", - "[80, 81, 82, 83, 84, 85, 86, 87, 88, 89]\n", - "[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]\n", - "-----------------随机采样----------------\n", - "[44, 29, 37, 11, 21, 53, 65, 3, 26, 23]\n", - "[17, 4, 48, 84, 86, 90, 92, 76, 97, 69]\n", - "[35, 51, 71, 45, 25, 38, 32, 83, 22, 57]\n", - "[47, 55, 39, 46, 78, 61, 68, 66, 18, 41]\n", - "[77, 81, 15, 63, 91, 54, 24, 75, 59, 99]\n", - "[73, 88, 20, 43, 93, 56, 95, 60, 87, 72]\n", - "[70, 98, 1, 64, 0, 16, 33, 14, 80, 89]\n", - "[36, 40, 62, 50, 9, 34, 8, 19, 82, 6]\n", - "[74, 27, 30, 58, 31, 28, 12, 13, 7, 49]\n", - "[10, 52, 2, 94, 67, 96, 79, 42, 5, 85]\n", - "-----------------分布式采样----------------\n", - "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", - "[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]\n", - "[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]\n", - "[60, 61, 62, 63, 64, 65, 66, 67, 68, 69]\n", - "[80, 81, 82, 83, 84, 85, 86, 87, 88, 89]\n" - ] - } - ], "source": [ "from paddle.io import SequenceSampler, RandomSampler, BatchSampler, DistributedBatchSampler\n", "\n", @@ -559,23 +499,59 @@ "\n", "for index in batch_sampler:\n", " print(index)" - ] + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "-----------------顺序采样----------------\n", + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n", + "[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]\n", + "[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]\n", + "[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]\n", + "[50, 51, 52, 53, 54, 55, 56, 57, 58, 59]\n", + "[60, 61, 62, 63, 64, 65, 66, 67, 68, 69]\n", + "[70, 71, 72, 73, 74, 75, 76, 77, 78, 79]\n", + "[80, 81, 82, 83, 84, 85, 86, 87, 88, 89]\n", + "[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]\n", + "-----------------随机采样----------------\n", + "[44, 29, 37, 11, 21, 53, 65, 3, 26, 23]\n", + "[17, 4, 48, 84, 86, 90, 92, 76, 97, 69]\n", + "[35, 51, 71, 45, 25, 38, 32, 83, 22, 57]\n", + "[47, 55, 39, 46, 78, 61, 68, 66, 18, 41]\n", + "[77, 81, 15, 63, 91, 54, 24, 75, 59, 99]\n", + "[73, 88, 20, 43, 93, 56, 95, 60, 87, 72]\n", + "[70, 98, 1, 64, 0, 16, 33, 14, 80, 89]\n", + "[36, 40, 62, 50, 9, 34, 8, 19, 82, 6]\n", + "[74, 27, 30, 58, 31, 28, 12, 13, 7, 49]\n", + "[10, 52, 2, 94, 67, 96, 79, 42, 5, 85]\n", + "-----------------分布式采样----------------\n", + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + "[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]\n", + "[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]\n", + "[60, 61, 62, 63, 64, 65, 66, 67, 68, 69]\n", + "[80, 81, 82, 83, 84, 85, 86, 87, 88, 89]\n" + ] + } + ], + "metadata": { + "scrolled": true + } }, { "cell_type": "markdown", - "id": "970dad59", - "metadata": {}, "source": [ "从代码输出结果可以看出:\n", "* 顺序采样:按照顺序的方式输出各个样本的索引。\n", "* 随机采样:先将样本顺序打乱,再输出乱序后的样本索引。\n", "* 分布式采样:常用于分布式训练场景,将样本数据切分成多份,分别放到不同卡上训练。示例中设置了 `num_replicas=2`,样本会被划分到两张卡上,所以这里只输出一半样本的索引。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "d3a256d5-33f0-4018-bd5a-3ee6d9bff372", - "metadata": {}, "source": [ "## 三、总结\n", "\n", @@ -585,10 +561,11 @@ "\n", "图 1:数据集定义和加载流程\n", "\n", - "主要包括定义数据集和定义数据读取器两个步骤,另外在数据读取器中可调用采样器实现更灵活地采样。其中,在定义数据集时,本节仅对数据集进行了归一化处理,如需了解更多数据增强相关操作,可以参考 [数据预处理](03_data_preprocessing_cn.html)。 \n", + "主要包括定义数据集和定义数据读取器两个步骤,另外在数据读取器中可调用采样器实现更灵活地采样。其中,在定义数据集时,本节仅对数据集进行了归一化处理,如需了解更多数据增强相关操作,可以参考 [数据预处理](data_preprocessing_cn.html)。 \n", "\n", - "以上所有数据处理工作完成后,即可进入下一个任务:[模型训练、评估与推理](05_train_eval_predict_cn.html)。" - ] + "以上所有数据处理工作完成后,即可进入下一个任务:[模型训练、评估与推理](train_eval_predict_cn.html)。" + ], + "metadata": {} } ], "metadata": { @@ -612,4 +589,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} +} \ No newline at end of file diff --git a/docs/guides/02_paddle2.0_develop/03_data_preprocessing_cn.ipynb b/docs/guides/beginner/data_preprocessing_cn.ipynb similarity index 100% rename from docs/guides/02_paddle2.0_develop/03_data_preprocessing_cn.ipynb rename to docs/guides/beginner/data_preprocessing_cn.ipynb diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/images/Axis_2.0.png b/docs/guides/beginner/images/Axis_2.0.png similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/images/Axis_2.0.png rename to docs/guides/beginner/images/Axis_2.0.png diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/images/Tensor_2.0.png b/docs/guides/beginner/images/Tensor_2.0.png similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/images/Tensor_2.0.png rename to docs/guides/beginner/images/Tensor_2.0.png diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/images/Tensor_broadcast.png b/docs/guides/beginner/images/Tensor_broadcast.png similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/images/Tensor_broadcast.png rename to docs/guides/beginner/images/Tensor_broadcast.png diff --git a/docs/guides/02_paddle2.0_develop/images/data_pipeline.png b/docs/guides/beginner/images/data_pipeline.png similarity index 100% rename from docs/guides/02_paddle2.0_develop/images/data_pipeline.png rename to docs/guides/beginner/images/data_pipeline.png diff --git a/docs/guides/02_paddle2.0_develop/images/data_preprocessing.png b/docs/guides/beginner/images/data_preprocessing.png similarity index 100% rename from docs/guides/02_paddle2.0_develop/images/data_preprocessing.png rename to docs/guides/beginner/images/data_preprocessing.png diff --git a/docs/guides/02_paddle2.0_develop/images/lenet.png b/docs/guides/beginner/images/lenet.png similarity index 100% rename from docs/guides/02_paddle2.0_develop/images/lenet.png rename to docs/guides/beginner/images/lenet.png diff --git a/docs/guides/02_paddle2.0_develop/images/mnist.png b/docs/guides/beginner/images/mnist.png similarity index 100% rename from docs/guides/02_paddle2.0_develop/images/mnist.png rename to docs/guides/beginner/images/mnist.png diff --git a/docs/guides/02_paddle2.0_develop/images/model.png b/docs/guides/beginner/images/model.png similarity index 100% rename from docs/guides/02_paddle2.0_develop/images/model.png rename to docs/guides/beginner/images/model.png diff --git a/docs/guides/02_paddle2.0_develop/images/model_develop_flow.png b/docs/guides/beginner/images/model_develop_flow.png similarity index 100% rename from docs/guides/02_paddle2.0_develop/images/model_develop_flow.png rename to docs/guides/beginner/images/model_develop_flow.png diff --git a/docs/guides/02_paddle2.0_develop/images/paddle_jit_save_load_2.1.png b/docs/guides/beginner/images/paddle_jit_save_load_2.1.png similarity index 100% rename from docs/guides/02_paddle2.0_develop/images/paddle_jit_save_load_2.1.png rename to docs/guides/beginner/images/paddle_jit_save_load_2.1.png diff --git a/docs/guides/02_paddle2.0_develop/images/paddle_save_load_2.1.png b/docs/guides/beginner/images/paddle_save_load_2.1.png similarity index 100% rename from docs/guides/02_paddle2.0_develop/images/paddle_save_load_2.1.png rename to docs/guides/beginner/images/paddle_save_load_2.1.png diff --git a/docs/guides/beginner/index_cn.rst b/docs/guides/beginner/index_cn.rst new file mode 100644 index 00000000000..6de9626265a --- /dev/null +++ b/docs/guides/beginner/index_cn.rst @@ -0,0 +1,28 @@ +################### +模型开发入门 +################### + +本部分将介绍飞桨框架2.0的开发流程。 + +为了快速上手飞桨框架2.0,你可以参考 `10分钟快速上手飞桨 <./quick_start_cn.html>`_ ; + +当完成了快速上手的任务后,下面这些模块会阐述如何用飞桨框架2.0,实现深度学习过程中的每一步。具体包括: + +- `Tensor 介绍 <./tensor_cn.html>`_ : 介绍飞桨基本数据类型 `Tensor` 的概念与常见用法。 +- `数据集定义与加载 <./data_load_cn.html>`_ : 飞桨框架数据加载的方式,主要为\ ``paddle.io.Dataset + paddle.io.DataLoader``\ ,以及飞桨内置数据集的介绍。 +- `数据预处理 <./data_preprocessing_cn.html>`_ : 飞桨框架数据预处理的方法,主要是\ ``paddle.vision.transform.*``\ 。 +- `模型组网 <./model_cn.html>`_ : 飞桨框架组网API的介绍,主要是\ ``paddle.nn.*``\ ,然后是飞桨框架组网方式的介绍,即 Sequential 的组网与 SubClass 的组网。 +- `训练与预测 <./train_eval_predict_cn.html>`_ : 飞桨框架训练与预测的方法,有两种方式,一种是使用高层API\ ``paddle.Model``\ 封装模型,然后调用\ ``model.fit()、model.evaluate()、model.predict()``\ 完成模型的训练与预测;另一种是用基础API完成模型的训练与预测,也就是对高层API的拆解。 +- `模型的加载与保存 <./model_save_load_cn.html>`_ : 飞桨框架模型的加载与保存体系介绍。 + +.. toctree:: + :hidden: + + quick_start_cn.ipynb + tensor_cn.md + data_load_cn.ipynb + data_preprocessing_cn.ipynb + model_cn.ipynb + train_eval_predict_cn.rst + model_save_load_cn.rst + diff --git a/docs/guides/beginner/index_en.rst b/docs/guides/beginner/index_en.rst new file mode 100644 index 00000000000..403bb7c6dff --- /dev/null +++ b/docs/guides/beginner/index_en.rst @@ -0,0 +1,12 @@ +################### +Model Development +################### + + +- `Introduction of Tensor <./tensor_en.html>`_ + +.. toctree:: + :hidden: + + tensor_en.md + diff --git a/docs/guides/02_paddle2.0_develop/04_model_cn.ipynb b/docs/guides/beginner/model_cn.ipynb similarity index 100% rename from docs/guides/02_paddle2.0_develop/04_model_cn.ipynb rename to docs/guides/beginner/model_cn.ipynb diff --git a/docs/guides/02_paddle2.0_develop/08_model_save_load_cn.rst b/docs/guides/beginner/model_save_load_cn.rst similarity index 100% rename from docs/guides/02_paddle2.0_develop/08_model_save_load_cn.rst rename to docs/guides/beginner/model_save_load_cn.rst diff --git a/docs/guides/02_paddle2.0_develop/01_quick_start_cn.ipynb b/docs/guides/beginner/quick_start_cn.ipynb similarity index 94% rename from docs/guides/02_paddle2.0_develop/01_quick_start_cn.ipynb rename to docs/guides/beginner/quick_start_cn.ipynb index 3dda15b4f3a..3b06e5fcc3b 100644 --- a/docs/guides/02_paddle2.0_develop/01_quick_start_cn.ipynb +++ b/docs/guides/beginner/quick_start_cn.ipynb @@ -2,8 +2,6 @@ "cells": [ { "cell_type": "markdown", - "id": "f848b574", - "metadata": {}, "source": [ "\n", "# 10分钟快速上手飞桨\n", @@ -15,12 +13,17 @@ "如果已经安装好飞桨那么可以跳过此步骤。飞桨支持很多种安装方式,这里介绍其中一种简单的安装命令。\n", "\n", "> 注:目前飞桨支持 Python 3.6 ~ 3.9 版本,pip3 要求 20.2.2 或更高版本,请提前安装对应版本的 Python 和 pip 工具。\n" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 2, - "id": "e7d9b5cf-fffe-4086-b84d-3f58eee2f602", + "source": [ + "# 使用 pip 工具安装飞桨 CPU 版\n", + "! python3 -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple" + ], + "outputs": [], "metadata": { "execution": { "iopub.execute_input": "2021-12-20T07:51:58.100025Z", @@ -30,17 +33,10 @@ "shell.execute_reply.started": "2021-12-20T07:51:58.099995Z" }, "scrolled": true - }, - "outputs": [], - "source": [ - "# 使用 pip 工具安装飞桨 CPU 版\n", - "! python3 -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple" - ] + } }, { "cell_type": "markdown", - "id": "54e32a09-1e8f-4bc6-a3bd-1eebd621fa66", - "metadata": {}, "source": [ "该命令用于安装 CPU 版本的飞桨。如果要安装其他计算平台或操作系统支持的版本,可点击 [ 快速安装]( ) 查看安装引导。\n", "\n", @@ -49,12 +45,25 @@ "安装完成后,需要在Python解释器中使用 import 导入飞桨,即可开始实践深度学习任务。\n", "\n", "若操作成功,会输出飞桨的版本号。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 3, - "id": "468426ec", + "source": [ + "import paddle \n", + "print(paddle.__version__)" + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "2.2.1\n" + ] + } + ], "metadata": { "execution": { "iopub.execute_input": "2021-12-16T04:18:43.298378Z", @@ -64,25 +73,10 @@ "shell.execute_reply.started": "2021-12-16T04:18:43.298346Z" }, "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2.2.1\n" - ] - } - ], - "source": [ - "import paddle \n", - "print(paddle.__version__)" - ] + } }, { "cell_type": "markdown", - "id": "a3e0d5e5", - "metadata": {}, "source": [ "\n", "\n", @@ -97,12 +91,17 @@ "图 1:MNIST 数据集样例\n", "\n", "开始之前,需要使用下面的命令安装 Python 的 matplotlib 库和 numpy 库,matplotlib 库用于可视化图片,numpy 库用于处理数据。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 3, - "id": "0b50ba8c-93dd-42d9-8b52-35025ecb122b", + "source": [ + "# 使用 pip 工具安装 matplotlib 和 numpy\n", + "! python3 -m pip install matplotlib numpy -i https://mirror.baidu.com/pypi/simple" + ], + "outputs": [], "metadata": { "execution": { "iopub.execute_input": "2021-12-16T06:31:04.356444Z", @@ -112,80 +111,18 @@ "shell.execute_reply.started": "2021-12-16T06:31:04.356403Z" }, "scrolled": true - }, - "outputs": [], - "source": [ - "# 使用 pip 工具安装 matplotlib 和 numpy\n", - "! python3 -m pip install matplotlib numpy -i https://mirror.baidu.com/pypi/simple" - ] + } }, { "cell_type": "markdown", - "id": "7ba0ec0a-bc6c-43cc-8478-488cc231bca8", - "metadata": {}, "source": [ "下面是手写数字识别任务的完整代码,如果想直接运行代码,可以拷贝下面的完整代码到一个Python文件中运行。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 5, - "id": "93d0c8a1", - "metadata": { - "execution": { - "iopub.execute_input": "2021-12-16T06:31:11.340019Z", - "iopub.status.busy": "2021-12-16T06:31:11.338995Z", - "iopub.status.idle": "2021-12-16T06:33:13.307650Z", - "shell.execute_reply": "2021-12-16T06:33:13.306938Z", - "shell.execute_reply.started": "2021-12-16T06:31:11.339980Z" - }, - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The loss value printed in the log is the current step, and the metric is the average value of previous steps.\n", - "Epoch 1/5\n", - "step 938/938 [==============================] - loss: 0.0519 - acc: 0.9344 - 14ms/step \n", - "Epoch 2/5\n", - "step 938/938 [==============================] - loss: 0.0239 - acc: 0.9767 - 14ms/step \n", - "Epoch 3/5\n", - "step 938/938 [==============================] - loss: 0.0416 - acc: 0.9811 - 14ms/step \n", - "Epoch 4/5\n", - "step 938/938 [==============================] - loss: 0.0084 - acc: 0.9837 - 14ms/step \n", - "Epoch 5/5\n", - "step 938/938 [==============================] - loss: 0.0838 - acc: 0.9860 - 14ms/step \n", - "Eval begin...\n", - "step 157/157 [==============================] - loss: 1.7577e-04 - acc: 0.9844 - 6ms/step \n", - "Eval samples: 10000\n", - "true label: 7, pred label: 7\n" - ] - }, - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - }, - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAANiklEQVR4nO3df4wc9XnH8c8n/kV8QGtDcF3j4ISQqE4aSHWBRNDKESUFImSiJBRLtVyJ5lALElRRW0QVBalVSlEIok0aySluHESgaQBhJTSNa6W1UKljg4yxgdaEmsau8QFOaxPAP/DTP24cHXD7vWNndmft5/2SVrs7z87Oo/F9PLMzO/t1RAjA8e9tbTcAoD8IO5AEYQeSIOxAEoQdSGJ6Pxc207PiBA31c5FAKq/qZzoYBzxRrVbYbV8s6XZJ0yT9bUTcXHr9CRrSeb6wziIBFGyIdR1rXe/G254m6auSLpG0WNIy24u7fT8AvVXnM/u5kp6OiGci4qCkeyQtbaYtAE2rE/YFkn4y7vnOatr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ata": { - "needs_background": "light" - }, - "output_type": "display_data" - } - ], "source": [ "import paddle\n", "import numpy as np\n", @@ -227,12 +164,65 @@ "# 可视化图片\n", "from matplotlib import pyplot as plt\n", "plt.imshow(img[0])" - ] + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The loss value printed in the log is the current step, and the metric is the average value of previous steps.\n", + "Epoch 1/5\n", + "step 938/938 [==============================] - loss: 0.0519 - acc: 0.9344 - 14ms/step \n", + "Epoch 2/5\n", + "step 938/938 [==============================] - loss: 0.0239 - acc: 0.9767 - 14ms/step \n", + "Epoch 3/5\n", + "step 938/938 [==============================] - loss: 0.0416 - acc: 0.9811 - 14ms/step \n", + "Epoch 4/5\n", + "step 938/938 [==============================] - loss: 0.0084 - acc: 0.9837 - 14ms/step \n", + "Epoch 5/5\n", + "step 938/938 [==============================] - loss: 0.0838 - acc: 0.9860 - 14ms/step \n", + "Eval begin...\n", + "step 157/157 [==============================] - loss: 1.7577e-04 - acc: 0.9844 - 6ms/step \n", + "Eval samples: 10000\n", + "true label: 7, pred label: 7\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 5 + }, + { + "output_type": "display_data", + "data": { + "text/plain": [ + "
" + ], + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAANiklEQVR4nO3df4wc9XnH8c8n/kV8QGtDcF3j4ISQqE4aSHWBRNDKESUFImSiJBRLtVyJ5lALElRRW0QVBalVSlEIok0aySluHESgaQBhJTSNa6W1UKljg4yxgdaEmsau8QFOaxPAP/DTP24cHXD7vWNndmft5/2SVrs7z87Oo/F9PLMzO/t1RAjA8e9tbTcAoD8IO5AEYQeSIOxAEoQdSGJ6P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mjtAvd0p6XFJWzQWrPkt9XaBxnbRt0jaXN0ubXvdFfrqy3rj67JAEhygA5Ig7EAShB1IgrADSRB2IAnCDiRB2IEk/h9BCfQTovZf9wAAAABJRU5ErkJggg==" + }, + "metadata": { + "needs_background": "light" + } + } + ], + "metadata": { + "execution": { + "iopub.execute_input": "2021-12-16T06:31:11.340019Z", + "iopub.status.busy": "2021-12-16T06:31:11.338995Z", + "iopub.status.idle": "2021-12-16T06:33:13.307650Z", + "shell.execute_reply": "2021-12-16T06:33:13.306938Z", + "shell.execute_reply.started": "2021-12-16T06:31:11.339980Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "f1be5eec", - "metadata": {}, "source": [ "以上代码使用 MNIST 数据集训练并测试了 LeNet 模型,并最终成功推理出了一张手写数字图片的标签,该图片推理结果是 7 ( pred label: 7),真实标签也是7 (true label: 7)。\n", "\n", @@ -245,41 +235,21 @@ "4. 模型推理\n", "\n", "接下来逐个步骤介绍,帮助你快速掌握使用飞桨框架实践深度学习任务的方法。\n" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "9fdc68f2-fe82-45f0-8022-e180a5960bf7", - "metadata": {}, "source": [ "### 3.1 数据集定义与加载\n", "\n", "飞桨在 [paddle.vision.datasets](../../api/paddle/vision/Overview_cn.html#api) 下内置了计算机视觉(Computer Vision,CV)领域常见的数据集,如 MNIST、Cifar10、Cifar100、FashionMNIST 和 VOC2012 等。在本任务中,先后加载了 MNIST 训练集(`mode='train'`)和测试集(`mode='test'`),训练集用于训练模型,测试集用于评估模型效果。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 2, - "id": "f99c914f", - "metadata": { - "execution": { - "iopub.execute_input": "2021-12-17T06:55:43.185059Z", - "iopub.status.busy": "2021-12-17T06:55:43.184142Z", - "iopub.status.idle": "2021-12-17T06:55:53.018229Z", - "shell.execute_reply": "2021-12-17T06:55:53.017346Z", - "shell.execute_reply.started": "2021-12-17T06:55:43.185027Z" - }, - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "60000 images in train_dataset, 10000 images in test_dataset\n" - ] - } - ], "source": [ "import paddle\n", "from paddle.vision.transforms import Normalize\n", @@ -291,12 +261,29 @@ "\n", "# 打印数据集里图片数量\n", "print('{} images in train_dataset, {} images in test_dataset'.format(len(train_dataset), len(test_dataset)))" - ] + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "60000 images in train_dataset, 10000 images in test_dataset\n" + ] + } + ], + "metadata": { + "execution": { + "iopub.execute_input": "2021-12-17T06:55:43.185059Z", + "iopub.status.busy": "2021-12-17T06:55:43.184142Z", + "iopub.status.idle": "2021-12-17T06:55:53.018229Z", + "shell.execute_reply": "2021-12-17T06:55:53.017346Z", + "shell.execute_reply.started": "2021-12-17T06:55:43.185027Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "2d89cb67", - "metadata": {}, "source": [ "飞桨除了内置了 CV 领域常见的数据集,还在 [paddle.text](../../api/paddle/text/Overview_cn.html#api) 下内置了自然语言处理(Natural Language Processing,NLP)领域常见的数据集,并提供了自定义数据集与加载功能的 [paddle.io.Dataset](../../api/paddle/io/Dataset_cn.html#dataset) 和 [paddle.io.DataLoader](../../api/paddle/io/DataLoader_cn.html#dataloader) API,详细使用方法可参考『数据集定义与加载』 章节。\n", "\n", @@ -305,14 +292,13 @@ "\n", "\n", "更多参考:\n", - "* [数据集定义与加载](02_data_load_cn.html)\n", - "* [数据预处理](03_data_preprocessing_cn.html)\n" - ] + "* [数据集定义与加载](data_load_cn.html)\n", + "* [数据预处理](data_preprocessing_cn.html)\n" + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "6ba82de9", - "metadata": {}, "source": [ "### 3.2 模型组网\n", "\n", @@ -322,26 +308,23 @@ "『手写数字识别任务』比较简单,普通的神经网络就能达到很高的精度,在本任务中使用了飞桨内置的 LeNet 作为模型。飞桨在 [paddle.vision.models](../../api/paddle/vision/Overview_cn.html#about-models) 下内置了 CV 领域的一些经典模型,LeNet 就是其中之一,调用很方便,只需一行代码即可完成 LeNet 的网络构建和初始化。`num_classes` 字段中定义分类的类别数,因为需要对 0 ~ 9 的十类数字进行分类,所以设置为 10。\n", "\n", "另外通过 [paddle.summary](../../api/paddle/summary_cn.html#summary) 可方便地打印网络的基础结构和参数信息。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 3, - "id": "e8bf3841", - "metadata": { - "execution": { - "iopub.execute_input": "2021-12-17T06:59:47.978121Z", - "iopub.status.busy": "2021-12-17T06:59:47.977323Z", - "iopub.status.idle": "2021-12-17T06:59:47.990123Z", - "shell.execute_reply": "2021-12-17T06:59:47.989596Z", - "shell.execute_reply.started": "2021-12-17T06:59:47.978088Z" - }, - "scrolled": true - }, + "source": [ + "# 模型组网并初始化网络\n", + "lenet = paddle.vision.models.LeNet(num_classes=10)\n", + "\n", + "# 可视化模型组网结构和参数\n", + "paddle.summary(lenet,(1, 1, 28, 28))" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "---------------------------------------------------------------------------\n", " Layer (type) Input Shape Output Shape Param # \n", @@ -369,39 +352,39 @@ ] }, { + "output_type": "execute_result", "data": { "text/plain": [ "{'total_params': 61610, 'trainable_params': 61610}" ] }, - "execution_count": 3, "metadata": {}, - "output_type": "execute_result" + "execution_count": 3 } ], - "source": [ - "# 模型组网并初始化网络\n", - "lenet = paddle.vision.models.LeNet(num_classes=10)\n", - "\n", - "# 可视化模型组网结构和参数\n", - "paddle.summary(lenet,(1, 1, 28, 28))" - ] + "metadata": { + "execution": { + "iopub.execute_input": "2021-12-17T06:59:47.978121Z", + "iopub.status.busy": "2021-12-17T06:59:47.977323Z", + "iopub.status.idle": "2021-12-17T06:59:47.990123Z", + "shell.execute_reply": "2021-12-17T06:59:47.989596Z", + "shell.execute_reply.started": "2021-12-17T06:59:47.978088Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "67dfcc50", - "metadata": {}, "source": [ "通过飞桨的 [paddle.nn.Sequential](../../api/paddle/nn/Sequential_cn.html) 和 [paddle.nn.Layer](../../api/paddle/nn/Layer_cn.html) API 可以更灵活方便的组建自定义的神经网络,详细使用方法可参考『模型组网』章节。\n", "\n", "更多参考:\n", - "* [模型组网](04_model_cn.html)" - ] + "* [模型组网](model_cn.html)" + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "4902817f", - "metadata": {}, "source": [ "### 3.3 模型训练与评估\n", "\n", @@ -415,26 +398,28 @@ "\n", "\n", "因为是分类任务,这里损失函数使用常见的 [CrossEntropyLoss](../../api/paddle/nn/CrossEntropyLoss_cn.html#crossentropyloss) (交叉熵损失函数),优化器使用 [Adam](../../api/paddle/optimizer/Adam_cn.html#adam),评价指标使用 [Accuracy](../../api/paddle/metric/Accuracy_cn.html#accuracy) 来计算模型在训练集上的精度。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 5, - "id": "3333a7bb", - "metadata": { - "execution": { - "iopub.execute_input": "2021-12-17T04:01:43.380962Z", - "iopub.status.busy": "2021-12-17T04:01:43.380575Z", - "iopub.status.idle": "2021-12-17T04:03:17.852495Z", - "shell.execute_reply": "2021-12-17T04:03:17.851918Z", - "shell.execute_reply.started": "2021-12-17T04:01:43.380928Z" - }, - "scrolled": true - }, + "source": [ + "# 封装模型,便于进行后续的训练、评估和推理\n", + "model = paddle.Model(lenet)\n", + "\n", + "# 模型训练的配置准备,准备损失函数,优化器和评价指标\n", + "model.prepare(paddle.optimizer.Adam(parameters=model.parameters()), \n", + " paddle.nn.CrossEntropyLoss(),\n", + " paddle.metric.Accuracy())\n", + "\n", + "# 开始训练\n", + "model.fit(train_dataset, epochs=5, batch_size=64, verbose=1)" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "The loss value printed in the log is the current step, and the metric is the average value of previous steps.\n", "Epoch 1/5\n", @@ -450,48 +435,44 @@ ] } ], - "source": [ - "# 封装模型,便于进行后续的训练、评估和推理\n", - "model = paddle.Model(lenet)\n", - "\n", - "# 模型训练的配置准备,准备损失函数,优化器和评价指标\n", - "model.prepare(paddle.optimizer.Adam(parameters=model.parameters()), \n", - " paddle.nn.CrossEntropyLoss(),\n", - " paddle.metric.Accuracy())\n", - "\n", - "# 开始训练\n", - "model.fit(train_dataset, epochs=5, batch_size=64, verbose=1)" - ] + "metadata": { + "execution": { + "iopub.execute_input": "2021-12-17T04:01:43.380962Z", + "iopub.status.busy": "2021-12-17T04:01:43.380575Z", + "iopub.status.idle": "2021-12-17T04:03:17.852495Z", + "shell.execute_reply": "2021-12-17T04:03:17.851918Z", + "shell.execute_reply.started": "2021-12-17T04:01:43.380928Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "c6604e7c", - "metadata": {}, "source": [ "从训练过程的打印日志中,可观察到损失函数值 loss 逐渐变小,精度 acc 逐渐上升的趋势,反映出不错的训练效果。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "684e7be6", - "metadata": {}, "source": [ "#### 3.3.2 模型评估\n", "\n", "模型训练完成之后,调用 [paddle.Model.evaluate](../../api/paddle/Model_cn.html#evaluate-eval-data-batch-size-1-log-freq-10-verbose-2-num-workers-0-callbacks-none) ,使用预先定义的测试数据集,来评估训练好的模型效果,评估完成后将输出模型在测试集上的损失函数值 loss 和精度 acc。\n" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 6, - "id": "b86f0289", - "metadata": { - "scrolled": true - }, + "source": [ + "# 进行模型评估\n", + "model.evaluate(test_dataset, batch_size=64, verbose=1)" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Eval begin...\n", "step 157/157 [==============================] - loss: 5.7177e-04 - acc: 0.9859 - 6ms/step \n", @@ -499,57 +480,57 @@ ] }, { + "output_type": "execute_result", "data": { "text/plain": [ "{'loss': [0.00057177414], 'acc': 0.9859}" ] }, - "execution_count": 6, "metadata": {}, - "output_type": "execute_result" + "execution_count": 6 } ], - "source": [ - "# 进行模型评估\n", - "model.evaluate(test_dataset, batch_size=64, verbose=1)" - ] + "metadata": { + "scrolled": true + } }, { "cell_type": "markdown", - "id": "94c4a7af", - "metadata": {}, "source": [ "从结果可以看到,初步训练得到的模型精度在98%附近,在逐渐熟悉深度学习模型开发和训练技巧后,可以通过调整其中的训练参数来进一步提升模型的精度。\n", "\n", "更多参考:\n", - "* [模型训练、评估与推理](05_train_eval_predict_cn.html)" - ] + "* [模型训练、评估与推理](train_eval_predict_cn.html)" + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "e991757c", - "metadata": {}, "source": [ "### 3.4 模型推理\n", "\n" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "39ee250a", - "metadata": {}, "source": [ "#### 3.4.1 模型保存\n", "\n", "模型训练完成后,通常需要将训练好的模型参数和优化器等信息,持久化保存到参数文件中,便于后续执行推理验证。\n", "\n", "在飞桨中可通过调用 [paddle.Model.save](../../api/paddle/Model_cn.html#save-path-training-true) 保存模型。代码示例如下,其中 output 为模型保存的文件夹名称,minst 为保存的模型文件名称。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 5, - "id": "07bff4b4", + "source": [ + "# 保存模型,文件夹会自动创建\n", + "model.save('./output/mnist')" + ], + "outputs": [], "metadata": { "execution": { "iopub.execute_input": "2021-12-17T04:11:42.623618Z", @@ -559,17 +540,10 @@ "shell.execute_reply.started": "2021-12-17T04:11:42.623579Z" }, "scrolled": true - }, - "outputs": [], - "source": [ - "# 保存模型,文件夹会自动创建\n", - "model.save('./output/mnist')" - ] + } }, { "cell_type": "markdown", - "id": "0daaf2e3", - "metadata": {}, "source": [ "以上代码执行后会在`output`目录下保存两个文件,`mnist.pdopt`为优化器的参数,`mnist.pdparams`为模型的参数。\n", "```bash\n", @@ -577,90 +551,86 @@ "├── mnist.pdopt # 优化器的参数\n", "└── mnist.pdparams # 模型的参数\n", "```\n" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "e2d87664-6b04-4969-8af4-02f57356e1d4", - "metadata": {}, "source": [ "#### 3.4.2 模型加载并执行推理\n", "\n", "执行模型推理时,可调用 [paddle.Model.load](../../api/paddle/Model_cn.html#load-path-skip-mismatch-false-reset-optimizer-false) 加载模型,然后即可通过 [paddle.Model.predict_batch](../../api/paddle/Model_cn.html#predict-batch-inputs) 执行推理操作。\n", "\n", "如下示例中,针对前面创建的 `model` 网络加载保存的参数文件 `output/mnist`,并选择测试集中的一张图片 `test_dataset[0]` 作为输入,执行推理并打印结果,可以看到推理的结果与可视化图片一致。\n" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 10, - "id": "bb8328ef", - "metadata": { - "scrolled": true - }, + "source": [ + "# 加载模型\n", + "model.load('output/mnist')\n", + "\n", + "# 从测试集中取出一张图片\n", + "img, label = test_dataset[0]\n", + "# 将图片shape从1*28*28变为1*1*28*28,增加一个batch维度,以匹配模型输入格式要求\n", + "img_batch = np.expand_dims(img.astype('float32'), axis=0)\n", + "\n", + "# 执行推理并打印结果,此处predict_batch返回的是一个list,取出其中数据获得预测结果\n", + "out = model.predict_batch(img_batch)[0]\n", + "pred_label = out.argmax()\n", + "print('true label: {}, pred label: {}'.format(label[0], pred_label))\n", + "# 可视化图片\n", + "from matplotlib import pyplot as plt\n", + "plt.imshow(img[0])" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "true label: 7, pred label: 7\n" ] }, { + "output_type": "execute_result", "data": { "text/plain": [ "" ] }, - "execution_count": 10, "metadata": {}, - "output_type": "execute_result" + "execution_count": 10 }, { + "output_type": "display_data", "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAANiklEQVR4nO3df4wc9XnH8c8n/kV8QGtDcF3j4ISQqE4aSHWBRNDK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2pdEb93qS2+tfmYH0D9tb9kB9AlhB5JoJey2L7b9H7aftn1DGz10YnuH7cdtb7a9qeVeVtketb113LS5ttfa3l7dTzjGXku93WR7V7XuNtu+tKXeFtr+oe0nbG+zfV01vdV1V+irL+ut75/ZbU+T9J+SLpK0U9JGScsi4om+NtKB7R2ShiOi9S9g2P4NSS9J+mZEfKCadoukvRFxc/Uf5ZyI+JMB6e0mSS+1PYx3NVrR/PHDjEu6XNLvqsV1V+jrCvVhvbWxZT9X0tMR8UxEHJR0j6SlLfQx8CJivaS9b5i8VNLq6vFqjf2x9F2H3gZCROyOiEerx/slHR1mvNV1V+irL9oI+wJJPxn3fKcGa7z3kPQD24/YHmm7mQnMi4jd1ePnJM1rs5kJTDqMdz+9YZjxgVl33Qx/XhcH6N7sgoj4NUmXSLqm2l0dSDH2GWyQzp1OaRjvfplgmPGfa3PddTv8eV1thH2XpIXjnp9eTRsIEbGruh+VdL8GbyjqPUdH0K3uR1vu5+cGaRjviYYZ1wCsuzaHP28j7BslnWX7XbZnSrpS0poW+ngT20PVgRPZHpL0cQ3eUNRrJK2oHq+Q9ECLvbzOoAzj3WmYcbW87lof/jwi+n6TdKnGjsj/WNKfttFDh77eLemx6rat7d4k3a2x3bpDGju2cZWkUyStk7Rd0j9LmjtAvd0p6XFJWzQWrPkt9XaBxnbRt0jaXN0ubXvdFfrqy3rj67JAEhygA5Ig7EAShB1IgrADSRB2IAnCDiRB2IEk/h9BCfQTovZf9wAAAABJRU5ErkJggg==" }, "metadata": { "needs_background": "light" - }, - "output_type": "display_data" + } } ], - "source": [ - "# 加载模型\n", - "model.load('output/mnist')\n", - "\n", - "# 从测试集中取出一张图片\n", - "img, label = test_dataset[0]\n", - "# 将图片shape从1*28*28变为1*1*28*28,增加一个batch维度,以匹配模型输入格式要求\n", - "img_batch = np.expand_dims(img.astype('float32'), axis=0)\n", - "\n", - "# 执行推理并打印结果,此处predict_batch返回的是一个list,取出其中数据获得预测结果\n", - "out = model.predict_batch(img_batch)[0]\n", - "pred_label = out.argmax()\n", - "print('true label: {}, pred label: {}'.format(label[0], pred_label))\n", - "# 可视化图片\n", - "from matplotlib import pyplot as plt\n", - "plt.imshow(img[0])" - ] + "metadata": { + "scrolled": true + } }, { "cell_type": "markdown", - "id": "54a041fd", - "metadata": {}, "source": [ "更多参考:\n", - "* [模型保存与加载](08_model_save_load_cn.html)\n", - "* [模型训练、评估与推理](05_train_eval_predict_cn.html)\n" - ] + "* [模型保存与加载](model_save_load_cn.html)\n", + "* [模型训练、评估与推理](train_eval_predict_cn.html)\n" + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "07dc4cc1-4f8e-439c-a7e5-ce9a8c8d014f", - "metadata": {}, "source": [ "## 四、总结\n", "\n", @@ -671,7 +641,8 @@ "图 2:模型开发流程\n", "\n", "如果想要完成更复杂的深度学习任务,开发更强大的模型,飞桨提供了功能丰富的 API 帮助开发者完成任务,比如对数据集应用数据增强、使用更大的 CNN 模型、调优性能等。飞桨官网提供了丰富的教程与案例可供参考,欢迎一起探索深度学习的世界。" - ] + ], + "metadata": {} } ], "metadata": { @@ -699,4 +670,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} +} \ No newline at end of file diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/tensor_introduction_cn.md b/docs/guides/beginner/tensor_cn.md similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/tensor_introduction_cn.md rename to docs/guides/beginner/tensor_cn.md diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/tensor_introduction_en.md b/docs/guides/beginner/tensor_en.md similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/tensor_introduction_en.md rename to docs/guides/beginner/tensor_en.md diff --git a/docs/guides/02_paddle2.0_develop/05_train_eval_predict_cn.ipynb b/docs/guides/beginner/train_eval_predict_cn.ipynb similarity index 94% rename from docs/guides/02_paddle2.0_develop/05_train_eval_predict_cn.ipynb rename to docs/guides/beginner/train_eval_predict_cn.ipynb index 56fe653cf06..1936f6d02fa 100644 --- a/docs/guides/02_paddle2.0_develop/05_train_eval_predict_cn.ipynb +++ b/docs/guides/beginner/train_eval_predict_cn.ipynb @@ -2,8 +2,6 @@ "cells": [ { "cell_type": "markdown", - "id": "f97a8a96", - "metadata": {}, "source": [ "# 模型训练、评估与推理\n", "\n", @@ -21,22 +19,26 @@ "\n", "\n", "高层 API 如 `Model.fit` 、 `Model.evaluate` 、 `Model.predict` 等都可以通过基础 API 实现,本文先介绍高层 API 的使用方式,然后将高层 API 拆解为基础 API 介绍,方便对比学习。\n" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "1659a965-891d-420e-bc6e-7b8346e5d641", - "metadata": {}, "source": [ "## 一、训练前准备\n", "\n", "开始之前,需要使用下面的命令安装 Python 的 matplotlib 库,用于可视化图片。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": null, - "id": "a88a8752-bbab-4ecf-b384-4199626210df", + "source": [ + "# 使用 pip 工具安装 matplotlib\n", + "! python3 -m pip install matplotlib -i https://mirror.baidu.com/pypi/simple" + ], + "outputs": [], "metadata": { "execution": { "iopub.execute_input": "2022-02-15T05:48:34.721177Z", @@ -46,99 +48,78 @@ "shell.execute_reply.started": "2022-02-15T05:48:34.721148Z" }, "scrolled": true - }, - "outputs": [], - "source": [ - "# 使用 pip 工具安装 matplotlib\n", - "! python3 -m pip install matplotlib -i https://mirror.baidu.com/pypi/simple" - ] + } }, { "cell_type": "markdown", - "id": "bc9cf191-82e7-47e0-b6c5-bf9c55802b3f", - "metadata": {}, "source": [ "### 1.1 (可选)指定训练的硬件\n", "\n", "模型训练时,需要用到 CPU、 GPU 等计算处理器资源,由于飞桨框架的安装包是区分处理器类型的,默认情况下飞桨框架会根据所安装的版本自动选择对应硬件,比如安装的 GPU 版本的飞桨,则自动使用 GPU 训练模型,无需手动指定。因此一般情况下,无需执行此步骤。\n", "\n", "但是如果安装的 GPU 版本的飞桨框架,想切换到 CPU 上训练,则可通过 [paddle.device.set_device](../../api/paddle/device/set_device_cn.html#set-device) 修改。如果本机有多个 GPU 卡,也可以通过该 API 选择指定的卡进行训练,不指定的情况下则默认使用 'gpu:0'。" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 1, - "id": "0da73e3c-adb6-49bb-9da2-637ab920f558", - "metadata": { - "execution": { - "iopub.execute_input": "2022-02-23T03:53:25.061855Z", - "iopub.status.busy": "2022-02-23T03:53:25.061069Z", - "iopub.status.idle": "2022-02-23T03:53:26.486095Z", - "shell.execute_reply": "2022-02-23T03:53:26.485309Z", - "shell.execute_reply.started": "2022-02-23T03:53:25.061819Z" - }, - "scrolled": true - }, + "source": [ + "import paddle\n", + "\n", + "# 指定在 CPU 上训练\n", + "paddle.device.set_device('cpu')\n", + "\n", + "# 指定在 GPU 第 0 号卡上训练\n", + "# paddle.device.set_device('gpu:0')" + ], "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "CPUPlace" ] }, - "execution_count": 1, "metadata": {}, - "output_type": "execute_result" + "execution_count": 1 } ], - "source": [ - "import paddle\n", - "\n", - "# 指定在 CPU 上训练\n", - "paddle.device.set_device('cpu')\n", - "\n", - "# 指定在 GPU 第 0 号卡上训练\n", - "# paddle.device.set_device('gpu:0')" - ] + "metadata": { + "execution": { + "iopub.execute_input": "2022-02-23T03:53:25.061855Z", + "iopub.status.busy": "2022-02-23T03:53:25.061069Z", + "iopub.status.idle": "2022-02-23T03:53:26.486095Z", + "shell.execute_reply": "2022-02-23T03:53:26.485309Z", + "shell.execute_reply.started": "2022-02-23T03:53:25.061819Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "28d8eeac-909d-4152-b223-89a3da9e4fc6", - "metadata": {}, "source": [ "需要注意的是,使用 `paddle.device.set_device` 时,只能使用 `CUDA_VISIBLE_DEVICES` 设置范围内的显卡,例如可以设置`export CUDA_VISIBLE_DEVICES=0,1,2` 和 `paddle.device.set_device('gpu:0')`,但是设置 `export CUDA_VISIBLE_DEVICES=1` 和 `paddle.device.set_device('gpu:0')` 时会冲突报错。\n", "\n", "\n", "> 注:\n", - "> * 本文仅以单机单卡场景为例,介绍模型训练的方法,如果需要使用单机多卡、多机多卡训练,请参考如下章节:[单机多卡训练](06_device_cn.html)、[分布式训练](../06_distributed_training/index_cn.html)。\n", - "> * 飞桨框架除了支持在 CPU、GPU 上训练,还支持在百度昆仑 XPU、华为昇腾 NPU 等 AI 计算处理器上训练,对应的训练指导请参考 [硬件支持](../09_hardware_support/index_cn.html) 章节。\n" - ] + "> * 本文仅以单机单卡场景为例,介绍模型训练的方法,如果需要使用单机多卡、多机多卡训练,请参考如下章节:[分布式训练](../06_distributed_training/index_cn.html)。\n", + "> * 飞桨框架除了支持在 CPU、GPU 上训练,还支持在百度昆仑 XPU、华为昇腾 NPU 等 AI 计算处理器上训练,对应的训练指导请参考 [硬件支持](../hardware_support/index_cn.html) 章节。\n" + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "ddc1ef31-4128-445d-9ff7-18d43f5b90b8", - "metadata": {}, "source": [ "### 1.2 准备训练用的数据集和模型\n", "\n", "模型训练前,需要先完成数据集的加载和模型组网,以 MNIST 手写数字识别任务为例,代码示例如下:\n" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 6, - "id": "c20487eb-ae05-4a35-a459-a00484f02f1b", - "metadata": { - "execution": { - "iopub.execute_input": "2022-02-23T04:04:26.920997Z", - "iopub.status.busy": "2022-02-23T04:04:26.920406Z", - "iopub.status.idle": "2022-02-23T04:04:30.892862Z", - "shell.execute_reply": "2022-02-23T04:04:30.892026Z", - "shell.execute_reply.started": "2022-02-23T04:04:26.920963Z" - }, - "scrolled": true - }, - "outputs": [], "source": [ "from paddle.vision.transforms import Normalize\n", "\n", @@ -155,12 +136,21 @@ " paddle.nn.Dropout(0.2), \n", " paddle.nn.Linear(512, 10)\n", ")" - ] + ], + "outputs": [], + "metadata": { + "execution": { + "iopub.execute_input": "2022-02-23T04:04:26.920997Z", + "iopub.status.busy": "2022-02-23T04:04:26.920406Z", + "iopub.status.idle": "2022-02-23T04:04:30.892862Z", + "shell.execute_reply": "2022-02-23T04:04:30.892026Z", + "shell.execute_reply.started": "2022-02-23T04:04:26.920963Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "d9d0367d", - "metadata": {}, "source": [ "\n", "\n", @@ -168,22 +158,26 @@ "\n", "\n", "以手写数字识别任务为例,使用高层 API 进行模型训练、评估与推理的步骤如下:\n" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "94022bff-6b80-40aa-a366-e41365099794", - "metadata": {}, "source": [ "### 2.1 使用 paddle.Model 封装模型\n", "\n", "使用高层 API 训练模型前,可使用 [paddle.Model](../../api/paddle/Model_cn.html) 将模型封装为一个实例,方便后续进行训练、评估与推理。代码如下:" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 4, - "id": "a7705595", + "source": [ + "# 封装模型为一个 model 实例,便于进行后续的训练、评估和推理\n", + "model = paddle.Model(mnist)" + ], + "outputs": [], "metadata": { "execution": { "iopub.execute_input": "2022-02-15T05:48:42.417342Z", @@ -193,17 +187,10 @@ "shell.execute_reply.started": "2022-02-15T05:48:42.417318Z" }, "scrolled": true - }, - "outputs": [], - "source": [ - "# 封装模型为一个 model 实例,便于进行后续的训练、评估和推理\n", - "model = paddle.Model(mnist)" - ] + } }, { "cell_type": "markdown", - "id": "ebab1c33-2e92-4632-b902-f8c2dea85e9d", - "metadata": {}, "source": [ "### 2.2 使用 Model.prepare 配置训练准备参数\n", "\n", @@ -212,12 +199,19 @@ "- **优化器(optimizer)**:即寻找最优解的方法,可计算和更新梯度,并根据梯度更新模型参数。飞桨框架在 [paddle.optimizer](../../api/paddle/optimizer/Overview_cn.html#paddle-optimizer) 下提供了优化器相关 API。并且需要为优化器设置合适的学习率,或者指定合适的学习率策略,飞桨框架在 [paddle.optimizer.lr](../../api/paddle/optimizer/Overview_cn.html#about-lr) 下提供了学习率策略相关的 API。\n", "- **损失函数(loss)**:用于评估模型的预测值和真实值的差距,模型训练过程即取得尽可能小的 loss 的过程。飞桨框架在 [paddle.nn Loss层](../../api/paddle/nn/Overview_cn.html#loss) 提供了适用不同深度学习任务的损失函数相关 API。\n", "- **评价指标(metrics)**:用于评估模型的好坏,不同的任务通常有不同的评价指标。飞桨框架在 [paddle.metric](../../api/paddle/metric/Overview_cn.html) 下提供了评价指标相关 API。\n" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 5, - "id": "ccefe291", + "source": [ + "# 为模型训练做准备,设置优化器及其学习率,并将网络的参数传入优化器,设置损失函数和精度计算方式\n", + "model.prepare(optimizer=paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters()), \n", + " loss=paddle.nn.CrossEntropyLoss(), \n", + " metrics=paddle.metric.Accuracy())" + ], + "outputs": [], "metadata": { "execution": { "iopub.execute_input": "2022-02-15T05:48:42.421929Z", @@ -227,27 +221,17 @@ "shell.execute_reply.started": "2022-02-15T05:48:42.421908Z" }, "scrolled": true - }, - "outputs": [], - "source": [ - "# 为模型训练做准备,设置优化器及其学习率,并将网络的参数传入优化器,设置损失函数和精度计算方式\n", - "model.prepare(optimizer=paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters()), \n", - " loss=paddle.nn.CrossEntropyLoss(), \n", - " metrics=paddle.metric.Accuracy())" - ] + } }, { "cell_type": "markdown", - "id": "6a73ec3f-8e7a-40ab-bf92-01ba7247c507", - "metadata": {}, "source": [ "示例中使用 [Adam](../../api/paddle/optimizer/Adam_cn.html#adam) 优化器,设置优化器的学习率 `learning_rate=0.001`,并传入封装好的全部模型参数 `model.parameters` 用于后续更新;使用交叉熵损失函数 [CrossEntropyLoss](../../api/paddle/nn/CrossEntropyLoss_cn.html#crossentropyloss) 用于分类任务评估;使用分类任务常用的准确率指标 [Accuracy](../../api/paddle/metric/Accuracy_cn.html#accuracy) 计算模型在训练集上的精度。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "5da3322e", - "metadata": {}, "source": [ "### 2.3 使用 Model.fit 训练模型\n", "\n", @@ -258,27 +242,24 @@ "- **训练轮次(epoch)**:训练时遍历数据集的次数,即外循环轮次。\n", "- **批次大小(batch_size)**:内循环中每个批次的训练样本数。\n", "\n", - "除此之外,还可以设置样本乱序(`shuffle`)、丢弃不完整的批次样本(`drop_last`)、同步/异步读取数据(`num_workers`) 等参数,另外可通过 `Callback` 参数传入回调函数,在模型训练的各个阶段进行一些自定义操作,比如收集训练过程中的一些数据和参数,详细介绍可参见 [自定义 Callback](07_customize_cn.html) 章节。" - ] + "除此之外,还可以设置样本乱序(`shuffle`)、丢弃不完整的批次样本(`drop_last`)、同步/异步读取数据(`num_workers`) 等参数,另外可通过 `Callback` 参数传入回调函数,在模型训练的各个阶段进行一些自定义操作,比如收集训练过程中的一些数据和参数,详细介绍可参见 [自定义 Callback](../advanced/customize_cn.html) 章节。" + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 6, - "id": "51021638", - "metadata": { - "execution": { - "iopub.execute_input": "2022-02-15T05:48:42.434747Z", - "iopub.status.busy": "2022-02-15T05:48:42.434575Z", - "iopub.status.idle": "2022-02-15T05:49:30.599976Z", - "shell.execute_reply": "2022-02-15T05:49:30.599106Z", - "shell.execute_reply.started": "2022-02-15T05:48:42.434726Z" - }, - "scrolled": true - }, + "source": [ + "# 启动模型训练,指定训练数据集,设置训练轮次,设置每次数据集计算的批次大小,设置日志格式\n", + "model.fit(train_dataset, \n", + " epochs=5, \n", + " batch_size=64,\n", + " verbose=1)" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "The loss value printed in the log is the current step, and the metric is the average value of previous steps.\n", "Epoch 1/5\n", @@ -294,27 +275,27 @@ ] } ], - "source": [ - "# 启动模型训练,指定训练数据集,设置训练轮次,设置每次数据集计算的批次大小,设置日志格式\n", - "model.fit(train_dataset, \n", - " epochs=5, \n", - " batch_size=64,\n", - " verbose=1)" - ] + "metadata": { + "execution": { + "iopub.execute_input": "2022-02-15T05:48:42.434747Z", + "iopub.status.busy": "2022-02-15T05:48:42.434575Z", + "iopub.status.idle": "2022-02-15T05:49:30.599976Z", + "shell.execute_reply": "2022-02-15T05:49:30.599106Z", + "shell.execute_reply.started": "2022-02-15T05:48:42.434726Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "74aa5dcd-70f7-45de-b2d4-3e5527bb8458", - "metadata": {}, "source": [ "示例中传入数据集 `train_dataset` 进行迭代训练,共遍历 5 轮(`epochs=5`),每轮迭代中分批次取数据训练,每批次 64 个样本(`batch_size=64`),并打印训练过程中的日志(`verbose=1`)。\n", "从打印日志中可观察到损失函数 loss 值减小,精度指标 acc 值提高的趋势,说明模型训练取得了成效。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "eeff5f00", - "metadata": {}, "source": [ "### 2.4 使用 Model.evaluate 评估模型\n", "\n", @@ -324,26 +305,21 @@ "* 只包含loss, `{'loss': xxx}` \n", "* 包含loss和一个评估指标, `{'loss': xxx, 'metric name': xxx}` \n", "* 包含loss和多个评估指标, `{'loss': xxx, 'metric name1': xxx, 'metric name2': xxx}`\n" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 7, - "id": "70f670ec", - "metadata": { - "execution": { - "iopub.execute_input": "2022-02-15T05:49:30.601360Z", - "iopub.status.busy": "2022-02-15T05:49:30.600927Z", - "iopub.status.idle": "2022-02-15T05:49:51.509694Z", - "shell.execute_reply": "2022-02-15T05:49:51.509091Z", - "shell.execute_reply.started": "2022-02-15T05:49:30.601333Z" - }, - "scrolled": true - }, + "source": [ + "# 用 evaluate 在测试集上对模型进行验证\n", + "eval_result = model.evaluate(test_dataset, verbose=1)\n", + "print(eval_result)" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Eval begin...\n", "step 10000/10000 [==============================] - loss: 2.3842e-07 - acc: 0.9714 - 2ms/step \n", @@ -352,24 +328,26 @@ ] } ], - "source": [ - "# 用 evaluate 在测试集上对模型进行验证\n", - "eval_result = model.evaluate(test_dataset, verbose=1)\n", - "print(eval_result)" - ] + "metadata": { + "execution": { + "iopub.execute_input": "2022-02-15T05:49:30.601360Z", + "iopub.status.busy": "2022-02-15T05:49:30.600927Z", + "iopub.status.idle": "2022-02-15T05:49:51.509694Z", + "shell.execute_reply": "2022-02-15T05:49:51.509091Z", + "shell.execute_reply.started": "2022-02-15T05:49:30.601333Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "62c8053a-bdee-4539-8df7-646919d22805", - "metadata": {}, "source": [ "示例中返回一个 loss 和 一个 acc 准确率指标的结果。在模型之前未\"见过\"的测试集上,评估出仍然有 98.1% 的准确率,验证了模型在该任务上取得不错的效果。" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "109ac763", - "metadata": {}, "source": [ "### 2.5 使用 Model.predict 执行推理\n", "\n", @@ -388,26 +366,34 @@ "\n", "\n", "\n" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 8, - "id": "d6318f18", - "metadata": { - "execution": { - "iopub.execute_input": "2022-02-15T05:49:51.511077Z", - "iopub.status.busy": "2022-02-15T05:49:51.510653Z", - "iopub.status.idle": "2022-02-15T05:50:07.724522Z", - "shell.execute_reply": "2022-02-15T05:50:07.723742Z", - "shell.execute_reply.started": "2022-02-15T05:49:51.511050Z" - }, - "scrolled": true - }, + "source": [ + "\n", + "# 用 predict 在测试集上对模型进行推理\n", + "test_result = model.predict(test_dataset)\n", + "# 由于模型是单一输出,test_result的形状为[1, 10000],10000是测试数据集的数据量。这里打印第一个数据的结果,这个数组表示每个数字的预测概率\n", + "print(len(test_result))\n", + "print(test_result[0][0])\n", + "\n", + "# 从测试集中取出一张图片\n", + "img, label = test_dataset[0]\n", + "\n", + "# 打印推理结果,这里的argmax函数用于取出预测值中概率最高的一个的下标,作为预测标签\n", + "pred_label = test_result[0][0].argmax()\n", + "print('true label: {}, pred label: {}'.format(label[0], pred_label))\n", + "# 使用matplotlib库,可视化图片\n", + "from matplotlib import pyplot as plt\n", + "plt.imshow(img[0])" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "Predict begin...\n", "step 10000/10000 [==============================] - 2ms/step \n", @@ -419,51 +405,41 @@ ] }, { + "output_type": "execute_result", "data": { "text/plain": [ "" ] }, - "execution_count": 12, "metadata": {}, - "output_type": "execute_result" + "execution_count": 12 }, { + "output_type": "display_data", "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAADaVJREFUeJzt3X+MXOV1xvHnib1e4jU0GILrGgcnhKA6NDjVxiSCVo4IKZAgEyWhWKrlSpRFLUhQRW2Rq6iWWqUUhSC3SSM5wY1BBGgCCCtx01CrrYVKHS/I2IBpTajT2DVewLQ2AfwDn/6x19EGdt5d5ted9fl+pNXO3HPv3KPrfXzvzDszryNCAPJ5R90NAKgH4QeSIvxAUoQfSIrwA0kRfiApwg8kRfiBpAg/kNT0bu5shvvjJA10c5dAKq/rZzochzyZdVsKv+1LJa2WNE3SNyPiltL6J2lAF/jiVnYJoGBzbJz0uk1f9tueJulrki6TtFDSMtsLm308AN3VynP+xZKejYjnIuKwpHslLW1PWwA6rZXwz5P00zH3d1fLfoHtIdvDtoeP6FALuwPQTh1/tT8i1kTEYEQM9qm/07sDMEmthH+PpPlj7p9ZLQMwBbQS/i2SzrH9XtszJF0taX172gLQaU0P9UXEUds3SPpHjQ71rY2Ip9rWGYCOammcPyI2SNrQpl4AdBFv7wWSIvxAUoQfSIrwA0kRfiApwg8kRfiBpAg/kBThB5Ii/EBShB9IivADSRF+ICnCDyRF+IGkCD+QFOEHkiL8QFKEH0iK8ANJEX4gKcIPJEX4gaQIP5AU4QeSIvxAUoQfSIrwA0kRfiCplmbptb1L0kFJb0g6Gh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WPun1kt6wkRsaf6PSLpQfXe7MP7jk+SWv0eqbmfn+ulmZvHm1laPXDsemnG6zrCv0XSObbfa3uGpKslra+hj7ewPVC9ECPbA5I+qd6bfXi9pBXV7RWSHqqxl1/QKzM3N5pZWjUfu56b8Toiuv4j6XKNvuL/Y0l/WkcPDfp6n6Qnqp+n6u5N0j0avQw8otHXRq6RdJqkjZJ2SvonSbN7qLe7JG2XtE2jQZtbU28XafSSfpukrdXP5XUfu0JftRw33uEHJMULfkBShB9IivADSRF+ICnCDyRF+IGkCD+QFOEHkvp/uK0ZUt56JeQAAAAASUVORK5CYII=" }, "metadata": { "needs_background": "light" - }, - "output_type": "display_data" + } } ], - "source": [ - "\n", - "# 用 predict 在测试集上对模型进行推理\n", - "test_result = model.predict(test_dataset)\n", - "# 由于模型是单一输出,test_result的形状为[1, 10000],10000是测试数据集的数据量。这里打印第一个数据的结果,这个数组表示每个数字的预测概率\n", - "print(len(test_result))\n", - "print(test_result[0][0])\n", - "\n", - "# 从测试集中取出一张图片\n", - "img, label = test_dataset[0]\n", - "\n", - "# 打印推理结果,这里的argmax函数用于取出预测值中概率最高的一个的下标,作为预测标签\n", - "pred_label = test_result[0][0].argmax()\n", - "print('true label: {}, pred label: {}'.format(label[0], pred_label))\n", - "# 使用matplotlib库,可视化图片\n", - "from matplotlib import pyplot as plt\n", - "plt.imshow(img[0])" - ] + "metadata": { + "execution": { + "iopub.execute_input": "2022-02-15T05:49:51.511077Z", + "iopub.status.busy": "2022-02-15T05:49:51.510653Z", + "iopub.status.idle": "2022-02-15T05:50:07.724522Z", + "shell.execute_reply": "2022-02-15T05:50:07.723742Z", + "shell.execute_reply.started": "2022-02-15T05:49:51.511050Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "a3110884-fc0f-41e5-901e-40d136eaab6c", - "metadata": {}, "source": [ "\n", "示例中对测试集 `test_dataset` 中每一个样本执行预测,测试数据集中包含 10000 个数据,因此将取得 10000 个预测输出。\n", @@ -480,22 +456,20 @@ "* [Model.predict_batch](../api/paddle/Model_cn.html#predict-batch-inputs):在一个批次的数据集上进行推理。\n", "\n", "这三个 API 与上面介绍的三个 API 的输入数据的维度有所不同,详细介绍可参考对应 API 文档。\n" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "9508034b", - "metadata": {}, "source": [ "## 三、使用基础 API 训练、评估与推理\n", "\n", "除了通过高层 API 实现模型的训练、评估与推理,飞桨框架也同样支持通过基础 API。简单来说, `Model.prepare` 、 `Model.fit` 、 `Model.evaluate` 、 `Model.predict` 都是由基础 API 封装而来。下面通过拆解高层 API 到基础 API 的方式,来了解如何用基础 API 完成模型训练、评估与推理。\n" - ] + ], + "metadata": {} }, { "cell_type": "markdown", - "id": "c02524fd", - "metadata": {}, "source": [ "### 3.1 模型训练(拆解 Model.prepare、Model.fit)\n", "\n", @@ -513,35 +487,12 @@ " - 3.7 执行一次优化器步骤,即按照选择的优化算法,根据当前批次数据的梯度更新传入优化器的参数\n", " - 3.8 将优化器的梯度进行清零\n", " \n" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 9, - "id": "8419b510", - "metadata": { - "execution": { - "iopub.execute_input": "2022-02-15T05:50:07.726048Z", - "iopub.status.busy": "2022-02-15T05:50:07.725599Z", - "iopub.status.idle": "2022-02-15T05:50:52.862759Z", - "shell.execute_reply": "2022-02-15T05:50:52.861931Z", - "shell.execute_reply.started": "2022-02-15T05:50:07.726018Z" - }, - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "epoch: 0, batch_id: 900, loss is: [0.06991791], acc is: [0.96875]\n", - "epoch: 1, batch_id: 900, loss is: [0.02878829], acc is: [1.]\n", - "epoch: 2, batch_id: 900, loss is: [0.07192856], acc is: [0.96875]\n", - "epoch: 3, batch_id: 900, loss is: [0.20411499], acc is: [0.96875]\n", - "epoch: 4, batch_id: 900, loss is: [0.13589518], acc is: [0.96875]\n" - ] - } - ], "source": [ "# dataset与mnist的定义与使用高层API的内容一致\n", "# 用 DataLoader 实现数据加载\n", @@ -580,12 +531,33 @@ " optim.step()\n", " # 梯度清零\n", " optim.clear_grad()" - ] + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "epoch: 0, batch_id: 900, loss is: [0.06991791], acc is: [0.96875]\n", + "epoch: 1, batch_id: 900, loss is: [0.02878829], acc is: [1.]\n", + "epoch: 2, batch_id: 900, loss is: [0.07192856], acc is: [0.96875]\n", + "epoch: 3, batch_id: 900, loss is: [0.20411499], acc is: [0.96875]\n", + "epoch: 4, batch_id: 900, loss is: [0.13589518], acc is: [0.96875]\n" + ] + } + ], + "metadata": { + "execution": { + "iopub.execute_input": "2022-02-15T05:50:07.726048Z", + "iopub.status.busy": "2022-02-15T05:50:07.725599Z", + "iopub.status.idle": "2022-02-15T05:50:52.862759Z", + "shell.execute_reply": "2022-02-15T05:50:52.861931Z", + "shell.execute_reply.started": "2022-02-15T05:50:07.726018Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "00a077d3", - "metadata": {}, "source": [ "### 3.2 模型评估(拆解 Model.evaluate)\n", "\n", @@ -595,35 +567,12 @@ "1. 模型实例从 `train` 模式改为 `eval` 模式\n", "1. 不需要反向传播、优化器参数更新和优化器梯度清零\n", "\n" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 10, - "id": "d27f6ec2", - "metadata": { - "execution": { - "iopub.execute_input": "2022-02-15T05:50:52.864342Z", - "iopub.status.busy": "2022-02-15T05:50:52.863912Z", - "iopub.status.idle": "2022-02-15T05:50:54.048046Z", - "shell.execute_reply": "2022-02-15T05:50:54.047104Z", - "shell.execute_reply.started": "2022-02-15T05:50:52.864305Z" - }, - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "batch_id: 30, loss is: [0.23106411], acc is: [0.953125]\n", - "batch_id: 60, loss is: [0.4329119], acc is: [0.90625]\n", - "batch_id: 90, loss is: [0.07333981], acc is: [0.96875]\n", - "batch_id: 120, loss is: [0.00324837], acc is: [1.]\n", - "batch_id: 150, loss is: [0.0857158], acc is: [0.96875]\n" - ] - } - ], "source": [ "# 加载测试数据集\n", "test_loader = paddle.io.DataLoader(test_dataset, batch_size=64, drop_last=True)\n", @@ -645,12 +594,33 @@ " # 打印信息\n", " if (batch_id+1) % 30 == 0:\n", " print(\"batch_id: {}, loss is: {}, acc is: {}\".format(batch_id+1, loss.numpy(), acc.numpy()))" - ] + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "batch_id: 30, loss is: [0.23106411], acc is: [0.953125]\n", + "batch_id: 60, loss is: [0.4329119], acc is: [0.90625]\n", + "batch_id: 90, loss is: [0.07333981], acc is: [0.96875]\n", + "batch_id: 120, loss is: [0.00324837], acc is: [1.]\n", + "batch_id: 150, loss is: [0.0857158], acc is: [0.96875]\n" + ] + } + ], + "metadata": { + "execution": { + "iopub.execute_input": "2022-02-15T05:50:52.864342Z", + "iopub.status.busy": "2022-02-15T05:50:52.863912Z", + "iopub.status.idle": "2022-02-15T05:50:54.048046Z", + "shell.execute_reply": "2022-02-15T05:50:54.047104Z", + "shell.execute_reply.started": "2022-02-15T05:50:52.864305Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "214cc6de", - "metadata": {}, "source": [ "### 3.3 模型推理(拆解 Model.predict)\n", "\n", @@ -659,108 +629,126 @@ "1. 加载待执行推理的测试数据,并将模型设置为 `eval` 模式\n", "1. 读取测试数据并获得预测结果\n", "1. 对预测结果进行后处理" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 11, - "id": "1d79305f", - "metadata": { - "execution": { - "iopub.execute_input": "2022-02-15T05:50:54.051031Z", - "iopub.status.busy": "2022-02-15T05:50:54.050435Z", - "iopub.status.idle": "2022-02-15T05:50:55.278437Z", - "shell.execute_reply": "2022-02-15T05:50:55.277810Z", - "shell.execute_reply.started": "2022-02-15T05:50:54.050992Z" - }, - "scrolled": true - }, + "source": [ + "# 加载测试数据集\n", + "test_loader = paddle.io.DataLoader(test_dataset, batch_size=64, drop_last=True)\n", + "# 将该模型及其所有子层设置为预测模式\n", + "mnist.eval()\n", + "for batch_id, data in enumerate(test_loader()):\n", + " # 取出测试数据\n", + " x_data = data[0] \n", + " # 获取预测结果\n", + " predicts = mnist(x_data)\n", + "print(\"predict finished\")\n", + "\n", + "# 从测试集中取出一组数据\n", + "img, label = test_loader().next()\n", + "\n", + "# 执行推理并打印结果\n", + "pred_label = mnist(img)[0].argmax()\n", + "print('true label: {}, pred label: {}'.format(label[0].item(), pred_label[0].item()))\n", + "# 可视化图片\n", + "from matplotlib import pyplot as plt\n", + "plt.imshow(img[0][0])" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "predict finished\n", "true label: 7, pred label: 7\n" ] }, { + "output_type": "execute_result", "data": { "text/plain": [ "" ] }, - "execution_count": 12, "metadata": {}, - "output_type": "execute_result" + "execution_count": 12 }, { + "output_type": "display_data", "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAADaVJREFUeJzt3X+MXOV1xvHnib1e4jU0GILrGgcnhKA6NDjVxiSCVo4IKZAgEyWhWKrlSpRFLUhQRW2Rq6iWWqUUhSC3SSM5wY1BBGgCCCtx01CrrYVKHS/I2IBpTajT2DVewLQ2AfwDn/6x19EGdt5d5ted9fl+pNXO3HPv3KPrfXzvzDszryNCAPJ5R90NAKgH4QeSIvxAUoQfSIrwA0kRfiApwg8kRfiBpAg/kNT0bu5shvvjJA10c5dAKq/rZzochzyZdVsKv+1LJa2WNE3SNyPiltL6J2lAF/jiVnYJoGBzbJz0uk1f9tueJulrki6TtFDSMtsLm308AN3VynP+xZKejYjnIuKwpHslLW1P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JW2RtCwinu5qIw3Y3iVpMCJqHxO2/ZuSXpF0Z0ScVy27VdL+iLil+o/z1Ij4kx7pbZWkV+qeubmaUGbu2JmlJV0p6XdV47Er9HWVajhudZz5F0t6NiKei4jDku6VtLSGPnpeRGyStP9Ni5dKWlfdXqfRP56ua9BbT4iIvRHxeHX7oKTjM0vXeuwKfdWijvDPk/TTMfd3q7em/A5JP7T9mO2hupsZx5xq2nRJel7SnDqbGceEMzd305tmlu6ZY9fMjNftxgt+b3VRRPy6pMskXV9d3vakGH3O1kvDNZOaublbxplZ+ufqPHbNznjdbnWEf4+k+WPun1kt6wkRsaf6PSLpQfXe7MP7jk+SWv0eqbmfn+ulmZvHm1laPXDsemnG6zrCv0XSObbfa3uGpKslra+hj7ewPVC9ECPbA5I+qd6bfXi9pBXV7RWSHqqxl1/QKzM3N5pZWjUfu56b8Toiuv4j6XKNvuL/Y0l/WkcPDfp6n6Qnqp+n6u5N0j0avQw8otHXRq6RdJqkjZJ2SvonSbN7qLe7JG2XtE2jQZtbU28XafSSfpukrdXP5XUfu0JftRw33uEHJMULfkBShB9IivADSRF+ICnCDyRF+IGkCD+QFOEHkvp/uK0ZUt56JeQAAAAASUVORK5CYII=" }, "metadata": { "needs_background": "light" - }, - "output_type": "display_data" + } } ], - "source": [ - "# 加载测试数据集\n", - "test_loader = paddle.io.DataLoader(test_dataset, batch_size=64, drop_last=True)\n", - "# 将该模型及其所有子层设置为预测模式\n", - "mnist.eval()\n", - "for batch_id, data in enumerate(test_loader()):\n", - " # 取出测试数据\n", - " x_data = data[0] \n", - " # 获取预测结果\n", - " predicts = mnist(x_data)\n", - "print(\"predict finished\")\n", - "\n", - "# 从测试集中取出一组数据\n", - "img, label = test_loader().next()\n", - "\n", - "# 执行推理并打印结果\n", - "pred_label = mnist(img)[0].argmax()\n", - "print('true label: {}, pred label: {}'.format(label[0].item(), pred_label[0].item()))\n", - "# 可视化图片\n", - "from matplotlib import pyplot as plt\n", - "plt.imshow(img[0][0])" - ] + "metadata": { + "execution": { + "iopub.execute_input": "2022-02-15T05:50:54.051031Z", + "iopub.status.busy": "2022-02-15T05:50:54.050435Z", + "iopub.status.idle": "2022-02-15T05:50:55.278437Z", + "shell.execute_reply": "2022-02-15T05:50:55.277810Z", + "shell.execute_reply.started": "2022-02-15T05:50:54.050992Z" + }, + "scrolled": true + } }, { "cell_type": "markdown", - "id": "924b3c12-90e7-41e9-9aef-205866c02ee5", - "metadata": {}, "source": [ "## 四、总结\n", "\n", - "本节中介绍了在飞桨框架中使用高层 API 进行模型训练、评估和推理的方法,并拆解出对应的基础 API 实现方法。需要注意的是,这里的推理仅用于模型效果验证,实际生产应用中,则可使用飞桨提供的一系列推理部署工具,满足服务器端、移动端、网页/小程序等多种环境的模型部署上线需求,具体可参见 [推理部署](../05_inference_deployment/index_cn.html) 章节。\n", + "本节中介绍了在飞桨框架中使用高层 API 进行模型训练、评估和推理的方法,并拆解出对应的基础 API 实现方法。需要注意的是,这里的推理仅用于模型效果验证,实际生产应用中,则可使用飞桨提供的一系列推理部署工具,满足服务器端、移动端、网页/小程序等多种环境的模型部署上线需求,具体可参见 [推理部署](../infer/index_cn.html) 章节。\n", "\n", "同时,飞桨的高层 API 和基础 API 可以组合使用,并不是完全割裂开的,这样有助于开发者更便捷地完成算法迭代。示例代码如下:\n", "\n" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 10, - "id": "50b8953f-9df0-4fa3-b420-f5987ad0cf3b", - "metadata": { - "execution": { - "iopub.execute_input": "2022-02-23T04:10:14.866170Z", - "iopub.status.busy": "2022-02-23T04:10:14.865665Z", - "iopub.status.idle": "2022-02-23T04:11:59.489596Z", - "shell.execute_reply": "2022-02-23T04:11:59.488958Z", - "shell.execute_reply.started": "2022-02-23T04:10:14.866136Z" - }, - "scrolled": true - }, + "source": [ + "from paddle.vision.models import LeNet\n", + "\n", + "class FaceNet(paddle.nn.Layer):\n", + " def __init__(self):\n", + " super().__init__()\n", + " # 使用高层API组网\n", + " self.backbone = LeNet()\n", + " # 使用基础API组网\n", + " self.outLayer1 = paddle.nn.Sequential(\n", + " paddle.nn.Linear(10, 512),\n", + " paddle.nn.ReLU(),\n", + " paddle.nn.Dropout(0.2)\n", + " )\n", + " self.outLayer2 = paddle.nn.Linear(512, 10)\n", + " \n", + " def forward(self, inputs):\n", + " out = self.backbone(inputs)\n", + " out = self.outLayer1(out)\n", + " out = self.outLayer2(out)\n", + " return out\n", + "# 使用高层API封装网络\n", + "model = paddle.Model(FaceNet())\n", + "# 使用基础API定义优化器\n", + "optim = paddle.optimizer.Adam(learning_rate=1e-3, parameters=model.parameters())\n", + "# 使用高层API封装优化器和损失函数\n", + "model.prepare(optim, paddle.nn.CrossEntropyLoss(), metrics=paddle.metric.Accuracy())\n", + "# 使用高层API训练网络\n", + "model.fit(train_dataset, test_dataset, epochs=5, batch_size=64, verbose=1)" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "The loss value printed in the log is the current step, and the metric is the average value of previous steps.\n", "Epoch 1/5\n", @@ -791,36 +779,16 @@ ] } ], - "source": [ - "from paddle.vision.models import LeNet\n", - "\n", - "class FaceNet(paddle.nn.Layer):\n", - " def __init__(self):\n", - " super().__init__()\n", - " # 使用高层API组网\n", - " self.backbone = LeNet()\n", - " # 使用基础API组网\n", - " self.outLayer1 = paddle.nn.Sequential(\n", - " paddle.nn.Linear(10, 512),\n", - " paddle.nn.ReLU(),\n", - " paddle.nn.Dropout(0.2)\n", - " )\n", - " self.outLayer2 = paddle.nn.Linear(512, 10)\n", - " \n", - " def forward(self, inputs):\n", - " out = self.backbone(inputs)\n", - " out = self.outLayer1(out)\n", - " out = self.outLayer2(out)\n", - " return out\n", - "# 使用高层API封装网络\n", - "model = paddle.Model(FaceNet())\n", - "# 使用基础API定义优化器\n", - "optim = paddle.optimizer.Adam(learning_rate=1e-3, parameters=model.parameters())\n", - "# 使用高层API封装优化器和损失函数\n", - "model.prepare(optim, paddle.nn.CrossEntropyLoss(), metrics=paddle.metric.Accuracy())\n", - "# 使用高层API训练网络\n", - "model.fit(train_dataset, test_dataset, epochs=5, batch_size=64, verbose=1)" - ] + "metadata": { + "execution": { + "iopub.execute_input": "2022-02-23T04:10:14.866170Z", + "iopub.status.busy": "2022-02-23T04:10:14.865665Z", + "iopub.status.idle": "2022-02-23T04:11:59.489596Z", + "shell.execute_reply": "2022-02-23T04:11:59.488958Z", + "shell.execute_reply.started": "2022-02-23T04:10:14.866136Z" + }, + "scrolled": true + } } ], "metadata": { @@ -844,4 +812,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} +} \ No newline at end of file diff --git a/docs/guides/09_hardware_support/hardware_info_cn.md b/docs/guides/hardware_support/hardware_info_cn.md similarity index 100% rename from docs/guides/09_hardware_support/hardware_info_cn.md rename to docs/guides/hardware_support/hardware_info_cn.md diff --git a/docs/guides/09_hardware_support/index_cn.rst b/docs/guides/hardware_support/index_cn.rst similarity index 100% rename from docs/guides/09_hardware_support/index_cn.rst rename to docs/guides/hardware_support/index_cn.rst diff --git a/docs/guides/09_hardware_support/ipu_docs/index_cn.rst b/docs/guides/hardware_support/ipu_docs/index_cn.rst similarity index 100% rename from docs/guides/09_hardware_support/ipu_docs/index_cn.rst rename to docs/guides/hardware_support/ipu_docs/index_cn.rst diff --git a/docs/guides/09_hardware_support/ipu_docs/infer_example_cn.md b/docs/guides/hardware_support/ipu_docs/infer_example_cn.md similarity index 100% rename from docs/guides/09_hardware_support/ipu_docs/infer_example_cn.md rename to docs/guides/hardware_support/ipu_docs/infer_example_cn.md diff --git a/docs/guides/09_hardware_support/ipu_docs/paddle_install_cn.md b/docs/guides/hardware_support/ipu_docs/paddle_install_cn.md similarity index 100% rename from docs/guides/09_hardware_support/ipu_docs/paddle_install_cn.md rename to docs/guides/hardware_support/ipu_docs/paddle_install_cn.md diff --git a/docs/guides/09_hardware_support/ipu_docs/train_example_cn.md b/docs/guides/hardware_support/ipu_docs/train_example_cn.md similarity index 100% rename from docs/guides/09_hardware_support/ipu_docs/train_example_cn.md rename to docs/guides/hardware_support/ipu_docs/train_example_cn.md diff --git a/docs/guides/09_hardware_support/npu_docs/index_cn.rst b/docs/guides/hardware_support/npu_docs/index_cn.rst similarity index 100% rename from docs/guides/09_hardware_support/npu_docs/index_cn.rst rename to docs/guides/hardware_support/npu_docs/index_cn.rst diff --git a/docs/guides/09_hardware_support/npu_docs/paddle_install_cn.md b/docs/guides/hardware_support/npu_docs/paddle_install_cn.md similarity index 100% rename from docs/guides/09_hardware_support/npu_docs/paddle_install_cn.md rename to docs/guides/hardware_support/npu_docs/paddle_install_cn.md diff --git a/docs/guides/09_hardware_support/npu_docs/train_example_cn.md b/docs/guides/hardware_support/npu_docs/train_example_cn.md similarity index 100% rename from docs/guides/09_hardware_support/npu_docs/train_example_cn.md rename to docs/guides/hardware_support/npu_docs/train_example_cn.md diff --git a/docs/guides/09_hardware_support/rocm_docs/index_cn.rst b/docs/guides/hardware_support/rocm_docs/index_cn.rst similarity index 100% rename from docs/guides/09_hardware_support/rocm_docs/index_cn.rst rename to docs/guides/hardware_support/rocm_docs/index_cn.rst diff --git a/docs/guides/09_hardware_support/rocm_docs/infer_example_cn.md b/docs/guides/hardware_support/rocm_docs/infer_example_cn.md similarity index 100% rename from docs/guides/09_hardware_support/rocm_docs/infer_example_cn.md rename to docs/guides/hardware_support/rocm_docs/infer_example_cn.md diff --git a/docs/guides/09_hardware_support/rocm_docs/paddle_install_cn.md b/docs/guides/hardware_support/rocm_docs/paddle_install_cn.md similarity index 100% rename from docs/guides/09_hardware_support/rocm_docs/paddle_install_cn.md rename to docs/guides/hardware_support/rocm_docs/paddle_install_cn.md diff --git a/docs/guides/09_hardware_support/rocm_docs/paddle_rocm_cn.md b/docs/guides/hardware_support/rocm_docs/paddle_rocm_cn.md similarity index 100% rename from docs/guides/09_hardware_support/rocm_docs/paddle_rocm_cn.md rename to docs/guides/hardware_support/rocm_docs/paddle_rocm_cn.md diff --git a/docs/guides/09_hardware_support/rocm_docs/train_example_cn.md b/docs/guides/hardware_support/rocm_docs/train_example_cn.md similarity index 100% rename from docs/guides/09_hardware_support/rocm_docs/train_example_cn.md rename to docs/guides/hardware_support/rocm_docs/train_example_cn.md diff --git a/docs/guides/09_hardware_support/xpu_docs/index_cn.rst b/docs/guides/hardware_support/xpu_docs/index_cn.rst similarity index 100% rename from docs/guides/09_hardware_support/xpu_docs/index_cn.rst rename to docs/guides/hardware_support/xpu_docs/index_cn.rst diff --git a/docs/guides/09_hardware_support/xpu_docs/inference_install_example_cn.md b/docs/guides/hardware_support/xpu_docs/inference_install_example_cn.md similarity index 100% rename from docs/guides/09_hardware_support/xpu_docs/inference_install_example_cn.md rename to docs/guides/hardware_support/xpu_docs/inference_install_example_cn.md diff --git a/docs/guides/09_hardware_support/xpu_docs/paddle_2.0_xpu2_cn.md b/docs/guides/hardware_support/xpu_docs/paddle_2.0_xpu2_cn.md similarity index 100% rename from docs/guides/09_hardware_support/xpu_docs/paddle_2.0_xpu2_cn.md rename to docs/guides/hardware_support/xpu_docs/paddle_2.0_xpu2_cn.md diff --git a/docs/guides/09_hardware_support/xpu_docs/paddle_2.0_xpu_cn.md b/docs/guides/hardware_support/xpu_docs/paddle_2.0_xpu_cn.md similarity index 100% rename from docs/guides/09_hardware_support/xpu_docs/paddle_2.0_xpu_cn.md rename to docs/guides/hardware_support/xpu_docs/paddle_2.0_xpu_cn.md diff --git a/docs/guides/09_hardware_support/xpu_docs/paddle_install_cn.md b/docs/guides/hardware_support/xpu_docs/paddle_install_cn.md similarity index 100% rename from docs/guides/09_hardware_support/xpu_docs/paddle_install_cn.md rename to docs/guides/hardware_support/xpu_docs/paddle_install_cn.md diff --git a/docs/guides/09_hardware_support/xpu_docs/paddle_install_xpu2_cn.md b/docs/guides/hardware_support/xpu_docs/paddle_install_xpu2_cn.md similarity index 100% rename from docs/guides/09_hardware_support/xpu_docs/paddle_install_xpu2_cn.md rename to docs/guides/hardware_support/xpu_docs/paddle_install_xpu2_cn.md diff --git a/docs/guides/09_hardware_support/xpu_docs/train_example_cn.md b/docs/guides/hardware_support/xpu_docs/train_example_cn.md similarity index 100% rename from docs/guides/09_hardware_support/xpu_docs/train_example_cn.md rename to docs/guides/hardware_support/xpu_docs/train_example_cn.md diff --git a/docs/guides/09_hardware_support/xpu_docs/train_example_xpu2_cn.md b/docs/guides/hardware_support/xpu_docs/train_example_xpu2_cn.md similarity index 100% rename from docs/guides/09_hardware_support/xpu_docs/train_example_xpu2_cn.md rename to docs/guides/hardware_support/xpu_docs/train_example_xpu2_cn.md diff --git a/docs/guides/index_cn.rst b/docs/guides/index_cn.rst index 0bdc6748534..c20007ced31 100644 --- a/docs/guides/index_cn.rst +++ b/docs/guides/index_cn.rst @@ -8,31 +8,27 @@ 使用教程分为如下的模块: -- `整体介绍 <./01_paddle2.0_introduction/index_cn.html>`_ : 飞桨框架2.0新特性的介绍与飞桨框架2.0升级指南的说明。 -- `模型开发 <./02_paddle2.0_develop/index_cn.html>`_ : 飞桨框架2.0模型开发全流程说明。 -- `模型可视化 <./03_VisualDL/index_cn.html>`_ : 介绍如何用VisualDL实现飞桨框架模型的可视化。 -- `动态图转静态图 <./04_dygraph_to_static/index_cn.html>`_ : 介绍飞桨框架动态图转静态图的方法。 -- `预测部署 <./05_inference_deployment/index_cn.html>`_ : 介绍如何使用训练好的模型进行预测。 -- `分布式训练 <./06_distributed_training/index_cn.html>`_ : 介绍如何使用分布式进行训练。 -- `自定义算子 <./07_new_op/index_cn.html>`_ : 介绍飞桨框架自定义算子的方法。 -- `性能调优 <./performance_improving/index_cn.html>`_ : 介绍飞桨框架性能调优的方法。 -- `算子映射 <./08_api_mapping/index_cn.html>`_ : 介绍飞桨框架API算子的映射信息。 -- `硬件支持 <./09_hardware_support/index_cn.html>`_ : 介绍飞桨框架硬件支持相关信息。 -- `参与开发 <./10_contribution/index_cn.html>`_ : 介绍如何参与飞桨框架的开发。 -- `环境变量 <./flags/flags_cn.html>`_ : 介绍飞桨相关环境变量的使用。 +- `模型开发入门 <./beginner/index_cn.html>`_ +- `模型开发更多用法 <./advanced/index_cn.html>`_ +- `动态图转静态图 <./jit/index_cn.html>`_ +- `预测部署 <./infer/index_cn.html>`_ +- `分布式训练 <./06_distributed_training/index_cn.html>`_ +- `性能调优 <./performance_improving/index_cn.html>`_ +- `模型迁移 <./model_convert/index_cn.html>`_ +- `硬件支持 <./hardware_support/index_cn.html>`_ +- `自定义算子 <./new_op/index_cn.html>`_ +- `环境变量 <./flags/flags_cn.html>`_ .. toctree:: :hidden: - 01_paddle2.0_introduction/index_cn.rst - 02_paddle2.0_develop/index_cn.rst - 03_VisualDL/index_cn.rst - 04_dygraph_to_static/index_cn.rst - 05_inference_deployment/index_cn.rst + beginner/index_cn.rst + advanced/index_cn.rst + jit/index_cn.rst + infer/index_cn.rst 06_distributed_training/index_cn.rst - 07_new_op/index_cn.rst performance_improving/index_cn.rst - 08_api_mapping/index_cn.rst - 09_hardware_support/index_cn.rst - 10_contribution/index_cn.rst + model_convert/index_cn.rst + hardware_support/index_cn.rst + new_op/index_cn.rst flags/flags_cn.rst diff --git a/docs/guides/index_en.rst b/docs/guides/index_en.rst index 945cdeec478..3f84a2970c7 100644 --- a/docs/guides/index_en.rst +++ b/docs/guides/index_en.rst @@ -9,25 +9,23 @@ Please refer to `PaddlePaddle Github `_ Let's start with studying basic concept of PaddlePaddle: -- `PaddlePaddle Introduction <./01_paddle2.0_introduction/index_en.html>`_ : Introduction of the new features of PaddlePaddle 2.0 and description of the PaddlePaddle 2.0 upgrade guide. -- `Model Visualization <./03_VisualDL/index_en.html>`_ : Introduce VisualDL, a visual tool of PaddlePaddle. -- `Dygraph to Static Graph <./04_dygraph_to_static/index_en.html>`_ : Introduce the transformation of dygraph to static graph. -- `Inference and Deployment <./05_inference_deployment/index_en.html>`_ : Introduce the method of using the trained model to inference. +- `Model Development <./beginner/index_en.html>`_ +- `More Uses for Model Development <./advanced/index_en.html>`_ +- `Dygraph to Static Graph <./jit/index_en.html>`_ : Introduce the transformation of dygraph to static graph. +- `Inference and Deployment <./infer/index_en.html>`_ : Introduce the method of using the trained model to inference. - `Distributed Training <./06_distributed_training/index_en.html>`_ : Introduce how the PaddlePaddle uses distributed training -- `Customize OP <./07_new_op/index_en.html>`_ : Introduce how to customize OP for PaddlePaddle. - `Performance Improving <./performance_improving/index_en.html>`_ : Introduce how to improve performance of PaddlePaddle. -- `Contribution <./10_contribution/index_en.html>`_: Introduce how to contribute for PaddlePaddle. +- `Model Convert <./model_convert/index_en.html>`_ : Introduce how to convert your model to PaddlePaddle. - `FLAGS <./flags/flags_en.html>`_ : Introduce the envirenment flags in paddle. .. toctree:: :hidden: - 01_paddle2.0_introduction/index_en.rst - 03_VisualDL/index_en.rst - 04_dygraph_to_static/index_en.rst - 05_inference_deployment/index_en.rst + beginner/index_en.rst + advanced/index_en.rst + jit/index_en.rst + infer/index_en.rst 06_distributed_training/index_en.rst - 07_new_op/index_en.rst performance_improving/index_en.rst - 10_contribution/index_en.rst + model_convert/index_en.rst flags/flags_en.rst diff --git a/docs/guides/05_inference_deployment/images/inference_ecosystem.png b/docs/guides/infer/images/inference_ecosystem.png similarity index 100% rename from docs/guides/05_inference_deployment/images/inference_ecosystem.png rename to docs/guides/infer/images/inference_ecosystem.png diff --git a/docs/guides/05_inference_deployment/index_cn.rst b/docs/guides/infer/index_cn.rst similarity index 100% rename from docs/guides/05_inference_deployment/index_cn.rst rename to docs/guides/infer/index_cn.rst diff --git a/docs/guides/05_inference_deployment/index_en.rst b/docs/guides/infer/index_en.rst similarity index 60% rename from docs/guides/05_inference_deployment/index_en.rst rename to docs/guides/infer/index_en.rst index 29ac0e214ba..9718091a8d5 100644 --- a/docs/guides/05_inference_deployment/index_en.rst +++ b/docs/guides/infer/index_en.rst @@ -2,12 +2,9 @@ Deploy Inference Model ####################### -- `Server side Deployment `_ : This section illustrates the method how to deploy and release the trained models on the servers - - `Model Compression `_ : Introduce the features and usage of PaddleSlim which is a toolkit for model compression. .. toctree:: :hidden: - inference/index_en.rst paddleslim/paddle_slim_en.rst diff --git a/docs/guides/05_inference_deployment/inference/images/inference.png b/docs/guides/infer/inference/images/inference.png similarity index 100% rename from docs/guides/05_inference_deployment/inference/images/inference.png rename to docs/guides/infer/inference/images/inference.png diff --git a/docs/guides/05_inference_deployment/inference/images/paddlepaddle.png b/docs/guides/infer/inference/images/paddlepaddle.png similarity index 100% rename from docs/guides/05_inference_deployment/inference/images/paddlepaddle.png rename to docs/guides/infer/inference/images/paddlepaddle.png diff --git a/docs/guides/05_inference_deployment/inference/images/wechat.png b/docs/guides/infer/inference/images/wechat.png similarity index 100% rename from docs/guides/05_inference_deployment/inference/images/wechat.png rename to docs/guides/infer/inference/images/wechat.png diff --git a/docs/guides/05_inference_deployment/inference/inference_cn.md b/docs/guides/infer/inference/inference_cn.md similarity index 100% rename from docs/guides/05_inference_deployment/inference/inference_cn.md rename to docs/guides/infer/inference/inference_cn.md diff --git a/docs/guides/05_inference_deployment/mobile/mobile_index_cn.md b/docs/guides/infer/mobile/mobile_index_cn.md similarity index 100% rename from docs/guides/05_inference_deployment/mobile/mobile_index_cn.md rename to docs/guides/infer/mobile/mobile_index_cn.md diff --git a/docs/guides/05_inference_deployment/paddleslim/paddle_slim_cn.md b/docs/guides/infer/paddleslim/paddle_slim_cn.md similarity index 100% rename from docs/guides/05_inference_deployment/paddleslim/paddle_slim_cn.md rename to docs/guides/infer/paddleslim/paddle_slim_cn.md diff --git a/docs/guides/05_inference_deployment/paddleslim/paddle_slim_en.rst b/docs/guides/infer/paddleslim/paddle_slim_en.rst similarity index 100% rename from docs/guides/05_inference_deployment/paddleslim/paddle_slim_en.rst rename to docs/guides/infer/paddleslim/paddle_slim_en.rst diff --git a/docs/guides/04_dygraph_to_static/basic_usage_cn.md b/docs/guides/jit/basic_usage_cn.md similarity index 100% rename from docs/guides/04_dygraph_to_static/basic_usage_cn.md rename to docs/guides/jit/basic_usage_cn.md diff --git a/docs/guides/04_dygraph_to_static/basic_usage_en.md b/docs/guides/jit/basic_usage_en.md similarity index 100% rename from docs/guides/04_dygraph_to_static/basic_usage_en.md rename to docs/guides/jit/basic_usage_en.md diff --git a/docs/guides/04_dygraph_to_static/case_analysis_cn.md b/docs/guides/jit/case_analysis_cn.md similarity index 100% rename from docs/guides/04_dygraph_to_static/case_analysis_cn.md rename to docs/guides/jit/case_analysis_cn.md diff --git a/docs/guides/04_dygraph_to_static/debugging_cn.md b/docs/guides/jit/debugging_cn.md similarity index 100% rename from docs/guides/04_dygraph_to_static/debugging_cn.md rename to docs/guides/jit/debugging_cn.md diff --git a/docs/guides/04_dygraph_to_static/debugging_en.md b/docs/guides/jit/debugging_en.md similarity index 100% rename from docs/guides/04_dygraph_to_static/debugging_en.md rename to docs/guides/jit/debugging_en.md diff --git a/docs/guides/04_dygraph_to_static/grammar_list_cn.md b/docs/guides/jit/grammar_list_cn.md similarity index 100% rename from docs/guides/04_dygraph_to_static/grammar_list_cn.md rename to docs/guides/jit/grammar_list_cn.md diff --git a/docs/guides/04_dygraph_to_static/grammar_list_en.md b/docs/guides/jit/grammar_list_en.md similarity index 100% rename from docs/guides/04_dygraph_to_static/grammar_list_en.md rename to docs/guides/jit/grammar_list_en.md diff --git a/docs/guides/04_dygraph_to_static/images/c++_error_log.png b/docs/guides/jit/images/c++_error_log.png similarity index 100% rename from docs/guides/04_dygraph_to_static/images/c++_error_log.png rename to docs/guides/jit/images/c++_error_log.png diff --git a/docs/guides/04_dygraph_to_static/images/convert_cond.png b/docs/guides/jit/images/convert_cond.png similarity index 100% rename from docs/guides/04_dygraph_to_static/images/convert_cond.png rename to docs/guides/jit/images/convert_cond.png diff --git a/docs/guides/04_dygraph_to_static/images/dy2stat_error_log.png b/docs/guides/jit/images/dy2stat_error_log.png similarity index 100% rename from docs/guides/04_dygraph_to_static/images/dy2stat_error_log.png rename to docs/guides/jit/images/dy2stat_error_log.png diff --git a/docs/guides/04_dygraph_to_static/images/dygraph_export.png b/docs/guides/jit/images/dygraph_export.png similarity index 100% rename from docs/guides/04_dygraph_to_static/images/dygraph_export.png rename to docs/guides/jit/images/dygraph_export.png diff --git a/docs/guides/04_dygraph_to_static/images/dygraph_to_static.png b/docs/guides/jit/images/dygraph_to_static.png similarity index 100% rename from docs/guides/04_dygraph_to_static/images/dygraph_to_static.png rename to docs/guides/jit/images/dygraph_to_static.png diff --git a/docs/guides/04_dygraph_to_static/images/original_error_log.png b/docs/guides/jit/images/original_error_log.png similarity index 100% rename from docs/guides/04_dygraph_to_static/images/original_error_log.png rename to docs/guides/jit/images/original_error_log.png diff --git a/docs/guides/04_dygraph_to_static/images/pdb_cmd.png b/docs/guides/jit/images/pdb_cmd.png similarity index 100% rename from docs/guides/04_dygraph_to_static/images/pdb_cmd.png rename to docs/guides/jit/images/pdb_cmd.png diff --git a/docs/guides/04_dygraph_to_static/images/pdb_cmd_en.png b/docs/guides/jit/images/pdb_cmd_en.png similarity index 100% rename from docs/guides/04_dygraph_to_static/images/pdb_cmd_en.png rename to docs/guides/jit/images/pdb_cmd_en.png diff --git a/docs/guides/04_dygraph_to_static/images/revise_suggestion.png b/docs/guides/jit/images/revise_suggestion.png similarity index 100% rename from docs/guides/04_dygraph_to_static/images/revise_suggestion.png rename to docs/guides/jit/images/revise_suggestion.png diff --git a/docs/guides/04_dygraph_to_static/images/slice.png b/docs/guides/jit/images/slice.png similarity index 100% rename from docs/guides/04_dygraph_to_static/images/slice.png rename to docs/guides/jit/images/slice.png diff --git a/docs/guides/04_dygraph_to_static/images/static_export.png b/docs/guides/jit/images/static_export.png similarity index 100% rename from docs/guides/04_dygraph_to_static/images/static_export.png rename to docs/guides/jit/images/static_export.png diff --git a/docs/guides/04_dygraph_to_static/images/to_static_export.png b/docs/guides/jit/images/to_static_export.png similarity index 100% rename from docs/guides/04_dygraph_to_static/images/to_static_export.png rename to docs/guides/jit/images/to_static_export.png diff --git a/docs/guides/04_dygraph_to_static/images/to_static_train.png b/docs/guides/jit/images/to_static_train.png similarity index 100% rename from docs/guides/04_dygraph_to_static/images/to_static_train.png rename to docs/guides/jit/images/to_static_train.png diff --git a/docs/guides/04_dygraph_to_static/index_cn.rst b/docs/guides/jit/index_cn.rst similarity index 100% rename from docs/guides/04_dygraph_to_static/index_cn.rst rename to docs/guides/jit/index_cn.rst diff --git a/docs/guides/04_dygraph_to_static/index_en.rst b/docs/guides/jit/index_en.rst similarity index 80% rename from docs/guides/04_dygraph_to_static/index_en.rst rename to docs/guides/jit/index_en.rst index 3dcdc63ceb8..8a9f6a5e096 100644 --- a/docs/guides/04_dygraph_to_static/index_en.rst +++ b/docs/guides/jit/index_en.rst @@ -10,23 +10,15 @@ While dygraph has usability and debug benefits and static graph yields performan We introduce the transformation of dygraph to static graph in the following links: -- `Basic Usage `_ : Introduce the basic usage for @to_static. - `Supported Grammars `_ : Introduce supported grammars and unsupported grammars . -- `Predictive Model Export Tutorial `_ : Introduce the tutorial for exporting predictive model. - -- `Case analysis of InputSpec `_ : Introduce the common case studies of @to_static. - - `Error Debugging Experience `_ : Introduce the debugging methods when using @to_static. .. toctree:: :hidden: - basic_usage_en.rst grammar_list_en.md - export_model_en.md - case_analysis_en.md debugging_en.md diff --git a/docs/guides/04_dygraph_to_static/principle_cn.md b/docs/guides/jit/principle_cn.md similarity index 100% rename from docs/guides/04_dygraph_to_static/principle_cn.md rename to docs/guides/jit/principle_cn.md diff --git a/docs/guides/model_convert/index_cn.rst b/docs/guides/model_convert/index_cn.rst new file mode 100644 index 00000000000..675d360202b --- /dev/null +++ b/docs/guides/model_convert/index_cn.rst @@ -0,0 +1,21 @@ +############### +模型迁移 +############### + +您可以通过下面的内容,了解如何迁移模型到飞桨2.X: + + +- `升级指南 <./update_cn.html>`_: 介绍飞桨框架2.0 的主要变化和如何升级到最新版飞桨。 +- `版本迁移工具 <./migration_cn.html>`_: 介绍飞桨框架版本转换工具的使用。 +- `兼容载入旧格式模型 <./load_old_format_model_cn.html>`_: 介绍飞桨框架如何在2.x版本加载1.x版本保存的模型。 +- `Paddle API映射表 <./paddle_api_mapping_cn.html>`_ : 说明 Paddle 1.8 版本与 Paddle 2.0 API对应关系。 +- `PyTorch API映射表 <./pytorch_api_mapping_cn.html>`_ : 说明 PyTorch 1.8 版本与 Paddle 2.0 API对应关系。 + +.. toctree:: + :hidden: + + update_cn.md + migration_cn.rst + load_old_format_model_cn.rst + paddle_api_mapping_cn.rst + pytorch_api_mapping_cn.rst diff --git a/docs/guides/model_convert/index_en.rst b/docs/guides/model_convert/index_en.rst new file mode 100644 index 00000000000..e1452214534 --- /dev/null +++ b/docs/guides/model_convert/index_en.rst @@ -0,0 +1,12 @@ +##################### +Model Convert +##################### + +Introduction of how to convert your model to Paddle 2.X. + +- `update <./update_en.html>`_ : update guides for paddle 2.0. + +.. toctree:: + :hidden: + + update_en.md diff --git a/docs/guides/01_paddle2.0_introduction/load_old_format_model_cn.rst b/docs/guides/model_convert/load_old_format_model_cn.rst similarity index 100% rename from docs/guides/01_paddle2.0_introduction/load_old_format_model_cn.rst rename to docs/guides/model_convert/load_old_format_model_cn.rst diff --git a/docs/guides/01_paddle2.0_introduction/migration_cn.rst b/docs/guides/model_convert/migration_cn.rst similarity index 100% rename from docs/guides/01_paddle2.0_introduction/migration_cn.rst rename to docs/guides/model_convert/migration_cn.rst diff --git a/docs/guides/08_api_mapping/paddle_api_mapping_cn.rst b/docs/guides/model_convert/paddle_api_mapping_cn.rst similarity index 100% rename from docs/guides/08_api_mapping/paddle_api_mapping_cn.rst rename to docs/guides/model_convert/paddle_api_mapping_cn.rst diff --git a/docs/guides/08_api_mapping/pytorch_api_mapping_cn.md b/docs/guides/model_convert/pytorch_api_mapping_cn.md similarity index 100% rename from docs/guides/08_api_mapping/pytorch_api_mapping_cn.md rename to docs/guides/model_convert/pytorch_api_mapping_cn.md diff --git a/docs/guides/01_paddle2.0_introduction/update_cn.md b/docs/guides/model_convert/update_cn.md similarity index 100% rename from docs/guides/01_paddle2.0_introduction/update_cn.md rename to docs/guides/model_convert/update_cn.md diff --git a/docs/guides/01_paddle2.0_introduction/update_en.md b/docs/guides/model_convert/update_en.md similarity index 100% rename from docs/guides/01_paddle2.0_introduction/update_en.md rename to docs/guides/model_convert/update_en.md diff --git a/docs/guides/07_new_op/index_cn.rst b/docs/guides/new_op/index_cn.rst similarity index 61% rename from docs/guides/07_new_op/index_cn.rst rename to docs/guides/new_op/index_cn.rst index 43701ad20f1..0c58c07093f 100644 --- a/docs/guides/07_new_op/index_cn.rst +++ b/docs/guides/new_op/index_cn.rst @@ -2,7 +2,7 @@ 自定义算子 ############# -本部分将指导您如何使用飞桨的自定义算子(Operator,简称Op)机制,包括以下两类: +介绍如何使用飞桨的自定义算子(Operator,简称Op)机制,包括以下两类: 1. C++算子:编写方法较为简洁,不涉及框架内部概念,无需重新编译飞桨框架,以外接模块的方式使用的算子 2. Python算子:使用Python编写实现前向(forward)和反向(backward)方法,在模型组网中使用的自定义API @@ -11,12 +11,10 @@ - `自定义Python算子 <./new_python_op_cn.html>`_ -- `Kernel Primitives API <./kernel_primitive_api/index_cn.html>`_ : 介绍 PaddlePaddle 为加快算子开发提供的 Block 级 CUDA 函数。 .. toctree:: :hidden: - op_notes_cn.md new_custom_op_cn.md new_python_op_cn.md - kernel_primitive_api/index_cn.rst + diff --git a/docs/guides/07_new_op/new_custom_op_cn.md b/docs/guides/new_op/new_custom_op_cn.md similarity index 100% rename from docs/guides/07_new_op/new_custom_op_cn.md rename to docs/guides/new_op/new_custom_op_cn.md diff --git a/docs/guides/07_new_op/new_python_op_cn.md b/docs/guides/new_op/new_python_op_cn.md similarity index 100% rename from docs/guides/07_new_op/new_python_op_cn.md rename to docs/guides/new_op/new_python_op_cn.md diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/amp_cn.md b/docs/guides/performance_improving/amp_cn.md similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/amp_cn.md rename to docs/guides/performance_improving/amp_cn.md diff --git a/docs/guides/01_paddle2.0_introduction/basic_concept/amp_en.md b/docs/guides/performance_improving/amp_en.md similarity index 100% rename from docs/guides/01_paddle2.0_introduction/basic_concept/amp_en.md rename to docs/guides/performance_improving/amp_en.md diff --git a/docs/guides/performance_improving/index_cn.rst b/docs/guides/performance_improving/index_cn.rst index 3ba00a463ab..13208283134 100644 --- a/docs/guides/performance_improving/index_cn.rst +++ b/docs/guides/performance_improving/index_cn.rst @@ -4,16 +4,13 @@ 你可以通过以下内容,了解飞桨框架性能调优相关的内容: +- `自动混合精度训练 <./amp_cn.html>`_ : 使用飞桨框架进行自动混合精度训练。 - `模型量化 <./quantization.html>`_ : 使用飞桨框架进行模型量化。 - -.. toctree:: - :hidden: - - quantization.md - - `模型性能分析 <./profiling_model.html>`_ : 使用飞桨性能分析器对模型进行性能调试。 .. toctree:: :hidden: + amp_cn.md + quantization.md profiling_model.md \ No newline at end of file diff --git a/docs/guides/performance_improving/index_en.rst b/docs/guides/performance_improving/index_en.rst index b0743bc8acd..cda6e67b286 100644 --- a/docs/guides/performance_improving/index_en.rst +++ b/docs/guides/performance_improving/index_en.rst @@ -1,7 +1,11 @@ -############### -Practice Improving -############### +####################### +Performance Improving +####################### + + +- `AMP <./amp_en.html>`_ .. toctree:: :maxdepth: 1 + amp_en.md \ No newline at end of file diff --git a/docs/release_note_cn.md b/docs/release_note_cn.md index 3932d35dc0f..7a3df48682f 100644 --- a/docs/release_note_cn.md +++ b/docs/release_note_cn.md @@ -1,9 +1,9 @@ -# 2.3.0-rc0 Release Note +# 2.3.0 Release Note ## 1. 重要更新 -我们很高兴地发布飞桨框架 2.3.0-rc0 版本,本版本包含如下重要更新。 +我们很高兴地发布飞桨框架 2.3.0 版本,本版本包含如下重要更新。 ### API @@ -33,7 +33,7 @@ ### 编译安装 -- 从 2.3.0-rc0 版本开始,飞桨对框架支持的 GPU 架构种类进行了调整和升级。 +- 从 2.3.0 版本开始,飞桨对框架支持的 GPU 架构种类进行了调整和升级。 ### 推理部署 @@ -53,6 +53,8 @@ ## 2. 不兼容升级 +- 预编译安装包中移除CUDA sm35 ARCH: 受到包体积大小的影响,在预编译的安装包中移除了 CUDA sm35 架构。 ([#41754](https://github.com/PaddlePaddle/Paddle/pull/41754)) + - `paddle.to_tensor` 将一个 python int scalar 转换为 Tensor 时,在 Windows 上的默认数据类型由 int32 变为 int64,从而与 Linux/Mac 保持对齐。([#39662](https://github.com/PaddlePaddle/Paddle/pull/39662)) - 为了与 python3 下的除法行为保持一致,除法符号 `/` 从 rounding divide 变成 true divide,计算输出结果的数据类型从 int 切换成 float。 ([#40890](https://github.com/PaddlePaddle/Paddle/pull/40890)) @@ -63,7 +65,7 @@ 2.2 -2.3.0-rc0 +2.3.0 @@ -105,7 +107,7 @@ Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True, 2.2 -2.3.0-rc0 +2.3.0 @@ -402,7 +404,7 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - 新增学习率类 API - - 新增 `paddle.optimizer.lr.MultiplicativeDecay`,提供 `lambda` 函数设置学习率的策略。([#38250](https://github.com/PaddlePaddle/Paddle/pull/38250)) + - 新增 `paddle.optimizer.lr.MultiplicativeDecay`,提供 `lambda` 函数设置学习率的策略。([#38250](https://github.com/PaddlePaddle/Paddle/pull/38250)) - 新增分布式相关 API @@ -416,6 +418,12 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - 新增 `paddle.incubate.multiprocessing`模块,支持 Tensor(CPU/GPU)在 python 进程间传输。([#37302](https://github.com/PaddlePaddle/Paddle/pull/37302), [#41339](https://github.com/PaddlePaddle/Paddle/pull/41339)) +- 新增 `paddle.incubate.autotune.set_config` API,支持多版本 Kernel 自动选择、混合精度数据布局自动转换、DataLoader 的 num_workers 自动选择,以自动提升模型性能。([#42301](https://github.com/PaddlePaddle/Paddle/pull/42301)) + +- 新增 `paddle.incubate.nn.FusedMultiTransformer` 和 `paddle.incubate.nn.functional.fused_multi_transformer` API,可将多层 transformer 融合到一个 op 中,提升模型推理性能,注意:仅支持前向推理。([#42311](https://github.com/PaddlePaddle/Paddle/pull/42311)) + +- 新增动静统一的 einsum_v2 op,兼容原有 python 端 `paddle.einsum` 实现的同时支持动转静导出和更加完备的 Infershape 推导。([#42495](https://github.com/PaddlePaddle/Paddle/pull/42495), [#42327](https://github.com/PaddlePaddle/Paddle/pull/42327), [#42397](https://github.com/PaddlePaddle/Paddle/pull/42397), [#42105](https://github.com/PaddlePaddle/Paddle/pull/42105)) + #### IR(Intermediate Representation) - 动态图转静态图 @@ -503,7 +511,7 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - **自定义算子机制与 Phi 整合并完善**:支持在自定义算子编写时调用 Phi 自动生成的200余个C++运算类 API,降低自定义算子开发成本,并进行一系列问题修复。([#37122](https://github.com/PaddlePaddle/Paddle/pull/37122), [#37276](https://github.com/PaddlePaddle/Paddle/pull/37276), [#37281](https://github.com/PaddlePaddle/Paddle/pull/37281), [#37262](https://github.com/PaddlePaddle/Paddle/pull/37281), [#37415](https://github.com/PaddlePaddle/Paddle/pull/37415), [#37423](https://github.com/PaddlePaddle/Paddle/pull/37423), [#37583](https://github.com/PaddlePaddle/Paddle/pull/37683), [#38776](https://github.com/PaddlePaddle/Paddle/pull/38776), [#39353](https://github.com/PaddlePaddle/Paddle/pull/39353), [#41072](https://github.com/PaddlePaddle/Paddle/pull/41072)) - **算子规模化迁移改写**:迁移了约250个高频算子的前、反向算子内核 Kernel 至新算子库,改写为函数式,支持在 C++端通过调用多个基础 Kernel 函数封装,快速组合实现高性能算子;同时,添加相应的 yaml 算子定义,并接入新动态图执行体系,提升 python API 调度性能。迁移改写的算子包括: - + - sqrt ([#40727](https://github.com/PaddlePaddle/Paddle/pull/40727)) - square([#40727](https://github.com/PaddlePaddle/Paddle/pull/40727)) @@ -1006,6 +1014,8 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - hard_sigmoid ([#40626](https://github.com/PaddlePaddle/Paddle/pull/40626)) + - exp, det, assign, gaussian_random, matrix_rank, eye, deformable_conv。([#41755]exp, det, assign, gaussian_random, matrix_rank, eye, deformable_conv。([#41755](https://github.com/PaddlePaddle/Paddle/pull/41755), [#41737](https://github.com/PaddlePaddle/Paddle/pull/41737) + #### 新动态图执行机制 针对飞桨原动态图执行机制的调度性能、二次开发能力差的问题,我们重构了动态图的底层执行机制。通过全新的调用执行方式,配合 Phi 算子库进行高效的运行时执行,对于 Phi 算子库支持的算子,切换到新动态图模式能体验到调度性能有较大幅度的提升。但是由于整体框架执行机制升级的工作量巨大,且该部分工作耦合了大量 Phi 算子库的工作, 因此在这个版本下我们仍未默认使用该执行方式。如果想要试用可以通过设置环境变量 `FLAGS_enable_eager_mode=1` 来切换使用。具体包括如下内容: @@ -1034,28 +1044,38 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - **动态图重构后支持 inplace 策略**:输入与输出为同一个 Tensor。 - - - 为动态图重构中间态适配 inplace 策略。([#40400](https://github.com/PaddlePaddle/Paddle/pull/40400)) - - - 为动态图重构最终态适配 inplace 策略。([#40695](https://github.com/PaddlePaddle/Paddle/pull/40695)) + - 为动态图重构中间态适配 inplace 策略。([#40400](https://github.com/PaddlePaddle/Paddle/pull/40400)) - - 动态图重构后,为 PyLayer 功能添加 inplace 策略。([#41043](https://github.com/PaddlePaddle/Paddle/pull/41043)) - - - 动态图重构后,为 Tensor 的 setitem 功能添加 inplace 策略。([#40915](https://github.com/PaddlePaddle/Paddle/pull/40915)) - - - 动态图重构后添加`_reset_grad_inplace_version`接口,将 Tensor 的梯度的 inplace version 置为0。([#41101](https://github.com/PaddlePaddle/Paddle/pull/41101)) - - - 反向计算过程中如果不需要前向 Tensor 的值(no need buffer 属性),则不需要对该 Tensor 进行 inplace version 的检测操作。 为 no_need_buffer 的 Tensor 跳过 inplace version 的检查。([#41350](https://github.com/PaddlePaddle/Paddle/pull/41350)) - - - 统一动态图重构后与重构前对 inplace version 检查的报错信息。([#41209](https://github.com/PaddlePaddle/Paddle/pull/41209)) + - 为动态图重构最终态适配 inplace 策略。([#40695](https://github.com/PaddlePaddle/Paddle/pull/40695)) + + - 动态图重构后,为 PyLayer 功能添加 inplace 策略。([#41043](https://github.com/PaddlePaddle/Paddle/pull/41043)) + + - 动态图重构后,为 Tensor 的 setitem 功能添加 inplace 策略。([#40915](https://github.com/PaddlePaddle/Paddle/pull/40915)) + + - 动态图重构后添加`_reset_grad_inplace_version`接口,将 Tensor 的梯度的 inplace version 置为0。([#41101](https://github.com/PaddlePaddle/Paddle/pull/41101)) + + - 反向计算过程中如果不需要前向 Tensor 的值(no need buffer 属性),则不需要对该 Tensor 进行 inplace version 的检测操作。 为 no_need_buffer 的 Tensor 跳过 inplace version 的检查。([#41350](https://github.com/PaddlePaddle/Paddle/pull/41350)) + + - 统一动态图重构后与重构前对 inplace version 检查的报错信息。([#41209](https://github.com/PaddlePaddle/Paddle/pull/41209)) - **动态图重构后支持 view 策略**:输入与输出 Tensor 共享底层数据。 - - - 为动态图重构中间态适配 view 机制。包括`reshape`、`squeeze`、`unsqueeze`、`flatten` API。([#40830](https://github.com/PaddlePaddle/Paddle/pull/40830)) + - 为动态图重构中间态适配 view 机制。包括`reshape`、`squeeze`、`unsqueeze`、`flatten` API。([#40830](https://github.com/PaddlePaddle/Paddle/pull/40830)) - - 为动态图重构最终态适配 view 机制。包括`reshape` API。([#40891](https://github.com/PaddlePaddle/Paddle/pull/40891)) + - 为动态图重构最终态适配 view 机制。包括`reshape` API。([#40891](https://github.com/PaddlePaddle/Paddle/pull/40891)) -#### 全新静态图执行器 +- **添加支持新动态图 eager Tensor 在 python 端的 weakref**。([#41797](https://github.com/PaddlePaddle/Paddle/pull/41797)) + +- **增强新动态图 DoubleGrad 功能**,支持基础的 DoubleGrad 功能。([#41893](https://github.com/PaddlePaddle/Paddle/pull/41893), [#41894](https://github.com/PaddlePaddle/Paddle/pull/41894), [#41895](https://github.com/PaddlePaddle/Paddle/pull/41895)) + +- **新增 `core.eager.StringTensor` 接口**,支持在 python 端构造 StringTensor 以及使用 StringTensor 相关 API。([#41039](https://github.com/PaddlePaddle/Paddle/pull/41039)) + +- **为 `core.eager.Tensor` 新增 `*grad_name` 和 `_grad_value` API**,返回梯度的名称和值。([#41990](https://github.com/PaddlePaddle/Paddle/pull/41990)) + +- **为动态图中间态添加对 no_need_buffer 属性的处理**。在 inplace 反向检查操作中,会跳过具有 no_need_buffer 属性的 Tensor 的检查。([#41720](https://github.com/PaddlePaddle/Paddle/pull/41720)) + +#### 全新静态图执行器 为了解决飞桨原静态图执行器在部分场景下调度性能不够理想,不便于扩展多 stream 等问题,我们实现了全新的性能优越,易于扩展的静态图执行器,充分利用了多 stream、多线程的异步调度能力。新执行器相当于原执行器是兼容升级,目前已在单机单卡场景下默认使用,用户不需要在训练代码中做任何修改即可自动使用。当然,我们也提供了接口来切换回原执行器,用户可以通过设置环境变量 `FLAGS_USE_STANDALONE_EXECUTOR=false` 来切换回原执行器。([#41179](https://github.com/PaddlePaddle/Paddle/pull/41179)) 主要内容如下: - 基础组件:用于执行器中多线程算子调度的高性能线程池 ([#35470](https://github.com/PaddlePaddle/Paddle/pull/35470), [#35930](https://github.com/PaddlePaddle/Paddle/pull/35930), [#36030](https://github.com/PaddlePaddle/Paddle/pull/36030), [#36480](https://github.com/PaddlePaddle/Paddle/pull/36480), [#36688](https://github.com/PaddlePaddle/Paddle/pull/36688), [#36740](https://github.com/PaddlePaddle/Paddle/pull/36740), [#38335](https://github.com/PaddlePaddle/Paddle/pull/38335), [#40770](https://github.com/PaddlePaddle/Paddle/pull/40770)) 及线程协同组件 ([#38779](https://github.com/PaddlePaddle/Paddle/pull/38779), [#40876](https://github.com/PaddlePaddle/Paddle/pull/40876), [#40912](https://github.com/PaddlePaddle/Paddle/pull/40912)),算子执行后及时地显存回收 ([#37642](https://github.com/PaddlePaddle/Paddle/pull/37642), [#39617](https://github.com/PaddlePaddle/Paddle/pull/39617), [#40859](https://github.com/PaddlePaddle/Paddle/pull/40859)),并行执行器新依赖分析算法 ([#37231](https://github.com/PaddlePaddle/Paddle/pull/37231)) 等。 @@ -1066,6 +1086,11 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - 增强多线程场景下调试和报错功能,将子线程的报错捕获到主线程中统一抛出,以提升用户体验。([#36692](https://github.com/PaddlePaddle/Paddle/pull/36692),[#36802](https://github.com/PaddlePaddle/Paddle/pull/36802)) +- 修复新执行器通信流重置 Allocator 中 stream 缓存信息的问题,减少跨 stream 场景下的 RecordStream 开销,优化后 DeepFM 模型性能提升约8%。([#42046](https://github.com/PaddlePaddle/Paddle/pull/42046)) + +- 优化新执行器算子间的依赖分析方法,提升运行性能;为 send/recv 通信算子建立正确依赖以支持流水线并行。([#42009](https://github.com/PaddlePaddle/Paddle/pull/42009)) + + #### 分布式训练 - 集合通信多机多卡训练基础功能 @@ -1147,6 +1172,8 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - 统一参数服务器下,重构通信、存储等各个模块基类,提升各个模块的易二次开发性。([#41207](https://github.com/PaddlePaddle/Paddle/pull/41207), [#41022](https://github.com/PaddlePaddle/Paddle/pull/41022), [#40702](https://github.com/PaddlePaddle/Paddle/pull/40702), [#39341](https://github.com/PaddlePaddle/Paddle/pull/39341) [#39377](https://github.com/PaddlePaddle/Paddle/pull/39377), [#39191](https://github.com/PaddlePaddle/Paddle/pull/39191), [#39064](https://github.com/PaddlePaddle/Paddle/pull/39064)) - 统一参数服务器下,新增评估指标模块,支持 AUC/WuAUC/MaskAuc 等评估指标计算及可自定义扩展。 ([#38789](https://github.com/PaddlePaddle/Paddle/pull/38789)) + + - 支持在昆仑2芯片上的 XPU 参数服务器训练。 ([#41917](https://github.com/PaddlePaddle/Paddle/pull/41917), [#42266](https://github.com/PaddlePaddle/Paddle/pull/42266), [#41916](https://github.com/PaddlePaddle/Paddle/pull/41916)) #### Profiler @@ -1182,6 +1209,90 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - Profiler 支持分级。([#39926](https://github.com/PaddlePaddle/Paddle/pull/39926)) +- 修改新动态图下 op 的打点名称和类型。([#41771](https://github.com/PaddlePaddle/Paddle/pull/41771/) + +- 添加 Kernel 表单,以及优化表单内容的展示方式。 ([#41989](https://github.com/PaddlePaddle/Paddle/pull/41989) + +- 消除 Profiler 关闭情况下对模型前向计算造成性能下降的影响。([#42142](https://github.com/PaddlePaddle/Paddle/pull/42142)) + +#### CINN 编译器接入 + +飞桨的编译器功能在逐步丰富中,针对 CINN([GitHub - PaddlePaddle/CINN: Compiler Infrastructure for Neural Networks](https://github.com/PaddlePaddle/CINN)) 的变更,Paddle 侧接入也进行了相对应的更改,以适配编译器 CINN 的功能。其中主要包括增加Paddle-CINN 运行流程的子图管理相关功能,显存和速度性能的优化、开发过程发现的 bug 修复。 + +- 功能开发: + + - 子图 op 相关: + + - 添加从计算图中找到并生成 CINN 子图的功能。([#36345](https://github.com/PaddlePaddle/Paddle/pull/36345)) + + - 新增 cinn_launch op 作为运行时接入 CINN 的入口,负责调度 CINN 对子图进行编译、初始化数据空间、调度生成 Kernel 的执行。([#36600](https://github.com/PaddlePaddle/Paddle/pull/36600)) + + - 为 cinn_launch op 的 Kernel 实现添加辅助类 CinnLaunchContext 管理子图编译、运行的中间数据,提升可扩展性和代码可读性。([#37938](https://github.com/PaddlePaddle/Paddle/pull/37938)) + + - 为 CINN 子图添加额外的 fetch 结点,从而保证 CINN 外部结点能取到待fetch变量的值。([#37172](https://github.com/PaddlePaddle/Paddle/pull/37172), [#37190](https://github.com/PaddlePaddle/Paddle/pull/37190)) + + - 添加对 CINN 子图符号化的功能,符号化用于拓扑排序子图并返回 CINN 执行序列。([#36417](https://github.com/PaddlePaddle/Paddle/pull/36417) + + - 新增 CinnCompiler 类,用于调用 CINN 编译模型中可使用 CINN 算子替换的子图。 ([#36562](https://github.com/PaddlePaddle/Paddle/pull/36562), [#36975](https://github.com/PaddlePaddle/Paddle/pull/36975)) + + - 为 CINN 符号化类新增获取子图 fetch 变量名的接口,防止编译优化中将 fetch 变量融合消除。([#37218](https://github.com/PaddlePaddle/Paddle/pull/37218)) + + - 程序开发检查、debug、API 变更相关: + + - 同步更新 CINN 中 NetBuilder API 名称的变化。([#40392](https://github.com/PaddlePaddle/Paddle/pull/40392)) + + - 为 Paddle-CINN 添加必要的用于 debug 的日志信息。([#36867](https://github.com/PaddlePaddle/Paddle/pull/36867)) + + - 添加 Paddle desc 与 CINN desc 互转函数。([#36100](https://github.com/PaddlePaddle/Paddle/pull/36100)) + + - 相比 Paddle,CINN 中实现的算子可能存在未使用到某些输入变量,因此在 cinn_launch op 中去除对输入变量必须被使用的检查。([#37119](https://github.com/PaddlePaddle/Paddle/pull/37119)) + + - 新增 cinn_instruction_run op 用于调用 CINN 执行单个生成指令,便于 Paddle 侧构建 Graph 调度运行子图。([#39435](https://github.com/PaddlePaddle/Paddle/pull/39435), [#39576](https://github.com/PaddlePaddle/Paddle/pull/39576)) + + - 在 Paddle 中添加编译 CINN 所需的 CUDA/CUBLAS/MKL/CINN pass 应用等控制宏。([#37066](https://github.com/PaddlePaddle/Paddle/pull/37066), [#36660](https://github.com/PaddlePaddle/Paddle/pull/36660)) + + - 增加 FLAGS_allow_cinn_ops 和 FLAGS_deny_cinn_ops 两个控制标记,用于控制 Paddle 训练中使用 CINN 算子代替原生算子的种类。([#36842](https://github.com/PaddlePaddle/Paddle/pull/36842)) + +- 性能优化: + + - 速度优化 + + - 优化 CinnCacheKey 的计算耗时。([#37786](https://github.com/PaddlePaddle/Paddle/pull/37786), [#37317](https://github.com/PaddlePaddle/Paddle/pull/37317)) + + - 缓存 CINN 编译子图的变量 scope,降低运行参数构造开销。([#37983](https://github.com/PaddlePaddle/Paddle/pull/37983)) + + - 子图编译时接入 CINN 自动调优,支持通过 flag 启用,便于后续进一步调优训练性能。([#41795](https://github.com/PaddlePaddle/Paddle/pull/41795)) + + - 重构子图编译时对编译结果的正确性校验,避免运行时重复检查,降低调度开销。([#41777](https://github.com/PaddlePaddle/Paddle/pull/41777)) + + - 在 Paddle-CINN 训练功能中默认启用 TransposeFolding 和 GemmRewriter 优化 pass。([#41084](https://github.com/PaddlePaddle/Paddle/pull/41084)) + + - 将 Paddle 中创建的 cuda stream 传入 CINN,使得 Paddle 和 CINN 执行计算时共用同一个 CUDA stream。([#37337](https://github.com/PaddlePaddle/Paddle/pull/37337)) + + - 将 CINN 优化 pass 应用逻辑从 Paddle 中移动到 CINN 中。([#42047](https://github.com/PaddlePaddle/Paddle/pull/42047), [#42070](https://github.com/PaddlePaddle/Paddle/pull/42070)) + + - 显存优化 + + - 为 cinn_launch op 添加 NoNeedBufferVars 声明无须 buffer 的输入变量列表,以便显存优化提前释放无效空间。([#38367](https://github.com/PaddlePaddle/Paddle/pull/38367)) + + - 传入子图外部变量的引用计数信息,便于 cinn_launch 内子图复用显存优化 pass,降低使用 CINN 的显存开销。([#39209](https://github.com/PaddlePaddle/Paddle/pull/39209), [#39622](https://github.com/PaddlePaddle/Paddle/pull/39622)) + + - 添加 CINN 编译生成的可执行指令集合转换为 Paddle Graph 的功能,支持复用 Paddle 调度器及显存优化 pass,进一步降低使用 CINN 的显存开销。([#39724](https://github.com/PaddlePaddle/Paddle/pull/39724), [#39911](https://github.com/PaddlePaddle/Paddle/pull/39911)) + + - 添加 cinn_instruction_run op 的 Kernel 支持根据编译结果推断的数据类型动态申请空间。([#40920](https://github.com/PaddlePaddle/Paddle/pull/40920)) + +- 问题修复: + + - 修复并优化 CINN 子图的生成逻辑。([#36503](https://github.com/PaddlePaddle/Paddle/pull/36503)) + + - 修复 Paddle-CINN 不支持无输入子图的问题。([#40814](https://github.com/PaddlePaddle/Paddle/pull/40814)) + + - 修复由于 CINN 无法处理 batch_norm 等算子中存在的无用输出而报错的问题。([#36996](https://github.com/PaddlePaddle/Paddle/pull/36996)) + + - 修复若干 CINN 子图划分以及符号化中存在的 bug,解决 Paddle 训练接入 CINN 全流程打通过程中遇到的问题。 ([#36739](https://github.com/PaddlePaddle/Paddle/pull/36739), [#36698](https://github.com/PaddlePaddle/Paddle/pull/36698) ) + + - CINN 尚不支持控制流,添加遇控制流跳过的逻辑。([#40812](https://github.com/PaddlePaddle/Paddle/pull/40812)) + #### 其他 - 模型量化 @@ -1274,6 +1385,10 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - LayerNorm ([#40418](https://github.com/PaddlePaddle/Paddle/pull/40418)) +- 增加基于 SSD-内存-GPU显存 的3级存储图检索引擎,支持大规模图神经网络训练。([#42472](https://github.com/PaddlePaddle/Paddle/pull/42472), [#42321](https://github.com/PaddlePaddle/Paddle/pull/42321), [#42027](https://github.com/PaddlePaddle/Paddle/pull/42027)) + +- 增加异构多云训练通信模块 switch,实现 Send/Recv 接口,支持多云异构通信。([#40965](https://github.com/PaddlePaddle/Paddle/pull/40965) [40911](https://github.com/PaddlePaddle/Paddle/pull/40911)) + ### (2)功能优化 #### API @@ -1346,7 +1461,13 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - 完善`paddle.amp.GradScaler`调用 check_finite_and_unscale op 的逻辑,消除该处创建 bool 变量所引入的 cudaMemcpy。([#37770](https://github.com/PaddlePaddle/Paddle/pull/37770)) -- 新增对 unstack 和 unique op 元素个数为0的 Tensor 增加检查。([#36021](https://github.com/PaddlePaddle/Paddle/pull/36021)) +- 新增对 unstack 和 unique op 元素个数为0的 Tensor 增加检查。([#36021](https://github.com/PaddlePaddle/Paddle/pull/36021)) + +- 新增支持昆仑2的多层、双向 LSTM 功能,完善 RNN 前反向 op,支持时序类模型训练使用。([#](https://github.com/PaddlePaddle/Paddle/pull/41781)[42076](https://github.com/PaddlePaddle/Paddle/pull/42076)) + +- 新增支持昆仑2的 bce_loss 前反向 op。([#41610](https://github.com/PaddlePaddle/Paddle/pull/41610)) + +- 添加 `paddle.linalg.det` 的反向实现。([#36013](https://github.com/PaddlePaddle/Paddle/pull/36013)) #### IR(Intermediate Representation) @@ -1408,7 +1529,23 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - 利用模型中 embedding op 的拓扑关系,优化 embedding op 的合并逻辑以提升性能。[(#35942)](https://github.com/PaddlePaddle/Paddle/pull/35942) -- 通信库:重构通信库,提升通信库的易扩展性和二次开发性,支持异构通信。 ([#41398](https://github.com/PaddlePaddle/Paddle/pull/41398), [#39720](https://github.com/PaddlePaddle/Paddle/pull/39720), [#40911](https://github.com/PaddlePaddle/Paddle/pull/40911), [#40579](https://github.com/PaddlePaddle/Paddle/pull/40579), [#40629](https://github.com/PaddlePaddle/Paddle/pull/40629), [#40437](https://github.com/PaddlePaddle/Paddle/pull/40437), [#40430](https://github.com/PaddlePaddle/Paddle/pull/40430), [#40228](https://github.com/PaddlePaddle/Paddle/pull/40228), [#40181](https://github.com/PaddlePaddle/Paddle/pull/40181), [#40100](https://github.com/PaddlePaddle/Paddle/pull/40100), [#40097](https://github.com/PaddlePaddle/Paddle/pull/40097), [#39892](https://github.com/PaddlePaddle/Paddle/pull/39892), [#39384](https://github.com/PaddlePaddle/Paddle/pull/39384), [#39737](https://github.com/PaddlePaddle/Paddle/pull/39737), [#40040](https://github.com/PaddlePaddle/Paddle/pull/40040)) +- 通信库:重构通信库,提升通信库的易扩展性和二次开发性,支持异构通信。 ([#41398](https://github.com/PaddlePaddle/Paddle/pull/41398), [#39720](https://github.com/PaddlePaddle/Paddle/pull/39720), [#40911](https://github.com/PaddlePaddle/Paddle/pull/40911), [#40579](https://github.com/PaddlePaddle/Paddle/pull/40579), [#40629](https://github.com/PaddlePaddle/Paddle/pull/40629), [#40437](https://github.com/PaddlePaddle/Paddle/pull/40437), [#40430](https://github.com/PaddlePaddle/Paddle/pull/40430), [#40228](https://github.com/PaddlePaddle/Paddle/pull/40228), [#40181](https://github.com/PaddlePaddle/Paddle/pull/40181), [#40100](https://github.com/PaddlePaddle/Paddle/pull/40100), [#40097](https://github.com/PaddlePaddle/Paddle/pull/40097), [#39892](https://github.com/PaddlePaddle/Paddle/pull/39892), [#39384](https://github.com/PaddlePaddle/Paddle/pull/39384), [#39737](https://github.com/PaddlePaddle/Paddle/pull/39737), [#40040](https://github.com/PaddlePaddle/Paddle/pull/40040)) + +- 支持 `paddle.incubate.distributed.models.moe`中 MoE 相关接口(`moe.GShardGate`, `moe.BaseGate`, `moe.SwitchGate`, `moe.MoELayer`, `moe.ClipGradForMOEByGlobalNorm` )的公开。([#42300](https://github.com/PaddlePaddle/Paddle/pull/42300)) + +- 修复 `paddle.incubate.distributed.models.moe.MoELayer` 中使用 recomputing 可能报错的问题。([#42128](https://github.com/PaddlePaddle/Paddle/pull/42128)) + +- 修复新动态图流水线并行因为数据类型不同导致的报错 ([#41937](https://github.com/PaddlePaddle/Paddle/pull/41937) [#42053](https://github.com/PaddlePaddle/Paddle/pull/42053)) + +- 修复新动态图张量模型并行因为数据类型不同导致的报错([#41960](https://github.com/PaddlePaddle/Paddle/pull/41960)) + +#### 自定义算子 + +- 增强 C++自定义算子机制对二阶反向算子编写功能,支持为二阶反向算子的梯度输入变量添加后缀作为输出使用。([#41781](https://github.com/PaddlePaddle/Paddle/pull/41781)) + +- 移除 Tensor API 成员方法中对废弃的枚举类型 PlaceType 的使用,进行相应兼容处理,并添加 deprecated warning 提示。([#41882](https://github.com/PaddlePaddle/Paddle/pull/41882)) + +- 为原 Tensor API 的一系列废弃接口,包括不完整构造函数、reshape、mutable_data、copy_to 方法添加 deprecated warning 提示。([#41882](https://github.com/PaddlePaddle/Paddle/pull/41882)) #### 其他 @@ -1496,6 +1633,26 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - 优化 `Categorical`的 `probs`计算,简化计算逻辑,性能提升 4 ~ 5 倍。([#42178](https://github.com/PaddlePaddle/Paddle/pull/42178)) +- `paddle.sum` 性能优化,性能相比优化前提升约20%。([#42309](https://github.com/PaddlePaddle/Paddle/pull/42309)) + +#### 自动调优 + +新增训练全流程硬件感知性能自动调优功能,在图像分类、分割、检测和图像生成任务上与模型默认参数配置下的性能相比提升约3%~50%以上。通过 `paddle.incubate.autotune.set_config` API设置自动调优状态,当前默认关闭。自动调优具体包括三个层次: + +- `paddle.io.DataLoader` 新增自动调优功能,根据训练数据和设备资源选择最佳的模型 num_workers。 ([#42004](https://github.com/PaddlePaddle/Paddle/pull/42004)) + +- 新增混合精度训练数据布局自动调优功能,根据设备类型和数据类型选择最佳数据布局,并在运行时自动转换。([#41964](https://github.com/PaddlePaddle/Paddle/pull/41964)) + +- 新增 Conv 运行时所需 workspace size 阈值自动调整功能,根据 GPU 当前可申请显存资源情况来自动设置;基于通用的 AlgorithmCache 设计和 Kernel 计时组件,新增 Conv cuDNN 算法自动选择功能,支持数据变长模型。([#41833](https://github.com/PaddlePaddle/Paddle/pull/41833)) + +#### 调度优化 + +- 移除 `paddle.nn.ClipGradByGlobalNorm` 中的 CudaStreamSync 隐藏操作,减少执行时的调度开销,在 ptb 模型上有5%的性能提升。([#42170](https://github.com/PaddlePaddle/Paddle/pull/42170)) + +- 优化一系列底层数据结构及原动态图执行体系中的细节实现,提升原动态图的调度性能。([#42010](https://github.com/PaddlePaddle/Paddle/pull/42010), [#42171](https://github.com/PaddlePaddle/Paddle/pull/42171), [#42224](https://github.com/PaddlePaddle/Paddle/pull/42224), [#42256](https://github.com/PaddlePaddle/Paddle/pull/42256), [#42306](https://github.com/PaddlePaddle/Paddle/pull/42306), [#42329](https://github.com/PaddlePaddle/Paddle/pull/42329)[, #42340](https://github.com/PaddlePaddle/Paddle/pull/42340), [#42368](https://github.com/PaddlePaddle/Paddle/pull/42368), [#42425](https://github.com/PaddlePaddle/Paddle/pull/42425)) + +- 简化 `paddle.distribution.Categorical`的 probs 计算逻辑,提升性能 4 到 5 倍。 ([#42178](https://github.com/PaddlePaddle/Paddle/pull/42178)) + ### (4)问题修复 #### API @@ -1548,7 +1705,7 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - 修复高阶微分 `gradients` 接口在指定 target_grad 时未按预期生效的问题。([#40940](https://github.com/PaddlePaddle/Paddle/pull/40940/)) -- 修复动态图 op`_BatchNormBase` 基类中修改了 default_dtype,导致后续组网参数类型错误的问题,受影响的API有 `paddle.nn.BatchNorm1D`,`paddle.nn.BatchNorm2D`,`paddle.nn.BatchNorm3D`,`paddle.nn.SyncBatchNorm`。具体原因是当 `get_default_dtype() == 'float16'` 时,通过 `set_default_dtype('float32')`修改默认参数数据类型,动态图组网的参数类型是通过 default_dtype 来创建的,因此当默认参数类型被修改后导致后续的组网参数类型错误。 ([#36376](https://github.com/PaddlePaddle/Paddle/pull/36376)) +- 修复动态图 op`_BatchNormBase` 基类中修改了 default_dtype,导致后续组网参数类型错误的问题,受影响的API有 `paddle.nn.BatchNorm1D`,`paddle.nn.BatchNorm2D`,`paddle.nn.BatchNorm3D`,`paddle.nn.SyncBatchNorm`。具体原因是当 `get_default_dtype() == 'float16'` 时,通过 `set_default_dtype('float32')`修改默认参数数据类型,动态图组网的参数类型是通过 default_dtype 来创建的,因此当默认参数类型被修改后导致后续的组网参数类型错误。 ([#36376](https://github.com/PaddlePaddle/Paddle/pull/36376)) - 修复 batchnorm op 中,当数据类型为 FP32 ,且数据维度 `dims = 2,data_layout = NHWC` 时,反向 op 内中间变量未定义问题。 ([#37020](https://github.com/PaddlePaddle/Paddle/pull/37020)) @@ -1612,7 +1769,29 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - 修复 `paddle.ifftshit` , `paddle.fftshift` 计算错误问题。([#36834](https://github.com/PaddlePaddle/Paddle/pull/36834), [#36748](https://github.com/PaddlePaddle/Paddle/pull/36748)) -- 修复 `paddle.fft` 系列 API 中的 `axis` 计算错误。 ([#36321](https://github.com/PaddlePaddle/Paddle/pull/36321)) +- 修复 `paddle.fft` 系列 API 中的 `axis` 计算错误。 ([#36321](https://github.com/PaddlePaddle/Paddle/pull/36321)) + +- 修复 batch_norm_grad op 在 FP16 数据类型时输出数据类型注册的 bug,该 bug 会导致部分场景下编译失败,并且对 FP16 计算精度会有一定影响。([#42461](https://github.com/PaddlePaddle/Paddle/pull/42461)) + +- 修复 `paddle.nn.functional.pad` API 在模型动转静时,padding 为 Tensor 条件下的 Infershape 信息错误问题。([#42414](https://github.com/PaddlePaddle/Paddle/pull/42414)) + +- 修复 `paddle.distribution.StickBreakingTransform` 输入维度超过2时异常的问题。([#41762](https://github.com/PaddlePaddle/Paddle/pull/41672)) + +- 修复 fused_attention op 中 QK^T 计算出 nan/inf 的问题。([#42032](https://github.com/PaddlePaddle/Paddle/pull/42032)) + +- 修复 fused_attention op 中 FusedResidualDropoutBias 在V100上计算出 nan/inf 问题。([#42398](https://github.com/PaddlePaddle/Paddle/pull/42398)) + +- 修复 full_like op 在执行时引入的多余的 data transform 问题。([#41973](https://github.com/PaddlePaddle/Paddle/pull/41973)) + +- 修复 p_norm op 在 GPU 环境上计算 nan 的问题。([#41804](https://github.com/PaddlePaddle/Paddle/pull/41804)) + +- 修复 split op 在参数 sections 存在为0的 size 情况下,段错误的问题。([#41755](https://github.com/PaddlePaddle/Paddle/pull/41755)) + +- 修复6个 elementwise op(pow、complex、divide_double、multiply_double、fmax、fmin)在需要 broadcast 的情况下,多卡训练时报Place(gpu:0) 不支持的问题。([#42332](https://github.com/PaddlePaddle/Paddle/pull/42332)) + +- 修复 import paddle 时由于 PIL 版本升级导致的废弃接口报 warning 的问题。([#42307](https://github.com/PaddlePaddle/Paddle/pull/42307)) + +- 修复静态图下 `paddle.linalg.matrix_rank`不支持 tol 为 FP64 Tensor 的问题。([#42085](https://github.com/PaddlePaddle/Paddle/pull/42085)) #### IR(Intermediate Representation) @@ -1638,7 +1817,11 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - 修复控制流 For 中返回单值时代码转换的问题。([#40683](https://github.com/PaddlePaddle/Paddle/pull/40683)) - - 修复控制流 cond 的输入包含 LoDTensorArray 时,生成反向 op 会报错的问题。([#39585](https://github.com/PaddlePaddle/Paddle/pull/39585)) + - 修复控制流 cond 的输入包含 LoDTensorArray 时,生成反向 op 会报错的问题。([#39585](https://github.com/PaddlePaddle/Paddle/pull/39585)) + + - 修复 `padddle.jit.save`在导出动转静模型时丢失顶层 Layer 的 forward_pre_hook 和 forward_post_hook 的问题。([#42273](https://github.com/PaddlePaddle/Paddle/pull/42273)) + + - 修复 `paddle.expand`中 shape 参数包含 Tensor 在动转静时会转换报错的问题。([#41973](https://github.com/PaddlePaddle/Paddle/pull/41973)) #### 分布式训练 @@ -1794,17 +1977,14 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - 修复 Expand_As op 计算输出 shape 时逻辑的错误。([#38677](https://github.com/PaddlePaddle/Paddle/pull/38677)) -- 框架功能修复 - - 修复 `core.VarDesc.VarType.STRINGS` 类型的变量获取 `lod_level` 属性报错的问题,并且设置其 `lod_level` 为None。([#39077](https://github.com/PaddlePaddle/Paddle/pull/39077)) +- 修复 `core.VarDesc.VarType.STRINGS` 类型的变量获取 `lod_level` 属性报错的问题,并且设置其 `lod_level` 为None。([#39077](https://github.com/PaddlePaddle/Paddle/pull/39077)) - - 修复框架功能 `Pylayer` 不支持不同 dtype 的问题。 ([#37974](https://github.com/PaddlePaddle/Paddle/pull/37974)) +- 修复框架功能 `PyLayer` 不支持不同 dtype 的问题。 ([#37974](https://github.com/PaddlePaddle/Paddle/pull/37974)) -- API修复 - - - 修复了学习率衰减 API `paddle.optimizer.lr.PolynomialDecay` 的零除问题。 ([#38782](https://github.com/PaddlePaddle/Paddle/pull/38782)) - - - 修复调用 DisableGlogInfo() 接口后依旧残留部分日志的问题。 ([#36356](https://github.com/PaddlePaddle/Paddle/pull/36356)) +- 修复了学习率衰减 API `paddle.optimizer.lr.PolynomialDecay` 的零除问题。 ([#38782](https://github.com/PaddlePaddle/Paddle/pull/38782)) + +- 修复调用 DisableGlogInfo() 接口后依旧残留部分日志的问题。 ([#36356](https://github.com/PaddlePaddle/Paddle/pull/36356)) - 修复 SimpleRNN、GRU和LSTM API CPU训练时多层RNN(dropout 设置为0时)反向计算出错的问题。 ([#37080](https://github.com/PaddlePaddle/Paddle/pull/37080)) @@ -1812,7 +1992,11 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - 使 `paddle.roll` 的 shifts 参数支持传入 Tensor。 ([#36727](https://github.com/PaddlePaddle/Paddle/pull/36727)) -- 为 fft 添加 onemkl 作为可选的计算后端。 ([#36414](https://github.com/PaddlePaddle/Paddle/pull/36414)) +- 为 fft 添加 onemkl 作为可选的计算后端。 ([#36414](https://github.com/PaddlePaddle/Paddle/pull/36414)) + +- 修复 mamtul_v2 和 elementwise_div 两个 op 在 bfloat16 类型下的精度问题。([#42479](https://github.com/PaddlePaddle/Paddle/pull/42479)) + +- 修复显存回收时 LoDTensorArray 只清理内部 Tensor 而未清空 Array 导致的下个 step 可能出错的问题。([#42398](https://github.com/PaddlePaddle/Paddle/pull/42398)) ## 4. 部署方向(Paddle Inference) @@ -1902,6 +2086,10 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - 将 matmul 融合相关的 pass 基于不同的后端(GPU、CPU、TensorRT)拆开,支持 FC 权重的转置功能。([#39369](https://github.com/PaddlePaddle/Paddle/pull/39369)) +- 新增 roll、strided_slice、slice op 在动态 shape 的情况下对 TensorRT 的支持。([#41913](https://github.com/PaddlePaddle/Paddle/pull/41913), [#41573](https://github.com/PaddlePaddle/Paddle/pull/41573), [#41467](https://github.com/PaddlePaddle/Paddle/pull/41467)) + +- 新增 div op 对 TensorRT 的支持。([#41243](https://github.com/PaddlePaddle/Paddle/pull/41243)) + - 量化支持 - `PostTrainingQuantization` API新增支持`paddle.io.DataLoader` 对象或者 `Python Generator`的输入。([#38686](https://github.com/PaddlePaddle/Paddle/pull/38686)) @@ -1948,6 +2136,8 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - TensorRT 动态 shape 参数自动生成接口增加文件存在性检查。([#36628](https://github.com/PaddlePaddle/Paddle/pull/36628)) +- 修复 MKLDNN 不支持 conv3d 的问题。([#42055](https://github.com/PaddlePaddle/Paddle/pull/42055)) + #### 后端能力修复 - 修复预测时 cuDNN 默认算法选择配置,使用非 deterministic 策略。 ([#41491](https://github.com/PaddlePaddle/Paddle/pull/41491)) @@ -2063,7 +2253,7 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. ### 编译安装 -- 从2.3.0-rc0版本开始,飞桨对框架支持的 GPU 架构种类进行了调整和升级。(更多请参考: [飞桨支持的 GPU 架构](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.3rc/install/Tables.html#gpu)) +- 从2.3.0 版本开始,飞桨对框架支持的 GPU 架构种类进行了调整和升级。(更多请参考: [飞桨支持的 GPU 架构](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.3rc/install/Tables.html#gpu)) 备注: @@ -2087,12 +2277,15 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - CUDA11 : 3.5, 5.0, 6.0, 6.1, 7.0, 7.5, 8.0。 +- 支持 Python 3.10,修复 Windows 下某些 PythonC API 变化导致的编译 bug。([#41180](https://github.com/PaddlePaddle/Paddle/pull/42180)) + - Windows 平台支持 Visual Studio 2019 编译。 ([#38719](https://github.com/PaddlePaddle/Paddle/pull/38719)) - 消除 Windows 平台编译时出现的各种 warning。 ([#38034](https://github.com/PaddlePaddle/Paddle/pull/38034), [#37890](https://github.com/PaddlePaddle/Paddle/pull/37890), [#37442](https://github.com/PaddlePaddle/Paddle/pull/37442), [#37439](https://github.com/PaddlePaddle/Paddle/pull/37439), [#36857](https://github.com/PaddlePaddle/Paddle/pull/36857)) - 修复底层数据结构升级引入的 jetson 编译问题。 ([#39669](https://github.com/PaddlePaddle/Paddle/pull/39669), [#39441](https://github.com/PaddlePaddle/Paddle/pull/39441)) + ### 新硬件适配 - 自定义新硬件接入:提供一种插件式扩展 PaddlePaddle 硬件后端的方式。通过该功能,开发者无需为特定硬件修改 PaddlePaddle 代码,只需实现标准接口,并编译成动态链接库,则可作为插件供 PaddlePaddle 调用。降低为 PaddlePaddle 添加新硬件后端的开发难度。当前支持自定义 Runtime 接入和自定义 Kernel 接入。 @@ -2107,6 +2300,6 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. ## Thanks to our Contributors -This release contains contributions from the project core team as well as : +This release contains contributions from the project core team as well as : Adam Osewski, Allen Guo, arlesniak, chenenquan, chenyanlann, fengkuangxiaxia, fuqianya, fwenguang, guguguzi, helen88, houj04, Jacek Czaja, jakpiase, jianghaicheng, joanna.wozna.intel, joeqiao12, Leo Chen, Leo Guo, Li-fAngyU, lidanqing, Liyulingyue, Matsumoto GAO, maxhuiy, Ming-Xu Huang, Nyakku Shigure, piotrekobi, piotrekobiIntel, QingshuChen, qipengh, Skr Bang, Sylwester Fraczek, Sławomir Siwek, taixiurong, tanzhipeng, Tomasz Socha, TTerror, Webbley, yaozhixin, ykkk2333, yujun, Zhangjingyu06, zhangxiaoci, zhangyikun02, zhangyk0314, zlsh80826, zn, Zuza diff --git a/docs/release_note_en.md b/docs/release_note_en.md index e9e51ea79a8..4969c710d50 100644 --- a/docs/release_note_en.md +++ b/docs/release_note_en.md @@ -1,9 +1,9 @@ -# 2.3.0-rc0 Release Note +# 2.3.0 Release Note ## 1. **Important Updates** -We are excited to release the PaddlePaddle Framework V2.3.0-rc0. This version contains the following highlights. +We are excited to release the PaddlePaddle Framework V2.3.0. This version contains the following highlights. ### API @@ -15,7 +15,7 @@ We are excited to release the PaddlePaddle Framework V2.3.0-rc0. This version co - Added 9 new framework performance analysis APIs. The new performance profiling APIs, centered around Paddle.Profiler.Profiler, help users collect and analyze performance statistics during training and inference. -- Added 7 APIs for device management, facilitating hardware information acquistion. +- Added 7 APIs for device management, facilitating hardware information acquistion. - Added several visual and text domain APIs to facilitate ~~the~~ reusability of MobileNetV3, ResNeXt and other backbone networks, to achieve the fast networking. @@ -35,7 +35,7 @@ We are excited to release the PaddlePaddle Framework V2.3.0-rc0. This version co ### **Compile and Install** -- From version 2.3.0-rc0, PaddlePaddle upgrades GPU architectures supported. +- From version 2.3.0, PaddlePaddle upgrades GPU architectures supported. ### **Inference Deployment** @@ -49,7 +49,7 @@ We are excited to release the PaddlePaddle Framework V2.3.0-rc0. This version co - Add custom device support: provide a plug-in way to extend PaddlePaddle hardware backend. -- Add training/inference support for multiple heterogeneous chips such as HUAWEI Ascend 910 / GraphCore IPU / Cambricon MLU / Kunlunxin 2. +- Add training/inference support for multiple heterogeneous chips such as HUAWEI Ascend 910 / GraphCore IPU / Cambricon MLU / KUNLUNXIN 2. ### **Framework Architecture** @@ -58,6 +58,8 @@ We are excited to release the PaddlePaddle Framework V2.3.0-rc0. This version co ## **2. Incompatibility Upgrade** +- Due to limitation of the binary size, sm35 CUDA ARCH is dropped in pre-compiled binaries. ([#41754](https://github.com/PaddlePaddle/Paddle/pull/41754)) + - When `paddle.to_tensor` converts a python int scalar to a Tensor, the default data type on Windows changes from int32 to int64, thus alignment with Linux/Mac. ([#39662](https://github.com/PaddlePaddle/Paddle/pull/39662)) - To keep consistency with division behavior under python3, the division symbol `/` has been changed from “rounding divide” to “true divide”, and the data type of the computed output has been switched from int to float. ([#40890](https://github.com/PaddlePaddle/Paddle/pull/40890)) @@ -69,7 +71,7 @@ We are excited to release the PaddlePaddle Framework V2.3.0-rc0. This version co 2.2 -2.3.0-rc0 +2.3.0 @@ -111,7 +113,7 @@ Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True, 2.2 -2.3.0-rc0 +2.3.0 @@ -419,6 +421,12 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. - `paddle.incubate.optimizer.functional.minimize_lbfgs`,add second-order optimizer L-BFGS. - Add `paddle.incubate.multiprocessing` module, to provide Tensor (CPU/GPU) data transfer between python processes. ([#37302](https://github.com/PaddlePaddle/Paddle/pull/37302), [#41339](https://github.com/PaddlePaddle/Paddle/pull/41339)) + +- Add `paddle.incubate.autotune.set_config` API, to support multi-version Kernel auto-selection, mixed precision data layout auto-conversion, and num_workers auto-selection for DataLoader to automatically improve model performance. ([#42301](https://github.com/PaddlePaddle/Paddle/pull/42301)) + +- Add `paddle.incubate.nn.FusedMultiTransformer` and `paddle.incubate.nn.functional.fused_multi_transformer` API, to fuse multiple layers of transformers into a single op to improve model inference performance. It should be noted that only forward is supported. ([#42311](https://github.com/PaddlePaddle/Paddle/pull/42311)) + +- Add einsum_v2 operators for consistent interface between imperative and static mode. It is compatible with the `paddle.einsum` implementation at the original python side, while supporting dynamic to static export and more complete Infershape inference. ([#42495](https://github.com/PaddlePaddle/Paddle/pull/42495), [#42327](https://github.com/PaddlePaddle/Paddle/pull/42327), [#42397](https://github.com/PaddlePaddle/Paddle/pull/42397), [#42105](https://github.com/PaddlePaddle/Paddle/pull/42105)) #### IR(Intermediate Representation) @@ -496,7 +504,7 @@ AssertionError: elu_ only support alpha >= 0, please use elu instead. #### **Paddle HIgh reusability operator library** -We anounce PHI as the new Paddle HIgh reusability operator library. PHI provides Primitive API, enabling kernel reuse for operator development. As a refactored functional operator library, PHI aims to solve legacy problems that harm the framework's performance and reusability, in particular on the operator development. Such problems include inefficient ways of cross using operators, unclear operator interfaces and lacking direct calls to the operator library in C++. With PHI, new operators can be easily implemented by composing functions available in the functional library. The library provides over 200 C++ operator class APIs and nearly 500 kernels. Composing new operators through these built-in functions can greatly reduce the user's development effort. PHI supports different types of hardware (e.g., GPU and XPU). In addition, PHI is extensible with plugins for accommodating third party accelerators (such as NPU) in a low cost and reusable fashion. In short, PHI supports low level operator composabilty, the reuse of kernels through Primitives, and accelerators through plugins.The main contents include six parts as below: +We announce PHI as the new Paddle HIgh reusability operator library. PHI provides Primitive API, enabling kernel reuse for operator development. As a refactored functional operator library, PHI aims to solve legacy problems that harm the framework's performance and reusability, in particular on the operator development. Such problems include inefficient ways of cross using operators, unclear operator interfaces and lacking direct calls to the operator library in C++. With PHI, new operators can be easily implemented by composing functions available in the functional library. The library provides over 200 C++ operator class APIs and nearly 500 kernels. Composing new operators through these built-in functions can greatly reduce the user's development effort. PHI supports different types of hardware (e.g., GPU and XPU). In addition, PHI is extensible with plugins for accommodating third party accelerators (such as NPU) in a low cost and reusable fashion. In short, PHI supports low level operator composabilty, the reuse of kernels through Primitives, and accelerators through plugins.The main contents include six parts as below: - **The implementation of the operator library infrastructure, core components and mechanisms** : The directory structure of the new operator library is reasonably planned, design and implement the common base data structure of the new operator library, the new functional InferMeta and Kernel development paradigm and the corresponding registration and management components. Support the automated compilation object generation and compilation dependency generation of Kernel files, allowing developers to focus only on the functional Kernel implementation, and making the development paradigm clear and concise. ([#34425](https://github.com/PaddlePaddle/Paddle/pull/34425), [#37107](https://github.com/PaddlePaddle/Paddle/pull/37107), [#36946](https://github.com/PaddlePaddle/Paddle/pull/36946), [#36948](https://github.com/PaddlePaddle/Paddle/pull/36948), [#37876](https://github.com/PaddlePaddle/Paddle/pull/37876), [#37916](https://github.com/PaddlePaddle/Paddle/pull/37916), [#37977](https://github.com/PaddlePaddle/Paddle/pull/37977), [38078](https://github.com/PaddlePaddle/Paddle/pull/38078), [#38861](https://github.com/PaddlePaddle/Paddle/pull/38861), [#39123](https://github.com/PaddlePaddle/Paddle/pull/39123), [#39131](https://github.com/PaddlePaddle/Paddle/pull/39131), [#39748](https://github.com/PaddlePaddle/Paddle/pull/39748), [#39790](https://github.com/PaddlePaddle/Paddle/pull/39790), [#39941](https://github.com/PaddlePaddle/Paddle/pull/39941), [#40239](https://github.com/PaddlePaddle/Paddle/pull/40239), [#40635](https://github.com/PaddlePaddle/Paddle/pull/40635), [#41091](https://github.com/PaddlePaddle/Paddle/pull/41091), [#37409](https://github.com/PaddlePaddle/Paddle/pull/37409), [#37942](https://github.com/PaddlePaddle/Paddle/pull/37942), [#39002](https://github.com/PaddlePaddle/Paddle/pull/39002), [#38109](https://github.com/PaddlePaddle/Paddle/pull/38109), [#37881](https://github.com/PaddlePaddle/Paddle/pull/37881), [#37517](https://github.com/PaddlePaddle/Paddle/pull/37517), [#39870](https://github.com/PaddlePaddle/Paddle/pull/39870), [#40975](https://github.com/PaddlePaddle/Paddle/pull/40975), [#39475](https://github.com/PaddlePaddle/Paddle/pull/39475), [#37304](https://github.com/PaddlePaddle/Paddle/pull/37304), #36910, #37120, #37146, #37215, #37255, #37369, #38258, #38257, #38355, #38853, #38937, #38977, #38946, #39085, #39153, #39228, #38301, #38275, #38506, #38607, #38473, #38632, #38811, #38880, #38996, #38914, #39101) @@ -1012,6 +1020,7 @@ We anounce PHI as the new Paddle HIgh reusability operator library. PHI provides - hard_sigmoid ([#40626](https://github.com/PaddlePaddle/Paddle/pull/40626)) + - exp, det, assign, gaussian_random, matrix_rank, eye, and deformable_conv. ([#41755](https://github.com/PaddlePaddle/Paddle/pull/41755), [#41737](https://github.com/PaddlePaddle/Paddle/pull/41737)) #### **New Dynamic Graph Execution Mechanism** @@ -1041,25 +1050,35 @@ To improve scheduling performance and custom development capability of the dynam - **Support inplace after dynamic graph reconstruction**: input and output are the same Tensor. - - - Adapt the inplace strategy for dynamic graph reconstruction intermediate states.([#40400](https://github.com/PaddlePaddle/Paddle/pull/40400)) - - - Adapt the inplace strategy to the final state of the dynamic graph reconstruction. ([#40695](https://github.com/PaddlePaddle/Paddle/pull/40695)) - - - Add inplace strategy to PyLayer function after dynamical graph reconstruction. ([#41043](https://github.com/PaddlePaddle/Paddle/pull/41043)) - - - Add inplace strategy for Tensor's setitem function after dynamical graph reconstruction. ([#40915](https://github.com/PaddlePaddle/Paddle/pull/40915)) - - - Add `_reset_grad_inplace_version` interface after dynamic graph reconstruction, to set the inplace version of the Tensor's gradient to 0. ([#41101](https://github.com/PaddlePaddle/Paddle/pull/41101)) - - - If the value of the forward Tensor is not needed during the inverse computation (no need buffer property), the inplace version detection operation is not needed for that Tensor. For Tensor with no_need_buffer, skip the inplace version check. ([#41350](https://github.com/PaddlePaddle/Paddle/pull/41350)) - - - Unify error messages for inplace version checks after and before reconstruction of dynamic graphs. ([#41209](https://github.com/PaddlePaddle/Paddle/pull/41209)) + - Adapt the inplace strategy for dynamic graph reconstruction intermediate states.([#40400](https://github.com/PaddlePaddle/Paddle/pull/40400)) + + - Adapt the inplace strategy to the final state of the dynamic graph reconstruction. ([#40695](https://github.com/PaddlePaddle/Paddle/pull/40695)) + + - Add inplace strategy to PyLayer function after dynamical graph reconstruction. ([#41043](https://github.com/PaddlePaddle/Paddle/pull/41043)) + + - Add inplace strategy for Tensor's setitem function after dynamical graph reconstruction. ([#40915](https://github.com/PaddlePaddle/Paddle/pull/40915)) + + - Add `_reset_grad_inplace_version` interface after dynamic graph reconstruction, to set the inplace version of the Tensor's gradient to 0. ([#41101](https://github.com/PaddlePaddle/Paddle/pull/41101)) + + - If the value of the forward Tensor is not needed during the inverse computation (no need buffer property), the inplace version detection operation is not needed for that Tensor. For Tensor with no_need_buffer, skip the inplace version check. ([#41350](https://github.com/PaddlePaddle/Paddle/pull/41350)) + + - Unify error messages for inplace version checks after and before reconstruction of dynamic graphs. ([#41209](https://github.com/PaddlePaddle/Paddle/pull/41209)) - **Support view strategy after dynamical graph reconstruction**: input and output Tensor share underlying data. - - - Adapt the view strategy for dynamic graph reconstruction intermediate states. Include `reshape` , `squeeze` , `unsqueeze` , and `flatten` APIs. ([#40830](https://github.com/PaddlePaddle/Paddle/pull/40830)) + - Adapt the view strategy for dynamic graph reconstruction intermediate states. Include `reshape` , `squeeze` , `unsqueeze` , and `flatten` APIs. ([#40830](https://github.com/PaddlePaddle/Paddle/pull/40830)) - - Adapt the view strategy for dynamic graph reconstruction final state. Include `reshape` API. ([#40891](https://github.com/PaddlePaddle/Paddle/pull/40891)) + - Adapt the view strategy for dynamic graph reconstruction final state. Include `reshape` API. ([#40891](https://github.com/PaddlePaddle/Paddle/pull/40891)) + +- **Add support for weakref on the python side of the new dynamic graph eager Tensor.** ([#41797](https://github.com/PaddlePaddle/Paddle/pull/41797)) + +- **Enhance the new dynamic graph DoubleGrad function** to support the basic DoubleGrad feature.([#41893](https://github.com/PaddlePaddle/Paddle/pull/41893), [#41894](https://github.com/PaddlePaddle/Paddle/pull/41894), [#41895](https://github.com/PaddlePaddle/Paddle/pull/41895)) + +- **Add `core.eager.StringTensor` interface**, to support the construction of StringTensor on python side and the use of the StringTensor related APIs. ([#41039](https://github.com/PaddlePaddle/Paddle/pull/41039)) + +- **Add `_grad_name` and `_grad_value`*to `core.eager.Tensor` to return the name and value of a gradient. ([#41990](https://github.com/PaddlePaddle/Paddle/pull/41990)) + +- **Add the processing of the no_need_buffer attribute for dynamic graph intermediate state.** The Tensor with the no_need_buffer attribute is skipped in the inplace backward check operation. ([#41720](https://github.com/PaddlePaddle/Paddle/pull/41720)) #### **New Static Graph Executor** @@ -1073,6 +1092,11 @@ In order to solve the problem that the original static graph executor of the Pad - Interface compatibility: Compatible with the user interface and functionality of the original executor, such as alignment with python interface Executor.run(), support for managing Tensor in Scope, etc. This ensures that users can switch to the new executor without perception. ([#37278](https://github.com/PaddlePaddle/Paddle/pull/37278), [#37379](https://github.com/PaddlePaddle/Paddle/pull/37379), [#37445](https://github.com/PaddlePaddle/Paddle/pull/37445), [#37510](https://github.com/PaddlePaddle/Paddle/pull/37510), [#40955](https://github.com/PaddlePaddle/Paddle/pull/40955), [#41778](https://github.com/PaddlePaddle/Paddle/pull/41178), [#41058](https://github.com/PaddlePaddle/Paddle/pull/41058), [#38584](https://github.com/PaddlePaddle/Paddle/pull/38584), [#37957](https://github.com/PaddlePaddle/Paddle/pull/37957), [#37672](https://github.com/PaddlePaddle/Paddle/pull/37672), [#37474](https://github.com/PaddlePaddle/Paddle/pull/37474), [#37085](https://github.com/PaddlePaddle/Paddle/pull/37085), [#37061](https://github.com/PaddlePaddle/Paddle/pull/37061), [#36945](https://github.com/PaddlePaddle/Paddle/pull/36945)) - Enhance debugging and error reporting in multi-threaded scenarios by capturing error reports from sub-threads and throwing them uniformly in the main thread. This can improve user experience. ([#36692](https://github.com/PaddlePaddle/Paddle/pull/36692),[#36802](https://github.com/PaddlePaddle/Paddle/pull/36802)) + +- Fix the bug with the new executor communication flow resetting stream cache information in the allocator, to reduce RecordStream overhead in cross-stream scenarios. This improves performance of DeepFM models by about 8% after optimization. ([#42046](https://github.com/PaddlePaddle/Paddle/pull/42046)) + +- Optimize the dependency analysis method between new executor operators to improve runtime performance. Establish correct dependencies for send/recv communication operators to support pipeline parallel. ([#42009](https://github.com/PaddlePaddle/Paddle/pull/42009)) + #### **Distributed Training** @@ -1157,6 +1181,7 @@ In order to solve the problem that the original static graph executor of the Pad - Add evaluation metrics module under the Unified Parameter Server, to support AUC/WuAUC/MaskAUC and other evaluation metrics calculation and customizable extensions. ([#38789](https://github.com/PaddlePaddle/Paddle/pull/38789)) + - Supports XPU parameter server training on KUNLUNXIN 2. ([#41917](https://github.com/PaddlePaddle/Paddle/pull/41917), [#42266](https://github.com/PaddlePaddle/Paddle/pull/42266), [#41916](https://github.com/PaddlePaddle/Paddle/pull/41916)) #### Profiler @@ -1192,6 +1217,89 @@ In order to solve the problem that the original static graph executor of the Pad - Profiler support for grading.([#39926](https://github.com/PaddlePaddle/Paddle/pull/39926)) +- Modify the name and type of logging for op under new dynamic graph.([#41771](https://github.com/PaddlePaddle/Paddle/pull/41771/) + +- Add Kernel running statistics into profilers' summarization and optimize the summarization.([#41989](https://github.com/PaddlePaddle/Paddle/pull/41989) + +- Remove side-effect to performance in forward computing forward when Profiler is off. ([#42142](https://github.com/PaddlePaddle/Paddle/pull/42142)) + +#### **CINN compiler adoption** + +With the recent development of PaddlePaddle's compiler, a.k.a, CINN([GitHub - PaddlePaddle/CINN: Compiler Infrastructure for Neural Networks](https://github.com/PaddlePaddle/CINN)), paddle framework has also been changed to adapt the compiler CINN features. These include the subgraph management related functions for the Paddle-CINN runtime, optimization of memory and speed performance, and bug fixing during development. + +- Functions developed: + + - Subgraph op related functions: + + - Add the function to find and generate CINN subgraphs from computational graphs.([#36345](https://github.com/PaddlePaddle/Paddle/pull/36345)) + + - Add cinn_launch op as a runtime entry point to CINN. It is responsible for scheduling CINN to compile the subgraph, to initialize the data, and to execute the generated kernels.([#36600](https://github.com/PaddlePaddle/Paddle/pull/36600)) + + - Add a helper class `CinnLaunchContext` to the kernel implementation of cinn_launch op to manage the intermediate data for compiling and running subgraphs, to improve scalability and code readability.([#37938](https://github.com/PaddlePaddle/Paddle/pull/37938)) + + - Add additional fetch nodes to CINN subgraphs, thus ensuring that CINN external nodes can fetch the values of variables.([#37172](https://github.com/PaddlePaddle/Paddle/pull/37172), [#37190](https://github.com/PaddlePaddle/Paddle/pull/37190)) + + - Add the function to symbolize a CINN subgraph, which is used to topologically sort the subgraphs and return the CINN execution sequence.([#36417](https://github.com/PaddlePaddle/Paddle/pull/36417) + + - Add `CinnCompiler` class for involking subgraphs in the CINN compiled graph that can be replaced by using CINN operators. ([#36562](https://github.com/PaddlePaddle/Paddle/pull/36562), [#36975](https://github.com/PaddlePaddle/Paddle/pull/36975)) + + - Add the interface to CINN symbolization class to get the names of subgraph fetched variables to prevent fetched variables from being eliminated in compilation optimizations.([#37218](https://github.com/PaddlePaddle/Paddle/pull/37218)) + + - Checking, debugging, and PI changes related: + + - Synchronize the update of NetBuilder API name changes in CINN.([#40392](https://github.com/PaddlePaddle/Paddle/pull/40392)) + + - Add necessary log information to Paddle-CINN for better debugging.([#36867](https://github.com/PaddlePaddle/Paddle/pull/36867)) + + - Add the bidirectional conversion function between Paddle desc and CINN desc.([#36100](https://github.com/PaddlePaddle/Paddle/pull/36100)) + + - The operator implemented in CINN may not use some input variables compared to Paddle. Therefore, remove the check that the input variables must be used in the cinn_launch op.([#37119](https://github.com/PaddlePaddle/Paddle/pull/37119)) + + - Added cinn_instruction_run op for invoking CINN to execute a single generation instruction, facilitating the construction of scheduling run subgraphs on the Paddle side.([#39435](https://github.com/PaddlePaddle/Paddle/pull/39435), [#39576](https://github.com/PaddlePaddle/Paddle/pull/39576)) + + - Add control macros to Paddle for CUDA/CUBLAS/MKL/CINN pass application required to compile CINN.([#37066](https://github.com/PaddlePaddle/Paddle/pull/37066), [#36660](https://github.com/PaddlePaddle/Paddle/pull/36660)) + + - Add two control flags FLAGS_allow_cinn_ops and FLAGS_deny_cinn_ops to control the categories of CINN operators used to replace native operators during Paddle training.([#36842](https://github.com/PaddlePaddle/Paddle/pull/36842)) + +- Performance optimization: + + - Speed optimization + + - Optimize the computational time consumed by CinnCacheKey.([#37786](https://github.com/PaddlePaddle/Paddle/pull/37786), [#37317](https://github.com/PaddlePaddle/Paddle/pull/37317)) + + - Cache variable scope for CINN compiled subgraphs to reduce runtime parameter construction overhead.([#37983](https://github.com/PaddlePaddle/Paddle/pull/37983)) + + - Utilize CINN's auto-tuning in case of subgraph compilation, could be enabled by flag, for further tuning of training performance.([#41795](https://github.com/PaddlePaddle/Paddle/pull/41795)) + + - Refactor the correctness check of compilation results in case of subgraph compilation to avoid repeated checks at runtime and reduce the scheduling overhead.([#41777](https://github.com/PaddlePaddle/Paddle/pull/41777)) + + - Enable TransposeFolding and GemmRewriter optimization passes by default in Paddle-CINN training.([#41084](https://github.com/PaddlePaddle/Paddle/pull/41084)) + + - Pass the cuda stream created in Paddle into CINN so that Paddle and CINN can use the same CUDA stream in cuda computing.([#37337](https://github.com/PaddlePaddle/Paddle/pull/37337)) + + - Move CINN optimization pass application logic from Paddle to CINN.([#42047](https://github.com/PaddlePaddle/Paddle/pull/42047), [#42070](https://github.com/PaddlePaddle/Paddle/pull/42070)) + + - Device memory optimization + + - Add NoNeedBufferVars to cinn_launch op to declare a list of input variables that do not require a buffer, so that the memory can be freed in advance.([#38367](https://github.com/PaddlePaddle/Paddle/pull/38367)) + + - Pass in reference count information for external variables to the subgraph, so that subgraphs within cinn_launch can reuse memory optimization passes and reduce the memory overhead in using CINN.([#39209](https://github.com/PaddlePaddle/Paddle/pull/39209), [#39622](https://github.com/PaddlePaddle/Paddle/pull/39622)) + + - Add the function to convert a collection of executable instructions generated by CINN compilation to a Paddle Graph, supporting reuse of the Paddle scheduler and memory optimization pass, further reducing the memory overhead in using CINN. ([#39724](https://github.com/PaddlePaddle/Paddle/pull/39724), [#39911](https://github.com/PaddlePaddle/Paddle/pull/39911)) + + - Add Kernel of cinn_instruction_run op, to support dynamic device memory requests based on data types inferred from compilation results.([#40920](https://github.com/PaddlePaddle/Paddle/pull/40920)) + +- Bug fixing: + + - Fix and optimize the generation logic of CINN subgraphs.([#36503](https://github.com/PaddlePaddle/Paddle/pull/36503)) + + - Fix the bug that Paddle-CINN does not support no-input subgraphs.([#40814](https://github.com/PaddlePaddle/Paddle/pull/40814)) + + - Fix an error reported due to CINN not being able to handle useless outputs in operators such as batch_norm.([#36996](https://github.com/PaddlePaddle/Paddle/pull/36996)) + + - Fix several bugs in CINN subgraph partitioning and symbolization, and solve problems with Paddle training accessing the CINN. ([#36739](https://github.com/PaddlePaddle/Paddle/pull/36739), [#36698](https://github.com/PaddlePaddle/Paddle/pull/36698) ) + + - CINN does not yet support the control flow yet. Add logic to skip control flow when encountered.([#40812](https://github.com/PaddlePaddle/Paddle/pull/40812)) #### **Other** @@ -1283,12 +1391,17 @@ In order to solve the problem that the original static graph executor of the Pad - conv2d ([#38507](https://github.com/PaddlePaddle/Paddle/pull/38507),[#38938](https://github.com/PaddlePaddle/Paddle/pull/38938),[#36284](https://github.com/PaddlePaddle/Paddle/pull/36284)) - LayerNorm ([#40418](https://github.com/PaddlePaddle/Paddle/pull/40418)) - + +- Add the 3-stage storage graph retrieval engine based on SSD - host memory - GPU device memory, to support large-scale graph neural network training. ([#42472](https://github.com/PaddlePaddle/Paddle/pull/42472), [#42321](https://github.com/PaddlePaddle/Paddle/pull/42321), [#42027](https://github.com/PaddlePaddle/Paddle/pull/42027)) + +- Add heterogeneous multi-cloud training communication module switch, implement the Send/Recv interface function, and support multiple heterogeneous cloud communication.([#40965](https://github.com/PaddlePaddle/Paddle/pull/40965) [40911](https://github.com/PaddlePaddle/Paddle/pull/40911)) ### **(2) Function optimization** #### API +- Add backward implementation of `paddle.linalg.det `. ([#36013](https://github.com/PaddlePaddle/Paddle/pull/36013)) + - Add support for mixed precision training O2 mode for `paddle.Model`, i.e., support for Pure FP16 training mode of the original dynamic/static graphs. ([#36441](https://github.com/PaddlePaddle/Paddle/pull/40962441)) - Support for self chain calls for `paddle.nn.Layer`. ([#36609](https://github.com/PaddlePaddle/Paddle/pull/36609)) @@ -1359,6 +1472,11 @@ In order to solve the problem that the original static graph executor of the Pad - Add check for unstack and unique op in case of input Tensor with 0 elements. ([#36021](https://github.com/PaddlePaddle/Paddle/pull/36021)) +- Add new multi-layer, bi-directional LSTM function that supports KUNLUNXIN 2, to improve RNN forward/backward ops, and support the use of temporal model training. ([#](https://github.com/PaddlePaddle/Paddle/pull/41781)[42076](https://github.com/PaddlePaddle/Paddle/pull/42076)) + +- Add bce_loss forward/backward ops for KUNLUNXIN 2. ([#41610](https://github.com/PaddlePaddle/Paddle/pull/41610)) + +- Add backward implementation of `paddle.linalg.det `. ([#36013](https://github.com/PaddlePaddle/Paddle/pull/36013)) #### IR(Intermediate Representation) @@ -1423,7 +1541,22 @@ In order to solve the problem that the original static graph executor of the Pad - Optimize the merge logic of embedding op to improve performance by exploiting the topological relationship of embedding op in the model. [(#35942)](https://github.com/PaddlePaddle/Paddle/pull/35942) - Communication library: restructure the communication library to improve the scalability and development of the communication library, and support heterogeneous communication. ([#41398](https://github.com/PaddlePaddle/Paddle/pull/41398), [#39720](https://github.com/PaddlePaddle/Paddle/pull/39720), [#40911](https://github.com/PaddlePaddle/Paddle/pull/40911), [#40579](https://github.com/PaddlePaddle/Paddle/pull/40579), [#40629](https://github.com/PaddlePaddle/Paddle/pull/40629), [#40437](https://github.com/PaddlePaddle/Paddle/pull/40437), [#40430](https://github.com/PaddlePaddle/Paddle/pull/40430), [#40228](https://github.com/PaddlePaddle/Paddle/pull/40228), [#40181](https://github.com/PaddlePaddle/Paddle/pull/40181), [#40100](https://github.com/PaddlePaddle/Paddle/pull/40100), [#40097](https://github.com/PaddlePaddle/Paddle/pull/40097), [#39892](https://github.com/PaddlePaddle/Paddle/pull/39892), [#39384](https://github.com/PaddlePaddle/Paddle/pull/39384), [#39737](https://github.com/PaddlePaddle/Paddle/pull/39737), [#40040](https://github.com/PaddlePaddle/Paddle/pull/40040)) - + +- Support the publication of MoE-related interfaces in `paddle.incubate.distributed.models.moe ` (`moe.GShardGate `, `moe.BaseGate `, `moe.SwitchGate `, `moe.MoELayer `, and `moe. ClipGradForMOEByGlobalNorm `). ([#42300](https://github.com/PaddlePaddle/Paddle/pull/42300)) + +- Fix the error report in the use of recomputing in `paddle.incubate.distributed.models.moe.MoELayer `. ([#42128](https://github.com/PaddlePaddle/Paddle/pull/42128)) + +- Fix the error report in the new dynamic graph pipeline parallel caused by different data types ([#41937](https://github.com/PaddlePaddle/Paddle/pull/41937) [#42053](https://github.com/PaddlePaddle/Paddle/pull/42053)) + +- Fix the error report in the new dynamic graph tensor model parallel due to different data types([#41960](https://github.com/PaddlePaddle/Paddle/pull/41960)) + +#### **Custom operator** + +- Enhance the C++ custom operator mechanism for writing second-order gradient operators, to support adding suffixes to the gradient input variables of second-order gradient operators for use as outputs. ([#41781](https://github.com/PaddlePaddle/Paddle/pull/41781)) + +- Remove the use of the deprecated enumeration type `PlaceType` from the Tensor API member methods, make it compatible, and add a deprecation warning. ([#41882](https://github.com/PaddlePaddle/Paddle/pull/41882)) + +- Add deprecated warning for a number of deprecated interfaces of the original Tensor API, including the incomplete constructor, reshape, mutable_data, and copy_to methods. ([#41882](https://github.com/PaddlePaddle/Paddle/pull/41882)) #### **Other** @@ -1454,6 +1587,15 @@ In order to solve the problem that the original static graph executor of the Pad - CPU parameter server streaming training optimization: support for automatic statistics of sparse parameter statistics, incremental saving of sparse parameters, etc. The training performance improves by 20%. ([#36465](https://github.com/PaddlePaddle/Paddle/pull/36465), [#36601](https://github.com/PaddlePaddle/Paddle/pull/36601), [#36734](https://github.com/PaddlePaddle/Paddle/pull/36734), [#36909](https://github.com/PaddlePaddle/Paddle/pull/36909), [#36943](https://github.com/PaddlePaddle/Paddle/pull/36943), [#37181](https://github.com/PaddlePaddle/Paddle/pull/37181), [#37194](https://github.com/PaddlePaddle/Paddle/pull/37194), [#37515](https://github.com/PaddlePaddle/Paddle/pull/37515), [#37626](https://github.com/PaddlePaddle/Paddle/pull/37626), [#37995](https://github.com/PaddlePaddle/Paddle/pull/37995), [#38582](https://github.com/PaddlePaddle/Paddle/pull/38582), [#39250](https://github.com/PaddlePaddle/Paddle/pull/39250), [#40762](https://github.com/PaddlePaddle/Paddle/pull/40762), [#41234](https://github.com/PaddlePaddle/Paddle/pull/41234), [#41320](https://github.com/PaddlePaddle/Paddle/pull/41320), [#41400](https://github.com/PaddlePaddle/Paddle/pull/41400)) +#### **Auto-tuning** + +Add hardware-aware automatic performance tuning for the full training process, with performance improvements of about 3% to 50% or more on image classification, segmentation, detection, and image generation tasks compared to the model's default configuration. The auto-tuning status is set via the `paddle.incubate.autotune.set_config ` API. By default, it is currently disabled. Auto-tuning has three specific levels: + +- Add the auto-tuning function to `paddle.io.DataLoader `, to select the best num_workers based on training data and device resources. ([#42004](https://github.com/PaddlePaddle/Paddle/pull/42004)) + +- Add mixed-precision training data layout auto-tuning feature, to select the best data layout based on device type and data type, and automatically convert it at runtime. ([#41964](https://github.com/PaddlePaddle/Paddle/pull/41964)) + +- Add the automatic tuning of the required workspace size threshold for Conv, which is automatically set based on the GPU's currently available requested device memory resources. Add the automatic selection of Conv cuDNN algorithms based on the generic AlgorithmCache design and Kernel timing component, which supports data variation length models.([#41833](https://github.com/PaddlePaddle/Paddle/pull/41833)) #### **Operator Optimization** @@ -1512,7 +1654,14 @@ In order to solve the problem that the original static graph executor of the Pad - Optimize `Elementwise` computation for multivariate output, improving performance by up to 15% over pre-optimization. ([#38329](https://github.com/PaddlePaddle/Paddle/pull/38329), [#38410](https://github.com/PaddlePaddle/Paddle/pull/38410)) - Optimize `Categorical`the probs computation, simplify the computation logic, and improve the performance by 4x to 5x. ([#42178](https://github.com/PaddlePaddle/Paddle/pull/42178)) - + +- Optimize the `paddle.sum ` performance, with performance improvement by about 20%. ([#42309](https://github.com/PaddlePaddle/Paddle/pull/42309)) + +- Remove CudaStreamSync operation from `paddle.nn.ClipGradByGlobalNorm ` to reduce scheduling overhead during execution, with 5% performance improvement on ptb models. ([#42170](https://github.com/PaddlePaddle/Paddle/pull/42170)) + +- Optimize a series of underlying data structures and detailed implementations in the original dynamic graph execution system to improve the scheduling performance of the original dynamic graph. ([#42010](https://github.com/PaddlePaddle/Paddle/pull/42010), [#42171](https://github.com/PaddlePaddle/Paddle/pull/42171), [#42224](https://github.com/PaddlePaddle/Paddle/pull/42224), [#42256](https://github.com/PaddlePaddle/Paddle/pull/42256), [#42306](https://github.com/PaddlePaddle/Paddle/pull/42306), [#42329](https://github.com/PaddlePaddle/Paddle/pull/42329)[, #42340](https://github.com/PaddlePaddle/Paddle/pull/42340), [#42368](https://github.com/PaddlePaddle/Paddle/pull/42368), [#42425](https://github.com/PaddlePaddle/Paddle/pull/42425)) + +- Simplify the probs calculation logics of `paddle.distribution.Categorical `, to improve performance by 4x to 5x. ([#42178](https://github.com/PaddlePaddle/Paddle/pull/42178)) ### **(4) Bug fixing** @@ -1632,6 +1781,27 @@ In order to solve the problem that the original static graph executor of the Pad - Fix the `axis` computation error in `paddle.fft` series of APIs. ([#36321](https://github.com/PaddlePaddle/Paddle/pull/36321)) +- Fix an output data type registration bug of batch_norm_grad op in case of FP16 data type. This bug causes the compilation failure in some scenarios. There is also the impact on FP16 computational precision. ([#42461](https://github.com/PaddlePaddle/Paddle/pull/42461)) + +- Fix the incorrect Infershape information bug in the `paddle.nn.functional.pad ` API when the padding is Tensor in dynamic to static conversion. ([#42414](https://github.com/PaddlePaddle/Paddle/pull/42414)) + +- Fix an exception in `paddle.distribution.StickBreakingTransform ` when the input dimension exceeds 2. ([#41762](https://github.com/PaddlePaddle/Paddle/pull/41672)) + +- Fix a nan/inf bug calculated with QK^T in fused_attention op. ([#42032](https://github.com/PaddlePaddle/Paddle/pull/42032)) + +- Fix a nan/inf bug calculated in fused_attention op with FusedResidualDropoutBias on V100. ([#42398](https://github.com/PaddlePaddle/Paddle/pull/42398)) + +- Fix a redundant data transform bug introduced by the full_like op during execution. ([#41973](https://github.com/PaddlePaddle/Paddle/pull/41973)) + +- Fix a problem with p_norm op calculating nan on GPU environments. ([#41804](https://github.com/PaddlePaddle/Paddle/pull/41804)) + +- Fix a section error of split op when the sections parameter has a size of 0. ([#41755](https://github.com/PaddlePaddle/Paddle/pull/41755)) + +- Fix the bug of reporting not supporting Place (gpu:0) in multi-card training when broadcast is required in 6 elementwise ops (pow, complex, divide_double, multiply_double, fmax, and fmin). ([#42332](https://github.com/PaddlePaddle/Paddle/pull/42332)) + +- Fix the bug that the deprecated interface reports a warning in case of `import paddle` due to a PIL version update. ([#42307](https://github.com/PaddlePaddle/Paddle/pull/42307)) + +- Fix the bug that `paddle.linalg.matrix_rank ` does not support tol as FP64 Tensor under static graph. ([#42085](https://github.com/PaddlePaddle/Paddle/pull/42085)) #### IR(Intermediate Representation) @@ -1658,6 +1828,10 @@ In order to solve the problem that the original static graph executor of the Pad - Fix the code conversion bug when returning a single value in control flow For. ([#40683](https://github.com/PaddlePaddle/Paddle/pull/40683)) - Fix the bug when generating a reverse op when the input to conditional_block op contains LoDTensorArray. ([#39585](https://github.com/PaddlePaddle/Paddle/pull/39585)) + + - Fix the bug that `padddle.jit.save ` loses the forward_pre_hook and forward_post_hook of the top Layer in case of the export of a dynamic-to-static model. ([#42273](https://github.com/PaddlePaddle/Paddle/pull/42273)) + + - Fix the dynamic to static conversion error report where the shape parameter in `paddle.expand ` contains a Tensor. ([#41973](https://github.com/PaddlePaddle/Paddle/pull/41973)) #### **Distributed Training** @@ -1814,17 +1988,13 @@ In order to solve the problem that the original static graph executor of the Pad - Fix the logic error when Expand_As op computes the output shape. ([#38677](https://github.com/PaddlePaddle/Paddle/pull/38677)) -- Frame function fixing - - - Fix the bug that the variables of the `core.VarDesc.VarType.STRINGS` type report error when getting the `lod_level` property and setting its `lod_level` to None. ([#39077](https://github.com/PaddlePaddle/Paddle/pull/39077)) - - - Fix an issue where the framework function `Pylayer` does not support different dtypes. ([#37974](https://github.com/PaddlePaddle/Paddle/pull/37974)) - -- API fixing +- Fix the bug that the variables of the `core.VarDesc.VarType.STRINGS` type report error when getting the `lod_level` property and setting its `lod_level` to None. ([#39077](https://github.com/PaddlePaddle/Paddle/pull/39077)) + +- Fix an issue where the framework function `Pylayer` does not support different dtypes. ([#37974](https://github.com/PaddlePaddle/Paddle/pull/37974)) - - Fix the bug of division by zero of the learning rate decay API `paddle.optimizer.lr.PolynomialDecay`. ([#38782](https://github.com/PaddlePaddle/Paddle/pull/38782)) - - - Fix the issue where some logs remained after calling the DisableGlogInfo() interface. ([#36356](https://github.com/PaddlePaddle/Paddle/pull/36356)) +- Fix the bug of division by zero of the learning rate decay API `paddle.optimizer.lr.PolynomialDecay`. ([#38782](https://github.com/PaddlePaddle/Paddle/pull/38782)) + +- Fix the issue where some logs remained after calling the DisableGlogInfo() interface. ([#36356](https://github.com/PaddlePaddle/Paddle/pull/36356)) - Fix an error in backward of multi-layer RNN (when dropout is set to 0) in the training of SimpleRNN, GRU and LSTM API CPU. ([#37080](https://github.com/PaddlePaddle/Paddle/pull/37080)) @@ -1833,6 +2003,10 @@ In order to solve the problem that the original static graph executor of the Pad - Enable the shifts parameter of `paddle.roll` to support transfer in Tensor. ([#36727](https://github.com/PaddlePaddle/Paddle/pull/36727)) - Add onemkl to fft as an optional computation backend. ([#36414](https://github.com/PaddlePaddle/Paddle/pull/36414)) + +- Fix the precision bug in the bfloat16 type under two mamtul_v2 and elementwise_div ops. ([#42479](https://github.com/PaddlePaddle/Paddle/pull/42479)) + +- Fix a possible error in the next step caused by LoDTensorArray clearing only the internal Tensor and not clearing the Array during device memory recycling. ([#42398](https://github.com/PaddlePaddle/Paddle/pull/42398)) ## **4. Deployment Direction (Paddle Inference)** @@ -1925,6 +2099,10 @@ In order to solve the problem that the original static graph executor of the Pad - Add TensorRT fuse pass: preln_embedding_eltwise_layernorm_fuse_pass, preln_skip_layernorm_fuse_pass, for ERNIE-like model performance optimization. ([#39508](https://github.com/PaddlePaddle/Paddle/pull/39508)) - Split matmul fusion-related passes based on different backends (GPU, CPU, TensorRT), to support transpose function for FC weights. ([#39369](https://github.com/PaddlePaddle/Paddle/pull/39369)) + +- Add the support to TensorRT by roll, strided_slice, and slice op in case of dynamic shapes. ([#41913](https://github.com/PaddlePaddle/Paddle/pull/41913), [#41573](https://github.com/PaddlePaddle/Paddle/pull/41573), [#41467](https://github.com/PaddlePaddle/Paddle/pull/41467)) + +- Add div op support for TensorRT. ([#41243](https://github.com/PaddlePaddle/Paddle/pull/41243)) - Quantization support @@ -1974,6 +2152,7 @@ In order to solve the problem that the original static graph executor of the Pad - The TensorRT dynamic shape parameter automatically generate the interface, to add the file existence check. ([#36628](https://github.com/PaddlePaddle/Paddle/pull/36628)) +- Fix the bug that the MKLDNN does not support conv3d. ([#42055](https://github.com/PaddlePaddle/Paddle/pull/42055)) #### **Backend Capability Fixing** @@ -2090,7 +2269,7 @@ In order to solve the problem that the original static graph executor of the Pad ### **Compile and Install** -- From version 2.3.0-rc0, PaddlePaddle has adjusted and upgraded the types of GPU architectures supported by the framework. (For more information, please refer to: [GPU architectures supported by PaddlePaddle](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.3rc/install/Tables.html#gpu)) +- From version 2.3.0, PaddlePaddle has adjusted and upgraded the types of GPU architectures supported by the framework. (For more information, please refer to: [GPU architectures supported by PaddlePaddle](https://www.paddlepaddle.org.cn/documentation/docs/zh/2.3rc/install/Tables.html#gpu)) Notes: @@ -2115,6 +2294,8 @@ Notes: - CUDA11 : 3.5, 5.0, 6.0, 6.1, 7.0, 7.5, 8.0。 +- Support Python 3.10. Fix compilation bugs caused by some PythonC API changes on Windows. ([#41180](https://github.com/PaddlePaddle/Paddle/pull/42180)) + - The Windows platform supports the compilation through Visual Studio 2019. ([#38719](https://github.com/PaddlePaddle/Paddle/pull/38719)) - Eliminate various warnings when compiling on the Windows platform. ([#38034](https://github.com/PaddlePaddle/Paddle/pull/38034), [#37890](https://github.com/PaddlePaddle/Paddle/pull/37890), [#37442](https://github.com/PaddlePaddle/Paddle/pull/37442), [#37439](https://github.com/PaddlePaddle/Paddle/pull/37439), [#36857](https://github.com/PaddlePaddle/Paddle/pull/36857)) @@ -2132,7 +2313,7 @@ Notes: - Support cambricon MLU chip (MLU370x4) training/inference. Support models such as ResNet50. Support static graph + dynamic graph training. Support auto-mixed precision training. Support single card, and distribute training across multiple cards, multiple machines. -- Support KUNLUNXIN 2 chips (Kunlunxin AI acceleration cards R200, R300) training/inference. Support ResNet50, YoloV3, OCR-DB, SSD, MobilnetV3, UNet, BERT, Transformer, GPT-2, Wide&Deep, and DeepFM. Support static graph + dynamic graph training. Support auto-mixed precision training. Support single card, and distribute training across multiple cards, multiple machines. +- Support KUNLUNXIN 2 chips (KUNLUNXIN AI acceleration cards R200, R300) training/inference. Support ResNet50, YoloV3, OCR-DB, SSD, MobilnetV3, UNet, BERT, Transformer, GPT-2, Wide&Deep, and DeepFM. Support static graph + dynamic graph training. Support auto-mixed precision training. Support single card, and distribute training across multiple cards, multiple machines. ## Thanks to our Contributors