# Công cụ tiện ích của Megatron-LM

## MegatronLMPlugin[[accelerate.utils.MegatronLMPlugin]]

#### accelerate.utils.MegatronLMPlugin[[accelerate.utils.MegatronLMPlugin]]

```python
accelerate.utils.MegatronLMPlugin(tp_degree: int = None, pp_degree: int = None, use_custom_fsdp: bool = None, overlap_cpu_optimizer_d2h_h2d: bool = None, no_load_optim: bool = None, eod_mask_loss: bool = None, no_save_optim: bool = None, optimizer_cpu_offload: bool = None, use_precision_aware_optimizer: bool = None, decoder_last_pipeline_num_layers: int = None, recompute_granularity: str = None, recompute_method: str = None, recompute_num_layers: int = None, attention_backend: bool = None, expert_model_parallel_size: int = None, context_parallel_size: int = None, attention_dropout: float = None, hidden_dropout: float = None, attention_softmax_in_fp32: bool = None, expert_tensor_parallel_size: int = None, calculate_per_token_loss: bool = None, use_rotary_position_embeddings: bool = None, num_micro_batches: int = None, gradient_clipping: float = None, sequence_parallelism: bool = None, recompute_activations: bool = None, use_distributed_optimizer: bool = None, pipeline_model_parallel_split_rank: int = None, num_layers_per_virtual_pipeline_stage: int = None, is_train_batch_min: str = True, train_iters: int = None, train_samples: int = None, weight_decay_incr_style: str = 'constant', start_weight_decay: float = None, end_weight_decay: float = None, lr_decay_style: str = 'linear', lr_decay_iters: int = None, lr_decay_samples: int = None, lr_warmup_iters: int = None, lr_warmup_samples: int = None, lr_warmup_fraction: float = None, min_lr: float = 0, consumed_samples: list = None, no_wd_decay_cond: typing.Callable | None = None, scale_lr_cond: typing.Callable | None = None, lr_mult: float = 1.0, megatron_dataset_flag: bool = False, seq_length: int = None, encoder_seq_length: int = None, decoder_seq_length: int = None, tensorboard_dir: str = None, set_all_logging_options: bool = False, eval_iters: int = 100, eval_interval: int = 1000, return_logits: bool = False, custom_train_step_class: typing.Any | None = None, custom_train_step_kwargs: dict[str, typing.Any] | None = None, custom_model_provider_function: typing.Callable | None = None, custom_prepare_model_function: typing.Callable | None = None, custom_megatron_datasets_provider_function: typing.Callable | None = None, custom_get_batch_function: typing.Callable | None = None, custom_loss_function: typing.Callable | None = None, other_megatron_args: dict[str, typing.Any] | None = None)
```

