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
SRNet
import torch import torch.nn as nn import torch.optim class eca_layer(nn.Module): """Constructs a ECA module. Args: channel: Number of channels of the input feature map k_size: Adaptive selection of kernel size """ def __init__(self, channel, k_size=3): super(eca_layer, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.conv = nn.Conv1d(1, 1, kernel_size=k_size, padding=(k_size - 1 ) // 2, bias=False) self.sigmoid = nn.Sigmoid() def forward(self, x): _b, _c, _h, _w = x.size() y = self.avg_pool(x) y = self.conv(y.squeeze(-1).transpose(-1, -2)).transpose(-1, -2 ).unsqueeze(-1) y = self.sigmoid(y) return x * y.expand_as(x) class SRNet(nn.Module): def __init__(self): super(SRNet, self).__init__() self.relu = nn.ReLU(inplace=True) self.pad = nn.ReflectionPad2d(1) self.Conv1 = nn.Conv2d(3, 64, 3, 1, 1, bias=True) self.Conv1_ = nn.Conv2d(3, 64, 5, 1, 1, bias=True) self.Conv2 = nn.Conv2d(128, 64, 3, 1, 1, bias=True) self.Conv2_ = nn.Conv2d(128, 64, 5, 1, 1, bias=True) self.Conv3 = nn.Conv2d(128, 64, 3, 1, 1, bias=True) self.Conv4 = nn.Conv2d(64, 3, 3, 1, 1, bias=True) self.Conv5 = nn.Conv2d(3, 64, 3, 1, 1, bias=True) self.Conv5_ = nn.Conv2d(3, 64, 5, 1, 1, bias=True) self.Conv6 = nn.Conv2d(128, 64, 3, 1, 1, bias=True) self.Conv6_ = nn.Conv2d(128, 64, 5, 1, 1, bias=True) self.Conv7 = nn.Conv2d(128, 64, 3, 1, 1, bias=True) self.Conv8 = nn.Conv2d(64, 3, 3, 1, 1, bias=True) self.eca = eca_layer(3, 3) def forward(self, LR_img): x = self.relu(self.Conv1(LR_img)) x_ = self.relu(self.Conv1_(self.pad(LR_img))) x = torch.cat((x, x_), dim=1) x1 = self.relu(self.Conv2(x)) x1_ = self.relu(self.Conv2_(self.pad(x))) x2 = torch.cat((x1, x1_), dim=1) x2 = self.relu(self.Conv3(x2)) x2 = self.Conv4(x2) x2 = self.eca(x2) x2 = x2 + LR_img x3 = self.relu(self.Conv5(x2)) x3_ = self.relu(self.Conv5_(self.pad(x2))) x4 = torch.cat((x3, x3_), dim=1) x5 = self.relu(self.Conv6(x4)) x5_ = self.relu(self.Conv6_(self.pad(x4))) x6 = torch.cat((x5, x5_), dim=1) x6 = self.relu(self.Conv7(x6)) x6 = self.Conv8(x6) x6 = self.eca(x6) x6 = x6 + x2 return x6 def get_inputs(): return [torch.rand([4, 3, 64, 64])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn import torch.optim assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_reflection_pad2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 52272 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 66 x1 = xindex // 66 % 66 x2 = xindex // 4356 x3 = xindex tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-1 + x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-1 + x1)) + 4096 * x2), xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_poi_fused_cat_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 4096 % 128 x0 = xindex % 4096 x2 = xindex // 524288 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 64, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 4096 * x1 + 262144 * x2), tmp4, other=0.0) tmp6 = tl.load(in_ptr1 + x1, tmp4, eviction_policy='evict_last', other=0.0) tmp7 = tmp5 + tmp6 tmp8 = tl.full([1], 0, tl.int32) tmp9 = triton_helpers.maximum(tmp8, tmp7) tmp10 = tl.full(tmp9.shape, 0.0, tmp9.dtype) tmp11 = tl.where(tmp4, tmp9, tmp10) tmp12 = tmp0 >= tmp3 tl.full([1], 128, tl.int64) tmp15 = tl.load(in_ptr2 + (x0 + 4096 * (-64 + x1) + 262144 * x2), tmp12, other=0.0) tmp16 = tl.load(in_ptr3 + (-64 + x1), tmp12, eviction_policy= 'evict_last', other=0.0) tmp17 = tmp15 + tmp16 tmp18 = triton_helpers.maximum(tmp8, tmp17) tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype) tmp20 = tl.where(tmp12, tmp18, tmp19) tmp21 = tl.where(tmp4, tmp11, tmp20) tl.store(out_ptr0 + x3, tmp21, None) @triton.jit def triton_poi_fused_reflection_pad2d_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x0 = xindex % 66 x1 = xindex // 66 % 66 x2 = xindex // 4356 x3 = xindex tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-1 + x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-1 + x1)) + 4096 * x2), None, eviction_policy='evict_last') tl.store(out_ptr0 + x3, tmp0, None) @triton.jit def triton_poi_fused_convolution_relu_3(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 % 64 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_red_fused_convolution_mean_4(in_out_ptr0, in_out_ptr1, in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr, RBLOCK: tl.constexpr): xnumel = 12 rnumel = 4096 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rbase = tl.arange(0, RBLOCK)[None, :] x3 = xindex x0 = xindex % 3 tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') _tmp4 = tl.full([XBLOCK, RBLOCK], 0, tl.float32) for roffset in range(0, rnumel, RBLOCK): rindex = roffset + rbase rmask = rindex < rnumel r2 = rindex tmp0 = tl.load(in_out_ptr0 + (r2 + 4096 * x3), rmask & xmask, eviction_policy='evict_first', other=0.0) tmp2 = tmp0 + tmp1 tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp5 = _tmp4 + tmp3 _tmp4 = tl.where(rmask & xmask, tmp5, _tmp4) tl.store(in_out_ptr0 + (r2 + 4096 * x3), tmp2, rmask & xmask) tmp4 = tl.sum(_tmp4, 1)[:, None] tmp6 = 4096.0 tmp7 = tmp4 / tmp6 tl.debug_barrier() tl.store(in_out_ptr1 + x3, tmp7, xmask) @triton.jit def triton_poi_fused_add_mul_5(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 x1 = xindex // 4096 tmp0 = tl.load(in_ptr0 + x2, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp4 = tl.load(in_ptr2 + x2, None) tmp2 = tl.sigmoid(tmp1) tmp3 = tmp0 * tmp2 tmp5 = tmp3 + tmp4 tl.store(out_ptr0 + x2, tmp5, None) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_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) x3 = xindex x1 = xindex // 4096 % 64 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(out_ptr0 + x3, tmp6, None) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16, primals_17, primals_18, primals_19, primals_20, primals_21, primals_22, primals_23, primals_24, primals_25, primals_26) = args args.clear() assert_size_stride(primals_1, (64, 3, 3, 3), (27, 9, 3, 1)) assert_size_stride(primals_2, (64,), (1,)) assert_size_stride(primals_3, (4, 3, 64, 64), (12288, 4096, 64, 1)) assert_size_stride(primals_4, (64, 3, 5, 5), (75, 25, 5, 1)) assert_size_stride(primals_5, (64,), (1,)) assert_size_stride(primals_6, (64, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_7, (64,), (1,)) assert_size_stride(primals_8, (64, 128, 5, 5), (3200, 25, 5, 1)) assert_size_stride(primals_9, (64,), (1,)) assert_size_stride(primals_10, (64, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_11, (64,), (1,)) assert_size_stride(primals_12, (3, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_13, (3,), (1,)) assert_size_stride(primals_14, (1, 1, 3), (3, 3, 1)) assert_size_stride(primals_15, (64, 3, 3, 3), (27, 9, 3, 1)) assert_size_stride(primals_16, (64,), (1,)) assert_size_stride(primals_17, (64, 3, 5, 5), (75, 25, 5, 1)) assert_size_stride(primals_18, (64,), (1,)) assert_size_stride(primals_19, (64, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_20, (64,), (1,)) assert_size_stride(primals_21, (64, 128, 5, 5), (3200, 25, 5, 1)) assert_size_stride(primals_22, (64,), (1,)) assert_size_stride(primals_23, (64, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_24, (64,), (1,)) assert_size_stride(primals_25, (3, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_26, (3,), (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, 64, 64, 64), (262144, 4096, 64, 1)) buf1 = empty_strided_cuda((4, 3, 66, 66), (13068, 4356, 66, 1), torch.float32) get_raw_stream(0) triton_poi_fused_reflection_pad2d_0[grid(52272)](primals_3, buf1, 52272, XBLOCK=256, num_warps=4, num_stages=1) 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, 64, 64, 64), (262144, 4096, 64, 1)) buf3 = empty_strided_cuda((4, 128, 64, 64), (524288, 4096, 64, 1), torch.float32) triton_poi_fused_cat_1[grid(2097152)](buf0, primals_2, buf2, primals_5, buf3, 2097152, XBLOCK=1024, num_warps=4, num_stages=1) buf4 = extern_kernels.convolution(buf3, primals_6, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf5 = empty_strided_cuda((4, 128, 66, 66), (557568, 4356, 66, 1), torch.float32) triton_poi_fused_reflection_pad2d_2[grid(2230272)](buf3, buf5, 2230272, XBLOCK=512, num_warps=8, num_stages=1) 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 = empty_strided_cuda((4, 128, 64, 64), (524288, 4096, 64, 1), torch.float32) triton_poi_fused_cat_1[grid(2097152)](buf4, primals_7, buf6, primals_9, buf7, 2097152, XBLOCK=1024, num_warps=4, num_stages=1) buf8 = extern_kernels.convolution(buf7, primals_10, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf9 = buf8 del buf8 triton_poi_fused_convolution_relu_3[grid(1048576)](buf9, primals_11, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del primals_11 buf10 = extern_kernels.convolution(buf9, primals_12, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf10, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf11 = buf10 del buf10 buf12 = empty_strided_cuda((4, 3, 1, 1), (3, 1, 12, 12), torch.float32) buf13 = buf12 del buf12 triton_red_fused_convolution_mean_4[grid(12)](buf11, buf13, primals_13, 12, 4096, XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1) del primals_13 buf14 = extern_kernels.convolution(reinterpret_tensor(buf13, (4, 1, 3), (3, 0, 1), 0), primals_14, stride=(1,), padding=(1,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf14, (4, 1, 3), (3, 3, 1)) buf15 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.float32) triton_poi_fused_add_mul_5[grid(49152)](buf11, buf14, primals_3, buf15, 49152, XBLOCK=512, num_warps=4, num_stages=1) buf16 = extern_kernels.convolution(buf15, primals_15, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf16, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf17 = empty_strided_cuda((4, 3, 66, 66), (13068, 4356, 66, 1), torch.float32) triton_poi_fused_reflection_pad2d_0[grid(52272)](buf15, buf17, 52272, XBLOCK=256, num_warps=4, num_stages=1) buf18 = extern_kernels.convolution(buf17, primals_17, 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 = empty_strided_cuda((4, 128, 64, 64), (524288, 4096, 64, 1), torch.float32) triton_poi_fused_cat_1[grid(2097152)](buf16, primals_16, buf18, primals_18, buf19, 2097152, XBLOCK=1024, num_warps=4, num_stages=1) buf20 = extern_kernels.convolution(buf19, primals_19, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf20, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf21 = empty_strided_cuda((4, 128, 66, 66), (557568, 4356, 66, 1), torch.float32) triton_poi_fused_reflection_pad2d_2[grid(2230272)](buf19, buf21, 2230272, XBLOCK=512, num_warps=8, num_stages=1) buf22 = extern_kernels.convolution(buf21, primals_21, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf22, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf23 = empty_strided_cuda((4, 128, 64, 64), (524288, 4096, 64, 1), torch.float32) triton_poi_fused_cat_1[grid(2097152)](buf20, primals_20, buf22, primals_22, buf23, 2097152, XBLOCK=1024, num_warps=4, num_stages=1) buf24 = extern_kernels.convolution(buf23, primals_23, 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_convolution_relu_3[grid(1048576)](buf25, primals_24, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del primals_24 buf26 = extern_kernels.convolution(buf25, primals_25, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf26, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf27 = buf26 del buf26 buf28 = empty_strided_cuda((4, 3, 1, 1), (3, 1, 12, 12), torch.float32) buf29 = buf28 del buf28 triton_red_fused_convolution_mean_4[grid(12)](buf27, buf29, primals_26, 12, 4096, XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1) del primals_26 buf30 = extern_kernels.convolution(reinterpret_tensor(buf29, (4, 1, 3), (3, 0, 1), 0), primals_14, stride=(1,), padding=(1,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf30, (4, 1, 3), (3, 3, 1)) buf31 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.float32) triton_poi_fused_add_mul_5[grid(49152)](buf27, buf30, buf15, buf31, 49152, XBLOCK=512, num_warps=4, num_stages=1) buf32 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_6[grid(1048576)]( buf22, primals_22, buf32, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del buf22 del primals_22 buf33 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_6[grid(1048576)]( buf20, primals_20, buf33, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del buf20 del primals_20 buf34 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_6[grid(1048576)]( buf18, primals_18, buf34, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del buf18 del primals_18 buf35 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_6[grid(1048576)]( buf16, primals_16, buf35, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del buf16 del primals_16 buf36 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_6[grid(1048576)]( buf6, primals_9, buf36, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del buf6 del primals_9 buf37 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_6[grid(1048576)]( buf4, primals_7, buf37, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del buf4 del primals_7 buf38 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_6[grid(1048576)]( buf2, primals_5, buf38, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del buf2 del primals_5 buf39 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_6[grid(1048576)]( buf0, primals_2, buf39, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del buf0 del primals_2 return (buf31, primals_1, primals_3, primals_4, primals_6, primals_8, primals_10, primals_12, primals_14, primals_15, primals_17, primals_19, primals_21, primals_23, primals_25, buf1, buf3, buf5, buf7, buf9, buf11, reinterpret_tensor(buf13, (4, 1, 3), (3, 1, 1), 0), buf14, buf15, buf17, buf19, buf21, buf23, buf25, buf27, reinterpret_tensor(buf29, (4, 1, 3), (3, 1, 1), 0), buf30, buf32, buf33, buf34, buf35, buf36, buf37, buf38, buf39) class eca_layer(nn.Module): """Constructs a ECA module. Args: channel: Number of channels of the input feature map k_size: Adaptive selection of kernel size """ def __init__(self, channel, k_size=3): super(eca_layer, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.conv = nn.Conv1d(1, 1, kernel_size=k_size, padding=(k_size - 1 ) // 2, bias=False) self.sigmoid = nn.Sigmoid() def forward(self, x): _b, _c, _h, _w = x.size() y = self.avg_pool(x) y = self.conv(y.squeeze(-1).transpose(-1, -2)).transpose(-1, -2 ).unsqueeze(-1) y = self.sigmoid(y) return x * y.expand_as(x) class SRNetNew(nn.Module): def __init__(self): super(SRNetNew, self).__init__() self.relu = nn.ReLU(inplace=True) self.pad = nn.ReflectionPad2d(1) self.Conv1 = nn.Conv2d(3, 64, 3, 1, 1, bias=True) self.Conv1_ = nn.Conv2d(3, 64, 5, 1, 1, bias=True) self.Conv2 = nn.Conv2d(128, 64, 3, 1, 1, bias=True) self.Conv2_ = nn.Conv2d(128, 64, 5, 1, 1, bias=True) self.Conv3 = nn.Conv2d(128, 64, 3, 1, 1, bias=True) self.Conv4 = nn.Conv2d(64, 3, 3, 1, 1, bias=True) self.Conv5 = nn.Conv2d(3, 64, 3, 1, 1, bias=True) self.Conv5_ = nn.Conv2d(3, 64, 5, 1, 1, bias=True) self.Conv6 = nn.Conv2d(128, 64, 3, 1, 1, bias=True) self.Conv6_ = nn.Conv2d(128, 64, 5, 1, 1, bias=True) self.Conv7 = nn.Conv2d(128, 64, 3, 1, 1, bias=True) self.Conv8 = nn.Conv2d(64, 3, 3, 1, 1, bias=True) self.eca = eca_layer(3, 3) def forward(self, input_0): primals_1 = self.Conv1.weight primals_2 = self.Conv1.bias primals_4 = self.Conv1_.weight primals_5 = self.Conv1_.bias primals_6 = self.Conv2.weight primals_7 = self.Conv2.bias primals_8 = self.Conv2_.weight primals_9 = self.Conv2_.bias primals_10 = self.Conv3.weight primals_11 = self.Conv3.bias primals_12 = self.Conv4.weight primals_13 = self.Conv4.bias primals_15 = self.Conv5.weight primals_16 = self.Conv5.bias primals_17 = self.Conv5_.weight primals_18 = self.Conv5_.bias primals_19 = self.Conv6.weight primals_20 = self.Conv6.bias primals_21 = self.Conv6_.weight primals_22 = self.Conv6_.bias primals_23 = self.Conv7.weight primals_24 = self.Conv7.bias primals_25 = self.Conv8.weight primals_26 = self.Conv8.bias primals_14 = self.eca.conv.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, primals_24, primals_25, primals_26]) return output[0]
purbayankar/PyTorch-Zero-Shot-Super-Resolution
SRNet
false
12,968
[ "MIT" ]
0
434fe5e84e166eef1f8c03880fc83c7e8749c49c
https://github.com/purbayankar/PyTorch-Zero-Shot-Super-Resolution/tree/434fe5e84e166eef1f8c03880fc83c7e8749c49c
EncoderLayer
import math import torch import torch.utils.data import torch.nn as nn import torch.nn.functional as F class MultiHeadAttention(nn.Module): def __init__(self, heads, d_model): super(MultiHeadAttention, self).__init__() assert d_model % heads == 0 self.d_k = d_model // heads self.heads = heads self.dropout = nn.Dropout(0.1) self.query = nn.Linear(d_model, d_model) self.key = nn.Linear(d_model, d_model) self.value = nn.Linear(d_model, d_model) self.concat = nn.Linear(d_model, d_model) def forward(self, query, key, value, mask): """ query, key, value of shape: (batch_size, max_len, 512) mask of shape: (batch_size, 1, 1, max_words) """ query = self.query(query) key = self.key(key) value = self.value(value) query = query.view(query.shape[0], -1, self.heads, self.d_k).permute( 0, 2, 1, 3) key = key.view(key.shape[0], -1, self.heads, self.d_k).permute(0, 2, 1, 3) value = value.view(value.shape[0], -1, self.heads, self.d_k).permute( 0, 2, 1, 3) scores = torch.matmul(query, key.permute(0, 1, 3, 2)) / math.sqrt(query .size(-1)) scores = scores.masked_fill(mask == 0, -1000000000.0) weights = F.softmax(scores, dim=-1) weights = self.dropout(weights) context = torch.matmul(weights, value) context = context.permute(0, 2, 1, 3).contiguous().view(context. shape[0], -1, self.heads * self.d_k) interacted = self.concat(context) return interacted class FeedForward(nn.Module): def __init__(self, d_model, middle_dim=2048): super(FeedForward, self).__init__() self.fc1 = nn.Linear(d_model, middle_dim) self.fc2 = nn.Linear(middle_dim, d_model) self.dropout = nn.Dropout(0.1) def forward(self, x): out = F.relu(self.fc1(x)) out = self.fc2(self.dropout(out)) return out class EncoderLayer(nn.Module): def __init__(self, d_model, heads): super(EncoderLayer, self).__init__() self.layernorm = nn.LayerNorm(d_model) self.self_multihead = MultiHeadAttention(heads, d_model) self.feed_forward = FeedForward(d_model) self.dropout = nn.Dropout(0.1) def forward(self, embeddings, mask): interacted = self.dropout(self.self_multihead(embeddings, embeddings, embeddings, mask)) interacted = self.layernorm(interacted + embeddings) feed_forward_out = self.dropout(self.feed_forward(interacted)) encoded = self.layernorm(feed_forward_out + interacted) return encoded def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'d_model': 4, 'heads': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import math import torch.utils.data 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_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_eq_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.0 tmp2 = tmp0 == tmp1 tl.store(out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_poi_fused__softmax_div_masked_fill_2(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 16 x2 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last').to(tl .int1) tmp1 = tl.load(in_ptr1 + 4 * x2, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp7 = tl.load(in_ptr1 + (1 + 4 * x2), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp12 = tl.load(in_ptr1 + (2 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp16 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp17 = tl.load(in_ptr1 + (3 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp2 = 1.0 tmp3 = tmp1 * tmp2 tmp4 = -1000000000.0 tmp5 = tl.where(tmp0, tmp4, tmp3) tmp8 = tmp7 * tmp2 tmp9 = tl.where(tmp6, tmp4, tmp8) tmp10 = triton_helpers.maximum(tmp5, tmp9) tmp13 = tmp12 * tmp2 tmp14 = tl.where(tmp11, tmp4, tmp13) tmp15 = triton_helpers.maximum(tmp10, tmp14) tmp18 = tmp17 * tmp2 tmp19 = tl.where(tmp16, tmp4, tmp18) tmp20 = triton_helpers.maximum(tmp15, tmp19) tmp21 = tmp5 - tmp20 tmp22 = tl_math.exp(tmp21) tmp23 = tmp9 - tmp20 tmp24 = tl_math.exp(tmp23) tmp25 = tmp22 + tmp24 tmp26 = tmp14 - tmp20 tmp27 = tl_math.exp(tmp26) tmp28 = tmp25 + tmp27 tmp29 = tmp19 - tmp20 tmp30 = tl_math.exp(tmp29) tmp31 = tmp28 + tmp30 tl.store(out_ptr0 + x2, tmp20, xmask) tl.store(out_ptr1 + x2, tmp31, xmask) @triton.jit def triton_poi_fused__softmax_div_masked_fill_3(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex % 64 x4 = xindex x5 = xindex // 4 tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last').to(tl .int1) tmp1 = tl.load(in_out_ptr0 + x4, xmask) tmp6 = tl.load(in_ptr1 + x5, xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr2 + x5, xmask, eviction_policy='evict_last') tmp2 = 1.0 tmp3 = tmp1 * tmp2 tmp4 = -1000000000.0 tmp5 = tl.where(tmp0, tmp4, tmp3) tmp7 = tmp5 - tmp6 tmp8 = tl_math.exp(tmp7) tmp10 = tmp8 / tmp9 tl.store(in_out_ptr0 + x4, tmp10, xmask) @triton.jit def triton_poi_fused_clone_4(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_add_native_layer_norm_5(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp9 = tmp7 + tmp8 tmp10 = tmp6 + tmp9 tmp13 = tmp11 + tmp12 tmp14 = tmp10 + tmp13 tmp15 = 4.0 tmp16 = tmp14 / tmp15 tmp17 = tmp2 - tmp16 tmp18 = tmp17 * tmp17 tmp19 = tmp5 - tmp16 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp22 = tmp9 - tmp16 tmp23 = tmp22 * tmp22 tmp24 = tmp21 + tmp23 tmp25 = tmp13 - tmp16 tmp26 = tmp25 * tmp25 tmp27 = tmp24 + tmp26 tmp28 = tmp27 / tmp15 tl.store(out_ptr0 + x0, tmp16, xmask) tl.store(out_ptr1 + x0, tmp28, xmask) @triton.jit def triton_poi_fused_add_native_layer_norm_6(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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_7(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 2048 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) @triton.jit def triton_poi_fused_add_8(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_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x2, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_native_layer_norm_9(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_10(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) 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) = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4,), (1,)) assert_size_stride(primals_8, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_9, (4, 4), (4, 1)) assert_size_stride(primals_10, (4,), (1,)) assert_size_stride(primals_11, (4,), (1,)) assert_size_stride(primals_12, (4,), (1,)) assert_size_stride(primals_13, (2048, 4), (4, 1)) assert_size_stride(primals_14, (2048,), (1,)) assert_size_stride(primals_15, (4, 2048), (2048, 1)) assert_size_stride(primals_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=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_2 buf4 = reinterpret_tensor(buf0, (4, 4, 1, 4), (16, 4, 4, 1), 0) del buf0 triton_poi_fused_clone_0[grid(16, 4)](buf1, primals_5, buf4, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_5 buf5 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf3, (16, 4, 1), (4, 1, 0), 0), reinterpret_tensor(buf4, (16, 1, 4), (4, 0, 1), 0), out=buf5) buf6 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool) triton_poi_fused_eq_1[grid(64)](primals_8, buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_8 buf7 = reinterpret_tensor(buf1, (4, 4, 4, 1), (16, 4, 1, 64), 0) del buf1 buf8 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) triton_poi_fused__softmax_div_masked_fill_2[grid(64)](buf6, buf5, buf7, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1) buf9 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf5 triton_poi_fused__softmax_div_masked_fill_3[grid(256)](buf9, buf6, buf7, buf8, 256, XBLOCK=128, num_warps=4, num_stages=1) buf10 = reinterpret_tensor(buf8, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf8 triton_poi_fused_clone_0[grid(16, 4)](buf2, primals_7, buf10, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_7 buf11 = reinterpret_tensor(buf2, (16, 4, 1), (4, 1, 1), 0) del buf2 extern_kernels.bmm(reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf10, (16, 4, 1), (4, 1, 0), 0), out=buf11) buf12 = reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf7 triton_poi_fused_clone_4[grid(16, 4)](buf11, buf12, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf13 = reinterpret_tensor(buf11, (16, 4), (4, 1), 0) del buf11 extern_kernels.addmm(primals_10, reinterpret_tensor(buf12, (16, 4), (4, 1), 0), reinterpret_tensor(primals_9, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf13) del primals_10 buf14 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf15 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) triton_poi_fused_add_native_layer_norm_5[grid(16)](buf13, primals_3, buf14, buf15, 16, XBLOCK=16, num_warps=1, num_stages=1) buf16 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_add_native_layer_norm_6[grid(64)](buf13, primals_3, buf14, buf15, primals_11, primals_12, buf16, 64, XBLOCK=64, num_warps=1, num_stages=1) buf17 = empty_strided_cuda((16, 2048), (2048, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf16, (16, 4), (4, 1), 0), reinterpret_tensor(primals_13, (4, 2048), (1, 4), 0), out=buf17) buf18 = reinterpret_tensor(buf17, (4, 4, 2048), (8192, 2048, 1), 0) del buf17 buf24 = empty_strided_cuda((4, 4, 2048), (8192, 2048, 1), torch.bool) triton_poi_fused_relu_threshold_backward_7[grid(32768)](buf18, primals_14, buf24, 32768, XBLOCK=256, num_warps=4, num_stages=1) del primals_14 buf19 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf18, (16, 2048), (2048, 1), 0), reinterpret_tensor(primals_15, (2048, 4), (1, 2048), 0), out=buf19) buf20 = reinterpret_tensor(buf19, (4, 4, 4), (16, 4, 1), 0) del buf19 triton_poi_fused_add_8[grid(64)](buf20, primals_16, buf16, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_16 buf21 = buf15 del buf15 buf22 = buf14 del buf14 triton_poi_fused_native_layer_norm_9[grid(16)](buf20, buf21, buf22, 16, XBLOCK=16, num_warps=1, num_stages=1) buf23 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_10[grid(64)](buf20, buf21, buf22, primals_11, primals_12, buf23, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf21 del buf22 del primals_12 return buf23, primals_3, primals_11, buf6, buf9, reinterpret_tensor(buf12, (16, 4), (4, 1), 0), buf13, reinterpret_tensor(buf16, (16, 4), (4, 1), 0), reinterpret_tensor(buf18, (16, 2048), (2048, 1), 0 ), buf20, primals_15, buf24, primals_13, primals_9, reinterpret_tensor( buf10, (16, 1, 4), (4, 1, 1), 0), reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0), reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 4), 0) class MultiHeadAttention(nn.Module): def __init__(self, heads, d_model): super(MultiHeadAttention, self).__init__() assert d_model % heads == 0 self.d_k = d_model // heads self.heads = heads self.dropout = nn.Dropout(0.1) self.query = nn.Linear(d_model, d_model) self.key = nn.Linear(d_model, d_model) self.value = nn.Linear(d_model, d_model) self.concat = nn.Linear(d_model, d_model) def forward(self, query, key, value, mask): """ query, key, value of shape: (batch_size, max_len, 512) mask of shape: (batch_size, 1, 1, max_words) """ query = self.query(query) key = self.key(key) value = self.value(value) query = query.view(query.shape[0], -1, self.heads, self.d_k).permute( 0, 2, 1, 3) key = key.view(key.shape[0], -1, self.heads, self.d_k).permute(0, 2, 1, 3) value = value.view(value.shape[0], -1, self.heads, self.d_k).permute( 0, 2, 1, 3) scores = torch.matmul(query, key.permute(0, 1, 3, 2)) / math.sqrt(query .size(-1)) scores = scores.masked_fill(mask == 0, -1000000000.0) weights = F.softmax(scores, dim=-1) weights = self.dropout(weights) context = torch.matmul(weights, value) context = context.permute(0, 2, 1, 3).contiguous().view(context. shape[0], -1, self.heads * self.d_k) interacted = self.concat(context) return interacted class FeedForward(nn.Module): def __init__(self, d_model, middle_dim=2048): super(FeedForward, self).__init__() self.fc1 = nn.Linear(d_model, middle_dim) self.fc2 = nn.Linear(middle_dim, d_model) self.dropout = nn.Dropout(0.1) def forward(self, x): out = F.relu(self.fc1(x)) out = self.fc2(self.dropout(out)) return out class EncoderLayerNew(nn.Module): def __init__(self, d_model, heads): super(EncoderLayerNew, self).__init__() self.layernorm = nn.LayerNorm(d_model) self.self_multihead = MultiHeadAttention(heads, d_model) self.feed_forward = FeedForward(d_model) self.dropout = nn.Dropout(0.1) def forward(self, input_0, input_1): primals_2 = self.layernorm.weight primals_5 = self.layernorm.bias primals_1 = self.self_multihead.query.weight primals_7 = self.self_multihead.query.bias primals_4 = self.self_multihead.key.weight primals_10 = self.self_multihead.key.bias primals_6 = self.self_multihead.value.weight primals_11 = self.self_multihead.value.bias primals_9 = self.self_multihead.concat.weight primals_12 = self.self_multihead.concat.bias primals_13 = self.feed_forward.fc1.weight primals_14 = self.feed_forward.fc1.bias primals_15 = self.feed_forward.fc2.weight primals_16 = self.feed_forward.fc2.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, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16]) return output[0]
sd2001/seqModeling
EncoderLayer
false
12,969
[ "MIT" ]
0
393f680de711ea8477e5450633b492298d253368
https://github.com/sd2001/seqModeling/tree/393f680de711ea8477e5450633b492298d253368
WassersteinDiscriminatorLoss
import torch import torch.nn as nn def reduce(x, reduction=None): """Applies reduction on a torch.Tensor. Args: x (torch.Tensor): The tensor on which reduction is to be applied. reduction (str, optional): The reduction to be applied. If ``mean`` the mean value of the Tensor is returned. If ``sum`` the elements of the Tensor will be summed. If none of the above then the Tensor is returning without any change. Returns: As per the above ``reduction`` convention. """ if reduction == 'mean': return torch.mean(x) elif reduction == 'sum': return torch.sum(x) else: return x def wasserstein_discriminator_loss(fx, fgz, reduction='mean'): return reduce(fgz - fx, reduction) class DiscriminatorLoss(nn.Module): """Base class for all discriminator losses. .. note:: All Losses meant to be minimized for optimizing the Discriminator must subclass this. Args: reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the outputs are averaged over batch size. If ``sum`` the elements of the output are summed. override_train_ops (function, optional): Function to be used in place of the default ``train_ops`` """ def __init__(self, reduction='mean', override_train_ops=None): super(DiscriminatorLoss, self).__init__() self.reduction = reduction self.override_train_ops = override_train_ops self.arg_map = {} def set_arg_map(self, value): """Updates the ``arg_map`` for passing a different value to the ``train_ops``. Args: value (dict): A mapping of the ``argument name`` in the method signature and the variable name in the ``Trainer`` it corresponds to. .. note:: If the ``train_ops`` signature is ``train_ops(self, gen, disc, optimizer_discriminator, device, batch_size, labels=None)`` then we need to map ``gen`` to ``generator`` and ``disc`` to ``discriminator``. In this case we make the following function call ``loss.set_arg_map({"gen": "generator", "disc": "discriminator"})``. """ self.arg_map.update(value) def train_ops(self, generator, discriminator, optimizer_discriminator, real_inputs, device, labels=None): """Defines the standard ``train_ops`` used by most losses. Losses which have a different training procedure can either ``subclass`` it **(recommended approach)** or make use of ``override_train_ops`` argument. The ``standard optimization algorithm`` for the ``discriminator`` defined in this train_ops is as follows: 1. :math:`fake = generator(noise)` 2. :math:`value_1 = discriminator(fake)` 3. :math:`value_2 = discriminator(real)` 4. :math:`loss = loss\\_function(value_1, value_2)` 5. Backpropagate by computing :math:`\\nabla loss` 6. Run a step of the optimizer for discriminator Args: generator (torchgan.models.Generator): The model to be optimized. discriminator (torchgan.models.Discriminator): The discriminator which judges the performance of the generator. optimizer_discriminator (torch.optim.Optimizer): Optimizer which updates the ``parameters`` of the ``discriminator``. real_inputs (torch.Tensor): The real data to be fed to the ``discriminator``. device (torch.device): Device on which the ``generator`` and ``discriminator`` is present. batch_size (int): Batch Size of the data infered from the ``DataLoader`` by the ``Trainer``. labels (torch.Tensor, optional): Labels for the data. Returns: Scalar value of the loss. """ if self.override_train_ops is not None: return self.override_train_ops(self, generator, discriminator, optimizer_discriminator, real_inputs, device, labels) else: if labels is None and (generator.label_type == 'required' or discriminator.label_type == 'required'): raise Exception('GAN model requires labels for training') batch_size = real_inputs.size(0) noise = torch.randn(batch_size, generator.encoding_dims, device =device) if generator.label_type == 'generated': label_gen = torch.randint(0, generator.num_classes, ( batch_size,), device=device) optimizer_discriminator.zero_grad() if discriminator.label_type == 'none': dx = discriminator(real_inputs) elif discriminator.label_type == 'required': dx = discriminator(real_inputs, labels) else: dx = discriminator(real_inputs, label_gen) if generator.label_type == 'none': fake = generator(noise) elif generator.label_type == 'required': fake = generator(noise, labels) else: fake = generator(noise, label_gen) if discriminator.label_type == 'none': dgz = discriminator(fake.detach()) elif generator.label_type == 'generated': dgz = discriminator(fake.detach(), label_gen) else: dgz = discriminator(fake.detach(), labels) loss = self.forward(dx, dgz) loss.backward() optimizer_discriminator.step() return loss.item() class WassersteinDiscriminatorLoss(DiscriminatorLoss): """Wasserstein GAN generator loss from `"Wasserstein GAN by Arjovsky et. al." <https://arxiv.org/abs/1701.07875>`_ paper The loss can be described as: .. math:: L(D) = f(G(z)) - f(x) where - :math:`G` : Generator - :math:`f` : Critic/Discriminator - :math:`x` : A sample from the data distribution - :math:`z` : A sample from the noise prior Args: reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the mean of the output. If ``sum`` the elements of the output will be summed. clip (tuple, optional): Tuple that specifies the maximum and minimum parameter clamping to be applied, as per the original version of the Wasserstein loss without Gradient Penalty. override_train_ops (function, optional): A function is passed to this argument, if the default ``train_ops`` is not to be used. """ def __init__(self, reduction='mean', clip=None, override_train_ops=None): super(WassersteinDiscriminatorLoss, self).__init__(reduction, override_train_ops) if (isinstance(clip, tuple) or isinstance(clip, list)) and len(clip ) > 1: self.clip = clip else: self.clip = None def forward(self, fx, fgz): """Computes the loss for the given input. Args: fx (torch.Tensor) : Output of the Discriminator with real data. It must have the dimensions (N, \\*) where \\* means any number of additional dimensions. fgz (torch.Tensor) : Output of the Discriminator with generated data. It must have the dimensions (N, \\*) where \\* means any number of additional dimensions. Returns: scalar if reduction is applied else Tensor with dimensions (N, \\*). """ return wasserstein_discriminator_loss(fx, fgz, self.reduction) def train_ops(self, generator, discriminator, optimizer_discriminator, real_inputs, device, labels=None): """Defines the standard ``train_ops`` used by wasserstein discriminator loss. The ``standard optimization algorithm`` for the ``discriminator`` defined in this train_ops is as follows: 1. Clamp the discriminator parameters to satisfy :math:`lipschitz\\ condition` 2. :math:`fake = generator(noise)` 3. :math:`value_1 = discriminator(fake)` 4. :math:`value_2 = discriminator(real)` 5. :math:`loss = loss\\_function(value_1, value_2)` 6. Backpropagate by computing :math:`\\nabla loss` 7. Run a step of the optimizer for discriminator Args: generator (torchgan.models.Generator): The model to be optimized. discriminator (torchgan.models.Discriminator): The discriminator which judges the performance of the generator. optimizer_discriminator (torch.optim.Optimizer): Optimizer which updates the ``parameters`` of the ``discriminator``. real_inputs (torch.Tensor): The real data to be fed to the ``discriminator``. device (torch.device): Device on which the ``generator`` and ``discriminator`` is present. labels (torch.Tensor, optional): Labels for the data. Returns: Scalar value of the loss. """ if self.override_train_ops is not None: return self.override_train_ops(generator, discriminator, optimizer_discriminator, real_inputs, device, labels) else: if self.clip is not None: for p in discriminator.parameters(): p.data.clamp_(self.clip[0], self.clip[1]) return super(WassersteinDiscriminatorLoss, self).train_ops( generator, discriminator, optimizer_discriminator, real_inputs, device, labels) 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_mean_sub_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.load(in_ptr1 + r0, None) tmp2 = tmp0 - tmp1 tmp3 = tl.broadcast_to(tmp2, [RBLOCK]) tmp5 = triton_helpers.promote_to_tensor(tl.sum(tmp3, 0)) tmp6 = 256.0 tmp7 = tmp5 / tmp6 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp7, None) def call(args): arg0_1, arg1_1 = 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_mean_sub_0[grid(1)](buf1, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, def reduce(x, reduction=None): """Applies reduction on a torch.Tensor. Args: x (torch.Tensor): The tensor on which reduction is to be applied. reduction (str, optional): The reduction to be applied. If ``mean`` the mean value of the Tensor is returned. If ``sum`` the elements of the Tensor will be summed. If none of the above then the Tensor is returning without any change. Returns: As per the above ``reduction`` convention. """ if reduction == 'mean': return torch.mean(x) elif reduction == 'sum': return torch.sum(x) else: return x def wasserstein_discriminator_loss(fx, fgz, reduction='mean'): return reduce(fgz - fx, reduction) class DiscriminatorLoss(nn.Module): """Base class for all discriminator losses. .. note:: All Losses meant to be minimized for optimizing the Discriminator must subclass this. Args: reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the outputs are averaged over batch size. If ``sum`` the elements of the output are summed. override_train_ops (function, optional): Function to be used in place of the default ``train_ops`` """ def __init__(self, reduction='mean', override_train_ops=None): super(DiscriminatorLoss, self).__init__() self.reduction = reduction self.override_train_ops = override_train_ops self.arg_map = {} def set_arg_map(self, value): """Updates the ``arg_map`` for passing a different value to the ``train_ops``. Args: value (dict): A mapping of the ``argument name`` in the method signature and the variable name in the ``Trainer`` it corresponds to. .. note:: If the ``train_ops`` signature is ``train_ops(self, gen, disc, optimizer_discriminator, device, batch_size, labels=None)`` then we need to map ``gen`` to ``generator`` and ``disc`` to ``discriminator``. In this case we make the following function call ``loss.set_arg_map({"gen": "generator", "disc": "discriminator"})``. """ self.arg_map.update(value) def train_ops(self, generator, discriminator, optimizer_discriminator, real_inputs, device, labels=None): """Defines the standard ``train_ops`` used by most losses. Losses which have a different training procedure can either ``subclass`` it **(recommended approach)** or make use of ``override_train_ops`` argument. The ``standard optimization algorithm`` for the ``discriminator`` defined in this train_ops is as follows: 1. :math:`fake = generator(noise)` 2. :math:`value_1 = discriminator(fake)` 3. :math:`value_2 = discriminator(real)` 4. :math:`loss = loss\\_function(value_1, value_2)` 5. Backpropagate by computing :math:`\\nabla loss` 6. Run a step of the optimizer for discriminator Args: generator (torchgan.models.Generator): The model to be optimized. discriminator (torchgan.models.Discriminator): The discriminator which judges the performance of the generator. optimizer_discriminator (torch.optim.Optimizer): Optimizer which updates the ``parameters`` of the ``discriminator``. real_inputs (torch.Tensor): The real data to be fed to the ``discriminator``. device (torch.device): Device on which the ``generator`` and ``discriminator`` is present. batch_size (int): Batch Size of the data infered from the ``DataLoader`` by the ``Trainer``. labels (torch.Tensor, optional): Labels for the data. Returns: Scalar value of the loss. """ if self.override_train_ops is not None: return self.override_train_ops(self, generator, discriminator, optimizer_discriminator, real_inputs, device, labels) else: if labels is None and (generator.label_type == 'required' or discriminator.label_type == 'required'): raise Exception('GAN model requires labels for training') batch_size = real_inputs.size(0) noise = torch.randn(batch_size, generator.encoding_dims, device =device) if generator.label_type == 'generated': label_gen = torch.randint(0, generator.num_classes, ( batch_size,), device=device) optimizer_discriminator.zero_grad() if discriminator.label_type == 'none': dx = discriminator(real_inputs) elif discriminator.label_type == 'required': dx = discriminator(real_inputs, labels) else: dx = discriminator(real_inputs, label_gen) if generator.label_type == 'none': fake = generator(noise) elif generator.label_type == 'required': fake = generator(noise, labels) else: fake = generator(noise, label_gen) if discriminator.label_type == 'none': dgz = discriminator(fake.detach()) elif generator.label_type == 'generated': dgz = discriminator(fake.detach(), label_gen) else: dgz = discriminator(fake.detach(), labels) loss = self.forward(dx, dgz) loss.backward() optimizer_discriminator.step() return loss.item() class WassersteinDiscriminatorLossNew(DiscriminatorLoss): """Wasserstein GAN generator loss from `"Wasserstein GAN by Arjovsky et. al." <https://arxiv.org/abs/1701.07875>`_ paper The loss can be described as: .. math:: L(D) = f(G(z)) - f(x) where - :math:`G` : Generator - :math:`f` : Critic/Discriminator - :math:`x` : A sample from the data distribution - :math:`z` : A sample from the noise prior Args: reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the mean of the output. If ``sum`` the elements of the output will be summed. clip (tuple, optional): Tuple that specifies the maximum and minimum parameter clamping to be applied, as per the original version of the Wasserstein loss without Gradient Penalty. override_train_ops (function, optional): A function is passed to this argument, if the default ``train_ops`` is not to be used. """ def __init__(self, reduction='mean', clip=None, override_train_ops=None): super(WassersteinDiscriminatorLossNew, self).__init__(reduction, override_train_ops) if (isinstance(clip, tuple) or isinstance(clip, list)) and len(clip ) > 1: self.clip = clip else: self.clip = None def train_ops(self, generator, discriminator, optimizer_discriminator, real_inputs, device, labels=None): """Defines the standard ``train_ops`` used by wasserstein discriminator loss. The ``standard optimization algorithm`` for the ``discriminator`` defined in this train_ops is as follows: 1. Clamp the discriminator parameters to satisfy :math:`lipschitz\\ condition` 2. :math:`fake = generator(noise)` 3. :math:`value_1 = discriminator(fake)` 4. :math:`value_2 = discriminator(real)` 5. :math:`loss = loss\\_function(value_1, value_2)` 6. Backpropagate by computing :math:`\\nabla loss` 7. Run a step of the optimizer for discriminator Args: generator (torchgan.models.Generator): The model to be optimized. discriminator (torchgan.models.Discriminator): The discriminator which judges the performance of the generator. optimizer_discriminator (torch.optim.Optimizer): Optimizer which updates the ``parameters`` of the ``discriminator``. real_inputs (torch.Tensor): The real data to be fed to the ``discriminator``. device (torch.device): Device on which the ``generator`` and ``discriminator`` is present. labels (torch.Tensor, optional): Labels for the data. Returns: Scalar value of the loss. """ if self.override_train_ops is not None: return self.override_train_ops(generator, discriminator, optimizer_discriminator, real_inputs, device, labels) else: if self.clip is not None: for p in discriminator.parameters(): p.data.clamp_(self.clip[0], self.clip[1]) return super(WassersteinDiscriminatorLossNew, self).train_ops( generator, discriminator, optimizer_discriminator, real_inputs, device, labels) def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
shi-weili/torchgan
WassersteinDiscriminatorLoss
false
12,970
[ "MIT" ]
0
28ffd4026b8c0db2217b667d30a222d6758bfc41
https://github.com/shi-weili/torchgan/tree/28ffd4026b8c0db2217b667d30a222d6758bfc41
_BoundaryRefineModule
import torch from torch import nn class _BoundaryRefineModule(nn.Module): def __init__(self, dim): super(_BoundaryRefineModule, self).__init__() self.relu = nn.ReLU(inplace=True) self.conv1 = nn.Conv2d(dim, dim, kernel_size=3, padding=1) self.conv2 = nn.Conv2d(dim, dim, kernel_size=3, padding=1) def forward(self, x): residual = self.conv1(x) residual = self.relu(residual) residual = self.conv2(residual) out = x + residual return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride @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_add_convolution_1(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_out_ptr0 + x3, xmask) tmp2 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp4 = tmp0 + tmp3 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, 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,)) 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=256, 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_add_convolution_1[grid(256)](buf3, primals_3, primals_5, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 return buf3, primals_1, primals_3, primals_4, buf1 class _BoundaryRefineModuleNew(nn.Module): def __init__(self, dim): super(_BoundaryRefineModuleNew, self).__init__() self.relu = nn.ReLU(inplace=True) self.conv1 = nn.Conv2d(dim, dim, kernel_size=3, padding=1) self.conv2 = nn.Conv2d(dim, dim, kernel_size=3, padding=1) def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
sharanry/pytorch-semantic-segmentation
_BoundaryRefineModule
false
12,971
[ "MIT" ]
0
47d637e3d5fcc1e2569203306c2fa5dca6f0e68a
https://github.com/sharanry/pytorch-semantic-segmentation/tree/47d637e3d5fcc1e2569203306c2fa5dca6f0e68a
MinimaxDiscriminatorLoss
import torch import torch.nn as nn import torch.nn.functional as F def minimax_discriminator_loss(dx, dgz, label_smoothing=0.0, reduction='mean'): target_ones = torch.ones_like(dgz) * (1.0 - label_smoothing) target_zeros = torch.zeros_like(dx) loss = F.binary_cross_entropy_with_logits(dx, target_ones, reduction= reduction) loss += F.binary_cross_entropy_with_logits(dgz, target_zeros, reduction =reduction) return loss class DiscriminatorLoss(nn.Module): """Base class for all discriminator losses. .. note:: All Losses meant to be minimized for optimizing the Discriminator must subclass this. Args: reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the outputs are averaged over batch size. If ``sum`` the elements of the output are summed. override_train_ops (function, optional): Function to be used in place of the default ``train_ops`` """ def __init__(self, reduction='mean', override_train_ops=None): super(DiscriminatorLoss, self).__init__() self.reduction = reduction self.override_train_ops = override_train_ops self.arg_map = {} def set_arg_map(self, value): """Updates the ``arg_map`` for passing a different value to the ``train_ops``. Args: value (dict): A mapping of the ``argument name`` in the method signature and the variable name in the ``Trainer`` it corresponds to. .. note:: If the ``train_ops`` signature is ``train_ops(self, gen, disc, optimizer_discriminator, device, batch_size, labels=None)`` then we need to map ``gen`` to ``generator`` and ``disc`` to ``discriminator``. In this case we make the following function call ``loss.set_arg_map({"gen": "generator", "disc": "discriminator"})``. """ self.arg_map.update(value) def train_ops(self, generator, discriminator, optimizer_discriminator, real_inputs, device, labels=None): """Defines the standard ``train_ops`` used by most losses. Losses which have a different training procedure can either ``subclass`` it **(recommended approach)** or make use of ``override_train_ops`` argument. The ``standard optimization algorithm`` for the ``discriminator`` defined in this train_ops is as follows: 1. :math:`fake = generator(noise)` 2. :math:`value_1 = discriminator(fake)` 3. :math:`value_2 = discriminator(real)` 4. :math:`loss = loss\\_function(value_1, value_2)` 5. Backpropagate by computing :math:`\\nabla loss` 6. Run a step of the optimizer for discriminator Args: generator (torchgan.models.Generator): The model to be optimized. discriminator (torchgan.models.Discriminator): The discriminator which judges the performance of the generator. optimizer_discriminator (torch.optim.Optimizer): Optimizer which updates the ``parameters`` of the ``discriminator``. real_inputs (torch.Tensor): The real data to be fed to the ``discriminator``. device (torch.device): Device on which the ``generator`` and ``discriminator`` is present. batch_size (int): Batch Size of the data infered from the ``DataLoader`` by the ``Trainer``. labels (torch.Tensor, optional): Labels for the data. Returns: Scalar value of the loss. """ if self.override_train_ops is not None: return self.override_train_ops(self, generator, discriminator, optimizer_discriminator, real_inputs, device, labels) else: if labels is None and (generator.label_type == 'required' or discriminator.label_type == 'required'): raise Exception('GAN model requires labels for training') batch_size = real_inputs.size(0) noise = torch.randn(batch_size, generator.encoding_dims, device =device) if generator.label_type == 'generated': label_gen = torch.randint(0, generator.num_classes, ( batch_size,), device=device) optimizer_discriminator.zero_grad() if discriminator.label_type == 'none': dx = discriminator(real_inputs) elif discriminator.label_type == 'required': dx = discriminator(real_inputs, labels) else: dx = discriminator(real_inputs, label_gen) if generator.label_type == 'none': fake = generator(noise) elif generator.label_type == 'required': fake = generator(noise, labels) else: fake = generator(noise, label_gen) if discriminator.label_type == 'none': dgz = discriminator(fake.detach()) elif generator.label_type == 'generated': dgz = discriminator(fake.detach(), label_gen) else: dgz = discriminator(fake.detach(), labels) loss = self.forward(dx, dgz) loss.backward() optimizer_discriminator.step() return loss.item() class MinimaxDiscriminatorLoss(DiscriminatorLoss): """Minimax game discriminator loss from the original GAN paper `"Generative Adversarial Networks by Goodfellow et. al." <https://arxiv.org/abs/1406.2661>`_ The loss can be described as: .. math:: L(D) = -[log(D(x)) + log(1 - D(G(z)))] where - :math:`G` : Generator - :math:`D` : Discriminator - :math:`x` : A sample from the data distribution - :math:`z` : A sample from the noise prior Args: label_smoothing (float, optional): The factor by which the labels (1 in this case) needs to be smoothened. For example, label_smoothing = 0.2 changes the value of the real labels to 0.8. reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the mean of the output. If ``sum`` the elements of the output will be summed. override_train_ops (function, optional): A function is passed to this argument, if the default ``train_ops`` is not to be used. """ def __init__(self, label_smoothing=0.0, reduction='mean', override_train_ops=None): super(MinimaxDiscriminatorLoss, self).__init__(reduction, override_train_ops) self.label_smoothing = label_smoothing def forward(self, dx, dgz): """Computes the loss for the given input. Args: dx (torch.Tensor) : Output of the Discriminator with real data. It must have the dimensions (N, \\*) where \\* means any number of additional dimensions. dgz (torch.Tensor) : Output of the Discriminator with generated data. It must have the dimensions (N, \\*) where \\* means any number of additional dimensions. Returns: scalar if reduction is applied else Tensor with dimensions (N, \\*). """ return minimax_discriminator_loss(dx, dgz, label_smoothing=self. label_smoothing, reduction=self.reduction) 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_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) tmp13 = tl.load(in_ptr1 + r0, None) tmp1 = 0.0 tmp2 = tmp1 * tmp0 tmp3 = triton_helpers.minimum(tmp1, tmp0) tmp4 = tl_math.abs(tmp0) tmp5 = -tmp4 tmp6 = tl_math.exp(tmp5) tmp7 = libdevice.log1p(tmp6) tmp8 = tmp3 - tmp7 tmp9 = tmp2 - tmp8 tmp10 = tl.broadcast_to(tmp9, [RBLOCK]) tmp12 = triton_helpers.promote_to_tensor(tl.sum(tmp10, 0)) tmp14 = triton_helpers.minimum(tmp1, tmp13) tmp15 = tl_math.abs(tmp13) tmp16 = -tmp15 tmp17 = tl_math.exp(tmp16) tmp18 = libdevice.log1p(tmp17) tmp19 = tmp14 - tmp18 tmp20 = tmp13 - tmp19 tmp21 = tl.broadcast_to(tmp20, [RBLOCK]) tmp23 = triton_helpers.promote_to_tensor(tl.sum(tmp21, 0)) tmp24 = 256.0 tmp25 = tmp12 / tmp24 tmp26 = tmp23 / tmp24 tmp27 = tmp25 + tmp26 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp27, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf2 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_binary_cross_entropy_with_logits_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 minimax_discriminator_loss(dx, dgz, label_smoothing=0.0, reduction='mean'): target_ones = torch.ones_like(dgz) * (1.0 - label_smoothing) target_zeros = torch.zeros_like(dx) loss = F.binary_cross_entropy_with_logits(dx, target_ones, reduction= reduction) loss += F.binary_cross_entropy_with_logits(dgz, target_zeros, reduction =reduction) return loss class DiscriminatorLoss(nn.Module): """Base class for all discriminator losses. .. note:: All Losses meant to be minimized for optimizing the Discriminator must subclass this. Args: reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the outputs are averaged over batch size. If ``sum`` the elements of the output are summed. override_train_ops (function, optional): Function to be used in place of the default ``train_ops`` """ def __init__(self, reduction='mean', override_train_ops=None): super(DiscriminatorLoss, self).__init__() self.reduction = reduction self.override_train_ops = override_train_ops self.arg_map = {} def set_arg_map(self, value): """Updates the ``arg_map`` for passing a different value to the ``train_ops``. Args: value (dict): A mapping of the ``argument name`` in the method signature and the variable name in the ``Trainer`` it corresponds to. .. note:: If the ``train_ops`` signature is ``train_ops(self, gen, disc, optimizer_discriminator, device, batch_size, labels=None)`` then we need to map ``gen`` to ``generator`` and ``disc`` to ``discriminator``. In this case we make the following function call ``loss.set_arg_map({"gen": "generator", "disc": "discriminator"})``. """ self.arg_map.update(value) def train_ops(self, generator, discriminator, optimizer_discriminator, real_inputs, device, labels=None): """Defines the standard ``train_ops`` used by most losses. Losses which have a different training procedure can either ``subclass`` it **(recommended approach)** or make use of ``override_train_ops`` argument. The ``standard optimization algorithm`` for the ``discriminator`` defined in this train_ops is as follows: 1. :math:`fake = generator(noise)` 2. :math:`value_1 = discriminator(fake)` 3. :math:`value_2 = discriminator(real)` 4. :math:`loss = loss\\_function(value_1, value_2)` 5. Backpropagate by computing :math:`\\nabla loss` 6. Run a step of the optimizer for discriminator Args: generator (torchgan.models.Generator): The model to be optimized. discriminator (torchgan.models.Discriminator): The discriminator which judges the performance of the generator. optimizer_discriminator (torch.optim.Optimizer): Optimizer which updates the ``parameters`` of the ``discriminator``. real_inputs (torch.Tensor): The real data to be fed to the ``discriminator``. device (torch.device): Device on which the ``generator`` and ``discriminator`` is present. batch_size (int): Batch Size of the data infered from the ``DataLoader`` by the ``Trainer``. labels (torch.Tensor, optional): Labels for the data. Returns: Scalar value of the loss. """ if self.override_train_ops is not None: return self.override_train_ops(self, generator, discriminator, optimizer_discriminator, real_inputs, device, labels) else: if labels is None and (generator.label_type == 'required' or discriminator.label_type == 'required'): raise Exception('GAN model requires labels for training') batch_size = real_inputs.size(0) noise = torch.randn(batch_size, generator.encoding_dims, device =device) if generator.label_type == 'generated': label_gen = torch.randint(0, generator.num_classes, ( batch_size,), device=device) optimizer_discriminator.zero_grad() if discriminator.label_type == 'none': dx = discriminator(real_inputs) elif discriminator.label_type == 'required': dx = discriminator(real_inputs, labels) else: dx = discriminator(real_inputs, label_gen) if generator.label_type == 'none': fake = generator(noise) elif generator.label_type == 'required': fake = generator(noise, labels) else: fake = generator(noise, label_gen) if discriminator.label_type == 'none': dgz = discriminator(fake.detach()) elif generator.label_type == 'generated': dgz = discriminator(fake.detach(), label_gen) else: dgz = discriminator(fake.detach(), labels) loss = self.forward(dx, dgz) loss.backward() optimizer_discriminator.step() return loss.item() class MinimaxDiscriminatorLossNew(DiscriminatorLoss): """Minimax game discriminator loss from the original GAN paper `"Generative Adversarial Networks by Goodfellow et. al." <https://arxiv.org/abs/1406.2661>`_ The loss can be described as: .. math:: L(D) = -[log(D(x)) + log(1 - D(G(z)))] where - :math:`G` : Generator - :math:`D` : Discriminator - :math:`x` : A sample from the data distribution - :math:`z` : A sample from the noise prior Args: label_smoothing (float, optional): The factor by which the labels (1 in this case) needs to be smoothened. For example, label_smoothing = 0.2 changes the value of the real labels to 0.8. reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the mean of the output. If ``sum`` the elements of the output will be summed. override_train_ops (function, optional): A function is passed to this argument, if the default ``train_ops`` is not to be used. """ def __init__(self, label_smoothing=0.0, reduction='mean', override_train_ops=None): super(MinimaxDiscriminatorLossNew, self).__init__(reduction, override_train_ops) self.label_smoothing = label_smoothing def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
shi-weili/torchgan
MinimaxDiscriminatorLoss
false
12,972
[ "MIT" ]
0
28ffd4026b8c0db2217b667d30a222d6758bfc41
https://github.com/shi-weili/torchgan/tree/28ffd4026b8c0db2217b667d30a222d6758bfc41
WassersteinGeneratorLoss
import torch import torch.nn as nn def reduce(x, reduction=None): """Applies reduction on a torch.Tensor. Args: x (torch.Tensor): The tensor on which reduction is to be applied. reduction (str, optional): The reduction to be applied. If ``mean`` the mean value of the Tensor is returned. If ``sum`` the elements of the Tensor will be summed. If none of the above then the Tensor is returning without any change. Returns: As per the above ``reduction`` convention. """ if reduction == 'mean': return torch.mean(x) elif reduction == 'sum': return torch.sum(x) else: return x def wasserstein_generator_loss(fgz, reduction='mean'): return reduce(-1.0 * fgz, reduction) class GeneratorLoss(nn.Module): """Base class for all generator losses. .. note:: All Losses meant to be minimized for optimizing the Generator must subclass this. Args: reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the outputs are averaged over batch size. If ``sum`` the elements of the output are summed. override_train_ops (function, optional): Function to be used in place of the default ``train_ops`` """ def __init__(self, reduction='mean', override_train_ops=None): super(GeneratorLoss, self).__init__() self.reduction = reduction self.override_train_ops = override_train_ops self.arg_map = {} def set_arg_map(self, value): """Updates the ``arg_map`` for passing a different value to the ``train_ops``. Args: value (dict): A mapping of the ``argument name`` in the method signature and the variable name in the ``Trainer`` it corresponds to. .. note:: If the ``train_ops`` signature is ``train_ops(self, gen, disc, optimizer_generator, device, batch_size, labels=None)`` then we need to map ``gen`` to ``generator`` and ``disc`` to ``discriminator``. In this case we make the following function call ``loss.set_arg_map({"gen": "generator", "disc": "discriminator"})``. """ self.arg_map.update(value) def train_ops(self, generator, discriminator, optimizer_generator, device, batch_size, labels=None): """Defines the standard ``train_ops`` used by most losses. Losses which have a different training procedure can either ``subclass`` it **(recommended approach)** or make use of ``override_train_ops`` argument. The ``standard optimization algorithm`` for the ``generator`` defined in this train_ops is as follows: 1. :math:`fake = generator(noise)` 2. :math:`value = discriminator(fake)` 3. :math:`loss = loss\\_function(value)` 4. Backpropagate by computing :math:`\\nabla loss` 5. Run a step of the optimizer for generator Args: generator (torchgan.models.Generator): The model to be optimized. discriminator (torchgan.models.Discriminator): The discriminator which judges the performance of the generator. optimizer_generator (torch.optim.Optimizer): Optimizer which updates the ``parameters`` of the ``generator``. device (torch.device): Device on which the ``generator`` and ``discriminator`` is present. batch_size (int): Batch Size of the data infered from the ``DataLoader`` by the ``Trainer``. labels (torch.Tensor, optional): Labels for the data. Returns: Scalar value of the loss. """ if self.override_train_ops is not None: return self.override_train_ops(generator, discriminator, optimizer_generator, device, batch_size, labels) else: if labels is None and generator.label_type == 'required': raise Exception('GAN model requires labels for training') noise = torch.randn(batch_size, generator.encoding_dims, device =device) optimizer_generator.zero_grad() if generator.label_type == 'generated': label_gen = torch.randint(0, generator.num_classes, ( batch_size,), device=device) if generator.label_type == 'none': fake = generator(noise) elif generator.label_type == 'required': fake = generator(noise, labels) elif generator.label_type == 'generated': fake = generator(noise, label_gen) if discriminator.label_type == 'none': dgz = discriminator(fake) elif generator.label_type == 'generated': dgz = discriminator(fake, label_gen) else: dgz = discriminator(fake, labels) loss = self.forward(dgz) loss.backward() optimizer_generator.step() return loss.item() class WassersteinGeneratorLoss(GeneratorLoss): """Wasserstein GAN generator loss from `"Wasserstein GAN by Arjovsky et. al." <https://arxiv.org/abs/1701.07875>`_ paper The loss can be described as: .. math:: L(G) = -f(G(z)) where - :math:`G` : Generator - :math:`f` : Critic/Discriminator - :math:`z` : A sample from the noise prior Args: reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the mean of the output. If ``sum`` the elements of the output will be summed. override_train_ops (function, optional): A function is passed to this argument, if the default ``train_ops`` is not to be used. """ def forward(self, fgz): """Computes the loss for the given input. Args: dgz (torch.Tensor) : Output of the Discriminator with generated data. It must have the dimensions (N, \\*) where \\* means any number of additional dimensions. Returns: scalar if reduction is applied else Tensor with dimensions (N, \\*). """ return wasserstein_generator_loss(fgz, self.reduction) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_mean_mul_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 = -1.0 tmp2 = tmp0 * tmp1 tmp3 = tl.broadcast_to(tmp2, [RBLOCK]) tmp5 = triton_helpers.promote_to_tensor(tl.sum(tmp3, 0)) tmp6 = 256.0 tmp7 = tmp5 / tmp6 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp7, None) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_mean_mul_0[grid(1)](buf1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 return buf1, def reduce(x, reduction=None): """Applies reduction on a torch.Tensor. Args: x (torch.Tensor): The tensor on which reduction is to be applied. reduction (str, optional): The reduction to be applied. If ``mean`` the mean value of the Tensor is returned. If ``sum`` the elements of the Tensor will be summed. If none of the above then the Tensor is returning without any change. Returns: As per the above ``reduction`` convention. """ if reduction == 'mean': return torch.mean(x) elif reduction == 'sum': return torch.sum(x) else: return x def wasserstein_generator_loss(fgz, reduction='mean'): return reduce(-1.0 * fgz, reduction) class GeneratorLoss(nn.Module): """Base class for all generator losses. .. note:: All Losses meant to be minimized for optimizing the Generator must subclass this. Args: reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the outputs are averaged over batch size. If ``sum`` the elements of the output are summed. override_train_ops (function, optional): Function to be used in place of the default ``train_ops`` """ def __init__(self, reduction='mean', override_train_ops=None): super(GeneratorLoss, self).__init__() self.reduction = reduction self.override_train_ops = override_train_ops self.arg_map = {} def set_arg_map(self, value): """Updates the ``arg_map`` for passing a different value to the ``train_ops``. Args: value (dict): A mapping of the ``argument name`` in the method signature and the variable name in the ``Trainer`` it corresponds to. .. note:: If the ``train_ops`` signature is ``train_ops(self, gen, disc, optimizer_generator, device, batch_size, labels=None)`` then we need to map ``gen`` to ``generator`` and ``disc`` to ``discriminator``. In this case we make the following function call ``loss.set_arg_map({"gen": "generator", "disc": "discriminator"})``. """ self.arg_map.update(value) def train_ops(self, generator, discriminator, optimizer_generator, device, batch_size, labels=None): """Defines the standard ``train_ops`` used by most losses. Losses which have a different training procedure can either ``subclass`` it **(recommended approach)** or make use of ``override_train_ops`` argument. The ``standard optimization algorithm`` for the ``generator`` defined in this train_ops is as follows: 1. :math:`fake = generator(noise)` 2. :math:`value = discriminator(fake)` 3. :math:`loss = loss\\_function(value)` 4. Backpropagate by computing :math:`\\nabla loss` 5. Run a step of the optimizer for generator Args: generator (torchgan.models.Generator): The model to be optimized. discriminator (torchgan.models.Discriminator): The discriminator which judges the performance of the generator. optimizer_generator (torch.optim.Optimizer): Optimizer which updates the ``parameters`` of the ``generator``. device (torch.device): Device on which the ``generator`` and ``discriminator`` is present. batch_size (int): Batch Size of the data infered from the ``DataLoader`` by the ``Trainer``. labels (torch.Tensor, optional): Labels for the data. Returns: Scalar value of the loss. """ if self.override_train_ops is not None: return self.override_train_ops(generator, discriminator, optimizer_generator, device, batch_size, labels) else: if labels is None and generator.label_type == 'required': raise Exception('GAN model requires labels for training') noise = torch.randn(batch_size, generator.encoding_dims, device =device) optimizer_generator.zero_grad() if generator.label_type == 'generated': label_gen = torch.randint(0, generator.num_classes, ( batch_size,), device=device) if generator.label_type == 'none': fake = generator(noise) elif generator.label_type == 'required': fake = generator(noise, labels) elif generator.label_type == 'generated': fake = generator(noise, label_gen) if discriminator.label_type == 'none': dgz = discriminator(fake) elif generator.label_type == 'generated': dgz = discriminator(fake, label_gen) else: dgz = discriminator(fake, labels) loss = self.forward(dgz) loss.backward() optimizer_generator.step() return loss.item() class WassersteinGeneratorLossNew(GeneratorLoss): """Wasserstein GAN generator loss from `"Wasserstein GAN by Arjovsky et. al." <https://arxiv.org/abs/1701.07875>`_ paper The loss can be described as: .. math:: L(G) = -f(G(z)) where - :math:`G` : Generator - :math:`f` : Critic/Discriminator - :math:`z` : A sample from the noise prior Args: reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the mean of the output. If ``sum`` the elements of the output will be summed. override_train_ops (function, optional): A function is passed to this argument, if the default ``train_ops`` is not to be used. """ def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
shi-weili/torchgan
WassersteinGeneratorLoss
false
12,973
[ "MIT" ]
0
28ffd4026b8c0db2217b667d30a222d6758bfc41
https://github.com/shi-weili/torchgan/tree/28ffd4026b8c0db2217b667d30a222d6758bfc41
MinibatchDiscrimination1d
import torch import torch.nn as nn class MinibatchDiscrimination1d(nn.Module): """1D Minibatch Discrimination Module as proposed in the paper `"Improved Techniques for Training GANs by Salimans et. al." <https://arxiv.org/abs/1805.08318>`_ Allows the Discriminator to easily detect mode collapse by augmenting the activations to the succeeding layer with side information that allows it to determine the 'closeness' of the minibatch examples with each other .. math :: M_i = T * f(x_{i}) .. math :: c_b(x_{i}, x_{j}) = \\exp(-||M_{i, b} - M_{j, b}||_1) \\in \\mathbb{R}. .. math :: o(x_{i})_b &= \\sum_{j=1}^{n} c_b(x_{i},x_{j}) \\in \\mathbb{R} \\\\ .. math :: o(x_{i}) &= \\Big[ o(x_{i})_1, o(x_{i})_2, \\dots, o(x_{i})_B \\Big] \\in \\mathbb{R}^B \\\\ .. math :: o(X) \\in \\mathbb{R}^{n \\times B} This is followed by concatenating :math:`o(x_{i})` and :math:`f(x_{i})` where - :math:`f(x_{i}) \\in \\mathbb{R}^A` : Activations from an intermediate layer - :math:`f(x_{i}) \\in \\mathbb{R}^A` : Parameter Tensor for generating minibatch discrimination matrix Args: in_features (int): Features input corresponding to dimension :math:`A` out_features (int): Number of output features that are to be concatenated corresponding to dimension :math:`B` intermediate_features (int): Intermediate number of features corresponding to dimension :math:`C` Returns: A Tensor of size :math:`(N, in_features + out_features)` where :math:`N` is the batch size """ def __init__(self, in_features, out_features, intermediate_features=16): super(MinibatchDiscrimination1d, self).__init__() self.in_features = in_features self.out_features = out_features self.intermediate_features = intermediate_features self.T = nn.Parameter(torch.Tensor(in_features, out_features, intermediate_features)) nn.init.normal_(self.T) def forward(self, x): """Computes the output of the Minibatch Discrimination Layer Args: x (torch.Tensor): A Torch Tensor of dimensions :math: `(N, infeatures)` Returns: 3D Torch Tensor of size :math: `(N,infeatures + outfeatures)` after applying Minibatch Discrimination """ M = torch.mm(x, self.T.view(self.in_features, -1)) M = M.view(-1, self.out_features, self.intermediate_features ).unsqueeze(0) M_t = M.permute(1, 0, 2, 3) out = torch.sum(torch.exp(-torch.abs(M - M_t).sum(3)), dim=0) - 1 return torch.cat([x, out], 1) 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.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_per_fused_abs_exp_neg_sub_sum_0(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 64 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r3 = rindex x5 = xindex % 16 x0 = xindex % 4 x2 = xindex // 16 x4 = xindex tmp0 = tl.load(in_ptr0 + (r3 + 16 * x5), xmask, eviction_policy= 'evict_last', other=0.0) tmp1 = tl.load(in_ptr0 + (r3 + 16 * x0 + 64 * x2), xmask, eviction_policy='evict_last', other=0.0) tmp2 = tmp0 - tmp1 tmp3 = tl_math.abs(tmp2) tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = -tmp7 tmp9 = tl_math.exp(tmp8) tl.debug_barrier() tl.store(in_out_ptr0 + x4, tmp9, xmask) @triton.jit def triton_poi_fused_cat_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 8 x1 = xindex // 8 x2 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (4 * x1 + x0), tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp6 = 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.load(in_ptr1 + (16 + 4 * x1 + (-4 + x0)), tmp6 & xmask, eviction_policy='evict_last', other=0.0) tmp11 = tmp9 + tmp10 tmp12 = tl.load(in_ptr1 + (32 + 4 * x1 + (-4 + x0)), tmp6 & xmask, eviction_policy='evict_last', other=0.0) tmp13 = tmp11 + tmp12 tmp14 = tl.load(in_ptr1 + (48 + 4 * x1 + (-4 + x0)), tmp6 & xmask, eviction_policy='evict_last', other=0.0) tmp15 = tmp13 + tmp14 tmp16 = 1.0 tmp17 = tmp15 - tmp16 tmp18 = tl.full(tmp17.shape, 0.0, tmp17.dtype) tmp19 = tl.where(tmp6, tmp17, tmp18) tmp20 = tl.where(tmp4, tmp5, tmp19) tl.store(out_ptr0 + x2, tmp20, xmask) def call(args): primals_1, primals_2 = args args.clear() assert_size_stride(primals_1, (4, 4, 16), (64, 16, 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, 64), (64, 1), torch.float32) extern_kernels.mm(primals_2, reinterpret_tensor(primals_1, (4, 64), (64, 1), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) buf2 = buf1 del buf1 get_raw_stream(0) triton_per_fused_abs_exp_neg_sub_sum_0[grid(64)](buf2, buf0, 64, 16, XBLOCK=8, num_warps=2, num_stages=1) buf3 = empty_strided_cuda((4, 8), (8, 1), torch.float32) triton_poi_fused_cat_1[grid(32)](primals_2, buf2, buf3, 32, XBLOCK= 32, num_warps=1, num_stages=1) return buf3, buf0, buf2, reinterpret_tensor(primals_2, (4, 4), (1, 4), 0) class MinibatchDiscrimination1dNew(nn.Module): """1D Minibatch Discrimination Module as proposed in the paper `"Improved Techniques for Training GANs by Salimans et. al." <https://arxiv.org/abs/1805.08318>`_ Allows the Discriminator to easily detect mode collapse by augmenting the activations to the succeeding layer with side information that allows it to determine the 'closeness' of the minibatch examples with each other .. math :: M_i = T * f(x_{i}) .. math :: c_b(x_{i}, x_{j}) = \\exp(-||M_{i, b} - M_{j, b}||_1) \\in \\mathbb{R}. .. math :: o(x_{i})_b &= \\sum_{j=1}^{n} c_b(x_{i},x_{j}) \\in \\mathbb{R} \\\\ .. math :: o(x_{i}) &= \\Big[ o(x_{i})_1, o(x_{i})_2, \\dots, o(x_{i})_B \\Big] \\in \\mathbb{R}^B \\\\ .. math :: o(X) \\in \\mathbb{R}^{n \\times B} This is followed by concatenating :math:`o(x_{i})` and :math:`f(x_{i})` where - :math:`f(x_{i}) \\in \\mathbb{R}^A` : Activations from an intermediate layer - :math:`f(x_{i}) \\in \\mathbb{R}^A` : Parameter Tensor for generating minibatch discrimination matrix Args: in_features (int): Features input corresponding to dimension :math:`A` out_features (int): Number of output features that are to be concatenated corresponding to dimension :math:`B` intermediate_features (int): Intermediate number of features corresponding to dimension :math:`C` Returns: A Tensor of size :math:`(N, in_features + out_features)` where :math:`N` is the batch size """ def __init__(self, in_features, out_features, intermediate_features=16): super(MinibatchDiscrimination1dNew, self).__init__() self.in_features = in_features self.out_features = out_features self.intermediate_features = intermediate_features self.T = nn.Parameter(torch.Tensor(in_features, out_features, intermediate_features)) nn.init.normal_(self.T) def forward(self, input_0): primals_1 = self.T primals_2 = input_0 output = call([primals_1, primals_2]) return output[0]
shi-weili/torchgan
MinibatchDiscrimination1d
false
12,974
[ "MIT" ]
0
28ffd4026b8c0db2217b667d30a222d6758bfc41
https://github.com/shi-weili/torchgan/tree/28ffd4026b8c0db2217b667d30a222d6758bfc41
Swish
import torch import torch.nn as nn import torch.distributed class Swish(nn.Module): def __init__(self): super(Swish, self).__init__() self.beta = nn.Parameter(torch.tensor(1.0)) def forward(self, x): return x * torch.sigmoid(self.beta * x) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn import torch.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_mul_sigmoid_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr1 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp3 = tmp2 * tmp0 tmp4 = tl.sigmoid(tmp3) tmp5 = tmp0 * tmp4 tl.store(out_ptr0 + x0, tmp5, xmask) def call(args): primals_1, primals_2 = args args.clear() assert_size_stride(primals_1, (), ()) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_sigmoid_0[grid(256)](primals_2, primals_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) return buf0, primals_1, primals_2 class SwishNew(nn.Module): def __init__(self): super(SwishNew, self).__init__() self.beta = nn.Parameter(torch.tensor(1.0)) def forward(self, input_0): primals_1 = self.beta primals_2 = input_0 output = call([primals_1, primals_2]) return output[0]
shnhrtkyk/PointFlow
Swish
false
12,975
[ "MIT" ]
0
26b8fac79bf3e71533f5c8b12f90cf5f9a385a99
https://github.com/shnhrtkyk/PointFlow/tree/26b8fac79bf3e71533f5c8b12f90cf5f9a385a99
PerceptronTanh
import torch import torch.nn as nn from typing import Any import torch.nn.functional as F class PerceptronTanh(nn.Module): """Implements a 1-layer perceptron with Tanh activaton.""" def _forward_unimplemented(self, *input: Any) ->None: pass def __init__(self, input_dimension, hidden_dimension, output_dimension): super(PerceptronTanh, self).__init__() self._layer1 = nn.Linear(input_dimension, hidden_dimension) self._layer2 = nn.Linear(hidden_dimension, output_dimension, bias=False ) def forward(self, inp): return F.tanh(self._layer2(F.relu(self._layer1(inp)))) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_dimension': 4, 'hidden_dimension': 4, 'output_dimension': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn from typing import Any 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_tanh_1(in_out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = libdevice.tanh(tmp0) tl.store(in_out_ptr0 + x0, tmp1, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 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 buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(256)](buf1, primals_2, buf4, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf2 triton_poi_fused_tanh_1[grid(256)](buf3, 256, XBLOCK=128, num_warps =4, num_stages=1) return buf3, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 4), (4, 1), 0), buf3, primals_4, buf4 class PerceptronTanhNew(nn.Module): """Implements a 1-layer perceptron with Tanh activaton.""" def _forward_unimplemented(self, *input: Any) ->None: pass def __init__(self, input_dimension, hidden_dimension, output_dimension): super(PerceptronTanhNew, self).__init__() self._layer1 = nn.Linear(input_dimension, hidden_dimension) self._layer2 = nn.Linear(hidden_dimension, output_dimension, bias=False ) def forward(self, input_0): primals_1 = self._layer1.weight primals_2 = self._layer1.bias primals_4 = self._layer2.weight primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
shi27feng/PDP-Solver
PerceptronTanh
false
12,976
[ "MIT" ]
0
bf6e392f72f8a3572e0987313230943d94d53c95
https://github.com/shi27feng/PDP-Solver/tree/bf6e392f72f8a3572e0987313230943d94d53c95
Perceptron
import torch import torch.nn as nn from typing import Any import torch.nn.functional as fn class Perceptron(nn.Module): """Implements a 1-layer perceptron.""" def _forward_unimplemented(self, *input: Any) ->None: pass def __init__(self, input_dimension, hidden_dimension, output_dimension): super(Perceptron, self).__init__() self._layer1 = nn.Linear(input_dimension, hidden_dimension) self._layer2 = nn.Linear(hidden_dimension, output_dimension, bias=False ) def forward(self, inp): return fn.sigmoid(self._layer2(fn.relu(self._layer1(inp)))) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_dimension': 4, 'hidden_dimension': 4, 'output_dimension': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn from typing import Any 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_sigmoid_1(in_out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = tl.sigmoid(tmp0) tl.store(in_out_ptr0 + x0, tmp1, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 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 buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(256)](buf1, primals_2, buf4, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf2 triton_poi_fused_sigmoid_1[grid(256)](buf3, 256, XBLOCK=128, num_warps=4, num_stages=1) return buf3, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 4), (4, 1), 0), buf3, primals_4, buf4 class PerceptronNew(nn.Module): """Implements a 1-layer perceptron.""" def _forward_unimplemented(self, *input: Any) ->None: pass def __init__(self, input_dimension, hidden_dimension, output_dimension): super(PerceptronNew, self).__init__() self._layer1 = nn.Linear(input_dimension, hidden_dimension) self._layer2 = nn.Linear(hidden_dimension, output_dimension, bias=False ) def forward(self, input_0): primals_1 = self._layer1.weight primals_2 = self._layer1.bias primals_4 = self._layer2.weight primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
shi27feng/PDP-Solver
Perceptron
false
12,977
[ "MIT" ]
0
bf6e392f72f8a3572e0987313230943d94d53c95
https://github.com/shi27feng/PDP-Solver/tree/bf6e392f72f8a3572e0987313230943d94d53c95
MinimaxGeneratorLoss
import torch import torch.nn as nn import torch.nn.functional as F def minimax_generator_loss(dgz, nonsaturating=True, reduction='mean'): if nonsaturating: target = torch.ones_like(dgz) return F.binary_cross_entropy_with_logits(dgz, target, reduction= reduction) else: target = torch.zeros_like(dgz) return -1.0 * F.binary_cross_entropy_with_logits(dgz, target, reduction=reduction) class GeneratorLoss(nn.Module): """Base class for all generator losses. .. note:: All Losses meant to be minimized for optimizing the Generator must subclass this. Args: reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the outputs are averaged over batch size. If ``sum`` the elements of the output are summed. override_train_ops (function, optional): Function to be used in place of the default ``train_ops`` """ def __init__(self, reduction='mean', override_train_ops=None): super(GeneratorLoss, self).__init__() self.reduction = reduction self.override_train_ops = override_train_ops self.arg_map = {} def set_arg_map(self, value): """Updates the ``arg_map`` for passing a different value to the ``train_ops``. Args: value (dict): A mapping of the ``argument name`` in the method signature and the variable name in the ``Trainer`` it corresponds to. .. note:: If the ``train_ops`` signature is ``train_ops(self, gen, disc, optimizer_generator, device, batch_size, labels=None)`` then we need to map ``gen`` to ``generator`` and ``disc`` to ``discriminator``. In this case we make the following function call ``loss.set_arg_map({"gen": "generator", "disc": "discriminator"})``. """ self.arg_map.update(value) def train_ops(self, generator, discriminator, optimizer_generator, device, batch_size, labels=None): """Defines the standard ``train_ops`` used by most losses. Losses which have a different training procedure can either ``subclass`` it **(recommended approach)** or make use of ``override_train_ops`` argument. The ``standard optimization algorithm`` for the ``generator`` defined in this train_ops is as follows: 1. :math:`fake = generator(noise)` 2. :math:`value = discriminator(fake)` 3. :math:`loss = loss\\_function(value)` 4. Backpropagate by computing :math:`\\nabla loss` 5. Run a step of the optimizer for generator Args: generator (torchgan.models.Generator): The model to be optimized. discriminator (torchgan.models.Discriminator): The discriminator which judges the performance of the generator. optimizer_generator (torch.optim.Optimizer): Optimizer which updates the ``parameters`` of the ``generator``. device (torch.device): Device on which the ``generator`` and ``discriminator`` is present. batch_size (int): Batch Size of the data infered from the ``DataLoader`` by the ``Trainer``. labels (torch.Tensor, optional): Labels for the data. Returns: Scalar value of the loss. """ if self.override_train_ops is not None: return self.override_train_ops(generator, discriminator, optimizer_generator, device, batch_size, labels) else: if labels is None and generator.label_type == 'required': raise Exception('GAN model requires labels for training') noise = torch.randn(batch_size, generator.encoding_dims, device =device) optimizer_generator.zero_grad() if generator.label_type == 'generated': label_gen = torch.randint(0, generator.num_classes, ( batch_size,), device=device) if generator.label_type == 'none': fake = generator(noise) elif generator.label_type == 'required': fake = generator(noise, labels) elif generator.label_type == 'generated': fake = generator(noise, label_gen) if discriminator.label_type == 'none': dgz = discriminator(fake) elif generator.label_type == 'generated': dgz = discriminator(fake, label_gen) else: dgz = discriminator(fake, labels) loss = self.forward(dgz) loss.backward() optimizer_generator.step() return loss.item() class MinimaxGeneratorLoss(GeneratorLoss): """Minimax game generator loss from the original GAN paper `"Generative Adversarial Networks by Goodfellow et. al." <https://arxiv.org/abs/1406.2661>`_ The loss can be described as: .. math:: L(G) = log(1 - D(G(z))) The nonsaturating heuristic is also supported: .. math:: L(G) = -log(D(G(z))) where - :math:`G` : Generator - :math:`D` : Discriminator - :math:`z` : A sample from the noise prior Args: reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the outputs are averaged over batch size. If ``sum`` the elements of the output are summed. override_train_ops (function, optional): Function to be used in place of the default ``train_ops`` nonsaturating(bool, optional): Specifies whether to use the nonsaturating heuristic loss for the generator. """ def __init__(self, reduction='mean', nonsaturating=True, override_train_ops=None): super(MinimaxGeneratorLoss, self).__init__(reduction, override_train_ops) self.nonsaturating = nonsaturating def forward(self, dgz): """Computes the loss for the given input. Args: dgz (torch.Tensor) : Output of the Discriminator with generated data. It must have the dimensions (N, \\*) where \\* means any number of additional dimensions. Returns: scalar if reduction is applied else Tensor with dimensions (N, \\*). """ return minimax_generator_loss(dgz, self.nonsaturating, self.reduction) 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.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_binary_cross_entropy_with_logits_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 = 0.0 tmp2 = tmp1 * tmp0 tmp3 = triton_helpers.minimum(tmp1, tmp0) tmp4 = tl_math.abs(tmp0) tmp5 = -tmp4 tmp6 = tl_math.exp(tmp5) tmp7 = libdevice.log1p(tmp6) tmp8 = tmp3 - tmp7 tmp9 = tmp2 - tmp8 tmp10 = tl.broadcast_to(tmp9, [RBLOCK]) tmp12 = triton_helpers.promote_to_tensor(tl.sum(tmp10, 0)) tmp13 = 256.0 tmp14 = tmp12 / tmp13 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp14, 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_binary_cross_entropy_with_logits_0[grid(1)](buf1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 return buf1, def minimax_generator_loss(dgz, nonsaturating=True, reduction='mean'): if nonsaturating: target = torch.ones_like(dgz) return F.binary_cross_entropy_with_logits(dgz, target, reduction= reduction) else: target = torch.zeros_like(dgz) return -1.0 * F.binary_cross_entropy_with_logits(dgz, target, reduction=reduction) class GeneratorLoss(nn.Module): """Base class for all generator losses. .. note:: All Losses meant to be minimized for optimizing the Generator must subclass this. Args: reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the outputs are averaged over batch size. If ``sum`` the elements of the output are summed. override_train_ops (function, optional): Function to be used in place of the default ``train_ops`` """ def __init__(self, reduction='mean', override_train_ops=None): super(GeneratorLoss, self).__init__() self.reduction = reduction self.override_train_ops = override_train_ops self.arg_map = {} def set_arg_map(self, value): """Updates the ``arg_map`` for passing a different value to the ``train_ops``. Args: value (dict): A mapping of the ``argument name`` in the method signature and the variable name in the ``Trainer`` it corresponds to. .. note:: If the ``train_ops`` signature is ``train_ops(self, gen, disc, optimizer_generator, device, batch_size, labels=None)`` then we need to map ``gen`` to ``generator`` and ``disc`` to ``discriminator``. In this case we make the following function call ``loss.set_arg_map({"gen": "generator", "disc": "discriminator"})``. """ self.arg_map.update(value) def train_ops(self, generator, discriminator, optimizer_generator, device, batch_size, labels=None): """Defines the standard ``train_ops`` used by most losses. Losses which have a different training procedure can either ``subclass`` it **(recommended approach)** or make use of ``override_train_ops`` argument. The ``standard optimization algorithm`` for the ``generator`` defined in this train_ops is as follows: 1. :math:`fake = generator(noise)` 2. :math:`value = discriminator(fake)` 3. :math:`loss = loss\\_function(value)` 4. Backpropagate by computing :math:`\\nabla loss` 5. Run a step of the optimizer for generator Args: generator (torchgan.models.Generator): The model to be optimized. discriminator (torchgan.models.Discriminator): The discriminator which judges the performance of the generator. optimizer_generator (torch.optim.Optimizer): Optimizer which updates the ``parameters`` of the ``generator``. device (torch.device): Device on which the ``generator`` and ``discriminator`` is present. batch_size (int): Batch Size of the data infered from the ``DataLoader`` by the ``Trainer``. labels (torch.Tensor, optional): Labels for the data. Returns: Scalar value of the loss. """ if self.override_train_ops is not None: return self.override_train_ops(generator, discriminator, optimizer_generator, device, batch_size, labels) else: if labels is None and generator.label_type == 'required': raise Exception('GAN model requires labels for training') noise = torch.randn(batch_size, generator.encoding_dims, device =device) optimizer_generator.zero_grad() if generator.label_type == 'generated': label_gen = torch.randint(0, generator.num_classes, ( batch_size,), device=device) if generator.label_type == 'none': fake = generator(noise) elif generator.label_type == 'required': fake = generator(noise, labels) elif generator.label_type == 'generated': fake = generator(noise, label_gen) if discriminator.label_type == 'none': dgz = discriminator(fake) elif generator.label_type == 'generated': dgz = discriminator(fake, label_gen) else: dgz = discriminator(fake, labels) loss = self.forward(dgz) loss.backward() optimizer_generator.step() return loss.item() class MinimaxGeneratorLossNew(GeneratorLoss): """Minimax game generator loss from the original GAN paper `"Generative Adversarial Networks by Goodfellow et. al." <https://arxiv.org/abs/1406.2661>`_ The loss can be described as: .. math:: L(G) = log(1 - D(G(z))) The nonsaturating heuristic is also supported: .. math:: L(G) = -log(D(G(z))) where - :math:`G` : Generator - :math:`D` : Discriminator - :math:`z` : A sample from the noise prior Args: reduction (str, optional): Specifies the reduction to apply to the output. If ``none`` no reduction will be applied. If ``mean`` the outputs are averaged over batch size. If ``sum`` the elements of the output are summed. override_train_ops (function, optional): Function to be used in place of the default ``train_ops`` nonsaturating(bool, optional): Specifies whether to use the nonsaturating heuristic loss for the generator. """ def __init__(self, reduction='mean', nonsaturating=True, override_train_ops=None): super(MinimaxGeneratorLossNew, self).__init__(reduction, override_train_ops) self.nonsaturating = nonsaturating def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
shi-weili/torchgan
MinimaxGeneratorLoss
false
12,978
[ "MIT" ]
0
28ffd4026b8c0db2217b667d30a222d6758bfc41
https://github.com/shi-weili/torchgan/tree/28ffd4026b8c0db2217b667d30a222d6758bfc41
SpatialCrossMapLRN
import torch import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data class SpatialCrossMapLRN(nn.Module): def __init__(self, local_size=1, alpha=1.0, beta=0.75, k=1, ACROSS_CHANNELS=True): super(SpatialCrossMapLRN, self).__init__() self.ACROSS_CHANNELS = ACROSS_CHANNELS if ACROSS_CHANNELS: self.average = nn.AvgPool3d(kernel_size=(local_size, 1, 1), stride=1, padding=(int((local_size - 1.0) / 2), 0, 0)) else: self.average = nn.AvgPool2d(kernel_size=local_size, stride=1, padding=int((local_size - 1.0) / 2)) self.alpha = alpha self.beta = beta self.k = k def forward(self, x): if self.ACROSS_CHANNELS: div = x.pow(2).unsqueeze(1) div = self.average(div).squeeze(1) div = div.mul(self.alpha).add(self.k).pow(self.beta) else: div = x.pow(2) div = self.average(div) div = div.mul(self.alpha).add(self.k).pow(self.beta) x = x.div(div) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.nn.parallel import torch.optim 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_add_div_mul_pow_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tmp0 * tmp0 tmp2 = 1.0 tmp3 = tmp1 * tmp2 tmp4 = tmp3 * tmp2 tmp5 = tmp4 + tmp2 tmp6 = 0.75 tmp7 = libdevice.pow(tmp5, tmp6) tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x0, tmp8, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_mul_pow_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, class SpatialCrossMapLRNNew(nn.Module): def __init__(self, local_size=1, alpha=1.0, beta=0.75, k=1, ACROSS_CHANNELS=True): super(SpatialCrossMapLRNNew, self).__init__() self.ACROSS_CHANNELS = ACROSS_CHANNELS if ACROSS_CHANNELS: self.average = nn.AvgPool3d(kernel_size=(local_size, 1, 1), stride=1, padding=(int((local_size - 1.0) / 2), 0, 0)) else: self.average = nn.AvgPool2d(kernel_size=local_size, stride=1, padding=int((local_size - 1.0) / 2)) self.alpha = alpha self.beta = beta self.k = k def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
shubham1206agra/pretrained-models.pytorch
SpatialCrossMapLRN
false
12,979
[ "BSD-3-Clause" ]
0
a2940f79dd65656eabe5a0cd6d5d014ef1fc2523
https://github.com/shubham1206agra/pretrained-models.pytorch/tree/a2940f79dd65656eabe5a0cd6d5d014ef1fc2523
GraphConv
import torch from torch import nn import torch.nn import torch.autograd def sparse_bmm(sparse_matrix, dense_matrix_batch): """ Perform torch.bmm on an unbatched sparse matrix and a batched dense matrix. Args: sparse_matrix (torch.sparse.FloatTensor): Shape = (m, n) dense_matrix_batch (torch.FloatTensor): Shape = (b, n, p) Returns: (torch.FloatTensor): Result of the batched matrix multiplication. Shape = (b, n, p) """ m = sparse_matrix.shape[0] b, n, p = dense_matrix_batch.shape dense_matrix = dense_matrix_batch.transpose(0, 1).reshape(n, b * p) result = torch.sparse.mm(sparse_matrix, dense_matrix) return result.reshape(m, b, p).transpose(0, 1) class GraphConv(nn.Module): """A simple graph convolution layer, similar to the one defined in Kipf et al. https://arxiv.org/abs/1609.02907 This operation with self_layer=False is equivalent to :math:`(A H W)` where: - :math:`H` is the node features with shape (batch_size, num_nodes, input_dim) - :math:`W` is a weight matrix of shape (input_dim, output_dim) - :math:`A` is the adjacency matrix of shape (num_nodes, num_nodes). It can include self-loop. With normalize_adj=True, it is equivalent to :math:`(D^{-1} A H W)`, where: - :math:`D` is a diagonal matrix with :math:`D_{ii}` = the sum of the i-th row of A. In other words, :math:`D` is the incoming degree of each node. With self_layer=True, it is equivalent to the above plus :math:`(H W_{\\text{self}})`, where: - :math:`W_{\\text{self}}` is a separate weight matrix to filter each node's self features. Note that when self_layer is True, A should not include self-loop. If you use this code, please cite the original paper in addition to Kaolin. .. code-block:: @article{kipf2016semi, title={Semi-Supervised Classification with Graph Convolutional Networks}, author={Kipf, Thomas N and Welling, Max}, journal={arXiv preprint arXiv:1609.02907}, year={2016} } Args: input_dim (int): The number of features in each input node. output_dim (int): The number of features in each output node. bias (bool): Whether to add bias after the node-wise linear layer. Example: >>> node_feat = torch.rand(1, 3, 5) >>> i = torch.LongTensor( ... [[0, 1, 1, 2, 2, 0], [1, 0, 2, 1, 0, 2]]) >>> v = torch.FloatTensor([1, 1, 1, 1, 1, 1]) >>> adj = torch.sparse.FloatTensor(i, v, torch.Size([3, 3])) >>> model = GraphConv(5, 10) >>> output = model(node_feat, adj) >>> # pre-normalize adj >>> adj = normalize_adj(adj) >>> output = model(node_feat, adj, normalize_adj=False) """ def __init__(self, input_dim, output_dim, self_layer=True, bias=True): super(GraphConv, self).__init__() self.self_layer = self_layer self.linear = nn.Linear(input_dim, output_dim, bias=bias) if self_layer: self.linear_self = nn.Linear(input_dim, output_dim, bias=bias) else: self.linear_self = None self.initialize() def initialize(self): nn.init.xavier_uniform_(self.linear.weight.data) if self.linear.bias is not None: self.linear.bias.data.uniform_(-1.0, 1.0) if self.self_layer: nn.init.xavier_uniform_(self.linear_self.weight.data) if self.linear_self.bias is not None: self.linear_self.bias.data.uniform_(-1.0, 1.0) def forward(self, node_feat, adj, normalize_adj=True): """ Args: node_feat (torch.FloatTensor): Shape = (batch_size, num_nodes, input_dim) The input features of each node. adj (torch.sparse.FloatTensor or torch.FloatTensor): Shape = (num_nodes, num_nodes) The adjacency matrix. adj[i, j] is non-zero if there's an incoming edge from j to i. Should not include self-loop if self_layer is True. normalize_adj (bool): Set this to true to apply normalization to adjacency; that is, each output feature will be divided by the number of incoming neighbors. If normalization is not desired, or if the adjacency matrix is pre-normalized, set this to False to improve performance. Returns: (torch.FloatTensor): The output features of each node. Shape = (batch_size, num_nodes, output_dim) """ if adj.type().endswith('sparse.FloatTensor'): if normalize_adj: norm = torch.sparse.mm(adj, torch.ones((adj.shape[0], 1), device=node_feat.device)) result = sparse_bmm(adj, self.linear(node_feat)) / norm else: result = sparse_bmm(adj, self.linear(node_feat)) elif normalize_adj: norm = torch.matmul(adj, torch.ones((adj.shape[0], 1), device= node_feat.device)) result = torch.matmul(adj, self.linear(node_feat)) / norm else: result = torch.matmul(adj, self.linear(node_feat)) if self.self_layer: result += self.linear_self(node_feat) return result def get_inputs(): return [torch.rand([4, 4, 4, 4]), 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 import nn import torch.nn import torch.autograd 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_ones_0(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 = 1.0 tl.store(out_ptr0 + x0, tmp0, xmask) @triton.jit def triton_poi_fused_add_div_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x2, xmask) tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 / tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tl.store(in_out_ptr0 + x2, tmp6, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4,), (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, 1), (1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_ones_0[grid(4)](buf0, 4, XBLOCK=4, num_warps=1, num_stages=1) buf1 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), buf0, out=buf1) del buf0 buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_4, reinterpret_tensor(primals_2, (64, 4), (4, 1), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf2) del primals_3 del primals_4 buf3 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(primals_1, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf2, (16, 4, 4), (16, 4, 1), 0), out=buf3) buf4 = buf2 del buf2 extern_kernels.mm(reinterpret_tensor(primals_2, (64, 4), (4, 1), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf4) del primals_5 buf5 = reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf3 triton_poi_fused_add_div_1[grid(256)](buf5, buf1, buf4, primals_6, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf4 del primals_6 return buf5, buf1, reinterpret_tensor(primals_2, (64, 4), (4, 1), 0 ), reinterpret_tensor(primals_1, (16, 4, 4), (16, 1, 4), 0) def sparse_bmm(sparse_matrix, dense_matrix_batch): """ Perform torch.bmm on an unbatched sparse matrix and a batched dense matrix. Args: sparse_matrix (torch.sparse.FloatTensor): Shape = (m, n) dense_matrix_batch (torch.FloatTensor): Shape = (b, n, p) Returns: (torch.FloatTensor): Result of the batched matrix multiplication. Shape = (b, n, p) """ m = sparse_matrix.shape[0] b, n, p = dense_matrix_batch.shape dense_matrix = dense_matrix_batch.transpose(0, 1).reshape(n, b * p) result = torch.sparse.mm(sparse_matrix, dense_matrix) return result.reshape(m, b, p).transpose(0, 1) class GraphConvNew(nn.Module): """A simple graph convolution layer, similar to the one defined in Kipf et al. https://arxiv.org/abs/1609.02907 This operation with self_layer=False is equivalent to :math:`(A H W)` where: - :math:`H` is the node features with shape (batch_size, num_nodes, input_dim) - :math:`W` is a weight matrix of shape (input_dim, output_dim) - :math:`A` is the adjacency matrix of shape (num_nodes, num_nodes). It can include self-loop. With normalize_adj=True, it is equivalent to :math:`(D^{-1} A H W)`, where: - :math:`D` is a diagonal matrix with :math:`D_{ii}` = the sum of the i-th row of A. In other words, :math:`D` is the incoming degree of each node. With self_layer=True, it is equivalent to the above plus :math:`(H W_{\\text{self}})`, where: - :math:`W_{\\text{self}}` is a separate weight matrix to filter each node's self features. Note that when self_layer is True, A should not include self-loop. If you use this code, please cite the original paper in addition to Kaolin. .. code-block:: @article{kipf2016semi, title={Semi-Supervised Classification with Graph Convolutional Networks}, author={Kipf, Thomas N and Welling, Max}, journal={arXiv preprint arXiv:1609.02907}, year={2016} } Args: input_dim (int): The number of features in each input node. output_dim (int): The number of features in each output node. bias (bool): Whether to add bias after the node-wise linear layer. Example: >>> node_feat = torch.rand(1, 3, 5) >>> i = torch.LongTensor( ... [[0, 1, 1, 2, 2, 0], [1, 0, 2, 1, 0, 2]]) >>> v = torch.FloatTensor([1, 1, 1, 1, 1, 1]) >>> adj = torch.sparse.FloatTensor(i, v, torch.Size([3, 3])) >>> model = GraphConv(5, 10) >>> output = model(node_feat, adj) >>> # pre-normalize adj >>> adj = normalize_adj(adj) >>> output = model(node_feat, adj, normalize_adj=False) """ def __init__(self, input_dim, output_dim, self_layer=True, bias=True): super(GraphConvNew, self).__init__() self.self_layer = self_layer self.linear = nn.Linear(input_dim, output_dim, bias=bias) if self_layer: self.linear_self = nn.Linear(input_dim, output_dim, bias=bias) else: self.linear_self = None self.initialize() def initialize(self): nn.init.xavier_uniform_(self.linear.weight.data) if self.linear.bias is not None: self.linear.bias.data.uniform_(-1.0, 1.0) if self.self_layer: nn.init.xavier_uniform_(self.linear_self.weight.data) if self.linear_self.bias is not None: self.linear_self.bias.data.uniform_(-1.0, 1.0) def forward(self, input_0, input_1): primals_3 = self.linear.weight primals_4 = self.linear.bias primals_5 = self.linear_self.weight primals_6 = self.linear_self.bias primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6]) return output[0]
shumash/kaolin
GraphConv
false
12,980
[ "ECL-2.0", "Apache-2.0" ]
0
2158b5ec7a28d57d7df7e606adbb0c693a0145f0
https://github.com/shumash/kaolin/tree/2158b5ec7a28d57d7df7e606adbb0c693a0145f0
Qnet
import random import torch import torch.nn as nn import torch.nn.functional as F class Qnet(nn.Module): def __init__(self): super(Qnet, self).__init__() self.fc1 = nn.Linear(4, 128) self.fc2 = nn.Linear(128, 128) self.fc3 = nn.Linear(128, 2) def forward(self, x): x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x def sample_action(self, obs, epsilon): out = self.forward(obs) coin = random.random() if coin < epsilon: return random.randint(0, 1) else: return out.argmax().item() 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 random import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 128 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (128, 4), (4, 1)) assert_size_stride(primals_2, (128,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (128, 128), (128, 1)) assert_size_stride(primals_5, (128,), (1,)) assert_size_stride(primals_6, (2, 128), (128, 1)) assert_size_stride(primals_7, (2,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 128), (128, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 128), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 128), (2048, 512, 128, 1), 0) del buf0 buf6 = empty_strided_cuda((4, 4, 4, 128), (2048, 512, 128, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(8192)](buf1, primals_2, buf6, 8192, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 128), (128, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 128), (128, 1), 0), reinterpret_tensor(primals_4, (128, 128), (1, 128), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 128), (2048, 512, 128, 1), 0) del buf2 buf5 = empty_strided_cuda((4, 4, 4, 128), (2048, 512, 128, 1), torch.bool) triton_poi_fused_relu_threshold_backward_0[grid(8192)](buf3, primals_5, buf5, 8192, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 2), (2, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 128), (128, 1), 0), reinterpret_tensor(primals_6, (128, 2), (1, 128), 0), alpha=1, beta=1, out=buf4) del primals_7 return reinterpret_tensor(buf4, (4, 4, 4, 2), (32, 8, 2, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 128), (128, 1), 0 ), reinterpret_tensor(buf3, (64, 128), (128, 1), 0 ), primals_6, buf5, primals_4, buf6 class QnetNew(nn.Module): def __init__(self): super(QnetNew, self).__init__() self.fc1 = nn.Linear(4, 128) self.fc2 = nn.Linear(128, 128) self.fc3 = nn.Linear(128, 2) def sample_action(self, obs, epsilon): out = self.forward(obs) coin = random.random() if coin < epsilon: return random.randint(0, 1) else: return out.argmax().item() def forward(self, input_0): primals_1 = self.fc1.weight primals_2 = self.fc1.bias primals_4 = self.fc2.weight primals_5 = self.fc2.bias primals_6 = self.fc3.weight primals_7 = self.fc3.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
shwetasrsh/minimalRL
Qnet
false
12,981
[ "MIT" ]
0
e6fef1730238dd268b1a43fd9fca0b0c40d97837
https://github.com/shwetasrsh/minimalRL/tree/e6fef1730238dd268b1a43fd9fca0b0c40d97837
ScaleHead
import torch import torch.nn as nn class ScaleHead(nn.Module): def __init__(self): super().__init__() self.flatten = torch.flatten self.dot = torch.dot def forward(self, mag, height): curr_mag = self.flatten(mag, start_dim=1) curr_height = self.flatten(height, start_dim=1) batch_size = curr_mag.shape[0] length = curr_mag.shape[1] denom = torch.squeeze(torch.bmm(curr_height.view(batch_size, 1, length), curr_height.view(batch_size, length, 1))) + 0.01 pinv = curr_height / denom.view(batch_size, 1) scale = torch.squeeze(torch.bmm(pinv.view(batch_size, 1, length), curr_mag.view(batch_size, length, 1))) return scale 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.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_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 64 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = 0.01 tmp3 = tmp1 + tmp2 tmp4 = tmp0 / tmp3 tl.store(out_ptr0 + x2, tmp4, 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, 1, 1), (1, 1, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(arg1_1, (4, 1, 64), (64, 64, 1), 0), reinterpret_tensor(arg1_1, (4, 64, 1), (64, 1, 1), 0), out=buf0) buf1 = empty_strided_cuda((4, 64), (64, 1), torch.float32) get_raw_stream(0) triton_poi_fused_div_0[grid(256)](arg1_1, buf0, buf1, 256, XBLOCK= 256, num_warps=4, num_stages=1) del arg1_1 buf2 = buf0 del buf0 extern_kernels.bmm(reinterpret_tensor(buf1, (4, 1, 64), (64, 0, 1), 0), reinterpret_tensor(arg0_1, (4, 64, 1), (64, 1, 1), 0), out=buf2 ) del arg0_1 del buf1 return reinterpret_tensor(buf2, (4,), (1,), 0), class ScaleHeadNew(nn.Module): def __init__(self): super().__init__() self.flatten = torch.flatten self.dot = torch.dot def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
shvedfun/geo_pos_baseline
ScaleHead
false
12,982
[ "Apache-2.0" ]
0
024716bfdaefd23baccfb5a0d2686015385d7b9c
https://github.com/shvedfun/geo_pos_baseline/tree/024716bfdaefd23baccfb5a0d2686015385d7b9c
RNNCell
import torch import torch.nn as nn class RNNCell(nn.Module): def __init__(self, embed_dim, hidden_size, vocab_dim): super().__init__() self.hidden_size = hidden_size self.input2hidden = nn.Linear(embed_dim + hidden_size, hidden_size) def forward(self, inputs, hidden): combined = torch.cat((inputs, hidden), 2) hidden = torch.relu(self.input2hidden(combined)) return hidden def init_hidden(self, batch_size): return torch.zeros(1, batch_size, self.hidden_size) def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'embed_dim': 4, 'hidden_size': 4, 'vocab_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 8 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_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 x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = 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, 8), (8, 1)) assert_size_stride(primals_4, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 8), (32, 8, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(128)](primals_1, primals_2, buf0, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 del primals_2 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (16, 8), (8, 1), 0), reinterpret_tensor(primals_3, (8, 4), (1, 8), 0), out=buf1) del primals_3 buf2 = reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 0) del buf1 buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool) triton_poi_fused_relu_threshold_backward_1[grid(64)](buf2, primals_4, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_4 return buf2, reinterpret_tensor(buf0, (16, 8), (8, 1), 0), buf3 class RNNCellNew(nn.Module): def __init__(self, embed_dim, hidden_size, vocab_dim): super().__init__() self.hidden_size = hidden_size self.input2hidden = nn.Linear(embed_dim + hidden_size, hidden_size) def init_hidden(self, batch_size): return torch.zeros(1, batch_size, self.hidden_size) def forward(self, input_0, input_1): primals_3 = self.input2hidden.weight primals_4 = self.input2hidden.bias primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
saidulislam/RNN-Sequential-Data-Processing
RNNCell
false
12,983
[ "Apache-2.0" ]
0
2e043f37f9a67177a3dc19cbfe67d187c9cbb5f9
https://github.com/saidulislam/RNN-Sequential-Data-Processing/tree/2e043f37f9a67177a3dc19cbfe67d187c9cbb5f9
EnsembleFC
import torch import torch.nn as nn class EnsembleFC(nn.Module): def __init__(self, in_features: 'int', out_features: 'int', ensemble_size: 'int', weight_decay: 'float'=0.0, bias: 'bool'=True ) ->None: super(EnsembleFC, self).__init__() self.in_features = in_features self.out_features = out_features self.ensemble_size = ensemble_size self.weight = nn.Parameter(torch.Tensor(ensemble_size, in_features, out_features)) self.weight_decay = weight_decay if bias: self.bias = nn.Parameter(torch.Tensor(ensemble_size, out_features)) else: self.register_parameter('bias', None) self.reset_parameters() def reset_parameters(self) ->None: pass def forward(self, input: 'torch.Tensor') ->torch.Tensor: w_times_x = torch.bmm(input, self.weight) return torch.add(w_times_x, self.bias[:, None, :]) def extra_repr(self) ->str: return 'in_features={}, out_features={}, bias={}'.format(self. in_features, self.out_features, self.bias is not None) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4, 'ensemble_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_add_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 4 x2 = xindex // 16 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(primals_2, primals_1, out=buf0) del primals_1 buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_add_0[grid(64)](buf1, primals_3, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_3 return buf1, reinterpret_tensor(primals_2, (4, 4, 4), (16, 1, 4), 0) class EnsembleFCNew(nn.Module): def __init__(self, in_features: 'int', out_features: 'int', ensemble_size: 'int', weight_decay: 'float'=0.0, bias: 'bool'=True ) ->None: super(EnsembleFCNew, self).__init__() self.in_features = in_features self.out_features = out_features self.ensemble_size = ensemble_size self.weight = nn.Parameter(torch.Tensor(ensemble_size, in_features, out_features)) self.weight_decay = weight_decay if bias: self.bias = nn.Parameter(torch.Tensor(ensemble_size, out_features)) else: self.register_parameter('bias', None) self.reset_parameters() def reset_parameters(self) ->None: pass def extra_repr(self) ->str: return 'in_features={}, out_features={}, bias={}'.format(self. in_features, self.out_features, self.bias is not None) def forward(self, input_0): primals_1 = self.weight primals_3 = self.bias primals_2 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
si0wang/transfer_dmc
EnsembleFC
false
12,984
[ "MIT" ]
0
6bda773244e0b709b3c13add2597f5f1cd01bfd7
https://github.com/si0wang/transfer_dmc/tree/6bda773244e0b709b3c13add2597f5f1cd01bfd7
DynamicsModel
import torch import torch.nn as nn import torch.nn.functional as F def weights_init_(m): if isinstance(m, nn.Linear) or isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d): torch.nn.init.xavier_uniform_(m.weight, gain=1) torch.nn.init.constant_(m.bias, 0) class Swish(nn.Module): def __init__(self): super(Swish, self).__init__() def forward(self, x): x = x * F.sigmoid(x) return x class DynamicsModel(nn.Module): def __init__(self, feature_size, hidden_size=256, use_decay=False): super(DynamicsModel, self).__init__() self.hidden_size = hidden_size self.nn1 = nn.Linear(feature_size + feature_size, feature_size) self.use_decay = use_decay self.apply(weights_init_) self.swish = Swish() def forward(self, state_latent, action_latent): x = torch.cat([state_latent, action_latent], 1) nn1_output = self.nn1(x) return nn1_output def get_decay_loss(self): decay_loss = 0.0 for m in self.children(): if type(m) == nn.Linear: decay_loss += m.weight_decay * torch.sum(torch.square(m.weight) ) / 2.0 return decay_loss def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'feature_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 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_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 8 x1 = xindex // 8 x2 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (4 * x1 + x0), tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp6 = 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) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 8), (8, 1)) assert_size_stride(primals_4, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 8), (8, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(32)](primals_1, primals_2, buf0, 32, XBLOCK=32, num_warps=1, num_stages=1) del primals_1 del primals_2 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_4, buf0, reinterpret_tensor(primals_3, (8, 4), (1, 8), 0), alpha=1, beta=1, out=buf1) del primals_3 del primals_4 return buf1, buf0 def weights_init_(m): if isinstance(m, nn.Linear) or isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d): torch.nn.init.xavier_uniform_(m.weight, gain=1) torch.nn.init.constant_(m.bias, 0) class Swish(nn.Module): def __init__(self): super(Swish, self).__init__() def forward(self, x): x = x * F.sigmoid(x) return x class DynamicsModelNew(nn.Module): def __init__(self, feature_size, hidden_size=256, use_decay=False): super(DynamicsModelNew, self).__init__() self.hidden_size = hidden_size self.nn1 = nn.Linear(feature_size + feature_size, feature_size) self.use_decay = use_decay self.apply(weights_init_) self.swish = Swish() def get_decay_loss(self): decay_loss = 0.0 for m in self.children(): if type(m) == nn.Linear: decay_loss += m.weight_decay * torch.sum(torch.square(m.weight) ) / 2.0 return decay_loss def forward(self, input_0, input_1): primals_3 = self.nn1.weight primals_4 = self.nn1.bias primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
si0wang/transfer_dmc
DynamicsModel
false
12,985
[ "MIT" ]
0
6bda773244e0b709b3c13add2597f5f1cd01bfd7
https://github.com/si0wang/transfer_dmc/tree/6bda773244e0b709b3c13add2597f5f1cd01bfd7
MnistMlp
import torch from torch import nn as nn from torch.nn import functional as F class MnistMlp(nn.Module): def __init__(self, width, dropout_p): super().__init__() self.fc1 = nn.Linear(784, width) self.fc2 = nn.Linear(width, 10) self.dropout = nn.Dropout(dropout_p) def forward(self, x): x = torch.reshape(x, (-1, 784)) x = F.relu(self.fc1(x)) x = self.dropout(x) x = F.log_softmax(self.fc2(x), dim=1) return x def get_inputs(): return [torch.rand([4, 784])] def get_init_inputs(): return [[], {'width': 4, 'dropout_p': 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 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_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 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_per_fused__log_softmax_1(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 = args args.clear() assert_size_stride(primals_1, (4, 784), (784, 1)) assert_size_stride(primals_2, (4, 784), (784, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (10, 4), (4, 1)) assert_size_stride(primals_5, (10,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (784, 4), (1, 784), 0), out=buf0) del primals_2 buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_relu_0[grid(16)](buf1, primals_3, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_3 buf2 = empty_strided_cuda((4, 10), (10, 1), torch.float32) extern_kernels.addmm(primals_5, buf1, reinterpret_tensor(primals_4, (4, 10), (1, 4), 0), alpha=1, beta=1, out=buf2) del primals_5 buf5 = empty_strided_cuda((4, 10), (10, 1), torch.float32) triton_per_fused__log_softmax_1[grid(4)](buf2, buf5, 4, 10, XBLOCK= 1, num_warps=2, num_stages=1) del buf2 return buf5, primals_1, buf1, buf5, primals_4 class MnistMlpNew(nn.Module): def __init__(self, width, dropout_p): super().__init__() self.fc1 = nn.Linear(784, width) self.fc2 = nn.Linear(width, 10) self.dropout = nn.Dropout(dropout_p) def forward(self, input_0): primals_1 = self.fc1.weight primals_3 = self.fc1.bias primals_4 = self.fc2.weight primals_5 = self.fc2.bias primals_2 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
shyam196/exptune
MnistMlp
false
12,986
[ "MIT" ]
0
be9bb23355ecd1a464dbc93dc35050b7f9d40227
https://github.com/shyam196/exptune/tree/be9bb23355ecd1a464dbc93dc35050b7f9d40227
EnsembleFC
import torch import torch.nn as nn class EnsembleFC(nn.Module): __constants__ = ['in_features', 'out_features'] in_features: 'int' out_features: 'int' ensemble_size: 'int' weight: 'torch.Tensor' def __init__(self, in_features: 'int', out_features: 'int', ensemble_size: 'int', weight_decay: 'float'=0.0) ->None: super(EnsembleFC, self).__init__() self.in_features = in_features self.out_features = out_features self.ensemble_size = ensemble_size self.weight = nn.Parameter(torch.Tensor(ensemble_size, in_features, out_features)) self.weight_decay = weight_decay self.bias = nn.Parameter(torch.Tensor(ensemble_size, 1, out_features)) def forward(self, input: 'torch.Tensor') ->torch.Tensor: assert input.shape[0] == self.ensemble_size and len(input.shape) == 3 return torch.bmm(input, self.weight) + self.bias def extra_repr(self) ->str: return 'in_features={}, out_features={}'.format(self.in_features, self.out_features) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4, 'ensemble_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_add_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 4 x2 = xindex // 16 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_3, (4, 1, 4), (4, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(primals_1, primals_2, out=buf0) del primals_2 buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_add_0[grid(64)](buf1, primals_3, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_3 return buf1, reinterpret_tensor(primals_1, (4, 4, 4), (16, 1, 4), 0) class EnsembleFCNew(nn.Module): __constants__ = ['in_features', 'out_features'] in_features: 'int' out_features: 'int' ensemble_size: 'int' weight: 'torch.Tensor' def __init__(self, in_features: 'int', out_features: 'int', ensemble_size: 'int', weight_decay: 'float'=0.0) ->None: super(EnsembleFCNew, self).__init__() self.in_features = in_features self.out_features = out_features self.ensemble_size = ensemble_size self.weight = nn.Parameter(torch.Tensor(ensemble_size, in_features, out_features)) self.weight_decay = weight_decay self.bias = nn.Parameter(torch.Tensor(ensemble_size, 1, out_features)) def extra_repr(self) ->str: return 'in_features={}, out_features={}'.format(self.in_features, self.out_features) def forward(self, input_0): primals_1 = self.weight primals_3 = self.bias primals_2 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
simonat2011/DI-engine
EnsembleFC
false
12,987
[ "Apache-2.0" ]
0
3a91c4297d58b3beff40b48bd37eb0b399c724a7
https://github.com/simonat2011/DI-engine/tree/3a91c4297d58b3beff40b48bd37eb0b399c724a7
EnsembleModel
import torch import torch.nn as nn import torch.nn.functional as F def weights_init_(m): if isinstance(m, nn.Linear) or isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d): torch.nn.init.xavier_uniform_(m.weight, gain=1) torch.nn.init.constant_(m.bias, 0) class Swish(nn.Module): def __init__(self): super(Swish, self).__init__() def forward(self, x): x = x * F.sigmoid(x) return x class EnsembleFC(nn.Module): def __init__(self, in_features: 'int', out_features: 'int', ensemble_size: 'int', weight_decay: 'float'=0.0, bias: 'bool'=True ) ->None: super(EnsembleFC, self).__init__() self.in_features = in_features self.out_features = out_features self.ensemble_size = ensemble_size self.weight = nn.Parameter(torch.Tensor(ensemble_size, in_features, out_features)) self.weight_decay = weight_decay if bias: self.bias = nn.Parameter(torch.Tensor(ensemble_size, out_features)) else: self.register_parameter('bias', None) self.reset_parameters() def reset_parameters(self) ->None: pass def forward(self, input: 'torch.Tensor') ->torch.Tensor: w_times_x = torch.bmm(input, self.weight) return torch.add(w_times_x, self.bias[:, None, :]) def extra_repr(self) ->str: return 'in_features={}, out_features={}, bias={}'.format(self. in_features, self.out_features, self.bias is not None) class EnsembleModel(nn.Module): def __init__(self, feature_size, ensemble_size, use_decay=False): super(EnsembleModel, self).__init__() self.nn1 = EnsembleFC(feature_size + feature_size, feature_size, ensemble_size, weight_decay=2.5e-05) self.use_decay = use_decay self.apply(weights_init_) self.swish = Swish() def forward(self, state_latent, action_latent): x = torch.cat([state_latent, action_latent], 2) nn1_output = self.nn1(x) return nn1_output def get_decay_loss(self): decay_loss = 0.0 for m in self.children(): if isinstance(m, EnsembleFC): decay_loss += m.weight_decay * torch.sum(torch.square(m.weight) ) / 2.0 return decay_loss def loss(self, mean, labels): """ mean, logvar: Ensemble_size x N x dim labels: N x dim """ assert len(mean.shape) == len(labels.shape) == 3 mse_loss = torch.mean(torch.pow(mean - labels, 2), dim=(1, 2)) total_loss = torch.sum(mse_loss) return total_loss, mse_loss def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'feature_size': 4, 'ensemble_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 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_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 8 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_add_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 4 x2 = xindex // 16 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_3, (4, 8, 4), (32, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 8), (32, 8, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(128)](primals_1, primals_2, buf0, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 del primals_2 buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf0, primals_3, out=buf1) del primals_3 buf2 = buf1 del buf1 triton_poi_fused_add_1[grid(64)](buf2, primals_4, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_4 return buf2, reinterpret_tensor(buf0, (4, 8, 4), (32, 1, 8), 0) def weights_init_(m): if isinstance(m, nn.Linear) or isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d): torch.nn.init.xavier_uniform_(m.weight, gain=1) torch.nn.init.constant_(m.bias, 0) class Swish(nn.Module): def __init__(self): super(Swish, self).__init__() def forward(self, x): x = x * F.sigmoid(x) return x class EnsembleFC(nn.Module): def __init__(self, in_features: 'int', out_features: 'int', ensemble_size: 'int', weight_decay: 'float'=0.0, bias: 'bool'=True ) ->None: super(EnsembleFC, self).__init__() self.in_features = in_features self.out_features = out_features self.ensemble_size = ensemble_size self.weight = nn.Parameter(torch.Tensor(ensemble_size, in_features, out_features)) self.weight_decay = weight_decay if bias: self.bias = nn.Parameter(torch.Tensor(ensemble_size, out_features)) else: self.register_parameter('bias', None) self.reset_parameters() def reset_parameters(self) ->None: pass def forward(self, input: 'torch.Tensor') ->torch.Tensor: w_times_x = torch.bmm(input, self.weight) return torch.add(w_times_x, self.bias[:, None, :]) def extra_repr(self) ->str: return 'in_features={}, out_features={}, bias={}'.format(self. in_features, self.out_features, self.bias is not None) class EnsembleModelNew(nn.Module): def __init__(self, feature_size, ensemble_size, use_decay=False): super(EnsembleModelNew, self).__init__() self.nn1 = EnsembleFC(feature_size + feature_size, feature_size, ensemble_size, weight_decay=2.5e-05) self.use_decay = use_decay self.apply(weights_init_) self.swish = Swish() def get_decay_loss(self): decay_loss = 0.0 for m in self.children(): if isinstance(m, EnsembleFC): decay_loss += m.weight_decay * torch.sum(torch.square(m.weight) ) / 2.0 return decay_loss def loss(self, mean, labels): """ mean, logvar: Ensemble_size x N x dim labels: N x dim """ assert len(mean.shape) == len(labels.shape) == 3 mse_loss = torch.mean(torch.pow(mean - labels, 2), dim=(1, 2)) total_loss = torch.sum(mse_loss) return total_loss, mse_loss def forward(self, input_0, input_1): primals_3 = self.nn1.weight primals_4 = self.nn1.bias primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
si0wang/transfer_dmc
EnsembleModel
false
12,988
[ "MIT" ]
0
6bda773244e0b709b3c13add2597f5f1cd01bfd7
https://github.com/si0wang/transfer_dmc/tree/6bda773244e0b709b3c13add2597f5f1cd01bfd7
Quantization
import torch import torch.utils.data import torch.nn as nn class Quant(torch.autograd.Function): @staticmethod def forward(ctx, input): input = torch.clamp(input, 0, 1) output = (input * 255.0).round() / 255.0 return output @staticmethod def backward(ctx, grad_output): return grad_output class Quantization(nn.Module): def __init__(self): super(Quantization, self).__init__() def forward(self, input): return Quant.apply(input) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.utils.data import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_clamp_div_mul_round_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) tmp5 = 255.0 tmp6 = tmp4 * tmp5 tmp7 = libdevice.nearbyint(tmp6) tmp8 = 0.00392156862745098 tmp9 = tmp7 * tmp8 tl.store(out_ptr0 + x0, tmp9, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clamp_div_mul_round_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, class Quant(torch.autograd.Function): @staticmethod def forward(ctx, input): input = torch.clamp(input, 0, 1) output = (input * 255.0).round() / 255.0 return output @staticmethod def backward(ctx, grad_output): return grad_output class QuantizationNew(nn.Module): def __init__(self): super(QuantizationNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
skipper17/Invertible-Image-Rescaling
Quantization
false
12,989
[ "Apache-2.0" ]
0
4755f21faa5f7c4599dfb971a875ecee86bc35a1
https://github.com/skipper17/Invertible-Image-Rescaling/tree/4755f21faa5f7c4599dfb971a875ecee86bc35a1
FocalLoss
import torch from torch import nn import torch.nn.functional as F class FocalLoss(nn.Module): def __init__(self, gamma=2): super().__init__() self.gamma = gamma def forward(self, logit, target, epoch=0): target = target.float() max_val = (-logit).clamp(min=0) loss = logit - logit * target + max_val + ((-max_val).exp() + (- logit - max_val).exp()).log() invprobs = F.logsigmoid(-logit * (target * 2.0 - 1.0)) loss = (invprobs * self.gamma).exp() * loss if len(loss.size()) == 2: loss = loss.sum(dim=1) 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 libdevice, math as tl_math from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_clamp_exp_log_log_sigmoid_forward_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) tmp2 = tl.load(in_ptr1 + r0, None) tmp1 = -tmp0 tmp3 = 2.0 tmp4 = tmp2 * tmp3 tmp5 = 1.0 tmp6 = tmp4 - tmp5 tmp7 = tmp1 * tmp6 tmp8 = 0.0 tmp9 = triton_helpers.minimum(tmp8, tmp7) tmp10 = tl_math.abs(tmp7) tmp11 = -tmp10 tmp12 = tl_math.exp(tmp11) tmp13 = libdevice.log1p(tmp12) tmp14 = tmp9 - tmp13 tmp15 = tmp14 * tmp3 tmp16 = tl_math.exp(tmp15) tmp17 = tmp0 * tmp2 tmp18 = tmp0 - tmp17 tmp19 = triton_helpers.maximum(tmp1, tmp8) tmp20 = tmp18 + tmp19 tmp21 = -tmp19 tmp22 = tl_math.exp(tmp21) tmp23 = tmp1 - tmp19 tmp24 = tl_math.exp(tmp23) tmp25 = tmp22 + tmp24 tmp26 = tl_math.log(tmp25) tmp27 = tmp20 + tmp26 tmp28 = tmp16 * tmp27 tmp29 = tl.broadcast_to(tmp28, [RBLOCK]) tmp31 = triton_helpers.promote_to_tensor(tl.sum(tmp29, 0)) tmp32 = 256.0 tmp33 = tmp31 / tmp32 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp33, 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_exp_log_log_sigmoid_forward_mean_mul_neg_sub_0[ grid(1)](buf1, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, class FocalLossNew(nn.Module): def __init__(self, gamma=2): super().__init__() self.gamma = gamma def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
sin1012/kaggle_baidu_autonomous_driving
FocalLoss
false
12,990
[ "Apache-2.0" ]
0
afa0da4fc06a05548306b885c6c804881104b403
https://github.com/sin1012/kaggle_baidu_autonomous_driving/tree/afa0da4fc06a05548306b885c6c804881104b403
PretrainedUNet
import torch import torchvision class Block(torch.nn.Module): def __init__(self, in_channels, mid_channel, out_channels, batch_norm=False ): super().__init__() self.conv1 = torch.nn.Conv2d(in_channels=in_channels, out_channels= mid_channel, kernel_size=3, padding=1) self.conv2 = torch.nn.Conv2d(in_channels=mid_channel, out_channels= out_channels, kernel_size=3, padding=1) self.batch_norm = batch_norm if batch_norm: self.bn1 = torch.nn.BatchNorm2d(mid_channel) self.bn2 = torch.nn.BatchNorm2d(out_channels) def forward(self, x): x = self.conv1(x) if self.batch_norm: x = self.bn1(x) x = torch.nn.ReLU(inplace=True)(x) x = self.conv2(x) if self.batch_norm: x = self.bn2(x) out = torch.nn.ReLU(inplace=True)(x) return out class PretrainedUNet(torch.nn.Module): def up(self, x, size): return torch.nn.functional.interpolate(x, size=size, mode=self. upscale_mode) def down(self, x): return torch.nn.MaxPool2d(kernel_size=2)(x) def __init__(self, in_channels, out_channels, batch_norm=False, upscale_mode='nearest'): super().__init__() self.in_channels = in_channels self.out_channels = out_channels self.batch_norm = batch_norm self.upscale_mode = upscale_mode self.init_conv = torch.nn.Conv2d(in_channels, 3, 1) endcoder = torchvision.models.vgg11(pretrained=True).features self.conv1 = endcoder[0] self.conv2 = endcoder[3] self.conv3 = endcoder[6] self.conv3s = endcoder[8] self.conv4 = endcoder[11] self.conv4s = endcoder[13] self.conv5 = endcoder[16] self.conv5s = endcoder[18] self.center = Block(512, 512, 256, batch_norm) self.dec5 = Block(512 + 256, 512, 256, batch_norm) self.dec4 = Block(512 + 256, 512, 128, batch_norm) self.dec3 = Block(256 + 128, 256, 64, batch_norm) self.dec2 = Block(128 + 64, 128, 32, batch_norm) self.dec1 = Block(64 + 32, 64, 32, batch_norm) self.out = torch.nn.Conv2d(in_channels=32, out_channels= out_channels, kernel_size=1) def forward(self, x): init_conv = torch.nn.ReLU(inplace=True)(self.init_conv(x)) enc1 = torch.nn.ReLU(inplace=True)(self.conv1(init_conv)) enc2 = torch.nn.ReLU(inplace=True)(self.conv2(self.down(enc1))) enc3 = torch.nn.ReLU(inplace=True)(self.conv3(self.down(enc2))) enc3 = torch.nn.ReLU(inplace=True)(self.conv3s(enc3)) enc4 = torch.nn.ReLU(inplace=True)(self.conv4(self.down(enc3))) enc4 = torch.nn.ReLU(inplace=True)(self.conv4s(enc4)) enc5 = torch.nn.ReLU(inplace=True)(self.conv5(self.down(enc4))) enc5 = torch.nn.ReLU(inplace=True)(self.conv5s(enc5)) center = self.center(self.down(enc5)) dec5 = self.dec5(torch.cat([self.up(center, enc5.size()[-2:]), enc5 ], 1)) dec4 = self.dec4(torch.cat([self.up(dec5, enc4.size()[-2:]), enc4], 1)) dec3 = self.dec3(torch.cat([self.up(dec4, enc3.size()[-2:]), enc3], 1)) dec2 = self.dec2(torch.cat([self.up(dec3, enc2.size()[-2:]), enc2], 1)) dec1 = self.dec1(torch.cat([self.up(dec2, enc1.size()[-2:]), enc1], 1)) out = self.out(dec1) return out def get_inputs(): return [torch.rand([4, 4, 64, 64])] 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 torchvision 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): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 3 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 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_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 % 64 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_2(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 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 128 * x1), None, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 128 * x1), None, eviction_policy ='evict_last') tmp3 = tl.load(in_ptr0 + (64 + 2 * x0 + 128 * x1), None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (65 + 2 * x0 + 128 * x1), None, eviction_policy='evict_last') tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + x2, tmp6, None) tl.store(out_ptr1 + x2, tmp16, None) @triton.jit def triton_poi_fused_convolution_relu_3(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 // 1024 % 128 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_4(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 % 16 x1 = xindex // 16 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 64 * x1), None, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 64 * x1), None, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (32 + 2 * x0 + 64 * x1), None, eviction_policy ='evict_last') tmp5 = tl.load(in_ptr0 + (33 + 2 * x0 + 64 * x1), None, eviction_policy ='evict_last') tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + x2, tmp6, None) tl.store(out_ptr1 + x2, tmp16, None) @triton.jit def triton_poi_fused_convolution_relu_5(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 // 256 % 256 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_6(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 % 8 x1 = xindex // 8 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 32 * x1), None, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 32 * x1), None, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (16 + 2 * x0 + 32 * x1), None, eviction_policy ='evict_last') tmp5 = tl.load(in_ptr0 + (17 + 2 * x0 + 32 * x1), None, eviction_policy ='evict_last') tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + x2, tmp6, None) tl.store(out_ptr1 + x2, tmp16, None) @triton.jit def triton_poi_fused_convolution_relu_7(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 // 64 % 512 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_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 % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 16 * x1), None, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 16 * x1), None, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (8 + 2 * x0 + 16 * x1), None, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (9 + 2 * x0 + 16 * x1), None, eviction_policy= 'evict_last') tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + x2, tmp6, None) tl.store(out_ptr1 + x2, tmp16, None) @triton.jit def triton_poi_fused_convolution_relu_9(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 16 % 512 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_10(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 % 2 x1 = xindex // 2 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 8 * x1), None, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 8 * x1), None, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (4 + 2 * x0 + 8 * x1), None, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (5 + 2 * x0 + 8 * x1), None, eviction_policy= 'evict_last') tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + x2, tmp6, None) tl.store(out_ptr1 + x2, tmp16, None) @triton.jit def triton_poi_fused_convolution_relu_11(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 // 4 % 512 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__to_copy_add_arange_mul_12(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp3.to(tl.int32) tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_poi_fused_cat_13(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex // 16 % 768 x1 = xindex // 4 % 4 x0 = xindex % 4 x3 = xindex // 12288 x4 = xindex % 16 x5 = xindex tmp0 = x2 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 256, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + x1, tmp4, eviction_policy='evict_last', other=0.0) tmp6 = tl.full([XBLOCK], 2, tl.int32) tmp7 = tmp5 + tmp6 tmp8 = tmp5 < 0 tmp9 = tl.where(tmp8, tmp7, tmp5) tmp10 = tl.load(in_ptr0 + x0, tmp4, eviction_policy='evict_last', other=0.0 ) tmp11 = tmp10 + tmp6 tmp12 = tmp10 < 0 tmp13 = tl.where(tmp12, tmp11, tmp10) tmp14 = tl.load(in_ptr1 + (tmp13 + 2 * tmp9 + 4 * x2 + 1024 * x3), tmp4, eviction_policy='evict_last', other=0.0) tmp15 = tl.load(in_ptr2 + x2, tmp4, eviction_policy='evict_last', other=0.0 ) tmp16 = tmp14 + tmp15 tmp17 = tl.full([1], 0, tl.int32) tmp18 = triton_helpers.maximum(tmp17, tmp16) tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype) tmp20 = tl.where(tmp4, tmp18, tmp19) tmp21 = tmp0 >= tmp3 tl.full([1], 768, tl.int64) tmp24 = tl.load(in_ptr3 + (x4 + 16 * (-256 + x2) + 8192 * x3), tmp21, other=0.0) tmp25 = tl.where(tmp4, tmp20, tmp24) tl.store(out_ptr0 + x5, tmp25, None) @triton.jit def triton_poi_fused__to_copy_add_arange_mul_14(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 8 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp3.to(tl.int32) tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_poi_fused_cat_15(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex // 64 % 768 x1 = xindex // 8 % 8 x0 = xindex % 8 x3 = xindex // 49152 x4 = xindex % 64 x5 = xindex tmp0 = x2 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 256, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + x1, tmp4, eviction_policy='evict_last', other=0.0) tmp6 = tl.full([XBLOCK], 4, tl.int32) tmp7 = tmp5 + tmp6 tmp8 = tmp5 < 0 tmp9 = tl.where(tmp8, tmp7, tmp5) tmp10 = tl.load(in_ptr0 + x0, tmp4, eviction_policy='evict_last', other=0.0 ) tmp11 = tmp10 + tmp6 tmp12 = tmp10 < 0 tmp13 = tl.where(tmp12, tmp11, tmp10) tmp14 = tl.load(in_ptr1 + (tmp13 + 4 * tmp9 + 16 * x2 + 4096 * x3), tmp4, eviction_policy='evict_last', other=0.0) tmp15 = tl.load(in_ptr2 + x2, tmp4, eviction_policy='evict_last', other=0.0 ) tmp16 = tmp14 + tmp15 tmp17 = tl.full([1], 0, tl.int32) tmp18 = triton_helpers.maximum(tmp17, tmp16) tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype) tmp20 = tl.where(tmp4, tmp18, tmp19) tmp21 = tmp0 >= tmp3 tl.full([1], 768, tl.int64) tmp24 = tl.load(in_ptr3 + (x4 + 64 * (-256 + x2) + 32768 * x3), tmp21, other=0.0) tmp25 = tl.where(tmp4, tmp20, tmp24) tl.store(out_ptr0 + x5, tmp25, None) @triton.jit def triton_poi_fused__to_copy_add_arange_mul_16(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp3.to(tl.int32) tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_poi_fused_cat_17(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex // 256 % 384 x1 = xindex // 16 % 16 x0 = xindex % 16 x3 = xindex // 98304 x4 = xindex % 256 x5 = xindex tmp0 = x2 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 128, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + x1, tmp4, eviction_policy='evict_last', other=0.0) tmp6 = tl.full([XBLOCK], 8, tl.int32) tmp7 = tmp5 + tmp6 tmp8 = tmp5 < 0 tmp9 = tl.where(tmp8, tmp7, tmp5) tmp10 = tl.load(in_ptr0 + x0, tmp4, eviction_policy='evict_last', other=0.0 ) tmp11 = tmp10 + tmp6 tmp12 = tmp10 < 0 tmp13 = tl.where(tmp12, tmp11, tmp10) tmp14 = tl.load(in_ptr1 + (tmp13 + 8 * tmp9 + 64 * x2 + 8192 * x3), tmp4, eviction_policy='evict_last', other=0.0) tmp15 = tl.load(in_ptr2 + x2, tmp4, eviction_policy='evict_last', other=0.0 ) tmp16 = tmp14 + tmp15 tmp17 = tl.full([1], 0, tl.int32) tmp18 = triton_helpers.maximum(tmp17, tmp16) tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype) tmp20 = tl.where(tmp4, tmp18, tmp19) tmp21 = tmp0 >= tmp3 tl.full([1], 384, tl.int64) tmp24 = tl.load(in_ptr3 + (x4 + 256 * (-128 + x2) + 65536 * x3), tmp21, other=0.0) tmp25 = tl.where(tmp4, tmp20, tmp24) tl.store(out_ptr0 + x5, tmp25, None) @triton.jit def triton_poi_fused__to_copy_add_arange_mul_18(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp3.to(tl.int32) tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_poi_fused_cat_19(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex // 1024 % 192 x1 = xindex // 32 % 32 x0 = xindex % 32 x3 = xindex // 196608 x4 = xindex % 1024 x5 = xindex tmp0 = x2 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 64, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + x1, tmp4, eviction_policy='evict_last', other=0.0) tmp6 = tl.full([XBLOCK], 16, tl.int32) tmp7 = tmp5 + tmp6 tmp8 = tmp5 < 0 tmp9 = tl.where(tmp8, tmp7, tmp5) tmp10 = tl.load(in_ptr0 + x0, tmp4, eviction_policy='evict_last', other=0.0 ) tmp11 = tmp10 + tmp6 tmp12 = tmp10 < 0 tmp13 = tl.where(tmp12, tmp11, tmp10) tmp14 = tl.load(in_ptr1 + (tmp13 + 16 * tmp9 + 256 * x2 + 16384 * x3), tmp4, eviction_policy='evict_last', other=0.0) tmp15 = tl.load(in_ptr2 + x2, tmp4, eviction_policy='evict_last', other=0.0 ) tmp16 = tmp14 + tmp15 tmp17 = tl.full([1], 0, tl.int32) tmp18 = triton_helpers.maximum(tmp17, tmp16) tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype) tmp20 = tl.where(tmp4, tmp18, tmp19) tmp21 = tmp0 >= tmp3 tl.full([1], 192, tl.int64) tmp24 = tl.load(in_ptr3 + (x4 + 1024 * (-64 + x2) + 131072 * x3), tmp21, other=0.0) tmp25 = tl.where(tmp4, tmp20, tmp24) tl.store(out_ptr0 + x5, tmp25, None) @triton.jit def triton_poi_fused__to_copy_add_arange_mul_20(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp3.to(tl.int32) tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_poi_fused_cat_21(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex // 4096 % 96 x1 = xindex // 64 % 64 x0 = xindex % 64 x3 = xindex // 393216 x4 = xindex % 4096 x5 = xindex tmp0 = x2 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 32, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + x1, tmp4, eviction_policy='evict_last', other=0.0) tmp6 = tl.full([XBLOCK], 32, tl.int32) tmp7 = tmp5 + tmp6 tmp8 = tmp5 < 0 tmp9 = tl.where(tmp8, tmp7, tmp5) tmp10 = tl.load(in_ptr0 + x0, tmp4, eviction_policy='evict_last', other=0.0 ) tmp11 = tmp10 + tmp6 tmp12 = tmp10 < 0 tmp13 = tl.where(tmp12, tmp11, tmp10) tmp14 = tl.load(in_ptr1 + (tmp13 + 32 * tmp9 + 1024 * x2 + 32768 * x3), tmp4, eviction_policy='evict_last', other=0.0) tmp15 = tl.load(in_ptr2 + x2, tmp4, eviction_policy='evict_last', other=0.0 ) tmp16 = tmp14 + tmp15 tmp17 = tl.full([1], 0, tl.int32) tmp18 = triton_helpers.maximum(tmp17, tmp16) tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype) tmp20 = tl.where(tmp4, tmp18, tmp19) tmp21 = tmp0 >= tmp3 tl.full([1], 96, tl.int64) tmp24 = tl.load(in_ptr3 + (x4 + 4096 * (-32 + x2) + 262144 * x3), tmp21, other=0.0) tmp25 = tl.where(tmp4, tmp20, tmp24) tl.store(out_ptr0 + x5, tmp25, None) @triton.jit def triton_poi_fused_convolution_relu_22(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 32 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_convolution_23(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 tl.store(in_out_ptr0 + x3, tmp2, None) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_24(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 1024 % 32 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(out_ptr0 + x3, tmp6, None) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_25(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 256 % 64 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(out_ptr0 + x3, tmp6, None) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_26(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 64 % 128 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(out_ptr0 + x3, tmp6, None) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_27(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 16 % 256 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(out_ptr0 + x3, tmp6, None) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_28(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4 % 256 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(out_ptr0 + x3, tmp6, None) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16, primals_17, primals_18, primals_19, primals_20, primals_21, primals_22, primals_23, primals_24, primals_25, primals_26, primals_27, primals_28, primals_29, primals_30, primals_31, primals_32, primals_33, primals_34, primals_35, primals_36, primals_37, primals_38, primals_39, primals_40, primals_41, primals_42, primals_43, primals_44, primals_45) = args args.clear() assert_size_stride(primals_1, (3, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_2, (3,), (1,)) assert_size_stride(primals_3, (4, 4, 64, 64), (16384, 4096, 64, 1)) assert_size_stride(primals_4, (64, 3, 3, 3), (27, 9, 3, 1)) assert_size_stride(primals_5, (64,), (1,)) assert_size_stride(primals_6, (128, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_7, (128,), (1,)) assert_size_stride(primals_8, (256, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_9, (256,), (1,)) assert_size_stride(primals_10, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_11, (256,), (1,)) assert_size_stride(primals_12, (512, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_13, (512,), (1,)) assert_size_stride(primals_14, (512, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_15, (512,), (1,)) assert_size_stride(primals_16, (512, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_17, (512,), (1,)) assert_size_stride(primals_18, (512, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_19, (512,), (1,)) assert_size_stride(primals_20, (512, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_21, (512,), (1,)) assert_size_stride(primals_22, (256, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_23, (256,), (1,)) assert_size_stride(primals_24, (512, 768, 3, 3), (6912, 9, 3, 1)) assert_size_stride(primals_25, (512,), (1,)) assert_size_stride(primals_26, (256, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_27, (256,), (1,)) assert_size_stride(primals_28, (512, 768, 3, 3), (6912, 9, 3, 1)) assert_size_stride(primals_29, (512,), (1,)) assert_size_stride(primals_30, (128, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_31, (128,), (1,)) assert_size_stride(primals_32, (256, 384, 3, 3), (3456, 9, 3, 1)) assert_size_stride(primals_33, (256,), (1,)) assert_size_stride(primals_34, (64, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_35, (64,), (1,)) assert_size_stride(primals_36, (128, 192, 3, 3), (1728, 9, 3, 1)) assert_size_stride(primals_37, (128,), (1,)) assert_size_stride(primals_38, (32, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_39, (32,), (1,)) assert_size_stride(primals_40, (64, 96, 3, 3), (864, 9, 3, 1)) assert_size_stride(primals_41, (64,), (1,)) assert_size_stride(primals_42, (32, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_43, (32,), (1,)) assert_size_stride(primals_44, (4, 32, 1, 1), (32, 1, 1, 1)) assert_size_stride(primals_45, (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, 3, 64, 64), (12288, 4096, 64, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(49152)](buf1, primals_2, 49152, XBLOCK=256, 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, 64, 64, 64), (262144, 4096, 64, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_relu_1[grid(1048576)](buf3, primals_5, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((4, 64, 32, 32), (65536, 1024, 32, 1), torch.float32) buf5 = empty_strided_cuda((4, 64, 32, 32), (65536, 1024, 32, 1), torch.int8) triton_poi_fused_max_pool2d_with_indices_2[grid(262144)](buf3, buf4, buf5, 262144, XBLOCK=512, num_warps=8, num_stages=1) buf6 = extern_kernels.convolution(buf4, primals_6, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf6, (4, 128, 32, 32), (131072, 1024, 32, 1)) buf7 = buf6 del buf6 triton_poi_fused_convolution_relu_3[grid(524288)](buf7, primals_7, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_7 buf8 = empty_strided_cuda((4, 128, 16, 16), (32768, 256, 16, 1), torch.float32) buf9 = empty_strided_cuda((4, 128, 16, 16), (32768, 256, 16, 1), torch.int8) triton_poi_fused_max_pool2d_with_indices_4[grid(131072)](buf7, buf8, buf9, 131072, XBLOCK=512, num_warps=8, num_stages=1) buf10 = extern_kernels.convolution(buf8, primals_8, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf10, (4, 256, 16, 16), (65536, 256, 16, 1)) buf11 = buf10 del buf10 triton_poi_fused_convolution_relu_5[grid(262144)](buf11, primals_9, 262144, XBLOCK=512, num_warps=8, num_stages=1) del primals_9 buf12 = extern_kernels.convolution(buf11, primals_10, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf12, (4, 256, 16, 16), (65536, 256, 16, 1)) buf13 = buf12 del buf12 triton_poi_fused_convolution_relu_5[grid(262144)](buf13, primals_11, 262144, XBLOCK=512, num_warps=8, num_stages=1) del primals_11 buf14 = empty_strided_cuda((4, 256, 8, 8), (16384, 64, 8, 1), torch .float32) buf15 = empty_strided_cuda((4, 256, 8, 8), (16384, 64, 8, 1), torch .int8) triton_poi_fused_max_pool2d_with_indices_6[grid(65536)](buf13, buf14, buf15, 65536, XBLOCK=256, num_warps=4, num_stages=1) buf16 = extern_kernels.convolution(buf14, primals_12, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf16, (4, 512, 8, 8), (32768, 64, 8, 1)) buf17 = buf16 del buf16 triton_poi_fused_convolution_relu_7[grid(131072)](buf17, primals_13, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del primals_13 buf18 = extern_kernels.convolution(buf17, primals_14, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf18, (4, 512, 8, 8), (32768, 64, 8, 1)) buf19 = buf18 del buf18 triton_poi_fused_convolution_relu_7[grid(131072)](buf19, primals_15, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del primals_15 buf20 = empty_strided_cuda((4, 512, 4, 4), (8192, 16, 4, 1), torch. float32) buf21 = empty_strided_cuda((4, 512, 4, 4), (8192, 16, 4, 1), torch.int8 ) triton_poi_fused_max_pool2d_with_indices_8[grid(32768)](buf19, buf20, buf21, 32768, XBLOCK=128, num_warps=4, num_stages=1) buf22 = extern_kernels.convolution(buf20, primals_16, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf22, (4, 512, 4, 4), (8192, 16, 4, 1)) buf23 = buf22 del buf22 triton_poi_fused_convolution_relu_9[grid(32768)](buf23, primals_17, 32768, XBLOCK=256, num_warps=4, num_stages=1) del primals_17 buf24 = extern_kernels.convolution(buf23, primals_18, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf24, (4, 512, 4, 4), (8192, 16, 4, 1)) buf25 = buf24 del buf24 triton_poi_fused_convolution_relu_9[grid(32768)](buf25, primals_19, 32768, XBLOCK=256, num_warps=4, num_stages=1) del primals_19 buf26 = empty_strided_cuda((4, 512, 2, 2), (2048, 4, 2, 1), torch. float32) buf27 = empty_strided_cuda((4, 512, 2, 2), (2048, 4, 2, 1), torch.int8) triton_poi_fused_max_pool2d_with_indices_10[grid(8192)](buf25, buf26, buf27, 8192, XBLOCK=128, num_warps=4, num_stages=1) buf28 = extern_kernels.convolution(buf26, primals_20, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf28, (4, 512, 2, 2), (2048, 4, 2, 1)) buf29 = buf28 del buf28 triton_poi_fused_convolution_relu_11[grid(8192)](buf29, primals_21, 8192, XBLOCK=256, num_warps=4, num_stages=1) del primals_21 buf30 = extern_kernels.convolution(buf29, primals_22, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf30, (4, 256, 2, 2), (1024, 4, 2, 1)) buf31 = empty_strided_cuda((4,), (1,), torch.int64) triton_poi_fused__to_copy_add_arange_mul_12[grid(4)](buf31, 4, XBLOCK=4, num_warps=1, num_stages=1) buf32 = empty_strided_cuda((4, 768, 4, 4), (12288, 16, 4, 1), torch .float32) triton_poi_fused_cat_13[grid(49152)](buf31, buf30, primals_23, buf25, buf32, 49152, XBLOCK=512, num_warps=4, num_stages=1) buf33 = extern_kernels.convolution(buf32, primals_24, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf33, (4, 512, 4, 4), (8192, 16, 4, 1)) buf34 = buf33 del buf33 triton_poi_fused_convolution_relu_9[grid(32768)](buf34, primals_25, 32768, XBLOCK=256, num_warps=4, num_stages=1) del primals_25 buf35 = extern_kernels.convolution(buf34, primals_26, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf35, (4, 256, 4, 4), (4096, 16, 4, 1)) buf36 = empty_strided_cuda((8,), (1,), torch.int64) triton_poi_fused__to_copy_add_arange_mul_14[grid(8)](buf36, 8, XBLOCK=8, num_warps=1, num_stages=1) buf37 = empty_strided_cuda((4, 768, 8, 8), (49152, 64, 8, 1), torch .float32) triton_poi_fused_cat_15[grid(196608)](buf36, buf35, primals_27, buf19, buf37, 196608, XBLOCK=1024, num_warps=4, num_stages=1) buf38 = extern_kernels.convolution(buf37, primals_28, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf38, (4, 512, 8, 8), (32768, 64, 8, 1)) buf39 = buf38 del buf38 triton_poi_fused_convolution_relu_7[grid(131072)](buf39, primals_29, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del primals_29 buf40 = extern_kernels.convolution(buf39, primals_30, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf40, (4, 128, 8, 8), (8192, 64, 8, 1)) buf41 = empty_strided_cuda((16,), (1,), torch.int64) triton_poi_fused__to_copy_add_arange_mul_16[grid(16)](buf41, 16, XBLOCK=16, num_warps=1, num_stages=1) buf42 = empty_strided_cuda((4, 384, 16, 16), (98304, 256, 16, 1), torch.float32) triton_poi_fused_cat_17[grid(393216)](buf41, buf40, primals_31, buf13, buf42, 393216, XBLOCK=512, num_warps=8, num_stages=1) buf43 = extern_kernels.convolution(buf42, primals_32, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf43, (4, 256, 16, 16), (65536, 256, 16, 1)) buf44 = buf43 del buf43 triton_poi_fused_convolution_relu_5[grid(262144)](buf44, primals_33, 262144, XBLOCK=512, num_warps=8, num_stages=1) del primals_33 buf45 = extern_kernels.convolution(buf44, primals_34, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf45, (4, 64, 16, 16), (16384, 256, 16, 1)) buf46 = empty_strided_cuda((32,), (1,), torch.int64) triton_poi_fused__to_copy_add_arange_mul_18[grid(32)](buf46, 32, XBLOCK=32, num_warps=1, num_stages=1) buf47 = empty_strided_cuda((4, 192, 32, 32), (196608, 1024, 32, 1), torch.float32) triton_poi_fused_cat_19[grid(786432)](buf46, buf45, primals_35, buf7, buf47, 786432, XBLOCK=1024, num_warps=4, num_stages=1) buf48 = extern_kernels.convolution(buf47, primals_36, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf48, (4, 128, 32, 32), (131072, 1024, 32, 1)) buf49 = buf48 del buf48 triton_poi_fused_convolution_relu_3[grid(524288)](buf49, primals_37, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_37 buf50 = extern_kernels.convolution(buf49, primals_38, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf50, (4, 32, 32, 32), (32768, 1024, 32, 1)) buf51 = empty_strided_cuda((64,), (1,), torch.int64) triton_poi_fused__to_copy_add_arange_mul_20[grid(64)](buf51, 64, XBLOCK=64, num_warps=1, num_stages=1) buf52 = empty_strided_cuda((4, 96, 64, 64), (393216, 4096, 64, 1), torch.float32) triton_poi_fused_cat_21[grid(1572864)](buf51, buf50, primals_39, buf3, buf52, 1572864, XBLOCK=1024, num_warps=4, num_stages=1) buf53 = extern_kernels.convolution(buf52, primals_40, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf53, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf54 = buf53 del buf53 triton_poi_fused_convolution_relu_1[grid(1048576)](buf54, primals_41, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del primals_41 buf55 = extern_kernels.convolution(buf54, primals_42, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf55, (4, 32, 64, 64), (131072, 4096, 64, 1)) buf56 = buf55 del buf55 triton_poi_fused_convolution_relu_22[grid(524288)](buf56, primals_43, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_43 buf57 = extern_kernels.convolution(buf56, primals_44, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf57, (4, 4, 64, 64), (16384, 4096, 64, 1)) buf58 = buf57 del buf57 triton_poi_fused_convolution_23[grid(65536)](buf58, primals_45, 65536, XBLOCK=512, num_warps=4, num_stages=1) del primals_45 buf59 = empty_strided_cuda((4, 32, 32, 32), (32768, 1024, 32, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_24[grid(131072)]( buf50, primals_39, buf59, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del buf50 del primals_39 buf60 = empty_strided_cuda((4, 64, 16, 16), (16384, 256, 16, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_25[grid(65536)]( buf45, primals_35, buf60, 65536, XBLOCK=256, num_warps=4, num_stages=1) del buf45 del primals_35 buf61 = empty_strided_cuda((4, 128, 8, 8), (8192, 64, 8, 1), torch.bool ) triton_poi_fused_convolution_relu_threshold_backward_26[grid(32768)]( buf40, primals_31, buf61, 32768, XBLOCK=128, num_warps=4, num_stages=1) del buf40 del primals_31 buf62 = empty_strided_cuda((4, 256, 4, 4), (4096, 16, 4, 1), torch.bool ) triton_poi_fused_convolution_relu_threshold_backward_27[grid(16384)]( buf35, primals_27, buf62, 16384, XBLOCK=128, num_warps=4, num_stages=1) del buf35 del primals_27 buf63 = empty_strided_cuda((4, 256, 2, 2), (1024, 4, 2, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_28[grid(4096)]( buf30, primals_23, buf63, 4096, XBLOCK=128, num_warps=4, num_stages=1) del buf30 del primals_23 return (buf58, primals_1, primals_3, primals_4, primals_6, primals_8, primals_10, primals_12, primals_14, primals_16, primals_18, primals_20, primals_22, primals_24, primals_26, primals_28, primals_30, primals_32, primals_34, primals_36, primals_38, primals_40, primals_42, primals_44, buf1, buf3, buf4, buf5, buf7, buf8, buf9, buf11, buf13, buf14, buf15, buf17, buf19, buf20, buf21, buf23, buf25, buf26, buf27, buf29, buf31, buf32, buf34, buf36, buf37, buf39, buf41, buf42, buf44, buf46, buf47, buf49, buf51, buf52, buf54, buf56, buf59, buf60, buf61, buf62, buf63) class Block(torch.nn.Module): def __init__(self, in_channels, mid_channel, out_channels, batch_norm=False ): super().__init__() self.conv1 = torch.nn.Conv2d(in_channels=in_channels, out_channels= mid_channel, kernel_size=3, padding=1) self.conv2 = torch.nn.Conv2d(in_channels=mid_channel, out_channels= out_channels, kernel_size=3, padding=1) self.batch_norm = batch_norm if batch_norm: self.bn1 = torch.nn.BatchNorm2d(mid_channel) self.bn2 = torch.nn.BatchNorm2d(out_channels) def forward(self, x): x = self.conv1(x) if self.batch_norm: x = self.bn1(x) x = torch.nn.ReLU(inplace=True)(x) x = self.conv2(x) if self.batch_norm: x = self.bn2(x) out = torch.nn.ReLU(inplace=True)(x) return out class PretrainedUNetNew(torch.nn.Module): def up(self, x, size): return torch.nn.functional.interpolate(x, size=size, mode=self. upscale_mode) def down(self, x): return torch.nn.MaxPool2d(kernel_size=2)(x) def __init__(self, in_channels, out_channels, batch_norm=False, upscale_mode='nearest'): super().__init__() self.in_channels = in_channels self.out_channels = out_channels self.batch_norm = batch_norm self.upscale_mode = upscale_mode self.init_conv = torch.nn.Conv2d(in_channels, 3, 1) endcoder = torchvision.models.vgg11(pretrained=True).features self.conv1 = endcoder[0] self.conv2 = endcoder[3] self.conv3 = endcoder[6] self.conv3s = endcoder[8] self.conv4 = endcoder[11] self.conv4s = endcoder[13] self.conv5 = endcoder[16] self.conv5s = endcoder[18] self.center = Block(512, 512, 256, batch_norm) self.dec5 = Block(512 + 256, 512, 256, batch_norm) self.dec4 = Block(512 + 256, 512, 128, batch_norm) self.dec3 = Block(256 + 128, 256, 64, batch_norm) self.dec2 = Block(128 + 64, 128, 32, batch_norm) self.dec1 = Block(64 + 32, 64, 32, batch_norm) self.out = torch.nn.Conv2d(in_channels=32, out_channels= out_channels, kernel_size=1) def forward(self, input_0): primals_1 = self.init_conv.weight primals_2 = self.init_conv.bias primals_4 = self.conv1.weight primals_5 = self.conv1.bias primals_6 = self.conv2.weight primals_7 = self.conv2.bias primals_8 = self.conv3.weight primals_9 = self.conv3.bias primals_10 = self.conv3s.weight primals_11 = self.conv3s.bias primals_12 = self.conv4.weight primals_13 = self.conv4.bias primals_14 = self.conv4s.weight primals_15 = self.conv4s.bias primals_16 = self.conv5.weight primals_17 = self.conv5.bias primals_18 = self.conv5s.weight primals_19 = self.conv5s.bias primals_20 = self.center.conv1.weight primals_21 = self.center.conv1.bias primals_22 = self.center.conv2.weight primals_23 = self.center.conv2.bias primals_24 = self.dec5.conv1.weight primals_25 = self.dec5.conv1.bias primals_26 = self.dec5.conv2.weight primals_27 = self.dec5.conv2.bias primals_28 = self.dec4.conv1.weight primals_29 = self.dec4.conv1.bias primals_30 = self.dec4.conv2.weight primals_31 = self.dec4.conv2.bias primals_32 = self.dec3.conv1.weight primals_33 = self.dec3.conv1.bias primals_34 = self.dec3.conv2.weight primals_35 = self.dec3.conv2.bias primals_36 = self.dec2.conv1.weight primals_37 = self.dec2.conv1.bias primals_38 = self.dec2.conv2.weight primals_39 = self.dec2.conv2.bias primals_40 = self.dec1.conv1.weight primals_41 = self.dec1.conv1.bias primals_42 = self.dec1.conv2.weight primals_43 = self.dec1.conv2.bias primals_44 = self.out.weight primals_45 = self.out.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16, primals_17, primals_18, primals_19, primals_20, primals_21, primals_22, primals_23, primals_24, primals_25, primals_26, primals_27, primals_28, primals_29, primals_30, primals_31, primals_32, primals_33, primals_34, primals_35, primals_36, primals_37, primals_38, primals_39, primals_40, primals_41, primals_42, primals_43, primals_44, primals_45]) return output[0]
amrane99/lung-segmentation
PretrainedUNet
false
12,991
[ "MIT" ]
0
ab29db75ac78918da5cbf66b830acaf36cf7b44a
https://github.com/amrane99/lung-segmentation/tree/ab29db75ac78918da5cbf66b830acaf36cf7b44a
BertSelfAttention
from _paritybench_helpers import _mock_config import math import torch import torch.nn as nn class BertSelfAttention(nn.Module): def __init__(self, config): 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 transform(self, x, linear_layer): bs, seq_len = x.shape[:2] proj = linear_layer(x) proj = proj.view(bs, seq_len, self.num_attention_heads, self. attention_head_size) proj = proj.transpose(1, 2) return proj def attention(self, key, query, value, attention_mask): norm = math.sqrt(key.shape[-1]) scores = torch.matmul(query, key.transpose(-1, -2)) bs = scores.shape[0] s_len = scores.shape[-1] scores = scores / norm scores_n = scores + attention_mask attention = nn.Softmax(dim=-1)(scores_n) v_prime = torch.matmul(attention, value) v_prime = v_prime.permute(0, 2, 1, 3) v_prime = v_prime.contiguous().view(bs, s_len, self.all_head_size) return v_prime def forward(self, hidden_states, attention_mask): """ hidden_states: [bs, seq_len, hidden_state] attention_mask: [bs, 1, 1, seq_len] output: [bs, seq_len, hidden_state] """ key_layer = self.transform(hidden_states, self.key) value_layer = self.transform(hidden_states, self.value) query_layer = self.transform(hidden_states, self.query) attn_value = self.attention(key_layer, query_layer, value_layer, attention_mask) return attn_value 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 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, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 1.0 tmp4 = tmp2 * tmp3 tl.store(out_ptr0 + (x2 + 4 * y3), tmp4, xmask & ymask) @triton.jit def triton_poi_fused_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 16 tmp0 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = triton_helpers.maximum(tmp2, tmp5) tmp9 = tmp7 + tmp8 tmp10 = triton_helpers.maximum(tmp6, tmp9) tmp13 = tmp11 + tmp12 tmp14 = triton_helpers.maximum(tmp10, tmp13) tmp15 = tmp2 - tmp14 tmp16 = tl_math.exp(tmp15) tmp17 = tmp5 - tmp14 tmp18 = tl_math.exp(tmp17) tmp19 = tmp16 + tmp18 tmp20 = tmp9 - tmp14 tmp21 = tl_math.exp(tmp20) tmp22 = tmp19 + tmp21 tmp23 = tmp13 - tmp14 tmp24 = tl_math.exp(tmp23) tmp25 = tmp22 + tmp24 tmp26 = float('-inf') tmp27 = tmp2 == tmp26 tmp28 = tmp27 == 0 tmp29 = tmp28.to(tl.int64) tmp30 = tmp29 != 0 tmp31 = tmp5 == tmp26 tmp32 = tmp31 == 0 tmp33 = tmp32.to(tl.int64) tmp34 = tmp33 != 0 tmp35 = tmp30 | tmp34 tmp36 = tmp9 == tmp26 tmp37 = tmp36 == 0 tmp38 = tmp37.to(tl.int64) tmp39 = tmp38 != 0 tmp40 = tmp35 | tmp39 tmp41 = tmp13 == tmp26 tmp42 = tmp41 == 0 tmp43 = tmp42.to(tl.int64) tmp44 = tmp43 != 0 tmp45 = tmp40 | tmp44 tl.store(out_ptr0 + x2, tmp14, xmask) tl.store(out_ptr1 + x2, tmp25, xmask) tl.store(out_ptr2 + x2, tmp45, xmask) @triton.jit def triton_poi_fused_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex // 4 x4 = xindex x5 = xindex % 64 tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last').to(tl .int1) tmp2 = tl.load(in_out_ptr0 + x4, xmask) tmp3 = tl.load(in_ptr1 + x5, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr2 + x3, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr3 + x3, xmask, eviction_policy='evict_last') tmp1 = tmp0 == 0 tmp4 = tmp2 + tmp3 tmp6 = tmp4 - tmp5 tmp7 = tl_math.exp(tmp6) tmp9 = tmp7 / tmp8 tmp10 = 0.0 tmp11 = tl.where(tmp1, tmp10, tmp9) tl.store(in_out_ptr0 + x4, tmp11, xmask) @triton.jit def triton_poi_fused_3(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask) @triton.jit def triton_poi_fused_clone_4(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8) = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4,), (1,)) 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_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1) del primals_4 buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), out=buf2) del primals_6 buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_0[grid(16, 4)](buf2, primals_7, buf3, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_7 buf4 = reinterpret_tensor(buf2, (4, 4, 1, 4), (16, 4, 4, 1), 0) del buf2 triton_poi_fused_0[grid(16, 4)](buf0, primals_3, buf4, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_3 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(buf0, (4, 4, 4, 1), (16, 4, 1, 64), 0) del buf0 buf7 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf8 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.bool) triton_poi_fused_1[grid(64)](buf5, primals_8, buf6, buf7, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1) buf9 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf5 triton_poi_fused_2[grid(256)](buf9, buf8, primals_8, buf6, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf8 del primals_8 buf10 = reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf7 triton_poi_fused_3[grid(16, 4)](buf1, primals_5, buf10, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_5 buf11 = reinterpret_tensor(buf1, (16, 4, 1), (4, 1, 1), 0) del buf1 extern_kernels.bmm(reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf10, (16, 4, 1), (4, 1, 0), 0), out=buf11) buf12 = reinterpret_tensor(buf6, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf6 triton_poi_fused_clone_4[grid(16, 4)](buf11, buf12, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) del buf11 return reinterpret_tensor(buf12, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(primals_1, (16, 4), (4, 1), 0 ), buf9, reinterpret_tensor(buf10, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 4), 0) class BertSelfAttentionNew(nn.Module): def __init__(self, config): 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 transform(self, x, linear_layer): bs, seq_len = x.shape[:2] proj = linear_layer(x) proj = proj.view(bs, seq_len, self.num_attention_heads, self. attention_head_size) proj = proj.transpose(1, 2) return proj def attention(self, key, query, value, attention_mask): norm = math.sqrt(key.shape[-1]) scores = torch.matmul(query, key.transpose(-1, -2)) bs = scores.shape[0] s_len = scores.shape[-1] scores = scores / norm scores_n = scores + attention_mask attention = nn.Softmax(dim=-1)(scores_n) v_prime = torch.matmul(attention, value) v_prime = v_prime.permute(0, 2, 1, 3) v_prime = v_prime.contiguous().view(bs, s_len, self.all_head_size) return v_prime def forward(self, input_0, input_1): primals_2 = self.query.weight primals_3 = self.query.bias primals_4 = self.key.weight primals_5 = self.key.bias primals_6 = self.value.weight primals_7 = self.value.bias primals_1 = 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]
shrishabh/cs769-assignments
BertSelfAttention
false
12,992
[ "MIT" ]
0
babce1def0d65728bf1d4e4a725d8939f1d5f9a7
https://github.com/shrishabh/cs769-assignments/tree/babce1def0d65728bf1d4e4a725d8939f1d5f9a7
diceloss
import torch class diceloss(torch.nn.Module): def init(self): super(diceloss, self).init() def forward(self, pred, target): smooth = 1.0 iflat = pred.contiguous().view(-1) tflat = target.contiguous().view(-1) intersection = (iflat * tflat).sum() A_sum = torch.sum(iflat * iflat) B_sum = torch.sum(tflat * tflat) return 1 - (2.0 * intersection + smooth) / (A_sum + B_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 assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_div_mul_rsub_sum_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.load(in_ptr1 + r0, None) tmp2 = tmp0 * tmp1 tmp3 = tl.broadcast_to(tmp2, [RBLOCK]) tmp5 = triton_helpers.promote_to_tensor(tl.sum(tmp3, 0)) tmp6 = tmp0 * tmp0 tmp7 = tl.broadcast_to(tmp6, [RBLOCK]) tmp9 = triton_helpers.promote_to_tensor(tl.sum(tmp7, 0)) tmp10 = tmp1 * tmp1 tmp11 = tl.broadcast_to(tmp10, [RBLOCK]) tmp13 = triton_helpers.promote_to_tensor(tl.sum(tmp11, 0)) tmp14 = 2.0 tmp15 = tmp5 * tmp14 tmp16 = 1.0 tmp17 = tmp15 + tmp16 tmp18 = tmp9 + tmp13 tmp19 = tmp18 + tmp16 tmp20 = tmp17 / tmp19 tmp21 = tmp16 - tmp20 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((), (), torch.float32) buf3 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_div_mul_rsub_sum_0[grid(1)](buf3, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf3, class dicelossNew(torch.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]
soffiafdz/nma-dl-modality-mongoose
diceloss
false
12,993
[ "MIT" ]
0
41ac1f2e0e818538bafedae93e5c68f8857411bd
https://github.com/soffiafdz/nma-dl-modality-mongoose/tree/41ac1f2e0e818538bafedae93e5c68f8857411bd
ConvReluPool
import torch from torch.nn import Conv2d from torch import nn from torch.nn import functional as F def Pool(k, stride=1, pad=0): return torch.nn.MaxPool2d(k, stride=stride, padding=pad) class ConvReluPool(nn.Module): def __init__(self, fIn, fOut, k, stride=1, pool=2): super().__init__() self.conv = Conv2d(fIn, fOut, k, stride) self.pool = Pool(k) def forward(self, x): x = self.conv(x) x = F.relu(x) x = self.pool(x) return x def get_inputs(): return [torch.rand([4, 4, 64, 64])] def get_init_inputs(): return [[], {'fIn': 4, 'fOut': 4, 'k': 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.nn import Conv2d from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_convolution_relu_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 59536 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 3721 % 4 x0 = xindex % 3721 x4 = xindex // 3721 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(out_ptr0 + (x0 + 3744 * x4), tmp4, xmask) @triton.jit def triton_poi_fused_max_pool2d_with_indices_1(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 53824 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 58 x1 = xindex // 58 % 58 x2 = xindex // 3364 x3 = xindex x4 = xindex % 3364 tmp0 = tl.load(in_ptr0 + (x0 + 61 * x1 + 3744 * x2), xmask) tmp1 = tl.load(in_ptr0 + (1 + x0 + 61 * x1 + 3744 * x2), xmask) tmp3 = tl.load(in_ptr0 + (2 + x0 + 61 * x1 + 3744 * x2), xmask) tmp5 = tl.load(in_ptr0 + (3 + x0 + 61 * x1 + 3744 * x2), xmask) tmp7 = tl.load(in_ptr0 + (61 + x0 + 61 * x1 + 3744 * x2), xmask) tmp9 = tl.load(in_ptr0 + (62 + x0 + 61 * x1 + 3744 * x2), xmask) tmp11 = tl.load(in_ptr0 + (63 + x0 + 61 * x1 + 3744 * x2), xmask) tmp13 = tl.load(in_ptr0 + (64 + x0 + 61 * x1 + 3744 * x2), xmask) tmp15 = tl.load(in_ptr0 + (122 + x0 + 61 * x1 + 3744 * x2), xmask) tmp17 = tl.load(in_ptr0 + (123 + x0 + 61 * x1 + 3744 * x2), xmask) tmp19 = tl.load(in_ptr0 + (124 + x0 + 61 * x1 + 3744 * x2), xmask) tmp21 = tl.load(in_ptr0 + (125 + x0 + 61 * x1 + 3744 * x2), xmask) tmp23 = tl.load(in_ptr0 + (183 + x0 + 61 * x1 + 3744 * x2), xmask) tmp25 = tl.load(in_ptr0 + (184 + x0 + 61 * x1 + 3744 * x2), xmask) tmp27 = tl.load(in_ptr0 + (185 + x0 + 61 * x1 + 3744 * x2), xmask) tmp29 = tl.load(in_ptr0 + (186 + x0 + 61 * x1 + 3744 * x2), xmask) tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp8 = triton_helpers.maximum(tmp7, tmp6) tmp10 = triton_helpers.maximum(tmp9, tmp8) tmp12 = triton_helpers.maximum(tmp11, tmp10) tmp14 = triton_helpers.maximum(tmp13, tmp12) tmp16 = triton_helpers.maximum(tmp15, tmp14) tmp18 = triton_helpers.maximum(tmp17, tmp16) tmp20 = triton_helpers.maximum(tmp19, tmp18) tmp22 = triton_helpers.maximum(tmp21, tmp20) tmp24 = triton_helpers.maximum(tmp23, tmp22) tmp26 = triton_helpers.maximum(tmp25, tmp24) tmp28 = triton_helpers.maximum(tmp27, tmp26) tmp30 = triton_helpers.maximum(tmp29, tmp28) tmp31 = tmp1 > tmp0 tmp32 = tl.full([1], 1, tl.int8) tmp33 = tl.full([1], 0, tl.int8) tmp34 = tl.where(tmp31, tmp32, tmp33) tmp35 = tmp3 > tmp2 tmp36 = tl.full([1], 2, tl.int8) tmp37 = tl.where(tmp35, tmp36, tmp34) tmp38 = tmp5 > tmp4 tmp39 = tl.full([1], 3, tl.int8) tmp40 = tl.where(tmp38, tmp39, tmp37) tmp41 = tmp7 > tmp6 tmp42 = tl.full([1], 4, tl.int8) tmp43 = tl.where(tmp41, tmp42, tmp40) tmp44 = tmp9 > tmp8 tmp45 = tl.full([1], 5, tl.int8) tmp46 = tl.where(tmp44, tmp45, tmp43) tmp47 = tmp11 > tmp10 tmp48 = tl.full([1], 6, tl.int8) tmp49 = tl.where(tmp47, tmp48, tmp46) tmp50 = tmp13 > tmp12 tmp51 = tl.full([1], 7, tl.int8) tmp52 = tl.where(tmp50, tmp51, tmp49) tmp53 = tmp15 > tmp14 tmp54 = tl.full([1], 8, tl.int8) tmp55 = tl.where(tmp53, tmp54, tmp52) tmp56 = tmp17 > tmp16 tmp57 = tl.full([1], 9, tl.int8) tmp58 = tl.where(tmp56, tmp57, tmp55) tmp59 = tmp19 > tmp18 tmp60 = tl.full([1], 10, tl.int8) tmp61 = tl.where(tmp59, tmp60, tmp58) tmp62 = tmp21 > tmp20 tmp63 = tl.full([1], 11, tl.int8) tmp64 = tl.where(tmp62, tmp63, tmp61) tmp65 = tmp23 > tmp22 tmp66 = tl.full([1], 12, tl.int8) tmp67 = tl.where(tmp65, tmp66, tmp64) tmp68 = tmp25 > tmp24 tmp69 = tl.full([1], 13, tl.int8) tmp70 = tl.where(tmp68, tmp69, tmp67) tmp71 = tmp27 > tmp26 tmp72 = tl.full([1], 14, tl.int8) tmp73 = tl.where(tmp71, tmp72, tmp70) tmp74 = tmp29 > tmp28 tmp75 = tl.full([1], 15, tl.int8) tmp76 = tl.where(tmp74, tmp75, tmp73) tl.store(out_ptr0 + x3, tmp30, xmask) tl.store(out_ptr1 + (x4 + 3456 * x2), tmp76, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 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, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 61, 61), (14884, 3721, 61, 1)) buf1 = empty_strided_cuda((4, 4, 61, 61), (14976, 3744, 61, 1), torch.float32) get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(59536)](buf0, primals_2, buf1, 59536, XBLOCK=256, num_warps=4, num_stages=1) del buf0 del primals_2 buf2 = empty_strided_cuda((4, 4, 58, 58), (13456, 3364, 58, 1), torch.float32) buf3 = empty_strided_cuda((4, 4, 58, 58), (13824, 3456, 58, 1), torch.int8) triton_poi_fused_max_pool2d_with_indices_1[grid(53824)](buf1, buf2, buf3, 53824, XBLOCK=256, num_warps=4, num_stages=1) return buf2, primals_1, primals_3, buf1, buf3 def Pool(k, stride=1, pad=0): return torch.nn.MaxPool2d(k, stride=stride, padding=pad) class ConvReluPoolNew(nn.Module): def __init__(self, fIn, fOut, k, stride=1, pool=2): super().__init__() self.conv = Conv2d(fIn, fOut, k, stride) self.pool = Pool(k) def forward(self, input_0): primals_1 = self.conv.weight primals_2 = self.conv.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
smearle/neural-mmo
ConvReluPool
false
12,994
[ "MIT" ]
0
7f1e98857cb32bdb59a273eb71ec43bbd9793b34
https://github.com/smearle/neural-mmo/tree/7f1e98857cb32bdb59a273eb71ec43bbd9793b34
mix_Linear
import torch from torch import nn def Binarize(tensor): """ Binarize function: binarize input tensors Input: tensor: the input tensor. Output: binarized: the binarized tensor. """ binarized = torch.where(tensor > 0, torch.ones_like(tensor, dtype=torch .float32, device='cuda'), torch.full(tensor.shape, -1, dtype=torch. float32, device='cuda')) return binarized def quantization(input, bits): """ Combination of quantization and de-quantization function Input: input: the original full-precision tensor. bits: number of quantized bits. Output: dequantized: the de-quantized tensor. """ quantized_max = 2 ** (bits - 1) - 1 quantized_min = -2 ** (bits - 1) pmax = input.max() pmin = input.min() scale_int = quantized_max - quantized_min scale_fp = pmax - pmin quantized = torch.round((input - pmin) * (scale_int / scale_fp) ) + quantized_min dequantized = (quantized - quantized_min) * (scale_fp / scale_int) + pmin return dequantized class mix_Linear(nn.Module): """ class mix_Linear: provide implementations of 32-bit and 1-bit layers Input: input (Tensor) bit_1_quantize (1-bit quantization flag) layer_number (the layer number) bit_32_quantize (full-precision flag) Output: out (Tensor) """ def __init__(self, in_features, out_features, bias=True): super(mix_Linear, self).__init__() self.in_features = in_features self.out_features = out_features self.weight = nn.Parameter(torch.Tensor(out_features, in_features)) self.quantized_weight = nn.Parameter(torch.Tensor(out_features, in_features)) if bias: self.bias = nn.Parameter(torch.Tensor(out_features)) else: self.register_parameter('bias', None) def forward(self, input, bit_1_quantize=False, layer_number=0, bit_32_quantize=False): if layer_number > 2: if input.dtype == torch.half: input.data = Binarize(input.data).half() else: input.data = Binarize(input.data) if bit_32_quantize is True: if input.data.dtype == torch.half: out = nn.functional.linear(input, self.weight.half(), self.bias ) else: out = nn.functional.linear(input, self.weight, self.bias) elif bit_1_quantize is True: input.data = Binarize(input.data) with torch.no_grad(): if input.data.dtype == torch.float: self.quantized_weight.data = Binarize(self.weight) else: input.data = input.data.half() self.quantized_weight.data = Binarize(self.weight) self.quantized_weight.data = (self.quantized_weight. data.half()) out = nn.functional.linear(input, self.quantized_weight, self.bias) else: if not hasattr(self.weight, 'org'): self.weight.org = self.weight.data.clone() self.weight.data = quantization(self.weight.org, 8) if input.dtype == torch.half: self.weight.data = self.weight.data out = nn.functional.linear(input, self.weight, self.bias) return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_per_fused_add_div_max_min_mul_reciprocal_round_sub_0(in_ptr0, out_ptr2, 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.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = triton_helpers.max2(tmp1, 1)[:, None] tmp5 = triton_helpers.min2(tmp1, 1)[:, None] tmp6 = tmp0 - tmp5 tmp7 = tmp3 - tmp5 tmp8 = tl.full([1, 1], 1, tl.int32) tmp9 = tmp8 / tmp7 tmp10 = 255.0 tmp11 = tmp9 * tmp10 tmp12 = tmp6 * tmp11 tmp13 = libdevice.nearbyint(tmp12) tmp14 = -128.0 tmp15 = tmp13 + tmp14 tmp16 = tmp15 - tmp14 tmp17 = 0.00392156862745098 tmp18 = tmp7 * tmp17 tmp19 = tmp16 * tmp18 tmp20 = tmp19 + tmp5 tl.store(out_ptr2 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp20, None) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_per_fused_add_div_max_min_mul_reciprocal_round_sub_0[grid(1)]( primals_2, buf2, 1, 16, XBLOCK=1, num_warps=2, num_stages=1) del primals_2 buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_4, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(buf2, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf3) del primals_4 buf4 = torch.ops.aten.set_.source_Tensor(primals_1, buf2) assert_size_stride(buf4, (4, 4), (4, 1)) del primals_1 return reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0) def Binarize(tensor): """ Binarize function: binarize input tensors Input: tensor: the input tensor. Output: binarized: the binarized tensor. """ binarized = torch.where(tensor > 0, torch.ones_like(tensor, dtype=torch .float32, device='cuda'), torch.full(tensor.shape, -1, dtype=torch. float32, device='cuda')) return binarized def quantization(input, bits): """ Combination of quantization and de-quantization function Input: input: the original full-precision tensor. bits: number of quantized bits. Output: dequantized: the de-quantized tensor. """ quantized_max = 2 ** (bits - 1) - 1 quantized_min = -2 ** (bits - 1) pmax = input.max() pmin = input.min() scale_int = quantized_max - quantized_min scale_fp = pmax - pmin quantized = torch.round((input - pmin) * (scale_int / scale_fp) ) + quantized_min dequantized = (quantized - quantized_min) * (scale_fp / scale_int) + pmin return dequantized class mix_LinearNew(nn.Module): """ class mix_Linear: provide implementations of 32-bit and 1-bit layers Input: input (Tensor) bit_1_quantize (1-bit quantization flag) layer_number (the layer number) bit_32_quantize (full-precision flag) Output: out (Tensor) """ def __init__(self, in_features, out_features, bias=True): super(mix_LinearNew, self).__init__() self.in_features = in_features self.out_features = out_features self.weight = nn.Parameter(torch.Tensor(out_features, in_features)) self.quantized_weight = nn.Parameter(torch.Tensor(out_features, in_features)) if bias: self.bias = nn.Parameter(torch.Tensor(out_features)) else: self.register_parameter('bias', None) def forward(self, input_0): primals_1 = self.weight primals_2 = self.quantized_weight primals_4 = self.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
snudatalab/SensiMix
mix_Linear
false
12,995
[ "Apache-2.0" ]
0
e5d790f48a96806e9ae01449bb4a66e8f09c4d3a
https://github.com/snudatalab/SensiMix/tree/e5d790f48a96806e9ae01449bb4a66e8f09c4d3a
Actor
import torch import torch.nn as nn import torch.nn.functional as F class Actor(nn.Module): def __init__(self, state_dim, action_dim, max_action): super(Actor, self).__init__() self.l1 = nn.Linear(state_dim, 128) self.l2 = nn.Linear(128, 128) self.l3 = nn.Linear(128, action_dim) self.max_action = max_action def forward(self, state): a = F.relu(self.l1(state)) a = F.relu(self.l2(a)) return self.max_action * torch.tanh(self.l3(a)) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'state_dim': 4, 'action_dim': 4, 'max_action': 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): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 128 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) @triton.jit def triton_poi_fused_mul_tanh_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 = libdevice.tanh(tmp0) tmp2 = 4.0 tmp3 = tmp1 * tmp2 tl.store(out_ptr0 + x0, 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, (128, 4), (4, 1)) assert_size_stride(primals_2, (128,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (128, 128), (128, 1)) assert_size_stride(primals_5, (128,), (1,)) assert_size_stride(primals_6, (4, 128), (128, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 128), (128, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 128), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 128), (2048, 512, 128, 1), 0) del buf0 buf7 = empty_strided_cuda((4, 4, 4, 128), (2048, 512, 128, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(8192)](buf1, primals_2, buf7, 8192, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 128), (128, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 128), (128, 1), 0), reinterpret_tensor(primals_4, (128, 128), (1, 128), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 128), (2048, 512, 128, 1), 0) del buf2 buf6 = empty_strided_cuda((4, 4, 4, 128), (2048, 512, 128, 1), torch.bool) triton_poi_fused_relu_threshold_backward_0[grid(8192)](buf3, primals_5, buf6, 8192, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 128), (128, 1), 0), reinterpret_tensor(primals_6, (128, 4), (1, 128), 0), alpha=1, beta=1, out=buf4) del primals_7 buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_mul_tanh_1[grid(256)](buf4, buf5, 256, XBLOCK=256, num_warps=4, num_stages=1) return buf5, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 128), (128, 1), 0 ), reinterpret_tensor(buf3, (64, 128), (128, 1), 0 ), buf4, primals_6, buf6, primals_4, buf7 class ActorNew(nn.Module): def __init__(self, state_dim, action_dim, max_action): super(ActorNew, self).__init__() self.l1 = nn.Linear(state_dim, 128) self.l2 = nn.Linear(128, 128) self.l3 = nn.Linear(128, action_dim) self.max_action = max_action def forward(self, input_0): primals_1 = self.l1.weight primals_2 = self.l1.bias primals_4 = self.l2.weight primals_5 = self.l2.bias primals_6 = self.l3.weight primals_7 = self.l3.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
sridas123/TD3
Actor
false
12,996
[ "MIT" ]
0
2556c952ef7623c8201fdfdd9102e23d98101f5c
https://github.com/sridas123/TD3/tree/2556c952ef7623c8201fdfdd9102e23d98101f5c
BackwardsNet
import torch from torch import nn class BackwardsNet(nn.Module): def __init__(self, h, ydim): super().__init__() self.loss = torch.nn.CrossEntropyLoss() self.fc1 = torch.nn.Linear(2 * h, h) self.fc2 = torch.nn.Linear(h, ydim) def forward(self, phiPrev, phi, atn): x = torch.cat((phiPrev, phi), 1) x = torch.nn.functional.relu(self.fc1(x)) x = self.fc2(x) loss = self.loss(x, atn) return loss def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'h': 4, 'ydim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 8 x1 = xindex // 8 x2 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (4 * x1 + x0), tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp6 = 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_relu_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused__log_softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_per_fused__log_softmax_div_mul_neg_sum_3(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r2 = rindex r1 = rindex // 4 tmp0 = tl.load(in_ptr0 + r2, None) tmp1 = tl.load(in_ptr0 + 4 * r1, None, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * r1), None, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (2 + 4 * r1), None, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (3 + 4 * r1), None, eviction_policy='evict_last') tmp14 = tl.load(in_ptr1 + r2, 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, [XBLOCK, RBLOCK]) tmp18 = tl.sum(tmp16, 1)[:, None] tmp19 = -tmp18 tmp20 = 0.25 tmp21 = tmp19 * tmp20 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp21, None) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 8), (8, 1)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4, 4), (4, 1)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 8), (8, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(32)](primals_1, primals_2, buf0, 32, XBLOCK=32, num_warps=1, num_stages=1) del primals_1 del primals_2 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf0, reinterpret_tensor(primals_3, (8, 4), (1, 8 ), 0), out=buf1) del primals_3 buf2 = buf1 del buf1 triton_poi_fused_relu_1[grid(16)](buf2, primals_4, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_4 buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_6, buf2, reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf3) del primals_6 buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused__log_softmax_2[grid(16)](buf3, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1) buf5 = empty_strided_cuda((), (), torch.float32) buf6 = buf5 del buf5 triton_per_fused__log_softmax_div_mul_neg_sum_3[grid(1)](buf6, buf4, primals_7, 1, 16, XBLOCK=1, num_warps=2, num_stages=1) del buf4 return buf6, primals_7, buf0, buf2, buf3, primals_5 class BackwardsNetNew(nn.Module): def __init__(self, h, ydim): super().__init__() self.loss = torch.nn.CrossEntropyLoss() self.fc1 = torch.nn.Linear(2 * h, h) self.fc2 = torch.nn.Linear(h, ydim) def forward(self, input_0, input_1, input_2): primals_3 = self.fc1.weight primals_4 = self.fc1.bias primals_1 = self.fc2.weight primals_6 = self.fc2.bias primals_2 = input_0 primals_5 = input_1 primals_7 = input_2 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
smearle/neural-mmo
BackwardsNet
false
12,997
[ "MIT" ]
0
7f1e98857cb32bdb59a273eb71ec43bbd9793b34
https://github.com/smearle/neural-mmo/tree/7f1e98857cb32bdb59a273eb71ec43bbd9793b34
DQN
import torch import torch.nn.functional as F import torch.nn as nn class DQN(nn.Module): """A simple deep Q network implementation. Computes Q values for each (action, object) tuple given an input state vector """ def __init__(self, state_dim, action_dim, object_dim, hidden_size=100): super(DQN, self).__init__() self.state_encoder = nn.Linear(state_dim, hidden_size) self.state2action = nn.Linear(hidden_size, action_dim) self.state2object = nn.Linear(hidden_size, object_dim) def forward(self, x): state = F.relu(self.state_encoder(x)) return self.state2action(state), self.state2object(state) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'state_dim': 4, 'action_dim': 4, 'object_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 6400 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex x0 = xindex % 100 x2 = xindex % 1600 x3 = xindex // 1600 tmp0 = tl.load(in_out_ptr0 + x4, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x4, tmp4, xmask) tl.store(out_ptr0 + (x2 + 1664 * x3), tmp6, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (100, 4), (4, 1)) assert_size_stride(primals_2, (100,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 100), (100, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 100), (100, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 100), (100, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 100), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 100), (1600, 400, 100, 1), 0) del buf0 buf4 = empty_strided_cuda((4, 4, 4, 100), (1664, 400, 100, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(6400)](buf1, primals_2, buf4, 6400, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 100), (100, 1), 0), reinterpret_tensor(primals_4, (100, 4), (1, 100), 0), alpha=1, beta=1, out=buf2) del primals_5 buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf1, (64, 100), (100, 1), 0), reinterpret_tensor(primals_6, (100, 4), (1, 100), 0), alpha=1, beta=1, out=buf3) del primals_7 return reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 100), (100, 1), 0 ), primals_6, primals_4, buf4 class DQNNew(nn.Module): """A simple deep Q network implementation. Computes Q values for each (action, object) tuple given an input state vector """ def __init__(self, state_dim, action_dim, object_dim, hidden_size=100): super(DQNNew, self).__init__() self.state_encoder = nn.Linear(state_dim, hidden_size) self.state2action = nn.Linear(hidden_size, action_dim) self.state2object = nn.Linear(hidden_size, object_dim) def forward(self, input_0): primals_1 = self.state_encoder.weight primals_2 = self.state_encoder.bias primals_4 = self.state2action.weight primals_5 = self.state2action.bias primals_6 = self.state2object.weight primals_7 = self.state2object.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]
stepinski/machinelearning
DQN
false
12,998
[ "MIT" ]
0
1f84883a25616da4cd76bb4655267efd3421e561
https://github.com/stepinski/machinelearning/tree/1f84883a25616da4cd76bb4655267efd3421e561
SelfAttentionPooling
import torch import torch.nn as nn class SelfAttentionPooling(nn.Module): """ Implementation of SelfAttentionPooling Original Paper: Self-Attention Encoding and Pooling for Speaker Recognition https://arxiv.org/pdf/2008.01077v1.pdf """ def __init__(self, input_dim): super(SelfAttentionPooling, self).__init__() self.W = nn.Linear(input_dim, 1) self.softmax = nn.functional.softmax def forward(self, batch_rep, att_mask): """ input: batch_rep : size (N, T, H), N: batch size, T: sequence length, H: Hidden dimension attention_weight: att_w : size (N, T, 1) return: utter_rep: size (N, H) """ batch_rep.shape[1] att_logits = self.W(batch_rep).squeeze(-1) att_logits = att_mask + att_logits att_w = self.softmax(att_logits, dim=-1).unsqueeze(-1) utter_rep = torch.sum(batch_rep * att_w, dim=1) return utter_rep def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn 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 = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 16 tmp0 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = triton_helpers.maximum(tmp2, tmp5) tmp9 = tmp7 + tmp8 tmp10 = triton_helpers.maximum(tmp6, tmp9) tmp13 = tmp11 + tmp12 tmp14 = triton_helpers.maximum(tmp10, tmp13) tmp15 = tmp2 - tmp14 tmp16 = tl_math.exp(tmp15) tmp17 = tmp5 - tmp14 tmp18 = tl_math.exp(tmp17) tmp19 = tmp16 + tmp18 tmp20 = tmp9 - tmp14 tmp21 = tl_math.exp(tmp20) tmp22 = tmp19 + tmp21 tmp23 = tmp13 - tmp14 tmp24 = tl_math.exp(tmp23) tmp25 = tmp22 + tmp24 tl.store(out_ptr0 + x2, tmp14, xmask) tl.store(out_ptr1 + x2, tmp25, xmask) @triton.jit def triton_poi_fused_mul_sum_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex % 64 x3 = xindex // 64 x5 = xindex // 4 % 16 x2 = xindex // 16 % 4 x7 = xindex tmp0 = tl.load(in_ptr0 + x4, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + (x5 + 64 * x3), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr2 + x5, xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr3 + (x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp7 = tl.load(in_ptr4 + (x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp10 = tl.load(in_ptr0 + (64 + x4), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr1 + (16 + x5 + 64 * x3), xmask, eviction_policy= 'evict_last') tmp12 = tl.load(in_ptr2 + (16 + x5), xmask, eviction_policy='evict_last') tmp14 = tl.load(in_ptr3 + (4 + x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp17 = tl.load(in_ptr4 + (4 + x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp21 = tl.load(in_ptr0 + (128 + x4), xmask, eviction_policy='evict_last') tmp22 = tl.load(in_ptr1 + (32 + x5 + 64 * x3), xmask, eviction_policy= 'evict_last') tmp23 = tl.load(in_ptr2 + (32 + x5), xmask, eviction_policy='evict_last') tmp25 = tl.load(in_ptr3 + (8 + x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp28 = tl.load(in_ptr4 + (8 + x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp32 = tl.load(in_ptr0 + (192 + x4), xmask, eviction_policy='evict_last') tmp33 = tl.load(in_ptr1 + (48 + x5 + 64 * x3), xmask, eviction_policy= 'evict_last') tmp34 = tl.load(in_ptr2 + (48 + x5), xmask, eviction_policy='evict_last') tmp36 = tl.load(in_ptr3 + (12 + x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp39 = tl.load(in_ptr4 + (12 + x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 - tmp4 tmp6 = tl_math.exp(tmp5) tmp8 = tmp6 / tmp7 tmp9 = tmp0 * tmp8 tmp13 = tmp11 + tmp12 tmp15 = tmp13 - tmp14 tmp16 = tl_math.exp(tmp15) tmp18 = tmp16 / tmp17 tmp19 = tmp10 * tmp18 tmp20 = tmp9 + tmp19 tmp24 = tmp22 + tmp23 tmp26 = tmp24 - tmp25 tmp27 = tl_math.exp(tmp26) tmp29 = tmp27 / tmp28 tmp30 = tmp21 * tmp29 tmp31 = tmp20 + tmp30 tmp35 = tmp33 + tmp34 tmp37 = tmp35 - tmp36 tmp38 = tl_math.exp(tmp37) tmp40 = tmp38 / tmp39 tmp41 = tmp32 * tmp40 tmp42 = tmp31 + tmp41 tl.store(out_ptr0 + x7, tmp42, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (1, 4), (4, 1)) assert_size_stride(primals_3, (1,), (1,)) assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_3, reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 1), (1, 4), 0 ), alpha=1, beta=1, out=buf1) del primals_2 del primals_3 buf2 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) get_raw_stream(0) triton_poi_fused__softmax_add_0[grid(64)](primals_4, buf1, buf2, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_mul_sum_1[grid(256)](primals_1, primals_4, buf1, buf2, buf3, buf4, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf2 del buf3 return buf4, primals_1, primals_4, buf1 class SelfAttentionPoolingNew(nn.Module): """ Implementation of SelfAttentionPooling Original Paper: Self-Attention Encoding and Pooling for Speaker Recognition https://arxiv.org/pdf/2008.01077v1.pdf """ def __init__(self, input_dim): super(SelfAttentionPoolingNew, self).__init__() self.W = nn.Linear(input_dim, 1) self.softmax = nn.functional.softmax def forward(self, input_0, input_1): primals_2 = self.W.weight primals_3 = self.W.bias primals_1 = input_0 primals_4 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
sumanthd17/s3prl
SelfAttentionPooling
false
12,999
[ "MIT" ]
0
bb74c705295d121c4308ceb6b6a2c8d1814d6f4c
https://github.com/sumanthd17/s3prl/tree/bb74c705295d121c4308ceb6b6a2c8d1814d6f4c
BertSelfAttention
from _paritybench_helpers import _mock_config import math import torch import torch.nn as nn import torch.nn.functional as F class BertSelfAttention(nn.Module): def __init__(self, config): 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 transform(self, x, linear_layer): """ x : bs * seq_len * hidden_size(word_embedding) fc : hidden_size * all_head_size x <- bs * seq_len * all_head_size(hidden_size) x <- bs * seq_len * (num_heads * head_size) x <- bs * num_heads * seq_len * head_size """ bs, seq_len = x.shape[:2] proj = linear_layer(x) proj = proj.view(bs, seq_len, self.num_attention_heads, self. attention_head_size) proj = proj.transpose(1, 2) return proj def attention(self, key, query, value, attention_mask): """ key, query, value: [bs, num_heads, seq_len, head_size] """ score = query @ key.transpose(2, 3) score = score / math.sqrt(self.attention_head_size) """ score: [bs, num_heads, seq_len, seq_len] """ score = score + attention_mask score = F.softmax(score, dim=3) score = self.dropout(score) score = score @ value """ score: [bs, num_heads, seq_len, head_size] """ bs, num_attention_heads, seq_len, attention_head_size = score.shape score = score.view(bs, seq_len, num_attention_heads * attention_head_size) return score def forward(self, hidden_states, attention_mask): """ hidden_states: [bs, seq_len, hidden_state] attention_mask: [bs, 1, 1, seq_len] output: [bs, seq_len, hidden_state] """ key_layer = self.transform(hidden_states, self.key) value_layer = self.transform(hidden_states, self.value) query_layer = self.transform(hidden_states, self.query) attn_value = self.attention(key_layer, query_layer, value_layer, attention_mask) return attn_value 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 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 reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_0(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 1.0 tmp4 = tmp2 * tmp3 tl.store(out_ptr0 + (x2 + 4 * y3), tmp4, xmask & ymask) @triton.jit def triton_poi_fused_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 16 tmp0 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = triton_helpers.maximum(tmp2, tmp5) tmp9 = tmp7 + tmp8 tmp10 = triton_helpers.maximum(tmp6, tmp9) tmp13 = tmp11 + tmp12 tmp14 = triton_helpers.maximum(tmp10, tmp13) tmp15 = tmp2 - tmp14 tmp16 = tl_math.exp(tmp15) tmp17 = tmp5 - tmp14 tmp18 = tl_math.exp(tmp17) tmp19 = tmp16 + tmp18 tmp20 = tmp9 - tmp14 tmp21 = tl_math.exp(tmp20) tmp22 = tmp19 + tmp21 tmp23 = tmp13 - tmp14 tmp24 = tl_math.exp(tmp23) tmp25 = tmp22 + tmp24 tmp26 = float('-inf') tmp27 = tmp2 == tmp26 tmp28 = tmp27 == 0 tmp29 = tmp28.to(tl.int64) tmp30 = tmp29 != 0 tmp31 = tmp5 == tmp26 tmp32 = tmp31 == 0 tmp33 = tmp32.to(tl.int64) tmp34 = tmp33 != 0 tmp35 = tmp30 | tmp34 tmp36 = tmp9 == tmp26 tmp37 = tmp36 == 0 tmp38 = tmp37.to(tl.int64) tmp39 = tmp38 != 0 tmp40 = tmp35 | tmp39 tmp41 = tmp13 == tmp26 tmp42 = tmp41 == 0 tmp43 = tmp42.to(tl.int64) tmp44 = tmp43 != 0 tmp45 = tmp40 | tmp44 tl.store(out_ptr0 + x2, tmp14, xmask) tl.store(out_ptr1 + x2, tmp25, xmask) tl.store(out_ptr2 + x2, tmp45, xmask) @triton.jit def triton_poi_fused_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex // 4 x4 = xindex x5 = xindex % 64 tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last').to(tl .int1) tmp2 = tl.load(in_out_ptr0 + x4, xmask) tmp3 = tl.load(in_ptr1 + x5, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr2 + x3, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr3 + x3, xmask, eviction_policy='evict_last') tmp1 = tmp0 == 0 tmp4 = tmp2 + tmp3 tmp6 = tmp4 - tmp5 tmp7 = tl_math.exp(tmp6) tmp9 = tmp7 / tmp8 tmp10 = 0.0 tmp11 = tl.where(tmp1, tmp10, tmp9) tl.store(in_out_ptr0 + x4, tmp11, xmask) @triton.jit def triton_poi_fused_3(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8) = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4,), (1,)) 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_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1) del primals_4 buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), out=buf2) del primals_6 buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_0[grid(16, 4)](buf2, primals_7, buf3, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_7 buf4 = reinterpret_tensor(buf2, (4, 4, 1, 4), (16, 4, 4, 1), 0) del buf2 triton_poi_fused_0[grid(16, 4)](buf0, primals_3, buf4, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_3 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(buf0, (4, 4, 4, 1), (16, 4, 1, 64), 0) del buf0 buf7 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf8 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.bool) triton_poi_fused_1[grid(64)](buf5, primals_8, buf6, buf7, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1) buf9 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf5 triton_poi_fused_2[grid(256)](buf9, buf8, primals_8, buf6, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf6 del buf8 del primals_8 buf10 = reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf7 triton_poi_fused_3[grid(16, 4)](buf1, primals_5, buf10, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_5 buf11 = reinterpret_tensor(buf1, (16, 4, 1), (4, 1, 1), 0) del buf1 extern_kernels.bmm(reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf10, (16, 4, 1), (4, 1, 0), 0), out=buf11) return reinterpret_tensor(buf11, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(primals_1, (16, 4), (4, 1), 0 ), buf9, reinterpret_tensor(buf10, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 4), 0) class BertSelfAttentionNew(nn.Module): def __init__(self, config): 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 transform(self, x, linear_layer): """ x : bs * seq_len * hidden_size(word_embedding) fc : hidden_size * all_head_size x <- bs * seq_len * all_head_size(hidden_size) x <- bs * seq_len * (num_heads * head_size) x <- bs * num_heads * seq_len * head_size """ bs, seq_len = x.shape[:2] proj = linear_layer(x) proj = proj.view(bs, seq_len, self.num_attention_heads, self. attention_head_size) proj = proj.transpose(1, 2) return proj def attention(self, key, query, value, attention_mask): """ key, query, value: [bs, num_heads, seq_len, head_size] """ score = query @ key.transpose(2, 3) score = score / math.sqrt(self.attention_head_size) """ score: [bs, num_heads, seq_len, seq_len] """ score = score + attention_mask score = F.softmax(score, dim=3) score = self.dropout(score) score = score @ value """ score: [bs, num_heads, seq_len, head_size] """ bs, num_attention_heads, seq_len, attention_head_size = score.shape score = score.view(bs, seq_len, num_attention_heads * attention_head_size) return score def forward(self, input_0, input_1): primals_2 = self.query.weight primals_3 = self.query.bias primals_4 = self.key.weight primals_5 = self.key.bias primals_6 = self.value.weight primals_7 = self.value.bias primals_1 = 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]
sleepope/cs769-assignments
BertSelfAttention
false
13,000
[ "MIT" ]
0
36c7a75d39507b7fe7b2b1bf1de6b8033b110da5
https://github.com/sleepope/cs769-assignments/tree/36c7a75d39507b7fe7b2b1bf1de6b8033b110da5
Critic
import torch import torch.nn as nn import torch.nn.functional as F class Critic(nn.Module): def __init__(self, state_dim, action_dim): super(Critic, self).__init__() self.l1 = nn.Linear(state_dim + action_dim, 128) self.l2 = nn.Linear(128, 128) self.l3 = nn.Linear(128, 1) def forward(self, state, action): sa = torch.cat([state, action], 1) q1 = F.relu(self.l1(sa)) q1 = F.relu(self.l2(q1)) q1 = self.l3(q1) return q1 def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'state_dim': 4, 'action_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 8 x1 = xindex // 8 x2 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (4 * x1 + x0), tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp6 = 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_relu_1(in_out_ptr0, in_ptr0, 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 % 128 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8) = 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, (128, 8), (8, 1)) assert_size_stride(primals_4, (128,), (1,)) assert_size_stride(primals_5, (128, 128), (128, 1)) assert_size_stride(primals_6, (128,), (1,)) assert_size_stride(primals_7, (1, 128), (128, 1)) assert_size_stride(primals_8, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 8), (8, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(32)](primals_1, primals_2, buf0, 32, XBLOCK=32, num_warps=1, num_stages=1) del primals_1 del primals_2 buf1 = empty_strided_cuda((4, 128), (128, 1), torch.float32) extern_kernels.mm(buf0, reinterpret_tensor(primals_3, (8, 128), (1, 8), 0), out=buf1) del primals_3 buf2 = buf1 del buf1 triton_poi_fused_relu_1[grid(512)](buf2, primals_4, 512, XBLOCK=256, num_warps=4, num_stages=1) del primals_4 buf3 = empty_strided_cuda((4, 128), (128, 1), torch.float32) extern_kernels.mm(buf2, reinterpret_tensor(primals_5, (128, 128), ( 1, 128), 0), out=buf3) buf4 = buf3 del buf3 triton_poi_fused_relu_1[grid(512)](buf4, primals_6, 512, XBLOCK=256, num_warps=4, num_stages=1) del primals_6 buf6 = empty_strided_cuda((4, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_8, buf4, reinterpret_tensor(primals_7, (128, 1), (1, 128), 0), alpha=1, beta=1, out=buf6) del primals_8 return buf6, buf0, buf2, buf4, primals_7, primals_5 class CriticNew(nn.Module): def __init__(self, state_dim, action_dim): super(CriticNew, self).__init__() self.l1 = nn.Linear(state_dim + action_dim, 128) self.l2 = nn.Linear(128, 128) self.l3 = nn.Linear(128, 1) def forward(self, input_0, input_1): primals_3 = self.l1.weight primals_4 = self.l1.bias primals_5 = self.l2.weight primals_6 = self.l2.bias primals_7 = self.l3.weight primals_8 = self.l3.bias primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8]) return output[0]
sridas123/TD3
Critic
false
13,001
[ "MIT" ]
0
2556c952ef7623c8201fdfdd9102e23d98101f5c
https://github.com/sridas123/TD3/tree/2556c952ef7623c8201fdfdd9102e23d98101f5c
BertLayer
from _paritybench_helpers import _mock_config import math import torch import torch.nn as nn import torch.nn.functional as F class BertSelfAttention(nn.Module): def __init__(self, config): 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 transform(self, x, linear_layer): bs, seq_len = x.shape[:2] proj = linear_layer(x) proj = proj.view(bs, seq_len, self.num_attention_heads, self. attention_head_size) proj = proj.transpose(1, 2) return proj def attention(self, key, query, value, attention_mask): norm = math.sqrt(key.shape[-1]) scores = torch.matmul(query, key.transpose(-1, -2)) bs = scores.shape[0] s_len = scores.shape[-1] scores = scores / norm scores_n = scores + attention_mask attention = nn.Softmax(dim=-1)(scores_n) v_prime = torch.matmul(attention, value) v_prime = v_prime.permute(0, 2, 1, 3) v_prime = v_prime.contiguous().view(bs, s_len, self.all_head_size) return v_prime def forward(self, hidden_states, attention_mask): """ hidden_states: [bs, seq_len, hidden_state] attention_mask: [bs, 1, 1, seq_len] output: [bs, seq_len, hidden_state] """ key_layer = self.transform(hidden_states, self.key) value_layer = self.transform(hidden_states, self.value) query_layer = self.transform(hidden_states, self.query) attn_value = self.attention(key_layer, query_layer, value_layer, attention_mask) return attn_value class BertLayer(nn.Module): def __init__(self, config): super().__init__() self.self_attention = BertSelfAttention(config) self.attention_dense = nn.Linear(config.hidden_size, config.hidden_size ) self.attention_layer_norm = nn.LayerNorm(config.hidden_size, eps= config.layer_norm_eps) self.attention_dropout = nn.Dropout(config.hidden_dropout_prob) self.interm_dense = nn.Linear(config.hidden_size, config. intermediate_size) self.interm_af = F.gelu self.out_dense = nn.Linear(config.intermediate_size, config.hidden_size ) self.out_layer_norm = nn.LayerNorm(config.hidden_size, eps=config. layer_norm_eps) self.out_dropout = nn.Dropout(config.hidden_dropout_prob) def add_norm(self, input, output, dense_layer, dropout, ln_layer): """ input: the input output: the input that requires the sublayer to transform dense_layer, dropput: the sublayer ln_layer: layer norm that takes input+sublayer(output) """ out = dense_layer(output) out = dropout(out) out = out + input out = ln_layer(out) return out def forward(self, hidden_states, attention_mask): """ hidden_states: either from the embedding layer (first bert layer) or from the previous bert layer as shown in the left of Figure 1 of https://arxiv.org/pdf/1706.03762.pdf each block consists of 1. a multi-head attention layer (BertSelfAttention) 2. a add-norm that takes the output of BertSelfAttention and the input of BertSelfAttention 3. a feed forward layer 4. a add-norm that takes the output of feed forward layer and the input of feed forward layer """ attn_value = self.self_attention(hidden_states, attention_mask) out_attn = self.add_norm(hidden_states, attn_value, self. attention_dense, self.attention_dropout, self.attention_layer_norm) out = self.interm_dense(out_attn) out = self.interm_af(out) out = self.add_norm(out_attn, out, self.out_dense, self.out_dropout, self.out_layer_norm) return out 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, layer_norm_eps=1, hidden_dropout_prob=0.5, intermediate_size=4)}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import 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 reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_0(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 1.0 tmp4 = tmp2 * tmp3 tl.store(out_ptr0 + (x2 + 4 * y3), tmp4, xmask & ymask) @triton.jit def triton_poi_fused_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 16 tmp0 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = triton_helpers.maximum(tmp2, tmp5) tmp9 = tmp7 + tmp8 tmp10 = triton_helpers.maximum(tmp6, tmp9) tmp13 = tmp11 + tmp12 tmp14 = triton_helpers.maximum(tmp10, tmp13) tmp15 = tmp2 - tmp14 tmp16 = tl_math.exp(tmp15) tmp17 = tmp5 - tmp14 tmp18 = tl_math.exp(tmp17) tmp19 = tmp16 + tmp18 tmp20 = tmp9 - tmp14 tmp21 = tl_math.exp(tmp20) tmp22 = tmp19 + tmp21 tmp23 = tmp13 - tmp14 tmp24 = tl_math.exp(tmp23) tmp25 = tmp22 + tmp24 tmp26 = float('-inf') tmp27 = tmp2 == tmp26 tmp28 = tmp27 == 0 tmp29 = tmp28.to(tl.int64) tmp30 = tmp29 != 0 tmp31 = tmp5 == tmp26 tmp32 = tmp31 == 0 tmp33 = tmp32.to(tl.int64) tmp34 = tmp33 != 0 tmp35 = tmp30 | tmp34 tmp36 = tmp9 == tmp26 tmp37 = tmp36 == 0 tmp38 = tmp37.to(tl.int64) tmp39 = tmp38 != 0 tmp40 = tmp35 | tmp39 tmp41 = tmp13 == tmp26 tmp42 = tmp41 == 0 tmp43 = tmp42.to(tl.int64) tmp44 = tmp43 != 0 tmp45 = tmp40 | tmp44 tl.store(out_ptr0 + x2, tmp14, xmask) tl.store(out_ptr1 + x2, tmp25, xmask) tl.store(out_ptr2 + x2, tmp45, xmask) @triton.jit def triton_poi_fused_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex // 4 x4 = xindex x5 = xindex % 64 tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last').to(tl .int1) tmp2 = tl.load(in_out_ptr0 + x4, xmask) tmp3 = tl.load(in_ptr1 + x5, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr2 + x3, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr3 + x3, xmask, eviction_policy='evict_last') tmp1 = tmp0 == 0 tmp4 = tmp2 + tmp3 tmp6 = tmp4 - tmp5 tmp7 = tl_math.exp(tmp6) tmp9 = tmp7 / tmp8 tmp10 = 0.0 tmp11 = tl.where(tmp1, tmp10, tmp9) tl.store(in_out_ptr0 + x4, tmp11, xmask) @triton.jit def triton_poi_fused_3(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask) @triton.jit def triton_poi_fused_clone_4(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_add_native_layer_norm_5(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp9 = tmp7 + tmp8 tmp10 = tmp6 + tmp9 tmp13 = tmp11 + tmp12 tmp14 = tmp10 + tmp13 tmp15 = 4.0 tmp16 = tmp14 / tmp15 tmp17 = tmp2 - tmp16 tmp18 = tmp17 * tmp17 tmp19 = tmp5 - tmp16 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp22 = tmp9 - tmp16 tmp23 = tmp22 * tmp22 tmp24 = tmp21 + tmp23 tmp25 = tmp13 - tmp16 tmp26 = tmp25 * tmp25 tmp27 = tmp24 + tmp26 tmp28 = tmp27 / tmp15 tl.store(out_ptr0 + x0, tmp16, xmask) tl.store(out_ptr1 + x0, tmp28, xmask) @triton.jit def triton_poi_fused_add_native_layer_norm_6(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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 = 1.0 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_gelu_7(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 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_add_8(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_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x2, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_native_layer_norm_9(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 = 1.0 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_10(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) 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 ) = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4,), (1,)) assert_size_stride(primals_8, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_9, (4, 4), (4, 1)) assert_size_stride(primals_10, (4,), (1,)) assert_size_stride(primals_11, (4,), (1,)) assert_size_stride(primals_12, (4,), (1,)) assert_size_stride(primals_13, (4, 4), (4, 1)) assert_size_stride(primals_14, (4,), (1,)) assert_size_stride(primals_15, (4, 4), (4, 1)) assert_size_stride(primals_16, (4,), (1,)) assert_size_stride(primals_17, (4,), (1,)) assert_size_stride(primals_18, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1) del primals_4 buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), out=buf2) del primals_6 buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_0[grid(16, 4)](buf2, primals_7, buf3, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_7 buf4 = reinterpret_tensor(buf2, (4, 4, 1, 4), (16, 4, 4, 1), 0) del buf2 triton_poi_fused_0[grid(16, 4)](buf0, primals_3, buf4, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_3 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(buf0, (4, 4, 4, 1), (16, 4, 1, 64), 0) del buf0 buf7 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf8 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.bool) triton_poi_fused_1[grid(64)](buf5, primals_8, buf6, buf7, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1) buf9 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf5 triton_poi_fused_2[grid(256)](buf9, buf8, primals_8, buf6, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf8 del primals_8 buf10 = reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf7 triton_poi_fused_3[grid(16, 4)](buf1, primals_5, buf10, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_5 buf11 = reinterpret_tensor(buf1, (16, 4, 1), (4, 1, 1), 0) del buf1 extern_kernels.bmm(reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf10, (16, 4, 1), (4, 1, 0), 0), out=buf11) buf12 = reinterpret_tensor(buf6, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf6 triton_poi_fused_clone_4[grid(16, 4)](buf11, buf12, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf13 = reinterpret_tensor(buf11, (16, 4), (4, 1), 0) del buf11 extern_kernels.addmm(primals_10, reinterpret_tensor(buf12, (16, 4), (4, 1), 0), reinterpret_tensor(primals_9, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf13) del primals_10 buf14 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf15 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) triton_poi_fused_add_native_layer_norm_5[grid(16)](buf13, primals_1, buf14, buf15, 16, XBLOCK=16, num_warps=1, num_stages=1) buf16 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_add_native_layer_norm_6[grid(64)](buf13, primals_1, buf14, buf15, primals_11, primals_12, buf16, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_12 buf17 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_14, reinterpret_tensor(buf16, (16, 4), (4, 1), 0), reinterpret_tensor(primals_13, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf17) del primals_14 buf18 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_gelu_7[grid(64)](buf17, buf18, 64, XBLOCK=64, num_warps=1, num_stages=1) buf19 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf18, (16, 4), (4, 1), 0), reinterpret_tensor(primals_15, (4, 4), (1, 4), 0), out=buf19) buf20 = reinterpret_tensor(buf19, (4, 4, 4), (16, 4, 1), 0) del buf19 triton_poi_fused_add_8[grid(64)](buf20, primals_16, buf16, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_16 buf21 = buf15 del buf15 buf22 = buf14 del buf14 triton_poi_fused_native_layer_norm_9[grid(16)](buf20, buf21, buf22, 16, XBLOCK=16, num_warps=1, num_stages=1) buf23 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_10[grid(64)](buf20, buf21, buf22, primals_17, primals_18, buf23, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf21 del buf22 del primals_18 return buf23, primals_1, primals_11, primals_17, buf9, reinterpret_tensor( buf10, (16, 1, 4), (4, 1, 1), 0), reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0), reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 4), 0 ), reinterpret_tensor(buf12, (16, 4), (4, 1), 0 ), buf13, reinterpret_tensor(buf16, (16, 4), (4, 1), 0 ), buf17, reinterpret_tensor(buf18, (16, 4), (4, 1), 0 ), buf20, primals_15, primals_13, primals_9 class BertSelfAttention(nn.Module): def __init__(self, config): 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 transform(self, x, linear_layer): bs, seq_len = x.shape[:2] proj = linear_layer(x) proj = proj.view(bs, seq_len, self.num_attention_heads, self. attention_head_size) proj = proj.transpose(1, 2) return proj def attention(self, key, query, value, attention_mask): norm = math.sqrt(key.shape[-1]) scores = torch.matmul(query, key.transpose(-1, -2)) bs = scores.shape[0] s_len = scores.shape[-1] scores = scores / norm scores_n = scores + attention_mask attention = nn.Softmax(dim=-1)(scores_n) v_prime = torch.matmul(attention, value) v_prime = v_prime.permute(0, 2, 1, 3) v_prime = v_prime.contiguous().view(bs, s_len, self.all_head_size) return v_prime def forward(self, hidden_states, attention_mask): """ hidden_states: [bs, seq_len, hidden_state] attention_mask: [bs, 1, 1, seq_len] output: [bs, seq_len, hidden_state] """ key_layer = self.transform(hidden_states, self.key) value_layer = self.transform(hidden_states, self.value) query_layer = self.transform(hidden_states, self.query) attn_value = self.attention(key_layer, query_layer, value_layer, attention_mask) return attn_value class BertLayerNew(nn.Module): def __init__(self, config): super().__init__() self.self_attention = BertSelfAttention(config) self.attention_dense = nn.Linear(config.hidden_size, config.hidden_size ) self.attention_layer_norm = nn.LayerNorm(config.hidden_size, eps= config.layer_norm_eps) self.attention_dropout = nn.Dropout(config.hidden_dropout_prob) self.interm_dense = nn.Linear(config.hidden_size, config. intermediate_size) self.interm_af = F.gelu self.out_dense = nn.Linear(config.intermediate_size, config.hidden_size ) self.out_layer_norm = nn.LayerNorm(config.hidden_size, eps=config. layer_norm_eps) self.out_dropout = nn.Dropout(config.hidden_dropout_prob) def add_norm(self, input, output, dense_layer, dropout, ln_layer): """ input: the input output: the input that requires the sublayer to transform dense_layer, dropput: the sublayer ln_layer: layer norm that takes input+sublayer(output) """ out = dense_layer(output) out = dropout(out) out = out + input out = ln_layer(out) return out def forward(self, input_0, input_1): primals_2 = self.self_attention.query.weight primals_3 = self.self_attention.query.bias primals_4 = self.self_attention.key.weight primals_5 = self.self_attention.key.bias primals_6 = self.self_attention.value.weight primals_7 = self.self_attention.value.bias primals_9 = self.attention_dense.weight primals_10 = self.attention_dense.bias primals_11 = self.attention_layer_norm.weight primals_12 = self.attention_layer_norm.bias primals_13 = self.interm_dense.weight primals_14 = self.interm_dense.bias primals_15 = self.out_dense.weight primals_16 = self.out_dense.bias primals_17 = self.out_layer_norm.weight primals_18 = self.out_layer_norm.bias primals_1 = input_0 primals_8 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16, primals_17, primals_18]) return output[0]
shrishabh/cs769-assignments
BertLayer
false
13,002
[ "MIT" ]
0
babce1def0d65728bf1d4e4a725d8939f1d5f9a7
https://github.com/shrishabh/cs769-assignments/tree/babce1def0d65728bf1d4e4a725d8939f1d5f9a7
DecoderLayer
import math import torch import torch.utils.data import torch.nn as nn import torch.nn.functional as F class MultiHeadAttention(nn.Module): def __init__(self, heads, d_model): super(MultiHeadAttention, self).__init__() assert d_model % heads == 0 self.d_k = d_model // heads self.heads = heads self.dropout = nn.Dropout(0.1) self.query = nn.Linear(d_model, d_model) self.key = nn.Linear(d_model, d_model) self.value = nn.Linear(d_model, d_model) self.concat = nn.Linear(d_model, d_model) def forward(self, query, key, value, mask): """ query, key, value of shape: (batch_size, max_len, 512) mask of shape: (batch_size, 1, 1, max_words) """ query = self.query(query) key = self.key(key) value = self.value(value) query = query.view(query.shape[0], -1, self.heads, self.d_k).permute( 0, 2, 1, 3) key = key.view(key.shape[0], -1, self.heads, self.d_k).permute(0, 2, 1, 3) value = value.view(value.shape[0], -1, self.heads, self.d_k).permute( 0, 2, 1, 3) scores = torch.matmul(query, key.permute(0, 1, 3, 2)) / math.sqrt(query .size(-1)) scores = scores.masked_fill(mask == 0, -1000000000.0) weights = F.softmax(scores, dim=-1) weights = self.dropout(weights) context = torch.matmul(weights, value) context = context.permute(0, 2, 1, 3).contiguous().view(context. shape[0], -1, self.heads * self.d_k) interacted = self.concat(context) return interacted class FeedForward(nn.Module): def __init__(self, d_model, middle_dim=2048): super(FeedForward, self).__init__() self.fc1 = nn.Linear(d_model, middle_dim) self.fc2 = nn.Linear(middle_dim, d_model) self.dropout = nn.Dropout(0.1) def forward(self, x): out = F.relu(self.fc1(x)) out = self.fc2(self.dropout(out)) return out class DecoderLayer(nn.Module): def __init__(self, d_model, heads): super(DecoderLayer, self).__init__() self.layernorm = nn.LayerNorm(d_model) self.self_multihead = MultiHeadAttention(heads, d_model) self.src_multihead = MultiHeadAttention(heads, d_model) self.feed_forward = FeedForward(d_model) self.dropout = nn.Dropout(0.1) def forward(self, embeddings, encoded, src_mask, target_mask): query = self.dropout(self.self_multihead(embeddings, embeddings, embeddings, target_mask)) query = self.layernorm(query + embeddings) interacted = self.dropout(self.src_multihead(query, encoded, encoded, src_mask)) interacted = self.layernorm(interacted + query) feed_forward_out = self.dropout(self.feed_forward(interacted)) decoded = self.layernorm(feed_forward_out + interacted) return decoded def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4]), torch.rand([4, 4, 4]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'d_model': 4, 'heads': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import math import torch.utils.data 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_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_eq_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.0 tmp2 = tmp0 == tmp1 tl.store(out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_poi_fused__softmax_div_masked_fill_2(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 16 x2 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last').to(tl .int1) tmp1 = tl.load(in_ptr1 + 4 * x2, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp7 = tl.load(in_ptr1 + (1 + 4 * x2), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp12 = tl.load(in_ptr1 + (2 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp16 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp17 = tl.load(in_ptr1 + (3 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp2 = 1.0 tmp3 = tmp1 * tmp2 tmp4 = -1000000000.0 tmp5 = tl.where(tmp0, tmp4, tmp3) tmp8 = tmp7 * tmp2 tmp9 = tl.where(tmp6, tmp4, tmp8) tmp10 = triton_helpers.maximum(tmp5, tmp9) tmp13 = tmp12 * tmp2 tmp14 = tl.where(tmp11, tmp4, tmp13) tmp15 = triton_helpers.maximum(tmp10, tmp14) tmp18 = tmp17 * tmp2 tmp19 = tl.where(tmp16, tmp4, tmp18) tmp20 = triton_helpers.maximum(tmp15, tmp19) tmp21 = tmp5 - tmp20 tmp22 = tl_math.exp(tmp21) tmp23 = tmp9 - tmp20 tmp24 = tl_math.exp(tmp23) tmp25 = tmp22 + tmp24 tmp26 = tmp14 - tmp20 tmp27 = tl_math.exp(tmp26) tmp28 = tmp25 + tmp27 tmp29 = tmp19 - tmp20 tmp30 = tl_math.exp(tmp29) tmp31 = tmp28 + tmp30 tl.store(out_ptr0 + x2, tmp20, xmask) tl.store(out_ptr1 + x2, tmp31, xmask) @triton.jit def triton_poi_fused__softmax_div_masked_fill_3(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex % 64 x4 = xindex x5 = xindex // 4 tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last').to(tl .int1) tmp1 = tl.load(in_out_ptr0 + x4, xmask) tmp6 = tl.load(in_ptr1 + x5, xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr2 + x5, xmask, eviction_policy='evict_last') tmp2 = 1.0 tmp3 = tmp1 * tmp2 tmp4 = -1000000000.0 tmp5 = tl.where(tmp0, tmp4, tmp3) tmp7 = tmp5 - tmp6 tmp8 = tl_math.exp(tmp7) tmp10 = tmp8 / tmp9 tl.store(in_out_ptr0 + x4, tmp10, xmask) @triton.jit def triton_poi_fused_clone_4(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_add_native_layer_norm_5(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp9 = tmp7 + tmp8 tmp10 = tmp6 + tmp9 tmp13 = tmp11 + tmp12 tmp14 = tmp10 + tmp13 tmp15 = 4.0 tmp16 = tmp14 / tmp15 tmp17 = tmp2 - tmp16 tmp18 = tmp17 * tmp17 tmp19 = tmp5 - tmp16 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp22 = tmp9 - tmp16 tmp23 = tmp22 * tmp22 tmp24 = tmp21 + tmp23 tmp25 = tmp13 - tmp16 tmp26 = tmp25 * tmp25 tmp27 = tmp24 + tmp26 tmp28 = tmp27 / tmp15 tl.store(out_ptr0 + x0, tmp16, xmask) tl.store(out_ptr1 + x0, tmp28, xmask) @triton.jit def triton_poi_fused_add_native_layer_norm_6(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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_add_7(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x2, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_native_layer_norm_8(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_9(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_relu_threshold_backward_10(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 2048 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, 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) = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4,), (1,)) assert_size_stride(primals_8, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_9, (4, 4), (4, 1)) assert_size_stride(primals_10, (4,), (1,)) assert_size_stride(primals_11, (4,), (1,)) assert_size_stride(primals_12, (4,), (1,)) assert_size_stride(primals_13, (4, 4), (4, 1)) assert_size_stride(primals_14, (4,), (1,)) assert_size_stride(primals_15, (4, 4), (4, 1)) assert_size_stride(primals_16, (4,), (1,)) assert_size_stride(primals_17, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_18, (4, 4), (4, 1)) assert_size_stride(primals_19, (4,), (1,)) assert_size_stride(primals_20, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_21, (4, 4), (4, 1)) assert_size_stride(primals_22, (4,), (1,)) assert_size_stride(primals_23, (2048, 4), (4, 1)) assert_size_stride(primals_24, (2048,), (1,)) assert_size_stride(primals_25, (4, 2048), (2048, 1)) assert_size_stride(primals_26, (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=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_2 buf4 = reinterpret_tensor(buf0, (4, 4, 1, 4), (16, 4, 4, 1), 0) del buf0 triton_poi_fused_clone_0[grid(16, 4)](buf1, primals_5, buf4, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_5 buf5 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf3, (16, 4, 1), (4, 1, 0), 0), reinterpret_tensor(buf4, (16, 1, 4), (4, 0, 1), 0), out=buf5) buf6 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool) triton_poi_fused_eq_1[grid(64)](primals_8, buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_8 buf7 = reinterpret_tensor(buf1, (4, 4, 4, 1), (16, 4, 1, 64), 0) del buf1 buf8 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) triton_poi_fused__softmax_div_masked_fill_2[grid(64)](buf6, buf5, buf7, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1) buf9 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf5 triton_poi_fused__softmax_div_masked_fill_3[grid(256)](buf9, buf6, buf7, buf8, 256, XBLOCK=128, num_warps=4, num_stages=1) buf10 = reinterpret_tensor(buf8, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf8 triton_poi_fused_clone_0[grid(16, 4)](buf2, primals_7, buf10, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_7 buf11 = reinterpret_tensor(buf2, (16, 4, 1), (4, 1, 1), 0) del buf2 extern_kernels.bmm(reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf10, (16, 4, 1), (4, 1, 0), 0), out=buf11) buf12 = reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf7 triton_poi_fused_clone_4[grid(16, 4)](buf11, buf12, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf13 = reinterpret_tensor(buf11, (16, 4), (4, 1), 0) del buf11 extern_kernels.addmm(primals_10, reinterpret_tensor(buf12, (16, 4), (4, 1), 0), reinterpret_tensor(primals_9, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf13) del primals_10 buf14 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf15 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) triton_poi_fused_add_native_layer_norm_5[grid(16)](buf13, primals_3, buf14, buf15, 16, XBLOCK=16, num_warps=1, num_stages=1) buf16 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_add_native_layer_norm_6[grid(64)](buf13, primals_3, buf14, buf15, primals_11, primals_12, buf16, 64, XBLOCK=64, num_warps=1, num_stages=1) buf17 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf16, (16, 4), (4, 1), 0), reinterpret_tensor(primals_13, (4, 4), (1, 4), 0), out=buf17) buf18 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_17, (16, 4), (4, 1), 0 ), reinterpret_tensor(primals_15, (4, 4), (1, 4), 0), out=buf18) del primals_15 buf19 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_17, (16, 4), (4, 1), 0 ), reinterpret_tensor(primals_18, (4, 4), (1, 4), 0), out=buf19) del primals_18 buf20 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) triton_poi_fused_clone_0[grid(16, 4)](buf17, primals_14, buf20, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_14 buf21 = reinterpret_tensor(buf17, (4, 4, 1, 4), (16, 4, 4, 1), 0) del buf17 triton_poi_fused_clone_0[grid(16, 4)](buf18, primals_16, buf21, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_16 buf22 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf20, (16, 4, 1), (4, 1, 0), 0), reinterpret_tensor(buf21, (16, 1, 4), (4, 0, 1), 0), out=buf22) buf23 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool) triton_poi_fused_eq_1[grid(64)](primals_20, buf23, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_20 buf24 = reinterpret_tensor(buf18, (4, 4, 4, 1), (16, 4, 1, 64), 0) del buf18 buf25 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) triton_poi_fused__softmax_div_masked_fill_2[grid(64)](buf23, buf22, buf24, buf25, 64, XBLOCK=64, num_warps=1, num_stages=1) buf26 = reinterpret_tensor(buf22, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf22 triton_poi_fused__softmax_div_masked_fill_3[grid(256)](buf26, buf23, buf24, buf25, 256, XBLOCK=128, num_warps=4, num_stages=1) buf27 = reinterpret_tensor(buf25, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf25 triton_poi_fused_clone_0[grid(16, 4)](buf19, primals_19, buf27, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_19 buf28 = reinterpret_tensor(buf19, (16, 4, 1), (4, 1, 1), 0) del buf19 extern_kernels.bmm(reinterpret_tensor(buf26, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf27, (16, 4, 1), (4, 1, 0), 0), out=buf28) buf29 = reinterpret_tensor(buf24, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf24 triton_poi_fused_clone_4[grid(16, 4)](buf28, buf29, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf30 = reinterpret_tensor(buf28, (16, 4), (4, 1), 0) del buf28 extern_kernels.mm(reinterpret_tensor(buf29, (16, 4), (4, 1), 0), reinterpret_tensor(primals_21, (4, 4), (1, 4), 0), out=buf30) buf31 = reinterpret_tensor(buf30, (4, 4, 4), (16, 4, 1), 0) del buf30 triton_poi_fused_add_7[grid(64)](buf31, primals_22, buf16, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_22 buf32 = buf15 del buf15 buf33 = buf14 del buf14 triton_poi_fused_native_layer_norm_8[grid(16)](buf31, buf32, buf33, 16, XBLOCK=16, num_warps=1, num_stages=1) buf34 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_9[grid(64)](buf31, buf32, buf33, primals_11, primals_12, buf34, 64, XBLOCK=64, num_warps=1, num_stages=1) buf35 = empty_strided_cuda((16, 2048), (2048, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf34, (16, 4), (4, 1), 0), reinterpret_tensor(primals_23, (4, 2048), (1, 4), 0), out=buf35) buf36 = reinterpret_tensor(buf35, (4, 4, 2048), (8192, 2048, 1), 0) del buf35 buf42 = empty_strided_cuda((4, 4, 2048), (8192, 2048, 1), torch.bool) triton_poi_fused_relu_threshold_backward_10[grid(32768)](buf36, primals_24, buf42, 32768, XBLOCK=128, num_warps=4, num_stages=1) del primals_24 buf37 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf36, (16, 2048), (2048, 1), 0), reinterpret_tensor(primals_25, (2048, 4), (1, 2048), 0), out=buf37) buf38 = reinterpret_tensor(buf37, (4, 4, 4), (16, 4, 1), 0) del buf37 triton_poi_fused_add_7[grid(64)](buf38, primals_26, buf34, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_26 buf39 = buf33 del buf33 buf40 = buf32 del buf32 triton_poi_fused_native_layer_norm_8[grid(16)](buf38, buf39, buf40, 16, XBLOCK=16, num_warps=1, num_stages=1) buf41 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_9[grid(64)](buf38, buf39, buf40, primals_11, primals_12, buf41, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf39 del buf40 del primals_12 return buf41, primals_3, primals_11, buf6, buf9, reinterpret_tensor(buf12, (16, 4), (4, 1), 0), buf13, reinterpret_tensor(buf16, (16, 4), (4, 1), 0), reinterpret_tensor(primals_17, (16, 4), (4, 1), 0 ), buf23, buf26, reinterpret_tensor(buf29, (16, 4), (4, 1), 0 ), buf31, reinterpret_tensor(buf34, (16, 4), (4, 1), 0 ), reinterpret_tensor(buf36, (16, 2048), (2048, 1), 0 ), buf38, primals_25, buf42, primals_23, primals_21, reinterpret_tensor( buf27, (16, 1, 4), (4, 1, 1), 0), reinterpret_tensor(buf20, (16, 1, 4), (4, 1, 1), 0), reinterpret_tensor(buf21, (16, 4, 1), (4, 1, 4), 0 ), primals_13, primals_9, reinterpret_tensor(buf10, (16, 1, 4), (4, 1, 1), 0), reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 4), 0) class MultiHeadAttention(nn.Module): def __init__(self, heads, d_model): super(MultiHeadAttention, self).__init__() assert d_model % heads == 0 self.d_k = d_model // heads self.heads = heads self.dropout = nn.Dropout(0.1) self.query = nn.Linear(d_model, d_model) self.key = nn.Linear(d_model, d_model) self.value = nn.Linear(d_model, d_model) self.concat = nn.Linear(d_model, d_model) def forward(self, query, key, value, mask): """ query, key, value of shape: (batch_size, max_len, 512) mask of shape: (batch_size, 1, 1, max_words) """ query = self.query(query) key = self.key(key) value = self.value(value) query = query.view(query.shape[0], -1, self.heads, self.d_k).permute( 0, 2, 1, 3) key = key.view(key.shape[0], -1, self.heads, self.d_k).permute(0, 2, 1, 3) value = value.view(value.shape[0], -1, self.heads, self.d_k).permute( 0, 2, 1, 3) scores = torch.matmul(query, key.permute(0, 1, 3, 2)) / math.sqrt(query .size(-1)) scores = scores.masked_fill(mask == 0, -1000000000.0) weights = F.softmax(scores, dim=-1) weights = self.dropout(weights) context = torch.matmul(weights, value) context = context.permute(0, 2, 1, 3).contiguous().view(context. shape[0], -1, self.heads * self.d_k) interacted = self.concat(context) return interacted class FeedForward(nn.Module): def __init__(self, d_model, middle_dim=2048): super(FeedForward, self).__init__() self.fc1 = nn.Linear(d_model, middle_dim) self.fc2 = nn.Linear(middle_dim, d_model) self.dropout = nn.Dropout(0.1) def forward(self, x): out = F.relu(self.fc1(x)) out = self.fc2(self.dropout(out)) return out class DecoderLayerNew(nn.Module): def __init__(self, d_model, heads): super(DecoderLayerNew, self).__init__() self.layernorm = nn.LayerNorm(d_model) self.self_multihead = MultiHeadAttention(heads, d_model) self.src_multihead = MultiHeadAttention(heads, d_model) self.feed_forward = FeedForward(d_model) self.dropout = nn.Dropout(0.1) def forward(self, input_0, input_1, input_2, input_3): primals_2 = self.layernorm.weight primals_5 = self.layernorm.bias primals_1 = self.self_multihead.query.weight primals_7 = self.self_multihead.query.bias primals_4 = self.self_multihead.key.weight primals_10 = self.self_multihead.key.bias primals_6 = self.self_multihead.value.weight primals_11 = self.self_multihead.value.bias primals_9 = self.self_multihead.concat.weight primals_12 = self.self_multihead.concat.bias primals_13 = self.src_multihead.query.weight primals_14 = self.src_multihead.query.bias primals_15 = self.src_multihead.key.weight primals_16 = self.src_multihead.key.bias primals_18 = self.src_multihead.value.weight primals_19 = self.src_multihead.value.bias primals_21 = self.src_multihead.concat.weight primals_22 = self.src_multihead.concat.bias primals_23 = self.feed_forward.fc1.weight primals_24 = self.feed_forward.fc1.bias primals_25 = self.feed_forward.fc2.weight primals_26 = self.feed_forward.fc2.bias primals_3 = input_0 primals_8 = input_1 primals_17 = input_2 primals_20 = input_3 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]) return output[0]
sd2001/seqModeling
DecoderLayer
false
13,003
[ "MIT" ]
0
393f680de711ea8477e5450633b492298d253368
https://github.com/sd2001/seqModeling/tree/393f680de711ea8477e5450633b492298d253368
BertLayer
from _paritybench_helpers import _mock_config import math import torch import torch.nn as nn import torch.nn.functional as F class BertSelfAttention(nn.Module): def __init__(self, config): 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 transform(self, x, linear_layer): """ x : bs * seq_len * hidden_size(word_embedding) fc : hidden_size * all_head_size x <- bs * seq_len * all_head_size(hidden_size) x <- bs * seq_len * (num_heads * head_size) x <- bs * num_heads * seq_len * head_size """ bs, seq_len = x.shape[:2] proj = linear_layer(x) proj = proj.view(bs, seq_len, self.num_attention_heads, self. attention_head_size) proj = proj.transpose(1, 2) return proj def attention(self, key, query, value, attention_mask): """ key, query, value: [bs, num_heads, seq_len, head_size] """ score = query @ key.transpose(2, 3) score = score / math.sqrt(self.attention_head_size) """ score: [bs, num_heads, seq_len, seq_len] """ score = score + attention_mask score = F.softmax(score, dim=3) score = self.dropout(score) score = score @ value """ score: [bs, num_heads, seq_len, head_size] """ bs, num_attention_heads, seq_len, attention_head_size = score.shape score = score.view(bs, seq_len, num_attention_heads * attention_head_size) return score def forward(self, hidden_states, attention_mask): """ hidden_states: [bs, seq_len, hidden_state] attention_mask: [bs, 1, 1, seq_len] output: [bs, seq_len, hidden_state] """ key_layer = self.transform(hidden_states, self.key) value_layer = self.transform(hidden_states, self.value) query_layer = self.transform(hidden_states, self.query) attn_value = self.attention(key_layer, query_layer, value_layer, attention_mask) return attn_value class BertLayer(nn.Module): def __init__(self, config): super().__init__() self.self_attention = BertSelfAttention(config) self.attention_dense = nn.Linear(config.hidden_size, config.hidden_size ) self.attention_layer_norm = nn.LayerNorm(config.hidden_size, eps= config.layer_norm_eps) self.attention_dropout = nn.Dropout(config.hidden_dropout_prob) self.interm_dense = nn.Linear(config.hidden_size, config. intermediate_size) self.interm_af = F.gelu self.out_dense = nn.Linear(config.intermediate_size, config.hidden_size ) self.out_layer_norm = nn.LayerNorm(config.hidden_size, eps=config. layer_norm_eps) self.out_dropout = nn.Dropout(config.hidden_dropout_prob) def add_norm(self, input, output, dense_layer, dropout, ln_layer): """ input: the input output: the input that requires the sublayer to transform dense_layer, dropput: the sublayer ln_layer: layer norm that takes input+sublayer(output) """ output = dense_layer(output) output = dropout(output) add_norm_output = ln_layer(input + output) return add_norm_output def forward(self, hidden_states, attention_mask): """ hidden_states: either from the embedding layer (first bert layer) or from the previous bert layer as shown in the left of Figure 1 of https://arxiv.org/pdf/1706.03762.pdf each block consists of 1. a multi-head attention layer (BertSelfAttention) 2. a add-norm that takes the output of BertSelfAttention and the input of BertSelfAttention 3. a feed forward layer 4. a add-norm that takes the output of feed forward layer and the input of feed forward layer """ attention_output = self.self_attention(hidden_states, attention_mask) add_norm_output = self.add_norm(hidden_states, attention_output, self.attention_dense, self.attention_dropout, self. attention_layer_norm) interm_output = self.interm_dense(add_norm_output) interm_output = self.interm_af(interm_output) output = self.add_norm(add_norm_output, interm_output, self. out_dense, self.out_dropout, self.out_layer_norm) return output 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, layer_norm_eps=1, hidden_dropout_prob=0.5, intermediate_size=4)}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import 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 reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_0(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 1.0 tmp4 = tmp2 * tmp3 tl.store(out_ptr0 + (x2 + 4 * y3), tmp4, xmask & ymask) @triton.jit def triton_poi_fused_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 16 tmp0 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = triton_helpers.maximum(tmp2, tmp5) tmp9 = tmp7 + tmp8 tmp10 = triton_helpers.maximum(tmp6, tmp9) tmp13 = tmp11 + tmp12 tmp14 = triton_helpers.maximum(tmp10, tmp13) tmp15 = tmp2 - tmp14 tmp16 = tl_math.exp(tmp15) tmp17 = tmp5 - tmp14 tmp18 = tl_math.exp(tmp17) tmp19 = tmp16 + tmp18 tmp20 = tmp9 - tmp14 tmp21 = tl_math.exp(tmp20) tmp22 = tmp19 + tmp21 tmp23 = tmp13 - tmp14 tmp24 = tl_math.exp(tmp23) tmp25 = tmp22 + tmp24 tmp26 = float('-inf') tmp27 = tmp2 == tmp26 tmp28 = tmp27 == 0 tmp29 = tmp28.to(tl.int64) tmp30 = tmp29 != 0 tmp31 = tmp5 == tmp26 tmp32 = tmp31 == 0 tmp33 = tmp32.to(tl.int64) tmp34 = tmp33 != 0 tmp35 = tmp30 | tmp34 tmp36 = tmp9 == tmp26 tmp37 = tmp36 == 0 tmp38 = tmp37.to(tl.int64) tmp39 = tmp38 != 0 tmp40 = tmp35 | tmp39 tmp41 = tmp13 == tmp26 tmp42 = tmp41 == 0 tmp43 = tmp42.to(tl.int64) tmp44 = tmp43 != 0 tmp45 = tmp40 | tmp44 tl.store(out_ptr0 + x2, tmp14, xmask) tl.store(out_ptr1 + x2, tmp25, xmask) tl.store(out_ptr2 + x2, tmp45, xmask) @triton.jit def triton_poi_fused_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex // 4 x4 = xindex x5 = xindex % 64 tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last').to(tl .int1) tmp2 = tl.load(in_out_ptr0 + x4, xmask) tmp3 = tl.load(in_ptr1 + x5, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr2 + x3, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr3 + x3, xmask, eviction_policy='evict_last') tmp1 = tmp0 == 0 tmp4 = tmp2 + tmp3 tmp6 = tmp4 - tmp5 tmp7 = tl_math.exp(tmp6) tmp9 = tmp7 / tmp8 tmp10 = 0.0 tmp11 = tl.where(tmp1, tmp10, tmp9) tl.store(in_out_ptr0 + x4, tmp11, xmask) @triton.jit def triton_poi_fused_3(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask) @triton.jit def triton_poi_fused_add_native_layer_norm_4(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_5(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 = 1.0 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_gelu_6(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 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_add_7(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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_8(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 = 1.0 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_9(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) 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 ) = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4,), (1,)) assert_size_stride(primals_8, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_9, (4, 4), (4, 1)) assert_size_stride(primals_10, (4,), (1,)) assert_size_stride(primals_11, (4,), (1,)) assert_size_stride(primals_12, (4,), (1,)) assert_size_stride(primals_13, (4, 4), (4, 1)) assert_size_stride(primals_14, (4,), (1,)) assert_size_stride(primals_15, (4, 4), (4, 1)) assert_size_stride(primals_16, (4,), (1,)) assert_size_stride(primals_17, (4,), (1,)) assert_size_stride(primals_18, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1) del primals_4 buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), out=buf2) del primals_6 buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_0[grid(16, 4)](buf2, primals_7, buf3, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_7 buf4 = reinterpret_tensor(buf2, (4, 4, 1, 4), (16, 4, 4, 1), 0) del buf2 triton_poi_fused_0[grid(16, 4)](buf0, primals_3, buf4, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_3 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(buf0, (4, 4, 4, 1), (16, 4, 1, 64), 0) del buf0 buf7 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf8 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.bool) triton_poi_fused_1[grid(64)](buf5, primals_8, buf6, buf7, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1) buf9 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf5 triton_poi_fused_2[grid(256)](buf9, buf8, primals_8, buf6, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf8 del primals_8 buf10 = reinterpret_tensor(buf7, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf7 triton_poi_fused_3[grid(16, 4)](buf1, primals_5, buf10, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_5 buf11 = reinterpret_tensor(buf1, (16, 4, 1), (4, 1, 1), 0) del buf1 extern_kernels.bmm(reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf10, (16, 4, 1), (4, 1, 0), 0), out=buf11) buf12 = reinterpret_tensor(buf6, (16, 4), (4, 1), 0) del buf6 extern_kernels.addmm(primals_10, reinterpret_tensor(buf11, (16, 4), (4, 1), 0), reinterpret_tensor(primals_9, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf12) del primals_10 buf13 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf14 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) triton_poi_fused_add_native_layer_norm_4[grid(16)](primals_1, buf12, buf13, buf14, 16, XBLOCK=16, num_warps=1, num_stages=1) buf15 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_add_native_layer_norm_5[grid(64)](primals_1, buf12, buf13, buf14, primals_11, primals_12, buf15, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_12 buf16 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_14, reinterpret_tensor(buf15, (16, 4), (4, 1), 0), reinterpret_tensor(primals_13, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf16) del primals_14 buf17 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_gelu_6[grid(64)](buf16, buf17, 64, XBLOCK=64, num_warps=1, num_stages=1) buf18 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf17, (16, 4), (4, 1), 0), reinterpret_tensor(primals_15, (4, 4), (1, 4), 0), out=buf18) buf19 = reinterpret_tensor(buf18, (4, 4, 4), (16, 4, 1), 0) del buf18 triton_poi_fused_add_7[grid(64)](buf19, buf15, primals_16, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_16 buf20 = buf14 del buf14 buf21 = buf13 del buf13 triton_poi_fused_native_layer_norm_8[grid(16)](buf19, buf20, buf21, 16, XBLOCK=16, num_warps=1, num_stages=1) buf22 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_9[grid(64)](buf19, buf20, buf21, primals_17, primals_18, buf22, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf20 del buf21 del primals_18 return buf22, primals_1, primals_11, primals_17, buf9, reinterpret_tensor( buf10, (16, 1, 4), (4, 1, 1), 0), reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0), reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 4), 0 ), reinterpret_tensor(buf11, (16, 4), (4, 1), 0 ), buf12, reinterpret_tensor(buf15, (16, 4), (4, 1), 0 ), buf16, reinterpret_tensor(buf17, (16, 4), (4, 1), 0 ), buf19, primals_15, primals_13, primals_9 class BertSelfAttention(nn.Module): def __init__(self, config): 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 transform(self, x, linear_layer): """ x : bs * seq_len * hidden_size(word_embedding) fc : hidden_size * all_head_size x <- bs * seq_len * all_head_size(hidden_size) x <- bs * seq_len * (num_heads * head_size) x <- bs * num_heads * seq_len * head_size """ bs, seq_len = x.shape[:2] proj = linear_layer(x) proj = proj.view(bs, seq_len, self.num_attention_heads, self. attention_head_size) proj = proj.transpose(1, 2) return proj def attention(self, key, query, value, attention_mask): """ key, query, value: [bs, num_heads, seq_len, head_size] """ score = query @ key.transpose(2, 3) score = score / math.sqrt(self.attention_head_size) """ score: [bs, num_heads, seq_len, seq_len] """ score = score + attention_mask score = F.softmax(score, dim=3) score = self.dropout(score) score = score @ value """ score: [bs, num_heads, seq_len, head_size] """ bs, num_attention_heads, seq_len, attention_head_size = score.shape score = score.view(bs, seq_len, num_attention_heads * attention_head_size) return score def forward(self, hidden_states, attention_mask): """ hidden_states: [bs, seq_len, hidden_state] attention_mask: [bs, 1, 1, seq_len] output: [bs, seq_len, hidden_state] """ key_layer = self.transform(hidden_states, self.key) value_layer = self.transform(hidden_states, self.value) query_layer = self.transform(hidden_states, self.query) attn_value = self.attention(key_layer, query_layer, value_layer, attention_mask) return attn_value class BertLayerNew(nn.Module): def __init__(self, config): super().__init__() self.self_attention = BertSelfAttention(config) self.attention_dense = nn.Linear(config.hidden_size, config.hidden_size ) self.attention_layer_norm = nn.LayerNorm(config.hidden_size, eps= config.layer_norm_eps) self.attention_dropout = nn.Dropout(config.hidden_dropout_prob) self.interm_dense = nn.Linear(config.hidden_size, config. intermediate_size) self.interm_af = F.gelu self.out_dense = nn.Linear(config.intermediate_size, config.hidden_size ) self.out_layer_norm = nn.LayerNorm(config.hidden_size, eps=config. layer_norm_eps) self.out_dropout = nn.Dropout(config.hidden_dropout_prob) def add_norm(self, input, output, dense_layer, dropout, ln_layer): """ input: the input output: the input that requires the sublayer to transform dense_layer, dropput: the sublayer ln_layer: layer norm that takes input+sublayer(output) """ output = dense_layer(output) output = dropout(output) add_norm_output = ln_layer(input + output) return add_norm_output def forward(self, input_0, input_1): primals_2 = self.self_attention.query.weight primals_3 = self.self_attention.query.bias primals_4 = self.self_attention.key.weight primals_5 = self.self_attention.key.bias primals_6 = self.self_attention.value.weight primals_7 = self.self_attention.value.bias primals_9 = self.attention_dense.weight primals_10 = self.attention_dense.bias primals_11 = self.attention_layer_norm.weight primals_12 = self.attention_layer_norm.bias primals_13 = self.interm_dense.weight primals_14 = self.interm_dense.bias primals_15 = self.out_dense.weight primals_16 = self.out_dense.bias primals_17 = self.out_layer_norm.weight primals_18 = self.out_layer_norm.bias primals_1 = input_0 primals_8 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16, primals_17, primals_18]) return output[0]
sleepope/cs769-assignments
BertLayer
false
13,004
[ "MIT" ]
0
36c7a75d39507b7fe7b2b1bf1de6b8033b110da5
https://github.com/sleepope/cs769-assignments/tree/36c7a75d39507b7fe7b2b1bf1de6b8033b110da5
FuseUnit
import torch import torch.nn as nn class FuseUnit(nn.Module): def __init__(self, channels): super(FuseUnit, self).__init__() self.proj1 = nn.Conv2d(2 * channels, channels, (1, 1)) self.proj2 = nn.Conv2d(channels, channels, (1, 1)) self.proj3 = nn.Conv2d(channels, channels, (1, 1)) self.fuse1x = nn.Conv2d(channels, 1, (1, 1), stride=1) self.fuse3x = nn.Conv2d(channels, 1, (3, 3), stride=1) self.fuse5x = nn.Conv2d(channels, 1, (5, 5), stride=1) self.pad3x = nn.ReflectionPad2d((1, 1, 1, 1)) self.pad5x = nn.ReflectionPad2d((2, 2, 2, 2)) self.sigmoid = nn.Sigmoid() def forward(self, F1, F2): Fcat = self.proj1(torch.cat((F1, F2), dim=1)) F1 = self.proj2(F1) F2 = self.proj3(F2) fusion1 = self.sigmoid(self.fuse1x(Fcat)) fusion3 = self.sigmoid(self.fuse3x(self.pad3x(Fcat))) fusion5 = self.sigmoid(self.fuse5x(self.pad5x(Fcat))) fusion = (fusion1 + fusion3 + fusion5) / 3 return torch.clamp(fusion, min=0, max=1.0) * F1 + torch.clamp(1 - fusion, min=0, max=1.0) * F2 def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime 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_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 16 % 8 x0 = xindex % 16 x2 = xindex // 128 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 16 * x1 + 64 * x2), tmp4 & xmask, other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 8, tl.int64) tmp9 = tl.load(in_ptr1 + (x0 + 16 * (-4 + x1) + 64 * x2), tmp6 & xmask, other=0.0) tmp10 = tl.where(tmp4, tmp5, tmp9) tl.store(out_ptr0 + x3, tmp10, xmask) @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_reflection_pad2d_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 576 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 6 x1 = xindex // 6 % 6 x2 = xindex // 36 x3 = xindex tmp0 = tl.load(in_ptr0 + (15 + -1 * tl_math.abs(-3 + tl_math.abs(-1 + x0)) + -4 * tl_math.abs(-3 + tl_math.abs(-1 + x1)) + 16 * x2), xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_poi_fused_convolution_3(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp3 = tmp0 + tmp2 tl.store(in_out_ptr0 + x0, tmp3, xmask) @triton.jit def triton_poi_fused_reflection_pad2d_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 8 x1 = xindex // 8 % 8 x2 = xindex // 64 x3 = xindex tmp0 = tl.load(in_ptr0 + (15 + -1 * tl_math.abs(-3 + tl_math.abs(-2 + x0)) + -4 * tl_math.abs(-3 + tl_math.abs(-2 + x1)) + 16 * x2), xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_poi_fused_add_clamp_convolution_div_mul_rsub_sigmoid_5(in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_out_ptr1 + x3, xmask) tmp4 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr2 + (x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp8 = tl.load(in_ptr3 + (x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp11 = tl.load(in_ptr4 + (x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp7 = tl.sigmoid(tmp6) tmp9 = tl.sigmoid(tmp8) tmp10 = tmp7 + tmp9 tmp12 = tl.sigmoid(tmp11) tmp13 = tmp10 + tmp12 tmp14 = 0.3333333333333333 tmp15 = tmp13 * tmp14 tmp16 = 0.0 tmp17 = triton_helpers.maximum(tmp15, tmp16) tmp18 = 1.0 tmp19 = triton_helpers.minimum(tmp17, tmp18) tmp20 = tmp19 * tmp2 tmp21 = tmp18 - tmp15 tmp22 = triton_helpers.maximum(tmp21, tmp16) tmp23 = triton_helpers.minimum(tmp22, tmp18) tmp24 = tmp23 * tmp5 tmp25 = tmp20 + tmp24 tl.store(in_out_ptr0 + x3, tmp2, xmask) tl.store(in_out_ptr1 + x3, tmp5, xmask) tl.store(out_ptr0 + x3, tmp25, 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) = 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, 8, 1, 1), (8, 1, 1, 1)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (4, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_8, (4,), (1,)) assert_size_stride(primals_9, (1, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_10, (1,), (1,)) assert_size_stride(primals_11, (1, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_12, (1,), (1,)) assert_size_stride(primals_13, (1, 4, 5, 5), (100, 25, 5, 1)) assert_size_stride(primals_14, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 8, 4, 4), (128, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(512)](primals_1, primals_2, buf0, 512, XBLOCK=256, num_warps=4, num_stages=1) buf1 = extern_kernels.convolution(buf0, primals_3, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 4, 4), (64, 16, 4, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_1[grid(256)](buf2, primals_4, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_4 buf3 = extern_kernels.convolution(primals_1, primals_5, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf3, (4, 4, 4, 4), (64, 16, 4, 1)) buf9 = empty_strided_cuda((4, 4, 6, 6), (144, 36, 6, 1), torch.float32) triton_poi_fused_reflection_pad2d_2[grid(576)](buf2, buf9, 576, XBLOCK=128, num_warps=4, num_stages=1) buf10 = extern_kernels.convolution(buf9, primals_11, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf10, (4, 1, 4, 4), (16, 16, 4, 1)) buf11 = buf10 del buf10 triton_poi_fused_convolution_3[grid(64)](buf11, primals_12, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_12 buf12 = empty_strided_cuda((4, 4, 8, 8), (256, 64, 8, 1), torch.float32 ) triton_poi_fused_reflection_pad2d_4[grid(1024)](buf2, buf12, 1024, XBLOCK=256, num_warps=4, num_stages=1) buf13 = extern_kernels.convolution(buf12, primals_13, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf13, (4, 1, 4, 4), (16, 16, 4, 1)) buf14 = buf13 del buf13 triton_poi_fused_convolution_3[grid(64)](buf14, primals_14, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_14 buf5 = extern_kernels.convolution(primals_2, 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, 4, 4, 4), (64, 16, 4, 1)) buf7 = extern_kernels.convolution(buf2, primals_9, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf7, (4, 1, 4, 4), (16, 16, 4, 1)) buf8 = buf7 del buf7 triton_poi_fused_convolution_3[grid(64)](buf8, primals_10, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_10 buf4 = buf3 del buf3 buf6 = buf5 del buf5 buf15 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_add_clamp_convolution_div_mul_rsub_sigmoid_5[grid(256) ](buf4, buf6, primals_6, primals_8, buf8, buf11, buf14, buf15, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_6 del primals_8 return (buf15, primals_1, primals_2, primals_3, primals_5, primals_7, primals_9, primals_11, primals_13, buf0, buf2, buf4, buf6, buf8, buf9, buf11, buf12, buf14) class FuseUnitNew(nn.Module): def __init__(self, channels): super(FuseUnitNew, self).__init__() self.proj1 = nn.Conv2d(2 * channels, channels, (1, 1)) self.proj2 = nn.Conv2d(channels, channels, (1, 1)) self.proj3 = nn.Conv2d(channels, channels, (1, 1)) self.fuse1x = nn.Conv2d(channels, 1, (1, 1), stride=1) self.fuse3x = nn.Conv2d(channels, 1, (3, 3), stride=1) self.fuse5x = nn.Conv2d(channels, 1, (5, 5), stride=1) self.pad3x = nn.ReflectionPad2d((1, 1, 1, 1)) self.pad5x = nn.ReflectionPad2d((2, 2, 2, 2)) self.sigmoid = nn.Sigmoid() def forward(self, input_0, input_1): primals_3 = self.proj1.weight primals_4 = self.proj1.bias primals_5 = self.proj2.weight primals_6 = self.proj2.bias primals_7 = self.proj3.weight primals_8 = self.proj3.bias primals_9 = self.fuse1x.weight primals_10 = self.fuse1x.bias primals_11 = self.fuse3x.weight primals_12 = self.fuse3x.bias primals_13 = self.fuse5x.weight primals_14 = self.fuse5x.bias primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14]) return output[0]
sugi-san/PAMA
FuseUnit
false
13,005
[ "MIT" ]
0
95141ebf0d3b61828a0e545f989f96b8ef569f34
https://github.com/sugi-san/PAMA/tree/95141ebf0d3b61828a0e545f989f96b8ef569f34
ConvNet
import torch import torch.nn as nn class ConvNet(nn.Module): def __init__(self): super(ConvNet, self).__init__() self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size= 5, padding=2) self.conv2 = nn.Conv2d(in_channels=32, out_channels=32, kernel_size =3, padding=1) self.conv3 = nn.Conv2d(in_channels=32, out_channels=16, kernel_size =3, padding=1) self.relu = nn.ReLU() self.pooling = nn.MaxPool2d(kernel_size=2) self.linear = nn.Linear(in_features=1296, out_features=3) def forward(self, x): x = self.pooling(self.relu(self.conv1(x))) x = self.pooling(self.relu(self.conv2(x))) x = self.pooling(self.relu(self.conv2(x))) x = self.pooling(self.relu(self.conv3(x))) x = self.linear(x.view(-1, 1296)) return x def get_inputs(): return [torch.rand([4, 3, 144, 144])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_relu_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 20736 % 32 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_1(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 % 72 x1 = xindex // 72 x4 = xindex x3 = xindex // 5184 x5 = xindex % 5184 tmp0 = tl.load(in_ptr0 + (2 * x0 + 288 * x1), None, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 288 * x1), None, eviction_policy ='evict_last') tmp3 = tl.load(in_ptr0 + (144 + 2 * x0 + 288 * x1), None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (145 + 2 * x0 + 288 * x1), None, eviction_policy='evict_last') tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + x4, tmp6, None) tl.store(out_ptr1 + (x5 + 5248 * x3), tmp16, None) @triton.jit def triton_poi_fused_convolution_relu_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 5184 % 32 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_3(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x0 = xindex % 36 x3 = xindex // 36 x2 = xindex // 1296 x4 = xindex % 1296 tmp0 = tl.load(in_ptr0 + (2 * x0 + 144 * x3), None, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 144 * x3), None, eviction_policy ='evict_last') tmp3 = tl.load(in_ptr0 + (72 + 2 * x0 + 144 * x3), None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (73 + 2 * x0 + 144 * x3), None, eviction_policy='evict_last') tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + (x4 + 1312 * x2), tmp6, None) tl.store(out_ptr1 + (x4 + 1408 * x2), tmp16, None) @triton.jit def triton_poi_fused_convolution_relu_4(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 // 1296 % 32 x0 = xindex % 1296 x4 = xindex // 1296 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(out_ptr0 + (x0 + 1312 * x4), tmp4, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_5(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 41472 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 18 x1 = xindex // 18 % 18 x2 = xindex // 324 x3 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 72 * x1 + 1312 * x2), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 72 * x1 + 1312 * x2), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (36 + 2 * x0 + 72 * x1 + 1312 * x2), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (37 + 2 * x0 + 72 * x1 + 1312 * x2), xmask, eviction_policy='evict_last') tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + x3, tmp6, xmask) tl.store(out_ptr1 + x3, tmp16, xmask) @triton.jit def triton_poi_fused_convolution_relu_6(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 20736 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 324 % 16 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, xmask) @triton.jit def triton_poi_fused_max_pool2d_with_indices_7(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 5184 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 9 x3 = xindex // 9 x2 = xindex // 1296 x4 = xindex % 1296 tmp0 = tl.load(in_ptr0 + (2 * x0 + 36 * x3), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 36 * x3), xmask, eviction_policy ='evict_last') tmp7 = tl.load(in_ptr0 + (18 + 2 * x0 + 36 * x3), xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (19 + 2 * x0 + 36 * x3), xmask, eviction_policy='evict_last') tmp2 = tmp1 > tmp0 tmp3 = tl.full([1], 1, tl.int8) tmp4 = tl.full([1], 0, tl.int8) tmp5 = tl.where(tmp2, tmp3, tmp4) tmp6 = triton_helpers.maximum(tmp1, tmp0) tmp8 = tmp7 > tmp6 tmp9 = tl.full([1], 2, tl.int8) tmp10 = tl.where(tmp8, tmp9, tmp5) tmp11 = triton_helpers.maximum(tmp7, tmp6) tmp13 = tmp12 > tmp11 tmp14 = tl.full([1], 3, tl.int8) tmp15 = tl.where(tmp13, tmp14, tmp10) tmp16 = triton_helpers.maximum(tmp12, tmp11) tl.store(out_ptr0 + (x4 + 1408 * x2), tmp15, xmask) tl.store(out_ptr1 + (x4 + 1312 * x2), tmp16, 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, (32, 3, 5, 5), (75, 25, 5, 1)) assert_size_stride(primals_2, (32,), (1,)) assert_size_stride(primals_3, (4, 3, 144, 144), (62208, 20736, 144, 1)) assert_size_stride(primals_4, (32, 32, 3, 3), (288, 9, 3, 1)) assert_size_stride(primals_5, (32,), (1,)) assert_size_stride(primals_6, (16, 32, 3, 3), (288, 9, 3, 1)) assert_size_stride(primals_7, (16,), (1,)) assert_size_stride(primals_8, (3, 1296), (1296, 1)) assert_size_stride(primals_9, (3,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = 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(buf0, (4, 32, 144, 144), (663552, 20736, 144, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(2654208)](buf1, primals_2, 2654208, XBLOCK=512, num_warps=8, num_stages=1) del primals_2 buf2 = empty_strided_cuda((4, 32, 72, 72), (165888, 5184, 72, 1), torch.float32) buf3 = empty_strided_cuda((4, 32, 72, 72), (167936, 5248, 72, 1), torch.int8) triton_poi_fused_max_pool2d_with_indices_1[grid(663552)](buf1, buf2, buf3, 663552, XBLOCK=512, num_warps=8, num_stages=1) buf4 = extern_kernels.convolution(buf2, 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, 32, 72, 72), (165888, 5184, 72, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_relu_2[grid(663552)](buf5, primals_5, 663552, XBLOCK=1024, num_warps=4, num_stages=1) buf6 = empty_strided_cuda((4, 32, 36, 36), (41984, 1312, 36, 1), torch.float32) buf7 = empty_strided_cuda((4, 32, 36, 36), (45056, 1408, 36, 1), torch.int8) triton_poi_fused_max_pool2d_with_indices_3[grid(165888)](buf5, buf6, buf7, 165888, XBLOCK=512, num_warps=8, num_stages=1) buf8 = extern_kernels.convolution(buf6, primals_4, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 32, 36, 36), (41472, 1296, 36, 1)) buf9 = empty_strided_cuda((4, 32, 36, 36), (41984, 1312, 36, 1), torch.float32) triton_poi_fused_convolution_relu_4[grid(165888)](buf8, primals_5, buf9, 165888, XBLOCK=1024, num_warps=4, num_stages=1) del buf8 del primals_5 buf10 = empty_strided_cuda((4, 32, 18, 18), (10368, 324, 18, 1), torch.float32) buf11 = empty_strided_cuda((4, 32, 18, 18), (10368, 324, 18, 1), torch.int8) triton_poi_fused_max_pool2d_with_indices_5[grid(41472)](buf9, buf10, buf11, 41472, XBLOCK=512, num_warps=4, num_stages=1) buf12 = extern_kernels.convolution(buf10, primals_6, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf12, (4, 16, 18, 18), (5184, 324, 18, 1)) buf13 = buf12 del buf12 triton_poi_fused_convolution_relu_6[grid(20736)](buf13, primals_7, 20736, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 buf14 = empty_strided_cuda((4, 16, 9, 9), (1408, 81, 9, 1), torch.int8) buf15 = empty_strided_cuda((4, 16, 9, 9), (1312, 81, 9, 1), torch. float32) triton_poi_fused_max_pool2d_with_indices_7[grid(5184)](buf13, buf14, buf15, 5184, XBLOCK=256, num_warps=4, num_stages=1) buf16 = empty_strided_cuda((4, 3), (3, 1), torch.float32) extern_kernels.addmm(primals_9, reinterpret_tensor(buf15, (4, 1296), (1312, 1), 0), reinterpret_tensor(primals_8, (1296, 3), (1, 1296), 0), alpha=1, beta=1, out=buf16) del primals_9 return (buf16, primals_1, primals_3, primals_4, primals_6, buf1, buf2, buf3, buf5, buf6, buf7, buf9, buf10, buf11, buf13, buf14, reinterpret_tensor(buf15, (4, 1296), (1312, 1), 0), primals_8) class ConvNetNew(nn.Module): def __init__(self): super(ConvNetNew, self).__init__() self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size= 5, padding=2) self.conv2 = nn.Conv2d(in_channels=32, out_channels=32, kernel_size =3, padding=1) self.conv3 = nn.Conv2d(in_channels=32, out_channels=16, kernel_size =3, padding=1) self.relu = nn.ReLU() self.pooling = nn.MaxPool2d(kernel_size=2) self.linear = nn.Linear(in_features=1296, out_features=3) def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_6 = self.conv3.weight primals_7 = self.conv3.bias primals_8 = self.linear.weight primals_9 = self.linear.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9]) return output[0]
slowy07/dffml
ConvNet
false
13,006
[ "MIT" ]
0
bbf491064470f1170be75b6bec572b6e576940b9
https://github.com/slowy07/dffml/tree/bbf491064470f1170be75b6bec572b6e576940b9
SAP
import torch import torch.nn as nn class SelfAttentionPooling(nn.Module): """ Implementation of SelfAttentionPooling Original Paper: Self-Attention Encoding and Pooling for Speaker Recognition https://arxiv.org/pdf/2008.01077v1.pdf """ def __init__(self, input_dim): super(SelfAttentionPooling, self).__init__() self.W = nn.Linear(input_dim, 1) self.softmax = nn.functional.softmax def forward(self, batch_rep, att_mask): """ input: batch_rep : size (N, T, H), N: batch size, T: sequence length, H: Hidden dimension attention_weight: att_w : size (N, T, 1) return: utter_rep: size (N, H) """ batch_rep.shape[1] att_logits = self.W(batch_rep).squeeze(-1) att_logits = att_mask + att_logits att_w = self.softmax(att_logits, dim=-1).unsqueeze(-1) utter_rep = torch.sum(batch_rep * att_w, dim=1) return utter_rep class SAP(nn.Module): """ Self Attention Pooling module incoporate attention mask""" def __init__(self, out_dim): super(SAP, self).__init__() self.act_fn = nn.Tanh() self.sap_layer = SelfAttentionPooling(out_dim) def forward(self, feature, att_mask): """ Arguments feature - [BxTxD] Acoustic feature with shape att_mask - [BxTx1] Attention Mask logits """ feature = self.act_fn(feature) sap_vec = self.sap_layer(feature, att_mask) return sap_vec def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'out_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._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_tanh_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = libdevice.tanh(tmp0) tl.store(out_ptr0 + x0, tmp1, xmask) @triton.jit def triton_poi_fused__softmax_add_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') tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = triton_helpers.maximum(tmp2, tmp5) tmp9 = tmp7 + tmp8 tmp10 = triton_helpers.maximum(tmp6, tmp9) tmp13 = tmp11 + tmp12 tmp14 = triton_helpers.maximum(tmp10, tmp13) tmp15 = tmp2 - tmp14 tmp16 = tl_math.exp(tmp15) tmp17 = tmp5 - tmp14 tmp18 = tl_math.exp(tmp17) tmp19 = tmp16 + tmp18 tmp20 = tmp9 - tmp14 tmp21 = tl_math.exp(tmp20) tmp22 = tmp19 + tmp21 tmp23 = tmp13 - tmp14 tmp24 = tl_math.exp(tmp23) tmp25 = tmp22 + tmp24 tl.store(out_ptr0 + x2, tmp14, xmask) tl.store(out_ptr1 + x2, tmp25, xmask) @triton.jit def triton_poi_fused_mul_sum_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex % 64 x3 = xindex // 64 x5 = xindex // 4 % 16 x2 = xindex // 16 % 4 x7 = xindex tmp0 = tl.load(in_ptr0 + x4, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + (x5 + 64 * x3), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr2 + x5, xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr3 + (x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp7 = tl.load(in_ptr4 + (x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp10 = tl.load(in_ptr0 + (64 + x4), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr1 + (16 + x5 + 64 * x3), xmask, eviction_policy= 'evict_last') tmp12 = tl.load(in_ptr2 + (16 + x5), xmask, eviction_policy='evict_last') tmp14 = tl.load(in_ptr3 + (4 + x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp17 = tl.load(in_ptr4 + (4 + x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp21 = tl.load(in_ptr0 + (128 + x4), xmask, eviction_policy='evict_last') tmp22 = tl.load(in_ptr1 + (32 + x5 + 64 * x3), xmask, eviction_policy= 'evict_last') tmp23 = tl.load(in_ptr2 + (32 + x5), xmask, eviction_policy='evict_last') tmp25 = tl.load(in_ptr3 + (8 + x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp28 = tl.load(in_ptr4 + (8 + x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp32 = tl.load(in_ptr0 + (192 + x4), xmask, eviction_policy='evict_last') tmp33 = tl.load(in_ptr1 + (48 + x5 + 64 * x3), xmask, eviction_policy= 'evict_last') tmp34 = tl.load(in_ptr2 + (48 + x5), xmask, eviction_policy='evict_last') tmp36 = tl.load(in_ptr3 + (12 + x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp39 = tl.load(in_ptr4 + (12 + x2 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 - tmp4 tmp6 = tl_math.exp(tmp5) tmp8 = tmp6 / tmp7 tmp9 = tmp0 * tmp8 tmp13 = tmp11 + tmp12 tmp15 = tmp13 - tmp14 tmp16 = tl_math.exp(tmp15) tmp18 = tmp16 / tmp17 tmp19 = tmp10 * tmp18 tmp20 = tmp9 + tmp19 tmp24 = tmp22 + tmp23 tmp26 = tmp24 - tmp25 tmp27 = tl_math.exp(tmp26) tmp29 = tmp27 / tmp28 tmp30 = tmp21 * tmp29 tmp31 = tmp20 + tmp30 tmp35 = tmp33 + tmp34 tmp37 = tmp35 - tmp36 tmp38 = tl_math.exp(tmp37) tmp40 = tmp38 / tmp39 tmp41 = tmp32 * tmp40 tmp42 = tmp31 + tmp41 tl.store(out_ptr0 + x7, tmp42, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (1, 4), (4, 1)) assert_size_stride(primals_3, (1,), (1,)) assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_tanh_0[grid(256)](primals_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 buf2 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_3, reinterpret_tensor(buf0, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_2, (4, 1), (1, 4), 0), alpha=1, beta=1, out=buf2) del primals_2 del primals_3 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__softmax_add_1[grid(64)](primals_4, buf2, 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_mul_sum_2[grid(256)](buf0, primals_4, buf2, buf3, buf4, buf5, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf3 del buf4 return buf5, primals_4, buf0, buf2 class SelfAttentionPooling(nn.Module): """ Implementation of SelfAttentionPooling Original Paper: Self-Attention Encoding and Pooling for Speaker Recognition https://arxiv.org/pdf/2008.01077v1.pdf """ def __init__(self, input_dim): super(SelfAttentionPooling, self).__init__() self.W = nn.Linear(input_dim, 1) self.softmax = nn.functional.softmax def forward(self, batch_rep, att_mask): """ input: batch_rep : size (N, T, H), N: batch size, T: sequence length, H: Hidden dimension attention_weight: att_w : size (N, T, 1) return: utter_rep: size (N, H) """ batch_rep.shape[1] att_logits = self.W(batch_rep).squeeze(-1) att_logits = att_mask + att_logits att_w = self.softmax(att_logits, dim=-1).unsqueeze(-1) utter_rep = torch.sum(batch_rep * att_w, dim=1) return utter_rep class SAPNew(nn.Module): """ Self Attention Pooling module incoporate attention mask""" def __init__(self, out_dim): super(SAPNew, self).__init__() self.act_fn = nn.Tanh() self.sap_layer = SelfAttentionPooling(out_dim) def forward(self, input_0, input_1): primals_2 = self.sap_layer.W.weight primals_3 = self.sap_layer.W.bias primals_1 = input_0 primals_4 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
sumanthd17/s3prl
SAP
false
13,007
[ "MIT" ]
0
bb74c705295d121c4308ceb6b6a2c8d1814d6f4c
https://github.com/sumanthd17/s3prl/tree/bb74c705295d121c4308ceb6b6a2c8d1814d6f4c
MultiHeadAttention
import torch import torch.nn as nn from torch import matmul class ScaledDotProductAttention(nn.Module): """ Scaled Dot-Product Attention """ def __init__(self, temperature, attn_dropout=0.1): super().__init__() self.temperature = temperature self.dropout = nn.Dropout(attn_dropout, inplace=False) self.softmax = nn.Softmax(dim=-1) def forward(self, q, k, v, mask=None): attn = matmul(q / self.temperature, k.transpose(2, 3)) if mask is not None: attn = attn.masked_fill(mask == 0, -1000000000.0) attn = self.dropout(self.softmax(attn)) output = matmul(attn, v) return output, attn class MultiHeadAttention(nn.Module): def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1): super().__init__() self.d_v = d_v self.d_k = d_k self.n_head = n_head self.w_qs = nn.Linear(d_model, n_head * d_k, bias=False) self.w_ks = nn.Linear(d_model, n_head * d_k, bias=False) self.w_vs = nn.Linear(d_model, n_head * d_v, bias=False) self.fc = nn.Linear(n_head * d_v, d_model, bias=False) self.attention = ScaledDotProductAttention(temperature=d_k ** 0.5) self.dropout = nn.Dropout(dropout, inplace=True) self.layer_norm = nn.LayerNorm(d_model, eps=1e-06) def forward(self, q, k, v, mask=None): sz_b, len_q, len_k, len_v = q.size(0), q.size(1), k.size(1), v.size(1) d_k, d_v, n_head = self.d_k, self.d_v, self.n_head residual = q q = self.w_qs(q).view(sz_b, len_q, n_head, d_k) k = self.w_ks(k).view(sz_b, len_k, n_head, d_k) v = self.w_vs(v).view(sz_b, len_v, n_head, d_v) q, k, v = q.transpose(1, 2), k.transpose(1, 2), v.transpose(1, 2) if mask is not None: mask = mask.unsqueeze(1) q, attn = self.attention(q, k, v, mask) q = q.transpose(1, 2).contiguous().view(sz_b, len_q, -1) q = self.dropout(self.fc(q)) q += residual q = self.layer_norm(q) return q, attn def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4]), torch.rand([4, 4, 4]) ] def get_init_inputs(): return [[], {'n_head': 4, 'd_model': 4, 'd_k': 4, 'd_v': 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 from torch import matmul 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_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 % 4 x1 = xindex // 4 % 4 x2 = xindex // 16 % 4 x3 = xindex // 64 x4 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1 + 64 * x3), xmask) tmp1 = 0.5 tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x4, tmp2, xmask) @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) @triton.jit def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @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 x0 = xindex % 4 x1 = xindex // 4 % 4 x2 = xindex // 16 % 4 x3 = xindex // 64 x4 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1 + 64 * x3), xmask) tl.store(out_ptr0 + x4, tmp0, xmask) @triton.jit def triton_poi_fused_native_layer_norm_5(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp9 = tmp7 + tmp8 tmp10 = tmp6 + tmp9 tmp13 = tmp11 + tmp12 tmp14 = tmp10 + tmp13 tmp15 = 4.0 tmp16 = tmp14 / tmp15 tmp17 = tmp2 - tmp16 tmp18 = tmp17 * tmp17 tmp19 = tmp5 - tmp16 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp22 = tmp9 - tmp16 tmp23 = tmp22 * tmp22 tmp24 = tmp21 + tmp23 tmp25 = tmp13 - tmp16 tmp26 = tmp25 * tmp25 tmp27 = tmp24 + tmp26 tmp28 = tmp27 / tmp15 tl.store(out_ptr0 + x0, tmp16, xmask) tl.store(out_ptr1 + x0, tmp28, xmask) @triton.jit def triton_poi_fused_native_layer_norm_6(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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-06 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, primals_8, primals_9) = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_3, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_4, (16, 4), (4, 1)) assert_size_stride(primals_5, (16, 4), (4, 1)) assert_size_stride(primals_6, (16, 4), (4, 1)) assert_size_stride(primals_7, (4, 16), (16, 1)) assert_size_stride(primals_8, (4,), (1,)) assert_size_stride(primals_9, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 16), (16, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 16), (1, 4), 0), out=buf0) del primals_4 buf1 = empty_strided_cuda((16, 16), (16, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_2, (16, 4), (4, 1), 0), reinterpret_tensor(primals_5, (4, 16), (1, 4), 0), out=buf1) del primals_5 buf2 = empty_strided_cuda((16, 16), (16, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (16, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 16), (1, 4), 0), out=buf2) del primals_6 buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clone_div_0[grid(256)](buf0, buf3, 256, XBLOCK=256, num_warps=4, num_stages=1) buf4 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 triton_poi_fused_clone_1[grid(64, 4)](buf1, buf4, 64, 4, XBLOCK=4, YBLOCK=32, num_warps=4, num_stages=1) buf5 = reinterpret_tensor(buf1, (16, 4, 4), (16, 4, 1), 0) del buf1 extern_kernels.bmm(reinterpret_tensor(buf3, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf4, (16, 4, 4), (16, 4, 1), 0), out=buf5) buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__softmax_2[grid(256)](buf5, buf6, 256, XBLOCK=256, num_warps=4, num_stages=1) buf7 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf5 triton_poi_fused__softmax_3[grid(256)](buf6, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1) buf8 = buf6 del buf6 triton_poi_fused_clone_4[grid(256)](buf2, buf8, 256, XBLOCK=256, num_warps=4, num_stages=1) buf9 = reinterpret_tensor(buf2, (16, 4, 4), (16, 4, 1), 0) del buf2 extern_kernels.bmm(reinterpret_tensor(buf7, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf8, (16, 4, 4), (16, 4, 1), 0), out=buf9) buf10 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_clone_4[grid(256)](buf9, buf10, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf9 buf11 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf10, (16, 16), (16, 1), 0), reinterpret_tensor(primals_7, (16, 4), (1, 16), 0), out=buf11) buf12 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf13 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) triton_poi_fused_native_layer_norm_5[grid(16)](buf11, primals_1, buf12, buf13, 16, XBLOCK=16, num_warps=1, num_stages=1) buf14 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_6[grid(64)](buf11, primals_1, buf12, buf13, primals_8, primals_9, buf14, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf12 del buf13 del primals_9 return buf14, buf7, primals_1, primals_8, reinterpret_tensor(primals_2, (16, 4), (4, 1), 0), reinterpret_tensor(primals_3, (16, 4), (4, 1), 0 ), buf7, reinterpret_tensor(buf10, (16, 16), (16, 1), 0 ), buf11, primals_7, reinterpret_tensor(buf8, (16, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf3, (16, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf4, (16, 4, 4), (16, 1, 4), 0) class ScaledDotProductAttention(nn.Module): """ Scaled Dot-Product Attention """ def __init__(self, temperature, attn_dropout=0.1): super().__init__() self.temperature = temperature self.dropout = nn.Dropout(attn_dropout, inplace=False) self.softmax = nn.Softmax(dim=-1) def forward(self, q, k, v, mask=None): attn = matmul(q / self.temperature, k.transpose(2, 3)) if mask is not None: attn = attn.masked_fill(mask == 0, -1000000000.0) attn = self.dropout(self.softmax(attn)) output = matmul(attn, v) return output, attn class MultiHeadAttentionNew(nn.Module): def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1): super().__init__() self.d_v = d_v self.d_k = d_k self.n_head = n_head self.w_qs = nn.Linear(d_model, n_head * d_k, bias=False) self.w_ks = nn.Linear(d_model, n_head * d_k, bias=False) self.w_vs = nn.Linear(d_model, n_head * d_v, bias=False) self.fc = nn.Linear(n_head * d_v, d_model, bias=False) self.attention = ScaledDotProductAttention(temperature=d_k ** 0.5) self.dropout = nn.Dropout(dropout, inplace=True) self.layer_norm = nn.LayerNorm(d_model, eps=1e-06) def forward(self, input_0, input_1, input_2): primals_4 = self.w_qs.weight primals_5 = self.w_ks.weight primals_6 = self.w_vs.weight primals_7 = self.fc.weight primals_8 = self.layer_norm.weight primals_9 = self.layer_norm.bias primals_1 = input_0 primals_2 = input_1 primals_3 = input_2 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9]) return output[0], output[1]
superMC5657/BiLSTMTransformer
MultiHeadAttention
false
13,008
[ "MIT" ]
0
43aa7bb4d8831a898c79ea89fcb1d3f5e09d564a
https://github.com/superMC5657/BiLSTMTransformer/tree/43aa7bb4d8831a898c79ea89fcb1d3f5e09d564a
AttentionUnit
import torch import torch.nn as nn def calc_mean_std(feat, eps=1e-05): size = feat.size() assert len(size) == 4 N, C = size[:2] feat_var = feat.view(N, C, -1).var(dim=2) + eps feat_std = feat_var.sqrt().view(N, C, 1, 1) feat_mean = feat.view(N, C, -1).mean(dim=2).view(N, C, 1, 1) return feat_mean, feat_std def mean_variance_norm(feat): size = feat.size() mean, std = calc_mean_std(feat) normalized_feat = (feat - mean.expand(size)) / std.expand(size) return normalized_feat class AttentionUnit(nn.Module): def __init__(self, channels): super(AttentionUnit, self).__init__() self.relu6 = nn.ReLU6() self.f = nn.Conv2d(channels, channels // 2, (1, 1)) self.g = nn.Conv2d(channels, channels // 2, (1, 1)) self.h = nn.Conv2d(channels, channels // 2, (1, 1)) self.out_conv = nn.Conv2d(channels // 2, channels, (1, 1)) self.softmax = nn.Softmax(dim=-1) def forward(self, Fc, Fs): B, C, H, W = Fc.shape f_Fc = self.relu6(self.f(mean_variance_norm(Fc))) g_Fs = self.relu6(self.g(mean_variance_norm(Fs))) h_Fs = self.relu6(self.h(Fs)) f_Fc = f_Fc.view(f_Fc.shape[0], f_Fc.shape[1], -1).permute(0, 2, 1) g_Fs = g_Fs.view(g_Fs.shape[0], g_Fs.shape[1], -1) Attention = self.softmax(torch.bmm(f_Fc, g_Fs)) h_Fs = h_Fs.view(h_Fs.shape[0], h_Fs.shape[1], -1) Fcs = torch.bmm(h_Fs, Attention.permute(0, 2, 1)) Fcs = Fcs.view(B, C // 2, H, W) Fcs = self.relu6(self.out_conv(Fcs)) return Fcs def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime 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_per_fused_div_mean_sub_var_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]) tmp3 = 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] tmp18 = tl.sum(tmp3, 1)[:, None] tmp19 = 16.0 tmp20 = tmp18 / tmp19 tmp21 = tmp0 - tmp20 tmp22 = 15.0 tmp23 = tmp16 / tmp22 tmp24 = 1e-05 tmp25 = tmp23 + tmp24 tmp26 = libdevice.sqrt(tmp25) tmp27 = tmp21 / tmp26 tl.store(out_ptr2 + (r1 + 16 * x0), tmp27, xmask) @triton.jit def triton_poi_fused_convolution_hardtanh_hardtanh_backward_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 2 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = 6.0 tmp6 = triton_helpers.minimum(tmp4, tmp5) tmp7 = tmp2 <= tmp3 tmp8 = tmp2 >= tmp5 tmp9 = tmp7 | tmp8 tl.store(out_ptr0 + x3, tmp6, xmask) tl.store(out_ptr1 + x3, tmp9, xmask) @triton.jit def triton_per_fused__softmax_2(in_ptr0, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 64 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, float('-inf')) tmp4 = triton_helpers.max2(tmp3, 1)[:, None] tmp5 = tmp0 - tmp4 tmp6 = tl_math.exp(tmp5) tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK]) tmp9 = tl.where(xmask, tmp7, 0) tmp10 = tl.sum(tmp9, 1)[:, None] tmp11 = tmp6 / tmp10 tl.store(out_ptr2 + (r1 + 16 * x0), tmp11, xmask) @triton.jit def triton_poi_fused_convolution_hardtanh_hardtanh_backward_3(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = 6.0 tmp6 = triton_helpers.minimum(tmp4, tmp5) tmp7 = tmp2 <= tmp3 tmp8 = tmp2 >= tmp5 tmp9 = tmp7 | tmp8 tl.store(out_ptr0 + x3, tmp6, xmask) tl.store(out_ptr1 + x3, tmp9, 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, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (2, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_3, (2,), (1,)) assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_5, (2, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_6, (2,), (1,)) assert_size_stride(primals_7, (2, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_8, (2,), (1,)) assert_size_stride(primals_9, (4, 2, 1, 1), (2, 1, 1, 1)) assert_size_stride(primals_10, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_per_fused_div_mean_sub_var_0[grid(16)](primals_1, buf4, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del primals_1 buf5 = extern_kernels.convolution(buf4, primals_2, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf5, (4, 2, 4, 4), (32, 16, 4, 1)) buf10 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_per_fused_div_mean_sub_var_0[grid(16)](primals_4, buf10, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) buf11 = extern_kernels.convolution(buf10, primals_5, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf11, (4, 2, 4, 4), (32, 16, 4, 1)) buf12 = extern_kernels.convolution(primals_4, primals_7, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf12, (4, 2, 4, 4), (32, 16, 4, 1)) buf13 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.float32) buf26 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.bool) triton_poi_fused_convolution_hardtanh_hardtanh_backward_1[grid(128)]( buf5, primals_3, buf13, buf26, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_3 buf14 = buf5 del buf5 buf25 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.bool) triton_poi_fused_convolution_hardtanh_hardtanh_backward_1[grid(128)]( buf11, primals_6, buf14, buf25, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_6 buf15 = empty_strided_cuda((4, 16, 16), (256, 16, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf13, (4, 16, 2), (32, 1, 16 ), 0), reinterpret_tensor(buf14, (4, 2, 16), (32, 16, 1), 0), out=buf15) buf18 = empty_strided_cuda((4, 16, 16), (256, 16, 1), torch.float32) triton_per_fused__softmax_2[grid(64)](buf15, buf18, 64, 16, XBLOCK= 32, num_warps=4, num_stages=1) del buf15 buf19 = buf11 del buf11 buf24 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.bool) triton_poi_fused_convolution_hardtanh_hardtanh_backward_1[grid(128)]( buf12, primals_8, buf19, buf24, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_8 buf20 = reinterpret_tensor(buf12, (4, 2, 16), (32, 16, 1), 0) del buf12 extern_kernels.bmm(reinterpret_tensor(buf19, (4, 2, 16), (32, 16, 1 ), 0), reinterpret_tensor(buf18, (4, 16, 16), (256, 1, 16), 0), out=buf20) buf21 = extern_kernels.convolution(reinterpret_tensor(buf20, (4, 2, 4, 4), (32, 16, 4, 1), 0), primals_9, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf21, (4, 4, 4, 4), (64, 16, 4, 1)) buf22 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf23 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_convolution_hardtanh_hardtanh_backward_3[grid(256)]( buf21, primals_10, buf22, buf23, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf21 del primals_10 return (buf22, primals_2, primals_4, primals_5, primals_7, primals_9, buf4, buf10, buf18, reinterpret_tensor(buf20, (4, 2, 4, 4), (32, 16, 4, 1), 0), buf23, reinterpret_tensor(buf19, (4, 16, 2), (32, 1, 16), 0), reinterpret_tensor(buf13, (4, 2, 16), (32, 16, 1), 0), reinterpret_tensor(buf14, (4, 16, 2), (32, 1, 16), 0), buf24, buf25, buf26) def calc_mean_std(feat, eps=1e-05): size = feat.size() assert len(size) == 4 N, C = size[:2] feat_var = feat.view(N, C, -1).var(dim=2) + eps feat_std = feat_var.sqrt().view(N, C, 1, 1) feat_mean = feat.view(N, C, -1).mean(dim=2).view(N, C, 1, 1) return feat_mean, feat_std def mean_variance_norm(feat): size = feat.size() mean, std = calc_mean_std(feat) normalized_feat = (feat - mean.expand(size)) / std.expand(size) return normalized_feat class AttentionUnitNew(nn.Module): def __init__(self, channels): super(AttentionUnitNew, self).__init__() self.relu6 = nn.ReLU6() self.f = nn.Conv2d(channels, channels // 2, (1, 1)) self.g = nn.Conv2d(channels, channels // 2, (1, 1)) self.h = nn.Conv2d(channels, channels // 2, (1, 1)) self.out_conv = nn.Conv2d(channels // 2, channels, (1, 1)) self.softmax = nn.Softmax(dim=-1) def forward(self, input_0, input_1): primals_2 = self.f.weight primals_3 = self.f.bias primals_5 = self.g.weight primals_6 = self.g.bias primals_7 = self.h.weight primals_8 = self.h.bias primals_9 = self.out_conv.weight primals_10 = self.out_conv.bias primals_1 = input_0 primals_4 = 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]
sugi-san/PAMA
AttentionUnit
false
13,009
[ "MIT" ]
0
95141ebf0d3b61828a0e545f989f96b8ef569f34
https://github.com/sugi-san/PAMA/tree/95141ebf0d3b61828a0e545f989f96b8ef569f34
Critic
import torch import numpy as np import torch.nn as nn import torch.nn.functional as F def hidden_init(layer): in_size = layer.weight.data.size()[0] lim = 1.0 / np.sqrt(in_size) return -lim, lim class Critic(nn.Module): def __init__(self, state_size, action_size, seed=0, fc1_size=128, fc2_size=128): super(Critic, self).__init__() self.seed = torch.manual_seed(seed) self.state_size = state_size self.action_size = action_size self.fc1 = nn.Linear(state_size, fc1_size) self.fc2 = nn.Linear(fc1_size + action_size, fc2_size) self.fc3 = nn.Linear(fc2_size, 1) self.bn1 = nn.BatchNorm1d(fc1_size) self.bn2 = nn.BatchNorm1d(fc2_size) self.reset_params() def reset_params(self): self.fc1.weight.data.uniform_(*hidden_init(self.fc1)) self.fc2.weight.data.uniform_(*hidden_init(self.fc2)) self.fc3.weight.data.uniform_(-0.003, 0.003) def forward(self, state, action): x1 = F.relu(self.fc1(state)) x1 = self.bn1(x1) x = torch.cat((x1, action), dim=1) x = F.relu(self.fc2(x)) return self.fc3(x) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'state_size': 4, 'action_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import numpy as np import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_cat_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 528 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 132 x1 = xindex // 132 x2 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 128, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (128 * x1 + x0), tmp4 & xmask, eviction_policy ='evict_last', other=0.0) tmp6 = tl.full([1], 0, tl.int32) tmp7 = triton_helpers.maximum(tmp6, tmp5) tmp8 = tl.load(in_ptr1 + x0, tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp9 = tmp7 - tmp8 tmp10 = tl.load(in_ptr2 + x0, tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp11 = 1e-05 tmp12 = tmp10 + tmp11 tmp13 = libdevice.sqrt(tmp12) tmp14 = tl.full([1], 1, tl.int32) tmp15 = tmp14 / tmp13 tmp16 = 1.0 tmp17 = tmp15 * tmp16 tmp18 = tmp9 * tmp17 tmp19 = tl.load(in_ptr3 + x0, tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp20 = tmp18 * tmp19 tmp21 = tl.load(in_ptr4 + x0, tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp22 = tmp20 + tmp21 tmp23 = tl.full(tmp22.shape, 0.0, tmp22.dtype) tmp24 = tl.where(tmp4, tmp22, tmp23) tmp25 = tmp0 >= tmp3 tl.full([1], 132, tl.int64) tmp28 = tl.load(in_ptr5 + (4 * x1 + (-128 + x0)), tmp25 & xmask, eviction_policy='evict_last', other=0.0) tmp29 = tl.where(tmp4, tmp24, tmp28) tl.store(out_ptr0 + x2, tmp29, xmask) @triton.jit def triton_poi_fused_relu_1(in_out_ptr0, in_ptr0, 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 % 128 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12 ) = args args.clear() assert_size_stride(primals_1, (128, 4), (4, 1)) assert_size_stride(primals_2, (128,), (1,)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (128,), (1,)) assert_size_stride(primals_5, (128,), (1,)) assert_size_stride(primals_6, (128,), (1,)) assert_size_stride(primals_7, (128,), (1,)) assert_size_stride(primals_8, (4, 4), (4, 1)) assert_size_stride(primals_9, (128, 132), (132, 1)) assert_size_stride(primals_10, (128,), (1,)) assert_size_stride(primals_11, (1, 128), (128, 1)) assert_size_stride(primals_12, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 128), (128, 1), torch.float32) extern_kernels.addmm(primals_2, primals_3, reinterpret_tensor( primals_1, (4, 128), (1, 4), 0), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((4, 132), (132, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(528)](buf0, primals_4, primals_5, primals_6, primals_7, primals_8, buf1, 528, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 del primals_8 buf2 = empty_strided_cuda((4, 128), (128, 1), torch.float32) extern_kernels.mm(buf1, reinterpret_tensor(primals_9, (132, 128), ( 1, 132), 0), out=buf2) buf3 = buf2 del buf2 triton_poi_fused_relu_1[grid(512)](buf3, primals_10, 512, XBLOCK= 256, num_warps=4, num_stages=1) del primals_10 buf5 = empty_strided_cuda((4, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_12, buf3, reinterpret_tensor( primals_11, (128, 1), (1, 128), 0), alpha=1, beta=1, out=buf5) del primals_12 return (buf5, primals_3, primals_4, primals_5, primals_6, buf0, buf1, buf3, primals_11, primals_9) def hidden_init(layer): in_size = layer.weight.data.size()[0] lim = 1.0 / np.sqrt(in_size) return -lim, lim class CriticNew(nn.Module): def __init__(self, state_size, action_size, seed=0, fc1_size=128, fc2_size=128): super(CriticNew, self).__init__() self.seed = torch.manual_seed(seed) self.state_size = state_size self.action_size = action_size self.fc1 = nn.Linear(state_size, fc1_size) self.fc2 = nn.Linear(fc1_size + action_size, fc2_size) self.fc3 = nn.Linear(fc2_size, 1) self.bn1 = nn.BatchNorm1d(fc1_size) self.bn2 = nn.BatchNorm1d(fc2_size) self.reset_params() def reset_params(self): self.fc1.weight.data.uniform_(*hidden_init(self.fc1)) self.fc2.weight.data.uniform_(*hidden_init(self.fc2)) self.fc3.weight.data.uniform_(-0.003, 0.003) def forward(self, input_0, input_1): primals_1 = self.fc1.weight primals_2 = self.fc1.bias primals_9 = self.fc2.weight primals_4 = self.fc2.bias primals_11 = self.fc3.weight primals_12 = self.fc3.bias primals_5 = self.bn1.weight primals_6 = self.bn1.bias primals_7 = self.bn2.weight primals_10 = self.bn2.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, primals_9, primals_10, primals_11, primals_12]) return output[0]
swastiknath/rl_ud_2
Critic
false
13,010
[ "MIT" ]
0
666e538f967252fa609c6b31cb5d66f9415eae82
https://github.com/swastiknath/rl_ud_2/tree/666e538f967252fa609c6b31cb5d66f9415eae82
LinearEmbedding
import math import torch import torch.utils.data import torch.nn as nn class LinearEmbedding(nn.Module): def __init__(self, inp_size, d_model): super(LinearEmbedding, self).__init__() self.lut = nn.Linear(inp_size, d_model) self.d_model = d_model def forward(self, x): return self.lut(x) * math.sqrt(self.d_model) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'inp_size': 4, 'd_model': 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 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 = 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 = 2.0 tmp4 = tmp2 * tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 get_raw_stream(0) triton_poi_fused_mul_0[grid(256)](buf1, primals_2, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 return buf1, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0) class LinearEmbeddingNew(nn.Module): def __init__(self, inp_size, d_model): super(LinearEmbeddingNew, self).__init__() self.lut = nn.Linear(inp_size, d_model) self.d_model = d_model def forward(self, input_0): primals_1 = self.lut.weight primals_2 = self.lut.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
swift88-clone/Trajectory-Transformer
LinearEmbedding
false
13,011
[ "MIT" ]
0
62983b645ec88d8972bc2c2af1b7b4a299d3feb0
https://github.com/swift88-clone/Trajectory-Transformer/tree/62983b645ec88d8972bc2c2af1b7b4a299d3feb0
FFN
import torch import torch.nn as nn import torch.utils.data class Conv(nn.Module): """ Convolution Module """ def __init__(self, in_channels, out_channels, kernel_size=1, stride=1, padding=0, dilation=1, bias=True, w_init='linear'): """ :param in_channels: dimension of input :param out_channels: dimension of output :param kernel_size: size of kernel :param stride: size of stride :param padding: size of padding :param dilation: dilation rate :param bias: boolean. if True, bias is included. :param w_init: str. weight inits with xavier initialization. """ super(Conv, self).__init__() self.conv = nn.Conv1d(in_channels, out_channels, kernel_size= kernel_size, stride=stride, padding=padding, dilation=dilation, bias=bias) nn.init.xavier_uniform_(self.conv.weight, gain=nn.init. calculate_gain(w_init)) def forward(self, x): x = self.conv(x) return x class FFN(nn.Module): """ Positionwise Feed-Forward Network """ def __init__(self, num_hidden): """ :param num_hidden: dimension of hidden """ super(FFN, self).__init__() self.w_1 = Conv(num_hidden, num_hidden * 4, kernel_size=3, padding= 1, w_init='relu') self.w_2 = Conv(num_hidden * 4, num_hidden, kernel_size=3, padding=1) self.dropout = nn.Dropout(p=0.1) self.layer_norm = nn.LayerNorm(num_hidden) def forward(self, input_): x = input_ x = self.w_2(torch.relu(self.w_1(x))) x = x + input_ x = self.dropout(x) x = self.layer_norm(x) return x def get_inputs(): return [torch.rand([4, 4])] def get_init_inputs(): return [[], {'num_hidden': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 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_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) @triton.jit def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x2, tmp2, xmask) @triton.jit def triton_poi_fused_add_native_layer_norm_2(in_ptr0, in_ptr1, 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_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_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, 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 + 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, 1)) assert_size_stride(primals_2, (16, 4, 3), (12, 3, 1)) assert_size_stride(primals_3, (16,), (1,)) assert_size_stride(primals_4, (4, 16, 3), (48, 3, 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 = extern_kernels.convolution(reinterpret_tensor(primals_1, (1, 4, 4), (16, 4, 1), 0), primals_2, stride=(1,), padding=(1,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf0, (1, 16, 4), (64, 4, 1)) buf1 = reinterpret_tensor(buf0, (16, 4), (4, 1), 0) del buf0 buf7 = empty_strided_cuda((16, 4), (4, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(64)](buf1, primals_3, buf7, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_3 buf2 = extern_kernels.convolution(reinterpret_tensor(buf1, (1, 16, 4), (0, 4, 1), 0), primals_4, stride=(1,), padding=(1,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf2, (1, 4, 4), (16, 4, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_1[grid(16)](buf3, primals_5, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_5 buf4 = empty_strided_cuda((4, 1), (1, 4), torch.float32) buf5 = empty_strided_cuda((4, 1), (1, 4), torch.float32) triton_poi_fused_add_native_layer_norm_2[grid(4)](buf3, primals_1, buf4, buf5, 4, XBLOCK=4, num_warps=1, num_stages=1) buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_add_native_layer_norm_3[grid(16)](buf3, primals_1, buf4, buf5, primals_6, primals_7, buf6, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf4 del buf5 del primals_7 return (buf6, primals_1, primals_2, primals_4, primals_6, reinterpret_tensor(buf1, (1, 16, 4), (64, 4, 1), 0), buf3, buf7) class Conv(nn.Module): """ Convolution Module """ def __init__(self, in_channels, out_channels, kernel_size=1, stride=1, padding=0, dilation=1, bias=True, w_init='linear'): """ :param in_channels: dimension of input :param out_channels: dimension of output :param kernel_size: size of kernel :param stride: size of stride :param padding: size of padding :param dilation: dilation rate :param bias: boolean. if True, bias is included. :param w_init: str. weight inits with xavier initialization. """ super(Conv, self).__init__() self.conv = nn.Conv1d(in_channels, out_channels, kernel_size= kernel_size, stride=stride, padding=padding, dilation=dilation, bias=bias) nn.init.xavier_uniform_(self.conv.weight, gain=nn.init. calculate_gain(w_init)) def forward(self, x): x = self.conv(x) return x class FFNNew(nn.Module): """ Positionwise Feed-Forward Network """ def __init__(self, num_hidden): """ :param num_hidden: dimension of hidden """ super(FFNNew, self).__init__() self.w_1 = Conv(num_hidden, num_hidden * 4, kernel_size=3, padding= 1, w_init='relu') self.w_2 = Conv(num_hidden * 4, num_hidden, kernel_size=3, padding=1) self.dropout = nn.Dropout(p=0.1) self.layer_norm = nn.LayerNorm(num_hidden) def forward(self, input_0): primals_2 = self.w_1.conv.weight primals_3 = self.w_1.conv.bias primals_4 = self.w_2.conv.weight primals_5 = self.w_2.conv.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]
supikiti/FastSpeech
FFN
false
13,012
[ "MIT" ]
0
775a9429c273450aefc2d346e5fc66c3f1e36832
https://github.com/supikiti/FastSpeech/tree/775a9429c273450aefc2d346e5fc66c3f1e36832
HubertFeatureProjection
from _paritybench_helpers import _mock_config import torch from torch import nn import torch.utils.checkpoint class HubertFeatureProjection(nn.Module): def __init__(self, config): super().__init__() self.layer_norm = nn.LayerNorm(config.conv_dim[-1], eps=config. layer_norm_eps) self.projection = nn.Linear(config.conv_dim[-1], config.hidden_size) self.dropout = nn.Dropout(config.feat_proj_dropout) def forward(self, hidden_states): hidden_states = self.layer_norm(hidden_states) hidden_states = self.projection(hidden_states) hidden_states = self.dropout(hidden_states) return hidden_states def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'config': _mock_config(conv_dim=[4, 4], layer_norm_eps=1, hidden_size=4, feat_proj_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 from torch import nn import torch.utils.checkpoint assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_native_layer_norm_0(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tmp9 = tmp0 - tmp8 tmp10 = tmp9 * tmp9 tmp11 = tmp1 - tmp8 tmp12 = tmp11 * tmp11 tmp13 = tmp10 + tmp12 tmp14 = tmp3 - tmp8 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp8 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp19 / tmp7 tmp21 = 1.0 tmp22 = tmp20 + tmp21 tmp23 = libdevice.rsqrt(tmp22) tl.store(out_ptr0 + x0, tmp8, xmask) tl.store(out_ptr1 + x0, tmp23, xmask) @triton.jit def triton_poi_fused_native_layer_norm_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4,), (1,)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf1 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) get_raw_stream(0) triton_poi_fused_native_layer_norm_0[grid(64)](primals_3, buf0, buf1, 64, XBLOCK=64, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_1[grid(256)](primals_3, buf0, buf1, primals_1, primals_2, buf2, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf0 del buf1 del primals_1 del primals_2 buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf2, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf3) del primals_5 return reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), primals_3, reinterpret_tensor(buf2, (64, 4), (4, 1), 0), primals_4 class HubertFeatureProjectionNew(nn.Module): def __init__(self, config): super().__init__() self.layer_norm = nn.LayerNorm(config.conv_dim[-1], eps=config. layer_norm_eps) self.projection = nn.Linear(config.conv_dim[-1], config.hidden_size) self.dropout = nn.Dropout(config.feat_proj_dropout) def forward(self, input_0): primals_1 = self.layer_norm.weight primals_2 = self.layer_norm.bias primals_4 = self.projection.weight primals_5 = self.projection.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
Clemens123/transformers
HubertFeatureProjection
false
13,013
[ "Apache-2.0" ]
0
22abe7bbc587c16ec30f9d1aa549dcbeba6e9e26
https://github.com/Clemens123/transformers/tree/22abe7bbc587c16ec30f9d1aa549dcbeba6e9e26
MaskNorm
import torch from torch import nn class MaskNorm(nn.Module): def __init__(self, norm_nc): super(MaskNorm, self).__init__() self.norm_layer = nn.InstanceNorm2d(norm_nc, affine=False) def normalize_region(self, region, mask): _b, _c, h, w = region.size() num_pixels = mask.sum((2, 3), keepdim=True) num_pixels[num_pixels == 0] = 1 mu = region.sum((2, 3), keepdim=True) / num_pixels normalized_region = self.norm_layer(region + (1 - mask) * mu) return normalized_region * torch.sqrt(num_pixels / (h * w)) def forward(self, x, mask): mask = mask.detach() normalized_foreground = self.normalize_region(x * mask, mask) normalized_background = self.normalize_region(x * (1 - mask), 1 - mask) return normalized_foreground + normalized_background def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'norm_nc': 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 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__native_batch_norm_legit_add_div_index_put_lift_fresh_mul_rsub_sqrt_sum_0( in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, out_ptr0, out_ptr3, out_ptr6, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 16 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp16 = tl.load(in_ptr1 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, 0) tmp4 = tl.sum(tmp3, 1)[:, None] tmp5 = 1.0 tmp6 = tmp5 - tmp0 tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK]) tmp9 = tl.where(xmask, tmp7, 0) tmp10 = tl.sum(tmp9, 1)[:, None] tmp11 = 0.0 tmp12 = tmp4 == tmp11 tmp13 = tl.where(tmp12, tmp5, tmp4) tmp14 = tmp10 == tmp11 tmp15 = tl.where(tmp14, tmp5, tmp10) tmp17 = tmp16 * tmp0 tmp18 = tl.broadcast_to(tmp17, [XBLOCK, RBLOCK]) tmp20 = tl.where(xmask, tmp18, 0) tmp21 = tl.sum(tmp20, 1)[:, None] tmp22 = tmp21 / tmp13 tmp23 = tmp6 * tmp22 tmp24 = tmp17 + tmp23 tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK]) tl.where(xmask, tmp25, 0) tmp28 = tl.broadcast_to(tmp25, [XBLOCK, RBLOCK]) tmp30 = tl.where(xmask, tmp28, 0) tmp31 = tl.sum(tmp30, 1)[:, None] tmp32 = tl.full([XBLOCK, 1], 16, tl.int32) tmp33 = tmp32.to(tl.float32) tmp34 = tmp31 / tmp33 tmp35 = tmp25 - tmp34 tmp36 = tmp35 * tmp35 tmp37 = tl.broadcast_to(tmp36, [XBLOCK, RBLOCK]) tmp39 = tl.where(xmask, tmp37, 0) tmp40 = tl.sum(tmp39, 1)[:, None] tmp41 = tmp16 * tmp6 tmp42 = tl.broadcast_to(tmp41, [XBLOCK, RBLOCK]) tmp44 = tl.where(xmask, tmp42, 0) tmp45 = tl.sum(tmp44, 1)[:, None] tmp46 = tmp5 - tmp6 tmp47 = tmp45 / tmp15 tmp48 = tmp46 * tmp47 tmp49 = tmp41 + tmp48 tmp50 = tl.broadcast_to(tmp49, [XBLOCK, RBLOCK]) tl.where(xmask, tmp50, 0) tmp53 = tl.broadcast_to(tmp50, [XBLOCK, RBLOCK]) tmp55 = tl.where(xmask, tmp53, 0) tmp56 = tl.sum(tmp55, 1)[:, None] tmp57 = tmp56 / tmp33 tmp58 = tmp50 - tmp57 tmp59 = tmp58 * tmp58 tmp60 = tl.broadcast_to(tmp59, [XBLOCK, RBLOCK]) tmp62 = tl.where(xmask, tmp60, 0) tmp63 = tl.sum(tmp62, 1)[:, None] tmp64 = tmp24 - tmp34 tmp65 = 16.0 tmp66 = tmp40 / tmp65 tmp67 = 1e-05 tmp68 = tmp66 + tmp67 tmp69 = libdevice.rsqrt(tmp68) tmp70 = tmp64 * tmp69 tmp71 = 0.0625 tmp72 = tmp13 * tmp71 tmp73 = libdevice.sqrt(tmp72) tmp74 = tmp70 * tmp73 tmp75 = tmp49 - tmp57 tmp76 = tmp63 / tmp65 tmp77 = tmp76 + tmp67 tmp78 = libdevice.rsqrt(tmp77) tmp79 = tmp75 * tmp78 tmp80 = tmp15 * tmp71 tmp81 = libdevice.sqrt(tmp80) tmp82 = tmp79 * tmp81 tmp83 = tmp74 + tmp82 tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp13, xmask) tl.debug_barrier() tl.store(in_out_ptr1 + x0, tmp15, xmask) tl.store(out_ptr6 + (r1 + 16 * x0), tmp83, xmask) tl.store(out_ptr0 + x0, tmp21, xmask) tl.store(out_ptr3 + x0, tmp45, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) buf7 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) buf2 = buf1 del buf1 buf8 = buf7 del buf7 buf0 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) buf6 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) buf12 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_per_fused__native_batch_norm_legit_add_div_index_put_lift_fresh_mul_rsub_sqrt_sum_0[ grid(16)](buf2, buf8, arg0_1, arg1_1, buf0, buf6, buf12, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 del buf0 del buf2 del buf6 del buf8 return buf12, class MaskNormNew(nn.Module): def __init__(self, norm_nc): super(MaskNormNew, self).__init__() self.norm_layer = nn.InstanceNorm2d(norm_nc, affine=False) def normalize_region(self, region, mask): _b, _c, h, w = region.size() num_pixels = mask.sum((2, 3), keepdim=True) num_pixels[num_pixels == 0] = 1 mu = region.sum((2, 3), keepdim=True) / num_pixels normalized_region = self.norm_layer(region + (1 - mask) * mu) return normalized_region * torch.sqrt(num_pixels / (h * w)) def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
swpang/xray-align-AR
MaskNorm
false
13,014
[ "MIT" ]
0
43cb0173ada9d1d71a6a923d605cb6fdae4d27aa
https://github.com/swpang/xray-align-AR/tree/43cb0173ada9d1d71a6a923d605cb6fdae4d27aa
FeatureCorrelation
import torch from torch import nn class FeatureCorrelation(nn.Module): def __init__(self): super(FeatureCorrelation, self).__init__() def forward(self, featureA, featureB): b, c, h, w = featureA.size() featureA = featureA.permute(0, 3, 2, 1).reshape(b, w * h, c) featureB = featureB.reshape(b, c, h * w) corr = torch.bmm(featureA, featureB).reshape(b, w * h, h, w) return corr def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clone_0(in_ptr0, out_ptr0, 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) tl.store(out_ptr0 + (x5 + 16 * y4), 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), (64, 16, 4, 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, 16, 16), (256, 16, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf0, (4, 16, 4), (64, 4, 1), 0), reinterpret_tensor(arg1_1, (4, 4, 16), (64, 16, 1), 0), out =buf1) del arg1_1 del buf0 return reinterpret_tensor(buf1, (4, 16, 4, 4), (256, 16, 4, 1), 0), class FeatureCorrelationNew(nn.Module): def __init__(self): super(FeatureCorrelationNew, 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]
swpang/xray-align-AR
FeatureCorrelation
false
13,015
[ "MIT" ]
0
43cb0173ada9d1d71a6a923d605cb6fdae4d27aa
https://github.com/swpang/xray-align-AR/tree/43cb0173ada9d1d71a6a923d605cb6fdae4d27aa
Discriminator
import torch import torch.nn as nn class Discriminator(nn.Module): def __init__(self, num_inputs, hidden_size): super(Discriminator, self).__init__() self.linear1 = nn.Linear(num_inputs, hidden_size) self.linear2 = nn.Linear(hidden_size, hidden_size) self.linear3 = nn.Linear(hidden_size, 1) self.linear3.weight.data.mul_(0.1) self.linear3.bias.data.mul_(0.0) def forward(self, x): x = torch.tanh(self.linear1(x)) x = torch.tanh(self.linear2(x)) prob = torch.sigmoid(self.linear3(x)) return prob def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_inputs': 4, 'hidden_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_tanh_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = libdevice.tanh(tmp2) tl.store(in_out_ptr0 + x2, tmp3, xmask) @triton.jit def triton_poi_fused_sigmoid_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp3 = tmp0 + tmp2 tmp4 = tl.sigmoid(tmp3) tl.store(in_out_ptr0 + x0, tmp4, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (1, 4), (4, 1)) assert_size_stride(primals_7, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 get_raw_stream(0) triton_poi_fused_tanh_0[grid(256)](buf1, primals_2, 256, XBLOCK=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, 1), (1, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 1), (1, 4), 0), out=buf4) buf5 = reinterpret_tensor(buf4, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf4 triton_poi_fused_sigmoid_1[grid(64)](buf5, primals_7, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_7 return buf5, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf1, buf3, buf5, primals_6, primals_4 class DiscriminatorNew(nn.Module): def __init__(self, num_inputs, hidden_size): super(DiscriminatorNew, self).__init__() self.linear1 = nn.Linear(num_inputs, hidden_size) self.linear2 = nn.Linear(hidden_size, hidden_size) self.linear3 = nn.Linear(hidden_size, 1) self.linear3.weight.data.mul_(0.1) self.linear3.bias.data.mul_(0.0) def forward(self, input_0): primals_1 = self.linear1.weight primals_2 = self.linear1.bias primals_4 = self.linear2.weight primals_5 = self.linear2.bias primals_6 = self.linear3.weight primals_7 = self.linear3.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
syuntoku14/flow
Discriminator
false
13,016
[ "MIT" ]
0
3a1157cde31d0b7d6a3cc2f91eef0ec9ea53575e
https://github.com/syuntoku14/flow/tree/3a1157cde31d0b7d6a3cc2f91eef0ec9ea53575e
Generator
import torch import torch.nn as nn import torch.nn.functional as F class Generator(nn.Module): def __init__(self, input_size, hidden_size, out_size): super(Generator, self).__init__() self.map1 = nn.Linear(input_size, hidden_size) self.map2 = nn.Linear(hidden_size, hidden_size) self.map3 = nn.Linear(hidden_size, out_size) def forward(self, x): x = F.elu(self.map1(x)) x = torch.sigmoid(self.map2(x)) return self.map3(x) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'hidden_size': 4, 'out_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_elu_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.0 tmp2 = tmp0 > tmp1 tmp3 = 1.0 tmp4 = tmp0 * tmp3 tmp5 = libdevice.expm1(tmp4) tmp6 = tmp5 * tmp3 tmp7 = tl.where(tmp2, tmp4, tmp6) tl.store(out_ptr0 + x0, tmp7, xmask) @triton.jit def triton_poi_fused_sigmoid_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 = tl.sigmoid(tmp2) tl.store(in_out_ptr0 + x2, tmp3, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_elu_0[grid(256)](buf0, buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf2 triton_poi_fused_sigmoid_1[grid(256)](buf3, primals_5, 256, XBLOCK= 128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf4) del primals_7 return reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf0, reinterpret_tensor(buf1, (64, 4), (4, 1), 0 ), buf3, primals_6, primals_4 class GeneratorNew(nn.Module): def __init__(self, input_size, hidden_size, out_size): super(GeneratorNew, self).__init__() self.map1 = nn.Linear(input_size, hidden_size) self.map2 = nn.Linear(hidden_size, hidden_size) self.map3 = nn.Linear(hidden_size, out_size) def forward(self, input_0): primals_1 = self.map1.weight primals_2 = self.map1.bias primals_4 = self.map2.weight primals_5 = self.map2.bias primals_6 = self.map3.weight primals_7 = self.map3.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
tan-huaiyu/Network_science-and-Evolutionary_dynamics
Generator
false
13,017
[ "Apache-2.0" ]
0
4bdaaed18c6f230213fd69a31144db8e97eb0c7b
https://github.com/tan-huaiyu/Network_science-and-Evolutionary_dynamics/tree/4bdaaed18c6f230213fd69a31144db8e97eb0c7b
DepthwiseSeparableConv
import torch import torch.nn as nn import torch.nn.functional as F class DepthwiseSeparableConv(nn.Module): def __init__(self, in_ch, out_ch, k, bias=True): super().__init__() self.depthwise_conv = nn.Conv1d(in_channels=in_ch, out_channels= in_ch, kernel_size=k, groups=in_ch, padding=k // 2, bias=False) self.pointwise_conv = nn.Conv1d(in_channels=in_ch, out_channels= out_ch, kernel_size=1, padding=0, bias=bias) def forward(self, x): return F.relu(self.pointwise_conv(self.depthwise_conv(x))) def get_inputs(): return [torch.rand([4, 4])] def get_init_inputs(): return [[], {'in_ch': 4, 'out_ch': 4, 'k': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 20 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 5 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) 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, 4), (4, 1)) assert_size_stride(primals_3, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_4, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(reinterpret_tensor(primals_2, (1, 4, 4), (16, 4, 1), 0), primals_1, stride=(1,), padding=(2,), dilation=(1,), transposed=False, output_padding=(0,), groups=4, bias=None) assert_size_stride(buf0, (1, 4, 5), (20, 5, 1)) buf1 = extern_kernels.convolution(buf0, primals_3, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf1, (1, 4, 5), (20, 5, 1)) buf2 = reinterpret_tensor(buf1, (4, 5), (5, 1), 0) del buf1 buf3 = empty_strided_cuda((4, 5), (5, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(20)](buf2, primals_4, buf3, 20, XBLOCK=32, num_warps=1, num_stages=1) del primals_4 return buf2, primals_1, primals_3, reinterpret_tensor(primals_2, (1, 4, 4), (16, 4, 1), 0), buf0, buf3 class DepthwiseSeparableConvNew(nn.Module): def __init__(self, in_ch, out_ch, k, bias=True): super().__init__() self.depthwise_conv = nn.Conv1d(in_channels=in_ch, out_channels= in_ch, kernel_size=k, groups=in_ch, padding=k // 2, bias=False) self.pointwise_conv = nn.Conv1d(in_channels=in_ch, out_channels= out_ch, kernel_size=1, padding=0, bias=bias) def forward(self, input_0): primals_1 = self.depthwise_conv.weight primals_3 = self.pointwise_conv.weight primals_4 = self.pointwise_conv.bias primals_2 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
raghavjajodia/squad
DepthwiseSeparableConv
false
13,018
[ "MIT" ]
0
4eb6ccdfaa904aa97215c8bc65cd77b54ff54601
https://github.com/raghavjajodia/squad/tree/4eb6ccdfaa904aa97215c8bc65cd77b54ff54601
Matcher
import math import torch import torch.nn as nn class Matcher(nn.Module): """ Matching between a pair of nodes to conduct link prediction. Use multi-head attention as matching model. """ def __init__(self, n_hid): super(Matcher, self).__init__() self.left_linear = nn.Linear(n_hid, n_hid) self.right_linear = nn.Linear(n_hid, n_hid) self.sqrt_hd = math.sqrt(n_hid) self.cache = None def forward(self, x, y, infer=False, pair=False): ty = self.right_linear(y) if infer: """ During testing, we will consider millions or even billions of nodes as candidates (x). It's not possible to calculate them again for different query (y) Since the model is fixed, we propose to cache them, and dirrectly use the results. """ if self.cache is not None: tx = self.cache else: tx = self.left_linear(x) self.cache = tx else: tx = self.left_linear(x) if pair: res = (tx * ty).sum(dim=-1) else: res = torch.matmul(tx, ty.transpose(0, 1)) return res / self.sqrt_hd def __repr__(self): return '{}(n_hid={})'.format(self.__class__.__name__, self.n_hid) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'n_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 import math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clone_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 x2 = xindex // 16 % 4 x3 = xindex // 64 x4 = xindex % 16 x0 = xindex % 4 x5 = xindex tmp0 = tl.load(in_ptr0 + (x4 + 16 * x3 + 64 * x2), xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + x5, tmp2, xmask) @triton.jit def triton_poi_fused_div_1(in_out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = 0.5 tmp2 = tmp0 * tmp1 tl.store(in_out_ptr0 + x0, tmp2, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = 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)) 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.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((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clone_0[grid(256)](buf0, primals_2, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf3 = reinterpret_tensor(buf0, (16, 4, 4), (16, 4, 1), 0) del buf0 extern_kernels.bmm(reinterpret_tensor(buf1, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf2, (16, 4, 4), (16, 4, 1), 0), out=buf3) buf4 = reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf3 triton_poi_fused_div_1[grid(256)](buf4, 256, XBLOCK=256, num_warps= 4, num_stages=1) return buf4, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(primals_6, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (16, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf2, (16, 4, 4), (16, 1, 4), 0) class MatcherNew(nn.Module): """ Matching between a pair of nodes to conduct link prediction. Use multi-head attention as matching model. """ def __init__(self, n_hid): super(MatcherNew, self).__init__() self.left_linear = nn.Linear(n_hid, n_hid) self.right_linear = nn.Linear(n_hid, n_hid) self.sqrt_hd = math.sqrt(n_hid) self.cache = None def __repr__(self): return '{}(n_hid={})'.format(self.__class__.__name__, self.n_hid) def forward(self, input_0, input_1): primals_1 = self.left_linear.weight primals_2 = self.left_linear.bias primals_4 = self.right_linear.weight primals_5 = self.right_linear.bias primals_3 = input_0 primals_6 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6]) return output[0]
syyunn/pyHGT-1
Matcher
false
13,019
[ "MIT" ]
0
ad0918a48777add1495b80f35b5f2b7a44b74625
https://github.com/syyunn/pyHGT-1/tree/ad0918a48777add1495b80f35b5f2b7a44b74625
FusionLayer
import torch from torch import nn from torch.nn import init class FusionLayer(nn.Module): def __init__(self, nums=6): super(FusionLayer, self).__init__() self.weights = nn.Parameter(torch.randn(nums)) self.nums = nums self._reset_parameters() def _reset_parameters(self): init.constant_(self.weights, 1 / self.nums) def forward(self, x): for i in range(self.nums): out = self.weights[i] * x[i] if i == 0 else out + self.weights[i ] * x[i] return out def get_inputs(): return [torch.rand([6, 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 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_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK]) tmp2 = tl.load(in_ptr1 + x0, xmask) tmp4 = tl.load(in_ptr0 + 1) tmp5 = tl.broadcast_to(tmp4, [XBLOCK]) tmp6 = tl.load(in_ptr1 + (64 + x0), xmask) tmp9 = tl.load(in_ptr0 + 2) tmp10 = tl.broadcast_to(tmp9, [XBLOCK]) tmp11 = tl.load(in_ptr1 + (128 + x0), xmask) tmp14 = tl.load(in_ptr0 + 3) tmp15 = tl.broadcast_to(tmp14, [XBLOCK]) tmp16 = tl.load(in_ptr1 + (192 + x0), xmask) tmp19 = tl.load(in_ptr0 + 4) tmp20 = tl.broadcast_to(tmp19, [XBLOCK]) tmp21 = tl.load(in_ptr1 + (256 + x0), xmask) tmp24 = tl.load(in_ptr0 + 5) tmp25 = tl.broadcast_to(tmp24, [XBLOCK]) tmp26 = tl.load(in_ptr1 + (320 + x0), xmask) tmp3 = tmp1 * tmp2 tmp7 = tmp5 * tmp6 tmp8 = tmp3 + tmp7 tmp12 = tmp10 * tmp11 tmp13 = tmp8 + tmp12 tmp17 = tmp15 * tmp16 tmp18 = tmp13 + tmp17 tmp22 = tmp20 * tmp21 tmp23 = tmp18 + tmp22 tmp27 = tmp25 * tmp26 tmp28 = tmp23 + tmp27 tl.store(in_out_ptr0 + x0, tmp28, xmask) def call(args): primals_1, primals_2 = args args.clear() assert_size_stride(primals_1, (6,), (1,)) assert_size_stride(primals_2, (6, 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 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_add_mul_0[grid(64)](buf1, primals_1, primals_2, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_1 return buf1, reinterpret_tensor(primals_2, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(primals_2, (4, 4, 4), (16, 4, 1), 64 ), reinterpret_tensor(primals_2, (4, 4, 4), (16, 4, 1), 128 ), reinterpret_tensor(primals_2, (4, 4, 4), (16, 4, 1), 192 ), reinterpret_tensor(primals_2, (4, 4, 4), (16, 4, 1), 256 ), reinterpret_tensor(primals_2, (4, 4, 4), (16, 4, 1), 320) class FusionLayerNew(nn.Module): def __init__(self, nums=6): super(FusionLayerNew, self).__init__() self.weights = nn.Parameter(torch.randn(nums)) self.nums = nums self._reset_parameters() def _reset_parameters(self): init.constant_(self.weights, 1 / self.nums) def forward(self, input_0): primals_1 = self.weights primals_2 = input_0 output = call([primals_1, primals_2]) return output[0]
tansyl/6883-SOD
FusionLayer
false
13,020
[ "MIT" ]
0
3a32c45be1c6c449fc7de145fe01746e3eeb16df
https://github.com/tansyl/6883-SOD/tree/3a32c45be1c6c449fc7de145fe01746e3eeb16df
GRUCell
import torch import numpy as np import torch.nn.functional as F import torch.utils.data import torch.nn as nn class GRUCell(nn.Module): def __init__(self, input_size, hidden_size, bias=True): super(GRUCell, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.bias = bias self.fc_ir = nn.Linear(input_size, hidden_size, bias=bias) self.fc_hr = nn.Linear(hidden_size, hidden_size, bias=bias) self.fc_iz = nn.Linear(input_size, hidden_size, bias=bias) self.fc_hz = nn.Linear(hidden_size, hidden_size, bias=bias) self.fc_in = nn.Linear(input_size, hidden_size, bias=bias) self.fc_hn = nn.Linear(hidden_size, hidden_size, bias=bias) self.init_parameters() def init_parameters(self): std = 1.0 / np.sqrt(self.hidden_size) for w in self.parameters(): w.data.uniform_(-std, std) def forward(self, x, h): x = x.view(-1, x.shape[1]) i_r = self.fc_ir(x) h_r = self.fc_hr(h) i_z = self.fc_iz(x) h_z = self.fc_hz(h) i_n = self.fc_in(x) h_n = self.fc_hn(h) resetgate = F.sigmoid(i_r + h_r) inputgate = F.sigmoid(i_z + h_z) newgate = F.tanh(i_n + resetgate * h_n) hy = newgate + inputgate * (h - newgate) return hy def get_inputs(): return [torch.rand([4, 4, 64, 4]), torch.rand([4, 4, 1024, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'hidden_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import numpy as np 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_add_mul_sigmoid_sub_tanh_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, 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 % 4096 x2 = xindex tmp0 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x0, None, eviction_policy='evict_last') tmp2 = tl.load(in_ptr2 + x2, None) tmp5 = tl.load(in_ptr3 + x2, None) tmp9 = tl.load(in_ptr4 + x0, None, eviction_policy='evict_last') tmp10 = tl.load(in_ptr5 + x2, None) tmp13 = tl.load(in_ptr6 + x2, None) tmp3 = tmp1 + tmp2 tmp4 = tl.sigmoid(tmp3) tmp6 = tmp4 * tmp5 tmp7 = tmp0 + tmp6 tmp8 = libdevice.tanh(tmp7) tmp11 = tmp9 + tmp10 tmp12 = tl.sigmoid(tmp11) tmp14 = tmp13 - tmp8 tmp15 = tmp12 * tmp14 tmp16 = tmp8 + tmp15 tl.store(out_ptr0 + x2, tmp16, 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, 4, 64, 4), (1024, 256, 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, 1024, 4), (16384, 4096, 4, 1)) assert_size_stride(primals_7, (4, 4), (4, 1)) assert_size_stride(primals_8, (4,), (1,)) assert_size_stride(primals_9, (4, 4), (4, 1)) assert_size_stride(primals_10, (4,), (1,)) assert_size_stride(primals_11, (4, 4), (4, 1)) assert_size_stride(primals_12, (4,), (1,)) assert_size_stride(primals_13, (4, 4), (4, 1)) assert_size_stride(primals_14, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((1024, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_3, reinterpret_tensor(primals_1, (1024, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_2 del primals_3 buf1 = empty_strided_cuda((16384, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(primals_6, ( 16384, 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((1024, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_8, reinterpret_tensor(primals_1, (1024, 4), (4, 1), 0), reinterpret_tensor(primals_7, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf2) del primals_7 del primals_8 buf3 = empty_strided_cuda((16384, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_10, reinterpret_tensor(primals_6, ( 16384, 4), (4, 1), 0), reinterpret_tensor(primals_9, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf3) del primals_10 del primals_9 buf4 = empty_strided_cuda((1024, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_12, reinterpret_tensor(primals_1, ( 1024, 4), (4, 1), 0), reinterpret_tensor(primals_11, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf4) del primals_11 del primals_12 buf5 = empty_strided_cuda((16384, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_14, reinterpret_tensor(primals_6, ( 16384, 4), (4, 1), 0), reinterpret_tensor(primals_13, (4, 4), ( 1, 4), 0), alpha=1, beta=1, out=buf5) del primals_13 del primals_14 buf6 = empty_strided_cuda((4, 4, 1024, 4), (16384, 4096, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mul_sigmoid_sub_tanh_0[grid(65536)](buf4, buf0, buf1, buf5, buf2, buf3, primals_6, buf6, 65536, XBLOCK=512, num_warps=4, num_stages=1) return buf6, primals_6, reinterpret_tensor(primals_1, (1024, 4), (4, 1), 0 ), buf0, buf1, buf2, buf3, buf4, buf5 class GRUCellNew(nn.Module): def __init__(self, input_size, hidden_size, bias=True): super(GRUCellNew, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.bias = bias self.fc_ir = nn.Linear(input_size, hidden_size, bias=bias) self.fc_hr = nn.Linear(hidden_size, hidden_size, bias=bias) self.fc_iz = nn.Linear(input_size, hidden_size, bias=bias) self.fc_hz = nn.Linear(hidden_size, hidden_size, bias=bias) self.fc_in = nn.Linear(input_size, hidden_size, bias=bias) self.fc_hn = nn.Linear(hidden_size, hidden_size, bias=bias) self.init_parameters() def init_parameters(self): std = 1.0 / np.sqrt(self.hidden_size) for w in self.parameters(): w.data.uniform_(-std, std) def forward(self, input_0, input_1): primals_2 = self.fc_ir.weight primals_3 = self.fc_ir.bias primals_4 = self.fc_hr.weight primals_5 = self.fc_hr.bias primals_7 = self.fc_iz.weight primals_8 = self.fc_iz.bias primals_9 = self.fc_hz.weight primals_10 = self.fc_hz.bias primals_11 = self.fc_in.weight primals_12 = self.fc_in.bias primals_13 = self.fc_hn.weight primals_14 = self.fc_hn.bias primals_1 = 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, primals_11, primals_12, primals_13, primals_14]) return output[0]
systemshift/PyGrid
GRUCell
false
13,021
[ "Apache-2.0" ]
0
d0ee3df8731a7576d6689fa8b4f5d3fe05ac11ff
https://github.com/systemshift/PyGrid/tree/d0ee3df8731a7576d6689fa8b4f5d3fe05ac11ff
Debayer2x2
import torch import torch.nn import torch.nn.functional class Debayer2x2(torch.nn.Module): """Demosaicing of Bayer images using 2x2 convolutions. Requires BG-Bayer color filter array layout. That is, the image[1,1]='B', image[1,2]='G'. This corresponds to OpenCV naming conventions. """ def __init__(self): super(Debayer2x2, self).__init__() self.kernels = torch.nn.Parameter(torch.tensor([[1, 0], [0, 0], [0, 0.5], [0.5, 0], [0, 0], [0, 1]]).view(3, 1, 2, 2), requires_grad=False) def forward(self, x): """Debayer image. Parameters ---------- x : Bx1xHxW tensor Images to debayer Returns ------- rgb : Bx3xHxW tensor Color images in RGB channel order. """ x = torch.nn.functional.conv2d(x, self.kernels, stride=2) x = torch.nn.functional.interpolate(x, scale_factor=2, mode= 'bilinear', align_corners=False) return x def get_inputs(): return [torch.rand([4, 1, 64, 64])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn import torch.nn.functional assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused__to_copy__unsafe_index_add_arange_clamp_mul_sub_0( in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 64 % 64 x0 = xindex % 64 x2 = xindex // 4096 x4 = xindex tmp0 = x1 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 + tmp2 tmp4 = tmp3 * tmp2 tmp5 = tmp4 - tmp2 tmp6 = 0.0 tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp7.to(tl.int32) tmp9 = tl.full([1], 1, tl.int64) tmp10 = tmp8 + tmp9 tmp11 = tl.full([1], 31, tl.int64) tmp12 = triton_helpers.minimum(tmp10, tmp11) tmp13 = x0 tmp14 = tmp13.to(tl.float32) tmp15 = tmp14 + tmp2 tmp16 = tmp15 * tmp2 tmp17 = tmp16 - tmp2 tmp18 = triton_helpers.maximum(tmp17, tmp6) tmp19 = tmp18.to(tl.int32) tmp20 = tmp19 + tmp9 tmp21 = triton_helpers.minimum(tmp20, tmp11) tmp22 = tl.load(in_ptr0 + (tmp21 + 32 * tmp12 + 1024 * x2), None, eviction_policy='evict_last') tmp23 = tl.load(in_ptr0 + (tmp19 + 32 * tmp12 + 1024 * x2), None, eviction_policy='evict_last') tmp24 = tmp22 - tmp23 tmp25 = tmp19.to(tl.float32) tmp26 = tmp18 - tmp25 tmp27 = triton_helpers.maximum(tmp26, tmp6) tmp28 = 1.0 tmp29 = triton_helpers.minimum(tmp27, tmp28) tmp30 = tmp24 * tmp29 tmp31 = tmp23 + tmp30 tmp32 = tl.load(in_ptr0 + (tmp19 + 32 * tmp8 + 1024 * x2), None, eviction_policy='evict_last') tmp33 = tl.load(in_ptr0 + (tmp21 + 32 * tmp8 + 1024 * x2), None, eviction_policy='evict_last') tmp34 = tmp33 - tmp32 tmp35 = tmp34 * tmp29 tmp36 = tmp32 + tmp35 tmp37 = tmp31 - tmp36 tmp38 = tmp8.to(tl.float32) tmp39 = tmp7 - tmp38 tmp40 = triton_helpers.maximum(tmp39, tmp6) tmp41 = triton_helpers.minimum(tmp40, tmp28) tmp42 = tmp37 * tmp41 tmp43 = tmp36 + tmp42 tl.store(in_out_ptr0 + x4, tmp43, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (3, 1, 2, 2), (4, 4, 2, 1)) assert_size_stride(arg1_1, (4, 1, 64, 64), (4096, 4096, 64, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(arg1_1, arg0_1, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 3, 32, 32), (3072, 1024, 32, 1)) del arg0_1 del arg1_1 buf1 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.float32) buf2 = buf1 del buf1 buf3 = buf2 del buf2 get_raw_stream(0) triton_poi_fused__to_copy__unsafe_index_add_arange_clamp_mul_sub_0[grid (49152)](buf3, buf0, 49152, XBLOCK=256, num_warps=4, num_stages=1) del buf0 return buf3, class Debayer2x2New(torch.nn.Module): """Demosaicing of Bayer images using 2x2 convolutions. Requires BG-Bayer color filter array layout. That is, the image[1,1]='B', image[1,2]='G'. This corresponds to OpenCV naming conventions. """ def __init__(self): super(Debayer2x2New, self).__init__() self.kernels = torch.nn.Parameter(torch.tensor([[1, 0], [0, 0], [0, 0.5], [0.5, 0], [0, 0], [0, 1]]).view(3, 1, 2, 2), requires_grad=False) def forward(self, input_0): arg0_1 = self.kernels arg1_1 = input_0 output = call([arg0_1, arg1_1]) return output[0]
tasptz/pytorch-debayer
Debayer2x2
false
13,022
[ "MIT" ]
0
ec35f34a57c045eb2319f4ef87f371d95f7394c3
https://github.com/tasptz/pytorch-debayer/tree/ec35f34a57c045eb2319f4ef87f371d95f7394c3
PowerLaw_Compressed_Loss
import torch import torch.nn as nn import torch.utils.data class PowerLaw_Compressed_Loss(nn.Module): def __init__(self, power=0.3, complex_loss_ratio=0.113): super(PowerLaw_Compressed_Loss, self).__init__() self.power = power self.complex_loss_ratio = complex_loss_ratio self.criterion = nn.MSELoss() self.epsilon = 1e-16 def forward(self, prediction, target, seq_len=None, spec_phase=None): prediction = prediction + self.epsilon target = target + self.epsilon prediction = torch.pow(prediction, self.power) target = torch.pow(target, self.power) spec_loss = self.criterion(torch.abs(target), torch.abs(prediction)) complex_loss = self.criterion(target, prediction) loss = spec_loss + complex_loss * self.complex_loss_ratio return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import 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_add_mse_loss_mul_pow_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) tmp6 = tl.load(in_ptr1 + r0, None) tmp1 = 1e-16 tmp2 = tmp0 + tmp1 tmp3 = 0.3 tmp4 = libdevice.pow(tmp2, tmp3) tmp5 = tl_math.abs(tmp4) tmp7 = tmp6 + tmp1 tmp8 = libdevice.pow(tmp7, tmp3) tmp9 = tl_math.abs(tmp8) tmp10 = tmp5 - tmp9 tmp11 = tmp10 * tmp10 tmp12 = tl.broadcast_to(tmp11, [RBLOCK]) tmp14 = triton_helpers.promote_to_tensor(tl.sum(tmp12, 0)) tmp15 = tmp4 - tmp8 tmp16 = tmp15 * tmp15 tmp17 = tl.broadcast_to(tmp16, [RBLOCK]) tmp19 = triton_helpers.promote_to_tensor(tl.sum(tmp17, 0)) tmp20 = 256.0 tmp21 = tmp14 / tmp20 tmp22 = tmp19 / tmp20 tmp23 = 0.113 tmp24 = tmp22 * tmp23 tmp25 = tmp21 + tmp24 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp25, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf2 = buf0 del buf0 get_raw_stream(0) triton_per_fused_abs_add_mse_loss_mul_pow_0[grid(1)](buf2, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf2, class PowerLaw_Compressed_LossNew(nn.Module): def __init__(self, power=0.3, complex_loss_ratio=0.113): super(PowerLaw_Compressed_LossNew, self).__init__() self.power = power self.complex_loss_ratio = complex_loss_ratio self.criterion = nn.MSELoss() self.epsilon = 1e-16 def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
taylorjdlee/VoiceSplit
PowerLaw_Compressed_Loss
false
13,023
[ "Apache-2.0" ]
0
bd914c42ae065bdda95d81a0ce0c173c29bb040f
https://github.com/taylorjdlee/VoiceSplit/tree/bd914c42ae065bdda95d81a0ce0c173c29bb040f
Discriminator
import torch import torch.nn as nn import torch.nn.functional as F class Discriminator(nn.Module): def __init__(self, input_size, hidden_size, out_size): super(Discriminator, self).__init__() self.map1 = nn.Linear(input_size, hidden_size) self.map2 = nn.Linear(hidden_size, hidden_size) self.map3 = nn.Linear(hidden_size, out_size) def forward(self, x): x = F.relu(self.map1(x)) x = F.elu(self.map2(x)) return torch.sigmoid(self.map3(x)) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'hidden_size': 4, 'out_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._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_elu_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 = 0.0 tmp2 = tmp0 > tmp1 tmp3 = 1.0 tmp4 = tmp0 * tmp3 tmp5 = libdevice.expm1(tmp4) tmp6 = tmp5 * tmp3 tmp7 = tl.where(tmp2, tmp4, tmp6) tl.store(out_ptr0 + x0, tmp7, xmask) @triton.jit def triton_poi_fused_sigmoid_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.sigmoid(tmp2) tl.store(in_out_ptr0 + x2, tmp3, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 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_2, buf6, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf2) del primals_5 buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_elu_1[grid(256)](buf2, buf3, 256, XBLOCK=256, num_warps=4, num_stages=1) buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf3, (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_sigmoid_2[grid(256)](buf5, primals_7, 256, XBLOCK= 128, num_warps=4, num_stages=1) del primals_7 return buf5, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 4), (4, 1), 0 ), buf2, reinterpret_tensor(buf3, (64, 4), (4, 1), 0 ), buf5, primals_6, primals_4, buf6 class DiscriminatorNew(nn.Module): def __init__(self, input_size, hidden_size, out_size): super(DiscriminatorNew, self).__init__() self.map1 = nn.Linear(input_size, hidden_size) self.map2 = nn.Linear(hidden_size, hidden_size) self.map3 = nn.Linear(hidden_size, out_size) def forward(self, input_0): primals_1 = self.map1.weight primals_2 = self.map1.bias primals_4 = self.map2.weight primals_5 = self.map2.bias primals_6 = self.map3.weight primals_7 = self.map3.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
tan-huaiyu/Network_science-and-Evolutionary_dynamics
Discriminator
false
13,024
[ "Apache-2.0" ]
0
4bdaaed18c6f230213fd69a31144db8e97eb0c7b
https://github.com/tan-huaiyu/Network_science-and-Evolutionary_dynamics/tree/4bdaaed18c6f230213fd69a31144db8e97eb0c7b
Conv2
import math import torch import torch.nn as nn class Conv2(nn.Module): """ 1D conv with (kernel, stride)=(4, 2). Input: x: (N, 2L+2, in_channels) numeric tensor global_cond: (N, global_cond_channels) numeric tensor Output: y: (N, L, out_channels) numeric tensor """ def __init__(self, in_channels, out_channels, global_cond_channels): super().__init__() ksz = 4 self.out_channels = out_channels if 0 < global_cond_channels: self.w_cond = nn.Linear(global_cond_channels, 2 * out_channels, bias=False) self.conv_wide = nn.Conv1d(in_channels, 2 * out_channels, ksz, stride=2 ) wsize = 2.967 / math.sqrt(ksz * in_channels) self.conv_wide.weight.data.uniform_(-wsize, wsize) self.conv_wide.bias.data.zero_() def forward(self, x, global_cond): x1 = self.conv_wide(x.transpose(1, 2)).transpose(1, 2) if global_cond is not None: x2 = self.w_cond(global_cond).unsqueeze(1).expand(-1, x1.size(1 ), -1) else: x2 = torch.zeros_like(x1) a, b = (x1 + x2).split(self.out_channels, dim=2) return torch.sigmoid(a) * torch.tanh(b) def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4, 'global_cond_channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_0(in_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_mul_sigmoid_tanh_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 8 * x1), xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + (x0 + 8 * x1), xmask) tmp6 = tl.load(in_ptr0 + (4 + x0 + 8 * x1), xmask) tmp7 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr2 + (4 + x0 + 8 * x1), xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp5 = tl.sigmoid(tmp4) tmp8 = tmp6 + tmp7 tmp10 = tmp8 + tmp9 tmp11 = libdevice.tanh(tmp10) tmp12 = tmp5 * tmp11 tl.store(out_ptr0 + x2, tmp5, xmask) tl.store(out_ptr1 + x2, tmp11, xmask) tl.store(out_ptr2 + x2, tmp12, 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, 4, 4), (16, 4, 1)) assert_size_stride(primals_3, (8,), (1,)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (8, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_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=(2,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf1, (4, 8, 1), (8, 1, 1)) del buf0 buf2 = empty_strided_cuda((4, 8), (8, 1), torch.float32) extern_kernels.mm(primals_4, reinterpret_tensor(primals_5, (4, 8), (1, 4), 0), out=buf2) del primals_5 buf3 = empty_strided_cuda((4, 1, 4), (4, 4, 1), torch.float32) buf4 = empty_strided_cuda((4, 1, 4), (4, 4, 1), torch.float32) buf5 = empty_strided_cuda((4, 1, 4), (4, 4, 1), torch.float32) triton_poi_fused_mul_sigmoid_tanh_1[grid(16)](buf1, primals_3, buf2, buf3, buf4, buf5, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf1 del buf2 del primals_3 return buf5, primals_2, primals_4, reinterpret_tensor(primals_1, (4, 4, 4), (16, 1, 4), 0), buf3, buf4 class Conv2New(nn.Module): """ 1D conv with (kernel, stride)=(4, 2). Input: x: (N, 2L+2, in_channels) numeric tensor global_cond: (N, global_cond_channels) numeric tensor Output: y: (N, L, out_channels) numeric tensor """ def __init__(self, in_channels, out_channels, global_cond_channels): super().__init__() ksz = 4 self.out_channels = out_channels if 0 < global_cond_channels: self.w_cond = nn.Linear(global_cond_channels, 2 * out_channels, bias=False) self.conv_wide = nn.Conv1d(in_channels, 2 * out_channels, ksz, stride=2 ) wsize = 2.967 / math.sqrt(ksz * in_channels) self.conv_wide.weight.data.uniform_(-wsize, wsize) self.conv_wide.bias.data.zero_() def forward(self, input_0, input_1): primals_5 = self.w_cond.weight primals_2 = self.conv_wide.weight primals_3 = self.conv_wide.bias primals_1 = input_0 primals_4 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
tarepan/vqvaevc
Conv2
false
13,025
[ "MIT" ]
0
dabbb9bae5ccb9d5dcb110caf3f0a59f68006a97
https://github.com/tarepan/vqvaevc/tree/dabbb9bae5ccb9d5dcb110caf3f0a59f68006a97
Debayer3x3
import torch import torch.nn import torch.nn.functional class Debayer3x3(torch.nn.Module): """Demosaicing of Bayer images using 3x3 convolutions. Requires BG-Bayer color filter array layout. That is, the image[1,1]='B', image[1,2]='G'. This corresponds to OpenCV naming conventions. Compared to Debayer2x2 this method does not use upsampling. Instead, we identify five 3x3 interpolation kernels that are sufficient to reconstruct every color channel at every pixel location. We convolve the image with these 5 kernels using stride=1 and a one pixel replication padding. Finally, we gather the correct channel values for each pixel location. Todo so, we recognize that the Bayer pattern repeats horizontally and vertically every 2 pixels. Therefore, we define the correct index lookups for a 2x2 grid cell and then repeat to image dimensions. Note, in every 2x2 grid cell we have red, blue and two greens (G1,G2). The lookups for the two greens differ. """ def __init__(self): super(Debayer3x3, self).__init__() self.kernels = torch.nn.Parameter(torch.tensor([[0, 0, 0], [0, 1, 0 ], [0, 0, 0], [0, 0.25, 0], [0.25, 0, 0.25], [0, 0.25, 0], [ 0.25, 0, 0.25], [0, 0, 0], [0.25, 0, 0.25], [0, 0, 0], [0.5, 0, 0.5], [0, 0, 0], [0, 0.5, 0], [0, 0, 0], [0, 0.5, 0]]).view(5, 1, 3, 3), requires_grad=False) self.index = torch.nn.Parameter(torch.tensor([[0, 3], [4, 2], [1, 0 ], [0, 1], [2, 4], [3, 0]]).view(1, 3, 2, 2), requires_grad=False) def forward(self, x): """Debayer image. Parameters ---------- x : Bx1xHxW tensor Images to debayer Returns ------- rgb : Bx3xHxW tensor Color images in RGB channel order. """ B, _C, H, W = x.shape x = torch.nn.functional.pad(x, (1, 1, 1, 1), mode='replicate') c = torch.nn.functional.conv2d(x, self.kernels, stride=1) rgb = torch.gather(c, 1, self.index.repeat(B, 1, H // 2, W // 2)) return rgb def get_inputs(): return [torch.rand([4, 1, 4, 4])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn import torch.nn.functional assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_replication_pad2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 144 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 6 x1 = xindex // 6 % 6 x2 = xindex // 36 x3 = xindex tmp0 = tl.load(in_ptr0 + (4 * (3 * (3 <= 0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0)) + (0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0)) * (0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0) < 3)) + 16 * x2 + ( 3 * (3 <= 0 * (0 >= -1 + x0) + (-1 + x0) * (-1 + x0 > 0)) + (0 * (0 >= -1 + x0) + (-1 + x0) * (-1 + x0 > 0)) * (0 * (0 >= -1 + x0) + (-1 + x0) * (-1 + x0 > 0) < 3))), xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_poi_fused_gather_repeat_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 % 4 x2 = xindex // 16 % 3 x3 = xindex // 48 x4 = xindex % 16 x5 = xindex tmp0 = tl.load(in_ptr0 + (2 * (x1 % 2) + 4 * x2 + x0 % 2), xmask, eviction_policy='evict_last') tmp1 = tl.full([XBLOCK], 5, tl.int32) tmp2 = tmp0 + tmp1 tmp3 = tmp0 < 0 tmp4 = tl.where(tmp3, tmp2, tmp0) tl.device_assert((0 <= tmp4) & (tmp4 < 5) | ~xmask, 'index out of bounds: 0 <= tmp4 < 5') tmp6 = tl.load(in_ptr1 + (x4 + 16 * tmp4 + 80 * x3), xmask) tl.store(out_ptr0 + x5, tmp6, xmask) def call(args): arg0_1, arg1_1, arg2_1 = args args.clear() assert_size_stride(arg0_1, (4, 1, 4, 4), (16, 16, 4, 1)) assert_size_stride(arg1_1, (5, 1, 3, 3), (9, 9, 3, 1)) assert_size_stride(arg2_1, (1, 3, 2, 2), (12, 4, 2, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 1, 6, 6), (36, 36, 6, 1), torch.float32) get_raw_stream(0) triton_poi_fused_replication_pad2d_0[grid(144)](arg0_1, buf0, 144, XBLOCK=256, 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=1, bias=None) assert_size_stride(buf1, (4, 5, 4, 4), (80, 16, 4, 1)) del arg1_1 del buf0 buf2 = empty_strided_cuda((4, 3, 4, 4), (48, 16, 4, 1), torch.float32) triton_poi_fused_gather_repeat_1[grid(192)](arg2_1, buf1, buf2, 192, XBLOCK=128, num_warps=4, num_stages=1) del arg2_1 del buf1 return buf2, class Debayer3x3New(torch.nn.Module): """Demosaicing of Bayer images using 3x3 convolutions. Requires BG-Bayer color filter array layout. That is, the image[1,1]='B', image[1,2]='G'. This corresponds to OpenCV naming conventions. Compared to Debayer2x2 this method does not use upsampling. Instead, we identify five 3x3 interpolation kernels that are sufficient to reconstruct every color channel at every pixel location. We convolve the image with these 5 kernels using stride=1 and a one pixel replication padding. Finally, we gather the correct channel values for each pixel location. Todo so, we recognize that the Bayer pattern repeats horizontally and vertically every 2 pixels. Therefore, we define the correct index lookups for a 2x2 grid cell and then repeat to image dimensions. Note, in every 2x2 grid cell we have red, blue and two greens (G1,G2). The lookups for the two greens differ. """ def __init__(self): super(Debayer3x3New, self).__init__() self.kernels = torch.nn.Parameter(torch.tensor([[0, 0, 0], [0, 1, 0 ], [0, 0, 0], [0, 0.25, 0], [0.25, 0, 0.25], [0, 0.25, 0], [ 0.25, 0, 0.25], [0, 0, 0], [0.25, 0, 0.25], [0, 0, 0], [0.5, 0, 0.5], [0, 0, 0], [0, 0.5, 0], [0, 0, 0], [0, 0.5, 0]]).view(5, 1, 3, 3), requires_grad=False) self.index = torch.nn.Parameter(torch.tensor([[0, 3], [4, 2], [1, 0 ], [0, 1], [2, 4], [3, 0]]).view(1, 3, 2, 2), requires_grad=False) def forward(self, input_0): arg1_1 = self.kernels arg2_1 = self.index arg0_1 = input_0 output = call([arg0_1, arg1_1, arg2_1]) return output[0]
tasptz/pytorch-debayer
Debayer3x3
false
13,026
[ "MIT" ]
0
ec35f34a57c045eb2319f4ef87f371d95f7394c3
https://github.com/tasptz/pytorch-debayer/tree/ec35f34a57c045eb2319f4ef87f371d95f7394c3
nSGC
import math import torch import torch.nn.functional as F import torch.utils.dlpack import torch.nn as nn class nSGC(nn.Module): def __init__(self, nfeat, nclass): super(nSGC, self).__init__() self.W1 = nn.Linear(nfeat, nclass * 2) self.W2 = nn.Linear(nclass * 2, nclass) self.init() def init(self): stdv = 1.0 / math.sqrt(self.W1.weight.size(1)) self.W1.weight.data.uniform_(-stdv, stdv) def forward(self, x): x = self.W1(x) x = F.relu(x) x = self.W2(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'nfeat': 4, 'nclass': 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 math import torch.utils.dlpack 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 = 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_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (8, 4), (4, 1)) assert_size_stride(primals_2, (8,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 8), (8, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 8), (8, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 8), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 8), (128, 32, 8, 1), 0) del buf0 buf3 = empty_strided_cuda((4, 4, 4, 8), (128, 32, 8, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(512)](buf1, primals_2, buf3, 512, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 8), ( 8, 1), 0), reinterpret_tensor(primals_4, (8, 4), (1, 8), 0), alpha=1, beta=1, out=buf2) del primals_5 return reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 8), (8, 1), 0), primals_4, buf3 class nSGCNew(nn.Module): def __init__(self, nfeat, nclass): super(nSGCNew, self).__init__() self.W1 = nn.Linear(nfeat, nclass * 2) self.W2 = nn.Linear(nclass * 2, nclass) self.init() def init(self): stdv = 1.0 / math.sqrt(self.W1.weight.size(1)) self.W1.weight.data.uniform_(-stdv, stdv) def forward(self, input_0): primals_1 = self.W1.weight primals_2 = self.W1.bias primals_4 = self.W2.weight primals_5 = self.W2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
tealminivan/FinalProject
nSGC
false
13,027
[ "MIT" ]
0
ef6e0cda619b7e00f112ffadd56d259a5cc8a85b
https://github.com/tealminivan/FinalProject/tree/ef6e0cda619b7e00f112ffadd56d259a5cc8a85b
Joiner
import torch from torch import nn import torch.nn.functional as F class Joiner(nn.Module): def __init__(self, input_dim: 'int', output_dim: 'int'): super().__init__() self.output_linear = nn.Linear(input_dim, output_dim) def forward(self, encoder_out: 'torch.Tensor', decoder_out: 'torch.Tensor' ) ->torch.Tensor: """ Args: encoder_out: Output from the encoder. Its shape is (N, T, C). decoder_out: Output from the decoder. Its shape is (N, U, C). Returns: Return a tensor of shape (N, T, U, C). """ assert encoder_out.ndim == decoder_out.ndim == 3 assert encoder_out.size(0) == decoder_out.size(0) assert encoder_out.size(2) == decoder_out.size(2) encoder_out = encoder_out.unsqueeze(2) decoder_out = decoder_out.unsqueeze(1) logit = encoder_out + decoder_out logit = F.relu(logit) output = self.output_linear(logit) return output def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'output_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_relu_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x4 = xindex // 16 x3 = xindex // 64 x5 = xindex % 16 x6 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * x4), xmask, eviction_policy='evict_last' ) tmp1 = tl.load(in_ptr1 + (x5 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(out_ptr0 + x6, 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,), (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_relu_0[grid(256)](primals_1, primals_2, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 del primals_2 buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_4, reinterpret_tensor(buf0, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf1) del primals_3 del primals_4 return reinterpret_tensor(buf1, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(buf0, (64, 4), (4, 1), 0) class JoinerNew(nn.Module): def __init__(self, input_dim: 'int', output_dim: 'int'): super().__init__() self.output_linear = nn.Linear(input_dim, output_dim) def forward(self, input_0, input_1): primals_3 = self.output_linear.weight primals_4 = self.output_linear.bias primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
thangdepzai/icefall
Joiner
false
13,028
[ "Apache-2.0" ]
0
8c7995d493c4309c3d09bdabfa1ab12b4eec2657
https://github.com/thangdepzai/icefall/tree/8c7995d493c4309c3d09bdabfa1ab12b4eec2657
NNTest
import torch import torch.nn as nn import torch.nn.functional as F class NNTest(nn.Module): def __init__(self, input_size, output_size): super(NNTest, self).__init__() self.fc1 = nn.Linear(input_size, 50) self.fc2 = nn.Linear(50, 100) self.fc3 = nn.Linear(100, 50) self.fc4 = nn.Linear(50, output_size) def forward(self, x): x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.relu(self.fc3(x)) x = self.fc4(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'output_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 3200 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 50 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) @triton.jit def triton_poi_fused_relu_threshold_backward_1(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 6400 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex x0 = xindex % 100 x2 = xindex % 1600 x3 = xindex // 1600 tmp0 = tl.load(in_out_ptr0 + x4, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x4, tmp4, xmask) tl.store(out_ptr0 + (x2 + 1664 * x3), tmp6, xmask) 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, (50, 4), (4, 1)) assert_size_stride(primals_2, (50,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (100, 50), (50, 1)) assert_size_stride(primals_5, (100,), (1,)) assert_size_stride(primals_6, (50, 100), (100, 1)) assert_size_stride(primals_7, (50,), (1,)) assert_size_stride(primals_8, (4, 50), (50, 1)) assert_size_stride(primals_9, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 50), (50, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 50), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 50), (800, 200, 50, 1), 0) del buf0 buf9 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(3200)](buf1, primals_2, buf9, 3200, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 100), (100, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 50), (50, 1), 0), reinterpret_tensor(primals_4, (50, 100), (1, 50), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 100), (1600, 400, 100, 1), 0) del buf2 buf8 = empty_strided_cuda((4, 4, 4, 100), (1664, 400, 100, 1), torch.bool) triton_poi_fused_relu_threshold_backward_1[grid(6400)](buf3, primals_5, buf8, 6400, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 50), (50, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf3, (64, 100), (100, 1), 0), reinterpret_tensor(primals_6, (100, 50), (1, 100), 0), out=buf4) buf5 = reinterpret_tensor(buf4, (4, 4, 4, 50), (800, 200, 50, 1), 0) del buf4 buf7 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.bool) triton_poi_fused_relu_threshold_backward_0[grid(3200)](buf5, primals_7, buf7, 3200, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 buf6 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_9, reinterpret_tensor(buf5, (64, 50), (50, 1), 0), reinterpret_tensor(primals_8, (50, 4), (1, 50), 0), alpha=1, beta=1, out=buf6) del primals_9 return reinterpret_tensor(buf6, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 50), (50, 1), 0), reinterpret_tensor( buf3, (64, 100), (100, 1), 0), reinterpret_tensor(buf5, (64, 50), ( 50, 1), 0), primals_8, buf7, primals_6, buf8, primals_4, buf9 class NNTestNew(nn.Module): def __init__(self, input_size, output_size): super(NNTestNew, self).__init__() self.fc1 = nn.Linear(input_size, 50) self.fc2 = nn.Linear(50, 100) self.fc3 = nn.Linear(100, 50) self.fc4 = nn.Linear(50, output_size) 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_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]
tassotirap/data-science
NNTest
false
13,029
[ "Apache-2.0" ]
0
644bc351740cda90c0d8c907132d9da9630266c9
https://github.com/tassotirap/data-science/tree/644bc351740cda90c0d8c907132d9da9630266c9
ConvElement
import torch import torch.nn as nn import torch.nn.functional as F class ConvElement(nn.Module): """ Residual Core element used inside the NN. Control the number of filters and batch normalization. """ def __init__(self, input_size, num_filters, use_leaky=True, stride=1, leaky_p=0.2): super(ConvElement, self).__init__() self.use_lr = use_leaky self.leaky_p = leaky_p self.conv1 = nn.Conv3d(input_size, num_filters, kernel_size=3, padding=1, stride=stride) def forward(self, x): """ include residual model """ x_1 = self.conv1(x) return F.leaky_relu(x_1, self.leaky_p) if self.use_lr else F.relu(x_1) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'num_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 torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_leaky_relu_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): 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_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.2 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr1 + x2, tmp7, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 3, 3, 3), (108, 27, 9, 3, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(reinterpret_tensor(primals_3, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0), primals_1, 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(buf0, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1)) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_leaky_relu_0[grid(256)](buf0, primals_2, buf1, buf2, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf0 del primals_2 return buf2, primals_1, reinterpret_tensor(primals_3, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0), buf1 class ConvElementNew(nn.Module): """ Residual Core element used inside the NN. Control the number of filters and batch normalization. """ def __init__(self, input_size, num_filters, use_leaky=True, stride=1, leaky_p=0.2): super(ConvElementNew, self).__init__() self.use_lr = use_leaky self.leaky_p = leaky_p self.conv1 = nn.Conv3d(input_size, num_filters, kernel_size=3, padding=1, stride=stride) def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
tensormedical/PARIETAL
ConvElement
false
13,030
[ "Apache-2.0" ]
0
25bf1cf7828b24d60ccff42efbd0537989aaf160
https://github.com/tensormedical/PARIETAL/tree/25bf1cf7828b24d60ccff42efbd0537989aaf160
Hill
import torch import torch.nn as nn class Hill(nn.Module): def forward(self, p): n = 2 return 1 / (1 + p ** n) 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_add_mul_pow_reciprocal_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tmp0 * tmp0 tmp2 = 1.0 tmp3 = tmp1 + tmp2 tmp4 = tl.full([1], 1, tl.int32) tmp5 = tmp4 / tmp3 tmp6 = tmp5 * tmp2 tl.store(out_ptr0 + x0, tmp6, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mul_pow_reciprocal_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, class HillNew(nn.Module): def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
tianyu-lu/latent_ode
Hill
false
13,031
[ "MIT" ]
0
1a9e9415eda1837ed78e50009752b90eda3ca0db
https://github.com/tianyu-lu/latent_ode/tree/1a9e9415eda1837ed78e50009752b90eda3ca0db
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 = nn.Conv2d(3, 12, 5) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(12, 16, 5) self.fc1 = nn.Linear(16 * 5 * 5, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10) def forward(self, x): x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self.conv2(x))) x = x.view(-1, 16 * 5 * 5) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x def get_inputs(): return [torch.rand([4, 3, 32, 32])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_relu_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 37632 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 784 % 12 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, xmask) @triton.jit def triton_poi_fused_max_pool2d_with_indices_1(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 9408 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 14 x3 = xindex // 14 x2 = xindex // 2352 x4 = xindex % 2352 tmp0 = tl.load(in_ptr0 + (2 * x0 + 56 * x3), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 56 * x3), xmask, eviction_policy ='evict_last') tmp3 = tl.load(in_ptr0 + (28 + 2 * x0 + 56 * x3), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (29 + 2 * x0 + 56 * x3), xmask, eviction_policy='evict_last') tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = tmp1 > tmp0 tmp8 = tl.full([1], 1, tl.int8) tmp9 = tl.full([1], 0, tl.int8) tmp10 = tl.where(tmp7, tmp8, tmp9) tmp11 = tmp3 > tmp2 tmp12 = tl.full([1], 2, tl.int8) tmp13 = tl.where(tmp11, tmp12, tmp10) tmp14 = tmp5 > tmp4 tmp15 = tl.full([1], 3, tl.int8) tmp16 = tl.where(tmp14, tmp15, tmp13) tl.store(out_ptr0 + (x4 + 2368 * x2), tmp6, xmask) tl.store(out_ptr1 + (x4 + 2432 * x2), tmp16, xmask) @triton.jit def triton_poi_fused_convolution_relu_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 6400 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 100 % 16 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, xmask) @triton.jit def triton_poi_fused_max_pool2d_with_indices_3(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 1600 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 5 x1 = xindex // 5 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 20 * x1), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 20 * x1), xmask, eviction_policy ='evict_last') tmp7 = tl.load(in_ptr0 + (10 + 2 * x0 + 20 * x1), xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (11 + 2 * x0 + 20 * x1), xmask, eviction_policy='evict_last') tmp2 = tmp1 > tmp0 tmp3 = tl.full([1], 1, tl.int8) tmp4 = tl.full([1], 0, tl.int8) tmp5 = tl.where(tmp2, tmp3, tmp4) tmp6 = triton_helpers.maximum(tmp1, tmp0) tmp8 = tmp7 > tmp6 tmp9 = tl.full([1], 2, tl.int8) tmp10 = tl.where(tmp8, tmp9, tmp5) tmp11 = triton_helpers.maximum(tmp7, tmp6) tmp13 = tmp12 > tmp11 tmp14 = tl.full([1], 3, tl.int8) tmp15 = tl.where(tmp13, tmp14, tmp10) tmp16 = triton_helpers.maximum(tmp12, tmp11) tl.store(out_ptr0 + x2, tmp15, xmask) tl.store(out_ptr1 + x2, tmp16, xmask) @triton.jit def triton_poi_fused_relu_4(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 480 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 120 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_relu_5(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 336 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 84 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11) = args args.clear() assert_size_stride(primals_1, (12, 3, 5, 5), (75, 25, 5, 1)) assert_size_stride(primals_2, (12,), (1,)) assert_size_stride(primals_3, (4, 3, 32, 32), (3072, 1024, 32, 1)) assert_size_stride(primals_4, (16, 12, 5, 5), (300, 25, 5, 1)) assert_size_stride(primals_5, (16,), (1,)) assert_size_stride(primals_6, (120, 400), (400, 1)) assert_size_stride(primals_7, (120,), (1,)) assert_size_stride(primals_8, (84, 120), (120, 1)) assert_size_stride(primals_9, (84,), (1,)) assert_size_stride(primals_10, (10, 84), (84, 1)) assert_size_stride(primals_11, (10,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 12, 28, 28), (9408, 784, 28, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(37632)](buf1, primals_2, 37632, XBLOCK=512, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((4, 12, 14, 14), (2368, 196, 14, 1), torch.float32) buf3 = empty_strided_cuda((4, 12, 14, 14), (2432, 196, 14, 1), torch.int8) triton_poi_fused_max_pool2d_with_indices_1[grid(9408)](buf1, buf2, buf3, 9408, XBLOCK=128, num_warps=4, num_stages=1) buf4 = extern_kernels.convolution(buf2, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 16, 10, 10), (1600, 100, 10, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_relu_2[grid(6400)](buf5, primals_5, 6400, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf6 = empty_strided_cuda((4, 16, 5, 5), (400, 25, 5, 1), torch.int8) buf7 = empty_strided_cuda((4, 16, 5, 5), (400, 25, 5, 1), torch.float32 ) triton_poi_fused_max_pool2d_with_indices_3[grid(1600)](buf5, buf6, buf7, 1600, XBLOCK=256, num_warps=4, num_stages=1) buf8 = empty_strided_cuda((4, 120), (120, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf7, (4, 400), (400, 1), 0), reinterpret_tensor(primals_6, (400, 120), (1, 400), 0), out=buf8) buf9 = buf8 del buf8 triton_poi_fused_relu_4[grid(480)](buf9, primals_7, 480, XBLOCK=128, num_warps=4, num_stages=1) del primals_7 buf10 = empty_strided_cuda((4, 84), (84, 1), torch.float32) extern_kernels.mm(buf9, reinterpret_tensor(primals_8, (120, 84), (1, 120), 0), out=buf10) buf11 = buf10 del buf10 triton_poi_fused_relu_5[grid(336)](buf11, primals_9, 336, XBLOCK= 128, num_warps=4, num_stages=1) del primals_9 buf12 = empty_strided_cuda((4, 10), (10, 1), torch.float32) extern_kernels.addmm(primals_11, buf11, reinterpret_tensor( primals_10, (84, 10), (1, 84), 0), alpha=1, beta=1, out=buf12) del primals_11 return (buf12, primals_1, primals_3, primals_4, buf1, buf2, buf3, buf5, buf6, reinterpret_tensor(buf7, (4, 400), (400, 1), 0), buf9, buf11, primals_10, primals_8, primals_6) class NetNew(nn.Module): def __init__(self): super(NetNew, self).__init__() self.conv1 = nn.Conv2d(3, 12, 5) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(12, 16, 5) self.fc1 = nn.Linear(16 * 5 * 5, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10) def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_6 = self.fc1.weight primals_7 = self.fc1.bias primals_8 = self.fc2.weight primals_9 = self.fc2.bias primals_10 = self.fc3.weight primals_11 = self.fc3.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11]) return output[0]
tassotirap/data-science
Net
false
13,032
[ "Apache-2.0" ]
0
644bc351740cda90c0d8c907132d9da9630266c9
https://github.com/tassotirap/data-science/tree/644bc351740cda90c0d8c907132d9da9630266c9
MaxPoolStride1
import torch import torch.nn as nn import torch.utils.data import torch.utils.data.distributed import torch.nn.functional as F import torch._utils class MaxPoolStride1(nn.Module): def __init__(self, kernel_size): super(MaxPoolStride1, self).__init__() self.kernel_size = kernel_size self.pad = kernel_size - 1 def forward(self, x): padding = int(self.pad / 2) padded_x = F.pad(x, (padding, padding, padding, padding), mode= 'constant', value=0) pooled_x = nn.MaxPool2d(self.kernel_size, 1)(padded_x) return pooled_x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'kernel_size': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn import torch.utils.data import torch.utils.data.distributed import torch._utils assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_max_pool2d_with_indices_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 144 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 3 % 3 x0 = xindex % 3 x2 = xindex // 9 x4 = xindex tmp0 = -1 + x1 tmp1 = tl.full([1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = -1 + x0 tmp6 = tmp5 >= tmp1 tmp7 = tmp5 < tmp3 tmp8 = tmp2 & tmp4 tmp9 = tmp8 & tmp6 tmp10 = tmp9 & tmp7 tmp11 = tl.load(in_ptr0 + (-5 + x0 + 4 * x1 + 16 * x2), tmp10 & xmask, other=0.0) tmp12 = x0 tmp13 = tmp12 >= tmp1 tmp14 = tmp12 < tmp3 tmp15 = tmp8 & tmp13 tmp16 = tmp15 & tmp14 tmp17 = tl.load(in_ptr0 + (-4 + x0 + 4 * x1 + 16 * x2), tmp16 & xmask, other=0.0) tmp18 = triton_helpers.maximum(tmp17, tmp11) tmp19 = 1 + x0 tmp20 = tmp19 >= tmp1 tmp21 = tmp19 < tmp3 tmp22 = tmp8 & tmp20 tmp23 = tmp22 & tmp21 tmp24 = tl.load(in_ptr0 + (-3 + x0 + 4 * x1 + 16 * x2), tmp23 & xmask, other=0.0) tmp25 = triton_helpers.maximum(tmp24, tmp18) tmp26 = 2 + x0 tmp27 = tmp26 >= tmp1 tmp28 = tmp26 < tmp3 tmp29 = tmp8 & tmp27 tmp30 = tmp29 & tmp28 tmp31 = tl.load(in_ptr0 + (-2 + x0 + 4 * x1 + 16 * x2), tmp30 & xmask, other=0.0) tmp32 = triton_helpers.maximum(tmp31, tmp25) tmp33 = x1 tmp34 = tmp33 >= tmp1 tmp35 = tmp33 < tmp3 tmp36 = tmp34 & tmp35 tmp37 = tmp36 & tmp6 tmp38 = tmp37 & tmp7 tmp39 = tl.load(in_ptr0 + (-1 + x0 + 4 * x1 + 16 * x2), tmp38 & xmask, other=0.0) tmp40 = triton_helpers.maximum(tmp39, tmp32) tmp41 = tmp36 & tmp13 tmp42 = tmp41 & tmp14 tmp43 = tl.load(in_ptr0 + (x0 + 4 * x1 + 16 * x2), tmp42 & xmask, other=0.0 ) tmp44 = triton_helpers.maximum(tmp43, tmp40) tmp45 = tmp36 & tmp20 tmp46 = tmp45 & tmp21 tmp47 = tl.load(in_ptr0 + (1 + x0 + 4 * x1 + 16 * x2), tmp46 & xmask, other=0.0) tmp48 = triton_helpers.maximum(tmp47, tmp44) tmp49 = tmp36 & tmp27 tmp50 = tmp49 & tmp28 tmp51 = tl.load(in_ptr0 + (2 + x0 + 4 * x1 + 16 * x2), tmp50 & xmask, other=0.0) tmp52 = triton_helpers.maximum(tmp51, tmp48) tmp53 = 1 + x1 tmp54 = tmp53 >= tmp1 tmp55 = tmp53 < tmp3 tmp56 = tmp54 & tmp55 tmp57 = tmp56 & tmp6 tmp58 = tmp57 & tmp7 tmp59 = tl.load(in_ptr0 + (3 + x0 + 4 * x1 + 16 * x2), tmp58 & xmask, other=0.0) tmp60 = triton_helpers.maximum(tmp59, tmp52) tmp61 = tmp56 & tmp13 tmp62 = tmp61 & tmp14 tmp63 = tl.load(in_ptr0 + (4 + x0 + 4 * x1 + 16 * x2), tmp62 & xmask, other=0.0) tmp64 = triton_helpers.maximum(tmp63, tmp60) tmp65 = tmp56 & tmp20 tmp66 = tmp65 & tmp21 tmp67 = tl.load(in_ptr0 + (5 + x0 + 4 * x1 + 16 * x2), tmp66 & xmask, other=0.0) tmp68 = triton_helpers.maximum(tmp67, tmp64) tmp69 = tmp56 & tmp27 tmp70 = tmp69 & tmp28 tmp71 = tl.load(in_ptr0 + (6 + x0 + 4 * x1 + 16 * x2), tmp70 & xmask, other=0.0) tmp72 = triton_helpers.maximum(tmp71, tmp68) tmp73 = 2 + x1 tmp74 = tmp73 >= tmp1 tmp75 = tmp73 < tmp3 tmp76 = tmp74 & tmp75 tmp77 = tmp76 & tmp6 tmp78 = tmp77 & tmp7 tmp79 = tl.load(in_ptr0 + (7 + x0 + 4 * x1 + 16 * x2), tmp78 & xmask, other=0.0) tmp80 = triton_helpers.maximum(tmp79, tmp72) tmp81 = tmp76 & tmp13 tmp82 = tmp81 & tmp14 tmp83 = tl.load(in_ptr0 + (8 + x0 + 4 * x1 + 16 * x2), tmp82 & xmask, other=0.0) tmp84 = triton_helpers.maximum(tmp83, tmp80) tmp85 = tmp76 & tmp20 tmp86 = tmp85 & tmp21 tmp87 = tl.load(in_ptr0 + (9 + x0 + 4 * x1 + 16 * x2), tmp86 & xmask, other=0.0) tmp88 = triton_helpers.maximum(tmp87, tmp84) tmp89 = tmp76 & tmp27 tmp90 = tmp89 & tmp28 tmp91 = tl.load(in_ptr0 + (10 + x0 + 4 * x1 + 16 * x2), tmp90 & xmask, other=0.0) tmp92 = triton_helpers.maximum(tmp91, tmp88) tl.store(out_ptr0 + x4, tmp92, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 3, 3), (36, 9, 3, 1), torch.float32) get_raw_stream(0) triton_poi_fused_max_pool2d_with_indices_0[grid(144)](arg0_1, buf0, 144, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf0, class MaxPoolStride1New(nn.Module): def __init__(self, kernel_size): super(MaxPoolStride1New, self).__init__() self.kernel_size = kernel_size self.pad = kernel_size - 1 def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
tiahflorens/AlphaPose
MaxPoolStride1
false
13,033
[ "Apache-2.0" ]
0
84b844eff543eaa619d994ea0b15cb6caf69950d
https://github.com/tiahflorens/AlphaPose/tree/84b844eff543eaa619d994ea0b15cb6caf69950d
ConcatBlock
import torch import torch.nn as nn import torch.nn.functional class ConcatBlock(nn.Module): def __init__(self, in_channels, out_channels): super(ConcatBlock, self).__init__() self.in_chns = in_channels self.out_chns = out_channels self.conv1 = nn.Conv2d(self.in_chns, self.in_chns, kernel_size=1, padding=0) self.conv2 = nn.Conv2d(self.in_chns, self.out_chns, kernel_size=1, padding=0) self.ac1 = nn.LeakyReLU() self.ac2 = nn.LeakyReLU() def forward(self, x): x = self.conv1(x) x = self.ac1(x) x = self.conv2(x) x = self.ac2(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn import torch.nn.functional assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_convolution_leaky_relu_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x3, tmp4, xmask) tl.store(out_ptr1 + x3, tmp7, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = 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,)) 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 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_convolution_leaky_relu_0[grid(256)](buf0, primals_2, buf1, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf3 = extern_kernels.convolution(buf2, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf3, (4, 4, 4, 4), (64, 16, 4, 1)) buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) buf5 = buf0 del buf0 triton_poi_fused_convolution_leaky_relu_0[grid(256)](buf3, primals_5, buf4, buf5, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf3 del primals_5 return buf5, primals_1, primals_3, primals_4, buf1, buf2, buf4 class ConcatBlockNew(nn.Module): def __init__(self, in_channels, out_channels): super(ConcatBlockNew, self).__init__() self.in_chns = in_channels self.out_chns = out_channels self.conv1 = nn.Conv2d(self.in_chns, self.in_chns, kernel_size=1, padding=0) self.conv2 = nn.Conv2d(self.in_chns, self.out_chns, kernel_size=1, padding=0) self.ac1 = nn.LeakyReLU() self.ac2 = nn.LeakyReLU() def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
timothytancy/SSL4MIS
ConcatBlock
false
13,034
[ "MIT" ]
0
7879ad3483223e31a2785f5112eac1d4fa36b66e
https://github.com/timothytancy/SSL4MIS/tree/7879ad3483223e31a2785f5112eac1d4fa36b66e
RingLoss
import torch import warnings import torch.nn as nn from torchvision.transforms import * class RingLoss(nn.Module): """Ring loss. Reference: Zheng et al. Ring loss: Convex Feature Normalization for Face Recognition. CVPR 2018. """ def __init__(self): super(RingLoss, self).__init__() warnings.warn('This method is deprecated') self.radius = nn.Parameter(torch.ones(1, dtype=torch.float)) def forward(self, x): loss = ((x.norm(p=2, dim=1) - self.radius) ** 2).mean() return loss def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import warnings import torch.nn as nn from torchvision.transforms 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_per_fused_linalg_vector_norm_mean_mul_pow_sub_0(in_out_ptr0, in_ptr0, in_ptr1, out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 64 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex % 16 r1 = rindex // 16 r2 = rindex tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None) tmp2 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None) tmp5 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None) tmp8 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None) tmp12 = tl.load(in_ptr1 + 0) tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK]) tmp1 = tmp0 * tmp0 tmp3 = tmp2 * tmp2 tmp4 = tmp1 + tmp3 tmp6 = tmp5 * tmp5 tmp7 = tmp4 + tmp6 tmp9 = tmp8 * tmp8 tmp10 = tmp7 + tmp9 tmp11 = libdevice.sqrt(tmp10) tmp14 = tmp11 - tmp13 tmp15 = 2.0 tmp16 = tmp14 * tmp15 tmp17 = tmp14 * tmp14 tmp18 = tl.broadcast_to(tmp17, [XBLOCK, RBLOCK]) tmp20 = tl.sum(tmp18, 1)[:, None] tmp21 = 64.0 tmp22 = tmp20 / tmp21 tl.store(out_ptr1 + tl.broadcast_to(r2, [XBLOCK, RBLOCK]), tmp16, None) tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp22, None) 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,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) buf1 = empty_strided_cuda((), (), torch.float32) buf3 = buf1 del buf1 get_raw_stream(0) triton_per_fused_linalg_vector_norm_mean_mul_pow_sub_0[grid(1)](buf3, primals_1, primals_2, buf2, 1, 64, XBLOCK=1, num_warps=2, num_stages=1) del primals_1 del primals_2 return buf3, buf2 class RingLossNew(nn.Module): """Ring loss. Reference: Zheng et al. Ring loss: Convex Feature Normalization for Face Recognition. CVPR 2018. """ def __init__(self): super(RingLossNew, self).__init__() warnings.warn('This method is deprecated') self.radius = nn.Parameter(torch.ones(1, dtype=torch.float)) def forward(self, input_0): primals_2 = self.radius primals_1 = input_0 output = call([primals_1, primals_2]) return output[0]
theodorhusefest/ABD-Net
RingLoss
false
13,035
[ "MIT" ]
0
4ad71205954726b88d081ca079c28378f74e3007
https://github.com/theodorhusefest/ABD-Net/tree/4ad71205954726b88d081ca079c28378f74e3007
OutPutBlock
import torch import torch.nn as nn import torch.nn.functional class OutPutBlock(nn.Module): def __init__(self, in_channels, out_channels): super(OutPutBlock, self).__init__() self.in_chns = in_channels self.out_chns = out_channels self.conv1 = nn.Conv2d(self.in_chns, self.in_chns // 2, kernel_size =1, padding=0) self.conv2 = nn.Conv2d(self.in_chns // 2, self.out_chns, kernel_size=1, padding=0) self.drop1 = nn.Dropout2d(0.3) self.drop2 = nn.Dropout2d(0.3) self.ac1 = nn.LeakyReLU() def forward(self, x): x = self.drop1(x) x = self.conv1(x) x = self.ac1(x) x = self.drop2(x) x = self.conv2(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn import torch.nn.functional assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_convolution_leaky_relu_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 2 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x3, tmp4, xmask) tl.store(out_ptr1 + x3, tmp7, xmask) @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) 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, (2, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_3, (2,), (1,)) assert_size_stride(primals_4, (4, 2, 1, 1), (2, 1, 1, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 2, 4, 4), (32, 16, 4, 1)) buf1 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.bool) buf2 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_convolution_leaky_relu_0[grid(128)](buf0, primals_3, buf1, buf2, 128, XBLOCK=128, num_warps=4, num_stages=1) del buf0 del primals_3 buf3 = extern_kernels.convolution(buf2, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf3, (4, 4, 4, 4), (64, 16, 4, 1)) buf4 = buf3 del buf3 triton_poi_fused_convolution_1[grid(256)](buf4, primals_5, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 return buf4, primals_1, primals_2, primals_4, buf1, buf2 class OutPutBlockNew(nn.Module): def __init__(self, in_channels, out_channels): super(OutPutBlockNew, self).__init__() self.in_chns = in_channels self.out_chns = out_channels self.conv1 = nn.Conv2d(self.in_chns, self.in_chns // 2, kernel_size =1, padding=0) self.conv2 = nn.Conv2d(self.in_chns // 2, self.out_chns, kernel_size=1, padding=0) self.drop1 = nn.Dropout2d(0.3) self.drop2 = nn.Dropout2d(0.3) self.ac1 = nn.LeakyReLU() 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]
timothytancy/SSL4MIS
OutPutBlock
false
13,036
[ "MIT" ]
0
7879ad3483223e31a2785f5112eac1d4fa36b66e
https://github.com/timothytancy/SSL4MIS/tree/7879ad3483223e31a2785f5112eac1d4fa36b66e
ConvolutionModule
import torch from torch import Tensor from torch import nn class Swish(torch.nn.Module): """Construct an Swish object.""" def forward(self, x: 'Tensor') ->Tensor: """Return Swich activation function.""" return x * torch.sigmoid(x) class ConvolutionModule(nn.Module): """ConvolutionModule in Conformer model. Modified from https://github.com/espnet/espnet/blob/master/espnet/nets/pytorch_backend/conformer/convolution.py Args: channels (int): The number of channels of conv layers. kernel_size (int): Kernerl size of conv layers. bias (bool): Whether to use bias in conv layers (default=True). """ def __init__(self, channels: 'int', kernel_size: 'int', bias: 'bool'=True ) ->None: """Construct an ConvolutionModule object.""" super(ConvolutionModule, self).__init__() assert (kernel_size - 1) % 2 == 0 self.pointwise_conv1 = nn.Conv1d(channels, 2 * channels, kernel_size=1, stride=1, padding=0, bias=bias) self.depthwise_conv = nn.Conv1d(channels, channels, kernel_size, stride=1, padding=(kernel_size - 1) // 2, groups=channels, bias =bias) self.norm = nn.LayerNorm(channels) self.pointwise_conv2 = nn.Conv1d(channels, channels, kernel_size=1, stride=1, padding=0, bias=bias) self.activation = Swish() def forward(self, x: 'Tensor') ->Tensor: """Compute convolution module. Args: x: Input tensor (#time, batch, channels). Returns: Tensor: Output tensor (#time, batch, channels). """ x = x.permute(1, 2, 0) x = self.pointwise_conv1(x) x = nn.functional.glu(x, dim=1) x = self.depthwise_conv(x) x = x.permute(0, 2, 1) x = self.norm(x) x = x.permute(0, 2, 1) x = self.activation(x) x = self.pointwise_conv2(x) return x.permute(2, 0, 1) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'channels': 4, 'kernel_size': 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.triton_helpers import libdevice from torch import Tensor from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_0(in_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_1(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_glu_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 % 16 x1 = xindex // 16 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 32 * x1), xmask) tmp1 = tl.load(in_ptr0 + (16 + x0 + 32 * x1), xmask) tmp2 = tl.sigmoid(tmp1) tmp3 = tmp0 * tmp2 tl.store(out_ptr0 + x2, tmp3, xmask) @triton.jit def triton_poi_fused_convolution_3(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 4 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) @triton.jit def triton_poi_fused_native_layer_norm_4(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 % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask) tmp1 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask) tmp3 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask) tmp5 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 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 + x2, tmp8, xmask) tl.store(out_ptr1 + x2, tmp23, xmask) @triton.jit def triton_poi_fused_convolution_mul_native_layer_norm_sigmoid_5(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr1, out_ptr2, 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 y1 = yindex // 4 y0 = yindex % 4 tmp0 = tl.load(in_ptr0 + (x2 + 4 * y3), xmask & ymask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr1 + (x2 + 4 * y1), xmask & ymask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr2 + (x2 + 4 * y1), xmask & ymask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr3 + y0, ymask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tmp9 = tl.sigmoid(tmp8) tmp10 = tmp8 * tmp9 tl.store(out_ptr1 + (y0 + 4 * x2 + 16 * y1), tmp10, xmask & ymask) tl.store(out_ptr2 + (x2 + 4 * y3), tmp10, xmask & ymask) 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), (16, 4, 1)) assert_size_stride(primals_2, (8, 4, 1), (4, 1, 1)) assert_size_stride(primals_3, (8,), (1,)) assert_size_stride(primals_4, (4, 1, 1), (1, 1, 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, 1), (4, 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), (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, 8, 4), (32, 4, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_1[grid(128)](buf2, primals_3, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_3 buf3 = buf0 del buf0 triton_poi_fused_glu_2[grid(64)](buf2, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) buf4 = extern_kernels.convolution(buf3, primals_4, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=4, bias=None) assert_size_stride(buf4, (4, 4, 4), (16, 4, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_3[grid(64)](buf5, primals_5, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_5 buf6 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf7 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) triton_poi_fused_native_layer_norm_4[grid(16)](buf5, buf6, buf7, 16, XBLOCK=16, num_warps=1, num_stages=1) buf9 = empty_strided_cuda((4, 4, 4), (16, 1, 4), torch.float32) buf10 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_convolution_mul_native_layer_norm_sigmoid_5[grid( 16, 4)](buf5, buf6, buf7, primals_6, primals_7, buf9, buf10, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del buf6 del buf7 buf11 = extern_kernels.convolution(buf10, primals_8, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf11, (4, 4, 4), (16, 4, 1)) del buf10 buf12 = buf11 del buf11 triton_poi_fused_convolution_3[grid(64)](buf12, primals_9, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_9 return (reinterpret_tensor(buf12, (4, 4, 4), (1, 16, 4), 0), primals_2, primals_4, primals_6, primals_7, primals_8, reinterpret_tensor( primals_1, (4, 4, 4), (4, 1, 16), 0), buf2, buf3, buf5, buf9) class Swish(torch.nn.Module): """Construct an Swish object.""" def forward(self, x: 'Tensor') ->Tensor: """Return Swich activation function.""" return x * torch.sigmoid(x) class ConvolutionModuleNew(nn.Module): """ConvolutionModule in Conformer model. Modified from https://github.com/espnet/espnet/blob/master/espnet/nets/pytorch_backend/conformer/convolution.py Args: channels (int): The number of channels of conv layers. kernel_size (int): Kernerl size of conv layers. bias (bool): Whether to use bias in conv layers (default=True). """ def __init__(self, channels: 'int', kernel_size: 'int', bias: 'bool'=True ) ->None: """Construct an ConvolutionModule object.""" super(ConvolutionModuleNew, self).__init__() assert (kernel_size - 1) % 2 == 0 self.pointwise_conv1 = nn.Conv1d(channels, 2 * channels, kernel_size=1, stride=1, padding=0, bias=bias) self.depthwise_conv = nn.Conv1d(channels, channels, kernel_size, stride=1, padding=(kernel_size - 1) // 2, groups=channels, bias =bias) self.norm = nn.LayerNorm(channels) self.pointwise_conv2 = nn.Conv1d(channels, channels, kernel_size=1, stride=1, padding=0, bias=bias) self.activation = Swish() def forward(self, input_0): primals_2 = self.pointwise_conv1.weight primals_3 = self.pointwise_conv1.bias primals_4 = self.depthwise_conv.weight primals_5 = self.depthwise_conv.bias primals_6 = self.norm.weight primals_7 = self.norm.bias primals_8 = self.pointwise_conv2.weight primals_9 = self.pointwise_conv2.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9]) return output[0]
thangdepzai/icefall
ConvolutionModule
false
13,037
[ "Apache-2.0" ]
0
8c7995d493c4309c3d09bdabfa1ab12b4eec2657
https://github.com/thangdepzai/icefall/tree/8c7995d493c4309c3d09bdabfa1ab12b4eec2657
PAMA
import torch import torch.nn as nn def calc_mean_std(feat, eps=1e-05): size = feat.size() assert len(size) == 4 N, C = size[:2] feat_var = feat.view(N, C, -1).var(dim=2) + eps feat_std = feat_var.sqrt().view(N, C, 1, 1) feat_mean = feat.view(N, C, -1).mean(dim=2).view(N, C, 1, 1) return feat_mean, feat_std def mean_variance_norm(feat): size = feat.size() mean, std = calc_mean_std(feat) normalized_feat = (feat - mean.expand(size)) / std.expand(size) return normalized_feat class AttentionUnit(nn.Module): def __init__(self, channels): super(AttentionUnit, self).__init__() self.relu6 = nn.ReLU6() self.f = nn.Conv2d(channels, channels // 2, (1, 1)) self.g = nn.Conv2d(channels, channels // 2, (1, 1)) self.h = nn.Conv2d(channels, channels // 2, (1, 1)) self.out_conv = nn.Conv2d(channels // 2, channels, (1, 1)) self.softmax = nn.Softmax(dim=-1) def forward(self, Fc, Fs): B, C, H, W = Fc.shape f_Fc = self.relu6(self.f(mean_variance_norm(Fc))) g_Fs = self.relu6(self.g(mean_variance_norm(Fs))) h_Fs = self.relu6(self.h(Fs)) f_Fc = f_Fc.view(f_Fc.shape[0], f_Fc.shape[1], -1).permute(0, 2, 1) g_Fs = g_Fs.view(g_Fs.shape[0], g_Fs.shape[1], -1) Attention = self.softmax(torch.bmm(f_Fc, g_Fs)) h_Fs = h_Fs.view(h_Fs.shape[0], h_Fs.shape[1], -1) Fcs = torch.bmm(h_Fs, Attention.permute(0, 2, 1)) Fcs = Fcs.view(B, C // 2, H, W) Fcs = self.relu6(self.out_conv(Fcs)) return Fcs class FuseUnit(nn.Module): def __init__(self, channels): super(FuseUnit, self).__init__() self.proj1 = nn.Conv2d(2 * channels, channels, (1, 1)) self.proj2 = nn.Conv2d(channels, channels, (1, 1)) self.proj3 = nn.Conv2d(channels, channels, (1, 1)) self.fuse1x = nn.Conv2d(channels, 1, (1, 1), stride=1) self.fuse3x = nn.Conv2d(channels, 1, (3, 3), stride=1) self.fuse5x = nn.Conv2d(channels, 1, (5, 5), stride=1) self.pad3x = nn.ReflectionPad2d((1, 1, 1, 1)) self.pad5x = nn.ReflectionPad2d((2, 2, 2, 2)) self.sigmoid = nn.Sigmoid() def forward(self, F1, F2): Fcat = self.proj1(torch.cat((F1, F2), dim=1)) F1 = self.proj2(F1) F2 = self.proj3(F2) fusion1 = self.sigmoid(self.fuse1x(Fcat)) fusion3 = self.sigmoid(self.fuse3x(self.pad3x(Fcat))) fusion5 = self.sigmoid(self.fuse5x(self.pad5x(Fcat))) fusion = (fusion1 + fusion3 + fusion5) / 3 return torch.clamp(fusion, min=0, max=1.0) * F1 + torch.clamp(1 - fusion, min=0, max=1.0) * F2 class PAMA(nn.Module): def __init__(self, channels): super(PAMA, self).__init__() self.conv_in = nn.Conv2d(channels, channels, (3, 3), stride=1) self.attn = AttentionUnit(channels) self.fuse = FuseUnit(channels) self.conv_out = nn.Conv2d(channels, channels, (3, 3), stride=1) self.pad = nn.ReflectionPad2d((1, 1, 1, 1)) self.relu6 = nn.ReLU6() def forward(self, Fc, Fs): Fc = self.relu6(self.conv_in(self.pad(Fc))) Fs = self.relu6(self.conv_in(self.pad(Fs))) Fcs = self.attn(Fc, Fs) Fcs = self.relu6(self.conv_out(self.pad(Fcs))) Fcs = self.fuse(Fc, Fcs) return Fcs def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime 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_reflection_pad2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 576 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 6 x1 = xindex // 6 % 6 x2 = xindex // 36 x3 = xindex tmp0 = tl.load(in_ptr0 + (15 + -1 * tl_math.abs(-3 + tl_math.abs(-1 + x0)) + -4 * tl_math.abs(-3 + tl_math.abs(-1 + x1)) + 16 * x2), xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_per_fused_add_convolution_div_hardtanh_hardtanh_backward_mean_sqrt_sub_var_1( in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr6, out_ptr7, 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_ptr0 + (r2 + 16 * x3), xmask, other=0.0) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr2 + (r2 + 16 * x3), xmask, other=0.0) tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = 6.0 tmp6 = triton_helpers.minimum(tmp4, tmp5) tmp7 = tmp2 <= tmp3 tmp8 = tmp2 >= tmp5 tmp9 = tmp7 | tmp8 tmp11 = tmp10 + tmp1 tmp12 = triton_helpers.maximum(tmp11, tmp3) tmp13 = triton_helpers.minimum(tmp12, tmp5) tmp14 = tmp11 <= tmp3 tmp15 = tmp11 >= tmp5 tmp16 = tmp14 | tmp15 tmp17 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK]) tmp19 = tl.where(xmask, tmp17, 0) tmp20 = tl.broadcast_to(tmp17, [XBLOCK, RBLOCK]) tmp22 = tl.where(xmask, tmp20, 0) tmp23 = tl.sum(tmp22, 1)[:, None] tmp24 = tl.full([XBLOCK, 1], 16, tl.int32) tmp25 = tmp24.to(tl.float32) tmp26 = tmp23 / tmp25 tmp27 = tmp17 - tmp26 tmp28 = tmp27 * tmp27 tmp29 = tl.broadcast_to(tmp28, [XBLOCK, RBLOCK]) tmp31 = tl.where(xmask, tmp29, 0) tmp32 = tl.sum(tmp31, 1)[:, None] tmp34 = tl.sum(tmp19, 1)[:, None] tmp35 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK]) tmp37 = tl.where(xmask, tmp35, 0) tmp38 = tl.broadcast_to(tmp35, [XBLOCK, RBLOCK]) tmp40 = tl.where(xmask, tmp38, 0) tmp41 = tl.sum(tmp40, 1)[:, None] tmp42 = tmp41 / tmp25 tmp43 = tmp35 - tmp42 tmp44 = tmp43 * tmp43 tmp45 = tl.broadcast_to(tmp44, [XBLOCK, RBLOCK]) tmp47 = tl.where(xmask, tmp45, 0) tmp48 = tl.sum(tmp47, 1)[:, None] tmp50 = tl.sum(tmp37, 1)[:, None] tmp51 = 15.0 tmp52 = tmp32 / tmp51 tmp53 = 1e-05 tmp54 = tmp52 + tmp53 tmp55 = libdevice.sqrt(tmp54) tmp56 = tmp48 / tmp51 tmp57 = tmp56 + tmp53 tmp58 = libdevice.sqrt(tmp57) tmp59 = 16.0 tmp60 = tmp50 / tmp59 tmp61 = tmp6 - tmp60 tmp62 = tmp61 / tmp58 tmp63 = tmp34 / tmp59 tmp64 = tmp13 - tmp63 tmp65 = tmp64 / tmp55 tl.store(out_ptr0 + (r2 + 16 * x3), tmp6, xmask) tl.store(out_ptr1 + (r2 + 16 * x3), tmp9, xmask) tl.store(out_ptr2 + (r2 + 16 * x3), tmp13, xmask) tl.store(out_ptr3 + (r2 + 16 * x3), tmp16, xmask) tl.debug_barrier() tl.store(in_out_ptr0 + x3, tmp55, xmask) tl.debug_barrier() tl.store(in_out_ptr1 + x3, tmp58, xmask) tl.store(out_ptr6 + (r2 + 16 * x3), tmp62, xmask) tl.store(out_ptr7 + (r2 + 16 * x3), tmp65, xmask) @triton.jit def triton_poi_fused_convolution_hardtanh_hardtanh_backward_2(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 2 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = 6.0 tmp6 = triton_helpers.minimum(tmp4, tmp5) tmp7 = tmp2 <= tmp3 tmp8 = tmp2 >= tmp5 tmp9 = tmp7 | tmp8 tl.store(out_ptr0 + x3, tmp6, xmask) tl.store(out_ptr1 + x3, tmp9, xmask) @triton.jit def triton_per_fused__softmax_3(in_ptr0, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 64 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, float('-inf')) tmp4 = triton_helpers.max2(tmp3, 1)[:, None] tmp5 = tmp0 - tmp4 tmp6 = tl_math.exp(tmp5) tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK]) tmp9 = tl.where(xmask, tmp7, 0) tmp10 = tl.sum(tmp9, 1)[:, None] tmp11 = tmp6 / tmp10 tl.store(out_ptr2 + (r1 + 16 * x0), tmp11, xmask) @triton.jit def triton_poi_fused_convolution_hardtanh_reflection_pad2d_4(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 576 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 6 x1 = xindex // 6 % 6 x4 = xindex // 36 x2 = xindex // 36 % 4 x5 = xindex tmp0 = tl.load(in_ptr0 + (15 + -1 * tl_math.abs(-3 + tl_math.abs(-1 + x0)) + -4 * tl_math.abs(-3 + tl_math.abs(-1 + x1)) + 16 * x4), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = 6.0 tmp6 = triton_helpers.minimum(tmp4, tmp5) tl.store(out_ptr0 + x5, tmp6, xmask) @triton.jit def triton_poi_fused_convolution_hardtanh_hardtanh_backward_5(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = 6.0 tmp6 = triton_helpers.minimum(tmp4, tmp5) tmp7 = tmp2 <= tmp3 tmp8 = tmp2 >= tmp5 tmp9 = tmp7 | tmp8 tl.store(out_ptr0 + x3, tmp6, xmask) tl.store(out_ptr1 + x3, tmp9, xmask) @triton.jit def triton_poi_fused_cat_6(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 16 % 8 x0 = xindex % 16 x2 = xindex // 128 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 16 * x1 + 64 * x2), tmp4 & xmask, other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 8, tl.int64) tmp9 = tl.load(in_ptr1 + (x0 + 16 * (-4 + x1) + 64 * x2), tmp6 & xmask, other=0.0) tmp10 = tl.where(tmp4, tmp5, tmp9) tl.store(out_ptr0 + x3, tmp10, xmask) @triton.jit def triton_poi_fused_convolution_7(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_convolution_8(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp3 = tmp0 + tmp2 tl.store(in_out_ptr0 + x0, tmp3, xmask) @triton.jit def triton_poi_fused_reflection_pad2d_9(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 8 x1 = xindex // 8 % 8 x2 = xindex // 64 x3 = xindex tmp0 = tl.load(in_ptr0 + (15 + -1 * tl_math.abs(-3 + tl_math.abs(-2 + x0)) + -4 * tl_math.abs(-3 + tl_math.abs(-2 + x1)) + 16 * x2), xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_poi_fused_add_clamp_convolution_div_mul_rsub_sigmoid_10(in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_out_ptr1 + x3, xmask) tmp4 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr2 + (x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp8 = tl.load(in_ptr3 + (x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp11 = tl.load(in_ptr4 + (x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp7 = tl.sigmoid(tmp6) tmp9 = tl.sigmoid(tmp8) tmp10 = tmp7 + tmp9 tmp12 = tl.sigmoid(tmp11) tmp13 = tmp10 + tmp12 tmp14 = 0.3333333333333333 tmp15 = tmp13 * tmp14 tmp16 = 0.0 tmp17 = triton_helpers.maximum(tmp15, tmp16) tmp18 = 1.0 tmp19 = triton_helpers.minimum(tmp17, tmp18) tmp20 = tmp19 * tmp2 tmp21 = tmp18 - tmp15 tmp22 = triton_helpers.maximum(tmp21, tmp16) tmp23 = triton_helpers.minimum(tmp22, tmp18) tmp24 = tmp23 * tmp5 tmp25 = tmp20 + tmp24 tl.store(in_out_ptr0 + x3, tmp2, xmask) tl.store(in_out_ptr1 + x3, tmp5, xmask) tl.store(out_ptr0 + x3, tmp25, xmask) @triton.jit def triton_poi_fused_convolution_hardtanh_backward_11(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 <= tmp3 tmp5 = 6.0 tmp6 = tmp2 >= tmp5 tmp7 = tmp4 | tmp6 tl.store(out_ptr0 + x3, tmp7, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, 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) = 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, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_5, (2, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_6, (2,), (1,)) assert_size_stride(primals_7, (2, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_8, (2,), (1,)) assert_size_stride(primals_9, (2, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_10, (2,), (1,)) assert_size_stride(primals_11, (4, 2, 1, 1), (2, 1, 1, 1)) assert_size_stride(primals_12, (4,), (1,)) assert_size_stride(primals_13, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_14, (4,), (1,)) assert_size_stride(primals_15, (4, 8, 1, 1), (8, 1, 1, 1)) assert_size_stride(primals_16, (4,), (1,)) assert_size_stride(primals_17, (4, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_18, (4,), (1,)) assert_size_stride(primals_19, (4, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_20, (4,), (1,)) assert_size_stride(primals_21, (1, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_22, (1,), (1,)) assert_size_stride(primals_23, (1, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_24, (1,), (1,)) assert_size_stride(primals_25, (1, 4, 5, 5), (100, 25, 5, 1)) assert_size_stride(primals_26, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 6, 6), (144, 36, 6, 1), torch.float32) get_raw_stream(0) triton_poi_fused_reflection_pad2d_0[grid(576)](primals_1, buf0, 576, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 4, 4), (64, 16, 4, 1)) buf3 = empty_strided_cuda((4, 4, 6, 6), (144, 36, 6, 1), torch.float32) triton_poi_fused_reflection_pad2d_0[grid(576)](primals_4, buf3, 576, XBLOCK=256, num_warps=4, num_stages=1) del primals_4 buf4 = extern_kernels.convolution(buf3, primals_2, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 4, 4, 4), (64, 16, 4, 1)) buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf54 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf55 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf14 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf9 = buf7 del buf7 buf16 = buf14 del buf14 buf18 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf11 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_per_fused_add_convolution_div_hardtanh_hardtanh_backward_mean_sqrt_sub_var_1[ grid(16)](buf9, buf16, buf4, primals_3, buf1, buf5, buf54, buf2, buf55, buf18, buf11, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) del buf1 del primals_3 buf12 = extern_kernels.convolution(buf11, primals_5, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf12, (4, 2, 4, 4), (32, 16, 4, 1)) buf19 = extern_kernels.convolution(buf18, primals_7, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf19, (4, 2, 4, 4), (32, 16, 4, 1)) buf20 = extern_kernels.convolution(buf5, primals_9, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf20, (4, 2, 4, 4), (32, 16, 4, 1)) buf21 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.float32) buf53 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.bool) triton_poi_fused_convolution_hardtanh_hardtanh_backward_2[grid(128)]( buf12, primals_6, buf21, buf53, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_6 buf22 = buf12 del buf12 buf52 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.bool) triton_poi_fused_convolution_hardtanh_hardtanh_backward_2[grid(128)]( buf19, primals_8, buf22, buf52, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_8 buf23 = empty_strided_cuda((4, 16, 16), (256, 16, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf21, (4, 16, 2), (32, 1, 16 ), 0), reinterpret_tensor(buf22, (4, 2, 16), (32, 16, 1), 0), out=buf23) buf26 = empty_strided_cuda((4, 16, 16), (256, 16, 1), torch.float32) triton_per_fused__softmax_3[grid(64)](buf23, buf26, 64, 16, XBLOCK= 1, num_warps=2, num_stages=1) buf27 = buf19 del buf19 buf51 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.bool) triton_poi_fused_convolution_hardtanh_hardtanh_backward_2[grid(128)]( buf20, primals_10, buf27, buf51, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_10 buf28 = reinterpret_tensor(buf20, (4, 2, 16), (32, 16, 1), 0) del buf20 extern_kernels.bmm(reinterpret_tensor(buf27, (4, 2, 16), (32, 16, 1 ), 0), reinterpret_tensor(buf26, (4, 16, 16), (256, 1, 16), 0), out=buf28) buf29 = extern_kernels.convolution(reinterpret_tensor(buf28, (4, 2, 4, 4), (32, 16, 4, 1), 0), primals_11, 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 = empty_strided_cuda((4, 4, 6, 6), (144, 36, 6, 1), torch.float32 ) triton_poi_fused_convolution_hardtanh_reflection_pad2d_4[grid(576)]( buf29, primals_12, buf30, 576, XBLOCK=128, num_warps=4, num_stages=1) buf31 = extern_kernels.convolution(buf30, primals_13, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf31, (4, 4, 4, 4), (64, 16, 4, 1)) buf32 = buf4 del buf4 buf49 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_convolution_hardtanh_hardtanh_backward_5[grid(256)]( buf31, primals_14, buf32, buf49, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_14 buf33 = empty_strided_cuda((4, 8, 4, 4), (128, 16, 4, 1), torch.float32 ) triton_poi_fused_cat_6[grid(512)](buf2, buf32, buf33, 512, XBLOCK= 256, num_warps=4, num_stages=1) buf34 = extern_kernels.convolution(buf33, primals_15, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf34, (4, 4, 4, 4), (64, 16, 4, 1)) buf35 = buf34 del buf34 triton_poi_fused_convolution_7[grid(256)](buf35, primals_16, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_16 buf36 = extern_kernels.convolution(buf2, primals_17, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf36, (4, 4, 4, 4), (64, 16, 4, 1)) buf38 = extern_kernels.convolution(buf32, primals_19, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf38, (4, 4, 4, 4), (64, 16, 4, 1)) buf40 = extern_kernels.convolution(buf35, primals_21, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf40, (4, 1, 4, 4), (16, 16, 4, 1)) buf41 = buf40 del buf40 triton_poi_fused_convolution_8[grid(64)](buf41, primals_22, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_22 buf42 = empty_strided_cuda((4, 4, 6, 6), (144, 36, 6, 1), torch.float32 ) triton_poi_fused_reflection_pad2d_0[grid(576)](buf35, buf42, 576, XBLOCK=256, num_warps=4, num_stages=1) buf43 = extern_kernels.convolution(buf42, primals_23, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf43, (4, 1, 4, 4), (16, 16, 4, 1)) buf44 = buf43 del buf43 triton_poi_fused_convolution_8[grid(64)](buf44, primals_24, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_24 buf45 = reinterpret_tensor(buf23, (4, 4, 8, 8), (256, 64, 8, 1), 0) del buf23 triton_poi_fused_reflection_pad2d_9[grid(1024)](buf35, buf45, 1024, XBLOCK=128, num_warps=4, num_stages=1) buf46 = extern_kernels.convolution(buf45, primals_25, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf46, (4, 1, 4, 4), (16, 16, 4, 1)) buf47 = buf46 del buf46 triton_poi_fused_convolution_8[grid(64)](buf47, primals_26, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_26 buf37 = buf36 del buf36 buf39 = buf38 del buf38 buf48 = buf31 del buf31 triton_poi_fused_add_clamp_convolution_div_mul_rsub_sigmoid_10[grid (256)](buf37, buf39, primals_18, primals_20, buf41, buf44, buf47, buf48, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_18 del primals_20 buf50 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_convolution_hardtanh_backward_11[grid(256)](buf29, primals_12, buf50, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf29 del primals_12 return (buf48, primals_2, primals_5, primals_7, primals_9, primals_11, primals_13, primals_15, primals_17, primals_19, primals_21, primals_23, primals_25, buf0, buf2, buf3, buf5, buf9, buf11, buf16, buf18, buf26, reinterpret_tensor(buf28, (4, 2, 4, 4), (32, 16, 4, 1 ), 0), buf30, buf32, buf33, buf35, buf37, buf39, buf41, buf42, buf44, buf45, buf47, buf49, buf50, reinterpret_tensor(buf27, (4, 16, 2), (32, 1, 16), 0), reinterpret_tensor(buf21, (4, 2, 16), (32, 16, 1), 0), reinterpret_tensor(buf22, (4, 16, 2), (32, 1, 16), 0), buf51, buf52, buf53, buf54, buf55) def calc_mean_std(feat, eps=1e-05): size = feat.size() assert len(size) == 4 N, C = size[:2] feat_var = feat.view(N, C, -1).var(dim=2) + eps feat_std = feat_var.sqrt().view(N, C, 1, 1) feat_mean = feat.view(N, C, -1).mean(dim=2).view(N, C, 1, 1) return feat_mean, feat_std def mean_variance_norm(feat): size = feat.size() mean, std = calc_mean_std(feat) normalized_feat = (feat - mean.expand(size)) / std.expand(size) return normalized_feat class AttentionUnit(nn.Module): def __init__(self, channels): super(AttentionUnit, self).__init__() self.relu6 = nn.ReLU6() self.f = nn.Conv2d(channels, channels // 2, (1, 1)) self.g = nn.Conv2d(channels, channels // 2, (1, 1)) self.h = nn.Conv2d(channels, channels // 2, (1, 1)) self.out_conv = nn.Conv2d(channels // 2, channels, (1, 1)) self.softmax = nn.Softmax(dim=-1) def forward(self, Fc, Fs): B, C, H, W = Fc.shape f_Fc = self.relu6(self.f(mean_variance_norm(Fc))) g_Fs = self.relu6(self.g(mean_variance_norm(Fs))) h_Fs = self.relu6(self.h(Fs)) f_Fc = f_Fc.view(f_Fc.shape[0], f_Fc.shape[1], -1).permute(0, 2, 1) g_Fs = g_Fs.view(g_Fs.shape[0], g_Fs.shape[1], -1) Attention = self.softmax(torch.bmm(f_Fc, g_Fs)) h_Fs = h_Fs.view(h_Fs.shape[0], h_Fs.shape[1], -1) Fcs = torch.bmm(h_Fs, Attention.permute(0, 2, 1)) Fcs = Fcs.view(B, C // 2, H, W) Fcs = self.relu6(self.out_conv(Fcs)) return Fcs class FuseUnit(nn.Module): def __init__(self, channels): super(FuseUnit, self).__init__() self.proj1 = nn.Conv2d(2 * channels, channels, (1, 1)) self.proj2 = nn.Conv2d(channels, channels, (1, 1)) self.proj3 = nn.Conv2d(channels, channels, (1, 1)) self.fuse1x = nn.Conv2d(channels, 1, (1, 1), stride=1) self.fuse3x = nn.Conv2d(channels, 1, (3, 3), stride=1) self.fuse5x = nn.Conv2d(channels, 1, (5, 5), stride=1) self.pad3x = nn.ReflectionPad2d((1, 1, 1, 1)) self.pad5x = nn.ReflectionPad2d((2, 2, 2, 2)) self.sigmoid = nn.Sigmoid() def forward(self, F1, F2): Fcat = self.proj1(torch.cat((F1, F2), dim=1)) F1 = self.proj2(F1) F2 = self.proj3(F2) fusion1 = self.sigmoid(self.fuse1x(Fcat)) fusion3 = self.sigmoid(self.fuse3x(self.pad3x(Fcat))) fusion5 = self.sigmoid(self.fuse5x(self.pad5x(Fcat))) fusion = (fusion1 + fusion3 + fusion5) / 3 return torch.clamp(fusion, min=0, max=1.0) * F1 + torch.clamp(1 - fusion, min=0, max=1.0) * F2 class PAMANew(nn.Module): def __init__(self, channels): super(PAMANew, self).__init__() self.conv_in = nn.Conv2d(channels, channels, (3, 3), stride=1) self.attn = AttentionUnit(channels) self.fuse = FuseUnit(channels) self.conv_out = nn.Conv2d(channels, channels, (3, 3), stride=1) self.pad = nn.ReflectionPad2d((1, 1, 1, 1)) self.relu6 = nn.ReLU6() def forward(self, input_0, input_1): primals_2 = self.conv_in.weight primals_3 = self.conv_in.bias primals_5 = self.attn.f.weight primals_6 = self.attn.f.bias primals_7 = self.attn.g.weight primals_8 = self.attn.g.bias primals_9 = self.attn.h.weight primals_10 = self.attn.h.bias primals_11 = self.attn.out_conv.weight primals_12 = self.attn.out_conv.bias primals_15 = self.fuse.proj1.weight primals_14 = self.fuse.proj1.bias primals_17 = self.fuse.proj2.weight primals_16 = self.fuse.proj2.bias primals_19 = self.fuse.proj3.weight primals_18 = self.fuse.proj3.bias primals_21 = self.fuse.fuse1x.weight primals_22 = self.fuse.fuse1x.bias primals_23 = self.fuse.fuse3x.weight primals_24 = self.fuse.fuse3x.bias primals_25 = self.fuse.fuse5x.weight primals_26 = self.fuse.fuse5x.bias primals_13 = self.conv_out.weight primals_20 = self.conv_out.bias primals_1 = input_0 primals_4 = 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, primals_15, primals_16, primals_17, primals_18, primals_19, primals_20, primals_21, primals_22, primals_23, primals_24, primals_25, primals_26]) return output[0]
sugi-san/PAMA
PAMA
false
13,038
[ "MIT" ]
0
95141ebf0d3b61828a0e545f989f96b8ef569f34
https://github.com/sugi-san/PAMA/tree/95141ebf0d3b61828a0e545f989f96b8ef569f34
Actor
import torch import numpy as np import torch.nn.functional as F from torch import nn def hidden_init(layer): fan_in = layer.weight.data.size()[0] lim = 1.0 / np.sqrt(fan_in) return -lim, lim class Actor(nn.Module): """Actor (Policy) Model.""" def __init__(self, state_size, action_size, seed, fc_units=128, fc_units2=64): super(Actor, self).__init__() self.seed = torch.manual_seed(seed) self.fc1 = nn.Linear(state_size, fc_units) self.fc2 = nn.Linear(fc_units, fc_units2) self.fc3 = nn.Linear(fc_units2, action_size) self.reset_parameters() def reset_parameters(self): self.fc1.weight.data.uniform_(*hidden_init(self.fc1)) self.fc2.weight.data.uniform_(*hidden_init(self.fc2)) self.fc3.weight.data.uniform_(-0.003, 0.003) def forward(self, state): x = F.relu(self.fc1(state)) x = F.relu(self.fc2(x)) return F.tanh(self.fc3(x)) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'state_size': 4, 'action_size': 4, 'seed': 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 numpy as np from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 128 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) @triton.jit def triton_poi_fused_relu_threshold_backward_1(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) @triton.jit def triton_poi_fused_tanh_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 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, (128, 4), (4, 1)) assert_size_stride(primals_2, (128,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (64, 128), (128, 1)) assert_size_stride(primals_5, (64,), (1,)) assert_size_stride(primals_6, (4, 64), (64, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 128), (128, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 128), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 128), (2048, 512, 128, 1), 0) del buf0 buf7 = empty_strided_cuda((4, 4, 4, 128), (2048, 512, 128, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(8192)](buf1, primals_2, buf7, 8192, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 64), (64, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 128), (128, 1), 0), reinterpret_tensor(primals_4, (128, 64), (1, 128), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 64), (1024, 256, 64, 1), 0) del buf2 buf6 = empty_strided_cuda((4, 4, 4, 64), (1024, 256, 64, 1), torch.bool ) triton_poi_fused_relu_threshold_backward_1[grid(4096)](buf3, primals_5, buf6, 4096, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf3, (64, 64), (64, 1), 0), reinterpret_tensor(primals_6, (64, 4), (1, 64), 0), out=buf4) buf5 = reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf4 triton_poi_fused_tanh_2[grid(256)](buf5, primals_7, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_7 return buf5, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 128), (128, 1), 0 ), reinterpret_tensor(buf3, (64, 64), (64, 1), 0 ), buf5, primals_6, buf6, primals_4, buf7 def hidden_init(layer): fan_in = layer.weight.data.size()[0] lim = 1.0 / np.sqrt(fan_in) return -lim, lim class ActorNew(nn.Module): """Actor (Policy) Model.""" def __init__(self, state_size, action_size, seed, fc_units=128, fc_units2=64): super(ActorNew, self).__init__() self.seed = torch.manual_seed(seed) self.fc1 = nn.Linear(state_size, fc_units) self.fc2 = nn.Linear(fc_units, fc_units2) self.fc3 = nn.Linear(fc_units2, action_size) self.reset_parameters() def reset_parameters(self): self.fc1.weight.data.uniform_(*hidden_init(self.fc1)) self.fc2.weight.data.uniform_(*hidden_init(self.fc2)) self.fc3.weight.data.uniform_(-0.003, 0.003) def forward(self, input_0): primals_1 = self.fc1.weight primals_2 = self.fc1.bias primals_4 = self.fc2.weight primals_5 = self.fc2.bias primals_6 = self.fc3.weight primals_7 = self.fc3.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
tjkemp/tennis-example
Actor
false
13,039
[ "MIT" ]
0
3cb0c52a93c65f88872cf44e3782bf87d9d8cef3
https://github.com/tjkemp/tennis-example/tree/3cb0c52a93c65f88872cf44e3782bf87d9d8cef3
CausalAttentionSortNet
import torch from torch.nn import functional as F from torch import nn def bucket(buckets, t, dim=1): shape = list(t.shape) shape[dim:dim + 1] = [buckets, -1] return t.reshape(*shape) def differentiable_topk(x, k, temperature=1.0): *_, n, dim = x.shape topk_tensors = [] for i in range(k): is_last = i == k - 1 values, indices = (x / temperature).softmax(dim=-1).topk(1, dim=-1) topks = torch.zeros_like(x).scatter_(-1, indices, values) topk_tensors.append(topks) if not is_last: x.scatter_(-1, indices, float('-inf')) topks = torch.cat(topk_tensors, dim=-1) return topks.reshape(*_, k * n, dim) def max_neg_value(tensor): return -torch.finfo(tensor.dtype).max def cumavg(t, dim): r = torch.arange(1, t.shape[dim] + 1, device=t.device, dtype=t.dtype) expand_slice = [None] * len(t.shape) expand_slice[dim] = slice(None, None) return t.cumsum(dim=dim) / r[tuple(expand_slice)] def mask_reordering_matrix(R, topk, temperature): buckets = R.shape[1] mask_value = max_neg_value(R) mask = torch.zeros(R.shape, device=R.device).bool() i, j = torch.triu_indices(buckets, buckets) mask[:, i, j + topk] = True R.masked_fill_(mask, mask_value) return differentiable_topk(R, topk, temperature) class CausalAttentionSortNet(nn.Module): def __init__(self, heads, bucket_size, dim, temperature): super().__init__() self.heads = heads self.bucket_size = bucket_size self.dim = dim self.temperature = temperature def forward(self, q, k, topk=1): bh, *_, h, dim = *q.shape, self.heads, self.dim bh // h buckets = q.shape[1] // self.bucket_size kv_buckets = k.shape[1] // self.bucket_size q_r = bucket(buckets, cumavg(q, dim=1)) k_r = bucket(kv_buckets, cumavg(k, dim=1)) sq = q_r[:, :, 0] sk = k_r.sum(dim=2) sk = F.pad(sk, (0, 0, topk, 0)) R = torch.einsum('bie,bje->bij', sq, sk) * dim ** -0.5 return mask_reordering_matrix(R, topk, self.temperature) def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'heads': 4, 'bucket_size': 4, 'dim': 4, 'temperature': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_triu_indices_0(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 2 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = x0 tmp1 = tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tmp0.to(tl.float64) tmp6 = tl.full([1], 2.0, tl.float64) tmp7 = tmp5 * tmp6 tmp8 = tl.full([1], 2.25, tl.float64) tmp9 = tmp8 - tmp7 tmp10 = libdevice.sqrt(tmp9) tmp11 = tl.full([1], 1.5, tl.float64) tmp12 = tmp11 - tmp10 tmp13 = libdevice.floor(tmp12) tmp14 = tmp13.to(tl.int64) tmp15 = tmp14 + tmp1 tmp16 = tl.full(tmp15.shape, 0.0, tmp15.dtype) tmp17 = tl.where(tmp4, tmp15, tmp16) tmp18 = tmp0 >= tmp3 tl.full([1], 2, tl.int64) tmp21 = -1 + x0 tmp22 = tmp21.to(tl.float64) tmp23 = tmp22 * tmp6 tmp24 = tmp8 - tmp23 tmp25 = libdevice.sqrt(tmp24) tmp26 = tmp11 - tmp25 tmp27 = libdevice.floor(tmp26) tmp28 = tl.full([1], 1.0, tl.float64) tmp29 = tmp28 - tmp27 tmp30 = tmp29 * tmp27 tmp31 = tl.full([1], 0.5, tl.float64) tmp32 = tmp30 * tmp31 tmp33 = tmp22 - tmp32 tmp34 = libdevice.floor(tmp33) tmp35 = tmp34.to(tl.int64) tmp36 = tmp35 + tmp1 tmp37 = tl.full(tmp36.shape, 0.0, tmp36.dtype) tmp38 = tl.where(tmp18, tmp36, tmp37) tmp39 = tl.where(tmp4, tmp17, tmp38) tl.store(out_ptr0 + x0, tmp39, xmask) @triton.jit def triton_poi_fused__to_copy_1(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.full([1], False, tl.int1) tl.store(out_ptr0 + x0, tmp0, xmask) @triton.jit def triton_poi_fused__to_copy_index_put_lift_fresh_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK]) tmp7 = tl.load(in_ptr0 + 1) tmp8 = tl.broadcast_to(tmp7, [XBLOCK]) tmp2 = tl.full([XBLOCK], 1, tl.int32) tmp3 = tmp1 + tmp2 tmp4 = tmp1 < 0 tmp5 = tl.where(tmp4, tmp3, tmp1) tl.device_assert((0 <= tmp5) & (tmp5 < 1), 'index out of bounds: 0 <= tmp5 < 1') tmp9 = tl.full([1], 1, tl.int64) tmp10 = tmp8 + tmp9 tmp11 = tl.full([XBLOCK], 2, tl.int32) tmp12 = tmp10 + tmp11 tmp13 = tmp10 < 0 tmp14 = tl.where(tmp13, tmp12, tmp10) tl.device_assert((0 <= tmp14) & (tmp14 < 2), 'index out of bounds: 0 <= tmp14 < 2') tmp16 = tl.full([1], True, tl.int1) tl.store(out_ptr0 + (tmp14 + 2 * x0), tmp16, xmask) @triton.jit def _triton_helper_fn_add0(arg0_0, arg1_0): tmp0 = arg0_0 + arg1_0 return tmp0 @triton.jit def triton_per_fused_cumsum_3(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl .constexpr): xnumel = 16 RBLOCK: tl.constexpr = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r2 = rindex x0 = xindex % 4 x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + (x0 + 4 * r2 + 16 * x1), xmask, other=0.0) tmp1 = tmp0.to(tl.float32) tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp3, = tl.associative_scan((tmp2,), 1, _triton_helper_fn_add0) tl.store(out_ptr0 + (x0 + 4 * r2 + 16 * x1), tmp3, xmask) @triton.jit def triton_poi_fused_constant_pad_nd_sum_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 % 2 x0 = xindex % 4 x2 = xindex // 8 x3 = xindex tmp0 = -1 + x1 tmp1 = tl.full([1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.load(in_ptr0 + (x0 + 16 * x2), tmp2 & xmask, eviction_policy= 'evict_last', other=0.0) tmp4 = 1.0 tmp5 = tmp3 / tmp4 tmp6 = tl.load(in_ptr0 + (4 + x0 + 16 * x2), tmp2 & xmask, eviction_policy='evict_last', other=0.0) tmp7 = 2.0 tmp8 = tmp6 / tmp7 tmp9 = tmp5 + tmp8 tmp10 = tl.load(in_ptr0 + (8 + x0 + 16 * x2), tmp2 & xmask, eviction_policy='evict_last', other=0.0) tmp11 = 3.0 tmp12 = tmp10 / tmp11 tmp13 = tmp9 + tmp12 tmp14 = tl.load(in_ptr0 + (12 + x0 + 16 * x2), tmp2 & xmask, eviction_policy='evict_last', other=0.0) tmp15 = 4.0 tmp16 = tmp14 / tmp15 tmp17 = tmp13 + tmp16 tmp18 = tl.full(tmp17.shape, 0.0, tmp17.dtype) tmp19 = tl.where(tmp2, tmp17, tmp18) tl.store(out_ptr0 + x3, tmp19, xmask) @triton.jit def triton_poi_fused_div_5(in_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 = 1 + x1 tmp2 = tmp1.to(tl.float32) tmp3 = tmp0 / tmp2 tl.store(in_out_ptr0 + x3, tmp3, xmask) @triton.jit def triton_poi_fused__softmax_masked_fill_mul_6(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 x2 = xindex x1 = xindex // 2 tmp0 = tl.load(in_ptr0 + x2, xmask).to(tl.int1) tmp1 = tl.load(in_ptr1 + x2, xmask) tmp8 = tl.load(in_ptr0 + 2 * x1, xmask, eviction_policy='evict_last').to(tl .int1) tmp9 = tl.load(in_ptr1 + 2 * x1, xmask, eviction_policy='evict_last') tmp13 = tl.load(in_ptr0 + (1 + 2 * x1), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp14 = tl.load(in_ptr1 + (1 + 2 * x1), xmask, eviction_policy='evict_last' ) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = -3.4028234663852886e+38 tmp5 = tl.where(tmp0, tmp4, tmp3) tmp6 = 1.0 tmp7 = tmp5 * tmp6 tmp10 = tmp9 * tmp2 tmp11 = tl.where(tmp8, tmp4, tmp10) tmp12 = tmp11 * tmp6 tmp15 = tmp14 * tmp2 tmp16 = tl.where(tmp13, tmp4, tmp15) tmp17 = tmp16 * tmp6 tmp18 = triton_helpers.maximum(tmp12, tmp17) tmp19 = tmp7 - tmp18 tmp20 = 0.25 tmp21 = tmp19 * tmp20 tmp22 = tl_math.exp(tmp21) tl.store(out_ptr0 + x2, tmp22, xmask) @triton.jit def triton_poi_fused__softmax_7(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 8 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 2 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 2 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 2 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp4 = tmp0 / tmp3 tl.store(out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_scatter_zeros_like_8(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 = 0.0 tl.store(out_ptr0 + x0, tmp0, xmask) @triton.jit def triton_poi_fused_scatter_zeros_like_9(in_ptr0, in_ptr1, 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) tmp2 = tl.load(in_ptr1 + x0, xmask) tl.device_assert((0 <= tmp0) & (tmp0 < 2) | ~xmask, 'index out of bounds: 0 <= tmp0 < 2') tl.store(out_ptr0 + (tmp0 + 2 * x0), tmp2, 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), (16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((2,), (1,), torch.int64) get_raw_stream(0) triton_poi_fused_triu_indices_0[grid(2)](buf0, 2, XBLOCK=2, num_warps=1, num_stages=1) buf1 = empty_strided_cuda((4, 1, 2), (2, 2, 1), torch.bool) triton_poi_fused__to_copy_1[grid(8)](buf1, 8, XBLOCK=8, num_warps=1, num_stages=1) triton_poi_fused__to_copy_index_put_lift_fresh_2[grid(4)](buf0, buf1, 4, XBLOCK=4, num_warps=1, num_stages=1) del buf0 buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_per_fused_cumsum_3[grid(16)](arg0_1, buf3, 16, 4, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_per_fused_cumsum_3[grid(16)](arg1_1, buf4, 16, 4, XBLOCK=1, num_warps=2, num_stages=1) del arg1_1 buf5 = empty_strided_cuda((4, 2, 4), (8, 4, 1), torch.float32) triton_poi_fused_constant_pad_nd_sum_4[grid(32)](buf4, buf5, 32, XBLOCK=32, num_warps=1, num_stages=1) del buf4 buf6 = buf3 del buf3 triton_poi_fused_div_5[grid(64)](buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) buf7 = empty_strided_cuda((4, 1, 2), (2, 2, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf6, (4, 1, 4), (16, 0, 1), 0), reinterpret_tensor(buf5, (4, 4, 2), (8, 1, 4), 0), out=buf7) del buf5 del buf6 buf8 = empty_strided_cuda((4, 1, 2), (2, 8, 1), torch.float32) triton_poi_fused__softmax_masked_fill_mul_6[grid(8)](buf1, buf7, buf8, 8, XBLOCK=8, num_warps=1, num_stages=1) del buf1 buf9 = reinterpret_tensor(buf7, (4, 1, 2), (2, 8, 1), 0) del buf7 triton_poi_fused__softmax_7[grid(8)](buf8, buf9, 8, XBLOCK=8, num_warps=1, num_stages=1) del buf8 buf10 = torch.ops.aten.topk.default(buf9, 1) buf11 = buf10[0] buf12 = buf10[1] del buf10 buf13 = reinterpret_tensor(buf9, (4, 1, 2), (2, 2, 1), 0) del buf9 triton_poi_fused_scatter_zeros_like_8[grid(8)](buf13, 8, XBLOCK=8, num_warps=1, num_stages=1) triton_poi_fused_scatter_zeros_like_9[grid(4)](buf12, buf11, buf13, 4, XBLOCK=4, num_warps=1, num_stages=1) del buf11 del buf12 return buf13, def bucket(buckets, t, dim=1): shape = list(t.shape) shape[dim:dim + 1] = [buckets, -1] return t.reshape(*shape) def differentiable_topk(x, k, temperature=1.0): *_, n, dim = x.shape topk_tensors = [] for i in range(k): is_last = i == k - 1 values, indices = (x / temperature).softmax(dim=-1).topk(1, dim=-1) topks = torch.zeros_like(x).scatter_(-1, indices, values) topk_tensors.append(topks) if not is_last: x.scatter_(-1, indices, float('-inf')) topks = torch.cat(topk_tensors, dim=-1) return topks.reshape(*_, k * n, dim) def max_neg_value(tensor): return -torch.finfo(tensor.dtype).max def cumavg(t, dim): r = torch.arange(1, t.shape[dim] + 1, device=t.device, dtype=t.dtype) expand_slice = [None] * len(t.shape) expand_slice[dim] = slice(None, None) return t.cumsum(dim=dim) / r[tuple(expand_slice)] def mask_reordering_matrix(R, topk, temperature): buckets = R.shape[1] mask_value = max_neg_value(R) mask = torch.zeros(R.shape, device=R.device).bool() i, j = torch.triu_indices(buckets, buckets) mask[:, i, j + topk] = True R.masked_fill_(mask, mask_value) return differentiable_topk(R, topk, temperature) class CausalAttentionSortNetNew(nn.Module): def __init__(self, heads, bucket_size, dim, temperature): super().__init__() self.heads = heads self.bucket_size = bucket_size self.dim = dim self.temperature = temperature def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
tatp22/sinkhorn-transformer
CausalAttentionSortNet
false
13,040
[ "MIT" ]
0
3eaa76e99efeee75cf8298defaaef51621c55ff4
https://github.com/tatp22/sinkhorn-transformer/tree/3eaa76e99efeee75cf8298defaaef51621c55ff4
Critic
import torch import numpy as np import torch.nn.functional as F from torch import nn def hidden_init(layer): fan_in = layer.weight.data.size()[0] lim = 1.0 / np.sqrt(fan_in) return -lim, lim class Critic(nn.Module): """Critic (Value) Model.""" def __init__(self, state_size, action_size, seed, fcs1_units=64, fc2_units=32, fc3_units=32): super(Critic, self).__init__() self.seed = torch.manual_seed(seed) self.fcs1 = nn.Linear(state_size, fcs1_units) self.fc2 = nn.Linear(fcs1_units + action_size, fc2_units) self.fc3 = nn.Linear(fc2_units, fc3_units) self.fc4 = nn.Linear(fc3_units, 1) self.reset_parameters() def reset_parameters(self): self.fcs1.weight.data.uniform_(*hidden_init(self.fcs1)) self.fc2.weight.data.uniform_(*hidden_init(self.fc2)) self.fc3.weight.data.uniform_(*hidden_init(self.fc3)) self.fc4.weight.data.uniform_(-0.003, 0.003) def forward(self, state, action): xs = F.relu(self.fcs1(state)) x = torch.cat((xs, action), dim=1) x = F.relu(self.fc2(x)) x = F.relu(self.fc3(x)) return self.fc4(x) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'state_size': 4, 'action_size': 4, 'seed': 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 assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_cat_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 272 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 68 x1 = xindex // 68 x2 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 64, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (64 * x1 + x0), tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp6 = tl.load(in_ptr1 + x0, tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp7 = tmp5 + tmp6 tmp8 = tl.full([1], 0, tl.int32) tmp9 = triton_helpers.maximum(tmp8, tmp7) tmp10 = tl.full(tmp9.shape, 0.0, tmp9.dtype) tmp11 = tl.where(tmp4, tmp9, tmp10) tmp12 = tmp0 >= tmp3 tl.full([1], 68, tl.int64) tmp15 = tl.load(in_ptr2 + (4 * x1 + (-64 + x0)), tmp12 & xmask, eviction_policy='evict_last', other=0.0) tmp16 = tl.where(tmp4, tmp11, tmp15) tl.store(out_ptr0 + x2, tmp16, xmask) @triton.jit def triton_poi_fused_relu_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 32 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_relu_threshold_backward_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(out_ptr0 + x2, tmp6, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10) = args args.clear() assert_size_stride(primals_1, (64, 4), (4, 1)) assert_size_stride(primals_2, (64,), (1,)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (32, 68), (68, 1)) assert_size_stride(primals_6, (32,), (1,)) assert_size_stride(primals_7, (32, 32), (32, 1)) assert_size_stride(primals_8, (32,), (1,)) assert_size_stride(primals_9, (1, 32), (32, 1)) assert_size_stride(primals_10, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 64), (64, 1), torch.float32) extern_kernels.mm(primals_3, reinterpret_tensor(primals_1, (4, 64), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 68), (68, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(272)](buf0, primals_2, primals_4, buf1, 272, XBLOCK=128, num_warps=4, num_stages=1) del primals_4 buf2 = empty_strided_cuda((4, 32), (32, 1), torch.float32) extern_kernels.mm(buf1, reinterpret_tensor(primals_5, (68, 32), (1, 68), 0), out=buf2) buf3 = buf2 del buf2 triton_poi_fused_relu_1[grid(128)](buf3, primals_6, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_6 buf4 = empty_strided_cuda((4, 32), (32, 1), torch.float32) extern_kernels.mm(buf3, reinterpret_tensor(primals_7, (32, 32), (1, 32), 0), out=buf4) buf5 = buf4 del buf4 triton_poi_fused_relu_1[grid(128)](buf5, primals_8, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_8 buf7 = empty_strided_cuda((4, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_10, buf5, reinterpret_tensor(primals_9, (32, 1), (1, 32), 0), alpha=1, beta=1, out=buf7) del primals_10 buf8 = empty_strided_cuda((4, 64), (64, 1), torch.bool) triton_poi_fused_relu_threshold_backward_2[grid(256)](buf0, primals_2, buf8, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf0 del primals_2 return (buf7, primals_3, buf1, buf3, buf5, primals_9, primals_7, primals_5, buf8) def hidden_init(layer): fan_in = layer.weight.data.size()[0] lim = 1.0 / np.sqrt(fan_in) return -lim, lim class CriticNew(nn.Module): """Critic (Value) Model.""" def __init__(self, state_size, action_size, seed, fcs1_units=64, fc2_units=32, fc3_units=32): super(CriticNew, self).__init__() self.seed = torch.manual_seed(seed) self.fcs1 = nn.Linear(state_size, fcs1_units) self.fc2 = nn.Linear(fcs1_units + action_size, fc2_units) self.fc3 = nn.Linear(fc2_units, fc3_units) self.fc4 = nn.Linear(fc3_units, 1) self.reset_parameters() def reset_parameters(self): self.fcs1.weight.data.uniform_(*hidden_init(self.fcs1)) self.fc2.weight.data.uniform_(*hidden_init(self.fc2)) self.fc3.weight.data.uniform_(*hidden_init(self.fc3)) self.fc4.weight.data.uniform_(-0.003, 0.003) def forward(self, input_0, input_1): primals_1 = self.fcs1.weight primals_2 = self.fcs1.bias primals_5 = self.fc2.weight primals_6 = self.fc2.bias primals_7 = self.fc3.weight primals_8 = self.fc3.bias primals_9 = self.fc4.weight primals_10 = self.fc4.bias primals_3 = input_0 primals_4 = 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]
tjkemp/tennis-example
Critic
false
13,041
[ "MIT" ]
0
3cb0c52a93c65f88872cf44e3782bf87d9d8cef3
https://github.com/tjkemp/tennis-example/tree/3cb0c52a93c65f88872cf44e3782bf87d9d8cef3
DeepModel
import torch import torch.nn as nn import torch.nn.functional as F class DeepModel(nn.Module): def __init__(self, in_size, out_size): super().__init__() self.linear1 = nn.Linear(in_size, 1024) self.linear2 = nn.Linear(1024, 512) self.linear3 = nn.Linear(512, 256) self.linear4 = nn.Linear(256, 128) self.linear5 = nn.Linear(128, 64) self.linear6 = nn.Linear(64, 32) self.linear7 = nn.Linear(32, out_size) def forward(self, xb): xb = xb.view(xb.size(0), -1) out = self.linear1(xb) out = F.relu(out) out = self.linear2(out) out = F.relu(out) out = self.linear3(out) out = F.relu(out) out = self.linear4(out) out = F.relu(out) out = self.linear5(out) out = F.relu(out) out = self.linear6(out) out = F.relu(out) out = self.linear7(out) return out def get_inputs(): return [torch.rand([4, 4])] def get_init_inputs(): return [[], {'in_size': 4, 'out_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers 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_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) x2 = xindex x0 = xindex % 1024 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, None) @triton.jit def triton_poi_fused_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) x2 = xindex x0 = xindex % 512 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, None) @triton.jit def triton_poi_fused_relu_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 256 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_relu_3(in_out_ptr0, in_ptr0, 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 % 128 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_relu_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 % 64 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_relu_5(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 32 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15) = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (1024, 4), (4, 1)) assert_size_stride(primals_3, (1024,), (1,)) assert_size_stride(primals_4, (512, 1024), (1024, 1)) assert_size_stride(primals_5, (512,), (1,)) assert_size_stride(primals_6, (256, 512), (512, 1)) assert_size_stride(primals_7, (256,), (1,)) assert_size_stride(primals_8, (128, 256), (256, 1)) assert_size_stride(primals_9, (128,), (1,)) assert_size_stride(primals_10, (64, 128), (128, 1)) assert_size_stride(primals_11, (64,), (1,)) assert_size_stride(primals_12, (32, 64), (64, 1)) assert_size_stride(primals_13, (32,), (1,)) assert_size_stride(primals_14, (4, 32), (32, 1)) assert_size_stride(primals_15, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 1024), (1024, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 1024 ), (1, 4), 0), out=buf0) del primals_2 buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_relu_0[grid(4096)](buf1, primals_3, 4096, XBLOCK= 256, num_warps=4, num_stages=1) del primals_3 buf2 = empty_strided_cuda((4, 512), (512, 1), torch.float32) extern_kernels.mm(buf1, reinterpret_tensor(primals_4, (1024, 512), (1, 1024), 0), out=buf2) buf3 = buf2 del buf2 triton_poi_fused_relu_1[grid(2048)](buf3, primals_5, 2048, XBLOCK= 256, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((4, 256), (256, 1), torch.float32) extern_kernels.mm(buf3, reinterpret_tensor(primals_6, (512, 256), ( 1, 512), 0), out=buf4) buf5 = buf4 del buf4 triton_poi_fused_relu_2[grid(1024)](buf5, primals_7, 1024, XBLOCK= 128, num_warps=4, num_stages=1) del primals_7 buf6 = empty_strided_cuda((4, 128), (128, 1), torch.float32) extern_kernels.mm(buf5, reinterpret_tensor(primals_8, (256, 128), ( 1, 256), 0), out=buf6) buf7 = buf6 del buf6 triton_poi_fused_relu_3[grid(512)](buf7, primals_9, 512, XBLOCK=128, num_warps=4, num_stages=1) del primals_9 buf8 = empty_strided_cuda((4, 64), (64, 1), torch.float32) extern_kernels.mm(buf7, reinterpret_tensor(primals_10, (128, 64), ( 1, 128), 0), out=buf8) buf9 = buf8 del buf8 triton_poi_fused_relu_4[grid(256)](buf9, primals_11, 256, XBLOCK= 256, num_warps=4, num_stages=1) del primals_11 buf10 = empty_strided_cuda((4, 32), (32, 1), torch.float32) extern_kernels.mm(buf9, reinterpret_tensor(primals_12, (64, 32), (1, 64), 0), out=buf10) buf11 = buf10 del buf10 triton_poi_fused_relu_5[grid(128)](buf11, primals_13, 128, XBLOCK= 128, num_warps=4, num_stages=1) del primals_13 buf12 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_15, buf11, reinterpret_tensor( primals_14, (32, 4), (1, 32), 0), alpha=1, beta=1, out=buf12) del primals_15 return (buf12, primals_1, buf1, buf3, buf5, buf7, buf9, buf11, primals_14, primals_12, primals_10, primals_8, primals_6, primals_4) class DeepModelNew(nn.Module): def __init__(self, in_size, out_size): super().__init__() self.linear1 = nn.Linear(in_size, 1024) self.linear2 = nn.Linear(1024, 512) self.linear3 = nn.Linear(512, 256) self.linear4 = nn.Linear(256, 128) self.linear5 = nn.Linear(128, 64) self.linear6 = nn.Linear(64, 32) self.linear7 = nn.Linear(32, out_size) def forward(self, input_0): primals_2 = self.linear1.weight primals_3 = self.linear1.bias primals_4 = self.linear2.weight primals_5 = self.linear2.bias primals_6 = self.linear3.weight primals_7 = self.linear3.bias primals_8 = self.linear4.weight primals_9 = self.linear4.bias primals_10 = self.linear5.weight primals_11 = self.linear5.bias primals_12 = self.linear6.weight primals_13 = self.linear6.bias primals_14 = self.linear7.weight primals_15 = self.linear7.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]) return output[0]
tianyi-ge/eecs598-a1
DeepModel
false
13,042
[ "MIT" ]
0
540140c5c2a59931ee051a0064932a1e81f84806
https://github.com/tianyi-ge/eecs598-a1/tree/540140c5c2a59931ee051a0064932a1e81f84806
GaussianNoiseSampler
import torch import torch as th import torch.nn as nn class GaussianNoiseSampler(nn.Module): def __init__(self, scale=0.01, inplace=False): super(GaussianNoiseSampler, self).__init__() if scale < 0: raise ValueError( 'noise scale has to be greather than 0, but got {}'.format( scale)) self.scale = scale self.inplace = inplace def forward(self, inputs): if self.scale: inputs.add_(th.randn_like(inputs) * self.scale) return inputs def extra_repr(self): inplace_str = ', inplace' if self.inplace else '' return 'scale={}{}'.format(self.scale, inplace_str) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
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, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_out_ptr0 + x0, xmask) tmp2 = 0.01 tmp3 = tmp1 * tmp2 tmp4 = tmp0 + 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 = 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, arg0_1, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf2 return arg0_1, class GaussianNoiseSamplerNew(nn.Module): def __init__(self, scale=0.01, inplace=False): super(GaussianNoiseSamplerNew, self).__init__() if scale < 0: raise ValueError( 'noise scale has to be greather than 0, but got {}'.format( scale)) self.scale = scale self.inplace = inplace def extra_repr(self): inplace_str = ', inplace' if self.inplace else '' return 'scale={}{}'.format(self.scale, inplace_str) def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
tritas/mixdat
GaussianNoiseSampler
false
13,043
[ "BSD-3-Clause" ]
0
38fb10df76df55cc1eddba5375c7699c23771fb3
https://github.com/tritas/mixdat/tree/38fb10df76df55cc1eddba5375c7699c23771fb3
Projection
from _paritybench_helpers import _mock_config import torch import torch.nn as nn class TimeDistributed(nn.Module): def __init__(self, layer, activation='relu'): super().__init__() self.layer = layer self.activation = self.select_activation(activation) def forward(self, x): x_reshaped = x.contiguous().view(-1, x.size(-1)) y = self.layer(x_reshaped) y = self.activation(y) y = y.contiguous().view(x.size(0), -1, y.size(-1)) return y def select_activation(self, activation): if activation == 'relu': return nn.ReLU() elif activation == 'sigmoid': return nn.Sigmoid() elif activation == 'tanh': return nn.Tanh() raise KeyError class Projection(nn.Module): def __init__(self, config): super().__init__() self.seq_fnn = TimeDistributed(nn.Linear(config['cnn_features'], 3)) self.softmax = nn.LogSoftmax(dim=-1) def forward(self, x): x = torch.transpose(x, 1, 0) x = self.seq_fnn(x) x = torch.transpose(x, 1, 0) x = self.softmax(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'config': _mock_config(cnn_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 math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clone_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 16 x1 = xindex // 16 % 4 x2 = xindex // 64 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 16 * x2 + 64 * x1), xmask) tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_poi_fused__log_softmax_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 + 3 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp6 = tl.load(in_ptr0 + (1 + 3 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr1 + 1) tmp8 = tl.broadcast_to(tmp7, [XBLOCK]) tmp12 = tl.load(in_ptr0 + (2 + 3 * x0), xmask, eviction_policy='evict_last' ) tmp13 = tl.load(in_ptr1 + 2) tmp14 = tl.broadcast_to(tmp13, [XBLOCK]) tmp3 = tmp0 + tmp2 tmp4 = tl.full([1], 0, tl.int32) tmp5 = triton_helpers.maximum(tmp4, tmp3) tmp9 = tmp6 + tmp8 tmp10 = triton_helpers.maximum(tmp4, tmp9) tmp11 = triton_helpers.maximum(tmp5, tmp10) tmp15 = tmp12 + tmp14 tmp16 = triton_helpers.maximum(tmp4, tmp15) tmp17 = triton_helpers.maximum(tmp11, tmp16) tmp18 = tmp5 - tmp17 tmp19 = tl_math.exp(tmp18) tmp20 = tmp10 - tmp17 tmp21 = tl_math.exp(tmp20) tmp22 = tmp19 + tmp21 tmp23 = tmp16 - tmp17 tmp24 = tl_math.exp(tmp23) tmp25 = tmp22 + tmp24 tmp26 = tl_math.log(tmp25) tl.store(out_ptr0 + x0, tmp17, xmask) tl.store(out_ptr1 + x0, tmp26, xmask) @triton.jit def triton_poi_fused__log_softmax_relu_threshold_backward_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr ): xnumel = 192 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex x0 = xindex % 3 x3 = xindex // 3 x1 = xindex // 3 % 16 x2 = xindex // 48 tmp0 = tl.load(in_ptr0 + x4, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr2 + x3, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr3 + x3, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = tmp4 - tmp5 tmp8 = tmp6 - tmp7 tmp9 = 0.0 tmp10 = tmp4 <= tmp9 tl.store(out_ptr0 + (x0 + 3 * x2 + 12 * x1), tmp8, xmask) tl.store(out_ptr1 + x4, tmp10, 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, (3, 4), (4, 1)) assert_size_stride(primals_3, (3,), (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(256)](primals_1, buf0, 256, XBLOCK= 256, num_warps=4, num_stages=1) del primals_1 buf1 = empty_strided_cuda((64, 3), (3, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (64, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 3), (1, 4), 0), out=buf1) del primals_2 buf2 = empty_strided_cuda((16, 4, 1), (1, 16, 64), torch.float32) buf3 = empty_strided_cuda((16, 4, 1), (1, 16, 64), torch.float32) triton_poi_fused__log_softmax_1[grid(64)](buf1, primals_3, buf2, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) buf4 = empty_strided_cuda((16, 4, 3), (12, 3, 1), torch.float32) buf5 = empty_strided_cuda((64, 3), (3, 1), torch.bool) triton_poi_fused__log_softmax_relu_threshold_backward_2[grid(192)](buf1 , primals_3, buf2, buf3, buf4, buf5, 192, XBLOCK=256, num_warps =4, num_stages=1) del buf1 del buf2 del buf3 del primals_3 return buf4, reinterpret_tensor(buf0, (64, 4), (4, 1), 0), buf4, buf5 class TimeDistributed(nn.Module): def __init__(self, layer, activation='relu'): super().__init__() self.layer = layer self.activation = self.select_activation(activation) def forward(self, x): x_reshaped = x.contiguous().view(-1, x.size(-1)) y = self.layer(x_reshaped) y = self.activation(y) y = y.contiguous().view(x.size(0), -1, y.size(-1)) return y def select_activation(self, activation): if activation == 'relu': return nn.ReLU() elif activation == 'sigmoid': return nn.Sigmoid() elif activation == 'tanh': return nn.Tanh() raise KeyError class ProjectionNew(nn.Module): def __init__(self, config): super().__init__() self.seq_fnn = TimeDistributed(nn.Linear(config['cnn_features'], 3)) self.softmax = nn.LogSoftmax(dim=-1) def forward(self, input_0): primals_2 = self.seq_fnn.layer.weight primals_3 = self.seq_fnn.layer.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
tndls9304/chatspace
Projection
false
13,044
[ "Apache-2.0" ]
0
42cb4bd9bd3b553706d9ac227150329103d681aa
https://github.com/tndls9304/chatspace/tree/42cb4bd9bd3b553706d9ac227150329103d681aa
Model
import torch import torch.nn as nn class Model(nn.Module): def __init__(self, input_dim, output_class_num, **kwargs): super(Model, self).__init__() self.linear = nn.Linear(input_dim, output_class_num) def forward(self, features): pooled = features.mean(dim=1) predicted = self.linear(pooled) return predicted def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'output_class_num': 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, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4), (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), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mean_0[grid(64)](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.addmm(primals_3, reinterpret_tensor(buf0, (16, 4), ( 4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf1) del primals_2 del primals_3 return reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(buf0, (16, 4), (4, 1), 0) class ModelNew(nn.Module): def __init__(self, input_dim, output_class_num, **kwargs): super(ModelNew, self).__init__() self.linear = nn.Linear(input_dim, output_class_num) def forward(self, input_0): primals_2 = self.linear.weight primals_3 = self.linear.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
triper1022/s3prl
Model
false
13,045
[ "MIT" ]
0
d48e9e1d062d6cb14b66048eb56193fb50c60c24
https://github.com/triper1022/s3prl/tree/d48e9e1d062d6cb14b66048eb56193fb50c60c24
resnet_block
import torch import torch.nn as nn import torch.nn.functional as F class resnet_block(nn.Module): def __init__(self, dim_in, dim_out): super(resnet_block, self).__init__() self.dim_in = dim_in self.dim_out = dim_out if self.dim_in == self.dim_out: self.conv_1 = nn.Conv2d(self.dim_in, self.dim_out, 3, stride=1, padding=1, bias=False) self.conv_2 = nn.Conv2d(self.dim_out, self.dim_out, 3, stride=1, padding=1, bias=False) nn.init.xavier_uniform_(self.conv_1.weight) nn.init.xavier_uniform_(self.conv_2.weight) else: self.conv_1 = nn.Conv2d(self.dim_in, self.dim_out, 3, stride=2, padding=1, bias=False) self.conv_2 = nn.Conv2d(self.dim_out, self.dim_out, 3, stride=1, padding=1, bias=False) self.conv_s = nn.Conv2d(self.dim_in, self.dim_out, 1, stride=2, padding=0, bias=False) nn.init.xavier_uniform_(self.conv_1.weight) nn.init.xavier_uniform_(self.conv_2.weight) nn.init.xavier_uniform_(self.conv_s.weight) def forward(self, input, is_training=False): if self.dim_in == self.dim_out: output = self.conv_1(input) output = F.leaky_relu(output, negative_slope=0.01, inplace=True) output = self.conv_2(output) output = output + input output = F.leaky_relu(output, negative_slope=0.01, inplace=True) else: output = self.conv_1(input) output = F.leaky_relu(output, negative_slope=0.01, inplace=True) output = self.conv_2(output) input_ = self.conv_s(input) output = output + input_ output = F.leaky_relu(output, negative_slope=0.01, inplace=True) return output def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'dim_in': 4, 'dim_out': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_leaky_relu_0(in_out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = 0.0 tmp2 = tmp0 > tmp1 tmp3 = 0.01 tmp4 = tmp0 * tmp3 tmp5 = tl.where(tmp2, tmp0, tmp4) tl.store(in_out_ptr0 + x0, tmp5, xmask) @triton.jit def triton_poi_fused_add_leaky_relu_leaky_relu_backward_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 x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask) tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tmp8 = tmp7 > tmp3 tl.store(in_out_ptr0 + x0, tmp7, xmask) tl.store(out_ptr0 + x0, tmp8, xmask) def call(args): primals_1, primals_2, primals_3 = 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, 4, 3, 3), (36, 9, 3, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_2, primals_1, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_leaky_relu_0[grid(256)](buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf2 = extern_kernels.convolution(buf1, primals_3, 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 buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_add_leaky_relu_leaky_relu_backward_1[grid(256)](buf3, primals_2, buf4, 256, XBLOCK=128, num_warps=4, num_stages=1) return buf3, primals_1, primals_2, primals_3, buf1, buf4 class resnet_blockNew(nn.Module): def __init__(self, dim_in, dim_out): super(resnet_blockNew, self).__init__() self.dim_in = dim_in self.dim_out = dim_out if self.dim_in == self.dim_out: self.conv_1 = nn.Conv2d(self.dim_in, self.dim_out, 3, stride=1, padding=1, bias=False) self.conv_2 = nn.Conv2d(self.dim_out, self.dim_out, 3, stride=1, padding=1, bias=False) nn.init.xavier_uniform_(self.conv_1.weight) nn.init.xavier_uniform_(self.conv_2.weight) else: self.conv_1 = nn.Conv2d(self.dim_in, self.dim_out, 3, stride=2, padding=1, bias=False) self.conv_2 = nn.Conv2d(self.dim_out, self.dim_out, 3, stride=1, padding=1, bias=False) self.conv_s = nn.Conv2d(self.dim_in, self.dim_out, 1, stride=2, padding=0, bias=False) nn.init.xavier_uniform_(self.conv_1.weight) nn.init.xavier_uniform_(self.conv_2.weight) nn.init.xavier_uniform_(self.conv_s.weight) def forward(self, input_0): primals_1 = self.conv_1.weight primals_3 = self.conv_2.weight primals_2 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
trisct/BSP-NET-pytorch
resnet_block
false
13,046
[ "MIT" ]
0
31f148aa3d7321bac854bc3de6c88f676236b7e4
https://github.com/trisct/BSP-NET-pytorch/tree/31f148aa3d7321bac854bc3de6c88f676236b7e4
MLP
import torch import torch.nn as nn import torch.nn.functional as F class MLP(nn.Module): def __init__(self): super(MLP, self).__init__() self.fc1 = nn.Linear(in_features=28 * 28, out_features=500) self.fc2 = nn.Linear(in_features=500, out_features=200) self.fc3 = nn.Linear(in_features=200, out_features=100) self.out = nn.Linear(in_features=100, out_features=10) def forward(self, t): t = t.view(-1, 28 * 28) t = self.fc1(t) t = F.relu(t) t = self.fc2(t) t = F.relu(t) t = self.fc3(t) t = F.relu(t) t = self.out(t) return t def get_inputs(): return [torch.rand([4, 784])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_relu_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 2000 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 500 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_relu_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 800 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 200 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_relu_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 400 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 100 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) 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, 784), (784, 1)) assert_size_stride(primals_2, (500, 784), (784, 1)) assert_size_stride(primals_3, (500,), (1,)) assert_size_stride(primals_4, (200, 500), (500, 1)) assert_size_stride(primals_5, (200,), (1,)) assert_size_stride(primals_6, (100, 200), (200, 1)) assert_size_stride(primals_7, (100,), (1,)) assert_size_stride(primals_8, (10, 100), (100, 1)) assert_size_stride(primals_9, (10,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 500), (500, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (784, 500), (1, 784), 0), out=buf0) del primals_2 buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_relu_0[grid(2000)](buf1, primals_3, 2000, XBLOCK= 256, num_warps=4, num_stages=1) del primals_3 buf2 = empty_strided_cuda((4, 200), (200, 1), torch.float32) extern_kernels.mm(buf1, reinterpret_tensor(primals_4, (500, 200), ( 1, 500), 0), out=buf2) buf3 = buf2 del buf2 triton_poi_fused_relu_1[grid(800)](buf3, primals_5, 800, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((4, 100), (100, 1), torch.float32) extern_kernels.mm(buf3, reinterpret_tensor(primals_6, (200, 100), ( 1, 200), 0), out=buf4) buf5 = buf4 del buf4 triton_poi_fused_relu_2[grid(400)](buf5, primals_7, 400, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 buf6 = empty_strided_cuda((4, 10), (10, 1), torch.float32) extern_kernels.addmm(primals_9, buf5, reinterpret_tensor(primals_8, (100, 10), (1, 100), 0), alpha=1, beta=1, out=buf6) del primals_9 return buf6, primals_1, buf1, buf3, buf5, primals_8, primals_6, primals_4 class MLPNew(nn.Module): def __init__(self): super(MLPNew, self).__init__() self.fc1 = nn.Linear(in_features=28 * 28, out_features=500) self.fc2 = nn.Linear(in_features=500, out_features=200) self.fc3 = nn.Linear(in_features=200, out_features=100) self.out = nn.Linear(in_features=100, out_features=10) def forward(self, input_0): primals_2 = self.fc1.weight primals_3 = 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.out.weight primals_9 = self.out.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]
trGiang99/ml-glossary-vn
MLP
false
13,047
[ "MIT" ]
0
1160300cee6ccb02712c790b76bbc11c06c2ca55
https://github.com/trGiang99/ml-glossary-vn/tree/1160300cee6ccb02712c790b76bbc11c06c2ca55
generator
import torch import torch.nn as nn class generator(nn.Module): def __init__(self, p_dim, c_dim): super(generator, self).__init__() self.p_dim = p_dim self.c_dim = c_dim convex_layer_weights = torch.zeros((self.p_dim, self.c_dim)) self.convex_layer_weights = nn.Parameter(convex_layer_weights) nn.init.normal_(self.convex_layer_weights, mean=0.0, std=0.02) def forward(self, points, plane_m, is_training=False): h1 = torch.matmul(points, plane_m) h1 = torch.clamp(h1, min=0) h2 = torch.matmul(h1, (self.convex_layer_weights > 0.01).float()) h3 = torch.min(h2, dim=2, keepdim=True)[0] return h2, h3 def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'p_dim': 4, 'c_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clamp_0(in_out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = 0.0 tmp2 = triton_helpers.maximum(tmp0, tmp1) tl.store(in_out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_poi_fused_gt_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) tmp1 = 0.01 tmp2 = tmp0 > tmp1 tl.store(out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_poi_fused_min_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 % 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.minimum(tmp0, tmp1) tmp4 = triton_helpers.minimum(tmp2, tmp3) tmp6 = triton_helpers.minimum(tmp4, tmp5) tl.store(out_ptr0 + x2, tmp6, xmask) def call(args): arg0_1, arg1_1, arg2_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg2_1, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(arg1_1, (16, 4, 4), (16, 4, 1 ), 0), reinterpret_tensor(arg0_1, (16, 4, 4), (16, 4, 1), 0), out=buf0) del arg0_1 del arg1_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 get_raw_stream(0) triton_poi_fused_clamp_0[grid(256)](buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.bool) triton_poi_fused_gt_1[grid(16)](arg2_1, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg2_1 buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.fallback_mixed_mm(reinterpret_tensor(buf1, (64, 4), (4, 1), 0), buf2, out=buf3) del buf1 del buf2 buf4 = empty_strided_cuda((4, 4, 1, 4), (16, 4, 4, 1), torch.float32) triton_poi_fused_min_2[grid(64)](buf3, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) return reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0), buf4 class generatorNew(nn.Module): def __init__(self, p_dim, c_dim): super(generatorNew, self).__init__() self.p_dim = p_dim self.c_dim = c_dim convex_layer_weights = torch.zeros((self.p_dim, self.c_dim)) self.convex_layer_weights = nn.Parameter(convex_layer_weights) nn.init.normal_(self.convex_layer_weights, mean=0.0, std=0.02) def forward(self, input_0, input_1): arg2_1 = self.convex_layer_weights arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1, arg2_1]) return output[0], output[1]
trisct/BSP-NET-pytorch
generator
false
13,048
[ "MIT" ]
0
31f148aa3d7321bac854bc3de6c88f676236b7e4
https://github.com/trisct/BSP-NET-pytorch/tree/31f148aa3d7321bac854bc3de6c88f676236b7e4
FocalLoss
import torch from torch import nn class FocalLoss(nn.Module): def __init__(self, gamma=0, eps=1e-07): super(FocalLoss, self).__init__() self.gamma = gamma self.eps = eps self.ce = torch.nn.CrossEntropyLoss(reduction='none') def forward(self, input, target): logp = self.ce(input, target) p = torch.exp(-logp) loss = (1 - p) ** self.gamma * logp 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_poi_fused__log_softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tl.store(out_ptr0 + x3, tmp8, xmask) @triton.jit def triton_per_fused__log_softmax_exp_mean_mul_neg_pow_rsub_sum_1(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 64 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex % 16 r1 = rindex // 16 tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None) tmp2 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None) tmp5 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None) tmp8 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None) tmp13 = tl.load(in_ptr1 + (r0 + 64 * r1), None) tmp16 = tl.load(in_ptr1 + (16 + r0 + 64 * r1), None) tmp20 = tl.load(in_ptr1 + (32 + r0 + 64 * r1), None) tmp24 = tl.load(in_ptr1 + (48 + r0 + 64 * r1), None) tmp1 = tl_math.exp(tmp0) tmp3 = tl_math.exp(tmp2) tmp4 = tmp1 + tmp3 tmp6 = tl_math.exp(tmp5) tmp7 = tmp4 + tmp6 tmp9 = tl_math.exp(tmp8) tmp10 = tmp7 + tmp9 tmp11 = tl_math.log(tmp10) tmp12 = tmp0 - tmp11 tmp14 = tmp12 * tmp13 tmp15 = tmp2 - tmp11 tmp17 = tmp15 * tmp16 tmp18 = tmp14 + tmp17 tmp19 = tmp5 - tmp11 tmp21 = tmp19 * tmp20 tmp22 = tmp18 + tmp21 tmp23 = tmp8 - tmp11 tmp25 = tmp23 * tmp24 tmp26 = tmp22 + tmp25 tmp27 = -tmp26 tmp28 = -tmp27 tmp29 = tl_math.exp(tmp28) tmp30 = 1.0 tmp30 - tmp29 tmp32 = tmp30 * tmp27 tmp33 = tl.broadcast_to(tmp32, [XBLOCK, RBLOCK]) tmp35 = tl.sum(tmp33, 1)[:, None] tmp36 = 64.0 tmp37 = tmp35 / tmp36 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp37, 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__log_softmax_0[grid(256)](arg1_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg1_1 buf2 = empty_strided_cuda((), (), torch.float32) buf3 = buf2 del buf2 triton_per_fused__log_softmax_exp_mean_mul_neg_pow_rsub_sum_1[grid(1)]( buf3, buf0, arg0_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del buf0 return buf3, class FocalLossNew(nn.Module): def __init__(self, gamma=0, eps=1e-07): super(FocalLossNew, self).__init__() self.gamma = gamma self.eps = eps self.ce = torch.nn.CrossEntropyLoss(reduction='none') def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
tropicbird/kaggle-landmark-recognition-2020-1st-place
FocalLoss
false
13,049
[ "MIT" ]
0
79a9d1b05c326a77b4859d4d41d30e52e6be710e
https://github.com/tropicbird/kaggle-landmark-recognition-2020-1st-place/tree/79a9d1b05c326a77b4859d4d41d30e52e6be710e
Conv2dStaticSamePadding
import math import torch from torch import nn from torch.nn import functional as F class Conv2dStaticSamePadding(nn.Module): """ created by Zylo117 The real keras/tensorflow conv2d with same padding """ def __init__(self, in_channels, out_channels, kernel_size, stride=1, bias=False, groups=1, dilation=1, **kwargs): super().__init__() self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride, bias=bias, groups=groups) self.stride = self.conv.stride self.kernel_size = self.conv.kernel_size self.dilation = self.conv.dilation if isinstance(self.stride, int): self.stride = [self.stride] * 2 elif len(self.stride) == 1: self.stride = [self.stride[0]] * 2 if isinstance(self.kernel_size, int): self.kernel_size = [self.kernel_size] * 2 elif len(self.kernel_size) == 1: self.kernel_size = [self.kernel_size[0]] * 2 def forward(self, x): h, w = x.shape[-2:] extra_h = (math.ceil(w / self.stride[1]) - 1) * self.stride[1 ] - w + self.kernel_size[1] extra_v = (math.ceil(h / self.stride[0]) - 1) * self.stride[0 ] - h + self.kernel_size[0] left = extra_h // 2 right = extra_h - left top = extra_v // 2 bottom = extra_v - top x = F.pad(x, [left, right, top, bottom]) x = self.conv(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4, 'kernel_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch 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_constant_pad_nd_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 784 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 7 % 7 x0 = xindex % 7 x2 = xindex // 49 x4 = xindex tmp0 = -1 + x1 tmp1 = tl.full([1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = -1 + x0 tmp6 = tmp5 >= tmp1 tmp7 = tmp5 < tmp3 tmp8 = tmp2 & tmp4 tmp9 = tmp8 & tmp6 tmp10 = tmp9 & tmp7 tmp11 = tl.load(in_ptr0 + (-5 + x0 + 4 * x1 + 16 * x2), tmp10 & xmask, other=0.0) tl.store(out_ptr0 + x4, tmp11, 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, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 7, 7), (196, 49, 7, 1), torch.float32) get_raw_stream(0) triton_poi_fused_constant_pad_nd_0[grid(784)](primals_1, buf0, 784, 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)) return buf1, primals_2, buf0 class Conv2dStaticSamePaddingNew(nn.Module): """ created by Zylo117 The real keras/tensorflow conv2d with same padding """ def __init__(self, in_channels, out_channels, kernel_size, stride=1, bias=False, groups=1, dilation=1, **kwargs): super().__init__() self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride, bias=bias, groups=groups) self.stride = self.conv.stride self.kernel_size = self.conv.kernel_size self.dilation = self.conv.dilation if isinstance(self.stride, int): self.stride = [self.stride] * 2 elif len(self.stride) == 1: self.stride = [self.stride[0]] * 2 if isinstance(self.kernel_size, int): self.kernel_size = [self.kernel_size] * 2 elif len(self.kernel_size) == 1: self.kernel_size = [self.kernel_size[0]] * 2 def forward(self, input_0): primals_1 = self.conv.weight primals_2 = input_0 output = call([primals_1, primals_2]) return output[0]
tujikuangmo/FishNet
Conv2dStaticSamePadding
false
13,050
[ "MIT" ]
0
1c2f7112639416bd12a02585a9e04e1d05960520
https://github.com/tujikuangmo/FishNet/tree/1c2f7112639416bd12a02585a9e04e1d05960520
GAT
import torch import torch.nn as nn import torch.nn.functional as F class GraphAttentionLayer(nn.Module): """ Simple GAT layer, similar to https://arxiv.org/abs/1710.10903 """ def __init__(self, in_features, out_features, dropout, alpha, concat=True): super(GraphAttentionLayer, self).__init__() self.dropout = dropout self.in_features = in_features self.out_features = out_features self.alpha = alpha self.concat = concat self.W = nn.Parameter(torch.zeros(size=(in_features, out_features))) nn.init.xavier_uniform_(self.W.data, gain=1.414) self.a = nn.Parameter(torch.zeros(size=(2 * out_features, 1))) nn.init.xavier_uniform_(self.a.data, gain=1.414) self.leakyrelu = nn.LeakyReLU(self.alpha) def forward(self, input, adj): h = torch.mm(input, self.W) """u = torch.mm(input, self.U) v = torch.mm(input, self.V) h = torch.mm(torch.sigmoid(u) * torch.sigmoid(v), self.P)""" N = h.size()[0] a_input = torch.cat([h.repeat(1, N).view(N * N, -1), h.repeat(N, 1) ], dim=1).view(N, -1, 2 * self.out_features) e = self.leakyrelu(torch.matmul(a_input, self.a).squeeze(2)) zero_vec = -9000000000000000.0 * torch.ones_like(e) attention = torch.where(adj > 0, e, zero_vec) attention = F.softmax(attention, dim=1) attention = F.dropout(attention, self.dropout, training=self.training) h_prime = torch.matmul(attention, h) if self.concat: return F.elu(h_prime) else: return h_prime def __repr__(self): return self.__class__.__name__ + ' (' + str(self.in_features ) + ' -> ' + str(self.out_features) + ')' class GAT(nn.Module): def __init__(self, nfeat, nhid, nclass, dropout, alpha, nheads): """Dense version of GAT.""" super(GAT, self).__init__() self.dropout = dropout self.encoder = nn.Parameter(torch.zeros(size=(nfeat, nhid))) nn.init.xavier_uniform_(self.encoder.data, gain=1.414) self.attentions = [GraphAttentionLayer(nhid, nhid, dropout=dropout, alpha=alpha, concat=True) for _ in range(nheads)] for i, attention in enumerate(self.attentions): self.add_module('attention_{}'.format(i), attention) self.out_att = GraphAttentionLayer(nhid * nheads, nclass, dropout= dropout, alpha=alpha, concat=False) def forward(self, x_org, adj): x = torch.mm(x_org, self.encoder) x = F.dropout(x, self.dropout, training=self.training) x = torch.cat([att(x, adj) for att in self.attentions], dim=1) x = F.dropout(x, self.dropout, training=self.training) x = F.elu(self.out_att(x, adj)) return F.log_softmax(x, dim=1) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'nfeat': 4, 'nhid': 4, 'nclass': 4, 'dropout': 0.5, 'alpha': 4, 'nheads': 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.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_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 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 * ((4 * x1 + x0) // 16 % 4) + (4 * x1 + x0) % 16 % 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 * (x1 % 4) + (-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_leaky_relu_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) tmp1 = 0.0 tmp2 = tmp0 > tmp1 tl.store(out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_poi_fused__softmax_leaky_relu_mul_where_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr4, out_ptr5, out_ptr6, out_ptr7, 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').to(tl .int1) tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last').to(tl .int1) tmp2 = tl.load(in_ptr2 + 4 * x0, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp9 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp10 = tl.load(in_ptr2 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp15 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp16 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp17 = tl.load(in_ptr2 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp22 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp23 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp24 = tl.load(in_ptr2 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp40 = tl.load(in_ptr3 + 4 * x0, xmask, eviction_policy='evict_last').to( tl.int1) tmp41 = tl.load(in_ptr4 + 4 * x0, xmask, eviction_policy='evict_last') tmp45 = tl.load(in_ptr3 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp46 = tl.load(in_ptr4 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp51 = tl.load(in_ptr3 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp52 = tl.load(in_ptr4 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp57 = tl.load(in_ptr3 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp58 = tl.load(in_ptr4 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp74 = tl.load(in_ptr5 + 4 * x0, xmask, eviction_policy='evict_last').to( tl.int1) tmp75 = tl.load(in_ptr6 + 4 * x0, xmask, eviction_policy='evict_last') tmp79 = tl.load(in_ptr5 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp80 = tl.load(in_ptr6 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp85 = tl.load(in_ptr5 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp86 = tl.load(in_ptr6 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp91 = tl.load(in_ptr5 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp92 = tl.load(in_ptr6 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp108 = tl.load(in_ptr7 + 4 * x0, xmask, eviction_policy='evict_last').to( tl.int1) tmp109 = tl.load(in_ptr8 + 4 * x0, xmask, eviction_policy='evict_last') tmp113 = tl.load(in_ptr7 + (1 + 4 * x0), xmask, eviction_policy= 'evict_last').to(tl.int1) tmp114 = tl.load(in_ptr8 + (1 + 4 * x0), xmask, eviction_policy= 'evict_last') tmp119 = tl.load(in_ptr7 + (2 + 4 * x0), xmask, eviction_policy= 'evict_last').to(tl.int1) tmp120 = tl.load(in_ptr8 + (2 + 4 * x0), xmask, eviction_policy= 'evict_last') tmp125 = tl.load(in_ptr7 + (3 + 4 * x0), xmask, eviction_policy= 'evict_last').to(tl.int1) tmp126 = tl.load(in_ptr8 + (3 + 4 * x0), xmask, eviction_policy= 'evict_last') tmp3 = 4.0 tmp4 = tmp2 * tmp3 tmp5 = tl.where(tmp1, tmp2, tmp4) tmp6 = -8999999815811072.0 tmp7 = tl.where(tmp0, tmp5, tmp6) tmp11 = tmp10 * tmp3 tmp12 = tl.where(tmp9, tmp10, tmp11) tmp13 = tl.where(tmp8, tmp12, tmp6) tmp14 = triton_helpers.maximum(tmp7, tmp13) tmp18 = tmp17 * tmp3 tmp19 = tl.where(tmp16, tmp17, tmp18) tmp20 = tl.where(tmp15, tmp19, tmp6) tmp21 = triton_helpers.maximum(tmp14, tmp20) tmp25 = tmp24 * tmp3 tmp26 = tl.where(tmp23, tmp24, tmp25) tmp27 = tl.where(tmp22, tmp26, tmp6) tmp28 = triton_helpers.maximum(tmp21, tmp27) tmp29 = tmp7 - tmp28 tmp30 = tl_math.exp(tmp29) tmp31 = tmp13 - tmp28 tmp32 = tl_math.exp(tmp31) tmp33 = tmp30 + tmp32 tmp34 = tmp20 - tmp28 tmp35 = tl_math.exp(tmp34) tmp36 = tmp33 + tmp35 tmp37 = tmp27 - tmp28 tmp38 = tl_math.exp(tmp37) tmp39 = tmp36 + tmp38 tmp42 = tmp41 * tmp3 tmp43 = tl.where(tmp40, tmp41, tmp42) tmp44 = tl.where(tmp0, tmp43, tmp6) tmp47 = tmp46 * tmp3 tmp48 = tl.where(tmp45, tmp46, tmp47) tmp49 = tl.where(tmp8, tmp48, tmp6) tmp50 = triton_helpers.maximum(tmp44, tmp49) tmp53 = tmp52 * tmp3 tmp54 = tl.where(tmp51, tmp52, tmp53) tmp55 = tl.where(tmp15, tmp54, tmp6) tmp56 = triton_helpers.maximum(tmp50, tmp55) tmp59 = tmp58 * tmp3 tmp60 = tl.where(tmp57, tmp58, tmp59) tmp61 = tl.where(tmp22, tmp60, tmp6) tmp62 = triton_helpers.maximum(tmp56, tmp61) tmp63 = tmp44 - tmp62 tmp64 = tl_math.exp(tmp63) tmp65 = tmp49 - tmp62 tmp66 = tl_math.exp(tmp65) tmp67 = tmp64 + tmp66 tmp68 = tmp55 - tmp62 tmp69 = tl_math.exp(tmp68) tmp70 = tmp67 + tmp69 tmp71 = tmp61 - tmp62 tmp72 = tl_math.exp(tmp71) tmp73 = tmp70 + tmp72 tmp76 = tmp75 * tmp3 tmp77 = tl.where(tmp74, tmp75, tmp76) tmp78 = tl.where(tmp0, tmp77, tmp6) tmp81 = tmp80 * tmp3 tmp82 = tl.where(tmp79, tmp80, tmp81) tmp83 = tl.where(tmp8, tmp82, tmp6) tmp84 = triton_helpers.maximum(tmp78, tmp83) tmp87 = tmp86 * tmp3 tmp88 = tl.where(tmp85, tmp86, tmp87) tmp89 = tl.where(tmp15, tmp88, tmp6) tmp90 = triton_helpers.maximum(tmp84, tmp89) tmp93 = tmp92 * tmp3 tmp94 = tl.where(tmp91, tmp92, tmp93) tmp95 = tl.where(tmp22, tmp94, tmp6) tmp96 = triton_helpers.maximum(tmp90, tmp95) tmp97 = tmp78 - tmp96 tmp98 = tl_math.exp(tmp97) tmp99 = tmp83 - tmp96 tmp100 = tl_math.exp(tmp99) tmp101 = tmp98 + tmp100 tmp102 = tmp89 - tmp96 tmp103 = tl_math.exp(tmp102) tmp104 = tmp101 + tmp103 tmp105 = tmp95 - tmp96 tmp106 = tl_math.exp(tmp105) tmp107 = tmp104 + tmp106 tmp110 = tmp109 * tmp3 tmp111 = tl.where(tmp108, tmp109, tmp110) tmp112 = tl.where(tmp0, tmp111, tmp6) tmp115 = tmp114 * tmp3 tmp116 = tl.where(tmp113, tmp114, tmp115) tmp117 = tl.where(tmp8, tmp116, tmp6) tmp118 = triton_helpers.maximum(tmp112, tmp117) tmp121 = tmp120 * tmp3 tmp122 = tl.where(tmp119, tmp120, tmp121) tmp123 = tl.where(tmp15, tmp122, tmp6) tmp124 = triton_helpers.maximum(tmp118, tmp123) tmp127 = tmp126 * tmp3 tmp128 = tl.where(tmp125, tmp126, tmp127) tmp129 = tl.where(tmp22, tmp128, tmp6) tmp130 = triton_helpers.maximum(tmp124, tmp129) tmp131 = tmp112 - tmp130 tmp132 = tl_math.exp(tmp131) tmp133 = tmp117 - tmp130 tmp134 = tl_math.exp(tmp133) tmp135 = tmp132 + tmp134 tmp136 = tmp123 - tmp130 tmp137 = tl_math.exp(tmp136) tmp138 = tmp135 + tmp137 tmp139 = tmp129 - tmp130 tmp140 = tl_math.exp(tmp139) tmp141 = tmp138 + tmp140 tl.store(out_ptr0 + x0, tmp28, xmask) tl.store(out_ptr1 + x0, tmp39, xmask) tl.store(out_ptr2 + x0, tmp62, xmask) tl.store(out_ptr3 + x0, tmp73, xmask) tl.store(out_ptr4 + x0, tmp96, xmask) tl.store(out_ptr5 + x0, tmp107, xmask) tl.store(out_ptr6 + x0, tmp130, xmask) tl.store(out_ptr7 + x0, tmp141, xmask) @triton.jit def triton_poi_fused__softmax_leaky_relu_mul_where_3(in_out_ptr0, in_out_ptr1, in_out_ptr2, in_out_ptr3, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, in_ptr9, in_ptr10, in_ptr11, in_ptr12, 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).to(tl.int1) tmp1 = tl.load(in_ptr1 + x2, xmask).to(tl.int1) tmp2 = tl.load(in_out_ptr0 + x2, xmask) tmp8 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp13 = tl.load(in_ptr4 + x2, xmask).to(tl.int1) tmp14 = tl.load(in_out_ptr1 + x2, xmask) tmp18 = tl.load(in_ptr5 + x1, xmask, eviction_policy='evict_last') tmp21 = tl.load(in_ptr6 + x1, xmask, eviction_policy='evict_last') tmp23 = tl.load(in_ptr7 + x2, xmask).to(tl.int1) tmp24 = tl.load(in_out_ptr2 + x2, xmask) tmp28 = tl.load(in_ptr8 + x1, xmask, eviction_policy='evict_last') tmp31 = tl.load(in_ptr9 + x1, xmask, eviction_policy='evict_last') tmp33 = tl.load(in_ptr10 + x2, xmask).to(tl.int1) tmp34 = tl.load(in_out_ptr3 + x2, xmask) tmp38 = tl.load(in_ptr11 + x1, xmask, eviction_policy='evict_last') tmp41 = tl.load(in_ptr12 + x1, xmask, eviction_policy='evict_last') tmp3 = 4.0 tmp4 = tmp2 * tmp3 tmp5 = tl.where(tmp1, tmp2, tmp4) tmp6 = -8999999815811072.0 tmp7 = tl.where(tmp0, tmp5, tmp6) tmp9 = tmp7 - tmp8 tmp10 = tl_math.exp(tmp9) tmp12 = tmp10 / tmp11 tmp15 = tmp14 * tmp3 tmp16 = tl.where(tmp13, tmp14, tmp15) tmp17 = tl.where(tmp0, tmp16, tmp6) tmp19 = tmp17 - tmp18 tmp20 = tl_math.exp(tmp19) tmp22 = tmp20 / tmp21 tmp25 = tmp24 * tmp3 tmp26 = tl.where(tmp23, tmp24, tmp25) tmp27 = tl.where(tmp0, tmp26, tmp6) tmp29 = tmp27 - tmp28 tmp30 = tl_math.exp(tmp29) tmp32 = tmp30 / tmp31 tmp35 = tmp34 * tmp3 tmp36 = tl.where(tmp33, tmp34, tmp35) tmp37 = tl.where(tmp0, tmp36, tmp6) tmp39 = tmp37 - tmp38 tmp40 = tl_math.exp(tmp39) tmp42 = tmp40 / tmp41 tl.store(in_out_ptr0 + x2, tmp12, xmask) tl.store(in_out_ptr1 + x2, tmp22, xmask) tl.store(in_out_ptr2 + x2, tmp32, xmask) tl.store(in_out_ptr3 + x2, tmp42, xmask) @triton.jit def triton_poi_fused_cat_4(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 = 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 = 0.0 tmp7 = tmp5 > tmp6 tmp8 = 1.0 tmp9 = tmp5 * tmp8 tmp10 = libdevice.expm1(tmp9) tmp11 = tmp10 * tmp8 tmp12 = tl.where(tmp7, tmp9, tmp11) tmp13 = tl.full(tmp12.shape, 0.0, tmp12.dtype) tmp14 = tl.where(tmp4, tmp12, tmp13) tmp15 = tmp0 >= tmp3 tmp16 = tl.full([1], 8, tl.int64) tmp17 = tmp0 < tmp16 tmp18 = tmp15 & tmp17 tmp19 = tl.load(in_ptr1 + (4 * x1 + (-4 + x0)), tmp18 & xmask, eviction_policy='evict_last', other=0.0) tmp20 = tmp19 > tmp6 tmp21 = tmp19 * tmp8 tmp22 = libdevice.expm1(tmp21) tmp23 = tmp22 * tmp8 tmp24 = tl.where(tmp20, tmp21, tmp23) tmp25 = tl.full(tmp24.shape, 0.0, tmp24.dtype) tmp26 = tl.where(tmp18, tmp24, tmp25) tmp27 = tmp0 >= tmp16 tmp28 = tl.full([1], 12, tl.int64) tmp29 = tmp0 < tmp28 tmp30 = tmp27 & tmp29 tmp31 = tl.load(in_ptr2 + (4 * x1 + (-8 + x0)), tmp30 & xmask, eviction_policy='evict_last', other=0.0) tmp32 = tmp31 > tmp6 tmp33 = tmp31 * tmp8 tmp34 = libdevice.expm1(tmp33) tmp35 = tmp34 * tmp8 tmp36 = tl.where(tmp32, tmp33, tmp35) tmp37 = tl.full(tmp36.shape, 0.0, tmp36.dtype) tmp38 = tl.where(tmp30, tmp36, tmp37) tmp39 = tmp0 >= tmp28 tl.full([1], 16, tl.int64) tmp42 = tl.load(in_ptr3 + (4 * x1 + (-12 + x0)), tmp39 & xmask, eviction_policy='evict_last', other=0.0) tmp43 = tmp42 > tmp6 tmp44 = tmp42 * tmp8 tmp45 = libdevice.expm1(tmp44) tmp46 = tmp45 * tmp8 tmp47 = tl.where(tmp43, tmp44, tmp46) tmp48 = tl.full(tmp47.shape, 0.0, tmp47.dtype) tmp49 = tl.where(tmp39, tmp47, tmp48) tmp50 = tl.where(tmp30, tmp38, tmp49) tmp51 = tl.where(tmp18, tmp26, tmp50) tmp52 = tl.where(tmp4, tmp14, tmp51) tl.store(out_ptr0 + x2, tmp52, xmask) @triton.jit def triton_poi_fused__softmax_leaky_relu_mul_where_5(in_ptr0, in_ptr1, in_ptr2, 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').to(tl .int1) tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last').to(tl .int1) tmp2 = tl.load(in_ptr2 + 4 * x0, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp9 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp10 = tl.load(in_ptr2 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp15 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp16 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp17 = tl.load(in_ptr2 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp22 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp23 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp24 = tl.load(in_ptr2 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp3 = 4.0 tmp4 = tmp2 * tmp3 tmp5 = tl.where(tmp1, tmp2, tmp4) tmp6 = -8999999815811072.0 tmp7 = tl.where(tmp0, tmp5, tmp6) tmp11 = tmp10 * tmp3 tmp12 = tl.where(tmp9, tmp10, tmp11) tmp13 = tl.where(tmp8, tmp12, tmp6) tmp14 = triton_helpers.maximum(tmp7, tmp13) tmp18 = tmp17 * tmp3 tmp19 = tl.where(tmp16, tmp17, tmp18) tmp20 = tl.where(tmp15, tmp19, tmp6) tmp21 = triton_helpers.maximum(tmp14, tmp20) tmp25 = tmp24 * tmp3 tmp26 = tl.where(tmp23, tmp24, tmp25) tmp27 = tl.where(tmp22, tmp26, tmp6) tmp28 = triton_helpers.maximum(tmp21, tmp27) tmp29 = tmp7 - tmp28 tmp30 = tl_math.exp(tmp29) tmp31 = tmp13 - tmp28 tmp32 = tl_math.exp(tmp31) tmp33 = tmp30 + tmp32 tmp34 = tmp20 - tmp28 tmp35 = tl_math.exp(tmp34) tmp36 = tmp33 + tmp35 tmp37 = tmp27 - tmp28 tmp38 = tl_math.exp(tmp37) tmp39 = tmp36 + tmp38 tl.store(out_ptr0 + x0, tmp28, xmask) tl.store(out_ptr1 + x0, tmp39, xmask) @triton.jit def triton_poi_fused__softmax_leaky_relu_mul_where_6(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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).to(tl.int1) tmp1 = tl.load(in_ptr1 + x2, xmask).to(tl.int1) tmp2 = tl.load(in_out_ptr0 + x2, xmask) tmp8 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp3 = 4.0 tmp4 = tmp2 * tmp3 tmp5 = tl.where(tmp1, tmp2, tmp4) tmp6 = -8999999815811072.0 tmp7 = tl.where(tmp0, tmp5, tmp6) tmp9 = tmp7 - tmp8 tmp10 = tl_math.exp(tmp9) tmp12 = tmp10 / tmp11 tl.store(in_out_ptr0 + x2, tmp12, xmask) @triton.jit def triton_poi_fused__log_softmax_elu_7(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) tmp8 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp14 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp21 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp28 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp1 = 0.0 tmp2 = tmp0 > tmp1 tmp3 = 1.0 tmp4 = tmp0 * tmp3 tmp5 = libdevice.expm1(tmp4) tmp6 = tmp5 * tmp3 tmp7 = tl.where(tmp2, tmp4, tmp6) tmp9 = tmp8 > tmp1 tmp10 = tmp8 * tmp3 tmp11 = libdevice.expm1(tmp10) tmp12 = tmp11 * tmp3 tmp13 = tl.where(tmp9, tmp10, tmp12) tmp15 = tmp14 > tmp1 tmp16 = tmp14 * tmp3 tmp17 = libdevice.expm1(tmp16) tmp18 = tmp17 * tmp3 tmp19 = tl.where(tmp15, tmp16, tmp18) tmp20 = triton_helpers.maximum(tmp13, tmp19) tmp22 = tmp21 > tmp1 tmp23 = tmp21 * tmp3 tmp24 = libdevice.expm1(tmp23) tmp25 = tmp24 * tmp3 tmp26 = tl.where(tmp22, tmp23, tmp25) tmp27 = triton_helpers.maximum(tmp20, tmp26) tmp29 = tmp28 > tmp1 tmp30 = tmp28 * tmp3 tmp31 = libdevice.expm1(tmp30) tmp32 = tmp31 * tmp3 tmp33 = tl.where(tmp29, tmp30, tmp32) tmp34 = triton_helpers.maximum(tmp27, tmp33) tmp35 = tmp7 - tmp34 tl.store(out_ptr0 + x2, tmp35, xmask) @triton.jit def triton_poi_fused__log_softmax_8(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 = tl_math.exp(tmp1) tmp4 = tl_math.exp(tmp3) tmp5 = tmp2 + tmp4 tmp7 = tl_math.exp(tmp6) tmp8 = tmp5 + tmp7 tmp10 = tl_math.exp(tmp9) tmp11 = tmp8 + tmp10 tmp12 = tl_math.log(tmp11) tmp13 = tmp0 - tmp12 tl.store(out_ptr0 + x2, tmp13, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13) = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (8, 1), (1, 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, 1), (1, 1)) assert_size_stride(primals_8, (4, 4), (4, 1)) assert_size_stride(primals_9, (8, 1), (1, 1)) assert_size_stride(primals_10, (4, 4), (4, 1)) assert_size_stride(primals_11, (8, 1), (1, 1)) assert_size_stride(primals_12, (16, 4), (4, 1)) assert_size_stride(primals_13, (8, 1), (1, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_2, primals_1, out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf0, primals_3, out=buf1) buf2 = empty_strided_cuda((16, 8), (8, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(128)](buf1, buf2, 128, XBLOCK=128, num_warps=4, num_stages=1) buf3 = empty_strided_cuda((16, 1), (1, 1), torch.float32) extern_kernels.mm(buf2, primals_4, out=buf3) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.bool) triton_poi_fused_leaky_relu_1[grid(16)](buf3, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.bool) triton_poi_fused_leaky_relu_1[grid(16)](primals_5, buf5, 16, XBLOCK =16, num_warps=1, num_stages=1) del primals_5 buf10 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf0, primals_6, out=buf10) buf11 = empty_strided_cuda((16, 8), (8, 1), torch.float32) triton_poi_fused_cat_0[grid(128)](buf10, buf11, 128, XBLOCK=128, num_warps=4, num_stages=1) buf12 = empty_strided_cuda((16, 1), (1, 1), torch.float32) extern_kernels.mm(buf11, primals_7, out=buf12) buf13 = empty_strided_cuda((4, 4), (4, 1), torch.bool) triton_poi_fused_leaky_relu_1[grid(16)](buf12, buf13, 16, XBLOCK=16, num_warps=1, num_stages=1) buf18 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf0, primals_8, out=buf18) buf19 = empty_strided_cuda((16, 8), (8, 1), torch.float32) triton_poi_fused_cat_0[grid(128)](buf18, buf19, 128, XBLOCK=128, num_warps=4, num_stages=1) buf20 = empty_strided_cuda((16, 1), (1, 1), torch.float32) extern_kernels.mm(buf19, primals_9, out=buf20) buf21 = empty_strided_cuda((4, 4), (4, 1), torch.bool) triton_poi_fused_leaky_relu_1[grid(16)](buf20, buf21, 16, XBLOCK=16, num_warps=1, num_stages=1) buf26 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf0, primals_10, out=buf26) buf27 = empty_strided_cuda((16, 8), (8, 1), torch.float32) triton_poi_fused_cat_0[grid(128)](buf26, buf27, 128, XBLOCK=128, num_warps=4, num_stages=1) buf28 = empty_strided_cuda((16, 1), (1, 1), torch.float32) extern_kernels.mm(buf27, primals_11, out=buf28) buf29 = empty_strided_cuda((4, 4), (4, 1), torch.bool) triton_poi_fused_leaky_relu_1[grid(16)](buf28, buf29, 16, XBLOCK=16, num_warps=1, num_stages=1) buf6 = empty_strided_cuda((4, 1), (1, 4), torch.float32) buf7 = empty_strided_cuda((4, 1), (1, 4), torch.float32) buf14 = empty_strided_cuda((4, 1), (1, 4), torch.float32) buf15 = empty_strided_cuda((4, 1), (1, 4), torch.float32) buf22 = empty_strided_cuda((4, 1), (1, 4), torch.float32) buf23 = empty_strided_cuda((4, 1), (1, 4), torch.float32) buf30 = empty_strided_cuda((4, 1), (1, 4), torch.float32) buf31 = empty_strided_cuda((4, 1), (1, 4), torch.float32) triton_poi_fused__softmax_leaky_relu_mul_where_2[grid(4)](buf5, buf4, buf3, buf13, buf12, buf21, buf20, buf29, buf28, buf6, buf7, buf14, buf15, buf22, buf23, buf30, buf31, 4, XBLOCK=4, num_warps=1, num_stages=1) buf8 = reinterpret_tensor(buf3, (4, 4), (4, 1), 0) del buf3 buf16 = reinterpret_tensor(buf12, (4, 4), (4, 1), 0) del buf12 buf24 = reinterpret_tensor(buf20, (4, 4), (4, 1), 0) del buf20 buf32 = reinterpret_tensor(buf28, (4, 4), (4, 1), 0) del buf28 triton_poi_fused__softmax_leaky_relu_mul_where_3[grid(16)](buf8, buf16, buf24, buf32, buf5, buf4, buf6, buf7, buf13, buf14, buf15, buf21, buf22, buf23, buf29, buf30, buf31, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf14 del buf15 del buf22 del buf23 del buf30 del buf31 buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf8, buf1, out=buf9) buf17 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf16, buf10, out=buf17) buf25 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf24, buf18, out=buf25) buf33 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf32, buf26, out=buf33) buf34 = empty_strided_cuda((4, 16), (16, 1), torch.float32) triton_poi_fused_cat_4[grid(64)](buf9, buf17, buf25, buf33, buf34, 64, XBLOCK=64, num_warps=1, num_stages=1) buf35 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf34, primals_12, out=buf35) buf36 = empty_strided_cuda((16, 8), (8, 1), torch.float32) triton_poi_fused_cat_0[grid(128)](buf35, buf36, 128, XBLOCK=128, num_warps=4, num_stages=1) buf37 = empty_strided_cuda((16, 1), (1, 1), torch.float32) extern_kernels.mm(buf36, primals_13, out=buf37) buf38 = empty_strided_cuda((4, 4), (4, 1), torch.bool) triton_poi_fused_leaky_relu_1[grid(16)](buf37, buf38, 16, XBLOCK=16, num_warps=1, num_stages=1) buf39 = buf7 del buf7 buf40 = buf6 del buf6 triton_poi_fused__softmax_leaky_relu_mul_where_5[grid(4)](buf5, buf38, buf37, buf39, buf40, 4, XBLOCK=4, num_warps=1, num_stages=1) buf41 = reinterpret_tensor(buf37, (4, 4), (4, 1), 0) del buf37 triton_poi_fused__softmax_leaky_relu_mul_where_6[grid(16)](buf41, buf5, buf38, buf39, buf40, 16, XBLOCK=16, num_warps=1, num_stages=1 ) del buf39 del buf40 buf42 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf41, buf35, out=buf42) buf43 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused__log_softmax_elu_7[grid(16)](buf42, buf43, 16, XBLOCK=16, num_warps=1, num_stages=1) buf44 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused__log_softmax_8[grid(16)](buf43, buf44, 16, XBLOCK= 16, num_warps=1, num_stages=1) del buf43 return (buf44, buf4, buf5, buf8, buf9, buf13, buf16, buf17, buf21, buf24, buf25, buf29, buf32, buf33, buf38, buf41, buf42, buf44, reinterpret_tensor(buf35, (4, 4), (1, 4), 0), reinterpret_tensor( buf36, (8, 16), (1, 8), 0), reinterpret_tensor(primals_13, (1, 8), (1, 1), 0), reinterpret_tensor(buf34, (16, 4), (1, 16), 0), reinterpret_tensor(primals_12, (4, 16), (1, 4), 0), reinterpret_tensor(buf26, (4, 4), (1, 4), 0), reinterpret_tensor( buf27, (8, 16), (1, 8), 0), reinterpret_tensor(primals_11, (1, 8), (1, 1), 0), reinterpret_tensor(buf0, (4, 4), (1, 4), 0), reinterpret_tensor(primals_10, (4, 4), (1, 4), 0), reinterpret_tensor(buf18, (4, 4), (1, 4), 0), reinterpret_tensor( buf19, (8, 16), (1, 8), 0), reinterpret_tensor(primals_9, (1, 8), ( 1, 1), 0), reinterpret_tensor(primals_8, (4, 4), (1, 4), 0), reinterpret_tensor(buf10, (4, 4), (1, 4), 0), reinterpret_tensor( buf11, (8, 16), (1, 8), 0), reinterpret_tensor(primals_7, (1, 8), ( 1, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), reinterpret_tensor(buf1, (4, 4), (1, 4), 0), reinterpret_tensor( buf2, (8, 16), (1, 8), 0), reinterpret_tensor(primals_4, (1, 8), (1, 1), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0)) class GraphAttentionLayer(nn.Module): """ Simple GAT layer, similar to https://arxiv.org/abs/1710.10903 """ def __init__(self, in_features, out_features, dropout, alpha, concat=True): super(GraphAttentionLayer, self).__init__() self.dropout = dropout self.in_features = in_features self.out_features = out_features self.alpha = alpha self.concat = concat self.W = nn.Parameter(torch.zeros(size=(in_features, out_features))) nn.init.xavier_uniform_(self.W.data, gain=1.414) self.a = nn.Parameter(torch.zeros(size=(2 * out_features, 1))) nn.init.xavier_uniform_(self.a.data, gain=1.414) self.leakyrelu = nn.LeakyReLU(self.alpha) def forward(self, input, adj): h = torch.mm(input, self.W) """u = torch.mm(input, self.U) v = torch.mm(input, self.V) h = torch.mm(torch.sigmoid(u) * torch.sigmoid(v), self.P)""" N = h.size()[0] a_input = torch.cat([h.repeat(1, N).view(N * N, -1), h.repeat(N, 1) ], dim=1).view(N, -1, 2 * self.out_features) e = self.leakyrelu(torch.matmul(a_input, self.a).squeeze(2)) zero_vec = -9000000000000000.0 * torch.ones_like(e) attention = torch.where(adj > 0, e, zero_vec) attention = F.softmax(attention, dim=1) attention = F.dropout(attention, self.dropout, training=self.training) h_prime = torch.matmul(attention, h) if self.concat: return F.elu(h_prime) else: return h_prime def __repr__(self): return self.__class__.__name__ + ' (' + str(self.in_features ) + ' -> ' + str(self.out_features) + ')' class GATNew(nn.Module): def __init__(self, nfeat, nhid, nclass, dropout, alpha, nheads): """Dense version of GAT.""" super(GATNew, self).__init__() self.dropout = dropout self.encoder = nn.Parameter(torch.zeros(size=(nfeat, nhid))) nn.init.xavier_uniform_(self.encoder.data, gain=1.414) self.attentions = [GraphAttentionLayer(nhid, nhid, dropout=dropout, alpha=alpha, concat=True) for _ in range(nheads)] for i, attention in enumerate(self.attentions): self.add_module('attention_{}'.format(i), attention) self.out_att = GraphAttentionLayer(nhid * nheads, nclass, dropout= dropout, alpha=alpha, concat=False) def forward(self, input_0, input_1): primals_1 = self.encoder primals_2 = self.attention_0.W primals_4 = self.attention_0.a primals_3 = self.attention_1.W primals_7 = self.attention_1.a primals_5 = self.attention_2.W primals_9 = self.attention_2.a primals_6 = self.attention_3.W primals_11 = self.attention_3.a primals_12 = self.out_att.W primals_13 = self.out_att.a primals_8 = input_0 primals_10 = 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]) return output[0]
thilinicooray/pyGAT
GAT
false
13,051
[ "MIT" ]
0
0c8fd0fdae20e42a41116cc9691e1223fd9d0a93
https://github.com/thilinicooray/pyGAT/tree/0c8fd0fdae20e42a41116cc9691e1223fd9d0a93
BinaryFocalLoss
import torch class BinaryFocalLoss(torch.nn.Module): """ from https://github.com/qubvel/segmentation_models""" def __init__(self, gamma=2.0, alpha=0.25, eps=1e-07): super().__init__() self.gamma = gamma self.alpha = alpha self.eps = eps def forward(self, pr, gt): pr = torch.clamp(pr, self.eps, 1 - self.eps) loss_1 = -gt * (self.alpha * torch.pow(1 - pr, self.gamma) ) * torch.log(pr) loss_0 = -(1 - gt) * ((1 - self.alpha) * torch.pow(pr, self.gamma) ) * torch.log(1 - pr) loss = loss_0 + loss_1 return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_clamp_log_mul_neg_pow_rsub_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp4 = tl.load(in_ptr1 + x0, xmask) tmp1 = 1.0 tmp2 = tmp1 - tmp0 tmp3 = -tmp2 tmp5 = 1e-07 tmp6 = triton_helpers.maximum(tmp4, tmp5) tmp7 = 0.9999999 tmp8 = triton_helpers.minimum(tmp6, tmp7) tmp9 = tmp8 * tmp8 tmp10 = 0.75 tmp11 = tmp9 * tmp10 tmp12 = tmp3 * tmp11 tmp13 = tmp1 - tmp8 tmp14 = tl_math.log(tmp13) tmp15 = tmp12 * tmp14 tmp16 = -tmp0 tmp17 = tmp13 * tmp13 tmp18 = 0.25 tmp19 = tmp17 * tmp18 tmp20 = tmp16 * tmp19 tmp21 = tl_math.log(tmp8) tmp22 = tmp20 * tmp21 tmp23 = tmp15 + tmp22 tl.store(out_ptr0 + x0, tmp23, 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_clamp_log_mul_neg_pow_rsub_0[grid(256)](arg1_1, arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf0, class BinaryFocalLossNew(torch.nn.Module): """ from https://github.com/qubvel/segmentation_models""" def __init__(self, gamma=2.0, alpha=0.25, eps=1e-07): super().__init__() self.gamma = gamma self.alpha = alpha self.eps = eps def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
uncharted-distil/d3m-primitives
BinaryFocalLoss
false
13,054
[ "Apache-2.0" ]
0
e8d37dbe302c0f2bae4e7f7fa241a46faebc9b79
https://github.com/uncharted-distil/d3m-primitives/tree/e8d37dbe302c0f2bae4e7f7fa241a46faebc9b79
GeM
import torch from torch import nn from torch.nn import functional as F from torchvision.transforms import * class GeM(nn.Module): def __init__(self, dim=2048, p=3, eps=1e-06): super(GeM, self).__init__() self.p = nn.Parameter(torch.ones(dim) * p, requires_grad=True) self.eps = eps self.dim = dim def forward(self, x): return self.gem(x, p=self.p, eps=self.eps) def gem(self, x, p=3, eps=1e-06): x = torch.transpose(x, 1, -1) x = x.clamp(min=eps).pow(p) x = torch.transpose(x, 1, -1) x = F.avg_pool2d(x, (x.size(-2), x.size(-1))) x = x.view(x.size(0), x.size(1)) x = x.pow(1.0 / p) return x def __repr__(self): return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self. p.data.tolist()[0]) + ', ' + 'eps=' + str(self.eps ) + ',' + 'dim=' + str(self.dim) + ')' def get_inputs(): return [torch.rand([4, 2048, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math from torch import nn from torch.nn import functional as F from torchvision.transforms 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_clamp_pow_0(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 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] tl.full([XBLOCK, YBLOCK], True, tl.int1) x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 8192 * y1), ymask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x2, None, eviction_policy='evict_last') tmp1 = 1e-06 tmp2 = triton_helpers.maximum(tmp0, tmp1) tmp4 = libdevice.pow(tmp2, tmp3) tl.store(out_ptr0 + (x2 + 2048 * y3), tmp4, ymask) @triton.jit def triton_poi_fused_clamp_eq_ge_log_logical_and_mul_pow_transpose_where_zeros_1( in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 2048 y1 = yindex // 2048 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 2048 * x2 + 8192 * y1), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + (x2 + 4 * y3), xmask, eviction_policy='evict_last' ) tmp6 = tl.load(in_ptr2 + y0, None, eviction_policy='evict_last') tmp2 = 1e-06 tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp4 = 0.0 tmp5 = tmp3 == tmp4 tmp7 = tmp6 >= tmp4 tmp8 = tmp5 & tmp7 tmp9 = tl_math.log(tmp3) tmp10 = tmp0 * tmp9 tmp11 = tl.where(tmp8, tmp4, tmp10) tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask) tl.store(out_ptr1 + (x2 + 4 * y3), tmp11, xmask) @triton.jit def triton_poi_fused_mul_pow_reciprocal_2(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) x1 = xindex // 2048 x0 = xindex % 2048 x2 = xindex tmp0 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x0, None, eviction_policy='evict_last') tmp2 = tl.full([1], 1, tl.int32) tmp3 = tmp2 / tmp1 tmp4 = 1.0 tmp5 = tmp3 * tmp4 tmp6 = libdevice.pow(tmp0, tmp5) tl.store(out_ptr0 + x2, tmp6, None) def call(args): primals_1, primals_2 = args args.clear() assert_size_stride(primals_1, (2048,), (1,)) assert_size_stride(primals_2, (4, 2048, 4), (8192, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 2048), (8192, 2048, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clamp_pow_0[grid(16, 2048)](primals_2, primals_1, buf0, 16, 2048, XBLOCK=256, YBLOCK=1, num_warps=4, num_stages=1) buf1 = empty_strided_cuda((4, 2048, 4), (8192, 4, 1), torch.float32) buf5 = empty_strided_cuda((4, 4, 2048), (8192, 1, 4), torch.float32) triton_poi_fused_clamp_eq_ge_log_logical_and_mul_pow_transpose_where_zeros_1[ grid(8192, 4)](buf0, primals_2, primals_1, buf1, buf5, 8192, 4, XBLOCK=1, YBLOCK=256, num_warps=4, num_stages=1) del buf0 del primals_2 buf2 = torch.ops.aten.avg_pool2d.default(buf1, [2048, 4], [2048, 4], [0, 0], False, True, None) buf3 = buf2 del buf2 buf4 = empty_strided_cuda((4, 2048), (2048, 1), torch.float32) triton_poi_fused_mul_pow_reciprocal_2[grid(8192)](buf3, primals_1, buf4, 8192, XBLOCK=128, num_warps=4, num_stages=1) return buf4, primals_1, buf1, buf3, buf4, buf5 class GeMNew(nn.Module): def __init__(self, dim=2048, p=3, eps=1e-06): super(GeMNew, self).__init__() self.p = nn.Parameter(torch.ones(dim) * p, requires_grad=True) self.eps = eps self.dim = dim def gem(self, x, p=3, eps=1e-06): x = torch.transpose(x, 1, -1) x = x.clamp(min=eps).pow(p) x = torch.transpose(x, 1, -1) x = F.avg_pool2d(x, (x.size(-2), x.size(-1))) x = x.view(x.size(0), x.size(1)) x = x.pow(1.0 / p) return x def __repr__(self): return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self. p.data.tolist()[0]) + ', ' + 'eps=' + str(self.eps ) + ',' + 'dim=' + str(self.dim) + ')' def forward(self, input_0): primals_1 = self.p primals_2 = input_0 output = call([primals_1, primals_2]) return output[0]
uestcwcw/University1652-Baseline
GeM
false
13,056
[ "MIT" ]
0
fda1e4773fc911cbb43a9b96901d436298dc1284
https://github.com/uestcwcw/University1652-Baseline/tree/fda1e4773fc911cbb43a9b96901d436298dc1284
CrossEntropy
import torch from torch import nn import torch.nn.functional as F def cross_entropy(y, target, mask=None): if target.ndim == 1: loss = F.cross_entropy(y, target, reduction='none') else: loss = -(target * F.log_softmax(y, 1)).sum(1) if mask is not None: loss = mask * loss return loss.mean() class CrossEntropy(nn.Module): def forward(self, y, target, mask=None, *args, **kwargs): return cross_entropy(y, target.detach(), mask) 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 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__log_softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tl.store(out_ptr0 + x3, tmp8, xmask) @triton.jit def triton_per_fused__log_softmax_mean_mul_neg_sum_1(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 64 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex % 16 r1 = rindex // 16 tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None) tmp1 = tl.load(in_ptr1 + (r0 + 64 * r1), None) tmp3 = tl.load(in_ptr1 + (16 + r0 + 64 * r1), None) tmp6 = tl.load(in_ptr1 + (32 + r0 + 64 * r1), None) tmp9 = tl.load(in_ptr1 + (48 + r0 + 64 * r1), None) tmp15 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None) tmp19 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None) tmp23 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), 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 = tmp1 - tmp12 tmp14 = tmp0 * tmp13 tmp16 = tmp3 - tmp12 tmp17 = tmp15 * tmp16 tmp18 = tmp14 + tmp17 tmp20 = tmp6 - tmp12 tmp21 = tmp19 * tmp20 tmp22 = tmp18 + tmp21 tmp24 = tmp9 - tmp12 tmp25 = tmp23 * tmp24 tmp26 = tmp22 + tmp25 tmp27 = -tmp26 tmp28 = tl.broadcast_to(tmp27, [XBLOCK, RBLOCK]) tmp30 = tl.sum(tmp28, 1)[:, None] tmp31 = 64.0 tmp32 = tmp30 / tmp31 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp32, 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__log_softmax_0[grid(256)](arg1_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg1_1 buf1 = empty_strided_cuda((), (), torch.float32) buf2 = buf1 del buf1 triton_per_fused__log_softmax_mean_mul_neg_sum_1[grid(1)](buf2, arg0_1, buf0, 1, 64, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del buf0 return buf2, def cross_entropy(y, target, mask=None): if target.ndim == 1: loss = F.cross_entropy(y, target, reduction='none') else: loss = -(target * F.log_softmax(y, 1)).sum(1) if mask is not None: loss = mask * loss return loss.mean() class CrossEntropyNew(nn.Module): def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
uncharted-distil/d3m-primitives
CrossEntropy
false
13,057
[ "Apache-2.0" ]
0
e8d37dbe302c0f2bae4e7f7fa241a46faebc9b79
https://github.com/uncharted-distil/d3m-primitives/tree/e8d37dbe302c0f2bae4e7f7fa241a46faebc9b79
CircleLoss
import torch from torch import Tensor from torch import nn from torchvision.transforms import * class CircleLoss(nn.Module): def __init__(self, m: 'float', gamma: 'float') ->None: super(CircleLoss, self).__init__() self.m = m self.gamma = gamma self.soft_plus = nn.Softplus() def forward(self, sp: 'Tensor', sn: 'Tensor') ->Tensor: ap = torch.clamp_min(-sp.detach() + 1 + self.m, min=0.0) an = torch.clamp_min(sn.detach() + self.m, min=0.0) delta_p = 1 - self.m delta_n = self.m logit_p = -ap * (sp - delta_p) * self.gamma logit_n = an * (sn - delta_n) * self.gamma loss = self.soft_plus(torch.logsumexp(logit_n, dim=0) + torch. logsumexp(logit_p, dim=0)) return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'m': 4, 'gamma': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math from torch import nn from torchvision.transforms 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_clamp_min_logsumexp_mul_neg_softplus_sub_0(in_out_ptr0 , in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp8 = tl.load(in_ptr0 + (64 + x0), xmask) tmp15 = tl.load(in_ptr0 + (128 + x0), xmask) tmp22 = tl.load(in_ptr0 + (192 + x0), xmask) tmp44 = tl.load(in_ptr1 + x0, xmask) tmp55 = tl.load(in_ptr1 + (64 + x0), xmask) tmp65 = tl.load(in_ptr1 + (128 + x0), xmask) tmp75 = tl.load(in_ptr1 + (192 + x0), xmask) tmp1 = 4.0 tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = tmp0 - tmp1 tmp6 = tmp4 * tmp5 tmp7 = tmp6 * tmp1 tmp9 = tmp8 + tmp1 tmp10 = triton_helpers.maximum(tmp9, tmp3) tmp11 = tmp8 - tmp1 tmp12 = tmp10 * tmp11 tmp13 = tmp12 * tmp1 tmp14 = triton_helpers.maximum(tmp7, tmp13) tmp16 = tmp15 + tmp1 tmp17 = triton_helpers.maximum(tmp16, tmp3) tmp18 = tmp15 - tmp1 tmp19 = tmp17 * tmp18 tmp20 = tmp19 * tmp1 tmp21 = triton_helpers.maximum(tmp14, tmp20) tmp23 = tmp22 + tmp1 tmp24 = triton_helpers.maximum(tmp23, tmp3) tmp25 = tmp22 - tmp1 tmp26 = tmp24 * tmp25 tmp27 = tmp26 * tmp1 tmp28 = triton_helpers.maximum(tmp21, tmp27) tmp29 = tl_math.abs(tmp28) tmp30 = float('inf') tmp31 = tmp29 == tmp30 tmp32 = tl.where(tmp31, tmp3, tmp28) tmp33 = tmp7 - tmp32 tmp34 = tl_math.exp(tmp33) tmp35 = tmp13 - tmp32 tmp36 = tl_math.exp(tmp35) tmp37 = tmp34 + tmp36 tmp38 = tmp20 - tmp32 tmp39 = tl_math.exp(tmp38) tmp40 = tmp37 + tmp39 tmp41 = tmp27 - tmp32 tmp42 = tl_math.exp(tmp41) tmp43 = tmp40 + tmp42 tmp45 = -tmp44 tmp46 = 1.0 tmp47 = tmp45 + tmp46 tmp48 = tmp47 + tmp1 tmp49 = triton_helpers.maximum(tmp48, tmp3) tmp50 = -tmp49 tmp51 = -3.0 tmp52 = tmp44 - tmp51 tmp53 = tmp50 * tmp52 tmp54 = tmp53 * tmp1 tmp56 = -tmp55 tmp57 = tmp56 + tmp46 tmp58 = tmp57 + tmp1 tmp59 = triton_helpers.maximum(tmp58, tmp3) tmp60 = -tmp59 tmp61 = tmp55 - tmp51 tmp62 = tmp60 * tmp61 tmp63 = tmp62 * tmp1 tmp64 = triton_helpers.maximum(tmp54, tmp63) tmp66 = -tmp65 tmp67 = tmp66 + tmp46 tmp68 = tmp67 + tmp1 tmp69 = triton_helpers.maximum(tmp68, tmp3) tmp70 = -tmp69 tmp71 = tmp65 - tmp51 tmp72 = tmp70 * tmp71 tmp73 = tmp72 * tmp1 tmp74 = triton_helpers.maximum(tmp64, tmp73) tmp76 = -tmp75 tmp77 = tmp76 + tmp46 tmp78 = tmp77 + tmp1 tmp79 = triton_helpers.maximum(tmp78, tmp3) tmp80 = -tmp79 tmp81 = tmp75 - tmp51 tmp82 = tmp80 * tmp81 tmp83 = tmp82 * tmp1 tmp84 = triton_helpers.maximum(tmp74, tmp83) tmp85 = tl_math.abs(tmp84) tmp86 = tmp85 == tmp30 tmp87 = tl.where(tmp86, tmp3, tmp84) tmp88 = tmp54 - tmp87 tmp89 = tl_math.exp(tmp88) tmp90 = tmp63 - tmp87 tmp91 = tl_math.exp(tmp90) tmp92 = tmp89 + tmp91 tmp93 = tmp73 - tmp87 tmp94 = tl_math.exp(tmp93) tmp95 = tmp92 + tmp94 tmp96 = tmp83 - tmp87 tmp97 = tl_math.exp(tmp96) tmp98 = tmp95 + tmp97 tmp99 = tl_math.log(tmp43) tmp100 = tmp99 + tmp32 tmp101 = tl_math.log(tmp98) tmp102 = tmp101 + tmp87 tmp103 = tmp100 + tmp102 tmp104 = tmp103 * tmp46 tmp105 = 20.0 tmp106 = tmp104 > tmp105 tmp107 = tl_math.exp(tmp104) tmp108 = libdevice.log1p(tmp107) tmp109 = tmp108 * tmp46 tmp110 = tl.where(tmp106, tmp103, tmp109) tl.store(in_out_ptr0 + x0, tmp110, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) buf5 = buf2 del buf2 get_raw_stream(0) triton_poi_fused_add_clamp_min_logsumexp_mul_neg_softplus_sub_0[grid (64)](buf5, arg1_1, arg0_1, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 del arg1_1 return buf5, class CircleLossNew(nn.Module): def __init__(self, m: 'float', gamma: 'float') ->None: super(CircleLossNew, self).__init__() self.m = m self.gamma = gamma self.soft_plus = nn.Softplus() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
uestcwcw/University1652-Baseline
CircleLoss
false
13,058
[ "MIT" ]
0
fda1e4773fc911cbb43a9b96901d436298dc1284
https://github.com/uestcwcw/University1652-Baseline/tree/fda1e4773fc911cbb43a9b96901d436298dc1284
Image2Patch
import torch import torch.nn as nn import torch.nn.functional as F class Image2Patch(nn.Module): """Some Information about Image2Patch""" def __init__(self, channels, image_size, patch_size): super(Image2Patch, self).__init__() if type(patch_size) == int: patch_size = [patch_size, patch_size] self.patch_size = patch_size if type(image_size) == int: image_size = [image_size, image_size] self.image_size = image_size self.channels = channels self.num_patch = [image_size[0] // patch_size[0], image_size[1] // patch_size[1]] def forward(self, x): x = F.unfold(x, kernel_size=self.patch_size, stride=self.patch_size) x = x.swapaxes(1, 2) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'channels': 4, 'image_size': 4, 'patch_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_im2col_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 tmp0 = tl.load(in_ptr0 + x3, xmask) tl.store(out_ptr0 + x3, 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, 1, 4, 1), (64, 16, 4, 4, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_im2col_0[grid(256)](arg0_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return reinterpret_tensor(buf0, (4, 1, 64), (64, 4, 1), 0), class Image2PatchNew(nn.Module): """Some Information about Image2Patch""" def __init__(self, channels, image_size, patch_size): super(Image2PatchNew, self).__init__() if type(patch_size) == int: patch_size = [patch_size, patch_size] self.patch_size = patch_size if type(image_size) == int: image_size = [image_size, image_size] self.image_size = image_size self.channels = channels self.num_patch = [image_size[0] // patch_size[0], image_size[1] // patch_size[1]] def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
uthree/ReMixer
Image2Patch
false
13,059
[ "MIT" ]
0
587e1b6a01850df649eccf043689f84a7dd5e2dc
https://github.com/uthree/ReMixer/tree/587e1b6a01850df649eccf043689f84a7dd5e2dc
CEFL
import torch import torch.nn as nn import torch.nn.functional as F import torch.onnx class CEFL(nn.Module): def __init__(self, gamma=1): super(CEFL, self).__init__() self.gamma = gamma def get_prob(self, input, target): prob = F.softmax(input, dim=-1) prob = prob[range(target.shape[0]), target] return prob def get_attention(self, input, target): prob = self.get_prob(input, target) prob = 1 - prob prob = prob ** self.gamma return prob def get_celoss(self, input, target): ce_loss = F.log_softmax(input, dim=1) ce_loss = -ce_loss[range(target.shape[0]), target] return ce_loss def forward(self, input, target): attn = self.get_attention(input, target) ce_loss = self.get_celoss(input, target) prob = self.get_prob(input, target) loss = (1 - prob) * ce_loss + prob * attn * ce_loss return loss.mean() def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.ones([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 import torch.nn.functional as F import torch.onnx assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused__log_softmax__softmax_0(in_ptr0, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x5 = xindex x1 = xindex // 4 x2 = xindex % 16 x4 = xindex // 64 tmp0 = tl.load(in_ptr0 + x5, 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 + (x2 + 64 * x4), xmask, eviction_policy= 'evict_last') tmp11 = tl.load(in_ptr0 + (16 + x2 + 64 * x4), xmask, eviction_policy= 'evict_last') tmp13 = tl.load(in_ptr0 + (32 + x2 + 64 * x4), xmask, eviction_policy= 'evict_last') tmp15 = tl.load(in_ptr0 + (48 + x2 + 64 * x4), 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 tl.store(out_ptr0 + x5, tmp9, xmask) tl.store(out_ptr1 + x5, tmp9, xmask) tl.store(out_ptr2 + x5, tmp17, xmask) @triton.jit def triton_per_fused__log_softmax__softmax_add_index_mean_mul_neg_rsub_1( in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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) r2 = rindex // 16 r3 = rindex % 16 r1 = rindex // 4 % 4 tmp11 = tl.load(in_ptr0 + r2, None, eviction_policy='evict_last') tmp0 = r2 tmp1 = tl.full([1, 1], 2, tl.int64) tmp2 = tmp0 < tmp1 tmp3 = tl.full([1, 1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.full([1, 1], 0, tl.int64) tmp6 = tl.where(tmp4, tmp5, tmp3) tmp7 = tl.full([1, 1], 3, tl.int64) tmp8 = tmp0 < tmp7 tmp9 = tl.where(tmp8, tmp1, tmp7) tmp10 = tl.where(tmp2, tmp6, tmp9) tmp12 = tl.full([XBLOCK, RBLOCK], 4, tl.int32) tmp13 = tmp11 + tmp12 tmp14 = tmp11 < 0 tmp15 = tl.where(tmp14, tmp13, tmp11) tl.device_assert((0 <= tmp15) & (tmp15 < 4), 'index out of bounds: 0 <= tmp15 < 4') tmp17 = tl.load(in_ptr1 + (r3 + 16 * tmp15 + 64 * tmp10), None) tmp18 = tl.load(in_ptr1 + (4 * r1 + 16 * tmp15 + 64 * tmp10), None, eviction_policy='evict_last') tmp19 = tl.load(in_ptr1 + (1 + 4 * r1 + 16 * tmp15 + 64 * tmp10), None, eviction_policy='evict_last') tmp20 = tmp18 + tmp19 tmp21 = tl.load(in_ptr1 + (2 + 4 * r1 + 16 * tmp15 + 64 * tmp10), None, eviction_policy='evict_last') tmp22 = tmp20 + tmp21 tmp23 = tl.load(in_ptr1 + (3 + 4 * r1 + 16 * tmp15 + 64 * tmp10), None, eviction_policy='evict_last') tmp24 = tmp22 + tmp23 tmp25 = tmp17 / tmp24 tmp26 = tl.load(in_ptr2 + (r3 + 16 * tmp15 + 64 * tmp10), None) tmp27 = tl.load(in_ptr2 + (r3 + 64 * tmp10), None) tmp28 = tl_math.exp(tmp27) tmp29 = tl.load(in_ptr2 + (16 + r3 + 64 * tmp10), None) tmp30 = tl_math.exp(tmp29) tmp31 = tmp28 + tmp30 tmp32 = tl.load(in_ptr2 + (32 + r3 + 64 * tmp10), None) tmp33 = tl_math.exp(tmp32) tmp34 = tmp31 + tmp33 tmp35 = tl.load(in_ptr2 + (48 + r3 + 64 * tmp10), None) tmp36 = tl_math.exp(tmp35) tmp37 = tmp34 + tmp36 tmp38 = tl_math.log(tmp37) tmp39 = tmp26 - tmp38 tmp40 = -tmp39 tmp41 = 1.0 tmp42 = tmp41 - tmp25 tmp43 = tmp42 * tmp40 tmp44 = tl.load(in_ptr3 + (r3 + 16 * tmp15 + 64 * tmp10), None) tmp45 = tl.load(in_ptr3 + (4 * r1 + 16 * tmp15 + 64 * tmp10), None, eviction_policy='evict_last') tmp46 = tl.load(in_ptr3 + (1 + 4 * r1 + 16 * tmp15 + 64 * tmp10), None, eviction_policy='evict_last') tmp47 = tmp45 + tmp46 tmp48 = tl.load(in_ptr3 + (2 + 4 * r1 + 16 * tmp15 + 64 * tmp10), None, eviction_policy='evict_last') tmp49 = tmp47 + tmp48 tmp50 = tl.load(in_ptr3 + (3 + 4 * r1 + 16 * tmp15 + 64 * tmp10), None, eviction_policy='evict_last') tmp51 = tmp49 + tmp50 tmp52 = tmp44 / tmp51 tmp53 = tmp41 - tmp52 tmp54 = tmp25 * tmp53 tmp55 = tmp54 * tmp40 tmp56 = tmp43 + tmp55 tmp57 = tl.broadcast_to(tmp56, [XBLOCK, RBLOCK]) tmp59 = tl.sum(tmp57, 1)[:, None] tmp60 = 64.0 tmp61 = tmp59 / tmp60 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp61, 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,), (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) buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused__log_softmax__softmax_0[grid(256)](arg0_1, buf0, buf4, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 buf5 = empty_strided_cuda((), (), torch.float32) buf6 = buf5 del buf5 triton_per_fused__log_softmax__softmax_add_index_mean_mul_neg_rsub_1[ grid(1)](buf6, arg1_1, buf0, buf2, buf4, 1, 64, XBLOCK=1, num_warps=2, num_stages=1) del arg1_1 del buf0 del buf2 del buf4 return buf6, class CEFLNew(nn.Module): def __init__(self, gamma=1): super(CEFLNew, self).__init__() self.gamma = gamma def get_prob(self, input, target): prob = F.softmax(input, dim=-1) prob = prob[range(target.shape[0]), target] return prob def get_attention(self, input, target): prob = self.get_prob(input, target) prob = 1 - prob prob = prob ** self.gamma return prob def get_celoss(self, input, target): ce_loss = F.log_softmax(input, dim=1) ce_loss = -ce_loss[range(target.shape[0]), target] return ce_loss def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
umairjavaid/staff-employee-classification
CEFL
false
13,060
[ "MIT" ]
0
fc5fe32acfbde2b188094df90d888eeb0f4f4acd
https://github.com/umairjavaid/staff-employee-classification/tree/fc5fe32acfbde2b188094df90d888eeb0f4f4acd
FocalLoss
import torch import torch.nn as nn import torch.nn.functional as F import torch.onnx class FocalLoss(nn.Module): def __init__(self, alpha=1, gamma=0): super(FocalLoss, self).__init__() self.gamma = gamma self.alpha = alpha def get_attention(self, input, target): prob = F.softmax(input, dim=-1) prob = prob[range(target.shape[0]), target] prob = 1 - prob prob = prob ** self.gamma return prob def get_celoss(self, input, target): ce_loss = F.log_softmax(input, dim=1) ce_loss = -ce_loss[range(target.shape[0]), target] return ce_loss def forward(self, input, target): attn = self.get_attention(input, target) ce_loss = self.get_celoss(input, target) loss = self.alpha * ce_loss * attn return loss.mean() def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.ones([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 import torch.nn.functional as F import torch.onnx assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused__log_softmax__softmax_0(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x5 = xindex x0 = xindex % 16 x2 = xindex // 64 x4 = xindex // 4 tmp0 = tl.load(in_ptr0 + x5, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr0 + 4 * x4, xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr0 + (1 + 4 * x4), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr0 + (2 + 4 * x4), xmask, eviction_policy='evict_last' ) tmp14 = tl.load(in_ptr0 + (3 + 4 * x4), 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 tmp11 = triton_helpers.maximum(tmp9, tmp10) tmp13 = triton_helpers.maximum(tmp11, tmp12) tmp15 = triton_helpers.maximum(tmp13, tmp14) tmp16 = tmp0 - tmp15 tmp17 = tl_math.exp(tmp16) tl.store(out_ptr0 + x5, tmp8, xmask) tl.store(out_ptr1 + x5, tmp17, xmask) @triton.jit def triton_per_fused__log_softmax__softmax_index_mean_mul_neg_pow_rsub_1( in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, 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) r2 = rindex // 16 r4 = rindex % 16 r1 = rindex // 4 % 4 tmp11 = tl.load(in_ptr0 + r2, None, eviction_policy='evict_last') tmp0 = r2 tmp1 = tl.full([1, 1], 2, tl.int64) tmp2 = tmp0 < tmp1 tmp3 = tl.full([1, 1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.full([1, 1], 0, tl.int64) tmp6 = tl.where(tmp4, tmp5, tmp3) tmp7 = tl.full([1, 1], 3, tl.int64) tmp8 = tmp0 < tmp7 tmp9 = tl.where(tmp8, tmp1, tmp7) tmp10 = tl.where(tmp2, tmp6, tmp9) tmp12 = tl.full([XBLOCK, RBLOCK], 4, tl.int32) tmp13 = tmp11 + tmp12 tmp14 = tmp11 < 0 tmp15 = tl.where(tmp14, tmp13, tmp11) tl.device_assert((0 <= tmp15) & (tmp15 < 4), 'index out of bounds: 0 <= tmp15 < 4') tmp17 = tl.load(in_ptr1 + (r4 + 16 * tmp15 + 64 * tmp10), None) tmp18 = tl.load(in_ptr1 + (r4 + 64 * tmp10), None) tmp19 = tl_math.exp(tmp18) tmp20 = tl.load(in_ptr1 + (16 + r4 + 64 * tmp10), None) tmp21 = tl_math.exp(tmp20) tmp22 = tmp19 + tmp21 tmp23 = tl.load(in_ptr1 + (32 + r4 + 64 * tmp10), None) tmp24 = tl_math.exp(tmp23) tmp25 = tmp22 + tmp24 tmp26 = tl.load(in_ptr1 + (48 + r4 + 64 * tmp10), None) tmp27 = tl_math.exp(tmp26) tmp28 = tmp25 + tmp27 tmp29 = tl_math.log(tmp28) tmp30 = tmp17 - tmp29 tmp31 = -tmp30 tmp32 = 1.0 tmp33 = tmp31 * tmp32 tmp34 = tl.load(in_ptr2 + (r4 + 16 * tmp15 + 64 * tmp10), None) tmp35 = tl.load(in_ptr2 + (4 * r1 + 16 * tmp15 + 64 * tmp10), None, eviction_policy='evict_last') tmp36 = tl.load(in_ptr2 + (1 + 4 * r1 + 16 * tmp15 + 64 * tmp10), None, eviction_policy='evict_last') tmp37 = tmp35 + tmp36 tmp38 = tl.load(in_ptr2 + (2 + 4 * r1 + 16 * tmp15 + 64 * tmp10), None, eviction_policy='evict_last') tmp39 = tmp37 + tmp38 tmp40 = tl.load(in_ptr2 + (3 + 4 * r1 + 16 * tmp15 + 64 * tmp10), None, eviction_policy='evict_last') tmp41 = tmp39 + tmp40 tmp42 = tmp34 / tmp41 tmp32 - tmp42 tmp44 = tmp33 * tmp32 tmp45 = tl.broadcast_to(tmp44, [XBLOCK, RBLOCK]) tmp47 = tl.sum(tmp45, 1)[:, None] tmp48 = 64.0 tmp49 = tmp47 / tmp48 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp49, 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,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused__log_softmax__softmax_0[grid(256)](arg0_1, buf0, buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 buf3 = empty_strided_cuda((), (), torch.float32) buf4 = buf3 del buf3 triton_per_fused__log_softmax__softmax_index_mean_mul_neg_pow_rsub_1[ grid(1)](buf4, arg1_1, buf0, buf1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1) del arg1_1 del buf0 del buf1 return buf4, class FocalLossNew(nn.Module): def __init__(self, alpha=1, gamma=0): super(FocalLossNew, self).__init__() self.gamma = gamma self.alpha = alpha def get_attention(self, input, target): prob = F.softmax(input, dim=-1) prob = prob[range(target.shape[0]), target] prob = 1 - prob prob = prob ** self.gamma return prob def get_celoss(self, input, target): ce_loss = F.log_softmax(input, dim=1) ce_loss = -ce_loss[range(target.shape[0]), target] return ce_loss def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
umairjavaid/staff-employee-classification
FocalLoss
false
13,061
[ "MIT" ]
0
fc5fe32acfbde2b188094df90d888eeb0f4f4acd
https://github.com/umairjavaid/staff-employee-classification/tree/fc5fe32acfbde2b188094df90d888eeb0f4f4acd
encoder
import torch import torch.nn as nn import torch.nn.functional as F class encoder(nn.Module): def __init__(self, ef_dim): super(encoder, self).__init__() self.ef_dim = ef_dim self.conv_1 = nn.Conv3d(1, self.ef_dim, 4, stride=2, padding=1, bias=True) self.conv_2 = nn.Conv3d(self.ef_dim, self.ef_dim * 2, 4, stride=2, padding=1, bias=True) self.conv_3 = nn.Conv3d(self.ef_dim * 2, self.ef_dim * 4, 4, stride =2, padding=1, bias=True) self.conv_4 = nn.Conv3d(self.ef_dim * 4, self.ef_dim * 8, 4, stride =2, padding=1, bias=True) self.conv_5 = nn.Conv3d(self.ef_dim * 8, self.ef_dim * 8, 4, stride =1, padding=0, bias=True) nn.init.xavier_uniform_(self.conv_1.weight) nn.init.constant_(self.conv_1.bias, 0) nn.init.xavier_uniform_(self.conv_2.weight) nn.init.constant_(self.conv_2.bias, 0) nn.init.xavier_uniform_(self.conv_3.weight) nn.init.constant_(self.conv_3.bias, 0) nn.init.xavier_uniform_(self.conv_4.weight) nn.init.constant_(self.conv_4.bias, 0) nn.init.xavier_uniform_(self.conv_5.weight) nn.init.constant_(self.conv_5.bias, 0) def forward(self, inputs, is_training=False): d_1 = self.conv_1(inputs) d_1 = F.leaky_relu(d_1, negative_slope=0.01, inplace=True) d_2 = self.conv_2(d_1) d_2 = F.leaky_relu(d_2, negative_slope=0.01, inplace=True) d_3 = self.conv_3(d_2) d_3 = F.leaky_relu(d_3, negative_slope=0.01, inplace=True) d_4 = self.conv_4(d_3) d_4 = F.leaky_relu(d_4, negative_slope=0.01, inplace=True) d_5 = self.conv_5(d_4) d_5 = d_5.view(-1, self.ef_dim * 8) d_5 = torch.sigmoid(d_5) return d_5 def get_inputs(): return [torch.rand([4, 1, 64, 64, 64])] def get_init_inputs(): return [[], {'ef_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_leaky_relu_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 32768 % 4 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(in_out_ptr0 + x3, tmp7, None) @triton.jit def triton_poi_fused_convolution_leaky_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 % 8 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(in_out_ptr0 + x3, tmp7, None) @triton.jit def triton_poi_fused_convolution_leaky_relu_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 512 % 16 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(in_out_ptr0 + x3, tmp7, None) @triton.jit def triton_poi_fused_convolution_leaky_relu_3(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 // 64 % 32 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(in_out_ptr0 + x3, tmp7, None) @triton.jit def triton_poi_fused_sigmoid_4(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 32 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.sigmoid(tmp2) tl.store(in_out_ptr0 + x2, tmp3, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11) = args args.clear() assert_size_stride(primals_1, (4, 1, 4, 4, 4), (64, 64, 16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 1, 64, 64, 64), (262144, 262144, 4096, 64, 1)) assert_size_stride(primals_4, (8, 4, 4, 4, 4), (256, 64, 16, 4, 1)) assert_size_stride(primals_5, (8,), (1,)) assert_size_stride(primals_6, (16, 8, 4, 4, 4), (512, 64, 16, 4, 1)) assert_size_stride(primals_7, (16,), (1,)) assert_size_stride(primals_8, (32, 16, 4, 4, 4), (1024, 64, 16, 4, 1)) assert_size_stride(primals_9, (32,), (1,)) assert_size_stride(primals_10, (32, 32, 4, 4, 4), (2048, 64, 16, 4, 1)) assert_size_stride(primals_11, (32,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(2, 2, 2), padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False, output_padding=(0, 0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 32, 32, 32), (131072, 32768, 1024, 32, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_leaky_relu_0[grid(524288)](buf1, primals_2, 524288, XBLOCK=512, num_warps=8, num_stages=1) del primals_2 buf2 = extern_kernels.convolution(buf1, primals_4, stride=(2, 2, 2), padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False, output_padding=(0, 0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 8, 16, 16, 16), (32768, 4096, 256, 16, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_leaky_relu_1[grid(131072)](buf3, primals_5, 131072, XBLOCK=512, num_warps=8, num_stages=1) del primals_5 buf4 = extern_kernels.convolution(buf3, primals_6, stride=(2, 2, 2), padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False, output_padding=(0, 0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 16, 8, 8, 8), (8192, 512, 64, 8, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_leaky_relu_2[grid(32768)](buf5, primals_7, 32768, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 buf6 = extern_kernels.convolution(buf5, primals_8, stride=(2, 2, 2), padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False, output_padding=(0, 0, 0), groups=1, bias=None) assert_size_stride(buf6, (4, 32, 4, 4, 4), (2048, 64, 16, 4, 1)) buf7 = buf6 del buf6 triton_poi_fused_convolution_leaky_relu_3[grid(8192)](buf7, primals_9, 8192, XBLOCK=256, num_warps=4, num_stages=1) del primals_9 buf8 = extern_kernels.convolution(buf7, primals_10, stride=(1, 1, 1 ), padding=(0, 0, 0), dilation=(1, 1, 1), transposed=False, output_padding=(0, 0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 32, 1, 1, 1), (32, 1, 1, 1, 1)) buf9 = reinterpret_tensor(buf8, (4, 32), (32, 1), 0) del buf8 triton_poi_fused_sigmoid_4[grid(128)](buf9, primals_11, 128, XBLOCK =128, num_warps=4, num_stages=1) del primals_11 return (buf9, primals_1, primals_3, primals_4, primals_6, primals_8, primals_10, buf1, buf3, buf5, buf7, buf9) class encoderNew(nn.Module): def __init__(self, ef_dim): super(encoderNew, self).__init__() self.ef_dim = ef_dim self.conv_1 = nn.Conv3d(1, self.ef_dim, 4, stride=2, padding=1, bias=True) self.conv_2 = nn.Conv3d(self.ef_dim, self.ef_dim * 2, 4, stride=2, padding=1, bias=True) self.conv_3 = nn.Conv3d(self.ef_dim * 2, self.ef_dim * 4, 4, stride =2, padding=1, bias=True) self.conv_4 = nn.Conv3d(self.ef_dim * 4, self.ef_dim * 8, 4, stride =2, padding=1, bias=True) self.conv_5 = nn.Conv3d(self.ef_dim * 8, self.ef_dim * 8, 4, stride =1, padding=0, bias=True) nn.init.xavier_uniform_(self.conv_1.weight) nn.init.constant_(self.conv_1.bias, 0) nn.init.xavier_uniform_(self.conv_2.weight) nn.init.constant_(self.conv_2.bias, 0) nn.init.xavier_uniform_(self.conv_3.weight) nn.init.constant_(self.conv_3.bias, 0) nn.init.xavier_uniform_(self.conv_4.weight) nn.init.constant_(self.conv_4.bias, 0) nn.init.xavier_uniform_(self.conv_5.weight) nn.init.constant_(self.conv_5.bias, 0) def forward(self, input_0): primals_1 = self.conv_1.weight primals_2 = self.conv_1.bias primals_4 = self.conv_2.weight primals_5 = self.conv_2.bias primals_6 = self.conv_3.weight primals_7 = self.conv_3.bias primals_8 = self.conv_4.weight primals_9 = self.conv_4.bias primals_10 = self.conv_5.weight primals_11 = self.conv_5.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11]) return output[0]
trisct/BSP-NET-pytorch
encoder
false
13,062
[ "MIT" ]
0
31f148aa3d7321bac854bc3de6c88f676236b7e4
https://github.com/trisct/BSP-NET-pytorch/tree/31f148aa3d7321bac854bc3de6c88f676236b7e4
CustomInverse
import torch class CustomTorchOp(torch.autograd.Function): @staticmethod def symbolic(g, input): return g.op('torchcustom::Add10', input) @staticmethod def forward(ctx, x): return x + 10 class CustomInverse(torch.nn.Module): def forward(self, x, y): ress = CustomTorchOp.apply(torch.inverse(x)) return ress, torch.all(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 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_add_0(in_out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = 10.0 tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_per_fused_all_1(in_out_ptr0, in_ptr0, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tmp0 != 0 tmp2 = tmp1 == 0 tmp3 = tl.broadcast_to(tmp2, [RBLOCK]) tmp5 = triton_helpers.promote_to_tensor(triton_helpers.any(tmp3, 0)) tmp6 = tmp5 == 0 tl.debug_barrier() tl.store(in_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 = torch.ops.aten.linalg_inv_ex.default(arg0_1) del arg0_1 buf1 = buf0[0] del buf0 buf3 = buf1 del buf1 get_raw_stream(0) triton_poi_fused_add_0[grid(256)](buf3, 256, XBLOCK=256, num_warps= 4, num_stages=1) buf4 = empty_strided_cuda((), (), torch.bool) buf5 = buf4 del buf4 triton_per_fused_all_1[grid(1)](buf5, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg1_1 return buf3, buf5 class CustomTorchOp(torch.autograd.Function): @staticmethod def symbolic(g, input): return g.op('torchcustom::Add10', input) @staticmethod def forward(ctx, x): return x + 10 class CustomInverseNew(torch.nn.Module): def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0], output[1]
shaahji/onnxruntime-extensions
CustomInverse
false
13,063
[ "MIT" ]
0
c30df08aee69db761b97185be9f87160a4efa6bc
https://github.com/shaahji/onnxruntime-extensions/tree/c30df08aee69db761b97185be9f87160a4efa6bc
MixerMLP
import torch import torch.nn as nn class MixerMLP(nn.Module): """Some Information about MixerMLP""" def __init__(self, dim, activation='gelu'): super(MixerMLP, self).__init__() if activation == 'gelu': self.activation = nn.GELU() elif activation == 'relu': self.activation = nn.ReLU() elif activation == 'leakyrelu': self.activation = nn.LeakyReLU(0.2) self.fc1 = nn.Linear(dim, dim) self.fc2 = nn.Linear(dim, dim) self.ln = nn.LayerNorm(dim) def forward(self, x): x = x.swapaxes(1, 2) x = self.ln(x) x = self.activation(self.fc1(x)) x = self.fc2(x) x = x.swapaxes(1, 2) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_native_layer_norm_0(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tmp9 = tmp0 - tmp8 tmp10 = tmp9 * tmp9 tmp11 = tmp1 - tmp8 tmp12 = tmp11 * tmp11 tmp13 = tmp10 + tmp12 tmp14 = tmp3 - tmp8 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp8 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp19 / tmp7 tmp21 = 1e-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 = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex x5 = xindex // 4 x0 = xindex % 4 x1 = xindex // 4 % 4 x2 = xindex // 16 % 4 x3 = xindex // 64 tmp0 = tl.load(in_ptr0 + x4, xmask) tmp1 = tl.load(in_ptr1 + x5, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x5, 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 + (x0 + 4 * x2 + 16 * x1 + 64 * x3), tmp8, xmask) @triton.jit def triton_poi_fused_gelu_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.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) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 1), (16, 1, 4, 64), torch.float32) buf1 = empty_strided_cuda((4, 4, 4, 1), (16, 1, 4, 64), torch.float32) get_raw_stream(0) triton_poi_fused_native_layer_norm_0[grid(64)](primals_1, buf0, buf1, 64, XBLOCK=64, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_1[grid(256)](primals_1, buf0, buf1, primals_2, primals_3, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf0 del buf1 del primals_2 del primals_3 buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf2, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf3) del primals_5 buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_gelu_2[grid(256)](buf3, buf4, 256, XBLOCK=256, num_warps=4, num_stages=1) buf5 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf4, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf5) del primals_7 return reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 4, 16, 1), 0 ), primals_1, reinterpret_tensor(buf2, (64, 4), (4, 1), 0 ), buf3, reinterpret_tensor(buf4, (64, 4), (4, 1), 0 ), primals_6, primals_4 class MixerMLPNew(nn.Module): """Some Information about MixerMLP""" def __init__(self, dim, activation='gelu'): super(MixerMLPNew, self).__init__() if activation == 'gelu': self.activation = nn.GELU() elif activation == 'relu': self.activation = nn.ReLU() elif activation == 'leakyrelu': self.activation = nn.LeakyReLU(0.2) self.fc1 = nn.Linear(dim, dim) self.fc2 = nn.Linear(dim, dim) self.ln = nn.LayerNorm(dim) def forward(self, input_0): primals_4 = self.fc1.weight primals_2 = self.fc1.bias primals_6 = self.fc2.weight primals_3 = self.fc2.bias primals_5 = self.ln.weight primals_7 = self.ln.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
uthree/ReMixer
MixerMLP
false
13,064
[ "MIT" ]
0
587e1b6a01850df649eccf043689f84a7dd5e2dc
https://github.com/uthree/ReMixer/tree/587e1b6a01850df649eccf043689f84a7dd5e2dc
SpatialShift2d
import torch import torch.nn as nn import torch.nn.functional as F class SpatialShift2d(nn.Module): def __init__(self, channels, padding_mode='replicate'): super(SpatialShift2d, self).__init__() qc = channels // 4 self.num_shift_left = qc self.num_shift_right = qc self.num_shift_up = qc self.num_shift_down = channels - qc * 3 self.padding_mode = padding_mode def forward(self, x): _l, _r, _u, _d = (self.num_shift_left, self.num_shift_right, self. num_shift_up, self.num_shift_down) x = F.pad(x, (1, 1, 1, 1), self.padding_mode) l, r, u, d = torch.split(x, [_l, _r, _u, _d], dim=1) l = l[:, :, 1:-1, 0:-2] r = r[:, :, 1:-1, 2:] u = u[:, :, 0:-2, 1:-1] d = d[:, :, 2:, 1:-1] x = torch.cat([l, r, u, d], dim=1) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'channels': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream 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 = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex // 16 % 4 x0 = xindex % 4 x1 = xindex // 4 % 4 x3 = xindex // 64 x4 = xindex tmp0 = x2 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (4 * (3 * (3 <= x1) + x1 * (x1 < 3)) + 16 * x2 + 64 * x3 + (3 * (3 <= 0 * (0 >= -1 + x0) + (-1 + x0) * (-1 + x0 > 0) ) + (0 * (0 >= -1 + x0) + (-1 + x0) * (-1 + x0 > 0)) * (0 * (0 >= - 1 + x0) + (-1 + x0) * (-1 + x0 > 0) < 3))), tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp6 = tmp0 >= tmp3 tmp7 = tl.full([1], 2, tl.int64) tmp8 = tmp0 < tmp7 tmp9 = tmp6 & tmp8 tmp10 = tl.load(in_ptr0 + (16 + 4 * (3 * (3 <= x1) + x1 * (x1 < 3)) + 16 * (-1 + x2) + 64 * x3 + (3 * (3 <= 1 + x0) + (1 + x0) * (1 + x0 < 3))), tmp9 & xmask, other=0.0) tmp11 = tmp0 >= tmp7 tmp12 = tl.full([1], 3, tl.int64) tmp13 = tmp0 < tmp12 tmp14 = tmp11 & tmp13 tmp15 = tl.load(in_ptr0 + (32 + 4 * (3 * (3 <= 0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0)) + (0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0)) * (0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0) < 3)) + 16 * (-2 + x2) + 64 * x3 + (3 * (3 <= x0) + x0 * (x0 < 3))), tmp14 & xmask, other=0.0) tmp16 = tmp0 >= tmp12 tl.full([1], 4, tl.int64) tmp19 = tl.load(in_ptr0 + (48 + 4 * (3 * (3 <= 1 + x1) + (1 + x1) * (1 + x1 < 3)) + 16 * (-3 + x2) + 64 * x3 + (3 * (3 <= x0) + x0 * (x0 < 3 ))), tmp16 & xmask, other=0.0) tmp20 = tl.where(tmp14, tmp15, tmp19) tmp21 = tl.where(tmp9, tmp10, tmp20) tmp22 = tl.where(tmp4, tmp5, tmp21) tl.store(out_ptr0 + x4, tmp22, 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_cat_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, class SpatialShift2dNew(nn.Module): def __init__(self, channels, padding_mode='replicate'): super(SpatialShift2dNew, self).__init__() qc = channels // 4 self.num_shift_left = qc self.num_shift_right = qc self.num_shift_up = qc self.num_shift_down = channels - qc * 3 self.padding_mode = padding_mode def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
uthree/ReMixer
SpatialShift2d
false
13,065
[ "MIT" ]
0
587e1b6a01850df649eccf043689f84a7dd5e2dc
https://github.com/uthree/ReMixer/tree/587e1b6a01850df649eccf043689f84a7dd5e2dc
ElementWiseMLP
import torch import torch.nn as nn class ElementWiseMLP(nn.Module): """Some Information about ElementWiseMLP""" def __init__(self, dim, activation='gelu'): super(ElementWiseMLP, self).__init__() if activation == 'gelu': self.activation = nn.GELU() elif activation == 'relu': self.activation = nn.ReLU() elif activation == 'leakyrelu': self.activation = nn.LeakyReLU(0.2) self.fc1 = nn.Linear(dim, dim) self.fc2 = nn.Linear(dim, dim) self.ln = nn.LayerNorm(dim) def forward(self, x): x = self.ln(x) x = self.activation(self.fc1(x)) x = self.fc2(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_native_layer_norm_0(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tmp9 = tmp0 - tmp8 tmp10 = tmp9 * tmp9 tmp11 = tmp1 - tmp8 tmp12 = tmp11 * tmp11 tmp13 = tmp10 + tmp12 tmp14 = tmp3 - tmp8 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp8 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp19 / tmp7 tmp21 = 1e-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 = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_gelu_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.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) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (4,), (1,)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf1 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) get_raw_stream(0) triton_poi_fused_native_layer_norm_0[grid(64)](primals_3, buf0, buf1, 64, XBLOCK=64, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_1[grid(256)](primals_3, buf0, buf1, primals_1, primals_2, buf2, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf0 del buf1 del primals_1 del primals_2 buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf2, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf3) del primals_5 buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_gelu_2[grid(256)](buf3, buf4, 256, XBLOCK=256, num_warps=4, num_stages=1) buf5 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf4, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf5) del primals_7 return reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), primals_3, reinterpret_tensor(buf2, (64, 4), (4, 1), 0 ), buf3, reinterpret_tensor(buf4, (64, 4), (4, 1), 0 ), primals_6, primals_4 class ElementWiseMLPNew(nn.Module): """Some Information about ElementWiseMLP""" def __init__(self, dim, activation='gelu'): super(ElementWiseMLPNew, self).__init__() if activation == 'gelu': self.activation = nn.GELU() elif activation == 'relu': self.activation = nn.ReLU() elif activation == 'leakyrelu': self.activation = nn.LeakyReLU(0.2) self.fc1 = nn.Linear(dim, dim) self.fc2 = nn.Linear(dim, dim) self.ln = nn.LayerNorm(dim) def forward(self, input_0): primals_4 = self.fc1.weight primals_1 = self.fc1.bias primals_6 = self.fc2.weight primals_2 = self.fc2.bias primals_5 = self.ln.weight primals_7 = self.ln.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
uthree/ReMixer
ElementWiseMLP
false
13,066
[ "MIT" ]
0
587e1b6a01850df649eccf043689f84a7dd5e2dc
https://github.com/uthree/ReMixer/tree/587e1b6a01850df649eccf043689f84a7dd5e2dc
DQN_Linear
import torch import torch.nn as nn import torch.nn.functional as F class DQN_Linear(nn.Module): def __init__(self, input_size, output_size): super(DQN_Linear, self).__init__() self.l1 = nn.Linear(input_size, 32) self.l2 = nn.Linear(32, 64) self.head = nn.Linear(64, output_size) def forward(self, x): x = F.relu(self.l1(x)) x = F.relu(self.l2(x)) return self.head(x) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'output_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_relu_threshold_backward_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 32 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) @triton.jit def triton_poi_fused_relu_threshold_backward_1(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (32, 4), (4, 1)) assert_size_stride(primals_2, (32,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (64, 32), (32, 1)) assert_size_stride(primals_5, (64,), (1,)) assert_size_stride(primals_6, (4, 64), (64, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 32), (32, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 32), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 32), (512, 128, 32, 1), 0) del buf0 buf6 = empty_strided_cuda((4, 4, 4, 32), (512, 128, 32, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(2048)](buf1, primals_2, buf6, 2048, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 64), (64, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 32), (32, 1), 0), reinterpret_tensor(primals_4, (32, 64), (1, 32), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 64), (1024, 256, 64, 1), 0) del buf2 buf5 = empty_strided_cuda((4, 4, 4, 64), (1024, 256, 64, 1), torch.bool ) triton_poi_fused_relu_threshold_backward_1[grid(4096)](buf3, primals_5, buf5, 4096, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 64), (64, 1), 0), reinterpret_tensor(primals_6, (64, 4), (1, 64), 0), alpha=1, beta=1, out=buf4) del primals_7 return reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 32), (32, 1), 0), reinterpret_tensor( buf3, (64, 64), (64, 1), 0), primals_6, buf5, primals_4, buf6 class DQN_LinearNew(nn.Module): def __init__(self, input_size, output_size): super(DQN_LinearNew, self).__init__() self.l1 = nn.Linear(input_size, 32) self.l2 = nn.Linear(32, 64) self.head = nn.Linear(64, output_size) def forward(self, input_0): primals_1 = self.l1.weight primals_2 = self.l1.bias primals_4 = self.l2.weight primals_5 = self.l2.bias primals_6 = self.head.weight primals_7 = self.head.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
vashineyu/dqn_cartpole
DQN_Linear
false
13,067
[ "MIT" ]
0
7d3d2c26e29d40fce7710dbd56c59045514f2e84
https://github.com/vashineyu/dqn_cartpole/tree/7d3d2c26e29d40fce7710dbd56c59045514f2e84
EnvModel
import torch import torch.nn as nn import torch.nn.functional as F def layer_init(layer, w_scale=1.0): nn.init.orthogonal_(layer.weight.data) layer.weight.data.mul_(w_scale) nn.init.constant_(layer.bias.data, 0) return layer class EnvModel(nn.Module): def __init__(self, phi_dim, action_dim): super(EnvModel, self).__init__() self.hidden_dim = 300 self.fc_r1 = layer_init(nn.Linear(phi_dim + action_dim, self. hidden_dim)) self.fc_r2 = layer_init(nn.Linear(self.hidden_dim, 1)) self.fc_t1 = layer_init(nn.Linear(phi_dim, phi_dim)) self.fc_t2 = layer_init(nn.Linear(phi_dim + action_dim, phi_dim)) def forward(self, phi_s, action): phi = torch.cat([phi_s, action], dim=-1) r = self.fc_r2(F.tanh(self.fc_r1(phi))) phi_s_prime = phi_s + F.tanh(self.fc_t1(phi_s)) phi_sa_prime = torch.cat([phi_s_prime, action], dim=-1) phi_s_prime = phi_s_prime + F.tanh(self.fc_t2(phi_sa_prime)) return phi_s_prime, r def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'phi_dim': 4, 'action_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_cat_0(in_ptr0, in_ptr1, in_ptr2, 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 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) tmp11 = tl.load(in_ptr2 + (4 * x1 + x0), tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp12 = libdevice.tanh(tmp11) tmp13 = tmp5 + tmp12 tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype) tmp15 = tl.where(tmp4, tmp13, tmp14) tmp16 = tl.where(tmp4, tmp15, tmp9) tl.store(out_ptr0 + x2, tmp10, xmask) tl.store(out_ptr1 + x2, tmp16, xmask) @triton.jit def triton_poi_fused_tanh_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 19200 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 300 x2 = xindex // 1200 x4 = xindex % 1200 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = libdevice.tanh(tmp2) tl.store(out_ptr0 + (x4 + 1216 * x2), tmp3, xmask) @triton.jit def triton_poi_fused_addmm_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 19200 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 300 x1 = xindex // 300 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 300 * (x1 % 4) + 1216 * (x1 // 4)), xmask) tl.store(out_ptr0 + x2, tmp0, xmask) @triton.jit def triton_poi_fused_add_tanh_3(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask) tmp4 = tl.load(in_ptr2 + x0, xmask) tmp2 = libdevice.tanh(tmp1) tmp3 = tmp0 + tmp2 tmp5 = libdevice.tanh(tmp4) tmp6 = tmp3 + tmp5 tl.store(out_ptr0 + x0, 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) = 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, (300, 8), (8, 1)) assert_size_stride(primals_4, (300,), (1,)) assert_size_stride(primals_5, (1, 300), (300, 1)) assert_size_stride(primals_6, (1,), (1,)) assert_size_stride(primals_7, (4, 4), (4, 1)) assert_size_stride(primals_8, (4,), (1,)) assert_size_stride(primals_9, (4, 8), (8, 1)) assert_size_stride(primals_10, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf6 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_8, reinterpret_tensor(primals_1, (64, 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 buf0 = empty_strided_cuda((4, 4, 4, 8), (128, 32, 8, 1), torch.float32) buf7 = empty_strided_cuda((4, 4, 4, 8), (128, 32, 8, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(512)](primals_1, primals_2, buf6, buf0, buf7, 512, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf1 = empty_strided_cuda((64, 300), (300, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (64, 8), (8, 1), 0), reinterpret_tensor(primals_3, (8, 300), (1, 8), 0), out=buf1) del primals_3 buf2 = empty_strided_cuda((4, 4, 4, 300), (4864, 1216, 300, 1), torch.float32) triton_poi_fused_tanh_1[grid(19200)](buf1, primals_4, buf2, 19200, XBLOCK=256, num_warps=4, num_stages=1) del primals_4 buf3 = buf1 del buf1 triton_poi_fused_addmm_2[grid(19200)](buf2, buf3, 19200, XBLOCK=256, num_warps=4, num_stages=1) buf5 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_6, buf3, reinterpret_tensor(primals_5, (300, 1), (1, 300), 0), alpha=1, beta=1, out=buf5) del buf3 del primals_6 buf8 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_10, reinterpret_tensor(buf7, (64, 8), (8, 1), 0), reinterpret_tensor(primals_9, (8, 4), (1, 8), 0), alpha=1, beta=1, out=buf8) del primals_10 buf9 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_add_tanh_3[grid(256)](primals_1, buf6, buf8, buf9, 256, XBLOCK=256, num_warps=4, num_stages=1) return buf9, reinterpret_tensor(buf5, (4, 4, 4, 1), (16, 4, 1, 1), 0 ), reinterpret_tensor(buf0, (64, 8), (8, 1), 0 ), buf2, reinterpret_tensor(primals_1, (64, 4), (4, 1), 0 ), buf6, reinterpret_tensor(buf7, (64, 8), (8, 1), 0 ), buf8, primals_9, primals_5 def layer_init(layer, w_scale=1.0): nn.init.orthogonal_(layer.weight.data) layer.weight.data.mul_(w_scale) nn.init.constant_(layer.bias.data, 0) return layer class EnvModelNew(nn.Module): def __init__(self, phi_dim, action_dim): super(EnvModelNew, self).__init__() self.hidden_dim = 300 self.fc_r1 = layer_init(nn.Linear(phi_dim + action_dim, self. hidden_dim)) self.fc_r2 = layer_init(nn.Linear(self.hidden_dim, 1)) self.fc_t1 = layer_init(nn.Linear(phi_dim, phi_dim)) self.fc_t2 = layer_init(nn.Linear(phi_dim + action_dim, phi_dim)) def forward(self, input_0, input_1): primals_3 = self.fc_r1.weight primals_4 = self.fc_r1.bias primals_5 = self.fc_r2.weight primals_6 = self.fc_r2.bias primals_7 = self.fc_t1.weight primals_8 = self.fc_t1.bias primals_9 = self.fc_t2.weight primals_10 = self.fc_t2.bias primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10]) return output[0], output[1]
spacegoing/oc_hrl_pytorch
EnvModel
false
13,068
[ "MIT" ]
0
3e6c3b32b41d7dad40a9ee35f436f8cbcde8633b
https://github.com/spacegoing/oc_hrl_pytorch/tree/3e6c3b32b41d7dad40a9ee35f436f8cbcde8633b
Scale
import math import torch import torch.nn as nn class Scale(nn.Module): def __init__(self, d_model): super(Scale, self).__init__() self.d_model = d_model def forward(self, x): return x * math.sqrt(self.d_model) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'d_model': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_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 = 2.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_mul_0[grid(256)](arg0_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf0, class ScaleNew(nn.Module): def __init__(self, d_model): super(ScaleNew, self).__init__() self.d_model = d_model def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
voidism/End-to-end-ASR-Pytorch
Scale
false
13,069
[ "MIT" ]
0
509c389fa6ab98c30e227c6f4c8f7474adbc1bb2
https://github.com/voidism/End-to-end-ASR-Pytorch/tree/509c389fa6ab98c30e227c6f4c8f7474adbc1bb2
GeneratorBlock
import torch import torch.nn as nn def leaky_relu(p=0.2): return nn.LeakyReLU(p) class GeneratorBlock(nn.Module): def __init__(self, input_channels, latent_channels, output_channels, upsample=True): super(GeneratorBlock, self).__init__() if upsample: self.upsample = nn.Upsample(scale_factor=2) else: self.upsample = nn.Identity() self.conv1 = nn.Conv2d(input_channels, latent_channels, 3, 1, 1, padding_mode='replicate') self.conv1.bias.data = torch.zeros(*self.conv1.bias.data.shape) self.act1 = leaky_relu() self.conv2 = nn.Conv2d(latent_channels, output_channels, 3, 1, 1, padding_mode='replicate') self.conv2.bias.data = torch.zeros(*self.conv2.bias.data.shape) self.act2 = leaky_relu() self.to_rgb = nn.Conv2d(output_channels, 3, 1, 1, 0) def forward(self, x): x = self.upsample(x) x = self.conv1(x) x = self.act1(x) x = self.conv2(x) x = self.act2(x) rgb = self.to_rgb(x) return x, rgb def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_channels': 4, 'latent_channels': 4, 'output_channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused__unsafe_index_replication_pad2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1600 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 10 % 10 x0 = xindex % 10 x2 = xindex // 100 x5 = xindex tmp0 = 7 * (7 <= 0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0)) + (0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0)) * (0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0) < 7) tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp3.to(tl.int32) tmp5 = 7 * (7 <= 0 * (0 >= -1 + x0) + (-1 + x0) * (-1 + x0 > 0)) + (0 * (0 >= -1 + x0) + (-1 + x0) * (-1 + x0 > 0)) * (0 * (0 >= -1 + x0) + (-1 + x0) * (-1 + x0 > 0) < 7) 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 + x5, tmp9, xmask) @triton.jit def triton_poi_fused_convolution_leaky_relu_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 64 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.2 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x3, tmp4, xmask) tl.store(out_ptr1 + x3, tmp7, xmask) @triton.jit def triton_poi_fused_replication_pad2d_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1600 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 10 x1 = xindex // 10 % 10 x2 = xindex // 100 x3 = xindex tmp0 = tl.load(in_ptr0 + (8 * (7 * (7 <= 0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0)) + (0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0)) * (0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0) < 7)) + 64 * x2 + ( 7 * (7 <= 0 * (0 >= -1 + x0) + (-1 + x0) * (-1 + x0 > 0)) + (0 * (0 >= -1 + x0) + (-1 + x0) * (-1 + x0 > 0)) * (0 * (0 >= -1 + x0) + (-1 + x0) * (-1 + x0 > 0) < 7))), xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_poi_fused_convolution_3(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 x3 = xindex x1 = xindex // 64 % 3 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) = 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,)) assert_size_stride(primals_6, (3, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_7, (3,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 10, 10), (400, 100, 10, 1), torch. float32) get_raw_stream(0) triton_poi_fused__unsafe_index_replication_pad2d_0[grid(1600)]( primals_1, buf0, 1600, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 8, 8), (256, 64, 8, 1)) buf2 = empty_strided_cuda((4, 4, 8, 8), (256, 64, 8, 1), torch.bool) buf3 = empty_strided_cuda((4, 4, 8, 8), (256, 64, 8, 1), torch.float32) triton_poi_fused_convolution_leaky_relu_1[grid(1024)](buf1, primals_3, buf2, buf3, 1024, XBLOCK=256, num_warps=4, num_stages=1) del primals_3 buf4 = empty_strided_cuda((4, 4, 10, 10), (400, 100, 10, 1), torch. float32) triton_poi_fused_replication_pad2d_2[grid(1600)](buf3, buf4, 1600, XBLOCK=128, num_warps=4, num_stages=1) buf5 = extern_kernels.convolution(buf4, primals_4, 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, 8, 8), (256, 64, 8, 1)) buf6 = empty_strided_cuda((4, 4, 8, 8), (256, 64, 8, 1), torch.bool) buf7 = buf1 del buf1 triton_poi_fused_convolution_leaky_relu_1[grid(1024)](buf5, primals_5, buf6, buf7, 1024, XBLOCK=256, num_warps=4, num_stages=1) del buf5 del primals_5 buf8 = extern_kernels.convolution(buf7, primals_6, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 3, 8, 8), (192, 64, 8, 1)) buf9 = buf8 del buf8 triton_poi_fused_convolution_3[grid(768)](buf9, primals_7, 768, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 return (buf7, buf9, primals_2, primals_4, primals_6, buf0, buf2, buf3, buf4, buf6, buf7) def leaky_relu(p=0.2): return nn.LeakyReLU(p) class GeneratorBlockNew(nn.Module): def __init__(self, input_channels, latent_channels, output_channels, upsample=True): super(GeneratorBlockNew, self).__init__() if upsample: self.upsample = nn.Upsample(scale_factor=2) else: self.upsample = nn.Identity() self.conv1 = nn.Conv2d(input_channels, latent_channels, 3, 1, 1, padding_mode='replicate') self.conv1.bias.data = torch.zeros(*self.conv1.bias.data.shape) self.act1 = leaky_relu() self.conv2 = nn.Conv2d(latent_channels, output_channels, 3, 1, 1, padding_mode='replicate') self.conv2.bias.data = torch.zeros(*self.conv2.bias.data.shape) self.act2 = leaky_relu() self.to_rgb = nn.Conv2d(output_channels, 3, 1, 1, 0) 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_6 = self.to_rgb.weight primals_7 = self.to_rgb.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0], output[1]
uthree/pg-gan
GeneratorBlock
false
13,070
[ "MIT" ]
0
7a72a9f3487a66ddc6c8c51a774e3d8128369b2a
https://github.com/uthree/pg-gan/tree/7a72a9f3487a66ddc6c8c51a774e3d8128369b2a