# tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks API Source: https://www.tensorplay.cn/docs/api/tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.html ## Functions 5 [#](#api-tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.allreduce_hook) ### allreduce_hook function[Full reference ↗](/docs/generated/tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.allreduce_hook.html) ```python tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.allreduce_hook(process_group, bucket: GradBucket) ``` Call allreduce using GradBucket tensors. Once gradient tensors are aggregated across all workers, its then callback takes the mean and returns the result. If user registers this DDP communication hook, DDP results is expected to be same as the case where no hook was registered. Hence, this won’t change behavior of DDP and user can use this as a reference or modify this hook to log useful information or any other purposes while unaffecting DDP behavior. Example:: ``` >>> # xdoctest: +SKIP >>> ddp_model.register_comm_hook(process_group, allreduce_hook) ``` [#](#api-tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.bf16_compress_hook) ### bf16_compress_hook function[Full reference ↗](/docs/generated/tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.bf16_compress_hook.html) ```python tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.bf16_compress_hook(process_group, bucket) ``` Warning: This API is experimental, and it requires NCCL version later than 2.9.6. This DDP communication hook implements a simple gradient compression approach that casts GradBucket tensor to half-precision [Brain floating point format](https://en.wikipedia.org/wiki/Bfloat16_floating-point_format) (tensorplay.bfloat16) and then divides it by the process group size. It allreduces those bfloat16 gradient tensors. Once compressed gradient tensors are allreduced, the chained callback decompress casts it back to the input data type (such as float32). Example:: ``` >>> # xdoctest: +SKIP >>> ddp_model.register_comm_hook(process_group, bf16_compress_hook) ``` [#](#api-tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.bf16_compress_wrapper) ### bf16_compress_wrapper function[Full reference ↗](/docs/generated/tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.bf16_compress_wrapper.html) ```python tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.bf16_compress_wrapper(hook: Callable[[Any], Any]) ``` Warning: This API is experimental, and it requires NCCL version later than 2.9.6. This wrapper casts the input gradient tensor of a given DDP communication hook to half-precision [Brain floating point format](https://en.wikipedia.org/wiki/Bfloat16_floating-point_format) (bfloat16) and casts the resulting tensor of the given hook back to the input data type. Therefore, bf16_compress_hook is equivalent to bf16_compress_wrapper(allreduce_hook). Example:: ``` >>> # xdoctest: +SKIP >>> state = PowerSGDState(process_group=process_group, matrix_approximation_rank=1, start_powerSGD_iter=10) >>> ddp_model.register_comm_hook(state, bf16_compress_wrapper(powerSGD_hook)) ``` [#](#api-tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.fp16_compress_hook) ### fp16_compress_hook function[Full reference ↗](/docs/generated/tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.fp16_compress_hook.html) ```python tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.fp16_compress_hook(process_group, bucket) ``` Compress by casting GradBucket to float16 divided by process group size. This DDP communication hook implements a simple gradient compression approach that casts GradBucket tensor to half-precision floating-point format (tensorplay.float16) and then divides it by the process group size. It allreduces those float16 gradient tensors. Once compressed gradient tensors are allreduced, the chained callback decompress casts it back to the input data type (such as float32). Example:: ``` >>> # xdoctest: +SKIP >>> ddp_model.register_comm_hook(process_group, fp16_compress_hook) ``` [#](#api-tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.fp16_compress_wrapper) ### fp16_compress_wrapper function[Full reference ↗](/docs/generated/tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.fp16_compress_wrapper.html) ```python tensorplay.distributed.algorithms.ddp_comm_hooks.default_hooks.fp16_compress_wrapper(hook: Callable[[Any], Any]) ``` Cast input tensor to float16, cast result of hook back to input dtype. This wrapper casts the input gradient tensor of a given DDP communication hook to half-precision floating point format (float16), and casts the resulting tensor of the given hook back to the input data type, such as float32. Therefore, fp16_compress_hook is equivalent to fp16_compress_wrapper(allreduce_hook). Example:: ``` >>> # xdoctest: +SKIP >>> state = PowerSGDState(process_group=process_group, matrix_approximation_rank=1, start_powerSGD_iter=10) >>> ddp_model.register_comm_hook(state, fp16_compress_wrapper(powerSGD_hook)) ```