PyTorch Bias Dropout Add Fused Kernel (jit/bias_dropout_add.py)

ColossalAI · colossalai/kernel/jit/bias_dropout_add.py · python · Apache-2.0
ai, lang:python, fw:pytorch, tag:optimization

PyTorch JIT-scripted fused operations combining bias, dropout, and residual addition for neural networks. Reuse this to optimize model training and inference performance.

# Source: https://github.com/hpcaitech/ColossalAI | License: Apache-2.0 | Copyright 2021- HPC-AI Technology Inc. All rights reserved
# Provided via justclaw.it.com; keep this notice with the code.
import torch


def bias_dropout_add(x, bias, residual, prob, training):
    # type: (Tensor, Tensor, Tensor, float, bool) -> Tensor
    out = torch.nn.functional.dropout(x + bias, p=prob, training=training)
    out = residual + out
    return out


@torch.jit.script
def bias_dropout_add_fused_train(
    x: torch.Tensor, bias: torch.Tensor, residual: torch.Tensor, prob: float
) -> torch.Tensor:
    return bias_dropout_add(x, bias, residual, prob, True)


@torch.jit.script
def bias_dropout_add_fused_inference(
    x: torch.Tensor, bias: torch.Tensor, residual: torch.Tensor, prob: float
) -> torch.T

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key