[Nguồn](https://github.com/huggingface/accelerate/blob/main/src/accelerate/utils/dataclasses.py#L2205)

**Tham số:**

tp_degree (`int`, mặc định là `None`) : Độ song song tensor.

pp_degree (`int`, mặc định là `None`) : Độ song song của pipeline.

num_micro_batches (`int`, mặc định là `None`) : Số lượng micro-batch.

gradient_clipping (`float`, mặc định là `None`) : Giá trị cắt gradient dựa trên chuẩn L2 toàn cục (0 để tắt).

sequence_parallelism (`bool`, mặc định là `None`) : Bật song song chuỗi.

recompute_activations (`bool`, mặc định là `None`) : Bật tính toán lại kích hoạt có chọn lọc.

use_distributed_optimizer (`bool`, mặc định là `None`) : Bật bộ tối ưu phân tán.

pipeline_model_parallel_split_rank (`int`, mặc định là `None`) : Rank nơi encoder và decoder nên được tách ra.

num_layers_per_virtual_pipeline_stage (`int`, mặc định là `None`) : Số lượng lớp cho mỗi giai đoạn pipeline ảo.

is_train_batch_min (`str`, mặc định là `True`) : Nếu cả tran & eval dataloaders đều được chỉ định, tham số này sẽ quyết định `micro_batch_size`.

train_iters (`int`, mặc định là `None`) : Tổng số mẫu để huấn luyện trên tất cả các lần chạy huấn luyện. Lưu ý rằng khi sử dụng `MegatronLMDummyScheduler`, bạn cần cung cấp train-iters hoặc train-samples.

train_samples (`int`, mặc định là `None`) : Tổng số mẫu để huấn luyện trên tất cả các lần chạy huấn luyện. Lưu ý rằng khi sử dụng `MegatronLMDummyScheduler`, bạn cần cung cấp train-iters hoặc train-samples.

weight_decay_incr_style (`str`, mặc định là `'constant'`) : Hàm tăng dần hệ số suy giảm trọng số. Các lựa chọn=["constant", "linear", "cosine"].

start_weight_decay (`float`, mặc định là `None`) : Hệ số suy giảm trọng số ban đầu cho quy tắc hóa L2.

end_weight_decay (`float`, mặc định là `None`) : Hệ số suy giảm trọng số cuối của quá trình chạy cho quy tắc hóa L2.

lr_decay_style (`str`, mặc định là `'linear'`) : Hàm suy giảm tốc độ học. choices=['constant', 'linear', 'cosine'].

lr_decay_iters (`int`, mặc định là `None`) : Số lần lặp cho việc suy giảm tốc độ học. Nếu là None, mặc định sẽ là `train_iters`.

lr_decay_samples (`int`, mặc định là `None`) : Số lượng mẫu cho việc suy giảm tốc độ học. Nếu là None, mặc định sẽ là `train_samples`.

lr_warmup_iters (`int`, mặc định là `None`) : Số lần lặp để tăng tuyến tính tốc độ học (learning rate) trong giai đoạn khởi động (warmup).

lr_warmup_samples (`int`, mặc định là `None`) : Số lượng mẫu để làm nóng tuyến tính tốc độ học.

lr_warmup_fraction (`float`, mặc định là `None`) : Tỉ lệ của lr-warmup-(iters/samples) để làm nóng dần (warmup) tuyến tính tốc độ học (learning rate).

min_lr (`float`, mặc định là `0`) : Giá trị tối thiểu cho learning rate. Scheduler sẽ cắt các giá trị thấp hơn ngưỡng này.

consumed_samples (`List`, mặc định là `None`) : Số lượng mẫu được tiêu thụ theo cùng thứ tự với các dataloader trong lời gọi `accelerator.prepare`.

no_wd_decay_cond (`Optional`, defaults to `None`) : Condition to disable weight decay.

scale_lr_cond (`Optional`, mặc định là `None`) : Điều kiện để điều chỉnh tốc độ học.

lr_mult (`float`, mặc định là `1.0`) : Hệ số nhân tốc độ học.

megatron_dataset_flag (`bool`, mặc định là `False`) : Xác định xem định dạng của tập dữ liệu có tuân theo định dạng Indexed/Cached/MemoryMapped của Megatron-LM hay không.

seq_length (`int`, mặc định là `None`) : Độ dài tối đa của chuỗi cần xử lý.

encoder_seq_length (`int`, mặc định là `None`) : Độ dài tối đa của chuỗi để xử lý cho bộ mã hóa.

decoder_seq_length (`int`, mặc định là `None`) : Độ dài tối đa của chuỗi để xử lý cho bộ giải mã.

tensorboard_dir (`str`, mặc định là `None`) : Đường dẫn để lưu các log tensorboard.

set_all_logging_options (`bool`, mặc định là `False`) : Xác định có thiết lập tất cả các tùy chọn ghi log hay không.

eval_iters (`int`, mặc định là `100`) : Số lần lặp chạy cho đánh giá kiểm tra/xác thực.

eval_interval (`int`, mặc định là `1000`) : Khoảng thời gian giữa các lần chạy đánh giá trên tập kiểm tra.

return_logits (`bool`, mặc định là `False`) : Có trả về logits từ mô hình hay không.

custom_train_step_class (`Optional`, mặc định là `None`) : Lớp bước huấn luyện tùy chỉnh.

custom_train_step_kwargs (`Optional`, defaults to `None`) : Custom train step kwargs.

custom_model_provider_function (`Optional`, mặc định là `None`) : Hàm cung cấp model tùy chỉnh.

custom_prepare_model_function (`Optional`, mặc định là `None`) : Hàm chuẩn bị mô hình tùy chỉnh.

custom_megatron_datasets_provider_function (`Optional`, defaults to `None`) : Custom megatron train_valid_test datasets provider function.

custom_get_batch_function (`Optional`, mặc định là `None`) : Hàm lấy batch tùy chỉnh.

custom_loss_function (`Optional`, defaults to `None`) : Custom loss function.

other_megatron_args (`Optional`, mặc định là `None`) : Các đối số Megatron-LM khác. Vui lòng tham khảo Megatron-LM.

Plugin cho Megatron-LM để kích hoạt song song hóa tensor, pipeline, sequence và dữ liệu. Đồng thời kích hoạt tính toán lại kích hoạt có chọn lọc và các kernel hợp nhất được tối ưu hóa.

## MegatronLMDummyScheduler[[accelerate.utils.MegatronLMDummyScheduler]]

#### accelerate.utils.MegatronLMDummyScheduler[[accelerate.utils.MegatronLMDummyScheduler]]

```python
accelerate.utils.MegatronLMDummyScheduler(optimizer, total_num_steps = None, warmup_num_steps = 0, **kwargs)
```

[Nguồn](https://github.com/huggingface/accelerate/blob/main/src/accelerate/utils/megatron_lm.py#L378)

**Tham số:**

optimizer (`torch.optim.optimizer.Optimizer`) : Bộ tối ưu cần bọc.

total_num_steps (int) : Tổng số bước.

warmup_num_steps (int) : Số bước cho quá trình khởi động.

- ****kwargs** (các đối số từ khóa bổ sung, *tùy chọn*) : Các đối số khác.

Dummy scheduler cung cấp các tham số mô hình hoặc các nhóm tham số, chủ yếu được sử dụng để tuân theo vòng lặp huấn luyện thông thường khi cấu hình scheduler được chỉ định trong tệp cấu hình deepspeed.

## MegatronLMDummyDataLoader[[accelerate.utils.MegatronLMDummyDataLoader]]

#### accelerate.utils.MegatronLMDummyDataLoader[[accelerate.utils.MegatronLMDummyDataLoader]]

```python
accelerate.utils.MegatronLMDummyDataLoader(**dataset_kwargs)
```

[Nguồn](https://github.com/huggingface/accelerate/blob/main/src/accelerate/utils/megatron_lm.py#L162)

**Tham số:**

- ****dataset_kwargs** : Các đối số dữ liệu của Megatron.

Dataloader giả lập cung cấp các tham số mô hình hoặc các nhóm tham số, chủ yếu được sử dụng để tuân theo quy trình huấn luyện thông thường

## AbstractTrainStep[[accelerate.utils.AbstractTrainStep]]

#### accelerate.utils.AbstractTrainStep[[accelerate.utils.AbstractTrainStep]]

```python
accelerate.utils.AbstractTrainStep(name)
```

[Nguồn](https://github.com/huggingface/accelerate/blob/main/src/accelerate/utils/megatron_lm.py#L415)

Lớp trừu tượng cho việc nhóm dữ liệu, truyền tiến và xử lý hàm mất mát.

## GPTTrainStep[[accelerate.utils.GPTTrainStep]]

#### accelerate.utils.GPTTrainStep[[accelerate.utils.GPTTrainStep]]

```python
accelerate.utils.GPTTrainStep(accelerator, args)
```

[Nguồn](https://github.com/huggingface/accelerate/blob/main/src/accelerate/utils/megatron_lm.py#L574)

**Tham số:**

args (`argparse.Namespace`) : Các đối số của Megatron-LM.

Lớp bước huấn luyện GPT.

## BertTrainStep[[accelerate.utils.BertTrainStep]]

#### accelerate.utils.BertTrainStep[[accelerate.utils.BertTrainStep]]

```python
accelerate.utils.BertTrainStep(accelerator, args)
```

[Nguồn](https://github.com/huggingface/accelerate/blob/main/src/accelerate/utils/megatron_lm.py#L432)

**Tham số:**

args (`argparse.Namespace`) : Các đối số của Megatron-LM.

Lớp bước huấn luyện Bert.

## T5TrainStep[[accelerate.utils.T5TrainStep]]

#### accelerate.utils.T5TrainStep[[accelerate.utils.T5TrainStep]]

```python
accelerate.utils.T5TrainStep(accelerator, args)
```

[Nguồn](https://github.com/huggingface/accelerate/blob/main/src/accelerate/utils/megatron_lm.py#L718)

**Tham số:**

args (`argparse.Namespace`) : Các đối số của Megatron-LM.

Lớp bước huấn luyện T5.

## avg_losses_across_data_parallel_group[[accelerate.utils.avg_losses_across_data_parallel_group]]

#### accelerate.utils.avg_losses_across_data_parallel_group[[accelerate.utils.avg_losses_across_data_parallel_group]]

```python
accelerate.utils.avg_losses_across_data_parallel_group(losses)
```

[Nguồn](https://github.com/huggingface/accelerate/blob/main/src/accelerate/utils/megatron_lm.py#L1217)

**Tham số:**

losses (List[Tensor]) : Danh sách các giá trị loss để lấy trung bình trên nhóm data parallel.

Mất trung bình trên nhóm song song dữ liệu.

