未验证 提交 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(
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,
......
......@@ -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})
......@@ -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)
......@@ -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()
......
......@@ -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 <https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__DEVICE.html#group__CUDART__DEVICE_1g1bf9d625a931d657e08db2b4391170f0>`_.
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
......@@ -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
......
......@@ -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]
......
......@@ -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
......
......@@ -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)
......
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册