未验证 提交 29cc8b1a 编写于 作者: W whisky-12 提交者: GitHub

[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/*
上级 f7d1a940
...@@ -784,50 +784,52 @@ def decorate( ...@@ -784,50 +784,52 @@ def decorate(
Examples: Examples:
.. code-block:: python .. code-block:: python
# required: gpu >>> # doctest: +REQUIRES(env:GPU)
# Demo1: single model and optimizer: >>> # Demo1: single model and optimizer:
import paddle >>> import paddle
>>> paddle.device.set_device('gpu')
model = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) >>> model = paddle.nn.Conv2D(3, 2, 3, bias_attr=False)
optimizer = paddle.optimizer.SGD(parameters=model.parameters()) >>> 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'): >>> with paddle.amp.auto_cast(enable=True, custom_white_list=None, custom_black_list=None, level='O2'):
output = model(data) ... output = model(data)
print(output.dtype) # FP16 ... assert output.dtype == paddle.float16
# required: gpu >>> # doctest: +REQUIRES(env:GPU)
# Demo2: multi models and optimizers: >>> # Demo2: multi models and optimizers:
model2 = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) >>> model2 = paddle.nn.Conv2D(3, 2, 3, bias_attr=False)
optimizer2 = paddle.optimizer.Adam(parameters=model2.parameters()) >>> 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'): >>> with paddle.amp.auto_cast(enable=True, custom_white_list=None, custom_black_list=None, level='O2'):
output = models[0](data) ... output = models[0](data)
output2 = models[1](data) ... output2 = models[1](data)
print(output.dtype) # FP16 ... assert output.dtype == paddle.float16
print(output2.dtype) # FP16 ... assert output2.dtype == paddle.float16
# required: gpu >>> # doctest: +REQUIRES(env:GPU)
# Demo3: optimizers is None: >>> # Demo3: optimizers is None:
model3 = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) >>> model3 = paddle.nn.Conv2D(3, 2, 3, bias_attr=False)
optimizer3 = paddle.optimizer.Adam(parameters=model3.parameters()) >>> 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( return amp_decorate(
models, models,
......
...@@ -608,25 +608,25 @@ def disable_tensor_checker(): ...@@ -608,25 +608,25 @@ def disable_tensor_checker():
Examples: 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) >>> 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) >>> paddle.amp.debugging.enable_tensor_checker(checker_config)
x = paddle.to_tensor([1, 0, 3], place=paddle.CPUPlace(), dtype='float32', stop_gradient=False) >>> 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') >>> y = paddle.to_tensor([0.2, 0, 0.5], place=paddle.CPUPlace(), dtype='float32')
res = paddle.pow(x, y) >>> res = paddle.pow(x, y)
paddle.autograd.backward(res, retain_graph=True) >>> paddle.autograd.backward(res, retain_graph=True)
paddle.amp.debugging.disable_tensor_checker() >>> 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 >>> #[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 >>> # when DebugMode.CHECK_NAN_INF_AND_ABORT and stack_height_limit = 1
# Traceback (most recent call last): >>> # Traceback (most recent call last):
# res = paddle.pow(x, y) >>> # res = paddle.pow(x, y)
# File "/usr/local/lib/python3.8/dist-packages/paddle/tensor/math.py", line 447, in pow >>> # File "/usr/local/lib/python3.8/dist-packages/paddle/tensor/math.py", line 447, in pow
# return _C_ops.elementwise_pow(x, y) >>> # return _C_ops.elementwise_pow(x, y)
""" """
paddle.set_flags({"FLAGS_check_nan_inf": 0}) paddle.set_flags({"FLAGS_check_nan_inf": 0})
...@@ -1180,17 +1180,17 @@ class GradScaler(AmpScaler): ...@@ -1180,17 +1180,17 @@ class GradScaler(AmpScaler):
.. code-block:: python .. code-block:: python
# required: gpu,xpu >>> # doctest: +REQUIRES(env:GPU, env:XPU)
import paddle >>> import paddle
scaler = paddle.amp.GradScaler(enable=True, >>> scaler = paddle.amp.GradScaler(enable=True,
init_loss_scaling=1024, ... init_loss_scaling=1024,
incr_ratio=2.0, ... incr_ratio=2.0,
decr_ratio=0.5, ... decr_ratio=0.5,
incr_every_n_steps=1000, ... incr_every_n_steps=1000,
decr_every_n_nan_or_inf=2, ... decr_every_n_nan_or_inf=2,
use_dynamic_loss_scaling=True) ... use_dynamic_loss_scaling=True)
scaler_state = scaler.state_dict() >>> scaler_state = scaler.state_dict()
scaler.load_state_dict(scaler_state) >>> scaler.load_state_dict(scaler_state)
""" """
super().load_state_dict(state_dict) super().load_state_dict(state_dict)
...@@ -875,23 +875,25 @@ class stream_guard: ...@@ -875,23 +875,25 @@ class stream_guard:
def synchronize(device=None): def synchronize(device=None):
''' """
Wait for the compute on the given device to finish. Wait for the compute on the given device to finish.
Parameters: 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. 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, 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). 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: Examples:
.. code-block:: python .. code-block:: python
# required: custom_device
import paddle
paddle.set_device('custom_cpu') >>> # doctest: +REQUIRES(env:CUSTOM_DEVICE)
paddle.device.synchronize() >>> import paddle
paddle.device.synchronize("custom_cpu:0")
place = paddle.CustomPlace('custom_cpu', 0) >>> paddle.set_device('custom_cpu')
paddle.device.synchronize(place) >>> paddle.device.synchronize()
''' >>> paddle.device.synchronize("custom_cpu:0")
>>> place = paddle.CustomPlace('custom_cpu', 0)
>>> paddle.device.synchronize(place)
"""
if device is None: if device is None:
place = paddle.framework._current_expected_place() place = paddle.framework._current_expected_place()
......
...@@ -509,7 +509,7 @@ def get_device_name(device=None): ...@@ -509,7 +509,7 @@ def get_device_name(device=None):
def get_device_capability(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 <https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__DEVICE.html#group__CUDART__DEVICE_1g1bf9d625a931d657e08db2b4391170f0>`_. Return the major and minor revision numbers defining the device's compute capability which are got from CUDA function `cudaDeviceProp <https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__DEVICE.html#group__CUDART__DEVICE_1g1bf9d625a931d657e08db2b4391170f0>`_.
Parameters: Parameters:
...@@ -522,16 +522,16 @@ def get_device_capability(device=None): ...@@ -522,16 +522,16 @@ def get_device_capability(device=None):
.. code-block:: python .. 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) prop = get_device_properties(device)
return prop.major, prop.minor return prop.major, prop.minor
...@@ -28,7 +28,7 @@ __all__ = [ ...@@ -28,7 +28,7 @@ __all__ = [
reason="synchronize in paddle.device.xpu will be removed in future", reason="synchronize in paddle.device.xpu will be removed in future",
) )
def synchronize(device=None): def synchronize(device=None):
''' """
Wait for the compute on the given XPU device to finish. Wait for the compute on the given XPU device to finish.
Parameters: Parameters:
...@@ -38,14 +38,14 @@ def synchronize(device=None): ...@@ -38,14 +38,14 @@ def synchronize(device=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
# required: xpu >>> # doctest: +REQUIRES(env:XPU)
import paddle >>> 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 device_id = -1
......
...@@ -860,20 +860,21 @@ class Profiler: ...@@ -860,20 +860,21 @@ class Profiler:
Examples: Examples:
.. code-block:: python .. code-block:: python
:name: code-example8
# required: gpu >>> # doctest: +REQUIRES(env:GPU)
import paddle.profiler as profiler >>> import paddle
prof = profiler.Profiler( >>> paddle.device.set_device('gpu')
targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU], >>> import paddle.profiler as profiler
scheduler = (3, 7), >>> prof = profiler.Profiler(
on_trace_ready = profiler.export_chrome_tracing('./log')) ... targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU],
prof.start() ... scheduler = (3, 7),
for iter in range(10): ... on_trace_ready = profiler.export_chrome_tracing('./log'))
#train() >>> prof.start()
prof.step() >>> for iter in range(10):
prof.stop() ... #train()
prof.summary(sorted_by=profiler.SortedKeys.CPUTotal, op_detail=True, thread_sep=False, time_unit='ms') ... prof.step()
>>> prof.stop()
>>> prof.summary(sorted_by=profiler.SortedKeys.CPUTotal, op_detail=True, thread_sep=False, time_unit='ms')
""" """
if isinstance(views, SummaryView): if isinstance(views, SummaryView):
views = [views] views = [views]
......
...@@ -116,7 +116,7 @@ class RecordEvent(ContextDecorator): ...@@ -116,7 +116,7 @@ class RecordEvent(ContextDecorator):
self.event = _RecordEvent(self.name, self.event_type) self.event = _RecordEvent(self.name, self.event_type)
def end(self): def end(self):
r''' r"""
Record the time of ending. Record the time of ending.
Examples: Examples:
...@@ -132,7 +132,7 @@ class RecordEvent(ContextDecorator): ...@@ -132,7 +132,7 @@ class RecordEvent(ContextDecorator):
data2 = paddle.randn(shape=[3]) data2 = paddle.randn(shape=[3])
result = data1 * data2 result = data1 * data2
record_event.end() record_event.end()
''' """
if self.event: if self.event:
self.event.end() self.event.end()
...@@ -197,15 +197,17 @@ def wrap_optimizers(): ...@@ -197,15 +197,17 @@ def wrap_optimizers():
@contextmanager @contextmanager
def _nvprof_range(iter_id, start, end, exit_after_prof=True): def _nvprof_range(iter_id, start, end, exit_after_prof=True):
''' """
A range profiler interface (not public yet). A range profiler interface (not public yet).
Examples: Examples:
.. code-block:: python .. code-block:: python
model = Model()
for i in range(max_iter): >>> import paddle
paddle.fluid.profiler._nvprof_range(i, 10, 20): >>> model = Model()
out = model(in) >>> for i in range(max_iter):
''' ... with paddle.profiler.utils._nvprof_range(i, 10, 20):
... out = model(in)
"""
if start >= end: if start >= end:
yield yield
return return
......
...@@ -193,25 +193,28 @@ def mv(x, vec, name=None): ...@@ -193,25 +193,28 @@ def mv(x, vec, name=None):
.. code-block:: python .. code-block:: python
# required: gpu >>> # doctest: +REQUIRES(env:GPU)
import paddle >>> import paddle
paddle.seed(100) >>> paddle.device.set_device('gpu')
>>> paddle.seed(100)
# csr @ dense -> dense
crows = [0, 2, 3, 5] >>> # csr @ dense -> dense
cols = [1, 3, 2, 0, 1] >>> crows = [0, 2, 3, 5]
values = [1., 2., 3., 4., 5.] >>> cols = [1, 3, 2, 0, 1]
dense_shape = [3, 4] >>> values = [1., 2., 3., 4., 5.]
csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, dense_shape) >>> dense_shape = [3, 4]
# Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True, >>> csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, dense_shape)
# crows=[0, 2, 3, 5], >>> print(csr)
# cols=[1, 3, 2, 0, 1], Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(gpu:0), stop_gradient=True,
# values=[1., 2., 3., 4., 5.]) crows=[0, 2, 3, 5],
vec = paddle.randn([4]) cols=[1, 3, 2, 0, 1],
values=[1., 2., 3., 4., 5.])
out = paddle.sparse.mv(csr, vec) >>> vec = paddle.randn([4])
# Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [-3.85499096, -2.42975140, -1.75087738]) >>> 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) return _C_ops.sparse_mv(x, vec)
......
...@@ -217,20 +217,20 @@ def sparse_csr_tensor( ...@@ -217,20 +217,20 @@ def sparse_csr_tensor(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
crows = [0, 2, 3, 5] >>> crows = [0, 2, 3, 5]
cols = [1, 3, 2, 0, 1] >>> cols = [1, 3, 2, 0, 1]
values = [1, 2, 3, 4, 5] >>> values = [1, 2, 3, 4, 5]
dense_shape = [3, 4] >>> dense_shape = [3, 4]
csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, dense_shape) >>> csr = paddle.sparse.sparse_csr_tensor(crows, cols, values, dense_shape)
# print(csr) >>> print(csr)
# Tensor(shape=[3, 4], dtype=paddle.int64, place=Place(gpu:0), stop_gradient=True, Tensor(shape=[3, 4], dtype=paddle.int64, place=Place(cpu), stop_gradient=True,
# crows=[0, 2, 3, 5], crows=[0, 2, 3, 5],
# cols=[1, 3, 2, 0, 1], cols=[1, 3, 2, 0, 1],
# values=[1, 2, 3, 4, 5]) values=[1, 2, 3, 4, 5])
""" """
place = _get_place(place) place = _get_place(place)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册