From b9353f6a44108760cd71f9aec340851f78a92a84 Mon Sep 17 00:00:00 2001 From: xingzhongfan Date: Fri, 11 Apr 2025 17:08:03 +0800 Subject: [PATCH] update flow doc --- MindFlow/mindflow/cell/diffusion.py | 2 +- MindFlow/mindflow/core/optimizers.py | 4 ++-- .../cell/mindflow.cell.AttentionBlock.rst | 4 ++-- .../cell/mindflow.cell.DDIMScheduler.rst | 4 ++-- .../cell/mindflow.cell.DDPMScheduler.rst | 4 ++-- .../cell/mindflow.cell.DiffusionTrainer.rst | 5 ++++- .../cell/mindflow.cell.DiffusionTransformer.rst | 17 ++++++++--------- .../cell/mindflow.cell.MultiHeadAttention.rst | 4 ++-- .../mindflow/cell/mindflow.cell.UNet2D.rst | 7 +++++-- .../mindflow/cell/mindflow.cell.ViT.rst | 5 ++--- 10 files changed, 30 insertions(+), 26 deletions(-) diff --git a/MindFlow/mindflow/cell/diffusion.py b/MindFlow/mindflow/cell/diffusion.py index 70914c080..57c618eee 100644 --- a/MindFlow/mindflow/cell/diffusion.py +++ b/MindFlow/mindflow/cell/diffusion.py @@ -1139,7 +1139,7 @@ class DiffusionTrainer: Args: original_samples (Tensor): The direct output from learned diffusion model. noise (Tensor): A current instance of a noise sample created by the diffusion process. - timesteps (float): The current discrete timestep in the diffusion chain. + timesteps (Tensor): The current discrete timestep in the diffusion chain. condition (Tensor): The condition for desired outputs. Default: ``None``. Returns: diff --git a/MindFlow/mindflow/core/optimizers.py b/MindFlow/mindflow/core/optimizers.py index e98ccc12f..22d12a0d7 100644 --- a/MindFlow/mindflow/core/optimizers.py +++ b/MindFlow/mindflow/core/optimizers.py @@ -58,7 +58,7 @@ class AdaHessian(nn.Adam): def gen_rand_vecs(self, grads): return [(2 * ops.randint(0, 2, p.shape) - 1).astype(ms.float32) for p in grads] - def modify_moments(self, grad_fn, inputs): + def _modify_moments(self, grad_fn, inputs): """ introduce Hutchinson trace by pre-adding its difference to grads' square into the second moment """ # generate the function for 2nd-order derivative @@ -104,7 +104,7 @@ class AdaHessian(nn.Adam): grad_fn (callable): the function that outputs 1st-order gradients inputs (Tensor): the inputs to the gradient function """ - gradients = self.modify_moments(grad_fn, inputs) + gradients = self._modify_moments(grad_fn, inputs) params = self._parameters moment1 = self.moment1 diff --git a/docs/api_python/mindflow/cell/mindflow.cell.AttentionBlock.rst b/docs/api_python/mindflow/cell/mindflow.cell.AttentionBlock.rst index ea17d3cb7..31db168b0 100644 --- a/docs/api_python/mindflow/cell/mindflow.cell.AttentionBlock.rst +++ b/docs/api_python/mindflow/cell/mindflow.cell.AttentionBlock.rst @@ -1,7 +1,7 @@ mindflow.cell.AttentionBlock ============================ -.. py:class:: mindflow.cell.AttentionBlock(in_channels, num_heads, drop_mode='dropout', dropout_rate='0.', compute_dtype=mstype.float32) +.. py:class:: mindflow.cell.AttentionBlock(in_channels, num_heads, drop_mode='dropout', dropout_rate='0.0', compute_dtype=mstype.float32) `AttentionBlock` 包含 `MultiHeadAttention` 和 `MLP` 网络堆叠而成。 @@ -13,7 +13,7 @@ mindflow.cell.AttentionBlock - **compute_dtype** (mindspore.dtype) - 网络层的数据类型。默认值: ``mstype.float32`` ,表示 ``mindspore.float32`` 。 输入: - - **input** (Tensor) - shape为 :math:`(batch\_size, sequence\_len, in\_channels)` 的Tensor。 + - **x** (Tensor) - shape为 :math:`(batch\_size, sequence\_len, in\_channels)` 的Tensor。 - **mask** (Tensor) - shape为 :math:`(batch\_size, sequence\_len, sequence\_len)` 或 :math:`(sequence\_len, sequence\_len)` 或 :math:`(batch\_size, num_heads, sequence\_len, sequence\_len)` 的Tensor. diff --git a/docs/api_python/mindflow/cell/mindflow.cell.DDIMScheduler.rst b/docs/api_python/mindflow/cell/mindflow.cell.DDIMScheduler.rst index e0297fb78..f36ee33d9 100644 --- a/docs/api_python/mindflow/cell/mindflow.cell.DDIMScheduler.rst +++ b/docs/api_python/mindflow/cell/mindflow.cell.DDIMScheduler.rst @@ -1,7 +1,7 @@ mindflow.cell.DDIMScheduler ============================ -.. py:class:: mindflow.cell.DDIMScheduler(num_train_timesteps=1000, beta_start=0.0001, beta_end=0.02, beta_schedule="squaredcos_cap_v2", prediction_type='epsilon', clip_sample=True, clip_sample_range=1.0, thresholding=False, sample_max_value=1., dynamic_thresholding_ratio=0.995, rescale_betas_zero_snr=False, timestep_spacing="leading", compute_dtype=mstype.float32) +.. py:class:: mindflow.cell.DDIMScheduler(num_train_timesteps: int = 1000, beta_start: float = 0.0001, beta_end: float = 0.02, beta_schedule: str = "squaredcos_cap_v2", prediction_type: str = 'epsilon', clip_sample: bool = True, clip_sample_range: float = 1.0, thresholding: bool = False, sample_max_value: float = 1.0, dynamic_thresholding_ratio: float = 0.995, rescale_betas_zero_snr: bool = False, timestep_spacing: str = "leading", compute_dtype=mstype.float32) `DDIMScheduler` 实现了去噪扩散概率模型DDIM中介绍的去噪过程。具体细节见 `Denoising Diffusion Implicit Models `_ 。 @@ -49,7 +49,7 @@ mindflow.cell.DDIMScheduler 参数: - **model_output** (Tensor) - 扩散模型预测的噪声。 - **sample** (Tensor) - 当前样本。 - - **timesteps** (Tensor) - 当前时间步。 + - **timestep** (Tensor) - 当前时间步。 - **eta** (float) - 去噪加入噪声的权重。必须满足 ``0 <= eta <=1`` 。默认值: ``0.0`` 。 - **use_clipped_model_output** (bool) - 如果为 ``True`` ,则根据裁剪的预测原始样本计算校正后的model_output。这是必要的,因为当 `self.scheduler.clip_sample`` 为 `True`` 时,预测的原始样本被裁剪到 `[-1,1]`。如果没有裁剪,校正的 `model_output` 将与作为输入提供的输出冲突, `use_cliped_model_output` 无效。默认值: ``False`` 。 diff --git a/docs/api_python/mindflow/cell/mindflow.cell.DDPMScheduler.rst b/docs/api_python/mindflow/cell/mindflow.cell.DDPMScheduler.rst index 81c1f6a52..e12deb707 100644 --- a/docs/api_python/mindflow/cell/mindflow.cell.DDPMScheduler.rst +++ b/docs/api_python/mindflow/cell/mindflow.cell.DDPMScheduler.rst @@ -1,7 +1,7 @@ mindflow.cell.DDPMScheduler ============================ -.. py:class:: mindflow.cell.DDPMScheduler(num_train_timesteps=1000, beta_start=0.0001, beta_end=0.02, beta_schedule="squaredcos_cap_v2", prediction_type='epsilon', variance_type='fixed_small_log', clip_sample=True, clip_sample_range=1.0, thresholding=False, sample_max_value=1., dynamic_thresholding_ratio=None, rescale_betas_zero_snr=False, timestep_spacing="leading", compute_dtype=mstype.float32) +.. py:class:: mindflow.cell.DDPMScheduler(num_train_timesteps: int = 1000, beta_start: float = 0.0001, beta_end: float = 0.02, beta_schedule: str = "squaredcos_cap_v2", prediction_type: str = 'epsilon', variance_type: str = 'fixed_small_log', clip_sample: bool = True, clip_sample_range: float = 1.0, thresholding: bool = False, sample_max_value: float = 1.0, dynamic_thresholding_ratio: float=0.995, rescale_betas_zero_snr: bool = False, timestep_spacing: str = "leading", compute_dtype=mstype.float32) `DDPMScheduler` 实现了去噪扩散概率模型DDPM中介绍的去噪过程。具体细节见 `Denoising Diffusion Probabilistic Models `_ 。 @@ -50,7 +50,7 @@ mindflow.cell.DDPMScheduler 参数: - **model_output** (Tensor) - 扩散模型预测的噪声。 - **sample** (Tensor) - 当前样本。 - - **timesteps** (Tensor) - 当前时间步。 + - **timestep** (Tensor) - 当前时间步。 - **predicted_variance** (Tensor) - 预测的方差。默认值: ``None`` 。 返回: diff --git a/docs/api_python/mindflow/cell/mindflow.cell.DiffusionTrainer.rst b/docs/api_python/mindflow/cell/mindflow.cell.DiffusionTrainer.rst index 15f17bfb4..4d30a2721 100644 --- a/docs/api_python/mindflow/cell/mindflow.cell.DiffusionTrainer.rst +++ b/docs/api_python/mindflow/cell/mindflow.cell.DiffusionTrainer.rst @@ -13,7 +13,10 @@ mindflow.cell.DiffusionTrainer - **p2_loss_weight_k** (float) - p2 loss权重 `k` ,具体信息查看 `Perception Prioritized Training of Diffusion Models `_ 。默认值: ``1`` 。 - **loss_type** (str) - loss函数类型。默认值: ``l1`` 。支持以下类型: ``l1`` 和 ``l2`` 。 - .. py:method:: get_loss(original_samples, noise, timesteps, condition=None) + 异常: + - **TypeError** - 如果 `scheduler` 不是 `DiffusionScheduler` 类型。 + + .. py:method:: get_loss(original_samples: Tensor, noise: Tensor, timesteps: Tensor, condition: Tensor = None) 计算扩散过程的前向loss。 diff --git a/docs/api_python/mindflow/cell/mindflow.cell.DiffusionTransformer.rst b/docs/api_python/mindflow/cell/mindflow.cell.DiffusionTransformer.rst index 9b02e831b..f07a19b14 100644 --- a/docs/api_python/mindflow/cell/mindflow.cell.DiffusionTransformer.rst +++ b/docs/api_python/mindflow/cell/mindflow.cell.DiffusionTransformer.rst @@ -1,19 +1,18 @@ mindflow.cell.DiffusionTransformer ================================================== -.. py:class:: mindflow.cell.DiffusionTransformer(in_channels, out_channels, cond_channels, hidden_channels, layers, heads, time_token_cond=True, compute_dtype=mstype.float32) +.. py:class:: mindflow.cell.DiffusionTransformer(in_channels, out_channels, hidden_channels, layers, heads, time_token_cond=True, compute_dtype=mstype.float32) 以Transformer作为骨干网络的扩散模型。 参数: - in_channels (int):输入特征维度。 - out_channels (int):输出特征维度。 - hidden_channels (int):隐藏层特征维度。 - layers (int): `Transformer` 层数。 - heads (int): 注意力头数。 - time_token_cond (bool):是否将时间作为条件token。 Default: ``True`` 。 - cond_as_token (bool):是否将条件作为token。 Default: ``True`` 。 - compute_dtype (mindspore.dtype):计算数据类型。支持 ``mstype.float32`` or ``mstype.float16`` 。 默认值: ``mstype.float32`` ,表示 ``mindspore.float32`` 。 + - **in_channels** (int) - 输入特征维度。 + - **out_channels** (int) - 输出特征维度。 + - **hidden_channels** (int) - 隐藏层特征维度。 + - **layers** (int) - `Transformer` 层数。 + - **heads** (int) - 注意力头数。 + - **time_token_cond** (bool) - 是否将时间作为作为条件token。 Default: ``True`` 。 + - **compute_dtype** (mindspore.dtype) 计算数据类型。支持 ``mstype.float32`` or ``mstype.float16`` 。 默认值: ``mstype.float32`` ,表示 ``mindspore.float32`` 。 输入: - **x** (Tensor) - 网络输入。shape为 :math:`(batch\_size, sequence\_len, in\_channels)` 的Tensor。 diff --git a/docs/api_python/mindflow/cell/mindflow.cell.MultiHeadAttention.rst b/docs/api_python/mindflow/cell/mindflow.cell.MultiHeadAttention.rst index dcfaf0183..0dcdd374d 100644 --- a/docs/api_python/mindflow/cell/mindflow.cell.MultiHeadAttention.rst +++ b/docs/api_python/mindflow/cell/mindflow.cell.MultiHeadAttention.rst @@ -1,14 +1,14 @@ mindflow.cell.MultiHeadAttention ================================= -.. py:class:: mindflow.cell.MultiHeadAttention(in_channels, num_heads, drop_mode='dropout', dropout_rate='0.', compute_dtype=mstype.float32) +.. py:class:: mindflow.cell.MultiHeadAttention(in_channels, num_heads, drop_mode='dropout', dropout_rate='0.0', compute_dtype=mstype.float32) 多头注意力机制,具体细节可以参见 `Attention Is All You Need `_ 。 参数: - **in_channels** (int) - 输入的输入特征维度。 - **num_heads** (int) - 输出的输出特征维度。 - - **drop_mode** (int) - dropout方式。默认值: ``dropout`` 。支持以下类型: ``dropout`` 和 ``droppath`` 。 + - **drop_mode** (str) - dropout方式。默认值: ``dropout`` 。支持以下类型: ``dropout`` 和 ``droppath`` 。 - **dropout_rate** (float) - dropout层丢弃的比率。取值在 `[0, 1]` 。默认值: ``0.0`` 。 - **compute_dtype** (mindspore.dtype) - 网络层的数据类型。默认值: ``mstype.float32`` ,表示 ``mindspore.float32`` 。 diff --git a/docs/api_python/mindflow/cell/mindflow.cell.UNet2D.rst b/docs/api_python/mindflow/cell/mindflow.cell.UNet2D.rst index ade0f67d6..cc8f068a0 100644 --- a/docs/api_python/mindflow/cell/mindflow.cell.UNet2D.rst +++ b/docs/api_python/mindflow/cell/mindflow.cell.UNet2D.rst @@ -1,7 +1,7 @@ mindflow.cell.UNet2D ========================= -.. py:class:: mindflow.cell.UNet2D(in_channels, out_channels, base_channels, data_format="NHWC", kernel_size=2, stride=2) +.. py:class:: mindflow.cell.UNet2D(in_channels, out_channels, base_channels, n_layers=4, data_format="NHWC", kernel_size=2, stride=2, activation="relu", enable_bn=True) 二维UNet模型。 UNet是一个呈U型的卷积神经网络。它由一个捕捉上下文的编码器和一个实现精确定位的解码器组成。 @@ -11,10 +11,13 @@ mindflow.cell.UNet2D - **in_channels** (int) - 输入的输入特征维度。 - **out_channels** (int) - 输出的输出特征维度。 - **base_channels** (int) - UNet的基础维度,以此维度为基础,UNet先成倍增加维度,后成倍减少维度。 + - **n_layers** (int) - 下采样和上采样卷积的层数。默认值: ``4``。 - **data_format** (str) - 输入数据的数据类型。默认值: ``"NHWC"``。 - **kernel_size** (int) - 卷积计算的卷积核大小。默认值: ``2``。 - **stride** (Union[int, tuple[int]]) - 卷积计算的stride大小。整型表示卷积核横向和纵向均移动相同的步长,元组型由两个整数组成,分别表示横向和纵向的卷积核移动步长。默认值: ``2``。 - + - **activation** (Union[str, class]) - 激活函数。默认值: ``"relu"``。 + - **enable_bn** (bool) - 指定是否在卷积中使用批量归一化。默认值: ``"True"``。 + 输入: - **x** (Tensor) - shape为 :math:`(batch\_size, feature\_size, image\_height, image\_width)` 的Tensor。 diff --git a/docs/api_python/mindflow/cell/mindflow.cell.ViT.rst b/docs/api_python/mindflow/cell/mindflow.cell.ViT.rst index bf34a0fed..09e4929ac 100644 --- a/docs/api_python/mindflow/cell/mindflow.cell.ViT.rst +++ b/docs/api_python/mindflow/cell/mindflow.cell.ViT.rst @@ -1,7 +1,7 @@ mindflow.cell.ViT ========================= -.. py:class:: mindflow.cell.ViT(image_size=(192, 384), in_channels=7, out_channels=3, patch_size=16, encoder_depths=12, encoder_embed_dim=768, encoder_num_heads=12, decoder_depths=8, decoder_embed_dim=512, decoder_num_heads=16, mlp_ratio=4, dropout_rate=1.0, compute_dtype=mstype.float16) +.. py:class:: mindflow.cell.ViT(image_size=(192, 384), in_channels=7, out_channels=3, patch_size=16, encoder_depths=12, encoder_embed_dim=768, encoder_num_heads=12, decoder_depths=8, decoder_embed_dim=512, decoder_num_heads=16, dropout_rate=0.0, compute_dtype=mstype.float16) 该模块基于ViT,包括encoder层、decoding_embedding层、decoder层和dense层。 @@ -16,8 +16,7 @@ mindflow.cell.ViT - **decoder_depths** (int) - decoder层的解码器深度。默认值: ``8``。 - **decoder_embed_dim** (int) - decoder层的解码器维度。默认值: ``512``。 - **decoder_num_heads** (int) - decoder层的head数。默认值: ``16``。 - - **mlp_ratio** (int) - mlp层的比例。默认值: ``4``。 - - **dropout_rate** (float) - dropout层的速率。默认值: ``1.0``。 + - **dropout_rate** (float) - dropout层的速率。默认值: ``0.0``。 - **compute_dtype** (dtype) - encoder层、decoding_embedding层、decoder层和dense层的数据类型。默认值: ``mstype.float16``。 输入: -- Gitee