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
DiceLoss
import torch import torch.nn as nn def IoU(logit, truth, smooth=1): prob = torch.sigmoid(logit) intersection = torch.sum(prob * truth) union = torch.sum(prob + truth) iou = (2 * intersection + smooth) / (union + smooth) return iou class DiceLoss(nn.Module): def __init__(self, smooth=1): super(DiceLoss, self).__init__() self.smooth = smooth def forward(self, logit, truth): iou = IoU(logit, truth, self.smooth) loss = 1 - iou 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 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_sigmoid_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 = 2.0 tmp12 = tmp6 * tmp11 tmp13 = 1.0 tmp14 = tmp12 + tmp13 tmp15 = tmp10 + tmp13 tmp16 = tmp14 / tmp15 tmp17 = tmp13 - 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) buf2 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_div_mul_rsub_sigmoid_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, def IoU(logit, truth, smooth=1): prob = torch.sigmoid(logit) intersection = torch.sum(prob * truth) union = torch.sum(prob + truth) iou = (2 * intersection + smooth) / (union + smooth) return iou class DiceLossNew(nn.Module): def __init__(self, smooth=1): super(DiceLossNew, self).__init__() self.smooth = smooth def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
evilidol/kaggle-Steel-Defect-Detection
DiceLoss
false
6,666
[ "MIT" ]
1
41e3e360f49d706c8c79bcd442342c529648a736
https://github.com/evilidol/kaggle-Steel-Defect-Detection/tree/41e3e360f49d706c8c79bcd442342c529648a736
HorizontalMaxPool2d
import torch import torch.nn as nn class HorizontalMaxPool2d(nn.Module): def __init__(self): super(HorizontalMaxPool2d, self).__init__() def forward(self, x): inp_size = x.size() return nn.functional.max_pool2d(input=x, kernel_size=(1, inp_size[3])) 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_max_pool2d_with_indices_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 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(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tl.store(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), (16, 4, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_max_pool2d_with_indices_0[grid(64)](arg0_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 return buf0, class HorizontalMaxPool2dNew(nn.Module): def __init__(self): super(HorizontalMaxPool2dNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
existentmember7/TEMP_monitor
HorizontalMaxPool2d
false
6,667
[ "MIT" ]
1
b8116f4c134793c4caa22eda78f90dd24d0cad30
https://github.com/existentmember7/TEMP_monitor/tree/b8116f4c134793c4caa22eda78f90dd24d0cad30
SpatialGate2d
import torch import torch.nn as nn class SpatialGate2d(nn.Module): def __init__(self, in_channels): super(SpatialGate2d, self).__init__() self.conv1 = nn.Conv2d(in_channels, 1, kernel_size=1, stride=1) self.sigmoid = nn.Sigmoid() def forward(self, x): cal = self.conv1(x) cal = self.sigmoid(cal) return cal * x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_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 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_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 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 tl.store(in_out_ptr0 + x0, tmp3, xmask) @triton.jit def triton_poi_fused_mul_sigmoid_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 % 16 x2 = xindex // 64 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr1 + x3, xmask) tmp1 = tl.sigmoid(tmp0) tmp3 = tmp1 * tmp2 tl.store(out_ptr0 + x3, tmp3, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (1, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_2, (1,), (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 = 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, 1, 4, 4), (16, 16, 4, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(64)](buf1, primals_2, 64, XBLOCK=64, 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_mul_sigmoid_1[grid(256)](buf1, primals_3, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) return buf2, primals_1, primals_3, buf1 class SpatialGate2dNew(nn.Module): def __init__(self, in_channels): super(SpatialGate2dNew, self).__init__() self.conv1 = nn.Conv2d(in_channels, 1, kernel_size=1, stride=1) self.sigmoid = nn.Sigmoid() def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
evilidol/kaggle-Steel-Defect-Detection
SpatialGate2d
false
6,668
[ "MIT" ]
1
41e3e360f49d706c8c79bcd442342c529648a736
https://github.com/evilidol/kaggle-Steel-Defect-Detection/tree/41e3e360f49d706c8c79bcd442342c529648a736
DiceCELoss
import torch import warnings from typing import Callable from typing import Union from typing import Optional from enum import Enum import torch.nn as nn from torch.nn.modules.loss import _Loss import torch.multiprocessing class LossReduction(Enum): """ See also: - :py:class:`monai.losses.dice.DiceLoss` - :py:class:`monai.losses.dice.GeneralizedDiceLoss` - :py:class:`monai.losses.focal_loss.FocalLoss` - :py:class:`monai.losses.tversky.TverskyLoss` """ NONE = 'none' MEAN = 'mean' SUM = 'sum' SUM_MEAN = 'sum_mean' class DiceLoss(_Loss): """ Compute average Dice loss between two tensors. It can support both multi-classes and multi-labels tasks. Input logits `input` (BNHW[D] where N is number of classes) is compared with ground truth `target` (BNHW[D]). Axis N of `input` is expected to have logit predictions for each class rather than being image channels, while the same axis of `target` can be 1 or N (one-hot format). The `smooth_nr` and `smooth_dr` parameters are values added to the intersection and union components of the inter-over-union calculation to smooth results respectively, these values should be small. The `include_background` class attribute can be set to False for an instance of DiceLoss to exclude the first category (channel index 0) which is by convention assumed to be background. If the non-background segmentations are small compared to the total image size they can get overwhelmed by the signal from the background so excluding it in such cases helps convergence. Milletari, F. et. al. (2016) V-Net: Fully Convolutional Neural Networks forVolumetric Medical Image Segmentation, 3DV, 2016. """ def __init__(self, include_background: 'bool'=True, to_onehot_y: 'bool' =False, sigmoid: 'bool'=False, softmax: 'bool'=False, other_act: 'Optional[Callable]'=None, squared_pred: 'bool'=False, jaccard: 'bool'=False, reduction: 'Union[LossReduction, str]'=LossReduction. MEAN, smooth_nr: 'float'=1e-05, smooth_dr: 'float'=1e-05, batch: 'bool'=False, my_dice: 'bool'=False) ->None: """ Args: include_background: if False channel index 0 (background category) is excluded from the calculation. to_onehot_y: whether to convert `y` into the one-hot format. Defaults to False. sigmoid: if True, apply a sigmoid function to the prediction. softmax: if True, apply a softmax function to the prediction. other_act: if don't want to use `sigmoid` or `softmax`, use other callable function to execute other activation layers, Defaults to ``None``. for example: `other_act = torch.tanh`. squared_pred: use squared versions of targets and predictions in the denominator or not. jaccard: compute Jaccard Index (soft IoU) instead of dice or not. reduction: {``"none"``, ``"mean"``, ``"sum"``} Specifies the reduction to apply to the output. Defaults to ``"mean"``. - ``"none"``: no reduction will be applied. - ``"mean"``: the sum of the output will be divided by the number of elements in the output. - ``"sum"``: the output will be summed. smooth_nr: a small constant added to the numerator to avoid zero. smooth_dr: a small constant added to the denominator to avoid nan. batch: whether to sum the intersection and union areas over the batch dimension before the dividing. Defaults to False, a Dice loss value is computed independently from each item in the batch before any `reduction`. Raises: TypeError: When ``other_act`` is not an ``Optional[Callable]``. ValueError: When more than 1 of [``sigmoid=True``, ``softmax=True``, ``other_act is not None``]. Incompatible values. """ super().__init__(reduction=LossReduction(reduction).value) if other_act is not None and not callable(other_act): raise TypeError( f'other_act must be None or callable but is {type(other_act).__name__}.' ) if int(sigmoid) + int(softmax) + int(other_act is not None) > 1: raise ValueError( 'Incompatible values: more than 1 of [sigmoid=True, softmax=True, other_act is not None].' ) self.include_background = include_background self.to_onehot_y = to_onehot_y self.sigmoid = sigmoid self.softmax = softmax self.other_act = other_act self.squared_pred = squared_pred self.jaccard = jaccard self.smooth_nr = float(smooth_nr) self.smooth_dr = float(smooth_dr) self.batch = batch self.my_dice = my_dice def forward(self, input: 'torch.Tensor', target: 'torch.Tensor' ) ->torch.Tensor: """ Args: input: the shape should be BNH[WD]. target: the shape should be BNH[WD]. Raises: ValueError: When ``self.reduction`` is not one of ["mean", "sum", "none"]. """ if self.sigmoid: input = torch.sigmoid(input) n_pred_ch = input.shape[1] if self.softmax: if n_pred_ch == 1: warnings.warn( 'single channel prediction, `softmax=True` ignored.') else: input = torch.softmax(input, 1) if self.other_act is not None: input = self.other_act(input) if self.to_onehot_y: if n_pred_ch == 1: warnings.warn( 'single channel prediction, `to_onehot_y=True` ignored.') else: target = one_hot(target, num_classes=n_pred_ch) assert target.shape == input.shape, f'ground truth has differing shape ({target.shape}) from input ({input.shape})' reduce_axis = list(range(2, len(input.shape))) if self.batch: reduce_axis = [0] + reduce_axis intersection = torch.sum(target * input, dim=reduce_axis) if self.squared_pred: target = torch.pow(target, 2) input = torch.pow(input, 2) ground_o = torch.sum(target, dim=reduce_axis) pred_o = torch.sum(input, dim=reduce_axis) denominator = ground_o + pred_o if self.jaccard: denominator = 2.0 * (denominator - intersection) if self.my_dice: def count_nonzeros(input, dim): mask = input != 0.0 return torch.sum(mask, dim) f: 'torch.Tensor' = 2.0 * intersection / (denominator + self. smooth_nr) if not self.include_background: if self.batch: f = f[1:] ground_o = ground_o[1:] else: f = f[:, 1:] ground_o = ground_o[:, 1:] f = -(torch.sum(f, dim=-1) / count_nonzeros(ground_o, dim=-1)) else: f: 'torch.Tensor' = -(2.0 * intersection + self.smooth_nr) / ( denominator + self.smooth_dr) if not self.include_background: if self.batch: f = f[1:] else: f = f[:, 1:] if self.reduction == LossReduction.MEAN.value: f = torch.mean(f) elif self.reduction == LossReduction.SUM.value: f = torch.sum(f) elif self.reduction == LossReduction.NONE.value: pass else: raise ValueError( f'Unsupported reduction: {self.reduction}, available options are ["mean", "sum", "none"].' ) return f class DiceCELoss(_Loss): """ Compute both Dice loss and Cross Entropy Loss, and return the sum of these two losses. Input logits `input` (BNHW[D] where N is number of classes) is compared with ground truth `target` (BNHW[D]). Axis N of `input` is expected to have logit predictions for each class rather than being image channels, while the same axis of `target` can be 1 or N (one-hot format). The `smooth_nr` and `smooth_dr` parameters are values added for dice loss part to the intersection and union components of the inter-over-union calculation to smooth results respectively, these values should be small. The `include_background` class attribute can be set to False for an instance of the loss to exclude the first category (channel index 0) which is by convention assumed to be background. If the non-background segmentations are small compared to the total image size they can get overwhelmed by the signal from the background so excluding it in such cases helps convergence. """ def __init__(self, include_background: 'bool'=True, to_onehot_y: 'bool' =False, sigmoid: 'bool'=False, softmax: 'bool'=False, other_act: 'Optional[Callable]'=None, squared_pred: 'bool'=False, jaccard: 'bool'=False, reduction: 'str'='mean', smooth_nr: 'float'=1e-05, smooth_dr: 'float'=1e-05, batch: 'bool'=False, ce_weight: 'Optional[torch.Tensor]'=None, negate_dice: 'bool'=False, my_dice: 'bool'=False) ->None: """ Args: ``ce_weight`` is only used for cross entropy loss, ``reduction`` is used for both losses and other parameters are only used for dice loss. include_background: if False channel index 0 (background category) is excluded from the calculation. to_onehot_y: whether to convert `y` into the one-hot format. Defaults to False. sigmoid: if True, apply a sigmoid function to the prediction. softmax: if True, apply a softmax function to the prediction. other_act: if don't want to use `sigmoid` or `softmax`, use other callable function to execute other activation layers, Defaults to ``None``. for example: `other_act = torch.tanh`. squared_pred: use squared versions of targets and predictions in the denominator or not. jaccard: compute Jaccard Index (soft IoU) instead of dice or not. reduction: {``"mean"``, ``"sum"``} Specifies the reduction to apply to the output. Defaults to ``"mean"``. The dice loss should as least reduce the spatial dimensions, which is different from cross entropy loss, thus here the ``none`` option cannot be used. - ``"mean"``: the sum of the output will be divided by the number of elements in the output. - ``"sum"``: the output will be summed. smooth_nr: a small constant added to the numerator to avoid zero. smooth_dr: a small constant added to the denominator to avoid nan. batch: whether to sum the intersection and union areas over the batch dimension before the dividing. Defaults to False, a Dice loss value is computed independently from each item in the batch before any `reduction`. ce_weight: a rescaling weight given to each class for cross entropy loss. See ``torch.nn.CrossEntropyLoss()`` for more information. """ super().__init__() self.dice = DiceLoss(include_background=include_background, to_onehot_y=to_onehot_y, sigmoid=sigmoid, softmax=softmax, other_act=other_act, squared_pred=squared_pred, jaccard=jaccard, reduction=reduction, smooth_nr=smooth_nr, smooth_dr=smooth_dr, batch=batch, my_dice=my_dice) self.cross_entropy = nn.CrossEntropyLoss(weight=ce_weight, reduction=reduction) self.negate_dice = negate_dice def forward(self, input: 'torch.Tensor', target: 'torch.Tensor' ) ->torch.Tensor: """ Args: input: the shape should be BNH[WD]. target: the shape should be BNH[WD] or B1H[WD]. Raises: ValueError: When number of dimensions for input and target are different. ValueError: When number of channels for target is nither 1 or the same as input. """ if len(input.shape) != len(target.shape): raise ValueError( 'the number of dimensions for input and target should be the same.' ) dice_loss = self.dice(input, target) if self.negate_dice: dice_loss = dice_loss * -1 n_pred_ch, n_target_ch = input.shape[1], target.shape[1] if n_pred_ch == n_target_ch: target = torch.argmax(target, dim=1) else: target = torch.squeeze(target, dim=1) target = target.long() ce_loss = self.cross_entropy(input, target) total_loss: 'torch.Tensor' = dice_loss + ce_loss return total_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 warnings from typing import Callable from typing import Union from typing import Optional from enum import Enum import torch.nn as nn from torch.nn.modules.loss import _Loss import torch.multiprocessing 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__log_softmax_mul_sum_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, out_ptr3, 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 x3 = xindex // 4 tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tl.load(in_ptr1 + (r1 + 16 * x0), xmask, other=0.0) tmp15 = tl.load(in_ptr1 + (r1 + 64 * x3), xmask, eviction_policy= 'evict_last', other=0.0) tmp16 = tl.load(in_ptr1 + (16 + r1 + 64 * x3), xmask, eviction_policy= 'evict_last', other=0.0) tmp18 = tl.load(in_ptr1 + (32 + r1 + 64 * x3), xmask, eviction_policy= 'evict_last', other=0.0) tmp20 = tl.load(in_ptr1 + (48 + r1 + 64 * x3), xmask, eviction_policy= 'evict_last', other=0.0) tmp2 = tmp0 * tmp1 tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp5 = tl.where(xmask, tmp3, 0) tmp6 = tl.sum(tmp5, 1)[:, None] tmp7 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp9 = tl.where(xmask, tmp7, 0) tmp10 = tl.sum(tmp9, 1)[:, None] tmp11 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp13 = tl.where(xmask, tmp11, 0) tmp14 = tl.sum(tmp13, 1)[:, None] tmp17 = triton_helpers.maximum(tmp15, tmp16) tmp19 = triton_helpers.maximum(tmp17, tmp18) tmp21 = triton_helpers.maximum(tmp19, tmp20) tmp22 = tmp1 - tmp21 tl.store(out_ptr3 + (r1 + 16 * x0), tmp22, xmask) tl.store(out_ptr0 + x0, tmp6, xmask) tl.store(out_ptr1 + x0, tmp10, xmask) tl.store(out_ptr2 + x0, tmp14, xmask) @triton.jit def triton_per_fused_add_div_mean_mul_neg_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0, 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 tmp0 = tl.load(in_ptr0 + r0, None) tmp6 = tl.load(in_ptr1 + r0, None) tmp7 = tl.load(in_ptr2 + r0, None) tmp1 = 2.0 tmp2 = tmp0 * tmp1 tmp3 = 1e-05 tmp4 = tmp2 + tmp3 tmp5 = -tmp4 tmp8 = tmp6 + tmp7 tmp9 = tmp8 + tmp3 tmp10 = tmp5 / tmp9 tmp11 = tl.broadcast_to(tmp10, [XBLOCK, RBLOCK]) tmp13 = tl.sum(tmp11, 1)[:, None] tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp13, None) @triton.jit def triton_per_fused_add_argmax_div_mean_mul_neg_nll_loss2d_forward_2( in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 64 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 % 16 r1 = rindex // 16 tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None) tmp1 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None) tmp17 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None) tmp32 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None) tmp56 = tl.load(in_ptr1 + (r0 + 64 * r1), None) tmp58 = tl.load(in_ptr1 + (16 + r0 + 64 * r1), None) tmp61 = tl.load(in_ptr1 + (32 + r0 + 64 * r1), None) tmp64 = tl.load(in_ptr1 + (48 + r0 + 64 * r1), None) tmp79 = tl.load(in_out_ptr0 + 0) tmp80 = tl.broadcast_to(tmp79, [XBLOCK, 1]) tmp2 = tmp0 > tmp1 tmp3 = tmp0 == tmp1 tmp4 = tmp0 != tmp0 tmp5 = tmp1 != tmp1 tmp6 = tmp4 > tmp5 tmp7 = tmp2 | tmp6 tmp8 = tmp4 & tmp5 tmp9 = tmp3 | tmp8 tmp10 = tl.full([1, 1], 0, tl.int64) tmp11 = tl.full([1, 1], 1, tl.int64) tmp12 = tmp10 < tmp11 tmp13 = tmp9 & tmp12 tmp14 = tmp7 | tmp13 tmp15 = tl.where(tmp14, tmp0, tmp1) tmp16 = tl.where(tmp14, tmp10, tmp11) tmp18 = tmp15 > tmp17 tmp19 = tmp15 == tmp17 tmp20 = tmp15 != tmp15 tmp21 = tmp17 != tmp17 tmp22 = tmp20 > tmp21 tmp23 = tmp18 | tmp22 tmp24 = tmp20 & tmp21 tmp25 = tmp19 | tmp24 tmp26 = tl.full([1, 1], 2, tl.int64) tmp27 = tmp16 < tmp26 tmp28 = tmp25 & tmp27 tmp29 = tmp23 | tmp28 tmp30 = tl.where(tmp29, tmp15, tmp17) tmp31 = tl.where(tmp29, tmp16, tmp26) tmp33 = tmp30 > tmp32 tmp34 = tmp30 == tmp32 tmp35 = tmp30 != tmp30 tmp36 = tmp32 != tmp32 tmp37 = tmp35 > tmp36 tmp38 = tmp33 | tmp37 tmp39 = tmp35 & tmp36 tmp40 = tmp34 | tmp39 tmp41 = tl.full([1, 1], 3, tl.int64) tmp42 = tmp31 < tmp41 tmp43 = tmp40 & tmp42 tmp44 = tmp38 | tmp43 tl.where(tmp44, tmp30, tmp32) tmp46 = tl.where(tmp44, tmp31, tmp41) tmp47 = tl.full([1, 1], -100, tl.int64) tmp48 = tmp46 != tmp47 tmp49 = tl.where(tmp48, tmp46, tmp10) tmp50 = tl.full([XBLOCK, RBLOCK], 4, tl.int32) tmp51 = tmp49 + tmp50 tmp52 = tmp49 < 0 tmp53 = tl.where(tmp52, tmp51, tmp49) tl.device_assert((0 <= tmp53) & (tmp53 < 4), 'index out of bounds: 0 <= tmp53 < 4') tmp55 = tl.load(in_ptr1 + (r0 + 16 * tmp53 + 64 * r1), None) tmp57 = tl_math.exp(tmp56) tmp59 = tl_math.exp(tmp58) tmp60 = tmp57 + tmp59 tmp62 = tl_math.exp(tmp61) tmp63 = tmp60 + tmp62 tmp65 = tl_math.exp(tmp64) tmp66 = tmp63 + tmp65 tmp67 = tl_math.log(tmp66) tmp68 = tmp55 - tmp67 tmp69 = -tmp68 tmp70 = 0.0 tmp71 = tl.where(tmp48, tmp69, tmp70) tmp72 = tl.broadcast_to(tmp71, [XBLOCK, RBLOCK]) tmp74 = tl.sum(tmp72, 1)[:, None] tmp75 = tmp48.to(tl.int64) tmp76 = tl.broadcast_to(tmp75, [XBLOCK, RBLOCK]) tmp78 = tl.sum(tmp76, 1)[:, None] tmp81 = 16.0 tmp82 = tmp80 / tmp81 tmp83 = tmp78.to(tl.float32) tmp84 = tmp74 / tmp83 tmp85 = tmp82 + tmp84 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp85, 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) buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_per_fused__log_softmax_mul_sum_0[grid(16)](arg1_1, arg0_1, buf0, buf1, buf2, buf5, 16, 16, XBLOCK=1, num_warps=2, num_stages=1 ) del arg0_1 buf3 = empty_strided_cuda((), (), torch.float32) triton_per_fused_add_div_mean_mul_neg_1[grid(1)](buf0, buf1, buf2, buf3, 1, 16, XBLOCK=1, num_warps=2, num_stages=1) del buf0 del buf1 del buf2 buf8 = buf3 del buf3 triton_per_fused_add_argmax_div_mean_mul_neg_nll_loss2d_forward_2[grid (1)](buf8, arg1_1, buf5, 1, 64, XBLOCK=1, num_warps=2, num_stages=1 ) del arg1_1 del buf5 return buf8, class LossReduction(Enum): """ See also: - :py:class:`monai.losses.dice.DiceLoss` - :py:class:`monai.losses.dice.GeneralizedDiceLoss` - :py:class:`monai.losses.focal_loss.FocalLoss` - :py:class:`monai.losses.tversky.TverskyLoss` """ NONE = 'none' MEAN = 'mean' SUM = 'sum' SUM_MEAN = 'sum_mean' class DiceLoss(_Loss): """ Compute average Dice loss between two tensors. It can support both multi-classes and multi-labels tasks. Input logits `input` (BNHW[D] where N is number of classes) is compared with ground truth `target` (BNHW[D]). Axis N of `input` is expected to have logit predictions for each class rather than being image channels, while the same axis of `target` can be 1 or N (one-hot format). The `smooth_nr` and `smooth_dr` parameters are values added to the intersection and union components of the inter-over-union calculation to smooth results respectively, these values should be small. The `include_background` class attribute can be set to False for an instance of DiceLoss to exclude the first category (channel index 0) which is by convention assumed to be background. If the non-background segmentations are small compared to the total image size they can get overwhelmed by the signal from the background so excluding it in such cases helps convergence. Milletari, F. et. al. (2016) V-Net: Fully Convolutional Neural Networks forVolumetric Medical Image Segmentation, 3DV, 2016. """ def __init__(self, include_background: 'bool'=True, to_onehot_y: 'bool' =False, sigmoid: 'bool'=False, softmax: 'bool'=False, other_act: 'Optional[Callable]'=None, squared_pred: 'bool'=False, jaccard: 'bool'=False, reduction: 'Union[LossReduction, str]'=LossReduction. MEAN, smooth_nr: 'float'=1e-05, smooth_dr: 'float'=1e-05, batch: 'bool'=False, my_dice: 'bool'=False) ->None: """ Args: include_background: if False channel index 0 (background category) is excluded from the calculation. to_onehot_y: whether to convert `y` into the one-hot format. Defaults to False. sigmoid: if True, apply a sigmoid function to the prediction. softmax: if True, apply a softmax function to the prediction. other_act: if don't want to use `sigmoid` or `softmax`, use other callable function to execute other activation layers, Defaults to ``None``. for example: `other_act = torch.tanh`. squared_pred: use squared versions of targets and predictions in the denominator or not. jaccard: compute Jaccard Index (soft IoU) instead of dice or not. reduction: {``"none"``, ``"mean"``, ``"sum"``} Specifies the reduction to apply to the output. Defaults to ``"mean"``. - ``"none"``: no reduction will be applied. - ``"mean"``: the sum of the output will be divided by the number of elements in the output. - ``"sum"``: the output will be summed. smooth_nr: a small constant added to the numerator to avoid zero. smooth_dr: a small constant added to the denominator to avoid nan. batch: whether to sum the intersection and union areas over the batch dimension before the dividing. Defaults to False, a Dice loss value is computed independently from each item in the batch before any `reduction`. Raises: TypeError: When ``other_act`` is not an ``Optional[Callable]``. ValueError: When more than 1 of [``sigmoid=True``, ``softmax=True``, ``other_act is not None``]. Incompatible values. """ super().__init__(reduction=LossReduction(reduction).value) if other_act is not None and not callable(other_act): raise TypeError( f'other_act must be None or callable but is {type(other_act).__name__}.' ) if int(sigmoid) + int(softmax) + int(other_act is not None) > 1: raise ValueError( 'Incompatible values: more than 1 of [sigmoid=True, softmax=True, other_act is not None].' ) self.include_background = include_background self.to_onehot_y = to_onehot_y self.sigmoid = sigmoid self.softmax = softmax self.other_act = other_act self.squared_pred = squared_pred self.jaccard = jaccard self.smooth_nr = float(smooth_nr) self.smooth_dr = float(smooth_dr) self.batch = batch self.my_dice = my_dice def forward(self, input: 'torch.Tensor', target: 'torch.Tensor' ) ->torch.Tensor: """ Args: input: the shape should be BNH[WD]. target: the shape should be BNH[WD]. Raises: ValueError: When ``self.reduction`` is not one of ["mean", "sum", "none"]. """ if self.sigmoid: input = torch.sigmoid(input) n_pred_ch = input.shape[1] if self.softmax: if n_pred_ch == 1: warnings.warn( 'single channel prediction, `softmax=True` ignored.') else: input = torch.softmax(input, 1) if self.other_act is not None: input = self.other_act(input) if self.to_onehot_y: if n_pred_ch == 1: warnings.warn( 'single channel prediction, `to_onehot_y=True` ignored.') else: target = one_hot(target, num_classes=n_pred_ch) assert target.shape == input.shape, f'ground truth has differing shape ({target.shape}) from input ({input.shape})' reduce_axis = list(range(2, len(input.shape))) if self.batch: reduce_axis = [0] + reduce_axis intersection = torch.sum(target * input, dim=reduce_axis) if self.squared_pred: target = torch.pow(target, 2) input = torch.pow(input, 2) ground_o = torch.sum(target, dim=reduce_axis) pred_o = torch.sum(input, dim=reduce_axis) denominator = ground_o + pred_o if self.jaccard: denominator = 2.0 * (denominator - intersection) if self.my_dice: def count_nonzeros(input, dim): mask = input != 0.0 return torch.sum(mask, dim) f: 'torch.Tensor' = 2.0 * intersection / (denominator + self. smooth_nr) if not self.include_background: if self.batch: f = f[1:] ground_o = ground_o[1:] else: f = f[:, 1:] ground_o = ground_o[:, 1:] f = -(torch.sum(f, dim=-1) / count_nonzeros(ground_o, dim=-1)) else: f: 'torch.Tensor' = -(2.0 * intersection + self.smooth_nr) / ( denominator + self.smooth_dr) if not self.include_background: if self.batch: f = f[1:] else: f = f[:, 1:] if self.reduction == LossReduction.MEAN.value: f = torch.mean(f) elif self.reduction == LossReduction.SUM.value: f = torch.sum(f) elif self.reduction == LossReduction.NONE.value: pass else: raise ValueError( f'Unsupported reduction: {self.reduction}, available options are ["mean", "sum", "none"].' ) return f class DiceCELossNew(_Loss): """ Compute both Dice loss and Cross Entropy Loss, and return the sum of these two losses. Input logits `input` (BNHW[D] where N is number of classes) is compared with ground truth `target` (BNHW[D]). Axis N of `input` is expected to have logit predictions for each class rather than being image channels, while the same axis of `target` can be 1 or N (one-hot format). The `smooth_nr` and `smooth_dr` parameters are values added for dice loss part to the intersection and union components of the inter-over-union calculation to smooth results respectively, these values should be small. The `include_background` class attribute can be set to False for an instance of the loss to exclude the first category (channel index 0) which is by convention assumed to be background. If the non-background segmentations are small compared to the total image size they can get overwhelmed by the signal from the background so excluding it in such cases helps convergence. """ def __init__(self, include_background: 'bool'=True, to_onehot_y: 'bool' =False, sigmoid: 'bool'=False, softmax: 'bool'=False, other_act: 'Optional[Callable]'=None, squared_pred: 'bool'=False, jaccard: 'bool'=False, reduction: 'str'='mean', smooth_nr: 'float'=1e-05, smooth_dr: 'float'=1e-05, batch: 'bool'=False, ce_weight: 'Optional[torch.Tensor]'=None, negate_dice: 'bool'=False, my_dice: 'bool'=False) ->None: """ Args: ``ce_weight`` is only used for cross entropy loss, ``reduction`` is used for both losses and other parameters are only used for dice loss. include_background: if False channel index 0 (background category) is excluded from the calculation. to_onehot_y: whether to convert `y` into the one-hot format. Defaults to False. sigmoid: if True, apply a sigmoid function to the prediction. softmax: if True, apply a softmax function to the prediction. other_act: if don't want to use `sigmoid` or `softmax`, use other callable function to execute other activation layers, Defaults to ``None``. for example: `other_act = torch.tanh`. squared_pred: use squared versions of targets and predictions in the denominator or not. jaccard: compute Jaccard Index (soft IoU) instead of dice or not. reduction: {``"mean"``, ``"sum"``} Specifies the reduction to apply to the output. Defaults to ``"mean"``. The dice loss should as least reduce the spatial dimensions, which is different from cross entropy loss, thus here the ``none`` option cannot be used. - ``"mean"``: the sum of the output will be divided by the number of elements in the output. - ``"sum"``: the output will be summed. smooth_nr: a small constant added to the numerator to avoid zero. smooth_dr: a small constant added to the denominator to avoid nan. batch: whether to sum the intersection and union areas over the batch dimension before the dividing. Defaults to False, a Dice loss value is computed independently from each item in the batch before any `reduction`. ce_weight: a rescaling weight given to each class for cross entropy loss. See ``torch.nn.CrossEntropyLoss()`` for more information. """ super().__init__() self.dice = DiceLoss(include_background=include_background, to_onehot_y=to_onehot_y, sigmoid=sigmoid, softmax=softmax, other_act=other_act, squared_pred=squared_pred, jaccard=jaccard, reduction=reduction, smooth_nr=smooth_nr, smooth_dr=smooth_dr, batch=batch, my_dice=my_dice) self.cross_entropy = nn.CrossEntropyLoss(weight=ce_weight, reduction=reduction) self.negate_dice = negate_dice def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
elitap/classimbalance
DiceCELoss
false
6,669
[ "Apache-2.0" ]
1
ae807ec533da5eef18f4180b29383399bc57696a
https://github.com/elitap/classimbalance/tree/ae807ec533da5eef18f4180b29383399bc57696a
BiasConvFc2Net
import torch import torch.nn as nn class BiasConvFc2Net(nn.Module): def __init__(self, in_channels, groups, n_segment, kernel_size=3, padding=1 ): super(BiasConvFc2Net, self).__init__() self.conv = nn.Conv1d(in_channels, 1, kernel_size, padding=padding) self.fc = nn.Linear(n_segment, n_segment) self.relu = nn.ReLU() self.lastlayer = nn.Linear(n_segment, groups) def forward(self, x): N, _C, T = x.shape x = self.conv(x) x = x.view(N, T) x = self.relu(self.fc(x)) x = self.lastlayer(x) x = x.view(N, 1, -1) return x def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'groups': 1, 'n_segment': 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_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 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 tl.store(in_out_ptr0 + x0, tmp3, xmask) @triton.jit def triton_poi_fused_relu_1(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 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) = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (1, 4, 3), (12, 3, 1)) assert_size_stride(primals_3, (1,), (1,)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (1, 4), (4, 1)) assert_size_stride(primals_7, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(1,), padding=(1,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf0, (4, 1, 4), (4, 4, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(16)](buf1, primals_3, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_3 buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (4, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf2) buf3 = buf2 del buf2 triton_poi_fused_relu_1[grid(16)](buf3, primals_5, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_5 buf5 = empty_strided_cuda((4, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_7, buf3, reinterpret_tensor(primals_6, (4, 1), (1, 4), 0), alpha=1, beta=1, out=buf5) del primals_7 return reinterpret_tensor(buf5, (4, 1, 1), (1, 1, 1), 0 ), primals_1, primals_2, reinterpret_tensor(buf1, (4, 4), (4, 1), 0 ), buf3, primals_6, primals_4 class BiasConvFc2NetNew(nn.Module): def __init__(self, in_channels, groups, n_segment, kernel_size=3, padding=1 ): super(BiasConvFc2NetNew, self).__init__() self.conv = nn.Conv1d(in_channels, 1, kernel_size, padding=padding) self.fc = nn.Linear(n_segment, n_segment) self.relu = nn.ReLU() self.lastlayer = nn.Linear(n_segment, groups) def forward(self, input_0): primals_2 = self.conv.weight primals_3 = self.conv.bias primals_4 = self.fc.weight primals_5 = self.fc.bias primals_6 = self.lastlayer.weight primals_7 = self.lastlayer.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
eynaij/X-Temporal_catdim
BiasConvFc2Net
false
6,670
[ "MIT" ]
1
6a2efba407c09c83ca061c8467c1373b6ed0c7eb
https://github.com/eynaij/X-Temporal_catdim/tree/6a2efba407c09c83ca061c8467c1373b6ed0c7eb
SCse
import torch import torch.nn as nn class GAB(nn.Module): def __init__(self, input_dim, reduction=4): super(GAB, self).__init__() self.global_avgpool = nn.AdaptiveAvgPool2d(1) self.conv1 = nn.Conv2d(input_dim, input_dim // reduction, kernel_size=1, stride=1) self.conv2 = nn.Conv2d(input_dim // reduction, input_dim, kernel_size=1, stride=1) self.relu = nn.ReLU(inplace=True) self.sigmoid = nn.Sigmoid() def forward(self, x): z = self.global_avgpool(x) z = self.relu(self.conv1(z)) z = self.sigmoid(self.conv2(z)) return x * z class SpatialAttention2d(nn.Module): def __init__(self, channel): super(SpatialAttention2d, self).__init__() self.squeeze = nn.Conv2d(channel, 1, kernel_size=1, bias=False) self.sigmoid = nn.Sigmoid() def forward(self, x): z = self.squeeze(x) z = self.sigmoid(z) return x * z class SCse(nn.Module): def __init__(self, dim): super(SCse, self).__init__() self.satt = SpatialAttention2d(dim) self.catt = GAB(dim) def forward(self, x): return self.satt(x) + self.catt(x) def get_inputs(): return [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 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_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) @triton.jit def triton_poi_fused_convolution_relu_1(in_out_ptr0, in_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_out_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp3 = tmp0 + tmp2 tmp4 = tl.full([1], 0, tl.int32) tmp5 = triton_helpers.maximum(tmp4, tmp3) tl.store(in_out_ptr0 + x0, tmp5, 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) @triton.jit def triton_poi_fused_add_mul_sigmoid_3(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 x0 = xindex % 16 x2 = xindex // 64 x4 = xindex // 16 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + (x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last') tmp2 = tl.sigmoid(tmp1) tmp3 = tmp0 * tmp2 tmp5 = tl.sigmoid(tmp4) tmp6 = tmp0 * tmp5 tmp7 = tmp3 + tmp6 tl.store(out_ptr0 + x3, tmp7, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args args.clear() assert_size_stride(primals_1, (1, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (1, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_4, (1,), (1,)) assert_size_stride(primals_5, (4, 1, 1, 1), (1, 1, 1, 1)) assert_size_stride(primals_6, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_2, 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, 1, 4, 4), (16, 16, 4, 1)) buf1 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) buf2 = reinterpret_tensor(buf1, (4, 4, 1, 1), (4, 1, 1, 1), 0) del buf1 get_raw_stream(0) triton_per_fused_mean_0[grid(16)](buf2, primals_2, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) buf3 = extern_kernels.convolution(buf2, primals_3, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf3, (4, 1, 1, 1), (1, 1, 1, 1)) buf4 = buf3 del buf3 triton_poi_fused_convolution_relu_1[grid(4)](buf4, primals_4, 4, XBLOCK=4, num_warps=1, num_stages=1) del primals_4 buf5 = extern_kernels.convolution(buf4, primals_5, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf5, (4, 4, 1, 1), (4, 1, 1, 1)) buf6 = buf5 del buf5 triton_poi_fused_convolution_2[grid(16)](buf6, primals_6, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_6 buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_add_mul_sigmoid_3[grid(256)](primals_2, buf0, buf6, buf7, 256, XBLOCK=256, num_warps=4, num_stages=1) return (buf7, primals_1, primals_2, primals_3, primals_5, buf0, buf2, buf4, buf6) class GAB(nn.Module): def __init__(self, input_dim, reduction=4): super(GAB, self).__init__() self.global_avgpool = nn.AdaptiveAvgPool2d(1) self.conv1 = nn.Conv2d(input_dim, input_dim // reduction, kernel_size=1, stride=1) self.conv2 = nn.Conv2d(input_dim // reduction, input_dim, kernel_size=1, stride=1) self.relu = nn.ReLU(inplace=True) self.sigmoid = nn.Sigmoid() def forward(self, x): z = self.global_avgpool(x) z = self.relu(self.conv1(z)) z = self.sigmoid(self.conv2(z)) return x * z class SpatialAttention2d(nn.Module): def __init__(self, channel): super(SpatialAttention2d, self).__init__() self.squeeze = nn.Conv2d(channel, 1, kernel_size=1, bias=False) self.sigmoid = nn.Sigmoid() def forward(self, x): z = self.squeeze(x) z = self.sigmoid(z) return x * z class SCseNew(nn.Module): def __init__(self, dim): super(SCseNew, self).__init__() self.satt = SpatialAttention2d(dim) self.catt = GAB(dim) def forward(self, input_0): primals_1 = self.satt.squeeze.weight primals_3 = self.catt.conv1.weight primals_4 = self.catt.conv1.bias primals_5 = self.catt.conv2.weight primals_6 = self.catt.conv2.bias primals_2 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6]) return output[0]
evilidol/kaggle-Steel-Defect-Detection
SCse
false
6,671
[ "MIT" ]
1
41e3e360f49d706c8c79bcd442342c529648a736
https://github.com/evilidol/kaggle-Steel-Defect-Detection/tree/41e3e360f49d706c8c79bcd442342c529648a736
BertAttention
from _paritybench_helpers import _mock_config import math import torch from torch import nn class BertLayerNorm(nn.Module): def __init__(self, config, variance_epsilon=1e-12): """Construct a layernorm module in the TF style (epsilon inside the square root). """ super(BertLayerNorm, self).__init__() self.gamma = nn.Parameter(torch.ones(config.hidden_size)) self.beta = nn.Parameter(torch.zeros(config.hidden_size)) self.variance_epsilon = variance_epsilon def forward(self, x): u = x.mean(-1, keepdim=True) s = (x - u).pow(2).mean(-1, keepdim=True) x = (x - u) / torch.sqrt(s + self.variance_epsilon) return self.gamma * x + self.beta class BertSelfAttention(nn.Module): def __init__(self, config): super(BertSelfAttention, self).__init__() if config.hidden_size % config.num_attention_heads != 0: raise ValueError( 'The hidden size (%d) is not a multiple of the number of attention heads (%d)' % (config.hidden_size, config.num_attention_heads)) self.num_attention_heads = config.num_attention_heads self.attention_head_size = int(config.hidden_size / config. num_attention_heads) self.all_head_size = (self.num_attention_heads * self. attention_head_size) self.query = nn.Linear(config.hidden_size, self.all_head_size) self.key = nn.Linear(config.hidden_size, self.all_head_size) self.value = nn.Linear(config.hidden_size, self.all_head_size) self.dropout = nn.Dropout(config.attention_probs_dropout_prob) def transpose_for_scores(self, x): new_x_shape = x.size()[:-1] + (self.num_attention_heads, self. attention_head_size) x = x.view(*new_x_shape) return x.permute(0, 2, 1, 3) def forward(self, hidden_states, attention_mask): mixed_query_layer = self.query(hidden_states) mixed_key_layer = self.key(hidden_states) mixed_value_layer = self.value(hidden_states) query_layer = self.transpose_for_scores(mixed_query_layer) key_layer = self.transpose_for_scores(mixed_key_layer) value_layer = self.transpose_for_scores(mixed_value_layer) attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2)) attention_scores = attention_scores / math.sqrt(self. attention_head_size) attention_scores = attention_scores + attention_mask attention_probs = nn.Softmax(dim=-1)(attention_scores) attention_probs = self.dropout(attention_probs) context_layer = torch.matmul(attention_probs, value_layer) context_layer = context_layer.permute(0, 2, 1, 3).contiguous() new_context_layer_shape = context_layer.size()[:-2] + (self. all_head_size,) context_layer = context_layer.view(*new_context_layer_shape) return context_layer class BertSelfOutput(nn.Module): def __init__(self, config): super(BertSelfOutput, self).__init__() self.dense = nn.Linear(config.hidden_size, config.hidden_size) self.LayerNorm = BertLayerNorm(config) self.dropout = nn.Dropout(config.hidden_dropout_prob) def forward(self, hidden_states, input_tensor): hidden_states = self.dense(hidden_states) hidden_states = self.dropout(hidden_states) hidden_states = self.LayerNorm(hidden_states + input_tensor) return hidden_states class BertAttention(nn.Module): def __init__(self, config): super(BertAttention, self).__init__() self.self = BertSelfAttention(config) self.output = BertSelfOutput(config) def forward(self, input_tensor, attention_mask): self_output = self.self(input_tensor, attention_mask) attention_output = self.output(self_output, input_tensor) return attention_output def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'config': _mock_config(hidden_size=4, num_attention_heads= 4, attention_probs_dropout_prob=0.5, hidden_dropout_prob=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 import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import 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_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 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 1.0 tmp4 = tmp2 * tmp3 tl.store(out_ptr0 + (x2 + 4 * y3), tmp4, xmask & ymask) @triton.jit def triton_poi_fused_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, 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 % 16 tmp0 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = triton_helpers.maximum(tmp2, tmp5) tmp9 = tmp7 + tmp8 tmp10 = triton_helpers.maximum(tmp6, tmp9) tmp13 = tmp11 + tmp12 tmp14 = triton_helpers.maximum(tmp10, tmp13) tmp15 = tmp2 - tmp14 tmp16 = tl_math.exp(tmp15) tmp17 = tmp5 - tmp14 tmp18 = tl_math.exp(tmp17) tmp19 = tmp16 + tmp18 tmp20 = tmp9 - tmp14 tmp21 = tl_math.exp(tmp20) tmp22 = tmp19 + tmp21 tmp23 = tmp13 - tmp14 tmp24 = tl_math.exp(tmp23) tmp25 = tmp22 + tmp24 tmp26 = float('-inf') tmp27 = tmp2 == tmp26 tmp28 = tmp27 == 0 tmp29 = tmp28.to(tl.int64) tmp30 = tmp29 != 0 tmp31 = tmp5 == tmp26 tmp32 = tmp31 == 0 tmp33 = tmp32.to(tl.int64) tmp34 = tmp33 != 0 tmp35 = tmp30 | tmp34 tmp36 = tmp9 == tmp26 tmp37 = tmp36 == 0 tmp38 = tmp37.to(tl.int64) tmp39 = tmp38 != 0 tmp40 = tmp35 | tmp39 tmp41 = tmp13 == tmp26 tmp42 = tmp41 == 0 tmp43 = tmp42.to(tl.int64) tmp44 = tmp43 != 0 tmp45 = tmp40 | tmp44 tl.store(out_ptr0 + x2, tmp14, xmask) tl.store(out_ptr1 + x2, tmp25, xmask) tl.store(out_ptr2 + x2, tmp45, xmask) @triton.jit def triton_poi_fused_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex // 4 x4 = xindex x5 = xindex % 64 tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last').to(tl .int1) tmp2 = tl.load(in_out_ptr0 + x4, xmask) tmp3 = tl.load(in_ptr1 + x5, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr2 + x3, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr3 + x3, xmask, eviction_policy='evict_last') tmp1 = tmp0 == 0 tmp4 = tmp2 + tmp3 tmp6 = tmp4 - tmp5 tmp7 = tl_math.exp(tmp6) tmp9 = tmp7 / tmp8 tmp10 = 0.0 tmp11 = tl.where(tmp1, tmp10, tmp9) tl.store(in_out_ptr0 + x4, tmp11, xmask) @triton.jit def triton_poi_fused_3(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 + 4 * x2 + 16 * 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_4(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_add_mean_pow_sub_5(in_ptr0, in_ptr1, out_ptr0, out_ptr1, 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 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp9 = tmp7 + tmp8 tmp10 = tmp6 + tmp9 tmp13 = tmp11 + tmp12 tmp14 = tmp10 + tmp13 tmp15 = 4.0 tmp16 = tmp14 / tmp15 tmp17 = tmp2 - tmp16 tmp18 = tmp17 * tmp17 tmp19 = tmp5 - tmp16 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp22 = tmp9 - tmp16 tmp23 = tmp22 * tmp22 tmp24 = tmp21 + tmp23 tmp25 = tmp13 - tmp16 tmp26 = tmp25 * tmp25 tmp27 = tmp24 + tmp26 tmp28 = tmp27 / tmp15 tl.store(out_ptr0 + x0, tmp16, xmask) tl.store(out_ptr1 + x0, tmp28, xmask) @triton.jit def triton_poi_fused_add_div_mean_mul_sqrt_sub_6(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, 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 x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask) tmp2 = tl.load(in_ptr2 + x2, xmask) tmp4 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 - tmp4 tmp7 = 1e-12 tmp8 = tmp6 + tmp7 tmp9 = libdevice.sqrt(tmp8) tmp10 = tmp5 / tmp9 tmp11 = tmp0 * tmp10 tmp13 = tmp11 + tmp12 tl.store(out_ptr0 + x2, tmp13, 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 ) = 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), (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,)) assert_size_stride(primals_8, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_9, (4, 4), (4, 1)) assert_size_stride(primals_10, (4,), (1,)) assert_size_stride(primals_11, (4,), (1,)) assert_size_stride(primals_12, (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_3, (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_3, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1) del primals_4 buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (16, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), out=buf2) del primals_6 buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_0[grid(16, 4)](buf0, primals_2, buf3, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_2 buf4 = reinterpret_tensor(buf0, (4, 4, 1, 4), (16, 4, 4, 1), 0) del buf0 triton_poi_fused_0[grid(16, 4)](buf1, primals_5, buf4, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_5 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 = reinterpret_tensor(buf1, (4, 4, 4, 1), (16, 4, 1, 64), 0) del buf1 buf7 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf8 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.bool) triton_poi_fused_1[grid(64)](buf5, primals_8, buf6, buf7, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1) buf9 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf5 triton_poi_fused_2[grid(256)](buf9, buf8, primals_8, buf6, buf7, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf8 del primals_8 buf10 = reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf7 triton_poi_fused_3[grid(16, 4)](buf2, primals_7, buf10, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) del primals_7 buf11 = reinterpret_tensor(buf2, (16, 4, 1), (4, 1, 1), 0) del buf2 extern_kernels.bmm(reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf10, (16, 4, 1), (4, 1, 0), 0), out=buf11) buf12 = reinterpret_tensor(buf6, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf6 triton_poi_fused_clone_4[grid(16, 4)](buf11, buf12, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf13 = reinterpret_tensor(buf11, (16, 4), (4, 1), 0) del buf11 extern_kernels.addmm(primals_10, reinterpret_tensor(buf12, (16, 4), (4, 1), 0), reinterpret_tensor(primals_9, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf13) del primals_10 buf14 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf15 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) triton_poi_fused_add_mean_pow_sub_5[grid(16)](buf13, primals_3, buf14, buf15, 16, XBLOCK=16, num_warps=1, num_stages=1) buf16 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_add_div_mean_mul_sqrt_sub_6[grid(64)](primals_11, buf13, primals_3, buf14, buf15, primals_12, buf16, 64, XBLOCK= 64, num_warps=1, num_stages=1) del buf14 del buf15 del primals_12 return buf16, primals_3, primals_11, buf9, reinterpret_tensor(buf10, ( 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 ), reinterpret_tensor(buf12, (16, 4), (4, 1), 0), buf13, primals_9 class BertLayerNorm(nn.Module): def __init__(self, config, variance_epsilon=1e-12): """Construct a layernorm module in the TF style (epsilon inside the square root). """ super(BertLayerNorm, self).__init__() self.gamma = nn.Parameter(torch.ones(config.hidden_size)) self.beta = nn.Parameter(torch.zeros(config.hidden_size)) self.variance_epsilon = variance_epsilon def forward(self, x): u = x.mean(-1, keepdim=True) s = (x - u).pow(2).mean(-1, keepdim=True) x = (x - u) / torch.sqrt(s + self.variance_epsilon) return self.gamma * x + self.beta class BertSelfAttention(nn.Module): def __init__(self, config): super(BertSelfAttention, self).__init__() if config.hidden_size % config.num_attention_heads != 0: raise ValueError( 'The hidden size (%d) is not a multiple of the number of attention heads (%d)' % (config.hidden_size, config.num_attention_heads)) self.num_attention_heads = config.num_attention_heads self.attention_head_size = int(config.hidden_size / config. num_attention_heads) self.all_head_size = (self.num_attention_heads * self. attention_head_size) self.query = nn.Linear(config.hidden_size, self.all_head_size) self.key = nn.Linear(config.hidden_size, self.all_head_size) self.value = nn.Linear(config.hidden_size, self.all_head_size) self.dropout = nn.Dropout(config.attention_probs_dropout_prob) def transpose_for_scores(self, x): new_x_shape = x.size()[:-1] + (self.num_attention_heads, self. attention_head_size) x = x.view(*new_x_shape) return x.permute(0, 2, 1, 3) def forward(self, hidden_states, attention_mask): mixed_query_layer = self.query(hidden_states) mixed_key_layer = self.key(hidden_states) mixed_value_layer = self.value(hidden_states) query_layer = self.transpose_for_scores(mixed_query_layer) key_layer = self.transpose_for_scores(mixed_key_layer) value_layer = self.transpose_for_scores(mixed_value_layer) attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2)) attention_scores = attention_scores / math.sqrt(self. attention_head_size) attention_scores = attention_scores + attention_mask attention_probs = nn.Softmax(dim=-1)(attention_scores) attention_probs = self.dropout(attention_probs) context_layer = torch.matmul(attention_probs, value_layer) context_layer = context_layer.permute(0, 2, 1, 3).contiguous() new_context_layer_shape = context_layer.size()[:-2] + (self. all_head_size,) context_layer = context_layer.view(*new_context_layer_shape) return context_layer class BertSelfOutput(nn.Module): def __init__(self, config): super(BertSelfOutput, self).__init__() self.dense = nn.Linear(config.hidden_size, config.hidden_size) self.LayerNorm = BertLayerNorm(config) self.dropout = nn.Dropout(config.hidden_dropout_prob) def forward(self, hidden_states, input_tensor): hidden_states = self.dense(hidden_states) hidden_states = self.dropout(hidden_states) hidden_states = self.LayerNorm(hidden_states + input_tensor) return hidden_states class BertAttentionNew(nn.Module): def __init__(self, config): super(BertAttentionNew, self).__init__() self.self = BertSelfAttention(config) self.output = BertSelfOutput(config) def forward(self, input_0, input_1): primals_1 = self.self.query.weight primals_2 = self.self.query.bias primals_4 = self.self.key.weight primals_5 = self.self.key.bias primals_6 = self.self.value.weight primals_7 = self.self.value.bias primals_9 = self.output.dense.weight primals_10 = self.output.dense.bias primals_11 = self.output.LayerNorm.gamma primals_12 = self.output.LayerNorm.beta primals_3 = input_0 primals_8 = input_1 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]) return output[0]
BLimmie/pytorch-pretrained-BERT
BertAttention
false
6,672
[ "Apache-2.0" ]
1
2ac4b29641e569020ed2acc28016f481f617052b
https://github.com/BLimmie/pytorch-pretrained-BERT/tree/2ac4b29641e569020ed2acc28016f481f617052b
WeightConvNet
import torch import torch.nn as nn class WeightConvNet(nn.Module): def __init__(self, in_channels, groups, n_segment): super(WeightConvNet, self).__init__() self.lastlayer = nn.Conv1d(in_channels, groups, 3, padding=1) self.groups = groups def forward(self, x): N, _C, T = x.shape x = self.lastlayer(x) x = x.view(N, self.groups, T) x = x.permute(0, 2, 1) return x def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'groups': 1, 'n_segment': 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 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 = 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) tmp1 = tl.load(in_ptr0 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp3 = tmp0 + tmp2 tl.store(in_out_ptr0 + x0, tmp3, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (1, 4, 3), (12, 3, 1)) assert_size_stride(primals_3, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(1,), padding=(1,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf0, (4, 1, 4), (4, 4, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(16)](buf1, primals_3, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_3 return reinterpret_tensor(buf1, (4, 4, 1), (4, 1, 4), 0 ), primals_1, primals_2 class WeightConvNetNew(nn.Module): def __init__(self, in_channels, groups, n_segment): super(WeightConvNetNew, self).__init__() self.lastlayer = nn.Conv1d(in_channels, groups, 3, padding=1) self.groups = groups def forward(self, input_0): primals_2 = self.lastlayer.weight primals_3 = self.lastlayer.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
eynaij/X-Temporal_catdim
WeightConvNet
false
6,673
[ "MIT" ]
1
6a2efba407c09c83ca061c8467c1373b6ed0c7eb
https://github.com/eynaij/X-Temporal_catdim/tree/6a2efba407c09c83ca061c8467c1373b6ed0c7eb
DiceLoss
import torch import torch.nn as nn class DiceLoss(nn.Module): def __init__(self, smooth=1e-06): super(DiceLoss, self).__init__() self.smooth = smooth def forward(self, y_pred, y_true): assert y_pred.size() == y_true.size() y_pred = y_pred.contiguous().view(y_pred.shape[0], -1) y_true = y_true.contiguous().view(y_true.shape[0], -1) intersection = (y_pred * y_true).sum() union = y_pred.sum() + y_true.sum() dsc = (2.0 * intersection + self.smooth) / (union + self.smooth) return 1.0 - dsc 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_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 = 1e-06 tmp15 = tmp13 + tmp14 tmp16 = tmp8 + tmp11 tmp17 = tmp16 + tmp14 tmp18 = tmp15 / tmp17 tmp19 = 1.0 tmp20 = tmp19 - tmp18 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp20, 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, smooth=1e-06): super(DiceLossNew, self).__init__() self.smooth = smooth def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
fadamsyah/pytorch-brain-mri-segmentation
DiceLoss
false
6,674
[ "MIT" ]
1
bdb310ecacbddfce2cef20d50cf0b638dd1bc7b1
https://github.com/fadamsyah/pytorch-brain-mri-segmentation/tree/bdb310ecacbddfce2cef20d50cf0b638dd1bc7b1
CircleLoss
import torch from torch import Tensor from torch import nn class CircleLoss(nn.Module): def __init__(self, m: 'float', gamma: 'float') ->None: super(CircleLoss, self).__init__() self.m = m self.gamma = gamma self.soft_plus = nn.Softplus() def forward(self, sp: 'Tensor', sn: 'Tensor') ->Tensor: ap = torch.clamp_min(-sp.detach() + 1 + self.m, min=0.0) an = torch.clamp_min(sn.detach() + self.m, min=0.0) delta_p = 1 - self.m delta_n = self.m logit_p = -ap * (sp - delta_p) * self.gamma logit_n = an * (sn - delta_n) * self.gamma loss = self.soft_plus(torch.logsumexp(logit_n, dim=0) + torch. logsumexp(logit_p, dim=0)) return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'m': 4, 'gamma': 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, 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_poi_fused_add_clamp_min_logsumexp_mul_neg_softplus_sub_0(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 x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp8 = tl.load(in_ptr0 + (64 + x0), xmask) tmp15 = tl.load(in_ptr0 + (128 + x0), xmask) tmp22 = tl.load(in_ptr0 + (192 + x0), xmask) tmp44 = tl.load(in_ptr1 + x0, xmask) tmp55 = tl.load(in_ptr1 + (64 + x0), xmask) tmp65 = tl.load(in_ptr1 + (128 + x0), xmask) tmp75 = tl.load(in_ptr1 + (192 + x0), xmask) tmp1 = 4.0 tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = tmp0 - tmp1 tmp6 = tmp4 * tmp5 tmp7 = tmp6 * tmp1 tmp9 = tmp8 + tmp1 tmp10 = triton_helpers.maximum(tmp9, tmp3) tmp11 = tmp8 - tmp1 tmp12 = tmp10 * tmp11 tmp13 = tmp12 * tmp1 tmp14 = triton_helpers.maximum(tmp7, tmp13) tmp16 = tmp15 + tmp1 tmp17 = triton_helpers.maximum(tmp16, tmp3) tmp18 = tmp15 - tmp1 tmp19 = tmp17 * tmp18 tmp20 = tmp19 * tmp1 tmp21 = triton_helpers.maximum(tmp14, tmp20) tmp23 = tmp22 + tmp1 tmp24 = triton_helpers.maximum(tmp23, tmp3) tmp25 = tmp22 - tmp1 tmp26 = tmp24 * tmp25 tmp27 = tmp26 * tmp1 tmp28 = triton_helpers.maximum(tmp21, tmp27) tmp29 = tl_math.abs(tmp28) tmp30 = float('inf') tmp31 = tmp29 == tmp30 tmp32 = tl.where(tmp31, tmp3, tmp28) tmp33 = tmp7 - tmp32 tmp34 = tl_math.exp(tmp33) tmp35 = tmp13 - tmp32 tmp36 = tl_math.exp(tmp35) tmp37 = tmp34 + tmp36 tmp38 = tmp20 - tmp32 tmp39 = tl_math.exp(tmp38) tmp40 = tmp37 + tmp39 tmp41 = tmp27 - tmp32 tmp42 = tl_math.exp(tmp41) tmp43 = tmp40 + tmp42 tmp45 = -tmp44 tmp46 = 1.0 tmp47 = tmp45 + tmp46 tmp48 = tmp47 + tmp1 tmp49 = triton_helpers.maximum(tmp48, tmp3) tmp50 = -tmp49 tmp51 = -3.0 tmp52 = tmp44 - tmp51 tmp53 = tmp50 * tmp52 tmp54 = tmp53 * tmp1 tmp56 = -tmp55 tmp57 = tmp56 + tmp46 tmp58 = tmp57 + tmp1 tmp59 = triton_helpers.maximum(tmp58, tmp3) tmp60 = -tmp59 tmp61 = tmp55 - tmp51 tmp62 = tmp60 * tmp61 tmp63 = tmp62 * tmp1 tmp64 = triton_helpers.maximum(tmp54, tmp63) tmp66 = -tmp65 tmp67 = tmp66 + tmp46 tmp68 = tmp67 + tmp1 tmp69 = triton_helpers.maximum(tmp68, tmp3) tmp70 = -tmp69 tmp71 = tmp65 - tmp51 tmp72 = tmp70 * tmp71 tmp73 = tmp72 * tmp1 tmp74 = triton_helpers.maximum(tmp64, tmp73) tmp76 = -tmp75 tmp77 = tmp76 + tmp46 tmp78 = tmp77 + tmp1 tmp79 = triton_helpers.maximum(tmp78, tmp3) tmp80 = -tmp79 tmp81 = tmp75 - tmp51 tmp82 = tmp80 * tmp81 tmp83 = tmp82 * tmp1 tmp84 = triton_helpers.maximum(tmp74, tmp83) tmp85 = tl_math.abs(tmp84) tmp86 = tmp85 == tmp30 tmp87 = tl.where(tmp86, tmp3, tmp84) tmp88 = tmp54 - tmp87 tmp89 = tl_math.exp(tmp88) tmp90 = tmp63 - tmp87 tmp91 = tl_math.exp(tmp90) tmp92 = tmp89 + tmp91 tmp93 = tmp73 - tmp87 tmp94 = tl_math.exp(tmp93) tmp95 = tmp92 + tmp94 tmp96 = tmp83 - tmp87 tmp97 = tl_math.exp(tmp96) tmp98 = tmp95 + tmp97 tmp99 = tl_math.log(tmp43) tmp100 = tmp99 + tmp32 tmp101 = tl_math.log(tmp98) tmp102 = tmp101 + tmp87 tmp103 = tmp100 + tmp102 tmp104 = tmp103 * tmp46 tmp105 = 20.0 tmp106 = tmp104 > tmp105 tmp107 = tl_math.exp(tmp104) tmp108 = libdevice.log1p(tmp107) tmp109 = tmp108 * tmp46 tmp110 = tl.where(tmp106, tmp103, tmp109) tl.store(in_out_ptr0 + x0, tmp110, 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) buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) buf5 = buf2 del buf2 get_raw_stream(0) triton_poi_fused_add_clamp_min_logsumexp_mul_neg_softplus_sub_0[grid (64)](buf5, arg1_1, arg0_1, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 del arg1_1 return buf5, class CircleLossNew(nn.Module): def __init__(self, m: 'float', gamma: 'float') ->None: super(CircleLossNew, self).__init__() self.m = m self.gamma = gamma self.soft_plus = nn.Softplus() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
fabiozappo/Person_reID_tensorrt
CircleLoss
false
6,675
[ "Apache-2.0" ]
1
164441f35777698274e7664a9aefcc8d54467dc3
https://github.com/fabiozappo/Person_reID_tensorrt/tree/164441f35777698274e7664a9aefcc8d54467dc3
Aggregation
import torch from torch import nn from torch.nn import * class Aggregation(nn.Module): """ Aggregation layer for the Dueling architecture. https://arxiv.org/abs/1511.06581 This layer computes a Q function by combining an estimate of V with an estimate of the advantage. The advantage is normalized by substracting the average advantage so that we can propertly """ def forward(self, value, advantages): return value + advantages - torch.mean(advantages, dim=1, keepdim=True) 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 import nn from torch.nn import * 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_mean_sub_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 x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x3, xmask) tmp3 = tl.load(in_ptr1 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr1 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr1 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp8 = tl.load(in_ptr1 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp9 = tmp7 + tmp8 tmp10 = 4.0 tmp11 = tmp9 / tmp10 tmp12 = tmp2 - tmp11 tl.store(out_ptr0 + x3, tmp12, 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_mean_sub_0[grid(256)](arg0_1, arg1_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf0, class AggregationNew(nn.Module): """ Aggregation layer for the Dueling architecture. https://arxiv.org/abs/1511.06581 This layer computes a Q function by combining an estimate of V with an estimate of the advantage. The advantage is normalized by substracting the average advantage so that we can propertly """ def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
ezelikman/autonomous-learning-library
Aggregation
false
6,676
[ "MIT" ]
1
b32d059ca8b191afe0b310102d0754796f391aff
https://github.com/ezelikman/autonomous-learning-library/tree/b32d059ca8b191afe0b310102d0754796f391aff
Entropy
import torch from torch import nn class Entropy(nn.Module): def __init__(self): super(Entropy, self).__init__() def forward(self, x): plogp = x * torch.log(x) plogp[plogp != plogp] = 0 return -torch.sum(plogp, dim=-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 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 @triton.jit def triton_poi_fused_index_put_lift_fresh_log_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 = tl_math.log(tmp0) tmp2 = tmp0 * tmp1 tmp3 = tmp2 != tmp2 tmp4 = 0.0 tmp5 = tl.where(tmp3, tmp4, tmp2) tl.store(out_ptr0 + x0, tmp5, xmask) @triton.jit def triton_poi_fused_neg_sum_1(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 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 = -tmp6 tl.store(out_ptr0 + x0, tmp7, 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_index_put_lift_fresh_log_mul_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), (16, 4, 1), torch.float32) triton_poi_fused_neg_sum_1[grid(64)](buf0, buf1, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf0 return buf1, class EntropyNew(nn.Module): def __init__(self): super(EntropyNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
fallcat/synst
Entropy
false
6,677
[ "BSD-3-Clause" ]
1
0fa4adffa825af4a62b6e739b59c4125a7b6698e
https://github.com/fallcat/synst/tree/0fa4adffa825af4a62b6e739b59c4125a7b6698e
QuaternionLinear
from torch.nn import Module import torch import numpy as np from numpy.random import RandomState from torch.autograd import Variable from torch.nn.parameter import Parameter from scipy.stats import chi import torch.fx def quaternion_init(in_features, out_features, rng, kernel_size=None, criterion='glorot'): if kernel_size is not None: receptive_field = np.prod(kernel_size) fan_in = in_features * receptive_field fan_out = out_features * receptive_field else: fan_in = in_features fan_out = out_features if criterion == 'glorot': s = 1.0 / np.sqrt(2 * (fan_in + fan_out)) elif criterion == 'he': s = 1.0 / np.sqrt(2 * fan_in) else: raise ValueError('Invalid criterion: ' + criterion) rng = RandomState(np.random.randint(1, 1234)) if kernel_size is None: kernel_shape = in_features, out_features elif type(kernel_size) is int: kernel_shape = (out_features, in_features) + tuple((kernel_size,)) else: kernel_shape = (out_features, in_features) + (*kernel_size,) modulus = chi.rvs(4, loc=0, scale=s, size=kernel_shape) number_of_weights = np.prod(kernel_shape) v_i = np.random.uniform(-1.0, 1.0, number_of_weights) v_j = np.random.uniform(-1.0, 1.0, number_of_weights) v_k = np.random.uniform(-1.0, 1.0, number_of_weights) for i in range(0, number_of_weights): norm = np.sqrt(v_i[i] ** 2 + v_j[i] ** 2 + v_k[i] ** 2 + 0.0001) v_i[i] /= norm v_j[i] /= norm v_k[i] /= norm v_i = v_i.reshape(kernel_shape) v_j = v_j.reshape(kernel_shape) v_k = v_k.reshape(kernel_shape) phase = rng.uniform(low=-np.pi, high=np.pi, size=kernel_shape) weight_r = modulus * np.cos(phase) weight_i = modulus * v_i * np.sin(phase) weight_j = modulus * v_j * np.sin(phase) weight_k = modulus * v_k * np.sin(phase) return weight_r, weight_i, weight_j, weight_k def unitary_init(in_features, out_features, rng, kernel_size=None, criterion='he'): if kernel_size is not None: receptive_field = np.prod(kernel_size) in_features * receptive_field out_features * receptive_field else: pass if kernel_size is None: kernel_shape = in_features, out_features elif type(kernel_size) is int: kernel_shape = (out_features, in_features) + tuple((kernel_size,)) else: kernel_shape = (out_features, in_features) + (*kernel_size,) number_of_weights = np.prod(kernel_shape) v_r = np.random.uniform(-1.0, 1.0, number_of_weights) v_i = np.random.uniform(-1.0, 1.0, number_of_weights) v_j = np.random.uniform(-1.0, 1.0, number_of_weights) v_k = np.random.uniform(-1.0, 1.0, number_of_weights) for i in range(0, number_of_weights): norm = np.sqrt(v_r[i] ** 2 + v_i[i] ** 2 + v_j[i] ** 2 + v_k[i] ** 2 ) + 0.0001 v_r[i] /= norm v_i[i] /= norm v_j[i] /= norm v_k[i] /= norm v_r = v_r.reshape(kernel_shape) v_i = v_i.reshape(kernel_shape) v_j = v_j.reshape(kernel_shape) v_k = v_k.reshape(kernel_shape) return v_r, v_i, v_j, v_k def affect_init(r_weight, i_weight, j_weight, k_weight, init_func, rng, init_criterion): if r_weight.size() != i_weight.size() or r_weight.size() != j_weight.size( ) or r_weight.size() != k_weight.size(): raise ValueError( 'The real and imaginary weights should have the same size . Found: r:' + str(r_weight.size()) + ' i:' + str(i_weight.size()) + ' j:' + str(j_weight.size()) + ' k:' + str(k_weight.size())) elif r_weight.dim() != 2: raise Exception( 'affect_init accepts only matrices. Found dimension = ' + str( r_weight.dim())) kernel_size = None r, i, j, k = init_func(r_weight.size(0), r_weight.size(1), rng, kernel_size, init_criterion) r, i, j, k = torch.from_numpy(r), torch.from_numpy(i), torch.from_numpy(j ), torch.from_numpy(k) r_weight.data = r.type_as(r_weight.data) i_weight.data = i.type_as(i_weight.data) j_weight.data = j.type_as(j_weight.data) k_weight.data = k.type_as(k_weight.data) def check_input(input): if input.dim() not in {2, 3, 4, 5}: raise RuntimeError( 'Quaternion linear accepts only input of dimension 2 or 3. Quaternion conv accepts up to 5 dim input.dim = ' + str(input.dim())) if input.dim() < 4: nb_hidden = input.size()[-1] else: nb_hidden = input.size()[1] if nb_hidden % 4 != 0: raise RuntimeError( 'Quaternion Tensors must be divisible by 4. input.size()[1] = ' + str(nb_hidden)) def get_i(input): if input.dim() < 4: nb_hidden = input.size()[-1] else: nb_hidden = input.size()[1] if input.dim() == 2: return input.narrow(1, nb_hidden // 4, nb_hidden // 4) if input.dim() == 3: return input.narrow(2, nb_hidden // 4, nb_hidden // 4) if input.dim() >= 4: return input.narrow(1, nb_hidden // 4, nb_hidden // 4) def get_j(input): check_input(input) if input.dim() < 4: nb_hidden = input.size()[-1] else: nb_hidden = input.size()[1] if input.dim() == 2: return input.narrow(1, nb_hidden // 2, nb_hidden // 4) if input.dim() == 3: return input.narrow(2, nb_hidden // 2, nb_hidden // 4) if input.dim() >= 4: return input.narrow(1, nb_hidden // 2, nb_hidden // 4) def get_k(input): check_input(input) if input.dim() < 4: nb_hidden = input.size()[-1] else: nb_hidden = input.size()[1] if input.dim() == 2: return input.narrow(1, nb_hidden - nb_hidden // 4, nb_hidden // 4) if input.dim() == 3: return input.narrow(2, nb_hidden - nb_hidden // 4, nb_hidden // 4) if input.dim() >= 4: return input.narrow(1, nb_hidden - nb_hidden // 4, nb_hidden // 4) def get_r(input): check_input(input) if input.dim() < 4: nb_hidden = input.size()[-1] else: nb_hidden = input.size()[1] if input.dim() == 2: return input.narrow(1, 0, nb_hidden // 4) if input.dim() == 3: return input.narrow(2, 0, nb_hidden // 4) if input.dim() >= 4: return input.narrow(1, 0, nb_hidden // 4) class QuaternionLinearFunction(torch.autograd.Function): @staticmethod def forward(ctx, input, r_weight, i_weight, j_weight, k_weight, bias=None): ctx.save_for_backward(input, r_weight, i_weight, j_weight, k_weight, bias) check_input(input) cat_kernels_4_r = torch.cat([r_weight, -i_weight, -j_weight, - k_weight], dim=0) cat_kernels_4_i = torch.cat([i_weight, r_weight, -k_weight, j_weight], dim=0) cat_kernels_4_j = torch.cat([j_weight, k_weight, r_weight, - i_weight], dim=0) cat_kernels_4_k = torch.cat([k_weight, -j_weight, i_weight, r_weight], dim=0) cat_kernels_4_quaternion = torch.cat([cat_kernels_4_r, cat_kernels_4_i, cat_kernels_4_j, cat_kernels_4_k], dim=1) if input.dim() == 2: if bias is not None: return torch.addmm(bias, input, cat_kernels_4_quaternion) else: return torch.mm(input, cat_kernels_4_quaternion) else: output = torch.matmul(input, cat_kernels_4_quaternion) if bias is not None: return output + bias else: return output @staticmethod def backward(ctx, grad_output): input, r_weight, i_weight, j_weight, k_weight, _bias = (ctx. saved_tensors) (grad_input) = (grad_weight_r) = (grad_weight_i) = (grad_weight_j) = ( grad_weight_k) = (grad_bias) = None input_r = torch.cat([r_weight, -i_weight, -j_weight, -k_weight], dim=0) input_i = torch.cat([i_weight, r_weight, -k_weight, j_weight], dim=0) input_j = torch.cat([j_weight, k_weight, r_weight, -i_weight], dim=0) input_k = torch.cat([k_weight, -j_weight, i_weight, r_weight], dim=0) cat_kernels_4_quaternion_T = Variable(torch.cat([input_r, input_i, input_j, input_k], dim=1).permute(1, 0), requires_grad=False) r = get_r(input) i = get_i(input) j = get_j(input) k = get_k(input) input_r = torch.cat([r, -i, -j, -k], dim=0) input_i = torch.cat([i, r, -k, j], dim=0) input_j = torch.cat([j, k, r, -i], dim=0) input_k = torch.cat([k, -j, i, r], dim=0) input_mat = Variable(torch.cat([input_r, input_i, input_j, input_k], dim=1), requires_grad=False) r = get_r(grad_output) i = get_i(grad_output) j = get_j(grad_output) k = get_k(grad_output) input_r = torch.cat([r, i, j, k], dim=1) input_i = torch.cat([-i, r, k, -j], dim=1) input_j = torch.cat([-j, -k, r, i], dim=1) input_k = torch.cat([-k, j, -i, r], dim=1) grad_mat = torch.cat([input_r, input_i, input_j, input_k], dim=0) if ctx.needs_input_grad[0]: grad_input = grad_output.mm(cat_kernels_4_quaternion_T) if ctx.needs_input_grad[1]: grad_weight = grad_mat.permute(1, 0).mm(input_mat).permute(1, 0) unit_size_x = r_weight.size(0) unit_size_y = r_weight.size(1) grad_weight_r = grad_weight.narrow(0, 0, unit_size_x).narrow(1, 0, unit_size_y) grad_weight_i = grad_weight.narrow(0, 0, unit_size_x).narrow(1, unit_size_y, unit_size_y) grad_weight_j = grad_weight.narrow(0, 0, unit_size_x).narrow(1, unit_size_y * 2, unit_size_y) grad_weight_k = grad_weight.narrow(0, 0, unit_size_x).narrow(1, unit_size_y * 3, unit_size_y) if ctx.needs_input_grad[5]: grad_bias = grad_output.sum(0).squeeze(0) return (grad_input, grad_weight_r, grad_weight_i, grad_weight_j, grad_weight_k, grad_bias) class QuaternionLinear(Module): """Applies a quaternion linear transformation to the incoming data. """ def __init__(self, in_features, out_features, bias=True, init_criterion ='he', weight_init='quaternion', seed=None): super(QuaternionLinear, self).__init__() self.in_features = in_features // 4 self.out_features = out_features // 4 self.r_weight = Parameter(torch.Tensor(self.in_features, self. out_features)) self.i_weight = Parameter(torch.Tensor(self.in_features, self. out_features)) self.j_weight = Parameter(torch.Tensor(self.in_features, self. out_features)) self.k_weight = Parameter(torch.Tensor(self.in_features, self. out_features)) if bias: self.bias = Parameter(torch.Tensor(self.out_features * 4)) else: self.register_parameter('bias', None) self.init_criterion = init_criterion self.weight_init = weight_init self.seed = seed if seed is not None else np.random.randint(0, 1234) self.rng = RandomState(self.seed) self.reset_parameters() def reset_parameters(self): winit = {'quaternion': quaternion_init, 'unitary': unitary_init}[self .weight_init] if self.bias is not None: self.bias.data.fill_(0) affect_init(self.r_weight, self.i_weight, self.j_weight, self. k_weight, winit, self.rng, self.init_criterion) def forward(self, input): if input.dim() == 3: T, N, C = input.size() input = input.view(T * N, C) output = QuaternionLinearFunction.apply(input, self.r_weight, self.i_weight, self.j_weight, self.k_weight, self.bias) output = output.view(T, N, output.size(1)) elif input.dim() == 2: output = QuaternionLinearFunction.apply(input, self.r_weight, self.i_weight, self.j_weight, self.k_weight, self.bias) else: raise NotImplementedError return output def __repr__(self): return self.__class__.__name__ + '(' + 'in_features=' + str(self. in_features) + ', out_features=' + str(self.out_features ) + ', bias=' + str(self.bias is not None ) + ', init_criterion=' + str(self.init_criterion ) + ', weight_init=' + str(self.weight_init) + ', seed=' + str(self .seed) + ')' def get_inputs(): return [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.nn import Module import numpy as np from numpy.random import RandomState from torch.autograd import Variable from torch.nn.parameter import Parameter from scipy.stats import chi import torch.fx 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, in_ptr2, in_ptr3, 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 tmp9 = tl.load(in_ptr0 + 0) tmp10 = tl.broadcast_to(tmp9, [XBLOCK]) tmp16 = tl.load(in_ptr1 + 0) tmp17 = tl.broadcast_to(tmp16, [XBLOCK]) tmp26 = tl.load(in_ptr2 + 0) tmp27 = tl.broadcast_to(tmp26, [XBLOCK]) tmp35 = tl.load(in_ptr3 + 0) tmp36 = tl.broadcast_to(tmp35, [XBLOCK]) tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = x1 tmp7 = tmp5 < tmp3 tmp7 & tmp4 tmp11 = tmp5 >= tmp3 tmp12 = tl.full([1], 2, tl.int64) tmp13 = tmp5 < tmp12 tmp14 = tmp11 & tmp13 tmp15 = tmp14 & tmp4 tmp18 = -tmp17 tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype) tmp20 = tl.where(tmp15, tmp18, tmp19) tmp21 = tmp5 >= tmp12 tmp22 = tl.full([1], 3, tl.int64) tmp23 = tmp5 < tmp22 tmp24 = tmp21 & tmp23 tmp25 = tmp24 & tmp4 tmp28 = -tmp27 tmp29 = tl.full(tmp28.shape, 0.0, tmp28.dtype) tmp30 = tl.where(tmp25, tmp28, tmp29) tmp31 = tmp5 >= tmp22 tl.full([1], 4, tl.int64) tmp34 = tmp31 & tmp4 tmp37 = -tmp36 tmp38 = tl.full(tmp37.shape, 0.0, tmp37.dtype) tmp39 = tl.where(tmp34, tmp37, tmp38) tmp40 = tl.where(tmp24, tmp30, tmp39) tmp41 = tl.where(tmp14, tmp20, tmp40) tmp42 = tl.where(tmp7, tmp10, tmp41) tmp43 = tl.full(tmp42.shape, 0.0, tmp42.dtype) tmp44 = tl.where(tmp4, tmp42, tmp43) tmp45 = tmp0 >= tmp3 tmp46 = tmp0 < tmp12 tmp47 = tmp45 & tmp46 tmp7 & tmp47 tmp14 & tmp47 tmp50 = tmp24 & tmp47 tmp51 = tl.where(tmp50, tmp37, tmp38) tmp31 & tmp47 tmp53 = tl.where(tmp24, tmp51, tmp27) tmp54 = tl.where(tmp14, tmp10, tmp53) tmp55 = tl.where(tmp7, tmp17, tmp54) tmp56 = tl.full(tmp55.shape, 0.0, tmp55.dtype) tmp57 = tl.where(tmp47, tmp55, tmp56) tmp58 = tmp0 >= tmp12 tmp59 = tmp0 < tmp22 tmp60 = tmp58 & tmp59 tmp7 & tmp60 tmp14 & tmp60 tmp24 & tmp60 tmp64 = tmp31 & tmp60 tmp65 = tl.where(tmp64, tmp18, tmp19) tmp66 = tl.where(tmp24, tmp10, tmp65) tmp67 = tl.where(tmp14, tmp36, tmp66) tmp68 = tl.where(tmp7, tmp27, tmp67) tmp69 = tl.full(tmp68.shape, 0.0, tmp68.dtype) tmp70 = tl.where(tmp60, tmp68, tmp69) tmp71 = tmp0 >= tmp22 tmp7 & tmp71 tmp74 = tmp14 & tmp71 tmp75 = tl.where(tmp74, tmp28, tmp29) tmp24 & tmp71 tmp31 & tmp71 tmp78 = tl.where(tmp24, tmp17, tmp10) tmp79 = tl.where(tmp14, tmp75, tmp78) tmp80 = tl.where(tmp7, tmp36, tmp79) tmp81 = tl.full(tmp80.shape, 0.0, tmp80.dtype) tmp82 = tl.where(tmp71, tmp80, tmp81) tmp83 = tl.where(tmp60, tmp70, tmp82) tmp84 = tl.where(tmp47, tmp57, tmp83) tmp85 = tl.where(tmp4, tmp44, tmp84) tl.store(out_ptr0 + x2, tmp85, xmask) def call(args): arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1 = args args.clear() assert_size_stride(arg0_1, (4, 4), (4, 1)) assert_size_stride(arg1_1, (1, 1), (1, 1)) assert_size_stride(arg2_1, (1, 1), (1, 1)) assert_size_stride(arg3_1, (1, 1), (1, 1)) assert_size_stride(arg4_1, (1, 1), (1, 1)) assert_size_stride(arg5_1, (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_cat_0[grid(16)](arg4_1, arg1_1, arg2_1, arg3_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg1_1 del arg2_1 del arg3_1 del arg4_1 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(arg5_1, arg0_1, buf0, alpha=1, beta=1, out=buf1) del arg0_1 del arg5_1 del buf0 return buf1, def quaternion_init(in_features, out_features, rng, kernel_size=None, criterion='glorot'): if kernel_size is not None: receptive_field = np.prod(kernel_size) fan_in = in_features * receptive_field fan_out = out_features * receptive_field else: fan_in = in_features fan_out = out_features if criterion == 'glorot': s = 1.0 / np.sqrt(2 * (fan_in + fan_out)) elif criterion == 'he': s = 1.0 / np.sqrt(2 * fan_in) else: raise ValueError('Invalid criterion: ' + criterion) rng = RandomState(np.random.randint(1, 1234)) if kernel_size is None: kernel_shape = in_features, out_features elif type(kernel_size) is int: kernel_shape = (out_features, in_features) + tuple((kernel_size,)) else: kernel_shape = (out_features, in_features) + (*kernel_size,) modulus = chi.rvs(4, loc=0, scale=s, size=kernel_shape) number_of_weights = np.prod(kernel_shape) v_i = np.random.uniform(-1.0, 1.0, number_of_weights) v_j = np.random.uniform(-1.0, 1.0, number_of_weights) v_k = np.random.uniform(-1.0, 1.0, number_of_weights) for i in range(0, number_of_weights): norm = np.sqrt(v_i[i] ** 2 + v_j[i] ** 2 + v_k[i] ** 2 + 0.0001) v_i[i] /= norm v_j[i] /= norm v_k[i] /= norm v_i = v_i.reshape(kernel_shape) v_j = v_j.reshape(kernel_shape) v_k = v_k.reshape(kernel_shape) phase = rng.uniform(low=-np.pi, high=np.pi, size=kernel_shape) weight_r = modulus * np.cos(phase) weight_i = modulus * v_i * np.sin(phase) weight_j = modulus * v_j * np.sin(phase) weight_k = modulus * v_k * np.sin(phase) return weight_r, weight_i, weight_j, weight_k def unitary_init(in_features, out_features, rng, kernel_size=None, criterion='he'): if kernel_size is not None: receptive_field = np.prod(kernel_size) in_features * receptive_field out_features * receptive_field else: pass if kernel_size is None: kernel_shape = in_features, out_features elif type(kernel_size) is int: kernel_shape = (out_features, in_features) + tuple((kernel_size,)) else: kernel_shape = (out_features, in_features) + (*kernel_size,) number_of_weights = np.prod(kernel_shape) v_r = np.random.uniform(-1.0, 1.0, number_of_weights) v_i = np.random.uniform(-1.0, 1.0, number_of_weights) v_j = np.random.uniform(-1.0, 1.0, number_of_weights) v_k = np.random.uniform(-1.0, 1.0, number_of_weights) for i in range(0, number_of_weights): norm = np.sqrt(v_r[i] ** 2 + v_i[i] ** 2 + v_j[i] ** 2 + v_k[i] ** 2 ) + 0.0001 v_r[i] /= norm v_i[i] /= norm v_j[i] /= norm v_k[i] /= norm v_r = v_r.reshape(kernel_shape) v_i = v_i.reshape(kernel_shape) v_j = v_j.reshape(kernel_shape) v_k = v_k.reshape(kernel_shape) return v_r, v_i, v_j, v_k def affect_init(r_weight, i_weight, j_weight, k_weight, init_func, rng, init_criterion): if r_weight.size() != i_weight.size() or r_weight.size() != j_weight.size( ) or r_weight.size() != k_weight.size(): raise ValueError( 'The real and imaginary weights should have the same size . Found: r:' + str(r_weight.size()) + ' i:' + str(i_weight.size()) + ' j:' + str(j_weight.size()) + ' k:' + str(k_weight.size())) elif r_weight.dim() != 2: raise Exception( 'affect_init accepts only matrices. Found dimension = ' + str( r_weight.dim())) kernel_size = None r, i, j, k = init_func(r_weight.size(0), r_weight.size(1), rng, kernel_size, init_criterion) r, i, j, k = torch.from_numpy(r), torch.from_numpy(i), torch.from_numpy(j ), torch.from_numpy(k) r_weight.data = r.type_as(r_weight.data) i_weight.data = i.type_as(i_weight.data) j_weight.data = j.type_as(j_weight.data) k_weight.data = k.type_as(k_weight.data) def check_input(input): if input.dim() not in {2, 3, 4, 5}: raise RuntimeError( 'Quaternion linear accepts only input of dimension 2 or 3. Quaternion conv accepts up to 5 dim input.dim = ' + str(input.dim())) if input.dim() < 4: nb_hidden = input.size()[-1] else: nb_hidden = input.size()[1] if nb_hidden % 4 != 0: raise RuntimeError( 'Quaternion Tensors must be divisible by 4. input.size()[1] = ' + str(nb_hidden)) def get_i(input): if input.dim() < 4: nb_hidden = input.size()[-1] else: nb_hidden = input.size()[1] if input.dim() == 2: return input.narrow(1, nb_hidden // 4, nb_hidden // 4) if input.dim() == 3: return input.narrow(2, nb_hidden // 4, nb_hidden // 4) if input.dim() >= 4: return input.narrow(1, nb_hidden // 4, nb_hidden // 4) def get_j(input): check_input(input) if input.dim() < 4: nb_hidden = input.size()[-1] else: nb_hidden = input.size()[1] if input.dim() == 2: return input.narrow(1, nb_hidden // 2, nb_hidden // 4) if input.dim() == 3: return input.narrow(2, nb_hidden // 2, nb_hidden // 4) if input.dim() >= 4: return input.narrow(1, nb_hidden // 2, nb_hidden // 4) def get_k(input): check_input(input) if input.dim() < 4: nb_hidden = input.size()[-1] else: nb_hidden = input.size()[1] if input.dim() == 2: return input.narrow(1, nb_hidden - nb_hidden // 4, nb_hidden // 4) if input.dim() == 3: return input.narrow(2, nb_hidden - nb_hidden // 4, nb_hidden // 4) if input.dim() >= 4: return input.narrow(1, nb_hidden - nb_hidden // 4, nb_hidden // 4) def get_r(input): check_input(input) if input.dim() < 4: nb_hidden = input.size()[-1] else: nb_hidden = input.size()[1] if input.dim() == 2: return input.narrow(1, 0, nb_hidden // 4) if input.dim() == 3: return input.narrow(2, 0, nb_hidden // 4) if input.dim() >= 4: return input.narrow(1, 0, nb_hidden // 4) class QuaternionLinearFunction(torch.autograd.Function): @staticmethod def forward(ctx, input, r_weight, i_weight, j_weight, k_weight, bias=None): ctx.save_for_backward(input, r_weight, i_weight, j_weight, k_weight, bias) check_input(input) cat_kernels_4_r = torch.cat([r_weight, -i_weight, -j_weight, - k_weight], dim=0) cat_kernels_4_i = torch.cat([i_weight, r_weight, -k_weight, j_weight], dim=0) cat_kernels_4_j = torch.cat([j_weight, k_weight, r_weight, - i_weight], dim=0) cat_kernels_4_k = torch.cat([k_weight, -j_weight, i_weight, r_weight], dim=0) cat_kernels_4_quaternion = torch.cat([cat_kernels_4_r, cat_kernels_4_i, cat_kernels_4_j, cat_kernels_4_k], dim=1) if input.dim() == 2: if bias is not None: return torch.addmm(bias, input, cat_kernels_4_quaternion) else: return torch.mm(input, cat_kernels_4_quaternion) else: output = torch.matmul(input, cat_kernels_4_quaternion) if bias is not None: return output + bias else: return output @staticmethod def backward(ctx, grad_output): input, r_weight, i_weight, j_weight, k_weight, _bias = (ctx. saved_tensors) (grad_input) = (grad_weight_r) = (grad_weight_i) = (grad_weight_j) = ( grad_weight_k) = (grad_bias) = None input_r = torch.cat([r_weight, -i_weight, -j_weight, -k_weight], dim=0) input_i = torch.cat([i_weight, r_weight, -k_weight, j_weight], dim=0) input_j = torch.cat([j_weight, k_weight, r_weight, -i_weight], dim=0) input_k = torch.cat([k_weight, -j_weight, i_weight, r_weight], dim=0) cat_kernels_4_quaternion_T = Variable(torch.cat([input_r, input_i, input_j, input_k], dim=1).permute(1, 0), requires_grad=False) r = get_r(input) i = get_i(input) j = get_j(input) k = get_k(input) input_r = torch.cat([r, -i, -j, -k], dim=0) input_i = torch.cat([i, r, -k, j], dim=0) input_j = torch.cat([j, k, r, -i], dim=0) input_k = torch.cat([k, -j, i, r], dim=0) input_mat = Variable(torch.cat([input_r, input_i, input_j, input_k], dim=1), requires_grad=False) r = get_r(grad_output) i = get_i(grad_output) j = get_j(grad_output) k = get_k(grad_output) input_r = torch.cat([r, i, j, k], dim=1) input_i = torch.cat([-i, r, k, -j], dim=1) input_j = torch.cat([-j, -k, r, i], dim=1) input_k = torch.cat([-k, j, -i, r], dim=1) grad_mat = torch.cat([input_r, input_i, input_j, input_k], dim=0) if ctx.needs_input_grad[0]: grad_input = grad_output.mm(cat_kernels_4_quaternion_T) if ctx.needs_input_grad[1]: grad_weight = grad_mat.permute(1, 0).mm(input_mat).permute(1, 0) unit_size_x = r_weight.size(0) unit_size_y = r_weight.size(1) grad_weight_r = grad_weight.narrow(0, 0, unit_size_x).narrow(1, 0, unit_size_y) grad_weight_i = grad_weight.narrow(0, 0, unit_size_x).narrow(1, unit_size_y, unit_size_y) grad_weight_j = grad_weight.narrow(0, 0, unit_size_x).narrow(1, unit_size_y * 2, unit_size_y) grad_weight_k = grad_weight.narrow(0, 0, unit_size_x).narrow(1, unit_size_y * 3, unit_size_y) if ctx.needs_input_grad[5]: grad_bias = grad_output.sum(0).squeeze(0) return (grad_input, grad_weight_r, grad_weight_i, grad_weight_j, grad_weight_k, grad_bias) class QuaternionLinearNew(Module): """Applies a quaternion linear transformation to the incoming data. """ def __init__(self, in_features, out_features, bias=True, init_criterion ='he', weight_init='quaternion', seed=None): super(QuaternionLinearNew, self).__init__() self.in_features = in_features // 4 self.out_features = out_features // 4 self.r_weight = Parameter(torch.Tensor(self.in_features, self. out_features)) self.i_weight = Parameter(torch.Tensor(self.in_features, self. out_features)) self.j_weight = Parameter(torch.Tensor(self.in_features, self. out_features)) self.k_weight = Parameter(torch.Tensor(self.in_features, self. out_features)) if bias: self.bias = Parameter(torch.Tensor(self.out_features * 4)) else: self.register_parameter('bias', None) self.init_criterion = init_criterion self.weight_init = weight_init self.seed = seed if seed is not None else np.random.randint(0, 1234) self.rng = RandomState(self.seed) self.reset_parameters() def reset_parameters(self): winit = {'quaternion': quaternion_init, 'unitary': unitary_init}[self .weight_init] if self.bias is not None: self.bias.data.fill_(0) affect_init(self.r_weight, self.i_weight, self.j_weight, self. k_weight, winit, self.rng, self.init_criterion) def __repr__(self): return self.__class__.__name__ + '(' + 'in_features=' + str(self. in_features) + ', out_features=' + str(self.out_features ) + ', bias=' + str(self.bias is not None ) + ', init_criterion=' + str(self.init_criterion ) + ', weight_init=' + str(self.weight_init) + ', seed=' + str(self .seed) + ')' def forward(self, input_0): arg1_1 = self.r_weight arg2_1 = self.i_weight arg3_1 = self.j_weight arg4_1 = self.k_weight arg5_1 = self.bias arg0_1 = input_0 output = call([arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1]) return output[0]
eleGAN23/HI2I
QuaternionLinear
false
6,678
[ "MIT" ]
1
7730ee0963614290099b011c113048ef6d1b149c
https://github.com/eleGAN23/HI2I/tree/7730ee0963614290099b011c113048ef6d1b149c
MultiHeadedAttention
import torch from torch import nn from torch.nn import functional as F def same_tensor(tensor, *args): """ Do the input tensors all point to the same underlying data """ for other in args: if not torch.is_tensor(other): return False if tensor.device != other.device: return False if tensor.dtype != other.dtype: return False if tensor.data_ptr() != other.data_ptr(): return False return True class MultiHeadedAttention(nn.Module): """ Implement a multi-headed attention module """ def __init__(self, embed_dim, num_heads=1): """ Initialize the attention module """ super(MultiHeadedAttention, self).__init__() assert embed_dim % num_heads == 0, f'num_heads={num_heads} should evenly divide embed_dim={embed_dim}' self.embed_dim = embed_dim self.num_heads = num_heads self.projection_dim = embed_dim // num_heads self.scale = self.projection_dim ** -0.5 self.input_weights = nn.Parameter(torch.Tensor(3 * embed_dim, embed_dim)) self.output_projection = nn.Linear(embed_dim, embed_dim, bias=False) self.reset_parameters() def reset_parameters(self): """ Reset parameters using xavier initialization """ gain = nn.init.calculate_gain('linear') nn.init.xavier_uniform_(self.input_weights, gain) nn.init.xavier_uniform_(self.output_projection.weight, gain) def project(self, inputs, index=0, chunks=1): """ Produce a linear projection using the weights """ batch_size = inputs.shape[0] start = index * self.embed_dim end = start + chunks * self.embed_dim projections = F.linear(inputs, self.input_weights[start:end]).chunk( chunks, dim=-1) output_projections = [] for projection in projections: output_projections.append(projection.view(batch_size, -1, self. num_heads, self.projection_dim).transpose(2, 1).contiguous( ).view(batch_size * self.num_heads, -1, self.projection_dim)) return output_projections def attention(self, values, keys, queries, key_mask=None, mask=None): """ Scaled dot product attention with optional masks """ logits = self.scale * torch.bmm(queries, keys.transpose(2, 1)) if mask is not None: logits += mask if key_mask is not None: logits_shape = logits.shape batch_size = logits_shape[0] // self.num_heads logits = logits.view(batch_size, self.num_heads, logits_shape[1 ], logits_shape[2]) logits.masked_fill_(key_mask[:, None, None], float('-inf')) logits = logits.view(logits_shape) attn_weights = F.softmax(logits, dim=-1) attended = torch.bmm(attn_weights, values) batch_size = queries.shape[0] // self.num_heads return attended.view(batch_size, self.num_heads, -1, self. projection_dim).transpose(2, 1).contiguous().view(batch_size, - 1, self.num_heads * self.projection_dim), attn_weights.view( batch_size, self.num_heads, attn_weights.size()[1], -1).transpose( 0, 1).contiguous() def forward(self, values, keys, queries, key_mask=None, attention_mask= None, num_queries=0): """ Forward pass of the attention """ if same_tensor(values, keys, queries): values, keys, queries = self.project(values, chunks=3) elif same_tensor(values, keys): values, keys = self.project(values, chunks=2) queries, = self.project(queries, 2) else: values, = self.project(values, 0) keys, = self.project(keys, 1) queries, = self.project(queries, 2) if num_queries: queries = queries[:, -num_queries:] attended, attn_weights = self.attention(values, keys, queries, key_mask, attention_mask) return self.output_projection(attended), attn_weights 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 [[], {'embed_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 math as tl_math from torch import nn from torch.nn import functional as F 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__softmax_0(in_ptr0, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 64 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 = 1.0 tmp2 = tmp0 * tmp1 tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp5 = tl.where(xmask, tmp3, float('-inf')) tmp6 = triton_helpers.max2(tmp5, 1)[:, None] tmp7 = tmp2 - tmp6 tmp8 = 0.5 tmp9 = tmp7 * tmp8 tmp10 = tl_math.exp(tmp9) tmp11 = tl.broadcast_to(tmp10, [XBLOCK, RBLOCK]) tmp13 = tl.where(xmask, tmp11, 0) tmp14 = tl.sum(tmp13, 1)[:, None] tmp15 = tmp10 / tmp14 tl.store(out_ptr2 + (r1 + 16 * x0), tmp15, 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, (12, 4), (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)) assert_size_stride(primals_5, (4, 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) buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 16), out=buf1) buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_4, (64, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 32), out=buf2) del primals_2 buf3 = empty_strided_cuda((4, 16, 16), (256, 16, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf2, (4, 16, 4), (64, 4, 1), 0), reinterpret_tensor(buf1, (4, 4, 16), (64, 1, 4), 0), out=buf3) buf6 = empty_strided_cuda((4, 16, 16), (256, 16, 1), torch.float32) get_raw_stream(0) triton_per_fused__softmax_0[grid(64)](buf3, buf6, 64, 16, XBLOCK=8, num_warps=2, num_stages=1) del buf3 buf7 = empty_strided_cuda((4, 16, 4), (64, 4, 1), torch.float32) extern_kernels.bmm(buf6, reinterpret_tensor(buf0, (4, 16, 4), (64, 4, 1), 0), out=buf7) buf8 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf7, (64, 4), (4, 1), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf8) return reinterpret_tensor(buf8, (4, 16, 4), (64, 4, 1), 0 ), reinterpret_tensor(buf6, (1, 4, 16, 16), (256, 256, 16, 1), 0 ), reinterpret_tensor(primals_1, (64, 4), (4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(primals_4, (64, 4), (4, 1), 0 ), buf6, reinterpret_tensor(buf7, (64, 4), (4, 1), 0 ), primals_5, reinterpret_tensor(buf0, (4, 4, 16), (64, 1, 4), 0 ), reinterpret_tensor(buf2, (4, 4, 16), (64, 1, 4), 0 ), reinterpret_tensor(buf1, (4, 16, 4), (64, 4, 1), 0) def same_tensor(tensor, *args): """ Do the input tensors all point to the same underlying data """ for other in args: if not torch.is_tensor(other): return False if tensor.device != other.device: return False if tensor.dtype != other.dtype: return False if tensor.data_ptr() != other.data_ptr(): return False return True class MultiHeadedAttentionNew(nn.Module): """ Implement a multi-headed attention module """ def __init__(self, embed_dim, num_heads=1): """ Initialize the attention module """ super(MultiHeadedAttentionNew, self).__init__() assert embed_dim % num_heads == 0, f'num_heads={num_heads} should evenly divide embed_dim={embed_dim}' self.embed_dim = embed_dim self.num_heads = num_heads self.projection_dim = embed_dim // num_heads self.scale = self.projection_dim ** -0.5 self.input_weights = nn.Parameter(torch.Tensor(3 * embed_dim, embed_dim)) self.output_projection = nn.Linear(embed_dim, embed_dim, bias=False) self.reset_parameters() def reset_parameters(self): """ Reset parameters using xavier initialization """ gain = nn.init.calculate_gain('linear') nn.init.xavier_uniform_(self.input_weights, gain) nn.init.xavier_uniform_(self.output_projection.weight, gain) def project(self, inputs, index=0, chunks=1): """ Produce a linear projection using the weights """ batch_size = inputs.shape[0] start = index * self.embed_dim end = start + chunks * self.embed_dim projections = F.linear(inputs, self.input_weights[start:end]).chunk( chunks, dim=-1) output_projections = [] for projection in projections: output_projections.append(projection.view(batch_size, -1, self. num_heads, self.projection_dim).transpose(2, 1).contiguous( ).view(batch_size * self.num_heads, -1, self.projection_dim)) return output_projections def attention(self, values, keys, queries, key_mask=None, mask=None): """ Scaled dot product attention with optional masks """ logits = self.scale * torch.bmm(queries, keys.transpose(2, 1)) if mask is not None: logits += mask if key_mask is not None: logits_shape = logits.shape batch_size = logits_shape[0] // self.num_heads logits = logits.view(batch_size, self.num_heads, logits_shape[1 ], logits_shape[2]) logits.masked_fill_(key_mask[:, None, None], float('-inf')) logits = logits.view(logits_shape) attn_weights = F.softmax(logits, dim=-1) attended = torch.bmm(attn_weights, values) batch_size = queries.shape[0] // self.num_heads return attended.view(batch_size, self.num_heads, -1, self. projection_dim).transpose(2, 1).contiguous().view(batch_size, - 1, self.num_heads * self.projection_dim), attn_weights.view( batch_size, self.num_heads, attn_weights.size()[1], -1).transpose( 0, 1).contiguous() def forward(self, input_0, input_1, input_2): primals_2 = self.input_weights primals_5 = self.output_projection.weight primals_1 = input_0 primals_3 = input_1 primals_4 = input_2 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0], output[1]
fallcat/synst
MultiHeadedAttention
false
6,679
[ "BSD-3-Clause" ]
1
0fa4adffa825af4a62b6e739b59c4125a7b6698e
https://github.com/fallcat/synst/tree/0fa4adffa825af4a62b6e739b59c4125a7b6698e
MultiLayeredConv1d
import torch import torch.nn class MultiLayeredConv1d(torch.nn.Module): """Multi-layered conv1d for Transformer block. This is a module of multi-leyered conv1d designed to replace positionwise feed-forward network in Transforner block, which is introduced in `FastSpeech: Fast, Robust and Controllable Text to Speech`_. .. _`FastSpeech: Fast, Robust and Controllable Text to Speech`: https://arxiv.org/pdf/1905.09263.pdf """ def __init__(self, in_chans, hidden_chans, kernel_size, dropout_rate): """Initialize MultiLayeredConv1d module. Args: in_chans (int): Number of input channels. hidden_chans (int): Number of hidden channels. kernel_size (int): Kernel size of conv1d. dropout_rate (float): Dropout rate. """ super(MultiLayeredConv1d, self).__init__() self.w_1 = torch.nn.Conv1d(in_chans, hidden_chans, kernel_size, stride=1, padding=(kernel_size - 1) // 2) self.w_2 = torch.nn.Conv1d(hidden_chans, in_chans, kernel_size, stride=1, padding=(kernel_size - 1) // 2) self.dropout = torch.nn.Dropout(dropout_rate) def forward(self, x): """Calculate forward propagation. Args: x (Tensor): Batch of input tensors (B, *, in_chans). Returns: Tensor: Batch of output tensors (B, *, hidden_chans) """ x = torch.relu(self.w_1(x.transpose(-1, 1))).transpose(-1, 1) return self.w_2(self.dropout(x).transpose(-1, 1)).transpose(-1, 1) def get_inputs(): return [torch.rand([4, 4])] def get_init_inputs(): return [[], {'in_chans': 4, 'hidden_chans': 4, 'kernel_size': 4, 'dropout_rate': 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 import triton_helpers import torch.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 = 12 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 3 tmp0 = tl.load(in_out_ptr0 + x2, 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) 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_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 8 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 2 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x1, 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, primals_5 = 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,), (1,)) assert_size_stride(primals_4, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(reinterpret_tensor(primals_1, (1, 4, 4), (16, 4, 1), 0), primals_2, stride=(1,), padding=(1,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf0, (1, 4, 3), (12, 3, 1)) buf1 = reinterpret_tensor(buf0, (4, 3), (3, 1), 0) del buf0 buf4 = empty_strided_cuda((4, 3), (3, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(12)](buf1, primals_3, buf4, 12, XBLOCK=16, num_warps=1, num_stages=1) del primals_3 buf2 = extern_kernels.convolution(reinterpret_tensor(buf1, (1, 4, 3 ), (0, 3, 1), 0), primals_4, stride=(1,), padding=(1,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf2, (1, 4, 2), (8, 2, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_1[grid(8)](buf3, primals_5, 8, XBLOCK= 8, num_warps=1, num_stages=1) del primals_5 return reinterpret_tensor(buf3, (4, 2), (2, 1), 0 ), primals_2, primals_4, reinterpret_tensor(primals_1, (1, 4, 4), ( 16, 4, 1), 0), reinterpret_tensor(buf1, (1, 4, 3), (12, 3, 1), 0), buf4 class MultiLayeredConv1dNew(torch.nn.Module): """Multi-layered conv1d for Transformer block. This is a module of multi-leyered conv1d designed to replace positionwise feed-forward network in Transforner block, which is introduced in `FastSpeech: Fast, Robust and Controllable Text to Speech`_. .. _`FastSpeech: Fast, Robust and Controllable Text to Speech`: https://arxiv.org/pdf/1905.09263.pdf """ def __init__(self, in_chans, hidden_chans, kernel_size, dropout_rate): """Initialize MultiLayeredConv1d module. Args: in_chans (int): Number of input channels. hidden_chans (int): Number of hidden channels. kernel_size (int): Kernel size of conv1d. dropout_rate (float): Dropout rate. """ super(MultiLayeredConv1dNew, self).__init__() self.w_1 = torch.nn.Conv1d(in_chans, hidden_chans, kernel_size, stride=1, padding=(kernel_size - 1) // 2) self.w_2 = torch.nn.Conv1d(hidden_chans, in_chans, kernel_size, stride=1, padding=(kernel_size - 1) // 2) self.dropout = torch.nn.Dropout(dropout_rate) def forward(self, input_0): primals_2 = self.w_1.weight primals_3 = self.w_1.bias primals_4 = self.w_2.weight primals_5 = self.w_2.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
fancyliumeng/asv-subtools
MultiLayeredConv1d
false
6,680
[ "Apache-2.0" ]
1
56a13484472e7ae6eb00d762c00d57e581e78eb4
https://github.com/fancyliumeng/asv-subtools/tree/56a13484472e7ae6eb00d762c00d57e581e78eb4
LDEPooling
import torch import torch.nn class LDEPooling(torch.nn.Module): """A novel learnable dictionary encoding layer according to [Weicheng Cai, etc., "A NOVEL LEARNABLE DICTIONARY ENCODING LAYER FOR END-TO-END LANGUAGE IDENTIFICATION", icassp, 2018]""" def __init__(self, input_dim, c_num=64): super(LDEPooling, self).__init__() self.input_dim = input_dim self.output_dim = input_dim * c_num self.mu = torch.nn.Parameter(torch.randn(input_dim, c_num)) self.s = torch.nn.Parameter(torch.ones(c_num)) self.softmax_for_w = torch.nn.Softmax(dim=3) def forward(self, inputs): """ @inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index] """ assert len(inputs.shape) == 3 assert inputs.shape[1] == self.input_dim r = inputs.transpose(1, 2).unsqueeze(3) - self.mu w = self.softmax_for_w(self.s * torch.sum(r ** 2, dim=2, keepdim=True)) e = torch.mean(w * r, dim=1) return e.reshape(-1, self.output_dim, 1) def get_output_dim(self): return self.output_dim def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 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 math as tl_math import torch.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__softmax_mul_pow_sub_sum_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 16 RBLOCK: tl.constexpr = 64 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 x0 = xindex % 4 x1 = xindex // 4 x3 = xindex tmp0 = tl.load(in_ptr0 + r2, None, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + (x0 + 16 * x1), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr2 + r2, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + (4 + x0 + 16 * x1), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr2 + (64 + r2), None, eviction_policy='evict_last') tmp10 = tl.load(in_ptr1 + (8 + x0 + 16 * x1), xmask, eviction_policy= 'evict_last') tmp11 = tl.load(in_ptr2 + (128 + r2), None, eviction_policy='evict_last') tmp15 = tl.load(in_ptr1 + (12 + x0 + 16 * x1), xmask, eviction_policy= 'evict_last') tmp16 = tl.load(in_ptr2 + (192 + r2), None, eviction_policy='evict_last') tmp3 = tmp1 - tmp2 tmp4 = tmp3 * tmp3 tmp7 = tmp5 - tmp6 tmp8 = tmp7 * tmp7 tmp9 = tmp4 + tmp8 tmp12 = tmp10 - tmp11 tmp13 = tmp12 * tmp12 tmp14 = tmp9 + tmp13 tmp17 = tmp15 - tmp16 tmp18 = tmp17 * tmp17 tmp19 = tmp14 + tmp18 tmp20 = tmp0 * tmp19 tmp21 = tl.broadcast_to(tmp20, [XBLOCK, RBLOCK]) tmp23 = tl.where(xmask, tmp21, float('-inf')) tmp24 = triton_helpers.max2(tmp23, 1)[:, None] tmp25 = tmp20 - tmp24 tmp26 = tl_math.exp(tmp25) tmp27 = tl.broadcast_to(tmp26, [XBLOCK, RBLOCK]) tmp29 = tl.where(xmask, tmp27, 0) tmp30 = tl.sum(tmp29, 1)[:, None] tl.store(out_ptr0 + (r2 + 64 * x3), tmp20, xmask) tl.store(out_ptr1 + x3, tmp24, xmask) tl.store(out_ptr2 + x3, tmp30, xmask) @triton.jit def triton_poi_fused__softmax_mean_mul_sub_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 % 64 x2 = xindex // 256 x4 = xindex // 64 x3 = xindex % 256 x5 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 256 * x2), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr1 + 4 * x2, xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr2 + 4 * x2, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr3 + 4 * x4, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x3, xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr0 + (64 + x0 + 256 * x2), xmask, eviction_policy= 'evict_last') tmp11 = tl.load(in_ptr1 + (1 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp14 = tl.load(in_ptr2 + (1 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp16 = tl.load(in_ptr3 + (1 + 4 * x4), xmask, eviction_policy='evict_last' ) tmp20 = tl.load(in_ptr0 + (128 + x0 + 256 * x2), xmask, eviction_policy ='evict_last') tmp21 = tl.load(in_ptr1 + (2 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp24 = tl.load(in_ptr2 + (2 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp26 = tl.load(in_ptr3 + (2 + 4 * x4), xmask, eviction_policy='evict_last' ) tmp30 = tl.load(in_ptr0 + (192 + x0 + 256 * x2), xmask, eviction_policy ='evict_last') tmp31 = tl.load(in_ptr1 + (3 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp34 = tl.load(in_ptr2 + (3 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp36 = tl.load(in_ptr3 + (3 + 4 * x4), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 - tmp1 tmp3 = tl_math.exp(tmp2) tmp5 = tmp3 / tmp4 tmp8 = tmp6 - tmp7 tmp9 = tmp5 * tmp8 tmp12 = tmp10 - tmp11 tmp13 = tl_math.exp(tmp12) tmp15 = tmp13 / tmp14 tmp17 = tmp16 - tmp7 tmp18 = tmp15 * tmp17 tmp19 = tmp9 + tmp18 tmp22 = tmp20 - tmp21 tmp23 = tl_math.exp(tmp22) tmp25 = tmp23 / tmp24 tmp27 = tmp26 - tmp7 tmp28 = tmp25 * tmp27 tmp29 = tmp19 + tmp28 tmp32 = tmp30 - tmp31 tmp33 = tl_math.exp(tmp32) tmp35 = tmp33 / tmp34 tmp37 = tmp36 - tmp7 tmp38 = tmp35 * tmp37 tmp39 = tmp29 + tmp38 tmp40 = 4.0 tmp41 = tmp39 / tmp40 tl.store(out_ptr0 + x5, tmp41, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 64), (64, 1)) assert_size_stride(primals_3, (64,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 1, 64), (256, 64, 1024, 1), torch. float32) buf1 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32) buf2 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32) get_raw_stream(0) triton_per_fused__softmax_mul_pow_sub_sum_0[grid(16)](primals_3, primals_1, primals_2, buf0, buf1, buf2, 16, 64, XBLOCK=1, num_warps=2, num_stages=1) buf3 = empty_strided_cuda((4, 4, 64), (256, 64, 1), torch.float32) triton_poi_fused__softmax_mean_mul_sub_1[grid(1024)](buf0, buf1, buf2, primals_1, primals_2, buf3, 1024, XBLOCK=128, num_warps=4, num_stages=1) del buf0 return reinterpret_tensor(buf3, (4, 256, 1), (256, 1, 1), 0 ), primals_1, primals_2, primals_3, buf1, buf2 class LDEPoolingNew(torch.nn.Module): """A novel learnable dictionary encoding layer according to [Weicheng Cai, etc., "A NOVEL LEARNABLE DICTIONARY ENCODING LAYER FOR END-TO-END LANGUAGE IDENTIFICATION", icassp, 2018]""" def __init__(self, input_dim, c_num=64): super(LDEPoolingNew, self).__init__() self.input_dim = input_dim self.output_dim = input_dim * c_num self.mu = torch.nn.Parameter(torch.randn(input_dim, c_num)) self.s = torch.nn.Parameter(torch.ones(c_num)) self.softmax_for_w = torch.nn.Softmax(dim=3) def get_output_dim(self): return self.output_dim def forward(self, input_0): primals_2 = self.mu primals_3 = self.s primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
fancyliumeng/asv-subtools
LDEPooling
false
6,681
[ "Apache-2.0" ]
1
56a13484472e7ae6eb00d762c00d57e581e78eb4
https://github.com/fancyliumeng/asv-subtools/tree/56a13484472e7ae6eb00d762c00d57e581e78eb4
Conv1dLinear
import torch import torch.nn class Conv1dLinear(torch.nn.Module): """Conv1D + Linear for Transformer block. A variant of MultiLayeredConv1d, which replaces second conv-layer to linear. """ def __init__(self, in_chans, hidden_chans, kernel_size, dropout_rate): """Initialize Conv1dLinear module. Args: in_chans (int): Number of input channels. hidden_chans (int): Number of hidden channels. kernel_size (int): Kernel size of conv1d. dropout_rate (float): Dropout rate. """ super(Conv1dLinear, self).__init__() self.w_1 = torch.nn.Conv1d(in_chans, hidden_chans, kernel_size, stride=1, padding=(kernel_size - 1) // 2) self.w_2 = torch.nn.Linear(hidden_chans, in_chans) self.dropout = torch.nn.Dropout(dropout_rate) def forward(self, x): """Calculate forward propagation. Args: x (Tensor): Batch of input tensors (B, *, in_chans). Returns: Tensor: Batch of output tensors (B, *, hidden_chans) """ x = torch.relu(self.w_1(x.transpose(-1, 1))).transpose(-1, 1) return self.w_2(self.dropout(x)) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'in_chans': 4, 'hidden_chans': 4, 'kernel_size': 4, 'dropout_rate': 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 import triton_helpers import torch.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_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_clone_1(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 12 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 % 3 y1 = yindex // 3 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 3 * x2 + 12 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask, 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 + (x2 + 4 * y3), tmp4, xmask & ymask) @triton.jit def triton_poi_fused_add_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 48 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_convolution_relu_threshold_backward_3(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 48 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 3 % 4 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 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(out_ptr0 + x3, 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), (16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4), (16, 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((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_convolution_0[grid(16, 4)](primals_1, buf0, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1,), padding=(1,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 3), (12, 3, 1)) del buf0 buf2 = empty_strided_cuda((4, 3, 4), (12, 4, 1), torch.float32) triton_poi_fused_clone_1[grid(12, 4)](buf1, primals_3, buf2, 12, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) buf3 = empty_strided_cuda((12, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf2, (12, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf3) buf4 = reinterpret_tensor(buf3, (4, 3, 4), (12, 4, 1), 0) del buf3 triton_poi_fused_add_2[grid(48)](buf4, primals_5, 48, XBLOCK=64, num_warps=1, num_stages=1) del primals_5 buf5 = empty_strided_cuda((4, 4, 3), (12, 3, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_3[grid(48)](buf1, primals_3, buf5, 48, XBLOCK=64, num_warps=1, num_stages=1) del buf1 del primals_3 return buf4, primals_2, reinterpret_tensor(primals_1, (4, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf2, (12, 4), (4, 1), 0), primals_4, buf5 class Conv1dLinearNew(torch.nn.Module): """Conv1D + Linear for Transformer block. A variant of MultiLayeredConv1d, which replaces second conv-layer to linear. """ def __init__(self, in_chans, hidden_chans, kernel_size, dropout_rate): """Initialize Conv1dLinear module. Args: in_chans (int): Number of input channels. hidden_chans (int): Number of hidden channels. kernel_size (int): Kernel size of conv1d. dropout_rate (float): Dropout rate. """ super(Conv1dLinearNew, self).__init__() self.w_1 = torch.nn.Conv1d(in_chans, hidden_chans, kernel_size, stride=1, padding=(kernel_size - 1) // 2) self.w_2 = torch.nn.Linear(hidden_chans, in_chans) self.dropout = torch.nn.Dropout(dropout_rate) def forward(self, input_0): primals_1 = self.w_1.weight primals_3 = self.w_1.bias primals_4 = self.w_2.weight primals_5 = self.w_2.bias primals_2 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
fancyliumeng/asv-subtools
Conv1dLinear
false
6,682
[ "Apache-2.0" ]
1
56a13484472e7ae6eb00d762c00d57e581e78eb4
https://github.com/fancyliumeng/asv-subtools/tree/56a13484472e7ae6eb00d762c00d57e581e78eb4
TdnnAffine
import torch import torch.nn.functional as F import torch.nn def to_device(device_object, tensor): """ Select device for non-parameters tensor w.r.t model or tensor which has been specified a device. """ if isinstance(device_object, torch.nn.Module): next(device_object.parameters()).device elif isinstance(device_object, torch.Tensor): pass return tensor class TdnnAffine(torch.nn.Module): """ An implemented tdnn affine component by conv1d y = splice(w * x, context) + b @input_dim: number of dims of frame <=> inputs channels of conv @output_dim: number of layer nodes <=> outputs channels of conv @context: a list of context e.g. [-2,0,2] If context is [0], then the TdnnAffine is equal to linear layer. """ def __init__(self, input_dim, output_dim, context=[0], bias=True, pad= True, norm_w=False, norm_f=False, subsampling_factor=1): super(TdnnAffine, self).__init__() for index in range(0, len(context) - 1): if context[index] >= context[index + 1]: raise ValueError( 'Context tuple {} is invalid, such as the order.'. format(context)) self.input_dim = input_dim self.output_dim = output_dim self.context = context self.bool_bias = bias self.pad = pad self.norm_w = norm_w self.norm_f = norm_f self.stride = subsampling_factor self.left_context = context[0] if context[0] < 0 else 0 self.right_context = context[-1] if context[-1] > 0 else 0 self.tot_context = self.right_context - self.left_context + 1 if self.tot_context > 1 and self.norm_f: self.norm_f = False None kernel_size = self.tot_context, self.weight = torch.nn.Parameter(torch.randn(output_dim, input_dim, *kernel_size)) if self.bool_bias: self.bias = torch.nn.Parameter(torch.randn(output_dim)) else: self.register_parameter('bias', None) self.init_weight() if len(context) != self.tot_context: self.mask = torch.tensor([[[(1 if index in context else 0) for index in range(self.left_context, self.right_context + 1)]]]) else: self.mask = None self.selected_device = False def init_weight(self): torch.nn.init.normal_(self.weight, 0.0, 0.01) if self.bias is not None: torch.nn.init.constant_(self.bias, 0.0) def forward(self, inputs): """ @inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index] """ assert len(inputs.shape) == 3 assert inputs.shape[1] == self.input_dim if self.pad: inputs = F.pad(inputs, (-self.left_context, self.right_context), mode='constant', value=0) assert inputs.shape[2] >= self.tot_context if not self.selected_device and self.mask is not None: self.mask = to_device(self, self.mask) self.selected_device = True filters = (self.weight * self.mask if self.mask is not None else self.weight) if self.norm_w: filters = F.normalize(filters, dim=1) if self.norm_f: inputs = F.normalize(inputs, dim=1) outputs = F.conv1d(inputs, filters, self.bias, self.stride, padding =0, dilation=1, groups=1) return outputs def extra_repr(self): return ( '{input_dim}, {output_dim}, context={context}, bias={bool_bias}, stride={stride}, pad={pad}, norm_w={norm_w}, norm_f={norm_f}' .format(**self.__dict__)) @classmethod def thop_count(self, m, x, y): x = x[0] kernel_ops = torch.zeros(m.weight.size()[2:]).numel() bias_ops = 1 if m.bias is not None else 0 total_ops = y.nelement() * (m.input_dim * kernel_ops + bias_ops) m.total_ops += torch.DoubleTensor([int(total_ops)]) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'output_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 import torch.nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride @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) def call(args): primals_1, primals_2, primals_3 = 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,)) 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 return buf1, primals_1, primals_2 def to_device(device_object, tensor): """ Select device for non-parameters tensor w.r.t model or tensor which has been specified a device. """ if isinstance(device_object, torch.nn.Module): next(device_object.parameters()).device elif isinstance(device_object, torch.Tensor): pass return tensor class TdnnAffineNew(torch.nn.Module): """ An implemented tdnn affine component by conv1d y = splice(w * x, context) + b @input_dim: number of dims of frame <=> inputs channels of conv @output_dim: number of layer nodes <=> outputs channels of conv @context: a list of context e.g. [-2,0,2] If context is [0], then the TdnnAffine is equal to linear layer. """ def __init__(self, input_dim, output_dim, context=[0], bias=True, pad= True, norm_w=False, norm_f=False, subsampling_factor=1): super(TdnnAffineNew, self).__init__() for index in range(0, len(context) - 1): if context[index] >= context[index + 1]: raise ValueError( 'Context tuple {} is invalid, such as the order.'. format(context)) self.input_dim = input_dim self.output_dim = output_dim self.context = context self.bool_bias = bias self.pad = pad self.norm_w = norm_w self.norm_f = norm_f self.stride = subsampling_factor self.left_context = context[0] if context[0] < 0 else 0 self.right_context = context[-1] if context[-1] > 0 else 0 self.tot_context = self.right_context - self.left_context + 1 if self.tot_context > 1 and self.norm_f: self.norm_f = False None kernel_size = self.tot_context, self.weight = torch.nn.Parameter(torch.randn(output_dim, input_dim, *kernel_size)) if self.bool_bias: self.bias = torch.nn.Parameter(torch.randn(output_dim)) else: self.register_parameter('bias', None) self.init_weight() if len(context) != self.tot_context: self.mask = torch.tensor([[[(1 if index in context else 0) for index in range(self.left_context, self.right_context + 1)]]]) else: self.mask = None self.selected_device = False def init_weight(self): torch.nn.init.normal_(self.weight, 0.0, 0.01) if self.bias is not None: torch.nn.init.constant_(self.bias, 0.0) def extra_repr(self): return ( '{input_dim}, {output_dim}, context={context}, bias={bool_bias}, stride={stride}, pad={pad}, norm_w={norm_w}, norm_f={norm_f}' .format(**self.__dict__)) @classmethod def thop_count(self, m, x, y): x = x[0] kernel_ops = torch.zeros(m.weight.size()[2:]).numel() bias_ops = 1 if m.bias is not None else 0 total_ops = y.nelement() * (m.input_dim * kernel_ops + bias_ops) m.total_ops += torch.DoubleTensor([int(total_ops)]) def forward(self, input_0): primals_2 = self.weight primals_3 = self.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
fancyliumeng/asv-subtools
TdnnAffine
false
6,683
[ "Apache-2.0" ]
1
56a13484472e7ae6eb00d762c00d57e581e78eb4
https://github.com/fancyliumeng/asv-subtools/tree/56a13484472e7ae6eb00d762c00d57e581e78eb4
UpBlok
import torch import torch.nn as nn import torch.nn.functional as F class UpBlok(nn.Module): def __init__(self, in_channels, out_channels): super().__init__() self.conv1x1 = nn.Conv2d(in_channels, in_channels, kernel_size=1, stride=1, padding=0) self.conv3x3 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1) self.deconv = nn.ConvTranspose2d(out_channels, out_channels, kernel_size=4, stride=2, padding=1) def forward(self, upsampled, shortcut): x = torch.cat([upsampled, shortcut], dim=1) x = self.conv1x1(x) x = F.relu(x) x = self.conv3x3(x) x = F.relu(x) x = self.deconv(x) return x def get_inputs(): return [torch.rand([4, 1, 4, 4]), torch.rand([4, 3, 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 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_cat_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 // 16 % 4 x0 = xindex % 16 x2 = xindex // 64 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 16 * x2), tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 4, tl.int64) tmp9 = tl.load(in_ptr1 + (x0 + 16 * (-1 + x1) + 48 * x2), tmp6 & xmask, other=0.0) tmp10 = tl.where(tmp4, tmp5, tmp9) tl.store(out_ptr0 + x3, tmp10, xmask) @triton.jit def triton_poi_fused_convolution_relu_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 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') 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_convolution_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 64 % 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) 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, 1, 4, 4), (16, 16, 4, 1)) assert_size_stride(primals_2, (4, 3, 4, 4), (48, 16, 4, 1)) assert_size_stride(primals_3, (4, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_8, (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_cat_0[grid(256)](primals_1, primals_2, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 del primals_2 buf1 = extern_kernels.convolution(buf0, primals_3, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 4, 4), (64, 16, 4, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_relu_1[grid(256)](buf2, primals_4, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_4 buf3 = extern_kernels.convolution(buf2, primals_5, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf3, (4, 4, 4, 4), (64, 16, 4, 1)) buf4 = buf3 del buf3 triton_poi_fused_convolution_relu_1[grid(256)](buf4, primals_6, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_6 buf5 = extern_kernels.convolution(buf4, primals_7, stride=(2, 2), padding=(1, 1), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf5, (4, 4, 8, 8), (256, 64, 8, 1)) buf6 = buf5 del buf5 triton_poi_fused_convolution_2[grid(1024)](buf6, primals_8, 1024, XBLOCK=128, num_warps=4, num_stages=1) del primals_8 return buf6, primals_3, primals_5, primals_7, buf0, buf2, buf4 class UpBlokNew(nn.Module): def __init__(self, in_channels, out_channels): super().__init__() self.conv1x1 = nn.Conv2d(in_channels, in_channels, kernel_size=1, stride=1, padding=0) self.conv3x3 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1) self.deconv = nn.ConvTranspose2d(out_channels, out_channels, kernel_size=4, stride=2, padding=1) def forward(self, input_0, input_1): primals_3 = self.conv1x1.weight primals_4 = self.conv1x1.bias primals_5 = self.conv3x3.weight primals_6 = self.conv3x3.bias primals_7 = self.deconv.weight primals_8 = self.deconv.bias primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8]) return output[0]
fendaq/TextRSN
UpBlok
false
6,684
[ "MIT" ]
1
02a6bc06cd64b581414ed5065a8c93e0c68a807a
https://github.com/fendaq/TextRSN/tree/02a6bc06cd64b581414ed5065a8c93e0c68a807a
Critic
import torch import torch.nn as nn import torch.nn.functional as F class Critic(nn.Module): def __init__(self, state_dim): super(Critic, self).__init__() self.fc1 = nn.Linear(state_dim, 256) self.fc2 = nn.Linear(256, 256) self.fc3 = nn.Linear(256, 1) def forward(self, x): 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, 4, 4, 4])] def get_init_inputs(): return [[], {'state_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 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): 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) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) 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, (256, 4), (4, 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), (256, 1)) assert_size_stride(primals_5, (256,), (1,)) assert_size_stride(primals_6, (1, 256), (256, 1)) assert_size_stride(primals_7, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 256), (256, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 256), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 256), (4096, 1024, 256, 1), 0 ) del buf0 buf7 = empty_strided_cuda((4, 4, 4, 256), (4096, 1024, 256, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(16384)](buf1, primals_2, buf7, 16384, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 256), (256, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 256), (256, 1), 0), reinterpret_tensor(primals_4, (256, 256), (1, 256), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 256), (4096, 1024, 256, 1), 0 ) del buf2 buf6 = empty_strided_cuda((4, 4, 4, 256), (4096, 1024, 256, 1), torch.bool) triton_poi_fused_relu_threshold_backward_0[grid(16384)](buf3, primals_5, buf6, 16384, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf5 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 256), (256, 1), 0), reinterpret_tensor(primals_6, (256, 1), (1, 256), 0), alpha=1, beta=1, out=buf5) del primals_7 return reinterpret_tensor(buf5, (4, 4, 4, 1), (16, 4, 1, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 256), (256, 1), 0 ), reinterpret_tensor(buf3, (64, 256), (256, 1), 0 ), primals_6, buf6, primals_4, buf7 class CriticNew(nn.Module): def __init__(self, state_dim): super(CriticNew, self).__init__() self.fc1 = nn.Linear(state_dim, 256) self.fc2 = nn.Linear(256, 256) self.fc3 = nn.Linear(256, 1) 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]
fengzhengyong-github/Deep-reinforcement-learning-with-pytorch
Critic
false
6,685
[ "MIT" ]
1
3c56b601d14b0b0c8cde4b6bc6df5c1e8f298c7b
https://github.com/fengzhengyong-github/Deep-reinforcement-learning-with-pytorch/tree/3c56b601d14b0b0c8cde4b6bc6df5c1e8f298c7b
ScaledDotProductAttention
import torch import torch.nn as nn class ScaledDotProductAttention(nn.Module): def __init__(self, dropout: 'float'=0.0) ->None: super(ScaledDotProductAttention, self).__init__() self._dropout = nn.Dropout(dropout) self._softmax = nn.Softmax(dim=2) def forward(self, query: 'torch.Tensor', key: 'torch.Tensor', value: 'torch.Tensor', scale: 'float'=None, attn_mask: 'torch.Tensor'=None ) ->torch.Tensor: attn = torch.bmm(query, key.transpose(1, 2)) if scale is not None: attn *= scale if attn_mask is not None: attn = attn.masked_fill(attn_mask, -1e+32) attn = self._softmax(attn) attn = self._dropout(attn) context = torch.bmm(attn, value) return context, attn def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4]), torch.rand([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 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 = 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_1(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): arg0_1, arg1_1, arg2_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(arg2_1, (4, 4, 4), (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) extern_kernels.bmm(arg1_1, reinterpret_tensor(arg0_1, (4, 4, 4), ( 16, 1, 4), 0), out=buf0) del arg0_1 del arg1_1 buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused__softmax_0[grid(64)](buf0, buf1, 64, XBLOCK=64, num_warps=1, num_stages=1) buf2 = buf0 del buf0 triton_poi_fused__softmax_1[grid(64)](buf1, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) buf3 = buf1 del buf1 extern_kernels.bmm(buf2, arg2_1, out=buf3) del arg2_1 return buf3, buf2 class ScaledDotProductAttentionNew(nn.Module): def __init__(self, dropout: 'float'=0.0) ->None: super(ScaledDotProductAttentionNew, self).__init__() self._dropout = nn.Dropout(dropout) self._softmax = nn.Softmax(dim=2) 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]
fengtaoo/opmft
ScaledDotProductAttention
false
6,686
[ "MIT" ]
1
64f2a12c724295cd913eda02502f2e2a20f2dd55
https://github.com/fengtaoo/opmft/tree/64f2a12c724295cd913eda02502f2e2a20f2dd55
L_TV
import torch import torch.nn as nn import torch.optim class L_TV(nn.Module): def __init__(self, TVLoss_weight=1): super(L_TV, self).__init__() self.TVLoss_weight = TVLoss_weight def forward(self, x): batch_size = x.size()[0] h_x = x.size()[2] w_x = x.size()[3] count_h = (x.size()[2] - 1) * x.size()[3] count_w = x.size()[2] * (x.size()[3] - 1) h_tv = torch.pow(x[:, :, 1:, :] - x[:, :, :h_x - 1, :], 2).sum() w_tv = torch.pow(x[:, :, :, 1:] - x[:, :, :, :w_x - 1], 2).sum() return self.TVLoss_weight * 2 * (h_tv / count_h + w_tv / count_w ) / batch_size 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.nn as nn import torch.optim 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_sub_sum_0(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): rnumel = 192 RBLOCK: tl.constexpr = 256 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, :] rmask = rindex < rnumel r0 = rindex % 12 r1 = rindex // 12 r2 = rindex % 3 r3 = rindex // 3 tmp0 = tl.load(in_ptr0 + (4 + r0 + 16 * r1), rmask, other=0.0) tmp1 = tl.load(in_ptr0 + (r0 + 16 * r1), rmask, other=0.0) tmp8 = tl.load(in_ptr0 + (1 + r2 + 4 * r3), rmask, other=0.0) tmp9 = tl.load(in_ptr0 + (r2 + 4 * r3), rmask, other=0.0) tmp2 = tmp0 - tmp1 tmp3 = tmp2 * tmp2 tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.where(rmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp10 = tmp8 - tmp9 tmp11 = tmp10 * tmp10 tmp12 = tl.broadcast_to(tmp11, [XBLOCK, RBLOCK]) tmp14 = tl.where(rmask, tmp12, 0) tmp15 = tl.sum(tmp14, 1)[:, None] tmp16 = 0.08333333333333333 tmp17 = tmp7 * tmp16 tmp18 = tmp15 * tmp16 tmp19 = tmp17 + tmp18 tmp20 = 2.0 tmp21 = tmp19 * tmp20 tmp22 = 0.25 tmp23 = tmp21 * tmp22 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp23, 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) buf2 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_div_mul_pow_sub_sum_0[grid(1)](buf2, arg0_1, 1, 192, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 return buf2, class L_TVNew(nn.Module): def __init__(self, TVLoss_weight=1): super(L_TVNew, self).__init__() self.TVLoss_weight = TVLoss_weight def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
farhantandia/Applied-CV-Zero-DCE-master
L_TV
false
6,687
[ "MIT" ]
1
56a0f8aec799eb5d125f5d9f44f692b9a9a3c990
https://github.com/farhantandia/Applied-CV-Zero-DCE-master/tree/56a0f8aec799eb5d125f5d9f44f692b9a9a3c990
PixelNormLayer
import torch import torch.nn as nn class PixelNormLayer(nn.Module): """ Pixelwise feature vector normalization. """ def __init__(self, eps=1e-08): super(PixelNormLayer, self).__init__() self.eps = eps def forward(self, x): return x / torch.sqrt(torch.mean(x ** 2, dim=1, keepdim=True) + 0.0001) def __repr__(self): return self.__class__.__name__ + '(eps = %s)' % self.eps 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 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_mean_pow_sqrt_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 = 4.0 tmp13 = tmp11 / tmp12 tmp14 = 0.0001 tmp15 = tmp13 + tmp14 tmp16 = libdevice.sqrt(tmp15) tmp17 = tmp0 / tmp16 tl.store(out_ptr0 + x3, tmp17, 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_mean_pow_sqrt_0[grid(256)](arg0_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf0, class PixelNormLayerNew(nn.Module): """ Pixelwise feature vector normalization. """ def __init__(self, eps=1e-08): super(PixelNormLayerNew, self).__init__() self.eps = eps def __repr__(self): return self.__class__.__name__ + '(eps = %s)' % self.eps def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
ferngonzalezp/turbulence-GAN
PixelNormLayer
false
6,688
[ "MIT" ]
1
a215a3c5af2dc9a723f95c344e295ecc08954f26
https://github.com/ferngonzalezp/turbulence-GAN/tree/a215a3c5af2dc9a723f95c344e295ecc08954f26
CNN
import torch from torch.nn import functional as F from torch import nn class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv1 = nn.Conv2d(3, 8, 6, 1) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(8, 16, 6, 1) self.conv3 = nn.Conv2d(16, 24, 6, 1) self.fc1 = nn.Linear(24 * 3 * 3, 100) self.fc2 = nn.Linear(100, 32) self.fc3 = nn.Linear(32, 3) def forward(self, x): x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self.conv2(x))) x = self.pool(F.relu(self.conv3(x))) x = x.view(-1, 24 * 3 * 3) 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, 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 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_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 111392 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 3481 % 8 x0 = xindex % 3481 x4 = xindex // 3481 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) tl.store(out_ptr0 + (x0 + 3488 * x4), tmp4, xmask) @triton.jit def triton_poi_fused_max_pool2d_with_indices_1(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 26912 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 29 x1 = xindex // 29 % 29 x4 = xindex // 841 x3 = xindex // 6728 x5 = xindex % 6728 tmp0 = tl.load(in_ptr0 + (2 * x0 + 118 * x1 + 3488 * x4), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 118 * x1 + 3488 * x4), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (59 + 2 * x0 + 118 * x1 + 3488 * x4), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (60 + 2 * x0 + 118 * x1 + 3488 * x4), 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 + (x5 + 6752 * x3), tmp6, xmask) tl.store(out_ptr1 + (x5 + 6784 * x3), tmp16, xmask) @triton.jit def triton_poi_fused_convolution_relu_2(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 // 576 % 16 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_max_pool2d_with_indices_3(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 9216 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 12 x1 = xindex // 12 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 48 * x1), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 48 * x1), xmask, eviction_policy ='evict_last') tmp3 = tl.load(in_ptr0 + (24 + 2 * x0 + 48 * x1), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (25 + 2 * x0 + 48 * x1), 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 + x2, tmp6, xmask) tl.store(out_ptr1 + x2, tmp16, xmask) @triton.jit def triton_poi_fused_convolution_relu_4(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 4704 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 49 % 24 x2 = xindex // 1176 x4 = xindex % 1176 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) tl.store(out_ptr0 + (x4 + 1184 * x2), tmp4, xmask) @triton.jit def triton_poi_fused_max_pool2d_with_indices_5(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 864 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 3 x1 = xindex // 3 % 3 x2 = xindex // 9 % 24 x3 = xindex // 216 x4 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 14 * x1 + 49 * x2 + 1184 * x3), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 14 * x1 + 49 * x2 + 1184 * x3), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (7 + 2 * x0 + 14 * x1 + 49 * x2 + 1184 * x3), xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (8 + 2 * x0 + 14 * x1 + 49 * x2 + 1184 * x3), 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 + x4, tmp15, xmask) tl.store(out_ptr1 + x4, tmp16, xmask) @triton.jit def triton_poi_fused_relu_6(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 400 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 100 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_7(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) 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, (8, 3, 6, 6), (108, 36, 6, 1)) assert_size_stride(primals_2, (8,), (1,)) assert_size_stride(primals_3, (4, 3, 64, 64), (12288, 4096, 64, 1)) assert_size_stride(primals_4, (16, 8, 6, 6), (288, 36, 6, 1)) assert_size_stride(primals_5, (16,), (1,)) assert_size_stride(primals_6, (24, 16, 6, 6), (576, 36, 6, 1)) assert_size_stride(primals_7, (24,), (1,)) assert_size_stride(primals_8, (100, 216), (216, 1)) assert_size_stride(primals_9, (100,), (1,)) assert_size_stride(primals_10, (32, 100), (100, 1)) assert_size_stride(primals_11, (32,), (1,)) assert_size_stride(primals_12, (3, 32), (32, 1)) assert_size_stride(primals_13, (3,), (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, 8, 59, 59), (27848, 3481, 59, 1)) buf1 = empty_strided_cuda((4, 8, 59, 59), (27904, 3488, 59, 1), torch.float32) get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(111392)](buf0, primals_2, buf1, 111392, XBLOCK=512, num_warps=8, num_stages=1) del buf0 del primals_2 buf2 = empty_strided_cuda((4, 8, 29, 29), (6752, 841, 29, 1), torch .float32) buf3 = empty_strided_cuda((4, 8, 29, 29), (6784, 841, 29, 1), torch .int8) triton_poi_fused_max_pool2d_with_indices_1[grid(26912)](buf1, buf2, buf3, 26912, XBLOCK=256, 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, 24, 24), (9216, 576, 24, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_relu_2[grid(36864)](buf5, primals_5, 36864, XBLOCK=512, num_warps=4, num_stages=1) del primals_5 buf6 = empty_strided_cuda((4, 16, 12, 12), (2304, 144, 12, 1), torch.float32) buf7 = empty_strided_cuda((4, 16, 12, 12), (2304, 144, 12, 1), torch.int8) triton_poi_fused_max_pool2d_with_indices_3[grid(9216)](buf5, buf6, buf7, 9216, XBLOCK=256, num_warps=4, num_stages=1) buf8 = extern_kernels.convolution(buf6, primals_6, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 24, 7, 7), (1176, 49, 7, 1)) buf9 = empty_strided_cuda((4, 24, 7, 7), (1184, 49, 7, 1), torch. float32) triton_poi_fused_convolution_relu_4[grid(4704)](buf8, primals_7, buf9, 4704, XBLOCK=128, num_warps=4, num_stages=1) del buf8 del primals_7 buf10 = empty_strided_cuda((4, 24, 3, 3), (216, 9, 3, 1), torch.int8) buf11 = empty_strided_cuda((4, 24, 3, 3), (216, 9, 3, 1), torch.float32 ) triton_poi_fused_max_pool2d_with_indices_5[grid(864)](buf9, buf10, buf11, 864, XBLOCK=128, num_warps=4, num_stages=1) buf12 = empty_strided_cuda((4, 100), (100, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf11, (4, 216), (216, 1), 0), reinterpret_tensor(primals_8, (216, 100), (1, 216), 0), out=buf12) buf13 = buf12 del buf12 triton_poi_fused_relu_6[grid(400)](buf13, primals_9, 400, XBLOCK= 128, num_warps=4, num_stages=1) del primals_9 buf14 = empty_strided_cuda((4, 32), (32, 1), torch.float32) extern_kernels.mm(buf13, reinterpret_tensor(primals_10, (100, 32), (1, 100), 0), out=buf14) buf15 = buf14 del buf14 triton_poi_fused_relu_7[grid(128)](buf15, primals_11, 128, XBLOCK= 128, num_warps=4, num_stages=1) del primals_11 buf16 = empty_strided_cuda((4, 3), (3, 1), torch.float32) extern_kernels.addmm(primals_13, buf15, reinterpret_tensor( primals_12, (32, 3), (1, 32), 0), alpha=1, beta=1, out=buf16) del primals_13 return (buf16, primals_1, primals_3, primals_4, primals_6, buf1, buf2, buf3, buf5, buf6, buf7, buf9, buf10, reinterpret_tensor(buf11, (4, 216), (216, 1), 0), buf13, buf15, primals_12, primals_10, primals_8) class CNNNew(nn.Module): def __init__(self): super(CNNNew, self).__init__() self.conv1 = nn.Conv2d(3, 8, 6, 1) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(8, 16, 6, 1) self.conv3 = nn.Conv2d(16, 24, 6, 1) self.fc1 = nn.Linear(24 * 3 * 3, 100) self.fc2 = nn.Linear(100, 32) self.fc3 = nn.Linear(32, 3) 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_12 = self.fc3.weight primals_13 = 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, primals_12, primals_13]) return output[0]
eosay/robotCNN
CNN
false
6,689
[ "MIT" ]
1
9eaefcc223e868c01f6d1a49a28d2a9f392857e5
https://github.com/eosay/robotCNN/tree/9eaefcc223e868c01f6d1a49a28d2a9f392857e5
SequenceBias
import torch import torch.nn as nn import torch.utils.data import torch.utils.data.distributed import torch.nn.parallel from torch.nn.parameter import Parameter class SequenceBias(nn.Module): """ Adds one bias element to the end of the sequence. so if the input has a shape ``(L, N, E)``, where ``L`` is the sequence length, ``N`` is the batch size, and ``E`` is the embedding dimension, the output will have a shape ``(L+1, N, E)``. Attributes: bias (:class:`torch.nn.parameter.Parameter`): the learnable bias of the module of shape ``(E)``, where ``E`` is the embedding dimension. Example: >>> m = SequenceBias(16) >>> input = torch.randn(20, 4, 16) >>> output = m(input) >>> print(output.size()) torch.Size([21, 4, 16]) """ def __init__(self, embed_dim: 'int'): """ Args: embed_dim: Embedding dimension """ super(SequenceBias, self).__init__() self.bias = Parameter(torch.empty(embed_dim)) self._reset_parameters() def _reset_parameters(self): """ assing's Normally distributed random values to bias. """ nn.init.normal_(self.bias) def forward(self, x): _, bsz, _ = x.shape return torch.cat([x, self.bias.repeat(1, bsz, 1)]) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'embed_dim': 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 import torch.utils.data import torch.utils.data.distributed import torch.nn.parallel 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 @triton.jit def triton_poi_fused_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 80 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex // 16 x3 = xindex % 16 x0 = xindex % 4 x4 = xindex tmp0 = x2 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x3 + 16 * x2), tmp4 & xmask, other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 5, tl.int64) tmp9 = tl.load(in_ptr1 + x0, tmp6 & xmask, eviction_policy='evict_last', other=0.0) tmp10 = tl.where(tmp4, tmp5, tmp9) tl.store(out_ptr0 + x4, tmp10, xmask) def call(args): primals_1, primals_2 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((5, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(80)](primals_1, primals_2, buf0, 80, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 del primals_2 return buf0, class SequenceBiasNew(nn.Module): """ Adds one bias element to the end of the sequence. so if the input has a shape ``(L, N, E)``, where ``L`` is the sequence length, ``N`` is the batch size, and ``E`` is the embedding dimension, the output will have a shape ``(L+1, N, E)``. Attributes: bias (:class:`torch.nn.parameter.Parameter`): the learnable bias of the module of shape ``(E)``, where ``E`` is the embedding dimension. Example: >>> m = SequenceBias(16) >>> input = torch.randn(20, 4, 16) >>> output = m(input) >>> print(output.size()) torch.Size([21, 4, 16]) """ def __init__(self, embed_dim: 'int'): """ Args: embed_dim: Embedding dimension """ super(SequenceBiasNew, self).__init__() self.bias = Parameter(torch.empty(embed_dim)) self._reset_parameters() def _reset_parameters(self): """ assing's Normally distributed random values to bias. """ nn.init.normal_(self.bias) def forward(self, input_0): primals_2 = self.bias primals_1 = input_0 output = call([primals_1, primals_2]) return output[0]
ffuuugor/opacus
SequenceBias
false
6,690
[ "Apache-2.0" ]
1
2048a6e92902685c2a735e9fb7c0d48b4846b494
https://github.com/ffuuugor/opacus/tree/2048a6e92902685c2a735e9fb7c0d48b4846b494
SoftmaxAffineLayer
import torch import torch.nn.functional as F import torch.nn def to_device(device_object, tensor): """ Select device for non-parameters tensor w.r.t model or tensor which has been specified a device. """ if isinstance(device_object, torch.nn.Module): next(device_object.parameters()).device elif isinstance(device_object, torch.Tensor): pass return tensor class TdnnAffine(torch.nn.Module): """ An implemented tdnn affine component by conv1d y = splice(w * x, context) + b @input_dim: number of dims of frame <=> inputs channels of conv @output_dim: number of layer nodes <=> outputs channels of conv @context: a list of context e.g. [-2,0,2] If context is [0], then the TdnnAffine is equal to linear layer. """ def __init__(self, input_dim, output_dim, context=[0], bias=True, pad= True, norm_w=False, norm_f=False, subsampling_factor=1): super(TdnnAffine, self).__init__() for index in range(0, len(context) - 1): if context[index] >= context[index + 1]: raise ValueError( 'Context tuple {} is invalid, such as the order.'. format(context)) self.input_dim = input_dim self.output_dim = output_dim self.context = context self.bool_bias = bias self.pad = pad self.norm_w = norm_w self.norm_f = norm_f self.stride = subsampling_factor self.left_context = context[0] if context[0] < 0 else 0 self.right_context = context[-1] if context[-1] > 0 else 0 self.tot_context = self.right_context - self.left_context + 1 if self.tot_context > 1 and self.norm_f: self.norm_f = False None kernel_size = self.tot_context, self.weight = torch.nn.Parameter(torch.randn(output_dim, input_dim, *kernel_size)) if self.bool_bias: self.bias = torch.nn.Parameter(torch.randn(output_dim)) else: self.register_parameter('bias', None) self.init_weight() if len(context) != self.tot_context: self.mask = torch.tensor([[[(1 if index in context else 0) for index in range(self.left_context, self.right_context + 1)]]]) else: self.mask = None self.selected_device = False def init_weight(self): torch.nn.init.normal_(self.weight, 0.0, 0.01) if self.bias is not None: torch.nn.init.constant_(self.bias, 0.0) def forward(self, inputs): """ @inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index] """ assert len(inputs.shape) == 3 assert inputs.shape[1] == self.input_dim if self.pad: inputs = F.pad(inputs, (-self.left_context, self.right_context), mode='constant', value=0) assert inputs.shape[2] >= self.tot_context if not self.selected_device and self.mask is not None: self.mask = to_device(self, self.mask) self.selected_device = True filters = (self.weight * self.mask if self.mask is not None else self.weight) if self.norm_w: filters = F.normalize(filters, dim=1) if self.norm_f: inputs = F.normalize(inputs, dim=1) outputs = F.conv1d(inputs, filters, self.bias, self.stride, padding =0, dilation=1, groups=1) return outputs def extra_repr(self): return ( '{input_dim}, {output_dim}, context={context}, bias={bool_bias}, stride={stride}, pad={pad}, norm_w={norm_w}, norm_f={norm_f}' .format(**self.__dict__)) @classmethod def thop_count(self, m, x, y): x = x[0] kernel_ops = torch.zeros(m.weight.size()[2:]).numel() bias_ops = 1 if m.bias is not None else 0 total_ops = y.nelement() * (m.input_dim * kernel_ops + bias_ops) m.total_ops += torch.DoubleTensor([int(total_ops)]) class SoftmaxAffineLayer(torch.nn.Module): """ An usual 2-fold softmax layer with an affine transform. @dim: which dim to apply softmax on """ def __init__(self, input_dim, output_dim, dim=1, log=True, bias=True, special_init=False): super(SoftmaxAffineLayer, self).__init__() self.affine = TdnnAffine(input_dim, output_dim, bias=bias) if log: self.softmax = torch.nn.LogSoftmax(dim=dim) else: self.softmax = torch.nn.Softmax(dim=dim) if special_init: torch.nn.init.xavier_uniform_(self.affine.weight, gain=torch.nn .init.calculate_gain('sigmoid')) def forward(self, inputs): """ @inputs: any, such as a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index] """ return self.softmax(self.affine(inputs)) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'output_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 math as tl_math import torch.nn.functional as F import torch.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__log_softmax_convolution_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, 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_ptr1 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp4 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask) tmp5 = tl.load(in_ptr1 + 1) tmp6 = tl.broadcast_to(tmp5, [XBLOCK]) tmp9 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask) tmp10 = tl.load(in_ptr1 + 2) tmp11 = tl.broadcast_to(tmp10, [XBLOCK]) tmp14 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask) tmp15 = tl.load(in_ptr1 + 3) tmp16 = tl.broadcast_to(tmp15, [XBLOCK]) tmp3 = tmp0 + tmp2 tmp7 = tmp4 + tmp6 tmp8 = triton_helpers.maximum(tmp3, tmp7) tmp12 = tmp9 + tmp11 tmp13 = triton_helpers.maximum(tmp8, tmp12) tmp17 = tmp14 + tmp16 tmp18 = triton_helpers.maximum(tmp13, tmp17) tmp19 = tmp3 - tmp18 tmp20 = tl_math.exp(tmp19) tmp21 = tmp7 - tmp18 tmp22 = tl_math.exp(tmp21) tmp23 = tmp20 + tmp22 tmp24 = tmp12 - tmp18 tmp25 = tl_math.exp(tmp24) tmp26 = tmp23 + tmp25 tmp27 = tmp17 - tmp18 tmp28 = tl_math.exp(tmp27) tmp29 = tmp26 + tmp28 tl.store(out_ptr0 + x2, tmp18, xmask) tl.store(out_ptr1 + x2, tmp29, xmask) @triton.jit def triton_poi_fused__log_softmax_convolution_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, 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 x0 = xindex % 4 x2 = xindex // 16 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + (x0 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp5 = tl.load(in_ptr2 + (x0 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp4 = tmp2 - tmp3 tmp6 = tl_math.log(tmp5) tmp7 = tmp4 - tmp6 tl.store(in_out_ptr0 + x3, tmp7, xmask) def call(args): primals_1, primals_2, primals_3 = 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,)) 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 = empty_strided_cuda((4, 1, 4), (4, 16, 1), torch.float32) buf2 = empty_strided_cuda((4, 1, 4), (4, 16, 1), torch.float32) get_raw_stream(0) triton_poi_fused__log_softmax_convolution_0[grid(16)](buf0, primals_3, buf1, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1) buf3 = buf0 del buf0 triton_poi_fused__log_softmax_convolution_1[grid(64)](buf3, primals_3, buf1, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf1 del buf2 del primals_3 return buf3, primals_1, primals_2, buf3 def to_device(device_object, tensor): """ Select device for non-parameters tensor w.r.t model or tensor which has been specified a device. """ if isinstance(device_object, torch.nn.Module): next(device_object.parameters()).device elif isinstance(device_object, torch.Tensor): pass return tensor class TdnnAffine(torch.nn.Module): """ An implemented tdnn affine component by conv1d y = splice(w * x, context) + b @input_dim: number of dims of frame <=> inputs channels of conv @output_dim: number of layer nodes <=> outputs channels of conv @context: a list of context e.g. [-2,0,2] If context is [0], then the TdnnAffine is equal to linear layer. """ def __init__(self, input_dim, output_dim, context=[0], bias=True, pad= True, norm_w=False, norm_f=False, subsampling_factor=1): super(TdnnAffine, self).__init__() for index in range(0, len(context) - 1): if context[index] >= context[index + 1]: raise ValueError( 'Context tuple {} is invalid, such as the order.'. format(context)) self.input_dim = input_dim self.output_dim = output_dim self.context = context self.bool_bias = bias self.pad = pad self.norm_w = norm_w self.norm_f = norm_f self.stride = subsampling_factor self.left_context = context[0] if context[0] < 0 else 0 self.right_context = context[-1] if context[-1] > 0 else 0 self.tot_context = self.right_context - self.left_context + 1 if self.tot_context > 1 and self.norm_f: self.norm_f = False None kernel_size = self.tot_context, self.weight = torch.nn.Parameter(torch.randn(output_dim, input_dim, *kernel_size)) if self.bool_bias: self.bias = torch.nn.Parameter(torch.randn(output_dim)) else: self.register_parameter('bias', None) self.init_weight() if len(context) != self.tot_context: self.mask = torch.tensor([[[(1 if index in context else 0) for index in range(self.left_context, self.right_context + 1)]]]) else: self.mask = None self.selected_device = False def init_weight(self): torch.nn.init.normal_(self.weight, 0.0, 0.01) if self.bias is not None: torch.nn.init.constant_(self.bias, 0.0) def forward(self, inputs): """ @inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index] """ assert len(inputs.shape) == 3 assert inputs.shape[1] == self.input_dim if self.pad: inputs = F.pad(inputs, (-self.left_context, self.right_context), mode='constant', value=0) assert inputs.shape[2] >= self.tot_context if not self.selected_device and self.mask is not None: self.mask = to_device(self, self.mask) self.selected_device = True filters = (self.weight * self.mask if self.mask is not None else self.weight) if self.norm_w: filters = F.normalize(filters, dim=1) if self.norm_f: inputs = F.normalize(inputs, dim=1) outputs = F.conv1d(inputs, filters, self.bias, self.stride, padding =0, dilation=1, groups=1) return outputs def extra_repr(self): return ( '{input_dim}, {output_dim}, context={context}, bias={bool_bias}, stride={stride}, pad={pad}, norm_w={norm_w}, norm_f={norm_f}' .format(**self.__dict__)) @classmethod def thop_count(self, m, x, y): x = x[0] kernel_ops = torch.zeros(m.weight.size()[2:]).numel() bias_ops = 1 if m.bias is not None else 0 total_ops = y.nelement() * (m.input_dim * kernel_ops + bias_ops) m.total_ops += torch.DoubleTensor([int(total_ops)]) class SoftmaxAffineLayerNew(torch.nn.Module): """ An usual 2-fold softmax layer with an affine transform. @dim: which dim to apply softmax on """ def __init__(self, input_dim, output_dim, dim=1, log=True, bias=True, special_init=False): super(SoftmaxAffineLayerNew, self).__init__() self.affine = TdnnAffine(input_dim, output_dim, bias=bias) if log: self.softmax = torch.nn.LogSoftmax(dim=dim) else: self.softmax = torch.nn.Softmax(dim=dim) if special_init: torch.nn.init.xavier_uniform_(self.affine.weight, gain=torch.nn .init.calculate_gain('sigmoid')) def forward(self, input_0): primals_2 = self.affine.weight primals_3 = self.affine.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
fancyliumeng/asv-subtools
SoftmaxAffineLayer
false
6,691
[ "Apache-2.0" ]
1
56a13484472e7ae6eb00d762c00d57e581e78eb4
https://github.com/fancyliumeng/asv-subtools/tree/56a13484472e7ae6eb00d762c00d57e581e78eb4
MLPLayer
from _paritybench_helpers import _mock_config import torch import torch.nn as nn class MLPLayer(nn.Module): """ Head for getting sentence representations over RoBERTa/BERT's CLS representation. """ def __init__(self, config): super().__init__() self.dense = nn.Linear(config.hidden_size, config.hidden_size) self.activation = nn.Tanh() def forward(self, features, **kwargs): x = self.dense(features) x = self.activation(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'config': _mock_config(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.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_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 = 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 get_raw_stream(0) triton_poi_fused_tanh_0[grid(256)](buf1, primals_2, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 return buf1, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), buf1 class MLPLayerNew(nn.Module): """ Head for getting sentence representations over RoBERTa/BERT's CLS representation. """ def __init__(self, config): super().__init__() self.dense = nn.Linear(config.hidden_size, config.hidden_size) self.activation = nn.Tanh() def forward(self, input_0): primals_1 = self.dense.weight primals_2 = self.dense.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
BDBC-KG-NLP/MixCSE_AAAI2022
MLPLayer
false
6,692
[ "MIT" ]
1
884145e24a5258c044fedb658df9999f012df875
https://github.com/BDBC-KG-NLP/MixCSE_AAAI2022/tree/884145e24a5258c044fedb658df9999f012df875
SEBlock
import torch import torch.nn.functional as F import torch.nn def to_device(device_object, tensor): """ Select device for non-parameters tensor w.r.t model or tensor which has been specified a device. """ if isinstance(device_object, torch.nn.Module): next(device_object.parameters()).device elif isinstance(device_object, torch.Tensor): pass return tensor class TdnnAffine(torch.nn.Module): """ An implemented tdnn affine component by conv1d y = splice(w * x, context) + b @input_dim: number of dims of frame <=> inputs channels of conv @output_dim: number of layer nodes <=> outputs channels of conv @context: a list of context e.g. [-2,0,2] If context is [0], then the TdnnAffine is equal to linear layer. """ def __init__(self, input_dim, output_dim, context=[0], bias=True, pad= True, norm_w=False, norm_f=False, subsampling_factor=1): super(TdnnAffine, self).__init__() for index in range(0, len(context) - 1): if context[index] >= context[index + 1]: raise ValueError( 'Context tuple {} is invalid, such as the order.'. format(context)) self.input_dim = input_dim self.output_dim = output_dim self.context = context self.bool_bias = bias self.pad = pad self.norm_w = norm_w self.norm_f = norm_f self.stride = subsampling_factor self.left_context = context[0] if context[0] < 0 else 0 self.right_context = context[-1] if context[-1] > 0 else 0 self.tot_context = self.right_context - self.left_context + 1 if self.tot_context > 1 and self.norm_f: self.norm_f = False None kernel_size = self.tot_context, self.weight = torch.nn.Parameter(torch.randn(output_dim, input_dim, *kernel_size)) if self.bool_bias: self.bias = torch.nn.Parameter(torch.randn(output_dim)) else: self.register_parameter('bias', None) self.init_weight() if len(context) != self.tot_context: self.mask = torch.tensor([[[(1 if index in context else 0) for index in range(self.left_context, self.right_context + 1)]]]) else: self.mask = None self.selected_device = False def init_weight(self): torch.nn.init.normal_(self.weight, 0.0, 0.01) if self.bias is not None: torch.nn.init.constant_(self.bias, 0.0) def forward(self, inputs): """ @inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index] """ assert len(inputs.shape) == 3 assert inputs.shape[1] == self.input_dim if self.pad: inputs = F.pad(inputs, (-self.left_context, self.right_context), mode='constant', value=0) assert inputs.shape[2] >= self.tot_context if not self.selected_device and self.mask is not None: self.mask = to_device(self, self.mask) self.selected_device = True filters = (self.weight * self.mask if self.mask is not None else self.weight) if self.norm_w: filters = F.normalize(filters, dim=1) if self.norm_f: inputs = F.normalize(inputs, dim=1) outputs = F.conv1d(inputs, filters, self.bias, self.stride, padding =0, dilation=1, groups=1) return outputs def extra_repr(self): return ( '{input_dim}, {output_dim}, context={context}, bias={bool_bias}, stride={stride}, pad={pad}, norm_w={norm_w}, norm_f={norm_f}' .format(**self.__dict__)) @classmethod def thop_count(self, m, x, y): x = x[0] kernel_ops = torch.zeros(m.weight.size()[2:]).numel() bias_ops = 1 if m.bias is not None else 0 total_ops = y.nelement() * (m.input_dim * kernel_ops + bias_ops) m.total_ops += torch.DoubleTensor([int(total_ops)]) class StatisticsPooling(torch.nn.Module): """ An usual mean [+ stddev] poolling layer""" def __init__(self, input_dim, stddev=True, unbiased=False, eps=1e-10): super(StatisticsPooling, self).__init__() self.stddev = stddev self.input_dim = input_dim if self.stddev: self.output_dim = 2 * input_dim else: self.output_dim = input_dim self.eps = eps self.unbiased = unbiased def forward(self, inputs): """ @inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index] """ assert len(inputs.shape) == 3 assert inputs.shape[1] == self.input_dim counts = inputs.shape[2] mean = torch.unsqueeze(inputs.sum(dim=2) / counts, dim=2) if self.stddev: if self.unbiased and counts > 1: counts = counts - 1 var = torch.sum((inputs - mean) ** 2, dim=2) / counts std = torch.unsqueeze(torch.sqrt(var.clamp(min=self.eps)), dim=2) return torch.cat((mean, std), dim=1) else: return mean def get_output_dim(self): return self.output_dim def extra_repr(self): return ( '{input_dim}, {output_dim}, stddev={stddev}, unbiased={unbiased}, eps={eps}' .format(**self.__dict__)) @classmethod def thop_count(self, m, x, y): pass class SEBlock(torch.nn.Module): """ A SE Block layer layer which can learn to use global information to selectively emphasise informative features and suppress less useful ones. This is a pytorch implementation of SE Block based on the paper: Squeeze-and-Excitation Networks by JFChou xmuspeech 2019-07-13 Snowdar xmuspeech 2020-04-28 [Check and update] """ def __init__(self, input_dim, ratio=4, inplace=True): """ @ratio: a reduction ratio which allows us to vary the capacity and computational cost of the SE blocks in the network. """ super(SEBlock, self).__init__() self.input_dim = input_dim self.pooling = StatisticsPooling(input_dim, stddev=False) self.fc_1 = TdnnAffine(input_dim, input_dim // ratio) self.relu = torch.nn.ReLU(inplace=inplace) self.fc_2 = TdnnAffine(input_dim // ratio, input_dim) self.sigmoid = torch.nn.Sigmoid() def forward(self, inputs): """ @inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index] """ assert len(inputs.shape) == 3 assert inputs.shape[1] == self.input_dim x = self.pooling(inputs) x = self.relu(self.fc_1(x)) scale = self.sigmoid(self.fc_2(x)) return inputs * scale def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'input_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 import torch.nn.functional as F import torch.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_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 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 = 0.25 tmp8 = tmp6 * tmp7 tl.store(out_ptr0 + x0, tmp8, xmask) @triton.jit def triton_poi_fused_convolution_relu_1(in_out_ptr0, in_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_out_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp3 = tmp0 + tmp2 tmp4 = tl.full([1], 0, tl.int32) tmp5 = triton_helpers.maximum(tmp4, tmp3) tl.store(in_out_ptr0 + x0, tmp5, 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) @triton.jit def triton_poi_fused_mul_sigmoid_3(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 x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tl.sigmoid(tmp1) tmp3 = tmp0 * tmp2 tl.store(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), (16, 4, 1)) assert_size_stride(primals_2, (1, 4, 1), (4, 1, 1)) assert_size_stride(primals_3, (1,), (1,)) assert_size_stride(primals_4, (4, 1, 1), (1, 1, 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, 1), torch.float32) get_raw_stream(0) triton_poi_fused_div_sum_0[grid(16)](primals_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) buf1 = extern_kernels.convolution(reinterpret_tensor(buf0, (4, 4, 1 ), (4, 1, 0), 0), primals_2, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf1, (4, 1, 1), (1, 1, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_relu_1[grid(4)](buf2, primals_3, 4, XBLOCK=4, num_warps=1, num_stages=1) del primals_3 buf3 = extern_kernels.convolution(buf2, primals_4, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf3, (4, 4, 1), (4, 1, 1)) buf4 = buf3 del buf3 triton_poi_fused_convolution_2[grid(16)](buf4, primals_5, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_5 buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_mul_sigmoid_3[grid(64)](primals_1, buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) return buf5, primals_1, primals_2, primals_4, reinterpret_tensor(buf0, (4, 4, 1), (4, 1, 1), 0), buf2, buf4 def to_device(device_object, tensor): """ Select device for non-parameters tensor w.r.t model or tensor which has been specified a device. """ if isinstance(device_object, torch.nn.Module): next(device_object.parameters()).device elif isinstance(device_object, torch.Tensor): pass return tensor class TdnnAffine(torch.nn.Module): """ An implemented tdnn affine component by conv1d y = splice(w * x, context) + b @input_dim: number of dims of frame <=> inputs channels of conv @output_dim: number of layer nodes <=> outputs channels of conv @context: a list of context e.g. [-2,0,2] If context is [0], then the TdnnAffine is equal to linear layer. """ def __init__(self, input_dim, output_dim, context=[0], bias=True, pad= True, norm_w=False, norm_f=False, subsampling_factor=1): super(TdnnAffine, self).__init__() for index in range(0, len(context) - 1): if context[index] >= context[index + 1]: raise ValueError( 'Context tuple {} is invalid, such as the order.'. format(context)) self.input_dim = input_dim self.output_dim = output_dim self.context = context self.bool_bias = bias self.pad = pad self.norm_w = norm_w self.norm_f = norm_f self.stride = subsampling_factor self.left_context = context[0] if context[0] < 0 else 0 self.right_context = context[-1] if context[-1] > 0 else 0 self.tot_context = self.right_context - self.left_context + 1 if self.tot_context > 1 and self.norm_f: self.norm_f = False None kernel_size = self.tot_context, self.weight = torch.nn.Parameter(torch.randn(output_dim, input_dim, *kernel_size)) if self.bool_bias: self.bias = torch.nn.Parameter(torch.randn(output_dim)) else: self.register_parameter('bias', None) self.init_weight() if len(context) != self.tot_context: self.mask = torch.tensor([[[(1 if index in context else 0) for index in range(self.left_context, self.right_context + 1)]]]) else: self.mask = None self.selected_device = False def init_weight(self): torch.nn.init.normal_(self.weight, 0.0, 0.01) if self.bias is not None: torch.nn.init.constant_(self.bias, 0.0) def forward(self, inputs): """ @inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index] """ assert len(inputs.shape) == 3 assert inputs.shape[1] == self.input_dim if self.pad: inputs = F.pad(inputs, (-self.left_context, self.right_context), mode='constant', value=0) assert inputs.shape[2] >= self.tot_context if not self.selected_device and self.mask is not None: self.mask = to_device(self, self.mask) self.selected_device = True filters = (self.weight * self.mask if self.mask is not None else self.weight) if self.norm_w: filters = F.normalize(filters, dim=1) if self.norm_f: inputs = F.normalize(inputs, dim=1) outputs = F.conv1d(inputs, filters, self.bias, self.stride, padding =0, dilation=1, groups=1) return outputs def extra_repr(self): return ( '{input_dim}, {output_dim}, context={context}, bias={bool_bias}, stride={stride}, pad={pad}, norm_w={norm_w}, norm_f={norm_f}' .format(**self.__dict__)) @classmethod def thop_count(self, m, x, y): x = x[0] kernel_ops = torch.zeros(m.weight.size()[2:]).numel() bias_ops = 1 if m.bias is not None else 0 total_ops = y.nelement() * (m.input_dim * kernel_ops + bias_ops) m.total_ops += torch.DoubleTensor([int(total_ops)]) class StatisticsPooling(torch.nn.Module): """ An usual mean [+ stddev] poolling layer""" def __init__(self, input_dim, stddev=True, unbiased=False, eps=1e-10): super(StatisticsPooling, self).__init__() self.stddev = stddev self.input_dim = input_dim if self.stddev: self.output_dim = 2 * input_dim else: self.output_dim = input_dim self.eps = eps self.unbiased = unbiased def forward(self, inputs): """ @inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index] """ assert len(inputs.shape) == 3 assert inputs.shape[1] == self.input_dim counts = inputs.shape[2] mean = torch.unsqueeze(inputs.sum(dim=2) / counts, dim=2) if self.stddev: if self.unbiased and counts > 1: counts = counts - 1 var = torch.sum((inputs - mean) ** 2, dim=2) / counts std = torch.unsqueeze(torch.sqrt(var.clamp(min=self.eps)), dim=2) return torch.cat((mean, std), dim=1) else: return mean def get_output_dim(self): return self.output_dim def extra_repr(self): return ( '{input_dim}, {output_dim}, stddev={stddev}, unbiased={unbiased}, eps={eps}' .format(**self.__dict__)) @classmethod def thop_count(self, m, x, y): pass class SEBlockNew(torch.nn.Module): """ A SE Block layer layer which can learn to use global information to selectively emphasise informative features and suppress less useful ones. This is a pytorch implementation of SE Block based on the paper: Squeeze-and-Excitation Networks by JFChou xmuspeech 2019-07-13 Snowdar xmuspeech 2020-04-28 [Check and update] """ def __init__(self, input_dim, ratio=4, inplace=True): """ @ratio: a reduction ratio which allows us to vary the capacity and computational cost of the SE blocks in the network. """ super(SEBlockNew, self).__init__() self.input_dim = input_dim self.pooling = StatisticsPooling(input_dim, stddev=False) self.fc_1 = TdnnAffine(input_dim, input_dim // ratio) self.relu = torch.nn.ReLU(inplace=inplace) self.fc_2 = TdnnAffine(input_dim // ratio, input_dim) self.sigmoid = torch.nn.Sigmoid() def forward(self, input_0): primals_2 = self.fc_1.weight primals_3 = self.fc_1.bias primals_4 = self.fc_2.weight primals_5 = self.fc_2.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
fancyliumeng/asv-subtools
SEBlock
false
6,693
[ "Apache-2.0" ]
1
56a13484472e7ae6eb00d762c00d57e581e78eb4
https://github.com/fancyliumeng/asv-subtools/tree/56a13484472e7ae6eb00d762c00d57e581e78eb4
MultiheadAttention
import torch import torch.nn.functional as F import torch.utils.data import torch.distributed import torch.nn as nn from torch.nn import Parameter import torch.optim import torch.optim.lr_scheduler class MultiheadAttention(nn.Module): """Multi-headed attention. See "Attention Is All You Need" for more details. """ def __init__(self, embed_dim, num_heads, dropout=0.0, bias=True): super().__init__() self.embed_dim = embed_dim self.num_heads = num_heads self.dropout = dropout self.head_dim = embed_dim // num_heads assert self.head_dim * num_heads == self.embed_dim, 'embed_dim must be divisible by num_heads' self.scaling = self.head_dim ** -0.5 self._mask = None self.in_proj_weight = Parameter(torch.Tensor(3 * embed_dim, embed_dim)) if bias: self.in_proj_bias = Parameter(torch.Tensor(3 * embed_dim)) else: self.register_parameter('in_proj_bias', None) self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias) self.reset_parameters() def reset_parameters(self): nn.init.xavier_uniform_(self.in_proj_weight) nn.init.xavier_uniform_(self.out_proj.weight) if self.in_proj_bias is not None: nn.init.constant_(self.in_proj_bias, 0.0) nn.init.constant_(self.out_proj.bias, 0.0) def forward(self, query, key, value, mask_future_timesteps=False, key_padding_mask=None, incremental_state=None, need_weights=True, static_kv=False): """Input shape: Time x Batch x Channel Self-attention can be implemented by passing in the same arguments for query, key and value. Future timesteps can be masked with the `mask_future_timesteps` argument. Padding elements can be excluded from the key by passing a binary ByteTensor (`key_padding_mask`) with shape: batch x src_len, where padding elements are indicated by 1s. """ qkv_same = query.data_ptr() == key.data_ptr() == value.data_ptr() kv_same = key.data_ptr() == value.data_ptr() tgt_len, bsz, embed_dim = query.size() assert embed_dim == self.embed_dim assert list(query.size()) == [tgt_len, bsz, embed_dim] assert key.size() == value.size() if incremental_state is not None: saved_state = self._get_input_buffer(incremental_state) if 'prev_key' in saved_state: if static_kv: assert kv_same and not qkv_same key = value = None else: saved_state = None if qkv_same: q, k, v = self.in_proj_qkv(query) elif kv_same: q = self.in_proj_q(query) if key is None: assert value is None k = v = q.new(0) else: k, v = self.in_proj_kv(key) else: q = self.in_proj_q(query) k = self.in_proj_k(key) v = self.in_proj_v(value) q *= self.scaling if saved_state is not None: if 'prev_key' in saved_state: k = torch.cat((saved_state['prev_key'], k), dim=0) if 'prev_value' in saved_state: v = torch.cat((saved_state['prev_value'], v), dim=0) saved_state['prev_key'] = k saved_state['prev_value'] = v self._set_input_buffer(incremental_state, saved_state) src_len = k.size(0) if key_padding_mask is not None: assert key_padding_mask.size(0) == bsz assert key_padding_mask.size(1) == src_len q = q.contiguous().view(tgt_len, bsz * self.num_heads, self.head_dim ).transpose(0, 1) k = k.contiguous().view(src_len, bsz * self.num_heads, self.head_dim ).transpose(0, 1) v = v.contiguous().view(src_len, bsz * self.num_heads, self.head_dim ).transpose(0, 1) attn_weights = torch.bmm(q, k.transpose(1, 2)) assert list(attn_weights.size()) == [bsz * self.num_heads, tgt_len, src_len] if mask_future_timesteps and incremental_state is None: assert query.size() == key.size( ), 'mask_future_timesteps only applies to self-attention' attn_weights += self.buffered_mask(attn_weights).unsqueeze(0) if key_padding_mask is not None: attn_weights = attn_weights.view(bsz, self.num_heads, tgt_len, src_len) attn_weights = attn_weights.float().masked_fill(key_padding_mask .unsqueeze(1).unsqueeze(2), float('-inf')).type_as(attn_weights ) attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len) attn_weights = F.softmax(attn_weights.float(), dim=-1).type_as( attn_weights) attn_weights = F.dropout(attn_weights, p=self.dropout, training= self.training) attn = torch.bmm(attn_weights, v) assert list(attn.size()) == [bsz * self.num_heads, tgt_len, self. head_dim] attn = attn.transpose(0, 1).contiguous().view(tgt_len, bsz, embed_dim) attn = self.out_proj(attn) if need_weights: attn_weights = attn_weights.view(bsz, self.num_heads, tgt_len, src_len) attn_weights = attn_weights.sum(dim=1) / self.num_heads else: attn_weights = None return attn, attn_weights def in_proj_qkv(self, query): return self._in_proj(query).chunk(3, dim=-1) def in_proj_kv(self, key): return self._in_proj(key, start=self.embed_dim).chunk(2, dim=-1) def in_proj_q(self, query): return self._in_proj(query, end=self.embed_dim) def in_proj_k(self, key): return self._in_proj(key, start=self.embed_dim, end=2 * self.embed_dim) def in_proj_v(self, value): return self._in_proj(value, start=2 * self.embed_dim) def _in_proj(self, input, start=0, end=None): weight = self.in_proj_weight bias = self.in_proj_bias weight = weight[start:end, :] if bias is not None: bias = bias[start:end] return F.linear(input, weight, bias) def buffered_mask(self, tensor): dim = tensor.size(-1) if self._mask is None: self._mask = torch.triu(utils.fill_with_neg_inf(tensor.new(dim, dim)), 1) if self._mask.size(0) < dim: self._mask = torch.triu(utils.fill_with_neg_inf(self._mask. resize_(dim, dim)), 1) return self._mask[:dim, :dim] def reorder_incremental_state(self, incremental_state, new_order): """Reorder buffered internal state (for incremental generation).""" input_buffer = self._get_input_buffer(incremental_state) if input_buffer is not None: for k in input_buffer.keys(): input_buffer[k] = input_buffer[k].index_select(1, new_order) self._set_input_buffer(incremental_state, input_buffer) def _get_input_buffer(self, incremental_state): return utils.get_incremental_state(self, incremental_state, 'attn_state') or {} def _set_input_buffer(self, incremental_state, buffer): utils.set_incremental_state(self, incremental_state, 'attn_state', buffer) def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4]), torch.rand([4, 4, 4]) ] def get_init_inputs(): return [[], {'embed_dim': 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.functional as F import torch.utils.data import torch.distributed import torch.nn as nn from torch.nn import Parameter import torch.optim import torch.optim.lr_scheduler 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_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 = 1.0 tmp4 = tmp2 * tmp3 tl.store(in_out_ptr0 + x2, tmp4, 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 = 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 = 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) @triton.jit def triton_poi_fused_clone_3(in_ptr0, 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 x1 = xindex y0 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x1), xmask & ymask, eviction_policy= 'evict_last') tl.store(out_ptr0 + (x1 + 16 * y0), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_div_sum_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 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 = 0.25 tmp8 = tmp6 * 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, (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), (16, 4, 1)) assert_size_stride(primals_4, (12, 4), (4, 1)) assert_size_stride(primals_5, (12,), (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((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf0) buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.addmm(reinterpret_tensor(primals_5, (4,), (1,), 4), reinterpret_tensor(primals_2, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 16), alpha=1, beta=1, out=buf1) buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.addmm(reinterpret_tensor(primals_5, (4,), (1,), 8), reinterpret_tensor(primals_3, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 32), alpha=1, beta=1, out=buf2) del primals_4 buf3 = reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 0) del buf0 get_raw_stream(0) triton_poi_fused_mul_0[grid(64)](buf3, primals_5, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_5 buf4 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf3, (16, 4, 1), (1, 16, 0), 0), reinterpret_tensor(buf1, (16, 1, 4), (1, 1, 16), 0), out=buf4) buf5 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_1[grid(256)](buf4, buf5, 256, XBLOCK=128, num_warps=4, num_stages=1) buf6 = buf4 del buf4 triton_poi_fused__softmax_2[grid(256)](buf5, buf6, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf5 buf7 = empty_strided_cuda((16, 4, 1), (4, 1, 1), torch.float32) extern_kernels.bmm(buf6, reinterpret_tensor(buf2, (16, 4, 1), (1, 16, 1), 0), out=buf7) buf8 = empty_strided_cuda((4, 16, 1), (16, 1, 1), torch.float32) triton_poi_fused_clone_3[grid(4, 16)](buf7, buf8, 4, 16, XBLOCK=16, YBLOCK=4, num_warps=1, num_stages=1) buf9 = reinterpret_tensor(buf7, (16, 4), (4, 1), 0) del buf7 extern_kernels.addmm(primals_7, reinterpret_tensor(buf8, (16, 4), ( 4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf9) del primals_7 buf10 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_div_sum_4[grid(64)](buf6, buf10, 64, XBLOCK=64, num_warps=1, num_stages=1) return reinterpret_tensor(buf9, (4, 4, 4), (16, 4, 1), 0 ), buf10, reinterpret_tensor(primals_1, (16, 4), (4, 1), 0 ), reinterpret_tensor(primals_2, (16, 4), (4, 1), 0 ), reinterpret_tensor(primals_3, (16, 4), (4, 1), 0 ), buf6, reinterpret_tensor(buf8, (16, 4), (4, 1), 0 ), primals_6, reinterpret_tensor(buf2, (16, 1, 4), (1, 1, 16), 0 ), reinterpret_tensor(buf3, (16, 1, 4), (1, 1, 16), 0 ), reinterpret_tensor(buf1, (16, 4, 1), (1, 16, 1), 0) class MultiheadAttentionNew(nn.Module): """Multi-headed attention. See "Attention Is All You Need" for more details. """ def __init__(self, embed_dim, num_heads, dropout=0.0, bias=True): super().__init__() self.embed_dim = embed_dim self.num_heads = num_heads self.dropout = dropout self.head_dim = embed_dim // num_heads assert self.head_dim * num_heads == self.embed_dim, 'embed_dim must be divisible by num_heads' self.scaling = self.head_dim ** -0.5 self._mask = None self.in_proj_weight = Parameter(torch.Tensor(3 * embed_dim, embed_dim)) if bias: self.in_proj_bias = Parameter(torch.Tensor(3 * embed_dim)) else: self.register_parameter('in_proj_bias', None) self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias) self.reset_parameters() def reset_parameters(self): nn.init.xavier_uniform_(self.in_proj_weight) nn.init.xavier_uniform_(self.out_proj.weight) if self.in_proj_bias is not None: nn.init.constant_(self.in_proj_bias, 0.0) nn.init.constant_(self.out_proj.bias, 0.0) def in_proj_qkv(self, query): return self._in_proj(query).chunk(3, dim=-1) def in_proj_kv(self, key): return self._in_proj(key, start=self.embed_dim).chunk(2, dim=-1) def in_proj_q(self, query): return self._in_proj(query, end=self.embed_dim) def in_proj_k(self, key): return self._in_proj(key, start=self.embed_dim, end=2 * self.embed_dim) def in_proj_v(self, value): return self._in_proj(value, start=2 * self.embed_dim) def _in_proj(self, input, start=0, end=None): weight = self.in_proj_weight bias = self.in_proj_bias weight = weight[start:end, :] if bias is not None: bias = bias[start:end] return F.linear(input, weight, bias) def buffered_mask(self, tensor): dim = tensor.size(-1) if self._mask is None: self._mask = torch.triu(utils.fill_with_neg_inf(tensor.new(dim, dim)), 1) if self._mask.size(0) < dim: self._mask = torch.triu(utils.fill_with_neg_inf(self._mask. resize_(dim, dim)), 1) return self._mask[:dim, :dim] def reorder_incremental_state(self, incremental_state, new_order): """Reorder buffered internal state (for incremental generation).""" input_buffer = self._get_input_buffer(incremental_state) if input_buffer is not None: for k in input_buffer.keys(): input_buffer[k] = input_buffer[k].index_select(1, new_order) self._set_input_buffer(incremental_state, input_buffer) def _get_input_buffer(self, incremental_state): return utils.get_incremental_state(self, incremental_state, 'attn_state') or {} def _set_input_buffer(self, incremental_state, buffer): utils.set_incremental_state(self, incremental_state, 'attn_state', buffer) def forward(self, input_0, input_1, input_2): primals_4 = self.in_proj_weight primals_5 = self.in_proj_bias primals_6 = self.out_proj.weight primals_7 = self.out_proj.bias primals_1 = input_0 primals_2 = input_1 primals_3 = input_2 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0], output[1]
farhanazareen/Fairseq
MultiheadAttention
false
6,694
[ "BSD-3-Clause" ]
1
39c7b6804b4a3426ef23b30f3ca8a3c0a9948079
https://github.com/farhanazareen/Fairseq/tree/39c7b6804b4a3426ef23b30f3ca8a3c0a9948079
DPLSTMCell
import math import torch import torch.nn as nn import torch.utils.data import torch.utils.data.distributed import torch.nn.parallel from typing import Optional from typing import Tuple class LSTMLinear(nn.Linear): """ This function is the same as a nn.Linear layer, except that in the backward pass the grad_samples get accumulated (instead of being concatenated as in the standard nn.Linear) """ def __init__(self, in_features: 'int', out_features: 'int', bias: 'bool'=True): super().__init__(in_features, out_features, bias) class DPLSTMCell(nn.Module): """ Internal-only class. Implements *one* step of LSTM so that a LSTM layer can be seen as repeated applications of this class. """ def __init__(self, input_size: 'int', hidden_size: 'int', bias: 'bool'): super().__init__() self.input_size = input_size self.hidden_size = hidden_size self.bias = bias self.ih = LSTMLinear(input_size, 4 * hidden_size, bias=self.bias) self.hh = LSTMLinear(hidden_size, 4 * hidden_size, bias=self.bias) self.reset_parameters() def reset_parameters(self): """ Resets parameters by initializing them from an uniform distribution. """ stdv = 1.0 / math.sqrt(self.hidden_size) for weight in self.parameters(): nn.init.uniform_(weight, -stdv, stdv) def set_max_batch_length(self, max_batch_length: 'int') ->None: """ Sets max batch length """ self.ih.max_batch_len = max_batch_length self.hh.max_batch_len = max_batch_length def forward(self, x: 'torch.Tensor', h_prev: 'torch.Tensor', c_prev: 'torch.Tensor', batch_size_t: 'Optional[int]'=None) ->Tuple[torch. Tensor, torch.Tensor]: if batch_size_t is None: gates = self.ih(x) + self.hh(h_prev) else: gates = self.ih(x) + self.hh(h_prev[:batch_size_t, :]) i_t_input, f_t_input, g_t_input, o_t_input = torch.split(gates, self.hidden_size, 1) i_t = torch.sigmoid(i_t_input) f_t = torch.sigmoid(f_t_input) g_t = torch.tanh(g_t_input) o_t = torch.sigmoid(o_t_input) if batch_size_t is None: c_t = f_t * c_prev + i_t * g_t else: c_t = f_t * c_prev[:batch_size_t, :] + i_t * g_t h_t = o_t * torch.tanh(c_t) return h_t, c_t def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'hidden_size': 4, 'bias': 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 math import torch.nn as nn import torch.utils.data import torch.utils.data.distributed import torch.nn.parallel 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_sigmoid_sigmoid_backward_tanh_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr4, out_ptr5, 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_ptr1 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + (x0 + 16 * x1), xmask) tmp4 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask) tmp9 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr2 + (12 + x0 + 16 * x1), xmask) tmp12 = tl.load(in_ptr3 + (12 + x0), xmask, eviction_policy='evict_last') tmp16 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask) tmp17 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last') tmp19 = tl.load(in_ptr2 + (8 + x0 + 16 * x1), xmask) tmp20 = tl.load(in_ptr3 + (8 + x0), xmask, eviction_policy='evict_last') tmp24 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask) tmp25 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last') tmp27 = tl.load(in_ptr2 + (4 + x0 + 16 * x1), xmask) tmp28 = tl.load(in_ptr3 + (4 + x0), xmask, eviction_policy='evict_last') tmp32 = tl.load(in_ptr4 + x2, xmask) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp7 = tl.sigmoid(tmp6) tmp10 = tmp8 + tmp9 tmp13 = tmp11 + tmp12 tmp14 = tmp10 + tmp13 tmp15 = tl.sigmoid(tmp14) tmp18 = tmp16 + tmp17 tmp21 = tmp19 + tmp20 tmp22 = tmp18 + tmp21 tmp23 = libdevice.tanh(tmp22) tmp26 = tmp24 + tmp25 tmp29 = tmp27 + tmp28 tmp30 = tmp26 + tmp29 tmp31 = tl.sigmoid(tmp30) tmp33 = tmp31 * tmp32 tmp34 = tmp7 * tmp23 tmp35 = tmp33 + tmp34 tmp36 = 1.0 tmp37 = tmp36 - tmp31 tmp38 = tmp31 * tmp37 tmp39 = libdevice.tanh(tmp35) tmp40 = tmp15 * tmp39 tl.store(out_ptr0 + x2, tmp7, xmask) tl.store(out_ptr1 + x2, tmp15, xmask) tl.store(out_ptr2 + x2, tmp23, xmask) tl.store(out_ptr3 + x2, tmp35, xmask) tl.store(out_ptr4 + x2, tmp38, xmask) tl.store(out_ptr5 + x2, tmp40, 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, (16, 4), (4, 1)) assert_size_stride(primals_2, (16,), (1,)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (16, 4), (4, 1)) assert_size_stride(primals_5, (16,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 16), (16, 1), torch.float32) extern_kernels.mm(primals_3, reinterpret_tensor(primals_1, (4, 16), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 16), (16, 1), torch.float32) extern_kernels.mm(primals_6, reinterpret_tensor(primals_4, (4, 16), (1, 4), 0), out=buf1) del primals_4 buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mul_sigmoid_sigmoid_backward_tanh_0[grid(16)](buf0 , primals_2, buf1, primals_5, primals_7, buf2, buf4, buf3, buf5, buf7, buf6, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf0 del buf1 del primals_2 del primals_5 return (buf6, buf5, primals_3, primals_6, primals_7, buf2, buf3, buf4, buf5, buf7) class LSTMLinear(nn.Linear): """ This function is the same as a nn.Linear layer, except that in the backward pass the grad_samples get accumulated (instead of being concatenated as in the standard nn.Linear) """ def __init__(self, in_features: 'int', out_features: 'int', bias: 'bool'=True): super().__init__(in_features, out_features, bias) class DPLSTMCellNew(nn.Module): """ Internal-only class. Implements *one* step of LSTM so that a LSTM layer can be seen as repeated applications of this class. """ def __init__(self, input_size: 'int', hidden_size: 'int', bias: 'bool'): super().__init__() self.input_size = input_size self.hidden_size = hidden_size self.bias = bias self.ih = LSTMLinear(input_size, 4 * hidden_size, bias=self.bias) self.hh = LSTMLinear(hidden_size, 4 * hidden_size, bias=self.bias) self.reset_parameters() def reset_parameters(self): """ Resets parameters by initializing them from an uniform distribution. """ stdv = 1.0 / math.sqrt(self.hidden_size) for weight in self.parameters(): nn.init.uniform_(weight, -stdv, stdv) def set_max_batch_length(self, max_batch_length: 'int') ->None: """ Sets max batch length """ self.ih.max_batch_len = max_batch_length self.hh.max_batch_len = max_batch_length def forward(self, input_0, input_1, input_2): primals_1 = self.ih.weight primals_2 = self.ih.bias primals_4 = self.hh.weight primals_5 = self.hh.bias primals_3 = input_0 primals_6 = input_1 primals_7 = input_2 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0], output[1]
ffuuugor/opacus
DPLSTMCell
false
6,695
[ "Apache-2.0" ]
1
2048a6e92902685c2a735e9fb7c0d48b4846b494
https://github.com/ffuuugor/opacus/tree/2048a6e92902685c2a735e9fb7c0d48b4846b494
ResidualBlock
import torch import torch.nn as nn def conv3x3(in_ch, out_ch, stride=1): """3x3 convolution with padding.""" return nn.Conv2d(in_ch, out_ch, kernel_size=3, stride=stride, padding=1) class ResidualBlock(nn.Module): """Simple residual block with two 3x3 convolutions. Args: in_ch (int): number of input channels out_ch (int): number of output channels """ def __init__(self, in_ch, out_ch): super().__init__() self.conv1 = conv3x3(in_ch, out_ch) self.leaky_relu = nn.LeakyReLU(inplace=True) self.conv2 = conv3x3(out_ch, out_ch) def forward(self, x): identity = x out = self.conv1(x) out = self.leaky_relu(out) out = self.conv2(out) out = self.leaky_relu(out) out = out + identity return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_ch': 4, 'out_ch': 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 @triton.jit def triton_poi_fused_convolution_leaky_relu_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 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') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(in_out_ptr0 + x3, tmp7, xmask) @triton.jit def triton_poi_fused_add_convolution_leaky_relu_leaky_relu_backward_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 x1 = xindex // 16 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr2 + x3, xmask) tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tmp9 = tmp7 + tmp8 tmp10 = tmp7 > tmp3 tl.store(out_ptr0 + x3, tmp9, xmask) tl.store(out_ptr1 + x3, 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, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_1, primals_2, 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 get_raw_stream(0) triton_poi_fused_convolution_leaky_relu_0[grid(256)](buf1, primals_3, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_3 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, 4, 4, 4), (64, 16, 4, 1)) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_add_convolution_leaky_relu_leaky_relu_backward_1[grid (256)](buf2, primals_5, primals_1, buf3, buf4, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf2 del primals_5 return buf3, primals_1, primals_2, primals_4, buf1, buf4 def conv3x3(in_ch, out_ch, stride=1): """3x3 convolution with padding.""" return nn.Conv2d(in_ch, out_ch, kernel_size=3, stride=stride, padding=1) class ResidualBlockNew(nn.Module): """Simple residual block with two 3x3 convolutions. Args: in_ch (int): number of input channels out_ch (int): number of output channels """ def __init__(self, in_ch, out_ch): super().__init__() self.conv1 = conv3x3(in_ch, out_ch) self.leaky_relu = nn.LeakyReLU(inplace=True) self.conv2 = conv3x3(out_ch, out_ch) def forward(self, input_0): primals_2 = self.conv1.weight primals_3 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
fqhank/HESIC
ResidualBlock
false
6,696
[ "Apache-2.0" ]
1
f15cb8e6822af45f0022ea4887fce915e250ed75
https://github.com/fqhank/HESIC/tree/f15cb8e6822af45f0022ea4887fce915e250ed75
AdaIN
import torch import torch.nn as nn class AdaIN(nn.Module): def __init__(self, style_dim, num_features): super().__init__() self.norm = nn.InstanceNorm2d(num_features, affine=False) self.fc = nn.Linear(style_dim, num_features * 2) def forward(self, x, s): h = self.fc(s) h = h.view(h.size(0), h.size(1), 1, 1) gamma, beta = torch.chunk(h, chunks=2, dim=1) return (1 + gamma) * self.norm(x) + beta def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'style_dim': 4, 'num_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 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__native_batch_norm_legit_add_mul_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, 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 x2 = xindex % 4 x3 = xindex // 4 tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp22 = tl.load(in_ptr1 + (x2 + 8 * x3), xmask, eviction_policy= 'evict_last') tmp23 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last') tmp30 = tl.load(in_ptr1 + (4 + x2 + 8 * x3), xmask, eviction_policy= 'evict_last') tmp31 = tl.load(in_ptr2 + (4 + x2), xmask, eviction_policy='evict_last') tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tl.where(xmask, tmp1, 0) tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.full([XBLOCK, 1], 16, tl.int32) tmp9 = tmp8.to(tl.float32) tmp10 = tmp7 / tmp9 tmp11 = tmp1 - tmp10 tmp12 = tmp11 * tmp11 tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK]) tmp15 = tl.where(xmask, tmp13, 0) tmp16 = tl.sum(tmp15, 1)[:, None] tmp17 = 16.0 tmp18 = tmp16 / tmp17 tmp19 = 1e-05 tmp20 = tmp18 + tmp19 tmp21 = libdevice.rsqrt(tmp20) tmp24 = tmp22 + tmp23 tmp25 = 1.0 tmp26 = tmp24 + tmp25 tmp27 = tmp0 - tmp10 tmp28 = tmp27 * tmp21 tmp29 = tmp26 * tmp28 tmp32 = tmp30 + tmp31 tmp33 = tmp29 + tmp32 tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp21, xmask) tl.store(out_ptr1 + (r1 + 16 * x0), tmp33, xmask) tl.store(out_ptr0 + x0, tmp10, 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, 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((4, 8), (8, 1), torch.float32) extern_kernels.mm(primals_3, reinterpret_tensor(primals_1, (4, 8), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 1, 1), torch.float32) buf2 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32 ) buf4 = reinterpret_tensor(buf2, (1, 16, 1, 1), (16, 1, 1, 1), 0) del buf2 buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_per_fused__native_batch_norm_legit_add_mul_0[grid(16)](buf4, primals_4, buf0, primals_2, buf1, buf5, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del buf0 del primals_2 return buf5, primals_3, primals_4, buf1, buf4 class AdaINNew(nn.Module): def __init__(self, style_dim, num_features): super().__init__() self.norm = nn.InstanceNorm2d(num_features, affine=False) self.fc = nn.Linear(style_dim, num_features * 2) def forward(self, input_0, input_1): primals_1 = self.fc.weight primals_2 = self.fc.bias primals_4 = input_0 primals_3 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
fpaupier/stargan-v2
AdaIN
false
6,697
[ "MIT" ]
1
18d2e04ed6e6df963b84345e798d94383757aaa2
https://github.com/fpaupier/stargan-v2/tree/18d2e04ed6e6df963b84345e798d94383757aaa2
SelfAttention
import math import torch import torch.nn as nn from torch.nn import init def weights_init(init_type='gaussian'): def init_fun(m): classname = m.__class__.__name__ if (classname.find('Conv') == 0 or classname.find('Linear') == 0 ) and hasattr(m, 'weight'): if init_type == 'gaussian': init.normal(m.weight.data, 0.0, 0.02) elif init_type == 'xavier': init.xavier_normal(m.weight.data, gain=math.sqrt(2)) elif init_type == 'kaiming': init.kaiming_normal(m.weight.data, a=0, mode='fan_in') elif init_type == 'orthogonal': init.orthogonal(m.weight.data, gain=math.sqrt(2)) elif init_type == 'default': pass else: assert 0, 'Unsupported initialization: {}'.format(init_type) if hasattr(m, 'bias') and m.bias is not None: init.constant(m.bias.data, 0.0) return init_fun class SelfAttention(nn.Module): def __init__(self, feat_dim, proj_dim, gamma): super(SelfAttention, self).__init__() self.W_g = torch.nn.Conv2d(feat_dim, proj_dim, kernel_size=(1, 1)) self.W_f = torch.nn.Conv2d(feat_dim, proj_dim, kernel_size=(1, 1)) self.W_h = torch.nn.Conv2d(feat_dim, feat_dim, kernel_size=(1, 1)) self.softmax = torch.nn.Softmax(dim=1) self.gamma = gamma self.W_g.apply(weights_init('gaussian')) self.W_f.apply(weights_init('gaussian')) self.W_h.apply(weights_init('gaussian')) self.W_g self.W_f self.W_h def forward(self, x): f = self.W_f.forward(x) g = self.W_g.forward(x) h = self.W_h.forward(x) N, feat_D, hgt, wid = x.size(0), x.size(1), x.size(2), x.size(3) proj_D = f.size(1) f = f.view(N, proj_D, -1).transpose(1, 2) g = g.view(N, proj_D, -1).transpose(1, 2) h = h.view(N, feat_D, -1).transpose(1, 2) o = [] for idx in range(N): aff = torch.mm(g[idx], f[idx].transpose(0, 1)) aff = self.softmax(aff) cur_o = torch.mm(aff, h[idx]) cur_o = cur_o.transpose(0, 1).contiguous() cur_o = cur_o.view(1, feat_D, hgt, wid) o.append(cur_o) o = torch.cat(o, 0) y = self.gamma * o + (1 - self.gamma) * x return y def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'feat_dim': 4, 'proj_dim': 4, 'gamma': 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 math import torch.nn as nn from torch.nn import init 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 = 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') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) @triton.jit def triton_per_fused__softmax_1(in_ptr0, out_ptr2, 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, float('-inf')) tmp4 = triton_helpers.max2(tmp3, 1)[:, None] tmp5 = tmp0 - tmp4 tmp6 = tl_math.exp(tmp5) tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK]) tmp9 = tl.where(xmask, tmp7, 0) tmp10 = tl.sum(tmp9, 1)[:, None] tmp11 = tmp6 / tmp10 tl.store(out_ptr2 + (r1 + 16 * x0), tmp11, xmask) @triton.jit def triton_poi_fused_add_cat_mul_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl. constexpr): ynumel = 64 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 y1 = yindex // 16 x2 = xindex y0 = yindex % 16 tmp25 = tl.load(in_ptr4 + (y0 + 16 * x2 + 64 * y1), xmask & ymask, eviction_policy='evict_last') tmp0 = y1 tl.full([1, 1], 0, tl.int64) tmp3 = tl.full([1, 1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x2 + 4 * y0), tmp4 & xmask & ymask, eviction_policy='evict_last', other=0.0) tmp6 = tmp0 >= tmp3 tmp7 = tl.full([1, 1], 2, tl.int64) tmp8 = tmp0 < tmp7 tmp9 = tmp6 & tmp8 tmp10 = tl.load(in_ptr1 + (x2 + 4 * y0), tmp9 & xmask & ymask, eviction_policy='evict_last', other=0.0) tmp11 = tmp0 >= tmp7 tmp12 = tl.full([1, 1], 3, tl.int64) tmp13 = tmp0 < tmp12 tmp14 = tmp11 & tmp13 tmp15 = tl.load(in_ptr2 + (x2 + 4 * y0), tmp14 & xmask & ymask, eviction_policy='evict_last', other=0.0) tmp16 = tmp0 >= tmp12 tl.full([1, 1], 4, tl.int64) tmp19 = tl.load(in_ptr3 + (x2 + 4 * y0), tmp16 & xmask & ymask, eviction_policy='evict_last', other=0.0) tmp20 = tl.where(tmp14, tmp15, tmp19) tmp21 = tl.where(tmp9, tmp10, tmp20) tmp22 = tl.where(tmp4, tmp5, tmp21) tmp23 = 4.0 tmp24 = tmp22 * tmp23 tmp26 = -3.0 tmp27 = tmp25 * tmp26 tmp28 = tmp24 + tmp27 tl.store(out_ptr0 + (y0 + 16 * x2 + 64 * y1), tmp28, xmask & ymask) 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, 1, 1), (4, 1, 1, 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, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_7, (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, 4, 4), (64, 16, 4, 1)) buf1 = extern_kernels.convolution(primals_3, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 4, 4), (64, 16, 4, 1)) buf2 = extern_kernels.convolution(primals_3, primals_6, 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, 4, 4), (64, 16, 4, 1)) buf3 = buf1 del buf1 get_raw_stream(0) triton_poi_fused_convolution_0[grid(256)](buf3, primals_5, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf4 = buf0 del buf0 triton_poi_fused_convolution_0[grid(256)](buf4, primals_2, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf5 = empty_strided_cuda((16, 16), (16, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf3, (16, 4), (1, 16), 0), reinterpret_tensor(buf4, (4, 16), (16, 1), 0), out=buf5) buf8 = empty_strided_cuda((16, 16), (16, 1), torch.float32) triton_per_fused__softmax_1[grid(16)](buf5, buf8, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) buf9 = buf2 del buf2 triton_poi_fused_convolution_0[grid(256)](buf9, primals_7, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 buf10 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(buf8, reinterpret_tensor(buf9, (16, 4), (1, 16), 0), out=buf10) buf11 = buf5 del buf5 extern_kernels.mm(reinterpret_tensor(buf3, (16, 4), (1, 16), 64), reinterpret_tensor(buf4, (4, 16), (16, 1), 64), out=buf11) buf14 = empty_strided_cuda((16, 16), (16, 1), torch.float32) triton_per_fused__softmax_1[grid(16)](buf11, buf14, 16, 16, XBLOCK= 8, num_warps=2, num_stages=1) buf15 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(buf14, reinterpret_tensor(buf9, (16, 4), (1, 16), 64), out=buf15) buf16 = buf11 del buf11 extern_kernels.mm(reinterpret_tensor(buf3, (16, 4), (1, 16), 128), reinterpret_tensor(buf4, (4, 16), (16, 1), 128), out=buf16) buf19 = empty_strided_cuda((16, 16), (16, 1), torch.float32) triton_per_fused__softmax_1[grid(16)](buf16, buf19, 16, 16, XBLOCK= 8, num_warps=2, num_stages=1) buf20 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(buf19, reinterpret_tensor(buf9, (16, 4), (1, 16), 128), out=buf20) buf21 = buf16 del buf16 extern_kernels.mm(reinterpret_tensor(buf3, (16, 4), (1, 16), 192), reinterpret_tensor(buf4, (4, 16), (16, 1), 192), out=buf21) buf24 = empty_strided_cuda((16, 16), (16, 1), torch.float32) triton_per_fused__softmax_1[grid(16)](buf21, buf24, 16, 16, XBLOCK= 8, num_warps=2, num_stages=1) buf25 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(buf24, reinterpret_tensor(buf9, (16, 4), (1, 16), 192), out=buf25) buf26 = reinterpret_tensor(buf21, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf21 triton_poi_fused_add_cat_mul_2[grid(64, 4)](buf10, buf15, buf20, buf25, primals_3, buf26, 64, 4, XBLOCK=4, YBLOCK=64, num_warps= 4, num_stages=1) del buf10 del buf15 del buf20 del buf25 return (buf26, primals_1, primals_3, primals_4, primals_6, reinterpret_tensor(buf4, (4, 16), (16, 1), 0), buf8, reinterpret_tensor(buf4, (4, 16), (16, 1), 64), buf14, reinterpret_tensor(buf4, (4, 16), (16, 1), 128), buf19, reinterpret_tensor(buf4, (4, 16), (16, 1), 192), buf24, reinterpret_tensor(buf9, (4, 16), (16, 1), 192), reinterpret_tensor (buf3, (4, 16), (16, 1), 192), reinterpret_tensor(buf9, (4, 16), ( 16, 1), 128), reinterpret_tensor(buf3, (4, 16), (16, 1), 128), reinterpret_tensor(buf9, (4, 16), (16, 1), 64), reinterpret_tensor( buf3, (4, 16), (16, 1), 64), reinterpret_tensor(buf9, (4, 16), (16, 1), 0), reinterpret_tensor(buf3, (4, 16), (16, 1), 0)) def weights_init(init_type='gaussian'): def init_fun(m): classname = m.__class__.__name__ if (classname.find('Conv') == 0 or classname.find('Linear') == 0 ) and hasattr(m, 'weight'): if init_type == 'gaussian': init.normal(m.weight.data, 0.0, 0.02) elif init_type == 'xavier': init.xavier_normal(m.weight.data, gain=math.sqrt(2)) elif init_type == 'kaiming': init.kaiming_normal(m.weight.data, a=0, mode='fan_in') elif init_type == 'orthogonal': init.orthogonal(m.weight.data, gain=math.sqrt(2)) elif init_type == 'default': pass else: assert 0, 'Unsupported initialization: {}'.format(init_type) if hasattr(m, 'bias') and m.bias is not None: init.constant(m.bias.data, 0.0) return init_fun class SelfAttentionNew(nn.Module): def __init__(self, feat_dim, proj_dim, gamma): super(SelfAttentionNew, self).__init__() self.W_g = torch.nn.Conv2d(feat_dim, proj_dim, kernel_size=(1, 1)) self.W_f = torch.nn.Conv2d(feat_dim, proj_dim, kernel_size=(1, 1)) self.W_h = torch.nn.Conv2d(feat_dim, feat_dim, kernel_size=(1, 1)) self.softmax = torch.nn.Softmax(dim=1) self.gamma = gamma self.W_g.apply(weights_init('gaussian')) self.W_f.apply(weights_init('gaussian')) self.W_h.apply(weights_init('gaussian')) self.W_g self.W_f self.W_h def forward(self, input_0): primals_1 = self.W_g.weight primals_2 = self.W_g.bias primals_4 = self.W_f.weight primals_5 = self.W_f.bias primals_6 = self.W_h.weight primals_7 = self.W_h.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
fanyix/flownet2
SelfAttention
false
6,698
[ "Apache-2.0" ]
1
0643beef59eeaf4cf4907d0d51f486ffd713363f
https://github.com/fanyix/flownet2/tree/0643beef59eeaf4cf4907d0d51f486ffd713363f
dense_warp
import torch import torch.nn as nn class dense_warp(nn.Module): def __init__(self): super().__init__() def forward(self, h1, cost): g2 = torch.zeros_like(h1) clone_h1 = h1.detach() if h1.device.type == 'cuda': g2 = g2 clone_h1 = clone_h1 for d in range(cost.size()[-3]): g2[:, :, :, 0:cost.size()[-1] - d] += cost[:, d:d + 1, :, 0: cost.size()[-1] - d].mul(clone_h1[:, :, :, d:cost.size()[-1]]) return g2 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_add_mul_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 x0 = xindex % 4 x3 = xindex // 64 x4 = xindex % 16 x5 = xindex x1 = xindex // 4 % 4 x6 = xindex // 4 tmp61 = tl.load(in_ptr0 + (x4 + 64 * x3), xmask, eviction_policy= 'evict_last') tmp62 = tl.load(in_ptr1 + x5, xmask) tmp0 = x0 tmp1 = tl.full([1], 2, tl.int64) tmp2 = tmp0 < tmp1 tmp3 = tl.full([1], 3, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tmp4 & tmp2 tmp6 = tmp4 & tmp5 tmp7 = tl.load(in_ptr0 + (x4 + 64 * x3), tmp6 & xmask, eviction_policy= 'evict_last', other=0.0) tmp8 = tl.load(in_ptr1 + x5, tmp6 & xmask, other=0.0) tmp9 = tmp7 * tmp8 tmp10 = tl.load(in_ptr0 + (16 + x4 + 64 * x3), tmp6 & xmask, eviction_policy='evict_last', other=0.0) tmp11 = tl.load(in_ptr1 + (1 + x5), tmp6 & xmask, other=0.0) tmp12 = tmp10 * tmp11 tmp13 = tmp9 + tmp12 tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype) tmp15 = tl.where(tmp6, tmp13, tmp14) tmp16 = tl.load(in_ptr0 + (x4 + 64 * x3), tmp5 & xmask, eviction_policy ='evict_last', other=0.0) tmp17 = tl.load(in_ptr1 + x5, tmp5 & xmask, other=0.0) tmp18 = tmp16 * tmp17 tmp19 = tl.where(tmp4, tmp15, tmp18) tmp20 = tl.full(tmp19.shape, 0.0, tmp19.dtype) tmp21 = tl.where(tmp5, tmp19, tmp20) tmp22 = tl.load(in_ptr0 + (16 + x4 + 64 * x3), tmp5 & xmask, eviction_policy='evict_last', other=0.0) tmp23 = tl.load(in_ptr1 + (1 + x5), tmp5 & xmask, other=0.0) tmp24 = tmp22 * tmp23 tmp25 = tmp18 + tmp24 tmp26 = tl.full(tmp25.shape, 0.0, tmp25.dtype) tmp27 = tl.where(tmp5, tmp25, tmp26) tmp28 = tl.load(in_ptr0 + (x4 + 64 * x3), tmp2 & xmask, eviction_policy ='evict_last', other=0.0) tmp29 = tl.load(in_ptr1 + x5, tmp2 & xmask, other=0.0) tmp30 = tmp28 * tmp29 tmp31 = tl.where(tmp4, tmp27, tmp30) tmp32 = tl.where(tmp4, tmp21, tmp31) tmp33 = tl.load(in_ptr0 + (32 + x4 + 64 * x3), tmp2 & xmask, eviction_policy='evict_last', other=0.0) tmp34 = tl.load(in_ptr1 + (2 + x5), tmp2 & xmask, other=0.0) tmp35 = tmp33 * tmp34 tmp36 = tmp32 + tmp35 tmp37 = tl.full(tmp36.shape, 0.0, tmp36.dtype) tmp38 = tl.where(tmp2, tmp36, tmp37) tmp39 = tmp4 & tmp4 tmp40 = tl.load(in_ptr0 + (x4 + 64 * x3), tmp39 & xmask, eviction_policy='evict_last', other=0.0) tmp41 = tl.load(in_ptr1 + x5, tmp39 & xmask, other=0.0) tmp42 = tmp40 * tmp41 tmp43 = tl.load(in_ptr0 + (16 + x4 + 64 * x3), tmp39 & xmask, eviction_policy='evict_last', other=0.0) tmp44 = tl.load(in_ptr1 + (1 + x5), tmp39 & xmask, other=0.0) tmp45 = tmp43 * tmp44 tmp46 = tmp42 + tmp45 tmp47 = tl.full(tmp46.shape, 0.0, tmp46.dtype) tmp48 = tl.where(tmp39, tmp46, tmp47) tmp49 = tl.load(in_ptr0 + (x4 + 64 * x3), tmp4 & xmask, eviction_policy ='evict_last', other=0.0) tmp50 = tl.load(in_ptr1 + x5, tmp4 & xmask, other=0.0) tmp51 = tmp49 * tmp50 tmp52 = tl.where(tmp4, tmp48, tmp51) tmp53 = tl.full(tmp52.shape, 0.0, tmp52.dtype) tmp54 = tl.where(tmp4, tmp52, tmp53) tmp55 = tl.load(in_ptr0 + (16 + x4 + 64 * x3), tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp56 = tl.load(in_ptr1 + (1 + x5), tmp4 & xmask, other=0.0) tmp57 = tmp55 * tmp56 tmp58 = tmp51 + tmp57 tmp59 = tl.full(tmp58.shape, 0.0, tmp58.dtype) tmp60 = tl.where(tmp4, tmp58, tmp59) tmp63 = tmp61 * tmp62 tmp64 = tl.where(tmp4, tmp60, tmp63) tmp65 = tl.where(tmp4, tmp54, tmp64) tmp66 = tl.where(tmp2, tmp38, tmp65) tmp67 = tl.full([1], 1, tl.int64) tmp68 = tmp0 < tmp67 tmp69 = tmp68 & tmp68 tmp2 & tmp69 tmp71 = tl.where(tmp2, tmp66, tmp66) tmp72 = tl.load(in_ptr0 + (48 + 4 * x1 + 64 * x3), tmp69 & xmask, eviction_policy='evict_last', other=0.0) tmp73 = tl.load(in_ptr1 + (3 + 4 * x6), tmp69 & xmask, eviction_policy= 'evict_last', other=0.0) tmp74 = tmp72 * tmp73 tmp75 = tmp71 + tmp74 tmp76 = tl.full(tmp75.shape, 0.0, tmp75.dtype) tmp77 = tl.where(tmp69, tmp75, tmp76) tmp2 & tmp68 tmp79 = tl.where(tmp68, tmp77, tmp71) tmp80 = tl.full(tmp79.shape, 0.0, tmp79.dtype) tmp81 = tl.where(tmp68, tmp79, tmp80) tmp82 = tl.load(in_ptr0 + (48 + 4 * x1 + 64 * x3), tmp68 & xmask, eviction_policy='evict_last', other=0.0) tmp83 = tl.load(in_ptr1 + (3 + 4 * x6), tmp68 & xmask, eviction_policy= 'evict_last', other=0.0) tmp84 = tmp82 * tmp83 tmp85 = tmp71 + tmp84 tmp86 = tl.full(tmp85.shape, 0.0, tmp85.dtype) tmp87 = tl.where(tmp68, tmp85, tmp86) tmp88 = tl.where(tmp68, tmp87, tmp71) tmp89 = tl.where(tmp68, tmp81, tmp88) tl.store(in_out_ptr0 + x5, tmp89, 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 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_add_mul_0[grid(256)](buf1, arg1_1, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf1, class dense_warpNew(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]
fqhank/HESIC
dense_warp
false
6,699
[ "Apache-2.0" ]
1
f15cb8e6822af45f0022ea4887fce915e250ed75
https://github.com/fqhank/HESIC/tree/f15cb8e6822af45f0022ea4887fce915e250ed75
KernelMatcher
import torch from typing import Dict import torch.nn as nn import torch.nn.functional as F class KernelMatcher(nn.Module): def __init__(self, embed_dim: 'int', kernel_num: 'int'=21) ->None: super(KernelMatcher, self).__init__() self._embed_dim = embed_dim self._kernel_num = kernel_num mus, sigmas = self.kernel_init(self._kernel_num) self._mus = nn.Parameter(mus, requires_grad=False) self._sigmas = nn.Parameter(sigmas, requires_grad=False) def kernel_init(self, kernel_num: 'int') ->Dict[str, torch.Tensor]: mus = [1] bin_size = 2.0 / (kernel_num - 1) mus.append(1 - bin_size / 2) for i in range(1, kernel_num - 1): mus.append(mus[i] - bin_size) mus = torch.tensor(mus).view(1, 1, 1, kernel_num) sigmas = [0.001] sigmas += [0.1] * (kernel_num - 1) sigmas = torch.tensor(sigmas).view(1, 1, 1, kernel_num) return mus, sigmas def forward(self, k_embed: 'torch.Tensor', k_mask: 'torch.Tensor', v_embed: 'torch.Tensor', v_mask: 'torch.Tensor') ->torch.Tensor: k_embed = k_embed * k_mask.unsqueeze(-1) v_embed = v_embed * v_mask.unsqueeze(-1) k_by_v_mask = torch.bmm(k_mask.float().unsqueeze(-1), v_mask.float( ).unsqueeze(-1).transpose(1, 2)) k_norm = F.normalize(k_embed, p=2, dim=2, eps=1e-10) v_norm = F.normalize(v_embed, p=2, dim=2, eps=1e-10) inter = (torch.bmm(k_norm, v_norm.transpose(1, 2)) * k_by_v_mask ).unsqueeze(-1) kernel_outputs = torch.exp(-(inter - self._mus) ** 2 / self._sigmas ** 2 / 2) kernel_outputs = kernel_outputs.sum(dim=2).clamp(min=1e-10).log( ) * 0.01 logits = kernel_outputs.sum(dim=1) return logits 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 [[], {'embed_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 from typing import Dict 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_clamp_min_linalg_vector_norm_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 x0 = xindex % 4 x2 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask) tmp4 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 * tmp1 tmp3 = tmp2 * tmp2 tmp5 = tmp4 * tmp1 tmp6 = tmp5 * tmp5 tmp7 = tmp3 + tmp6 tmp9 = tmp8 * tmp1 tmp10 = tmp9 * tmp9 tmp11 = tmp7 + tmp10 tmp13 = tmp12 * tmp1 tmp14 = tmp13 * tmp13 tmp15 = tmp11 + tmp14 tmp16 = libdevice.sqrt(tmp15) tmp17 = 1e-10 tmp18 = triton_helpers.maximum(tmp16, tmp17) tl.store(out_ptr0 + x2, tmp18, xmask) @triton.jit def triton_poi_fused_div_mul_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 x3 = xindex % 16 x4 = xindex // 4 x5 = xindex tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last') tmp2 = tmp0 * tmp1 tmp4 = tmp2 / tmp3 tl.store(out_ptr0 + x5, tmp4, xmask) @triton.jit def triton_poi_fused_div_exp_neg_pow_sub_sum_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 336 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 21 x0 = xindex % 21 x2 = xindex tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp13 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp14 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp23 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp24 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp33 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp34 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 * tmp1 tmp4 = tmp2 - tmp3 tmp5 = tmp4 * tmp4 tmp6 = -tmp5 tmp8 = tmp7 * tmp7 tmp9 = tmp6 / tmp8 tmp10 = 0.5 tmp11 = tmp9 * tmp10 tmp12 = tl_math.exp(tmp11) tmp15 = tmp13 * tmp14 tmp16 = tmp15 - tmp3 tmp17 = tmp16 * tmp16 tmp18 = -tmp17 tmp19 = tmp18 / tmp8 tmp20 = tmp19 * tmp10 tmp21 = tl_math.exp(tmp20) tmp22 = tmp12 + tmp21 tmp25 = tmp23 * tmp24 tmp26 = tmp25 - tmp3 tmp27 = tmp26 * tmp26 tmp28 = -tmp27 tmp29 = tmp28 / tmp8 tmp30 = tmp29 * tmp10 tmp31 = tl_math.exp(tmp30) tmp32 = tmp22 + tmp31 tmp35 = tmp33 * tmp34 tmp36 = tmp35 - tmp3 tmp37 = tmp36 * tmp36 tmp38 = -tmp37 tmp39 = tmp38 / tmp8 tmp40 = tmp39 * tmp10 tmp41 = tl_math.exp(tmp40) tmp42 = tmp32 + tmp41 tl.store(out_ptr0 + x2, tmp42, xmask) @triton.jit def triton_poi_fused_clamp_log_mul_sum_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 84 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 21 x1 = xindex // 21 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 84 * x1), xmask) tmp6 = tl.load(in_ptr0 + (21 + x0 + 84 * x1), xmask) tmp11 = tl.load(in_ptr0 + (42 + x0 + 84 * x1), xmask) tmp16 = tl.load(in_ptr0 + (63 + x0 + 84 * x1), xmask) tmp1 = 1e-10 tmp2 = triton_helpers.maximum(tmp0, tmp1) tmp3 = tl_math.log(tmp2) tmp4 = 0.01 tmp5 = tmp3 * tmp4 tmp7 = triton_helpers.maximum(tmp6, tmp1) tmp8 = tl_math.log(tmp7) tmp9 = tmp8 * tmp4 tmp10 = tmp5 + tmp9 tmp12 = triton_helpers.maximum(tmp11, tmp1) tmp13 = tl_math.log(tmp12) tmp14 = tmp13 * tmp4 tmp15 = tmp10 + tmp14 tmp17 = triton_helpers.maximum(tmp16, tmp1) tmp18 = tl_math.log(tmp17) tmp19 = tmp18 * tmp4 tmp20 = tmp15 + tmp19 tl.store(out_ptr0 + x2, tmp20, xmask) def call(args): arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1 = args args.clear() assert_size_stride(arg0_1, (4, 4), (4, 1)) assert_size_stride(arg1_1, (4, 4), (4, 1)) assert_size_stride(arg2_1, (4, 4), (4, 1)) assert_size_stride(arg3_1, (4, 4), (4, 1)) assert_size_stride(arg4_1, (1, 1, 1, 21), (21, 21, 21, 1)) assert_size_stride(arg5_1, (1, 1, 1, 21), (21, 21, 21, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) get_raw_stream(0) triton_poi_fused_clamp_min_linalg_vector_norm_mul_0[grid(16)](arg1_1, arg0_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) buf1 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) triton_poi_fused_clamp_min_linalg_vector_norm_mul_0[grid(16)](arg3_1, arg2_1, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_div_mul_1[grid(64)](arg1_1, arg0_1, buf0, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg1_1 del buf0 buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_div_mul_1[grid(64)](arg3_1, arg2_1, buf1, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg3_1 del buf1 buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf2, reinterpret_tensor(buf3, (4, 4, 4), (16, 1, 4), 0), out=buf4) del buf2 buf5 = buf3 del buf3 extern_kernels.bmm(reinterpret_tensor(arg0_1, (4, 4, 1), (4, 1, 1), 0), reinterpret_tensor(arg2_1, (4, 1, 4), (4, 1, 1), 0), out=buf5) del arg0_1 del arg2_1 buf6 = empty_strided_cuda((4, 4, 21), (84, 21, 1), torch.float32) triton_poi_fused_div_exp_neg_pow_sub_sum_2[grid(336)](buf4, buf5, arg4_1, arg5_1, buf6, 336, XBLOCK=128, num_warps=4, num_stages=1) del arg4_1 del arg5_1 del buf4 del buf5 buf7 = empty_strided_cuda((4, 21), (21, 1), torch.float32) triton_poi_fused_clamp_log_mul_sum_3[grid(84)](buf6, buf7, 84, XBLOCK=128, num_warps=4, num_stages=1) del buf6 return buf7, class KernelMatcherNew(nn.Module): def __init__(self, embed_dim: 'int', kernel_num: 'int'=21) ->None: super(KernelMatcherNew, self).__init__() self._embed_dim = embed_dim self._kernel_num = kernel_num mus, sigmas = self.kernel_init(self._kernel_num) self._mus = nn.Parameter(mus, requires_grad=False) self._sigmas = nn.Parameter(sigmas, requires_grad=False) def kernel_init(self, kernel_num: 'int') ->Dict[str, torch.Tensor]: mus = [1] bin_size = 2.0 / (kernel_num - 1) mus.append(1 - bin_size / 2) for i in range(1, kernel_num - 1): mus.append(mus[i] - bin_size) mus = torch.tensor(mus).view(1, 1, 1, kernel_num) sigmas = [0.001] sigmas += [0.1] * (kernel_num - 1) sigmas = torch.tensor(sigmas).view(1, 1, 1, kernel_num) return mus, sigmas def forward(self, input_0, input_1, input_2, input_3): arg4_1 = self._mus arg5_1 = self._sigmas arg0_1 = input_0 arg1_1 = input_1 arg2_1 = input_2 arg3_1 = input_3 output = call([arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1]) return output[0]
fengtaoo/opmft
KernelMatcher
false
6,700
[ "MIT" ]
1
64f2a12c724295cd913eda02502f2e2a20f2dd55
https://github.com/fengtaoo/opmft/tree/64f2a12c724295cd913eda02502f2e2a20f2dd55
ResBlk
import math import torch import torch.nn as nn import torch.nn.functional as F def normalize(x, eps=1e-06): """Apply min-max normalization.""" x = x.contiguous() N, C, H, W = x.size() x_ = x.view(N * C, -1) max_val = torch.max(x_, dim=1, keepdim=True)[0] min_val = torch.min(x_, dim=1, keepdim=True)[0] x_ = (x_ - min_val) / (max_val - min_val + eps) out = x_.view(N, C, H, W) return out class ResBlk(nn.Module): def __init__(self, dim_in, dim_out, actv=nn.LeakyReLU(0.2), normalize= False, downsample=False): super().__init__() self.actv = actv self.normalize = normalize self.downsample = downsample self.learned_sc = dim_in != dim_out self._build_weights(dim_in, dim_out) def _build_weights(self, dim_in, dim_out): self.conv1 = nn.Conv2d(dim_in, dim_in, 3, 1, 1) self.conv2 = nn.Conv2d(dim_in, dim_out, 3, 1, 1) if self.normalize: self.norm1 = nn.InstanceNorm2d(dim_in, affine=True) self.norm2 = nn.InstanceNorm2d(dim_in, affine=True) if self.learned_sc: self.conv1x1 = nn.Conv2d(dim_in, dim_out, 1, 1, 0, bias=False) def _shortcut(self, x): if self.learned_sc: x = self.conv1x1(x) if self.downsample: x = F.avg_pool2d(x, 2) return x def _residual(self, x): if self.normalize: x = self.norm1(x) x = self.actv(x) x = self.conv1(x) if self.downsample: x = F.avg_pool2d(x, 2) if self.normalize: x = self.norm2(x) x = self.actv(x) x = self.conv2(x) return x def forward(self, x): x = self._shortcut(x) + self._residual(x) return x / math.sqrt(2) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'dim_in': 4, 'dim_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 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_leaky_relu_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 = tmp0 > tmp1 tmp3 = 0.2 tmp4 = tmp0 * tmp3 tmp5 = tl.where(tmp2, tmp0, tmp4) tl.store(out_ptr0 + x0, tmp5, xmask) @triton.jit def triton_poi_fused_convolution_leaky_relu_1(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 x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.2 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x3, tmp4, xmask) tl.store(out_ptr1 + x3, tmp7, xmask) @triton.jit def triton_poi_fused_add_convolution_div_2(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 x3 = xindex x1 = xindex // 16 % 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 tmp5 = 0.7071067811865475 tmp6 = tmp4 * tmp5 tl.store(in_out_ptr0 + x3, 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, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4, 3, 3), (36, 9, 3, 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, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_leaky_relu_0[grid(256)](primals_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=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, 4, 4, 4), (64, 16, 4, 1)) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_convolution_leaky_relu_1[grid(256)](buf1, primals_3, buf2, buf3, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf1 del primals_3 buf4 = extern_kernels.convolution(buf3, primals_4, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 4, 4, 4), (64, 16, 4, 1)) buf5 = buf4 del buf4 triton_poi_fused_add_convolution_div_2[grid(256)](buf5, primals_1, primals_5, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 del primals_5 return buf5, primals_2, primals_4, buf0, buf2, buf3 def normalize(x, eps=1e-06): """Apply min-max normalization.""" x = x.contiguous() N, C, H, W = x.size() x_ = x.view(N * C, -1) max_val = torch.max(x_, dim=1, keepdim=True)[0] min_val = torch.min(x_, dim=1, keepdim=True)[0] x_ = (x_ - min_val) / (max_val - min_val + eps) out = x_.view(N, C, H, W) return out class ResBlkNew(nn.Module): def __init__(self, dim_in, dim_out, actv=nn.LeakyReLU(0.2), normalize= False, downsample=False): super().__init__() self.actv = actv self.normalize = normalize self.downsample = downsample self.learned_sc = dim_in != dim_out self._build_weights(dim_in, dim_out) def _build_weights(self, dim_in, dim_out): self.conv1 = nn.Conv2d(dim_in, dim_in, 3, 1, 1) self.conv2 = nn.Conv2d(dim_in, dim_out, 3, 1, 1) if self.normalize: self.norm1 = nn.InstanceNorm2d(dim_in, affine=True) self.norm2 = nn.InstanceNorm2d(dim_in, affine=True) if self.learned_sc: self.conv1x1 = nn.Conv2d(dim_in, dim_out, 1, 1, 0, bias=False) def _shortcut(self, x): if self.learned_sc: x = self.conv1x1(x) if self.downsample: x = F.avg_pool2d(x, 2) return x def _residual(self, x): if self.normalize: x = self.norm1(x) x = self.actv(x) x = self.conv1(x) if self.downsample: x = F.avg_pool2d(x, 2) if self.normalize: x = self.norm2(x) x = self.actv(x) x = self.conv2(x) return x def forward(self, input_0): primals_2 = self.conv1.weight primals_3 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
fpaupier/stargan-v2
ResBlk
false
6,701
[ "MIT" ]
1
18d2e04ed6e6df963b84345e798d94383757aaa2
https://github.com/fpaupier/stargan-v2/tree/18d2e04ed6e6df963b84345e798d94383757aaa2
Enhancement_Block
import torch import torch.nn as nn def conv3x3(in_ch, out_ch, stride=1): """3x3 convolution with padding.""" return nn.Conv2d(in_ch, out_ch, kernel_size=3, stride=stride, padding=1) class ResidualBlock(nn.Module): """Simple residual block with two 3x3 convolutions. Args: in_ch (int): number of input channels out_ch (int): number of output channels """ def __init__(self, in_ch, out_ch): super().__init__() self.conv1 = conv3x3(in_ch, out_ch) self.leaky_relu = nn.LeakyReLU(inplace=True) self.conv2 = conv3x3(out_ch, out_ch) def forward(self, x): identity = x out = self.conv1(x) out = self.leaky_relu(out) out = self.conv2(out) out = self.leaky_relu(out) out = out + identity return out class Enhancement_Block(nn.Module): def __init__(self): super().__init__() self.RB1 = ResidualBlock(32, 32) self.RB2 = ResidualBlock(32, 32) self.RB3 = ResidualBlock(32, 32) def forward(self, x): identity = x out = self.RB1(x) out = self.RB2(out) out = self.RB3(out) out = out + identity return out def get_inputs(): return [torch.rand([4, 32, 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 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_leaky_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 // 4096 % 32 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(in_out_ptr0 + x3, tmp7, None) @triton.jit def triton_poi_fused_add_convolution_leaky_relu_leaky_relu_backward_1(in_ptr0, in_ptr1, in_ptr2, 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) x3 = xindex x1 = xindex // 4096 % 32 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp8 = tl.load(in_ptr2 + x3, None) tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tmp9 = tmp7 + tmp8 tmp10 = tmp7 > tmp3 tl.store(out_ptr0 + x3, tmp9, None) tl.store(out_ptr1 + x3, tmp10, None) @triton.jit def triton_poi_fused_add_convolution_leaky_relu_leaky_relu_backward_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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) x3 = xindex x1 = xindex // 4096 % 32 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp8 = tl.load(in_ptr2 + x3, None) tmp10 = tl.load(in_ptr3 + x3, None) tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tmp9 = tmp7 + tmp8 tmp11 = tmp9 + tmp10 tmp12 = tmp7 > tmp3 tl.store(out_ptr0 + x3, tmp11, None) tl.store(out_ptr1 + x3, tmp12, 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) = args args.clear() assert_size_stride(primals_1, (4, 32, 64, 64), (131072, 4096, 64, 1)) assert_size_stride(primals_2, (32, 32, 3, 3), (288, 9, 3, 1)) assert_size_stride(primals_3, (32,), (1,)) assert_size_stride(primals_4, (32, 32, 3, 3), (288, 9, 3, 1)) assert_size_stride(primals_5, (32,), (1,)) assert_size_stride(primals_6, (32, 32, 3, 3), (288, 9, 3, 1)) assert_size_stride(primals_7, (32,), (1,)) assert_size_stride(primals_8, (32, 32, 3, 3), (288, 9, 3, 1)) assert_size_stride(primals_9, (32,), (1,)) assert_size_stride(primals_10, (32, 32, 3, 3), (288, 9, 3, 1)) assert_size_stride(primals_11, (32,), (1,)) assert_size_stride(primals_12, (32, 32, 3, 3), (288, 9, 3, 1)) assert_size_stride(primals_13, (32,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 32, 64, 64), (131072, 4096, 64, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_leaky_relu_0[grid(524288)](buf1, primals_3, 524288, XBLOCK=512, num_warps=8, num_stages=1) del primals_3 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, 32, 64, 64), (131072, 4096, 64, 1)) buf3 = empty_strided_cuda((4, 32, 64, 64), (131072, 4096, 64, 1), torch.float32) buf14 = empty_strided_cuda((4, 32, 64, 64), (131072, 4096, 64, 1), torch.bool) triton_poi_fused_add_convolution_leaky_relu_leaky_relu_backward_1[grid (524288)](buf2, primals_5, primals_1, buf3, buf14, 524288, XBLOCK=512, num_warps=8, 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, 32, 64, 64), (131072, 4096, 64, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_leaky_relu_0[grid(524288)](buf5, primals_7, 524288, XBLOCK=512, num_warps=8, 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, 32, 64, 64), (131072, 4096, 64, 1)) buf7 = buf2 del buf2 buf13 = empty_strided_cuda((4, 32, 64, 64), (131072, 4096, 64, 1), torch.bool) triton_poi_fused_add_convolution_leaky_relu_leaky_relu_backward_1[grid (524288)](buf6, primals_9, buf3, buf7, buf13, 524288, XBLOCK= 512, num_warps=8, 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, 32, 64, 64), (131072, 4096, 64, 1)) buf9 = buf8 del buf8 triton_poi_fused_convolution_leaky_relu_0[grid(524288)](buf9, primals_11, 524288, XBLOCK=512, num_warps=8, num_stages=1) del primals_11 buf10 = extern_kernels.convolution(buf9, primals_12, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf10, (4, 32, 64, 64), (131072, 4096, 64, 1)) buf11 = buf6 del buf6 buf12 = empty_strided_cuda((4, 32, 64, 64), (131072, 4096, 64, 1), torch.bool) triton_poi_fused_add_convolution_leaky_relu_leaky_relu_backward_2[grid (524288)](buf10, primals_13, buf7, primals_1, buf11, buf12, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del buf10 del primals_13 return (buf11, primals_1, primals_2, primals_4, primals_6, primals_8, primals_10, primals_12, buf1, buf3, buf5, buf7, buf9, buf12, buf13, buf14) def conv3x3(in_ch, out_ch, stride=1): """3x3 convolution with padding.""" return nn.Conv2d(in_ch, out_ch, kernel_size=3, stride=stride, padding=1) class ResidualBlock(nn.Module): """Simple residual block with two 3x3 convolutions. Args: in_ch (int): number of input channels out_ch (int): number of output channels """ def __init__(self, in_ch, out_ch): super().__init__() self.conv1 = conv3x3(in_ch, out_ch) self.leaky_relu = nn.LeakyReLU(inplace=True) self.conv2 = conv3x3(out_ch, out_ch) def forward(self, x): identity = x out = self.conv1(x) out = self.leaky_relu(out) out = self.conv2(out) out = self.leaky_relu(out) out = out + identity return out class Enhancement_BlockNew(nn.Module): def __init__(self): super().__init__() self.RB1 = ResidualBlock(32, 32) self.RB2 = ResidualBlock(32, 32) self.RB3 = ResidualBlock(32, 32) def forward(self, input_0): primals_2 = self.RB1.conv1.weight primals_3 = self.RB1.conv1.bias primals_4 = self.RB1.conv2.weight primals_5 = self.RB1.conv2.bias primals_6 = self.RB2.conv1.weight primals_7 = self.RB2.conv1.bias primals_8 = self.RB2.conv2.weight primals_9 = self.RB2.conv2.bias primals_10 = self.RB3.conv1.weight primals_11 = self.RB3.conv1.bias primals_12 = self.RB3.conv2.weight primals_13 = self.RB3.conv2.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]
fqhank/HESIC
Enhancement_Block
false
6,702
[ "Apache-2.0" ]
1
f15cb8e6822af45f0022ea4887fce915e250ed75
https://github.com/fqhank/HESIC/tree/f15cb8e6822af45f0022ea4887fce915e250ed75
PolicyNetwork
import torch import torch.nn as nn import torch.nn.functional as F class PolicyNetwork(nn.Module): def __init__(self, num_inputs, num_actions, hidden_size, init_w=3e-05): super(PolicyNetwork, self).__init__() self.linear1 = nn.Linear(num_inputs, hidden_size) self.linear2 = nn.Linear(hidden_size, hidden_size) self.linear3 = nn.Linear(hidden_size, num_actions) self.linear3.weight.data.uniform_(-init_w, init_w) self.linear3.bias.data.uniform_(-init_w, init_w) def forward(self, state): """ Get output of the policy Args: state (torch array): State of the dynamical system """ x = F.relu(self.linear1(state)) x = F.relu(self.linear2(x)) x = self.sigmoid_mod(self.linear3(x)) return x def get_action(self, state): state = torch.FloatTensor(state).unsqueeze(0) action = self.forward(state) action_np = action.detach().cpu().numpy()[0] return action_np def tanh_mod(self, x, p=1): x = x.float() x = 2 / (1 + torch.exp(-2 * (x / 100))) - 1 x = x * p return x def sigmoid_mod(self, x, p=1.5): x = x.float() x = (2 / (1 + torch.exp(x) * 1) - 1) * -1 x = x * p return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_inputs': 4, 'num_actions': 4, '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 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 = 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_exp_mul_reciprocal_sub_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 = tl_math.exp(tmp0) tmp2 = 1.0 tmp3 = tmp1 * tmp2 tmp4 = tmp3 + tmp2 tmp5 = tl.full([1], 1, tl.int32) tmp6 = tmp5 / tmp4 tmp7 = 2.0 tmp8 = tmp6 * tmp7 tmp9 = tmp8 - tmp2 tmp10 = -1.0 tmp11 = tmp9 * tmp10 tmp12 = 1.5 tmp13 = tmp11 * tmp12 tl.store(out_ptr0 + x0, tmp13, 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, 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((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 buf7 = 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, buf7, 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 buf6 = 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, buf6, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf4) del primals_7 buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_add_exp_mul_reciprocal_sub_1[grid(256)](buf4, buf5, 256, XBLOCK=256, num_warps=4, num_stages=1) return buf5, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 4), (4, 1), 0), reinterpret_tensor( buf3, (64, 4), (4, 1), 0), buf4, primals_6, buf6, primals_4, buf7 class PolicyNetworkNew(nn.Module): def __init__(self, num_inputs, num_actions, hidden_size, init_w=3e-05): super(PolicyNetworkNew, self).__init__() self.linear1 = nn.Linear(num_inputs, hidden_size) self.linear2 = nn.Linear(hidden_size, hidden_size) self.linear3 = nn.Linear(hidden_size, num_actions) self.linear3.weight.data.uniform_(-init_w, init_w) self.linear3.bias.data.uniform_(-init_w, init_w) def get_action(self, state): state = torch.FloatTensor(state).unsqueeze(0) action = self.forward(state) action_np = action.detach().cpu().numpy()[0] return action_np def tanh_mod(self, x, p=1): x = x.float() x = 2 / (1 + torch.exp(-2 * (x / 100))) - 1 x = x * p return x def sigmoid_mod(self, x, p=1.5): x = x.float() x = (2 / (1 + torch.exp(x) * 1) - 1) * -1 x = x * p return x def forward(self, input_0): primals_1 = self.linear1.weight primals_2 = self.linear1.bias primals_4 = self.linear2.weight primals_5 = self.linear2.bias primals_6 = self.linear3.weight primals_7 = self.linear3.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
frknayk/Reinforcement-Learning-In-Control
PolicyNetwork
false
6,703
[ "MIT" ]
1
24c7eb6fa6b6390ee2dd04f25036c37896ecd944
https://github.com/frknayk/Reinforcement-Learning-In-Control/tree/24c7eb6fa6b6390ee2dd04f25036c37896ecd944
enhance_net_nopool
import torch import torch.nn as nn import torch.nn.functional as F import torch.optim class enhance_net_nopool(nn.Module): def __init__(self): super(enhance_net_nopool, self).__init__() self.relu = nn.ReLU(inplace=True) number_f = 32 self.e_conv1 = nn.Conv2d(3, number_f, 3, 1, 1, bias=True) self.e_conv2 = nn.Conv2d(number_f, number_f, 3, 1, 1, bias=True) self.e_conv3 = nn.Conv2d(number_f, number_f, 3, 1, 1, bias=True) self.e_conv4 = nn.Conv2d(number_f, number_f, 3, 1, 1, bias=True) self.e_conv5 = nn.Conv2d(number_f * 2, number_f, 3, 1, 1, bias=True) self.e_conv6 = nn.Conv2d(number_f * 2, number_f, 3, 1, 1, bias=True) self.e_conv7 = nn.Conv2d(number_f * 2, 24, 3, 1, 1, bias=True) self.maxpool = nn.MaxPool2d(2, stride=2, return_indices=False, ceil_mode=False) self.upsample = nn.UpsamplingBilinear2d(scale_factor=2) def forward(self, x): x1 = self.relu(self.e_conv1(x)) x2 = self.relu(self.e_conv2(x1)) x3 = self.relu(self.e_conv3(x2)) x4 = self.relu(self.e_conv4(x3)) x5 = self.relu(self.e_conv5(torch.cat([x3, x4], 1))) x6 = self.relu(self.e_conv6(torch.cat([x2, x5], 1))) x_r = F.tanh(self.e_conv7(torch.cat([x1, x6], 1))) r1, r2, r3, r4, r5, r6, r7, r8 = torch.split(x_r, 3, dim=1) x = x + r1 * (torch.pow(x, 2) - x) x = x + r2 * (torch.pow(x, 2) - x) x = x + r3 * (torch.pow(x, 2) - x) enhance_image_1 = x + r4 * (torch.pow(x, 2) - x) x = enhance_image_1 + r5 * (torch.pow(enhance_image_1, 2) - enhance_image_1) x = x + r6 * (torch.pow(x, 2) - x) x = x + r7 * (torch.pow(x, 2) - x) enhance_image = x + r8 * (torch.pow(x, 2) - x) r = torch.cat([r1, r2, r3, r4, r5, r6, r7, r8], 1) return enhance_image_1, enhance_image, r 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 import torch.nn as nn import torch.optim 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 // 4096 % 32 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_cat_1(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) x1 = xindex // 4096 % 64 x0 = xindex % 4096 x2 = xindex // 262144 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 32, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 4096 * x1 + 131072 * x2), tmp4, other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 64, tl.int64) tmp9 = tl.load(in_ptr1 + (x0 + 4096 * (-32 + x1) + 131072 * x2), tmp6, other=0.0) tmp10 = tl.load(in_ptr2 + (-32 + x1), tmp6, eviction_policy= 'evict_last', other=0.0) tmp11 = tmp9 + tmp10 tmp12 = tl.full([1], 0, tl.int32) tmp13 = triton_helpers.maximum(tmp12, tmp11) tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype) tmp15 = tl.where(tmp6, tmp13, tmp14) tmp16 = tl.where(tmp4, tmp5, tmp15) tl.store(out_ptr0 + x3, tmp16, None) @triton.jit def triton_poi_fused_convolution_tanh_2(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 % 24 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = libdevice.tanh(tmp2) tl.store(in_out_ptr0 + x3, tmp3, None) @triton.jit def triton_poi_fused_add_mul_pow_sub_3(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, 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 % 12288 x1 = xindex // 12288 tmp0 = tl.load(in_ptr0 + x2, None) tmp1 = tl.load(in_ptr1 + (x0 + 98304 * x1), None) tmp6 = tl.load(in_ptr1 + (12288 + x0 + 98304 * x1), None) tmp11 = tl.load(in_ptr1 + (24576 + x0 + 98304 * x1), None) tmp16 = tl.load(in_ptr1 + (36864 + x0 + 98304 * x1), None) tmp21 = tl.load(in_ptr1 + (49152 + x0 + 98304 * x1), None) tmp26 = tl.load(in_ptr1 + (61440 + x0 + 98304 * x1), None) tmp31 = tl.load(in_ptr1 + (73728 + x0 + 98304 * x1), None) tmp36 = tl.load(in_ptr1 + (86016 + x0 + 98304 * x1), None) tmp2 = tmp0 * tmp0 tmp3 = tmp2 - tmp0 tmp4 = tmp1 * tmp3 tmp5 = tmp0 + tmp4 tmp7 = tmp5 * tmp5 tmp8 = tmp7 - tmp5 tmp9 = tmp6 * tmp8 tmp10 = tmp5 + tmp9 tmp12 = tmp10 * tmp10 tmp13 = tmp12 - tmp10 tmp14 = tmp11 * tmp13 tmp15 = tmp10 + tmp14 tmp17 = tmp15 * tmp15 tmp18 = tmp17 - tmp15 tmp19 = tmp16 * tmp18 tmp20 = tmp15 + tmp19 tmp22 = tmp20 * tmp20 tmp23 = tmp22 - tmp20 tmp24 = tmp21 * tmp23 tmp25 = tmp20 + tmp24 tmp27 = tmp25 * tmp25 tmp28 = tmp27 - tmp25 tmp29 = tmp26 * tmp28 tmp30 = tmp25 + tmp29 tmp32 = tmp30 * tmp30 tmp33 = tmp32 - tmp30 tmp34 = tmp31 * tmp33 tmp35 = tmp30 + tmp34 tmp37 = tmp35 * tmp35 tmp38 = tmp37 - tmp35 tmp39 = tmp36 * tmp38 tmp40 = tmp35 + tmp39 tl.store(out_ptr0 + x2, tmp5, None) tl.store(out_ptr1 + x2, tmp20, None) tl.store(out_ptr2 + x2, tmp40, None) @triton.jit def triton_poi_fused_cat_4(in_ptr0, 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 // 4096 % 24 x0 = xindex % 4096 x2 = xindex // 98304 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 3, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 4096 * x1 + 98304 * x2), tmp4, other=0.0) tmp6 = tmp0 >= tmp3 tmp7 = tl.full([1], 6, tl.int64) tmp8 = tmp0 < tmp7 tmp9 = tmp6 & tmp8 tmp10 = tl.load(in_ptr0 + (12288 + x0 + 4096 * (-3 + x1) + 98304 * x2), tmp9, other=0.0) tmp11 = tmp0 >= tmp7 tmp12 = tl.full([1], 9, tl.int64) tmp13 = tmp0 < tmp12 tmp14 = tmp11 & tmp13 tmp15 = tl.load(in_ptr0 + (24576 + x0 + 4096 * (-6 + x1) + 98304 * x2), tmp14, other=0.0) tmp16 = tmp0 >= tmp12 tmp17 = tl.full([1], 12, tl.int64) tmp18 = tmp0 < tmp17 tmp19 = tmp16 & tmp18 tmp20 = tl.load(in_ptr0 + (36864 + x0 + 4096 * (-9 + x1) + 98304 * x2), tmp19, other=0.0) tmp21 = tmp0 >= tmp17 tmp22 = tl.full([1], 15, tl.int64) tmp23 = tmp0 < tmp22 tmp24 = tmp21 & tmp23 tmp25 = tl.load(in_ptr0 + (49152 + x0 + 4096 * (-12 + x1) + 98304 * x2), tmp24, other=0.0) tmp26 = tmp0 >= tmp22 tmp27 = tl.full([1], 18, tl.int64) tmp28 = tmp0 < tmp27 tmp29 = tmp26 & tmp28 tmp30 = tl.load(in_ptr0 + (61440 + x0 + 4096 * (-15 + x1) + 98304 * x2), tmp29, other=0.0) tmp31 = tmp0 >= tmp27 tmp32 = tl.full([1], 21, tl.int64) tmp33 = tmp0 < tmp32 tmp34 = tmp31 & tmp33 tmp35 = tl.load(in_ptr0 + (73728 + x0 + 4096 * (-18 + x1) + 98304 * x2), tmp34, other=0.0) tmp36 = tmp0 >= tmp32 tl.full([1], 24, tl.int64) tmp39 = tl.load(in_ptr0 + (86016 + x0 + 4096 * (-21 + x1) + 98304 * x2), tmp36, other=0.0) tmp40 = tl.where(tmp34, tmp35, tmp39) tmp41 = tl.where(tmp29, tmp30, tmp40) tmp42 = tl.where(tmp24, tmp25, tmp41) tmp43 = tl.where(tmp19, tmp20, tmp42) tmp44 = tl.where(tmp14, tmp15, tmp43) tmp45 = tl.where(tmp9, tmp10, tmp44) tmp46 = tl.where(tmp4, tmp5, tmp45) tl.store(out_ptr0 + x3, tmp46, None) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_5(in_ptr0, in_ptr1, 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 x1 = xindex // 4096 % 32 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, 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, tmp6, 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) = args args.clear() assert_size_stride(primals_1, (32, 3, 3, 3), (27, 9, 3, 1)) assert_size_stride(primals_2, (32,), (1,)) assert_size_stride(primals_3, (4, 3, 64, 64), (12288, 4096, 64, 1)) assert_size_stride(primals_4, (32, 32, 3, 3), (288, 9, 3, 1)) assert_size_stride(primals_5, (32,), (1,)) assert_size_stride(primals_6, (32, 32, 3, 3), (288, 9, 3, 1)) assert_size_stride(primals_7, (32,), (1,)) assert_size_stride(primals_8, (32, 32, 3, 3), (288, 9, 3, 1)) assert_size_stride(primals_9, (32,), (1,)) assert_size_stride(primals_10, (32, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_11, (32,), (1,)) assert_size_stride(primals_12, (32, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_13, (32,), (1,)) assert_size_stride(primals_14, (24, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_15, (24,), (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, 32, 64, 64), (131072, 4096, 64, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(524288)](buf1, primals_2, 524288, XBLOCK=512, num_warps=8, 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, 32, 64, 64), (131072, 4096, 64, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_relu_0[grid(524288)](buf3, primals_5, 524288, XBLOCK=512, num_warps=8, 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, 32, 64, 64), (131072, 4096, 64, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_relu_0[grid(524288)](buf5, primals_7, 524288, XBLOCK=512, num_warps=8, 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, 32, 64, 64), (131072, 4096, 64, 1)) buf7 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.float32) triton_poi_fused_cat_1[grid(1048576)](buf5, buf6, primals_9, buf7, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) 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, 32, 64, 64), (131072, 4096, 64, 1)) buf9 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.float32) triton_poi_fused_cat_1[grid(1048576)](buf3, buf8, primals_11, buf9, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) buf10 = extern_kernels.convolution(buf9, primals_12, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf10, (4, 32, 64, 64), (131072, 4096, 64, 1)) buf11 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.float32) triton_poi_fused_cat_1[grid(1048576)](buf1, buf10, primals_13, buf11, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) buf12 = extern_kernels.convolution(buf11, primals_14, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf12, (4, 24, 64, 64), (98304, 4096, 64, 1)) buf13 = buf12 del buf12 triton_poi_fused_convolution_tanh_2[grid(393216)](buf13, primals_15, 393216, XBLOCK=512, num_warps=8, num_stages=1) del primals_15 buf14 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.float32) buf15 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.float32) buf16 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.float32) triton_poi_fused_add_mul_pow_sub_3[grid(49152)](primals_3, buf13, buf14, buf15, buf16, 49152, XBLOCK=256, num_warps=4, num_stages=1) buf17 = empty_strided_cuda((4, 24, 64, 64), (98304, 4096, 64, 1), torch.float32) triton_poi_fused_cat_4[grid(393216)](buf13, buf17, 393216, XBLOCK= 1024, num_warps=4, num_stages=1) buf18 = empty_strided_cuda((4, 32, 64, 64), (131072, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_5[grid(524288)]( buf10, primals_13, buf18, 524288, XBLOCK=512, num_warps=8, num_stages=1) del buf10 del primals_13 buf19 = empty_strided_cuda((4, 32, 64, 64), (131072, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_5[grid(524288)]( buf8, primals_11, buf19, 524288, XBLOCK=512, num_warps=8, num_stages=1) del buf8 del primals_11 buf20 = empty_strided_cuda((4, 32, 64, 64), (131072, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_5[grid(524288)]( buf6, primals_9, buf20, 524288, XBLOCK=512, num_warps=8, num_stages=1) del buf6 del primals_9 return (buf15, buf16, buf17, primals_1, primals_3, primals_4, primals_6, primals_8, primals_10, primals_12, primals_14, buf1, buf3, buf5, buf7, buf9, buf11, buf13, reinterpret_tensor(buf13, (4, 3, 64, 64), (98304, 4096, 64, 1), 12288), reinterpret_tensor(buf13, (4, 3, 64, 64), (98304, 4096, 64, 1), 24576), reinterpret_tensor(buf13, (4, 3, 64, 64), (98304, 4096, 64, 1), 36864), reinterpret_tensor(buf13, (4, 3, 64, 64), (98304, 4096, 64, 1), 49152), reinterpret_tensor(buf13, (4, 3, 64, 64), (98304, 4096, 64, 1), 61440), reinterpret_tensor( buf13, (4, 3, 64, 64), (98304, 4096, 64, 1), 73728), reinterpret_tensor(buf13, (4, 3, 64, 64), (98304, 4096, 64, 1), 86016), buf14, buf15, buf18, buf19, buf20) class enhance_net_nopoolNew(nn.Module): def __init__(self): super(enhance_net_nopoolNew, self).__init__() self.relu = nn.ReLU(inplace=True) number_f = 32 self.e_conv1 = nn.Conv2d(3, number_f, 3, 1, 1, bias=True) self.e_conv2 = nn.Conv2d(number_f, number_f, 3, 1, 1, bias=True) self.e_conv3 = nn.Conv2d(number_f, number_f, 3, 1, 1, bias=True) self.e_conv4 = nn.Conv2d(number_f, number_f, 3, 1, 1, bias=True) self.e_conv5 = nn.Conv2d(number_f * 2, number_f, 3, 1, 1, bias=True) self.e_conv6 = nn.Conv2d(number_f * 2, number_f, 3, 1, 1, bias=True) self.e_conv7 = nn.Conv2d(number_f * 2, 24, 3, 1, 1, bias=True) self.maxpool = nn.MaxPool2d(2, stride=2, return_indices=False, ceil_mode=False) self.upsample = nn.UpsamplingBilinear2d(scale_factor=2) def forward(self, input_0): primals_1 = self.e_conv1.weight primals_2 = self.e_conv1.bias primals_4 = self.e_conv2.weight primals_5 = self.e_conv2.bias primals_6 = self.e_conv3.weight primals_7 = self.e_conv3.bias primals_8 = self.e_conv4.weight primals_9 = self.e_conv4.bias primals_10 = self.e_conv5.weight primals_11 = self.e_conv5.bias primals_12 = self.e_conv6.weight primals_13 = self.e_conv6.bias primals_14 = self.e_conv7.weight primals_15 = self.e_conv7.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, primals_12, primals_13, primals_14, primals_15]) return output[0], output[1], output[2]
farhantandia/Applied-CV-Zero-DCE-master
enhance_net_nopool
false
6,704
[ "MIT" ]
1
56a0f8aec799eb5d125f5d9f44f692b9a9a3c990
https://github.com/farhantandia/Applied-CV-Zero-DCE-master/tree/56a0f8aec799eb5d125f5d9f44f692b9a9a3c990
ActorCriticContinuous
import torch from torch import nn import torch.nn.functional as F class ActorCriticContinuous(nn.Module): """ Actor-Critic for continuous action spaces. The network returns a state_value (critic) and action mean and action standarddeviation (actor). The action is the sampled from a normal distribution with mean and std given by the actor. """ def __init__(self, action_dim, state_dim, hidden_dim): super(ActorCriticContinuous, self).__init__() self.fc_1 = nn.Linear(state_dim, hidden_dim) self.fc_2 = nn.Linear(hidden_dim, int(hidden_dim / 2)) self.critic_head = nn.Linear(int(hidden_dim / 2), 1) self.actor_head_mean = nn.Linear(int(hidden_dim / 2), action_dim) self.actor_head_sigma = nn.Linear(int(hidden_dim / 2), action_dim) def forward(self, inp): x = F.leaky_relu(self.fc_1(inp)) x = F.leaky_relu(self.fc_2(x)) state_value = self.critic_head(x) action_mean = self.actor_head_mean(x) action_sigma = F.softplus(self.actor_head_sigma(x) + 0.0001) return action_mean, action_sigma, state_value def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'action_dim': 4, 'state_dim': 4, 'hidden_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, 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_leaky_relu_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 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 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr1 + x2, tmp7, xmask) @triton.jit def triton_poi_fused_leaky_relu_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, 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 % 2 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr1 + x2, tmp7, xmask) @triton.jit def triton_poi_fused_add_softplus_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 x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.0001 tmp2 = tmp0 + tmp1 tmp3 = 20.0 tmp4 = tmp2 > tmp3 tmp5 = tl_math.exp(tmp2) tmp6 = libdevice.log1p(tmp5) tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x0, tmp7, 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, 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, (2, 4), (4, 1)) assert_size_stride(primals_5, (2,), (1,)) assert_size_stride(primals_6, (1, 2), (2, 1)) assert_size_stride(primals_7, (1,), (1,)) assert_size_stride(primals_8, (4, 2), (2, 1)) assert_size_stride(primals_9, (4,), (1,)) assert_size_stride(primals_10, (4, 2), (2, 1)) assert_size_stride(primals_11, (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 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_leaky_relu_0[grid(256)](buf0, primals_2, buf1, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf3 = empty_strided_cuda((64, 2), (2, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf2, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 2), (1, 4), 0), out=buf3) buf4 = empty_strided_cuda((4, 4, 4, 2), (32, 8, 2, 1), torch.bool) buf5 = empty_strided_cuda((4, 4, 4, 2), (32, 8, 2, 1), torch.float32) triton_poi_fused_leaky_relu_1[grid(128)](buf3, primals_5, buf4, buf5, 128, XBLOCK=128, num_warps=4, num_stages=1) del buf3 del primals_5 buf7 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf5, (64, 2), ( 2, 1), 0), reinterpret_tensor(primals_6, (2, 1), (1, 2), 0), alpha=1, beta=1, out=buf7) del primals_7 buf8 = buf0 del buf0 extern_kernels.addmm(primals_9, reinterpret_tensor(buf5, (64, 2), ( 2, 1), 0), reinterpret_tensor(primals_8, (2, 4), (1, 2), 0), alpha=1, beta=1, out=buf8) del primals_9 buf9 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_11, reinterpret_tensor(buf5, (64, 2), (2, 1), 0), reinterpret_tensor(primals_10, (2, 4), (1, 2), 0), alpha=1, beta=1, out=buf9) del primals_11 buf10 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_add_softplus_2[grid(256)](buf9, buf10, 256, XBLOCK =256, num_warps=4, num_stages=1) return reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), buf10, reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf1, reinterpret_tensor(buf2, (64, 4), (4, 1), 0 ), buf4, reinterpret_tensor(buf5, (64, 2), (2, 1), 0 ), buf9, primals_10, primals_8, primals_6, primals_4 class ActorCriticContinuousNew(nn.Module): """ Actor-Critic for continuous action spaces. The network returns a state_value (critic) and action mean and action standarddeviation (actor). The action is the sampled from a normal distribution with mean and std given by the actor. """ def __init__(self, action_dim, state_dim, hidden_dim): super(ActorCriticContinuousNew, self).__init__() self.fc_1 = nn.Linear(state_dim, hidden_dim) self.fc_2 = nn.Linear(hidden_dim, int(hidden_dim / 2)) self.critic_head = nn.Linear(int(hidden_dim / 2), 1) self.actor_head_mean = nn.Linear(int(hidden_dim / 2), action_dim) self.actor_head_sigma = nn.Linear(int(hidden_dim / 2), action_dim) def forward(self, input_0): primals_1 = self.fc_1.weight primals_2 = self.fc_1.bias primals_4 = self.fc_2.weight primals_5 = self.fc_2.bias primals_6 = self.critic_head.weight primals_7 = self.critic_head.bias primals_8 = self.actor_head_mean.weight primals_9 = self.actor_head_mean.bias primals_10 = self.actor_head_sigma.weight primals_11 = self.actor_head_sigma.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], output[1], output[2]
fschur/Advantage-Actor-Critic-for-OpenAi-gym
ActorCriticContinuous
false
6,705
[ "MIT" ]
1
c130038789425301684825e09e77f17e89d21859
https://github.com/fschur/Advantage-Actor-Critic-for-OpenAi-gym/tree/c130038789425301684825e09e77f17e89d21859
Policy
import torch import torch.nn.functional as F from torch import nn class Policy(nn.Module): def __init__(self): super(Policy, self).__init__() self.conv1 = nn.Conv2d(2, 4, kernel_size=6, stride=2, bias=False) self.conv2 = nn.Conv2d(4, 16, kernel_size=6, stride=4) self.size = 9 * 9 * 16 self.fc1 = nn.Linear(self.size, 256) self.fc2 = nn.Linear(256, 1) self.sig = nn.Sigmoid() def forward(self, x): x = F.relu(self.conv1(x)) x = F.relu(self.conv2(x)) x = x.view(-1, self.size) x = F.relu(self.fc1(x)) return self.sig(self.fc2(x)) def get_inputs(): return [torch.rand([4, 2, 81, 81])] 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 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_relu_0(in_out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 23104 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) tl.store(in_out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 5184 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex x1 = xindex // 81 % 16 x2 = xindex // 1296 x3 = xindex % 1296 tmp0 = tl.load(in_ptr0 + x4, 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 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(out_ptr0 + (x3 + 1312 * x2), tmp4, xmask) tl.store(out_ptr1 + (x3 + 1408 * x2), tmp6, xmask) @triton.jit def triton_poi_fused_relu_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 256 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_sigmoid_3(in_out_ptr0, in_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_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) = args args.clear() assert_size_stride(primals_1, (4, 2, 6, 6), (72, 36, 6, 1)) assert_size_stride(primals_2, (4, 2, 81, 81), (13122, 6561, 81, 1)) assert_size_stride(primals_3, (16, 4, 6, 6), (144, 36, 6, 1)) assert_size_stride(primals_4, (16,), (1,)) assert_size_stride(primals_5, (256, 1296), (1296, 1)) assert_size_stride(primals_6, (256,), (1,)) assert_size_stride(primals_7, (1, 256), (256, 1)) assert_size_stride(primals_8, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_2, primals_1, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 38, 38), (5776, 1444, 38, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_relu_0[grid(23104)](buf1, 23104, XBLOCK=128, num_warps=4, num_stages=1) buf2 = extern_kernels.convolution(buf1, primals_3, stride=(4, 4), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 16, 9, 9), (1296, 81, 9, 1)) buf3 = empty_strided_cuda((4, 16, 9, 9), (1312, 81, 9, 1), torch. float32) buf8 = empty_strided_cuda((4, 16, 9, 9), (1408, 81, 9, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_1[grid(5184)](buf2 , primals_4, buf3, buf8, 5184, XBLOCK=256, num_warps=4, num_stages=1) del buf2 del primals_4 buf4 = empty_strided_cuda((4, 256), (256, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf3, (4, 1296), (1312, 1), 0), reinterpret_tensor(primals_5, (1296, 256), (1, 1296), 0), out=buf4) buf5 = buf4 del buf4 triton_poi_fused_relu_2[grid(1024)](buf5, primals_6, 1024, XBLOCK= 256, num_warps=4, num_stages=1) del primals_6 buf6 = empty_strided_cuda((4, 1), (1, 1), torch.float32) extern_kernels.mm(buf5, reinterpret_tensor(primals_7, (256, 1), (1, 256), 0), out=buf6) buf7 = buf6 del buf6 triton_poi_fused_sigmoid_3[grid(4)](buf7, primals_8, 4, XBLOCK=4, num_warps=1, num_stages=1) del primals_8 return buf7, primals_1, primals_2, primals_3, buf1, reinterpret_tensor(buf3 , (4, 1296), (1312, 1), 0), buf5, buf7, primals_7, primals_5, buf8 class PolicyNew(nn.Module): def __init__(self): super(PolicyNew, self).__init__() self.conv1 = nn.Conv2d(2, 4, kernel_size=6, stride=2, bias=False) self.conv2 = nn.Conv2d(4, 16, kernel_size=6, stride=4) self.size = 9 * 9 * 16 self.fc1 = nn.Linear(self.size, 256) self.fc2 = nn.Linear(256, 1) self.sig = nn.Sigmoid() def forward(self, input_0): primals_1 = self.conv1.weight primals_3 = self.conv2.weight primals_4 = self.conv2.bias primals_5 = self.fc1.weight primals_6 = self.fc1.bias primals_7 = self.fc2.weight primals_8 = self.fc2.bias primals_2 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8]) return output[0]
francescotorregrossa/deep-reinforcement-learning-nanodegree
Policy
false
6,706
[ "MIT" ]
1
396648570aa53c9e727a8de69175e4a139d4ded5
https://github.com/francescotorregrossa/deep-reinforcement-learning-nanodegree/tree/396648570aa53c9e727a8de69175e4a139d4ded5
EqualConv2d
import torch from torch import nn from math import sqrt def equal_lr(module, name='weight'): EqualLR.apply(module, name) return module class EqualLR: def __init__(self, name): self.name = name def compute_weight(self, module): weight = getattr(module, self.name + '_orig') fan_in = weight.data.size(1) * weight.data[0][0].numel() return weight * sqrt(2 / fan_in) @staticmethod def apply(module, name): fn = EqualLR(name) weight = getattr(module, name) del module._parameters[name] module.register_parameter(name + '_orig', nn.Parameter(weight.data)) module.register_forward_pre_hook(fn) return fn def __call__(self, module, input): weight = self.compute_weight(module) setattr(module, self.name, weight) class EqualConv2d(nn.Module): def __init__(self, *args, **kwargs): super().__init__() conv = nn.Conv2d(*args, **kwargs) conv.weight.data.normal_() conv.bias.data.zero_() self.conv = equal_lr(conv) def forward(self, input): return self.conv(input) 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 import nn from math import sqrt 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, 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.1767766952966369 tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_poi_fused_convolution_1(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,), (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((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_0[grid(256)](primals_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 buf1 = extern_kernels.convolution(primals_3, buf0, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 1, 1), (4, 1, 1, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_1[grid(16)](buf2, primals_2, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_2 return buf2, buf0, primals_3, buf0 def equal_lr(module, name='weight'): EqualLR.apply(module, name) return module class EqualLR: def __init__(self, name): self.name = name def compute_weight(self, module): weight = getattr(module, self.name + '_orig') fan_in = weight.data.size(1) * weight.data[0][0].numel() return weight * sqrt(2 / fan_in) @staticmethod def apply(module, name): fn = EqualLR(name) weight = getattr(module, name) del module._parameters[name] module.register_parameter(name + '_orig', nn.Parameter(weight.data)) module.register_forward_pre_hook(fn) return fn def __call__(self, module, input): weight = self.compute_weight(module) setattr(module, self.name, weight) class EqualConv2dNew(nn.Module): def __init__(self, *args, **kwargs): super().__init__() conv = nn.Conv2d(*args, **kwargs) conv.weight.data.normal_() conv.bias.data.zero_() self.conv = equal_lr(conv) def forward(self, input_0): primals_2 = self.conv.bias primals_1 = self.conv.weight_orig primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
g33sean/RTIL
EqualConv2d
false
6,707
[ "BSD-2-Clause", "MIT" ]
1
5325f6d5e3ddf7579b6bd8199898e00eff3da631
https://github.com/g33sean/RTIL/tree/5325f6d5e3ddf7579b6bd8199898e00eff3da631
Feedforward
import torch class Feedforward(torch.nn.Module): def __init__(self, input_size, output_size, hidden_size): super(Feedforward, self).__init__() self.input_size = input_size self.output_size = output_size self.hidden_size = hidden_size self.fc1 = torch.nn.Linear(self.input_size, self.hidden_size) self.relu = torch.nn.ReLU() self.fc2 = torch.nn.Linear(self.hidden_size, self.hidden_size) self.fc3 = torch.nn.Linear(self.hidden_size, self.output_size) self.sigmoid = torch.nn.Sigmoid() def forward(self, x): y = self.fc1(x) y = self.sigmoid(y) y = self.fc2(y) y = self.sigmoid(y) y = self.fc3(y) return y def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'output_size': 4, '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 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_sigmoid_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 = 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, (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,)) 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((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 get_raw_stream(0) triton_poi_fused_sigmoid_0[grid(256)](buf1, primals_2, 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 triton_poi_fused_sigmoid_0[grid(256)](buf3, primals_5, 256, XBLOCK= 128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf4) del primals_7 return reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf1, buf3, primals_6, primals_4 class FeedforwardNew(torch.nn.Module): def __init__(self, input_size, output_size, hidden_size): super(FeedforwardNew, self).__init__() self.input_size = input_size self.output_size = output_size self.hidden_size = hidden_size self.fc1 = torch.nn.Linear(self.input_size, self.hidden_size) self.relu = torch.nn.ReLU() self.fc2 = torch.nn.Linear(self.hidden_size, self.hidden_size) self.fc3 = torch.nn.Linear(self.hidden_size, self.output_size) self.sigmoid = torch.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]
fywu85/eecs206a_project
Feedforward
false
6,708
[ "MIT" ]
1
73ea518779da4d187df8bbe4cbe46bca6d1a0714
https://github.com/fywu85/eecs206a_project/tree/73ea518779da4d187df8bbe4cbe46bca6d1a0714
ActorCriticDiscrete
import torch from torch import nn import torch.nn.functional as F class ActorCriticDiscrete(nn.Module): """ Actor-Critic for discrete action spaces. The network returns a state_value (critic)and action probabilities (actor). """ def __init__(self, action_dim, state_dim, hidden_dim): super(ActorCriticDiscrete, self).__init__() self.fc_1 = nn.Linear(state_dim, hidden_dim) self.fc_2 = nn.Linear(hidden_dim, int(hidden_dim / 2)) self.critic_head = nn.Linear(int(hidden_dim / 2), 1) self.actor_head = nn.Linear(int(hidden_dim / 2), action_dim) def forward(self, inp): x = F.leaky_relu(self.fc_1(inp)) x = F.leaky_relu(self.fc_2(x)) state_value = self.critic_head(x) action_prob = F.softmax(self.actor_head(x), dim=-1) return action_prob, state_value def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'action_dim': 4, 'state_dim': 4, 'hidden_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 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_leaky_relu_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 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 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr1 + x2, tmp7, xmask) @triton.jit def triton_poi_fused_leaky_relu_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, 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 % 2 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr1 + x2, tmp7, 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 = 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, primals_7, primals_8, primals_9) = 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, (2, 4), (4, 1)) assert_size_stride(primals_5, (2,), (1,)) assert_size_stride(primals_6, (1, 2), (2, 1)) assert_size_stride(primals_7, (1,), (1,)) assert_size_stride(primals_8, (4, 2), (2, 1)) assert_size_stride(primals_9, (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 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_leaky_relu_0[grid(256)](buf0, primals_2, buf1, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf3 = empty_strided_cuda((64, 2), (2, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf2, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 2), (1, 4), 0), out=buf3) buf4 = empty_strided_cuda((4, 4, 4, 2), (32, 8, 2, 1), torch.bool) buf5 = empty_strided_cuda((4, 4, 4, 2), (32, 8, 2, 1), torch.float32) triton_poi_fused_leaky_relu_1[grid(128)](buf3, primals_5, buf4, buf5, 128, XBLOCK=128, num_warps=4, num_stages=1) del buf3 del primals_5 buf7 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf5, (64, 2), ( 2, 1), 0), reinterpret_tensor(primals_6, (2, 1), (1, 2), 0), alpha=1, beta=1, out=buf7) del primals_7 buf8 = buf0 del buf0 extern_kernels.addmm(primals_9, reinterpret_tensor(buf5, (64, 2), ( 2, 1), 0), reinterpret_tensor(primals_8, (2, 4), (1, 2), 0), alpha=1, beta=1, out=buf8) del primals_9 buf9 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__softmax_2[grid(256)](buf8, buf9, 256, XBLOCK=128, num_warps=4, num_stages=1) buf10 = reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf8 triton_poi_fused__softmax_3[grid(256)](buf9, buf10, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf9 return buf10, reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf1, reinterpret_tensor(buf2, (64, 4), (4, 1), 0 ), buf4, reinterpret_tensor(buf5, (64, 2), (2, 1), 0 ), buf10, primals_8, primals_6, primals_4 class ActorCriticDiscreteNew(nn.Module): """ Actor-Critic for discrete action spaces. The network returns a state_value (critic)and action probabilities (actor). """ def __init__(self, action_dim, state_dim, hidden_dim): super(ActorCriticDiscreteNew, self).__init__() self.fc_1 = nn.Linear(state_dim, hidden_dim) self.fc_2 = nn.Linear(hidden_dim, int(hidden_dim / 2)) self.critic_head = nn.Linear(int(hidden_dim / 2), 1) self.actor_head = nn.Linear(int(hidden_dim / 2), action_dim) def forward(self, input_0): primals_1 = self.fc_1.weight primals_2 = self.fc_1.bias primals_4 = self.fc_2.weight primals_5 = self.fc_2.bias primals_6 = self.critic_head.weight primals_7 = self.critic_head.bias primals_8 = self.actor_head.weight primals_9 = self.actor_head.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], output[1]
fschur/Advantage-Actor-Critic-for-OpenAi-gym
ActorCriticDiscrete
false
6,709
[ "MIT" ]
1
c130038789425301684825e09e77f17e89d21859
https://github.com/fschur/Advantage-Actor-Critic-for-OpenAi-gym/tree/c130038789425301684825e09e77f17e89d21859
EqualLinear
import torch from torch import nn from math import sqrt def equal_lr(module, name='weight'): EqualLR.apply(module, name) return module class EqualLR: def __init__(self, name): self.name = name def compute_weight(self, module): weight = getattr(module, self.name + '_orig') fan_in = weight.data.size(1) * weight.data[0][0].numel() return weight * sqrt(2 / fan_in) @staticmethod def apply(module, name): fn = EqualLR(name) weight = getattr(module, name) del module._parameters[name] module.register_parameter(name + '_orig', nn.Parameter(weight.data)) module.register_forward_pre_hook(fn) return fn def __call__(self, module, input): weight = self.compute_weight(module) setattr(module, self.name, weight) class EqualLinear(nn.Module): def __init__(self, in_dim, out_dim): super().__init__() linear = nn.Linear(in_dim, out_dim) linear.weight.data.normal_() linear.bias.data.zero_() self.linear = equal_lr(linear) def forward(self, input): return self.linear(input) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_dim': 4, 'out_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 import nn from math import sqrt 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, 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 = 0.7071067811865476 tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x0, tmp2, 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((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_0[grid(16)](primals_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_1 buf1 = 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(buf0, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf1) del primals_2 return reinterpret_tensor(buf1, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), buf0, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0) def equal_lr(module, name='weight'): EqualLR.apply(module, name) return module class EqualLR: def __init__(self, name): self.name = name def compute_weight(self, module): weight = getattr(module, self.name + '_orig') fan_in = weight.data.size(1) * weight.data[0][0].numel() return weight * sqrt(2 / fan_in) @staticmethod def apply(module, name): fn = EqualLR(name) weight = getattr(module, name) del module._parameters[name] module.register_parameter(name + '_orig', nn.Parameter(weight.data)) module.register_forward_pre_hook(fn) return fn def __call__(self, module, input): weight = self.compute_weight(module) setattr(module, self.name, weight) class EqualLinearNew(nn.Module): def __init__(self, in_dim, out_dim): super().__init__() linear = nn.Linear(in_dim, out_dim) linear.weight.data.normal_() linear.bias.data.zero_() self.linear = equal_lr(linear) def forward(self, input_0): primals_2 = self.linear.bias primals_1 = self.linear.weight_orig primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
g33sean/RTIL
EqualLinear
false
6,710
[ "BSD-2-Clause", "MIT" ]
1
5325f6d5e3ddf7579b6bd8199898e00eff3da631
https://github.com/g33sean/RTIL/tree/5325f6d5e3ddf7579b6bd8199898e00eff3da631
UNet
import torch from torch.functional import F import torch.nn as nn import torch.nn.functional as F class down(nn.Module): def __init__(self, inChannels, outChannels, filterSize): """ Parameters ---------- inChannels : int number of input channels for the first convolutional layer. outChannels : int number of output channels for the first convolutional layer. This is also used as input and output channels for the second convolutional layer. filterSize : int filter size for the convolution filter. input N would create a N x N filter. """ super(down, self).__init__() self.conv1 = nn.Conv2d(inChannels, outChannels, filterSize, stride= 1, padding=int((filterSize - 1) / 2)) self.conv2 = nn.Conv2d(outChannels, outChannels, filterSize, stride =1, padding=int((filterSize - 1) / 2)) def forward(self, x): """ Forward. """ x = F.avg_pool2d(x, 2) x = F.leaky_relu(self.conv1(x), negative_slope=0.1) x = F.leaky_relu(self.conv2(x), negative_slope=0.1) return x class up(nn.Module): def __init__(self, inChannels, outChannels): super(up, self).__init__() self.conv1 = nn.Conv2d(inChannels, outChannels, 3, stride=1, padding=1) self.conv2 = nn.Conv2d(2 * outChannels, outChannels, 3, stride=1, padding=1) def forward(self, x, skpCn): """ Forward . """ x = F.interpolate(x, scale_factor=2, mode='bilinear') x = F.leaky_relu(self.conv1(x), negative_slope=0.1) x = F.leaky_relu(self.conv2(torch.cat((x, skpCn), 1)), negative_slope=0.1) return x class UNet(nn.Module): def __init__(self, inChannels, outChannels): super(UNet, self).__init__() self.conv1 = nn.Conv2d(inChannels, 32, 7, stride=1, padding=3) self.conv2 = nn.Conv2d(32, 32, 7, stride=1, padding=3) self.down1 = down(32, 64, 5) self.down2 = down(64, 128, 3) self.down3 = down(128, 256, 3) self.down4 = down(256, 512, 3) self.down5 = down(512, 512, 3) self.up1 = up(512, 512) self.up2 = up(512, 256) self.up3 = up(256, 128) self.up4 = up(128, 64) self.up5 = up(64, 32) self.conv3 = nn.Conv2d(32, outChannels, 3, stride=1, padding=1) def forward(self, x): """ Forward.""" x = F.leaky_relu(self.conv1(x), negative_slope=0.1) s1 = F.leaky_relu(self.conv2(x), negative_slope=0.1) s2 = self.down1(s1) s3 = self.down2(s2) s4 = self.down3(s3) s5 = self.down4(s4) x = self.down5(s5) x = self.up1(x, s5) x = self.up2(x, s4) x = self.up3(x, s3) x = self.up4(x, s2) x = self.up5(x, s1) x = F.leaky_relu(self.conv3(x), negative_slope=0.1) return x def get_inputs(): return [torch.rand([4, 4, 64, 64])] def get_init_inputs(): return [[], {'inChannels': 4, 'outChannels': 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.functional import F 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 reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_leaky_relu_0(in_ptr0, in_ptr1, 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) x3 = xindex x1 = xindex // 4096 % 32 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.1 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x3, tmp4, None) tl.store(out_ptr1 + x3, tmp7, None) @triton.jit def triton_poi_fused_avg_pool2d_1(in_ptr0, 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 % 32 x1 = xindex // 32 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 128 * x1), None, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 128 * x1), None, eviction_policy ='evict_last') tmp3 = tl.load(in_ptr0 + (64 + 2 * x0 + 128 * x1), None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (65 + 2 * x0 + 128 * x1), None, eviction_policy='evict_last') tmp2 = tmp1 + tmp0 tmp4 = tmp3 + tmp2 tmp6 = tmp5 + tmp4 tmp7 = 0.25 tmp8 = tmp6 * tmp7 tl.store(out_ptr0 + x2, tmp8, None) @triton.jit def triton_poi_fused_convolution_leaky_relu_2(in_ptr0, in_ptr1, 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) x3 = xindex x1 = xindex // 1024 % 64 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.1 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x3, tmp4, None) tl.store(out_ptr1 + x3, tmp7, None) @triton.jit def triton_poi_fused_avg_pool2d_3(in_ptr0, 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 % 16 x1 = xindex // 16 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 64 * x1), None, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 64 * x1), None, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (32 + 2 * x0 + 64 * x1), None, eviction_policy ='evict_last') tmp5 = tl.load(in_ptr0 + (33 + 2 * x0 + 64 * x1), None, eviction_policy ='evict_last') tmp2 = tmp1 + tmp0 tmp4 = tmp3 + tmp2 tmp6 = tmp5 + tmp4 tmp7 = 0.25 tmp8 = tmp6 * tmp7 tl.store(out_ptr0 + x2, tmp8, None) @triton.jit def triton_poi_fused_convolution_leaky_relu_4(in_ptr0, in_ptr1, 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) x3 = xindex x1 = xindex // 256 % 128 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.1 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x3, tmp4, None) tl.store(out_ptr1 + x3, tmp7, None) @triton.jit def triton_poi_fused_avg_pool2d_5(in_ptr0, 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 % 8 x1 = xindex // 8 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 32 * x1), None, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 32 * x1), None, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (16 + 2 * x0 + 32 * x1), None, eviction_policy ='evict_last') tmp5 = tl.load(in_ptr0 + (17 + 2 * x0 + 32 * x1), None, eviction_policy ='evict_last') tmp2 = tmp1 + tmp0 tmp4 = tmp3 + tmp2 tmp6 = tmp5 + tmp4 tmp7 = 0.25 tmp8 = tmp6 * tmp7 tl.store(out_ptr0 + x2, tmp8, None) @triton.jit def triton_poi_fused_convolution_leaky_relu_6(in_ptr0, in_ptr1, 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) x3 = xindex x1 = xindex // 64 % 256 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.1 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x3, tmp4, None) tl.store(out_ptr1 + x3, tmp7, None) @triton.jit def triton_poi_fused_avg_pool2d_7(in_ptr0, 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 % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 16 * x1), None, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 16 * x1), None, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (8 + 2 * x0 + 16 * x1), None, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (9 + 2 * x0 + 16 * x1), None, eviction_policy= 'evict_last') tmp2 = tmp1 + tmp0 tmp4 = tmp3 + tmp2 tmp6 = tmp5 + tmp4 tmp7 = 0.25 tmp8 = tmp6 * tmp7 tl.store(out_ptr0 + x2, tmp8, None) @triton.jit def triton_poi_fused_convolution_leaky_relu_8(in_ptr0, in_ptr1, 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) x3 = xindex x1 = xindex // 16 % 512 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.1 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x3, tmp4, None) tl.store(out_ptr1 + x3, tmp7, None) @triton.jit def triton_poi_fused_avg_pool2d_9(in_ptr0, 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 % 2 x1 = xindex // 2 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 8 * x1), None, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 8 * x1), None, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (4 + 2 * x0 + 8 * x1), None, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (5 + 2 * x0 + 8 * x1), None, eviction_policy= 'evict_last') tmp2 = tmp1 + tmp0 tmp4 = tmp3 + tmp2 tmp6 = tmp5 + tmp4 tmp7 = 0.25 tmp8 = tmp6 * tmp7 tl.store(out_ptr0 + x2, tmp8, None) @triton.jit def triton_poi_fused_convolution_leaky_relu_10(in_ptr0, in_ptr1, 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) x3 = xindex x1 = xindex // 4 % 512 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.1 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x3, tmp4, None) tl.store(out_ptr1 + x3, tmp7, None) @triton.jit def triton_poi_fused_convolution_leaky_relu_11(in_ptr0, in_ptr1, 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 x1 = xindex // 4 % 512 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tl.store(out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused__to_copy_12(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 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 + tmp2 tmp4 = tmp3 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tl.store(out_ptr0 + x0, tmp8, xmask) @triton.jit def triton_poi_fused_add_clamp_13(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 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 + tmp2 tmp4 = tmp3 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tmp9 = tl.full([1], 1, tl.int64) tmp10 = tmp8 + tmp9 tmp11 = triton_helpers.minimum(tmp10, tmp9) tl.store(out_ptr0 + x0, tmp11, xmask) @triton.jit def triton_poi_fused__to_copy_add_arange_clamp_mul_sub_14(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 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 + tmp2 tmp4 = tmp3 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tmp9 = tmp8.to(tl.float32) tmp10 = tmp7 - tmp9 tmp11 = triton_helpers.maximum(tmp10, tmp6) tmp12 = 1.0 tmp13 = triton_helpers.minimum(tmp11, tmp12) tl.store(out_ptr0 + x0, tmp13, xmask) @triton.jit def triton_poi_fused__unsafe_index_add_convolution_leaky_relu_mul_sub_15( 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): 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 x6 = xindex // 16 x2 = xindex // 16 % 512 x4 = xindex tmp0 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + x0, None, eviction_policy='evict_last') tmp11 = tl.load(in_ptr4 + x2, None, eviction_policy='evict_last') tmp16 = tl.load(in_ptr5 + x1, None, eviction_policy='evict_last') tmp25 = tl.load(in_ptr6 + x0, None, eviction_policy='evict_last') tmp35 = tl.load(in_ptr7 + x0, None, eviction_policy='evict_last') tmp47 = tl.load(in_ptr8 + x1, None, eviction_policy='evict_last') tmp1 = tl.full([XBLOCK], 2, 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_ptr2 + (tmp8 + 2 * tmp4 + 4 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp10 = tl.load(in_ptr3 + (tmp8 + 2 * tmp4 + 4 * x6), None, eviction_policy='evict_last') tmp12 = tmp10 + tmp11 tmp13 = 0.1 tmp14 = tmp12 * tmp13 tmp15 = tl.where(tmp9, tmp12, tmp14) tmp17 = tmp16 + tmp1 tmp18 = tmp16 < 0 tmp19 = tl.where(tmp18, tmp17, tmp16) tmp20 = tl.load(in_ptr2 + (tmp8 + 2 * tmp19 + 4 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp21 = tl.load(in_ptr3 + (tmp8 + 2 * tmp19 + 4 * x6), None, eviction_policy='evict_last') tmp22 = tmp21 + tmp11 tmp23 = tmp22 * tmp13 tmp24 = tl.where(tmp20, tmp22, tmp23) tmp26 = tmp25 + tmp1 tmp27 = tmp25 < 0 tmp28 = tl.where(tmp27, tmp26, tmp25) tmp29 = tl.load(in_ptr2 + (tmp28 + 2 * tmp19 + 4 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp30 = tl.load(in_ptr3 + (tmp28 + 2 * tmp19 + 4 * x6), None, eviction_policy='evict_last') tmp31 = tmp30 + tmp11 tmp32 = tmp31 * tmp13 tmp33 = tl.where(tmp29, tmp31, tmp32) tmp34 = tmp33 - tmp24 tmp36 = tmp34 * tmp35 tmp37 = tmp24 + tmp36 tmp38 = tl.load(in_ptr2 + (tmp28 + 2 * tmp4 + 4 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp39 = tl.load(in_ptr3 + (tmp28 + 2 * tmp4 + 4 * x6), None, eviction_policy='evict_last') tmp40 = tmp39 + tmp11 tmp41 = tmp40 * tmp13 tmp42 = tl.where(tmp38, tmp40, tmp41) tmp43 = tmp42 - tmp15 tmp44 = tmp43 * tmp35 tmp45 = tmp15 + tmp44 tmp46 = tmp45 - tmp37 tmp48 = tmp46 * tmp47 tmp49 = tmp37 + tmp48 tl.store(in_out_ptr1 + x4, tmp49, None) @triton.jit def triton_poi_fused_convolution_leaky_relu_16(in_ptr0, in_ptr1, 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 x1 = xindex // 16 % 512 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tl.store(out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_cat_17(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 // 16 % 1024 x0 = xindex % 16 x2 = xindex // 16384 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 512, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 16 * x1 + 8192 * x2), tmp4, other=0.0).to(tl .int1) tmp6 = tl.load(in_ptr1 + (x0 + 16 * x1 + 8192 * x2), tmp4, other=0.0) tmp7 = tl.load(in_ptr2 + x1, tmp4, eviction_policy='evict_last', other=0.0) tmp8 = tmp6 + tmp7 tmp9 = 0.1 tmp10 = tmp8 * tmp9 tmp11 = tl.where(tmp5, tmp8, tmp10) tmp12 = tl.full(tmp11.shape, 0.0, tmp11.dtype) tmp13 = tl.where(tmp4, tmp11, tmp12) tmp14 = tmp0 >= tmp3 tl.full([1], 1024, tl.int64) tmp17 = tl.load(in_ptr3 + (x0 + 16 * (-512 + x1) + 8192 * x2), tmp14, other=0.0) tmp18 = tl.where(tmp4, tmp13, tmp17) tl.store(out_ptr0 + x3, tmp18, None) @triton.jit def triton_poi_fused__to_copy_18(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 8 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 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tl.store(out_ptr0 + x0, tmp8, xmask) @triton.jit def triton_poi_fused_add_clamp_19(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 8 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 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tmp9 = tl.full([1], 1, tl.int64) tmp10 = tmp8 + tmp9 tmp11 = tl.full([1], 3, tl.int64) tmp12 = triton_helpers.minimum(tmp10, tmp11) tl.store(out_ptr0 + x0, tmp12, xmask) @triton.jit def triton_poi_fused__to_copy_add_arange_clamp_mul_sub_20(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 8 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 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tmp9 = tmp8.to(tl.float32) tmp10 = tmp7 - tmp9 tmp11 = triton_helpers.maximum(tmp10, tmp6) tmp12 = 1.0 tmp13 = triton_helpers.minimum(tmp11, tmp12) tl.store(out_ptr0 + x0, tmp13, xmask) @triton.jit def triton_poi_fused__unsafe_index_add_convolution_leaky_relu_mul_sub_21( 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): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 8 % 8 x0 = xindex % 8 x6 = xindex // 64 x2 = xindex // 64 % 512 x4 = xindex tmp0 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + x0, None, eviction_policy='evict_last') tmp11 = tl.load(in_ptr4 + x2, None, eviction_policy='evict_last') tmp16 = tl.load(in_ptr5 + x1, None, eviction_policy='evict_last') tmp25 = tl.load(in_ptr6 + x0, None, eviction_policy='evict_last') tmp35 = tl.load(in_ptr7 + x0, None, eviction_policy='evict_last') tmp47 = tl.load(in_ptr8 + x1, None, eviction_policy='evict_last') tmp1 = tl.full([XBLOCK], 4, 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_ptr2 + (tmp8 + 4 * tmp4 + 16 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp10 = tl.load(in_ptr3 + (tmp8 + 4 * tmp4 + 16 * x6), None, eviction_policy='evict_last') tmp12 = tmp10 + tmp11 tmp13 = 0.1 tmp14 = tmp12 * tmp13 tmp15 = tl.where(tmp9, tmp12, tmp14) tmp17 = tmp16 + tmp1 tmp18 = tmp16 < 0 tmp19 = tl.where(tmp18, tmp17, tmp16) tmp20 = tl.load(in_ptr2 + (tmp8 + 4 * tmp19 + 16 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp21 = tl.load(in_ptr3 + (tmp8 + 4 * tmp19 + 16 * x6), None, eviction_policy='evict_last') tmp22 = tmp21 + tmp11 tmp23 = tmp22 * tmp13 tmp24 = tl.where(tmp20, tmp22, tmp23) tmp26 = tmp25 + tmp1 tmp27 = tmp25 < 0 tmp28 = tl.where(tmp27, tmp26, tmp25) tmp29 = tl.load(in_ptr2 + (tmp28 + 4 * tmp19 + 16 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp30 = tl.load(in_ptr3 + (tmp28 + 4 * tmp19 + 16 * x6), None, eviction_policy='evict_last') tmp31 = tmp30 + tmp11 tmp32 = tmp31 * tmp13 tmp33 = tl.where(tmp29, tmp31, tmp32) tmp34 = tmp33 - tmp24 tmp36 = tmp34 * tmp35 tmp37 = tmp24 + tmp36 tmp38 = tl.load(in_ptr2 + (tmp28 + 4 * tmp4 + 16 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp39 = tl.load(in_ptr3 + (tmp28 + 4 * tmp4 + 16 * x6), None, eviction_policy='evict_last') tmp40 = tmp39 + tmp11 tmp41 = tmp40 * tmp13 tmp42 = tl.where(tmp38, tmp40, tmp41) tmp43 = tmp42 - tmp15 tmp44 = tmp43 * tmp35 tmp45 = tmp15 + tmp44 tmp46 = tmp45 - tmp37 tmp48 = tmp46 * tmp47 tmp49 = tmp37 + tmp48 tl.store(in_out_ptr1 + x4, tmp49, None) @triton.jit def triton_poi_fused_convolution_leaky_relu_22(in_ptr0, in_ptr1, 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 x1 = xindex // 64 % 256 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tl.store(out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_cat_23(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 // 64 % 512 x0 = xindex % 64 x2 = xindex // 32768 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 256, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 64 * x1 + 16384 * x2), tmp4, other=0.0).to( tl.int1) tmp6 = tl.load(in_ptr1 + (x0 + 64 * x1 + 16384 * x2), tmp4, other=0.0) tmp7 = tl.load(in_ptr2 + x1, tmp4, eviction_policy='evict_last', other=0.0) tmp8 = tmp6 + tmp7 tmp9 = 0.1 tmp10 = tmp8 * tmp9 tmp11 = tl.where(tmp5, tmp8, tmp10) tmp12 = tl.full(tmp11.shape, 0.0, tmp11.dtype) tmp13 = tl.where(tmp4, tmp11, tmp12) tmp14 = tmp0 >= tmp3 tl.full([1], 512, tl.int64) tmp17 = tl.load(in_ptr3 + (x0 + 64 * (-256 + x1) + 16384 * x2), tmp14, other=0.0) tmp18 = tl.where(tmp4, tmp13, tmp17) tl.store(out_ptr0 + x3, tmp18, None) @triton.jit def triton_poi_fused__to_copy_24(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 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 + tmp2 tmp4 = tmp3 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tl.store(out_ptr0 + x0, tmp8, xmask) @triton.jit def triton_poi_fused_add_clamp_25(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 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 + tmp2 tmp4 = tmp3 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tmp9 = tl.full([1], 1, tl.int64) tmp10 = tmp8 + tmp9 tmp11 = tl.full([1], 7, tl.int64) tmp12 = triton_helpers.minimum(tmp10, tmp11) tl.store(out_ptr0 + x0, tmp12, xmask) @triton.jit def triton_poi_fused__to_copy_add_arange_clamp_mul_sub_26(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 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 + tmp2 tmp4 = tmp3 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tmp9 = tmp8.to(tl.float32) tmp10 = tmp7 - tmp9 tmp11 = triton_helpers.maximum(tmp10, tmp6) tmp12 = 1.0 tmp13 = triton_helpers.minimum(tmp11, tmp12) tl.store(out_ptr0 + x0, tmp13, xmask) @triton.jit def triton_poi_fused__unsafe_index_add_convolution_leaky_relu_mul_sub_27( 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): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 16 % 16 x0 = xindex % 16 x6 = xindex // 256 x2 = xindex // 256 % 256 x4 = xindex tmp0 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + x0, None, eviction_policy='evict_last') tmp11 = tl.load(in_ptr4 + x2, None, eviction_policy='evict_last') tmp16 = tl.load(in_ptr5 + x1, None, eviction_policy='evict_last') tmp25 = tl.load(in_ptr6 + x0, None, eviction_policy='evict_last') tmp35 = tl.load(in_ptr7 + x0, None, eviction_policy='evict_last') tmp47 = tl.load(in_ptr8 + x1, None, eviction_policy='evict_last') tmp1 = tl.full([XBLOCK], 8, 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_ptr2 + (tmp8 + 8 * tmp4 + 64 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp10 = tl.load(in_ptr3 + (tmp8 + 8 * tmp4 + 64 * x6), None, eviction_policy='evict_last') tmp12 = tmp10 + tmp11 tmp13 = 0.1 tmp14 = tmp12 * tmp13 tmp15 = tl.where(tmp9, tmp12, tmp14) tmp17 = tmp16 + tmp1 tmp18 = tmp16 < 0 tmp19 = tl.where(tmp18, tmp17, tmp16) tmp20 = tl.load(in_ptr2 + (tmp8 + 8 * tmp19 + 64 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp21 = tl.load(in_ptr3 + (tmp8 + 8 * tmp19 + 64 * x6), None, eviction_policy='evict_last') tmp22 = tmp21 + tmp11 tmp23 = tmp22 * tmp13 tmp24 = tl.where(tmp20, tmp22, tmp23) tmp26 = tmp25 + tmp1 tmp27 = tmp25 < 0 tmp28 = tl.where(tmp27, tmp26, tmp25) tmp29 = tl.load(in_ptr2 + (tmp28 + 8 * tmp19 + 64 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp30 = tl.load(in_ptr3 + (tmp28 + 8 * tmp19 + 64 * x6), None, eviction_policy='evict_last') tmp31 = tmp30 + tmp11 tmp32 = tmp31 * tmp13 tmp33 = tl.where(tmp29, tmp31, tmp32) tmp34 = tmp33 - tmp24 tmp36 = tmp34 * tmp35 tmp37 = tmp24 + tmp36 tmp38 = tl.load(in_ptr2 + (tmp28 + 8 * tmp4 + 64 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp39 = tl.load(in_ptr3 + (tmp28 + 8 * tmp4 + 64 * x6), None, eviction_policy='evict_last') tmp40 = tmp39 + tmp11 tmp41 = tmp40 * tmp13 tmp42 = tl.where(tmp38, tmp40, tmp41) tmp43 = tmp42 - tmp15 tmp44 = tmp43 * tmp35 tmp45 = tmp15 + tmp44 tmp46 = tmp45 - tmp37 tmp48 = tmp46 * tmp47 tmp49 = tmp37 + tmp48 tl.store(in_out_ptr1 + x4, tmp49, None) @triton.jit def triton_poi_fused_convolution_leaky_relu_28(in_ptr0, in_ptr1, 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 x1 = xindex // 256 % 128 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tl.store(out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_cat_29(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 // 256 % 256 x0 = xindex % 256 x2 = xindex // 65536 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 128, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 256 * x1 + 32768 * x2), tmp4, other=0.0).to( tl.int1) tmp6 = tl.load(in_ptr1 + (x0 + 256 * x1 + 32768 * x2), tmp4, other=0.0) tmp7 = tl.load(in_ptr2 + x1, tmp4, eviction_policy='evict_last', other=0.0) tmp8 = tmp6 + tmp7 tmp9 = 0.1 tmp10 = tmp8 * tmp9 tmp11 = tl.where(tmp5, tmp8, tmp10) tmp12 = tl.full(tmp11.shape, 0.0, tmp11.dtype) tmp13 = tl.where(tmp4, tmp11, tmp12) tmp14 = tmp0 >= tmp3 tl.full([1], 256, tl.int64) tmp17 = tl.load(in_ptr3 + (x0 + 256 * (-128 + x1) + 32768 * x2), tmp14, other=0.0) tmp18 = tl.where(tmp4, tmp13, tmp17) tl.store(out_ptr0 + x3, tmp18, None) @triton.jit def triton_poi_fused__to_copy_30(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 32 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 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tl.store(out_ptr0 + x0, tmp8, xmask) @triton.jit def triton_poi_fused_add_clamp_31(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 32 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 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tmp9 = tl.full([1], 1, tl.int64) tmp10 = tmp8 + tmp9 tmp11 = tl.full([1], 15, tl.int64) tmp12 = triton_helpers.minimum(tmp10, tmp11) tl.store(out_ptr0 + x0, tmp12, xmask) @triton.jit def triton_poi_fused__to_copy_add_arange_clamp_mul_sub_32(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 32 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 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tmp9 = tmp8.to(tl.float32) tmp10 = tmp7 - tmp9 tmp11 = triton_helpers.maximum(tmp10, tmp6) tmp12 = 1.0 tmp13 = triton_helpers.minimum(tmp11, tmp12) tl.store(out_ptr0 + x0, tmp13, xmask) @triton.jit def triton_poi_fused__unsafe_index_add_convolution_leaky_relu_mul_sub_33( 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): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 32 % 32 x0 = xindex % 32 x6 = xindex // 1024 x2 = xindex // 1024 % 128 x4 = xindex tmp0 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + x0, None, eviction_policy='evict_last') tmp11 = tl.load(in_ptr4 + x2, None, eviction_policy='evict_last') tmp16 = tl.load(in_ptr5 + x1, None, eviction_policy='evict_last') tmp25 = tl.load(in_ptr6 + x0, None, eviction_policy='evict_last') tmp35 = tl.load(in_ptr7 + x0, None, eviction_policy='evict_last') tmp47 = tl.load(in_ptr8 + x1, None, eviction_policy='evict_last') tmp1 = tl.full([XBLOCK], 16, 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_ptr2 + (tmp8 + 16 * tmp4 + 256 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp10 = tl.load(in_ptr3 + (tmp8 + 16 * tmp4 + 256 * x6), None, eviction_policy='evict_last') tmp12 = tmp10 + tmp11 tmp13 = 0.1 tmp14 = tmp12 * tmp13 tmp15 = tl.where(tmp9, tmp12, tmp14) tmp17 = tmp16 + tmp1 tmp18 = tmp16 < 0 tmp19 = tl.where(tmp18, tmp17, tmp16) tmp20 = tl.load(in_ptr2 + (tmp8 + 16 * tmp19 + 256 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp21 = tl.load(in_ptr3 + (tmp8 + 16 * tmp19 + 256 * x6), None, eviction_policy='evict_last') tmp22 = tmp21 + tmp11 tmp23 = tmp22 * tmp13 tmp24 = tl.where(tmp20, tmp22, tmp23) tmp26 = tmp25 + tmp1 tmp27 = tmp25 < 0 tmp28 = tl.where(tmp27, tmp26, tmp25) tmp29 = tl.load(in_ptr2 + (tmp28 + 16 * tmp19 + 256 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp30 = tl.load(in_ptr3 + (tmp28 + 16 * tmp19 + 256 * x6), None, eviction_policy='evict_last') tmp31 = tmp30 + tmp11 tmp32 = tmp31 * tmp13 tmp33 = tl.where(tmp29, tmp31, tmp32) tmp34 = tmp33 - tmp24 tmp36 = tmp34 * tmp35 tmp37 = tmp24 + tmp36 tmp38 = tl.load(in_ptr2 + (tmp28 + 16 * tmp4 + 256 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp39 = tl.load(in_ptr3 + (tmp28 + 16 * tmp4 + 256 * x6), None, eviction_policy='evict_last') tmp40 = tmp39 + tmp11 tmp41 = tmp40 * tmp13 tmp42 = tl.where(tmp38, tmp40, tmp41) tmp43 = tmp42 - tmp15 tmp44 = tmp43 * tmp35 tmp45 = tmp15 + tmp44 tmp46 = tmp45 - tmp37 tmp48 = tmp46 * tmp47 tmp49 = tmp37 + tmp48 tl.store(in_out_ptr1 + x4, tmp49, None) @triton.jit def triton_poi_fused_convolution_leaky_relu_34(in_ptr0, in_ptr1, 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 x1 = xindex // 1024 % 64 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tl.store(out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_cat_35(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 // 1024 % 128 x0 = xindex % 1024 x2 = xindex // 131072 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 64, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 1024 * x1 + 65536 * x2), tmp4, other=0.0 ).to(tl.int1) tmp6 = tl.load(in_ptr1 + (x0 + 1024 * x1 + 65536 * x2), tmp4, other=0.0) tmp7 = tl.load(in_ptr2 + x1, tmp4, eviction_policy='evict_last', other=0.0) tmp8 = tmp6 + tmp7 tmp9 = 0.1 tmp10 = tmp8 * tmp9 tmp11 = tl.where(tmp5, tmp8, tmp10) tmp12 = tl.full(tmp11.shape, 0.0, tmp11.dtype) tmp13 = tl.where(tmp4, tmp11, tmp12) tmp14 = tmp0 >= tmp3 tl.full([1], 128, tl.int64) tmp17 = tl.load(in_ptr3 + (x0 + 1024 * (-64 + x1) + 65536 * x2), tmp14, other=0.0) tmp18 = tl.where(tmp4, tmp13, tmp17) tl.store(out_ptr0 + x3, tmp18, None) @triton.jit def triton_poi_fused__to_copy_36(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 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tl.store(out_ptr0 + x0, tmp8, xmask) @triton.jit def triton_poi_fused_add_clamp_37(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 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tmp9 = tl.full([1], 1, tl.int64) tmp10 = tmp8 + tmp9 tmp11 = tl.full([1], 31, tl.int64) tmp12 = triton_helpers.minimum(tmp10, tmp11) tl.store(out_ptr0 + x0, tmp12, xmask) @triton.jit def triton_poi_fused__to_copy_add_arange_clamp_mul_sub_38(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 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tmp9 = tmp8.to(tl.float32) tmp10 = tmp7 - tmp9 tmp11 = triton_helpers.maximum(tmp10, tmp6) tmp12 = 1.0 tmp13 = triton_helpers.minimum(tmp11, tmp12) tl.store(out_ptr0 + x0, tmp13, xmask) @triton.jit def triton_poi_fused__unsafe_index_add_convolution_leaky_relu_mul_sub_39( 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): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 64 % 64 x0 = xindex % 64 x6 = xindex // 4096 x2 = xindex // 4096 % 64 x4 = xindex tmp0 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + x0, None, eviction_policy='evict_last') tmp11 = tl.load(in_ptr4 + x2, None, eviction_policy='evict_last') tmp16 = tl.load(in_ptr5 + x1, None, eviction_policy='evict_last') tmp25 = tl.load(in_ptr6 + x0, None, eviction_policy='evict_last') tmp35 = tl.load(in_ptr7 + x0, None, eviction_policy='evict_last') tmp47 = tl.load(in_ptr8 + x1, None, 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_ptr2 + (tmp8 + 32 * tmp4 + 1024 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp10 = tl.load(in_ptr3 + (tmp8 + 32 * tmp4 + 1024 * x6), None, eviction_policy='evict_last') tmp12 = tmp10 + tmp11 tmp13 = 0.1 tmp14 = tmp12 * tmp13 tmp15 = tl.where(tmp9, tmp12, tmp14) tmp17 = tmp16 + tmp1 tmp18 = tmp16 < 0 tmp19 = tl.where(tmp18, tmp17, tmp16) tmp20 = tl.load(in_ptr2 + (tmp8 + 32 * tmp19 + 1024 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp21 = tl.load(in_ptr3 + (tmp8 + 32 * tmp19 + 1024 * x6), None, eviction_policy='evict_last') tmp22 = tmp21 + tmp11 tmp23 = tmp22 * tmp13 tmp24 = tl.where(tmp20, tmp22, tmp23) tmp26 = tmp25 + tmp1 tmp27 = tmp25 < 0 tmp28 = tl.where(tmp27, tmp26, tmp25) tmp29 = tl.load(in_ptr2 + (tmp28 + 32 * tmp19 + 1024 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp30 = tl.load(in_ptr3 + (tmp28 + 32 * tmp19 + 1024 * x6), None, eviction_policy='evict_last') tmp31 = tmp30 + tmp11 tmp32 = tmp31 * tmp13 tmp33 = tl.where(tmp29, tmp31, tmp32) tmp34 = tmp33 - tmp24 tmp36 = tmp34 * tmp35 tmp37 = tmp24 + tmp36 tmp38 = tl.load(in_ptr2 + (tmp28 + 32 * tmp4 + 1024 * x6), None, eviction_policy='evict_last').to(tl.int1) tmp39 = tl.load(in_ptr3 + (tmp28 + 32 * tmp4 + 1024 * x6), None, eviction_policy='evict_last') tmp40 = tmp39 + tmp11 tmp41 = tmp40 * tmp13 tmp42 = tl.where(tmp38, tmp40, tmp41) tmp43 = tmp42 - tmp15 tmp44 = tmp43 * tmp35 tmp45 = tmp15 + tmp44 tmp46 = tmp45 - tmp37 tmp48 = tmp46 * tmp47 tmp49 = tmp37 + tmp48 tl.store(in_out_ptr1 + x4, tmp49, None) @triton.jit def triton_poi_fused_convolution_leaky_relu_40(in_ptr0, in_ptr1, 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 x1 = xindex // 4096 % 32 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tl.store(out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_cat_41(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 // 4096 % 64 x0 = xindex % 4096 x2 = xindex // 262144 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 32, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 4096 * x1 + 131072 * x2), tmp4, other=0.0 ).to(tl.int1) tmp6 = tl.load(in_ptr1 + (x0 + 4096 * x1 + 131072 * x2), tmp4, other=0.0) tmp7 = tl.load(in_ptr2 + x1, tmp4, eviction_policy='evict_last', other=0.0) tmp8 = tmp6 + tmp7 tmp9 = 0.1 tmp10 = tmp8 * tmp9 tmp11 = tl.where(tmp5, tmp8, tmp10) tmp12 = tl.full(tmp11.shape, 0.0, tmp11.dtype) tmp13 = tl.where(tmp4, tmp11, tmp12) tmp14 = tmp0 >= tmp3 tl.full([1], 64, tl.int64) tmp17 = tl.load(in_ptr3 + (x0 + 4096 * (-32 + x1) + 131072 * x2), tmp14, other=0.0) tmp18 = tl.where(tmp4, tmp13, tmp17) tl.store(out_ptr0 + x3, tmp18, None) @triton.jit def triton_poi_fused_convolution_leaky_relu_42(in_ptr0, in_ptr1, 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) x3 = xindex x1 = xindex // 4096 % 4 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.1 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x3, tmp4, None) tl.store(out_ptr1 + x3, tmp7, 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, primals_32, primals_33, primals_34, primals_35, primals_36, primals_37, primals_38, primals_39, primals_40, primals_41, primals_42, primals_43, primals_44, primals_45, primals_46, primals_47) = args args.clear() assert_size_stride(primals_1, (32, 4, 7, 7), (196, 49, 7, 1)) assert_size_stride(primals_2, (32,), (1,)) assert_size_stride(primals_3, (4, 4, 64, 64), (16384, 4096, 64, 1)) assert_size_stride(primals_4, (32, 32, 7, 7), (1568, 49, 7, 1)) assert_size_stride(primals_5, (32,), (1,)) assert_size_stride(primals_6, (64, 32, 5, 5), (800, 25, 5, 1)) assert_size_stride(primals_7, (64,), (1,)) assert_size_stride(primals_8, (64, 64, 5, 5), (1600, 25, 5, 1)) assert_size_stride(primals_9, (64,), (1,)) assert_size_stride(primals_10, (128, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_11, (128,), (1,)) assert_size_stride(primals_12, (128, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_13, (128,), (1,)) assert_size_stride(primals_14, (256, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_15, (256,), (1,)) assert_size_stride(primals_16, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_17, (256,), (1,)) assert_size_stride(primals_18, (512, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_19, (512,), (1,)) assert_size_stride(primals_20, (512, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_21, (512,), (1,)) assert_size_stride(primals_22, (512, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_23, (512,), (1,)) assert_size_stride(primals_24, (512, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_25, (512,), (1,)) assert_size_stride(primals_26, (512, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_27, (512,), (1,)) assert_size_stride(primals_28, (512, 1024, 3, 3), (9216, 9, 3, 1)) assert_size_stride(primals_29, (512,), (1,)) assert_size_stride(primals_30, (256, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_31, (256,), (1,)) assert_size_stride(primals_32, (256, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_33, (256,), (1,)) assert_size_stride(primals_34, (128, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_35, (128,), (1,)) assert_size_stride(primals_36, (128, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_37, (128,), (1,)) assert_size_stride(primals_38, (64, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_39, (64,), (1,)) assert_size_stride(primals_40, (64, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_41, (64,), (1,)) assert_size_stride(primals_42, (32, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_43, (32,), (1,)) assert_size_stride(primals_44, (32, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_45, (32,), (1,)) assert_size_stride(primals_46, (4, 32, 3, 3), (288, 9, 3, 1)) assert_size_stride(primals_47, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(3, 3), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 32, 64, 64), (131072, 4096, 64, 1)) buf1 = empty_strided_cuda((4, 32, 64, 64), (131072, 4096, 64, 1), torch.bool) buf2 = empty_strided_cuda((4, 32, 64, 64), (131072, 4096, 64, 1), torch.float32) get_raw_stream(0) triton_poi_fused_convolution_leaky_relu_0[grid(524288)](buf0, primals_2, buf1, buf2, 524288, XBLOCK=512, num_warps=8, num_stages=1) del primals_2 buf3 = extern_kernels.convolution(buf2, primals_4, stride=(1, 1), padding=(3, 3), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf3, (4, 32, 64, 64), (131072, 4096, 64, 1)) buf4 = empty_strided_cuda((4, 32, 64, 64), (131072, 4096, 64, 1), torch.bool) buf5 = buf0 del buf0 triton_poi_fused_convolution_leaky_relu_0[grid(524288)](buf3, primals_5, buf4, buf5, 524288, XBLOCK=512, num_warps=8, num_stages=1) del primals_5 buf6 = empty_strided_cuda((4, 32, 32, 32), (32768, 1024, 32, 1), torch.float32) triton_poi_fused_avg_pool2d_1[grid(131072)](buf5, buf6, 131072, XBLOCK=512, num_warps=8, num_stages=1) buf7 = extern_kernels.convolution(buf6, primals_6, stride=(1, 1), padding=(2, 2), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf7, (4, 64, 32, 32), (65536, 1024, 32, 1)) buf8 = empty_strided_cuda((4, 64, 32, 32), (65536, 1024, 32, 1), torch.bool) buf9 = empty_strided_cuda((4, 64, 32, 32), (65536, 1024, 32, 1), torch.float32) triton_poi_fused_convolution_leaky_relu_2[grid(262144)](buf7, primals_7, buf8, buf9, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del primals_7 buf10 = extern_kernels.convolution(buf9, primals_8, stride=(1, 1), padding=(2, 2), 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)) buf11 = empty_strided_cuda((4, 64, 32, 32), (65536, 1024, 32, 1), torch.bool) buf12 = buf7 del buf7 triton_poi_fused_convolution_leaky_relu_2[grid(262144)](buf10, primals_9, buf11, buf12, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del primals_9 buf13 = empty_strided_cuda((4, 64, 16, 16), (16384, 256, 16, 1), torch.float32) triton_poi_fused_avg_pool2d_3[grid(65536)](buf12, buf13, 65536, XBLOCK=512, num_warps=4, num_stages=1) buf14 = extern_kernels.convolution(buf13, primals_10, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf14, (4, 128, 16, 16), (32768, 256, 16, 1)) buf15 = empty_strided_cuda((4, 128, 16, 16), (32768, 256, 16, 1), torch.bool) buf16 = empty_strided_cuda((4, 128, 16, 16), (32768, 256, 16, 1), torch.float32) triton_poi_fused_convolution_leaky_relu_4[grid(131072)](buf14, primals_11, buf15, buf16, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del primals_11 buf17 = extern_kernels.convolution(buf16, primals_12, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf17, (4, 128, 16, 16), (32768, 256, 16, 1)) buf18 = empty_strided_cuda((4, 128, 16, 16), (32768, 256, 16, 1), torch.bool) buf19 = buf14 del buf14 triton_poi_fused_convolution_leaky_relu_4[grid(131072)](buf17, primals_13, buf18, buf19, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del primals_13 buf20 = empty_strided_cuda((4, 128, 8, 8), (8192, 64, 8, 1), torch. float32) triton_poi_fused_avg_pool2d_5[grid(32768)](buf19, buf20, 32768, XBLOCK=128, num_warps=4, num_stages=1) buf21 = extern_kernels.convolution(buf20, primals_14, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf21, (4, 256, 8, 8), (16384, 64, 8, 1)) buf22 = empty_strided_cuda((4, 256, 8, 8), (16384, 64, 8, 1), torch .bool) buf23 = empty_strided_cuda((4, 256, 8, 8), (16384, 64, 8, 1), torch .float32) triton_poi_fused_convolution_leaky_relu_6[grid(65536)](buf21, primals_15, buf22, buf23, 65536, XBLOCK=512, num_warps=4, num_stages=1) del primals_15 buf24 = extern_kernels.convolution(buf23, primals_16, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf24, (4, 256, 8, 8), (16384, 64, 8, 1)) buf25 = empty_strided_cuda((4, 256, 8, 8), (16384, 64, 8, 1), torch .bool) buf26 = buf21 del buf21 triton_poi_fused_convolution_leaky_relu_6[grid(65536)](buf24, primals_17, buf25, buf26, 65536, XBLOCK=512, num_warps=4, num_stages=1) del primals_17 buf27 = empty_strided_cuda((4, 256, 4, 4), (4096, 16, 4, 1), torch. float32) triton_poi_fused_avg_pool2d_7[grid(16384)](buf26, buf27, 16384, XBLOCK=128, num_warps=4, num_stages=1) buf28 = extern_kernels.convolution(buf27, primals_18, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf28, (4, 512, 4, 4), (8192, 16, 4, 1)) buf29 = empty_strided_cuda((4, 512, 4, 4), (8192, 16, 4, 1), torch.bool ) buf30 = empty_strided_cuda((4, 512, 4, 4), (8192, 16, 4, 1), torch. float32) triton_poi_fused_convolution_leaky_relu_8[grid(32768)](buf28, primals_19, buf29, buf30, 32768, XBLOCK=128, num_warps=4, num_stages=1) del primals_19 buf31 = extern_kernels.convolution(buf30, primals_20, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf31, (4, 512, 4, 4), (8192, 16, 4, 1)) buf32 = empty_strided_cuda((4, 512, 4, 4), (8192, 16, 4, 1), torch.bool ) buf33 = buf28 del buf28 triton_poi_fused_convolution_leaky_relu_8[grid(32768)](buf31, primals_21, buf32, buf33, 32768, XBLOCK=128, num_warps=4, num_stages=1) del primals_21 buf34 = empty_strided_cuda((4, 512, 2, 2), (2048, 4, 2, 1), torch. float32) triton_poi_fused_avg_pool2d_9[grid(8192)](buf33, buf34, 8192, XBLOCK=256, num_warps=4, num_stages=1) buf35 = extern_kernels.convolution(buf34, primals_22, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf35, (4, 512, 2, 2), (2048, 4, 2, 1)) buf36 = empty_strided_cuda((4, 512, 2, 2), (2048, 4, 2, 1), torch.bool) buf37 = empty_strided_cuda((4, 512, 2, 2), (2048, 4, 2, 1), torch. float32) triton_poi_fused_convolution_leaky_relu_10[grid(8192)](buf35, primals_23, buf36, buf37, 8192, XBLOCK=256, num_warps=4, num_stages=1) del buf35 del primals_23 buf38 = extern_kernels.convolution(buf37, primals_24, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf38, (4, 512, 2, 2), (2048, 4, 2, 1)) buf39 = empty_strided_cuda((4, 512, 2, 2), (2048, 4, 2, 1), torch.bool) triton_poi_fused_convolution_leaky_relu_11[grid(8192)](buf38, primals_25, buf39, 8192, XBLOCK=256, num_warps=4, num_stages=1) buf40 = empty_strided_cuda((4, 1), (1, 1), torch.int64) triton_poi_fused__to_copy_12[grid(4)](buf40, 4, XBLOCK=4, num_warps =1, num_stages=1) buf41 = empty_strided_cuda((4, 1), (1, 1), torch.int64) triton_poi_fused_add_clamp_13[grid(4)](buf41, 4, XBLOCK=4, num_warps=1, num_stages=1) buf42 = empty_strided_cuda((4,), (1,), torch.int64) triton_poi_fused__to_copy_12[grid(4)](buf42, 4, XBLOCK=4, num_warps =1, num_stages=1) buf43 = empty_strided_cuda((4,), (1,), torch.int64) triton_poi_fused_add_clamp_13[grid(4)](buf43, 4, XBLOCK=4, num_warps=1, num_stages=1) buf46 = empty_strided_cuda((4,), (1,), torch.float32) triton_poi_fused__to_copy_add_arange_clamp_mul_sub_14[grid(4)](buf46, 4, XBLOCK=4, num_warps=1, num_stages=1) buf48 = empty_strided_cuda((4, 1), (1, 1), torch.float32) triton_poi_fused__to_copy_add_arange_clamp_mul_sub_14[grid(4)](buf48, 4, XBLOCK=4, num_warps=1, num_stages=1) buf45 = buf31 del buf31 buf49 = buf45 del buf45 buf50 = buf49 del buf49 triton_poi_fused__unsafe_index_add_convolution_leaky_relu_mul_sub_15[ grid(32768)](buf50, buf41, buf42, buf39, buf38, primals_25, buf40, buf43, buf46, buf48, 32768, XBLOCK=128, num_warps=4, num_stages=1) del buf38 del primals_25 buf51 = extern_kernels.convolution(buf50, primals_26, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf51, (4, 512, 4, 4), (8192, 16, 4, 1)) buf52 = empty_strided_cuda((4, 512, 4, 4), (8192, 16, 4, 1), torch.bool ) triton_poi_fused_convolution_leaky_relu_16[grid(32768)](buf51, primals_27, buf52, 32768, XBLOCK=256, num_warps=4, num_stages=1) buf53 = reinterpret_tensor(buf24, (4, 1024, 4, 4), (16384, 16, 4, 1), 0 ) del buf24 triton_poi_fused_cat_17[grid(65536)](buf52, buf51, primals_27, buf33, buf53, 65536, XBLOCK=512, num_warps=4, num_stages=1) del buf51 del primals_27 buf54 = extern_kernels.convolution(buf53, primals_28, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf54, (4, 512, 4, 4), (8192, 16, 4, 1)) buf55 = empty_strided_cuda((4, 512, 4, 4), (8192, 16, 4, 1), torch.bool ) triton_poi_fused_convolution_leaky_relu_16[grid(32768)](buf54, primals_29, buf55, 32768, XBLOCK=256, num_warps=4, num_stages=1) buf56 = empty_strided_cuda((8, 1), (1, 1), torch.int64) triton_poi_fused__to_copy_18[grid(8)](buf56, 8, XBLOCK=8, num_warps =1, num_stages=1) buf57 = empty_strided_cuda((8, 1), (1, 1), torch.int64) triton_poi_fused_add_clamp_19[grid(8)](buf57, 8, XBLOCK=8, num_warps=1, num_stages=1) buf58 = empty_strided_cuda((8,), (1,), torch.int64) triton_poi_fused__to_copy_18[grid(8)](buf58, 8, XBLOCK=8, num_warps =1, num_stages=1) buf59 = empty_strided_cuda((8,), (1,), torch.int64) triton_poi_fused_add_clamp_19[grid(8)](buf59, 8, XBLOCK=8, num_warps=1, num_stages=1) buf62 = empty_strided_cuda((8,), (1,), torch.float32) triton_poi_fused__to_copy_add_arange_clamp_mul_sub_20[grid(8)](buf62, 8, XBLOCK=8, num_warps=1, num_stages=1) buf64 = empty_strided_cuda((8, 1), (1, 1), torch.float32) triton_poi_fused__to_copy_add_arange_clamp_mul_sub_20[grid(8)](buf64, 8, XBLOCK=8, num_warps=1, num_stages=1) buf61 = reinterpret_tensor(buf17, (4, 512, 8, 8), (32768, 64, 8, 1), 0) del buf17 buf65 = buf61 del buf61 buf66 = buf65 del buf65 triton_poi_fused__unsafe_index_add_convolution_leaky_relu_mul_sub_21[ grid(131072)](buf66, buf57, buf58, buf55, buf54, primals_29, buf56, buf59, buf62, buf64, 131072, XBLOCK=512, num_warps=8, num_stages=1) del buf54 del primals_29 buf67 = extern_kernels.convolution(buf66, primals_30, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf67, (4, 256, 8, 8), (16384, 64, 8, 1)) buf68 = empty_strided_cuda((4, 256, 8, 8), (16384, 64, 8, 1), torch .bool) triton_poi_fused_convolution_leaky_relu_22[grid(65536)](buf67, primals_31, buf68, 65536, XBLOCK=256, num_warps=4, num_stages=1) buf69 = empty_strided_cuda((4, 512, 8, 8), (32768, 64, 8, 1), torch .float32) triton_poi_fused_cat_23[grid(131072)](buf68, buf67, primals_31, buf26, buf69, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del buf67 del primals_31 buf70 = extern_kernels.convolution(buf69, primals_32, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf70, (4, 256, 8, 8), (16384, 64, 8, 1)) buf71 = empty_strided_cuda((4, 256, 8, 8), (16384, 64, 8, 1), torch .bool) triton_poi_fused_convolution_leaky_relu_22[grid(65536)](buf70, primals_33, buf71, 65536, XBLOCK=256, num_warps=4, num_stages=1) buf72 = empty_strided_cuda((16, 1), (1, 1), torch.int64) triton_poi_fused__to_copy_24[grid(16)](buf72, 16, XBLOCK=16, num_warps=1, num_stages=1) buf73 = empty_strided_cuda((16, 1), (1, 1), torch.int64) triton_poi_fused_add_clamp_25[grid(16)](buf73, 16, XBLOCK=16, num_warps=1, num_stages=1) buf74 = empty_strided_cuda((16,), (1,), torch.int64) triton_poi_fused__to_copy_24[grid(16)](buf74, 16, XBLOCK=16, num_warps=1, num_stages=1) buf75 = empty_strided_cuda((16,), (1,), torch.int64) triton_poi_fused_add_clamp_25[grid(16)](buf75, 16, XBLOCK=16, num_warps=1, num_stages=1) buf78 = empty_strided_cuda((16,), (1,), torch.float32) triton_poi_fused__to_copy_add_arange_clamp_mul_sub_26[grid(16)](buf78, 16, XBLOCK=16, num_warps=1, num_stages=1) buf80 = empty_strided_cuda((16, 1), (1, 1), torch.float32) triton_poi_fused__to_copy_add_arange_clamp_mul_sub_26[grid(16)](buf80, 16, XBLOCK=16, num_warps=1, num_stages=1) buf77 = reinterpret_tensor(buf10, (4, 256, 16, 16), (65536, 256, 16, 1), 0) del buf10 buf81 = buf77 del buf77 buf82 = buf81 del buf81 triton_poi_fused__unsafe_index_add_convolution_leaky_relu_mul_sub_27[ grid(262144)](buf82, buf73, buf74, buf71, buf70, primals_33, buf72, buf75, buf78, buf80, 262144, XBLOCK=512, num_warps=8, num_stages=1) del primals_33 buf83 = extern_kernels.convolution(buf82, primals_34, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf83, (4, 128, 16, 16), (32768, 256, 16, 1)) buf84 = empty_strided_cuda((4, 128, 16, 16), (32768, 256, 16, 1), torch.bool) triton_poi_fused_convolution_leaky_relu_28[grid(131072)](buf83, primals_35, buf84, 131072, XBLOCK=1024, num_warps=4, num_stages=1) buf85 = empty_strided_cuda((4, 256, 16, 16), (65536, 256, 16, 1), torch.float32) triton_poi_fused_cat_29[grid(262144)](buf84, buf83, primals_35, buf19, buf85, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del buf83 del primals_35 buf86 = extern_kernels.convolution(buf85, primals_36, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf86, (4, 128, 16, 16), (32768, 256, 16, 1)) buf87 = empty_strided_cuda((4, 128, 16, 16), (32768, 256, 16, 1), torch.bool) triton_poi_fused_convolution_leaky_relu_28[grid(131072)](buf86, primals_37, buf87, 131072, XBLOCK=1024, num_warps=4, num_stages=1) buf88 = empty_strided_cuda((32, 1), (1, 1), torch.int64) triton_poi_fused__to_copy_30[grid(32)](buf88, 32, XBLOCK=32, num_warps=1, num_stages=1) buf89 = empty_strided_cuda((32, 1), (1, 1), torch.int64) triton_poi_fused_add_clamp_31[grid(32)](buf89, 32, XBLOCK=32, num_warps=1, num_stages=1) buf90 = empty_strided_cuda((32,), (1,), torch.int64) triton_poi_fused__to_copy_30[grid(32)](buf90, 32, XBLOCK=32, num_warps=1, num_stages=1) buf91 = empty_strided_cuda((32,), (1,), torch.int64) triton_poi_fused_add_clamp_31[grid(32)](buf91, 32, XBLOCK=32, num_warps=1, num_stages=1) buf94 = empty_strided_cuda((32,), (1,), torch.float32) triton_poi_fused__to_copy_add_arange_clamp_mul_sub_32[grid(32)](buf94, 32, XBLOCK=32, num_warps=1, num_stages=1) buf96 = empty_strided_cuda((32, 1), (1, 1), torch.float32) triton_poi_fused__to_copy_add_arange_clamp_mul_sub_32[grid(32)](buf96, 32, XBLOCK=32, num_warps=1, num_stages=1) buf93 = reinterpret_tensor(buf3, (4, 128, 32, 32), (131072, 1024, 32, 1), 0) del buf3 buf97 = buf93 del buf93 buf98 = buf97 del buf97 triton_poi_fused__unsafe_index_add_convolution_leaky_relu_mul_sub_33[ grid(524288)](buf98, buf89, buf90, buf87, buf86, primals_37, buf88, buf91, buf94, buf96, 524288, XBLOCK=512, num_warps=8, num_stages=1) del buf86 del primals_37 buf99 = extern_kernels.convolution(buf98, primals_38, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf99, (4, 64, 32, 32), (65536, 1024, 32, 1)) buf100 = empty_strided_cuda((4, 64, 32, 32), (65536, 1024, 32, 1), torch.bool) triton_poi_fused_convolution_leaky_relu_34[grid(262144)](buf99, primals_39, buf100, 262144, XBLOCK=512, num_warps=8, num_stages=1) buf101 = empty_strided_cuda((4, 128, 32, 32), (131072, 1024, 32, 1), torch.float32) triton_poi_fused_cat_35[grid(524288)](buf100, buf99, primals_39, buf12, buf101, 524288, XBLOCK=512, num_warps=8, num_stages=1) del buf99 del primals_39 buf102 = extern_kernels.convolution(buf101, primals_40, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf102, (4, 64, 32, 32), (65536, 1024, 32, 1)) buf103 = empty_strided_cuda((4, 64, 32, 32), (65536, 1024, 32, 1), torch.bool) triton_poi_fused_convolution_leaky_relu_34[grid(262144)](buf102, primals_41, buf103, 262144, XBLOCK=512, num_warps=8, num_stages=1) buf104 = empty_strided_cuda((64, 1), (1, 1), torch.int64) triton_poi_fused__to_copy_36[grid(64)](buf104, 64, XBLOCK=64, num_warps=1, num_stages=1) buf105 = empty_strided_cuda((64, 1), (1, 1), torch.int64) triton_poi_fused_add_clamp_37[grid(64)](buf105, 64, XBLOCK=64, num_warps=1, num_stages=1) buf106 = empty_strided_cuda((64,), (1,), torch.int64) triton_poi_fused__to_copy_36[grid(64)](buf106, 64, XBLOCK=64, num_warps=1, num_stages=1) buf107 = empty_strided_cuda((64,), (1,), torch.int64) triton_poi_fused_add_clamp_37[grid(64)](buf107, 64, XBLOCK=64, num_warps=1, num_stages=1) buf110 = empty_strided_cuda((64,), (1,), torch.float32) triton_poi_fused__to_copy_add_arange_clamp_mul_sub_38[grid(64)](buf110, 64, XBLOCK=64, num_warps=1, num_stages=1) buf112 = empty_strided_cuda((64, 1), (1, 1), torch.float32) triton_poi_fused__to_copy_add_arange_clamp_mul_sub_38[grid(64)](buf112, 64, XBLOCK=64, num_warps=1, num_stages=1) buf109 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.float32) buf113 = buf109 del buf109 buf114 = buf113 del buf113 triton_poi_fused__unsafe_index_add_convolution_leaky_relu_mul_sub_39[ grid(1048576)](buf114, buf105, buf106, buf103, buf102, primals_41, buf104, buf107, buf110, buf112, 1048576, XBLOCK= 1024, num_warps=4, num_stages=1) del buf102 del primals_41 buf115 = extern_kernels.convolution(buf114, primals_42, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf115, (4, 32, 64, 64), (131072, 4096, 64, 1)) buf116 = empty_strided_cuda((4, 32, 64, 64), (131072, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_leaky_relu_40[grid(524288)](buf115, primals_43, buf116, 524288, XBLOCK=1024, num_warps=4, num_stages=1) buf117 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.float32) triton_poi_fused_cat_41[grid(1048576)](buf116, buf115, primals_43, buf5, buf117, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del primals_43 buf118 = extern_kernels.convolution(buf117, primals_44, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf118, (4, 32, 64, 64), (131072, 4096, 64, 1)) buf119 = empty_strided_cuda((4, 32, 64, 64), (131072, 4096, 64, 1), torch.bool) buf120 = buf115 del buf115 triton_poi_fused_convolution_leaky_relu_0[grid(524288)](buf118, primals_45, buf119, buf120, 524288, XBLOCK=512, num_warps=8, num_stages=1) del buf118 del primals_45 buf121 = extern_kernels.convolution(buf120, primals_46, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf121, (4, 4, 64, 64), (16384, 4096, 64, 1)) buf122 = empty_strided_cuda((4, 4, 64, 64), (16384, 4096, 64, 1), torch.bool) buf123 = reinterpret_tensor(buf70, (4, 4, 64, 64), (16384, 4096, 64, 1), 0) del buf70 triton_poi_fused_convolution_leaky_relu_42[grid(65536)](buf121, primals_47, buf122, buf123, 65536, XBLOCK=256, num_warps=4, num_stages=1) del buf121 del primals_47 return (buf123, primals_1, primals_3, primals_4, primals_6, primals_8, primals_10, primals_12, primals_14, primals_16, primals_18, primals_20, primals_22, primals_24, primals_26, primals_28, primals_30, primals_32, primals_34, primals_36, primals_38, primals_40, primals_42, primals_44, primals_46, buf1, buf2, buf4, buf5, buf6, buf8, buf9, buf11, buf12, buf13, buf15, buf16, buf18, buf19, buf20, buf22, buf23, buf25, buf26, buf27, buf29, buf30, buf32, buf33, buf34, buf36, buf37, buf39, buf40, buf41, buf42, buf43, buf46, buf48, buf50, buf52, buf53, buf55, buf56, buf57, buf58, buf59, buf62, buf64, buf66, buf68, buf69, buf71, buf72, buf73, buf74, buf75, buf78, buf80, buf82, buf84, buf85, buf87, buf88, buf89, buf90, buf91, buf94, buf96, buf98, buf100, buf101, buf103, buf104, buf105, buf106, buf107, buf110, buf112, buf114, buf116, buf117, buf119, buf120, buf122) class down(nn.Module): def __init__(self, inChannels, outChannels, filterSize): """ Parameters ---------- inChannels : int number of input channels for the first convolutional layer. outChannels : int number of output channels for the first convolutional layer. This is also used as input and output channels for the second convolutional layer. filterSize : int filter size for the convolution filter. input N would create a N x N filter. """ super(down, self).__init__() self.conv1 = nn.Conv2d(inChannels, outChannels, filterSize, stride= 1, padding=int((filterSize - 1) / 2)) self.conv2 = nn.Conv2d(outChannels, outChannels, filterSize, stride =1, padding=int((filterSize - 1) / 2)) def forward(self, x): """ Forward. """ x = F.avg_pool2d(x, 2) x = F.leaky_relu(self.conv1(x), negative_slope=0.1) x = F.leaky_relu(self.conv2(x), negative_slope=0.1) return x class up(nn.Module): def __init__(self, inChannels, outChannels): super(up, self).__init__() self.conv1 = nn.Conv2d(inChannels, outChannels, 3, stride=1, padding=1) self.conv2 = nn.Conv2d(2 * outChannels, outChannels, 3, stride=1, padding=1) def forward(self, x, skpCn): """ Forward . """ x = F.interpolate(x, scale_factor=2, mode='bilinear') x = F.leaky_relu(self.conv1(x), negative_slope=0.1) x = F.leaky_relu(self.conv2(torch.cat((x, skpCn), 1)), negative_slope=0.1) return x class UNetNew(nn.Module): def __init__(self, inChannels, outChannels): super(UNetNew, self).__init__() self.conv1 = nn.Conv2d(inChannels, 32, 7, stride=1, padding=3) self.conv2 = nn.Conv2d(32, 32, 7, stride=1, padding=3) self.down1 = down(32, 64, 5) self.down2 = down(64, 128, 3) self.down3 = down(128, 256, 3) self.down4 = down(256, 512, 3) self.down5 = down(512, 512, 3) self.up1 = up(512, 512) self.up2 = up(512, 256) self.up3 = up(256, 128) self.up4 = up(128, 64) self.up5 = up(64, 32) self.conv3 = nn.Conv2d(32, outChannels, 3, stride=1, 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.down1.conv1.weight primals_7 = self.down1.conv1.bias primals_8 = self.down1.conv2.weight primals_9 = self.down1.conv2.bias primals_10 = self.down2.conv1.weight primals_11 = self.down2.conv1.bias primals_12 = self.down2.conv2.weight primals_13 = self.down2.conv2.bias primals_14 = self.down3.conv1.weight primals_15 = self.down3.conv1.bias primals_16 = self.down3.conv2.weight primals_17 = self.down3.conv2.bias primals_18 = self.down4.conv1.weight primals_19 = self.down4.conv1.bias primals_20 = self.down4.conv2.weight primals_21 = self.down4.conv2.bias primals_22 = self.down5.conv1.weight primals_23 = self.down5.conv1.bias primals_24 = self.down5.conv2.weight primals_25 = self.down5.conv2.bias primals_26 = self.up1.conv1.weight primals_27 = self.up1.conv1.bias primals_28 = self.up1.conv2.weight primals_29 = self.up1.conv2.bias primals_30 = self.up2.conv1.weight primals_31 = self.up2.conv1.bias primals_32 = self.up2.conv2.weight primals_33 = self.up2.conv2.bias primals_34 = self.up3.conv1.weight primals_35 = self.up3.conv1.bias primals_36 = self.up3.conv2.weight primals_37 = self.up3.conv2.bias primals_38 = self.up4.conv1.weight primals_39 = self.up4.conv1.bias primals_40 = self.up4.conv2.weight primals_41 = self.up4.conv2.bias primals_42 = self.up5.conv1.weight primals_43 = self.up5.conv1.bias primals_44 = self.up5.conv2.weight primals_45 = self.up5.conv2.bias primals_46 = self.conv3.weight primals_47 = self.conv3.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, 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, primals_32, primals_33, primals_34, primals_35, primals_36, primals_37, primals_38, primals_39, primals_40, primals_41, primals_42, primals_43, primals_44, primals_45, primals_46, primals_47]) return output[0]
delldu/VideoSlow
UNet
false
6,711
[ "MIT" ]
1
2badbbfa2187ea15ea37ef35e70a103ef98c1e33
https://github.com/delldu/VideoSlow/tree/2badbbfa2187ea15ea37ef35e70a103ef98c1e33
ConstantODE
import torch class ConstantODE(torch.nn.Module): def __init__(self): super(ConstantODE, self).__init__() self.a = torch.nn.Parameter(torch.tensor(0.2)) self.b = torch.nn.Parameter(torch.tensor(3.0)) def forward(self, t, y): return self.a + (y - (self.a * t + self.b)) ** 5 def y_exact(self, t): return self.a * t + self.b 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 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_sub_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 + 0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK]) tmp2 = tl.load(in_ptr1 + x0, xmask) tmp3 = tl.load(in_ptr2 + x0, xmask) tmp5 = tl.load(in_ptr3 + 0) tmp6 = tl.broadcast_to(tmp5, [XBLOCK]) tmp4 = tmp1 * tmp3 tmp7 = tmp4 + tmp6 tmp8 = tmp2 - tmp7 tmp9 = tmp8 * tmp8 tmp10 = tmp9 * tmp9 tmp11 = tmp10 * tmp8 tmp12 = tmp1 + tmp11 tl.store(out_ptr0 + x0, tmp12, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (), ()) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (), ()) 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((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mul_pow_sub_0[grid(256)](primals_1, primals_4, primals_2, primals_3, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) return buf0, primals_1, primals_2, primals_3, primals_4 class ConstantODENew(torch.nn.Module): def __init__(self): super(ConstantODENew, self).__init__() self.a = torch.nn.Parameter(torch.tensor(0.2)) self.b = torch.nn.Parameter(torch.tensor(3.0)) def y_exact(self, t): return self.a * t + self.b def forward(self, input_0, input_1): primals_1 = self.a primals_3 = self.b primals_2 = input_0 primals_4 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
gaozhihan/torchdiffeq
ConstantODE
false
6,712
[ "MIT" ]
1
414781617d595ba01cc3f23382e25ab890f4ca66
https://github.com/gaozhihan/torchdiffeq/tree/414781617d595ba01cc3f23382e25ab890f4ca66
SineODE
import math import torch class SineODE(torch.nn.Module): def forward(self, t, y): return 2 * y / t + t ** 4 * torch.sin(2 * t) - t ** 2 + 4 * t ** 3 def y_exact(self, t): return -0.5 * t ** 4 * torch.cos(2 * t) + 0.5 * t ** 3 * torch.sin( 2 * t) + 0.25 * t ** 2 * torch.cos(2 * t) - t ** 3 + 2 * t ** 4 + ( math.pi - 0.25) * t ** 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.triton_helpers import math as tl_math import 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_poi_fused_add_div_mul_pow_sin_sub_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) tmp3 = tl.load(in_ptr1 + x0, xmask) tmp1 = 2.0 tmp2 = tmp0 * tmp1 tmp4 = tmp2 / tmp3 tmp5 = tmp3 * tmp3 tmp6 = tmp5 * tmp5 tmp7 = tmp3 * tmp1 tmp8 = tl_math.sin(tmp7) tmp9 = tmp6 * tmp8 tmp10 = tmp4 + tmp9 tmp11 = tmp10 - tmp5 tmp12 = tmp5 * tmp3 tmp13 = 4.0 tmp14 = tmp12 * tmp13 tmp15 = tmp11 + tmp14 tl.store(out_ptr0 + x0, tmp15, 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_div_mul_pow_sin_sub_0[grid(256)](arg0_1, arg1_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf0, class SineODENew(torch.nn.Module): def y_exact(self, t): return -0.5 * t ** 4 * torch.cos(2 * t) + 0.5 * t ** 3 * torch.sin( 2 * t) + 0.25 * t ** 2 * torch.cos(2 * t) - t ** 3 + 2 * t ** 4 + ( math.pi - 0.25) * t ** 2 def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
gaozhihan/torchdiffeq
SineODE
false
6,713
[ "MIT" ]
1
414781617d595ba01cc3f23382e25ab890f4ca66
https://github.com/gaozhihan/torchdiffeq/tree/414781617d595ba01cc3f23382e25ab890f4ca66
ResidualBlock
import torch from torch import nn class ResidualBlock(nn.Module): def __init__(self, filter_size, dilation, residual_channels, dilated_channels, skip_channels): super().__init__() self.conv = nn.Conv1d(residual_channels, dilated_channels, kernel_size=filter_size, padding=dilation * (filter_size - 1), dilation=dilation) self.res = nn.Conv1d(dilated_channels // 2, residual_channels, 1) self.skip = nn.Conv1d(dilated_channels // 2, skip_channels, 1) self.filter_size = filter_size self.dilation = dilation self.residual_channels = residual_channels def forward(self, x, condition): length = x.shape[2] h = self.conv(x) h = h[:, :, :length] h += condition tanh_z, sig_z = torch.split(h, h.size(1) // 2, dim=1) z = torch.tanh(tanh_z) * torch.sigmoid(sig_z) if x.shape[2] == z.shape[2]: residual = self.res(z) + x else: residual = self.res(z) + x[:, :, -1:] skip_connection = self.skip(z) return residual, skip_connection def initialize(self, n): self.queue = torch.zeros((n, self.residual_channels, self.dilation * (self.filter_size - 1) + 1), dtype=self.conv.weight.dtype) self.conv.padding = 0 def pop(self, condition): return self(self.queue, condition) def push(self, x): self.queue = torch.cat((self.queue[:, :, 1:], x), dim=2) def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'filter_size': 4, 'dilation': 1, 'residual_channels': 4, 'dilated_channels': 4, 'skip_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 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_poi_fused_mul_sigmoid_tanh_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 % 2 x2 = xindex // 8 x4 = xindex % 8 x3 = xindex tmp10 = tl.load(in_ptr0 + (x0 + 7 * x1 + 28 * x2), xmask) tmp11 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp22 = tl.load(in_ptr0 + (14 + x0 + 7 * x1 + 28 * x2), xmask) tmp23 = tl.load(in_ptr1 + (2 + x1), xmask, eviction_policy='evict_last') tmp0 = x0 tmp1 = tl.full([1], 4, tl.int64) tmp2 = tmp0 < tmp1 tmp3 = tl.load(in_ptr0 + (x0 + 7 * x1 + 28 * x2), tmp2 & xmask, other=0.0) tmp4 = tl.load(in_ptr1 + x1, tmp2 & xmask, eviction_policy='evict_last', other=0.0) tmp5 = tmp3 + tmp4 tmp6 = tl.load(in_ptr2 + (x4 + 16 * x2), tmp2 & xmask, other=0.0) tmp7 = tmp5 + tmp6 tmp8 = tl.full(tmp7.shape, 0.0, tmp7.dtype) tmp9 = tl.where(tmp2, tmp7, tmp8) tmp12 = tmp10 + tmp11 tmp13 = tl.where(tmp2, tmp9, tmp12) tmp14 = libdevice.tanh(tmp13) tmp15 = tl.load(in_ptr0 + (14 + x0 + 7 * x1 + 28 * x2), tmp2 & xmask, other=0.0) tmp16 = tl.load(in_ptr1 + (2 + x1), tmp2 & xmask, eviction_policy= 'evict_last', other=0.0) tmp17 = tmp15 + tmp16 tmp18 = tl.load(in_ptr2 + (8 + x4 + 16 * x2), tmp2 & xmask, other=0.0) tmp19 = tmp17 + tmp18 tmp20 = tl.full(tmp19.shape, 0.0, tmp19.dtype) tmp21 = tl.where(tmp2, tmp19, tmp20) tmp24 = tmp22 + tmp23 tmp25 = tl.where(tmp2, tmp21, tmp24) tmp26 = tl.sigmoid(tmp25) tmp27 = tmp14 * tmp26 tl.store(out_ptr0 + x3, tmp14, xmask) tl.store(out_ptr1 + x3, tmp26, xmask) tl.store(out_ptr2 + x3, tmp27, xmask) @triton.jit def triton_poi_fused_add_convolution_1(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_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x3, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tl.store(in_out_ptr0 + x3, tmp4, xmask) @triton.jit def triton_poi_fused_convolution_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 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) 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,)) assert_size_stride(primals_4, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_5, (4, 2, 1), (2, 1, 1)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (4, 2, 1), (2, 1, 1)) assert_size_stride(primals_8, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(1,), padding=(3,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 7), (28, 7, 1)) buf1 = empty_strided_cuda((4, 2, 4), (8, 4, 1), torch.float32) buf2 = empty_strided_cuda((4, 2, 4), (8, 4, 1), torch.float32) buf3 = empty_strided_cuda((4, 2, 4), (8, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_sigmoid_tanh_0[grid(32)](buf0, primals_3, primals_4, buf1, buf2, buf3, 32, XBLOCK=32, num_warps=1, num_stages=1) del buf0 del primals_3 del primals_4 buf4 = extern_kernels.convolution(buf3, primals_5, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf4, (4, 4, 4), (16, 4, 1)) buf5 = buf4 del buf4 triton_poi_fused_add_convolution_1[grid(64)](buf5, primals_6, primals_1, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_6 buf6 = extern_kernels.convolution(buf3, primals_7, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf6, (4, 4, 4), (16, 4, 1)) buf7 = buf6 del buf6 triton_poi_fused_convolution_2[grid(64)](buf7, primals_8, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_8 return (buf5, buf7, primals_1, primals_2, primals_5, primals_7, buf1, buf2, buf3) class ResidualBlockNew(nn.Module): def __init__(self, filter_size, dilation, residual_channels, dilated_channels, skip_channels): super().__init__() self.conv = nn.Conv1d(residual_channels, dilated_channels, kernel_size=filter_size, padding=dilation * (filter_size - 1), dilation=dilation) self.res = nn.Conv1d(dilated_channels // 2, residual_channels, 1) self.skip = nn.Conv1d(dilated_channels // 2, skip_channels, 1) self.filter_size = filter_size self.dilation = dilation self.residual_channels = residual_channels def initialize(self, n): self.queue = torch.zeros((n, self.residual_channels, self.dilation * (self.filter_size - 1) + 1), dtype=self.conv.weight.dtype) self.conv.padding = 0 def pop(self, condition): return self(self.queue, condition) def push(self, x): self.queue = torch.cat((self.queue[:, :, 1:], x), dim=2) def forward(self, input_0, input_1): primals_1 = self.conv.weight primals_3 = self.conv.bias primals_5 = self.res.weight primals_6 = self.res.bias primals_7 = self.skip.weight primals_8 = self.skip.bias primals_2 = input_0 primals_4 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8]) return output[0], output[1]
fukuroder/pytorch_lightning_wavenet
ResidualBlock
false
6,714
[ "MIT" ]
1
440ef4092397998edf0df4625f1f10157db2243e
https://github.com/fukuroder/pytorch_lightning_wavenet/tree/440ef4092397998edf0df4625f1f10157db2243e
EqualConvTranspose2d
import torch from torch import nn from math import sqrt def equal_lr(module, name='weight'): EqualLR.apply(module, name) return module class EqualLR: def __init__(self, name): self.name = name def compute_weight(self, module): weight = getattr(module, self.name + '_orig') fan_in = weight.data.size(1) * weight.data[0][0].numel() return weight * sqrt(2 / fan_in) @staticmethod def apply(module, name): fn = EqualLR(name) weight = getattr(module, name) del module._parameters[name] module.register_parameter(name + '_orig', nn.Parameter(weight.data)) module.register_forward_pre_hook(fn) return fn def __call__(self, module, input): weight = self.compute_weight(module) setattr(module, self.name, weight) class EqualConvTranspose2d(nn.Module): def __init__(self, *args, **kwargs): super().__init__() conv = nn.ConvTranspose2d(*args, **kwargs) conv.weight.data.normal_() conv.bias.data.zero_() self.conv = equal_lr(conv) def forward(self, input): return self.conv(input) 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 import nn from math import sqrt 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, 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.1767766952966369 tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 784 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 49 % 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) 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,), (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((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_0[grid(256)](primals_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 buf1 = extern_kernels.convolution(primals_3, buf0, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 7, 7), (196, 49, 7, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_1[grid(784)](buf2, primals_2, 784, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 return buf2, buf0, primals_3, buf0 def equal_lr(module, name='weight'): EqualLR.apply(module, name) return module class EqualLR: def __init__(self, name): self.name = name def compute_weight(self, module): weight = getattr(module, self.name + '_orig') fan_in = weight.data.size(1) * weight.data[0][0].numel() return weight * sqrt(2 / fan_in) @staticmethod def apply(module, name): fn = EqualLR(name) weight = getattr(module, name) del module._parameters[name] module.register_parameter(name + '_orig', nn.Parameter(weight.data)) module.register_forward_pre_hook(fn) return fn def __call__(self, module, input): weight = self.compute_weight(module) setattr(module, self.name, weight) class EqualConvTranspose2dNew(nn.Module): def __init__(self, *args, **kwargs): super().__init__() conv = nn.ConvTranspose2d(*args, **kwargs) conv.weight.data.normal_() conv.bias.data.zero_() self.conv = equal_lr(conv) def forward(self, input_0): primals_2 = self.conv.bias primals_1 = self.conv.weight_orig primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
g33sean/RTIL
EqualConvTranspose2d
false
6,715
[ "BSD-2-Clause", "MIT" ]
1
5325f6d5e3ddf7579b6bd8199898e00eff3da631
https://github.com/g33sean/RTIL/tree/5325f6d5e3ddf7579b6bd8199898e00eff3da631
CombineSlices
import torch from torch import nn import torch.utils.data import torch.utils.data.distributed import torch.optim class CombineSlices(nn.Module): def __init__(self, slice_dim=2): super().__init__() self.slice_dim = slice_dim def forward(self, x): return torch.index_select(x, dim=self.slice_dim, index=torch.tensor (0, device=x.device)) 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 import nn import torch.utils.data import torch.utils.data.distributed import torch.optim 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_index_select_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 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask) tl.store(out_ptr0 + x2, tmp0, 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, 4), (16, 4, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_index_select_0[grid(64)](arg0_1, buf0, 64, XBLOCK= 64, num_warps=1, num_stages=1) del arg0_1 return buf0, class CombineSlicesNew(nn.Module): def __init__(self, slice_dim=2): super().__init__() self.slice_dim = slice_dim def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
gbosdet/fastMRI
CombineSlices
false
6,716
[ "MIT" ]
1
7f94f8006f8919d98fb87788b6dadec9a58d1a3a
https://github.com/gbosdet/fastMRI/tree/7f94f8006f8919d98fb87788b6dadec9a58d1a3a
ResBlock
import torch import torch.nn as nn def conv3x3(in_planes, out_planes, stride=1): """3x3 convolution with padding""" return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False) def norm(dim): return nn.GroupNorm(min(32, dim), dim) class ResBlock(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=None): super(ResBlock, self).__init__() self.norm1 = norm(inplanes) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.conv1 = conv3x3(inplanes, planes, stride) self.norm2 = norm(planes) self.conv2 = conv3x3(planes, planes) def forward(self, x): shortcut = x out = self.relu(self.norm1(x)) if self.downsample is not None: shortcut = self.downsample(out) out = self.conv1(out) out = self.norm2(out) out = self.relu(out) out = self.conv2(out) return out + shortcut def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'inplanes': 4, 'planes': 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_per_fused_native_group_norm_relu_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr2, out_ptr3, 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 x2 = xindex % 4 tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp24 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last') tmp26 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last') tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tl.where(xmask, tmp1, 0) tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.full([XBLOCK, 1], 16, tl.int32) tmp9 = tmp8.to(tl.float32) tmp10 = tmp7 / tmp9 tmp11 = tmp1 - tmp10 tmp12 = tmp11 * tmp11 tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK]) tmp15 = tl.where(xmask, tmp13, 0) tmp16 = tl.sum(tmp15, 1)[:, None] tmp17 = tmp0 - tmp10 tmp18 = 16.0 tmp19 = tmp16 / tmp18 tmp20 = 1e-05 tmp21 = tmp19 + tmp20 tmp22 = libdevice.rsqrt(tmp21) tmp23 = tmp17 * tmp22 tmp25 = tmp23 * tmp24 tmp27 = tmp25 + tmp26 tmp28 = tl.full([1, 1], 0, tl.int32) tmp29 = triton_helpers.maximum(tmp28, tmp27) tl.store(out_ptr2 + (r1 + 16 * x0), tmp29, xmask) tl.store(out_ptr3 + x0, tmp22, xmask) tl.store(out_ptr0 + x0, tmp10, 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 x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask) tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x0, tmp2, 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, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (4, 4, 3, 3), (36, 9, 3, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf12 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) get_raw_stream(0) triton_per_fused_native_group_norm_relu_0[grid(16)](primals_1, primals_2, primals_3, buf0, buf3, buf12, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del primals_2 del primals_3 buf4 = extern_kernels.convolution(buf3, primals_4, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 4, 4, 4), (64, 16, 4, 1)) buf5 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) buf9 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf8 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) triton_per_fused_native_group_norm_relu_0[grid(16)](buf4, primals_5, primals_6, buf5, buf9, buf8, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del primals_6 buf10 = extern_kernels.convolution(buf9, primals_7, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf10, (4, 4, 4, 4), (64, 16, 4, 1)) buf11 = buf10 del buf10 triton_poi_fused_add_1[grid(256)](buf11, primals_1, 256, XBLOCK=128, num_warps=4, num_stages=1) return (buf11, primals_1, primals_4, primals_5, primals_7, buf3, buf4, reinterpret_tensor(buf5, (4, 4), (4, 1), 0), reinterpret_tensor( buf8, (4, 4), (4, 1), 0), buf9, reinterpret_tensor(buf0, (4, 4, 1), (4, 1, 1), 0), reinterpret_tensor(buf12, (4, 4, 1), (4, 1, 1), 0)) def conv3x3(in_planes, out_planes, stride=1): """3x3 convolution with padding""" return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False) def norm(dim): return nn.GroupNorm(min(32, dim), dim) class ResBlockNew(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=None): super(ResBlockNew, self).__init__() self.norm1 = norm(inplanes) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.conv1 = conv3x3(inplanes, planes, stride) self.norm2 = norm(planes) self.conv2 = conv3x3(planes, planes) def forward(self, input_0): primals_2 = self.norm1.weight primals_3 = self.norm1.bias primals_4 = self.conv1.weight primals_5 = self.norm2.weight primals_6 = self.norm2.bias primals_7 = self.conv2.weight primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
gaozhihan/torchdiffeq
ResBlock
false
6,717
[ "MIT" ]
1
414781617d595ba01cc3f23382e25ab890f4ca66
https://github.com/gaozhihan/torchdiffeq/tree/414781617d595ba01cc3f23382e25ab890f4ca66
ConcatConv2d
import torch import torch.nn as nn class ConcatConv2d(nn.Module): def __init__(self, dim_in, dim_out, ksize=3, stride=1, padding=0, dilation=1, groups=1, bias=True, transpose=False): super(ConcatConv2d, self).__init__() module = nn.ConvTranspose2d if transpose else nn.Conv2d self._layer = module(dim_in + 1, dim_out, kernel_size=ksize, stride =stride, padding=padding, dilation=dilation, groups=groups, bias=bias) def forward(self, t, x): tt = torch.ones_like(x[:, :1, :, :]) * t ttx = torch.cat([tt, x], 1) return self._layer(ttx) def get_inputs(): return [torch.rand([4, 1, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'dim_in': 4, 'dim_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 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_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 320 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 16 % 5 x0 = xindex % 16 x2 = xindex // 80 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 16 * x2), tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 5, tl.int64) tmp9 = tl.load(in_ptr1 + (x0 + 16 * (-1 + x1) + 64 * x2), tmp6 & xmask, other=0.0) tmp10 = tl.where(tmp4, tmp5, tmp9) tl.store(out_ptr0 + x3, tmp10, xmask) @triton.jit def triton_poi_fused_convolution_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 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) 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, 4, 4), (16, 16, 4, 1)) assert_size_stride(primals_3, (4, 5, 3, 3), (45, 9, 3, 1)) assert_size_stride(primals_4, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 5, 4, 4), (80, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(320)](primals_2, primals_1, buf0, 320, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 del primals_2 buf1 = extern_kernels.convolution(buf0, primals_3, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 2, 2), (16, 4, 2, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_1[grid(64)](buf2, primals_4, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_4 return buf2, primals_3, buf0 class ConcatConv2dNew(nn.Module): def __init__(self, dim_in, dim_out, ksize=3, stride=1, padding=0, dilation=1, groups=1, bias=True, transpose=False): super(ConcatConv2dNew, self).__init__() module = nn.ConvTranspose2d if transpose else nn.Conv2d self._layer = module(dim_in + 1, dim_out, kernel_size=ksize, stride =stride, padding=padding, dilation=dilation, groups=groups, bias=bias) def forward(self, input_0, input_1): primals_3 = self._layer.weight primals_4 = self._layer.bias primals_2 = input_0 primals_1 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
gaozhihan/torchdiffeq
ConcatConv2d
false
6,718
[ "MIT" ]
1
414781617d595ba01cc3f23382e25ab890f4ca66
https://github.com/gaozhihan/torchdiffeq/tree/414781617d595ba01cc3f23382e25ab890f4ca66
Decoder
import torch import torch.nn as nn class Decoder(nn.Module): def __init__(self, latent_dim=4, obs_dim=2, nhidden=20): super(Decoder, self).__init__() self.relu = nn.ReLU(inplace=True) self.fc1 = nn.Linear(latent_dim, nhidden) self.fc2 = nn.Linear(nhidden, obs_dim) def forward(self, z): out = self.fc1(z) out = self.relu(out) out = self.fc2(out) return out def get_inputs(): return [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_relu_threshold_backward_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1280 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex x0 = xindex % 20 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 + x4, tmp6, xmask) @triton.jit def triton_poi_fused_view_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1280 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 20 x1 = xindex // 20 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 20 * x1 + 80 * (x1 % 4 // 4) + 320 * ((4 * (x1 // 4 % 4) + x1 % 4) // 16)), xmask) tl.store(out_ptr0 + x2, tmp0, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (20, 4), (4, 1)) assert_size_stride(primals_2, (20,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (2, 20), (20, 1)) assert_size_stride(primals_5, (2,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 20), (20, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 20), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 20), (320, 80, 20, 1), 0) del buf0 buf4 = empty_strided_cuda((4, 4, 4, 20), (320, 80, 20, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(1280)](buf1, primals_2, buf4, 1280, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 20), (20, 1), torch.float32) triton_poi_fused_view_1[grid(1280)](buf1, buf2, 1280, XBLOCK=256, num_warps=4, num_stages=1) del buf1 buf3 = empty_strided_cuda((64, 2), (2, 1), torch.float32) extern_kernels.addmm(primals_5, buf2, reinterpret_tensor(primals_4, (20, 2), (1, 20), 0), alpha=1, beta=1, out=buf3) del primals_5 return reinterpret_tensor(buf3, (4, 4, 4, 2), (32, 8, 2, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf2, primals_4, buf4 class DecoderNew(nn.Module): def __init__(self, latent_dim=4, obs_dim=2, nhidden=20): super(DecoderNew, self).__init__() self.relu = nn.ReLU(inplace=True) self.fc1 = nn.Linear(latent_dim, nhidden) self.fc2 = nn.Linear(nhidden, obs_dim) 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]
gaozhihan/torchdiffeq
Decoder
false
6,719
[ "MIT" ]
1
414781617d595ba01cc3f23382e25ab890f4ca66
https://github.com/gaozhihan/torchdiffeq/tree/414781617d595ba01cc3f23382e25ab890f4ca66
MaskedHuberLoss
import torch import torch.nn as nn class MaskedHuberLoss(torch.nn.Module): def __init__(self): super(MaskedHuberLoss, self).__init__() def forward(self, output, labels, mask): lossHuber = nn.SmoothL1Loss(reduction='none') l = lossHuber(output * mask, labels * mask) l = l.sum(dim=(1, 2)) mask = mask.sum(dim=(1, 2)) l = l / mask return l.mean() 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 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_smooth_l1_loss_sum_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, 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) r2 = rindex x0 = xindex % 4 x1 = xindex // 4 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * r2 + 64 * x1), xmask, other=0.0) tmp1 = tl.load(in_ptr1 + (x0 + 4 * r2 + 64 * x1), xmask, other=0.0) tmp3 = tl.load(in_ptr2 + (x0 + 4 * r2 + 64 * x1), xmask, other=0.0) tmp2 = tmp0 * tmp1 tmp4 = tmp3 * tmp1 tmp5 = tmp2 - tmp4 tmp6 = tl_math.abs(tmp5) tmp7 = 1.0 tmp8 = tmp6 < tmp7 tmp9 = tmp6 * tmp6 tmp10 = 0.5 tmp11 = tmp9 * tmp10 tmp12 = tmp11 * tmp7 tmp13 = tmp6 - tmp10 tmp14 = tl.where(tmp8, tmp12, tmp13) tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK]) tmp17 = tl.where(xmask, tmp15, 0) tmp18 = tl.sum(tmp17, 1)[:, None] tmp19 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp21 = tl.where(xmask, tmp19, 0) tmp22 = tl.sum(tmp21, 1)[:, None] tl.store(out_ptr0 + x3, tmp18, xmask) tl.store(out_ptr1 + x3, tmp22, xmask) @triton.jit def triton_per_fused_div_mean_1(in_out_ptr0, in_ptr0, in_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) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.load(in_ptr1 + r0, None) tmp2 = tmp0 / tmp1 tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp5 = tl.sum(tmp3, 1)[:, None] tmp6 = 16.0 tmp7 = tmp5 / tmp6 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 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) buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_per_fused_mul_smooth_l1_loss_sum_0[grid(16)](arg0_1, arg1_1, arg2_1, buf0, buf1, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 del arg2_1 buf2 = empty_strided_cuda((), (), torch.float32) buf3 = buf2 del buf2 triton_per_fused_div_mean_1[grid(1)](buf3, buf0, buf1, 1, 16, XBLOCK=1, num_warps=2, num_stages=1) del buf0 del buf1 return buf3, class MaskedHuberLossNew(torch.nn.Module): def __init__(self): super(MaskedHuberLossNew, 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]
gabrieleangeletti/GndNet
MaskedHuberLoss
false
6,720
[ "MIT" ]
1
323af65c9c16a725805f480ff799936b77b04d53
https://github.com/gabrieleangeletti/GndNet/tree/323af65c9c16a725805f480ff799936b77b04d53
Netleaky
import torch import torch.nn as nn import torch.nn.functional as F class Netleaky(nn.Module): def __init__(self, input_dim, output_dim): super(Netleaky, self).__init__() self.linear1 = nn.Linear(input_dim, 32) self.linear2 = nn.Linear(32, 32) self.linear3 = nn.Linear(32, 64) self.linear4 = nn.Linear(64, output_dim) def forward(self, x): x = F.relu(self.linear1(x)) x = F.relu(self.linear2(x)) x = F.relu(self.linear3(x)) x = self.linear4(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'output_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 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): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 32 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) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) @triton.jit def triton_poi_fused_relu_threshold_backward_1(in_out_ptr0, in_ptr0, out_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 % 64 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) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) 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, (32, 4), (4, 1)) assert_size_stride(primals_2, (32,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (32, 32), (32, 1)) assert_size_stride(primals_5, (32,), (1,)) assert_size_stride(primals_6, (64, 32), (32, 1)) assert_size_stride(primals_7, (64,), (1,)) assert_size_stride(primals_8, (4, 64), (64, 1)) assert_size_stride(primals_9, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 32), (32, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 32), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 32), (512, 128, 32, 1), 0) del buf0 buf9 = empty_strided_cuda((4, 4, 4, 32), (512, 128, 32, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(2048)](buf1, primals_2, buf9, 2048, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 32), (32, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 32), (32, 1), 0), reinterpret_tensor(primals_4, (32, 32), (1, 32), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 32), (512, 128, 32, 1), 0) del buf2 buf8 = empty_strided_cuda((4, 4, 4, 32), (512, 128, 32, 1), torch.bool) triton_poi_fused_relu_threshold_backward_0[grid(2048)](buf3, primals_5, buf8, 2048, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 64), (64, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf3, (64, 32), (32, 1), 0), reinterpret_tensor(primals_6, (32, 64), (1, 32), 0), out=buf4) buf5 = reinterpret_tensor(buf4, (4, 4, 4, 64), (1024, 256, 64, 1), 0) del buf4 buf7 = empty_strided_cuda((4, 4, 4, 64), (1024, 256, 64, 1), torch.bool ) triton_poi_fused_relu_threshold_backward_1[grid(4096)](buf5, primals_7, buf7, 4096, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 buf6 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_9, reinterpret_tensor(buf5, (64, 64), (64, 1), 0), reinterpret_tensor(primals_8, (64, 4), (1, 64), 0), alpha=1, beta=1, out=buf6) del primals_9 return reinterpret_tensor(buf6, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 32), (32, 1), 0), reinterpret_tensor( buf3, (64, 32), (32, 1), 0), reinterpret_tensor(buf5, (64, 64), (64, 1), 0), primals_8, buf7, primals_6, buf8, primals_4, buf9 class NetleakyNew(nn.Module): def __init__(self, input_dim, output_dim): super(NetleakyNew, self).__init__() self.linear1 = nn.Linear(input_dim, 32) self.linear2 = nn.Linear(32, 32) self.linear3 = nn.Linear(32, 64) self.linear4 = nn.Linear(64, output_dim) def forward(self, input_0): primals_1 = self.linear1.weight primals_2 = self.linear1.bias primals_4 = self.linear2.weight primals_5 = self.linear2.bias primals_6 = self.linear3.weight primals_7 = self.linear3.bias primals_8 = self.linear4.weight primals_9 = self.linear4.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]
gautam-sharma1/openRL
Netleaky
false
6,721
[ "MIT" ]
1
14310a97a328fe5682a01ee85d83a6b5e1ae29ca
https://github.com/gautam-sharma1/openRL/tree/14310a97a328fe5682a01ee85d83a6b5e1ae29ca
Cartesian
import torch from torch import nn import torch.utils.data import torch.utils.data.distributed import torch.optim class Cartesian(nn.Module): def forward(self, x): r, phi = x[..., 0], x[..., 1] return torch.stack((r * torch.cos(phi), r * torch.sin(phi)), dim=-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 from torch._inductor.runtime.triton_helpers import math as tl_math from torch import nn import torch.utils.data import torch.utils.data.distributed import torch.optim 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_stack_0(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 % 2 x1 = xindex // 2 x2 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + 4 * x1, tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp6 = tl.load(in_ptr0 + (1 + 4 * x1), tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp7 = tl_math.cos(tmp6) tmp8 = tmp5 * tmp7 tmp9 = tl.full(tmp8.shape, 0.0, tmp8.dtype) tmp10 = tl.where(tmp4, tmp8, tmp9) tmp11 = tmp0 >= tmp3 tl.full([1], 2, tl.int64) tmp14 = tl.load(in_ptr0 + 4 * x1, tmp11 & xmask, eviction_policy= 'evict_last', other=0.0) tmp15 = tl.load(in_ptr0 + (1 + 4 * x1), tmp11 & xmask, eviction_policy= 'evict_last', other=0.0) tmp16 = tl_math.sin(tmp15) tmp17 = tmp14 * tmp16 tmp18 = tl.full(tmp17.shape, 0.0, tmp17.dtype) tmp19 = tl.where(tmp11, tmp17, tmp18) tmp20 = tl.where(tmp4, tmp10, tmp19) tl.store(out_ptr0 + x2, tmp20, 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, 2), (32, 8, 2, 1), torch.float32) get_raw_stream(0) triton_poi_fused_stack_0[grid(128)](arg0_1, buf0, 128, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf0, class CartesianNew(nn.Module): def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
gbosdet/fastMRI
Cartesian
false
6,722
[ "MIT" ]
1
7f94f8006f8919d98fb87788b6dadec9a58d1a3a
https://github.com/gbosdet/fastMRI/tree/7f94f8006f8919d98fb87788b6dadec9a58d1a3a
Net
import torch import torch.nn as nn import torch.nn.functional as F class Net(nn.Module): def __init__(self, input_dim, output_dim): super(Net, self).__init__() self.linear1 = nn.Linear(input_dim, 256) self.linear2 = nn.Linear(256, output_dim) def forward(self, x): x = F.relu(self.linear1(x)) x = self.linear2(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'output_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 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): 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) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (256, 4), (4, 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, (4, 256), (256, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 256), (256, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 256), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 256), (4096, 1024, 256, 1), 0 ) del buf0 buf3 = empty_strided_cuda((4, 4, 4, 256), (4096, 1024, 256, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(16384)](buf1, primals_2, buf3, 16384, XBLOCK=128, 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, 256), (256, 1), 0), reinterpret_tensor(primals_4, (256, 4), (1, 256), 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, 256), (256, 1), 0), primals_4, buf3 class NetNew(nn.Module): def __init__(self, input_dim, output_dim): super(NetNew, self).__init__() self.linear1 = nn.Linear(input_dim, 256) self.linear2 = nn.Linear(256, output_dim) def forward(self, input_0): primals_1 = self.linear1.weight primals_2 = self.linear1.bias primals_4 = self.linear2.weight primals_5 = self.linear2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
gautam-sharma1/openRL
Net
false
6,723
[ "MIT" ]
1
14310a97a328fe5682a01ee85d83a6b5e1ae29ca
https://github.com/gautam-sharma1/openRL/tree/14310a97a328fe5682a01ee85d83a6b5e1ae29ca
NN_2layer_regression
import torch from torch import nn class NN_2layer_regression(nn.Module): def __init__(self, input_dim, interm_dim1, interm_dim2): super().__init__() self.d = input_dim self.interm_dim1 = interm_dim1 self.interm_dim2 = interm_dim2 self.fc1 = nn.Linear(input_dim, interm_dim1) self.fc2 = nn.Linear(interm_dim1, interm_dim2) self.fc3 = nn.Linear(interm_dim2, 1) self.modules_sizes = [self.d * self.interm_dim1, self.interm_dim1, self.interm_dim1 * self.interm_dim2, self.interm_dim2, self. interm_dim2, 1] self.w_opt = None self.alpha = None self.mixed_linead_weights = None def set_mixed_linear_weights(self, w): self.mixed_linead_weights = self.alpha * w + (1 - self.alpha ) * self.w_opt self.mixed_linead_weights.retain_grad() fc_parameters = torch.split(self.mixed_linead_weights, self. modules_sizes) ind = 0 for module in self.modules(): if type(module) == nn.Linear: module.weight = torch.nn.Parameter(fc_parameters[ind].view( module.weight.shape)) ind += 1 module.bias = torch.nn.Parameter(fc_parameters[ind].view( module.bias.shape)) ind += 1 def forward(self, x, w=None): if w is not None: assert w.requires_grad assert self.alpha is not None assert self.w_opt is not None self.set_mixed_linear_weights(w) out = torch.tanh(self.fc1(x)) out = torch.tanh(self.fc2(out)) out = self.fc3(out) return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'interm_dim1': 4, 'interm_dim2': 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_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, primals_6, primals_7) = 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,)) assert_size_stride(primals_6, (1, 4), (4, 1)) assert_size_stride(primals_7, (1,), (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 get_raw_stream(0) triton_poi_fused_tanh_0[grid(256)](buf1, primals_2, 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 triton_poi_fused_tanh_0[grid(256)](buf3, primals_5, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf5 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_6, (4, 1), (1, 4), 0), alpha=1, beta=1, out=buf5) del primals_7 return reinterpret_tensor(buf5, (4, 4, 4, 1), (16, 4, 1, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf1, buf3, primals_6, primals_4 class NN_2layer_regressionNew(nn.Module): def __init__(self, input_dim, interm_dim1, interm_dim2): super().__init__() self.d = input_dim self.interm_dim1 = interm_dim1 self.interm_dim2 = interm_dim2 self.fc1 = nn.Linear(input_dim, interm_dim1) self.fc2 = nn.Linear(interm_dim1, interm_dim2) self.fc3 = nn.Linear(interm_dim2, 1) self.modules_sizes = [self.d * self.interm_dim1, self.interm_dim1, self.interm_dim1 * self.interm_dim2, self.interm_dim2, self. interm_dim2, 1] self.w_opt = None self.alpha = None self.mixed_linead_weights = None def set_mixed_linear_weights(self, w): self.mixed_linead_weights = self.alpha * w + (1 - self.alpha ) * self.w_opt self.mixed_linead_weights.retain_grad() fc_parameters = torch.split(self.mixed_linead_weights, self. modules_sizes) ind = 0 for module in self.modules(): if type(module) == nn.Linear: module.weight = torch.nn.Parameter(fc_parameters[ind].view( module.weight.shape)) ind += 1 module.bias = torch.nn.Parameter(fc_parameters[ind].view( module.bias.shape)) ind += 1 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]
gaseln/FLIX_small_scale_experiments
NN_2layer_regression
false
6,724
[ "MIT" ]
1
af9ebd7f192fc0f67a6a94af7939fd3d6f548bd6
https://github.com/gaseln/FLIX_small_scale_experiments/tree/af9ebd7f192fc0f67a6a94af7939fd3d6f548bd6
HighwayLayer
import torch import torch.nn.functional as F import torch.nn as nn import torch.onnx.operators class HighwayLayer(nn.Module): def __init__(self, input_dim, transform_activation=F.relu, gate_activation=F.softmax, gate_bias=-2): super().__init__() self.highway_transform_activation = transform_activation self.highway_gate_activation = gate_activation self.highway_transform = nn.Linear(input_dim, input_dim) self.highway_gate = nn.Linear(input_dim, input_dim) self.highway_gate.bias.data.fill_(gate_bias) def forward(self, x): transform_output = self.highway_transform_activation(self. highway_transform(x)) gate_output = self.highway_gate_activation(self.highway_gate(x)) transformation_part = torch.mul(transform_output, gate_output) carry_part = torch.mul(1 - gate_output, x) return torch.add(transformation_part, carry_part) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_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 math as tl_math import torch.nn.functional as F import torch.nn as nn import torch.onnx.operators 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_add_mul_relu_rsub_1(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 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') tmp9 = tl.load(in_ptr1 + x3, xmask) tmp15 = tl.load(in_ptr2 + x3, xmask) tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tmp10 = tl.full([1], 0, tl.int32) tmp11 = triton_helpers.maximum(tmp10, tmp9) tmp12 = tmp11 * tmp8 tmp13 = 1.0 tmp14 = tmp13 - tmp8 tmp16 = tmp14 * tmp15 tmp17 = tmp12 + tmp16 tl.store(in_out_ptr0 + x3, tmp17, 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__softmax_0[grid(256)](buf1, buf2, 256, XBLOCK=256, num_warps=4, num_stages=1) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf4 = buf3 del buf3 triton_poi_fused__softmax_add_mul_relu_rsub_1[grid(256)](buf4, buf2, buf0, primals_3, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf2 return buf4, primals_3, buf0, buf1 class HighwayLayerNew(nn.Module): def __init__(self, input_dim, transform_activation=F.relu, gate_activation=F.softmax, gate_bias=-2): super().__init__() self.highway_transform_activation = transform_activation self.highway_gate_activation = gate_activation self.highway_transform = nn.Linear(input_dim, input_dim) self.highway_gate = nn.Linear(input_dim, input_dim) self.highway_gate.bias.data.fill_(gate_bias) def forward(self, input_0): primals_1 = self.highway_transform.weight primals_2 = self.highway_transform.bias primals_4 = self.highway_gate.weight primals_5 = self.highway_gate.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
gardenia22/translate
HighwayLayer
false
6,725
[ "BSD-3-Clause" ]
1
0be57c8f55b52fc9d39197efa02e05d1c1cda024
https://github.com/gardenia22/translate/tree/0be57c8f55b52fc9d39197efa02e05d1c1cda024
DPSLTMAdapter
import math import torch from torch import Tensor import torch.nn as nn from torch.nn.utils.rnn import pad_sequence import torch.utils.data import torch.utils.data.distributed import torch.nn.parallel from typing import Optional from typing import Union from typing import List from typing import Tuple from torch.nn.utils.rnn import PackedSequence from torch.nn.utils.rnn import pack_padded_sequence from typing import Dict from torch.nn.modules.module import _IncompatibleKeys def _compute_last_states(h_n: 'List[torch.Tensor]', c_n: 'List[torch.Tensor]', seq_lengths: 'List[int]') ->Tuple[torch.Tensor, torch.Tensor]: """ Given h and c values of all time steps, this function computes the h and c values for each sequence at their last timestep (this can vary across sequences with different sequence lengths). Args: h_n: A list of hidden state values across all timesteps. c_n: A list of cell state values across all timesteps. seq_lengths: the length parameter used in the torch.nn.utils.rnn.packed_padded_sequence function to create a PackedSequence. This can be computed using the _compute_seq_lengths function. Returns: h_last: Contains the last hidden state values for each of the sequences. If the i'th sequence has a length of l_i, then h_last[i,:] contains the hidden state corresponding to the i'th sequence at timestep l_i. c_last: The structure is the same as h_last, except that it contains the last cell state values for each of the sequences. """ max_batch_size = len(seq_lengths) hidden_size = h_n[0].shape[-1] h_last = torch.zeros(max_batch_size, hidden_size) c_last = torch.zeros(max_batch_size, hidden_size) for i, seq_len in enumerate(seq_lengths): h_last[i, :] = h_n[seq_len - 1][i, :] c_last[i, :] = c_n[seq_len - 1][i, :] return h_last, c_last def _compute_seq_lengths(batch_sizes: 'torch.Tensor') ->List[int]: """ Computes the sequence lengths (the length parameter used in the packed_padded_sequence function to create a PackedSequence). Args: batch_sizes: Contains the batch sizes as stored in a PackedSequence Returns: running_seq_lengths: the length parameter used in the torch.nn.utils.rnn.packed_padded_sequence function to create a PackedSequence. It's a list of the same length as batch_sizes. """ max_batch_size = batch_sizes[0] if len(batch_sizes) == 1: return [1] * max_batch_size running_seq = 0 running_seq_lengths = [] for i in range(1, len(batch_sizes)): delta = batch_sizes[i - 1].item() - batch_sizes[i].item() running_seq += 1 running_seq_lengths += delta * [running_seq] running_seq += 1 running_seq_lengths += batch_sizes[-1].item() * [running_seq] running_seq_lengths.reverse() return running_seq_lengths def _concat_sequence_directions(forward: 'Union[List[torch.Tensor], Tuple[torch.Tensor]]', reverse: 'Union[List[torch.Tensor], Tuple[torch.Tensor]]', dim: 'int') ->Tuple[torch .Tensor]: """ Given two list/tuple of same length containing tensors, this function returns a concatenation along dimension d. So, output[i] : concatenation of forward[i] and reverse[i] along dimension dim. forward[i] and reverse[i] should have the same shape. This function is used for concatenating the outputs of the forward and reverse layer of a bidirectional LSTM. Args: forward: list/tuple containing n tensors, representing the output of the forward layer. reverse: list/tuple containing n tensors, representing the output of the backward layer. dim: the dimension along which the sequence of tensors within forward and reverse will be concatenated. Returns: output: list/tuple containing n concatenated tensors. """ if len(forward) != len(reverse): raise ValueError( 'The forward and reverse layer output sequences should have the same length' ) seq_length = len(forward) output = [0] * seq_length for i in range(seq_length): output[i] = torch.cat((forward[i], reverse[i]), dim=dim) return output def filter_out_old_keys(self, state_dict, prefix, local_metadata): new_state_dict = {param_name: param_value for param_name, param_value in state_dict.items() if param_name not in self.old_to_new} return new_state_dict class LSTMLinear(nn.Linear): """ This function is the same as a nn.Linear layer, except that in the backward pass the grad_samples get accumulated (instead of being concatenated as in the standard nn.Linear) """ def __init__(self, in_features: 'int', out_features: 'int', bias: 'bool'=True): super().__init__(in_features, out_features, bias) class DPLSTMCell(nn.Module): """ Internal-only class. Implements *one* step of LSTM so that a LSTM layer can be seen as repeated applications of this class. """ def __init__(self, input_size: 'int', hidden_size: 'int', bias: 'bool'): super().__init__() self.input_size = input_size self.hidden_size = hidden_size self.bias = bias self.ih = LSTMLinear(input_size, 4 * hidden_size, bias=self.bias) self.hh = LSTMLinear(hidden_size, 4 * hidden_size, bias=self.bias) self.reset_parameters() def reset_parameters(self): """ Resets parameters by initializing them from an uniform distribution. """ stdv = 1.0 / math.sqrt(self.hidden_size) for weight in self.parameters(): nn.init.uniform_(weight, -stdv, stdv) def set_max_batch_length(self, max_batch_length: 'int') ->None: """ Sets max batch length """ self.ih.max_batch_len = max_batch_length self.hh.max_batch_len = max_batch_length def forward(self, x: 'torch.Tensor', h_prev: 'torch.Tensor', c_prev: 'torch.Tensor', batch_size_t: 'Optional[int]'=None) ->Tuple[torch. Tensor, torch.Tensor]: if batch_size_t is None: gates = self.ih(x) + self.hh(h_prev) else: gates = self.ih(x) + self.hh(h_prev[:batch_size_t, :]) i_t_input, f_t_input, g_t_input, o_t_input = torch.split(gates, self.hidden_size, 1) i_t = torch.sigmoid(i_t_input) f_t = torch.sigmoid(f_t_input) g_t = torch.tanh(g_t_input) o_t = torch.sigmoid(o_t_input) if batch_size_t is None: c_t = f_t * c_prev + i_t * g_t else: c_t = f_t * c_prev[:batch_size_t, :] + i_t * g_t h_t = o_t * torch.tanh(c_t) return h_t, c_t class DPLSTMLayer(nn.Module): """ Implements *one* layer of LSTM in a way amenable to differential privacy. We don't expect you to use this directly: use DPLSTM instead :) """ def __init__(self, input_size: 'int', hidden_size: 'int', bias: 'bool', dropout: 'float', reverse: 'bool'=False): super().__init__() self.input_size = input_size self.hidden_size = hidden_size self.bias = bias self.dropout = dropout self.reverse = reverse self.cell = DPLSTMCell(input_size=input_size, hidden_size= hidden_size, bias=bias) self.dropout_layer = nn.Dropout(dropout) if dropout > 0 else None def set_max_batch_length(self, max_batch_length: 'int') ->None: """ Sets max batch length. Useful for PackedSequences """ self.cell.set_max_batch_length(max_batch_length) def forward(self, x: 'Union[torch.Tensor, Tuple]', state_init: 'Tuple[torch.Tensor, torch.Tensor]', batch_sizes: 'Optional[torch.Tensor]'=None) ->Tuple[torch.Tensor, Tuple[torch. Tensor, torch.Tensor]]: """ Implements the forward pass of the DPLSTMLayer when a sequence is given in input. Args: x: Input sequence to the DPLSTMCell of shape ``[T, B, D]``. state_init: Initial state of the LSTMCell as a tuple ``(h_0, c_0)`` where ``h_0`` is the initial hidden state and ``c_0`` is the initial cell state of the DPLSTMCell batch_sizes: Contains the batch sizes as stored in PackedSequence Returns: ``output, (h_n, c_n)`` where, ``output`` is of shape ``[T, B, H]`` and is a tensor containing the output features (``h_t``) from the last layer of the DPLSTMCell for each timestep ``t``. ``h_n`` is of shape ``[B, H]`` and is a tensor containing the hidden state for ``t = T``. ``c_n`` is of shape ``[B, H]`` tensor containing the cell state for ``t = T``. """ if batch_sizes is not None: seq_length = batch_sizes.size(0) if self.reverse: x = tuple(reversed(x)) batch_sizes = batch_sizes.flip(0) else: seq_length, _batch_sz, _ = x.shape if self.reverse: x = x.flip(0) x = torch.unbind(x, dim=0) h_0, c_0 = state_init h_n = [h_0] c_n = [c_0] batch_size_prev = h_0.shape[0] for t in range(seq_length): if batch_sizes is not None: batch_size_t = batch_sizes[t].item() delta = batch_size_t - batch_size_prev if delta > 0: h_cat = torch.cat((h_n[t], h_0[batch_size_prev: batch_size_t, :]), 0) c_cat = torch.cat((c_n[t], c_0[batch_size_prev: batch_size_t, :]), 0) h_next, c_next = self.cell(x[t], h_cat, c_cat, batch_size_t ) else: h_next, c_next = self.cell(x[t], h_n[t], c_n[t], batch_size_t) else: h_next, c_next = self.cell(x[t], h_n[t], c_n[t]) if self.dropout: h_next = self.dropout_layer(h_next) h_n.append(h_next) c_n.append(c_next) batch_size_prev = h_next.shape[0] if batch_sizes is None: h_n = torch.stack(h_n[1:], dim=0) return h_n.flip(0) if self.reverse else h_n, (h_n[-1], c_n[-1]) else: seq_lengths = _compute_seq_lengths(batch_sizes) h_temp, c_temp = h_n[1:], c_n[1:] h_last, c_last = _compute_last_states(h_temp, c_temp, seq_lengths) if self.reverse: h_temp = tuple(reversed(h_temp)) return h_temp, (h_last, c_last) class BidirectionalDPLSTMLayer(nn.Module): """ Implements *one* layer of Bidirectional LSTM in a way amenable to differential privacy. We don't expect you to use this directly: use DPLSTM instead :) """ def __init__(self, input_size: 'int', hidden_size: 'int', bias: 'bool', dropout: 'float'): super().__init__() self.input_size = input_size self.hidden_size = hidden_size self.bias = bias self.dropout = dropout self.forward_layer = DPLSTMLayer(input_size=input_size, hidden_size =hidden_size, bias=bias, dropout=dropout, reverse=False) self.reverse_layer = DPLSTMLayer(input_size=input_size, hidden_size =hidden_size, bias=bias, dropout=dropout, reverse=True) def set_max_batch_length(self, max_batch_length: 'int') ->None: """ Sets max batch length """ self.forward_layer.set_max_batch_length(max_batch_length) self.reverse_layer.set_max_batch_length(max_batch_length) def forward(self, x: 'torch.Tensor', state_init: 'Tuple[torch.Tensor, torch.Tensor]', batch_sizes: 'Optional[torch.Tensor]'=None) ->Tuple[torch.Tensor, Tuple[torch. Tensor, torch.Tensor]]: """ Implements the forward pass of the DPLSTM when a sequence is input. Dimensions as follows: - B: Batch size - T: Sequence length - D: LSTM input hidden size (eg from a word embedding) - H: LSTM output hidden size - P: number of directions (2 if bidirectional, else 1) Args: x: Input sequence to the DPLSTM of shape ``[T, B, D]`` state_init: Initial state of the LSTM as a tuple ``(h_0, c_0)``, where ``h_0`` of shape ``[P, B, H]`` contains the initial hidden state, and ``c_0`` of shape ``[P, B, H]`` contains the initial cell state. This argument can be (and defaults to) None, in which case zero tensors will be used. Returns: ``output, (h_n, c_n)`` where, ``output`` is of shape ``[T, B, H * P]`` and is a tensor containing the output features (``h_t``) from the last layer of the DPLSTM for each timestep ``t``. ``h_n`` is of shape ``[P, B, H]`` and contains the hidden state for ``t = T``. ``c_n`` is of shape ``[P, B, H]`` and contains the cell state for ``t = T``. """ h0, c0 = state_init h0_f, h0_r = h0.unbind(0) c0_f, c0_r = c0.unbind(0) out_f, (h_f, c_f) = self.forward_layer(x, (h0_f, c0_f), batch_sizes) out_r, (h_r, c_r) = self.reverse_layer(x, (h0_r, c0_r), batch_sizes) if batch_sizes is None: out = torch.cat([out_f, out_r], dim=-1) else: out = _concat_sequence_directions(out_f, out_r, -1) h = torch.stack([h_f, h_r], dim=0) c = torch.stack([c_f, c_r], dim=0) return out, (h, c) class ParamRenamedModule(nn.Module): """ This class defines a nn.Module whose parameters are renamed. This is useful when you want to reimplement a layer but make sure its state_dict and list of parameters are exactly the same as another reference layer so that you can have a drop-in replacement that does not depend on how your layer is actually implemented. In Opacus, this is used for DPLSTM, where our implementation leverages submodules and requires alignment to the state_dict of nn.LSTM. """ def __init__(self, rename_map: 'Dict[str, str]'): """ Initializes internal state. Subclass this instead of ``torch.nn.Module`` whenever you need to rename your model's state. Args: rename_map: mapping from old name -> new name for each parameter you want renamed. Note that this must be a 1:1 mapping! """ super().__init__() self.old_to_new = rename_map self.new_to_old = {v: k for k, v in rename_map.items()} self._register_state_dict_hook(filter_out_old_keys) def _register_renamed_parameters(self): """ Internal function. This function simply registers parameters under their new name. They will automatically mask their duplicates coming from submodules. This trick works because self.parameters() proceeds recursively from the top, going into submodules after processing items at the current level, and will not return duplicates. """ for old_name, param in super().named_parameters(): if old_name in self.old_to_new: new_name = self.old_to_new[old_name] self.register_parameter(new_name, param) def __setattr__(self, name: 'str', value: 'Union[Tensor, nn.Module]' ) ->None: """ Whenever you set an attribute, eg `self.linear`, this is called to actually register it in any nn.Module. We rely on the masking trick explained in the docs for ``_register_renamed_parameters`` to make sure we replace things only once. If a new parameter in the rename list is detected, we rename and mask it so next time this is called we will no longer find it. """ super().__setattr__(name, value) try: self._register_renamed_parameters() except AttributeError: pass def load_state_dict(self, state_dict: 'Dict[str, Tensor]', strict: 'bool'=True): """ Identical to ``torch.nn.Module.load_state_dict()`` but handles the renamed keys. """ missing_keys, unexpected_keys = super().load_state_dict(state_dict, strict=False) missing_keys = [k for k in missing_keys if k not in self.old_to_new] if strict: error_msgs = [] if len(unexpected_keys) > 0: error_msgs.insert(0, 'Unexpected key(s) in state_dict: {}. '.format(', '. join('"{}"'.format(k) for k in unexpected_keys))) if len(missing_keys) > 0: error_msgs.insert(0, 'Missing key(s) in state_dict: {}. '. format(', '.join('"{}"'.format(k) for k in missing_keys))) if len(error_msgs) > 0: raise RuntimeError( 'Error(s) in loading state_dict for {}:\n\t{}'.format( self.__class__.__name__, '\n\t'.join(error_msgs))) return _IncompatibleKeys(missing_keys, unexpected_keys) class DPLSTM(ParamRenamedModule): """ DP-friendly drop-in replacement of the ``torch.nn.LSTM`` module. Its state_dict matches that of nn.LSTM exactly, so that after training it can be exported and loaded by an nn.LSTM for inference. Refer to nn.LSTM's documentation for all parameters and inputs. """ def __init__(self, input_size: 'int', hidden_size: 'int', num_layers: 'int'=1, bias: 'bool'=True, batch_first: 'bool'=False, dropout: 'float'=0, bidirectional: 'bool'=False): rename_dict = self._make_rename_dict(num_layers, bias, bidirectional) super().__init__(rename_dict) self.input_size = input_size self.hidden_size = hidden_size self.num_layers = num_layers self.bias = bias self.batch_first = batch_first self.dropout = dropout self.bidirectional = bidirectional self.num_directions = 2 if self.bidirectional else 1 LayerClass = BidirectionalDPLSTMLayer if bidirectional else DPLSTMLayer self.layers = nn.ModuleList([LayerClass(input_size=self.input_size if i == 0 else self.hidden_size * self.num_directions, hidden_size =self.hidden_size, bias=self.bias, dropout=self.dropout if i < self.num_layers - 1 else 0) for i in range(num_layers)]) def forward(self, x: 'Union[torch.Tensor, PackedSequence]', state_init: 'Optional[Tuple[torch.Tensor, torch.Tensor]]'=None) ->Tuple[torch. Tensor, Tuple[torch.Tensor, torch.Tensor]]: """ Implements the forward pass of the DPLSTM when a sequence is input. Dimensions as follows: - B: Batch size - T: Sequence length - D: LSTM input hidden size (eg from a word embedding) - H: LSTM output hidden size - L: number of layers in the LSTM - P: number of directions (2 if bidirectional, else 1) Args: x: Input sequence to the DPLSTM of shape ``[T, B, D]``. Or it can be a PackedSequence. state_init: Initial state of the LSTM as a tuple ``(h_0, c_0)``, where: - ``h_0`` of shape ``[L*P, B, H]`` contains the initial hidden state - ``c_0`` of shape ``[L*P, B, H]`` contains the initial cell state This argument can be (and defaults to) None, in which case zero tensors will be used. Returns: ``output, (h_n, c_n)`` where, ``output`` is of shape ``[T, B, H * P]`` and is a tensor containing the output features (``h_t``) from the last layer of the DPLSTM for each timestep ``t``. ``h_n`` is of shape ``[L * P, B, H]`` and contains the hidden state for ``t = T``. ``c_n`` is of shape ``[L * P, B, H]`` and contains the cell state for ``t = T``. """ if isinstance(x, PackedSequence): x, batch_sizes, sorted_indices, unsorted_indices = x B = batch_sizes[0].item() _, _D = x.shape x = x.split(tuple(batch_sizes)) for layer in self.layers: layer.set_max_batch_length(B) else: sorted_indices = None unsorted_indices = None batch_sizes = None x = self._rearrange_batch_dim(x) _T, B, _D = x.shape L = self.num_layers P = 2 if self.bidirectional else 1 H = self.hidden_size h_0s, c_0s = state_init or (None, None) if h_0s is None: h_0s = torch.zeros(L, P, B, self.hidden_size, dtype=x[0].dtype, device=x[0].device) else: h_0s = h_0s.reshape([L, P, B, H]) h_0s = self._permute_hidden(h_0s, sorted_indices, 2) if c_0s is None: c_0s = torch.zeros(L, P, B, self.hidden_size, dtype=x[0].dtype, device=x[0].device) else: c_0s = c_0s.reshape([L, P, B, H]) c_0s = self._permute_hidden(c_0s, sorted_indices, 2) hs: 'List[torch.Tensor]' = [] cs: 'List[torch.Tensor]' = [] for layer, h0, c0 in zip(self.layers, h_0s, c_0s): if not self.bidirectional: h0 = h0.squeeze(0) c0 = c0.squeeze(0) x, (h, c) = layer(x, (h0, c0), batch_sizes) if not self.bidirectional: h = h.unsqueeze(0) c = c.unsqueeze(0) hs.append(h) cs.append(c) hs = torch.cat(hs, dim=0) cs = torch.cat(cs, dim=0) if batch_sizes is not None: seq_lengths = _compute_seq_lengths(batch_sizes) packed_data = pack_padded_sequence(pad_sequence(x, batch_first= False), seq_lengths, batch_first=True)[0] out = PackedSequence(packed_data, batch_sizes, sorted_indices, unsorted_indices) else: out = self._rearrange_batch_dim(x) return out, (self._permute_hidden(hs, unsorted_indices), self. _permute_hidden(cs, unsorted_indices)) def _permute_hidden(self, x: 'torch.Tensor', permutation: 'Optional[torch.Tensor]'=None, dim: 'int'=1) ->torch.Tensor: if permutation is None: return x if dim == 1: return x[:, permutation, :] elif dim == 2: return x[:, :, permutation, :] def _rearrange_batch_dim(self, x: 'torch.Tensor') ->torch.Tensor: if self.batch_first: x = x.transpose(0, 1) return x def __repr__(self): s = f'DPLSTM({self.input_size}, {self.hidden_size}, bias={self.bias}' if self.batch_first: s += f', batch_first={self.batch_first}' if self.num_layers > 1: s += f', num_layers={self.num_layers}' if self.dropout: s += f', dropout={self.dropout}' if self.bidirectional: s += f', bidirectional={self.bidirectional}' return s def _make_rename_dict(self, num_layers, bias, bidirectional): """ Programmatically constructs a dictionary old_name -> new_name to align with the param names used in ``torch.nn.LSTM``. """ d = {} components = ['weight'] + ['bias' if bias else []] matrices = ['ih', 'hh'] for i in range(num_layers): for c in components: for m in matrices: nn_name = f'{c}_{m}_l{i}' if bidirectional: d[f'layers.{i}.forward_layer.cell.{m}.{c}'] = nn_name d[f'layers.{i}.reverse_layer.cell.{m}.{c}' ] = nn_name + '_reverse' else: d[f'layers.{i}.cell.{m}.{c}'] = nn_name return d class DPSLTMAdapter(nn.Module): """ Adapter for DPLSTM. LSTM returns a tuple, but our testing tools need the model to return a single tensor in output. We do this adaption here. """ def __init__(self, *args, **kwargs): super().__init__() self.dplstm = DPLSTM(*args, **kwargs) def forward(self, x): out, _rest = self.dplstm(x) return out def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4, '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.triton_helpers import libdevice import math from torch import Tensor import torch.nn as nn from torch.nn.utils.rnn import pad_sequence import torch.utils.data import torch.utils.data.distributed import torch.nn.parallel from typing import Optional from typing import Union from typing import List from typing import Tuple from torch.nn.utils.rnn import PackedSequence from torch.nn.utils.rnn import pack_padded_sequence from typing import Dict from torch.nn.modules.module import _IncompatibleKeys 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_zeros_0(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 = 0.0 tl.store(out_ptr0 + x0, tmp0, xmask) @triton.jit def triton_poi_fused_add_mul_sigmoid_sigmoid_backward_tanh_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr4, out_ptr5, 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_ptr1 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + (x0 + 16 * x1), xmask) tmp4 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask) tmp9 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr2 + (12 + x0 + 16 * x1), xmask) tmp12 = tl.load(in_ptr3 + (12 + x0), xmask, eviction_policy='evict_last') tmp16 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask) tmp17 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last') tmp19 = tl.load(in_ptr2 + (8 + x0 + 16 * x1), xmask) tmp20 = tl.load(in_ptr3 + (8 + x0), xmask, eviction_policy='evict_last') tmp24 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask) tmp25 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last') tmp27 = tl.load(in_ptr2 + (4 + x0 + 16 * x1), xmask) tmp28 = tl.load(in_ptr3 + (4 + x0), xmask, eviction_policy='evict_last') tmp32 = tl.load(in_ptr4 + x2, xmask) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp7 = tl.sigmoid(tmp6) tmp10 = tmp8 + tmp9 tmp13 = tmp11 + tmp12 tmp14 = tmp10 + tmp13 tmp15 = tl.sigmoid(tmp14) tmp18 = tmp16 + tmp17 tmp21 = tmp19 + tmp20 tmp22 = tmp18 + tmp21 tmp23 = libdevice.tanh(tmp22) tmp26 = tmp24 + tmp25 tmp29 = tmp27 + tmp28 tmp30 = tmp26 + tmp29 tmp31 = tl.sigmoid(tmp30) tmp33 = tmp31 * tmp32 tmp34 = tmp7 * tmp23 tmp35 = tmp33 + tmp34 tmp36 = 1.0 tmp37 = tmp36 - tmp31 tmp38 = tmp31 * tmp37 tmp39 = libdevice.tanh(tmp35) tmp40 = tmp15 * tmp39 tl.store(out_ptr0 + x2, tmp7, xmask) tl.store(out_ptr1 + x2, tmp15, xmask) tl.store(out_ptr2 + x2, tmp23, xmask) tl.store(out_ptr3 + x2, tmp35, xmask) tl.store(out_ptr4 + x2, tmp38, xmask) tl.store(out_ptr5 + x2, tmp40, xmask) @triton.jit def triton_poi_fused_add_mul_sigmoid_tanh_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr4, out_ptr5, 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_ptr1 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + (x0 + 16 * x1), xmask) tmp4 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask) tmp9 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr2 + (4 + x0 + 16 * x1), xmask) tmp12 = tl.load(in_ptr3 + (4 + x0), xmask, eviction_policy='evict_last') tmp16 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask) tmp17 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last') tmp19 = tl.load(in_ptr2 + (12 + x0 + 16 * x1), xmask) tmp20 = tl.load(in_ptr3 + (12 + x0), xmask, eviction_policy='evict_last') tmp24 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask) tmp25 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last') tmp27 = tl.load(in_ptr2 + (8 + x0 + 16 * x1), xmask) tmp28 = tl.load(in_ptr3 + (8 + x0), xmask, eviction_policy='evict_last') tmp32 = tl.load(in_ptr4 + x2, xmask) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp7 = tl.sigmoid(tmp6) tmp10 = tmp8 + tmp9 tmp13 = tmp11 + tmp12 tmp14 = tmp10 + tmp13 tmp15 = tl.sigmoid(tmp14) tmp18 = tmp16 + tmp17 tmp21 = tmp19 + tmp20 tmp22 = tmp18 + tmp21 tmp23 = tl.sigmoid(tmp22) tmp26 = tmp24 + tmp25 tmp29 = tmp27 + tmp28 tmp30 = tmp26 + tmp29 tmp31 = libdevice.tanh(tmp30) tmp33 = tmp15 * tmp32 tmp34 = tmp7 * tmp31 tmp35 = tmp33 + tmp34 tmp36 = libdevice.tanh(tmp35) tmp37 = tmp23 * tmp36 tl.store(out_ptr0 + x2, tmp7, xmask) tl.store(out_ptr1 + x2, tmp15, xmask) tl.store(out_ptr2 + x2, tmp23, xmask) tl.store(out_ptr3 + x2, tmp31, xmask) tl.store(out_ptr4 + x2, tmp35, xmask) tl.store(out_ptr5 + x2, tmp37, xmask) @triton.jit def triton_poi_fused_add_mul_sigmoid_tanh_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, out_ptr1, out_ptr2, out_ptr3, 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_ptr1 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + (x0 + 16 * x1), xmask) tmp4 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask) tmp9 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr2 + (4 + x0 + 16 * x1), xmask) tmp12 = tl.load(in_ptr3 + (4 + x0), xmask, eviction_policy='evict_last') tmp16 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask) tmp17 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last') tmp19 = tl.load(in_ptr2 + (8 + x0 + 16 * x1), xmask) tmp20 = tl.load(in_ptr3 + (8 + x0), xmask, eviction_policy='evict_last') tmp24 = tl.load(in_ptr4 + x2, xmask) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp7 = tl.sigmoid(tmp6) tmp10 = tmp8 + tmp9 tmp13 = tmp11 + tmp12 tmp14 = tmp10 + tmp13 tmp15 = tl.sigmoid(tmp14) tmp18 = tmp16 + tmp17 tmp21 = tmp19 + tmp20 tmp22 = tmp18 + tmp21 tmp23 = libdevice.tanh(tmp22) tmp25 = tmp15 * tmp24 tmp26 = tmp7 * tmp23 tmp27 = tmp25 + tmp26 tmp28 = libdevice.tanh(tmp27) tl.store(out_ptr0 + x2, tmp7, xmask) tl.store(out_ptr1 + x2, tmp15, xmask) tl.store(out_ptr2 + x2, tmp23, xmask) tl.store(out_ptr3 + x2, tmp28, xmask) @triton.jit def triton_poi_fused_sigmoid_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 + (12 + x0 + 16 * x1), xmask) tmp1 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + (12 + x0 + 16 * x1), xmask) tmp4 = tl.load(in_ptr3 + (12 + x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp7 = tl.sigmoid(tmp6) tl.store(out_ptr0 + x2, tmp7, xmask) @triton.jit def triton_poi_fused_stack_5(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 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 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 4 * x1), 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_ptr1 + (x0 + 4 * (-4 + x1)), tmp9 & xmask, other=0.0) tmp11 = tmp0 >= tmp7 tmp12 = tl.full([1], 12, tl.int64) tmp13 = tmp0 < tmp12 tmp14 = tmp11 & tmp13 tmp15 = tl.load(in_ptr2 + (x0 + 4 * (-8 + x1)), tmp14 & xmask, other=0.0) tmp16 = tmp0 >= tmp12 tl.full([1], 16, tl.int64) tmp19 = tl.load(in_ptr3 + (x0 + 4 * (-12 + x1)), tmp16 & xmask, other=0.0) tmp20 = tl.load(in_ptr4 + (x0 + 4 * (-12 + x1)), tmp16 & xmask, other=0.0) tmp21 = tmp19 * tmp20 tmp22 = tl.full(tmp21.shape, 0.0, tmp21.dtype) tmp23 = tl.where(tmp16, tmp21, tmp22) tmp24 = tl.where(tmp14, tmp15, tmp23) tmp25 = tl.where(tmp9, tmp10, tmp24) tmp26 = tl.where(tmp4, tmp5, tmp25) tl.store(out_ptr0 + x2, tmp26, 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, (16, 4), (4, 1)) assert_size_stride(primals_3, (16,), (1,)) assert_size_stride(primals_4, (16, 4), (4, 1)) assert_size_stride(primals_5, (16,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((1, 1, 4, 4), (16, 1, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_zeros_0[grid(16)](buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) buf1 = empty_strided_cuda((4, 16), (16, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (4, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 16), (1, 4), 0), out=buf1) buf2 = empty_strided_cuda((4, 16), (16, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (4, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 16), (1, 4), 0), out=buf2) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf32 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_add_mul_sigmoid_sigmoid_backward_tanh_1[grid(16)](buf1 , primals_3, buf2, primals_5, buf0, buf3, buf5, buf4, buf6, buf32, buf7, 16, XBLOCK=16, num_warps=1, num_stages=1) buf8 = buf2 del buf2 extern_kernels.mm(reinterpret_tensor(primals_1, (4, 4), (4, 1), 16), reinterpret_tensor(primals_2, (4, 16), (1, 4), 0), out=buf8) buf9 = buf1 del buf1 extern_kernels.mm(buf7, reinterpret_tensor(primals_4, (4, 16), (1, 4), 0), out=buf9) buf10 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf11 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf13 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf12 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf14 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf15 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_add_mul_sigmoid_tanh_2[grid(16)](buf8, primals_3, buf9, primals_5, buf6, buf10, buf11, buf13, buf12, buf14, buf15, 16, XBLOCK=16, num_warps=1, num_stages=1) buf16 = buf9 del buf9 extern_kernels.mm(reinterpret_tensor(primals_1, (4, 4), (4, 1), 32), reinterpret_tensor(primals_2, (4, 16), (1, 4), 0), out=buf16) buf17 = buf8 del buf8 extern_kernels.mm(buf15, reinterpret_tensor(primals_4, (4, 16), (1, 4), 0), out=buf17) buf18 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf19 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf21 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf20 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf22 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf23 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_add_mul_sigmoid_tanh_2[grid(16)](buf16, primals_3, buf17, primals_5, buf14, buf18, buf19, buf21, buf20, buf22, buf23, 16, XBLOCK=16, num_warps=1, num_stages=1) buf24 = buf17 del buf17 extern_kernels.mm(reinterpret_tensor(primals_1, (4, 4), (4, 1), 48), reinterpret_tensor(primals_2, (4, 16), (1, 4), 0), out=buf24) del primals_2 buf25 = buf16 del buf16 extern_kernels.mm(buf23, reinterpret_tensor(primals_4, (4, 16), (1, 4), 0), out=buf25) buf26 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf27 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf28 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf30 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_add_mul_sigmoid_tanh_3[grid(16)](buf24, primals_3, buf25, primals_5, buf22, buf26, buf27, buf28, buf30, 16, XBLOCK =16, num_warps=1, num_stages=1) buf29 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_sigmoid_4[grid(16)](buf24, primals_3, buf25, primals_5, buf29, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf24 del primals_3 del primals_5 buf31 = reinterpret_tensor(buf25, (16, 4), (4, 1), 0) del buf25 triton_poi_fused_stack_5[grid(64)](buf7, buf15, buf23, buf29, buf30, buf31, 64, XBLOCK=64, num_warps=1, num_stages=1) return (reinterpret_tensor(buf31, (4, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf0, (4, 4), (4, 1), 0), reinterpret_tensor( primals_1, (4, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (4, 1), 16), reinterpret_tensor(primals_1, (4, 4), (4, 1), 32), reinterpret_tensor(primals_1, (4, 4), (4, 1), 48), buf3, buf4, buf5, buf6, buf7, buf10, buf11, buf12, buf13, buf14, buf15, buf18, buf19, buf20, buf21, buf22, buf23, buf26, buf27, buf28, buf29, buf30, primals_4, buf32) def _compute_last_states(h_n: 'List[torch.Tensor]', c_n: 'List[torch.Tensor]', seq_lengths: 'List[int]') ->Tuple[torch.Tensor, torch.Tensor]: """ Given h and c values of all time steps, this function computes the h and c values for each sequence at their last timestep (this can vary across sequences with different sequence lengths). Args: h_n: A list of hidden state values across all timesteps. c_n: A list of cell state values across all timesteps. seq_lengths: the length parameter used in the torch.nn.utils.rnn.packed_padded_sequence function to create a PackedSequence. This can be computed using the _compute_seq_lengths function. Returns: h_last: Contains the last hidden state values for each of the sequences. If the i'th sequence has a length of l_i, then h_last[i,:] contains the hidden state corresponding to the i'th sequence at timestep l_i. c_last: The structure is the same as h_last, except that it contains the last cell state values for each of the sequences. """ max_batch_size = len(seq_lengths) hidden_size = h_n[0].shape[-1] h_last = torch.zeros(max_batch_size, hidden_size) c_last = torch.zeros(max_batch_size, hidden_size) for i, seq_len in enumerate(seq_lengths): h_last[i, :] = h_n[seq_len - 1][i, :] c_last[i, :] = c_n[seq_len - 1][i, :] return h_last, c_last def _compute_seq_lengths(batch_sizes: 'torch.Tensor') ->List[int]: """ Computes the sequence lengths (the length parameter used in the packed_padded_sequence function to create a PackedSequence). Args: batch_sizes: Contains the batch sizes as stored in a PackedSequence Returns: running_seq_lengths: the length parameter used in the torch.nn.utils.rnn.packed_padded_sequence function to create a PackedSequence. It's a list of the same length as batch_sizes. """ max_batch_size = batch_sizes[0] if len(batch_sizes) == 1: return [1] * max_batch_size running_seq = 0 running_seq_lengths = [] for i in range(1, len(batch_sizes)): delta = batch_sizes[i - 1].item() - batch_sizes[i].item() running_seq += 1 running_seq_lengths += delta * [running_seq] running_seq += 1 running_seq_lengths += batch_sizes[-1].item() * [running_seq] running_seq_lengths.reverse() return running_seq_lengths def _concat_sequence_directions(forward: 'Union[List[torch.Tensor], Tuple[torch.Tensor]]', reverse: 'Union[List[torch.Tensor], Tuple[torch.Tensor]]', dim: 'int') ->Tuple[torch .Tensor]: """ Given two list/tuple of same length containing tensors, this function returns a concatenation along dimension d. So, output[i] : concatenation of forward[i] and reverse[i] along dimension dim. forward[i] and reverse[i] should have the same shape. This function is used for concatenating the outputs of the forward and reverse layer of a bidirectional LSTM. Args: forward: list/tuple containing n tensors, representing the output of the forward layer. reverse: list/tuple containing n tensors, representing the output of the backward layer. dim: the dimension along which the sequence of tensors within forward and reverse will be concatenated. Returns: output: list/tuple containing n concatenated tensors. """ if len(forward) != len(reverse): raise ValueError( 'The forward and reverse layer output sequences should have the same length' ) seq_length = len(forward) output = [0] * seq_length for i in range(seq_length): output[i] = torch.cat((forward[i], reverse[i]), dim=dim) return output def filter_out_old_keys(self, state_dict, prefix, local_metadata): new_state_dict = {param_name: param_value for param_name, param_value in state_dict.items() if param_name not in self.old_to_new} return new_state_dict class LSTMLinear(nn.Linear): """ This function is the same as a nn.Linear layer, except that in the backward pass the grad_samples get accumulated (instead of being concatenated as in the standard nn.Linear) """ def __init__(self, in_features: 'int', out_features: 'int', bias: 'bool'=True): super().__init__(in_features, out_features, bias) class DPLSTMCell(nn.Module): """ Internal-only class. Implements *one* step of LSTM so that a LSTM layer can be seen as repeated applications of this class. """ def __init__(self, input_size: 'int', hidden_size: 'int', bias: 'bool'): super().__init__() self.input_size = input_size self.hidden_size = hidden_size self.bias = bias self.ih = LSTMLinear(input_size, 4 * hidden_size, bias=self.bias) self.hh = LSTMLinear(hidden_size, 4 * hidden_size, bias=self.bias) self.reset_parameters() def reset_parameters(self): """ Resets parameters by initializing them from an uniform distribution. """ stdv = 1.0 / math.sqrt(self.hidden_size) for weight in self.parameters(): nn.init.uniform_(weight, -stdv, stdv) def set_max_batch_length(self, max_batch_length: 'int') ->None: """ Sets max batch length """ self.ih.max_batch_len = max_batch_length self.hh.max_batch_len = max_batch_length def forward(self, x: 'torch.Tensor', h_prev: 'torch.Tensor', c_prev: 'torch.Tensor', batch_size_t: 'Optional[int]'=None) ->Tuple[torch. Tensor, torch.Tensor]: if batch_size_t is None: gates = self.ih(x) + self.hh(h_prev) else: gates = self.ih(x) + self.hh(h_prev[:batch_size_t, :]) i_t_input, f_t_input, g_t_input, o_t_input = torch.split(gates, self.hidden_size, 1) i_t = torch.sigmoid(i_t_input) f_t = torch.sigmoid(f_t_input) g_t = torch.tanh(g_t_input) o_t = torch.sigmoid(o_t_input) if batch_size_t is None: c_t = f_t * c_prev + i_t * g_t else: c_t = f_t * c_prev[:batch_size_t, :] + i_t * g_t h_t = o_t * torch.tanh(c_t) return h_t, c_t class DPLSTMLayer(nn.Module): """ Implements *one* layer of LSTM in a way amenable to differential privacy. We don't expect you to use this directly: use DPLSTM instead :) """ def __init__(self, input_size: 'int', hidden_size: 'int', bias: 'bool', dropout: 'float', reverse: 'bool'=False): super().__init__() self.input_size = input_size self.hidden_size = hidden_size self.bias = bias self.dropout = dropout self.reverse = reverse self.cell = DPLSTMCell(input_size=input_size, hidden_size= hidden_size, bias=bias) self.dropout_layer = nn.Dropout(dropout) if dropout > 0 else None def set_max_batch_length(self, max_batch_length: 'int') ->None: """ Sets max batch length. Useful for PackedSequences """ self.cell.set_max_batch_length(max_batch_length) def forward(self, x: 'Union[torch.Tensor, Tuple]', state_init: 'Tuple[torch.Tensor, torch.Tensor]', batch_sizes: 'Optional[torch.Tensor]'=None) ->Tuple[torch.Tensor, Tuple[torch. Tensor, torch.Tensor]]: """ Implements the forward pass of the DPLSTMLayer when a sequence is given in input. Args: x: Input sequence to the DPLSTMCell of shape ``[T, B, D]``. state_init: Initial state of the LSTMCell as a tuple ``(h_0, c_0)`` where ``h_0`` is the initial hidden state and ``c_0`` is the initial cell state of the DPLSTMCell batch_sizes: Contains the batch sizes as stored in PackedSequence Returns: ``output, (h_n, c_n)`` where, ``output`` is of shape ``[T, B, H]`` and is a tensor containing the output features (``h_t``) from the last layer of the DPLSTMCell for each timestep ``t``. ``h_n`` is of shape ``[B, H]`` and is a tensor containing the hidden state for ``t = T``. ``c_n`` is of shape ``[B, H]`` tensor containing the cell state for ``t = T``. """ if batch_sizes is not None: seq_length = batch_sizes.size(0) if self.reverse: x = tuple(reversed(x)) batch_sizes = batch_sizes.flip(0) else: seq_length, _batch_sz, _ = x.shape if self.reverse: x = x.flip(0) x = torch.unbind(x, dim=0) h_0, c_0 = state_init h_n = [h_0] c_n = [c_0] batch_size_prev = h_0.shape[0] for t in range(seq_length): if batch_sizes is not None: batch_size_t = batch_sizes[t].item() delta = batch_size_t - batch_size_prev if delta > 0: h_cat = torch.cat((h_n[t], h_0[batch_size_prev: batch_size_t, :]), 0) c_cat = torch.cat((c_n[t], c_0[batch_size_prev: batch_size_t, :]), 0) h_next, c_next = self.cell(x[t], h_cat, c_cat, batch_size_t ) else: h_next, c_next = self.cell(x[t], h_n[t], c_n[t], batch_size_t) else: h_next, c_next = self.cell(x[t], h_n[t], c_n[t]) if self.dropout: h_next = self.dropout_layer(h_next) h_n.append(h_next) c_n.append(c_next) batch_size_prev = h_next.shape[0] if batch_sizes is None: h_n = torch.stack(h_n[1:], dim=0) return h_n.flip(0) if self.reverse else h_n, (h_n[-1], c_n[-1]) else: seq_lengths = _compute_seq_lengths(batch_sizes) h_temp, c_temp = h_n[1:], c_n[1:] h_last, c_last = _compute_last_states(h_temp, c_temp, seq_lengths) if self.reverse: h_temp = tuple(reversed(h_temp)) return h_temp, (h_last, c_last) class BidirectionalDPLSTMLayer(nn.Module): """ Implements *one* layer of Bidirectional LSTM in a way amenable to differential privacy. We don't expect you to use this directly: use DPLSTM instead :) """ def __init__(self, input_size: 'int', hidden_size: 'int', bias: 'bool', dropout: 'float'): super().__init__() self.input_size = input_size self.hidden_size = hidden_size self.bias = bias self.dropout = dropout self.forward_layer = DPLSTMLayer(input_size=input_size, hidden_size =hidden_size, bias=bias, dropout=dropout, reverse=False) self.reverse_layer = DPLSTMLayer(input_size=input_size, hidden_size =hidden_size, bias=bias, dropout=dropout, reverse=True) def set_max_batch_length(self, max_batch_length: 'int') ->None: """ Sets max batch length """ self.forward_layer.set_max_batch_length(max_batch_length) self.reverse_layer.set_max_batch_length(max_batch_length) def forward(self, x: 'torch.Tensor', state_init: 'Tuple[torch.Tensor, torch.Tensor]', batch_sizes: 'Optional[torch.Tensor]'=None) ->Tuple[torch.Tensor, Tuple[torch. Tensor, torch.Tensor]]: """ Implements the forward pass of the DPLSTM when a sequence is input. Dimensions as follows: - B: Batch size - T: Sequence length - D: LSTM input hidden size (eg from a word embedding) - H: LSTM output hidden size - P: number of directions (2 if bidirectional, else 1) Args: x: Input sequence to the DPLSTM of shape ``[T, B, D]`` state_init: Initial state of the LSTM as a tuple ``(h_0, c_0)``, where ``h_0`` of shape ``[P, B, H]`` contains the initial hidden state, and ``c_0`` of shape ``[P, B, H]`` contains the initial cell state. This argument can be (and defaults to) None, in which case zero tensors will be used. Returns: ``output, (h_n, c_n)`` where, ``output`` is of shape ``[T, B, H * P]`` and is a tensor containing the output features (``h_t``) from the last layer of the DPLSTM for each timestep ``t``. ``h_n`` is of shape ``[P, B, H]`` and contains the hidden state for ``t = T``. ``c_n`` is of shape ``[P, B, H]`` and contains the cell state for ``t = T``. """ h0, c0 = state_init h0_f, h0_r = h0.unbind(0) c0_f, c0_r = c0.unbind(0) out_f, (h_f, c_f) = self.forward_layer(x, (h0_f, c0_f), batch_sizes) out_r, (h_r, c_r) = self.reverse_layer(x, (h0_r, c0_r), batch_sizes) if batch_sizes is None: out = torch.cat([out_f, out_r], dim=-1) else: out = _concat_sequence_directions(out_f, out_r, -1) h = torch.stack([h_f, h_r], dim=0) c = torch.stack([c_f, c_r], dim=0) return out, (h, c) class ParamRenamedModule(nn.Module): """ This class defines a nn.Module whose parameters are renamed. This is useful when you want to reimplement a layer but make sure its state_dict and list of parameters are exactly the same as another reference layer so that you can have a drop-in replacement that does not depend on how your layer is actually implemented. In Opacus, this is used for DPLSTM, where our implementation leverages submodules and requires alignment to the state_dict of nn.LSTM. """ def __init__(self, rename_map: 'Dict[str, str]'): """ Initializes internal state. Subclass this instead of ``torch.nn.Module`` whenever you need to rename your model's state. Args: rename_map: mapping from old name -> new name for each parameter you want renamed. Note that this must be a 1:1 mapping! """ super().__init__() self.old_to_new = rename_map self.new_to_old = {v: k for k, v in rename_map.items()} self._register_state_dict_hook(filter_out_old_keys) def _register_renamed_parameters(self): """ Internal function. This function simply registers parameters under their new name. They will automatically mask their duplicates coming from submodules. This trick works because self.parameters() proceeds recursively from the top, going into submodules after processing items at the current level, and will not return duplicates. """ for old_name, param in super().named_parameters(): if old_name in self.old_to_new: new_name = self.old_to_new[old_name] self.register_parameter(new_name, param) def __setattr__(self, name: 'str', value: 'Union[Tensor, nn.Module]' ) ->None: """ Whenever you set an attribute, eg `self.linear`, this is called to actually register it in any nn.Module. We rely on the masking trick explained in the docs for ``_register_renamed_parameters`` to make sure we replace things only once. If a new parameter in the rename list is detected, we rename and mask it so next time this is called we will no longer find it. """ super().__setattr__(name, value) try: self._register_renamed_parameters() except AttributeError: pass def load_state_dict(self, state_dict: 'Dict[str, Tensor]', strict: 'bool'=True): """ Identical to ``torch.nn.Module.load_state_dict()`` but handles the renamed keys. """ missing_keys, unexpected_keys = super().load_state_dict(state_dict, strict=False) missing_keys = [k for k in missing_keys if k not in self.old_to_new] if strict: error_msgs = [] if len(unexpected_keys) > 0: error_msgs.insert(0, 'Unexpected key(s) in state_dict: {}. '.format(', '. join('"{}"'.format(k) for k in unexpected_keys))) if len(missing_keys) > 0: error_msgs.insert(0, 'Missing key(s) in state_dict: {}. '. format(', '.join('"{}"'.format(k) for k in missing_keys))) if len(error_msgs) > 0: raise RuntimeError( 'Error(s) in loading state_dict for {}:\n\t{}'.format( self.__class__.__name__, '\n\t'.join(error_msgs))) return _IncompatibleKeys(missing_keys, unexpected_keys) class DPLSTM(ParamRenamedModule): """ DP-friendly drop-in replacement of the ``torch.nn.LSTM`` module. Its state_dict matches that of nn.LSTM exactly, so that after training it can be exported and loaded by an nn.LSTM for inference. Refer to nn.LSTM's documentation for all parameters and inputs. """ def __init__(self, input_size: 'int', hidden_size: 'int', num_layers: 'int'=1, bias: 'bool'=True, batch_first: 'bool'=False, dropout: 'float'=0, bidirectional: 'bool'=False): rename_dict = self._make_rename_dict(num_layers, bias, bidirectional) super().__init__(rename_dict) self.input_size = input_size self.hidden_size = hidden_size self.num_layers = num_layers self.bias = bias self.batch_first = batch_first self.dropout = dropout self.bidirectional = bidirectional self.num_directions = 2 if self.bidirectional else 1 LayerClass = BidirectionalDPLSTMLayer if bidirectional else DPLSTMLayer self.layers = nn.ModuleList([LayerClass(input_size=self.input_size if i == 0 else self.hidden_size * self.num_directions, hidden_size =self.hidden_size, bias=self.bias, dropout=self.dropout if i < self.num_layers - 1 else 0) for i in range(num_layers)]) def forward(self, x: 'Union[torch.Tensor, PackedSequence]', state_init: 'Optional[Tuple[torch.Tensor, torch.Tensor]]'=None) ->Tuple[torch. Tensor, Tuple[torch.Tensor, torch.Tensor]]: """ Implements the forward pass of the DPLSTM when a sequence is input. Dimensions as follows: - B: Batch size - T: Sequence length - D: LSTM input hidden size (eg from a word embedding) - H: LSTM output hidden size - L: number of layers in the LSTM - P: number of directions (2 if bidirectional, else 1) Args: x: Input sequence to the DPLSTM of shape ``[T, B, D]``. Or it can be a PackedSequence. state_init: Initial state of the LSTM as a tuple ``(h_0, c_0)``, where: - ``h_0`` of shape ``[L*P, B, H]`` contains the initial hidden state - ``c_0`` of shape ``[L*P, B, H]`` contains the initial cell state This argument can be (and defaults to) None, in which case zero tensors will be used. Returns: ``output, (h_n, c_n)`` where, ``output`` is of shape ``[T, B, H * P]`` and is a tensor containing the output features (``h_t``) from the last layer of the DPLSTM for each timestep ``t``. ``h_n`` is of shape ``[L * P, B, H]`` and contains the hidden state for ``t = T``. ``c_n`` is of shape ``[L * P, B, H]`` and contains the cell state for ``t = T``. """ if isinstance(x, PackedSequence): x, batch_sizes, sorted_indices, unsorted_indices = x B = batch_sizes[0].item() _, _D = x.shape x = x.split(tuple(batch_sizes)) for layer in self.layers: layer.set_max_batch_length(B) else: sorted_indices = None unsorted_indices = None batch_sizes = None x = self._rearrange_batch_dim(x) _T, B, _D = x.shape L = self.num_layers P = 2 if self.bidirectional else 1 H = self.hidden_size h_0s, c_0s = state_init or (None, None) if h_0s is None: h_0s = torch.zeros(L, P, B, self.hidden_size, dtype=x[0].dtype, device=x[0].device) else: h_0s = h_0s.reshape([L, P, B, H]) h_0s = self._permute_hidden(h_0s, sorted_indices, 2) if c_0s is None: c_0s = torch.zeros(L, P, B, self.hidden_size, dtype=x[0].dtype, device=x[0].device) else: c_0s = c_0s.reshape([L, P, B, H]) c_0s = self._permute_hidden(c_0s, sorted_indices, 2) hs: 'List[torch.Tensor]' = [] cs: 'List[torch.Tensor]' = [] for layer, h0, c0 in zip(self.layers, h_0s, c_0s): if not self.bidirectional: h0 = h0.squeeze(0) c0 = c0.squeeze(0) x, (h, c) = layer(x, (h0, c0), batch_sizes) if not self.bidirectional: h = h.unsqueeze(0) c = c.unsqueeze(0) hs.append(h) cs.append(c) hs = torch.cat(hs, dim=0) cs = torch.cat(cs, dim=0) if batch_sizes is not None: seq_lengths = _compute_seq_lengths(batch_sizes) packed_data = pack_padded_sequence(pad_sequence(x, batch_first= False), seq_lengths, batch_first=True)[0] out = PackedSequence(packed_data, batch_sizes, sorted_indices, unsorted_indices) else: out = self._rearrange_batch_dim(x) return out, (self._permute_hidden(hs, unsorted_indices), self. _permute_hidden(cs, unsorted_indices)) def _permute_hidden(self, x: 'torch.Tensor', permutation: 'Optional[torch.Tensor]'=None, dim: 'int'=1) ->torch.Tensor: if permutation is None: return x if dim == 1: return x[:, permutation, :] elif dim == 2: return x[:, :, permutation, :] def _rearrange_batch_dim(self, x: 'torch.Tensor') ->torch.Tensor: if self.batch_first: x = x.transpose(0, 1) return x def __repr__(self): s = f'DPLSTM({self.input_size}, {self.hidden_size}, bias={self.bias}' if self.batch_first: s += f', batch_first={self.batch_first}' if self.num_layers > 1: s += f', num_layers={self.num_layers}' if self.dropout: s += f', dropout={self.dropout}' if self.bidirectional: s += f', bidirectional={self.bidirectional}' return s def _make_rename_dict(self, num_layers, bias, bidirectional): """ Programmatically constructs a dictionary old_name -> new_name to align with the param names used in ``torch.nn.LSTM``. """ d = {} components = ['weight'] + ['bias' if bias else []] matrices = ['ih', 'hh'] for i in range(num_layers): for c in components: for m in matrices: nn_name = f'{c}_{m}_l{i}' if bidirectional: d[f'layers.{i}.forward_layer.cell.{m}.{c}'] = nn_name d[f'layers.{i}.reverse_layer.cell.{m}.{c}' ] = nn_name + '_reverse' else: d[f'layers.{i}.cell.{m}.{c}'] = nn_name return d class DPSLTMAdapterNew(nn.Module): """ Adapter for DPLSTM. LSTM returns a tuple, but our testing tools need the model to return a single tensor in output. We do this adaption here. """ def __init__(self, *args, **kwargs): super().__init__() self.dplstm = DPLSTM(*args, **kwargs) def forward(self, input_0): primals_2 = self.dplstm.weight_ih_l0 primals_3 = self.dplstm.bias_ih_l0 primals_4 = self.dplstm.weight_hh_l0 primals_5 = self.dplstm.bias_hh_l0 primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
ffuuugor/opacus
DPSLTMAdapter
false
6,726
[ "Apache-2.0" ]
1
2048a6e92902685c2a735e9fb7c0d48b4846b494
https://github.com/ffuuugor/opacus/tree/2048a6e92902685c2a735e9fb7c0d48b4846b494
Net16
import torch import torch.nn as nn import torch.nn.functional as F class Net16(nn.Module): def __init__(self, input_dim, output_dim): super(Net16, self).__init__() self.linear1 = nn.Linear(input_dim, 16) self.linear2 = nn.Linear(16, output_dim) def forward(self, x): x = F.relu(self.linear1(x)) x = self.linear2(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'output_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 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 = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 16 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, (16, 4), (4, 1)) assert_size_stride(primals_2, (16,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 16), (16, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 16), (16, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 16), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 16), (256, 64, 16, 1), 0) del buf0 buf3 = empty_strided_cuda((4, 4, 4, 16), (256, 64, 16, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(1024)](buf1, primals_2, buf3, 1024, XBLOCK=128, 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, 16), (16, 1), 0), reinterpret_tensor(primals_4, (16, 4), (1, 16), 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, 16), (16, 1), 0), primals_4, buf3 class Net16New(nn.Module): def __init__(self, input_dim, output_dim): super(Net16New, self).__init__() self.linear1 = nn.Linear(input_dim, 16) self.linear2 = nn.Linear(16, output_dim) def forward(self, input_0): primals_1 = self.linear1.weight primals_2 = self.linear1.bias primals_4 = self.linear2.weight primals_5 = self.linear2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
gautam-sharma1/openRL
Net16
false
6,727
[ "MIT" ]
1
14310a97a328fe5682a01ee85d83a6b5e1ae29ca
https://github.com/gautam-sharma1/openRL/tree/14310a97a328fe5682a01ee85d83a6b5e1ae29ca
MLP
import torch import torch.nn as nn import torch.utils.data class MLP(nn.Module): def __init__(self, input_size, output_size, hidden_size=500, weight_decay=0.0): super(MLP, self).__init__() self.i2h = nn.Linear(in_features=input_size, out_features=hidden_size) self.Dropout = nn.Dropout(p=0.5) self.h2o = nn.Linear(in_features=hidden_size, out_features=output_size) self.loss = nn.functional.cross_entropy self.weight_decay = weight_decay def forward(self, x): return self.h2o(torch.relu(self.Dropout(self.i2h(x)))) def predict(self, x): return self.forward(x).argmax(dim=1) 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.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_relu_threshold_backward_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 32000 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex x0 = xindex % 500 x2 = xindex // 2000 x3 = xindex % 2000 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 + 2016 * x2), tmp4, xmask) tl.store(out_ptr1 + (x3 + 2048 * x2), tmp6, xmask) @triton.jit def triton_poi_fused_relu_view_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 32000 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 500 x1 = xindex // 500 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 500 * (x1 % 4) + 2016 * (x1 // 4)), xmask) tl.store(out_ptr0 + x2, tmp0, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (500, 4), (4, 1)) assert_size_stride(primals_2, (500,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 500), (500, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 500), (500, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 500), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 4, 4, 500), (8064, 2016, 500, 1), torch.float32) buf4 = empty_strided_cuda((4, 4, 4, 500), (8192, 2048, 500, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(32000)](buf0, primals_2, buf1, buf4, 32000, XBLOCK=256, num_warps=4, num_stages=1 ) del primals_2 buf2 = buf0 del buf0 triton_poi_fused_relu_view_1[grid(32000)](buf1, buf2, 32000, XBLOCK =256, num_warps=4, num_stages=1) del buf1 buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, buf2, reinterpret_tensor(primals_4, (500, 4), (1, 500), 0), alpha=1, beta=1, out=buf3) del primals_5 return reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf2, primals_4, buf4 class MLPNew(nn.Module): def __init__(self, input_size, output_size, hidden_size=500, weight_decay=0.0): super(MLPNew, self).__init__() self.i2h = nn.Linear(in_features=input_size, out_features=hidden_size) self.Dropout = nn.Dropout(p=0.5) self.h2o = nn.Linear(in_features=hidden_size, out_features=output_size) self.loss = nn.functional.cross_entropy self.weight_decay = weight_decay def predict(self, x): return self.forward(x).argmax(dim=1) def forward(self, input_0): primals_1 = self.i2h.weight primals_2 = self.i2h.bias primals_4 = self.h2o.weight primals_5 = self.h2o.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
gchrupala/platalea
MLP
false
6,728
[ "Apache-2.0" ]
1
65833307bb6c5ad6cbdd6b17ad8ca59cf51fcd81
https://github.com/gchrupala/platalea/tree/65833307bb6c5ad6cbdd6b17ad8ca59cf51fcd81
MeanPool
import torch import torch.nn as nn import torch.utils.data class MeanPool(nn.Module): def __init__(self): super(MeanPool, self).__init__() def forward(self, input): x = input.mean(dim=1) 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 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 @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) 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), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mean_0[grid(64)](arg0_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 return buf0, class MeanPoolNew(nn.Module): def __init__(self): super(MeanPoolNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
gchrupala/platalea
MeanPool
false
6,729
[ "Apache-2.0" ]
1
65833307bb6c5ad6cbdd6b17ad8ca59cf51fcd81
https://github.com/gchrupala/platalea/tree/65833307bb6c5ad6cbdd6b17ad8ca59cf51fcd81
NNet
import torch import torch.nn as nn import torch.nn.functional as F class NNet(nn.Module): def __init__(self, input_dim, output_dim): super(NNet, self).__init__() self.linear1 = nn.Linear(input_dim, 64) self.linear2 = nn.Linear(64, 256) self.linear3 = nn.Linear(256, output_dim) def forward(self, x): x = F.relu(self.linear1(x)) x = F.relu(self.linear2(x)) x = self.linear3(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'output_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 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): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 64 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) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) @triton.jit def triton_poi_fused_relu_threshold_backward_1(in_out_ptr0, in_ptr0, out_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) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) 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, (64, 4), (4, 1)) assert_size_stride(primals_2, (64,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (256, 64), (64, 1)) assert_size_stride(primals_5, (256,), (1,)) assert_size_stride(primals_6, (4, 256), (256, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 64), (64, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 64), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 64), (1024, 256, 64, 1), 0) del buf0 buf6 = empty_strided_cuda((4, 4, 4, 64), (1024, 256, 64, 1), torch.bool ) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(4096)](buf1, primals_2, buf6, 4096, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 256), (256, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 64), (64, 1), 0), reinterpret_tensor(primals_4, (64, 256), (1, 64), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 256), (4096, 1024, 256, 1), 0 ) del buf2 buf5 = empty_strided_cuda((4, 4, 4, 256), (4096, 1024, 256, 1), torch.bool) triton_poi_fused_relu_threshold_backward_1[grid(16384)](buf3, primals_5, buf5, 16384, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 256), (256, 1), 0), reinterpret_tensor(primals_6, (256, 4), (1, 256), 0), alpha=1, beta=1, out=buf4) del primals_7 return reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 64), (64, 1), 0), reinterpret_tensor( buf3, (64, 256), (256, 1), 0), primals_6, buf5, primals_4, buf6 class NNetNew(nn.Module): def __init__(self, input_dim, output_dim): super(NNetNew, self).__init__() self.linear1 = nn.Linear(input_dim, 64) self.linear2 = nn.Linear(64, 256) self.linear3 = nn.Linear(256, output_dim) def forward(self, input_0): primals_1 = self.linear1.weight primals_2 = self.linear1.bias primals_4 = self.linear2.weight primals_5 = self.linear2.bias primals_6 = self.linear3.weight primals_7 = self.linear3.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
gautam-sharma1/openRL
NNet
false
6,730
[ "MIT" ]
1
14310a97a328fe5682a01ee85d83a6b5e1ae29ca
https://github.com/gautam-sharma1/openRL/tree/14310a97a328fe5682a01ee85d83a6b5e1ae29ca
ELBO
import torch import torch.nn.functional import torch.nn as nn class ELBO(nn.Module): def __init__(self, train_size, loss_function=nn.MSELoss()): """ Quantify the Evidence Lower Bound (ELBO) and provide the total loss. """ super(ELBO, self).__init__() self.train_size = train_size self.loss_function = loss_function def forward(self, input, target, kl, kl_weight=1.0): """ Kullback-Leibler divergence. This comes from the euqation (4) in Shridhar et. al. 2019, which consists of likelihood cost (is dependent on data) and complexity cost (id dependent on distribution). """ assert not target.requires_grad likelihood_cost = self.loss_function(input, target) complexity_cost = kl_weight * kl total_loss = likelihood_cost + complexity_cost return total_loss, likelihood_cost, complexity_cost 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 [[], {'train_size': False}]
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.functional 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_mse_loss_mul_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_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) tmp9 = tl.load(in_ptr2 + 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 = 256.0 tmp8 = tmp6 / tmp7 tmp10 = 1.0 tmp11 = tmp9 * tmp10 tmp12 = tmp8 + tmp11 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp8, None) tl.store(out_ptr0 + tl.broadcast_to(r0, [RBLOCK]), tmp11, None) tl.store(out_ptr1 + tl.broadcast_to(r0, [RBLOCK]), tmp12, 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((), (), torch.float32) buf1 = buf0 del buf0 buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_per_fused_add_mse_loss_mul_0[grid(1)](buf1, arg1_1, arg0_1, arg2_1, buf2, buf3, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 del arg2_1 return buf3, buf1, buf2 class ELBONew(nn.Module): def __init__(self, train_size, loss_function=nn.MSELoss()): """ Quantify the Evidence Lower Bound (ELBO) and provide the total loss. """ super(ELBONew, self).__init__() self.train_size = train_size self.loss_function = loss_function 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], output[2]
geek-yang/NEmo
ELBO
false
6,731
[ "Apache-2.0" ]
1
4f310535c4865f3816155b99b4a2bbb891672cc9
https://github.com/geek-yang/NEmo/tree/4f310535c4865f3816155b99b4a2bbb891672cc9
SCNLayer
import torch import torch.nn as nn def chebyshev(L, X, k=3): if k == 1: return torch.sparse.mm(L, X) dp = [X, torch.sparse.mm(L, X)] for i in range(2, k): nxt = 2 * torch.sparse.mm(L, dp[i - 1]) dp.append(torch.sparse.FloatTensor.add(nxt, -dp[i - 2])) return torch.cat(dp, dim=1) class SCNLayer(nn.Module): def __init__(self, feature_size, output_size, enable_bias=True, k=1): super().__init__() self.k = k self.conv = nn.Linear(k * feature_size, output_size, bias=enable_bias) def forward(self, L, x): X = chebyshev(L, x, self.k) return self.conv(X) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'feature_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 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_zeros_0(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 = 0.0 tl.store(out_ptr0 + x0, 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, 4), (4, 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) get_raw_stream(0) triton_poi_fused_zeros_0[grid(16)](buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) buf1 = torch.ops.aten._sparse_addmm.default(reinterpret_tensor(buf0, (4, 4), (1, 4), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4 ), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), beta=0) del primals_1 del primals_2 buf2 = buf1 del buf1 buf3 = buf0 del buf0 extern_kernels.addmm(primals_4, reinterpret_tensor(buf2, (4, 4), (1, 4), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), alpha =1, beta=1, out=buf3) del primals_3 del primals_4 return buf3, reinterpret_tensor(buf2, (4, 4), (1, 4), 0) def chebyshev(L, X, k=3): if k == 1: return torch.sparse.mm(L, X) dp = [X, torch.sparse.mm(L, X)] for i in range(2, k): nxt = 2 * torch.sparse.mm(L, dp[i - 1]) dp.append(torch.sparse.FloatTensor.add(nxt, -dp[i - 2])) return torch.cat(dp, dim=1) class SCNLayerNew(nn.Module): def __init__(self, feature_size, output_size, enable_bias=True, k=1): super().__init__() self.k = k self.conv = nn.Linear(k * feature_size, output_size, bias=enable_bias) def forward(self, input_0, input_1): primals_1 = self.conv.weight primals_4 = self.conv.bias primals_2 = input_0 primals_3 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
ggoh29/Simplicial-neural-network-benchmark
SCNLayer
false
6,733
[ "MIT" ]
1
9a12bcd054251790d85e3971f5473dcffaa5664b
https://github.com/ggoh29/Simplicial-neural-network-benchmark/tree/9a12bcd054251790d85e3971f5473dcffaa5664b
Temp
import torch import torch.nn as nn import torch.nn.functional as F class Temp(nn.Module): def __init__(self, input_dim, output_dim): super(Temp, self).__init__() self.linear1 = nn.Linear(input_dim, 256) self.linear2 = nn.Linear(256, 256) self.linear3 = nn.Linear(256, 256) self.linear4 = nn.Linear(256, output_dim) def forward(self, x): x = F.leaky_relu(self.linear1(x)) x = F.sigmoid(self.linear2(x)) x = F.leaky_relu(self.linear3(x)) x = self.linear4(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'output_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 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_leaky_relu_0(in_ptr0, in_ptr1, 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) x2 = xindex x0 = xindex % 256 tmp0 = tl.load(in_ptr0 + x2, None) tmp1 = tl.load(in_ptr1 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x2, tmp4, None) tl.store(out_ptr1 + x2, tmp7, None) @triton.jit def triton_poi_fused_sigmoid_1(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.sigmoid(tmp2) tl.store(in_out_ptr0 + x2, tmp3, None) 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, (256, 4), (4, 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), (256, 1)) assert_size_stride(primals_5, (256,), (1,)) assert_size_stride(primals_6, (256, 256), (256, 1)) assert_size_stride(primals_7, (256,), (1,)) assert_size_stride(primals_8, (4, 256), (256, 1)) assert_size_stride(primals_9, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 256), (256, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 256), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 4, 4, 256), (4096, 1024, 256, 1), torch.bool) buf2 = empty_strided_cuda((4, 4, 4, 256), (4096, 1024, 256, 1), torch.float32) get_raw_stream(0) triton_poi_fused_leaky_relu_0[grid(16384)](buf0, primals_2, buf1, buf2, 16384, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf3 = buf0 del buf0 extern_kernels.mm(reinterpret_tensor(buf2, (64, 256), (256, 1), 0), reinterpret_tensor(primals_4, (256, 256), (1, 256), 0), out=buf3) buf4 = reinterpret_tensor(buf3, (4, 4, 4, 256), (4096, 1024, 256, 1), 0 ) del buf3 triton_poi_fused_sigmoid_1[grid(16384)](buf4, primals_5, 16384, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf5 = empty_strided_cuda((64, 256), (256, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf4, (64, 256), (256, 1), 0), reinterpret_tensor(primals_6, (256, 256), (1, 256), 0), out=buf5) buf6 = empty_strided_cuda((4, 4, 4, 256), (4096, 1024, 256, 1), torch.bool) buf7 = empty_strided_cuda((4, 4, 4, 256), (4096, 1024, 256, 1), torch.float32) triton_poi_fused_leaky_relu_0[grid(16384)](buf5, primals_7, buf6, buf7, 16384, XBLOCK=256, num_warps=4, num_stages=1) del buf5 del primals_7 buf8 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_9, reinterpret_tensor(buf7, (64, 256), (256, 1), 0), reinterpret_tensor(primals_8, (256, 4), (1, 256), 0), alpha=1, beta=1, out=buf8) del primals_9 return reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf1, reinterpret_tensor(buf2, (64, 256), (256, 1), 0 ), buf4, buf6, reinterpret_tensor(buf7, (64, 256), (256, 1), 0 ), primals_8, primals_6, primals_4 class TempNew(nn.Module): def __init__(self, input_dim, output_dim): super(TempNew, self).__init__() self.linear1 = nn.Linear(input_dim, 256) self.linear2 = nn.Linear(256, 256) self.linear3 = nn.Linear(256, 256) self.linear4 = nn.Linear(256, output_dim) def forward(self, input_0): primals_1 = self.linear1.weight primals_2 = self.linear1.bias primals_4 = self.linear2.weight primals_5 = self.linear2.bias primals_6 = self.linear3.weight primals_7 = self.linear3.bias primals_8 = self.linear4.weight primals_9 = self.linear4.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]
gautam-sharma1/openRL
Temp
false
6,734
[ "MIT" ]
1
14310a97a328fe5682a01ee85d83a6b5e1ae29ca
https://github.com/gautam-sharma1/openRL/tree/14310a97a328fe5682a01ee85d83a6b5e1ae29ca
GroupNorm
import torch import torch.nn as nn class GroupNorm(nn.Module): def __init__(self, c_num, group_num=16, eps=1e-10): """ The groupnorm layer from https://arxiv.org/abs/1803.08494 Args: c_num (int): Number of input channels group_num (int): Number of group by which to divide the channels eps (float): Epsilon """ super(GroupNorm, self).__init__() self.group_num = group_num self.gamma = nn.Parameter(torch.ones(c_num, 1, 1)) self.beta = nn.Parameter(torch.zeros(c_num, 1, 1)) self.eps = eps def forward(self, x): batch_size, channels, height, width = x.size() x = x.view(batch_size, self.group_num, -1) mean = x.mean(dim=2, keepdim=True) std = x.std(dim=2, keepdim=True) x = (x - mean) / (std + self.eps) x = x.view(batch_size, channels, height, width) return x * self.gamma + self.beta def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'c_num': 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.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 @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 x4 = xindex x5 = xindex // 4 x2 = xindex // 16 % 4 tmp0 = tl.load(in_ptr0 + x4, xmask) tmp1 = tl.load(in_ptr0 + 4 * x5, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x5), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x5), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x5), xmask, eviction_policy='evict_last') tmp28 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last') tmp30 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = 4.0 tmp9 = tmp7 / tmp8 tmp10 = tmp0 - tmp9 tmp11 = tmp1 - tmp9 tmp12 = tmp11 * tmp11 tmp13 = tmp2 - tmp9 tmp14 = tmp13 * tmp13 tmp15 = tmp12 + tmp14 tmp16 = tmp4 - tmp9 tmp17 = tmp16 * tmp16 tmp18 = tmp15 + tmp17 tmp19 = tmp6 - tmp9 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp22 = 3.0 tmp23 = tmp21 / tmp22 tmp24 = libdevice.sqrt(tmp23) tmp25 = 1e-10 tmp26 = tmp24 + tmp25 tmp27 = tmp10 / tmp26 tmp29 = tmp27 * tmp28 tmp31 = tmp29 + tmp30 tl.store(out_ptr0 + x4, tmp31, 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, 1, 1), (1, 1, 1)) assert_size_stride(primals_3, (4, 1, 1), (1, 1, 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_2 del primals_3 return buf0, primals_1 class GroupNormNew(nn.Module): def __init__(self, c_num, group_num=16, eps=1e-10): """ The groupnorm layer from https://arxiv.org/abs/1803.08494 Args: c_num (int): Number of input channels group_num (int): Number of group by which to divide the channels eps (float): Epsilon """ super(GroupNormNew, self).__init__() self.group_num = group_num self.gamma = nn.Parameter(torch.ones(c_num, 1, 1)) self.beta = nn.Parameter(torch.zeros(c_num, 1, 1)) self.eps = eps def forward(self, input_0): primals_2 = self.gamma primals_3 = self.beta primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
generall/Torchlite
GroupNorm
false
6,735
[ "MIT" ]
1
2eb3e2a20b7619bd58b0b0fca120e2aefca0e79a
https://github.com/generall/Torchlite/tree/2eb3e2a20b7619bd58b0b0fca120e2aefca0e79a
LinearAttention
import torch import torch.nn as nn import torch.utils.data class LinearAttention(nn.Module): def __init__(self, in_size): super(LinearAttention, self).__init__() self.out = nn.Linear(in_size, 1) nn.init.orthogonal_(self.out.weight.data) self.softmax = nn.Softmax(dim=1) def forward(self, input): self.alpha = self.softmax(self.out(input)) x = (self.alpha.expand_as(input) * input).sum(dim=1) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_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 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__softmax_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 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_1(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_mul_sum_2(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 x1 = xindex // 4 % 4 x2 = xindex // 16 x3 = xindex % 16 x4 = xindex tmp0 = tl.load(in_ptr0 + (x1 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr1 + (x3 + 64 * x2), xmask) tmp3 = tl.load(in_ptr0 + (4 + x1 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr1 + (16 + x3 + 64 * x2), xmask) tmp7 = tl.load(in_ptr0 + (8 + x1 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp8 = tl.load(in_ptr1 + (32 + x3 + 64 * x2), xmask) tmp11 = tl.load(in_ptr0 + (12 + x1 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp12 = tl.load(in_ptr1 + (48 + x3 + 64 * x2), 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 + x4, tmp14, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (1, 4), (4, 1)) assert_size_stride(primals_2, (1,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 1), (1, 4), 0 ), alpha=1, beta=1, out=buf1) del primals_1 del primals_2 buf2 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) get_raw_stream(0) triton_poi_fused__softmax_0[grid(64)](buf1, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) buf3 = reinterpret_tensor(buf1, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf1 triton_poi_fused__softmax_1[grid(64)](buf2, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) buf4 = reinterpret_tensor(buf2, (4, 4, 4), (16, 4, 1), 0) del buf2 triton_poi_fused_mul_sum_2[grid(64)](buf3, primals_3, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) return buf4, buf3, primals_3, buf3 class LinearAttentionNew(nn.Module): def __init__(self, in_size): super(LinearAttentionNew, self).__init__() self.out = nn.Linear(in_size, 1) nn.init.orthogonal_(self.out.weight.data) self.softmax = nn.Softmax(dim=1) def forward(self, input_0): primals_1 = self.out.weight primals_2 = self.out.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
gchrupala/platalea
LinearAttention
false
6,736
[ "Apache-2.0" ]
1
65833307bb6c5ad6cbdd6b17ad8ca59cf51fcd81
https://github.com/gchrupala/platalea/tree/65833307bb6c5ad6cbdd6b17ad8ca59cf51fcd81
GreedyCTCDecoder
import torch import torch.nn as nn class GreedyCTCDecoder(nn.Module): """ Greedy CTC Decoder """ def __init__(self, **kwargs): nn.Module.__init__(self) def forward(self, log_probs): with torch.no_grad(): argmx = log_probs.argmax(dim=-1, keepdim=False).int() return argmx 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.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_argmax_0(in_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') tmp17 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp32 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 > tmp1 tmp3 = tmp0 == tmp1 tmp4 = tmp0 != tmp0 tmp5 = tmp1 != tmp1 tmp6 = tmp4 > tmp5 tmp7 = tmp2 | tmp6 tmp8 = tmp4 & tmp5 tmp9 = tmp3 | tmp8 tmp10 = tl.full([1], 0, tl.int64) tmp11 = tl.full([1], 1, tl.int64) tmp12 = tmp10 < tmp11 tmp13 = tmp9 & tmp12 tmp14 = tmp7 | tmp13 tmp15 = tl.where(tmp14, tmp0, tmp1) tmp16 = tl.where(tmp14, tmp10, tmp11) tmp18 = tmp15 > tmp17 tmp19 = tmp15 == tmp17 tmp20 = tmp15 != tmp15 tmp21 = tmp17 != tmp17 tmp22 = tmp20 > tmp21 tmp23 = tmp18 | tmp22 tmp24 = tmp20 & tmp21 tmp25 = tmp19 | tmp24 tmp26 = tl.full([1], 2, tl.int64) tmp27 = tmp16 < tmp26 tmp28 = tmp25 & tmp27 tmp29 = tmp23 | tmp28 tmp30 = tl.where(tmp29, tmp15, tmp17) tmp31 = tl.where(tmp29, tmp16, tmp26) tmp33 = tmp30 > tmp32 tmp34 = tmp30 == tmp32 tmp35 = tmp30 != tmp30 tmp36 = tmp32 != tmp32 tmp37 = tmp35 > tmp36 tmp38 = tmp33 | tmp37 tmp39 = tmp35 & tmp36 tmp40 = tmp34 | tmp39 tmp41 = tl.full([1], 3, tl.int64) tmp42 = tmp31 < tmp41 tmp43 = tmp40 & tmp42 tmp44 = tmp38 | tmp43 tl.where(tmp44, tmp30, tmp32) tmp46 = tl.where(tmp44, tmp31, tmp41) tmp47 = tmp46.to(tl.int32) tl.store(out_ptr1 + x0, tmp47, 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) buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.int32) get_raw_stream(0) triton_poi_fused__to_copy_argmax_0[grid(64)](arg0_1, buf1, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 return buf1, class GreedyCTCDecoderNew(nn.Module): """ Greedy CTC Decoder """ def __init__(self, **kwargs): nn.Module.__init__(self) def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
ghdrl95/Naver-Speech-Hackathon
GreedyCTCDecoder
false
6,737
[ "Apache-2.0" ]
1
10b4526d98ce535415cb91d24338790d9c175b63
https://github.com/ghdrl95/Naver-Speech-Hackathon/tree/10b4526d98ce535415cb91d24338790d9c175b63
RegionPenaltyLoss
import torch from torch import nn class RegionPenaltyLoss(nn.Module): def __init__(self, scale=1.0): """ Multiplicative penalty. Penalizes "forbidden" regions instead of exact distribution matches. Optionally used in tandem with MTCrossEntropyRegionAwareLoss. `scale` param allows caller to scale the loss in order to match magnitude of other loss terms """ super().__init__() self.scale = scale def forward(self, preds, targets): """ """ batch_size = preds.shape[0] penalty = torch.abs(targets - targets.max()) penalty /= torch.sum(penalty) loss = preds * penalty loss = loss.view(batch_size, -1) return torch.sum(loss, dim=1) * self.scale 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 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_abs_max_sub_sum_0(in_ptr0, out_ptr0, out_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.broadcast_to(tmp0, [RBLOCK]) tmp3 = triton_helpers.promote_to_tensor(triton_helpers.max2(tmp1, 0)) tmp4 = tmp0 - tmp3 tmp5 = tl_math.abs(tmp4) tmp6 = tl.broadcast_to(tmp5, [RBLOCK]) tmp8 = triton_helpers.promote_to_tensor(tl.sum(tmp6, 0)) tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp3, None) tl.store(out_ptr1 + tl.full([1], 0, tl.int32), tmp8, None) @triton.jit def triton_per_fused_mul_sum_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 RBLOCK: tl.constexpr = 64 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 + 64 * x0), xmask, other=0.0) tmp1 = tl.load(in_ptr1 + (r1 + 64 * x0), xmask, other=0.0) tmp2 = tl.load(in_ptr2 + 0) tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp6 = tl.load(in_ptr3 + 0) tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK]) tmp4 = tmp1 - tmp3 tmp5 = tl_math.abs(tmp4) tmp8 = tmp5 / tmp7 tmp9 = tmp0 * tmp8 tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK]) tmp12 = tl.where(xmask, tmp10, 0) tmp13 = tl.sum(tmp12, 1)[:, None] tmp14 = 1.0 tmp15 = tmp13 * tmp14 tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp15, 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((), (), torch.float32) buf1 = empty_strided_cuda((), (), torch.float32) get_raw_stream(0) triton_per_fused_abs_max_sub_sum_0[grid(1)](arg1_1, buf0, buf1, 1, 256, num_warps=2, num_stages=1) buf2 = empty_strided_cuda((4,), (1,), torch.float32) buf3 = buf2 del buf2 triton_per_fused_mul_sum_1[grid(4)](buf3, arg0_1, arg1_1, buf0, buf1, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 del buf0 del buf1 return buf3, class RegionPenaltyLossNew(nn.Module): def __init__(self, scale=1.0): """ Multiplicative penalty. Penalizes "forbidden" regions instead of exact distribution matches. Optionally used in tandem with MTCrossEntropyRegionAwareLoss. `scale` param allows caller to scale the loss in order to match magnitude of other loss terms """ super().__init__() self.scale = scale def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
geoffreyangus/pet-ct
RegionPenaltyLoss
false
6,738
[ "Apache-2.0" ]
1
fa96a07734afade475f6a1e1587ec14965fe2de3
https://github.com/geoffreyangus/pet-ct/tree/fa96a07734afade475f6a1e1587ec14965fe2de3
Network
import torch class Network(torch.nn.Module): def __init__(self, input_dimension, output_dimension): super(Network, self).__init__() self.layer_1 = torch.nn.Linear(in_features=input_dimension, out_features=100) self.layer_2 = torch.nn.Linear(in_features=100, out_features=200) self.output_layer = torch.nn.Linear(in_features=200, out_features= output_dimension) self.relu = torch.nn.ReLU() def forward(self, input): layer_1_output = self.relu(self.layer_1(input)) layer_2_output = self.relu(self.layer_2(layer_1_output)) output = self.output_layer(layer_2_output) return output def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_dimension': 4, 'output_dimension': 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 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 = 6400 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex x0 = xindex % 100 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_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 12800 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 200 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, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (100, 4), (4, 1)) assert_size_stride(primals_2, (100,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (200, 100), (100, 1)) assert_size_stride(primals_5, (200,), (1,)) assert_size_stride(primals_6, (4, 200), (200, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 100), (100, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 100), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 100), (1600, 400, 100, 1), 0) del buf0 buf6 = empty_strided_cuda((4, 4, 4, 100), (1664, 400, 100, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(6400)](buf1, primals_2, buf6, 6400, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 200), (200, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 100), (100, 1), 0), reinterpret_tensor(primals_4, (100, 200), (1, 100), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 200), (3200, 800, 200, 1), 0) del buf2 buf5 = empty_strided_cuda((4, 4, 4, 200), (3200, 800, 200, 1), torch.bool) triton_poi_fused_relu_threshold_backward_1[grid(12800)](buf3, primals_5, buf5, 12800, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 200), (200, 1), 0), reinterpret_tensor(primals_6, (200, 4), (1, 200), 0), alpha=1, beta=1, out=buf4) del primals_7 return reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 100), (100, 1), 0 ), reinterpret_tensor(buf3, (64, 200), (200, 1), 0 ), primals_6, buf5, primals_4, buf6 class NetworkNew(torch.nn.Module): def __init__(self, input_dimension, output_dimension): super(NetworkNew, self).__init__() self.layer_1 = torch.nn.Linear(in_features=input_dimension, out_features=100) self.layer_2 = torch.nn.Linear(in_features=100, out_features=200) self.output_layer = torch.nn.Linear(in_features=200, out_features= output_dimension) self.relu = torch.nn.ReLU() def forward(self, input_0): primals_1 = self.layer_1.weight primals_2 = self.layer_1.bias primals_4 = self.layer_2.weight primals_5 = self.layer_2.bias primals_6 = self.output_layer.weight primals_7 = self.output_layer.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
georgeyiasemis/Deep-Reinforcement-Learning-on-a-Grid-World-
Network
false
6,739
[ "MIT" ]
1
f32ceac5f4e29cba212d6fd1b8a25c08ac733666
https://github.com/georgeyiasemis/Deep-Reinforcement-Learning-on-a-Grid-World-/tree/f32ceac5f4e29cba212d6fd1b8a25c08ac733666
ScalarAttention
import torch import torch.nn as nn import torch.utils.data class ScalarAttention(nn.Module): def __init__(self, in_size, hidden_size): super(ScalarAttention, self).__init__() self.hidden = nn.Linear(in_size, hidden_size) nn.init.orthogonal_(self.hidden.weight.data) self.out = nn.Linear(hidden_size, 1) nn.init.orthogonal_(self.hidden.weight.data) self.softmax = nn.Softmax(dim=1) def forward(self, input): self.alpha = self.softmax(self.out(torch.tanh(self.hidden(input)))) x = (self.alpha.expand_as(input) * input).sum(dim=1) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_size': 4, '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 libdevice, 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_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) @triton.jit def triton_poi_fused__softmax_1(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_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 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_mul_sum_3(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 x1 = xindex // 4 % 4 x2 = xindex // 16 x3 = xindex % 16 x4 = xindex tmp0 = tl.load(in_ptr0 + (x1 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr1 + (x3 + 64 * x2), xmask) tmp3 = tl.load(in_ptr0 + (4 + x1 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr1 + (16 + x3 + 64 * x2), xmask) tmp7 = tl.load(in_ptr0 + (8 + x1 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp8 = tl.load(in_ptr1 + (32 + x3 + 64 * x2), xmask) tmp11 = tl.load(in_ptr0 + (12 + x1 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp12 = tl.load(in_ptr1 + (48 + x3 + 64 * x2), 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 + x4, tmp14, 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, (1, 4), (4, 1)) assert_size_stride(primals_5, (1,), (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 get_raw_stream(0) triton_poi_fused_tanh_0[grid(256)](buf1, primals_2, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf3 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_4, (4, 1), (1, 4), 0), alpha=1, beta=1, out=buf3) del primals_5 buf4 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) triton_poi_fused__softmax_1[grid(64)](buf3, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) buf5 = reinterpret_tensor(buf3, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf3 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_mul_sum_3[grid(64)](buf5, primals_3, buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) return buf6, buf5, primals_3, buf1, buf5, primals_4 class ScalarAttentionNew(nn.Module): def __init__(self, in_size, hidden_size): super(ScalarAttentionNew, self).__init__() self.hidden = nn.Linear(in_size, hidden_size) nn.init.orthogonal_(self.hidden.weight.data) self.out = nn.Linear(hidden_size, 1) nn.init.orthogonal_(self.hidden.weight.data) self.softmax = nn.Softmax(dim=1) def forward(self, input_0): primals_1 = self.hidden.weight primals_2 = self.hidden.bias primals_4 = self.out.weight primals_5 = self.out.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
gchrupala/platalea
ScalarAttention
false
6,740
[ "Apache-2.0" ]
1
65833307bb6c5ad6cbdd6b17ad8ca59cf51fcd81
https://github.com/gchrupala/platalea/tree/65833307bb6c5ad6cbdd6b17ad8ca59cf51fcd81
ODEfunc
import torch import torch.nn as nn def norm(dim): return nn.GroupNorm(min(32, dim), dim) class ConcatConv2d(nn.Module): def __init__(self, dim_in, dim_out, ksize=3, stride=1, padding=0, dilation=1, groups=1, bias=True, transpose=False): super(ConcatConv2d, self).__init__() module = nn.ConvTranspose2d if transpose else nn.Conv2d self._layer = module(dim_in + 1, dim_out, kernel_size=ksize, stride =stride, padding=padding, dilation=dilation, groups=groups, bias=bias) def forward(self, t, x): tt = torch.ones_like(x[:, :1, :, :]) * t ttx = torch.cat([tt, x], 1) return self._layer(ttx) class ODEfunc(nn.Module): def __init__(self, dim): super(ODEfunc, self).__init__() self.norm1 = norm(dim) self.relu = nn.ReLU(inplace=True) self.conv1 = ConcatConv2d(dim, dim, 3, 1, 1) self.norm2 = norm(dim) self.conv2 = ConcatConv2d(dim, dim, 3, 1, 1) self.norm3 = norm(dim) self.nfe = 0 def forward(self, t, x): self.nfe += 1 out = self.norm1(x) out = self.relu(out) out = self.conv1(t, out) out = self.norm2(out) out = self.relu(out) out = self.conv2(t, out) out = self.norm3(out) return out def get_inputs(): return [torch.rand([4, 1, 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 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_per_fused_native_group_norm_relu_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, 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 x2 = xindex % 4 x3 = xindex // 4 tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp24 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last') tmp26 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last') tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tl.where(xmask, tmp1, 0) tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.full([XBLOCK, 1], 16, tl.int32) tmp9 = tmp8.to(tl.float32) tmp10 = tmp7 / tmp9 tmp11 = tmp1 - tmp10 tmp12 = tmp11 * tmp11 tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK]) tmp15 = tl.where(xmask, tmp13, 0) tmp16 = tl.sum(tmp15, 1)[:, None] tmp17 = 16.0 tmp18 = tmp16 / tmp17 tmp19 = 1e-05 tmp20 = tmp18 + tmp19 tmp21 = libdevice.rsqrt(tmp20) tmp22 = tmp0 - tmp10 tmp23 = tmp22 * tmp21 tmp25 = tmp23 * tmp24 tmp27 = tmp25 + tmp26 tmp28 = tl.full([1, 1], 0, tl.int32) tmp29 = triton_helpers.maximum(tmp28, tmp27) tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp21, xmask) tl.store(out_ptr1 + (r1 + 16 * x2 + 80 * x3), tmp29, xmask) tl.store(out_ptr0 + x0, tmp10, xmask) @triton.jit def triton_poi_fused_cat_1(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 x2 = xindex x0 = xindex % 16 x1 = xindex // 16 tmp0 = tl.load(in_ptr0 + x2, xmask) tl.store(out_ptr0 + (x0 + 80 * x1), tmp0, xmask) tl.store(out_ptr1 + (x0 + 80 * x1), tmp0, xmask) @triton.jit def triton_per_fused_convolution_native_group_norm_relu_2(in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, 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) r2 = rindex x3 = xindex x0 = xindex % 4 x1 = xindex // 4 tmp0 = tl.load(in_out_ptr0 + (r2 + 16 * x3), xmask, other=0.0) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp26 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp28 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tl.where(xmask, tmp3, 0) tmp6 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp8 = tl.where(xmask, tmp6, 0) tmp9 = tl.sum(tmp8, 1)[:, None] tmp10 = tl.full([XBLOCK, 1], 16, tl.int32) tmp11 = tmp10.to(tl.float32) tmp12 = tmp9 / tmp11 tmp13 = tmp3 - tmp12 tmp14 = tmp13 * tmp13 tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK]) tmp17 = tl.where(xmask, tmp15, 0) tmp18 = tl.sum(tmp17, 1)[:, None] tmp19 = 16.0 tmp20 = tmp18 / tmp19 tmp21 = 1e-05 tmp22 = tmp20 + tmp21 tmp23 = libdevice.rsqrt(tmp22) tmp24 = tmp2 - tmp12 tmp25 = tmp24 * tmp23 tmp27 = tmp25 * tmp26 tmp29 = tmp27 + tmp28 tmp30 = tl.full([1, 1], 0, tl.int32) tmp31 = triton_helpers.maximum(tmp30, tmp29) tl.store(in_out_ptr0 + (r2 + 16 * x3), tmp2, xmask) tl.debug_barrier() tl.store(in_out_ptr1 + x3, tmp23, xmask) tl.store(out_ptr1 + (r2 + 16 * x0 + 80 * x1), tmp31, xmask) tl.store(out_ptr0 + x3, tmp12, xmask) @triton.jit def triton_per_fused_convolution_native_group_norm_3(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr2, out_ptr3, 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) r2 = rindex x3 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + (r2 + 16 * x3), xmask, other=0.0) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp26 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp28 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tl.where(xmask, tmp3, 0) tmp6 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp8 = tl.where(xmask, tmp6, 0) tmp9 = tl.sum(tmp8, 1)[:, None] tmp10 = tl.full([XBLOCK, 1], 16, tl.int32) tmp11 = tmp10.to(tl.float32) tmp12 = tmp9 / tmp11 tmp13 = tmp3 - tmp12 tmp14 = tmp13 * tmp13 tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK]) tmp17 = tl.where(xmask, tmp15, 0) tmp18 = tl.sum(tmp17, 1)[:, None] tmp19 = tmp2 - tmp12 tmp20 = 16.0 tmp21 = tmp18 / tmp20 tmp22 = 1e-05 tmp23 = tmp21 + tmp22 tmp24 = libdevice.rsqrt(tmp23) tmp25 = tmp19 * tmp24 tmp27 = tmp25 * tmp26 tmp29 = tmp27 + tmp28 tl.store(in_out_ptr0 + (r2 + 16 * x3), tmp2, xmask) tl.store(out_ptr2 + (r2 + 16 * x3), tmp29, xmask) tl.store(out_ptr3 + x3, tmp24, xmask) tl.store(out_ptr0 + x3, tmp12, 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 ) = 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, 1, 4, 4), (16, 16, 4, 1)) assert_size_stride(primals_5, (4, 5, 3, 3), (45, 9, 3, 1)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (4,), (1,)) assert_size_stride(primals_8, (4,), (1,)) assert_size_stride(primals_9, (4, 5, 3, 3), (45, 9, 3, 1)) assert_size_stride(primals_10, (4,), (1,)) assert_size_stride(primals_11, (4,), (1,)) assert_size_stride(primals_12, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32) buf1 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) buf3 = reinterpret_tensor(buf1, (4, 4, 1, 1), (4, 1, 1, 1), 0) del buf1 buf6 = empty_strided_cuda((4, 5, 4, 4), (80, 16, 4, 1), torch.float32) buf5 = reinterpret_tensor(buf6, (4, 4, 4, 4), (80, 16, 4, 1), 16) get_raw_stream(0) triton_per_fused_native_group_norm_relu_0[grid(16)](buf3, primals_3, primals_1, primals_2, buf0, buf5, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) buf4 = reinterpret_tensor(buf6, (4, 1, 4, 4), (80, 16, 4, 1), 0) buf15 = empty_strided_cuda((4, 5, 4, 4), (80, 16, 4, 1), torch.float32) buf13 = reinterpret_tensor(buf15, (4, 1, 4, 4), (80, 16, 4, 1), 0) triton_poi_fused_cat_1[grid(64)](primals_4, buf4, buf13, 64, XBLOCK =64, num_warps=1, num_stages=1) del primals_4 buf7 = extern_kernels.convolution(buf6, primals_5, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf7, (4, 4, 4, 4), (64, 16, 4, 1)) buf8 = buf7 del buf7 buf9 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32) buf10 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) buf12 = reinterpret_tensor(buf10, (4, 4, 1, 1), (4, 1, 1, 1), 0) del buf10 buf14 = reinterpret_tensor(buf15, (4, 4, 4, 4), (80, 16, 4, 1), 16) triton_per_fused_convolution_native_group_norm_relu_2[grid(16)](buf8, buf12, primals_6, primals_7, primals_8, buf9, buf14, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del primals_6 buf16 = extern_kernels.convolution(buf15, primals_9, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf16, (4, 4, 4, 4), (64, 16, 4, 1)) buf17 = buf16 del buf16 buf18 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) buf21 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf22 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) triton_per_fused_convolution_native_group_norm_3[grid(16)](buf17, primals_10, primals_11, primals_12, buf18, buf21, buf22, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del primals_10 del primals_12 return (buf21, primals_1, primals_2, primals_3, primals_5, primals_7, primals_8, primals_9, primals_11, buf0, buf3, buf6, buf8, buf9, buf12, buf15, buf17, reinterpret_tensor(buf18, (4, 4), (4, 1), 0), reinterpret_tensor(buf22, (4, 4), (4, 1), 0)) def norm(dim): return nn.GroupNorm(min(32, dim), dim) class ConcatConv2d(nn.Module): def __init__(self, dim_in, dim_out, ksize=3, stride=1, padding=0, dilation=1, groups=1, bias=True, transpose=False): super(ConcatConv2d, self).__init__() module = nn.ConvTranspose2d if transpose else nn.Conv2d self._layer = module(dim_in + 1, dim_out, kernel_size=ksize, stride =stride, padding=padding, dilation=dilation, groups=groups, bias=bias) def forward(self, t, x): tt = torch.ones_like(x[:, :1, :, :]) * t ttx = torch.cat([tt, x], 1) return self._layer(ttx) class ODEfuncNew(nn.Module): def __init__(self, dim): super(ODEfuncNew, self).__init__() self.norm1 = norm(dim) self.relu = nn.ReLU(inplace=True) self.conv1 = ConcatConv2d(dim, dim, 3, 1, 1) self.norm2 = norm(dim) self.conv2 = ConcatConv2d(dim, dim, 3, 1, 1) self.norm3 = norm(dim) self.nfe = 0 def forward(self, input_0, input_1): primals_1 = self.norm1.weight primals_2 = self.norm1.bias primals_5 = self.conv1._layer.weight primals_6 = self.conv1._layer.bias primals_7 = self.norm2.weight primals_8 = self.norm2.bias primals_9 = self.conv2._layer.weight primals_10 = self.conv2._layer.bias primals_11 = self.norm3.weight primals_12 = self.norm3.bias primals_4 = input_0 primals_3 = input_1 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]) return output[0]
gaozhihan/torchdiffeq
ODEfunc
false
6,741
[ "MIT" ]
1
414781617d595ba01cc3f23382e25ab890f4ca66
https://github.com/gaozhihan/torchdiffeq/tree/414781617d595ba01cc3f23382e25ab890f4ca66
MSELoss2d
import torch from torch import nn class MSELoss2d(nn.Module): def __init__(self, size_average=None, reduce=None, reduction='mean', ignore_index=255): super(MSELoss2d, self).__init__() self.MSE = nn.MSELoss(size_average=size_average, reduce=reduce, reduction=reduction) def forward(self, output, target): loss = self.MSE(torch.softmax(output, dim=1), target) 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 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__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_per_fused__softmax_mse_loss_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) r3 = rindex r0 = rindex % 16 r2 = rindex // 64 tmp0 = tl.load(in_ptr0 + r3, None) tmp1 = tl.load(in_ptr0 + (r0 + 64 * r2), None, eviction_policy='evict_last' ) tmp2 = tl.load(in_ptr0 + (16 + r0 + 64 * r2), None, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + r0 + 64 * r2), None, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + r0 + 64 * r2), None, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr1 + r3, None) tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tmp10 = tmp8 - tmp9 tmp11 = tmp10 * tmp10 tmp12 = tl.broadcast_to(tmp11, [RBLOCK]) tmp14 = triton_helpers.promote_to_tensor(tl.sum(tmp12, 0)) tmp15 = 256.0 tmp16 = tmp14 / 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((4, 4, 4, 4), (64, 16, 4, 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((), (), torch.float32) buf2 = buf1 del buf1 triton_per_fused__softmax_mse_loss_1[grid(1)](buf2, buf0, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg1_1 del buf0 return buf2, class MSELoss2dNew(nn.Module): def __init__(self, size_average=None, reduce=None, reduction='mean', ignore_index=255): super(MSELoss2dNew, self).__init__() self.MSE = nn.MSELoss(size_average=size_average, reduce=reduce, reduction=reduction) def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
giannifranchi/deeplabv3-superpixelmix
MSELoss2d
false
6,742
[ "MIT" ]
1
db52bf83b3b242af05bde5e39ee3de896e44c264
https://github.com/giannifranchi/deeplabv3-superpixelmix/tree/db52bf83b3b242af05bde5e39ee3de896e44c264
ResidualBlock
import torch import torch.nn as nn class ResidualBlock(nn.Module): def __init__(self, channels): super(ResidualBlock, self).__init__() self.conv1 = nn.Conv2d(channels, channels, kernel_size=3, padding=1) self.in1 = nn.InstanceNorm2d(channels) self.prelu = nn.PReLU() self.conv2 = nn.Conv2d(channels, channels, kernel_size=3, padding=1) self.in2 = nn.InstanceNorm2d(channels) def forward(self, x): residual = self.conv1(x) residual = self.in1(residual) residual = self.prelu(residual) residual = self.conv2(residual) residual = self.in2(residual) return x + residual def get_inputs(): return [torch.rand([4, 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.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_per_fused__native_batch_norm_legit__prelu_kernel_convolution_0( in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, out_ptr0, out_ptr1, 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) r2 = rindex x3 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + (r2 + 16 * x3), xmask, other=0.0) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp28 = tl.load(in_ptr1 + 0) tmp29 = tl.broadcast_to(tmp28, [XBLOCK, RBLOCK]) tmp2 = tmp0 + tmp1 tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tl.where(xmask, tmp3, 0) tmp6 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp8 = tl.where(xmask, tmp6, 0) tmp9 = tl.sum(tmp8, 1)[:, None] tmp10 = tl.full([XBLOCK, 1], 16, tl.int32) tmp11 = tmp10.to(tl.float32) tmp12 = tmp9 / tmp11 tmp13 = tmp3 - tmp12 tmp14 = tmp13 * tmp13 tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK]) tmp17 = tl.where(xmask, tmp15, 0) tmp18 = tl.sum(tmp17, 1)[:, None] tmp19 = 16.0 tmp20 = tmp18 / tmp19 tmp21 = 1e-05 tmp22 = tmp20 + tmp21 tmp23 = libdevice.rsqrt(tmp22) tmp24 = tmp2 - tmp12 tmp25 = tmp24 * tmp23 tmp26 = 0.0 tmp27 = tmp25 > tmp26 tmp30 = tmp29 * tmp25 tmp31 = tl.where(tmp27, tmp25, tmp30) tl.store(in_out_ptr0 + (r2 + 16 * x3), tmp2, xmask) tl.debug_barrier() tl.store(in_out_ptr1 + x3, tmp23, xmask) tl.store(out_ptr1 + (r2 + 16 * x3), tmp31, xmask) tl.store(out_ptr0 + x3, tmp12, xmask) @triton.jit def triton_per_fused__native_batch_norm_legit_add_convolution_1(in_out_ptr0, in_ptr0, in_ptr1, out_ptr0, out_ptr2, out_ptr3, 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) r2 = rindex x3 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + (r2 + 16 * x3), xmask, other=0.0) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp19 = tl.load(in_ptr1 + (r2 + 16 * x3), xmask, other=0.0) tmp2 = tmp0 + tmp1 tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tl.where(xmask, tmp3, 0) tmp6 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp8 = tl.where(xmask, tmp6, 0) tmp9 = tl.sum(tmp8, 1)[:, None] tmp10 = tl.full([XBLOCK, 1], 16, tl.int32) tmp11 = tmp10.to(tl.float32) tmp12 = tmp9 / tmp11 tmp13 = tmp3 - tmp12 tmp14 = tmp13 * tmp13 tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK]) tmp17 = tl.where(xmask, tmp15, 0) tmp18 = tl.sum(tmp17, 1)[:, None] tmp20 = tmp2 - tmp12 tmp21 = 16.0 tmp22 = tmp18 / tmp21 tmp23 = 1e-05 tmp24 = tmp22 + tmp23 tmp25 = libdevice.rsqrt(tmp24) tmp26 = tmp20 * tmp25 tmp27 = tmp19 + tmp26 tl.store(in_out_ptr0 + (r2 + 16 * x3), tmp2, xmask) tl.store(out_ptr2 + (r2 + 16 * x3), tmp27, xmask) tl.store(out_ptr3 + x3, tmp25, xmask) tl.store(out_ptr0 + x3, tmp12, 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, 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, (1,), (1,)) assert_size_stride(primals_5, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_6, (4,), (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((1, 16, 1, 1), (16, 1, 1, 1), torch.float32) buf3 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32 ) buf5 = reinterpret_tensor(buf3, (1, 16, 1, 1), (16, 1, 1, 1), 0) del buf3 buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_per_fused__native_batch_norm_legit__prelu_kernel_convolution_0[ grid(16)](buf1, buf5, primals_2, primals_4, buf2, buf6, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) del primals_2 buf7 = extern_kernels.convolution(buf6, primals_5, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf7, (4, 4, 4, 4), (64, 16, 4, 1)) buf8 = buf7 del buf7 buf9 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32 ) buf13 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf12 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch. float32) triton_per_fused__native_batch_norm_legit_add_convolution_1[grid(16)]( buf8, primals_6, primals_3, buf9, buf13, buf12, 16, 16, XBLOCK= 1, num_warps=2, num_stages=1) del primals_6 return (buf13, primals_1, primals_3, primals_4, primals_5, buf1, buf2, buf5, buf6, buf8, reinterpret_tensor(buf12, (16,), (1,), 0), reinterpret_tensor(buf9, (1, 16, 1, 1), (16, 1, 1, 1), 0)) class ResidualBlockNew(nn.Module): def __init__(self, channels): super(ResidualBlockNew, self).__init__() self.conv1 = nn.Conv2d(channels, channels, kernel_size=3, padding=1) self.in1 = nn.InstanceNorm2d(channels) self.prelu = nn.PReLU() self.conv2 = nn.Conv2d(channels, channels, kernel_size=3, padding=1) self.in2 = nn.InstanceNorm2d(channels) def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.prelu.weight primals_5 = self.conv2.weight primals_6 = self.conv2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6]) return output[0]
generall/Torchlite
ResidualBlock
false
6,743
[ "MIT" ]
1
2eb3e2a20b7619bd58b0b0fca120e2aefca0e79a
https://github.com/generall/Torchlite/tree/2eb3e2a20b7619bd58b0b0fca120e2aefca0e79a
ConvBlock
import torch import torch.nn as nn class ConvBlock(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride): super(ConvBlock, self).__init__() self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding=1) self.lr = nn.LeakyReLU() def forward(self, x): x = self.conv(x) return self.lr(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, 'stride': 1}]
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 @triton.jit def triton_poi_fused_convolution_leaky_relu_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 144 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 9 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x3, tmp4, xmask) tl.store(out_ptr1 + x3, tmp7, 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,), (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 = 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, 3, 3), (36, 9, 3, 1)) buf1 = empty_strided_cuda((4, 4, 3, 3), (36, 9, 3, 1), torch.bool) buf2 = empty_strided_cuda((4, 4, 3, 3), (36, 9, 3, 1), torch.float32) get_raw_stream(0) triton_poi_fused_convolution_leaky_relu_0[grid(144)](buf0, primals_2, buf1, buf2, 144, XBLOCK=128, num_warps=4, num_stages=1) del buf0 del primals_2 return buf2, primals_1, primals_3, buf1 class ConvBlockNew(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride): super(ConvBlockNew, self).__init__() self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding=1) self.lr = nn.LeakyReLU() def forward(self, input_0): primals_1 = self.conv.weight primals_2 = self.conv.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
gle-bellier/DuelingNetwork
ConvBlock
false
6,744
[ "MIT" ]
1
8909fe1ba6aee08b6249cb6ca3287752039c6410
https://github.com/gle-bellier/DuelingNetwork/tree/8909fe1ba6aee08b6249cb6ca3287752039c6410
WordPredictor
import torch import torch.nn.functional as F import torch.nn as nn import torch.onnx.operators class WordPredictor(nn.Module): def __init__(self, encoder_output_dim, hidden_dim, output_dim): super().__init__() self.encoder_output_dim = encoder_output_dim self.hidden_dim = hidden_dim self.output_dim = output_dim self.init_layer = nn.Linear(encoder_output_dim, encoder_output_dim) self.attn_layer = nn.Linear(2 * encoder_output_dim, 1) self.hidden_layer = nn.Linear(2 * encoder_output_dim, hidden_dim) self.output_layer = nn.Linear(hidden_dim, output_dim) def forward(self, encoder_output): encoder_hiddens, *_ = encoder_output assert encoder_hiddens.dim() == 3 init_state = self._get_init_state(encoder_hiddens) attn_scores = self._attention(encoder_hiddens, init_state) attned_state = (encoder_hiddens * attn_scores).sum(0) pred_input = torch.cat([init_state, attned_state], 1) pred_hidden = F.relu(self.hidden_layer(pred_input)) pred_logit = self.output_layer(pred_hidden) return pred_logit def _get_init_state(self, encoder_hiddens): x = torch.mean(encoder_hiddens, 0) x = F.relu(self.init_layer(x)) return x def _attention(self, encoder_hiddens, init_state): init_state = init_state.unsqueeze(0).expand_as(encoder_hiddens) attn_input = torch.cat([init_state, encoder_hiddens], 2) attn_scores = F.relu(self.attn_layer(attn_input)) attn_scores = F.softmax(attn_scores, 0) return attn_scores def get_normalized_probs(self, net_output, log_probs): """Get normalized probabilities (or log probs) from a net's output.""" logits = net_output if log_probs: return F.log_softmax(logits, dim=1) else: return F.softmax(logits, dim=1) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'encoder_output_dim': 4, 'hidden_dim': 4, 'output_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 math as tl_math import torch.nn.functional as F import torch.nn as nn import torch.onnx.operators 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 = 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 = tl.load(in_ptr0 + (16 + x0), xmask) tmp3 = tl.load(in_ptr0 + (32 + x0), xmask) tmp5 = tl.load(in_ptr0 + (48 + x0), xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tl.store(out_ptr0 + x0, tmp8, xmask) @triton.jit def triton_poi_fused_cat_1(in_ptr0, in_ptr1, in_ptr2, 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 x1 = xindex // 8 % 4 x3 = xindex // 8 x4 = 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 = tl.load(in_ptr1 + x0, tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp7 = tmp5 + tmp6 tmp8 = tl.full([1], 0, tl.int32) tmp9 = triton_helpers.maximum(tmp8, tmp7) tmp10 = tl.full(tmp9.shape, 0.0, tmp9.dtype) tmp11 = tl.where(tmp4, tmp9, tmp10) tmp12 = tmp0 >= tmp3 tl.full([1], 8, tl.int64) tmp15 = tl.load(in_ptr2 + (4 * x3 + (-4 + x0)), tmp12 & xmask, eviction_policy='evict_last', other=0.0) tmp16 = tl.where(tmp4, tmp11, tmp15) tl.store(out_ptr0 + x4, tmp16, xmask) @triton.jit def triton_poi_fused__softmax_relu_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 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp3 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (4 + x0), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (8 + x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (12 + x0), xmask, eviction_policy='evict_last') tmp1 = tl.full([1], 0, tl.int32) tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp1, tmp3) tmp6 = triton_helpers.maximum(tmp1, tmp5) tmp7 = triton_helpers.maximum(tmp4, tmp6) tmp9 = triton_helpers.maximum(tmp1, tmp8) tmp10 = triton_helpers.maximum(tmp7, tmp9) tmp12 = triton_helpers.maximum(tmp1, tmp11) tmp13 = triton_helpers.maximum(tmp10, tmp12) tmp14 = tmp2 - tmp13 tmp15 = tl_math.exp(tmp14) tl.store(out_ptr0 + x2, tmp15, 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 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (4 + x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (8 + x0), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (12 + x0), 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_cat_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 8 x1 = xindex // 8 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 = tl.load(in_ptr1 + x0, tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp7 = tmp5 + tmp6 tmp8 = tl.full([1], 0, tl.int32) tmp9 = triton_helpers.maximum(tmp8, tmp7) tmp10 = tl.full(tmp9.shape, 0.0, tmp9.dtype) tmp11 = tl.where(tmp4, tmp9, tmp10) tmp12 = tmp0 >= tmp3 tl.full([1], 8, tl.int64) tmp15 = tl.load(in_ptr2 + (4 * x1 + (-4 + x0)), tmp12 & xmask, eviction_policy='evict_last', other=0.0) tmp16 = tl.load(in_ptr3 + x1, tmp12 & xmask, eviction_policy= 'evict_last', other=0.0) tmp17 = tmp15 * tmp16 tmp18 = tl.load(in_ptr2 + (16 + 4 * x1 + (-4 + x0)), tmp12 & xmask, eviction_policy='evict_last', other=0.0) tmp19 = tl.load(in_ptr3 + (4 + x1), tmp12 & xmask, eviction_policy= 'evict_last', other=0.0) tmp20 = tmp18 * tmp19 tmp21 = tmp17 + tmp20 tmp22 = tl.load(in_ptr2 + (32 + 4 * x1 + (-4 + x0)), tmp12 & xmask, eviction_policy='evict_last', other=0.0) tmp23 = tl.load(in_ptr3 + (8 + x1), tmp12 & xmask, eviction_policy= 'evict_last', other=0.0) tmp24 = tmp22 * tmp23 tmp25 = tmp21 + tmp24 tmp26 = tl.load(in_ptr2 + (48 + 4 * x1 + (-4 + x0)), tmp12 & xmask, eviction_policy='evict_last', other=0.0) tmp27 = tl.load(in_ptr3 + (12 + x1), tmp12 & xmask, eviction_policy= 'evict_last', other=0.0) tmp28 = tmp26 * tmp27 tmp29 = tmp25 + tmp28 tmp30 = tl.full(tmp29.shape, 0.0, tmp29.dtype) tmp31 = tl.where(tmp12, tmp29, tmp30) tmp32 = tl.where(tmp4, tmp11, tmp31) tl.store(out_ptr0 + x2, tmp32, xmask) @triton.jit def triton_poi_fused_relu_5(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 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_threshold_backward_6(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 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 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(out_ptr0 + x2, tmp6, 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, (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, (1, 8), (8, 1)) assert_size_stride(primals_5, (1,), (1,)) assert_size_stride(primals_6, (4, 8), (8, 1)) assert_size_stride(primals_7, (4,), (1,)) assert_size_stride(primals_8, (4, 4), (4, 1)) assert_size_stride(primals_9, (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_0[grid(16)](primals_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf0, reinterpret_tensor(primals_2, (4, 4), (1, 4 ), 0), out=buf1) del primals_2 buf2 = empty_strided_cuda((4, 4, 8), (32, 8, 1), torch.float32) triton_poi_fused_cat_1[grid(128)](buf1, primals_3, primals_1, buf2, 128, XBLOCK=128, num_warps=4, num_stages=1) buf4 = empty_strided_cuda((16, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf2, (16, 8), ( 8, 1), 0), reinterpret_tensor(primals_4, (8, 1), (1, 8), 0), alpha=1, beta=1, out=buf4) del primals_5 buf5 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) triton_poi_fused__softmax_relu_2[grid(16)](buf4, buf5, 16, XBLOCK= 16, num_warps=1, num_stages=1) buf6 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) triton_poi_fused__softmax_3[grid(16)](buf5, buf6, 16, XBLOCK=16, num_warps=1, num_stages=1) buf7 = empty_strided_cuda((4, 8), (8, 1), torch.float32) triton_poi_fused_cat_4[grid(32)](buf1, primals_3, primals_1, buf6, buf7, 32, XBLOCK=32, num_warps=1, num_stages=1) buf8 = reinterpret_tensor(buf6, (4, 4), (4, 1), 0) del buf6 extern_kernels.mm(buf7, reinterpret_tensor(primals_6, (8, 4), (1, 8 ), 0), out=buf8) buf9 = buf8 del buf8 triton_poi_fused_relu_5[grid(16)](buf9, primals_7, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_7 buf10 = reinterpret_tensor(buf5, (4, 4), (4, 1), 0) del buf5 extern_kernels.addmm(primals_9, buf9, reinterpret_tensor(primals_8, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf10) del primals_9 buf11 = empty_strided_cuda((4, 4), (4, 1), torch.bool) triton_poi_fused_relu_threshold_backward_6[grid(16)](buf1, primals_3, buf11, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf1 del primals_3 return buf10, reinterpret_tensor(primals_1, (4, 4, 4), (16, 4, 1), 0 ), buf0, reinterpret_tensor(buf2, (16, 8), (8, 1), 0 ), buf4, buf7, buf9, primals_8, primals_6, primals_4, buf11 class WordPredictorNew(nn.Module): def __init__(self, encoder_output_dim, hidden_dim, output_dim): super().__init__() self.encoder_output_dim = encoder_output_dim self.hidden_dim = hidden_dim self.output_dim = output_dim self.init_layer = nn.Linear(encoder_output_dim, encoder_output_dim) self.attn_layer = nn.Linear(2 * encoder_output_dim, 1) self.hidden_layer = nn.Linear(2 * encoder_output_dim, hidden_dim) self.output_layer = nn.Linear(hidden_dim, output_dim) def _get_init_state(self, encoder_hiddens): x = torch.mean(encoder_hiddens, 0) x = F.relu(self.init_layer(x)) return x def _attention(self, encoder_hiddens, init_state): init_state = init_state.unsqueeze(0).expand_as(encoder_hiddens) attn_input = torch.cat([init_state, encoder_hiddens], 2) attn_scores = F.relu(self.attn_layer(attn_input)) attn_scores = F.softmax(attn_scores, 0) return attn_scores def get_normalized_probs(self, net_output, log_probs): """Get normalized probabilities (or log probs) from a net's output.""" logits = net_output if log_probs: return F.log_softmax(logits, dim=1) else: return F.softmax(logits, dim=1) def forward(self, input_0): primals_2 = self.init_layer.weight primals_3 = self.init_layer.bias primals_4 = self.attn_layer.weight primals_5 = self.attn_layer.bias primals_6 = self.hidden_layer.weight primals_7 = self.hidden_layer.bias primals_8 = self.output_layer.weight primals_9 = self.output_layer.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]) return output[0]
gardenia22/translate
WordPredictor
false
6,745
[ "BSD-3-Clause" ]
1
0be57c8f55b52fc9d39197efa02e05d1c1cda024
https://github.com/gardenia22/translate/tree/0be57c8f55b52fc9d39197efa02e05d1c1cda024
VideoNormalizer
import torch import torch.nn as nn class VideoNormalizer(nn.Module): def __init__(self): super(VideoNormalizer, self).__init__() self.scale = nn.Parameter(torch.Tensor([255.0]), requires_grad=False) self.mean = nn.Parameter(torch.Tensor([0.485, 0.456, 0.406]), requires_grad=False) self.std = nn.Parameter(torch.Tensor([0.229, 0.224, 0.225]), requires_grad=False) def forward(self, video): video = video.float() video = (video / self.scale - self.mean) / self.std return video.permute(0, 3, 1, 2) def get_inputs(): return [torch.rand([4, 4, 4, 3])] 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 reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_div_sub_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 192 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 3 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp3 = tmp0 / tmp2 tmp5 = tmp3 - tmp4 tmp7 = tmp5 / tmp6 tl.store(out_ptr0 + x2, tmp7, xmask) def call(args): arg0_1, arg1_1, arg2_1, arg3_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 3), (48, 12, 3, 1)) assert_size_stride(arg1_1, (1,), (1,)) assert_size_stride(arg2_1, (3,), (1,)) assert_size_stride(arg3_1, (3,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 3), (48, 12, 3, 1), torch.float32) get_raw_stream(0) triton_poi_fused_div_sub_0[grid(192)](arg0_1, arg1_1, arg2_1, arg3_1, buf0, 192, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del arg1_1 del arg2_1 del arg3_1 return reinterpret_tensor(buf0, (4, 3, 4, 4), (48, 1, 12, 3), 0), class VideoNormalizerNew(nn.Module): def __init__(self): super(VideoNormalizerNew, self).__init__() self.scale = nn.Parameter(torch.Tensor([255.0]), requires_grad=False) self.mean = nn.Parameter(torch.Tensor([0.485, 0.456, 0.406]), requires_grad=False) self.std = nn.Parameter(torch.Tensor([0.229, 0.224, 0.225]), requires_grad=False) def forward(self, input_0): arg1_1 = self.scale arg2_1 = self.mean arg3_1 = self.std arg0_1 = input_0 output = call([arg0_1, arg1_1, arg2_1, arg3_1]) return output[0]
glee1228/segment_temporal_context_aggregation
VideoNormalizer
false
6,746
[ "Apache-2.0" ]
1
e5778f848f1cfd89bd1f77beb5e1b38a66a2f13d
https://github.com/glee1228/segment_temporal_context_aggregation/tree/e5778f848f1cfd89bd1f77beb5e1b38a66a2f13d
LogSparsemax
from torch.autograd import Function import torch import torch.nn.init import torch.nn as nn def _make_ix_like(input, dim=0): d = input.size(dim) rho = torch.arange(1, d + 1, device=input.device, dtype=input.dtype) view = [1] * input.dim() view[0] = -1 return rho.view(view).transpose(0, dim) def _threshold_and_support(input, dim=0): """Sparsemax building block: compute the threshold Args: input: any dimension dim: dimension along which to apply the sparsemax Returns: the threshold value """ input_srt, _ = torch.sort(input, descending=True, dim=dim) input_cumsum = input_srt.cumsum(dim) - 1 rhos = _make_ix_like(input, dim) support = rhos * input_srt > input_cumsum support_size = support.sum(dim=dim).unsqueeze(dim) tau = input_cumsum.gather(dim, support_size - 1) tau /= support_size return tau, support_size def sparsemax(input, dim=0): return SparsemaxFunction.apply(input, dim) class SparsemaxFunction(Function): @staticmethod def forward(ctx, input, dim=0): """sparsemax: normalizing sparse transform (a la softmax) Parameters: input (Tensor): any shape dim: dimension along which to apply sparsemax Returns: output (Tensor): same shape as input """ ctx.dim = dim max_val, _ = input.max(dim=dim, keepdim=True) input -= max_val tau, supp_size = _threshold_and_support(input, dim=dim) output = torch.clamp(input - tau, min=0) ctx.save_for_backward(supp_size, output) return output @staticmethod def backward(ctx, grad_output): supp_size, output = ctx.saved_tensors dim = ctx.dim grad_input = grad_output.clone() grad_input[output == 0] = 0 v_hat = grad_input.sum(dim=dim) / supp_size.squeeze() v_hat = v_hat.unsqueeze(dim) grad_input = torch.where(output != 0, grad_input - v_hat, grad_input) return grad_input, None class LogSparsemax(nn.Module): def __init__(self, dim=0): self.dim = dim super(LogSparsemax, self).__init__() def forward(self, input): return torch.log(sparsemax(input, 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.autograd import Function import torch.nn.init 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_max_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 x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (64 + x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (128 + x0), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (192 + x0), 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_helper_fn_add0(arg0_0, arg1_0): tmp0 = arg0_0 + arg1_0 return tmp0 @triton.jit def triton_per_fused_cumsum_sort_1(in_ptr0, out_ptr0, out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 64 RBLOCK: tl.constexpr = 4 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 + (x0 + 64 * r1), xmask, other=0.0) tmp1 = r1 tmp2 = tmp1.to(tl.int16) tmp3 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp4 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp5, _tmp6 = triton_helpers.sort_with_index(tmp3, tmp4, None, 1, stable=False, descending=True) tmp7 = tmp5.to(tl.float32) tmp8 = tl.broadcast_to(tmp7, [XBLOCK, RBLOCK]) tmp9, = tl.associative_scan((tmp8,), 1, _triton_helper_fn_add0) tl.store(out_ptr0 + (x0 + 64 * r1), tmp5, xmask) tl.store(out_ptr1 + (x0 + 64 * r1), tmp9, xmask) @triton.jit def triton_poi_fused_gt_mul_sub_sum_2(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 tmp0 = tl.load(in_ptr0 + x0, xmask) tmp3 = tl.load(in_ptr1 + x0, xmask) tmp7 = tl.load(in_ptr0 + (64 + x0), xmask) tmp10 = tl.load(in_ptr1 + (64 + x0), xmask) tmp15 = tl.load(in_ptr0 + (128 + x0), xmask) tmp18 = tl.load(in_ptr1 + (128 + x0), xmask) tmp23 = tl.load(in_ptr0 + (192 + x0), xmask) tmp26 = tl.load(in_ptr1 + (192 + x0), xmask) tmp1 = 1.0 tmp2 = tmp1 * tmp0 tmp4 = tmp3 - tmp1 tmp5 = tmp2 > tmp4 tmp6 = tmp5.to(tl.int64) tmp8 = 2.0 tmp9 = tmp8 * tmp7 tmp11 = tmp10 - tmp1 tmp12 = tmp9 > tmp11 tmp13 = tmp12.to(tl.int64) tmp14 = tmp6 + tmp13 tmp16 = 3.0 tmp17 = tmp16 * tmp15 tmp19 = tmp18 - tmp1 tmp20 = tmp17 > tmp19 tmp21 = tmp20.to(tl.int64) tmp22 = tmp14 + tmp21 tmp24 = 4.0 tmp25 = tmp24 * tmp23 tmp27 = tmp26 - tmp1 tmp28 = tmp25 > tmp27 tmp29 = tmp28.to(tl.int64) tmp30 = tmp22 + tmp29 tl.store(out_ptr0 + x0, tmp30, xmask) @triton.jit def triton_poi_fused_clamp_div_gather_log_sub_3(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 x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tl.full([1], 1, tl.int64) tmp3 = tmp1 - tmp2 tmp4 = tl.full([XBLOCK], 4, tl.int32) tmp5 = tmp3 + tmp4 tmp6 = tmp3 < 0 tmp7 = tl.where(tmp6, tmp5, tmp3) tl.device_assert((0 <= tmp7) & (tmp7 < 4) | ~xmask, 'index out of bounds: 0 <= tmp7 < 4') tmp9 = tl.load(in_ptr2 + (x0 + 64 * tmp7), xmask) tmp10 = 1.0 tmp11 = tmp9 - tmp10 tmp12 = tmp1.to(tl.float32) tmp13 = tmp11 / tmp12 tmp14 = tmp0 - tmp13 tmp15 = 0.0 tmp16 = triton_helpers.maximum(tmp14, tmp15) tmp17 = tl_math.log(tmp16) tl.store(out_ptr0 + x2, tmp17, xmask) tl.store(out_ptr1 + x2, tmp0, 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_max_sub_0[grid(256)](arg0_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) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_per_fused_cumsum_sort_1[grid(64)](buf0, buf1, buf3, 64, 4, XBLOCK=32, num_warps=2, num_stages=1) buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.int64) triton_poi_fused_gt_mul_sub_sum_2[grid(64)](buf1, buf3, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) buf5 = buf1 del buf1 triton_poi_fused_clamp_div_gather_log_sub_3[grid(256)](buf0, buf4, buf3, buf5, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del buf0 del buf3 del buf4 return buf5, def _make_ix_like(input, dim=0): d = input.size(dim) rho = torch.arange(1, d + 1, device=input.device, dtype=input.dtype) view = [1] * input.dim() view[0] = -1 return rho.view(view).transpose(0, dim) def _threshold_and_support(input, dim=0): """Sparsemax building block: compute the threshold Args: input: any dimension dim: dimension along which to apply the sparsemax Returns: the threshold value """ input_srt, _ = torch.sort(input, descending=True, dim=dim) input_cumsum = input_srt.cumsum(dim) - 1 rhos = _make_ix_like(input, dim) support = rhos * input_srt > input_cumsum support_size = support.sum(dim=dim).unsqueeze(dim) tau = input_cumsum.gather(dim, support_size - 1) tau /= support_size return tau, support_size def sparsemax(input, dim=0): return SparsemaxFunction.apply(input, dim) class SparsemaxFunction(Function): @staticmethod def forward(ctx, input, dim=0): """sparsemax: normalizing sparse transform (a la softmax) Parameters: input (Tensor): any shape dim: dimension along which to apply sparsemax Returns: output (Tensor): same shape as input """ ctx.dim = dim max_val, _ = input.max(dim=dim, keepdim=True) input -= max_val tau, supp_size = _threshold_and_support(input, dim=dim) output = torch.clamp(input - tau, min=0) ctx.save_for_backward(supp_size, output) return output @staticmethod def backward(ctx, grad_output): supp_size, output = ctx.saved_tensors dim = ctx.dim grad_input = grad_output.clone() grad_input[output == 0] = 0 v_hat = grad_input.sum(dim=dim) / supp_size.squeeze() v_hat = v_hat.unsqueeze(dim) grad_input = torch.where(output != 0, grad_input - v_hat, grad_input) return grad_input, None class LogSparsemaxNew(nn.Module): def __init__(self, dim=0): self.dim = dim super(LogSparsemaxNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
gililior/qasrl-modeling
LogSparsemax
false
6,747
[ "MIT" ]
1
2f9684536f6d5f0283b0e4b90a911ea12fa72f72
https://github.com/gililior/qasrl-modeling/tree/2f9684536f6d5f0283b0e4b90a911ea12fa72f72
RegressionSubNet
import torch import torch.nn as nn class RegressionSubNet(nn.Module): def __init__(self, in_channels, num_anchors=9): super().__init__() self.conv2d_1 = nn.Conv2d(in_channels, 256, 3, padding=1) nn.init.normal_(self.conv2d_1.weight.data, std=0.01) nn.init.zeros_(self.conv2d_1.bias.data) self.conv2d_2 = nn.Conv2d(256, 256, 3, padding=1) nn.init.normal_(self.conv2d_2.weight.data, std=0.01) nn.init.zeros_(self.conv2d_2.bias.data) self.conv2d_3 = nn.Conv2d(256, 256, 3, padding=1) nn.init.normal_(self.conv2d_3.weight.data, std=0.01) nn.init.zeros_(self.conv2d_3.bias.data) self.conv2d_4 = nn.Conv2d(256, 256, 3, padding=1) nn.init.normal_(self.conv2d_4.weight.data, std=0.01) nn.init.zeros_(self.conv2d_4.bias.data) self.conv2d_5 = nn.Conv2d(256, 4 * num_anchors, 3, padding=1) nn.init.normal_(self.conv2d_5.weight.data, std=0.01) nn.init.zeros_(self.conv2d_5.bias.data) def forward(self, x): x = self.conv2d_1(x) x = nn.functional.relu(x) x = self.conv2d_2(x) x = nn.functional.relu(x) x = self.conv2d_3(x) x = nn.functional.relu(x) x = self.conv2d_4(x) x = nn.functional.relu(x) x = self.conv2d_5(x) x = x.permute(0, 2, 3, 1) return x.reshape(x.size(0), -1, 4) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_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 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_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__unsafe_view_clone_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__unsafe_view_clone_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 RegressionSubNetNew(nn.Module): def __init__(self, in_channels, num_anchors=9): super().__init__() self.conv2d_1 = nn.Conv2d(in_channels, 256, 3, padding=1) nn.init.normal_(self.conv2d_1.weight.data, std=0.01) nn.init.zeros_(self.conv2d_1.bias.data) self.conv2d_2 = nn.Conv2d(256, 256, 3, padding=1) nn.init.normal_(self.conv2d_2.weight.data, std=0.01) nn.init.zeros_(self.conv2d_2.bias.data) self.conv2d_3 = nn.Conv2d(256, 256, 3, padding=1) nn.init.normal_(self.conv2d_3.weight.data, std=0.01) nn.init.zeros_(self.conv2d_3.bias.data) self.conv2d_4 = nn.Conv2d(256, 256, 3, padding=1) nn.init.normal_(self.conv2d_4.weight.data, std=0.01) nn.init.zeros_(self.conv2d_4.bias.data) self.conv2d_5 = nn.Conv2d(256, 4 * num_anchors, 3, padding=1) nn.init.normal_(self.conv2d_5.weight.data, std=0.01) nn.init.zeros_(self.conv2d_5.bias.data) def forward(self, input_0): primals_1 = self.conv2d_1.weight primals_2 = self.conv2d_1.bias primals_4 = self.conv2d_2.weight primals_5 = self.conv2d_2.bias primals_6 = self.conv2d_3.weight primals_7 = self.conv2d_3.bias primals_8 = self.conv2d_4.weight primals_9 = self.conv2d_4.bias primals_10 = self.conv2d_5.weight primals_11 = self.conv2d_5.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]
geez0219/ARC
RegressionSubNet
false
6,748
[ "Apache-2.0" ]
1
f2176f0d442d4a2d6028f0770b1efc1a9ae982b8
https://github.com/geez0219/ARC/tree/f2176f0d442d4a2d6028f0770b1efc1a9ae982b8
Attention
import torch import torch.nn as nn class Attention(nn.Module): def __init__(self, dims, norm=False): super(Attention, self).__init__() self.norm = norm if self.norm: self.constrain = L2Constrain() else: self.transform = nn.Linear(dims, dims) self.context_vector = nn.Linear(dims, 1, bias=False) self.reset_parameters() def forward(self, x): if self.norm: weights = self.context_vector(x) weights = torch.add(torch.div(weights, 2.0), 0.5) else: x_tr = torch.tanh(self.transform(x)) weights = self.context_vector(x_tr) weights = torch.sigmoid(weights) x = x * weights return x, weights def reset_parameters(self): if self.norm: nn.init.normal_(self.context_vector.weight) self.constrain(self.context_vector) else: nn.init.xavier_uniform_(self.context_vector.weight) nn.init.xavier_uniform_(self.transform.weight) nn.init.zeros_(self.transform.bias) def apply_contraint(self): if self.norm: self.constrain(self.context_vector) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'dims': 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_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) @triton.jit def triton_poi_fused_sigmoid_1(in_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_out_ptr0 + x0, xmask) tmp1 = tl.sigmoid(tmp0) tl.store(in_out_ptr0 + x0, tmp1, xmask) @triton.jit def triton_poi_fused_mul_2(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 x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 * tmp1 tl.store(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, 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, (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_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 get_raw_stream(0) triton_poi_fused_tanh_0[grid(256)](buf1, primals_2, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 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) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf2 triton_poi_fused_sigmoid_1[grid(64)](buf3, 64, XBLOCK=64, num_warps =1, num_stages=1) buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_mul_2[grid(256)](primals_3, buf3, buf4, 256, XBLOCK=128, num_warps=4, num_stages=1) return buf4, buf3, primals_3, buf1, buf3, primals_4 class AttentionNew(nn.Module): def __init__(self, dims, norm=False): super(AttentionNew, self).__init__() self.norm = norm if self.norm: self.constrain = L2Constrain() else: self.transform = nn.Linear(dims, dims) self.context_vector = nn.Linear(dims, 1, bias=False) self.reset_parameters() def reset_parameters(self): if self.norm: nn.init.normal_(self.context_vector.weight) self.constrain(self.context_vector) else: nn.init.xavier_uniform_(self.context_vector.weight) nn.init.xavier_uniform_(self.transform.weight) nn.init.zeros_(self.transform.bias) def apply_contraint(self): if self.norm: self.constrain(self.context_vector) def forward(self, input_0): primals_1 = self.transform.weight primals_2 = self.transform.bias primals_4 = self.context_vector.weight primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0], output[1]
glee1228/segment_temporal_context_aggregation
Attention
false
6,749
[ "Apache-2.0" ]
1
e5778f848f1cfd89bd1f77beb5e1b38a66a2f13d
https://github.com/glee1228/segment_temporal_context_aggregation/tree/e5778f848f1cfd89bd1f77beb5e1b38a66a2f13d
Sparsemax
from torch.autograd import Function import torch import torch.nn.init import torch.nn as nn def _make_ix_like(input, dim=0): d = input.size(dim) rho = torch.arange(1, d + 1, device=input.device, dtype=input.dtype) view = [1] * input.dim() view[0] = -1 return rho.view(view).transpose(0, dim) def _threshold_and_support(input, dim=0): """Sparsemax building block: compute the threshold Args: input: any dimension dim: dimension along which to apply the sparsemax Returns: the threshold value """ input_srt, _ = torch.sort(input, descending=True, dim=dim) input_cumsum = input_srt.cumsum(dim) - 1 rhos = _make_ix_like(input, dim) support = rhos * input_srt > input_cumsum support_size = support.sum(dim=dim).unsqueeze(dim) tau = input_cumsum.gather(dim, support_size - 1) tau /= support_size return tau, support_size def sparsemax(input, dim=0): return SparsemaxFunction.apply(input, dim) class SparsemaxFunction(Function): @staticmethod def forward(ctx, input, dim=0): """sparsemax: normalizing sparse transform (a la softmax) Parameters: input (Tensor): any shape dim: dimension along which to apply sparsemax Returns: output (Tensor): same shape as input """ ctx.dim = dim max_val, _ = input.max(dim=dim, keepdim=True) input -= max_val tau, supp_size = _threshold_and_support(input, dim=dim) output = torch.clamp(input - tau, min=0) ctx.save_for_backward(supp_size, output) return output @staticmethod def backward(ctx, grad_output): supp_size, output = ctx.saved_tensors dim = ctx.dim grad_input = grad_output.clone() grad_input[output == 0] = 0 v_hat = grad_input.sum(dim=dim) / supp_size.squeeze() v_hat = v_hat.unsqueeze(dim) grad_input = torch.where(output != 0, grad_input - v_hat, grad_input) return grad_input, None class Sparsemax(nn.Module): def __init__(self, dim=0): self.dim = dim super(Sparsemax, self).__init__() def forward(self, input): return sparsemax(input, 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.autograd import Function import torch.nn.init 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_max_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 x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (64 + x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (128 + x0), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (192 + x0), 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_helper_fn_add0(arg0_0, arg1_0): tmp0 = arg0_0 + arg1_0 return tmp0 @triton.jit def triton_per_fused_cumsum_sort_1(in_ptr0, out_ptr0, out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 64 RBLOCK: tl.constexpr = 4 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 + (x0 + 64 * r1), xmask, other=0.0) tmp1 = r1 tmp2 = tmp1.to(tl.int16) tmp3 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp4 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp5, _tmp6 = triton_helpers.sort_with_index(tmp3, tmp4, None, 1, stable=False, descending=True) tmp7 = tmp5.to(tl.float32) tmp8 = tl.broadcast_to(tmp7, [XBLOCK, RBLOCK]) tmp9, = tl.associative_scan((tmp8,), 1, _triton_helper_fn_add0) tl.store(out_ptr0 + (x0 + 64 * r1), tmp5, xmask) tl.store(out_ptr1 + (x0 + 64 * r1), tmp9, xmask) @triton.jit def triton_poi_fused_gt_mul_sub_sum_2(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 tmp0 = tl.load(in_ptr0 + x0, xmask) tmp3 = tl.load(in_ptr1 + x0, xmask) tmp7 = tl.load(in_ptr0 + (64 + x0), xmask) tmp10 = tl.load(in_ptr1 + (64 + x0), xmask) tmp15 = tl.load(in_ptr0 + (128 + x0), xmask) tmp18 = tl.load(in_ptr1 + (128 + x0), xmask) tmp23 = tl.load(in_ptr0 + (192 + x0), xmask) tmp26 = tl.load(in_ptr1 + (192 + x0), xmask) tmp1 = 1.0 tmp2 = tmp1 * tmp0 tmp4 = tmp3 - tmp1 tmp5 = tmp2 > tmp4 tmp6 = tmp5.to(tl.int64) tmp8 = 2.0 tmp9 = tmp8 * tmp7 tmp11 = tmp10 - tmp1 tmp12 = tmp9 > tmp11 tmp13 = tmp12.to(tl.int64) tmp14 = tmp6 + tmp13 tmp16 = 3.0 tmp17 = tmp16 * tmp15 tmp19 = tmp18 - tmp1 tmp20 = tmp17 > tmp19 tmp21 = tmp20.to(tl.int64) tmp22 = tmp14 + tmp21 tmp24 = 4.0 tmp25 = tmp24 * tmp23 tmp27 = tmp26 - tmp1 tmp28 = tmp25 > tmp27 tmp29 = tmp28.to(tl.int64) tmp30 = tmp22 + tmp29 tl.store(out_ptr0 + x0, tmp30, xmask) @triton.jit def triton_poi_fused_clamp_div_gather_sub_3(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 x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tl.full([1], 1, tl.int64) tmp3 = tmp1 - tmp2 tmp4 = tl.full([XBLOCK], 4, tl.int32) tmp5 = tmp3 + tmp4 tmp6 = tmp3 < 0 tmp7 = tl.where(tmp6, tmp5, tmp3) tl.device_assert((0 <= tmp7) & (tmp7 < 4) | ~xmask, 'index out of bounds: 0 <= tmp7 < 4') tmp9 = tl.load(in_ptr2 + (x0 + 64 * tmp7), xmask) tmp10 = 1.0 tmp11 = tmp9 - tmp10 tmp12 = tmp1.to(tl.float32) tmp13 = tmp11 / tmp12 tmp14 = tmp0 - tmp13 tmp15 = 0.0 tmp16 = triton_helpers.maximum(tmp14, tmp15) tl.store(out_ptr0 + x2, tmp16, xmask) tl.store(out_ptr1 + x2, tmp0, 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_max_sub_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_per_fused_cumsum_sort_1[grid(64)](buf0, buf1, buf3, 64, 4, XBLOCK=32, num_warps=2, num_stages=1) buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.int64) triton_poi_fused_gt_mul_sub_sum_2[grid(64)](buf1, buf3, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) buf5 = buf1 del buf1 triton_poi_fused_clamp_div_gather_sub_3[grid(256)](buf0, buf4, buf3, buf5, arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del buf0 del buf3 del buf4 return buf5, def _make_ix_like(input, dim=0): d = input.size(dim) rho = torch.arange(1, d + 1, device=input.device, dtype=input.dtype) view = [1] * input.dim() view[0] = -1 return rho.view(view).transpose(0, dim) def _threshold_and_support(input, dim=0): """Sparsemax building block: compute the threshold Args: input: any dimension dim: dimension along which to apply the sparsemax Returns: the threshold value """ input_srt, _ = torch.sort(input, descending=True, dim=dim) input_cumsum = input_srt.cumsum(dim) - 1 rhos = _make_ix_like(input, dim) support = rhos * input_srt > input_cumsum support_size = support.sum(dim=dim).unsqueeze(dim) tau = input_cumsum.gather(dim, support_size - 1) tau /= support_size return tau, support_size def sparsemax(input, dim=0): return SparsemaxFunction.apply(input, dim) class SparsemaxFunction(Function): @staticmethod def forward(ctx, input, dim=0): """sparsemax: normalizing sparse transform (a la softmax) Parameters: input (Tensor): any shape dim: dimension along which to apply sparsemax Returns: output (Tensor): same shape as input """ ctx.dim = dim max_val, _ = input.max(dim=dim, keepdim=True) input -= max_val tau, supp_size = _threshold_and_support(input, dim=dim) output = torch.clamp(input - tau, min=0) ctx.save_for_backward(supp_size, output) return output @staticmethod def backward(ctx, grad_output): supp_size, output = ctx.saved_tensors dim = ctx.dim grad_input = grad_output.clone() grad_input[output == 0] = 0 v_hat = grad_input.sum(dim=dim) / supp_size.squeeze() v_hat = v_hat.unsqueeze(dim) grad_input = torch.where(output != 0, grad_input - v_hat, grad_input) return grad_input, None class SparsemaxNew(nn.Module): def __init__(self, dim=0): self.dim = dim super(SparsemaxNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
gililior/qasrl-modeling
Sparsemax
false
6,750
[ "MIT" ]
1
2f9684536f6d5f0283b0e4b90a911ea12fa72f72
https://github.com/gililior/qasrl-modeling/tree/2f9684536f6d5f0283b0e4b90a911ea12fa72f72
ActorNetwork
import torch import torch.nn as nn import torch.nn.functional as F import torch.utils.data class ActorNetwork(nn.Module): def __init__(self, state_size, action_size, hidden_size, init_w=0.003, log_std_min=-20, log_std_max=2): super(ActorNetwork, self).__init__() self.log_std_min = log_std_min self.log_std_max = log_std_max self.linear1 = nn.Linear(state_size, hidden_size) self.linear2 = nn.Linear(hidden_size, hidden_size) self.mean_linear = nn.Linear(hidden_size, action_size) self.mean_linear.weight.data.uniform_(-init_w, init_w) self.mean_linear.bias.data.uniform_(-init_w, init_w) self.log_std_linear = nn.Linear(hidden_size, action_size) self.log_std_linear.weight.data.uniform_(-init_w, init_w) self.log_std_linear.bias.data.uniform_(-init_w, init_w) def forward(self, state): x = F.relu(self.linear1(state)) x = F.relu(self.linear2(x)) mean = self.mean_linear(x) log_std = self.log_std_linear(x) log_std = torch.clamp(log_std, self.log_std_min, self.log_std_max) return mean, log_std def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'state_size': 4, 'action_size': 4, '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 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_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) @triton.jit def triton_poi_fused_clamp_ge_le_logical_and_1(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 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 tmp3 = -20.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = 2.0 tmp6 = triton_helpers.minimum(tmp4, tmp5) tmp7 = tmp2 >= tmp3 tmp8 = tmp2 <= tmp5 tmp9 = tmp7 & tmp8 tl.store(out_ptr0 + x2, tmp6, xmask) tl.store(out_ptr1 + x2, tmp9, 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, (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,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4,), (1,)) assert_size_stride(primals_8, (4, 4), (4, 1)) assert_size_stride(primals_9, (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 buf9 = 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, buf9, 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 buf8 = 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, buf8, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf4) del primals_7 buf5 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_8, (4, 4), (1, 4), 0), out=buf5) buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_clamp_ge_le_logical_and_1[grid(256)](buf5, primals_9, buf6, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf5 del primals_9 return reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), buf6, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 4), (4, 1), 0), reinterpret_tensor( buf3, (64, 4), (4, 1), 0 ), buf7, primals_8, primals_6, buf8, primals_4, buf9 class ActorNetworkNew(nn.Module): def __init__(self, state_size, action_size, hidden_size, init_w=0.003, log_std_min=-20, log_std_max=2): super(ActorNetworkNew, self).__init__() self.log_std_min = log_std_min self.log_std_max = log_std_max self.linear1 = nn.Linear(state_size, hidden_size) self.linear2 = nn.Linear(hidden_size, hidden_size) self.mean_linear = nn.Linear(hidden_size, action_size) self.mean_linear.weight.data.uniform_(-init_w, init_w) self.mean_linear.bias.data.uniform_(-init_w, init_w) self.log_std_linear = nn.Linear(hidden_size, action_size) self.log_std_linear.weight.data.uniform_(-init_w, init_w) self.log_std_linear.bias.data.uniform_(-init_w, init_w) def forward(self, input_0): primals_1 = self.linear1.weight primals_2 = self.linear1.bias primals_4 = self.linear2.weight primals_5 = self.linear2.bias primals_6 = self.mean_linear.weight primals_7 = self.mean_linear.bias primals_8 = self.log_std_linear.weight primals_9 = self.log_std_linear.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], output[1]
godnpeter/DMC_Clustering_PICA
ActorNetwork
false
6,751
[ "BSD-2-Clause" ]
1
1b3e14dd4034f3941af1caa06c1d4b6f9d606408
https://github.com/godnpeter/DMC_Clustering_PICA/tree/1b3e14dd4034f3941af1caa06c1d4b6f9d606408
Net
import torch import torch.nn.functional as F import torch.nn as nn class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(1, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.conv2_drop = nn.Dropout2d() self.fc1 = nn.Linear(320, 150) self.fc2 = nn.Linear(150, 10) def forward(self, x): x = F.relu(F.max_pool2d(self.conv1(x), 2)) x = F.relu(F.adaptive_max_pool2d(self.conv2_drop(self.conv2(x)), 4)) x = x.view(-1, 320) x = F.relu(self.fc1(x)) x = F.dropout(x, training=self.training) x = self.fc2(x) return F.log_softmax(x, dim=-1) def get_inputs(): return [torch.rand([4, 1, 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 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_convolution_0(in_out_ptr0, in_ptr0, 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 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_max_pool2d_with_indices_relu_1(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 36000 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 30 x3 = xindex // 30 x2 = xindex // 9000 x4 = xindex % 9000 x5 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 120 * x3), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 120 * x3), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (60 + 2 * x0 + 120 * x3), xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (61 + 2 * x0 + 120 * x3), 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) tmp17 = tl.full([1], 0, tl.int32) tmp18 = triton_helpers.maximum(tmp17, tmp16) tl.store(out_ptr0 + (x4 + 9088 * x2), tmp15, xmask) tl.store(out_ptr1 + x5, tmp18, xmask) @triton.jit def triton_poi_fused_convolution_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 54080 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 676 % 20 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_relu_threshold_backward_3(in_out_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1280 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) @triton.jit def triton_poi_fused_relu_4(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 600 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 150 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_per_fused__log_softmax_5(in_ptr0, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 rnumel = 10 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, :] rmask = rindex < rnumel r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 10 * x0), rmask & xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(rmask & xmask, tmp1, float('-inf')) tmp4 = triton_helpers.max2(tmp3, 1)[:, None] tmp5 = tmp0 - tmp4 tmp6 = tl_math.exp(tmp5) tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK]) tmp9 = tl.where(rmask & xmask, tmp7, 0) tmp10 = tl.sum(tmp9, 1)[:, None] tmp11 = tl_math.log(tmp10) tmp12 = tmp5 - tmp11 tl.store(out_ptr2 + (r1 + 10 * x0), tmp12, rmask & 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, 1, 5, 5), (25, 25, 5, 1)) assert_size_stride(primals_2, (10,), (1,)) assert_size_stride(primals_3, (4, 1, 64, 64), (4096, 4096, 64, 1)) assert_size_stride(primals_4, (20, 10, 5, 5), (250, 25, 5, 1)) assert_size_stride(primals_5, (20,), (1,)) assert_size_stride(primals_6, (150, 320), (320, 1)) assert_size_stride(primals_7, (150,), (1,)) assert_size_stride(primals_8, (10, 150), (150, 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, 10, 60, 60), (36000, 3600, 60, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(144000)](buf1, primals_2, 144000, XBLOCK=512, num_warps=8, num_stages=1) del primals_2 buf2 = empty_strided_cuda((4, 10, 30, 30), (9088, 900, 30, 1), torch.int8) buf3 = empty_strided_cuda((4, 10, 30, 30), (9000, 900, 30, 1), torch.float32) triton_poi_fused_max_pool2d_with_indices_relu_1[grid(36000)](buf1, buf2, buf3, 36000, XBLOCK=256, num_warps=4, num_stages=1) buf4 = extern_kernels.convolution(buf3, 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, 20, 26, 26), (13520, 676, 26, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_2[grid(54080)](buf5, primals_5, 54080, XBLOCK=512, num_warps=4, num_stages=1) del primals_5 buf6 = torch.ops.aten.adaptive_max_pool2d.default(buf5, [4, 4]) buf7 = buf6[0] buf8 = buf6[1] del buf6 buf9 = buf7 del buf7 buf16 = empty_strided_cuda((4, 20, 4, 4), (320, 16, 4, 1), torch.bool) triton_poi_fused_relu_threshold_backward_3[grid(1280)](buf9, buf16, 1280, XBLOCK=256, num_warps=4, num_stages=1) buf10 = empty_strided_cuda((4, 150), (150, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf9, (4, 320), (320, 1), 0), reinterpret_tensor(primals_6, (320, 150), (1, 320), 0), out=buf10) buf11 = buf10 del buf10 triton_poi_fused_relu_4[grid(600)](buf11, primals_7, 600, XBLOCK= 256, num_warps=4, num_stages=1) del primals_7 buf12 = empty_strided_cuda((4, 10), (10, 1), torch.float32) extern_kernels.addmm(primals_9, buf11, reinterpret_tensor(primals_8, (150, 10), (1, 150), 0), alpha=1, beta=1, out=buf12) del primals_9 buf15 = empty_strided_cuda((4, 10), (10, 1), torch.float32) triton_per_fused__log_softmax_5[grid(4)](buf12, buf15, 4, 10, XBLOCK=1, num_warps=2, num_stages=1) del buf12 return (buf15, primals_1, primals_3, primals_4, buf1, buf2, buf3, buf5, buf8, reinterpret_tensor(buf9, (4, 320), (320, 1), 0), buf11, buf15, primals_8, primals_6, buf16) class NetNew(nn.Module): def __init__(self): super(NetNew, self).__init__() self.conv1 = nn.Conv2d(1, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.conv2_drop = nn.Dropout2d() self.fc1 = nn.Linear(320, 150) self.fc2 = nn.Linear(150, 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]
generall/Torchlite
Net
false
6,752
[ "MIT" ]
1
2eb3e2a20b7619bd58b0b0fca120e2aefca0e79a
https://github.com/generall/Torchlite/tree/2eb3e2a20b7619bd58b0b0fca120e2aefca0e79a
ClassificationSubNet
import torch import numpy as np import torch.nn as nn class ClassificationSubNet(nn.Module): def __init__(self, in_channels, num_classes, num_anchors=9): super().__init__() self.num_classes = num_classes self.conv2d_1 = nn.Conv2d(in_channels, 256, 3, padding=1) nn.init.normal_(self.conv2d_1.weight.data, std=0.01) nn.init.zeros_(self.conv2d_1.bias.data) self.conv2d_2 = nn.Conv2d(256, 256, 3, padding=1) nn.init.normal_(self.conv2d_2.weight.data, std=0.01) nn.init.zeros_(self.conv2d_2.bias.data) self.conv2d_3 = nn.Conv2d(256, 256, 3, padding=1) nn.init.normal_(self.conv2d_3.weight.data, std=0.01) nn.init.zeros_(self.conv2d_3.bias.data) self.conv2d_4 = nn.Conv2d(256, 256, 3, padding=1) nn.init.normal_(self.conv2d_4.weight.data, std=0.01) nn.init.zeros_(self.conv2d_4.bias.data) self.conv2d_5 = nn.Conv2d(256, num_classes * num_anchors, 3, padding=1) nn.init.normal_(self.conv2d_5.weight.data, std=0.01) nn.init.constant_(self.conv2d_5.bias.data, val=np.log(1 / 99)) def forward(self, x): x = self.conv2d_1(x) x = nn.functional.relu(x) x = self.conv2d_2(x) x = nn.functional.relu(x) x = self.conv2d_3(x) x = nn.functional.relu(x) x = self.conv2d_4(x) x = nn.functional.relu(x) x = self.conv2d_5(x) x = torch.sigmoid(x) x = x.permute(0, 2, 3, 1) return x.reshape(x.size(0), -1, self.num_classes) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 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 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 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_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 2304 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 36 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__unsafe_view_clone_2(in_out_ptr0, in_ptr0, 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.sigmoid(tmp0) tl.debug_barrier() tl.store(in_out_ptr0 + (x2 + 36 * y3), tmp1, 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 = buf8 del buf8 triton_poi_fused_convolution_1[grid(2304)](buf9, primals_11, 2304, XBLOCK=256, num_warps=4, num_stages=1) del primals_11 buf10 = empty_strided_cuda((4, 4, 4, 36), (576, 144, 36, 1), torch. float32) buf11 = reinterpret_tensor(buf10, (4, 144, 4), (576, 4, 1), 0) del buf10 triton_poi_fused__unsafe_view_clone_2[grid(64, 36)](buf11, buf9, 64, 36, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1) return (buf11, primals_1, primals_3, primals_4, primals_6, primals_8, primals_10, buf1, buf3, buf5, buf7, buf9) class ClassificationSubNetNew(nn.Module): def __init__(self, in_channels, num_classes, num_anchors=9): super().__init__() self.num_classes = num_classes self.conv2d_1 = nn.Conv2d(in_channels, 256, 3, padding=1) nn.init.normal_(self.conv2d_1.weight.data, std=0.01) nn.init.zeros_(self.conv2d_1.bias.data) self.conv2d_2 = nn.Conv2d(256, 256, 3, padding=1) nn.init.normal_(self.conv2d_2.weight.data, std=0.01) nn.init.zeros_(self.conv2d_2.bias.data) self.conv2d_3 = nn.Conv2d(256, 256, 3, padding=1) nn.init.normal_(self.conv2d_3.weight.data, std=0.01) nn.init.zeros_(self.conv2d_3.bias.data) self.conv2d_4 = nn.Conv2d(256, 256, 3, padding=1) nn.init.normal_(self.conv2d_4.weight.data, std=0.01) nn.init.zeros_(self.conv2d_4.bias.data) self.conv2d_5 = nn.Conv2d(256, num_classes * num_anchors, 3, padding=1) nn.init.normal_(self.conv2d_5.weight.data, std=0.01) nn.init.constant_(self.conv2d_5.bias.data, val=np.log(1 / 99)) def forward(self, input_0): primals_1 = self.conv2d_1.weight primals_2 = self.conv2d_1.bias primals_4 = self.conv2d_2.weight primals_5 = self.conv2d_2.bias primals_6 = self.conv2d_3.weight primals_7 = self.conv2d_3.bias primals_8 = self.conv2d_4.weight primals_9 = self.conv2d_4.bias primals_10 = self.conv2d_5.weight primals_11 = self.conv2d_5.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]
geez0219/ARC
ClassificationSubNet
false
6,753
[ "Apache-2.0" ]
1
f2176f0d442d4a2d6028f0770b1efc1a9ae982b8
https://github.com/geez0219/ARC/tree/f2176f0d442d4a2d6028f0770b1efc1a9ae982b8
ZeroConv1d
import torch import torch.nn as nn class ZeroConv1d(nn.Module): def __init__(self, in_channel, out_channel): super().__init__() self.conv = nn.Conv1d(in_channel, out_channel, 1, padding=0) self.conv.weight.data.zero_() self.conv.bias.data.zero_() self.scale = nn.Parameter(torch.zeros(1, out_channel, 1)) def forward(self, x): out = self.conv(x) out = out * torch.exp(self.scale * 3) return out def get_inputs(): return [torch.rand([4, 4])] def get_init_inputs(): return [[], {'in_channel': 4, 'out_channel': 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_convolution_exp_mul_0(in_out_ptr0, 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 x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_out_ptr0 + x2, 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 + x2, tmp2, xmask) tl.store(out_ptr0 + x2, tmp7, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (1, 4, 1), (4, 1, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(reinterpret_tensor(primals_3, (1, 4, 4), (16, 4, 1), 0), primals_1, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf0, (1, 4, 4), (16, 4, 1)) buf1 = buf0 del buf0 buf2 = empty_strided_cuda((1, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_convolution_exp_mul_0[grid(16)](buf1, primals_2, primals_4, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_2 return buf2, primals_1, primals_4, reinterpret_tensor(primals_3, (1, 4, 4), (16, 4, 1), 0), buf1 class ZeroConv1dNew(nn.Module): def __init__(self, in_channel, out_channel): super().__init__() self.conv = nn.Conv1d(in_channel, out_channel, 1, padding=0) self.conv.weight.data.zero_() self.conv.bias.data.zero_() self.scale = nn.Parameter(torch.zeros(1, out_channel, 1)) def forward(self, input_0): primals_4 = self.scale primals_1 = self.conv.weight primals_2 = self.conv.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
gorinars/VQ-VAE-Speech
ZeroConv1d
false
6,754
[ "MIT" ]
1
60398f03eb129195bce402a423ace8cca8995f3c
https://github.com/gorinars/VQ-VAE-Speech/tree/60398f03eb129195bce402a423ace8cca8995f3c
Conv
import torch import torch.nn as nn class Conv(nn.Module): def __init__(self, in_channels, out_channels, kernel_size=3, dilation=1, causal=True): super(Conv, self).__init__() self.causal = causal if self.causal: self.padding = dilation * (kernel_size - 1) else: self.padding = dilation * (kernel_size - 1) // 2 self.conv = nn.Conv1d(in_channels, out_channels, kernel_size, dilation=dilation, padding=self.padding) self.conv = nn.utils.weight_norm(self.conv) nn.init.kaiming_normal_(self.conv.weight) def forward(self, tensor): out = self.conv(tensor) if self.causal and self.padding != 0: out = out[:, :, :-self.padding] return out def get_inputs(): return [torch.rand([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 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_per_fused__weight_norm_interface_0(in_out_ptr0, in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 rnumel = 12 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, :] rmask = rindex < rnumel r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 12 * x0), rmask & xmask, other=0.0) tmp7 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp1 = tmp0 * tmp0 tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp4 = tl.where(rmask & xmask, tmp2, 0) tmp5 = tl.sum(tmp4, 1)[:, None] tmp6 = libdevice.sqrt(tmp5) tmp8 = tmp7 / tmp6 tmp9 = tmp0 * tmp8 tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp6, xmask) tl.store(out_ptr0 + (r1 + 12 * x0), tmp9, rmask & xmask) @triton.jit def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 96 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 6 % 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) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 1, 1), (1, 1, 1)) assert_size_stride(primals_2, (4, 4, 3), (12, 3, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4, 4), (16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 1, 1), (1, 4, 4), torch.float32) buf1 = reinterpret_tensor(buf0, (4, 1, 1), (1, 1, 1), 0) del buf0 buf2 = empty_strided_cuda((4, 4, 3), (12, 3, 1), torch.float32) get_raw_stream(0) triton_per_fused__weight_norm_interface_0[grid(4)](buf1, primals_2, primals_1, buf2, 4, 12, XBLOCK=1, num_warps=2, num_stages=1) buf3 = extern_kernels.convolution(primals_4, buf2, stride=(1,), padding=(2,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf3, (4, 4, 6), (24, 6, 1)) buf4 = buf3 del buf3 triton_poi_fused_convolution_1[grid(96)](buf4, primals_3, 96, XBLOCK=128, num_warps=4, num_stages=1) del primals_3 return reinterpret_tensor(buf4, (4, 4, 4), (24, 6, 1), 0 ), buf2, primals_1, primals_2, primals_4, buf1, buf2 class ConvNew(nn.Module): def __init__(self, in_channels, out_channels, kernel_size=3, dilation=1, causal=True): super(ConvNew, self).__init__() self.causal = causal if self.causal: self.padding = dilation * (kernel_size - 1) else: self.padding = dilation * (kernel_size - 1) // 2 self.conv = nn.Conv1d(in_channels, out_channels, kernel_size, dilation=dilation, padding=self.padding) self.conv = nn.utils.weight_norm(self.conv) nn.init.kaiming_normal_(self.conv.weight) def forward(self, input_0): primals_3 = self.conv.bias primals_1 = self.conv.weight_g primals_2 = self.conv.weight_v primals_4 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
gorinars/VQ-VAE-Speech
Conv
false
6,755
[ "MIT" ]
1
60398f03eb129195bce402a423ace8cca8995f3c
https://github.com/gorinars/VQ-VAE-Speech/tree/60398f03eb129195bce402a423ace8cca8995f3c
HingeLoss
import torch import torch.utils.data from torch import nn import torch import torch.nn.parallel import torch.optim class HingeLoss(nn.Module): def __init__(self): super(HingeLoss, self).__init__() self.margin = 1.0 def hinge_loss(self, input, target): output = self.margin - input.mul(target) output[output.le(0)] = 0 return output.mean() def forward(self, input, target): return self.hinge_loss(input, target) 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.utils.data from torch import nn import torch import torch.nn.parallel import torch.optim 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_index_put_lift_fresh_mean_mul_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) tmp1 = tl.load(in_ptr1 + r0, None) tmp2 = tmp0 * tmp1 tmp3 = 1.0 tmp4 = tmp3 - tmp2 tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tmp7 = tl.where(tmp6, tmp5, tmp4) tmp8 = tl.broadcast_to(tmp7, [RBLOCK]) tmp10 = triton_helpers.promote_to_tensor(tl.sum(tmp8, 0)) tmp11 = 256.0 tmp12 = tmp10 / tmp11 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp12, 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 get_raw_stream(0) triton_per_fused_index_put_lift_fresh_mean_mul_rsub_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 HingeLossNew(nn.Module): def __init__(self): super(HingeLossNew, self).__init__() self.margin = 1.0 def hinge_loss(self, input, target): output = self.margin - input.mul(target) output[output.le(0)] = 0 return output.mean() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
graphbuilder/BNN
HingeLoss
false
6,756
[ "MIT" ]
1
d99eb5c7ef19f8b0c14a135d40a489f154a3c894
https://github.com/graphbuilder/BNN/tree/d99eb5c7ef19f8b0c14a135d40a489f154a3c894
ContrastiveLoss
import torch import torch.utils.data class ContrastiveLoss(torch.nn.Module): """ Contrastive loss function. """ def __init__(self, margin=1.0): super(ContrastiveLoss, self).__init__() self.margin = margin def forward(self, x0, x1, y): diff = x0 - x1 dist_sq = torch.sum(torch.pow(diff, 2), 1) dist = torch.sqrt(dist_sq) mdist = self.margin - dist dist = torch.clamp(mdist, min=0.0) loss = y * dist_sq + (1 - y) * torch.pow(dist, 2) loss = torch.sum(loss) / 2.0 / x0.size()[0] 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.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_pow_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 tl.store(out_ptr0 + x2, tmp18, xmask) @triton.jit def triton_per_fused_add_clamp_div_mul_pow_rsub_sqrt_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) tmp1 = tl.load(in_ptr1 + r0, None, eviction_policy='evict_last') tmp2 = tmp0 * tmp1 tmp3 = 1.0 tmp4 = tmp3 - tmp0 tmp5 = libdevice.sqrt(tmp1) tmp6 = tmp3 - tmp5 tmp7 = 0.0 tmp8 = triton_helpers.maximum(tmp6, tmp7) tmp9 = tmp8 * tmp8 tmp10 = tmp4 * tmp9 tmp11 = tmp2 + 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_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_sqrt_sum_1[grid(1)](buf2, arg2_1, buf0, 1, 256, num_warps=2, num_stages=1) del arg2_1 del buf0 return buf2, class ContrastiveLossNew(torch.nn.Module): """ Contrastive loss function. """ def __init__(self, margin=1.0): super(ContrastiveLossNew, self).__init__() self.margin = margin 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]
guruprasaad123/all_dl_projects
ContrastiveLoss
false
6,757
[ "Apache-2.0" ]
1
04c869f7f001ef94c467740260663d91a34815e0
https://github.com/guruprasaad123/all_dl_projects/tree/04c869f7f001ef94c467740260663d91a34815e0
AlignQuestionEmbedding
import torch import torch.nn.functional as F from torch import nn class AlignQuestionEmbedding(nn.Module): def __init__(self, input_dim): super().__init__() self.linear = nn.Linear(input_dim, input_dim) self.relu = nn.ReLU() def forward(self, context, question, question_mask): ctx_ = self.linear(context) ctx_ = self.relu(ctx_) qtn_ = self.linear(question) qtn_ = self.relu(qtn_) qtn_transpose = qtn_.permute(0, 2, 1) align_scores = torch.bmm(ctx_, qtn_transpose) qtn_mask = question_mask.unsqueeze(1).expand(align_scores.size()) align_scores = align_scores.masked_fill(qtn_mask == 1, -float('inf')) align_scores_flat = align_scores.view(-1, question.size(1)) alpha = F.softmax(align_scores_flat, dim=1) alpha = alpha.view(-1, context.shape[1], question.shape[1]) align_embedding = torch.bmm(alpha, question) return align_embedding def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'input_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 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_relu_threshold_backward_0(in_out_ptr0, in_out_ptr1, 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 x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_out_ptr1 + x2, xmask) tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = tmp5 + tmp1 tmp7 = triton_helpers.maximum(tmp3, tmp6) tmp8 = 0.0 tmp9 = tmp7 <= tmp8 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(in_out_ptr1 + x2, tmp7, xmask) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, 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 + 4 * (x0 // 4), xmask, eviction_policy='evict_last' ) tmp3 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (1 + 4 * (x0 // 4)), xmask, eviction_policy= 'evict_last') tmp8 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (2 + 4 * (x0 // 4)), xmask, eviction_policy= 'evict_last') tmp13 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp16 = tl.load(in_ptr0 + (3 + 4 * (x0 // 4)), xmask, eviction_policy= 'evict_last') tmp18 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp1 = 1.0 tmp2 = tmp0 == tmp1 tmp4 = float('-inf') tmp5 = tl.where(tmp2, tmp4, tmp3) tmp7 = tmp6 == tmp1 tmp9 = tl.where(tmp7, tmp4, tmp8) tmp10 = triton_helpers.maximum(tmp5, tmp9) tmp12 = tmp11 == tmp1 tmp14 = tl.where(tmp12, tmp4, tmp13) tmp15 = triton_helpers.maximum(tmp10, tmp14) tmp17 = tmp16 == tmp1 tmp19 = tl.where(tmp17, tmp4, tmp18) tmp20 = triton_helpers.maximum(tmp15, tmp19) tmp21 = tmp5 - tmp20 tmp22 = tl_math.exp(tmp21) tmp23 = tmp9 - tmp20 tmp24 = tl_math.exp(tmp23) tmp25 = tmp22 + tmp24 tmp26 = tmp14 - tmp20 tmp27 = tl_math.exp(tmp26) tmp28 = tmp25 + tmp27 tmp29 = tmp19 - tmp20 tmp30 = tl_math.exp(tmp29) tmp31 = tmp28 + tmp30 tl.store(out_ptr0 + x0, tmp20, xmask) tl.store(out_ptr1 + x0, tmp31, xmask) @triton.jit def triton_poi_fused__softmax_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * (x1 // 4)), xmask) tmp3 = tl.load(in_ptr1 + x2, xmask) tmp6 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp1 = 1.0 tmp2 = tmp0 == tmp1 tmp4 = float('-inf') tmp5 = tl.where(tmp2, tmp4, tmp3) tmp7 = tmp5 - tmp6 tmp8 = tl_math.exp(tmp7) tmp10 = tmp8 / tmp9 tl.store(out_ptr0 + x2, 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), (16, 4, 1)) assert_size_stride(primals_4, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_5, (4, 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_3, (16, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_4, (16, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf2) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 0) del buf0 buf3 = reinterpret_tensor(buf2, (4, 4, 4), (16, 4, 1), 0) del buf2 buf9 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(64)](buf1, buf3, primals_2, buf9, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_2 buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf1, reinterpret_tensor(buf3, (4, 4, 4), (16, 1, 4), 0), out=buf4) buf5 = empty_strided_cuda((16, 1), (1, 16), torch.float32) buf6 = empty_strided_cuda((16, 1), (1, 16), torch.float32) triton_poi_fused__softmax_1[grid(16)](primals_5, buf4, buf5, buf6, 16, XBLOCK=16, num_warps=1, num_stages=1) buf7 = empty_strided_cuda((16, 4), (4, 1), torch.float32) triton_poi_fused__softmax_2[grid(64)](primals_5, buf4, buf5, buf6, buf7, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf5 del buf6 buf8 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf7, (4, 4, 4), (16, 4, 1), 0), primals_4, out=buf8) del buf7 return buf8, primals_4, primals_5, reinterpret_tensor(primals_3, (16, 4 ), (4, 1), 0), buf1, buf4, buf3, buf9 class AlignQuestionEmbeddingNew(nn.Module): def __init__(self, input_dim): super().__init__() self.linear = nn.Linear(input_dim, input_dim) self.relu = nn.ReLU() def forward(self, input_0, input_1, input_2): primals_1 = self.linear.weight primals_2 = self.linear.bias primals_3 = input_0 primals_4 = input_1 primals_5 = input_2 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
gustavhartz/legal-contract-elements
AlignQuestionEmbedding
false
6,758
[ "MIT" ]
1
7a1e1f0024f9d336c7166f51b4325acf03db86a2
https://github.com/gustavhartz/legal-contract-elements/tree/7a1e1f0024f9d336c7166f51b4325acf03db86a2
BasicBlock
import torch import torch.utils.data from torch import nn import torch import torch.nn.parallel import torch.optim def Binarize(tensor, quant_mode='det'): if quant_mode == 'det': tensor = tensor.sign() zero = torch.zeros_like(tensor) one = torch.ones_like(tensor) zero - one tensor = torch.where(tensor == 0, one, tensor) return tensor else: return tensor.add_(1).div_(2).add_(torch.rand(tensor.size()).add(-0.5) ).clamp_(0, 1).round().mul_(2).add_(-1) def Binaryconv3x3(in_planes, out_planes, stride=1): """3x3 convolution with padding""" return BinarizeConv2d(in_planes, out_planes, kernel_size=3, stride= stride, padding=1, bias=False) class BinarizeConv2d(nn.Conv2d): def __init__(self, *kargs, **kwargs): super(BinarizeConv2d, self).__init__(*kargs, **kwargs) def forward(self, input): input.data = Binarize(input.data) if not hasattr(self.weight, 'org'): self.weight.org = self.weight.data.clone() self.weight.data = Binarize(self.weight.org) out = nn.functional.conv2d(input, self.weight, None, self.stride, self.padding, self.dilation, self.groups) if self.bias is not None: self.bias.org = self.bias.data.clone() out += self.bias.view(1, -1, 1, 1).expand_as(out) return out class BasicBlock(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=None, do_bntan=True): super(BasicBlock, self).__init__() self.conv1 = Binaryconv3x3(inplanes, planes, stride) self.bn1 = nn.BatchNorm2d(planes) self.tanh1 = nn.Hardtanh(inplace=True) self.conv2 = Binaryconv3x3(planes, planes) self.tanh2 = nn.Hardtanh(inplace=True) self.bn2 = nn.BatchNorm2d(planes) self.downsample = downsample self.do_bntan = do_bntan self.stride = stride def forward(self, x): residual = x.clone() out = self.conv1(x) out = self.bn1(out) out = self.tanh1(out) out = self.conv2(out) if self.downsample is not None: if residual.data.max() > 1: pdb.set_trace() residual = self.downsample(residual) out += residual if self.do_bntan: out = self.bn2(out) out = self.tanh2(out) return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'inplanes': 4, 'planes': 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.utils.data from torch import nn import torch import torch.nn.parallel import torch.optim 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_clone_eq_ones_like_sign_where_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 + x0, xmask) tmp1 = tl.full([1], 0, tl.int32) tmp2 = tmp1 < tmp0 tmp3 = tmp2.to(tl.int8) tmp4 = tmp0 < tmp1 tmp5 = tmp4.to(tl.int8) tmp6 = tmp3 - tmp5 tmp7 = tmp6.to(tmp0.dtype) tmp8 = 0.0 tmp9 = tmp7 == tmp8 tmp10 = 1.0 tmp11 = tl.where(tmp9, tmp10, tmp7) tl.store(out_ptr0 + x0, tmp0, xmask) tl.store(out_ptr1 + x0, tmp11, xmask) @triton.jit def triton_poi_fused_eq_ones_like_sign_where_1(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 x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.full([1], 0, tl.int32) tmp2 = tmp1 < tmp0 tmp3 = tmp2.to(tl.int8) tmp4 = tmp0 < tmp1 tmp5 = tmp4.to(tl.int8) tmp6 = tmp3 - tmp5 tmp7 = tmp6.to(tmp0.dtype) tmp8 = 0.0 tmp9 = tmp7 == tmp8 tmp10 = 1.0 tmp11 = tl.where(tmp9, tmp10, tmp7) tl.store(out_ptr0 + x0, tmp11, xmask) @triton.jit def triton_poi_fused__native_batch_norm_legit_no_training_hardtanh_2(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 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') tmp12 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp14 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = 1e-05 tmp5 = tmp3 + tmp4 tmp6 = libdevice.sqrt(tmp5) tmp7 = tl.full([1], 1, tl.int32) tmp8 = tmp7 / tmp6 tmp9 = 1.0 tmp10 = tmp8 * tmp9 tmp11 = tmp2 * tmp10 tmp13 = tmp11 * tmp12 tmp15 = tmp13 + tmp14 tmp16 = -1.0 tmp17 = triton_helpers.maximum(tmp15, tmp16) tmp18 = triton_helpers.minimum(tmp17, tmp9) tl.store(out_ptr0 + x3, tmp18, 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, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_3, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (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, 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_clone_eq_ones_like_sign_where_0[grid(256)](primals_1, buf0, buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf2 = empty_strided_cuda((4, 4, 3, 3), (36, 9, 3, 1), torch.float32) triton_poi_fused_eq_ones_like_sign_where_1[grid(144)](primals_3, buf2, 144, XBLOCK=256, num_warps=4, num_stages=1) del primals_3 buf3 = extern_kernels.convolution(buf1, buf2, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf3, (4, 4, 4, 4), (64, 16, 4, 1)) buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__native_batch_norm_legit_no_training_hardtanh_2[grid (256)](buf3, primals_4, primals_5, primals_6, primals_7, buf4, 256, XBLOCK=128, num_warps=4, num_stages=1) buf5 = torch.ops.aten.set_.source_Tensor(primals_1, buf1) assert_size_stride(buf5, (4, 4, 4, 4), (64, 16, 4, 1)) del primals_1 buf10 = torch.ops.aten.set_.source_Tensor(primals_2, buf2) assert_size_stride(buf10, (4, 4, 3, 3), (36, 9, 3, 1)) del primals_2 return (buf4, buf0, primals_4, primals_5, primals_6, primals_7, buf1, buf2, buf3) def Binarize(tensor, quant_mode='det'): if quant_mode == 'det': tensor = tensor.sign() zero = torch.zeros_like(tensor) one = torch.ones_like(tensor) zero - one tensor = torch.where(tensor == 0, one, tensor) return tensor else: return tensor.add_(1).div_(2).add_(torch.rand(tensor.size()).add(-0.5) ).clamp_(0, 1).round().mul_(2).add_(-1) def Binaryconv3x3(in_planes, out_planes, stride=1): """3x3 convolution with padding""" return BinarizeConv2d(in_planes, out_planes, kernel_size=3, stride= stride, padding=1, bias=False) class BinarizeConv2d(nn.Conv2d): def __init__(self, *kargs, **kwargs): super(BinarizeConv2d, self).__init__(*kargs, **kwargs) def forward(self, input): input.data = Binarize(input.data) if not hasattr(self.weight, 'org'): self.weight.org = self.weight.data.clone() self.weight.data = Binarize(self.weight.org) out = nn.functional.conv2d(input, self.weight, None, self.stride, self.padding, self.dilation, self.groups) if self.bias is not None: self.bias.org = self.bias.data.clone() out += self.bias.view(1, -1, 1, 1).expand_as(out) return out class BasicBlockNew(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=None, do_bntan=True): super(BasicBlockNew, self).__init__() self.conv1 = Binaryconv3x3(inplanes, planes, stride) self.bn1 = nn.BatchNorm2d(planes) self.tanh1 = nn.Hardtanh(inplace=True) self.conv2 = Binaryconv3x3(planes, planes) self.tanh2 = nn.Hardtanh(inplace=True) self.bn2 = nn.BatchNorm2d(planes) self.downsample = downsample self.do_bntan = do_bntan self.stride = stride def forward(self, input_0): primals_2 = self.conv1.weight primals_4 = self.bn1.weight primals_5 = self.bn1.bias primals_3 = self.conv2.weight primals_6 = self.bn2.weight primals_7 = self.bn2.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
graphbuilder/BNN
BasicBlock
false
6,759
[ "MIT" ]
1
d99eb5c7ef19f8b0c14a135d40a489f154a3c894
https://github.com/graphbuilder/BNN/tree/d99eb5c7ef19f8b0c14a135d40a489f154a3c894
NetVLAD
import math import torch import torch.nn as nn import torch.nn.functional as F class NetVLAD(nn.Module): def __init__(self, dims, num_clusters, outdims=None): super(NetVLAD, self).__init__() self.num_clusters = num_clusters self.dims = dims self.centroids = nn.Parameter(torch.randn(num_clusters, dims) / math.sqrt(self.dims)) self.conv = nn.Conv2d(dims, num_clusters, kernel_size=1, bias=False) if outdims is not None: self.outdims = outdims self.reduction_layer = nn.Linear(self.num_clusters * self.dims, self.outdims, bias=False) else: self.outdims = self.num_clusters * self.dims self.norm = nn.LayerNorm(self.outdims) self.reset_parameters() def reset_parameters(self): self.conv.weight = nn.Parameter(self.centroids.detach().clone(). unsqueeze(-1).unsqueeze(-1)) if hasattr(self, 'reduction_layer'): nn.init.normal_(self.reduction_layer.weight, std=1 / math.sqrt( self.num_clusters * self.dims)) def forward(self, x, mask=None, sample=False): N, C, T, R = x.shape soft_assign = self.conv(x).view(N, self.num_clusters, -1) soft_assign = F.softmax(soft_assign, dim=1).view(N, self. num_clusters, T, R) x_flatten = x.view(N, C, -1) vlad = torch.zeros([N, self.num_clusters, C], dtype=x.dtype, layout =x.layout, device=x.device) for cluster in range(self.num_clusters): residual = x_flatten.unsqueeze(0).permute(1, 0, 2, 3 ) - self.centroids[cluster:cluster + 1, :].expand(x_flatten .size(-1), -1, -1).permute(1, 2, 0).unsqueeze(0) residual = residual.view(N, C, T, R) residual *= soft_assign[:, cluster:cluster + 1, :] if mask is not None: residual = residual.masked_fill((1 - mask.unsqueeze(1). unsqueeze(-1)).bool(), 0.0) vlad[:, cluster:cluster + 1, :] = residual.sum([-2, -1]).unsqueeze( 1) vlad = F.normalize(vlad, p=2, dim=2) vlad = vlad.view(x.size(0), -1) vlad = F.normalize(vlad, p=2, dim=1) if hasattr(self, 'reduction_layer'): vlad = self.reduction_layer(vlad) return self.norm(vlad) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'dims': 4, 'num_clusters': 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__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) @triton.jit def triton_per_fused_sub_sum_2(in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr4, out_ptr5, out_ptr6, 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) r2 = rindex x3 = xindex x0 = xindex % 4 x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + (r2 + 16 * x3), xmask, other=0.0) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + (r2 + 64 * x1), xmask, eviction_policy= 'evict_last', other=0.0) tmp9 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last') tmp13 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last') tmp15 = tl.load(in_ptr2 + (16 + r2 + 64 * x1), xmask, eviction_policy= 'evict_last', other=0.0) tmp21 = tl.load(in_ptr2 + (32 + r2 + 64 * x1), xmask, eviction_policy= 'evict_last', other=0.0) tmp27 = tl.load(in_ptr2 + (48 + r2 + 64 * x1), xmask, eviction_policy= 'evict_last', other=0.0) tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK]) tmp7 = tl.where(xmask, tmp5, 0) tmp8 = tl.sum(tmp7, 1)[:, None] tmp10 = tmp0 - tmp9 tmp12 = tmp0 - tmp11 tmp14 = tmp0 - tmp13 tmp16 = tmp10 * tmp15 tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK]) tmp19 = tl.where(xmask, tmp17, 0) tmp20 = tl.sum(tmp19, 1)[:, None] tmp22 = tmp12 * tmp21 tmp23 = tl.broadcast_to(tmp22, [XBLOCK, RBLOCK]) tmp25 = tl.where(xmask, tmp23, 0) tmp26 = tl.sum(tmp25, 1)[:, None] tmp28 = tmp14 * tmp27 tmp29 = tl.broadcast_to(tmp28, [XBLOCK, RBLOCK]) tmp31 = tl.where(xmask, tmp29, 0) tmp32 = tl.sum(tmp31, 1)[:, None] tl.store(out_ptr1 + (r2 + 16 * x3), tmp10, xmask) tl.store(out_ptr2 + (r2 + 16 * x3), tmp12, xmask) tl.store(out_ptr3 + (r2 + 16 * x3), tmp14, xmask) tl.store(out_ptr0 + x3, tmp8, xmask) tl.store(out_ptr4 + x3, tmp20, xmask) tl.store(out_ptr5 + x3, tmp26, xmask) tl.store(out_ptr6 + x3, tmp32, xmask) @triton.jit def triton_poi_fused_copy_zeros_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 % 4 x0 = xindex % 4 x2 = xindex // 16 x3 = xindex tmp0 = x1 tmp1 = tl.full([1], 3, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.load(in_ptr0 + (x0 + 4 * x2), tmp2 & xmask, eviction_policy= 'evict_last', other=0.0) tmp4 = tl.full([1], 2, tl.int64) tmp5 = tmp0 >= tmp4 tmp6 = tmp0 < tmp1 tmp7 = tmp5 & tmp6 tmp8 = tl.load(in_ptr1 + (x0 + 4 * x2), tmp7 & xmask, eviction_policy= 'evict_last', other=0.0) tmp9 = tl.full([1], 1, tl.int64) tmp10 = tmp0 >= tmp9 tmp11 = tmp0 < tmp4 tmp12 = tmp10 & tmp11 tmp13 = tl.load(in_ptr2 + (x0 + 4 * x2), tmp12 & xmask, eviction_policy ='evict_last', other=0.0) tmp14 = tmp0 < tmp9 tmp15 = tl.load(in_ptr3 + (x0 + 4 * x2), tmp14 & xmask, eviction_policy ='evict_last', other=0.0) tmp16 = 0.0 tmp17 = tl.where(tmp14, tmp15, tmp16) tmp18 = tl.where(tmp12, tmp13, tmp17) tmp19 = tl.where(tmp7, tmp8, tmp18) tmp20 = tl.where(tmp2, tmp3, tmp19) tl.store(out_ptr0 + x3, tmp20, xmask) @triton.jit def triton_per_fused_div_linalg_vector_norm_native_layer_norm_4(in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 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) r3 = rindex x0 = xindex r2 = rindex // 4 tmp0 = tl.load(in_ptr0 + (r3 + 16 * x0), xmask, other=0.0) tmp1 = tl.load(in_ptr0 + (4 * r2 + 16 * x0), xmask, eviction_policy= 'evict_last', other=0.0) tmp3 = tl.load(in_ptr0 + (1 + 4 * r2 + 16 * x0), xmask, eviction_policy ='evict_last', other=0.0) tmp6 = tl.load(in_ptr0 + (2 + 4 * r2 + 16 * x0), xmask, eviction_policy ='evict_last', other=0.0) tmp9 = tl.load(in_ptr0 + (3 + 4 * r2 + 16 * x0), xmask, eviction_policy ='evict_last', other=0.0) tmp47 = tl.load(in_ptr1 + r3, None, eviction_policy='evict_last') tmp49 = tl.load(in_ptr2 + r3, None, 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 tmp16 = tmp15 * tmp15 tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK]) tmp19 = tl.where(xmask, tmp17, 0) tmp20 = tl.sum(tmp19, 1)[:, None] tmp21 = libdevice.sqrt(tmp20) tmp22 = triton_helpers.maximum(tmp21, tmp13) tmp23 = tmp15 / tmp22 tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK]) tl.where(xmask, tmp24, 0) tmp27 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK]) tmp29 = tl.where(xmask, tmp27, 0) tmp30 = tl.sum(tmp29, 1)[:, None] tmp31 = tl.full([XBLOCK, 1], 16, tl.int32) tmp32 = tmp31.to(tl.float32) tmp33 = tmp30 / tmp32 tmp34 = tmp24 - tmp33 tmp35 = tmp34 * tmp34 tmp36 = tl.broadcast_to(tmp35, [XBLOCK, RBLOCK]) tmp38 = tl.where(xmask, tmp36, 0) tmp39 = tl.sum(tmp38, 1)[:, None] tmp40 = 16.0 tmp41 = tmp39 / tmp40 tmp42 = 1e-05 tmp43 = tmp41 + tmp42 tmp44 = libdevice.rsqrt(tmp43) tmp45 = tmp23 - tmp33 tmp46 = tmp45 * tmp44 tmp48 = tmp46 * tmp47 tmp50 = tmp48 + tmp49 tl.store(out_ptr0 + (r3 + 16 * x0), tmp15, xmask) tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp21, xmask) tl.debug_barrier() tl.store(in_out_ptr1 + x0, tmp44, xmask) tl.store(out_ptr2 + (r3 + 16 * x0), tmp50, xmask) tl.store(out_ptr1 + x0, tmp33, 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, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (16,), (1,)) assert_size_stride(primals_5, (16,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_1, primals_2, 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, 4, 4), (64, 16, 4, 1)) buf1 = empty_strided_cuda((4, 4, 16), (64, 16, 1), torch.float32) get_raw_stream(0) triton_poi_fused__softmax_0[grid(256)](buf0, buf1, 256, XBLOCK=256, num_warps=4, num_stages=1) buf2 = empty_strided_cuda((4, 4, 16), (64, 16, 1), torch.float32) triton_poi_fused__softmax_1[grid(256)](buf1, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf4 = reinterpret_tensor(buf1, (4, 1, 4, 16), (64, 1, 16, 1), 0) del buf1 buf6 = empty_strided_cuda((4, 1, 4, 16), (64, 1, 16, 1), torch.float32) buf8 = empty_strided_cuda((4, 1, 4, 16), (64, 1, 16, 1), torch.float32) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_per_fused_sub_sum_2[grid(16)](primals_1, primals_3, buf2, buf3, buf4, buf6, buf8, buf5, buf7, buf9, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del buf2 buf10 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_copy_zeros_3[grid(64)](buf9, buf7, buf5, buf3, buf10, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf3 del buf5 del buf7 del buf9 buf11 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) buf12 = empty_strided_cuda((4, 1), (1, 4), torch.float32) buf13 = reinterpret_tensor(buf12, (4, 1), (1, 1), 0) del buf12 buf14 = empty_strided_cuda((4, 1), (1, 1), torch.float32) buf15 = empty_strided_cuda((4, 1), (1, 4), torch.float32) buf17 = reinterpret_tensor(buf15, (4, 1), (1, 1), 0) del buf15 buf18 = empty_strided_cuda((4, 16), (16, 1), torch.float32) triton_per_fused_div_linalg_vector_norm_native_layer_norm_4[grid(4)]( buf13, buf17, buf10, primals_4, primals_5, buf11, buf14, buf18, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) del buf11 del primals_5 return buf18, primals_1, primals_2, primals_4, buf0, reinterpret_tensor( primals_3, (1, 4), (4, 1), 0), reinterpret_tensor(buf4, (4, 4, 4, 4 ), (64, 16, 4, 1), 0), reinterpret_tensor(buf6, (4, 4, 4, 4), (64, 16, 4, 1), 0), reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), buf10, buf13, buf14, buf17 class NetVLADNew(nn.Module): def __init__(self, dims, num_clusters, outdims=None): super(NetVLADNew, self).__init__() self.num_clusters = num_clusters self.dims = dims self.centroids = nn.Parameter(torch.randn(num_clusters, dims) / math.sqrt(self.dims)) self.conv = nn.Conv2d(dims, num_clusters, kernel_size=1, bias=False) if outdims is not None: self.outdims = outdims self.reduction_layer = nn.Linear(self.num_clusters * self.dims, self.outdims, bias=False) else: self.outdims = self.num_clusters * self.dims self.norm = nn.LayerNorm(self.outdims) self.reset_parameters() def reset_parameters(self): self.conv.weight = nn.Parameter(self.centroids.detach().clone(). unsqueeze(-1).unsqueeze(-1)) if hasattr(self, 'reduction_layer'): nn.init.normal_(self.reduction_layer.weight, std=1 / math.sqrt( self.num_clusters * self.dims)) def forward(self, input_0): primals_3 = self.centroids primals_2 = self.conv.weight primals_4 = self.norm.weight primals_5 = self.norm.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
glee1228/segment_temporal_context_aggregation
NetVLAD
false
6,760
[ "Apache-2.0" ]
1
e5778f848f1cfd89bd1f77beb5e1b38a66a2f13d
https://github.com/glee1228/segment_temporal_context_aggregation/tree/e5778f848f1cfd89bd1f77beb5e1b38a66a2f13d
Attention
import torch import torch.nn as nn import torch.utils.data class Attention(nn.Module): def __init__(self, in_size, hidden_size): super(Attention, self).__init__() self.hidden = nn.Linear(in_size, hidden_size) nn.init.orthogonal_(self.hidden.weight.data) self.out = nn.Linear(hidden_size, in_size) nn.init.orthogonal_(self.hidden.weight.data) self.softmax = nn.Softmax(dim=1) def forward(self, input): self.alpha = self.softmax(self.out(torch.tanh(self.hidden(input)))) x = torch.sum(self.alpha * input, 1) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_size': 4, '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 libdevice, 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_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) @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 = 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_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 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) @triton.jit def triton_poi_fused_mul_sum_3(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) tmp3 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask) tmp4 = tl.load(in_ptr1 + (16 + x0 + 64 * x1), xmask) tmp7 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask) tmp8 = tl.load(in_ptr1 + (32 + x0 + 64 * x1), xmask) tmp11 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask) tmp12 = tl.load(in_ptr1 + (48 + x0 + 64 * 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, 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 get_raw_stream(0) triton_poi_fused_tanh_0[grid(256)](buf1, primals_2, 256, XBLOCK=128, 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, 4), ( 4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf2) del primals_5 buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__softmax_1[grid(256)](buf2, buf3, 256, XBLOCK=128, num_warps=4, num_stages=1) buf4 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf2 triton_poi_fused__softmax_2[grid(256)](buf3, buf4, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf3 buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_mul_sum_3[grid(64)](buf4, primals_3, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) return buf5, buf4, primals_3, buf1, buf4, primals_4 class AttentionNew(nn.Module): def __init__(self, in_size, hidden_size): super(AttentionNew, self).__init__() self.hidden = nn.Linear(in_size, hidden_size) nn.init.orthogonal_(self.hidden.weight.data) self.out = nn.Linear(hidden_size, in_size) nn.init.orthogonal_(self.hidden.weight.data) self.softmax = nn.Softmax(dim=1) def forward(self, input_0): primals_1 = self.hidden.weight primals_2 = self.hidden.bias primals_4 = self.out.weight primals_5 = self.out.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
gchrupala/platalea
Attention
false
6,761
[ "Apache-2.0" ]
1
65833307bb6c5ad6cbdd6b17ad8ca59cf51fcd81
https://github.com/gchrupala/platalea/tree/65833307bb6c5ad6cbdd6b17ad8ca59cf51fcd81
Auxiliary
import torch import torch.nn as nn import torch.nn.functional as F class Auxiliary(nn.Module): def __init__(self, input_channels, n_classes): super(Auxiliary, self).__init__() self.Conv2 = nn.Conv2d(input_channels, 128, kernel_size=1) self.FC1 = nn.Linear(2048, 1024) self.FC2 = nn.Linear(1024, n_classes) def forward(self, x): x = F.adaptive_avg_pool2d(x, (4, 4)) x = self.Conv2(x) x = x.view(x.size(0), -1) x = F.relu(self.FC1(x), inplace=True) x = F.dropout(x, 0.7, training=self.training) x = self.FC2(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_channels': 4, 'n_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__adaptive_avg_pool2d_0(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_convolution_1(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 512 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 y0 = yindex % 128 y1 = yindex // 128 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 128 * x2 + 2048 * 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 + 16 * y3), tmp2, xmask & ymask) @triton.jit def triton_poi_fused_relu_2(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 % 1024 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) = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (128, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_3, (128,), (1,)) assert_size_stride(primals_4, (1024, 2048), (2048, 1)) assert_size_stride(primals_5, (1024,), (1,)) assert_size_stride(primals_6, (4, 1024), (1024, 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, 4), (64, 1, 16, 4), torch.float32) get_raw_stream(0) triton_poi_fused__adaptive_avg_pool2d_0[grid(16, 16)](primals_1, buf0, 16, 16, XBLOCK=16, YBLOCK=16, 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, 128, 4, 4), (2048, 1, 512, 128)) buf2 = empty_strided_cuda((4, 128, 4, 4), (2048, 16, 4, 1), torch. float32) triton_poi_fused_convolution_1[grid(512, 16)](buf1, primals_3, buf2, 512, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) del buf1 del primals_3 buf3 = empty_strided_cuda((4, 1024), (1024, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf2, (4, 2048), (2048, 1), 0), reinterpret_tensor(primals_4, (2048, 1024), (1, 2048), 0), out=buf3 ) buf4 = buf3 del buf3 triton_poi_fused_relu_2[grid(4096)](buf4, primals_5, 4096, XBLOCK= 256, num_warps=4, num_stages=1) del primals_5 buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, buf4, reinterpret_tensor(primals_6, (1024, 4), (1, 1024), 0), alpha=1, beta=1, out=buf5) del primals_7 return buf5, primals_2, buf0, reinterpret_tensor(buf2, (4, 2048), (2048, 1), 0), buf4, primals_6, primals_4 class AuxiliaryNew(nn.Module): def __init__(self, input_channels, n_classes): super(AuxiliaryNew, self).__init__() self.Conv2 = nn.Conv2d(input_channels, 128, kernel_size=1) self.FC1 = nn.Linear(2048, 1024) self.FC2 = nn.Linear(1024, n_classes) def forward(self, input_0): primals_2 = self.Conv2.weight primals_3 = self.Conv2.bias primals_4 = self.FC1.weight primals_5 = self.FC1.bias primals_6 = self.FC2.weight primals_7 = self.FC2.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
gogo5911/PyTorch_Network
Auxiliary
false
6,762
[ "MIT" ]
1
396e2ebfe2c7e23143e72972e2fd55613c0098a3
https://github.com/gogo5911/PyTorch_Network/tree/396e2ebfe2c7e23143e72972e2fd55613c0098a3
Conv2dBlock
import torch import torch.utils.data import torch from torch import nn class LayerNorm(nn.Module): def __init__(self, num_features, eps=1e-05, affine=True): super(LayerNorm, self).__init__() self.num_features = num_features self.affine = affine self.eps = eps if self.affine: self.gamma = nn.Parameter(torch.Tensor(num_features).uniform_()) self.beta = nn.Parameter(torch.zeros(num_features)) def forward(self, x): shape = [-1] + [1] * (x.dim() - 1) mean = x.view(x.size(0), -1).mean(1).view(*shape) std = x.view(x.size(0), -1).std(1).view(*shape) x = (x - mean) / (std + self.eps) if self.affine: shape = [1, -1] + [1] * (x.dim() - 2) x = x * self.gamma.view(*shape) + self.beta.view(*shape) return x class Conv2dBlock(nn.Module): def __init__(self, input_dim, output_dim, kernel_size, stride, padding= 0, norm='none', activation='relu', pad_type='zero'): super(Conv2dBlock, self).__init__() self.use_bias = True if pad_type == 'reflect': self.pad = nn.ReflectionPad2d(padding) elif pad_type == 'zero': self.pad = nn.ZeroPad2d(padding) else: assert 0, 'Unsupported padding type: {}'.format(pad_type) norm_dim = output_dim if norm == 'batch': self.norm = nn.BatchNorm2d(norm_dim) elif norm == 'inst': self.norm = nn.InstanceNorm2d(norm_dim, track_running_stats=False) elif norm == 'ln': self.norm = LayerNorm(norm_dim) elif norm == 'none': self.norm = None else: assert 0, 'Unsupported normalization: {}'.format(norm) if activation == 'relu': self.activation = nn.ReLU(inplace=True) elif activation == 'lrelu': self.activation = nn.LeakyReLU(0.2, inplace=True) elif activation == 'prelu': self.activation = nn.PReLU() elif activation == 'selu': self.activation = nn.SELU(inplace=True) elif activation == 'tanh': self.activation = nn.Tanh() elif activation == 'none': self.activation = None else: assert 0, 'Unsupported activation: {}'.format(activation) self.conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride, bias=self.use_bias) def forward(self, x): x = self.conv(self.pad(x)) if self.norm: x = self.norm(x) if self.activation: x = self.activation(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'output_dim': 4, 'kernel_size': 4, 'stride': 1}]
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 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_convolution_relu_threshold_backward_0(in_out_ptr0, 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 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, 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 = extern_kernels.convolution(primals_1, primals_2, 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 buf2 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.bool) get_raw_stream(0) triton_poi_fused_convolution_relu_threshold_backward_0[grid(16)](buf1, primals_3, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_3 return buf1, primals_1, primals_2, buf2 class LayerNorm(nn.Module): def __init__(self, num_features, eps=1e-05, affine=True): super(LayerNorm, self).__init__() self.num_features = num_features self.affine = affine self.eps = eps if self.affine: self.gamma = nn.Parameter(torch.Tensor(num_features).uniform_()) self.beta = nn.Parameter(torch.zeros(num_features)) def forward(self, x): shape = [-1] + [1] * (x.dim() - 1) mean = x.view(x.size(0), -1).mean(1).view(*shape) std = x.view(x.size(0), -1).std(1).view(*shape) x = (x - mean) / (std + self.eps) if self.affine: shape = [1, -1] + [1] * (x.dim() - 2) x = x * self.gamma.view(*shape) + self.beta.view(*shape) return x class Conv2dBlockNew(nn.Module): def __init__(self, input_dim, output_dim, kernel_size, stride, padding= 0, norm='none', activation='relu', pad_type='zero'): super(Conv2dBlockNew, self).__init__() self.use_bias = True if pad_type == 'reflect': self.pad = nn.ReflectionPad2d(padding) elif pad_type == 'zero': self.pad = nn.ZeroPad2d(padding) else: assert 0, 'Unsupported padding type: {}'.format(pad_type) norm_dim = output_dim if norm == 'batch': self.norm = nn.BatchNorm2d(norm_dim) elif norm == 'inst': self.norm = nn.InstanceNorm2d(norm_dim, track_running_stats=False) elif norm == 'ln': self.norm = LayerNorm(norm_dim) elif norm == 'none': self.norm = None else: assert 0, 'Unsupported normalization: {}'.format(norm) if activation == 'relu': self.activation = nn.ReLU(inplace=True) elif activation == 'lrelu': self.activation = nn.LeakyReLU(0.2, inplace=True) elif activation == 'prelu': self.activation = nn.PReLU() elif activation == 'selu': self.activation = nn.SELU(inplace=True) elif activation == 'tanh': self.activation = nn.Tanh() elif activation == 'none': self.activation = None else: assert 0, 'Unsupported activation: {}'.format(activation) self.conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride, bias=self.use_bias) def forward(self, input_0): primals_1 = self.conv.weight primals_3 = self.conv.bias primals_2 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
guyii54/Contrastive-I2I
Conv2dBlock
false
6,763
[ "BSD-3-Clause" ]
1
e73daa0f9d3770c2280a304c39678d5b22440647
https://github.com/guyii54/Contrastive-I2I/tree/e73daa0f9d3770c2280a304c39678d5b22440647
GRU
import torch import torch as tc import torch.nn as nn class Layer_Norm(nn.Module): def __init__(self, d_hid, eps=0.001): super(Layer_Norm, self).__init__() self.eps = eps self.g = nn.Parameter(tc.ones(d_hid), requires_grad=True) self.b = nn.Parameter(tc.zeros(d_hid), requires_grad=True) def forward(self, z): if z.size(-1) == 1: return z mu = tc.mean(z, dim=-1, keepdim=True) variance = tc.mean(tc.pow(z - mu, 2), dim=-1, keepdim=True) sigma = tc.sqrt(variance) z_norm = tc.div(z - mu, sigma + self.eps) if z_norm.is_cuda: z_norm = z_norm * self.g.expand_as(z_norm) + self.b.expand_as( z_norm) else: z_norm = z_norm * self.g.expand_as(z_norm) + self.b.expand_as( z_norm) return z_norm class GRU(nn.Module): """ Gated Recurrent Unit network with initial state as parameter: z_t = sigmoid((x_t dot W_xz + b_xz) + (h_{t-1} dot U_hz + b_hz)) r_t = sigmoid((x_t dot W_xr + b_xr) + (h_{t-1} dot U_hr + b_xr)) => zr_t = sigmoid((x_t dot W_xzr + b_xzr) + (h_{t-1} dot U_hzr + b_hzr)) slice ... h_above = tanh(x_t dot W_xh + b_xh + (h_{t-1} dot U_hh + b_hh) * r_t) h_t = (1 - z_t) * h_above + z_t * h_{t-1} #h_t = (1 - z_t) * h_{t-1} + z_t * h_above all parameters are initialized in [-0.01, 0.01] """ def __init__(self, input_size, hidden_size, enc_hid_size=None, with_ln= False, prefix='GRU', **kwargs): super(GRU, self).__init__() self.enc_hid_size = enc_hid_size self.hidden_size = hidden_size self.with_ln = with_ln self.prefix = prefix self.xh = nn.Linear(input_size, hidden_size) self.hh = nn.Linear(hidden_size, hidden_size) self.xrz = nn.Linear(input_size, 2 * hidden_size) self.hrz = nn.Linear(hidden_size, 2 * hidden_size) if self.enc_hid_size is not None: self.crz = nn.Linear(enc_hid_size, 2 * hidden_size) self.ch = nn.Linear(enc_hid_size, hidden_size) if self.with_ln is True: self.ln0 = Layer_Norm(2 * hidden_size) self.ln1 = Layer_Norm(2 * hidden_size) self.ln2 = Layer_Norm(hidden_size) self.ln3 = Layer_Norm(hidden_size) if self.enc_hid_size is not None: self.ln4 = Layer_Norm(2 * hidden_size) self.ln5 = Layer_Norm(hidden_size) self.sigmoid = nn.Sigmoid() self.tanh = nn.Tanh() """ x_t: input at time t x_m: mask of x_t h_tm1: previous state """ def forward(self, x_t, x_m, h_tm1, attend=None): x_rz_t, h_rz_tm1, x_h_t = self.xrz(x_t), self.hrz(h_tm1), self.xh(x_t) if self.with_ln is not True: if self.enc_hid_size is None: rz_t = x_rz_t + h_rz_tm1 else: a_rz_t, a_h_t = self.crz(attend), self.ch(attend) rz_t = x_rz_t + h_rz_tm1 + a_rz_t else: x_rz_t, h_rz_tm1, x_h_t = self.ln0(x_rz_t), self.ln1(h_rz_tm1 ), self.ln2(x_h_t) if self.enc_hid_size is None: rz_t = x_rz_t + h_rz_tm1 else: a_rz_t, a_h_t = self.crz(attend), self.ch(attend) a_rz_t, a_h_t = self.ln4(a_rz_t), self.ln5(a_h_t) rz_t = x_rz_t + h_rz_tm1 + a_rz_t assert rz_t.dim() == 2 rz_t = self.sigmoid(rz_t) r_t, z_t = rz_t[:, :self.hidden_size], rz_t[:, self.hidden_size:] h_h_tm1 = self.hh(r_t * h_tm1) if self.with_ln: h_h_tm1 = self.ln3(h_h_tm1) if self.enc_hid_size is None: h_h_tm1 = x_h_t + h_h_tm1 else: h_h_tm1 = x_h_t + h_h_tm1 + a_h_t h_t_above = self.tanh(h_h_tm1) h_t = (1.0 - z_t) * h_tm1 + z_t * h_t_above if x_m is not None: h_t = x_m[:, None] * h_t + (1.0 - x_m[:, None]) * h_tm1 return h_t def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'input_size': 4, '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.triton_helpers import libdevice import torch as tc 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_sigmoid_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, 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') tmp3 = tl.load(in_ptr1 + x2, xmask) tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp7 = tl.sigmoid(tmp6) tl.store(in_out_ptr0 + x2, tmp7, xmask) @triton.jit def triton_poi_fused_mul_1(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 + x2, xmask) tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x2, tmp2, xmask) @triton.jit def triton_poi_fused_add_tanh_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, 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') tmp3 = tl.load(in_ptr1 + x2, xmask) tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp7 = libdevice.tanh(tmp6) tl.store(in_out_ptr0 + x2, tmp7, xmask) @triton.jit def triton_poi_fused_add_mul_rsub_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 x2 = xindex // 16 x1 = xindex // 4 % 4 x3 = xindex % 16 x4 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp1 = tl.load(in_ptr1 + (4 + x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr2 + x3, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr3 + x3, xmask, eviction_policy='evict_last') tmp2 = 1.0 tmp3 = tmp2 - tmp1 tmp5 = tmp3 * tmp4 tmp7 = tmp1 * tmp6 tmp8 = tmp5 + tmp7 tmp9 = tmp0 * tmp8 tmp10 = tmp2 - tmp0 tmp11 = tmp10 * tmp4 tmp12 = tmp9 + tmp11 tl.store(out_ptr0 + x4, tmp12, 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, (8, 4), (4, 1)) assert_size_stride(primals_2, (8,), (1,)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (8, 4), (4, 1)) assert_size_stride(primals_5, (8,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4, 4), (4, 1)) assert_size_stride(primals_8, (4,), (1,)) assert_size_stride(primals_9, (4, 4), (4, 1)) assert_size_stride(primals_10, (4,), (1,)) assert_size_stride(primals_11, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 8), (8, 1), torch.float32) extern_kernels.mm(primals_3, reinterpret_tensor(primals_1, (4, 8), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 8), (8, 1), torch.float32) extern_kernels.mm(primals_6, reinterpret_tensor(primals_4, (4, 8), (1, 4), 0), out=buf1) del primals_4 buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, reinterpret_tensor(primals_7, (4, 4), (1, 4), 0), out=buf2) del primals_7 buf3 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_add_sigmoid_0[grid(32)](buf3, primals_2, buf1, primals_5, 32, XBLOCK=32, num_warps=1, num_stages=1) del buf1 del primals_2 del primals_5 buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_mul_1[grid(16)](buf3, primals_6, buf4, 16, XBLOCK= 16, num_warps=1, num_stages=1) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf4, reinterpret_tensor(primals_9, (4, 4), (1, 4 ), 0), out=buf5) buf6 = buf2 del buf2 triton_poi_fused_add_tanh_2[grid(16)](buf6, primals_8, buf5, primals_10, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf5 del primals_10 del primals_8 buf7 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_add_mul_rsub_3[grid(64)](primals_11, buf3, primals_6, buf6, buf7, 64, XBLOCK=64, num_warps=1, num_stages=1) return buf7, primals_3, primals_6, primals_11, buf3, buf4, buf6, primals_9 class Layer_Norm(nn.Module): def __init__(self, d_hid, eps=0.001): super(Layer_Norm, self).__init__() self.eps = eps self.g = nn.Parameter(tc.ones(d_hid), requires_grad=True) self.b = nn.Parameter(tc.zeros(d_hid), requires_grad=True) def forward(self, z): if z.size(-1) == 1: return z mu = tc.mean(z, dim=-1, keepdim=True) variance = tc.mean(tc.pow(z - mu, 2), dim=-1, keepdim=True) sigma = tc.sqrt(variance) z_norm = tc.div(z - mu, sigma + self.eps) if z_norm.is_cuda: z_norm = z_norm * self.g.expand_as(z_norm) + self.b.expand_as( z_norm) else: z_norm = z_norm * self.g.expand_as(z_norm) + self.b.expand_as( z_norm) return z_norm class GRUNew(nn.Module): """ Gated Recurrent Unit network with initial state as parameter: z_t = sigmoid((x_t dot W_xz + b_xz) + (h_{t-1} dot U_hz + b_hz)) r_t = sigmoid((x_t dot W_xr + b_xr) + (h_{t-1} dot U_hr + b_xr)) => zr_t = sigmoid((x_t dot W_xzr + b_xzr) + (h_{t-1} dot U_hzr + b_hzr)) slice ... h_above = tanh(x_t dot W_xh + b_xh + (h_{t-1} dot U_hh + b_hh) * r_t) h_t = (1 - z_t) * h_above + z_t * h_{t-1} #h_t = (1 - z_t) * h_{t-1} + z_t * h_above all parameters are initialized in [-0.01, 0.01] """ def __init__(self, input_size, hidden_size, enc_hid_size=None, with_ln= False, prefix='GRU', **kwargs): super(GRUNew, self).__init__() self.enc_hid_size = enc_hid_size self.hidden_size = hidden_size self.with_ln = with_ln self.prefix = prefix self.xh = nn.Linear(input_size, hidden_size) self.hh = nn.Linear(hidden_size, hidden_size) self.xrz = nn.Linear(input_size, 2 * hidden_size) self.hrz = nn.Linear(hidden_size, 2 * hidden_size) if self.enc_hid_size is not None: self.crz = nn.Linear(enc_hid_size, 2 * hidden_size) self.ch = nn.Linear(enc_hid_size, hidden_size) if self.with_ln is True: self.ln0 = Layer_Norm(2 * hidden_size) self.ln1 = Layer_Norm(2 * hidden_size) self.ln2 = Layer_Norm(hidden_size) self.ln3 = Layer_Norm(hidden_size) if self.enc_hid_size is not None: self.ln4 = Layer_Norm(2 * hidden_size) self.ln5 = Layer_Norm(hidden_size) self.sigmoid = nn.Sigmoid() self.tanh = nn.Tanh() """ x_t: input at time t x_m: mask of x_t h_tm1: previous state """ def forward(self, input_0, input_1, input_2): primals_3 = self.xh.weight primals_8 = self.xh.bias primals_6 = self.hh.weight primals_10 = self.hh.bias primals_1 = self.xrz.weight primals_2 = self.xrz.bias primals_4 = self.hrz.weight primals_5 = self.hrz.bias primals_7 = input_0 primals_9 = input_1 primals_11 = input_2 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]
gushu333/DA4NMT
GRU
false
6,764
[ "Apache-2.0" ]
1
dba52a3d3784cd795b6f9aaf655b63475a848798
https://github.com/gushu333/DA4NMT/tree/dba52a3d3784cd795b6f9aaf655b63475a848798
Normalize
import torch import torch.utils.data import torch from torch import nn class Normalize(nn.Module): def __init__(self, power=2): super(Normalize, self).__init__() self.power = power def forward(self, x): norm = x.pow(self.power).sum(1, keepdim=True).pow(1.0 / self.power) out = x.div(norm + 1e-07) return out 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.utils.data import torch 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_add_div_pow_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') 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-07 tmp14 = tmp12 + tmp13 tmp15 = tmp0 / tmp14 tl.store(out_ptr0 + x3, tmp15, 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_pow_sum_0[grid(256)](arg0_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf0, class NormalizeNew(nn.Module): def __init__(self, power=2): super(NormalizeNew, self).__init__() self.power = power def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
guyii54/Contrastive-I2I
Normalize
false
6,765
[ "BSD-3-Clause" ]
1
e73daa0f9d3770c2280a304c39678d5b22440647
https://github.com/guyii54/Contrastive-I2I/tree/e73daa0f9d3770c2280a304c39678d5b22440647
GroupedChannelNorm
import torch import torch.utils.data import torch from torch import nn class GroupedChannelNorm(nn.Module): def __init__(self, num_groups): super().__init__() self.num_groups = num_groups def forward(self, x): shape = list(x.shape) new_shape = [shape[0], self.num_groups, shape[1] // self.num_groups ] + shape[2:] x = x.view(*new_shape) mean = x.mean(dim=2, keepdim=True) std = x.std(dim=2, keepdim=True) x_norm = (x - mean) / (std + 1e-07) return x_norm.view(*shape) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_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.triton_helpers import libdevice import torch.utils.data import torch 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_div_mean_std_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 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 = 4.0 tmp9 = tmp7 / tmp8 tmp10 = tmp0 - tmp9 tmp11 = tmp1 - tmp9 tmp12 = tmp11 * tmp11 tmp13 = tmp2 - tmp9 tmp14 = tmp13 * tmp13 tmp15 = tmp12 + tmp14 tmp16 = tmp4 - tmp9 tmp17 = tmp16 * tmp16 tmp18 = tmp15 + tmp17 tmp19 = tmp6 - tmp9 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp22 = 3.0 tmp23 = tmp21 / tmp22 tmp24 = libdevice.sqrt(tmp23) tmp25 = 1e-07 tmp26 = tmp24 + tmp25 tmp27 = tmp10 / tmp26 tl.store(out_ptr0 + x3, tmp27, 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, 4), (64, 1, 16, 4, 1), torch .float32) get_raw_stream(0) triton_poi_fused_add_div_mean_std_sub_0[grid(256)](arg0_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0), class GroupedChannelNormNew(nn.Module): def __init__(self, num_groups): super().__init__() self.num_groups = num_groups def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
guyii54/Contrastive-I2I
GroupedChannelNorm
false
6,766
[ "BSD-3-Clause" ]
1
e73daa0f9d3770c2280a304c39678d5b22440647
https://github.com/guyii54/Contrastive-I2I/tree/e73daa0f9d3770c2280a304c39678d5b22440647