entry_point
stringlengths
1
65
original_triton_python_code
stringlengths
208
619k
optimised_triton_code
stringlengths
1.15k
275k
repo_name
stringlengths
7
115
module_name
stringlengths
1
65
synthetic
bool
1 class
uuid
int64
0
18.5k
licenses
listlengths
1
6
stars
int64
0
19.8k
sha
stringlengths
40
40
repo_link
stringlengths
72
180
MaxPoolStride1
import torch from torch import nn import torch.nn.functional as F import torch.utils.data class MaxPoolStride1(nn.Module): def __init__(self, kernel_size): super(MaxPoolStride1, self).__init__() self.kernel_size = kernel_size self.pad = kernel_size - 1 def forward(self, x): padding = int(self.pad / 2) padded_x = F.pad(x, (padding, padding, padding, padding), mode= 'constant', value=0) pooled_x = nn.MaxPool2d(self.kernel_size, 1)(padded_x) return pooled_x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'kernel_size': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_max_pool2d_with_indices_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 144 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 3 % 3 x0 = xindex % 3 x2 = xindex // 9 x4 = xindex tmp0 = -1 + x1 tmp1 = tl.full([1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = -1 + x0 tmp6 = tmp5 >= tmp1 tmp7 = tmp5 < tmp3 tmp8 = tmp2 & tmp4 tmp9 = tmp8 & tmp6 tmp10 = tmp9 & tmp7 tmp11 = tl.load(in_ptr0 + (-5 + x0 + 4 * x1 + 16 * x2), tmp10 & xmask, other=0.0) tmp12 = x0 tmp13 = tmp12 >= tmp1 tmp14 = tmp12 < tmp3 tmp15 = tmp8 & tmp13 tmp16 = tmp15 & tmp14 tmp17 = tl.load(in_ptr0 + (-4 + x0 + 4 * x1 + 16 * x2), tmp16 & xmask, other=0.0) tmp18 = triton_helpers.maximum(tmp17, tmp11) tmp19 = 1 + x0 tmp20 = tmp19 >= tmp1 tmp21 = tmp19 < tmp3 tmp22 = tmp8 & tmp20 tmp23 = tmp22 & tmp21 tmp24 = tl.load(in_ptr0 + (-3 + x0 + 4 * x1 + 16 * x2), tmp23 & xmask, other=0.0) tmp25 = triton_helpers.maximum(tmp24, tmp18) tmp26 = 2 + x0 tmp27 = tmp26 >= tmp1 tmp28 = tmp26 < tmp3 tmp29 = tmp8 & tmp27 tmp30 = tmp29 & tmp28 tmp31 = tl.load(in_ptr0 + (-2 + x0 + 4 * x1 + 16 * x2), tmp30 & xmask, other=0.0) tmp32 = triton_helpers.maximum(tmp31, tmp25) tmp33 = x1 tmp34 = tmp33 >= tmp1 tmp35 = tmp33 < tmp3 tmp36 = tmp34 & tmp35 tmp37 = tmp36 & tmp6 tmp38 = tmp37 & tmp7 tmp39 = tl.load(in_ptr0 + (-1 + x0 + 4 * x1 + 16 * x2), tmp38 & xmask, other=0.0) tmp40 = triton_helpers.maximum(tmp39, tmp32) tmp41 = tmp36 & tmp13 tmp42 = tmp41 & tmp14 tmp43 = tl.load(in_ptr0 + (x0 + 4 * x1 + 16 * x2), tmp42 & xmask, other=0.0 ) tmp44 = triton_helpers.maximum(tmp43, tmp40) tmp45 = tmp36 & tmp20 tmp46 = tmp45 & tmp21 tmp47 = tl.load(in_ptr0 + (1 + x0 + 4 * x1 + 16 * x2), tmp46 & xmask, other=0.0) tmp48 = triton_helpers.maximum(tmp47, tmp44) tmp49 = tmp36 & tmp27 tmp50 = tmp49 & tmp28 tmp51 = tl.load(in_ptr0 + (2 + x0 + 4 * x1 + 16 * x2), tmp50 & xmask, other=0.0) tmp52 = triton_helpers.maximum(tmp51, tmp48) tmp53 = 1 + x1 tmp54 = tmp53 >= tmp1 tmp55 = tmp53 < tmp3 tmp56 = tmp54 & tmp55 tmp57 = tmp56 & tmp6 tmp58 = tmp57 & tmp7 tmp59 = tl.load(in_ptr0 + (3 + x0 + 4 * x1 + 16 * x2), tmp58 & xmask, other=0.0) tmp60 = triton_helpers.maximum(tmp59, tmp52) tmp61 = tmp56 & tmp13 tmp62 = tmp61 & tmp14 tmp63 = tl.load(in_ptr0 + (4 + x0 + 4 * x1 + 16 * x2), tmp62 & xmask, other=0.0) tmp64 = triton_helpers.maximum(tmp63, tmp60) tmp65 = tmp56 & tmp20 tmp66 = tmp65 & tmp21 tmp67 = tl.load(in_ptr0 + (5 + x0 + 4 * x1 + 16 * x2), tmp66 & xmask, other=0.0) tmp68 = triton_helpers.maximum(tmp67, tmp64) tmp69 = tmp56 & tmp27 tmp70 = tmp69 & tmp28 tmp71 = tl.load(in_ptr0 + (6 + x0 + 4 * x1 + 16 * x2), tmp70 & xmask, other=0.0) tmp72 = triton_helpers.maximum(tmp71, tmp68) tmp73 = 2 + x1 tmp74 = tmp73 >= tmp1 tmp75 = tmp73 < tmp3 tmp76 = tmp74 & tmp75 tmp77 = tmp76 & tmp6 tmp78 = tmp77 & tmp7 tmp79 = tl.load(in_ptr0 + (7 + x0 + 4 * x1 + 16 * x2), tmp78 & xmask, other=0.0) tmp80 = triton_helpers.maximum(tmp79, tmp72) tmp81 = tmp76 & tmp13 tmp82 = tmp81 & tmp14 tmp83 = tl.load(in_ptr0 + (8 + x0 + 4 * x1 + 16 * x2), tmp82 & xmask, other=0.0) tmp84 = triton_helpers.maximum(tmp83, tmp80) tmp85 = tmp76 & tmp20 tmp86 = tmp85 & tmp21 tmp87 = tl.load(in_ptr0 + (9 + x0 + 4 * x1 + 16 * x2), tmp86 & xmask, other=0.0) tmp88 = triton_helpers.maximum(tmp87, tmp84) tmp89 = tmp76 & tmp27 tmp90 = tmp89 & tmp28 tmp91 = tl.load(in_ptr0 + (10 + x0 + 4 * x1 + 16 * x2), tmp90 & xmask, other=0.0) tmp92 = triton_helpers.maximum(tmp91, tmp88) tl.store(out_ptr0 + x4, tmp92, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 3, 3), (36, 9, 3, 1), torch.float32) get_raw_stream(0) triton_poi_fused_max_pool2d_with_indices_0[grid(144)](arg0_1, buf0, 144, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf0, class MaxPoolStride1New(nn.Module): def __init__(self, kernel_size): super(MaxPoolStride1New, self).__init__() self.kernel_size = kernel_size self.pad = kernel_size - 1 def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
Dazz993/AlphaPose
MaxPoolStride1
false
5,049
[ "Apache-2.0" ]
1
d4b9a3af5f590fa21bd033b4a19e98b5748ae683
https://github.com/Dazz993/AlphaPose/tree/d4b9a3af5f590fa21bd033b4a19e98b5748ae683
RSoftmax
import torch import torch.nn as nn import torch.nn.functional as F class RSoftmax(nn.Module): """Radix Softmax module in ``SplitAttentionConv2d``. Args: radix (int): Radix of input. groups (int): Groups of input. """ def __init__(self, radix, groups): super().__init__() self.radix = radix self.groups = groups def forward(self, x): batch = x.size(0) if self.radix > 1: x = x.view(batch, self.groups, self.radix, -1).transpose(1, 2) x = F.softmax(x, dim=1) x = x.reshape(batch, -1) else: x = torch.sigmoid(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'radix': 4, 'groups': 1}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x3, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x3, tmp8, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 1, 16), (64, 16, 256, 1), torch. float32) get_raw_stream(0) triton_poi_fused__softmax_0[grid(256)](arg0_1, buf0, 256, XBLOCK= 256, num_warps=4, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((4, 4, 1, 16), (64, 16, 16, 1), torch.float32 ) triton_poi_fused__softmax_1[grid(256)](buf0, buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf0 return reinterpret_tensor(buf1, (4, 64), (64, 1), 0), class RSoftmaxNew(nn.Module): """Radix Softmax module in ``SplitAttentionConv2d``. Args: radix (int): Radix of input. groups (int): Groups of input. """ def __init__(self, radix, groups): super().__init__() self.radix = radix self.groups = groups def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
David-19940718/mmclassification
RSoftmax
false
5,050
[ "Apache-2.0" ]
1
987dd45457e38c4787237ea468799849dce11ada
https://github.com/David-19940718/mmclassification/tree/987dd45457e38c4787237ea468799849dce11ada
ConvRelu
import torch import torch.utils.data import torch.nn as nn import torch.onnx import torch.autograd import torch.backends.cudnn class ConvRelu(nn.Module): """3x3 convolution followed by ReLU activation building block.""" def __init__(self, num_in, num_out): super().__init__() self.block = nn.Conv2d(num_in, num_out, kernel_size=3, padding=1, bias=False) def forward(self, x): return nn.functional.relu(self.block(x), inplace=True) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_in': 4, 'num_out': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.utils.data import torch.nn as nn import torch.onnx import torch.autograd import torch.backends.cudnn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = tl.full([1], 0, tl.int32) tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp3 = 0.0 tmp4 = tmp2 <= tmp3 tl.store(in_out_ptr0 + x0, tmp2, xmask) tl.store(out_ptr0 + x0, tmp4, xmask) def call(args): primals_1, primals_2 = args args.clear() assert_size_stride(primals_1, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_2, primals_1, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1)) buf1 = buf0 del buf0 buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(256)](buf1, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) return buf1, primals_1, primals_2, buf2 class ConvReluNew(nn.Module): """3x3 convolution followed by ReLU activation building block.""" def __init__(self, num_in, num_out): super().__init__() self.block = nn.Conv2d(num_in, num_out, kernel_size=3, padding=1, bias=False) def forward(self, input_0): primals_1 = self.block.weight primals_2 = input_0 output = call([primals_1, primals_2]) return output[0]
CorentinLemaitre/robosat.pink
ConvRelu
false
5,051
[ "MIT" ]
1
6ec29a4dd4c0cbf953e73818d7338ee68b2451d3
https://github.com/CorentinLemaitre/robosat.pink/tree/6ec29a4dd4c0cbf953e73818d7338ee68b2451d3
FocalLoss
import torch import torch.nn as nn import torch.nn.functional as F def reduce_loss(loss, reduction): """Reduce loss as specified. Args: loss (Tensor): Elementwise loss tensor. reduction (str): Options are "none", "mean" and "sum". Return: Tensor: Reduced loss tensor. """ reduction_enum = F._Reduction.get_enum(reduction) if reduction_enum == 0: return loss elif reduction_enum == 1: return loss.mean() elif reduction_enum == 2: return loss.sum() def weight_reduce_loss(loss, weight=None, reduction='mean', avg_factor=None): """Apply element-wise weight and reduce loss. Args: loss (Tensor): Element-wise loss. weight (Tensor): Element-wise weights. reduction (str): Same as built-in losses of PyTorch. avg_factor (float): Average factor when computing the mean of losses. Returns: Tensor: Processed loss values. """ if weight is not None: loss = loss * weight if avg_factor is None: loss = reduce_loss(loss, reduction) elif reduction == 'mean': loss = loss.sum() / avg_factor elif reduction != 'none': raise ValueError('avg_factor can not be used with reduction="sum"') return loss def convert_to_one_hot(targets: 'torch.Tensor', classes) ->torch.Tensor: """This function converts target class indices to one-hot vectors, given the number of classes. Args: targets (Tensor): The ground truth label of the prediction with shape (N, 1) classes (int): the number of classes. Returns: Tensor: Processed loss values. """ assert torch.max(targets).item( ) < classes, 'Class Index must be less than number of classes' one_hot_targets = torch.zeros((targets.shape[0], classes), dtype=torch. long, device=targets.device) one_hot_targets.scatter_(1, targets.long(), 1) return one_hot_targets def sigmoid_focal_loss(pred, target, weight=None, gamma=2.0, alpha=0.25, reduction='mean', avg_factor=None): """Sigmoid focal loss. Args: pred (torch.Tensor): The prediction with shape (N, \\*). target (torch.Tensor): The ground truth label of the prediction with shape (N, \\*). weight (torch.Tensor, optional): Sample-wise loss weight with shape (N, ). Defaults to None. gamma (float): The gamma for calculating the modulating factor. Defaults to 2.0. alpha (float): A balanced form for Focal Loss. Defaults to 0.25. reduction (str): The method used to reduce the loss. Options are "none", "mean" and "sum". If reduction is 'none' , loss is same shape as pred and label. Defaults to 'mean'. avg_factor (int, optional): Average factor that is used to average the loss. Defaults to None. Returns: torch.Tensor: Loss. """ assert pred.shape == target.shape, 'pred and target should be in the same shape.' pred_sigmoid = pred.sigmoid() target = target.type_as(pred) pt = (1 - pred_sigmoid) * target + pred_sigmoid * (1 - target) focal_weight = (alpha * target + (1 - alpha) * (1 - target)) * pt.pow(gamma ) loss = F.binary_cross_entropy_with_logits(pred, target, reduction='none' ) * focal_weight if weight is not None: assert weight.dim() == 1 weight = weight.float() if pred.dim() > 1: weight = weight.reshape(-1, 1) loss = weight_reduce_loss(loss, weight, reduction, avg_factor) return loss class FocalLoss(nn.Module): """Focal loss. Args: gamma (float): Focusing parameter in focal loss. Defaults to 2.0. alpha (float): The parameter in balanced form of focal loss. Defaults to 0.25. reduction (str): The method used to reduce the loss into a scalar. Options are "none" and "mean". Defaults to 'mean'. loss_weight (float): Weight of loss. Defaults to 1.0. """ def __init__(self, gamma=2.0, alpha=0.25, reduction='mean', loss_weight=1.0 ): super(FocalLoss, self).__init__() self.gamma = gamma self.alpha = alpha self.reduction = reduction self.loss_weight = loss_weight def forward(self, pred, target, weight=None, avg_factor=None, reduction_override=None): """Sigmoid focal loss. Args: pred (torch.Tensor): The prediction with shape (N, \\*). target (torch.Tensor): The ground truth label of the prediction with shape (N, \\*), N or (N,1). weight (torch.Tensor, optional): Sample-wise loss weight with shape (N, \\*). Defaults to None. avg_factor (int, optional): Average factor that is used to average the loss. Defaults to None. reduction_override (str, optional): The method used to reduce the loss into a scalar. Options are "none", "mean" and "sum". Defaults to None. Returns: torch.Tensor: Loss. """ assert reduction_override in (None, 'none', 'mean', 'sum') reduction = (reduction_override if reduction_override else self. reduction) if target.dim() == 1 or target.dim() == 2 and target.shape[1] == 1: target = convert_to_one_hot(target.view(-1, 1), pred.shape[-1]) loss_cls = self.loss_weight * sigmoid_focal_loss(pred, target, weight, gamma=self.gamma, alpha=self.alpha, reduction=reduction, avg_factor=avg_factor) return loss_cls def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn import torch.nn.functional as F assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_binary_cross_entropy_with_logits_mean_mul_pow_rsub_sigmoid_0( in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp3 = tl.load(in_ptr1 + r0, None) tmp1 = 1.0 tmp2 = tmp1 - tmp0 tmp4 = tmp2 * tmp3 tmp5 = 0.0 tmp6 = triton_helpers.minimum(tmp5, tmp3) tmp7 = tl_math.abs(tmp3) tmp8 = -tmp7 tmp9 = tl_math.exp(tmp8) tmp10 = libdevice.log1p(tmp9) tmp11 = tmp6 - tmp10 tmp12 = tmp4 - tmp11 tmp13 = 0.25 tmp14 = tmp0 * tmp13 tmp15 = 0.75 tmp16 = tmp2 * tmp15 tmp17 = tmp14 + tmp16 tmp18 = tl.sigmoid(tmp3) tmp19 = tmp1 - tmp18 tmp20 = tmp19 * tmp0 tmp21 = tmp18 * tmp2 tmp22 = tmp20 + tmp21 tmp23 = tmp22 * tmp22 tmp24 = tmp17 * tmp23 tmp25 = tmp12 * tmp24 tmp26 = tl.broadcast_to(tmp25, [RBLOCK]) tmp28 = triton_helpers.promote_to_tensor(tl.sum(tmp26, 0)) tmp29 = 256.0 tmp30 = tmp28 / tmp29 tmp31 = tmp30 * tmp1 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp31, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_binary_cross_entropy_with_logits_mean_mul_pow_rsub_sigmoid_0[ grid(1)](buf1, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, def reduce_loss(loss, reduction): """Reduce loss as specified. Args: loss (Tensor): Elementwise loss tensor. reduction (str): Options are "none", "mean" and "sum". Return: Tensor: Reduced loss tensor. """ reduction_enum = F._Reduction.get_enum(reduction) if reduction_enum == 0: return loss elif reduction_enum == 1: return loss.mean() elif reduction_enum == 2: return loss.sum() def weight_reduce_loss(loss, weight=None, reduction='mean', avg_factor=None): """Apply element-wise weight and reduce loss. Args: loss (Tensor): Element-wise loss. weight (Tensor): Element-wise weights. reduction (str): Same as built-in losses of PyTorch. avg_factor (float): Average factor when computing the mean of losses. Returns: Tensor: Processed loss values. """ if weight is not None: loss = loss * weight if avg_factor is None: loss = reduce_loss(loss, reduction) elif reduction == 'mean': loss = loss.sum() / avg_factor elif reduction != 'none': raise ValueError('avg_factor can not be used with reduction="sum"') return loss def convert_to_one_hot(targets: 'torch.Tensor', classes) ->torch.Tensor: """This function converts target class indices to one-hot vectors, given the number of classes. Args: targets (Tensor): The ground truth label of the prediction with shape (N, 1) classes (int): the number of classes. Returns: Tensor: Processed loss values. """ assert torch.max(targets).item( ) < classes, 'Class Index must be less than number of classes' one_hot_targets = torch.zeros((targets.shape[0], classes), dtype=torch. long, device=targets.device) one_hot_targets.scatter_(1, targets.long(), 1) return one_hot_targets def sigmoid_focal_loss(pred, target, weight=None, gamma=2.0, alpha=0.25, reduction='mean', avg_factor=None): """Sigmoid focal loss. Args: pred (torch.Tensor): The prediction with shape (N, \\*). target (torch.Tensor): The ground truth label of the prediction with shape (N, \\*). weight (torch.Tensor, optional): Sample-wise loss weight with shape (N, ). Defaults to None. gamma (float): The gamma for calculating the modulating factor. Defaults to 2.0. alpha (float): A balanced form for Focal Loss. Defaults to 0.25. reduction (str): The method used to reduce the loss. Options are "none", "mean" and "sum". If reduction is 'none' , loss is same shape as pred and label. Defaults to 'mean'. avg_factor (int, optional): Average factor that is used to average the loss. Defaults to None. Returns: torch.Tensor: Loss. """ assert pred.shape == target.shape, 'pred and target should be in the same shape.' pred_sigmoid = pred.sigmoid() target = target.type_as(pred) pt = (1 - pred_sigmoid) * target + pred_sigmoid * (1 - target) focal_weight = (alpha * target + (1 - alpha) * (1 - target)) * pt.pow(gamma ) loss = F.binary_cross_entropy_with_logits(pred, target, reduction='none' ) * focal_weight if weight is not None: assert weight.dim() == 1 weight = weight.float() if pred.dim() > 1: weight = weight.reshape(-1, 1) loss = weight_reduce_loss(loss, weight, reduction, avg_factor) return loss class FocalLossNew(nn.Module): """Focal loss. Args: gamma (float): Focusing parameter in focal loss. Defaults to 2.0. alpha (float): The parameter in balanced form of focal loss. Defaults to 0.25. reduction (str): The method used to reduce the loss into a scalar. Options are "none" and "mean". Defaults to 'mean'. loss_weight (float): Weight of loss. Defaults to 1.0. """ def __init__(self, gamma=2.0, alpha=0.25, reduction='mean', loss_weight=1.0 ): super(FocalLossNew, self).__init__() self.gamma = gamma self.alpha = alpha self.reduction = reduction self.loss_weight = loss_weight def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
David-19940718/mmclassification
FocalLoss
false
5,052
[ "Apache-2.0" ]
1
987dd45457e38c4787237ea468799849dce11ada
https://github.com/David-19940718/mmclassification/tree/987dd45457e38c4787237ea468799849dce11ada
FocalTverskyLoss
import torch from torch import nn class FocalTverskyLoss(nn.Module): def __init__(self, weight=None, size_average=True): super(FocalTverskyLoss, self).__init__() def forward(self, inputs, targets, smooth=1, alpha=0.3, beta=0.7, gamma=2): inputs = inputs.view(-1) targets = targets.view(-1) TP = (inputs * targets).sum() FP = ((1 - targets) * inputs).sum() FN = (targets * (1 - inputs)).sum() Tversky = (TP + smooth) / (TP + alpha * FP + beta * FN + smooth) FocalTversky = (1 - Tversky) ** gamma return FocalTversky def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_div_mul_pow_rsub_sum_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.load(in_ptr1 + r0, None) tmp2 = tmp0 * tmp1 tmp3 = tl.broadcast_to(tmp2, [RBLOCK]) tmp5 = triton_helpers.promote_to_tensor(tl.sum(tmp3, 0)) tmp6 = 1.0 tmp7 = tmp6 - tmp1 tmp8 = tmp7 * tmp0 tmp9 = tl.broadcast_to(tmp8, [RBLOCK]) tmp11 = triton_helpers.promote_to_tensor(tl.sum(tmp9, 0)) tmp12 = tmp6 - tmp0 tmp13 = tmp1 * tmp12 tmp14 = tl.broadcast_to(tmp13, [RBLOCK]) tmp16 = triton_helpers.promote_to_tensor(tl.sum(tmp14, 0)) tmp17 = tmp5 + tmp6 tmp18 = 0.3 tmp19 = tmp11 * tmp18 tmp20 = tmp5 + tmp19 tmp21 = 0.7 tmp22 = tmp16 * tmp21 tmp23 = tmp20 + tmp22 tmp24 = tmp23 + tmp6 tmp25 = tmp17 / tmp24 tmp26 = tmp6 - tmp25 tmp27 = tmp26 * tmp26 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp27, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf3 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_div_mul_pow_rsub_sum_0[grid(1)](buf3, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf3, class FocalTverskyLossNew(nn.Module): def __init__(self, weight=None, size_average=True): super(FocalTverskyLossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
DeVriesMatt/cellshape-voxel
FocalTverskyLoss
false
5,053
[ "BSD-3-Clause" ]
1
64c2c57cc8b8ebe7f6ba1934caaaa3aaa1d6a0c1
https://github.com/DeVriesMatt/cellshape-voxel/tree/64c2c57cc8b8ebe7f6ba1934caaaa3aaa1d6a0c1
SplAtConv2d
from torch.nn import Module import torch from torch import nn import torch.nn.functional as F from torch.nn import Conv2d from torch.nn import ReLU from torch.nn.modules.utils import _pair class DropBlock2D(object): def __init__(self, *args, **kwargs): raise NotImplementedError class rSoftMax(nn.Module): def __init__(self, radix, cardinality): super().__init__() self.radix = radix self.cardinality = cardinality def forward(self, x): batch = x.size(0) if self.radix > 1: x = x.view(batch, self.cardinality, self.radix, -1).transpose(1, 2) x = F.softmax(x, dim=1) x = x.reshape(batch, -1) else: x = torch.sigmoid(x) return x class SplAtConv2d(Module): """Split-Attention Conv2d """ def __init__(self, in_channels, channels, kernel_size, stride=(1, 1), padding=(0, 0), dilation=(1, 1), groups=1, bias=True, radix=2, reduction_factor=4, rectify=False, rectify_avg=False, norm_layer= None, dropblock_prob=0.0, **kwargs): super(SplAtConv2d, self).__init__() padding = _pair(padding) self.rectify = rectify and (padding[0] > 0 or padding[1] > 0) self.rectify_avg = rectify_avg inter_channels = max(in_channels * radix // reduction_factor, 32) self.radix = radix self.cardinality = groups self.channels = channels self.dropblock_prob = dropblock_prob if self.rectify: self.conv = RFConv2d(in_channels, channels * radix, kernel_size, stride, padding, dilation, groups=groups * radix, bias=bias, average_mode=rectify_avg, **kwargs) else: self.conv = Conv2d(in_channels, channels * radix, kernel_size, stride, padding, dilation, groups=groups * radix, bias=bias, **kwargs) self.use_bn = norm_layer is not None if self.use_bn: self.bn0 = norm_layer(channels * radix) self.relu = ReLU(inplace=True) self.fc1 = Conv2d(channels, inter_channels, 1, groups=self.cardinality) if self.use_bn: self.bn1 = norm_layer(inter_channels) self.fc2 = Conv2d(inter_channels, channels * radix, 1, groups=self. cardinality) if dropblock_prob > 0.0: self.dropblock = DropBlock2D(dropblock_prob, 3) self.rsoftmax = rSoftMax(radix, groups) def forward(self, x): x = self.conv(x) if self.use_bn: x = self.bn0(x) if self.dropblock_prob > 0.0: x = self.dropblock(x) x = self.relu(x) batch, rchannel = x.shape[:2] if self.radix > 1: if torch.__version__ < '1.5': splited = torch.split(x, int(rchannel // self.radix), dim=1) else: splited = torch.split(x, rchannel // self.radix, dim=1) gap = sum(splited) else: gap = x gap = F.adaptive_avg_pool2d(gap, 1) gap = self.fc1(gap) if self.use_bn: gap = self.bn1(gap) gap = self.relu(gap) atten = self.fc2(gap) atten = self.rsoftmax(atten).view(batch, -1, 1, 1) if self.radix > 1: if torch.__version__ < '1.5': attens = torch.split(atten, int(rchannel // self.radix), dim=1) else: attens = torch.split(atten, rchannel // self.radix, dim=1) out = sum([(att * split) for att, split in zip(attens, splited)]) else: out = atten * x return out.contiguous() def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'channels': 4, 'kernel_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math from torch.nn import Module from torch import nn import torch.nn.functional as F from torch.nn import Conv2d from torch.nn import ReLU from torch.nn.modules.utils import _pair assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 8 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) @triton.jit def triton_poi_fused_add_mean_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 8 * x1), xmask) tmp3 = tl.load(in_ptr0 + (4 + x0 + 8 * x1), xmask) tmp1 = 0.0 tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp5 = 1.0 tmp6 = tmp4 / tmp5 tl.store(out_ptr0 + x2, tmp6, xmask) @triton.jit def triton_poi_fused_convolution_relu_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 32 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_convolution_3(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 8 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x2, tmp2, xmask) @triton.jit def triton_poi_fused__softmax_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 4 x2 = xindex // 8 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 8 * x2), xmask, eviction_policy='evict_last' ) tmp2 = tl.load(in_ptr0 + (4 + x0 + 8 * x2), xmask, eviction_policy= 'evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp4 = tmp0 - tmp3 tmp5 = tl_math.exp(tmp4) tmp6 = tmp1 - tmp3 tmp7 = tl_math.exp(tmp6) tmp8 = tmp2 - tmp3 tmp9 = tl_math.exp(tmp8) tmp10 = tmp7 + tmp9 tmp11 = tmp5 / tmp10 tl.store(out_ptr0 + x3, tmp11, xmask) @triton.jit def triton_poi_fused_add_mul_5(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 8 * x1), xmask) tmp1 = tl.load(in_ptr1 + (x0 + 8 * x1), xmask) tmp5 = tl.load(in_ptr0 + (4 + x0 + 8 * x1), xmask) tmp6 = tl.load(in_ptr1 + (4 + x0 + 8 * x1), xmask) tmp2 = tmp0 * tmp1 tmp3 = 0.0 tmp4 = tmp2 + tmp3 tmp7 = tmp5 * tmp6 tmp8 = tmp4 + tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (8, 2, 4, 4), (32, 16, 4, 1)) assert_size_stride(primals_2, (8,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (32, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_5, (32,), (1,)) assert_size_stride(primals_6, (8, 32, 1, 1), (32, 1, 1, 1)) assert_size_stride(primals_7, (8,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=2, bias=None) assert_size_stride(buf0, (4, 8, 1, 1), (8, 1, 1, 1)) buf1 = reinterpret_tensor(buf0, (4, 8, 1, 1), (8, 1, 32, 32), 0) del buf0 buf9 = empty_strided_cuda((4, 8, 1, 1), (8, 1, 1, 1), torch.bool) get_raw_stream(0) triton_poi_fused_convolution_relu_threshold_backward_0[grid(32)](buf1, primals_2, buf9, 32, XBLOCK=32, num_warps=1, num_stages=1) del primals_2 buf2 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32) triton_poi_fused_add_mean_1[grid(16)](buf1, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1) buf3 = extern_kernels.convolution(buf2, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf3, (4, 32, 1, 1), (32, 1, 1, 1)) buf4 = buf3 del buf3 triton_poi_fused_convolution_relu_2[grid(128)](buf4, primals_5, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf5 = extern_kernels.convolution(buf4, primals_6, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf5, (4, 8, 1, 1), (8, 1, 1, 1)) buf6 = buf5 del buf5 triton_poi_fused_convolution_3[grid(32)](buf6, primals_7, 32, XBLOCK=32, num_warps=1, num_stages=1) del primals_7 buf7 = empty_strided_cuda((4, 2, 1, 4), (8, 4, 4, 1), torch.float32) triton_poi_fused__softmax_4[grid(32)](buf6, buf7, 32, XBLOCK=32, num_warps=1, num_stages=1) buf8 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32) triton_poi_fused_add_mul_5[grid(16)](buf7, buf1, buf8, 16, XBLOCK= 16, num_warps=1, num_stages=1) return (buf8, primals_1, primals_3, primals_4, primals_6, reinterpret_tensor(buf1, (4, 4, 1, 1), (8, 1, 1, 1), 0), reinterpret_tensor(buf1, (4, 4, 1, 1), (8, 1, 1, 1), 4), buf2, buf4, buf6, reinterpret_tensor(buf7, (4, 4, 1, 1), (8, 1, 1, 1), 0), reinterpret_tensor(buf7, (4, 4, 1, 1), (8, 1, 1, 1), 4), buf9) class DropBlock2D(object): def __init__(self, *args, **kwargs): raise NotImplementedError class rSoftMax(nn.Module): def __init__(self, radix, cardinality): super().__init__() self.radix = radix self.cardinality = cardinality def forward(self, x): batch = x.size(0) if self.radix > 1: x = x.view(batch, self.cardinality, self.radix, -1).transpose(1, 2) x = F.softmax(x, dim=1) x = x.reshape(batch, -1) else: x = torch.sigmoid(x) return x class SplAtConv2dNew(Module): """Split-Attention Conv2d """ def __init__(self, in_channels, channels, kernel_size, stride=(1, 1), padding=(0, 0), dilation=(1, 1), groups=1, bias=True, radix=2, reduction_factor=4, rectify=False, rectify_avg=False, norm_layer= None, dropblock_prob=0.0, **kwargs): super(SplAtConv2dNew, self).__init__() padding = _pair(padding) self.rectify = rectify and (padding[0] > 0 or padding[1] > 0) self.rectify_avg = rectify_avg inter_channels = max(in_channels * radix // reduction_factor, 32) self.radix = radix self.cardinality = groups self.channels = channels self.dropblock_prob = dropblock_prob if self.rectify: self.conv = RFConv2d(in_channels, channels * radix, kernel_size, stride, padding, dilation, groups=groups * radix, bias=bias, average_mode=rectify_avg, **kwargs) else: self.conv = Conv2d(in_channels, channels * radix, kernel_size, stride, padding, dilation, groups=groups * radix, bias=bias, **kwargs) self.use_bn = norm_layer is not None if self.use_bn: self.bn0 = norm_layer(channels * radix) self.relu = ReLU(inplace=True) self.fc1 = Conv2d(channels, inter_channels, 1, groups=self.cardinality) if self.use_bn: self.bn1 = norm_layer(inter_channels) self.fc2 = Conv2d(inter_channels, channels * radix, 1, groups=self. cardinality) if dropblock_prob > 0.0: self.dropblock = DropBlock2D(dropblock_prob, 3) self.rsoftmax = rSoftMax(radix, groups) def forward(self, input_0): primals_1 = self.conv.weight primals_2 = self.conv.bias primals_4 = self.fc1.weight primals_5 = self.fc1.bias primals_6 = self.fc2.weight primals_7 = self.fc2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
DYF-AI/openvino-x
SplAtConv2d
false
5,054
[ "Apache-2.0" ]
1
0f18ebb240ea3394f7e461aca34fac158e686d95
https://github.com/DYF-AI/openvino-x/tree/0f18ebb240ea3394f7e461aca34fac158e686d95
SpatialCrossMapLRN
import torch import torch.nn as nn import torch.utils.data.dataloader import torch.utils.data import torch.backends.cudnn class SpatialCrossMapLRN(nn.Module): def __init__(self, local_size=1, alpha=1.0, beta=0.75, k=1, ACROSS_CHANNELS=True): super(SpatialCrossMapLRN, self).__init__() self.ACROSS_CHANNELS = ACROSS_CHANNELS if ACROSS_CHANNELS: self.average = nn.AvgPool3d(kernel_size=(local_size, 1, 1), stride=1, padding=(int((local_size - 1.0) / 2), 0, 0)) else: self.average = nn.AvgPool2d(kernel_size=local_size, stride=1, padding=int((local_size - 1.0) / 2)) self.alpha = alpha self.beta = beta self.k = k def forward(self, x): if self.ACROSS_CHANNELS: div = x.pow(2).unsqueeze(1) div = self.average(div).squeeze(1) div = div.mul(self.alpha).add(self.k).pow(self.beta) else: div = x.pow(2) div = self.average(div) div = div.mul(self.alpha).add(self.k).pow(self.beta) x = x.div(div) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.utils.data.dataloader import torch.utils.data import torch.backends.cudnn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_div_mul_pow_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tmp0 * tmp0 tmp2 = 1.0 tmp3 = tmp1 * tmp2 tmp4 = tmp3 * tmp2 tmp5 = tmp4 + tmp2 tmp6 = 0.75 tmp7 = libdevice.pow(tmp5, tmp6) tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x0, tmp8, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_mul_pow_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, class SpatialCrossMapLRNNew(nn.Module): def __init__(self, local_size=1, alpha=1.0, beta=0.75, k=1, ACROSS_CHANNELS=True): super(SpatialCrossMapLRNNew, self).__init__() self.ACROSS_CHANNELS = ACROSS_CHANNELS if ACROSS_CHANNELS: self.average = nn.AvgPool3d(kernel_size=(local_size, 1, 1), stride=1, padding=(int((local_size - 1.0) / 2), 0, 0)) else: self.average = nn.AvgPool2d(kernel_size=local_size, stride=1, padding=int((local_size - 1.0) / 2)) self.alpha = alpha self.beta = beta self.k = k def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
DeepBrainsMe/PyDoctor_Final
SpatialCrossMapLRN
false
5,055
[ "MIT" ]
1
49ecfc64b2a2866e7f37cc79c1f32a817975f064
https://github.com/DeepBrainsMe/PyDoctor_Final/tree/49ecfc64b2a2866e7f37cc79c1f32a817975f064
StyleAdaptiveLayerNorm
import torch import torch.nn from torch import nn import torch.utils.data import torch.utils.data.distributed class AffineLinear(nn.Module): def __init__(self, in_dim, out_dim): super(AffineLinear, self).__init__() affine = nn.Linear(in_dim, out_dim) self.affine = affine def forward(self, input): return self.affine(input) class StyleAdaptiveLayerNorm(nn.Module): def __init__(self, in_channel, style_dim): super(StyleAdaptiveLayerNorm, self).__init__() self.in_channel = in_channel self.norm = nn.LayerNorm(in_channel, elementwise_affine=False) self.style = AffineLinear(style_dim, in_channel * 2) self.style.affine.bias.data[:in_channel] = 1 self.style.affine.bias.data[in_channel:] = 0 def forward(self, input, style_code): style = self.style(style_code).unsqueeze(1) gamma, beta = style.chunk(2, dim=-1) out = self.norm(input) out = gamma * out + beta return out def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channel': 4, 'style_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn from torch import nn import torch.utils.data import torch.utils.data.distributed assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_native_layer_norm_0(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tmp9 = tmp0 - tmp8 tmp10 = tmp9 * tmp9 tmp11 = tmp1 - tmp8 tmp12 = tmp11 * tmp11 tmp13 = tmp10 + tmp12 tmp14 = tmp3 - tmp8 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp8 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp19 / tmp7 tmp21 = 1e-05 tmp22 = tmp20 + tmp21 tmp23 = libdevice.rsqrt(tmp22) tl.store(out_ptr0 + x0, tmp8, xmask) tl.store(out_ptr1 + x0, tmp23, xmask) @triton.jit def triton_poi_fused_add_mul_native_layer_norm_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 % 16 x3 = xindex // 256 x4 = xindex % 256 x5 = xindex // 4 % 64 x7 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 8 * x1 + 128 * x3), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr3 + x5, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr4 + x5, xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (4 + x0 + 8 * x1 + 128 * x3), xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp5 = tmp3 - tmp4 tmp7 = tmp5 * tmp6 tmp8 = tmp2 * tmp7 tmp11 = tmp9 + tmp10 tmp12 = tmp8 + tmp11 tl.store(out_ptr0 + x7, tmp12, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (8, 4), (4, 1)) assert_size_stride(primals_2, (8,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 8), (8, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 8), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf2 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) get_raw_stream(0) triton_poi_fused_native_layer_norm_0[grid(64)](primals_4, buf1, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) buf3 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1), torch.float32) triton_poi_fused_add_mul_native_layer_norm_1[grid(1024)](buf0, primals_2, primals_4, buf1, buf2, buf3, 1024, XBLOCK=256, num_warps=4, num_stages=1) del buf0 del buf1 del buf2 del primals_2 return buf3, primals_4, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0) class AffineLinear(nn.Module): def __init__(self, in_dim, out_dim): super(AffineLinear, self).__init__() affine = nn.Linear(in_dim, out_dim) self.affine = affine def forward(self, input): return self.affine(input) class StyleAdaptiveLayerNormNew(nn.Module): def __init__(self, in_channel, style_dim): super(StyleAdaptiveLayerNormNew, self).__init__() self.in_channel = in_channel self.norm = nn.LayerNorm(in_channel, elementwise_affine=False) self.style = AffineLinear(style_dim, in_channel * 2) self.style.affine.bias.data[:in_channel] = 1 self.style.affine.bias.data[in_channel:] = 0 def forward(self, input_0, input_1): primals_1 = self.style.affine.weight primals_2 = self.style.affine.bias primals_3 = input_0 primals_4 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
DanielLin94144/StyleSpeech
StyleAdaptiveLayerNorm
false
5,056
[ "MIT" ]
1
809e8ead55bea2c63f714fdc19bf24d80f0f546c
https://github.com/DanielLin94144/StyleSpeech/tree/809e8ead55bea2c63f714fdc19bf24d80f0f546c
ATLoss
import torch import torch.nn as nn import torch.nn.functional as F class ATLoss(nn.Module): """ Module for calculating AT Loss :param norm_type (int): Norm to be used in calculating loss """ def __init__(self, norm_type=2): super(ATLoss, self).__init__() self.p = norm_type def forward(self, teacher_output, student_output): """ Forward function :param teacher_output (torch.FloatTensor): Prediction made by the teacher model :param student_output (torch.FloatTensor): Prediction made by the student model """ A_t = teacher_output[1:] A_s = student_output[1:] loss = 0.0 for layerT, layerS in zip(A_t, A_s): xT = self.single_at_loss(layerT) xS = self.single_at_loss(layerS) loss += (xS - xT).pow(self.p).mean() return loss def single_at_loss(self, activation): """ Function for calculating single attention loss """ return F.normalize(activation.pow(self.p).mean(1).view(activation. size(0), -1)) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.nn.functional as F assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_linalg_vector_norm_mean_pow_view_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + (64 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (68 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (72 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp8 = tl.load(in_ptr0 + (76 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp14 = tl.load(in_ptr0 + (65 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp16 = tl.load(in_ptr0 + (69 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp19 = tl.load(in_ptr0 + (73 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp22 = tl.load(in_ptr0 + (77 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp28 = tl.load(in_ptr0 + (66 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp30 = tl.load(in_ptr0 + (70 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp33 = tl.load(in_ptr0 + (74 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp36 = tl.load(in_ptr0 + (78 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp42 = tl.load(in_ptr0 + (67 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp44 = tl.load(in_ptr0 + (71 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp47 = tl.load(in_ptr0 + (75 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp50 = tl.load(in_ptr0 + (79 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp1 = tmp0 * tmp0 tmp3 = tmp2 * tmp2 tmp4 = tmp1 + tmp3 tmp6 = tmp5 * tmp5 tmp7 = tmp4 + tmp6 tmp9 = tmp8 * tmp8 tmp10 = tmp7 + tmp9 tmp11 = 4.0 tmp12 = tmp10 / tmp11 tmp13 = tmp12 * tmp12 tmp15 = tmp14 * tmp14 tmp17 = tmp16 * tmp16 tmp18 = tmp15 + tmp17 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp23 = tmp22 * tmp22 tmp24 = tmp21 + tmp23 tmp25 = tmp24 / tmp11 tmp26 = tmp25 * tmp25 tmp27 = tmp13 + tmp26 tmp29 = tmp28 * tmp28 tmp31 = tmp30 * tmp30 tmp32 = tmp29 + tmp31 tmp34 = tmp33 * tmp33 tmp35 = tmp32 + tmp34 tmp37 = tmp36 * tmp36 tmp38 = tmp35 + tmp37 tmp39 = tmp38 / tmp11 tmp40 = tmp39 * tmp39 tmp41 = tmp27 + tmp40 tmp43 = tmp42 * tmp42 tmp45 = tmp44 * tmp44 tmp46 = tmp43 + tmp45 tmp48 = tmp47 * tmp47 tmp49 = tmp46 + tmp48 tmp51 = tmp50 * tmp50 tmp52 = tmp49 + tmp51 tmp53 = tmp52 / tmp11 tmp54 = tmp53 * tmp53 tmp55 = tmp41 + tmp54 tl.store(out_ptr0 + x0, tmp55, xmask) @triton.jit def triton_poi_fused_linalg_vector_norm_mean_pow_view_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + (128 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (132 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (136 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp8 = tl.load(in_ptr0 + (140 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp14 = tl.load(in_ptr0 + (129 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp16 = tl.load(in_ptr0 + (133 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp19 = tl.load(in_ptr0 + (137 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp22 = tl.load(in_ptr0 + (141 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp28 = tl.load(in_ptr0 + (130 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp30 = tl.load(in_ptr0 + (134 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp33 = tl.load(in_ptr0 + (138 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp36 = tl.load(in_ptr0 + (142 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp42 = tl.load(in_ptr0 + (131 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp44 = tl.load(in_ptr0 + (135 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp47 = tl.load(in_ptr0 + (139 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp50 = tl.load(in_ptr0 + (143 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp1 = tmp0 * tmp0 tmp3 = tmp2 * tmp2 tmp4 = tmp1 + tmp3 tmp6 = tmp5 * tmp5 tmp7 = tmp4 + tmp6 tmp9 = tmp8 * tmp8 tmp10 = tmp7 + tmp9 tmp11 = 4.0 tmp12 = tmp10 / tmp11 tmp13 = tmp12 * tmp12 tmp15 = tmp14 * tmp14 tmp17 = tmp16 * tmp16 tmp18 = tmp15 + tmp17 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp23 = tmp22 * tmp22 tmp24 = tmp21 + tmp23 tmp25 = tmp24 / tmp11 tmp26 = tmp25 * tmp25 tmp27 = tmp13 + tmp26 tmp29 = tmp28 * tmp28 tmp31 = tmp30 * tmp30 tmp32 = tmp29 + tmp31 tmp34 = tmp33 * tmp33 tmp35 = tmp32 + tmp34 tmp37 = tmp36 * tmp36 tmp38 = tmp35 + tmp37 tmp39 = tmp38 / tmp11 tmp40 = tmp39 * tmp39 tmp41 = tmp27 + tmp40 tmp43 = tmp42 * tmp42 tmp45 = tmp44 * tmp44 tmp46 = tmp43 + tmp45 tmp48 = tmp47 * tmp47 tmp49 = tmp46 + tmp48 tmp51 = tmp50 * tmp50 tmp52 = tmp49 + tmp51 tmp53 = tmp52 / tmp11 tmp54 = tmp53 * tmp53 tmp55 = tmp41 + tmp54 tl.store(out_ptr0 + x0, tmp55, xmask) @triton.jit def triton_poi_fused_linalg_vector_norm_mean_pow_view_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + (192 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (196 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (200 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp8 = tl.load(in_ptr0 + (204 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp14 = tl.load(in_ptr0 + (193 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp16 = tl.load(in_ptr0 + (197 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp19 = tl.load(in_ptr0 + (201 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp22 = tl.load(in_ptr0 + (205 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp28 = tl.load(in_ptr0 + (194 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp30 = tl.load(in_ptr0 + (198 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp33 = tl.load(in_ptr0 + (202 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp36 = tl.load(in_ptr0 + (206 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp42 = tl.load(in_ptr0 + (195 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp44 = tl.load(in_ptr0 + (199 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp47 = tl.load(in_ptr0 + (203 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp50 = tl.load(in_ptr0 + (207 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp1 = tmp0 * tmp0 tmp3 = tmp2 * tmp2 tmp4 = tmp1 + tmp3 tmp6 = tmp5 * tmp5 tmp7 = tmp4 + tmp6 tmp9 = tmp8 * tmp8 tmp10 = tmp7 + tmp9 tmp11 = 4.0 tmp12 = tmp10 / tmp11 tmp13 = tmp12 * tmp12 tmp15 = tmp14 * tmp14 tmp17 = tmp16 * tmp16 tmp18 = tmp15 + tmp17 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp23 = tmp22 * tmp22 tmp24 = tmp21 + tmp23 tmp25 = tmp24 / tmp11 tmp26 = tmp25 * tmp25 tmp27 = tmp13 + tmp26 tmp29 = tmp28 * tmp28 tmp31 = tmp30 * tmp30 tmp32 = tmp29 + tmp31 tmp34 = tmp33 * tmp33 tmp35 = tmp32 + tmp34 tmp37 = tmp36 * tmp36 tmp38 = tmp35 + tmp37 tmp39 = tmp38 / tmp11 tmp40 = tmp39 * tmp39 tmp41 = tmp27 + tmp40 tmp43 = tmp42 * tmp42 tmp45 = tmp44 * tmp44 tmp46 = tmp43 + tmp45 tmp48 = tmp47 * tmp47 tmp49 = tmp46 + tmp48 tmp51 = tmp50 * tmp50 tmp52 = tmp49 + tmp51 tmp53 = tmp52 / tmp11 tmp54 = tmp53 * tmp53 tmp55 = tmp41 + tmp54 tl.store(out_ptr0 + x0, tmp55, xmask) @triton.jit def triton_per_fused_add_div_mean_pow_sub_view_3(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex % 4 r1 = rindex // 4 tmp0 = tl.load(in_ptr0 + (64 + r0 + 16 * r1), None) tmp2 = tl.load(in_ptr0 + (68 + r0 + 16 * r1), None) tmp5 = tl.load(in_ptr0 + (72 + r0 + 16 * r1), None) tmp8 = tl.load(in_ptr0 + (76 + r0 + 16 * r1), None) tmp13 = tl.load(in_ptr1 + r1, None, eviction_policy='evict_last') tmp18 = tl.load(in_ptr2 + (64 + r0 + 16 * r1), None) tmp20 = tl.load(in_ptr2 + (68 + r0 + 16 * r1), None) tmp23 = tl.load(in_ptr2 + (72 + r0 + 16 * r1), None) tmp26 = tl.load(in_ptr2 + (76 + r0 + 16 * r1), None) tmp30 = tl.load(in_ptr3 + r1, None, eviction_policy='evict_last') tmp39 = tl.load(in_ptr0 + (128 + r0 + 16 * r1), None) tmp41 = tl.load(in_ptr0 + (132 + r0 + 16 * r1), None) tmp44 = tl.load(in_ptr0 + (136 + r0 + 16 * r1), None) tmp47 = tl.load(in_ptr0 + (140 + r0 + 16 * r1), None) tmp51 = tl.load(in_ptr4 + r1, None, eviction_policy='evict_last') tmp55 = tl.load(in_ptr2 + (128 + r0 + 16 * r1), None) tmp57 = tl.load(in_ptr2 + (132 + r0 + 16 * r1), None) tmp60 = tl.load(in_ptr2 + (136 + r0 + 16 * r1), None) tmp63 = tl.load(in_ptr2 + (140 + r0 + 16 * r1), None) tmp67 = tl.load(in_ptr5 + r1, None, eviction_policy='evict_last') tmp76 = tl.load(in_ptr0 + (192 + r0 + 16 * r1), None) tmp78 = tl.load(in_ptr0 + (196 + r0 + 16 * r1), None) tmp81 = tl.load(in_ptr0 + (200 + r0 + 16 * r1), None) tmp84 = tl.load(in_ptr0 + (204 + r0 + 16 * r1), None) tmp88 = tl.load(in_ptr6 + r1, None, eviction_policy='evict_last') tmp92 = tl.load(in_ptr2 + (192 + r0 + 16 * r1), None) tmp94 = tl.load(in_ptr2 + (196 + r0 + 16 * r1), None) tmp97 = tl.load(in_ptr2 + (200 + r0 + 16 * r1), None) tmp100 = tl.load(in_ptr2 + (204 + r0 + 16 * r1), None) tmp104 = tl.load(in_ptr7 + r1, None, eviction_policy='evict_last') tmp1 = tmp0 * tmp0 tmp3 = tmp2 * tmp2 tmp4 = tmp1 + tmp3 tmp6 = tmp5 * tmp5 tmp7 = tmp4 + tmp6 tmp9 = tmp8 * tmp8 tmp10 = tmp7 + tmp9 tmp11 = 4.0 tmp12 = tmp10 / tmp11 tmp14 = libdevice.sqrt(tmp13) tmp15 = 1e-12 tmp16 = triton_helpers.maximum(tmp14, tmp15) tmp17 = tmp12 / tmp16 tmp19 = tmp18 * tmp18 tmp21 = tmp20 * tmp20 tmp22 = tmp19 + tmp21 tmp24 = tmp23 * tmp23 tmp25 = tmp22 + tmp24 tmp27 = tmp26 * tmp26 tmp28 = tmp25 + tmp27 tmp29 = tmp28 / tmp11 tmp31 = libdevice.sqrt(tmp30) tmp32 = triton_helpers.maximum(tmp31, tmp15) tmp33 = tmp29 / tmp32 tmp34 = tmp17 - tmp33 tmp35 = tmp34 * tmp34 tmp36 = tl.broadcast_to(tmp35, [XBLOCK, RBLOCK]) tmp38 = tl.sum(tmp36, 1)[:, None] tmp40 = tmp39 * tmp39 tmp42 = tmp41 * tmp41 tmp43 = tmp40 + tmp42 tmp45 = tmp44 * tmp44 tmp46 = tmp43 + tmp45 tmp48 = tmp47 * tmp47 tmp49 = tmp46 + tmp48 tmp50 = tmp49 / tmp11 tmp52 = libdevice.sqrt(tmp51) tmp53 = triton_helpers.maximum(tmp52, tmp15) tmp54 = tmp50 / tmp53 tmp56 = tmp55 * tmp55 tmp58 = tmp57 * tmp57 tmp59 = tmp56 + tmp58 tmp61 = tmp60 * tmp60 tmp62 = tmp59 + tmp61 tmp64 = tmp63 * tmp63 tmp65 = tmp62 + tmp64 tmp66 = tmp65 / tmp11 tmp68 = libdevice.sqrt(tmp67) tmp69 = triton_helpers.maximum(tmp68, tmp15) tmp70 = tmp66 / tmp69 tmp71 = tmp54 - tmp70 tmp72 = tmp71 * tmp71 tmp73 = tl.broadcast_to(tmp72, [XBLOCK, RBLOCK]) tmp75 = tl.sum(tmp73, 1)[:, None] tmp77 = tmp76 * tmp76 tmp79 = tmp78 * tmp78 tmp80 = tmp77 + tmp79 tmp82 = tmp81 * tmp81 tmp83 = tmp80 + tmp82 tmp85 = tmp84 * tmp84 tmp86 = tmp83 + tmp85 tmp87 = tmp86 / tmp11 tmp89 = libdevice.sqrt(tmp88) tmp90 = triton_helpers.maximum(tmp89, tmp15) tmp91 = tmp87 / tmp90 tmp93 = tmp92 * tmp92 tmp95 = tmp94 * tmp94 tmp96 = tmp93 + tmp95 tmp98 = tmp97 * tmp97 tmp99 = tmp96 + tmp98 tmp101 = tmp100 * tmp100 tmp102 = tmp99 + tmp101 tmp103 = tmp102 / tmp11 tmp105 = libdevice.sqrt(tmp104) tmp106 = triton_helpers.maximum(tmp105, tmp15) tmp107 = tmp103 / tmp106 tmp108 = tmp91 - tmp107 tmp109 = tmp108 * tmp108 tmp110 = tl.broadcast_to(tmp109, [XBLOCK, RBLOCK]) tmp112 = tl.sum(tmp110, 1)[:, None] tmp113 = 16.0 tmp114 = tmp38 / tmp113 tmp115 = 0.0 tmp116 = tmp114 + tmp115 tmp117 = tmp75 / tmp113 tmp118 = tmp116 + tmp117 tmp119 = tmp112 / tmp113 tmp120 = tmp118 + tmp119 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp120, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 1), (1, 4), torch.float32) get_raw_stream(0) triton_poi_fused_linalg_vector_norm_mean_pow_view_0[grid(4)](arg1_1, buf0, 4, XBLOCK=4, num_warps=1, num_stages=1) buf1 = empty_strided_cuda((4, 1), (1, 4), torch.float32) triton_poi_fused_linalg_vector_norm_mean_pow_view_0[grid(4)](arg0_1, buf1, 4, XBLOCK=4, num_warps=1, num_stages=1) buf4 = empty_strided_cuda((4, 1), (1, 4), torch.float32) triton_poi_fused_linalg_vector_norm_mean_pow_view_1[grid(4)](arg1_1, buf4, 4, XBLOCK=4, num_warps=1, num_stages=1) buf5 = empty_strided_cuda((4, 1), (1, 4), torch.float32) triton_poi_fused_linalg_vector_norm_mean_pow_view_1[grid(4)](arg0_1, buf5, 4, XBLOCK=4, num_warps=1, num_stages=1) buf8 = empty_strided_cuda((4, 1), (1, 4), torch.float32) triton_poi_fused_linalg_vector_norm_mean_pow_view_2[grid(4)](arg1_1, buf8, 4, XBLOCK=4, num_warps=1, num_stages=1) buf9 = empty_strided_cuda((4, 1), (1, 4), torch.float32) triton_poi_fused_linalg_vector_norm_mean_pow_view_2[grid(4)](arg0_1, buf9, 4, XBLOCK=4, num_warps=1, num_stages=1) buf11 = empty_strided_cuda((), (), torch.float32) buf12 = buf11 del buf11 triton_per_fused_add_div_mean_pow_sub_view_3[grid(1)](buf12, arg1_1, buf0, arg0_1, buf1, buf4, buf5, buf8, buf9, 1, 16, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 del buf0 del buf1 del buf4 del buf5 del buf8 del buf9 return buf12, class ATLossNew(nn.Module): """ Module for calculating AT Loss :param norm_type (int): Norm to be used in calculating loss """ def __init__(self, norm_type=2): super(ATLossNew, self).__init__() self.p = norm_type def single_at_loss(self, activation): """ Function for calculating single attention loss """ return F.normalize(activation.pow(self.p).mean(1).view(activation. size(0), -1)) def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
DA-southampton/KD_Lib
ATLoss
false
5,057
[ "MIT" ]
1
bd4a9b93b9674607ecf467d280d5cab1c516bdc6
https://github.com/DA-southampton/KD_Lib/tree/bd4a9b93b9674607ecf467d280d5cab1c516bdc6
DiceBCELoss
import torch from torch import nn import torch.nn.functional as F class DiceBCELoss(nn.Module): def __init__(self, weight=None, size_average=True): super(DiceBCELoss, self).__init__() def forward(self, inputs, targets, smooth=1): inputs = inputs.view(-1) targets = targets.view(-1) intersection = (inputs * targets).sum() dice_loss = 1 - (2.0 * intersection + smooth) / (inputs.sum() + targets.sum() + smooth) BCE = F.binary_cross_entropy(inputs, targets, reduction='mean') Dice_BCE = BCE + dice_loss return Dice_BCE def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_binary_cross_entropy_div_mul_rsub_sum_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp3 = tl.load(in_ptr1 + r0, None) tmp1 = 1.0 tmp2 = tmp0 - tmp1 tmp4 = -tmp3 tmp5 = libdevice.log1p(tmp4) tmp6 = -100.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp2 * tmp7 tmp9 = tl_math.log(tmp3) tmp10 = triton_helpers.maximum(tmp9, tmp6) tmp11 = tmp0 * tmp10 tmp12 = tmp8 - tmp11 tmp13 = tl.broadcast_to(tmp12, [RBLOCK]) tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0)) tmp16 = tmp3 * tmp0 tmp17 = tl.broadcast_to(tmp16, [RBLOCK]) tmp19 = triton_helpers.promote_to_tensor(tl.sum(tmp17, 0)) tmp20 = tl.broadcast_to(tmp3, [RBLOCK]) tmp22 = triton_helpers.promote_to_tensor(tl.sum(tmp20, 0)) tmp23 = tl.broadcast_to(tmp0, [RBLOCK]) tmp25 = triton_helpers.promote_to_tensor(tl.sum(tmp23, 0)) tmp26 = 256.0 tmp27 = tmp15 / tmp26 tmp28 = 2.0 tmp29 = tmp19 * tmp28 tmp30 = tmp29 + tmp1 tmp31 = tmp22 + tmp25 tmp32 = tmp31 + tmp1 tmp33 = tmp30 / tmp32 tmp34 = tmp1 - tmp33 tmp35 = tmp27 + tmp34 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp35, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf4 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_binary_cross_entropy_div_mul_rsub_sum_0[grid(1)]( buf4, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf4, class DiceBCELossNew(nn.Module): def __init__(self, weight=None, size_average=True): super(DiceBCELossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
DeVriesMatt/cellshape-voxel
DiceBCELoss
false
5,058
[ "BSD-3-Clause" ]
1
64c2c57cc8b8ebe7f6ba1934caaaa3aaa1d6a0c1
https://github.com/DeVriesMatt/cellshape-voxel/tree/64c2c57cc8b8ebe7f6ba1934caaaa3aaa1d6a0c1
TverskyLoss
import torch from torch import nn class TverskyLoss(nn.Module): def __init__(self, weight=None, size_average=True): super(TverskyLoss, self).__init__() def forward(self, inputs, targets, smooth=1, alpha=0.3, beta=0.7): inputs = inputs.view(-1) targets = targets.view(-1) TP = (inputs * targets).sum() FP = ((1 - targets) * inputs).sum() FN = (targets * (1 - inputs)).sum() Tversky = (TP + smooth) / (TP + alpha * FP + beta * FN + smooth) return 1 - Tversky def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_div_mul_rsub_sum_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.load(in_ptr1 + r0, None) tmp2 = tmp0 * tmp1 tmp3 = tl.broadcast_to(tmp2, [RBLOCK]) tmp5 = triton_helpers.promote_to_tensor(tl.sum(tmp3, 0)) tmp6 = 1.0 tmp7 = tmp6 - tmp1 tmp8 = tmp7 * tmp0 tmp9 = tl.broadcast_to(tmp8, [RBLOCK]) tmp11 = triton_helpers.promote_to_tensor(tl.sum(tmp9, 0)) tmp12 = tmp6 - tmp0 tmp13 = tmp1 * tmp12 tmp14 = tl.broadcast_to(tmp13, [RBLOCK]) tmp16 = triton_helpers.promote_to_tensor(tl.sum(tmp14, 0)) tmp17 = tmp5 + tmp6 tmp18 = 0.3 tmp19 = tmp11 * tmp18 tmp20 = tmp5 + tmp19 tmp21 = 0.7 tmp22 = tmp16 * tmp21 tmp23 = tmp20 + tmp22 tmp24 = tmp23 + tmp6 tmp25 = tmp17 / tmp24 tmp26 = tmp6 - tmp25 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp26, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf3 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_div_mul_rsub_sum_0[grid(1)](buf3, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf3, class TverskyLossNew(nn.Module): def __init__(self, weight=None, size_average=True): super(TverskyLossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
DeVriesMatt/cellshape-voxel
TverskyLoss
false
5,059
[ "BSD-3-Clause" ]
1
64c2c57cc8b8ebe7f6ba1934caaaa3aaa1d6a0c1
https://github.com/DeVriesMatt/cellshape-voxel/tree/64c2c57cc8b8ebe7f6ba1934caaaa3aaa1d6a0c1
EuclideanDistLoss
import torch from torch import nn class EuclideanDistLoss(nn.Module): def __init__(self): super(EuclideanDistLoss, self).__init__() def forward(self, inputs, inputs_rot): dist = torch.dist(inputs, inputs_rot, p=2.0) return dist def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_dist_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.load(in_ptr1 + r0, None) tmp2 = tmp0 - tmp1 tmp3 = tmp2 * tmp2 tmp4 = tl.broadcast_to(tmp3, [RBLOCK]) tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0)) tmp7 = libdevice.sqrt(tmp6) tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp7, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_dist_0[grid(1)](buf1, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, class EuclideanDistLossNew(nn.Module): def __init__(self): super(EuclideanDistLossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
DeVriesMatt/cellshape-voxel
EuclideanDistLoss
false
5,060
[ "BSD-3-Clause" ]
1
64c2c57cc8b8ebe7f6ba1934caaaa3aaa1d6a0c1
https://github.com/DeVriesMatt/cellshape-voxel/tree/64c2c57cc8b8ebe7f6ba1934caaaa3aaa1d6a0c1
MaskedMSELoss
import torch import torch.utils.data from torch import nn class MaskedMSELoss(nn.Module): def __init__(self): super(MaskedMSELoss, self).__init__() def forward(self, pred, target, output_lengths): squared_error = (target - pred) ** 2 loss = (squared_error.mean(1).sum(1) / output_lengths).mean() return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand( [4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.utils.data from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_mean_pow_sub_sum_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask) tmp1 = tl.load(in_ptr1 + (x0 + 64 * x1), xmask) tmp4 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask) tmp5 = tl.load(in_ptr1 + (16 + x0 + 64 * x1), xmask) tmp9 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask) tmp10 = tl.load(in_ptr1 + (32 + x0 + 64 * x1), xmask) tmp14 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask) tmp15 = tl.load(in_ptr1 + (48 + x0 + 64 * x1), xmask) tmp21 = tl.load(in_ptr0 + (4 + x0 + 64 * x1), xmask) tmp22 = tl.load(in_ptr1 + (4 + x0 + 64 * x1), xmask) tmp25 = tl.load(in_ptr0 + (20 + x0 + 64 * x1), xmask) tmp26 = tl.load(in_ptr1 + (20 + x0 + 64 * x1), xmask) tmp30 = tl.load(in_ptr0 + (36 + x0 + 64 * x1), xmask) tmp31 = tl.load(in_ptr1 + (36 + x0 + 64 * x1), xmask) tmp35 = tl.load(in_ptr0 + (52 + x0 + 64 * x1), xmask) tmp36 = tl.load(in_ptr1 + (52 + x0 + 64 * x1), xmask) tmp42 = tl.load(in_ptr0 + (8 + x0 + 64 * x1), xmask) tmp43 = tl.load(in_ptr1 + (8 + x0 + 64 * x1), xmask) tmp46 = tl.load(in_ptr0 + (24 + x0 + 64 * x1), xmask) tmp47 = tl.load(in_ptr1 + (24 + x0 + 64 * x1), xmask) tmp51 = tl.load(in_ptr0 + (40 + x0 + 64 * x1), xmask) tmp52 = tl.load(in_ptr1 + (40 + x0 + 64 * x1), xmask) tmp56 = tl.load(in_ptr0 + (56 + x0 + 64 * x1), xmask) tmp57 = tl.load(in_ptr1 + (56 + x0 + 64 * x1), xmask) tmp63 = tl.load(in_ptr0 + (12 + x0 + 64 * x1), xmask) tmp64 = tl.load(in_ptr1 + (12 + x0 + 64 * x1), xmask) tmp67 = tl.load(in_ptr0 + (28 + x0 + 64 * x1), xmask) tmp68 = tl.load(in_ptr1 + (28 + x0 + 64 * x1), xmask) tmp72 = tl.load(in_ptr0 + (44 + x0 + 64 * x1), xmask) tmp73 = tl.load(in_ptr1 + (44 + x0 + 64 * x1), xmask) tmp77 = tl.load(in_ptr0 + (60 + x0 + 64 * x1), xmask) tmp78 = tl.load(in_ptr1 + (60 + x0 + 64 * x1), xmask) tmp2 = tmp0 - tmp1 tmp3 = tmp2 * tmp2 tmp6 = tmp4 - tmp5 tmp7 = tmp6 * tmp6 tmp8 = tmp3 + tmp7 tmp11 = tmp9 - tmp10 tmp12 = tmp11 * tmp11 tmp13 = tmp8 + tmp12 tmp16 = tmp14 - tmp15 tmp17 = tmp16 * tmp16 tmp18 = tmp13 + tmp17 tmp19 = 4.0 tmp20 = tmp18 / tmp19 tmp23 = tmp21 - tmp22 tmp24 = tmp23 * tmp23 tmp27 = tmp25 - tmp26 tmp28 = tmp27 * tmp27 tmp29 = tmp24 + tmp28 tmp32 = tmp30 - tmp31 tmp33 = tmp32 * tmp32 tmp34 = tmp29 + tmp33 tmp37 = tmp35 - tmp36 tmp38 = tmp37 * tmp37 tmp39 = tmp34 + tmp38 tmp40 = tmp39 / tmp19 tmp41 = tmp20 + tmp40 tmp44 = tmp42 - tmp43 tmp45 = tmp44 * tmp44 tmp48 = tmp46 - tmp47 tmp49 = tmp48 * tmp48 tmp50 = tmp45 + tmp49 tmp53 = tmp51 - tmp52 tmp54 = tmp53 * tmp53 tmp55 = tmp50 + tmp54 tmp58 = tmp56 - tmp57 tmp59 = tmp58 * tmp58 tmp60 = tmp55 + tmp59 tmp61 = tmp60 / tmp19 tmp62 = tmp41 + tmp61 tmp65 = tmp63 - tmp64 tmp66 = tmp65 * tmp65 tmp69 = tmp67 - tmp68 tmp70 = tmp69 * tmp69 tmp71 = tmp66 + tmp70 tmp74 = tmp72 - tmp73 tmp75 = tmp74 * tmp74 tmp76 = tmp71 + tmp75 tmp79 = tmp77 - tmp78 tmp80 = tmp79 * tmp79 tmp81 = tmp76 + tmp80 tmp82 = tmp81 / tmp19 tmp83 = tmp62 + tmp82 tl.store(out_ptr0 + x2, tmp83, xmask) @triton.jit def triton_per_fused_div_mean_1(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex % 16 r2 = rindex tmp0 = tl.load(in_ptr0 + r0, None, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + r2, None) tmp2 = tmp0 / tmp1 tmp3 = tl.broadcast_to(tmp2, [RBLOCK]) tmp5 = triton_helpers.promote_to_tensor(tl.sum(tmp3, 0)) tmp6 = 256.0 tmp7 = tmp5 / tmp6 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp7, None) def call(args): arg0_1, arg1_1, arg2_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mean_pow_sub_sum_0[grid(16)](arg0_1, arg1_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg0_1 del arg1_1 buf1 = empty_strided_cuda((), (), torch.float32) buf2 = buf1 del buf1 triton_per_fused_div_mean_1[grid(1)](buf2, buf0, arg2_1, 1, 256, num_warps=2, num_stages=1) del arg2_1 del buf0 return buf2, class MaskedMSELossNew(nn.Module): def __init__(self): super(MaskedMSELossNew, self).__init__() def forward(self, input_0, input_1, input_2): arg0_1 = input_0 arg1_1 = input_1 arg2_1 = input_2 output = call([arg0_1, arg1_1, arg2_1]) return output[0]
DashaSerdyuk/tacotron2
MaskedMSELoss
false
5,061
[ "BSD-3-Clause" ]
1
1a88669670750f8b0e1aff76abc8b1b15300e1dc
https://github.com/DashaSerdyuk/tacotron2/tree/1a88669670750f8b0e1aff76abc8b1b15300e1dc
FocalLoss
import torch from torch import nn import torch.nn.functional as F class FocalLoss(nn.Module): def __init__(self, weight=None, size_average=True): super(FocalLoss, self).__init__() def forward(self, inputs, targets, alpha=0.8, gamma=2, smooth=1): inputs = inputs.view(-1) targets = targets.view(-1) BCE = F.binary_cross_entropy(inputs, targets, reduction='mean') BCE_EXP = torch.exp(-BCE) focal_loss = alpha * (1 - BCE_EXP) ** gamma * BCE return focal_loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_binary_cross_entropy_exp_mul_neg_pow_rsub_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp3 = tl.load(in_ptr1 + r0, None) tmp1 = 1.0 tmp2 = tmp0 - tmp1 tmp4 = -tmp3 tmp5 = libdevice.log1p(tmp4) tmp6 = -100.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp2 * tmp7 tmp9 = tl_math.log(tmp3) tmp10 = triton_helpers.maximum(tmp9, tmp6) tmp11 = tmp0 * tmp10 tmp12 = tmp8 - tmp11 tmp13 = tl.broadcast_to(tmp12, [RBLOCK]) tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0)) tmp16 = 256.0 tmp17 = tmp15 / tmp16 tmp18 = -tmp17 tmp19 = tl_math.exp(tmp18) tmp20 = tmp1 - tmp19 tmp21 = tmp20 * tmp20 tmp22 = 0.8 tmp23 = tmp21 * tmp22 tmp24 = tmp23 * tmp17 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp24, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_binary_cross_entropy_exp_mul_neg_pow_rsub_0[grid(1)]( buf1, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, class FocalLossNew(nn.Module): def __init__(self, weight=None, size_average=True): super(FocalLossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
DeVriesMatt/cellshape-voxel
FocalLoss
false
5,062
[ "BSD-3-Clause" ]
1
64c2c57cc8b8ebe7f6ba1934caaaa3aaa1d6a0c1
https://github.com/DeVriesMatt/cellshape-voxel/tree/64c2c57cc8b8ebe7f6ba1934caaaa3aaa1d6a0c1
h_swish
import torch import torch.nn as nn import torch.nn.functional as F import torch.utils.data.dataloader import torch.utils.data import torch.backends.cudnn class h_swish(nn.Module): def __init__(self, inplace=True): super(h_swish, self).__init__() self.inplace = inplace def forward(self, x): out = F.relu6(x + 3.0, self.inplace) / 6.0 return out * x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn import torch.utils.data.dataloader import torch.utils.data import torch.backends.cudnn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_div_hardtanh_mul_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 3.0 tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = 6.0 tmp6 = triton_helpers.minimum(tmp4, tmp5) tmp7 = 0.16666666666666666 tmp8 = tmp6 * tmp7 tmp9 = tmp8 * tmp0 tl.store(out_ptr0 + x0, tmp9, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_hardtanh_mul_0[grid(256)](arg0_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf0, class h_swishNew(nn.Module): def __init__(self, inplace=True): super(h_swishNew, self).__init__() self.inplace = inplace def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
DeepBrainsMe/PyDoctor_Final
h_swish
false
5,063
[ "MIT" ]
1
49ecfc64b2a2866e7f37cc79c1f32a817975f064
https://github.com/DeepBrainsMe/PyDoctor_Final/tree/49ecfc64b2a2866e7f37cc79c1f32a817975f064
ReconstructLoss
import torch import torch.nn as nn class ReconstructLoss(nn.Module): def __init__(self): super(ReconstructLoss, self).__init__() self.criterion = nn.L1Loss() def forward(self, x, y): loss = self.criterion(x, y) return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_abs_mean_sub_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.load(in_ptr1 + r0, None) tmp2 = tmp0 - tmp1 tmp3 = tl_math.abs(tmp2) tmp4 = tl.broadcast_to(tmp3, [RBLOCK]) tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0)) tmp7 = 256.0 tmp8 = tmp6 / tmp7 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp8, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_abs_mean_sub_0[grid(1)](buf1, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, class ReconstructLossNew(nn.Module): def __init__(self): super(ReconstructLossNew, self).__init__() self.criterion = nn.L1Loss() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
DevKiHyun/SRNTT.pytorch
ReconstructLoss
false
5,064
[ "MIT" ]
1
d7540921983cf42ea2a7eef544862a95318e6a35
https://github.com/DevKiHyun/SRNTT.pytorch/tree/d7540921983cf42ea2a7eef544862a95318e6a35
SinActv
import torch import torch.nn as nn class SinActv(nn.Module): """The sin activation function. """ def __init__(self): """Initializer method. """ super().__init__() def forward(self, input_): return torch.sin(input_) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_sin_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl_math.sin(tmp0) tl.store(out_ptr0 + x0, tmp1, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_sin_0[grid(256)](arg0_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf0, class SinActvNew(nn.Module): """The sin activation function. """ def __init__(self): """Initializer method. """ super().__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
DiffEqML/neurodiffeq
SinActv
false
5,065
[ "MIT" ]
1
c5e7404c47a4729578ee2149f289be0a8909d775
https://github.com/DiffEqML/neurodiffeq/tree/c5e7404c47a4729578ee2149f289be0a8909d775
AvgSpacial
import torch import torch.utils.data import torch.nn as nn import torch.utils.checkpoint class AvgSpacial(nn.Module): def forward(self, inp): return inp.view(inp.size(0), inp.size(1), -1).mean(-1) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.utils.data import torch.nn as nn import torch.utils.checkpoint assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_mean_0(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 16 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, 0) tmp4 = tl.sum(tmp3, 1)[:, None] tmp5 = 16.0 tmp6 = tmp4 / tmp5 tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp6, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_mean_0[grid(16)](buf1, arg0_1, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) del arg0_1 return buf1, class AvgSpacialNew(nn.Module): def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
CNNs4QSPR/se3cnn
AvgSpacial
false
5,066
[ "MIT" ]
1
513f5f827c4c511bdc96e3c6ea663c8fbce60f57
https://github.com/CNNs4QSPR/se3cnn/tree/513f5f827c4c511bdc96e3c6ea663c8fbce60f57
DiceLoss
import torch from torch import nn class DiceLoss(nn.Module): def __init__(self, weight=None, size_average=True): super(DiceLoss, self).__init__() def forward(self, inputs, targets, smooth=1): inputs = inputs.view(-1) targets = targets.view(-1) intersection = (inputs * targets).sum() dice = (2.0 * intersection + smooth) / (inputs.sum() + targets.sum( ) + smooth) return 1 - dice def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_div_mul_rsub_sum_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.load(in_ptr1 + r0, None) tmp2 = tmp0 * tmp1 tmp3 = tl.broadcast_to(tmp2, [RBLOCK]) tmp5 = triton_helpers.promote_to_tensor(tl.sum(tmp3, 0)) tmp6 = tl.broadcast_to(tmp0, [RBLOCK]) tmp8 = triton_helpers.promote_to_tensor(tl.sum(tmp6, 0)) tmp9 = tl.broadcast_to(tmp1, [RBLOCK]) tmp11 = triton_helpers.promote_to_tensor(tl.sum(tmp9, 0)) tmp12 = 2.0 tmp13 = tmp5 * tmp12 tmp14 = 1.0 tmp15 = tmp13 + tmp14 tmp16 = tmp8 + tmp11 tmp17 = tmp16 + tmp14 tmp18 = tmp15 / tmp17 tmp19 = tmp14 - tmp18 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp19, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf3 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_div_mul_rsub_sum_0[grid(1)](buf3, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf3, class DiceLossNew(nn.Module): def __init__(self, weight=None, size_average=True): super(DiceLossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
DeVriesMatt/cellshape-voxel
DiceLoss
false
5,067
[ "BSD-3-Clause" ]
1
64c2c57cc8b8ebe7f6ba1934caaaa3aaa1d6a0c1
https://github.com/DeVriesMatt/cellshape-voxel/tree/64c2c57cc8b8ebe7f6ba1934caaaa3aaa1d6a0c1
maxout
import torch import torch.nn as nn import torch.utils.data class maxout(nn.Module): def __init__(self, in_feature, out_feature, pool_size): super(maxout, self).__init__() self.in_feature = in_feature self.out_feature = out_feature self.pool_size = pool_size self.linear = nn.Linear(in_feature, out_feature * pool_size) def forward(self, x): output = self.linear(x) output = output.view(-1, self.out_feature, self.pool_size) output = output.max(2)[0] return output def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_feature': 4, 'out_feature': 4, 'pool_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_max_0(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp2 = triton_helpers.maximum(tmp0, tmp1) tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp6 = triton_helpers.maximum(tmp4, tmp5) tmp7 = tmp0 > tmp1 tmp8 = tmp0 == tmp1 tmp9 = tmp0 != tmp0 tmp10 = tmp1 != tmp1 tmp11 = tmp9 > tmp10 tmp12 = tmp7 | tmp11 tmp13 = tmp9 & tmp10 tmp14 = tmp8 | tmp13 tmp15 = tl.full([1], 0, tl.int64) tmp16 = tl.full([1], 1, tl.int64) tmp17 = tmp15 < tmp16 tmp18 = tmp14 & tmp17 tmp19 = tmp12 | tmp18 tmp20 = tl.where(tmp19, tmp0, tmp1) tmp21 = tl.where(tmp19, tmp15, tmp16) tmp22 = tmp20 > tmp3 tmp23 = tmp20 == tmp3 tmp24 = tmp20 != tmp20 tmp25 = tmp3 != tmp3 tmp26 = tmp24 > tmp25 tmp27 = tmp22 | tmp26 tmp28 = tmp24 & tmp25 tmp29 = tmp23 | tmp28 tmp30 = tl.full([1], 2, tl.int64) tmp31 = tmp21 < tmp30 tmp32 = tmp29 & tmp31 tmp33 = tmp27 | tmp32 tmp34 = tl.where(tmp33, tmp20, tmp3) tmp35 = tl.where(tmp33, tmp21, tmp30) tmp36 = tmp34 > tmp5 tmp37 = tmp34 == tmp5 tmp38 = tmp34 != tmp34 tmp39 = tmp5 != tmp5 tmp40 = tmp38 > tmp39 tmp41 = tmp36 | tmp40 tmp42 = tmp38 & tmp39 tmp43 = tmp37 | tmp42 tmp44 = tl.full([1], 3, tl.int64) tmp45 = tmp35 < tmp44 tmp46 = tmp43 & tmp45 tmp47 = tmp41 | tmp46 tl.where(tmp47, tmp34, tmp5) tmp49 = tl.where(tmp47, tmp35, tmp44) tl.store(out_ptr0 + x0, tmp6, xmask) tl.store(out_ptr1 + x0, tmp49, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (16, 4), (4, 1)) assert_size_stride(primals_2, (16,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 16), (16, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 16), (1, 4), 0), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32) buf2 = empty_strided_cuda((64, 4), (4, 1), torch.int64) get_raw_stream(0) triton_poi_fused_max_0[grid(256)](buf0, buf1, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf0 return buf1, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf2, (64, 4, 1), (4, 1, 1), 0) class maxoutNew(nn.Module): def __init__(self, in_feature, out_feature, pool_size): super(maxoutNew, self).__init__() self.in_feature = in_feature self.out_feature = out_feature self.pool_size = pool_size self.linear = nn.Linear(in_feature, out_feature * pool_size) def forward(self, input_0): primals_1 = self.linear.weight primals_2 = self.linear.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
Diego999/Global-Encoding
maxout
false
5,068
[ "MIT" ]
1
d3a4af9459ac3192686c94de6f2693afd6083638
https://github.com/Diego999/Global-Encoding/tree/d3a4af9459ac3192686c94de6f2693afd6083638
MonomialNN
import torch import torch.nn as nn from warnings import warn class MonomialNN(nn.Module): """A network that expands its input to a given list of monomials. Its output shape will be (n_samples, n_input_units * n_degrees) :param degrees: max degree to be included, or a list of degrees that will be used :type degrees: int or list[int] or tuple[int] """ def __init__(self, degrees): super(MonomialNN, self).__init__() if isinstance(degrees, int): degrees = [d for d in range(1, degrees + 1)] self.degrees = tuple(degrees) if len(self.degrees) == 0: raise ValueError('No degrees used, check `degrees` argument again') if 0 in degrees: warn( 'One of the degrees is 0 which might introduce redundant features' ) if len(set(self.degrees)) < len(self.degrees): warn(f'Duplicate degrees found: {self.degrees}') def forward(self, x): return torch.cat([(x ** d) for d in self.degrees], dim=1) def __repr__(self): return f'{self.__class__.__name__}(degrees={self.degrees})' def __str__(self): return self.__repr__() def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'degrees': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn from warnings import warn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 16 % 16 x0 = xindex % 16 x2 = xindex // 256 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 16 * x1 + 64 * x2), tmp4 & xmask, other=0.0) tmp6 = tmp0 >= tmp3 tmp7 = tl.full([1], 8, tl.int64) tmp8 = tmp0 < tmp7 tmp9 = tmp6 & tmp8 tmp10 = tl.load(in_ptr0 + (x0 + 16 * (-4 + x1) + 64 * x2), tmp9 & xmask, other=0.0) tmp11 = tmp10 * tmp10 tmp12 = tl.full(tmp11.shape, 0.0, tmp11.dtype) tmp13 = tl.where(tmp9, tmp11, tmp12) tmp14 = tmp0 >= tmp7 tmp15 = tl.full([1], 12, tl.int64) tmp16 = tmp0 < tmp15 tmp17 = tmp14 & tmp16 tmp18 = tl.load(in_ptr0 + (x0 + 16 * (-8 + x1) + 64 * x2), tmp17 & xmask, other=0.0) tmp19 = tmp18 * tmp18 tmp20 = tmp19 * tmp18 tmp21 = tl.full(tmp20.shape, 0.0, tmp20.dtype) tmp22 = tl.where(tmp17, tmp20, tmp21) tmp23 = tmp0 >= tmp15 tl.full([1], 16, tl.int64) tmp26 = tl.load(in_ptr0 + (x0 + 16 * (-12 + x1) + 64 * x2), tmp23 & xmask, other=0.0) tmp27 = tmp26 * tmp26 tmp28 = tmp27 * tmp27 tmp29 = tl.full(tmp28.shape, 0.0, tmp28.dtype) tmp30 = tl.where(tmp23, tmp28, tmp29) tmp31 = tl.where(tmp17, tmp22, tmp30) tmp32 = tl.where(tmp9, tmp13, tmp31) tmp33 = tl.where(tmp4, tmp5, tmp32) tl.store(out_ptr0 + x3, tmp33, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 16, 4, 4), (256, 16, 4, 1), torch.float32 ) get_raw_stream(0) triton_poi_fused_cat_0[grid(1024)](arg0_1, buf0, 1024, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf0, class MonomialNNNew(nn.Module): """A network that expands its input to a given list of monomials. Its output shape will be (n_samples, n_input_units * n_degrees) :param degrees: max degree to be included, or a list of degrees that will be used :type degrees: int or list[int] or tuple[int] """ def __init__(self, degrees): super(MonomialNNNew, self).__init__() if isinstance(degrees, int): degrees = [d for d in range(1, degrees + 1)] self.degrees = tuple(degrees) if len(self.degrees) == 0: raise ValueError('No degrees used, check `degrees` argument again') if 0 in degrees: warn( 'One of the degrees is 0 which might introduce redundant features' ) if len(set(self.degrees)) < len(self.degrees): warn(f'Duplicate degrees found: {self.degrees}') def __repr__(self): return f'{self.__class__.__name__}(degrees={self.degrees})' def __str__(self): return self.__repr__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
DiffEqML/neurodiffeq
MonomialNN
false
5,069
[ "MIT" ]
1
c5e7404c47a4729578ee2149f289be0a8909d775
https://github.com/DiffEqML/neurodiffeq/tree/c5e7404c47a4729578ee2149f289be0a8909d775
SigSoftmaxV1
import torch from torch import nn import torch.nn.parallel import torch.optim import torch.utils.data import torch.utils.data.distributed def logsigsoftmax_v1(logits, dim=1): """ Computes sigsoftmax from the paper - https://arxiv.org/pdf/1805.10829.pdf """ max_values = torch.max(logits, dim, keepdim=True)[0] exp_logits_sigmoided = torch.exp(logits - max_values) * torch.sigmoid( logits) sum_exp_logits_sigmoided = exp_logits_sigmoided.sum(1, keepdim=True) log_probs = logits - max_values + torch.log(torch.sigmoid(logits) ) - torch.log(sum_exp_logits_sigmoided) return log_probs class SigSoftmaxV1(nn.Module): """ Sigmoid 加上 softmax的实现 """ def __init__(self, dim=1): super().__init__() self.dim = dim def forward(self, logits): return logsigsoftmax_v1(logits, dim=self.dim) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math from torch import nn import torch.nn.parallel import torch.optim import torch.utils.data import torch.utils.data.distributed assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_exp_log_max_mul_sigmoid_sub_sum_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl.sigmoid(tmp0) tmp10 = tl_math.log(tmp9) tmp11 = tmp8 + tmp10 tmp12 = tmp1 - tmp7 tmp13 = tl_math.exp(tmp12) tmp14 = tl.sigmoid(tmp1) tmp15 = tmp13 * tmp14 tmp16 = tmp2 - tmp7 tmp17 = tl_math.exp(tmp16) tmp18 = tl.sigmoid(tmp2) tmp19 = tmp17 * tmp18 tmp20 = tmp15 + tmp19 tmp21 = tmp4 - tmp7 tmp22 = tl_math.exp(tmp21) tmp23 = tl.sigmoid(tmp4) tmp24 = tmp22 * tmp23 tmp25 = tmp20 + tmp24 tmp26 = tmp6 - tmp7 tmp27 = tl_math.exp(tmp26) tmp28 = tl.sigmoid(tmp6) tmp29 = tmp27 * tmp28 tmp30 = tmp25 + tmp29 tmp31 = tl_math.log(tmp30) tmp32 = tmp11 - tmp31 tl.store(out_ptr0 + x3, tmp32, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_exp_log_max_mul_sigmoid_sub_sum_0[grid(256)]( arg0_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf0, def logsigsoftmax_v1(logits, dim=1): """ Computes sigsoftmax from the paper - https://arxiv.org/pdf/1805.10829.pdf """ max_values = torch.max(logits, dim, keepdim=True)[0] exp_logits_sigmoided = torch.exp(logits - max_values) * torch.sigmoid( logits) sum_exp_logits_sigmoided = exp_logits_sigmoided.sum(1, keepdim=True) log_probs = logits - max_values + torch.log(torch.sigmoid(logits) ) - torch.log(sum_exp_logits_sigmoided) return log_probs class SigSoftmaxV1New(nn.Module): """ Sigmoid 加上 softmax的实现 """ def __init__(self, dim=1): super().__init__() self.dim = dim def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
DingYuan0118/DeepEMD
SigSoftmaxV1
false
5,070
[ "MIT" ]
1
a91f77c3da16fecefa62b14aa8b2f195b0e49b84
https://github.com/DingYuan0118/DeepEMD/tree/a91f77c3da16fecefa62b14aa8b2f195b0e49b84
IoULoss
import torch import torch.nn as nn import torch.nn.functional as F class IoULoss(nn.Module): def __init__(self, weight=None, size_average=True): super(IoULoss, self).__init__() def forward(self, inputs, targets, smooth=1): inputs = F.sigmoid(inputs) inputs = inputs.view(-1) targets = targets.view(-1) intersection = (inputs * targets).sum() total = (inputs + targets).sum() union = total - intersection IoU = (intersection + smooth) / (union + smooth) return 1 - IoU def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_div_mul_rsub_sub_sum_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp2 = tl.load(in_ptr1 + r0, None) tmp1 = tl.sigmoid(tmp0) tmp3 = tmp1 * tmp2 tmp4 = tl.broadcast_to(tmp3, [RBLOCK]) tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0)) tmp7 = tmp1 + tmp2 tmp8 = tl.broadcast_to(tmp7, [RBLOCK]) tmp10 = triton_helpers.promote_to_tensor(tl.sum(tmp8, 0)) tmp11 = 1.0 tmp12 = tmp6 + tmp11 tmp13 = tmp10 - tmp6 tmp14 = tmp13 + tmp11 tmp15 = tmp12 / tmp14 tmp16 = tmp11 - tmp15 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp16, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf2 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_div_mul_rsub_sub_sum_0[grid(1)](buf2, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf2, class IoULossNew(nn.Module): def __init__(self, weight=None, size_average=True): super(IoULossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
DoggyLiu0116/MamboNet
IoULoss
false
5,071
[ "MIT" ]
1
3b708091422491f660c4bd5eb12b06ce3b8a5f79
https://github.com/DoggyLiu0116/MamboNet/tree/3b708091422491f660c4bd5eb12b06ce3b8a5f79
ContrastiveDistanceLoss
import torch import torch.nn as nn import torch.distributed from torch.nn.modules.loss import * from torch.nn.modules import * from torch.optim import * from torch.optim.lr_scheduler import * import torch.backends class ContrastiveDistanceLoss(nn.Module): """The Contrastive distance loss. @TODO: Docs. Contribution is welcome. """ def __init__(self, margin=1.0, reduction='mean'): """ Args: margin: margin parameter reduction: criterion reduction type """ super().__init__() self.margin = margin self.reduction = reduction or 'none' def forward(self, distance_pred, distance_true) ->torch.Tensor: """Forward propagation method for the contrastive loss. Args: distance_pred: predicted distances distance_true: true distances Returns: torch.Tensor: loss """ bs = len(distance_true) margin_distance = self.margin - distance_pred margin_distance = torch.clamp(margin_distance, min=0.0) loss = (1 - distance_true) * torch.pow(distance_pred, 2 ) + distance_true * torch.pow(margin_distance, 2) if self.reduction == 'mean': loss = torch.sum(loss) / 2.0 / bs elif self.reduction == 'sum': loss = torch.sum(loss) return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn import torch.distributed from torch.nn.modules.loss import * from torch.nn.modules import * from torch.optim import * from torch.optim.lr_scheduler import * import torch.backends assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_clamp_div_mul_pow_rsub_sum_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp3 = tl.load(in_ptr1 + r0, None) tmp1 = 1.0 tmp2 = tmp1 - tmp0 tmp4 = tmp3 * tmp3 tmp5 = tmp2 * tmp4 tmp6 = tmp1 - tmp3 tmp7 = 0.0 tmp8 = triton_helpers.maximum(tmp6, tmp7) tmp9 = tmp8 * tmp8 tmp10 = tmp0 * tmp9 tmp11 = tmp5 + tmp10 tmp12 = tl.broadcast_to(tmp11, [RBLOCK]) tmp14 = triton_helpers.promote_to_tensor(tl.sum(tmp12, 0)) tmp15 = 0.5 tmp16 = tmp14 * tmp15 tmp17 = 0.25 tmp18 = tmp16 * tmp17 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp18, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_clamp_div_mul_pow_rsub_sum_0[grid(1)](buf1, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, class ContrastiveDistanceLossNew(nn.Module): """The Contrastive distance loss. @TODO: Docs. Contribution is welcome. """ def __init__(self, margin=1.0, reduction='mean'): """ Args: margin: margin parameter reduction: criterion reduction type """ super().__init__() self.margin = margin self.reduction = reduction or 'none' def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
Ditwoo/catalyst
ContrastiveDistanceLoss
false
5,072
[ "Apache-2.0" ]
1
3126390f9f679ebcfedbe01707b416678a2732ac
https://github.com/Ditwoo/catalyst/tree/3126390f9f679ebcfedbe01707b416678a2732ac
AsymLoss
import torch import numpy as np import torch.nn as nn def sum_tensor(inp, axes, keepdim=False): axes = np.unique(axes).astype(int) if keepdim: for ax in axes: inp = inp.sum(int(ax), keepdim=True) else: for ax in sorted(axes, reverse=True): inp = inp.sum(int(ax)) return inp def get_tp_fp_fn(net_output, gt, axes=None, mask=None, square=False): """ net_output must be (b, c, x, y(, z))) gt must be a label map (shape (b, 1, x, y(, z)) OR shape (b, x, y(, z))) or one hot encoding (b, c, x, y(, z)) if mask is provided it must have shape (b, 1, x, y(, z))) :param net_output: :param gt: :param axes: :param mask: mask must be 1 for valid pixels and 0 for invalid pixels :param square: if True then fp, tp and fn will be squared before summation :return: """ if axes is None: axes = tuple(range(2, len(net_output.size()))) shp_x = net_output.shape shp_y = gt.shape with torch.no_grad(): if len(shp_x) != len(shp_y): gt = gt.view((shp_y[0], 1, *shp_y[1:])) if all([(i == j) for i, j in zip(net_output.shape, gt.shape)]): y_onehot = gt else: gt = gt.long() y_onehot = torch.zeros(shp_x) if net_output.device.type == 'cuda': y_onehot = y_onehot y_onehot.scatter_(1, gt, 1) tp = net_output * y_onehot fp = net_output * (1 - y_onehot) fn = (1 - net_output) * y_onehot if mask is not None: tp = torch.stack(tuple(x_i * mask[:, 0] for x_i in torch.unbind(tp, dim=1)), dim=1) fp = torch.stack(tuple(x_i * mask[:, 0] for x_i in torch.unbind(fp, dim=1)), dim=1) fn = torch.stack(tuple(x_i * mask[:, 0] for x_i in torch.unbind(fn, dim=1)), dim=1) if square: tp = tp ** 2 fp = fp ** 2 fn = fn ** 2 tp = sum_tensor(tp, axes, keepdim=False) fp = sum_tensor(fp, axes, keepdim=False) fn = sum_tensor(fn, axes, keepdim=False) return tp, fp, fn class AsymLoss(nn.Module): def __init__(self, apply_nonlin=None, batch_dice=False, do_bg=True, smooth=1.0, square=False): """ paper: https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=8573779 """ super(AsymLoss, self).__init__() self.square = square self.do_bg = do_bg self.batch_dice = batch_dice self.apply_nonlin = apply_nonlin self.smooth = smooth self.beta = 1.5 def forward(self, x, y, loss_mask=None): shp_x = x.shape if self.batch_dice: axes = [0] + list(range(2, len(shp_x))) else: axes = list(range(2, len(shp_x))) if self.apply_nonlin is not None: x = self.apply_nonlin(x) tp, fp, fn = get_tp_fp_fn(x, y, axes, loss_mask, self.square) weight = self.beta ** 2 / (1 + self.beta ** 2) asym = (tp + self.smooth) / (tp + weight * fn + (1 - weight) * fp + self.smooth) if not self.do_bg: if self.batch_dice: asym = asym[1:] else: asym = asym[:, 1:] asym = asym.mean() return -asym def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import numpy as np import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_mul_rsub_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask) tmp2 = tmp0 * tmp1 tmp3 = 1.0 tmp4 = tmp3 - tmp1 tmp5 = tmp0 * tmp4 tmp6 = tmp3 - tmp0 tmp7 = tmp6 * tmp1 tl.store(out_ptr0 + x0, tmp2, xmask) tl.store(out_ptr1 + x0, tmp5, xmask) tl.store(out_ptr2 + x0, tmp7, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_rsub_0[grid(256)](arg0_1, arg1_1, buf0, buf1, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf0, buf1, buf2 def sum_tensor(inp, axes, keepdim=False): axes = np.unique(axes).astype(int) if keepdim: for ax in axes: inp = inp.sum(int(ax), keepdim=True) else: for ax in sorted(axes, reverse=True): inp = inp.sum(int(ax)) return inp def get_tp_fp_fn(net_output, gt, axes=None, mask=None, square=False): """ net_output must be (b, c, x, y(, z))) gt must be a label map (shape (b, 1, x, y(, z)) OR shape (b, x, y(, z))) or one hot encoding (b, c, x, y(, z)) if mask is provided it must have shape (b, 1, x, y(, z))) :param net_output: :param gt: :param axes: :param mask: mask must be 1 for valid pixels and 0 for invalid pixels :param square: if True then fp, tp and fn will be squared before summation :return: """ if axes is None: axes = tuple(range(2, len(net_output.size()))) shp_x = net_output.shape shp_y = gt.shape with torch.no_grad(): if len(shp_x) != len(shp_y): gt = gt.view((shp_y[0], 1, *shp_y[1:])) if all([(i == j) for i, j in zip(net_output.shape, gt.shape)]): y_onehot = gt else: gt = gt.long() y_onehot = torch.zeros(shp_x) if net_output.device.type == 'cuda': y_onehot = y_onehot y_onehot.scatter_(1, gt, 1) tp = net_output * y_onehot fp = net_output * (1 - y_onehot) fn = (1 - net_output) * y_onehot if mask is not None: tp = torch.stack(tuple(x_i * mask[:, 0] for x_i in torch.unbind(tp, dim=1)), dim=1) fp = torch.stack(tuple(x_i * mask[:, 0] for x_i in torch.unbind(fp, dim=1)), dim=1) fn = torch.stack(tuple(x_i * mask[:, 0] for x_i in torch.unbind(fn, dim=1)), dim=1) if square: tp = tp ** 2 fp = fp ** 2 fn = fn ** 2 tp = sum_tensor(tp, axes, keepdim=False) fp = sum_tensor(fp, axes, keepdim=False) fn = sum_tensor(fn, axes, keepdim=False) return tp, fp, fn class AsymLossNew(nn.Module): def __init__(self, apply_nonlin=None, batch_dice=False, do_bg=True, smooth=1.0, square=False): """ paper: https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=8573779 """ super(AsymLossNew, self).__init__() self.square = square self.do_bg = do_bg self.batch_dice = batch_dice self.apply_nonlin = apply_nonlin self.smooth = smooth self.beta = 1.5 def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
DoggyLiu0116/MamboNet
AsymLoss
false
5,073
[ "MIT" ]
1
3b708091422491f660c4bd5eb12b06ce3b8a5f79
https://github.com/DoggyLiu0116/MamboNet/tree/3b708091422491f660c4bd5eb12b06ce3b8a5f79
LeakyReLU
import torch import numpy as np import torch.nn as nn from numbers import Number def normcdf(value, mu=0.0, stddev=1.0): sinv = 1.0 / stddev if isinstance(stddev, Number) else stddev.reciprocal() return 0.5 * (1.0 + torch.erf((value - mu) * sinv / np.sqrt(2.0))) def _normal_log_pdf(value, mu, stddev): var = stddev ** 2 log_scale = np.log(stddev) if isinstance(stddev, Number) else torch.log( stddev) return -(value - mu) ** 2 / (2.0 * var) - log_scale - np.log(np.sqrt( 2.0 * np.pi)) def normpdf(value, mu=0.0, stddev=1.0): return torch.exp(_normal_log_pdf(value, mu, stddev)) def keep_variance_fn(x): return x + 0.001 class LeakyReLU(nn.Module): def __init__(self, negative_slope=0.01, keep_variance_fn=None): super(LeakyReLU, self).__init__() self._keep_variance_fn = keep_variance_fn self._negative_slope = negative_slope def forward(self, features_mean, features_variance): features_stddev = torch.sqrt(features_variance) div = features_mean / features_stddev pdf = normpdf(div) cdf = normcdf(div) negative_cdf = 1.0 - cdf mu_cdf = features_mean * cdf stddev_pdf = features_stddev * pdf squared_mean_variance = features_mean ** 2 + features_variance mean_stddev_pdf = features_mean * stddev_pdf mean_r = mu_cdf + stddev_pdf variance_r = (squared_mean_variance * cdf + mean_stddev_pdf - mean_r ** 2) mean_n = -features_mean * negative_cdf + stddev_pdf variance_n = (squared_mean_variance * negative_cdf - mean_stddev_pdf - mean_n ** 2) covxy = -mean_r * mean_n outputs_mean = mean_r - self._negative_slope * mean_n outputs_variance = (variance_r + self._negative_slope * self. _negative_slope * variance_n - 2.0 * self._negative_slope * covxy) if self._keep_variance_fn is not None: outputs_variance = self._keep_variance_fn(outputs_variance) return outputs_mean, outputs_variance def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import numpy as np import torch.nn as nn from numbers import Number assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_div_erf_exp_log_mul_neg_pow_rsub_sqrt_sub_0( in_out_ptr0, in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask) tmp2 = libdevice.sqrt(tmp1) tmp3 = tmp0 / tmp2 tmp4 = 0.0 tmp5 = tmp3 - tmp4 tmp6 = 1.0 tmp7 = tmp5 * tmp6 tmp8 = 1.414213562373095 tmp9 = tmp7 / tmp8 tmp10 = libdevice.erf(tmp9) tmp11 = tmp10 + tmp6 tmp12 = 0.5 tmp13 = tmp11 * tmp12 tmp14 = tmp0 * tmp13 tmp15 = tmp5 * tmp5 tmp16 = -tmp15 tmp17 = tmp16 * tmp12 tmp18 = 0.9189385332046727 tmp19 = tmp17 - tmp18 tmp20 = tl_math.exp(tmp19) tmp21 = tmp2 * tmp20 tmp22 = tmp14 + tmp21 tmp23 = -tmp0 tmp24 = tmp6 - tmp13 tmp25 = tmp23 * tmp24 tmp26 = tmp25 + tmp21 tmp27 = 0.01 tmp28 = tmp26 * tmp27 tmp29 = tmp22 - tmp28 tmp30 = tmp0 * tmp0 tmp31 = tmp30 + tmp1 tmp32 = tmp31 * tmp13 tmp33 = tmp0 * tmp21 tmp34 = tmp32 + tmp33 tmp35 = tmp22 * tmp22 tmp36 = tmp34 - tmp35 tmp37 = tmp31 * tmp24 tmp38 = tmp37 - tmp33 tmp39 = tmp26 * tmp26 tmp40 = tmp38 - tmp39 tmp41 = -tmp22 tmp42 = tmp41 * tmp26 tmp43 = 0.0001 tmp44 = tmp40 * tmp43 tmp45 = tmp36 + tmp44 tmp46 = 0.02 tmp47 = tmp42 * tmp46 tmp48 = tmp45 - tmp47 tl.store(out_ptr0 + x0, tmp29, xmask) tl.store(in_out_ptr0 + x0, tmp48, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf4 = buf1 del buf1 get_raw_stream(0) triton_poi_fused_add_div_erf_exp_log_mul_neg_pow_rsub_sqrt_sub_0[grid (256)](buf4, arg1_1, arg0_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf0, buf4 def normcdf(value, mu=0.0, stddev=1.0): sinv = 1.0 / stddev if isinstance(stddev, Number) else stddev.reciprocal() return 0.5 * (1.0 + torch.erf((value - mu) * sinv / np.sqrt(2.0))) def _normal_log_pdf(value, mu, stddev): var = stddev ** 2 log_scale = np.log(stddev) if isinstance(stddev, Number) else torch.log( stddev) return -(value - mu) ** 2 / (2.0 * var) - log_scale - np.log(np.sqrt( 2.0 * np.pi)) def normpdf(value, mu=0.0, stddev=1.0): return torch.exp(_normal_log_pdf(value, mu, stddev)) def keep_variance_fn(x): return x + 0.001 class LeakyReLUNew(nn.Module): def __init__(self, negative_slope=0.01, keep_variance_fn=None): super(LeakyReLUNew, self).__init__() self._keep_variance_fn = keep_variance_fn self._negative_slope = negative_slope def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0], output[1]
DoggyLiu0116/MamboNet
LeakyReLU
false
5,074
[ "MIT" ]
1
3b708091422491f660c4bd5eb12b06ce3b8a5f79
https://github.com/DoggyLiu0116/MamboNet/tree/3b708091422491f660c4bd5eb12b06ce3b8a5f79
LayerScale
import torch from torch import nn class LayerScale(nn.Module): """Layer scale from [Touvron et al 2021] (https://arxiv.org/pdf/2103.17239.pdf). This rescales diagonaly residual outputs close to 0 initially, then learnt. """ def __init__(self, channels: 'int', init: 'float'=0): super().__init__() self.scale = nn.Parameter(torch.zeros(channels, requires_grad=True)) self.scale.data[:] = init def forward(self, x): return self.scale[:, None] * x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'channels': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_mul_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 % 4 x3 = xindex tmp0 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x3, xmask) tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x3, tmp2, xmask) def call(args): primals_1, primals_2 = args args.clear() assert_size_stride(primals_1, (4,), (1,)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_0[grid(256)](primals_1, primals_2, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 return buf0, primals_2 class LayerScaleNew(nn.Module): """Layer scale from [Touvron et al 2021] (https://arxiv.org/pdf/2103.17239.pdf). This rescales diagonaly residual outputs close to 0 initially, then learnt. """ def __init__(self, channels: 'int', init: 'float'=0): super().__init__() self.scale = nn.Parameter(torch.zeros(channels, requires_grad=True)) self.scale.data[:] = init def forward(self, input_0): primals_1 = self.scale primals_2 = input_0 output = call([primals_1, primals_2]) return output[0]
DilwoarH/demucs
LayerScale
false
5,075
[ "MIT" ]
1
32d21592dfa015468aa117cace52b21e7af79d71
https://github.com/DilwoarH/demucs/tree/32d21592dfa015468aa117cace52b21e7af79d71
ContrastiveEmbeddingLoss
import torch import torch.nn as nn import torch.distributed from torch.nn.modules.loss import * from torch.nn.modules import * from torch.optim import * from torch.optim.lr_scheduler import * import torch.backends class ContrastiveEmbeddingLoss(nn.Module): """The Contrastive embedding loss. It has been proposed in `Dimensionality Reduction by Learning an Invariant Mapping`_. .. _Dimensionality Reduction by Learning an Invariant Mapping: http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf """ def __init__(self, margin=1.0, reduction='mean'): """ Args: margin: margin parameter reduction: criterion reduction type """ super().__init__() self.margin = margin self.reduction = reduction or 'none' def forward(self, embeddings_left: 'torch.Tensor', embeddings_right: 'torch.Tensor', distance_true) ->torch.Tensor: """Forward propagation method for the contrastive loss. Args: embeddings_left: left objects embeddings embeddings_right: right objects embeddings distance_true: true distances Returns: torch.Tensor: loss """ diff = embeddings_left - embeddings_right distance_pred = torch.sqrt(torch.sum(torch.pow(diff, 2), 1)) bs = len(distance_true) margin_distance = self.margin - distance_pred margin_distance = torch.clamp(margin_distance, min=0.0) loss = (1 - distance_true) * torch.pow(distance_pred, 2 ) + distance_true * torch.pow(margin_distance, 2) if self.reduction == 'mean': loss = torch.sum(loss) / 2.0 / bs elif self.reduction == 'sum': loss = torch.sum(loss) return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand( [4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.distributed from torch.nn.modules.loss import * from torch.nn.modules import * from torch.optim import * from torch.optim.lr_scheduler import * import torch.backends assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_pow_sqrt_sub_sum_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 16 x1 = xindex // 16 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask) tmp1 = tl.load(in_ptr1 + (x0 + 64 * x1), xmask) tmp4 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask) tmp5 = tl.load(in_ptr1 + (16 + x0 + 64 * x1), xmask) tmp9 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask) tmp10 = tl.load(in_ptr1 + (32 + x0 + 64 * x1), xmask) tmp14 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask) tmp15 = tl.load(in_ptr1 + (48 + x0 + 64 * x1), xmask) tmp2 = tmp0 - tmp1 tmp3 = tmp2 * tmp2 tmp6 = tmp4 - tmp5 tmp7 = tmp6 * tmp6 tmp8 = tmp3 + tmp7 tmp11 = tmp9 - tmp10 tmp12 = tmp11 * tmp11 tmp13 = tmp8 + tmp12 tmp16 = tmp14 - tmp15 tmp17 = tmp16 * tmp16 tmp18 = tmp13 + tmp17 tmp19 = libdevice.sqrt(tmp18) tl.store(out_ptr0 + x2, tmp19, xmask) @triton.jit def triton_per_fused_add_clamp_div_mul_pow_rsub_sum_1(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r2 = rindex r0 = rindex % 64 tmp0 = tl.load(in_ptr0 + r2, None) tmp3 = tl.load(in_ptr1 + r0, None, eviction_policy='evict_last') tmp1 = 1.0 tmp2 = tmp1 - tmp0 tmp4 = tmp3 * tmp3 tmp5 = tmp2 * tmp4 tmp6 = tmp1 - tmp3 tmp7 = 0.0 tmp8 = triton_helpers.maximum(tmp6, tmp7) tmp9 = tmp8 * tmp8 tmp10 = tmp0 * tmp9 tmp11 = tmp5 + tmp10 tmp12 = tl.broadcast_to(tmp11, [RBLOCK]) tmp14 = triton_helpers.promote_to_tensor(tl.sum(tmp12, 0)) tmp15 = 0.5 tmp16 = tmp14 * tmp15 tmp17 = 0.25 tmp18 = tmp16 * tmp17 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp18, None) def call(args): arg0_1, arg1_1, arg2_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_pow_sqrt_sub_sum_0[grid(64)](arg0_1, arg1_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 del arg1_1 buf1 = empty_strided_cuda((), (), torch.float32) buf2 = buf1 del buf1 triton_per_fused_add_clamp_div_mul_pow_rsub_sum_1[grid(1)](buf2, arg2_1, buf0, 1, 256, num_warps=2, num_stages=1) del arg2_1 del buf0 return buf2, class ContrastiveEmbeddingLossNew(nn.Module): """The Contrastive embedding loss. It has been proposed in `Dimensionality Reduction by Learning an Invariant Mapping`_. .. _Dimensionality Reduction by Learning an Invariant Mapping: http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf """ def __init__(self, margin=1.0, reduction='mean'): """ Args: margin: margin parameter reduction: criterion reduction type """ super().__init__() self.margin = margin self.reduction = reduction or 'none' def forward(self, input_0, input_1, input_2): arg0_1 = input_0 arg1_1 = input_1 arg2_1 = input_2 output = call([arg0_1, arg1_1, arg2_1]) return output[0]
Ditwoo/catalyst
ContrastiveEmbeddingLoss
false
5,076
[ "Apache-2.0" ]
1
3126390f9f679ebcfedbe01707b416678a2732ac
https://github.com/Ditwoo/catalyst/tree/3126390f9f679ebcfedbe01707b416678a2732ac
SigSoftmaxV2
import torch from torch import nn import torch.nn.parallel import torch.optim import torch.utils.data import torch.utils.data.distributed def logsigsoftmax_v2(logits, dim=1): """ v 1与 v2 差别在于 pytorch 计算softmax时有一个中心化的过程,v1 与 v2 实质上应该等同 """ sigmoid_logits = logits.sigmoid().log() sigsoftmax_logits = logits + sigmoid_logits return sigsoftmax_logits.log_softmax(dim=dim) class SigSoftmaxV2(nn.Module): def __init__(self, dim=1): super().__init__() self.dim = dim def forward(self, logits): return logsigsoftmax_v2(logits, dim=self.dim) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math from torch import nn import torch.nn.parallel import torch.optim import torch.utils.data import torch.utils.data.distributed assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused__log_softmax_add_log_sigmoid_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp4 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp8 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp13 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp18 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp1 = tl.sigmoid(tmp0) tmp2 = tl_math.log(tmp1) tmp3 = tmp0 + tmp2 tmp5 = tl.sigmoid(tmp4) tmp6 = tl_math.log(tmp5) tmp7 = tmp4 + tmp6 tmp9 = tl.sigmoid(tmp8) tmp10 = tl_math.log(tmp9) tmp11 = tmp8 + tmp10 tmp12 = triton_helpers.maximum(tmp7, tmp11) tmp14 = tl.sigmoid(tmp13) tmp15 = tl_math.log(tmp14) tmp16 = tmp13 + tmp15 tmp17 = triton_helpers.maximum(tmp12, tmp16) tmp19 = tl.sigmoid(tmp18) tmp20 = tl_math.log(tmp19) tmp21 = tmp18 + tmp20 tmp22 = triton_helpers.maximum(tmp17, tmp21) tmp23 = tmp3 - tmp22 tl.store(out_ptr0 + x3, tmp23, xmask) @triton.jit def triton_poi_fused__log_softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl_math.exp(tmp1) tmp4 = tl_math.exp(tmp3) tmp5 = tmp2 + tmp4 tmp7 = tl_math.exp(tmp6) tmp8 = tmp5 + tmp7 tmp10 = tl_math.exp(tmp9) tmp11 = tmp8 + tmp10 tmp12 = tl_math.log(tmp11) tmp13 = tmp0 - tmp12 tl.store(out_ptr0 + x3, tmp13, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused__log_softmax_add_log_sigmoid_0[grid(256)](arg0_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__log_softmax_1[grid(256)](buf0, buf1, 256, XBLOCK= 256, num_warps=4, num_stages=1) del buf0 return buf1, def logsigsoftmax_v2(logits, dim=1): """ v 1与 v2 差别在于 pytorch 计算softmax时有一个中心化的过程,v1 与 v2 实质上应该等同 """ sigmoid_logits = logits.sigmoid().log() sigsoftmax_logits = logits + sigmoid_logits return sigsoftmax_logits.log_softmax(dim=dim) class SigSoftmaxV2New(nn.Module): def __init__(self, dim=1): super().__init__() self.dim = dim def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
DingYuan0118/DeepEMD
SigSoftmaxV2
false
5,077
[ "MIT" ]
1
a91f77c3da16fecefa62b14aa8b2f195b0e49b84
https://github.com/DingYuan0118/DeepEMD/tree/a91f77c3da16fecefa62b14aa8b2f195b0e49b84
SimpleCNN
import torch import torch.nn.functional as F class Model(torch.nn.Module): def __init__(self): pass class SimpleCNN(Model): def __init__(self): super(Model, self).__init__() self.conv1 = torch.nn.Conv2d(in_channels=1, out_channels=64, kernel_size=3, stride=1, padding=1) self.pool = torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=0) self.conv2 = torch.nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1) self.pool2 = torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=0) self.conv3 = torch.nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1) self.pool3 = torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=0) self.fc1 = torch.nn.Linear(256 * 3 * 3, 2048) self.fc2 = torch.nn.Linear(2048, 10) def forward(self, x): x = F.relu(self.conv1(x)) x = self.pool(x) x = F.relu(self.conv2(x)) x = self.pool2(x) x = F.relu(self.conv3(x)) x = self.pool3(x) x = x.view(-1, 256 * 3 * 3) x = F.relu(self.fc1(x)) x = self.fc2(x) return x def get_inputs(): return [torch.rand([4, 1, 24, 24])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 9 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 64 y1 = yindex // 64 tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last' ) tl.store(out_ptr0 + (y0 + 64 * x2 + 576 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 9 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 128 y1 = yindex // 128 tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last' ) tl.store(out_ptr0 + (y0 + 128 * x2 + 1152 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_convolution_relu_2(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 256 xnumel = 576 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 64 y1 = yindex // 64 tmp0 = tl.load(in_ptr0 + (x2 + 576 * y3), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1, 1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(out_ptr0 + (y0 + 64 * x2 + 36864 * y1), tmp4, xmask & ymask) @triton.jit def triton_poi_fused_max_pool2d_with_indices_3(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x0 = xindex % 64 x1 = xindex // 64 % 12 x2 = xindex // 768 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 128 * x1 + 3072 * x2), None) tmp1 = tl.load(in_ptr0 + (64 + x0 + 128 * x1 + 3072 * x2), None) tmp3 = tl.load(in_ptr0 + (1536 + x0 + 128 * x1 + 3072 * x2), None) tmp5 = tl.load(in_ptr0 + (1600 + x0 + 128 * x1 + 3072 * x2), None) tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + x3, tmp6, None) tl.store(out_ptr1 + x3, tmp16, None) @triton.jit def triton_poi_fused_convolution_relu_4(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 128 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_5(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x0 = xindex % 128 x1 = xindex // 128 % 6 x2 = xindex // 768 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 256 * x1 + 3072 * x2), None) tmp1 = tl.load(in_ptr0 + (128 + x0 + 256 * x1 + 3072 * x2), None) tmp3 = tl.load(in_ptr0 + (1536 + x0 + 256 * x1 + 3072 * x2), None) tmp5 = tl.load(in_ptr0 + (1664 + x0 + 256 * x1 + 3072 * x2), None) tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + x3, tmp6, None) tl.store(out_ptr1 + x3, tmp16, None) @triton.jit def triton_poi_fused_convolution_relu_6(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 256 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_7(in_ptr0, out_ptr0, out_ptr1, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 36 xnumel = 256 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 3 y1 = yindex // 3 y5 = yindex y4 = yindex // 9 y6 = yindex % 9 tmp0 = tl.load(in_ptr0 + (x2 + 512 * y0 + 3072 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (256 + x2 + 512 * y0 + 3072 * y1), xmask & ymask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (1536 + x2 + 512 * y0 + 3072 * y1), xmask & ymask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (1792 + x2 + 512 * y0 + 3072 * y1), xmask & ymask, eviction_policy='evict_last') tmp2 = tmp1 > tmp0 tmp3 = tl.full([1, 1], 1, tl.int8) tmp4 = tl.full([1, 1], 0, tl.int8) tmp5 = tl.where(tmp2, tmp3, tmp4) tmp6 = triton_helpers.maximum(tmp1, tmp0) tmp8 = tmp7 > tmp6 tmp9 = tl.full([1, 1], 2, tl.int8) tmp10 = tl.where(tmp8, tmp9, tmp5) tmp11 = triton_helpers.maximum(tmp7, tmp6) tmp13 = tmp12 > tmp11 tmp14 = tl.full([1, 1], 3, tl.int8) tmp15 = tl.where(tmp13, tmp14, tmp10) tmp16 = triton_helpers.maximum(tmp12, tmp11) tl.store(out_ptr0 + (x2 + 256 * y5), tmp15, xmask & ymask) tl.store(out_ptr1 + (y6 + 9 * x2 + 2304 * y4), tmp16, xmask & ymask) @triton.jit def triton_poi_fused_relu_8(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 2048 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, None) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11) = args args.clear() assert_size_stride(primals_1, (64, 1, 3, 3), (9, 9, 3, 1)) assert_size_stride(primals_2, (64,), (1,)) assert_size_stride(primals_3, (4, 1, 24, 24), (576, 576, 24, 1)) assert_size_stride(primals_4, (128, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_5, (128,), (1,)) assert_size_stride(primals_6, (256, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_7, (256,), (1,)) assert_size_stride(primals_8, (2048, 2304), (2304, 1)) assert_size_stride(primals_9, (2048,), (1,)) assert_size_stride(primals_10, (10, 2048), (2048, 1)) assert_size_stride(primals_11, (10,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((128, 64, 3, 3), (576, 1, 192, 64), torch .float32) get_raw_stream(0) triton_poi_fused_0[grid(8192, 9)](primals_4, buf0, 8192, 9, XBLOCK= 16, YBLOCK=64, num_warps=4, num_stages=1) del primals_4 buf1 = empty_strided_cuda((256, 128, 3, 3), (1152, 1, 384, 128), torch.float32) triton_poi_fused_1[grid(32768, 9)](primals_6, buf1, 32768, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_6 buf2 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 64, 24, 24), (36864, 576, 24, 1)) buf3 = empty_strided_cuda((4, 64, 24, 24), (36864, 1, 1536, 64), torch.float32) triton_poi_fused_convolution_relu_2[grid(256, 576)](buf2, primals_2, buf3, 256, 576, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1) del buf2 del primals_2 buf4 = empty_strided_cuda((4, 64, 12, 12), (9216, 1, 768, 64), torch.float32) buf5 = empty_strided_cuda((4, 64, 12, 12), (9216, 1, 768, 64), torch.int8) triton_poi_fused_max_pool2d_with_indices_3[grid(36864)](buf3, buf4, buf5, 36864, XBLOCK=512, num_warps=4, num_stages=1) buf6 = extern_kernels.convolution(buf4, buf0, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf6, (4, 128, 12, 12), (18432, 1, 1536, 128)) buf7 = buf6 del buf6 triton_poi_fused_convolution_relu_4[grid(73728)](buf7, primals_5, 73728, XBLOCK=1024, num_warps=4, num_stages=1) del primals_5 buf8 = empty_strided_cuda((4, 128, 6, 6), (4608, 1, 768, 128), torch.float32) buf9 = empty_strided_cuda((4, 128, 6, 6), (4608, 1, 768, 128), torch.int8) triton_poi_fused_max_pool2d_with_indices_5[grid(18432)](buf7, buf8, buf9, 18432, XBLOCK=256, num_warps=4, num_stages=1) buf10 = extern_kernels.convolution(buf8, buf1, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf10, (4, 256, 6, 6), (9216, 1, 1536, 256)) buf11 = buf10 del buf10 triton_poi_fused_convolution_relu_6[grid(36864)](buf11, primals_7, 36864, XBLOCK=512, num_warps=4, num_stages=1) del primals_7 buf12 = empty_strided_cuda((4, 256, 3, 3), (2304, 1, 768, 256), torch.int8) buf13 = empty_strided_cuda((4, 256, 3, 3), (2304, 9, 3, 1), torch. float32) triton_poi_fused_max_pool2d_with_indices_7[grid(36, 256)](buf11, buf12, buf13, 36, 256, XBLOCK=4, YBLOCK=64, num_warps=4, num_stages=1) buf14 = empty_strided_cuda((4, 2048), (2048, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf13, (4, 2304), (2304, 1), 0 ), reinterpret_tensor(primals_8, (2304, 2048), (1, 2304), 0), out=buf14) buf15 = buf14 del buf14 triton_poi_fused_relu_8[grid(8192)](buf15, primals_9, 8192, XBLOCK= 256, num_warps=4, num_stages=1) del primals_9 buf16 = empty_strided_cuda((4, 10), (10, 1), torch.float32) extern_kernels.addmm(primals_11, buf15, reinterpret_tensor( primals_10, (2048, 10), (1, 2048), 0), alpha=1, beta=1, out=buf16) del primals_11 return (buf16, primals_1, primals_3, buf0, buf1, buf3, buf4, buf5, buf7, buf8, buf9, buf11, buf12, reinterpret_tensor(buf13, (4, 2304), ( 2304, 1), 0), buf15, primals_10, primals_8) class Model(torch.nn.Module): def __init__(self): pass class SimpleCNNNew(Model): def __init__(self): super(Model, self).__init__() self.conv1 = torch.nn.Conv2d(in_channels=1, out_channels=64, kernel_size=3, stride=1, padding=1) self.pool = torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=0) self.conv2 = torch.nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1) self.pool2 = torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=0) self.conv3 = torch.nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1) self.pool3 = torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=0) self.fc1 = torch.nn.Linear(256 * 3 * 3, 2048) self.fc2 = torch.nn.Linear(2048, 10) def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_6 = self.conv3.weight primals_7 = self.conv3.bias primals_8 = self.fc1.weight primals_9 = self.fc1.bias primals_10 = self.fc2.weight primals_11 = self.fc2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11]) return output[0]
Cuilie/Collect-feature-maps
SimpleCNN
false
5,078
[ "MIT" ]
1
32e8ac59690837f2a299ab6d4c11b98f5d3d721a
https://github.com/Cuilie/Collect-feature-maps/tree/32e8ac59690837f2a299ab6d4c11b98f5d3d721a
ContrastivePairwiseEmbeddingLoss
import torch import torch.nn as nn import torch.distributed import torch.nn.functional as F from torch.nn.modules.loss import * from torch.nn.modules import * from torch.optim import * from torch.optim.lr_scheduler import * import torch.backends class ContrastivePairwiseEmbeddingLoss(nn.Module): """ContrastivePairwiseEmbeddingLoss – proof of concept criterion. Still work in progress. @TODO: Docs. Contribution is welcome. """ def __init__(self, margin=1.0, reduction='mean'): """ Args: margin: margin parameter reduction: criterion reduction type """ super().__init__() self.margin = margin self.reduction = reduction or 'none' def forward(self, embeddings_pred, embeddings_true) ->torch.Tensor: """Forward propagation method for the contrastive loss. Work in progress. Args: embeddings_pred: predicted embeddings embeddings_true: true embeddings Returns: torch.Tensor: loss """ device = embeddings_pred.device pairwise_similarity = torch.einsum('se,ae->sa', embeddings_pred, embeddings_true) bs = embeddings_pred.shape[0] batch_idx = torch.arange(bs, device=device) loss = F.cross_entropy(pairwise_similarity, batch_idx, reduction= self.reduction) return loss def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn import torch.distributed from torch.nn.modules.loss import * from torch.nn.modules import * from torch.optim import * from torch.optim.lr_scheduler import * import torch.backends assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused__log_softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_per_fused_arange_nll_loss_forward_1(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 4 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp6 = tl.load(in_ptr0 + 4 * r0, None, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (1 + 4 * r0), None, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (2 + 4 * r0), None, eviction_policy='evict_last') tmp14 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last') tmp0 = r0 tmp1 = tl.full([1, 1], -100, tl.int64) tmp2 = tmp0 != tmp1 tmp3 = tl.full([1, 1], 0, tl.int64) tmp4 = tl.where(tmp2, tmp0, tmp3) tmp5 = tl.load(in_ptr0 + (tmp4 + 4 * r0), None, eviction_policy= 'evict_last') tmp7 = tl_math.exp(tmp6) tmp9 = tl_math.exp(tmp8) tmp10 = tmp7 + tmp9 tmp12 = tl_math.exp(tmp11) tmp13 = tmp10 + tmp12 tmp15 = tl_math.exp(tmp14) tmp16 = tmp13 + tmp15 tmp17 = tl_math.log(tmp16) tmp18 = tmp5 - tmp17 tmp19 = -tmp18 tmp20 = 0.0 tmp21 = tl.where(tmp2, tmp19, tmp20) tmp22 = tl.broadcast_to(tmp21, [XBLOCK, RBLOCK]) tmp24 = tl.sum(tmp22, 1)[:, None] tmp25 = tmp2.to(tl.int64) tmp26 = tl.broadcast_to(tmp25, [XBLOCK, RBLOCK]) tmp28 = tl.sum(tmp26, 1)[:, None] tmp29 = tmp28.to(tl.float32) tmp30 = tmp24 / tmp29 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp30, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4), (4, 1)) assert_size_stride(arg1_1, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((1, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(arg0_1, (1, 4, 4), (16, 4, 1), 0), reinterpret_tensor(arg1_1, (1, 4, 4), (0, 1, 4), 0), out=buf0) del arg0_1 del arg1_1 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused__log_softmax_0[grid(16)](buf0, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf0 buf2 = empty_strided_cuda((), (), torch.float32) buf4 = buf2 del buf2 triton_per_fused_arange_nll_loss_forward_1[grid(1)](buf4, buf1, 1, 4, XBLOCK=1, num_warps=2, num_stages=1) del buf1 return buf4, class ContrastivePairwiseEmbeddingLossNew(nn.Module): """ContrastivePairwiseEmbeddingLoss – proof of concept criterion. Still work in progress. @TODO: Docs. Contribution is welcome. """ def __init__(self, margin=1.0, reduction='mean'): """ Args: margin: margin parameter reduction: criterion reduction type """ super().__init__() self.margin = margin self.reduction = reduction or 'none' def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
Ditwoo/catalyst
ContrastivePairwiseEmbeddingLoss
false
5,079
[ "Apache-2.0" ]
1
3126390f9f679ebcfedbe01707b416678a2732ac
https://github.com/Ditwoo/catalyst/tree/3126390f9f679ebcfedbe01707b416678a2732ac
ShallowConvNet
import torch import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data import torch.nn.functional as F import torch.onnx class ShallowConvNet(nn.Module): def __init__(self, hidden=1000): super(ShallowConvNet, self).__init__() self.conv1 = nn.Conv2d(3, 6, 5) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) self.fc1 = nn.Linear(16 * 5 * 5, hidden) self.fc2 = nn.Linear(hidden, 10) def forward(self, x): x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self.conv2(x))) x = x.view(-1, 16 * 5 * 5) x = F.relu(self.fc1(x)) x = self.fc2(x) return x def get_inputs(): return [torch.rand([4, 3, 32, 32])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data import torch.onnx assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_relu_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 18816 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 784 % 6 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, xmask) @triton.jit def triton_poi_fused_max_pool2d_with_indices_1(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 4704 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 14 x3 = xindex // 14 x2 = xindex // 1176 x4 = xindex % 1176 tmp0 = tl.load(in_ptr0 + (2 * x0 + 56 * x3), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 56 * x3), xmask, eviction_policy ='evict_last') tmp3 = tl.load(in_ptr0 + (28 + 2 * x0 + 56 * x3), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (29 + 2 * x0 + 56 * x3), xmask, eviction_policy='evict_last') tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + (x4 + 1184 * x2), tmp6, xmask) tl.store(out_ptr1 + (x4 + 1280 * x2), tmp16, xmask) @triton.jit def triton_poi_fused_convolution_relu_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 6400 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 100 % 16 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, xmask) @triton.jit def triton_poi_fused_max_pool2d_with_indices_3(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 1600 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 5 x1 = xindex // 5 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 20 * x1), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 20 * x1), xmask, eviction_policy ='evict_last') tmp7 = tl.load(in_ptr0 + (10 + 2 * x0 + 20 * x1), xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (11 + 2 * x0 + 20 * x1), xmask, eviction_policy='evict_last') tmp2 = tmp1 > tmp0 tmp3 = tl.full([1], 1, tl.int8) tmp4 = tl.full([1], 0, tl.int8) tmp5 = tl.where(tmp2, tmp3, tmp4) tmp6 = triton_helpers.maximum(tmp1, tmp0) tmp8 = tmp7 > tmp6 tmp9 = tl.full([1], 2, tl.int8) tmp10 = tl.where(tmp8, tmp9, tmp5) tmp11 = triton_helpers.maximum(tmp7, tmp6) tmp13 = tmp12 > tmp11 tmp14 = tl.full([1], 3, tl.int8) tmp15 = tl.where(tmp13, tmp14, tmp10) tmp16 = triton_helpers.maximum(tmp12, tmp11) tl.store(out_ptr0 + x2, tmp15, xmask) tl.store(out_ptr1 + x2, tmp16, xmask) @triton.jit def triton_poi_fused_relu_4(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 4000 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 1000 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9) = args args.clear() assert_size_stride(primals_1, (6, 3, 5, 5), (75, 25, 5, 1)) assert_size_stride(primals_2, (6,), (1,)) assert_size_stride(primals_3, (4, 3, 32, 32), (3072, 1024, 32, 1)) assert_size_stride(primals_4, (16, 6, 5, 5), (150, 25, 5, 1)) assert_size_stride(primals_5, (16,), (1,)) assert_size_stride(primals_6, (1000, 400), (400, 1)) assert_size_stride(primals_7, (1000,), (1,)) assert_size_stride(primals_8, (10, 1000), (1000, 1)) assert_size_stride(primals_9, (10,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 6, 28, 28), (4704, 784, 28, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(18816)](buf1, primals_2, 18816, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((4, 6, 14, 14), (1184, 196, 14, 1), torch .float32) buf3 = empty_strided_cuda((4, 6, 14, 14), (1280, 196, 14, 1), torch .int8) triton_poi_fused_max_pool2d_with_indices_1[grid(4704)](buf1, buf2, buf3, 4704, XBLOCK=128, num_warps=4, num_stages=1) buf4 = extern_kernels.convolution(buf2, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 16, 10, 10), (1600, 100, 10, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_relu_2[grid(6400)](buf5, primals_5, 6400, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf6 = empty_strided_cuda((4, 16, 5, 5), (400, 25, 5, 1), torch.int8) buf7 = empty_strided_cuda((4, 16, 5, 5), (400, 25, 5, 1), torch.float32 ) triton_poi_fused_max_pool2d_with_indices_3[grid(1600)](buf5, buf6, buf7, 1600, XBLOCK=256, num_warps=4, num_stages=1) buf8 = empty_strided_cuda((4, 1000), (1000, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf7, (4, 400), (400, 1), 0), reinterpret_tensor(primals_6, (400, 1000), (1, 400), 0), out=buf8) buf9 = buf8 del buf8 triton_poi_fused_relu_4[grid(4000)](buf9, primals_7, 4000, XBLOCK= 256, num_warps=4, num_stages=1) del primals_7 buf10 = empty_strided_cuda((4, 10), (10, 1), torch.float32) extern_kernels.addmm(primals_9, buf9, reinterpret_tensor(primals_8, (1000, 10), (1, 1000), 0), alpha=1, beta=1, out=buf10) del primals_9 return (buf10, primals_1, primals_3, primals_4, buf1, buf2, buf3, buf5, buf6, reinterpret_tensor(buf7, (4, 400), (400, 1), 0), buf9, primals_8, primals_6) class ShallowConvNetNew(nn.Module): def __init__(self, hidden=1000): super(ShallowConvNetNew, self).__init__() self.conv1 = nn.Conv2d(3, 6, 5) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) self.fc1 = nn.Linear(16 * 5 * 5, hidden) self.fc2 = nn.Linear(hidden, 10) def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_6 = self.fc1.weight primals_7 = self.fc1.bias primals_8 = self.fc2.weight primals_9 = self.fc2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9]) return output[0]
CorentinChauvin/style-transfer-KD
ShallowConvNet
false
5,080
[ "MIT" ]
1
87bcb2963dbb8d09faf94c74a744f358cafe5427
https://github.com/CorentinChauvin/style-transfer-KD/tree/87bcb2963dbb8d09faf94c74a744f358cafe5427
ReLU
import torch import numpy as np import torch.nn as nn from numbers import Number def normcdf(value, mu=0.0, stddev=1.0): sinv = 1.0 / stddev if isinstance(stddev, Number) else stddev.reciprocal() return 0.5 * (1.0 + torch.erf((value - mu) * sinv / np.sqrt(2.0))) def _normal_log_pdf(value, mu, stddev): var = stddev ** 2 log_scale = np.log(stddev) if isinstance(stddev, Number) else torch.log( stddev) return -(value - mu) ** 2 / (2.0 * var) - log_scale - np.log(np.sqrt( 2.0 * np.pi)) def normpdf(value, mu=0.0, stddev=1.0): return torch.exp(_normal_log_pdf(value, mu, stddev)) def keep_variance_fn(x): return x + 0.001 class ReLU(nn.Module): def __init__(self, keep_variance_fn=None): super(ReLU, self).__init__() self._keep_variance_fn = keep_variance_fn def forward(self, features_mean, features_variance): features_stddev = torch.sqrt(features_variance) div = features_mean / features_stddev pdf = normpdf(div) cdf = normcdf(div) outputs_mean = features_mean * cdf + features_stddev * pdf outputs_variance = (features_mean ** 2 + features_variance ) * cdf + features_mean * features_stddev * pdf - outputs_mean ** 2 if self._keep_variance_fn is not None: outputs_variance = self._keep_variance_fn(outputs_variance) return outputs_mean, outputs_variance def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import numpy as np import torch.nn as nn from numbers import Number assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_div_erf_exp_log_mul_neg_pow_sqrt_sub_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask) tmp2 = libdevice.sqrt(tmp1) tmp3 = tmp0 / tmp2 tmp4 = 0.0 tmp5 = tmp3 - tmp4 tmp6 = 1.0 tmp7 = tmp5 * tmp6 tmp8 = 1.414213562373095 tmp9 = tmp7 / tmp8 tmp10 = libdevice.erf(tmp9) tmp11 = tmp10 + tmp6 tmp12 = 0.5 tmp13 = tmp11 * tmp12 tmp14 = tmp0 * tmp13 tmp15 = tmp5 * tmp5 tmp16 = -tmp15 tmp17 = tmp16 * tmp12 tmp18 = 0.9189385332046727 tmp19 = tmp17 - tmp18 tmp20 = tl_math.exp(tmp19) tmp21 = tmp2 * tmp20 tmp22 = tmp14 + tmp21 tmp23 = tmp0 * tmp0 tmp24 = tmp23 + tmp1 tmp25 = tmp24 * tmp13 tmp26 = tmp0 * tmp2 tmp27 = tmp26 * tmp20 tmp28 = tmp25 + tmp27 tmp29 = tmp22 * tmp22 tmp30 = tmp28 - tmp29 tl.store(out_ptr0 + x0, tmp22, xmask) tl.store(out_ptr1 + x0, tmp30, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_erf_exp_log_mul_neg_pow_sqrt_sub_0[grid(256)]( arg1_1, arg0_1, buf0, buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf0, buf1 def normcdf(value, mu=0.0, stddev=1.0): sinv = 1.0 / stddev if isinstance(stddev, Number) else stddev.reciprocal() return 0.5 * (1.0 + torch.erf((value - mu) * sinv / np.sqrt(2.0))) def _normal_log_pdf(value, mu, stddev): var = stddev ** 2 log_scale = np.log(stddev) if isinstance(stddev, Number) else torch.log( stddev) return -(value - mu) ** 2 / (2.0 * var) - log_scale - np.log(np.sqrt( 2.0 * np.pi)) def normpdf(value, mu=0.0, stddev=1.0): return torch.exp(_normal_log_pdf(value, mu, stddev)) def keep_variance_fn(x): return x + 0.001 class ReLUNew(nn.Module): def __init__(self, keep_variance_fn=None): super(ReLUNew, self).__init__() self._keep_variance_fn = keep_variance_fn def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0], output[1]
DoggyLiu0116/MamboNet
ReLU
false
5,081
[ "MIT" ]
1
3b708091422491f660c4bd5eb12b06ce3b8a5f79
https://github.com/DoggyLiu0116/MamboNet/tree/3b708091422491f660c4bd5eb12b06ce3b8a5f79
JointHeatmapLoss
import torch import torch.utils.data import torch.nn as nn class JointHeatmapLoss(nn.Module): def __ini__(self): super(JointHeatmapLoss, self).__init__() def forward(self, joint_out, joint_gt, joint_valid): loss = (joint_out - joint_gt) ** 2 * joint_valid[:, :, None, None, None ] return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand( [4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.utils.data import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_mul_pow_sub_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex % 256 x0 = xindex % 16 x2 = xindex // 256 x4 = xindex tmp0 = tl.load(in_ptr0 + x3, None, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x3, None, eviction_policy='evict_last') tmp4 = tl.load(in_ptr2 + (x0 + 16 * x2), None, eviction_policy='evict_last' ) tmp2 = tmp0 - tmp1 tmp3 = tmp2 * tmp2 tmp5 = tmp3 * tmp4 tl.store(out_ptr0 + x4, tmp5, None) def call(args): arg0_1, arg1_1, arg2_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 1, 4, 4, 4, 4), (1024, 256, 256, 64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_pow_sub_0[grid(4096)](arg0_1, arg1_1, arg2_1, buf0, 4096, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 del arg1_1 del arg2_1 return buf0, class JointHeatmapLossNew(nn.Module): def __ini__(self): super(JointHeatmapLossNew, self).__init__() def forward(self, input_0, input_1, input_2): arg0_1 = input_0 arg1_1 = input_1 arg2_1 = input_2 output = call([arg0_1, arg1_1, arg2_1]) return output[0]
DuinoDu/InterHand2.6M.pl
JointHeatmapLoss
false
5,082
[ "MIT" ]
1
2d216960cf95b066a197a9b49795840b1ecfd0c1
https://github.com/DuinoDu/InterHand2.6M.pl/tree/2d216960cf95b066a197a9b49795840b1ecfd0c1
RegressionModel
import torch from torch import nn class RegressionModel(nn.Module): def __init__(self, num_features_in, num_anchors=9, feature_size=256): super(RegressionModel, self).__init__() self.conv1 = nn.Conv2d(num_features_in, feature_size, kernel_size=3, padding=1) self.act1 = nn.ReLU() self.conv2 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1) self.act2 = nn.ReLU() self.conv3 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1) self.act3 = nn.ReLU() self.conv4 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1) self.act4 = nn.ReLU() self.output = nn.Conv2d(feature_size, num_anchors * 4, kernel_size= 3, padding=1) def forward(self, x): out = self.conv1(x) out = self.act1(out) out = self.conv2(out) out = self.act2(out) out = self.conv3(out) out = self.act3(out) out = self.conv4(out) out = self.act4(out) out = self.output(out) out = out.permute(0, 2, 3, 1) return out.contiguous().view(out.shape[0], -1, 4) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_features_in': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_relu_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 16 % 256 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_clone_view_1(in_out_ptr0, in_ptr0, in_ptr1, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 64 xnumel = 36 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 16 y1 = yindex // 16 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 16 * x2 + 576 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.debug_barrier() tl.store(in_out_ptr0 + (x2 + 36 * y3), tmp2, xmask & ymask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11) = args args.clear() assert_size_stride(primals_1, (256, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_2, (256,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_5, (256,), (1,)) assert_size_stride(primals_6, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_7, (256,), (1,)) assert_size_stride(primals_8, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_9, (256,), (1,)) assert_size_stride(primals_10, (36, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_11, (36,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 256, 4, 4), (4096, 16, 4, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(16384)](buf1, primals_2, 16384, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = extern_kernels.convolution(buf1, primals_4, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 256, 4, 4), (4096, 16, 4, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_relu_0[grid(16384)](buf3, primals_5, 16384, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf4 = extern_kernels.convolution(buf3, primals_6, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 256, 4, 4), (4096, 16, 4, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_relu_0[grid(16384)](buf5, primals_7, 16384, XBLOCK=128, num_warps=4, num_stages=1) del primals_7 buf6 = extern_kernels.convolution(buf5, primals_8, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf6, (4, 256, 4, 4), (4096, 16, 4, 1)) buf7 = buf6 del buf6 triton_poi_fused_convolution_relu_0[grid(16384)](buf7, primals_9, 16384, XBLOCK=128, num_warps=4, num_stages=1) del primals_9 buf8 = extern_kernels.convolution(buf7, primals_10, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 36, 4, 4), (576, 16, 4, 1)) buf9 = empty_strided_cuda((4, 4, 4, 36), (576, 144, 36, 1), torch. float32) buf10 = reinterpret_tensor(buf9, (4, 144, 4), (576, 4, 1), 0) del buf9 triton_poi_fused_clone_view_1[grid(64, 36)](buf10, buf8, primals_11, 64, 36, XBLOCK=64, YBLOCK=4, num_warps=4, num_stages=1) del buf8 del primals_11 return (buf10, primals_1, primals_3, primals_4, primals_6, primals_8, primals_10, buf1, buf3, buf5, buf7) class RegressionModelNew(nn.Module): def __init__(self, num_features_in, num_anchors=9, feature_size=256): super(RegressionModelNew, self).__init__() self.conv1 = nn.Conv2d(num_features_in, feature_size, kernel_size=3, padding=1) self.act1 = nn.ReLU() self.conv2 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1) self.act2 = nn.ReLU() self.conv3 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1) self.act3 = nn.ReLU() self.conv4 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1) self.act4 = nn.ReLU() self.output = nn.Conv2d(feature_size, num_anchors * 4, kernel_size= 3, padding=1) def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_6 = self.conv3.weight primals_7 = self.conv3.bias primals_8 = self.conv4.weight primals_9 = self.conv4.bias primals_10 = self.output.weight primals_11 = self.output.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11]) return output[0]
DerekGloudemans/temporary-repo
RegressionModel
false
5,083
[ "MIT" ]
1
f278e9c7c9c7c1f362a64aec492ddb8fb1f984ad
https://github.com/DerekGloudemans/temporary-repo/tree/f278e9c7c9c7c1f362a64aec492ddb8fb1f984ad
AvgPool2d
import torch import torch.nn as nn import torch.nn.functional as F def keep_variance_fn(x): return x + 0.001 class AvgPool2d(nn.Module): def __init__(self, keep_variance_fn=None, kernel_size=2): super(AvgPool2d, self).__init__() self._keep_variance_fn = keep_variance_fn self.kernel_size = kernel_size def forward(self, inputs_mean, inputs_variance): outputs_mean = F.avg_pool2d(inputs_mean, self.kernel_size, stride=2, padding=1) outputs_variance = F.avg_pool2d(inputs_variance, self.kernel_size, stride=2, padding=1) outputs_variance = outputs_variance / (inputs_mean.size(2) * inputs_mean.size(3)) if self._keep_variance_fn is not None: outputs_variance = self._keep_variance_fn(outputs_variance) return outputs_mean, outputs_variance / (inputs_mean.shape[2] * inputs_mean.shape[3]) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_avg_pool2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 144 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 3 % 3 x0 = xindex % 3 x2 = xindex // 9 x4 = xindex tmp0 = -1 + 2 * x1 tmp1 = tl.full([1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tmp2 & tmp4 tmp6 = -1 + 2 * x0 tmp7 = tmp6 >= tmp1 tmp8 = tmp6 < tmp3 tmp9 = tmp7 & tmp8 tmp10 = tmp5 & tmp9 tmp11 = tl.load(in_ptr0 + (-5 + 2 * x0 + 8 * x1 + 16 * x2), tmp10 & xmask, eviction_policy='evict_last', other=0.0) tmp12 = 2 * x0 tmp13 = tmp12 >= tmp1 tmp14 = tmp12 < tmp3 tmp15 = tmp13 & tmp14 tmp16 = tmp5 & tmp15 tmp17 = tl.load(in_ptr0 + (-4 + 2 * x0 + 8 * x1 + 16 * x2), tmp16 & xmask, eviction_policy='evict_last', other=0.0) tmp18 = tmp17 + tmp11 tmp19 = 2 * x1 tmp20 = tmp19 >= tmp1 tmp21 = tmp19 < tmp3 tmp22 = tmp20 & tmp21 tmp23 = tmp22 & tmp9 tmp24 = tl.load(in_ptr0 + (-1 + 2 * x0 + 8 * x1 + 16 * x2), tmp23 & xmask, eviction_policy='evict_last', other=0.0) tmp25 = tmp24 + tmp18 tmp26 = tmp22 & tmp15 tmp27 = tl.load(in_ptr0 + (2 * x0 + 8 * x1 + 16 * x2), tmp26 & xmask, eviction_policy='evict_last', other=0.0) tmp28 = tmp27 + tmp25 tmp29 = 1 + -2 * x0 + -2 * x1 + (5 * (5 <= 1 + 2 * x0) + (1 + 2 * x0) * (1 + 2 * x0 < 5)) * (5 * (5 <= 1 + 2 * x1) + (1 + 2 * x1) * (1 + 2 * x1 < 5)) + -2 * x0 * (5 * (5 <= 1 + 2 * x1) + (1 + 2 * x1) * (1 + 2 * x1 < 5)) + -2 * x1 * (5 * (5 <= 1 + 2 * x0) + (1 + 2 * x0) * (1 + 2 * x0 < 5)) + 4 * x0 * x1 + (5 * (5 <= 1 + 2 * x0) + (1 + 2 * x0) * (1 + 2 * x0 < 5)) + (5 * (5 <= 1 + 2 * x1) + (1 + 2 * x1) * (1 + 2 * x1 < 5) ) tmp30 = tmp28 / tmp29 tl.store(out_ptr0 + x4, tmp30, xmask) @triton.jit def triton_poi_fused_avg_pool2d_div_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 144 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 3 % 3 x0 = xindex % 3 x2 = xindex // 9 x3 = xindex tmp0 = -1 + 2 * x1 tmp1 = tl.full([1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tmp2 & tmp4 tmp6 = -1 + 2 * x0 tmp7 = tmp6 >= tmp1 tmp8 = tmp6 < tmp3 tmp9 = tmp7 & tmp8 tmp10 = tmp5 & tmp9 tmp11 = tl.load(in_ptr0 + (-5 + 2 * x0 + 8 * x1 + 16 * x2), tmp10 & xmask, eviction_policy='evict_last', other=0.0) tmp12 = 2 * x0 tmp13 = tmp12 >= tmp1 tmp14 = tmp12 < tmp3 tmp15 = tmp13 & tmp14 tmp16 = tmp5 & tmp15 tmp17 = tl.load(in_ptr0 + (-4 + 2 * x0 + 8 * x1 + 16 * x2), tmp16 & xmask, eviction_policy='evict_last', other=0.0) tmp18 = tmp17 + tmp11 tmp19 = 2 * x1 tmp20 = tmp19 >= tmp1 tmp21 = tmp19 < tmp3 tmp22 = tmp20 & tmp21 tmp23 = tmp22 & tmp9 tmp24 = tl.load(in_ptr0 + (-1 + 2 * x0 + 8 * x1 + 16 * x2), tmp23 & xmask, eviction_policy='evict_last', other=0.0) tmp25 = tmp24 + tmp18 tmp26 = tmp22 & tmp15 tmp27 = tl.load(in_ptr0 + (2 * x0 + 8 * x1 + 16 * x2), tmp26 & xmask, eviction_policy='evict_last', other=0.0) tmp28 = tmp27 + tmp25 tmp29 = 1 + -2 * x0 + -2 * x1 + (5 * (5 <= 1 + 2 * x0) + (1 + 2 * x0) * (1 + 2 * x0 < 5)) * (5 * (5 <= 1 + 2 * x1) + (1 + 2 * x1) * (1 + 2 * x1 < 5)) + -2 * x0 * (5 * (5 <= 1 + 2 * x1) + (1 + 2 * x1) * (1 + 2 * x1 < 5)) + -2 * x1 * (5 * (5 <= 1 + 2 * x0) + (1 + 2 * x0) * (1 + 2 * x0 < 5)) + 4 * x0 * x1 + (5 * (5 <= 1 + 2 * x0) + (1 + 2 * x0) * (1 + 2 * x0 < 5)) + (5 * (5 <= 1 + 2 * x1) + (1 + 2 * x1) * (1 + 2 * x1 < 5) ) tmp30 = tmp28 / tmp29 tmp31 = 0.0625 tmp32 = tmp30 * tmp31 tmp33 = tmp32 * tmp31 tl.store(in_out_ptr0 + x3, tmp33, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 3, 3), (36, 9, 3, 1), torch.float32) get_raw_stream(0) triton_poi_fused_avg_pool2d_0[grid(144)](arg0_1, buf0, 144, XBLOCK= 128, num_warps=4, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((4, 4, 3, 3), (36, 9, 3, 1), torch.float32) buf2 = buf1 del buf1 triton_poi_fused_avg_pool2d_div_1[grid(144)](buf2, arg1_1, 144, XBLOCK=256, num_warps=4, num_stages=1) del arg1_1 return buf0, buf2 def keep_variance_fn(x): return x + 0.001 class AvgPool2dNew(nn.Module): def __init__(self, keep_variance_fn=None, kernel_size=2): super(AvgPool2dNew, self).__init__() self._keep_variance_fn = keep_variance_fn self.kernel_size = kernel_size def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0], output[1]
DoggyLiu0116/MamboNet
AvgPool2d
false
5,084
[ "MIT" ]
1
3b708091422491f660c4bd5eb12b06ce3b8a5f79
https://github.com/DoggyLiu0116/MamboNet/tree/3b708091422491f660c4bd5eb12b06ce3b8a5f79
SimpleConvNet
import torch import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data import torch.nn.functional as F import torch.onnx class SimpleConvNet(nn.Module): def __init__(self, hidden=1000): super(SimpleConvNet, self).__init__() self.conv1 = nn.Conv2d(3, 6, 5) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) self.fc1 = nn.Linear(16 * 5 * 5, hidden) self.fc2 = nn.Linear(hidden, 84) self.fc3 = nn.Linear(84, 10) def forward(self, x): x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self.conv2(x))) x = x.view(-1, 16 * 5 * 5) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x def get_inputs(): return [torch.rand([4, 3, 32, 32])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data import torch.onnx assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_relu_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 18816 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 784 % 6 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, xmask) @triton.jit def triton_poi_fused_max_pool2d_with_indices_1(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 4704 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 14 x3 = xindex // 14 x2 = xindex // 1176 x4 = xindex % 1176 tmp0 = tl.load(in_ptr0 + (2 * x0 + 56 * x3), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 56 * x3), xmask, eviction_policy ='evict_last') tmp3 = tl.load(in_ptr0 + (28 + 2 * x0 + 56 * x3), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (29 + 2 * x0 + 56 * x3), xmask, eviction_policy='evict_last') tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + (x4 + 1184 * x2), tmp6, xmask) tl.store(out_ptr1 + (x4 + 1280 * x2), tmp16, xmask) @triton.jit def triton_poi_fused_convolution_relu_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 6400 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 100 % 16 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, xmask) @triton.jit def triton_poi_fused_max_pool2d_with_indices_3(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 1600 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 5 x1 = xindex // 5 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 20 * x1), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 20 * x1), xmask, eviction_policy ='evict_last') tmp7 = tl.load(in_ptr0 + (10 + 2 * x0 + 20 * x1), xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (11 + 2 * x0 + 20 * x1), xmask, eviction_policy='evict_last') tmp2 = tmp1 > tmp0 tmp3 = tl.full([1], 1, tl.int8) tmp4 = tl.full([1], 0, tl.int8) tmp5 = tl.where(tmp2, tmp3, tmp4) tmp6 = triton_helpers.maximum(tmp1, tmp0) tmp8 = tmp7 > tmp6 tmp9 = tl.full([1], 2, tl.int8) tmp10 = tl.where(tmp8, tmp9, tmp5) tmp11 = triton_helpers.maximum(tmp7, tmp6) tmp13 = tmp12 > tmp11 tmp14 = tl.full([1], 3, tl.int8) tmp15 = tl.where(tmp13, tmp14, tmp10) tmp16 = triton_helpers.maximum(tmp12, tmp11) tl.store(out_ptr0 + x2, tmp15, xmask) tl.store(out_ptr1 + x2, tmp16, xmask) @triton.jit def triton_poi_fused_relu_4(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 4000 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 1000 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_relu_5(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 336 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 84 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11) = args args.clear() assert_size_stride(primals_1, (6, 3, 5, 5), (75, 25, 5, 1)) assert_size_stride(primals_2, (6,), (1,)) assert_size_stride(primals_3, (4, 3, 32, 32), (3072, 1024, 32, 1)) assert_size_stride(primals_4, (16, 6, 5, 5), (150, 25, 5, 1)) assert_size_stride(primals_5, (16,), (1,)) assert_size_stride(primals_6, (1000, 400), (400, 1)) assert_size_stride(primals_7, (1000,), (1,)) assert_size_stride(primals_8, (84, 1000), (1000, 1)) assert_size_stride(primals_9, (84,), (1,)) assert_size_stride(primals_10, (10, 84), (84, 1)) assert_size_stride(primals_11, (10,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 6, 28, 28), (4704, 784, 28, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(18816)](buf1, primals_2, 18816, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((4, 6, 14, 14), (1184, 196, 14, 1), torch .float32) buf3 = empty_strided_cuda((4, 6, 14, 14), (1280, 196, 14, 1), torch .int8) triton_poi_fused_max_pool2d_with_indices_1[grid(4704)](buf1, buf2, buf3, 4704, XBLOCK=128, num_warps=4, num_stages=1) buf4 = extern_kernels.convolution(buf2, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 16, 10, 10), (1600, 100, 10, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_relu_2[grid(6400)](buf5, primals_5, 6400, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf6 = empty_strided_cuda((4, 16, 5, 5), (400, 25, 5, 1), torch.int8) buf7 = empty_strided_cuda((4, 16, 5, 5), (400, 25, 5, 1), torch.float32 ) triton_poi_fused_max_pool2d_with_indices_3[grid(1600)](buf5, buf6, buf7, 1600, XBLOCK=256, num_warps=4, num_stages=1) buf8 = empty_strided_cuda((4, 1000), (1000, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf7, (4, 400), (400, 1), 0), reinterpret_tensor(primals_6, (400, 1000), (1, 400), 0), out=buf8) buf9 = buf8 del buf8 triton_poi_fused_relu_4[grid(4000)](buf9, primals_7, 4000, XBLOCK= 256, num_warps=4, num_stages=1) del primals_7 buf10 = empty_strided_cuda((4, 84), (84, 1), torch.float32) extern_kernels.mm(buf9, reinterpret_tensor(primals_8, (1000, 84), ( 1, 1000), 0), out=buf10) buf11 = buf10 del buf10 triton_poi_fused_relu_5[grid(336)](buf11, primals_9, 336, XBLOCK= 256, num_warps=4, num_stages=1) del primals_9 buf12 = empty_strided_cuda((4, 10), (10, 1), torch.float32) extern_kernels.addmm(primals_11, buf11, reinterpret_tensor( primals_10, (84, 10), (1, 84), 0), alpha=1, beta=1, out=buf12) del primals_11 return (buf12, primals_1, primals_3, primals_4, buf1, buf2, buf3, buf5, buf6, reinterpret_tensor(buf7, (4, 400), (400, 1), 0), buf9, buf11, primals_10, primals_8, primals_6) class SimpleConvNetNew(nn.Module): def __init__(self, hidden=1000): super(SimpleConvNetNew, self).__init__() self.conv1 = nn.Conv2d(3, 6, 5) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) self.fc1 = nn.Linear(16 * 5 * 5, hidden) self.fc2 = nn.Linear(hidden, 84) self.fc3 = nn.Linear(84, 10) def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_6 = self.fc1.weight primals_7 = self.fc1.bias primals_8 = self.fc2.weight primals_9 = self.fc2.bias primals_10 = self.fc3.weight primals_11 = self.fc3.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11]) return output[0]
CorentinChauvin/style-transfer-KD
SimpleConvNet
false
5,085
[ "MIT" ]
1
87bcb2963dbb8d09faf94c74a744f358cafe5427
https://github.com/CorentinChauvin/style-transfer-KD/tree/87bcb2963dbb8d09faf94c74a744f358cafe5427
RelRootDepthLoss
import torch import torch.utils.data import torch.nn as nn class RelRootDepthLoss(nn.Module): def __init__(self): super(RelRootDepthLoss, self).__init__() def forward(self, root_depth_out, root_depth_gt, root_valid): loss = torch.abs(root_depth_out - root_depth_gt) * root_valid return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand( [4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math import torch.utils.data import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_abs_mul_sub_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask) tmp4 = tl.load(in_ptr2 + x0, xmask) tmp2 = tmp0 - tmp1 tmp3 = tl_math.abs(tmp2) tmp5 = tmp3 * tmp4 tl.store(out_ptr0 + x0, tmp5, xmask) def call(args): arg0_1, arg1_1, arg2_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_abs_mul_sub_0[grid(256)](arg0_1, arg1_1, arg2_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del arg1_1 del arg2_1 return buf0, class RelRootDepthLossNew(nn.Module): def __init__(self): super(RelRootDepthLossNew, self).__init__() def forward(self, input_0, input_1, input_2): arg0_1 = input_0 arg1_1 = input_1 arg2_1 = input_2 output = call([arg0_1, arg1_1, arg2_1]) return output[0]
DuinoDu/InterHand2.6M.pl
RelRootDepthLoss
false
5,086
[ "MIT" ]
1
2d216960cf95b066a197a9b49795840b1ecfd0c1
https://github.com/DuinoDu/InterHand2.6M.pl/tree/2d216960cf95b066a197a9b49795840b1ecfd0c1
Linear
import torch import torch.nn as nn import torch.nn.functional as F from torch.nn.parameter import Parameter def keep_variance_fn(x): return x + 0.001 class Linear(nn.Module): def __init__(self, in_features, out_features, bias=True, keep_variance_fn=None): super(Linear, self).__init__() self._keep_variance_fn = keep_variance_fn self.in_features = in_features self.out_features = out_features self.weight = Parameter(torch.Tensor(out_features, in_features)) if bias: self.bias = Parameter(torch.Tensor(out_features)) else: self.register_parameter('bias', None) def forward(self, inputs_mean, inputs_variance): outputs_mean = F.linear(inputs_mean, self.weight, self.bias) outputs_variance = F.linear(inputs_variance, self.weight ** 2, None) if self._keep_variance_fn is not None: outputs_variance = self._keep_variance_fn(outputs_variance) return outputs_mean, outputs_variance def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn from torch.nn.parameter import Parameter assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_pow_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tmp0 * tmp0 tl.store(out_ptr0 + x0, tmp1, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_2 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_pow_0[grid(16)](primals_1, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_4, (64, 4), (4, 1), 0), reinterpret_tensor(buf1, (4, 4), (1, 4), 0), out=buf2) del buf1 return reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), primals_1, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(primals_4, (64, 4), (4, 1), 0) def keep_variance_fn(x): return x + 0.001 class LinearNew(nn.Module): def __init__(self, in_features, out_features, bias=True, keep_variance_fn=None): super(LinearNew, self).__init__() self._keep_variance_fn = keep_variance_fn self.in_features = in_features self.out_features = out_features self.weight = Parameter(torch.Tensor(out_features, in_features)) if bias: self.bias = Parameter(torch.Tensor(out_features)) else: self.register_parameter('bias', None) def forward(self, input_0, input_1): primals_1 = self.weight primals_2 = self.bias primals_3 = input_0 primals_4 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0], output[1]
DoggyLiu0116/MamboNet
Linear
false
5,087
[ "MIT" ]
1
3b708091422491f660c4bd5eb12b06ce3b8a5f79
https://github.com/DoggyLiu0116/MamboNet/tree/3b708091422491f660c4bd5eb12b06ce3b8a5f79
Softmax
import torch import torch.nn as nn def keep_variance_fn(x): return x + 0.001 class Softmax(nn.Module): def __init__(self, dim=1, keep_variance_fn=None): super(Softmax, self).__init__() self.dim = dim self._keep_variance_fn = keep_variance_fn def forward(self, features_mean, features_variance, eps=1e-05): """Softmax function applied to a multivariate Gaussian distribution. It works under the assumption that features_mean and features_variance are the parameters of a the indepent gaussians that contribute to the multivariate gaussian. Mean and variance of the log-normal distribution are computed following https://en.wikipedia.org/wiki/Log-normal_distribution.""" log_gaussian_mean = features_mean + 0.5 * features_variance log_gaussian_variance = 2 * log_gaussian_mean log_gaussian_mean = torch.exp(log_gaussian_mean) log_gaussian_variance = torch.exp(log_gaussian_variance) log_gaussian_variance = log_gaussian_variance * (torch.exp( features_variance) - 1) constant = torch.sum(log_gaussian_mean, dim=self.dim) + eps constant = constant.unsqueeze(self.dim) outputs_mean = log_gaussian_mean / constant outputs_variance = log_gaussian_variance / constant ** 2 if self._keep_variance_fn is not None: outputs_variance = self._keep_variance_fn(outputs_variance) return outputs_mean, outputs_variance def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_exp_mul_sum_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 16 x1 = xindex // 16 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask) tmp1 = tl.load(in_ptr1 + (x0 + 64 * x1), xmask) tmp6 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask) tmp7 = tl.load(in_ptr1 + (16 + x0 + 64 * x1), xmask) tmp12 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask) tmp13 = tl.load(in_ptr1 + (32 + x0 + 64 * x1), xmask) tmp18 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask) tmp19 = tl.load(in_ptr1 + (48 + x0 + 64 * x1), xmask) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp0 + tmp3 tmp5 = tl_math.exp(tmp4) tmp8 = tmp7 * tmp2 tmp9 = tmp6 + tmp8 tmp10 = tl_math.exp(tmp9) tmp11 = tmp5 + tmp10 tmp14 = tmp13 * tmp2 tmp15 = tmp12 + tmp14 tmp16 = tl_math.exp(tmp15) tmp17 = tmp11 + tmp16 tmp20 = tmp19 * tmp2 tmp21 = tmp18 + tmp20 tmp22 = tl_math.exp(tmp21) tmp23 = tmp17 + tmp22 tmp24 = 1e-05 tmp25 = tmp23 + tmp24 tl.store(out_ptr0 + x2, tmp25, xmask) @triton.jit def triton_poi_fused_add_div_exp_mul_pow_sub_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x3, xmask) tmp6 = tl.load(in_ptr2 + (x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp0 + tmp3 tmp5 = tl_math.exp(tmp4) tmp7 = tmp5 / tmp6 tmp8 = 2.0 tmp9 = tmp4 * tmp8 tmp10 = tl_math.exp(tmp9) tmp11 = tl_math.exp(tmp1) tmp12 = 1.0 tmp13 = tmp11 - tmp12 tmp14 = tmp10 * tmp13 tmp15 = tmp6 * tmp6 tmp16 = tmp14 / tmp15 tl.store(out_ptr0 + x3, tmp7, xmask) tl.store(out_ptr1 + x3, tmp16, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_exp_mul_sum_0[grid(64)](arg1_1, arg0_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_add_div_exp_mul_pow_sub_1[grid(256)](arg1_1, arg0_1, buf0, buf1, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del arg1_1 del buf0 return buf1, buf2 def keep_variance_fn(x): return x + 0.001 class SoftmaxNew(nn.Module): def __init__(self, dim=1, keep_variance_fn=None): super(SoftmaxNew, self).__init__() self.dim = dim self._keep_variance_fn = keep_variance_fn def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0], output[1]
DoggyLiu0116/MamboNet
Softmax
false
5,088
[ "MIT" ]
1
3b708091422491f660c4bd5eb12b06ce3b8a5f79
https://github.com/DoggyLiu0116/MamboNet/tree/3b708091422491f660c4bd5eb12b06ce3b8a5f79
Conv2d
import torch import torch.nn.functional as F from torch.nn.modules.conv import _ConvNd from torch.nn.modules.utils import _pair def keep_variance_fn(x): return x + 0.001 class Conv2d(_ConvNd): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, keep_variance_fn=None, padding_mode='zeros'): self._keep_variance_fn = keep_variance_fn kernel_size = _pair(kernel_size) stride = _pair(stride) padding = _pair(padding) dilation = _pair(dilation) super(Conv2d, self).__init__(in_channels, out_channels, kernel_size, stride, padding, dilation, False, _pair(0), groups, bias, padding_mode) def forward(self, inputs_mean, inputs_variance): outputs_mean = F.conv2d(inputs_mean, self.weight, self.bias, self. stride, self.padding, self.dilation, self.groups) outputs_variance = F.conv2d(inputs_variance, self.weight ** 2, None, self.stride, self.padding, self.dilation, self.groups) if self._keep_variance_fn is not None: outputs_variance = self._keep_variance_fn(outputs_variance) return outputs_mean, outputs_variance def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4, 'kernel_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch.nn.modules.conv import _ConvNd from torch.nn.modules.utils import _pair assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_convolution_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x2, tmp2, xmask) @triton.jit def triton_poi_fused_pow_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tmp0 * tmp0 tl.store(out_ptr0 + x0, tmp1, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 1, 1), (4, 1, 1, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(16)](buf1, primals_2, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_2 buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_pow_1[grid(256)](primals_1, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) buf3 = extern_kernels.convolution(primals_4, buf2, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf3, (4, 4, 1, 1), (4, 1, 1, 1)) return buf1, buf3, primals_1, primals_3, primals_4, buf2 def keep_variance_fn(x): return x + 0.001 class Conv2dNew(_ConvNd): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, keep_variance_fn=None, padding_mode='zeros'): self._keep_variance_fn = keep_variance_fn kernel_size = _pair(kernel_size) stride = _pair(stride) padding = _pair(padding) dilation = _pair(dilation) super(Conv2dNew, self).__init__(in_channels, out_channels, kernel_size, stride, padding, dilation, False, _pair(0), groups, bias, padding_mode) def forward(self, input_0, input_1): primals_1 = self.weight primals_2 = self.bias primals_3 = input_0 primals_4 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0], output[1]
DoggyLiu0116/MamboNet
Conv2d
false
5,089
[ "MIT" ]
1
3b708091422491f660c4bd5eb12b06ce3b8a5f79
https://github.com/DoggyLiu0116/MamboNet/tree/3b708091422491f660c4bd5eb12b06ce3b8a5f79
NN
import torch import torch.nn as nn class NN(nn.Module): def __init__(self, input_size, num_classes): super(NN, self).__init__() self.fc1 = nn.Linear(in_features=input_size, out_features=50) self.activation1 = nn.ReLU() self.fc2 = nn.Linear(in_features=50, out_features=num_classes) def forward(self, x): x = self.fc1(x) x = self.activation1(x) x = self.fc2(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'num_classes': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 3200 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 50 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (50, 4), (4, 1)) assert_size_stride(primals_2, (50,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 50), (50, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 50), (50, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 50), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 50), (800, 200, 50, 1), 0) del buf0 buf3 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(3200)](buf1, primals_2, buf3, 3200, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 50), (50, 1), 0), reinterpret_tensor(primals_4, (50, 4), (1, 50), 0), alpha=1, beta=1, out=buf2) del primals_5 return reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 50), (50, 1), 0), primals_4, buf3 class NNNew(nn.Module): def __init__(self, input_size, num_classes): super(NNNew, self).__init__() self.fc1 = nn.Linear(in_features=input_size, out_features=50) self.activation1 = nn.ReLU() self.fc2 = nn.Linear(in_features=50, out_features=num_classes) def forward(self, input_0): primals_1 = self.fc1.weight primals_2 = self.fc1.bias primals_4 = self.fc2.weight primals_5 = self.fc2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
Dutta-SD/Python_Programs
NN
false
5,090
[ "MIT" ]
1
f002dbd49c979a6d8b156f88003a79f364ff01da
https://github.com/Dutta-SD/Python_Programs/tree/f002dbd49c979a6d8b156f88003a79f364ff01da
BiDAFAttention
import torch import torch.nn.functional as F import torch.nn as nn def masked_softmax(logits, mask, dim=-1, log_softmax=False): """Take the softmax of `logits` over given dimension, and set entries to 0 wherever `mask` is 0. Args: logits (torch.Tensor): Inputs to the softmax function. mask (torch.Tensor): Same shape as `logits`, with 0 indicating positions that should be assigned 0 probability in the output. dim (int): Dimension over which to take softmax. log_softmax (bool): Take log-softmax rather than regular softmax. E.g., some PyTorch functions such as `F.nll_loss` expect log-softmax. Returns: probs (torch.Tensor): Result of taking masked softmax over the logits. """ mask = mask.type(torch.float32) masked_logits = mask * logits + (1 - mask) * -1e+30 softmax_fn = F.log_softmax if log_softmax else F.softmax probs = softmax_fn(masked_logits, dim) return probs class BiDAFAttention(nn.Module): """Bidirectional attention originally used by BiDAF. Bidirectional attention computes attention in two directions: The context attends to the query and the query attends to the context. The output of this layer is the concatenation of [context, c2q_attention, context * c2q_attention, context * q2c_attention]. This concatenation allows the attention vector at each timestep, along with the embeddings from previous layers, to flow through the attention layer to the modeling layer. The output has shape (batch_size, context_len, 8 * hidden_size). Args: hidden_size (int): Size of hidden activations. drop_prob (float): Probability of zero-ing out activations. """ def __init__(self, hidden_size, drop_prob=0.1): super(BiDAFAttention, self).__init__() self.drop_prob = drop_prob self.c_weight = nn.Parameter(torch.zeros(hidden_size, 1)) self.q_weight = nn.Parameter(torch.zeros(hidden_size, 1)) self.cq_weight = nn.Parameter(torch.zeros(1, 1, hidden_size)) for weight in (self.c_weight, self.q_weight, self.cq_weight): nn.init.xavier_uniform_(weight) self.bias = nn.Parameter(torch.zeros(1)) def forward(self, c, q, c_mask, q_mask): batch_size, c_len, _ = c.size() q_len = q.size(1) s = self.get_similarity_matrix(c, q) c_mask = c_mask.view(batch_size, c_len, 1) q_mask = q_mask.view(batch_size, 1, q_len) s1 = masked_softmax(s, q_mask, dim=2) s2 = masked_softmax(s, c_mask, dim=1) a = torch.bmm(s1, q) b = torch.bmm(torch.bmm(s1, s2.transpose(1, 2)), c) x = torch.cat([c, a, c * a, c * b], dim=2) return x def get_similarity_matrix(self, c, q): """Get the "similarity matrix" between context and query (using the terminology of the BiDAF paper). A naive implementation as described in BiDAF would concatenate the three vectors then project the result with a single weight matrix. This method is a more memory-efficient implementation of the same operation. See Also: Equation 1 in https://arxiv.org/abs/1611.01603 """ c_len, q_len = c.size(1), q.size(1) c = F.dropout(c, self.drop_prob, self.training) q = F.dropout(q, self.drop_prob, self.training) s0 = torch.matmul(c, self.c_weight).expand([-1, -1, q_len]) s1 = torch.matmul(q, self.q_weight).transpose(1, 2).expand([-1, c_len, -1]) s2 = torch.matmul(c * self.cq_weight, q.transpose(1, 2)) s = s0 + s1 + s2 + self.bias return s def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4]), torch.rand([4, 4, 1]), torch.rand([4, 1, 4])] def get_init_inputs(): return [[], {'hidden_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn.functional as F import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_mul_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x2, tmp2, xmask) @triton.jit def triton_poi_fused_add_mul_rsub_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x2 = xindex // 16 x3 = xindex // 4 x4 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp1 = tl.load(in_ptr1 + x3, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr2 + (x0 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp4 = tl.load(in_ptr3 + x4, xmask) tmp6 = tl.load(in_ptr4 + 0) tmp7 = tl.broadcast_to(tmp6, [XBLOCK]) tmp15 = tl.load(in_ptr5 + x3, xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp8 = tmp5 + tmp7 tmp9 = tmp0 * tmp8 tmp10 = 1.0 tmp11 = tmp10 - tmp0 tmp12 = -1e+30 tmp13 = tmp11 * tmp12 tmp14 = tmp9 + tmp13 tmp16 = tmp15 * tmp8 tmp17 = tmp10 - tmp15 tmp18 = tmp17 * tmp12 tmp19 = tmp16 + tmp18 tl.store(out_ptr0 + x4, tmp14, xmask) tl.store(out_ptr1 + x4, tmp19, xmask) @triton.jit def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused__softmax_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 4 x2 = xindex // 16 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (4 + x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (8 + x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (12 + x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x3, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_5(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 4 x2 = xindex // 16 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (4 + x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (8 + x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (12 + x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x3, tmp8, xmask) @triton.jit def triton_poi_fused_cat_6(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 16 x1 = xindex // 16 x2 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (4 * x1 + x0), tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp6 = tmp0 >= tmp3 tmp7 = tl.full([1], 8, tl.int64) tmp8 = tmp0 < tmp7 tmp9 = tmp6 & tmp8 tmp10 = tl.load(in_ptr1 + (4 * x1 + (-4 + x0)), tmp9 & xmask, eviction_policy='evict_last', other=0.0) tmp11 = tmp0 >= tmp7 tmp12 = tl.full([1], 12, tl.int64) tmp13 = tmp0 < tmp12 tmp14 = tmp11 & tmp13 tmp15 = tl.load(in_ptr0 + (4 * x1 + (-8 + x0)), tmp14 & xmask, eviction_policy='evict_last', other=0.0) tmp16 = tl.load(in_ptr1 + (4 * x1 + (-8 + x0)), tmp14 & xmask, eviction_policy='evict_last', other=0.0) tmp17 = tmp15 * tmp16 tmp18 = tl.full(tmp17.shape, 0.0, tmp17.dtype) tmp19 = tl.where(tmp14, tmp17, tmp18) tmp20 = tmp0 >= tmp12 tl.full([1], 16, tl.int64) tmp23 = tl.load(in_ptr0 + (4 * x1 + (-12 + x0)), tmp20 & xmask, eviction_policy='evict_last', other=0.0) tmp24 = tl.load(in_ptr2 + (4 * x1 + (-12 + x0)), tmp20 & xmask, eviction_policy='evict_last', other=0.0) tmp25 = tmp23 * tmp24 tmp26 = tl.full(tmp25.shape, 0.0, tmp25.dtype) tmp27 = tl.where(tmp20, tmp25, tmp26) tmp28 = tl.where(tmp14, tmp19, tmp27) tmp29 = tl.where(tmp9, tmp10, tmp28) tmp30 = tl.where(tmp4, tmp5, tmp29) tl.store(out_ptr0 + x2, tmp30, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8) = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_3, (4, 1), (1, 1)) assert_size_stride(primals_4, (4, 1), (1, 1)) assert_size_stride(primals_5, (1, 1, 4), (4, 4, 1)) assert_size_stride(primals_6, (1,), (1,)) assert_size_stride(primals_7, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_8, (4, 1, 4), (4, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 1), (1, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), primals_3, out=buf0) del primals_3 buf1 = empty_strided_cuda((16, 1), (1, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_2, (16, 4), (4, 1), 0), primals_4, out=buf1) del primals_4 buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_0[grid(64)](primals_1, primals_5, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_5 buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf2, reinterpret_tensor(primals_2, (4, 4, 4), ( 16, 1, 4), 0), out=buf3) buf4 = buf2 del buf2 buf7 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_add_mul_rsub_1[grid(64)](primals_8, buf0, buf1, buf3, primals_6, primals_7, buf4, buf7, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf0 del buf1 del primals_6 buf5 = buf3 del buf3 triton_poi_fused__softmax_2[grid(64)](buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf6 = buf4 del buf4 triton_poi_fused__softmax_3[grid(64)](buf5, buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) buf8 = buf5 del buf5 triton_poi_fused__softmax_4[grid(64)](buf7, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1) buf9 = buf7 del buf7 triton_poi_fused__softmax_5[grid(64)](buf8, buf9, 64, XBLOCK=64, num_warps=1, num_stages=1) buf10 = buf8 del buf8 extern_kernels.bmm(buf6, primals_2, out=buf10) buf11 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf6, reinterpret_tensor(buf9, (4, 4, 4), (16, 1, 4), 0), out=buf11) buf12 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf11, primals_1, out=buf12) del buf11 buf13 = empty_strided_cuda((4, 4, 16), (64, 16, 1), torch.float32) triton_poi_fused_cat_6[grid(256)](primals_1, buf10, buf12, buf13, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf10 del buf12 return buf13, primals_1, primals_2, primals_7, primals_8, buf6, buf9 def masked_softmax(logits, mask, dim=-1, log_softmax=False): """Take the softmax of `logits` over given dimension, and set entries to 0 wherever `mask` is 0. Args: logits (torch.Tensor): Inputs to the softmax function. mask (torch.Tensor): Same shape as `logits`, with 0 indicating positions that should be assigned 0 probability in the output. dim (int): Dimension over which to take softmax. log_softmax (bool): Take log-softmax rather than regular softmax. E.g., some PyTorch functions such as `F.nll_loss` expect log-softmax. Returns: probs (torch.Tensor): Result of taking masked softmax over the logits. """ mask = mask.type(torch.float32) masked_logits = mask * logits + (1 - mask) * -1e+30 softmax_fn = F.log_softmax if log_softmax else F.softmax probs = softmax_fn(masked_logits, dim) return probs class BiDAFAttentionNew(nn.Module): """Bidirectional attention originally used by BiDAF. Bidirectional attention computes attention in two directions: The context attends to the query and the query attends to the context. The output of this layer is the concatenation of [context, c2q_attention, context * c2q_attention, context * q2c_attention]. This concatenation allows the attention vector at each timestep, along with the embeddings from previous layers, to flow through the attention layer to the modeling layer. The output has shape (batch_size, context_len, 8 * hidden_size). Args: hidden_size (int): Size of hidden activations. drop_prob (float): Probability of zero-ing out activations. """ def __init__(self, hidden_size, drop_prob=0.1): super(BiDAFAttentionNew, self).__init__() self.drop_prob = drop_prob self.c_weight = nn.Parameter(torch.zeros(hidden_size, 1)) self.q_weight = nn.Parameter(torch.zeros(hidden_size, 1)) self.cq_weight = nn.Parameter(torch.zeros(1, 1, hidden_size)) for weight in (self.c_weight, self.q_weight, self.cq_weight): nn.init.xavier_uniform_(weight) self.bias = nn.Parameter(torch.zeros(1)) def get_similarity_matrix(self, c, q): """Get the "similarity matrix" between context and query (using the terminology of the BiDAF paper). A naive implementation as described in BiDAF would concatenate the three vectors then project the result with a single weight matrix. This method is a more memory-efficient implementation of the same operation. See Also: Equation 1 in https://arxiv.org/abs/1611.01603 """ c_len, q_len = c.size(1), q.size(1) c = F.dropout(c, self.drop_prob, self.training) q = F.dropout(q, self.drop_prob, self.training) s0 = torch.matmul(c, self.c_weight).expand([-1, -1, q_len]) s1 = torch.matmul(q, self.q_weight).transpose(1, 2).expand([-1, c_len, -1]) s2 = torch.matmul(c * self.cq_weight, q.transpose(1, 2)) s = s0 + s1 + s2 + self.bias return s def forward(self, input_0, input_1, input_2, input_3): primals_3 = self.c_weight primals_4 = self.q_weight primals_5 = self.cq_weight primals_6 = self.bias primals_1 = input_0 primals_2 = input_1 primals_7 = input_2 primals_8 = input_3 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8]) return output[0]
Derek318/Adversarial-Squad-CS224N
BiDAFAttention
false
5,091
[ "MIT" ]
1
9b4a5da2a262f4de9b9b05d7b67dc48b2b857e46
https://github.com/Derek318/Adversarial-Squad-CS224N/tree/9b4a5da2a262f4de9b9b05d7b67dc48b2b857e46
MinusRbfHSIC
import torch import torch.nn as nn import torch.utils.data class HSIC(nn.Module): """Base class for the finite sample estimator of Hilbert-Schmidt Independence Criterion (HSIC) ..math:: HSIC (X, Y) := || C_{x, y} ||^2_{HS}, where HSIC (X, Y) = 0 iif X and Y are independent. Empirically, we use the finite sample estimator of HSIC (with m observations) by, (1) biased estimator (HSIC_0) Gretton, Arthur, et al. "Measuring statistical dependence with Hilbert-Schmidt norms." 2005. :math: (m - 1)^2 tr KHLH. where K_{ij} = kernel_x (x_i, x_j), L_{ij} = kernel_y (y_i, y_j), H = 1 - m^{-1} 1 1 (Hence, K, L, H are m by m matrices). (2) unbiased estimator (HSIC_1) Song, Le, et al. "Feature selection via dependence maximization." 2012. :math: rac{1}{m (m - 3)} igg[ tr ( ilde K ilde L) + rac{1^ op ilde K 1 1^ op ilde L 1}{(m-1)(m-2)} - rac{2}{m-2} 1^ op ilde K ilde L 1 igg]. where ilde K and ilde L are related to K and L by the diagonal entries of ilde K_{ij} and ilde L_{ij} are set to zero. Parameters ---------- sigma_x : float the kernel size of the kernel function for X. sigma_y : float the kernel size of the kernel function for Y. algorithm: str ('unbiased' / 'biased') the algorithm for the finite sample estimator. 'unbiased' is used for our paper. reduction: not used (for compatibility with other losses). """ def __init__(self, sigma_x, sigma_y=None, algorithm='unbiased', reduction=None): super(HSIC, self).__init__() if sigma_y is None: sigma_y = sigma_x self.sigma_x = sigma_x self.sigma_y = sigma_y if algorithm == 'biased': self.estimator = self.biased_estimator elif algorithm == 'unbiased': self.estimator = self.unbiased_estimator else: raise ValueError('invalid estimator: {}'.format(algorithm)) def _kernel_x(self, X): raise NotImplementedError def _kernel_y(self, Y): raise NotImplementedError def biased_estimator(self, input1, input2): """Biased estimator of Hilbert-Schmidt Independence Criterion Gretton, Arthur, et al. "Measuring statistical dependence with Hilbert-Schmidt norms." 2005. """ K = self._kernel_x(input1) L = self._kernel_y(input2) KH = K - K.mean(0, keepdim=True) LH = L - L.mean(0, keepdim=True) N = len(input1) return torch.trace(KH @ LH / (N - 1) ** 2) def unbiased_estimator(self, input1, input2): """Unbiased estimator of Hilbert-Schmidt Independence Criterion Song, Le, et al. "Feature selection via dependence maximization." 2012. """ kernel_XX = self._kernel_x(input1) kernel_YY = self._kernel_y(input2) tK = kernel_XX - torch.diag(kernel_XX) tL = kernel_YY - torch.diag(kernel_YY) N = len(input1) hsic = torch.trace(tK @ tL) + torch.sum(tK) * torch.sum(tL) / (N - 1 ) / (N - 2) - 2 * torch.sum(tK, 0).dot(torch.sum(tL, 0)) / (N - 2) return hsic / (N * (N - 3)) def forward(self, input1, input2, **kwargs): return self.estimator(input1, input2) class RbfHSIC(HSIC): """Radial Basis Function (RBF) kernel HSIC implementation. """ def _kernel(self, X, sigma): X = X.view(len(X), -1) XX = X @ X.t() X_sqnorms = torch.diag(XX) X_L2 = -2 * XX + X_sqnorms.unsqueeze(1) + X_sqnorms.unsqueeze(0) gamma = 1 / (2 * sigma ** 2) kernel_XX = torch.exp(-gamma * X_L2) return kernel_XX def _kernel_x(self, X): return self._kernel(X, self.sigma_x) def _kernel_y(self, Y): return self._kernel(Y, self.sigma_y) class MinusRbfHSIC(RbfHSIC): """``Minus'' RbfHSIC for the ``max'' optimization. """ def forward(self, input1, input2, **kwargs): return -self.estimator(input1, input2) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'sigma_x': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_per_fused_add_diagonal_copy_exp_mul_sub_sum_0(in_ptr0, out_ptr0, out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r2 = rindex r1 = rindex // 4 r0 = rindex % 4 tmp0 = tl.load(in_ptr0 + r2, None) tmp3 = tl.load(in_ptr0 + 5 * r1, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + 5 * r0, None, eviction_policy='evict_last') tmp1 = -2.0 tmp2 = tmp0 * tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = -0.03125 tmp8 = tmp6 * tmp7 tmp9 = tl_math.exp(tmp8) tmp10 = tmp5 * tmp1 tmp11 = tmp10 + tmp5 tmp12 = tmp11 + tmp5 tmp13 = tmp12 * tmp7 tmp14 = tl_math.exp(tmp13) tmp15 = tmp9 - tmp14 tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK]) tmp18 = tl.sum(tmp16, 1)[:, None] tl.store(out_ptr0 + tl.broadcast_to(r2, [XBLOCK, RBLOCK]), tmp15, None) tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp18, None) @triton.jit def triton_per_fused_add_div_dot_mul_neg_sub_sum_trace_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, xnumel, rnumel, XBLOCK: tl .constexpr): RBLOCK: tl.constexpr = 4 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + 5 * r0, None, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + r0, None) tmp5 = tl.load(in_ptr1 + (4 + r0), None) tmp7 = tl.load(in_ptr1 + (8 + r0), None) tmp9 = tl.load(in_ptr1 + (12 + r0), None) tmp11 = tl.load(in_ptr2 + r0, None) tmp12 = tl.load(in_ptr2 + (4 + r0), None) tmp14 = tl.load(in_ptr2 + (8 + r0), None) tmp16 = tl.load(in_ptr2 + (12 + r0), None) tmp22 = tl.load(in_ptr3 + 0) tmp23 = tl.broadcast_to(tmp22, [XBLOCK, 1]) tmp24 = tl.load(in_ptr4 + 0) tmp25 = tl.broadcast_to(tmp24, [XBLOCK, 1]) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.sum(tmp1, 1)[:, None] tmp6 = tmp4 + tmp5 tmp8 = tmp6 + tmp7 tmp10 = tmp8 + tmp9 tmp13 = tmp11 + tmp12 tmp15 = tmp13 + tmp14 tmp17 = tmp15 + tmp16 tmp18 = tmp10 * tmp17 tmp19 = tl.broadcast_to(tmp18, [XBLOCK, RBLOCK]) tmp21 = tl.sum(tmp19, 1)[:, None] tmp26 = tmp23 * tmp25 tmp27 = 0.3333333333333333 tmp28 = tmp26 * tmp27 tmp29 = 0.5 tmp30 = tmp28 * tmp29 tmp31 = tmp3 + tmp30 tmp32 = 2.0 tmp33 = tmp21 * tmp32 tmp34 = tmp33 * tmp29 tmp35 = tmp31 - tmp34 tmp36 = 0.25 tmp37 = tmp35 * tmp36 tmp38 = -tmp37 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp38, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(arg0_1, (4, 64), (64, 1), 0), reinterpret_tensor(arg0_1, (64, 4), (1, 64), 0), out=buf0) del arg0_1 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf6 = empty_strided_cuda((), (), torch.float32) get_raw_stream(0) triton_per_fused_add_diagonal_copy_exp_mul_sub_sum_0[grid(1)](buf0, buf1, buf6, 1, 16, XBLOCK=1, num_warps=2, num_stages=1) buf2 = buf0 del buf0 extern_kernels.mm(reinterpret_tensor(arg1_1, (4, 64), (64, 1), 0), reinterpret_tensor(arg1_1, (64, 4), (1, 64), 0), out=buf2) del arg1_1 buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf7 = empty_strided_cuda((), (), torch.float32) triton_per_fused_add_diagonal_copy_exp_mul_sub_sum_0[grid(1)](buf2, buf3, buf7, 1, 16, XBLOCK=1, num_warps=2, num_stages=1) buf4 = buf2 del buf2 extern_kernels.mm(buf1, buf3, out=buf4) buf5 = empty_strided_cuda((), (), torch.float32) buf9 = buf5 del buf5 triton_per_fused_add_div_dot_mul_neg_sub_sum_trace_1[grid(1)](buf9, buf4, buf1, buf3, buf6, buf7, 1, 4, XBLOCK=1, num_warps=2, num_stages=1) del buf1 del buf3 del buf4 del buf6 del buf7 return buf9, class HSIC(nn.Module): """Base class for the finite sample estimator of Hilbert-Schmidt Independence Criterion (HSIC) ..math:: HSIC (X, Y) := || C_{x, y} ||^2_{HS}, where HSIC (X, Y) = 0 iif X and Y are independent. Empirically, we use the finite sample estimator of HSIC (with m observations) by, (1) biased estimator (HSIC_0) Gretton, Arthur, et al. "Measuring statistical dependence with Hilbert-Schmidt norms." 2005. :math: (m - 1)^2 tr KHLH. where K_{ij} = kernel_x (x_i, x_j), L_{ij} = kernel_y (y_i, y_j), H = 1 - m^{-1} 1 1 (Hence, K, L, H are m by m matrices). (2) unbiased estimator (HSIC_1) Song, Le, et al. "Feature selection via dependence maximization." 2012. :math: rac{1}{m (m - 3)} igg[ tr ( ilde K ilde L) + rac{1^ op ilde K 1 1^ op ilde L 1}{(m-1)(m-2)} - rac{2}{m-2} 1^ op ilde K ilde L 1 igg]. where ilde K and ilde L are related to K and L by the diagonal entries of ilde K_{ij} and ilde L_{ij} are set to zero. Parameters ---------- sigma_x : float the kernel size of the kernel function for X. sigma_y : float the kernel size of the kernel function for Y. algorithm: str ('unbiased' / 'biased') the algorithm for the finite sample estimator. 'unbiased' is used for our paper. reduction: not used (for compatibility with other losses). """ def __init__(self, sigma_x, sigma_y=None, algorithm='unbiased', reduction=None): super(HSIC, self).__init__() if sigma_y is None: sigma_y = sigma_x self.sigma_x = sigma_x self.sigma_y = sigma_y if algorithm == 'biased': self.estimator = self.biased_estimator elif algorithm == 'unbiased': self.estimator = self.unbiased_estimator else: raise ValueError('invalid estimator: {}'.format(algorithm)) def _kernel_x(self, X): raise NotImplementedError def _kernel_y(self, Y): raise NotImplementedError def biased_estimator(self, input1, input2): """Biased estimator of Hilbert-Schmidt Independence Criterion Gretton, Arthur, et al. "Measuring statistical dependence with Hilbert-Schmidt norms." 2005. """ K = self._kernel_x(input1) L = self._kernel_y(input2) KH = K - K.mean(0, keepdim=True) LH = L - L.mean(0, keepdim=True) N = len(input1) return torch.trace(KH @ LH / (N - 1) ** 2) def unbiased_estimator(self, input1, input2): """Unbiased estimator of Hilbert-Schmidt Independence Criterion Song, Le, et al. "Feature selection via dependence maximization." 2012. """ kernel_XX = self._kernel_x(input1) kernel_YY = self._kernel_y(input2) tK = kernel_XX - torch.diag(kernel_XX) tL = kernel_YY - torch.diag(kernel_YY) N = len(input1) hsic = torch.trace(tK @ tL) + torch.sum(tK) * torch.sum(tL) / (N - 1 ) / (N - 2) - 2 * torch.sum(tK, 0).dot(torch.sum(tL, 0)) / (N - 2) return hsic / (N * (N - 3)) def forward(self, input1, input2, **kwargs): return self.estimator(input1, input2) class RbfHSIC(HSIC): """Radial Basis Function (RBF) kernel HSIC implementation. """ def _kernel(self, X, sigma): X = X.view(len(X), -1) XX = X @ X.t() X_sqnorms = torch.diag(XX) X_L2 = -2 * XX + X_sqnorms.unsqueeze(1) + X_sqnorms.unsqueeze(0) gamma = 1 / (2 * sigma ** 2) kernel_XX = torch.exp(-gamma * X_L2) return kernel_XX def _kernel_x(self, X): return self._kernel(X, self.sigma_x) def _kernel_y(self, Y): return self._kernel(Y, self.sigma_y) class MinusRbfHSICNew(RbfHSIC): """``Minus'' RbfHSIC for the ``max'' optimization. """ def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
EIDOSlab/bridging-debiasing-privacy-deep-learning
MinusRbfHSIC
false
5,092
[ "MIT" ]
1
b30ab798d5ffd7d44a6d7136523400c14a4d08f5
https://github.com/EIDOSlab/bridging-debiasing-privacy-deep-learning/tree/b30ab798d5ffd7d44a6d7136523400c14a4d08f5
HandTypeLoss
import torch import torch.utils.data import torch.nn as nn import torch.nn.functional as F class HandTypeLoss(nn.Module): def __init__(self): super(HandTypeLoss, self).__init__() def forward(self, hand_type_out, hand_type_gt, hand_type_valid): loss = F.binary_cross_entropy(hand_type_out, hand_type_gt, reduction='none') loss = loss.mean(1) loss = loss * hand_type_valid return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand( [4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.utils.data import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_binary_cross_entropy_mean_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 16 x1 = xindex // 16 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask) tmp3 = tl.load(in_ptr1 + (x0 + 64 * x1), xmask) tmp13 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask) tmp15 = tl.load(in_ptr1 + (16 + x0 + 64 * x1), xmask) tmp25 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask) tmp27 = tl.load(in_ptr1 + (32 + x0 + 64 * x1), xmask) tmp37 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask) tmp39 = tl.load(in_ptr1 + (48 + x0 + 64 * x1), xmask) tmp1 = 1.0 tmp2 = tmp0 - tmp1 tmp4 = -tmp3 tmp5 = libdevice.log1p(tmp4) tmp6 = -100.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp2 * tmp7 tmp9 = tl_math.log(tmp3) tmp10 = triton_helpers.maximum(tmp9, tmp6) tmp11 = tmp0 * tmp10 tmp12 = tmp8 - tmp11 tmp14 = tmp13 - tmp1 tmp16 = -tmp15 tmp17 = libdevice.log1p(tmp16) tmp18 = triton_helpers.maximum(tmp17, tmp6) tmp19 = tmp14 * tmp18 tmp20 = tl_math.log(tmp15) tmp21 = triton_helpers.maximum(tmp20, tmp6) tmp22 = tmp13 * tmp21 tmp23 = tmp19 - tmp22 tmp24 = tmp12 + tmp23 tmp26 = tmp25 - tmp1 tmp28 = -tmp27 tmp29 = libdevice.log1p(tmp28) tmp30 = triton_helpers.maximum(tmp29, tmp6) tmp31 = tmp26 * tmp30 tmp32 = tl_math.log(tmp27) tmp33 = triton_helpers.maximum(tmp32, tmp6) tmp34 = tmp25 * tmp33 tmp35 = tmp31 - tmp34 tmp36 = tmp24 + tmp35 tmp38 = tmp37 - tmp1 tmp40 = -tmp39 tmp41 = libdevice.log1p(tmp40) tmp42 = triton_helpers.maximum(tmp41, tmp6) tmp43 = tmp38 * tmp42 tmp44 = tl_math.log(tmp39) tmp45 = triton_helpers.maximum(tmp44, tmp6) tmp46 = tmp37 * tmp45 tmp47 = tmp43 - tmp46 tmp48 = tmp36 + tmp47 tmp49 = 4.0 tmp50 = tmp48 / tmp49 tl.store(out_ptr0 + x2, tmp50, xmask) @triton.jit def triton_poi_fused_mul_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 64 x2 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask) tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x2, tmp2, xmask) def call(args): arg0_1, arg1_1, arg2_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_binary_cross_entropy_mean_0[grid(64)](arg0_1, arg1_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 del arg1_1 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_mul_1[grid(256)](buf0, arg2_1, buf1, 256, XBLOCK= 256, num_warps=4, num_stages=1) del arg2_1 del buf0 return buf1, class HandTypeLossNew(nn.Module): def __init__(self): super(HandTypeLossNew, self).__init__() def forward(self, input_0, input_1, input_2): arg0_1 = input_0 arg1_1 = input_1 arg2_1 = input_2 output = call([arg0_1, arg1_1, arg2_1]) return output[0]
DuinoDu/InterHand2.6M.pl
HandTypeLoss
false
5,093
[ "MIT" ]
1
2d216960cf95b066a197a9b49795840b1ecfd0c1
https://github.com/DuinoDu/InterHand2.6M.pl/tree/2d216960cf95b066a197a9b49795840b1ecfd0c1
ScaledDotProductAttention
import torch import torch.nn as nn import torch.nn.functional as F class ScaledDotProductAttention(nn.Module): def __init__(self, temperature, attn_dropout=0.1): super().__init__() self.temperature = temperature self.dropout = nn.Dropout(attn_dropout) def forward(self, q, k, v, mask=None): attn = torch.matmul(q, k.transpose(2, 3)) / self.temperature if mask is not None: attn = attn.masked_fill(mask is True, -1000000000.0) attn = self.dropout(F.softmax(attn, dim=-1)) output = torch.matmul(attn, v) return output, attn def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand( [4, 4, 4, 4])] def get_init_inputs(): return [[], {'temperature': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp3 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp1 = 1.0 tmp2 = tmp0 * tmp1 tmp4 = tmp3 * tmp1 tmp6 = tmp5 * tmp1 tmp7 = triton_helpers.maximum(tmp4, tmp6) tmp9 = tmp8 * tmp1 tmp10 = triton_helpers.maximum(tmp7, tmp9) tmp12 = tmp11 * tmp1 tmp13 = triton_helpers.maximum(tmp10, tmp12) tmp14 = tmp2 - tmp13 tmp15 = 0.25 tmp16 = tmp14 * tmp15 tmp17 = tl_math.exp(tmp16) tl.store(out_ptr0 + x2, tmp17, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): arg0_1, arg1_1, arg2_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(arg0_1, (16, 4, 4), (16, 1, 4), 0), out=buf0) del arg0_1 del arg1_1 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused__softmax_0[grid(256)](buf0, buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf2 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 triton_poi_fused__softmax_1[grid(256)](buf1, buf2, 256, XBLOCK=256, num_warps=4, num_stages=1) buf3 = reinterpret_tensor(buf1, (16, 4, 4), (16, 4, 1), 0) del buf1 extern_kernels.bmm(reinterpret_tensor(buf2, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(arg2_1, (16, 4, 4), (16, 4, 1), 0), out=buf3 ) del arg2_1 return reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0), buf2 class ScaledDotProductAttentionNew(nn.Module): def __init__(self, temperature, attn_dropout=0.1): super().__init__() self.temperature = temperature self.dropout = nn.Dropout(attn_dropout) def forward(self, input_0, input_1, input_2): arg0_1 = input_0 arg1_1 = input_1 arg2_1 = input_2 output = call([arg0_1, arg1_1, arg2_1]) return output[0], output[1]
Eddie-Hwang/Co-Eye_Motion_Generation
ScaledDotProductAttention
false
5,094
[ "MIT" ]
1
8e244680115fb63bc26018cb6b53bcfbd04e9683
https://github.com/Eddie-Hwang/Co-Eye_Motion_Generation/tree/8e244680115fb63bc26018cb6b53bcfbd04e9683
StableBCELoss
import torch class StableBCELoss(torch.nn.modules.Module): def __init__(self): super(StableBCELoss, self).__init__() def forward(self, input, target): neg_abs = -input.abs() loss = input.clamp(min=0) - input * target + (1 + neg_abs.exp()).log() return loss.mean() def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_abs_add_clamp_exp_log_mean_mul_neg_sub_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp3 = tl.load(in_ptr1 + r0, None) tmp1 = 0.0 tmp2 = triton_helpers.maximum(tmp0, tmp1) tmp4 = tmp0 * tmp3 tmp5 = tmp2 - tmp4 tmp6 = tl_math.abs(tmp0) tmp7 = -tmp6 tmp8 = tl_math.exp(tmp7) tmp9 = 1.0 tmp10 = tmp8 + tmp9 tmp11 = tl_math.log(tmp10) tmp12 = tmp5 + tmp11 tmp13 = tl.broadcast_to(tmp12, [RBLOCK]) tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0)) tmp16 = 256.0 tmp17 = tmp15 / tmp16 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp17, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_abs_add_clamp_exp_log_mean_mul_neg_sub_0[grid(1)](buf1 , arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, class StableBCELossNew(torch.nn.modules.Module): def __init__(self): super(StableBCELossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
EastGit0/JITNet_segmentation
StableBCELoss
false
5,095
[ "MIT" ]
1
7f6598a38b39dafbe6def90385e342b12982143e
https://github.com/EastGit0/JITNet_segmentation/tree/7f6598a38b39dafbe6def90385e342b12982143e
MaxPool2d
import torch import numpy as np import torch.nn as nn from numbers import Number def normcdf(value, mu=0.0, stddev=1.0): sinv = 1.0 / stddev if isinstance(stddev, Number) else stddev.reciprocal() return 0.5 * (1.0 + torch.erf((value - mu) * sinv / np.sqrt(2.0))) def _normal_log_pdf(value, mu, stddev): var = stddev ** 2 log_scale = np.log(stddev) if isinstance(stddev, Number) else torch.log( stddev) return -(value - mu) ** 2 / (2.0 * var) - log_scale - np.log(np.sqrt( 2.0 * np.pi)) def normpdf(value, mu=0.0, stddev=1.0): return torch.exp(_normal_log_pdf(value, mu, stddev)) def keep_variance_fn(x): return x + 0.001 class MaxPool2d(nn.Module): def __init__(self, keep_variance_fn=None): super(MaxPool2d, self).__init__() self._keep_variance_fn = keep_variance_fn def _max_pool_internal(self, mu_a, mu_b, var_a, var_b): stddev = torch.sqrt(var_a + var_b) ab = mu_a - mu_b alpha = ab / stddev pdf = normpdf(alpha) cdf = normcdf(alpha) z_mu = stddev * pdf + ab * cdf + mu_b z_var = (mu_a + mu_b) * stddev * pdf + (mu_a ** 2 + var_a) * cdf + ( mu_b ** 2 + var_b) * (1.0 - cdf) - z_mu ** 2 if self._keep_variance_fn is not None: z_var = self._keep_variance_fn(z_var) return z_mu, z_var def _max_pool_1x2(self, inputs_mean, inputs_variance): mu_a = inputs_mean[:, :, :, 0::2] mu_b = inputs_mean[:, :, :, 1::2] var_a = inputs_variance[:, :, :, 0::2] var_b = inputs_variance[:, :, :, 1::2] outputs_mean, outputs_variance = self._max_pool_internal(mu_a, mu_b, var_a, var_b) return outputs_mean, outputs_variance def _max_pool_2x1(self, inputs_mean, inputs_variance): mu_a = inputs_mean[:, :, 0::2, :] mu_b = inputs_mean[:, :, 1::2, :] var_a = inputs_variance[:, :, 0::2, :] var_b = inputs_variance[:, :, 1::2, :] outputs_mean, outputs_variance = self._max_pool_internal(mu_a, mu_b, var_a, var_b) return outputs_mean, outputs_variance def forward(self, inputs_mean, inputs_variance): z_mean, z_variance = self._max_pool_1x2(inputs_mean, inputs_variance) outputs_mean, outputs_variance = self._max_pool_2x1(z_mean, z_variance) return outputs_mean, outputs_variance def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import numpy as np import torch.nn as nn from numbers import Number assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_div_erf_exp_log_mul_neg_pow_rsub_sqrt_sub_0( in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 2 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + 2 * x0, xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 2 * x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = libdevice.sqrt(tmp5) tmp7 = tmp2 * tmp6 tmp8 = tmp0 - tmp1 tmp9 = tmp8 / tmp6 tmp10 = 0.0 tmp11 = tmp9 - tmp10 tmp12 = tmp11 * tmp11 tmp13 = -tmp12 tmp14 = 0.5 tmp15 = tmp13 * tmp14 tmp16 = 0.9189385332046727 tmp17 = tmp15 - tmp16 tmp18 = tl_math.exp(tmp17) tmp19 = tmp7 * tmp18 tmp20 = tmp0 * tmp0 tmp21 = tmp20 + tmp3 tmp22 = 1.0 tmp23 = tmp11 * tmp22 tmp24 = 1.414213562373095 tmp25 = tmp23 / tmp24 tmp26 = libdevice.erf(tmp25) tmp27 = tmp26 + tmp22 tmp28 = tmp27 * tmp14 tmp29 = tmp21 * tmp28 tmp30 = tmp19 + tmp29 tmp31 = tmp6 * tmp18 tmp32 = tmp8 * tmp28 tmp33 = tmp31 + tmp32 tmp34 = tmp33 + tmp1 tmp35 = tmp34 * tmp34 tmp36 = tmp1 * tmp1 tmp37 = tmp36 + tmp4 tmp38 = tmp22 - tmp28 tmp39 = tmp37 * tmp38 tmp40 = tmp30 + tmp39 tmp41 = tmp40 - tmp35 tl.store(in_out_ptr0 + x0, tmp41, xmask) @triton.jit def triton_poi_fused_add_div_erf_exp_log_mul_neg_pow_rsub_sqrt_sub_1( in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, out_ptr2, xnumel, XBLOCK: tl. constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 2 x1 = xindex // 2 x2 = xindex tmp0 = tl.load(in_ptr0 + (4 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (5 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr1 + (4 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr1 + (5 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp29 = tl.load(in_ptr0 + (2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp30 = tl.load(in_ptr0 + (1 + 2 * x0 + 8 * x1), xmask, eviction_policy ='evict_last') tmp33 = tl.load(in_ptr1 + (2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp34 = tl.load(in_ptr1 + (1 + 2 * x0 + 8 * x1), xmask, eviction_policy ='evict_last') tmp54 = tl.load(in_ptr2 + (x0 + 4 * x1), xmask) tmp55 = tl.load(in_ptr2 + (2 + x0 + 4 * x1), xmask) tmp2 = tmp0 + tmp1 tmp3 = libdevice.sqrt(tmp2) tmp6 = tmp4 - tmp5 tmp7 = tmp6 / tmp3 tmp8 = 0.0 tmp9 = tmp7 - tmp8 tmp10 = tmp9 * tmp9 tmp11 = -tmp10 tmp12 = 0.5 tmp13 = tmp11 * tmp12 tmp14 = 0.9189385332046727 tmp15 = tmp13 - tmp14 tmp16 = tl_math.exp(tmp15) tmp17 = tmp3 * tmp16 tmp18 = 1.0 tmp19 = tmp9 * tmp18 tmp20 = 1.414213562373095 tmp21 = tmp19 / tmp20 tmp22 = libdevice.erf(tmp21) tmp23 = tmp22 + tmp18 tmp24 = tmp23 * tmp12 tmp25 = tmp6 * tmp24 tmp26 = tmp17 + tmp25 tmp27 = tmp26 + tmp5 tmp28 = tmp27 * tmp27 tmp31 = tmp29 + tmp30 tmp32 = libdevice.sqrt(tmp31) tmp35 = tmp33 - tmp34 tmp36 = tmp35 / tmp32 tmp37 = tmp36 - tmp8 tmp38 = tmp37 * tmp37 tmp39 = -tmp38 tmp40 = tmp39 * tmp12 tmp41 = tmp40 - tmp14 tmp42 = tl_math.exp(tmp41) tmp43 = tmp32 * tmp42 tmp44 = tmp37 * tmp18 tmp45 = tmp44 / tmp20 tmp46 = libdevice.erf(tmp45) tmp47 = tmp46 + tmp18 tmp48 = tmp47 * tmp12 tmp49 = tmp35 * tmp48 tmp50 = tmp43 + tmp49 tmp51 = tmp50 + tmp34 tmp52 = tmp51 + tmp27 tmp53 = tmp51 - tmp27 tmp56 = tmp54 + tmp55 tmp57 = libdevice.sqrt(tmp56) tmp58 = tmp53 / tmp57 tmp59 = tmp58 - tmp8 tmp60 = tmp59 * tmp59 tmp61 = -tmp60 tmp62 = tmp61 * tmp12 tmp63 = tmp62 - tmp14 tmp64 = tl_math.exp(tmp63) tmp65 = tmp57 * tmp64 tmp66 = tmp59 * tmp18 tmp67 = tmp66 / tmp20 tmp68 = libdevice.erf(tmp67) tmp69 = tmp68 + tmp18 tmp70 = tmp69 * tmp12 tmp71 = tmp53 * tmp70 tmp72 = tmp65 + tmp71 tmp73 = tmp72 + tmp27 tmp74 = tmp51 * tmp51 tmp75 = tmp52 * tmp57 tmp76 = tmp75 * tmp64 tmp77 = tmp74 + tmp54 tmp78 = tmp77 * tmp70 tmp79 = tmp76 + tmp78 tmp80 = tmp28 + tmp55 tmp81 = tmp18 - tmp70 tmp82 = tmp80 * tmp81 tmp83 = tmp79 + tmp82 tmp84 = tmp73 * tmp73 tmp85 = tmp83 - tmp84 tl.store(out_ptr2 + x2, tmp73, xmask) tl.store(in_out_ptr0 + x2, tmp85, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((4, 4, 4, 2), (32, 8, 2, 1), torch.float32) buf3 = buf1 del buf1 get_raw_stream(0) triton_poi_fused_add_div_erf_exp_log_mul_neg_pow_rsub_sqrt_sub_0[grid (128)](buf3, arg0_1, arg1_1, 128, XBLOCK=128, num_warps=4, num_stages=1) buf0 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32) buf8 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32) buf6 = buf0 del buf0 buf9 = buf6 del buf6 triton_poi_fused_add_div_erf_exp_log_mul_neg_pow_rsub_sqrt_sub_1[grid (64)](buf9, arg1_1, arg0_1, buf3, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 del arg1_1 del buf3 return buf8, buf9 def normcdf(value, mu=0.0, stddev=1.0): sinv = 1.0 / stddev if isinstance(stddev, Number) else stddev.reciprocal() return 0.5 * (1.0 + torch.erf((value - mu) * sinv / np.sqrt(2.0))) def _normal_log_pdf(value, mu, stddev): var = stddev ** 2 log_scale = np.log(stddev) if isinstance(stddev, Number) else torch.log( stddev) return -(value - mu) ** 2 / (2.0 * var) - log_scale - np.log(np.sqrt( 2.0 * np.pi)) def normpdf(value, mu=0.0, stddev=1.0): return torch.exp(_normal_log_pdf(value, mu, stddev)) def keep_variance_fn(x): return x + 0.001 class MaxPool2dNew(nn.Module): def __init__(self, keep_variance_fn=None): super(MaxPool2dNew, self).__init__() self._keep_variance_fn = keep_variance_fn def _max_pool_internal(self, mu_a, mu_b, var_a, var_b): stddev = torch.sqrt(var_a + var_b) ab = mu_a - mu_b alpha = ab / stddev pdf = normpdf(alpha) cdf = normcdf(alpha) z_mu = stddev * pdf + ab * cdf + mu_b z_var = (mu_a + mu_b) * stddev * pdf + (mu_a ** 2 + var_a) * cdf + ( mu_b ** 2 + var_b) * (1.0 - cdf) - z_mu ** 2 if self._keep_variance_fn is not None: z_var = self._keep_variance_fn(z_var) return z_mu, z_var def _max_pool_1x2(self, inputs_mean, inputs_variance): mu_a = inputs_mean[:, :, :, 0::2] mu_b = inputs_mean[:, :, :, 1::2] var_a = inputs_variance[:, :, :, 0::2] var_b = inputs_variance[:, :, :, 1::2] outputs_mean, outputs_variance = self._max_pool_internal(mu_a, mu_b, var_a, var_b) return outputs_mean, outputs_variance def _max_pool_2x1(self, inputs_mean, inputs_variance): mu_a = inputs_mean[:, :, 0::2, :] mu_b = inputs_mean[:, :, 1::2, :] var_a = inputs_variance[:, :, 0::2, :] var_b = inputs_variance[:, :, 1::2, :] outputs_mean, outputs_variance = self._max_pool_internal(mu_a, mu_b, var_a, var_b) return outputs_mean, outputs_variance def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0], output[1]
DoggyLiu0116/MamboNet
MaxPool2d
false
5,096
[ "MIT" ]
1
3b708091422491f660c4bd5eb12b06ce3b8a5f79
https://github.com/DoggyLiu0116/MamboNet/tree/3b708091422491f660c4bd5eb12b06ce3b8a5f79
ClassWisePool
import torch from torch import nn class ClassWisePool(nn.Module): def __init__(self, num_maps): super(ClassWisePool, self).__init__() self.num_maps = num_maps def forward(self, input): batch_size, num_channels, s = input.size() num_outputs = int(num_channels / self.num_maps) x = input.view(batch_size, num_outputs, self.num_maps, s) output = torch.sum(x, 2) return output.view(batch_size, num_outputs, s) / self.num_maps def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'num_maps': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_div_sum_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask) tmp1 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask) tmp3 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask) tmp5 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 0.25 tmp8 = tmp6 * tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 1, 4), (4, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_div_sum_0[grid(16)](arg0_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg0_1 return buf0, class ClassWisePoolNew(nn.Module): def __init__(self, num_maps): super(ClassWisePoolNew, self).__init__() self.num_maps = num_maps def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
Ecocytus/Roberta-ZeroShot-Label
ClassWisePool
false
5,097
[ "MIT" ]
1
8a6d74187a0e2fd5b1b75549cfb724f54269c5a5
https://github.com/Ecocytus/Roberta-ZeroShot-Label/tree/8a6d74187a0e2fd5b1b75549cfb724f54269c5a5
SimpleArch
import torch import torch.nn as nn class SimpleArch(nn.Module): def __init__(self, input_size, dropout=0.1, hidden_layer_size=10, output_neurons=1): """ A simple architecture wrapper -- build with intuitive Sklearn-like API. """ super(SimpleArch, self).__init__() self.h1 = nn.Linear(input_size, hidden_layer_size) self.h2 = nn.Linear(hidden_layer_size, hidden_layer_size) self.h3 = nn.Linear(hidden_layer_size, 16) self.h4 = nn.Linear(16, output_neurons) self.drop = nn.Dropout(dropout) self.act = nn.ELU() self.sigma = nn.Sigmoid() def forward(self, x): """ The standard forward pass. See the original paper for the formal description of this part of the DRMs. """ out = self.h1(x) out = self.drop(out) out = self.act(out) out = self.h2(out) out = self.drop(out) out = self.act(out) out = self.h3(out) out = self.drop(out) out = self.act(out) out = self.h4(out) out = self.sigma(out) return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_elu_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 640 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.0 tmp2 = tmp0 > tmp1 tmp3 = 1.0 tmp4 = tmp0 * tmp3 tmp5 = libdevice.expm1(tmp4) tmp6 = tmp5 * tmp3 tmp7 = tl.where(tmp2, tmp4, tmp6) tl.store(out_ptr0 + x0, tmp7, xmask) @triton.jit def triton_poi_fused_elu_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.0 tmp2 = tmp0 > tmp1 tmp3 = 1.0 tmp4 = tmp0 * tmp3 tmp5 = libdevice.expm1(tmp4) tmp6 = tmp5 * tmp3 tmp7 = tl.where(tmp2, tmp4, tmp6) tl.store(out_ptr0 + x0, tmp7, xmask) @triton.jit def triton_poi_fused_sigmoid_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp3 = tmp0 + tmp2 tmp4 = tl.sigmoid(tmp3) tl.store(in_out_ptr0 + x0, tmp4, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9) = args args.clear() assert_size_stride(primals_1, (10, 4), (4, 1)) assert_size_stride(primals_2, (10,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (10, 10), (10, 1)) assert_size_stride(primals_5, (10,), (1,)) assert_size_stride(primals_6, (16, 10), (10, 1)) assert_size_stride(primals_7, (16,), (1,)) assert_size_stride(primals_8, (1, 16), (16, 1)) assert_size_stride(primals_9, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 10), (10, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 10), (1, 4), 0), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((4, 4, 4, 10), (160, 40, 10, 1), torch. float32) get_raw_stream(0) triton_poi_fused_elu_0[grid(640)](buf0, buf1, 640, XBLOCK=256, num_warps=4, num_stages=1) buf2 = empty_strided_cuda((64, 10), (10, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 10), (10, 1), 0), reinterpret_tensor(primals_4, (10, 10), (1, 10), 0 ), alpha=1, beta=1, out=buf2) del primals_5 buf3 = empty_strided_cuda((4, 4, 4, 10), (160, 40, 10, 1), torch. float32) triton_poi_fused_elu_0[grid(640)](buf2, buf3, 640, XBLOCK=256, num_warps=4, num_stages=1) buf4 = empty_strided_cuda((64, 16), (16, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 10), (10, 1), 0), reinterpret_tensor(primals_6, (10, 16), (1, 10), 0 ), alpha=1, beta=1, out=buf4) del primals_7 buf5 = empty_strided_cuda((4, 4, 4, 16), (256, 64, 16, 1), torch. float32) triton_poi_fused_elu_1[grid(1024)](buf4, buf5, 1024, XBLOCK=128, num_warps=4, num_stages=1) buf6 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf5, (64, 16), (16, 1), 0), reinterpret_tensor(primals_8, (16, 1), (1, 16), 0), out=buf6) buf7 = reinterpret_tensor(buf6, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf6 triton_poi_fused_sigmoid_2[grid(64)](buf7, primals_9, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_9 return buf7, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf0, reinterpret_tensor(buf1, (64, 10), (10, 1), 0 ), buf2, reinterpret_tensor(buf3, (64, 10), (10, 1), 0 ), buf4, reinterpret_tensor(buf5, (64, 16), (16, 1), 0 ), buf7, primals_8, primals_6, primals_4 class SimpleArchNew(nn.Module): def __init__(self, input_size, dropout=0.1, hidden_layer_size=10, output_neurons=1): """ A simple architecture wrapper -- build with intuitive Sklearn-like API. """ super(SimpleArchNew, self).__init__() self.h1 = nn.Linear(input_size, hidden_layer_size) self.h2 = nn.Linear(hidden_layer_size, hidden_layer_size) self.h3 = nn.Linear(hidden_layer_size, 16) self.h4 = nn.Linear(16, output_neurons) self.drop = nn.Dropout(dropout) self.act = nn.ELU() self.sigma = nn.Sigmoid() def forward(self, input_0): primals_1 = self.h1.weight primals_2 = self.h1.bias primals_4 = self.h2.weight primals_5 = self.h2.bias primals_6 = self.h3.weight primals_7 = self.h3.bias primals_8 = self.h4.weight primals_9 = self.h4.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9]) return output[0]
EMBEDDIA/PropStar
SimpleArch
false
5,098
[ "BSD-3-Clause" ]
1
987be390775130893f2c3440a5f1f94025309e4d
https://github.com/EMBEDDIA/PropStar/tree/987be390775130893f2c3440a5f1f94025309e4d
APPNProp
import torch import torch.nn.functional as F import torch.nn as nn class SparseDropout(nn.Module): def __init__(self, p=0.5): super().__init__() self.p = p def forward(self, x): if not self.training: return x x_coal = x.coalesce() drop_val = F.dropout(x_coal._values(), self.p, self.training) return torch.sparse.FloatTensor(x_coal._indices(), drop_val, x.shape) class MixedDropout(nn.Module): def __init__(self, p=0.5): super().__init__() self.dense_dropout = nn.Dropout(p) self.sparse_dropout = SparseDropout(p) def forward(self, x): if x.is_sparse: return self.sparse_dropout(x) else: return self.dense_dropout(x) class APPNProp(nn.Module): def __init__(self, alpha: 'float'=0.1, K: 'int'=10, dropout: 'float'=0.0): super().__init__() self.alpha = alpha self.K = K if not dropout: self.dropout = lambda x: x else: self.dropout = MixedDropout(dropout) def forward(self, x, adj): h = x for _ in range(self.K): A_drop = self.dropout(adj) h = (1 - self.alpha) * A_drop.mm(h) + self.alpha * x return h def __repr__(self): return ( f'{self.__class__.__name__}(alpha={self.alpha}, K={self.K}, dropout={self.dropout})' ) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn.functional as F import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_mul_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp3 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.9 tmp2 = tmp0 * tmp1 tmp4 = 0.1 tmp5 = tmp3 * tmp4 tmp6 = tmp2 + tmp5 tl.store(in_out_ptr0 + x0, tmp6, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4), (4, 1)) assert_size_stride(arg1_1, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, arg0_1, out=buf0) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_add_mul_0[grid(16)](buf1, arg0_1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf1, out=buf2) buf3 = buf2 del buf2 triton_poi_fused_add_mul_0[grid(16)](buf3, arg0_1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf4 = buf1 del buf1 extern_kernels.mm(arg1_1, buf3, out=buf4) buf5 = buf4 del buf4 triton_poi_fused_add_mul_0[grid(16)](buf5, arg0_1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf6 = buf3 del buf3 extern_kernels.mm(arg1_1, buf5, out=buf6) buf7 = buf6 del buf6 triton_poi_fused_add_mul_0[grid(16)](buf7, arg0_1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf8 = buf5 del buf5 extern_kernels.mm(arg1_1, buf7, out=buf8) buf9 = buf8 del buf8 triton_poi_fused_add_mul_0[grid(16)](buf9, arg0_1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf10 = buf7 del buf7 extern_kernels.mm(arg1_1, buf9, out=buf10) buf11 = buf10 del buf10 triton_poi_fused_add_mul_0[grid(16)](buf11, arg0_1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf12 = buf9 del buf9 extern_kernels.mm(arg1_1, buf11, out=buf12) buf13 = buf12 del buf12 triton_poi_fused_add_mul_0[grid(16)](buf13, arg0_1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf14 = buf11 del buf11 extern_kernels.mm(arg1_1, buf13, out=buf14) buf15 = buf14 del buf14 triton_poi_fused_add_mul_0[grid(16)](buf15, arg0_1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf16 = buf13 del buf13 extern_kernels.mm(arg1_1, buf15, out=buf16) buf17 = buf16 del buf16 triton_poi_fused_add_mul_0[grid(16)](buf17, arg0_1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf18 = buf15 del buf15 extern_kernels.mm(arg1_1, buf17, out=buf18) del arg1_1 del buf17 buf19 = buf18 del buf18 triton_poi_fused_add_mul_0[grid(16)](buf19, arg0_1, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg0_1 return buf19, class SparseDropout(nn.Module): def __init__(self, p=0.5): super().__init__() self.p = p def forward(self, x): if not self.training: return x x_coal = x.coalesce() drop_val = F.dropout(x_coal._values(), self.p, self.training) return torch.sparse.FloatTensor(x_coal._indices(), drop_val, x.shape) class MixedDropout(nn.Module): def __init__(self, p=0.5): super().__init__() self.dense_dropout = nn.Dropout(p) self.sparse_dropout = SparseDropout(p) def forward(self, x): if x.is_sparse: return self.sparse_dropout(x) else: return self.dense_dropout(x) class APPNPropNew(nn.Module): def __init__(self, alpha: 'float'=0.1, K: 'int'=10, dropout: 'float'=0.0): super().__init__() self.alpha = alpha self.K = K if not dropout: self.dropout = lambda x: x else: self.dropout = MixedDropout(dropout) def __repr__(self): return ( f'{self.__class__.__name__}(alpha={self.alpha}, K={self.K}, dropout={self.dropout})' ) def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
EdisonLeeeee/Graphgallery
APPNProp
false
5,099
[ "MIT" ]
1
8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
https://github.com/EdisonLeeeee/Graphgallery/tree/8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
NormedConv2d
import torch from torch import nn import torch.onnx class NormedConv2d(nn.Conv2d): """Normalized Conv2d Layer. Args: tempeature (float, optional): Tempeature term. Default to 20. power (int, optional): Power term. Default to 1.0. eps (float, optional): The minimal value of divisor to keep numerical stability. Default to 1e-6. norm_over_kernel (bool, optional): Normalize over kernel. Default to False. """ def __init__(self, *args, tempearture=20, power=1.0, eps=1e-06, norm_over_kernel=False, **kwargs): super(NormedConv2d, self).__init__(*args, **kwargs) self.tempearture = tempearture self.power = power self.norm_over_kernel = norm_over_kernel self.eps = eps def forward(self, x): if not self.norm_over_kernel: weight_ = self.weight / (self.weight.norm(dim=1, keepdim=True). pow(self.power) + self.eps) else: weight_ = self.weight / (self.weight.view(self.weight.size(0), -1).norm(dim=1, keepdim=True).pow(self.power)[..., None, None] + self.eps) x_ = x / (x.norm(dim=1, keepdim=True).pow(self.power) + self.eps) x_ = x_ * self.tempearture if hasattr(self, 'conv2d_forward'): x_ = self.conv2d_forward(x_, weight_) elif torch.__version__ >= '1.8': x_ = self._conv_forward(x_, weight_, self.bias) else: x_ = self._conv_forward(x_, weight_) return x_ def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4, 'kernel_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice from torch import nn import torch.onnx assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_div_linalg_vector_norm_pow_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = libdevice.sqrt(tmp11) tmp13 = 1e-06 tmp14 = tmp12 + tmp13 tmp15 = tmp0 / tmp14 tl.store(out_ptr0 + x3, tmp15, xmask) @triton.jit def triton_poi_fused_add_div_linalg_vector_norm_mul_pow_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = libdevice.sqrt(tmp11) tmp13 = 1e-06 tmp14 = tmp12 + tmp13 tmp15 = tmp0 / tmp14 tmp16 = 20.0 tmp17 = tmp15 * tmp16 tl.store(out_ptr0 + x3, tmp17, xmask) @triton.jit def triton_poi_fused_convolution_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x2, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_linalg_vector_norm_pow_0[grid(256)](primals_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_add_div_linalg_vector_norm_mul_pow_1[grid(256)]( primals_2, buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = extern_kernels.convolution(buf1, buf0, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 4, 1, 1), (4, 1, 1, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_2[grid(16)](buf3, primals_3, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_3 return buf3, primals_1, buf0, buf1 class NormedConv2dNew(nn.Conv2d): """Normalized Conv2d Layer. Args: tempeature (float, optional): Tempeature term. Default to 20. power (int, optional): Power term. Default to 1.0. eps (float, optional): The minimal value of divisor to keep numerical stability. Default to 1e-6. norm_over_kernel (bool, optional): Normalize over kernel. Default to False. """ def __init__(self, *args, tempearture=20, power=1.0, eps=1e-06, norm_over_kernel=False, **kwargs): super(NormedConv2dNew, self).__init__(*args, **kwargs) self.tempearture = tempearture self.power = power self.norm_over_kernel = norm_over_kernel self.eps = eps def forward(self, input_0): primals_1 = self.weight primals_3 = self.bias primals_2 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
ENOT-AutoDL/mmdetection-enot
NormedConv2d
false
5,100
[ "Apache-2.0" ]
1
f541749554436e3327bac00eee89b84f66c03551
https://github.com/ENOT-AutoDL/mmdetection-enot/tree/f541749554436e3327bac00eee89b84f66c03551
ClassificationModel
import torch from torch import nn class ClassificationModel(nn.Module): def __init__(self, num_features_in, num_anchors=9, num_classes=80, prior=0.01, feature_size=256): super(ClassificationModel, self).__init__() self.num_classes = num_classes self.num_anchors = num_anchors self.conv1 = nn.Conv2d(num_features_in, feature_size, kernel_size=3, padding=1) self.act1 = nn.ReLU() self.conv2 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1) self.act2 = nn.ReLU() self.conv3 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1) self.act3 = nn.ReLU() self.conv4 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1) self.act4 = nn.ReLU() self.output = nn.Conv2d(feature_size, num_anchors * num_classes, kernel_size=3, padding=1) self.output_act = nn.Sigmoid() def forward(self, x): out = self.conv1(x) out = self.act1(out) out = self.conv2(out) out = self.act2(out) out = self.conv3(out) out = self.act3(out) out = self.conv4(out) out = self.act4(out) out = self.output(out) out = self.output_act(out) out1 = out.permute(0, 2, 3, 1) batch_size, width, height, _channels = out1.shape out2 = out1.view(batch_size, width, height, self.num_anchors, self. num_classes) return out2.contiguous().view(x.shape[0], -1, self.num_classes) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_features_in': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 9 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 4 y1 = yindex // 4 tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last' ) tl.store(out_ptr0 + (y0 + 4 * x2 + 36 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 16 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 4 y1 = yindex // 4 tmp0 = tl.load(in_ptr0 + (x2 + 16 * y3), xmask & ymask) tl.store(out_ptr0 + (y0 + 4 * x2 + 64 * y1), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_2(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 9 yoffset = (tl.program_id(1) + tl.program_id(2) * tl.num_programs(1) ) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 256 y1 = yindex // 256 tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last' ) tl.store(out_ptr0 + (y0 + 256 * x2 + 2304 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 9 yoffset = (tl.program_id(1) + tl.program_id(2) * tl.num_programs(1) ) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 256 y1 = yindex // 256 tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last' ) tl.store(out_ptr0 + (y0 + 256 * x2 + 2304 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_convolution_relu_4(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 256 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, None) @triton.jit def triton_poi_fused_clone_convolution_5(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 46080 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 720 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.sigmoid(tmp2) tl.store(in_out_ptr0 + x2, tmp2, xmask) tl.store(out_ptr0 + x2, tmp3, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11) = args args.clear() assert_size_stride(primals_1, (256, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_2, (256,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_5, (256,), (1,)) assert_size_stride(primals_6, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_7, (256,), (1,)) assert_size_stride(primals_8, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_9, (256,), (1,)) assert_size_stride(primals_10, (720, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_11, (720,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((256, 4, 3, 3), (36, 1, 12, 4), torch.float32 ) get_raw_stream(0) triton_poi_fused_0[grid(1024, 9)](primals_1, buf0, 1024, 9, XBLOCK= 16, YBLOCK=64, num_warps=4, num_stages=1) del primals_1 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 1, 16, 4), torch.float32) triton_poi_fused_1[grid(16, 16)](primals_3, buf1, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) del primals_3 buf2 = empty_strided_cuda((256, 256, 3, 3), (2304, 1, 768, 256), torch.float32) triton_poi_fused_2[grid(65536, 9)](primals_4, buf2, 65536, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_4 buf3 = empty_strided_cuda((256, 256, 3, 3), (2304, 1, 768, 256), torch.float32) triton_poi_fused_2[grid(65536, 9)](primals_6, buf3, 65536, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_6 buf4 = empty_strided_cuda((256, 256, 3, 3), (2304, 1, 768, 256), torch.float32) triton_poi_fused_2[grid(65536, 9)](primals_8, buf4, 65536, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_8 buf5 = empty_strided_cuda((720, 256, 3, 3), (2304, 1, 768, 256), torch.float32) triton_poi_fused_3[grid(184320, 9)](primals_10, buf5, 184320, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_10 buf6 = extern_kernels.convolution(buf1, buf0, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf6, (4, 256, 4, 4), (4096, 1, 1024, 256)) buf7 = buf6 del buf6 triton_poi_fused_convolution_relu_4[grid(16384)](buf7, primals_2, 16384, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf8 = extern_kernels.convolution(buf7, buf2, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 256, 4, 4), (4096, 1, 1024, 256)) buf9 = buf8 del buf8 triton_poi_fused_convolution_relu_4[grid(16384)](buf9, primals_5, 16384, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf10 = extern_kernels.convolution(buf9, buf3, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf10, (4, 256, 4, 4), (4096, 1, 1024, 256)) buf11 = buf10 del buf10 triton_poi_fused_convolution_relu_4[grid(16384)](buf11, primals_7, 16384, XBLOCK=128, num_warps=4, num_stages=1) del primals_7 buf12 = extern_kernels.convolution(buf11, buf4, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf12, (4, 256, 4, 4), (4096, 1, 1024, 256)) buf13 = buf12 del buf12 triton_poi_fused_convolution_relu_4[grid(16384)](buf13, primals_9, 16384, XBLOCK=128, num_warps=4, num_stages=1) del primals_9 buf14 = extern_kernels.convolution(buf13, buf5, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf14, (4, 720, 4, 4), (11520, 1, 2880, 720)) buf15 = buf14 del buf14 buf16 = empty_strided_cuda((4, 4, 4, 9, 80), (11520, 2880, 720, 80, 1), torch.float32) triton_poi_fused_clone_convolution_5[grid(46080)](buf15, primals_11, buf16, 46080, XBLOCK=256, num_warps=4, num_stages=1) del primals_11 return reinterpret_tensor(buf16, (4, 144, 80), (11520, 80, 1), 0 ), buf0, buf1, buf2, buf3, buf4, buf5, buf7, buf9, buf11, buf13, buf15 class ClassificationModelNew(nn.Module): def __init__(self, num_features_in, num_anchors=9, num_classes=80, prior=0.01, feature_size=256): super(ClassificationModelNew, self).__init__() self.num_classes = num_classes self.num_anchors = num_anchors self.conv1 = nn.Conv2d(num_features_in, feature_size, kernel_size=3, padding=1) self.act1 = nn.ReLU() self.conv2 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1) self.act2 = nn.ReLU() self.conv3 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1) self.act3 = nn.ReLU() self.conv4 = nn.Conv2d(feature_size, feature_size, kernel_size=3, padding=1) self.act4 = nn.ReLU() self.output = nn.Conv2d(feature_size, num_anchors * num_classes, kernel_size=3, padding=1) self.output_act = nn.Sigmoid() def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_6 = self.conv3.weight primals_7 = self.conv3.bias primals_8 = self.conv4.weight primals_9 = self.conv4.bias primals_10 = self.output.weight primals_11 = self.output.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11]) return output[0]
DerekGloudemans/temporary-repo
ClassificationModel
false
5,101
[ "MIT" ]
1
f278e9c7c9c7c1f362a64aec492ddb8fb1f984ad
https://github.com/DerekGloudemans/temporary-repo/tree/f278e9c7c9c7c1f362a64aec492ddb8fb1f984ad
Merge
import torch import torch.utils.data import torch.nn as nn import torch.utils.checkpoint class Merge(nn.Module): def forward(self, x1, x2): return torch.cat([x1, x2], dim=1) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.utils.data import torch.nn as nn import torch.utils.checkpoint assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 16 % 8 x0 = xindex % 16 x2 = xindex // 128 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 16 * x1 + 64 * x2), tmp4 & xmask, other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 8, tl.int64) tmp9 = tl.load(in_ptr1 + (x0 + 16 * (-4 + x1) + 64 * x2), tmp6 & xmask, other=0.0) tmp10 = tl.where(tmp4, tmp5, tmp9) tl.store(out_ptr0 + x3, tmp10, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 8, 4, 4), (128, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(512)](arg0_1, arg1_1, buf0, 512, XBLOCK =256, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf0, class MergeNew(nn.Module): def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
CNNs4QSPR/se3cnn
Merge
false
5,102
[ "MIT" ]
1
513f5f827c4c511bdc96e3c6ea663c8fbce60f57
https://github.com/CNNs4QSPR/se3cnn/tree/513f5f827c4c511bdc96e3c6ea663c8fbce60f57
GaussionConvF
import torch import torch.nn.functional as F import torch.nn as nn class GaussionConvF(nn.Module): """The first layer in `RobustGCN` that conver node features to distribution (mean, var)""" def __init__(self, in_features, out_features, bias=False, gamma=1.0): super().__init__() self.in_features = in_features self.out_features = out_features self.w = nn.Linear(in_features, out_features, bias=bias) self.gamma = gamma def reset_parameters(self): self.w.reset_parameters() def forward(self, x, adj_mean, adj_var): h = self.w(x) mean = F.elu(h) var = F.relu(h) attention = torch.exp(-self.gamma * var) mean = adj_mean.mm(mean * attention) var = adj_var.mm(var * attention * attention) return mean, var def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features})' ) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_elu_exp_mul_relu_0(in_ptr0, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.0 tmp2 = tmp0 > tmp1 tmp3 = 1.0 tmp4 = tmp0 * tmp3 tmp5 = libdevice.expm1(tmp4) tmp6 = tmp5 * tmp3 tmp7 = tl.where(tmp2, tmp4, tmp6) tmp8 = tl.full([1], 0, tl.int32) tmp9 = triton_helpers.maximum(tmp8, tmp0) tmp10 = -1.0 tmp11 = tmp9 * tmp10 tmp12 = tl_math.exp(tmp11) tmp13 = tmp7 * tmp12 tmp14 = tmp9 * tmp12 tmp15 = tmp14 * tmp12 tl.store(out_ptr0 + x0, tmp13, xmask) tl.store(out_ptr1 + x0, tmp14, xmask) tl.store(out_ptr2 + x0, tmp15, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_2, reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_elu_exp_mul_relu_0[grid(16)](buf0, buf1, buf3, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf1, out=buf2) buf5 = buf1 del buf1 extern_kernels.mm(primals_4, buf4, out=buf5) del buf4 return buf2, buf5, primals_2, buf0, buf3, reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0) class GaussionConvFNew(nn.Module): """The first layer in `RobustGCN` that conver node features to distribution (mean, var)""" def __init__(self, in_features, out_features, bias=False, gamma=1.0): super().__init__() self.in_features = in_features self.out_features = out_features self.w = nn.Linear(in_features, out_features, bias=bias) self.gamma = gamma def reset_parameters(self): self.w.reset_parameters() def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features})' ) def forward(self, input_0, input_1, input_2): primals_1 = self.w.weight primals_2 = input_0 primals_3 = input_1 primals_4 = input_2 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0], output[1]
EdisonLeeeee/Graphgallery
GaussionConvF
false
5,103
[ "MIT" ]
1
8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
https://github.com/EdisonLeeeee/Graphgallery/tree/8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
SSGConv
from torch.nn import Module import torch class SSGConv(Module): def __init__(self, K=16, alpha=0.1, **kwargs): super().__init__() assert K > 0 self.K = K self.alpha = alpha def forward(self, x, adj): x_in = x x_out = torch.zeros_like(x) for _ in range(self.K): x = torch.spmm(adj, x) x_out += (1 - self.alpha) * x x_out /= self.K x_out += self.alpha * x_in return x_out def reset_parameters(self): pass def extra_repr(self): return f'K={self.K}, alpha={self.alpha}' def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch.nn import Module assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_div_mul_0(in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, in_ptr9, in_ptr10, in_ptr11, in_ptr12, in_ptr13, in_ptr14, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp3 = tl.load(in_ptr0 + x0, xmask) tmp6 = tl.load(in_ptr1 + x0, xmask) tmp9 = tl.load(in_ptr2 + x0, xmask) tmp12 = tl.load(in_ptr3 + x0, xmask) tmp15 = tl.load(in_ptr4 + x0, xmask) tmp18 = tl.load(in_ptr5 + x0, xmask) tmp21 = tl.load(in_ptr6 + x0, xmask) tmp24 = tl.load(in_ptr7 + x0, xmask) tmp27 = tl.load(in_out_ptr1 + x0, xmask) tmp30 = tl.load(in_ptr8 + x0, xmask) tmp33 = tl.load(in_ptr9 + x0, xmask) tmp36 = tl.load(in_ptr10 + x0, xmask) tmp39 = tl.load(in_ptr11 + x0, xmask) tmp42 = tl.load(in_ptr12 + x0, xmask) tmp45 = tl.load(in_ptr13 + x0, xmask) tmp50 = tl.load(in_ptr14 + x0, xmask) tmp1 = 0.9 tmp2 = tmp0 * tmp1 tmp4 = tmp3 * tmp1 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp1 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp1 tmp11 = tmp8 + tmp10 tmp13 = tmp12 * tmp1 tmp14 = tmp11 + tmp13 tmp16 = tmp15 * tmp1 tmp17 = tmp14 + tmp16 tmp19 = tmp18 * tmp1 tmp20 = tmp17 + tmp19 tmp22 = tmp21 * tmp1 tmp23 = tmp20 + tmp22 tmp25 = tmp24 * tmp1 tmp26 = tmp23 + tmp25 tmp28 = tmp27 * tmp1 tmp29 = tmp26 + tmp28 tmp31 = tmp30 * tmp1 tmp32 = tmp29 + tmp31 tmp34 = tmp33 * tmp1 tmp35 = tmp32 + tmp34 tmp37 = tmp36 * tmp1 tmp38 = tmp35 + tmp37 tmp40 = tmp39 * tmp1 tmp41 = tmp38 + tmp40 tmp43 = tmp42 * tmp1 tmp44 = tmp41 + tmp43 tmp46 = tmp45 * tmp1 tmp47 = tmp44 + tmp46 tmp48 = 0.0625 tmp49 = tmp47 * tmp48 tmp51 = 0.1 tmp52 = tmp50 * tmp51 tmp53 = tmp49 + tmp52 tl.store(in_out_ptr1 + x0, tmp53, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4), (4, 1)) assert_size_stride(arg1_1, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, arg0_1, out=buf0) buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf0, out=buf1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf1, out=buf2) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf2, out=buf3) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf3, out=buf4) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf4, out=buf5) buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf5, out=buf6) buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf6, out=buf7) buf8 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf7, out=buf8) buf10 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf8, out=buf10) buf11 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf10, out=buf11) buf12 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf11, out=buf12) buf13 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf12, out=buf13) buf14 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf13, out=buf14) buf15 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf14, out=buf15) buf16 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(arg1_1, buf15, out=buf16) del arg1_1 buf9 = buf0 del buf0 buf17 = buf10 del buf10 get_raw_stream(0) triton_poi_fused_add_div_mul_0[grid(16)](buf9, buf17, buf1, buf2, buf3, buf4, buf5, buf6, buf7, buf8, buf11, buf12, buf13, buf14, buf15, buf16, arg0_1, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg0_1 del buf1 del buf11 del buf12 del buf13 del buf14 del buf15 del buf16 del buf2 del buf3 del buf4 del buf5 del buf6 del buf7 del buf8 del buf9 return buf17, class SSGConvNew(Module): def __init__(self, K=16, alpha=0.1, **kwargs): super().__init__() assert K > 0 self.K = K self.alpha = alpha def reset_parameters(self): pass def extra_repr(self): return f'K={self.K}, alpha={self.alpha}' def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
EdisonLeeeee/Graphgallery
SSGConv
false
5,104
[ "MIT" ]
1
8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
https://github.com/EdisonLeeeee/Graphgallery/tree/8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
GaussionConvD
import torch import torch.nn.functional as F import torch.nn as nn class GaussionConvD(nn.Module): """The subsequent layer in `RobustGCN` that takes node distribution (mean, var) as input""" def __init__(self, in_features, out_features, bias=False, gamma=1.0): super().__init__() self.in_features = in_features self.out_features = out_features self.w_mean = nn.Linear(in_features, out_features, bias=bias) self.w_var = nn.Linear(in_features, out_features, bias=bias) self.gamma = gamma def reset_parameters(self): self.w_mean.reset_parameters() self.w_var.reset_parameters() def forward(self, mean, var, adj_mean, adj_var): mean = F.elu(self.w_mean(mean)) var = F.relu(self.w_var(var)) attention = torch.exp(-self.gamma * var) mean = adj_mean.mm(mean * attention) var = adj_var.mm(var * attention * attention) return mean, var def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features})' ) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_elu_exp_mul_relu_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp8 = tl.load(in_ptr1 + x0, xmask) tmp1 = 0.0 tmp2 = tmp0 > tmp1 tmp3 = 1.0 tmp4 = tmp0 * tmp3 tmp5 = libdevice.expm1(tmp4) tmp6 = tmp5 * tmp3 tmp7 = tl.where(tmp2, tmp4, tmp6) tmp9 = tl.full([1], 0, tl.int32) tmp10 = triton_helpers.maximum(tmp9, tmp8) tmp11 = -1.0 tmp12 = tmp10 * tmp11 tmp13 = tl_math.exp(tmp12) tmp14 = tmp7 * tmp13 tmp15 = tmp10 * tmp13 tmp16 = tmp15 * tmp13 tl.store(out_ptr0 + x0, tmp14, xmask) tl.store(out_ptr1 + x0, tmp15, xmask) tl.store(out_ptr2 + x0, tmp16, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4, 4), (4, 1)) assert_size_stride(primals_6, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_2, reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_4, reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf1) del primals_3 buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_elu_exp_mul_relu_0[grid(16)](buf0, buf1, buf2, buf4, buf5, 16, XBLOCK=16, num_warps=1, num_stages=1) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_5, buf2, out=buf3) buf6 = buf2 del buf2 extern_kernels.mm(primals_6, buf5, out=buf6) del buf5 return (buf3, buf6, primals_2, primals_4, buf0, buf1, buf4, reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0)) class GaussionConvDNew(nn.Module): """The subsequent layer in `RobustGCN` that takes node distribution (mean, var) as input""" def __init__(self, in_features, out_features, bias=False, gamma=1.0): super().__init__() self.in_features = in_features self.out_features = out_features self.w_mean = nn.Linear(in_features, out_features, bias=bias) self.w_var = nn.Linear(in_features, out_features, bias=bias) self.gamma = gamma def reset_parameters(self): self.w_mean.reset_parameters() self.w_var.reset_parameters() def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features})' ) def forward(self, input_0, input_1, input_2, input_3): primals_1 = self.w_mean.weight primals_2 = self.w_var.weight primals_3 = input_0 primals_4 = input_1 primals_5 = input_2 primals_6 = input_3 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6]) return output[0], output[1]
EdisonLeeeee/Graphgallery
GaussionConvD
false
5,105
[ "MIT" ]
1
8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
https://github.com/EdisonLeeeee/Graphgallery/tree/8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
PositionwiseFeedForward
import torch import torch.nn as nn import torch.nn.functional as F class PositionwiseFeedForward(nn.Module): def __init__(self, d_in, d_hid, dropout=0.1): super().__init__() self.w1 = nn.Linear(d_in, d_hid) self.w2 = nn.Linear(d_hid, d_in) self.layer_norm = nn.LayerNorm(d_in, eps=1e-06) self.dropout = nn.Dropout(dropout) def forward(self, x): residual = x x = self.layer_norm(x) x = self.w2(F.relu(self.w1(x))) x = self.dropout(x) x += residual return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'d_in': 4, 'd_hid': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_native_layer_norm_0(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tmp9 = tmp0 - tmp8 tmp10 = tmp9 * tmp9 tmp11 = tmp1 - tmp8 tmp12 = tmp11 * tmp11 tmp13 = tmp10 + tmp12 tmp14 = tmp3 - tmp8 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp8 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp19 / tmp7 tmp21 = 1e-06 tmp22 = tmp20 + tmp21 tmp23 = libdevice.rsqrt(tmp22) tl.store(out_ptr0 + x0, tmp8, xmask) tl.store(out_ptr1 + x0, tmp23, xmask) @triton.jit def triton_poi_fused_native_layer_norm_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_relu_threshold_backward_2(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) @triton.jit def triton_poi_fused_add_3(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x2, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf1 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) get_raw_stream(0) triton_poi_fused_native_layer_norm_0[grid(64)](primals_1, buf0, buf1, 64, XBLOCK=64, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_1[grid(256)](primals_1, buf0, buf1, primals_2, primals_3, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf0 del buf1 del primals_2 del primals_3 buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf2, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf3) buf4 = reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf3 buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_relu_threshold_backward_2[grid(256)](buf4, primals_5, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf5 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf4, (64, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), out=buf5) buf6 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf5 triton_poi_fused_add_3[grid(256)](buf6, primals_7, primals_1, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_7 return buf6, primals_1, reinterpret_tensor(buf2, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf4, (64, 4), (4, 1), 0 ), primals_6, buf7, primals_4 class PositionwiseFeedForwardNew(nn.Module): def __init__(self, d_in, d_hid, dropout=0.1): super().__init__() self.w1 = nn.Linear(d_in, d_hid) self.w2 = nn.Linear(d_hid, d_in) self.layer_norm = nn.LayerNorm(d_in, eps=1e-06) self.dropout = nn.Dropout(dropout) def forward(self, input_0): primals_4 = self.w1.weight primals_2 = self.w1.bias primals_6 = self.w2.weight primals_3 = self.w2.bias primals_5 = self.layer_norm.weight primals_7 = self.layer_norm.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
Eddie-Hwang/Co-Eye_Motion_Generation
PositionwiseFeedForward
false
5,106
[ "MIT" ]
1
8e244680115fb63bc26018cb6b53bcfbd04e9683
https://github.com/Eddie-Hwang/Co-Eye_Motion_Generation/tree/8e244680115fb63bc26018cb6b53bcfbd04e9683
MultiHeadSelfAttention
import torch import torch.nn as nn import torch.nn.functional as F class MultiHeadSelfAttention(nn.Module): def __init__(self, d_ipt: 'int', n_head: 'int', dropout_p: 'float'=0.1): super(MultiHeadSelfAttention, self).__init__() self.qkv_linear = nn.Linear(d_ipt, d_ipt * 3, True) self.n_head = n_head self.output_linear = nn.Linear(d_ipt, d_ipt, True) self.dropout = nn.Dropout(dropout_p) def forward(self, src: 'torch.FloatTensor', attn_mask: 'torch.FloatTensor' ) ->torch.FloatTensor: if attn_mask.dim() == 2: attn_mask = attn_mask.unsqueeze(0) if attn_mask.dim() == 3: attn_mask = attn_mask.unsqueeze(1) q, k, v = self.qkv_linear(src).chunk(3, dim=-1) q = q.contiguous().view(src.shape[0], src.shape[1], self.n_head, src.shape[2] // self.n_head).permute(0, 2, 1, 3) k = k.contiguous().view(src.shape[0], src.shape[1], self.n_head, src.shape[2] // self.n_head).permute(0, 2, 3, 1) v = v.contiguous().view(src.shape[0], src.shape[1], self.n_head, src.shape[2] // self.n_head).permute(0, 2, 1, 3) attn_weights = torch.matmul(q, k) attn_weights = attn_weights * float(src.shape[2] // self.n_head ) ** -0.5 attn_weights = attn_weights * attn_mask + (attn_mask - 1) * 10000.0 attn_weights = F.softmax(attn_weights, dim=-1) attn_weights = self.dropout(attn_weights) attn_output = torch.matmul(attn_weights, v) attn_output = attn_output.permute(0, 2, 1, 3).contiguous().view(src .shape) attn_output = self.output_linear(attn_output) return attn_output def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'d_ipt': 4, 'n_head': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clone_0(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 12 * x2 + 48 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask) @triton.jit def triton_poi_fused_clone_1(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (4 + y0 + 12 * x2 + 48 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + (4 + y0), ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask) @triton.jit def triton_poi_fused__softmax_add_mul_sub_2(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 4 x2 = xindex // 16 tmp0 = tl.load(in_ptr0 + 4 * x3, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + (4 * x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr0 + (1 + 4 * x3), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr1 + (1 + 4 * x0 + 16 * x2), xmask, eviction_policy='evict_last') tmp17 = tl.load(in_ptr0 + (2 + 4 * x3), xmask, eviction_policy='evict_last' ) tmp19 = tl.load(in_ptr1 + (2 + 4 * x0 + 16 * x2), xmask, eviction_policy='evict_last') tmp25 = tl.load(in_ptr0 + (3 + 4 * x3), xmask, eviction_policy='evict_last' ) tmp27 = tl.load(in_ptr1 + (3 + 4 * x0 + 16 * x2), xmask, eviction_policy='evict_last') tmp1 = 1.0 tmp2 = tmp0 * tmp1 tmp4 = tmp2 * tmp3 tmp5 = tmp3 - tmp1 tmp6 = 10000.0 tmp7 = tmp5 * tmp6 tmp8 = tmp4 + tmp7 tmp10 = tmp9 * tmp1 tmp12 = tmp10 * tmp11 tmp13 = tmp11 - tmp1 tmp14 = tmp13 * tmp6 tmp15 = tmp12 + tmp14 tmp16 = triton_helpers.maximum(tmp8, tmp15) tmp18 = tmp17 * tmp1 tmp20 = tmp18 * tmp19 tmp21 = tmp19 - tmp1 tmp22 = tmp21 * tmp6 tmp23 = tmp20 + tmp22 tmp24 = triton_helpers.maximum(tmp16, tmp23) tmp26 = tmp25 * tmp1 tmp28 = tmp26 * tmp27 tmp29 = tmp27 - tmp1 tmp30 = tmp29 * tmp6 tmp31 = tmp28 + tmp30 tmp32 = triton_helpers.maximum(tmp24, tmp31) tmp33 = tmp8 - tmp32 tmp34 = tl_math.exp(tmp33) tmp35 = tmp15 - tmp32 tmp36 = tl_math.exp(tmp35) tmp37 = tmp34 + tmp36 tmp38 = tmp23 - tmp32 tmp39 = tl_math.exp(tmp38) tmp40 = tmp37 + tmp39 tmp41 = tmp31 - tmp32 tmp42 = tl_math.exp(tmp41) tmp43 = tmp40 + tmp42 tl.store(out_ptr0 + x3, tmp32, xmask) tl.store(out_ptr1 + x3, tmp43, xmask) @triton.jit def triton_poi_fused__softmax_add_mul_sub_3(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex x3 = xindex // 64 x5 = xindex % 16 x6 = xindex // 4 tmp0 = tl.load(in_out_ptr0 + x4, xmask) tmp3 = tl.load(in_ptr0 + (x5 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr1 + x6, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr2 + x6, xmask, eviction_policy='evict_last') tmp1 = 1.0 tmp2 = tmp0 * tmp1 tmp4 = tmp2 * tmp3 tmp5 = tmp3 - tmp1 tmp6 = 10000.0 tmp7 = tmp5 * tmp6 tmp8 = tmp4 + tmp7 tmp10 = tmp8 - tmp9 tmp11 = tl_math.exp(tmp10) tmp13 = tmp11 / tmp12 tl.store(in_out_ptr0 + x4, tmp13, xmask) @triton.jit def triton_poi_fused_clone_4(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (8 + y0 + 12 * x2 + 48 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + (8 + y0), ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask) @triton.jit def triton_poi_fused_clone_5(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (12, 4), (4, 1)) assert_size_stride(primals_3, (12,), (1,)) assert_size_stride(primals_4, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_5, (4, 4), (4, 1)) assert_size_stride(primals_6, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 12), (12, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_4, (16, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 12), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clone_0[grid(16, 4)](buf0, primals_3, buf1, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4, 1, 4), (16, 4, 4, 1), torch.float32) triton_poi_fused_clone_1[grid(16, 4)](buf0, primals_3, buf2, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) buf3 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf1, (16, 4, 1), (4, 1, 0), 0), reinterpret_tensor(buf2, (16, 1, 4), (4, 0, 1), 0), out=buf3) buf4 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf5 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) triton_poi_fused__softmax_add_mul_sub_2[grid(64)](buf3, primals_1, buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf6 = reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf3 triton_poi_fused__softmax_add_mul_sub_3[grid(256)](buf6, primals_1, buf4, buf5, 256, XBLOCK=256, num_warps=4, num_stages=1) buf7 = reinterpret_tensor(buf5, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf5 triton_poi_fused_clone_4[grid(16, 4)](buf0, primals_3, buf7, 16, 4, XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1) del buf0 del primals_3 buf8 = reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 1), 0) del buf4 extern_kernels.bmm(reinterpret_tensor(buf6, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf7, (16, 4, 1), (4, 1, 0), 0), out=buf8) buf9 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) triton_poi_fused_clone_5[grid(16, 4)](buf8, buf9, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf10 = reinterpret_tensor(buf8, (16, 4), (4, 1), 0) del buf8 extern_kernels.addmm(primals_6, reinterpret_tensor(buf9, (16, 4), ( 4, 1), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf10) del primals_6 return reinterpret_tensor(buf10, (4, 4, 4), (16, 4, 1), 0 ), primals_1, reinterpret_tensor(primals_4, (16, 4), (4, 1), 0 ), buf6, reinterpret_tensor(buf9, (16, 4), (4, 1), 0 ), primals_5, reinterpret_tensor(buf7, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf1, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf2, (16, 4, 1), (4, 1, 4), 0) class MultiHeadSelfAttentionNew(nn.Module): def __init__(self, d_ipt: 'int', n_head: 'int', dropout_p: 'float'=0.1): super(MultiHeadSelfAttentionNew, self).__init__() self.qkv_linear = nn.Linear(d_ipt, d_ipt * 3, True) self.n_head = n_head self.output_linear = nn.Linear(d_ipt, d_ipt, True) self.dropout = nn.Dropout(dropout_p) def forward(self, input_0, input_1): primals_2 = self.qkv_linear.weight primals_3 = self.qkv_linear.bias primals_5 = self.output_linear.weight primals_6 = self.output_linear.bias primals_1 = input_0 primals_4 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6]) return output[0]
DunZhang/GPT2SourceCode
MultiHeadSelfAttention
false
5,107
[ "MIT" ]
1
d598dbae278c93f88469d45ec025da4cfa7d69ee
https://github.com/DunZhang/GPT2SourceCode/tree/d598dbae278c93f88469d45ec025da4cfa7d69ee
LocalState
import math import torch from torch import nn class LocalState(nn.Module): """Local state allows to have attention based only on data (no positional embedding), but while setting a constraint on the time window (e.g. decaying penalty term). Also a failed experiments with trying to provide some frequency based attention. """ def __init__(self, channels: 'int', heads: 'int'=4, nfreqs: 'int'=0, ndecay: 'int'=4): super().__init__() assert channels % heads == 0, (channels, heads) self.heads = heads self.nfreqs = nfreqs self.ndecay = ndecay self.content = nn.Conv1d(channels, channels, 1) self.query = nn.Conv1d(channels, channels, 1) self.key = nn.Conv1d(channels, channels, 1) if nfreqs: self.query_freqs = nn.Conv1d(channels, heads * nfreqs, 1) if ndecay: self.query_decay = nn.Conv1d(channels, heads * ndecay, 1) self.query_decay.weight.data *= 0.01 assert self.query_decay.bias is not None self.query_decay.bias.data[:] = -2 self.proj = nn.Conv1d(channels + heads * nfreqs, channels, 1) def forward(self, x): B, _C, T = x.shape heads = self.heads indexes = torch.arange(T, device=x.device, dtype=x.dtype) delta = indexes[:, None] - indexes[None, :] queries = self.query(x).view(B, heads, -1, T) keys = self.key(x).view(B, heads, -1, T) dots = torch.einsum('bhct,bhcs->bhts', keys, queries) dots /= keys.shape[2] ** 0.5 if self.nfreqs: periods = torch.arange(1, self.nfreqs + 1, device=x.device, dtype=x.dtype) freq_kernel = torch.cos(2 * math.pi * delta / periods.view(-1, 1, 1)) freq_q = self.query_freqs(x).view(B, heads, -1, T ) / self.nfreqs ** 0.5 dots += torch.einsum('fts,bhfs->bhts', freq_kernel, freq_q) if self.ndecay: decays = torch.arange(1, self.ndecay + 1, device=x.device, dtype=x.dtype) decay_q = self.query_decay(x).view(B, heads, -1, T) decay_q = torch.sigmoid(decay_q) / 2 decay_kernel = -decays.view(-1, 1, 1) * delta.abs( ) / self.ndecay ** 0.5 dots += torch.einsum('fts,bhfs->bhts', decay_kernel, decay_q) dots.masked_fill_(torch.eye(T, device=dots.device, dtype=torch.bool ), -100) weights = torch.softmax(dots, dim=2) content = self.content(x).view(B, heads, -1, T) result = torch.einsum('bhts,bhct->bhcs', weights, content) if self.nfreqs: time_sig = torch.einsum('bhts,fts->bhfs', weights, freq_kernel) result = torch.cat([result, time_sig], 2) result = result.reshape(B, -1, T) return x + self.proj(result) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 4 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) @triton.jit def triton_poi_fused_convolution_div_sigmoid_1(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 4 % 16 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.sigmoid(tmp2) tmp4 = 0.5 tmp5 = tmp3 * tmp4 tl.store(in_out_ptr0 + x3, tmp2, xmask) tl.store(out_ptr0 + x3, tmp5, xmask) @triton.jit def triton_poi_fused_abs_div_mul_neg_sub_2(out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 % 4 x2 = xindex // 16 x3 = xindex tmp0 = -1 * tl_math.abs(x0 + -1 * x1) + -1 * x2 * tl_math.abs(x0 + -1 * x1) tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tl.store(out_ptr0 + x3, tmp3, xmask) @triton.jit def triton_poi_fused_arange_eye_3(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 x0 = xindex % 4 x2 = xindex tmp0 = x1 tmp1 = x0 tmp2 = tmp0 == tmp1 tl.store(out_ptr0 + x2, tmp2, xmask) @triton.jit def triton_poi_fused__softmax_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 4 xnumel = 16 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel y0 = yindex x1 = xindex tmp0 = tl.load(in_ptr0 + y0, ymask, eviction_policy='evict_last').to(tl .int1) tmp1 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr2 + (y0 + 4 * x1), xmask & ymask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr3 + (x1 + 64 * y0), xmask & ymask, eviction_policy ='evict_last') tmp10 = tl.load(in_ptr0 + (4 + y0), ymask, eviction_policy='evict_last' ).to(tl.int1) tmp11 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp14 = tl.load(in_ptr3 + (16 + x1 + 64 * y0), xmask & ymask, eviction_policy='evict_last') tmp18 = tl.load(in_ptr0 + (8 + y0), ymask, eviction_policy='evict_last' ).to(tl.int1) tmp19 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp22 = tl.load(in_ptr3 + (32 + x1 + 64 * y0), xmask & ymask, eviction_policy='evict_last') tmp26 = tl.load(in_ptr0 + (12 + y0), ymask, eviction_policy='evict_last' ).to(tl.int1) tmp27 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp30 = tl.load(in_ptr3 + (48 + x1 + 64 * y0), xmask & ymask, eviction_policy='evict_last') tmp3 = tmp1 * tmp2 tmp4 = 1.0 tmp5 = tmp3 * tmp4 tmp7 = tmp5 + tmp6 tmp8 = -100.0 tmp9 = tl.where(tmp0, tmp8, tmp7) tmp12 = tmp11 * tmp2 tmp13 = tmp12 * tmp4 tmp15 = tmp13 + tmp14 tmp16 = tl.where(tmp10, tmp8, tmp15) tmp17 = triton_helpers.maximum(tmp9, tmp16) tmp20 = tmp19 * tmp2 tmp21 = tmp20 * tmp4 tmp23 = tmp21 + tmp22 tmp24 = tl.where(tmp18, tmp8, tmp23) tmp25 = triton_helpers.maximum(tmp17, tmp24) tmp28 = tmp27 * tmp2 tmp29 = tmp28 * tmp4 tmp31 = tmp29 + tmp30 tmp32 = tl.where(tmp26, tmp8, tmp31) tmp33 = triton_helpers.maximum(tmp25, tmp32) tl.store(out_ptr0 + (x1 + 16 * y0), tmp33, xmask & ymask) @triton.jit def triton_poi_fused__softmax_5(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 16 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x3 = xindex x2 = xindex // 4 y0 = yindex x1 = xindex % 4 tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last').to(tl .int1) tmp1 = tl.load(in_ptr1 + (x2 + 4 * y0), xmask & ymask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr2 + (x1 + 4 * y0), xmask & ymask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr3 + (y0 + 16 * x2 + 64 * x1), xmask & ymask) tmp10 = tl.load(in_ptr4 + (y0 + 16 * x1), xmask & ymask, eviction_policy='evict_last') tmp3 = tmp1 * tmp2 tmp4 = 1.0 tmp5 = tmp3 * tmp4 tmp7 = tmp5 + tmp6 tmp8 = -100.0 tmp9 = tl.where(tmp0, tmp8, tmp7) tmp11 = tmp9 - tmp10 tmp12 = tl_math.exp(tmp11) tl.store(out_ptr0 + (x3 + 16 * y0), tmp12, xmask & ymask) @triton.jit def triton_poi_fused__softmax_6(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 4 x2 = xindex // 16 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (4 + x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (8 + x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (12 + x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x3, tmp8, xmask) @triton.jit def triton_poi_fused_add_convolution_7(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 4 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_out_ptr0 + x3, xmask) tmp2 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp4 = tmp0 + tmp3 tl.store(in_out_ptr0 + x3, tmp4, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11) = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (16, 4, 1), (4, 1, 1)) assert_size_stride(primals_7, (16,), (1,)) assert_size_stride(primals_8, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_9, (4,), (1,)) assert_size_stride(primals_10, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_11, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 4), (16, 4, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(64)](buf1, primals_3, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_3 buf2 = extern_kernels.convolution(primals_1, primals_4, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf2, (4, 4, 4), (16, 4, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_0[grid(64)](buf3, primals_5, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_5 buf4 = extern_kernels.convolution(primals_1, primals_6, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf4, (4, 16, 4), (64, 4, 1)) buf5 = buf4 del buf4 buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_convolution_div_sigmoid_1[grid(256)](buf5, primals_7, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_7 buf6 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_abs_div_mul_neg_sub_2[grid(64)](buf6, 64, XBLOCK= 64, num_warps=1, num_stages=1) buf8 = empty_strided_cuda((4, 4, 16), (64, 16, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf6, (4, 4, 4), (1, 4, 16), 0), reinterpret_tensor(buf7, (4, 4, 16), (1, 4, 16), 0), out=buf8) buf9 = empty_strided_cuda((4, 4), (4, 1), torch.bool) triton_poi_fused_arange_eye_3[grid(16)](buf9, 16, XBLOCK=16, num_warps=1, num_stages=1) buf10 = empty_strided_cuda((4, 4, 1, 4), (4, 1, 64, 16), torch.float32) triton_poi_fused__softmax_4[grid(4, 16)](buf9, buf3, buf1, buf8, buf10, 4, 16, XBLOCK=16, YBLOCK=4, num_warps=1, num_stages=1) buf11 = buf7 del buf7 triton_poi_fused__softmax_5[grid(16, 16)](buf9, buf3, buf1, buf8, buf10, buf11, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) buf12 = reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf8 triton_poi_fused__softmax_6[grid(256)](buf11, buf12, 256, XBLOCK= 256, num_warps=4, num_stages=1) del buf11 buf13 = extern_kernels.convolution(primals_1, primals_8, stride=(1, ), padding=(0,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf13, (4, 4, 4), (16, 4, 1)) buf14 = buf13 del buf13 triton_poi_fused_convolution_0[grid(64)](buf14, primals_9, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_9 buf15 = reinterpret_tensor(buf10, (16, 4, 1), (4, 1, 1), 0) del buf10 extern_kernels.bmm(reinterpret_tensor(buf12, (16, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf14, (16, 4, 1), (4, 1, 0), 0), out=buf15) buf16 = extern_kernels.convolution(reinterpret_tensor(buf15, (4, 4, 4), (16, 4, 1), 0), primals_10, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf16, (4, 4, 4), (16, 4, 1)) buf17 = buf16 del buf16 triton_poi_fused_add_convolution_7[grid(64)](buf17, primals_1, primals_11, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_11 return (buf17, primals_1, primals_2, primals_4, primals_6, primals_8, primals_10, buf1, buf3, buf5, buf9, buf12, reinterpret_tensor(buf15, (4, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf14, (16, 1, 4), (4, 4, 1), 0), reinterpret_tensor(buf6, (4, 4, 4), (1, 16, 4), 0)) class LocalStateNew(nn.Module): """Local state allows to have attention based only on data (no positional embedding), but while setting a constraint on the time window (e.g. decaying penalty term). Also a failed experiments with trying to provide some frequency based attention. """ def __init__(self, channels: 'int', heads: 'int'=4, nfreqs: 'int'=0, ndecay: 'int'=4): super().__init__() assert channels % heads == 0, (channels, heads) self.heads = heads self.nfreqs = nfreqs self.ndecay = ndecay self.content = nn.Conv1d(channels, channels, 1) self.query = nn.Conv1d(channels, channels, 1) self.key = nn.Conv1d(channels, channels, 1) if nfreqs: self.query_freqs = nn.Conv1d(channels, heads * nfreqs, 1) if ndecay: self.query_decay = nn.Conv1d(channels, heads * ndecay, 1) self.query_decay.weight.data *= 0.01 assert self.query_decay.bias is not None self.query_decay.bias.data[:] = -2 self.proj = nn.Conv1d(channels + heads * nfreqs, channels, 1) def forward(self, input_0): primals_2 = self.content.weight primals_3 = self.content.bias primals_4 = self.query.weight primals_5 = self.query.bias primals_8 = self.key.weight primals_9 = self.key.bias primals_6 = self.query_decay.weight primals_7 = self.query_decay.bias primals_10 = self.proj.weight primals_11 = self.proj.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11]) return output[0]
DilwoarH/demucs
LocalState
false
5,108
[ "MIT" ]
1
32d21592dfa015468aa117cace52b21e7af79d71
https://github.com/DilwoarH/demucs/tree/32d21592dfa015468aa117cace52b21e7af79d71
SAGEAggregator
import torch import torch.nn as nn class SAGEAggregator(nn.Module): def __init__(self, in_features, out_features, agg_method='mean', concat =False, bias=False): super().__init__() self.in_features = in_features self.out_features = out_features self.concat = concat self.agg_method = agg_method self.aggregator = {'mean': torch.mean, 'sum': torch.sum}[agg_method] self.lin_l = nn.Linear(in_features, out_features, bias=bias) self.lin_r = nn.Linear(in_features, out_features, bias=bias) def reset_parameters(self): self.lin_l.reset_parameters() self.lin_r.reset_parameters() def forward(self, x, neigh_x): if not isinstance(x, torch.Tensor): x = torch.cat(x, dim=0) if not isinstance(neigh_x, torch.Tensor): neigh_x = torch.cat([self.aggregator(h, dim=1) for h in neigh_x ], dim=0) else: neigh_x = self.aggregator(neigh_x, dim=1) neigh_x = self.lin_r(neigh_x) x = self.lin_l(x) out = torch.cat([x, neigh_x], dim=1) if self.concat else x + neigh_x return out def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features})' ) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_mean_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 16 x1 = xindex // 16 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask) tmp1 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask) tmp3 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask) tmp5 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_add_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x2, tmp2, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mean_0[grid(64)](primals_2, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_2 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf1) del primals_3 buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf2) del primals_4 buf3 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf2 triton_poi_fused_add_1[grid(256)](buf3, buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf1 return buf3, reinterpret_tensor(buf0, (16, 4), (4, 1), 0 ), reinterpret_tensor(primals_1, (64, 4), (4, 1), 0) class SAGEAggregatorNew(nn.Module): def __init__(self, in_features, out_features, agg_method='mean', concat =False, bias=False): super().__init__() self.in_features = in_features self.out_features = out_features self.concat = concat self.agg_method = agg_method self.aggregator = {'mean': torch.mean, 'sum': torch.sum}[agg_method] self.lin_l = nn.Linear(in_features, out_features, bias=bias) self.lin_r = nn.Linear(in_features, out_features, bias=bias) def reset_parameters(self): self.lin_l.reset_parameters() self.lin_r.reset_parameters() def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features})' ) def forward(self, input_0, input_1): primals_3 = self.lin_l.weight primals_4 = self.lin_r.weight primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
EdisonLeeeee/Graphgallery
SAGEAggregator
false
5,109
[ "MIT" ]
1
8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
https://github.com/EdisonLeeeee/Graphgallery/tree/8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
TransformerNet2
import torch class TransformerNet2(torch.nn.Module): def __init__(self): super(TransformerNet2, self).__init__() self.tanh = torch.nn.Tanh() self.a = 10 def forward(self, r, p): m = -0.5 * self.tanh(self.a * (p - 2 * r)) + 0.5 * self.tanh(self.a * (p - 2 * (1 - r))) return m def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_mul_rsub_sub_tanh_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask) tmp2 = 2.0 tmp3 = tmp1 * tmp2 tmp4 = tmp0 - tmp3 tmp5 = 10.0 tmp6 = tmp4 * tmp5 tmp7 = libdevice.tanh(tmp6) tmp8 = -0.5 tmp9 = tmp7 * tmp8 tmp10 = 1.0 tmp11 = tmp10 - tmp1 tmp12 = tmp11 * tmp2 tmp13 = tmp0 - tmp12 tmp14 = tmp13 * tmp5 tmp15 = libdevice.tanh(tmp14) tmp16 = 0.5 tmp17 = tmp15 * tmp16 tmp18 = tmp9 + tmp17 tl.store(out_ptr0 + x0, tmp18, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mul_rsub_sub_tanh_0[grid(256)](arg1_1, arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf0, class TransformerNet2New(torch.nn.Module): def __init__(self): super(TransformerNet2New, self).__init__() self.tanh = torch.nn.Tanh() self.a = 10 def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
Ekko-zn/StegoAdv
TransformerNet2
false
5,110
[ "MIT" ]
1
2852dbc85d66f30efb7127695c0d75806bf4aa4c
https://github.com/Ekko-zn/StegoAdv/tree/2852dbc85d66f30efb7127695c0d75806bf4aa4c
NormedLinear
import torch import torch.nn.functional as F from torch import nn import torch.onnx class NormedLinear(nn.Linear): """Normalized Linear Layer. Args: tempeature (float, optional): Tempeature term. Default to 20. power (int, optional): Power term. Default to 1.0. eps (float, optional): The minimal value of divisor to keep numerical stability. Default to 1e-6. """ def __init__(self, *args, tempearture=20, power=1.0, eps=1e-06, **kwargs): super(NormedLinear, self).__init__(*args, **kwargs) self.tempearture = tempearture self.power = power self.eps = eps self.init_weights() def init_weights(self): nn.init.normal_(self.weight, mean=0, std=0.01) if self.bias is not None: nn.init.constant_(self.bias, 0) def forward(self, x): weight_ = self.weight / (self.weight.norm(dim=1, keepdim=True).pow( self.power) + self.eps) x_ = x / (x.norm(dim=1, keepdim=True).pow(self.power) + self.eps) x_ = x_ * self.tempearture return F.linear(x_, weight_, self.bias) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice from torch import nn import torch.onnx assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_div_linalg_vector_norm_mul_pow_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = libdevice.sqrt(tmp11) tmp13 = 1e-06 tmp14 = tmp12 + tmp13 tmp15 = tmp0 / tmp14 tmp16 = 20.0 tmp17 = tmp15 * tmp16 tl.store(out_ptr0 + x3, tmp17, xmask) @triton.jit def triton_poi_fused_add_div_linalg_vector_norm_pow_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = libdevice.sqrt(tmp11) tmp13 = 1e-06 tmp14 = tmp12 + tmp13 tmp15 = tmp0 / tmp14 tl.store(out_ptr0 + x2, tmp15, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_linalg_vector_norm_mul_pow_0[grid(256)]( primals_2, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_add_div_linalg_vector_norm_pow_1[grid(16)](primals_1, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_3, reinterpret_tensor(buf0, (64, 4), ( 4, 1), 0), reinterpret_tensor(buf1, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf2) del buf1 del primals_3 return reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), primals_1, reinterpret_tensor(buf0, (64, 4), (4, 1), 0) class NormedLinearNew(nn.Linear): """Normalized Linear Layer. Args: tempeature (float, optional): Tempeature term. Default to 20. power (int, optional): Power term. Default to 1.0. eps (float, optional): The minimal value of divisor to keep numerical stability. Default to 1e-6. """ def __init__(self, *args, tempearture=20, power=1.0, eps=1e-06, **kwargs): super(NormedLinearNew, self).__init__(*args, **kwargs) self.tempearture = tempearture self.power = power self.eps = eps self.init_weights() def init_weights(self): nn.init.normal_(self.weight, mean=0, std=0.01) if self.bias is not None: nn.init.constant_(self.bias, 0) def forward(self, input_0): primals_1 = self.weight primals_3 = self.bias primals_2 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
ENOT-AutoDL/mmdetection-enot
NormedLinear
false
5,111
[ "Apache-2.0" ]
1
f541749554436e3327bac00eee89b84f66c03551
https://github.com/ENOT-AutoDL/mmdetection-enot/tree/f541749554436e3327bac00eee89b84f66c03551
WaveletConv
import torch import torch.nn as nn class WaveletConv(nn.Module): def __init__(self, in_features, out_features, num_nodes, bias=False): super().__init__() self.in_features = in_features self.out_features = out_features self.w = nn.Linear(in_features, out_features, bias=bias) self.filter = nn.Parameter(torch.ones(num_nodes, 1)) def reset_parameters(self): self.w.reset_parameters() self.filter.data.fill_(1.0) def forward(self, x, wavelet, inverse_wavelet): h = self.w(x) h = inverse_wavelet.mm(h) h = self.filter * h out = wavelet.mm(h) return out def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features})' ) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4, 'num_nodes': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_mul_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask) tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x2, tmp2, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4, 1), (1, 1)) assert_size_stride(primals_5, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_2, reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf0, out=buf1) buf2 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_mul_0[grid(16)](primals_4, buf1, buf2, 16, XBLOCK= 16, num_warps=1, num_stages=1) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_5, buf2, out=buf3) del buf2 return buf3, primals_2, primals_4, buf1, reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0) class WaveletConvNew(nn.Module): def __init__(self, in_features, out_features, num_nodes, bias=False): super().__init__() self.in_features = in_features self.out_features = out_features self.w = nn.Linear(in_features, out_features, bias=bias) self.filter = nn.Parameter(torch.ones(num_nodes, 1)) def reset_parameters(self): self.w.reset_parameters() self.filter.data.fill_(1.0) def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features})' ) def forward(self, input_0, input_1, input_2): primals_4 = self.filter primals_1 = self.w.weight primals_2 = input_0 primals_3 = input_1 primals_5 = input_2 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
EdisonLeeeee/Graphgallery
WaveletConv
false
5,112
[ "MIT" ]
1
8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
https://github.com/EdisonLeeeee/Graphgallery/tree/8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
ASPP
import torch import torch.nn as nn import torch.nn.functional as F class ASPP(nn.Module): def __init__(self, in_channel=256, depth=256): super(ASPP, self).__init__() self.mean = nn.AdaptiveAvgPool2d((1, 1)) self.conv = nn.Conv2d(in_channel, depth, 1, 1) self.atrous_block1 = nn.Conv2d(in_channel, depth, 1, 1) self.atrous_block6 = nn.Conv2d(in_channel, depth, 3, 1, padding=6, dilation=6) self.atrous_block12 = nn.Conv2d(in_channel, depth, 3, 1, padding=12, dilation=12) self.atrous_block18 = nn.Conv2d(in_channel, depth, 3, 1, padding=18, dilation=18) self.conv_1x1_output = nn.Conv2d(depth * 5, depth, 1, 1) def forward(self, x): size = x.shape[2:] image_features = self.mean(x) image_features = self.conv(image_features) image_features = F.interpolate(image_features, size=size, mode= 'bilinear', align_corners=True) atrous_block1 = self.atrous_block1(x) atrous_block6 = self.atrous_block6(x) atrous_block12 = self.atrous_block12(x) atrous_block18 = self.atrous_block18(x) net = self.conv_1x1_output(torch.cat([image_features, atrous_block1, atrous_block6, atrous_block12, atrous_block18], dim=1)) return net def get_inputs(): return [torch.rand([4, 256, 4, 4])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_per_fused_mean_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 1024 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r2 = rindex x3 = xindex x0 = xindex % 256 x1 = xindex // 256 tmp0 = tl.load(in_ptr0 + (r2 + 16 * x3), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, 0) tmp4 = tl.sum(tmp3, 1)[:, None] tmp5 = 16.0 tmp6 = tmp4 / tmp5 tl.store(out_ptr0 + (x0 + 256 * r2 + 4096 * x1), tmp0, xmask) tl.debug_barrier() tl.store(in_out_ptr0 + x3, tmp6, xmask) @triton.jit def triton_poi_fused_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 9 yoffset = (tl.program_id(1) + tl.program_id(2) * tl.num_programs(1) ) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 256 y1 = yindex // 256 tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last' ) tl.store(out_ptr0 + (y0 + 256 * x2 + 2304 * y1), tmp0, xmask) @triton.jit def triton_poi_fused__to_copy_2(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.full([1], 0, tl.int64) tl.store(out_ptr0 + x0, tmp0, xmask) @triton.jit def triton_poi_fused__to_copy_arange_clamp_mul_sub_3(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = 0.0 tl.store(out_ptr0 + x0, tmp0, xmask) @triton.jit def triton_poi_fused__unsafe_index_add_convolution_mul_sub_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 4 % 4 x0 = xindex % 4 x5 = xindex // 16 x2 = xindex // 16 % 256 x6 = xindex tmp0 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + x0, None, eviction_policy='evict_last') tmp9 = tl.load(in_ptr2 + x5, None, eviction_policy='evict_last') tmp10 = tl.load(in_ptr3 + x2, None, eviction_policy='evict_last') tmp12 = tl.load(in_ptr4 + x0, None, eviction_policy='evict_last') tmp17 = tl.load(in_ptr5 + x0, None, eviction_policy='evict_last') tmp1 = tl.full([XBLOCK], 1, tl.int32) tmp2 = tmp0 + tmp1 tmp3 = tmp0 < 0 tl.where(tmp3, tmp2, tmp0) tmp6 = tmp5 + tmp1 tmp7 = tmp5 < 0 tl.where(tmp7, tmp6, tmp5) tmp11 = tmp9 + tmp10 tmp13 = tmp12 + tmp1 tmp14 = tmp12 < 0 tl.where(tmp14, tmp13, tmp12) tmp16 = tmp11 - tmp11 tmp18 = tmp16 * tmp17 tmp19 = tmp11 + tmp18 tl.store(out_ptr0 + x6, tmp19, None) @triton.jit def triton_poi_fused_cat_5(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, in_ptr9, in_ptr10, in_ptr11, in_ptr12, in_ptr13, in_ptr14, in_ptr15, out_ptr0, xnumel, XBLOCK: tl. constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x0 = xindex % 1280 x3 = xindex // 20480 x4 = xindex // 1280 % 16 x2 = xindex // 5120 % 4 x1 = xindex // 1280 % 4 x5 = xindex // 1280 x6 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 256, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x4 + 16 * x0 + 4096 * x3), tmp4, eviction_policy='evict_last', other=0.0) tmp6 = tl.load(in_ptr1 + x2, tmp4, eviction_policy='evict_last', other=0.0) tmp7 = tl.full([XBLOCK], 1, tl.int32) tmp8 = tmp6 + tmp7 tmp9 = tmp6 < 0 tl.where(tmp9, tmp8, tmp6) tmp11 = tl.load(in_ptr2 + x1, tmp4, eviction_policy='evict_last', other=0.0 ) tmp12 = tmp11 + tmp7 tmp13 = tmp11 < 0 tl.where(tmp13, tmp12, tmp11) tmp15 = tl.load(in_ptr3 + (256 * x3 + x0), tmp4, eviction_policy= 'evict_last', other=0.0) tmp16 = tl.load(in_ptr4 + x0, tmp4, eviction_policy='evict_last', other=0.0 ) tmp17 = tmp15 + tmp16 tmp18 = tl.load(in_ptr5 + x1, tmp4, eviction_policy='evict_last', other=0.0 ) tmp19 = tmp18 + tmp7 tmp20 = tmp18 < 0 tl.where(tmp20, tmp19, tmp18) tmp22 = tmp17 - tmp17 tmp23 = tl.load(in_ptr6 + x1, tmp4, eviction_policy='evict_last', other=0.0 ) tmp24 = tmp22 * tmp23 tmp25 = tmp17 + tmp24 tmp26 = tmp25 - tmp5 tmp27 = tl.load(in_ptr7 + x2, tmp4, eviction_policy='evict_last', other=0.0 ) tmp28 = tmp26 * tmp27 tmp29 = tmp5 + tmp28 tmp30 = tl.full(tmp29.shape, 0.0, tmp29.dtype) tmp31 = tl.where(tmp4, tmp29, tmp30) tmp32 = tmp0 >= tmp3 tmp33 = tl.full([1], 512, tl.int64) tmp34 = tmp0 < tmp33 tmp35 = tmp32 & tmp34 tmp36 = tl.load(in_ptr8 + (256 * x5 + (-256 + x0)), tmp35, eviction_policy='evict_last', other=0.0) tmp37 = tl.load(in_ptr9 + (-256 + x0), tmp35, eviction_policy= 'evict_last', other=0.0) tmp38 = tmp36 + tmp37 tmp39 = tl.full(tmp38.shape, 0.0, tmp38.dtype) tmp40 = tl.where(tmp35, tmp38, tmp39) tmp41 = tmp0 >= tmp33 tmp42 = tl.full([1], 768, tl.int64) tmp43 = tmp0 < tmp42 tmp44 = tmp41 & tmp43 tmp45 = tl.load(in_ptr10 + (256 * x5 + (-512 + x0)), tmp44, eviction_policy='evict_last', other=0.0) tmp46 = tl.load(in_ptr11 + (-512 + x0), tmp44, eviction_policy= 'evict_last', other=0.0) tmp47 = tmp45 + tmp46 tmp48 = tl.full(tmp47.shape, 0.0, tmp47.dtype) tmp49 = tl.where(tmp44, tmp47, tmp48) tmp50 = tmp0 >= tmp42 tmp51 = tl.full([1], 1024, tl.int64) tmp52 = tmp0 < tmp51 tmp53 = tmp50 & tmp52 tmp54 = tl.load(in_ptr12 + (256 * x5 + (-768 + x0)), tmp53, eviction_policy='evict_last', other=0.0) tmp55 = tl.load(in_ptr13 + (-768 + x0), tmp53, eviction_policy= 'evict_last', other=0.0) tmp56 = tmp54 + tmp55 tmp57 = tl.full(tmp56.shape, 0.0, tmp56.dtype) tmp58 = tl.where(tmp53, tmp56, tmp57) tmp59 = tmp0 >= tmp51 tl.full([1], 1280, tl.int64) tmp62 = tl.load(in_ptr14 + (256 * x5 + (-1024 + x0)), tmp59, eviction_policy='evict_last', other=0.0) tmp63 = tl.load(in_ptr15 + (-1024 + x0), tmp59, eviction_policy= 'evict_last', other=0.0) tmp64 = tmp62 + tmp63 tmp65 = tl.full(tmp64.shape, 0.0, tmp64.dtype) tmp66 = tl.where(tmp59, tmp64, tmp65) tmp67 = tl.where(tmp53, tmp58, tmp66) tmp68 = tl.where(tmp44, tmp49, tmp67) tmp69 = tl.where(tmp35, tmp40, tmp68) tmp70 = tl.where(tmp4, tmp31, tmp69) tl.store(out_ptr0 + x6, tmp70, None) @triton.jit def triton_poi_fused_convolution_6(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): xnumel = 16 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 256 y1 = yindex // 256 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 256 * x2 + 4096 * y1), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + (x2 + 16 * y3), tmp2, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13) = args args.clear() assert_size_stride(primals_1, (4, 256, 4, 4), (4096, 16, 4, 1)) assert_size_stride(primals_2, (256, 256, 1, 1), (256, 1, 1, 1)) assert_size_stride(primals_3, (256,), (1,)) assert_size_stride(primals_4, (256, 256, 1, 1), (256, 1, 1, 1)) assert_size_stride(primals_5, (256,), (1,)) assert_size_stride(primals_6, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_7, (256,), (1,)) assert_size_stride(primals_8, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_9, (256,), (1,)) assert_size_stride(primals_10, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_11, (256,), (1,)) assert_size_stride(primals_12, (256, 1280, 1, 1), (1280, 1, 1, 1)) assert_size_stride(primals_13, (256,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 256, 4, 4), (4096, 1, 1024, 256), torch.float32) buf4 = empty_strided_cuda((4, 256, 1, 1), (256, 1, 1024, 1024), torch.float32) buf5 = reinterpret_tensor(buf4, (4, 256, 1, 1), (256, 1, 256, 256), 0) del buf4 get_raw_stream(0) triton_per_fused_mean_0[grid(1024)](buf5, primals_1, buf0, 1024, 16, XBLOCK=32, num_warps=4, num_stages=1) del primals_1 buf1 = empty_strided_cuda((256, 256, 3, 3), (2304, 1, 768, 256), torch.float32) triton_poi_fused_1[grid(65536, 9)](primals_6, buf1, 65536, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_6 buf2 = empty_strided_cuda((256, 256, 3, 3), (2304, 1, 768, 256), torch.float32) triton_poi_fused_1[grid(65536, 9)](primals_8, buf2, 65536, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_8 buf3 = empty_strided_cuda((256, 256, 3, 3), (2304, 1, 768, 256), torch.float32) triton_poi_fused_1[grid(65536, 9)](primals_10, buf3, 65536, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_10 buf6 = extern_kernels.convolution(buf5, primals_2, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf6, (4, 256, 1, 1), (256, 1, 256, 256)) buf7 = empty_strided_cuda((4, 1), (1, 1), torch.int64) triton_poi_fused__to_copy_2[grid(4)](buf7, 4, XBLOCK=4, num_warps=1, num_stages=1) buf8 = empty_strided_cuda((4, 1), (1, 1), torch.int64) triton_poi_fused__to_copy_2[grid(4)](buf8, 4, XBLOCK=4, num_warps=1, num_stages=1) buf9 = empty_strided_cuda((4,), (1,), torch.int64) triton_poi_fused__to_copy_2[grid(4)](buf9, 4, XBLOCK=4, num_warps=1, num_stages=1) buf10 = empty_strided_cuda((4,), (1,), torch.int64) triton_poi_fused__to_copy_2[grid(4)](buf10, 4, XBLOCK=4, num_warps= 1, num_stages=1) buf11 = empty_strided_cuda((4,), (1,), torch.float32) triton_poi_fused__to_copy_arange_clamp_mul_sub_3[grid(4)](buf11, 4, XBLOCK=4, num_warps=1, num_stages=1) buf12 = empty_strided_cuda((4, 256, 4, 4), (4096, 16, 4, 1), torch. float32) triton_poi_fused__unsafe_index_add_convolution_mul_sub_4[grid(16384)]( buf7, buf9, buf6, primals_3, buf10, buf11, buf12, 16384, XBLOCK =256, num_warps=4, num_stages=1) buf13 = empty_strided_cuda((4, 1), (1, 1), torch.float32) triton_poi_fused__to_copy_arange_clamp_mul_sub_3[grid(4)](buf13, 4, XBLOCK=4, num_warps=1, num_stages=1) buf14 = extern_kernels.convolution(buf0, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf14, (4, 256, 4, 4), (4096, 1, 1024, 256)) buf15 = extern_kernels.convolution(buf0, buf1, stride=(1, 1), padding=(6, 6), dilation=(6, 6), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf15, (4, 256, 4, 4), (4096, 1, 1024, 256)) buf16 = extern_kernels.convolution(buf0, buf2, stride=(1, 1), padding=(12, 12), dilation=(12, 12), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf16, (4, 256, 4, 4), (4096, 1, 1024, 256)) buf17 = extern_kernels.convolution(buf0, buf3, stride=(1, 1), padding=(18, 18), dilation=(18, 18), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf17, (4, 256, 4, 4), (4096, 1, 1024, 256)) buf18 = empty_strided_cuda((4, 1280, 4, 4), (20480, 1, 5120, 1280), torch.float32) triton_poi_fused_cat_5[grid(81920)](buf12, buf8, buf9, buf6, primals_3, buf10, buf11, buf13, buf14, primals_5, buf15, primals_7, buf16, primals_9, buf17, primals_11, buf18, 81920, XBLOCK=512, num_warps=8, num_stages=1) del buf12 del buf14 del buf15 del buf16 del buf6 del primals_11 del primals_3 del primals_5 del primals_7 del primals_9 buf19 = extern_kernels.convolution(buf18, primals_12, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf19, (4, 256, 4, 4), (4096, 1, 1024, 256)) buf20 = reinterpret_tensor(buf17, (4, 256, 4, 4), (4096, 16, 4, 1), 0) del buf17 triton_poi_fused_convolution_6[grid(1024, 16)](buf19, primals_13, buf20, 1024, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) del buf19 del primals_13 return (buf20, buf0, primals_2, primals_4, buf1, buf2, buf3, primals_12, buf5, buf7, buf8, buf9, buf10, buf11, buf13, buf18) class ASPPNew(nn.Module): def __init__(self, in_channel=256, depth=256): super(ASPPNew, self).__init__() self.mean = nn.AdaptiveAvgPool2d((1, 1)) self.conv = nn.Conv2d(in_channel, depth, 1, 1) self.atrous_block1 = nn.Conv2d(in_channel, depth, 1, 1) self.atrous_block6 = nn.Conv2d(in_channel, depth, 3, 1, padding=6, dilation=6) self.atrous_block12 = nn.Conv2d(in_channel, depth, 3, 1, padding=12, dilation=12) self.atrous_block18 = nn.Conv2d(in_channel, depth, 3, 1, padding=18, dilation=18) self.conv_1x1_output = nn.Conv2d(depth * 5, depth, 1, 1) def forward(self, input_0): primals_2 = self.conv.weight primals_3 = self.conv.bias primals_4 = self.atrous_block1.weight primals_5 = self.atrous_block1.bias primals_6 = self.atrous_block6.weight primals_7 = self.atrous_block6.bias primals_8 = self.atrous_block12.weight primals_9 = self.atrous_block12.bias primals_10 = self.atrous_block18.weight primals_11 = self.atrous_block18.bias primals_12 = self.conv_1x1_output.weight primals_13 = self.conv_1x1_output.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13]) return output[0]
DoggyLiu0116/MamboNet
ASPP
false
5,113
[ "MIT" ]
1
3b708091422491f660c4bd5eb12b06ce3b8a5f79
https://github.com/DoggyLiu0116/MamboNet/tree/3b708091422491f660c4bd5eb12b06ce3b8a5f79
TAGConv
import torch import torch.nn as nn class TAGConv(nn.Module): def __init__(self, in_features, out_features, K=3, bias=True): super().__init__() self.in_features = in_features self.out_features = out_features self.K = K self.w = nn.Linear(in_features * (self.K + 1), out_features, bias=bias) def reset_parameters(self): self.w.reset_parameters() def forward(self, x, adj): out = x xs = [x] for _ in range(self.K): out = adj.mm(out) xs.append(out) out = self.w(torch.cat(xs, dim=-1)) return out def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features}, K={self.K})' ) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tl.store(out_ptr0 + (x0 + 16 * x1), tmp0, xmask) @triton.jit def triton_poi_fused_cat_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tl.store(out_ptr0 + (x0 + 16 * x1), tmp0, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 16), (16, 1)) assert_size_stride(primals_4, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_2, primals_1, out=buf0) buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_2, buf0, out=buf1) buf6 = empty_strided_cuda((4, 16), (16, 1), torch.float32) buf2 = reinterpret_tensor(buf6, (4, 4), (16, 1), 12) extern_kernels.mm(primals_2, buf1, out=buf2) del primals_2 buf3 = reinterpret_tensor(buf6, (4, 4), (16, 1), 0) get_raw_stream(0) triton_poi_fused_cat_0[grid(16)](primals_1, buf3, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_1 buf4 = reinterpret_tensor(buf6, (4, 4), (16, 1), 4) triton_poi_fused_cat_1[grid(16)](buf0, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf0 buf5 = reinterpret_tensor(buf6, (4, 4), (16, 1), 8) triton_poi_fused_cat_1[grid(16)](buf1, buf5, 16, XBLOCK=16, num_warps=1, num_stages=1) buf7 = buf1 del buf1 extern_kernels.addmm(primals_4, buf6, reinterpret_tensor(primals_3, (16, 4), (1, 16), 0), alpha=1, beta=1, out=buf7) del primals_3 del primals_4 return buf7, buf6 class TAGConvNew(nn.Module): def __init__(self, in_features, out_features, K=3, bias=True): super().__init__() self.in_features = in_features self.out_features = out_features self.K = K self.w = nn.Linear(in_features * (self.K + 1), out_features, bias=bias) def reset_parameters(self): self.w.reset_parameters() def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features}, K={self.K})' ) def forward(self, input_0, input_1): primals_3 = self.w.weight primals_4 = self.w.bias primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
EdisonLeeeee/Graphgallery
TAGConv
false
5,114
[ "MIT" ]
1
8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
https://github.com/EdisonLeeeee/Graphgallery/tree/8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
DAGNNConv
import torch import torch.nn as nn class DAGNNConv(nn.Module): def __init__(self, in_features, out_features=1, K=10, bias=False): super().__init__() assert out_features == 1, "'out_features' must be 1" self.in_features = in_features self.out_features = out_features self.lin = nn.Linear(in_features, out_features, bias=bias) self.K = K self.act = nn.Sigmoid() def reset_parameters(self): self.lin.reset_parameters() def forward(self, x, adj): propagations = [x] for _ in range(self.K): x = adj.mm(x) propagations.append(x) h = torch.stack(propagations, dim=1) retain_score = self.act(self.lin(h)).permute(0, 2, 1).contiguous() out = (retain_score @ h).squeeze(1) return out def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features}, K={self.K})' ) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'in_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_stack_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tl.store(out_ptr0 + (x0 + 44 * x1), tmp0, xmask) @triton.jit def triton_poi_fused_stack_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tl.store(out_ptr0 + (x0 + 44 * x1), tmp0, xmask) @triton.jit def triton_poi_fused_sigmoid_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 44 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.sigmoid(tmp0) tl.store(out_ptr0 + x0, tmp1, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (1, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, primals_2, out=buf0) buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, buf0, out=buf1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, buf1, out=buf2) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, buf2, out=buf3) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, buf3, out=buf4) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, buf4, out=buf5) buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, buf5, out=buf6) buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, buf6, out=buf7) buf8 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, buf7, out=buf8) buf20 = empty_strided_cuda((4, 44), (44, 1), torch.float32) buf9 = reinterpret_tensor(buf20, (4, 4), (44, 1), 40) extern_kernels.mm(primals_1, buf8, out=buf9) del primals_1 buf10 = reinterpret_tensor(buf20, (4, 4), (44, 1), 0) get_raw_stream(0) triton_poi_fused_stack_0[grid(16)](primals_2, buf10, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_2 buf11 = reinterpret_tensor(buf20, (4, 4), (44, 1), 4) triton_poi_fused_stack_1[grid(16)](buf0, buf11, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf0 buf12 = reinterpret_tensor(buf20, (4, 4), (44, 1), 8) triton_poi_fused_stack_1[grid(16)](buf1, buf12, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf1 buf13 = reinterpret_tensor(buf20, (4, 4), (44, 1), 12) triton_poi_fused_stack_1[grid(16)](buf2, buf13, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf2 buf14 = reinterpret_tensor(buf20, (4, 4), (44, 1), 16) triton_poi_fused_stack_0[grid(16)](buf3, buf14, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf3 buf15 = reinterpret_tensor(buf20, (4, 4), (44, 1), 20) triton_poi_fused_stack_1[grid(16)](buf4, buf15, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf4 buf16 = reinterpret_tensor(buf20, (4, 4), (44, 1), 24) triton_poi_fused_stack_1[grid(16)](buf5, buf16, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf5 buf17 = reinterpret_tensor(buf20, (4, 4), (44, 1), 28) triton_poi_fused_stack_1[grid(16)](buf6, buf17, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf6 buf18 = reinterpret_tensor(buf20, (4, 4), (44, 1), 32) triton_poi_fused_stack_0[grid(16)](buf7, buf18, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf7 buf19 = reinterpret_tensor(buf20, (4, 4), (44, 1), 36) triton_poi_fused_stack_1[grid(16)](buf8, buf19, 16, XBLOCK=16, num_warps=1, num_stages=1) buf21 = empty_strided_cuda((44, 1), (1, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf20, (44, 4), (4, 1), 0), reinterpret_tensor(primals_3, (4, 1), (1, 4), 0), out=buf21) del primals_3 buf22 = empty_strided_cuda((4, 11, 1), (11, 1, 1), torch.float32) triton_poi_fused_sigmoid_2[grid(44)](buf21, buf22, 44, XBLOCK=64, num_warps=1, num_stages=1) buf23 = reinterpret_tensor(buf8, (4, 1, 4), (4, 4, 1), 0) del buf8 extern_kernels.bmm(reinterpret_tensor(buf22, (4, 1, 11), (11, 0, 1), 0), reinterpret_tensor(buf20, (4, 11, 4), (44, 4, 1), 0), out=buf23 ) del buf22 return reinterpret_tensor(buf23, (4, 4), (4, 1), 0), buf20, buf21 class DAGNNConvNew(nn.Module): def __init__(self, in_features, out_features=1, K=10, bias=False): super().__init__() assert out_features == 1, "'out_features' must be 1" self.in_features = in_features self.out_features = out_features self.lin = nn.Linear(in_features, out_features, bias=bias) self.K = K self.act = nn.Sigmoid() def reset_parameters(self): self.lin.reset_parameters() def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features}, K={self.K})' ) def forward(self, input_0, input_1): primals_3 = self.lin.weight primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3]) return output[0]
EdisonLeeeee/Graphgallery
DAGNNConv
false
5,115
[ "MIT" ]
1
8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
https://github.com/EdisonLeeeee/Graphgallery/tree/8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
Attn
import math import torch import torch.nn as nn import torch.nn.functional as F class Attn(nn.Module): def __init__(self, hidden): super().__init__() self.hidden = hidden self.attn = nn.Linear(self.hidden * 2, hidden) self.v = nn.Parameter(torch.rand(hidden)) stdv = 1.0 / math.sqrt(self.v.size(0)) self.v.data.normal_(mean=0, std=stdv) def forward(self, hidden, enc_out): l = enc_out.size(0) enc_out.size(1) H = hidden.repeat(l, 1, 1).transpose(0, 1) enc_out = enc_out.transpose(0, 1) attn_score = self.score(H, enc_out) return F.softmax(attn_score, dim=1).unsqueeze(1) def score(self, hidden, enc_out): """ concat score function score(s_t, h_i) = vT_a tanh(Wa[s_t; h_i]) """ energy = torch.tanh(self.attn(torch.cat([hidden, enc_out], 2))) energy = energy.transpose(2, 1) v = self.v.repeat(enc_out.data.shape[0], 1).unsqueeze(1) energy = torch.bmm(v, energy) return energy.squeeze(1) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'hidden': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 8 x2 = xindex // 32 x1 = xindex // 8 % 4 x3 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (4 * x2 + x0), tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 8, tl.int64) tmp9 = tl.load(in_ptr1 + (4 * x2 + 16 * x1 + (-4 + x0)), tmp6 & xmask, eviction_policy='evict_last', other=0.0) tmp10 = tl.where(tmp4, tmp5, tmp9) tl.store(out_ptr0 + x3, tmp10, xmask) @triton.jit def triton_poi_fused_tanh_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = libdevice.tanh(tmp2) tl.store(in_out_ptr0 + x2, tmp3, xmask) @triton.jit def triton_poi_fused_repeat_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x2 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x2, tmp0, xmask) @triton.jit def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 8), (8, 1)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 8), (32, 8, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(128)](primals_2, primals_1, buf0, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 del primals_2 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (16, 8), (8, 1), 0), reinterpret_tensor(primals_3, (8, 4), (1, 8), 0), out=buf1) del primals_3 buf2 = reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 0) del buf1 triton_poi_fused_tanh_1[grid(64)](buf2, primals_4, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_4 buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_repeat_2[grid(16)](primals_5, buf3, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_5 buf4 = empty_strided_cuda((4, 1, 4), (4, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf3, (4, 1, 4), (4, 0, 1), 0 ), reinterpret_tensor(buf2, (4, 4, 4), (16, 1, 4), 0), out=buf4) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused__softmax_3[grid(16)](buf4, buf5, 16, XBLOCK=16, num_warps=1, num_stages=1) buf6 = reinterpret_tensor(buf4, (4, 4), (4, 1), 0) del buf4 triton_poi_fused__softmax_4[grid(16)](buf5, buf6, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf5 return reinterpret_tensor(buf6, (4, 1, 4), (4, 4, 1), 0 ), reinterpret_tensor(buf0, (16, 8), (8, 1), 0 ), buf2, buf6, reinterpret_tensor(buf3, (4, 4, 1), (4, 1, 4), 0) class AttnNew(nn.Module): def __init__(self, hidden): super().__init__() self.hidden = hidden self.attn = nn.Linear(self.hidden * 2, hidden) self.v = nn.Parameter(torch.rand(hidden)) stdv = 1.0 / math.sqrt(self.v.size(0)) self.v.data.normal_(mean=0, std=stdv) def score(self, hidden, enc_out): """ concat score function score(s_t, h_i) = vT_a tanh(Wa[s_t; h_i]) """ energy = torch.tanh(self.attn(torch.cat([hidden, enc_out], 2))) energy = energy.transpose(2, 1) v = self.v.repeat(enc_out.data.shape[0], 1).unsqueeze(1) energy = torch.bmm(v, energy) return energy.squeeze(1) def forward(self, input_0, input_1): primals_4 = self.v primals_3 = self.attn.weight primals_5 = self.attn.bias primals_2 = input_0 primals_1 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
Eddie-Hwang/Co-Eye_Motion_Generation
Attn
false
5,116
[ "MIT" ]
1
8e244680115fb63bc26018cb6b53bcfbd04e9683
https://github.com/Eddie-Hwang/Co-Eye_Motion_Generation/tree/8e244680115fb63bc26018cb6b53bcfbd04e9683
SpectralEigenConv
import torch import torch.nn as nn class SpectralEigenConv(nn.Module): def __init__(self, in_features, out_features, bias=False, K=10, alpha= 0.1, **kwargs): super().__init__() assert K > 0 self.K = K self.alpha = alpha self.in_features = in_features self.out_features = out_features self.w = nn.Linear(in_features, out_features, bias=bias) def forward(self, x, U, V=None): """ x: node attribute matrix if `V=None`: U: (N, N) adjacency matrix else: U: (N, k) eigenvector matrix V: (k,) eigenvalue """ x = self.w(x) if V is not None: V_pow = torch.ones_like(V) V_out = torch.zeros_like(V) for _ in range(self.K): V_pow *= V V_out += (1 - self.alpha) * V_pow V_out = V_out / self.K x_out = U * V_out @ (U.t() @ x) + self.alpha * x else: adj = U x_in = x x_out = torch.zeros_like(x) for _ in range(self.K): x = torch.spmm(adj, x) x_out += (1 - self.alpha) * x x_out /= self.K x_out += self.alpha * x_in return x_out def reset_parameters(self): self.w.reset_parameters() def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features}, K={self.K}, alpha={self.alpha})' ) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_div_mul_0(in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp3 = tl.load(in_ptr0 + x0, xmask) tmp6 = tl.load(in_ptr1 + x0, xmask) tmp9 = tl.load(in_ptr2 + x0, xmask) tmp12 = tl.load(in_ptr3 + x0, xmask) tmp15 = tl.load(in_ptr4 + x0, xmask) tmp18 = tl.load(in_ptr5 + x0, xmask) tmp21 = tl.load(in_ptr6 + x0, xmask) tmp24 = tl.load(in_ptr7 + x0, xmask) tmp27 = tl.load(in_ptr8 + x0, xmask) tmp32 = tl.load(in_out_ptr1 + x0, xmask) tmp1 = 0.9 tmp2 = tmp0 * tmp1 tmp4 = tmp3 * tmp1 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp1 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp1 tmp11 = tmp8 + tmp10 tmp13 = tmp12 * tmp1 tmp14 = tmp11 + tmp13 tmp16 = tmp15 * tmp1 tmp17 = tmp14 + tmp16 tmp19 = tmp18 * tmp1 tmp20 = tmp17 + tmp19 tmp22 = tmp21 * tmp1 tmp23 = tmp20 + tmp22 tmp25 = tmp24 * tmp1 tmp26 = tmp23 + tmp25 tmp28 = tmp27 * tmp1 tmp29 = tmp26 + tmp28 tmp30 = 0.1 tmp31 = tmp29 * tmp30 tmp33 = tmp32 * tmp30 tmp34 = tmp31 + tmp33 tl.store(in_out_ptr1 + x0, tmp34, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_2, reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf0, out=buf1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf1, out=buf2) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf2, out=buf3) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf3, out=buf4) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf4, out=buf5) buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf5, out=buf6) buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf6, out=buf7) buf8 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf7, out=buf8) buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf8, out=buf9) buf11 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf9, out=buf11) buf10 = buf1 del buf1 buf12 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_add_div_mul_0[grid(16)](buf10, buf12, buf2, buf3, buf4, buf5, buf6, buf7, buf8, buf9, buf11, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf10 del buf11 del buf2 del buf3 del buf4 del buf5 del buf6 del buf7 del buf8 del buf9 return buf12, primals_2, reinterpret_tensor(primals_3, (4, 4), (1, 4), 0) class SpectralEigenConvNew(nn.Module): def __init__(self, in_features, out_features, bias=False, K=10, alpha= 0.1, **kwargs): super().__init__() assert K > 0 self.K = K self.alpha = alpha self.in_features = in_features self.out_features = out_features self.w = nn.Linear(in_features, out_features, bias=bias) def reset_parameters(self): self.w.reset_parameters() def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features}, K={self.K}, alpha={self.alpha})' ) def forward(self, input_0, input_1): primals_1 = self.w.weight primals_2 = input_0 primals_3 = input_1 output = call([primals_1, primals_2, primals_3]) return output[0]
EdisonLeeeee/Graphgallery
SpectralEigenConv
false
5,117
[ "MIT" ]
1
8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
https://github.com/EdisonLeeeee/Graphgallery/tree/8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
CO2Regularizer
import torch class MemoryBankModule(torch.nn.Module): """Memory bank implementation This is a parent class to all loss functions implemented by the lightly Python package. This way, any loss can be used with a memory bank if desired. Attributes: size: Number of keys the memory bank can store. If set to 0, memory bank is not used. Examples: >>> class MyLossFunction(MemoryBankModule): >>> >>> def __init__(self, memory_bank_size: int = 2 ** 16): >>> super(MyLossFunction, self).__init__(memory_bank_size) >>> >>> def forward(self, output: torch.Tensor, >>> labels: torch.Tensor = None): >>> >>> output, negatives = super( >>> MyLossFunction, self).forward(output) >>> >>> if negatives is not None: >>> # evaluate loss with negative samples >>> else: >>> # evaluate loss without negative samples """ def __init__(self, size: 'int'=2 ** 16): super(MemoryBankModule, self).__init__() if size < 0: msg = f'Illegal memory bank size {size}, must be non-negative.' raise ValueError(msg) self.size = size self.bank = None self.bank_ptr = None @torch.no_grad() def _init_memory_bank(self, dim: 'int'): """Initialize the memory bank if it's empty Args: dim: The dimension of the which are stored in the bank. """ self.bank = torch.randn(dim, self.size) self.bank = torch.nn.functional.normalize(self.bank, dim=0) self.bank_ptr = torch.LongTensor([0]) @torch.no_grad() def _dequeue_and_enqueue(self, batch: 'torch.Tensor'): """Dequeue the oldest batch and add the latest one Args: batch: The latest batch of keys to add to the memory bank. """ batch_size = batch.shape[0] ptr = int(self.bank_ptr) if ptr + batch_size >= self.size: self.bank[:, ptr:] = batch[:self.size - ptr].T.detach() self.bank_ptr[0] = 0 else: self.bank[:, ptr:ptr + batch_size] = batch.T.detach() self.bank_ptr[0] = ptr + batch_size def forward(self, output: 'torch.Tensor', labels: 'torch.Tensor'=None, update: 'bool'=False): """Query memory bank for additional negative samples Args: output: The output of the model. labels: Should always be None, will be ignored. Returns: The output if the memory bank is of size 0, otherwise the output and the entries from the memory bank. """ if self.size == 0: return output, None _, dim = output.shape if self.bank is None: self._init_memory_bank(dim) bank = self.bank.clone().detach() if update: self._dequeue_and_enqueue(output) return output, bank class CO2Regularizer(MemoryBankModule): """Implementation of the CO2 regularizer [0] for self-supervised learning. [0] CO2, 2021, https://arxiv.org/abs/2010.02217 Attributes: alpha: Weight of the regularization term. t_consistency: Temperature used during softmax calculations. memory_bank_size: Number of negative samples to store in the memory bank. Use 0 to use the second batch for negative samples. Examples: >>> # initialize loss function for MoCo >>> loss_fn = NTXentLoss(memory_bank_size=4096) >>> >>> # initialize CO2 regularizer >>> co2 = CO2Regularizer(alpha=1.0, memory_bank_size=4096) >>> >>> # generate two random trasnforms of images >>> t0 = transforms(images) >>> t1 = transforms(images) >>> >>> # feed through the MoCo model >>> out0, out1 = model(t0, t1) >>> >>> # calculate loss and apply regularizer >>> loss = loss_fn(out0, out1) + co2(out0, out1) """ def __init__(self, alpha: 'float'=1, t_consistency: 'float'=0.05, memory_bank_size: 'int'=0): super(CO2Regularizer, self).__init__(size=memory_bank_size) self.log_target = True try: self.kl_div = torch.nn.KLDivLoss(reduction='batchmean', log_target=True) except TypeError: self.log_target = False self.kl_div = torch.nn.KLDivLoss(reduction='batchmean') self.t_consistency = t_consistency self.alpha = alpha def _get_pseudo_labels(self, out0: 'torch.Tensor', out1: 'torch.Tensor', negatives: 'torch.Tensor'=None): """Computes the soft pseudo labels across negative samples. Args: out0: Output projections of the first set of transformed images (query). Shape: bsz x n_ftrs out1: Output projections of the second set of transformed images (positive sample). Shape: bsz x n_ftrs negatives: Negative samples to compare against. If this is None, the second batch of images will be used as negative samples. Shape: memory_bank_size x n_ftrs Returns: Log probability that a positive samples will classify each negative sample as the positive sample. Shape: bsz x (bsz - 1) or bsz x memory_bank_size """ batch_size, _ = out0.shape if negatives is None: l_pos = torch.einsum('nc,nc->n', [out0, out1]).unsqueeze(-1) l_neg = torch.einsum('nc,ck->nk', [out0, out1.t()]) l_neg = l_neg.masked_select(~torch.eye(batch_size, dtype=bool, device=l_neg.device)).view(batch_size, batch_size - 1) else: negatives = negatives l_pos = torch.einsum('nc,nc->n', [out0, out1]).unsqueeze(-1) l_neg = torch.einsum('nc,ck->nk', [out0, negatives.clone(). detach()]) logits = torch.cat([l_pos, l_neg], dim=1) logits = logits / self.t_consistency return torch.nn.functional.log_softmax(logits, dim=-1) def forward(self, out0: 'torch.Tensor', out1: 'torch.Tensor'): """Computes the CO2 regularization term for two model outputs. Args: out0: Output projections of the first set of transformed images. out1: Output projections of the second set of transformed images. Returns: The regularization term multiplied by the weight factor alpha. """ out0 = torch.nn.functional.normalize(out0, dim=1) out1 = torch.nn.functional.normalize(out1, dim=1) out1, negatives = super(CO2Regularizer, self).forward(out1, update=True ) p = self._get_pseudo_labels(out0, out1, negatives) q = self._get_pseudo_labels(out1, out0, negatives) if self.log_target: div = self.kl_div(p, q) + self.kl_div(q, p) else: div = self.kl_div(p, torch.exp(q)) + self.kl_div(q, torch.exp(p)) return self.alpha * 0.5 * div def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_div_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = libdevice.sqrt(tmp11) tmp13 = 1e-12 tmp14 = triton_helpers.maximum(tmp12, tmp13) tmp15 = tmp0 / tmp14 tl.store(out_ptr0 + x2, tmp15, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4), (4, 1)) assert_size_stride(arg1_1, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_div_0[grid(16)](arg0_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_div_0[grid(16)](arg1_1, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg1_1 return buf0, buf1 class MemoryBankModule(torch.nn.Module): """Memory bank implementation This is a parent class to all loss functions implemented by the lightly Python package. This way, any loss can be used with a memory bank if desired. Attributes: size: Number of keys the memory bank can store. If set to 0, memory bank is not used. Examples: >>> class MyLossFunction(MemoryBankModule): >>> >>> def __init__(self, memory_bank_size: int = 2 ** 16): >>> super(MyLossFunction, self).__init__(memory_bank_size) >>> >>> def forward(self, output: torch.Tensor, >>> labels: torch.Tensor = None): >>> >>> output, negatives = super( >>> MyLossFunction, self).forward(output) >>> >>> if negatives is not None: >>> # evaluate loss with negative samples >>> else: >>> # evaluate loss without negative samples """ def __init__(self, size: 'int'=2 ** 16): super(MemoryBankModule, self).__init__() if size < 0: msg = f'Illegal memory bank size {size}, must be non-negative.' raise ValueError(msg) self.size = size self.bank = None self.bank_ptr = None @torch.no_grad() def _init_memory_bank(self, dim: 'int'): """Initialize the memory bank if it's empty Args: dim: The dimension of the which are stored in the bank. """ self.bank = torch.randn(dim, self.size) self.bank = torch.nn.functional.normalize(self.bank, dim=0) self.bank_ptr = torch.LongTensor([0]) @torch.no_grad() def _dequeue_and_enqueue(self, batch: 'torch.Tensor'): """Dequeue the oldest batch and add the latest one Args: batch: The latest batch of keys to add to the memory bank. """ batch_size = batch.shape[0] ptr = int(self.bank_ptr) if ptr + batch_size >= self.size: self.bank[:, ptr:] = batch[:self.size - ptr].T.detach() self.bank_ptr[0] = 0 else: self.bank[:, ptr:ptr + batch_size] = batch.T.detach() self.bank_ptr[0] = ptr + batch_size def forward(self, output: 'torch.Tensor', labels: 'torch.Tensor'=None, update: 'bool'=False): """Query memory bank for additional negative samples Args: output: The output of the model. labels: Should always be None, will be ignored. Returns: The output if the memory bank is of size 0, otherwise the output and the entries from the memory bank. """ if self.size == 0: return output, None _, dim = output.shape if self.bank is None: self._init_memory_bank(dim) bank = self.bank.clone().detach() if update: self._dequeue_and_enqueue(output) return output, bank class CO2RegularizerNew(MemoryBankModule): """Implementation of the CO2 regularizer [0] for self-supervised learning. [0] CO2, 2021, https://arxiv.org/abs/2010.02217 Attributes: alpha: Weight of the regularization term. t_consistency: Temperature used during softmax calculations. memory_bank_size: Number of negative samples to store in the memory bank. Use 0 to use the second batch for negative samples. Examples: >>> # initialize loss function for MoCo >>> loss_fn = NTXentLoss(memory_bank_size=4096) >>> >>> # initialize CO2 regularizer >>> co2 = CO2Regularizer(alpha=1.0, memory_bank_size=4096) >>> >>> # generate two random trasnforms of images >>> t0 = transforms(images) >>> t1 = transforms(images) >>> >>> # feed through the MoCo model >>> out0, out1 = model(t0, t1) >>> >>> # calculate loss and apply regularizer >>> loss = loss_fn(out0, out1) + co2(out0, out1) """ def __init__(self, alpha: 'float'=1, t_consistency: 'float'=0.05, memory_bank_size: 'int'=0): super(CO2RegularizerNew, self).__init__(size=memory_bank_size) self.log_target = True try: self.kl_div = torch.nn.KLDivLoss(reduction='batchmean', log_target=True) except TypeError: self.log_target = False self.kl_div = torch.nn.KLDivLoss(reduction='batchmean') self.t_consistency = t_consistency self.alpha = alpha def _get_pseudo_labels(self, out0: 'torch.Tensor', out1: 'torch.Tensor', negatives: 'torch.Tensor'=None): """Computes the soft pseudo labels across negative samples. Args: out0: Output projections of the first set of transformed images (query). Shape: bsz x n_ftrs out1: Output projections of the second set of transformed images (positive sample). Shape: bsz x n_ftrs negatives: Negative samples to compare against. If this is None, the second batch of images will be used as negative samples. Shape: memory_bank_size x n_ftrs Returns: Log probability that a positive samples will classify each negative sample as the positive sample. Shape: bsz x (bsz - 1) or bsz x memory_bank_size """ batch_size, _ = out0.shape if negatives is None: l_pos = torch.einsum('nc,nc->n', [out0, out1]).unsqueeze(-1) l_neg = torch.einsum('nc,ck->nk', [out0, out1.t()]) l_neg = l_neg.masked_select(~torch.eye(batch_size, dtype=bool, device=l_neg.device)).view(batch_size, batch_size - 1) else: negatives = negatives l_pos = torch.einsum('nc,nc->n', [out0, out1]).unsqueeze(-1) l_neg = torch.einsum('nc,ck->nk', [out0, negatives.clone(). detach()]) logits = torch.cat([l_pos, l_neg], dim=1) logits = logits / self.t_consistency return torch.nn.functional.log_softmax(logits, dim=-1) def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
EelcoHoogendoorn/lightly
CO2Regularizer
false
5,118
[ "MIT" ]
1
98e0148967738404fa7f45196ec5eabfe00cd22e
https://github.com/EelcoHoogendoorn/lightly/tree/98e0148967738404fa7f45196ec5eabfe00cd22e
SmallTransformerNet
import torch import torch.nn.parallel import torch.optim import torch.utils.data import torch.onnx class ConvLayer(torch.nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride): super(ConvLayer, self).__init__() reflection_padding = kernel_size // 2 self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding) self.conv2d = torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride) def forward(self, x): out = self.reflection_pad(x) out = self.conv2d(out) return out class ResidualBlock(torch.nn.Module): """ResidualBlock introduced in: https://arxiv.org/abs/1512.03385 recommended architecture: http://torch.ch/blog/2016/02/04/resnets.html """ def __init__(self, channels): super(ResidualBlock, self).__init__() self.conv1 = ConvLayer(channels, channels, kernel_size=3, stride=1) self.in1 = torch.nn.InstanceNorm2d(channels, affine=True) self.conv2 = ConvLayer(channels, channels, kernel_size=3, stride=1) self.in2 = torch.nn.InstanceNorm2d(channels, affine=True) self.relu = torch.nn.ReLU() def forward(self, x): residual = x out = self.relu(self.in1(self.conv1(x))) out = self.in2(self.conv2(out)) out = out + residual return out class UpsampleConvLayer(torch.nn.Module): """UpsampleConvLayer Upsamples the input and then does a convolution. This method gives better results compared to ConvTranspose2d. ref: http://distill.pub/2016/deconv-checkerboard/ """ def __init__(self, in_channels, out_channels, kernel_size, stride, upsample=None): super(UpsampleConvLayer, self).__init__() self.upsample = upsample reflection_padding = kernel_size // 2 self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding) self.conv2d = torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride) def forward(self, x): x_in = x if self.upsample: x_in = torch.nn.functional.interpolate(x_in, mode='nearest', scale_factor=self.upsample) out = self.reflection_pad(x_in) out = self.conv2d(out) return out class SmallTransformerNet(torch.nn.Module): def __init__(self): super(SmallTransformerNet, self).__init__() self.conv1 = ConvLayer(3, 32, kernel_size=9, stride=1) self.in1 = torch.nn.InstanceNorm2d(32, affine=True) self.conv2 = ConvLayer(32, 64, kernel_size=3, stride=2) self.in2 = torch.nn.InstanceNorm2d(64, affine=True) self.res6 = ResidualBlock(64) self.res7 = ResidualBlock(64) self.deconv2 = UpsampleConvLayer(64, 32, kernel_size=3, stride=1, upsample=2) self.in5 = torch.nn.InstanceNorm2d(32, affine=True) self.deconv3 = ConvLayer(32, 3, kernel_size=9, stride=1) self.relu = torch.nn.ReLU() def forward(self, X): y = self.relu(self.in1(self.conv1(X))) y = self.relu(self.in2(self.conv2(y))) y = self.res6(y) y = self.res7(y) y = self.relu(self.in5(self.deconv2(y))) y = self.deconv3(y) return y def get_inputs(): return [torch.rand([4, 3, 64, 64])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn.parallel import torch.optim import torch.utils.data import torch.onnx assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_reflection_pad2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 62208 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 72 x1 = xindex // 72 % 72 x2 = xindex // 5184 x3 = xindex tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-4 + x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-4 + x1)) + 4096 * x2), xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_red_fused__native_batch_norm_legit_convolution_1(in_out_ptr0, in_out_ptr1, in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr, RBLOCK: tl.constexpr): xnumel = 128 rnumel = 4096 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rbase = tl.arange(0, RBLOCK)[None, :] x3 = xindex x0 = xindex % 32 tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp4_mean = tl.zeros([XBLOCK, RBLOCK], tl.float32) tmp4_m2 = tl.zeros([XBLOCK, RBLOCK], tl.float32) tmp4_weight = tl.zeros([XBLOCK, RBLOCK], tl.float32) for roffset in range(0, rnumel, RBLOCK): rindex = roffset + rbase rmask = rindex < rnumel r2 = rindex tmp0 = tl.load(in_out_ptr0 + (r2 + 4096 * x3), rmask & xmask, eviction_policy='evict_first', other=0.0) tmp2 = tmp0 + tmp1 tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp4_mean_next, tmp4_m2_next, tmp4_weight_next = (triton_helpers. welford_reduce(tmp3, tmp4_mean, tmp4_m2, tmp4_weight, roffset == 0) ) tmp4_mean = tl.where(rmask & xmask, tmp4_mean_next, tmp4_mean) tmp4_m2 = tl.where(rmask & xmask, tmp4_m2_next, tmp4_m2) tmp4_weight = tl.where(rmask & xmask, tmp4_weight_next, tmp4_weight) tl.store(in_out_ptr0 + (r2 + 4096 * x3), tmp2, rmask & xmask) tmp4_tmp, tmp5_tmp, tmp6_tmp = triton_helpers.welford(tmp4_mean, tmp4_m2, tmp4_weight, 1) tmp4 = tmp4_tmp[:, None] tmp5 = tmp5_tmp[:, None] tmp6_tmp[:, None] tl.store(out_ptr0 + x3, tmp4, xmask) tmp7 = 4096.0 tmp8 = tmp5 / tmp7 tmp9 = 1e-05 tmp10 = tmp8 + tmp9 tmp11 = libdevice.rsqrt(tmp10) tl.debug_barrier() tl.store(in_out_ptr1 + x3, tmp11, xmask) @triton.jit def triton_poi_fused_repeat_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0 % 32, xmask) tl.store(out_ptr0 + x0, tmp0, xmask) @triton.jit def triton_poi_fused_reflection_pad2d_relu_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 557568 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 66 x1 = xindex // 66 % 66 x2 = xindex // 4356 x3 = xindex tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-1 + x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-1 + x1)) + 4096 * x2), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x2, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tmp9 = tl.full([1], 0, tl.int32) tmp10 = triton_helpers.maximum(tmp9, tmp8) tl.store(out_ptr0 + x3, tmp10, xmask) @triton.jit def triton_per_fused__native_batch_norm_legit_convolution_relu_repeat_4( in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, out_ptr2, out_ptr3, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) x0 = xindex r3 = rindex x1 = xindex % 64 tmp0 = tl.load(in_ptr0 + x0 % 64, None, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x0 % 64, None, eviction_policy='evict_last') tmp2 = tl.load(in_out_ptr0 + (r3 + 1024 * x0), None) tmp3 = tl.load(in_ptr2 + x1, None, eviction_policy='evict_last') tmp4 = tmp2 + tmp3 tmp5 = tl.broadcast_to(tmp4, [RBLOCK]) tmp7 = tl.broadcast_to(tmp5, [RBLOCK]) tmp9 = triton_helpers.promote_to_tensor(tl.sum(tmp7, 0)) tmp10 = tl.full([1], 1024, tl.int32) tmp11 = tmp10.to(tl.float32) tmp12 = tmp9 / tmp11 tmp13 = tmp5 - tmp12 tmp14 = tmp13 * tmp13 tmp15 = tl.broadcast_to(tmp14, [RBLOCK]) tmp17 = triton_helpers.promote_to_tensor(tl.sum(tmp15, 0)) tmp18 = 1024.0 tmp19 = tmp17 / tmp18 tmp20 = 1e-05 tmp21 = tmp19 + tmp20 tmp22 = libdevice.rsqrt(tmp21) tmp23 = tmp4 - tmp12 tmp24 = tmp23 * tmp22 tmp25 = tmp24 * tmp0 tmp26 = tmp25 + tmp1 tmp27 = tl.full([1], 0, tl.int32) tmp28 = triton_helpers.maximum(tmp27, tmp26) tl.store(out_ptr0 + x0, tmp0, None) tl.store(out_ptr1 + x0, tmp1, None) tl.store(in_out_ptr0 + (r3 + 1024 * x0), tmp4, None) tl.debug_barrier() tl.store(in_out_ptr1 + x0, tmp22, None) tl.store(out_ptr3 + (r3 + 1024 * x0), tmp28, None) tl.store(out_ptr2 + x0, tmp12, None) @triton.jit def triton_poi_fused_reflection_pad2d_5(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 295936 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 34 x1 = xindex // 34 % 34 x2 = xindex // 1156 x3 = xindex tmp0 = tl.load(in_ptr0 + (1023 + -1 * tl_math.abs(-31 + tl_math.abs(-1 + x0)) + -32 * tl_math.abs(-31 + tl_math.abs(-1 + x1)) + 1024 * x2), xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_per_fused__native_batch_norm_legit_convolution_6(in_out_ptr0, in_out_ptr1, in_ptr0, out_ptr0, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r2 = rindex x3 = xindex x0 = xindex % 64 tmp0 = tl.load(in_out_ptr0 + (r2 + 1024 * x3), None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.broadcast_to(tmp2, [RBLOCK]) tmp5 = tl.broadcast_to(tmp3, [RBLOCK]) tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0)) tmp8 = tl.full([1], 1024, tl.int32) tmp9 = tmp8.to(tl.float32) tmp10 = tmp7 / tmp9 tmp11 = tmp3 - tmp10 tmp12 = tmp11 * tmp11 tmp13 = tl.broadcast_to(tmp12, [RBLOCK]) tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0)) tmp16 = 1024.0 tmp17 = tmp15 / tmp16 tmp18 = 1e-05 tmp19 = tmp17 + tmp18 tmp20 = libdevice.rsqrt(tmp19) tl.store(in_out_ptr0 + (r2 + 1024 * x3), tmp2, None) tl.debug_barrier() tl.store(in_out_ptr1 + x3, tmp20, None) tl.store(out_ptr0 + x3, tmp10, None) @triton.jit def triton_poi_fused_repeat_7(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0 % 64, xmask) tl.store(out_ptr0 + x0, tmp0, xmask) @triton.jit def triton_poi_fused_reflection_pad2d_relu_8(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 295936 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 34 x1 = xindex // 34 % 34 x2 = xindex // 1156 x3 = xindex tmp0 = tl.load(in_ptr0 + (1023 + -1 * tl_math.abs(-31 + tl_math.abs(-1 + x0)) + -32 * tl_math.abs(-31 + tl_math.abs(-1 + x1)) + 1024 * x2), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x2, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tmp9 = tl.full([1], 0, tl.int32) tmp10 = triton_helpers.maximum(tmp9, tmp8) tl.store(out_ptr0 + x3, tmp10, xmask) @triton.jit def triton_per_fused__native_batch_norm_legit_add_convolution_repeat_9( in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, out_ptr3, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) x0 = xindex r3 = rindex x1 = xindex % 64 tmp0 = tl.load(in_ptr0 + x0 % 64, None, eviction_policy='evict_last') tmp1 = tl.load(in_out_ptr0 + (r3 + 1024 * x0), None) tmp2 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp25 = tl.load(in_ptr2 + x1, None, eviction_policy='evict_last') tmp27 = tl.load(in_out_ptr1 + (r3 + 1024 * x0), None) tmp3 = tmp1 + tmp2 tmp4 = tl.broadcast_to(tmp3, [RBLOCK]) tmp6 = tl.broadcast_to(tmp4, [RBLOCK]) tmp8 = triton_helpers.promote_to_tensor(tl.sum(tmp6, 0)) tmp9 = tl.full([1], 1024, tl.int32) tmp10 = tmp9.to(tl.float32) tmp11 = tmp8 / tmp10 tmp12 = tmp4 - tmp11 tmp13 = tmp12 * tmp12 tmp14 = tl.broadcast_to(tmp13, [RBLOCK]) tmp16 = triton_helpers.promote_to_tensor(tl.sum(tmp14, 0)) tmp17 = tmp3 - tmp11 tmp18 = 1024.0 tmp19 = tmp16 / tmp18 tmp20 = 1e-05 tmp21 = tmp19 + tmp20 tmp22 = libdevice.rsqrt(tmp21) tmp23 = tmp17 * tmp22 tmp24 = tmp23 * tmp0 tmp26 = tmp24 + tmp25 tmp28 = tmp26 + tmp27 tl.store(out_ptr0 + x0, tmp0, None) tl.store(in_out_ptr0 + (r3 + 1024 * x0), tmp3, None) tl.store(in_out_ptr1 + (r3 + 1024 * x0), tmp28, None) tl.store(out_ptr3 + x0, tmp22, None) tl.store(out_ptr1 + x0, tmp11, None) @triton.jit def triton_per_fused__native_batch_norm_legit_convolution_10(in_out_ptr0, in_ptr0, out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r2 = rindex x3 = xindex x0 = xindex % 64 tmp0 = tl.load(in_out_ptr0 + (r2 + 1024 * x3), None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.broadcast_to(tmp2, [RBLOCK]) tmp5 = tl.broadcast_to(tmp3, [RBLOCK]) tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0)) tmp8 = tl.full([1], 1024, tl.int32) tmp9 = tmp8.to(tl.float32) tmp10 = tmp7 / tmp9 tmp11 = tmp3 - tmp10 tmp12 = tmp11 * tmp11 tmp13 = tl.broadcast_to(tmp12, [RBLOCK]) tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0)) tmp16 = 1024.0 tmp17 = tmp15 / tmp16 tmp18 = 1e-05 tmp19 = tmp17 + tmp18 tmp20 = libdevice.rsqrt(tmp19) tl.store(in_out_ptr0 + (r2 + 1024 * x3), tmp2, None) tl.store(out_ptr2 + x3, tmp20, None) tl.store(out_ptr0 + x3, tmp10, None) tl.store(out_ptr1 + x3, tmp15, None) @triton.jit def triton_poi_fused_arange_11(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = x0 tl.store(out_ptr0 + x0, tmp0, xmask) @triton.jit def triton_poi_fused__to_copy_add_arange_mul_12(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp3.to(tl.int32) tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_poi_fused__unsafe_index_add_reflection_pad2d_13(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1115136 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 66 % 66 x0 = xindex % 66 x4 = xindex // 4356 x2 = xindex // 4356 % 64 x7 = xindex tmp0 = tl.load(in_ptr0 + (63 + -1 * tl_math.abs(-63 + tl_math.abs(-1 + x1))), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (63 + -1 * tl_math.abs(-63 + tl_math.abs(-1 + x0))), xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr3 + x4, xmask, eviction_policy='evict_last') tmp19 = tl.load(in_ptr4 + x4, xmask, eviction_policy='evict_last') tmp21 = tl.load(in_ptr5 + x2, xmask, eviction_policy='evict_last') tmp1 = tl.full([XBLOCK], 32, tl.int32) tmp2 = tmp0 + tmp1 tmp3 = tmp0 < 0 tmp4 = tl.where(tmp3, tmp2, tmp0) tmp6 = tmp5 + tmp1 tmp7 = tmp5 < 0 tmp8 = tl.where(tmp7, tmp6, tmp5) tmp9 = tl.load(in_ptr1 + (tmp8 + 32 * tmp4 + 1024 * x4), xmask, eviction_policy='evict_last') tmp11 = tmp9 - tmp10 tmp13 = 1024.0 tmp14 = tmp12 / tmp13 tmp15 = 1e-05 tmp16 = tmp14 + tmp15 tmp17 = libdevice.rsqrt(tmp16) tmp18 = tmp11 * tmp17 tmp20 = tmp18 * tmp19 tmp22 = tmp20 + tmp21 tmp23 = tl.load(in_ptr6 + (tmp8 + 32 * tmp4 + 1024 * x4), xmask, eviction_policy='evict_last') tmp24 = tmp22 + tmp23 tl.store(out_ptr0 + x7, tmp24, xmask) @triton.jit def triton_poi_fused_reflection_pad2d_relu_14(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x0 = xindex % 72 x1 = xindex // 72 % 72 x2 = xindex // 5184 x3 = xindex tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-4 + x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-4 + x1)) + 4096 * x2), None, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, None, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x2, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x2, None, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x2, None, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tmp9 = tl.full([1], 0, tl.int32) tmp10 = triton_helpers.maximum(tmp9, tmp8) tl.store(out_ptr0 + x3, tmp10, None) @triton.jit def triton_poi_fused_convolution_15(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 3 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, None) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16, primals_17, primals_18, primals_19, primals_20, primals_21, primals_22, primals_23, primals_24, primals_25, primals_26, primals_27, primals_28, primals_29, primals_30, primals_31) = args args.clear() assert_size_stride(primals_1, (4, 3, 64, 64), (12288, 4096, 64, 1)) assert_size_stride(primals_2, (32, 3, 9, 9), (243, 81, 9, 1)) assert_size_stride(primals_3, (32,), (1,)) assert_size_stride(primals_4, (32,), (1,)) assert_size_stride(primals_5, (32,), (1,)) assert_size_stride(primals_6, (64, 32, 3, 3), (288, 9, 3, 1)) assert_size_stride(primals_7, (64,), (1,)) assert_size_stride(primals_8, (64,), (1,)) assert_size_stride(primals_9, (64,), (1,)) assert_size_stride(primals_10, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_11, (64,), (1,)) assert_size_stride(primals_12, (64,), (1,)) assert_size_stride(primals_13, (64,), (1,)) assert_size_stride(primals_14, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_15, (64,), (1,)) assert_size_stride(primals_16, (64,), (1,)) assert_size_stride(primals_17, (64,), (1,)) assert_size_stride(primals_18, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_19, (64,), (1,)) assert_size_stride(primals_20, (64,), (1,)) assert_size_stride(primals_21, (64,), (1,)) assert_size_stride(primals_22, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_23, (64,), (1,)) assert_size_stride(primals_24, (64,), (1,)) assert_size_stride(primals_25, (64,), (1,)) assert_size_stride(primals_26, (32, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_27, (32,), (1,)) assert_size_stride(primals_28, (32,), (1,)) assert_size_stride(primals_29, (32,), (1,)) assert_size_stride(primals_30, (3, 32, 9, 9), (2592, 81, 9, 1)) assert_size_stride(primals_31, (3,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 3, 72, 72), (15552, 5184, 72, 1), torch.float32) get_raw_stream(0) triton_poi_fused_reflection_pad2d_0[grid(62208)](primals_1, buf0, 62208, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 32, 64, 64), (131072, 4096, 64, 1)) buf2 = buf1 del buf1 buf5 = empty_strided_cuda((1, 128, 1, 1), (128, 1, 1, 1), torch.float32 ) buf6 = empty_strided_cuda((1, 128, 1, 1), (128, 1, 128, 128), torch .float32) buf8 = reinterpret_tensor(buf6, (1, 128, 1, 1), (128, 1, 1, 1), 0) del buf6 triton_red_fused__native_batch_norm_legit_convolution_1[grid(128)](buf2 , buf8, primals_3, buf5, 128, 4096, XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1) del primals_3 buf3 = empty_strided_cuda((128,), (1,), torch.float32) triton_poi_fused_repeat_2[grid(128)](primals_4, buf3, 128, XBLOCK= 128, num_warps=4, num_stages=1) del primals_4 buf4 = empty_strided_cuda((128,), (1,), torch.float32) triton_poi_fused_repeat_2[grid(128)](primals_5, buf4, 128, XBLOCK= 128, num_warps=4, num_stages=1) del primals_5 buf9 = empty_strided_cuda((4, 32, 66, 66), (139392, 4356, 66, 1), torch.float32) triton_poi_fused_reflection_pad2d_relu_3[grid(557568)](buf2, buf5, buf8, buf3, buf4, buf9, 557568, XBLOCK=512, num_warps=8, num_stages=1) buf10 = extern_kernels.convolution(buf9, primals_6, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf10, (4, 64, 32, 32), (65536, 1024, 32, 1)) buf12 = empty_strided_cuda((256,), (1,), torch.float32) buf13 = empty_strided_cuda((256,), (1,), torch.float32) buf11 = buf10 del buf10 buf14 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 1, 1), torch. float32) buf15 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 256, 256), torch.float32) buf17 = reinterpret_tensor(buf15, (1, 256, 1, 1), (256, 1, 1, 1), 0) del buf15 buf18 = empty_strided_cuda((4, 64, 32, 32), (65536, 1024, 32, 1), torch.float32) triton_per_fused__native_batch_norm_legit_convolution_relu_repeat_4[ grid(256)](buf11, buf17, primals_8, primals_9, primals_7, buf12, buf13, buf14, buf18, 256, 1024, num_warps=8, num_stages=1) del primals_7 del primals_8 del primals_9 buf19 = empty_strided_cuda((4, 64, 34, 34), (73984, 1156, 34, 1), torch.float32) triton_poi_fused_reflection_pad2d_5[grid(295936)](buf18, buf19, 295936, XBLOCK=1024, num_warps=4, num_stages=1) buf20 = extern_kernels.convolution(buf19, primals_10, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf20, (4, 64, 32, 32), (65536, 1024, 32, 1)) buf21 = buf20 del buf20 buf24 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 1, 1), torch. float32) buf25 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 256, 256), torch.float32) buf27 = reinterpret_tensor(buf25, (1, 256, 1, 1), (256, 1, 1, 1), 0) del buf25 triton_per_fused__native_batch_norm_legit_convolution_6[grid(256)]( buf21, buf27, primals_11, buf24, 256, 1024, num_warps=8, num_stages=1) del primals_11 buf22 = empty_strided_cuda((256,), (1,), torch.float32) triton_poi_fused_repeat_7[grid(256)](primals_12, buf22, 256, XBLOCK =128, num_warps=4, num_stages=1) del primals_12 buf23 = empty_strided_cuda((256,), (1,), torch.float32) triton_poi_fused_repeat_7[grid(256)](primals_13, buf23, 256, XBLOCK =128, num_warps=4, num_stages=1) del primals_13 buf28 = empty_strided_cuda((4, 64, 34, 34), (73984, 1156, 34, 1), torch.float32) triton_poi_fused_reflection_pad2d_relu_8[grid(295936)](buf21, buf24, buf27, buf22, buf23, buf28, 295936, XBLOCK=1024, num_warps=4, num_stages=1) buf29 = extern_kernels.convolution(buf28, primals_14, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf29, (4, 64, 32, 32), (65536, 1024, 32, 1)) buf31 = empty_strided_cuda((256,), (1,), torch.float32) buf30 = buf29 del buf29 buf32 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 256, 256), torch.float32) buf36 = buf18 del buf18 buf35 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 256, 256), torch.float32) triton_per_fused__native_batch_norm_legit_add_convolution_repeat_9[grid (256)](buf30, buf36, primals_16, primals_15, primals_17, buf31, buf32, buf35, 256, 1024, num_warps=8, num_stages=1) del primals_15 del primals_16 del primals_17 buf37 = empty_strided_cuda((4, 64, 34, 34), (73984, 1156, 34, 1), torch.float32) triton_poi_fused_reflection_pad2d_5[grid(295936)](buf36, buf37, 295936, XBLOCK=1024, num_warps=4, num_stages=1) buf38 = extern_kernels.convolution(buf37, primals_18, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf38, (4, 64, 32, 32), (65536, 1024, 32, 1)) buf39 = buf38 del buf38 buf42 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 1, 1), torch. float32) buf43 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 256, 256), torch.float32) buf45 = reinterpret_tensor(buf43, (1, 256, 1, 1), (256, 1, 1, 1), 0) del buf43 triton_per_fused__native_batch_norm_legit_convolution_6[grid(256)]( buf39, buf45, primals_19, buf42, 256, 1024, num_warps=8, num_stages=1) del primals_19 buf40 = empty_strided_cuda((256,), (1,), torch.float32) triton_poi_fused_repeat_7[grid(256)](primals_20, buf40, 256, XBLOCK =128, num_warps=4, num_stages=1) del primals_20 buf41 = empty_strided_cuda((256,), (1,), torch.float32) triton_poi_fused_repeat_7[grid(256)](primals_21, buf41, 256, XBLOCK =128, num_warps=4, num_stages=1) del primals_21 buf46 = empty_strided_cuda((4, 64, 34, 34), (73984, 1156, 34, 1), torch.float32) triton_poi_fused_reflection_pad2d_relu_8[grid(295936)](buf39, buf42, buf45, buf40, buf41, buf46, 295936, XBLOCK=1024, num_warps=4, num_stages=1) buf47 = extern_kernels.convolution(buf46, primals_22, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf47, (4, 64, 32, 32), (65536, 1024, 32, 1)) buf48 = buf47 del buf47 buf50 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 256, 256), torch.float32) buf51 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 256, 256), torch.float32) buf53 = empty_strided_cuda((1, 256, 1, 1), (256, 1, 256, 256), torch.float32) triton_per_fused__native_batch_norm_legit_convolution_10[grid(256)]( buf48, primals_23, buf50, buf51, buf53, 256, 1024, num_warps=8, num_stages=1) del primals_23 buf49 = empty_strided_cuda((256,), (1,), torch.float32) triton_poi_fused_repeat_7[grid(256)](primals_24, buf49, 256, XBLOCK =128, num_warps=4, num_stages=1) del primals_24 buf54 = empty_strided_cuda((64,), (1,), torch.int64) triton_poi_fused_arange_11[grid(64)](buf54, 64, XBLOCK=64, num_warps=1, num_stages=1) buf55 = empty_strided_cuda((64,), (1,), torch.int64) triton_poi_fused__to_copy_add_arange_mul_12[grid(64)](buf55, 64, XBLOCK=64, num_warps=1, num_stages=1) buf56 = empty_strided_cuda((4, 64, 66, 66), (278784, 4356, 66, 1), torch.float32) triton_poi_fused__unsafe_index_add_reflection_pad2d_13[grid(1115136)]( buf55, buf48, buf50, buf51, buf49, primals_25, buf36, buf56, 1115136, XBLOCK=512, num_warps=8, num_stages=1) del buf36 del buf51 del primals_25 buf57 = extern_kernels.convolution(buf56, primals_26, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf57, (4, 32, 64, 64), (131072, 4096, 64, 1)) buf58 = buf57 del buf57 buf61 = empty_strided_cuda((1, 128, 1, 1), (128, 1, 1, 1), torch. float32) buf62 = empty_strided_cuda((1, 128, 1, 1), (128, 1, 128, 128), torch.float32) buf64 = reinterpret_tensor(buf62, (1, 128, 1, 1), (128, 1, 1, 1), 0) del buf62 triton_red_fused__native_batch_norm_legit_convolution_1[grid(128)]( buf58, buf64, primals_27, buf61, 128, 4096, XBLOCK=1, RBLOCK= 2048, num_warps=16, num_stages=1) del primals_27 buf59 = empty_strided_cuda((128,), (1,), torch.float32) triton_poi_fused_repeat_2[grid(128)](primals_28, buf59, 128, XBLOCK =128, num_warps=4, num_stages=1) del primals_28 buf60 = empty_strided_cuda((128,), (1,), torch.float32) triton_poi_fused_repeat_2[grid(128)](primals_29, buf60, 128, XBLOCK =128, num_warps=4, num_stages=1) del primals_29 buf65 = empty_strided_cuda((4, 32, 72, 72), (165888, 5184, 72, 1), torch.float32) triton_poi_fused_reflection_pad2d_relu_14[grid(663552)](buf58, buf61, buf64, buf59, buf60, buf65, 663552, XBLOCK=512, num_warps=8, num_stages=1) buf66 = extern_kernels.convolution(buf65, primals_30, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf66, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf67 = buf66 del buf66 triton_poi_fused_convolution_15[grid(49152)](buf67, primals_31, 49152, XBLOCK=256, num_warps=4, num_stages=1) del primals_31 return (buf67, primals_2, primals_6, primals_10, primals_14, primals_18, primals_22, primals_26, primals_30, buf0, buf2, buf3, buf4, buf5, buf8, buf9, buf11, buf12, buf13, buf14, buf17, buf19, buf21, buf22, buf23, buf24, buf27, buf28, buf30, buf31, reinterpret_tensor(buf35, (256,), (1,), 0), buf37, buf39, buf40, buf41, buf42, buf45, buf46, buf48, buf49, reinterpret_tensor(buf53, (256,), (1,), 0), buf54, buf55, buf56, buf58, buf59, buf60, buf61, buf64, buf65, reinterpret_tensor(buf50, (1, 256, 1, 1), (256, 1, 1, 1), 0), reinterpret_tensor(buf32, (1, 256, 1, 1), (256, 1, 1, 1), 0)) class ConvLayer(torch.nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride): super(ConvLayer, self).__init__() reflection_padding = kernel_size // 2 self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding) self.conv2d = torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride) def forward(self, x): out = self.reflection_pad(x) out = self.conv2d(out) return out class ResidualBlock(torch.nn.Module): """ResidualBlock introduced in: https://arxiv.org/abs/1512.03385 recommended architecture: http://torch.ch/blog/2016/02/04/resnets.html """ def __init__(self, channels): super(ResidualBlock, self).__init__() self.conv1 = ConvLayer(channels, channels, kernel_size=3, stride=1) self.in1 = torch.nn.InstanceNorm2d(channels, affine=True) self.conv2 = ConvLayer(channels, channels, kernel_size=3, stride=1) self.in2 = torch.nn.InstanceNorm2d(channels, affine=True) self.relu = torch.nn.ReLU() def forward(self, x): residual = x out = self.relu(self.in1(self.conv1(x))) out = self.in2(self.conv2(out)) out = out + residual return out class UpsampleConvLayer(torch.nn.Module): """UpsampleConvLayer Upsamples the input and then does a convolution. This method gives better results compared to ConvTranspose2d. ref: http://distill.pub/2016/deconv-checkerboard/ """ def __init__(self, in_channels, out_channels, kernel_size, stride, upsample=None): super(UpsampleConvLayer, self).__init__() self.upsample = upsample reflection_padding = kernel_size // 2 self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding) self.conv2d = torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride) def forward(self, x): x_in = x if self.upsample: x_in = torch.nn.functional.interpolate(x_in, mode='nearest', scale_factor=self.upsample) out = self.reflection_pad(x_in) out = self.conv2d(out) return out class SmallTransformerNetNew(torch.nn.Module): def __init__(self): super(SmallTransformerNetNew, self).__init__() self.conv1 = ConvLayer(3, 32, kernel_size=9, stride=1) self.in1 = torch.nn.InstanceNorm2d(32, affine=True) self.conv2 = ConvLayer(32, 64, kernel_size=3, stride=2) self.in2 = torch.nn.InstanceNorm2d(64, affine=True) self.res6 = ResidualBlock(64) self.res7 = ResidualBlock(64) self.deconv2 = UpsampleConvLayer(64, 32, kernel_size=3, stride=1, upsample=2) self.in5 = torch.nn.InstanceNorm2d(32, affine=True) self.deconv3 = ConvLayer(32, 3, kernel_size=9, stride=1) self.relu = torch.nn.ReLU() def forward(self, input_0): primals_2 = self.conv1.conv2d.weight primals_3 = self.conv1.conv2d.bias primals_4 = self.in1.weight primals_5 = self.in1.bias primals_6 = self.conv2.conv2d.weight primals_7 = self.conv2.conv2d.bias primals_8 = self.in2.weight primals_9 = self.in2.bias primals_10 = self.res6.conv1.conv2d.weight primals_11 = self.res6.conv1.conv2d.bias primals_12 = self.res6.in1.weight primals_13 = self.res6.in1.bias primals_14 = self.res6.conv2.conv2d.weight primals_15 = self.res6.conv2.conv2d.bias primals_16 = self.res6.in2.weight primals_17 = self.res6.in2.bias primals_18 = self.res7.conv1.conv2d.weight primals_19 = self.res7.conv1.conv2d.bias primals_20 = self.res7.in1.weight primals_21 = self.res7.in1.bias primals_22 = self.res7.conv2.conv2d.weight primals_23 = self.res7.conv2.conv2d.bias primals_24 = self.res7.in2.weight primals_25 = self.res7.in2.bias primals_26 = self.deconv2.conv2d.weight primals_27 = self.deconv2.conv2d.bias primals_28 = self.in5.weight primals_29 = self.in5.bias primals_30 = self.deconv3.conv2d.weight primals_31 = self.deconv3.conv2d.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16, primals_17, primals_18, primals_19, primals_20, primals_21, primals_22, primals_23, primals_24, primals_25, primals_26, primals_27, primals_28, primals_29, primals_30, primals_31]) return output[0]
CorentinChauvin/style-transfer-KD
SmallTransformerNet
false
5,119
[ "MIT" ]
1
87bcb2963dbb8d09faf94c74a744f358cafe5427
https://github.com/CorentinChauvin/style-transfer-KD/tree/87bcb2963dbb8d09faf94c74a744f358cafe5427
LabelPropagation
import torch import torch.nn.functional as F import torch.nn as nn class LabelPropagation(nn.Module): """label propagation model adapted from https://github.com/CUAI/CorrectAndSmooth `"Learning from Labeled and Unlabeled Datawith Label Propagation" <http://mlg.eng.cam.ac.uk/zoubin/papers/CMU-CALD-02-107.pdf>`_ paper """ def __init__(self, num_layers=50, alpha=0.5, residual=True): super().__init__() self.num_layers = num_layers self.alpha = alpha self.residual = residual @torch.no_grad() def forward(self, y, adj, mask=None): if y.dtype == torch.long: y = F.one_hot(y.view(-1)).float() out = y if mask is not None: out = torch.zeros_like(y) out[mask] = y[mask] if self.residual: res = (1 - self.alpha) * out else: res = out.clone() for _ in range(self.num_layers): out = self.alpha * (adj @ out) + res out = torch.clamp(out, 0, 1) return out def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_clamp_mul_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp3 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.5 tmp2 = tmp0 * tmp1 tmp4 = tmp3 * tmp1 tmp5 = tmp2 + tmp4 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = 1.0 tmp9 = triton_helpers.minimum(tmp7, tmp8) tl.store(in_out_ptr0 + x0, tmp9, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(arg0_1, (16, 4, 4), (16, 4, 1), 0), out=buf0) buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 get_raw_stream(0) triton_poi_fused_add_clamp_mul_0[grid(256)](buf1, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf2 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf1, (16, 4, 4), (16, 4, 1), 0), out =buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf2 triton_poi_fused_add_clamp_mul_0[grid(256)](buf3, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf4 = reinterpret_tensor(buf1, (16, 4, 4), (16, 4, 1), 0) del buf1 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf3, (16, 4, 4), (16, 4, 1), 0), out =buf4) buf5 = reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf4 triton_poi_fused_add_clamp_mul_0[grid(256)](buf5, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf6 = reinterpret_tensor(buf3, (16, 4, 4), (16, 4, 1), 0) del buf3 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf5, (16, 4, 4), (16, 4, 1), 0), out =buf6) buf7 = reinterpret_tensor(buf6, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf6 triton_poi_fused_add_clamp_mul_0[grid(256)](buf7, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf8 = reinterpret_tensor(buf5, (16, 4, 4), (16, 4, 1), 0) del buf5 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf7, (16, 4, 4), (16, 4, 1), 0), out =buf8) buf9 = reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf8 triton_poi_fused_add_clamp_mul_0[grid(256)](buf9, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf10 = reinterpret_tensor(buf7, (16, 4, 4), (16, 4, 1), 0) del buf7 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0), out =buf10) buf11 = reinterpret_tensor(buf10, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf10 triton_poi_fused_add_clamp_mul_0[grid(256)](buf11, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf12 = reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0) del buf9 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf11, (16, 4, 4), (16, 4, 1), 0), out=buf12) buf13 = reinterpret_tensor(buf12, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf12 triton_poi_fused_add_clamp_mul_0[grid(256)](buf13, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf14 = reinterpret_tensor(buf11, (16, 4, 4), (16, 4, 1), 0) del buf11 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf13, (16, 4, 4), (16, 4, 1), 0), out=buf14) buf15 = reinterpret_tensor(buf14, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf14 triton_poi_fused_add_clamp_mul_0[grid(256)](buf15, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf16 = reinterpret_tensor(buf13, (16, 4, 4), (16, 4, 1), 0) del buf13 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf15, (16, 4, 4), (16, 4, 1), 0), out=buf16) buf17 = reinterpret_tensor(buf16, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf16 triton_poi_fused_add_clamp_mul_0[grid(256)](buf17, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf18 = reinterpret_tensor(buf15, (16, 4, 4), (16, 4, 1), 0) del buf15 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf17, (16, 4, 4), (16, 4, 1), 0), out=buf18) buf19 = reinterpret_tensor(buf18, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf18 triton_poi_fused_add_clamp_mul_0[grid(256)](buf19, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf20 = reinterpret_tensor(buf17, (16, 4, 4), (16, 4, 1), 0) del buf17 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf19, (16, 4, 4), (16, 4, 1), 0), out=buf20) buf21 = reinterpret_tensor(buf20, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf20 triton_poi_fused_add_clamp_mul_0[grid(256)](buf21, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf22 = reinterpret_tensor(buf19, (16, 4, 4), (16, 4, 1), 0) del buf19 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf21, (16, 4, 4), (16, 4, 1), 0), out=buf22) buf23 = reinterpret_tensor(buf22, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf22 triton_poi_fused_add_clamp_mul_0[grid(256)](buf23, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf24 = reinterpret_tensor(buf21, (16, 4, 4), (16, 4, 1), 0) del buf21 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf23, (16, 4, 4), (16, 4, 1), 0), out=buf24) buf25 = reinterpret_tensor(buf24, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf24 triton_poi_fused_add_clamp_mul_0[grid(256)](buf25, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf26 = reinterpret_tensor(buf23, (16, 4, 4), (16, 4, 1), 0) del buf23 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf25, (16, 4, 4), (16, 4, 1), 0), out=buf26) buf27 = reinterpret_tensor(buf26, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf26 triton_poi_fused_add_clamp_mul_0[grid(256)](buf27, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf28 = reinterpret_tensor(buf25, (16, 4, 4), (16, 4, 1), 0) del buf25 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf27, (16, 4, 4), (16, 4, 1), 0), out=buf28) buf29 = reinterpret_tensor(buf28, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf28 triton_poi_fused_add_clamp_mul_0[grid(256)](buf29, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf30 = reinterpret_tensor(buf27, (16, 4, 4), (16, 4, 1), 0) del buf27 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf29, (16, 4, 4), (16, 4, 1), 0), out=buf30) buf31 = reinterpret_tensor(buf30, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf30 triton_poi_fused_add_clamp_mul_0[grid(256)](buf31, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf32 = reinterpret_tensor(buf29, (16, 4, 4), (16, 4, 1), 0) del buf29 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf31, (16, 4, 4), (16, 4, 1), 0), out=buf32) buf33 = reinterpret_tensor(buf32, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf32 triton_poi_fused_add_clamp_mul_0[grid(256)](buf33, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf34 = reinterpret_tensor(buf31, (16, 4, 4), (16, 4, 1), 0) del buf31 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf33, (16, 4, 4), (16, 4, 1), 0), out=buf34) buf35 = reinterpret_tensor(buf34, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf34 triton_poi_fused_add_clamp_mul_0[grid(256)](buf35, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf36 = reinterpret_tensor(buf33, (16, 4, 4), (16, 4, 1), 0) del buf33 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf35, (16, 4, 4), (16, 4, 1), 0), out=buf36) buf37 = reinterpret_tensor(buf36, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf36 triton_poi_fused_add_clamp_mul_0[grid(256)](buf37, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf38 = reinterpret_tensor(buf35, (16, 4, 4), (16, 4, 1), 0) del buf35 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf37, (16, 4, 4), (16, 4, 1), 0), out=buf38) buf39 = reinterpret_tensor(buf38, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf38 triton_poi_fused_add_clamp_mul_0[grid(256)](buf39, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf40 = reinterpret_tensor(buf37, (16, 4, 4), (16, 4, 1), 0) del buf37 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf39, (16, 4, 4), (16, 4, 1), 0), out=buf40) buf41 = reinterpret_tensor(buf40, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf40 triton_poi_fused_add_clamp_mul_0[grid(256)](buf41, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf42 = reinterpret_tensor(buf39, (16, 4, 4), (16, 4, 1), 0) del buf39 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf41, (16, 4, 4), (16, 4, 1), 0), out=buf42) buf43 = reinterpret_tensor(buf42, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf42 triton_poi_fused_add_clamp_mul_0[grid(256)](buf43, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf44 = reinterpret_tensor(buf41, (16, 4, 4), (16, 4, 1), 0) del buf41 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf43, (16, 4, 4), (16, 4, 1), 0), out=buf44) buf45 = reinterpret_tensor(buf44, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf44 triton_poi_fused_add_clamp_mul_0[grid(256)](buf45, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf46 = reinterpret_tensor(buf43, (16, 4, 4), (16, 4, 1), 0) del buf43 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf45, (16, 4, 4), (16, 4, 1), 0), out=buf46) buf47 = reinterpret_tensor(buf46, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf46 triton_poi_fused_add_clamp_mul_0[grid(256)](buf47, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf48 = reinterpret_tensor(buf45, (16, 4, 4), (16, 4, 1), 0) del buf45 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf47, (16, 4, 4), (16, 4, 1), 0), out=buf48) buf49 = reinterpret_tensor(buf48, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf48 triton_poi_fused_add_clamp_mul_0[grid(256)](buf49, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf50 = reinterpret_tensor(buf47, (16, 4, 4), (16, 4, 1), 0) del buf47 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf49, (16, 4, 4), (16, 4, 1), 0), out=buf50) buf51 = reinterpret_tensor(buf50, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf50 triton_poi_fused_add_clamp_mul_0[grid(256)](buf51, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf52 = reinterpret_tensor(buf49, (16, 4, 4), (16, 4, 1), 0) del buf49 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf51, (16, 4, 4), (16, 4, 1), 0), out=buf52) buf53 = reinterpret_tensor(buf52, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf52 triton_poi_fused_add_clamp_mul_0[grid(256)](buf53, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf54 = reinterpret_tensor(buf51, (16, 4, 4), (16, 4, 1), 0) del buf51 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf53, (16, 4, 4), (16, 4, 1), 0), out=buf54) buf55 = reinterpret_tensor(buf54, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf54 triton_poi_fused_add_clamp_mul_0[grid(256)](buf55, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf56 = reinterpret_tensor(buf53, (16, 4, 4), (16, 4, 1), 0) del buf53 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf55, (16, 4, 4), (16, 4, 1), 0), out=buf56) buf57 = reinterpret_tensor(buf56, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf56 triton_poi_fused_add_clamp_mul_0[grid(256)](buf57, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf58 = reinterpret_tensor(buf55, (16, 4, 4), (16, 4, 1), 0) del buf55 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf57, (16, 4, 4), (16, 4, 1), 0), out=buf58) buf59 = reinterpret_tensor(buf58, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf58 triton_poi_fused_add_clamp_mul_0[grid(256)](buf59, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf60 = reinterpret_tensor(buf57, (16, 4, 4), (16, 4, 1), 0) del buf57 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf59, (16, 4, 4), (16, 4, 1), 0), out=buf60) buf61 = reinterpret_tensor(buf60, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf60 triton_poi_fused_add_clamp_mul_0[grid(256)](buf61, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf62 = reinterpret_tensor(buf59, (16, 4, 4), (16, 4, 1), 0) del buf59 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf61, (16, 4, 4), (16, 4, 1), 0), out=buf62) buf63 = reinterpret_tensor(buf62, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf62 triton_poi_fused_add_clamp_mul_0[grid(256)](buf63, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf64 = reinterpret_tensor(buf61, (16, 4, 4), (16, 4, 1), 0) del buf61 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf63, (16, 4, 4), (16, 4, 1), 0), out=buf64) buf65 = reinterpret_tensor(buf64, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf64 triton_poi_fused_add_clamp_mul_0[grid(256)](buf65, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf66 = reinterpret_tensor(buf63, (16, 4, 4), (16, 4, 1), 0) del buf63 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf65, (16, 4, 4), (16, 4, 1), 0), out=buf66) buf67 = reinterpret_tensor(buf66, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf66 triton_poi_fused_add_clamp_mul_0[grid(256)](buf67, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf68 = reinterpret_tensor(buf65, (16, 4, 4), (16, 4, 1), 0) del buf65 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf67, (16, 4, 4), (16, 4, 1), 0), out=buf68) buf69 = reinterpret_tensor(buf68, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf68 triton_poi_fused_add_clamp_mul_0[grid(256)](buf69, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf70 = reinterpret_tensor(buf67, (16, 4, 4), (16, 4, 1), 0) del buf67 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf69, (16, 4, 4), (16, 4, 1), 0), out=buf70) buf71 = reinterpret_tensor(buf70, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf70 triton_poi_fused_add_clamp_mul_0[grid(256)](buf71, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf72 = reinterpret_tensor(buf69, (16, 4, 4), (16, 4, 1), 0) del buf69 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf71, (16, 4, 4), (16, 4, 1), 0), out=buf72) buf73 = reinterpret_tensor(buf72, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf72 triton_poi_fused_add_clamp_mul_0[grid(256)](buf73, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf74 = reinterpret_tensor(buf71, (16, 4, 4), (16, 4, 1), 0) del buf71 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf73, (16, 4, 4), (16, 4, 1), 0), out=buf74) buf75 = reinterpret_tensor(buf74, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf74 triton_poi_fused_add_clamp_mul_0[grid(256)](buf75, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf76 = reinterpret_tensor(buf73, (16, 4, 4), (16, 4, 1), 0) del buf73 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf75, (16, 4, 4), (16, 4, 1), 0), out=buf76) buf77 = reinterpret_tensor(buf76, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf76 triton_poi_fused_add_clamp_mul_0[grid(256)](buf77, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf78 = reinterpret_tensor(buf75, (16, 4, 4), (16, 4, 1), 0) del buf75 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf77, (16, 4, 4), (16, 4, 1), 0), out=buf78) buf79 = reinterpret_tensor(buf78, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf78 triton_poi_fused_add_clamp_mul_0[grid(256)](buf79, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf80 = reinterpret_tensor(buf77, (16, 4, 4), (16, 4, 1), 0) del buf77 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf79, (16, 4, 4), (16, 4, 1), 0), out=buf80) buf81 = reinterpret_tensor(buf80, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf80 triton_poi_fused_add_clamp_mul_0[grid(256)](buf81, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf82 = reinterpret_tensor(buf79, (16, 4, 4), (16, 4, 1), 0) del buf79 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf81, (16, 4, 4), (16, 4, 1), 0), out=buf82) buf83 = reinterpret_tensor(buf82, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf82 triton_poi_fused_add_clamp_mul_0[grid(256)](buf83, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf84 = reinterpret_tensor(buf81, (16, 4, 4), (16, 4, 1), 0) del buf81 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf83, (16, 4, 4), (16, 4, 1), 0), out=buf84) buf85 = reinterpret_tensor(buf84, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf84 triton_poi_fused_add_clamp_mul_0[grid(256)](buf85, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf86 = reinterpret_tensor(buf83, (16, 4, 4), (16, 4, 1), 0) del buf83 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf85, (16, 4, 4), (16, 4, 1), 0), out=buf86) buf87 = reinterpret_tensor(buf86, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf86 triton_poi_fused_add_clamp_mul_0[grid(256)](buf87, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf88 = reinterpret_tensor(buf85, (16, 4, 4), (16, 4, 1), 0) del buf85 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf87, (16, 4, 4), (16, 4, 1), 0), out=buf88) buf89 = reinterpret_tensor(buf88, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf88 triton_poi_fused_add_clamp_mul_0[grid(256)](buf89, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf90 = reinterpret_tensor(buf87, (16, 4, 4), (16, 4, 1), 0) del buf87 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf89, (16, 4, 4), (16, 4, 1), 0), out=buf90) buf91 = reinterpret_tensor(buf90, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf90 triton_poi_fused_add_clamp_mul_0[grid(256)](buf91, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf92 = reinterpret_tensor(buf89, (16, 4, 4), (16, 4, 1), 0) del buf89 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf91, (16, 4, 4), (16, 4, 1), 0), out=buf92) buf93 = reinterpret_tensor(buf92, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf92 triton_poi_fused_add_clamp_mul_0[grid(256)](buf93, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf94 = reinterpret_tensor(buf91, (16, 4, 4), (16, 4, 1), 0) del buf91 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf93, (16, 4, 4), (16, 4, 1), 0), out=buf94) buf95 = reinterpret_tensor(buf94, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf94 triton_poi_fused_add_clamp_mul_0[grid(256)](buf95, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf96 = reinterpret_tensor(buf93, (16, 4, 4), (16, 4, 1), 0) del buf93 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf95, (16, 4, 4), (16, 4, 1), 0), out=buf96) buf97 = reinterpret_tensor(buf96, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf96 triton_poi_fused_add_clamp_mul_0[grid(256)](buf97, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf98 = reinterpret_tensor(buf95, (16, 4, 4), (16, 4, 1), 0) del buf95 extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(buf97, (16, 4, 4), (16, 4, 1), 0), out=buf98) del arg1_1 del buf97 buf99 = reinterpret_tensor(buf98, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf98 triton_poi_fused_add_clamp_mul_0[grid(256)](buf99, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf99, class LabelPropagationNew(nn.Module): """label propagation model adapted from https://github.com/CUAI/CorrectAndSmooth `"Learning from Labeled and Unlabeled Datawith Label Propagation" <http://mlg.eng.cam.ac.uk/zoubin/papers/CMU-CALD-02-107.pdf>`_ paper """ def __init__(self, num_layers=50, alpha=0.5, residual=True): super().__init__() self.num_layers = num_layers self.alpha = alpha self.residual = residual def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
EdisonLeeeee/Graphgallery
LabelPropagation
false
5,120
[ "MIT" ]
1
8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
https://github.com/EdisonLeeeee/Graphgallery/tree/8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
Foo
import torch import torch.nn.functional import torch.nn.parallel import torch.utils.data import torch.optim import torch.utils.data.distributed class Foo(torch.nn.Module): def __init__(self, size): super(Foo, self).__init__() self.n = torch.nn.Parameter(torch.ones(size)) self.m = torch.nn.Parameter(torch.ones(size)) def forward(self, input): return self.n * input + self.m def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'size': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn.functional import torch.nn.parallel import torch.utils.data import torch.optim import torch.utils.data.distributed assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_mul_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x2 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask) tmp3 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 * tmp1 tmp4 = tmp2 + tmp3 tl.store(out_ptr0 + x2, tmp4, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4,), (1,)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mul_0[grid(256)](primals_1, primals_2, primals_3, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 del primals_3 return buf0, primals_2 class FooNew(torch.nn.Module): def __init__(self, size): super(FooNew, self).__init__() self.n = torch.nn.Parameter(torch.ones(size)) self.m = torch.nn.Parameter(torch.ones(size)) def forward(self, input_0): primals_1 = self.n primals_3 = self.m primals_2 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
Ella77/tacotron2_multispeaker_pytorch
Foo
false
5,121
[ "BSD-3-Clause" ]
1
859eab0a8e3bd7545e623ce47fe1563702d38442
https://github.com/Ella77/tacotron2_multispeaker_pytorch/tree/859eab0a8e3bd7545e623ce47fe1563702d38442
LinearZeros
import torch import torch.nn as nn class LinearZeros(nn.Linear): def __init__(self, in_features, out_features, bias=True, logscale_factor=3.0): """ Linear layer with zero initialization :param in_features: size of each input sample :type in_features: int :param out_features: size of each output sample :type out_features: int :param bias: whether to learn an additive bias. :type bias: bool :param logscale_factor: factor of logscale :type logscale_factor: float """ super().__init__(in_features, out_features, bias) self.logscale_factor = logscale_factor self.weight.data.zero_() self.bias.data.zero_() self.register_parameter('logs', nn.Parameter(torch.zeros(out_features)) ) def forward(self, x): """ Forward linear zero layer :param x: input :type x: torch.Tensor :return: output :rtype: torch.Tensor """ output = super().forward(x) output *= torch.exp(self.logs * self.logscale_factor) return output def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_exp_mul_view_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x4, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = 3.0 tmp3 = tmp1 * tmp2 tmp4 = tl_math.exp(tmp3) tmp5 = tmp0 * tmp4 tl.store(in_out_ptr0 + x4, tmp5, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf2 = buf1 del buf1 get_raw_stream(0) triton_poi_fused_exp_mul_view_0[grid(256)](buf2, buf0, primals_4, 256, XBLOCK=256, num_warps=4, num_stages=1) return buf2, primals_4, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf0 class LinearZerosNew(nn.Linear): def __init__(self, in_features, out_features, bias=True, logscale_factor=3.0): """ Linear layer with zero initialization :param in_features: size of each input sample :type in_features: int :param out_features: size of each output sample :type out_features: int :param bias: whether to learn an additive bias. :type bias: bool :param logscale_factor: factor of logscale :type logscale_factor: float """ super().__init__(in_features, out_features, bias) self.logscale_factor = logscale_factor self.weight.data.zero_() self.bias.data.zero_() self.register_parameter('logs', nn.Parameter(torch.zeros(out_features)) ) def forward(self, input_0): primals_1 = self.weight primals_2 = self.bias primals_4 = self.logs primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
Eladhi/VI_Glow
LinearZeros
false
5,122
[ "MIT" ]
1
9c48fbf8fa10c81fc2354a07fcc2837a77d06cef
https://github.com/Eladhi/VI_Glow/tree/9c48fbf8fa10c81fc2354a07fcc2837a77d06cef
SSGC
import torch import torch.nn as nn class SpectralEigenConv(nn.Module): def __init__(self, in_features, out_features, bias=False, K=10, alpha= 0.1, **kwargs): super().__init__() assert K > 0 self.K = K self.alpha = alpha self.in_features = in_features self.out_features = out_features self.w = nn.Linear(in_features, out_features, bias=bias) def forward(self, x, U, V=None): """ x: node attribute matrix if `V=None`: U: (N, N) adjacency matrix else: U: (N, k) eigenvector matrix V: (k,) eigenvalue """ x = self.w(x) if V is not None: V_pow = torch.ones_like(V) V_out = torch.zeros_like(V) for _ in range(self.K): V_pow *= V V_out += (1 - self.alpha) * V_pow V_out = V_out / self.K x_out = U * V_out @ (U.t() @ x) + self.alpha * x else: adj = U x_in = x x_out = torch.zeros_like(x) for _ in range(self.K): x = torch.spmm(adj, x) x_out += (1 - self.alpha) * x x_out /= self.K x_out += self.alpha * x_in return x_out def reset_parameters(self): self.w.reset_parameters() def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features}, K={self.K}, alpha={self.alpha})' ) class SSGC(nn.Module): def __init__(self, in_features, out_features, K=10, alpha=0.1, hids=[], acts=[], dropout=0.5, bias=False): super().__init__() lin = [] lin.append(nn.Dropout(dropout)) for hid, act in zip(hids, acts): lin.append(nn.Linear(in_features, hid, bias=bias)) lin.append(activations.get(act)) lin.append(nn.Dropout(dropout)) in_features = hid lin = nn.Sequential(*lin) conv = SpectralEigenConv(in_features, out_features, bias=bias, K=K, alpha=alpha) self.lin = lin self.conv = conv def forward(self, x, U, V=None): """ x: feature matrix if `V=None`: U: (N, N) adjacency matrix else: U: (N, k) eigenvector matrix V: (k,) eigenvalue """ x = self.lin(x) x = self.conv(x, U, V) return x def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_div_mul_0(in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp3 = tl.load(in_ptr0 + x0, xmask) tmp6 = tl.load(in_ptr1 + x0, xmask) tmp9 = tl.load(in_ptr2 + x0, xmask) tmp12 = tl.load(in_ptr3 + x0, xmask) tmp15 = tl.load(in_ptr4 + x0, xmask) tmp18 = tl.load(in_ptr5 + x0, xmask) tmp21 = tl.load(in_ptr6 + x0, xmask) tmp24 = tl.load(in_ptr7 + x0, xmask) tmp27 = tl.load(in_ptr8 + x0, xmask) tmp32 = tl.load(in_out_ptr1 + x0, xmask) tmp1 = 0.9 tmp2 = tmp0 * tmp1 tmp4 = tmp3 * tmp1 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp1 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp1 tmp11 = tmp8 + tmp10 tmp13 = tmp12 * tmp1 tmp14 = tmp11 + tmp13 tmp16 = tmp15 * tmp1 tmp17 = tmp14 + tmp16 tmp19 = tmp18 * tmp1 tmp20 = tmp17 + tmp19 tmp22 = tmp21 * tmp1 tmp23 = tmp20 + tmp22 tmp25 = tmp24 * tmp1 tmp26 = tmp23 + tmp25 tmp28 = tmp27 * tmp1 tmp29 = tmp26 + tmp28 tmp30 = 0.1 tmp31 = tmp29 * tmp30 tmp33 = tmp32 * tmp30 tmp34 = tmp31 + tmp33 tl.store(in_out_ptr1 + x0, tmp34, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf0, out=buf1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf1, out=buf2) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf2, out=buf3) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf3, out=buf4) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf4, out=buf5) buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf5, out=buf6) buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf6, out=buf7) buf8 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf7, out=buf8) buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf8, out=buf9) buf11 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf9, out=buf11) buf10 = buf1 del buf1 buf12 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_add_div_mul_0[grid(16)](buf10, buf12, buf2, buf3, buf4, buf5, buf6, buf7, buf8, buf9, buf11, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf10 del buf11 del buf2 del buf3 del buf4 del buf5 del buf6 del buf7 del buf8 del buf9 return buf12, primals_1, reinterpret_tensor(primals_3, (4, 4), (1, 4), 0) class SpectralEigenConv(nn.Module): def __init__(self, in_features, out_features, bias=False, K=10, alpha= 0.1, **kwargs): super().__init__() assert K > 0 self.K = K self.alpha = alpha self.in_features = in_features self.out_features = out_features self.w = nn.Linear(in_features, out_features, bias=bias) def forward(self, x, U, V=None): """ x: node attribute matrix if `V=None`: U: (N, N) adjacency matrix else: U: (N, k) eigenvector matrix V: (k,) eigenvalue """ x = self.w(x) if V is not None: V_pow = torch.ones_like(V) V_out = torch.zeros_like(V) for _ in range(self.K): V_pow *= V V_out += (1 - self.alpha) * V_pow V_out = V_out / self.K x_out = U * V_out @ (U.t() @ x) + self.alpha * x else: adj = U x_in = x x_out = torch.zeros_like(x) for _ in range(self.K): x = torch.spmm(adj, x) x_out += (1 - self.alpha) * x x_out /= self.K x_out += self.alpha * x_in return x_out def reset_parameters(self): self.w.reset_parameters() def __repr__(self): return ( f'{self.__class__.__name__}({self.in_features}, {self.out_features}, K={self.K}, alpha={self.alpha})' ) class SSGCNew(nn.Module): def __init__(self, in_features, out_features, K=10, alpha=0.1, hids=[], acts=[], dropout=0.5, bias=False): super().__init__() lin = [] lin.append(nn.Dropout(dropout)) for hid, act in zip(hids, acts): lin.append(nn.Linear(in_features, hid, bias=bias)) lin.append(activations.get(act)) lin.append(nn.Dropout(dropout)) in_features = hid lin = nn.Sequential(*lin) conv = SpectralEigenConv(in_features, out_features, bias=bias, K=K, alpha=alpha) self.lin = lin self.conv = conv def forward(self, input_0, input_1): primals_1 = self.conv.w.weight primals_2 = input_0 primals_3 = input_1 output = call([primals_1, primals_2, primals_3]) return output[0]
EdisonLeeeee/Graphgallery
SSGC
false
5,123
[ "MIT" ]
1
8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
https://github.com/EdisonLeeeee/Graphgallery/tree/8ae9ef57d44f073d0ceaf3f33a3a998546f960a8
Conv2dZeros
import torch import torch.nn as nn class ActNorm(nn.Module): def __init__(self, num_channels, scale=1.0, logscale_factor=3.0, batch_variance=False): """ Activation normalization layer :param num_channels: number of channels :type num_channels: int :param scale: scale :type scale: float :param logscale_factor: factor for logscale :type logscale_factor: float :param batch_variance: use batch variance :type batch_variance: bool """ super().__init__() self.num_channels = num_channels self.scale = scale self.logscale_factor = logscale_factor self.batch_variance = batch_variance self.bias_inited = False self.logs_inited = False self.register_parameter('bias', nn.Parameter(torch.zeros(1, self. num_channels, 1, 1))) self.register_parameter('logs', nn.Parameter(torch.zeros(1, self. num_channels, 1, 1))) def actnorm_center(self, x, reverse=False): """ center operation of activation normalization :param x: input :type x: torch.Tensor :param reverse: whether to reverse bias :type reverse: bool :return: centered input :rtype: torch.Tensor """ if not self.bias_inited: self.initialize_bias(x) if not reverse: return x + self.bias else: return x - self.bias def actnorm_scale(self, x, logdet, reverse=False): """ scale operation of activation normalization :param x: input :type x: torch.Tensor :param logdet: log determinant :type logdet: :param reverse: whether to reverse bias :type reverse: bool :return: centered input and logdet :rtype: tuple(torch.Tensor, torch.Tensor) """ if not self.logs_inited: self.initialize_logs(x) logs = self.logs * self.logscale_factor if not reverse: x *= torch.exp(logs) else: x *= torch.exp(-logs) if logdet is not None: logdet_factor = ops.count_pixels(x) dlogdet = torch.sum(logs) * logdet_factor if reverse: dlogdet *= -1 logdet += dlogdet return x, logdet def initialize_bias(self, x): """ Initialize bias :param x: input :type x: torch.Tensor """ if not self.training: return with torch.no_grad(): x_mean = -1.0 * ops.reduce_mean(x, dim=[0, 2, 3], keepdim=True) self.bias.data.copy_(x_mean.data) self.bias_inited = True def initialize_logs(self, x): """ Initialize logs :param x: input :type x: torch.Tensor """ if not self.training: return with torch.no_grad(): if self.batch_variance: x_var = ops.reduce_mean(x ** 2, keepdim=True) else: x_var = ops.reduce_mean(x ** 2, dim=[0, 2, 3], keepdim=True) logs = torch.log(self.scale / (torch.sqrt(x_var) + 1e-06) ) / self.logscale_factor self.logs.data.copy_(logs.data) self.logs_inited = True def forward(self, x, logdet=None, reverse=False): """ Forward activation normalization layer :param x: input :type x: torch.Tensor :param logdet: log determinant :type logdet: :param reverse: whether to reverse bias :type reverse: bool :return: normalized input and logdet :rtype: tuple(torch.Tensor, torch.Tensor) """ assert len(x.shape) == 4 assert x.shape[1 ] == self.num_channels, 'Input shape should be NxCxHxW, however channels are {} instead of {}'.format( x.shape[1], self.num_channels) assert x.device == self.bias.device and x.device == self.logs.device, 'Expect input device {} instead of {}'.format( self.bias.device, x.device) if not reverse: x = self.actnorm_center(x, reverse=False) x, logdet = self.actnorm_scale(x, logdet, reverse=False) else: x, logdet = self.actnorm_scale(x, logdet, reverse=True) x = self.actnorm_center(x, reverse=True) return x, logdet class Conv2d(nn.Conv2d): @staticmethod def get_padding(padding_type, kernel_size, stride): """ Get padding size. mentioned in https://github.com/pytorch/pytorch/issues/3867#issuecomment-361775080 behaves as 'SAME' padding in TensorFlow independent on input size when stride is 1 :param padding_type: type of padding in ['SAME', 'VALID'] :type padding_type: str :param kernel_size: kernel size :type kernel_size: tuple(int) or int :param stride: stride :type stride: int :return: padding size :rtype: tuple(int) """ assert padding_type in ['SAME', 'VALID' ], 'Unsupported padding type: {}'.format(padding_type) if isinstance(kernel_size, int): kernel_size = [kernel_size, kernel_size] if padding_type == 'SAME': assert stride == 1, "'SAME' padding only supports stride=1" return tuple((k - 1) // 2 for k in kernel_size) return tuple(0 for _ in kernel_size) def __init__(self, in_channels, out_channels, kernel_size=(3, 3), stride=1, padding_type='SAME', do_weightnorm=False, do_actnorm=True, dilation=1, groups=1): """ Wrapper of nn.Conv2d with weight normalization and activation normalization :param padding_type: type of padding in ['SAME', 'VALID'] :type padding_type: str :param do_weightnorm: whether to do weight normalization after convolution :type do_weightnorm: bool :param do_actnorm: whether to do activation normalization after convolution :type do_actnorm: bool """ padding = self.get_padding(padding_type, kernel_size, stride) super().__init__(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias=not do_actnorm) self.do_weight_norm = do_weightnorm self.do_actnorm = do_actnorm self.weight.data.normal_(mean=0.0, std=0.05) if self.do_actnorm: self.actnorm = ActNorm(out_channels) else: self.bias.data.zero_() def forward(self, x): """ Forward wrapped Conv2d layer :param x: input :type x: torch.Tensor :return: output :rtype: torch.Tensor """ x = super().forward(x) if self.do_actnorm: x, _ = self.actnorm(x) return x class Conv2dZeros(nn.Conv2d): def __init__(self, in_channels, out_channels, kernel_size=(3, 3), stride=1, padding_type='SAME', logscale_factor=3, dilation=1, groups=1, bias=True): """ Wrapper of nn.Conv2d with zero initialization and logs :param padding_type: type of padding in ['SAME', 'VALID'] :type padding_type: str :param logscale_factor: factor for logscale :type logscale_factor: float """ padding = Conv2d.get_padding(padding_type, kernel_size, stride) super().__init__(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias) self.logscale_factor = logscale_factor self.bias.data.zero_() self.weight.data.zero_() self.register_parameter('logs', nn.Parameter(torch.zeros( out_channels, 1, 1))) def forward(self, x): """ Forward wrapped Conv2d layer :param x: input :type x: torch.Tensor :return: output :rtype: torch.Tensor """ x = super().forward(x) x *= torch.exp(self.logs * self.logscale_factor) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_convolution_exp_mul_0(in_out_ptr0, in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = 3.0 tmp5 = tmp3 * tmp4 tmp6 = tl_math.exp(tmp5) tmp7 = tmp2 * tmp6 tl.store(in_out_ptr0 + x3, tmp2, xmask) tl.store(out_ptr0 + x3, tmp7, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 1, 1), (1, 1, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1)) buf1 = buf0 del buf0 buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_convolution_exp_mul_0[grid(256)](buf1, primals_2, primals_4, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 return buf2, primals_1, primals_3, primals_4, buf1 class ActNorm(nn.Module): def __init__(self, num_channels, scale=1.0, logscale_factor=3.0, batch_variance=False): """ Activation normalization layer :param num_channels: number of channels :type num_channels: int :param scale: scale :type scale: float :param logscale_factor: factor for logscale :type logscale_factor: float :param batch_variance: use batch variance :type batch_variance: bool """ super().__init__() self.num_channels = num_channels self.scale = scale self.logscale_factor = logscale_factor self.batch_variance = batch_variance self.bias_inited = False self.logs_inited = False self.register_parameter('bias', nn.Parameter(torch.zeros(1, self. num_channels, 1, 1))) self.register_parameter('logs', nn.Parameter(torch.zeros(1, self. num_channels, 1, 1))) def actnorm_center(self, x, reverse=False): """ center operation of activation normalization :param x: input :type x: torch.Tensor :param reverse: whether to reverse bias :type reverse: bool :return: centered input :rtype: torch.Tensor """ if not self.bias_inited: self.initialize_bias(x) if not reverse: return x + self.bias else: return x - self.bias def actnorm_scale(self, x, logdet, reverse=False): """ scale operation of activation normalization :param x: input :type x: torch.Tensor :param logdet: log determinant :type logdet: :param reverse: whether to reverse bias :type reverse: bool :return: centered input and logdet :rtype: tuple(torch.Tensor, torch.Tensor) """ if not self.logs_inited: self.initialize_logs(x) logs = self.logs * self.logscale_factor if not reverse: x *= torch.exp(logs) else: x *= torch.exp(-logs) if logdet is not None: logdet_factor = ops.count_pixels(x) dlogdet = torch.sum(logs) * logdet_factor if reverse: dlogdet *= -1 logdet += dlogdet return x, logdet def initialize_bias(self, x): """ Initialize bias :param x: input :type x: torch.Tensor """ if not self.training: return with torch.no_grad(): x_mean = -1.0 * ops.reduce_mean(x, dim=[0, 2, 3], keepdim=True) self.bias.data.copy_(x_mean.data) self.bias_inited = True def initialize_logs(self, x): """ Initialize logs :param x: input :type x: torch.Tensor """ if not self.training: return with torch.no_grad(): if self.batch_variance: x_var = ops.reduce_mean(x ** 2, keepdim=True) else: x_var = ops.reduce_mean(x ** 2, dim=[0, 2, 3], keepdim=True) logs = torch.log(self.scale / (torch.sqrt(x_var) + 1e-06) ) / self.logscale_factor self.logs.data.copy_(logs.data) self.logs_inited = True def forward(self, x, logdet=None, reverse=False): """ Forward activation normalization layer :param x: input :type x: torch.Tensor :param logdet: log determinant :type logdet: :param reverse: whether to reverse bias :type reverse: bool :return: normalized input and logdet :rtype: tuple(torch.Tensor, torch.Tensor) """ assert len(x.shape) == 4 assert x.shape[1 ] == self.num_channels, 'Input shape should be NxCxHxW, however channels are {} instead of {}'.format( x.shape[1], self.num_channels) assert x.device == self.bias.device and x.device == self.logs.device, 'Expect input device {} instead of {}'.format( self.bias.device, x.device) if not reverse: x = self.actnorm_center(x, reverse=False) x, logdet = self.actnorm_scale(x, logdet, reverse=False) else: x, logdet = self.actnorm_scale(x, logdet, reverse=True) x = self.actnorm_center(x, reverse=True) return x, logdet class Conv2d(nn.Conv2d): @staticmethod def get_padding(padding_type, kernel_size, stride): """ Get padding size. mentioned in https://github.com/pytorch/pytorch/issues/3867#issuecomment-361775080 behaves as 'SAME' padding in TensorFlow independent on input size when stride is 1 :param padding_type: type of padding in ['SAME', 'VALID'] :type padding_type: str :param kernel_size: kernel size :type kernel_size: tuple(int) or int :param stride: stride :type stride: int :return: padding size :rtype: tuple(int) """ assert padding_type in ['SAME', 'VALID' ], 'Unsupported padding type: {}'.format(padding_type) if isinstance(kernel_size, int): kernel_size = [kernel_size, kernel_size] if padding_type == 'SAME': assert stride == 1, "'SAME' padding only supports stride=1" return tuple((k - 1) // 2 for k in kernel_size) return tuple(0 for _ in kernel_size) def __init__(self, in_channels, out_channels, kernel_size=(3, 3), stride=1, padding_type='SAME', do_weightnorm=False, do_actnorm=True, dilation=1, groups=1): """ Wrapper of nn.Conv2d with weight normalization and activation normalization :param padding_type: type of padding in ['SAME', 'VALID'] :type padding_type: str :param do_weightnorm: whether to do weight normalization after convolution :type do_weightnorm: bool :param do_actnorm: whether to do activation normalization after convolution :type do_actnorm: bool """ padding = self.get_padding(padding_type, kernel_size, stride) super().__init__(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias=not do_actnorm) self.do_weight_norm = do_weightnorm self.do_actnorm = do_actnorm self.weight.data.normal_(mean=0.0, std=0.05) if self.do_actnorm: self.actnorm = ActNorm(out_channels) else: self.bias.data.zero_() def forward(self, x): """ Forward wrapped Conv2d layer :param x: input :type x: torch.Tensor :return: output :rtype: torch.Tensor """ x = super().forward(x) if self.do_actnorm: x, _ = self.actnorm(x) return x class Conv2dZerosNew(nn.Conv2d): def __init__(self, in_channels, out_channels, kernel_size=(3, 3), stride=1, padding_type='SAME', logscale_factor=3, dilation=1, groups=1, bias=True): """ Wrapper of nn.Conv2d with zero initialization and logs :param padding_type: type of padding in ['SAME', 'VALID'] :type padding_type: str :param logscale_factor: factor for logscale :type logscale_factor: float """ padding = Conv2d.get_padding(padding_type, kernel_size, stride) super().__init__(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias) self.logscale_factor = logscale_factor self.bias.data.zero_() self.weight.data.zero_() self.register_parameter('logs', nn.Parameter(torch.zeros( out_channels, 1, 1))) def forward(self, input_0): primals_1 = self.weight primals_2 = self.bias primals_4 = self.logs primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
Eladhi/VI_Glow
Conv2dZeros
false
5,124
[ "MIT" ]
1
9c48fbf8fa10c81fc2354a07fcc2837a77d06cef
https://github.com/Eladhi/VI_Glow/tree/9c48fbf8fa10c81fc2354a07fcc2837a77d06cef
PositionwiseFeedForward
import torch import torch.nn as nn import torch.distributed class PositionwiseFeedForward(nn.Module): """ A two-layer Feed-Forward-Network with residual layer norm. Args: d_model (int): the size of input for the first-layer of the FFN. d_ff (int): the hidden layer size of the second-layer of the FNN. dropout (float): dropout probability(0-1.0). """ def __init__(self, d_model, d_ff, dropout=0.1): super(PositionwiseFeedForward, self).__init__() self.w_1 = nn.Linear(d_model, d_ff) self.w_2 = nn.Linear(d_ff, d_model) self.layer_norm = nn.LayerNorm(d_model, eps=1e-06) self.dropout_1 = nn.Dropout(dropout) self.relu = nn.ReLU() self.dropout_2 = nn.Dropout(dropout) def forward(self, x): """ Layer definition. Args: input: [ batch_size, input_len, model_dim ] Returns: output: [ batch_size, input_len, model_dim ] """ inter = self.dropout_1(self.relu(self.w_1(self.layer_norm(x)))) output = self.dropout_2(self.w_2(inter)) return output + x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'d_model': 4, 'd_ff': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.distributed assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_native_layer_norm_0(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tmp9 = tmp0 - tmp8 tmp10 = tmp9 * tmp9 tmp11 = tmp1 - tmp8 tmp12 = tmp11 * tmp11 tmp13 = tmp10 + tmp12 tmp14 = tmp3 - tmp8 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp8 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp19 / tmp7 tmp21 = 1e-06 tmp22 = tmp20 + tmp21 tmp23 = libdevice.rsqrt(tmp22) tl.store(out_ptr0 + x0, tmp8, xmask) tl.store(out_ptr1 + x0, tmp23, xmask) @triton.jit def triton_poi_fused_native_layer_norm_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_relu_threshold_backward_2(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) @triton.jit def triton_poi_fused_add_3(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x2, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (4,), (1,)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf1 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) get_raw_stream(0) triton_poi_fused_native_layer_norm_0[grid(64)](primals_3, buf0, buf1, 64, XBLOCK=64, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_1[grid(256)](primals_3, buf0, buf1, primals_1, primals_2, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf0 del buf1 del primals_1 del primals_2 buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf2, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf3) buf4 = reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf3 buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_relu_threshold_backward_2[grid(256)](buf4, primals_5, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf5 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf4, (64, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), out=buf5) buf6 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf5 triton_poi_fused_add_3[grid(256)](buf6, primals_7, primals_3, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_7 return buf6, primals_3, reinterpret_tensor(buf2, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf4, (64, 4), (4, 1), 0 ), primals_6, buf7, primals_4 class PositionwiseFeedForwardNew(nn.Module): """ A two-layer Feed-Forward-Network with residual layer norm. Args: d_model (int): the size of input for the first-layer of the FFN. d_ff (int): the hidden layer size of the second-layer of the FNN. dropout (float): dropout probability(0-1.0). """ def __init__(self, d_model, d_ff, dropout=0.1): super(PositionwiseFeedForwardNew, self).__init__() self.w_1 = nn.Linear(d_model, d_ff) self.w_2 = nn.Linear(d_ff, d_model) self.layer_norm = nn.LayerNorm(d_model, eps=1e-06) self.dropout_1 = nn.Dropout(dropout) self.relu = nn.ReLU() self.dropout_2 = nn.Dropout(dropout) def forward(self, input_0): primals_4 = self.w_1.weight primals_1 = self.w_1.bias primals_6 = self.w_2.weight primals_2 = self.w_2.bias primals_5 = self.layer_norm.weight primals_7 = self.layer_norm.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
Eldriann/Master-thesis
PositionwiseFeedForward
false
5,125
[ "MIT" ]
1
9d09d97f4002cc9fc730f10317614e1d0d307353
https://github.com/Eldriann/Master-thesis/tree/9d09d97f4002cc9fc730f10317614e1d0d307353
BartClassificationHead
import torch import torch.nn as nn import torch.utils.checkpoint class BartClassificationHead(nn.Module): """Head for sentence-level classification tasks.""" def __init__(self, input_dim: 'int', inner_dim: 'int', num_classes: 'int', pooler_dropout: 'float'): super().__init__() self.dense = nn.Linear(input_dim, inner_dim) self.dropout = nn.Dropout(p=pooler_dropout) self.out_proj = nn.Linear(inner_dim, num_classes) def forward(self, hidden_states: 'torch.Tensor'): hidden_states = self.dropout(hidden_states) hidden_states = self.dense(hidden_states) hidden_states = torch.tanh(hidden_states) hidden_states = self.dropout(hidden_states) hidden_states = self.out_proj(hidden_states) return hidden_states def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'inner_dim': 4, 'num_classes': 4, 'pooler_dropout': 0.5}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.utils.checkpoint assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_tanh_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = libdevice.tanh(tmp2) tl.store(in_out_ptr0 + x2, tmp3, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 get_raw_stream(0) triton_poi_fused_tanh_0[grid(256)](buf1, primals_3, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_3 buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf2) del primals_5 return reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), buf1, primals_4 class BartClassificationHeadNew(nn.Module): """Head for sentence-level classification tasks.""" def __init__(self, input_dim: 'int', inner_dim: 'int', num_classes: 'int', pooler_dropout: 'float'): super().__init__() self.dense = nn.Linear(input_dim, inner_dim) self.dropout = nn.Dropout(p=pooler_dropout) self.out_proj = nn.Linear(inner_dim, num_classes) def forward(self, input_0): primals_2 = self.dense.weight primals_3 = self.dense.bias primals_4 = self.out_proj.weight primals_5 = self.out_proj.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
Elvisambition/bert_seq2seq
BartClassificationHead
false
5,126
[ "Apache-2.0" ]
1
643ac537c16872f0d13200de06001d8201a54fbb
https://github.com/Elvisambition/bert_seq2seq/tree/643ac537c16872f0d13200de06001d8201a54fbb
DummyModelWithSharedSubmodule
import torch import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler import torch.onnx import torch.testing class DummyDenseWithRelu(nn.Module): def __init__(self, input_size, output_size, relu=None): super(DummyDenseWithRelu, self).__init__() self.input_size = input_size self.output_size = output_size self.relu = relu or nn.ReLU() self.linear = nn.Linear(input_size, output_size) def forward(self, x): return self.relu(self.linear(x)) class DummyModelWithSharedSubmodule(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(DummyModelWithSharedSubmodule, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.output_size = output_size self.dense1 = DummyDenseWithRelu(input_size, hidden_size) self.dense2 = DummyDenseWithRelu(hidden_size, output_size, self. dense1.relu) def forward(self, x): x = self.dense1(x) x = self.dense2(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'hidden_size': 4, 'output_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler import torch.onnx import torch.testing assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(256)](buf1, primals_2, buf5, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf2 buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_relu_threshold_backward_0[grid(256)](buf3, primals_5, buf4, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 return buf3, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 4), (4, 1), 0), buf4, primals_4, buf5 class DummyDenseWithRelu(nn.Module): def __init__(self, input_size, output_size, relu=None): super(DummyDenseWithRelu, self).__init__() self.input_size = input_size self.output_size = output_size self.relu = relu or nn.ReLU() self.linear = nn.Linear(input_size, output_size) def forward(self, x): return self.relu(self.linear(x)) class DummyModelWithSharedSubmoduleNew(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(DummyModelWithSharedSubmoduleNew, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.output_size = output_size self.dense1 = DummyDenseWithRelu(input_size, hidden_size) self.dense2 = DummyDenseWithRelu(hidden_size, output_size, self. dense1.relu) def forward(self, input_0): primals_1 = self.dense1.linear.weight primals_2 = self.dense1.linear.bias primals_4 = self.dense2.linear.weight primals_5 = self.dense2.linear.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
Emily0219/distiller
DummyModelWithSharedSubmodule
false
5,127
[ "Apache-2.0" ]
1
445ed35b671fb54586acc280b53d951f18bf97ae
https://github.com/Emily0219/distiller/tree/445ed35b671fb54586acc280b53d951f18bf97ae
DummyDenseWithRelu
import torch import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler import torch.onnx import torch.testing class DummyDenseWithRelu(nn.Module): def __init__(self, input_size, output_size, relu=None): super(DummyDenseWithRelu, self).__init__() self.input_size = input_size self.output_size = output_size self.relu = relu or nn.ReLU() self.linear = nn.Linear(input_size, output_size) def forward(self, x): return self.relu(self.linear(x)) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'output_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler import torch.onnx import torch.testing assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(256)](buf1, primals_2, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 return buf1, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), buf2 class DummyDenseWithReluNew(nn.Module): def __init__(self, input_size, output_size, relu=None): super(DummyDenseWithReluNew, self).__init__() self.input_size = input_size self.output_size = output_size self.relu = relu or nn.ReLU() self.linear = nn.Linear(input_size, output_size) def forward(self, input_0): primals_1 = self.linear.weight primals_2 = self.linear.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
Emily0219/distiller
DummyDenseWithRelu
false
5,128
[ "Apache-2.0" ]
1
445ed35b671fb54586acc280b53d951f18bf97ae
https://github.com/Emily0219/distiller/tree/445ed35b671fb54586acc280b53d951f18bf97ae
SelfAttention
import torch import torch.nn as nn import torch.distributed class SelfAttention(nn.Module): def __init__(self, model_dim, dropout=0.1): super(SelfAttention, self).__init__() self.Va = nn.Linear(model_dim, 1, bias=False) self.Wa = nn.Linear(model_dim, model_dim) self.dropout = nn.Dropout(dropout) def forward(self, x, mask=None): b, t, n = x.size() proj = torch.tanh(self.Wa(x.view(b * t, n).contiguous())) scores = self.Va(proj) scores = scores.view(b, t).contiguous() if mask is not None: scores = scores.masked_fill(mask, -1e+18) attn = torch.softmax(scores, -1) drop_attn = self.dropout(attn) context = torch.sum(drop_attn.unsqueeze(-1) * x, -2) return context, attn def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'model_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn import torch.distributed assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_tanh_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = libdevice.tanh(tmp2) tl.store(in_out_ptr0 + x2, tmp3, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_mul_sum_3(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 x0 = xindex % 4 x2 = xindex tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + (x0 + 16 * x1), xmask) tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (4 + x0 + 16 * x1), xmask) tmp7 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (8 + x0 + 16 * x1), xmask) tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (12 + x0 + 16 * x1), xmask) tmp2 = tmp0 * tmp1 tmp5 = tmp3 * tmp4 tmp6 = tmp2 + tmp5 tmp9 = tmp7 * tmp8 tmp10 = tmp6 + tmp9 tmp13 = tmp11 * tmp12 tmp14 = tmp10 + tmp13 tl.store(out_ptr0 + x2, tmp14, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (1, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_tanh_0[grid(64)](buf1, primals_3, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_3 buf2 = empty_strided_cuda((16, 1), (1, 1), torch.float32) extern_kernels.mm(buf1, reinterpret_tensor(primals_4, (4, 1), (1, 4 ), 0), out=buf2) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused__softmax_1[grid(16)](buf2, buf3, 16, XBLOCK=16, num_warps=1, num_stages=1) buf4 = reinterpret_tensor(buf2, (4, 4), (4, 1), 0) del buf2 triton_poi_fused__softmax_2[grid(16)](buf3, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1) buf5 = buf3 del buf3 triton_poi_fused_mul_sum_3[grid(16)](buf4, primals_1, buf5, 16, XBLOCK=16, num_warps=1, num_stages=1) return buf5, buf4, primals_1, buf1, buf4, primals_4 class SelfAttentionNew(nn.Module): def __init__(self, model_dim, dropout=0.1): super(SelfAttentionNew, self).__init__() self.Va = nn.Linear(model_dim, 1, bias=False) self.Wa = nn.Linear(model_dim, model_dim) self.dropout = nn.Dropout(dropout) def forward(self, input_0): primals_4 = self.Va.weight primals_2 = self.Wa.weight primals_3 = self.Wa.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0], output[1]
Eldriann/Master-thesis
SelfAttention
false
5,129
[ "MIT" ]
1
9d09d97f4002cc9fc730f10317614e1d0d307353
https://github.com/Eldriann/Master-thesis/tree/9d09d97f4002cc9fc730f10317614e1d0d307353
ContentLoss
import torch from torch import nn class ContentLoss(nn.Module): def __init__(self): super().__init__() def forward(self, x, y): return 0.5 * torch.sum((x - y) ** 2) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_mul_pow_sub_sum_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.load(in_ptr1 + r0, None) tmp2 = tmp0 - tmp1 tmp3 = tmp2 * tmp2 tmp4 = tl.broadcast_to(tmp3, [RBLOCK]) tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0)) tmp7 = 0.5 tmp8 = tmp6 * tmp7 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp8, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_mul_pow_sub_sum_0[grid(1)](buf1, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, class ContentLossNew(nn.Module): def __init__(self): super().__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
Enigmatisms/NeuralStyle
ContentLoss
false
5,130
[ "Apache-2.0" ]
1
27b435b5c51b41427e9f465793a0b81ad7248ab8
https://github.com/Enigmatisms/NeuralStyle/tree/27b435b5c51b41427e9f465793a0b81ad7248ab8
BahdanauAttention
import torch from torch import nn class BahdanauAttention(nn.Module): def __init__(self, dim): super(BahdanauAttention, self).__init__() self.query_layer = nn.Linear(dim, dim, bias=False) self.tanh = nn.Tanh() self.v = nn.Linear(dim, 1, bias=False) def forward(self, query, processed_memory): """ Args: query: (batch, 1, dim) or (batch, dim) processed_memory: (batch, max_time, dim) """ if query.dim() == 2: query = query.unsqueeze(1) processed_query = self.query_layer(query) alignment = self.v(self.tanh(processed_query + processed_memory)) return alignment.squeeze(-1) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_tanh_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask) tmp2 = tmp0 + tmp1 tmp3 = libdevice.tanh(tmp2) tl.store(in_out_ptr0 + x0, tmp3, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (1, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 get_raw_stream(0) triton_poi_fused_add_tanh_0[grid(256)](buf1, primals_3, 256, XBLOCK =128, num_warps=4, num_stages=1) del primals_3 buf2 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 1), (1, 4), 0), out=buf2) return reinterpret_tensor(buf2, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), buf1, primals_4 class BahdanauAttentionNew(nn.Module): def __init__(self, dim): super(BahdanauAttentionNew, self).__init__() self.query_layer = nn.Linear(dim, dim, bias=False) self.tanh = nn.Tanh() self.v = nn.Linear(dim, 1, bias=False) def forward(self, input_0, input_1): primals_2 = self.query_layer.weight primals_4 = self.v.weight primals_1 = input_0 primals_3 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
Emotional-Text-to-Speech/tacotron_pytorch
BahdanauAttention
false
5,131
[ "MIT" ]
1
e6b1a3907afb01fe31bcbd77c677667adf6733f5
https://github.com/Emotional-Text-to-Speech/tacotron_pytorch/tree/e6b1a3907afb01fe31bcbd77c677667adf6733f5
HILL
import torch import torch.nn as nn class HILL(nn.Module): def __init__(self, img_size): super(HILL, self).__init__() self.img_size = img_size self.pad_3 = nn.ReplicationPad2d(3) self.pad = nn.ReplicationPad2d(7) self.conv1 = nn.Conv2d(1, 1, 3, 1, padding=1, bias=False) self.avepool1 = nn.AvgPool2d(3, stride=1, padding=1) self.avepool2 = nn.AvgPool2d(15, stride=1) self.eps = 1e-10 self.res() def res(self): self.conv1.weight.data = torch.tensor([[-1, 2, -1], [2, -4, 2], [-1, 2, -1]], dtype=torch.float).view(1, 1, 3, 3) def forward(self, x): t1 = self.pad_3(x) t2 = self.conv1(t1) t3 = self.avepool1(torch.abs(t2)) t4 = 1 / (t3[:, :, 3:self.img_size + 3, 3:self.img_size + 3] + self.eps ) t5 = self.avepool2(self.pad(t4)) return t5 def get_inputs(): return [torch.rand([4, 1, 4, 4])] def get_init_inputs(): return [[], {'img_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_replication_pad2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 400 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 10 x1 = xindex // 10 % 10 x2 = xindex // 100 x3 = xindex tmp0 = tl.load(in_ptr0 + (4 * (3 * (3 <= 0 * (0 >= -3 + x1) + (-3 + x1) * (-3 + x1 > 0)) + (0 * (0 >= -3 + x1) + (-3 + x1) * (-3 + x1 > 0)) * (0 * (0 >= -3 + x1) + (-3 + x1) * (-3 + x1 > 0) < 3)) + 16 * x2 + ( 3 * (3 <= 0 * (0 >= -3 + x0) + (-3 + x0) * (-3 + x0 > 0)) + (0 * (0 >= -3 + x0) + (-3 + x0) * (-3 + x0 > 0)) * (0 * (0 >= -3 + x0) + (-3 + x0) * (-3 + x0 > 0) < 3))), xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_poi_fused_abs_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 400 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl_math.abs(tmp0) tl.store(out_ptr0 + x0, tmp1, xmask) @triton.jit def triton_poi_fused_avg_pool2d_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 400 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 10 % 10 x0 = xindex % 10 x4 = xindex tmp0 = -1 + x1 tmp1 = tl.full([1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.full([1], 10, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tmp2 & tmp4 tmp6 = -1 + x0 tmp7 = tmp6 >= tmp1 tmp8 = tmp6 < tmp3 tmp9 = tmp7 & tmp8 tmp10 = tmp5 & tmp9 tmp11 = tl.load(in_ptr0 + (-11 + x4), tmp10 & xmask, other=0.0) tmp12 = x0 tmp13 = tmp12 >= tmp1 tmp14 = tmp12 < tmp3 tmp15 = tmp13 & tmp14 tmp16 = tmp5 & tmp15 tmp17 = tl.load(in_ptr0 + (-10 + x4), tmp16 & xmask, other=0.0) tmp18 = tmp17 + tmp11 tmp19 = 1 + x0 tmp20 = tmp19 >= tmp1 tmp21 = tmp19 < tmp3 tmp22 = tmp20 & tmp21 tmp23 = tmp5 & tmp22 tmp24 = tl.load(in_ptr0 + (-9 + x4), tmp23 & xmask, other=0.0) tmp25 = tmp24 + tmp18 tmp26 = x1 tmp27 = tmp26 >= tmp1 tmp28 = tmp26 < tmp3 tmp29 = tmp27 & tmp28 tmp30 = tmp29 & tmp9 tmp31 = tl.load(in_ptr0 + (-1 + x4), tmp30 & xmask, other=0.0) tmp32 = tmp31 + tmp25 tmp33 = tmp29 & tmp15 tmp34 = tl.load(in_ptr0 + x4, tmp33 & xmask, other=0.0) tmp35 = tmp34 + tmp32 tmp36 = tmp29 & tmp22 tmp37 = tl.load(in_ptr0 + (1 + x4), tmp36 & xmask, other=0.0) tmp38 = tmp37 + tmp35 tmp39 = 1 + x1 tmp40 = tmp39 >= tmp1 tmp41 = tmp39 < tmp3 tmp42 = tmp40 & tmp41 tmp43 = tmp42 & tmp9 tmp44 = tl.load(in_ptr0 + (9 + x4), tmp43 & xmask, other=0.0) tmp45 = tmp44 + tmp38 tmp46 = tmp42 & tmp15 tmp47 = tl.load(in_ptr0 + (10 + x4), tmp46 & xmask, other=0.0) tmp48 = tmp47 + tmp45 tmp49 = tmp42 & tmp22 tmp50 = tl.load(in_ptr0 + (11 + x4), tmp49 & xmask, other=0.0) tmp51 = tmp50 + tmp48 tmp52 = 1 + -1 * x0 + -1 * x1 + x0 * x1 + (11 * (11 <= 2 + x0) + (2 + x0) * (2 + x0 < 11)) * (11 * (11 <= 2 + x1) + (2 + x1) * (2 + x1 < 11) ) + -1 * x0 * (11 * (11 <= 2 + x1) + (2 + x1) * (2 + x1 < 11) ) + -1 * x1 * (11 * (11 <= 2 + x0) + (2 + x0) * (2 + x0 < 11)) + ( 11 * (11 <= 2 + x0) + (2 + x0) * (2 + x0 < 11)) + (11 * (11 <= 2 + x1) + (2 + x1) * (2 + x1 < 11)) tmp53 = tmp51 / tmp52 tl.store(out_ptr0 + x4, tmp53, xmask) @triton.jit def triton_poi_fused_add_mul_reciprocal_3(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 % 4 x2 = xindex // 16 x3 = xindex tmp0 = tl.load(in_ptr0 + (33 + x0 + 10 * x1 + 100 * x2), xmask) tmp1 = 1e-10 tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 1, tl.int32) tmp4 = tmp3 / tmp2 tmp5 = 1.0 tmp6 = tmp4 * tmp5 tmp7 = tmp4 * tmp4 tl.store(out_ptr0 + x3, tmp6, xmask) tl.store(out_ptr1 + x3, tmp7, xmask) @triton.jit def triton_poi_fused_replication_pad2d_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1296 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 18 x1 = xindex // 18 % 18 x2 = xindex // 324 x3 = xindex tmp0 = tl.load(in_ptr0 + (4 * (3 * (3 <= 0 * (0 >= -7 + x1) + (-7 + x1) * (-7 + x1 > 0)) + (0 * (0 >= -7 + x1) + (-7 + x1) * (-7 + x1 > 0)) * (0 * (0 >= -7 + x1) + (-7 + x1) * (-7 + x1 > 0) < 3)) + 16 * x2 + ( 3 * (3 <= 0 * (0 >= -7 + x0) + (-7 + x0) * (-7 + x0 > 0)) + (0 * (0 >= -7 + x0) + (-7 + x0) * (-7 + x0 > 0)) * (0 * (0 >= -7 + x0) + (-7 + x0) * (-7 + x0 > 0) < 3))), xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x3, tmp0, xmask) def call(args): primals_1, primals_2 = args args.clear() assert_size_stride(primals_1, (4, 1, 4, 4), (16, 16, 4, 1)) assert_size_stride(primals_2, (1, 1, 3, 3), (9, 9, 3, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 1, 10, 10), (100, 100, 10, 1), torch. float32) get_raw_stream(0) triton_poi_fused_replication_pad2d_0[grid(400)](primals_1, buf0, 400, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 1, 10, 10), (100, 100, 10, 1)) buf2 = empty_strided_cuda((4, 1, 10, 10), (100, 100, 10, 1), torch. float32) triton_poi_fused_abs_1[grid(400)](buf1, buf2, 400, XBLOCK=256, num_warps=4, num_stages=1) buf3 = empty_strided_cuda((4, 1, 10, 10), (100, 400, 10, 1), torch. float32) triton_poi_fused_avg_pool2d_2[grid(400)](buf2, buf3, 400, XBLOCK= 128, num_warps=4, num_stages=1) buf4 = empty_strided_cuda((4, 1, 4, 4), (16, 16, 4, 1), torch.float32) buf8 = empty_strided_cuda((4, 1, 4, 4), (16, 16, 4, 1), torch.float32) triton_poi_fused_add_mul_reciprocal_3[grid(64)](buf3, buf4, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf3 buf5 = empty_strided_cuda((4, 1, 18, 18), (324, 324, 18, 1), torch. float32) triton_poi_fused_replication_pad2d_4[grid(1296)](buf4, buf5, 1296, XBLOCK=128, num_warps=4, num_stages=1) buf6 = torch.ops.aten.avg_pool2d.default(buf5, [15, 15], [1, 1], [0, 0], False, True, None) buf7 = buf6 del buf6 return buf7, primals_2, buf0, buf1, buf2, buf4, buf5, buf8 class HILLNew(nn.Module): def __init__(self, img_size): super(HILLNew, self).__init__() self.img_size = img_size self.pad_3 = nn.ReplicationPad2d(3) self.pad = nn.ReplicationPad2d(7) self.conv1 = nn.Conv2d(1, 1, 3, 1, padding=1, bias=False) self.avepool1 = nn.AvgPool2d(3, stride=1, padding=1) self.avepool2 = nn.AvgPool2d(15, stride=1) self.eps = 1e-10 self.res() def res(self): self.conv1.weight.data = torch.tensor([[-1, 2, -1], [2, -4, 2], [-1, 2, -1]], dtype=torch.float).view(1, 1, 3, 3) def forward(self, input_0): primals_2 = self.conv1.weight primals_1 = input_0 output = call([primals_1, primals_2]) return output[0]
Ekko-zn/StegoAdv
HILL
false
5,132
[ "MIT" ]
1
2852dbc85d66f30efb7127695c0d75806bf4aa4c
https://github.com/Ekko-zn/StegoAdv/tree/2852dbc85d66f30efb7127695c0d75806bf4aa4c
MultiHeadAttention
import torch import torch.nn as nn import torch.nn.functional as F import torch.nn.functional import torch.nn.parallel import torch.utils.data import torch.optim import torch.utils.data.distributed class MultiHeadAttention(nn.Module): """ input: query [N, T_q, query_dim] key [N, T_k, key_dim] output: out [N, T_q, num_units] """ def __init__(self, query_dim, key_dim, num_units, num_heads): super(MultiHeadAttention, self).__init__() if key_dim % num_heads != 0: raise ValueError( 'Key depth {} must be divisible by the number of attention heads {}' .format(key_dim, num_heads)) self.num_units = num_units self.num_heads = num_heads self.query_scale = (key_dim // num_heads) ** -0.5 self.query_linear = nn.Linear(query_dim, num_units, bias=False) self.key_linear = nn.Linear(key_dim, num_units, bias=False) self.value_linear = nn.Linear(key_dim, num_units, bias=False) def _split_heads(self, x): """ Split x such to add an extra num_heads dimension Input: x: a Tensor with shape [batch_size, seq_len, depth] Returns: A Tensor with shape [batch_size, num_heads, seq_len, depth / num_heads] """ if len(x.shape) != 3: raise ValueError('x must have rank 3') shape = x.shape return x.view(shape[0], shape[1], self.num_heads, shape[2] // self. num_heads).permute(0, 2, 1, 3) def _merge_heads(self, x): """ Merge the extra num_heads into the last dimension Input: x: a Tensor with shape [batch_size, num_heads, seq_len, depth/num_heads] Output: A Tensor with shape [batch_size, seq_len, depth] """ if len(x.shape) != 4: raise ValueError('x must have rank 4') shape = x.shape return x.permute(0, 2, 1, 3).contiguous().view(shape[0], shape[2], shape[3] * self.num_heads) def forward(self, queries, keys, values): queries = self.query_linear(queries) keys = self.key_linear(keys) values = self.value_linear(values) queries = self._split_heads(queries) keys = self._split_heads(keys) values = self._split_heads(values) queries *= self.query_scale logits = torch.matmul(queries, keys.permute(0, 1, 3, 2)) scores = F.softmax(logits, dim=-1) contexts = torch.matmul(scores, values) contexts = self._merge_heads(contexts) return contexts def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4]), torch.rand([4, 4, 4]) ] def get_init_inputs(): return [[], {'query_dim': 4, 'key_dim': 4, 'num_units': 4, 'num_heads': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn import torch.nn.functional import torch.nn.parallel import torch.utils.data import torch.optim import torch.utils.data.distributed assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clone_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = 1.0 tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask) @triton.jit def triton_poi_fused_clone_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask) @triton.jit def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_5, (4, 4), (4, 1)) assert_size_stride(primals_6, (4, 4, 4), (16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_2, (16, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_4, (16, 4), (4, 1), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf1) del primals_3 buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_6, (16, 4), (4, 1), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf2) del primals_5 buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clone_0[grid(16, 4)](buf0, buf3, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf4 = reinterpret_tensor(buf0, (4, 4, 1, 4), (16, 4, 4, 1), 0) del buf0 triton_poi_fused_clone_1[grid(16, 4)](buf1, buf4, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf5 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf3, (16, 4, 1), (4, 1, 0), 0), reinterpret_tensor(buf4, (16, 1, 4), (4, 0, 1), 0), out=buf5) buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__softmax_2[grid(256)](buf5, buf6, 256, XBLOCK=128, num_warps=4, num_stages=1) buf7 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf5 triton_poi_fused__softmax_3[grid(256)](buf6, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf6 buf8 = reinterpret_tensor(buf1, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf1 triton_poi_fused_clone_1[grid(16, 4)](buf2, buf8, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf9 = reinterpret_tensor(buf2, (16, 4, 1), (4, 1, 1), 0) del buf2 extern_kernels.bmm(reinterpret_tensor(buf7, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf8, (16, 4, 1), (4, 1, 0), 0), out=buf9) buf10 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) triton_poi_fused_clone_1[grid(16, 4)](buf9, buf10, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) del buf9 return reinterpret_tensor(buf10, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(primals_2, (16, 4), (4, 1), 0 ), reinterpret_tensor(primals_4, (16, 4), (4, 1), 0 ), reinterpret_tensor(primals_6, (16, 4), (4, 1), 0 ), buf7, reinterpret_tensor(buf8, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 4), 0) class MultiHeadAttentionNew(nn.Module): """ input: query [N, T_q, query_dim] key [N, T_k, key_dim] output: out [N, T_q, num_units] """ def __init__(self, query_dim, key_dim, num_units, num_heads): super(MultiHeadAttentionNew, self).__init__() if key_dim % num_heads != 0: raise ValueError( 'Key depth {} must be divisible by the number of attention heads {}' .format(key_dim, num_heads)) self.num_units = num_units self.num_heads = num_heads self.query_scale = (key_dim // num_heads) ** -0.5 self.query_linear = nn.Linear(query_dim, num_units, bias=False) self.key_linear = nn.Linear(key_dim, num_units, bias=False) self.value_linear = nn.Linear(key_dim, num_units, bias=False) def _split_heads(self, x): """ Split x such to add an extra num_heads dimension Input: x: a Tensor with shape [batch_size, seq_len, depth] Returns: A Tensor with shape [batch_size, num_heads, seq_len, depth / num_heads] """ if len(x.shape) != 3: raise ValueError('x must have rank 3') shape = x.shape return x.view(shape[0], shape[1], self.num_heads, shape[2] // self. num_heads).permute(0, 2, 1, 3) def _merge_heads(self, x): """ Merge the extra num_heads into the last dimension Input: x: a Tensor with shape [batch_size, num_heads, seq_len, depth/num_heads] Output: A Tensor with shape [batch_size, seq_len, depth] """ if len(x.shape) != 4: raise ValueError('x must have rank 4') shape = x.shape return x.permute(0, 2, 1, 3).contiguous().view(shape[0], shape[2], shape[3] * self.num_heads) def forward(self, input_0, input_1, input_2): primals_1 = self.query_linear.weight primals_3 = self.key_linear.weight primals_5 = self.value_linear.weight primals_2 = input_0 primals_4 = input_1 primals_6 = input_2 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6]) return output[0]
Ella77/tacotron2_multispeaker_pytorch
MultiHeadAttention
false
5,133
[ "BSD-3-Clause" ]
1
859eab0a8e3bd7545e623ce47fe1563702d38442
https://github.com/Ella77/tacotron2_multispeaker_pytorch/tree/859eab0a8e3bd7545e623ce47fe1563702d38442
Actor
import torch import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler import torch.onnx import torch.testing class Actor(nn.Module): def __init__(self, nb_states, nb_actions, hidden1=400, hidden2=300): super(Actor, self).__init__() self.fc1 = nn.Linear(nb_states, hidden1) self.fc2 = nn.Linear(hidden1, hidden2) self.fc3 = nn.Linear(hidden2, nb_actions) self.relu = nn.ReLU() self.sigmoid = nn.Sigmoid() def forward(self, x): out = self.fc1(x) out = self.relu(out) out = self.fc2(out) out = self.relu(out) out = self.fc3(out) out = self.sigmoid(out) return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'nb_states': 4, 'nb_actions': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler import torch.onnx import torch.testing assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 25600 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex x0 = xindex % 400 x2 = xindex % 1600 x3 = xindex // 1600 tmp0 = tl.load(in_out_ptr0 + x4, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x4, tmp4, xmask) tl.store(out_ptr0 + (x2 + 1664 * x3), tmp6, xmask) @triton.jit def triton_poi_fused_relu_threshold_backward_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 19200 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex x0 = xindex % 300 x2 = xindex // 1200 x3 = xindex % 1200 tmp0 = tl.load(in_ptr0 + x4, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(out_ptr0 + (x3 + 1216 * x2), tmp4, xmask) tl.store(out_ptr1 + (x3 + 1280 * x2), tmp6, xmask) @triton.jit def triton_poi_fused_relu_view_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 19200 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 300 x1 = xindex // 300 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 300 * (x1 % 4) + 1216 * (x1 // 4)), xmask) tl.store(out_ptr0 + x2, tmp0, xmask) @triton.jit def triton_poi_fused_sigmoid_3(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.sigmoid(tmp2) tl.store(in_out_ptr0 + x2, tmp3, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (400, 4), (4, 1)) assert_size_stride(primals_2, (400,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (300, 400), (400, 1)) assert_size_stride(primals_5, (300,), (1,)) assert_size_stride(primals_6, (4, 300), (300, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 400), (400, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 400), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 400), (6400, 1600, 400, 1), 0 ) del buf0 buf8 = empty_strided_cuda((4, 4, 4, 400), (6656, 1664, 400, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(25600)](buf1, primals_2, buf8, 25600, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 300), (300, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 400), (400, 1), 0), reinterpret_tensor(primals_4, (400, 300), (1, 400), 0), out=buf2) buf3 = empty_strided_cuda((4, 4, 4, 300), (4864, 1216, 300, 1), torch.float32) buf7 = empty_strided_cuda((4, 4, 4, 300), (5120, 1280, 300, 1), torch.bool) triton_poi_fused_relu_threshold_backward_1[grid(19200)](buf2, primals_5, buf3, buf7, 19200, XBLOCK=256, num_warps=4, num_stages=1 ) del primals_5 buf4 = buf2 del buf2 triton_poi_fused_relu_view_2[grid(19200)](buf3, buf4, 19200, XBLOCK =128, num_warps=4, num_stages=1) del buf3 buf5 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(buf4, reinterpret_tensor(primals_6, (300, 4), (1, 300), 0), out=buf5) buf6 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf5 triton_poi_fused_sigmoid_3[grid(256)](buf6, primals_7, 256, XBLOCK= 128, num_warps=4, num_stages=1) del primals_7 return buf6, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 400), (400, 1), 0 ), buf4, buf6, primals_6, buf7, primals_4, buf8 class ActorNew(nn.Module): def __init__(self, nb_states, nb_actions, hidden1=400, hidden2=300): super(ActorNew, self).__init__() self.fc1 = nn.Linear(nb_states, hidden1) self.fc2 = nn.Linear(hidden1, hidden2) self.fc3 = nn.Linear(hidden2, nb_actions) self.relu = nn.ReLU() self.sigmoid = nn.Sigmoid() def forward(self, input_0): primals_1 = self.fc1.weight primals_2 = self.fc1.bias primals_4 = self.fc2.weight primals_5 = self.fc2.bias primals_6 = self.fc3.weight primals_7 = self.fc3.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
Emily0219/distiller
Actor
false
5,134
[ "Apache-2.0" ]
1
445ed35b671fb54586acc280b53d951f18bf97ae
https://github.com/Emily0219/distiller/tree/445ed35b671fb54586acc280b53d951f18bf97ae
RBFExpansion
import torch import numpy as np import torch.nn as nn class RBFExpansion(nn.Module): """Expand distances between nodes by radial basis functions. .. math:: \\exp(- \\gamma * ||d - \\mu||^2) where :math:`d` is the distance between two nodes and :math:`\\mu` helps centralizes the distances. We use multiple centers evenly distributed in the range of :math:`[\\text{low}, \\text{high}]` with the difference between two adjacent centers being :math:`gap`. The number of centers is decided by :math:`(\\text{high} - \\text{low}) / \\text{gap}`. Choosing fewer centers corresponds to reducing the resolution of the filter. Parameters ---------- low : float Smallest center. Default to 0. high : float Largest center. Default to 30. gap : float Difference between two adjacent centers. :math:`\\gamma` will be computed as the reciprocal of gap. Default to 0.1. """ def __init__(self, low=0.0, high=30.0, gap=0.1): super(RBFExpansion, self).__init__() num_centers = int(np.ceil((high - low) / gap)) self.centers = np.linspace(low, high, num_centers) self.centers = nn.Parameter(torch.tensor(self.centers).float(), requires_grad=False) self.gamma = 1 / gap def reset_parameters(self): """Reinitialize model parameters.""" self.centers = nn.Parameter(torch.tensor(self.centers).float(), requires_grad=False) def forward(self, edge_dists): """Expand distances. Parameters ---------- edge_dists : float32 tensor of shape (E, 1) Distances between end nodes of edges, E for the number of edges. Returns ------- float32 tensor of shape (E, len(self.centers)) Expanded distances. """ radial = edge_dists - self.centers coef = -self.gamma return torch.exp(coef * radial ** 2) def get_inputs(): return [torch.rand([4, 4, 4, 300])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math import numpy as np import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_exp_mul_pow_sub_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 19200 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 300 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp3 = tmp2 * tmp2 tmp4 = -10.0 tmp5 = tmp3 * tmp4 tmp6 = tl_math.exp(tmp5) tl.store(out_ptr0 + x2, tmp6, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (300,), (1,)) assert_size_stride(arg1_1, (4, 4, 4, 300), (4800, 1200, 300, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 300), (4800, 1200, 300, 1), torch.float32) get_raw_stream(0) triton_poi_fused_exp_mul_pow_sub_0[grid(19200)](arg1_1, arg0_1, buf0, 19200, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf0, class RBFExpansionNew(nn.Module): """Expand distances between nodes by radial basis functions. .. math:: \\exp(- \\gamma * ||d - \\mu||^2) where :math:`d` is the distance between two nodes and :math:`\\mu` helps centralizes the distances. We use multiple centers evenly distributed in the range of :math:`[\\text{low}, \\text{high}]` with the difference between two adjacent centers being :math:`gap`. The number of centers is decided by :math:`(\\text{high} - \\text{low}) / \\text{gap}`. Choosing fewer centers corresponds to reducing the resolution of the filter. Parameters ---------- low : float Smallest center. Default to 0. high : float Largest center. Default to 30. gap : float Difference between two adjacent centers. :math:`\\gamma` will be computed as the reciprocal of gap. Default to 0.1. """ def __init__(self, low=0.0, high=30.0, gap=0.1): super(RBFExpansionNew, self).__init__() num_centers = int(np.ceil((high - low) / gap)) self.centers = np.linspace(low, high, num_centers) self.centers = nn.Parameter(torch.tensor(self.centers).float(), requires_grad=False) self.gamma = 1 / gap def reset_parameters(self): """Reinitialize model parameters.""" self.centers = nn.Parameter(torch.tensor(self.centers).float(), requires_grad=False) def forward(self, input_0): arg0_1 = self.centers arg1_1 = input_0 output = call([arg0_1, arg1_1]) return output[0]
Erfaan-Rostami/dgl-lifesci
RBFExpansion
false
5,135
[ "Apache-2.0" ]
1
08fc317f634fbaee4a8d074c332e871357845e4f
https://github.com/Erfaan-Rostami/dgl-lifesci/tree/08fc317f634fbaee4a8d074c332e871357845e4f
Highway
import torch from torch import nn class Highway(nn.Module): def __init__(self, in_size, out_size): super(Highway, self).__init__() self.H = nn.Linear(in_size, out_size) self.H.bias.data.zero_() self.T = nn.Linear(in_size, out_size) self.T.bias.data.fill_(-1) self.relu = nn.ReLU() self.sigmoid = nn.Sigmoid() def forward(self, inputs): H = self.relu(self.H(inputs)) T = self.sigmoid(self.T(inputs)) return H * T + inputs * (1.0 - T) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_size': 4, 'out_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_mul_relu_rsub_sigmoid_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp3 = tl.load(in_ptr1 + x0, xmask) tmp6 = tl.load(in_ptr2 + x0, xmask) tmp1 = tl.full([1], 0, tl.int32) tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = tl.sigmoid(tmp3) tmp5 = tmp2 * tmp4 tmp7 = 1.0 tmp8 = tmp7 - tmp4 tmp9 = tmp6 * tmp8 tmp10 = tmp5 + tmp9 tl.store(out_ptr0 + x0, tmp10, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf1) del primals_4 del primals_5 buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mul_relu_rsub_sigmoid_0[grid(256)](buf0, buf1, primals_3, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) return buf2, primals_3, buf0, buf1 class HighwayNew(nn.Module): def __init__(self, in_size, out_size): super(HighwayNew, self).__init__() self.H = nn.Linear(in_size, out_size) self.H.bias.data.zero_() self.T = nn.Linear(in_size, out_size) self.T.bias.data.fill_(-1) self.relu = nn.ReLU() self.sigmoid = nn.Sigmoid() def forward(self, input_0): primals_1 = self.H.weight primals_2 = self.H.bias primals_4 = self.T.weight primals_5 = self.T.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
Emotional-Text-to-Speech/tacotron_pytorch
Highway
false
5,136
[ "MIT" ]
1
e6b1a3907afb01fe31bcbd77c677667adf6733f5
https://github.com/Emotional-Text-to-Speech/tacotron_pytorch/tree/e6b1a3907afb01fe31bcbd77c677667adf6733f5
Norm
import torch import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler import torch.onnx import torch.testing class Norm(nn.Module): """ A module wrapper for vector/matrix norm """ def __init__(self, p='fro', dim=None, keepdim=False): super(Norm, self).__init__() self.p = p self.dim = dim self.keepdim = keepdim def forward(self, x: 'torch.Tensor'): return torch.norm(x, p=self.p, dim=self.dim, keepdim=self.keepdim) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler import torch.onnx import torch.testing assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_linalg_vector_norm_0(in_out_ptr0, in_ptr0, xnumel, rnumel ): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tmp0 * tmp0 tmp2 = tl.broadcast_to(tmp1, [RBLOCK]) tmp4 = triton_helpers.promote_to_tensor(tl.sum(tmp2, 0)) tmp5 = libdevice.sqrt(tmp4) tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp5, None) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_linalg_vector_norm_0[grid(1)](buf1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 return buf1, class NormNew(nn.Module): """ A module wrapper for vector/matrix norm """ def __init__(self, p='fro', dim=None, keepdim=False): super(NormNew, self).__init__() self.p = p self.dim = dim self.keepdim = keepdim def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
Emily0219/distiller
Norm
false
5,137
[ "Apache-2.0" ]
1
445ed35b671fb54586acc280b53d951f18bf97ae
https://github.com/Emily0219/distiller/tree/445ed35b671fb54586acc280b53d951f18bf97ae
ModelWithDuplicates
import torch from collections import OrderedDict import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler import torch.onnx import torch.testing class ModelWithDuplicates(nn.Module): def __init__(self): super(ModelWithDuplicates, self).__init__() self.conv1 = nn.Conv2d(3, 10, 5) self.post_conv1 = nn.ModuleList([nn.ReLU(), nn.Tanh()]) self.conv2 = nn.Conv2d(10, 20, 3) self.post_conv2 = self.post_conv1 self.expected_mlist_to_dmlist = OrderedDict([('post_conv1', [ 'post_conv1']), ('post_conv2', ['post_conv2'])]) self.expected_list_contents_name_changes = OrderedDict([( 'post_conv1.0', 'post_conv1_0'), ('post_conv1.1', 'post_conv1_1'), ('post_conv2.0', 'post_conv2_0'), ( 'post_conv2.1', 'post_conv2_1')]) def forward(self, x): x = self.conv1(x) for m in self.post_conv1: x = m(x) x = self.conv2(x) for m in self.post_conv2: x = m(x) return x def get_inputs(): return [torch.rand([4, 3, 64, 64])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice from collections import OrderedDict import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler import torch.onnx import torch.testing assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_convolution_relu_tanh_threshold_backward_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 144000 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 3600 % 10 x0 = xindex % 3600 x4 = xindex // 3600 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = libdevice.tanh(tmp4) tmp6 = 0.0 tmp7 = tmp4 <= tmp6 tl.store(out_ptr0 + x3, tmp5, xmask) tl.store(out_ptr1 + (x0 + 3712 * x4), tmp7, xmask) @triton.jit def triton_poi_fused_convolution_relu_tanh_threshold_backward_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 269120 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 3364 % 20 x0 = xindex % 3364 x4 = xindex // 3364 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = libdevice.tanh(tmp4) tmp6 = 0.0 tmp7 = tmp4 <= tmp6 tl.store(out_ptr0 + x3, tmp5, xmask) tl.store(out_ptr1 + (x0 + 3456 * x4), tmp7, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (10, 3, 5, 5), (75, 25, 5, 1)) assert_size_stride(primals_2, (10,), (1,)) assert_size_stride(primals_3, (4, 3, 64, 64), (12288, 4096, 64, 1)) assert_size_stride(primals_4, (20, 10, 3, 3), (90, 9, 3, 1)) assert_size_stride(primals_5, (20,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 10, 60, 60), (36000, 3600, 60, 1)) buf1 = empty_strided_cuda((4, 10, 60, 60), (36000, 3600, 60, 1), torch.float32) buf5 = empty_strided_cuda((4, 10, 60, 60), (37120, 3712, 60, 1), torch.bool) get_raw_stream(0) triton_poi_fused_convolution_relu_tanh_threshold_backward_0[grid( 144000)](buf0, primals_2, buf1, buf5, 144000, XBLOCK=512, num_warps=8, num_stages=1) del buf0 del primals_2 buf2 = extern_kernels.convolution(buf1, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 20, 58, 58), (67280, 3364, 58, 1)) buf3 = empty_strided_cuda((4, 20, 58, 58), (67280, 3364, 58, 1), torch.float32) buf4 = empty_strided_cuda((4, 20, 58, 58), (69120, 3456, 58, 1), torch.bool) triton_poi_fused_convolution_relu_tanh_threshold_backward_1[grid( 269120)](buf2, primals_5, buf3, buf4, 269120, XBLOCK=512, num_warps=8, num_stages=1) del buf2 del primals_5 return buf3, primals_1, primals_3, primals_4, buf1, buf3, buf4, buf5 class ModelWithDuplicatesNew(nn.Module): def __init__(self): super(ModelWithDuplicatesNew, self).__init__() self.conv1 = nn.Conv2d(3, 10, 5) self.post_conv1 = nn.ModuleList([nn.ReLU(), nn.Tanh()]) self.conv2 = nn.Conv2d(10, 20, 3) self.post_conv2 = self.post_conv1 self.expected_mlist_to_dmlist = OrderedDict([('post_conv1', [ 'post_conv1']), ('post_conv2', ['post_conv2'])]) self.expected_list_contents_name_changes = OrderedDict([( 'post_conv1.0', 'post_conv1_0'), ('post_conv1.1', 'post_conv1_1'), ('post_conv2.0', 'post_conv2_0'), ( 'post_conv2.1', 'post_conv2_1')]) def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
Emily0219/distiller
ModelWithDuplicates
false
5,138
[ "Apache-2.0" ]
1
445ed35b671fb54586acc280b53d951f18bf97ae
https://github.com/Emily0219/distiller/tree/445ed35b671fb54586acc280b53d951f18bf97ae
Mean
import torch import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler import torch.onnx import torch.testing class Mean(nn.Module): def __init__(self, *args, **kwargs): super(Mean, self).__init__() self.args = args self.kwargs = kwargs def forward(self, x: 'torch.Tensor'): return torch.mean(x, *self.args, **self.kwargs) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler import torch.onnx import torch.testing assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_mean_0(in_out_ptr0, in_ptr0, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.broadcast_to(tmp0, [RBLOCK]) tmp3 = triton_helpers.promote_to_tensor(tl.sum(tmp1, 0)) tmp4 = 256.0 tmp5 = tmp3 / tmp4 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp5, None) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_mean_0[grid(1)](buf1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 return buf1, class MeanNew(nn.Module): def __init__(self, *args, **kwargs): super(MeanNew, self).__init__() self.args = args self.kwargs = kwargs def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
Emily0219/distiller
Mean
false
5,139
[ "Apache-2.0" ]
1
445ed35b671fb54586acc280b53d951f18bf97ae
https://github.com/Emily0219/distiller/tree/445ed35b671fb54586acc280b53d951f18bf97ae
upsample
import torch import torch.nn as nn class upsample(nn.Module): def __init__(self): super(upsample, self).__init__() self.upsample = torch.nn.UpsamplingBilinear2d([256, 256]) def forward(self, input): return (self.upsample(input) + 1.0) / 2 def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused__to_copy__unsafe_index_add_arange_clamp_div_mul_sub_0( in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 256 % 256 x0 = xindex % 256 x2 = xindex // 65536 x3 = xindex tmp0 = x1 tmp1 = tmp0.to(tl.float32) tmp2 = 0.011764705882352941 tmp3 = tmp1 * tmp2 tmp4 = 0.0 tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp6 = tmp5.to(tl.int32) tmp7 = tl.full([1], 1, tl.int64) tmp8 = tmp6 + tmp7 tmp9 = tl.full([1], 3, tl.int64) tmp10 = triton_helpers.minimum(tmp8, tmp9) tmp11 = x0 tmp12 = tmp11.to(tl.float32) tmp13 = tmp12 * tmp2 tmp14 = triton_helpers.maximum(tmp13, tmp4) tmp15 = tmp14.to(tl.int32) tmp16 = tl.load(in_ptr0 + (tmp15 + 4 * tmp10 + 16 * x2), None, eviction_policy='evict_last') tmp17 = tmp15 + tmp7 tmp18 = triton_helpers.minimum(tmp17, tmp9) tmp19 = tl.load(in_ptr0 + (tmp18 + 4 * tmp10 + 16 * x2), None, eviction_policy='evict_last') tmp20 = tmp19 - tmp16 tmp21 = tmp15.to(tl.float32) tmp22 = tmp14 - tmp21 tmp23 = triton_helpers.maximum(tmp22, tmp4) tmp24 = 1.0 tmp25 = triton_helpers.minimum(tmp23, tmp24) tmp26 = tmp20 * tmp25 tmp27 = tmp16 + tmp26 tmp28 = tl.load(in_ptr0 + (tmp15 + 4 * tmp6 + 16 * x2), None, eviction_policy='evict_last') tmp29 = tl.load(in_ptr0 + (tmp18 + 4 * tmp6 + 16 * x2), None, eviction_policy='evict_last') tmp30 = tmp29 - tmp28 tmp31 = tmp30 * tmp25 tmp32 = tmp28 + tmp31 tmp33 = tmp27 - tmp32 tmp34 = tmp6.to(tl.float32) tmp35 = tmp5 - tmp34 tmp36 = triton_helpers.maximum(tmp35, tmp4) tmp37 = triton_helpers.minimum(tmp36, tmp24) tmp38 = tmp33 * tmp37 tmp39 = tmp32 + tmp38 tmp40 = tmp39 + tmp24 tmp41 = 0.5 tmp42 = tmp40 * tmp41 tl.store(in_out_ptr0 + x3, tmp42, None) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 256, 256), (262144, 65536, 256, 1), torch.float32) buf1 = buf0 del buf0 buf2 = buf1 del buf1 get_raw_stream(0) triton_poi_fused__to_copy__unsafe_index_add_arange_clamp_div_mul_sub_0[ grid(1048576)](buf2, arg0_1, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del arg0_1 return buf2, class upsampleNew(nn.Module): def __init__(self): super(upsampleNew, self).__init__() self.upsample = torch.nn.UpsamplingBilinear2d([256, 256]) def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
Euiyeon-Kim/SuperFAN-Pytorch
upsample
false
5,140
[ "MIT" ]
1
4a18e559c4b91d0d422b66e63509aeea8a7dc8f2
https://github.com/Euiyeon-Kim/SuperFAN-Pytorch/tree/4a18e559c4b91d0d422b66e63509aeea8a7dc8f2
ClippedLinearQuantization
import torch import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler import torch.onnx import torch.testing def linear_dequantize(input, scale, zero_point, inplace=False): if inplace: input.add_(zero_point).div_(scale) return input return (input + zero_point) / scale def linear_quantize(input, scale, zero_point, inplace=False): if inplace: input.mul_(scale).sub_(zero_point).round_() return input return torch.round(scale * input - zero_point) def _prep_saturation_val_tensor(sat_val): is_scalar = not isinstance(sat_val, torch.Tensor) out = torch.tensor(sat_val) if is_scalar else sat_val.clone().detach() if not out.is_floating_point(): out = out if out.dim() == 0: out = out.unsqueeze(0) return is_scalar, out def asymmetric_linear_quantization_params(num_bits, saturation_min, saturation_max, integral_zero_point=True, signed=False): scalar_min, sat_min = _prep_saturation_val_tensor(saturation_min) scalar_max, sat_max = _prep_saturation_val_tensor(saturation_max) is_scalar = scalar_min and scalar_max if scalar_max and not scalar_min: sat_max = sat_max elif scalar_min and not scalar_max: sat_min = sat_min if any(sat_min > sat_max): raise ValueError('saturation_min must be smaller than saturation_max') n = 2 ** num_bits - 1 sat_min = torch.min(sat_min, torch.zeros_like(sat_min)) sat_max = torch.max(sat_max, torch.zeros_like(sat_max)) diff = sat_max - sat_min diff[diff == 0] = n scale = n / diff zero_point = scale * sat_min if integral_zero_point: zero_point = zero_point.round() if signed: zero_point += 2 ** (num_bits - 1) if is_scalar: return scale.item(), zero_point.item() return scale, zero_point def clamp(input, min, max, inplace=False): if inplace: input.clamp_(min, max) return input return torch.clamp(input, min, max) class LinearQuantizeSTE(torch.autograd.Function): @staticmethod def forward(ctx, input, scale, zero_point, dequantize, inplace): if inplace: ctx.mark_dirty(input) output = linear_quantize(input, scale, zero_point, inplace) if dequantize: output = linear_dequantize(output, scale, zero_point, inplace) return output @staticmethod def backward(ctx, grad_output): return grad_output, None, None, None, None class ClippedLinearQuantization(nn.Module): def __init__(self, num_bits, clip_val, dequantize=True, inplace=False): super(ClippedLinearQuantization, self).__init__() self.num_bits = num_bits self.clip_val = clip_val self.scale, self.zero_point = asymmetric_linear_quantization_params( num_bits, 0, clip_val, signed=False) self.dequantize = dequantize self.inplace = inplace def forward(self, input): input = clamp(input, 0, self.clip_val, self.inplace) input = LinearQuantizeSTE.apply(input, self.scale, self.zero_point, self.dequantize, self.inplace) return input def __repr__(self): inplace_str = ', inplace' if self.inplace else '' return '{0}(num_bits={1}, clip_val={2}{3})'.format(self.__class__. __name__, self.num_bits, self.clip_val, inplace_str) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_bits': 4, 'clip_val': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler import torch.onnx import torch.testing assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_clamp_div_mul_round_sub_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.0 tmp2 = triton_helpers.maximum(tmp0, tmp1) tmp3 = 4.0 tmp4 = triton_helpers.minimum(tmp2, tmp3) tmp5 = 3.75 tmp6 = tmp4 * tmp5 tmp7 = tmp6 - tmp1 tmp8 = libdevice.nearbyint(tmp7) tmp9 = tmp8 + tmp1 tmp10 = 0.26666666666666666 tmp11 = tmp9 * tmp10 tl.store(out_ptr0 + x0, tmp11, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_clamp_div_mul_round_sub_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, def linear_dequantize(input, scale, zero_point, inplace=False): if inplace: input.add_(zero_point).div_(scale) return input return (input + zero_point) / scale def linear_quantize(input, scale, zero_point, inplace=False): if inplace: input.mul_(scale).sub_(zero_point).round_() return input return torch.round(scale * input - zero_point) def _prep_saturation_val_tensor(sat_val): is_scalar = not isinstance(sat_val, torch.Tensor) out = torch.tensor(sat_val) if is_scalar else sat_val.clone().detach() if not out.is_floating_point(): out = out if out.dim() == 0: out = out.unsqueeze(0) return is_scalar, out def asymmetric_linear_quantization_params(num_bits, saturation_min, saturation_max, integral_zero_point=True, signed=False): scalar_min, sat_min = _prep_saturation_val_tensor(saturation_min) scalar_max, sat_max = _prep_saturation_val_tensor(saturation_max) is_scalar = scalar_min and scalar_max if scalar_max and not scalar_min: sat_max = sat_max elif scalar_min and not scalar_max: sat_min = sat_min if any(sat_min > sat_max): raise ValueError('saturation_min must be smaller than saturation_max') n = 2 ** num_bits - 1 sat_min = torch.min(sat_min, torch.zeros_like(sat_min)) sat_max = torch.max(sat_max, torch.zeros_like(sat_max)) diff = sat_max - sat_min diff[diff == 0] = n scale = n / diff zero_point = scale * sat_min if integral_zero_point: zero_point = zero_point.round() if signed: zero_point += 2 ** (num_bits - 1) if is_scalar: return scale.item(), zero_point.item() return scale, zero_point def clamp(input, min, max, inplace=False): if inplace: input.clamp_(min, max) return input return torch.clamp(input, min, max) class LinearQuantizeSTE(torch.autograd.Function): @staticmethod def forward(ctx, input, scale, zero_point, dequantize, inplace): if inplace: ctx.mark_dirty(input) output = linear_quantize(input, scale, zero_point, inplace) if dequantize: output = linear_dequantize(output, scale, zero_point, inplace) return output @staticmethod def backward(ctx, grad_output): return grad_output, None, None, None, None class ClippedLinearQuantizationNew(nn.Module): def __init__(self, num_bits, clip_val, dequantize=True, inplace=False): super(ClippedLinearQuantizationNew, self).__init__() self.num_bits = num_bits self.clip_val = clip_val self.scale, self.zero_point = asymmetric_linear_quantization_params( num_bits, 0, clip_val, signed=False) self.dequantize = dequantize self.inplace = inplace def __repr__(self): inplace_str = ', inplace' if self.inplace else '' return '{0}(num_bits={1}, clip_val={2}{3})'.format(self.__class__. __name__, self.num_bits, self.clip_val, inplace_str) def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
Emily0219/distiller
ClippedLinearQuantization
false
5,141
[ "Apache-2.0" ]
1
445ed35b671fb54586acc280b53d951f18bf97ae
https://github.com/Emily0219/distiller/tree/445ed35b671fb54586acc280b53d951f18bf97ae
BahdanauAttention
import math import torch import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler from torch.nn.parameter import Parameter import torch.onnx import torch.testing class EltwiseAdd(nn.Module): def __init__(self, inplace=False): super(EltwiseAdd, self).__init__() self.inplace = inplace def forward(self, *input): res = input[0] if self.inplace: for t in input[1:]: res += t else: for t in input[1:]: res = res + t return res class EltwiseMult(nn.Module): def __init__(self, inplace=False): super(EltwiseMult, self).__init__() self.inplace = inplace def forward(self, *input): res = input[0] if self.inplace: for t in input[1:]: res *= t else: for t in input[1:]: res = res * t return res class Matmul(nn.Module): """ A wrapper module for matmul operation between 2 tensors. """ def __init__(self): super(Matmul, self).__init__() def forward(self, a: 'torch.Tensor', b: 'torch.Tensor'): return a.matmul(b) class BatchMatmul(nn.Module): """ A wrapper module for torch.bmm operation between 2 tensors. """ def __init__(self): super(BatchMatmul, self).__init__() def forward(self, a: 'torch.Tensor', b: 'torch.Tensor'): return torch.bmm(a, b) class BahdanauAttention(nn.Module): """ It should be very similar to tf.contrib.seq2seq.BahdanauAttention """ def __init__(self, query_size, key_size, num_units, normalize=False, dropout=0, batch_first=False): super(BahdanauAttention, self).__init__() self.normalize = normalize self.batch_first = batch_first self.num_units = num_units self.linear_q = nn.Linear(query_size, num_units, bias=False) self.linear_k = nn.Linear(key_size, num_units, bias=False) self.linear_att = Parameter(torch.Tensor(num_units)) self.dropout = nn.Dropout(dropout) self.mask = None self.eltwiseadd_qk = EltwiseAdd() self.eltwiseadd_norm_bias = EltwiseAdd() self.eltwisemul_norm_scaler = EltwiseMult() self.tanh = nn.Tanh() self.matmul_score = Matmul() self.softmax_att = nn.Softmax(dim=-1) self.context_matmul = BatchMatmul() if self.normalize: self.normalize_scalar = Parameter(torch.Tensor(1)) self.normalize_bias = Parameter(torch.Tensor(num_units)) else: self.register_parameter('normalize_scalar', None) self.register_parameter('normalize_bias', None) self.reset_parameters() def reset_parameters(self): stdv = 1.0 / math.sqrt(self.num_units) self.linear_att.data.uniform_(-stdv, stdv) if self.normalize: self.normalize_scalar.data.fill_(stdv) self.normalize_bias.data.zero_() def set_mask(self, context_len, context): """ sets self.mask which is applied before softmax ones for inactive context fields, zeros for active context fields :param context_len: b :param context: if batch_first: (b x t_k x n) else: (t_k x b x n) self.mask: (b x t_k) """ if self.batch_first: max_len = context.size(1) else: max_len = context.size(0) indices = torch.arange(0, max_len, dtype=torch.int64, device= context.device) self.mask = indices >= context_len.unsqueeze(1) def calc_score(self, att_query, att_keys): """ Calculate Bahdanau score :param att_query: b x t_q x n :param att_keys: b x t_k x n return b x t_q x t_k scores """ b, t_k, n = att_keys.size() t_q = att_query.size(1) att_query = att_query.unsqueeze(2).expand(b, t_q, t_k, n) att_keys = att_keys.unsqueeze(1).expand(b, t_q, t_k, n) sum_qk = self.eltwiseadd_qk(att_query, att_keys) if self.normalize: sum_qk = self.eltwiseadd_norm_bias(sum_qk, self.normalize_bias) tmp = self.linear_att linear_att = tmp / tmp.norm() linear_att = linear_att linear_att = self.eltwisemul_norm_scaler(linear_att, self. normalize_scalar) else: linear_att = self.linear_att out = self.matmul_score(self.tanh(sum_qk), linear_att) return out def forward(self, query, keys): """ :param query: if batch_first: (b x t_q x n) else: (t_q x b x n) :param keys: if batch_first: (b x t_k x n) else (t_k x b x n) :returns: (context, scores_normalized) context: if batch_first: (b x t_q x n) else (t_q x b x n) scores_normalized: if batch_first (b x t_q x t_k) else (t_q x b x t_k) """ if not self.batch_first: keys = keys.transpose(0, 1) if query.dim() == 3: query = query.transpose(0, 1) if query.dim() == 2: single_query = True query = query.unsqueeze(1) else: single_query = False b = query.size(0) t_k = keys.size(1) t_q = query.size(1) processed_query = self.linear_q(query) processed_key = self.linear_k(keys) scores = self.calc_score(processed_query, processed_key) if self.mask is not None: mask = self.mask.unsqueeze(1).expand(b, t_q, t_k) scores.data.masked_fill_(mask, -65504.0) scores_normalized = self.softmax_att(scores) scores_normalized = self.dropout(scores_normalized) context = self.context_matmul(scores_normalized, keys) if single_query: context = context.squeeze(1) scores_normalized = scores_normalized.squeeze(1) elif not self.batch_first: context = context.transpose(0, 1) scores_normalized = scores_normalized.transpose(0, 1) return context, scores_normalized def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'query_size': 4, 'key_size': 4, 'num_units': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import math import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data from torch.optim.lr_scheduler import * import torch.optim.lr_scheduler from torch.nn.parameter import Parameter import torch.onnx import torch.testing assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clone_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 % 4 x2 = xindex // 16 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1), xmask) tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_poi_fused_mv_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * (x0 // 4), xmask, eviction_policy='evict_last' ) tmp1 = tl.load(in_ptr1 + (4 * (x0 % 4) + 16 * (x0 // 16)), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr2 + 0) tmp5 = tl.broadcast_to(tmp4, [XBLOCK]) tmp7 = tl.load(in_ptr0 + (1 + 4 * (x0 // 4)), xmask, eviction_policy= 'evict_last') tmp8 = tl.load(in_ptr1 + (1 + 4 * (x0 % 4) + 16 * (x0 // 16)), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr2 + 1) tmp12 = tl.broadcast_to(tmp11, [XBLOCK]) tmp15 = tl.load(in_ptr0 + (2 + 4 * (x0 // 4)), xmask, eviction_policy= 'evict_last') tmp16 = tl.load(in_ptr1 + (2 + 4 * (x0 % 4) + 16 * (x0 // 16)), xmask, eviction_policy='evict_last') tmp19 = tl.load(in_ptr2 + 2) tmp20 = tl.broadcast_to(tmp19, [XBLOCK]) tmp23 = tl.load(in_ptr0 + (3 + 4 * (x0 // 4)), xmask, eviction_policy= 'evict_last') tmp24 = tl.load(in_ptr1 + (3 + 4 * (x0 % 4) + 16 * (x0 // 16)), xmask, eviction_policy='evict_last') tmp27 = tl.load(in_ptr2 + 3) tmp28 = tl.broadcast_to(tmp27, [XBLOCK]) tmp2 = tmp0 + tmp1 tmp3 = libdevice.tanh(tmp2) tmp6 = tmp3 * tmp5 tmp9 = tmp7 + tmp8 tmp10 = libdevice.tanh(tmp9) tmp13 = tmp10 * tmp12 tmp14 = tmp6 + tmp13 tmp17 = tmp15 + tmp16 tmp18 = libdevice.tanh(tmp17) tmp21 = tmp18 * tmp20 tmp22 = tmp14 + tmp21 tmp25 = tmp23 + tmp24 tmp26 = libdevice.tanh(tmp25) tmp29 = tmp26 * tmp28 tmp30 = tmp22 + tmp29 tl.store(out_ptr0 + x0, tmp30, xmask) @triton.jit def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clone_0[grid(64)](primals_2, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_2 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf1) del primals_3 buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_clone_0[grid(64)](primals_1, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) buf3 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf2, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf3) del primals_4 buf4 = empty_strided_cuda((64,), (1,), torch.float32) triton_poi_fused_mv_1[grid(64)](buf1, buf3, primals_5, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_2[grid(64)](buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf6 = reinterpret_tensor(buf4, (4, 4, 4), (16, 4, 1), 0) del buf4 triton_poi_fused__softmax_3[grid(64)](buf5, buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) buf7 = buf5 del buf5 extern_kernels.bmm(buf6, reinterpret_tensor(primals_1, (4, 4, 4), ( 4, 16, 1), 0), out=buf7) return reinterpret_tensor(buf7, (4, 4, 4), (4, 16, 1), 0 ), reinterpret_tensor(buf6, (4, 4, 4), (4, 16, 1), 0 ), primals_5, reinterpret_tensor(buf0, (16, 4), (4, 1), 0 ), buf1, reinterpret_tensor(buf2, (16, 4), (4, 1), 0 ), buf3, buf6, reinterpret_tensor(primals_1, (4, 4, 4), (4, 1, 16), 0) class EltwiseAdd(nn.Module): def __init__(self, inplace=False): super(EltwiseAdd, self).__init__() self.inplace = inplace def forward(self, *input): res = input[0] if self.inplace: for t in input[1:]: res += t else: for t in input[1:]: res = res + t return res class EltwiseMult(nn.Module): def __init__(self, inplace=False): super(EltwiseMult, self).__init__() self.inplace = inplace def forward(self, *input): res = input[0] if self.inplace: for t in input[1:]: res *= t else: for t in input[1:]: res = res * t return res class Matmul(nn.Module): """ A wrapper module for matmul operation between 2 tensors. """ def __init__(self): super(Matmul, self).__init__() def forward(self, a: 'torch.Tensor', b: 'torch.Tensor'): return a.matmul(b) class BatchMatmul(nn.Module): """ A wrapper module for torch.bmm operation between 2 tensors. """ def __init__(self): super(BatchMatmul, self).__init__() def forward(self, a: 'torch.Tensor', b: 'torch.Tensor'): return torch.bmm(a, b) class BahdanauAttentionNew(nn.Module): """ It should be very similar to tf.contrib.seq2seq.BahdanauAttention """ def __init__(self, query_size, key_size, num_units, normalize=False, dropout=0, batch_first=False): super(BahdanauAttentionNew, self).__init__() self.normalize = normalize self.batch_first = batch_first self.num_units = num_units self.linear_q = nn.Linear(query_size, num_units, bias=False) self.linear_k = nn.Linear(key_size, num_units, bias=False) self.linear_att = Parameter(torch.Tensor(num_units)) self.dropout = nn.Dropout(dropout) self.mask = None self.eltwiseadd_qk = EltwiseAdd() self.eltwiseadd_norm_bias = EltwiseAdd() self.eltwisemul_norm_scaler = EltwiseMult() self.tanh = nn.Tanh() self.matmul_score = Matmul() self.softmax_att = nn.Softmax(dim=-1) self.context_matmul = BatchMatmul() if self.normalize: self.normalize_scalar = Parameter(torch.Tensor(1)) self.normalize_bias = Parameter(torch.Tensor(num_units)) else: self.register_parameter('normalize_scalar', None) self.register_parameter('normalize_bias', None) self.reset_parameters() def reset_parameters(self): stdv = 1.0 / math.sqrt(self.num_units) self.linear_att.data.uniform_(-stdv, stdv) if self.normalize: self.normalize_scalar.data.fill_(stdv) self.normalize_bias.data.zero_() def set_mask(self, context_len, context): """ sets self.mask which is applied before softmax ones for inactive context fields, zeros for active context fields :param context_len: b :param context: if batch_first: (b x t_k x n) else: (t_k x b x n) self.mask: (b x t_k) """ if self.batch_first: max_len = context.size(1) else: max_len = context.size(0) indices = torch.arange(0, max_len, dtype=torch.int64, device= context.device) self.mask = indices >= context_len.unsqueeze(1) def calc_score(self, att_query, att_keys): """ Calculate Bahdanau score :param att_query: b x t_q x n :param att_keys: b x t_k x n return b x t_q x t_k scores """ b, t_k, n = att_keys.size() t_q = att_query.size(1) att_query = att_query.unsqueeze(2).expand(b, t_q, t_k, n) att_keys = att_keys.unsqueeze(1).expand(b, t_q, t_k, n) sum_qk = self.eltwiseadd_qk(att_query, att_keys) if self.normalize: sum_qk = self.eltwiseadd_norm_bias(sum_qk, self.normalize_bias) tmp = self.linear_att linear_att = tmp / tmp.norm() linear_att = linear_att linear_att = self.eltwisemul_norm_scaler(linear_att, self. normalize_scalar) else: linear_att = self.linear_att out = self.matmul_score(self.tanh(sum_qk), linear_att) return out def forward(self, input_0, input_1): primals_5 = self.linear_att primals_3 = self.linear_q.weight primals_4 = self.linear_k.weight primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0], output[1]
Emily0219/distiller
BahdanauAttention
false
5,142
[ "Apache-2.0" ]
1
445ed35b671fb54586acc280b53d951f18bf97ae
https://github.com/Emily0219/distiller/tree/445ed35b671fb54586acc280b53d951f18bf97ae
SelfAttention
import torch import torch.nn.functional as F from torch import nn class SelfAttention(nn.Module): def __init__(self, embedding_dimension, num_heads): super().__init__() assert embedding_dimension % num_heads == 0, f'embedding dimension must be divisible by number of heads, got embedding_dimension={embedding_dimension!r}, num_heads={num_heads!r}' self.num_heads = num_heads k = embedding_dimension self.to_keys = nn.Linear(k, k * num_heads, bias=False) self.to_queries = nn.Linear(k, k * num_heads, bias=False) self.to_values = nn.Linear(k, k * num_heads, bias=False) self.unify_heads = nn.Linear(num_heads * k, k) def forward(self, x): b, t, k = x.size() h = self.num_heads keys = self.to_keys(x).view(b, t, h, k) queries = self.to_queries(x).view(b, t, h, k) values = self.to_values(x).view(b, t, h, k) keys = keys.transpose(1, 2).contiguous().view(b * h, t, k) queries = queries.transpose(1, 2).contiguous().view(b * h, t, k) values = values.transpose(1, 2).contiguous().view(b * h, t, k) dot = torch.bmm(queries, keys.transpose(1, 2)) dot = dot / k ** (1 / 2) dot = F.softmax(dot, dim=2) out = torch.bmm(dot, values).view(b, h, t, k) out = out.transpose(1, 2).contiguous().view(b, t, h * k) return self.unify_heads(out) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'embedding_dimension': 4, 'num_heads': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clone_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 % 4 x2 = xindex // 16 % 4 x3 = xindex // 64 x4 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1 + 64 * x3), xmask) tl.store(out_ptr0 + x4, tmp0, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp3 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp1 = 1.0 tmp2 = tmp0 * tmp1 tmp4 = tmp3 * tmp1 tmp6 = tmp5 * tmp1 tmp7 = triton_helpers.maximum(tmp4, tmp6) tmp9 = tmp8 * tmp1 tmp10 = triton_helpers.maximum(tmp7, tmp9) tmp12 = tmp11 * tmp1 tmp13 = triton_helpers.maximum(tmp10, tmp12) tmp14 = tmp2 - tmp13 tmp15 = 0.5 tmp16 = tmp14 * tmp15 tmp17 = tl_math.exp(tmp16) tl.store(out_ptr0 + x2, tmp17, xmask) @triton.jit def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (16, 4), (4, 1)) assert_size_stride(primals_3, (16, 4), (4, 1)) assert_size_stride(primals_4, (16, 4), (4, 1)) assert_size_stride(primals_5, (4, 16), (16, 1)) assert_size_stride(primals_6, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 16), (16, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 16), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((16, 16), (16, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_3, (4, 16), (1, 4), 0), out=buf1) del primals_3 buf2 = empty_strided_cuda((16, 16), (16, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 16), (1, 4), 0), out=buf2) del primals_4 buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clone_0[grid(256)](buf1, buf3, 256, XBLOCK=128, num_warps=4, num_stages=1) buf4 = reinterpret_tensor(buf1, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf1 triton_poi_fused_clone_0[grid(256)](buf0, buf4, 256, XBLOCK=128, num_warps=4, num_stages=1) buf5 = reinterpret_tensor(buf0, (16, 4, 4), (16, 4, 1), 0) del buf0 extern_kernels.bmm(reinterpret_tensor(buf3, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf4, (16, 4, 4), (16, 1, 4), 0), out=buf5) buf6 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_1[grid(256)](buf5, buf6, 256, XBLOCK=256, num_warps=4, num_stages=1) buf7 = buf5 del buf5 triton_poi_fused__softmax_2[grid(256)](buf6, buf7, 256, XBLOCK=256, num_warps=4, num_stages=1) buf8 = reinterpret_tensor(buf6, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf6 triton_poi_fused_clone_0[grid(256)](buf2, buf8, 256, XBLOCK=128, num_warps=4, num_stages=1) buf9 = reinterpret_tensor(buf2, (16, 4, 4), (16, 4, 1), 0) del buf2 extern_kernels.bmm(buf7, reinterpret_tensor(buf8, (16, 4, 4), (16, 4, 1), 0), out=buf9) buf10 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_clone_0[grid(256)](buf9, buf10, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf9 buf11 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_6, reinterpret_tensor(buf10, (16, 16), (16, 1), 0), reinterpret_tensor(primals_5, (16, 4), (1, 16), 0), alpha=1, beta=1, out=buf11) del primals_6 return reinterpret_tensor(buf11, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(primals_1, (16, 4), (4, 1), 0 ), buf7, reinterpret_tensor(buf10, (16, 16), (16, 1), 0 ), primals_5, reinterpret_tensor(buf8, (16, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf3, (16, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf4, (16, 4, 4), (16, 4, 1), 0) class SelfAttentionNew(nn.Module): def __init__(self, embedding_dimension, num_heads): super().__init__() assert embedding_dimension % num_heads == 0, f'embedding dimension must be divisible by number of heads, got embedding_dimension={embedding_dimension!r}, num_heads={num_heads!r}' self.num_heads = num_heads k = embedding_dimension self.to_keys = nn.Linear(k, k * num_heads, bias=False) self.to_queries = nn.Linear(k, k * num_heads, bias=False) self.to_values = nn.Linear(k, k * num_heads, bias=False) self.unify_heads = nn.Linear(num_heads * k, k) def forward(self, input_0): primals_2 = self.to_keys.weight primals_3 = self.to_queries.weight primals_4 = self.to_values.weight primals_5 = self.unify_heads.weight primals_6 = self.unify_heads.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6]) return output[0]
Ensembl/gene_pcp
SelfAttention
false
5,143
[ "Apache-2.0" ]
1
121be9895d414da3f13b5c8ec7588754e03336e1
https://github.com/Ensembl/gene_pcp/tree/121be9895d414da3f13b5c8ec7588754e03336e1
AffineChannel2d
import torch import torch.nn as nn class AffineChannel2d(nn.Module): """ A simple channel-wise affine transformation operation """ def __init__(self, num_features): super().__init__() self.num_features = num_features self.weight = nn.Parameter(torch.Tensor(num_features)) self.bias = nn.Parameter(torch.Tensor(num_features)) self.weight.data.uniform_() self.bias.data.zero_() def forward(self, x): return x * self.weight.view(1, self.num_features, 1, 1 ) + self.bias.view(1, self.num_features, 1, 1) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_features': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_mul_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 * tmp1 tmp4 = tmp2 + tmp3 tl.store(out_ptr0 + x3, tmp4, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4,), (1,)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mul_0[grid(256)](primals_2, primals_1, primals_3, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 del primals_3 return buf0, primals_2 class AffineChannel2dNew(nn.Module): """ A simple channel-wise affine transformation operation """ def __init__(self, num_features): super().__init__() self.num_features = num_features self.weight = nn.Parameter(torch.Tensor(num_features)) self.bias = nn.Parameter(torch.Tensor(num_features)) self.weight.data.uniform_() self.bias.data.zero_() def forward(self, input_0): primals_1 = self.weight primals_3 = self.bias primals_2 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
FVL2020/2DImage2BMI
AffineChannel2d
false
5,144
[ "MIT" ]
1
90783bcb6fce0b91fb5ab70f62f595e3cfff39d0
https://github.com/FVL2020/2DImage2BMI/tree/90783bcb6fce0b91fb5ab70f62f595e3cfff39d0
FactorizationMachine
import torch import torch.utils.data class FactorizationMachine(torch.nn.Module): def __init__(self, reduce_sum=True): super().__init__() self.reduce_sum = reduce_sum def forward(self, x): """ :param x: Float tensor of size ``(batch_size, num_fields, embed_dim)`` """ square_of_sum = torch.sum(x, dim=1) ** 2 sum_of_square = torch.sum(x ** 2, dim=1) ix = square_of_sum - sum_of_square if self.reduce_sum: ix = torch.sum(ix, dim=1, keepdim=True) return 0.5 * ix def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_mul_pow_sub_sum_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask) tmp1 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask) tmp3 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask) tmp5 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask) tmp16 = tl.load(in_ptr0 + (4 + x0 + 64 * x1), xmask) tmp17 = tl.load(in_ptr0 + (20 + x0 + 64 * x1), xmask) tmp19 = tl.load(in_ptr0 + (36 + x0 + 64 * x1), xmask) tmp21 = tl.load(in_ptr0 + (52 + x0 + 64 * x1), xmask) tmp33 = tl.load(in_ptr0 + (8 + x0 + 64 * x1), xmask) tmp34 = tl.load(in_ptr0 + (24 + x0 + 64 * x1), xmask) tmp36 = tl.load(in_ptr0 + (40 + x0 + 64 * x1), xmask) tmp38 = tl.load(in_ptr0 + (56 + x0 + 64 * x1), xmask) tmp50 = tl.load(in_ptr0 + (12 + x0 + 64 * x1), xmask) tmp51 = tl.load(in_ptr0 + (28 + x0 + 64 * x1), xmask) tmp53 = tl.load(in_ptr0 + (44 + x0 + 64 * x1), xmask) tmp55 = tl.load(in_ptr0 + (60 + x0 + 64 * x1), xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = tmp6 * tmp6 tmp8 = tmp0 * tmp0 tmp9 = tmp1 * tmp1 tmp10 = tmp8 + tmp9 tmp11 = tmp3 * tmp3 tmp12 = tmp10 + tmp11 tmp13 = tmp5 * tmp5 tmp14 = tmp12 + tmp13 tmp15 = tmp7 - tmp14 tmp18 = tmp16 + tmp17 tmp20 = tmp18 + tmp19 tmp22 = tmp20 + tmp21 tmp23 = tmp22 * tmp22 tmp24 = tmp16 * tmp16 tmp25 = tmp17 * tmp17 tmp26 = tmp24 + tmp25 tmp27 = tmp19 * tmp19 tmp28 = tmp26 + tmp27 tmp29 = tmp21 * tmp21 tmp30 = tmp28 + tmp29 tmp31 = tmp23 - tmp30 tmp32 = tmp15 + tmp31 tmp35 = tmp33 + tmp34 tmp37 = tmp35 + tmp36 tmp39 = tmp37 + tmp38 tmp40 = tmp39 * tmp39 tmp41 = tmp33 * tmp33 tmp42 = tmp34 * tmp34 tmp43 = tmp41 + tmp42 tmp44 = tmp36 * tmp36 tmp45 = tmp43 + tmp44 tmp46 = tmp38 * tmp38 tmp47 = tmp45 + tmp46 tmp48 = tmp40 - tmp47 tmp49 = tmp32 + tmp48 tmp52 = tmp50 + tmp51 tmp54 = tmp52 + tmp53 tmp56 = tmp54 + tmp55 tmp57 = tmp56 * tmp56 tmp58 = tmp50 * tmp50 tmp59 = tmp51 * tmp51 tmp60 = tmp58 + tmp59 tmp61 = tmp53 * tmp53 tmp62 = tmp60 + tmp61 tmp63 = tmp55 * tmp55 tmp64 = tmp62 + tmp63 tmp65 = tmp57 - tmp64 tmp66 = tmp49 + tmp65 tmp67 = 0.5 tmp68 = tmp66 * tmp67 tl.store(in_out_ptr0 + x2, tmp68, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 1, 4), (4, 16, 1), torch.float32) buf1 = reinterpret_tensor(buf0, (4, 1, 4), (4, 4, 1), 0) del buf0 get_raw_stream(0) triton_poi_fused_mul_pow_sub_sum_0[grid(16)](buf1, arg0_1, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg0_1 return buf1, class FactorizationMachineNew(torch.nn.Module): def __init__(self, reduce_sum=True): super().__init__() self.reduce_sum = reduce_sum def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
Fanxingye/Autotabular
FactorizationMachine
false
5,145
[ "Apache-2.0" ]
1
d630c78290a52f8c73885afb16884e18135c34f6
https://github.com/Fanxingye/Autotabular/tree/d630c78290a52f8c73885afb16884e18135c34f6
AGELU
import math import torch import torch.utils.data import torch.cuda import torch.utils.checkpoint def agelu(x): SQRT_M2_PI = math.sqrt(2 / math.pi) COEFF = 0.044715 return 0.5 * x * (1.0 + torch.tanh(SQRT_M2_PI * (x + COEFF * torch.pow( x, 3)))) class AGELU(torch.nn.Module): def forward(self, input): return agelu(input) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import math import torch.utils.data import torch.cuda import torch.utils.checkpoint assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_mul_pow_tanh_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.5 tmp2 = tmp0 * tmp1 tmp3 = tmp0 * tmp0 tmp4 = tmp3 * tmp0 tmp5 = 0.044715 tmp6 = tmp4 * tmp5 tmp7 = tmp0 + tmp6 tmp8 = 0.7978845608028654 tmp9 = tmp7 * tmp8 tmp10 = libdevice.tanh(tmp9) tmp11 = 1.0 tmp12 = tmp10 + tmp11 tmp13 = tmp2 * tmp12 tl.store(out_ptr0 + x0, tmp13, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mul_pow_tanh_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, def agelu(x): SQRT_M2_PI = math.sqrt(2 / math.pi) COEFF = 0.044715 return 0.5 * x * (1.0 + torch.tanh(SQRT_M2_PI * (x + COEFF * torch.pow( x, 3)))) class AGELUNew(torch.nn.Module): def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
Dan-hbd/NMTGMinor
AGELU
false
5,146
[ "MIT" ]
1
84e59ac8391ee78852d7c71afc60c3c8b8e3d44d
https://github.com/Dan-hbd/NMTGMinor/tree/84e59ac8391ee78852d7c71afc60c3c8b8e3d44d
FocalLoss
import torch import torch.nn as nn def log_minus_sigmoid(x): return torch.clamp(-x, max=0) - torch.log(1 + torch.exp(-torch.abs(x)) ) + 0.5 * torch.clamp(x, min=0, max=0) def log_sigmoid(x): return torch.clamp(x, max=0) - torch.log(1 + torch.exp(-torch.abs(x)) ) + 0.5 * torch.clamp(x, min=0, max=0) class FocalLoss(nn.Module): def __init__(self, gamma=2): super(FocalLoss, self).__init__() self.gamma = gamma def forward(self, input, target): pos_log_sig = log_sigmoid(input) neg_log_sig = log_minus_sigmoid(input) prob = torch.sigmoid(input) pos_weight = torch.pow(1 - prob, self.gamma) neg_weight = torch.pow(prob, self.gamma) loss = -(target * pos_weight * pos_log_sig + (1 - target) * neg_weight * neg_log_sig) avg_weight = target * pos_weight + (1 - target) * neg_weight loss /= avg_weight.mean() return loss.mean() def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_abs_add_clamp_div_exp_log_mean_mul_neg_pow_rsub_sigmoid_sub_0( in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.load(in_ptr1 + r0, None) tmp2 = tl.sigmoid(tmp1) tmp3 = 1.0 tmp4 = tmp3 - tmp2 tmp5 = tmp4 * tmp4 tmp6 = tmp0 * tmp5 tmp7 = 0.0 tmp8 = triton_helpers.minimum(tmp1, tmp7) tmp9 = tl_math.abs(tmp1) tmp10 = -tmp9 tmp11 = tl_math.exp(tmp10) tmp12 = tmp11 + tmp3 tmp13 = tl_math.log(tmp12) tmp14 = tmp8 - tmp13 tmp15 = triton_helpers.maximum(tmp1, tmp7) tmp16 = triton_helpers.minimum(tmp15, tmp7) tmp17 = 0.5 tmp18 = tmp16 * tmp17 tmp19 = tmp14 + tmp18 tmp20 = tmp6 * tmp19 tmp21 = tmp3 - tmp0 tmp22 = tmp2 * tmp2 tmp23 = tmp21 * tmp22 tmp24 = -tmp1 tmp25 = triton_helpers.minimum(tmp24, tmp7) tmp26 = tmp25 - tmp13 tmp27 = tmp26 + tmp18 tmp28 = tmp23 * tmp27 tmp29 = tmp20 + tmp28 tmp30 = -tmp29 tmp31 = tmp6 + tmp23 tmp32 = tl.broadcast_to(tmp31, [RBLOCK]) tmp34 = triton_helpers.promote_to_tensor(tl.sum(tmp32, 0)) tmp35 = 256.0 tmp36 = tmp34 / tmp35 tmp37 = tmp30 / tmp36 tmp38 = tl.broadcast_to(tmp37, [RBLOCK]) tmp40 = triton_helpers.promote_to_tensor(tl.sum(tmp38, 0)) tmp41 = tmp40 / tmp35 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp41, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((), (), torch.float32) buf2 = buf1 del buf1 buf3 = buf2 del buf2 get_raw_stream(0) triton_per_fused_abs_add_clamp_div_exp_log_mean_mul_neg_pow_rsub_sigmoid_sub_0[ grid(1)](buf3, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf3, def log_minus_sigmoid(x): return torch.clamp(-x, max=0) - torch.log(1 + torch.exp(-torch.abs(x)) ) + 0.5 * torch.clamp(x, min=0, max=0) def log_sigmoid(x): return torch.clamp(x, max=0) - torch.log(1 + torch.exp(-torch.abs(x)) ) + 0.5 * torch.clamp(x, min=0, max=0) class FocalLossNew(nn.Module): def __init__(self, gamma=2): super(FocalLossNew, self).__init__() self.gamma = gamma def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
FadedFate/TrackerSiamRPN
FocalLoss
false
5,147
[ "MIT" ]
1
f4156fa4bed9a0ca6c7ac9b653c07e564d8a058d
https://github.com/FadedFate/TrackerSiamRPN/tree/f4156fa4bed9a0ca6c7ac9b653c07e564d8a058d
ReLUDropout
import torch import torch.utils.data import torch.cuda import torch.utils.checkpoint def relu_dropout(x, p=0, training=False, variational=False, batch_first=False): if not training or p == 0: return x.clamp_(min=0) p1m = 1 - p if variational: if batch_first: mask = torch.rand_like(x[:, 0, :]) > p1m mask = mask.unsqueeze(1).repeat(1, x.size(1), 1) else: mask = torch.rand_like(x[0]) > p1m mask = mask.unsqueeze(0).repeat(x.size(0), 1, 1) else: mask = torch.rand_like(x) > p1m mask |= x < 0 return x.masked_fill_(mask, 0).div_(p1m) class ReLUDropout(torch.nn.Dropout): def __init__(self, p=0.5, variational=False, batch_first=False, inplace =False): super().__init__(p, inplace=True) self.variational = variational self.batch_first = batch_first def forward(self, input): return relu_dropout(input, p=self.p, training=self.training, variational=self.variational, batch_first=self.batch_first) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.utils.data import torch.cuda import torch.utils.checkpoint assert_size_stride = torch._C._dynamo.guards.assert_size_stride @triton.jit def triton_poi_fused_clamp_0(in_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.0 tmp2 = triton_helpers.maximum(tmp0, tmp1) tl.store(out_ptr1 + x0, tmp2, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) get_raw_stream(0) triton_poi_fused_clamp_0[grid(256)](arg0_1, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) return arg0_1, def relu_dropout(x, p=0, training=False, variational=False, batch_first=False): if not training or p == 0: return x.clamp_(min=0) p1m = 1 - p if variational: if batch_first: mask = torch.rand_like(x[:, 0, :]) > p1m mask = mask.unsqueeze(1).repeat(1, x.size(1), 1) else: mask = torch.rand_like(x[0]) > p1m mask = mask.unsqueeze(0).repeat(x.size(0), 1, 1) else: mask = torch.rand_like(x) > p1m mask |= x < 0 return x.masked_fill_(mask, 0).div_(p1m) class ReLUDropoutNew(torch.nn.Dropout): def __init__(self, p=0.5, variational=False, batch_first=False, inplace =False): super().__init__(p, inplace=True) self.variational = variational self.batch_first = batch_first def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
Dan-hbd/NMTGMinor
ReLUDropout
false
5,148
[ "MIT" ]
1
84e59ac8391ee78852d7c71afc60c3c8b8e3d44d
https://github.com/Dan-hbd/NMTGMinor/tree/84e59ac8391ee78852d7c71afc60c3c8b8e3d44d