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
TerConv2d
import torch import numpy as np from itertools import product as product import torch.nn.functional as F from torch import nn import torch.optim import torch.utils.data def ternary_threshold(delta: 'float'=0.7, *ws): """Ternary threshold find in ws.""" assert isinstance(delta, float) num_params = sum_w = 0 if not ws: threshold = torch.tensor(np.nan) else: for w in ws: num_params += w.data.numel() sum_w += w.abs().sum() threshold = delta * (sum_w / num_params) return threshold class TerQuant(torch.autograd.Function): """TeraryNet quantization function.""" @staticmethod def forward(ctx, w, threshold): ctx.save_for_backward(w, threshold) w_ter = torch.where(w > threshold, torch.tensor(1.0), torch.tensor(0.0) ) w_ter = torch.where(w.abs() <= -threshold, torch.tensor(0.0), w_ter) w_ter = torch.where(w < -threshold, torch.tensor(-1.0), w_ter) return w_ter @staticmethod def backward(ctx, grad_o): """Back propagation using same as identity function.""" grad_i = grad_o.clone() return grad_i, None class TerConv2d(nn.Conv2d): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.delta = 0.7 def forward(self, x: 'torch.Tensor') ->torch.Tensor: threshold = ternary_threshold(self.delta, self.weight) self.weight_q = TerQuant.apply(self.weight, threshold) x = F.conv2d(x, self.weight_q, self.bias, self.stride, self.padding, self.dilation, self.groups) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4, 'kernel_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import numpy as np from itertools import product as product from torch import nn 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_per_fused_abs_add_div_gt_le_lift_fresh_lt_mul_neg_sum_where_0( in_ptr0, out_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl_math.abs(tmp0) tmp2 = tl.broadcast_to(tmp1, [RBLOCK]) tmp4 = triton_helpers.promote_to_tensor(tl.sum(tmp2, 0)) tmp5 = 0.0 tmp6 = tmp4 + tmp5 tmp7 = 0.00390625 tmp8 = tmp6 * tmp7 tmp9 = 0.7 tmp10 = tmp8 * tmp9 tmp11 = -tmp10 tmp12 = tmp0 < tmp11 tmp13 = tmp1 <= tmp11 tmp14 = tmp0 > tmp10 tmp15 = 1.0 tmp16 = tl.where(tmp14, tmp15, tmp5) tmp17 = tl.where(tmp13, tmp5, tmp16) tmp18 = -1.0 tmp19 = tl.where(tmp12, tmp18, tmp17) tl.store(out_ptr1 + tl.broadcast_to(r0, [RBLOCK]), tmp19, None) @triton.jit def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x2, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_per_fused_abs_add_div_gt_le_lift_fresh_lt_mul_neg_sum_where_0[ grid(1)](primals_1, buf1, 1, 256, num_warps=2, num_stages=1) del primals_1 buf2 = extern_kernels.convolution(primals_3, buf1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 4, 1, 1), (4, 1, 1, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_1[grid(16)](buf3, primals_2, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_2 return buf3, buf1, primals_3, buf1 def ternary_threshold(delta: 'float'=0.7, *ws): """Ternary threshold find in ws.""" assert isinstance(delta, float) num_params = sum_w = 0 if not ws: threshold = torch.tensor(np.nan) else: for w in ws: num_params += w.data.numel() sum_w += w.abs().sum() threshold = delta * (sum_w / num_params) return threshold class TerQuant(torch.autograd.Function): """TeraryNet quantization function.""" @staticmethod def forward(ctx, w, threshold): ctx.save_for_backward(w, threshold) w_ter = torch.where(w > threshold, torch.tensor(1.0), torch.tensor(0.0) ) w_ter = torch.where(w.abs() <= -threshold, torch.tensor(0.0), w_ter) w_ter = torch.where(w < -threshold, torch.tensor(-1.0), w_ter) return w_ter @staticmethod def backward(ctx, grad_o): """Back propagation using same as identity function.""" grad_i = grad_o.clone() return grad_i, None class TerConv2dNew(nn.Conv2d): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.delta = 0.7 def forward(self, input_0): primals_1 = self.weight primals_2 = self.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
ninfueng/a-PyTorch-Tutorial-to-Object-Detection
TerConv2d
false
10,637
[ "MIT" ]
0
fc7544720a7e939f5a56f4f7214e4965b7775f77
https://github.com/ninfueng/a-PyTorch-Tutorial-to-Object-Detection/tree/fc7544720a7e939f5a56f4f7214e4965b7775f77
BinConv2d
import torch from itertools import product as product import torch.nn.functional as F from torch import nn import torch.optim import torch.utils.data class BinQuant(torch.autograd.Function): """BinaryConnect quantization. Refer: https://pytorch.org/tutorials/beginner/examples_autograd/two_layer_net_custom_function.html https://discuss.pytorch.org/t/difference-between-apply-an-call-for-an-autograd-function/13845/3 """ @staticmethod def forward(ctx, w): """Require w be in range of [0, 1]. Otherwise, it is not in activate range. """ return w.sign() @staticmethod def backward(ctx, grad_o): grad_i = grad_o.clone() return grad_i class BinConv2d(nn.Conv2d): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def forward(self, x, *args): self.weight_q = BinQuant.apply(self.weight) y = F.conv2d(x, self.weight_q, self.bias, self.stride, self.padding, self.dilation, self.groups) return y 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 itertools import product as product from torch import nn 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_sign_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.full([1], 0, tl.int32) tmp2 = tmp1 < tmp0 tmp3 = tmp2.to(tl.int8) tmp4 = tmp0 < tmp1 tmp5 = tmp4.to(tl.int8) tmp6 = tmp3 - tmp5 tmp7 = tmp6.to(tmp0.dtype) tl.store(out_ptr0 + x0, tmp7, xmask) @triton.jit def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x2, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_sign_0[grid(256)](primals_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 buf1 = extern_kernels.convolution(primals_3, buf0, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 1, 1), (4, 1, 1, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_1[grid(16)](buf2, primals_2, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_2 return buf2, buf0, primals_3, buf0 class BinQuant(torch.autograd.Function): """BinaryConnect quantization. Refer: https://pytorch.org/tutorials/beginner/examples_autograd/two_layer_net_custom_function.html https://discuss.pytorch.org/t/difference-between-apply-an-call-for-an-autograd-function/13845/3 """ @staticmethod def forward(ctx, w): """Require w be in range of [0, 1]. Otherwise, it is not in activate range. """ return w.sign() @staticmethod def backward(ctx, grad_o): grad_i = grad_o.clone() return grad_i class BinConv2dNew(nn.Conv2d): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def forward(self, input_0): primals_1 = self.weight primals_2 = self.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
ninfueng/a-PyTorch-Tutorial-to-Object-Detection
BinConv2d
false
10,638
[ "MIT" ]
0
fc7544720a7e939f5a56f4f7214e4965b7775f77
https://github.com/ninfueng/a-PyTorch-Tutorial-to-Object-Detection/tree/fc7544720a7e939f5a56f4f7214e4965b7775f77
TerLinear
import torch import numpy as np from itertools import product as product import torch.nn.functional as F from torch import nn import torch.optim import torch.utils.data def ternary_threshold(delta: 'float'=0.7, *ws): """Ternary threshold find in ws.""" assert isinstance(delta, float) num_params = sum_w = 0 if not ws: threshold = torch.tensor(np.nan) else: for w in ws: num_params += w.data.numel() sum_w += w.abs().sum() threshold = delta * (sum_w / num_params) return threshold class TerQuant(torch.autograd.Function): """TeraryNet quantization function.""" @staticmethod def forward(ctx, w, threshold): ctx.save_for_backward(w, threshold) w_ter = torch.where(w > threshold, torch.tensor(1.0), torch.tensor(0.0) ) w_ter = torch.where(w.abs() <= -threshold, torch.tensor(0.0), w_ter) w_ter = torch.where(w < -threshold, torch.tensor(-1.0), w_ter) return w_ter @staticmethod def backward(ctx, grad_o): """Back propagation using same as identity function.""" grad_i = grad_o.clone() return grad_i, None class TerLinear(nn.Linear): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.delta = 0.7 def forward(self, x: 'torch.Tensor') ->torch.Tensor: threshold = ternary_threshold(self.delta, self.weight) self.weight_q = TerQuant.apply(self.weight, threshold) x = F.linear(x, self.weight_q, self.bias) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math import numpy as np from itertools import product as product from torch import nn 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 reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_per_fused_abs_add_div_gt_le_lift_fresh_lt_mul_neg_sum_where_0( in_ptr0, out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl_math.abs(tmp0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp4 = tl.sum(tmp2, 1)[:, None] tmp5 = 0.0 tmp6 = tmp4 + tmp5 tmp7 = 0.0625 tmp8 = tmp6 * tmp7 tmp9 = 0.7 tmp10 = tmp8 * tmp9 tmp11 = -tmp10 tmp12 = tmp0 < tmp11 tmp13 = tmp1 <= tmp11 tmp14 = tmp0 > tmp10 tmp15 = 1.0 tmp16 = tl.where(tmp14, tmp15, tmp5) tmp17 = tl.where(tmp13, tmp5, tmp16) tmp18 = -1.0 tmp19 = tl.where(tmp12, tmp18, tmp17) tl.store(out_ptr1 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp19, None) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_per_fused_abs_add_div_gt_le_lift_fresh_lt_mul_neg_sum_where_0[ grid(1)](primals_1, buf1, 1, 16, XBLOCK=1, num_warps=2, num_stages=1) del primals_1 buf2 = 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(buf1, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf2) del primals_2 return reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), buf1, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0) def ternary_threshold(delta: 'float'=0.7, *ws): """Ternary threshold find in ws.""" assert isinstance(delta, float) num_params = sum_w = 0 if not ws: threshold = torch.tensor(np.nan) else: for w in ws: num_params += w.data.numel() sum_w += w.abs().sum() threshold = delta * (sum_w / num_params) return threshold class TerQuant(torch.autograd.Function): """TeraryNet quantization function.""" @staticmethod def forward(ctx, w, threshold): ctx.save_for_backward(w, threshold) w_ter = torch.where(w > threshold, torch.tensor(1.0), torch.tensor(0.0) ) w_ter = torch.where(w.abs() <= -threshold, torch.tensor(0.0), w_ter) w_ter = torch.where(w < -threshold, torch.tensor(-1.0), w_ter) return w_ter @staticmethod def backward(ctx, grad_o): """Back propagation using same as identity function.""" grad_i = grad_o.clone() return grad_i, None class TerLinearNew(nn.Linear): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.delta = 0.7 def forward(self, input_0): primals_1 = self.weight primals_2 = self.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
ninfueng/a-PyTorch-Tutorial-to-Object-Detection
TerLinear
false
10,639
[ "MIT" ]
0
fc7544720a7e939f5a56f4f7214e4965b7775f77
https://github.com/ninfueng/a-PyTorch-Tutorial-to-Object-Detection/tree/fc7544720a7e939f5a56f4f7214e4965b7775f77
pixelwise_norm_layer
import torch import torch.nn as nn class pixelwise_norm_layer(nn.Module): def __init__(self): super(pixelwise_norm_layer, self).__init__() self.eps = 1e-08 def forward(self, x): return x / (torch.mean(x ** 2, dim=1, keepdim=True) + self.eps) ** 0.5 def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_div_mean_pow_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = 4.0 tmp13 = tmp11 / tmp12 tmp14 = 1e-08 tmp15 = tmp13 + tmp14 tmp16 = libdevice.sqrt(tmp15) tmp17 = tmp0 / tmp16 tl.store(out_ptr0 + x3, tmp17, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_mean_pow_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, class pixelwise_norm_layerNew(nn.Module): def __init__(self): super(pixelwise_norm_layerNew, self).__init__() self.eps = 1e-08 def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
mikanCan/PG-GAN
pixelwise_norm_layer
false
10,640
[ "MIT" ]
0
bc4a1bd2101f836c22a164174381f80b3f5c73c1
https://github.com/mikanCan/PG-GAN/tree/bc4a1bd2101f836c22a164174381f80b3f5c73c1
Norm
import torch import torch.nn as nn class Norm(nn.Module): def __init__(self, d_model, eps=1e-06): super().__init__() self.size = d_model self.alpha = nn.Parameter(torch.ones(self.size)) self.bias = nn.Parameter(torch.zeros(self.size)) self.eps = eps def forward(self, x): norm = self.alpha * (x - x.mean(dim=-1, keepdim=True)) / (x.std(dim =-1, keepdim=True) + self.eps) + self.bias return norm 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 from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_div_mean_mul_std_sub_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask) tmp2 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp30 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp8 = tmp6 + tmp7 tmp9 = 4.0 tmp10 = tmp8 / tmp9 tmp11 = tmp1 - tmp10 tmp12 = tmp0 * tmp11 tmp13 = tmp2 - tmp10 tmp14 = tmp13 * tmp13 tmp15 = tmp3 - tmp10 tmp16 = tmp15 * tmp15 tmp17 = tmp14 + tmp16 tmp18 = tmp5 - tmp10 tmp19 = tmp18 * tmp18 tmp20 = tmp17 + tmp19 tmp21 = tmp7 - tmp10 tmp22 = tmp21 * tmp21 tmp23 = tmp20 + tmp22 tmp24 = 3.0 tmp25 = tmp23 / tmp24 tmp26 = libdevice.sqrt(tmp25) tmp27 = 1e-06 tmp28 = tmp26 + tmp27 tmp29 = tmp12 / tmp28 tmp31 = tmp29 + tmp30 tl.store(out_ptr0 + x2, tmp31, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4,), (1,)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_mean_mul_std_sub_0[grid(256)](primals_1, primals_2, primals_3, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 del primals_3 return buf0, primals_2 class NormNew(nn.Module): def __init__(self, d_model, eps=1e-06): super().__init__() self.size = d_model self.alpha = nn.Parameter(torch.ones(self.size)) self.bias = nn.Parameter(torch.zeros(self.size)) self.eps = eps def forward(self, input_0): primals_1 = self.alpha primals_3 = self.bias primals_2 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
nlakshmanan/Transformer
Norm
false
10,641
[ "Apache-2.0" ]
0
4562f8e9b282d0a70f26903a7b4410cb6132364b
https://github.com/nlakshmanan/Transformer/tree/4562f8e9b282d0a70f26903a7b4410cb6132364b
ConcatModel
import torch import torch.nn.functional class ConcatModel(torch.nn.Module): def __init__(self): super(ConcatModel, self).__init__() def forward(self, x): return torch.concat([x, 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.functional assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 256 x2 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x2, tmp0, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((2, 4, 4, 4, 4), (256, 64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(512)](arg0_1, buf0, 512, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return reinterpret_tensor(buf0, (8, 4, 4, 4), (64, 16, 4, 1), 0), class ConcatModelNew(torch.nn.Module): def __init__(self): super(ConcatModelNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
elad-c/model_optimization
ConcatModel
false
10,642
[ "Apache-2.0" ]
0
b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
https://github.com/elad-c/model_optimization/tree/b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
CatModel
import torch import torch.nn.functional class CatModel(torch.nn.Module): def __init__(self): super(CatModel, self).__init__() def forward(self, x): return torch.cat([x, 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.functional assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 256 x2 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x2, tmp0, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((2, 4, 4, 4, 4), (256, 64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(512)](arg0_1, buf0, 512, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return reinterpret_tensor(buf0, (8, 4, 4, 4), (64, 16, 4, 1), 0), class CatModelNew(torch.nn.Module): def __init__(self): super(CatModelNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
elad-c/model_optimization
CatModel
false
10,643
[ "Apache-2.0" ]
0
b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
https://github.com/elad-c/model_optimization/tree/b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
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, 256) self.fc2 = nn.Linear(256, 2) def forward(self, x): x = F.relu(self.fc1(x)) x = self.fc2(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 % 256 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, None) tl.store(out_ptr0 + x2, tmp6, None) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (256, 4), (4, 1)) assert_size_stride(primals_2, (256,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (2, 256), (256, 1)) assert_size_stride(primals_5, (2,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 256), (256, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 256), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 256), (4096, 1024, 256, 1), 0 ) del buf0 buf3 = empty_strided_cuda((4, 4, 4, 256), (4096, 1024, 256, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(16384)](buf1, primals_2, buf3, 16384, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 2), (2, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 256), (256, 1), 0), reinterpret_tensor(primals_4, (256, 2), (1, 256), 0), alpha=1, beta=1, out=buf2) del primals_5 return reinterpret_tensor(buf2, (4, 4, 4, 2), (32, 8, 2, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 256), (256, 1), 0), primals_4, buf3 class QnetNew(nn.Module): def __init__(self): super(QnetNew, self).__init__() self.fc1 = nn.Linear(4, 256) self.fc2 = nn.Linear(256, 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_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
rainwangphy/minimalRL
Qnet
false
10,644
[ "MIT" ]
0
646cc771107f1b15098d7f52f0e7c4444862fb90
https://github.com/rainwangphy/minimalRL/tree/646cc771107f1b15098d7f52f0e7c4444862fb90
SparsemaxBisect
from torch.autograd import Function import torch import torch.nn as nn def sparsemax_bisect(X, dim=-1, n_iter=50, ensure_sum_one=True): """sparsemax: normalizing sparse transform (a la softmax), via bisection. Solves the projection: min_p ||x - p||_2 s.t. p >= 0, sum(p) == 1. Parameters ---------- X : torch.Tensor The input tensor. dim : int The dimension along which to apply sparsemax. n_iter : int Number of bisection iterations. For float32, 24 iterations should suffice for machine precision. ensure_sum_one : bool, Whether to divide the result by its sum. If false, the result might sum to close but not exactly 1, which might cause downstream problems. Note: This function does not yet support normalizing along anything except the last dimension. Please use transposing and views to achieve more general behavior. Returns ------- P : torch tensor, same shape as X The projection result, such that P.sum(dim=dim) == 1 elementwise. """ return SparsemaxBisectFunction.apply(X, dim, n_iter, ensure_sum_one) class EntmaxBisectFunction(Function): @classmethod def _gp(cls, x, alpha): return x ** (alpha - 1) @classmethod def _gp_inv(cls, y, alpha): return y ** (1 / (alpha - 1)) @classmethod def _p(cls, X, alpha): return cls._gp_inv(torch.clamp(X, min=0), alpha) @classmethod def forward(cls, ctx, X, alpha=1.5, dim=-1, n_iter=50, ensure_sum_one=True ): if not isinstance(alpha, torch.Tensor): alpha = torch.tensor(alpha, dtype=X.dtype, device=X.device) alpha_shape = list(X.shape) alpha_shape[dim] = 1 alpha = alpha.expand(*alpha_shape) ctx.alpha = alpha ctx.dim = dim d = X.shape[dim] X = X * (alpha - 1) max_val, _ = X.max(dim=dim, keepdim=True) tau_lo = max_val - cls._gp(1, alpha) tau_hi = max_val - cls._gp(1 / d, alpha) f_lo = cls._p(X - tau_lo, alpha).sum(dim) - 1 dm = tau_hi - tau_lo for it in range(n_iter): dm /= 2 tau_m = tau_lo + dm p_m = cls._p(X - tau_m, alpha) f_m = p_m.sum(dim) - 1 mask = (f_m * f_lo >= 0).unsqueeze(dim) tau_lo = torch.where(mask, tau_m, tau_lo) if ensure_sum_one: p_m /= p_m.sum(dim=dim).unsqueeze(dim=dim) ctx.save_for_backward(p_m) return p_m @classmethod def backward(cls, ctx, dY): Y, = ctx.saved_tensors gppr = torch.where(Y > 0, Y ** (2 - ctx.alpha), Y.new_zeros(1)) dX = dY * gppr q = dX.sum(ctx.dim) / gppr.sum(ctx.dim) q = q.unsqueeze(ctx.dim) dX -= q * gppr d_alpha = None if ctx.needs_input_grad[1]: S = torch.where(Y > 0, Y * torch.log(Y), Y.new_zeros(1)) ent = S.sum(ctx.dim).unsqueeze(ctx.dim) Y_skewed = gppr / gppr.sum(ctx.dim).unsqueeze(ctx.dim) d_alpha = dY * (Y - Y_skewed) / (ctx.alpha - 1) ** 2 d_alpha -= dY * (S - Y_skewed * ent) / (ctx.alpha - 1) d_alpha = d_alpha.sum(ctx.dim).unsqueeze(ctx.dim) return dX, d_alpha, None, None, None class SparsemaxBisectFunction(EntmaxBisectFunction): @classmethod def _gp(cls, x, alpha): return x @classmethod def _gp_inv(cls, y, alpha): return y @classmethod def _p(cls, x, alpha): return torch.clamp(x, min=0) @classmethod def forward(cls, ctx, X, dim=-1, n_iter=50, ensure_sum_one=True): return super().forward(ctx, X, alpha=2, dim=dim, n_iter=50, ensure_sum_one=True) @classmethod def backward(cls, ctx, dY): Y, = ctx.saved_tensors gppr = Y > 0 dX = dY * gppr q = dX.sum(ctx.dim) / gppr.sum(ctx.dim) q = q.unsqueeze(ctx.dim) dX -= q * gppr return dX, None, None, None class SparsemaxBisect(nn.Module): def __init__(self, dim=-1, n_iter=None): """sparsemax: normalizing sparse transform (a la softmax) via bisection Solves the projection: min_p ||x - p||_2 s.t. p >= 0, sum(p) == 1. Parameters ---------- dim : int The dimension along which to apply sparsemax. n_iter : int Number of bisection iterations. For float32, 24 iterations should suffice for machine precision. """ self.dim = dim self.n_iter = n_iter super().__init__() def forward(self, X): return sparsemax_bisect(X, dim=self.dim, n_iter=self.n_iter) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch.autograd import Function import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_clamp_div_max_mul_sub_sum_where_0(in_out_ptr8, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp2 = triton_helpers.maximum(tmp0, tmp1) tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp6 = triton_helpers.maximum(tmp4, tmp5) tmp7 = 1.0 tmp8 = tmp6 - tmp7 tmp9 = 0.25 tmp10 = tmp6 - tmp9 tmp11 = tmp10 - tmp8 tmp12 = 0.5 tmp13 = tmp11 * tmp12 tmp14 = tmp8 + tmp13 tmp15 = tmp0 - tmp14 tmp16 = 0.0 tmp17 = triton_helpers.maximum(tmp15, tmp16) tmp18 = tmp1 - tmp14 tmp19 = triton_helpers.maximum(tmp18, tmp16) tmp20 = tmp17 + tmp19 tmp21 = tmp3 - tmp14 tmp22 = triton_helpers.maximum(tmp21, tmp16) tmp23 = tmp20 + tmp22 tmp24 = tmp5 - tmp14 tmp25 = triton_helpers.maximum(tmp24, tmp16) tmp26 = tmp23 + tmp25 tmp27 = tmp26 - tmp7 tmp28 = tmp0 - tmp8 tmp29 = triton_helpers.maximum(tmp28, tmp16) tmp30 = tmp1 - tmp8 tmp31 = triton_helpers.maximum(tmp30, tmp16) tmp32 = tmp29 + tmp31 tmp33 = tmp3 - tmp8 tmp34 = triton_helpers.maximum(tmp33, tmp16) tmp35 = tmp32 + tmp34 tmp36 = tmp5 - tmp8 tmp37 = triton_helpers.maximum(tmp36, tmp16) tmp38 = tmp35 + tmp37 tmp39 = tmp38 - tmp7 tmp40 = tmp27 * tmp39 tmp41 = tmp40 >= tmp16 tmp42 = tl.where(tmp41, tmp14, tmp8) tmp43 = tmp13 * tmp12 tmp44 = tmp42 + tmp43 tmp45 = tmp0 - tmp44 tmp46 = triton_helpers.maximum(tmp45, tmp16) tmp47 = tmp1 - tmp44 tmp48 = triton_helpers.maximum(tmp47, tmp16) tmp49 = tmp46 + tmp48 tmp50 = tmp3 - tmp44 tmp51 = triton_helpers.maximum(tmp50, tmp16) tmp52 = tmp49 + tmp51 tmp53 = tmp5 - tmp44 tmp54 = triton_helpers.maximum(tmp53, tmp16) tmp55 = tmp52 + tmp54 tmp56 = tmp55 - tmp7 tmp57 = tmp56 * tmp39 tmp58 = tmp57 >= tmp16 tmp59 = tl.where(tmp58, tmp44, tmp42) tmp60 = tmp43 * tmp12 tmp61 = tmp59 + tmp60 tmp62 = tmp0 - tmp61 tmp63 = triton_helpers.maximum(tmp62, tmp16) tmp64 = tmp1 - tmp61 tmp65 = triton_helpers.maximum(tmp64, tmp16) tmp66 = tmp63 + tmp65 tmp67 = tmp3 - tmp61 tmp68 = triton_helpers.maximum(tmp67, tmp16) tmp69 = tmp66 + tmp68 tmp70 = tmp5 - tmp61 tmp71 = triton_helpers.maximum(tmp70, tmp16) tmp72 = tmp69 + tmp71 tmp73 = tmp72 - tmp7 tmp74 = tmp73 * tmp39 tmp75 = tmp74 >= tmp16 tmp76 = tl.where(tmp75, tmp61, tmp59) tmp77 = tmp60 * tmp12 tmp78 = tmp76 + tmp77 tmp79 = tmp0 - tmp78 tmp80 = triton_helpers.maximum(tmp79, tmp16) tmp81 = tmp1 - tmp78 tmp82 = triton_helpers.maximum(tmp81, tmp16) tmp83 = tmp80 + tmp82 tmp84 = tmp3 - tmp78 tmp85 = triton_helpers.maximum(tmp84, tmp16) tmp86 = tmp83 + tmp85 tmp87 = tmp5 - tmp78 tmp88 = triton_helpers.maximum(tmp87, tmp16) tmp89 = tmp86 + tmp88 tmp90 = tmp89 - tmp7 tmp91 = tmp90 * tmp39 tmp92 = tmp91 >= tmp16 tmp93 = tl.where(tmp92, tmp78, tmp76) tmp94 = tmp77 * tmp12 tmp95 = tmp93 + tmp94 tmp96 = tmp0 - tmp95 tmp97 = triton_helpers.maximum(tmp96, tmp16) tmp98 = tmp1 - tmp95 tmp99 = triton_helpers.maximum(tmp98, tmp16) tmp100 = tmp97 + tmp99 tmp101 = tmp3 - tmp95 tmp102 = triton_helpers.maximum(tmp101, tmp16) tmp103 = tmp100 + tmp102 tmp104 = tmp5 - tmp95 tmp105 = triton_helpers.maximum(tmp104, tmp16) tmp106 = tmp103 + tmp105 tmp107 = tmp106 - tmp7 tmp108 = tmp107 * tmp39 tmp109 = tmp108 >= tmp16 tmp110 = tl.where(tmp109, tmp95, tmp93) tmp111 = tmp94 * tmp12 tmp112 = tmp110 + tmp111 tmp113 = tmp0 - tmp112 tmp114 = triton_helpers.maximum(tmp113, tmp16) tmp115 = tmp1 - tmp112 tmp116 = triton_helpers.maximum(tmp115, tmp16) tmp117 = tmp114 + tmp116 tmp118 = tmp3 - tmp112 tmp119 = triton_helpers.maximum(tmp118, tmp16) tmp120 = tmp117 + tmp119 tmp121 = tmp5 - tmp112 tmp122 = triton_helpers.maximum(tmp121, tmp16) tmp123 = tmp120 + tmp122 tmp124 = tmp123 - tmp7 tmp125 = tmp124 * tmp39 tmp126 = tmp125 >= tmp16 tmp127 = tl.where(tmp126, tmp112, tmp110) tmp128 = tmp111 * tmp12 tmp129 = tmp127 + tmp128 tmp130 = tmp0 - tmp129 tmp131 = triton_helpers.maximum(tmp130, tmp16) tmp132 = tmp1 - tmp129 tmp133 = triton_helpers.maximum(tmp132, tmp16) tmp134 = tmp131 + tmp133 tmp135 = tmp3 - tmp129 tmp136 = triton_helpers.maximum(tmp135, tmp16) tmp137 = tmp134 + tmp136 tmp138 = tmp5 - tmp129 tmp139 = triton_helpers.maximum(tmp138, tmp16) tmp140 = tmp137 + tmp139 tmp141 = tmp140 - tmp7 tmp142 = tmp141 * tmp39 tmp143 = tmp142 >= tmp16 tmp144 = tl.where(tmp143, tmp129, tmp127) tmp145 = tmp128 * tmp12 tmp146 = tmp144 + tmp145 tmp147 = tmp0 - tmp146 tmp148 = triton_helpers.maximum(tmp147, tmp16) tmp149 = tmp1 - tmp146 tmp150 = triton_helpers.maximum(tmp149, tmp16) tmp151 = tmp148 + tmp150 tmp152 = tmp3 - tmp146 tmp153 = triton_helpers.maximum(tmp152, tmp16) tmp154 = tmp151 + tmp153 tmp155 = tmp5 - tmp146 tmp156 = triton_helpers.maximum(tmp155, tmp16) tmp157 = tmp154 + tmp156 tmp158 = tmp157 - tmp7 tmp159 = tmp158 * tmp39 tmp160 = tmp159 >= tmp16 tmp161 = tl.where(tmp160, tmp146, tmp144) tmp162 = tmp145 * tmp12 tmp163 = tmp161 + tmp162 tmp164 = tmp0 - tmp163 tmp165 = triton_helpers.maximum(tmp164, tmp16) tmp166 = tmp1 - tmp163 tmp167 = triton_helpers.maximum(tmp166, tmp16) tmp168 = tmp165 + tmp167 tmp169 = tmp3 - tmp163 tmp170 = triton_helpers.maximum(tmp169, tmp16) tmp171 = tmp168 + tmp170 tmp172 = tmp5 - tmp163 tmp173 = triton_helpers.maximum(tmp172, tmp16) tmp174 = tmp171 + tmp173 tmp175 = tmp174 - tmp7 tmp176 = tmp175 * tmp39 tmp177 = tmp176 >= tmp16 tmp178 = tl.where(tmp177, tmp163, tmp161) tmp179 = tmp162 * tmp12 tmp180 = tmp178 + tmp179 tmp181 = tmp0 - tmp180 tmp182 = triton_helpers.maximum(tmp181, tmp16) tmp183 = tmp1 - tmp180 tmp184 = triton_helpers.maximum(tmp183, tmp16) tmp185 = tmp182 + tmp184 tmp186 = tmp3 - tmp180 tmp187 = triton_helpers.maximum(tmp186, tmp16) tmp188 = tmp185 + tmp187 tmp189 = tmp5 - tmp180 tmp190 = triton_helpers.maximum(tmp189, tmp16) tmp191 = tmp188 + tmp190 tmp192 = tmp191 - tmp7 tmp193 = tmp192 * tmp39 tmp194 = tmp193 >= tmp16 tmp195 = tl.where(tmp194, tmp180, tmp178) tmp196 = tmp179 * tmp12 tmp197 = tmp195 + tmp196 tmp198 = tmp0 - tmp197 tmp199 = triton_helpers.maximum(tmp198, tmp16) tmp200 = tmp1 - tmp197 tmp201 = triton_helpers.maximum(tmp200, tmp16) tmp202 = tmp199 + tmp201 tmp203 = tmp3 - tmp197 tmp204 = triton_helpers.maximum(tmp203, tmp16) tmp205 = tmp202 + tmp204 tmp206 = tmp5 - tmp197 tmp207 = triton_helpers.maximum(tmp206, tmp16) tmp208 = tmp205 + tmp207 tmp209 = tmp208 - tmp7 tmp210 = tmp209 * tmp39 tmp211 = tmp210 >= tmp16 tmp212 = tl.where(tmp211, tmp197, tmp195) tmp213 = tmp196 * tmp12 tmp214 = tmp212 + tmp213 tmp215 = tmp0 - tmp214 tmp216 = triton_helpers.maximum(tmp215, tmp16) tmp217 = tmp1 - tmp214 tmp218 = triton_helpers.maximum(tmp217, tmp16) tmp219 = tmp216 + tmp218 tmp220 = tmp3 - tmp214 tmp221 = triton_helpers.maximum(tmp220, tmp16) tmp222 = tmp219 + tmp221 tmp223 = tmp5 - tmp214 tmp224 = triton_helpers.maximum(tmp223, tmp16) tmp225 = tmp222 + tmp224 tmp226 = tmp225 - tmp7 tmp227 = tmp226 * tmp39 tmp228 = tmp227 >= tmp16 tmp229 = tl.where(tmp228, tmp214, tmp212) tmp230 = tmp213 * tmp12 tmp231 = tmp229 + tmp230 tmp232 = tmp0 - tmp231 tmp233 = triton_helpers.maximum(tmp232, tmp16) tmp234 = tmp1 - tmp231 tmp235 = triton_helpers.maximum(tmp234, tmp16) tmp236 = tmp233 + tmp235 tmp237 = tmp3 - tmp231 tmp238 = triton_helpers.maximum(tmp237, tmp16) tmp239 = tmp236 + tmp238 tmp240 = tmp5 - tmp231 tmp241 = triton_helpers.maximum(tmp240, tmp16) tmp242 = tmp239 + tmp241 tmp243 = tmp242 - tmp7 tmp244 = tmp243 * tmp39 tmp245 = tmp244 >= tmp16 tmp246 = tl.where(tmp245, tmp231, tmp229) tmp247 = tmp230 * tmp12 tmp248 = tmp246 + tmp247 tmp249 = tmp0 - tmp248 tmp250 = triton_helpers.maximum(tmp249, tmp16) tmp251 = tmp1 - tmp248 tmp252 = triton_helpers.maximum(tmp251, tmp16) tmp253 = tmp250 + tmp252 tmp254 = tmp3 - tmp248 tmp255 = triton_helpers.maximum(tmp254, tmp16) tmp256 = tmp253 + tmp255 tmp257 = tmp5 - tmp248 tmp258 = triton_helpers.maximum(tmp257, tmp16) tmp259 = tmp256 + tmp258 tmp260 = tmp259 - tmp7 tmp261 = tmp260 * tmp39 tmp262 = tmp261 >= tmp16 tmp263 = tl.where(tmp262, tmp248, tmp246) tmp264 = tmp247 * tmp12 tmp265 = tmp263 + tmp264 tmp266 = tmp0 - tmp265 tmp267 = triton_helpers.maximum(tmp266, tmp16) tmp268 = tmp1 - tmp265 tmp269 = triton_helpers.maximum(tmp268, tmp16) tmp270 = tmp267 + tmp269 tmp271 = tmp3 - tmp265 tmp272 = triton_helpers.maximum(tmp271, tmp16) tmp273 = tmp270 + tmp272 tmp274 = tmp5 - tmp265 tmp275 = triton_helpers.maximum(tmp274, tmp16) tmp276 = tmp273 + tmp275 tmp277 = tmp276 - tmp7 tmp278 = tmp277 * tmp39 tmp279 = tmp278 >= tmp16 tmp280 = tl.where(tmp279, tmp265, tmp263) tmp281 = tmp264 * tmp12 tmp282 = tmp280 + tmp281 tmp283 = tmp0 - tmp282 tmp284 = triton_helpers.maximum(tmp283, tmp16) tmp285 = tmp1 - tmp282 tmp286 = triton_helpers.maximum(tmp285, tmp16) tmp287 = tmp284 + tmp286 tmp288 = tmp3 - tmp282 tmp289 = triton_helpers.maximum(tmp288, tmp16) tmp290 = tmp287 + tmp289 tmp291 = tmp5 - tmp282 tmp292 = triton_helpers.maximum(tmp291, tmp16) tmp293 = tmp290 + tmp292 tmp294 = tmp293 - tmp7 tmp295 = tmp294 * tmp39 tmp296 = tmp295 >= tmp16 tmp297 = tl.where(tmp296, tmp282, tmp280) tmp298 = tmp281 * tmp12 tmp299 = tmp297 + tmp298 tmp300 = tmp0 - tmp299 tmp301 = triton_helpers.maximum(tmp300, tmp16) tmp302 = tmp1 - tmp299 tmp303 = triton_helpers.maximum(tmp302, tmp16) tmp304 = tmp301 + tmp303 tmp305 = tmp3 - tmp299 tmp306 = triton_helpers.maximum(tmp305, tmp16) tmp307 = tmp304 + tmp306 tmp308 = tmp5 - tmp299 tmp309 = triton_helpers.maximum(tmp308, tmp16) tmp310 = tmp307 + tmp309 tmp311 = tmp310 - tmp7 tmp312 = tmp311 * tmp39 tmp313 = tmp312 >= tmp16 tmp314 = tl.where(tmp313, tmp299, tmp297) tl.store(in_out_ptr8 + x0, tmp314, xmask) @triton.jit def triton_poi_fused_add_clamp_div_max_mul_sub_sum_where_1(in_out_ptr16, in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_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') tmp31 = tl.load(in_ptr1 + x0, xmask) tmp2 = triton_helpers.maximum(tmp0, tmp1) tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp6 = triton_helpers.maximum(tmp4, tmp5) tmp7 = 0.25 tmp8 = tmp6 - tmp7 tmp9 = 1.0 tmp10 = tmp6 - tmp9 tmp11 = tmp8 - tmp10 tmp12 = 0.5 tmp13 = tmp11 * tmp12 tmp14 = tmp13 * tmp12 tmp15 = tmp14 * tmp12 tmp16 = tmp15 * tmp12 tmp17 = tmp16 * tmp12 tmp18 = tmp17 * tmp12 tmp19 = tmp18 * tmp12 tmp20 = tmp19 * tmp12 tmp21 = tmp20 * tmp12 tmp22 = tmp21 * tmp12 tmp23 = tmp22 * tmp12 tmp24 = tmp23 * tmp12 tmp25 = tmp24 * tmp12 tmp26 = tmp25 * tmp12 tmp27 = tmp26 * tmp12 tmp28 = tmp27 * tmp12 tmp29 = tmp28 * tmp12 tmp30 = tmp29 * tmp12 tmp32 = tmp31 + tmp30 tmp33 = tmp0 - tmp32 tmp34 = 0.0 tmp35 = triton_helpers.maximum(tmp33, tmp34) tmp36 = tmp1 - tmp32 tmp37 = triton_helpers.maximum(tmp36, tmp34) tmp38 = tmp35 + tmp37 tmp39 = tmp3 - tmp32 tmp40 = triton_helpers.maximum(tmp39, tmp34) tmp41 = tmp38 + tmp40 tmp42 = tmp5 - tmp32 tmp43 = triton_helpers.maximum(tmp42, tmp34) tmp44 = tmp41 + tmp43 tmp45 = tmp44 - tmp9 tmp46 = tmp0 - tmp10 tmp47 = triton_helpers.maximum(tmp46, tmp34) tmp48 = tmp1 - tmp10 tmp49 = triton_helpers.maximum(tmp48, tmp34) tmp50 = tmp47 + tmp49 tmp51 = tmp3 - tmp10 tmp52 = triton_helpers.maximum(tmp51, tmp34) tmp53 = tmp50 + tmp52 tmp54 = tmp5 - tmp10 tmp55 = triton_helpers.maximum(tmp54, tmp34) tmp56 = tmp53 + tmp55 tmp57 = tmp56 - tmp9 tmp58 = tmp45 * tmp57 tmp59 = tmp58 >= tmp34 tmp60 = tl.where(tmp59, tmp32, tmp31) tmp61 = tmp30 * tmp12 tmp62 = tmp60 + tmp61 tmp63 = tmp0 - tmp62 tmp64 = triton_helpers.maximum(tmp63, tmp34) tmp65 = tmp1 - tmp62 tmp66 = triton_helpers.maximum(tmp65, tmp34) tmp67 = tmp64 + tmp66 tmp68 = tmp3 - tmp62 tmp69 = triton_helpers.maximum(tmp68, tmp34) tmp70 = tmp67 + tmp69 tmp71 = tmp5 - tmp62 tmp72 = triton_helpers.maximum(tmp71, tmp34) tmp73 = tmp70 + tmp72 tmp74 = tmp73 - tmp9 tmp75 = tmp74 * tmp57 tmp76 = tmp75 >= tmp34 tmp77 = tl.where(tmp76, tmp62, tmp60) tmp78 = tmp61 * tmp12 tmp79 = tmp77 + tmp78 tmp80 = tmp0 - tmp79 tmp81 = triton_helpers.maximum(tmp80, tmp34) tmp82 = tmp1 - tmp79 tmp83 = triton_helpers.maximum(tmp82, tmp34) tmp84 = tmp81 + tmp83 tmp85 = tmp3 - tmp79 tmp86 = triton_helpers.maximum(tmp85, tmp34) tmp87 = tmp84 + tmp86 tmp88 = tmp5 - tmp79 tmp89 = triton_helpers.maximum(tmp88, tmp34) tmp90 = tmp87 + tmp89 tmp91 = tmp90 - tmp9 tmp92 = tmp91 * tmp57 tmp93 = tmp92 >= tmp34 tmp94 = tl.where(tmp93, tmp79, tmp77) tmp95 = tmp78 * tmp12 tmp96 = tmp94 + tmp95 tmp97 = tmp0 - tmp96 tmp98 = triton_helpers.maximum(tmp97, tmp34) tmp99 = tmp1 - tmp96 tmp100 = triton_helpers.maximum(tmp99, tmp34) tmp101 = tmp98 + tmp100 tmp102 = tmp3 - tmp96 tmp103 = triton_helpers.maximum(tmp102, tmp34) tmp104 = tmp101 + tmp103 tmp105 = tmp5 - tmp96 tmp106 = triton_helpers.maximum(tmp105, tmp34) tmp107 = tmp104 + tmp106 tmp108 = tmp107 - tmp9 tmp109 = tmp108 * tmp57 tmp110 = tmp109 >= tmp34 tmp111 = tl.where(tmp110, tmp96, tmp94) tmp112 = tmp95 * tmp12 tmp113 = tmp111 + tmp112 tmp114 = tmp0 - tmp113 tmp115 = triton_helpers.maximum(tmp114, tmp34) tmp116 = tmp1 - tmp113 tmp117 = triton_helpers.maximum(tmp116, tmp34) tmp118 = tmp115 + tmp117 tmp119 = tmp3 - tmp113 tmp120 = triton_helpers.maximum(tmp119, tmp34) tmp121 = tmp118 + tmp120 tmp122 = tmp5 - tmp113 tmp123 = triton_helpers.maximum(tmp122, tmp34) tmp124 = tmp121 + tmp123 tmp125 = tmp124 - tmp9 tmp126 = tmp125 * tmp57 tmp127 = tmp126 >= tmp34 tmp128 = tl.where(tmp127, tmp113, tmp111) tmp129 = tmp112 * tmp12 tmp130 = tmp128 + tmp129 tmp131 = tmp0 - tmp130 tmp132 = triton_helpers.maximum(tmp131, tmp34) tmp133 = tmp1 - tmp130 tmp134 = triton_helpers.maximum(tmp133, tmp34) tmp135 = tmp132 + tmp134 tmp136 = tmp3 - tmp130 tmp137 = triton_helpers.maximum(tmp136, tmp34) tmp138 = tmp135 + tmp137 tmp139 = tmp5 - tmp130 tmp140 = triton_helpers.maximum(tmp139, tmp34) tmp141 = tmp138 + tmp140 tmp142 = tmp141 - tmp9 tmp143 = tmp142 * tmp57 tmp144 = tmp143 >= tmp34 tmp145 = tl.where(tmp144, tmp130, tmp128) tmp146 = tmp129 * tmp12 tmp147 = tmp145 + tmp146 tmp148 = tmp0 - tmp147 tmp149 = triton_helpers.maximum(tmp148, tmp34) tmp150 = tmp1 - tmp147 tmp151 = triton_helpers.maximum(tmp150, tmp34) tmp152 = tmp149 + tmp151 tmp153 = tmp3 - tmp147 tmp154 = triton_helpers.maximum(tmp153, tmp34) tmp155 = tmp152 + tmp154 tmp156 = tmp5 - tmp147 tmp157 = triton_helpers.maximum(tmp156, tmp34) tmp158 = tmp155 + tmp157 tmp159 = tmp158 - tmp9 tmp160 = tmp159 * tmp57 tmp161 = tmp160 >= tmp34 tmp162 = tl.where(tmp161, tmp147, tmp145) tmp163 = tmp146 * tmp12 tmp164 = tmp162 + tmp163 tmp165 = tmp0 - tmp164 tmp166 = triton_helpers.maximum(tmp165, tmp34) tmp167 = tmp1 - tmp164 tmp168 = triton_helpers.maximum(tmp167, tmp34) tmp169 = tmp166 + tmp168 tmp170 = tmp3 - tmp164 tmp171 = triton_helpers.maximum(tmp170, tmp34) tmp172 = tmp169 + tmp171 tmp173 = tmp5 - tmp164 tmp174 = triton_helpers.maximum(tmp173, tmp34) tmp175 = tmp172 + tmp174 tmp176 = tmp175 - tmp9 tmp177 = tmp176 * tmp57 tmp178 = tmp177 >= tmp34 tmp179 = tl.where(tmp178, tmp164, tmp162) tmp180 = tmp163 * tmp12 tmp181 = tmp179 + tmp180 tmp182 = tmp0 - tmp181 tmp183 = triton_helpers.maximum(tmp182, tmp34) tmp184 = tmp1 - tmp181 tmp185 = triton_helpers.maximum(tmp184, tmp34) tmp186 = tmp183 + tmp185 tmp187 = tmp3 - tmp181 tmp188 = triton_helpers.maximum(tmp187, tmp34) tmp189 = tmp186 + tmp188 tmp190 = tmp5 - tmp181 tmp191 = triton_helpers.maximum(tmp190, tmp34) tmp192 = tmp189 + tmp191 tmp193 = tmp192 - tmp9 tmp194 = tmp193 * tmp57 tmp195 = tmp194 >= tmp34 tmp196 = tl.where(tmp195, tmp181, tmp179) tmp197 = tmp180 * tmp12 tmp198 = tmp196 + tmp197 tmp199 = tmp0 - tmp198 tmp200 = triton_helpers.maximum(tmp199, tmp34) tmp201 = tmp1 - tmp198 tmp202 = triton_helpers.maximum(tmp201, tmp34) tmp203 = tmp200 + tmp202 tmp204 = tmp3 - tmp198 tmp205 = triton_helpers.maximum(tmp204, tmp34) tmp206 = tmp203 + tmp205 tmp207 = tmp5 - tmp198 tmp208 = triton_helpers.maximum(tmp207, tmp34) tmp209 = tmp206 + tmp208 tmp210 = tmp209 - tmp9 tmp211 = tmp210 * tmp57 tmp212 = tmp211 >= tmp34 tmp213 = tl.where(tmp212, tmp198, tmp196) tmp214 = tmp197 * tmp12 tmp215 = tmp213 + tmp214 tmp216 = tmp0 - tmp215 tmp217 = triton_helpers.maximum(tmp216, tmp34) tmp218 = tmp1 - tmp215 tmp219 = triton_helpers.maximum(tmp218, tmp34) tmp220 = tmp217 + tmp219 tmp221 = tmp3 - tmp215 tmp222 = triton_helpers.maximum(tmp221, tmp34) tmp223 = tmp220 + tmp222 tmp224 = tmp5 - tmp215 tmp225 = triton_helpers.maximum(tmp224, tmp34) tmp226 = tmp223 + tmp225 tmp227 = tmp226 - tmp9 tmp228 = tmp227 * tmp57 tmp229 = tmp228 >= tmp34 tmp230 = tl.where(tmp229, tmp215, tmp213) tmp231 = tmp214 * tmp12 tmp232 = tmp230 + tmp231 tmp233 = tmp0 - tmp232 tmp234 = triton_helpers.maximum(tmp233, tmp34) tmp235 = tmp1 - tmp232 tmp236 = triton_helpers.maximum(tmp235, tmp34) tmp237 = tmp234 + tmp236 tmp238 = tmp3 - tmp232 tmp239 = triton_helpers.maximum(tmp238, tmp34) tmp240 = tmp237 + tmp239 tmp241 = tmp5 - tmp232 tmp242 = triton_helpers.maximum(tmp241, tmp34) tmp243 = tmp240 + tmp242 tmp244 = tmp243 - tmp9 tmp245 = tmp244 * tmp57 tmp246 = tmp245 >= tmp34 tmp247 = tl.where(tmp246, tmp232, tmp230) tmp248 = tmp231 * tmp12 tmp249 = tmp247 + tmp248 tmp250 = tmp0 - tmp249 tmp251 = triton_helpers.maximum(tmp250, tmp34) tmp252 = tmp1 - tmp249 tmp253 = triton_helpers.maximum(tmp252, tmp34) tmp254 = tmp251 + tmp253 tmp255 = tmp3 - tmp249 tmp256 = triton_helpers.maximum(tmp255, tmp34) tmp257 = tmp254 + tmp256 tmp258 = tmp5 - tmp249 tmp259 = triton_helpers.maximum(tmp258, tmp34) tmp260 = tmp257 + tmp259 tmp261 = tmp260 - tmp9 tmp262 = tmp261 * tmp57 tmp263 = tmp262 >= tmp34 tmp264 = tl.where(tmp263, tmp249, tmp247) tmp265 = tmp248 * tmp12 tmp266 = tmp264 + tmp265 tmp267 = tmp0 - tmp266 tmp268 = triton_helpers.maximum(tmp267, tmp34) tmp269 = tmp1 - tmp266 tmp270 = triton_helpers.maximum(tmp269, tmp34) tmp271 = tmp268 + tmp270 tmp272 = tmp3 - tmp266 tmp273 = triton_helpers.maximum(tmp272, tmp34) tmp274 = tmp271 + tmp273 tmp275 = tmp5 - tmp266 tmp276 = triton_helpers.maximum(tmp275, tmp34) tmp277 = tmp274 + tmp276 tmp278 = tmp277 - tmp9 tmp279 = tmp278 * tmp57 tmp280 = tmp279 >= tmp34 tmp281 = tl.where(tmp280, tmp266, tmp264) tmp282 = tmp265 * tmp12 tmp283 = tmp281 + tmp282 tmp284 = tmp0 - tmp283 tmp285 = triton_helpers.maximum(tmp284, tmp34) tmp286 = tmp1 - tmp283 tmp287 = triton_helpers.maximum(tmp286, tmp34) tmp288 = tmp285 + tmp287 tmp289 = tmp3 - tmp283 tmp290 = triton_helpers.maximum(tmp289, tmp34) tmp291 = tmp288 + tmp290 tmp292 = tmp5 - tmp283 tmp293 = triton_helpers.maximum(tmp292, tmp34) tmp294 = tmp291 + tmp293 tmp295 = tmp294 - tmp9 tmp296 = tmp295 * tmp57 tmp297 = tmp296 >= tmp34 tmp298 = tl.where(tmp297, tmp283, tmp281) tmp299 = tmp282 * tmp12 tmp300 = tmp298 + tmp299 tmp301 = tmp0 - tmp300 tmp302 = triton_helpers.maximum(tmp301, tmp34) tmp303 = tmp1 - tmp300 tmp304 = triton_helpers.maximum(tmp303, tmp34) tmp305 = tmp302 + tmp304 tmp306 = tmp3 - tmp300 tmp307 = triton_helpers.maximum(tmp306, tmp34) tmp308 = tmp305 + tmp307 tmp309 = tmp5 - tmp300 tmp310 = triton_helpers.maximum(tmp309, tmp34) tmp311 = tmp308 + tmp310 tmp312 = tmp311 - tmp9 tmp313 = tmp312 * tmp57 tmp314 = tmp313 >= tmp34 tmp315 = tl.where(tmp314, tmp300, tmp298) tmp316 = tmp299 * tmp12 tmp317 = tmp315 + tmp316 tmp318 = tmp0 - tmp317 tmp319 = triton_helpers.maximum(tmp318, tmp34) tmp320 = tmp1 - tmp317 tmp321 = triton_helpers.maximum(tmp320, tmp34) tmp322 = tmp319 + tmp321 tmp323 = tmp3 - tmp317 tmp324 = triton_helpers.maximum(tmp323, tmp34) tmp325 = tmp322 + tmp324 tmp326 = tmp5 - tmp317 tmp327 = triton_helpers.maximum(tmp326, tmp34) tmp328 = tmp325 + tmp327 tmp329 = tmp328 - tmp9 tmp330 = tmp329 * tmp57 tmp331 = tmp330 >= tmp34 tmp332 = tl.where(tmp331, tmp317, tmp315) tmp333 = tmp316 * tmp12 tmp334 = tmp332 + tmp333 tmp335 = tmp0 - tmp334 tmp336 = triton_helpers.maximum(tmp335, tmp34) tmp337 = tmp1 - tmp334 tmp338 = triton_helpers.maximum(tmp337, tmp34) tmp339 = tmp336 + tmp338 tmp340 = tmp3 - tmp334 tmp341 = triton_helpers.maximum(tmp340, tmp34) tmp342 = tmp339 + tmp341 tmp343 = tmp5 - tmp334 tmp344 = triton_helpers.maximum(tmp343, tmp34) tmp345 = tmp342 + tmp344 tmp346 = tmp345 - tmp9 tmp347 = tmp346 * tmp57 tmp348 = tmp347 >= tmp34 tmp349 = tl.where(tmp348, tmp334, tmp332) tmp350 = tmp333 * tmp12 tmp351 = tmp349 + tmp350 tmp352 = tmp0 - tmp351 tmp353 = triton_helpers.maximum(tmp352, tmp34) tmp354 = tmp1 - tmp351 tmp355 = triton_helpers.maximum(tmp354, tmp34) tmp356 = tmp353 + tmp355 tmp357 = tmp3 - tmp351 tmp358 = triton_helpers.maximum(tmp357, tmp34) tmp359 = tmp356 + tmp358 tmp360 = tmp5 - tmp351 tmp361 = triton_helpers.maximum(tmp360, tmp34) tmp362 = tmp359 + tmp361 tmp363 = tmp362 - tmp9 tmp364 = tmp363 * tmp57 tmp365 = tmp364 >= tmp34 tmp366 = tl.where(tmp365, tmp351, tmp349) tmp367 = tmp350 * tmp12 tmp368 = tmp366 + tmp367 tmp369 = tmp0 - tmp368 tmp370 = triton_helpers.maximum(tmp369, tmp34) tmp371 = tmp1 - tmp368 tmp372 = triton_helpers.maximum(tmp371, tmp34) tmp373 = tmp370 + tmp372 tmp374 = tmp3 - tmp368 tmp375 = triton_helpers.maximum(tmp374, tmp34) tmp376 = tmp373 + tmp375 tmp377 = tmp5 - tmp368 tmp378 = triton_helpers.maximum(tmp377, tmp34) tmp379 = tmp376 + tmp378 tmp380 = tmp379 - tmp9 tmp381 = tmp380 * tmp57 tmp382 = tmp381 >= tmp34 tmp383 = tl.where(tmp382, tmp368, tmp366) tmp384 = tmp367 * tmp12 tmp385 = tmp383 + tmp384 tmp386 = tmp0 - tmp385 tmp387 = triton_helpers.maximum(tmp386, tmp34) tmp388 = tmp1 - tmp385 tmp389 = triton_helpers.maximum(tmp388, tmp34) tmp390 = tmp387 + tmp389 tmp391 = tmp3 - tmp385 tmp392 = triton_helpers.maximum(tmp391, tmp34) tmp393 = tmp390 + tmp392 tmp394 = tmp5 - tmp385 tmp395 = triton_helpers.maximum(tmp394, tmp34) tmp396 = tmp393 + tmp395 tmp397 = tmp396 - tmp9 tmp398 = tmp397 * tmp57 tmp399 = tmp398 >= tmp34 tmp400 = tl.where(tmp399, tmp385, tmp383) tmp401 = tmp384 * tmp12 tmp402 = tmp400 + tmp401 tmp403 = tmp0 - tmp402 tmp404 = triton_helpers.maximum(tmp403, tmp34) tmp405 = tmp1 - tmp402 tmp406 = triton_helpers.maximum(tmp405, tmp34) tmp407 = tmp404 + tmp406 tmp408 = tmp3 - tmp402 tmp409 = triton_helpers.maximum(tmp408, tmp34) tmp410 = tmp407 + tmp409 tmp411 = tmp5 - tmp402 tmp412 = triton_helpers.maximum(tmp411, tmp34) tmp413 = tmp410 + tmp412 tmp414 = tmp413 - tmp9 tmp415 = tmp414 * tmp57 tmp416 = tmp415 >= tmp34 tmp417 = tl.where(tmp416, tmp402, tmp400) tmp418 = tmp401 * tmp12 tmp419 = tmp417 + tmp418 tmp420 = tmp0 - tmp419 tmp421 = triton_helpers.maximum(tmp420, tmp34) tmp422 = tmp1 - tmp419 tmp423 = triton_helpers.maximum(tmp422, tmp34) tmp424 = tmp421 + tmp423 tmp425 = tmp3 - tmp419 tmp426 = triton_helpers.maximum(tmp425, tmp34) tmp427 = tmp424 + tmp426 tmp428 = tmp5 - tmp419 tmp429 = triton_helpers.maximum(tmp428, tmp34) tmp430 = tmp427 + tmp429 tmp431 = tmp430 - tmp9 tmp432 = tmp431 * tmp57 tmp433 = tmp432 >= tmp34 tmp434 = tl.where(tmp433, tmp419, tmp417) tl.store(out_ptr0 + x0, tmp30, xmask) tl.store(in_out_ptr16 + x0, tmp434, xmask) @triton.jit def triton_poi_fused_add_clamp_div_sub_2(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 x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp3 = 0.5 tmp4 = tmp2 * tmp3 tmp5 = tmp4 * tmp3 tmp6 = tmp5 * tmp3 tmp7 = tmp6 * tmp3 tmp8 = tmp7 * tmp3 tmp9 = tmp8 * tmp3 tmp10 = tmp9 * tmp3 tmp11 = tmp10 * tmp3 tmp12 = tmp11 * tmp3 tmp13 = tmp12 * tmp3 tmp14 = tmp13 * tmp3 tmp15 = tmp14 * tmp3 tmp16 = tmp15 * tmp3 tmp17 = tmp16 * tmp3 tmp18 = tmp17 * tmp3 tmp19 = tmp18 * tmp3 tmp20 = tmp19 * tmp3 tmp21 = tmp20 * tmp3 tmp22 = tmp21 * tmp3 tmp23 = tmp22 * tmp3 tmp24 = tmp23 * tmp3 tmp25 = tmp24 * tmp3 tmp26 = tmp25 * tmp3 tmp27 = tmp1 + tmp26 tmp28 = tmp0 - tmp27 tmp29 = 0.0 tmp30 = triton_helpers.maximum(tmp28, tmp29) tl.store(out_ptr0 + x2, tmp30, xmask) @triton.jit def triton_poi_fused_add_clamp_div_max_mul_sub_sum_where_3(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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') tmp9 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp14 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp32 = tl.load(in_out_ptr0 + x0, xmask) tmp33 = tl.load(in_ptr2 + x0, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 1.0 tmp8 = tmp6 - tmp7 tmp11 = triton_helpers.maximum(tmp9, tmp10) tmp13 = triton_helpers.maximum(tmp11, tmp12) tmp15 = triton_helpers.maximum(tmp13, tmp14) tmp16 = tmp15 - tmp7 tmp17 = tmp9 - tmp16 tmp18 = 0.0 tmp19 = triton_helpers.maximum(tmp17, tmp18) tmp20 = tmp10 - tmp16 tmp21 = triton_helpers.maximum(tmp20, tmp18) tmp22 = tmp19 + tmp21 tmp23 = tmp12 - tmp16 tmp24 = triton_helpers.maximum(tmp23, tmp18) tmp25 = tmp22 + tmp24 tmp26 = tmp14 - tmp16 tmp27 = triton_helpers.maximum(tmp26, tmp18) tmp28 = tmp25 + tmp27 tmp29 = tmp28 - tmp7 tmp30 = tmp8 * tmp29 tmp31 = tmp30 >= tmp18 tmp34 = 0.5 tmp35 = tmp33 * tmp34 tmp36 = tmp35 * tmp34 tmp37 = tmp36 * tmp34 tmp38 = tmp37 * tmp34 tmp39 = tmp38 * tmp34 tmp40 = tmp39 * tmp34 tmp41 = tmp40 * tmp34 tmp42 = tmp41 * tmp34 tmp43 = tmp42 * tmp34 tmp44 = tmp43 * tmp34 tmp45 = tmp44 * tmp34 tmp46 = tmp45 * tmp34 tmp47 = tmp46 * tmp34 tmp48 = tmp47 * tmp34 tmp49 = tmp48 * tmp34 tmp50 = tmp49 * tmp34 tmp51 = tmp50 * tmp34 tmp52 = tmp51 * tmp34 tmp53 = tmp52 * tmp34 tmp54 = tmp53 * tmp34 tmp55 = tmp54 * tmp34 tmp56 = tmp55 * tmp34 tmp57 = tmp56 * tmp34 tmp58 = tmp32 + tmp57 tmp59 = tl.where(tmp31, tmp58, tmp32) tl.store(in_out_ptr0 + x0, tmp59, xmask) @triton.jit def triton_poi_fused_add_clamp_div_sub_4(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 x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp3 = 0.5 tmp4 = tmp2 * tmp3 tmp5 = tmp4 * tmp3 tmp6 = tmp5 * tmp3 tmp7 = tmp6 * tmp3 tmp8 = tmp7 * tmp3 tmp9 = tmp8 * tmp3 tmp10 = tmp9 * tmp3 tmp11 = tmp10 * tmp3 tmp12 = tmp11 * tmp3 tmp13 = tmp12 * tmp3 tmp14 = tmp13 * tmp3 tmp15 = tmp14 * tmp3 tmp16 = tmp15 * tmp3 tmp17 = tmp16 * tmp3 tmp18 = tmp17 * tmp3 tmp19 = tmp18 * tmp3 tmp20 = tmp19 * tmp3 tmp21 = tmp20 * tmp3 tmp22 = tmp21 * tmp3 tmp23 = tmp22 * tmp3 tmp24 = tmp23 * tmp3 tmp25 = tmp24 * tmp3 tmp26 = tmp25 * tmp3 tmp27 = tmp26 * tmp3 tmp28 = tmp1 + tmp27 tmp29 = tmp0 - tmp28 tmp30 = 0.0 tmp31 = triton_helpers.maximum(tmp29, tmp30) tl.store(out_ptr0 + x2, tmp31, xmask) @triton.jit def triton_poi_fused_add_clamp_div_max_mul_sub_sum_where_5(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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') tmp9 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp14 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp32 = tl.load(in_out_ptr0 + x0, xmask) tmp33 = tl.load(in_ptr2 + x0, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 1.0 tmp8 = tmp6 - tmp7 tmp11 = triton_helpers.maximum(tmp9, tmp10) tmp13 = triton_helpers.maximum(tmp11, tmp12) tmp15 = triton_helpers.maximum(tmp13, tmp14) tmp16 = tmp15 - tmp7 tmp17 = tmp9 - tmp16 tmp18 = 0.0 tmp19 = triton_helpers.maximum(tmp17, tmp18) tmp20 = tmp10 - tmp16 tmp21 = triton_helpers.maximum(tmp20, tmp18) tmp22 = tmp19 + tmp21 tmp23 = tmp12 - tmp16 tmp24 = triton_helpers.maximum(tmp23, tmp18) tmp25 = tmp22 + tmp24 tmp26 = tmp14 - tmp16 tmp27 = triton_helpers.maximum(tmp26, tmp18) tmp28 = tmp25 + tmp27 tmp29 = tmp28 - tmp7 tmp30 = tmp8 * tmp29 tmp31 = tmp30 >= tmp18 tmp34 = 0.5 tmp35 = tmp33 * tmp34 tmp36 = tmp35 * tmp34 tmp37 = tmp36 * tmp34 tmp38 = tmp37 * tmp34 tmp39 = tmp38 * tmp34 tmp40 = tmp39 * tmp34 tmp41 = tmp40 * tmp34 tmp42 = tmp41 * tmp34 tmp43 = tmp42 * tmp34 tmp44 = tmp43 * tmp34 tmp45 = tmp44 * tmp34 tmp46 = tmp45 * tmp34 tmp47 = tmp46 * tmp34 tmp48 = tmp47 * tmp34 tmp49 = tmp48 * tmp34 tmp50 = tmp49 * tmp34 tmp51 = tmp50 * tmp34 tmp52 = tmp51 * tmp34 tmp53 = tmp52 * tmp34 tmp54 = tmp53 * tmp34 tmp55 = tmp54 * tmp34 tmp56 = tmp55 * tmp34 tmp57 = tmp56 * tmp34 tmp58 = tmp57 * tmp34 tmp59 = tmp32 + tmp58 tmp60 = tl.where(tmp31, tmp59, tmp32) tl.store(in_out_ptr0 + x0, tmp60, xmask) @triton.jit def triton_poi_fused_add_div_sub_6(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp3 = 0.5 tmp4 = tmp2 * tmp3 tmp5 = tmp4 * tmp3 tmp6 = tmp5 * tmp3 tmp7 = tmp6 * tmp3 tmp8 = tmp7 * tmp3 tmp9 = tmp8 * tmp3 tmp10 = tmp9 * tmp3 tmp11 = tmp10 * tmp3 tmp12 = tmp11 * tmp3 tmp13 = tmp12 * tmp3 tmp14 = tmp13 * tmp3 tmp15 = tmp14 * tmp3 tmp16 = tmp15 * tmp3 tmp17 = tmp16 * tmp3 tmp18 = tmp17 * tmp3 tmp19 = tmp18 * tmp3 tmp20 = tmp19 * tmp3 tmp21 = tmp20 * tmp3 tmp22 = tmp21 * tmp3 tmp23 = tmp22 * tmp3 tmp24 = tmp23 * tmp3 tmp25 = tmp24 * tmp3 tmp26 = tmp25 * tmp3 tmp27 = tmp26 * tmp3 tmp28 = tmp27 * tmp3 tmp29 = tmp1 + tmp28 tmp30 = tmp0 - tmp29 tl.store(out_ptr0 + x2, tmp30, xmask) @triton.jit def triton_poi_fused_add_clamp_div_max_mul_sub_sum_where_7(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp14 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp15 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp17 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp19 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp36 = tl.load(in_out_ptr0 + x0, xmask) tmp37 = tl.load(in_ptr2 + x0, xmask) tmp1 = 0.0 tmp2 = triton_helpers.maximum(tmp0, tmp1) tmp4 = triton_helpers.maximum(tmp3, tmp1) tmp5 = tmp2 + tmp4 tmp7 = triton_helpers.maximum(tmp6, tmp1) tmp8 = tmp5 + tmp7 tmp10 = triton_helpers.maximum(tmp9, tmp1) tmp11 = tmp8 + tmp10 tmp12 = 1.0 tmp13 = tmp11 - tmp12 tmp16 = triton_helpers.maximum(tmp14, tmp15) tmp18 = triton_helpers.maximum(tmp16, tmp17) tmp20 = triton_helpers.maximum(tmp18, tmp19) tmp21 = tmp20 - tmp12 tmp22 = tmp14 - tmp21 tmp23 = triton_helpers.maximum(tmp22, tmp1) tmp24 = tmp15 - tmp21 tmp25 = triton_helpers.maximum(tmp24, tmp1) tmp26 = tmp23 + tmp25 tmp27 = tmp17 - tmp21 tmp28 = triton_helpers.maximum(tmp27, tmp1) tmp29 = tmp26 + tmp28 tmp30 = tmp19 - tmp21 tmp31 = triton_helpers.maximum(tmp30, tmp1) tmp32 = tmp29 + tmp31 tmp33 = tmp32 - tmp12 tmp34 = tmp13 * tmp33 tmp35 = tmp34 >= tmp1 tmp38 = 0.5 tmp39 = tmp37 * tmp38 tmp40 = tmp39 * tmp38 tmp41 = tmp40 * tmp38 tmp42 = tmp41 * tmp38 tmp43 = tmp42 * tmp38 tmp44 = tmp43 * tmp38 tmp45 = tmp44 * tmp38 tmp46 = tmp45 * tmp38 tmp47 = tmp46 * tmp38 tmp48 = tmp47 * tmp38 tmp49 = tmp48 * tmp38 tmp50 = tmp49 * tmp38 tmp51 = tmp50 * tmp38 tmp52 = tmp51 * tmp38 tmp53 = tmp52 * tmp38 tmp54 = tmp53 * tmp38 tmp55 = tmp54 * tmp38 tmp56 = tmp55 * tmp38 tmp57 = tmp56 * tmp38 tmp58 = tmp57 * tmp38 tmp59 = tmp58 * tmp38 tmp60 = tmp59 * tmp38 tmp61 = tmp60 * tmp38 tmp62 = tmp61 * tmp38 tmp63 = tmp62 * tmp38 tmp64 = tmp36 + tmp63 tmp65 = tl.where(tmp35, tmp64, tmp36) tl.store(in_out_ptr0 + x0, tmp65, xmask) @triton.jit def triton_poi_fused_add_div_sub_8(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 x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp3 = 0.5 tmp4 = tmp2 * tmp3 tmp5 = tmp4 * tmp3 tmp6 = tmp5 * tmp3 tmp7 = tmp6 * tmp3 tmp8 = tmp7 * tmp3 tmp9 = tmp8 * tmp3 tmp10 = tmp9 * tmp3 tmp11 = tmp10 * tmp3 tmp12 = tmp11 * tmp3 tmp13 = tmp12 * tmp3 tmp14 = tmp13 * tmp3 tmp15 = tmp14 * tmp3 tmp16 = tmp15 * tmp3 tmp17 = tmp16 * tmp3 tmp18 = tmp17 * tmp3 tmp19 = tmp18 * tmp3 tmp20 = tmp19 * tmp3 tmp21 = tmp20 * tmp3 tmp22 = tmp21 * tmp3 tmp23 = tmp22 * tmp3 tmp24 = tmp23 * tmp3 tmp25 = tmp24 * tmp3 tmp26 = tmp25 * tmp3 tmp27 = tmp26 * tmp3 tmp28 = tmp27 * tmp3 tmp29 = tmp28 * tmp3 tmp30 = tmp1 + tmp29 tmp31 = tmp0 - tmp30 tl.store(out_ptr0 + x2, tmp31, xmask) @triton.jit def triton_poi_fused_add_clamp_div_max_mul_sub_sum_where_9(in_out_ptr0, in_out_ptr2, in_ptr0, in_ptr1, in_ptr2, out_ptr4, out_ptr7, 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') tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp14 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp15 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp17 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp19 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp36 = tl.load(in_out_ptr0 + x0, xmask) tmp37 = tl.load(in_ptr2 + x0, xmask) tmp1 = 0.0 tmp2 = triton_helpers.maximum(tmp0, tmp1) tmp4 = triton_helpers.maximum(tmp3, tmp1) tmp5 = tmp2 + tmp4 tmp7 = triton_helpers.maximum(tmp6, tmp1) tmp8 = tmp5 + tmp7 tmp10 = triton_helpers.maximum(tmp9, tmp1) tmp11 = tmp8 + tmp10 tmp12 = 1.0 tmp13 = tmp11 - tmp12 tmp16 = triton_helpers.maximum(tmp14, tmp15) tmp18 = triton_helpers.maximum(tmp16, tmp17) tmp20 = triton_helpers.maximum(tmp18, tmp19) tmp21 = tmp20 - tmp12 tmp22 = tmp14 - tmp21 tmp23 = triton_helpers.maximum(tmp22, tmp1) tmp24 = tmp15 - tmp21 tmp25 = triton_helpers.maximum(tmp24, tmp1) tmp26 = tmp23 + tmp25 tmp27 = tmp17 - tmp21 tmp28 = triton_helpers.maximum(tmp27, tmp1) tmp29 = tmp26 + tmp28 tmp30 = tmp19 - tmp21 tmp31 = triton_helpers.maximum(tmp30, tmp1) tmp32 = tmp29 + tmp31 tmp33 = tmp32 - tmp12 tmp34 = tmp13 * tmp33 tmp35 = tmp34 >= tmp1 tmp38 = 0.5 tmp39 = tmp37 * tmp38 tmp40 = tmp39 * tmp38 tmp41 = tmp40 * tmp38 tmp42 = tmp41 * tmp38 tmp43 = tmp42 * tmp38 tmp44 = tmp43 * tmp38 tmp45 = tmp44 * tmp38 tmp46 = tmp45 * tmp38 tmp47 = tmp46 * tmp38 tmp48 = tmp47 * tmp38 tmp49 = tmp48 * tmp38 tmp50 = tmp49 * tmp38 tmp51 = tmp50 * tmp38 tmp52 = tmp51 * tmp38 tmp53 = tmp52 * tmp38 tmp54 = tmp53 * tmp38 tmp55 = tmp54 * tmp38 tmp56 = tmp55 * tmp38 tmp57 = tmp56 * tmp38 tmp58 = tmp57 * tmp38 tmp59 = tmp58 * tmp38 tmp60 = tmp59 * tmp38 tmp61 = tmp60 * tmp38 tmp62 = tmp61 * tmp38 tmp63 = tmp62 * tmp38 tmp64 = tmp63 * tmp38 tmp65 = tmp36 + tmp64 tmp66 = tl.where(tmp35, tmp65, tmp36) tmp67 = tmp64 * tmp38 tmp68 = tmp66 + tmp67 tmp69 = tmp14 - tmp68 tmp70 = triton_helpers.maximum(tmp69, tmp1) tmp71 = tmp15 - tmp68 tmp72 = triton_helpers.maximum(tmp71, tmp1) tmp73 = tmp70 + tmp72 tmp74 = tmp17 - tmp68 tmp75 = triton_helpers.maximum(tmp74, tmp1) tmp76 = tmp73 + tmp75 tmp77 = tmp19 - tmp68 tmp78 = triton_helpers.maximum(tmp77, tmp1) tmp79 = tmp76 + tmp78 tmp80 = tmp79 - tmp12 tmp81 = tmp80 * tmp33 tmp82 = tmp81 >= tmp1 tmp83 = tl.where(tmp82, tmp68, tmp66) tmp84 = tmp67 * tmp38 tmp85 = tmp83 + tmp84 tmp86 = tmp14 - tmp85 tmp87 = triton_helpers.maximum(tmp86, tmp1) tmp88 = tmp15 - tmp85 tmp89 = triton_helpers.maximum(tmp88, tmp1) tmp90 = tmp87 + tmp89 tmp91 = tmp17 - tmp85 tmp92 = triton_helpers.maximum(tmp91, tmp1) tmp93 = tmp90 + tmp92 tmp94 = tmp19 - tmp85 tmp95 = triton_helpers.maximum(tmp94, tmp1) tmp96 = tmp93 + tmp95 tmp97 = tmp96 - tmp12 tmp98 = tmp97 * tmp33 tmp99 = tmp98 >= tmp1 tmp100 = tl.where(tmp99, tmp85, tmp83) tmp101 = tmp84 * tmp38 tmp102 = tmp100 + tmp101 tmp103 = tmp14 - tmp102 tmp104 = triton_helpers.maximum(tmp103, tmp1) tmp105 = tmp15 - tmp102 tmp106 = triton_helpers.maximum(tmp105, tmp1) tmp107 = tmp104 + tmp106 tmp108 = tmp17 - tmp102 tmp109 = triton_helpers.maximum(tmp108, tmp1) tmp110 = tmp107 + tmp109 tmp111 = tmp19 - tmp102 tmp112 = triton_helpers.maximum(tmp111, tmp1) tmp113 = tmp110 + tmp112 tmp114 = tmp113 - tmp12 tmp115 = tmp114 * tmp33 tmp116 = tmp115 >= tmp1 tmp117 = tl.where(tmp116, tmp102, tmp100) tmp118 = tmp101 * tmp38 tmp119 = tmp117 + tmp118 tmp120 = tmp14 - tmp119 tmp121 = triton_helpers.maximum(tmp120, tmp1) tmp122 = tmp15 - tmp119 tmp123 = triton_helpers.maximum(tmp122, tmp1) tmp124 = tmp121 + tmp123 tmp125 = tmp17 - tmp119 tmp126 = triton_helpers.maximum(tmp125, tmp1) tmp127 = tmp124 + tmp126 tmp128 = tmp19 - tmp119 tmp129 = triton_helpers.maximum(tmp128, tmp1) tmp130 = tmp127 + tmp129 tmp131 = tmp130 - tmp12 tmp132 = tmp131 * tmp33 tmp133 = tmp132 >= tmp1 tmp134 = tl.where(tmp133, tmp119, tmp117) tmp135 = tmp118 * tmp38 tmp136 = tmp134 + tmp135 tmp137 = tmp14 - tmp136 tmp138 = triton_helpers.maximum(tmp137, tmp1) tmp139 = tmp15 - tmp136 tmp140 = triton_helpers.maximum(tmp139, tmp1) tmp141 = tmp138 + tmp140 tmp142 = tmp17 - tmp136 tmp143 = triton_helpers.maximum(tmp142, tmp1) tmp144 = tmp141 + tmp143 tmp145 = tmp19 - tmp136 tmp146 = triton_helpers.maximum(tmp145, tmp1) tmp147 = tmp144 + tmp146 tmp148 = tmp147 - tmp12 tmp149 = tmp148 * tmp33 tmp150 = tmp149 >= tmp1 tmp151 = tl.where(tmp150, tmp136, tmp134) tmp152 = tmp135 * tmp38 tmp153 = tmp151 + tmp152 tmp154 = tmp14 - tmp153 tmp155 = triton_helpers.maximum(tmp154, tmp1) tmp156 = tmp15 - tmp153 tmp157 = triton_helpers.maximum(tmp156, tmp1) tmp158 = tmp155 + tmp157 tmp159 = tmp17 - tmp153 tmp160 = triton_helpers.maximum(tmp159, tmp1) tmp161 = tmp158 + tmp160 tmp162 = tmp19 - tmp153 tmp163 = triton_helpers.maximum(tmp162, tmp1) tmp164 = tmp161 + tmp163 tl.store(out_ptr4 + x0, tmp101, xmask) tl.store(in_out_ptr2 + x0, tmp151, xmask) tl.store(out_ptr7 + x0, tmp164, xmask) @triton.jit def triton_poi_fused_add_clamp_div_sub_10(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp3 = 0.5 tmp4 = tmp2 * tmp3 tmp5 = tmp4 * tmp3 tmp6 = tmp5 * tmp3 tmp7 = tmp1 + tmp6 tmp8 = tmp0 - tmp7 tmp9 = 0.0 tmp10 = triton_helpers.maximum(tmp8, tmp9) tmp12 = tmp10 / tmp11 tl.store(out_ptr0 + x2, tmp12, 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) buf40 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) buf41 = reinterpret_tensor(buf40, (4, 4, 4, 1), (16, 4, 1, 64), 0) del buf40 get_raw_stream(0) triton_poi_fused_add_clamp_div_max_mul_sub_sum_where_0[grid(64)](buf41, arg0_1, 64, XBLOCK=64, num_warps=1, num_stages=1) buf42 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf81 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) buf82 = reinterpret_tensor(buf81, (4, 4, 4, 1), (16, 4, 1, 64), 0) del buf81 triton_poi_fused_add_clamp_div_max_mul_sub_sum_where_1[grid(64)](buf82, arg0_1, buf41, buf42, 64, XBLOCK=64, num_warps=1, num_stages=1) buf83 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_add_clamp_div_sub_2[grid(256)](arg0_1, buf82, buf42, buf83, 256, XBLOCK=256, num_warps=4, num_stages=1) buf85 = buf82 del buf82 triton_poi_fused_add_clamp_div_max_mul_sub_sum_where_3[grid(64)](buf85, buf83, arg0_1, buf42, 64, XBLOCK=64, num_warps=1, num_stages=1) buf86 = buf83 del buf83 triton_poi_fused_add_clamp_div_sub_4[grid(256)](arg0_1, buf85, buf42, buf86, 256, XBLOCK=256, num_warps=4, num_stages=1) buf88 = buf85 del buf85 triton_poi_fused_add_clamp_div_max_mul_sub_sum_where_5[grid(64)](buf88, buf86, arg0_1, buf42, 64, XBLOCK=64, num_warps=1, num_stages=1) buf89 = buf86 del buf86 triton_poi_fused_add_div_sub_6[grid(256)](arg0_1, buf88, buf42, buf89, 256, XBLOCK=256, num_warps=4, num_stages=1) buf91 = buf88 del buf88 triton_poi_fused_add_clamp_div_max_mul_sub_sum_where_7[grid(64)](buf91, buf89, arg0_1, buf42, 64, XBLOCK=64, num_warps=1, num_stages=1) buf92 = buf89 del buf89 triton_poi_fused_add_div_sub_8[grid(256)](arg0_1, buf91, buf42, buf92, 256, XBLOCK=256, num_warps=4, num_stages=1) buf94 = buf91 del buf91 buf100 = buf41 del buf41 buf103 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) buf104 = reinterpret_tensor(buf103, (4, 4, 4, 1), (16, 4, 1, 64), 0) del buf103 buf105 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_add_clamp_div_max_mul_sub_sum_where_9[grid(64)](buf94, buf104, buf92, arg0_1, buf42, buf100, buf105, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf42 del buf94 buf106 = buf92 del buf92 triton_poi_fused_add_clamp_div_sub_10[grid(256)](arg0_1, buf104, buf100, buf105, buf106, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del buf100 del buf104 del buf105 return buf106, def sparsemax_bisect(X, dim=-1, n_iter=50, ensure_sum_one=True): """sparsemax: normalizing sparse transform (a la softmax), via bisection. Solves the projection: min_p ||x - p||_2 s.t. p >= 0, sum(p) == 1. Parameters ---------- X : torch.Tensor The input tensor. dim : int The dimension along which to apply sparsemax. n_iter : int Number of bisection iterations. For float32, 24 iterations should suffice for machine precision. ensure_sum_one : bool, Whether to divide the result by its sum. If false, the result might sum to close but not exactly 1, which might cause downstream problems. Note: This function does not yet support normalizing along anything except the last dimension. Please use transposing and views to achieve more general behavior. Returns ------- P : torch tensor, same shape as X The projection result, such that P.sum(dim=dim) == 1 elementwise. """ return SparsemaxBisectFunction.apply(X, dim, n_iter, ensure_sum_one) class EntmaxBisectFunction(Function): @classmethod def _gp(cls, x, alpha): return x ** (alpha - 1) @classmethod def _gp_inv(cls, y, alpha): return y ** (1 / (alpha - 1)) @classmethod def _p(cls, X, alpha): return cls._gp_inv(torch.clamp(X, min=0), alpha) @classmethod def forward(cls, ctx, X, alpha=1.5, dim=-1, n_iter=50, ensure_sum_one=True ): if not isinstance(alpha, torch.Tensor): alpha = torch.tensor(alpha, dtype=X.dtype, device=X.device) alpha_shape = list(X.shape) alpha_shape[dim] = 1 alpha = alpha.expand(*alpha_shape) ctx.alpha = alpha ctx.dim = dim d = X.shape[dim] X = X * (alpha - 1) max_val, _ = X.max(dim=dim, keepdim=True) tau_lo = max_val - cls._gp(1, alpha) tau_hi = max_val - cls._gp(1 / d, alpha) f_lo = cls._p(X - tau_lo, alpha).sum(dim) - 1 dm = tau_hi - tau_lo for it in range(n_iter): dm /= 2 tau_m = tau_lo + dm p_m = cls._p(X - tau_m, alpha) f_m = p_m.sum(dim) - 1 mask = (f_m * f_lo >= 0).unsqueeze(dim) tau_lo = torch.where(mask, tau_m, tau_lo) if ensure_sum_one: p_m /= p_m.sum(dim=dim).unsqueeze(dim=dim) ctx.save_for_backward(p_m) return p_m @classmethod def backward(cls, ctx, dY): Y, = ctx.saved_tensors gppr = torch.where(Y > 0, Y ** (2 - ctx.alpha), Y.new_zeros(1)) dX = dY * gppr q = dX.sum(ctx.dim) / gppr.sum(ctx.dim) q = q.unsqueeze(ctx.dim) dX -= q * gppr d_alpha = None if ctx.needs_input_grad[1]: S = torch.where(Y > 0, Y * torch.log(Y), Y.new_zeros(1)) ent = S.sum(ctx.dim).unsqueeze(ctx.dim) Y_skewed = gppr / gppr.sum(ctx.dim).unsqueeze(ctx.dim) d_alpha = dY * (Y - Y_skewed) / (ctx.alpha - 1) ** 2 d_alpha -= dY * (S - Y_skewed * ent) / (ctx.alpha - 1) d_alpha = d_alpha.sum(ctx.dim).unsqueeze(ctx.dim) return dX, d_alpha, None, None, None class SparsemaxBisectFunction(EntmaxBisectFunction): @classmethod def _gp(cls, x, alpha): return x @classmethod def _gp_inv(cls, y, alpha): return y @classmethod def _p(cls, x, alpha): return torch.clamp(x, min=0) @classmethod def forward(cls, ctx, X, dim=-1, n_iter=50, ensure_sum_one=True): return super().forward(ctx, X, alpha=2, dim=dim, n_iter=50, ensure_sum_one=True) @classmethod def backward(cls, ctx, dY): Y, = ctx.saved_tensors gppr = Y > 0 dX = dY * gppr q = dX.sum(ctx.dim) / gppr.sum(ctx.dim) q = q.unsqueeze(ctx.dim) dX -= q * gppr return dX, None, None, None class SparsemaxBisectNew(nn.Module): def __init__(self, dim=-1, n_iter=None): """sparsemax: normalizing sparse transform (a la softmax) via bisection Solves the projection: min_p ||x - p||_2 s.t. p >= 0, sum(p) == 1. Parameters ---------- dim : int The dimension along which to apply sparsemax. n_iter : int Number of bisection iterations. For float32, 24 iterations should suffice for machine precision. """ self.dim = dim self.n_iter = n_iter super().__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
mtreviso/entmax
SparsemaxBisect
false
10,645
[ "MIT" ]
0
5b029d07fe00d7aacc77c8e684a5796d29287575
https://github.com/mtreviso/entmax/tree/5b029d07fe00d7aacc77c8e684a5796d29287575
AddNet
import torch import torch.nn.functional class AddNet(torch.nn.Module): def __init__(self): super(AddNet, self).__init__() self.conv1 = torch.nn.Conv2d(3, 4, kernel_size=1, stride=1) self.conv2 = torch.nn.Conv2d(3, 4, kernel_size=1, stride=1) def forward(self, x, y): x = self.conv1(x) x = x + 3 y = self.conv2(y) return x - y, y - x, x + y def get_inputs(): return [torch.rand([4, 3, 64, 64]), 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 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_add_convolution_sub_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 4 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr2 + x3, None) tmp6 = tl.load(in_ptr3 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 3.0 tmp4 = tmp2 + tmp3 tmp7 = tmp5 + tmp6 tmp8 = tmp4 - tmp7 tmp9 = tmp7 - tmp4 tmp10 = tmp4 + tmp7 tl.store(out_ptr0 + x3, tmp8, None) tl.store(out_ptr1 + x3, tmp9, None) tl.store(out_ptr2 + x3, tmp10, None) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args args.clear() assert_size_stride(primals_1, (4, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 3, 64, 64), (12288, 4096, 64, 1)) assert_size_stride(primals_4, (4, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 3, 64, 64), (12288, 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, 64, 64), (16384, 4096, 64, 1)) buf1 = extern_kernels.convolution(primals_6, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 64, 64), (16384, 4096, 64, 1)) buf2 = empty_strided_cuda((4, 4, 64, 64), (16384, 4096, 64, 1), torch.float32) buf3 = empty_strided_cuda((4, 4, 64, 64), (16384, 4096, 64, 1), torch.float32) buf4 = empty_strided_cuda((4, 4, 64, 64), (16384, 4096, 64, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_convolution_sub_0[grid(65536)](buf0, primals_2, buf1, primals_5, buf2, buf3, buf4, 65536, XBLOCK=512, num_warps =4, num_stages=1) del buf0 del buf1 del primals_2 del primals_5 return buf2, buf3, buf4, primals_1, primals_3, primals_4, primals_6 class AddNetNew(torch.nn.Module): def __init__(self): super(AddNetNew, self).__init__() self.conv1 = torch.nn.Conv2d(3, 4, kernel_size=1, stride=1) self.conv2 = torch.nn.Conv2d(3, 4, kernel_size=1, stride=1) def forward(self, input_0, input_1): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.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], output[1], output[2]
elad-c/model_optimization
AddNet
false
10,646
[ "Apache-2.0" ]
0
b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
https://github.com/elad-c/model_optimization/tree/b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
UNet
import torch from torch import nn import torch.nn.functional as F from torchvision import models class UNet(nn.Module): """ The U-Net Convolutional Neural Network for semantic segmentation Source material for the algorithm: https://link.springer.com/chapter/10.1007%2F978-3-319-24574-4_28 """ def __init__(self, in_channels=1, num_classes=2, pretrained=False, padding=1): """ Arguments: in_channels (int): the number of channels in the input image num_classes (int, optional): the number of classes to be predicted. Note: The background is always considered as 1 class. (By default predicts the background and foreground: 2 classes). pretrained (bool, optional): whether to use VGG13 encoders pretrained on ImageNet for the first 8 convolutional layers in the encoder section of the U-Net model padding (int, optional): the padding applied on images at convolutional layers. A padding of 1 helps keep the original input image's height and witdh. Note: pretrained VGG13 encoders use a padding of 1 """ super().__init__() if pretrained: encoder = models.vgg13(pretrained=True).features self.conv1_input = encoder[0] self.conv1 = encoder[2] self.conv2_input = encoder[5] self.conv2 = encoder[7] self.conv3_input = encoder[10] self.conv3 = encoder[12] self.conv4_input = encoder[15] self.conv4 = encoder[17] else: self.conv1_input = nn.Conv2d(in_channels, 64, 3, padding=padding) self.conv1 = nn.Conv2d(64, 64, 3, padding=padding) self.conv2_input = nn.Conv2d(64, 128, 3, padding=padding) self.conv2 = nn.Conv2d(128, 128, 3, padding=padding) self.conv3_input = nn.Conv2d(128, 256, 3, padding=padding) self.conv3 = nn.Conv2d(256, 256, 3, padding=padding) self.conv4_input = nn.Conv2d(256, 512, 3, padding=padding) self.conv4 = nn.Conv2d(512, 512, 3, padding=padding) self.conv5_input = nn.Conv2d(512, 1024, 3, padding=padding) self.conv5 = nn.Conv2d(1024, 1024, 3, padding=padding) self.conv6_up = nn.ConvTranspose2d(1024, 512, 2, 2) self.conv6_input = nn.Conv2d(1024, 512, 3, padding=padding) self.conv6 = nn.Conv2d(512, 512, 3, padding=padding) self.conv7_up = nn.ConvTranspose2d(512, 256, 2, 2) self.conv7_input = nn.Conv2d(512, 256, 3, padding=padding) self.conv7 = nn.Conv2d(256, 256, 3, padding=padding) self.conv8_up = nn.ConvTranspose2d(256, 128, 2, 2) self.conv8_input = nn.Conv2d(256, 128, 3, padding=padding) self.conv8 = nn.Conv2d(128, 128, 3, padding=padding) self.conv9_up = nn.ConvTranspose2d(128, 64, 2, 2) self.conv9_input = nn.Conv2d(128, 64, 3, padding=padding) self.conv9 = nn.Conv2d(64, 64, 3, padding=padding) self.conv9_output = nn.Conv2d(64, num_classes, 1) def forward(self, x): layer1 = F.relu(self.conv1_input(x)) layer1 = F.relu(self.conv1(layer1)) layer2 = F.max_pool2d(layer1, 2) layer2 = F.relu(self.conv2_input(layer2)) layer2 = F.relu(self.conv2(layer2)) layer3 = F.max_pool2d(layer2, 2) layer3 = F.relu(self.conv3_input(layer3)) layer3 = F.relu(self.conv3(layer3)) layer4 = F.max_pool2d(layer3, 2) layer4 = F.relu(self.conv4_input(layer4)) layer4 = F.relu(self.conv4(layer4)) layer5 = F.max_pool2d(layer4, 2) layer5 = F.relu(self.conv5_input(layer5)) layer5 = F.relu(self.conv5(layer5)) layer6 = F.relu(self.conv6_up(layer5)) layer6 = torch.cat((layer4, layer6), 1) layer6 = F.relu(self.conv6_input(layer6)) layer6 = F.relu(self.conv6(layer6)) layer7 = F.relu(self.conv7_up(layer6)) layer7 = torch.cat((layer3, layer7), 1) layer7 = F.relu(self.conv7_input(layer7)) layer7 = F.relu(self.conv7(layer7)) layer8 = F.relu(self.conv8_up(layer7)) layer8 = torch.cat((layer2, layer8), 1) layer8 = F.relu(self.conv8_input(layer8)) layer8 = F.relu(self.conv8(layer8)) layer9 = F.relu(self.conv9_up(layer8)) layer9 = torch.cat((layer1, layer9), 1) layer9 = F.relu(self.conv9_input(layer9)) layer9 = F.relu(self.conv9(layer9)) layer9 = self.conv9_output(layer9) return layer9 def get_inputs(): return [torch.rand([4, 1, 64, 64])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn from torchvision import models 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 % 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_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 % 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_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 // 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_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 % 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_4(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) 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_5(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x0 = xindex % 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_6(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) 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_7(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_8(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 16 % 1024 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_cat_9(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 64 % 1024 x0 = xindex % 64 x2 = xindex // 65536 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 512, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 64 * x1 + 32768 * x2), tmp4, other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 1024, tl.int64) tmp9 = tl.load(in_ptr1 + (x0 + 64 * (-512 + x1) + 32768 * x2), tmp6, other=0.0) tmp10 = tl.load(in_ptr2 + (-512 + x1), tmp6, eviction_policy= 'evict_last', other=0.0) tmp11 = tmp9 + tmp10 tmp12 = tl.full([1], 0, tl.int32) tmp13 = triton_helpers.maximum(tmp12, tmp11) tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype) tmp15 = tl.where(tmp6, tmp13, tmp14) tmp16 = tl.where(tmp4, tmp5, tmp15) tl.store(out_ptr0 + x3, tmp16, None) @triton.jit def triton_poi_fused_cat_10(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 256 % 512 x0 = xindex % 256 x2 = xindex // 131072 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 256, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 256 * x1 + 65536 * x2), tmp4, other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 512, tl.int64) tmp9 = tl.load(in_ptr1 + (x0 + 256 * (-256 + x1) + 65536 * x2), tmp6, other=0.0) tmp10 = tl.load(in_ptr2 + (-256 + x1), tmp6, eviction_policy= 'evict_last', other=0.0) tmp11 = tmp9 + tmp10 tmp12 = tl.full([1], 0, tl.int32) tmp13 = triton_helpers.maximum(tmp12, tmp11) tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype) tmp15 = tl.where(tmp6, tmp13, tmp14) tmp16 = tl.where(tmp4, tmp5, tmp15) tl.store(out_ptr0 + x3, tmp16, None) @triton.jit def triton_poi_fused_cat_11(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 1024 % 256 x0 = xindex % 1024 x2 = xindex // 262144 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 128, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 1024 * x1 + 131072 * x2), tmp4, other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 256, tl.int64) tmp9 = tl.load(in_ptr1 + (x0 + 1024 * (-128 + x1) + 131072 * x2), tmp6, other=0.0) tmp10 = tl.load(in_ptr2 + (-128 + x1), tmp6, eviction_policy= 'evict_last', other=0.0) tmp11 = tmp9 + tmp10 tmp12 = tl.full([1], 0, tl.int32) tmp13 = triton_helpers.maximum(tmp12, tmp11) tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype) tmp15 = tl.where(tmp6, tmp13, tmp14) tmp16 = tl.where(tmp4, tmp5, tmp15) tl.store(out_ptr0 + x3, tmp16, None) @triton.jit def triton_poi_fused_cat_12(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) 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 = tmp0 >= tmp3 tl.full([1], 128, tl.int64) tmp9 = tl.load(in_ptr1 + (x0 + 4096 * (-64 + x1) + 262144 * x2), tmp6, other=0.0) tmp10 = tl.load(in_ptr2 + (-64 + x1), tmp6, eviction_policy= 'evict_last', other=0.0) tmp11 = tmp9 + tmp10 tmp12 = tl.full([1], 0, tl.int32) tmp13 = triton_helpers.maximum(tmp12, tmp11) tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype) tmp15 = tl.where(tmp6, tmp13, tmp14) tmp16 = tl.where(tmp4, tmp5, tmp15) tl.store(out_ptr0 + x3, tmp16, None) @triton.jit def triton_poi_fused_convolution_13(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 2 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_14(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) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_15(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 % 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_16(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 256 % 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_17(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 % 512 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, primals_46, primals_47) = args args.clear() assert_size_stride(primals_1, (64, 1, 3, 3), (9, 9, 3, 1)) assert_size_stride(primals_2, (64,), (1,)) assert_size_stride(primals_3, (4, 1, 64, 64), (4096, 4096, 64, 1)) assert_size_stride(primals_4, (64, 64, 3, 3), (576, 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, (128, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_9, (128,), (1,)) assert_size_stride(primals_10, (256, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_11, (256,), (1,)) assert_size_stride(primals_12, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_13, (256,), (1,)) assert_size_stride(primals_14, (512, 256, 3, 3), (2304, 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, (1024, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_19, (1024,), (1,)) assert_size_stride(primals_20, (1024, 1024, 3, 3), (9216, 9, 3, 1)) assert_size_stride(primals_21, (1024,), (1,)) assert_size_stride(primals_22, (1024, 512, 2, 2), (2048, 4, 2, 1)) assert_size_stride(primals_23, (512,), (1,)) assert_size_stride(primals_24, (512, 1024, 3, 3), (9216, 9, 3, 1)) assert_size_stride(primals_25, (512,), (1,)) assert_size_stride(primals_26, (512, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_27, (512,), (1,)) assert_size_stride(primals_28, (512, 256, 2, 2), (1024, 4, 2, 1)) assert_size_stride(primals_29, (256,), (1,)) assert_size_stride(primals_30, (256, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_31, (256,), (1,)) assert_size_stride(primals_32, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_33, (256,), (1,)) assert_size_stride(primals_34, (256, 128, 2, 2), (512, 4, 2, 1)) assert_size_stride(primals_35, (128,), (1,)) assert_size_stride(primals_36, (128, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_37, (128,), (1,)) assert_size_stride(primals_38, (128, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_39, (128,), (1,)) assert_size_stride(primals_40, (128, 64, 2, 2), (256, 4, 2, 1)) assert_size_stride(primals_41, (64,), (1,)) assert_size_stride(primals_42, (64, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_43, (64,), (1,)) assert_size_stride(primals_44, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_45, (64,), (1,)) assert_size_stride(primals_46, (2, 64, 1, 1), (64, 1, 1, 1)) assert_size_stride(primals_47, (2,), (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 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(1048576)](buf1, primals_2, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del primals_2 buf2 = extern_kernels.convolution(buf1, primals_4, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_relu_0[grid(1048576)](buf3, primals_5, 1048576, XBLOCK=512, num_warps=8, 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_1[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_2[grid(524288)](buf7, primals_7, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_7 buf8 = extern_kernels.convolution(buf7, primals_8, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 128, 32, 32), (131072, 1024, 32, 1)) buf9 = buf8 del buf8 triton_poi_fused_convolution_relu_2[grid(524288)](buf9, primals_9, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_9 buf10 = empty_strided_cuda((4, 128, 16, 16), (32768, 256, 16, 1), torch.float32) buf11 = empty_strided_cuda((4, 128, 16, 16), (32768, 256, 16, 1), torch.int8) triton_poi_fused_max_pool2d_with_indices_3[grid(131072)](buf9, buf10, buf11, 131072, XBLOCK=512, num_warps=8, num_stages=1) buf12 = extern_kernels.convolution(buf10, 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_4[grid(262144)](buf13, primals_11, 262144, XBLOCK=512, num_warps=8, num_stages=1) del primals_11 buf14 = extern_kernels.convolution(buf13, primals_12, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf14, (4, 256, 16, 16), (65536, 256, 16, 1)) buf15 = buf14 del buf14 triton_poi_fused_convolution_relu_4[grid(262144)](buf15, primals_13, 262144, XBLOCK=512, num_warps=8, num_stages=1) del primals_13 buf16 = empty_strided_cuda((4, 256, 8, 8), (16384, 64, 8, 1), torch .float32) buf17 = empty_strided_cuda((4, 256, 8, 8), (16384, 64, 8, 1), torch .int8) triton_poi_fused_max_pool2d_with_indices_5[grid(65536)](buf15, buf16, buf17, 65536, XBLOCK=256, num_warps=4, num_stages=1) buf18 = extern_kernels.convolution(buf16, 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_6[grid(131072)](buf19, primals_15, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del primals_15 buf20 = extern_kernels.convolution(buf19, primals_16, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf20, (4, 512, 8, 8), (32768, 64, 8, 1)) buf21 = buf20 del buf20 triton_poi_fused_convolution_relu_6[grid(131072)](buf21, primals_17, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del primals_17 buf22 = empty_strided_cuda((4, 512, 4, 4), (8192, 16, 4, 1), torch. float32) buf23 = empty_strided_cuda((4, 512, 4, 4), (8192, 16, 4, 1), torch.int8 ) triton_poi_fused_max_pool2d_with_indices_7[grid(32768)](buf21, buf22, buf23, 32768, XBLOCK=256, num_warps=4, num_stages=1) buf24 = extern_kernels.convolution(buf22, 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, 1024, 4, 4), (16384, 16, 4, 1)) buf25 = buf24 del buf24 triton_poi_fused_convolution_relu_8[grid(65536)](buf25, primals_19, 65536, XBLOCK=512, num_warps=4, num_stages=1) del primals_19 buf26 = extern_kernels.convolution(buf25, primals_20, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf26, (4, 1024, 4, 4), (16384, 16, 4, 1)) buf27 = buf26 del buf26 triton_poi_fused_convolution_relu_8[grid(65536)](buf27, primals_21, 65536, XBLOCK=512, num_warps=4, num_stages=1) del primals_21 buf28 = extern_kernels.convolution(buf27, primals_22, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf28, (4, 512, 8, 8), (32768, 64, 8, 1)) buf29 = empty_strided_cuda((4, 1024, 8, 8), (65536, 64, 8, 1), torch.float32) triton_poi_fused_cat_9[grid(262144)](buf21, buf28, primals_23, buf29, 262144, XBLOCK=512, num_warps=8, num_stages=1) buf30 = extern_kernels.convolution(buf29, primals_24, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf30, (4, 512, 8, 8), (32768, 64, 8, 1)) buf31 = buf30 del buf30 triton_poi_fused_convolution_relu_6[grid(131072)](buf31, primals_25, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del primals_25 buf32 = extern_kernels.convolution(buf31, primals_26, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf32, (4, 512, 8, 8), (32768, 64, 8, 1)) buf33 = buf32 del buf32 triton_poi_fused_convolution_relu_6[grid(131072)](buf33, primals_27, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del primals_27 buf34 = extern_kernels.convolution(buf33, primals_28, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf34, (4, 256, 16, 16), (65536, 256, 16, 1)) buf35 = empty_strided_cuda((4, 512, 16, 16), (131072, 256, 16, 1), torch.float32) triton_poi_fused_cat_10[grid(524288)](buf15, buf34, primals_29, buf35, 524288, XBLOCK=1024, num_warps=4, num_stages=1) buf36 = extern_kernels.convolution(buf35, primals_30, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf36, (4, 256, 16, 16), (65536, 256, 16, 1)) buf37 = buf36 del buf36 triton_poi_fused_convolution_relu_4[grid(262144)](buf37, primals_31, 262144, XBLOCK=512, num_warps=8, num_stages=1) del primals_31 buf38 = extern_kernels.convolution(buf37, primals_32, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf38, (4, 256, 16, 16), (65536, 256, 16, 1)) buf39 = buf38 del buf38 triton_poi_fused_convolution_relu_4[grid(262144)](buf39, primals_33, 262144, XBLOCK=512, num_warps=8, num_stages=1) del primals_33 buf40 = extern_kernels.convolution(buf39, primals_34, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf40, (4, 128, 32, 32), (131072, 1024, 32, 1)) buf41 = empty_strided_cuda((4, 256, 32, 32), (262144, 1024, 32, 1), torch.float32) triton_poi_fused_cat_11[grid(1048576)](buf9, buf40, primals_35, buf41, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) buf42 = extern_kernels.convolution(buf41, primals_36, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf42, (4, 128, 32, 32), (131072, 1024, 32, 1)) buf43 = buf42 del buf42 triton_poi_fused_convolution_relu_2[grid(524288)](buf43, primals_37, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_37 buf44 = extern_kernels.convolution(buf43, primals_38, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf44, (4, 128, 32, 32), (131072, 1024, 32, 1)) buf45 = buf44 del buf44 triton_poi_fused_convolution_relu_2[grid(524288)](buf45, primals_39, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_39 buf46 = extern_kernels.convolution(buf45, primals_40, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf46, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf47 = empty_strided_cuda((4, 128, 64, 64), (524288, 4096, 64, 1), torch.float32) triton_poi_fused_cat_12[grid(2097152)](buf3, buf46, primals_41, buf47, 2097152, XBLOCK=1024, num_warps=4, num_stages=1) buf48 = extern_kernels.convolution(buf47, primals_42, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf48, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf49 = buf48 del buf48 triton_poi_fused_convolution_relu_0[grid(1048576)](buf49, primals_43, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del primals_43 buf50 = extern_kernels.convolution(buf49, primals_44, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf50, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf51 = buf50 del buf50 triton_poi_fused_convolution_relu_0[grid(1048576)](buf51, primals_45, 1048576, XBLOCK=512, num_warps=8, num_stages=1) del primals_45 buf52 = extern_kernels.convolution(buf51, primals_46, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf52, (4, 2, 64, 64), (8192, 4096, 64, 1)) buf53 = buf52 del buf52 triton_poi_fused_convolution_13[grid(32768)](buf53, primals_47, 32768, XBLOCK=256, num_warps=4, num_stages=1) del primals_47 buf54 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_14[grid(1048576)]( buf46, primals_41, buf54, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del buf46 del primals_41 buf55 = empty_strided_cuda((4, 128, 32, 32), (131072, 1024, 32, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_15[grid(524288)]( buf40, primals_35, buf55, 524288, XBLOCK=512, num_warps=8, num_stages=1) del buf40 del primals_35 buf56 = empty_strided_cuda((4, 256, 16, 16), (65536, 256, 16, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_16[grid(262144)]( buf34, primals_29, buf56, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del buf34 del primals_29 buf57 = empty_strided_cuda((4, 512, 8, 8), (32768, 64, 8, 1), torch .bool) triton_poi_fused_convolution_relu_threshold_backward_17[grid(131072)]( buf28, primals_23, buf57, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del buf28 del primals_23 return (buf53, primals_1, primals_3, primals_4, primals_6, primals_8, primals_10, primals_12, primals_14, primals_16, primals_18, primals_20, primals_22, primals_24, primals_26, primals_28, primals_30, primals_32, primals_34, primals_36, primals_38, primals_40, primals_42, primals_44, primals_46, buf1, buf3, buf4, buf5, buf7, buf9, buf10, buf11, buf13, buf15, buf16, buf17, buf19, buf21, buf22, buf23, buf25, buf27, buf29, buf31, buf33, buf35, buf37, buf39, buf41, buf43, buf45, buf47, buf49, buf51, buf54, buf55, buf56, buf57) class UNetNew(nn.Module): """ The U-Net Convolutional Neural Network for semantic segmentation Source material for the algorithm: https://link.springer.com/chapter/10.1007%2F978-3-319-24574-4_28 """ def __init__(self, in_channels=1, num_classes=2, pretrained=False, padding=1): """ Arguments: in_channels (int): the number of channels in the input image num_classes (int, optional): the number of classes to be predicted. Note: The background is always considered as 1 class. (By default predicts the background and foreground: 2 classes). pretrained (bool, optional): whether to use VGG13 encoders pretrained on ImageNet for the first 8 convolutional layers in the encoder section of the U-Net model padding (int, optional): the padding applied on images at convolutional layers. A padding of 1 helps keep the original input image's height and witdh. Note: pretrained VGG13 encoders use a padding of 1 """ super().__init__() if pretrained: encoder = models.vgg13(pretrained=True).features self.conv1_input = encoder[0] self.conv1 = encoder[2] self.conv2_input = encoder[5] self.conv2 = encoder[7] self.conv3_input = encoder[10] self.conv3 = encoder[12] self.conv4_input = encoder[15] self.conv4 = encoder[17] else: self.conv1_input = nn.Conv2d(in_channels, 64, 3, padding=padding) self.conv1 = nn.Conv2d(64, 64, 3, padding=padding) self.conv2_input = nn.Conv2d(64, 128, 3, padding=padding) self.conv2 = nn.Conv2d(128, 128, 3, padding=padding) self.conv3_input = nn.Conv2d(128, 256, 3, padding=padding) self.conv3 = nn.Conv2d(256, 256, 3, padding=padding) self.conv4_input = nn.Conv2d(256, 512, 3, padding=padding) self.conv4 = nn.Conv2d(512, 512, 3, padding=padding) self.conv5_input = nn.Conv2d(512, 1024, 3, padding=padding) self.conv5 = nn.Conv2d(1024, 1024, 3, padding=padding) self.conv6_up = nn.ConvTranspose2d(1024, 512, 2, 2) self.conv6_input = nn.Conv2d(1024, 512, 3, padding=padding) self.conv6 = nn.Conv2d(512, 512, 3, padding=padding) self.conv7_up = nn.ConvTranspose2d(512, 256, 2, 2) self.conv7_input = nn.Conv2d(512, 256, 3, padding=padding) self.conv7 = nn.Conv2d(256, 256, 3, padding=padding) self.conv8_up = nn.ConvTranspose2d(256, 128, 2, 2) self.conv8_input = nn.Conv2d(256, 128, 3, padding=padding) self.conv8 = nn.Conv2d(128, 128, 3, padding=padding) self.conv9_up = nn.ConvTranspose2d(128, 64, 2, 2) self.conv9_input = nn.Conv2d(128, 64, 3, padding=padding) self.conv9 = nn.Conv2d(64, 64, 3, padding=padding) self.conv9_output = nn.Conv2d(64, num_classes, 1) def forward(self, input_0): primals_1 = self.conv1_input.weight primals_2 = self.conv1_input.bias primals_4 = self.conv1.weight primals_5 = self.conv1.bias primals_6 = self.conv2_input.weight primals_7 = self.conv2_input.bias primals_8 = self.conv2.weight primals_9 = self.conv2.bias primals_10 = self.conv3_input.weight primals_11 = self.conv3_input.bias primals_12 = self.conv3.weight primals_13 = self.conv3.bias primals_14 = self.conv4_input.weight primals_15 = self.conv4_input.bias primals_16 = self.conv4.weight primals_17 = self.conv4.bias primals_18 = self.conv5_input.weight primals_19 = self.conv5_input.bias primals_20 = self.conv5.weight primals_21 = self.conv5.bias primals_22 = self.conv6_up.weight primals_23 = self.conv6_up.bias primals_24 = self.conv6_input.weight primals_25 = self.conv6_input.bias primals_26 = self.conv6.weight primals_27 = self.conv6.bias primals_28 = self.conv7_up.weight primals_29 = self.conv7_up.bias primals_30 = self.conv7_input.weight primals_31 = self.conv7_input.bias primals_32 = self.conv7.weight primals_33 = self.conv7.bias primals_34 = self.conv8_up.weight primals_35 = self.conv8_up.bias primals_36 = self.conv8_input.weight primals_37 = self.conv8_input.bias primals_38 = self.conv8.weight primals_39 = self.conv8.bias primals_40 = self.conv9_up.weight primals_41 = self.conv9_up.bias primals_42 = self.conv9_input.weight primals_43 = self.conv9_input.bias primals_44 = self.conv9.weight primals_45 = self.conv9.bias primals_46 = self.conv9_output.weight primals_47 = self.conv9_output.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16, primals_17, primals_18, primals_19, primals_20, primals_21, primals_22, primals_23, primals_24, primals_25, primals_26, primals_27, primals_28, primals_29, primals_30, primals_31, primals_32, primals_33, primals_34, primals_35, primals_36, primals_37, primals_38, primals_39, primals_40, primals_41, primals_42, primals_43, primals_44, primals_45, primals_46, primals_47]) return output[0]
mattesko/torch-toolkit
UNet
false
10,647
[ "MIT" ]
0
1b4526640232843bdd4022c86cf1856e2e3248b0
https://github.com/mattesko/torch-toolkit/tree/1b4526640232843bdd4022c86cf1856e2e3248b0
minibatch_std_concat_layer
import copy import torch import torch.nn as nn class minibatch_std_concat_layer(nn.Module): def __init__(self, averaging='all'): super(minibatch_std_concat_layer, self).__init__() self.averaging = averaging.lower() if 'group' in self.averaging: self.n = int(self.averaging[5:]) else: assert self.averaging in ['all', 'flat', 'spatial', 'none', 'gpool' ], 'Invalid averaging mode' % self.averaging self.adjusted_std = lambda x, **kwargs: torch.sqrt(torch.mean((x - torch.mean(x, **kwargs)) ** 2, **kwargs) + 1e-08) def forward(self, x): shape = list(x.size()) target_shape = copy.deepcopy(shape) vals = self.adjusted_std(x, dim=0, keepdim=True) if self.averaging == 'all': target_shape[1] = 1 vals = torch.mean(vals, dim=1, keepdim=True) elif self.averaging == 'spatial': if len(shape) == 4: vals = mean(vals, axis=[2, 3], keepdim=True) elif self.averaging == 'none': target_shape = [target_shape[0]] + [s for s in target_shape[1:]] elif self.averaging == 'gpool': if len(shape) == 4: vals = mean(x, [0, 2, 3], keepdim=True) elif self.averaging == 'flat': target_shape[1] = 1 vals = torch.FloatTensor([self.adjusted_std(x)]) else: target_shape[1] = self.n vals = vals.view(self.n, self.shape[1] / self.n, self.shape[2], self.shape[3]) vals = mean(vals, axis=0, keepdim=True).view(1, self.n, 1, 1) vals = vals.expand(*target_shape) return torch.cat([x, vals], 1) def __repr__(self): return self.__class__.__name__ + '(averaging = %s)' % self.averaging def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_mean_pow_sqrt_sub_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + (64 + x0), xmask) tmp3 = tl.load(in_ptr0 + (128 + x0), xmask) tmp5 = tl.load(in_ptr0 + (192 + x0), xmask) tmp24 = tl.load(in_ptr0 + (16 + x0), xmask) tmp25 = tl.load(in_ptr0 + (80 + x0), xmask) tmp27 = tl.load(in_ptr0 + (144 + x0), xmask) tmp29 = tl.load(in_ptr0 + (208 + x0), xmask) tmp47 = tl.load(in_ptr0 + (32 + x0), xmask) tmp48 = tl.load(in_ptr0 + (96 + x0), xmask) tmp50 = tl.load(in_ptr0 + (160 + x0), xmask) tmp52 = tl.load(in_ptr0 + (224 + x0), xmask) tmp70 = tl.load(in_ptr0 + (48 + x0), xmask) tmp71 = tl.load(in_ptr0 + (112 + x0), xmask) tmp73 = tl.load(in_ptr0 + (176 + x0), xmask) tmp75 = tl.load(in_ptr0 + (240 + x0), 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-08 tmp22 = tmp20 + tmp21 tmp23 = libdevice.sqrt(tmp22) tmp26 = tmp24 + tmp25 tmp28 = tmp26 + tmp27 tmp30 = tmp28 + tmp29 tmp31 = tmp30 / tmp7 tmp32 = tmp24 - tmp31 tmp33 = tmp32 * tmp32 tmp34 = tmp25 - tmp31 tmp35 = tmp34 * tmp34 tmp36 = tmp33 + tmp35 tmp37 = tmp27 - tmp31 tmp38 = tmp37 * tmp37 tmp39 = tmp36 + tmp38 tmp40 = tmp29 - tmp31 tmp41 = tmp40 * tmp40 tmp42 = tmp39 + tmp41 tmp43 = tmp42 / tmp7 tmp44 = tmp43 + tmp21 tmp45 = libdevice.sqrt(tmp44) tmp46 = tmp23 + tmp45 tmp49 = tmp47 + tmp48 tmp51 = tmp49 + tmp50 tmp53 = tmp51 + tmp52 tmp54 = tmp53 / tmp7 tmp55 = tmp47 - tmp54 tmp56 = tmp55 * tmp55 tmp57 = tmp48 - tmp54 tmp58 = tmp57 * tmp57 tmp59 = tmp56 + tmp58 tmp60 = tmp50 - tmp54 tmp61 = tmp60 * tmp60 tmp62 = tmp59 + tmp61 tmp63 = tmp52 - tmp54 tmp64 = tmp63 * tmp63 tmp65 = tmp62 + tmp64 tmp66 = tmp65 / tmp7 tmp67 = tmp66 + tmp21 tmp68 = libdevice.sqrt(tmp67) tmp69 = tmp46 + tmp68 tmp72 = tmp70 + tmp71 tmp74 = tmp72 + tmp73 tmp76 = tmp74 + tmp75 tmp77 = tmp76 / tmp7 tmp78 = tmp70 - tmp77 tmp79 = tmp78 * tmp78 tmp80 = tmp71 - tmp77 tmp81 = tmp80 * tmp80 tmp82 = tmp79 + tmp81 tmp83 = tmp73 - tmp77 tmp84 = tmp83 * tmp83 tmp85 = tmp82 + tmp84 tmp86 = tmp75 - tmp77 tmp87 = tmp86 * tmp86 tmp88 = tmp85 + tmp87 tmp89 = tmp88 / tmp7 tmp90 = tmp89 + tmp21 tmp91 = libdevice.sqrt(tmp90) tmp92 = tmp69 + tmp91 tmp93 = tmp92 / tmp7 tl.store(out_ptr0 + x0, tmp93, xmask) @triton.jit def triton_poi_fused_cat_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 320 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 16 % 5 x0 = xindex % 16 x2 = xindex // 80 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 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], 5, tl.int64) tmp9 = tl.load(in_ptr1 + x0, tmp6 & xmask, eviction_policy='evict_last', other=0.0) tmp10 = tl.where(tmp4, tmp5, tmp9) tl.store(out_ptr0 + x3, tmp10, 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((1, 1, 4, 4), (16, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mean_pow_sqrt_sub_0[grid(16)](arg0_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) buf1 = empty_strided_cuda((4, 5, 4, 4), (80, 16, 4, 1), torch.float32) triton_poi_fused_cat_1[grid(320)](arg0_1, buf0, buf1, 320, XBLOCK= 128, num_warps=4, num_stages=1) del arg0_1 del buf0 return buf1, class minibatch_std_concat_layerNew(nn.Module): def __init__(self, averaging='all'): super(minibatch_std_concat_layerNew, self).__init__() self.averaging = averaging.lower() if 'group' in self.averaging: self.n = int(self.averaging[5:]) else: assert self.averaging in ['all', 'flat', 'spatial', 'none', 'gpool' ], 'Invalid averaging mode' % self.averaging self.adjusted_std = lambda x, **kwargs: torch.sqrt(torch.mean((x - torch.mean(x, **kwargs)) ** 2, **kwargs) + 1e-08) def __repr__(self): return self.__class__.__name__ + '(averaging = %s)' % self.averaging def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
mikanCan/PG-GAN
minibatch_std_concat_layer
false
10,648
[ "MIT" ]
0
bc4a1bd2101f836c22a164174381f80b3f5c73c1
https://github.com/mikanCan/PG-GAN/tree/bc4a1bd2101f836c22a164174381f80b3f5c73c1
Actor
import torch import torch.nn.functional as F import torch.nn as nn class Actor(nn.Module): """Actor (Policy) Model.""" def __init__(self, state_size, action_size, seed, fc1_units=200, fc2_units=150): """Initialize parameters and build model. Params ====== state_size (int): Dimension of each state action_size (int): Dimension of each action seed (int): Random seed fc1_units (int): Number of nodes in first hidden layer fc2_units (int): Number of nodes in second hidden layer """ super(Actor, self).__init__() self.seed = seed self.fc1 = nn.Linear(state_size, fc1_units) self.fc2 = nn.Linear(fc1_units, fc2_units) self.fc3 = nn.Linear(fc2_units, action_size) self.fc3.weight.data.uniform_(-0.003, 0.003) self.fc3.bias.data.uniform_(-0.003, 0.003) def forward(self, state): """Build an actor (policy) network that maps states -> actions.""" x = F.relu(self.fc1(state)) x = F.relu(self.fc2(x)) return torch.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 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 = 12800 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 200 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) @triton.jit def triton_poi_fused_relu_threshold_backward_1(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 9600 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex x0 = xindex % 150 x2 = xindex % 2400 x3 = xindex // 2400 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 + 2432 * x3), tmp6, xmask) @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, (200, 4), (4, 1)) assert_size_stride(primals_2, (200,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (150, 200), (200, 1)) assert_size_stride(primals_5, (150,), (1,)) assert_size_stride(primals_6, (4, 150), (150, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 200), (200, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 200), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 200), (3200, 800, 200, 1), 0) del buf0 buf7 = empty_strided_cuda((4, 4, 4, 200), (3200, 800, 200, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(12800)](buf1, primals_2, buf7, 12800, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 150), (150, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 200), (200, 1), 0), reinterpret_tensor(primals_4, (200, 150), (1, 200), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 150), (2400, 600, 150, 1), 0) del buf2 buf6 = empty_strided_cuda((4, 4, 4, 150), (2432, 600, 150, 1), torch.bool) triton_poi_fused_relu_threshold_backward_1[grid(9600)](buf3, primals_5, buf6, 9600, 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, 150), (150, 1), 0), reinterpret_tensor(primals_6, (150, 4), (1, 150), 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, 200), (200, 1), 0 ), reinterpret_tensor(buf3, (64, 150), (150, 1), 0 ), buf5, primals_6, buf6, primals_4, buf7 class ActorNew(nn.Module): """Actor (Policy) Model.""" def __init__(self, state_size, action_size, seed, fc1_units=200, fc2_units=150): """Initialize parameters and build model. Params ====== state_size (int): Dimension of each state action_size (int): Dimension of each action seed (int): Random seed fc1_units (int): Number of nodes in first hidden layer fc2_units (int): Number of nodes in second hidden layer """ super(ActorNew, self).__init__() self.seed = seed self.fc1 = nn.Linear(state_size, fc1_units) self.fc2 = nn.Linear(fc1_units, fc2_units) self.fc3 = nn.Linear(fc2_units, action_size) self.fc3.weight.data.uniform_(-0.003, 0.003) self.fc3.bias.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]
rafapi/PMTG
Actor
false
10,649
[ "Apache-2.0" ]
0
8a89a3dd9620e2fdf747d20781b46daebd41569c
https://github.com/rafapi/PMTG/tree/8a89a3dd9620e2fdf747d20781b46daebd41569c
Entmax15
from torch.autograd import Function import torch import torch.nn as nn def _make_ix_like(X, dim): d = X.size(dim) rho = torch.arange(1, d + 1, device=X.device, dtype=X.dtype) view = [1] * X.dim() view[0] = -1 return rho.view(view).transpose(0, dim) def _roll_last(X, dim): if dim == -1: return X elif dim < 0: dim = X.dim() - dim perm = [i for i in range(X.dim()) if i != dim] + [dim] return X.permute(perm) def _entmax_threshold_and_support(X, dim=-1, k=None): """Core computation for 1.5-entmax: optimal threshold and support size. Parameters ---------- X : torch.Tensor The input tensor to compute thresholds over. dim : int The dimension along which to apply 1.5-entmax. k : int or None number of largest elements to partial-sort over. For optimal performance, should be slightly bigger than the expected number of nonzeros in the solution. If the solution is more than k-sparse, this function is recursively called with a 2*k schedule. If `None`, full sorting is performed from the beginning. Returns ------- tau : torch.Tensor like `X`, with all but the `dim` dimension intact the threshold value for each vector support_size : torch LongTensor, shape like `tau` the number of nonzeros in each vector. """ if k is None or k >= X.shape[dim]: Xsrt, _ = torch.sort(X, dim=dim, descending=True) else: Xsrt, _ = torch.topk(X, k=k, dim=dim) rho = _make_ix_like(Xsrt, dim) mean = Xsrt.cumsum(dim) / rho mean_sq = (Xsrt ** 2).cumsum(dim) / rho ss = rho * (mean_sq - mean ** 2) delta = (1 - ss) / rho delta_nz = torch.clamp(delta, 0) tau = mean - torch.sqrt(delta_nz) support_size = (tau <= Xsrt).sum(dim).unsqueeze(dim) tau_star = tau.gather(dim, support_size - 1) if k is not None and k < X.shape[dim]: unsolved = (support_size == k).squeeze(dim) if torch.any(unsolved): X_ = _roll_last(X, dim)[unsolved] tau_, ss_ = _entmax_threshold_and_support(X_, dim=-1, k=2 * k) _roll_last(tau_star, dim)[unsolved] = tau_ _roll_last(support_size, dim)[unsolved] = ss_ return tau_star, support_size def entmax15(X, dim=-1, k=None): """1.5-entmax: normalizing sparse transform (a la softmax). Solves the optimization problem: max_p <x, p> - H_1.5(p) s.t. p >= 0, sum(p) == 1. where H_1.5(p) is the Tsallis alpha-entropy with alpha=1.5. Parameters ---------- X : torch.Tensor The input tensor. dim : int The dimension along which to apply 1.5-entmax. k : int or None number of largest elements to partial-sort over. For optimal performance, should be slightly bigger than the expected number of nonzeros in the solution. If the solution is more than k-sparse, this function is recursively called with a 2*k schedule. If `None`, full sorting is performed from the beginning. Returns ------- P : torch tensor, same shape as X The projection result, such that P.sum(dim=dim) == 1 elementwise. """ return Entmax15Function.apply(X, dim, k) class Entmax15Function(Function): @classmethod def forward(cls, ctx, X, dim=0, k=None): ctx.dim = dim max_val, _ = X.max(dim=dim, keepdim=True) X = X - max_val X = X / 2 tau_star, _ = _entmax_threshold_and_support(X, dim=dim, k=k) Y = torch.clamp(X - tau_star, min=0) ** 2 ctx.save_for_backward(Y) return Y @classmethod def backward(cls, ctx, dY): Y, = ctx.saved_tensors gppr = Y.sqrt() dX = dY * gppr q = dX.sum(ctx.dim) / gppr.sum(ctx.dim) q = q.unsqueeze(ctx.dim) dX -= q * gppr return dX, None, None class Entmax15(nn.Module): def __init__(self, dim=-1, k=None): """1.5-entmax: normalizing sparse transform (a la softmax). Solves the optimization problem: max_p <x, p> - H_1.5(p) s.t. p >= 0, sum(p) == 1. where H_1.5(p) is the Tsallis alpha-entropy with alpha=1.5. Parameters ---------- dim : int The dimension along which to apply 1.5-entmax. k : int or None number of largest elements to partial-sort over. For optimal performance, should be slightly bigger than the expected number of nonzeros in the solution. If the solution is more than k-sparse, this function is recursively called with a 2*k schedule. If `None`, full sorting is performed from the beginning. """ self.dim = dim self.k = k super(Entmax15, self).__init__() def forward(self, X): return entmax15(X, dim=self.dim, k=self.k) 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 from torch.autograd import Function 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_helper_fn_add0(arg0_0, arg1_0): tmp0 = arg0_0 + arg1_0 return tmp0 @triton.jit def triton_per_fused_cumsum_div_max_pow_sort_sub_0(in_ptr0, out_ptr0, out_ptr1, out_ptr2, out_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 64 RBLOCK: tl.constexpr = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 4 * x0), xmask, other=0.0) tmp1 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = 0.5 tmp10 = tmp8 * tmp9 tmp11 = r1 tmp12 = tmp11.to(tl.int16) tmp13 = tl.broadcast_to(tmp10, [XBLOCK, RBLOCK]) tmp14 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK]) tmp15, _tmp16 = triton_helpers.sort_with_index(tmp13, tmp14, None, 1, stable=False, descending=True) tmp17 = tmp15 * tmp15 tmp18 = tmp17.to(tl.float32) tmp19 = tl.broadcast_to(tmp18, [XBLOCK, RBLOCK]) tmp20, = tl.associative_scan((tmp19,), 1, _triton_helper_fn_add0) tmp21 = tmp15.to(tl.float32) tmp22 = tl.broadcast_to(tmp21, [XBLOCK, RBLOCK]) tmp23, = tl.associative_scan((tmp22,), 1, _triton_helper_fn_add0) tl.store(out_ptr0 + (r1 + 4 * x0), tmp10, xmask) tl.store(out_ptr1 + (r1 + 4 * x0), tmp15, xmask) tl.store(out_ptr2 + (r1 + 4 * x0), tmp20, xmask) tl.store(out_ptr3 + (r1 + 4 * x0), tmp23, xmask) @triton.jit def triton_poi_fused_clamp_div_le_mul_pow_rsub_sqrt_sub_sum_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp14 = tl.load(in_ptr2 + 4 * x0, xmask, eviction_policy='evict_last') tmp17 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp20 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp30 = tl.load(in_ptr2 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp34 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp37 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp47 = tl.load(in_ptr2 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp51 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp54 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp64 = tl.load(in_ptr2 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp1 = 1.0 tmp2 = tmp0 / tmp1 tmp4 = tmp3 / tmp1 tmp5 = tmp2 * tmp2 tmp6 = tmp4 - tmp5 tmp7 = tmp1 * tmp6 tmp8 = tmp1 - tmp7 tmp9 = tmp8 / tmp1 tmp10 = 0.0 tmp11 = triton_helpers.maximum(tmp9, tmp10) tmp12 = libdevice.sqrt(tmp11) tmp13 = tmp2 - tmp12 tmp15 = tmp13 <= tmp14 tmp16 = tmp15.to(tl.int64) tmp18 = 2.0 tmp19 = tmp17 / tmp18 tmp21 = tmp20 / tmp18 tmp22 = tmp19 * tmp19 tmp23 = tmp21 - tmp22 tmp24 = tmp18 * tmp23 tmp25 = tmp1 - tmp24 tmp26 = tmp25 / tmp18 tmp27 = triton_helpers.maximum(tmp26, tmp10) tmp28 = libdevice.sqrt(tmp27) tmp29 = tmp19 - tmp28 tmp31 = tmp29 <= tmp30 tmp32 = tmp31.to(tl.int64) tmp33 = tmp16 + tmp32 tmp35 = 3.0 tmp36 = tmp34 / tmp35 tmp38 = tmp37 / tmp35 tmp39 = tmp36 * tmp36 tmp40 = tmp38 - tmp39 tmp41 = tmp35 * tmp40 tmp42 = tmp1 - tmp41 tmp43 = tmp42 / tmp35 tmp44 = triton_helpers.maximum(tmp43, tmp10) tmp45 = libdevice.sqrt(tmp44) tmp46 = tmp36 - tmp45 tmp48 = tmp46 <= tmp47 tmp49 = tmp48.to(tl.int64) tmp50 = tmp33 + tmp49 tmp52 = 4.0 tmp53 = tmp51 / tmp52 tmp55 = tmp54 / tmp52 tmp56 = tmp53 * tmp53 tmp57 = tmp55 - tmp56 tmp58 = tmp52 * tmp57 tmp59 = tmp1 - tmp58 tmp60 = tmp59 / tmp52 tmp61 = triton_helpers.maximum(tmp60, tmp10) tmp62 = libdevice.sqrt(tmp61) tmp63 = tmp53 - tmp62 tmp65 = tmp63 <= tmp64 tmp66 = tmp65.to(tl.int64) tmp67 = tmp50 + tmp66 tl.store(out_ptr0 + x0, tmp67, xmask) @triton.jit def triton_poi_fused_clamp_div_gather_mul_pow_rsub_sqrt_sub_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tl.full([1], 1, tl.int64) tmp3 = tmp1 - tmp2 tmp4 = tl.full([XBLOCK], 4, tl.int32) tmp5 = tmp3 + tmp4 tmp6 = tmp3 < 0 tmp7 = tl.where(tmp6, tmp5, tmp3) tl.device_assert((0 <= tmp7) & (tmp7 < 4) | ~xmask, 'index out of bounds: 0 <= tmp7 < 4') tmp9 = tl.load(in_ptr2 + (tmp7 + 4 * x1), xmask, eviction_policy= 'evict_last') tmp10 = 1 + tmp7 tmp11 = tmp10.to(tl.float32) tmp12 = tmp9 / tmp11 tmp13 = tl.load(in_ptr3 + (tmp7 + 4 * x1), xmask, eviction_policy= 'evict_last') tmp14 = tmp13 / tmp11 tmp15 = tmp12 * tmp12 tmp16 = tmp14 - tmp15 tmp17 = tmp11 * tmp16 tmp18 = 1.0 tmp19 = tmp18 - tmp17 tmp20 = tmp19 / tmp11 tmp21 = 0.0 tmp22 = triton_helpers.maximum(tmp20, tmp21) tmp23 = libdevice.sqrt(tmp22) tmp24 = tmp12 - tmp23 tmp25 = tmp0 - tmp24 tmp26 = triton_helpers.maximum(tmp25, tmp21) tmp27 = tmp26 * tmp26 tl.store(out_ptr0 + x2, tmp27, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_per_fused_cumsum_div_max_pow_sort_sub_0[grid(64)](arg0_1, buf0, buf1, buf3, buf4, 64, 4, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.int64) triton_poi_fused_clamp_div_le_mul_pow_rsub_sqrt_sub_sum_1[grid(64)]( buf4, buf3, buf1, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf6 = buf1 del buf1 triton_poi_fused_clamp_div_gather_mul_pow_rsub_sqrt_sub_2[grid(256)]( buf0, buf5, buf4, buf3, buf6, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf0 del buf3 del buf4 del buf5 return buf6, def _make_ix_like(X, dim): d = X.size(dim) rho = torch.arange(1, d + 1, device=X.device, dtype=X.dtype) view = [1] * X.dim() view[0] = -1 return rho.view(view).transpose(0, dim) def _roll_last(X, dim): if dim == -1: return X elif dim < 0: dim = X.dim() - dim perm = [i for i in range(X.dim()) if i != dim] + [dim] return X.permute(perm) def _entmax_threshold_and_support(X, dim=-1, k=None): """Core computation for 1.5-entmax: optimal threshold and support size. Parameters ---------- X : torch.Tensor The input tensor to compute thresholds over. dim : int The dimension along which to apply 1.5-entmax. k : int or None number of largest elements to partial-sort over. For optimal performance, should be slightly bigger than the expected number of nonzeros in the solution. If the solution is more than k-sparse, this function is recursively called with a 2*k schedule. If `None`, full sorting is performed from the beginning. Returns ------- tau : torch.Tensor like `X`, with all but the `dim` dimension intact the threshold value for each vector support_size : torch LongTensor, shape like `tau` the number of nonzeros in each vector. """ if k is None or k >= X.shape[dim]: Xsrt, _ = torch.sort(X, dim=dim, descending=True) else: Xsrt, _ = torch.topk(X, k=k, dim=dim) rho = _make_ix_like(Xsrt, dim) mean = Xsrt.cumsum(dim) / rho mean_sq = (Xsrt ** 2).cumsum(dim) / rho ss = rho * (mean_sq - mean ** 2) delta = (1 - ss) / rho delta_nz = torch.clamp(delta, 0) tau = mean - torch.sqrt(delta_nz) support_size = (tau <= Xsrt).sum(dim).unsqueeze(dim) tau_star = tau.gather(dim, support_size - 1) if k is not None and k < X.shape[dim]: unsolved = (support_size == k).squeeze(dim) if torch.any(unsolved): X_ = _roll_last(X, dim)[unsolved] tau_, ss_ = _entmax_threshold_and_support(X_, dim=-1, k=2 * k) _roll_last(tau_star, dim)[unsolved] = tau_ _roll_last(support_size, dim)[unsolved] = ss_ return tau_star, support_size def entmax15(X, dim=-1, k=None): """1.5-entmax: normalizing sparse transform (a la softmax). Solves the optimization problem: max_p <x, p> - H_1.5(p) s.t. p >= 0, sum(p) == 1. where H_1.5(p) is the Tsallis alpha-entropy with alpha=1.5. Parameters ---------- X : torch.Tensor The input tensor. dim : int The dimension along which to apply 1.5-entmax. k : int or None number of largest elements to partial-sort over. For optimal performance, should be slightly bigger than the expected number of nonzeros in the solution. If the solution is more than k-sparse, this function is recursively called with a 2*k schedule. If `None`, full sorting is performed from the beginning. Returns ------- P : torch tensor, same shape as X The projection result, such that P.sum(dim=dim) == 1 elementwise. """ return Entmax15Function.apply(X, dim, k) class Entmax15Function(Function): @classmethod def forward(cls, ctx, X, dim=0, k=None): ctx.dim = dim max_val, _ = X.max(dim=dim, keepdim=True) X = X - max_val X = X / 2 tau_star, _ = _entmax_threshold_and_support(X, dim=dim, k=k) Y = torch.clamp(X - tau_star, min=0) ** 2 ctx.save_for_backward(Y) return Y @classmethod def backward(cls, ctx, dY): Y, = ctx.saved_tensors gppr = Y.sqrt() dX = dY * gppr q = dX.sum(ctx.dim) / gppr.sum(ctx.dim) q = q.unsqueeze(ctx.dim) dX -= q * gppr return dX, None, None class Entmax15New(nn.Module): def __init__(self, dim=-1, k=None): """1.5-entmax: normalizing sparse transform (a la softmax). Solves the optimization problem: max_p <x, p> - H_1.5(p) s.t. p >= 0, sum(p) == 1. where H_1.5(p) is the Tsallis alpha-entropy with alpha=1.5. Parameters ---------- dim : int The dimension along which to apply 1.5-entmax. k : int or None number of largest elements to partial-sort over. For optimal performance, should be slightly bigger than the expected number of nonzeros in the solution. If the solution is more than k-sparse, this function is recursively called with a 2*k schedule. If `None`, full sorting is performed from the beginning. """ self.dim = dim self.k = k super(Entmax15New, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
mtreviso/entmax
Entmax15
false
10,650
[ "MIT" ]
0
5b029d07fe00d7aacc77c8e684a5796d29287575
https://github.com/mtreviso/entmax/tree/5b029d07fe00d7aacc77c8e684a5796d29287575
fadein_layer
from _paritybench_helpers import _mock_config import torch import torch.nn as nn class fadein_layer(nn.Module): def __init__(self, config): super(fadein_layer, self).__init__() self.alpha = 0.0 def update_alpha(self, delta): self.alpha = self.alpha + delta self.alpha = max(0, min(self.alpha, 1.0)) def forward(self, x): return torch.add(x[0].mul(1.0 - self.alpha), x[1].mul(self.alpha)) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'config': _mock_config()}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_mul_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp3 = tl.load(in_ptr0 + (64 + x0), xmask) tmp1 = 1.0 tmp2 = tmp0 * tmp1 tmp4 = 0.0 tmp5 = tmp3 * tmp4 tmp6 = tmp2 + tmp5 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), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mul_0[grid(64)](arg0_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 return buf0, class fadein_layerNew(nn.Module): def __init__(self, config): super(fadein_layerNew, self).__init__() self.alpha = 0.0 def update_alpha(self, delta): self.alpha = self.alpha + delta self.alpha = max(0, min(self.alpha, 1.0)) def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
mikanCan/PG-GAN
fadein_layer
false
10,651
[ "MIT" ]
0
bc4a1bd2101f836c22a164174381f80b3f5c73c1
https://github.com/mikanCan/PG-GAN/tree/bc4a1bd2101f836c22a164174381f80b3f5c73c1
BertPreTrainingHeads
from _paritybench_helpers import _mock_config import math import torch from torch import nn def gelu(x): """Gaussian Error Linear Unitという活性化関数です。 LeLUが0でカクっと不連続なので、そこを連続になるように滑らかにした形のLeLUです。 """ return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0))) class BertLayerNorm(nn.Module): def __init__(self, hidden_size, eps=1e-12): """LayerNormalization層です。 学習済みモデルをそのままロードするため、学習済みモデルの変数名に変えています。 オリジナルのGitHubの実装から変数名を変えています。 weight→gamma、bias→beta """ super(BertLayerNorm, self).__init__() self.gamma = nn.Parameter(torch.ones(hidden_size)) self.beta = nn.Parameter(torch.zeros(hidden_size)) self.variance_epsilon = eps def forward(self, x): u = x.mean(-1, keepdim=True) s = (x - u).pow(2).mean(-1, keepdim=True) x = (x - u) / torch.sqrt(s + self.variance_epsilon) return self.gamma * x + self.beta class BertPredictionHeadTransform(nn.Module): """MaskedWordPredictionsにて、BERTからの特徴量を変換するモジュール(入出力のサイズは同じ)""" def __init__(self, config): super(BertPredictionHeadTransform, self).__init__() self.dense = nn.Linear(config.hidden_size, config.hidden_size) self.transform_act_fn = gelu self.LayerNorm = BertLayerNorm(config.hidden_size, eps=1e-12) def forward(self, hidden_states): """hidden_statesはsequence_output:[minibatch, seq_len, hidden_size]""" hidden_states = self.dense(hidden_states) hidden_states = self.transform_act_fn(hidden_states) hidden_states = self.LayerNorm(hidden_states) return hidden_states class MaskedWordPredictions(nn.Module): def __init__(self, config): """事前学習課題:Masked Language Model用のモジュール 元の[2]の実装では、BertLMPredictionHeadという名前です。 """ super(MaskedWordPredictions, self).__init__() self.transform = BertPredictionHeadTransform(config) self.decoder = nn.Linear(in_features=config.hidden_size, out_features=config.vocab_size, bias=False) self.bias = nn.Parameter(torch.zeros(config.vocab_size)) def forward(self, hidden_states): """ hidden_states:BERTからの出力[batch_size, seq_len, hidden_size] """ hidden_states = self.transform(hidden_states) hidden_states = self.decoder(hidden_states) + self.bias return hidden_states class BertPreTrainingHeads(nn.Module): """BERTの事前学習課題を行うアダプターモジュール""" def __init__(self, config, bert_model_embedding_weights): super(BertPreTrainingHeads, self).__init__() self.predictions = MaskedWordPredictions(config) self.seq_relationship = nn.Linear(config.hidden_size, 2) def forward(self, sequence_output, pooled_output): """入力情報 sequence_output:[batch_size, seq_len, hidden_size] pooled_output:[batch_size, hidden_size] """ prediction_scores = self.predictions(sequence_output) seq_relationship_score = self.seq_relationship(pooled_output) return prediction_scores, seq_relationship_score def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'config': _mock_config(hidden_size=4, vocab_size=4), 'bert_model_embedding_weights': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import math from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_div_erf_mean_mul_pow_sub_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') tmp9 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp16 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp23 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp1 = 0.5 tmp2 = tmp0 * tmp1 tmp3 = 0.7071067811865475 tmp4 = tmp0 * tmp3 tmp5 = libdevice.erf(tmp4) tmp6 = 1.0 tmp7 = tmp5 + tmp6 tmp8 = tmp2 * tmp7 tmp10 = tmp9 * tmp1 tmp11 = tmp9 * tmp3 tmp12 = libdevice.erf(tmp11) tmp13 = tmp12 + tmp6 tmp14 = tmp10 * tmp13 tmp15 = tmp8 + tmp14 tmp17 = tmp16 * tmp1 tmp18 = tmp16 * tmp3 tmp19 = libdevice.erf(tmp18) tmp20 = tmp19 + tmp6 tmp21 = tmp17 * tmp20 tmp22 = tmp15 + tmp21 tmp24 = tmp23 * tmp1 tmp25 = tmp23 * tmp3 tmp26 = libdevice.erf(tmp25) tmp27 = tmp26 + tmp6 tmp28 = tmp24 * tmp27 tmp29 = tmp22 + tmp28 tmp30 = 4.0 tmp31 = tmp29 / tmp30 tmp32 = tmp8 - tmp31 tmp33 = tmp32 * tmp32 tmp34 = tmp14 - tmp31 tmp35 = tmp34 * tmp34 tmp36 = tmp33 + tmp35 tmp37 = tmp21 - tmp31 tmp38 = tmp37 * tmp37 tmp39 = tmp36 + tmp38 tmp40 = tmp28 - tmp31 tmp41 = tmp40 * tmp40 tmp42 = tmp39 + tmp41 tmp43 = tmp42 / tmp30 tl.store(out_ptr0 + x0, tmp31, xmask) tl.store(out_ptr1 + x0, tmp43, xmask) @triton.jit def triton_poi_fused_add_div_erf_mul_sqrt_sub_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 x0 = xindex % 4 x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask) tmp10 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp18 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last') tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = 0.7071067811865475 tmp5 = tmp1 * tmp4 tmp6 = libdevice.erf(tmp5) tmp7 = 1.0 tmp8 = tmp6 + tmp7 tmp9 = tmp3 * tmp8 tmp11 = tmp9 - tmp10 tmp13 = 1e-12 tmp14 = tmp12 + tmp13 tmp15 = libdevice.sqrt(tmp14) tmp16 = tmp11 / tmp15 tmp17 = tmp0 * tmp16 tmp19 = tmp17 + tmp18 tl.store(out_ptr0 + x2, tmp19, xmask) @triton.jit def triton_poi_fused_add_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 tl.store(in_out_ptr0 + x2, tmp2, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10) = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4,), (1,)) 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, (2, 4), (4, 1)) assert_size_stride(primals_9, (2,), (1,)) assert_size_stride(primals_10, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf2 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_erf_mean_mul_pow_sub_0[grid(64)](buf0, buf1, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_add_div_erf_mul_sqrt_sub_1[grid(256)](primals_4, buf0, buf1, buf2, primals_5, buf3, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf1 del buf2 del primals_5 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_add_2[grid(256)](buf5, primals_7, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 buf6 = empty_strided_cuda((64, 2), (2, 1), torch.float32) extern_kernels.addmm(primals_9, reinterpret_tensor(primals_10, (64, 4), (4, 1), 0), reinterpret_tensor(primals_8, (4, 2), (1, 4), 0 ), alpha=1, beta=1, out=buf6) del primals_8 del primals_9 return buf5, reinterpret_tensor(buf6, (4, 4, 4, 2), (32, 8, 2, 1), 0 ), primals_4, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf0, reinterpret_tensor(buf3, (64, 4), (4, 1), 0 ), reinterpret_tensor(primals_10, (64, 4), (4, 1), 0), primals_6 def gelu(x): """Gaussian Error Linear Unitという活性化関数です。 LeLUが0でカクっと不連続なので、そこを連続になるように滑らかにした形のLeLUです。 """ return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0))) class BertLayerNorm(nn.Module): def __init__(self, hidden_size, eps=1e-12): """LayerNormalization層です。 学習済みモデルをそのままロードするため、学習済みモデルの変数名に変えています。 オリジナルのGitHubの実装から変数名を変えています。 weight→gamma、bias→beta """ super(BertLayerNorm, self).__init__() self.gamma = nn.Parameter(torch.ones(hidden_size)) self.beta = nn.Parameter(torch.zeros(hidden_size)) self.variance_epsilon = eps def forward(self, x): u = x.mean(-1, keepdim=True) s = (x - u).pow(2).mean(-1, keepdim=True) x = (x - u) / torch.sqrt(s + self.variance_epsilon) return self.gamma * x + self.beta class BertPredictionHeadTransform(nn.Module): """MaskedWordPredictionsにて、BERTからの特徴量を変換するモジュール(入出力のサイズは同じ)""" def __init__(self, config): super(BertPredictionHeadTransform, self).__init__() self.dense = nn.Linear(config.hidden_size, config.hidden_size) self.transform_act_fn = gelu self.LayerNorm = BertLayerNorm(config.hidden_size, eps=1e-12) def forward(self, hidden_states): """hidden_statesはsequence_output:[minibatch, seq_len, hidden_size]""" hidden_states = self.dense(hidden_states) hidden_states = self.transform_act_fn(hidden_states) hidden_states = self.LayerNorm(hidden_states) return hidden_states class MaskedWordPredictions(nn.Module): def __init__(self, config): """事前学習課題:Masked Language Model用のモジュール 元の[2]の実装では、BertLMPredictionHeadという名前です。 """ super(MaskedWordPredictions, self).__init__() self.transform = BertPredictionHeadTransform(config) self.decoder = nn.Linear(in_features=config.hidden_size, out_features=config.vocab_size, bias=False) self.bias = nn.Parameter(torch.zeros(config.vocab_size)) def forward(self, hidden_states): """ hidden_states:BERTからの出力[batch_size, seq_len, hidden_size] """ hidden_states = self.transform(hidden_states) hidden_states = self.decoder(hidden_states) + self.bias return hidden_states class BertPreTrainingHeadsNew(nn.Module): """BERTの事前学習課題を行うアダプターモジュール""" def __init__(self, config, bert_model_embedding_weights): super(BertPreTrainingHeadsNew, self).__init__() self.predictions = MaskedWordPredictions(config) self.seq_relationship = nn.Linear(config.hidden_size, 2) def forward(self, input_0, input_1): primals_2 = self.predictions.bias primals_1 = self.predictions.transform.dense.weight primals_4 = self.predictions.transform.dense.bias primals_5 = self.predictions.transform.LayerNorm.gamma primals_7 = self.predictions.transform.LayerNorm.beta primals_6 = self.predictions.decoder.weight primals_8 = self.seq_relationship.weight primals_9 = self.seq_relationship.bias primals_3 = 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]) return output[0], output[1]
Cyndi-Tokyotech/Fin_Text_Analysis_ML
BertPreTrainingHeads
false
10,652
[ "MIT" ]
0
7f9b6c1ea78f8e6f32c003b2de32809722df88d4
https://github.com/Cyndi-Tokyotech/Fin_Text_Analysis_ML/tree/7f9b6c1ea78f8e6f32c003b2de32809722df88d4
ReshapeNet
import torch import torch.nn.functional class ReshapeNet(torch.nn.Module): def __init__(self): super(ReshapeNet, self).__init__() self.conv1 = torch.nn.Conv2d(3, 4, kernel_size=1, stride=1) def forward(self, x): x = self.conv1(x) batch, channels, height, width = x.size() x = x * height channels = channels + width channels = channels - width x = torch.transpose(x, 1, 2).contiguous() x = x.view(-1, height, width, channels) batch, channels, height, width = x.size() height = height + batch height = height - batch x = torch.transpose(x, 1, 2) return x.reshape(-1, channels, height, width) 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 import torch.nn.functional 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): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x0 = xindex % 64 x1 = xindex // 64 % 4 x2 = xindex // 256 % 64 x3 = xindex // 16384 x4 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 64 * x2 + 4096 * x1 + 16384 * x3), None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 64.0 tmp4 = tmp2 * tmp3 tl.store(out_ptr0 + x4, tmp4, None) @triton.jit def triton_poi_fused_transpose_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x4 = xindex tmp0 = tl.load(in_ptr0 + x4, None) tl.store(out_ptr0 + x4, tmp0, None) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 3, 64, 64), (12288, 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, 64, 64), (16384, 4096, 64, 1)) buf1 = empty_strided_cuda((4, 64, 4, 64), (16384, 256, 64, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clone_0[grid(65536)](buf0, primals_2, buf1, 65536, XBLOCK=512, num_warps=4, num_stages=1) del primals_2 buf2 = reinterpret_tensor(buf0, (4, 64, 64, 4), (16384, 4, 256, 1), 0) del buf0 triton_poi_fused_transpose_1[grid(65536)](buf1, buf2, 65536, XBLOCK =256, num_warps=4, num_stages=1) del buf1 return buf2, primals_1, primals_3 class ReshapeNetNew(torch.nn.Module): def __init__(self): super(ReshapeNetNew, self).__init__() self.conv1 = torch.nn.Conv2d(3, 4, kernel_size=1, stride=1) 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]
elad-c/model_optimization
ReshapeNet
false
10,653
[ "Apache-2.0" ]
0
b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
https://github.com/elad-c/model_optimization/tree/b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
MultiHeadAttention
import math import torch import torch.nn.functional as F import torch.nn as nn class MultiHeadAttention(nn.Module): def __init__(self, heads, d_model, dropout=0.1): super().__init__() self.d_model = d_model self.d_k = d_model // heads self.h = heads self.q_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.v_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.k_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.dropout = nn.Dropout(dropout) self.out = nn.Linear(d_model, d_model) def forward(self, q, k, v, mask=None, dropout=None): bs = q.size(0) if q.size(1) > 230: self.h = 8 self.d_k = self.d_model // self.h elif q.size(1) <= 138 and q.size(1) > 230: self.h = 4 self.d_k = self.d_model // self.h elif q.size(1) <= 138 and q.size(1) > 0: self.h = 2 self.d_k = self.d_model // self.h k = torch.matmul(k, self.k_linear1) k = k.view(bs, -1, self.h, self.d_k) q = torch.matmul(q, self.q_linear1) q = q.view(bs, -1, self.h, self.d_k) v = torch.matmul(v, self.v_linear1) v = v.view(bs, -1, self.h, self.d_k) q = q.transpose(1, 2) k = k.transpose(1, 2) v = v.transpose(1, 2) k = k.transpose(-2, -1) scores = torch.matmul(q, k) scores = scores / math.sqrt(self.d_k) if mask is not None: mask = mask.unsqueeze(1) scores = scores.masked_fill(mask == 0, -1000000000.0) scores = F.softmax(scores, dim=-1) if dropout is not None: scores = dropout(scores) scores = torch.matmul(scores, v) concat = scores.transpose(1, 2).contiguous().view(bs, -1, self.d_model) output = self.out(concat) return output def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand( [4, 4, 4, 4])] def get_init_inputs(): return [[], {'heads': 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 from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 2 x1 = xindex // 2 % 16 x2 = xindex // 32 % 2 x3 = xindex // 64 x4 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 2 * x2 + 4 * x1 + 64 * x3), xmask) tmp1 = 0.8408964152537145 tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x4, tmp2, xmask) @triton.jit def triton_poi_fused_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 16 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 64 * y1), xmask & ymask) tmp1 = 0.8408964152537145 tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + (x2 + 16 * y3), tmp2, xmask & ymask) @triton.jit def triton_per_fused_2(in_ptr0, out_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr ): xnumel = 128 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, float('-inf')) tmp4 = triton_helpers.max2(tmp3, 1)[:, None] tmp5 = tmp0 - tmp4 tmp6 = tl_math.exp(tmp5) tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK]) tmp9 = tl.where(xmask, tmp7, 0) tmp10 = tl.sum(tmp9, 1)[:, None] tmp11 = float('-inf') tmp12 = tmp0 == tmp11 tmp13 = tmp12 == 0 tmp14 = tmp13.to(tl.int64) tmp15 = tmp14 != 0 tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK]) tmp18 = tl.where(xmask, tmp16, 0) tmp19 = triton_helpers.any(tmp18, 1)[:, None] tmp20 = tmp19 == 0 tmp21 = tmp6 / tmp10 tmp22 = 0.0 tmp23 = tl.where(tmp20, tmp22, tmp21) tl.store(out_ptr3 + (r1 + 16 * x0), tmp23, xmask) @triton.jit def triton_poi_fused_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 x0 = xindex % 2 x1 = xindex // 2 % 16 x2 = xindex // 32 % 2 x3 = xindex // 64 x4 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 2 * x2 + 4 * x1 + 64 * x3), xmask) tl.store(out_ptr0 + x4, tmp0, 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 % 2 x1 = xindex // 2 % 2 x2 = xindex // 4 % 16 x3 = xindex // 64 x4 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 2 * x2 + 32 * x1 + 64 * x3), xmask) tl.store(out_ptr0 + x4, tmp0, 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, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4, 4), (4, 1)) assert_size_stride(primals_6, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_7, (4, 4), (4, 1)) assert_size_stride(primals_8, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), primals_2, out=buf0) del primals_2 buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), primals_4, out=buf1) del primals_4 buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_6, (64, 4), (4, 1), 0), primals_5, out=buf2) del primals_5 buf3 = empty_strided_cuda((4, 2, 16, 2), (64, 32, 2, 1), torch.float32) get_raw_stream(0) triton_poi_fused_0[grid(256)](buf1, buf3, 256, XBLOCK=128, num_warps=4, num_stages=1) buf4 = reinterpret_tensor(buf1, (4, 2, 2, 16), (64, 32, 16, 1), 0) del buf1 triton_poi_fused_1[grid(16, 16)](buf0, buf4, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) buf5 = empty_strided_cuda((8, 16, 16), (256, 16, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf3, (8, 16, 2), (32, 2, 1), 0), reinterpret_tensor(buf4, (8, 2, 16), (32, 16, 1), 0), out=buf5) buf9 = empty_strided_cuda((4, 2, 16, 16), (512, 256, 16, 1), torch. float32) triton_per_fused_2[grid(128)](buf5, buf9, 128, 16, XBLOCK=8, num_warps=2, num_stages=1) del buf5 buf10 = reinterpret_tensor(buf0, (4, 2, 16, 2), (64, 32, 2, 1), 0) del buf0 triton_poi_fused_3[grid(256)](buf2, buf10, 256, XBLOCK=256, num_warps=4, num_stages=1) buf11 = reinterpret_tensor(buf2, (8, 16, 2), (32, 2, 1), 0) del buf2 extern_kernels.bmm(reinterpret_tensor(buf9, (8, 16, 16), (256, 16, 1), 0), reinterpret_tensor(buf10, (8, 16, 2), (32, 2, 1), 0), out=buf11) buf12 = empty_strided_cuda((4, 16, 2, 2), (64, 4, 2, 1), torch.float32) triton_poi_fused_clone_4[grid(256)](buf11, buf12, 256, XBLOCK=128, num_warps=4, num_stages=1) buf13 = reinterpret_tensor(buf11, (64, 4), (4, 1), 0) del buf11 extern_kernels.addmm(primals_8, reinterpret_tensor(buf12, (64, 4), (4, 1), 0), reinterpret_tensor(primals_7, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf13) del primals_8 return reinterpret_tensor(buf13, (4, 16, 4), (64, 4, 1), 0 ), buf9, reinterpret_tensor(buf10, (8, 2, 16), (32, 1, 2), 0 ), reinterpret_tensor(buf3, (8, 2, 16), (32, 1, 2), 0 ), reinterpret_tensor(buf4, (8, 16, 2), (32, 1, 16), 0 ), reinterpret_tensor(buf12, (64, 4), (4, 1), 0 ), primals_7, reinterpret_tensor(primals_6, (4, 64), (1, 4), 0 ), reinterpret_tensor(primals_1, (4, 64), (1, 4), 0 ), reinterpret_tensor(primals_3, (4, 64), (1, 4), 0) class MultiHeadAttentionNew(nn.Module): def __init__(self, heads, d_model, dropout=0.1): super().__init__() self.d_model = d_model self.d_k = d_model // heads self.h = heads self.q_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.v_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.k_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.dropout = nn.Dropout(dropout) self.out = nn.Linear(d_model, d_model) def forward(self, input_0, input_1, input_2): primals_2 = self.q_linear1 primals_4 = self.v_linear1 primals_5 = self.k_linear1 primals_7 = self.out.weight primals_8 = self.out.bias primals_1 = input_0 primals_3 = input_1 primals_6 = input_2 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8]) return output[0]
nlakshmanan/Transformer
MultiHeadAttention
false
10,654
[ "Apache-2.0" ]
0
4562f8e9b282d0a70f26903a7b4410cb6132364b
https://github.com/nlakshmanan/Transformer/tree/4562f8e9b282d0a70f26903a7b4410cb6132364b
ReuseLayerNet
import torch import torch.nn.functional class ReuseLayerNet(torch.nn.Module): def __init__(self): super(ReuseLayerNet, self).__init__() self.conv1 = torch.nn.Conv2d(3, 3, kernel_size=1, stride=1) self.conv2 = torch.nn.Conv2d(3, 3, kernel_size=1, stride=1) self.identity = torch.nn.Identity() def forward(self, x, y): x = self.conv1(x) x = self.identity(x) x = self.conv1(x) x = self.identity(x) x = self.conv1(x) x = self.identity(x) x = self.conv2(x) x = self.identity(x) x = self.conv2(x) x = self.identity(x) x = self.conv2(x) x = self.identity(x) y = self.conv2(y) y = self.identity(y) y = self.conv2(y) y = self.identity(y) y = self.conv1(y) y = self.identity(y) y = self.conv1(y) y = self.identity(y) y = self.conv1(y) return x - y, y - x def get_inputs(): return [torch.rand([4, 3, 64, 64]), 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 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_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 tl.store(in_out_ptr0 + x3, tmp2, None) @triton.jit def triton_poi_fused_convolution_1(in_out_ptr0, in_out_ptr1, 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') tmp3 = tl.load(in_out_ptr1 + x3, None) tmp2 = tmp0 + tmp1 tmp4 = tmp3 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, None) tl.store(in_out_ptr1 + x3, tmp4, None) @triton.jit def triton_poi_fused_convolution_sub_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 3 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x3, None) tmp4 = tl.load(in_ptr3 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 - tmp5 tmp7 = tmp5 - tmp2 tl.store(out_ptr0 + x3, tmp6, None) tl.store(out_ptr1 + x3, tmp7, None) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args args.clear() assert_size_stride(primals_1, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_2, (3,), (1,)) assert_size_stride(primals_3, (4, 3, 64, 64), (12288, 4096, 64, 1)) assert_size_stride(primals_4, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_5, (3,), (1,)) assert_size_stride(primals_6, (4, 3, 64, 64), (12288, 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, 3, 64, 64), (12288, 4096, 64, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(49152)](buf1, primals_2, 49152, XBLOCK=512, num_warps=4, num_stages=1) buf2 = extern_kernels.convolution(buf1, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_0[grid(49152)](buf3, primals_2, 49152, XBLOCK=512, num_warps=4, num_stages=1) buf4 = extern_kernels.convolution(buf3, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_0[grid(49152)](buf5, primals_2, 49152, XBLOCK=512, num_warps=4, num_stages=1) buf6 = extern_kernels.convolution(buf5, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf6, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf7 = buf6 del buf6 triton_poi_fused_convolution_0[grid(49152)](buf7, primals_5, 49152, XBLOCK=512, num_warps=4, num_stages=1) buf8 = extern_kernels.convolution(buf7, primals_4, 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, 64, 64), (12288, 4096, 64, 1)) buf11 = extern_kernels.convolution(primals_6, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf11, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf9 = buf8 del buf8 buf12 = buf11 del buf11 triton_poi_fused_convolution_1[grid(49152)](buf9, buf12, primals_5, 49152, XBLOCK=512, num_warps=4, num_stages=1) buf10 = extern_kernels.convolution(buf9, primals_4, stride=(1, 1), padding=(0, 0), 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)) buf13 = extern_kernels.convolution(buf12, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf13, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf14 = buf13 del buf13 triton_poi_fused_convolution_0[grid(49152)](buf14, primals_5, 49152, XBLOCK=512, num_warps=4, num_stages=1) buf15 = extern_kernels.convolution(buf14, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf15, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf16 = buf15 del buf15 triton_poi_fused_convolution_0[grid(49152)](buf16, primals_2, 49152, XBLOCK=512, num_warps=4, num_stages=1) buf17 = extern_kernels.convolution(buf16, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf17, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf18 = buf17 del buf17 triton_poi_fused_convolution_0[grid(49152)](buf18, primals_2, 49152, XBLOCK=512, num_warps=4, num_stages=1) buf19 = extern_kernels.convolution(buf18, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf19, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf20 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.float32) buf21 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.float32) triton_poi_fused_convolution_sub_2[grid(49152)](buf10, primals_5, buf19, primals_2, buf20, buf21, 49152, XBLOCK=512, num_warps=4, num_stages=1) del buf10 del buf19 del primals_2 del primals_5 return (buf20, buf21, primals_1, primals_3, primals_4, primals_6, buf1, buf3, buf5, buf7, buf9, buf12, buf14, buf16, buf18) class ReuseLayerNetNew(torch.nn.Module): def __init__(self): super(ReuseLayerNetNew, self).__init__() self.conv1 = torch.nn.Conv2d(3, 3, kernel_size=1, stride=1) self.conv2 = torch.nn.Conv2d(3, 3, kernel_size=1, stride=1) self.identity = torch.nn.Identity() def forward(self, input_0, input_1): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.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], output[1]
elad-c/model_optimization
ReuseLayerNet
false
10,655
[ "Apache-2.0" ]
0
b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
https://github.com/elad-c/model_optimization/tree/b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
BinaryLoss
import functools import torch import torch.nn.functional as F import torch.nn as nn import torch._C import torch.serialization def binary_ce_loss(pred, label, **kwargs): loss = F.binary_cross_entropy(pred, label, reduction='none') loss = torch.mean(loss, dim=(1, 2)) return loss def reduce_loss(loss, reduction): """Reduce loss as specified. Args: loss (Tensor): Elementwise loss tensor. reduction (str): Options are "none", "mean" and "sum". Return: Tensor: Reduced loss tensor. """ reduction_enum = F._Reduction.get_enum(reduction) if reduction_enum == 0: return loss elif reduction_enum == 1: return loss.mean() elif reduction_enum == 2: return loss.sum() def weight_reduce_loss(loss, weight=None, reduction='mean', avg_factor=None): """Apply element-wise weight and reduce loss. Args: loss (Tensor): Element-wise loss. weight (Tensor): Element-wise weights. reduction (str): Same as built-in losses of PyTorch. avg_factor (float): Avarage factor when computing the mean of losses. Returns: Tensor: Processed loss values. """ if weight is not None: assert weight.dim() == loss.dim() if weight.dim() > 1: assert weight.size(1) == 1 or weight.size(1) == loss.size(1) loss = loss * weight if avg_factor is None: loss = reduce_loss(loss, reduction) elif reduction == 'mean': loss = loss.sum() / avg_factor elif reduction != 'none': raise ValueError('avg_factor can not be used with reduction="sum"') return loss def weighted_loss(loss_func): """Create a weighted version of a given loss function. To use this decorator, the loss function must have the signature like `loss_func(pred, target, **kwargs)`. The function only needs to compute element-wise loss without any reduction. This decorator will add weight and reduction arguments to the function. The decorated function will have the signature like `loss_func(pred, target, weight=None, reduction='mean', avg_factor=None, **kwargs)`. :Example: >>> import torch >>> @weighted_loss >>> def l1_loss(pred, target): >>> return (pred - target).abs() >>> pred = torch.Tensor([0, 2, 3]) >>> target = torch.Tensor([1, 1, 1]) >>> weight = torch.Tensor([1, 0, 1]) >>> l1_loss(pred, target) tensor(1.3333) >>> l1_loss(pred, target, weight) tensor(1.) >>> l1_loss(pred, target, reduction='none') tensor([1., 1., 2.]) >>> l1_loss(pred, target, weight, avg_factor=2) tensor(1.5000) """ @functools.wraps(loss_func) def wrapper(pred, target, weight=None, reduction='mean', avg_factor= None, **kwargs): loss = loss_func(pred, target, **kwargs) loss = weight_reduce_loss(loss, weight, reduction, avg_factor) return loss return wrapper @weighted_loss def binary_dice_loss(pred, target, valid_mask, smooth=1, exponent=2, **kwards): assert pred.shape[0] == target.shape[0] pred = pred.contiguous().view(pred.shape[0], -1) target = target.contiguous().view(target.shape[0], -1) valid_mask = valid_mask.contiguous().view(valid_mask.shape[0], -1) num = torch.sum(torch.mul(pred, target) * valid_mask, dim=1) * 2 + smooth den = torch.sum(pred.pow(exponent) + target.pow(exponent), dim=1) + smooth return 1 - num / den def binary_ce_dice_loss(pred, label, smooth=1.0, **kwargs): loss1 = binary_ce_loss(pred, label, **kwargs) loss2 = binary_dice_loss(pred, label, smooth=smooth) return loss1 + loss2 def _make_one_hot(gt, num_classes, ignore=(0, 255)): """ :param label: [N, *], values in [0,num_classes) :param ignore: ignore value of background, here is (0, 255) :return: [N, C, *] """ label = gt label = label.unsqueeze(1) shape = list(label.shape) shape[1] = num_classes + 1 if ignore is not None: if 0 in ignore: for index in ignore: label[label == index] = num_classes + 1 label = label - 1 else: for index in ignore: label[label == index] = num_classes result = torch.zeros(shape, device=label.device) result.scatter_(1, label, 1) return result[:, :-1] def binary_loss(pred_raw, label_raw, loss_func, weight=None, class_weight= None, class_weight_norm=False, reduction='mean', avg_factor=None, smooth=1.0, **kwargs): """ :param pred: [N, C, *] scores without softmax :param label: [N, *] in [0, C], 0 stands for background, 1~C stands for pred in 0~C-1 :return: reduction([N]) """ pred = pred_raw.clone() label = label_raw.clone() num_classes = pred.shape[1] if class_weight is not None: class_weight = class_weight.float() if pred.shape != label.shape: label = _make_one_hot(label, num_classes) pred = torch.sigmoid(pred) loss = 0.0 for i in range(num_classes): if isinstance(loss_func, tuple): loss_function = loss_func[i] else: loss_function = loss_func class_loss = loss_function(pred[:, i], label[:, i], smooth=smooth) if class_weight is not None: class_loss *= class_weight[i] loss += class_loss if class_weight is not None and class_weight_norm: loss = loss / torch.sum(class_weight) else: loss = loss / num_classes loss = weight_reduce_loss(loss, weight=weight, reduction=reduction, avg_factor=avg_factor) return loss class BinaryLoss(nn.Module): def __init__(self, loss_type='ce', reduction='mean', class_weight=None, class_weight_norm=False, loss_weight=1.0, smooth=1.0, **kwargs): super(BinaryLoss, self).__init__() assert loss_type in ['ce', 'dice', 'ce_dice'] self.reduction = reduction self.loss_weight = loss_weight self.class_weight = class_weight self.class_weight_norm = class_weight_norm self.loss_type = loss_type self.smooth = smooth def forward(self, cls_score, label, weight=None, avg_factor=None, reduction_override=None, **kwargs): assert reduction_override in (None, 'none', 'mean', 'sum') reduction = (reduction_override if reduction_override else self. reduction) if self.class_weight is not None: class_weight = cls_score.new_tensor(self.class_weight) assert class_weight.shape[0] == cls_score.shape[1 ], 'Expect weight shape [{}], get[{}]'.format(cls_score. shape[1], class_weight.shape[0]) else: class_weight = None loss_func = None if self.loss_type == 'ce': loss_func = binary_ce_loss elif self.loss_type == 'dice': loss_func = binary_dice_loss elif self.loss_type == 'ce_dice': loss_func = binary_ce_dice_loss loss_cls = self.loss_weight * binary_loss(cls_score, label, loss_func, weight, class_weight=class_weight, class_weight_norm =self.class_weight_norm, reduction=reduction, avg_factor= avg_factor, smooth=self.smooth) return loss_cls def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import functools import torch.nn.functional as F import torch.nn as nn import torch._C import torch.serialization 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_mean_0(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 64 * x0), xmask, other=0.0) tmp3 = tl.load(in_ptr1 + (r1 + 64 * x0), xmask, other=0.0) tmp1 = 1.0 tmp2 = tmp0 - tmp1 tmp4 = tl.sigmoid(tmp3) tmp5 = -tmp4 tmp6 = libdevice.log1p(tmp5) tmp7 = -100.0 tmp8 = triton_helpers.maximum(tmp6, tmp7) tmp9 = tmp2 * tmp8 tmp10 = tl_math.log(tmp4) tmp11 = triton_helpers.maximum(tmp10, tmp7) tmp12 = tmp0 * tmp11 tmp13 = tmp9 - tmp12 tmp14 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK]) tmp16 = tl.where(xmask, tmp14, 0) tmp17 = tl.sum(tmp16, 1)[:, None] tl.store(out_ptr0 + x0, tmp17, xmask) @triton.jit def triton_per_fused_binary_cross_entropy_mean_1(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (16 + r1 + 64 * x0), xmask, other=0.0) tmp3 = tl.load(in_ptr1 + (16 + r1 + 64 * x0), xmask, other=0.0) tmp1 = 1.0 tmp2 = tmp0 - tmp1 tmp4 = tl.sigmoid(tmp3) tmp5 = -tmp4 tmp6 = libdevice.log1p(tmp5) tmp7 = -100.0 tmp8 = triton_helpers.maximum(tmp6, tmp7) tmp9 = tmp2 * tmp8 tmp10 = tl_math.log(tmp4) tmp11 = triton_helpers.maximum(tmp10, tmp7) tmp12 = tmp0 * tmp11 tmp13 = tmp9 - tmp12 tmp14 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK]) tmp16 = tl.where(xmask, tmp14, 0) tmp17 = tl.sum(tmp16, 1)[:, None] tl.store(out_ptr0 + x0, tmp17, xmask) @triton.jit def triton_per_fused_binary_cross_entropy_mean_2(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (32 + r1 + 64 * x0), xmask, other=0.0) tmp3 = tl.load(in_ptr1 + (32 + r1 + 64 * x0), xmask, other=0.0) tmp1 = 1.0 tmp2 = tmp0 - tmp1 tmp4 = tl.sigmoid(tmp3) tmp5 = -tmp4 tmp6 = libdevice.log1p(tmp5) tmp7 = -100.0 tmp8 = triton_helpers.maximum(tmp6, tmp7) tmp9 = tmp2 * tmp8 tmp10 = tl_math.log(tmp4) tmp11 = triton_helpers.maximum(tmp10, tmp7) tmp12 = tmp0 * tmp11 tmp13 = tmp9 - tmp12 tmp14 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK]) tmp16 = tl.where(xmask, tmp14, 0) tmp17 = tl.sum(tmp16, 1)[:, None] tl.store(out_ptr0 + x0, tmp17, xmask) @triton.jit def triton_per_fused_binary_cross_entropy_mean_3(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (48 + r1 + 64 * x0), xmask, other=0.0) tmp3 = tl.load(in_ptr1 + (48 + r1 + 64 * x0), xmask, other=0.0) tmp1 = 1.0 tmp2 = tmp0 - tmp1 tmp4 = tl.sigmoid(tmp3) tmp5 = -tmp4 tmp6 = libdevice.log1p(tmp5) tmp7 = -100.0 tmp8 = triton_helpers.maximum(tmp6, tmp7) tmp9 = tmp2 * tmp8 tmp10 = tl_math.log(tmp4) tmp11 = triton_helpers.maximum(tmp10, tmp7) tmp12 = tmp0 * tmp11 tmp13 = tmp9 - tmp12 tmp14 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK]) tmp16 = tl.where(xmask, tmp14, 0) tmp17 = tl.sum(tmp16, 1)[:, None] tl.store(out_ptr0 + x0, tmp17, xmask) @triton.jit def triton_per_fused_add_binary_cross_entropy_div_mean_mul_4(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 4 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp5 = tl.load(in_ptr1 + r0, None) tmp8 = tl.load(in_ptr2 + r0, None) tmp11 = tl.load(in_ptr3 + r0, None) tmp1 = 16.0 tmp2 = tmp0 / tmp1 tmp3 = 0.0 tmp4 = tmp2 + tmp3 tmp6 = tmp5 / tmp1 tmp7 = tmp4 + tmp6 tmp9 = tmp8 / tmp1 tmp10 = tmp7 + tmp9 tmp12 = tmp11 / tmp1 tmp13 = tmp10 + tmp12 tmp14 = 0.25 tmp15 = tmp13 * tmp14 tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK]) tmp18 = tl.sum(tmp16, 1)[:, None] tmp19 = 4.0 tmp20 = tmp18 / tmp19 tmp21 = 1.0 tmp22 = tmp20 * tmp21 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp22, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4,), (1,), torch.float32) get_raw_stream(0) triton_per_fused_binary_cross_entropy_mean_0[grid(4)](arg1_1, arg0_1, buf0, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) buf1 = empty_strided_cuda((4,), (1,), torch.float32) triton_per_fused_binary_cross_entropy_mean_1[grid(4)](arg1_1, arg0_1, buf1, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) buf2 = empty_strided_cuda((4,), (1,), torch.float32) triton_per_fused_binary_cross_entropy_mean_2[grid(4)](arg1_1, arg0_1, buf2, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) buf3 = empty_strided_cuda((4,), (1,), torch.float32) triton_per_fused_binary_cross_entropy_mean_3[grid(4)](arg1_1, arg0_1, buf3, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 buf4 = empty_strided_cuda((), (), torch.float32) buf5 = buf4 del buf4 triton_per_fused_add_binary_cross_entropy_div_mean_mul_4[grid(1)](buf5, buf0, buf1, buf2, buf3, 1, 4, XBLOCK=1, num_warps=2, num_stages=1) del buf0 del buf1 del buf2 del buf3 return buf5, def binary_ce_loss(pred, label, **kwargs): loss = F.binary_cross_entropy(pred, label, reduction='none') loss = torch.mean(loss, dim=(1, 2)) return loss def reduce_loss(loss, reduction): """Reduce loss as specified. Args: loss (Tensor): Elementwise loss tensor. reduction (str): Options are "none", "mean" and "sum". Return: Tensor: Reduced loss tensor. """ reduction_enum = F._Reduction.get_enum(reduction) if reduction_enum == 0: return loss elif reduction_enum == 1: return loss.mean() elif reduction_enum == 2: return loss.sum() def weight_reduce_loss(loss, weight=None, reduction='mean', avg_factor=None): """Apply element-wise weight and reduce loss. Args: loss (Tensor): Element-wise loss. weight (Tensor): Element-wise weights. reduction (str): Same as built-in losses of PyTorch. avg_factor (float): Avarage factor when computing the mean of losses. Returns: Tensor: Processed loss values. """ if weight is not None: assert weight.dim() == loss.dim() if weight.dim() > 1: assert weight.size(1) == 1 or weight.size(1) == loss.size(1) loss = loss * weight if avg_factor is None: loss = reduce_loss(loss, reduction) elif reduction == 'mean': loss = loss.sum() / avg_factor elif reduction != 'none': raise ValueError('avg_factor can not be used with reduction="sum"') return loss def weighted_loss(loss_func): """Create a weighted version of a given loss function. To use this decorator, the loss function must have the signature like `loss_func(pred, target, **kwargs)`. The function only needs to compute element-wise loss without any reduction. This decorator will add weight and reduction arguments to the function. The decorated function will have the signature like `loss_func(pred, target, weight=None, reduction='mean', avg_factor=None, **kwargs)`. :Example: >>> import torch >>> @weighted_loss >>> def l1_loss(pred, target): >>> return (pred - target).abs() >>> pred = torch.Tensor([0, 2, 3]) >>> target = torch.Tensor([1, 1, 1]) >>> weight = torch.Tensor([1, 0, 1]) >>> l1_loss(pred, target) tensor(1.3333) >>> l1_loss(pred, target, weight) tensor(1.) >>> l1_loss(pred, target, reduction='none') tensor([1., 1., 2.]) >>> l1_loss(pred, target, weight, avg_factor=2) tensor(1.5000) """ @functools.wraps(loss_func) def wrapper(pred, target, weight=None, reduction='mean', avg_factor= None, **kwargs): loss = loss_func(pred, target, **kwargs) loss = weight_reduce_loss(loss, weight, reduction, avg_factor) return loss return wrapper @weighted_loss def binary_dice_loss(pred, target, valid_mask, smooth=1, exponent=2, **kwards): assert pred.shape[0] == target.shape[0] pred = pred.contiguous().view(pred.shape[0], -1) target = target.contiguous().view(target.shape[0], -1) valid_mask = valid_mask.contiguous().view(valid_mask.shape[0], -1) num = torch.sum(torch.mul(pred, target) * valid_mask, dim=1) * 2 + smooth den = torch.sum(pred.pow(exponent) + target.pow(exponent), dim=1) + smooth return 1 - num / den def binary_ce_dice_loss(pred, label, smooth=1.0, **kwargs): loss1 = binary_ce_loss(pred, label, **kwargs) loss2 = binary_dice_loss(pred, label, smooth=smooth) return loss1 + loss2 def _make_one_hot(gt, num_classes, ignore=(0, 255)): """ :param label: [N, *], values in [0,num_classes) :param ignore: ignore value of background, here is (0, 255) :return: [N, C, *] """ label = gt label = label.unsqueeze(1) shape = list(label.shape) shape[1] = num_classes + 1 if ignore is not None: if 0 in ignore: for index in ignore: label[label == index] = num_classes + 1 label = label - 1 else: for index in ignore: label[label == index] = num_classes result = torch.zeros(shape, device=label.device) result.scatter_(1, label, 1) return result[:, :-1] def binary_loss(pred_raw, label_raw, loss_func, weight=None, class_weight= None, class_weight_norm=False, reduction='mean', avg_factor=None, smooth=1.0, **kwargs): """ :param pred: [N, C, *] scores without softmax :param label: [N, *] in [0, C], 0 stands for background, 1~C stands for pred in 0~C-1 :return: reduction([N]) """ pred = pred_raw.clone() label = label_raw.clone() num_classes = pred.shape[1] if class_weight is not None: class_weight = class_weight.float() if pred.shape != label.shape: label = _make_one_hot(label, num_classes) pred = torch.sigmoid(pred) loss = 0.0 for i in range(num_classes): if isinstance(loss_func, tuple): loss_function = loss_func[i] else: loss_function = loss_func class_loss = loss_function(pred[:, i], label[:, i], smooth=smooth) if class_weight is not None: class_loss *= class_weight[i] loss += class_loss if class_weight is not None and class_weight_norm: loss = loss / torch.sum(class_weight) else: loss = loss / num_classes loss = weight_reduce_loss(loss, weight=weight, reduction=reduction, avg_factor=avg_factor) return loss class BinaryLossNew(nn.Module): def __init__(self, loss_type='ce', reduction='mean', class_weight=None, class_weight_norm=False, loss_weight=1.0, smooth=1.0, **kwargs): super(BinaryLossNew, self).__init__() assert loss_type in ['ce', 'dice', 'ce_dice'] self.reduction = reduction self.loss_weight = loss_weight self.class_weight = class_weight self.class_weight_norm = class_weight_norm self.loss_type = loss_type self.smooth = smooth def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
puzzledsky/mmsegmentation-lesion
BinaryLoss
false
10,656
[ "Apache-2.0" ]
0
522efceab6735dfec13acf6f45dc6bfdb35cfd60
https://github.com/puzzledsky/mmsegmentation-lesion/tree/522efceab6735dfec13acf6f45dc6bfdb35cfd60
SoftMaxAvgPoolModel
import torch import torch.cuda import torch.nn import torch.utils.data import torch.fx import torch.utils.tensorboard._pytorch_graph import torch.onnx.symbolic_caffe2 class SoftMaxAvgPoolModel(torch.nn.Module): def __init__(self): super(SoftMaxAvgPoolModel, self).__init__() self.sfmax = torch.nn.Softmax(dim=1) self.avgpool = torch.nn.AvgPool2d(3) def forward(self, inp): x = self.sfmax(inp) return self.avgpool(x) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.cuda import torch.nn import torch.utils.data import torch.fx import torch.utils.tensorboard._pytorch_graph import torch.onnx.symbolic_caffe2 assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x3, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x3, tmp8, xmask) @triton.jit def triton_poi_fused__softmax_avg_pool2d_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 16 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 16 * x0), xmask, eviction_policy='evict_last' ) tmp3 = tl.load(in_ptr0 + (2 + 16 * x0), xmask, eviction_policy='evict_last' ) tmp5 = tl.load(in_ptr0 + (4 + 16 * x0), xmask, eviction_policy='evict_last' ) tmp7 = tl.load(in_ptr0 + (5 + 16 * x0), xmask, eviction_policy='evict_last' ) tmp9 = tl.load(in_ptr0 + (6 + 16 * x0), xmask, eviction_policy='evict_last' ) tmp11 = tl.load(in_ptr0 + (8 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp13 = tl.load(in_ptr0 + (9 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp15 = tl.load(in_ptr0 + (10 + 16 * x0), xmask, eviction_policy= 'evict_last') tmp2 = tmp1 + tmp0 tmp4 = tmp3 + tmp2 tmp6 = tmp5 + tmp4 tmp8 = tmp7 + tmp6 tmp10 = tmp9 + tmp8 tmp12 = tmp11 + tmp10 tmp14 = tmp13 + tmp12 tmp16 = tmp15 + tmp14 tmp17 = 0.1111111111111111 tmp18 = tmp16 * tmp17 tl.store(out_ptr0 + x0, tmp18, 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__softmax_0[grid(256)](arg0_1, buf0, 256, XBLOCK= 256, num_warps=4, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__softmax_1[grid(256)](buf0, buf1, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf0 buf2 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32) triton_poi_fused__softmax_avg_pool2d_2[grid(16)](buf1, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf1 return buf2, class SoftMaxAvgPoolModelNew(torch.nn.Module): def __init__(self): super(SoftMaxAvgPoolModelNew, self).__init__() self.sfmax = torch.nn.Softmax(dim=1) self.avgpool = torch.nn.AvgPool2d(3) def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
quic-araha/aimet
SoftMaxAvgPoolModel
false
10,657
[ "BSD-3-Clause" ]
0
1afd5ce23f06bed74fec9812d5d2ea256ac4a650
https://github.com/quic-araha/aimet/tree/1afd5ce23f06bed74fec9812d5d2ea256ac4a650
HardtanhBoundToPOTNet
import torch from torch.nn.functional import relu from torch.nn import Conv2d from torch.nn import Hardtanh from torch.nn.functional import hardtanh import torch.nn.functional class HardtanhBoundToPOTNet(torch.nn.Module): def __init__(self): super(HardtanhBoundToPOTNet, self).__init__() self.conv1 = Conv2d(3, 3, kernel_size=1, stride=1) self.hardtanh1 = Hardtanh(min_val=0.0, max_val=6.0) self.conv2 = Conv2d(3, 3, kernel_size=1, stride=1) self.conv3 = Conv2d(3, 3, kernel_size=1, stride=1) self.hardtanh2 = Hardtanh(min_val=-2.0, max_val=6.0) self.conv4 = Conv2d(3, 3, kernel_size=1, stride=1) self.conv5 = Conv2d(3, 3, kernel_size=1, stride=1) self.conv6 = Conv2d(3, 3, kernel_size=1, stride=1) self.conv7 = Conv2d(3, 3, kernel_size=1, stride=1) self.hardtanh3 = Hardtanh(min_val=0.0, max_val=4.0) self.conv8 = Conv2d(3, 3, kernel_size=1, stride=1) self.conv9 = Conv2d(3, 3, kernel_size=1, stride=1) def forward(self, inp): x = self.conv1(inp) x = self.hardtanh1(x) x = self.conv2(x) x = relu(x) x = self.conv3(x) x = self.hardtanh2(x) x = self.conv4(x) x = relu(x) x = self.conv5(x) x = hardtanh(x, min_val=0.0, max_val=6.0) x = self.conv6(x) x = relu(x) x = self.conv7(x) x = self.hardtanh3(x) x = self.conv8(x) x = self.conv9(x) x = relu(x) return x def get_inputs(): return [torch.rand([4, 3, 64, 64])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch.nn import Conv2d from torch.nn import Hardtanh 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_hardtanh_hardtanh_backward_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 3 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = 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, None) tl.store(out_ptr1 + x3, tmp9, 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 % 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_hardtanh_hardtanh_backward_2(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 3 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = -2.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, None) tl.store(out_ptr1 + x3, tmp9, None) @triton.jit def triton_poi_fused_convolution_hardtanh_hardtanh_backward_3(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 3 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = 4.0 tmp6 = triton_helpers.minimum(tmp4, tmp5) tmp7 = tmp2 <= tmp3 tmp8 = tmp2 >= tmp5 tmp9 = tmp7 | tmp8 tl.store(out_ptr0 + x3, tmp6, None) tl.store(out_ptr1 + x3, tmp9, None) @triton.jit def triton_poi_fused_convolution_4(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 3 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, None) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_5(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) 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) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x3, tmp4, None) 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) = args args.clear() assert_size_stride(primals_1, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_2, (3,), (1,)) assert_size_stride(primals_3, (4, 3, 64, 64), (12288, 4096, 64, 1)) assert_size_stride(primals_4, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_5, (3,), (1,)) assert_size_stride(primals_6, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_7, (3,), (1,)) assert_size_stride(primals_8, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_9, (3,), (1,)) assert_size_stride(primals_10, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_11, (3,), (1,)) assert_size_stride(primals_12, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_13, (3,), (1,)) assert_size_stride(primals_14, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_15, (3,), (1,)) assert_size_stride(primals_16, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_17, (3,), (1,)) assert_size_stride(primals_18, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_19, (3,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf1 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.float32) buf22 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.bool) get_raw_stream(0) triton_poi_fused_convolution_hardtanh_hardtanh_backward_0[grid(49152)]( buf0, primals_2, buf1, buf22, 49152, XBLOCK=512, num_warps=4, num_stages=1) del primals_2 buf2 = extern_kernels.convolution(buf1, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_relu_1[grid(49152)](buf3, primals_5, 49152, XBLOCK=512, num_warps=4, num_stages=1) del primals_5 buf4 = extern_kernels.convolution(buf3, primals_6, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf5 = buf0 del buf0 buf21 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_hardtanh_hardtanh_backward_2[grid(49152)]( buf4, primals_7, buf5, buf21, 49152, XBLOCK=512, num_warps=4, num_stages=1) del primals_7 buf6 = extern_kernels.convolution(buf5, primals_8, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf6, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf7 = buf6 del buf6 triton_poi_fused_convolution_relu_1[grid(49152)](buf7, primals_9, 49152, XBLOCK=512, num_warps=4, num_stages=1) del primals_9 buf8 = extern_kernels.convolution(buf7, primals_10, 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, 64, 64), (12288, 4096, 64, 1)) buf9 = buf4 del buf4 buf20 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_hardtanh_hardtanh_backward_0[grid(49152)]( buf8, primals_11, buf9, buf20, 49152, XBLOCK=512, num_warps=4, num_stages=1) del primals_11 buf10 = extern_kernels.convolution(buf9, primals_12, stride=(1, 1), padding=(0, 0), 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 triton_poi_fused_convolution_relu_1[grid(49152)](buf11, primals_13, 49152, XBLOCK=512, num_warps=4, num_stages=1) del primals_13 buf12 = extern_kernels.convolution(buf11, primals_14, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf12, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf13 = buf8 del buf8 buf19 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_hardtanh_hardtanh_backward_3[grid(49152)]( buf12, primals_15, buf13, buf19, 49152, XBLOCK=512, num_warps=4, num_stages=1) del buf12 del primals_15 buf14 = extern_kernels.convolution(buf13, primals_16, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf14, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf15 = buf14 del buf14 triton_poi_fused_convolution_4[grid(49152)](buf15, primals_17, 49152, XBLOCK=512, num_warps=4, num_stages=1) del primals_17 buf16 = extern_kernels.convolution(buf15, primals_18, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf16, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf17 = buf16 del buf16 buf18 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_5[grid(49152)]( buf17, primals_19, buf18, 49152, XBLOCK=512, num_warps=4, num_stages=1) del primals_19 return (buf17, primals_1, primals_3, primals_4, primals_6, primals_8, primals_10, primals_12, primals_14, primals_16, primals_18, buf1, buf3, buf5, buf7, buf9, buf11, buf13, buf15, buf18, buf19, buf20, buf21, buf22) class HardtanhBoundToPOTNetNew(torch.nn.Module): def __init__(self): super(HardtanhBoundToPOTNetNew, self).__init__() self.conv1 = Conv2d(3, 3, kernel_size=1, stride=1) self.hardtanh1 = Hardtanh(min_val=0.0, max_val=6.0) self.conv2 = Conv2d(3, 3, kernel_size=1, stride=1) self.conv3 = Conv2d(3, 3, kernel_size=1, stride=1) self.hardtanh2 = Hardtanh(min_val=-2.0, max_val=6.0) self.conv4 = Conv2d(3, 3, kernel_size=1, stride=1) self.conv5 = Conv2d(3, 3, kernel_size=1, stride=1) self.conv6 = Conv2d(3, 3, kernel_size=1, stride=1) self.conv7 = Conv2d(3, 3, kernel_size=1, stride=1) self.hardtanh3 = Hardtanh(min_val=0.0, max_val=4.0) self.conv8 = Conv2d(3, 3, kernel_size=1, stride=1) self.conv9 = Conv2d(3, 3, kernel_size=1, stride=1) def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_6 = self.conv3.weight primals_7 = self.conv3.bias primals_8 = self.conv4.weight primals_9 = self.conv4.bias primals_10 = self.conv5.weight primals_11 = self.conv5.bias primals_12 = self.conv6.weight primals_13 = self.conv6.bias primals_14 = self.conv7.weight primals_15 = self.conv7.bias primals_16 = self.conv8.weight primals_17 = self.conv8.bias primals_18 = self.conv9.weight primals_19 = self.conv9.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]) return output[0]
elad-c/model_optimization
HardtanhBoundToPOTNet
false
10,658
[ "Apache-2.0" ]
0
b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
https://github.com/elad-c/model_optimization/tree/b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
TorchTensorAttrNet
import torch import torch.nn.functional class TorchTensorAttrNet(torch.nn.Module): def __init__(self): super(TorchTensorAttrNet, self).__init__() self.conv1 = torch.nn.Conv2d(3, 4, kernel_size=1, stride=1) def forward(self, x): x = self.conv1(x) x = x * x.size(1) return x.view(1, -1) 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 import torch.nn.functional 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_mul_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 % 4 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 4.0 tmp4 = tmp2 * tmp3 tl.store(in_out_ptr0 + x3, tmp4, None) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 3, 64, 64), (12288, 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, 64, 64), (16384, 4096, 64, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_mul_0[grid(65536)](buf1, primals_2, 65536, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 return reinterpret_tensor(buf1, (1, 65536), (65536, 1), 0 ), primals_1, primals_3 class TorchTensorAttrNetNew(torch.nn.Module): def __init__(self): super(TorchTensorAttrNetNew, self).__init__() self.conv1 = torch.nn.Conv2d(3, 4, kernel_size=1, stride=1) 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]
elad-c/model_optimization
TorchTensorAttrNet
false
10,659
[ "Apache-2.0" ]
0
b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
https://github.com/elad-c/model_optimization/tree/b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
ReLUBoundToPOTNet
import torch from torch.nn import ReLU from torch.nn import ReLU6 from torch.nn.functional import relu from torch.nn.functional import relu6 from torch.nn import Conv2d import torch.nn.functional class ReLUBoundToPOTNet(torch.nn.Module): def __init__(self): super(ReLUBoundToPOTNet, self).__init__() self.conv1 = Conv2d(3, 3, kernel_size=1, stride=1) self.relu1 = ReLU6() self.conv2 = Conv2d(3, 3, kernel_size=1, stride=1) self.conv3 = Conv2d(3, 3, kernel_size=1, stride=1) self.conv4 = Conv2d(3, 3, kernel_size=1, stride=1) self.relu2 = ReLU() self.identity = torch.nn.Identity() def forward(self, inp): x = self.conv1(inp) x = self.relu1(x) x = self.conv2(x) x = self.identity(x) x = self.conv3(x) x = relu6(x) x = self.conv4(x) x = self.relu2(x) x = relu(x) return x def get_inputs(): return [torch.rand([4, 3, 64, 64])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch.nn import ReLU from torch.nn import ReLU6 from torch.nn import Conv2d 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_hardtanh_hardtanh_backward_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 3 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = 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, None) tl.store(out_ptr1 + x3, tmp9, None) @triton.jit def triton_poi_fused_convolution_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 % 3 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, None) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_2(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 3 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 = triton_helpers.maximum(tmp3, tmp4) tmp6 = 0.0 tmp7 = tmp4 <= tmp6 tmp8 = tmp5 <= tmp6 tl.store(out_ptr0 + x3, tmp5, None) tl.store(out_ptr1 + x3, tmp7, None) tl.store(out_ptr2 + x3, tmp8, None) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9) = args args.clear() assert_size_stride(primals_1, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_2, (3,), (1,)) assert_size_stride(primals_3, (4, 3, 64, 64), (12288, 4096, 64, 1)) assert_size_stride(primals_4, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_5, (3,), (1,)) assert_size_stride(primals_6, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_7, (3,), (1,)) assert_size_stride(primals_8, (3, 3, 1, 1), (3, 1, 1, 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=(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 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.float32) buf11 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.bool) get_raw_stream(0) triton_poi_fused_convolution_hardtanh_hardtanh_backward_0[grid(49152)]( buf0, primals_2, buf1, buf11, 49152, XBLOCK=512, num_warps=4, num_stages=1) del primals_2 buf2 = extern_kernels.convolution(buf1, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_1[grid(49152)](buf3, primals_5, 49152, XBLOCK=512, num_warps=4, num_stages=1) del primals_5 buf4 = extern_kernels.convolution(buf3, primals_6, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf5 = buf0 del buf0 buf10 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_hardtanh_hardtanh_backward_0[grid(49152)]( buf4, primals_7, buf5, buf10, 49152, XBLOCK=512, num_warps=4, num_stages=1) del primals_7 buf6 = extern_kernels.convolution(buf5, primals_8, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf6, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf7 = buf4 del buf4 buf9 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.bool) buf8 = empty_strided_cuda((4, 3, 64, 64), (12288, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_2[grid(49152)]( buf6, primals_9, buf7, buf9, buf8, 49152, XBLOCK=512, num_warps =4, num_stages=1) del buf6 del primals_9 return (buf7, primals_1, primals_3, primals_4, primals_6, primals_8, buf1, buf3, buf5, buf8, buf9, buf10, buf11) class ReLUBoundToPOTNetNew(torch.nn.Module): def __init__(self): super(ReLUBoundToPOTNetNew, self).__init__() self.conv1 = Conv2d(3, 3, kernel_size=1, stride=1) self.relu1 = ReLU6() self.conv2 = Conv2d(3, 3, kernel_size=1, stride=1) self.conv3 = Conv2d(3, 3, kernel_size=1, stride=1) self.conv4 = Conv2d(3, 3, kernel_size=1, stride=1) self.relu2 = ReLU() self.identity = torch.nn.Identity() def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_6 = self.conv3.weight primals_7 = self.conv3.bias primals_8 = self.conv4.weight primals_9 = self.conv4.bias primals_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]
elad-c/model_optimization
ReLUBoundToPOTNet
false
10,660
[ "Apache-2.0" ]
0
b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
https://github.com/elad-c/model_optimization/tree/b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
focal_loss
import torch import torch.nn.functional as F class focal_loss(torch.nn.Module): """ Loss function for classification tasks with large data imbalance. Focal loss (FL) is define as: FL(p_t) = -alpha*((1-p_t)^gamma))*log(p_t), where p_t is a cross-entropy loss for binary classification. For more details, see https://arxiv.org/abs/1708.02002. Args: alpha (float): "balance" coefficient, gamma (float): "focusing" parameter (>=0), with_logits (bool): indicates if the sigmoid operation was applied at the end of a neural network's forward path. """ def __init__(self, alpha: 'int'=0.5, gamma: 'int'=2, with_logits: 'bool'=True) ->None: """ Parameter initialization """ super(focal_loss, self).__init__() self.alpha = alpha self.gamma = gamma self.logits = with_logits def forward(self, prediction: 'torch.Tensor', labels: 'torch.Tensor'): """ Calculates loss """ if self.logits: CE_loss = F.binary_cross_entropy_with_logits(prediction, labels) else: CE_loss = F.binary_cross_entropy(prediction, labels) pt = torch.exp(-CE_loss) F_loss = self.alpha * (1 - pt) ** self.gamma * CE_loss return F_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 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_exp_mul_neg_pow_rsub_0( in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp3 = tl.load(in_ptr1 + r0, None) tmp1 = 1.0 tmp2 = tmp1 - tmp0 tmp4 = tmp2 * tmp3 tmp5 = 0.0 tmp6 = triton_helpers.minimum(tmp5, tmp3) tmp7 = tl_math.abs(tmp3) tmp8 = -tmp7 tmp9 = tl_math.exp(tmp8) tmp10 = libdevice.log1p(tmp9) tmp11 = tmp6 - tmp10 tmp12 = tmp4 - tmp11 tmp13 = tl.broadcast_to(tmp12, [RBLOCK]) tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0)) tmp16 = 256.0 tmp17 = tmp15 / tmp16 tmp18 = -tmp17 tmp19 = tl_math.exp(tmp18) tmp20 = tmp1 - tmp19 tmp21 = tmp20 * tmp20 tmp22 = 0.5 tmp23 = tmp21 * tmp22 tmp24 = tmp23 * tmp17 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp24, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_binary_cross_entropy_with_logits_exp_mul_neg_pow_rsub_0[ grid(1)](buf1, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, class focal_lossNew(torch.nn.Module): """ Loss function for classification tasks with large data imbalance. Focal loss (FL) is define as: FL(p_t) = -alpha*((1-p_t)^gamma))*log(p_t), where p_t is a cross-entropy loss for binary classification. For more details, see https://arxiv.org/abs/1708.02002. Args: alpha (float): "balance" coefficient, gamma (float): "focusing" parameter (>=0), with_logits (bool): indicates if the sigmoid operation was applied at the end of a neural network's forward path. """ def __init__(self, alpha: 'int'=0.5, gamma: 'int'=2, with_logits: 'bool'=True) ->None: """ Parameter initialization """ super(focal_lossNew, self).__init__() self.alpha = alpha self.gamma = gamma self.logits = with_logits def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
miguel-fc/atomai
focal_loss
false
10,661
[ "MIT" ]
0
f51699ef5e1bfc577781977d38f7414b1b51449d
https://github.com/miguel-fc/atomai/tree/f51699ef5e1bfc577781977d38f7414b1b51449d
SplitConcatNet
import torch import torch.nn.functional class SplitConcatNet(torch.nn.Module): def __init__(self): super(SplitConcatNet, self).__init__() self.conv1 = torch.nn.Conv2d(3, 3, kernel_size=1, stride=1) self.conv2 = torch.nn.Conv2d(1, 3, kernel_size=1, stride=1) self.conv3 = torch.nn.Conv2d(1, 3, kernel_size=1, stride=1) def forward(self, x, y): x = self.conv1(x) y = self.conv1(y) x1, x2, x3 = torch.split(x, split_size_or_sections=1, dim=1) _y1, y2, y3 = torch.split(y, split_size_or_sections=1, dim=1) x4 = (x3 - x1) * x2 xy1 = torch.concat([x2, y2], 1) xy2 = torch.concat([x1, y3], 1) return self.conv3(x3), self.conv2(x2), x4, xy1 - xy2 def get_inputs(): return [torch.rand([4, 3, 64, 64]), 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 import torch.nn.functional assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 3 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, None) @triton.jit def triton_poi_fused_mul_sub_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 % 4096 x1 = xindex // 4096 x2 = xindex tmp0 = tl.load(in_ptr0 + (8192 + x0 + 12288 * x1), None) tmp1 = tl.load(in_ptr0 + (x0 + 12288 * x1), None) tmp3 = tl.load(in_ptr0 + (4096 + x0 + 12288 * x1), None) tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tl.store(out_ptr0 + x2, tmp2, None) tl.store(out_ptr1 + x2, tmp4, None) @triton.jit def triton_poi_fused_cat_sub_2(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 4096 % 2 x0 = xindex % 4096 x2 = xindex // 8192 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (4096 + x0 + 12288 * x2), tmp4, eviction_policy='evict_last', other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 2, tl.int64) tmp9 = tl.load(in_ptr1 + (4096 + x0 + 4096 * (-1 + x1) + 12288 * x2), tmp6, other=0.0) tmp10 = tl.load(in_ptr2 + (1 + (-1 + x1)), tmp6, eviction_policy= 'evict_last', other=0.0) tmp11 = tmp9 + tmp10 tmp12 = tl.full(tmp11.shape, 0.0, tmp11.dtype) tmp13 = tl.where(tmp6, tmp11, tmp12) tmp14 = tl.where(tmp4, tmp5, tmp13) tmp15 = tl.load(in_ptr0 + (x0 + 4096 * x1 + 12288 * x2), tmp4, other=0.0) tmp16 = tl.load(in_ptr1 + (8192 + x0 + 4096 * (-1 + x1) + 12288 * x2), tmp6, other=0.0) tmp17 = tl.load(in_ptr2 + (2 + (-1 + x1)), tmp6, eviction_policy= 'evict_last', other=0.0) tmp18 = tmp16 + tmp17 tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype) tmp20 = tl.where(tmp6, tmp18, tmp19) tmp21 = tl.where(tmp4, tmp15, tmp20) tmp22 = tmp14 - tmp21 tl.store(out_ptr0 + x3, tmp22, None) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8) = args args.clear() assert_size_stride(primals_1, (3, 3, 1, 1), (3, 1, 1, 1)) assert_size_stride(primals_2, (3,), (1,)) assert_size_stride(primals_3, (4, 3, 64, 64), (12288, 4096, 64, 1)) assert_size_stride(primals_4, (4, 3, 64, 64), (12288, 4096, 64, 1)) assert_size_stride(primals_5, (3, 1, 1, 1), (1, 1, 1, 1)) assert_size_stride(primals_6, (3,), (1,)) assert_size_stride(primals_7, (3, 1, 1, 1), (1, 1, 1, 1)) assert_size_stride(primals_8, (3,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf1 = extern_kernels.convolution(primals_4, primals_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, 3, 64, 64), (12288, 4096, 64, 1)) buf2 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(49152)](buf2, primals_2, 49152, XBLOCK=512, num_warps=4, num_stages=1) buf3 = empty_strided_cuda((4, 1, 64, 64), (4096, 4096, 64, 1), torch.float32) buf4 = empty_strided_cuda((4, 1, 64, 64), (4096, 4096, 64, 1), torch.float32) triton_poi_fused_mul_sub_1[grid(16384)](buf2, buf3, buf4, 16384, XBLOCK=128, num_warps=4, num_stages=1) buf5 = extern_kernels.convolution(reinterpret_tensor(buf2, (4, 1, 64, 64), (12288, 0, 64, 1), 8192), primals_5, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf5, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf6 = buf5 del buf5 triton_poi_fused_convolution_0[grid(49152)](buf6, primals_6, 49152, XBLOCK=512, num_warps=4, num_stages=1) del primals_6 buf7 = extern_kernels.convolution(reinterpret_tensor(buf2, (4, 1, 64, 64), (12288, 0, 64, 1), 4096), primals_7, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf7, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf8 = buf7 del buf7 triton_poi_fused_convolution_0[grid(49152)](buf8, primals_8, 49152, XBLOCK=512, num_warps=4, num_stages=1) del primals_8 buf9 = empty_strided_cuda((4, 2, 64, 64), (8192, 4096, 64, 1), torch.float32) triton_poi_fused_cat_sub_2[grid(32768)](buf2, buf1, primals_2, buf9, 32768, XBLOCK=256, num_warps=4, num_stages=1) del buf1 del primals_2 return (buf6, buf8, buf4, buf9, primals_1, primals_3, primals_4, primals_5, primals_7, reinterpret_tensor(buf2, (4, 1, 64, 64), ( 12288, 4096, 64, 1), 4096), reinterpret_tensor(buf2, (4, 1, 64, 64), (12288, 4096, 64, 1), 8192), buf3) class SplitConcatNetNew(torch.nn.Module): def __init__(self): super(SplitConcatNetNew, self).__init__() self.conv1 = torch.nn.Conv2d(3, 3, kernel_size=1, stride=1) self.conv2 = torch.nn.Conv2d(1, 3, kernel_size=1, stride=1) self.conv3 = torch.nn.Conv2d(1, 3, kernel_size=1, stride=1) def forward(self, input_0, input_1): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_5 = self.conv2.weight primals_6 = self.conv2.bias primals_7 = self.conv3.weight primals_8 = self.conv3.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]) return output[0], output[1], output[2], output[3]
elad-c/model_optimization
SplitConcatNet
false
10,662
[ "Apache-2.0" ]
0
b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
https://github.com/elad-c/model_optimization/tree/b0ecf41c3f9434008d57d7fe724ff8585e19d4cc
SplitAndConcat
import torch import torch.nn as nn import torch.quantization.quantize_fx import torch.utils.data class SplitAndConcat(nn.Module): """Split the data from split_dim and concatenate in concat_dim. @param split_dim from which axis the data will be chunk @param concat_dim to which axis the data will be concatenated @param chunk size of the data to be chunk/concatenated copied: oculus/face/social_eye/lib/model/resnet_backbone.py """ def __init__(self, split_dim: 'int'=1, concat_dim: 'int'=0, chunk: 'int'=2 ): super(SplitAndConcat, self).__init__() self.split_dim = split_dim self.concat_dim = concat_dim self.chunk = chunk def forward(self, x): x = torch.chunk(x, self.chunk, dim=self.split_dim) x = torch.cat(x, dim=self.concat_dim) return x def extra_repr(self): return ( f'split_dim={self.split_dim}, concat_dim={self.concat_dim}, chunk={self.chunk}' ) 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.quantization.quantize_fx 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_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 x1 = xindex // 32 x0 = xindex % 32 x2 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 64 * x1), tmp4 & xmask, other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 8, tl.int64) tmp9 = tl.load(in_ptr0 + (32 + x0 + 64 * (-4 + x1)), tmp6 & xmask, other=0.0) tmp10 = tl.where(tmp4, tmp5, tmp9) tl.store(out_ptr0 + x2, tmp10, xmask) def call(args): arg0_1, = 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((8, 2, 4, 4), (32, 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 SplitAndConcatNew(nn.Module): """Split the data from split_dim and concatenate in concat_dim. @param split_dim from which axis the data will be chunk @param concat_dim to which axis the data will be concatenated @param chunk size of the data to be chunk/concatenated copied: oculus/face/social_eye/lib/model/resnet_backbone.py """ def __init__(self, split_dim: 'int'=1, concat_dim: 'int'=0, chunk: 'int'=2 ): super(SplitAndConcatNew, self).__init__() self.split_dim = split_dim self.concat_dim = concat_dim self.chunk = chunk def extra_repr(self): return ( f'split_dim={self.split_dim}, concat_dim={self.concat_dim}, chunk={self.chunk}' ) def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
petoor/d2go
SplitAndConcat
false
10,663
[ "Apache-2.0" ]
0
d0a20d048738f447945d7c948a8d3019a110d2e8
https://github.com/petoor/d2go/tree/d0a20d048738f447945d7c948a8d3019a110d2e8
UNet
import torch import torch.nn as nn class double_conv(nn.Module): def __init__(self, input_channels, output_channels): super(double_conv, self).__init__() self.conv1 = nn.Conv2d(input_channels, output_channels, kernel_size =3, padding='same') self.conv2 = nn.Conv2d(output_channels, output_channels, kernel_size=3, padding='same') self.relu_activation = nn.ReLU(inplace=True) def forward(self, x): x = self.conv1(self.relu_activation(x)) x = self.conv2(self.relu_activation(x)) return x class traspose_conv(nn.Module): def __init__(self, num_of_channels): super(traspose_conv, self).__init__() self.trasnpose_conv = nn.ConvTranspose2d(num_of_channels, int( num_of_channels / 2), kernel_size=2, stride=2) def forward(self, x): x = self.trasnpose_conv(x) return x class double_decoder_conv(nn.Module): def __init__(self, input_channels1, output_channels1, output_channels2): super(double_decoder_conv, self).__init__() self.conv1 = nn.Conv2d(input_channels1, output_channels1, kernel_size=3, padding='same') self.conv2 = nn.Conv2d(output_channels1, output_channels2, kernel_size=3, padding='same') self.relu_activation = nn.ReLU(inplace=True) def forward(self, x): x = self.conv1(self.relu_activation(x)) x = self.conv2(self.relu_activation(x)) return x class UNet(nn.Module): def __init__(self): super(UNet, self).__init__() self.double_conv1 = double_conv(1, 64) self.double_conv2 = double_conv(64, 128) self.double_conv3 = double_conv(128, 256) self.double_conv4 = double_conv(256, 512) self.double_conv5 = double_conv(512, 1024) self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2) self.traspose_conv1 = traspose_conv(1024) self.traspose_conv2 = traspose_conv(512) self.traspose_conv3 = traspose_conv(256) self.traspose_conv4 = traspose_conv(128) self.double_decoder_conv1 = double_decoder_conv(1024, 512, 512) self.double_decoder_conv2 = double_decoder_conv(512, 256, 256) self.double_decoder_conv3 = double_decoder_conv(256, 128, 128) self.double_decoder_conv4 = double_decoder_conv(128, 64, 64) self.final_conv = nn.Conv2d(64, 1, kernel_size=1, padding='same') def forward(self, x): conv_output1 = self.double_conv1(x) conv_output2 = self.double_conv2(self.maxpool(conv_output1)) conv_output3 = self.double_conv3(self.maxpool(conv_output2)) conv_output4 = self.double_conv4(self.maxpool(conv_output3)) x = self.double_conv5(self.maxpool(conv_output4)) x = self.traspose_conv1(x) x = torch.cat([x, conv_output4], dim=1) x = self.double_decoder_conv1(x) x = self.traspose_conv2(x) x = torch.cat([x, conv_output3], dim=1) x = self.double_decoder_conv2(x) x = self.traspose_conv3(x) x = torch.cat([x, conv_output2], dim=1) x = self.double_decoder_conv3(x) x = self.traspose_conv4(x) x = torch.cat([x, conv_output1], dim=1) x = self.double_decoder_conv4(x) x = self.final_conv(x) 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 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_relu_0(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 tmp0 = tl.load(in_ptr0 + x0, None) tmp1 = tl.full([1], 0, tl.int32) tmp2 = triton_helpers.maximum(tmp1, tmp0) tl.store(out_ptr0 + x0, tmp2, None) tl.store(out_ptr1 + x0, tmp2, None) @triton.jit def triton_poi_fused_convolution_relu_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 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_convolution_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 4096 % 64 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_max_pool2d_with_indices_relu_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 % 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') tmp7 = tl.load(in_ptr0 + (64 + 2 * x0 + 128 * x1), None, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (65 + 2 * x0 + 128 * x1), None, eviction_policy='evict_last') tmp2 = tmp1 > tmp0 tmp3 = tl.full([1], 1, tl.int8) tmp4 = tl.full([1], 0, tl.int8) tmp5 = tl.where(tmp2, tmp3, tmp4) tmp6 = triton_helpers.maximum(tmp1, tmp0) tmp8 = tmp7 > tmp6 tmp9 = tl.full([1], 2, tl.int8) tmp10 = tl.where(tmp8, tmp9, tmp5) tmp11 = triton_helpers.maximum(tmp7, tmp6) tmp13 = tmp12 > tmp11 tmp14 = tl.full([1], 3, tl.int8) tmp15 = tl.where(tmp13, tmp14, tmp10) tmp16 = triton_helpers.maximum(tmp12, tmp11) tmp17 = tl.full([1], 0, tl.int32) tmp18 = triton_helpers.maximum(tmp17, tmp16) tl.store(out_ptr0 + x2, tmp15, None) tl.store(out_ptr1 + x2, tmp18, None) @triton.jit def triton_poi_fused_convolution_relu_4(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) 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_convolution_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 // 1024 % 128 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_max_pool2d_with_indices_relu_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 % 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') tmp7 = tl.load(in_ptr0 + (32 + 2 * x0 + 64 * x1), None, eviction_policy ='evict_last') tmp12 = tl.load(in_ptr0 + (33 + 2 * x0 + 64 * x1), None, eviction_policy='evict_last') tmp2 = tmp1 > tmp0 tmp3 = tl.full([1], 1, tl.int8) tmp4 = tl.full([1], 0, tl.int8) tmp5 = tl.where(tmp2, tmp3, tmp4) tmp6 = triton_helpers.maximum(tmp1, tmp0) tmp8 = tmp7 > tmp6 tmp9 = tl.full([1], 2, tl.int8) tmp10 = tl.where(tmp8, tmp9, tmp5) tmp11 = triton_helpers.maximum(tmp7, tmp6) tmp13 = tmp12 > tmp11 tmp14 = tl.full([1], 3, tl.int8) tmp15 = tl.where(tmp13, tmp14, tmp10) tmp16 = triton_helpers.maximum(tmp12, tmp11) tmp17 = tl.full([1], 0, tl.int32) tmp18 = triton_helpers.maximum(tmp17, tmp16) tl.store(out_ptr0 + x2, tmp15, None) tl.store(out_ptr1 + x2, tmp18, 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 // 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_convolution_8(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) 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 tl.store(in_out_ptr0 + x3, tmp2, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_relu_9(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') tmp7 = tl.load(in_ptr0 + (16 + 2 * x0 + 32 * x1), None, eviction_policy ='evict_last') tmp12 = tl.load(in_ptr0 + (17 + 2 * x0 + 32 * x1), None, eviction_policy='evict_last') tmp2 = tmp1 > tmp0 tmp3 = tl.full([1], 1, tl.int8) tmp4 = tl.full([1], 0, tl.int8) tmp5 = tl.where(tmp2, tmp3, tmp4) tmp6 = triton_helpers.maximum(tmp1, tmp0) tmp8 = tmp7 > tmp6 tmp9 = tl.full([1], 2, tl.int8) tmp10 = tl.where(tmp8, tmp9, tmp5) tmp11 = triton_helpers.maximum(tmp7, tmp6) tmp13 = tmp12 > tmp11 tmp14 = tl.full([1], 3, tl.int8) tmp15 = tl.where(tmp13, tmp14, tmp10) tmp16 = triton_helpers.maximum(tmp12, tmp11) tmp17 = tl.full([1], 0, tl.int32) tmp18 = triton_helpers.maximum(tmp17, tmp16) tl.store(out_ptr0 + x2, tmp15, None) tl.store(out_ptr1 + x2, tmp18, None) @triton.jit def triton_poi_fused_convolution_relu_10(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_convolution_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 // 64 % 512 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_max_pool2d_with_indices_relu_12(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') tmp7 = tl.load(in_ptr0 + (8 + 2 * x0 + 16 * x1), None, eviction_policy= 'evict_last') tmp12 = tl.load(in_ptr0 + (9 + 2 * x0 + 16 * x1), None, eviction_policy ='evict_last') tmp2 = tmp1 > tmp0 tmp3 = tl.full([1], 1, tl.int8) tmp4 = tl.full([1], 0, tl.int8) tmp5 = tl.where(tmp2, tmp3, tmp4) tmp6 = triton_helpers.maximum(tmp1, tmp0) tmp8 = tmp7 > tmp6 tmp9 = tl.full([1], 2, tl.int8) tmp10 = tl.where(tmp8, tmp9, tmp5) tmp11 = triton_helpers.maximum(tmp7, tmp6) tmp13 = tmp12 > tmp11 tmp14 = tl.full([1], 3, tl.int8) tmp15 = tl.where(tmp13, tmp14, tmp10) tmp16 = triton_helpers.maximum(tmp12, tmp11) tmp17 = tl.full([1], 0, tl.int32) tmp18 = triton_helpers.maximum(tmp17, tmp16) tl.store(out_ptr0 + x2, tmp15, None) tl.store(out_ptr1 + x2, tmp18, None) @triton.jit def triton_poi_fused_convolution_relu_13(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 16 % 1024 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_14(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 % 1024 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_cat_relu_15(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 64 % 1024 x0 = xindex % 64 x2 = xindex // 65536 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 512, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 64 * x1 + 32768 * 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(tmp7.shape, 0.0, tmp7.dtype) tmp9 = tl.where(tmp4, tmp7, tmp8) tmp10 = tmp0 >= tmp3 tl.full([1], 1024, tl.int64) tmp13 = tl.load(in_ptr2 + (x0 + 64 * (-512 + x1) + 32768 * x2), tmp10, other=0.0) tmp14 = tl.where(tmp4, tmp9, tmp13) tmp15 = tl.full([1], 0, tl.int32) tmp16 = triton_helpers.maximum(tmp15, tmp14) tl.store(out_ptr0 + x3, tmp16, None) @triton.jit def triton_poi_fused_cat_relu_16(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 256 % 512 x0 = xindex % 256 x2 = xindex // 131072 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 256, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 256 * x1 + 65536 * 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(tmp7.shape, 0.0, tmp7.dtype) tmp9 = tl.where(tmp4, tmp7, tmp8) tmp10 = tmp0 >= tmp3 tl.full([1], 512, tl.int64) tmp13 = tl.load(in_ptr2 + (x0 + 256 * (-256 + x1) + 65536 * x2), tmp10, other=0.0) tmp14 = tl.where(tmp4, tmp9, tmp13) tmp15 = tl.full([1], 0, tl.int32) tmp16 = triton_helpers.maximum(tmp15, tmp14) tl.store(out_ptr0 + x3, tmp16, None) @triton.jit def triton_poi_fused_cat_relu_17(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 1024 % 256 x0 = xindex % 1024 x2 = xindex // 262144 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 128, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 1024 * x1 + 131072 * 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(tmp7.shape, 0.0, tmp7.dtype) tmp9 = tl.where(tmp4, tmp7, tmp8) tmp10 = tmp0 >= tmp3 tl.full([1], 256, tl.int64) tmp13 = tl.load(in_ptr2 + (x0 + 1024 * (-128 + x1) + 131072 * x2), tmp10, other=0.0) tmp14 = tl.where(tmp4, tmp9, tmp13) tmp15 = tl.full([1], 0, tl.int32) tmp16 = triton_helpers.maximum(tmp15, tmp14) tl.store(out_ptr0 + x3, tmp16, None) @triton.jit def triton_poi_fused_cat_relu_18(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 4096 % 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(tmp7.shape, 0.0, tmp7.dtype) tmp9 = tl.where(tmp4, tmp7, tmp8) tmp10 = tmp0 >= tmp3 tl.full([1], 128, tl.int64) tmp13 = tl.load(in_ptr2 + (x0 + 4096 * (-64 + x1) + 262144 * x2), tmp10, other=0.0) tmp14 = tl.where(tmp4, tmp9, tmp13) tmp15 = tl.full([1], 0, tl.int32) tmp16 = triton_helpers.maximum(tmp15, tmp14) tl.store(out_ptr0 + x3, tmp16, None) @triton.jit def triton_poi_fused_convolution_19(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, None) tmp1 = tl.load(in_ptr0 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp3 = tmp0 + tmp2 tl.store(in_out_ptr0 + x0, tmp3, None) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16, primals_17, primals_18, primals_19, primals_20, primals_21, primals_22, primals_23, primals_24, primals_25, primals_26, primals_27, primals_28, primals_29, primals_30, primals_31, primals_32, primals_33, primals_34, primals_35, primals_36, primals_37, primals_38, primals_39, primals_40, primals_41, primals_42, primals_43, primals_44, primals_45, primals_46, primals_47) = args args.clear() assert_size_stride(primals_1, (4, 1, 64, 64), (4096, 4096, 64, 1)) assert_size_stride(primals_2, (64, 1, 3, 3), (9, 9, 3, 1)) assert_size_stride(primals_3, (64,), (1,)) assert_size_stride(primals_4, (64, 64, 3, 3), (576, 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, (128, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_9, (128,), (1,)) assert_size_stride(primals_10, (256, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_11, (256,), (1,)) assert_size_stride(primals_12, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_13, (256,), (1,)) assert_size_stride(primals_14, (512, 256, 3, 3), (2304, 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, (1024, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_19, (1024,), (1,)) assert_size_stride(primals_20, (1024, 1024, 3, 3), (9216, 9, 3, 1)) assert_size_stride(primals_21, (1024,), (1,)) assert_size_stride(primals_22, (1024, 512, 2, 2), (2048, 4, 2, 1)) assert_size_stride(primals_23, (512,), (1,)) assert_size_stride(primals_24, (512, 1024, 3, 3), (9216, 9, 3, 1)) assert_size_stride(primals_25, (512,), (1,)) assert_size_stride(primals_26, (512, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_27, (512,), (1,)) assert_size_stride(primals_28, (512, 256, 2, 2), (1024, 4, 2, 1)) assert_size_stride(primals_29, (256,), (1,)) assert_size_stride(primals_30, (256, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_31, (256,), (1,)) assert_size_stride(primals_32, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_33, (256,), (1,)) assert_size_stride(primals_34, (256, 128, 2, 2), (512, 4, 2, 1)) assert_size_stride(primals_35, (128,), (1,)) assert_size_stride(primals_36, (128, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_37, (128,), (1,)) assert_size_stride(primals_38, (128, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_39, (128,), (1,)) assert_size_stride(primals_40, (128, 64, 2, 2), (256, 4, 2, 1)) assert_size_stride(primals_41, (64,), (1,)) assert_size_stride(primals_42, (64, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_43, (64,), (1,)) assert_size_stride(primals_44, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_45, (64,), (1,)) assert_size_stride(primals_46, (1, 64, 1, 1), (64, 1, 1, 1)) assert_size_stride(primals_47, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 1, 64, 64), (4096, 4096, 64, 1), torch.float32) get_raw_stream(0) triton_poi_fused_relu_0[grid(16384)](primals_1, buf0, primals_1, 16384, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_relu_1[grid(1048576)](buf2, primals_3, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del primals_3 buf3 = 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(buf3, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf4 = buf3 del buf3 triton_poi_fused_convolution_2[grid(1048576)](buf4, primals_5, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del primals_5 buf5 = empty_strided_cuda((4, 64, 32, 32), (65536, 1024, 32, 1), torch.int8) buf6 = empty_strided_cuda((4, 64, 32, 32), (65536, 1024, 32, 1), torch.float32) triton_poi_fused_max_pool2d_with_indices_relu_3[grid(262144)](buf4, buf5, buf6, 262144, XBLOCK=512, num_warps=8, num_stages=1) buf7 = extern_kernels.convolution(buf6, primals_6, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf7, (4, 128, 32, 32), (131072, 1024, 32, 1)) buf8 = buf7 del buf7 triton_poi_fused_convolution_relu_4[grid(524288)](buf8, primals_7, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_7 buf9 = 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(buf9, (4, 128, 32, 32), (131072, 1024, 32, 1)) buf10 = buf9 del buf9 triton_poi_fused_convolution_5[grid(524288)](buf10, primals_9, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_9 buf11 = empty_strided_cuda((4, 128, 16, 16), (32768, 256, 16, 1), torch.int8) buf12 = empty_strided_cuda((4, 128, 16, 16), (32768, 256, 16, 1), torch.float32) triton_poi_fused_max_pool2d_with_indices_relu_6[grid(131072)](buf10, buf11, buf12, 131072, XBLOCK=512, num_warps=8, num_stages=1) buf13 = extern_kernels.convolution(buf12, primals_10, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf13, (4, 256, 16, 16), (65536, 256, 16, 1)) buf14 = buf13 del buf13 triton_poi_fused_convolution_relu_7[grid(262144)](buf14, primals_11, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del primals_11 buf15 = 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(buf15, (4, 256, 16, 16), (65536, 256, 16, 1)) buf16 = buf15 del buf15 triton_poi_fused_convolution_8[grid(262144)](buf16, primals_13, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del primals_13 buf17 = empty_strided_cuda((4, 256, 8, 8), (16384, 64, 8, 1), torch .int8) buf18 = empty_strided_cuda((4, 256, 8, 8), (16384, 64, 8, 1), torch .float32) triton_poi_fused_max_pool2d_with_indices_relu_9[grid(65536)](buf16, buf17, buf18, 65536, XBLOCK=256, num_warps=4, num_stages=1) buf19 = extern_kernels.convolution(buf18, primals_14, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf19, (4, 512, 8, 8), (32768, 64, 8, 1)) buf20 = buf19 del buf19 triton_poi_fused_convolution_relu_10[grid(131072)](buf20, primals_15, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del primals_15 buf21 = 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(buf21, (4, 512, 8, 8), (32768, 64, 8, 1)) buf22 = buf21 del buf21 triton_poi_fused_convolution_11[grid(131072)](buf22, primals_17, 131072, XBLOCK=512, num_warps=8, num_stages=1) del primals_17 buf23 = empty_strided_cuda((4, 512, 4, 4), (8192, 16, 4, 1), torch.int8 ) buf24 = empty_strided_cuda((4, 512, 4, 4), (8192, 16, 4, 1), torch. float32) triton_poi_fused_max_pool2d_with_indices_relu_12[grid(32768)](buf22, buf23, buf24, 32768, XBLOCK=128, num_warps=4, num_stages=1) buf25 = extern_kernels.convolution(buf24, primals_18, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf25, (4, 1024, 4, 4), (16384, 16, 4, 1)) buf26 = buf25 del buf25 triton_poi_fused_convolution_relu_13[grid(65536)](buf26, primals_19, 65536, XBLOCK=256, num_warps=4, num_stages=1) del primals_19 buf27 = 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(buf27, (4, 1024, 4, 4), (16384, 16, 4, 1)) buf28 = buf27 del buf27 triton_poi_fused_convolution_14[grid(65536)](buf28, primals_21, 65536, XBLOCK=256, num_warps=4, num_stages=1) del primals_21 buf29 = extern_kernels.convolution(buf28, primals_22, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf29, (4, 512, 8, 8), (32768, 64, 8, 1)) buf30 = empty_strided_cuda((4, 1024, 8, 8), (65536, 64, 8, 1), torch.float32) triton_poi_fused_cat_relu_15[grid(262144)](buf29, primals_23, buf22, buf30, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del buf29 del primals_23 buf31 = extern_kernels.convolution(buf30, primals_24, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf31, (4, 512, 8, 8), (32768, 64, 8, 1)) buf32 = buf31 del buf31 triton_poi_fused_convolution_relu_10[grid(131072)](buf32, primals_25, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del primals_25 buf33 = extern_kernels.convolution(buf32, primals_26, 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, 8, 8), (32768, 64, 8, 1)) buf34 = buf33 del buf33 triton_poi_fused_convolution_11[grid(131072)](buf34, primals_27, 131072, XBLOCK=512, num_warps=8, num_stages=1) del primals_27 buf35 = extern_kernels.convolution(buf34, primals_28, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf35, (4, 256, 16, 16), (65536, 256, 16, 1)) buf36 = empty_strided_cuda((4, 512, 16, 16), (131072, 256, 16, 1), torch.float32) triton_poi_fused_cat_relu_16[grid(524288)](buf35, primals_29, buf16, buf36, 524288, XBLOCK=512, num_warps=8, num_stages=1) del buf35 del primals_29 buf37 = extern_kernels.convolution(buf36, primals_30, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf37, (4, 256, 16, 16), (65536, 256, 16, 1)) buf38 = buf37 del buf37 triton_poi_fused_convolution_relu_7[grid(262144)](buf38, primals_31, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del primals_31 buf39 = extern_kernels.convolution(buf38, primals_32, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf39, (4, 256, 16, 16), (65536, 256, 16, 1)) buf40 = buf39 del buf39 triton_poi_fused_convolution_8[grid(262144)](buf40, primals_33, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del primals_33 buf41 = extern_kernels.convolution(buf40, primals_34, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf41, (4, 128, 32, 32), (131072, 1024, 32, 1)) buf42 = empty_strided_cuda((4, 256, 32, 32), (262144, 1024, 32, 1), torch.float32) triton_poi_fused_cat_relu_17[grid(1048576)](buf41, primals_35, buf10, buf42, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del buf41 del primals_35 buf43 = extern_kernels.convolution(buf42, primals_36, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf43, (4, 128, 32, 32), (131072, 1024, 32, 1)) buf44 = buf43 del buf43 triton_poi_fused_convolution_relu_4[grid(524288)](buf44, primals_37, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_37 buf45 = extern_kernels.convolution(buf44, primals_38, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf45, (4, 128, 32, 32), (131072, 1024, 32, 1)) buf46 = buf45 del buf45 triton_poi_fused_convolution_5[grid(524288)](buf46, primals_39, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_39 buf47 = extern_kernels.convolution(buf46, primals_40, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf47, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf48 = empty_strided_cuda((4, 128, 64, 64), (524288, 4096, 64, 1), torch.float32) triton_poi_fused_cat_relu_18[grid(2097152)](buf47, primals_41, buf4, buf48, 2097152, XBLOCK=1024, num_warps=4, num_stages=1) del buf47 del primals_41 buf49 = extern_kernels.convolution(buf48, primals_42, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf49, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf50 = buf49 del buf49 triton_poi_fused_convolution_relu_1[grid(1048576)](buf50, primals_43, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del primals_43 buf51 = extern_kernels.convolution(buf50, primals_44, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf51, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf52 = buf51 del buf51 triton_poi_fused_convolution_2[grid(1048576)](buf52, primals_45, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del primals_45 buf53 = extern_kernels.convolution(buf52, primals_46, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf53, (4, 1, 64, 64), (4096, 4096, 64, 1)) buf54 = buf53 del buf53 triton_poi_fused_convolution_19[grid(16384)](buf54, primals_47, 16384, XBLOCK=256, num_warps=4, num_stages=1) del primals_47 return (buf54, primals_2, primals_4, primals_6, primals_8, primals_10, primals_12, primals_14, primals_16, primals_18, primals_20, primals_22, primals_24, primals_26, primals_28, primals_30, primals_32, primals_34, primals_36, primals_38, primals_40, primals_42, primals_44, primals_46, buf0, buf2, buf4, buf5, buf6, buf8, buf10, buf11, buf12, buf14, buf16, buf17, buf18, buf20, buf22, buf23, buf24, buf26, buf28, buf30, buf32, buf34, buf36, buf38, buf40, buf42, buf44, buf46, buf48, buf50, buf52) class double_conv(nn.Module): def __init__(self, input_channels, output_channels): super(double_conv, self).__init__() self.conv1 = nn.Conv2d(input_channels, output_channels, kernel_size =3, padding='same') self.conv2 = nn.Conv2d(output_channels, output_channels, kernel_size=3, padding='same') self.relu_activation = nn.ReLU(inplace=True) def forward(self, x): x = self.conv1(self.relu_activation(x)) x = self.conv2(self.relu_activation(x)) return x class traspose_conv(nn.Module): def __init__(self, num_of_channels): super(traspose_conv, self).__init__() self.trasnpose_conv = nn.ConvTranspose2d(num_of_channels, int( num_of_channels / 2), kernel_size=2, stride=2) def forward(self, x): x = self.trasnpose_conv(x) return x class double_decoder_conv(nn.Module): def __init__(self, input_channels1, output_channels1, output_channels2): super(double_decoder_conv, self).__init__() self.conv1 = nn.Conv2d(input_channels1, output_channels1, kernel_size=3, padding='same') self.conv2 = nn.Conv2d(output_channels1, output_channels2, kernel_size=3, padding='same') self.relu_activation = nn.ReLU(inplace=True) def forward(self, x): x = self.conv1(self.relu_activation(x)) x = self.conv2(self.relu_activation(x)) return x class UNetNew(nn.Module): def __init__(self): super(UNetNew, self).__init__() self.double_conv1 = double_conv(1, 64) self.double_conv2 = double_conv(64, 128) self.double_conv3 = double_conv(128, 256) self.double_conv4 = double_conv(256, 512) self.double_conv5 = double_conv(512, 1024) self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2) self.traspose_conv1 = traspose_conv(1024) self.traspose_conv2 = traspose_conv(512) self.traspose_conv3 = traspose_conv(256) self.traspose_conv4 = traspose_conv(128) self.double_decoder_conv1 = double_decoder_conv(1024, 512, 512) self.double_decoder_conv2 = double_decoder_conv(512, 256, 256) self.double_decoder_conv3 = double_decoder_conv(256, 128, 128) self.double_decoder_conv4 = double_decoder_conv(128, 64, 64) self.final_conv = nn.Conv2d(64, 1, kernel_size=1, padding='same') def forward(self, input_0): primals_2 = self.double_conv1.conv1.weight primals_3 = self.double_conv1.conv1.bias primals_4 = self.double_conv1.conv2.weight primals_5 = self.double_conv1.conv2.bias primals_6 = self.double_conv2.conv1.weight primals_7 = self.double_conv2.conv1.bias primals_8 = self.double_conv2.conv2.weight primals_9 = self.double_conv2.conv2.bias primals_10 = self.double_conv3.conv1.weight primals_11 = self.double_conv3.conv1.bias primals_12 = self.double_conv3.conv2.weight primals_13 = self.double_conv3.conv2.bias primals_14 = self.double_conv4.conv1.weight primals_15 = self.double_conv4.conv1.bias primals_16 = self.double_conv4.conv2.weight primals_17 = self.double_conv4.conv2.bias primals_18 = self.double_conv5.conv1.weight primals_19 = self.double_conv5.conv1.bias primals_20 = self.double_conv5.conv2.weight primals_21 = self.double_conv5.conv2.bias primals_22 = self.traspose_conv1.trasnpose_conv.weight primals_23 = self.traspose_conv1.trasnpose_conv.bias primals_28 = self.traspose_conv2.trasnpose_conv.weight primals_29 = self.traspose_conv2.trasnpose_conv.bias primals_34 = self.traspose_conv3.trasnpose_conv.weight primals_35 = self.traspose_conv3.trasnpose_conv.bias primals_40 = self.traspose_conv4.trasnpose_conv.weight primals_41 = self.traspose_conv4.trasnpose_conv.bias primals_24 = self.double_decoder_conv1.conv1.weight primals_25 = self.double_decoder_conv1.conv1.bias primals_26 = self.double_decoder_conv1.conv2.weight primals_27 = self.double_decoder_conv1.conv2.bias primals_30 = self.double_decoder_conv2.conv1.weight primals_31 = self.double_decoder_conv2.conv1.bias primals_32 = self.double_decoder_conv2.conv2.weight primals_33 = self.double_decoder_conv2.conv2.bias primals_36 = self.double_decoder_conv3.conv1.weight primals_37 = self.double_decoder_conv3.conv1.bias primals_38 = self.double_decoder_conv3.conv2.weight primals_39 = self.double_decoder_conv3.conv2.bias primals_42 = self.double_decoder_conv4.conv1.weight primals_43 = self.double_decoder_conv4.conv1.bias primals_44 = self.double_decoder_conv4.conv2.weight primals_45 = self.double_decoder_conv4.conv2.bias primals_46 = self.final_conv.weight primals_47 = self.final_conv.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16, primals_17, primals_18, primals_19, primals_20, primals_21, primals_22, primals_23, primals_24, primals_25, primals_26, primals_27, primals_28, primals_29, primals_30, primals_31, primals_32, primals_33, primals_34, primals_35, primals_36, primals_37, primals_38, primals_39, primals_40, primals_41, primals_42, primals_43, primals_44, primals_45, primals_46, primals_47]) return output[0]
mhakyash/UNet-MNIST-denoising
UNet
false
10,664
[ "MIT" ]
0
0e3c20cbb3f34af575e33209425ae4d7cb0bcd82
https://github.com/mhakyash/UNet-MNIST-denoising/tree/0e3c20cbb3f34af575e33209425ae4d7cb0bcd82
UpsampleBlock
import torch import torch.nn.functional as F import torch.nn as nn class UpsampleBlock(nn.Module): """ Defines upsampling block performed using bilinear or nearest-neigbor interpolation followed by 1-by-1 convolution (the latter can be used to reduce a number of feature channels) Args: ndim: Data dimensionality (1D or 2D) input_channels: Number of input channels for the block output_channels: Number of the output channels for the block scale_factor: Scale factor for upsampling mode: Upsampling mode. Select between "bilinear" and "nearest" """ def __init__(self, ndim: 'int', input_channels: 'int', output_channels: 'int', scale_factor: 'int'=2, mode: 'str'='bilinear') ->None: """ Initializes module parameters """ super(UpsampleBlock, self).__init__() if not any([mode == 'bilinear', mode == 'nearest']): raise NotImplementedError( "use 'bilinear' or 'nearest' for upsampling mode") if not 0 < ndim < 3: raise AssertionError('ndim must be equal to 1 or 2') conv = nn.Conv2d if ndim == 2 else nn.Conv1d self.scale_factor = scale_factor self.mode = mode if ndim == 2 else 'nearest' self.conv = conv(input_channels, output_channels, kernel_size=1, stride=1, padding=0) def forward(self, x: 'torch.Tensor') ->torch.Tensor: """ Defines a forward pass """ x = F.interpolate(x, scale_factor=self.scale_factor, mode=self.mode) return self.conv(x) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'ndim': 1, 'input_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_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 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp3.to(tl.int32) tmp5 = tl.load(in_ptr0 + (tmp4 + 4 * x1), xmask, eviction_policy= 'evict_last') tl.store(out_ptr0 + x2, tmp5, xmask) @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 // 8 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 8), (32, 8, 1), torch.float32) get_raw_stream(0) triton_poi_fused__unsafe_index_0[grid(128)](primals_1, buf0, 128, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 8), (32, 8, 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 return buf2, primals_2, buf0 class UpsampleBlockNew(nn.Module): """ Defines upsampling block performed using bilinear or nearest-neigbor interpolation followed by 1-by-1 convolution (the latter can be used to reduce a number of feature channels) Args: ndim: Data dimensionality (1D or 2D) input_channels: Number of input channels for the block output_channels: Number of the output channels for the block scale_factor: Scale factor for upsampling mode: Upsampling mode. Select between "bilinear" and "nearest" """ def __init__(self, ndim: 'int', input_channels: 'int', output_channels: 'int', scale_factor: 'int'=2, mode: 'str'='bilinear') ->None: """ Initializes module parameters """ super(UpsampleBlockNew, self).__init__() if not any([mode == 'bilinear', mode == 'nearest']): raise NotImplementedError( "use 'bilinear' or 'nearest' for upsampling mode") if not 0 < ndim < 3: raise AssertionError('ndim must be equal to 1 or 2') conv = nn.Conv2d if ndim == 2 else nn.Conv1d self.scale_factor = scale_factor self.mode = mode if ndim == 2 else 'nearest' self.conv = conv(input_channels, output_channels, kernel_size=1, stride=1, padding=0) def forward(self, input_0): primals_2 = self.conv.weight primals_3 = self.conv.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
miguel-fc/atomai
UpsampleBlock
false
10,665
[ "MIT" ]
0
f51699ef5e1bfc577781977d38f7414b1b51449d
https://github.com/miguel-fc/atomai/tree/f51699ef5e1bfc577781977d38f7414b1b51449d
KeypointRCNNPredictorNoUpscale
import torch import torch.nn as nn import torch.quantization.quantize_fx import torch.utils.data class KeypointRCNNPredictorNoUpscale(nn.Module): def __init__(self, in_channels, num_keypoints): super(KeypointRCNNPredictorNoUpscale, self).__init__() input_features = in_channels deconv_kernel = 4 self.kps_score_lowres = nn.ConvTranspose2d(input_features, num_keypoints, deconv_kernel, stride=2, padding=deconv_kernel // 2 - 1) nn.init.kaiming_normal_(self.kps_score_lowres.weight, mode= 'fan_out', nonlinearity='relu') nn.init.constant_(self.kps_score_lowres.bias, 0) self.out_channels = num_keypoints def forward(self, x): x = self.kps_score_lowres(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'num_keypoints': 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.quantization.quantize_fx import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride @triton.jit def triton_poi_fused_convolution_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 64 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(2, 2), padding=(1, 1), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 8, 8), (256, 64, 8, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(1024)](buf1, primals_2, 1024, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 return buf1, primals_1, primals_3 class KeypointRCNNPredictorNoUpscaleNew(nn.Module): def __init__(self, in_channels, num_keypoints): super(KeypointRCNNPredictorNoUpscaleNew, self).__init__() input_features = in_channels deconv_kernel = 4 self.kps_score_lowres = nn.ConvTranspose2d(input_features, num_keypoints, deconv_kernel, stride=2, padding=deconv_kernel // 2 - 1) nn.init.kaiming_normal_(self.kps_score_lowres.weight, mode= 'fan_out', nonlinearity='relu') nn.init.constant_(self.kps_score_lowres.bias, 0) self.out_channels = num_keypoints def forward(self, input_0): primals_1 = self.kps_score_lowres.weight primals_2 = self.kps_score_lowres.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
petoor/d2go
KeypointRCNNPredictorNoUpscale
false
10,666
[ "Apache-2.0" ]
0
d0a20d048738f447945d7c948a8d3019a110d2e8
https://github.com/petoor/d2go/tree/d0a20d048738f447945d7c948a8d3019a110d2e8
DiceLoss
import torch import torch.nn as nn class DiceLoss(nn.Module): def __init__(self): super(DiceLoss, self).__init__() def forward(self, input, target): N = target.size(0) smooth = 1 input_flat = input.view(N, -1) target_flat = target.view(N, -1) intersection = input_flat * target_flat loss = 2 * (intersection.sum(1) + smooth) / (input_flat.sum(1) + target_flat.sum(1) + smooth) loss = 1 - loss.sum() / N return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_mul_sum_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 RBLOCK: tl.constexpr = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 64 * x0), xmask, other=0.0) tmp1 = tl.load(in_ptr1 + (r1 + 64 * x0), xmask, other=0.0) tmp2 = tmp0 * tmp1 tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp5 = tl.where(xmask, tmp3, 0) tmp6 = tl.sum(tmp5, 1)[:, None] tmp7 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp9 = tl.where(xmask, tmp7, 0) tmp10 = tl.sum(tmp9, 1)[:, None] tmp11 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp13 = tl.where(xmask, tmp11, 0) tmp14 = tl.sum(tmp13, 1)[:, None] tl.store(out_ptr0 + x0, tmp6, xmask) tl.store(out_ptr1 + x0, tmp10, xmask) tl.store(out_ptr2 + x0, tmp14, xmask) @triton.jit def triton_per_fused_add_div_mul_rsub_sum_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 4 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp5 = tl.load(in_ptr1 + r0, None) tmp6 = tl.load(in_ptr2 + r0, None) tmp1 = 1.0 tmp2 = tmp0 + tmp1 tmp3 = 2.0 tmp4 = tmp2 * tmp3 tmp7 = tmp5 + tmp6 tmp8 = tmp7 + tmp1 tmp9 = tmp4 / tmp8 tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK]) tmp12 = tl.sum(tmp10, 1)[:, None] tmp13 = 0.25 tmp14 = tmp12 * tmp13 tmp15 = tmp1 - tmp14 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp15, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4,), (1,), torch.float32) buf1 = empty_strided_cuda((4,), (1,), torch.float32) buf2 = empty_strided_cuda((4,), (1,), torch.float32) get_raw_stream(0) triton_per_fused_mul_sum_0[grid(4)](arg1_1, arg0_1, buf0, buf1, buf2, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 buf3 = empty_strided_cuda((), (), torch.float32) buf4 = buf3 del buf3 triton_per_fused_add_div_mul_rsub_sum_1[grid(1)](buf4, buf0, buf1, buf2, 1, 4, XBLOCK=1, num_warps=2, num_stages=1) del buf0 del buf1 del buf2 return buf4, class DiceLossNew(nn.Module): def __init__(self): super(DiceLossNew, self).__init__() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
phenixcxz/DeepGlobe-Road-Extraction-Challenge
DiceLoss
false
10,667
[ "MIT" ]
0
4dee0f0866ff6f06b888afd28a60940b75a8eadd
https://github.com/phenixcxz/DeepGlobe-Road-Extraction-Challenge/tree/4dee0f0866ff6f06b888afd28a60940b75a8eadd
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, 6) self.l2 = nn.Linear(6, 4) self.l3 = nn.Linear(4, 1) self.l4 = nn.Linear(state_dim + action_dim, 6) self.l5 = nn.Linear(6, 4) self.l6 = nn.Linear(4, 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) q2 = F.relu(self.l4(sa)) q2 = F.relu(self.l5(q2)) q2 = self.l6(q2) return q1, q2 def Q1(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 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) @triton.jit def triton_poi_fused_relu_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 24 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 6 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 = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, 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, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (6, 8), (8, 1)) assert_size_stride(primals_4, (6,), (1,)) assert_size_stride(primals_5, (4, 6), (6, 1)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (1, 4), (4, 1)) assert_size_stride(primals_8, (1,), (1,)) assert_size_stride(primals_9, (6, 8), (8, 1)) assert_size_stride(primals_10, (6,), (1,)) assert_size_stride(primals_11, (4, 6), (6, 1)) assert_size_stride(primals_12, (4,), (1,)) assert_size_stride(primals_13, (1, 4), (4, 1)) assert_size_stride(primals_14, (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, 6), (6, 1), torch.float32) extern_kernels.mm(buf0, reinterpret_tensor(primals_3, (8, 6), (1, 8 ), 0), out=buf1) del primals_3 buf2 = buf1 del buf1 triton_poi_fused_relu_1[grid(24)](buf2, primals_4, 24, XBLOCK=32, num_warps=1, num_stages=1) del primals_4 buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf2, reinterpret_tensor(primals_5, (6, 4), (1, 6 ), 0), out=buf3) buf4 = buf3 del buf3 triton_poi_fused_relu_2[grid(16)](buf4, primals_6, 16, XBLOCK=16, num_warps=1, 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, (4, 1), (1, 4), 0), alpha=1, beta=1, out=buf6) del primals_8 buf7 = empty_strided_cuda((4, 6), (6, 1), torch.float32) extern_kernels.mm(buf0, reinterpret_tensor(primals_9, (8, 6), (1, 8 ), 0), out=buf7) del primals_9 buf8 = buf7 del buf7 triton_poi_fused_relu_1[grid(24)](buf8, primals_10, 24, XBLOCK=32, num_warps=1, num_stages=1) del primals_10 buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf8, reinterpret_tensor(primals_11, (6, 4), (1, 6), 0), out=buf9) buf10 = buf9 del buf9 triton_poi_fused_relu_2[grid(16)](buf10, primals_12, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_12 buf12 = empty_strided_cuda((4, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_14, buf10, reinterpret_tensor( primals_13, (4, 1), (1, 4), 0), alpha=1, beta=1, out=buf12) del primals_14 return (buf6, buf12, buf0, buf2, buf4, buf8, buf10, primals_13, primals_11, 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, 6) self.l2 = nn.Linear(6, 4) self.l3 = nn.Linear(4, 1) self.l4 = nn.Linear(state_dim + action_dim, 6) self.l5 = nn.Linear(6, 4) self.l6 = nn.Linear(4, 1) def Q1(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 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_9 = self.l4.weight primals_10 = self.l4.bias primals_11 = self.l5.weight primals_12 = self.l5.bias primals_13 = self.l6.weight primals_14 = self.l6.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], output[1]
pkj415/CityLearn-2
Critic
false
10,668
[ "MIT" ]
0
003012ddeb52868d42d85b835a9a5f2c28008927
https://github.com/pkj415/CityLearn-2/tree/003012ddeb52868d42d85b835a9a5f2c28008927
MulticlassDiceLoss
import torch import torch.nn as nn class DiceLoss(nn.Module): def __init__(self): super(DiceLoss, self).__init__() def forward(self, input, target): N = target.size(0) smooth = 1 input_flat = input.view(N, -1) target_flat = target.view(N, -1) intersection = input_flat * target_flat loss = 2 * (intersection.sum(1) + smooth) / (input_flat.sum(1) + target_flat.sum(1) + smooth) loss = 1 - loss.sum() / N return loss class MulticlassDiceLoss(nn.Module): """ requires one hot encoded target. Applies DiceLoss on each class iteratively. requires input.shape[0:1] and target.shape[0:1] to be (N, C) where N is batch size and C is number of classes """ def __init__(self): super(MulticlassDiceLoss, self).__init__() def forward(self, input, target, weights=None): C = target.shape[1] dice = DiceLoss() totalLoss = 0 for i in range(C): diceLoss = dice(input[:, i], target[:, i]) if weights is not None: diceLoss *= weights[i] totalLoss += diceLoss return totalLoss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_mul_sum_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 RBLOCK: tl.constexpr = 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 + 64 * x0), xmask, other=0.0) tmp1 = tl.load(in_ptr1 + (r1 + 64 * x0), xmask, other=0.0) tmp2 = tmp0 * tmp1 tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp5 = tl.where(xmask, tmp3, 0) tmp6 = tl.sum(tmp5, 1)[:, None] tmp7 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp9 = tl.where(xmask, tmp7, 0) tmp10 = tl.sum(tmp9, 1)[:, None] tmp11 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp13 = tl.where(xmask, tmp11, 0) tmp14 = tl.sum(tmp13, 1)[:, None] tl.store(out_ptr0 + x0, tmp6, xmask) tl.store(out_ptr1 + x0, tmp10, xmask) tl.store(out_ptr2 + x0, tmp14, xmask) @triton.jit def triton_per_fused_mul_sum_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (32 + r1 + 64 * x0), xmask, other=0.0) tmp1 = tl.load(in_ptr1 + (32 + r1 + 64 * x0), xmask, other=0.0) tmp2 = tmp0 * tmp1 tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp5 = tl.where(xmask, tmp3, 0) tmp6 = tl.sum(tmp5, 1)[:, None] tmp7 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp9 = tl.where(xmask, tmp7, 0) tmp10 = tl.sum(tmp9, 1)[:, None] tmp11 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp13 = tl.where(xmask, tmp11, 0) tmp14 = tl.sum(tmp13, 1)[:, None] tl.store(out_ptr0 + x0, tmp6, xmask) tl.store(out_ptr1 + x0, tmp10, xmask) tl.store(out_ptr2 + x0, tmp14, xmask) @triton.jit def triton_per_fused_mul_sum_2(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (48 + r1 + 64 * x0), xmask, other=0.0) tmp1 = tl.load(in_ptr1 + (48 + r1 + 64 * x0), xmask, other=0.0) tmp2 = tmp0 * tmp1 tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp5 = tl.where(xmask, tmp3, 0) tmp6 = tl.sum(tmp5, 1)[:, None] tmp7 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp9 = tl.where(xmask, tmp7, 0) tmp10 = tl.sum(tmp9, 1)[:, None] tmp11 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp13 = tl.where(xmask, tmp11, 0) tmp14 = tl.sum(tmp13, 1)[:, None] tl.store(out_ptr0 + x0, tmp6, xmask) tl.store(out_ptr1 + x0, tmp10, xmask) tl.store(out_ptr2 + x0, tmp14, xmask) @triton.jit def triton_per_fused_mul_sum_3(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (16 + r1 + 64 * x0), xmask, other=0.0) tmp1 = tl.load(in_ptr1 + (16 + r1 + 64 * x0), xmask, other=0.0) tmp2 = tmp0 * tmp1 tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp5 = tl.where(xmask, tmp3, 0) tmp6 = tl.sum(tmp5, 1)[:, None] tmp7 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp9 = tl.where(xmask, tmp7, 0) tmp10 = tl.sum(tmp9, 1)[:, None] tmp11 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp13 = tl.where(xmask, tmp11, 0) tmp14 = tl.sum(tmp13, 1)[:, None] tl.store(out_ptr0 + x0, tmp6, xmask) tl.store(out_ptr1 + x0, tmp10, xmask) tl.store(out_ptr2 + x0, tmp14, xmask) @triton.jit def triton_per_fused_add_div_mul_rsub_sum_4(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, in_ptr9, in_ptr10, in_ptr11, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 4 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp5 = tl.load(in_ptr1 + r0, None) tmp6 = tl.load(in_ptr2 + r0, None) tmp13 = tl.load(in_ptr3 + r0, None) tmp16 = tl.load(in_ptr4 + r0, None) tmp17 = tl.load(in_ptr5 + r0, None) tmp24 = tl.load(in_ptr6 + r0, None) tmp27 = tl.load(in_ptr7 + r0, None) tmp28 = tl.load(in_ptr8 + r0, None) tmp35 = tl.load(in_ptr9 + r0, None) tmp38 = tl.load(in_ptr10 + r0, None) tmp39 = tl.load(in_ptr11 + r0, None) tmp1 = 1.0 tmp2 = tmp0 + tmp1 tmp3 = 2.0 tmp4 = tmp2 * tmp3 tmp7 = tmp5 + tmp6 tmp8 = tmp7 + tmp1 tmp9 = tmp4 / tmp8 tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK]) tmp12 = tl.sum(tmp10, 1)[:, None] tmp14 = tmp13 + tmp1 tmp15 = tmp14 * tmp3 tmp18 = tmp16 + tmp17 tmp19 = tmp18 + tmp1 tmp20 = tmp15 / tmp19 tmp21 = tl.broadcast_to(tmp20, [XBLOCK, RBLOCK]) tmp23 = tl.sum(tmp21, 1)[:, None] tmp25 = tmp24 + tmp1 tmp26 = tmp25 * tmp3 tmp29 = tmp27 + tmp28 tmp30 = tmp29 + tmp1 tmp31 = tmp26 / tmp30 tmp32 = tl.broadcast_to(tmp31, [XBLOCK, RBLOCK]) tmp34 = tl.sum(tmp32, 1)[:, None] tmp36 = tmp35 + tmp1 tmp37 = tmp36 * tmp3 tmp40 = tmp38 + tmp39 tmp41 = tmp40 + tmp1 tmp42 = tmp37 / tmp41 tmp43 = tl.broadcast_to(tmp42, [XBLOCK, RBLOCK]) tmp45 = tl.sum(tmp43, 1)[:, None] tmp46 = 0.25 tmp47 = tmp12 * tmp46 tmp48 = tmp1 - tmp47 tmp49 = 0.0 tmp50 = tmp48 + tmp49 tmp51 = tmp23 * tmp46 tmp52 = tmp1 - tmp51 tmp53 = tmp50 + tmp52 tmp54 = tmp34 * tmp46 tmp55 = tmp1 - tmp54 tmp56 = tmp53 + tmp55 tmp57 = tmp45 * tmp46 tmp58 = tmp1 - tmp57 tmp59 = tmp56 + tmp58 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp59, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4,), (1,), torch.float32) buf1 = empty_strided_cuda((4,), (1,), torch.float32) buf2 = empty_strided_cuda((4,), (1,), torch.float32) get_raw_stream(0) triton_per_fused_mul_sum_0[grid(4)](arg1_1, arg0_1, buf0, buf1, buf2, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) buf8 = empty_strided_cuda((4,), (1,), torch.float32) buf9 = empty_strided_cuda((4,), (1,), torch.float32) buf10 = empty_strided_cuda((4,), (1,), torch.float32) triton_per_fused_mul_sum_1[grid(4)](arg1_1, arg0_1, buf8, buf9, buf10, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) buf12 = empty_strided_cuda((4,), (1,), torch.float32) buf13 = empty_strided_cuda((4,), (1,), torch.float32) buf14 = empty_strided_cuda((4,), (1,), torch.float32) triton_per_fused_mul_sum_2[grid(4)](arg1_1, arg0_1, buf12, buf13, buf14, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) buf4 = empty_strided_cuda((4,), (1,), torch.float32) buf5 = empty_strided_cuda((4,), (1,), torch.float32) buf6 = empty_strided_cuda((4,), (1,), torch.float32) triton_per_fused_mul_sum_3[grid(4)](arg1_1, arg0_1, buf4, buf5, buf6, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 buf11 = empty_strided_cuda((), (), torch.float32) buf16 = buf11 del buf11 triton_per_fused_add_div_mul_rsub_sum_4[grid(1)](buf16, buf0, buf1, buf2, buf4, buf5, buf6, buf8, buf9, buf10, buf12, buf13, buf14, 1, 4, XBLOCK=1, num_warps=2, num_stages=1) del buf0 del buf1 del buf10 del buf12 del buf13 del buf14 del buf2 del buf4 del buf5 del buf6 del buf8 del buf9 return buf16, class DiceLoss(nn.Module): def __init__(self): super(DiceLoss, self).__init__() def forward(self, input, target): N = target.size(0) smooth = 1 input_flat = input.view(N, -1) target_flat = target.view(N, -1) intersection = input_flat * target_flat loss = 2 * (intersection.sum(1) + smooth) / (input_flat.sum(1) + target_flat.sum(1) + smooth) loss = 1 - loss.sum() / N return loss class MulticlassDiceLossNew(nn.Module): """ requires one hot encoded target. Applies DiceLoss on each class iteratively. requires input.shape[0:1] and target.shape[0:1] to be (N, C) where N is batch size and C is number of classes """ def __init__(self): super(MulticlassDiceLossNew, 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]
phenixcxz/DeepGlobe-Road-Extraction-Challenge
MulticlassDiceLoss
false
10,669
[ "MIT" ]
0
4dee0f0866ff6f06b888afd28a60940b75a8eadd
https://github.com/phenixcxz/DeepGlobe-Road-Extraction-Challenge/tree/4dee0f0866ff6f06b888afd28a60940b75a8eadd
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): scores = torch.matmul(query, torch.transpose(key, 2, 3)) / math.sqrt( key.shape[-1]) scores = scores.masked_fill(attention_mask < 0, -10000) normed = torch.softmax(scores, -1) per_head = torch.matmul(normed, value) atten = torch.cat([per_head[:, i, :, :] for i in range(per_head. shape[1])], -1) return atten 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_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_lt_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 = -10000.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 = -10000.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_cat_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 % 4 x2 = xindex // 16 x3 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x1 + 16 * x2), 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 + (4 + x1 + 16 * x2), tmp9 & xmask, eviction_policy='evict_last', other=0.0) tmp11 = tmp0 >= tmp7 tmp12 = tl.full([1], 3, tl.int64) tmp13 = tmp0 < tmp12 tmp14 = tmp11 & tmp13 tmp15 = tl.load(in_ptr0 + (8 + x1 + 16 * x2), tmp14 & xmask, eviction_policy='evict_last', other=0.0) tmp16 = tmp0 >= tmp12 tl.full([1], 4, tl.int64) tmp19 = tl.load(in_ptr0 + (12 + x1 + 16 * x2), tmp16 & xmask, eviction_policy='evict_last', other=0.0) tmp20 = tl.where(tmp14, tmp15, tmp19) tmp21 = tl.where(tmp9, tmp10, tmp20) tmp22 = tl.where(tmp4, tmp5, tmp21) tl.store(out_ptr0 + x3, tmp22, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8) = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 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_clone_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_clone_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 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool) triton_poi_fused_lt_1[grid(64)](primals_8, buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_8 buf7 = reinterpret_tensor(buf0, (4, 4, 4, 1), (16, 4, 1, 64), 0) del buf0 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)](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(buf7, (4, 4, 4), (16, 4, 1), 0) del buf7 triton_poi_fused_cat_4[grid(64)](buf11, buf12, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf11 return buf12, reinterpret_tensor(primals_1, (16, 4), (4, 1), 0 ), buf6, 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): scores = torch.matmul(query, torch.transpose(key, 2, 3)) / math.sqrt( key.shape[-1]) scores = scores.masked_fill(attention_mask < 0, -10000) normed = torch.softmax(scores, -1) per_head = torch.matmul(normed, value) atten = torch.cat([per_head[:, i, :, :] for i in range(per_head. shape[1])], -1) return atten 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]
priyamtejaswin/minbert-assignment
BertSelfAttention
false
10,670
[ "Apache-2.0" ]
0
fd41a54441916a6d421640bbee910f64786b303d
https://github.com/priyamtejaswin/minbert-assignment/tree/fd41a54441916a6d421640bbee910f64786b303d
VariableBoxMLP
import torch import torch.optim import torch.jit import torch.nn as nn class VariableBoxMLP(nn.Module): def __init__(self, num_in_features: 'int', num_out_features: 'int', neurons_per_layer: 'int', hidden_layers: 'int'): super(VariableBoxMLP, self).__init__() self.hidden_layers = hidden_layers self.act = nn.ELU() self.l_in = nn.Linear(in_features=num_in_features, out_features= neurons_per_layer) for i in range(0, hidden_layers): layer = nn.Linear(in_features=neurons_per_layer, out_features= neurons_per_layer) torch.nn.init.xavier_normal_(layer.weight) torch.nn.init.zeros_(layer.bias) setattr(self, 'l' + str(i), layer) self.l_out = nn.Linear(in_features=neurons_per_layer, out_features= num_out_features) torch.nn.init.xavier_normal_(self.l_in.weight) torch.nn.init.zeros_(self.l_in.bias) torch.nn.init.xavier_normal_(self.l_out.weight) torch.nn.init.zeros_(self.l_out.bias) def forward(self, x): x = self.act(self.l_in(x)) for i in range(self.hidden_layers): x = self.act(getattr(self, 'l' + str(i)).__call__(x)) x = self.l_out(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_in_features': 4, 'num_out_features': 4, 'neurons_per_layer': 1, 'hidden_layers': 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 import torch.optim import torch.jit 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 = 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 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) 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, (1, 4), (4, 1)) assert_size_stride(primals_2, (1,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (1, 1), (1, 1)) assert_size_stride(primals_5, (1,), (1,)) assert_size_stride(primals_6, (4, 1), (1, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 1), (1, 4), 0 ), alpha=1, beta=1, out=buf1) del primals_1 del primals_2 buf2 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_elu_0[grid(64)](buf1, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) buf4 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf2, (64, 1), ( 1, 0), 0), primals_4, alpha=1, beta=1, out=buf4) del primals_5 buf5 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) triton_poi_fused_elu_0[grid(64)](buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf6 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf5, (64, 1), ( 1, 0), 0), reinterpret_tensor(primals_6, (1, 4), (1, 1), 0), alpha=1, beta=1, out=buf6) del primals_7 return reinterpret_tensor(buf6, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf1, reinterpret_tensor(buf2, (64, 1), (1, 1), 0 ), buf4, reinterpret_tensor(buf5, (64, 1), (1, 1), 0 ), primals_6, primals_4 class VariableBoxMLPNew(nn.Module): def __init__(self, num_in_features: 'int', num_out_features: 'int', neurons_per_layer: 'int', hidden_layers: 'int'): super(VariableBoxMLPNew, self).__init__() self.hidden_layers = hidden_layers self.act = nn.ELU() self.l_in = nn.Linear(in_features=num_in_features, out_features= neurons_per_layer) for i in range(0, hidden_layers): layer = nn.Linear(in_features=neurons_per_layer, out_features= neurons_per_layer) torch.nn.init.xavier_normal_(layer.weight) torch.nn.init.zeros_(layer.bias) setattr(self, 'l' + str(i), layer) self.l_out = nn.Linear(in_features=neurons_per_layer, out_features= num_out_features) torch.nn.init.xavier_normal_(self.l_in.weight) torch.nn.init.zeros_(self.l_in.bias) torch.nn.init.xavier_normal_(self.l_out.weight) torch.nn.init.zeros_(self.l_out.bias) def forward(self, input_0): primals_1 = self.l_in.weight primals_2 = self.l_in.bias primals_4 = self.l0.weight primals_5 = self.l0.bias primals_6 = self.l_out.weight primals_7 = self.l_out.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
plaveczlambert/nonlinearbubbledynamics
VariableBoxMLP
false
10,671
[ "MIT" ]
0
190c5170f7ff6068badeee818c01226c55aaec97
https://github.com/plaveczlambert/nonlinearbubbledynamics/tree/190c5170f7ff6068badeee818c01226c55aaec97
TilePad2d
import torch import torch.nn as nn import torch.nn.functional as F class TilePad2d(nn.Module): def __init__(self, left, right, top, bottom): super().__init__() self.left = left self.right = right self.top = top self.bottom = bottom def forward(self, x): return F.pad(x, [self.left, self.right, self.top, self.bottom], mode='circular') def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'left': 4, 'right': 4, 'top': 4, 'bottom': 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_copy_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 2304 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 12 x1 = xindex // 12 % 12 x2 = xindex // 144 x4 = xindex tmp0 = x0 tmp1 = tl.full([1], 8, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = -4 + x0 tmp4 = tl.full([1], 4, tl.int64) tmp5 = tmp3 < tmp4 tmp6 = tmp5 & tmp2 tmp7 = tmp0 >= tmp4 tmp8 = tmp0 < tmp1 tmp9 = tmp7 & tmp8 tmp10 = tmp9 & tmp6 tmp11 = x1 tmp12 = tmp11 >= tmp4 tmp13 = tmp11 < tmp1 tmp14 = tmp12 & tmp13 tmp15 = tmp14 & tmp10 tmp16 = tl.load(in_ptr0 + (-20 + x0 + 4 * x1 + 16 * x2), tmp15 & xmask, other=0.0) tmp17 = tl.load(in_ptr1 + x4, tmp10 & xmask, other=0.0) tmp18 = tl.where(tmp14, tmp16, tmp17) tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype) tmp20 = tl.where(tmp10, tmp18, tmp19) tmp21 = float('nan') tmp22 = tl.where(tmp9, tmp20, tmp21) tmp23 = tl.full(tmp22.shape, 0.0, tmp22.dtype) tmp24 = tl.where(tmp6, tmp22, tmp23) tmp25 = tmp3 >= tmp4 tmp26 = tmp3 < tmp1 tmp27 = tmp25 & tmp26 tmp28 = tmp27 & tmp2 tmp29 = tmp14 & tmp28 tmp30 = tl.load(in_ptr0 + (-24 + x0 + 4 * x1 + 16 * x2), tmp29 & xmask, other=0.0) tmp31 = tl.load(in_ptr1 + (-4 + x4), tmp28 & xmask, other=0.0) tmp32 = tl.where(tmp14, tmp30, tmp31) tmp33 = tl.full(tmp32.shape, 0.0, tmp32.dtype) tmp34 = tl.where(tmp28, tmp32, tmp33) tmp35 = tl.where(tmp27, tmp34, tmp21) tmp36 = tl.where(tmp5, tmp24, tmp35) tmp37 = tl.full(tmp36.shape, 0.0, tmp36.dtype) tmp38 = tl.where(tmp2, tmp36, tmp37) tmp39 = tmp0 < tmp4 tmp40 = 4 + x0 tmp41 = tmp40 >= tmp4 tmp42 = tmp40 < tmp1 tmp43 = tmp41 & tmp42 tmp44 = tmp43 & tmp39 tmp45 = tmp14 & tmp44 tmp46 = tl.load(in_ptr0 + (-16 + x0 + 4 * x1 + 16 * x2), tmp45 & xmask, other=0.0) tmp47 = tl.load(in_ptr1 + (4 + x4), tmp44 & xmask, other=0.0) tmp48 = tl.where(tmp14, tmp46, tmp47) tmp49 = tl.full(tmp48.shape, 0.0, tmp48.dtype) tmp50 = tl.where(tmp44, tmp48, tmp49) tmp51 = tl.where(tmp43, tmp50, tmp21) tmp52 = tl.full(tmp51.shape, 0.0, tmp51.dtype) tmp53 = tl.where(tmp39, tmp51, tmp52) tmp54 = tmp14 & tmp9 tmp55 = tl.load(in_ptr0 + (-20 + x0 + 4 * x1 + 16 * x2), tmp54 & xmask, other=0.0) tmp56 = tl.load(in_ptr1 + x4, tmp9 & xmask, other=0.0) tmp57 = tl.where(tmp14, tmp55, tmp56) tmp58 = tl.full(tmp57.shape, 0.0, tmp57.dtype) tmp59 = tl.where(tmp9, tmp57, tmp58) tmp60 = tl.where(tmp9, tmp59, tmp21) tmp61 = tl.where(tmp39, tmp53, tmp60) tmp62 = tl.where(tmp2, tmp38, tmp61) tl.store(out_ptr0 + x4, tmp62, xmask) @triton.jit def triton_poi_fused_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 2304 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 12 % 12 x3 = xindex tmp14 = tl.load(in_ptr0 + x3, xmask) tmp0 = x1 tmp1 = tl.full([1], 8, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = -4 + x1 tmp4 = tl.full([1], 4, tl.int64) tmp5 = tmp3 < tmp4 tmp6 = tmp5 & tmp2 tmp7 = tl.load(in_ptr0 + x3, tmp6 & xmask, other=0.0) tmp8 = tl.load(in_ptr0 + (-48 + x3), tmp2 & xmask, other=0.0) tmp9 = tl.where(tmp5, tmp7, tmp8) tmp10 = tl.full(tmp9.shape, 0.0, tmp9.dtype) tmp11 = tl.where(tmp2, tmp9, tmp10) tmp12 = tmp0 < tmp4 tmp13 = tl.load(in_ptr0 + (48 + x3), tmp12 & xmask, other=0.0) tmp15 = tl.where(tmp12, tmp13, tmp14) tmp16 = tl.where(tmp2, tmp11, tmp15) tl.store(out_ptr0 + x3, tmp16, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) buf0 = empty_strided_cuda((4, 4, 12, 12), (576, 144, 12, 1), torch.float32) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((4, 4, 12, 12), (576, 144, 12, 1), torch. float32) get_raw_stream(0) triton_poi_fused_copy_0[grid(2304)](arg0_1, buf0, buf1, 2304, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 buf2 = buf0 del buf0 triton_poi_fused_1[grid(2304)](buf1, buf2, 2304, XBLOCK=256, num_warps=4, num_stages=1) del buf1 return buf2, class TilePad2dNew(nn.Module): def __init__(self, left, right, top, bottom): super().__init__() self.left = left self.right = right self.top = top self.bottom = bottom def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
mkarmann/conway-reversed
TilePad2d
false
10,672
[ "MIT" ]
0
a3ae10dd5768affb9caf193a246395ee0fb2bc6f
https://github.com/mkarmann/conway-reversed/tree/a3ae10dd5768affb9caf193a246395ee0fb2bc6f
SimpleMLP
import torch import torch.optim import torch.jit import torch.nn as nn class SimpleMLP(nn.Module): def __init__(self, num_in_features: 'int', num_out_features: 'int', neurons_per_layer: 'int'): super(SimpleMLP, self).__init__() self.act = nn.ELU() self.l_in = nn.Linear(in_features=num_in_features, out_features= neurons_per_layer) self.l1 = nn.Linear(in_features=neurons_per_layer, out_features= neurons_per_layer) self.l2 = nn.Linear(in_features=neurons_per_layer, out_features= neurons_per_layer) self.l3 = nn.Linear(in_features=neurons_per_layer, out_features= neurons_per_layer) self.l4 = nn.Linear(in_features=neurons_per_layer, out_features= neurons_per_layer) self.l5 = nn.Linear(in_features=neurons_per_layer, out_features= neurons_per_layer) self.l6 = nn.Linear(in_features=neurons_per_layer, out_features= neurons_per_layer) self.l_out = nn.Linear(in_features=neurons_per_layer, out_features= num_out_features) torch.nn.init.xavier_normal_(self.l_in.weight) torch.nn.init.zeros_(self.l_in.bias) torch.nn.init.xavier_normal_(self.l1.weight) torch.nn.init.zeros_(self.l1.bias) torch.nn.init.xavier_normal_(self.l2.weight) torch.nn.init.zeros_(self.l2.bias) torch.nn.init.xavier_normal_(self.l3.weight) torch.nn.init.zeros_(self.l3.bias) torch.nn.init.xavier_normal_(self.l4.weight) torch.nn.init.zeros_(self.l4.bias) torch.nn.init.xavier_normal_(self.l5.weight) torch.nn.init.zeros_(self.l5.bias) torch.nn.init.xavier_normal_(self.l6.weight) torch.nn.init.zeros_(self.l6.bias) torch.nn.init.xavier_normal_(self.l_out.weight) torch.nn.init.zeros_(self.l_out.bias) def forward(self, x): x = self.act(self.l_in(x)) x = self.act(self.l1(x)) x = self.act(self.l2(x)) x = self.act(self.l3(x)) x = self.act(self.l4(x)) x = self.act(self.l5(x)) x = self.act(self.l6(x)) x = self.l_out(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_in_features': 4, 'num_out_features': 4, 'neurons_per_layer': 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 import torch.optim import torch.jit 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 = 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 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) 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) = args args.clear() assert_size_stride(primals_1, (1, 4), (4, 1)) assert_size_stride(primals_2, (1,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (1, 1), (1, 1)) assert_size_stride(primals_5, (1,), (1,)) assert_size_stride(primals_6, (1, 1), (1, 1)) assert_size_stride(primals_7, (1,), (1,)) assert_size_stride(primals_8, (1, 1), (1, 1)) assert_size_stride(primals_9, (1,), (1,)) assert_size_stride(primals_10, (1, 1), (1, 1)) assert_size_stride(primals_11, (1,), (1,)) assert_size_stride(primals_12, (1, 1), (1, 1)) assert_size_stride(primals_13, (1,), (1,)) assert_size_stride(primals_14, (1, 1), (1, 1)) assert_size_stride(primals_15, (1,), (1,)) assert_size_stride(primals_16, (4, 1), (1, 1)) assert_size_stride(primals_17, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 1), (1, 4), 0 ), alpha=1, beta=1, out=buf1) del primals_1 del primals_2 buf2 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_elu_0[grid(64)](buf1, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) buf4 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf2, (64, 1), ( 1, 0), 0), primals_4, alpha=1, beta=1, out=buf4) del primals_5 buf5 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) triton_poi_fused_elu_0[grid(64)](buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf7 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf5, (64, 1), ( 1, 0), 0), primals_6, alpha=1, beta=1, out=buf7) del primals_7 buf8 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) triton_poi_fused_elu_0[grid(64)](buf7, buf8, 64, XBLOCK=64, num_warps=1, num_stages=1) buf10 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_9, reinterpret_tensor(buf8, (64, 1), ( 1, 0), 0), primals_8, alpha=1, beta=1, out=buf10) del primals_9 buf11 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) triton_poi_fused_elu_0[grid(64)](buf10, buf11, 64, XBLOCK=64, num_warps=1, num_stages=1) buf13 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_11, reinterpret_tensor(buf11, (64, 1), (1, 0), 0), primals_10, alpha=1, beta=1, out=buf13) del primals_11 buf14 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) triton_poi_fused_elu_0[grid(64)](buf13, buf14, 64, XBLOCK=64, num_warps=1, num_stages=1) buf16 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_13, reinterpret_tensor(buf14, (64, 1), (1, 0), 0), primals_12, alpha=1, beta=1, out=buf16) del primals_13 buf17 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) triton_poi_fused_elu_0[grid(64)](buf16, buf17, 64, XBLOCK=64, num_warps=1, num_stages=1) buf19 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_15, reinterpret_tensor(buf17, (64, 1), (1, 0), 0), primals_14, alpha=1, beta=1, out=buf19) del primals_15 buf20 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) triton_poi_fused_elu_0[grid(64)](buf19, buf20, 64, XBLOCK=64, num_warps=1, num_stages=1) buf21 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_17, reinterpret_tensor(buf20, (64, 1), (1, 0), 0), reinterpret_tensor(primals_16, (1, 4), (1, 1), 0), alpha=1, beta=1, out=buf21) del primals_17 return (reinterpret_tensor(buf21, (4, 4, 4, 4), (64, 16, 4, 1), 0), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), buf1, reinterpret_tensor(buf2, (64, 1), (1, 1), 0), buf4, reinterpret_tensor(buf5, (64, 1), (1, 1), 0), buf7, reinterpret_tensor(buf8, (64, 1), (1, 1), 0), buf10, reinterpret_tensor(buf11, (64, 1), (1, 1), 0), buf13, reinterpret_tensor(buf14, (64, 1), (1, 1), 0), buf16, reinterpret_tensor(buf17, (64, 1), (1, 1), 0), buf19, reinterpret_tensor(buf20, (64, 1), (1, 1), 0), primals_16, primals_14, primals_12, primals_10, primals_8, primals_6, primals_4) class SimpleMLPNew(nn.Module): def __init__(self, num_in_features: 'int', num_out_features: 'int', neurons_per_layer: 'int'): super(SimpleMLPNew, self).__init__() self.act = nn.ELU() self.l_in = nn.Linear(in_features=num_in_features, out_features= neurons_per_layer) self.l1 = nn.Linear(in_features=neurons_per_layer, out_features= neurons_per_layer) self.l2 = nn.Linear(in_features=neurons_per_layer, out_features= neurons_per_layer) self.l3 = nn.Linear(in_features=neurons_per_layer, out_features= neurons_per_layer) self.l4 = nn.Linear(in_features=neurons_per_layer, out_features= neurons_per_layer) self.l5 = nn.Linear(in_features=neurons_per_layer, out_features= neurons_per_layer) self.l6 = nn.Linear(in_features=neurons_per_layer, out_features= neurons_per_layer) self.l_out = nn.Linear(in_features=neurons_per_layer, out_features= num_out_features) torch.nn.init.xavier_normal_(self.l_in.weight) torch.nn.init.zeros_(self.l_in.bias) torch.nn.init.xavier_normal_(self.l1.weight) torch.nn.init.zeros_(self.l1.bias) torch.nn.init.xavier_normal_(self.l2.weight) torch.nn.init.zeros_(self.l2.bias) torch.nn.init.xavier_normal_(self.l3.weight) torch.nn.init.zeros_(self.l3.bias) torch.nn.init.xavier_normal_(self.l4.weight) torch.nn.init.zeros_(self.l4.bias) torch.nn.init.xavier_normal_(self.l5.weight) torch.nn.init.zeros_(self.l5.bias) torch.nn.init.xavier_normal_(self.l6.weight) torch.nn.init.zeros_(self.l6.bias) torch.nn.init.xavier_normal_(self.l_out.weight) torch.nn.init.zeros_(self.l_out.bias) def forward(self, input_0): primals_1 = self.l_in.weight primals_2 = self.l_in.bias primals_4 = self.l1.weight primals_5 = self.l1.bias primals_6 = self.l2.weight primals_7 = self.l2.bias primals_8 = self.l3.weight primals_9 = self.l3.bias primals_10 = self.l4.weight primals_11 = self.l4.bias primals_12 = self.l5.weight primals_13 = self.l5.bias primals_14 = self.l6.weight primals_15 = self.l6.bias primals_16 = self.l_out.weight primals_17 = self.l_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]) return output[0]
plaveczlambert/nonlinearbubbledynamics
SimpleMLP
false
10,673
[ "MIT" ]
0
190c5170f7ff6068badeee818c01226c55aaec97
https://github.com/plaveczlambert/nonlinearbubbledynamics/tree/190c5170f7ff6068badeee818c01226c55aaec97
EncoderLayer
import math import torch import torch.nn.functional as F import torch.nn as nn class FeedForward(nn.Module): def __init__(self, d_model, d_ff=2048, dropout=0.1): super().__init__() self.linear_1 = nn.Linear(d_model, d_ff) self.dropout = nn.Dropout(dropout) self.linear_2 = nn.Linear(d_ff, d_model) def forward(self, x): x = self.dropout(F.relu(self.linear_1(x))) x = self.linear_2(x) return x class MultiHeadAttention(nn.Module): def __init__(self, heads, d_model, dropout=0.1): super().__init__() self.d_model = d_model self.d_k = d_model // heads self.h = heads self.q_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.v_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.k_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.dropout = nn.Dropout(dropout) self.out = nn.Linear(d_model, d_model) def forward(self, q, k, v, mask=None, dropout=None): bs = q.size(0) if q.size(1) > 230: self.h = 8 self.d_k = self.d_model // self.h elif q.size(1) <= 138 and q.size(1) > 230: self.h = 4 self.d_k = self.d_model // self.h elif q.size(1) <= 138 and q.size(1) > 0: self.h = 2 self.d_k = self.d_model // self.h k = torch.matmul(k, self.k_linear1) k = k.view(bs, -1, self.h, self.d_k) q = torch.matmul(q, self.q_linear1) q = q.view(bs, -1, self.h, self.d_k) v = torch.matmul(v, self.v_linear1) v = v.view(bs, -1, self.h, self.d_k) q = q.transpose(1, 2) k = k.transpose(1, 2) v = v.transpose(1, 2) k = k.transpose(-2, -1) scores = torch.matmul(q, k) scores = scores / math.sqrt(self.d_k) if mask is not None: mask = mask.unsqueeze(1) scores = scores.masked_fill(mask == 0, -1000000000.0) scores = F.softmax(scores, dim=-1) if dropout is not None: scores = dropout(scores) scores = torch.matmul(scores, v) concat = scores.transpose(1, 2).contiguous().view(bs, -1, self.d_model) output = self.out(concat) return output class Norm(nn.Module): def __init__(self, d_model, eps=1e-06): super().__init__() self.size = d_model self.alpha = nn.Parameter(torch.ones(self.size)) self.bias = nn.Parameter(torch.zeros(self.size)) self.eps = eps def forward(self, x): norm = self.alpha * (x - x.mean(dim=-1, keepdim=True)) / (x.std(dim =-1, keepdim=True) + self.eps) + self.bias return norm class EncoderLayer(nn.Module): def __init__(self, d_model, heads, dropout=0.1): super().__init__() self.norm_1 = Norm(d_model) self.norm_2 = Norm(d_model) self.attn = MultiHeadAttention(heads, d_model, dropout=dropout) self.ff = FeedForward(d_model, dropout=dropout) self.dropout_1 = nn.Dropout(dropout) self.dropout_2 = nn.Dropout(dropout) def forward(self, x, mask): x2 = self.norm_1(x) x = x + self.dropout_1(self.attn(x2, x2, x2, mask)) x2 = self.norm_2(x) x = x + self.dropout_2(self.ff(x2)) return x 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.nn.functional as F import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_div_mean_mul_std_sub_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask) tmp2 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp30 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp8 = tmp6 + tmp7 tmp9 = 4.0 tmp10 = tmp8 / tmp9 tmp11 = tmp1 - tmp10 tmp12 = tmp0 * tmp11 tmp13 = tmp2 - tmp10 tmp14 = tmp13 * tmp13 tmp15 = tmp3 - tmp10 tmp16 = tmp15 * tmp15 tmp17 = tmp14 + tmp16 tmp18 = tmp5 - tmp10 tmp19 = tmp18 * tmp18 tmp20 = tmp17 + tmp19 tmp21 = tmp7 - tmp10 tmp22 = tmp21 * tmp21 tmp23 = tmp20 + tmp22 tmp24 = 3.0 tmp25 = tmp23 / tmp24 tmp26 = libdevice.sqrt(tmp25) tmp27 = 1e-06 tmp28 = tmp26 + tmp27 tmp29 = tmp12 / tmp28 tmp31 = tmp29 + tmp30 tl.store(out_ptr0 + x2, tmp31, xmask) @triton.jit def triton_poi_fused_clone_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 % 2 x1 = xindex // 2 % 4 x2 = xindex // 8 % 2 x3 = xindex // 16 x4 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 2 * x2 + 4 * x1 + 16 * x3), xmask) tl.store(out_ptr0 + x4, tmp0, xmask) @triton.jit def triton_poi_fused_clone_2(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_eq_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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_4(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x2 = xindex // 8 x3 = xindex tmp0 = tl.load(in_ptr0 + (4 * x0 + 16 * x2), xmask, eviction_policy= 'evict_last').to(tl.int1) tmp1 = tl.load(in_ptr1 + 4 * x3, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (1 + 4 * x0 + 16 * x2), xmask, eviction_policy ='evict_last').to(tl.int1) tmp7 = tl.load(in_ptr1 + (1 + 4 * x3), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (2 + 4 * x0 + 16 * x2), xmask, eviction_policy='evict_last').to(tl.int1) tmp12 = tl.load(in_ptr1 + (2 + 4 * x3), xmask, eviction_policy='evict_last' ) tmp16 = tl.load(in_ptr0 + (3 + 4 * x0 + 16 * x2), xmask, eviction_policy='evict_last').to(tl.int1) tmp17 = tl.load(in_ptr1 + (3 + 4 * x3), xmask, eviction_policy='evict_last' ) tmp2 = 0.7071067811865475 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 + x3, tmp20, xmask) tl.store(out_ptr1 + x3, tmp31, xmask) @triton.jit def triton_poi_fused__softmax_div_masked_fill_5(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex // 32 x4 = xindex % 16 x5 = xindex x6 = xindex // 4 tmp0 = tl.load(in_ptr0 + (x4 + 16 * x3), xmask, eviction_policy= 'evict_last').to(tl.int1) tmp1 = tl.load(in_out_ptr0 + x5, xmask) tmp6 = tl.load(in_ptr1 + x6, xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr2 + x6, xmask, eviction_policy='evict_last') tmp2 = 0.7071067811865475 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 + x5, tmp10, xmask) @triton.jit def triton_poi_fused_clone_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 % 2 x1 = xindex // 2 % 2 x2 = xindex // 4 % 4 x3 = xindex // 16 x4 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 2 * x2 + 8 * x1 + 16 * x3), xmask) tl.store(out_ptr0 + x4, tmp0, xmask) @triton.jit def triton_poi_fused_add_mean_std_7(in_out_ptr0, in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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 = 3.0 tmp29 = tmp27 / tmp28 tl.store(out_ptr0 + x0, tmp16, xmask) tl.store(in_out_ptr0 + x0, tmp29, xmask) @triton.jit def triton_poi_fused_add_div_mean_mul_std_sub_8(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask) tmp2 = tl.load(in_ptr2 + x2, xmask) tmp4 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 - tmp4 tmp6 = tmp0 * tmp5 tmp8 = libdevice.sqrt(tmp7) tmp9 = 1e-06 tmp10 = tmp8 + tmp9 tmp11 = tmp6 / tmp10 tmp13 = tmp11 + tmp12 tl.store(out_ptr0 + x2, tmp13, xmask) @triton.jit def triton_poi_fused_relu_threshold_backward_9(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): 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_10(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x2, xmask) tmp3 = tl.load(in_out_ptr0 + 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, 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,), (1,)) assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4, 4), (4, 1)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_8, (4, 4), (4, 1)) assert_size_stride(primals_9, (4,), (1,)) assert_size_stride(primals_10, (4,), (1,)) assert_size_stride(primals_11, (4,), (1,)) assert_size_stride(primals_12, (2048, 4), (4, 1)) assert_size_stride(primals_13, (2048,), (1,)) assert_size_stride(primals_14, (4, 2048), (2048, 1)) assert_size_stride(primals_15, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_mean_mul_std_sub_0[grid(64)](primals_1, primals_2, primals_3, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_1 del primals_3 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0), primals_4, out=buf1) buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0), primals_5, out=buf2) buf3 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0), primals_6, out=buf3) buf4 = empty_strided_cuda((4, 2, 4, 2), (16, 8, 2, 1), torch.float32) triton_poi_fused_clone_1[grid(64)](buf2, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) buf5 = reinterpret_tensor(buf2, (4, 2, 2, 4), (16, 8, 4, 1), 0) del buf2 triton_poi_fused_clone_2[grid(16, 4)](buf1, buf5, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf6 = empty_strided_cuda((8, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf4, (8, 4, 2), (8, 2, 1), 0 ), reinterpret_tensor(buf5, (8, 2, 4), (8, 4, 1), 0), out=buf6) buf7 = empty_strided_cuda((4, 1, 4, 4), (16, 16, 4, 1), torch.bool) triton_poi_fused_eq_3[grid(64)](primals_7, buf7, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_7 buf8 = empty_strided_cuda((4, 2, 4, 1), (8, 4, 1, 32), torch.float32) buf9 = empty_strided_cuda((4, 2, 4, 1), (8, 4, 1, 32), torch.float32) triton_poi_fused__softmax_div_masked_fill_4[grid(32)](buf7, buf6, buf8, buf9, 32, XBLOCK=32, num_warps=1, num_stages=1) buf10 = reinterpret_tensor(buf6, (4, 2, 4, 4), (32, 16, 4, 1), 0) del buf6 triton_poi_fused__softmax_div_masked_fill_5[grid(128)](buf10, buf7, buf8, buf9, 128, XBLOCK=128, num_warps=4, num_stages=1) del buf8 del buf9 buf11 = reinterpret_tensor(buf1, (4, 2, 4, 2), (16, 8, 2, 1), 0) del buf1 triton_poi_fused_clone_1[grid(64)](buf3, buf11, 64, XBLOCK=64, num_warps=1, num_stages=1) buf12 = reinterpret_tensor(buf3, (8, 4, 2), (8, 2, 1), 0) del buf3 extern_kernels.bmm(reinterpret_tensor(buf10, (8, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf11, (8, 4, 2), (8, 2, 1), 0), out=buf12) buf13 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32) triton_poi_fused_clone_6[grid(64)](buf12, buf13, 64, XBLOCK=64, num_warps=1, num_stages=1) buf14 = reinterpret_tensor(buf12, (16, 4), (4, 1), 0) del buf12 extern_kernels.addmm(primals_9, reinterpret_tensor(buf13, (16, 4), (4, 1), 0), reinterpret_tensor(primals_8, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf14) del primals_9 buf15 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf16 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf17 = buf16 del buf16 triton_poi_fused_add_mean_std_7[grid(16)](buf17, primals_2, buf14, buf15, 16, XBLOCK=16, num_warps=1, num_stages=1) buf18 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_add_div_mean_mul_std_sub_8[grid(64)](primals_10, primals_2, buf14, buf15, buf17, primals_11, buf18, 64, XBLOCK= 64, num_warps=1, num_stages=1) del buf15 del buf17 del primals_11 buf19 = empty_strided_cuda((16, 2048), (2048, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf18, (16, 4), (4, 1), 0), reinterpret_tensor(primals_12, (4, 2048), (1, 4), 0), out=buf19) buf20 = reinterpret_tensor(buf19, (4, 4, 2048), (8192, 2048, 1), 0) del buf19 buf23 = empty_strided_cuda((4, 4, 2048), (8192, 2048, 1), torch.bool) triton_poi_fused_relu_threshold_backward_9[grid(32768)](buf20, primals_13, buf23, 32768, XBLOCK=256, num_warps=4, num_stages=1) del primals_13 buf21 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf20, (16, 2048), (2048, 1), 0), reinterpret_tensor(primals_14, (2048, 4), (1, 2048), 0), out=buf21) buf22 = reinterpret_tensor(buf21, (4, 4, 4), (16, 4, 1), 0) del buf21 triton_poi_fused_add_10[grid(64)](buf22, primals_2, buf14, primals_15, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_15 return buf22, primals_2, primals_10, buf7, buf10, reinterpret_tensor(buf13, (16, 4), (4, 1), 0), buf14, reinterpret_tensor(buf18, (16, 4), (4, 1), 0), reinterpret_tensor(buf20, (16, 2048), (2048, 1), 0 ), primals_14, buf23, primals_12, primals_8, reinterpret_tensor(buf11, (8, 2, 4), (8, 1, 2), 0), reinterpret_tensor(buf4, (8, 2, 4), (8, 1, 2), 0), reinterpret_tensor(buf5, (8, 4, 2), (8, 1, 4), 0 ), reinterpret_tensor(buf0, (4, 16), (1, 4), 0), reinterpret_tensor( primals_6, (4, 4), (1, 4), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0) class FeedForward(nn.Module): def __init__(self, d_model, d_ff=2048, dropout=0.1): super().__init__() self.linear_1 = nn.Linear(d_model, d_ff) self.dropout = nn.Dropout(dropout) self.linear_2 = nn.Linear(d_ff, d_model) def forward(self, x): x = self.dropout(F.relu(self.linear_1(x))) x = self.linear_2(x) return x class MultiHeadAttention(nn.Module): def __init__(self, heads, d_model, dropout=0.1): super().__init__() self.d_model = d_model self.d_k = d_model // heads self.h = heads self.q_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.v_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.k_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.dropout = nn.Dropout(dropout) self.out = nn.Linear(d_model, d_model) def forward(self, q, k, v, mask=None, dropout=None): bs = q.size(0) if q.size(1) > 230: self.h = 8 self.d_k = self.d_model // self.h elif q.size(1) <= 138 and q.size(1) > 230: self.h = 4 self.d_k = self.d_model // self.h elif q.size(1) <= 138 and q.size(1) > 0: self.h = 2 self.d_k = self.d_model // self.h k = torch.matmul(k, self.k_linear1) k = k.view(bs, -1, self.h, self.d_k) q = torch.matmul(q, self.q_linear1) q = q.view(bs, -1, self.h, self.d_k) v = torch.matmul(v, self.v_linear1) v = v.view(bs, -1, self.h, self.d_k) q = q.transpose(1, 2) k = k.transpose(1, 2) v = v.transpose(1, 2) k = k.transpose(-2, -1) scores = torch.matmul(q, k) scores = scores / math.sqrt(self.d_k) if mask is not None: mask = mask.unsqueeze(1) scores = scores.masked_fill(mask == 0, -1000000000.0) scores = F.softmax(scores, dim=-1) if dropout is not None: scores = dropout(scores) scores = torch.matmul(scores, v) concat = scores.transpose(1, 2).contiguous().view(bs, -1, self.d_model) output = self.out(concat) return output class Norm(nn.Module): def __init__(self, d_model, eps=1e-06): super().__init__() self.size = d_model self.alpha = nn.Parameter(torch.ones(self.size)) self.bias = nn.Parameter(torch.zeros(self.size)) self.eps = eps def forward(self, x): norm = self.alpha * (x - x.mean(dim=-1, keepdim=True)) / (x.std(dim =-1, keepdim=True) + self.eps) + self.bias return norm class EncoderLayerNew(nn.Module): def __init__(self, d_model, heads, dropout=0.1): super().__init__() self.norm_1 = Norm(d_model) self.norm_2 = Norm(d_model) self.attn = MultiHeadAttention(heads, d_model, dropout=dropout) self.ff = FeedForward(d_model, dropout=dropout) self.dropout_1 = nn.Dropout(dropout) self.dropout_2 = nn.Dropout(dropout) def forward(self, input_0, input_1): primals_1 = self.norm_1.alpha primals_3 = self.norm_1.bias primals_9 = self.norm_2.alpha primals_10 = self.norm_2.bias primals_4 = self.attn.q_linear1 primals_5 = self.attn.v_linear1 primals_6 = self.attn.k_linear1 primals_8 = self.attn.out.weight primals_11 = self.attn.out.bias primals_12 = self.ff.linear_1.weight primals_13 = self.ff.linear_1.bias primals_14 = self.ff.linear_2.weight primals_15 = self.ff.linear_2.bias primals_2 = input_0 primals_7 = 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]) return output[0]
nlakshmanan/Transformer
EncoderLayer
false
10,674
[ "Apache-2.0" ]
0
4562f8e9b282d0a70f26903a7b4410cb6132364b
https://github.com/nlakshmanan/Transformer/tree/4562f8e9b282d0a70f26903a7b4410cb6132364b
HighwayCNN
import torch import torch.nn as nn class HighwayCNN(nn.Module): def __init__(self, input_size, gate_bias=-1, activation_function=nn. functional.relu, gate_activation=nn.functional.softmax): super(HighwayCNN, self).__init__() self.activation_function = activation_function self.gate_activation = gate_activation self.normal_layer = nn.Linear(input_size, input_size) self.gate_layer = nn.Linear(input_size, input_size) self.gate_layer.bias.data.fill_(gate_bias) def forward(self, x): normal_layer_result = self.activation_function(self.normal_layer(x)) gate_layer_result = self.gate_activation(self.gate_layer(x)) multiplyed_gate_and_normal = torch.mul(normal_layer_result, gate_layer_result) multiplyed_gate_and_input = torch.mul(1 - gate_layer_result, x) return torch.add(multiplyed_gate_and_normal, multiplyed_gate_and_input) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x3, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_add_mul_relu_rsub_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr1 + x3, xmask) tmp15 = tl.load(in_ptr2 + x3, xmask) tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tmp10 = tl.full([1], 0, tl.int32) tmp11 = triton_helpers.maximum(tmp10, tmp9) tmp12 = tmp11 * tmp8 tmp13 = 1.0 tmp14 = tmp13 - tmp8 tmp16 = tmp14 * tmp15 tmp17 = tmp12 + tmp16 tl.store(in_out_ptr0 + x3, tmp17, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf1) del primals_4 del primals_5 buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused__softmax_0[grid(256)](buf1, buf2, 256, XBLOCK=256, num_warps=4, num_stages=1) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf4 = buf3 del buf3 triton_poi_fused__softmax_add_mul_relu_rsub_1[grid(256)](buf4, buf2, buf0, primals_3, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf2 return buf4, primals_3, buf0, buf1 class HighwayCNNNew(nn.Module): def __init__(self, input_size, gate_bias=-1, activation_function=nn. functional.relu, gate_activation=nn.functional.softmax): super(HighwayCNNNew, self).__init__() self.activation_function = activation_function self.gate_activation = gate_activation self.normal_layer = nn.Linear(input_size, input_size) self.gate_layer = nn.Linear(input_size, input_size) self.gate_layer.bias.data.fill_(gate_bias) def forward(self, input_0): primals_1 = self.normal_layer.weight primals_2 = self.normal_layer.bias primals_4 = self.gate_layer.weight primals_5 = self.gate_layer.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
okcd00/glyce
HighwayCNN
false
10,675
[ "Apache-2.0" ]
0
010d88ac5cff4969308d2f8d105831ddcb352a02
https://github.com/okcd00/glyce/tree/010d88ac5cff4969308d2f8d105831ddcb352a02
HighwayMLP
import torch import torch.nn as nn class HighwayMLP(nn.Module): def __init__(self, input_size, gate_bias=-2, activation_function=nn. functional.relu, gate_activation=nn.functional.softmax): super(HighwayMLP, self).__init__() self.activation_function = activation_function self.gate_activation = gate_activation self.normal_layer = nn.Linear(input_size, input_size) self.gate_layer = nn.Linear(input_size, input_size) self.gate_layer.bias.data.fill_(gate_bias) def forward(self, x): normal_layer_result = self.activation_function(self.normal_layer(x)) gate_layer_result = self.gate_activation(self.gate_layer(x), dim=0) multiplyed_gate_and_normal = torch.mul(normal_layer_result, gate_layer_result) multiplyed_gate_and_input = torch.mul(1 - gate_layer_result, x) return torch.add(multiplyed_gate_and_normal, multiplyed_gate_and_input) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (64 + x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (128 + x0), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (192 + x0), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_add_mul_relu_rsub_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (64 + x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (128 + x0), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (192 + x0), xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr1 + x2, xmask) tmp15 = tl.load(in_ptr2 + x2, xmask) tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tmp10 = tl.full([1], 0, tl.int32) tmp11 = triton_helpers.maximum(tmp10, tmp9) tmp12 = tmp11 * tmp8 tmp13 = 1.0 tmp14 = tmp13 - tmp8 tmp16 = tmp14 * tmp15 tmp17 = tmp12 + tmp16 tl.store(in_out_ptr0 + x2, tmp17, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf1) del primals_4 del primals_5 buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused__softmax_0[grid(256)](buf1, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf4 = buf3 del buf3 triton_poi_fused__softmax_add_mul_relu_rsub_1[grid(256)](buf4, buf2, buf0, primals_3, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf2 return buf4, primals_3, buf0, buf1 class HighwayMLPNew(nn.Module): def __init__(self, input_size, gate_bias=-2, activation_function=nn. functional.relu, gate_activation=nn.functional.softmax): super(HighwayMLPNew, self).__init__() self.activation_function = activation_function self.gate_activation = gate_activation self.normal_layer = nn.Linear(input_size, input_size) self.gate_layer = nn.Linear(input_size, input_size) self.gate_layer.bias.data.fill_(gate_bias) def forward(self, input_0): primals_1 = self.normal_layer.weight primals_2 = self.normal_layer.bias primals_4 = self.gate_layer.weight primals_5 = self.gate_layer.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
okcd00/glyce
HighwayMLP
false
10,676
[ "Apache-2.0" ]
0
010d88ac5cff4969308d2f8d105831ddcb352a02
https://github.com/okcd00/glyce/tree/010d88ac5cff4969308d2f8d105831ddcb352a02
DecoderLayer
import math import torch import torch.nn.functional as F import torch.nn as nn class FeedForward(nn.Module): def __init__(self, d_model, d_ff=2048, dropout=0.1): super().__init__() self.linear_1 = nn.Linear(d_model, d_ff) self.dropout = nn.Dropout(dropout) self.linear_2 = nn.Linear(d_ff, d_model) def forward(self, x): x = self.dropout(F.relu(self.linear_1(x))) x = self.linear_2(x) return x class MultiHeadAttention(nn.Module): def __init__(self, heads, d_model, dropout=0.1): super().__init__() self.d_model = d_model self.d_k = d_model // heads self.h = heads self.q_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.v_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.k_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.dropout = nn.Dropout(dropout) self.out = nn.Linear(d_model, d_model) def forward(self, q, k, v, mask=None, dropout=None): bs = q.size(0) if q.size(1) > 230: self.h = 8 self.d_k = self.d_model // self.h elif q.size(1) <= 138 and q.size(1) > 230: self.h = 4 self.d_k = self.d_model // self.h elif q.size(1) <= 138 and q.size(1) > 0: self.h = 2 self.d_k = self.d_model // self.h k = torch.matmul(k, self.k_linear1) k = k.view(bs, -1, self.h, self.d_k) q = torch.matmul(q, self.q_linear1) q = q.view(bs, -1, self.h, self.d_k) v = torch.matmul(v, self.v_linear1) v = v.view(bs, -1, self.h, self.d_k) q = q.transpose(1, 2) k = k.transpose(1, 2) v = v.transpose(1, 2) k = k.transpose(-2, -1) scores = torch.matmul(q, k) scores = scores / math.sqrt(self.d_k) if mask is not None: mask = mask.unsqueeze(1) scores = scores.masked_fill(mask == 0, -1000000000.0) scores = F.softmax(scores, dim=-1) if dropout is not None: scores = dropout(scores) scores = torch.matmul(scores, v) concat = scores.transpose(1, 2).contiguous().view(bs, -1, self.d_model) output = self.out(concat) return output class Norm(nn.Module): def __init__(self, d_model, eps=1e-06): super().__init__() self.size = d_model self.alpha = nn.Parameter(torch.ones(self.size)) self.bias = nn.Parameter(torch.zeros(self.size)) self.eps = eps def forward(self, x): norm = self.alpha * (x - x.mean(dim=-1, keepdim=True)) / (x.std(dim =-1, keepdim=True) + self.eps) + self.bias return norm class DecoderLayer(nn.Module): def __init__(self, d_model, heads, dropout=0.1): super().__init__() self.norm_1 = Norm(d_model) self.norm_2 = Norm(d_model) self.norm_3 = Norm(d_model) self.dropout_1 = nn.Dropout(dropout) self.dropout_2 = nn.Dropout(dropout) self.dropout_3 = nn.Dropout(dropout) self.attn_1 = MultiHeadAttention(heads, d_model, dropout=dropout) self.attn_2 = MultiHeadAttention(heads, d_model, dropout=dropout) self.ff = FeedForward(d_model, dropout=dropout) def forward(self, x, e_outputs, src_mask, trg_mask): x2 = self.norm_1(x) x = x + self.dropout_1(self.attn_1(x2, x2, x2, trg_mask)) x2 = self.norm_2(x) x = x + self.dropout_2(self.attn_2(x2, e_outputs, e_outputs, src_mask)) x2 = self.norm_3(x) x = x + self.dropout_3(self.ff(x2)) return x 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.nn.functional as F import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_div_mean_mul_std_sub_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask) tmp2 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp30 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp8 = tmp6 + tmp7 tmp9 = 4.0 tmp10 = tmp8 / tmp9 tmp11 = tmp1 - tmp10 tmp12 = tmp0 * tmp11 tmp13 = tmp2 - tmp10 tmp14 = tmp13 * tmp13 tmp15 = tmp3 - tmp10 tmp16 = tmp15 * tmp15 tmp17 = tmp14 + tmp16 tmp18 = tmp5 - tmp10 tmp19 = tmp18 * tmp18 tmp20 = tmp17 + tmp19 tmp21 = tmp7 - tmp10 tmp22 = tmp21 * tmp21 tmp23 = tmp20 + tmp22 tmp24 = 3.0 tmp25 = tmp23 / tmp24 tmp26 = libdevice.sqrt(tmp25) tmp27 = 1e-06 tmp28 = tmp26 + tmp27 tmp29 = tmp12 / tmp28 tmp31 = tmp29 + tmp30 tl.store(out_ptr0 + x2, tmp31, xmask) @triton.jit def triton_poi_fused_clone_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 % 2 x1 = xindex // 2 % 4 x2 = xindex // 8 % 2 x3 = xindex // 16 x4 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 2 * x2 + 4 * x1 + 16 * x3), xmask) tl.store(out_ptr0 + x4, tmp0, xmask) @triton.jit def triton_poi_fused_clone_2(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_eq_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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_4(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x2 = xindex // 8 x3 = xindex tmp0 = tl.load(in_ptr0 + (4 * x0 + 16 * x2), xmask, eviction_policy= 'evict_last').to(tl.int1) tmp1 = tl.load(in_ptr1 + 4 * x3, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (1 + 4 * x0 + 16 * x2), xmask, eviction_policy ='evict_last').to(tl.int1) tmp7 = tl.load(in_ptr1 + (1 + 4 * x3), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (2 + 4 * x0 + 16 * x2), xmask, eviction_policy='evict_last').to(tl.int1) tmp12 = tl.load(in_ptr1 + (2 + 4 * x3), xmask, eviction_policy='evict_last' ) tmp16 = tl.load(in_ptr0 + (3 + 4 * x0 + 16 * x2), xmask, eviction_policy='evict_last').to(tl.int1) tmp17 = tl.load(in_ptr1 + (3 + 4 * x3), xmask, eviction_policy='evict_last' ) tmp2 = 0.7071067811865475 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 + x3, tmp20, xmask) tl.store(out_ptr1 + x3, tmp31, xmask) @triton.jit def triton_poi_fused__softmax_div_masked_fill_5(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex // 32 x4 = xindex % 16 x5 = xindex x6 = xindex // 4 tmp0 = tl.load(in_ptr0 + (x4 + 16 * x3), xmask, eviction_policy= 'evict_last').to(tl.int1) tmp1 = tl.load(in_out_ptr0 + x5, xmask) tmp6 = tl.load(in_ptr1 + x6, xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr2 + x6, xmask, eviction_policy='evict_last') tmp2 = 0.7071067811865475 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 + x5, tmp10, xmask) @triton.jit def triton_poi_fused_clone_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 % 2 x1 = xindex // 2 % 2 x2 = xindex // 4 % 4 x3 = xindex // 16 x4 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 2 * x2 + 8 * x1 + 16 * x3), xmask) tl.store(out_ptr0 + x4, tmp0, xmask) @triton.jit def triton_poi_fused_add_mean_std_7(in_out_ptr0, in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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 = 3.0 tmp29 = tmp27 / tmp28 tl.store(out_ptr0 + x0, tmp16, xmask) tl.store(in_out_ptr0 + x0, tmp29, xmask) @triton.jit def triton_poi_fused_add_div_mean_mul_std_sub_8(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask) tmp2 = tl.load(in_ptr2 + x2, xmask) tmp4 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 - tmp4 tmp6 = tmp0 * tmp5 tmp8 = libdevice.sqrt(tmp7) tmp9 = 1e-06 tmp10 = tmp8 + tmp9 tmp11 = tmp6 / tmp10 tmp13 = tmp11 + tmp12 tl.store(out_ptr0 + x2, tmp13, xmask) @triton.jit def triton_poi_fused_add_9(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x2, xmask) tmp3 = tl.load(in_out_ptr0 + 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) @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) @triton.jit def triton_poi_fused_add_11(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) 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) = args args.clear() assert_size_stride(primals_1, (4,), (1,)) assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4, 4), (4, 1)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_8, (4, 4), (4, 1)) assert_size_stride(primals_9, (4,), (1,)) assert_size_stride(primals_10, (4,), (1,)) assert_size_stride(primals_11, (4,), (1,)) assert_size_stride(primals_12, (4, 4), (4, 1)) assert_size_stride(primals_13, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_14, (4, 4), (4, 1)) assert_size_stride(primals_15, (4, 4), (4, 1)) assert_size_stride(primals_16, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_17, (4, 4), (4, 1)) assert_size_stride(primals_18, (4,), (1,)) assert_size_stride(primals_19, (4,), (1,)) assert_size_stride(primals_20, (4,), (1,)) assert_size_stride(primals_21, (2048, 4), (4, 1)) assert_size_stride(primals_22, (2048,), (1,)) assert_size_stride(primals_23, (4, 2048), (2048, 1)) assert_size_stride(primals_24, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_mean_mul_std_sub_0[grid(64)](primals_1, primals_2, primals_3, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_1 del primals_3 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0), primals_4, out=buf1) buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0), primals_5, out=buf2) buf3 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0), primals_6, out=buf3) buf4 = empty_strided_cuda((4, 2, 4, 2), (16, 8, 2, 1), torch.float32) triton_poi_fused_clone_1[grid(64)](buf2, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) buf5 = reinterpret_tensor(buf2, (4, 2, 2, 4), (16, 8, 4, 1), 0) del buf2 triton_poi_fused_clone_2[grid(16, 4)](buf1, buf5, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf6 = empty_strided_cuda((8, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf4, (8, 4, 2), (8, 2, 1), 0 ), reinterpret_tensor(buf5, (8, 2, 4), (8, 4, 1), 0), out=buf6) buf7 = empty_strided_cuda((4, 1, 4, 4), (16, 16, 4, 1), torch.bool) triton_poi_fused_eq_3[grid(64)](primals_7, buf7, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_7 buf8 = empty_strided_cuda((4, 2, 4, 1), (8, 4, 1, 32), torch.float32) buf9 = empty_strided_cuda((4, 2, 4, 1), (8, 4, 1, 32), torch.float32) triton_poi_fused__softmax_div_masked_fill_4[grid(32)](buf7, buf6, buf8, buf9, 32, XBLOCK=32, num_warps=1, num_stages=1) buf10 = reinterpret_tensor(buf6, (4, 2, 4, 4), (32, 16, 4, 1), 0) del buf6 triton_poi_fused__softmax_div_masked_fill_5[grid(128)](buf10, buf7, buf8, buf9, 128, XBLOCK=128, num_warps=4, num_stages=1) buf11 = reinterpret_tensor(buf1, (4, 2, 4, 2), (16, 8, 2, 1), 0) del buf1 triton_poi_fused_clone_1[grid(64)](buf3, buf11, 64, XBLOCK=64, num_warps=1, num_stages=1) buf12 = reinterpret_tensor(buf3, (8, 4, 2), (8, 2, 1), 0) del buf3 extern_kernels.bmm(reinterpret_tensor(buf10, (8, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf11, (8, 4, 2), (8, 2, 1), 0), out=buf12) buf13 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32) triton_poi_fused_clone_6[grid(64)](buf12, buf13, 64, XBLOCK=64, num_warps=1, num_stages=1) buf14 = reinterpret_tensor(buf12, (16, 4), (4, 1), 0) del buf12 extern_kernels.addmm(primals_9, reinterpret_tensor(buf13, (16, 4), (4, 1), 0), reinterpret_tensor(primals_8, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf14) del primals_9 buf15 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf16 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf17 = buf16 del buf16 triton_poi_fused_add_mean_std_7[grid(16)](buf17, primals_2, buf14, buf15, 16, XBLOCK=16, num_warps=1, num_stages=1) buf18 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_13, (16, 4), (4, 1), 0 ), primals_12, out=buf18) del primals_12 buf19 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_add_div_mean_mul_std_sub_8[grid(64)](primals_10, primals_2, buf14, buf15, buf17, primals_11, buf19, 64, XBLOCK= 64, num_warps=1, num_stages=1) del buf15 del buf17 del primals_11 buf20 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf19, (16, 4), (4, 1), 0), primals_14, out=buf20) buf21 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_13, (16, 4), (4, 1), 0 ), primals_15, out=buf21) del primals_15 buf22 = empty_strided_cuda((4, 2, 4, 2), (16, 8, 2, 1), torch.float32) triton_poi_fused_clone_1[grid(64)](buf20, buf22, 64, XBLOCK=64, num_warps=1, num_stages=1) buf23 = reinterpret_tensor(buf20, (4, 2, 2, 4), (16, 8, 4, 1), 0) del buf20 triton_poi_fused_clone_2[grid(16, 4)](buf18, buf23, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf24 = empty_strided_cuda((8, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf22, (8, 4, 2), (8, 2, 1), 0), reinterpret_tensor(buf23, (8, 2, 4), (8, 4, 1), 0), out=buf24) buf25 = empty_strided_cuda((4, 1, 4, 4), (16, 16, 4, 1), torch.bool) triton_poi_fused_eq_3[grid(64)](primals_16, buf25, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_16 buf26 = buf9 del buf9 buf27 = buf8 del buf8 triton_poi_fused__softmax_div_masked_fill_4[grid(32)](buf25, buf24, buf26, buf27, 32, XBLOCK=32, num_warps=1, num_stages=1) buf28 = reinterpret_tensor(buf24, (4, 2, 4, 4), (32, 16, 4, 1), 0) del buf24 triton_poi_fused__softmax_div_masked_fill_5[grid(128)](buf28, buf25, buf26, buf27, 128, XBLOCK=128, num_warps=4, num_stages=1) del buf26 del buf27 buf29 = reinterpret_tensor(buf18, (4, 2, 4, 2), (16, 8, 2, 1), 0) del buf18 triton_poi_fused_clone_1[grid(64)](buf21, buf29, 64, XBLOCK=64, num_warps=1, num_stages=1) buf30 = reinterpret_tensor(buf21, (8, 4, 2), (8, 2, 1), 0) del buf21 extern_kernels.bmm(reinterpret_tensor(buf28, (8, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf29, (8, 4, 2), (8, 2, 1), 0), out=buf30) buf31 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32) triton_poi_fused_clone_6[grid(64)](buf30, buf31, 64, XBLOCK=64, num_warps=1, num_stages=1) buf32 = reinterpret_tensor(buf30, (16, 4), (4, 1), 0) del buf30 extern_kernels.mm(reinterpret_tensor(buf31, (16, 4), (4, 1), 0), reinterpret_tensor(primals_17, (4, 4), (1, 4), 0), out=buf32) buf33 = reinterpret_tensor(buf32, (4, 4, 4), (16, 4, 1), 0) del buf32 triton_poi_fused_add_9[grid(64)](buf33, primals_2, buf14, primals_18, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_18 buf34 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_add_div_mean_mul_std_sub_0[grid(64)](primals_19, buf33, primals_20, buf34, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_20 buf35 = empty_strided_cuda((16, 2048), (2048, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf34, (16, 4), (4, 1), 0), reinterpret_tensor(primals_21, (4, 2048), (1, 4), 0), out=buf35) buf36 = reinterpret_tensor(buf35, (4, 4, 2048), (8192, 2048, 1), 0) del buf35 buf39 = empty_strided_cuda((4, 4, 2048), (8192, 2048, 1), torch.bool) triton_poi_fused_relu_threshold_backward_10[grid(32768)](buf36, primals_22, buf39, 32768, XBLOCK=256, num_warps=4, num_stages=1) del primals_22 buf37 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf36, (16, 2048), (2048, 1), 0), reinterpret_tensor(primals_23, (2048, 4), (1, 2048), 0), out=buf37) buf38 = reinterpret_tensor(buf37, (4, 4, 4), (16, 4, 1), 0) del buf37 triton_poi_fused_add_11[grid(64)](buf38, buf33, primals_24, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_24 return (buf38, primals_2, primals_10, primals_19, buf7, buf10, reinterpret_tensor(buf13, (16, 4), (4, 1), 0), buf14, buf25, buf28, reinterpret_tensor(buf31, (16, 4), (4, 1), 0), buf33, reinterpret_tensor(buf34, (16, 4), (4, 1), 0), reinterpret_tensor( buf36, (16, 2048), (2048, 1), 0), primals_23, buf39, primals_21, primals_17, reinterpret_tensor(buf29, (8, 2, 4), (8, 1, 2), 0), reinterpret_tensor(buf22, (8, 2, 4), (8, 1, 2), 0), reinterpret_tensor(buf23, (8, 4, 2), (8, 1, 4), 0), reinterpret_tensor(primals_13, (4, 16), (1, 4), 0), reinterpret_tensor(buf19, (4, 16), (1, 4), 0), reinterpret_tensor( primals_14, (4, 4), (1, 4), 0), primals_8, reinterpret_tensor(buf11, (8, 2, 4), (8, 1, 2), 0), reinterpret_tensor(buf4, (8, 2, 4), (8, 1, 2), 0), reinterpret_tensor(buf5, (8, 4, 2), (8, 1, 4), 0), reinterpret_tensor(buf0, (4, 16), (1, 4), 0), reinterpret_tensor( primals_6, (4, 4), (1, 4), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0)) class FeedForward(nn.Module): def __init__(self, d_model, d_ff=2048, dropout=0.1): super().__init__() self.linear_1 = nn.Linear(d_model, d_ff) self.dropout = nn.Dropout(dropout) self.linear_2 = nn.Linear(d_ff, d_model) def forward(self, x): x = self.dropout(F.relu(self.linear_1(x))) x = self.linear_2(x) return x class MultiHeadAttention(nn.Module): def __init__(self, heads, d_model, dropout=0.1): super().__init__() self.d_model = d_model self.d_k = d_model // heads self.h = heads self.q_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.v_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.k_linear1 = nn.Parameter(torch.randn(d_model, d_model)) self.dropout = nn.Dropout(dropout) self.out = nn.Linear(d_model, d_model) def forward(self, q, k, v, mask=None, dropout=None): bs = q.size(0) if q.size(1) > 230: self.h = 8 self.d_k = self.d_model // self.h elif q.size(1) <= 138 and q.size(1) > 230: self.h = 4 self.d_k = self.d_model // self.h elif q.size(1) <= 138 and q.size(1) > 0: self.h = 2 self.d_k = self.d_model // self.h k = torch.matmul(k, self.k_linear1) k = k.view(bs, -1, self.h, self.d_k) q = torch.matmul(q, self.q_linear1) q = q.view(bs, -1, self.h, self.d_k) v = torch.matmul(v, self.v_linear1) v = v.view(bs, -1, self.h, self.d_k) q = q.transpose(1, 2) k = k.transpose(1, 2) v = v.transpose(1, 2) k = k.transpose(-2, -1) scores = torch.matmul(q, k) scores = scores / math.sqrt(self.d_k) if mask is not None: mask = mask.unsqueeze(1) scores = scores.masked_fill(mask == 0, -1000000000.0) scores = F.softmax(scores, dim=-1) if dropout is not None: scores = dropout(scores) scores = torch.matmul(scores, v) concat = scores.transpose(1, 2).contiguous().view(bs, -1, self.d_model) output = self.out(concat) return output class Norm(nn.Module): def __init__(self, d_model, eps=1e-06): super().__init__() self.size = d_model self.alpha = nn.Parameter(torch.ones(self.size)) self.bias = nn.Parameter(torch.zeros(self.size)) self.eps = eps def forward(self, x): norm = self.alpha * (x - x.mean(dim=-1, keepdim=True)) / (x.std(dim =-1, keepdim=True) + self.eps) + self.bias return norm class DecoderLayerNew(nn.Module): def __init__(self, d_model, heads, dropout=0.1): super().__init__() self.norm_1 = Norm(d_model) self.norm_2 = Norm(d_model) self.norm_3 = Norm(d_model) self.dropout_1 = nn.Dropout(dropout) self.dropout_2 = nn.Dropout(dropout) self.dropout_3 = nn.Dropout(dropout) self.attn_1 = MultiHeadAttention(heads, d_model, dropout=dropout) self.attn_2 = MultiHeadAttention(heads, d_model, dropout=dropout) self.ff = FeedForward(d_model, dropout=dropout) def forward(self, input_0, input_1, input_2, input_3): primals_1 = self.norm_1.alpha primals_3 = self.norm_1.bias primals_9 = self.norm_2.alpha primals_10 = self.norm_2.bias primals_11 = self.norm_3.alpha primals_18 = self.norm_3.bias primals_4 = self.attn_1.q_linear1 primals_5 = self.attn_1.v_linear1 primals_6 = self.attn_1.k_linear1 primals_8 = self.attn_1.out.weight primals_19 = self.attn_1.out.bias primals_12 = self.attn_2.q_linear1 primals_14 = self.attn_2.v_linear1 primals_15 = self.attn_2.k_linear1 primals_17 = self.attn_2.out.weight primals_20 = self.attn_2.out.bias primals_21 = self.ff.linear_1.weight primals_22 = self.ff.linear_1.bias primals_23 = self.ff.linear_2.weight primals_24 = self.ff.linear_2.bias primals_2 = input_0 primals_7 = input_1 primals_13 = input_2 primals_16 = 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]) return output[0]
nlakshmanan/Transformer
DecoderLayer
false
10,677
[ "Apache-2.0" ]
0
4562f8e9b282d0a70f26903a7b4410cb6132364b
https://github.com/nlakshmanan/Transformer/tree/4562f8e9b282d0a70f26903a7b4410cb6132364b
TiledConv2d
import torch import torch.nn as nn import torch.nn.functional as F class TiledConv2d(nn.Module): def __init__(self, in_features, out_features): super().__init__() self.conv = nn.Conv2d(in_features, out_features, kernel_size=3, bias=False) def forward(self, x): return self.conv(F.pad(x, [1, 1, 1, 1], mode='circular')) 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 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_copy_0(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 x2 = xindex // 36 x4 = xindex tmp0 = x0 tmp1 = tl.full([1], 5, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = -4 + x0 tmp4 = tl.full([1], 1, tl.int64) tmp5 = tmp3 < tmp4 tmp6 = tmp5 & tmp2 tmp7 = tmp0 >= tmp4 tmp8 = tmp0 < tmp1 tmp9 = tmp7 & tmp8 tmp10 = tmp9 & tmp6 tmp11 = x1 tmp12 = tmp11 >= tmp4 tmp13 = tmp11 < tmp1 tmp14 = tmp12 & tmp13 tmp15 = tmp14 & tmp10 tmp16 = tl.load(in_ptr0 + (-5 + x0 + 4 * x1 + 16 * x2), tmp15 & xmask, other=0.0) tmp17 = tl.load(in_ptr1 + x4, tmp10 & xmask, other=0.0) tmp18 = tl.where(tmp14, tmp16, tmp17) tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype) tmp20 = tl.where(tmp10, tmp18, tmp19) tmp21 = float('nan') tmp22 = tl.where(tmp9, tmp20, tmp21) tmp23 = tl.full(tmp22.shape, 0.0, tmp22.dtype) tmp24 = tl.where(tmp6, tmp22, tmp23) tmp25 = tmp3 >= tmp4 tmp26 = tmp3 < tmp1 tmp27 = tmp25 & tmp26 tmp28 = tmp27 & tmp2 tmp29 = tmp14 & tmp28 tmp30 = tl.load(in_ptr0 + (-9 + x0 + 4 * x1 + 16 * x2), tmp29 & xmask, other=0.0) tmp31 = tl.load(in_ptr1 + (-4 + x4), tmp28 & xmask, other=0.0) tmp32 = tl.where(tmp14, tmp30, tmp31) tmp33 = tl.full(tmp32.shape, 0.0, tmp32.dtype) tmp34 = tl.where(tmp28, tmp32, tmp33) tmp35 = tl.where(tmp27, tmp34, tmp21) tmp36 = tl.where(tmp5, tmp24, tmp35) tmp37 = tl.full(tmp36.shape, 0.0, tmp36.dtype) tmp38 = tl.where(tmp2, tmp36, tmp37) tmp39 = tmp0 < tmp4 tmp40 = 4 + x0 tmp41 = tmp40 >= tmp4 tmp42 = tmp40 < tmp1 tmp43 = tmp41 & tmp42 tmp44 = tmp43 & tmp39 tmp45 = tmp14 & tmp44 tmp46 = tl.load(in_ptr0 + (-1 + x0 + 4 * x1 + 16 * x2), tmp45 & xmask, other=0.0) tmp47 = tl.load(in_ptr1 + (4 + x4), tmp44 & xmask, other=0.0) tmp48 = tl.where(tmp14, tmp46, tmp47) tmp49 = tl.full(tmp48.shape, 0.0, tmp48.dtype) tmp50 = tl.where(tmp44, tmp48, tmp49) tmp51 = tl.where(tmp43, tmp50, tmp21) tmp52 = tl.full(tmp51.shape, 0.0, tmp51.dtype) tmp53 = tl.where(tmp39, tmp51, tmp52) tmp54 = tmp14 & tmp9 tmp55 = tl.load(in_ptr0 + (-5 + x0 + 4 * x1 + 16 * x2), tmp54 & xmask, other=0.0) tmp56 = tl.load(in_ptr1 + x4, tmp9 & xmask, other=0.0) tmp57 = tl.where(tmp14, tmp55, tmp56) tmp58 = tl.full(tmp57.shape, 0.0, tmp57.dtype) tmp59 = tl.where(tmp9, tmp57, tmp58) tmp60 = tl.where(tmp9, tmp59, tmp21) tmp61 = tl.where(tmp39, tmp53, tmp60) tmp62 = tl.where(tmp2, tmp38, tmp61) tl.store(out_ptr0 + x4, tmp62, xmask) @triton.jit def triton_poi_fused_1(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 x1 = xindex // 6 % 6 x0 = xindex % 6 x2 = xindex // 36 x3 = xindex tmp14 = tl.load(in_ptr0 + x3, xmask) tmp0 = x1 tmp1 = tl.full([1], 5, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = -4 + x1 tmp4 = tl.full([1], 1, tl.int64) tmp5 = tmp3 < tmp4 tmp6 = tmp5 & tmp2 tmp7 = tl.load(in_ptr0 + (24 + x0 + 36 * x2), tmp6 & xmask, eviction_policy='evict_last', other=0.0) tmp8 = tl.load(in_ptr0 + (-24 + x3), tmp2 & xmask, other=0.0) tmp9 = tl.where(tmp5, tmp7, tmp8) tmp10 = tl.full(tmp9.shape, 0.0, tmp9.dtype) tmp11 = tl.where(tmp2, tmp9, tmp10) tmp12 = tmp0 < tmp4 tmp13 = tl.load(in_ptr0 + (24 + x0 + 36 * x2), tmp12 & xmask, eviction_policy='evict_last', other=0.0) tmp15 = tl.where(tmp12, tmp13, tmp14) tmp16 = tl.where(tmp2, tmp11, tmp15) tl.store(out_ptr0 + x3, tmp16, 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, 3, 3), (36, 9, 3, 1)) buf0 = empty_strided_cuda((4, 4, 6, 6), (144, 36, 6, 1), torch.float32) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((4, 4, 6, 6), (144, 36, 6, 1), torch.float32) get_raw_stream(0) triton_poi_fused_copy_0[grid(576)](primals_1, buf0, buf1, 576, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 buf2 = buf0 del buf0 triton_poi_fused_1[grid(576)](buf1, buf2, 576, XBLOCK=256, num_warps=4, num_stages=1) del buf1 buf3 = extern_kernels.convolution(buf2, primals_2, 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)) return buf3, primals_2, buf2 class TiledConv2dNew(nn.Module): def __init__(self, in_features, out_features): super().__init__() self.conv = nn.Conv2d(in_features, out_features, kernel_size=3, bias=False) def forward(self, input_0): primals_2 = self.conv.weight primals_1 = input_0 output = call([primals_1, primals_2]) return output[0]
mkarmann/conway-reversed
TiledConv2d
false
10,678
[ "MIT" ]
0
a3ae10dd5768affb9caf193a246395ee0fb2bc6f
https://github.com/mkarmann/conway-reversed/tree/a3ae10dd5768affb9caf193a246395ee0fb2bc6f
PositionWiseFFN
import torch from torch import nn from torch.nn.functional import relu class PositionWiseFFN(nn.Module): def __init__(self, model_dim, dropout=0.0): super().__init__() dff = model_dim * 4 self.l = nn.Linear(model_dim, dff) self.o = nn.Linear(dff, model_dim) self.dropout = nn.Dropout(dropout) self.layer_norm = nn.LayerNorm(model_dim) def forward(self, x): o = relu(self.l(x)) o = self.o(o) o = self.dropout(o) o = self.layer_norm(x + o) return o def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'model_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice 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): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 16 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) @triton.jit def triton_poi_fused_add_native_layer_norm_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp9 = tmp7 + tmp8 tmp10 = tmp6 + tmp9 tmp13 = tmp11 + tmp12 tmp14 = tmp10 + tmp13 tmp15 = 4.0 tmp16 = tmp14 / tmp15 tmp17 = tmp2 - tmp16 tmp18 = tmp17 * tmp17 tmp19 = tmp5 - tmp16 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp22 = tmp9 - tmp16 tmp23 = tmp22 * tmp22 tmp24 = tmp21 + tmp23 tmp25 = tmp13 - tmp16 tmp26 = tmp25 * tmp25 tmp27 = tmp24 + tmp26 tmp28 = tmp27 / tmp15 tl.store(out_ptr0 + x0, tmp16, xmask) tl.store(out_ptr1 + x0, tmp28, xmask) @triton.jit def triton_poi_fused_add_native_layer_norm_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x2, xmask) tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 - tmp3 tmp6 = 1e-05 tmp7 = tmp5 + tmp6 tmp8 = libdevice.rsqrt(tmp7) tmp9 = tmp4 * tmp8 tmp11 = tmp9 * tmp10 tmp13 = tmp11 + tmp12 tl.store(out_ptr0 + x2, tmp13, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (16, 4), (4, 1)) assert_size_stride(primals_2, (16,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 16), (16, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 16), (16, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 16), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 16), (256, 64, 16, 1), 0) del buf0 buf6 = empty_strided_cuda((4, 4, 4, 16), (256, 64, 16, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(1024)](buf1, primals_2, buf6, 1024, 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, 16), (16, 1), 0), reinterpret_tensor(primals_4, (16, 4), (1, 16), 0), alpha=1, beta=1, out=buf2) del primals_5 buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf4 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) triton_poi_fused_add_native_layer_norm_1[grid(64)](primals_3, 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_add_native_layer_norm_2[grid(256)](primals_3, buf2, buf3, buf4, primals_6, primals_7, buf5, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf3 del buf4 del primals_7 return buf5, primals_3, primals_6, reinterpret_tensor(buf1, (64, 16), ( 16, 1), 0), buf2, primals_4, buf6 class PositionWiseFFNNew(nn.Module): def __init__(self, model_dim, dropout=0.0): super().__init__() dff = model_dim * 4 self.l = nn.Linear(model_dim, dff) self.o = nn.Linear(dff, model_dim) self.dropout = nn.Dropout(dropout) self.layer_norm = nn.LayerNorm(model_dim) def forward(self, input_0): primals_1 = self.l.weight primals_2 = self.l.bias primals_4 = self.o.weight primals_5 = self.o.bias primals_6 = self.layer_norm.weight primals_7 = self.layer_norm.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
richardzhangy26/NLP-Tutorials
PositionWiseFFN
false
10,679
[ "MIT" ]
0
ddf123853c53cef1142207c3a4fb9aa6ac87febd
https://github.com/richardzhangy26/NLP-Tutorials/tree/ddf123853c53cef1142207c3a4fb9aa6ac87febd
PoseRegHead
import torch import torch.nn as nn import torch.nn.functional as F def _get_fc_layer(in_cn, out_cn): x = nn.Linear(in_cn, out_cn) x.bias.data.zero_() nn.init.normal_(x.weight, 0.0, 0.001) return x class PoseRegHead(nn.Module): def __init__(self, dim_in, dim_out, num_units=4096): super(PoseRegHead, self).__init__() self.dim_in = dim_in self.dim_out = dim_out * 4 self.num_units = num_units self.poses_fc1 = _get_fc_layer(self.dim_in, num_units) self.poses_fc2 = _get_fc_layer(num_units, num_units) self.poses_fc3 = _get_fc_layer(num_units, self.dim_out) def forward(self, x, drop_prob=0.0, is_train=False): x_flat = x.view(-1, self.dim_in) fc1 = self.poses_fc1(x_flat) fc1 = F.normalize(fc1, p=2, dim=1) fc1 = F.dropout(F.relu(fc1, inplace=True), drop_prob, training=is_train ) fc2 = self.poses_fc2(fc1) fc2 = F.normalize(fc2, p=2, dim=1) fc2 = F.dropout(F.relu(fc2, inplace=True), drop_prob, training=is_train ) fc3 = self.poses_fc3(fc2) fc3 = F.normalize(fc3, p=2, dim=1) return F.tanh(fc3) 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 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_red_fused_div_linalg_vector_norm_relu_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr, RBLOCK: tl.constexpr): xnumel = 64 rnumel = 4096 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rbase = tl.arange(0, RBLOCK)[None, :] x0 = xindex _tmp3 = tl.full([XBLOCK, RBLOCK], 0, tl.float32) for roffset in range(0, rnumel, RBLOCK): rindex = roffset + rbase rmask = rindex < rnumel r1 = rindex tmp0 = tl.load(in_ptr0 + (r1 + 4096 * x0), rmask & xmask, eviction_policy='evict_last', other=0.0) tmp1 = tmp0 * tmp0 tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp4 = _tmp3 + tmp2 _tmp3 = tl.where(rmask & xmask, tmp4, _tmp3) tmp3 = tl.sum(_tmp3, 1)[:, None] tmp5 = libdevice.sqrt(tmp3) tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp5, xmask) for roffset in range(0, rnumel, RBLOCK): rindex = roffset + rbase rmask = rindex < rnumel r1 = rindex tmp6 = tl.load(in_ptr0 + (r1 + 4096 * x0), rmask & xmask, eviction_policy='evict_first', other=0.0) tmp7 = 1e-12 tmp8 = triton_helpers.maximum(tmp5, tmp7) tmp9 = tmp6 / tmp8 tmp10 = tl.full([1, 1], 0, tl.int32) tmp11 = triton_helpers.maximum(tmp10, tmp9) tl.store(out_ptr0 + (r1 + 4096 * x0), tmp11, rmask & xmask) @triton.jit def triton_per_fused_div_linalg_vector_norm_tanh_1(in_out_ptr0, in_ptr0, out_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) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tmp0 * tmp0 tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp4 = tl.where(xmask, tmp2, 0) tmp5 = tl.sum(tmp4, 1)[:, None] tmp6 = libdevice.sqrt(tmp5) tmp7 = 1e-12 tmp8 = triton_helpers.maximum(tmp6, tmp7) tmp9 = tmp0 / tmp8 tmp10 = libdevice.tanh(tmp9) tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp6, xmask) tl.store(out_ptr0 + (r1 + 16 * x0), tmp10, 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, (4096, 4), (4, 1)) assert_size_stride(primals_3, (4096,), (1,)) assert_size_stride(primals_4, (4096, 4096), (4096, 1)) assert_size_stride(primals_5, (4096,), (1,)) assert_size_stride(primals_6, (16, 4096), (4096, 1)) assert_size_stride(primals_7, (16,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4096), (4096, 1), torch.float32) extern_kernels.addmm(primals_3, reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4096), (1, 4), 0), alpha=1, beta=1, out=buf0) del primals_2 del primals_3 buf1 = empty_strided_cuda((64, 1), (1, 64), torch.float32) buf2 = reinterpret_tensor(buf1, (64, 1), (1, 1), 0) del buf1 buf3 = empty_strided_cuda((64, 4096), (4096, 1), torch.float32) get_raw_stream(0) triton_red_fused_div_linalg_vector_norm_relu_0[grid(64)](buf2, buf0, buf3, 64, 4096, XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1) buf4 = empty_strided_cuda((64, 4096), (4096, 1), torch.float32) extern_kernels.addmm(primals_5, buf3, reinterpret_tensor(primals_4, (4096, 4096), (1, 4096), 0), alpha=1, beta=1, out=buf4) del primals_5 buf5 = empty_strided_cuda((64, 1), (1, 64), torch.float32) buf6 = reinterpret_tensor(buf5, (64, 1), (1, 1), 0) del buf5 buf7 = empty_strided_cuda((64, 4096), (4096, 1), torch.float32) triton_red_fused_div_linalg_vector_norm_relu_0[grid(64)](buf6, buf4, buf7, 64, 4096, XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1) buf8 = empty_strided_cuda((64, 16), (16, 1), torch.float32) extern_kernels.addmm(primals_7, buf7, reinterpret_tensor(primals_6, (4096, 16), (1, 4096), 0), alpha=1, beta=1, out=buf8) del primals_7 buf9 = empty_strided_cuda((64, 1), (1, 64), torch.float32) buf10 = reinterpret_tensor(buf9, (64, 1), (1, 1), 0) del buf9 buf11 = empty_strided_cuda((64, 16), (16, 1), torch.float32) triton_per_fused_div_linalg_vector_norm_tanh_1[grid(64)](buf10, buf8, buf11, 64, 16, XBLOCK=8, num_warps=2, num_stages=1) return (buf11, reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), buf0, buf2, buf3, buf4, buf6, buf7, buf8, buf10, buf11, primals_6, primals_4) def _get_fc_layer(in_cn, out_cn): x = nn.Linear(in_cn, out_cn) x.bias.data.zero_() nn.init.normal_(x.weight, 0.0, 0.001) return x class PoseRegHeadNew(nn.Module): def __init__(self, dim_in, dim_out, num_units=4096): super(PoseRegHeadNew, self).__init__() self.dim_in = dim_in self.dim_out = dim_out * 4 self.num_units = num_units self.poses_fc1 = _get_fc_layer(self.dim_in, num_units) self.poses_fc2 = _get_fc_layer(num_units, num_units) self.poses_fc3 = _get_fc_layer(num_units, self.dim_out) def forward(self, input_0): primals_2 = self.poses_fc1.weight primals_3 = self.poses_fc1.bias primals_4 = self.poses_fc2.weight primals_5 = self.poses_fc2.bias primals_6 = self.poses_fc3.weight primals_7 = self.poses_fc3.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
mrlooi/PoseCNN
PoseRegHead
false
10,680
[ "MIT" ]
0
c103bd7dc743edbc9c7cc8a4687b035e3d1150f6
https://github.com/mrlooi/PoseCNN/tree/c103bd7dc743edbc9c7cc8a4687b035e3d1150f6
FCNet
import torch import torch.nn as nn import torch.nn.parallel import torch.optim import torch.utils.data class FCNet(nn.Module): def __init__(self): super(FCNet, self).__init__() self.fc1 = nn.Linear(3 * 28 * 28, 128) self.fc2 = nn.Linear(128, 5) def forward(self, x): x = x.view(-1, 3 * 28 * 28) x = torch.sigmoid(self.fc1(x)) x = self.fc2(x) return x def get_inputs(): return [torch.rand([4, 2352])] 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 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 reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_sigmoid_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 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.sigmoid(tmp2) tl.store(in_out_ptr0 + x2, tmp3, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 2352), (2352, 1)) assert_size_stride(primals_2, (128, 2352), (2352, 1)) assert_size_stride(primals_3, (128,), (1,)) assert_size_stride(primals_4, (5, 128), (128, 1)) assert_size_stride(primals_5, (5,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 128), (128, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (2352, 128), (1, 2352), 0), out=buf0) del primals_2 buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_sigmoid_0[grid(512)](buf1, primals_3, 512, XBLOCK= 256, num_warps=4, num_stages=1) del primals_3 buf2 = empty_strided_cuda((4, 5), (5, 1), torch.float32) extern_kernels.addmm(primals_5, buf1, reinterpret_tensor(primals_4, (128, 5), (1, 128), 0), alpha=1, beta=1, out=buf2) del primals_5 return buf2, primals_1, buf1, primals_4 class FCNetNew(nn.Module): def __init__(self): super(FCNetNew, self).__init__() self.fc1 = nn.Linear(3 * 28 * 28, 128) self.fc2 = nn.Linear(128, 5) 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_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
rilu0361/mytorch
FCNet
false
10,681
[ "MIT" ]
0
9f00b830b3ce8fdf942cd19704dedfe6ffd359a5
https://github.com/rilu0361/mytorch/tree/9f00b830b3ce8fdf942cd19704dedfe6ffd359a5
MultiHeadSelfAttention
from torch.nn import Module import torch from torch.nn import Dropout from torch.nn import Linear from torch.nn.modules import Dropout def masked_softmax(vector: 'torch.Tensor', mask: 'torch.Tensor', dim: 'int'=-1 ) ->torch.Tensor: """ ``torch.nn.functional.softmax(vector)`` does not work if some elements of ``vector`` should be masked. This performs a softmax on just the non-masked portions of ``vector``. Passing ``None`` in for the mask is also acceptable; you'll just get a regular softmax. ``vector`` can have an arbitrary number of dimensions; the only requirement is that ``mask`` is broadcastable to ``vector's`` shape. If ``mask`` has fewer dimensions than ``vector``, we will unsqueeze on dimension 1 until they match. If you need a different unsqueezing of your mask, do it yourself before passing the mask into this function. In the case that the input vector is completely masked, this function returns an array of ``0.0``. This behavior may cause ``NaN`` if this is used as the last layer of a model that uses categorical cross-entropy loss. """ if mask is None: result = torch.nn.functional.softmax(vector, dim=dim) else: mask = mask.float() while mask.dim() < vector.dim(): mask = mask.unsqueeze(1) result = torch.nn.functional.softmax(vector + (1 - mask) * - 10000000000.0, dim=dim) return result def weighted_sum(matrix: 'torch.Tensor', attention: 'torch.Tensor' ) ->torch.Tensor: """ Takes a matrix of vectors and a set of weights over the rows in the matrix (which we call an "attention" vector), and returns a weighted sum of the rows in the matrix. This is the typical computation performed after an attention mechanism. Note that while we call this a "matrix" of vectors and an attention "vector", we also handle higher-order tensors. We always sum over the second-to-last dimension of the "matrix", and we assume that all dimensions in the "matrix" prior to the last dimension are matched in the "vector". Non-matched dimensions in the "vector" must be `directly after the batch dimension`. For example, say I have a "matrix" with dimensions ``(batch_size, num_queries, num_words, embedding_dim)``. The attention "vector" then must have at least those dimensions, and could have more. Both: - ``(batch_size, num_queries, num_words)`` (distribution over words for each query) - ``(batch_size, num_documents, num_queries, num_words)`` (distribution over words in a query for each document) are valid input "vectors", producing tensors of shape: ``(batch_size, num_queries, embedding_dim)`` and ``(batch_size, num_documents, num_queries, embedding_dim)`` respectively. """ if attention.dim() == 2 and matrix.dim() == 3: return attention.unsqueeze(1).bmm(matrix).squeeze(1) if attention.dim() == 3 and matrix.dim() == 3: return attention.bmm(matrix) if matrix.dim() - 1 < attention.dim(): expanded_size = list(matrix.size()) for i in range(attention.dim() - matrix.dim() + 1): matrix = matrix.unsqueeze(1) expanded_size.insert(i + 1, attention.size(i + 1)) matrix = matrix.expand(*expanded_size) intermediate = attention.unsqueeze(-1).expand_as(matrix) * matrix return intermediate.sum(dim=-2) class MultiHeadSelfAttention(Module): """ This class implements the key-value scaled dot product attention mechanism detailed in the paper `Attention is all you Need <https://www.semanticscholar.org/paper/Attention-Is-All-You-Need-Vaswani-Shazeer/0737da0767d77606169cbf4187b83e1ab62f6077>`_ . The attention mechanism is a weighted sum of a projection V of the inputs, with respect to the scaled, normalised dot product of Q and K, which are also both linear projections of the input. This procedure is repeated for each attention head, using different parameters. Parameters ---------- num_heads : ``int``, required. The number of attention heads to use. input_dim : ``int``, required. The size of the last dimension of the input tensor. attention_dim ``int``, required. The total dimension of the query and key projections which comprise the dot product attention function. Must be divisible by ``num_heads``. values_dim : ``int``, required. The total dimension which the input is projected to for representing the values, which are combined using the attention. Must be divisible by ``num_heads``. output_projection_dim : ``int``, optional (default = None) The dimensionality of the final output projection. If this is not passed explicitly, the projection has size `input_size`. attention_dropout_prob : ``float``, optional (default = 0.1). The dropout probability applied to the normalised attention distributions. """ def __init__(self, input_dim: 'int', attention_dim: 'int', values_dim: 'int', num_heads: 'int'=1, output_projection_dim: 'int'=None, attention_dropout_prob: 'float'=0.1) ->None: super(MultiHeadSelfAttention, self).__init__() self._num_heads = num_heads self._input_dim = input_dim self._output_dim = output_projection_dim or input_dim self._attention_dim = attention_dim self._values_dim = values_dim if attention_dim % num_heads != 0: raise ValueError( f'Key size ({attention_dim}) must be divisible by the number of attention heads ({num_heads}).' ) if values_dim % num_heads != 0: raise ValueError( f'Value size ({values_dim}) must be divisible by the number of attention heads ({num_heads}).' ) self._combined_projection = Linear(input_dim, 2 * attention_dim + values_dim) self._scale = (input_dim // num_heads) ** 0.5 self._output_projection = Linear(values_dim, self._output_dim) self._attention_dropout = Dropout(attention_dropout_prob) def get_input_dim(self): return self._input_dim def get_output_dim(self): return self._output_dim def is_bidirectional(self): return False def forward(self, inputs: 'torch.Tensor', mask: 'torch.LongTensor'=None ) ->torch.FloatTensor: """ Parameters ---------- inputs : ``torch.FloatTensor``, required. A tensor of shape (batch_size, timesteps, input_dim) mask : ``torch.FloatTensor``, optional (default = None). A tensor of shape (batch_size, timesteps). Returns ------- A tensor of shape (batch_size, timesteps, output_projection_dim), where output_projection_dim = input_dim by default. """ num_heads = self._num_heads batch_size, timesteps, _ = inputs.size() if mask is None: mask = inputs.new_ones(batch_size, timesteps) combined_projection = self._combined_projection(inputs) queries, keys, *values = combined_projection.split(self. _attention_dim, -1) queries = queries.contiguous() keys = keys.contiguous() values = torch.cat(values, -1).contiguous() values_per_head = values.view(batch_size, timesteps, num_heads, int (self._values_dim / num_heads)) values_per_head = values_per_head.transpose(1, 2).contiguous() values_per_head = values_per_head.view(batch_size * num_heads, timesteps, int(self._values_dim / num_heads)) queries_per_head = queries.view(batch_size, timesteps, num_heads, int(self._attention_dim / num_heads)) queries_per_head = queries_per_head.transpose(1, 2).contiguous() queries_per_head = queries_per_head.view(batch_size * num_heads, timesteps, int(self._attention_dim / num_heads)) keys_per_head = keys.view(batch_size, timesteps, num_heads, int( self._attention_dim / num_heads)) keys_per_head = keys_per_head.transpose(1, 2).contiguous() keys_per_head = keys_per_head.view(batch_size * num_heads, timesteps, int(self._attention_dim / num_heads)) scaled_similarities = torch.bmm(queries_per_head / self._scale, keys_per_head.transpose(1, 2)) attention = masked_softmax(scaled_similarities, mask.repeat(1, num_heads).view(batch_size * num_heads, timesteps)) attention = self._attention_dropout(attention) outputs = weighted_sum(values_per_head, attention) outputs = outputs.view(batch_size, num_heads, timesteps, int(self. _values_dim / num_heads)) outputs = outputs.transpose(1, 2).contiguous() outputs = outputs.view(batch_size, timesteps, self._values_dim) outputs = self._output_projection(outputs) return outputs def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'attention_dim': 4, 'values_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math from torch.nn import Module from torch.nn import Dropout from torch.nn import Linear from torch.nn.modules import Dropout 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 = 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 + (8 + x0 + 12 * x1), xmask) tmp1 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + x2, tmp2, xmask) @triton.jit def triton_poi_fused_div_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 12 * x1), xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.5 tmp4 = tmp2 * tmp3 tl.store(out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_clone_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (4 + x0 + 12 * x1), xmask) tmp1 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + x2, tmp2, xmask) @triton.jit def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (12, 4), (4, 1)) assert_size_stride(primals_3, (12,), (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((16, 12), (12, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 12), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(64)](buf0, primals_3, buf1, 64, XBLOCK= 64, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_div_1[grid(64)](buf0, primals_3, buf2, 64, XBLOCK= 64, num_warps=1, num_stages=1) buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_clone_2[grid(64)](buf0, primals_3, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf0 del primals_3 buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf2, reinterpret_tensor(buf3, (4, 4, 4), (16, 1, 4), 0), out=buf4) buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_3[grid(64)](buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf6 = buf4 del buf4 triton_poi_fused__softmax_4[grid(64)](buf5, buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) buf7 = buf5 del buf5 extern_kernels.bmm(buf6, buf1, out=buf7) buf8 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(buf7, (16, 4), ( 4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf8) del primals_5 return reinterpret_tensor(buf8, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(primals_1, (16, 4), (4, 1), 0 ), buf6, reinterpret_tensor(buf7, (16, 4), (4, 1), 0 ), primals_4, reinterpret_tensor(buf1, (4, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf2, (4, 4, 4), (16, 1, 4), 0), buf3 def masked_softmax(vector: 'torch.Tensor', mask: 'torch.Tensor', dim: 'int'=-1 ) ->torch.Tensor: """ ``torch.nn.functional.softmax(vector)`` does not work if some elements of ``vector`` should be masked. This performs a softmax on just the non-masked portions of ``vector``. Passing ``None`` in for the mask is also acceptable; you'll just get a regular softmax. ``vector`` can have an arbitrary number of dimensions; the only requirement is that ``mask`` is broadcastable to ``vector's`` shape. If ``mask`` has fewer dimensions than ``vector``, we will unsqueeze on dimension 1 until they match. If you need a different unsqueezing of your mask, do it yourself before passing the mask into this function. In the case that the input vector is completely masked, this function returns an array of ``0.0``. This behavior may cause ``NaN`` if this is used as the last layer of a model that uses categorical cross-entropy loss. """ if mask is None: result = torch.nn.functional.softmax(vector, dim=dim) else: mask = mask.float() while mask.dim() < vector.dim(): mask = mask.unsqueeze(1) result = torch.nn.functional.softmax(vector + (1 - mask) * - 10000000000.0, dim=dim) return result def weighted_sum(matrix: 'torch.Tensor', attention: 'torch.Tensor' ) ->torch.Tensor: """ Takes a matrix of vectors and a set of weights over the rows in the matrix (which we call an "attention" vector), and returns a weighted sum of the rows in the matrix. This is the typical computation performed after an attention mechanism. Note that while we call this a "matrix" of vectors and an attention "vector", we also handle higher-order tensors. We always sum over the second-to-last dimension of the "matrix", and we assume that all dimensions in the "matrix" prior to the last dimension are matched in the "vector". Non-matched dimensions in the "vector" must be `directly after the batch dimension`. For example, say I have a "matrix" with dimensions ``(batch_size, num_queries, num_words, embedding_dim)``. The attention "vector" then must have at least those dimensions, and could have more. Both: - ``(batch_size, num_queries, num_words)`` (distribution over words for each query) - ``(batch_size, num_documents, num_queries, num_words)`` (distribution over words in a query for each document) are valid input "vectors", producing tensors of shape: ``(batch_size, num_queries, embedding_dim)`` and ``(batch_size, num_documents, num_queries, embedding_dim)`` respectively. """ if attention.dim() == 2 and matrix.dim() == 3: return attention.unsqueeze(1).bmm(matrix).squeeze(1) if attention.dim() == 3 and matrix.dim() == 3: return attention.bmm(matrix) if matrix.dim() - 1 < attention.dim(): expanded_size = list(matrix.size()) for i in range(attention.dim() - matrix.dim() + 1): matrix = matrix.unsqueeze(1) expanded_size.insert(i + 1, attention.size(i + 1)) matrix = matrix.expand(*expanded_size) intermediate = attention.unsqueeze(-1).expand_as(matrix) * matrix return intermediate.sum(dim=-2) class MultiHeadSelfAttentionNew(Module): """ This class implements the key-value scaled dot product attention mechanism detailed in the paper `Attention is all you Need <https://www.semanticscholar.org/paper/Attention-Is-All-You-Need-Vaswani-Shazeer/0737da0767d77606169cbf4187b83e1ab62f6077>`_ . The attention mechanism is a weighted sum of a projection V of the inputs, with respect to the scaled, normalised dot product of Q and K, which are also both linear projections of the input. This procedure is repeated for each attention head, using different parameters. Parameters ---------- num_heads : ``int``, required. The number of attention heads to use. input_dim : ``int``, required. The size of the last dimension of the input tensor. attention_dim ``int``, required. The total dimension of the query and key projections which comprise the dot product attention function. Must be divisible by ``num_heads``. values_dim : ``int``, required. The total dimension which the input is projected to for representing the values, which are combined using the attention. Must be divisible by ``num_heads``. output_projection_dim : ``int``, optional (default = None) The dimensionality of the final output projection. If this is not passed explicitly, the projection has size `input_size`. attention_dropout_prob : ``float``, optional (default = 0.1). The dropout probability applied to the normalised attention distributions. """ def __init__(self, input_dim: 'int', attention_dim: 'int', values_dim: 'int', num_heads: 'int'=1, output_projection_dim: 'int'=None, attention_dropout_prob: 'float'=0.1) ->None: super(MultiHeadSelfAttentionNew, self).__init__() self._num_heads = num_heads self._input_dim = input_dim self._output_dim = output_projection_dim or input_dim self._attention_dim = attention_dim self._values_dim = values_dim if attention_dim % num_heads != 0: raise ValueError( f'Key size ({attention_dim}) must be divisible by the number of attention heads ({num_heads}).' ) if values_dim % num_heads != 0: raise ValueError( f'Value size ({values_dim}) must be divisible by the number of attention heads ({num_heads}).' ) self._combined_projection = Linear(input_dim, 2 * attention_dim + values_dim) self._scale = (input_dim // num_heads) ** 0.5 self._output_projection = Linear(values_dim, self._output_dim) self._attention_dropout = Dropout(attention_dropout_prob) def get_input_dim(self): return self._input_dim def get_output_dim(self): return self._output_dim def is_bidirectional(self): return False def forward(self, input_0): primals_2 = self._combined_projection.weight primals_3 = self._combined_projection.bias primals_4 = self._output_projection.weight primals_5 = self._output_projection.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
okcd00/glyce
MultiHeadSelfAttention
false
10,682
[ "Apache-2.0" ]
0
010d88ac5cff4969308d2f8d105831ddcb352a02
https://github.com/okcd00/glyce/tree/010d88ac5cff4969308d2f8d105831ddcb352a02
StatsPool
import torch import torch.nn as nn class StatsPool(nn.Module): def __init__(self, floor=1e-10, bessel=False): super(StatsPool, self).__init__() self.floor = floor self.bessel = bessel def forward(self, x): means = torch.mean(x, dim=1) _, t, _ = x.shape if self.bessel: t = t - 1 residuals = x - means.unsqueeze(1) numerator = torch.sum(residuals ** 2, dim=1) stds = torch.sqrt(torch.clamp(numerator, min=self.floor) / t) x = torch.cat([means, stds], dim=1) return x def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_cat_0(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 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 + (16 * x1 + x0), tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp6 = tl.load(in_ptr0 + (4 + 16 * x1 + x0), tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp7 = tmp5 + tmp6 tmp8 = tl.load(in_ptr0 + (8 + 16 * x1 + x0), tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp9 = tmp7 + tmp8 tmp10 = tl.load(in_ptr0 + (12 + 16 * x1 + x0), tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp11 = tmp9 + tmp10 tmp12 = 4.0 tmp13 = tmp11 / tmp12 tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype) tmp15 = tl.where(tmp4, tmp13, tmp14) tmp16 = tmp0 >= tmp3 tl.full([1], 8, tl.int64) tmp19 = tl.load(in_ptr0 + (16 * x1 + (-4 + x0)), tmp16 & xmask, eviction_policy='evict_last', other=0.0) tmp20 = tl.load(in_ptr0 + (4 + 16 * x1 + (-4 + x0)), tmp16 & xmask, eviction_policy='evict_last', other=0.0) tmp21 = tmp19 + tmp20 tmp22 = tl.load(in_ptr0 + (8 + 16 * x1 + (-4 + x0)), tmp16 & xmask, eviction_policy='evict_last', other=0.0) tmp23 = tmp21 + tmp22 tmp24 = tl.load(in_ptr0 + (12 + 16 * x1 + (-4 + x0)), tmp16 & xmask, eviction_policy='evict_last', other=0.0) tmp25 = tmp23 + tmp24 tmp26 = tmp25 / tmp12 tmp27 = tmp19 - tmp26 tmp28 = tmp27 * tmp27 tmp29 = tmp20 - tmp26 tmp30 = tmp29 * tmp29 tmp31 = tmp28 + tmp30 tmp32 = tmp22 - tmp26 tmp33 = tmp32 * tmp32 tmp34 = tmp31 + tmp33 tmp35 = tmp24 - tmp26 tmp36 = tmp35 * tmp35 tmp37 = tmp34 + tmp36 tmp38 = 1e-10 tmp39 = triton_helpers.maximum(tmp37, tmp38) tmp40 = 0.25 tmp41 = tmp39 * tmp40 tmp42 = libdevice.sqrt(tmp41) tmp43 = tl.full(tmp42.shape, 0.0, tmp42.dtype) tmp44 = tl.where(tmp16, tmp42, tmp43) tmp45 = tl.where(tmp4, tmp15, tmp44) tl.store(out_ptr0 + x2, tmp45, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 8), (8, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(32)](arg0_1, buf0, 32, XBLOCK=32, num_warps=1, num_stages=1) del arg0_1 return buf0, class StatsPoolNew(nn.Module): def __init__(self, floor=1e-10, bessel=False): super(StatsPoolNew, self).__init__() self.floor = floor self.bessel = bessel def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
penguinwang96825/Umigame
StatsPool
false
10,683
[ "Apache-2.0" ]
0
98d647ab6f40df08fe31d6b3bc444afe229a914e
https://github.com/penguinwang96825/Umigame/tree/98d647ab6f40df08fe31d6b3bc444afe229a914e
LayerNorm
import torch import torch.nn as nn class LayerNorm(nn.Module): def __init__(self, *args): super().__init__() def forward(self, activation): if len(activation.size()) == 3: ori_size = activation.size() activation = activation.view(-1, activation.size(-1)) else: ori_size = None means = torch.mean(activation, dim=1, keepdim=True) stds = torch.std(activation, dim=1, keepdim=True) activation = (activation - means) / stds if ori_size is not None: activation = activation.view(ori_size) return activation def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_div_mean_std_sub_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = 4.0 tmp9 = tmp7 / tmp8 tmp10 = tmp0 - tmp9 tmp11 = tmp1 - tmp9 tmp12 = tmp11 * tmp11 tmp13 = tmp2 - tmp9 tmp14 = tmp13 * tmp13 tmp15 = tmp12 + tmp14 tmp16 = tmp4 - tmp9 tmp17 = tmp16 * tmp16 tmp18 = tmp15 + tmp17 tmp19 = tmp6 - tmp9 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp22 = 3.0 tmp23 = tmp21 / tmp22 tmp24 = libdevice.sqrt(tmp23) tmp25 = tmp10 / tmp24 tl.store(out_ptr0 + x3, tmp25, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_div_mean_std_sub_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, class LayerNormNew(nn.Module): def __init__(self, *args): super().__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
mansoorcheema/segan_pytorch
LayerNorm
false
10,684
[ "MIT" ]
0
8f3b401e42cadfd1f8ad57a8ba0e89c16cc7ee65
https://github.com/mansoorcheema/segan_pytorch/tree/8f3b401e42cadfd1f8ad57a8ba0e89c16cc7ee65
convTranspose23DUnit
import torch import numpy as np import torch.nn as nn import torch.nn.init as init import torch.nn.init class convTranspose23DUnit(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True, dilation=1, nd=2): super(convTranspose23DUnit, self).__init__() assert nd == 1 or nd == 2 or nd == 3, 'nd is not correctly specified!!!!, it should be {1,2,3}' if nd == 2: self.conv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, output_padding =output_padding, groups=groups, bias=bias, dilation=dilation) elif nd == 3: self.conv = nn.ConvTranspose3d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, output_padding =output_padding, groups=groups, bias=bias, dilation=dilation) else: self.conv = nn.ConvTranspose1d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, output_padding =output_padding, groups=groups, bias=bias, dilation=dilation) init.xavier_uniform(self.conv.weight, gain=np.sqrt(2.0)) init.constant(self.conv.bias, 0) def forward(self, x): return self.conv(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 import numpy as np import torch.nn as nn import torch.nn.init as init import torch.nn.init assert_size_stride = torch._C._dynamo.guards.assert_size_stride @triton.jit def triton_poi_fused_convolution_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 784 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 49 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 7, 7), (196, 49, 7, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(784)](buf1, primals_2, 784, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 return buf1, primals_1, primals_3 class convTranspose23DUnitNew(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True, dilation=1, nd=2): super(convTranspose23DUnitNew, self).__init__() assert nd == 1 or nd == 2 or nd == 3, 'nd is not correctly specified!!!!, it should be {1,2,3}' if nd == 2: self.conv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, output_padding =output_padding, groups=groups, bias=bias, dilation=dilation) elif nd == 3: self.conv = nn.ConvTranspose3d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, output_padding =output_padding, groups=groups, bias=bias, dilation=dilation) else: self.conv = nn.ConvTranspose1d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, output_padding =output_padding, groups=groups, bias=bias, dilation=dilation) init.xavier_uniform(self.conv.weight, gain=np.sqrt(2.0)) init.constant(self.conv.bias, 0) 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]
navid0308/medSynthesisV1
convTranspose23DUnit
false
10,685
[ "MIT" ]
0
6731a67d0eb9bb3e0c1646f01feb24229aa4fe30
https://github.com/navid0308/medSynthesisV1/tree/6731a67d0eb9bb3e0c1646f01feb24229aa4fe30
residualUnit
import torch import numpy as np import torch.nn as nn import torch.nn.functional as F import torch.nn.init as init import torch.nn.init class conv23DUnit(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, groups=1, bias=True, dilation=1, nd=2): super(conv23DUnit, self).__init__() assert nd == 1 or nd == 2 or nd == 3, 'nd is not correctly specified!!!!, it should be {1,2,3}' if nd == 2: self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, groups=groups, bias=bias, dilation=dilation) elif nd == 3: self.conv = nn.Conv3d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, groups=groups, bias=bias, dilation=dilation) else: self.conv = nn.Conv1d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, groups=groups, bias=bias, dilation=dilation) init.xavier_uniform(self.conv.weight, gain=np.sqrt(2.0)) init.constant(self.conv.bias, 0) def forward(self, x): return self.conv(x) class residualUnit(nn.Module): def __init__(self, in_size, out_size, kernel_size=3, stride=1, padding= 1, activation=F.relu, nd=2): super(residualUnit, self).__init__() self.conv1 = conv23DUnit(in_size, out_size, kernel_size, stride, padding, nd=nd) self.conv2 = conv23DUnit(out_size, out_size, kernel_size, stride, padding, nd=nd) def forward(self, x): return F.relu(self.conv2(F.elu(self.conv1(x))) + x) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_size': 4, 'out_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import numpy as np import torch.nn as nn import torch.nn.functional as F import torch.nn.init as init import torch.nn.init 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_elu_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 x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 1.0 tmp6 = tmp2 * tmp5 tmp7 = libdevice.expm1(tmp6) tmp8 = tmp7 * tmp5 tmp9 = tl.where(tmp4, tmp6, tmp8) tl.store(in_out_ptr0 + x3, tmp2, xmask) tl.store(out_ptr0 + x3, tmp9, xmask) @triton.jit def triton_poi_fused_add_convolution_relu_threshold_backward_1(in_out_ptr0, in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x3, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp5 = tl.full([1], 0, tl.int32) tmp6 = triton_helpers.maximum(tmp5, tmp4) tmp7 = 0.0 tmp8 = tmp6 <= tmp7 tl.store(in_out_ptr0 + x3, tmp6, xmask) tl.store(out_ptr0 + x3, tmp8, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4, 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 buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_convolution_elu_0[grid(256)](buf1, primals_2, buf2, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf3 = 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(buf3, (4, 4, 4, 4), (64, 16, 4, 1)) buf4 = buf3 del buf3 buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_add_convolution_relu_threshold_backward_1[grid(256)]( buf4, primals_5, primals_3, buf5, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 return buf4, primals_1, primals_3, primals_4, buf1, buf2, buf5 class conv23DUnit(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, groups=1, bias=True, dilation=1, nd=2): super(conv23DUnit, self).__init__() assert nd == 1 or nd == 2 or nd == 3, 'nd is not correctly specified!!!!, it should be {1,2,3}' if nd == 2: self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, groups=groups, bias=bias, dilation=dilation) elif nd == 3: self.conv = nn.Conv3d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, groups=groups, bias=bias, dilation=dilation) else: self.conv = nn.Conv1d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, groups=groups, bias=bias, dilation=dilation) init.xavier_uniform(self.conv.weight, gain=np.sqrt(2.0)) init.constant(self.conv.bias, 0) def forward(self, x): return self.conv(x) class residualUnitNew(nn.Module): def __init__(self, in_size, out_size, kernel_size=3, stride=1, padding= 1, activation=F.relu, nd=2): super(residualUnitNew, self).__init__() self.conv1 = conv23DUnit(in_size, out_size, kernel_size, stride, padding, nd=nd) self.conv2 = conv23DUnit(out_size, out_size, kernel_size, stride, padding, nd=nd) def forward(self, input_0): primals_1 = self.conv1.conv.weight primals_2 = self.conv1.conv.bias primals_4 = self.conv2.conv.weight primals_5 = self.conv2.conv.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
navid0308/medSynthesisV1
residualUnit
false
10,686
[ "MIT" ]
0
6731a67d0eb9bb3e0c1646f01feb24229aa4fe30
https://github.com/navid0308/medSynthesisV1/tree/6731a67d0eb9bb3e0c1646f01feb24229aa4fe30
CombFilter
import torch import torch.nn as nn import torch.nn.functional as F class CombFilter(nn.Module): def __init__(self, ninputs, fmaps, L): super().__init__() self.L = L self.filt = nn.Conv1d(ninputs, fmaps, 2, dilation=L, bias=False) r_init_weight = torch.ones(ninputs * fmaps, 2) r_init_weight[:, 0] = torch.rand(r_init_weight.size(0)) self.filt.weight.data = r_init_weight.view(fmaps, ninputs, 2) def forward(self, x): x_p = F.pad(x, (self.L, 0)) y = self.filt(x_p) return y def get_inputs(): return [torch.rand([4, 4])] def get_init_inputs(): return [[], {'ninputs': 4, 'fmaps': 4, 'L': 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_constant_pad_nd_0(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 x0 = xindex % 8 x1 = xindex // 8 x2 = xindex tmp0 = -4 + x0 tmp1 = tl.full([1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.load(in_ptr0 + (-4 + x0 + 4 * x1), tmp2 & xmask, other=0.0) tl.store(out_ptr0 + x2, tmp3, xmask) def call(args): primals_1, primals_2 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4, 2), (8, 2, 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_constant_pad_nd_0[grid(32)](primals_1, buf0, 32, XBLOCK=32, num_warps=1, num_stages=1) del primals_1 buf1 = extern_kernels.convolution(reinterpret_tensor(buf0, (1, 4, 8 ), (0, 8, 1), 0), primals_2, stride=(1,), padding=(0,), dilation=(4,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf1, (1, 4, 4), (16, 4, 1)) return reinterpret_tensor(buf1, (4, 4), (4, 1), 0 ), primals_2, reinterpret_tensor(buf0, (1, 4, 8), (32, 8, 1), 0) class CombFilterNew(nn.Module): def __init__(self, ninputs, fmaps, L): super().__init__() self.L = L self.filt = nn.Conv1d(ninputs, fmaps, 2, dilation=L, bias=False) r_init_weight = torch.ones(ninputs * fmaps, 2) r_init_weight[:, 0] = torch.rand(r_init_weight.size(0)) self.filt.weight.data = r_init_weight.view(fmaps, ninputs, 2) def forward(self, input_0): primals_2 = self.filt.weight primals_1 = input_0 output = call([primals_1, primals_2]) return output[0]
mansoorcheema/segan_pytorch
CombFilter
false
10,687
[ "MIT" ]
0
8f3b401e42cadfd1f8ad57a8ba0e89c16cc7ee65
https://github.com/mansoorcheema/segan_pytorch/tree/8f3b401e42cadfd1f8ad57a8ba0e89c16cc7ee65
quadexp
import torch import torch as tr import torch.nn as nn class quadexp(nn.Module): def __init__(self, sigma=2.0): super(quadexp, self).__init__() self.sigma = sigma def forward(self, x: 'tr.Tensor'): return tr.exp(-x ** 2 / self.sigma ** 2) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.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_div_exp_neg_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 = -tmp1 tmp3 = 0.25 tmp4 = tmp2 * tmp3 tmp5 = tl_math.exp(tmp4) tl.store(out_ptr0 + x0, tmp5, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_div_exp_neg_pow_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, class quadexpNew(nn.Module): def __init__(self, sigma=2.0): super(quadexpNew, self).__init__() self.sigma = sigma def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
pierreglaser/MMD-gradient-flow
quadexp
false
10,688
[ "BSD-3-Clause" ]
0
43591137e1d04bed5153887a364fae72621b01ae
https://github.com/pierreglaser/MMD-gradient-flow/tree/43591137e1d04bed5153887a364fae72621b01ae
power
import torch import torch as tr import torch.nn as nn class power(nn.Module): def __init__(self): super(power, self).__init__() def forward(self, x: 'tr.Tensor'): return x.pow(2) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream 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_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 tl.store(out_ptr0 + x0, tmp1, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_pow_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, class powerNew(nn.Module): def __init__(self): super(powerNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
pierreglaser/MMD-gradient-flow
power
false
10,689
[ "BSD-3-Clause" ]
0
43591137e1d04bed5153887a364fae72621b01ae
https://github.com/pierreglaser/MMD-gradient-flow/tree/43591137e1d04bed5153887a364fae72621b01ae
MultiNonLinearClassifier
import torch import torch.nn as nn class MultiNonLinearClassifier(nn.Module): def __init__(self, hidden_size, num_label): super(MultiNonLinearClassifier, self).__init__() self.num_label = num_label self.classifier1 = nn.Linear(hidden_size, int(hidden_size / 2)) self.classifier2 = nn.Linear(int(hidden_size / 2), num_label) def forward(self, input_features): features_output1 = self.classifier1(input_features) features_output1 = nn.ReLU()(features_output1) features_output2 = self.classifier2(features_output1) return features_output2 def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'hidden_size': 4, 'num_label': 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 = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 2 tmp0 = tl.load(in_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, (2, 4), (4, 1)) assert_size_stride(primals_2, (2,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 2), (2, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 2), (2, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 2), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 2), (32, 8, 2, 1), 0) del buf0 buf3 = empty_strided_cuda((4, 4, 4, 2), (32, 8, 2, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(128)](buf1, primals_2, buf3, 128, 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, 2), ( 2, 1), 0), reinterpret_tensor(primals_4, (2, 4), (1, 2), 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, 2), (2, 1), 0), primals_4, buf3 class MultiNonLinearClassifierNew(nn.Module): def __init__(self, hidden_size, num_label): super(MultiNonLinearClassifierNew, self).__init__() self.num_label = num_label self.classifier1 = nn.Linear(hidden_size, int(hidden_size / 2)) self.classifier2 = nn.Linear(int(hidden_size / 2), num_label) def forward(self, input_0): primals_1 = self.classifier1.weight primals_2 = self.classifier1.bias primals_4 = self.classifier2.weight primals_5 = self.classifier2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
okcd00/glyce
MultiNonLinearClassifier
false
10,690
[ "Apache-2.0" ]
0
010d88ac5cff4969308d2f8d105831ddcb352a02
https://github.com/okcd00/glyce/tree/010d88ac5cff4969308d2f8d105831ddcb352a02
Conv2dWithConstraint
import torch import torch.nn as nn class Conv2dWithConstraint(nn.Conv2d): def __init__(self, *args, max_norm=1, **kwargs): self.max_norm = max_norm super(Conv2dWithConstraint, self).__init__(*args, **kwargs) def forward(self, x): self.weight.data = torch.renorm(self.weight.data, p=2, dim=0, maxnorm=self.max_norm) return super(Conv2dWithConstraint, self).forward(x) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4, 'kernel_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice 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_renorm_0(in_ptr0, out_ptr1, xnumel, rnumel, XBLOCK: tl .constexpr): xnumel = 4 RBLOCK: tl.constexpr = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 64 * x0), xmask, other=0.0) tmp1 = tmp0 * tmp0 tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp4 = tl.where(xmask, tmp2, 0) tmp5 = tl.sum(tmp4, 1)[:, None] tmp6 = libdevice.sqrt(tmp5) tmp7 = 1.0 tmp8 = tmp6 > tmp7 tmp9 = 1e-07 tmp10 = tmp6 + tmp9 tmp11 = tl.full([1, 1], 1, tl.int32) tmp12 = tmp11 / tmp10 tmp13 = tmp12 * tmp7 tmp14 = tl.where(tmp8, tmp13, tmp7) tmp15 = tmp0 * tmp14 tl.store(out_ptr1 + (r1 + 64 * x0), tmp15, xmask) @triton.jit def triton_poi_fused_convolution_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + x2, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_per_fused_renorm_0[grid(4)](primals_1, buf1, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) buf2 = extern_kernels.convolution(primals_3, buf1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 4, 1, 1), (4, 1, 1, 1)) buf3 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32) triton_poi_fused_convolution_1[grid(16)](buf2, primals_2, buf3, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_2 buf4 = torch.ops.aten.set_.source_Tensor(primals_1, buf1) assert_size_stride(buf4, (4, 4, 4, 4), (64, 16, 4, 1)) del buf2 del primals_1 return buf3, primals_3, buf1 class Conv2dWithConstraintNew(nn.Conv2d): def __init__(self, *args, max_norm=1, **kwargs): self.max_norm = max_norm super(Conv2dWithConstraintNew, self).__init__(*args, **kwargs) def forward(self, input_0): primals_1 = self.weight primals_2 = self.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
rmpeng/TIE-EEGNet
Conv2dWithConstraint
false
10,691
[ "MIT" ]
0
69817fce3edb67f68bf4e85b53596f122dbc78fb
https://github.com/rmpeng/TIE-EEGNet/tree/69817fce3edb67f68bf4e85b53596f122dbc78fb
laplace
import torch import torch as tr import torch.nn as nn class laplace(nn.Module): def __init__(self, lambda_=2.0): super(laplace, self).__init__() self.lambda_ = lambda_ def forward(self, x: 'tr.Tensor'): return tr.exp(-self.lambda_ * tr.abs(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 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_abs_exp_mul_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl_math.abs(tmp0) tmp2 = -2.0 tmp3 = tmp1 * tmp2 tmp4 = tl_math.exp(tmp3) tl.store(out_ptr0 + x0, tmp4, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_abs_exp_mul_0[grid(256)](arg0_1, buf0, 256, XBLOCK =256, num_warps=4, num_stages=1) del arg0_1 return buf0, class laplaceNew(nn.Module): def __init__(self, lambda_=2.0): super(laplaceNew, self).__init__() self.lambda_ = lambda_ def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
pierreglaser/MMD-gradient-flow
laplace
false
10,692
[ "BSD-3-Clause" ]
0
43591137e1d04bed5153887a364fae72621b01ae
https://github.com/pierreglaser/MMD-gradient-flow/tree/43591137e1d04bed5153887a364fae72621b01ae
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): scores = torch.matmul(query, torch.transpose(key, 2, 3)) / math.sqrt( key.shape[-1]) scores = scores.masked_fill(attention_mask < 0, -10000) normed = torch.softmax(scores, -1) per_head = torch.matmul(normed, value) atten = torch.cat([per_head[:, i, :, :] for i in range(per_head. shape[1])], -1) return atten 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) """ return ln_layer(input + dropout(dense_layer(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 """ atten = self.self_attention(hidden_states, attention_mask) norm_atten = self.add_norm(hidden_states, atten, self. attention_dense, self.attention_dropout, self.attention_layer_norm) interim = self.interm_af(self.interm_dense(norm_atten)) ffn = self.add_norm(norm_atten, interim, self.out_dense, self. out_dropout, self.out_layer_norm) return ffn 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_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_lt_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 = -10000.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 = -10000.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_cat_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 % 4 x2 = xindex // 16 x3 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x1 + 16 * x2), 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 + (4 + x1 + 16 * x2), tmp9 & xmask, eviction_policy='evict_last', other=0.0) tmp11 = tmp0 >= tmp7 tmp12 = tl.full([1], 3, tl.int64) tmp13 = tmp0 < tmp12 tmp14 = tmp11 & tmp13 tmp15 = tl.load(in_ptr0 + (8 + x1 + 16 * x2), tmp14 & xmask, eviction_policy='evict_last', other=0.0) tmp16 = tmp0 >= tmp12 tl.full([1], 4, tl.int64) tmp19 = tl.load(in_ptr0 + (12 + x1 + 16 * x2), tmp16 & xmask, eviction_policy='evict_last', other=0.0) tmp20 = tl.where(tmp14, tmp15, tmp19) tmp21 = tl.where(tmp9, tmp10, tmp20) tmp22 = tl.where(tmp4, tmp5, tmp21) tl.store(out_ptr0 + x3, tmp22, xmask) @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_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_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_clone_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_clone_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 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool) triton_poi_fused_lt_1[grid(64)](primals_8, buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_8 buf7 = reinterpret_tensor(buf0, (4, 4, 4, 1), (16, 4, 1, 64), 0) del buf0 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)](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(buf7, (4, 4, 4), (16, 4, 1), 0) del buf7 triton_poi_fused_cat_4[grid(64)](buf11, buf12, 64, XBLOCK=64, 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)](primals_1, buf13, 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)](primals_1, buf13, 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, buf16, primals_16, 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, buf6, buf9, 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, 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 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): scores = torch.matmul(query, torch.transpose(key, 2, 3)) / math.sqrt( key.shape[-1]) scores = scores.masked_fill(attention_mask < 0, -10000) normed = torch.softmax(scores, -1) per_head = torch.matmul(normed, value) atten = torch.cat([per_head[:, i, :, :] for i in range(per_head. shape[1])], -1) return atten 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) """ return ln_layer(input + dropout(dense_layer(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]
priyamtejaswin/minbert-assignment
BertLayer
false
10,693
[ "Apache-2.0" ]
0
fd41a54441916a6d421640bbee910f64786b303d
https://github.com/priyamtejaswin/minbert-assignment/tree/fd41a54441916a6d421640bbee910f64786b303d
cosine
import torch import torch as tr import torch.nn as nn class cosine(nn.Module): def __init__(self): super(cosine, self).__init__() def forward(self, x: 'tr.Tensor'): return tr.cos(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 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_cos_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl_math.cos(tmp0) tl.store(out_ptr0 + x0, tmp1, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cos_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, class cosineNew(nn.Module): def __init__(self): super(cosineNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
pierreglaser/MMD-gradient-flow
cosine
false
10,694
[ "BSD-3-Clause" ]
0
43591137e1d04bed5153887a364fae72621b01ae
https://github.com/pierreglaser/MMD-gradient-flow/tree/43591137e1d04bed5153887a364fae72621b01ae
OptimizedMLP
import torch import torch.optim import torch.jit import torch.nn as nn class OptimizedMLP(nn.Module): def __init__(self, num_in_features: 'int', num_out_features: 'int'): super(OptimizedMLP, self).__init__() self.act = nn.ELU() self.l_in = nn.Linear(in_features=num_in_features, out_features=107) self.l1 = nn.Linear(in_features=107, out_features=179) self.l2 = nn.Linear(in_features=179, out_features=179) self.l3 = nn.Linear(in_features=179, out_features=184) self.l4 = nn.Linear(in_features=184, out_features=115) self.l_out = nn.Linear(in_features=115, out_features=num_out_features) torch.nn.init.xavier_normal_(self.l_in.weight) torch.nn.init.zeros_(self.l_in.bias) torch.nn.init.xavier_normal_(self.l1.weight) torch.nn.init.zeros_(self.l1.bias) torch.nn.init.xavier_normal_(self.l2.weight) torch.nn.init.zeros_(self.l2.bias) torch.nn.init.xavier_normal_(self.l3.weight) torch.nn.init.zeros_(self.l3.bias) torch.nn.init.xavier_normal_(self.l4.weight) torch.nn.init.zeros_(self.l4.bias) torch.nn.init.xavier_normal_(self.l_out.weight) torch.nn.init.zeros_(self.l_out.bias) def forward(self, x): x = self.act(self.l_in(x)) x = self.act(self.l1(x)) x = self.act(self.l2(x)) x = self.act(self.l3(x)) x = self.act(self.l4(x)) x = self.l_out(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_in_features': 4, 'num_out_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.optim import torch.jit 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 = 6848 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 1712 x1 = xindex // 1712 tmp0 = tl.load(in_ptr0 + x2, 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 + 1728 * x1), tmp7, xmask) @triton.jit def triton_poi_fused_elu_view_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 6848 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 107 x1 = xindex // 107 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 107 * (x1 % 16) + 1728 * (x1 // 16)), xmask) tl.store(out_ptr0 + x2, tmp0, xmask) @triton.jit def triton_poi_fused_elu_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 11456 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 2864 x1 = xindex // 2864 tmp0 = tl.load(in_ptr0 + x2, 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 + 2880 * x1), tmp7, xmask) @triton.jit def triton_poi_fused_elu_view_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 11456 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 179 x1 = xindex // 179 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 179 * (x1 % 16) + 2880 * (x1 // 16)), xmask) tl.store(out_ptr0 + x2, tmp0, xmask) @triton.jit def triton_poi_fused_elu_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 11776 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.0 tmp2 = tmp0 > tmp1 tmp3 = 1.0 tmp4 = tmp0 * tmp3 tmp5 = libdevice.expm1(tmp4) tmp6 = tmp5 * tmp3 tmp7 = tl.where(tmp2, tmp4, tmp6) tl.store(out_ptr0 + x0, tmp7, xmask) @triton.jit def triton_poi_fused_elu_5(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 7360 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 1840 x1 = xindex // 1840 tmp0 = tl.load(in_ptr0 + x2, 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 + 1856 * x1), tmp7, xmask) @triton.jit def triton_poi_fused_elu_view_6(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 7360 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 115 x1 = xindex // 115 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 115 * (x1 % 16) + 1856 * (x1 // 16)), xmask) tl.store(out_ptr0 + x2, tmp0, 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, (107, 4), (4, 1)) assert_size_stride(primals_2, (107,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (179, 107), (107, 1)) assert_size_stride(primals_5, (179,), (1,)) assert_size_stride(primals_6, (179, 179), (179, 1)) assert_size_stride(primals_7, (179,), (1,)) assert_size_stride(primals_8, (184, 179), (179, 1)) assert_size_stride(primals_9, (184,), (1,)) assert_size_stride(primals_10, (115, 184), (184, 1)) assert_size_stride(primals_11, (115,), (1,)) assert_size_stride(primals_12, (4, 115), (115, 1)) assert_size_stride(primals_13, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 107), (107, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 107), (1, 4), 0), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((4, 4, 4, 107), (1728, 428, 107, 1), torch.float32) get_raw_stream(0) triton_poi_fused_elu_0[grid(6848)](buf0, buf1, 6848, XBLOCK=128, num_warps=4, num_stages=1) buf2 = empty_strided_cuda((64, 107), (107, 1), torch.float32) triton_poi_fused_elu_view_1[grid(6848)](buf1, buf2, 6848, XBLOCK= 128, num_warps=4, num_stages=1) del buf1 buf3 = empty_strided_cuda((64, 179), (179, 1), torch.float32) extern_kernels.addmm(primals_5, buf2, reinterpret_tensor(primals_4, (107, 179), (1, 107), 0), alpha=1, beta=1, out=buf3) del primals_5 buf4 = empty_strided_cuda((4, 4, 4, 179), (2880, 716, 179, 1), torch.float32) triton_poi_fused_elu_2[grid(11456)](buf3, buf4, 11456, XBLOCK=128, num_warps=4, num_stages=1) buf5 = empty_strided_cuda((64, 179), (179, 1), torch.float32) triton_poi_fused_elu_view_3[grid(11456)](buf4, buf5, 11456, XBLOCK= 256, num_warps=4, num_stages=1) buf6 = empty_strided_cuda((64, 179), (179, 1), torch.float32) extern_kernels.addmm(primals_7, buf5, reinterpret_tensor(primals_6, (179, 179), (1, 179), 0), alpha=1, beta=1, out=buf6) del primals_7 buf7 = buf4 del buf4 triton_poi_fused_elu_2[grid(11456)](buf6, buf7, 11456, XBLOCK=128, num_warps=4, num_stages=1) buf8 = empty_strided_cuda((64, 179), (179, 1), torch.float32) triton_poi_fused_elu_view_3[grid(11456)](buf7, buf8, 11456, XBLOCK= 256, num_warps=4, num_stages=1) del buf7 buf9 = empty_strided_cuda((64, 184), (184, 1), torch.float32) extern_kernels.addmm(primals_9, buf8, reinterpret_tensor(primals_8, (179, 184), (1, 179), 0), alpha=1, beta=1, out=buf9) del primals_9 buf10 = empty_strided_cuda((4, 4, 4, 184), (2944, 736, 184, 1), torch.float32) triton_poi_fused_elu_4[grid(11776)](buf9, buf10, 11776, XBLOCK=128, num_warps=4, num_stages=1) buf11 = empty_strided_cuda((64, 115), (115, 1), torch.float32) extern_kernels.addmm(primals_11, reinterpret_tensor(buf10, (64, 184 ), (184, 1), 0), reinterpret_tensor(primals_10, (184, 115), (1, 184), 0), alpha=1, beta=1, out=buf11) del primals_11 buf12 = empty_strided_cuda((4, 4, 4, 115), (1856, 460, 115, 1), torch.float32) triton_poi_fused_elu_5[grid(7360)](buf11, buf12, 7360, XBLOCK=256, num_warps=4, num_stages=1) buf13 = empty_strided_cuda((64, 115), (115, 1), torch.float32) triton_poi_fused_elu_view_6[grid(7360)](buf12, buf13, 7360, XBLOCK= 128, num_warps=4, num_stages=1) del buf12 buf14 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_13, buf13, reinterpret_tensor( primals_12, (115, 4), (1, 115), 0), alpha=1, beta=1, out=buf14) del primals_13 return reinterpret_tensor(buf14, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf0, buf2, buf3, buf5, buf6, buf8, buf9, reinterpret_tensor(buf10, (64, 184), (184, 1), 0 ), buf11, buf13, primals_12, primals_10, primals_8, primals_6, primals_4 class OptimizedMLPNew(nn.Module): def __init__(self, num_in_features: 'int', num_out_features: 'int'): super(OptimizedMLPNew, self).__init__() self.act = nn.ELU() self.l_in = nn.Linear(in_features=num_in_features, out_features=107) self.l1 = nn.Linear(in_features=107, out_features=179) self.l2 = nn.Linear(in_features=179, out_features=179) self.l3 = nn.Linear(in_features=179, out_features=184) self.l4 = nn.Linear(in_features=184, out_features=115) self.l_out = nn.Linear(in_features=115, out_features=num_out_features) torch.nn.init.xavier_normal_(self.l_in.weight) torch.nn.init.zeros_(self.l_in.bias) torch.nn.init.xavier_normal_(self.l1.weight) torch.nn.init.zeros_(self.l1.bias) torch.nn.init.xavier_normal_(self.l2.weight) torch.nn.init.zeros_(self.l2.bias) torch.nn.init.xavier_normal_(self.l3.weight) torch.nn.init.zeros_(self.l3.bias) torch.nn.init.xavier_normal_(self.l4.weight) torch.nn.init.zeros_(self.l4.bias) torch.nn.init.xavier_normal_(self.l_out.weight) torch.nn.init.zeros_(self.l_out.bias) def forward(self, input_0): primals_1 = self.l_in.weight primals_2 = self.l_in.bias primals_4 = self.l1.weight primals_5 = self.l1.bias primals_6 = self.l2.weight primals_7 = self.l2.bias primals_8 = self.l3.weight primals_9 = self.l3.bias primals_10 = self.l4.weight primals_11 = self.l4.bias primals_12 = self.l_out.weight primals_13 = self.l_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]) return output[0]
plaveczlambert/nonlinearbubbledynamics
OptimizedMLP
false
10,695
[ "MIT" ]
0
190c5170f7ff6068badeee818c01226c55aaec97
https://github.com/plaveczlambert/nonlinearbubbledynamics/tree/190c5170f7ff6068badeee818c01226c55aaec97
ScoreCap
import torch from torch import nn import torch.nn import torch.optim class ScoreCap(nn.Module): def __init__(self, cap: 'float'): super().__init__() self.cap = cap def forward(self, input): return torch.clip(input, max=self.cap) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'cap': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn import torch.nn import torch.optim assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_clamp_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 = 4.0 tmp2 = triton_helpers.minimum(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_clamp_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, class ScoreCapNew(nn.Module): def __init__(self, cap: 'float'): super().__init__() self.cap = cap def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
mikaylagawarecki/ReAgent
ScoreCap
false
10,696
[ "BSD-3-Clause" ]
0
b1a306a9d3641c8adeb03ac272e5774a0009fa88
https://github.com/mikaylagawarecki/ReAgent/tree/b1a306a9d3641c8adeb03ac272e5774a0009fa88
Concat
import torch from torch import nn import torch.nn import torch.optim class Concat(nn.Module): def forward(self, state: 'torch.Tensor', action: 'torch.Tensor'): return torch.cat((state, action), dim=-1) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch import nn import torch.nn import torch.optim assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_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 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): 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, 8), (128, 32, 8, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(512)](arg0_1, arg1_1, buf0, 512, XBLOCK =256, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf0, class ConcatNew(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]
mikaylagawarecki/ReAgent
Concat
false
10,697
[ "BSD-3-Clause" ]
0
b1a306a9d3641c8adeb03ac272e5774a0009fa88
https://github.com/mikaylagawarecki/ReAgent/tree/b1a306a9d3641c8adeb03ac272e5774a0009fa88
imq
import torch import torch as tr import torch.nn as nn class imq(nn.Module): def __init__(self, c=1.0): super(imq, self).__init__() self.c = c def forward(self, x: 'tr.Tensor'): return 1 / (self.c ** 2 + x ** 2) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream 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 imqNew(nn.Module): def __init__(self, c=1.0): super(imqNew, self).__init__() self.c = c def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
pierreglaser/MMD-gradient-flow
imq
false
10,698
[ "BSD-3-Clause" ]
0
43591137e1d04bed5153887a364fae72621b01ae
https://github.com/pierreglaser/MMD-gradient-flow/tree/43591137e1d04bed5153887a364fae72621b01ae
ResARModule
import torch import torch.nn as nn import torch.nn.functional as F from torch.nn.utils.spectral_norm import spectral_norm from torch.nn.utils.weight_norm import weight_norm def build_norm_layer(norm_type, param=None, num_feats=None): if norm_type == 'bnorm': return nn.BatchNorm1d(num_feats) elif norm_type == 'snorm': spectral_norm(param) return None elif norm_type == 'bsnorm': spectral_norm(param) return nn.BatchNorm1d(num_feats) elif norm_type == 'lnorm': return nn.LayerNorm(num_feats) elif norm_type == 'wnorm': weight_norm(param) return None elif norm_type == 'inorm': return nn.InstanceNorm1d(num_feats, affine=False) elif norm_type == 'affinorm': return nn.InstanceNorm1d(num_feats, affine=True) elif norm_type is None: return None else: raise TypeError('Unrecognized norm type: ', norm_type) class ResARModule(nn.Module): def __init__(self, ninp, fmaps, res_fmaps, kwidth, dilation, bias=True, norm_type=None, act=None): super().__init__() self.dil_conv = nn.Conv1d(ninp, fmaps, kwidth, dilation=dilation, bias=bias) if act is not None: self.act = getattr(nn, act)() else: self.act = nn.PReLU(fmaps, init=0) self.dil_norm = build_norm_layer(norm_type, self.dil_conv, fmaps) self.kwidth = kwidth self.dilation = dilation self.conv_1x1_skip = nn.Conv1d(fmaps, ninp, 1, bias=bias) self.conv_1x1_skip_norm = build_norm_layer(norm_type, self. conv_1x1_skip, ninp) self.conv_1x1_res = nn.Conv1d(fmaps, res_fmaps, 1, bias=bias) self.conv_1x1_res_norm = build_norm_layer(norm_type, self. conv_1x1_res, res_fmaps) def forward_norm(self, x, norm_layer): if norm_layer is not None: return norm_layer(x) else: return x def forward(self, x): kw__1 = self.kwidth - 1 P = kw__1 + kw__1 * (self.dilation - 1) x_p = F.pad(x, (P, 0)) h = self.dil_conv(x_p) h = self.forward_norm(h, self.dil_norm) h = self.act(h) a = h h = self.conv_1x1_skip(h) h = self.forward_norm(h, self.conv_1x1_skip_norm) y = x + h sh = self.conv_1x1_res(a) sh = self.forward_norm(sh, self.conv_1x1_res_norm) return y, sh def get_inputs(): return [torch.rand([4, 4])] def get_init_inputs(): return [[], {'ninp': 4, 'fmaps': 4, 'res_fmaps': 4, 'kwidth': 4, 'dilation': 1}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn from torch.nn.utils.spectral_norm import spectral_norm from torch.nn.utils.weight_norm import weight_norm assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_constant_pad_nd_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 28 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 7 x1 = xindex // 7 x2 = xindex tmp0 = -3 + x0 tmp1 = tl.full([1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.load(in_ptr0 + (-3 + x0 + 4 * x1), tmp2 & xmask, other=0.0) tl.store(out_ptr0 + x2, tmp3, xmask) @triton.jit def triton_poi_fused__prelu_kernel_convolution_1(in_out_ptr0, in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp6 = tmp5 * tmp2 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(in_out_ptr0 + x2, tmp2, xmask) tl.store(out_ptr0 + x2, tmp7, xmask) @triton.jit def triton_poi_fused_add_2(in_out_ptr0, in_ptr0, in_ptr1, 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_out_ptr0 + x2, xmask) tmp2 = tl.load(in_ptr1 + x1, 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_convolution_3(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) 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), (16, 4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_8, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 7), (7, 1), torch.float32) get_raw_stream(0) triton_poi_fused_constant_pad_nd_0[grid(28)](primals_1, buf0, 28, XBLOCK=32, num_warps=1, num_stages=1) buf1 = extern_kernels.convolution(reinterpret_tensor(buf0, (1, 4, 7 ), (0, 7, 1), 0), primals_2, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf1, (1, 4, 4), (16, 4, 1)) buf2 = buf1 del buf1 buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused__prelu_kernel_convolution_1[grid(16)](buf2, primals_3, primals_4, buf3, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_3 buf4 = extern_kernels.convolution(reinterpret_tensor(buf3, (1, 4, 4 ), (0, 4, 1), 0), primals_5, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf4, (1, 4, 4), (16, 4, 1)) buf5 = reinterpret_tensor(buf4, (4, 4), (4, 1), 0) del buf4 triton_poi_fused_add_2[grid(16)](buf5, primals_1, primals_6, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_1 del primals_6 buf6 = extern_kernels.convolution(reinterpret_tensor(buf3, (1, 4, 4 ), (0, 4, 1), 0), primals_7, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf6, (1, 4, 4), (16, 4, 1)) buf7 = buf6 del buf6 triton_poi_fused_convolution_3[grid(16)](buf7, primals_8, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_8 return buf5, reinterpret_tensor(buf7, (4, 4), (4, 1), 0 ), primals_2, primals_4, primals_5, primals_7, reinterpret_tensor(buf0, (1, 4, 7), (28, 7, 1), 0), buf2, reinterpret_tensor(buf3, (1, 4, 4), (16, 4, 1), 0) def build_norm_layer(norm_type, param=None, num_feats=None): if norm_type == 'bnorm': return nn.BatchNorm1d(num_feats) elif norm_type == 'snorm': spectral_norm(param) return None elif norm_type == 'bsnorm': spectral_norm(param) return nn.BatchNorm1d(num_feats) elif norm_type == 'lnorm': return nn.LayerNorm(num_feats) elif norm_type == 'wnorm': weight_norm(param) return None elif norm_type == 'inorm': return nn.InstanceNorm1d(num_feats, affine=False) elif norm_type == 'affinorm': return nn.InstanceNorm1d(num_feats, affine=True) elif norm_type is None: return None else: raise TypeError('Unrecognized norm type: ', norm_type) class ResARModuleNew(nn.Module): def __init__(self, ninp, fmaps, res_fmaps, kwidth, dilation, bias=True, norm_type=None, act=None): super().__init__() self.dil_conv = nn.Conv1d(ninp, fmaps, kwidth, dilation=dilation, bias=bias) if act is not None: self.act = getattr(nn, act)() else: self.act = nn.PReLU(fmaps, init=0) self.dil_norm = build_norm_layer(norm_type, self.dil_conv, fmaps) self.kwidth = kwidth self.dilation = dilation self.conv_1x1_skip = nn.Conv1d(fmaps, ninp, 1, bias=bias) self.conv_1x1_skip_norm = build_norm_layer(norm_type, self. conv_1x1_skip, ninp) self.conv_1x1_res = nn.Conv1d(fmaps, res_fmaps, 1, bias=bias) self.conv_1x1_res_norm = build_norm_layer(norm_type, self. conv_1x1_res, res_fmaps) def forward_norm(self, x, norm_layer): if norm_layer is not None: return norm_layer(x) else: return x def forward(self, input_0): primals_2 = self.dil_conv.weight primals_3 = self.dil_conv.bias primals_4 = self.act.weight primals_5 = self.conv_1x1_skip.weight primals_6 = self.conv_1x1_skip.bias primals_7 = self.conv_1x1_res.weight primals_8 = self.conv_1x1_res.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8]) return output[0], output[1]
mansoorcheema/segan_pytorch
ResARModule
false
10,699
[ "MIT" ]
0
8f3b401e42cadfd1f8ad57a8ba0e89c16cc7ee65
https://github.com/mansoorcheema/segan_pytorch/tree/8f3b401e42cadfd1f8ad57a8ba0e89c16cc7ee65
ContractiveAutoencoder
import torch import torch.utils.data import torch.nn as nn class ContractiveAutoencoder(nn.Module): """ Simple contractive autoencoder with a single hidden layer. Constructor parameters: - num_inputs: Number of input features - num_hidden_layer_inputs: Number of input features for the single hidden layer """ def __init__(self, num_inputs, num_hidden_layer_inputs): super(ContractiveAutoencoder, self).__init__() self.num_inputs = num_inputs self.num_hidden_layer_inputs = num_hidden_layer_inputs self.fc1 = nn.Linear(num_inputs, num_hidden_layer_inputs, bias=False) self.fc2 = nn.Linear(num_hidden_layer_inputs, num_inputs, bias=False) self.relu = nn.ReLU() self.sigmoid = nn.Sigmoid() def encoder(self, x): h1 = self.relu(self.fc1(x.view(-1, self.num_inputs))) return h1 def decoder(self, z): h2 = self.sigmoid(self.fc2(z)) return h2 def forward(self, x): h1 = self.encoder(x) h2 = self.decoder(h1) return h1, h2 def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_inputs': 4, 'num_hidden_layer_inputs': 1}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.utils.data import torch.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, 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.full([1], 0, tl.int32) tmp2 = triton_helpers.maximum(tmp1, tmp0) tl.store(in_out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_poi_fused_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 = 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, (4, 1), (1, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 1), (1, 4), 0), out=buf0) del primals_2 buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_relu_0[grid(64)](buf1, 64, XBLOCK=64, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(buf1, reinterpret_tensor(primals_3, (1, 4), (1, 1 ), 0), out=buf2) buf3 = buf2 del buf2 triton_poi_fused_sigmoid_1[grid(256)](buf3, 256, XBLOCK=256, num_warps=4, num_stages=1) return buf1, buf3, reinterpret_tensor(primals_1, (64, 4), (4, 1), 0 ), buf1, buf3, primals_3 class ContractiveAutoencoderNew(nn.Module): """ Simple contractive autoencoder with a single hidden layer. Constructor parameters: - num_inputs: Number of input features - num_hidden_layer_inputs: Number of input features for the single hidden layer """ def __init__(self, num_inputs, num_hidden_layer_inputs): super(ContractiveAutoencoderNew, self).__init__() self.num_inputs = num_inputs self.num_hidden_layer_inputs = num_hidden_layer_inputs self.fc1 = nn.Linear(num_inputs, num_hidden_layer_inputs, bias=False) self.fc2 = nn.Linear(num_hidden_layer_inputs, num_inputs, bias=False) self.relu = nn.ReLU() self.sigmoid = nn.Sigmoid() def encoder(self, x): h1 = self.relu(self.fc1(x.view(-1, self.num_inputs))) return h1 def decoder(self, z): h2 = self.sigmoid(self.fc2(z)) return h2 def forward(self, input_0): primals_2 = self.fc1.weight primals_3 = self.fc2.weight primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0], output[1]
rocklegende/DL2020_R3
ContractiveAutoencoder
false
10,700
[ "MIT" ]
0
467ed759a9f9935d56863c79f71040e922d72829
https://github.com/rocklegende/DL2020_R3/tree/467ed759a9f9935d56863c79f71040e922d72829
Discrete
import torch import torch.nn as nn class Discrete(nn.Module): def __init__(self, num_outputs): super(Discrete, self).__init__() def forward(self, x): probs = nn.functional.softmax(x, dim=0) dist = torch.distributions.Categorical(probs=probs) return dist.entropy() def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_outputs': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn 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__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (64 + x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (128 + x0), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (192 + x0), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (64 + x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (128 + x0), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (192 + x0), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_div_sum_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): 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__softmax_0[grid(256)](arg0_1, buf0, 256, XBLOCK= 128, num_warps=4, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__softmax_1[grid(256)](buf0, buf1, 256, XBLOCK=256, num_warps=4, num_stages=1) buf2 = buf0 del buf0 triton_poi_fused_div_sum_2[grid(256)](buf1, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf1 return buf2, class DiscreteNew(nn.Module): def __init__(self, num_outputs): super(DiscreteNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
rsomani95/client
Discrete
false
10,701
[ "MIT" ]
0
772c6de325b30323397cfb98ab7e126910c5912b
https://github.com/rsomani95/client/tree/772c6de325b30323397cfb98ab7e126910c5912b
TransformerEncoderLayer
import torch from torch import nn import torch.nn.functional as F from typing import Optional class LearnedRelativePositionalEmbedding(nn.Module): """ This module learns relative positional embeddings up to a fixed maximum size. These are masked for decoder and unmasked for encoder self attention. By default the embeddings are added to keys, but could be added to values as well. Args: max_relative_pos (int): the maximum relative positions to compute embeddings for num_heads (int): number of attention heads embedding_dim (int): depth of embeddings unmasked (bool): if the attention is unmasked (for transformer encoder) heads_share_embeddings (bool): if heads share the same relative positional embeddings add_to_values (bool): compute embeddings to be added to values as well """ def __init__(self, max_relative_pos: 'int', num_heads: 'int', embedding_dim: 'int', unmasked: 'bool'=False, heads_share_embeddings: 'bool'=False, add_to_values: 'bool'=False): super().__init__() self.max_relative_pos = max_relative_pos self.num_heads = num_heads self.embedding_dim = embedding_dim self.unmasked = unmasked self.heads_share_embeddings = heads_share_embeddings self.add_to_values = add_to_values num_embeddings = (2 * max_relative_pos - 1 if unmasked else max_relative_pos) embedding_size = [num_embeddings, embedding_dim, 1 ] if heads_share_embeddings else [num_heads, num_embeddings, embedding_dim, 1] if add_to_values: embedding_size[-1] = 2 initial_stddev = embedding_dim ** -0.5 self.embeddings = nn.Parameter(torch.zeros(*embedding_size)) nn.init.normal_(self.embeddings, mean=0.0, std=initial_stddev) def forward(self, query, saved_state=None): """ Computes relative positional embeddings to be added to keys (and optionally values), multiplies the embeddings for keys with queries to create positional logits, returns the positional logits, along with embeddings for values (optionally) which could be added to values outside this module. Args: query (torch.Tensor): query tensor saved_state (dict): saved state from previous time step Shapes: query: `(length, batch_size*num_heads, embed_dim)` Returns: tuple(torch.Tensor): - positional logits - relative positional embeddings to be added to values """ if saved_state is not None and 'prev_key' in saved_state: assert not self.unmasked, 'This should only be for decoder attention' length = saved_state['prev_key'].shape[-2] + 1 decoder_step = True else: length = query.shape[0] decoder_step = False used_embeddings = self.get_embeddings_for_query(length) values_embeddings = used_embeddings[..., 1 ] if self.add_to_values else None positional_logits = self.calculate_positional_logits(query, used_embeddings[..., 0]) positional_logits = self.relative_to_absolute_indexing( positional_logits, decoder_step) return positional_logits, values_embeddings def get_embeddings_for_query(self, length): """ Extract the required embeddings. The maximum relative position between two time steps is `length` for masked case or `2*length - 1` for the unmasked case. If `length` is greater than `max_relative_pos`, we first pad the embeddings tensor with zero-embeddings, which represent embeddings when relative position is greater than `max_relative_pos`. In case `length` is less than `max_relative_pos`, we don't use the first `max_relative_pos - length embeddings`. Args: length (int): length of the query Returns: torch.Tensor: embeddings used by the query """ pad_length = max(length - self.max_relative_pos, 0) start_pos = max(self.max_relative_pos - length, 0) if self.unmasked: with torch.no_grad(): padded_embeddings = nn.functional.pad(self.embeddings, (0, 0, 0, 0, pad_length, pad_length)) used_embeddings = padded_embeddings.narrow(-3, start_pos, 2 * length - 1) else: with torch.no_grad(): padded_embeddings = nn.functional.pad(self.embeddings, (0, 0, 0, 0, pad_length, 0)) used_embeddings = padded_embeddings.narrow(-3, start_pos, length) return used_embeddings def calculate_positional_logits(self, query, relative_embeddings): """ Multiplies query with the relative positional embeddings to create relative positional logits Args: query (torch.Tensor): Input tensor representing queries relative_embeddings (torch.Tensor): relative embeddings compatible with query Shapes: query: `(length, batch_size*num_heads, embed_dim)` if heads share embeddings else `(length, batch_size, num_heads, embed_dim)` relative_embeddings: `(max_allowed_relative_positions, embed_dim)` if heads share embeddings else `(num_heads, max_allowed_relative_positions, embed_dim)` where `max_allowed_relative_positions` is `length` if masked else `2*length - 1` Returns: torch.Tensor: relative positional logits """ if self.heads_share_embeddings: positional_logits = torch.einsum('lbd,md->lbm', query, relative_embeddings) else: query = query.view(query.shape[0], -1, self.num_heads, self. embedding_dim) positional_logits = torch.einsum('lbhd,hmd->lbhm', query, relative_embeddings) positional_logits = positional_logits.contiguous().view( positional_logits.shape[0], -1, positional_logits.shape[-1]) length = query.size(0) if length > self.max_relative_pos: pad_length = length - self.max_relative_pos positional_logits[:, :, :pad_length] -= 100000000.0 if self.unmasked: positional_logits[:, :, -pad_length:] -= 100000000.0 return positional_logits def relative_to_absolute_indexing(self, x, decoder_step): """ Index tensor x (relative positional logits) in terms of absolute positions rather than relative positions. Last dimension of x represents relative position with respect to the first dimension, whereas returned tensor has both the first and last dimension indexed with absolute positions. Args: x (torch.Tensor): positional logits indexed by relative positions decoder_step (bool): is this is a single decoder step (during inference) Shapes: x: `(length, batch_size*num_heads, length)` for masked case or `(length, batch_size*num_heads, 2*length - 1)` for unmasked Returns: torch.Tensor: positional logits represented using absolute positions """ length, bsz_heads, _ = x.shape if decoder_step: return x.contiguous().view(bsz_heads, 1, -1) if self.unmasked: x = nn.functional.pad(x, (0, 1)) x = x.transpose(0, 1) x = x.contiguous().view(bsz_heads, length * 2 * length) x = nn.functional.pad(x, (0, length - 1)) x = x.view(bsz_heads, length + 1, 2 * length - 1) return x[:, :length, length - 1:] else: x = nn.functional.pad(x, (1, 0)) x = x.transpose(0, 1) x = x.contiguous().view(bsz_heads, length + 1, length) return x[:, 1:, :] class MultiHeadAttention(nn.Module): def __init__(self, d_model=256, n_head=4, dropout=0.1, relative_positional=True, relative_positional_distance=100): super().__init__() self.d_model = d_model self.n_head = n_head d_qkv = d_model // n_head assert d_qkv * n_head == d_model, 'd_model must be divisible by n_head' self.d_qkv = d_qkv self.w_q = nn.Parameter(torch.Tensor(n_head, d_model, d_qkv)) self.w_k = nn.Parameter(torch.Tensor(n_head, d_model, d_qkv)) self.w_v = nn.Parameter(torch.Tensor(n_head, d_model, d_qkv)) self.w_o = nn.Parameter(torch.Tensor(n_head, d_qkv, d_model)) nn.init.xavier_normal_(self.w_q) nn.init.xavier_normal_(self.w_k) nn.init.xavier_normal_(self.w_v) nn.init.xavier_normal_(self.w_o) self.dropout = nn.Dropout(dropout) if relative_positional: self.relative_positional = LearnedRelativePositionalEmbedding( relative_positional_distance, n_head, d_qkv, True) else: self.relative_positional = None def forward(self, x): """Runs the multi-head self-attention layer. Args: x: the input to the layer, a tensor of shape [length, batch_size, d_model] Returns: A single tensor containing the output from this layer """ q = torch.einsum('tbf,hfa->bhta', x, self.w_q) k = torch.einsum('tbf,hfa->bhta', x, self.w_k) v = torch.einsum('tbf,hfa->bhta', x, self.w_v) logits = torch.einsum('bhqa,bhka->bhqk', q, k) / self.d_qkv ** 0.5 if self.relative_positional is not None: q_pos = q.permute(2, 0, 1, 3) l, b, h, d = q_pos.size() position_logits, _ = self.relative_positional(q_pos.reshape(l, b * h, d)) logits = logits + position_logits.view(b, h, l, l) probs = F.softmax(logits, dim=-1) probs = self.dropout(probs) o = torch.einsum('bhqk,bhka->bhqa', probs, v) out = torch.einsum('bhta,haf->tbf', o, self.w_o) return out class TransformerEncoderLayer(nn.Module): """TransformerEncoderLayer is made up of self-attn and feedforward network. This standard encoder layer is based on the paper "Attention Is All You Need". Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz Kaiser, and Illia Polosukhin. 2017. Attention is all you need. In Advances in Neural Information Processing Systems, pages 6000-6010. Users may modify or implement in a different way during application. Args: d_model: the number of expected features in the input (required). nhead: the number of heads in the multiheadattention models (required). dim_feedforward: the dimension of the feedforward network model (default=2048). dropout: the dropout value (default=0.1). Examples:: >>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8) >>> src = torch.rand(10, 32, 512) >>> out = encoder_layer(src) """ def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1, relative_positional=True, relative_positional_distance=100): super(TransformerEncoderLayer, self).__init__() self.self_attn = MultiHeadAttention(d_model, nhead, dropout=dropout, relative_positional=relative_positional, relative_positional_distance=relative_positional_distance) self.linear1 = nn.Linear(d_model, dim_feedforward) self.dropout = nn.Dropout(dropout) self.linear2 = nn.Linear(dim_feedforward, d_model) self.norm1 = nn.LayerNorm(d_model) self.norm2 = nn.LayerNorm(d_model) self.dropout1 = nn.Dropout(dropout) self.dropout2 = nn.Dropout(dropout) self.activation = nn.ReLU() def forward(self, src: 'torch.Tensor', src_mask: 'Optional[torch.Tensor]'=None, src_key_padding_mask: 'Optional[torch.Tensor]'=None) ->torch.Tensor: """Pass the input through the encoder layer. Args: src: the sequence to the encoder layer (required). src_mask: the mask for the src sequence (optional). src_key_padding_mask: the mask for the src keys per batch (optional). Shape: see the docs in Transformer class. """ src2 = self.self_attn(src) src = src + self.dropout1(src2) src = self.norm1(src) src2 = self.linear2(self.dropout(self.activation(self.linear1(src)))) src = src + self.dropout2(src2) src = self.norm2(src) return src def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'d_model': 4, 'nhead': 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 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, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 % 4 x2 = xindex // 16 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1), xmask) tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_poi_fused__softmax_add_div_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 4 x2 = xindex // 16 x1 = xindex // 4 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + (x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp20 = tl.load(in_ptr1 + (4 + x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp37 = tl.load(in_ptr1 + (8 + x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp54 = tl.load(in_ptr1 + (12 + x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tmp0 * tmp1 tmp3 = 1.0 tmp4 = tmp2 * tmp3 tmp5 = 3 + 7 * x1 tmp6 = tl.full([1], 32, tl.int64) tmp7 = tmp5 < tmp6 tmp8 = (3 + 7 * x1) % 8 tmp9 = tl.full([1], 7, tl.int64) tmp10 = tmp8 < tmp9 tmp11 = tmp10 & tmp7 tmp12 = tl.load(in_ptr0 + (x0 + 4 * ((3 + 7 * x1) // 8) + 16 * x2), tmp11 & xmask, other=0.0) tmp13 = tl.load(in_ptr2 + (96 + 199 * x0 + (3 + 7 * x1) % 8), tmp11 & xmask, eviction_policy='evict_last', other=0.0) tmp14 = tmp12 * tmp13 tmp15 = tl.full(tmp14.shape, 0.0, tmp14.dtype) tmp16 = tl.where(tmp11, tmp14, tmp15) tmp17 = tl.full(tmp16.shape, 0.0, tmp16.dtype) tmp18 = tl.where(tmp7, tmp16, tmp17) tmp19 = tmp4 + tmp18 tmp21 = tmp0 * tmp20 tmp22 = tmp21 * tmp3 tmp23 = 4 + 7 * x1 tmp24 = tmp23 < tmp6 tmp25 = (4 + 7 * x1) % 8 tmp26 = tmp25 < tmp9 tmp27 = tmp26 & tmp24 tmp28 = tl.load(in_ptr0 + (x0 + 4 * ((4 + 7 * x1) // 8) + 16 * x2), tmp27 & xmask, other=0.0) tmp29 = tl.load(in_ptr2 + (96 + 199 * x0 + (4 + 7 * x1) % 8), tmp27 & xmask, eviction_policy='evict_last', other=0.0) tmp30 = tmp28 * tmp29 tmp31 = tl.full(tmp30.shape, 0.0, tmp30.dtype) tmp32 = tl.where(tmp27, tmp30, tmp31) tmp33 = tl.full(tmp32.shape, 0.0, tmp32.dtype) tmp34 = tl.where(tmp24, tmp32, tmp33) tmp35 = tmp22 + tmp34 tmp36 = triton_helpers.maximum(tmp19, tmp35) tmp38 = tmp0 * tmp37 tmp39 = tmp38 * tmp3 tmp40 = 5 + 7 * x1 tmp41 = tmp40 < tmp6 tmp42 = (5 + 7 * x1) % 8 tmp43 = tmp42 < tmp9 tmp44 = tmp43 & tmp41 tmp45 = tl.load(in_ptr0 + (x0 + 4 * ((5 + 7 * x1) // 8) + 16 * x2), tmp44 & xmask, other=0.0) tmp46 = tl.load(in_ptr2 + (96 + 199 * x0 + (5 + 7 * x1) % 8), tmp44 & xmask, eviction_policy='evict_last', other=0.0) tmp47 = tmp45 * tmp46 tmp48 = tl.full(tmp47.shape, 0.0, tmp47.dtype) tmp49 = tl.where(tmp44, tmp47, tmp48) tmp50 = tl.full(tmp49.shape, 0.0, tmp49.dtype) tmp51 = tl.where(tmp41, tmp49, tmp50) tmp52 = tmp39 + tmp51 tmp53 = triton_helpers.maximum(tmp36, tmp52) tmp55 = tmp0 * tmp54 tmp56 = tmp55 * tmp3 tmp57 = 6 + 7 * x1 tmp58 = tmp57 < tmp6 tmp59 = (6 + 7 * x1) % 8 tmp60 = tmp59 < tmp9 tmp61 = tmp60 & tmp58 tmp62 = tl.load(in_ptr0 + (x0 + 4 * ((6 + 7 * x1) // 8) + 16 * x2), tmp61 & xmask, other=0.0) tmp63 = tl.load(in_ptr2 + (96 + 199 * x0 + (6 + 7 * x1) % 8), tmp61 & xmask, eviction_policy='evict_last', other=0.0) tmp64 = tmp62 * tmp63 tmp65 = tl.full(tmp64.shape, 0.0, tmp64.dtype) tmp66 = tl.where(tmp61, tmp64, tmp65) tmp67 = tl.full(tmp66.shape, 0.0, tmp66.dtype) tmp68 = tl.where(tmp58, tmp66, tmp67) tmp69 = tmp56 + tmp68 tmp70 = triton_helpers.maximum(tmp53, tmp69) tl.store(out_ptr0 + x3, tmp70, xmask) @triton.jit def triton_poi_fused__softmax_add_div_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x4 = xindex // 4 x0 = xindex % 4 x1 = xindex // 4 % 4 x3 = xindex // 64 x2 = xindex // 16 % 4 x5 = xindex tmp0 = tl.load(in_ptr0 + x4, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + (x1 + 4 * x0 + 16 * x3), xmask, eviction_policy='evict_last') tmp20 = tl.load(in_ptr3 + x4, xmask, eviction_policy='evict_last') tmp2 = tmp0 * tmp1 tmp3 = 1.0 tmp4 = tmp2 * tmp3 tmp5 = 3 + x0 + 7 * x2 tmp6 = tl.full([1], 32, tl.int64) tmp7 = tmp5 < tmp6 tmp8 = (3 + x0 + 7 * x2) % 8 tmp9 = tl.full([1], 7, tl.int64) tmp10 = tmp8 < tmp9 tmp11 = tmp10 & tmp7 tmp12 = tl.load(in_ptr0 + (x1 + 4 * ((3 + x0 + 7 * x2) // 8) + 16 * x3), tmp11 & xmask, eviction_policy='evict_last', other=0.0) tmp13 = tl.load(in_ptr2 + (96 + 199 * x1 + (3 + x0 + 7 * x2) % 8), tmp11 & xmask, eviction_policy='evict_last', other=0.0) tmp14 = tmp12 * tmp13 tmp15 = tl.full(tmp14.shape, 0.0, tmp14.dtype) tmp16 = tl.where(tmp11, tmp14, tmp15) tmp17 = tl.full(tmp16.shape, 0.0, tmp16.dtype) tmp18 = tl.where(tmp7, tmp16, tmp17) tmp19 = tmp4 + tmp18 tmp21 = tmp19 - tmp20 tmp22 = tl_math.exp(tmp21) tl.store(out_ptr0 + x5, tmp22, 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 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_ptr0 + 4 * x5, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x5), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x5), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x5), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + (x0 + 4 * x2 + 16 * x1 + 64 * x3), tmp8, 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_clone_5(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 4 xnumel = 16 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x1 = xindex y0 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x1), xmask & ymask, eviction_policy= 'evict_last') tl.store(out_ptr0 + (x1 + 16 * y0), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_add_6(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_out_ptr0 + x0, xmask) tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_poi_fused_native_layer_norm_7(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_8(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_9(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_10(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_out_ptr0 + x2, xmask) tmp2 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp4 = tmp0 + tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) 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, 1), (4, 1, 1)) assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_3, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_4, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_5, (4, 199, 1, 1), (199, 1, 1, 1)) assert_size_stride(primals_6, (4, 1, 4), (4, 4, 1)) assert_size_stride(primals_7, (4,), (1,)) assert_size_stride(primals_8, (4,), (1,)) assert_size_stride(primals_9, (2048, 4), (4, 1)) assert_size_stride(primals_10, (2048,), (1,)) assert_size_stride(primals_11, (4, 2048), (2048, 1)) assert_size_stride(primals_12, (4,), (1,)) assert_size_stride(primals_13, (4,), (1,)) assert_size_stride(primals_14, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 1, 1), (16, 4, 1, 1, 1), torch. float32) get_raw_stream(0) triton_poi_fused_clone_0[grid(64)](primals_2, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) buf1 = empty_strided_cuda((1, 16, 4), (64, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf0, (1, 16, 4), (0, 4, 1), 0), reinterpret_tensor(primals_1, (1, 4, 4), (0, 1, 4), 0), out =buf1) del primals_1 buf2 = empty_strided_cuda((1, 16, 4), (64, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf0, (1, 16, 4), (0, 4, 1), 0), reinterpret_tensor(primals_3, (1, 4, 4), (0, 1, 4), 0), out =buf2) del primals_3 buf3 = empty_strided_cuda((1, 16, 4), (64, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf0, (1, 16, 4), (0, 4, 1), 0), reinterpret_tensor(primals_4, (1, 4, 4), (0, 1, 4), 0), out =buf3) del primals_4 buf4 = empty_strided_cuda((4, 4, 4, 1), (16, 1, 4, 64), torch.float32) triton_poi_fused__softmax_add_div_1[grid(64)](buf1, buf2, primals_5, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 4, 16, 1), torch.float32) triton_poi_fused__softmax_add_div_2[grid(256)](buf1, buf2, primals_5, buf4, buf5, 256, XBLOCK=128, num_warps=4, num_stages=1) buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__softmax_3[grid(256)](buf5, buf6, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf5 buf7 = reinterpret_tensor(buf4, (4, 4, 4, 1, 1), (16, 4, 1, 1, 1), 0) del buf4 triton_poi_fused_clone_4[grid(16, 4)](buf3, buf7, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf8 = reinterpret_tensor(buf3, (16, 4, 1), (4, 1, 1), 0) del buf3 extern_kernels.bmm(reinterpret_tensor(buf6, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf7, (16, 4, 1), (4, 1, 0), 0), out=buf8) buf9 = empty_strided_cuda((4, 4, 4, 1, 1), (16, 4, 1, 1, 1), torch. float32) triton_poi_fused_clone_5[grid(4, 16)](buf8, buf9, 4, 16, XBLOCK=16, YBLOCK=4, num_warps=1, num_stages=1) buf10 = reinterpret_tensor(buf8, (1, 16, 4), (64, 4, 1), 0) del buf8 extern_kernels.bmm(reinterpret_tensor(buf9, (1, 16, 4), (0, 4, 1), 0), reinterpret_tensor(primals_6, (1, 4, 4), (16, 4, 1), 0), out=buf10) buf11 = reinterpret_tensor(buf10, (4, 4, 4), (16, 4, 1), 0) del buf10 triton_poi_fused_add_6[grid(64)](buf11, primals_2, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_2 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_7[grid(16)](buf11, 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_8[grid(64)](buf11, buf12, buf13, primals_7, primals_8, buf14, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_8 buf15 = empty_strided_cuda((16, 2048), (2048, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf14, (16, 4), (4, 1), 0), reinterpret_tensor(primals_9, (4, 2048), (1, 4), 0), out=buf15) buf16 = reinterpret_tensor(buf15, (4, 4, 2048), (8192, 2048, 1), 0) del buf15 buf22 = empty_strided_cuda((4, 4, 2048), (8192, 2048, 1), torch.bool) triton_poi_fused_relu_threshold_backward_9[grid(32768)](buf16, primals_10, buf22, 32768, XBLOCK=256, num_warps=4, num_stages=1) del primals_10 buf17 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf16, (16, 2048), (2048, 1), 0), reinterpret_tensor(primals_11, (2048, 4), (1, 2048), 0), out=buf17) buf18 = reinterpret_tensor(buf17, (4, 4, 4), (16, 4, 1), 0) del buf17 triton_poi_fused_add_10[grid(64)](buf18, buf14, primals_12, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_12 buf19 = buf13 del buf13 buf20 = buf12 del buf12 triton_poi_fused_native_layer_norm_7[grid(16)](buf18, buf19, buf20, 16, XBLOCK=16, num_warps=1, num_stages=1) buf21 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_8[grid(64)](buf18, buf19, buf20, primals_13, primals_14, buf21, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf19 del buf20 del primals_14 return buf21, primals_7, primals_13, buf1, buf2, reinterpret_tensor( primals_5, (1, 1, 4, 7, 1), (1, 1, 199, 1, 1), 96 ), buf6, buf11, reinterpret_tensor(buf14, (16, 4), (4, 1), 0 ), reinterpret_tensor(buf16, (16, 2048), (2048, 1), 0 ), buf18, primals_11, buf22, primals_9, reinterpret_tensor(buf9, (1, 4, 16), (64, 1, 4), 0), reinterpret_tensor(primals_6, (1, 4, 4), ( 16, 1, 4), 0), reinterpret_tensor(buf7, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf0, (1, 4, 16), (64, 1, 4), 0) class LearnedRelativePositionalEmbedding(nn.Module): """ This module learns relative positional embeddings up to a fixed maximum size. These are masked for decoder and unmasked for encoder self attention. By default the embeddings are added to keys, but could be added to values as well. Args: max_relative_pos (int): the maximum relative positions to compute embeddings for num_heads (int): number of attention heads embedding_dim (int): depth of embeddings unmasked (bool): if the attention is unmasked (for transformer encoder) heads_share_embeddings (bool): if heads share the same relative positional embeddings add_to_values (bool): compute embeddings to be added to values as well """ def __init__(self, max_relative_pos: 'int', num_heads: 'int', embedding_dim: 'int', unmasked: 'bool'=False, heads_share_embeddings: 'bool'=False, add_to_values: 'bool'=False): super().__init__() self.max_relative_pos = max_relative_pos self.num_heads = num_heads self.embedding_dim = embedding_dim self.unmasked = unmasked self.heads_share_embeddings = heads_share_embeddings self.add_to_values = add_to_values num_embeddings = (2 * max_relative_pos - 1 if unmasked else max_relative_pos) embedding_size = [num_embeddings, embedding_dim, 1 ] if heads_share_embeddings else [num_heads, num_embeddings, embedding_dim, 1] if add_to_values: embedding_size[-1] = 2 initial_stddev = embedding_dim ** -0.5 self.embeddings = nn.Parameter(torch.zeros(*embedding_size)) nn.init.normal_(self.embeddings, mean=0.0, std=initial_stddev) def forward(self, query, saved_state=None): """ Computes relative positional embeddings to be added to keys (and optionally values), multiplies the embeddings for keys with queries to create positional logits, returns the positional logits, along with embeddings for values (optionally) which could be added to values outside this module. Args: query (torch.Tensor): query tensor saved_state (dict): saved state from previous time step Shapes: query: `(length, batch_size*num_heads, embed_dim)` Returns: tuple(torch.Tensor): - positional logits - relative positional embeddings to be added to values """ if saved_state is not None and 'prev_key' in saved_state: assert not self.unmasked, 'This should only be for decoder attention' length = saved_state['prev_key'].shape[-2] + 1 decoder_step = True else: length = query.shape[0] decoder_step = False used_embeddings = self.get_embeddings_for_query(length) values_embeddings = used_embeddings[..., 1 ] if self.add_to_values else None positional_logits = self.calculate_positional_logits(query, used_embeddings[..., 0]) positional_logits = self.relative_to_absolute_indexing( positional_logits, decoder_step) return positional_logits, values_embeddings def get_embeddings_for_query(self, length): """ Extract the required embeddings. The maximum relative position between two time steps is `length` for masked case or `2*length - 1` for the unmasked case. If `length` is greater than `max_relative_pos`, we first pad the embeddings tensor with zero-embeddings, which represent embeddings when relative position is greater than `max_relative_pos`. In case `length` is less than `max_relative_pos`, we don't use the first `max_relative_pos - length embeddings`. Args: length (int): length of the query Returns: torch.Tensor: embeddings used by the query """ pad_length = max(length - self.max_relative_pos, 0) start_pos = max(self.max_relative_pos - length, 0) if self.unmasked: with torch.no_grad(): padded_embeddings = nn.functional.pad(self.embeddings, (0, 0, 0, 0, pad_length, pad_length)) used_embeddings = padded_embeddings.narrow(-3, start_pos, 2 * length - 1) else: with torch.no_grad(): padded_embeddings = nn.functional.pad(self.embeddings, (0, 0, 0, 0, pad_length, 0)) used_embeddings = padded_embeddings.narrow(-3, start_pos, length) return used_embeddings def calculate_positional_logits(self, query, relative_embeddings): """ Multiplies query with the relative positional embeddings to create relative positional logits Args: query (torch.Tensor): Input tensor representing queries relative_embeddings (torch.Tensor): relative embeddings compatible with query Shapes: query: `(length, batch_size*num_heads, embed_dim)` if heads share embeddings else `(length, batch_size, num_heads, embed_dim)` relative_embeddings: `(max_allowed_relative_positions, embed_dim)` if heads share embeddings else `(num_heads, max_allowed_relative_positions, embed_dim)` where `max_allowed_relative_positions` is `length` if masked else `2*length - 1` Returns: torch.Tensor: relative positional logits """ if self.heads_share_embeddings: positional_logits = torch.einsum('lbd,md->lbm', query, relative_embeddings) else: query = query.view(query.shape[0], -1, self.num_heads, self. embedding_dim) positional_logits = torch.einsum('lbhd,hmd->lbhm', query, relative_embeddings) positional_logits = positional_logits.contiguous().view( positional_logits.shape[0], -1, positional_logits.shape[-1]) length = query.size(0) if length > self.max_relative_pos: pad_length = length - self.max_relative_pos positional_logits[:, :, :pad_length] -= 100000000.0 if self.unmasked: positional_logits[:, :, -pad_length:] -= 100000000.0 return positional_logits def relative_to_absolute_indexing(self, x, decoder_step): """ Index tensor x (relative positional logits) in terms of absolute positions rather than relative positions. Last dimension of x represents relative position with respect to the first dimension, whereas returned tensor has both the first and last dimension indexed with absolute positions. Args: x (torch.Tensor): positional logits indexed by relative positions decoder_step (bool): is this is a single decoder step (during inference) Shapes: x: `(length, batch_size*num_heads, length)` for masked case or `(length, batch_size*num_heads, 2*length - 1)` for unmasked Returns: torch.Tensor: positional logits represented using absolute positions """ length, bsz_heads, _ = x.shape if decoder_step: return x.contiguous().view(bsz_heads, 1, -1) if self.unmasked: x = nn.functional.pad(x, (0, 1)) x = x.transpose(0, 1) x = x.contiguous().view(bsz_heads, length * 2 * length) x = nn.functional.pad(x, (0, length - 1)) x = x.view(bsz_heads, length + 1, 2 * length - 1) return x[:, :length, length - 1:] else: x = nn.functional.pad(x, (1, 0)) x = x.transpose(0, 1) x = x.contiguous().view(bsz_heads, length + 1, length) return x[:, 1:, :] class MultiHeadAttention(nn.Module): def __init__(self, d_model=256, n_head=4, dropout=0.1, relative_positional=True, relative_positional_distance=100): super().__init__() self.d_model = d_model self.n_head = n_head d_qkv = d_model // n_head assert d_qkv * n_head == d_model, 'd_model must be divisible by n_head' self.d_qkv = d_qkv self.w_q = nn.Parameter(torch.Tensor(n_head, d_model, d_qkv)) self.w_k = nn.Parameter(torch.Tensor(n_head, d_model, d_qkv)) self.w_v = nn.Parameter(torch.Tensor(n_head, d_model, d_qkv)) self.w_o = nn.Parameter(torch.Tensor(n_head, d_qkv, d_model)) nn.init.xavier_normal_(self.w_q) nn.init.xavier_normal_(self.w_k) nn.init.xavier_normal_(self.w_v) nn.init.xavier_normal_(self.w_o) self.dropout = nn.Dropout(dropout) if relative_positional: self.relative_positional = LearnedRelativePositionalEmbedding( relative_positional_distance, n_head, d_qkv, True) else: self.relative_positional = None def forward(self, x): """Runs the multi-head self-attention layer. Args: x: the input to the layer, a tensor of shape [length, batch_size, d_model] Returns: A single tensor containing the output from this layer """ q = torch.einsum('tbf,hfa->bhta', x, self.w_q) k = torch.einsum('tbf,hfa->bhta', x, self.w_k) v = torch.einsum('tbf,hfa->bhta', x, self.w_v) logits = torch.einsum('bhqa,bhka->bhqk', q, k) / self.d_qkv ** 0.5 if self.relative_positional is not None: q_pos = q.permute(2, 0, 1, 3) l, b, h, d = q_pos.size() position_logits, _ = self.relative_positional(q_pos.reshape(l, b * h, d)) logits = logits + position_logits.view(b, h, l, l) probs = F.softmax(logits, dim=-1) probs = self.dropout(probs) o = torch.einsum('bhqk,bhka->bhqa', probs, v) out = torch.einsum('bhta,haf->tbf', o, self.w_o) return out class TransformerEncoderLayerNew(nn.Module): """TransformerEncoderLayer is made up of self-attn and feedforward network. This standard encoder layer is based on the paper "Attention Is All You Need". Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz Kaiser, and Illia Polosukhin. 2017. Attention is all you need. In Advances in Neural Information Processing Systems, pages 6000-6010. Users may modify or implement in a different way during application. Args: d_model: the number of expected features in the input (required). nhead: the number of heads in the multiheadattention models (required). dim_feedforward: the dimension of the feedforward network model (default=2048). dropout: the dropout value (default=0.1). Examples:: >>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8) >>> src = torch.rand(10, 32, 512) >>> out = encoder_layer(src) """ def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1, relative_positional=True, relative_positional_distance=100): super(TransformerEncoderLayerNew, self).__init__() self.self_attn = MultiHeadAttention(d_model, nhead, dropout=dropout, relative_positional=relative_positional, relative_positional_distance=relative_positional_distance) self.linear1 = nn.Linear(d_model, dim_feedforward) self.dropout = nn.Dropout(dropout) self.linear2 = nn.Linear(dim_feedforward, d_model) self.norm1 = nn.LayerNorm(d_model) self.norm2 = nn.LayerNorm(d_model) self.dropout1 = nn.Dropout(dropout) self.dropout2 = nn.Dropout(dropout) self.activation = nn.ReLU() def forward(self, input_0): primals_1 = self.self_attn.w_q primals_3 = self.self_attn.w_k primals_4 = self.self_attn.w_v primals_6 = self.self_attn.w_o primals_5 = self.self_attn.relative_positional.embeddings primals_9 = self.linear1.weight primals_10 = self.linear1.bias primals_11 = self.linear2.weight primals_7 = self.linear2.bias primals_8 = self.norm1.weight primals_12 = self.norm1.bias primals_13 = self.norm2.weight primals_14 = self.norm2.bias primals_2 = 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]) return output[0]
neuroidss/silent_speech
TransformerEncoderLayer
false
10,702
[ "MIT" ]
0
4a6d8e944007071de02261bfd7f8ecedd9a06ccd
https://github.com/neuroidss/silent_speech/tree/4a6d8e944007071de02261bfd7f8ecedd9a06ccd
LinearLR
import torch import torch.nn as nn class LinearLR(nn.Module): """[u * v + res] version of torch.nn.Linear""" def __init__(self, in_features, out_features, rank_ratio=0.25, bias= True, device=None, dtype=None): super().__init__() sliced_rank = int(min(in_features, out_features) * rank_ratio) self.u = nn.Linear(in_features, sliced_rank, bias=False, device= device, dtype=dtype) self.v = nn.Linear(sliced_rank, out_features, bias=bias, device= device, dtype=dtype) self.res = nn.Linear(in_features, out_features, bias=False, device= device, dtype=dtype) def freeze(self): for param in self.res.parameters(): param.requires_grad = False def unfreeze(self): for param in self.res.parameters(): param.requires_grad = True def forward(self, input): return self.v(self.u(input)) + self.res(input) 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 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, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x2, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (1, 4), (4, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4, 1), (1, 1)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 1), (1, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_2, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 1), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(buf0, reinterpret_tensor(primals_3, (1, 4), (1, 1 ), 0), out=buf1) buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_2, (64, 4), (4, 1), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf2) del primals_5 buf3 = reinterpret_tensor(buf1, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf1 get_raw_stream(0) triton_poi_fused_add_0[grid(256)](buf3, primals_4, buf2, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf2 del primals_4 return buf3, reinterpret_tensor(primals_2, (64, 4), (4, 1), 0 ), buf0, primals_3 class LinearLRNew(nn.Module): """[u * v + res] version of torch.nn.Linear""" def __init__(self, in_features, out_features, rank_ratio=0.25, bias= True, device=None, dtype=None): super().__init__() sliced_rank = int(min(in_features, out_features) * rank_ratio) self.u = nn.Linear(in_features, sliced_rank, bias=False, device= device, dtype=dtype) self.v = nn.Linear(sliced_rank, out_features, bias=bias, device= device, dtype=dtype) self.res = nn.Linear(in_features, out_features, bias=False, device= device, dtype=dtype) def freeze(self): for param in self.res.parameters(): param.requires_grad = False def unfreeze(self): for param in self.res.parameters(): param.requires_grad = True def forward(self, input_0): primals_1 = self.u.weight primals_3 = self.v.weight primals_4 = self.v.bias primals_5 = self.res.weight primals_2 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
razered/alternate
LinearLR
false
10,703
[ "MIT" ]
0
18e876aadc76d5f675cf940549b4bcd6e80a0288
https://github.com/razered/alternate/tree/18e876aadc76d5f675cf940549b4bcd6e80a0288
ProposalNet
import torch from torch import nn import torch.utils.data class ProposalNet(nn.Module): def __init__(self): super(ProposalNet, self).__init__() self.down1 = nn.Conv2d(2048, 128, 3, 1, 1) self.down2 = nn.Conv2d(128, 128, 3, 2, 1) self.down3 = nn.Conv2d(128, 128, 3, 2, 1) self.ReLU = nn.ReLU() self.tidy1 = nn.Conv2d(128, 6, 1, 1, 0) self.tidy2 = nn.Conv2d(128, 6, 1, 1, 0) self.tidy3 = nn.Conv2d(128, 9, 1, 1, 0) def forward(self, x): batch_size = x.size(0) d1 = self.ReLU(self.down1(x)) d2 = self.ReLU(self.down2(d1)) d3 = self.ReLU(self.down3(d2)) t1 = self.tidy1(d1).view(batch_size, -1) t2 = self.tidy2(d2).view(batch_size, -1) t3 = self.tidy3(d3).view(batch_size, -1) return torch.cat((t1, t2, t3), dim=1) def get_inputs(): return [torch.rand([4, 2048, 64, 64])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn 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_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 % 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_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 // 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_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 // 256 % 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_cat_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 132096 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 33024 x1 = xindex // 33024 x2 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 24576, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (24576 * x1 + x0 % 24576), tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp6 = tl.load(in_ptr1 + x0 // 4096 % 6, tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp7 = tmp5 + tmp6 tmp8 = tl.full(tmp7.shape, 0.0, tmp7.dtype) tmp9 = tl.where(tmp4, tmp7, tmp8) tmp10 = tmp0 >= tmp3 tmp11 = tl.full([1], 30720, tl.int64) tmp12 = tmp0 < tmp11 tmp13 = tmp10 & tmp12 tmp14 = tl.load(in_ptr2 + (6144 * x1 + (-24576 + x0) % 6144), tmp13 & xmask, eviction_policy='evict_last', other=0.0) tmp15 = tl.load(in_ptr3 + (-24576 + x0) // 1024 % 6, tmp13 & xmask, eviction_policy='evict_last', other=0.0) tmp16 = tmp14 + tmp15 tmp17 = tl.full(tmp16.shape, 0.0, tmp16.dtype) tmp18 = tl.where(tmp13, tmp16, tmp17) tmp19 = tmp0 >= tmp11 tl.full([1], 33024, tl.int64) tmp22 = tl.load(in_ptr4 + (2304 * x1 + (-30720 + x0) % 2304), tmp19 & xmask, eviction_policy='evict_last', other=0.0) tmp23 = tl.load(in_ptr5 + (-30720 + x0) // 256 % 9, tmp19 & xmask, eviction_policy='evict_last', other=0.0) tmp24 = tmp22 + tmp23 tmp25 = tl.full(tmp24.shape, 0.0, tmp24.dtype) tmp26 = tl.where(tmp19, tmp24, tmp25) tmp27 = tl.where(tmp13, tmp18, tmp26) tmp28 = tl.where(tmp4, tmp9, tmp27) tl.store(out_ptr0 + x2, tmp28, 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, 2048, 64, 64), (8388608, 4096, 64, 1)) assert_size_stride(primals_2, (128, 2048, 3, 3), (18432, 9, 3, 1)) assert_size_stride(primals_3, (128,), (1,)) assert_size_stride(primals_4, (128, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_5, (128,), (1,)) assert_size_stride(primals_6, (128, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_7, (128,), (1,)) assert_size_stride(primals_8, (6, 128, 1, 1), (128, 1, 1, 1)) assert_size_stride(primals_9, (6,), (1,)) assert_size_stride(primals_10, (6, 128, 1, 1), (128, 1, 1, 1)) assert_size_stride(primals_11, (6,), (1,)) assert_size_stride(primals_12, (9, 128, 1, 1), (128, 1, 1, 1)) assert_size_stride(primals_13, (9,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 128, 64, 64), (524288, 4096, 64, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(2097152)](buf1, primals_3, 2097152, XBLOCK=1024, num_warps=4, num_stages=1) del primals_3 buf2 = extern_kernels.convolution(buf1, primals_4, stride=(2, 2), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 128, 32, 32), (131072, 1024, 32, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_relu_1[grid(524288)](buf3, primals_5, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_5 buf4 = extern_kernels.convolution(buf3, primals_6, stride=(2, 2), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 128, 16, 16), (32768, 256, 16, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_relu_2[grid(131072)](buf5, primals_7, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del primals_7 buf6 = extern_kernels.convolution(buf1, primals_8, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf6, (4, 6, 64, 64), (24576, 4096, 64, 1)) buf7 = extern_kernels.convolution(buf3, primals_10, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf7, (4, 6, 32, 32), (6144, 1024, 32, 1)) buf8 = extern_kernels.convolution(buf5, primals_12, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 9, 16, 16), (2304, 256, 16, 1)) buf9 = empty_strided_cuda((4, 33024), (33024, 1), torch.float32) triton_poi_fused_cat_3[grid(132096)](buf6, primals_9, buf7, primals_11, buf8, primals_13, buf9, 132096, XBLOCK=512, num_warps=8, num_stages=1) del buf6 del buf7 del buf8 del primals_11 del primals_13 del primals_9 return (buf9, primals_1, primals_2, primals_4, primals_6, primals_8, primals_10, primals_12, buf1, buf3, buf5) class ProposalNetNew(nn.Module): def __init__(self): super(ProposalNetNew, self).__init__() self.down1 = nn.Conv2d(2048, 128, 3, 1, 1) self.down2 = nn.Conv2d(128, 128, 3, 2, 1) self.down3 = nn.Conv2d(128, 128, 3, 2, 1) self.ReLU = nn.ReLU() self.tidy1 = nn.Conv2d(128, 6, 1, 1, 0) self.tidy2 = nn.Conv2d(128, 6, 1, 1, 0) self.tidy3 = nn.Conv2d(128, 9, 1, 1, 0) def forward(self, input_0): primals_2 = self.down1.weight primals_3 = self.down1.bias primals_4 = self.down2.weight primals_5 = self.down2.bias primals_6 = self.down3.weight primals_7 = self.down3.bias primals_8 = self.tidy1.weight primals_9 = self.tidy1.bias primals_10 = self.tidy2.weight primals_11 = self.tidy2.bias primals_12 = self.tidy3.weight primals_13 = self.tidy3.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13]) return output[0]
mobulan/NTS-Net
ProposalNet
false
10,704
[ "MIT" ]
0
9246c33b9e9aed2514f53fd0aef48c8ed3eb91d3
https://github.com/mobulan/NTS-Net/tree/9246c33b9e9aed2514f53fd0aef48c8ed3eb91d3
ConvLR
import torch import torch.nn as nn class ConvLR(nn.Module): """[u * v + res] version of torch.nn.ConvLR""" def __init__(self, in_planes, out_planes, kernel_size, stride, padding, rank_ratio=0.25, bias=True, device=None, dtype=None): super().__init__() sliced_rank = int(min(in_planes, out_planes) * rank_ratio) self.u = nn.Conv2d(in_channels=in_planes, out_channels=sliced_rank, kernel_size=kernel_size, stride=stride, padding=padding, bias= False, device=device, dtype=dtype) self.v = nn.Conv2d(in_channels=sliced_rank, out_channels=out_planes, kernel_size=1, stride=1, padding=0, bias=bias, device=device, dtype=dtype) self.res = nn.Conv2d(in_channels=in_planes, out_channels=out_planes, kernel_size=kernel_size, stride=stride, padding=padding, bias= False, device=device, dtype=dtype) def forward(self, input): return self.v(self.u(input)) + self.res(input) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_planes': 4, 'out_planes': 4, 'kernel_size': 4, 'stride': 1, 'padding': 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 @triton.jit def triton_poi_fused_add_convolution_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 1296 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 81 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x3, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tl.store(in_out_ptr0 + x3, tmp4, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (1, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4, 1, 1, 1), (1, 1, 1, 1)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_2, primals_1, stride=(1, 1), padding=(4, 4), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 1, 9, 9), (81, 81, 9, 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, 9, 9), (324, 81, 9, 1)) buf2 = extern_kernels.convolution(primals_2, primals_5, stride=(1, 1), padding=(4, 4), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 4, 9, 9), (324, 81, 9, 1)) buf3 = buf1 del buf1 get_raw_stream(0) triton_poi_fused_add_convolution_0[grid(1296)](buf3, primals_4, buf2, 1296, XBLOCK=128, num_warps=4, num_stages=1) del buf2 del primals_4 return buf3, primals_1, primals_2, primals_3, primals_5, buf0 class ConvLRNew(nn.Module): """[u * v + res] version of torch.nn.ConvLR""" def __init__(self, in_planes, out_planes, kernel_size, stride, padding, rank_ratio=0.25, bias=True, device=None, dtype=None): super().__init__() sliced_rank = int(min(in_planes, out_planes) * rank_ratio) self.u = nn.Conv2d(in_channels=in_planes, out_channels=sliced_rank, kernel_size=kernel_size, stride=stride, padding=padding, bias= False, device=device, dtype=dtype) self.v = nn.Conv2d(in_channels=sliced_rank, out_channels=out_planes, kernel_size=1, stride=1, padding=0, bias=bias, device=device, dtype=dtype) self.res = nn.Conv2d(in_channels=in_planes, out_channels=out_planes, kernel_size=kernel_size, stride=stride, padding=padding, bias= False, device=device, dtype=dtype) def forward(self, input_0): primals_1 = self.u.weight primals_3 = self.v.weight primals_4 = self.v.bias primals_2 = self.res.weight primals_5 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
razered/alternate
ConvLR
false
10,705
[ "MIT" ]
0
18e876aadc76d5f675cf940549b4bcd6e80a0288
https://github.com/razered/alternate/tree/18e876aadc76d5f675cf940549b4bcd6e80a0288
LocationNetwork
import torch import torch.nn as nn import torch.nn.functional as F class LocationNetwork(nn.Module): """ Uses the internal state `h_t` of the core network to produce the location coordinates `l_t` for the next time step. Concretely, feeds the hidden state `h_t` through a fc layer followed by a tanh to clamp the output beween [-1, 1]. This produces a 2D vector of means used to parametrize a two-component Gaussian with a fixed variance from which the location coordinates `l_t` for the next time step are sampled. Hence, the location `l_t` is chosen stochastically from a distribution conditioned on an affine transformation of the hidden state vector `h_t`. Args ---- - input_size: input size of the fc layer. - output_size: output size of the fc layer. - std: standard deviation of the normal distribution. - h_t: the hidden state vector of the core network for the current time step `t`. Returns ------- - mu: a 2D vector of shape (B, 2). - l_t: a 2D vector of shape (B, 2). """ def __init__(self, input_size, output_size, std): super(LocationNetwork, self).__init__() self.std = std self.fc = nn.Linear(input_size, output_size) def forward(self, h_t): mu = F.tanh(self.fc(h_t.detach())) noise = torch.zeros_like(mu) noise.data.normal_(std=self.std) l_t = mu + noise l_t = F.tanh(l_t) return mu, l_t def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'output_size': 4, 'std': 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_zeros_like_0(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 = 0.0 tl.store(out_ptr0 + x0, tmp0, xmask) @triton.jit def triton_poi_fused_add_tanh_1(in_out_ptr0, in_out_ptr1, 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') tmp4 = tl.load(in_out_ptr1 + x2, xmask) tmp2 = tmp0 + tmp1 tmp3 = libdevice.tanh(tmp2) tmp5 = tmp3 + tmp4 tmp6 = libdevice.tanh(tmp5) tl.store(in_out_ptr0 + x2, tmp3, xmask) tl.store(in_out_ptr1 + x2, tmp6, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_zeros_like_0[grid(256)](buf2, 256, XBLOCK=256, num_warps=4, num_stages=1) buf3 = torch.ops.aten.normal_functional.default(buf2, 0.0, 4.0) del buf2 buf4 = buf3 del buf3 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 buf5 = buf4 del buf4 triton_poi_fused_add_tanh_1[grid(256)](buf1, buf5, primals_3, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_3 return buf1, buf5, reinterpret_tensor(primals_1, (64, 4), (4, 1), 0 ), buf1, buf5 class LocationNetworkNew(nn.Module): """ Uses the internal state `h_t` of the core network to produce the location coordinates `l_t` for the next time step. Concretely, feeds the hidden state `h_t` through a fc layer followed by a tanh to clamp the output beween [-1, 1]. This produces a 2D vector of means used to parametrize a two-component Gaussian with a fixed variance from which the location coordinates `l_t` for the next time step are sampled. Hence, the location `l_t` is chosen stochastically from a distribution conditioned on an affine transformation of the hidden state vector `h_t`. Args ---- - input_size: input size of the fc layer. - output_size: output size of the fc layer. - std: standard deviation of the normal distribution. - h_t: the hidden state vector of the core network for the current time step `t`. Returns ------- - mu: a 2D vector of shape (B, 2). - l_t: a 2D vector of shape (B, 2). """ def __init__(self, input_size, output_size, std): super(LocationNetworkNew, self).__init__() self.std = std self.fc = nn.Linear(input_size, output_size) def forward(self, input_0): primals_2 = self.fc.weight primals_3 = self.fc.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0], output[1]
reinvantveer/topography-detection
LocationNetwork
false
10,706
[ "MIT" ]
0
b471dbaa1bc276584374ed3bb5382e2d63046611
https://github.com/reinvantveer/topography-detection/tree/b471dbaa1bc276584374ed3bb5382e2d63046611
CoreNetwork
import torch import torch.nn as nn import torch.nn.functional as F class CoreNetwork(nn.Module): """ An RNN that maintains an internal state that integrates information extracted from the history of past observations. It encodes the agent's knowledge of the environment through a state vector `h_t` that gets updated at every time step `t`. Concretely, it takes the glimpse representation `g_t` as input, and combines it with its internal state `h_t_prev` at the previous time step, to produce the new internal state `h_t` at the current time step. In other words: `h_t = relu( fc(h_t_prev) + fc(g_t) )` Args ---- - input_size: input size of the rnn. - hidden_size: hidden size of the rnn. - g_t: a 2D tensor of shape (B, hidden_size). The glimpse representation returned by the glimpse network for the current timestep `t`. - h_t_prev: a 2D tensor of shape (B, hidden_size). The hidden state vector for the previous timestep `t-1`. Returns ------- - h_t: a 2D tensor of shape (B, hidden_size). The hidden state vector for the current timestep `t`. """ def __init__(self, input_size, hidden_size): super(CoreNetwork, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.i2h = nn.Linear(input_size, hidden_size) self.h2h = nn.Linear(hidden_size, hidden_size) def forward(self, g_t, h_t_prev): h1 = self.i2h(g_t) h2 = self.h2h(h_t_prev) h_t = F.relu(h1 + h2) return h_t def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'hidden_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_relu_threshold_backward_0(in_out_ptr0, 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 x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x2, xmask) tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tmp7 = tl.full([1], 0, tl.int32) tmp8 = triton_helpers.maximum(tmp7, tmp6) tmp9 = 0.0 tmp10 = tmp8 <= tmp9 tl.store(in_out_ptr0 + x2, tmp8, xmask) tl.store(out_ptr0 + x2, tmp10, 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.mm(reinterpret_tensor(primals_6, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1) del primals_4 buf2 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) get_raw_stream(0) triton_poi_fused_add_relu_threshold_backward_0[grid(256)](buf2, primals_2, buf1, primals_5, buf3, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf1 del primals_2 del primals_5 return buf2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(primals_6, (64, 4), (4, 1), 0), buf3 class CoreNetworkNew(nn.Module): """ An RNN that maintains an internal state that integrates information extracted from the history of past observations. It encodes the agent's knowledge of the environment through a state vector `h_t` that gets updated at every time step `t`. Concretely, it takes the glimpse representation `g_t` as input, and combines it with its internal state `h_t_prev` at the previous time step, to produce the new internal state `h_t` at the current time step. In other words: `h_t = relu( fc(h_t_prev) + fc(g_t) )` Args ---- - input_size: input size of the rnn. - hidden_size: hidden size of the rnn. - g_t: a 2D tensor of shape (B, hidden_size). The glimpse representation returned by the glimpse network for the current timestep `t`. - h_t_prev: a 2D tensor of shape (B, hidden_size). The hidden state vector for the previous timestep `t-1`. Returns ------- - h_t: a 2D tensor of shape (B, hidden_size). The hidden state vector for the current timestep `t`. """ def __init__(self, input_size, hidden_size): super(CoreNetworkNew, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.i2h = nn.Linear(input_size, hidden_size) self.h2h = nn.Linear(hidden_size, hidden_size) def forward(self, input_0, input_1): primals_1 = self.i2h.weight primals_2 = self.i2h.bias primals_4 = self.h2h.weight primals_5 = self.h2h.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]
reinvantveer/topography-detection
CoreNetwork
false
10,707
[ "MIT" ]
0
b471dbaa1bc276584374ed3bb5382e2d63046611
https://github.com/reinvantveer/topography-detection/tree/b471dbaa1bc276584374ed3bb5382e2d63046611
Decoder
import torch from torch import nn class Decoder(nn.Module): def __init__(self, latent_channel_dim): super(Decoder, self).__init__() self.t_conv1 = nn.ConvTranspose2d(in_channels=latent_channel_dim, out_channels=16, kernel_size=(2, 2), stride=(2, 2)) self.t_conv2 = nn.ConvTranspose2d(in_channels=16, out_channels=3, kernel_size=(2, 2), stride=(2, 2)) self.act1 = nn.ReLU() self.act2 = nn.Sigmoid() def forward(self, x): x = self.t_conv1(x) x = self.act1(x) x = self.t_conv2(x) x = self.act2(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'latent_channel_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): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 64 % 16 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_convolution_sigmoid_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 3072 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 256 % 3 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.sigmoid(tmp2) tl.store(in_out_ptr0 + x3, tmp3, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 16, 2, 2), (64, 4, 2, 1)) assert_size_stride(primals_2, (16,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (16, 3, 2, 2), (12, 4, 2, 1)) assert_size_stride(primals_5, (3,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 16, 8, 8), (1024, 64, 8, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(4096)](buf1, primals_2, 4096, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = extern_kernels.convolution(buf1, primals_4, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 3, 16, 16), (768, 256, 16, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_sigmoid_1[grid(3072)](buf3, primals_5, 3072, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 return buf3, primals_1, primals_3, primals_4, buf1, buf3 class DecoderNew(nn.Module): def __init__(self, latent_channel_dim): super(DecoderNew, self).__init__() self.t_conv1 = nn.ConvTranspose2d(in_channels=latent_channel_dim, out_channels=16, kernel_size=(2, 2), stride=(2, 2)) self.t_conv2 = nn.ConvTranspose2d(in_channels=16, out_channels=3, kernel_size=(2, 2), stride=(2, 2)) self.act1 = nn.ReLU() self.act2 = nn.Sigmoid() def forward(self, input_0): primals_1 = self.t_conv1.weight primals_2 = self.t_conv1.bias primals_4 = self.t_conv2.weight primals_5 = self.t_conv2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
quickgrid/CodeLab
Decoder
false
10,708
[ "MIT" ]
0
710ebf107b7938f09c055e806c1fed5574d91308
https://github.com/quickgrid/CodeLab/tree/710ebf107b7938f09c055e806c1fed5574d91308
PixelShuffle2d
import functools import torch from torch import nn import torch.nn.functional as F class PixelShuffle2d(nn.Conv2d): def __init__(self, in_nc, out_nc, kernel_size: 'int', scale: 'int'=2, **kwargs): super().__init__(in_nc, out_nc * scale * scale, kernel_size, **kwargs) self.up = functools.partial(F.pixel_shuffle, upscale_factor=scale) def forward(self, x): x = super().forward(x) x = self.up(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_nc': 4, 'out_nc': 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 import functools from torch import nn import torch.nn.functional as F assert_size_stride = torch._C._dynamo.guards.assert_size_stride reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 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_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x2, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (16, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (16,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = 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, 16, 1, 1), (16, 1, 1, 1)) buf1 = reinterpret_tensor(buf0, (4, 16, 1, 1), (16, 1, 64, 64), 0) del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(64)](buf1, primals_2, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_2 return reinterpret_tensor(buf1, (4, 4, 2, 2), (16, 4, 2, 1), 0 ), primals_1, primals_3 class PixelShuffle2dNew(nn.Conv2d): def __init__(self, in_nc, out_nc, kernel_size: 'int', scale: 'int'=2, **kwargs): super().__init__(in_nc, out_nc * scale * scale, kernel_size, **kwargs) self.up = functools.partial(F.pixel_shuffle, upscale_factor=scale) def forward(self, input_0): primals_1 = self.weight primals_2 = self.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
pomelyu/ML_HW
PixelShuffle2d
false
10,709
[ "MIT" ]
0
b87697f3ee86592a34d80c8dbf167a5767731630
https://github.com/pomelyu/ML_HW/tree/b87697f3ee86592a34d80c8dbf167a5767731630
AttentionBlock
import torch from torch import nn class AttentionBlock(nn.Module): def __init__(self, in_nc, out_nc, nd, bias=False): super().__init__() self.in_nc = in_nc self.Wq = nn.Linear(in_nc, nd, bias=bias) self.Wk = nn.Linear(in_nc, nd, bias=bias) self.Wv = nn.Linear(in_nc, out_nc, bias=bias) def forward(self, x): B, N = x.shape N = N // self.in_nc x = x.view(B * N, self.in_nc) Q: 'torch.Tensor' = self.Wq(x).view(B, N, -1) K: 'torch.Tensor' = self.Wk(x).view(B, N, -1) V: 'torch.Tensor' = self.Wv(x).view(B, N, -1) A = Q @ K.transpose(1, 2) A = torch.softmax(A, dim=-1) out = A @ V out = out.view(B, -1) return out def get_inputs(): return [torch.rand([4, 4])] def get_init_inputs(): return [[], {'in_nc': 4, 'out_nc': 4, 'nd': 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 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__softmax_0(in_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_out_ptr0 + x0, xmask) tmp1 = tmp0 - tmp0 tmp2 = tl_math.exp(tmp1) tmp3 = tmp2 / tmp2 tl.store(in_out_ptr0 + x0, tmp3, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf1) del primals_3 buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf2) del primals_4 buf3 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf0, (4, 1, 4), (4, 4, 1), 0 ), reinterpret_tensor(buf1, (4, 4, 1), (4, 1, 4), 0), out=buf3) buf4 = buf3 del buf3 get_raw_stream(0) triton_poi_fused__softmax_0[grid(4)](buf4, 4, XBLOCK=4, num_warps=1, num_stages=1) buf5 = empty_strided_cuda((4, 1, 4), (4, 4, 1), torch.float32) extern_kernels.bmm(buf4, reinterpret_tensor(buf2, (4, 1, 4), (4, 4, 1), 0), out=buf5) return reinterpret_tensor(buf5, (4, 4), (4, 1), 0 ), primals_1, buf4, reinterpret_tensor(buf2, (4, 4, 1), (4, 1, 4), 0 ), reinterpret_tensor(buf0, (4, 4, 1), (4, 1, 4), 0 ), reinterpret_tensor(buf1, (4, 1, 4), (4, 4, 1), 0) class AttentionBlockNew(nn.Module): def __init__(self, in_nc, out_nc, nd, bias=False): super().__init__() self.in_nc = in_nc self.Wq = nn.Linear(in_nc, nd, bias=bias) self.Wk = nn.Linear(in_nc, nd, bias=bias) self.Wv = nn.Linear(in_nc, out_nc, bias=bias) def forward(self, input_0): primals_1 = self.Wq.weight primals_2 = self.Wk.weight primals_3 = self.Wv.weight primals_4 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
pomelyu/ML_HW
AttentionBlock
false
10,710
[ "MIT" ]
0
b87697f3ee86592a34d80c8dbf167a5767731630
https://github.com/pomelyu/ML_HW/tree/b87697f3ee86592a34d80c8dbf167a5767731630
DeConv2d
import functools import torch from torch import nn from typing import Optional import torch.nn.functional as F class DeConv2d(nn.Conv2d): def __init__(self, in_nc: 'int', out_nc: 'int', kernel_size: 'int', scale: 'int'=2, mode: 'str'='nearest', align_corners: 'Optional[bool]'=None, **kwargs): super().__init__(in_nc, out_nc, kernel_size, **kwargs) self.up = functools.partial(F.interpolate, scale_factor=scale, mode =mode, align_corners=align_corners) def forward(self, x): x = self.up(x) x = super().forward(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_nc': 4, 'out_nc': 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 import functools from torch import nn from typing import Optional 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__unsafe_index_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 8 % 8 x0 = xindex % 8 x2 = xindex // 64 x4 = xindex tmp0 = x1 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp3.to(tl.int32) tmp5 = x0 tmp6 = tmp5.to(tl.float32) tmp7 = tmp6 * tmp2 tmp8 = tmp7.to(tl.int32) tmp9 = tl.load(in_ptr0 + (tmp8 + 4 * tmp4 + 16 * x2), xmask, eviction_policy='evict_last') tl.store(out_ptr0 + x4, tmp9, xmask) @triton.jit def triton_poi_fused_convolution_1(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 x3 = xindex x1 = xindex // 25 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 8, 8), (256, 64, 8, 1), torch.float32) get_raw_stream(0) triton_poi_fused__unsafe_index_0[grid(1024)](primals_1, buf0, 1024, 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, 5, 5), (100, 25, 5, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_1[grid(400)](buf2, primals_3, 400, XBLOCK=128, num_warps=4, num_stages=1) del primals_3 return buf2, primals_2, buf0 class DeConv2dNew(nn.Conv2d): def __init__(self, in_nc: 'int', out_nc: 'int', kernel_size: 'int', scale: 'int'=2, mode: 'str'='nearest', align_corners: 'Optional[bool]'=None, **kwargs): super().__init__(in_nc, out_nc, kernel_size, **kwargs) self.up = functools.partial(F.interpolate, scale_factor=scale, mode =mode, align_corners=align_corners) 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]
pomelyu/ML_HW
DeConv2d
false
10,711
[ "MIT" ]
0
b87697f3ee86592a34d80c8dbf167a5767731630
https://github.com/pomelyu/ML_HW/tree/b87697f3ee86592a34d80c8dbf167a5767731630
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(1, 16, 5) self.conv2 = nn.Conv2d(16, 32, 3) self.conv3 = nn.Conv2d(32, 64, 2) self.pool = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(64 * 26 * 26, 1000) self.fc2 = nn.Linear(1000, 136) self.dropout1 = nn.Dropout(0.1) self.dropout2 = nn.Dropout(0.2) self.dropout3 = nn.Dropout(0.3) self.dropout4 = nn.Dropout(0.4) def forward(self, x): x = self.pool(F.relu(self.conv1(x))) x = self.dropout1(x) x = self.pool(F.relu(self.conv2(x))) x = self.dropout2(x) x = self.pool(F.relu(self.conv3(x))) x = self.dropout3(x) x = x.view(-1, 64 * 26 * 26) x = F.relu(self.fc1(x)) x = self.dropout4(x) x = self.fc2(x) return x def get_inputs(): return [torch.rand([4, 1, 432, 432])] 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_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 11723776 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 183184 % 16 x0 = xindex % 183184 x4 = xindex // 183184 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 + 183200 * 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 = 2930944 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 214 x1 = xindex // 214 % 214 x2 = xindex // 45796 x3 = xindex % 45796 tmp0 = tl.load(in_ptr0 + (2 * x0 + 856 * x1 + 183200 * x2), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 856 * x1 + 183200 * x2), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (428 + 2 * x0 + 856 * x1 + 183200 * x2), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (429 + 2 * x0 + 856 * x1 + 183200 * 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 + 45824 * x2), tmp6, xmask) tl.store(out_ptr1 + (x3 + 45824 * x2), tmp16, xmask) @triton.jit def triton_poi_fused_convolution_relu_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) x3 = xindex x1 = xindex // 44944 % 32 x0 = xindex % 44944 x4 = xindex // 44944 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 + 44960 * x4), tmp4, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_3(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 1438208 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 106 x1 = xindex // 106 % 106 x2 = xindex // 11236 x3 = xindex % 11236 tmp0 = tl.load(in_ptr0 + (2 * x0 + 424 * x1 + 44960 * x2), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 424 * x1 + 44960 * x2), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (212 + 2 * x0 + 424 * x1 + 44960 * x2), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (213 + 2 * x0 + 424 * x1 + 44960 * 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 + 11264 * x2), tmp6, xmask) tl.store(out_ptr1 + (x3 + 11264 * x2), tmp16, xmask) @triton.jit def triton_poi_fused_convolution_relu_4(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 2822400 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 11025 % 64 x0 = xindex % 11025 x4 = xindex // 11025 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 + 11040 * x4), tmp4, xmask) @triton.jit def triton_poi_fused_max_pool2d_with_indices_5(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x0 = xindex % 52 x1 = xindex // 52 % 52 x2 = xindex // 2704 x3 = xindex % 2704 tmp0 = tl.load(in_ptr0 + (2 * x0 + 210 * x1 + 11040 * x2), None, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 210 * x1 + 11040 * x2), None, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (105 + 2 * x0 + 210 * x1 + 11040 * x2), None, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (106 + 2 * x0 + 210 * x1 + 11040 * x2), None, 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 + (x3 + 2816 * x2), tmp15, None) tl.store(out_ptr1 + (x3 + 2720 * x2), tmp16, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_view_6(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 % 43264 x1 = xindex // 43264 x2 = xindex tmp0 = tl.load(in_ptr0 + (2720 * (x0 // 2704) + 43520 * x1 + x0 % 2704), None) tl.store(out_ptr0 + x2, tmp0, None) @triton.jit def triton_poi_fused_relu_7(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16000 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 1000 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11) = args args.clear() assert_size_stride(primals_1, (16, 1, 5, 5), (25, 25, 5, 1)) assert_size_stride(primals_2, (16,), (1,)) assert_size_stride(primals_3, (4, 1, 432, 432), (186624, 186624, 432, 1)) assert_size_stride(primals_4, (32, 16, 3, 3), (144, 9, 3, 1)) assert_size_stride(primals_5, (32,), (1,)) assert_size_stride(primals_6, (64, 32, 2, 2), (128, 4, 2, 1)) assert_size_stride(primals_7, (64,), (1,)) assert_size_stride(primals_8, (1000, 43264), (43264, 1)) assert_size_stride(primals_9, (1000,), (1,)) assert_size_stride(primals_10, (136, 1000), (1000, 1)) assert_size_stride(primals_11, (136,), (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, 16, 428, 428), (2930944, 183184, 428, 1)) buf1 = empty_strided_cuda((4, 16, 428, 428), (2931200, 183200, 428, 1), torch.float32) get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(11723776)](buf0, primals_2, buf1, 11723776, XBLOCK=1024, num_warps=4, num_stages=1) del buf0 del primals_2 buf2 = empty_strided_cuda((4, 16, 214, 214), (733184, 45824, 214, 1 ), torch.float32) buf3 = empty_strided_cuda((4, 16, 214, 214), (733184, 45824, 214, 1 ), torch.int8) triton_poi_fused_max_pool2d_with_indices_1[grid(2930944)](buf1, buf2, buf3, 2930944, XBLOCK=512, num_warps=8, 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, 32, 212, 212), (1438208, 44944, 212, 1)) buf5 = empty_strided_cuda((4, 32, 212, 212), (1438720, 44960, 212, 1), torch.float32) triton_poi_fused_convolution_relu_2[grid(5752832)](buf4, primals_5, buf5, 5752832, XBLOCK=512, num_warps=8, num_stages=1) del buf4 del primals_5 buf6 = empty_strided_cuda((4, 32, 106, 106), (360448, 11264, 106, 1 ), torch.float32) buf7 = empty_strided_cuda((4, 32, 106, 106), (360448, 11264, 106, 1 ), torch.int8) triton_poi_fused_max_pool2d_with_indices_3[grid(1438208)](buf5, buf6, buf7, 1438208, XBLOCK=512, num_warps=8, num_stages=1) buf8 = extern_kernels.convolution(buf6, primals_6, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 64, 105, 105), (705600, 11025, 105, 1)) buf9 = empty_strided_cuda((4, 64, 105, 105), (706560, 11040, 105, 1 ), torch.float32) triton_poi_fused_convolution_relu_4[grid(2822400)](buf8, primals_7, buf9, 2822400, XBLOCK=1024, num_warps=4, num_stages=1) del buf8 del primals_7 buf10 = empty_strided_cuda((4, 64, 52, 52), (180224, 2816, 52, 1), torch.int8) buf11 = empty_strided_cuda((4, 64, 52, 52), (174080, 2720, 52, 1), torch.float32) triton_poi_fused_max_pool2d_with_indices_5[grid(692224)](buf9, buf10, buf11, 692224, XBLOCK=512, num_warps=8, num_stages=1) buf12 = empty_strided_cuda((16, 43264), (43264, 1), torch.float32) triton_poi_fused_max_pool2d_with_indices_view_6[grid(692224)](buf11, buf12, 692224, XBLOCK=512, num_warps=8, num_stages=1) del buf11 buf13 = empty_strided_cuda((16, 1000), (1000, 1), torch.float32) extern_kernels.mm(buf12, reinterpret_tensor(primals_8, (43264, 1000 ), (1, 43264), 0), out=buf13) buf14 = buf13 del buf13 triton_poi_fused_relu_7[grid(16000)](buf14, primals_9, 16000, XBLOCK=256, num_warps=4, num_stages=1) del primals_9 buf15 = empty_strided_cuda((16, 136), (136, 1), torch.float32) extern_kernels.addmm(primals_11, buf14, reinterpret_tensor( primals_10, (1000, 136), (1, 1000), 0), alpha=1, beta=1, out=buf15) del primals_11 return (buf15, primals_1, primals_3, primals_4, primals_6, buf1, buf2, buf3, buf5, buf6, buf7, buf9, buf10, buf12, buf14, primals_10, primals_8) class NetNew(nn.Module): def __init__(self): super(NetNew, self).__init__() self.conv1 = nn.Conv2d(1, 16, 5) self.conv2 = nn.Conv2d(16, 32, 3) self.conv3 = nn.Conv2d(32, 64, 2) self.pool = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(64 * 26 * 26, 1000) self.fc2 = nn.Linear(1000, 136) self.dropout1 = nn.Dropout(0.1) self.dropout2 = nn.Dropout(0.2) self.dropout3 = nn.Dropout(0.3) self.dropout4 = nn.Dropout(0.4) def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_6 = self.conv3.weight primals_7 = self.conv3.bias primals_8 = self.fc1.weight primals_9 = self.fc1.bias primals_10 = self.fc2.weight primals_11 = self.fc2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11]) return output[0]
olbedo/AAIND-Facial-Keypoints
Net
false
10,712
[ "MIT" ]
0
3d11094665d45feb312e375ee57e09ff7f601eb9
https://github.com/olbedo/AAIND-Facial-Keypoints/tree/3d11094665d45feb312e375ee57e09ff7f601eb9
LinearNetwork
import torch from typing import List import torch.nn as nn class LinearNetwork(nn.Module): def __init__(self, input_size: 'int', output_size: 'int', hidden_layers: 'List[int]', activation: 'nn.Module'=nn.LeakyReLU): super(LinearNetwork, self).__init__() self.input_size = input_size self.output_size = output_size self.hidden_layers = hidden_layers self.activation = activation self.n_layers = len(hidden_layers) self.layers = nn.Sequential() if self.n_layers == 0: self.layers.add_module('single_layer', nn.Linear(input_size, output_size)) else: for i in range(self.n_layers + 1): if i == 0: self.layers.add_module('input_layer', nn.Linear( input_size, hidden_layers[0])) self.layers.add_module('input_layer_activation', self. activation()) elif i < self.n_layers: self.layers.add_module(f'hidden_layer_{i}', nn.Linear( hidden_layers[i - 1], hidden_layers[i])) self.layers.add_module(f'input_layer_{i}_activation', self.activation()) else: self.layers.add_module('output_layer', nn.Linear( hidden_layers[i - 1], output_size)) def forward(self, x: 'torch.Tensor') ->torch.Tensor: assert len(x.shape) == 2 assert x.shape[1] == self.input_size return self.layers(x) def get_inputs(): return [torch.rand([4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'output_size': 4, 'hidden_layers': [4, 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 typing import List 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 = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr1 + x2, tmp7, xmask) 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,), (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), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.bool) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_leaky_relu_0[grid(16)](buf0, primals_3, buf1, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_3 buf3 = buf0 del buf0 extern_kernels.mm(buf2, reinterpret_tensor(primals_4, (4, 4), (1, 4 ), 0), out=buf3) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.bool) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_leaky_relu_0[grid(16)](buf3, primals_5, buf4, buf5, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_5 buf6 = buf3 del buf3 extern_kernels.addmm(primals_7, buf5, reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf6) del primals_7 return buf6, primals_1, buf1, buf2, buf4, buf5, primals_6, primals_4 class LinearNetworkNew(nn.Module): def __init__(self, input_size: 'int', output_size: 'int', hidden_layers: 'List[int]', activation: 'nn.Module'=nn.LeakyReLU): super(LinearNetworkNew, self).__init__() self.input_size = input_size self.output_size = output_size self.hidden_layers = hidden_layers self.activation = activation self.n_layers = len(hidden_layers) self.layers = nn.Sequential() if self.n_layers == 0: self.layers.add_module('single_layer', nn.Linear(input_size, output_size)) else: for i in range(self.n_layers + 1): if i == 0: self.layers.add_module('input_layer', nn.Linear( input_size, hidden_layers[0])) self.layers.add_module('input_layer_activation', self. activation()) elif i < self.n_layers: self.layers.add_module(f'hidden_layer_{i}', nn.Linear( hidden_layers[i - 1], hidden_layers[i])) self.layers.add_module(f'input_layer_{i}_activation', self.activation()) else: self.layers.add_module('output_layer', nn.Linear( hidden_layers[i - 1], output_size)) def forward(self, input_0): primals_1 = self.layers.input_layer.weight primals_3 = self.layers.input_layer.bias primals_2 = self.layers.hidden_layer_1.weight primals_5 = self.layers.hidden_layer_1.bias primals_4 = self.layers.output_layer.weight primals_7 = self.layers.output_layer.bias primals_6 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
redvinaa/multiagent-path-finding-continuous
LinearNetwork
false
10,713
[ "MIT" ]
0
2d4ba3388f9b951c443ba72a33bd7af4f461275f
https://github.com/redvinaa/multiagent-path-finding-continuous/tree/2d4ba3388f9b951c443ba72a33bd7af4f461275f
avgpool
import torch import torch.nn as nn import torch.nn.parallel import torch.utils.data class avgpool(nn.Module): """ Mean pooling class - downsampling """ def __init__(self, up_size=0): super(avgpool, self).__init__() def forward(self, x): out_man = (x[:, :, ::2, ::2] + x[:, :, 1::2, ::2] + x[:, :, ::2, 1: :2] + x[:, :, 1::2, 1::2]) / 4 return out_man 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.nn.parallel 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_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 % 2 x1 = xindex // 2 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (4 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (1 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (5 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 0.25 tmp8 = tmp6 * tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_0[grid(64)](arg0_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 return buf0, class avgpoolNew(nn.Module): """ Mean pooling class - downsampling """ def __init__(self, up_size=0): super(avgpoolNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
nathalia-kim/nu_gan
avgpool
false
10,714
[ "MIT" ]
0
c1d0891945bd7ac3d95869db91f490f57f203110
https://github.com/nathalia-kim/nu_gan/tree/c1d0891945bd7ac3d95869db91f490f57f203110
Encoder
import torch from torch import nn class Encoder(nn.Module): def __init__(self, latent_channel_dim): super(Encoder, self).__init__() self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size= (3, 3), stride=(1, 1), padding=(1, 1)) self.conv2 = nn.Conv2d(in_channels=16, out_channels= latent_channel_dim, kernel_size=(3, 3), stride=(1, 1), padding= (1, 1)) self.pool1 = nn.MaxPool2d(2, stride=2) self.act1 = nn.ReLU() def forward(self, x): x = self.conv1(x) x = self.act1(x) x = self.pool1(x) x = self.conv2(x) x = self.act1(x) x = self.pool1(x) return x def get_inputs(): return [torch.rand([4, 3, 64, 64])] def get_init_inputs(): return [[], {'latent_channel_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_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 % 16 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_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 % 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_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 // 1024 % 4 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_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 % 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) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (16, 3, 3, 3), (27, 9, 3, 1)) assert_size_stride(primals_2, (16,), (1,)) assert_size_stride(primals_3, (4, 3, 64, 64), (12288, 4096, 64, 1)) assert_size_stride(primals_4, (4, 16, 3, 3), (144, 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, 16, 64, 64), (65536, 4096, 64, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(262144)](buf1, primals_2, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((4, 16, 32, 32), (16384, 1024, 32, 1), torch.float32) buf3 = empty_strided_cuda((4, 16, 32, 32), (16384, 1024, 32, 1), torch.int8) triton_poi_fused_max_pool2d_with_indices_1[grid(65536)](buf1, buf2, buf3, 65536, XBLOCK=512, num_warps=4, 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, 4, 32, 32), (4096, 1024, 32, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_relu_2[grid(16384)](buf5, primals_5, 16384, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf6 = empty_strided_cuda((4, 4, 16, 16), (1024, 256, 16, 1), torch .float32) buf7 = empty_strided_cuda((4, 4, 16, 16), (1024, 256, 16, 1), torch .int8) triton_poi_fused_max_pool2d_with_indices_3[grid(4096)](buf5, buf6, buf7, 4096, XBLOCK=256, num_warps=4, num_stages=1) return buf6, primals_1, primals_3, primals_4, buf1, buf2, buf3, buf5, buf7 class EncoderNew(nn.Module): def __init__(self, latent_channel_dim): super(EncoderNew, self).__init__() self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size= (3, 3), stride=(1, 1), padding=(1, 1)) self.conv2 = nn.Conv2d(in_channels=16, out_channels= latent_channel_dim, kernel_size=(3, 3), stride=(1, 1), padding= (1, 1)) self.pool1 = nn.MaxPool2d(2, stride=2) self.act1 = nn.ReLU() 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]
quickgrid/CodeLab
Encoder
false
10,715
[ "MIT" ]
0
710ebf107b7938f09c055e806c1fed5574d91308
https://github.com/quickgrid/CodeLab/tree/710ebf107b7938f09c055e806c1fed5574d91308
myFeature
import torch class myFeature(torch.nn.Module): """ Feature: sin(x) """ def __init__(self): super(myFeature, self).__init__() def forward(self, x): return torch.sin(x[:, 0] * torch.pi) * torch.sin(x[:, 1] * torch.pi) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math 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_sin_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) tmp4 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask) tmp1 = 3.141592653589793 tmp2 = tmp0 * tmp1 tmp3 = tl_math.sin(tmp2) tmp5 = tmp4 * tmp1 tmp6 = tl_math.sin(tmp5) tmp7 = tmp3 * tmp6 tl.store(out_ptr0 + x2, tmp7, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_sin_0[grid(64)](arg0_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 return buf0, class myFeatureNew(torch.nn.Module): """ Feature: sin(x) """ def __init__(self): super(myFeatureNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
ndem0/PINA
myFeature
false
10,716
[ "MIT" ]
0
1812ddb8d96a9c8aeb80ce35002dbd115e7d7931
https://github.com/ndem0/PINA/tree/1812ddb8d96a9c8aeb80ce35002dbd115e7d7931
AdaptiveCos
import torch from torch.nn.parameter import Parameter class AdaptiveCos(torch.nn.Module): """ Implementation of soft exponential activation. Shape: - Input: (N, *) where * means, any number of additional dimensions - Output: (N, *), same shape as the input Parameters: - alpha - trainable parameter References: - See related paper: https://arxiv.org/pdf/1602.01321.pdf Examples: >>> a1 = soft_exponential(256) >>> x = torch.randn(256) >>> x = a1(x) """ def __init__(self, alpha=None): """ Initialization. INPUT: - in_features: shape of the input - aplha: trainable parameter aplha is initialized with zero value by default """ super(AdaptiveCos, self).__init__() if alpha is None: self.alpha = Parameter(torch.tensor(1.0)) else: self.alpha = Parameter(torch.tensor(alpha)) self.alpha.requiresGrad = True self.scale = Parameter(torch.tensor(1.0)) self.scale.requiresGrad = True self.translate = Parameter(torch.tensor(0.0)) self.translate.requiresGrad = True def forward(self, x): """ Forward pass of the function. Applies the function to the input elementwise. """ return self.scale * torch.cos(self.alpha * x + self.translate) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math from torch.nn.parameter import Parameter assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_cos_mul_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK]) tmp2 = tl.load(in_ptr1 + 0) tmp3 = tl.broadcast_to(tmp2, [XBLOCK]) tmp4 = tl.load(in_ptr2 + x0, xmask) tmp6 = tl.load(in_ptr3 + 0) tmp7 = tl.broadcast_to(tmp6, [XBLOCK]) tmp5 = tmp3 * tmp4 tmp8 = tmp5 + tmp7 tmp9 = tl_math.cos(tmp8) tmp10 = tmp1 * tmp9 tl.store(out_ptr0 + x0, tmp10, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (), ()) assert_size_stride(primals_2, (), ()) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (), ()) 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_cos_mul_0[grid(256)](primals_1, primals_2, primals_3, primals_4, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) return buf0, primals_1, primals_2, primals_3, primals_4 class AdaptiveCosNew(torch.nn.Module): """ Implementation of soft exponential activation. Shape: - Input: (N, *) where * means, any number of additional dimensions - Output: (N, *), same shape as the input Parameters: - alpha - trainable parameter References: - See related paper: https://arxiv.org/pdf/1602.01321.pdf Examples: >>> a1 = soft_exponential(256) >>> x = torch.randn(256) >>> x = a1(x) """ def __init__(self, alpha=None): """ Initialization. INPUT: - in_features: shape of the input - aplha: trainable parameter aplha is initialized with zero value by default """ super(AdaptiveCosNew, self).__init__() if alpha is None: self.alpha = Parameter(torch.tensor(1.0)) else: self.alpha = Parameter(torch.tensor(alpha)) self.alpha.requiresGrad = True self.scale = Parameter(torch.tensor(1.0)) self.scale.requiresGrad = True self.translate = Parameter(torch.tensor(0.0)) self.translate.requiresGrad = True def forward(self, input_0): primals_1 = self.alpha primals_2 = self.scale primals_4 = self.translate primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
ndem0/PINA
AdaptiveCos
false
10,717
[ "MIT" ]
0
1812ddb8d96a9c8aeb80ce35002dbd115e7d7931
https://github.com/ndem0/PINA/tree/1812ddb8d96a9c8aeb80ce35002dbd115e7d7931
AutoEncoder
import torch from torch import nn class Encoder(nn.Module): def __init__(self, latent_channel_dim): super(Encoder, self).__init__() self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size= (3, 3), stride=(1, 1), padding=(1, 1)) self.conv2 = nn.Conv2d(in_channels=16, out_channels= latent_channel_dim, kernel_size=(3, 3), stride=(1, 1), padding= (1, 1)) self.pool1 = nn.MaxPool2d(2, stride=2) self.act1 = nn.ReLU() def forward(self, x): x = self.conv1(x) x = self.act1(x) x = self.pool1(x) x = self.conv2(x) x = self.act1(x) x = self.pool1(x) return x class Decoder(nn.Module): def __init__(self, latent_channel_dim): super(Decoder, self).__init__() self.t_conv1 = nn.ConvTranspose2d(in_channels=latent_channel_dim, out_channels=16, kernel_size=(2, 2), stride=(2, 2)) self.t_conv2 = nn.ConvTranspose2d(in_channels=16, out_channels=3, kernel_size=(2, 2), stride=(2, 2)) self.act1 = nn.ReLU() self.act2 = nn.Sigmoid() def forward(self, x): x = self.t_conv1(x) x = self.act1(x) x = self.t_conv2(x) x = self.act2(x) return x class AutoEncoder(nn.Module): def __init__(self, latent_channel_dim): super(AutoEncoder, self).__init__() self.encoder = Encoder(latent_channel_dim=latent_channel_dim) self.decoder = Decoder(latent_channel_dim=latent_channel_dim) def forward(self, x): x = self.encoder(x) x = self.decoder(x) return x def get_inputs(): return [torch.rand([4, 3, 64, 64])] def get_init_inputs(): return [[], {'latent_channel_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_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 % 16 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_max_pool2d_with_indices_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 % 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_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 // 1024 % 4 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_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 % 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_4(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 1024 % 16 tmp0 = tl.load(in_out_ptr0 + x3, None) tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, None) @triton.jit def triton_poi_fused_convolution_sigmoid_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 // 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.sigmoid(tmp2) tl.store(in_out_ptr0 + x3, tmp3, None) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9) = args args.clear() assert_size_stride(primals_1, (16, 3, 3, 3), (27, 9, 3, 1)) assert_size_stride(primals_2, (16,), (1,)) assert_size_stride(primals_3, (4, 3, 64, 64), (12288, 4096, 64, 1)) assert_size_stride(primals_4, (4, 16, 3, 3), (144, 9, 3, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 16, 2, 2), (64, 4, 2, 1)) assert_size_stride(primals_7, (16,), (1,)) assert_size_stride(primals_8, (16, 3, 2, 2), (12, 4, 2, 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=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 16, 64, 64), (65536, 4096, 64, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(262144)](buf1, primals_2, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((4, 16, 32, 32), (16384, 1024, 32, 1), torch.float32) buf3 = empty_strided_cuda((4, 16, 32, 32), (16384, 1024, 32, 1), torch.int8) triton_poi_fused_max_pool2d_with_indices_1[grid(65536)](buf1, buf2, buf3, 65536, XBLOCK=512, num_warps=4, 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, 4, 32, 32), (4096, 1024, 32, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_relu_2[grid(16384)](buf5, primals_5, 16384, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf6 = empty_strided_cuda((4, 4, 16, 16), (1024, 256, 16, 1), torch .float32) buf7 = empty_strided_cuda((4, 4, 16, 16), (1024, 256, 16, 1), torch .int8) triton_poi_fused_max_pool2d_with_indices_3[grid(4096)](buf5, buf6, buf7, 4096, XBLOCK=256, num_warps=4, num_stages=1) buf8 = extern_kernels.convolution(buf6, primals_6, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 16, 32, 32), (16384, 1024, 32, 1)) buf9 = buf8 del buf8 triton_poi_fused_convolution_relu_4[grid(65536)](buf9, primals_7, 65536, XBLOCK=512, num_warps=4, num_stages=1) del primals_7 buf10 = extern_kernels.convolution(buf9, primals_8, stride=(2, 2), padding=(0, 0), dilation=(1, 1), transposed=True, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf10, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf11 = buf10 del buf10 triton_poi_fused_convolution_sigmoid_5[grid(49152)](buf11, primals_9, 49152, XBLOCK=256, num_warps=4, num_stages=1) del primals_9 return (buf11, primals_1, primals_3, primals_4, primals_6, primals_8, buf1, buf2, buf3, buf5, buf6, buf7, buf9, buf11) class Encoder(nn.Module): def __init__(self, latent_channel_dim): super(Encoder, self).__init__() self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size= (3, 3), stride=(1, 1), padding=(1, 1)) self.conv2 = nn.Conv2d(in_channels=16, out_channels= latent_channel_dim, kernel_size=(3, 3), stride=(1, 1), padding= (1, 1)) self.pool1 = nn.MaxPool2d(2, stride=2) self.act1 = nn.ReLU() def forward(self, x): x = self.conv1(x) x = self.act1(x) x = self.pool1(x) x = self.conv2(x) x = self.act1(x) x = self.pool1(x) return x class Decoder(nn.Module): def __init__(self, latent_channel_dim): super(Decoder, self).__init__() self.t_conv1 = nn.ConvTranspose2d(in_channels=latent_channel_dim, out_channels=16, kernel_size=(2, 2), stride=(2, 2)) self.t_conv2 = nn.ConvTranspose2d(in_channels=16, out_channels=3, kernel_size=(2, 2), stride=(2, 2)) self.act1 = nn.ReLU() self.act2 = nn.Sigmoid() def forward(self, x): x = self.t_conv1(x) x = self.act1(x) x = self.t_conv2(x) x = self.act2(x) return x class AutoEncoderNew(nn.Module): def __init__(self, latent_channel_dim): super(AutoEncoderNew, self).__init__() self.encoder = Encoder(latent_channel_dim=latent_channel_dim) self.decoder = Decoder(latent_channel_dim=latent_channel_dim) def forward(self, input_0): primals_1 = self.encoder.conv1.weight primals_2 = self.encoder.conv1.bias primals_4 = self.encoder.conv2.weight primals_5 = self.encoder.conv2.bias primals_6 = self.decoder.t_conv1.weight primals_7 = self.decoder.t_conv1.bias primals_8 = self.decoder.t_conv2.weight primals_9 = self.decoder.t_conv2.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]
quickgrid/CodeLab
AutoEncoder
false
10,718
[ "MIT" ]
0
710ebf107b7938f09c055e806c1fed5574d91308
https://github.com/quickgrid/CodeLab/tree/710ebf107b7938f09c055e806c1fed5574d91308
AdaptiveSquare
import torch from torch.nn.parameter import Parameter class AdaptiveSquare(torch.nn.Module): """ Implementation of soft exponential activation. Shape: - Input: (N, *) where * means, any number of additional dimensions - Output: (N, *), same shape as the input Parameters: - alpha - trainable parameter References: - See related paper: https://arxiv.org/pdf/1602.01321.pdf Examples: >>> a1 = soft_exponential(256) >>> x = torch.randn(256) >>> x = a1(x) """ def __init__(self, alpha=None): """ Initialization. INPUT: - in_features: shape of the input - aplha: trainable parameter aplha is initialized with zero value by default """ super(AdaptiveSquare, self).__init__() self.scale = Parameter(torch.tensor(1.0)) self.scale.requiresGrad = True self.translate = Parameter(torch.tensor(0.0)) self.translate.requiresGrad = True def forward(self, x): """ Forward pass of the function. Applies the function to the input elementwise. """ return self.scale * (x + self.translate) ** 2 def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch.nn.parameter import Parameter assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_mul_pow_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK]) tmp2 = tl.load(in_ptr1 + x0, xmask) tmp3 = tl.load(in_ptr2 + 0) tmp4 = tl.broadcast_to(tmp3, [XBLOCK]) tmp5 = tmp2 + tmp4 tmp6 = tmp5 * tmp5 tmp7 = tmp1 * tmp6 tl.store(out_ptr0 + x0, tmp7, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (), ()) assert_size_stride(primals_2, (), ()) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mul_pow_0[grid(256)](primals_1, primals_3, primals_2, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) return buf0, primals_1, primals_2, primals_3 class AdaptiveSquareNew(torch.nn.Module): """ Implementation of soft exponential activation. Shape: - Input: (N, *) where * means, any number of additional dimensions - Output: (N, *), same shape as the input Parameters: - alpha - trainable parameter References: - See related paper: https://arxiv.org/pdf/1602.01321.pdf Examples: >>> a1 = soft_exponential(256) >>> x = torch.randn(256) >>> x = a1(x) """ def __init__(self, alpha=None): """ Initialization. INPUT: - in_features: shape of the input - aplha: trainable parameter aplha is initialized with zero value by default """ super(AdaptiveSquareNew, self).__init__() self.scale = Parameter(torch.tensor(1.0)) self.scale.requiresGrad = True self.translate = Parameter(torch.tensor(0.0)) self.translate.requiresGrad = True def forward(self, input_0): primals_1 = self.scale primals_2 = self.translate primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
ndem0/PINA
AdaptiveSquare
false
10,719
[ "MIT" ]
0
1812ddb8d96a9c8aeb80ce35002dbd115e7d7931
https://github.com/ndem0/PINA/tree/1812ddb8d96a9c8aeb80ce35002dbd115e7d7931
AdaptiveSin
import torch from torch.nn.parameter import Parameter class AdaptiveSin(torch.nn.Module): """ Implementation of soft exponential activation. Shape: - Input: (N, *) where * means, any number of additional dimensions - Output: (N, *), same shape as the input Parameters: - alpha - trainable parameter References: - See related paper: https://arxiv.org/pdf/1602.01321.pdf Examples: >>> a1 = soft_exponential(256) >>> x = torch.randn(256) >>> x = a1(x) """ def __init__(self, alpha=None): """ Initialization. INPUT: - in_features: shape of the input - aplha: trainable parameter aplha is initialized with zero value by default """ super(AdaptiveSin, self).__init__() self.alpha = Parameter(torch.normal(torch.tensor(1.0), torch.tensor (0.1))) self.alpha.requiresGrad = True self.scale = Parameter(torch.normal(torch.tensor(1.0), torch.tensor (0.1))) self.scale.requiresGrad = True self.translate = Parameter(torch.normal(torch.tensor(0.0), torch. tensor(0.1))) self.translate.requiresGrad = True def forward(self, x): """ Forward pass of the function. Applies the function to the input elementwise. """ return self.scale * torch.sin(self.alpha * x + self.translate) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math from torch.nn.parameter import Parameter assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_mul_sin_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK]) tmp2 = tl.load(in_ptr1 + 0) tmp3 = tl.broadcast_to(tmp2, [XBLOCK]) tmp4 = tl.load(in_ptr2 + x0, xmask) tmp6 = tl.load(in_ptr3 + 0) tmp7 = tl.broadcast_to(tmp6, [XBLOCK]) tmp5 = tmp3 * tmp4 tmp8 = tmp5 + tmp7 tmp9 = tl_math.sin(tmp8) tmp10 = tmp1 * tmp9 tl.store(out_ptr0 + x0, tmp10, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (), ()) assert_size_stride(primals_2, (), ()) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (), ()) 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_sin_0[grid(256)](primals_1, primals_2, primals_3, primals_4, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) return buf0, primals_1, primals_2, primals_3, primals_4 class AdaptiveSinNew(torch.nn.Module): """ Implementation of soft exponential activation. Shape: - Input: (N, *) where * means, any number of additional dimensions - Output: (N, *), same shape as the input Parameters: - alpha - trainable parameter References: - See related paper: https://arxiv.org/pdf/1602.01321.pdf Examples: >>> a1 = soft_exponential(256) >>> x = torch.randn(256) >>> x = a1(x) """ def __init__(self, alpha=None): """ Initialization. INPUT: - in_features: shape of the input - aplha: trainable parameter aplha is initialized with zero value by default """ super(AdaptiveSinNew, self).__init__() self.alpha = Parameter(torch.normal(torch.tensor(1.0), torch.tensor (0.1))) self.alpha.requiresGrad = True self.scale = Parameter(torch.normal(torch.tensor(1.0), torch.tensor (0.1))) self.scale.requiresGrad = True self.translate = Parameter(torch.normal(torch.tensor(0.0), torch. tensor(0.1))) self.translate.requiresGrad = True def forward(self, input_0): primals_1 = self.alpha primals_2 = self.scale primals_4 = self.translate primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
ndem0/PINA
AdaptiveSin
false
10,720
[ "MIT" ]
0
1812ddb8d96a9c8aeb80ce35002dbd115e7d7931
https://github.com/ndem0/PINA/tree/1812ddb8d96a9c8aeb80ce35002dbd115e7d7931
_netD_Q
import torch import torch.nn as nn import torch.nn.parallel import torch.utils.data class _netD_Q(nn.Module): """ Second part of auxiliary network Q """ def __init__(self, nd=10): super(_netD_Q, self).__init__() self.linear = nn.Linear(128, nd, bias=True) self.softmax = nn.LogSoftmax() self.nd = nd def forward(self, x): x = x.view(-1, 128) x = self.linear(x) x = torch.nn.functional.log_softmax(x, -1) return x.view(-1, self.nd, 1, 1) 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 from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn import torch.nn.parallel import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_per_fused__log_softmax__log_softmax_backward_data_0(in_ptr0, out_ptr2, out_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 2 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 tmp13 = tl_math.exp(tmp12) tl.store(out_ptr2 + (r1 + 10 * x0), tmp12, rmask & xmask) tl.store(out_ptr3 + (r1 + 10 * x0), tmp13, rmask & 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, (10, 128), (128, 1)) assert_size_stride(primals_3, (10,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((2, 10), (10, 1), torch.float32) extern_kernels.addmm(primals_3, reinterpret_tensor(primals_1, (2, 128), (128, 1), 0), reinterpret_tensor(primals_2, (128, 10), (1, 128), 0), alpha=1, beta=1, out=buf0) del primals_2 del primals_3 buf3 = empty_strided_cuda((2, 10), (10, 1), torch.float32) buf4 = empty_strided_cuda((2, 10), (10, 1), torch.float32) get_raw_stream(0) triton_per_fused__log_softmax__log_softmax_backward_data_0[grid(2)]( buf0, buf3, buf4, 2, 10, XBLOCK=1, num_warps=2, num_stages=1) del buf0 return reinterpret_tensor(buf3, (2, 10, 1, 1), (10, 1, 1, 1), 0 ), reinterpret_tensor(primals_1, (2, 128), (128, 1), 0), buf4 class _netD_QNew(nn.Module): """ Second part of auxiliary network Q """ def __init__(self, nd=10): super(_netD_QNew, self).__init__() self.linear = nn.Linear(128, nd, bias=True) self.softmax = nn.LogSoftmax() self.nd = nd 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]
nathalia-kim/nu_gan
_netD_Q
false
10,721
[ "MIT" ]
0
c1d0891945bd7ac3d95869db91f490f57f203110
https://github.com/nathalia-kim/nu_gan/tree/c1d0891945bd7ac3d95869db91f490f57f203110
TanhGaussianPolicy
import torch from typing import List from typing import Tuple import torch.nn as nn class LinearNetwork(nn.Module): def __init__(self, input_size: 'int', output_size: 'int', hidden_layers: 'List[int]', activation: 'nn.Module'=nn.LeakyReLU): super(LinearNetwork, self).__init__() self.input_size = input_size self.output_size = output_size self.hidden_layers = hidden_layers self.activation = activation self.n_layers = len(hidden_layers) self.layers = nn.Sequential() if self.n_layers == 0: self.layers.add_module('single_layer', nn.Linear(input_size, output_size)) else: for i in range(self.n_layers + 1): if i == 0: self.layers.add_module('input_layer', nn.Linear( input_size, hidden_layers[0])) self.layers.add_module('input_layer_activation', self. activation()) elif i < self.n_layers: self.layers.add_module(f'hidden_layer_{i}', nn.Linear( hidden_layers[i - 1], hidden_layers[i])) self.layers.add_module(f'input_layer_{i}_activation', self.activation()) else: self.layers.add_module('output_layer', nn.Linear( hidden_layers[i - 1], output_size)) def forward(self, x: 'torch.Tensor') ->torch.Tensor: assert len(x.shape) == 2 assert x.shape[1] == self.input_size return self.layers(x) class TanhGaussianPolicy(nn.Module): LOG_STD_MIN: 'float' = -20.0 LOG_STD_MAX: 'float' = 2.0 EPS: 'float' = 1e-06 def __init__(self, n_agents: 'int', obs_size: 'int', act_size: 'int', hidden_layers: 'List[int]', activation: 'nn.Module'=nn.LeakyReLU): super(TanhGaussianPolicy, self).__init__() self.n_agents = n_agents self.obs_size = obs_size self.act_size = act_size self.hidden_layers = hidden_layers self.activation = activation self.input_size = obs_size self.output_size = 2 * act_size self.policy = LinearNetwork(self.input_size, self.output_size, hidden_layers, activation) def forward(self, obs: 'torch.Tensor') ->Tuple[torch.Tensor, torch.Tensor]: assert len(obs.shape) == 2 assert obs.shape[1] == self.obs_size obs.shape[0] mean, log_std = torch.chunk(self.policy(obs), 2, dim=-1) log_std = torch.clamp(log_std, min=self.LOG_STD_MIN, max=self. LOG_STD_MAX) return mean, log_std def sample(self, obs: 'torch.Tensor') ->Tuple[torch.Tensor, torch. Tensor, torch.Tensor]: assert len(obs.shape) == 3 assert obs.shape[1] == self.n_agents assert obs.shape[2] == self.obs_size N = obs.shape[0] means = torch.empty((N, self.n_agents, self.act_size)) stds = torch.empty((N, self.n_agents, self.act_size)) for i in range(self.n_agents): mean, log_std = self.forward(obs[:, i]) std = log_std.exp() means[:, i] = mean stds[:, i] = std dist = torch.distributions.normal.Normal(means, stds) act_sampled = dist.rsample() act_sampled_tanh = torch.tanh(act_sampled) log_probs = dist.log_prob(act_sampled) - torch.log(1 - act_sampled_tanh.square() + self.EPS) entropies = -log_probs.sum(dim=-1, keepdim=False) return act_sampled_tanh, entropies, torch.tanh(means) def get_inputs(): return [torch.rand([4, 4])] def get_init_inputs(): return [[], {'n_agents': 4, 'obs_size': 4, 'act_size': 4, 'hidden_layers': [4, 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 typing import List from typing import Tuple 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 = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr1 + x2, tmp7, xmask) @triton.jit def triton_poi_fused_clamp_ge_le_logical_and_1(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 + (4 + x0 + 8 * x1), xmask) tmp1 = -20.0 tmp2 = triton_helpers.maximum(tmp0, tmp1) tmp3 = 2.0 tmp4 = triton_helpers.minimum(tmp2, tmp3) tmp5 = tmp0 >= tmp1 tmp6 = tmp0 <= tmp3 tmp7 = tmp5 & tmp6 tl.store(out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr1 + x2, tmp7, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 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, (8, 4), (4, 1)) assert_size_stride(primals_7, (8,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.bool) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_leaky_relu_0[grid(16)](buf0, primals_3, buf1, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_3 buf3 = buf0 del buf0 extern_kernels.mm(buf2, reinterpret_tensor(primals_4, (4, 4), (1, 4 ), 0), out=buf3) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.bool) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_leaky_relu_0[grid(16)](buf3, primals_5, buf4, buf5, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_5 buf6 = empty_strided_cuda((4, 8), (8, 1), torch.float32) extern_kernels.addmm(primals_7, buf5, reinterpret_tensor(primals_6, (4, 8), (1, 4), 0), alpha=1, beta=1, out=buf6) del primals_7 buf7 = buf3 del buf3 buf8 = empty_strided_cuda((4, 4), (4, 1), torch.bool) triton_poi_fused_clamp_ge_le_logical_and_1[grid(16)](buf6, buf7, buf8, 16, XBLOCK=16, num_warps=1, num_stages=1) return reinterpret_tensor(buf6, (4, 4), (8, 1), 0 ), buf7, primals_1, buf1, buf2, buf4, buf5, buf8, primals_6, primals_4 class LinearNetwork(nn.Module): def __init__(self, input_size: 'int', output_size: 'int', hidden_layers: 'List[int]', activation: 'nn.Module'=nn.LeakyReLU): super(LinearNetwork, self).__init__() self.input_size = input_size self.output_size = output_size self.hidden_layers = hidden_layers self.activation = activation self.n_layers = len(hidden_layers) self.layers = nn.Sequential() if self.n_layers == 0: self.layers.add_module('single_layer', nn.Linear(input_size, output_size)) else: for i in range(self.n_layers + 1): if i == 0: self.layers.add_module('input_layer', nn.Linear( input_size, hidden_layers[0])) self.layers.add_module('input_layer_activation', self. activation()) elif i < self.n_layers: self.layers.add_module(f'hidden_layer_{i}', nn.Linear( hidden_layers[i - 1], hidden_layers[i])) self.layers.add_module(f'input_layer_{i}_activation', self.activation()) else: self.layers.add_module('output_layer', nn.Linear( hidden_layers[i - 1], output_size)) def forward(self, x: 'torch.Tensor') ->torch.Tensor: assert len(x.shape) == 2 assert x.shape[1] == self.input_size return self.layers(x) class TanhGaussianPolicyNew(nn.Module): LOG_STD_MIN: 'float' = -20.0 LOG_STD_MAX: 'float' = 2.0 EPS: 'float' = 1e-06 def __init__(self, n_agents: 'int', obs_size: 'int', act_size: 'int', hidden_layers: 'List[int]', activation: 'nn.Module'=nn.LeakyReLU): super(TanhGaussianPolicyNew, self).__init__() self.n_agents = n_agents self.obs_size = obs_size self.act_size = act_size self.hidden_layers = hidden_layers self.activation = activation self.input_size = obs_size self.output_size = 2 * act_size self.policy = LinearNetwork(self.input_size, self.output_size, hidden_layers, activation) def sample(self, obs: 'torch.Tensor') ->Tuple[torch.Tensor, torch. Tensor, torch.Tensor]: assert len(obs.shape) == 3 assert obs.shape[1] == self.n_agents assert obs.shape[2] == self.obs_size N = obs.shape[0] means = torch.empty((N, self.n_agents, self.act_size)) stds = torch.empty((N, self.n_agents, self.act_size)) for i in range(self.n_agents): mean, log_std = self.forward(obs[:, i]) std = log_std.exp() means[:, i] = mean stds[:, i] = std dist = torch.distributions.normal.Normal(means, stds) act_sampled = dist.rsample() act_sampled_tanh = torch.tanh(act_sampled) log_probs = dist.log_prob(act_sampled) - torch.log(1 - act_sampled_tanh.square() + self.EPS) entropies = -log_probs.sum(dim=-1, keepdim=False) return act_sampled_tanh, entropies, torch.tanh(means) def forward(self, input_0): primals_1 = self.policy.layers.input_layer.weight primals_3 = self.policy.layers.input_layer.bias primals_2 = self.policy.layers.hidden_layer_1.weight primals_5 = self.policy.layers.hidden_layer_1.bias primals_6 = self.policy.layers.output_layer.weight primals_7 = self.policy.layers.output_layer.bias primals_4 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0], output[1]
redvinaa/multiagent-path-finding-continuous
TanhGaussianPolicy
false
10,722
[ "MIT" ]
0
2d4ba3388f9b951c443ba72a33bd7af4f461275f
https://github.com/redvinaa/multiagent-path-finding-continuous/tree/2d4ba3388f9b951c443ba72a33bd7af4f461275f
AuxiliaryConvolutions
import torch from torch import nn import torch.nn.functional as F from itertools import product as product import torch.optim import torch.utils.data class AuxiliaryConvolutions(nn.Module): """ Additional convolutions to produce higher-level feature maps. """ def __init__(self): super(AuxiliaryConvolutions, self).__init__() self.conv8_1 = nn.Conv2d(1024, 256, kernel_size=1, padding=0) self.conv8_2 = nn.Conv2d(256, 512, kernel_size=3, stride=2, padding=1) self.conv9_1 = nn.Conv2d(512, 128, kernel_size=1, padding=0) self.conv9_2 = nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1) self.conv10_1 = nn.Conv2d(256, 128, kernel_size=1, padding=0) self.conv10_2 = nn.Conv2d(128, 256, kernel_size=3, padding=0) self.conv11_1 = nn.Conv2d(256, 128, kernel_size=1, padding=0) self.conv11_2 = nn.Conv2d(128, 256, kernel_size=3, padding=0) self.init_conv2d() def init_conv2d(self): """ Initialize convolution parameters. """ for c in self.children(): if isinstance(c, nn.Conv2d): nn.init.xavier_uniform_(c.weight) nn.init.constant_(c.bias, 0.0) def forward(self, conv7_feats): """ Forward propagation. :param conv7_feats: lower-level conv7 feature map, a tensor of dimensions (N, 1024, 19, 19) :return: higher-level feature maps conv8_2, conv9_2, conv10_2, and conv11_2 """ out = F.relu(self.conv8_1(conv7_feats)) out = F.relu(self.conv8_2(out)) conv8_2_feats = out out = F.relu(self.conv9_1(out)) out = F.relu(self.conv9_2(out)) conv9_2_feats = out out = F.relu(self.conv10_1(out)) out = F.relu(self.conv10_2(out)) conv10_2_feats = out out = F.relu(self.conv11_1(out)) conv11_2_feats = F.relu(self.conv11_2(out)) return conv8_2_feats, conv9_2_feats, conv10_2_feats, conv11_2_feats def get_inputs(): return [torch.rand([4, 1024, 64, 64])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn from itertools import product as product 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_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): 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] tl.full([XBLOCK, YBLOCK], True, tl.int1) x2 = xindex y3 = yindex y0 = yindex % 1024 y1 = yindex // 1024 tmp0 = tl.load(in_ptr0 + (x2 + 4096 * y3), None) tl.store(out_ptr0 + (y0 + 1024 * x2 + 4194304 * y1), tmp0, None) @triton.jit def triton_poi_fused_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 9 yoffset = (tl.program_id(1) + tl.program_id(2) * tl.num_programs(1) ) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 256 y1 = yindex // 256 tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last' ) tl.store(out_ptr0 + (y0 + 256 * x2 + 2304 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_2(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): xnumel = 9 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 128 y1 = yindex // 128 tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask, eviction_policy='evict_last' ) tl.store(out_ptr0 + (y0 + 128 * x2 + 1152 * y1), tmp0, xmask) @triton.jit def triton_poi_fused_convolution_relu_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) x2 = xindex x0 = xindex % 256 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, None) @triton.jit def triton_poi_fused_convolution_relu_4(in_ptr0, in_ptr1, out_ptr0, out_ptr1, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): xnumel = 1024 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 % 512 y1 = yindex // 512 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 512 * x2 + 524288 * y1), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1, 1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(out_ptr0 + (x2 + 1024 * y3), tmp4, xmask) tl.store(out_ptr1 + (y0 + 512 * x2 + 524288 * y1), tmp4, xmask) @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) x2 = xindex x0 = xindex % 128 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, None) @triton.jit def triton_poi_fused_convolution_relu_6(in_ptr0, in_ptr1, out_ptr0, out_ptr1, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): xnumel = 256 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 256 y1 = yindex // 256 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 256 * x2 + 65536 * y1), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1, 1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(out_ptr0 + (x2 + 256 * y3), tmp4, xmask) tl.store(out_ptr1 + (y0 + 256 * x2 + 65536 * y1), tmp4, xmask) @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) x2 = xindex x0 = xindex % 128 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, None) @triton.jit def triton_poi_fused_convolution_relu_8(in_ptr0, in_ptr1, out_ptr0, out_ptr1, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): xnumel = 196 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 256 y1 = yindex // 256 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 256 * x2 + 50176 * y1), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1, 1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(out_ptr0 + (x2 + 196 * y3), tmp4, xmask) tl.store(out_ptr1 + (y0 + 256 * x2 + 50176 * y1), tmp4, xmask) @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) x2 = xindex x0 = xindex % 128 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, None) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_10(in_ptr0, in_ptr1, out_ptr0, out_ptr1, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): xnumel = 144 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] tl.full([XBLOCK, YBLOCK], True, tl.int1) xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 256 y1 = yindex // 256 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 256 * x2 + 36864 * y1), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1, 1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(out_ptr0 + (x2 + 144 * y3), tmp4, xmask) tl.store(out_ptr1 + (y0 + 256 * x2 + 36864 * y1), tmp6, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16, primals_17) = args args.clear() assert_size_stride(primals_1, (256, 1024, 1, 1), (1024, 1, 1, 1)) assert_size_stride(primals_2, (256,), (1,)) assert_size_stride(primals_3, (4, 1024, 64, 64), (4194304, 4096, 64, 1)) assert_size_stride(primals_4, (512, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_5, (512,), (1,)) assert_size_stride(primals_6, (128, 512, 1, 1), (512, 1, 1, 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, (128, 256, 1, 1), (256, 1, 1, 1)) assert_size_stride(primals_11, (128,), (1,)) assert_size_stride(primals_12, (256, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_13, (256,), (1,)) assert_size_stride(primals_14, (128, 256, 1, 1), (256, 1, 1, 1)) assert_size_stride(primals_15, (128,), (1,)) assert_size_stride(primals_16, (256, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_17, (256,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 1024, 64, 64), (4194304, 1, 65536, 1024), torch.float32) get_raw_stream(0) triton_poi_fused_0[grid(4096, 4096)](primals_3, buf0, 4096, 4096, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1) del primals_3 buf1 = empty_strided_cuda((512, 256, 3, 3), (2304, 1, 768, 256), torch.float32) triton_poi_fused_1[grid(131072, 9)](primals_4, buf1, 131072, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_4 buf2 = empty_strided_cuda((256, 128, 3, 3), (1152, 1, 384, 128), torch.float32) triton_poi_fused_2[grid(32768, 9)](primals_8, buf2, 32768, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_8 buf3 = empty_strided_cuda((256, 128, 3, 3), (1152, 1, 384, 128), torch.float32) triton_poi_fused_2[grid(32768, 9)](primals_12, buf3, 32768, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_12 buf4 = empty_strided_cuda((256, 128, 3, 3), (1152, 1, 384, 128), torch.float32) triton_poi_fused_2[grid(32768, 9)](primals_16, buf4, 32768, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_16 buf5 = extern_kernels.convolution(buf0, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf5, (4, 256, 64, 64), (1048576, 1, 16384, 256)) buf6 = buf5 del buf5 triton_poi_fused_convolution_relu_3[grid(4194304)](buf6, primals_2, 4194304, XBLOCK=1024, num_warps=4, num_stages=1) del primals_2 buf7 = extern_kernels.convolution(buf6, buf1, stride=(2, 2), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf7, (4, 512, 32, 32), (524288, 1, 16384, 512)) buf8 = empty_strided_cuda((4, 512, 32, 32), (524288, 1024, 32, 1), torch.float32) buf9 = empty_strided_cuda((4, 512, 32, 32), (524288, 1, 16384, 512), torch.float32) triton_poi_fused_convolution_relu_4[grid(2048, 1024)](buf7, primals_5, buf8, buf9, 2048, 1024, XBLOCK=64, YBLOCK=64, num_warps=8, num_stages=1) del buf7 del primals_5 buf10 = extern_kernels.convolution(buf9, primals_6, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf10, (4, 128, 32, 32), (131072, 1, 4096, 128)) del buf9 buf11 = buf10 del buf10 triton_poi_fused_convolution_relu_5[grid(524288)](buf11, primals_7, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del primals_7 buf12 = extern_kernels.convolution(buf11, buf2, stride=(2, 2), 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, 1, 4096, 256)) buf13 = empty_strided_cuda((4, 256, 16, 16), (65536, 256, 16, 1), torch.float32) buf14 = empty_strided_cuda((4, 256, 16, 16), (65536, 1, 4096, 256), torch.float32) triton_poi_fused_convolution_relu_6[grid(1024, 256)](buf12, primals_9, buf13, buf14, 1024, 256, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1) del buf12 del primals_9 buf15 = extern_kernels.convolution(buf14, primals_10, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf15, (4, 128, 16, 16), (32768, 1, 2048, 128)) del buf14 buf16 = buf15 del buf15 triton_poi_fused_convolution_relu_7[grid(131072)](buf16, primals_11, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del primals_11 buf17 = extern_kernels.convolution(buf16, buf3, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf17, (4, 256, 14, 14), (50176, 1, 3584, 256)) buf18 = empty_strided_cuda((4, 256, 14, 14), (50176, 196, 14, 1), torch.float32) buf19 = empty_strided_cuda((4, 256, 14, 14), (50176, 1, 3584, 256), torch.float32) triton_poi_fused_convolution_relu_8[grid(1024, 196)](buf17, primals_13, buf18, buf19, 1024, 196, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1) del buf17 del primals_13 buf20 = extern_kernels.convolution(buf19, primals_14, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf20, (4, 128, 14, 14), (25088, 1, 1792, 128)) del buf19 buf21 = buf20 del buf20 triton_poi_fused_convolution_relu_9[grid(100352)](buf21, primals_15, 100352, XBLOCK=1024, num_warps=4, num_stages=1) del primals_15 buf22 = extern_kernels.convolution(buf21, buf4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf22, (4, 256, 12, 12), (36864, 1, 3072, 256)) buf23 = empty_strided_cuda((4, 256, 12, 12), (36864, 144, 12, 1), torch.float32) buf24 = empty_strided_cuda((4, 256, 12, 12), (36864, 1, 3072, 256), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_10[grid(1024, 144) ](buf22, primals_17, buf23, buf24, 1024, 144, XBLOCK=64, YBLOCK =64, num_warps=8, num_stages=1) del buf22 del primals_17 return (buf8, buf13, buf18, buf23, primals_1, buf0, buf1, primals_6, buf2, primals_10, buf3, primals_14, buf4, buf6, buf8, buf11, buf13, buf16, buf18, buf21, buf24) class AuxiliaryConvolutionsNew(nn.Module): """ Additional convolutions to produce higher-level feature maps. """ def __init__(self): super(AuxiliaryConvolutionsNew, self).__init__() self.conv8_1 = nn.Conv2d(1024, 256, kernel_size=1, padding=0) self.conv8_2 = nn.Conv2d(256, 512, kernel_size=3, stride=2, padding=1) self.conv9_1 = nn.Conv2d(512, 128, kernel_size=1, padding=0) self.conv9_2 = nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1) self.conv10_1 = nn.Conv2d(256, 128, kernel_size=1, padding=0) self.conv10_2 = nn.Conv2d(128, 256, kernel_size=3, padding=0) self.conv11_1 = nn.Conv2d(256, 128, kernel_size=1, padding=0) self.conv11_2 = nn.Conv2d(128, 256, kernel_size=3, padding=0) self.init_conv2d() def init_conv2d(self): """ Initialize convolution parameters. """ for c in self.children(): if isinstance(c, nn.Conv2d): nn.init.xavier_uniform_(c.weight) nn.init.constant_(c.bias, 0.0) def forward(self, input_0): primals_1 = self.conv8_1.weight primals_2 = self.conv8_1.bias primals_4 = self.conv8_2.weight primals_5 = self.conv8_2.bias primals_6 = self.conv9_1.weight primals_7 = self.conv9_1.bias primals_8 = self.conv9_2.weight primals_9 = self.conv9_2.bias primals_10 = self.conv10_1.weight primals_11 = self.conv10_1.bias primals_12 = self.conv10_2.weight primals_13 = self.conv10_2.bias primals_14 = self.conv11_1.weight primals_15 = self.conv11_1.bias primals_16 = self.conv11_2.weight primals_17 = self.conv11_2.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]) return output[0], output[1], output[2], output[3]
mosevg/ssd
AuxiliaryConvolutions
false
10,723
[ "MIT" ]
0
8fd9f6cc376c027427531bcf475188ae43c4b2d6
https://github.com/mosevg/ssd/tree/8fd9f6cc376c027427531bcf475188ae43c4b2d6
ResidualBlock
import torch import torch.nn as nn import torch.nn.parallel import torch.utils.data class avgpool(nn.Module): """ Mean pooling class - downsampling """ def __init__(self, up_size=0): super(avgpool, self).__init__() def forward(self, x): out_man = (x[:, :, ::2, ::2] + x[:, :, 1::2, ::2] + x[:, :, ::2, 1: :2] + x[:, :, 1::2, 1::2]) / 4 return out_man class ResidualBlock(nn.Module): """ Residual block class 3 types: upsample, downsample, None """ def __init__(self, in_dim, out_dim, resample=None, up_size=0): super(ResidualBlock, self).__init__() if resample == 'up': self.bn1 = nn.BatchNorm2d(in_dim) self.conv1 = nn.Conv2d(in_dim, out_dim, 3, 1, 1, bias=True) self.upsample = torch.nn.Upsample(up_size, 2) self.upsample = torch.nn.Upsample(scale_factor=2) self.upsample_conv = nn.Conv2d(in_dim, out_dim, 1, 1, 0, bias=True) self.conv2 = nn.Conv2d(out_dim, out_dim, 3, 1, 1, bias=True) self.bn2 = nn.BatchNorm2d(out_dim) elif resample == 'down': self.conv1 = nn.Conv2d(in_dim, out_dim, 3, 1, 1, bias=True) self.conv2 = nn.Conv2d(out_dim, out_dim, 3, 1, 1, bias=True) self.pool = avgpool() self.pool_conv = nn.Conv2d(in_dim, out_dim, 1, 1, 0, bias=True) elif resample is None: self.conv1 = nn.Conv2d(in_dim, out_dim, 3, 1, 1, bias=True) self.conv2 = nn.Conv2d(out_dim, out_dim, 3, 1, 1, bias=True) self.resample = resample def forward(self, x): if self.resample is None: shortcut = x output = x output = nn.functional.relu(output) output = self.conv1(output) output = nn.functional.relu(output) output = self.conv2(output) elif self.resample == 'up': shortcut = x output = x shortcut = self.upsample(shortcut) shortcut = self.upsample_conv(shortcut) output = self.bn1(output) output = nn.functional.relu(output) output = self.conv1(output) output = self.bn2(output) output = nn.functional.relu(output) output = self.upsample(output) output = self.conv2(output) elif self.resample == 'down': shortcut = x output = x shortcut = self.pool_conv(shortcut) shortcut = self.pool(shortcut) output = nn.functional.relu(output) output = self.conv1(output) output = nn.functional.relu(output) output = self.conv2(output) output = self.pool(output) return output + shortcut def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_dim': 4, 'out_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn import torch.nn.parallel 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_relu_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.full([1], 0, tl.int32) tmp2 = triton_helpers.maximum(tmp1, tmp0) tl.store(out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_poi_fused_convolution_relu_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, xmask) @triton.jit def triton_poi_fused_add_convolution_2(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x3, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tl.store(in_out_ptr0 + x3, tmp4, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_relu_0[grid(256)](primals_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 4, 4), (64, 16, 4, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_relu_1[grid(256)](buf2, primals_3, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_3 buf3 = 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(buf3, (4, 4, 4, 4), (64, 16, 4, 1)) buf4 = buf3 del buf3 triton_poi_fused_add_convolution_2[grid(256)](buf4, primals_5, primals_1, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 del primals_5 return buf4, primals_2, primals_4, buf0, buf2 class avgpool(nn.Module): """ Mean pooling class - downsampling """ def __init__(self, up_size=0): super(avgpool, self).__init__() def forward(self, x): out_man = (x[:, :, ::2, ::2] + x[:, :, 1::2, ::2] + x[:, :, ::2, 1: :2] + x[:, :, 1::2, 1::2]) / 4 return out_man class ResidualBlockNew(nn.Module): """ Residual block class 3 types: upsample, downsample, None """ def __init__(self, in_dim, out_dim, resample=None, up_size=0): super(ResidualBlockNew, self).__init__() if resample == 'up': self.bn1 = nn.BatchNorm2d(in_dim) self.conv1 = nn.Conv2d(in_dim, out_dim, 3, 1, 1, bias=True) self.upsample = torch.nn.Upsample(up_size, 2) self.upsample = torch.nn.Upsample(scale_factor=2) self.upsample_conv = nn.Conv2d(in_dim, out_dim, 1, 1, 0, bias=True) self.conv2 = nn.Conv2d(out_dim, out_dim, 3, 1, 1, bias=True) self.bn2 = nn.BatchNorm2d(out_dim) elif resample == 'down': self.conv1 = nn.Conv2d(in_dim, out_dim, 3, 1, 1, bias=True) self.conv2 = nn.Conv2d(out_dim, out_dim, 3, 1, 1, bias=True) self.pool = avgpool() self.pool_conv = nn.Conv2d(in_dim, out_dim, 1, 1, 0, bias=True) elif resample is None: self.conv1 = nn.Conv2d(in_dim, out_dim, 3, 1, 1, bias=True) self.conv2 = nn.Conv2d(out_dim, out_dim, 3, 1, 1, bias=True) self.resample = resample 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]
nathalia-kim/nu_gan
ResidualBlock
false
10,724
[ "MIT" ]
0
c1d0891945bd7ac3d95869db91f490f57f203110
https://github.com/nathalia-kim/nu_gan/tree/c1d0891945bd7ac3d95869db91f490f57f203110
ResidualBlock_thefirstone
import torch import torch.nn as nn import torch.nn.parallel import torch.utils.data class avgpool(nn.Module): """ Mean pooling class - downsampling """ def __init__(self, up_size=0): super(avgpool, self).__init__() def forward(self, x): out_man = (x[:, :, ::2, ::2] + x[:, :, 1::2, ::2] + x[:, :, ::2, 1: :2] + x[:, :, 1::2, 1::2]) / 4 return out_man class ResidualBlock_thefirstone(nn.Module): """ First residual block class """ def __init__(self, in_dim, out_dim, resample=None, up_size=0): super(ResidualBlock_thefirstone, self).__init__() self.conv1 = nn.Conv2d(in_dim, out_dim, 3, 1, 1, bias=True) self.conv2 = nn.Conv2d(out_dim, out_dim, 3, 1, 1, bias=True) self.pool = avgpool() self.pool_conv = nn.Conv2d(in_dim, out_dim, 1, 1, 0, bias=True) def forward(self, x): shortcut = x output = x shortcut = self.pool(shortcut) shortcut = self.pool_conv(shortcut) output = self.conv1(output) output = nn.functional.relu(output) output = self.conv2(output) output = self.pool(output) return output + shortcut def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_dim': 4, 'out_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn import torch.nn.parallel 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_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 % 2 x1 = xindex // 2 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (4 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (1 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (5 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 0.25 tmp8 = tmp6 * tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_convolution_relu_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, xmask) @triton.jit def triton_poi_fused_add_convolution_div_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 2 x4 = xindex // 2 x2 = xindex // 4 % 4 x5 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 8 * x4), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (4 + 2 * x0 + 8 * x4), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (1 + 2 * x0 + 8 * x4), xmask, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr0 + (5 + 2 * x0 + 8 * x4), xmask, eviction_policy= 'evict_last') tmp14 = tl.load(in_out_ptr0 + x5, xmask) tmp15 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp3 + tmp1 tmp5 = tmp2 + tmp4 tmp7 = tmp6 + tmp1 tmp8 = tmp5 + tmp7 tmp10 = tmp9 + tmp1 tmp11 = tmp8 + tmp10 tmp12 = 0.25 tmp13 = tmp11 * tmp12 tmp16 = tmp14 + tmp15 tmp17 = tmp13 + tmp16 tl.store(in_out_ptr0 + x5, tmp17, 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, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_0[grid(64)](primals_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=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, 2, 2), (16, 4, 2, 1)) buf2 = extern_kernels.convolution(primals_1, primals_4, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 4, 4, 4), (64, 16, 4, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_relu_1[grid(256)](buf3, primals_5, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf4 = extern_kernels.convolution(buf3, primals_6, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 4, 4, 4), (64, 16, 4, 1)) buf5 = buf1 del buf1 triton_poi_fused_add_convolution_div_2[grid(64)](buf5, buf4, primals_7, primals_3, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf4 del primals_3 del primals_7 return buf5, primals_1, primals_2, primals_4, primals_6, buf0, buf3 class avgpool(nn.Module): """ Mean pooling class - downsampling """ def __init__(self, up_size=0): super(avgpool, self).__init__() def forward(self, x): out_man = (x[:, :, ::2, ::2] + x[:, :, 1::2, ::2] + x[:, :, ::2, 1: :2] + x[:, :, 1::2, 1::2]) / 4 return out_man class ResidualBlock_thefirstoneNew(nn.Module): """ First residual block class """ def __init__(self, in_dim, out_dim, resample=None, up_size=0): super(ResidualBlock_thefirstoneNew, self).__init__() self.conv1 = nn.Conv2d(in_dim, out_dim, 3, 1, 1, bias=True) self.conv2 = nn.Conv2d(out_dim, out_dim, 3, 1, 1, bias=True) self.pool = avgpool() self.pool_conv = nn.Conv2d(in_dim, out_dim, 1, 1, 0, bias=True) def forward(self, input_0): primals_4 = self.conv1.weight primals_3 = self.conv1.bias primals_6 = self.conv2.weight primals_5 = self.conv2.bias primals_2 = self.pool_conv.weight primals_7 = self.pool_conv.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
nathalia-kim/nu_gan
ResidualBlock_thefirstone
false
10,725
[ "MIT" ]
0
c1d0891945bd7ac3d95869db91f490f57f203110
https://github.com/nathalia-kim/nu_gan/tree/c1d0891945bd7ac3d95869db91f490f57f203110
AdaptiveReLU
import torch from torch.nn.parameter import Parameter class AdaptiveReLU(torch.nn.Module): """ Implementation of soft exponential activation. Shape: - Input: (N, *) where * means, any number of additional dimensions - Output: (N, *), same shape as the input Parameters: - alpha - trainable parameter References: - See related paper: https://arxiv.org/pdf/1602.01321.pdf Examples: >>> a1 = soft_exponential(256) >>> x = torch.randn(256) >>> x = a1(x) """ def __init__(self): """ Initialization. INPUT: - in_features: shape of the input - aplha: trainable parameter aplha is initialized with zero value by default """ super(AdaptiveReLU, self).__init__() self.scale = Parameter(torch.rand(1)) self.scale.requiresGrad = True self.translate = Parameter(torch.rand(1)) self.translate.requiresGrad = True def forward(self, x): """ Forward pass of the function. Applies the function to the input elementwise. """ return torch.relu(x + self.translate) * self.scale 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.nn.parameter import Parameter assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_mul_relu_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr1 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp6 = tl.load(in_ptr2 + 0) tmp7 = tl.broadcast_to(tmp6, [XBLOCK]) tmp3 = tmp0 + tmp2 tmp4 = tl.full([1], 0, tl.int32) tmp5 = triton_helpers.maximum(tmp4, tmp3) tmp8 = tmp5 * tmp7 tl.store(out_ptr0 + x0, tmp8, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (1,), (1,)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mul_relu_0[grid(256)](primals_2, primals_1, primals_3, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) return buf0, primals_1, primals_2, primals_3 class AdaptiveReLUNew(torch.nn.Module): """ Implementation of soft exponential activation. Shape: - Input: (N, *) where * means, any number of additional dimensions - Output: (N, *), same shape as the input Parameters: - alpha - trainable parameter References: - See related paper: https://arxiv.org/pdf/1602.01321.pdf Examples: >>> a1 = soft_exponential(256) >>> x = torch.randn(256) >>> x = a1(x) """ def __init__(self): """ Initialization. INPUT: - in_features: shape of the input - aplha: trainable parameter aplha is initialized with zero value by default """ super(AdaptiveReLUNew, self).__init__() self.scale = Parameter(torch.rand(1)) self.scale.requiresGrad = True self.translate = Parameter(torch.rand(1)) self.translate.requiresGrad = True def forward(self, input_0): primals_1 = self.scale primals_3 = self.translate primals_2 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
ndem0/PINA
AdaptiveReLU
false
10,726
[ "MIT" ]
0
1812ddb8d96a9c8aeb80ce35002dbd115e7d7931
https://github.com/ndem0/PINA/tree/1812ddb8d96a9c8aeb80ce35002dbd115e7d7931
AdaptiveTanh
import torch from torch.nn.parameter import Parameter class AdaptiveTanh(torch.nn.Module): """ Implementation of soft exponential activation. Shape: - Input: (N, *) where * means, any number of additional dimensions - Output: (N, *), same shape as the input Parameters: - alpha - trainable parameter References: - See related paper: https://arxiv.org/pdf/1602.01321.pdf Examples: >>> a1 = soft_exponential(256) >>> x = torch.randn(256) >>> x = a1(x) """ def __init__(self, alpha=None): """ Initialization. INPUT: - in_features: shape of the input - aplha: trainable parameter aplha is initialized with zero value by default """ super(AdaptiveTanh, self).__init__() if alpha is None: self.alpha = Parameter(torch.tensor(1.0)) else: self.alpha = Parameter(torch.tensor(alpha)) self.alpha.requiresGrad = True self.scale = Parameter(torch.tensor(1.0)) self.scale.requiresGrad = True self.translate = Parameter(torch.tensor(0.0)) self.translate.requiresGrad = True def forward(self, x): """ Forward pass of the function. Applies the function to the input elementwise. """ x += self.translate return self.scale * (torch.exp(self.alpha * x) - torch.exp(-self. alpha * x)) / (torch.exp(self.alpha * x) + torch.exp(-self. alpha * 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 math as tl_math from torch.nn.parameter import Parameter assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_div_exp_mul_neg_sub_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr1 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp4 = tl.load(in_ptr2 + 0) tmp5 = tl.broadcast_to(tmp4, [XBLOCK]) tmp6 = tl.load(in_ptr3 + 0) tmp7 = tl.broadcast_to(tmp6, [XBLOCK]) tmp3 = tmp0 + tmp2 tmp8 = tmp7 * tmp3 tmp9 = tl_math.exp(tmp8) tmp10 = -tmp7 tmp11 = tmp10 * tmp3 tmp12 = tl_math.exp(tmp11) tmp13 = tmp9 - tmp12 tmp14 = tmp5 * tmp13 tmp15 = tmp9 + tmp12 tmp16 = tmp14 / tmp15 tl.store(out_ptr0 + x0, tmp3, xmask) tl.store(out_ptr1 + x0, tmp16, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (), ()) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (), ()) assert_size_stride(primals_4, (), ()) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_exp_mul_neg_sub_0[grid(256)](primals_2, primals_1, primals_3, primals_4, buf0, buf1, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 del primals_2 return buf0, buf1, primals_3, primals_4, buf0 class AdaptiveTanhNew(torch.nn.Module): """ Implementation of soft exponential activation. Shape: - Input: (N, *) where * means, any number of additional dimensions - Output: (N, *), same shape as the input Parameters: - alpha - trainable parameter References: - See related paper: https://arxiv.org/pdf/1602.01321.pdf Examples: >>> a1 = soft_exponential(256) >>> x = torch.randn(256) >>> x = a1(x) """ def __init__(self, alpha=None): """ Initialization. INPUT: - in_features: shape of the input - aplha: trainable parameter aplha is initialized with zero value by default """ super(AdaptiveTanhNew, self).__init__() if alpha is None: self.alpha = Parameter(torch.tensor(1.0)) else: self.alpha = Parameter(torch.tensor(alpha)) self.alpha.requiresGrad = True self.scale = Parameter(torch.tensor(1.0)) self.scale.requiresGrad = True self.translate = Parameter(torch.tensor(0.0)) self.translate.requiresGrad = True def forward(self, input_0): primals_1 = self.alpha primals_3 = self.scale primals_4 = self.translate primals_2 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
ndem0/PINA
AdaptiveTanh
false
10,727
[ "MIT" ]
0
1812ddb8d96a9c8aeb80ce35002dbd115e7d7931
https://github.com/ndem0/PINA/tree/1812ddb8d96a9c8aeb80ce35002dbd115e7d7931
OneParam
import torch from torch import nn import torch.utils.data class OneParam(nn.Module): def __init__(self, xdim, ydim): """This module computes the dynamics at a point x. That is it return the Jacobian matrix where each element is dy_i/dx_j Output is a matrix of size ydim x xdim """ super(OneParam, self).__init__() self.W = nn.Parameter(torch.zeros(xdim, requires_grad=True)) def forward(self, x): out = torch.tanh(self.W * x) return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'xdim': 4, 'ydim': 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 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_mul_tanh_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x2 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, xmask) tmp2 = tmp0 * tmp1 tmp3 = libdevice.tanh(tmp2) tl.store(out_ptr0 + x2, tmp3, xmask) def call(args): primals_1, primals_2 = args args.clear() assert_size_stride(primals_1, (4,), (1,)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_tanh_0[grid(256)](primals_1, primals_2, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 return buf0, primals_2, buf0 class OneParamNew(nn.Module): def __init__(self, xdim, ydim): """This module computes the dynamics at a point x. That is it return the Jacobian matrix where each element is dy_i/dx_j Output is a matrix of size ydim x xdim """ super(OneParamNew, self).__init__() self.W = nn.Parameter(torch.zeros(xdim, requires_grad=True)) def forward(self, input_0): primals_1 = self.W primals_2 = input_0 output = call([primals_1, primals_2]) return output[0]
safwanhossain/grad_constraints
OneParam
false
10,728
[ "MIT" ]
0
fb66f5e01dff6a587e5f1e9b5316f19b4be36ca7
https://github.com/safwanhossain/grad_constraints/tree/fb66f5e01dff6a587e5f1e9b5316f19b4be36ca7
FBetaLoss
import torch import torch.nn as nn class FBetaLoss(nn.Module): def __init__(self, beta=1): super(FBetaLoss, self).__init__() self.eps = 1e-08 self.beta = beta self.beta2 = beta ** 2 return def forward(self, inputs, target): inputs = torch.sigmoid(inputs) tp = (inputs * target).sum(dim=1) precision = tp.div(inputs.sum(dim=1).add(self.eps)) recall = tp.div(target.sum(dim=1).add(self.eps)) fbeta = torch.mean((precision * recall).div(precision.mul(self. beta2) + recall + self.eps).mul(1 + self.beta2)) return 1 - fbeta def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_div_mean_mul_rsub_sigmoid_sum_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 64 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex % 16 r1 = rindex // 16 tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None) tmp2 = tl.load(in_ptr1 + (r0 + 64 * r1), None) tmp4 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None) tmp6 = tl.load(in_ptr1 + (16 + r0 + 64 * r1), None) tmp9 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None) tmp11 = tl.load(in_ptr1 + (32 + r0 + 64 * r1), None) tmp14 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None) tmp16 = tl.load(in_ptr1 + (48 + r0 + 64 * r1), None) tmp1 = tl.sigmoid(tmp0) tmp3 = tmp1 * tmp2 tmp5 = tl.sigmoid(tmp4) tmp7 = tmp5 * tmp6 tmp8 = tmp3 + tmp7 tmp10 = tl.sigmoid(tmp9) tmp12 = tmp10 * tmp11 tmp13 = tmp8 + tmp12 tmp15 = tl.sigmoid(tmp14) tmp17 = tmp15 * tmp16 tmp18 = tmp13 + tmp17 tmp19 = tmp1 + tmp5 tmp20 = tmp19 + tmp10 tmp21 = tmp20 + tmp15 tmp22 = 1e-08 tmp23 = tmp21 + tmp22 tmp24 = tmp18 / tmp23 tmp25 = tmp2 + tmp6 tmp26 = tmp25 + tmp11 tmp27 = tmp26 + tmp16 tmp28 = tmp27 + tmp22 tmp29 = tmp18 / tmp28 tmp30 = tmp24 * tmp29 tmp31 = 1.0 tmp32 = tmp24 * tmp31 tmp33 = tmp32 + tmp29 tmp34 = tmp33 + tmp22 tmp35 = tmp30 / tmp34 tmp36 = 2.0 tmp37 = tmp35 * tmp36 tmp38 = tl.broadcast_to(tmp37, [XBLOCK, RBLOCK]) tmp40 = tl.sum(tmp38, 1)[:, None] tmp41 = 64.0 tmp42 = tmp40 / tmp41 tmp43 = tmp31 - tmp42 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp43, 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) buf3 = empty_strided_cuda((), (), torch.float32) buf4 = buf3 del buf3 get_raw_stream(0) triton_per_fused_add_div_mean_mul_rsub_sigmoid_sum_0[grid(1)](buf4, arg0_1, arg1_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf4, class FBetaLossNew(nn.Module): def __init__(self, beta=1): super(FBetaLossNew, self).__init__() self.eps = 1e-08 self.beta = beta self.beta2 = beta ** 2 return def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
quqixun/ECG-MLC
FBetaLoss
false
10,729
[ "MIT" ]
0
582d68200b79e3b2ac322c1ed17630727e283605
https://github.com/quqixun/ECG-MLC/tree/582d68200b79e3b2ac322c1ed17630727e283605
AdaptiveSoftplus
import torch from torch.nn.parameter import Parameter class AdaptiveSoftplus(torch.nn.Module): """ Implementation of soft exponential activation. Shape: - Input: (N, *) where * means, any number of additional dimensions - Output: (N, *), same shape as the input Parameters: - alpha - trainable parameter References: - See related paper: https://arxiv.org/pdf/1602.01321.pdf Examples: >>> a1 = soft_exponential(256) >>> x = torch.randn(256) >>> x = a1(x) """ def __init__(self): """ Initialization. INPUT: - in_features: shape of the input - aplha: trainable parameter aplha is initialized with zero value by default """ super().__init__() self.soft = torch.nn.Softplus() self.scale = Parameter(torch.rand(1)) self.scale.requiresGrad = True def forward(self, x): """ Forward pass of the function. Applies the function to the input elementwise. """ return self.soft(x) * self.scale 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, math as tl_math from torch.nn.parameter import Parameter assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_mul_softplus_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) tmp9 = tl.load(in_ptr1 + 0) tmp10 = tl.broadcast_to(tmp9, [XBLOCK]) tmp1 = 1.0 tmp2 = tmp0 * tmp1 tmp3 = 20.0 tmp4 = tmp2 > tmp3 tmp5 = tl_math.exp(tmp2) tmp6 = libdevice.log1p(tmp5) tmp7 = tmp6 * tmp1 tmp8 = tl.where(tmp4, tmp0, tmp7) tmp11 = tmp8 * tmp10 tl.store(out_ptr0 + x0, 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, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_softplus_0[grid(256)](primals_1, primals_2, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 return buf0, primals_1 class AdaptiveSoftplusNew(torch.nn.Module): """ Implementation of soft exponential activation. Shape: - Input: (N, *) where * means, any number of additional dimensions - Output: (N, *), same shape as the input Parameters: - alpha - trainable parameter References: - See related paper: https://arxiv.org/pdf/1602.01321.pdf Examples: >>> a1 = soft_exponential(256) >>> x = torch.randn(256) >>> x = a1(x) """ def __init__(self): """ Initialization. INPUT: - in_features: shape of the input - aplha: trainable parameter aplha is initialized with zero value by default """ super().__init__() self.soft = torch.nn.Softplus() self.scale = Parameter(torch.rand(1)) self.scale.requiresGrad = True def forward(self, input_0): primals_2 = self.scale primals_1 = input_0 output = call([primals_1, primals_2]) return output[0]
ndem0/PINA
AdaptiveSoftplus
false
10,730
[ "MIT" ]
0
1812ddb8d96a9c8aeb80ce35002dbd115e7d7931
https://github.com/ndem0/PINA/tree/1812ddb8d96a9c8aeb80ce35002dbd115e7d7931
FocalLoss
import torch import torch.nn as nn import torch.nn.functional as F class FocalLoss(nn.Module): def __init__(self, gamma=1, weight=None, balance=0.75): super(FocalLoss, self).__init__() self.gamma = gamma self.weight = weight self.balance = balance return def forward(self, inputs, target): logpt = -F.binary_cross_entropy_with_logits(input=inputs, target= target, reduction='none') if self.weight is not None: logpt = logpt * self.weight logpt = logpt.mean() pt = torch.exp(logpt) focal_loss = -(1 - pt) ** self.gamma * logpt balanced_focal_loss = self.balance * focal_loss return balanced_focal_loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math 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_binary_cross_entropy_with_logits_exp_mean_mul_neg_rsub_0( in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp3 = tl.load(in_ptr1 + r0, None) tmp1 = 1.0 tmp2 = tmp1 - tmp0 tmp4 = tmp2 * tmp3 tmp5 = 0.0 tmp6 = triton_helpers.minimum(tmp5, tmp3) tmp7 = tl_math.abs(tmp3) tmp8 = -tmp7 tmp9 = tl_math.exp(tmp8) tmp10 = libdevice.log1p(tmp9) tmp11 = tmp6 - tmp10 tmp12 = tmp4 - tmp11 tmp13 = -tmp12 tmp14 = tl.broadcast_to(tmp13, [RBLOCK]) tmp16 = triton_helpers.promote_to_tensor(tl.sum(tmp14, 0)) tmp17 = 256.0 tmp18 = tmp16 / tmp17 tmp19 = tl_math.exp(tmp18) tmp20 = tmp1 - tmp19 tmp21 = -tmp20 tmp22 = tmp21 * tmp18 tmp23 = 0.75 tmp24 = tmp22 * tmp23 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp24, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_binary_cross_entropy_with_logits_exp_mean_mul_neg_rsub_0[ grid(1)](buf1, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf1, class FocalLossNew(nn.Module): def __init__(self, gamma=1, weight=None, balance=0.75): super(FocalLossNew, self).__init__() self.gamma = gamma self.weight = weight self.balance = balance return def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
quqixun/ECG-MLC
FocalLoss
false
10,731
[ "MIT" ]
0
582d68200b79e3b2ac322c1ed17630727e283605
https://github.com/quqixun/ECG-MLC/tree/582d68200b79e3b2ac322c1ed17630727e283605
GeometricLoss
import torch import numpy as np import torch.nn as nn class GeometricLoss(nn.Module): def __init__(self, num_parameters=2, init=[0.0, -3.0]): self.num_parameters = num_parameters super(GeometricLoss, self).__init__() assert len(init) == num_parameters self.weight = nn.Parameter(torch.Tensor(np.array(init))) def forward(self, inpt): return torch.sum(inpt * torch.exp(-self.weight)) + torch.sum(self. weight) def extra_repr(self): return 'num_parameters={}'.format(self.num_parameters) def get_inputs(): return [torch.rand([4, 4, 4, 2])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import math as tl_math import numpy as np import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_exp_mul_neg_sum_0(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 128 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 r0 = rindex % 2 tmp0 = tl.load(in_ptr0 + r2, None) tmp1 = tl.load(in_ptr1 + r0, None, eviction_policy='evict_last') tmp2 = -tmp1 tmp3 = tl_math.exp(tmp2) tmp4 = tmp0 * tmp3 tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK]) tmp7 = tl.sum(tmp5, 1)[:, None] tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp7, None) @triton.jit def triton_per_fused_add_sum_1(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 2 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp4 = tl.load(in_out_ptr0 + 0) tmp5 = tl.broadcast_to(tmp4, [XBLOCK, 1]) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.sum(tmp1, 1)[:, None] tmp6 = tmp5 + tmp3 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp6, None) def call(args): primals_1, primals_2 = args args.clear() assert_size_stride(primals_1, (2,), (1,)) assert_size_stride(primals_2, (4, 4, 4, 2), (32, 8, 2, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) get_raw_stream(0) triton_per_fused_exp_mul_neg_sum_0[grid(1)](primals_2, primals_1, buf0, 1, 128, XBLOCK=1, num_warps=2, num_stages=1) buf2 = buf0 del buf0 triton_per_fused_add_sum_1[grid(1)](buf2, primals_1, 1, 2, XBLOCK=1, num_warps=2, num_stages=1) return buf2, primals_1, primals_2 class GeometricLossNew(nn.Module): def __init__(self, num_parameters=2, init=[0.0, -3.0]): self.num_parameters = num_parameters super(GeometricLossNew, self).__init__() assert len(init) == num_parameters self.weight = nn.Parameter(torch.Tensor(np.array(init))) def extra_repr(self): return 'num_parameters={}'.format(self.num_parameters) def forward(self, input_0): primals_1 = self.weight primals_2 = input_0 output = call([primals_1, primals_2]) return output[0]
sanfengliao/DeepNavi
GeometricLoss
false
10,732
[ "Apache-2.0" ]
0
dc405ac0010075c2eea63083528db7cb765ad161
https://github.com/sanfengliao/DeepNavi/tree/dc405ac0010075c2eea63083528db7cb765ad161
BackgroundRelationModel
import torch import numpy as np from torch import nn from torch.nn.parameter import Parameter class BackgroundRelationModel(nn.Module): def __init__(self, n_bg, n_ml): """ n_bg: number of background tags n_ml: number of ml tags """ super().__init__() self.config = {'n_bg': n_bg, 'n_ml': n_ml} self.W = Parameter(torch.randn(n_bg, n_ml)) self.b = Parameter(torch.randn(n_ml) / 2) nn.init.xavier_uniform_(self.W) if torch.cuda.is_available(): self.device = torch.device('cuda') else: self.device = torch.device('cpu') self def forward(self, alpha_bg): """ The inputs to the forward function should be: the alpha matrix (np array) with columns corresponding to the background tags it should have the shape (batch_size, n_bg) """ alpha_bg_tensor = torch.tensor(alpha_bg) return alpha_bg_tensor @ self.W + self.b def fit(self, alpha_bg, alpha_ml, lr=0.001, epochs=10): """ alpha_bg: the alpha matrix (np array) with columns corresponding to the background tags alpha_ml: the alpha matrix (np array) with columns corresponding to the ML tags lr: learning rate epochs: number of epochs to train """ self.train() optimizer = torch.optim.Adam(self.parameters(), lr=lr) loss_fn = nn.MSELoss() ys = torch.tensor(alpha_ml) for epoch in range(epochs): optimizer.zero_grad() y_hats = self.forward(alpha_bg) loss = loss_fn(y_hats, ys) loss.backward() optimizer.step() None def auto_fit(self, alpha_bg, alpha_ml, val_alpha_bg, val_alpha_ml, lr= 0.001, reg=0.01, patience=10): """ alpha_bg: the alpha matrix (np array) with columns corresponding to the background tags alpha_ml: the alpha matrix (np array) with columns corresponding to the ML tags val_*: same data but from the validation set lr: learning rate reg: weight decay coefficient (similar to L2 penalty) patience: number of epochs to continue evaluating even after loss not decreasing """ self.train() optimizer = torch.optim.Adam(self.parameters(), lr=lr, weight_decay=reg ) loss_fn = nn.MSELoss() epoch = 0 frustration = 0 best_loss = np.inf ys = torch.tensor(alpha_ml) val_ys = torch.tensor(val_alpha_ml) while True: optimizer.zero_grad() y_hats = self.forward(alpha_bg) loss = loss_fn(y_hats, ys) loss.backward() optimizer.step() val_y_hats = self.forward(val_alpha_bg) val_loss = loss_fn(val_y_hats, val_ys) None if val_loss.item() < best_loss: best_loss = val_loss.item() frustration = 0 else: frustration += 1 if frustration > patience: break epoch += 1 def predict(self, alpha_bg): self.eval() return self.forward(alpha_bg).cpu().detach().numpy() def save(self, model_path): model_state = {'state_dict': self.state_dict(), 'config': self.config} torch.save(model_state, model_path) @classmethod def load(cls, model_path): model_state = torch.load(str(model_path), map_location=lambda storage, loc: storage) args = model_state['config'] model = cls(**args) model.load_state_dict(model_state['state_dict']) if torch.cuda.is_available(): model.device = torch.device('cuda') else: model.device = torch.device('cpu') model return model def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'n_bg': 4, 'n_ml': 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 numpy as np from torch import nn from torch.nn.parameter import Parameter assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_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 tl.store(in_out_ptr0 + x2, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), primals_2, out=buf0) del primals_2 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 get_raw_stream(0) triton_poi_fused_add_0[grid(256)](buf1, primals_3, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_3 return buf1, reinterpret_tensor(primals_1, (4, 64), (1, 4), 0) class BackgroundRelationModelNew(nn.Module): def __init__(self, n_bg, n_ml): """ n_bg: number of background tags n_ml: number of ml tags """ super().__init__() self.config = {'n_bg': n_bg, 'n_ml': n_ml} self.W = Parameter(torch.randn(n_bg, n_ml)) self.b = Parameter(torch.randn(n_ml) / 2) nn.init.xavier_uniform_(self.W) if torch.cuda.is_available(): self.device = torch.device('cuda') else: self.device = torch.device('cpu') self def fit(self, alpha_bg, alpha_ml, lr=0.001, epochs=10): """ alpha_bg: the alpha matrix (np array) with columns corresponding to the background tags alpha_ml: the alpha matrix (np array) with columns corresponding to the ML tags lr: learning rate epochs: number of epochs to train """ self.train() optimizer = torch.optim.Adam(self.parameters(), lr=lr) loss_fn = nn.MSELoss() ys = torch.tensor(alpha_ml) for epoch in range(epochs): optimizer.zero_grad() y_hats = self.forward(alpha_bg) loss = loss_fn(y_hats, ys) loss.backward() optimizer.step() None def auto_fit(self, alpha_bg, alpha_ml, val_alpha_bg, val_alpha_ml, lr= 0.001, reg=0.01, patience=10): """ alpha_bg: the alpha matrix (np array) with columns corresponding to the background tags alpha_ml: the alpha matrix (np array) with columns corresponding to the ML tags val_*: same data but from the validation set lr: learning rate reg: weight decay coefficient (similar to L2 penalty) patience: number of epochs to continue evaluating even after loss not decreasing """ self.train() optimizer = torch.optim.Adam(self.parameters(), lr=lr, weight_decay=reg ) loss_fn = nn.MSELoss() epoch = 0 frustration = 0 best_loss = np.inf ys = torch.tensor(alpha_ml) val_ys = torch.tensor(val_alpha_ml) while True: optimizer.zero_grad() y_hats = self.forward(alpha_bg) loss = loss_fn(y_hats, ys) loss.backward() optimizer.step() val_y_hats = self.forward(val_alpha_bg) val_loss = loss_fn(val_y_hats, val_ys) None if val_loss.item() < best_loss: best_loss = val_loss.item() frustration = 0 else: frustration += 1 if frustration > patience: break epoch += 1 def predict(self, alpha_bg): self.eval() return self.forward(alpha_bg).cpu().detach().numpy() def save(self, model_path): model_state = {'state_dict': self.state_dict(), 'config': self.config} torch.save(model_state, model_path) @classmethod def load(cls, model_path): model_state = torch.load(str(model_path), map_location=lambda storage, loc: storage) args = model_state['config'] model = cls(**args) model.load_state_dict(model_state['state_dict']) if torch.cuda.is_available(): model.device = torch.device('cuda') else: model.device = torch.device('cpu') model return model def forward(self, input_0): primals_2 = self.W primals_3 = self.b primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
scott0123/psychometrics
BackgroundRelationModel
false
10,733
[ "MIT" ]
0
1caa451c46b4c2a3b5e17da3dc89b8cfbded1d11
https://github.com/scott0123/psychometrics/tree/1caa451c46b4c2a3b5e17da3dc89b8cfbded1d11
Net
import torch import torch.nn as nn import torch.nn.functional as F class Net(nn.Module): def __init__(self, dropout=False, input_size=4, output_size=2): super().__init__() hidden_layer_size = 32 self.fc1 = nn.Linear(input_size, hidden_layer_size) self.use_dropout = dropout self.fc2 = nn.Linear(hidden_layer_size, hidden_layer_size) self.use_dropout = dropout self.fc3 = nn.Linear(hidden_layer_size, hidden_layer_size // 2) self.use_dropout = dropout self.fc4 = nn.Linear(hidden_layer_size // 2, output_size) def forward(self, x): x = F.relu(self.fc1(x)) if self.use_dropout: x = F.dropout(x, training=self.training) x = F.relu(self.fc2(x)) if self.use_dropout: x = F.dropout(x, training=self.training) x = F.relu(self.fc3(x)) if self.use_dropout: x = F.dropout(x, training=self.training) x = torch.sigmoid(self.fc4(x)) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda 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): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 16 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) @triton.jit def triton_poi_fused_sigmoid_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 128 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 2 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) = args args.clear() assert_size_stride(primals_1, (32, 4), (4, 1)) assert_size_stride(primals_2, (32,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (32, 32), (32, 1)) assert_size_stride(primals_5, (32,), (1,)) assert_size_stride(primals_6, (16, 32), (32, 1)) assert_size_stride(primals_7, (16,), (1,)) assert_size_stride(primals_8, (2, 16), (16, 1)) assert_size_stride(primals_9, (2,), (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 buf10 = 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, buf10, 2048, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 32), (32, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 32), (32, 1), 0), reinterpret_tensor(primals_4, (32, 32), (1, 32), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 32), (512, 128, 32, 1), 0) del buf2 buf9 = empty_strided_cuda((4, 4, 4, 32), (512, 128, 32, 1), torch.bool) triton_poi_fused_relu_threshold_backward_0[grid(2048)](buf3, primals_5, buf9, 2048, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 16), (16, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf3, (64, 32), (32, 1), 0), reinterpret_tensor(primals_6, (32, 16), (1, 32), 0), out=buf4) buf5 = reinterpret_tensor(buf4, (4, 4, 4, 16), (256, 64, 16, 1), 0) del buf4 buf8 = empty_strided_cuda((4, 4, 4, 16), (256, 64, 16, 1), torch.bool) triton_poi_fused_relu_threshold_backward_1[grid(1024)](buf5, primals_7, buf8, 1024, XBLOCK=128, num_warps=4, num_stages=1) del primals_7 buf6 = empty_strided_cuda((64, 2), (2, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf5, (64, 16), (16, 1), 0), reinterpret_tensor(primals_8, (16, 2), (1, 16), 0), out=buf6) buf7 = reinterpret_tensor(buf6, (4, 4, 4, 2), (32, 8, 2, 1), 0) del buf6 triton_poi_fused_sigmoid_2[grid(128)](buf7, primals_9, 128, XBLOCK= 128, num_warps=4, num_stages=1) del primals_9 return buf7, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 32), (32, 1), 0), reinterpret_tensor( buf3, (64, 32), (32, 1), 0), reinterpret_tensor(buf5, (64, 16), (16, 1), 0), buf7, primals_8, buf8, primals_6, buf9, primals_4, buf10 class NetNew(nn.Module): def __init__(self, dropout=False, input_size=4, output_size=2): super().__init__() hidden_layer_size = 32 self.fc1 = nn.Linear(input_size, hidden_layer_size) self.use_dropout = dropout self.fc2 = nn.Linear(hidden_layer_size, hidden_layer_size) self.use_dropout = dropout self.fc3 = nn.Linear(hidden_layer_size, hidden_layer_size // 2) self.use_dropout = dropout self.fc4 = nn.Linear(hidden_layer_size // 2, 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]
sansastra/clustering_ml
Net
false
10,734
[ "Apache-2.0" ]
0
12f65f86432e51c15dbd1af5208fdfe4e454470a
https://github.com/sansastra/clustering_ml/tree/12f65f86432e51c15dbd1af5208fdfe4e454470a
Attention
import torch import numpy as np class Attention(torch.nn.Module): def __init__(self, d_model, heads): super().__init__() self.d_model = d_model self.heads = heads self.query = torch.nn.Linear(in_features=d_model, out_features= d_model, bias=False) self.key = torch.nn.Linear(in_features=d_model, out_features= d_model, bias=False) self.value = torch.nn.Linear(in_features=d_model, out_features= d_model, bias=False) self.dense = torch.nn.Linear(in_features=d_model, out_features= d_model, bias=False) def forward(self, x): assert x.shape[-1] == self.d_model seq_length = x.shape[0] q = self.query(x) k = self.key(x) v = self.value(x) q = q.reshape(seq_length, self.heads, -1) k = k.reshape(seq_length, self.heads, -1) v = v.reshape(seq_length, self.heads, -1) q = q.transpose(0, 1) k = k.transpose(0, 1).transpose(1, 2) v = v.transpose(0, 1) logits = torch.matmul(q, k) logits = logits / np.sqrt(self.d_model // self.heads) logits = torch.softmax(logits, dim=-1) output = torch.matmul(logits, v) output = output.reshape((seq_length, self.d_model)) output = self.dense(output) return output def get_inputs(): return [torch.rand([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 math as tl_math assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf1) del primals_3 buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf2) del primals_4 buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf0, (4, 4, 1), (1, 4, 1), 0 ), reinterpret_tensor(buf1, (4, 1, 4), (1, 1, 4), 0), out=buf3) buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused__softmax_0[grid(64)](buf3, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) buf5 = buf3 del buf3 triton_poi_fused__softmax_1[grid(64)](buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf4 buf6 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) extern_kernels.bmm(buf5, reinterpret_tensor(buf2, (4, 4, 1), (1, 4, 1), 0), out=buf6) buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf6, (4, 4), (4, 1), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf7) return buf7, primals_1, buf5, reinterpret_tensor(buf6, (4, 4), (4, 1), 0 ), primals_5, reinterpret_tensor(buf2, (4, 1, 4), (1, 1, 4), 0 ), reinterpret_tensor(buf0, (4, 1, 4), (1, 1, 4), 0 ), reinterpret_tensor(buf1, (4, 4, 1), (1, 4, 1), 0) class AttentionNew(torch.nn.Module): def __init__(self, d_model, heads): super().__init__() self.d_model = d_model self.heads = heads self.query = torch.nn.Linear(in_features=d_model, out_features= d_model, bias=False) self.key = torch.nn.Linear(in_features=d_model, out_features= d_model, bias=False) self.value = torch.nn.Linear(in_features=d_model, out_features= d_model, bias=False) self.dense = torch.nn.Linear(in_features=d_model, out_features= d_model, bias=False) def forward(self, input_0): primals_1 = self.query.weight primals_2 = self.key.weight primals_3 = self.value.weight primals_4 = self.dense.weight primals_5 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
santhnm2/TASO
Attention
false
10,735
[ "Apache-2.0" ]
0
f8025dda00922e4313ba6efbca6573421d95cbba
https://github.com/santhnm2/TASO/tree/f8025dda00922e4313ba6efbca6573421d95cbba
ILN
import torch import torch.onnx from torch import nn import torch from torch.nn.parameter import Parameter class ILN(nn.Module): def __init__(self, num_features, eps=1e-05): super(ILN, self).__init__() self.eps = eps self.rho = Parameter(torch.Tensor(1, num_features, 1, 1)) self.gamma = Parameter(torch.Tensor(1, num_features, 1, 1)) self.beta = Parameter(torch.Tensor(1, num_features, 1, 1)) self.rho.data.fill_(0.0) self.gamma.data.fill_(1.0) self.beta.data.fill_(0.0) def forward(self, input): in_mean, in_var = torch.mean(input, dim=[2, 3], keepdim=True ), torch.std(input, dim=[2, 3], keepdim=True) ** 2 out_in = (input - in_mean) / torch.sqrt(in_var + self.eps) ln_mean, ln_var = torch.mean(input, dim=[1, 2, 3], keepdim=True ), torch.std(input, dim=[1, 2, 3], keepdim=True) ** 2 out_ln = (input - ln_mean) / torch.sqrt(ln_var + self.eps) out = self.rho.expand(input.shape[0], -1, -1, -1) * out_in + (1 - self.rho.expand(input.shape[0], -1, -1, -1)) * out_ln out = out * self.gamma.expand(input.shape[0], -1, -1, -1 ) + self.beta.expand(input.shape[0], -1, -1, -1) return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_features': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.onnx from torch import nn import torch from torch.nn.parameter import Parameter assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_per_fused_add_mean_pow_sqrt_std_0(in_out_ptr0, in_out_ptr1, in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 RBLOCK: tl.constexpr = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 64 * x0), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, 0) tmp4 = tl.sum(tmp3, 1)[:, None] tmp6 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp8 = tl.where(xmask, tmp6, 0) tmp9 = tl.sum(tmp8, 1)[:, None] tmp10 = tl.full([XBLOCK, 1], 64, tl.int32) tmp11 = tmp10.to(tl.float32) tmp12 = tmp9 / tmp11 tmp13 = tmp1 - tmp12 tmp14 = tmp13 * tmp13 tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK]) tmp17 = tl.where(xmask, tmp15, 0) tmp18 = tl.sum(tmp17, 1)[:, None] tmp19 = 64.0 tmp20 = tmp4 / tmp19 tmp21 = 63.0 tmp22 = tmp18 / tmp21 tmp23 = libdevice.sqrt(tmp22) tmp24 = tmp23 * tmp23 tmp25 = 1e-05 tmp26 = tmp24 + tmp25 tmp27 = libdevice.sqrt(tmp26) tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp20, xmask) tl.debug_barrier() tl.store(in_out_ptr1 + x0, tmp27, xmask) @triton.jit def triton_per_fused_add_div_mean_mul_pow_rsub_sqrt_std_sub_1(in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 16 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex x2 = xindex % 4 x3 = xindex // 4 tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp28 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last') tmp34 = tl.load(in_ptr2 + x3, xmask, eviction_policy='evict_last') tmp36 = tl.load(in_ptr3 + x3, xmask, eviction_policy='evict_last') tmp40 = tl.load(in_ptr4 + x2, xmask, eviction_policy='evict_last') tmp42 = tl.load(in_ptr5 + x2, xmask, eviction_policy='evict_last') tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, 0) tmp4 = tl.sum(tmp3, 1)[:, None] tmp6 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp8 = tl.where(xmask, tmp6, 0) tmp9 = tl.sum(tmp8, 1)[:, None] tmp10 = tl.full([XBLOCK, 1], 16, tl.int32) tmp11 = tmp10.to(tl.float32) tmp12 = tmp9 / tmp11 tmp13 = tmp1 - tmp12 tmp14 = tmp13 * tmp13 tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK]) tmp17 = tl.where(xmask, tmp15, 0) tmp18 = tl.sum(tmp17, 1)[:, None] tmp19 = 16.0 tmp20 = tmp4 / tmp19 tmp21 = 15.0 tmp22 = tmp18 / tmp21 tmp23 = libdevice.sqrt(tmp22) tmp24 = tmp23 * tmp23 tmp25 = 1e-05 tmp26 = tmp24 + tmp25 tmp27 = libdevice.sqrt(tmp26) tmp29 = tmp0 - tmp20 tmp30 = tmp29 / tmp27 tmp31 = tmp28 * tmp30 tmp32 = 1.0 tmp33 = tmp32 - tmp28 tmp35 = tmp0 - tmp34 tmp37 = tmp35 / tmp36 tmp38 = tmp33 * tmp37 tmp39 = tmp31 + tmp38 tmp41 = tmp39 * tmp40 tmp43 = tmp41 + tmp42 tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp20, xmask) tl.debug_barrier() tl.store(in_out_ptr1 + x0, tmp27, xmask) tl.store(out_ptr0 + (r1 + 16 * x0), tmp43, 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, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_3, (1, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_4, (1, 4, 1, 1), (4, 1, 1, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf6 = empty_strided_cuda((4, 1, 1, 1), (1, 4, 4, 4), torch.float32) buf9 = empty_strided_cuda((4, 1, 1, 1), (1, 4, 4, 4), torch.float32) buf7 = reinterpret_tensor(buf6, (4, 1, 1, 1), (1, 1, 1, 1), 0) del buf6 buf11 = reinterpret_tensor(buf9, (4, 1, 1, 1), (1, 1, 1, 1), 0) del buf9 get_raw_stream(0) triton_per_fused_add_mean_pow_sqrt_std_0[grid(4)](buf7, buf11, primals_1, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) buf0 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) buf3 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) buf1 = reinterpret_tensor(buf0, (4, 4, 1, 1), (4, 1, 1, 1), 0) del buf0 buf5 = reinterpret_tensor(buf3, (4, 4, 1, 1), (4, 1, 1, 1), 0) del buf3 buf12 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_per_fused_add_div_mean_mul_pow_rsub_sqrt_std_sub_1[grid(16)]( buf1, buf5, primals_1, primals_2, buf7, buf11, primals_3, primals_4, buf12, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del primals_4 return buf12, primals_1, primals_2, primals_3, buf1, buf5, buf7, buf11 class ILNNew(nn.Module): def __init__(self, num_features, eps=1e-05): super(ILNNew, self).__init__() self.eps = eps self.rho = Parameter(torch.Tensor(1, num_features, 1, 1)) self.gamma = Parameter(torch.Tensor(1, num_features, 1, 1)) self.beta = Parameter(torch.Tensor(1, num_features, 1, 1)) self.rho.data.fill_(0.0) self.gamma.data.fill_(1.0) self.beta.data.fill_(0.0) def forward(self, input_0): primals_2 = self.rho primals_3 = self.gamma primals_4 = self.beta primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
rtolps/Cats2dogs_ONNX
ILN
false
10,736
[ "MIT" ]
0
9c18a9ea9c6ae65feb5c2a1a4c814d31999b6ffc
https://github.com/rtolps/Cats2dogs_ONNX/tree/9c18a9ea9c6ae65feb5c2a1a4c814d31999b6ffc