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
AttentionModule
import torch from torch import nn from torch.nn import functional as F class AttentionModule(nn.Module): """ A neural module that takes a feature map and attention, attends to the features, and produces an attention. Extended Summary ---------------- A :class:`AttentionModule` takes input features and an attention and produces an attention. It multiplicatively combines its input feature map and attention to attend to the relevant region of the feature map. It then processes the attended features via a series of convolutions and produces an attention mask highlighting the objects that possess the attribute the module is looking for. For example, an :class:`AttentionModule` may be tasked with finding cubes. Given an input attention of all ones, it will highlight all the cubes in the provided input features. Given an attention mask highlighting all the red objects, it will produce an attention mask highlighting all the red cubes. Parameters ---------- dim: int The number of channels of each convolutional filter. """ def __init__(self, dim: 'int'): super().__init__() self.conv1 = nn.Conv2d(dim, dim, kernel_size=3, padding=1) self.conv2 = nn.Conv2d(dim, dim, kernel_size=3, padding=1) self.conv3 = nn.Conv2d(dim, 1, kernel_size=1, padding=0) torch.nn.init.kaiming_normal_(self.conv1.weight) torch.nn.init.kaiming_normal_(self.conv2.weight) torch.nn.init.kaiming_normal_(self.conv3.weight) self.dim = dim def forward(self, feats, attn): attended_feats = torch.mul(feats, attn.repeat(1, self.dim, 1, 1)) out = F.relu(self.conv1(attended_feats)) out = F.relu(self.conv2(out)) out = torch.sigmoid(self.conv3(out)) return out def get_inputs(): return [torch.rand([4, 4, 64, 64]), torch.rand([4, 1, 64, 64])] 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 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_repeat_0(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 x0 = xindex % 4096 x2 = xindex // 16384 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + (x0 + 4096 * x2), None, eviction_policy= 'evict_last') tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x3, tmp2, None) @triton.jit def triton_poi_fused_convolution_relu_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) x3 = xindex x1 = xindex // 4096 % 4 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_sigmoid_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) x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, None) 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, None) 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, 64, 64), (4096, 4096, 64, 1)) assert_size_stride(primals_2, (4, 4, 64, 64), (16384, 4096, 64, 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, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (1, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_8, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 64, 64), (16384, 4096, 64, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_repeat_0[grid(65536)](primals_2, primals_1, buf0, 65536, 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=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 64, 64), (16384, 4096, 64, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_relu_1[grid(65536)](buf2, primals_4, 65536, XBLOCK=512, 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, 64, 64), (16384, 4096, 64, 1)) buf4 = buf3 del buf3 triton_poi_fused_convolution_relu_1[grid(65536)](buf4, primals_6, 65536, XBLOCK=512, num_warps=4, num_stages=1) del primals_6 buf5 = extern_kernels.convolution(buf4, primals_7, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf5, (4, 1, 64, 64), (4096, 4096, 64, 1)) buf6 = buf5 del buf5 triton_poi_fused_convolution_sigmoid_2[grid(16384)](buf6, primals_8, 16384, XBLOCK=256, num_warps=4, num_stages=1) del primals_8 return buf6, primals_3, primals_5, primals_7, buf0, buf2, buf4, buf6 class AttentionModuleNew(nn.Module): """ A neural module that takes a feature map and attention, attends to the features, and produces an attention. Extended Summary ---------------- A :class:`AttentionModule` takes input features and an attention and produces an attention. It multiplicatively combines its input feature map and attention to attend to the relevant region of the feature map. It then processes the attended features via a series of convolutions and produces an attention mask highlighting the objects that possess the attribute the module is looking for. For example, an :class:`AttentionModule` may be tasked with finding cubes. Given an input attention of all ones, it will highlight all the cubes in the provided input features. Given an attention mask highlighting all the red objects, it will produce an attention mask highlighting all the red cubes. Parameters ---------- dim: int The number of channels of each convolutional filter. """ def __init__(self, dim: 'int'): super().__init__() self.conv1 = nn.Conv2d(dim, dim, kernel_size=3, padding=1) self.conv2 = nn.Conv2d(dim, dim, kernel_size=3, padding=1) self.conv3 = nn.Conv2d(dim, 1, kernel_size=1, padding=0) torch.nn.init.kaiming_normal_(self.conv1.weight) torch.nn.init.kaiming_normal_(self.conv2.weight) torch.nn.init.kaiming_normal_(self.conv3.weight) self.dim = dim def forward(self, input_0, input_1): primals_3 = self.conv1.weight primals_4 = self.conv1.bias primals_5 = self.conv2.weight primals_6 = self.conv2.bias primals_7 = self.conv3.weight primals_8 = self.conv3.bias primals_2 = input_0 primals_1 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8]) return output[0]
kdexd/probnmn-clevr
AttentionModule
false
15,795
[ "MIT" ]
69
9c1b2286cf30e9fb045370153c9242a39760e02e
https://github.com/kdexd/probnmn-clevr/tree/9c1b2286cf30e9fb045370153c9242a39760e02e
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 subtracting the average advantage so that we can properly """ 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 subtracting the average advantage so that we can properly """ def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kcorder/autonomous-learning-library
Aggregation
false
15,796
[ "MIT" ]
584
0266195fa47564e51a32087bc007bff6dda5e263
https://github.com/kcorder/autonomous-learning-library/tree/0266195fa47564e51a32087bc007bff6dda5e263
FeatureCorrelation
import torch import torch.nn as nn class FeatureCorrelation(nn.Module): def __init__(self, scale): super(FeatureCorrelation, self).__init__() self.scale = scale def forward(self, feature_A, feature_B): b, c, h, w = feature_A.size() feature_A = feature_A.transpose(2, 3).contiguous().view(b, c, h * w) feature_B = feature_B.view(b, c, h * w).transpose(1, 2) feature_mul = torch.bmm(feature_B, feature_A) correlation_tensor = self.scale * feature_mul.view(b, h, w, h * w ).transpose(2, 3).transpose(1, 2) return correlation_tensor def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'scale': 1.0}]
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_clone_0(in_ptr0, 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 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_mul_1(in_out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = 1.0 tmp2 = tmp0 * tmp1 tl.store(in_out_ptr0 + x0, tmp2, 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_clone_0[grid(64, 4)](arg0_1, buf0, 64, 4, XBLOCK=4, YBLOCK=32, num_warps=4, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((4, 16, 16), (256, 16, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(arg1_1, (4, 16, 4), (64, 1, 16), 0), reinterpret_tensor(buf0, (4, 4, 16), (64, 16, 1), 0), out=buf1) del arg1_1 del buf0 buf2 = reinterpret_tensor(buf1, (4, 16, 4, 4), (256, 1, 64, 16), 0) del buf1 triton_poi_fused_mul_1[grid(1024)](buf2, 1024, XBLOCK=256, num_warps=4, num_stages=1) return buf2, class FeatureCorrelationNew(nn.Module): def __init__(self, scale): super(FeatureCorrelationNew, self).__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]
kensakurada/SceneChangeDet
FeatureCorrelation
false
15,797
[ "MIT" ]
199
0530e0162863fec0c5296188526f0d27e0109814
https://github.com/kensakurada/SceneChangeDet/tree/0530e0162863fec0c5296188526f0d27e0109814
KLCoefficient
import torch import torch.nn as nn import torch.nn.functional as F class KLCoefficient(nn.Module): def __init__(self): super(KLCoefficient, self).__init__() def forward(self, hist1, hist2): kl = F.kl_div(hist1, hist2) dist = 1.0 / 1 + kl return dist def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_mean_mul_sub_xlogy_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) tmp9 = tl.load(in_ptr1 + r0, None) tmp1 = libdevice.isnan(tmp0).to(tl.int1) tmp2 = 0.0 tmp3 = tmp0 == tmp2 tmp4 = tl_math.log(tmp0) tmp5 = tmp0 * tmp4 tmp6 = tl.where(tmp3, tmp2, tmp5) tmp7 = float('nan') tmp8 = tl.where(tmp1, tmp7, tmp6) tmp10 = tmp0 * tmp9 tmp11 = tmp8 - tmp10 tmp12 = tl.broadcast_to(tmp11, [RBLOCK]) tmp14 = triton_helpers.promote_to_tensor(tl.sum(tmp12, 0)) tmp15 = 256.0 tmp16 = tmp14 / tmp15 tmp17 = 1.0 tmp18 = tmp16 + tmp17 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp18, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_mean_mul_sub_xlogy_0[grid(1)](buf1, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, class KLCoefficientNew(nn.Module): def __init__(self): super(KLCoefficientNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kensakurada/SceneChangeDet
KLCoefficient
false
15,798
[ "MIT" ]
199
0530e0162863fec0c5296188526f0d27e0109814
https://github.com/kensakurada/SceneChangeDet/tree/0530e0162863fec0c5296188526f0d27e0109814
RelateModule
import torch from torch import nn from torch.nn import functional as F class RelateModule(nn.Module): """ A neural module that takes as input a feature map and an attention and produces an attention as output. Extended Summary ---------------- A :class:`RelateModule` takes input features and an attention and produces an attention. It multiplicatively combines the attention and the features to attend to a relevant region, then uses a series of dilated convolutional filters to indicate a spatial relationship to the input attended region. Parameters ---------- dim: int The number of channels of each convolutional filter. """ def __init__(self, dim: 'int'): super().__init__() self.conv1 = nn.Conv2d(dim, dim, kernel_size=3, padding=1, dilation=1) self.conv2 = nn.Conv2d(dim, dim, kernel_size=3, padding=2, dilation=2) self.conv3 = nn.Conv2d(dim, dim, kernel_size=3, padding=4, dilation=4) self.conv4 = nn.Conv2d(dim, dim, kernel_size=3, padding=8, dilation=8) self.conv5 = nn.Conv2d(dim, dim, kernel_size=3, padding=1, dilation=1) self.conv6 = nn.Conv2d(dim, 1, kernel_size=1, padding=0) torch.nn.init.kaiming_normal_(self.conv1.weight) torch.nn.init.kaiming_normal_(self.conv2.weight) torch.nn.init.kaiming_normal_(self.conv3.weight) torch.nn.init.kaiming_normal_(self.conv4.weight) torch.nn.init.kaiming_normal_(self.conv5.weight) torch.nn.init.kaiming_normal_(self.conv6.weight) self.dim = dim def forward(self, feats, attn): feats = torch.mul(feats, attn.repeat(1, self.dim, 1, 1)) out = F.relu(self.conv1(feats)) out = F.relu(self.conv2(out)) out = F.relu(self.conv3(out)) out = F.relu(self.conv4(out)) out = F.relu(self.conv5(out)) out = torch.sigmoid(self.conv6(out)) return out def get_inputs(): return [torch.rand([4, 4, 64, 64]), torch.rand([4, 1, 64, 64])] 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 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_repeat_0(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 x0 = xindex % 4096 x2 = xindex // 16384 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + (x0 + 4096 * x2), None, eviction_policy= 'evict_last') tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x3, tmp2, None) @triton.jit def triton_poi_fused_convolution_relu_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) x3 = xindex x1 = xindex // 4096 % 4 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_sigmoid_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) x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, None) 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, 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) = args args.clear() assert_size_stride(primals_1, (4, 1, 64, 64), (4096, 4096, 64, 1)) assert_size_stride(primals_2, (4, 4, 64, 64), (16384, 4096, 64, 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, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_8, (4,), (1,)) assert_size_stride(primals_9, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_10, (4,), (1,)) assert_size_stride(primals_11, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_12, (4,), (1,)) assert_size_stride(primals_13, (1, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_14, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 64, 64), (16384, 4096, 64, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_repeat_0[grid(65536)](primals_2, primals_1, buf0, 65536, 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=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 64, 64), (16384, 4096, 64, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_relu_1[grid(65536)](buf2, primals_4, 65536, XBLOCK=512, num_warps=4, num_stages=1) del primals_4 buf3 = extern_kernels.convolution(buf2, primals_5, stride=(1, 1), padding=(2, 2), dilation=(2, 2), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf3, (4, 4, 64, 64), (16384, 4096, 64, 1)) buf4 = buf3 del buf3 triton_poi_fused_convolution_relu_1[grid(65536)](buf4, primals_6, 65536, XBLOCK=512, num_warps=4, num_stages=1) del primals_6 buf5 = extern_kernels.convolution(buf4, primals_7, stride=(1, 1), padding=(4, 4), dilation=(4, 4), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf5, (4, 4, 64, 64), (16384, 4096, 64, 1)) buf6 = buf5 del buf5 triton_poi_fused_convolution_relu_1[grid(65536)](buf6, primals_8, 65536, XBLOCK=512, num_warps=4, num_stages=1) del primals_8 buf7 = extern_kernels.convolution(buf6, primals_9, stride=(1, 1), padding=(8, 8), dilation=(8, 8), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf7, (4, 4, 64, 64), (16384, 4096, 64, 1)) buf8 = buf7 del buf7 triton_poi_fused_convolution_relu_1[grid(65536)](buf8, primals_10, 65536, XBLOCK=512, num_warps=4, num_stages=1) del primals_10 buf9 = extern_kernels.convolution(buf8, primals_11, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf9, (4, 4, 64, 64), (16384, 4096, 64, 1)) buf10 = buf9 del buf9 triton_poi_fused_convolution_relu_1[grid(65536)](buf10, primals_12, 65536, XBLOCK=512, num_warps=4, num_stages=1) del primals_12 buf11 = extern_kernels.convolution(buf10, primals_13, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf11, (4, 1, 64, 64), (4096, 4096, 64, 1)) buf12 = buf11 del buf11 triton_poi_fused_convolution_sigmoid_2[grid(16384)](buf12, primals_14, 16384, XBLOCK=256, num_warps=4, num_stages=1) del primals_14 return (buf12, primals_3, primals_5, primals_7, primals_9, primals_11, primals_13, buf0, buf2, buf4, buf6, buf8, buf10, buf12) class RelateModuleNew(nn.Module): """ A neural module that takes as input a feature map and an attention and produces an attention as output. Extended Summary ---------------- A :class:`RelateModule` takes input features and an attention and produces an attention. It multiplicatively combines the attention and the features to attend to a relevant region, then uses a series of dilated convolutional filters to indicate a spatial relationship to the input attended region. Parameters ---------- dim: int The number of channels of each convolutional filter. """ def __init__(self, dim: 'int'): super().__init__() self.conv1 = nn.Conv2d(dim, dim, kernel_size=3, padding=1, dilation=1) self.conv2 = nn.Conv2d(dim, dim, kernel_size=3, padding=2, dilation=2) self.conv3 = nn.Conv2d(dim, dim, kernel_size=3, padding=4, dilation=4) self.conv4 = nn.Conv2d(dim, dim, kernel_size=3, padding=8, dilation=8) self.conv5 = nn.Conv2d(dim, dim, kernel_size=3, padding=1, dilation=1) self.conv6 = nn.Conv2d(dim, 1, kernel_size=1, padding=0) torch.nn.init.kaiming_normal_(self.conv1.weight) torch.nn.init.kaiming_normal_(self.conv2.weight) torch.nn.init.kaiming_normal_(self.conv3.weight) torch.nn.init.kaiming_normal_(self.conv4.weight) torch.nn.init.kaiming_normal_(self.conv5.weight) torch.nn.init.kaiming_normal_(self.conv6.weight) self.dim = dim def forward(self, input_0, input_1): primals_3 = self.conv1.weight primals_4 = self.conv1.bias primals_5 = self.conv2.weight primals_6 = self.conv2.bias primals_7 = self.conv3.weight primals_8 = self.conv3.bias primals_9 = self.conv4.weight primals_10 = self.conv4.bias primals_11 = self.conv5.weight primals_12 = self.conv5.bias primals_13 = self.conv6.weight primals_14 = self.conv6.bias primals_2 = input_0 primals_1 = 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, primals_13, primals_14]) return output[0]
kdexd/probnmn-clevr
RelateModule
false
15,799
[ "MIT" ]
69
9c1b2286cf30e9fb045370153c9242a39760e02e
https://github.com/kdexd/probnmn-clevr/tree/9c1b2286cf30e9fb045370153c9242a39760e02e
l2normalization
import torch import torch.nn as nn class l2normalization(nn.Module): def __init__(self, scale): super(l2normalization, self).__init__() self.scale = scale def forward(self, x, dim=1): """out = scale * x / sqrt(\\sum x_i^2)""" return self.scale * x * x.pow(2).sum(dim).clamp(min=1e-12).rsqrt( ).expand_as(x) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'scale': 1.0}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn 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 x3 = xindex x0 = xindex % 16 x1 = xindex // 16 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp3 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask, eviction_policy= 'evict_last') tmp8 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask, eviction_policy= 'evict_last') tmp11 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask, eviction_policy= 'evict_last') tmp1 = 1.0 tmp2 = tmp0 * tmp1 tmp4 = tmp3 * tmp3 tmp6 = tmp5 * tmp5 tmp7 = tmp4 + tmp6 tmp9 = tmp8 * tmp8 tmp10 = tmp7 + tmp9 tmp12 = tmp11 * tmp11 tmp13 = tmp10 + tmp12 tmp14 = 1e-12 tmp15 = triton_helpers.maximum(tmp13, tmp14) tmp16 = libdevice.rsqrt(tmp15) tmp17 = tmp2 * 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_mul_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, class l2normalizationNew(nn.Module): def __init__(self, scale): super(l2normalizationNew, self).__init__() self.scale = scale def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
kensakurada/SceneChangeDet
l2normalization
false
15,800
[ "MIT" ]
199
0530e0162863fec0c5296188526f0d27e0109814
https://github.com/kensakurada/SceneChangeDet/tree/0530e0162863fec0c5296188526f0d27e0109814
StatsNet
import torch from torch import nn import torch.utils.data class StatsNet(nn.Module): def __init__(self): super(StatsNet, self).__init__() def forward(self, x): x = x.view(x.data.shape[0], x.data.shape[1], x.data.shape[2] * x. data.shape[3]) mean = torch.mean(x, 2) std = torch.std(x, 2) return torch.stack((mean, std), 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 libdevice from torch import nn import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_per_fused_mean_std_0(in_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 x3 = xindex // 4 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] tmp6 = tl.broadcast_to(tmp1, [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 = tmp1 - 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 = tmp4 / tmp19 tmp21 = 15.0 tmp22 = tmp18 / tmp21 tmp23 = libdevice.sqrt(tmp22) tl.store(out_ptr2 + (x2 + 8 * x3), tmp20, xmask) tl.store(out_ptr3 + (x2 + 8 * x3), tmp23, 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) buf6 = empty_strided_cuda((4, 8), (8, 1), torch.float32) buf4 = reinterpret_tensor(buf6, (4, 4), (8, 1), 0) buf5 = reinterpret_tensor(buf6, (4, 4), (8, 1), 4) get_raw_stream(0) triton_per_fused_mean_std_0[grid(16)](arg0_1, buf4, buf5, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 return reinterpret_tensor(buf6, (4, 2, 4), (8, 4, 1), 0), class StatsNetNew(nn.Module): def __init__(self): super(StatsNetNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
kerenalli/Capsule-Forensics-v2
StatsNet
false
15,801
[ "BSD-3-Clause" ]
97
8e60ca0035f8392a543f7fad37ab3704d43021cf
https://github.com/kerenalli/Capsule-Forensics-v2/tree/8e60ca0035f8392a543f7fad37ab3704d43021cf
GaussianKLLoss
import torch import torch.nn as nn class GaussianKLLoss(nn.Module): def __init__(self): super(GaussianKLLoss, self).__init__() def forward(self, mu1, logvar1, mu2, logvar2): numerator = logvar1.exp() + torch.pow(mu1 - mu2, 2) fraction = torch.div(numerator, logvar2.exp()) kl = 0.5 * torch.sum(logvar2 - logvar1 + fraction - 1, dim=1) return kl.mean(dim=0) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand( [4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math import torch.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_exp_pow_sub_sum_0(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 % 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_ptr2 + (x0 + 64 * x1), xmask) tmp5 = tl.load(in_ptr3 + (x0 + 64 * x1), xmask) tmp14 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask) tmp15 = tl.load(in_ptr1 + (16 + x0 + 64 * x1), xmask) tmp18 = tl.load(in_ptr2 + (16 + x0 + 64 * x1), xmask) tmp19 = tl.load(in_ptr3 + (16 + x0 + 64 * x1), xmask) tmp28 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask) tmp29 = tl.load(in_ptr1 + (32 + x0 + 64 * x1), xmask) tmp32 = tl.load(in_ptr2 + (32 + x0 + 64 * x1), xmask) tmp33 = tl.load(in_ptr3 + (32 + x0 + 64 * x1), xmask) tmp42 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask) tmp43 = tl.load(in_ptr1 + (48 + x0 + 64 * x1), xmask) tmp46 = tl.load(in_ptr2 + (48 + x0 + 64 * x1), xmask) tmp47 = tl.load(in_ptr3 + (48 + x0 + 64 * x1), xmask) tmp2 = tmp0 - tmp1 tmp3 = tl_math.exp(tmp1) tmp6 = tmp4 - tmp5 tmp7 = tmp6 * tmp6 tmp8 = tmp3 + tmp7 tmp9 = tl_math.exp(tmp0) tmp10 = tmp8 / tmp9 tmp11 = tmp2 + tmp10 tmp12 = 1.0 tmp13 = tmp11 - tmp12 tmp16 = tmp14 - tmp15 tmp17 = tl_math.exp(tmp15) tmp20 = tmp18 - tmp19 tmp21 = tmp20 * tmp20 tmp22 = tmp17 + tmp21 tmp23 = tl_math.exp(tmp14) tmp24 = tmp22 / tmp23 tmp25 = tmp16 + tmp24 tmp26 = tmp25 - tmp12 tmp27 = tmp13 + tmp26 tmp30 = tmp28 - tmp29 tmp31 = tl_math.exp(tmp29) tmp34 = tmp32 - tmp33 tmp35 = tmp34 * tmp34 tmp36 = tmp31 + tmp35 tmp37 = tl_math.exp(tmp28) tmp38 = tmp36 / tmp37 tmp39 = tmp30 + tmp38 tmp40 = tmp39 - tmp12 tmp41 = tmp27 + tmp40 tmp44 = tmp42 - tmp43 tmp45 = tl_math.exp(tmp43) tmp48 = tmp46 - tmp47 tmp49 = tmp48 * tmp48 tmp50 = tmp45 + tmp49 tmp51 = tl_math.exp(tmp42) tmp52 = tmp50 / tmp51 tmp53 = tmp44 + tmp52 tmp54 = tmp53 - tmp12 tmp55 = tmp41 + tmp54 tl.store(out_ptr0 + x2, tmp55, xmask) @triton.jit def triton_poi_fused_mean_mul_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp3 = tl.load(in_ptr0 + (16 + x0), xmask) tmp6 = tl.load(in_ptr0 + (32 + x0), xmask) tmp9 = tl.load(in_ptr0 + (48 + x0), xmask) tmp1 = 0.5 tmp2 = tmp0 * tmp1 tmp4 = tmp3 * tmp1 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp1 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp1 tmp11 = tmp8 + tmp10 tmp12 = 4.0 tmp13 = tmp11 / tmp12 tl.store(out_ptr0 + x0, tmp13, xmask) def call(args): arg0_1, arg1_1, arg2_1, arg3_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)) assert_size_stride(arg3_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_exp_pow_sub_sum_0[grid(64)](arg3_1, arg0_1, arg1_1, arg2_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 del arg1_1 del arg2_1 del arg3_1 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_mean_mul_1[grid(16)](buf0, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf0 return buf1, class GaussianKLLossNew(nn.Module): def __init__(self): super(GaussianKLLossNew, self).__init__() def forward(self, input_0, input_1, input_2, input_3): 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]) return output[0]
kekayan/Info-HCVAE
GaussianKLLoss
false
15,802
[ "Apache-2.0" ]
120
1f4d536523767f439e689d8963c54a55fb75c6f9
https://github.com/kekayan/Info-HCVAE/tree/1f4d536523767f439e689d8963c54a55fb75c6f9
BhattacharyyaDistance
import torch import torch.nn as nn class BhattacharyyaDistance(nn.Module): def __init__(self): super(BhattacharyyaDistance, self).__init__() def forward(self, hist1, hist2): bh_dist = torch.sqrt(hist1 * hist2).sum() return bh_dist def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice 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_mul_sqrt_sum_0(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel ): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.load(in_ptr1 + r0, None) tmp2 = tmp0 * tmp1 tmp3 = libdevice.sqrt(tmp2) tmp4 = tl.broadcast_to(tmp3, [RBLOCK]) tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0)) tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp6, 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) get_raw_stream(0) triton_per_fused_mul_sqrt_sum_0[grid(1)](arg0_1, arg1_1, buf0, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf0, class BhattacharyyaDistanceNew(nn.Module): def __init__(self): super(BhattacharyyaDistanceNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kensakurada/SceneChangeDet
BhattacharyyaDistance
false
15,803
[ "MIT" ]
199
0530e0162863fec0c5296188526f0d27e0109814
https://github.com/kensakurada/SceneChangeDet/tree/0530e0162863fec0c5296188526f0d27e0109814
Perplexity
import torch import torch as t import torch.nn as nn import torch.nn.functional as F class Perplexity(nn.Module): def __init__(self): super(Perplexity, self).__init__() def forward(self, logits, target): """ :param logits: tensor with shape of [batch_size, seq_len, input_size] :param target: tensor with shape of [batch_size, seq_len] of Long type filled with indexes to gather from logits :return: tensor with shape of [batch_size] with perplexity evaluation """ [batch_size, seq_len, input_size] = logits.size() logits = logits.view(-1, input_size) log_probs = F.log_softmax(logits) del logits log_probs = log_probs.view(batch_size, seq_len, input_size) target = target.unsqueeze(2) out = t.gather(log_probs, dim=2, index=target).squeeze(2).neg() ppl = out.mean(1).exp() return ppl def get_inputs(): return [torch.rand([4, 4, 4]), torch.ones([4, 4], dtype=torch.int64)] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused__log_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 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_exp_mean_neg_1(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr1 + 16 * x0, xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr1 + (1 + 16 * x0), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (2 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp15 = tl.load(in_ptr1 + (3 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp21 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp27 = tl.load(in_ptr1 + (4 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp29 = tl.load(in_ptr1 + (5 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp32 = tl.load(in_ptr1 + (6 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp35 = tl.load(in_ptr1 + (7 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp42 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp48 = tl.load(in_ptr1 + (8 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp50 = tl.load(in_ptr1 + (9 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp53 = tl.load(in_ptr1 + (10 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp56 = tl.load(in_ptr1 + (11 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp63 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp69 = tl.load(in_ptr1 + (12 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp71 = tl.load(in_ptr1 + (13 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp74 = tl.load(in_ptr1 + (14 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp77 = tl.load(in_ptr1 + (15 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp1 = tl.full([XBLOCK], 4, tl.int32) tmp2 = tmp0 + tmp1 tmp3 = tmp0 < 0 tmp4 = tl.where(tmp3, tmp2, tmp0) tl.device_assert((0 <= tmp4) & (tmp4 < 4) | ~xmask, 'index out of bounds: 0 <= tmp4 < 4') tmp6 = tl.load(in_ptr1 + (tmp4 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp8 = tl_math.exp(tmp7) tmp10 = tl_math.exp(tmp9) tmp11 = tmp8 + tmp10 tmp13 = tl_math.exp(tmp12) tmp14 = tmp11 + tmp13 tmp16 = tl_math.exp(tmp15) tmp17 = tmp14 + tmp16 tmp18 = tl_math.log(tmp17) tmp19 = tmp6 - tmp18 tmp20 = -tmp19 tmp22 = tmp21 + tmp1 tmp23 = tmp21 < 0 tmp24 = tl.where(tmp23, tmp22, tmp21) tl.device_assert((0 <= tmp24) & (tmp24 < 4) | ~xmask, 'index out of bounds: 0 <= tmp24 < 4') tmp26 = tl.load(in_ptr1 + (4 + tmp24 + 16 * x0), xmask, eviction_policy ='evict_last') tmp28 = tl_math.exp(tmp27) tmp30 = tl_math.exp(tmp29) tmp31 = tmp28 + tmp30 tmp33 = tl_math.exp(tmp32) tmp34 = tmp31 + tmp33 tmp36 = tl_math.exp(tmp35) tmp37 = tmp34 + tmp36 tmp38 = tl_math.log(tmp37) tmp39 = tmp26 - tmp38 tmp40 = -tmp39 tmp41 = tmp20 + tmp40 tmp43 = tmp42 + tmp1 tmp44 = tmp42 < 0 tmp45 = tl.where(tmp44, tmp43, tmp42) tl.device_assert((0 <= tmp45) & (tmp45 < 4) | ~xmask, 'index out of bounds: 0 <= tmp45 < 4') tmp47 = tl.load(in_ptr1 + (8 + tmp45 + 16 * x0), xmask, eviction_policy ='evict_last') tmp49 = tl_math.exp(tmp48) tmp51 = tl_math.exp(tmp50) tmp52 = tmp49 + tmp51 tmp54 = tl_math.exp(tmp53) tmp55 = tmp52 + tmp54 tmp57 = tl_math.exp(tmp56) tmp58 = tmp55 + tmp57 tmp59 = tl_math.log(tmp58) tmp60 = tmp47 - tmp59 tmp61 = -tmp60 tmp62 = tmp41 + tmp61 tmp64 = tmp63 + tmp1 tmp65 = tmp63 < 0 tmp66 = tl.where(tmp65, tmp64, tmp63) tl.device_assert((0 <= tmp66) & (tmp66 < 4) | ~xmask, 'index out of bounds: 0 <= tmp66 < 4') tmp68 = tl.load(in_ptr1 + (12 + tmp66 + 16 * x0), xmask, eviction_policy='evict_last') tmp70 = tl_math.exp(tmp69) tmp72 = tl_math.exp(tmp71) tmp73 = tmp70 + tmp72 tmp75 = tl_math.exp(tmp74) tmp76 = tmp73 + tmp75 tmp78 = tl_math.exp(tmp77) tmp79 = tmp76 + tmp78 tmp80 = tl_math.log(tmp79) tmp81 = tmp68 - tmp80 tmp82 = -tmp81 tmp83 = tmp62 + tmp82 tmp84 = 4.0 tmp85 = tmp83 / tmp84 tmp86 = tl_math.exp(tmp85) tl.store(in_out_ptr0 + x0, tmp86, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(arg1_1, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused__log_softmax_0[grid(64)](arg0_1, buf0, 64, XBLOCK= 64, num_warps=1, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((4,), (1,), torch.float32) buf2 = buf1 del buf1 triton_poi_fused_exp_mean_neg_1[grid(4)](buf2, arg1_1, buf0, 4, XBLOCK=4, num_warps=1, num_stages=1) del arg1_1 del buf0 return buf2, class PerplexityNew(nn.Module): def __init__(self): super(PerplexityNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kefirski/contiguous-succotash
Perplexity
false
15,804
[ "MIT" ]
57
7497efd1392693248ed98805dcdbbf5dc125afc2
https://github.com/kefirski/contiguous-succotash/tree/7497efd1392693248ed98805dcdbbf5dc125afc2
StableBCELoss
import torch from torch import nn class StableBCELoss(nn.Module): def __init__(self): super(StableBCELoss, self).__init__() def forward(self, input: 'torch.Tensor', target: 'torch.Tensor'): input = input.float().view(-1) target = target.float().view(-1) neg_abs = -input.abs() loss = input.clamp(min=0) - input * target + (1 + neg_abs.exp()).log() return loss.mean() def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math 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_add_clamp_exp_log_mean_mul_neg_sub_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp3 = tl.load(in_ptr1 + r0, None) tmp1 = 0.0 tmp2 = triton_helpers.maximum(tmp0, tmp1) tmp4 = tmp0 * tmp3 tmp5 = tmp2 - tmp4 tmp6 = tl_math.abs(tmp0) tmp7 = -tmp6 tmp8 = tl_math.exp(tmp7) tmp9 = 1.0 tmp10 = tmp8 + tmp9 tmp11 = tl_math.log(tmp10) tmp12 = tmp5 + tmp11 tmp13 = tl.broadcast_to(tmp12, [RBLOCK]) tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0)) tmp16 = 256.0 tmp17 = tmp15 / tmp16 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp17, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_abs_add_clamp_exp_log_mean_mul_neg_sub_0[grid(1)](buf1 , arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, class StableBCELossNew(nn.Module): def __init__(self): super(StableBCELossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kevinkwshin/kaggle-pneumothorax
StableBCELoss
false
15,805
[ "MIT" ]
74
24b91a9425097023f0cc7781a9380cb247babe22
https://github.com/kevinkwshin/kaggle-pneumothorax/tree/24b91a9425097023f0cc7781a9380cb247babe22
MultiHeadedAttention
import math import torch import numpy as np from typing import Optional from torch import nn class MultiHeadedAttention(nn.Module): """Multi-Head Attention layer :param int n_head: the number of head s :param int n_feat: the number of features :param float dropout_rate: dropout rate """ def __init__(self, n_head: 'int', n_feat: 'int', dropout_rate: 'float'): super(MultiHeadedAttention, self).__init__() assert n_feat % n_head == 0 self.d_k = n_feat // n_head self.h = n_head self.linear_q = nn.Linear(n_feat, n_feat) self.linear_k = nn.Linear(n_feat, n_feat) self.linear_v = nn.Linear(n_feat, n_feat) self.linear_out = nn.Linear(n_feat, n_feat) self.dropout = nn.Dropout(p=dropout_rate) def forward(self, query: 'torch.Tensor', key: 'torch.Tensor', value: 'torch.Tensor', mask: 'Optional[torch.Tensor]'=None) ->torch.Tensor: """Compute 'Scaled Dot Product Attention' :param torch.Tensor query: (batch, time1, size) :param torch.Tensor key: (batch, time2, size) :param torch.Tensor value: (batch, time2, size) :param torch.Tensor mask: (batch, time1, time2) :param torch.nn.Dropout dropout: :return torch.Tensor: attentined and transformed `value` (batch, time1, d_model) weighted by the query dot key attention (batch, head, time1, time2) """ n_batch = query.size(0) q = self.linear_q(query).view(n_batch, -1, self.h, self.d_k) k = self.linear_k(key).view(n_batch, -1, self.h, self.d_k) v = self.linear_v(value).view(n_batch, -1, self.h, self.d_k) q = q.transpose(1, 2) k = k.transpose(1, 2) v = v.transpose(1, 2) scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.d_k) if mask is not None: mask = mask.unsqueeze(1).eq(0) mask = mask scores = scores.masked_fill_(mask, -np.inf) attn = torch.softmax(scores, dim=-1).masked_fill(mask, 0.0) else: attn = torch.softmax(scores, dim=-1) p_attn = self.dropout(attn) x = torch.matmul(p_attn, v) x = x.transpose(1, 2).contiguous().view(n_batch, -1, self.h * self.d_k) return self.linear_out(x) 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 [[], {'n_head': 4, 'n_feat': 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 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_0(in_ptr0, in_ptr1, 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 y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 64 * y1), xmask & ymask) 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 + 16 * y3), tmp4, xmask & ymask) @triton.jit def triton_per_fused_1(in_ptr0, out_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr ): xnumel = 256 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 = float('-inf') tmp12 = tmp0 == tmp11 tmp13 = tmp12 == 0 tmp14 = tmp13.to(tl.int64) tmp15 = tmp14 != 0 tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK]) tmp18 = tl.where(xmask, tmp16, 0) tmp19 = triton_helpers.any(tmp18, 1)[:, None] tmp20 = tmp19 == 0 tmp21 = tmp6 / tmp10 tmp22 = 0.0 tmp23 = tl.where(tmp20, tmp22, tmp21) tl.store(out_ptr3 + (r1 + 16 * x0), tmp23, xmask) @triton.jit def triton_poi_fused_2(in_ptr0, in_ptr1, 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 y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 64 * y1), xmask & ymask) 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_clone_3(in_ptr0, 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 x2 = xindex y0 = yindex % 16 y1 = yindex // 16 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 16 * x2 + 64 * y1), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11) = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4, 4, 4), (64, 16, 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, 4), (64, 16, 4, 1)) assert_size_stride(primals_10, (4, 4), (4, 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_1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_6, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1) del primals_4 buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_9, (64, 4), (4, 1), 0), reinterpret_tensor(primals_7, (4, 4), (1, 4), 0), out=buf2) del primals_7 buf3 = empty_strided_cuda((4, 4, 16, 1), (64, 16, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_0[grid(16, 16)](buf0, primals_3, buf3, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) del primals_3 buf4 = reinterpret_tensor(buf0, (4, 4, 1, 16), (64, 16, 16, 1), 0) del buf0 triton_poi_fused_0[grid(16, 16)](buf1, primals_5, buf4, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) del primals_5 buf5 = empty_strided_cuda((16, 16, 16), (256, 16, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf3, (16, 16, 1), (16, 1, 0), 0), reinterpret_tensor(buf4, (16, 1, 16), (16, 0, 1), 0), out=buf5) buf9 = empty_strided_cuda((4, 4, 16, 16), (1024, 256, 16, 1), torch .float32) triton_per_fused_1[grid(256)](buf5, buf9, 256, 16, XBLOCK=8, num_warps=2, num_stages=1) del buf5 buf10 = reinterpret_tensor(buf1, (4, 4, 16, 1), (64, 16, 1, 1), 0) del buf1 triton_poi_fused_2[grid(16, 16)](buf2, primals_8, buf10, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) del primals_8 buf11 = reinterpret_tensor(buf2, (16, 16, 1), (16, 1, 1), 0) del buf2 extern_kernels.bmm(reinterpret_tensor(buf9, (16, 16, 16), (256, 16, 1), 0), reinterpret_tensor(buf10, (16, 16, 1), (16, 1, 0), 0), out=buf11) buf12 = empty_strided_cuda((4, 16, 4, 1), (64, 4, 1, 1), torch.float32) triton_poi_fused_clone_3[grid(64, 4)](buf11, buf12, 64, 4, XBLOCK=4, YBLOCK=32, num_warps=4, num_stages=1) buf13 = reinterpret_tensor(buf11, (64, 4), (4, 1), 0) del buf11 extern_kernels.addmm(primals_11, reinterpret_tensor(buf12, (64, 4), (4, 1), 0), reinterpret_tensor(primals_10, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf13) del primals_11 return reinterpret_tensor(buf13, (4, 16, 4), (64, 4, 1), 0 ), reinterpret_tensor(primals_1, (64, 4), (4, 1), 0 ), reinterpret_tensor(primals_6, (64, 4), (4, 1), 0 ), reinterpret_tensor(primals_9, (64, 4), (4, 1), 0 ), buf9, reinterpret_tensor(buf10, (16, 1, 16), (16, 1, 1), 0 ), reinterpret_tensor(buf3, (16, 1, 16), (16, 1, 1), 0 ), reinterpret_tensor(buf4, (16, 16, 1), (16, 1, 16), 0 ), reinterpret_tensor(buf12, (64, 4), (4, 1), 0), primals_10 class MultiHeadedAttentionNew(nn.Module): """Multi-Head Attention layer :param int n_head: the number of head s :param int n_feat: the number of features :param float dropout_rate: dropout rate """ def __init__(self, n_head: 'int', n_feat: 'int', dropout_rate: 'float'): super(MultiHeadedAttentionNew, self).__init__() assert n_feat % n_head == 0 self.d_k = n_feat // n_head self.h = n_head self.linear_q = nn.Linear(n_feat, n_feat) self.linear_k = nn.Linear(n_feat, n_feat) self.linear_v = nn.Linear(n_feat, n_feat) self.linear_out = nn.Linear(n_feat, n_feat) self.dropout = nn.Dropout(p=dropout_rate) def forward(self, input_0, input_1, input_2): primals_2 = self.linear_q.weight primals_3 = self.linear_q.bias primals_4 = self.linear_k.weight primals_5 = self.linear_k.bias primals_7 = self.linear_v.weight primals_8 = self.linear_v.bias primals_10 = self.linear_out.weight primals_11 = self.linear_out.bias primals_1 = input_0 primals_6 = input_1 primals_9 = 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]
karan-deepsync/FastSpeech2
MultiHeadedAttention
false
15,806
[ "Apache-2.0" ]
148
84ad261db4a865536b2e15dfb8346644c3192704
https://github.com/karan-deepsync/FastSpeech2/tree/84ad261db4a865536b2e15dfb8346644c3192704
Attention
import torch import torch.nn as nn import torch.utils import torch.nn.functional as F import torch.nn.parallel class Attention(nn.Module): def __init__(self, input_dim, source_dim=None, output_dim=None, bias=False ): super(Attention, self).__init__() if source_dim is None: source_dim = input_dim if output_dim is None: output_dim = input_dim self.input_dim = input_dim self.source_dim = source_dim self.output_dim = output_dim self.input_proj = nn.Linear(input_dim, source_dim, bias=bias) self.output_proj = nn.Linear(input_dim + source_dim, output_dim, bias=bias) self.mask = None def set_mask(self, mask): self.mask = mask def forward(self, input, source_hids): batch_size = input.size(0) source_len = source_hids.size(1) x = self.input_proj(input) attn = torch.bmm(x, source_hids.transpose(1, 2)) if self.mask is not None: attn.data.masked_fill_(self.mask, -float('inf')) attn = F.softmax(attn.view(-1, source_len), dim=1).view(batch_size, -1, source_len) mix = torch.bmm(attn, source_hids) combined = torch.cat((mix, input), dim=2) output = torch.tanh(self.output_proj(combined.view(-1, self. input_dim + self.source_dim))).view(batch_size, -1, self.output_dim ) return output, attn def get_inputs(): return [torch.rand([4, 4, 4]), 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 from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn import torch.utils 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__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) @triton.jit def triton_poi_fused_cat_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 8 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 = tmp0 >= tmp3 tl.full([1], 8, tl.int64) tmp9 = tl.load(in_ptr1 + (4 * x1 + (-4 + x0)), tmp6 & xmask, eviction_policy='evict_last', other=0.0) tmp10 = tl.where(tmp4, tmp5, tmp9) tl.store(out_ptr0 + x2, tmp10, xmask) @triton.jit def triton_poi_fused_tanh_tanh_backward_3(in_out_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_out_ptr0 + x0, xmask) tmp1 = libdevice.tanh(tmp0) tmp2 = tmp1 * tmp1 tmp3 = 1.0 tmp4 = tmp3 - tmp2 tl.store(in_out_ptr0 + x0, tmp1, xmask) tl.store(out_ptr0 + x0, tmp4, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4, 8), (8, 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_3, (4, 4), (1, 4), 0), out=buf0) del primals_3 buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 0), reinterpret_tensor(primals_2, (4, 4, 4), (16, 1, 4), 0), out=buf1) buf2 = buf0 del buf0 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, (16, 4), (4, 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 extern_kernels.bmm(reinterpret_tensor(buf3, (4, 4, 4), (16, 4, 1), 0), primals_2, out=buf4) buf5 = empty_strided_cuda((4, 4, 8), (32, 8, 1), torch.float32) triton_poi_fused_cat_2[grid(128)](buf4, primals_1, buf5, 128, XBLOCK=128, num_warps=4, num_stages=1) buf6 = reinterpret_tensor(buf4, (16, 4), (4, 1), 0) del buf4 extern_kernels.mm(reinterpret_tensor(buf5, (16, 8), (8, 1), 0), reinterpret_tensor(primals_4, (8, 4), (1, 8), 0), out=buf6) buf7 = buf6 del buf6 buf8 = empty_strided_cuda((16, 4), (4, 1), torch.float32) triton_poi_fused_tanh_tanh_backward_3[grid(64)](buf7, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1) return reinterpret_tensor(buf7, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(buf3, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(primals_1, (16, 4), (4, 1), 0 ), reinterpret_tensor(primals_2, (4, 4, 4), (16, 1, 4), 0 ), buf3, reinterpret_tensor(buf5, (16, 8), (8, 1), 0), buf8, primals_4 class AttentionNew(nn.Module): def __init__(self, input_dim, source_dim=None, output_dim=None, bias=False ): super(AttentionNew, self).__init__() if source_dim is None: source_dim = input_dim if output_dim is None: output_dim = input_dim self.input_dim = input_dim self.source_dim = source_dim self.output_dim = output_dim self.input_proj = nn.Linear(input_dim, source_dim, bias=bias) self.output_proj = nn.Linear(input_dim + source_dim, output_dim, bias=bias) self.mask = None def set_mask(self, mask): self.mask = mask def forward(self, input_0, input_1): primals_3 = self.input_proj.weight primals_4 = self.output_proj.weight primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0], output[1]
kcyu2014/eval-nas
Attention
false
15,807
[ "MIT" ]
47
385376a3ef96336b54ee7e696af1d02b97aa5c32
https://github.com/kcyu2014/eval-nas/tree/385376a3ef96336b54ee7e696af1d02b97aa5c32
ConstractiveThresholdHingeLoss
import torch import torch.nn as nn import torch.nn.functional as F class ConstractiveThresholdHingeLoss(nn.Module): def __init__(self, hingethresh=0.0, margin=2.0): super(ConstractiveThresholdHingeLoss, self).__init__() self.threshold = hingethresh self.margin = margin def forward(self, out_vec_t0, out_vec_t1, label): distance = F.pairwise_distance(out_vec_t0, out_vec_t1, p=2) similar_pair = torch.clamp(distance - self.threshold, min=0.0) dissimilar_pair = torch.clamp(self.margin - distance, min=0.0) constractive_thresh_loss = torch.sum((1 - label) * torch.pow( similar_pair, 2) + label * torch.pow(dissimilar_pair, 2)) return constractive_thresh_loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand( [4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn 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_norm_sub_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 tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp13 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp18 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp19 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 - tmp1 tmp3 = 1e-06 tmp4 = tmp2 + tmp3 tmp5 = tmp4 * tmp4 tmp8 = tmp6 - tmp7 tmp9 = tmp8 + tmp3 tmp10 = tmp9 * tmp9 tmp11 = tmp5 + tmp10 tmp14 = tmp12 - tmp13 tmp15 = tmp14 + tmp3 tmp16 = tmp15 * tmp15 tmp17 = tmp11 + tmp16 tmp20 = tmp18 - tmp19 tmp21 = tmp20 + tmp3 tmp22 = tmp21 * tmp21 tmp23 = tmp17 + tmp22 tmp24 = libdevice.sqrt(tmp23) tl.store(out_ptr0 + x0, tmp24, xmask) @triton.jit def triton_per_fused_add_clamp_mul_pow_rsub_sub_sum_1(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r2 = rindex r0 = rindex % 64 tmp0 = tl.load(in_ptr0 + r2, None) tmp3 = tl.load(in_ptr1 + r0, None, eviction_policy='evict_last') tmp1 = 1.0 tmp2 = tmp1 - tmp0 tmp4 = 0.0 tmp5 = tmp3 - tmp4 tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp6 * tmp6 tmp8 = tmp2 * tmp7 tmp9 = 2.0 tmp10 = tmp9 - tmp3 tmp11 = triton_helpers.maximum(tmp10, tmp4) tmp12 = tmp11 * tmp11 tmp13 = tmp0 * tmp12 tmp14 = tmp8 + tmp13 tmp15 = tl.broadcast_to(tmp14, [RBLOCK]) tmp17 = triton_helpers.promote_to_tensor(tl.sum(tmp15, 0)) tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp17, 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_add_norm_sub_0[grid(64)](arg1_1, arg0_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 del arg1_1 buf1 = empty_strided_cuda((), (), torch.float32) triton_per_fused_add_clamp_mul_pow_rsub_sub_sum_1[grid(1)](arg2_1, buf0, buf1, 1, 256, num_warps=2, num_stages=1) del arg2_1 del buf0 return buf1, class ConstractiveThresholdHingeLossNew(nn.Module): def __init__(self, hingethresh=0.0, margin=2.0): super(ConstractiveThresholdHingeLossNew, self).__init__() self.threshold = hingethresh 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]
kensakurada/SceneChangeDet
ConstractiveThresholdHingeLoss
false
15,808
[ "MIT" ]
199
0530e0162863fec0c5296188526f0d27e0109814
https://github.com/kensakurada/SceneChangeDet/tree/0530e0162863fec0c5296188526f0d27e0109814
AdjDecoder
import torch from torch import nn import torch.utils.data class AdjDecoder(nn.Module): u""" Decode an input (parent) feature into a left-child and a right-child feature """ def __init__(self, feature_size, hidden_size): super(AdjDecoder, self).__init__() self.mlp = nn.Linear(feature_size, hidden_size) self.mlp_left = nn.Linear(hidden_size, feature_size) self.mlp_right = nn.Linear(hidden_size, feature_size) self.tanh = nn.Tanh() def forward(self, parent_feature): vector = self.mlp(parent_feature) vector = self.tanh(vector) left_feature = self.mlp_left(vector) left_feature = self.tanh(left_feature) right_feature = self.mlp_right(vector) right_feature = self.tanh(right_feature) return left_feature, right_feature def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'feature_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 from torch import nn import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda 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, (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_tanh_0[grid(256)](buf1, primals_2, 256, XBLOCK=256, 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=256, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), out=buf4) buf5 = reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf4 triton_poi_fused_tanh_0[grid(256)](buf5, primals_7, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 return buf3, buf5, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf1, buf3, buf5, primals_6, primals_4 class AdjDecoderNew(nn.Module): u""" Decode an input (parent) feature into a left-child and a right-child feature """ def __init__(self, feature_size, hidden_size): super(AdjDecoderNew, self).__init__() self.mlp = nn.Linear(feature_size, hidden_size) self.mlp_left = nn.Linear(hidden_size, feature_size) self.mlp_right = nn.Linear(hidden_size, feature_size) self.tanh = nn.Tanh() def forward(self, input_0): primals_1 = self.mlp.weight primals_2 = self.mlp.bias primals_4 = self.mlp_left.weight primals_5 = self.mlp_left.bias primals_6 = self.mlp_right.weight primals_7 = self.mlp_right.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0], output[1]
kevin-kaixu/grass_pytorch
AdjDecoder
false
15,809
[ "Apache-2.0" ]
85
1d8dc6dcc0ab3ca029e449f57c37ba3910a4f90a
https://github.com/kevin-kaixu/grass_pytorch/tree/1d8dc6dcc0ab3ca029e449f57c37ba3910a4f90a
NodeClassifier
import torch from torch import nn import torch.utils.data class NodeClassifier(nn.Module): def __init__(self, feature_size, hidden_size): super(NodeClassifier, self).__init__() self.mlp1 = nn.Linear(feature_size, hidden_size) self.tanh = nn.Tanh() self.mlp2 = nn.Linear(hidden_size, 3) def forward(self, input_feature): output = self.mlp1(input_feature) output = self.tanh(output) output = self.mlp2(output) return output def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'feature_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 from torch import nn import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_tanh_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = libdevice.tanh(tmp2) tl.store(in_out_ptr0 + x2, tmp3, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 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, (3, 4), (4, 1)) assert_size_stride(primals_5, (3,), (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=256, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 3), (3, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_4, (4, 3), (1, 4), 0), alpha=1, beta=1, out=buf2) del primals_5 return reinterpret_tensor(buf2, (4, 4, 4, 3), (48, 12, 3, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), buf1, primals_4 class NodeClassifierNew(nn.Module): def __init__(self, feature_size, hidden_size): super(NodeClassifierNew, self).__init__() self.mlp1 = nn.Linear(feature_size, hidden_size) self.tanh = nn.Tanh() self.mlp2 = nn.Linear(hidden_size, 3) def forward(self, input_0): primals_1 = self.mlp1.weight primals_2 = self.mlp1.bias primals_4 = self.mlp2.weight primals_5 = self.mlp2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
kevin-kaixu/grass_pytorch
NodeClassifier
false
15,810
[ "Apache-2.0" ]
85
1d8dc6dcc0ab3ca029e449f57c37ba3910a4f90a
https://github.com/kevin-kaixu/grass_pytorch/tree/1d8dc6dcc0ab3ca029e449f57c37ba3910a4f90a
CategoricalKLLoss
import torch import torch.nn as nn class CategoricalKLLoss(nn.Module): def __init__(self): super(CategoricalKLLoss, self).__init__() def forward(self, P, Q): log_P = P.log() log_Q = Q.log() kl = (P * (log_P - log_Q)).sum(dim=-1).sum(dim=-1) return kl.mean(dim=0) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_log_mul_sub_sum_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 16 * x0, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr1 + 16 * x0, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (1 + 16 * x0), xmask, eviction_policy='evict_last' ) tmp8 = tl.load(in_ptr1 + (1 + 16 * x0), xmask, eviction_policy='evict_last' ) tmp13 = tl.load(in_ptr0 + (2 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp15 = tl.load(in_ptr1 + (2 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp20 = tl.load(in_ptr0 + (3 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp22 = tl.load(in_ptr1 + (3 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp27 = tl.load(in_ptr0 + (4 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp29 = tl.load(in_ptr1 + (4 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp33 = tl.load(in_ptr0 + (5 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp35 = tl.load(in_ptr1 + (5 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp40 = tl.load(in_ptr0 + (6 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp42 = tl.load(in_ptr1 + (6 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp47 = tl.load(in_ptr0 + (7 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp49 = tl.load(in_ptr1 + (7 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp55 = tl.load(in_ptr0 + (8 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp57 = tl.load(in_ptr1 + (8 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp61 = tl.load(in_ptr0 + (9 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp63 = tl.load(in_ptr1 + (9 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp68 = tl.load(in_ptr0 + (10 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp70 = tl.load(in_ptr1 + (10 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp75 = tl.load(in_ptr0 + (11 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp77 = tl.load(in_ptr1 + (11 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp83 = tl.load(in_ptr0 + (12 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp85 = tl.load(in_ptr1 + (12 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp89 = tl.load(in_ptr0 + (13 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp91 = tl.load(in_ptr1 + (13 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp96 = tl.load(in_ptr0 + (14 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp98 = tl.load(in_ptr1 + (14 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp103 = tl.load(in_ptr0 + (15 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp105 = tl.load(in_ptr1 + (15 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp1 = tl_math.log(tmp0) tmp3 = tl_math.log(tmp2) tmp4 = tmp1 - tmp3 tmp5 = tmp0 * tmp4 tmp7 = tl_math.log(tmp6) tmp9 = tl_math.log(tmp8) tmp10 = tmp7 - tmp9 tmp11 = tmp6 * tmp10 tmp12 = tmp5 + tmp11 tmp14 = tl_math.log(tmp13) tmp16 = tl_math.log(tmp15) tmp17 = tmp14 - tmp16 tmp18 = tmp13 * tmp17 tmp19 = tmp12 + tmp18 tmp21 = tl_math.log(tmp20) tmp23 = tl_math.log(tmp22) tmp24 = tmp21 - tmp23 tmp25 = tmp20 * tmp24 tmp26 = tmp19 + tmp25 tmp28 = tl_math.log(tmp27) tmp30 = tl_math.log(tmp29) tmp31 = tmp28 - tmp30 tmp32 = tmp27 * tmp31 tmp34 = tl_math.log(tmp33) tmp36 = tl_math.log(tmp35) tmp37 = tmp34 - tmp36 tmp38 = tmp33 * tmp37 tmp39 = tmp32 + tmp38 tmp41 = tl_math.log(tmp40) tmp43 = tl_math.log(tmp42) tmp44 = tmp41 - tmp43 tmp45 = tmp40 * tmp44 tmp46 = tmp39 + tmp45 tmp48 = tl_math.log(tmp47) tmp50 = tl_math.log(tmp49) tmp51 = tmp48 - tmp50 tmp52 = tmp47 * tmp51 tmp53 = tmp46 + tmp52 tmp54 = tmp26 + tmp53 tmp56 = tl_math.log(tmp55) tmp58 = tl_math.log(tmp57) tmp59 = tmp56 - tmp58 tmp60 = tmp55 * tmp59 tmp62 = tl_math.log(tmp61) tmp64 = tl_math.log(tmp63) tmp65 = tmp62 - tmp64 tmp66 = tmp61 * tmp65 tmp67 = tmp60 + tmp66 tmp69 = tl_math.log(tmp68) tmp71 = tl_math.log(tmp70) tmp72 = tmp69 - tmp71 tmp73 = tmp68 * tmp72 tmp74 = tmp67 + tmp73 tmp76 = tl_math.log(tmp75) tmp78 = tl_math.log(tmp77) tmp79 = tmp76 - tmp78 tmp80 = tmp75 * tmp79 tmp81 = tmp74 + tmp80 tmp82 = tmp54 + tmp81 tmp84 = tl_math.log(tmp83) tmp86 = tl_math.log(tmp85) tmp87 = tmp84 - tmp86 tmp88 = tmp83 * tmp87 tmp90 = tl_math.log(tmp89) tmp92 = tl_math.log(tmp91) tmp93 = tmp90 - tmp92 tmp94 = tmp89 * tmp93 tmp95 = tmp88 + tmp94 tmp97 = tl_math.log(tmp96) tmp99 = tl_math.log(tmp98) tmp100 = tmp97 - tmp99 tmp101 = tmp96 * tmp100 tmp102 = tmp95 + tmp101 tmp104 = tl_math.log(tmp103) tmp106 = tl_math.log(tmp105) tmp107 = tmp104 - tmp106 tmp108 = tmp103 * tmp107 tmp109 = tmp102 + tmp108 tmp110 = tmp82 + tmp109 tl.store(out_ptr0 + x0, tmp110, xmask) @triton.jit def triton_poi_fused_mean_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + (4 + x0), xmask) tmp3 = tl.load(in_ptr0 + (8 + x0), xmask) tmp5 = tl.load(in_ptr0 + (12 + x0), xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tl.store(out_ptr0 + x0, tmp8, 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, 1), torch.float32) get_raw_stream(0) triton_poi_fused_log_mul_sub_sum_0[grid(16)](arg0_1, arg1_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg0_1 del arg1_1 buf1 = empty_strided_cuda((4,), (1,), torch.float32) triton_poi_fused_mean_1[grid(4)](buf0, buf1, 4, XBLOCK=4, num_warps =1, num_stages=1) del buf0 return buf1, class CategoricalKLLossNew(nn.Module): def __init__(self): super(CategoricalKLLossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kekayan/Info-HCVAE
CategoricalKLLoss
false
15,811
[ "Apache-2.0" ]
120
1f4d536523767f439e689d8963c54a55fb75c6f9
https://github.com/kekayan/Info-HCVAE/tree/1f4d536523767f439e689d8963c54a55fb75c6f9
Net
import torch import torch.nn as nn import torch.nn.functional as F class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1_1 = nn.Conv2d(1, 32, kernel_size=5, padding=2) self.prelu1_1 = nn.PReLU() self.conv1_2 = nn.Conv2d(32, 32, kernel_size=5, padding=2) self.prelu1_2 = nn.PReLU() self.conv2_1 = nn.Conv2d(32, 64, kernel_size=5, padding=2) self.prelu2_1 = nn.PReLU() self.conv2_2 = nn.Conv2d(64, 64, kernel_size=5, padding=2) self.prelu2_2 = nn.PReLU() self.conv3_1 = nn.Conv2d(64, 128, kernel_size=5, padding=2) self.prelu3_1 = nn.PReLU() self.conv3_2 = nn.Conv2d(128, 128, kernel_size=5, padding=2) self.prelu3_2 = nn.PReLU() self.preluip1 = nn.PReLU() self.ip1 = nn.Linear(128 * 3 * 3, 2) self.ip2 = nn.Linear(2, 10, bias=False) def forward(self, x): x = self.prelu1_1(self.conv1_1(x)) x = self.prelu1_2(self.conv1_2(x)) x = F.max_pool2d(x, 2) x = self.prelu2_1(self.conv2_1(x)) x = self.prelu2_2(self.conv2_2(x)) x = F.max_pool2d(x, 2) x = self.prelu3_1(self.conv3_1(x)) x = self.prelu3_2(self.conv3_2(x)) x = F.max_pool2d(x, 2) x = x.view(-1, 128 * 3 * 3) ip1 = self.preluip1(self.ip1(x)) ip2 = self.ip2(ip1) return ip1, F.log_softmax(ip2, dim=1) def get_inputs(): return [torch.rand([4, 1, 24, 24])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers 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_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 25 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 32 y1 = yindex // 32 tmp0 = tl.load(in_ptr0 + (x2 + 25 * y3), xmask, eviction_policy= 'evict_last') tl.store(out_ptr0 + (y0 + 32 * x2 + 800 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 25 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 32 y1 = yindex // 32 tmp0 = tl.load(in_ptr0 + (x2 + 25 * y3), xmask, eviction_policy= 'evict_last') tl.store(out_ptr0 + (y0 + 32 * x2 + 800 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_2(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 25 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 64 y1 = yindex // 64 tmp0 = tl.load(in_ptr0 + (x2 + 25 * y3), xmask, eviction_policy= 'evict_last') tl.store(out_ptr0 + (y0 + 64 * x2 + 1600 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 25 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 64 y1 = yindex // 64 tmp0 = tl.load(in_ptr0 + (x2 + 25 * y3), xmask, eviction_policy= 'evict_last') tl.store(out_ptr0 + (y0 + 64 * x2 + 1600 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_4(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 25 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 128 y1 = yindex // 128 tmp0 = tl.load(in_ptr0 + (x2 + 25 * y3), xmask, eviction_policy= 'evict_last') tl.store(out_ptr0 + (y0 + 128 * x2 + 3200 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_convolution_5(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 128 xnumel = 576 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 32 y1 = yindex // 32 tmp0 = tl.load(in_ptr0 + (x2 + 576 * y3), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + (y0 + 32 * x2 + 18432 * y1), tmp2, xmask & ymask) @triton.jit def triton_poi_fused__prelu_kernel_6(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) x0 = xindex tmp0 = tl.load(in_ptr0 + x0, None) tmp3 = tl.load(in_ptr1 + 0) tmp4 = tl.broadcast_to(tmp3, [XBLOCK]) tmp1 = 0.0 tmp2 = tmp0 > tmp1 tmp5 = tmp4 * tmp0 tmp6 = tl.where(tmp2, tmp0, tmp5) tl.store(out_ptr0 + x0, tmp6, None) @triton.jit def triton_poi_fused__prelu_kernel_convolution_7(in_out_ptr0, 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) x2 = xindex x0 = xindex % 32 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + 0) tmp6 = tl.broadcast_to(tmp5, [XBLOCK]) tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp7 = tmp6 * tmp2 tmp8 = tl.where(tmp4, tmp2, tmp7) tl.store(in_out_ptr0 + x2, tmp2, None) tl.store(out_ptr0 + x2, tmp8, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_8(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x0 = xindex % 32 x1 = xindex // 32 % 12 x2 = xindex // 384 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1 + 1536 * x2), None) tmp1 = tl.load(in_ptr0 + (32 + x0 + 64 * x1 + 1536 * x2), None) tmp3 = tl.load(in_ptr0 + (768 + x0 + 64 * x1 + 1536 * x2), None) tmp5 = tl.load(in_ptr0 + (800 + x0 + 64 * x1 + 1536 * x2), None) tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + x3, tmp6, None) tl.store(out_ptr1 + x3, tmp16, None) @triton.jit def triton_poi_fused__prelu_kernel_convolution_9(in_out_ptr0, 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) x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + 0) tmp6 = tl.broadcast_to(tmp5, [XBLOCK]) tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp7 = tmp6 * tmp2 tmp8 = tl.where(tmp4, tmp2, tmp7) tl.store(in_out_ptr0 + x2, tmp2, None) tl.store(out_ptr0 + x2, tmp8, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_10(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 % 64 x1 = xindex // 64 % 6 x2 = xindex // 384 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 128 * x1 + 1536 * x2), xmask) tmp1 = tl.load(in_ptr0 + (64 + x0 + 128 * x1 + 1536 * x2), xmask) tmp3 = tl.load(in_ptr0 + (768 + x0 + 128 * x1 + 1536 * x2), xmask) tmp5 = tl.load(in_ptr0 + (832 + x0 + 128 * x1 + 1536 * x2), xmask) tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + x3, tmp6, xmask) tl.store(out_ptr1 + x3, tmp16, xmask) @triton.jit def triton_poi_fused__prelu_kernel_convolution_11(in_out_ptr0, 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) x2 = xindex x0 = xindex % 128 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + 0) tmp6 = tl.broadcast_to(tmp5, [XBLOCK]) tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp7 = tmp6 * tmp2 tmp8 = tl.where(tmp4, tmp2, tmp7) tl.store(in_out_ptr0 + x2, tmp2, None) tl.store(out_ptr0 + x2, tmp8, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_12(in_ptr0, out_ptr0, out_ptr1, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 36 xnumel = 128 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 3 y1 = yindex // 3 y5 = yindex y4 = yindex // 9 y6 = yindex % 9 tmp0 = tl.load(in_ptr0 + (x2 + 256 * y0 + 1536 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (128 + x2 + 256 * y0 + 1536 * y1), xmask & ymask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (768 + x2 + 256 * y0 + 1536 * y1), xmask & ymask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (896 + x2 + 256 * y0 + 1536 * y1), xmask & ymask, eviction_policy='evict_last') tmp2 = tmp1 > tmp0 tmp3 = tl.full([1, 1], 1, tl.int8) tmp4 = tl.full([1, 1], 0, tl.int8) tmp5 = tl.where(tmp2, tmp3, tmp4) tmp6 = triton_helpers.maximum(tmp1, tmp0) tmp8 = tmp7 > tmp6 tmp9 = tl.full([1, 1], 2, tl.int8) tmp10 = tl.where(tmp8, tmp9, tmp5) tmp11 = triton_helpers.maximum(tmp7, tmp6) tmp13 = tmp12 > tmp11 tmp14 = tl.full([1, 1], 3, tl.int8) tmp15 = tl.where(tmp13, tmp14, tmp10) tmp16 = triton_helpers.maximum(tmp12, tmp11) tl.store(out_ptr0 + (x2 + 128 * y5), tmp15, xmask & ymask) tl.store(out_ptr1 + (y6 + 9 * x2 + 1152 * y4), tmp16, xmask & ymask) @triton.jit def triton_poi_fused__prelu_kernel_13(in_ptr0, in_ptr1, 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 = tl.load(in_ptr0 + x0, xmask) tmp3 = tl.load(in_ptr1 + 0) tmp4 = tl.broadcast_to(tmp3, [XBLOCK]) tmp1 = 0.0 tmp2 = tmp0 > tmp1 tmp5 = tmp4 * tmp0 tmp6 = tl.where(tmp2, tmp0, tmp5) tl.store(out_ptr0 + x0, tmp6, xmask) @triton.jit def triton_per_fused__log_softmax_14(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, 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 ) = args args.clear() assert_size_stride(primals_1, (32, 1, 5, 5), (25, 25, 5, 1)) assert_size_stride(primals_2, (32,), (1,)) assert_size_stride(primals_3, (4, 1, 24, 24), (576, 576, 24, 1)) assert_size_stride(primals_4, (1,), (1,)) assert_size_stride(primals_5, (32, 32, 5, 5), (800, 25, 5, 1)) assert_size_stride(primals_6, (32,), (1,)) assert_size_stride(primals_7, (1,), (1,)) assert_size_stride(primals_8, (64, 32, 5, 5), (800, 25, 5, 1)) assert_size_stride(primals_9, (64,), (1,)) assert_size_stride(primals_10, (1,), (1,)) assert_size_stride(primals_11, (64, 64, 5, 5), (1600, 25, 5, 1)) assert_size_stride(primals_12, (64,), (1,)) assert_size_stride(primals_13, (1,), (1,)) assert_size_stride(primals_14, (128, 64, 5, 5), (1600, 25, 5, 1)) assert_size_stride(primals_15, (128,), (1,)) assert_size_stride(primals_16, (1,), (1,)) assert_size_stride(primals_17, (128, 128, 5, 5), (3200, 25, 5, 1)) assert_size_stride(primals_18, (128,), (1,)) assert_size_stride(primals_19, (1,), (1,)) assert_size_stride(primals_20, (2, 1152), (1152, 1)) assert_size_stride(primals_21, (2,), (1,)) assert_size_stride(primals_22, (1,), (1,)) assert_size_stride(primals_23, (10, 2), (2, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((32, 32, 5, 5), (800, 1, 160, 32), torch. float32) get_raw_stream(0) triton_poi_fused_0[grid(1024, 25)](primals_5, buf0, 1024, 25, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1) del primals_5 buf1 = empty_strided_cuda((64, 32, 5, 5), (800, 1, 160, 32), torch. float32) triton_poi_fused_1[grid(2048, 25)](primals_8, buf1, 2048, 25, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1) del primals_8 buf2 = empty_strided_cuda((64, 64, 5, 5), (1600, 1, 320, 64), torch .float32) triton_poi_fused_2[grid(4096, 25)](primals_11, buf2, 4096, 25, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1) del primals_11 buf3 = empty_strided_cuda((128, 64, 5, 5), (1600, 1, 320, 64), torch.float32) triton_poi_fused_3[grid(8192, 25)](primals_14, buf3, 8192, 25, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1) del primals_14 buf4 = empty_strided_cuda((128, 128, 5, 5), (3200, 1, 640, 128), torch.float32) triton_poi_fused_4[grid(16384, 25)](primals_17, buf4, 16384, 25, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1) del primals_17 buf5 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(2, 2), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf5, (4, 32, 24, 24), (18432, 576, 24, 1)) buf6 = empty_strided_cuda((4, 32, 24, 24), (18432, 1, 768, 32), torch.float32) triton_poi_fused_convolution_5[grid(128, 576)](buf5, primals_2, buf6, 128, 576, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1) del primals_2 buf7 = reinterpret_tensor(buf5, (4, 32, 24, 24), (18432, 1, 768, 32), 0 ) del buf5 triton_poi_fused__prelu_kernel_6[grid(73728)](buf6, primals_4, buf7, 73728, XBLOCK=512, num_warps=8, num_stages=1) buf8 = extern_kernels.convolution(buf7, buf0, stride=(1, 1), padding=(2, 2), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 32, 24, 24), (18432, 1, 768, 32)) buf9 = buf8 del buf8 buf10 = empty_strided_cuda((4, 32, 24, 24), (18432, 1, 768, 32), torch.float32) triton_poi_fused__prelu_kernel_convolution_7[grid(73728)](buf9, primals_6, primals_7, buf10, 73728, XBLOCK=1024, num_warps=4, num_stages=1) del primals_6 buf11 = empty_strided_cuda((4, 32, 12, 12), (4608, 1, 384, 32), torch.float32) buf12 = empty_strided_cuda((4, 32, 12, 12), (4608, 1, 384, 32), torch.int8) triton_poi_fused_max_pool2d_with_indices_8[grid(18432)](buf10, buf11, buf12, 18432, XBLOCK=256, num_warps=4, num_stages=1) buf13 = extern_kernels.convolution(buf11, buf1, stride=(1, 1), padding=(2, 2), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf13, (4, 64, 12, 12), (9216, 1, 768, 64)) buf14 = buf13 del buf13 buf15 = empty_strided_cuda((4, 64, 12, 12), (9216, 1, 768, 64), torch.float32) triton_poi_fused__prelu_kernel_convolution_9[grid(36864)](buf14, primals_9, primals_10, buf15, 36864, XBLOCK=512, num_warps=4, num_stages=1) del primals_9 buf16 = extern_kernels.convolution(buf15, buf2, stride=(1, 1), padding=(2, 2), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf16, (4, 64, 12, 12), (9216, 1, 768, 64)) buf17 = buf16 del buf16 buf18 = empty_strided_cuda((4, 64, 12, 12), (9216, 1, 768, 64), torch.float32) triton_poi_fused__prelu_kernel_convolution_9[grid(36864)](buf17, primals_12, primals_13, buf18, 36864, XBLOCK=512, num_warps=4, num_stages=1) del primals_12 buf19 = empty_strided_cuda((4, 64, 6, 6), (2304, 1, 384, 64), torch .float32) buf20 = empty_strided_cuda((4, 64, 6, 6), (2304, 1, 384, 64), torch .int8) triton_poi_fused_max_pool2d_with_indices_10[grid(9216)](buf18, buf19, buf20, 9216, XBLOCK=256, num_warps=4, num_stages=1) buf21 = extern_kernels.convolution(buf19, buf3, stride=(1, 1), padding=(2, 2), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf21, (4, 128, 6, 6), (4608, 1, 768, 128)) buf22 = buf21 del buf21 buf23 = empty_strided_cuda((4, 128, 6, 6), (4608, 1, 768, 128), torch.float32) triton_poi_fused__prelu_kernel_convolution_11[grid(18432)](buf22, primals_15, primals_16, buf23, 18432, XBLOCK=128, num_warps=4, num_stages=1) del primals_15 buf24 = extern_kernels.convolution(buf23, buf4, stride=(1, 1), padding=(2, 2), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf24, (4, 128, 6, 6), (4608, 1, 768, 128)) buf25 = buf24 del buf24 buf26 = empty_strided_cuda((4, 128, 6, 6), (4608, 1, 768, 128), torch.float32) triton_poi_fused__prelu_kernel_convolution_11[grid(18432)](buf25, primals_18, primals_19, buf26, 18432, XBLOCK=128, num_warps=4, num_stages=1) del primals_18 buf27 = empty_strided_cuda((4, 128, 3, 3), (1152, 1, 384, 128), torch.int8) buf28 = empty_strided_cuda((4, 128, 3, 3), (1152, 9, 3, 1), torch. float32) triton_poi_fused_max_pool2d_with_indices_12[grid(36, 128)](buf26, buf27, buf28, 36, 128, XBLOCK=4, YBLOCK=64, num_warps=4, num_stages=1) buf29 = empty_strided_cuda((4, 2), (2, 1), torch.float32) extern_kernels.addmm(primals_21, reinterpret_tensor(buf28, (4, 1152 ), (1152, 1), 0), reinterpret_tensor(primals_20, (1152, 2), (1, 1152), 0), alpha=1, beta=1, out=buf29) del primals_21 buf30 = empty_strided_cuda((4, 2), (2, 1), torch.float32) triton_poi_fused__prelu_kernel_13[grid(8)](buf29, primals_22, buf30, 8, XBLOCK=8, num_warps=1, num_stages=1) buf31 = empty_strided_cuda((4, 10), (10, 1), torch.float32) extern_kernels.mm(buf30, reinterpret_tensor(primals_23, (2, 10), (1, 2), 0), out=buf31) buf34 = empty_strided_cuda((4, 10), (10, 1), torch.float32) triton_per_fused__log_softmax_14[grid(4)](buf31, buf34, 4, 10, XBLOCK=1, num_warps=2, num_stages=1) del buf31 return (buf30, buf34, primals_1, primals_3, primals_4, buf0, primals_7, buf1, primals_10, buf2, primals_13, buf3, primals_16, buf4, primals_19, primals_22, buf6, buf7, buf9, buf10, buf11, buf12, buf14, buf15, buf17, buf18, buf19, buf20, buf22, buf23, buf25, buf26, buf27, reinterpret_tensor(buf28, (4, 1152), (1152, 1), 0), buf29, buf30, buf34, primals_23, primals_20) class NetNew(nn.Module): def __init__(self): super(NetNew, self).__init__() self.conv1_1 = nn.Conv2d(1, 32, kernel_size=5, padding=2) self.prelu1_1 = nn.PReLU() self.conv1_2 = nn.Conv2d(32, 32, kernel_size=5, padding=2) self.prelu1_2 = nn.PReLU() self.conv2_1 = nn.Conv2d(32, 64, kernel_size=5, padding=2) self.prelu2_1 = nn.PReLU() self.conv2_2 = nn.Conv2d(64, 64, kernel_size=5, padding=2) self.prelu2_2 = nn.PReLU() self.conv3_1 = nn.Conv2d(64, 128, kernel_size=5, padding=2) self.prelu3_1 = nn.PReLU() self.conv3_2 = nn.Conv2d(128, 128, kernel_size=5, padding=2) self.prelu3_2 = nn.PReLU() self.preluip1 = nn.PReLU() self.ip1 = nn.Linear(128 * 3 * 3, 2) self.ip2 = nn.Linear(2, 10, bias=False) def forward(self, input_0): primals_1 = self.conv1_1.weight primals_2 = self.conv1_1.bias primals_4 = self.prelu1_1.weight primals_5 = self.conv1_2.weight primals_6 = self.conv1_2.bias primals_7 = self.prelu1_2.weight primals_8 = self.conv2_1.weight primals_9 = self.conv2_1.bias primals_10 = self.prelu2_1.weight primals_11 = self.conv2_2.weight primals_12 = self.conv2_2.bias primals_13 = self.prelu2_2.weight primals_14 = self.conv3_1.weight primals_15 = self.conv3_1.bias primals_16 = self.prelu3_1.weight primals_17 = self.conv3_2.weight primals_18 = self.conv3_2.bias primals_19 = self.prelu3_2.weight primals_22 = self.preluip1.weight primals_20 = self.ip1.weight primals_21 = self.ip1.bias primals_23 = self.ip2.weight 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]) return output[0], output[1]
jxgu1016/MNIST_with_centerloss.pytorch
Net
false
15,812
[ "MIT" ]
346
4e94cc77fe94056a7f1f081fcaf0325781ba0224
https://github.com/jxgu1016/MNIST_with_centerloss.pytorch/tree/4e94cc77fe94056a7f1f081fcaf0325781ba0224
dilated_1D
import torch import torch.utils.data import torch.nn as nn class dilated_1D(nn.Module): def __init__(self, cin, cout, dilation_factor=2): super(dilated_1D, self).__init__() self.tconv = nn.ModuleList() self.kernel_set = [2, 3, 6, 7] self.tconv = nn.Conv2d(cin, cout, (1, 7), dilation=(1, dilation_factor) ) def forward(self, input): x = self.tconv(input) return x def get_inputs(): return [torch.rand([4, 4, 64, 64])] def get_init_inputs(): return [[], {'cin': 4, 'cout': 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.utils.data import torch.nn as 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): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 3328 % 4 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, None) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 1, 7), (28, 7, 7, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 64, 64), (16384, 4096, 64, 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, 2), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 64, 52), (13312, 3328, 52, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(53248)](buf1, primals_2, 53248, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 return buf1, primals_1, primals_3 class dilated_1DNew(nn.Module): def __init__(self, cin, cout, dilation_factor=2): super(dilated_1DNew, self).__init__() self.tconv = nn.ModuleList() self.kernel_set = [2, 3, 6, 7] self.tconv = nn.Conv2d(cin, cout, (1, 7), dilation=(1, dilation_factor) ) def forward(self, input_0): primals_1 = self.tconv.weight primals_2 = self.tconv.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
kevin-xuan/Traffic-Benchmark
dilated_1D
false
15,813
[ "MIT" ]
120
b9f8e40b4df9b58f5ad88432dc070cbbbcdc0228
https://github.com/kevin-xuan/Traffic-Benchmark/tree/b9f8e40b4df9b58f5ad88432dc070cbbbcdc0228
SymEncoder
import torch from torch import nn import torch.utils.data class SymEncoder(nn.Module): def __init__(self, feature_size, symmetry_size, hidden_size): super(SymEncoder, self).__init__() self.left = nn.Linear(feature_size, hidden_size) self.right = nn.Linear(symmetry_size, hidden_size) self.second = nn.Linear(hidden_size, feature_size) self.tanh = nn.Tanh() def forward(self, left_input, right_input): output = self.left(left_input) output += self.right(right_input) output = self.tanh(output) output = self.second(output) output = self.tanh(output) return output def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'feature_size': 4, 'symmetry_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 from torch import nn import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_tanh_0(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 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x3, 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 + x3, tmp7, xmask) @triton.jit def triton_poi_fused_tanh_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 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, primals_8) = 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, 4), (64, 16, 4, 1)) assert_size_stride(primals_7, (4, 4), (4, 1)) assert_size_stride(primals_8, (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((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_6, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1) del primals_4 buf2 = 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)](buf2, primals_2, buf1, primals_5, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 del primals_5 buf3 = buf1 del buf1 extern_kernels.mm(reinterpret_tensor(buf2, (64, 4), (4, 1), 0), reinterpret_tensor(primals_7, (4, 4), (1, 4), 0), out=buf3) buf4 = reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf3 triton_poi_fused_tanh_1[grid(256)](buf4, primals_8, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_8 return buf4, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(primals_6, (64, 4), (4, 1), 0 ), buf2, buf4, primals_7 class SymEncoderNew(nn.Module): def __init__(self, feature_size, symmetry_size, hidden_size): super(SymEncoderNew, self).__init__() self.left = nn.Linear(feature_size, hidden_size) self.right = nn.Linear(symmetry_size, hidden_size) self.second = nn.Linear(hidden_size, feature_size) self.tanh = nn.Tanh() def forward(self, input_0, input_1): primals_1 = self.left.weight primals_2 = self.left.bias primals_4 = self.right.weight primals_5 = self.right.bias primals_7 = self.second.weight primals_8 = self.second.bias primals_3 = input_0 primals_6 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8]) return output[0]
kevin-kaixu/grass_pytorch
SymEncoder
false
15,814
[ "Apache-2.0" ]
85
1d8dc6dcc0ab3ca029e449f57c37ba3910a4f90a
https://github.com/kevin-kaixu/grass_pytorch/tree/1d8dc6dcc0ab3ca029e449f57c37ba3910a4f90a
gconv_RNN
import torch import torch.utils.data import torch.nn as nn class gconv_RNN(nn.Module): def __init__(self): super(gconv_RNN, self).__init__() def forward(self, x, A): x = torch.einsum('nvc,nvw->nwc', (x, A)) return x.contiguous() def get_inputs(): return [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 import torch.utils.data import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clone_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask) def call(args): arg0_1, arg1_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)) 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(reinterpret_tensor(arg0_1, (4, 4, 4), (16, 1, 4), 0), arg1_1, 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_clone_0[grid(16, 4)](buf0, buf1, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) del buf0 return buf1, class gconv_RNNNew(nn.Module): def __init__(self): super(gconv_RNNNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kevin-xuan/Traffic-Benchmark
gconv_RNN
false
15,815
[ "MIT" ]
120
b9f8e40b4df9b58f5ad88432dc070cbbbcdc0228
https://github.com/kevin-xuan/Traffic-Benchmark/tree/b9f8e40b4df9b58f5ad88432dc070cbbbcdc0228
AdjEncoder
import torch from torch import nn import torch.utils.data class AdjEncoder(nn.Module): def __init__(self, feature_size, hidden_size): super(AdjEncoder, self).__init__() self.left = nn.Linear(feature_size, hidden_size) self.right = nn.Linear(feature_size, hidden_size, bias=False) self.second = nn.Linear(hidden_size, feature_size) self.tanh = nn.Tanh() def forward(self, left_input, right_input): output = self.left(left_input) output += self.right(right_input) output = self.tanh(output) output = self.second(output) output = self.tanh(output) return output def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'feature_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 from torch import nn import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_tanh_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 x3 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x3, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp5 = libdevice.tanh(tmp4) tl.store(in_out_ptr0 + x3, tmp5, xmask) @triton.jit def triton_poi_fused_tanh_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 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, 4, 4, 4), (64, 16, 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 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_5, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1) del primals_4 buf2 = 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)](buf2, primals_2, buf1, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf3 = buf1 del buf1 extern_kernels.mm(reinterpret_tensor(buf2, (64, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), out=buf3) buf4 = reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf3 triton_poi_fused_tanh_1[grid(256)](buf4, primals_7, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 return buf4, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(primals_5, (64, 4), (4, 1), 0 ), buf2, buf4, primals_6 class AdjEncoderNew(nn.Module): def __init__(self, feature_size, hidden_size): super(AdjEncoderNew, self).__init__() self.left = nn.Linear(feature_size, hidden_size) self.right = nn.Linear(feature_size, hidden_size, bias=False) self.second = nn.Linear(hidden_size, feature_size) self.tanh = nn.Tanh() def forward(self, input_0, input_1): primals_1 = self.left.weight primals_2 = self.left.bias primals_4 = self.right.weight primals_6 = self.second.weight primals_7 = self.second.bias primals_3 = input_0 primals_5 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
kevin-kaixu/grass_pytorch
AdjEncoder
false
15,816
[ "Apache-2.0" ]
85
1d8dc6dcc0ab3ca029e449f57c37ba3910a4f90a
https://github.com/kevin-kaixu/grass_pytorch/tree/1d8dc6dcc0ab3ca029e449f57c37ba3910a4f90a
SymDecoder
import torch from torch import nn import torch.utils.data class SymDecoder(nn.Module): def __init__(self, feature_size, symmetry_size, hidden_size): super(SymDecoder, self).__init__() self.mlp = nn.Linear(feature_size, hidden_size) self.tanh = nn.Tanh() self.mlp_sg = nn.Linear(hidden_size, feature_size) self.mlp_sp = nn.Linear(hidden_size, symmetry_size) def forward(self, parent_feature): vector = self.mlp(parent_feature) vector = self.tanh(vector) sym_gen_vector = self.mlp_sg(vector) sym_gen_vector = self.tanh(sym_gen_vector) sym_param_vector = self.mlp_sp(vector) sym_param_vector = self.tanh(sym_param_vector) return sym_gen_vector, sym_param_vector def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'feature_size': 4, 'symmetry_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 from torch import nn import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda 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, (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_tanh_0[grid(256)](buf1, primals_2, 256, XBLOCK=256, 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=256, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), out=buf4) buf5 = reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf4 triton_poi_fused_tanh_0[grid(256)](buf5, primals_7, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 return buf3, buf5, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf1, buf3, buf5, primals_6, primals_4 class SymDecoderNew(nn.Module): def __init__(self, feature_size, symmetry_size, hidden_size): super(SymDecoderNew, self).__init__() self.mlp = nn.Linear(feature_size, hidden_size) self.tanh = nn.Tanh() self.mlp_sg = nn.Linear(hidden_size, feature_size) self.mlp_sp = nn.Linear(hidden_size, symmetry_size) def forward(self, input_0): primals_1 = self.mlp.weight primals_2 = self.mlp.bias primals_4 = self.mlp_sg.weight primals_5 = self.mlp_sg.bias primals_6 = self.mlp_sp.weight primals_7 = self.mlp_sp.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0], output[1]
kevin-kaixu/grass_pytorch
SymDecoder
false
15,817
[ "Apache-2.0" ]
85
1d8dc6dcc0ab3ca029e449f57c37ba3910a4f90a
https://github.com/kevin-kaixu/grass_pytorch/tree/1d8dc6dcc0ab3ca029e449f57c37ba3910a4f90a
FocalLoss2d
import torch from torch import nn class FocalLoss2d(nn.Module): def __init__(self, gamma=2, ignore_index=255): super().__init__() self.gamma = gamma self.ignore_index = ignore_index def forward(self, outputs: 'torch.Tensor', targets: 'torch.Tensor'): outputs = outputs.contiguous() targets = targets.contiguous() eps = 1e-08 non_ignored = targets.view(-1) != self.ignore_index targets = targets.view(-1)[non_ignored].float() outputs = outputs.contiguous().view(-1)[non_ignored] outputs = torch.clamp(outputs, eps, 1.0 - eps) targets = torch.clamp(targets, eps, 1.0 - eps) pt = (1 - targets) * (1 - outputs) + targets * outputs return (-(1.0 - pt) ** self.gamma * torch.log(pt)).mean() def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch 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_ne_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 = 255.0 tmp2 = tmp0 != tmp1 tl.store(out_ptr0 + x0, tmp2, 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((256,), (1,), torch.bool) get_raw_stream(0) triton_poi_fused_ne_0[grid(256)](arg1_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) return reinterpret_tensor(arg1_1, (256,), (1,), 0), buf0, arg0_1 class FocalLoss2dNew(nn.Module): def __init__(self, gamma=2, ignore_index=255): super().__init__() self.gamma = gamma self.ignore_index = ignore_index def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kevinkwshin/kaggle-pneumothorax
FocalLoss2d
false
15,818
[ "MIT" ]
74
24b91a9425097023f0cc7781a9380cb247babe22
https://github.com/kevinkwshin/kaggle-pneumothorax/tree/24b91a9425097023f0cc7781a9380cb247babe22
AdaptiveAvgPool3dOutSize1
import torch from typing import Tuple import torch.nn as nn from abc import abstractmethod import torch.utils.data import torch.nn class EfficientBlockBase(nn.Module): """ PyTorchVideo/accelerator provides a set of efficient blocks that have optimal efficiency for each target hardware device. Each efficient block has two forms: - original form: this form is for training. When efficient block is instantiated, it is in this original form. - deployable form: this form is for deployment. Once the network is ready for deploy, it can be converted into deployable form for efficient execution on target hardware. One block is transformed into deployable form by calling convert() method. By conversion to deployable form, various optimization (operator fuse, kernel optimization, etc.) are applied. EfficientBlockBase is the base class for efficient blocks. All efficient blocks should inherit this base class and implement following methods: - forward(): same as required by nn.Module - convert(): called to convert block into deployable form """ @abstractmethod def convert(self): pass @abstractmethod def forward(self): pass class AdaptiveAvgPool3dOutSize1(EfficientBlockBase): """ Implements AdaptiveAvgPool3d with output (T, H, W) = (1, 1, 1). This operator has better efficiency than AdaptiveAvgPool for mobile CPU. """ def __init__(self): super().__init__() self.pool = nn.AdaptiveAvgPool3d(1) self.convert_flag = False def convert(self, input_blob_size: 'Tuple', **kwargs): """ Converts AdaptiveAvgPool into AvgPool with constant kernel size for better efficiency. Args: input_blob_size (tuple): blob size at the input of AdaptiveAvgPool3dOutSize1 instance during forward. kwargs (any): any keyword argument (unused). """ assert self.convert_flag is False, 'AdaptiveAvgPool3dOutSize1: already converted, cannot be converted again' kernel_size = input_blob_size[2:] self.pool = nn.AvgPool3d(kernel_size) self.convert_flag = True def forward(self, x): return self.pool(x) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from typing import Tuple import torch.nn as nn from abc import abstractmethod import torch.utils.data 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_mean_0(in_out_ptr0, in_ptr0, 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.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, 0) tmp4 = tl.sum(tmp3, 1)[:, None] tmp5 = 64.0 tmp6 = tmp4 / tmp5 tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp6, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 1, 1, 1), (1, 4, 4, 4), torch.float32) buf1 = reinterpret_tensor(buf0, (4, 1, 1, 1), (1, 1, 1, 1), 0) del buf0 get_raw_stream(0) triton_per_fused_mean_0[grid(4)](buf1, arg0_1, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 return buf1, class EfficientBlockBase(nn.Module): """ PyTorchVideo/accelerator provides a set of efficient blocks that have optimal efficiency for each target hardware device. Each efficient block has two forms: - original form: this form is for training. When efficient block is instantiated, it is in this original form. - deployable form: this form is for deployment. Once the network is ready for deploy, it can be converted into deployable form for efficient execution on target hardware. One block is transformed into deployable form by calling convert() method. By conversion to deployable form, various optimization (operator fuse, kernel optimization, etc.) are applied. EfficientBlockBase is the base class for efficient blocks. All efficient blocks should inherit this base class and implement following methods: - forward(): same as required by nn.Module - convert(): called to convert block into deployable form """ @abstractmethod def convert(self): pass @abstractmethod def forward(self): pass class AdaptiveAvgPool3dOutSize1New(EfficientBlockBase): """ Implements AdaptiveAvgPool3d with output (T, H, W) = (1, 1, 1). This operator has better efficiency than AdaptiveAvgPool for mobile CPU. """ def __init__(self): super().__init__() self.pool = nn.AdaptiveAvgPool3d(1) self.convert_flag = False def convert(self, input_blob_size: 'Tuple', **kwargs): """ Converts AdaptiveAvgPool into AvgPool with constant kernel size for better efficiency. Args: input_blob_size (tuple): blob size at the input of AdaptiveAvgPool3dOutSize1 instance during forward. kwargs (any): any keyword argument (unused). """ assert self.convert_flag is False, 'AdaptiveAvgPool3dOutSize1: already converted, cannot be converted again' kernel_size = input_blob_size[2:] self.pool = nn.AvgPool3d(kernel_size) self.convert_flag = True def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
kevinmtian/pytorchvideo
AdaptiveAvgPool3dOutSize1
false
15,819
[ "Apache-2.0" ]
2,391
168e16859a6029ef8ebeb476f9163bebb6c6b87d
https://github.com/kevinmtian/pytorchvideo/tree/168e16859a6029ef8ebeb476f9163bebb6c6b87d
JaccardLoss
import torch from torch import nn def jaccard(outputs, targets, per_image=False, non_empty=False, min_pixels=5): batch_size = outputs.size()[0] eps = 0.001 if not per_image: batch_size = 1 dice_target = targets.contiguous().view(batch_size, -1).float() dice_output = outputs.contiguous().view(batch_size, -1) target_sum = torch.sum(dice_target, dim=1) intersection = torch.sum(dice_output * dice_target, dim=1) losses = 1.0 - (intersection + eps) / (torch.sum(dice_output + dice_target, dim=1) - intersection + eps) if non_empty: assert per_image non_empty_images = 0 sum_loss = 0 for i in range(batch_size): if target_sum[i] > min_pixels: sum_loss += losses[i] non_empty_images += 1 if non_empty_images == 0: return 0 else: return sum_loss / non_empty_images return losses.mean() class JaccardLoss(nn.Module): def __init__(self, weight=None, size_average=True, per_image=False, non_empty=False, apply_sigmoid=False, min_pixels=5): super().__init__() self.size_average = size_average self.register_buffer('weight', weight) self.per_image = per_image self.non_empty = non_empty self.apply_sigmoid = apply_sigmoid self.min_pixels = min_pixels def forward(self, input, target): if self.apply_sigmoid: input = torch.sigmoid(input) return jaccard(input, target, per_image=self.per_image, non_empty= self.non_empty, min_pixels=self.min_pixels) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_per_fused_add_div_mean_mul_rsub_sub_sum_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) 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 = tmp0 + tmp1 tmp7 = tl.broadcast_to(tmp6, [RBLOCK]) tmp9 = triton_helpers.promote_to_tensor(tl.sum(tmp7, 0)) tmp10 = 0.001 tmp11 = tmp5 + tmp10 tmp12 = tmp9 - tmp5 tmp13 = tmp12 + tmp10 tmp14 = tmp11 / tmp13 tmp15 = 1.0 tmp16 = tmp15 - tmp14 tmp17 = tmp16 / tmp15 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((1,), (1,), torch.float32) buf2 = reinterpret_tensor(buf0, (), (), 0) del buf0 get_raw_stream(0) triton_per_fused_add_div_mean_mul_rsub_sub_sum_0[grid(1)](buf2, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf2, def jaccard(outputs, targets, per_image=False, non_empty=False, min_pixels=5): batch_size = outputs.size()[0] eps = 0.001 if not per_image: batch_size = 1 dice_target = targets.contiguous().view(batch_size, -1).float() dice_output = outputs.contiguous().view(batch_size, -1) target_sum = torch.sum(dice_target, dim=1) intersection = torch.sum(dice_output * dice_target, dim=1) losses = 1.0 - (intersection + eps) / (torch.sum(dice_output + dice_target, dim=1) - intersection + eps) if non_empty: assert per_image non_empty_images = 0 sum_loss = 0 for i in range(batch_size): if target_sum[i] > min_pixels: sum_loss += losses[i] non_empty_images += 1 if non_empty_images == 0: return 0 else: return sum_loss / non_empty_images return losses.mean() class JaccardLossNew(nn.Module): def __init__(self, weight=None, size_average=True, per_image=False, non_empty=False, apply_sigmoid=False, min_pixels=5): super().__init__() self.size_average = size_average self.register_buffer('weight', weight) self.per_image = per_image self.non_empty = non_empty self.apply_sigmoid = apply_sigmoid self.min_pixels = min_pixels def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kevinkwshin/kaggle-pneumothorax
JaccardLoss
false
15,820
[ "MIT" ]
74
24b91a9425097023f0cc7781a9380cb247babe22
https://github.com/kevinkwshin/kaggle-pneumothorax/tree/24b91a9425097023f0cc7781a9380cb247babe22
DiceLoss
import torch from torch import nn def soft_dice_loss(outputs, targets, per_image=False): batch_size = outputs.size()[0] eps = 1e-05 if not per_image: batch_size = 1 dice_target = targets.contiguous().view(batch_size, -1).float() dice_output = outputs.contiguous().view(batch_size, -1) intersection = torch.sum(dice_output * dice_target, dim=1) union = torch.sum(dice_output, dim=1) + torch.sum(dice_target, dim=1) + eps loss = (1 - (2 * intersection + eps) / union).mean() return loss class DiceLoss(nn.Module): def __init__(self, weight=None, size_average=True, per_image=False): super().__init__() self.size_average = size_average self.register_buffer('weight', weight) self.per_image = per_image def forward(self, input, target): return soft_dice_loss(input, target, per_image=self.per_image) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_per_fused_add_div_mean_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-05 tmp15 = tmp13 + tmp14 tmp16 = tmp8 + tmp11 tmp17 = tmp16 + tmp14 tmp18 = tmp15 / tmp17 tmp19 = 1.0 tmp20 = tmp19 - tmp18 tmp21 = tmp20 / tmp19 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp21, 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((1,), (1,), torch.float32) buf3 = reinterpret_tensor(buf0, (), (), 0) del buf0 get_raw_stream(0) triton_per_fused_add_div_mean_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, def soft_dice_loss(outputs, targets, per_image=False): batch_size = outputs.size()[0] eps = 1e-05 if not per_image: batch_size = 1 dice_target = targets.contiguous().view(batch_size, -1).float() dice_output = outputs.contiguous().view(batch_size, -1) intersection = torch.sum(dice_output * dice_target, dim=1) union = torch.sum(dice_output, dim=1) + torch.sum(dice_target, dim=1) + eps loss = (1 - (2 * intersection + eps) / union).mean() return loss class DiceLossNew(nn.Module): def __init__(self, weight=None, size_average=True, per_image=False): super().__init__() self.size_average = size_average self.register_buffer('weight', weight) self.per_image = per_image def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kevinkwshin/kaggle-pneumothorax
DiceLoss
false
15,821
[ "MIT" ]
74
24b91a9425097023f0cc7781a9380cb247babe22
https://github.com/kevinkwshin/kaggle-pneumothorax/tree/24b91a9425097023f0cc7781a9380cb247babe22
MaskedTemporalPooling
import torch from typing import Optional import torch.utils.data import torch.nn class MaskedTemporalPooling(torch.nn.Module): """ Applies temporal pooling operations on masked inputs. For each pooling operation all masked values are ignored. """ def __init__(self, method: 'str'): """ method (str): the method of pooling to use. Options: 'max': reduces temporal dimension to each valid max value. 'avg': averages valid values in the temporal dimension. 'sum': sums valid values in the temporal dimension. Note if all batch row elements are invalid, the temporal dimension is pooled to 0 values. """ super().__init__() assert method in ('max', 'avg', 'sum') self._method = method def forward(self, x: 'torch.Tensor', mask: 'Optional[torch.Tensor]'=None ) ->torch.Tensor: """ Args: x (torch.Tensor): tensor with shape (batch_size, seq_len, feature_dim) mask (torch.Tensor): bool tensor with shape (batch_size, seq_len). Sequence elements that are False are invalid. Returns: Tensor with shape (batch_size, feature_dim) """ assert x.dim( ) == 3, 'Requires x shape (batch_size x seq_len x feature_dim)' b, t = x.shape[0], x.shape[1] if mask is None: mask = torch.ones((b, t), dtype=torch.bool) if self._method == 'max': x[~mask, :] = float('-inf') invalid_first_dim = ~mask.view(b, -1).any(dim=-1) x[invalid_first_dim, :] = 0 x = torch.max(x, dim=1)[0] elif self._method == 'avg': x = x * mask.unsqueeze(-1).float() mask = mask.view(b, t, -1).any(dim=-1) valid_lengths = mask.float().sum(dim=-1).int() x = x.sum(dim=1) x = x.div(valid_lengths.clamp(min=1).unsqueeze(-1).expand(x. size()).float()) elif self._method == 'sum': x = x * mask.unsqueeze(-1).float() x = x.sum(dim=1) else: raise NotImplementedError( f"{self._method} not available options are: 'max', 'avg', 'sum'" ) return x def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'method': 'max'}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.utils.data import torch.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_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 + x0, xmask) tmp1 = tl.full([1], False, tl.int1) tmp2 = float('-inf') tmp3 = tl.where(tmp1, tmp2, tmp0) tl.store(out_ptr0 + x0, tmp3, xmask) @triton.jit def triton_poi_fused_index_put_lift_fresh_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 tmp5 = tl.load(in_ptr0 + x0, xmask) tmp0 = tl.full([1], True, tl.int1) tmp1 = tmp0 | tmp0 tmp2 = tmp1 | tmp0 tmp3 = tmp2 | tmp0 tmp4 = tmp3 == 0 tmp6 = 0.0 tmp7 = tl.where(tmp4, tmp6, tmp5) tl.store(out_ptr0 + x0, tmp7, xmask) @triton.jit def triton_poi_fused_max_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask) tmp1 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask) tmp3 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask) tmp5 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask) tmp2 = triton_helpers.maximum(tmp0, tmp1) tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp6 = triton_helpers.maximum(tmp4, tmp5) tl.store(out_ptr0 + x2, tmp6, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) get_raw_stream(0) triton_poi_fused_index_put_lift_fresh_0[grid(64)](arg0_1, arg0_1, 64, XBLOCK=64, num_warps=1, num_stages=1) triton_poi_fused_index_put_lift_fresh_1[grid(64)](arg0_1, arg0_1, 64, XBLOCK=64, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_max_2[grid(16)](arg0_1, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg0_1 return buf2, class MaskedTemporalPoolingNew(torch.nn.Module): """ Applies temporal pooling operations on masked inputs. For each pooling operation all masked values are ignored. """ def __init__(self, method: 'str'): """ method (str): the method of pooling to use. Options: 'max': reduces temporal dimension to each valid max value. 'avg': averages valid values in the temporal dimension. 'sum': sums valid values in the temporal dimension. Note if all batch row elements are invalid, the temporal dimension is pooled to 0 values. """ super().__init__() assert method in ('max', 'avg', 'sum') self._method = method def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
kevinmtian/pytorchvideo
MaskedTemporalPooling
false
15,822
[ "Apache-2.0" ]
2,391
168e16859a6029ef8ebeb476f9163bebb6c6b87d
https://github.com/kevinmtian/pytorchvideo/tree/168e16859a6029ef8ebeb476f9163bebb6c6b87d
PSNRLoss
import torch def psnr(gt, pred, data_range=None, batch=True, reduce=True): """ Compute the peak signal to noise ratio (psnr) :param gt: gt image (torch.Tensor :param pred: input image (torch.Tensor) :param data_range: if None, estimated from gt :return: (mean) psnr """ if batch: batch_size = gt.shape[0] else: batch_size = 1 pred = pred.contiguous().view(batch_size, -1) gt = gt.contiguous().view(batch_size, -1) if data_range is None: data_range = gt.max(dim=1)[0] mse_err = (abs(gt - pred) ** 2).mean(1) psnr_val = 10 * torch.log10(data_range ** 2 / mse_err) if reduce: return psnr_val.mean() else: return psnr_val class PSNRLoss(torch.nn.Module): """ Computes PSNR between two images according to: psnr(x, y) = 10 * log10(1/MSE(x, y)), MSE(x, y) = ||x-y||^2 / size(x) Parameters: ----------- x: Tensor - gterence image (or batch) y: Tensor - reconstructed image (or batch) normalized: bool - If abs(data) is normalized to [0, 1] batch_mode: bool - If batch is passed, set this to True is_complex: bool - If data is complex valued, 2 values (e.g. (x,y)) are paired Notice that ``abs'' squares Be cagtul with the order, since peak intensity is taken from the gterence image (taking from reconstruction yields a different value). """ def __init__(self, batch=True, reduce=True): """ normalized: bool - If abs(data) is normalized to [0, 1] batch_mode: bool - If batch is passed, set this to True is_complex: bool - If data is complex valued, 2 values (e.g. (x,y)) are paired """ super(PSNRLoss, self).__init__() self.batch = batch self.reduce = reduce def forward(self, pred, gt, data_range=None): return psnr(pred, gt, data_range=data_range, batch=self.batch, reduce=self.reduce) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math 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_mean_pow_sub_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, 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) tmp5 = tl.load(in_ptr1 + (r1 + 64 * 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] tmp6 = tmp0 - tmp5 tmp7 = tl_math.abs(tmp6) tmp8 = tmp7 * tmp7 tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK]) tmp11 = tl.where(xmask, tmp9, 0) tmp12 = tl.sum(tmp11, 1)[:, None] tl.store(out_ptr0 + x0, tmp4, xmask) tl.store(out_ptr1 + x0, tmp12, xmask) @triton.jit def triton_per_fused_abs_div_log10_mean_mul_pow_sub_1(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 4 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp2 = tl.load(in_ptr1 + r0, None) tmp1 = tmp0 * tmp0 tmp3 = 64.0 tmp4 = tmp2 / tmp3 tmp5 = tmp1 / tmp4 tmp6 = libdevice.log10(tmp5) tmp7 = 10.0 tmp8 = tmp6 * tmp7 tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK]) tmp11 = tl.sum(tmp9, 1)[:, None] tmp12 = 4.0 tmp13 = tmp11 / tmp12 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp13, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4,), (1,), torch.float32) buf2 = empty_strided_cuda((4,), (1,), torch.float32) get_raw_stream(0) triton_per_fused_abs_max_mean_pow_sub_0[grid(4)](arg0_1, arg1_1, buf0, buf2, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 buf3 = empty_strided_cuda((), (), torch.float32) buf4 = buf3 del buf3 triton_per_fused_abs_div_log10_mean_mul_pow_sub_1[grid(1)](buf4, buf0, buf2, 1, 4, XBLOCK=1, num_warps=2, num_stages=1) del buf0 del buf2 return buf4, def psnr(gt, pred, data_range=None, batch=True, reduce=True): """ Compute the peak signal to noise ratio (psnr) :param gt: gt image (torch.Tensor :param pred: input image (torch.Tensor) :param data_range: if None, estimated from gt :return: (mean) psnr """ if batch: batch_size = gt.shape[0] else: batch_size = 1 pred = pred.contiguous().view(batch_size, -1) gt = gt.contiguous().view(batch_size, -1) if data_range is None: data_range = gt.max(dim=1)[0] mse_err = (abs(gt - pred) ** 2).mean(1) psnr_val = 10 * torch.log10(data_range ** 2 / mse_err) if reduce: return psnr_val.mean() else: return psnr_val class PSNRLossNew(torch.nn.Module): """ Computes PSNR between two images according to: psnr(x, y) = 10 * log10(1/MSE(x, y)), MSE(x, y) = ||x-y||^2 / size(x) Parameters: ----------- x: Tensor - gterence image (or batch) y: Tensor - reconstructed image (or batch) normalized: bool - If abs(data) is normalized to [0, 1] batch_mode: bool - If batch is passed, set this to True is_complex: bool - If data is complex valued, 2 values (e.g. (x,y)) are paired Notice that ``abs'' squares Be cagtul with the order, since peak intensity is taken from the gterence image (taking from reconstruction yields a different value). """ def __init__(self, batch=True, reduce=True): """ normalized: bool - If abs(data) is normalized to [0, 1] batch_mode: bool - If batch is passed, set this to True is_complex: bool - If data is complex valued, 2 values (e.g. (x,y)) are paired """ super(PSNRLossNew, self).__init__() self.batch = batch self.reduce = reduce def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
khammernik/sigmanet
PSNRLoss
false
15,823
[ "MIT" ]
50
6eb8dbd1ee350bb9baee60eb254080f7d660bbc5
https://github.com/khammernik/sigmanet/tree/6eb8dbd1ee350bb9baee60eb254080f7d660bbc5
LearnMaskedDefault
import torch import torch.nn as nn import torch.utils.data import torch.nn class LearnMaskedDefault(nn.Module): """ Learns default values to fill invalid entries within input tensors. The invalid entries are represented by a mask which is passed into forward alongside the input tensor. Note the default value is only used if all entries in the batch row are invalid rather than just a portion of invalid entries within each batch row. """ def __init__(self, feature_dim: 'int', init_method: 'str'='gaussian', freeze: 'bool'=False): """ Args: feature_dim (int): the size of the default value parameter, this must match the input tensor size. init_method (str): the initial default value parameter. Options: 'guassian' 'zeros' freeze (bool): If True, the learned default parameter weights are frozen. """ super().__init__() if init_method == 'zeros': self._learned_defaults = nn.Parameter(torch.zeros(feature_dim), requires_grad=not freeze) elif init_method == 'gaussian': self._learned_defaults = nn.Parameter(torch.Tensor(feature_dim), requires_grad=not freeze) nn.init.normal_(self._learned_defaults) else: raise NotImplementedError( f"{init_method} not available. Options are: 'zeros' or 'gaussian'" ) def forward(self, x: 'torch.Tensor', mask: 'torch.Tensor') ->torch.Tensor: """ Args: x (torch.Tensor): tensor of shape (batch_size, feature_dim). mask (torch.Tensor): bool tensor of shape (batch_size, seq_len) If all elements in the batch dimension are False the learned default parameter is used for that batch element. Returns: Tensor with shape (batch_size, feature_dim) """ mask = mask.view(mask.shape[0], -1).any(dim=-1) for i in range(1, x.dim()): mask = mask.unsqueeze(i) x = x * mask.float() + self._learned_defaults * (1 - mask.float()) return x def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'feature_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 import torch.nn as nn import torch.utils.data 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__to_copy_add_any_mul_rsub_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, 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 r2 = rindex % 4 tmp0 = tl.load(in_ptr0 + (r1 + 64 * x0), xmask, other=0.0) tmp6 = tl.load(in_ptr1 + (r1 + 64 * x0), xmask, other=0.0) tmp9 = tl.load(in_ptr2 + r2, None, eviction_policy='evict_last') tmp1 = tmp0 != 0 tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp4 = tl.where(xmask, tmp2, 0) tmp5 = triton_helpers.any(tmp4, 1)[:, None] tmp7 = tmp5.to(tl.float32) tmp8 = tmp6 * tmp7 tmp10 = 1.0 tmp11 = tmp10 - tmp7 tmp12 = tmp9 * tmp11 tmp13 = tmp8 + tmp12 tl.store(out_ptr1 + (r1 + 64 * x0), tmp13, xmask) tl.store(out_ptr0 + x0, tmp5, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4,), (1,), torch.bool) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_per_fused__to_copy_add_any_mul_rsub_0[grid(4)](primals_1, primals_2, primals_3, buf0, buf1, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) del primals_1 del primals_2 del primals_3 return buf1, reinterpret_tensor(buf0, (4, 1, 1, 1), (1, 1, 1, 1), 0) class LearnMaskedDefaultNew(nn.Module): """ Learns default values to fill invalid entries within input tensors. The invalid entries are represented by a mask which is passed into forward alongside the input tensor. Note the default value is only used if all entries in the batch row are invalid rather than just a portion of invalid entries within each batch row. """ def __init__(self, feature_dim: 'int', init_method: 'str'='gaussian', freeze: 'bool'=False): """ Args: feature_dim (int): the size of the default value parameter, this must match the input tensor size. init_method (str): the initial default value parameter. Options: 'guassian' 'zeros' freeze (bool): If True, the learned default parameter weights are frozen. """ super().__init__() if init_method == 'zeros': self._learned_defaults = nn.Parameter(torch.zeros(feature_dim), requires_grad=not freeze) elif init_method == 'gaussian': self._learned_defaults = nn.Parameter(torch.Tensor(feature_dim), requires_grad=not freeze) nn.init.normal_(self._learned_defaults) else: raise NotImplementedError( f"{init_method} not available. Options are: 'zeros' or 'gaussian'" ) def forward(self, input_0, input_1): primals_3 = self._learned_defaults primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3]) return output[0]
kevinmtian/pytorchvideo
LearnMaskedDefault
false
15,824
[ "Apache-2.0" ]
2,391
168e16859a6029ef8ebeb476f9163bebb6c6b87d
https://github.com/kevinmtian/pytorchvideo/tree/168e16859a6029ef8ebeb476f9163bebb6c6b87d
SpatialSoftArgmax
import torch from torch import Tensor import torch.nn as nn import torch.nn.functional as F class SpatialSoftArgmax(nn.Module): """Spatial softmax as defined in `1`_. Concretely, the spatial softmax of each feature map is used to compute a weighted mean of the pixel locations, effectively performing a soft arg-max over the feature dimension. .. _1: https://arxiv.org/abs/1504.00702 """ def __init__(self, normalize: 'bool'=False): """Constructor. Args: normalize: Whether to use normalized image coordinates, i.e. coordinates in the range `[-1, 1]`. """ super().__init__() self.normalize = normalize def _coord_grid(self, h: 'int', w: 'int', device: 'torch.device') ->Tensor: if self.normalize: return torch.stack(torch.meshgrid(torch.linspace(-1, 1, w, device=device), torch.linspace(-1, 1, h, device=device))) return torch.stack(torch.meshgrid(torch.arange(0, w, device=device), torch.arange(0, h, device=device))) def forward(self, x: 'Tensor') ->Tensor: assert x.ndim == 4, 'Expecting a tensor of shape (B, C, H, W).' _b, c, h, w = x.shape softmax = F.softmax(x.view(-1, h * w), dim=-1) xc, yc = self._coord_grid(h, w, x.device) x_mean = (softmax * xc.flatten()).sum(dim=1, keepdims=True) y_mean = (softmax * yc.flatten()).sum(dim=1, keepdims=True) return torch.cat([x_mean, y_mean], dim=1).view(-1, c * 2) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math from torch import Tensor 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__softmax_mul_sum_0(in_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 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 tmp12 = r1 // 4 tl.full([1, 1], 0, tl.int64) tmp15 = tl.full([1, 1], 4, tl.int64) tmp16 = tmp12 < tmp15 tmp17 = tl.broadcast_to(r1 // 4, [XBLOCK, RBLOCK]) tmp18 = tl.full(tmp17.shape, 0.0, tmp17.dtype) tmp19 = tl.where(tmp16, tmp17, tmp18) tmp20 = tmp12 >= tmp15 tl.full([1, 1], 8, tl.int64) tmp23 = tl.broadcast_to(r1 % 4, [XBLOCK, RBLOCK]) tmp24 = tl.full(tmp23.shape, 0.0, tmp23.dtype) tmp25 = tl.where(tmp20, tmp23, tmp24) tmp26 = tl.where(tmp16, tmp19, tmp25) tmp27 = tmp26.to(tl.float32) tmp28 = tmp11 * tmp27 tmp29 = tl.broadcast_to(tmp28, [XBLOCK, RBLOCK]) tmp31 = tl.where(xmask, tmp29, 0) tmp32 = tl.sum(tmp31, 1)[:, None] tmp33 = 4 + r1 // 4 tmp35 = tmp33 < tmp15 tmp36 = tl.broadcast_to(4 + r1 // 4, [XBLOCK, RBLOCK]) tmp37 = tl.full(tmp36.shape, 0.0, tmp36.dtype) tmp38 = tl.where(tmp35, tmp36, tmp37) tmp39 = tmp33 >= tmp15 tmp41 = tl.where(tmp39, tmp23, tmp24) tmp42 = tl.where(tmp35, tmp38, tmp41) tmp43 = tmp42.to(tl.float32) tmp44 = tmp11 * tmp43 tmp45 = tl.broadcast_to(tmp44, [XBLOCK, RBLOCK]) tmp47 = tl.where(xmask, tmp45, 0) tmp48 = tl.sum(tmp47, 1)[:, None] tl.store(out_ptr2 + 2 * x0, tmp32, xmask) tl.store(out_ptr3 + 2 * x0, tmp48, 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) buf4 = empty_strided_cuda((16, 2), (2, 1), torch.float32) buf2 = reinterpret_tensor(buf4, (16, 1), (2, 1), 0) buf3 = reinterpret_tensor(buf4, (16, 1), (2, 1), 1) get_raw_stream(0) triton_per_fused__softmax_mul_sum_0[grid(16)](arg0_1, buf2, buf3, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) del arg0_1 return reinterpret_tensor(buf4, (4, 8), (8, 1), 0), class SpatialSoftArgmaxNew(nn.Module): """Spatial softmax as defined in `1`_. Concretely, the spatial softmax of each feature map is used to compute a weighted mean of the pixel locations, effectively performing a soft arg-max over the feature dimension. .. _1: https://arxiv.org/abs/1504.00702 """ def __init__(self, normalize: 'bool'=False): """Constructor. Args: normalize: Whether to use normalized image coordinates, i.e. coordinates in the range `[-1, 1]`. """ super().__init__() self.normalize = normalize def _coord_grid(self, h: 'int', w: 'int', device: 'torch.device') ->Tensor: if self.normalize: return torch.stack(torch.meshgrid(torch.linspace(-1, 1, w, device=device), torch.linspace(-1, 1, h, device=device))) return torch.stack(torch.meshgrid(torch.arange(0, w, device=device), torch.arange(0, h, device=device))) def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
kevinzakka/torchkit
SpatialSoftArgmax
false
15,825
[ "MIT" ]
144
930dba9560d2473406b59b99a474dce1a6621813
https://github.com/kevinzakka/torchkit/tree/930dba9560d2473406b59b99a474dce1a6621813
TransposeMultiheadAttention
import torch import torch.nn as nn from typing import Optional import torch.utils.data import torch.nn class TransposeMultiheadAttention(nn.Module): """ Wrapper for nn.MultiheadAttention which first transposes the input tensor from (batch_size, seq_len, feature_dim) to (seq_length, batch_size, feature_dim), then applies the attention and transposes the attention outputs back to the input shape. """ def __init__(self, feature_dim: 'int', num_heads: 'int'=1): """ Args: feature_dim (int): attention embedding dimension num_heads (int): number of attention heads """ super().__init__() self._attention = nn.MultiheadAttention(embed_dim=feature_dim, num_heads=num_heads) self._attention_weights = None @property def attention_weights(self) ->Optional[torch.Tensor]: """ Contains attention weights from last forward call. """ return self._attention_weights def forward(self, x: 'torch.Tensor', mask: 'Optional[torch.Tensor]'=None ) ->torch.Tensor: """ Args: x (torch.Tensor): tensor of shape (batch_size, seq_len, feature_dim) mask (torch.Tensor): bool tensor with shape (batch_size, seq_len). Sequence elements that are False are invalid. Returns: Tensor with shape (batch_size, seq_len, feature_dim) """ assert x.dim( ) == 3, 'Requires x shape (batch_size x seq_len x feature_dim)' if mask is not None: mask[:, 0] = True mask = ~mask x = x.transpose(0, 1) attn_output, self._attention_weights = self._attention(x, x, x, key_padding_mask=mask) attn_output = attn_output.transpose(0, 1) return attn_output def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'feature_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 as nn from typing import Optional import torch.utils.data 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_clone_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 % 4 x2 = xindex // 16 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1), xmask) tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_poi_fused_clone_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 192 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 % 16 x2 = xindex // 64 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 12 * x1), xmask) tmp1 = tl.load(in_ptr1 + (x0 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + x3, tmp2, xmask) @triton.jit def triton_poi_fused_mul_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 x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.5 tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = 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_mean_4(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 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 tmp9 = 1.0 tmp10 = tmp8 / tmp9 tl.store(out_ptr0 + x2, tmp8, xmask) tl.store(out_ptr1 + 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), (16, 4, 1)) assert_size_stride(primals_2, (12,), (1,)) assert_size_stride(primals_3, (12, 4), (4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clone_0[grid(64)](primals_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_1 buf1 = empty_strided_cuda((16, 12), (12, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0), reinterpret_tensor(primals_3, (4, 12), (1, 4), 0), out=buf1) del primals_3 buf2 = empty_strided_cuda((3, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_clone_1[grid(192)](buf1, primals_2, buf2, 192, XBLOCK=256, num_warps=4, num_stages=1) del buf1 del primals_2 buf3 = empty_strided_cuda((4, 4, 4), (4, 16, 1), torch.float32) triton_poi_fused_mul_2[grid(64)](buf2, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf3, reinterpret_tensor(buf2, (4, 4, 4), (4, 1, 16), 64), out=buf4) buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_3[grid(64)](buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf6 = buf4 del buf4 buf10 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_mean_4[grid(64)](buf5, buf6, buf10, 64, XBLOCK=64, num_warps=1, num_stages=1) buf7 = buf5 del buf5 extern_kernels.bmm(buf6, reinterpret_tensor(buf2, (4, 4, 4), (4, 16, 1), 128), out=buf7) buf8 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_clone_0[grid(64)](buf7, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1) buf9 = reinterpret_tensor(buf7, (16, 4), (4, 1), 0) del buf7 extern_kernels.addmm(primals_5, reinterpret_tensor(buf8, (16, 4), ( 4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf9) del primals_5 return reinterpret_tensor(buf9, (4, 4, 4), (4, 16, 1), 0 ), buf10, reinterpret_tensor(buf0, (16, 4), (4, 1), 0 ), buf6, reinterpret_tensor(buf8, (16, 4), (4, 1), 0 ), primals_4, reinterpret_tensor(buf2, (4, 4, 4), (4, 1, 16), 128 ), reinterpret_tensor(buf3, (4, 4, 4), (4, 1, 16), 0 ), reinterpret_tensor(buf2, (4, 4, 4), (4, 16, 1), 64) class TransposeMultiheadAttentionNew(nn.Module): """ Wrapper for nn.MultiheadAttention which first transposes the input tensor from (batch_size, seq_len, feature_dim) to (seq_length, batch_size, feature_dim), then applies the attention and transposes the attention outputs back to the input shape. """ def __init__(self, feature_dim: 'int', num_heads: 'int'=1): """ Args: feature_dim (int): attention embedding dimension num_heads (int): number of attention heads """ super().__init__() self._attention = nn.MultiheadAttention(embed_dim=feature_dim, num_heads=num_heads) self._attention_weights = None @property def attention_weights(self) ->Optional[torch.Tensor]: """ Contains attention weights from last forward call. """ return self._attention_weights def forward(self, input_0): primals_3 = self._attention.in_proj_weight primals_2 = self._attention.in_proj_bias primals_4 = self._attention.out_proj.weight primals_5 = self._attention.out_proj.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
kevinmtian/pytorchvideo
TransposeMultiheadAttention
false
15,826
[ "Apache-2.0" ]
2,391
168e16859a6029ef8ebeb476f9163bebb6c6b87d
https://github.com/kevinmtian/pytorchvideo/tree/168e16859a6029ef8ebeb476f9163bebb6c6b87d
ScalingBlock
import torch import torch.nn as nn class ScalingBlock(nn.Module): def __init__(self, temp=5.0, **kwargs): super(ScalingBlock, self).__init__() self.temp = temp def forward(self, x): x = x / self.temp return x def extra_repr(self): return 'temp=%.3e' % self.temp 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_div_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.2 tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x0, tmp2, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_div_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, class ScalingBlockNew(nn.Module): def __init__(self, temp=5.0, **kwargs): super(ScalingBlockNew, self).__init__() self.temp = temp def extra_repr(self): return 'temp=%.3e' % self.temp def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
kimfunn/spatial-smoothing
ScalingBlock
false
15,827
[ "Apache-2.0" ]
438
4f849d57c66c2dbdfaa56fc28727e95eddfd337c
https://github.com/kimfunn/spatial-smoothing/tree/4f849d57c66c2dbdfaa56fc28727e95eddfd337c
ResidualBlock
import math import torch import torch.nn as nn class ConvNorm(nn.Module): """ 1D Convolution """ def __init__(self, in_channels, out_channels, kernel_size=1, stride=1, padding=None, dilation=1, bias=True, w_init_gain='linear'): super(ConvNorm, self).__init__() if padding is None: assert kernel_size % 2 == 1 padding = int(dilation * (kernel_size - 1) / 2) self.conv = nn.Conv1d(in_channels, out_channels, kernel_size= kernel_size, stride=stride, padding=padding, dilation=dilation, bias=bias) nn.init.kaiming_normal_(self.conv.weight) def forward(self, signal): conv_signal = self.conv(signal) return conv_signal class LinearNorm(nn.Module): """ LinearNorm Projection """ def __init__(self, in_features, out_features, bias=False): super(LinearNorm, self).__init__() self.linear = nn.Linear(in_features, out_features, bias) nn.init.xavier_uniform_(self.linear.weight) if bias: nn.init.constant_(self.linear.bias, 0.0) def forward(self, x): x = self.linear(x) return x class ResidualBlock(nn.Module): """ Residual Block """ def __init__(self, d_encoder, residual_channels, dropout): super(ResidualBlock, self).__init__() self.conv_layer = ConvNorm(residual_channels, 2 * residual_channels, kernel_size=3, stride=1, padding=int((3 - 1) / 2), dilation=1) self.diffusion_projection = LinearNorm(residual_channels, residual_channels) self.conditioner_projection = ConvNorm(d_encoder, 2 * residual_channels, kernel_size=1) self.output_projection = ConvNorm(residual_channels, 2 * residual_channels, kernel_size=1) def forward(self, x, conditioner, diffusion_step, mask=None): diffusion_step = self.diffusion_projection(diffusion_step).unsqueeze(-1 ) conditioner = self.conditioner_projection(conditioner) y = x + diffusion_step y = self.conv_layer(y) + conditioner gate, filter = torch.chunk(y, 2, dim=1) y = torch.sigmoid(gate) * torch.tanh(filter) y = self.output_projection(y) residual, skip = torch.chunk(y, 2, dim=1) return (x + residual) / math.sqrt(2.0), skip def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'d_encoder': 4, 'residual_channels': 4, 'dropout': 0.5}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn 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_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 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') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + x5, tmp2, xmask) @triton.jit def triton_poi_fused_mul_sigmoid_tanh_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 // 16 x4 = xindex % 16 x1 = xindex // 4 % 4 x3 = xindex tmp0 = tl.load(in_ptr0 + (x4 + 32 * x2), xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (16 + x4 + 32 * x2), xmask) tmp9 = tl.load(in_ptr1 + (4 + x1), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr2 + (16 + x4), xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr3 + (4 + x1), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp7 = tl.sigmoid(tmp6) tmp10 = tmp8 + tmp9 tmp13 = tmp11 + tmp12 tmp14 = tmp10 + tmp13 tmp15 = libdevice.tanh(tmp14) tmp16 = tmp7 * tmp15 tl.store(out_ptr0 + x3, tmp7, xmask) tl.store(out_ptr1 + x3, tmp15, xmask) tl.store(out_ptr2 + x3, tmp16, xmask) @triton.jit def triton_poi_fused_convolution_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 4 % 8 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_add_div_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, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + (x0 + 32 * x1), xmask) tmp2 = tmp0 + tmp1 tmp3 = 0.7071067811865475 tmp4 = tmp2 * tmp3 tl.store(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) = 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, (8, 4, 1), (4, 1, 1)) assert_size_stride(primals_4, (8,), (1,)) assert_size_stride(primals_5, (4, 4), (4, 1)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (8, 4, 3), (12, 3, 1)) assert_size_stride(primals_8, (8,), (1,)) assert_size_stride(primals_9, (8, 4, 1), (4, 1, 1)) assert_size_stride(primals_10, (8,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_2, reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) del primals_1 buf1 = extern_kernels.convolution(reinterpret_tensor(primals_5, (1, 4, 4), (16, 4, 1), 0), primals_3, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf1, (1, 8, 4), (32, 4, 1)) buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_0[grid(64)](primals_6, buf0, buf2, 64, XBLOCK= 64, num_warps=1, num_stages=1) del buf0 buf3 = extern_kernels.convolution(buf2, primals_7, stride=(1,), padding=(1,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf3, (4, 8, 4), (32, 4, 1)) buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) buf6 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_mul_sigmoid_tanh_1[grid(64)](buf3, primals_8, buf1, primals_4, buf4, buf5, buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf1 del buf3 del primals_4 del primals_8 buf7 = extern_kernels.convolution(buf6, primals_9, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf7, (4, 8, 4), (32, 4, 1)) buf8 = buf7 del buf7 triton_poi_fused_convolution_2[grid(128)](buf8, primals_10, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_10 buf9 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_add_div_3[grid(64)](primals_6, buf8, buf9, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_6 return buf9, reinterpret_tensor(buf8, (4, 4, 4), (32, 4, 1), 16 ), primals_2, primals_3, primals_7, primals_9, reinterpret_tensor( primals_5, (1, 4, 4), (16, 4, 1), 0), buf2, buf4, buf5, buf6 class ConvNorm(nn.Module): """ 1D Convolution """ def __init__(self, in_channels, out_channels, kernel_size=1, stride=1, padding=None, dilation=1, bias=True, w_init_gain='linear'): super(ConvNorm, self).__init__() if padding is None: assert kernel_size % 2 == 1 padding = int(dilation * (kernel_size - 1) / 2) self.conv = nn.Conv1d(in_channels, out_channels, kernel_size= kernel_size, stride=stride, padding=padding, dilation=dilation, bias=bias) nn.init.kaiming_normal_(self.conv.weight) def forward(self, signal): conv_signal = self.conv(signal) return conv_signal class LinearNorm(nn.Module): """ LinearNorm Projection """ def __init__(self, in_features, out_features, bias=False): super(LinearNorm, self).__init__() self.linear = nn.Linear(in_features, out_features, bias) nn.init.xavier_uniform_(self.linear.weight) if bias: nn.init.constant_(self.linear.bias, 0.0) def forward(self, x): x = self.linear(x) return x class ResidualBlockNew(nn.Module): """ Residual Block """ def __init__(self, d_encoder, residual_channels, dropout): super(ResidualBlockNew, self).__init__() self.conv_layer = ConvNorm(residual_channels, 2 * residual_channels, kernel_size=3, stride=1, padding=int((3 - 1) / 2), dilation=1) self.diffusion_projection = LinearNorm(residual_channels, residual_channels) self.conditioner_projection = ConvNorm(d_encoder, 2 * residual_channels, kernel_size=1) self.output_projection = ConvNorm(residual_channels, 2 * residual_channels, kernel_size=1) def forward(self, input_0, input_1, input_2): primals_7 = self.conv_layer.conv.weight primals_4 = self.conv_layer.conv.bias primals_1 = self.diffusion_projection.linear.weight primals_3 = self.conditioner_projection.conv.weight primals_8 = self.conditioner_projection.conv.bias primals_9 = self.output_projection.conv.weight primals_10 = self.output_projection.conv.bias primals_2 = input_0 primals_5 = input_1 primals_6 = input_2 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10]) return output[0], output[1]
keonlee9420/DiffSinger
ResidualBlock
false
15,828
[ "MIT" ]
95
2bfcae4a78068c2061eae64ee675959a077aa54b
https://github.com/keonlee9420/DiffSinger/tree/2bfcae4a78068c2061eae64ee675959a077aa54b
FocalLoss
import torch import torch.nn as nn import torch.nn.functional as F def reduce_loss(loss, reduction): """Reduce loss as specified. Args: loss (Tensor): Elementwise loss tensor. reduction (str): Options are "none", "mean" and "sum". Return: Tensor: Reduced loss tensor. """ reduction_enum = F._Reduction.get_enum(reduction) if reduction_enum == 0: return loss elif reduction_enum == 1: return loss.mean() elif reduction_enum == 2: return loss.sum() def weight_reduce_loss(loss, weight=None, reduction='mean', avg_factor=None): """Apply element-wise weight and reduce loss. Args: loss (Tensor): Element-wise loss. weight (Tensor): Element-wise weights. reduction (str): Same as built-in losses of PyTorch. avg_factor (float): Average factor when computing the mean of losses. Returns: Tensor: Processed loss values. """ if weight is not None: loss = loss * weight if avg_factor is None: loss = reduce_loss(loss, reduction) elif reduction == 'mean': loss = loss.sum() / avg_factor elif reduction != 'none': raise ValueError('avg_factor can not be used with reduction="sum"') return loss def sigmoid_focal_loss(pred, target, weight=None, gamma=2.0, alpha=0.25, reduction='mean', avg_factor=None): """Sigmoid focal loss. Args: pred (torch.Tensor): The prediction with shape (N, \\*). target (torch.Tensor): The ground truth label of the prediction with shape (N, \\*). weight (torch.Tensor, optional): Sample-wise loss weight with shape (N, ). Defaults to None. gamma (float): The gamma for calculating the modulating factor. Defaults to 2.0. alpha (float): A balanced form for Focal Loss. Defaults to 0.25. reduction (str): The method used to reduce the loss. Options are "none", "mean" and "sum". If reduction is 'none' , loss is same shape as pred and label. Defaults to 'mean'. avg_factor (int, optional): Average factor that is used to average the loss. Defaults to None. Returns: torch.Tensor: Loss. """ assert pred.shape == target.shape, 'pred and target should be in the same shape.' pred_sigmoid = pred.sigmoid() target = target.type_as(pred) pt = (1 - pred_sigmoid) * target + pred_sigmoid * (1 - target) focal_weight = (alpha * target + (1 - alpha) * (1 - target)) * pt.pow(gamma ) loss = F.binary_cross_entropy_with_logits(pred, target, reduction='none' ) * focal_weight if weight is not None: assert weight.dim() == 1 weight = weight.float() if pred.dim() > 1: weight = weight.reshape(-1, 1) loss = weight_reduce_loss(loss, weight, reduction, avg_factor) return loss class FocalLoss(nn.Module): """Focal loss. Args: gamma (float): Focusing parameter in focal loss. Defaults to 2.0. alpha (float): The parameter in balanced form of focal loss. Defaults to 0.25. reduction (str): The method used to reduce the loss into a scalar. Options are "none" and "mean". Defaults to 'mean'. loss_weight (float): Weight of loss. Defaults to 1.0. """ def __init__(self, gamma=2.0, alpha=0.25, reduction='mean', loss_weight=1.0 ): super(FocalLoss, self).__init__() self.gamma = gamma self.alpha = alpha self.reduction = reduction self.loss_weight = loss_weight def forward(self, pred, target, weight=None, avg_factor=None, reduction_override=None): """Sigmoid focal loss. Args: pred (torch.Tensor): The prediction with shape (N, \\*). target (torch.Tensor): The ground truth label of the prediction with shape (N, \\*). weight (torch.Tensor, optional): Sample-wise loss weight with shape (N, \\*). Defaults to None. avg_factor (int, optional): Average factor that is used to average the loss. Defaults to None. reduction_override (str, optional): The method used to reduce the loss into a scalar. Options are "none", "mean" and "sum". Defaults to None. Returns: torch.Tensor: Loss. """ assert reduction_override in (None, 'none', 'mean', 'sum') reduction = (reduction_override if reduction_override else self. reduction) loss_cls = self.loss_weight * sigmoid_focal_loss(pred, target, weight, gamma=self.gamma, alpha=self.alpha, reduction=reduction, avg_factor=avg_factor) return loss_cls def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn import torch.nn.functional as F assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_binary_cross_entropy_with_logits_mean_mul_pow_rsub_sigmoid_0( in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp3 = tl.load(in_ptr1 + r0, None) tmp1 = 1.0 tmp2 = tmp1 - tmp0 tmp4 = tmp2 * tmp3 tmp5 = 0.0 tmp6 = triton_helpers.minimum(tmp5, tmp3) tmp7 = tl_math.abs(tmp3) tmp8 = -tmp7 tmp9 = tl_math.exp(tmp8) tmp10 = libdevice.log1p(tmp9) tmp11 = tmp6 - tmp10 tmp12 = tmp4 - tmp11 tmp13 = 0.25 tmp14 = tmp0 * tmp13 tmp15 = 0.75 tmp16 = tmp2 * tmp15 tmp17 = tmp14 + tmp16 tmp18 = tl.sigmoid(tmp3) tmp19 = tmp1 - tmp18 tmp20 = tmp19 * tmp0 tmp21 = tmp18 * tmp2 tmp22 = tmp20 + tmp21 tmp23 = tmp22 * tmp22 tmp24 = tmp17 * tmp23 tmp25 = tmp12 * tmp24 tmp26 = tl.broadcast_to(tmp25, [RBLOCK]) tmp28 = triton_helpers.promote_to_tensor(tl.sum(tmp26, 0)) tmp29 = 256.0 tmp30 = tmp28 / tmp29 tmp31 = tmp30 * tmp1 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp31, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_binary_cross_entropy_with_logits_mean_mul_pow_rsub_sigmoid_0[ grid(1)](buf1, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, def reduce_loss(loss, reduction): """Reduce loss as specified. Args: loss (Tensor): Elementwise loss tensor. reduction (str): Options are "none", "mean" and "sum". Return: Tensor: Reduced loss tensor. """ reduction_enum = F._Reduction.get_enum(reduction) if reduction_enum == 0: return loss elif reduction_enum == 1: return loss.mean() elif reduction_enum == 2: return loss.sum() def weight_reduce_loss(loss, weight=None, reduction='mean', avg_factor=None): """Apply element-wise weight and reduce loss. Args: loss (Tensor): Element-wise loss. weight (Tensor): Element-wise weights. reduction (str): Same as built-in losses of PyTorch. avg_factor (float): Average factor when computing the mean of losses. Returns: Tensor: Processed loss values. """ if weight is not None: loss = loss * weight if avg_factor is None: loss = reduce_loss(loss, reduction) elif reduction == 'mean': loss = loss.sum() / avg_factor elif reduction != 'none': raise ValueError('avg_factor can not be used with reduction="sum"') return loss def sigmoid_focal_loss(pred, target, weight=None, gamma=2.0, alpha=0.25, reduction='mean', avg_factor=None): """Sigmoid focal loss. Args: pred (torch.Tensor): The prediction with shape (N, \\*). target (torch.Tensor): The ground truth label of the prediction with shape (N, \\*). weight (torch.Tensor, optional): Sample-wise loss weight with shape (N, ). Defaults to None. gamma (float): The gamma for calculating the modulating factor. Defaults to 2.0. alpha (float): A balanced form for Focal Loss. Defaults to 0.25. reduction (str): The method used to reduce the loss. Options are "none", "mean" and "sum". If reduction is 'none' , loss is same shape as pred and label. Defaults to 'mean'. avg_factor (int, optional): Average factor that is used to average the loss. Defaults to None. Returns: torch.Tensor: Loss. """ assert pred.shape == target.shape, 'pred and target should be in the same shape.' pred_sigmoid = pred.sigmoid() target = target.type_as(pred) pt = (1 - pred_sigmoid) * target + pred_sigmoid * (1 - target) focal_weight = (alpha * target + (1 - alpha) * (1 - target)) * pt.pow(gamma ) loss = F.binary_cross_entropy_with_logits(pred, target, reduction='none' ) * focal_weight if weight is not None: assert weight.dim() == 1 weight = weight.float() if pred.dim() > 1: weight = weight.reshape(-1, 1) loss = weight_reduce_loss(loss, weight, reduction, avg_factor) return loss class FocalLossNew(nn.Module): """Focal loss. Args: gamma (float): Focusing parameter in focal loss. Defaults to 2.0. alpha (float): The parameter in balanced form of focal loss. Defaults to 0.25. reduction (str): The method used to reduce the loss into a scalar. Options are "none" and "mean". Defaults to 'mean'. loss_weight (float): Weight of loss. Defaults to 1.0. """ def __init__(self, gamma=2.0, alpha=0.25, reduction='mean', loss_weight=1.0 ): super(FocalLossNew, self).__init__() self.gamma = gamma self.alpha = alpha self.reduction = reduction self.loss_weight = loss_weight def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kivanctezoren/mmclassification
FocalLoss
false
15,829
[ "Apache-2.0" ]
1,190
5c73d4b29f61c47d379bbec4621a465099e64bd7
https://github.com/kivanctezoren/mmclassification/tree/5c73d4b29f61c47d379bbec4621a465099e64bd7
AsymmetricLoss
import torch import torch.nn as nn import torch.nn.functional as F def reduce_loss(loss, reduction): """Reduce loss as specified. Args: loss (Tensor): Elementwise loss tensor. reduction (str): Options are "none", "mean" and "sum". Return: Tensor: Reduced loss tensor. """ reduction_enum = F._Reduction.get_enum(reduction) if reduction_enum == 0: return loss elif reduction_enum == 1: return loss.mean() elif reduction_enum == 2: return loss.sum() def weight_reduce_loss(loss, weight=None, reduction='mean', avg_factor=None): """Apply element-wise weight and reduce loss. Args: loss (Tensor): Element-wise loss. weight (Tensor): Element-wise weights. reduction (str): Same as built-in losses of PyTorch. avg_factor (float): Average factor when computing the mean of losses. Returns: Tensor: Processed loss values. """ if weight is not None: loss = loss * weight if avg_factor is None: loss = reduce_loss(loss, reduction) elif reduction == 'mean': loss = loss.sum() / avg_factor elif reduction != 'none': raise ValueError('avg_factor can not be used with reduction="sum"') return loss def asymmetric_loss(pred, target, weight=None, gamma_pos=1.0, gamma_neg=4.0, clip=0.05, reduction='mean', avg_factor=None): """asymmetric loss. Please refer to the `paper <https://arxiv.org/abs/2009.14119>`__ for details. Args: pred (torch.Tensor): The prediction with shape (N, \\*). target (torch.Tensor): The ground truth label of the prediction with shape (N, \\*). weight (torch.Tensor, optional): Sample-wise loss weight with shape (N, ). Defaults to None. gamma_pos (float): positive focusing parameter. Defaults to 0.0. gamma_neg (float): Negative focusing parameter. We usually set gamma_neg > gamma_pos. Defaults to 4.0. clip (float, optional): Probability margin. Defaults to 0.05. reduction (str): The method used to reduce the loss. Options are "none", "mean" and "sum". If reduction is 'none' , loss is same shape as pred and label. Defaults to 'mean'. avg_factor (int, optional): Average factor that is used to average the loss. Defaults to None. Returns: torch.Tensor: Loss. """ assert pred.shape == target.shape, 'pred and target should be in the same shape.' eps = 1e-08 pred_sigmoid = pred.sigmoid() target = target.type_as(pred) if clip and clip > 0: pt = (1 - pred_sigmoid + clip).clamp(max=1) * (1 - target ) + pred_sigmoid * target else: pt = (1 - pred_sigmoid) * (1 - target) + pred_sigmoid * target asymmetric_weight = (1 - pt).pow(gamma_pos * target + gamma_neg * (1 - target)) loss = -torch.log(pt.clamp(min=eps)) * asymmetric_weight if weight is not None: assert weight.dim() == 1 weight = weight.float() if pred.dim() > 1: weight = weight.reshape(-1, 1) loss = weight_reduce_loss(loss, weight, reduction, avg_factor) return loss class AsymmetricLoss(nn.Module): """asymmetric loss. Args: gamma_pos (float): positive focusing parameter. Defaults to 0.0. gamma_neg (float): Negative focusing parameter. We usually set gamma_neg > gamma_pos. Defaults to 4.0. clip (float, optional): Probability margin. Defaults to 0.05. reduction (str): The method used to reduce the loss into a scalar. loss_weight (float): Weight of loss. Defaults to 1.0. """ def __init__(self, gamma_pos=0.0, gamma_neg=4.0, clip=0.05, reduction= 'mean', loss_weight=1.0): super(AsymmetricLoss, self).__init__() self.gamma_pos = gamma_pos self.gamma_neg = gamma_neg self.clip = clip self.reduction = reduction self.loss_weight = loss_weight def forward(self, pred, target, weight=None, avg_factor=None, reduction_override=None): """asymmetric loss.""" assert reduction_override in (None, 'none', 'mean', 'sum') reduction = (reduction_override if reduction_override else self. reduction) loss_cls = self.loss_weight * asymmetric_loss(pred, target, weight, gamma_pos=self.gamma_pos, gamma_neg=self.gamma_neg, clip=self. clip, reduction=reduction, avg_factor=avg_factor) return loss_cls def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn import torch.nn.functional as F assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_clamp_log_mean_mul_neg_pow_rsub_sigmoid_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp7 = tl.load(in_ptr1 + r0, None) tmp1 = tl.sigmoid(tmp0) tmp2 = 1.0 tmp3 = tmp2 - tmp1 tmp4 = 0.05 tmp5 = tmp3 + tmp4 tmp6 = triton_helpers.minimum(tmp5, tmp2) tmp8 = tmp2 - tmp7 tmp9 = tmp6 * tmp8 tmp10 = tmp1 * tmp7 tmp11 = tmp9 + tmp10 tmp12 = 1e-08 tmp13 = triton_helpers.maximum(tmp11, tmp12) tmp14 = tl_math.log(tmp13) tmp15 = -tmp14 tmp16 = tmp2 - tmp11 tmp17 = 0.0 tmp18 = tmp7 * tmp17 tmp19 = 4.0 tmp20 = tmp8 * tmp19 tmp21 = tmp18 + tmp20 tmp22 = libdevice.pow(tmp16, tmp21) tmp23 = tmp15 * tmp22 tmp24 = tl.broadcast_to(tmp23, [RBLOCK]) tmp26 = triton_helpers.promote_to_tensor(tl.sum(tmp24, 0)) tmp27 = 256.0 tmp28 = tmp26 / tmp27 tmp29 = tmp28 * tmp2 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp29, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_clamp_log_mean_mul_neg_pow_rsub_sigmoid_0[grid(1) ](buf1, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, def reduce_loss(loss, reduction): """Reduce loss as specified. Args: loss (Tensor): Elementwise loss tensor. reduction (str): Options are "none", "mean" and "sum". Return: Tensor: Reduced loss tensor. """ reduction_enum = F._Reduction.get_enum(reduction) if reduction_enum == 0: return loss elif reduction_enum == 1: return loss.mean() elif reduction_enum == 2: return loss.sum() def weight_reduce_loss(loss, weight=None, reduction='mean', avg_factor=None): """Apply element-wise weight and reduce loss. Args: loss (Tensor): Element-wise loss. weight (Tensor): Element-wise weights. reduction (str): Same as built-in losses of PyTorch. avg_factor (float): Average factor when computing the mean of losses. Returns: Tensor: Processed loss values. """ if weight is not None: loss = loss * weight if avg_factor is None: loss = reduce_loss(loss, reduction) elif reduction == 'mean': loss = loss.sum() / avg_factor elif reduction != 'none': raise ValueError('avg_factor can not be used with reduction="sum"') return loss def asymmetric_loss(pred, target, weight=None, gamma_pos=1.0, gamma_neg=4.0, clip=0.05, reduction='mean', avg_factor=None): """asymmetric loss. Please refer to the `paper <https://arxiv.org/abs/2009.14119>`__ for details. Args: pred (torch.Tensor): The prediction with shape (N, \\*). target (torch.Tensor): The ground truth label of the prediction with shape (N, \\*). weight (torch.Tensor, optional): Sample-wise loss weight with shape (N, ). Defaults to None. gamma_pos (float): positive focusing parameter. Defaults to 0.0. gamma_neg (float): Negative focusing parameter. We usually set gamma_neg > gamma_pos. Defaults to 4.0. clip (float, optional): Probability margin. Defaults to 0.05. reduction (str): The method used to reduce the loss. Options are "none", "mean" and "sum". If reduction is 'none' , loss is same shape as pred and label. Defaults to 'mean'. avg_factor (int, optional): Average factor that is used to average the loss. Defaults to None. Returns: torch.Tensor: Loss. """ assert pred.shape == target.shape, 'pred and target should be in the same shape.' eps = 1e-08 pred_sigmoid = pred.sigmoid() target = target.type_as(pred) if clip and clip > 0: pt = (1 - pred_sigmoid + clip).clamp(max=1) * (1 - target ) + pred_sigmoid * target else: pt = (1 - pred_sigmoid) * (1 - target) + pred_sigmoid * target asymmetric_weight = (1 - pt).pow(gamma_pos * target + gamma_neg * (1 - target)) loss = -torch.log(pt.clamp(min=eps)) * asymmetric_weight if weight is not None: assert weight.dim() == 1 weight = weight.float() if pred.dim() > 1: weight = weight.reshape(-1, 1) loss = weight_reduce_loss(loss, weight, reduction, avg_factor) return loss class AsymmetricLossNew(nn.Module): """asymmetric loss. Args: gamma_pos (float): positive focusing parameter. Defaults to 0.0. gamma_neg (float): Negative focusing parameter. We usually set gamma_neg > gamma_pos. Defaults to 4.0. clip (float, optional): Probability margin. Defaults to 0.05. reduction (str): The method used to reduce the loss into a scalar. loss_weight (float): Weight of loss. Defaults to 1.0. """ def __init__(self, gamma_pos=0.0, gamma_neg=4.0, clip=0.05, reduction= 'mean', loss_weight=1.0): super(AsymmetricLossNew, self).__init__() self.gamma_pos = gamma_pos self.gamma_neg = gamma_neg self.clip = clip self.reduction = reduction self.loss_weight = loss_weight def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kivanctezoren/mmclassification
AsymmetricLoss
false
15,830
[ "Apache-2.0" ]
1,190
5c73d4b29f61c47d379bbec4621a465099e64bd7
https://github.com/kivanctezoren/mmclassification/tree/5c73d4b29f61c47d379bbec4621a465099e64bd7
TransformerEncoderLayerWithConv1d
import torch import torch.nn as nn import torch.nn.functional as F class TransformerEncoderLayerWithConv1d(nn.Module): """ Input and output shape: seqlen x batch_size x dim """ def __init__(self, dim_model, nheads, dim_feedforward, dropout, kernel_size, stride): super(TransformerEncoderLayerWithConv1d, self).__init__() self.encoder_layer = nn.TransformerEncoderLayer(dim_model, nheads, dim_feedforward, dropout) self.conv1d = nn.Conv1d(dim_model, dim_model, kernel_size, stride= stride, padding=1) def forward(self, src, src_mask=None, src_key_padding_mask=None): output = self.encoder_layer(src, src_mask, src_key_padding_mask) output = F.relu(self.conv1d(output.permute(1, 2, 0))) return output.permute(2, 0, 1) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'dim_model': 4, 'nheads': 4, 'dim_feedforward': 4, 'dropout': 0.5, '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 from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_mul_transpose_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 12 * y1 + 48 * x2), 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) tl.store(out_ptr1 + (y3 + 16 * x2), tmp4, xmask & ymask) @triton.jit def triton_poi_fused_mul_transpose_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (4 + y0 + 12 * y1 + 48 * x2), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + (4 + 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) tl.store(out_ptr1 + (y3 + 16 * x2), tmp4, xmask & ymask) @triton.jit def triton_poi_fused__safe_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__safe_softmax_3(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp18 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp25 = tl.load(in_ptr1 + x2, xmask) tmp26 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last') tmp27 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp29 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp31 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp1 = float('-inf') tmp2 = tmp0 == tmp1 tmp3 = tmp2 == 0 tmp4 = tmp3.to(tl.int64) tmp5 = tmp4 != 0 tmp7 = tmp6 == tmp1 tmp8 = tmp7 == 0 tmp9 = tmp8.to(tl.int64) tmp10 = tmp9 != 0 tmp11 = tmp5 | tmp10 tmp13 = tmp12 == tmp1 tmp14 = tmp13 == 0 tmp15 = tmp14.to(tl.int64) tmp16 = tmp15 != 0 tmp17 = tmp11 | tmp16 tmp19 = tmp18 == tmp1 tmp20 = tmp19 == 0 tmp21 = tmp20.to(tl.int64) tmp22 = tmp21 != 0 tmp23 = tmp17 | tmp22 tmp24 = tmp23 == 0 tmp28 = tmp26 + tmp27 tmp30 = tmp28 + tmp29 tmp32 = tmp30 + tmp31 tmp33 = tmp25 / tmp32 tmp34 = 0.0 tmp35 = tl.where(tmp24, tmp34, tmp33) tl.store(out_ptr0 + x2, tmp35, xmask) @triton.jit def triton_poi_fused_clone_4(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 192 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 % 16 x2 = xindex // 64 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 12 * x1), xmask) tmp1 = tl.load(in_ptr1 + (x0 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + x3, tmp2, xmask) @triton.jit def triton_poi_fused_bmm_transpose_5(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 16 x1 = xindex // 16 x2 = xindex tmp0 = tl.load(in_ptr0 + (128 + x0 + 4 * (x0 % 4 // 4) + 16 * x1), xmask) tl.store(out_ptr0 + x2, tmp0, xmask) tl.store(out_ptr1 + x2, tmp0, xmask) @triton.jit def triton_poi_fused_clone_6(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_add_native_layer_norm_7(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_native_layer_norm_8(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 x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x2, xmask) tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 - tmp3 tmp6 = 1e-05 tmp7 = tmp5 + tmp6 tmp8 = libdevice.rsqrt(tmp7) tmp9 = tmp4 * tmp8 tmp11 = tmp9 * tmp10 tmp13 = tmp11 + tmp12 tl.store(out_ptr0 + x2, tmp13, xmask) @triton.jit def triton_poi_fused_relu_threshold_backward_9(in_out_ptr0, 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') 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_10(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 x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_out_ptr0 + x2, xmask) tmp2 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp4 = tmp0 + tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_native_layer_norm_11(in_ptr0, 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_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tmp9 = tmp0 - tmp8 tmp10 = tmp9 * tmp9 tmp11 = tmp1 - tmp8 tmp12 = tmp11 * tmp11 tmp13 = tmp10 + tmp12 tmp14 = tmp3 - tmp8 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp8 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp19 / tmp7 tmp21 = 1e-05 tmp22 = tmp20 + tmp21 tmp23 = libdevice.rsqrt(tmp22) tl.store(out_ptr0 + x0, tmp8, xmask) tl.store(out_ptr1 + x0, tmp23, xmask) @triton.jit def triton_poi_fused_native_layer_norm_12(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 x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_convolution_13(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 x1 = xindex y0 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 16 * x1), xmask & ymask, eviction_policy ='evict_last') tl.store(out_ptr0 + (x1 + 4 * y0), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_14(in_out_ptr0, in_ptr0, 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_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) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x3, tmp4, xmask) tl.store(out_ptr0 + x3, tmp6, 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, primals_14, primals_15) = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (12,), (1,)) assert_size_stride(primals_3, (12, 4), (4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (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,)) assert_size_stride(primals_10, (4, 4), (4, 1)) assert_size_stride(primals_11, (4,), (1,)) assert_size_stride(primals_12, (4,), (1,)) assert_size_stride(primals_13, (4,), (1,)) assert_size_stride(primals_14, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_15, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 12), (12, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_3, (4, 12), (1, 4), 0), out=buf0) del primals_3 buf1 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) buf27 = empty_strided_cuda((16, 1, 4), (1, 1, 16), torch.float32) get_raw_stream(0) triton_poi_fused_mul_transpose_0[grid(16, 4)](buf0, primals_2, buf1, buf27, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4, 1, 4), (16, 4, 4, 1), torch.float32) buf28 = empty_strided_cuda((16, 4, 1), (1, 16, 1), torch.float32) triton_poi_fused_mul_transpose_1[grid(16, 4)](buf0, primals_2, buf2, buf28, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) buf3 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf1, (16, 4, 1), (4, 1, 0), 0), reinterpret_tensor(buf2, (16, 1, 4), (4, 0, 1), 0), out=buf3) buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__safe_softmax_2[grid(256)](buf3, buf4, 256, XBLOCK =256, num_warps=4, num_stages=1) buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__safe_softmax_3[grid(256)](buf3, buf4, buf5, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf3 del buf4 buf6 = empty_strided_cuda((3, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_clone_4[grid(192)](buf0, primals_2, buf6, 192, XBLOCK=256, num_warps=4, num_stages=1) del buf0 del primals_2 buf7 = reinterpret_tensor(buf2, (16, 4, 1), (1, 16, 64), 0) del buf2 buf26 = reinterpret_tensor(buf1, (16, 1, 4), (1, 1, 16), 0) del buf1 triton_poi_fused_bmm_transpose_5[grid(64)](buf6, buf7, buf26, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf6 buf8 = empty_strided_cuda((16, 4, 1), (4, 1, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf5, (16, 4, 4), (16, 4, 1), 0), buf7, out=buf8) buf9 = reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf7 triton_poi_fused_clone_6[grid(4, 16)](buf8, buf9, 4, 16, XBLOCK=16, YBLOCK=4, num_warps=1, num_stages=1) buf10 = reinterpret_tensor(buf8, (16, 4), (4, 1), 0) del buf8 extern_kernels.addmm(primals_5, reinterpret_tensor(buf9, (16, 4), ( 4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf10) del primals_5 buf11 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf12 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) triton_poi_fused_add_native_layer_norm_7[grid(16)](primals_1, buf10, buf11, buf12, 16, XBLOCK=16, num_warps=1, num_stages=1) buf13 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_add_native_layer_norm_8[grid(64)](primals_1, buf10, buf11, buf12, primals_6, primals_7, buf13, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_7 buf14 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf13, (16, 4), (4, 1), 0), reinterpret_tensor(primals_8, (4, 4), (1, 4), 0), out=buf14) buf15 = reinterpret_tensor(buf14, (4, 4, 4), (16, 4, 1), 0) del buf14 buf25 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool) triton_poi_fused_relu_threshold_backward_9[grid(64)](buf15, primals_9, buf25, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_9 buf16 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf15, (16, 4), (4, 1), 0), reinterpret_tensor(primals_10, (4, 4), (1, 4), 0), out=buf16) buf17 = reinterpret_tensor(buf16, (4, 4, 4), (16, 4, 1), 0) del buf16 triton_poi_fused_add_10[grid(64)](buf17, buf13, primals_11, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_11 buf18 = buf12 del buf12 buf19 = buf11 del buf11 triton_poi_fused_native_layer_norm_11[grid(16)](buf17, buf18, buf19, 16, XBLOCK=16, num_warps=1, num_stages=1) buf20 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_12[grid(64)](buf17, buf18, buf19, primals_12, primals_13, buf20, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf18 del buf19 del primals_13 buf21 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_convolution_13[grid(16, 4)](buf20, buf21, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf22 = extern_kernels.convolution(buf21, primals_14, stride=(1,), padding=(1,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf22, (4, 4, 3), (12, 3, 1)) del buf21 buf23 = buf22 del buf22 buf24 = empty_strided_cuda((4, 4, 3), (12, 3, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_14[grid(48)](buf23 , primals_15, buf24, 48, XBLOCK=64, num_warps=1, num_stages=1) del primals_15 return reinterpret_tensor(buf23, (3, 4, 4), (1, 12, 3), 0 ), primals_1, primals_6, primals_12, primals_14, buf5, reinterpret_tensor( buf9, (16, 4), (4, 1), 0), buf10, reinterpret_tensor(buf13, (16, 4), (4, 1), 0), reinterpret_tensor(buf15, (16, 4), (4, 1), 0 ), buf17, reinterpret_tensor(buf20, (4, 4, 4), (4, 1, 16), 0 ), buf24, primals_10, buf25, primals_8, primals_4, buf26, buf27, buf28 class TransformerEncoderLayerWithConv1dNew(nn.Module): """ Input and output shape: seqlen x batch_size x dim """ def __init__(self, dim_model, nheads, dim_feedforward, dropout, kernel_size, stride): super(TransformerEncoderLayerWithConv1dNew, self).__init__() self.encoder_layer = nn.TransformerEncoderLayer(dim_model, nheads, dim_feedforward, dropout) self.conv1d = nn.Conv1d(dim_model, dim_model, kernel_size, stride= stride, padding=1) def forward(self, input_0): primals_3 = self.encoder_layer.self_attn.in_proj_weight primals_2 = self.encoder_layer.self_attn.in_proj_bias primals_4 = self.encoder_layer.self_attn.out_proj.weight primals_5 = self.encoder_layer.self_attn.out_proj.bias primals_8 = self.encoder_layer.linear1.weight primals_6 = self.encoder_layer.linear1.bias primals_10 = self.encoder_layer.linear2.weight primals_7 = self.encoder_layer.linear2.bias primals_9 = self.encoder_layer.norm1.weight primals_11 = self.encoder_layer.norm1.bias primals_12 = self.encoder_layer.norm2.weight primals_13 = self.encoder_layer.norm2.bias primals_1 = self.conv1d.weight primals_15 = self.conv1d.bias primals_14 = 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]
jzlianglu/pykaldi2
TransformerEncoderLayerWithConv1d
false
15,831
[ "MIT" ]
179
4d31968f8dff7cccf6a8395b7e69005ae3b2b30a
https://github.com/jzlianglu/pykaldi2/tree/4d31968f8dff7cccf6a8395b7e69005ae3b2b30a
DeterministicSumming
import torch import torch.nn as nn class DeterministicSumming(nn.Module): """Transform a tensor into repetitions of its sum. Intended for use in tests, not useful for actual learning. The last dimension of the input should contain feature vectors. The result will be an array of matching shape with the last dimension replaced by repeated utility values (i.e. sums). Let's use this as a pairwise utility function. As an example, consider this pairing. There are two instances with two objects each. All object combinations are considered. Objects have two features. >>> import torch >>> pairs = torch.tensor( ... [[[0.5000, 0.6000, 0.5000, 0.6000], ... [0.5000, 0.6000, 1.5000, 1.6000], ... [1.5000, 1.6000, 0.5000, 0.6000], ... [1.5000, 1.6000, 1.5000, 1.6000]], ... [[2.5000, 2.6000, 2.5000, 2.6000], ... [2.5000, 2.6000, 3.5000, 3.6000], ... [3.5000, 3.6000, 2.5000, 2.6000], ... [3.5000, 3.6000, 3.5000, 3.6000]]]) We can compute the mock utility of this pairing as follows: >>> utility = DeterministicSumming(input_size=2) >>> utilities = utility(pairs) >>> utilities tensor([[[ 2.2000], [ 4.2000], [ 4.2000], [ 6.2000]], <BLANKLINE> [[10.2000], [12.2000], [12.2000], [14.2000]]]) Note that for example :math:`2.2 = 0.5 + 0.6 + 0.5 + 0.6`, that is >>> utilities[0][0] == pairs[0][0].sum() tensor([True]) Parameters ---------- input_size : int The size of the last dimension of the input. output_size : int The size of the last dimension of the output. Defaults to `1` to make it more convenient to use this as a utility. """ def __init__(self, input_size: 'int', output_size: 'int'=1): super().__init__() self.output_size = output_size def forward(self, inputs): """Forward inputs through the network. Parameters ---------- inputs : tensor The input tensor of shape (N, *, I), where I is the input size. Returns ------- tensor A tensor of shape (N, *, O), where O is the output size. """ summed = inputs.sum(dim=-1) repeated = summed.view(-1, 1).repeat(1, self.output_size).view( summed.shape + (self.output_size,)) return repeated def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn 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_sum_view_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_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 tl.store(in_out_ptr0 + x0, tmp6, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) buf1 = reinterpret_tensor(buf0, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf0 get_raw_stream(0) triton_poi_fused_sum_view_0[grid(64)](buf1, arg0_1, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 return buf1, class DeterministicSummingNew(nn.Module): """Transform a tensor into repetitions of its sum. Intended for use in tests, not useful for actual learning. The last dimension of the input should contain feature vectors. The result will be an array of matching shape with the last dimension replaced by repeated utility values (i.e. sums). Let's use this as a pairwise utility function. As an example, consider this pairing. There are two instances with two objects each. All object combinations are considered. Objects have two features. >>> import torch >>> pairs = torch.tensor( ... [[[0.5000, 0.6000, 0.5000, 0.6000], ... [0.5000, 0.6000, 1.5000, 1.6000], ... [1.5000, 1.6000, 0.5000, 0.6000], ... [1.5000, 1.6000, 1.5000, 1.6000]], ... [[2.5000, 2.6000, 2.5000, 2.6000], ... [2.5000, 2.6000, 3.5000, 3.6000], ... [3.5000, 3.6000, 2.5000, 2.6000], ... [3.5000, 3.6000, 3.5000, 3.6000]]]) We can compute the mock utility of this pairing as follows: >>> utility = DeterministicSumming(input_size=2) >>> utilities = utility(pairs) >>> utilities tensor([[[ 2.2000], [ 4.2000], [ 4.2000], [ 6.2000]], <BLANKLINE> [[10.2000], [12.2000], [12.2000], [14.2000]]]) Note that for example :math:`2.2 = 0.5 + 0.6 + 0.5 + 0.6`, that is >>> utilities[0][0] == pairs[0][0].sum() tensor([True]) Parameters ---------- input_size : int The size of the last dimension of the input. output_size : int The size of the last dimension of the output. Defaults to `1` to make it more convenient to use this as a utility. """ def __init__(self, input_size: 'int', output_size: 'int'=1): super().__init__() self.output_size = output_size def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
kiudee/cs-ranking
DeterministicSumming
false
15,832
[ "Apache-2.0" ]
65
47cf648fa286c37b9214bbad1926004d4d7d9796
https://github.com/kiudee/cs-ranking/tree/47cf648fa286c37b9214bbad1926004d4d7d9796
SparsityLoss
import torch import torch.nn as nn import torch.utils.data class SparsityLoss(nn.Module): """ Penalizes small values to encourage sparsity """ def __init__(self): super(SparsityLoss, self).__init__() self.power = 0.2 self.loss = nn.L1Loss() def forward(self, kernel): return self.loss(torch.abs(kernel) ** self.power, torch.zeros_like( kernel)) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, 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 @triton.jit def triton_per_fused_abs_mean_sub_0(in_out_ptr0, in_ptr0, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl_math.abs(tmp0) tmp2 = 0.2 tmp3 = libdevice.pow(tmp1, tmp2) tmp4 = tl_math.abs(tmp3) tmp5 = tl.broadcast_to(tmp4, [RBLOCK]) tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0)) tmp8 = 256.0 tmp9 = tmp7 / tmp8 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp9, None) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_abs_mean_sub_0[grid(1)](buf1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 return buf1, class SparsityLossNew(nn.Module): """ Penalizes small values to encourage sparsity """ def __init__(self): super(SparsityLossNew, self).__init__() self.power = 0.2 self.loss = nn.L1Loss() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
kingsj0405/Explorable-Super-Resolution
SparsityLoss
false
15,833
[ "Apache-2.0" ]
54
6582477ec1e2b0c6f4bd781552ac880fabdb4496
https://github.com/kingsj0405/Explorable-Super-Resolution/tree/6582477ec1e2b0c6f4bd781552ac880fabdb4496
BertSelfAttention
from _paritybench_helpers import _mock_config import math import torch from torch import nn 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 = torch.clamp(attention_scores, -10000.0, 10000.0) 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 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)}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clone_0(in_ptr0, 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__softmax_add_clamp_div_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 16 tmp0 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last') tmp13 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp16 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp20 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp23 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp27 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp1 = 1.0 tmp2 = tmp0 * tmp1 tmp3 = -10000.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = 10000.0 tmp6 = triton_helpers.minimum(tmp4, tmp5) tmp8 = tmp6 + tmp7 tmp10 = tmp9 * tmp1 tmp11 = triton_helpers.maximum(tmp10, tmp3) tmp12 = triton_helpers.minimum(tmp11, tmp5) tmp14 = tmp12 + tmp13 tmp15 = triton_helpers.maximum(tmp8, tmp14) tmp17 = tmp16 * tmp1 tmp18 = triton_helpers.maximum(tmp17, tmp3) tmp19 = triton_helpers.minimum(tmp18, tmp5) tmp21 = tmp19 + tmp20 tmp22 = triton_helpers.maximum(tmp15, tmp21) tmp24 = tmp23 * tmp1 tmp25 = triton_helpers.maximum(tmp24, tmp3) tmp26 = triton_helpers.minimum(tmp25, tmp5) tmp28 = tmp26 + tmp27 tmp29 = triton_helpers.maximum(tmp22, tmp28) tmp30 = tmp8 - tmp29 tmp31 = tl_math.exp(tmp30) tmp32 = tmp14 - tmp29 tmp33 = tl_math.exp(tmp32) tmp34 = tmp31 + tmp33 tmp35 = tmp21 - tmp29 tmp36 = tl_math.exp(tmp35) tmp37 = tmp34 + tmp36 tmp38 = tmp28 - tmp29 tmp39 = tl_math.exp(tmp38) tmp40 = tmp37 + tmp39 tl.store(out_ptr0 + x2, tmp29, xmask) tl.store(out_ptr1 + x2, tmp40, xmask) @triton.jit def triton_poi_fused__softmax_add_clamp_div_ge_le_logical_and_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 x4 = xindex % 64 x5 = xindex // 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp7 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr2 + x5, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr3 + x5, xmask, eviction_policy='evict_last') tmp1 = 1.0 tmp2 = tmp0 * tmp1 tmp3 = -10000.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = 10000.0 tmp6 = triton_helpers.minimum(tmp4, tmp5) tmp8 = tmp6 + tmp7 tmp10 = tmp8 - tmp9 tmp11 = tl_math.exp(tmp10) tmp13 = tmp11 / tmp12 tmp14 = tmp2 >= tmp3 tmp15 = tmp2 <= tmp5 tmp16 = tmp14 & tmp15 tl.store(out_ptr0 + x3, tmp13, xmask) tl.store(out_ptr1 + x3, tmp16, xmask) @triton.jit def triton_poi_fused_clone_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8) = 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)) 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_clone_0[grid(16, 4)](buf0, primals_2, buf3, 16, 4, XBLOCK=2, YBLOCK=16, 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_clone_0[grid(16, 4)](buf1, primals_5, buf4, 16, 4, XBLOCK=2, YBLOCK=16, 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) triton_poi_fused__softmax_add_clamp_div_1[grid(64)](buf5, primals_8, buf6, buf7, 64, XBLOCK=64, num_warps=1, num_stages=1) buf8 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf12 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused__softmax_add_clamp_div_ge_le_logical_and_2[grid(256)]( buf5, primals_8, buf6, buf7, buf8, buf12, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf5 del primals_8 buf9 = reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf7 triton_poi_fused_clone_0[grid(16, 4)](buf2, primals_7, buf9, 16, 4, XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1) del primals_7 buf10 = reinterpret_tensor(buf2, (16, 4, 1), (4, 1, 1), 0) del buf2 extern_kernels.bmm(reinterpret_tensor(buf8, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf9, (16, 4, 1), (4, 1, 0), 0), out=buf10) buf11 = reinterpret_tensor(buf6, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf6 triton_poi_fused_clone_3[grid(16, 4)](buf10, buf11, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) del buf10 return reinterpret_tensor(buf11, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(primals_3, (16, 4), (4, 1), 0 ), buf8, reinterpret_tensor(buf9, (16, 1, 4), (4, 1, 1), 0 ), buf12, reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 4), 0) class BertSelfAttentionNew(nn.Module): def __init__(self, config): super(BertSelfAttentionNew, 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, input_0, input_1): primals_1 = self.query.weight primals_2 = self.query.bias primals_4 = self.key.weight primals_5 = self.key.bias primals_6 = self.value.weight primals_7 = self.value.bias 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]) return output[0]
BIT-ENGD/eeqa
BertSelfAttention
false
15,834
[ "MIT" ]
142
2995abbaff1fb47131246a247ee7ed62aa94f4c3
https://github.com/BIT-ENGD/eeqa/tree/2995abbaff1fb47131246a247ee7ed62aa94f4c3
WayPoly
import torch class WayPoly(torch.nn.Module): """Apply multiple modules to input and sum. It's equation for `poly_modules` length equal to :math:`N` could be expressed by !!!math I + F_1(I) + F_2(I) + ... + F_N where :math:`I` is identity and consecutive :math:`F_N` are consecutive `poly_modules` applied to input. Could be considered as an extension of standard `ResNet` to many parallel modules. Originally proposed by Xingcheng Zhang et al. in `PolyNet: A Pursuit of Structural Diversity in Very Deep Networks [here](https://arxiv.org/abs/1608.06993) Attributes: *poly_modules : Variable arg of modules to use. If empty, acts as an identity. For single module acts like `ResNet`. `2` was used in original paper. All modules need `inputs` and `outputs` of equal `shape`. """ def __init__(self, *poly_modules: torch.nn.Module): """Initialize `WayPoly` object. Arguments: *poly_modules : Variable arg of modules to use. If empty, acts as an identity. For single module acts like `ResNet`. `2` was used in original paper. All modules need `inputs` and `outputs` of equal `shape`. """ super().__init__() self.poly_modules: 'torch.nn.Module' = torch.nn.ModuleList(poly_modules ) def forward(self, inputs): outputs = [] for module in self.poly_modules: outputs.append(module(inputs)) return torch.stack([inputs] + outputs, dim=0).sum(dim=0) 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 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_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 x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tl.store(out_ptr0 + x0, 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_sum_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, class WayPolyNew(torch.nn.Module): """Apply multiple modules to input and sum. It's equation for `poly_modules` length equal to :math:`N` could be expressed by !!!math I + F_1(I) + F_2(I) + ... + F_N where :math:`I` is identity and consecutive :math:`F_N` are consecutive `poly_modules` applied to input. Could be considered as an extension of standard `ResNet` to many parallel modules. Originally proposed by Xingcheng Zhang et al. in `PolyNet: A Pursuit of Structural Diversity in Very Deep Networks [here](https://arxiv.org/abs/1608.06993) Attributes: *poly_modules : Variable arg of modules to use. If empty, acts as an identity. For single module acts like `ResNet`. `2` was used in original paper. All modules need `inputs` and `outputs` of equal `shape`. """ def __init__(self, *poly_modules: torch.nn.Module): """Initialize `WayPoly` object. Arguments: *poly_modules : Variable arg of modules to use. If empty, acts as an identity. For single module acts like `ResNet`. `2` was used in original paper. All modules need `inputs` and `outputs` of equal `shape`. """ super().__init__() self.poly_modules: 'torch.nn.Module' = torch.nn.ModuleList(poly_modules ) def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
klaudiapalasz/torchlayers
WayPoly
false
15,835
[ "MIT" ]
573
e6edd8797875325b7c0539d75a12f0d51f494127
https://github.com/klaudiapalasz/torchlayers/tree/e6edd8797875325b7c0539d75a12f0d51f494127
Spatial_Attention_layer
import torch import torch.utils.data import torch.nn.functional as F import torch.nn as nn class Spatial_Attention_layer(nn.Module): """ compute spatial attention scores """ def __init__(self, DEVICE, in_channels, num_of_vertices, num_of_timesteps): super(Spatial_Attention_layer, self).__init__() self.W1 = nn.Parameter(torch.FloatTensor(num_of_timesteps)) self.W2 = nn.Parameter(torch.FloatTensor(in_channels, num_of_timesteps) ) self.W3 = nn.Parameter(torch.FloatTensor(in_channels)) self.bs = nn.Parameter(torch.FloatTensor(1, num_of_vertices, num_of_vertices)) self.Vs = nn.Parameter(torch.FloatTensor(num_of_vertices, num_of_vertices)) def forward(self, x): """ :param x: (batch_size, N, F_in, T) :return: (B,N,N) """ lhs = torch.matmul(torch.matmul(x, self.W1), self.W2) rhs = torch.matmul(self.W3, x).transpose(-1, -2) product = torch.matmul(lhs, rhs) S = torch.matmul(self.Vs, torch.sigmoid(product + self.bs)) S_normalized = F.softmax(S, dim=1) return S_normalized def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'DEVICE': 4, 'in_channels': 4, 'num_of_vertices': 4, 'num_of_timesteps': 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.utils.data import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_mv_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 tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp4 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + 1) tmp6 = tl.broadcast_to(tmp5, [XBLOCK]) tmp9 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr1 + 2) tmp11 = tl.broadcast_to(tmp10, [XBLOCK]) tmp14 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp15 = tl.load(in_ptr1 + 3) tmp16 = tl.broadcast_to(tmp15, [XBLOCK]) tmp3 = tmp0 * tmp2 tmp7 = tmp4 * tmp6 tmp8 = tmp3 + tmp7 tmp12 = tmp9 * tmp11 tmp13 = tmp8 + tmp12 tmp17 = tmp14 * tmp16 tmp18 = tmp13 + tmp17 tl.store(out_ptr0 + x0, tmp18, xmask) @triton.jit def triton_poi_fused_mv_1(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 + (16 * (x0 // 4) + x0 % 4), xmask) tmp1 = tl.load(in_ptr1 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp4 = tl.load(in_ptr0 + (4 + 16 * (x0 // 4) + x0 % 4), xmask) tmp5 = tl.load(in_ptr1 + 1) tmp6 = tl.broadcast_to(tmp5, [XBLOCK]) tmp9 = tl.load(in_ptr0 + (8 + 16 * (x0 // 4) + x0 % 4), xmask) tmp10 = tl.load(in_ptr1 + 2) tmp11 = tl.broadcast_to(tmp10, [XBLOCK]) tmp14 = tl.load(in_ptr0 + (12 + 16 * (x0 // 4) + x0 % 4), xmask) tmp15 = tl.load(in_ptr1 + 3) tmp16 = tl.broadcast_to(tmp15, [XBLOCK]) tmp3 = tmp0 * tmp2 tmp7 = tmp4 * tmp6 tmp8 = tmp3 + tmp7 tmp12 = tmp9 * tmp11 tmp13 = tmp8 + tmp12 tmp17 = tmp14 * tmp16 tmp18 = tmp13 + tmp17 tl.store(out_ptr0 + x0, tmp18, xmask) @triton.jit def triton_poi_fused_clone_2(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 y3 = yindex y0 = yindex % 4 y1 = yindex // 4 tmp0 = tl.load(in_ptr0 + (x2 + 4 * y3), xmask & ymask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr1 + (x2 + 4 * y0), xmask & ymask, eviction_policy= 'evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.sigmoid(tmp2) tl.store(out_ptr0 + (y0 + 4 * x2 + 16 * y1), tmp3, xmask & ymask) @triton.jit def triton_poi_fused__softmax_clone_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_4(in_ptr0, out_ptr0, 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_ptr0 + (4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + (x2 + 4 * y3), tmp8, xmask & ymask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args args.clear() assert_size_stride(primals_1, (4,), (1,)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4, 4), (4, 1)) assert_size_stride(primals_6, (1, 4, 4), (16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64,), (1,), torch.float32) get_raw_stream(0) triton_poi_fused_mv_0[grid(64)](primals_2, primals_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_1 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0), primals_3, out=buf1) buf2 = empty_strided_cuda((64,), (1,), torch.float32) triton_poi_fused_mv_1[grid(64)](primals_2, primals_4, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_4 buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf2, (4, 4, 4), (16, 1, 4), 0), out=buf3) buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_clone_2[grid(16, 4)](buf3, primals_6, buf4, 16, 4, XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1) buf5 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf4, (16, 4), (4, 1), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf5) buf6 = empty_strided_cuda((4, 4, 4), (16, 1, 4), torch.float32) triton_poi_fused__softmax_clone_3[grid(64)](buf5, buf6, 64, XBLOCK= 64, num_warps=1, num_stages=1) buf7 = reinterpret_tensor(buf5, (4, 4, 4), (16, 4, 1), 0) del buf5 triton_poi_fused__softmax_4[grid(16, 4)](buf6, buf7, 16, 4, XBLOCK= 4, YBLOCK=16, num_warps=1, num_stages=1) del buf6 return buf7, primals_2, primals_6, buf3, reinterpret_tensor(buf4, (16, 4), (4, 1), 0), buf7, primals_5, reinterpret_tensor(buf1, (4, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf2, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(buf0, (4, 16), (1, 4), 0), reinterpret_tensor( primals_3, (4, 4), (1, 4), 0) class Spatial_Attention_layerNew(nn.Module): """ compute spatial attention scores """ def __init__(self, DEVICE, in_channels, num_of_vertices, num_of_timesteps): super(Spatial_Attention_layerNew, self).__init__() self.W1 = nn.Parameter(torch.FloatTensor(num_of_timesteps)) self.W2 = nn.Parameter(torch.FloatTensor(in_channels, num_of_timesteps) ) self.W3 = nn.Parameter(torch.FloatTensor(in_channels)) self.bs = nn.Parameter(torch.FloatTensor(1, num_of_vertices, num_of_vertices)) self.Vs = nn.Parameter(torch.FloatTensor(num_of_vertices, num_of_vertices)) def forward(self, input_0): primals_1 = self.W1 primals_3 = self.W2 primals_4 = self.W3 primals_6 = self.bs primals_5 = self.Vs primals_2 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6]) return output[0]
kevin-xuan/Traffic-Benchmark
Spatial_Attention_layer
false
15,836
[ "MIT" ]
120
b9f8e40b4df9b58f5ad88432dc070cbbbcdc0228
https://github.com/kevin-xuan/Traffic-Benchmark/tree/b9f8e40b4df9b58f5ad88432dc070cbbbcdc0228
Temporal_Attention_layer
import torch import torch.utils.data import torch.nn.functional as F import torch.nn as nn class Temporal_Attention_layer(nn.Module): def __init__(self, DEVICE, in_channels, num_of_vertices, num_of_timesteps): super(Temporal_Attention_layer, self).__init__() self.U1 = nn.Parameter(torch.FloatTensor(num_of_vertices)) self.U2 = nn.Parameter(torch.FloatTensor(in_channels, num_of_vertices)) self.U3 = nn.Parameter(torch.FloatTensor(in_channels)) self.be = nn.Parameter(torch.FloatTensor(1, num_of_timesteps, num_of_timesteps)) self.Ve = nn.Parameter(torch.FloatTensor(num_of_timesteps, num_of_timesteps)) def forward(self, x): """ :param x: (batch_size, N, F_in, T) :return: (B, T, T) """ _, _num_of_vertices, _num_of_features, _num_of_timesteps = x.shape lhs = torch.matmul(torch.matmul(x.permute(0, 3, 2, 1), self.U1), self.U2) rhs = torch.matmul(self.U3, x) product = torch.matmul(lhs, rhs) E = torch.matmul(self.Ve, torch.sigmoid(product + self.be)) E_normalized = F.softmax(E, dim=1) return E_normalized def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'DEVICE': 4, 'in_channels': 4, 'num_of_vertices': 4, 'num_of_timesteps': 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.utils.data import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_mv_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 tmp0 = tl.load(in_ptr0 + (4 * (x0 % 4) + 64 * (x0 // 16) + x0 // 4 % 4), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp4 = tl.load(in_ptr0 + (16 + 4 * (x0 % 4) + 64 * (x0 // 16) + x0 // 4 % 4), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + 1) tmp6 = tl.broadcast_to(tmp5, [XBLOCK]) tmp9 = tl.load(in_ptr0 + (32 + 4 * (x0 % 4) + 64 * (x0 // 16) + x0 // 4 % 4), xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr1 + 2) tmp11 = tl.broadcast_to(tmp10, [XBLOCK]) tmp14 = tl.load(in_ptr0 + (48 + 4 * (x0 % 4) + 64 * (x0 // 16) + x0 // 4 % 4), xmask, eviction_policy='evict_last') tmp15 = tl.load(in_ptr1 + 3) tmp16 = tl.broadcast_to(tmp15, [XBLOCK]) tmp3 = tmp0 * tmp2 tmp7 = tmp4 * tmp6 tmp8 = tmp3 + tmp7 tmp12 = tmp9 * tmp11 tmp13 = tmp8 + tmp12 tmp17 = tmp14 * tmp16 tmp18 = tmp13 + tmp17 tl.store(out_ptr0 + x0, tmp18, xmask) @triton.jit def triton_poi_fused_mv_1(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 + (16 * (x0 // 4) + x0 % 4), xmask) tmp1 = tl.load(in_ptr1 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp4 = tl.load(in_ptr0 + (4 + 16 * (x0 // 4) + x0 % 4), xmask) tmp5 = tl.load(in_ptr1 + 1) tmp6 = tl.broadcast_to(tmp5, [XBLOCK]) tmp9 = tl.load(in_ptr0 + (8 + 16 * (x0 // 4) + x0 % 4), xmask) tmp10 = tl.load(in_ptr1 + 2) tmp11 = tl.broadcast_to(tmp10, [XBLOCK]) tmp14 = tl.load(in_ptr0 + (12 + 16 * (x0 // 4) + x0 % 4), xmask) tmp15 = tl.load(in_ptr1 + 3) tmp16 = tl.broadcast_to(tmp15, [XBLOCK]) tmp3 = tmp0 * tmp2 tmp7 = tmp4 * tmp6 tmp8 = tmp3 + tmp7 tmp12 = tmp9 * tmp11 tmp13 = tmp8 + tmp12 tmp17 = tmp14 * tmp16 tmp18 = tmp13 + tmp17 tl.store(out_ptr0 + x0, tmp18, xmask) @triton.jit def triton_poi_fused_clone_2(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 y3 = yindex y0 = yindex % 4 y1 = yindex // 4 tmp0 = tl.load(in_ptr0 + (x2 + 4 * y3), xmask & ymask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr1 + (x2 + 4 * y0), xmask & ymask, eviction_policy= 'evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.sigmoid(tmp2) tl.store(out_ptr0 + (y0 + 4 * x2 + 16 * y1), tmp3, xmask & ymask) @triton.jit def triton_poi_fused__softmax_clone_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_4(in_ptr0, out_ptr0, 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_ptr0 + (4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + (x2 + 4 * y3), tmp8, xmask & ymask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4, 4), (4, 1)) assert_size_stride(primals_6, (1, 4, 4), (16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64,), (1,), torch.float32) get_raw_stream(0) triton_poi_fused_mv_0[grid(64)](primals_1, primals_2, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_2 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0), primals_3, out=buf1) buf2 = empty_strided_cuda((64,), (1,), torch.float32) triton_poi_fused_mv_1[grid(64)](primals_1, primals_4, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_4 buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf2, (4, 4, 4), (16, 4, 1), 0), out=buf3) buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_clone_2[grid(16, 4)](buf3, primals_6, buf4, 16, 4, XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1) buf5 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf4, (16, 4), (4, 1), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf5) buf6 = empty_strided_cuda((4, 4, 4), (16, 1, 4), torch.float32) triton_poi_fused__softmax_clone_3[grid(64)](buf5, buf6, 64, XBLOCK= 64, num_warps=1, num_stages=1) buf7 = reinterpret_tensor(buf5, (4, 4, 4), (16, 4, 1), 0) del buf5 triton_poi_fused__softmax_4[grid(16, 4)](buf6, buf7, 16, 4, XBLOCK= 4, YBLOCK=16, num_warps=1, num_stages=1) del buf6 return buf7, primals_1, primals_6, buf3, reinterpret_tensor(buf4, (16, 4), (4, 1), 0), buf7, primals_5, reinterpret_tensor(buf1, (4, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf2, (4, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf0, (4, 16), (1, 4), 0), reinterpret_tensor( primals_3, (4, 4), (1, 4), 0) class Temporal_Attention_layerNew(nn.Module): def __init__(self, DEVICE, in_channels, num_of_vertices, num_of_timesteps): super(Temporal_Attention_layerNew, self).__init__() self.U1 = nn.Parameter(torch.FloatTensor(num_of_vertices)) self.U2 = nn.Parameter(torch.FloatTensor(in_channels, num_of_vertices)) self.U3 = nn.Parameter(torch.FloatTensor(in_channels)) self.be = nn.Parameter(torch.FloatTensor(1, num_of_timesteps, num_of_timesteps)) self.Ve = nn.Parameter(torch.FloatTensor(num_of_timesteps, num_of_timesteps)) def forward(self, input_0): primals_2 = self.U1 primals_3 = self.U2 primals_4 = self.U3 primals_6 = self.be primals_5 = self.Ve primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6]) return output[0]
kevin-xuan/Traffic-Benchmark
Temporal_Attention_layer
false
15,837
[ "MIT" ]
120
b9f8e40b4df9b58f5ad88432dc070cbbbcdc0228
https://github.com/kevin-xuan/Traffic-Benchmark/tree/b9f8e40b4df9b58f5ad88432dc070cbbbcdc0228
Optimizable_Temperature
import torch import torch.utils.data class Optimizable_Temperature(torch.nn.Module): def __init__(self, initial_temperature=None): super(Optimizable_Temperature, self).__init__() self.log_temperature = torch.nn.Parameter(data=torch.zeros([1]). type(torch.DoubleTensor)) if initial_temperature is not None: self.log_temperature.data = torch.log(torch.tensor( initial_temperature).type(torch.DoubleTensor)) def forward(self): return torch.exp(self.log_temperature) def get_inputs(): return [] 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 assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_exp_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) tmp0 = tl.load(in_ptr0 + 0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK]) tmp2 = libdevice.exp(tmp1) tl.store(out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp2, None) def call(args): primals_1, = args args.clear() assert_size_stride(primals_1, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((1,), (1,), torch.float64) get_raw_stream(0) triton_poi_fused_exp_0[grid(1)](primals_1, buf0, 1, XBLOCK=1, num_warps=1, num_stages=1) del primals_1 return buf0, buf0 class Optimizable_TemperatureNew(torch.nn.Module): def __init__(self, initial_temperature=None): super(Optimizable_TemperatureNew, self).__init__() self.log_temperature = torch.nn.Parameter(data=torch.zeros([1]). type(torch.DoubleTensor)) if initial_temperature is not None: self.log_temperature.data = torch.log(torch.tensor( initial_temperature).type(torch.DoubleTensor)) def forward(self): primals_1 = self.log_temperature output = call([primals_1]) return output[0]
kingsj0405/Explorable-Super-Resolution
Optimizable_Temperature
false
15,838
[ "Apache-2.0" ]
54
6582477ec1e2b0c6f4bd781552ac880fabdb4496
https://github.com/kingsj0405/Explorable-Super-Resolution/tree/6582477ec1e2b0c6f4bd781552ac880fabdb4496
HardSigmoid
import torch def hard_sigmoid(tensor: 'torch.Tensor', inplace: 'bool'=False) ->torch.Tensor: """ Applies HardSigmoid function element-wise. See :class:`torchlayers.activations.HardSigmoid` for more details. Arguments: tensor : Tensor activated element-wise inplace : Whether operation should be performed `in-place`. Default: `False` Returns: torch.Tensor: """ return torch.nn.functional.hardtanh(tensor, min_val=0, inplace=inplace) class HardSigmoid(torch.nn.Module): """ Applies HardSigmoid function element-wise. Uses `torch.nn.functional.hardtanh` internally with `0` and `1` ranges. Arguments: tensor : Tensor activated element-wise """ def forward(self, tensor: 'torch.Tensor'): return hard_sigmoid(tensor) 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 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_hardtanh_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.0 tmp2 = triton_helpers.maximum(tmp0, tmp1) tmp3 = 1.0 tmp4 = triton_helpers.minimum(tmp2, tmp3) tl.store(out_ptr0 + x0, tmp4, 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_hardtanh_0[grid(256)](arg0_1, buf0, 256, XBLOCK= 256, num_warps=4, num_stages=1) del arg0_1 return buf0, def hard_sigmoid(tensor: 'torch.Tensor', inplace: 'bool'=False) ->torch.Tensor: """ Applies HardSigmoid function element-wise. See :class:`torchlayers.activations.HardSigmoid` for more details. Arguments: tensor : Tensor activated element-wise inplace : Whether operation should be performed `in-place`. Default: `False` Returns: torch.Tensor: """ return torch.nn.functional.hardtanh(tensor, min_val=0, inplace=inplace) class HardSigmoidNew(torch.nn.Module): """ Applies HardSigmoid function element-wise. Uses `torch.nn.functional.hardtanh` internally with `0` and `1` ranges. Arguments: tensor : Tensor activated element-wise """ def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
klaudiapalasz/torchlayers
HardSigmoid
false
15,839
[ "MIT" ]
573
e6edd8797875325b7c0539d75a12f0d51f494127
https://github.com/klaudiapalasz/torchlayers/tree/e6edd8797875325b7c0539d75a12f0d51f494127
Blur
import math import torch import torch.nn as nn import torch.nn.functional as F class SamePad(nn.Module): def __init__(self, filter_size, pad_mode='constant', **kwargs): super(SamePad, self).__init__() self.pad_size = [int((filter_size - 1) / 2.0), int(math.ceil(( filter_size - 1) / 2.0)), int((filter_size - 1) / 2.0), int( math.ceil((filter_size - 1) / 2.0))] self.pad_mode = pad_mode def forward(self, x): x = F.pad(x, self.pad_size, mode=self.pad_mode) return x def extra_repr(self): return 'pad_size=%s, pad_mode=%s' % (self.pad_size, self.pad_mode) class Blur(nn.Module): def __init__(self, in_filters, sfilter=(1, 1), pad_mode='replicate', ** kwargs): super(Blur, self).__init__() filter_size = len(sfilter) self.pad = SamePad(filter_size, pad_mode=pad_mode) self.filter_proto = torch.tensor(sfilter, dtype=torch.float, requires_grad=False) self.filter = torch.tensordot(self.filter_proto, self.filter_proto, dims=0) self.filter = self.filter / torch.sum(self.filter) self.filter = self.filter.repeat([in_filters, 1, 1, 1]) self.filter = torch.nn.Parameter(self.filter, requires_grad=False) def forward(self, x): x = self.pad(x) x = F.conv2d(x, self.filter, groups=x.size()[1]) return x def extra_repr(self): return 'pad=%s, filter_proto=%s' % (self.pad, self.filter_proto. tolist()) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_filters': 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 math import torch.nn as nn import torch.nn.functional as F assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_replication_pad2d_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 25 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 % 5 x3 = xindex // 5 y4 = yindex x5 = xindex y0 = yindex % 4 y1 = yindex // 4 tmp0 = tl.load(in_ptr0 + (4 * (3 * (3 <= x3) + x3 * (x3 < 3)) + 16 * y4 + (3 * (3 <= x2) + x2 * (x2 < 3))), xmask & ymask, eviction_policy= 'evict_last') tl.store(out_ptr0 + (y0 + 4 * x5 + 100 * y1), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_convolution_replication_pad2d_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 16 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 64 * y1), xmask & ymask) tl.store(out_ptr0 + (x2 + 16 * y3), tmp0, xmask & ymask) 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, 1, 2, 2), (4, 4, 2, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 5, 5), (100, 1, 20, 4), torch.float32) get_raw_stream(0) triton_poi_fused_replication_pad2d_0[grid(16, 25)](arg0_1, buf0, 16, 25, XBLOCK=32, YBLOCK=16, num_warps=4, num_stages=1) del arg0_1 buf1 = extern_kernels.convolution(buf0, arg1_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=4, bias=None) assert_size_stride(buf1, (4, 4, 4, 4), (64, 1, 16, 4)) del arg1_1 del buf0 buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_convolution_replication_pad2d_1[grid(16, 16)](buf1, buf2, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) del buf1 return buf2, class SamePad(nn.Module): def __init__(self, filter_size, pad_mode='constant', **kwargs): super(SamePad, self).__init__() self.pad_size = [int((filter_size - 1) / 2.0), int(math.ceil(( filter_size - 1) / 2.0)), int((filter_size - 1) / 2.0), int( math.ceil((filter_size - 1) / 2.0))] self.pad_mode = pad_mode def forward(self, x): x = F.pad(x, self.pad_size, mode=self.pad_mode) return x def extra_repr(self): return 'pad_size=%s, pad_mode=%s' % (self.pad_size, self.pad_mode) class BlurNew(nn.Module): def __init__(self, in_filters, sfilter=(1, 1), pad_mode='replicate', ** kwargs): super(BlurNew, self).__init__() filter_size = len(sfilter) self.pad = SamePad(filter_size, pad_mode=pad_mode) self.filter_proto = torch.tensor(sfilter, dtype=torch.float, requires_grad=False) self.filter = torch.tensordot(self.filter_proto, self.filter_proto, dims=0) self.filter = self.filter / torch.sum(self.filter) self.filter = self.filter.repeat([in_filters, 1, 1, 1]) self.filter = torch.nn.Parameter(self.filter, requires_grad=False) def extra_repr(self): return 'pad=%s, filter_proto=%s' % (self.pad, self.filter_proto. tolist()) def forward(self, input_0): arg1_1 = self.filter arg0_1 = input_0 output = call([arg0_1, arg1_1]) return output[0]
kimfunn/spatial-smoothing
Blur
false
15,840
[ "Apache-2.0" ]
438
4f849d57c66c2dbdfaa56fc28727e95eddfd337c
https://github.com/kimfunn/spatial-smoothing/tree/4f849d57c66c2dbdfaa56fc28727e95eddfd337c
Downsample
import torch import torch.nn as nn import torch.nn.functional as F class Downsample(nn.Module): def __init__(self, strides=(2, 2), **kwargs): super(Downsample, self).__init__() if isinstance(strides, int): strides = strides, strides self.strides = strides def forward(self, x): shape = -(-x.size()[2] // self.strides[0]), -(-x.size()[3] // self. strides[1]) x = F.interpolate(x, size=shape, mode='nearest') return x def extra_repr(self): return 'strides=%s' % repr(self.strides) 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__unsafe_index_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 x1 = xindex // 2 % 2 x0 = xindex % 2 x2 = xindex // 4 x4 = xindex tmp0 = x1 tmp1 = tmp0.to(tl.float32) tmp2 = 2.0 tmp3 = tmp1 * tmp2 tmp4 = tmp3.to(tl.int32) tmp5 = x0 tmp6 = tmp5.to(tl.float32) tmp7 = tmp6 * tmp2 tmp8 = tmp7.to(tl.int32) tmp9 = tl.load(in_ptr0 + (tmp8 + 4 * tmp4 + 16 * x2), xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x4, tmp9, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32) get_raw_stream(0) triton_poi_fused__unsafe_index_0[grid(64)](arg0_1, buf0, 64, XBLOCK =64, num_warps=1, num_stages=1) del arg0_1 return buf0, class DownsampleNew(nn.Module): def __init__(self, strides=(2, 2), **kwargs): super(DownsampleNew, self).__init__() if isinstance(strides, int): strides = strides, strides self.strides = strides def extra_repr(self): return 'strides=%s' % repr(self.strides) def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
kimfunn/spatial-smoothing
Downsample
false
15,841
[ "Apache-2.0" ]
438
4f849d57c66c2dbdfaa56fc28727e95eddfd337c
https://github.com/kimfunn/spatial-smoothing/tree/4f849d57c66c2dbdfaa56fc28727e95eddfd337c
SoftDiceLoss
import torch import torch.nn as nn import torch.nn.functional as F class SoftDiceLoss(nn.Module): def __init__(self, weight=None, size_average=True): super(SoftDiceLoss, self).__init__() def forward(self, logits, targets): smooth = 1.0 logits = F.sigmoid(logits) iflat = logits.view(-1) tflat = targets.view(-1) intersection = (iflat * tflat).sum() return 1 - (2.0 * intersection + smooth) / (iflat.sum() + tflat.sum () + smooth) 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) 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 = tl.broadcast_to(tmp1, [RBLOCK]) tmp9 = triton_helpers.promote_to_tensor(tl.sum(tmp7, 0)) tmp10 = tl.broadcast_to(tmp2, [RBLOCK]) tmp12 = triton_helpers.promote_to_tensor(tl.sum(tmp10, 0)) tmp13 = 2.0 tmp14 = tmp6 * tmp13 tmp15 = 1.0 tmp16 = tmp14 + tmp15 tmp17 = tmp9 + tmp12 tmp18 = tmp17 + tmp15 tmp19 = tmp16 / tmp18 tmp20 = tmp15 - tmp19 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 SoftDiceLossNew(nn.Module): def __init__(self, weight=None, size_average=True): super(SoftDiceLossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kryptonite0/Global_Convolutional_Network
SoftDiceLoss
false
15,842
[ "MIT" ]
88
33de71bbe468f485eb38345f4982923945d1a0be
https://github.com/kryptonite0/Global_Convolutional_Network/tree/33de71bbe468f485eb38345f4982923945d1a0be
Swish
import torch def swish(tensor: 'torch.Tensor', beta: 'float'=1.0) ->torch.Tensor: """ Applies Swish function element-wise. See :class:`torchlayers.activations.Swish` for more details. Arguments: tensor : Tensor activated element-wise beta : Multiplier used for sigmoid. Default: 1.0 (no multiplier) Returns: torch.Tensor: """ return torch.sigmoid(beta * tensor) * tensor class Swish(torch.nn.Module): """ Applies Swish function element-wise. !!!math Swish(x) = x / (1 + \\exp(-beta * x)) This form was originally proposed by Prajit Ramachandran et al. in `Searching for Activation Functions <https://arxiv.org/pdf/1710.05941.pdf>`__ Attributes: beta : Multiplier used for sigmoid. Default: 1.0 (no multiplier) """ def __init__(self, beta: 'float'=1.0): """Initialize `Swish` object. Arguments: beta : Multiplier used for sigmoid. Default: 1.0 (no multiplier) """ super().__init__() self.beta = beta def forward(self, tensor: 'torch.Tensor'): return swish(tensor, self.beta) 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 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_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 = 1.0 tmp2 = tmp0 * tmp1 tmp3 = tl.sigmoid(tmp2) tmp4 = tmp3 * tmp0 tl.store(out_ptr0 + x0, tmp4, 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_mul_sigmoid_0[grid(256)](arg0_1, buf0, 256, XBLOCK =128, num_warps=4, num_stages=1) del arg0_1 return buf0, def swish(tensor: 'torch.Tensor', beta: 'float'=1.0) ->torch.Tensor: """ Applies Swish function element-wise. See :class:`torchlayers.activations.Swish` for more details. Arguments: tensor : Tensor activated element-wise beta : Multiplier used for sigmoid. Default: 1.0 (no multiplier) Returns: torch.Tensor: """ return torch.sigmoid(beta * tensor) * tensor class SwishNew(torch.nn.Module): """ Applies Swish function element-wise. !!!math Swish(x) = x / (1 + \\exp(-beta * x)) This form was originally proposed by Prajit Ramachandran et al. in `Searching for Activation Functions <https://arxiv.org/pdf/1710.05941.pdf>`__ Attributes: beta : Multiplier used for sigmoid. Default: 1.0 (no multiplier) """ def __init__(self, beta: 'float'=1.0): """Initialize `Swish` object. Arguments: beta : Multiplier used for sigmoid. Default: 1.0 (no multiplier) """ super().__init__() self.beta = beta def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
klaudiapalasz/torchlayers
Swish
false
15,843
[ "MIT" ]
573
e6edd8797875325b7c0539d75a12f0d51f494127
https://github.com/klaudiapalasz/torchlayers/tree/e6edd8797875325b7c0539d75a12f0d51f494127
ContextualCell
from _paritybench_helpers import _mock_config import torch from torch import nn def conv_bn_relu(C_in, C_out, kernel_size, stride, padding, affine=True): return nn.Sequential(nn.Conv2d(C_in, C_out, kernel_size, stride=stride, padding=padding, bias=False), nn.BatchNorm2d(C_out, affine=affine), nn.ReLU(inplace=False)) class AggregateCell(nn.Module): """ before aggregating, both paths should undergo conv1x1 with the output channels being equal to the smallest channel size among them upsampled to the highest resolution among them pre_transform: whether to do convbnrelu before summing up """ def __init__(self, size_1, size_2, agg_size, pre_transform=True): super(AggregateCell, self).__init__() self.pre_transform = pre_transform if self.pre_transform: self.branch_1 = conv_bn_relu(size_1, agg_size, 1, 1, 0) self.branch_2 = conv_bn_relu(size_2, agg_size, 1, 1, 0) def forward(self, x1, x2): if self.pre_transform: x1 = self.branch_1(x1) x2 = self.branch_2(x2) if x1.size()[2:] > x2.size()[2:]: x2 = nn.Upsample(size=x1.size()[2:], mode='bilinear')(x2) elif x1.size()[2:] < x2.size()[2:]: x1 = nn.Upsample(size=x2.size()[2:], mode='bilinear')(x1) return x1 + x2 class ContextualCell(nn.Module): """New contextual cell design Config contains [op1, [loc1, loc2, op1, op2], [...], [...]] """ def __init__(self, config, inp, repeats=1): super(ContextualCell, self).__init__() self._ops = nn.ModuleList() self._pos = [] self._collect_inds = [0] self._pools = ['x'] for ind, op in enumerate(config): if ind == 0: pos = 0 op_id = op self._collect_inds.remove(pos) op_name = OP_NAMES[op_id] self._ops.append(OPS[op_name](inp, inp, 1, True, repeats)) self._pos.append(pos) self._collect_inds.append(ind + 1) self._pools.append('{}({})'.format(op_name, self._pools[pos])) else: pos1, pos2, op_id1, op_id2 = op for pos, op_id in zip([pos1, pos2], [op_id1, op_id2]): if pos in self._collect_inds: self._collect_inds.remove(pos) op_name = OP_NAMES[op_id] self._ops.append(OPS[op_name](inp, inp, 1, True, repeats)) self._pos.append(pos) self._pools.append('{}({})'.format(op_name, self._pools [pos])) op_name = 'sum' self._ops.append(AggregateCell(size_1=None, size_2=None, agg_size=inp, pre_transform=False)) self._pos.append([ind * 3 - 1, ind * 3]) self._collect_inds.append(ind * 3 + 1) self._pools.append('{}({},{})'.format(op_name, self._pools[ ind * 3 - 1], self._pools[ind * 3])) def forward(self, x): feats = [x] for pos, op in zip(self._pos, self._ops): if isinstance(pos, list): assert len(pos) == 2, 'Two ops must be provided' feats.append(op(feats[pos[0]], feats[pos[1]])) else: feats.append(op(feats[pos])) out = 0 for i in self._collect_inds: out += feats[i] return out def prettify(self): return ' + '.join(self._pools[i] for i in self._collect_inds) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'config': _mock_config(), 'inp': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_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 tl.store(out_ptr0 + x0, tmp2, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, def conv_bn_relu(C_in, C_out, kernel_size, stride, padding, affine=True): return nn.Sequential(nn.Conv2d(C_in, C_out, kernel_size, stride=stride, padding=padding, bias=False), nn.BatchNorm2d(C_out, affine=affine), nn.ReLU(inplace=False)) class AggregateCell(nn.Module): """ before aggregating, both paths should undergo conv1x1 with the output channels being equal to the smallest channel size among them upsampled to the highest resolution among them pre_transform: whether to do convbnrelu before summing up """ def __init__(self, size_1, size_2, agg_size, pre_transform=True): super(AggregateCell, self).__init__() self.pre_transform = pre_transform if self.pre_transform: self.branch_1 = conv_bn_relu(size_1, agg_size, 1, 1, 0) self.branch_2 = conv_bn_relu(size_2, agg_size, 1, 1, 0) def forward(self, x1, x2): if self.pre_transform: x1 = self.branch_1(x1) x2 = self.branch_2(x2) if x1.size()[2:] > x2.size()[2:]: x2 = nn.Upsample(size=x1.size()[2:], mode='bilinear')(x2) elif x1.size()[2:] < x2.size()[2:]: x1 = nn.Upsample(size=x2.size()[2:], mode='bilinear')(x1) return x1 + x2 class ContextualCellNew(nn.Module): """New contextual cell design Config contains [op1, [loc1, loc2, op1, op2], [...], [...]] """ def __init__(self, config, inp, repeats=1): super(ContextualCellNew, self).__init__() self._ops = nn.ModuleList() self._pos = [] self._collect_inds = [0] self._pools = ['x'] for ind, op in enumerate(config): if ind == 0: pos = 0 op_id = op self._collect_inds.remove(pos) op_name = OP_NAMES[op_id] self._ops.append(OPS[op_name](inp, inp, 1, True, repeats)) self._pos.append(pos) self._collect_inds.append(ind + 1) self._pools.append('{}({})'.format(op_name, self._pools[pos])) else: pos1, pos2, op_id1, op_id2 = op for pos, op_id in zip([pos1, pos2], [op_id1, op_id2]): if pos in self._collect_inds: self._collect_inds.remove(pos) op_name = OP_NAMES[op_id] self._ops.append(OPS[op_name](inp, inp, 1, True, repeats)) self._pos.append(pos) self._pools.append('{}({})'.format(op_name, self._pools [pos])) op_name = 'sum' self._ops.append(AggregateCell(size_1=None, size_2=None, agg_size=inp, pre_transform=False)) self._pos.append([ind * 3 - 1, ind * 3]) self._collect_inds.append(ind * 3 + 1) self._pools.append('{}({},{})'.format(op_name, self._pools[ ind * 3 - 1], self._pools[ind * 3])) def prettify(self): return ' + '.join(self._pools[i] for i in self._collect_inds) def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
DrSleep/nas-segm-pytorch
ContextualCell
false
15,844
[ "BSD-2-Clause" ]
155
5de0c5c60cc05f94305ff59ae9f822656e3e7a96
https://github.com/DrSleep/nas-segm-pytorch/tree/5de0c5c60cc05f94305ff59ae9f822656e3e7a96
DeepSet
import torch import torch.nn as nn class DeepSet(nn.Module): """Aggregate object-level embeddings with a mean reduction. This module evaluates each object individually (using a object level embedding) and then aggregates the embeddings with a mean reduction. Parameters ---------- n_features : int The number of features per object. embedding_size : int The target embedding size. embedding_module : torch module An uninitialized torch module that expects two parameters: the input and the output size. It should then act similar to ``nn.Linear``, i.e. transform only the last dimension of the input. Defaults to a simple linear module. """ def __init__(self, n_features: 'int', embedding_size: 'int', embedding_module: 'nn.Module'=nn.Linear): super().__init__() self.embedding_module = embedding_module(n_features, embedding_size) def forward(self, instances): """Forward inputs through the network. Parameters ---------- instances : tensor The input tensor of shape (N, *, O, F), where F is the number of features and O is the number of objects. Returns ------- tensor A tensor of shape (N, *, E), where E ist the embedding size. """ embedded_objects = self.embedding_module(instances) return torch.mean(embedded_objects, dim=1) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'n_features': 4, 'embedding_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_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): 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.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mean_0[grid(64)](buf0, buf1, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf0 return buf1, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0) class DeepSetNew(nn.Module): """Aggregate object-level embeddings with a mean reduction. This module evaluates each object individually (using a object level embedding) and then aggregates the embeddings with a mean reduction. Parameters ---------- n_features : int The number of features per object. embedding_size : int The target embedding size. embedding_module : torch module An uninitialized torch module that expects two parameters: the input and the output size. It should then act similar to ``nn.Linear``, i.e. transform only the last dimension of the input. Defaults to a simple linear module. """ def __init__(self, n_features: 'int', embedding_size: 'int', embedding_module: 'nn.Module'=nn.Linear): super().__init__() self.embedding_module = embedding_module(n_features, embedding_size) def forward(self, input_0): primals_1 = self.embedding_module.weight primals_2 = self.embedding_module.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
kiudee/cs-ranking
DeepSet
false
15,845
[ "Apache-2.0" ]
65
47cf648fa286c37b9214bbad1926004d4d7d9796
https://github.com/kiudee/cs-ranking/tree/47cf648fa286c37b9214bbad1926004d4d7d9796
ConvertFloatToUint8
import torch import torchvision import torch.utils.data import torchvision.transforms import torch.nn class ConvertFloatToUint8(torch.nn.Module): """ Converts a video from dtype float32 to dtype uint8. """ def __init__(self): super().__init__() self.convert_func = torchvision.transforms.ConvertImageDtype(torch. uint8) def forward(self, x: 'torch.Tensor') ->torch.Tensor: """ Args: x (torch.Tensor): video tensor with shape (C, T, H, W). """ assert x.dtype == torch.float or x.dtype == torch.half, 'image must have dtype torch.uint8' return self.convert_func(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 torchvision import torch.utils.data import torchvision.transforms 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__to_copy_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 = 255.999 tmp2 = tmp0 * tmp1 tmp3 = tmp2.to(tl.int8).to(tl.uint8) tl.store(out_ptr0 + x0, tmp3, 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.uint8) get_raw_stream(0) triton_poi_fused__to_copy_mul_0[grid(256)](arg0_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf0, class ConvertFloatToUint8New(torch.nn.Module): """ Converts a video from dtype float32 to dtype uint8. """ def __init__(self): super().__init__() self.convert_func = torchvision.transforms.ConvertImageDtype(torch. uint8) def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
kevinmtian/pytorchvideo
ConvertFloatToUint8
false
15,846
[ "Apache-2.0" ]
2,391
168e16859a6029ef8ebeb476f9163bebb6c6b87d
https://github.com/kevinmtian/pytorchvideo/tree/168e16859a6029ef8ebeb476f9163bebb6c6b87d
NormSoftmaxLoss
import math import torch import torch.nn as nn from torch.nn import Parameter class NormSoftmaxLoss(nn.Module): """ L2 normalize weights and apply temperature scaling on logits. """ def __init__(self, dim, num_instances, temperature=0.05): super(NormSoftmaxLoss, self).__init__() self.weight = Parameter(torch.Tensor(num_instances, dim)) stdv = 1.0 / math.sqrt(self.weight.size(1)) self.weight.data.uniform_(-stdv, stdv) self.temperature = temperature self.loss_fn = nn.CrossEntropyLoss() def forward(self, embeddings, instance_targets): norm_weight = nn.functional.normalize(self.weight, dim=1) prediction_logits = nn.functional.linear(embeddings, norm_weight) loss = self.loss_fn(prediction_logits / self.temperature, instance_targets) return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'dim': 4, 'num_instances': 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 from torch.nn import Parameter assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_div_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = libdevice.sqrt(tmp11) tmp13 = 1e-12 tmp14 = triton_helpers.maximum(tmp12, tmp13) tmp15 = tmp0 / tmp14 tl.store(out_ptr0 + x2, tmp15, xmask) @triton.jit def triton_poi_fused_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) tmp3 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp8 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp11 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp1 = 1.0 tmp2 = tmp0 * tmp1 tmp4 = tmp3 * tmp1 tmp6 = tmp5 * tmp1 tmp7 = triton_helpers.maximum(tmp4, tmp6) tmp9 = tmp8 * tmp1 tmp10 = triton_helpers.maximum(tmp7, tmp9) tmp12 = tmp11 * tmp1 tmp13 = triton_helpers.maximum(tmp10, tmp12) tmp14 = tmp2 - tmp13 tmp15 = 20.0 tmp16 = tmp14 * tmp15 tl.store(out_ptr0 + x3, tmp16, xmask) @triton.jit def triton_per_fused__log_softmax_div_mul_neg_sum_2(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' ) tmp3 = tl.load(in_ptr0 + (16 + r0 + 64 * r2), None, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (32 + r0 + 64 * r2), None, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr0 + (48 + r0 + 64 * r2), None, eviction_policy= 'evict_last') tmp14 = tl.load(in_ptr1 + r3, None) tmp2 = tl_math.exp(tmp1) tmp4 = tl_math.exp(tmp3) tmp5 = tmp2 + tmp4 tmp7 = tl_math.exp(tmp6) tmp8 = tmp5 + tmp7 tmp10 = tl_math.exp(tmp9) tmp11 = tmp8 + tmp10 tmp12 = tl_math.log(tmp11) tmp13 = tmp0 - tmp12 tmp15 = tmp13 * tmp14 tmp16 = tl.broadcast_to(tmp15, [RBLOCK]) tmp18 = triton_helpers.promote_to_tensor(tl.sum(tmp16, 0)) tmp19 = -tmp18 tmp20 = 0.015625 tmp21 = tmp19 * tmp20 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp21, None) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4, 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_div_0[grid(16)](primals_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_2, (64, 4), (4, 1), 0), reinterpret_tensor(buf0, (4, 4), (1, 4), 0), out=buf1) del buf0 buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_1[grid(256)](buf1, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) buf3 = empty_strided_cuda((), (), torch.float32) buf4 = buf3 del buf3 triton_per_fused__log_softmax_div_mul_neg_sum_2[grid(1)](buf4, buf2, primals_3, 1, 256, num_warps=2, num_stages=1) del buf2 return buf4, primals_1, primals_3, reinterpret_tensor(primals_2, (64, 4 ), (4, 1), 0), buf1 class NormSoftmaxLossNew(nn.Module): """ L2 normalize weights and apply temperature scaling on logits. """ def __init__(self, dim, num_instances, temperature=0.05): super(NormSoftmaxLossNew, self).__init__() self.weight = Parameter(torch.Tensor(num_instances, dim)) stdv = 1.0 / math.sqrt(self.weight.size(1)) self.weight.data.uniform_(-stdv, stdv) self.temperature = temperature self.loss_fn = nn.CrossEntropyLoss() def forward(self, input_0, input_1): primals_1 = self.weight primals_2 = input_0 primals_3 = input_1 output = call([primals_1, primals_2, primals_3]) return output[0]
kikaitech/classification_metric_learning
NormSoftmaxLoss
false
15,847
[ "Apache-2.0" ]
93
6c90cecf8be01eda6efb7f6aa4049d8449ca33f1
https://github.com/kikaitech/classification_metric_learning/tree/6c90cecf8be01eda6efb7f6aa4049d8449ca33f1
Recon_Block
import torch from torch import nn class Recon_Block(nn.Module): def __init__(self, num_chans=64): super(Recon_Block, self).__init__() bias = True self.conv1 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride= 1, padding=1, bias=bias) self.relu2 = nn.PReLU() self.conv3 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride= 1, padding=1, bias=bias) self.relu4 = nn.PReLU() self.conv5 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride= 1, padding=1, bias=bias) self.relu6 = nn.PReLU() self.conv7 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride= 1, padding=1, bias=bias) self.relu8 = nn.PReLU() self.conv9 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride= 1, padding=1, bias=bias) self.relu10 = nn.PReLU() self.conv11 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride =1, padding=1, bias=bias) self.relu12 = nn.PReLU() self.conv13 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride =1, padding=1, bias=bias) self.relu14 = nn.PReLU() self.conv15 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride =1, padding=1, bias=bias) self.relu16 = nn.PReLU() self.conv17 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride =1, padding=1, bias=bias) def forward(self, x): res1 = x output = self.relu4(self.conv3(self.relu2(self.conv1(x)))) output = torch.add(output, res1) res2 = output output = self.relu8(self.conv7(self.relu6(self.conv5(output)))) output = torch.add(output, res2) res3 = output output = self.relu12(self.conv11(self.relu10(self.conv9(output)))) output = torch.add(output, res3) res4 = output output = self.relu16(self.conv15(self.relu14(self.conv13(output)))) output = torch.add(output, res4) output = self.conv17(output) output = torch.add(output, res1) return output def get_inputs(): return [torch.rand([4, 64, 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 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__prelu_kernel_convolution_0(in_out_ptr0, 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 % 64 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + 0) tmp6 = tl.broadcast_to(tmp5, [XBLOCK]) tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp7 = tmp6 * tmp2 tmp8 = tl.where(tmp4, tmp2, tmp7) tl.store(in_out_ptr0 + x3, tmp2, None) tl.store(out_ptr0 + x3, tmp8, None) @triton.jit def triton_poi_fused__prelu_kernel_add_convolution_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 64 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + 0) tmp6 = tl.broadcast_to(tmp5, [XBLOCK]) tmp9 = tl.load(in_ptr2 + x3, None) tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp7 = tmp6 * tmp2 tmp8 = tl.where(tmp4, tmp2, tmp7) tmp10 = tmp8 + tmp9 tl.store(in_out_ptr0 + x3, tmp2, None) tl.store(out_ptr0 + x3, tmp10, None) @triton.jit def triton_poi_fused_add_convolution_2(in_out_ptr0, in_ptr0, in_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 % 64 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x3, None) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tl.store(in_out_ptr0 + x3, tmp4, None) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, 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) = args args.clear() assert_size_stride(primals_1, (4, 64, 64, 64), (262144, 4096, 64, 1)) assert_size_stride(primals_2, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_3, (64,), (1,)) assert_size_stride(primals_4, (1,), (1,)) assert_size_stride(primals_5, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_6, (64,), (1,)) assert_size_stride(primals_7, (1,), (1,)) assert_size_stride(primals_8, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_9, (64,), (1,)) assert_size_stride(primals_10, (1,), (1,)) assert_size_stride(primals_11, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_12, (64,), (1,)) assert_size_stride(primals_13, (1,), (1,)) assert_size_stride(primals_14, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_15, (64,), (1,)) assert_size_stride(primals_16, (1,), (1,)) assert_size_stride(primals_17, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_18, (64,), (1,)) assert_size_stride(primals_19, (1,), (1,)) assert_size_stride(primals_20, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_21, (64,), (1,)) assert_size_stride(primals_22, (1,), (1,)) assert_size_stride(primals_23, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_24, (64,), (1,)) assert_size_stride(primals_25, (1,), (1,)) assert_size_stride(primals_26, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_27, (64,), (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, 64, 64, 64), (262144, 4096, 64, 1)) buf1 = buf0 del buf0 buf2 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.float32) get_raw_stream(0) triton_poi_fused__prelu_kernel_convolution_0[grid(1048576)](buf1, primals_3, primals_4, buf2, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del primals_3 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, 64, 64, 64), (262144, 4096, 64, 1)) buf4 = buf3 del buf3 buf5 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.float32) triton_poi_fused__prelu_kernel_add_convolution_1[grid(1048576)](buf4, primals_6, primals_7, primals_1, buf5, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del primals_6 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, 64, 64, 64), (262144, 4096, 64, 1)) buf7 = buf6 del buf6 buf8 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.float32) triton_poi_fused__prelu_kernel_convolution_0[grid(1048576)](buf7, primals_9, primals_10, buf8, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del primals_9 buf9 = extern_kernels.convolution(buf8, primals_11, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf9, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf10 = buf9 del buf9 buf11 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.float32) triton_poi_fused__prelu_kernel_add_convolution_1[grid(1048576)](buf10, primals_12, primals_13, buf5, buf11, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del primals_12 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, 64, 64, 64), (262144, 4096, 64, 1)) buf13 = buf12 del buf12 buf14 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.float32) triton_poi_fused__prelu_kernel_convolution_0[grid(1048576)](buf13, primals_15, primals_16, buf14, 1048576, XBLOCK=1024, num_warps= 4, num_stages=1) del primals_15 buf15 = extern_kernels.convolution(buf14, primals_17, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf15, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf16 = buf15 del buf15 buf17 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.float32) triton_poi_fused__prelu_kernel_add_convolution_1[grid(1048576)](buf16, primals_18, primals_19, buf11, buf17, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del primals_18 buf18 = extern_kernels.convolution(buf17, primals_20, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf18, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf19 = buf18 del buf18 buf20 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.float32) triton_poi_fused__prelu_kernel_convolution_0[grid(1048576)](buf19, primals_21, primals_22, buf20, 1048576, XBLOCK=1024, num_warps= 4, num_stages=1) del primals_21 buf21 = extern_kernels.convolution(buf20, primals_23, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf21, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf22 = buf21 del buf21 buf23 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.float32) triton_poi_fused__prelu_kernel_add_convolution_1[grid(1048576)](buf22, primals_24, primals_25, buf17, buf23, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del primals_24 buf24 = extern_kernels.convolution(buf23, primals_26, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf24, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf25 = buf24 del buf24 triton_poi_fused_add_convolution_2[grid(1048576)](buf25, primals_27, primals_1, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del primals_27 return (buf25, primals_1, primals_2, primals_4, primals_5, primals_7, primals_8, primals_10, primals_11, primals_13, primals_14, primals_16, primals_17, primals_19, primals_20, primals_22, primals_23, primals_25, primals_26, buf1, buf2, buf4, buf5, buf7, buf8, buf10, buf11, buf13, buf14, buf16, buf17, buf19, buf20, buf22, buf23) class Recon_BlockNew(nn.Module): def __init__(self, num_chans=64): super(Recon_BlockNew, self).__init__() bias = True self.conv1 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride= 1, padding=1, bias=bias) self.relu2 = nn.PReLU() self.conv3 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride= 1, padding=1, bias=bias) self.relu4 = nn.PReLU() self.conv5 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride= 1, padding=1, bias=bias) self.relu6 = nn.PReLU() self.conv7 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride= 1, padding=1, bias=bias) self.relu8 = nn.PReLU() self.conv9 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride= 1, padding=1, bias=bias) self.relu10 = nn.PReLU() self.conv11 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride =1, padding=1, bias=bias) self.relu12 = nn.PReLU() self.conv13 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride =1, padding=1, bias=bias) self.relu14 = nn.PReLU() self.conv15 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride =1, padding=1, bias=bias) self.relu16 = nn.PReLU() self.conv17 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride =1, padding=1, bias=bias) def forward(self, input_0): primals_2 = self.conv1.weight primals_3 = self.conv1.bias primals_4 = self.relu2.weight primals_5 = self.conv3.weight primals_6 = self.conv3.bias primals_7 = self.relu4.weight primals_8 = self.conv5.weight primals_9 = self.conv5.bias primals_10 = self.relu6.weight primals_11 = self.conv7.weight primals_12 = self.conv7.bias primals_13 = self.relu8.weight primals_14 = self.conv9.weight primals_15 = self.conv9.bias primals_16 = self.relu10.weight primals_17 = self.conv11.weight primals_18 = self.conv11.bias primals_19 = self.relu12.weight primals_20 = self.conv13.weight primals_21 = self.conv13.bias primals_22 = self.relu14.weight primals_23 = self.conv15.weight primals_24 = self.conv15.bias primals_25 = self.relu16.weight primals_26 = self.conv17.weight primals_27 = self.conv17.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16, primals_17, primals_18, primals_19, primals_20, primals_21, primals_22, primals_23, primals_24, primals_25, primals_26, primals_27]) return output[0]
khammernik/sigmanet
Recon_Block
false
15,848
[ "MIT" ]
50
6eb8dbd1ee350bb9baee60eb254080f7d660bbc5
https://github.com/khammernik/sigmanet/tree/6eb8dbd1ee350bb9baee60eb254080f7d660bbc5
GaussianLayer
import torch import torch.nn as nn class GaussianLayer(nn.Module): def __init__(self, std, device): super().__init__() self.std = std self.device = device def forward(self, x): return x + self.std * torch.randn_like(x) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'std': 4, 'device': 0}]
import torch from torch import device 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 @triton.jit def triton_poi_fused_add_mul_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_out_ptr0 + x0, xmask) tmp2 = 4.0 tmp3 = tmp1 * tmp2 tmp4 = tmp0 + tmp3 tl.store(in_out_ptr0 + x0, tmp4, 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 = torch.ops.aten.randn.default([4, 4, 4, 4], dtype=torch. float32, device=device(type='cuda', index=0), pin_memory=False) buf1 = buf0 del buf0 buf2 = buf1 del buf1 get_raw_stream(0) triton_poi_fused_add_mul_0[grid(256)](buf2, arg0_1, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf2, class GaussianLayerNew(nn.Module): def __init__(self, std, device): super().__init__() self.std = std self.device = device def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
krylea/mine-pytorch
GaussianLayer
false
15,849
[ "MIT" ]
108
a638ca3e46ff21a3b9dfebe25480eaed0e3304bc
https://github.com/krylea/mine-pytorch/tree/a638ca3e46ff21a3b9dfebe25480eaed0e3304bc
Expansion2D
import torch class Expansion2D(torch.nn.Module): """ Expands a tensor in the last two dimensions, effectively to a coarse grid of smaller grids. """ def __init__(self, expsize1: 'int', expsize2: 'int'): """ :param expsize1: size of the second last dimension to be created :param expsize2: size of the last dimension to be created """ super().__init__() self.expsize1 = expsize1 self.expsize2 = expsize2 def forward(self, input: 'torch.Tensor') ->torch.Tensor: """ :param input: input tensor :returns: output tensor """ shape = list(input.shape) newshape = shape[:-2] + [shape[-2] // self.expsize1, shape[-1] // self.expsize2, self.expsize1, self.expsize2] sliceshape = list(newshape) sliceshape[-4] = 1 sliceshape[-3] = 1 output = torch.zeros(newshape, device=input.device) baseslice = [slice(None, None, 1) for _ in range(len(shape) - 2)] for i in range(shape[-2] // self.expsize1): for j in range(shape[-1] // self.expsize2): inslice = tuple(baseslice + [slice(self.expsize1 * i, self. expsize1 * (i + 1)), slice(self.expsize2 * j, self. expsize2 * (j + 1))]) outslice = tuple(baseslice + [i, j, slice(None, None, 1), slice(None, None, 1)]) output[outslice] += input[inslice] return output def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'expsize1': 4, 'expsize2': 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 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_zeros_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 tmp2 = tl.load(in_ptr0 + x0, xmask) tmp0 = tl.full([1], 0, tl.int32) tmp1 = tmp0 == tmp0 tmp3 = 0.0 tmp4 = tmp3 + tmp2 tmp5 = tl.where(tmp1, tmp4, tmp3) tmp6 = tl.where(tmp1, tmp5, tmp3) tmp7 = tl.where(tmp1, tmp6, tmp6) tmp8 = tl.where(tmp1, tmp7, tmp6) tl.store(out_ptr0 + x0, tmp8, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 1, 1, 4, 4), (64, 16, 16, 16, 4, 1 ), torch.float32) get_raw_stream(0) triton_poi_fused_add_zeros_0[grid(256)](arg0_1, buf0, 256, XBLOCK= 256, num_warps=4, num_stages=1) del arg0_1 return buf0, class Expansion2DNew(torch.nn.Module): """ Expands a tensor in the last two dimensions, effectively to a coarse grid of smaller grids. """ def __init__(self, expsize1: 'int', expsize2: 'int'): """ :param expsize1: size of the second last dimension to be created :param expsize2: size of the last dimension to be created """ super().__init__() self.expsize1 = expsize1 self.expsize2 = expsize2 def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
kpoeppel/pytorch_probgraph
Expansion2D
false
15,850
[ "BSD-3-Clause" ]
47
b78595ab03bbe92595ad2f6b35f5dd8bf84d6da0
https://github.com/kpoeppel/pytorch_probgraph/tree/b78595ab03bbe92595ad2f6b35f5dd8bf84d6da0
Projection
import torch from typing import Tuple class Projection(torch.nn.Module): """ | A class for a projection of an input to a different shape effectively mapping from | [..., inshape[1] .. inshape[-1]] -> [..., outshape[1] .. outshape[-1]] | only going over the subelements. | Example input (4,6) to (4,5) (shapes): | with instart (0, 1) inend (4, 5) outstart (0, 0), outend (4, 4) maps essentially input[:, 1:5] to a new tensor output[:4, 0:4] with shape (4, 5) | Non-indexed elements in the output are set to zero. """ def __init__(self, instart: 'Tuple[int]', inend: 'Tuple[int]', inshape: 'Tuple[int]', outstart: 'Tuple[int]', outend: 'Tuple[int]', outshape: 'Tuple[int]'): """ :param instart: List of start indices of different dimensions in input :param inend: End indices (exclusive) in input :param inshape: Real input shapes (dimension sizes) :param outstart: List of start indices of different dimensions in output :param outend: End indices (exclusive) in output :param outshape: Real output shapes (dimension sizes) """ super().__init__() self.inindex = tuple([slice(instart[i], inend[i], 1) for i in range (len(inshape))]) self.outindex = tuple([slice(outstart[i], outend[i], 1) for i in range(len(outshape))]) self.inshape = inshape self.outshape = outshape def forward(self, input: 'torch.Tensor') ->torch.Tensor: """ :param input: Input tensor :returns: output tensor """ inindex = [slice(None, None, 1) for _ in range(len(input.shape) - len(self.inshape))] outindex = inindex inindex = tuple(inindex + list(self.inindex)) outindex = tuple(outindex + list(self.outindex)) outshape = [input.shape[i] for i in range(len(input.shape) - len( self.inshape))] outshape += self.outshape output = torch.zeros(outshape, device=input.device, requires_grad=False ) output[outindex] += input[inindex] return output def backward(self, output: 'torch.Tensor') ->torch.Tensor: """ :param output: output tensor to backward through module :returns: input gradient """ outindex = [slice(None, None, 1) for _ in range(len(output.shape) - len(self.outshape))] inindex = outindex outindex = tuple(outindex + list(self.outindex)) inindex = tuple(inindex + list(self.inindex)) inshape = [output.shape[i] for i in range(len(output.shape) - len( self.inshape))] inshape += self.inshape input = torch.zeros(inshape, device=output.device, requires_grad= input.requires_grad) input[inindex] += output[outindex] return input def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'instart': [4, 4], 'inend': [4, 4], 'inshape': [4, 4], 'outstart': [4, 4], 'outend': [4, 4], 'outshape': [4, 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 typing import Tuple 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_zeros_0(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 % 4 x3 = xindex tmp0 = x1 tmp1 = tl.full([1], 4, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = float('nan') tmp4 = tl.full(tmp3.shape, 0.0, tmp3.dtype) tmp5 = tl.where(tmp2, tmp3, tmp4) tmp6 = 0.0 tmp7 = tl.where(tmp2, tmp5, tmp6) tmp8 = tl.where(tmp2, tmp5, tmp7) tl.store(out_ptr0 + x3, tmp8, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_zeros_0[grid(256)](buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) return buf0, class ProjectionNew(torch.nn.Module): """ | A class for a projection of an input to a different shape effectively mapping from | [..., inshape[1] .. inshape[-1]] -> [..., outshape[1] .. outshape[-1]] | only going over the subelements. | Example input (4,6) to (4,5) (shapes): | with instart (0, 1) inend (4, 5) outstart (0, 0), outend (4, 4) maps essentially input[:, 1:5] to a new tensor output[:4, 0:4] with shape (4, 5) | Non-indexed elements in the output are set to zero. """ def __init__(self, instart: 'Tuple[int]', inend: 'Tuple[int]', inshape: 'Tuple[int]', outstart: 'Tuple[int]', outend: 'Tuple[int]', outshape: 'Tuple[int]'): """ :param instart: List of start indices of different dimensions in input :param inend: End indices (exclusive) in input :param inshape: Real input shapes (dimension sizes) :param outstart: List of start indices of different dimensions in output :param outend: End indices (exclusive) in output :param outshape: Real output shapes (dimension sizes) """ super().__init__() self.inindex = tuple([slice(instart[i], inend[i], 1) for i in range (len(inshape))]) self.outindex = tuple([slice(outstart[i], outend[i], 1) for i in range(len(outshape))]) self.inshape = inshape self.outshape = outshape def backward(self, output: 'torch.Tensor') ->torch.Tensor: """ :param output: output tensor to backward through module :returns: input gradient """ outindex = [slice(None, None, 1) for _ in range(len(output.shape) - len(self.outshape))] inindex = outindex outindex = tuple(outindex + list(self.outindex)) inindex = tuple(inindex + list(self.inindex)) inshape = [output.shape[i] for i in range(len(output.shape) - len( self.inshape))] inshape += self.inshape input = torch.zeros(inshape, device=output.device, requires_grad= input.requires_grad) input[inindex] += output[outindex] return input def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
kpoeppel/pytorch_probgraph
Projection
false
15,851
[ "BSD-3-Clause" ]
47
b78595ab03bbe92595ad2f6b35f5dd8bf84d6da0
https://github.com/kpoeppel/pytorch_probgraph/tree/b78595ab03bbe92595ad2f6b35f5dd8bf84d6da0
AngleSimpleLinear
import torch import torch.nn.parallel import torch.optim import torch.utils.data.distributed import torch.nn as nn from torch.nn import Parameter import torch.nn.functional as F class AngleSimpleLinear(nn.Module): """Computes cos of angles between input vectors and weights vectors""" def __init__(self, in_features, out_features): super().__init__() self.in_features = in_features self.out_features = out_features self.weight = Parameter(torch.Tensor(in_features, out_features)) self.weight.data.normal_().renorm_(2, 1, 1e-05).mul_(100000.0) def forward(self, x): cos_theta = F.normalize(x.view(x.shape[0], -1), dim=1).mm(F. normalize(self.weight, p=2, dim=0)) return cos_theta.clamp(-1, 1) def get_centers(self): return torch.t(self.weight) 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._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn.parallel import torch.optim import torch.utils.data.distributed import torch.nn as nn from torch.nn import Parameter assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_div_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = libdevice.sqrt(tmp11) tmp13 = 1e-12 tmp14 = triton_helpers.maximum(tmp12, tmp13) tmp15 = tmp0 / tmp14 tl.store(out_ptr0 + x2, tmp15, xmask) @triton.jit def triton_poi_fused_div_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (4 + x0), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (8 + x0), xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (12 + x0), xmask, eviction_policy='evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = libdevice.sqrt(tmp11) tmp13 = 1e-12 tmp14 = triton_helpers.maximum(tmp12, tmp13) tmp15 = tmp0 / tmp14 tl.store(out_ptr0 + x2, tmp15, xmask) @triton.jit def triton_poi_fused_clamp_ge_le_logical_and_2(in_ptr0, 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 + x0, xmask) tmp1 = -1.0 tmp2 = triton_helpers.maximum(tmp0, tmp1) tmp3 = 1.0 tmp4 = triton_helpers.minimum(tmp2, tmp3) tmp5 = tmp0 >= tmp1 tmp6 = tmp0 <= tmp3 tmp7 = tmp5 & tmp6 tl.store(out_ptr0 + x0, tmp4, xmask) tl.store(out_ptr1 + x0, tmp7, xmask) def call(args): primals_1, primals_2 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_div_0[grid(16)](primals_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_1 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_div_1[grid(16)](primals_2, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf0, buf1, out=buf2) buf3 = buf1 del buf1 buf4 = empty_strided_cuda((4, 4), (4, 1), torch.bool) triton_poi_fused_clamp_ge_le_logical_and_2[grid(16)](buf2, buf3, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf2 return buf3, primals_2, buf4, reinterpret_tensor(buf0, (4, 4), (1, 4), 0) class AngleSimpleLinearNew(nn.Module): """Computes cos of angles between input vectors and weights vectors""" def __init__(self, in_features, out_features): super().__init__() self.in_features = in_features self.out_features = out_features self.weight = Parameter(torch.Tensor(in_features, out_features)) self.weight.data.normal_().renorm_(2, 1, 1e-05).mul_(100000.0) def get_centers(self): return torch.t(self.weight) def forward(self, input_0): primals_1 = self.weight primals_2 = input_0 output = call([primals_1, primals_2]) return output[0]
kprokofi/ML_Decoder
AngleSimpleLinear
false
15,852
[ "MIT" ]
99
c01c50e0165e607afbebd8d615708ef9c084dd5b
https://github.com/kprokofi/ML_Decoder/tree/c01c50e0165e607afbebd8d615708ef9c084dd5b
MultiHeadAttention
import torch import torch.nn as nn class MultiHeadAttention(nn.Module): def __init__(self, num_q_channels: 'int', num_kv_channels: 'int', num_heads: 'int', dropout: 'float'): super().__init__() self.attention = nn.MultiheadAttention(embed_dim=num_q_channels, num_heads=num_heads, kdim=num_kv_channels, vdim=num_kv_channels, dropout=dropout, batch_first=True) def forward(self, x_q, x_kv, pad_mask=None, attn_mask=None): return self.attention(x_q, x_kv, x_kv, key_padding_mask=pad_mask, attn_mask=attn_mask)[0] def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'num_q_channels': 4, 'num_kv_channels': 4, 'num_heads': 4, 'dropout': 0.5}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime 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_mul_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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 = 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_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = 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 = 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 x1 = xindex y0 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x1), xmask & ymask) tl.store(out_ptr0 + (x1 + 4 * y0), tmp0, xmask & ymask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (12, 4), (4, 1)) assert_size_stride(primals_4, (12,), (1,)) assert_size_stride(primals_5, (4, 4), (4, 1)) assert_size_stride(primals_6, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf0) buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(reinterpret_tensor(primals_4, (4,), (1,), 4), primals_2, reinterpret_tensor(primals_3, (4, 4), (1, 4), 16), alpha=1, beta=1, out=buf1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(reinterpret_tensor(primals_4, (4,), (1,), 8), primals_2, reinterpret_tensor(primals_3, (4, 4), (1, 4), 32), alpha=1, beta=1, out=buf2) del primals_3 buf3 = reinterpret_tensor(buf0, (4, 4, 1), (1, 4, 16), 0) del buf0 get_raw_stream(0) triton_poi_fused_mul_0[grid(16)](buf3, primals_4, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_4 buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf3, reinterpret_tensor(buf1, (4, 1, 4), (1, 1, 4), 0), out=buf4) buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_1[grid(64)](buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf6 = buf4 del buf4 triton_poi_fused__softmax_2[grid(64)](buf5, buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf5 buf7 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) extern_kernels.bmm(buf6, reinterpret_tensor(buf2, (4, 4, 1), (1, 4, 1), 0), out=buf7) buf8 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) triton_poi_fused_clone_3[grid(4, 4)](buf7, buf8, 4, 4, XBLOCK=4, YBLOCK=4, num_warps=1, num_stages=1) buf9 = reinterpret_tensor(buf7, (4, 4), (4, 1), 0) del buf7 extern_kernels.addmm(primals_6, reinterpret_tensor(buf8, (4, 4), (4, 1), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), alpha =1, beta=1, out=buf9) del primals_6 return buf9, primals_1, primals_2, buf6, reinterpret_tensor(buf8, (4, 4 ), (4, 1), 0), primals_5, reinterpret_tensor(buf2, (4, 1, 4), (1, 1, 4), 0), reinterpret_tensor(buf3, (4, 1, 4), (1, 1, 4), 0 ), reinterpret_tensor(buf1, (4, 4, 1), (1, 4, 1), 0) class MultiHeadAttentionNew(nn.Module): def __init__(self, num_q_channels: 'int', num_kv_channels: 'int', num_heads: 'int', dropout: 'float'): super().__init__() self.attention = nn.MultiheadAttention(embed_dim=num_q_channels, num_heads=num_heads, kdim=num_kv_channels, vdim=num_kv_channels, dropout=dropout, batch_first=True) def forward(self, input_0, input_1): primals_3 = self.attention.in_proj_weight primals_4 = self.attention.in_proj_bias primals_1 = self.attention.out_proj.weight primals_6 = self.attention.out_proj.bias primals_2 = input_0 primals_5 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6]) return output[0]
krasserm/perceiver-io
MultiHeadAttention
false
15,853
[ "Apache-2.0" ]
133
16e1029300304b617c0b0ae8eb06129ec103c755
https://github.com/krasserm/perceiver-io/tree/16e1029300304b617c0b0ae8eb06129ec103c755
SoftInvDiceLoss
import torch import torch.nn as nn import torch.nn.functional as F class SoftInvDiceLoss(nn.Module): def __init__(self, weight=None, size_average=True): super(SoftInvDiceLoss, self).__init__() def forward(self, logits, targets): smooth = 1.0 logits = F.sigmoid(logits) iflat = 1 - logits.view(-1) tflat = 1 - targets.view(-1) intersection = (iflat * tflat).sum() return 1 - (2.0 * intersection + smooth) / (iflat.sum() + tflat.sum () + smooth) 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) tmp4 = tl.load(in_ptr1 + r0, None) tmp1 = tl.sigmoid(tmp0) tmp2 = 1.0 tmp3 = tmp2 - tmp1 tmp5 = tmp2 - tmp4 tmp6 = tmp3 * tmp5 tmp7 = tl.broadcast_to(tmp6, [RBLOCK]) tmp9 = triton_helpers.promote_to_tensor(tl.sum(tmp7, 0)) tmp10 = tl.broadcast_to(tmp3, [RBLOCK]) tmp12 = triton_helpers.promote_to_tensor(tl.sum(tmp10, 0)) tmp13 = tl.broadcast_to(tmp5, [RBLOCK]) tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0)) tmp16 = 2.0 tmp17 = tmp9 * tmp16 tmp18 = tmp17 + tmp2 tmp19 = tmp12 + tmp15 tmp20 = tmp19 + tmp2 tmp21 = tmp18 / tmp20 tmp22 = tmp2 - tmp21 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp22, 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 SoftInvDiceLossNew(nn.Module): def __init__(self, weight=None, size_average=True): super(SoftInvDiceLossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kryptonite0/Global_Convolutional_Network
SoftInvDiceLoss
false
15,854
[ "MIT" ]
88
33de71bbe468f485eb38345f4982923945d1a0be
https://github.com/kryptonite0/Global_Convolutional_Network/tree/33de71bbe468f485eb38345f4982923945d1a0be
SelfAttention
import torch import torch.nn as nn class MultiHeadAttention(nn.Module): def __init__(self, num_q_channels: 'int', num_kv_channels: 'int', num_heads: 'int', dropout: 'float'): super().__init__() self.attention = nn.MultiheadAttention(embed_dim=num_q_channels, num_heads=num_heads, kdim=num_kv_channels, vdim=num_kv_channels, dropout=dropout, batch_first=True) def forward(self, x_q, x_kv, pad_mask=None, attn_mask=None): return self.attention(x_q, x_kv, x_kv, key_padding_mask=pad_mask, attn_mask=attn_mask)[0] class SelfAttention(nn.Module): def __init__(self, num_channels: 'int', num_heads: 'int', dropout: 'float' ): super().__init__() self.norm = nn.LayerNorm(num_channels) self.attention = MultiHeadAttention(num_q_channels=num_channels, num_kv_channels=num_channels, num_heads=num_heads, dropout=dropout) def forward(self, x, pad_mask=None, attn_mask=None): x = self.norm(x) return self.attention(x, x, pad_mask=pad_mask, attn_mask=attn_mask) def get_inputs(): return [torch.rand([4, 4])] def get_init_inputs(): return [[], {'num_channels': 4, 'num_heads': 4, 'dropout': 0.5}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_native_layer_norm_0(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tmp9 = tmp0 - tmp8 tmp10 = tmp9 * tmp9 tmp11 = tmp1 - tmp8 tmp12 = tmp11 * tmp11 tmp13 = tmp10 + tmp12 tmp14 = tmp3 - tmp8 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp8 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp19 / tmp7 tmp21 = 1e-05 tmp22 = tmp20 + tmp21 tmp23 = libdevice.rsqrt(tmp22) tl.store(out_ptr0 + x0, tmp8, xmask) tl.store(out_ptr1 + x0, tmp23, xmask) @triton.jit def triton_poi_fused_native_layer_norm_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, 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 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_mul_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 tmp3 = 1.0 tmp4 = tmp2 * tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_clone_5(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 4 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 x1 = xindex y0 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x1), xmask & ymask) tl.store(out_ptr0 + (x1 + 4 * y0), tmp0, 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,), (1,)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4), (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((4, 1), (1, 4), torch.float32) buf1 = empty_strided_cuda((4, 1), (1, 4), torch.float32) get_raw_stream(0) triton_poi_fused_native_layer_norm_0[grid(4)](primals_3, buf0, buf1, 4, XBLOCK=4, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_native_layer_norm_1[grid(16)](primals_3, buf0, buf1, primals_1, primals_2, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf0 del buf1 del primals_1 del primals_2 buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf2, reinterpret_tensor(primals_4, (4, 4), (1, 4 ), 0), out=buf3) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(reinterpret_tensor(primals_5, (4,), (1,), 4), buf2, reinterpret_tensor(primals_4, (4, 4), (1, 4), 16), alpha= 1, beta=1, out=buf4) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(reinterpret_tensor(primals_5, (4,), (1,), 8), buf2, reinterpret_tensor(primals_4, (4, 4), (1, 4), 32), alpha= 1, beta=1, out=buf5) buf6 = reinterpret_tensor(buf3, (4, 4, 1), (1, 4, 16), 0) del buf3 triton_poi_fused_mul_2[grid(16)](buf6, primals_5, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_5 buf7 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf6, reinterpret_tensor(buf4, (4, 1, 4), (1, 1, 4), 0), out=buf7) buf8 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_3[grid(64)](buf7, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1) buf9 = buf7 del buf7 triton_poi_fused__softmax_4[grid(64)](buf8, buf9, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf8 buf10 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) extern_kernels.bmm(buf9, reinterpret_tensor(buf5, (4, 4, 1), (1, 4, 1), 0), out=buf10) buf11 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) triton_poi_fused_clone_5[grid(4, 4)](buf10, buf11, 4, 4, XBLOCK=4, YBLOCK=4, num_warps=1, num_stages=1) buf12 = reinterpret_tensor(buf10, (4, 4), (4, 1), 0) del buf10 extern_kernels.addmm(primals_7, reinterpret_tensor(buf11, (4, 4), ( 4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf12) del primals_7 return buf12, primals_3, buf2, buf9, reinterpret_tensor(buf11, (4, 4), (4, 1), 0), primals_6, reinterpret_tensor(buf5, (4, 1, 4), (1, 1, 4), 0 ), reinterpret_tensor(buf6, (4, 1, 4), (1, 1, 4), 0 ), reinterpret_tensor(buf4, (4, 4, 1), (1, 4, 1), 0 ), reinterpret_tensor(primals_4, (4, 4), (4, 1), 32 ), reinterpret_tensor(primals_4, (4, 4), (4, 1), 16 ), reinterpret_tensor(primals_4, (4, 4), (4, 1), 0) class MultiHeadAttention(nn.Module): def __init__(self, num_q_channels: 'int', num_kv_channels: 'int', num_heads: 'int', dropout: 'float'): super().__init__() self.attention = nn.MultiheadAttention(embed_dim=num_q_channels, num_heads=num_heads, kdim=num_kv_channels, vdim=num_kv_channels, dropout=dropout, batch_first=True) def forward(self, x_q, x_kv, pad_mask=None, attn_mask=None): return self.attention(x_q, x_kv, x_kv, key_padding_mask=pad_mask, attn_mask=attn_mask)[0] class SelfAttentionNew(nn.Module): def __init__(self, num_channels: 'int', num_heads: 'int', dropout: 'float' ): super().__init__() self.norm = nn.LayerNorm(num_channels) self.attention = MultiHeadAttention(num_q_channels=num_channels, num_kv_channels=num_channels, num_heads=num_heads, dropout=dropout) def forward(self, input_0): primals_1 = self.norm.weight primals_2 = self.norm.bias primals_4 = self.attention.attention.in_proj_weight primals_5 = self.attention.attention.in_proj_bias primals_3 = self.attention.attention.out_proj.weight primals_7 = self.attention.attention.out_proj.bias primals_6 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
krasserm/perceiver-io
SelfAttention
false
15,855
[ "Apache-2.0" ]
133
16e1029300304b617c0b0ae8eb06129ec103c755
https://github.com/krasserm/perceiver-io/tree/16e1029300304b617c0b0ae8eb06129ec103c755
TransitionUp
import torch from torch import nn import torch.onnx import torch.nn.functional as F import torch.utils.data class TransitionUp(nn.Module): def __init__(self, in_channels, out_channels): super().__init__() def forward(self, x, skip, concat=True): out = F.interpolate(x, size=(skip.size(2), skip.size(3)), mode= 'bilinear', align_corners=True) if concat: out = torch.cat([out, skip], 1) return out def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn import torch.onnx 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__to_copy__unsafe_index_add_arange_clamp_mul_sub_0(in_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 % 4 x0 = xindex % 4 x2 = xindex // 16 x4 = xindex // 64 x7 = xindex % 64 tmp0 = x1 tmp1 = tmp0.to(tl.float32) tmp2 = 1.0 tmp3 = tmp1 * tmp2 tmp4 = 0.0 tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp6 = tmp5.to(tl.int32) tmp7 = tl.full([1], 1, tl.int64) tmp8 = tmp6 + tmp7 tmp9 = tl.full([1], 3, tl.int64) tmp10 = triton_helpers.minimum(tmp8, tmp9) tmp11 = x0 tmp12 = tmp11.to(tl.float32) tmp13 = tmp12 * tmp2 tmp14 = triton_helpers.maximum(tmp13, tmp4) tmp15 = tmp14.to(tl.int32) tmp16 = tl.load(in_ptr0 + (tmp15 + 4 * tmp10 + 16 * x2), xmask, eviction_policy='evict_last') tmp17 = tmp15 + tmp7 tmp18 = triton_helpers.minimum(tmp17, tmp9) tmp19 = tl.load(in_ptr0 + (tmp18 + 4 * tmp10 + 16 * x2), xmask, eviction_policy='evict_last') tmp20 = tmp19 - tmp16 tmp21 = tmp15.to(tl.float32) tmp22 = tmp14 - tmp21 tmp23 = triton_helpers.maximum(tmp22, tmp4) tmp24 = triton_helpers.minimum(tmp23, tmp2) tmp25 = tmp20 * tmp24 tmp26 = tmp16 + tmp25 tmp27 = tl.load(in_ptr0 + (tmp15 + 4 * tmp6 + 16 * x2), xmask, eviction_policy='evict_last') tmp28 = tl.load(in_ptr0 + (tmp18 + 4 * tmp6 + 16 * x2), xmask, eviction_policy='evict_last') tmp29 = tmp28 - tmp27 tmp30 = tmp29 * tmp24 tmp31 = tmp27 + tmp30 tmp32 = tmp26 - tmp31 tmp33 = tmp6.to(tl.float32) tmp34 = tmp5 - tmp33 tmp35 = triton_helpers.maximum(tmp34, tmp4) tmp36 = triton_helpers.minimum(tmp35, tmp2) tmp37 = tmp32 * tmp36 tmp38 = tmp31 + tmp37 tl.store(out_ptr1 + (x7 + 128 * x4), tmp38, xmask) @triton.jit def triton_poi_fused_cat_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 x0 = xindex % 64 x1 = xindex // 64 tmp0 = tl.load(in_ptr0 + x2, xmask) tl.store(out_ptr0 + (x0 + 128 * x1), tmp0, 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) buf3 = empty_strided_cuda((4, 8, 4, 4), (128, 16, 4, 1), torch.float32) buf1 = reinterpret_tensor(buf3, (4, 4, 4, 4), (128, 16, 4, 1), 0) get_raw_stream(0) triton_poi_fused__to_copy__unsafe_index_add_arange_clamp_mul_sub_0[grid (256)](arg1_1, buf1, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg1_1 buf2 = reinterpret_tensor(buf3, (4, 4, 4, 4), (128, 16, 4, 1), 64) triton_poi_fused_cat_1[grid(256)](arg0_1, buf2, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf3, class TransitionUpNew(nn.Module): def __init__(self, in_channels, out_channels): 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]
kuanhungchen/CenterNet-HarDNet
TransitionUp
false
15,856
[ "MIT" ]
164
050d55a532706d989105982c5bc10f1c89edc8d2
https://github.com/kuanhungchen/CenterNet-HarDNet/tree/050d55a532706d989105982c5bc10f1c89edc8d2
CrossAttention
import torch import torch.nn as nn class MultiHeadAttention(nn.Module): def __init__(self, num_q_channels: 'int', num_kv_channels: 'int', num_heads: 'int', dropout: 'float'): super().__init__() self.attention = nn.MultiheadAttention(embed_dim=num_q_channels, num_heads=num_heads, kdim=num_kv_channels, vdim=num_kv_channels, dropout=dropout, batch_first=True) def forward(self, x_q, x_kv, pad_mask=None, attn_mask=None): return self.attention(x_q, x_kv, x_kv, key_padding_mask=pad_mask, attn_mask=attn_mask)[0] class CrossAttention(nn.Module): def __init__(self, num_q_channels: 'int', num_kv_channels: 'int', num_heads: 'int', dropout: 'float'): super().__init__() self.q_norm = nn.LayerNorm(num_q_channels) self.kv_norm = nn.LayerNorm(num_kv_channels) self.attention = MultiHeadAttention(num_q_channels=num_q_channels, num_kv_channels=num_kv_channels, num_heads=num_heads, dropout= dropout) def forward(self, x_q, x_kv, pad_mask=None, attn_mask=None): x_q = self.q_norm(x_q) x_kv = self.kv_norm(x_kv) return self.attention(x_q, x_kv, pad_mask=pad_mask, attn_mask=attn_mask ) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'num_q_channels': 4, 'num_kv_channels': 4, 'num_heads': 4, 'dropout': 0.5}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_native_layer_norm_0(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tmp9 = tmp0 - tmp8 tmp10 = tmp9 * tmp9 tmp11 = tmp1 - tmp8 tmp12 = tmp11 * tmp11 tmp13 = tmp10 + tmp12 tmp14 = tmp3 - tmp8 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp8 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp19 / tmp7 tmp21 = 1e-05 tmp22 = tmp20 + tmp21 tmp23 = libdevice.rsqrt(tmp22) tl.store(out_ptr0 + x0, tmp8, xmask) tl.store(out_ptr1 + x0, tmp23, xmask) @triton.jit def triton_poi_fused_native_layer_norm_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, 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 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_mul_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 tmp3 = 1.0 tmp4 = tmp2 * tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_clone_5(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 4 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 x1 = xindex y0 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x1), xmask & ymask) tl.store(out_ptr0 + (x1 + 4 * y0), tmp0, 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) = 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, 1)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (12, 4), (4, 1)) assert_size_stride(primals_8, (12,), (1,)) assert_size_stride(primals_9, (4, 4), (4, 1)) assert_size_stride(primals_10, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 1), (1, 4), torch.float32) buf1 = empty_strided_cuda((4, 1), (1, 4), torch.float32) get_raw_stream(0) triton_poi_fused_native_layer_norm_0[grid(4)](primals_3, buf0, buf1, 4, XBLOCK=4, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 1), (1, 4), torch.float32) buf3 = empty_strided_cuda((4, 1), (1, 4), torch.float32) triton_poi_fused_native_layer_norm_0[grid(4)](primals_6, buf2, buf3, 4, XBLOCK=4, num_warps=1, num_stages=1) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_native_layer_norm_1[grid(16)](primals_3, buf0, buf1, primals_1, primals_2, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf0 del buf1 del primals_1 del primals_2 buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf4, reinterpret_tensor(primals_7, (4, 4), (1, 4 ), 0), out=buf5) buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_native_layer_norm_1[grid(16)](primals_6, buf2, buf3, primals_4, primals_5, buf6, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf2 del buf3 del primals_4 del primals_5 buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(reinterpret_tensor(primals_8, (4,), (1,), 4), buf6, reinterpret_tensor(primals_7, (4, 4), (1, 4), 16), alpha= 1, beta=1, out=buf7) buf8 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(reinterpret_tensor(primals_8, (4,), (1,), 8), buf6, reinterpret_tensor(primals_7, (4, 4), (1, 4), 32), alpha= 1, beta=1, out=buf8) buf9 = reinterpret_tensor(buf5, (4, 4, 1), (1, 4, 16), 0) del buf5 triton_poi_fused_mul_2[grid(16)](buf9, primals_8, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_8 buf10 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf9, reinterpret_tensor(buf7, (4, 1, 4), (1, 1, 4), 0), out=buf10) buf11 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_3[grid(64)](buf10, buf11, 64, XBLOCK=64, num_warps=1, num_stages=1) buf12 = buf10 del buf10 triton_poi_fused__softmax_4[grid(64)](buf11, buf12, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf11 buf13 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) extern_kernels.bmm(buf12, reinterpret_tensor(buf8, (4, 4, 1), (1, 4, 1), 0), out=buf13) buf14 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) triton_poi_fused_clone_5[grid(4, 4)](buf13, buf14, 4, 4, XBLOCK=4, YBLOCK=4, num_warps=1, num_stages=1) buf15 = reinterpret_tensor(buf13, (4, 4), (4, 1), 0) del buf13 extern_kernels.addmm(primals_10, reinterpret_tensor(buf14, (4, 4), (4, 1), 0), reinterpret_tensor(primals_9, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf15) del primals_10 return buf15, primals_3, primals_6, buf4, buf6, buf12, reinterpret_tensor( buf14, (4, 4), (4, 1), 0), primals_9, reinterpret_tensor(buf8, (4, 1, 4), (1, 1, 4), 0), reinterpret_tensor(buf9, (4, 1, 4), (1, 1, 4), 0 ), reinterpret_tensor(buf7, (4, 4, 1), (1, 4, 1), 0 ), reinterpret_tensor(primals_7, (4, 4), (4, 1), 32 ), reinterpret_tensor(primals_7, (4, 4), (4, 1), 16 ), reinterpret_tensor(primals_7, (4, 4), (4, 1), 0) class MultiHeadAttention(nn.Module): def __init__(self, num_q_channels: 'int', num_kv_channels: 'int', num_heads: 'int', dropout: 'float'): super().__init__() self.attention = nn.MultiheadAttention(embed_dim=num_q_channels, num_heads=num_heads, kdim=num_kv_channels, vdim=num_kv_channels, dropout=dropout, batch_first=True) def forward(self, x_q, x_kv, pad_mask=None, attn_mask=None): return self.attention(x_q, x_kv, x_kv, key_padding_mask=pad_mask, attn_mask=attn_mask)[0] class CrossAttentionNew(nn.Module): def __init__(self, num_q_channels: 'int', num_kv_channels: 'int', num_heads: 'int', dropout: 'float'): super().__init__() self.q_norm = nn.LayerNorm(num_q_channels) self.kv_norm = nn.LayerNorm(num_kv_channels) self.attention = MultiHeadAttention(num_q_channels=num_q_channels, num_kv_channels=num_kv_channels, num_heads=num_heads, dropout= dropout) def forward(self, input_0, input_1): primals_1 = self.q_norm.weight primals_2 = self.q_norm.bias primals_4 = self.kv_norm.weight primals_5 = self.kv_norm.bias primals_7 = self.attention.attention.in_proj_weight primals_8 = self.attention.attention.in_proj_bias primals_3 = self.attention.attention.out_proj.weight primals_10 = self.attention.attention.out_proj.bias primals_6 = input_0 primals_9 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10]) return output[0]
krasserm/perceiver-io
CrossAttention
false
15,857
[ "Apache-2.0" ]
133
16e1029300304b617c0b0ae8eb06129ec103c755
https://github.com/krasserm/perceiver-io/tree/16e1029300304b617c0b0ae8eb06129ec103c755
ResNetBlock
from torch.nn import Module import torch from torch.nn import Conv2d from torch.nn import InstanceNorm2d from torch.nn.init import kaiming_normal_ from torch.nn.init import xavier_normal_ from torch import relu def create_init_function(method: 'str'='none'): def init(module: 'Module'): if method == 'none': return module elif method == 'he': kaiming_normal_(module.weight) return module elif method == 'xavier': xavier_normal_(module.weight) return module else: raise ('Invalid initialization method %s' % method) return init def Conv3(in_channels: 'int', out_channels: 'int', initialization_method='he' ) ->Module: init = create_init_function(initialization_method) return init(Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False)) class ResNetBlock(Module): def __init__(self, num_channels: 'int', initialization_method: 'str'='he'): super().__init__() self.conv1 = Conv3(num_channels, num_channels, initialization_method) self.norm1 = InstanceNorm2d(num_features=num_channels, affine=True) self.conv2 = Conv3(num_channels, num_channels, initialization_method) self.norm2 = InstanceNorm2d(num_features=num_channels, affine=True) def forward(self, x): return x + self.norm2(self.conv2(relu(self.norm1(self.conv1(x))))) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice from torch.nn import Module from torch.nn import Conv2d from torch.nn import InstanceNorm2d from torch.nn.init import kaiming_normal_ from torch.nn.init import xavier_normal_ 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_relu_repeat_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, out_ptr3, out_ptr4, 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) x0 = xindex r1 = rindex x2 = xindex % 4 tmp0 = tl.load(in_ptr0 + x0 % 4, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + (r1 + 16 * x0), xmask, other=0.0) tmp26 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last') tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tl.where(xmask, tmp2, 0) tmp5 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp7 = tl.where(xmask, tmp5, 0) tmp8 = tl.sum(tmp7, 1)[:, None] tmp9 = tl.full([XBLOCK, 1], 16, tl.int32) tmp10 = tmp9.to(tl.float32) tmp11 = tmp8 / tmp10 tmp12 = tmp2 - tmp11 tmp13 = tmp12 * tmp12 tmp14 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK]) tmp16 = tl.where(xmask, tmp14, 0) tmp17 = tl.sum(tmp16, 1)[:, None] tmp18 = tmp1 - tmp11 tmp19 = 16.0 tmp20 = tmp17 / tmp19 tmp21 = 1e-05 tmp22 = tmp20 + tmp21 tmp23 = libdevice.rsqrt(tmp22) tmp24 = tmp18 * tmp23 tmp25 = tmp24 * tmp0 tmp27 = tmp25 + tmp26 tmp28 = tl.full([1, 1], 0, tl.int32) tmp29 = triton_helpers.maximum(tmp28, tmp27) tl.store(out_ptr0 + x0, tmp0, xmask) tl.store(out_ptr3 + (r1 + 16 * x0), tmp29, xmask) tl.store(out_ptr4 + x0, tmp23, xmask) tl.store(out_ptr1 + x0, tmp11, xmask) @triton.jit def triton_per_fused__native_batch_norm_legit_add_repeat_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, out_ptr1, out_ptr3, out_ptr4, 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) x0 = xindex r1 = rindex x2 = xindex % 4 tmp0 = tl.load(in_ptr0 + x0 % 4, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + (r1 + 16 * x0), xmask, other=0.0) tmp18 = tl.load(in_ptr2 + (r1 + 16 * x0), xmask, other=0.0) tmp27 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last') tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tl.where(xmask, tmp2, 0) tmp5 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp7 = tl.where(xmask, tmp5, 0) tmp8 = tl.sum(tmp7, 1)[:, None] tmp9 = tl.full([XBLOCK, 1], 16, tl.int32) tmp10 = tmp9.to(tl.float32) tmp11 = tmp8 / tmp10 tmp12 = tmp2 - tmp11 tmp13 = tmp12 * tmp12 tmp14 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK]) tmp16 = tl.where(xmask, tmp14, 0) tmp17 = tl.sum(tmp16, 1)[:, None] tmp19 = tmp1 - tmp11 tmp20 = 16.0 tmp21 = tmp17 / tmp20 tmp22 = 1e-05 tmp23 = tmp21 + tmp22 tmp24 = libdevice.rsqrt(tmp23) tmp25 = tmp19 * tmp24 tmp26 = tmp25 * tmp0 tmp28 = tmp26 + tmp27 tmp29 = tmp18 + tmp28 tl.store(out_ptr0 + x0, tmp0, xmask) tl.store(out_ptr3 + (r1 + 16 * x0), tmp29, xmask) tl.store(out_ptr4 + x0, tmp24, xmask) tl.store(out_ptr1 + x0, tmp11, 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, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4,), (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,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_2, primals_1, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1)) buf1 = empty_strided_cuda((16,), (1,), torch.float32) buf2 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32 ) buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf5 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32 ) get_raw_stream(0) triton_per_fused__native_batch_norm_legit_relu_repeat_0[grid(16)]( primals_3, buf0, primals_4, buf1, buf2, buf6, buf5, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) del primals_3 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 = empty_strided_cuda((16,), (1,), torch.float32) 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_repeat_1[grid(16)]( primals_6, buf7, primals_2, primals_7, buf8, buf9, buf13, buf12, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del primals_6 del primals_7 return (buf13, primals_1, primals_2, primals_5, buf0, buf1, reinterpret_tensor(buf5, (16,), (1,), 0), buf6, buf7, buf8, reinterpret_tensor(buf12, (16,), (1,), 0), reinterpret_tensor(buf9, (1, 16, 1, 1), (16, 1, 1, 1), 0), reinterpret_tensor(buf2, (1, 16, 1, 1), (16, 1, 1, 1), 0)) def create_init_function(method: 'str'='none'): def init(module: 'Module'): if method == 'none': return module elif method == 'he': kaiming_normal_(module.weight) return module elif method == 'xavier': xavier_normal_(module.weight) return module else: raise ('Invalid initialization method %s' % method) return init def Conv3(in_channels: 'int', out_channels: 'int', initialization_method='he' ) ->Module: init = create_init_function(initialization_method) return init(Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False)) class ResNetBlockNew(Module): def __init__(self, num_channels: 'int', initialization_method: 'str'='he'): super().__init__() self.conv1 = Conv3(num_channels, num_channels, initialization_method) self.norm1 = InstanceNorm2d(num_features=num_channels, affine=True) self.conv2 = Conv3(num_channels, num_channels, initialization_method) self.norm2 = InstanceNorm2d(num_features=num_channels, affine=True) def forward(self, input_0): primals_1 = self.conv1.weight primals_3 = self.norm1.weight primals_4 = self.norm1.bias primals_5 = self.conv2.weight primals_6 = self.norm2.weight primals_7 = self.norm2.bias primals_2 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
kongdongdien/talking-head-anime-demo
ResNetBlock
false
15,858
[ "MIT" ]
1,670
d66c27a341f7256e4a37c55493b93dc9e846b423
https://github.com/kongdongdien/talking-head-anime-demo/tree/d66c27a341f7256e4a37c55493b93dc9e846b423
PoswiseFeedForwardNet
from _paritybench_helpers import _mock_config import torch import torch.nn as nn import torch.nn.functional as F class PoswiseFeedForwardNet(nn.Module): def __init__(self, config): super().__init__() self.config = config self.conv1 = nn.Conv1d(in_channels=self.config.d_hidn, out_channels =self.config.d_ff, kernel_size=1) self.conv2 = nn.Conv1d(in_channels=self.config.d_ff, out_channels= self.config.d_hidn, kernel_size=1) self.active = F.gelu self.dropout = nn.Dropout(config.dropout) def forward(self, inputs): output = self.active(self.conv1(inputs.transpose(1, 2))) output = self.conv2(output).transpose(1, 2) output = self.dropout(output) return output def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'config': _mock_config(d_hidn=4, d_ff=4, dropout=0.5)}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.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_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_convolution_gelu_1(in_out_ptr0, 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 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 tmp3 = 0.5 tmp4 = tmp2 * tmp3 tmp5 = 0.7071067811865476 tmp6 = tmp2 * tmp5 tmp7 = libdevice.erf(tmp6) tmp8 = 1.0 tmp9 = tmp7 + tmp8 tmp10 = tmp4 * tmp9 tl.store(in_out_ptr0 + x3, tmp2, xmask) tl.store(out_ptr0 + x3, tmp10, 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 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_5, (4,), (1,)) 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=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 4), (16, 4, 1)) buf2 = buf1 del buf1 buf3 = buf0 del buf0 triton_poi_fused_convolution_gelu_1[grid(64)](buf2, primals_3, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_3 buf4 = extern_kernels.convolution(buf3, primals_4, 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_convolution_2[grid(64)](buf5, primals_5, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_5 return reinterpret_tensor(buf5, (4, 4, 4), (16, 1, 4), 0 ), primals_2, primals_4, reinterpret_tensor(primals_1, (4, 4, 4), ( 16, 1, 4), 0), buf2, buf3 class PoswiseFeedForwardNetNew(nn.Module): def __init__(self, config): super().__init__() self.config = config self.conv1 = nn.Conv1d(in_channels=self.config.d_hidn, out_channels =self.config.d_ff, kernel_size=1) self.conv2 = nn.Conv1d(in_channels=self.config.d_ff, out_channels= self.config.d_hidn, kernel_size=1) self.active = F.gelu self.dropout = nn.Dropout(config.dropout) 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]
kyuhyoung/transformer-evolution
PoswiseFeedForwardNet
false
15,859
[ "Apache-2.0" ]
105
fae06f677df0be55c67cd58efea158e5517ac045
https://github.com/kyuhyoung/transformer-evolution/tree/fae06f677df0be55c67cd58efea158e5517ac045
JaccardLoss
import torch from torch import nn def jaccard(outputs, targets, per_image=False, non_empty=False, min_pixels=5): batch_size = outputs.size()[0] eps = 0.001 if not per_image: batch_size = 1 dice_target = targets.contiguous().view(batch_size, -1).float() dice_output = outputs.contiguous().view(batch_size, -1) target_sum = torch.sum(dice_target, dim=1) intersection = torch.sum(dice_output * dice_target, dim=1) losses = 1 - (intersection + eps) / (torch.sum(dice_output + dice_target, dim=1) - intersection + eps) if non_empty: assert per_image is True non_empty_images = 0 sum_loss = 0 for i in range(batch_size): if target_sum[i] > min_pixels: sum_loss += losses[i] non_empty_images += 1 if non_empty_images == 0: return 0 else: return sum_loss / non_empty_images return losses.mean() class JaccardLoss(nn.Module): def __init__(self, weight=None, size_average=True, per_image=False, non_empty=False, apply_sigmoid=False, min_pixels=5): super().__init__() self.size_average = size_average self.register_buffer('weight', weight) self.per_image = per_image self.non_empty = non_empty self.apply_sigmoid = apply_sigmoid self.min_pixels = min_pixels def forward(self, input, target): if self.apply_sigmoid: input = torch.sigmoid(input) return jaccard(input, target, per_image=self.per_image, non_empty= self.non_empty, min_pixels=self.min_pixels) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_per_fused_add_div_mean_mul_rsub_sub_sum_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) 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 = tmp0 + tmp1 tmp7 = tl.broadcast_to(tmp6, [RBLOCK]) tmp9 = triton_helpers.promote_to_tensor(tl.sum(tmp7, 0)) tmp10 = 0.001 tmp11 = tmp5 + tmp10 tmp12 = tmp9 - tmp5 tmp13 = tmp12 + tmp10 tmp14 = tmp11 / tmp13 tmp15 = 1.0 tmp16 = tmp15 - tmp14 tmp17 = tmp16 / tmp15 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((1,), (1,), torch.float32) buf2 = reinterpret_tensor(buf0, (), (), 0) del buf0 get_raw_stream(0) triton_per_fused_add_div_mean_mul_rsub_sub_sum_0[grid(1)](buf2, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf2, def jaccard(outputs, targets, per_image=False, non_empty=False, min_pixels=5): batch_size = outputs.size()[0] eps = 0.001 if not per_image: batch_size = 1 dice_target = targets.contiguous().view(batch_size, -1).float() dice_output = outputs.contiguous().view(batch_size, -1) target_sum = torch.sum(dice_target, dim=1) intersection = torch.sum(dice_output * dice_target, dim=1) losses = 1 - (intersection + eps) / (torch.sum(dice_output + dice_target, dim=1) - intersection + eps) if non_empty: assert per_image is True non_empty_images = 0 sum_loss = 0 for i in range(batch_size): if target_sum[i] > min_pixels: sum_loss += losses[i] non_empty_images += 1 if non_empty_images == 0: return 0 else: return sum_loss / non_empty_images return losses.mean() class JaccardLossNew(nn.Module): def __init__(self, weight=None, size_average=True, per_image=False, non_empty=False, apply_sigmoid=False, min_pixels=5): super().__init__() self.size_average = size_average self.register_buffer('weight', weight) self.per_image = per_image self.non_empty = non_empty self.apply_sigmoid = apply_sigmoid self.min_pixels = min_pixels def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
ktncktnc/SpaceNet_Off_Nadir_Solutions
JaccardLoss
false
15,860
[ "Apache-2.0" ]
164
2a9ef1c3b72fb749c808ddb8593a85cb16b9f1ca
https://github.com/ktncktnc/SpaceNet_Off_Nadir_Solutions/tree/2a9ef1c3b72fb749c808ddb8593a85cb16b9f1ca
dy_nconv
import torch import torch.utils.data import torch.nn as nn class dy_nconv(nn.Module): def __init__(self): super(dy_nconv, self).__init__() def forward(self, x, A): x = torch.einsum('ncvl,nvwl->ncwl', (x, A)) return x.contiguous() def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.utils.data import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clone_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 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 % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 64 * y1), xmask & ymask) tl.store(out_ptr0 + (x2 + 16 * y3), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_clone_1(in_ptr0, 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 x2 = xindex y0 = yindex % 16 y1 = yindex // 16 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 16 * x2 + 64 * y1), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask) 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, 1), (64, 16, 4, 1, 1), torch .float32) get_raw_stream(0) triton_poi_fused_clone_0[grid(16, 16)](arg0_1, buf0, 16, 16, XBLOCK =16, YBLOCK=16, num_warps=4, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 16, 4, 1, 1), torch .float32) triton_poi_fused_clone_0[grid(16, 16)](arg1_1, buf1, 16, 16, XBLOCK =16, YBLOCK=16, num_warps=4, num_stages=1) del arg1_1 buf2 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf0, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf1, (16, 4, 4), (16, 4, 1), 0), out=buf2) del buf0 buf3 = reinterpret_tensor(buf1, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf1 triton_poi_fused_clone_1[grid(64, 4)](buf2, buf3, 64, 4, XBLOCK=4, YBLOCK=32, num_warps=4, num_stages=1) del buf2 return buf3, class dy_nconvNew(nn.Module): def __init__(self): super(dy_nconvNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
kevin-xuan/Traffic-Benchmark
dy_nconv
false
15,861
[ "MIT" ]
120
b9f8e40b4df9b58f5ad88432dc070cbbbcdc0228
https://github.com/kevin-xuan/Traffic-Benchmark/tree/b9f8e40b4df9b58f5ad88432dc070cbbbcdc0228
BertAttention
from _paritybench_helpers import _mock_config import math import torch import torch.nn import torch.nn as nn class BertAttention(nn.Module): """BERT attention layer. Based on: BERT (pytorch-transformer) https://github.com/huggingface/transformers """ def __init__(self, config) ->None: super().__init__() 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, context): mixed_query_layer = self.query(hidden_states) mixed_key_layer = self.key(context) mixed_value_layer = self.value(context) 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_probs = self.dropout(nn.Softmax(dim=-1)(attention_scores)) 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 def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'config': _mock_config(num_attention_heads=4, hidden_size= 4, attention_probs_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 math as tl_math import torch.nn 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_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, 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_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 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp18 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp25 = tl.load(in_ptr1 + x2, xmask) tmp26 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last') tmp27 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp29 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp31 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp1 = float('-inf') tmp2 = tmp0 == tmp1 tmp3 = tmp2 == 0 tmp4 = tmp3.to(tl.int64) tmp5 = tmp4 != 0 tmp7 = tmp6 == tmp1 tmp8 = tmp7 == 0 tmp9 = tmp8.to(tl.int64) tmp10 = tmp9 != 0 tmp11 = tmp5 | tmp10 tmp13 = tmp12 == tmp1 tmp14 = tmp13 == 0 tmp15 = tmp14.to(tl.int64) tmp16 = tmp15 != 0 tmp17 = tmp11 | tmp16 tmp19 = tmp18 == tmp1 tmp20 = tmp19 == 0 tmp21 = tmp20.to(tl.int64) tmp22 = tmp21 != 0 tmp23 = tmp17 | tmp22 tmp24 = tmp23 == 0 tmp28 = tmp26 + tmp27 tmp30 = tmp28 + tmp29 tmp32 = tmp30 + tmp31 tmp33 = tmp25 / tmp32 tmp34 = 0.0 tmp35 = tl.where(tmp24, tmp34, tmp33) tl.store(out_ptr0 + x2, tmp35, 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) 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, 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), (16, 4, 1)) assert_size_stride(primals_7, (4, 4), (4, 1)) assert_size_stride(primals_8, (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_6, (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_6, (16, 4), (4, 1), 0), reinterpret_tensor(primals_7, (4, 4), (1, 4), 0), out=buf2) del primals_7 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=2, YBLOCK=16, 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=2, YBLOCK=16, 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 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_1[grid(256)](buf5, buf6, 256, XBLOCK=128, num_warps=4, num_stages=1) buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_2[grid(256)](buf5, buf6, buf7, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf5 del buf6 buf8 = reinterpret_tensor(buf1, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf1 triton_poi_fused_3[grid(16, 4)](buf2, primals_8, buf8, 16, 4, XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1) del primals_8 buf9 = reinterpret_tensor(buf2, (16, 4, 1), (4, 1, 1), 0) del buf2 extern_kernels.bmm(reinterpret_tensor(buf7, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf8, (16, 4, 1), (4, 1, 0), 0), out=buf9) buf10 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) triton_poi_fused_clone_4[grid(16, 4)](buf9, buf10, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) del buf9 return reinterpret_tensor(buf10, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(primals_3, (16, 4), (4, 1), 0 ), reinterpret_tensor(primals_6, (16, 4), (4, 1), 0 ), buf7, reinterpret_tensor(buf8, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 4), 0) class BertAttentionNew(nn.Module): """BERT attention layer. Based on: BERT (pytorch-transformer) https://github.com/huggingface/transformers """ def __init__(self, config) ->None: super().__init__() 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, input_0, input_1): primals_1 = self.query.weight primals_2 = self.query.bias primals_4 = self.key.weight primals_5 = self.key.bias primals_7 = self.value.weight primals_8 = self.value.bias primals_3 = input_0 primals_6 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8]) return output[0]
Project-MONAI/MONAI
BertAttention
false
15,862
[ "Apache-2.0" ]
2,971
2bab12c67c3cc1d54a4847628ce1e879064be11c
https://github.com/Project-MONAI/MONAI/tree/2bab12c67c3cc1d54a4847628ce1e879064be11c
ResBlock
import torch import torch.nn as nn import torch.nn.functional as F def get_conv(in_dim, out_dim, kernel_size, stride, padding, zero_bias=True, zero_weights=False, groups=1): c = nn.Conv2d(in_dim, out_dim, kernel_size, stride, padding, groups=groups) if zero_bias: c.bias.data *= 0.0 if zero_weights: c.weight.data *= 0.0 return c def get_1x1(in_dim, out_dim, zero_bias=True, zero_weights=False, groups=1): return get_conv(in_dim, out_dim, 1, 1, 0, zero_bias, zero_weights, groups=groups) def get_3x3(in_dim, out_dim, zero_bias=True, zero_weights=False, groups=1): return get_conv(in_dim, out_dim, 3, 1, 1, zero_bias, zero_weights, groups=groups) class ResBlock(nn.Module): def __init__(self, in_width, middle_width, out_width, down_rate=None, residual=False, use_3x3=True, zero_last=False): super().__init__() self.down_rate = down_rate self.residual = residual self.c1 = get_1x1(in_width, middle_width) self.c2 = get_3x3(middle_width, middle_width) if use_3x3 else get_1x1( middle_width, middle_width) self.c3 = get_3x3(middle_width, middle_width) if use_3x3 else get_1x1( middle_width, middle_width) self.c4 = get_1x1(middle_width, out_width, zero_weights=zero_last) def forward(self, x): xhat = self.c1(F.gelu(x)) xhat = self.c2(F.gelu(xhat)) xhat = self.c3(F.gelu(xhat)) xhat = self.c4(F.gelu(xhat)) out = x + xhat if self.residual else xhat if self.down_rate is not None: out = F.avg_pool2d(out, kernel_size=self.down_rate, stride=self .down_rate) return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_width': 4, 'middle_width': 4, 'out_width': 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 @triton.jit def triton_poi_fused_gelu_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.5 tmp2 = tmp0 * tmp1 tmp3 = 0.7071067811865476 tmp4 = tmp0 * tmp3 tmp5 = libdevice.erf(tmp4) tmp6 = 1.0 tmp7 = tmp5 + tmp6 tmp8 = tmp2 * tmp7 tl.store(out_ptr0 + x0, tmp8, xmask) @triton.jit def triton_poi_fused_convolution_gelu_1(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 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.5 tmp4 = tmp2 * tmp3 tmp5 = 0.7071067811865476 tmp6 = tmp2 * tmp5 tmp7 = libdevice.erf(tmp6) tmp8 = 1.0 tmp9 = tmp7 + tmp8 tmp10 = tmp4 * tmp9 tl.store(in_out_ptr0 + x3, tmp2, xmask) tl.store(out_ptr0 + x3, tmp10, xmask) @triton.jit def triton_poi_fused_convolution_2(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) 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, 1, 1), (4, 1, 1, 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, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_7, (4,), (1,)) assert_size_stride(primals_8, (4, 4, 1, 1), (4, 1, 1, 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, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_gelu_0[grid(256)](primals_1, buf0, 256, XBLOCK=128, 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, 4, 4, 4), (64, 16, 4, 1)) buf2 = buf1 del buf1 buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_convolution_gelu_1[grid(256)](buf2, primals_3, buf3, 256, XBLOCK=128, num_warps=4, num_stages=1) 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 buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_convolution_gelu_1[grid(256)](buf5, primals_5, buf6, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf7 = extern_kernels.convolution(buf6, primals_6, 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, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_convolution_gelu_1[grid(256)](buf8, primals_7, buf9, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_7 buf10 = extern_kernels.convolution(buf9, primals_8, stride=(1, 1), padding=(0, 0), 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_convolution_2[grid(256)](buf11, primals_9, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_9 return (buf11, primals_2, primals_4, primals_6, primals_8, buf0, buf2, buf3, buf5, buf6, buf8, buf9) def get_conv(in_dim, out_dim, kernel_size, stride, padding, zero_bias=True, zero_weights=False, groups=1): c = nn.Conv2d(in_dim, out_dim, kernel_size, stride, padding, groups=groups) if zero_bias: c.bias.data *= 0.0 if zero_weights: c.weight.data *= 0.0 return c def get_1x1(in_dim, out_dim, zero_bias=True, zero_weights=False, groups=1): return get_conv(in_dim, out_dim, 1, 1, 0, zero_bias, zero_weights, groups=groups) def get_3x3(in_dim, out_dim, zero_bias=True, zero_weights=False, groups=1): return get_conv(in_dim, out_dim, 3, 1, 1, zero_bias, zero_weights, groups=groups) class ResBlockNew(nn.Module): def __init__(self, in_width, middle_width, out_width, down_rate=None, residual=False, use_3x3=True, zero_last=False): super().__init__() self.down_rate = down_rate self.residual = residual self.c1 = get_1x1(in_width, middle_width) self.c2 = get_3x3(middle_width, middle_width) if use_3x3 else get_1x1( middle_width, middle_width) self.c3 = get_3x3(middle_width, middle_width) if use_3x3 else get_1x1( middle_width, middle_width) self.c4 = get_1x1(middle_width, out_width, zero_weights=zero_last) def forward(self, input_0): primals_2 = self.c1.weight primals_3 = self.c1.bias primals_4 = self.c2.weight primals_5 = self.c2.bias primals_6 = self.c3.weight primals_7 = self.c3.bias primals_8 = self.c4.weight primals_9 = self.c4.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]
kpandey008/DiffuseVAE
ResBlock
false
15,863
[ "MIT" ]
90
b505894668ac1e4ef9a66ec220f5b40f5c83629e
https://github.com/kpandey008/DiffuseVAE/tree/b505894668ac1e4ef9a66ec220f5b40f5c83629e
RegLoss
import torch from torch import nn import torch.onnx from torch.nn.parallel.scatter_gather import gather import torch.utils.data def _gather_feat(feat, ind, mask=None, trt=False): dim = feat.size(2) ind = ind.unsqueeze(2).expand(ind.size(0), ind.size(1), dim) if trt: feat = gather(feat, 1, ind) else: feat = torch.gather(feat, 1, ind) if mask is not None: mask = mask.unsqueeze(2).expand_as(feat) feat = feat[mask] feat = feat.view(-1, dim) return feat def _transpose_and_gather_feat(feat, ind, trt=False): feat = feat.permute(0, 2, 3, 1).contiguous() feat = feat.view(feat.size(0), -1, feat.size(3)) feat = _gather_feat(feat, ind, trt=trt) return feat def _reg_loss(regr, gt_regr, mask): """ L1 regression loss Arguments: regr (batch x max_objects x dim) gt_regr (batch x max_objects x dim) mask (batch x max_objects) """ num = mask.float().sum() mask = mask.unsqueeze(2).expand_as(gt_regr).float() regr = regr * mask gt_regr = gt_regr * mask regr_loss = nn.functional.smooth_l1_loss(regr, gt_regr, size_average=False) regr_loss = regr_loss / (num + 0.0001) return regr_loss class RegLoss(nn.Module): """Regression loss for an output tensor Arguments: output (batch x dim x h x w) mask (batch x max_objects) ind (batch x max_objects) target (batch x max_objects x dim) """ def __init__(self): super(RegLoss, self).__init__() def forward(self, output, mask, ind, target): pred = _transpose_and_gather_feat(output, ind) loss = _reg_loss(pred, target, mask) return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4]), torch.ones([4, 4], dtype=torch.int64), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math from torch import nn import torch.onnx from torch.nn.parallel.scatter_gather import gather 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_per_fused_gather_mul_smooth_l1_loss_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r4 = rindex // 4 % 16 r0 = rindex % 4 r2 = rindex // 16 % 4 r5 = rindex // 16 r6 = rindex tmp0 = tl.load(in_ptr0 + r4, None, eviction_policy='evict_last') tmp7 = tl.load(in_ptr2 + (r0 + 4 * r5), None, eviction_policy='evict_last') tmp9 = tl.load(in_ptr3 + r6, None) tmp1 = tl.full([RBLOCK], 16, tl.int32) tmp2 = tmp0 + tmp1 tmp3 = tmp0 < 0 tmp4 = tl.where(tmp3, tmp2, tmp0) tl.device_assert((0 <= tmp4) & (tmp4 < 16), 'index out of bounds: 0 <= tmp4 < 16') tmp6 = tl.load(in_ptr1 + (16 * r0 + 64 * r2 + tmp4 % 16), None, eviction_policy='evict_last') tmp8 = tmp6 * tmp7 tmp10 = tmp9 * tmp7 tmp11 = tmp8 - tmp10 tmp12 = tl_math.abs(tmp11) tmp13 = 1.0 tmp14 = tmp12 < tmp13 tmp15 = tmp12 * tmp12 tmp16 = 0.5 tmp17 = tmp15 * tmp16 tmp18 = tmp17 * tmp13 tmp19 = tmp12 - tmp16 tmp20 = tl.where(tmp14, tmp18, tmp19) tmp21 = tl.broadcast_to(tmp20, [RBLOCK]) tmp23 = triton_helpers.promote_to_tensor(tl.sum(tmp21, 0)) tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp23, None) @triton.jit def triton_per_fused_add_div_sum_1(in_out_ptr0, in_ptr0, 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 tmp0 = tl.load(in_ptr0 + r0, None) tmp4 = tl.load(in_out_ptr0 + 0) tmp5 = tl.broadcast_to(tmp4, [XBLOCK, 1]) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.sum(tmp1, 1)[:, None] tmp6 = 0.0001 tmp7 = tmp3 + tmp6 tmp8 = tmp5 / tmp7 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp8, None) def call(args): arg0_1, arg1_1, arg2_1, arg3_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, 1)) assert_size_stride(arg2_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(arg3_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) get_raw_stream(0) triton_per_fused_gather_mul_smooth_l1_loss_0[grid(1)](arg1_1, arg0_1, arg2_1, arg3_1, buf0, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 del arg3_1 buf2 = buf0 del buf0 triton_per_fused_add_div_sum_1[grid(1)](buf2, arg2_1, 1, 64, XBLOCK =1, num_warps=2, num_stages=1) del arg2_1 return buf2, def _gather_feat(feat, ind, mask=None, trt=False): dim = feat.size(2) ind = ind.unsqueeze(2).expand(ind.size(0), ind.size(1), dim) if trt: feat = gather(feat, 1, ind) else: feat = torch.gather(feat, 1, ind) if mask is not None: mask = mask.unsqueeze(2).expand_as(feat) feat = feat[mask] feat = feat.view(-1, dim) return feat def _transpose_and_gather_feat(feat, ind, trt=False): feat = feat.permute(0, 2, 3, 1).contiguous() feat = feat.view(feat.size(0), -1, feat.size(3)) feat = _gather_feat(feat, ind, trt=trt) return feat def _reg_loss(regr, gt_regr, mask): """ L1 regression loss Arguments: regr (batch x max_objects x dim) gt_regr (batch x max_objects x dim) mask (batch x max_objects) """ num = mask.float().sum() mask = mask.unsqueeze(2).expand_as(gt_regr).float() regr = regr * mask gt_regr = gt_regr * mask regr_loss = nn.functional.smooth_l1_loss(regr, gt_regr, size_average=False) regr_loss = regr_loss / (num + 0.0001) return regr_loss class RegLossNew(nn.Module): """Regression loss for an output tensor Arguments: output (batch x dim x h x w) mask (batch x max_objects) ind (batch x max_objects) target (batch x max_objects x dim) """ def __init__(self): super(RegLossNew, self).__init__() def forward(self, input_0, input_1, input_2, input_3): arg0_1 = input_0 arg2_1 = input_1 arg1_1 = input_2 arg3_1 = input_3 output = call([arg0_1, arg1_1, arg2_1, arg3_1]) return output[0]
kuanhungchen/CenterNet-HarDNet
RegLoss
false
15,864
[ "MIT" ]
164
050d55a532706d989105982c5bc10f1c89edc8d2
https://github.com/kuanhungchen/CenterNet-HarDNet/tree/050d55a532706d989105982c5bc10f1c89edc8d2
dy_mixprop
import torch import torch.utils.data import torch.nn as nn class linear(nn.Module): def __init__(self, c_in, c_out, bias=True): super(linear, self).__init__() self.mlp = torch.nn.Conv2d(c_in, c_out, kernel_size=(1, 1), padding =(0, 0), stride=(1, 1), bias=bias) def forward(self, x): return self.mlp(x) class dy_nconv(nn.Module): def __init__(self): super(dy_nconv, self).__init__() def forward(self, x, A): x = torch.einsum('ncvl,nvwl->ncwl', (x, A)) return x.contiguous() class dy_mixprop(nn.Module): def __init__(self, c_in, c_out, gdep, dropout, alpha): super(dy_mixprop, self).__init__() self.nconv = dy_nconv() self.mlp1 = linear((gdep + 1) * c_in, c_out) self.mlp2 = linear((gdep + 1) * c_in, c_out) self.gdep = gdep self.dropout = dropout self.alpha = alpha self.lin1 = linear(c_in, c_in) self.lin2 = linear(c_in, c_in) def forward(self, x): x1 = torch.tanh(self.lin1(x)) x2 = torch.tanh(self.lin2(x)) adj = self.nconv(x1.transpose(2, 1), x2) adj0 = torch.softmax(adj, dim=2) adj1 = torch.softmax(adj.transpose(2, 1), dim=2) h = x out = [h] for i in range(self.gdep): h = self.alpha * x + (1 - self.alpha) * self.nconv(h, adj0) out.append(h) ho = torch.cat(out, dim=1) ho1 = self.mlp1(ho) h = x out = [h] for i in range(self.gdep): h = self.alpha * x + (1 - self.alpha) * self.nconv(h, adj1) out.append(h) ho = torch.cat(out, dim=1) ho2 = self.mlp2(ho) return ho1 + ho2 def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'c_in': 4, 'c_out': 4, 'gdep': 4, 'dropout': 0.5, 'alpha': 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.utils.data import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda 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_poi_fused_clone_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 16 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex % 4 x3 = xindex // 4 y0 = yindex % 4 y1 = yindex // 4 x5 = xindex y4 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x3 + 16 * x2 + 64 * y1), xmask & ymask) tmp1 = libdevice.tanh(tmp0) tl.store(out_ptr0 + (x5 + 16 * y4), tmp1, xmask & ymask) @triton.jit def triton_poi_fused_clone_2(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 y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 64 * y1), xmask & ymask) tmp1 = libdevice.tanh(tmp0) tl.store(out_ptr0 + (x2 + 16 * y3), tmp1, xmask & ymask) @triton.jit def triton_poi_fused__softmax_clone_3(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 x4 = xindex x1 = xindex // 4 x0 = xindex % 4 x3 = xindex // 16 tmp0 = tl.load(in_ptr0 + x4, 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') tmp10 = tl.load(in_ptr0 + (x0 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp11 = tl.load(in_ptr0 + (4 + x0 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp13 = tl.load(in_ptr0 + (8 + x0 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp15 = tl.load(in_ptr0 + (12 + x0 + 16 * x3), 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) tmp12 = triton_helpers.maximum(tmp10, tmp11) tmp14 = triton_helpers.maximum(tmp12, tmp13) tmp16 = triton_helpers.maximum(tmp14, tmp15) tmp17 = tmp0 - tmp16 tmp18 = tl_math.exp(tmp17) tl.store(out_ptr0 + x4, tmp9, xmask) tl.store(out_ptr1 + x4, tmp18, xmask) @triton.jit def triton_poi_fused_clone_4(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_5(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 y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 64 * y1), xmask & ymask) tl.store(out_ptr0 + (x2 + 16 * y3), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_clone_6(in_ptr0, in_ptr1, 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 y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 64 * y1), xmask & ymask) tmp3 = tl.load(in_ptr1 + (x2 + 16 * y3), xmask & ymask) tmp1 = 4.0 tmp2 = tmp0 * tmp1 tmp4 = -3.0 tmp5 = tmp3 * tmp4 tmp6 = tmp2 + tmp5 tl.store(out_ptr0 + (x2 + 16 * y3), tmp6, xmask & ymask) @triton.jit def triton_poi_fused_clone_7(in_ptr0, 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 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_ptr0 + (y0 + 16 * y1), ymask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (4 + y0 + 16 * y1), ymask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (8 + y0 + 16 * y1), ymask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (12 + y0 + 16 * y1), ymask, eviction_policy= 'evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + (x2 + 4 * y3), tmp8, xmask & ymask) @triton.jit def triton_poi_fused_clone_8(in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, 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 y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 64 * y1), xmask & ymask) tmp3 = tl.load(in_ptr1 + (x2 + 16 * y3), xmask & ymask) tmp7 = tl.load(in_ptr2 + (x2 + 16 * y3), xmask & ymask) tmp1 = 4.0 tmp2 = tmp0 * tmp1 tmp4 = -3.0 tmp5 = tmp3 * tmp4 tmp6 = tmp2 + tmp5 tmp8 = tmp7 * tmp4 tmp9 = tmp2 + tmp8 tl.store(out_ptr0 + (x2 + 16 * y3), tmp6, xmask & ymask) tl.store(out_ptr1 + (x2 + 16 * y3), tmp9, xmask & ymask) @triton.jit def triton_poi_fused_cat_9(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 1280 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex // 16 % 20 x3 = xindex // 320 x4 = xindex % 16 x0 = xindex % 4 x1 = xindex // 4 % 4 x5 = xindex tmp0 = x2 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x4 + 16 * x2 + 64 * x3), tmp4 & xmask, other=0.0) tmp6 = tmp0 >= tmp3 tmp7 = tl.full([1], 8, tl.int64) tmp8 = tmp0 < tmp7 tmp9 = tmp6 & tmp8 tmp10 = tl.load(in_ptr0 + (x4 + 16 * (-4 + x2) + 64 * x3), tmp9 & xmask, other=0.0) tmp11 = 4.0 tmp12 = tmp10 * tmp11 tmp13 = tl.load(in_ptr1 + (x1 + 4 * (-4 + x2) + 16 * x0 + 64 * x3), tmp9 & xmask, eviction_policy='evict_last', other=0.0) tmp14 = -3.0 tmp15 = tmp13 * tmp14 tmp16 = tmp12 + tmp15 tmp17 = tl.full(tmp16.shape, 0.0, tmp16.dtype) tmp18 = tl.where(tmp9, tmp16, tmp17) tmp19 = tmp0 >= tmp7 tmp20 = tl.full([1], 12, tl.int64) tmp21 = tmp0 < tmp20 tmp22 = tmp19 & tmp21 tmp23 = tl.load(in_ptr0 + (x4 + 16 * (-8 + x2) + 64 * x3), tmp22 & xmask, other=0.0) tmp24 = tmp23 * tmp11 tmp25 = tl.load(in_ptr2 + (x1 + 4 * (-8 + x2) + 16 * x0 + 64 * x3), tmp22 & xmask, eviction_policy='evict_last', other=0.0) tmp26 = tmp25 * tmp14 tmp27 = tmp24 + tmp26 tmp28 = tl.full(tmp27.shape, 0.0, tmp27.dtype) tmp29 = tl.where(tmp22, tmp27, tmp28) tmp30 = tmp0 >= tmp20 tmp31 = tl.full([1], 16, tl.int64) tmp32 = tmp0 < tmp31 tmp33 = tmp30 & tmp32 tmp34 = tl.load(in_ptr0 + (x4 + 16 * (-12 + x2) + 64 * x3), tmp33 & xmask, other=0.0) tmp35 = tmp34 * tmp11 tmp36 = tl.load(in_ptr3 + (x1 + 4 * (-12 + x2) + 16 * x0 + 64 * x3), tmp33 & xmask, eviction_policy='evict_last', other=0.0) tmp37 = tmp36 * tmp14 tmp38 = tmp35 + tmp37 tmp39 = tl.full(tmp38.shape, 0.0, tmp38.dtype) tmp40 = tl.where(tmp33, tmp38, tmp39) tmp41 = tmp0 >= tmp31 tl.full([1], 20, tl.int64) tmp44 = tl.load(in_ptr0 + (x4 + 16 * (-16 + x2) + 64 * x3), tmp41 & xmask, other=0.0) tmp45 = tmp44 * tmp11 tmp46 = tl.load(in_ptr4 + (x1 + 4 * (-16 + x2) + 16 * x0 + 64 * x3), tmp41 & xmask, eviction_policy='evict_last', other=0.0) tmp47 = tmp46 * tmp14 tmp48 = tmp45 + tmp47 tmp49 = tl.full(tmp48.shape, 0.0, tmp48.dtype) tmp50 = tl.where(tmp41, tmp48, tmp49) tmp51 = tl.where(tmp33, tmp40, tmp50) tmp52 = tl.where(tmp22, tmp29, tmp51) tmp53 = tl.where(tmp9, tmp18, tmp52) tmp54 = tl.where(tmp4, tmp5, tmp53) tmp55 = tl.load(in_ptr5 + (x1 + 4 * (-4 + x2) + 16 * x0 + 64 * x3), tmp9 & xmask, eviction_policy='evict_last', other=0.0) tmp56 = tmp55 * tmp14 tmp57 = tmp12 + tmp56 tmp58 = tl.full(tmp57.shape, 0.0, tmp57.dtype) tmp59 = tl.where(tmp9, tmp57, tmp58) tmp60 = tl.load(in_ptr6 + (x1 + 4 * (-8 + x2) + 16 * x0 + 64 * x3), tmp22 & xmask, eviction_policy='evict_last', other=0.0) tmp61 = tmp60 * tmp14 tmp62 = tmp24 + tmp61 tmp63 = tl.full(tmp62.shape, 0.0, tmp62.dtype) tmp64 = tl.where(tmp22, tmp62, tmp63) tmp65 = tl.load(in_ptr7 + (x1 + 4 * (-12 + x2) + 16 * x0 + 64 * x3), tmp33 & xmask, eviction_policy='evict_last', other=0.0) tmp66 = tmp65 * tmp14 tmp67 = tmp35 + tmp66 tmp68 = tl.full(tmp67.shape, 0.0, tmp67.dtype) tmp69 = tl.where(tmp33, tmp67, tmp68) tmp70 = tl.load(in_ptr8 + (x1 + 4 * (-16 + x2) + 16 * x0 + 64 * x3), tmp41 & xmask, eviction_policy='evict_last', other=0.0) tmp71 = tmp70 * tmp14 tmp72 = tmp45 + tmp71 tmp73 = tl.full(tmp72.shape, 0.0, tmp72.dtype) tmp74 = tl.where(tmp41, tmp72, tmp73) tmp75 = tl.where(tmp33, tmp69, tmp74) tmp76 = tl.where(tmp22, tmp64, tmp75) tmp77 = tl.where(tmp9, tmp59, tmp76) tmp78 = tl.where(tmp4, tmp5, tmp77) tl.store(out_ptr0 + x5, tmp54, xmask) tl.store(out_ptr1 + x5, tmp78, xmask) @triton.jit def triton_poi_fused_add_convolution_10(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 x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x3, xmask) tmp4 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tl.store(in_out_ptr0 + x3, 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, 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, 20, 1, 1), (20, 1, 1, 1)) assert_size_stride(primals_7, (4,), (1,)) assert_size_stride(primals_8, (4, 20, 1, 1), (20, 1, 1, 1)) assert_size_stride(primals_9, (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 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(256)](buf1, primals_2, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf2 = 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(buf2, (4, 4, 4, 4), (64, 16, 4, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_0[grid(256)](buf3, primals_5, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 16, 4, 1, 1), torch .float32) triton_poi_fused_clone_1[grid(16, 16)](buf1, buf4, 16, 16, XBLOCK= 16, YBLOCK=16, num_warps=4, num_stages=1) buf5 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 16, 4, 1, 1), torch .float32) triton_poi_fused_clone_2[grid(16, 16)](buf3, buf5, 16, 16, XBLOCK= 16, YBLOCK=16, num_warps=4, num_stages=1) buf6 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf4, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf5, (16, 4, 4), (16, 4, 1), 0), out=buf6) buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 4, 1, 16), torch.float32) buf8 = empty_strided_cuda((4, 4, 4, 4), (64, 1, 4, 16), torch.float32) triton_poi_fused__softmax_clone_3[grid(256)](buf6, buf7, buf8, 256, XBLOCK=128, num_warps=4, num_stages=1) buf9 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 16, 4, 1, 1), torch .float32) triton_poi_fused_clone_4[grid(256)](buf7, buf9, 256, XBLOCK=256, num_warps=4, num_stages=1) buf10 = reinterpret_tensor(buf7, (4, 4, 4, 4, 1), (64, 16, 4, 1, 1), 0) del buf7 triton_poi_fused_clone_5[grid(16, 16)](primals_3, buf10, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) buf11 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf10, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0), out=buf11) buf12 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 16, 4, 1, 1), torch.float32) triton_poi_fused_clone_6[grid(16, 16)](primals_3, buf11, buf12, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) buf13 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf12, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0), out=buf13) buf14 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 16, 4, 1, 1), torch.float32) triton_poi_fused_clone_6[grid(16, 16)](primals_3, buf13, buf14, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) buf15 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf14, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0), out=buf15) buf20 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 16, 4, 1, 1), torch.float32) triton_poi_fused_clone_7[grid(64, 4)](buf8, buf20, 64, 4, XBLOCK=4, YBLOCK=32, num_warps=4, num_stages=1) buf21 = reinterpret_tensor(buf8, (16, 4, 4), (16, 4, 1), 0) del buf8 extern_kernels.bmm(reinterpret_tensor(buf10, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf20, (16, 4, 4), (16, 4, 1), 0), out=buf21 ) buf16 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 16, 4, 1, 1), torch.float32) buf22 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 16, 4, 1, 1), torch.float32) triton_poi_fused_clone_8[grid(16, 16)](primals_3, buf15, buf21, buf16, buf22, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) buf17 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf16, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0), out=buf17) buf23 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf22, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf20, (16, 4, 4), (16, 4, 1), 0), out=buf23 ) buf24 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 16, 4, 1, 1), torch.float32) triton_poi_fused_clone_6[grid(16, 16)](primals_3, buf23, buf24, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) buf25 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf24, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf20, (16, 4, 4), (16, 4, 1), 0), out=buf25 ) buf26 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 16, 4, 1, 1), torch.float32) triton_poi_fused_clone_6[grid(16, 16)](primals_3, buf25, buf26, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) buf27 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf26, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf20, (16, 4, 4), (16, 4, 1), 0), out=buf27 ) buf18 = empty_strided_cuda((4, 20, 4, 4), (320, 16, 4, 1), torch. float32) buf28 = empty_strided_cuda((4, 20, 4, 4), (320, 16, 4, 1), torch. float32) triton_poi_fused_cat_9[grid(1280)](primals_3, buf11, buf13, buf15, buf17, buf21, buf23, buf25, buf27, buf18, buf28, 1280, XBLOCK= 256, num_warps=4, num_stages=1) del buf11 del buf13 del buf15 del buf17 del buf21 del buf23 del buf25 del buf27 buf19 = extern_kernels.convolution(buf18, primals_6, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf19, (4, 4, 4, 4), (64, 16, 4, 1)) buf29 = extern_kernels.convolution(buf28, primals_8, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf29, (4, 4, 4, 4), (64, 16, 4, 1)) buf30 = buf19 del buf19 triton_poi_fused_add_convolution_10[grid(256)](buf30, primals_7, buf29, primals_9, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf29 del primals_7 del primals_9 return (buf30, primals_1, primals_3, primals_4, primals_6, primals_8, buf1, buf3, buf6, buf18, buf28, reinterpret_tensor(buf26, (16, 4, 4 ), (16, 1, 4), 0), reinterpret_tensor(buf20, (16, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf24, (16, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf22, (16, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf10, (16, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf16, (16, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf9, (16, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf14, (16, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf12, (16, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf4, (16, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf5, (16, 4, 4), (16, 1, 4), 0)) class linear(nn.Module): def __init__(self, c_in, c_out, bias=True): super(linear, self).__init__() self.mlp = torch.nn.Conv2d(c_in, c_out, kernel_size=(1, 1), padding =(0, 0), stride=(1, 1), bias=bias) def forward(self, x): return self.mlp(x) class dy_nconv(nn.Module): def __init__(self): super(dy_nconv, self).__init__() def forward(self, x, A): x = torch.einsum('ncvl,nvwl->ncwl', (x, A)) return x.contiguous() class dy_mixpropNew(nn.Module): def __init__(self, c_in, c_out, gdep, dropout, alpha): super(dy_mixpropNew, self).__init__() self.nconv = dy_nconv() self.mlp1 = linear((gdep + 1) * c_in, c_out) self.mlp2 = linear((gdep + 1) * c_in, c_out) self.gdep = gdep self.dropout = dropout self.alpha = alpha self.lin1 = linear(c_in, c_in) self.lin2 = linear(c_in, c_in) def forward(self, input_0): primals_6 = self.mlp1.mlp.weight primals_2 = self.mlp1.mlp.bias primals_8 = self.mlp2.mlp.weight primals_5 = self.mlp2.mlp.bias primals_1 = self.lin1.mlp.weight primals_7 = self.lin1.mlp.bias primals_4 = self.lin2.mlp.weight primals_9 = self.lin2.mlp.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]
kevin-xuan/Traffic-Benchmark
dy_mixprop
false
15,865
[ "MIT" ]
120
b9f8e40b4df9b58f5ad88432dc070cbbbcdc0228
https://github.com/kevin-xuan/Traffic-Benchmark/tree/b9f8e40b4df9b58f5ad88432dc070cbbbcdc0228
FBLoss
import torch from torch import nn def fb_loss(preds, trues, beta): smooth = 0.0001 beta2 = beta * beta batch = preds.size(0) classes = preds.size(1) preds = preds.view(batch, classes, -1) trues = trues.view(batch, classes, -1) weights = torch.clamp(trues.sum(-1), 0.0, 1.0) TP = (preds * trues).sum(2) FP = (preds * (1 - trues)).sum(2) FN = ((1 - preds) * trues).sum(2) Fb = ((1 + beta2) * TP + smooth) / ((1 + beta2) * TP + beta2 * FN + FP + smooth) Fb = Fb * weights score = Fb.sum() / (weights.sum() + smooth) return torch.clamp(score, 0.0, 1.0) class FBLoss(nn.Module): def __init__(self, beta=1): super().__init__() self.beta = beta def forward(self, output, target): return 1 - fb_loss(output, target, self.beta) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_mul_rsub_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 tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tl.load(in_ptr1 + (r1 + 16 * x0), xmask, 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 = 1.0 tmp8 = tmp7 - tmp0 tmp9 = tmp8 * tmp1 tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK]) tmp12 = tl.where(xmask, tmp10, 0) tmp13 = tl.sum(tmp12, 1)[:, None] tmp14 = tmp7 - tmp1 tmp15 = tmp0 * tmp14 tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK]) tmp18 = tl.where(xmask, tmp16, 0) tmp19 = tl.sum(tmp18, 1)[:, None] tmp20 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp22 = tl.where(xmask, tmp20, 0) tmp23 = tl.sum(tmp22, 1)[:, None] tl.store(out_ptr0 + x0, tmp6, xmask) tl.store(out_ptr1 + x0, tmp13, xmask) tl.store(out_ptr2 + x0, tmp19, xmask) tl.store(out_ptr3 + x0, tmp23, xmask) @triton.jit def triton_per_fused_add_clamp_div_mul_rsub_sum_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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) tmp5 = tl.load(in_ptr1 + r0, None) tmp9 = tl.load(in_ptr2 + r0, None) tmp13 = tl.load(in_ptr3 + r0, None) tmp1 = 2.0 tmp2 = tmp0 * tmp1 tmp3 = 0.0001 tmp4 = tmp2 + tmp3 tmp6 = 1.0 tmp7 = tmp5 * tmp6 tmp8 = tmp2 + tmp7 tmp10 = tmp8 + tmp9 tmp11 = tmp10 + tmp3 tmp12 = tmp4 / tmp11 tmp14 = 0.0 tmp15 = triton_helpers.maximum(tmp13, tmp14) tmp16 = triton_helpers.minimum(tmp15, tmp6) tmp17 = tmp12 * tmp16 tmp18 = tl.broadcast_to(tmp17, [XBLOCK, RBLOCK]) tmp20 = tl.sum(tmp18, 1)[:, None] tmp21 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK]) tmp23 = tl.sum(tmp21, 1)[:, None] tmp24 = tmp23 + tmp3 tmp25 = tmp20 / tmp24 tmp26 = triton_helpers.maximum(tmp25, tmp14) tmp27 = triton_helpers.minimum(tmp26, tmp6) tmp28 = tmp6 - tmp27 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp28, 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) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_per_fused_mul_rsub_sum_0[grid(16)](arg0_1, arg1_1, buf0, buf1, buf2, buf3, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 buf4 = empty_strided_cuda((), (), torch.float32) buf6 = buf4 del buf4 triton_per_fused_add_clamp_div_mul_rsub_sum_1[grid(1)](buf6, buf0, buf1, buf2, buf3, 1, 16, XBLOCK=1, num_warps=2, num_stages=1) del buf0 del buf1 del buf2 del buf3 return buf6, def fb_loss(preds, trues, beta): smooth = 0.0001 beta2 = beta * beta batch = preds.size(0) classes = preds.size(1) preds = preds.view(batch, classes, -1) trues = trues.view(batch, classes, -1) weights = torch.clamp(trues.sum(-1), 0.0, 1.0) TP = (preds * trues).sum(2) FP = (preds * (1 - trues)).sum(2) FN = ((1 - preds) * trues).sum(2) Fb = ((1 + beta2) * TP + smooth) / ((1 + beta2) * TP + beta2 * FN + FP + smooth) Fb = Fb * weights score = Fb.sum() / (weights.sum() + smooth) return torch.clamp(score, 0.0, 1.0) class FBLossNew(nn.Module): def __init__(self, beta=1): super().__init__() self.beta = beta def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
lRomul/argus-tgs-salt
FBLoss
false
15,866
[ "MIT" ]
74
2ba7db4d09256bc025c49860cd79560ced6b8a1b
https://github.com/lRomul/argus-tgs-salt/tree/2ba7db4d09256bc025c49860cd79560ced6b8a1b
PositionwiseFeedForward
import torch import torch.nn as nn import torch.nn.functional as F class PositionwiseFeedForward(nn.Module): """ A two-feed-forward-layer module """ def __init__(self, d_in, d_hid, dropout=0.1): super().__init__() self.w_1 = nn.Linear(d_in, d_hid) self.w_2 = nn.Linear(d_hid, d_in) self.layer_norm = nn.LayerNorm(d_in) self.dropout = nn.Dropout(dropout) def forward(self, x): residual = x output = x output = self.w_2(F.relu(self.w_1(output))) output = self.dropout(output) output = self.layer_norm(output + residual) return output def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'d_in': 4, 'd_hid': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_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_native_layer_norm_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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_native_layer_norm_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x2, xmask) tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 - tmp3 tmp6 = 1e-05 tmp7 = tmp5 + tmp6 tmp8 = libdevice.rsqrt(tmp7) tmp9 = tmp4 * tmp8 tmp11 = tmp9 * 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) = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) 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((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 buf6 = 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_3, buf6, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_3 buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf2) del primals_5 buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf4 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) triton_poi_fused_add_native_layer_norm_1[grid(64)](buf2, primals_1, buf3, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_add_native_layer_norm_2[grid(256)](buf2, primals_1, buf3, buf4, primals_6, primals_7, buf5, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf3 del buf4 del primals_7 return buf5, primals_1, primals_6, reinterpret_tensor(buf1, (64, 4), (4, 1), 0), buf2, primals_4, buf6 class PositionwiseFeedForwardNew(nn.Module): """ A two-feed-forward-layer module """ def __init__(self, d_in, d_hid, dropout=0.1): super().__init__() self.w_1 = nn.Linear(d_in, d_hid) self.w_2 = nn.Linear(d_hid, d_in) self.layer_norm = nn.LayerNorm(d_in) self.dropout = nn.Dropout(dropout) 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_6 = self.layer_norm.weight primals_7 = self.layer_norm.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
kyteinsky/OmniNet
PositionwiseFeedForward
false
15,867
[ "Apache-2.0" ]
525
497dfbeaa9e4bdd8b076152e71ab7999ca5cfc4a
https://github.com/kyteinsky/OmniNet/tree/497dfbeaa9e4bdd8b076152e71ab7999ca5cfc4a
RegWeightedL1Loss
import torch from torch import nn import torch.onnx from torch.nn.parallel.scatter_gather import gather import torch.nn.functional as F import torch.utils.data def _gather_feat(feat, ind, mask=None, trt=False): dim = feat.size(2) ind = ind.unsqueeze(2).expand(ind.size(0), ind.size(1), dim) if trt: feat = gather(feat, 1, ind) else: feat = torch.gather(feat, 1, ind) if mask is not None: mask = mask.unsqueeze(2).expand_as(feat) feat = feat[mask] feat = feat.view(-1, dim) return feat def _transpose_and_gather_feat(feat, ind, trt=False): feat = feat.permute(0, 2, 3, 1).contiguous() feat = feat.view(feat.size(0), -1, feat.size(3)) feat = _gather_feat(feat, ind, trt=trt) return feat class RegWeightedL1Loss(nn.Module): def __init__(self): super(RegWeightedL1Loss, self).__init__() def forward(self, output, mask, ind, target): pred = _transpose_and_gather_feat(output, ind) mask = mask.float() loss = F.l1_loss(pred * mask, target * mask, size_average=False) loss = loss / (mask.sum() + 0.0001) return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.ones( [4, 4], dtype=torch.int64), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math from torch import nn import torch.onnx from torch.nn.parallel.scatter_gather import gather 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_per_fused_abs_add_div_gather_mul_sub_sum_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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) r5 = rindex // 4 % 16 r0 = rindex % 4 r2 = rindex // 16 % 4 r4 = rindex tmp0 = tl.load(in_ptr0 + r5, None, eviction_policy='evict_last') tmp7 = tl.load(in_ptr2 + r4, None) tmp9 = tl.load(in_ptr3 + r4, None) tmp1 = tl.full([RBLOCK], 16, tl.int32) tmp2 = tmp0 + tmp1 tmp3 = tmp0 < 0 tmp4 = tl.where(tmp3, tmp2, tmp0) tl.device_assert((0 <= tmp4) & (tmp4 < 16), 'index out of bounds: 0 <= tmp4 < 16') tmp6 = tl.load(in_ptr1 + (16 * r0 + 64 * r2 + tmp4 % 16), None, eviction_policy='evict_last') tmp8 = tmp6 * tmp7 tmp10 = tmp9 * tmp7 tmp11 = tmp8 - tmp10 tmp12 = tl_math.abs(tmp11) tmp13 = tl.broadcast_to(tmp12, [RBLOCK]) tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0)) tmp16 = tl.broadcast_to(tmp7, [RBLOCK]) tmp18 = triton_helpers.promote_to_tensor(tl.sum(tmp16, 0)) tmp19 = 0.0001 tmp20 = tmp18 + tmp19 tmp21 = tmp15 / tmp20 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp21, None) def call(args): arg0_1, arg1_1, arg2_1, arg3_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, 1)) assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg3_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_abs_add_div_gather_mul_sub_sum_0[grid(1)](buf2, arg1_1, arg0_1, arg2_1, arg3_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 del arg2_1 del arg3_1 return buf2, def _gather_feat(feat, ind, mask=None, trt=False): dim = feat.size(2) ind = ind.unsqueeze(2).expand(ind.size(0), ind.size(1), dim) if trt: feat = gather(feat, 1, ind) else: feat = torch.gather(feat, 1, ind) if mask is not None: mask = mask.unsqueeze(2).expand_as(feat) feat = feat[mask] feat = feat.view(-1, dim) return feat def _transpose_and_gather_feat(feat, ind, trt=False): feat = feat.permute(0, 2, 3, 1).contiguous() feat = feat.view(feat.size(0), -1, feat.size(3)) feat = _gather_feat(feat, ind, trt=trt) return feat class RegWeightedL1LossNew(nn.Module): def __init__(self): super(RegWeightedL1LossNew, self).__init__() def forward(self, input_0, input_1, input_2, input_3): arg0_1 = input_0 arg2_1 = input_1 arg1_1 = input_2 arg3_1 = input_3 output = call([arg0_1, arg1_1, arg2_1, arg3_1]) return output[0]
kuanhungchen/CenterNet-HarDNet
RegWeightedL1Loss
false
15,868
[ "MIT" ]
164
050d55a532706d989105982c5bc10f1c89edc8d2
https://github.com/kuanhungchen/CenterNet-HarDNet/tree/050d55a532706d989105982c5bc10f1c89edc8d2
Fusion
from _paritybench_helpers import _mock_config import torch from torch import nn import torch.nn.init class Fusion(nn.Module): def __init__(self, opt): super(Fusion, self).__init__() self.f_size = opt.embed_size self.gate0 = nn.Linear(self.f_size, self.f_size) self.gate1 = nn.Linear(self.f_size, self.f_size) self.fusion0 = nn.Linear(self.f_size, self.f_size) self.fusion1 = nn.Linear(self.f_size, self.f_size) def forward(self, vec1, vec2): features_1 = self.gate0(vec1) features_2 = self.gate1(vec2) t = torch.sigmoid(self.fusion0(features_1) + self.fusion1(features_2)) f = t * features_1 + (1 - t) * features_2 return f def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'opt': _mock_config(embed_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 import torch.nn.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_add_mul_rsub_sigmoid_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex 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') tmp8 = tl.load(in_ptr3 + x2, xmask) tmp12 = tl.load(in_ptr4 + x2, xmask) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp7 = tl.sigmoid(tmp6) tmp9 = tmp7 * tmp8 tmp10 = 1.0 tmp11 = tmp10 - tmp7 tmp13 = tmp11 * tmp12 tmp14 = tmp9 + tmp13 tl.store(in_out_ptr0 + x2, tmp7, xmask) tl.store(out_ptr0 + x2, tmp14, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10) = 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, 4), (64, 16, 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,)) 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_6, (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((64, 4), (4, 1), torch.float32) extern_kernels.mm(buf0, reinterpret_tensor(primals_7, (4, 4), (1, 4 ), 0), out=buf2) buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(buf1, reinterpret_tensor(primals_9, (4, 4), (1, 4 ), 0), out=buf3) buf4 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf2 buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mul_rsub_sigmoid_0[grid(256)](buf4, primals_8, buf3, primals_10, buf0, buf1, buf5, 256, XBLOCK=256, num_warps= 4, num_stages=1) del buf3 del primals_10 del primals_8 return buf5, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf0, reinterpret_tensor(primals_6, (64, 4), (4, 1), 0 ), buf1, buf4, primals_9, primals_7 class FusionNew(nn.Module): def __init__(self, opt): super(FusionNew, self).__init__() self.f_size = opt.embed_size self.gate0 = nn.Linear(self.f_size, self.f_size) self.gate1 = nn.Linear(self.f_size, self.f_size) self.fusion0 = nn.Linear(self.f_size, self.f_size) self.fusion1 = nn.Linear(self.f_size, self.f_size) def forward(self, input_0, input_1): primals_1 = self.gate0.weight primals_2 = self.gate0.bias primals_4 = self.gate1.weight primals_5 = self.gate1.bias primals_7 = self.fusion0.weight primals_8 = self.fusion0.bias primals_9 = self.fusion1.weight primals_10 = self.fusion1.bias primals_3 = input_0 primals_6 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10]) return output[0]
kywen1119/DSRAN
Fusion
false
15,869
[ "Apache-2.0" ]
56
eb5e515c8d9e527de493f32b62469107a9d398e7
https://github.com/kywen1119/DSRAN/tree/eb5e515c8d9e527de493f32b62469107a9d398e7
folder
import torch from torch import nn import torch.nn.functional as F import torch.nn.parallel class folder(nn.Module): def __init__(self): super().__init__() def forward(self, feature_map): N, _, H, W = feature_map.size() feature_map = F.unfold(feature_map, kernel_size=3, padding=1) feature_map = feature_map.view(N, -1, H, W) return feature_map 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.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_im2col_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl .constexpr, XBLOCK: tl.constexpr): ynumel = 144 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 x4 = xindex // 4 y1 = yindex // 3 % 3 x3 = xindex % 4 y0 = yindex % 3 x6 = xindex y2 = yindex // 9 y7 = yindex tmp0 = -1 + x4 + y1 tmp1 = tl.full([1, 1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.full([1, 1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = -1 + x3 + y0 tmp6 = tmp5 >= tmp1 tmp7 = tmp5 < tmp3 tmp8 = tmp2 & tmp4 tmp9 = tmp8 & tmp6 tmp10 = tmp9 & tmp7 tmp11 = tl.load(in_ptr0 + (-5 + x6 + y0 + 4 * y1 + 16 * y2), tmp10 & xmask & ymask, eviction_policy='evict_last', other=0.0) tl.store(out_ptr0 + (x6 + 16 * y7), tmp11, xmask & ymask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 3, 3, 4, 4), (576, 144, 48, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_im2col_0[grid(144, 16)](arg0_1, buf0, 144, 16, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del arg0_1 return reinterpret_tensor(buf0, (4, 36, 4, 4), (576, 16, 4, 1), 0), class folderNew(nn.Module): def __init__(self): super().__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
lbin/AdelaiDet
folder
false
15,870
[ "BSD-2-Clause" ]
277
9bfb73c51d6e6cd1348cb9ed2174b1cb63bc662a
https://github.com/lbin/AdelaiDet/tree/9bfb73c51d6e6cd1348cb9ed2174b1cb63bc662a
CEL
import torch from torch import nn class CEL(nn.Module): def __init__(self): super(CEL, self).__init__() None self.eps = 1e-06 def forward(self, pred, target): pred = pred.sigmoid() intersection = pred * target numerator = (pred - intersection).sum() + (target - intersection).sum() denominator = pred.sum() + target.sum() return numerator / (denominator + self.eps) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_div_mul_sigmoid_sub_sum_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp2 = tl.load(in_ptr1 + r0, None) tmp1 = tl.sigmoid(tmp0) tmp3 = tmp1 * tmp2 tmp4 = tmp1 - tmp3 tmp5 = tl.broadcast_to(tmp4, [RBLOCK]) tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0)) tmp8 = tmp2 - tmp3 tmp9 = tl.broadcast_to(tmp8, [RBLOCK]) tmp11 = triton_helpers.promote_to_tensor(tl.sum(tmp9, 0)) tmp12 = tl.broadcast_to(tmp1, [RBLOCK]) tmp14 = triton_helpers.promote_to_tensor(tl.sum(tmp12, 0)) tmp15 = tl.broadcast_to(tmp2, [RBLOCK]) tmp17 = triton_helpers.promote_to_tensor(tl.sum(tmp15, 0)) tmp18 = tmp7 + tmp11 tmp19 = tmp14 + tmp17 tmp20 = 1e-06 tmp21 = tmp19 + tmp20 tmp22 = tmp18 / tmp21 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp22, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf4 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_div_mul_sigmoid_sub_sum_0[grid(1)](buf4, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf4, class CELNew(nn.Module): def __init__(self): super(CELNew, self).__init__() None self.eps = 1e-06 def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
lartpang/MINet
CEL
false
15,871
[ "MIT" ]
202
0f4ecf70010af83b432bebc614af90d86a4a6564
https://github.com/lartpang/MINet/tree/0f4ecf70010af83b432bebc614af90d86a4a6564
StackTime
import torch import torch.nn as nn import torch.utils.data import torch.jit import torch.optim import torch.utils.collect_env import torch.nn.parallel import torch.utils.data.distributed class StackTime(nn.Module): def __init__(self, factor): super().__init__() self.factor = int(factor) def forward(self, x, x_lens): seq = [x] for i in range(1, self.factor): tmp = torch.zeros_like(x) tmp[:-i, :, :] = x[i:, :, :] seq.append(tmp) x_lens = (x_lens.int() + self.factor - 1) // self.factor return torch.cat(seq, dim=2)[::self.factor, :, :], x_lens def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'factor': 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.jit import torch.optim import torch.utils.collect_env import torch.nn.parallel import torch.utils.data.distributed assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 % 16 x0 = xindex % 4 x4 = xindex // 64 x3 = xindex // 256 x2 = xindex // 64 % 4 x5 = 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 + 16 * x4), tmp4 & xmask, other=0.0) tmp6 = tmp0 >= tmp3 tmp7 = tl.full([1], 8, tl.int64) tmp8 = tmp0 < tmp7 tmp9 = tmp6 & tmp8 tmp10 = x3 tmp11 = tl.full([1], 3, tl.int64) tmp12 = tmp10 < tmp11 tmp13 = tmp12 & tmp9 tmp14 = tl.load(in_ptr0 + (64 + x0 + 4 * (-4 + x1) + 16 * x4), tmp13 & xmask, other=0.0) tmp15 = 0.0 tmp16 = tl.where(tmp12, tmp14, tmp15) tmp17 = tl.full(tmp16.shape, 0.0, tmp16.dtype) tmp18 = tl.where(tmp9, tmp16, tmp17) tmp19 = tmp0 >= tmp7 tmp20 = tl.full([1], 12, tl.int64) tmp21 = tmp0 < tmp20 tmp22 = tmp19 & tmp21 tmp23 = tl.full([1], 2, tl.int64) tmp24 = tmp10 < tmp23 tmp25 = tmp24 & tmp22 tmp26 = tl.load(in_ptr0 + (128 + x0 + 4 * (-8 + x1) + 16 * x4), tmp25 & xmask, other=0.0) tmp27 = tl.where(tmp24, tmp26, tmp15) tmp28 = tl.full(tmp27.shape, 0.0, tmp27.dtype) tmp29 = tl.where(tmp22, tmp27, tmp28) tmp30 = tmp0 >= tmp20 tl.full([1], 16, tl.int64) tmp33 = tl.full([1], 1, tl.int64) tmp34 = tmp10 < tmp33 tmp35 = tmp34 & tmp30 tmp36 = tl.load(in_ptr0 + (192 + x0 + 4 * (-12 + x1) + 16 * x2), tmp35 & xmask, eviction_policy='evict_last', other=0.0) tmp37 = tl.where(tmp34, tmp36, tmp15) tmp38 = tl.full(tmp37.shape, 0.0, tmp37.dtype) tmp39 = tl.where(tmp30, tmp37, tmp38) tmp40 = tl.where(tmp22, tmp29, tmp39) tmp41 = tl.where(tmp9, tmp18, tmp40) tmp42 = tl.where(tmp4, tmp5, tmp41) tl.store(out_ptr0 + x5, tmp42, xmask) @triton.jit def triton_poi_fused__to_copy_add_floor_divide_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 = tmp0.to(tl.int32) tmp2 = tl.full([1], 4, tl.int32) tmp3 = tmp1 + tmp2 tmp4 = tl.full([1], 1, tl.int32) tmp5 = tmp3 - tmp4 tmp6 = tl.where((tmp5 < 0) != (tmp2 < 0), tl.where(tmp5 % tmp2 != 0, tmp5 // tmp2 - 1, tmp5 // tmp2), tmp5 // tmp2) tl.store(out_ptr0 + x0, tmp6, 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, 16, 4), (256, 64, 4, 1), torch.float32 ) get_raw_stream(0) triton_poi_fused_cat_0[grid(1024)](arg0_1, buf0, 1024, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.int32) triton_poi_fused__to_copy_add_floor_divide_sub_1[grid(256)](arg1_1, buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg1_1 return reinterpret_tensor(buf0, (1, 4, 16, 4), (1024, 64, 4, 1), 0), buf1 class StackTimeNew(nn.Module): def __init__(self, factor): super().__init__() self.factor = int(factor) def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0], output[1]
lamyiowce/training
StackTime
false
15,872
[ "Apache-2.0" ]
567
da4c959b5a7b65091b850872cdd4014d768c087c
https://github.com/lamyiowce/training/tree/da4c959b5a7b65091b850872cdd4014d768c087c
LayerNormalization
import torch from torch import nn from torch.autograd import * class LayerNormalization(nn.Module): def __init__(self, d_hid, eps=0.001): super(LayerNormalization, self).__init__() self.gamma = nn.Parameter(torch.ones(d_hid), requires_grad=True) self.beta = nn.Parameter(torch.zeros(d_hid), requires_grad=True) self.eps = eps def forward(self, z): mean = z.mean(dim=-1, keepdim=True) std = z.std(dim=-1, keepdim=True) ln_out = (z - mean.expand_as(z)) / (std.expand_as(z) + self.eps) ln_out = self.gamma.expand_as(ln_out) * ln_out + self.beta.expand_as( ln_out) return ln_out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'d_hid': 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 from torch import nn from torch.autograd 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_div_mul_sub_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 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_ptr1 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp30 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp8 = tmp6 + tmp7 tmp9 = 4.0 tmp10 = tmp8 / tmp9 tmp11 = tmp1 - tmp10 tmp12 = tmp2 - tmp10 tmp13 = tmp12 * tmp12 tmp14 = tmp3 - tmp10 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp10 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp7 - tmp10 tmp21 = tmp20 * tmp20 tmp22 = tmp19 + tmp21 tmp23 = 3.0 tmp24 = tmp22 / tmp23 tmp25 = libdevice.sqrt(tmp24) tmp26 = 0.001 tmp27 = tmp25 + tmp26 tmp28 = tmp11 / tmp27 tmp29 = tmp0 * tmp28 tmp31 = tmp29 + tmp30 tl.store(out_ptr0 + x2, 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,)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_mul_sub_0[grid(256)](primals_2, primals_1, primals_3, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 del primals_3 return buf0, primals_1 class LayerNormalizationNew(nn.Module): def __init__(self, d_hid, eps=0.001): super(LayerNormalizationNew, self).__init__() self.gamma = nn.Parameter(torch.ones(d_hid), requires_grad=True) self.beta = nn.Parameter(torch.zeros(d_hid), requires_grad=True) 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]
learnerhouse/ner-bert
LayerNormalization
false
15,873
[ "MIT" ]
391
606328a27a7313b6c22b78590e06618ad77402cd
https://github.com/learnerhouse/ner-bert/tree/606328a27a7313b6c22b78590e06618ad77402cd
D
import torch import torch.nn as nn import torch.nn.functional as F class D(nn.Module): def __init__(self): super(D, self).__init__() def forward(self, p, z): z = z.detach() p = F.normalize(p, p=2, dim=1) z = F.normalize(z, p=2, dim=1) return -(p * z).sum(dim=1).mean() def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import 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_div_mul_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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') tmp16 = tl.load(in_ptr1 + x3, xmask) tmp17 = tl.load(in_ptr1 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp19 = tl.load(in_ptr1 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp22 = tl.load(in_ptr1 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp25 = tl.load(in_ptr1 + (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-12 tmp14 = triton_helpers.maximum(tmp12, tmp13) tmp15 = tmp0 / tmp14 tmp18 = tmp17 * tmp17 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp23 = tmp22 * tmp22 tmp24 = tmp21 + tmp23 tmp26 = tmp25 * tmp25 tmp27 = tmp24 + tmp26 tmp28 = libdevice.sqrt(tmp27) tmp29 = triton_helpers.maximum(tmp28, tmp13) tmp30 = tmp16 / tmp29 tmp31 = tmp15 * tmp30 tl.store(out_ptr0 + x3, tmp31, xmask) @triton.jit def triton_per_fused_mean_neg_sum_1(in_out_ptr0, in_ptr0, 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) tmp3 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None) tmp5 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK]) tmp9 = tl.sum(tmp7, 1)[:, None] tmp10 = 64.0 tmp11 = tmp9 / tmp10 tmp12 = -tmp11 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 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) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_div_mul_0[grid(256)](arg1_1, arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 del arg1_1 buf1 = empty_strided_cuda((), (), torch.float32) buf2 = buf1 del buf1 triton_per_fused_mean_neg_sum_1[grid(1)](buf2, buf0, 1, 64, XBLOCK= 1, num_warps=2, num_stages=1) del buf0 return buf2, class DNew(nn.Module): def __init__(self): super(DNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
leaderj1001/SimSiam
D
false
15,874
[ "MIT" ]
53
ed36348d3d5a8621674c78c3ed77c1188bd18e16
https://github.com/leaderj1001/SimSiam/tree/ed36348d3d5a8621674c78c3ed77c1188bd18e16
TishbyNet
import math import torch import numpy as np import torch.nn as nn from torch.nn import functional as F def ema(mu, alpha, past_ema): return alpha * mu + (1.0 - alpha) * past_ema def ema_loss(x, running_mean, alpha): t_exp = torch.exp(torch.logsumexp(x, 0) - math.log(x.shape[0])).detach() if running_mean == 0: running_mean = t_exp else: running_mean = ema(t_exp, alpha, running_mean.item()) t_log = EMALoss.apply(x, running_mean) return t_log, running_mean class ConcatLayer(nn.Module): def __init__(self, dim=1): super().__init__() self.dim = dim def forward(self, x, y): return torch.cat((x, y), self.dim) class CustomSequential(nn.Sequential): def forward(self, *input): for module in self._modules.values(): if isinstance(input, tuple): input = module(*input) else: input = module(input) return input class EMALoss(torch.autograd.Function): @staticmethod def forward(ctx, input, running_ema): ctx.save_for_backward(input, running_ema) input_log_sum_exp = input.exp().mean().log() return input_log_sum_exp @staticmethod def backward(ctx, grad_output): input, running_mean = ctx.saved_tensors grad = grad_output * input.exp().detach() / (running_mean + EPS ) / input.shape[0] return grad, None class Mine(nn.Module): def __init__(self, T, loss='mine', alpha=0.01, method=None): super().__init__() self.running_mean = 0 self.loss = loss self.alpha = alpha self.method = method if method == 'concat': if isinstance(T, nn.Sequential): self.T = CustomSequential(ConcatLayer(), *T) else: self.T = CustomSequential(ConcatLayer(), T) else: self.T = T def forward(self, x, z, z_marg=None): if z_marg is None: z_marg = z[torch.randperm(x.shape[0])] t = self.T(x, z).mean() t_marg = self.T(x, z_marg) if self.loss in ['mine']: second_term, self.running_mean = ema_loss(t_marg, self. running_mean, self.alpha) elif self.loss in ['fdiv']: second_term = torch.exp(t_marg - 1).mean() elif self.loss in ['mine_biased']: second_term = torch.logsumexp(t_marg, 0) - math.log(t_marg.shape[0] ) return -t + second_term def mi(self, x, z, z_marg=None): if isinstance(x, np.ndarray): x = torch.from_numpy(x).float() if isinstance(z, np.ndarray): z = torch.from_numpy(z).float() with torch.no_grad(): mi = -self.forward(x, z, z_marg) return mi def optimize(self, X, Y, iters, batch_size, opt=None): if opt is None: opt = torch.optim.Adam(self.parameters(), lr=0.0001) for iter in range(1, iters + 1): mu_mi = 0 for x, y in utils.batch(X, Y, batch_size): opt.zero_grad() loss = self.forward(x, y) loss.backward() opt.step() mu_mi -= loss.item() if iter % (iters // 3) == 0: pass final_mi = self.mi(X, Y) None return final_mi class TishbyNet(nn.Module): def __init__(self, input_dim, output_dim, activation='tanh', device='cpu'): super().__init__() self.device = device self.fc1 = nn.Linear(input_dim, 12) self.fc2 = nn.Linear(12, 10) self.fc3 = nn.Linear(10, 7) self.fc4 = nn.Linear(7, 5) self.fc5 = nn.Linear(5, 4) self.fc6 = nn.Linear(4, 3) self.fc7 = nn.Linear(3, output_dim) self.activation = activation self.softmax = nn.Softmax() def non_linear(self, x): if self.activation == 'tanh': return torch.tanh(x) elif self.activation == 'relu': return F.relu(x) else: raise NotImplementedError def forward(self, x): return self.get_layer_outputs(x)[-1] def get_layer_outputs(self, x): x1 = self.non_linear(self.fc1(x)) x2 = self.non_linear(self.fc2(x1)) x3 = self.non_linear(self.fc3(x2)) x4 = self.non_linear(self.fc4(x3)) x5 = self.non_linear(self.fc5(x4)) x6 = self.non_linear(self.fc6(x5)) out = self.fc7(x6) return [x1, x2, x3, x4, x5, x6, out] def estimate_layerwise_mutual_information(self, x, target, iters): n, input_dim = target.shape layer_outputs = self.get_layer_outputs(x) layer_outputs[-1] = F.softmax(layer_outputs[-1]) to_return = dict() for layer_id, layer_output in enumerate(layer_outputs): _, layer_dim = layer_output.shape statistics_network = nn.Sequential(nn.Linear(input_dim + layer_dim, 400), nn.ReLU(), nn.Linear(400, 400), nn.ReLU(), nn.Linear(400, 1)) mi_estimator = Mine(T=statistics_network) mi = mi_estimator.optimize(target, layer_output.detach(), iters =iters, batch_size=n // 1, opt=None) to_return[layer_id] = mi.item() return to_return def calculate_information_plane(self, x, y, iters=100): info_x_t = self.estimate_layerwise_mutual_information(x, x, iters) info_y_t = self.estimate_layerwise_mutual_information(x, y, iters) return info_x_t, info_y_t class T(nn.Module): def __init__(self, x_dim, z_dim): super().__init__() self.layers = CustomSequential(ConcatLayer(), nn.Linear(x_dim + z_dim, 400), nn.ReLU(), nn.Linear(400, 400), nn.ReLU(), nn. Linear(400, 400), nn.ReLU(), nn.Linear(400, 1)) def forward(self, x, z): return self.layers(x, z) 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.triton_helpers import libdevice import math import numpy as np import torch.nn as 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_poi_fused_tanh_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 768 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 12 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_tanh_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 640 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 10 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_tanh_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 448 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 7 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_tanh_3(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 320 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 5 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_tanh_4(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_tanh_5(in_out_ptr0, in_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_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, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15) = args args.clear() assert_size_stride(primals_1, (12, 4), (4, 1)) assert_size_stride(primals_2, (12,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (10, 12), (12, 1)) assert_size_stride(primals_5, (10,), (1,)) assert_size_stride(primals_6, (7, 10), (10, 1)) assert_size_stride(primals_7, (7,), (1,)) assert_size_stride(primals_8, (5, 7), (7, 1)) assert_size_stride(primals_9, (5,), (1,)) assert_size_stride(primals_10, (4, 5), (5, 1)) assert_size_stride(primals_11, (4,), (1,)) assert_size_stride(primals_12, (3, 4), (4, 1)) assert_size_stride(primals_13, (3,), (1,)) assert_size_stride(primals_14, (4, 3), (3, 1)) assert_size_stride(primals_15, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 12), (12, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 12), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 12), (192, 48, 12, 1), 0) del buf0 get_raw_stream(0) triton_poi_fused_tanh_0[grid(768)](buf1, primals_2, 768, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 10), (10, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 12), (12, 1), 0), reinterpret_tensor(primals_4, (12, 10), (1, 12), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 10), (160, 40, 10, 1), 0) del buf2 triton_poi_fused_tanh_1[grid(640)](buf3, primals_5, 640, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 7), (7, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf3, (64, 10), (10, 1), 0), reinterpret_tensor(primals_6, (10, 7), (1, 10), 0), out=buf4) buf5 = reinterpret_tensor(buf4, (4, 4, 4, 7), (112, 28, 7, 1), 0) del buf4 triton_poi_fused_tanh_2[grid(448)](buf5, primals_7, 448, XBLOCK=128, num_warps=4, num_stages=1) del primals_7 buf6 = empty_strided_cuda((64, 5), (5, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf5, (64, 7), (7, 1), 0), reinterpret_tensor(primals_8, (7, 5), (1, 7), 0), out=buf6) buf7 = reinterpret_tensor(buf6, (4, 4, 4, 5), (80, 20, 5, 1), 0) del buf6 triton_poi_fused_tanh_3[grid(320)](buf7, primals_9, 320, XBLOCK=128, num_warps=4, num_stages=1) del primals_9 buf8 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf7, (64, 5), (5, 1), 0), reinterpret_tensor(primals_10, (5, 4), (1, 5), 0), out=buf8) buf9 = reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf8 triton_poi_fused_tanh_4[grid(256)](buf9, primals_11, 256, XBLOCK= 256, num_warps=4, num_stages=1) del primals_11 buf10 = empty_strided_cuda((64, 3), (3, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf9, (64, 4), (4, 1), 0), reinterpret_tensor(primals_12, (4, 3), (1, 4), 0), out=buf10) buf11 = reinterpret_tensor(buf10, (4, 4, 4, 3), (48, 12, 3, 1), 0) del buf10 triton_poi_fused_tanh_5[grid(192)](buf11, primals_13, 192, XBLOCK= 128, num_warps=4, num_stages=1) del primals_13 buf12 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_15, reinterpret_tensor(buf11, (64, 3), (3, 1), 0), reinterpret_tensor(primals_14, (3, 4), (1, 3), 0), alpha=1, beta=1, out=buf12) del primals_15 return (reinterpret_tensor(buf12, (4, 4, 4, 4), (64, 16, 4, 1), 0), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), buf1, buf3, buf5, buf7, buf9, buf11, primals_14, primals_12, primals_10, primals_8, primals_6, primals_4) def ema(mu, alpha, past_ema): return alpha * mu + (1.0 - alpha) * past_ema def ema_loss(x, running_mean, alpha): t_exp = torch.exp(torch.logsumexp(x, 0) - math.log(x.shape[0])).detach() if running_mean == 0: running_mean = t_exp else: running_mean = ema(t_exp, alpha, running_mean.item()) t_log = EMALoss.apply(x, running_mean) return t_log, running_mean class ConcatLayer(nn.Module): def __init__(self, dim=1): super().__init__() self.dim = dim def forward(self, x, y): return torch.cat((x, y), self.dim) class CustomSequential(nn.Sequential): def forward(self, *input): for module in self._modules.values(): if isinstance(input, tuple): input = module(*input) else: input = module(input) return input class EMALoss(torch.autograd.Function): @staticmethod def forward(ctx, input, running_ema): ctx.save_for_backward(input, running_ema) input_log_sum_exp = input.exp().mean().log() return input_log_sum_exp @staticmethod def backward(ctx, grad_output): input, running_mean = ctx.saved_tensors grad = grad_output * input.exp().detach() / (running_mean + EPS ) / input.shape[0] return grad, None class Mine(nn.Module): def __init__(self, T, loss='mine', alpha=0.01, method=None): super().__init__() self.running_mean = 0 self.loss = loss self.alpha = alpha self.method = method if method == 'concat': if isinstance(T, nn.Sequential): self.T = CustomSequential(ConcatLayer(), *T) else: self.T = CustomSequential(ConcatLayer(), T) else: self.T = T def forward(self, x, z, z_marg=None): if z_marg is None: z_marg = z[torch.randperm(x.shape[0])] t = self.T(x, z).mean() t_marg = self.T(x, z_marg) if self.loss in ['mine']: second_term, self.running_mean = ema_loss(t_marg, self. running_mean, self.alpha) elif self.loss in ['fdiv']: second_term = torch.exp(t_marg - 1).mean() elif self.loss in ['mine_biased']: second_term = torch.logsumexp(t_marg, 0) - math.log(t_marg.shape[0] ) return -t + second_term def mi(self, x, z, z_marg=None): if isinstance(x, np.ndarray): x = torch.from_numpy(x).float() if isinstance(z, np.ndarray): z = torch.from_numpy(z).float() with torch.no_grad(): mi = -self.forward(x, z, z_marg) return mi def optimize(self, X, Y, iters, batch_size, opt=None): if opt is None: opt = torch.optim.Adam(self.parameters(), lr=0.0001) for iter in range(1, iters + 1): mu_mi = 0 for x, y in utils.batch(X, Y, batch_size): opt.zero_grad() loss = self.forward(x, y) loss.backward() opt.step() mu_mi -= loss.item() if iter % (iters // 3) == 0: pass final_mi = self.mi(X, Y) None return final_mi class TishbyNetNew(nn.Module): def __init__(self, input_dim, output_dim, activation='tanh', device='cpu'): super().__init__() self.device = device self.fc1 = nn.Linear(input_dim, 12) self.fc2 = nn.Linear(12, 10) self.fc3 = nn.Linear(10, 7) self.fc4 = nn.Linear(7, 5) self.fc5 = nn.Linear(5, 4) self.fc6 = nn.Linear(4, 3) self.fc7 = nn.Linear(3, output_dim) self.activation = activation self.softmax = nn.Softmax() def non_linear(self, x): if self.activation == 'tanh': return torch.tanh(x) elif self.activation == 'relu': return F.relu(x) else: raise NotImplementedError def get_layer_outputs(self, x): x1 = self.non_linear(self.fc1(x)) x2 = self.non_linear(self.fc2(x1)) x3 = self.non_linear(self.fc3(x2)) x4 = self.non_linear(self.fc4(x3)) x5 = self.non_linear(self.fc5(x4)) x6 = self.non_linear(self.fc6(x5)) out = self.fc7(x6) return [x1, x2, x3, x4, x5, x6, out] def estimate_layerwise_mutual_information(self, x, target, iters): n, input_dim = target.shape layer_outputs = self.get_layer_outputs(x) layer_outputs[-1] = F.softmax(layer_outputs[-1]) to_return = dict() for layer_id, layer_output in enumerate(layer_outputs): _, layer_dim = layer_output.shape statistics_network = nn.Sequential(nn.Linear(input_dim + layer_dim, 400), nn.ReLU(), nn.Linear(400, 400), nn.ReLU(), nn.Linear(400, 1)) mi_estimator = Mine(T=statistics_network) mi = mi_estimator.optimize(target, layer_output.detach(), iters =iters, batch_size=n // 1, opt=None) to_return[layer_id] = mi.item() return to_return def calculate_information_plane(self, x, y, iters=100): info_x_t = self.estimate_layerwise_mutual_information(x, x, iters) info_y_t = self.estimate_layerwise_mutual_information(x, y, iters) return info_x_t, info_y_t 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_8 = self.fc4.weight primals_9 = self.fc4.bias primals_10 = self.fc5.weight primals_11 = self.fc5.bias primals_12 = self.fc6.weight primals_13 = self.fc6.bias primals_14 = self.fc7.weight primals_15 = self.fc7.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] class T(nn.Module): def __init__(self, x_dim, z_dim): super().__init__() self.layers = CustomSequential(ConcatLayer(), nn.Linear(x_dim + z_dim, 400), nn.ReLU(), nn.Linear(400, 400), nn.ReLU(), nn. Linear(400, 400), nn.ReLU(), nn.Linear(400, 1)) def forward(self, x, z): return self.layers(x, z)
krylea/mine-pytorch
TishbyNet
false
15,875
[ "MIT" ]
108
a638ca3e46ff21a3b9dfebe25480eaed0e3304bc
https://github.com/krylea/mine-pytorch/tree/a638ca3e46ff21a3b9dfebe25480eaed0e3304bc
ConvLayer
import torch import torch.nn as nn class ConvLayer(nn.Module): """1-D Convolution layer to extract high-level features of each time-series input :param n_features: Number of input features/nodes :param window_size: length of the input sequence :param kernel_size: size of kernel to use in the convolution operation """ def __init__(self, n_features, kernel_size=7): super(ConvLayer, self).__init__() self.padding = nn.ConstantPad1d((kernel_size - 1) // 2, 0.0) self.conv = nn.Conv1d(in_channels=n_features, out_channels= n_features, kernel_size=kernel_size) self.relu = nn.ReLU() def forward(self, x): x = x.permute(0, 2, 1) x = self.padding(x) x = self.relu(self.conv(x)) return x.permute(0, 2, 1) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'n_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers 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_constant_pad_nd_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 10 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 = -3 + x2 tmp1 = tl.full([1, 1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.full([1, 1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tmp2 & tmp4 tmp6 = tl.load(in_ptr0 + (-12 + y0 + 4 * x2 + 16 * y1), tmp5 & xmask & ymask, eviction_policy='evict_last', other=0.0) tl.store(out_ptr0 + (x2 + 10 * y3), tmp6, xmask & ymask) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_1(in_out_ptr0, 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 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 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x3, tmp4, xmask) tl.store(out_ptr0 + x3, tmp6, 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, 7), (28, 7, 1)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 10), (40, 10, 1), torch.float32) get_raw_stream(0) triton_poi_fused_constant_pad_nd_0[grid(16, 10)](primals_1, buf0, 16, 10, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) del primals_1 buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 4), (16, 4, 1)) buf2 = buf1 del buf1 buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_1[grid(64)](buf2, primals_3, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_3 return reinterpret_tensor(buf2, (4, 4, 4), (16, 1, 4), 0 ), primals_2, buf0, buf3 class ConvLayerNew(nn.Module): """1-D Convolution layer to extract high-level features of each time-series input :param n_features: Number of input features/nodes :param window_size: length of the input sequence :param kernel_size: size of kernel to use in the convolution operation """ def __init__(self, n_features, kernel_size=7): super(ConvLayerNew, self).__init__() self.padding = nn.ConstantPad1d((kernel_size - 1) // 2, 0.0) self.conv = nn.Conv1d(in_channels=n_features, out_channels= n_features, kernel_size=kernel_size) self.relu = nn.ReLU() def forward(self, input_0): primals_2 = self.conv.weight primals_3 = self.conv.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
lawson-source/mtad-gat-pytorch
ConvLayer
false
15,876
[ "MIT" ]
93
9e671ea99dedd82ac55f53e53af1d1b56c13ebff
https://github.com/lawson-source/mtad-gat-pytorch/tree/9e671ea99dedd82ac55f53e53af1d1b56c13ebff
MixtureSynthesizers
import torch import torch.nn as nn class MixtureSynthesizers(nn.Module): def __init__(self, in_dims, sentence_length): super(MixtureSynthesizers, self).__init__() self.attention = nn.Parameter(torch.empty(1, sentence_length, sentence_length), requires_grad=True) nn.init.xavier_uniform_(self.attention) self.query_fc = nn.Linear(in_dims, in_dims) self.key_fc = nn.Linear(in_dims, in_dims) self.value_fc = nn.Linear(in_dims, in_dims) self.softmax = nn.Softmax(dim=-1) def forward(self, x): query = self.query_fc(x) key = self.key_fc(x).permute(0, 2, 1) vanilla_energy = torch.bmm(query, key) energy = self.attention + vanilla_energy attention = self.softmax(energy) value = self.value_fc(x) out = torch.bmm(attention, value) return out, attention def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'in_dims': 4, 'sentence_length': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused__softmax_add_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 x2 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x2, 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 * x2), 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 * x2), 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 * x2), 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 tl.store(out_ptr0 + x2, tmp14, xmask) tl.store(out_ptr1 + x2, tmp25, xmask) @triton.jit def triton_poi_fused__softmax_add_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 % 16 x4 = xindex x5 = xindex // 4 tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_out_ptr0 + x4, xmask) tmp3 = tl.load(in_ptr1 + x5, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr2 + x5, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 - tmp3 tmp5 = tl_math.exp(tmp4) tmp7 = tmp5 / tmp6 tl.store(in_out_ptr0 + x4, tmp7, 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, 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, (1, 4, 4), (16, 4, 1)) assert_size_stride(primals_7, (4, 4), (4, 1)) assert_size_stride(primals_8, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (16, 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((16, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(primals_3, (16, 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), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf1, (4, 4, 4), (16, 1, 4), 0), out=buf2) buf3 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf4 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) get_raw_stream(0) triton_poi_fused__softmax_add_0[grid(16)](primals_6, buf2, buf3, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1) buf5 = buf2 del buf2 triton_poi_fused__softmax_add_1[grid(64)](buf5, primals_6, buf3, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf3 del buf4 del primals_6 buf6 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_8, reinterpret_tensor(primals_3, (16, 4), (4, 1), 0), reinterpret_tensor(primals_7, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf6) del primals_7 del primals_8 buf7 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf5, reinterpret_tensor(buf6, (4, 4, 4), (16, 4, 1), 0), out=buf7) return buf7, buf5, reinterpret_tensor(primals_3, (16, 4), (4, 1), 0 ), buf5, reinterpret_tensor(buf6, (4, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf0, (4, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 0) class MixtureSynthesizersNew(nn.Module): def __init__(self, in_dims, sentence_length): super(MixtureSynthesizersNew, self).__init__() self.attention = nn.Parameter(torch.empty(1, sentence_length, sentence_length), requires_grad=True) nn.init.xavier_uniform_(self.attention) self.query_fc = nn.Linear(in_dims, in_dims) self.key_fc = nn.Linear(in_dims, in_dims) self.value_fc = nn.Linear(in_dims, in_dims) self.softmax = nn.Softmax(dim=-1) def forward(self, input_0): primals_6 = self.attention primals_1 = self.query_fc.weight primals_2 = self.query_fc.bias primals_4 = self.key_fc.weight primals_5 = self.key_fc.bias primals_7 = self.value_fc.weight primals_8 = self.value_fc.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8]) return output[0], output[1]
leaderj1001/Synthesizer-Rethinking-Self-Attention-Transformer-Models
MixtureSynthesizers
false
15,877
[ "MIT" ]
58
3ee5829438a8f9c063ae485e77c9ce7649d24139
https://github.com/leaderj1001/Synthesizer-Rethinking-Self-Attention-Transformer-Models/tree/3ee5829438a8f9c063ae485e77c9ce7649d24139
FactorizedSynthesizerDense
import torch import torch.nn as nn class FactorizedSynthesizerDense(nn.Module): def __init__(self, in_dims, sentence_length): super(FactorizedSynthesizerDense, self).__init__() self.a = 4 self.b = sentence_length // self.a self.a_proj = nn.Linear(in_dims, self.a) self.b_proj = nn.Linear(in_dims, self.b) self.value_fc = nn.Linear(in_dims, in_dims) self.softmax = nn.Softmax(dim=-1) def forward(self, x): A = self.a_proj(x).repeat([1, 1, self.b]) B = self.b_proj(x).repeat([1, 1, self.a]) energy = A * B attention = self.softmax(energy) value = self.value_fc(x) out = torch.bmm(attention, value) return out, attention def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'in_dims': 4, 'sentence_length': 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_repeat_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 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x2, tmp0, xmask) @triton.jit def triton_poi_fused__softmax_mul_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, 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 = 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 tl.store(out_ptr0 + x0, tmp14, xmask) tl.store(out_ptr1 + x0, tmp25, xmask) @triton.jit def triton_poi_fused__softmax_mul_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 x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x2, xmask) tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 * tmp1 tmp4 = tmp2 - tmp3 tmp5 = tl_math.exp(tmp4) tmp7 = tmp5 / tmp6 tl.store(out_ptr0 + x2, tmp7, 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), (16, 4, 1)) assert_size_stride(primals_4, (1, 4), (4, 1)) assert_size_stride(primals_5, (1,), (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.addmm(primals_2, reinterpret_tensor(primals_3, (16, 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 buf2 = empty_strided_cuda((16, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(primals_3, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 1), (1, 4), 0 ), alpha=1, beta=1, out=buf2) del primals_4 del primals_5 buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_repeat_0[grid(64)](buf2, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) buf4 = reinterpret_tensor(buf2, (4, 4, 1), (4, 1, 16), 0) del buf2 buf5 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) triton_poi_fused__softmax_mul_1[grid(16)](buf0, buf3, buf4, buf5, 16, XBLOCK=16, num_warps=1, num_stages=1) buf6 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_mul_2[grid(64)](buf0, buf3, buf4, buf5, buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf4 del buf5 buf7 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(primals_3, (16, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf7) del primals_6 del primals_7 buf8 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf6, reinterpret_tensor(buf7, (4, 4, 4), (16, 4, 1), 0), out=buf8) return buf8, buf6, reinterpret_tensor(primals_3, (16, 4), (4, 1), 0 ), buf0, buf3, buf6, reinterpret_tensor(buf7, (4, 4, 4), (16, 1, 4), 0) class FactorizedSynthesizerDenseNew(nn.Module): def __init__(self, in_dims, sentence_length): super(FactorizedSynthesizerDenseNew, self).__init__() self.a = 4 self.b = sentence_length // self.a self.a_proj = nn.Linear(in_dims, self.a) self.b_proj = nn.Linear(in_dims, self.b) self.value_fc = nn.Linear(in_dims, in_dims) self.softmax = nn.Softmax(dim=-1) def forward(self, input_0): primals_1 = self.a_proj.weight primals_2 = self.a_proj.bias primals_4 = self.b_proj.weight primals_5 = self.b_proj.bias primals_6 = self.value_fc.weight primals_7 = self.value_fc.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0], output[1]
leaderj1001/Synthesizer-Rethinking-Self-Attention-Transformer-Models
FactorizedSynthesizerDense
false
15,878
[ "MIT" ]
58
3ee5829438a8f9c063ae485e77c9ce7649d24139
https://github.com/leaderj1001/Synthesizer-Rethinking-Self-Attention-Transformer-Models/tree/3ee5829438a8f9c063ae485e77c9ce7649d24139
TemporalAttentionLayer
import torch import torch.nn as nn class TemporalAttentionLayer(nn.Module): """Single Graph Temporal Attention Layer :param n_features: number of input features/nodes :param window_size: length of the input sequence :param dropout: percentage of nodes to dropout :param alpha: negative slope used in the leaky rely activation function :param embed_dim: embedding dimension (output dimension of linear transformation) :param use_gatv2: whether to use the modified attention mechanism of GATv2 instead of standard GAT :param use_bias: whether to include a bias term in the attention layer """ def __init__(self, n_features, window_size, dropout, alpha, embed_dim= None, use_gatv2=True, use_bias=True): super(TemporalAttentionLayer, self).__init__() self.n_features = n_features self.window_size = window_size self.dropout = dropout self.use_gatv2 = use_gatv2 self.embed_dim = embed_dim if embed_dim is not None else n_features self.num_nodes = window_size self.use_bias = use_bias if self.use_gatv2: self.embed_dim *= 2 lin_input_dim = 2 * n_features a_input_dim = self.embed_dim else: lin_input_dim = n_features a_input_dim = 2 * self.embed_dim self.lin = nn.Linear(lin_input_dim, self.embed_dim) self.a = nn.Parameter(torch.empty((a_input_dim, 1))) nn.init.xavier_uniform_(self.a.data, gain=1.414) if self.use_bias: self.bias = nn.Parameter(torch.empty(window_size, window_size)) self.leakyrelu = nn.LeakyReLU(alpha) self.sigmoid = nn.Sigmoid() def forward(self, x): if self.use_gatv2: a_input = self._make_attention_input(x) a_input = self.leakyrelu(self.lin(a_input)) e = torch.matmul(a_input, self.a).squeeze(3) else: Wx = self.lin(x) a_input = self._make_attention_input(Wx) e = self.leakyrelu(torch.matmul(a_input, self.a)).squeeze(3) if self.use_bias: e += self.bias attention = torch.softmax(e, dim=2) attention = torch.dropout(attention, self.dropout, train=self.training) h = self.sigmoid(torch.matmul(attention, x)) return h def _make_attention_input(self, v): """Preparing the temporal attention mechanism. Creating matrix with all possible combinations of concatenations of node values: (v1, v2..)_t1 || (v1, v2..)_t1 (v1, v2..)_t1 || (v1, v2..)_t2 ... ... (v1, v2..)_tn || (v1, v2..)_t1 (v1, v2..)_tn || (v1, v2..)_t2 """ K = self.num_nodes blocks_repeating = v.repeat_interleave(K, dim=1) blocks_alternating = v.repeat(1, K, 1) combined = torch.cat((blocks_repeating, blocks_alternating), dim=2) if self.use_gatv2: return combined.view(v.size(0), K, K, 2 * self.n_features) else: return combined.view(v.size(0), K, K, 2 * self.embed_dim) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'n_features': 4, 'window_size': 4, 'dropout': 0.5, 'alpha': 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_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 8 x1 = xindex // 8 % 16 x2 = xindex // 128 x3 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (4 * (x1 // 4) + 16 * x2 + x0), tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 8, tl.int64) tmp9 = tl.load(in_ptr0 + (4 * (x1 % 4) + 16 * x2 + (-4 + x0)), tmp6 & xmask, eviction_policy='evict_last', other=0.0) tmp10 = tl.where(tmp4, tmp5, tmp9) tl.store(out_ptr0 + x3, tmp10, xmask) @triton.jit def triton_poi_fused_leaky_relu_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 8 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 = 4.0 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, 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 x2 = xindex x0 = xindex % 4 x1 = xindex // 4 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 * x0 + 16 * x1 + 16 * ((1 + 4 * x0) // 16)), 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 + 16 * x1 + 16 * ((1 + 2 * x0) // 8)), 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 + 16 * x1 + 16 * ((3 + 4 * x0) // 16)), 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 tl.store(out_ptr0 + x2, tmp14, xmask) tl.store(out_ptr1 + x2, tmp25, xmask) @triton.jit def triton_poi_fused__softmax_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 x4 = xindex x3 = xindex % 16 x5 = xindex // 4 tmp0 = tl.load(in_ptr0 + x4, xmask) tmp1 = tl.load(in_ptr1 + x3, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x5, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr3 + x5, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 - tmp3 tmp5 = tl_math.exp(tmp4) tmp7 = tmp5 / tmp6 tl.store(out_ptr0 + x4, tmp7, xmask) @triton.jit def triton_poi_fused_sigmoid_4(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) 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, (8, 8), (8, 1)) assert_size_stride(primals_3, (8,), (1,)) assert_size_stride(primals_4, (8, 1), (1, 1)) assert_size_stride(primals_5, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 16, 8), (128, 8, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(512)](primals_1, buf0, 512, XBLOCK=256, num_warps=4, num_stages=1) buf1 = empty_strided_cuda((64, 8), (8, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (64, 8), (8, 1), 0), reinterpret_tensor(primals_2, (8, 8), (1, 8), 0), out=buf1) del primals_2 buf2 = empty_strided_cuda((4, 4, 4, 8), (128, 32, 8, 1), torch.bool) buf3 = empty_strided_cuda((4, 4, 4, 8), (128, 32, 8, 1), torch.float32) triton_poi_fused_leaky_relu_1[grid(512)](buf1, primals_3, buf2, buf3, 512, XBLOCK=256, num_warps=4, num_stages=1) del buf1 del primals_3 buf4 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf3, (64, 8), (8, 1), 0), primals_4, out=buf4) buf5 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf6 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) triton_poi_fused__softmax_2[grid(16)](buf4, primals_5, buf5, buf6, 16, XBLOCK=16, num_warps=1, num_stages=1) buf7 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_3[grid(64)](buf4, primals_5, buf5, buf6, buf7, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf5 del buf6 del primals_5 buf8 = reinterpret_tensor(buf4, (4, 4, 4), (16, 4, 1), 0) del buf4 extern_kernels.bmm(buf7, primals_1, out=buf8) buf9 = buf8 del buf8 triton_poi_fused_sigmoid_4[grid(64)](buf9, 64, XBLOCK=64, num_warps =1, num_stages=1) return buf9, reinterpret_tensor(buf0, (64, 8), (8, 1), 0 ), buf2, buf7, buf9, reinterpret_tensor(primals_1, (4, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf3, (8, 64), (1, 8), 0 ), reinterpret_tensor(primals_4, (1, 8), (1, 1), 0) class TemporalAttentionLayerNew(nn.Module): """Single Graph Temporal Attention Layer :param n_features: number of input features/nodes :param window_size: length of the input sequence :param dropout: percentage of nodes to dropout :param alpha: negative slope used in the leaky rely activation function :param embed_dim: embedding dimension (output dimension of linear transformation) :param use_gatv2: whether to use the modified attention mechanism of GATv2 instead of standard GAT :param use_bias: whether to include a bias term in the attention layer """ def __init__(self, n_features, window_size, dropout, alpha, embed_dim= None, use_gatv2=True, use_bias=True): super(TemporalAttentionLayerNew, self).__init__() self.n_features = n_features self.window_size = window_size self.dropout = dropout self.use_gatv2 = use_gatv2 self.embed_dim = embed_dim if embed_dim is not None else n_features self.num_nodes = window_size self.use_bias = use_bias if self.use_gatv2: self.embed_dim *= 2 lin_input_dim = 2 * n_features a_input_dim = self.embed_dim else: lin_input_dim = n_features a_input_dim = 2 * self.embed_dim self.lin = nn.Linear(lin_input_dim, self.embed_dim) self.a = nn.Parameter(torch.empty((a_input_dim, 1))) nn.init.xavier_uniform_(self.a.data, gain=1.414) if self.use_bias: self.bias = nn.Parameter(torch.empty(window_size, window_size)) self.leakyrelu = nn.LeakyReLU(alpha) self.sigmoid = nn.Sigmoid() def _make_attention_input(self, v): """Preparing the temporal attention mechanism. Creating matrix with all possible combinations of concatenations of node values: (v1, v2..)_t1 || (v1, v2..)_t1 (v1, v2..)_t1 || (v1, v2..)_t2 ... ... (v1, v2..)_tn || (v1, v2..)_t1 (v1, v2..)_tn || (v1, v2..)_t2 """ K = self.num_nodes blocks_repeating = v.repeat_interleave(K, dim=1) blocks_alternating = v.repeat(1, K, 1) combined = torch.cat((blocks_repeating, blocks_alternating), dim=2) if self.use_gatv2: return combined.view(v.size(0), K, K, 2 * self.n_features) else: return combined.view(v.size(0), K, K, 2 * self.embed_dim) def forward(self, input_0): primals_4 = self.a primals_5 = self.bias primals_2 = self.lin.weight primals_3 = self.lin.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
lawson-source/mtad-gat-pytorch
TemporalAttentionLayer
false
15,879
[ "MIT" ]
93
9e671ea99dedd82ac55f53e53af1d1b56c13ebff
https://github.com/lawson-source/mtad-gat-pytorch/tree/9e671ea99dedd82ac55f53e53af1d1b56c13ebff
ResBlock3d
import torch from torch import nn class ResBlock3d(nn.Module): def __init__(self, in_ch, out_ch): super(ResBlock3d, self).__init__() self.conv1 = nn.Conv3d(in_ch, out_ch, 3, 1, padding=1) self.conv2 = nn.Conv3d(out_ch, out_ch, 3, 1, padding=1) self.bn = nn.InstanceNorm3d(in_ch) self.relu = nn.LeakyReLU() self.bn2 = nn.InstanceNorm3d(out_ch) nn.init.xavier_uniform(self.conv1.weight.data, 1.0) nn.init.xavier_uniform(self.conv2.weight.data, 1.0) bypass = [] if in_ch != out_ch: bypass.append(nn.Conv3d(in_ch, out_ch, 1, 1)) self.bypass = nn.Sequential(*bypass) def forward(self, inp): x = self.bn(inp) x = self.relu(x) x = self.conv1(x) x = self.bn2(x) x = self.relu(x) x = self.conv2(x) return x + self.bypass(inp) 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 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_per_fused__native_batch_norm_legit_leaky_relu_0(in_ptr0, out_ptr2, 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.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], 64, 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 = 64.0 tmp19 = tmp16 / tmp18 tmp20 = 1e-05 tmp21 = tmp19 + tmp20 tmp22 = libdevice.rsqrt(tmp21) tmp23 = tmp17 * tmp22 tmp24 = 0.0 tmp25 = tmp23 > tmp24 tmp26 = 0.01 tmp27 = tmp23 * tmp26 tmp28 = tl.where(tmp25, tmp23, tmp27) tl.store(out_ptr2 + (r1 + 64 * x0), tmp28, xmask) @triton.jit def triton_per_fused__native_batch_norm_legit_convolution_leaky_relu_1( in_out_ptr0, in_out_ptr1, in_ptr0, out_ptr0, out_ptr1, 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_out_ptr0 + (r1 + 64 * x0), xmask, other=0.0) tmp1 = tl.load(in_ptr0 + 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], 64, 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 = 64.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 tmp28 = 0.01 tmp29 = tmp25 * tmp28 tmp30 = tl.where(tmp27, tmp25, tmp29) tl.store(in_out_ptr0 + (r1 + 64 * x0), tmp2, xmask) tl.debug_barrier() tl.store(in_out_ptr1 + x0, tmp23, xmask) tl.store(out_ptr1 + (r1 + 64 * x0), tmp30, xmask) tl.store(out_ptr0 + x0, tmp12, xmask) @triton.jit def triton_poi_fused_add_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 x2 = xindex x1 = xindex // 64 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x2, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = 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, 3), (108, 27, 9, 3, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4, 3, 3, 3), (108, 27, 9, 3, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_per_fused__native_batch_norm_legit_leaky_relu_0[grid(4)]( primals_1, buf3, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) buf4 = extern_kernels.convolution(reinterpret_tensor(buf3, (1, 4, 4, 4, 4), (0, 64, 16, 4, 1), 0), primals_2, stride=(1, 1, 1), padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False, output_padding=(0, 0, 0), groups=1, bias=None) assert_size_stride(buf4, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1)) buf5 = buf4 del buf4 buf6 = empty_strided_cuda((1, 4, 1, 1, 1), (4, 1, 1, 1, 1), torch. float32) buf7 = empty_strided_cuda((1, 4, 1, 1, 1), (4, 1, 4, 4, 4), torch. float32) buf9 = reinterpret_tensor(buf7, (1, 4, 1, 1, 1), (4, 1, 1, 1, 1), 0) del buf7 buf10 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_per_fused__native_batch_norm_legit_convolution_leaky_relu_1[grid (4)](buf5, buf9, primals_3, buf6, buf10, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) del primals_3 buf11 = extern_kernels.convolution(reinterpret_tensor(buf10, (1, 4, 4, 4, 4), (0, 64, 16, 4, 1), 0), primals_4, stride=(1, 1, 1), padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False, output_padding=(0, 0, 0), groups=1, bias=None) assert_size_stride(buf11, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1)) buf12 = reinterpret_tensor(buf11, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf11 triton_poi_fused_add_2[grid(256)](buf12, primals_5, primals_1, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 del primals_5 return buf12, primals_2, primals_4, reinterpret_tensor(buf3, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0), buf5, buf6, buf9, reinterpret_tensor( buf10, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0) class ResBlock3dNew(nn.Module): def __init__(self, in_ch, out_ch): super(ResBlock3dNew, self).__init__() self.conv1 = nn.Conv3d(in_ch, out_ch, 3, 1, padding=1) self.conv2 = nn.Conv3d(out_ch, out_ch, 3, 1, padding=1) self.bn = nn.InstanceNorm3d(in_ch) self.relu = nn.LeakyReLU() self.bn2 = nn.InstanceNorm3d(out_ch) nn.init.xavier_uniform(self.conv1.weight.data, 1.0) nn.init.xavier_uniform(self.conv2.weight.data, 1.0) bypass = [] if in_ch != out_ch: bypass.append(nn.Conv3d(in_ch, out_ch, 1, 1)) self.bypass = nn.Sequential(*bypass) 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]
ldlasso2/hologan-pytorch
ResBlock3d
false
15,880
[ "BSD-3-Clause" ]
61
baec67d3673cc68e51434516d19465f3d6dd0a1b
https://github.com/ldlasso2/hologan-pytorch/tree/baec67d3673cc68e51434516d19465f3d6dd0a1b
HEL
import torch import torch.nn as nn import torch.nn.functional as F class HEL(nn.Module): def __init__(self): super(HEL, self).__init__() None self.eps = 1e-06 def edge_loss(self, pred, target): edge = target - F.avg_pool2d(target, kernel_size=5, stride=1, padding=2 ) edge[edge != 0] = 1 numerator = (edge * (pred - target).abs_()).sum([2, 3]) denominator = edge.sum([2, 3]) + self.eps return numerator / denominator def region_loss(self, pred, target): numerator_fore = (target - target * pred).sum([2, 3]) denominator_fore = target.sum([2, 3]) + self.eps numerator_back = ((1 - target) * pred).sum([2, 3]) denominator_back = (1 - target).sum([2, 3]) + self.eps return (numerator_fore / denominator_fore + numerator_back / denominator_back) def forward(self, pred, target): edge_loss = self.edge_loss(pred, target) region_loss = self.region_loss(pred, target) return (edge_loss + region_loss).mean() def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn import torch.nn.functional as F assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_abs_avg_pool2d_index_put_lift_fresh_mul_rsub_sub_sum_0( in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr4, out_ptr5, 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 // 4 r1 = rindex % 4 r3 = rindex x0 = xindex tmp118 = tl.load(in_ptr0 + (r3 + 16 * x0), xmask, other=0.0) tmp124 = tl.load(in_ptr1 + (r3 + 16 * x0), xmask, other=0.0) tmp0 = -2 + r2 tmp1 = tl.full([1, 1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.full([1, 1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tmp2 & tmp4 tmp6 = -2 + r1 tmp7 = tmp6 >= tmp1 tmp8 = tmp6 < tmp3 tmp9 = tmp7 & tmp8 tmp10 = tmp5 & tmp9 tmp11 = tl.load(in_ptr0 + (-10 + r3 + 16 * x0), tmp10 & xmask, other=0.0) tmp12 = -1 + r1 tmp13 = tmp12 >= tmp1 tmp14 = tmp12 < tmp3 tmp15 = tmp13 & tmp14 tmp16 = tmp5 & tmp15 tmp17 = tl.load(in_ptr0 + (-9 + r3 + 16 * x0), tmp16 & xmask, other=0.0) tmp18 = tmp17 + tmp11 tmp19 = r1 tmp20 = tmp19 >= tmp1 tmp21 = tmp19 < tmp3 tmp22 = tmp20 & tmp21 tmp23 = tmp5 & tmp22 tmp24 = tl.load(in_ptr0 + (-8 + r3 + 16 * x0), tmp23 & xmask, other=0.0) tmp25 = tmp24 + tmp18 tmp26 = 1 + r1 tmp27 = tmp26 >= tmp1 tmp28 = tmp26 < tmp3 tmp29 = tmp27 & tmp28 tmp30 = tmp5 & tmp29 tmp31 = tl.load(in_ptr0 + (-7 + r3 + 16 * x0), tmp30 & xmask, other=0.0) tmp32 = tmp31 + tmp25 tmp33 = 2 + r1 tmp34 = tmp33 >= tmp1 tmp35 = tmp33 < tmp3 tmp36 = tmp34 & tmp35 tmp37 = tmp5 & tmp36 tmp38 = tl.load(in_ptr0 + (-6 + r3 + 16 * x0), tmp37 & xmask, other=0.0) tmp39 = tmp38 + tmp32 tmp40 = -1 + r2 tmp41 = tmp40 >= tmp1 tmp42 = tmp40 < tmp3 tmp43 = tmp41 & tmp42 tmp44 = tmp43 & tmp9 tmp45 = tl.load(in_ptr0 + (-6 + r3 + 16 * x0), tmp44 & xmask, other=0.0) tmp46 = tmp45 + tmp39 tmp47 = tmp43 & tmp15 tmp48 = tl.load(in_ptr0 + (-5 + r3 + 16 * x0), tmp47 & xmask, other=0.0) tmp49 = tmp48 + tmp46 tmp50 = tmp43 & tmp22 tmp51 = tl.load(in_ptr0 + (-4 + r3 + 16 * x0), tmp50 & xmask, other=0.0) tmp52 = tmp51 + tmp49 tmp53 = tmp43 & tmp29 tmp54 = tl.load(in_ptr0 + (-3 + r3 + 16 * x0), tmp53 & xmask, other=0.0) tmp55 = tmp54 + tmp52 tmp56 = tmp43 & tmp36 tmp57 = tl.load(in_ptr0 + (-2 + r3 + 16 * x0), tmp56 & xmask, other=0.0) tmp58 = tmp57 + tmp55 tmp59 = r2 tmp60 = tmp59 >= tmp1 tmp61 = tmp59 < tmp3 tmp62 = tmp60 & tmp61 tmp63 = tmp62 & tmp9 tmp64 = tl.load(in_ptr0 + (-2 + r3 + 16 * x0), tmp63 & xmask, other=0.0) tmp65 = tmp64 + tmp58 tmp66 = tmp62 & tmp15 tmp67 = tl.load(in_ptr0 + (-1 + r3 + 16 * x0), tmp66 & xmask, other=0.0) tmp68 = tmp67 + tmp65 tmp69 = tmp62 & tmp22 tmp70 = tl.load(in_ptr0 + (r3 + 16 * x0), tmp69 & xmask, other=0.0) tmp71 = tmp70 + tmp68 tmp72 = tmp62 & tmp29 tmp73 = tl.load(in_ptr0 + (1 + r3 + 16 * x0), tmp72 & xmask, other=0.0) tmp74 = tmp73 + tmp71 tmp75 = tmp62 & tmp36 tmp76 = tl.load(in_ptr0 + (2 + r3 + 16 * x0), tmp75 & xmask, other=0.0) tmp77 = tmp76 + tmp74 tmp78 = 1 + r2 tmp79 = tmp78 >= tmp1 tmp80 = tmp78 < tmp3 tmp81 = tmp79 & tmp80 tmp82 = tmp81 & tmp9 tmp83 = tl.load(in_ptr0 + (2 + r3 + 16 * x0), tmp82 & xmask, other=0.0) tmp84 = tmp83 + tmp77 tmp85 = tmp81 & tmp15 tmp86 = tl.load(in_ptr0 + (3 + r3 + 16 * x0), tmp85 & xmask, other=0.0) tmp87 = tmp86 + tmp84 tmp88 = tmp81 & tmp22 tmp89 = tl.load(in_ptr0 + (4 + r3 + 16 * x0), tmp88 & xmask, other=0.0) tmp90 = tmp89 + tmp87 tmp91 = tmp81 & tmp29 tmp92 = tl.load(in_ptr0 + (5 + r3 + 16 * x0), tmp91 & xmask, other=0.0) tmp93 = tmp92 + tmp90 tmp94 = tmp81 & tmp36 tmp95 = tl.load(in_ptr0 + (6 + r3 + 16 * x0), tmp94 & xmask, other=0.0) tmp96 = tmp95 + tmp93 tmp97 = 2 + r2 tmp98 = tmp97 >= tmp1 tmp99 = tmp97 < tmp3 tmp100 = tmp98 & tmp99 tmp101 = tmp100 & tmp9 tmp102 = tl.load(in_ptr0 + (6 + r3 + 16 * x0), tmp101 & xmask, other=0.0) tmp103 = tmp102 + tmp96 tmp104 = tmp100 & tmp15 tmp105 = tl.load(in_ptr0 + (7 + r3 + 16 * x0), tmp104 & xmask, other=0.0) tmp106 = tmp105 + tmp103 tmp107 = tmp100 & tmp22 tmp108 = tl.load(in_ptr0 + (8 + r3 + 16 * x0), tmp107 & xmask, other=0.0) tmp109 = tmp108 + tmp106 tmp110 = tmp100 & tmp29 tmp111 = tl.load(in_ptr0 + (9 + r3 + 16 * x0), tmp110 & xmask, other=0.0) tmp112 = tmp111 + tmp109 tmp113 = tmp100 & tmp36 tmp114 = tl.load(in_ptr0 + (10 + r3 + 16 * x0), tmp113 & xmask, other=0.0) tmp115 = tmp114 + tmp112 tmp116 = 4 + -2 * r1 + -2 * r2 + 2 * (6 * (6 <= 3 + r1) + (3 + r1) * (3 + r1 < 6)) + 2 * (6 * (6 <= 3 + r2) + (3 + r2) * (3 + r2 < 6) ) + r1 * r2 + (6 * (6 <= 3 + r1) + (3 + r1) * (3 + r1 < 6)) * (6 * (6 <= 3 + r2) + (3 + r2) * (3 + r2 < 6)) + -1 * r1 * (6 * (6 <= 3 + r2) + (3 + r2) * (3 + r2 < 6)) + -1 * r2 * (6 * (6 <= 3 + r1) + (3 + r1) * (3 + r1 < 6)) tmp117 = tmp115 / tmp116 tmp119 = tmp118 - tmp117 tmp120 = 0.0 tmp121 = tmp119 != tmp120 tmp122 = 1.0 tmp123 = tl.where(tmp121, tmp122, tmp119) tmp125 = tmp124 - tmp118 tmp126 = tl_math.abs(tmp125) tmp127 = tmp123 * tmp126 tmp128 = tl.broadcast_to(tmp127, [XBLOCK, RBLOCK]) tmp130 = tl.where(xmask, tmp128, 0) tmp131 = tl.sum(tmp130, 1)[:, None] tmp132 = tmp118 * tmp124 tmp133 = tmp118 - tmp132 tmp134 = tl.broadcast_to(tmp133, [XBLOCK, RBLOCK]) tmp136 = tl.where(xmask, tmp134, 0) tmp137 = tl.sum(tmp136, 1)[:, None] tmp138 = tmp122 - tmp118 tmp139 = tmp138 * tmp124 tmp140 = tl.broadcast_to(tmp139, [XBLOCK, RBLOCK]) tmp142 = tl.where(xmask, tmp140, 0) tmp143 = tl.sum(tmp142, 1)[:, None] tmp144 = tl.broadcast_to(tmp118, [XBLOCK, RBLOCK]) tmp146 = tl.where(xmask, tmp144, 0) tmp147 = tl.sum(tmp146, 1)[:, None] tmp148 = tl.broadcast_to(tmp138, [XBLOCK, RBLOCK]) tmp150 = tl.where(xmask, tmp148, 0) tmp151 = tl.sum(tmp150, 1)[:, None] tmp152 = tl.broadcast_to(tmp123, [XBLOCK, RBLOCK]) tmp154 = tl.where(xmask, tmp152, 0) tmp155 = tl.sum(tmp154, 1)[:, None] tl.store(out_ptr0 + x0, tmp131, xmask) tl.store(out_ptr1 + x0, tmp137, xmask) tl.store(out_ptr2 + x0, tmp143, xmask) tl.store(out_ptr3 + x0, tmp147, xmask) tl.store(out_ptr4 + x0, tmp151, xmask) tl.store(out_ptr5 + x0, tmp155, xmask) @triton.jit def triton_per_fused_add_div_mean_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, 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) tmp5 = tl.load(in_ptr2 + r0, None) tmp6 = tl.load(in_ptr3 + r0, None) tmp9 = tl.load(in_ptr4 + r0, None) tmp10 = tl.load(in_ptr5 + r0, None) tmp2 = 1e-06 tmp3 = tmp1 + tmp2 tmp4 = tmp0 / tmp3 tmp7 = tmp6 + tmp2 tmp8 = tmp5 / tmp7 tmp11 = tmp10 + tmp2 tmp12 = tmp9 / tmp11 tmp13 = tmp8 + tmp12 tmp14 = tmp4 + tmp13 tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK]) tmp17 = tl.sum(tmp15, 1)[:, None] tmp18 = 16.0 tmp19 = tmp17 / tmp18 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp19, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf2 = 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) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_per_fused_abs_avg_pool2d_index_put_lift_fresh_mul_rsub_sub_sum_0[ grid(16)](arg0_1, arg1_1, buf2, buf4, buf6, buf5, buf7, buf3, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) del arg0_1 del arg1_1 buf8 = empty_strided_cuda((), (), torch.float32) buf9 = buf8 del buf8 triton_per_fused_add_div_mean_1[grid(1)](buf9, buf2, buf3, buf4, buf5, buf6, buf7, 1, 16, XBLOCK=1, num_warps=2, num_stages=1) del buf2 del buf3 del buf4 del buf5 del buf6 del buf7 return buf9, class HELNew(nn.Module): def __init__(self): super(HELNew, self).__init__() None self.eps = 1e-06 def edge_loss(self, pred, target): edge = target - F.avg_pool2d(target, kernel_size=5, stride=1, padding=2 ) edge[edge != 0] = 1 numerator = (edge * (pred - target).abs_()).sum([2, 3]) denominator = edge.sum([2, 3]) + self.eps return numerator / denominator def region_loss(self, pred, target): numerator_fore = (target - target * pred).sum([2, 3]) denominator_fore = target.sum([2, 3]) + self.eps numerator_back = ((1 - target) * pred).sum([2, 3]) denominator_back = (1 - target).sum([2, 3]) + self.eps return (numerator_fore / denominator_fore + numerator_back / denominator_back) def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
lartpang/HDFNet
HEL
false
15,881
[ "MIT" ]
67
e2e4136a336f171481d2a6a954e901568932b8d3
https://github.com/lartpang/HDFNet/tree/e2e4136a336f171481d2a6a954e901568932b8d3
FactorizedSynthesizerRandom
import torch import torch.nn as nn class FactorizedSynthesizerRandom(nn.Module): def __init__(self, in_dims): super(FactorizedSynthesizerRandom, self).__init__() self.k = 8 self.query_fc = nn.Linear(in_dims, self.k) self.key_fc = nn.Linear(in_dims, self.k) self.value_fc = nn.Linear(in_dims, in_dims) self.softmax = nn.Softmax(dim=-1) def forward(self, x): query = self.query_fc(x) key = self.key_fc(x).permute(0, 2, 1) energy = torch.bmm(query, key) attention = self.softmax(energy) value = self.value_fc(x) out = torch.bmm(attention, value) return out, attention def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'in_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 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): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = 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), (16, 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,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 8), (8, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (16, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 8), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((16, 8), (8, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(primals_3, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 8), (1, 4), 0 ), alpha=1, beta=1, out=buf1) del primals_4 del primals_5 buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf0, (4, 4, 8), (32, 8, 1), 0), reinterpret_tensor(buf1, (4, 8, 4), (32, 1, 8), 0), out=buf2) buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused__softmax_0[grid(64)](buf2, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) buf4 = buf2 del buf2 triton_poi_fused__softmax_1[grid(64)](buf3, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) buf5 = reinterpret_tensor(buf3, (16, 4), (4, 1), 0) del buf3 extern_kernels.addmm(primals_7, reinterpret_tensor(primals_3, (16, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf5) del primals_6 del primals_7 buf6 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf4, reinterpret_tensor(buf5, (4, 4, 4), (16, 4, 1), 0), out=buf6) return buf6, buf4, reinterpret_tensor(primals_3, (16, 4), (4, 1), 0 ), buf4, reinterpret_tensor(buf5, (4, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf0, (4, 8, 4), (32, 1, 8), 0 ), reinterpret_tensor(buf1, (4, 4, 8), (32, 8, 1), 0) class FactorizedSynthesizerRandomNew(nn.Module): def __init__(self, in_dims): super(FactorizedSynthesizerRandomNew, self).__init__() self.k = 8 self.query_fc = nn.Linear(in_dims, self.k) self.key_fc = nn.Linear(in_dims, self.k) self.value_fc = nn.Linear(in_dims, in_dims) self.softmax = nn.Softmax(dim=-1) def forward(self, input_0): primals_1 = self.query_fc.weight primals_2 = self.query_fc.bias primals_4 = self.key_fc.weight primals_5 = self.key_fc.bias primals_6 = self.value_fc.weight primals_7 = self.value_fc.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0], output[1]
leaderj1001/Synthesizer-Rethinking-Self-Attention-Transformer-Models
FactorizedSynthesizerRandom
false
15,882
[ "MIT" ]
58
3ee5829438a8f9c063ae485e77c9ce7649d24139
https://github.com/leaderj1001/Synthesizer-Rethinking-Self-Attention-Transformer-Models/tree/3ee5829438a8f9c063ae485e77c9ce7649d24139
FeatureAttentionLayer
import torch import torch.nn as nn class FeatureAttentionLayer(nn.Module): """Single Graph Feature/Spatial Attention Layer :param n_features: Number of input features/nodes :param window_size: length of the input sequence :param dropout: percentage of nodes to dropout :param alpha: negative slope used in the leaky rely activation function :param embed_dim: embedding dimension (output dimension of linear transformation) :param use_gatv2: whether to use the modified attention mechanism of GATv2 instead of standard GAT :param use_bias: whether to include a bias term in the attention layer """ def __init__(self, n_features, window_size, dropout, alpha, embed_dim= None, use_gatv2=True, use_bias=True): super(FeatureAttentionLayer, self).__init__() self.n_features = n_features self.window_size = window_size self.dropout = dropout self.embed_dim = embed_dim if embed_dim is not None else window_size self.use_gatv2 = use_gatv2 self.num_nodes = n_features self.use_bias = use_bias if self.use_gatv2: self.embed_dim *= 2 lin_input_dim = 2 * window_size a_input_dim = self.embed_dim else: lin_input_dim = window_size a_input_dim = 2 * self.embed_dim self.lin = nn.Linear(lin_input_dim, self.embed_dim) self.a = nn.Parameter(torch.empty((a_input_dim, 1))) nn.init.xavier_uniform_(self.a.data, gain=1.414) if self.use_bias: self.bias = nn.Parameter(torch.empty(n_features, n_features)) self.leakyrelu = nn.LeakyReLU(alpha) self.sigmoid = nn.Sigmoid() def forward(self, x): x = x.permute(0, 2, 1) if self.use_gatv2: a_input = self._make_attention_input(x) a_input = self.leakyrelu(self.lin(a_input)) e = torch.matmul(a_input, self.a).squeeze(3) else: Wx = self.lin(x) a_input = self._make_attention_input(Wx) e = self.leakyrelu(torch.matmul(a_input, self.a)).squeeze(3) if self.use_bias: e += self.bias attention = torch.softmax(e, dim=2) attention = torch.dropout(attention, self.dropout, train=self.training) h = self.sigmoid(torch.matmul(attention, x)) return h.permute(0, 2, 1) def _make_attention_input(self, v): """Preparing the feature attention mechanism. Creating matrix with all possible combinations of concatenations of node. Each node consists of all values of that node within the window v1 || v1, ... v1 || vK, v2 || v1, ... v2 || vK, ... ... vK || v1, ... vK || vK, """ K = self.num_nodes blocks_repeating = v.repeat_interleave(K, dim=1) blocks_alternating = v.repeat(1, K, 1) combined = torch.cat((blocks_repeating, blocks_alternating), dim=2) if self.use_gatv2: return combined.view(v.size(0), K, K, 2 * self.window_size) else: return combined.view(v.size(0), K, K, 2 * self.embed_dim) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'n_features': 4, 'window_size': 4, 'dropout': 0.5, 'alpha': 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_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 8 x1 = xindex // 8 % 16 x2 = xindex // 128 x3 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (4 * x0 + 16 * x2 + x1 // 4), tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 8, tl.int64) tmp9 = tl.load(in_ptr0 + (4 * (-4 + x0) + 16 * x2 + x1 % 4), tmp6 & xmask, eviction_policy='evict_last', other=0.0) tmp10 = tl.where(tmp4, tmp5, tmp9) tl.store(out_ptr0 + x3, tmp10, xmask) @triton.jit def triton_poi_fused_leaky_relu_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 8 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 = 4.0 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, 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 x2 = xindex x0 = xindex % 4 x1 = xindex // 4 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 * x0 + 16 * x1 + 16 * ((1 + 4 * x0) // 16)), 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 + 16 * x1 + 16 * ((1 + 2 * x0) // 8)), 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 + 16 * x1 + 16 * ((3 + 4 * x0) // 16)), 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 tl.store(out_ptr0 + x2, tmp14, xmask) tl.store(out_ptr1 + x2, tmp25, xmask) @triton.jit def triton_poi_fused__softmax_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 x4 = xindex x3 = xindex % 16 x5 = xindex // 4 tmp0 = tl.load(in_ptr0 + x4, xmask) tmp1 = tl.load(in_ptr1 + x3, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x5, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr3 + x5, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 - tmp3 tmp5 = tl_math.exp(tmp4) tmp7 = tmp5 / tmp6 tl.store(out_ptr0 + x4, tmp7, xmask) @triton.jit def triton_poi_fused_sigmoid_sigmoid_backward_4(in_out_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_out_ptr0 + x0, xmask) tmp1 = tl.sigmoid(tmp0) tmp2 = 1.0 tmp3 = tmp2 - tmp1 tmp4 = tmp1 * tmp3 tl.store(in_out_ptr0 + x0, tmp1, xmask) tl.store(out_ptr0 + x0, tmp4, 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, (8, 8), (8, 1)) assert_size_stride(primals_3, (8,), (1,)) assert_size_stride(primals_4, (8, 1), (1, 1)) assert_size_stride(primals_5, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 16, 8), (128, 8, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(512)](primals_1, buf0, 512, XBLOCK=128, num_warps=4, num_stages=1) buf1 = empty_strided_cuda((64, 8), (8, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (64, 8), (8, 1), 0), reinterpret_tensor(primals_2, (8, 8), (1, 8), 0), out=buf1) del primals_2 buf2 = empty_strided_cuda((4, 4, 4, 8), (128, 32, 8, 1), torch.bool) buf3 = empty_strided_cuda((4, 4, 4, 8), (128, 32, 8, 1), torch.float32) triton_poi_fused_leaky_relu_1[grid(512)](buf1, primals_3, buf2, buf3, 512, XBLOCK=256, num_warps=4, num_stages=1) del buf1 del primals_3 buf4 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf3, (64, 8), (8, 1), 0), primals_4, out=buf4) buf5 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf6 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) triton_poi_fused__softmax_2[grid(16)](buf4, primals_5, buf5, buf6, 16, XBLOCK=16, num_warps=1, num_stages=1) buf7 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_3[grid(64)](buf4, primals_5, buf5, buf6, buf7, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf5 del buf6 del primals_5 buf8 = reinterpret_tensor(buf4, (4, 4, 4), (16, 4, 1), 0) del buf4 extern_kernels.bmm(buf7, reinterpret_tensor(primals_1, (4, 4, 4), ( 16, 1, 4), 0), out=buf8) buf9 = buf8 del buf8 buf10 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_sigmoid_sigmoid_backward_4[grid(64)](buf9, buf10, 64, XBLOCK=64, num_warps=1, num_stages=1) return reinterpret_tensor(buf9, (4, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf0, (64, 8), (8, 1), 0 ), buf2, buf7, buf10, primals_1, reinterpret_tensor(buf3, (8, 64), (1, 8), 0), reinterpret_tensor(primals_4, (1, 8), (1, 1), 0) class FeatureAttentionLayerNew(nn.Module): """Single Graph Feature/Spatial Attention Layer :param n_features: Number of input features/nodes :param window_size: length of the input sequence :param dropout: percentage of nodes to dropout :param alpha: negative slope used in the leaky rely activation function :param embed_dim: embedding dimension (output dimension of linear transformation) :param use_gatv2: whether to use the modified attention mechanism of GATv2 instead of standard GAT :param use_bias: whether to include a bias term in the attention layer """ def __init__(self, n_features, window_size, dropout, alpha, embed_dim= None, use_gatv2=True, use_bias=True): super(FeatureAttentionLayerNew, self).__init__() self.n_features = n_features self.window_size = window_size self.dropout = dropout self.embed_dim = embed_dim if embed_dim is not None else window_size self.use_gatv2 = use_gatv2 self.num_nodes = n_features self.use_bias = use_bias if self.use_gatv2: self.embed_dim *= 2 lin_input_dim = 2 * window_size a_input_dim = self.embed_dim else: lin_input_dim = window_size a_input_dim = 2 * self.embed_dim self.lin = nn.Linear(lin_input_dim, self.embed_dim) self.a = nn.Parameter(torch.empty((a_input_dim, 1))) nn.init.xavier_uniform_(self.a.data, gain=1.414) if self.use_bias: self.bias = nn.Parameter(torch.empty(n_features, n_features)) self.leakyrelu = nn.LeakyReLU(alpha) self.sigmoid = nn.Sigmoid() def _make_attention_input(self, v): """Preparing the feature attention mechanism. Creating matrix with all possible combinations of concatenations of node. Each node consists of all values of that node within the window v1 || v1, ... v1 || vK, v2 || v1, ... v2 || vK, ... ... vK || v1, ... vK || vK, """ K = self.num_nodes blocks_repeating = v.repeat_interleave(K, dim=1) blocks_alternating = v.repeat(1, K, 1) combined = torch.cat((blocks_repeating, blocks_alternating), dim=2) if self.use_gatv2: return combined.view(v.size(0), K, K, 2 * self.window_size) else: return combined.view(v.size(0), K, K, 2 * self.embed_dim) def forward(self, input_0): primals_4 = self.a primals_5 = self.bias primals_2 = self.lin.weight primals_3 = self.lin.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
lawson-source/mtad-gat-pytorch
FeatureAttentionLayer
false
15,883
[ "MIT" ]
93
9e671ea99dedd82ac55f53e53af1d1b56c13ebff
https://github.com/lawson-source/mtad-gat-pytorch/tree/9e671ea99dedd82ac55f53e53af1d1b56c13ebff
_Residual_Block
import torch from torch import nn class _Residual_Block(nn.Module): def __init__(self, num_chans=64): super(_Residual_Block, self).__init__() bias = True self.conv1 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride= 1, padding=1, bias=bias) self.relu2 = nn.PReLU() self.conv3 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride= 1, padding=1, bias=bias) self.relu4 = nn.PReLU() self.conv5 = nn.Conv2d(num_chans, num_chans * 2, kernel_size=3, stride=2, padding=1, bias=bias) self.relu6 = nn.PReLU() self.conv7 = nn.Conv2d(num_chans * 2, num_chans * 2, kernel_size=3, stride=1, padding=1, bias=bias) self.relu8 = nn.PReLU() self.conv9 = nn.Conv2d(num_chans * 2, num_chans * 4, kernel_size=3, stride=2, padding=1, bias=bias) self.relu10 = nn.PReLU() self.conv11 = nn.Conv2d(num_chans * 4, num_chans * 4, kernel_size=3, stride=1, padding=1, bias=bias) self.relu12 = nn.PReLU() self.conv13 = nn.Conv2d(num_chans * 4, num_chans * 8, kernel_size=1, stride=1, padding=0, bias=bias) self.up14 = nn.PixelShuffle(2) self.conv15 = nn.Conv2d(num_chans * 4, num_chans * 2, kernel_size=1, stride=1, padding=0, bias=bias) self.conv16 = nn.Conv2d(num_chans * 2, num_chans * 2, kernel_size=3, stride=1, padding=1, bias=bias) self.relu17 = nn.PReLU() self.conv18 = nn.Conv2d(num_chans * 2, num_chans * 4, kernel_size=1, stride=1, padding=0, bias=bias) self.up19 = nn.PixelShuffle(2) self.conv20 = nn.Conv2d(num_chans * 2, num_chans, kernel_size=1, stride=1, padding=0, bias=bias) self.conv21 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride =1, padding=1, bias=bias) self.relu22 = nn.PReLU() self.conv23 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride =1, padding=1, bias=bias) self.relu24 = nn.PReLU() self.conv25 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride =1, padding=1, bias=bias) def forward(self, x): res1 = x out = self.relu4(self.conv3(self.relu2(self.conv1(x)))) out = torch.add(res1, out) cat1 = out out = self.relu6(self.conv5(out)) res2 = out out = self.relu8(self.conv7(out)) out = torch.add(res2, out) cat2 = out out = self.relu10(self.conv9(out)) res3 = out out = self.relu12(self.conv11(out)) out = torch.add(res3, out) out = self.up14(self.conv13(out)) out = torch.cat([out, cat2], 1) out = self.conv15(out) res4 = out out = self.relu17(self.conv16(out)) out = torch.add(res4, out) out = self.up19(self.conv18(out)) out = torch.cat([out, cat1], 1) out = self.conv20(out) res5 = out out = self.relu24(self.conv23(self.relu22(self.conv21(out)))) out = torch.add(res5, out) out = self.conv25(out) out = torch.add(out, res1) return out def get_inputs(): return [torch.rand([4, 64, 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 import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 256 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, YBLOCK], True, tl.int1) x2 = xindex y3 = yindex y0 = yindex % 64 y1 = yindex // 64 tmp0 = tl.load(in_ptr0 + (x2 + 4096 * y3), ymask, eviction_policy= 'evict_last') tl.store(out_ptr0 + (y0 + 64 * x2 + 262144 * y1), tmp0, ymask) @triton.jit def triton_poi_fused_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 9 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 64 y1 = yindex // 64 tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last' ) tl.store(out_ptr0 + (y0 + 64 * x2 + 576 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_2(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 9 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 64 y1 = yindex // 64 tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last' ) tl.store(out_ptr0 + (y0 + 64 * x2 + 576 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 9 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 128 y1 = yindex // 128 tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last' ) tl.store(out_ptr0 + (y0 + 128 * x2 + 1152 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_4(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 9 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 128 y1 = yindex // 128 tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last' ) tl.store(out_ptr0 + (y0 + 128 * x2 + 1152 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_5(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 9 yoffset = (tl.program_id(1) + tl.program_id(2) * tl.num_programs(1) ) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 256 y1 = yindex // 256 tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last' ) tl.store(out_ptr0 + (y0 + 256 * x2 + 2304 * y1), tmp0, xmask) @triton.jit def triton_poi_fused__prelu_kernel_convolution_6(in_out_ptr0, 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) x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + 0) tmp6 = tl.broadcast_to(tmp5, [XBLOCK]) tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp7 = tmp6 * tmp2 tmp8 = tl.where(tmp4, tmp2, tmp7) tl.store(in_out_ptr0 + x2, tmp2, None) tl.store(out_ptr0 + x2, tmp8, None) @triton.jit def triton_poi_fused__prelu_kernel_add_convolution_7(in_out_ptr0, 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) x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x2, None) tmp6 = tl.load(in_ptr2 + 0) tmp7 = tl.broadcast_to(tmp6, [XBLOCK]) tmp2 = tmp0 + tmp1 tmp4 = 0.0 tmp5 = tmp2 > tmp4 tmp8 = tmp7 * tmp2 tmp9 = tl.where(tmp5, tmp2, tmp8) tmp10 = tmp3 + tmp9 tl.store(in_out_ptr0 + x2, tmp2, None) tl.store(out_ptr0 + x2, tmp10, None) @triton.jit def triton_poi_fused__prelu_kernel_convolution_8(in_out_ptr0, 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) x2 = xindex x0 = xindex % 128 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + 0) tmp6 = tl.broadcast_to(tmp5, [XBLOCK]) tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp7 = tmp6 * tmp2 tmp8 = tl.where(tmp4, tmp2, tmp7) tl.store(in_out_ptr0 + x2, tmp2, None) tl.store(out_ptr0 + x2, tmp8, None) @triton.jit def triton_poi_fused__prelu_kernel_add_convolution_9(in_out_ptr0, 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) x2 = xindex x0 = xindex % 128 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x2, None) tmp6 = tl.load(in_ptr2 + 0) tmp7 = tl.broadcast_to(tmp6, [XBLOCK]) tmp2 = tmp0 + tmp1 tmp4 = 0.0 tmp5 = tmp2 > tmp4 tmp8 = tmp7 * tmp2 tmp9 = tl.where(tmp5, tmp2, tmp8) tmp10 = tmp3 + tmp9 tl.store(in_out_ptr0 + x2, tmp2, None) tl.store(out_ptr0 + x2, tmp10, None) @triton.jit def triton_poi_fused__prelu_kernel_convolution_10(in_out_ptr0, 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) x2 = xindex x0 = xindex % 256 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + 0) tmp6 = tl.broadcast_to(tmp5, [XBLOCK]) tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp7 = tmp6 * tmp2 tmp8 = tl.where(tmp4, tmp2, tmp7) tl.store(in_out_ptr0 + x2, tmp2, None) tl.store(out_ptr0 + x2, tmp8, None) @triton.jit def triton_poi_fused__prelu_kernel_add_convolution_11(in_out_ptr0, 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) x2 = xindex x0 = xindex % 256 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x2, None) tmp6 = tl.load(in_ptr2 + 0) tmp7 = tl.broadcast_to(tmp6, [XBLOCK]) tmp2 = tmp0 + tmp1 tmp4 = 0.0 tmp5 = tmp2 > tmp4 tmp8 = tmp7 * tmp2 tmp9 = tl.where(tmp5, tmp2, tmp8) tmp10 = tmp3 + tmp9 tl.store(in_out_ptr0 + x2, tmp2, None) tl.store(out_ptr0 + x2, tmp10, None) @triton.jit def triton_poi_fused_cat_12(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) x0 = xindex % 256 x1 = xindex // 256 % 32 x2 = xindex // 8192 % 32 x3 = xindex // 262144 x4 = xindex // 256 x5 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 128, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (2 * (x2 % 2) + 4 * x0 + 512 * (x1 // 2) + 8192 * (x2 // 2) + 131072 * x3 + x1 % 2), tmp4, eviction_policy= 'evict_last', other=0.0) tmp6 = tl.load(in_ptr1 + (2 * (x2 % 2) + 4 * x0 + x1 % 2), tmp4, eviction_policy='evict_last', other=0.0) tmp7 = tmp5 + tmp6 tmp8 = tl.full(tmp7.shape, 0.0, tmp7.dtype) tmp9 = tl.where(tmp4, tmp7, tmp8) tmp10 = tmp0 >= tmp3 tl.full([1], 256, tl.int64) tmp13 = tl.load(in_ptr2 + (128 * x4 + (-128 + x0)), tmp10, eviction_policy='evict_last', other=0.0) tmp14 = tl.where(tmp4, tmp9, tmp13) tl.store(out_ptr0 + x5, tmp14, None) @triton.jit def triton_poi_fused_convolution_13(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 128 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x2, tmp2, None) @triton.jit def triton_poi_fused_cat_14(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) x0 = xindex % 128 x1 = xindex // 128 % 64 x2 = xindex // 8192 % 64 x3 = xindex // 524288 x4 = xindex // 128 x5 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 64, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (2 * (x2 % 2) + 4 * x0 + 256 * (x1 // 2) + 8192 * (x2 // 2) + 262144 * x3 + x1 % 2), tmp4, eviction_policy= 'evict_last', other=0.0) tmp6 = tl.load(in_ptr1 + (2 * (x2 % 2) + 4 * x0 + x1 % 2), tmp4, eviction_policy='evict_last', other=0.0) tmp7 = tmp5 + tmp6 tmp8 = tl.full(tmp7.shape, 0.0, tmp7.dtype) tmp9 = tl.where(tmp4, tmp7, tmp8) tmp10 = tmp0 >= tmp3 tl.full([1], 128, tl.int64) tmp13 = tl.load(in_ptr2 + (64 * x4 + (-64 + x0)), tmp10, eviction_policy='evict_last', other=0.0) tmp14 = tl.where(tmp4, tmp9, tmp13) tl.store(out_ptr0 + x5, tmp14, None) @triton.jit def triton_poi_fused_convolution_15(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) 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 tl.store(in_out_ptr0 + x2, tmp2, None) @triton.jit def triton_poi_fused_add_convolution_16(in_ptr0, in_ptr1, in_ptr2, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): xnumel = 64 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 4096 y1 = yindex // 4096 tmp0 = tl.load(in_ptr0 + (x2 + 64 * y3), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + (x2 + 64 * y3), xmask, eviction_policy= 'evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tl.store(out_ptr0 + (y0 + 4096 * x2 + 262144 * y1), 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, 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 ) = args args.clear() assert_size_stride(primals_1, (4, 64, 64, 64), (262144, 4096, 64, 1)) assert_size_stride(primals_2, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_3, (64,), (1,)) assert_size_stride(primals_4, (1,), (1,)) assert_size_stride(primals_5, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_6, (64,), (1,)) assert_size_stride(primals_7, (1,), (1,)) assert_size_stride(primals_8, (128, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_9, (128,), (1,)) assert_size_stride(primals_10, (1,), (1,)) assert_size_stride(primals_11, (128, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_12, (128,), (1,)) assert_size_stride(primals_13, (1,), (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, (1,), (1,)) assert_size_stride(primals_17, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_18, (256,), (1,)) assert_size_stride(primals_19, (1,), (1,)) assert_size_stride(primals_20, (512, 256, 1, 1), (256, 1, 1, 1)) assert_size_stride(primals_21, (512,), (1,)) assert_size_stride(primals_22, (128, 256, 1, 1), (256, 1, 1, 1)) assert_size_stride(primals_23, (128,), (1,)) assert_size_stride(primals_24, (128, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_25, (128,), (1,)) assert_size_stride(primals_26, (1,), (1,)) assert_size_stride(primals_27, (256, 128, 1, 1), (128, 1, 1, 1)) assert_size_stride(primals_28, (256,), (1,)) assert_size_stride(primals_29, (64, 128, 1, 1), (128, 1, 1, 1)) assert_size_stride(primals_30, (64,), (1,)) assert_size_stride(primals_31, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_32, (64,), (1,)) assert_size_stride(primals_33, (1,), (1,)) assert_size_stride(primals_34, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_35, (64,), (1,)) assert_size_stride(primals_36, (1,), (1,)) assert_size_stride(primals_37, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_38, (64,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 64, 64, 64), (262144, 1, 4096, 64), torch.float32) get_raw_stream(0) triton_poi_fused_0[grid(256, 4096)](primals_1, buf0, 256, 4096, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1) del primals_1 buf1 = empty_strided_cuda((64, 64, 3, 3), (576, 1, 192, 64), torch. float32) triton_poi_fused_1[grid(4096, 9)](primals_2, buf1, 4096, 9, XBLOCK= 16, YBLOCK=64, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 64, 3, 3), (576, 1, 192, 64), torch. float32) triton_poi_fused_1[grid(4096, 9)](primals_5, buf2, 4096, 9, XBLOCK= 16, YBLOCK=64, num_warps=4, num_stages=1) del primals_5 buf3 = empty_strided_cuda((128, 64, 3, 3), (576, 1, 192, 64), torch .float32) triton_poi_fused_2[grid(8192, 9)](primals_8, buf3, 8192, 9, XBLOCK= 16, YBLOCK=64, num_warps=4, num_stages=1) del primals_8 buf4 = empty_strided_cuda((128, 128, 3, 3), (1152, 1, 384, 128), torch.float32) triton_poi_fused_3[grid(16384, 9)](primals_11, buf4, 16384, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_11 buf5 = empty_strided_cuda((256, 128, 3, 3), (1152, 1, 384, 128), torch.float32) triton_poi_fused_4[grid(32768, 9)](primals_14, buf5, 32768, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_14 buf6 = empty_strided_cuda((256, 256, 3, 3), (2304, 1, 768, 256), torch.float32) triton_poi_fused_5[grid(65536, 9)](primals_17, buf6, 65536, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_17 buf7 = empty_strided_cuda((128, 128, 3, 3), (1152, 1, 384, 128), torch.float32) triton_poi_fused_3[grid(16384, 9)](primals_24, buf7, 16384, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_24 buf8 = empty_strided_cuda((64, 64, 3, 3), (576, 1, 192, 64), torch. float32) triton_poi_fused_1[grid(4096, 9)](primals_31, buf8, 4096, 9, XBLOCK =16, YBLOCK=64, num_warps=4, num_stages=1) del primals_31 buf9 = empty_strided_cuda((64, 64, 3, 3), (576, 1, 192, 64), torch. float32) triton_poi_fused_1[grid(4096, 9)](primals_34, buf9, 4096, 9, XBLOCK =16, YBLOCK=64, num_warps=4, num_stages=1) del primals_34 buf10 = empty_strided_cuda((64, 64, 3, 3), (576, 1, 192, 64), torch .float32) triton_poi_fused_1[grid(4096, 9)](primals_37, buf10, 4096, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_37 buf11 = extern_kernels.convolution(buf0, buf1, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf11, (4, 64, 64, 64), (262144, 1, 4096, 64)) buf12 = buf11 del buf11 buf13 = empty_strided_cuda((4, 64, 64, 64), (262144, 1, 4096, 64), torch.float32) triton_poi_fused__prelu_kernel_convolution_6[grid(1048576)](buf12, primals_3, primals_4, buf13, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del primals_3 buf14 = extern_kernels.convolution(buf13, buf2, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf14, (4, 64, 64, 64), (262144, 1, 4096, 64)) buf15 = buf14 del buf14 buf16 = empty_strided_cuda((4, 64, 64, 64), (262144, 1, 4096, 64), torch.float32) triton_poi_fused__prelu_kernel_add_convolution_7[grid(1048576)](buf15, primals_6, buf0, primals_7, buf16, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del primals_6 buf17 = extern_kernels.convolution(buf16, buf3, stride=(2, 2), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf17, (4, 128, 32, 32), (131072, 1, 4096, 128)) buf18 = buf17 del buf17 buf19 = empty_strided_cuda((4, 128, 32, 32), (131072, 1, 4096, 128), torch.float32) triton_poi_fused__prelu_kernel_convolution_8[grid(524288)](buf18, primals_9, primals_10, buf19, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_9 buf20 = extern_kernels.convolution(buf19, buf4, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf20, (4, 128, 32, 32), (131072, 1, 4096, 128)) buf21 = buf20 del buf20 buf22 = empty_strided_cuda((4, 128, 32, 32), (131072, 1, 4096, 128), torch.float32) triton_poi_fused__prelu_kernel_add_convolution_9[grid(524288)](buf21, primals_12, buf19, primals_13, buf22, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_12 buf23 = extern_kernels.convolution(buf22, buf5, stride=(2, 2), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf23, (4, 256, 16, 16), (65536, 1, 4096, 256)) buf24 = buf23 del buf23 buf25 = empty_strided_cuda((4, 256, 16, 16), (65536, 1, 4096, 256), torch.float32) triton_poi_fused__prelu_kernel_convolution_10[grid(262144)](buf24, primals_15, primals_16, buf25, 262144, XBLOCK=512, num_warps=8, num_stages=1) del primals_15 buf26 = extern_kernels.convolution(buf25, buf6, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf26, (4, 256, 16, 16), (65536, 1, 4096, 256)) buf27 = buf26 del buf26 buf28 = empty_strided_cuda((4, 256, 16, 16), (65536, 1, 4096, 256), torch.float32) triton_poi_fused__prelu_kernel_add_convolution_11[grid(262144)](buf27, primals_18, buf25, primals_19, buf28, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del primals_18 buf29 = extern_kernels.convolution(buf28, primals_20, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf29, (4, 512, 16, 16), (131072, 1, 8192, 512)) buf30 = empty_strided_cuda((4, 256, 32, 32), (262144, 1, 8192, 256), torch.float32) triton_poi_fused_cat_12[grid(1048576)](buf29, primals_21, buf22, buf30, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del primals_21 buf31 = extern_kernels.convolution(buf30, primals_22, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf31, (4, 128, 32, 32), (131072, 1, 4096, 128)) buf32 = buf31 del buf31 triton_poi_fused_convolution_13[grid(524288)](buf32, primals_23, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_23 buf33 = extern_kernels.convolution(buf32, buf7, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf33, (4, 128, 32, 32), (131072, 1, 4096, 128)) buf34 = buf33 del buf33 buf35 = reinterpret_tensor(buf29, (4, 128, 32, 32), (131072, 1, 4096, 128), 0) del buf29 triton_poi_fused__prelu_kernel_add_convolution_9[grid(524288)](buf34, primals_25, buf32, primals_26, buf35, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_25 buf36 = extern_kernels.convolution(buf35, primals_27, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf36, (4, 256, 32, 32), (262144, 1, 8192, 256)) buf37 = empty_strided_cuda((4, 128, 64, 64), (524288, 1, 8192, 128), torch.float32) triton_poi_fused_cat_14[grid(2097152)](buf36, primals_28, buf16, buf37, 2097152, XBLOCK=1024, num_warps=4, num_stages=1) del primals_28 buf38 = extern_kernels.convolution(buf37, primals_29, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf38, (4, 64, 64, 64), (262144, 1, 4096, 64)) buf39 = buf38 del buf38 triton_poi_fused_convolution_15[grid(1048576)](buf39, primals_30, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del primals_30 buf40 = extern_kernels.convolution(buf39, buf8, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf40, (4, 64, 64, 64), (262144, 1, 4096, 64)) buf41 = buf40 del buf40 buf42 = reinterpret_tensor(buf36, (4, 64, 64, 64), (262144, 1, 4096, 64), 0) del buf36 triton_poi_fused__prelu_kernel_convolution_6[grid(1048576)](buf41, primals_32, primals_33, buf42, 1048576, XBLOCK=1024, num_warps= 4, num_stages=1) del primals_32 buf43 = extern_kernels.convolution(buf42, buf9, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf43, (4, 64, 64, 64), (262144, 1, 4096, 64)) buf44 = buf43 del buf43 buf45 = empty_strided_cuda((4, 64, 64, 64), (262144, 1, 4096, 64), torch.float32) triton_poi_fused__prelu_kernel_add_convolution_7[grid(1048576)](buf44, primals_35, buf39, primals_36, buf45, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del primals_35 buf46 = extern_kernels.convolution(buf45, buf10, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf46, (4, 64, 64, 64), (262144, 1, 4096, 64)) buf47 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.float32) triton_poi_fused_add_convolution_16[grid(16384, 64)](buf46, primals_38, buf0, buf47, 16384, 64, XBLOCK=64, YBLOCK=64, num_warps=8, num_stages=1) del buf46 del primals_38 return (buf47, buf0, buf1, primals_4, buf2, primals_7, buf3, primals_10, buf4, primals_13, buf5, primals_16, buf6, primals_19, primals_20, primals_22, buf7, primals_26, primals_27, primals_29, buf8, primals_33, buf9, primals_36, buf10, buf12, buf13, buf15, buf16, buf18, buf19, buf21, buf22, buf24, buf25, buf27, buf28, buf30, buf32, buf34, buf35, buf37, buf39, buf41, buf42, buf44, buf45) class _Residual_BlockNew(nn.Module): def __init__(self, num_chans=64): super(_Residual_BlockNew, self).__init__() bias = True self.conv1 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride= 1, padding=1, bias=bias) self.relu2 = nn.PReLU() self.conv3 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride= 1, padding=1, bias=bias) self.relu4 = nn.PReLU() self.conv5 = nn.Conv2d(num_chans, num_chans * 2, kernel_size=3, stride=2, padding=1, bias=bias) self.relu6 = nn.PReLU() self.conv7 = nn.Conv2d(num_chans * 2, num_chans * 2, kernel_size=3, stride=1, padding=1, bias=bias) self.relu8 = nn.PReLU() self.conv9 = nn.Conv2d(num_chans * 2, num_chans * 4, kernel_size=3, stride=2, padding=1, bias=bias) self.relu10 = nn.PReLU() self.conv11 = nn.Conv2d(num_chans * 4, num_chans * 4, kernel_size=3, stride=1, padding=1, bias=bias) self.relu12 = nn.PReLU() self.conv13 = nn.Conv2d(num_chans * 4, num_chans * 8, kernel_size=1, stride=1, padding=0, bias=bias) self.up14 = nn.PixelShuffle(2) self.conv15 = nn.Conv2d(num_chans * 4, num_chans * 2, kernel_size=1, stride=1, padding=0, bias=bias) self.conv16 = nn.Conv2d(num_chans * 2, num_chans * 2, kernel_size=3, stride=1, padding=1, bias=bias) self.relu17 = nn.PReLU() self.conv18 = nn.Conv2d(num_chans * 2, num_chans * 4, kernel_size=1, stride=1, padding=0, bias=bias) self.up19 = nn.PixelShuffle(2) self.conv20 = nn.Conv2d(num_chans * 2, num_chans, kernel_size=1, stride=1, padding=0, bias=bias) self.conv21 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride =1, padding=1, bias=bias) self.relu22 = nn.PReLU() self.conv23 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride =1, padding=1, bias=bias) self.relu24 = nn.PReLU() self.conv25 = nn.Conv2d(num_chans, num_chans, kernel_size=3, stride =1, padding=1, bias=bias) def forward(self, input_0): primals_2 = self.conv1.weight primals_3 = self.conv1.bias primals_4 = self.relu2.weight primals_5 = self.conv3.weight primals_6 = self.conv3.bias primals_7 = self.relu4.weight primals_8 = self.conv5.weight primals_9 = self.conv5.bias primals_10 = self.relu6.weight primals_11 = self.conv7.weight primals_12 = self.conv7.bias primals_13 = self.relu8.weight primals_14 = self.conv9.weight primals_15 = self.conv9.bias primals_16 = self.relu10.weight primals_17 = self.conv11.weight primals_18 = self.conv11.bias primals_19 = self.relu12.weight primals_20 = self.conv13.weight primals_21 = self.conv13.bias primals_22 = self.conv15.weight primals_23 = self.conv15.bias primals_24 = self.conv16.weight primals_25 = self.conv16.bias primals_26 = self.relu17.weight primals_27 = self.conv18.weight primals_28 = self.conv18.bias primals_29 = self.conv20.weight primals_30 = self.conv20.bias primals_31 = self.conv21.weight primals_32 = self.conv21.bias primals_33 = self.relu22.weight primals_34 = self.conv23.weight primals_35 = self.conv23.bias primals_36 = self.relu24.weight primals_37 = self.conv25.weight primals_38 = self.conv25.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16, primals_17, primals_18, primals_19, primals_20, primals_21, primals_22, primals_23, primals_24, primals_25, primals_26, primals_27, primals_28, primals_29, primals_30, primals_31, primals_32, primals_33, primals_34, primals_35, primals_36, primals_37, primals_38]) return output[0]
khammernik/sigmanet
_Residual_Block
false
15,884
[ "MIT" ]
50
6eb8dbd1ee350bb9baee60eb254080f7d660bbc5
https://github.com/khammernik/sigmanet/tree/6eb8dbd1ee350bb9baee60eb254080f7d660bbc5
Transformer
import torch import torch.nn as nn class Transformer(nn.Module): def __init__(self, in_dims): super(Transformer, self).__init__() self.temperature = in_dims ** 0.5 self.query_fc = nn.Linear(in_dims, in_dims) self.key_fc = nn.Linear(in_dims, in_dims) self.value_fc = nn.Linear(in_dims, in_dims) self.softmax = nn.Softmax(dim=-1) def forward(self, x): query = self.query_fc(x) key = self.key_fc(x).permute(0, 2, 1) energy = torch.bmm(query / self.temperature, key) attention = self.softmax(energy) value = self.value_fc(x) out = torch.bmm(attention, value) return out, attention def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'in_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 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_div_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 = 0.5 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 = 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_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = 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) = 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,)) 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.addmm(primals_5, reinterpret_tensor(primals_3, (16, 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 = reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 0) del buf0 get_raw_stream(0) triton_poi_fused_div_0[grid(64)](buf2, primals_2, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_2 buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf2, reinterpret_tensor(buf1, (4, 4, 4), (16, 1, 4), 0), out=buf3) buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_1[grid(64)](buf3, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) buf5 = buf3 del buf3 triton_poi_fused__softmax_2[grid(64)](buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf6 = reinterpret_tensor(buf4, (16, 4), (4, 1), 0) del buf4 extern_kernels.addmm(primals_7, reinterpret_tensor(primals_3, (16, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf6) del primals_6 del primals_7 buf7 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf5, reinterpret_tensor(buf6, (4, 4, 4), (16, 4, 1), 0), out=buf7) return buf7, buf5, reinterpret_tensor(primals_3, (16, 4), (4, 1), 0 ), buf5, reinterpret_tensor(buf6, (4, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf2, (4, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 0) class TransformerNew(nn.Module): def __init__(self, in_dims): super(TransformerNew, self).__init__() self.temperature = in_dims ** 0.5 self.query_fc = nn.Linear(in_dims, in_dims) self.key_fc = nn.Linear(in_dims, in_dims) self.value_fc = nn.Linear(in_dims, in_dims) self.softmax = nn.Softmax(dim=-1) def forward(self, input_0): primals_1 = self.query_fc.weight primals_2 = self.query_fc.bias primals_4 = self.key_fc.weight primals_5 = self.key_fc.bias primals_6 = self.value_fc.weight primals_7 = self.value_fc.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0], output[1]
leaderj1001/Synthesizer-Rethinking-Self-Attention-Transformer-Models
Transformer
false
15,885
[ "MIT" ]
58
3ee5829438a8f9c063ae485e77c9ce7649d24139
https://github.com/leaderj1001/Synthesizer-Rethinking-Self-Attention-Transformer-Models/tree/3ee5829438a8f9c063ae485e77c9ce7649d24139
DiceLoss
import torch import torch.nn as nn class DiceLoss(nn.Module): def __init__(self): super(DiceLoss, self).__init__() def forward(self, input, target): N = target.size(0) smooth = 1 input_flat = input.view(N, -1) target_flat = target.view(N, -1) intersection = input_flat * target_flat loss = 2 * (intersection.sum(1) + smooth) / (input_flat.sum(1) + target_flat.sum(1) + smooth) loss = 1 - loss.sum() / N 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 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_mul_sum_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, 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 = 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] 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_mul_rsub_sum_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 4 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp5 = tl.load(in_ptr1 + r0, None) tmp6 = tl.load(in_ptr2 + r0, None) tmp1 = 1.0 tmp2 = tmp0 + tmp1 tmp3 = 2.0 tmp4 = tmp2 * tmp3 tmp7 = tmp5 + tmp6 tmp8 = tmp7 + tmp1 tmp9 = tmp4 / tmp8 tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK]) tmp12 = tl.sum(tmp10, 1)[:, None] tmp13 = 0.25 tmp14 = tmp12 * tmp13 tmp15 = tmp1 - tmp14 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp15, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4,), (1,), torch.float32) buf1 = empty_strided_cuda((4,), (1,), torch.float32) buf2 = empty_strided_cuda((4,), (1,), torch.float32) get_raw_stream(0) triton_per_fused_mul_sum_0[grid(4)](arg1_1, arg0_1, buf0, buf1, buf2, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 buf3 = empty_strided_cuda((), (), torch.float32) buf4 = buf3 del buf3 triton_per_fused_add_div_mul_rsub_sum_1[grid(1)](buf4, buf0, buf1, buf2, 1, 4, XBLOCK=1, num_warps=2, num_stages=1) del buf0 del buf1 del buf2 return buf4, class DiceLossNew(nn.Module): def __init__(self): super(DiceLossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
lee-zq/VesselSeg-pytorch
DiceLoss
false
15,886
[ "Apache-2.0" ]
83
b4f6571fc1fb1fbdaad60ff9282a54a1f1c455fa
https://github.com/lee-zq/VesselSeg-pytorch/tree/b4f6571fc1fb1fbdaad60ff9282a54a1f1c455fa
CapsuleLoss
import torch import torch.nn.functional as F from torch import nn class CapsuleLoss(nn.Module): def __init__(self): super(CapsuleLoss, self).__init__() self.reconstruction_loss = nn.MSELoss(size_average=False) def forward(self, images, labels, classes, reconstructions): left = F.relu(0.9 - classes, inplace=True) ** 2 right = F.relu(classes - 0.1, inplace=True) ** 2 margin_loss = labels * left + 0.5 * (1.0 - labels) * right margin_loss = margin_loss.sum() reconstruction_loss = self.reconstruction_loss(reconstructions, images) return (margin_loss + 0.0005 * reconstruction_loss) / images.size(0) def get_inputs(): return [torch.rand([4, 4, 4, 4]), 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 import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_div_mse_loss_mul_pow_relu_rsub_sub_sum_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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) tmp21 = tl.load(in_ptr2 + r0, None) tmp22 = tl.load(in_ptr3 + r0, None) tmp2 = 0.9 tmp3 = tmp2 - tmp1 tmp4 = tl.full([1], 0, tl.int32) tmp5 = triton_helpers.maximum(tmp4, tmp3) tmp6 = tmp5 * tmp5 tmp7 = tmp0 * tmp6 tmp8 = 1.0 tmp9 = tmp8 - tmp0 tmp10 = 0.5 tmp11 = tmp9 * tmp10 tmp12 = 0.1 tmp13 = tmp1 - tmp12 tmp14 = triton_helpers.maximum(tmp4, tmp13) tmp15 = tmp14 * tmp14 tmp16 = tmp11 * tmp15 tmp17 = tmp7 + tmp16 tmp18 = tl.broadcast_to(tmp17, [RBLOCK]) tmp20 = triton_helpers.promote_to_tensor(tl.sum(tmp18, 0)) tmp23 = tmp21 - tmp22 tmp24 = tmp23 * tmp23 tmp25 = tl.broadcast_to(tmp24, [RBLOCK]) tmp27 = triton_helpers.promote_to_tensor(tl.sum(tmp25, 0)) tmp28 = 0.0005 tmp29 = tmp27 * tmp28 tmp30 = tmp20 + tmp29 tmp31 = 0.25 tmp32 = tmp30 * tmp31 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp32, None) def call(args): arg0_1, arg1_1, arg2_1, arg3_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)) assert_size_stride(arg3_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_mse_loss_mul_pow_relu_rsub_sub_sum_0[grid(1)]( buf2, arg1_1, arg0_1, arg3_1, arg2_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 del arg2_1 del arg3_1 return buf2, class CapsuleLossNew(nn.Module): def __init__(self): super(CapsuleLossNew, self).__init__() self.reconstruction_loss = nn.MSELoss(size_average=False) def forward(self, input_0, input_1, input_2, input_3): 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]) return output[0]
leftthomas/CapsNet
CapsuleLoss
false
15,887
[ "MIT" ]
163
5de2f45daadbe4377df4ccf8a4d31683d7f397bf
https://github.com/leftthomas/CapsNet/tree/5de2f45daadbe4377df4ccf8a4d31683d7f397bf
CircularPad
import torch class CircularPad(torch.nn.Module): def __init__(self, padding=(1, 1, 0, 0)): super(CircularPad, self).__init__() self.padding = padding def forward(self, input): return torch.nn.functional.pad(input=input, pad=self.padding, mode= 'circular') 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 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_copy_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 384 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 6 x1 = xindex // 6 x2 = xindex tmp0 = x0 tmp1 = tl.full([1], 5, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = -4 + x0 tmp4 = tl.full([1], 1, tl.int64) tmp5 = tmp3 < tmp4 tmp6 = tmp5 & tmp2 tmp7 = tmp0 >= tmp4 tmp8 = tmp0 < tmp1 tmp9 = tmp7 & tmp8 tmp10 = tmp9 & tmp6 tmp11 = tl.load(in_ptr0 + (-1 + x0 + 4 * x1), tmp10 & xmask, other=0.0) tmp12 = float('nan') tmp13 = tl.where(tmp9, tmp11, tmp12) tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype) tmp15 = tl.where(tmp6, tmp13, tmp14) tmp16 = tmp3 >= tmp4 tmp17 = tmp3 < tmp1 tmp18 = tmp16 & tmp17 tmp19 = tmp18 & tmp2 tmp20 = tl.load(in_ptr0 + (-5 + x0 + 4 * x1), tmp19 & xmask, other=0.0) tmp21 = tl.where(tmp18, tmp20, tmp12) tmp22 = tl.where(tmp5, tmp15, tmp21) tmp23 = tl.full(tmp22.shape, 0.0, tmp22.dtype) tmp24 = tl.where(tmp2, tmp22, tmp23) tmp25 = tmp0 < tmp4 tmp26 = 4 + x0 tmp27 = tmp26 >= tmp4 tmp28 = tmp26 < tmp1 tmp29 = tmp27 & tmp28 tmp30 = tmp29 & tmp25 tmp31 = tl.load(in_ptr0 + (3 + x0 + 4 * x1), tmp30 & xmask, other=0.0) tmp32 = tl.where(tmp29, tmp31, tmp12) tmp33 = tl.full(tmp32.shape, 0.0, tmp32.dtype) tmp34 = tl.where(tmp25, tmp32, tmp33) tmp35 = tl.load(in_ptr0 + (-1 + x0 + 4 * x1), tmp9 & xmask, other=0.0) tmp36 = tl.where(tmp9, tmp35, tmp12) tmp37 = tl.where(tmp25, tmp34, tmp36) tmp38 = tl.where(tmp2, tmp24, tmp37) tl.store(out_ptr0 + x2, tmp38, 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, 6), (96, 24, 6, 1), torch.float32) get_raw_stream(0) triton_poi_fused_copy_0[grid(384)](arg0_1, buf1, 384, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf1, class CircularPadNew(torch.nn.Module): def __init__(self, padding=(1, 1, 0, 0)): super(CircularPadNew, self).__init__() self.padding = padding def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
leggedrobotics/DeLORA
CircularPad
false
15,888
[ "BSD-3-Clause" ]
154
909948d63a9517e6dd54bedcf099f6b39ded2cb4
https://github.com/leggedrobotics/DeLORA/tree/909948d63a9517e6dd54bedcf099f6b39ded2cb4
M
import torch import torch.nn.parallel import torch.utils.data import torch.onnx import torch.fx import torch.optim import torch.utils.data.distributed class M(torch.nn.Module): def __init__(self): super().__init__() def forward(self, x, y): y = torch.cat([x, y]) return y 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.parallel import torch.utils.data import torch.onnx import torch.fx import torch.optim import torch.utils.data.distributed assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 64 x0 = xindex % 64 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 + 64 * x1), tmp4 & xmask, other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 8, tl.int64) tmp9 = tl.load(in_ptr1 + (x0 + 64 * (-4 + x1)), tmp6 & xmask, other=0.0) tmp10 = tl.where(tmp4, tmp5, tmp9) tl.store(out_ptr0 + x2, tmp10, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((8, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(512)](arg0_1, arg1_1, buf0, 512, XBLOCK =128, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf0, class MNew(torch.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]
lenaguignard/examples
M
false
15,889
[ "BSD-3-Clause" ]
19,783
973e77b725a6028289a90170f0b237ea2e71d4f2
https://github.com/lenaguignard/examples/tree/973e77b725a6028289a90170f0b237ea2e71d4f2
FirstResBlockDiscriminator
import torch import numpy as np from torch import nn from torch.nn.utils.spectral_norm import spectral_norm as SpectralNorm class FirstResBlockDiscriminator(nn.Module): def __init__(self, in_channels, out_channels, stride=1, spec_norm=False): super(FirstResBlockDiscriminator, self).__init__() self.conv1 = nn.Conv2d(in_channels, out_channels, 3, 1, padding=1) self.conv2 = nn.Conv2d(out_channels, out_channels, 3, 1, padding=1) self.bypass_conv = nn.Conv2d(in_channels, out_channels, 1, 1, padding=0 ) nn.init.xavier_uniform(self.conv1.weight.data, 1.0) nn.init.xavier_uniform(self.conv2.weight.data, 1.0) nn.init.xavier_uniform(self.bypass_conv.weight.data, np.sqrt(2)) if spec_norm: self.spec_norm = SpectralNorm else: self.spec_norm = lambda x: x self.model = nn.Sequential(self.spec_norm(self.conv1), nn.ReLU(), self.spec_norm(self.conv2), nn.AvgPool2d(2)) self.bypass = nn.Sequential(nn.AvgPool2d(2), self.spec_norm(self. bypass_conv)) def forward(self, x): return self.model(x) + self.bypass(x) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import numpy as np from torch import nn from torch.nn.utils.spectral_norm import spectral_norm as SpectralNorm 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_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 = 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_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 tl.store(in_out_ptr0 + x3, tmp2, xmask) @triton.jit def triton_poi_fused_avg_pool2d_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 x0 = xindex % 2 x1 = xindex // 2 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (4 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (5 + 2 * x0 + 8 * x1), xmask, 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, xmask) @triton.jit def triton_poi_fused_add_avg_pool2d_convolution_3(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 % 2 x4 = xindex // 2 x5 = xindex x2 = xindex // 4 % 4 tmp0 = tl.load(in_ptr0 + (2 * x0 + 8 * x4), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 8 * x4), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (4 + 2 * x0 + 8 * x4), xmask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (5 + 2 * x0 + 8 * x4), xmask, eviction_policy= 'evict_last') tmp9 = tl.load(in_out_ptr0 + x5, xmask) tmp10 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last') tmp2 = tmp1 + tmp0 tmp4 = tmp3 + tmp2 tmp6 = tmp5 + tmp4 tmp7 = 0.25 tmp8 = tmp6 * tmp7 tmp11 = tmp9 + tmp10 tmp12 = tmp8 + tmp11 tl.store(in_out_ptr0 + x5, tmp12, 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, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4, 3, 3), (36, 9, 3, 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=(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_relu_0[grid(256)](buf1, primals_2, 256, 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, 4, 4, 4), (64, 16, 4, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_1[grid(256)](buf3, primals_5, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32) triton_poi_fused_avg_pool2d_2[grid(64)](primals_3, buf4, 64, XBLOCK =64, num_warps=1, num_stages=1) buf5 = extern_kernels.convolution(buf4, primals_6, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf5, (4, 4, 2, 2), (16, 4, 2, 1)) buf6 = buf5 del buf5 triton_poi_fused_add_avg_pool2d_convolution_3[grid(64)](buf6, buf3, primals_7, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_7 return buf6, primals_1, primals_3, primals_4, primals_6, buf1, buf3, buf4 class FirstResBlockDiscriminatorNew(nn.Module): def __init__(self, in_channels, out_channels, stride=1, spec_norm=False): super(FirstResBlockDiscriminatorNew, self).__init__() self.conv1 = nn.Conv2d(in_channels, out_channels, 3, 1, padding=1) self.conv2 = nn.Conv2d(out_channels, out_channels, 3, 1, padding=1) self.bypass_conv = nn.Conv2d(in_channels, out_channels, 1, 1, padding=0 ) nn.init.xavier_uniform(self.conv1.weight.data, 1.0) nn.init.xavier_uniform(self.conv2.weight.data, 1.0) nn.init.xavier_uniform(self.bypass_conv.weight.data, np.sqrt(2)) if spec_norm: self.spec_norm = SpectralNorm else: self.spec_norm = lambda x: x self.model = nn.Sequential(self.spec_norm(self.conv1), nn.ReLU(), self.spec_norm(self.conv2), nn.AvgPool2d(2)) self.bypass = nn.Sequential(nn.AvgPool2d(2), self.spec_norm(self. bypass_conv)) 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.bypass_conv.weight primals_7 = self.bypass_conv.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
ldlasso2/hologan-pytorch
FirstResBlockDiscriminator
false
15,890
[ "BSD-3-Clause" ]
61
baec67d3673cc68e51434516d19465f3d6dd0a1b
https://github.com/ldlasso2/hologan-pytorch/tree/baec67d3673cc68e51434516d19465f3d6dd0a1b
Tacotron2Loss
import torch import torch.utils.data from torch import nn class Tacotron2Loss(nn.Module): def __init__(self): super(Tacotron2Loss, self).__init__() def forward(self, model_output, targets): mel_target, gate_target = targets[0], targets[1] mel_out_before, mel_out_after, gate_out, _ = model_output mel_loss = nn.MSELoss()(mel_out_before, mel_target) + nn.MSELoss()( mel_out_after, mel_target) gate_loss = nn.BCEWithLogitsLoss()(gate_out.view(-1, 1), gate_target.view(-1, 1)) return mel_loss + gate_loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.utils.data from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_binary_cross_entropy_with_logits_mse_loss_0( 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 tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.load(in_ptr1 + r0, None) tmp7 = tl.load(in_ptr0 + (64 + r0), None) tmp13 = tl.load(in_ptr1 + (64 + r0), None) tmp16 = tl.load(in_ptr0 + (128 + r0), None) tmp2 = tmp0 - tmp1 tmp3 = tmp2 * tmp2 tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.sum(tmp4, 1)[:, None] tmp8 = tmp7 - tmp1 tmp9 = tmp8 * tmp8 tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK]) tmp12 = tl.sum(tmp10, 1)[:, None] tmp14 = 1.0 tmp15 = tmp14 - tmp13 tmp17 = tmp15 * tmp16 tmp18 = 0.0 tmp19 = triton_helpers.minimum(tmp18, tmp16) tmp20 = tl_math.abs(tmp16) tmp21 = -tmp20 tmp22 = tl_math.exp(tmp21) tmp23 = libdevice.log1p(tmp22) tmp24 = tmp19 - tmp23 tmp25 = tmp17 - tmp24 tmp26 = tl.broadcast_to(tmp25, [XBLOCK, RBLOCK]) tmp28 = tl.sum(tmp26, 1)[:, None] tmp29 = 64.0 tmp30 = tmp6 / tmp29 tmp31 = tmp12 / tmp29 tmp32 = tmp30 + tmp31 tmp33 = tmp28 / tmp29 tmp34 = tmp32 + tmp33 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp34, 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_binary_cross_entropy_with_logits_mse_loss_0[grid (1)](buf3, arg1_1, arg0_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf3, class Tacotron2LossNew(nn.Module): def __init__(self): super(Tacotron2LossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
leijue222/tacotron2
Tacotron2Loss
false
15,891
[ "BSD-3-Clause" ]
93
5950728a91e7a9355f42f658e00db2a2aef94247
https://github.com/leijue222/tacotron2/tree/5950728a91e7a9355f42f658e00db2a2aef94247
LocationLayer
import torch import torch.utils.data from torch import nn class LinearNorm(torch.nn.Module): def __init__(self, in_dim, out_dim, bias=True, w_init_gain='linear'): super(LinearNorm, self).__init__() self.linear_layer = torch.nn.Linear(in_dim, out_dim, bias=bias) def forward(self, x): return self.linear_layer(x) class ConvNorm(torch.nn.Module): def __init__(self, in_channels, out_channels, kernel_size=1, stride=1, padding=None, dilation=1, bias=True, w_init_gain='linear'): super(ConvNorm, self).__init__() if padding is None: assert kernel_size % 2 == 1 padding = int(dilation * (kernel_size - 1) / 2) self.conv = torch.nn.Conv1d(in_channels, out_channels, kernel_size= kernel_size, stride=stride, padding=padding, dilation=dilation, bias=bias) def forward(self, signal): return self.conv(signal) class LocationLayer(nn.Module): def __init__(self, attention_n_filters, attention_kernel_size, attention_dim): super(LocationLayer, self).__init__() self.location_conv = ConvNorm(1, attention_n_filters, kernel_size= attention_kernel_size, padding=int((attention_kernel_size - 1) / 2), stride=1, dilation=1) self.location_dense = LinearNorm(attention_n_filters, attention_dim, bias=False, w_init_gain='tanh') def forward(self, attention_weights_cum): processed_attention_weights = self.location_conv(attention_weights_cum) processed_attention_weights = processed_attention_weights.transpose( 1, 2) processed_attention_weights = self.location_dense( processed_attention_weights) return processed_attention_weights def get_inputs(): return [torch.rand([4, 1, 64])] def get_init_inputs(): return [[], {'attention_n_filters': 4, 'attention_kernel_size': 4, 'attention_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.utils.data from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clone_0(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 252 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 % 63 y1 = yindex // 63 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 63 * x2 + 252 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 1, 4), (4, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 1, 64), (64, 64, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1,), padding=(1,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 63), (252, 63, 1)) buf1 = empty_strided_cuda((4, 63, 4), (252, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clone_0[grid(252, 4)](buf0, primals_2, buf1, 252, 4, XBLOCK=4, YBLOCK=64, num_warps=4, num_stages=1) del primals_2 buf2 = reinterpret_tensor(buf0, (252, 4), (4, 1), 0) del buf0 extern_kernels.mm(reinterpret_tensor(buf1, (252, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf2) return reinterpret_tensor(buf2, (4, 63, 4), (252, 4, 1), 0 ), primals_1, primals_3, reinterpret_tensor(buf1, (252, 4), (4, 1), 0 ), primals_4 class LinearNorm(torch.nn.Module): def __init__(self, in_dim, out_dim, bias=True, w_init_gain='linear'): super(LinearNorm, self).__init__() self.linear_layer = torch.nn.Linear(in_dim, out_dim, bias=bias) def forward(self, x): return self.linear_layer(x) class ConvNorm(torch.nn.Module): def __init__(self, in_channels, out_channels, kernel_size=1, stride=1, padding=None, dilation=1, bias=True, w_init_gain='linear'): super(ConvNorm, self).__init__() if padding is None: assert kernel_size % 2 == 1 padding = int(dilation * (kernel_size - 1) / 2) self.conv = torch.nn.Conv1d(in_channels, out_channels, kernel_size= kernel_size, stride=stride, padding=padding, dilation=dilation, bias=bias) def forward(self, signal): return self.conv(signal) class LocationLayerNew(nn.Module): def __init__(self, attention_n_filters, attention_kernel_size, attention_dim): super(LocationLayerNew, self).__init__() self.location_conv = ConvNorm(1, attention_n_filters, kernel_size= attention_kernel_size, padding=int((attention_kernel_size - 1) / 2), stride=1, dilation=1) self.location_dense = LinearNorm(attention_n_filters, attention_dim, bias=False, w_init_gain='tanh') def forward(self, input_0): primals_1 = self.location_conv.conv.weight primals_2 = self.location_conv.conv.bias primals_4 = self.location_dense.linear_layer.weight primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
leijue222/tacotron2
LocationLayer
false
15,892
[ "BSD-3-Clause" ]
93
5950728a91e7a9355f42f658e00db2a2aef94247
https://github.com/leijue222/tacotron2/tree/5950728a91e7a9355f42f658e00db2a2aef94247
ResBlock2d
import torch from torch import nn class ResBlock2d(nn.Module): def __init__(self, in_ch, out_ch): super(ResBlock2d, self).__init__() self.conv1 = nn.Conv2d(in_ch, out_ch, 3, 1, padding=1) self.conv2 = nn.Conv2d(out_ch, out_ch, 3, 1, padding=1) self.bn = nn.InstanceNorm2d(in_ch) self.relu = nn.LeakyReLU() self.bn2 = nn.InstanceNorm2d(out_ch) nn.init.xavier_uniform(self.conv1.weight.data, 1.0) nn.init.xavier_uniform(self.conv2.weight.data, 1.0) bypass = [] if in_ch != out_ch: bypass.append(nn.Conv2d(in_ch, out_ch, 1, 1)) self.bypass = nn.Sequential(*bypass) def forward(self, inp): x = self.bn(inp) x = self.relu(x) x = self.conv1(x) x = self.bn2(x) x = self.relu(x) x = self.conv2(x) return x + self.bypass(inp) 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 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_per_fused__native_batch_norm_legit_leaky_relu_0(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]) 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 tmp24 = 0.0 tmp25 = tmp23 > tmp24 tmp26 = 0.01 tmp27 = tmp23 * tmp26 tmp28 = tl.where(tmp25, tmp23, tmp27) tl.store(out_ptr2 + (r1 + 16 * x0), tmp28, xmask) @triton.jit def triton_per_fused__native_batch_norm_legit_convolution_leaky_relu_1( in_out_ptr0, in_out_ptr1, in_ptr0, 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') 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 tmp28 = 0.01 tmp29 = tmp25 * tmp28 tmp30 = tl.where(tmp27, tmp25, 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 * x3), tmp30, xmask) tl.store(out_ptr0 + x3, tmp12, xmask) @triton.jit def triton_poi_fused_add_convolution_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_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) 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) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_per_fused__native_batch_norm_legit_leaky_relu_0[grid(16)]( primals_1, buf3, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) buf4 = extern_kernels.convolution(buf3, primals_2, 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 buf6 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 1, 1), torch.float32) buf7 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32 ) buf9 = reinterpret_tensor(buf7, (1, 16, 1, 1), (16, 1, 1, 1), 0) del buf7 buf10 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_per_fused__native_batch_norm_legit_convolution_leaky_relu_1[grid (16)](buf5, buf9, primals_3, buf6, buf10, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) del primals_3 buf11 = extern_kernels.convolution(buf10, primals_4, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf11, (4, 4, 4, 4), (64, 16, 4, 1)) buf12 = buf11 del buf11 triton_poi_fused_add_convolution_2[grid(256)](buf12, primals_5, primals_1, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 del primals_5 return buf12, primals_2, primals_4, buf3, buf5, buf6, buf9, buf10 class ResBlock2dNew(nn.Module): def __init__(self, in_ch, out_ch): super(ResBlock2dNew, self).__init__() self.conv1 = nn.Conv2d(in_ch, out_ch, 3, 1, padding=1) self.conv2 = nn.Conv2d(out_ch, out_ch, 3, 1, padding=1) self.bn = nn.InstanceNorm2d(in_ch) self.relu = nn.LeakyReLU() self.bn2 = nn.InstanceNorm2d(out_ch) nn.init.xavier_uniform(self.conv1.weight.data, 1.0) nn.init.xavier_uniform(self.conv2.weight.data, 1.0) bypass = [] if in_ch != out_ch: bypass.append(nn.Conv2d(in_ch, out_ch, 1, 1)) self.bypass = nn.Sequential(*bypass) 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]
ldlasso2/hologan-pytorch
ResBlock2d
false
15,893
[ "BSD-3-Clause" ]
61
baec67d3673cc68e51434516d19465f3d6dd0a1b
https://github.com/ldlasso2/hologan-pytorch/tree/baec67d3673cc68e51434516d19465f3d6dd0a1b
SpatialAttention
import torch import torch.nn as nn class SpatialAttention(nn.Module): def __init__(self, kernel_size=3): super(SpatialAttention, self).__init__() assert kernel_size in (3, 7), 'kernel size must be 3 or 7' padding = 3 if kernel_size == 7 else 1 self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False) self.sigmoid = nn.Sigmoid() def forward(self, x): avg_out = torch.mean(x, dim=1, keepdim=True) max_out, _ = torch.max(x, dim=1, keepdim=True) y = torch.cat([avg_out, max_out], dim=1) y = self.conv1(y) return self.sigmoid(y) * x 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 @triton.jit def triton_poi_fused_cat_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 x1 = xindex // 16 % 2 x0 = xindex % 16 x2 = xindex // 32 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 + 64 * x2), tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp6 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp7 = tmp5 + tmp6 tmp8 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp9 = tmp7 + tmp8 tmp10 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp11 = tmp9 + tmp10 tmp12 = 4.0 tmp13 = tmp11 / tmp12 tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype) tmp15 = tl.where(tmp4, tmp13, tmp14) tmp16 = tmp0 >= tmp3 tl.full([1], 2, tl.int64) tmp19 = tl.load(in_ptr0 + (x0 + 64 * x2), tmp16 & xmask, eviction_policy='evict_last', other=0.0) tmp20 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), tmp16 & xmask, eviction_policy='evict_last', other=0.0) tmp21 = triton_helpers.maximum(tmp19, tmp20) tmp22 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), tmp16 & xmask, eviction_policy='evict_last', other=0.0) tmp23 = triton_helpers.maximum(tmp21, tmp22) tmp24 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), tmp16 & xmask, eviction_policy='evict_last', other=0.0) tmp25 = triton_helpers.maximum(tmp23, tmp24) tmp26 = tl.full(tmp25.shape, 0.0, tmp25.dtype) tmp27 = tl.where(tmp16, tmp25, tmp26) tmp28 = tl.where(tmp4, tmp15, tmp27) tl.store(out_ptr0 + x3, tmp28, 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 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (1, 2, 3, 3), (18, 9, 3, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(128)](primals_1, buf0, 128, XBLOCK=128, 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, 1, 4, 4), (16, 16, 4, 1)) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_mul_sigmoid_1[grid(256)](buf1, primals_1, buf2, 256, XBLOCK=256, num_warps=4, num_stages=1) return buf2, primals_1, primals_2, buf0, buf1 class SpatialAttentionNew(nn.Module): def __init__(self, kernel_size=3): super(SpatialAttentionNew, self).__init__() assert kernel_size in (3, 7), 'kernel size must be 3 or 7' padding = 3 if kernel_size == 7 else 1 self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False) self.sigmoid = nn.Sigmoid() def forward(self, input_0): primals_2 = self.conv1.weight primals_1 = input_0 output = call([primals_1, primals_2]) return output[0]
lee-zq/VesselSeg-pytorch
SpatialAttention
false
15,894
[ "Apache-2.0" ]
83
b4f6571fc1fb1fbdaad60ff9282a54a1f1c455fa
https://github.com/lee-zq/VesselSeg-pytorch/tree/b4f6571fc1fb1fbdaad60ff9282a54a1f1c455fa