From 29cc8b1a82d54a40da92baaab1db724a34fd6a37 Mon Sep 17 00:00:00 2001 From: whisky-12 <79358023+whisky-12@users.noreply.github.com> Date: Tue, 22 Aug 2023 19:27:44 +0800 Subject: [PATCH] [xdoctest][task 171-180] reformat example code with google style in audio/* (#56159) * [xdoctest][task 171-180] reformat example code with google style in audio/* * [xdoctest][task 171-180] reformat example code with google style in audio/* * [xdoctest][task 171-180] reformat example code with google style in audio/* * [xdoctest][task 171-180] reformat example code with google style in audio/* * [xdoctest][task 171-180] reformat example code with google style in audio/* * [xdoctest][task 171-180] reformat example code with google style in audio * [xdoctest][task 171-180] reformat example code with google style in audio/* * [xdoctest][task 171-180] reformat example code with google style in audio/* * [xdoctest][task 171-180] reformat example code with google style in audio/* * [xdoctest][task 171-180] reformat example code with google style in audio/* * [xdoctest][task 171-180] reformat example code with google style in audio/* * [xdoctest][task 171-180] reformat example code with google style in audio/* * [xdoctest][task 171-180] reformat example code with google style in audio/* * [xdoctest][task 171-180] reformat example code with google style in audio/* * [xdoctest][task 171-180] reformat example code with google style in audio/* * [xdoctest][task 171-180] reformat example code with google style in audio/* --- python/paddle/amp/auto_cast.py | 64 ++++++++++++++------------- python/paddle/amp/debugging.py | 30 ++++++------- python/paddle/amp/grad_scaler.py | 24 +++++----- python/paddle/device/__init__.py | 20 +++++---- python/paddle/device/cuda/__init__.py | 16 +++---- python/paddle/device/xpu/__init__.py | 16 +++---- python/paddle/profiler/profiler.py | 27 +++++------ python/paddle/profiler/utils.py | 18 ++++---- python/paddle/sparse/binary.py | 41 +++++++++-------- python/paddle/sparse/creation.py | 28 ++++++------ 10 files changed, 147 insertions(+), 137 deletions(-) diff --git a/python/paddle/amp/auto_cast.py b/python/paddle/amp/auto_cast.py index b1e9b4c00b2..699d9f32d67 100644 --- a/python/paddle/amp/auto_cast.py +++ b/python/paddle/amp/auto_cast.py @@ -784,50 +784,52 @@ def decorate( Examples: - .. code-block:: python + .. code-block:: python - # required: gpu - # Demo1: single model and optimizer: - import paddle + >>> # doctest: +REQUIRES(env:GPU) + >>> # Demo1: single model and optimizer: + >>> import paddle + >>> paddle.device.set_device('gpu') - model = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) - optimizer = paddle.optimizer.SGD(parameters=model.parameters()) + >>> model = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) + >>> optimizer = paddle.optimizer.SGD(parameters=model.parameters()) - model, optimizer = paddle.amp.decorate(models=model, optimizers=optimizer, level='O2') + >>> model, optimizer = paddle.amp.decorate(models=model, optimizers=optimizer, level='O2') - data = paddle.rand([10, 3, 32, 32]) + >>> data = paddle.rand([10, 3, 32, 32]) - with paddle.amp.auto_cast(enable=True, custom_white_list=None, custom_black_list=None, level='O2'): - output = model(data) - print(output.dtype) # FP16 + >>> with paddle.amp.auto_cast(enable=True, custom_white_list=None, custom_black_list=None, level='O2'): + ... output = model(data) + ... assert output.dtype == paddle.float16 - # required: gpu - # Demo2: multi models and optimizers: - model2 = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) - optimizer2 = paddle.optimizer.Adam(parameters=model2.parameters()) + >>> # doctest: +REQUIRES(env:GPU) + >>> # Demo2: multi models and optimizers: + >>> model2 = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) + >>> optimizer2 = paddle.optimizer.Adam(parameters=model2.parameters()) - models, optimizers = paddle.amp.decorate(models=[model, model2], optimizers=[optimizer, optimizer2], level='O2') + >>> models, optimizers = paddle.amp.decorate(models=[model, model2], optimizers=[optimizer, optimizer2], level='O2') - data = paddle.rand([10, 3, 32, 32]) + >>> data = paddle.rand([10, 3, 32, 32]) - with paddle.amp.auto_cast(enable=True, custom_white_list=None, custom_black_list=None, level='O2'): - output = models[0](data) - output2 = models[1](data) - print(output.dtype) # FP16 - print(output2.dtype) # FP16 + >>> with paddle.amp.auto_cast(enable=True, custom_white_list=None, custom_black_list=None, level='O2'): + ... output = models[0](data) + ... output2 = models[1](data) + ... assert output.dtype == paddle.float16 + ... assert output2.dtype == paddle.float16 - # required: gpu - # Demo3: optimizers is None: - model3 = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) - optimizer3 = paddle.optimizer.Adam(parameters=model3.parameters()) + >>> # doctest: +REQUIRES(env:GPU) + >>> # Demo3: optimizers is None: + >>> model3 = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) + >>> optimizer3 = paddle.optimizer.Adam(parameters=model3.parameters()) - model = paddle.amp.decorate(models=model3, level='O2') + >>> model = paddle.amp.decorate(models=model3, level='O2') - data = paddle.rand([10, 3, 32, 32]) + >>> data = paddle.rand([10, 3, 32, 32]) + + >>> with paddle.amp.auto_cast(enable=True, custom_white_list=None, custom_black_list=None, level='O2'): + ... output = model(data) + ... assert output.dtype == paddle.float16 - with paddle.amp.auto_cast(enable=True, custom_white_list=None, custom_black_list=None, level='O2'): - output = model(data) - print(output.dtype) # FP16 """ return amp_decorate( models, diff --git a/python/paddle/amp/debugging.py b/python/paddle/amp/debugging.py index 4c2b2c96edf..82011d89c0c 100644 --- a/python/paddle/amp/debugging.py +++ b/python/paddle/amp/debugging.py @@ -608,25 +608,25 @@ def disable_tensor_checker(): Examples: - .. code-block:: python + .. code-block:: python - import paddle + >>> import paddle - checker_config = paddle.amp.debugging.TensorCheckerConfig(enable=True, debug_mode=paddle.amp.debugging.DebugMode.CHECK_NAN_INF) - paddle.amp.debugging.enable_tensor_checker(checker_config) + >>> checker_config = paddle.amp.debugging.TensorCheckerConfig(enable=True, debug_mode=paddle.amp.debugging.DebugMode.CHECK_NAN_INF) + >>> paddle.amp.debugging.enable_tensor_checker(checker_config) - x = paddle.to_tensor([1, 0, 3], place=paddle.CPUPlace(), dtype='float32', stop_gradient=False) - y = paddle.to_tensor([0.2, 0, 0.5], place=paddle.CPUPlace(), dtype='float32') - res = paddle.pow(x, y) - paddle.autograd.backward(res, retain_graph=True) - paddle.amp.debugging.disable_tensor_checker() - #[PRECISION] [ERROR] in [device=cpu, op=elementwise_pow_grad, tensor=, dtype=fp32], numel=3, num_nan=1, num_inf=0, num_zero=0, max=2.886751e-01, min=2.000000e-01, mean=-nan + >>> x = paddle.to_tensor([1, 0, 3], place=paddle.CPUPlace(), dtype='float32', stop_gradient=False) + >>> y = paddle.to_tensor([0.2, 0, 0.5], place=paddle.CPUPlace(), dtype='float32') + >>> res = paddle.pow(x, y) + >>> paddle.autograd.backward(res, retain_graph=True) + >>> paddle.amp.debugging.disable_tensor_checker() + >>> #[PRECISION] [ERROR] in [device=cpu, op=elementwise_pow_grad, tensor=, dtype=fp32], numel=3, num_nan=1, num_inf=0, num_zero=0, max=2.886751e-01, min=2.000000e-01, mean=-nan - # when DebugMode.CHECK_NAN_INF_AND_ABORT and stack_height_limit = 1 - # Traceback (most recent call last): - # res = paddle.pow(x, y) - # File "/usr/local/lib/python3.8/dist-packages/paddle/tensor/math.py", line 447, in pow - # return _C_ops.elementwise_pow(x, y) + >>> # when DebugMode.CHECK_NAN_INF_AND_ABORT and stack_height_limit = 1 + >>> # Traceback (most recent call last): + >>> # res = paddle.pow(x, y) + >>> # File "/usr/local/lib/python3.8/dist-packages/paddle/tensor/math.py", line 447, in pow + >>> # return _C_ops.elementwise_pow(x, y) """ paddle.set_flags({"FLAGS_check_nan_inf": 0}) diff --git a/python/paddle/amp/grad_scaler.py b/python/paddle/amp/grad_scaler.py index 1680b6ace85..72dc592f62b 100644 --- a/python/paddle/amp/grad_scaler.py +++ b/python/paddle/amp/grad_scaler.py @@ -1180,17 +1180,17 @@ class GradScaler(AmpScaler): .. code-block:: python - # required: gpu,xpu - import paddle - - scaler = paddle.amp.GradScaler(enable=True, - init_loss_scaling=1024, - incr_ratio=2.0, - decr_ratio=0.5, - incr_every_n_steps=1000, - decr_every_n_nan_or_inf=2, - use_dynamic_loss_scaling=True) - scaler_state = scaler.state_dict() - scaler.load_state_dict(scaler_state) + >>> # doctest: +REQUIRES(env:GPU, env:XPU) + >>> import paddle + + >>> scaler = paddle.amp.GradScaler(enable=True, + ... init_loss_scaling=1024, + ... incr_ratio=2.0, + ... decr_ratio=0.5, + ... incr_every_n_steps=1000, + ... decr_every_n_nan_or_inf=2, + ... use_dynamic_loss_scaling=True) + >>> scaler_state = scaler.state_dict() + >>> scaler.load_state_dict(scaler_state) """ super().load_state_dict(state_dict) diff --git a/python/paddle/device/__init__.py b/python/paddle/device/__init__.py index c19e6e08b4d..a7b7dd1be05 100644 --- a/python/paddle/device/__init__.py +++ b/python/paddle/device/__init__.py @@ -875,23 +875,25 @@ class stream_guard: def synchronize(device=None): - ''' + """ Wait for the compute on the given device to finish. Parameters: device(str|paddle.CUDAPlace(n)|paddle.XPUPlace(n)|paddle.CustomPlace(n)): The device which want to wait for. If device is None, the device is the current device. Default: None. It can be ``gpu``, ``gpu:x``, ``xpu``, ``xpu:x``, ``custom_device``, ``custom_device:x``, where ``custom_device`` is the name of CustomDevicec, where ``x`` is the index of the GPUs, XPUs. And it can be paddle.CUDAPlace(n) or paddle.XPUPlace(n) or paddle.CustomPlace(n). Examples: + .. code-block:: python - # required: custom_device - import paddle - paddle.set_device('custom_cpu') - paddle.device.synchronize() - paddle.device.synchronize("custom_cpu:0") - place = paddle.CustomPlace('custom_cpu', 0) - paddle.device.synchronize(place) - ''' + >>> # doctest: +REQUIRES(env:CUSTOM_DEVICE) + >>> import paddle + + >>> paddle.set_device('custom_cpu') + >>> paddle.device.synchronize() + >>> paddle.device.synchronize("custom_cpu:0") + >>> place = paddle.CustomPlace('custom_cpu', 0) + >>> paddle.device.synchronize(place) + """ if device is None: place = paddle.framework._current_expected_place() diff --git a/python/paddle/device/cuda/__init__.py b/python/paddle/device/cuda/__init__.py index 916cb77af98..7f3ea571fb2 100644 --- a/python/paddle/device/cuda/__init__.py +++ b/python/paddle/device/cuda/__init__.py @@ -509,7 +509,7 @@ def get_device_name(device=None): def get_device_capability(device=None): - ''' + """ Return the major and minor revision numbers defining the device's compute capability which are got from CUDA function `cudaDeviceProp `_. Parameters: @@ -522,16 +522,16 @@ def get_device_capability(device=None): .. code-block:: python - # required: gpu + >>> # doctest: +REQUIRES(env:GPU) - import paddle + >>> import paddle + >>> paddle.device.set_device('gpu') + >>> paddle.device.cuda.get_device_capability() - paddle.device.cuda.get_device_capability() + >>> paddle.device.cuda.get_device_capability(0) - paddle.device.cuda.get_device_capability(0) + >>> paddle.device.cuda.get_device_capability(paddle.CUDAPlace(0)) - paddle.device.cuda.get_device_capability(paddle.CUDAPlace(0)) - - ''' + """ prop = get_device_properties(device) return prop.major, prop.minor diff --git a/python/paddle/device/xpu/__init__.py b/python/paddle/device/xpu/__init__.py index 832c1baa631..9d0ff4b3ea6 100644 --- a/python/paddle/device/xpu/__init__.py +++ b/python/paddle/device/xpu/__init__.py @@ -28,7 +28,7 @@ __all__ = [ reason="synchronize in paddle.device.xpu will be removed in future", ) def synchronize(device=None): - ''' + """ Wait for the compute on the given XPU device to finish. Parameters: @@ -38,14 +38,14 @@ def synchronize(device=None): Examples: .. code-block:: python - # required: xpu - import paddle + >>> # doctest: +REQUIRES(env:XPU) + >>> import paddle + >>> paddle.device.set_device('xpu') + >>> paddle.device.xpu.synchronize() + >>> paddle.device.xpu.synchronize(0) + >>> paddle.device.xpu.synchronize(paddle.XPUPlace(0)) - paddle.device.xpu.synchronize() - paddle.device.xpu.synchronize(0) - paddle.device.xpu.synchronize(paddle.XPUPlace(0)) - - ''' + """ device_id = -1 diff --git a/python/paddle/profiler/profiler.py b/python/paddle/profiler/profiler.py index 2260382453c..87b63336d04 100644 --- a/python/paddle/profiler/profiler.py +++ b/python/paddle/profiler/profiler.py @@ -860,20 +860,21 @@ class Profiler: Examples: .. code-block:: python - :name: code-example8 - # required: gpu - import paddle.profiler as profiler - prof = profiler.Profiler( - targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU], - scheduler = (3, 7), - on_trace_ready = profiler.export_chrome_tracing('./log')) - prof.start() - for iter in range(10): - #train() - prof.step() - prof.stop() - prof.summary(sorted_by=profiler.SortedKeys.CPUTotal, op_detail=True, thread_sep=False, time_unit='ms') + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> paddle.device.set_device('gpu') + >>> import paddle.profiler as profiler + >>> prof = profiler.Profiler( + ... targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU], + ... scheduler = (3, 7), + ... on_trace_ready = profiler.export_chrome_tracing('./log')) + >>> prof.start() + >>> for iter in range(10): + ... #train() + ... prof.step() + >>> prof.stop() + >>> prof.summary(sorted_by=profiler.SortedKeys.CPUTotal, op_detail=True, thread_sep=False, time_unit='ms') """ if isinstance(views, SummaryView): views = [views] diff --git a/python/paddle/profiler/utils.py b/python/paddle/profiler/utils.py index aad128cb95a..851d34d5b7a 100644 --- a/python/paddle/profiler/utils.py +++ b/python/paddle/profiler/utils.py @@ -116,7 +116,7 @@ class RecordEvent(ContextDecorator): self.event = _RecordEvent(self.name, self.event_type) def end(self): - r''' + r""" Record the time of ending. Examples: @@ -132,7 +132,7 @@ class RecordEvent(ContextDecorator): data2 = paddle.randn(shape=[3]) result = data1 * data2 record_event.end() - ''' + """ if self.event: self.event.end() @@ -197,15 +197,17 @@ def wrap_optimizers(): @contextmanager def _nvprof_range(iter_id, start, end, exit_after_prof=True): - ''' + """ A range profiler interface (not public yet). Examples: .. code-block:: python - model = Model() - for i in range(max_iter): - paddle.fluid.profiler._nvprof_range(i, 10, 20): - out = model(in) - ''' + + >>> import paddle + >>> model = Model() + >>> for i in range(max_iter): + ... with paddle.profiler.utils._nvprof_range(i, 10, 20): + ... out = model(in) + """ if start >= end: yield return diff --git a/python/paddle/sparse/binary.py b/python/paddle/sparse/binary.py index 36ced04b558..9de43619097 100644 --- a/python/paddle/sparse/binary.py +++ b/python/paddle/sparse/binary.py @@ -193,25 +193,28 @@ def mv(x, vec, name=None): .. code-block:: python - # required: gpu - import paddle - paddle.seed(100) - - # csr @ dense -> dense - crows = [0, 2, 3, 5] - cols = [1, 3, 2, 0, 1] - values = [1., 2., 3., 4., 5.] - dense_shape = [3, 4] - csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, dense_shape) - # Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True, - # crows=[0, 2, 3, 5], - # cols=[1, 3, 2, 0, 1], - # values=[1., 2., 3., 4., 5.]) - vec = paddle.randn([4]) - - out = paddle.sparse.mv(csr, vec) - # Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [-3.85499096, -2.42975140, -1.75087738]) + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> paddle.device.set_device('gpu') + >>> paddle.seed(100) + + >>> # csr @ dense -> dense + >>> crows = [0, 2, 3, 5] + >>> cols = [1, 3, 2, 0, 1] + >>> values = [1., 2., 3., 4., 5.] + >>> dense_shape = [3, 4] + >>> csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, dense_shape) + >>> print(csr) + Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True, + crows=[0, 2, 3, 5], + cols=[1, 3, 2, 0, 1], + values=[1., 2., 3., 4., 5.]) + >>> vec = paddle.randn([4]) + + >>> out = paddle.sparse.mv(csr, vec) + >>> print(out) + Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [-3.85499096, -2.42975140, -1.75087738]) """ return _C_ops.sparse_mv(x, vec) diff --git a/python/paddle/sparse/creation.py b/python/paddle/sparse/creation.py index b57de8d245c..62a83aab398 100644 --- a/python/paddle/sparse/creation.py +++ b/python/paddle/sparse/creation.py @@ -217,20 +217,20 @@ def sparse_csr_tensor( Examples: - .. code-block:: python - - import paddle - - crows = [0, 2, 3, 5] - cols = [1, 3, 2, 0, 1] - values = [1, 2, 3, 4, 5] - dense_shape = [3, 4] - csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, dense_shape) - # print(csr) - # Tensor(shape=[3, 4], dtype=paddle.int64, place=Place(gpu:0), stop_gradient=True, - # crows=[0, 2, 3, 5], - # cols=[1, 3, 2, 0, 1], - # values=[1, 2, 3, 4, 5]) + .. code-block:: python + + >>> import paddle + + >>> crows = [0, 2, 3, 5] + >>> cols = [1, 3, 2, 0, 1] + >>> values = [1, 2, 3, 4, 5] + >>> dense_shape = [3, 4] + >>> csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, dense_shape) + >>> print(csr) + Tensor(shape=[3, 4], dtype=paddle.int64, place=Place(cpu), stop_gradient=True, + crows=[0, 2, 3, 5], + cols=[1, 3, 2, 0, 1], + values=[1, 2, 3, 4, 5]) """ place = _get_place(place) -- GitLab