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
TemperatureHolder
import torch import torch.nn as nn class TemperatureHolder(nn.Module): """Module that holds a temperature as a learnable value. Args: initial_log_temperature (float): Initial value of log(temperature). """ def __init__(self, initial_log_temperature=0): super().__init__() self.log_temperature = nn.Parameter(torch.tensor( initial_log_temperature, dtype=torch.float32)) def forward(self): """Return a temperature as a torch.Tensor.""" return torch.exp(self.log_temperature) def get_inputs(): return [] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import 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_exp_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) tmp0 = tl.load(in_ptr0 + 0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK]) tmp2 = tl_math.exp(tmp1) tl.store(out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp2, None) def call(args): primals_1, = args args.clear() assert_size_stride(primals_1, (), ()) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) get_raw_stream(0) triton_poi_fused_exp_0[grid(1)](primals_1, buf0, 1, XBLOCK=1, num_warps=1, num_stages=1) del primals_1 return buf0, buf0 class TemperatureHolderNew(nn.Module): """Module that holds a temperature as a learnable value. Args: initial_log_temperature (float): Initial value of log(temperature). """ def __init__(self, initial_log_temperature=0): super().__init__() self.log_temperature = nn.Parameter(torch.tensor( initial_log_temperature, dtype=torch.float32)) def forward(self): primals_1 = self.log_temperature output = call([primals_1]) return output[0]
imatge-upc/pixelcoordEDL
TemperatureHolder
false
6,868
[ "MIT" ]
1
353632feed6ac8c93758c1a2a1b7a477e7ff053c
https://github.com/imatge-upc/pixelcoordEDL/tree/353632feed6ac8c93758c1a2a1b7a477e7ff053c
AsymmetricLossOptimized
import torch import torch.nn as nn class AsymmetricLossOptimized(nn.Module): """ Notice - optimized version, minimizes memory allocation and gpu uploading, favors inplace operations""" def __init__(self, gamma_neg=4, gamma_pos=1, clip=0.05, eps=1e-08, disable_torch_grad_focal_loss=False): super(AsymmetricLossOptimized, self).__init__() self.gamma_neg = gamma_neg self.gamma_pos = gamma_pos self.clip = clip self.disable_torch_grad_focal_loss = disable_torch_grad_focal_loss self.eps = eps (self.targets) = (self.anti_targets) = (self.xs_pos) = (self.xs_neg ) = (self.asymmetric_w) = (self.loss) = None def forward(self, x, y): """" Parameters ---------- x: input logits y: targets (multi-label binarized vector) """ self.targets = y self.anti_targets = 1 - y self.xs_pos = torch.sigmoid(x) self.xs_neg = 1.0 - self.xs_pos if self.clip is not None and self.clip > 0: self.xs_neg.add_(self.clip).clamp_(max=1) self.loss = self.targets * torch.log(self.xs_pos.clamp(min=self.eps)) self.loss.add_(self.anti_targets * torch.log(self.xs_neg.clamp(min= self.eps))) if self.gamma_neg > 0 or self.gamma_pos > 0: if self.disable_torch_grad_focal_loss: torch._C.set_grad_enabled(False) self.xs_pos = self.xs_pos * self.targets self.xs_neg = self.xs_neg * self.anti_targets self.asymmetric_w = torch.pow(1 - self.xs_pos - self.xs_neg, self.gamma_pos * self.targets + self.gamma_neg * self. anti_targets) if self.disable_torch_grad_focal_loss: torch._C.set_grad_enabled(True) self.loss *= self.asymmetric_w return -self.loss.sum() def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_clamp_log_mul_neg_pow_rsub_sigmoid_sub_sum_0( in_out_ptr0, in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr4, 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 = tl.sigmoid(tmp3) tmp5 = tmp4 * tmp0 tmp6 = tmp1 - tmp4 tmp7 = 0.05 tmp8 = tmp6 + tmp7 tmp9 = triton_helpers.minimum(tmp8, tmp1) tmp10 = tmp9 * tmp2 tmp11 = tmp1 - tmp5 tmp12 = tmp11 - tmp10 tmp13 = tmp0 * tmp1 tmp14 = 4.0 tmp15 = tmp2 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = libdevice.pow(tmp12, tmp16) tmp18 = 1e-08 tmp19 = triton_helpers.maximum(tmp4, tmp18) tmp20 = tl_math.log(tmp19) tmp21 = tmp0 * tmp20 tmp22 = triton_helpers.maximum(tmp9, tmp18) tmp23 = tl_math.log(tmp22) tmp24 = tmp2 * tmp23 tmp25 = tmp21 + tmp24 tmp26 = tmp25 * tmp17 tmp27 = tl.broadcast_to(tmp26, [RBLOCK]) tmp29 = triton_helpers.promote_to_tensor(tl.sum(tmp27, 0)) tmp30 = -tmp29 tl.store(out_ptr0 + tl.broadcast_to(r0, [RBLOCK]), tmp2, None) tl.store(out_ptr1 + tl.broadcast_to(r0, [RBLOCK]), tmp5, None) tl.store(out_ptr2 + tl.broadcast_to(r0, [RBLOCK]), tmp10, None) tl.store(out_ptr3 + tl.broadcast_to(r0, [RBLOCK]), tmp17, None) tl.store(out_ptr4 + tl.broadcast_to(r0, [RBLOCK]), tmp26, None) tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp30, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) 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) buf5 = empty_strided_cuda((), (), torch.float32) buf6 = buf5 del buf5 get_raw_stream(0) triton_per_fused_add_clamp_log_mul_neg_pow_rsub_sigmoid_sub_sum_0[grid (1)](buf6, arg0_1, arg1_1, buf0, buf1, buf2, buf3, buf4, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf6, buf3, buf4, buf2, buf1, buf0 class AsymmetricLossOptimizedNew(nn.Module): """ Notice - optimized version, minimizes memory allocation and gpu uploading, favors inplace operations""" def __init__(self, gamma_neg=4, gamma_pos=1, clip=0.05, eps=1e-08, disable_torch_grad_focal_loss=False): super(AsymmetricLossOptimizedNew, self).__init__() self.gamma_neg = gamma_neg self.gamma_pos = gamma_pos self.clip = clip self.disable_torch_grad_focal_loss = disable_torch_grad_focal_loss self.eps = eps (self.targets) = (self.anti_targets) = (self.xs_pos) = (self.xs_neg ) = (self.asymmetric_w) = (self.loss) = None def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
imvladikon/pytorch-loss
AsymmetricLossOptimized
false
6,869
[ "MIT" ]
1
6cfaabe1be898e1ff000b3dffb46d0ef09096f6b
https://github.com/imvladikon/pytorch-loss/tree/6cfaabe1be898e1ff000b3dffb46d0ef09096f6b
Self_Attentive_Pooling
import torch import torch.nn as nn import torch.nn.functional as F class Self_Attentive_Pooling(nn.Module): def __init__(self, dim): """SAP Paper: Self-Attentive Speaker Embeddings for Text-Independent Speaker Verification Link: https://danielpovey.com/files/2018_interspeech_xvector_attention.pdf Args: dim (pair): the size of attention weights """ super(Self_Attentive_Pooling, self).__init__() self.sap_linear = nn.Linear(dim, dim) self.attention = nn.Parameter(torch.FloatTensor(dim, 1)) def forward(self, x): """Computes Self-Attentive Pooling Module Args: x (torch.Tensor): Input tensor (#batch, dim, frames). Returns: torch.Tensor: Output tensor (#batch, dim) """ x = x.permute(0, 2, 1) h = torch.tanh(self.sap_linear(x)) w = torch.matmul(h, self.attention).squeeze(dim=2) w = F.softmax(w, dim=1).view(x.size(0), x.size(1), 1) x = torch.sum(x * w, dim=1) return x def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clone_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_add_tanh_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = libdevice.tanh(tmp2) tl.store(in_out_ptr0 + x2, tmp3, xmask) @triton.jit def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_mul_sum_4(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (3 + 4 * x1), 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 tl.store(out_ptr0 + x2, tmp14, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 1), (1, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clone_0[grid(16, 4)](primals_1, buf0, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf1) del primals_2 buf2 = reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 0) del buf1 triton_poi_fused_add_tanh_1[grid(64)](buf2, primals_3, 64, XBLOCK= 64, num_warps=1, num_stages=1) del primals_3 buf3 = empty_strided_cuda((16, 1), (1, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf2, (16, 4), (4, 1), 0), primals_4, out=buf3) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused__softmax_2[grid(16)](buf3, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1) buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused__softmax_3[grid(16)](buf4, buf5, 16, XBLOCK=16, num_warps=1, num_stages=1) buf6 = buf4 del buf4 triton_poi_fused_mul_sum_4[grid(16)](primals_1, buf5, buf6, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf5 return buf6, primals_1, reinterpret_tensor(buf0, (16, 4), (4, 1), 0 ), buf2, buf3, reinterpret_tensor(primals_4, (1, 4), (1, 1), 0) class Self_Attentive_PoolingNew(nn.Module): def __init__(self, dim): """SAP Paper: Self-Attentive Speaker Embeddings for Text-Independent Speaker Verification Link: https://danielpovey.com/files/2018_interspeech_xvector_attention.pdf Args: dim (pair): the size of attention weights """ super(Self_Attentive_PoolingNew, self).__init__() self.sap_linear = nn.Linear(dim, dim) self.attention = nn.Parameter(torch.FloatTensor(dim, 1)) def forward(self, input_0): primals_4 = self.attention primals_2 = self.sap_linear.weight primals_3 = self.sap_linear.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
ikunsaikou/lhy_ML2021Spring
Self_Attentive_Pooling
false
6,870
[ "WTFPL" ]
1
80d8922077e2f5abba6a440c17654a143ebc8c9c
https://github.com/ikunsaikou/lhy_ML2021Spring/tree/80d8922077e2f5abba6a440c17654a143ebc8c9c
FCLateActionSAQFunction
import torch import numpy as np import torch.nn as nn import torch.nn.functional as F from abc import ABCMeta from abc import abstractmethod def init_lecun_normal(tensor, scale=1.0): """Initializes the tensor with LeCunNormal.""" fan_in = torch.nn.init._calculate_correct_fan(tensor, 'fan_in') std = scale * np.sqrt(1.0 / fan_in) with torch.no_grad(): return tensor.normal_(0, std) @torch.no_grad() def init_chainer_default(layer): """Initializes the layer with the chainer default. weights with LeCunNormal(scale=1.0) and zeros as biases """ assert isinstance(layer, nn.Module) if isinstance(layer, (nn.Linear, nn.Conv2d)): init_lecun_normal(layer.weight) if layer.bias is not None: nn.init.zeros_(layer.bias) return layer class MLP(nn.Module): """Multi-Layer Perceptron""" def __init__(self, in_size, out_size, hidden_sizes, nonlinearity=F.relu, last_wscale=1): self.in_size = in_size self.out_size = out_size self.hidden_sizes = hidden_sizes self.nonlinearity = nonlinearity super().__init__() if hidden_sizes: self.hidden_layers = nn.ModuleList() self.hidden_layers.append(nn.Linear(in_size, hidden_sizes[0])) for hin, hout in zip(hidden_sizes, hidden_sizes[1:]): self.hidden_layers.append(nn.Linear(hin, hout)) self.hidden_layers.apply(init_chainer_default) self.output = nn.Linear(hidden_sizes[-1], out_size) else: self.output = nn.Linear(in_size, out_size) init_lecun_normal(self.output.weight, scale=last_wscale) nn.init.zeros_(self.output.bias) def forward(self, x): h = x if self.hidden_sizes: for l in self.hidden_layers: h = self.nonlinearity(l(h)) return self.output(h) class StateActionQFunction(object, metaclass=ABCMeta): """Abstract Q-function with state and action input.""" @abstractmethod def __call__(self, x, a): """Evaluates Q-function Args: x (ndarray): state input a (ndarray): action input Returns: Q-value for state x and action a """ raise NotImplementedError() class FCLateActionSAQFunction(nn.Module, StateActionQFunction): """Fully-connected (s,a)-input Q-function with late action input. Actions are not included until the second hidden layer and not normalized. This architecture is used in the DDPG paper: http://arxiv.org/abs/1509.02971 Args: n_dim_obs (int): Number of dimensions of observation space. n_dim_action (int): Number of dimensions of action space. n_hidden_channels (int): Number of hidden channels. n_hidden_layers (int): Number of hidden layers. It must be greater than or equal to 1. nonlinearity (callable): Nonlinearity between layers. It must accept a Variable as an argument and return a Variable with the same shape. Nonlinearities with learnable parameters such as PReLU are not supported. last_wscale (float): Scale of weight initialization of the last layer. """ def __init__(self, n_dim_obs, n_dim_action, n_hidden_channels, n_hidden_layers, nonlinearity=F.relu, last_wscale=1.0): assert n_hidden_layers >= 1 self.n_input_channels = n_dim_obs + n_dim_action self.n_hidden_layers = n_hidden_layers self.n_hidden_channels = n_hidden_channels self.nonlinearity = nonlinearity super().__init__() self.obs_mlp = MLP(in_size=n_dim_obs, out_size=n_hidden_channels, hidden_sizes=[]) self.mlp = MLP(in_size=n_hidden_channels + n_dim_action, out_size=1, hidden_sizes=[self.n_hidden_channels] * (self.n_hidden_layers - 1), nonlinearity=nonlinearity, last_wscale=last_wscale) self.output = self.mlp.output def forward(self, state, action): h = self.nonlinearity(self.obs_mlp(state)) h = torch.cat((h, action), dim=1) return self.mlp(h) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'n_dim_obs': 4, 'n_dim_action': 4, 'n_hidden_channels': 4, 'n_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 import triton_helpers import numpy as np import torch.nn as nn import torch.nn.functional as F from abc import ABCMeta from abc import abstractmethod assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_cat_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 8 x1 = xindex // 8 x2 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (4 * x1 + x0), tmp4 & xmask, eviction_policy= 'evict_last', other=0.0) tmp6 = tl.load(in_ptr1 + x0, tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp7 = tmp5 + tmp6 tmp8 = tl.full([1], 0, tl.int32) tmp9 = triton_helpers.maximum(tmp8, tmp7) tmp10 = tl.full(tmp9.shape, 0.0, tmp9.dtype) tmp11 = tl.where(tmp4, tmp9, tmp10) tmp12 = tmp0 >= tmp3 tl.full([1], 8, tl.int64) tmp15 = tl.load(in_ptr2 + (4 * x1 + (-4 + x0)), tmp12 & xmask, eviction_policy='evict_last', other=0.0) tmp16 = tl.where(tmp4, tmp11, tmp15) tl.store(out_ptr0 + x2, tmp16, xmask) @triton.jit def triton_poi_fused_relu_threshold_backward_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 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(out_ptr0 + x2, tmp6, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = 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, (1, 8), (8, 1)) assert_size_stride(primals_6, (1,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((4, 8), (8, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(32)](buf0, primals_3, primals_4, buf1, 32, XBLOCK=32, num_warps=1, num_stages=1) del primals_4 buf3 = empty_strided_cuda((4, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_6, buf1, reinterpret_tensor(primals_5, (8, 1), (1, 8), 0), alpha=1, beta=1, out=buf3) del primals_6 buf4 = empty_strided_cuda((4, 4), (4, 1), torch.bool) triton_poi_fused_relu_threshold_backward_1[grid(16)](buf0, primals_3, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf0 del primals_3 return buf3, primals_1, buf1, primals_5, buf4 def init_lecun_normal(tensor, scale=1.0): """Initializes the tensor with LeCunNormal.""" fan_in = torch.nn.init._calculate_correct_fan(tensor, 'fan_in') std = scale * np.sqrt(1.0 / fan_in) with torch.no_grad(): return tensor.normal_(0, std) @torch.no_grad() def init_chainer_default(layer): """Initializes the layer with the chainer default. weights with LeCunNormal(scale=1.0) and zeros as biases """ assert isinstance(layer, nn.Module) if isinstance(layer, (nn.Linear, nn.Conv2d)): init_lecun_normal(layer.weight) if layer.bias is not None: nn.init.zeros_(layer.bias) return layer class MLP(nn.Module): """Multi-Layer Perceptron""" def __init__(self, in_size, out_size, hidden_sizes, nonlinearity=F.relu, last_wscale=1): self.in_size = in_size self.out_size = out_size self.hidden_sizes = hidden_sizes self.nonlinearity = nonlinearity super().__init__() if hidden_sizes: self.hidden_layers = nn.ModuleList() self.hidden_layers.append(nn.Linear(in_size, hidden_sizes[0])) for hin, hout in zip(hidden_sizes, hidden_sizes[1:]): self.hidden_layers.append(nn.Linear(hin, hout)) self.hidden_layers.apply(init_chainer_default) self.output = nn.Linear(hidden_sizes[-1], out_size) else: self.output = nn.Linear(in_size, out_size) init_lecun_normal(self.output.weight, scale=last_wscale) nn.init.zeros_(self.output.bias) def forward(self, x): h = x if self.hidden_sizes: for l in self.hidden_layers: h = self.nonlinearity(l(h)) return self.output(h) class StateActionQFunction(object, metaclass=ABCMeta): """Abstract Q-function with state and action input.""" @abstractmethod def __call__(self, x, a): """Evaluates Q-function Args: x (ndarray): state input a (ndarray): action input Returns: Q-value for state x and action a """ raise NotImplementedError() class FCLateActionSAQFunctionNew(nn.Module, StateActionQFunction): """Fully-connected (s,a)-input Q-function with late action input. Actions are not included until the second hidden layer and not normalized. This architecture is used in the DDPG paper: http://arxiv.org/abs/1509.02971 Args: n_dim_obs (int): Number of dimensions of observation space. n_dim_action (int): Number of dimensions of action space. n_hidden_channels (int): Number of hidden channels. n_hidden_layers (int): Number of hidden layers. It must be greater than or equal to 1. nonlinearity (callable): Nonlinearity between layers. It must accept a Variable as an argument and return a Variable with the same shape. Nonlinearities with learnable parameters such as PReLU are not supported. last_wscale (float): Scale of weight initialization of the last layer. """ def __init__(self, n_dim_obs, n_dim_action, n_hidden_channels, n_hidden_layers, nonlinearity=F.relu, last_wscale=1.0): assert n_hidden_layers >= 1 self.n_input_channels = n_dim_obs + n_dim_action self.n_hidden_layers = n_hidden_layers self.n_hidden_channels = n_hidden_channels self.nonlinearity = nonlinearity super().__init__() self.obs_mlp = MLP(in_size=n_dim_obs, out_size=n_hidden_channels, hidden_sizes=[]) self.mlp = MLP(in_size=n_hidden_channels + n_dim_action, out_size=1, hidden_sizes=[self.n_hidden_channels] * (self.n_hidden_layers - 1), nonlinearity=nonlinearity, last_wscale=last_wscale) self.output = self.mlp.output def forward(self, input_0, input_1): primals_1 = self.obs_mlp.output.weight primals_3 = self.obs_mlp.output.bias primals_5 = self.mlp.output.weight primals_6 = self.mlp.output.bias primals_2 = input_0 primals_4 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6]) return output[0]
imatge-upc/pixelcoordEDL
FCLateActionSAQFunction
false
6,871
[ "MIT" ]
1
353632feed6ac8c93758c1a2a1b7a477e7ff053c
https://github.com/imatge-upc/pixelcoordEDL/tree/353632feed6ac8c93758c1a2a1b7a477e7ff053c
TransformerEncoderLayer
import torch import torch.nn as nn class TransformerEncoderLayer(nn.Module): def __init__(self, d_model, nhead, dim_feedforward=16, dropout=0): super(TransformerEncoderLayer, self).__init__() self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout) self.linear1 = nn.Linear(d_model, dim_feedforward) self.dropout = nn.Dropout(dropout) self.linear2 = nn.Linear(dim_feedforward, d_model) self.dropout1 = nn.Dropout(dropout) self.dropout2 = nn.Dropout(dropout) self.activation = nn.LeakyReLU(True) def forward(self, src, src_mask=None, src_key_padding_mask=None): src2 = self.self_attn(src, src, src)[0] src = src + self.dropout1(src2) src2 = self.linear2(self.dropout(self.activation(self.linear1(src)))) src = src + self.dropout2(src2) return src def get_inputs(): return [torch.rand([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 math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_mul_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 1.0 tmp4 = tmp2 * tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_clone_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 4 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x1 = xindex y0 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x1), xmask & ymask) tl.store(out_ptr0 + (x1 + 4 * y0), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_add_4(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 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_leaky_relu_5(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 16 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 1.0 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, primals_8, primals_9) = args args.clear() assert_size_stride(primals_1, (4, 4), (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,)) assert_size_stride(primals_6, (16, 4), (4, 1)) assert_size_stride(primals_7, (16,), (1,)) assert_size_stride(primals_8, (4, 16), (16, 1)) assert_size_stride(primals_9, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(reinterpret_tensor(primals_3, (4,), (1,), 4), primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 16), alpha=1, beta=1, out=buf1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(reinterpret_tensor(primals_3, (4,), (1,), 8), primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 32), alpha=1, beta=1, out=buf2) del primals_2 buf3 = reinterpret_tensor(buf0, (4, 4, 1), (1, 4, 16), 0) del buf0 get_raw_stream(0) triton_poi_fused_mul_0[grid(16)](buf3, primals_3, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_3 buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf3, reinterpret_tensor(buf1, (4, 1, 4), (1, 1, 4), 0), out=buf4) buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_1[grid(64)](buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf6 = buf4 del buf4 triton_poi_fused__softmax_2[grid(64)](buf5, buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) buf7 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) extern_kernels.bmm(buf6, reinterpret_tensor(buf2, (4, 4, 1), (1, 4, 1), 0), out=buf7) buf8 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) triton_poi_fused_clone_3[grid(4, 4)](buf7, buf8, 4, 4, XBLOCK=4, YBLOCK=4, num_warps=1, num_stages=1) buf9 = reinterpret_tensor(buf7, (4, 4), (4, 1), 0) del buf7 extern_kernels.mm(reinterpret_tensor(buf8, (4, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf9) buf10 = buf9 del buf9 triton_poi_fused_add_4[grid(16)](buf10, primals_1, primals_5, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_5 buf11 = reinterpret_tensor(buf5, (4, 16), (16, 1), 0) del buf5 extern_kernels.mm(buf10, reinterpret_tensor(primals_6, (4, 16), (1, 4), 0), out=buf11) buf12 = empty_strided_cuda((4, 16), (16, 1), torch.bool) buf13 = empty_strided_cuda((4, 16), (16, 1), torch.float32) triton_poi_fused_leaky_relu_5[grid(64)](buf11, primals_7, buf12, buf13, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf11 del primals_7 buf14 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf13, reinterpret_tensor(primals_8, (16, 4), (1, 16), 0), out=buf14) buf15 = buf14 del buf14 triton_poi_fused_add_4[grid(16)](buf15, buf10, primals_9, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_9 return (buf15, primals_1, buf6, reinterpret_tensor(buf8, (4, 4), (4, 1), 0), buf10, buf12, buf13, primals_8, primals_6, primals_4, reinterpret_tensor(buf2, (4, 1, 4), (1, 1, 4), 0), reinterpret_tensor(buf3, (4, 1, 4), (1, 1, 4), 0), reinterpret_tensor(buf1, (4, 4, 1), (1, 4, 1), 0)) class TransformerEncoderLayerNew(nn.Module): def __init__(self, d_model, nhead, dim_feedforward=16, dropout=0): super(TransformerEncoderLayerNew, self).__init__() self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout) self.linear1 = nn.Linear(d_model, dim_feedforward) self.dropout = nn.Dropout(dropout) self.linear2 = nn.Linear(dim_feedforward, d_model) self.dropout1 = nn.Dropout(dropout) self.dropout2 = nn.Dropout(dropout) self.activation = nn.LeakyReLU(True) def forward(self, input_0): primals_2 = self.self_attn.in_proj_weight primals_3 = self.self_attn.in_proj_bias primals_1 = self.self_attn.out_proj.weight primals_5 = self.self_attn.out_proj.bias primals_6 = self.linear1.weight primals_7 = self.linear1.bias primals_8 = self.linear2.weight primals_9 = self.linear2.bias primals_4 = 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]
imperial-qore/CAROL
TransformerEncoderLayer
false
6,872
[ "BSD-3-Clause" ]
1
57dc42c4ddeb9e75eed43a91ceb336a1ecc9c8b9
https://github.com/imperial-qore/CAROL/tree/57dc42c4ddeb9e75eed43a91ceb336a1ecc9c8b9
WNConv2d
import torch import torch.nn as nn class WNConv2d(nn.Module): def __init__(self, in_channel, out_channel, kernel_size, stride=1, padding=0, bias=True, activation=None): super().__init__() self.conv = nn.utils.weight_norm(nn.Conv2d(in_channel, out_channel, kernel_size, stride=stride, padding=padding, bias=bias)) self.out_channel = out_channel if isinstance(kernel_size, int): kernel_size = [kernel_size, kernel_size] self.kernel_size = kernel_size self.activation = activation def forward(self, input): out = self.conv(input) if self.activation is not None: out = self.activation(out) return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channel': 4, 'out_channel': 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 reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_per_fused__weight_norm_interface_0(in_out_ptr0, in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 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) tmp7 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') 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) tmp8 = tmp7 / tmp6 tmp9 = tmp0 * tmp8 tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp6, xmask) tl.store(out_ptr0 + (r1 + 64 * x0), tmp9, 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, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 1, 1, 1), (1, 1, 1, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 1, 1, 1), (1, 4, 4, 4), torch.float32) buf1 = reinterpret_tensor(buf0, (4, 1, 1, 1), (1, 1, 1, 1), 0) del buf0 buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_per_fused__weight_norm_interface_0[grid(4)](buf1, primals_2, primals_1, buf2, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) buf3 = extern_kernels.convolution(primals_4, buf2, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf3, (4, 4, 1, 1), (4, 1, 1, 1)) buf4 = buf3 del buf3 triton_poi_fused_convolution_1[grid(16)](buf4, primals_3, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_3 return buf4, buf2, primals_1, primals_2, primals_4, buf1, buf2 class WNConv2dNew(nn.Module): def __init__(self, in_channel, out_channel, kernel_size, stride=1, padding=0, bias=True, activation=None): super().__init__() self.conv = nn.utils.weight_norm(nn.Conv2d(in_channel, out_channel, kernel_size, stride=stride, padding=padding, bias=bias)) self.out_channel = out_channel if isinstance(kernel_size, int): kernel_size = [kernel_size, kernel_size] self.kernel_size = kernel_size self.activation = activation def forward(self, input_0): primals_3 = self.conv.bias primals_1 = self.conv.weight_g primals_2 = self.conv.weight_v primals_4 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
imatge-upc/pixelcoordEDL
WNConv2d
false
6,873
[ "MIT" ]
1
353632feed6ac8c93758c1a2a1b7a477e7ff053c
https://github.com/imatge-upc/pixelcoordEDL/tree/353632feed6ac8c93758c1a2a1b7a477e7ff053c
TransformerDecoderLayer
import torch import torch.nn as nn class TransformerDecoderLayer(nn.Module): def __init__(self, d_model, nhead, dim_feedforward=16, dropout=0): super(TransformerDecoderLayer, self).__init__() self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout) self.multihead_attn = nn.MultiheadAttention(d_model, nhead, dropout =dropout) self.linear1 = nn.Linear(d_model, dim_feedforward) self.dropout = nn.Dropout(dropout) self.linear2 = nn.Linear(dim_feedforward, d_model) self.dropout1 = nn.Dropout(dropout) self.dropout2 = nn.Dropout(dropout) self.dropout3 = nn.Dropout(dropout) self.activation = nn.LeakyReLU(True) def forward(self, tgt, memory, tgt_mask=None, memory_mask=None, tgt_key_padding_mask=None, memory_key_padding_mask=None): tgt2 = self.self_attn(tgt, tgt, tgt)[0] tgt = tgt + self.dropout1(tgt2) tgt2 = self.multihead_attn(tgt, memory, memory)[0] tgt = tgt + self.dropout2(tgt2) tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt)))) tgt = tgt + self.dropout3(tgt2) return tgt def get_inputs(): return [torch.rand([4, 4]), torch.rand([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 math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_mul_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 1.0 tmp4 = tmp2 * tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_clone_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 4 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x1 = xindex y0 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x1), xmask & ymask) tl.store(out_ptr0 + (x1 + 4 * y0), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_add_4(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 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_leaky_relu_5(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 16 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 1.0 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, 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, (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,)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (12, 4), (4, 1)) assert_size_stride(primals_8, (12,), (1,)) assert_size_stride(primals_9, (4, 4), (4, 1)) assert_size_stride(primals_10, (4,), (1,)) assert_size_stride(primals_11, (16, 4), (4, 1)) assert_size_stride(primals_12, (16,), (1,)) assert_size_stride(primals_13, (4, 16), (16, 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), torch.float32) extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(reinterpret_tensor(primals_3, (4,), (1,), 4), primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 16), alpha=1, beta=1, out=buf1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(reinterpret_tensor(primals_3, (4,), (1,), 8), primals_1, reinterpret_tensor(primals_2, (4, 4), (1, 4), 32), alpha=1, beta=1, out=buf2) del primals_2 buf3 = reinterpret_tensor(buf0, (4, 4, 1), (1, 4, 16), 0) del buf0 get_raw_stream(0) triton_poi_fused_mul_0[grid(16)](buf3, primals_3, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_3 buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf3, reinterpret_tensor(buf1, (4, 1, 4), (1, 1, 4), 0), out=buf4) buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_1[grid(64)](buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf6 = buf4 del buf4 triton_poi_fused__softmax_2[grid(64)](buf5, buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) buf7 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) extern_kernels.bmm(buf6, reinterpret_tensor(buf2, (4, 4, 1), (1, 4, 1), 0), out=buf7) buf8 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) triton_poi_fused_clone_3[grid(4, 4)](buf7, buf8, 4, 4, XBLOCK=4, YBLOCK=4, num_warps=1, num_stages=1) buf9 = reinterpret_tensor(buf7, (4, 4), (4, 1), 0) del buf7 extern_kernels.mm(reinterpret_tensor(buf8, (4, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf9) buf10 = buf9 del buf9 triton_poi_fused_add_4[grid(16)](buf10, primals_1, primals_5, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_5 buf11 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf10, reinterpret_tensor(primals_7, (4, 4), (1, 4), 0), out=buf11) buf12 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(reinterpret_tensor(primals_8, (4,), (1,), 4), primals_6, reinterpret_tensor(primals_7, (4, 4), (1, 4), 16), alpha=1, beta=1, out=buf12) buf13 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(reinterpret_tensor(primals_8, (4,), (1,), 8), primals_6, reinterpret_tensor(primals_7, (4, 4), (1, 4), 32), alpha=1, beta=1, out=buf13) buf14 = reinterpret_tensor(buf11, (4, 4, 1), (1, 4, 16), 0) del buf11 triton_poi_fused_mul_0[grid(16)](buf14, primals_8, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_8 buf15 = buf5 del buf5 extern_kernels.bmm(buf14, reinterpret_tensor(buf12, (4, 1, 4), (1, 1, 4), 0), out=buf15) buf16 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_1[grid(64)](buf15, buf16, 64, XBLOCK=64, num_warps=1, num_stages=1) buf17 = buf15 del buf15 triton_poi_fused__softmax_2[grid(64)](buf16, buf17, 64, XBLOCK=64, num_warps=1, num_stages=1) buf18 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) extern_kernels.bmm(buf17, reinterpret_tensor(buf13, (4, 4, 1), (1, 4, 1), 0), out=buf18) buf19 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) triton_poi_fused_clone_3[grid(4, 4)](buf18, buf19, 4, 4, XBLOCK=4, YBLOCK=4, num_warps=1, num_stages=1) buf20 = reinterpret_tensor(buf18, (4, 4), (4, 1), 0) del buf18 extern_kernels.mm(reinterpret_tensor(buf19, (4, 4), (4, 1), 0), reinterpret_tensor(primals_9, (4, 4), (1, 4), 0), out=buf20) buf21 = buf20 del buf20 triton_poi_fused_add_4[grid(16)](buf21, buf10, primals_10, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_10 buf22 = reinterpret_tensor(buf16, (4, 16), (16, 1), 0) del buf16 extern_kernels.mm(buf21, reinterpret_tensor(primals_11, (4, 16), (1, 4), 0), out=buf22) buf23 = empty_strided_cuda((4, 16), (16, 1), torch.bool) buf24 = empty_strided_cuda((4, 16), (16, 1), torch.float32) triton_poi_fused_leaky_relu_5[grid(64)](buf22, primals_12, buf23, buf24, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf22 del primals_12 buf25 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf24, reinterpret_tensor(primals_13, (16, 4), (1, 16), 0), out=buf25) buf26 = buf25 del buf25 triton_poi_fused_add_4[grid(16)](buf26, buf21, primals_14, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_14 return (buf26, primals_1, buf6, reinterpret_tensor(buf8, (4, 4), (4, 1), 0), buf10, primals_6, buf17, reinterpret_tensor(buf19, (4, 4), (4, 1), 0), buf21, buf23, buf24, primals_13, primals_11, primals_9, reinterpret_tensor(buf13, (4, 1, 4), (1, 1, 4), 0), reinterpret_tensor(buf14, (4, 1, 4), (1, 1, 4), 0), reinterpret_tensor(buf12, (4, 4, 1), (1, 4, 1), 0), reinterpret_tensor(primals_7, (4, 4), (4, 1), 0), primals_4, reinterpret_tensor(buf2, (4, 1, 4), (1, 1, 4), 0), reinterpret_tensor(buf3, (4, 1, 4), (1, 1, 4), 0), reinterpret_tensor(buf1, (4, 4, 1), (1, 4, 1), 0)) class TransformerDecoderLayerNew(nn.Module): def __init__(self, d_model, nhead, dim_feedforward=16, dropout=0): super(TransformerDecoderLayerNew, self).__init__() self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout) self.multihead_attn = nn.MultiheadAttention(d_model, nhead, dropout =dropout) self.linear1 = nn.Linear(d_model, dim_feedforward) self.dropout = nn.Dropout(dropout) self.linear2 = nn.Linear(dim_feedforward, d_model) self.dropout1 = nn.Dropout(dropout) self.dropout2 = nn.Dropout(dropout) self.dropout3 = nn.Dropout(dropout) self.activation = nn.LeakyReLU(True) def forward(self, input_0, input_1): primals_2 = self.self_attn.in_proj_weight primals_3 = self.self_attn.in_proj_bias primals_1 = self.self_attn.out_proj.weight primals_5 = self.self_attn.out_proj.bias primals_7 = self.multihead_attn.in_proj_weight primals_8 = self.multihead_attn.in_proj_bias primals_4 = self.multihead_attn.out_proj.weight primals_10 = self.multihead_attn.out_proj.bias primals_11 = self.linear1.weight primals_12 = self.linear1.bias primals_13 = self.linear2.weight primals_14 = self.linear2.bias primals_6 = input_0 primals_9 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14]) return output[0]
imperial-qore/CAROL
TransformerDecoderLayer
false
6,874
[ "BSD-3-Clause" ]
1
57dc42c4ddeb9e75eed43a91ceb336a1ecc9c8b9
https://github.com/imperial-qore/CAROL/tree/57dc42c4ddeb9e75eed43a91ceb336a1ecc9c8b9
FocalLossV1
import torch import torch.nn as nn class FocalLossV1(nn.Module): def __init__(self, alpha=0.25, gamma=2, reduction='mean'): super(FocalLossV1, self).__init__() self.alpha = alpha self.gamma = gamma self.reduction = reduction self.crit = nn.BCEWithLogitsLoss(reduction='none') def forward(self, logits, label): """ logits and label have same shape, and label data type is long args: logits: tensor of shape (N, ...) label: tensor of shape(N, ...) """ logits = logits.float() with torch.no_grad(): alpha = torch.empty_like(logits).fill_(1 - self.alpha) alpha[label == 1] = self.alpha probs = torch.sigmoid(logits) pt = torch.where(label == 1, probs, 1 - probs) ce_loss = self.crit(logits, label.float()) loss = alpha * torch.pow(1 - pt, self.gamma) * ce_loss if self.reduction == 'mean': loss = loss.mean() if self.reduction == 'sum': loss = loss.sum() return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn 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_eq_fill_index_put_lift_fresh_mean_mul_pow_rsub_sigmoid_where_0( in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp6 = tl.load(in_ptr1 + r0, None) tmp1 = 1.0 tmp2 = tmp0 == tmp1 tmp3 = 0.25 tmp4 = 0.75 tmp5 = tl.where(tmp2, tmp3, tmp4) tmp7 = tl.sigmoid(tmp6) tmp8 = tmp1 - tmp7 tmp9 = tl.where(tmp2, tmp7, tmp8) tmp10 = tmp1 - tmp9 tmp11 = tmp10 * tmp10 tmp12 = tmp5 * tmp11 tmp13 = tmp1 - tmp0 tmp14 = tmp13 * tmp6 tmp15 = 0.0 tmp16 = triton_helpers.minimum(tmp15, tmp6) tmp17 = tl_math.abs(tmp6) tmp18 = -tmp17 tmp19 = tl_math.exp(tmp18) tmp20 = libdevice.log1p(tmp19) tmp21 = tmp16 - tmp20 tmp22 = tmp14 - tmp21 tmp23 = tmp12 * tmp22 tmp24 = tl.broadcast_to(tmp23, [RBLOCK]) tmp26 = triton_helpers.promote_to_tensor(tl.sum(tmp24, 0)) tmp27 = 256.0 tmp28 = tmp26 / tmp27 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp28, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((), (), torch.float32) buf2 = buf1 del buf1 get_raw_stream(0) triton_per_fused_binary_cross_entropy_with_logits_eq_fill_index_put_lift_fresh_mean_mul_pow_rsub_sigmoid_where_0[ grid(1)](buf2, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf2, class FocalLossV1New(nn.Module): def __init__(self, alpha=0.25, gamma=2, reduction='mean'): super(FocalLossV1New, self).__init__() self.alpha = alpha self.gamma = gamma self.reduction = reduction self.crit = nn.BCEWithLogitsLoss(reduction='none') def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
imvladikon/pytorch-loss
FocalLossV1
false
6,875
[ "MIT" ]
1
6cfaabe1be898e1ff000b3dffb46d0ef09096f6b
https://github.com/imvladikon/pytorch-loss/tree/6cfaabe1be898e1ff000b3dffb46d0ef09096f6b
SoftDiceLossV1
import torch import torch.nn as nn class SoftDiceLossV1(nn.Module): """ soft-dice loss, useful in binary segmentation """ def __init__(self, p=1, smooth=1, reduction='mean'): super(SoftDiceLossV1, self).__init__() self.p = p self.smooth = smooth self.reduction = reduction def forward(self, logits, labels): """ args: logits: tensor of shape (N, H, W) args: label: tensor of shape(N, H, W) """ probs = torch.sigmoid(logits) numer = (probs * labels).sum(dim=(1, 2)) denor = (probs.pow(self.p) + labels).sum(dim=(1, 2)) loss = 1.0 - (2 * numer + self.smooth) / (denor + self.smooth) if self.reduction == 'mean': loss = loss.mean() elif self.reduction == 'sum': loss = loss.sum() 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_add_mul_sigmoid_sum_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 16 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r2 = rindex x0 = xindex % 4 x1 = xindex // 4 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * r2 + 64 * x1), xmask, other=0.0) tmp2 = tl.load(in_ptr1 + (x0 + 4 * r2 + 64 * x1), xmask, other=0.0) tmp1 = tl.sigmoid(tmp0) tmp3 = tmp1 * tmp2 tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tmp1 + tmp2 tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK]) tmp11 = tl.where(xmask, tmp9, 0) tmp12 = tl.sum(tmp11, 1)[:, None] tl.store(out_ptr0 + x3, tmp7, xmask) tl.store(out_ptr1 + x3, tmp12, xmask) @triton.jit def triton_per_fused_add_div_mean_mul_rsub_1(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp5 = tl.load(in_ptr1 + r0, None) tmp1 = 2.0 tmp2 = tmp0 * tmp1 tmp3 = 1.0 tmp4 = tmp2 + tmp3 tmp6 = tmp5 + tmp3 tmp7 = tmp4 / tmp6 tmp8 = tmp3 - tmp7 tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK]) tmp11 = tl.sum(tmp9, 1)[:, None] tmp12 = 16.0 tmp13 = tmp11 / tmp12 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp13, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_per_fused_add_mul_sigmoid_sum_0[grid(16)](arg0_1, arg1_1, buf0, buf1, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 buf2 = empty_strided_cuda((), (), torch.float32) buf3 = buf2 del buf2 triton_per_fused_add_div_mean_mul_rsub_1[grid(1)](buf3, buf0, buf1, 1, 16, XBLOCK=1, num_warps=2, num_stages=1) del buf0 del buf1 return buf3, class SoftDiceLossV1New(nn.Module): """ soft-dice loss, useful in binary segmentation """ def __init__(self, p=1, smooth=1, reduction='mean'): super(SoftDiceLossV1New, self).__init__() self.p = p self.smooth = smooth self.reduction = reduction def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
imvladikon/pytorch-loss
SoftDiceLossV1
false
6,876
[ "MIT" ]
1
6cfaabe1be898e1ff000b3dffb46d0ef09096f6b
https://github.com/imvladikon/pytorch-loss/tree/6cfaabe1be898e1ff000b3dffb46d0ef09096f6b
CoordConv2d
import torch import torch.nn as nn import torch.nn.functional as F class CoordConv2d(nn.Conv2d): def __init__(self, in_chan, out_chan, kernel_size=3, stride=1, padding= 1, dilation=1, groups=1, bias=True): super(CoordConv2d, self).__init__(in_chan + 2, out_chan, kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias) def forward(self, x): batchsize, H, W = x.size(0), x.size(2), x.size(3) h_range = torch.linspace(-1, 1, H, device=x.device, dtype=x.dtype) w_range = torch.linspace(-1, 1, W, device=x.device, dtype=x.dtype) h_chan, w_chan = torch.meshgrid(h_range, w_range) h_chan = h_chan.expand([batchsize, 1, -1, -1]) w_chan = w_chan.expand([batchsize, 1, -1, -1]) feat = torch.cat([h_chan, w_chan, x], dim=1) return F.conv2d(feat, self.weight, self.bias, self.stride, self. padding, self.dilation, self.groups) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_chan': 4, 'out_chan': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 384 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex // 16 % 6 x1 = xindex // 4 % 4 x0 = xindex % 4 x3 = xindex // 96 x6 = xindex % 16 x7 = xindex tmp0 = x2 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = x1 tmp6 = tmp5.to(tl.float32) tmp7 = 2.0 tmp8 = tmp6 < tmp7 tmp9 = 0.6666666666666666 tmp10 = tmp6 * tmp9 tmp11 = -1.0 tmp12 = tmp10 + tmp11 tmp13 = 3 + -1 * x1 tmp14 = tmp13.to(tl.float32) tmp15 = tmp14 * tmp9 tmp16 = 1.0 tmp17 = tmp16 - tmp15 tmp18 = tl.where(tmp8, tmp12, tmp17) tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype) tmp20 = tl.where(tmp4, tmp18, tmp19) tmp21 = tmp0 >= tmp3 tmp22 = tl.full([1], 2, tl.int64) tmp23 = tmp0 < tmp22 tmp24 = tmp21 & tmp23 tmp25 = x0 tmp26 = tmp25.to(tl.float32) tmp27 = tmp26 < tmp7 tmp28 = tmp26 * tmp9 tmp29 = tmp28 + tmp11 tmp30 = 3 + -1 * x0 tmp31 = tmp30.to(tl.float32) tmp32 = tmp31 * tmp9 tmp33 = tmp16 - tmp32 tmp34 = tl.where(tmp27, tmp29, tmp33) tmp35 = tl.full(tmp34.shape, 0.0, tmp34.dtype) tmp36 = tl.where(tmp24, tmp34, tmp35) tmp37 = tmp0 >= tmp22 tl.full([1], 6, tl.int64) tmp40 = tl.load(in_ptr0 + (x6 + 16 * (-2 + x2) + 64 * x3), tmp37 & xmask, other=0.0) tmp41 = tl.where(tmp24, tmp36, tmp40) tmp42 = tl.where(tmp4, tmp20, tmp41) tl.store(out_ptr0 + x7, tmp42, xmask) @triton.jit def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 6, 3, 3), (54, 9, 3, 1)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 6, 4, 4), (96, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(384)](primals_1, buf0, 384, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 4, 4, 4), (64, 16, 4, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_1[grid(256)](buf2, primals_3, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_3 return buf2, primals_2, buf0 class CoordConv2dNew(nn.Conv2d): def __init__(self, in_chan, out_chan, kernel_size=3, stride=1, padding= 1, dilation=1, groups=1, bias=True): super(CoordConv2dNew, self).__init__(in_chan + 2, out_chan, kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias) def forward(self, input_0): primals_2 = self.weight primals_3 = self.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
imvladikon/pytorch-loss
CoordConv2d
false
6,877
[ "MIT" ]
1
6cfaabe1be898e1ff000b3dffb46d0ef09096f6b
https://github.com/imvladikon/pytorch-loss/tree/6cfaabe1be898e1ff000b3dffb46d0ef09096f6b
EncoderLayer
import math import torch import torch.nn as nn import torch.nn.functional as F class AffineLayer(nn.Module): def __init__(self, dropout, d_model, d_ff): super(AffineLayer, self).__init__() self.w_1 = nn.Linear(d_model, d_ff) self.w_2 = nn.Linear(d_ff, d_model) self.dropout = nn.Dropout(dropout) def forward(self, x): return self.w_2(self.dropout(F.relu(self.w_1(x)))) class MultiHeadedAttention(nn.Module): def __init__(self, num_head, d_model, dropout=0.1): super(MultiHeadedAttention, self).__init__() assert d_model % num_head == 0 self.d_k = d_model // num_head self.h = num_head self.linear_key = nn.Linear(d_model, d_model) self.linear_value = nn.Linear(d_model, d_model) self.linear_query = nn.Linear(d_model, d_model) self.linear_out = nn.Linear(d_model, d_model) self.dropout = nn.Dropout(p=dropout) def attention(self, query, key, value, mask, dropout=None): d_k = query.size(-1) scores = torch.matmul(query, key.transpose(-2, -1)) / math.sqrt(d_k) scores = scores.masked_fill(mask == 0, -1000000000.0) p_attn = F.softmax(scores, dim=-1) if dropout is not None: p_attn = dropout(p_attn) return torch.matmul(p_attn, value), p_attn def forward(self, query, key, value, mask): nbatches = query.size(0) query = self.linear_query(query).view(nbatches, -1, self.h, self.d_k ).transpose(1, 2) key = self.linear_key(key).view(nbatches, -1, self.h, self.d_k ).transpose(1, 2) value = self.linear_value(value).view(nbatches, -1, self.h, self.d_k ).transpose(1, 2) mask = mask.unsqueeze(1) x, _attn = self.attention(query, key, value, mask, dropout=self.dropout ) x = x.transpose(1, 2).contiguous().view(nbatches, -1, self.h * self.d_k ) return self.linear_out(x) class EncoderLayer(nn.Module): def __init__(self, num_head, dropout, d_model, d_ff): super(EncoderLayer, self).__init__() self.att_layer = MultiHeadedAttention(num_head, d_model, dropout) self.norm_att = nn.LayerNorm(d_model) self.dropout_att = nn.Dropout(dropout) self.affine_layer = AffineLayer(dropout, d_model, d_ff) self.norm_affine = nn.LayerNorm(d_model) self.dropout_affine = nn.Dropout(dropout) def forward(self, x, mask): x_att = self.norm_att(x * mask) x_att = self.att_layer(x_att, x_att, x_att, mask) x = x + self.dropout_att(x_att) x_affine = self.norm_affine(x * mask) x_affine = self.affine_layer(x_affine) return x + self.dropout_affine(x_affine) def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'num_head': 4, 'dropout': 0.5, 'd_model': 4, 'd_ff': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, 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_mul_native_layer_norm_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex 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_mul_native_layer_norm_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x2, xmask) tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 * tmp1 tmp4 = tmp2 - tmp3 tmp6 = 1e-05 tmp7 = tmp5 + tmp6 tmp8 = libdevice.rsqrt(tmp7) tmp9 = tmp4 * tmp8 tmp11 = tmp9 * tmp10 tmp13 = tmp11 + tmp12 tl.store(out_ptr0 + x2, tmp13, xmask) @triton.jit def triton_poi_fused_clone_2(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask) @triton.jit def triton_poi_fused__softmax_div_eq_masked_fill_3(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 % 4 x2 = xindex // 16 x3 = xindex tmp0 = tl.load(in_ptr0 + (4 * x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr1 + 4 * x3, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (1 + 4 * x0 + 16 * x2), xmask, eviction_policy ='evict_last') tmp10 = tl.load(in_ptr1 + (1 + 4 * x3), xmask, eviction_policy='evict_last' ) tmp14 = tl.load(in_ptr0 + (2 + 4 * x0 + 16 * x2), xmask, eviction_policy='evict_last') tmp16 = tl.load(in_ptr1 + (2 + 4 * x3), xmask, eviction_policy='evict_last' ) tmp20 = tl.load(in_ptr0 + (3 + 4 * x0 + 16 * x2), xmask, eviction_policy='evict_last') tmp22 = tl.load(in_ptr1 + (3 + 4 * x3), xmask, eviction_policy='evict_last' ) tmp1 = 0.0 tmp2 = tmp0 == tmp1 tmp4 = 1.0 tmp5 = tmp3 * tmp4 tmp6 = -1000000000.0 tmp7 = tl.where(tmp2, tmp6, tmp5) tmp9 = tmp8 == tmp1 tmp11 = tmp10 * tmp4 tmp12 = tl.where(tmp9, tmp6, tmp11) tmp13 = triton_helpers.maximum(tmp7, tmp12) tmp15 = tmp14 == tmp1 tmp17 = tmp16 * tmp4 tmp18 = tl.where(tmp15, tmp6, tmp17) tmp19 = triton_helpers.maximum(tmp13, tmp18) tmp21 = tmp20 == tmp1 tmp23 = tmp22 * tmp4 tmp24 = tl.where(tmp21, tmp6, tmp23) tmp25 = triton_helpers.maximum(tmp19, tmp24) tmp26 = tmp7 - tmp25 tmp27 = tl_math.exp(tmp26) tmp28 = tmp12 - tmp25 tmp29 = tl_math.exp(tmp28) tmp30 = tmp27 + tmp29 tmp31 = tmp18 - tmp25 tmp32 = tl_math.exp(tmp31) tmp33 = tmp30 + tmp32 tmp34 = tmp24 - tmp25 tmp35 = tl_math.exp(tmp34) tmp36 = tmp33 + tmp35 tl.store(out_ptr0 + x3, tmp25, xmask) tl.store(out_ptr1 + x3, tmp36, xmask) @triton.jit def triton_poi_fused__softmax_div_eq_masked_fill_4(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 % 16 x5 = xindex x6 = xindex // 4 tmp0 = tl.load(in_ptr0 + (x4 + 16 * x3), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_out_ptr0 + x5, xmask) tmp8 = tl.load(in_ptr1 + x6, xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr2 + x6, xmask, eviction_policy='evict_last') tmp1 = 0.0 tmp2 = tmp0 == tmp1 tmp4 = 1.0 tmp5 = tmp3 * tmp4 tmp6 = -1000000000.0 tmp7 = tl.where(tmp2, tmp6, tmp5) tmp9 = tmp7 - tmp8 tmp10 = tl_math.exp(tmp9) tmp12 = tmp10 / tmp11 tl.store(in_out_ptr0 + x5, tmp12, xmask) @triton.jit def triton_poi_fused_clone_5(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_add_mul_native_layer_norm_6(in_ptr0, in_ptr1, in_ptr2, 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_ptr2 + 4 * x0, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr2 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp14 = tl.load(in_ptr2 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp17 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp18 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp20 = tl.load(in_ptr2 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 + tmp1 tmp4 = tmp2 * tmp3 tmp7 = tmp5 + tmp6 tmp9 = tmp7 * tmp8 tmp10 = tmp4 + tmp9 tmp13 = tmp11 + tmp12 tmp15 = tmp13 * tmp14 tmp16 = tmp10 + tmp15 tmp19 = tmp17 + tmp18 tmp21 = tmp19 * tmp20 tmp22 = tmp16 + tmp21 tmp23 = 4.0 tmp24 = tmp22 / tmp23 tmp25 = tmp4 - tmp24 tmp26 = tmp25 * tmp25 tmp27 = tmp9 - tmp24 tmp28 = tmp27 * tmp27 tmp29 = tmp26 + tmp28 tmp30 = tmp15 - tmp24 tmp31 = tmp30 * tmp30 tmp32 = tmp29 + tmp31 tmp33 = tmp21 - tmp24 tmp34 = tmp33 * tmp33 tmp35 = tmp32 + tmp34 tmp36 = tmp35 / tmp23 tl.store(out_ptr0 + x0, tmp24, xmask) tl.store(out_ptr1 + x0, tmp36, xmask) @triton.jit def triton_poi_fused_add_mul_native_layer_norm_7(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, 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 + x2, xmask) tmp5 = 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') tmp14 = tl.load(in_ptr6 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 - tmp5 tmp8 = 1e-05 tmp9 = tmp7 + tmp8 tmp10 = libdevice.rsqrt(tmp9) tmp11 = tmp6 * tmp10 tmp13 = tmp11 * tmp12 tmp15 = tmp13 + tmp14 tl.store(out_ptr0 + x2, tmp15, xmask) @triton.jit def triton_poi_fused_relu_threshold_backward_8(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp5 = 0.0 tmp6 = tmp4 <= tmp5 tl.store(in_out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr0 + x2, tmp6, xmask) @triton.jit def triton_poi_fused_add_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) 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), (16, 4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4, 4), (4, 1)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (4, 4), (4, 1)) assert_size_stride(primals_8, (4,), (1,)) assert_size_stride(primals_9, (4, 4), (4, 1)) assert_size_stride(primals_10, (4,), (1,)) assert_size_stride(primals_11, (4, 4), (4, 1)) assert_size_stride(primals_12, (4,), (1,)) assert_size_stride(primals_13, (4,), (1,)) assert_size_stride(primals_14, (4,), (1,)) assert_size_stride(primals_15, (4, 4), (4, 1)) assert_size_stride(primals_16, (4,), (1,)) assert_size_stride(primals_17, (4, 4), (4, 1)) assert_size_stride(primals_18, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf1 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) get_raw_stream(0) triton_poi_fused_mul_native_layer_norm_0[grid(16)](primals_1, primals_2, buf0, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_mul_native_layer_norm_1[grid(64)](primals_1, primals_2, buf0, buf1, primals_3, primals_4, buf2, 64, XBLOCK= 64, num_warps=1, num_stages=1) del primals_3 del primals_4 buf3 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf2, (16, 4), (4, 1), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf3) buf4 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf2, (16, 4), (4, 1), 0), reinterpret_tensor(primals_7, (4, 4), (1, 4), 0), out=buf4) buf5 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf2, (16, 4), (4, 1), 0), reinterpret_tensor(primals_9, (4, 4), (1, 4), 0), out=buf5) buf6 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) triton_poi_fused_clone_2[grid(16, 4)](buf3, primals_6, buf6, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_6 buf7 = reinterpret_tensor(buf3, (4, 4, 1, 4), (16, 4, 4, 1), 0) del buf3 triton_poi_fused_clone_2[grid(16, 4)](buf4, primals_8, buf7, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_8 buf8 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf6, (16, 4, 1), (4, 1, 0), 0), reinterpret_tensor(buf7, (16, 1, 4), (4, 0, 1), 0), out=buf8) buf9 = reinterpret_tensor(buf4, (4, 4, 4, 1), (16, 4, 1, 64), 0) del buf4 buf10 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) triton_poi_fused__softmax_div_eq_masked_fill_3[grid(64)](primals_2, buf8, buf9, buf10, 64, XBLOCK=64, num_warps=1, num_stages=1) buf11 = reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf8 triton_poi_fused__softmax_div_eq_masked_fill_4[grid(256)](buf11, primals_2, buf9, buf10, 256, XBLOCK=128, num_warps=4, num_stages=1) buf12 = reinterpret_tensor(buf9, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf9 triton_poi_fused_clone_2[grid(16, 4)](buf5, primals_10, buf12, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_10 buf13 = reinterpret_tensor(buf5, (16, 4, 1), (4, 1, 1), 0) del buf5 extern_kernels.bmm(reinterpret_tensor(buf11, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf12, (16, 4, 1), (4, 1, 0), 0), out=buf13) buf14 = reinterpret_tensor(buf10, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf10 triton_poi_fused_clone_5[grid(16, 4)](buf13, buf14, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf15 = reinterpret_tensor(buf13, (16, 4), (4, 1), 0) del buf13 extern_kernels.addmm(primals_12, reinterpret_tensor(buf14, (16, 4), (4, 1), 0), reinterpret_tensor(primals_11, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf15) del primals_12 buf16 = buf1 del buf1 buf17 = buf0 del buf0 triton_poi_fused_add_mul_native_layer_norm_6[grid(16)](primals_1, buf15, primals_2, buf16, buf17, 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_mul_native_layer_norm_7[grid(64)](primals_1, buf15, primals_2, buf16, buf17, primals_13, primals_14, buf18, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf16 del buf17 del primals_14 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 buf23 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool) triton_poi_fused_relu_threshold_backward_8[grid(64)](buf20, primals_16, buf23, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_16 buf21 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf20, (16, 4), (4, 1), 0), reinterpret_tensor(primals_17, (4, 4), (1, 4), 0), out=buf21) buf22 = reinterpret_tensor(buf21, (4, 4, 4), (16, 4, 1), 0) del buf21 triton_poi_fused_add_9[grid(64)](buf22, primals_1, buf15, primals_18, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_18 return buf22, primals_1, primals_2, primals_13, reinterpret_tensor(buf2, (16, 4), (4, 1), 0), buf11, reinterpret_tensor(buf14, (16, 4), (4, 1), 0), buf15, reinterpret_tensor(buf18, (16, 4), (4, 1), 0 ), reinterpret_tensor(buf20, (16, 4), (4, 1), 0 ), primals_17, buf23, primals_15, primals_11, reinterpret_tensor(buf12, (16, 1, 4), (4, 1, 1), 0), reinterpret_tensor(buf6, (16, 1, 4), (4, 1, 1), 0), reinterpret_tensor(buf7, (16, 4, 1), (4, 1, 4), 0 ), primals_9, primals_7, primals_5 class AffineLayer(nn.Module): def __init__(self, dropout, d_model, d_ff): super(AffineLayer, self).__init__() self.w_1 = nn.Linear(d_model, d_ff) self.w_2 = nn.Linear(d_ff, d_model) self.dropout = nn.Dropout(dropout) def forward(self, x): return self.w_2(self.dropout(F.relu(self.w_1(x)))) class MultiHeadedAttention(nn.Module): def __init__(self, num_head, d_model, dropout=0.1): super(MultiHeadedAttention, self).__init__() assert d_model % num_head == 0 self.d_k = d_model // num_head self.h = num_head self.linear_key = nn.Linear(d_model, d_model) self.linear_value = nn.Linear(d_model, d_model) self.linear_query = nn.Linear(d_model, d_model) self.linear_out = nn.Linear(d_model, d_model) self.dropout = nn.Dropout(p=dropout) def attention(self, query, key, value, mask, dropout=None): d_k = query.size(-1) scores = torch.matmul(query, key.transpose(-2, -1)) / math.sqrt(d_k) scores = scores.masked_fill(mask == 0, -1000000000.0) p_attn = F.softmax(scores, dim=-1) if dropout is not None: p_attn = dropout(p_attn) return torch.matmul(p_attn, value), p_attn def forward(self, query, key, value, mask): nbatches = query.size(0) query = self.linear_query(query).view(nbatches, -1, self.h, self.d_k ).transpose(1, 2) key = self.linear_key(key).view(nbatches, -1, self.h, self.d_k ).transpose(1, 2) value = self.linear_value(value).view(nbatches, -1, self.h, self.d_k ).transpose(1, 2) mask = mask.unsqueeze(1) x, _attn = self.attention(query, key, value, mask, dropout=self.dropout ) x = x.transpose(1, 2).contiguous().view(nbatches, -1, self.h * self.d_k ) return self.linear_out(x) class EncoderLayerNew(nn.Module): def __init__(self, num_head, dropout, d_model, d_ff): super(EncoderLayerNew, self).__init__() self.att_layer = MultiHeadedAttention(num_head, d_model, dropout) self.norm_att = nn.LayerNorm(d_model) self.dropout_att = nn.Dropout(dropout) self.affine_layer = AffineLayer(dropout, d_model, d_ff) self.norm_affine = nn.LayerNorm(d_model) self.dropout_affine = nn.Dropout(dropout) def forward(self, input_0, input_1): primals_5 = self.att_layer.linear_key.weight primals_3 = self.att_layer.linear_key.bias primals_7 = self.att_layer.linear_value.weight primals_4 = self.att_layer.linear_value.bias primals_9 = self.att_layer.linear_query.weight primals_6 = self.att_layer.linear_query.bias primals_11 = self.att_layer.linear_out.weight primals_8 = self.att_layer.linear_out.bias primals_10 = self.norm_att.weight primals_12 = self.norm_att.bias primals_15 = self.affine_layer.w_1.weight primals_13 = self.affine_layer.w_1.bias primals_17 = self.affine_layer.w_2.weight primals_14 = self.affine_layer.w_2.bias primals_16 = self.norm_affine.weight primals_18 = self.norm_affine.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, primals_15, primals_16, primals_17, primals_18]) return output[0]
iamxpy/pointer_summarizer
EncoderLayer
false
6,878
[ "Apache-2.0" ]
1
ebeb2ad32a45162c0da14dac0b6241b0b0d00fa0
https://github.com/iamxpy/pointer_summarizer/tree/ebeb2ad32a45162c0da14dac0b6241b0b0d00fa0
SoftDiceLossV2
import torch import torch.nn as nn import torch.cuda.amp as amp class SoftDiceLossV2Func(torch.autograd.Function): """ compute backward directly for better numeric stability """ @staticmethod @amp.custom_fwd def forward(ctx, logits, labels, p, smooth): logits = logits.float() probs = torch.sigmoid(logits) numer = 2 * (probs * labels).sum(dim=(1, 2)) + smooth denor = (probs.pow(p) + labels).sum(dim=(1, 2)) + smooth loss = 1.0 - numer / denor ctx.vars = probs, labels, numer, denor, p, smooth return loss @staticmethod @amp.custom_bwd def backward(ctx, grad_output): """ compute gradient of soft-dice loss """ probs, labels, numer, denor, p, _smooth = ctx.vars M = numer.view(-1, 1, 1) - (probs * labels).mul_(2) N = denor.view(-1, 1, 1) - probs.pow(p) mppi_1 = probs.pow(p - 1).mul_(p).mul_(M) grads = torch.where(labels == 1, probs.pow(p).mul_(2 * (1.0 - p)) - mppi_1 + N.mul_(2), -mppi_1) grads = grads.div_((probs.pow(p) + N).pow(2)).mul_(probs).mul_(1.0 - probs) grads = grads.mul_(grad_output.view(-1, 1, 1)).neg_() return grads, None, None, None class SoftDiceLossV2(nn.Module): """ soft-dice loss, useful in binary segmentation """ def __init__(self, p=1, smooth=1, reduction='mean'): super(SoftDiceLossV2, self).__init__() self.p = p self.smooth = smooth self.reduction = reduction def forward(self, logits, labels): """ args: logits: tensor of shape (N, H, W) args: label: tensor of shape(N, H, W) """ loss = SoftDiceLossV2Func.apply(logits, labels, self.p, self.smooth) if self.reduction == 'mean': loss = loss.mean() elif self.reduction == 'sum': loss = loss.sum() 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 import torch.cuda.amp as amp 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_mul_sigmoid_sum_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 16 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r2 = rindex x0 = xindex % 4 x1 = xindex // 4 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * r2 + 64 * x1), xmask, other=0.0) tmp2 = tl.load(in_ptr1 + (x0 + 4 * r2 + 64 * x1), xmask, other=0.0) tmp1 = tl.sigmoid(tmp0) tmp3 = tmp1 * tmp2 tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tmp1 + tmp2 tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK]) tmp11 = tl.where(xmask, tmp9, 0) tmp12 = tl.sum(tmp11, 1)[:, None] tl.store(out_ptr0 + x3, tmp7, xmask) tl.store(out_ptr1 + x3, tmp12, xmask) @triton.jit def triton_per_fused_add_div_mean_mul_rsub_1(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp5 = tl.load(in_ptr1 + r0, None) tmp1 = 2.0 tmp2 = tmp0 * tmp1 tmp3 = 1.0 tmp4 = tmp2 + tmp3 tmp6 = tmp5 + tmp3 tmp7 = tmp4 / tmp6 tmp8 = tmp3 - tmp7 tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK]) tmp11 = tl.sum(tmp9, 1)[:, None] tmp12 = 16.0 tmp13 = tmp11 / tmp12 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp13, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_per_fused_add_mul_sigmoid_sum_0[grid(16)](arg0_1, arg1_1, buf0, buf1, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 buf2 = empty_strided_cuda((), (), torch.float32) buf3 = buf2 del buf2 triton_per_fused_add_div_mean_mul_rsub_1[grid(1)](buf3, buf0, buf1, 1, 16, XBLOCK=1, num_warps=2, num_stages=1) del buf0 del buf1 return buf3, class SoftDiceLossV2Func(torch.autograd.Function): """ compute backward directly for better numeric stability """ @staticmethod @amp.custom_fwd def forward(ctx, logits, labels, p, smooth): logits = logits.float() probs = torch.sigmoid(logits) numer = 2 * (probs * labels).sum(dim=(1, 2)) + smooth denor = (probs.pow(p) + labels).sum(dim=(1, 2)) + smooth loss = 1.0 - numer / denor ctx.vars = probs, labels, numer, denor, p, smooth return loss @staticmethod @amp.custom_bwd def backward(ctx, grad_output): """ compute gradient of soft-dice loss """ probs, labels, numer, denor, p, _smooth = ctx.vars M = numer.view(-1, 1, 1) - (probs * labels).mul_(2) N = denor.view(-1, 1, 1) - probs.pow(p) mppi_1 = probs.pow(p - 1).mul_(p).mul_(M) grads = torch.where(labels == 1, probs.pow(p).mul_(2 * (1.0 - p)) - mppi_1 + N.mul_(2), -mppi_1) grads = grads.div_((probs.pow(p) + N).pow(2)).mul_(probs).mul_(1.0 - probs) grads = grads.mul_(grad_output.view(-1, 1, 1)).neg_() return grads, None, None, None class SoftDiceLossV2New(nn.Module): """ soft-dice loss, useful in binary segmentation """ def __init__(self, p=1, smooth=1, reduction='mean'): super(SoftDiceLossV2New, self).__init__() self.p = p self.smooth = smooth self.reduction = reduction def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
imvladikon/pytorch-loss
SoftDiceLossV2
false
6,879
[ "MIT" ]
1
6cfaabe1be898e1ff000b3dffb46d0ef09096f6b
https://github.com/imvladikon/pytorch-loss/tree/6cfaabe1be898e1ff000b3dffb46d0ef09096f6b
DilConv1dWithGLU
import torch import torch.nn as nn import torch.nn.functional as F class DilConv1dWithGLU(nn.Module): def __init__(self, num_channels, dilation, lenght=100, kernel_size=2, activation=F.leaky_relu, residual_connection=True, dropout=0.2): super(DilConv1dWithGLU, self).__init__() self.dilation = dilation self.start_ln = nn.LayerNorm(num_channels) self.start_conv1x1 = nn.Conv1d(num_channels, num_channels, kernel_size=1) self.dilconv_ln = nn.LayerNorm(num_channels) self.dilated_conv = nn.Conv1d(num_channels, num_channels, dilation= dilation, kernel_size=kernel_size, padding=dilation) self.gate_ln = nn.LayerNorm(num_channels) self.end_conv1x1 = nn.Conv1d(num_channels, num_channels, kernel_size=1) self.gated_conv1x1 = nn.Conv1d(num_channels, num_channels, kernel_size=1) self.activation = activation self.buffer = None self.residual_connection = residual_connection def clear_buffer(self): self.buffer = None def forward(self, x_inp, sampling=False): x = self.start_ln(x_inp.transpose(1, 2)).transpose(1, 2) x = self.activation(x) x = self.start_conv1x1(x) x = self.dilconv_ln(x.transpose(1, 2)).transpose(1, 2) x = self.activation(x) if sampling: if self.buffer is None: self.buffer = x else: pre_buffer = torch.cat([self.buffer, x], dim=2) self.buffer = pre_buffer[:, :, -(self.dilation + 1):] if self.buffer.shape[2] == self.dilation + 1: x = self.buffer else: x = torch.cat([torch.zeros(self.buffer.shape[0], self. buffer.shape[1], self.dilation + 1 - self.buffer.shape[ 2], device=x_inp.device), self.buffer], dim=2) x = self.dilated_conv(x)[:, :, self.dilation:] x = x[:, :, :x_inp.shape[-1]] else: x = self.dilated_conv(x)[:, :, :x_inp.shape[-1]] x = self.gate_ln(x.transpose(1, 2)).transpose(1, 2) x = self.activation(x) x = self.end_conv1x1(x) * torch.sigmoid(self.gated_conv1x1(x)) if self.residual_connection: x = x + x_inp return x def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'num_channels': 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 from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.nn.functional as F assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_native_layer_norm_0(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask) tmp1 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask) tmp3 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask) tmp5 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tmp9 = tmp0 - tmp8 tmp10 = tmp9 * tmp9 tmp11 = tmp1 - tmp8 tmp12 = tmp11 * tmp11 tmp13 = tmp10 + tmp12 tmp14 = tmp3 - tmp8 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp8 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp19 / tmp7 tmp21 = 1e-05 tmp22 = tmp20 + tmp21 tmp23 = libdevice.rsqrt(tmp22) tl.store(out_ptr0 + x2, tmp8, xmask) tl.store(out_ptr1 + x2, tmp23, xmask) @triton.jit def triton_poi_fused_convolution_leaky_relu_native_layer_norm_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr1, out_ptr2, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y1 = yindex // 4 y0 = yindex % 4 tmp0 = tl.load(in_ptr0 + (x2 + 4 * y3), xmask & ymask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr1 + (x2 + 4 * y1), xmask & ymask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr2 + (x2 + 4 * y1), xmask & ymask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr3 + y0, ymask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tmp9 = 0.0 tmp10 = tmp8 > tmp9 tmp11 = 0.01 tmp12 = tmp8 * tmp11 tmp13 = tl.where(tmp10, tmp8, tmp12) tl.store(out_ptr1 + (y0 + 4 * x2 + 16 * y1), tmp13, xmask & ymask) tl.store(out_ptr2 + (x2 + 4 * y3), tmp13, xmask & ymask) @triton.jit def triton_poi_fused_convolution_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 4 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) @triton.jit def triton_poi_fused_convolution_3(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 80 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 5 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) @triton.jit def triton_poi_fused_native_layer_norm_4(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 20 * x1), xmask) tmp1 = tl.load(in_ptr0 + (5 + x0 + 20 * x1), xmask) tmp3 = tl.load(in_ptr0 + (10 + x0 + 20 * x1), xmask) tmp5 = tl.load(in_ptr0 + (15 + x0 + 20 * x1), xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tmp9 = tmp0 - tmp8 tmp10 = tmp9 * tmp9 tmp11 = tmp1 - tmp8 tmp12 = tmp11 * tmp11 tmp13 = tmp10 + tmp12 tmp14 = tmp3 - tmp8 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp8 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp19 / tmp7 tmp21 = 1e-05 tmp22 = tmp20 + tmp21 tmp23 = libdevice.rsqrt(tmp22) tl.store(out_ptr0 + x2, tmp8, xmask) tl.store(out_ptr1 + x2, tmp23, xmask) @triton.jit def triton_poi_fused_convolution_leaky_relu_native_layer_norm_5(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr1, out_ptr2, out_ptr3, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y1 = yindex // 4 y0 = yindex % 4 tmp0 = tl.load(in_ptr0 + (x2 + 5 * y3), xmask & ymask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr1 + (x2 + 4 * y1), xmask & ymask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr2 + (x2 + 4 * y1), xmask & ymask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr3 + y0, ymask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tmp9 = 0.0 tmp10 = tmp8 > tmp9 tmp11 = 0.01 tmp12 = tmp8 * tmp11 tmp13 = tl.where(tmp10, tmp8, tmp12) tl.store(out_ptr1 + (y0 + 4 * x2 + 16 * y1), tmp13, xmask & ymask) tl.store(out_ptr2 + (x2 + 4 * y3), tmp13, xmask & ymask) tl.store(out_ptr3 + (x2 + 4 * y3), tmp13, xmask & ymask) @triton.jit def triton_poi_fused_add_convolution_mul_sigmoid_6(in_out_ptr0, in_out_ptr1, 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 x1 = xindex // 4 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_out_ptr1 + x3, xmask) tmp4 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr2 + x3, xmask) tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tl.sigmoid(tmp5) tmp7 = tmp2 * tmp6 tmp9 = tmp7 + tmp8 tl.store(in_out_ptr0 + x3, tmp2, xmask) tl.store(in_out_ptr1 + x3, tmp5, xmask) tl.store(out_ptr0 + x3, tmp9, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15) = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (4,), (1,)) assert_size_stride(primals_8, (4, 4, 2), (8, 2, 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, 1), (4, 1, 1)) assert_size_stride(primals_13, (4,), (1,)) assert_size_stride(primals_14, (4, 4, 1), (4, 1, 1)) assert_size_stride(primals_15, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) buf1 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) get_raw_stream(0) triton_poi_fused_native_layer_norm_0[grid(16)](primals_1, buf0, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf3 = empty_strided_cuda((4, 4, 4), (16, 1, 4), torch.float32) buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_convolution_leaky_relu_native_layer_norm_1[grid(16, 4) ](primals_1, buf0, buf1, primals_2, primals_3, buf3, buf4, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) buf5 = extern_kernels.convolution(buf4, primals_4, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf5, (4, 4, 4), (16, 4, 1)) buf6 = buf5 del buf5 triton_poi_fused_convolution_2[grid(64)](buf6, primals_5, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_5 buf7 = buf1 del buf1 buf8 = buf0 del buf0 triton_poi_fused_native_layer_norm_0[grid(16)](buf6, buf7, buf8, 16, XBLOCK=16, num_warps=1, num_stages=1) buf10 = reinterpret_tensor(buf4, (4, 4, 4), (16, 1, 4), 0) del buf4 buf11 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_convolution_leaky_relu_native_layer_norm_1[grid(16, 4) ](buf6, buf7, buf8, primals_6, primals_7, buf10, buf11, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) buf12 = extern_kernels.convolution(buf11, primals_8, stride=(1,), padding=(1,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf12, (4, 4, 5), (20, 5, 1)) buf13 = buf12 del buf12 triton_poi_fused_convolution_3[grid(80)](buf13, primals_9, 80, XBLOCK=128, num_warps=4, num_stages=1) del primals_9 buf14 = buf8 del buf8 buf15 = buf7 del buf7 triton_poi_fused_native_layer_norm_4[grid(16)](buf13, buf14, buf15, 16, XBLOCK=16, num_warps=1, num_stages=1) buf17 = reinterpret_tensor(buf11, (4, 4, 4), (16, 1, 4), 0) del buf11 buf18 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) buf21 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_convolution_leaky_relu_native_layer_norm_5[grid(16, 4) ](buf13, buf14, buf15, primals_10, primals_11, buf17, buf18, buf21, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) del buf14 del buf15 buf19 = extern_kernels.convolution(buf18, primals_12, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf19, (4, 4, 4), (16, 4, 1)) del buf18 buf22 = extern_kernels.convolution(buf21, primals_14, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf22, (4, 4, 4), (16, 4, 1)) buf20 = buf19 del buf19 buf23 = buf22 del buf22 buf24 = buf21 del buf21 triton_poi_fused_add_convolution_mul_sigmoid_6[grid(64)](buf20, buf23, primals_13, primals_15, primals_1, buf24, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_13 del primals_15 return (buf24, primals_1, primals_2, primals_3, primals_4, primals_6, primals_7, primals_8, primals_10, primals_11, primals_12, primals_14, buf3, buf6, buf10, buf13, buf17, buf20, buf23) class DilConv1dWithGLUNew(nn.Module): def __init__(self, num_channels, dilation, lenght=100, kernel_size=2, activation=F.leaky_relu, residual_connection=True, dropout=0.2): super(DilConv1dWithGLUNew, self).__init__() self.dilation = dilation self.start_ln = nn.LayerNorm(num_channels) self.start_conv1x1 = nn.Conv1d(num_channels, num_channels, kernel_size=1) self.dilconv_ln = nn.LayerNorm(num_channels) self.dilated_conv = nn.Conv1d(num_channels, num_channels, dilation= dilation, kernel_size=kernel_size, padding=dilation) self.gate_ln = nn.LayerNorm(num_channels) self.end_conv1x1 = nn.Conv1d(num_channels, num_channels, kernel_size=1) self.gated_conv1x1 = nn.Conv1d(num_channels, num_channels, kernel_size=1) self.activation = activation self.buffer = None self.residual_connection = residual_connection def clear_buffer(self): self.buffer = None def forward(self, input_0): primals_2 = self.start_ln.weight primals_3 = self.start_ln.bias primals_4 = self.start_conv1x1.weight primals_5 = self.start_conv1x1.bias primals_6 = self.dilconv_ln.weight primals_7 = self.dilconv_ln.bias primals_8 = self.dilated_conv.weight primals_9 = self.dilated_conv.bias primals_10 = self.gate_ln.weight primals_11 = self.gate_ln.bias primals_12 = self.end_conv1x1.weight primals_13 = self.end_conv1x1.bias primals_14 = self.gated_conv1x1.weight primals_15 = self.gated_conv1x1.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15]) return output[0]
icyray/proGENTRL
DilConv1dWithGLU
false
6,880
[ "MIT" ]
1
c48305c3411ecb604c4f26f5e6b62f285e42e696
https://github.com/icyray/proGENTRL/tree/c48305c3411ecb604c4f26f5e6b62f285e42e696
CausalConv2d
import torch import torch.nn as nn class WNConv2d(nn.Module): def __init__(self, in_channel, out_channel, kernel_size, stride=1, padding=0, bias=True, activation=None): super().__init__() self.conv = nn.utils.weight_norm(nn.Conv2d(in_channel, out_channel, kernel_size, stride=stride, padding=padding, bias=bias)) self.out_channel = out_channel if isinstance(kernel_size, int): kernel_size = [kernel_size, kernel_size] self.kernel_size = kernel_size self.activation = activation def forward(self, input): out = self.conv(input) if self.activation is not None: out = self.activation(out) return out class CausalConv2d(nn.Module): def __init__(self, in_channel, out_channel, kernel_size, stride=1, padding='downright', activation=None): super().__init__() if isinstance(kernel_size, int): kernel_size = [kernel_size] * 2 self.kernel_size = kernel_size if padding == 'downright': pad = [kernel_size[1] - 1, 0, kernel_size[0] - 1, 0] elif padding == 'down' or padding == 'causal': pad = kernel_size[1] // 2 pad = [pad, pad, kernel_size[0] - 1, 0] self.causal = 0 if padding == 'causal': self.causal = kernel_size[1] // 2 self.pad = nn.ZeroPad2d(pad) self.conv = WNConv2d(in_channel, out_channel, kernel_size, stride= stride, padding=0, activation=activation) def forward(self, input): out = self.pad(input) if self.causal > 0: self.conv.conv.weight_v.data[:, :, -1, self.causal:].zero_() out = self.conv(out) return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channel': 4, 'out_channel': 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 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 = 784 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 7 % 7 x0 = xindex % 7 x2 = xindex // 49 x4 = xindex tmp0 = -3 + x1 tmp1 = tl.full([1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = -3 + x0 tmp4 = tmp3 >= tmp1 tmp5 = tmp2 & tmp4 tmp6 = tl.load(in_ptr0 + (-15 + x0 + 4 * x1 + 16 * x2), tmp5 & xmask, other=0.0) tl.store(out_ptr0 + x4, tmp6, xmask) @triton.jit def triton_per_fused__weight_norm_interface_1(in_out_ptr0, in_ptr0, in_ptr1, out_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) tmp7 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') 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) tmp8 = tmp7 / tmp6 tmp9 = tmp0 * tmp8 tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp6, xmask) tl.store(out_ptr0 + (r1 + 64 * x0), tmp9, xmask) @triton.jit def triton_poi_fused_convolution_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 1, 1, 1), (1, 1, 1, 1)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 7, 7), (196, 49, 7, 1), torch.float32) get_raw_stream(0) triton_poi_fused_constant_pad_nd_0[grid(784)](primals_1, buf0, 784, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 buf1 = empty_strided_cuda((4, 1, 1, 1), (1, 4, 4, 4), torch.float32) buf2 = reinterpret_tensor(buf1, (4, 1, 1, 1), (1, 1, 1, 1), 0) del buf1 buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_per_fused__weight_norm_interface_1[grid(4)](buf2, primals_3, primals_2, buf3, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) buf4 = extern_kernels.convolution(buf0, buf3, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 4, 4, 4), (64, 16, 4, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_2[grid(256)](buf5, primals_4, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_4 return buf5, buf3, primals_2, primals_3, buf0, buf2, buf3 class WNConv2d(nn.Module): def __init__(self, in_channel, out_channel, kernel_size, stride=1, padding=0, bias=True, activation=None): super().__init__() self.conv = nn.utils.weight_norm(nn.Conv2d(in_channel, out_channel, kernel_size, stride=stride, padding=padding, bias=bias)) self.out_channel = out_channel if isinstance(kernel_size, int): kernel_size = [kernel_size, kernel_size] self.kernel_size = kernel_size self.activation = activation def forward(self, input): out = self.conv(input) if self.activation is not None: out = self.activation(out) return out class CausalConv2dNew(nn.Module): def __init__(self, in_channel, out_channel, kernel_size, stride=1, padding='downright', activation=None): super().__init__() if isinstance(kernel_size, int): kernel_size = [kernel_size] * 2 self.kernel_size = kernel_size if padding == 'downright': pad = [kernel_size[1] - 1, 0, kernel_size[0] - 1, 0] elif padding == 'down' or padding == 'causal': pad = kernel_size[1] // 2 pad = [pad, pad, kernel_size[0] - 1, 0] self.causal = 0 if padding == 'causal': self.causal = kernel_size[1] // 2 self.pad = nn.ZeroPad2d(pad) self.conv = WNConv2d(in_channel, out_channel, kernel_size, stride= stride, padding=0, activation=activation) def forward(self, input_0): primals_4 = self.conv.conv.bias primals_2 = self.conv.conv.weight_g primals_1 = self.conv.conv.weight_v primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
imatge-upc/pixelcoordEDL
CausalConv2d
false
6,881
[ "MIT" ]
1
353632feed6ac8c93758c1a2a1b7a477e7ff053c
https://github.com/imatge-upc/pixelcoordEDL/tree/353632feed6ac8c93758c1a2a1b7a477e7ff053c
DY_Conv2d
import torch import torch.nn as nn import torch.nn.functional as F class DY_Conv2d(nn.Conv2d): def __init__(self, in_chan, out_chan, kernel_size=3, stride=1, padding= 1, dilation=1, groups=1, bias=False, act=nn.ReLU(inplace=True), K=4, temperature=30, temp_anneal_steps=3000): super(DY_Conv2d, self).__init__(in_chan, out_chan * K, kernel_size, stride=stride, padding=padding, dilation=dilation, groups= groups, bias=bias) assert in_chan // 4 > 0 self.K = K self.act = act self.se_conv1 = nn.Conv2d(in_chan, in_chan // 4, 1, 1, 0, bias=True) self.se_conv2 = nn.Conv2d(in_chan // 4, K, 1, 1, 0, bias=True) self.temperature = temperature self.temp_anneal_steps = temp_anneal_steps self.temp_interval = (temperature - 1) / temp_anneal_steps def get_atten(self, x): bs, _, _h, _w = x.size() atten = torch.mean(x, dim=(2, 3), keepdim=True) atten = self.se_conv1(atten) atten = self.act(atten) atten = self.se_conv2(atten) if self.training and self.temp_anneal_steps > 0: atten = atten / self.temperature self.temperature -= self.temp_interval self.temp_anneal_steps -= 1 atten = atten.softmax(dim=1).view(bs, -1) return atten def forward(self, x): bs, _, h, w = x.size() atten = self.get_atten(x) _out_chan, in_chan, k1, k2 = self.weight.size() W = self.weight.view(1, self.K, -1, in_chan, k1, k2) W = (W * atten.view(bs, self.K, 1, 1, 1, 1)).sum(dim=1) W = W.view(-1, in_chan, k1, k2) b = self.bias if b is not None: b = b.view(1, self.K, -1) b = (b * atten.view(bs, self.K, 1)).sum(dim=1).view(-1) x = x.view(1, -1, h, w) out = F.conv2d(x, W, b, self.stride, self.padding, self.dilation, self.groups * bs) out = out.view(bs, -1, out.size(2), out.size(3)) return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_chan': 4, 'out_chan': 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_per_fused_mean_0(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 16 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, 0) tmp4 = tl.sum(tmp3, 1)[:, None] tmp5 = 16.0 tmp6 = tmp4 / tmp5 tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp6, xmask) @triton.jit def triton_poi_fused_convolution_relu_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp3 = tmp0 + tmp2 tmp4 = tl.full([1], 0, tl.int32) tmp5 = triton_helpers.maximum(tmp4, tmp3) tl.store(in_out_ptr0 + x0, tmp5, xmask) @triton.jit def triton_poi_fused_convolution_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x2, tmp2, xmask) @triton.jit def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_mul_sum_5(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 % 144 x1 = xindex // 144 x2 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (144 + x0), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (288 + x0), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (432 + x0), xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr1 + (3 + 4 * x1), 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 tl.store(out_ptr0 + x2, tmp14, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (1, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_3, (1,), (1,)) assert_size_stride(primals_4, (4, 1, 1, 1), (1, 1, 1, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (16, 4, 3, 3), (36, 9, 3, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) buf1 = reinterpret_tensor(buf0, (4, 4, 1, 1), (4, 1, 1, 1), 0) del buf0 get_raw_stream(0) triton_per_fused_mean_0[grid(16)](buf1, primals_1, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) buf2 = extern_kernels.convolution(buf1, primals_2, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 1, 1, 1), (1, 1, 1, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_relu_1[grid(4)](buf3, primals_3, 4, XBLOCK=4, num_warps=1, num_stages=1) del primals_3 buf4 = extern_kernels.convolution(buf3, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 4, 1, 1), (4, 1, 1, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_2[grid(16)](buf5, primals_5, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_5 buf6 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) triton_poi_fused__softmax_3[grid(16)](buf5, buf6, 16, XBLOCK=16, num_warps=1, num_stages=1) buf7 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32) triton_poi_fused__softmax_4[grid(16)](buf6, buf7, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf6 buf8 = empty_strided_cuda((4, 4, 4, 3, 3), (144, 36, 9, 3, 1), torch.float32) triton_poi_fused_mul_sum_5[grid(576)](primals_6, buf7, buf8, 576, XBLOCK=128, num_warps=4, num_stages=1) del buf7 buf9 = extern_kernels.convolution(reinterpret_tensor(primals_1, (1, 16, 4, 4), (256, 16, 4, 1), 0), reinterpret_tensor(buf8, (16, 4, 3, 3), (36, 9, 3, 1), 0), stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=4, bias=None) assert_size_stride(buf9, (1, 16, 4, 4), (256, 16, 4, 1)) return reinterpret_tensor(buf9, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), primals_2, primals_4, primals_6, buf1, buf3, buf5, reinterpret_tensor( buf8, (16, 4, 3, 3), (36, 9, 3, 1), 0), reinterpret_tensor(primals_1, (1, 16, 4, 4), (256, 16, 4, 1), 0) class DY_Conv2dNew(nn.Conv2d): def __init__(self, in_chan, out_chan, kernel_size=3, stride=1, padding= 1, dilation=1, groups=1, bias=False, act=nn.ReLU(inplace=True), K=4, temperature=30, temp_anneal_steps=3000): super(DY_Conv2dNew, self).__init__(in_chan, out_chan * K, kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias) assert in_chan // 4 > 0 self.K = K self.act = act self.se_conv1 = nn.Conv2d(in_chan, in_chan // 4, 1, 1, 0, bias=True) self.se_conv2 = nn.Conv2d(in_chan // 4, K, 1, 1, 0, bias=True) self.temperature = temperature self.temp_anneal_steps = temp_anneal_steps self.temp_interval = (temperature - 1) / temp_anneal_steps def get_atten(self, x): bs, _, _h, _w = x.size() atten = torch.mean(x, dim=(2, 3), keepdim=True) atten = self.se_conv1(atten) atten = self.act(atten) atten = self.se_conv2(atten) if self.training and self.temp_anneal_steps > 0: atten = atten / self.temperature self.temperature -= self.temp_interval self.temp_anneal_steps -= 1 atten = atten.softmax(dim=1).view(bs, -1) return atten def forward(self, input_0): primals_6 = self.weight primals_2 = self.se_conv1.weight primals_3 = self.se_conv1.bias primals_4 = self.se_conv2.weight primals_5 = self.se_conv2.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6]) return output[0]
imvladikon/pytorch-loss
DY_Conv2d
false
6,882
[ "MIT" ]
1
6cfaabe1be898e1ff000b3dffb46d0ef09096f6b
https://github.com/imvladikon/pytorch-loss/tree/6cfaabe1be898e1ff000b3dffb46d0ef09096f6b
Conv2dSame
import math import torch import torch.nn.functional as F import torch.nn as nn from typing import List from typing import Optional from typing import Tuple from torch.jit.annotations import List def get_same_padding(x: 'int', k: 'int', s: 'int', d: 'int'): return max((math.ceil(x / s) - 1) * s + (k - 1) * d + 1 - x, 0) def pad_same(x, k: 'List[int]', s: 'List[int]', d: 'List[int]'=(1, 1), value: 'float'=0): ih, iw = x.size()[-2:] pad_h, pad_w = get_same_padding(ih, k[0], s[0], d[0]), get_same_padding(iw, k[1], s[1], d[1]) if pad_h > 0 or pad_w > 0: x = F.pad(x, [pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2], value=value) return x def conv2d_same(x, weight: 'torch.Tensor', bias: 'Optional[torch.Tensor]'= None, stride: 'Tuple[int, int]'=(1, 1), padding: 'Tuple[int, int]'=(0, 0), dilation: 'Tuple[int, int]'=(1, 1), groups: 'int'=1): x = pad_same(x, weight.shape[-2:], stride, dilation) return F.conv2d(x, weight, bias, stride, (0, 0), dilation, groups) class Conv2dSame(nn.Conv2d): """ Tensorflow like 'SAME' convolution wrapper for 2D convolutions """ def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True): super(Conv2dSame, self).__init__(in_channels, out_channels, kernel_size, stride, 0, dilation, groups, bias) def forward(self, x): return conv2d_same(x, self.weight, self.bias, self.stride, self. padding, self.dilation, self.groups) 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 math import torch.nn.functional as F import torch.nn as nn from typing import List from typing import Optional from typing import Tuple from torch.jit.annotations import List assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_constant_pad_nd_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 784 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 7 % 7 x0 = xindex % 7 x2 = xindex // 49 x4 = xindex tmp0 = -1 + x1 tmp1 = tl.full([1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = -1 + x0 tmp6 = tmp5 >= tmp1 tmp7 = tmp5 < tmp3 tmp8 = tmp2 & tmp4 tmp9 = tmp8 & tmp6 tmp10 = tmp9 & tmp7 tmp11 = tl.load(in_ptr0 + (-5 + x0 + 4 * x1 + 16 * x2), tmp10 & xmask, other=0.0) tl.store(out_ptr0 + x4, tmp11, xmask) @triton.jit def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = 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, 7, 7), (196, 49, 7, 1), torch.float32) get_raw_stream(0) triton_poi_fused_constant_pad_nd_0[grid(784)](primals_3, buf0, 784, XBLOCK=128, num_warps=4, num_stages=1) del primals_3 buf1 = 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(buf1, (4, 4, 4, 4), (64, 16, 4, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_1[grid(256)](buf2, primals_2, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 return buf2, primals_1, buf0 def get_same_padding(x: 'int', k: 'int', s: 'int', d: 'int'): return max((math.ceil(x / s) - 1) * s + (k - 1) * d + 1 - x, 0) def pad_same(x, k: 'List[int]', s: 'List[int]', d: 'List[int]'=(1, 1), value: 'float'=0): ih, iw = x.size()[-2:] pad_h, pad_w = get_same_padding(ih, k[0], s[0], d[0]), get_same_padding(iw, k[1], s[1], d[1]) if pad_h > 0 or pad_w > 0: x = F.pad(x, [pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2], value=value) return x def conv2d_same(x, weight: 'torch.Tensor', bias: 'Optional[torch.Tensor]'= None, stride: 'Tuple[int, int]'=(1, 1), padding: 'Tuple[int, int]'=(0, 0), dilation: 'Tuple[int, int]'=(1, 1), groups: 'int'=1): x = pad_same(x, weight.shape[-2:], stride, dilation) return F.conv2d(x, weight, bias, stride, (0, 0), dilation, groups) class Conv2dSameNew(nn.Conv2d): """ Tensorflow like 'SAME' convolution wrapper for 2D convolutions """ def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True): super(Conv2dSameNew, self).__init__(in_channels, out_channels, kernel_size, stride, 0, dilation, groups, bias) 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]
infomon/meta_nas
Conv2dSame
false
6,883
[ "Apache-2.0" ]
1
b81b7de86d26ae1ec0d6646b4277f3c918e5e35d
https://github.com/infomon/meta_nas/tree/b81b7de86d26ae1ec0d6646b4277f3c918e5e35d
SelfAttention_naive
import math import torch from torch import nn import torch.nn.functional as F class SelfAttention_naive(nn.Module): def __init__(self, dim_emb, dim_internal, heads=8, mask=False, dropout= 0.0, dtype=torch.float32): """ A single self attention block :param dim_emb: embedding dimension :param dim_internal: dimension of internal representation, usually the same as dim_emb :param head: number of multi head :param mask """ super().__init__() self.dim_emb = dim_emb self.dim_internal = dim_internal self.heads = heads self.mask = mask self.toqueries = nn.Linear(dim_emb, dim_internal).type(dtype) self.tokeys = nn.Linear(dim_emb, dim_internal).type(dtype) self.tovalues = nn.Linear(dim_emb, dim_internal).type(dtype) self.kSqrt_dim_emb = math.sqrt(self.dim_emb) def forward(self, x): _b, _t, e = x.size() assert e == self.dim_emb, f'Input embedding ({e}) should match the layer embedding ({self.dim_emb})' queries = self.toqueries(x) keys = self.tokeys(x) values = self.tovalues(x) keys_transposed = keys.transpose(-2, -1) dot = torch.matmul(queries, keys_transposed) / self.kSqrt_dim_emb p_attn = F.softmax(dot, dim=2) z = torch.matmul(p_attn, values) return z def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'dim_emb': 4, 'dim_internal': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import math 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_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) tmp3 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp1 = 1.0 tmp2 = tmp0 * tmp1 tmp4 = tmp3 * tmp1 tmp6 = tmp5 * tmp1 tmp7 = triton_helpers.maximum(tmp4, tmp6) tmp9 = tmp8 * tmp1 tmp10 = triton_helpers.maximum(tmp7, tmp9) tmp12 = tmp11 * tmp1 tmp13 = triton_helpers.maximum(tmp10, tmp12) tmp14 = tmp2 - tmp13 tmp15 = 0.5 tmp16 = tmp14 * tmp15 tmp17 = tl_math.exp(tmp16) tl.store(out_ptr0 + x2, tmp17, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (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,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_3, reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_2 del primals_3 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf1) del primals_4 del primals_5 buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf2) del primals_6 del primals_7 buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf1, (4, 4, 4), (16, 1, 4), 0), out=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) buf6 = buf4 del buf4 extern_kernels.bmm(buf5, reinterpret_tensor(buf2, (4, 4, 4), (16, 4, 1), 0), out=buf6) return buf6, reinterpret_tensor(primals_1, (16, 4), (4, 1), 0 ), buf5, reinterpret_tensor(buf2, (4, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf0, (4, 4, 4), (16, 1, 4), 0 ), reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 0) class SelfAttention_naiveNew(nn.Module): def __init__(self, dim_emb, dim_internal, heads=8, mask=False, dropout= 0.0, dtype=torch.float32): """ A single self attention block :param dim_emb: embedding dimension :param dim_internal: dimension of internal representation, usually the same as dim_emb :param head: number of multi head :param mask """ super().__init__() self.dim_emb = dim_emb self.dim_internal = dim_internal self.heads = heads self.mask = mask self.toqueries = nn.Linear(dim_emb, dim_internal).type(dtype) self.tokeys = nn.Linear(dim_emb, dim_internal).type(dtype) self.tovalues = nn.Linear(dim_emb, dim_internal).type(dtype) self.kSqrt_dim_emb = math.sqrt(self.dim_emb) def forward(self, input_0): primals_2 = self.toqueries.weight primals_3 = self.toqueries.bias primals_4 = self.tokeys.weight primals_5 = self.tokeys.bias primals_6 = self.tovalues.weight primals_7 = self.tovalues.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
insop/transformer_simple
SelfAttention_naive
false
6,884
[ "Apache-2.0" ]
1
d07e6c3b9ddc9687d332ac3a980bbce22880ad46
https://github.com/insop/transformer_simple/tree/d07e6c3b9ddc9687d332ac3a980bbce22880ad46
MultiHead
import torch import torch.nn as nn class Attention(nn.Module): def __init__(self): super().__init__() self.softmax = nn.Softmax(dim=-1) def forward(self, Q, K, V, mask=None, dk=64): w = torch.bmm(Q, K.transpose(1, 2)) if mask is not None: assert w.size() == mask.size() w.masked_fill_(mask, -float('inf')) w = self.softmax(w / dk ** 0.5) c = torch.bmm(w, V) return c class MultiHead(nn.Module): def __init__(self, hidden_size, n_splits): super().__init__() self.hidden_size = hidden_size self.n_splits = n_splits self.Q_linear = nn.Linear(hidden_size, hidden_size, bias=False) self.K_linear = nn.Linear(hidden_size, hidden_size, bias=False) self.V_linear = nn.Linear(hidden_size, hidden_size, bias=False) self.linear = nn.Linear(hidden_size, hidden_size, bias=False) self.attn = Attention() def forward(self, Q, K, V, mask=None): QWs = self.Q_linear(Q).split(self.hidden_size // self.n_splits, dim=-1) KWs = self.K_linear(K).split(self.hidden_size // self.n_splits, dim=-1) VWs = self.V_linear(V).split(self.hidden_size // self.n_splits, dim=-1) QWs = torch.cat(QWs, dim=0) KWs = torch.cat(KWs, dim=0) VWs = torch.cat(VWs, dim=0) if mask is not None: mask = torch.cat([mask for _ in range(self.n_splits)], dim=0) c = self.attn(QWs, KWs, VWs, mask=mask, dk=self.hidden_size // self .n_splits) c = c.split(Q.size(0), dim=0) c = self.linear(torch.cat(c, dim=-1)) return c def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4]), torch.rand([4, 4, 4]) ] def get_init_inputs(): return [[], {'hidden_size': 4, 'n_splits': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 x0 = xindex % 4 x2 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (4 * x0 + 16 * x1), tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp6 = tmp0 >= tmp3 tmp7 = tl.full([1], 8, tl.int64) tmp8 = tmp0 < tmp7 tmp9 = tmp6 & tmp8 tmp10 = tl.load(in_ptr0 + (1 + 4 * x0 + 16 * (-4 + x1)), tmp9 & xmask, eviction_policy='evict_last', other=0.0) tmp11 = tmp0 >= tmp7 tmp12 = tl.full([1], 12, tl.int64) tmp13 = tmp0 < tmp12 tmp14 = tmp11 & tmp13 tmp15 = tl.load(in_ptr0 + (2 + 4 * x0 + 16 * (-8 + x1)), tmp14 & xmask, eviction_policy='evict_last', other=0.0) tmp16 = tmp0 >= tmp12 tl.full([1], 16, tl.int64) tmp19 = tl.load(in_ptr0 + (3 + 4 * x0 + 16 * (-12 + x1)), 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 + x2, tmp22, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp3 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp1 = 1.0 tmp2 = tmp0 * tmp1 tmp4 = tmp3 * tmp1 tmp6 = tmp5 * tmp1 tmp7 = triton_helpers.maximum(tmp4, tmp6) tmp9 = tmp8 * tmp1 tmp10 = triton_helpers.maximum(tmp7, tmp9) tmp12 = tmp11 * tmp1 tmp13 = triton_helpers.maximum(tmp10, tmp12) tmp14 = tmp2 - tmp13 tmp15 = tmp14 * tmp1 tmp16 = tl_math.exp(tmp15) tl.store(out_ptr0 + x2, tmp16, xmask) @triton.jit def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_cat_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 % 4 x1 = xindex // 4 x2 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + x1, tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp6 = tmp0 >= tmp3 tmp7 = tl.full([1], 2, tl.int64) tmp8 = tmp0 < tmp7 tmp9 = tmp6 & tmp8 tmp10 = tl.load(in_ptr0 + (16 + x1), 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 + (32 + x1), tmp14 & xmask, eviction_policy= 'evict_last', other=0.0) tmp16 = tmp0 >= tmp12 tl.full([1], 4, tl.int64) tmp19 = tl.load(in_ptr0 + (48 + x1), 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 + x2, tmp22, 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), (16, 4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_5, (4, 4), (4, 1)) assert_size_stride(primals_6, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_7, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_2, (16, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_4, (16, 4), (4, 1), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf1) del primals_3 buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_6, (16, 4), (4, 1), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf2) del primals_5 buf3 = empty_strided_cuda((16, 4, 1), (4, 1, 64), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(64)](buf0, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) buf4 = reinterpret_tensor(buf0, (16, 4, 1), (4, 1, 64), 0) del buf0 triton_poi_fused_cat_0[grid(64)](buf2, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) buf5 = reinterpret_tensor(buf2, (16, 4, 1), (4, 1, 1), 0) del buf2 triton_poi_fused_cat_0[grid(64)](buf1, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf6 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(buf3, reinterpret_tensor(buf5, (16, 1, 4), (4, 0, 1), 0), out=buf6) buf7 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_1[grid(256)](buf6, buf7, 256, XBLOCK=256, num_warps=4, num_stages=1) buf8 = buf6 del buf6 triton_poi_fused__softmax_2[grid(256)](buf7, buf8, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf7 buf9 = reinterpret_tensor(buf1, (16, 4, 1), (4, 1, 1), 0) del buf1 extern_kernels.bmm(buf8, buf4, out=buf9) buf10 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_cat_3[grid(64)](buf9, buf10, 64, XBLOCK=64, num_warps=1, num_stages=1) buf11 = reinterpret_tensor(buf9, (16, 4), (4, 1), 0) del buf9 extern_kernels.mm(reinterpret_tensor(buf10, (16, 4), (4, 1), 0), reinterpret_tensor(primals_7, (4, 4), (1, 4), 0), out=buf11) return reinterpret_tensor(buf11, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(primals_2, (16, 4), (4, 1), 0 ), reinterpret_tensor(primals_4, (16, 4), (4, 1), 0 ), reinterpret_tensor(primals_6, (16, 4), (4, 1), 0 ), buf8, reinterpret_tensor(buf10, (16, 4), (4, 1), 0 ), primals_7, reinterpret_tensor(buf4, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0), buf5 class Attention(nn.Module): def __init__(self): super().__init__() self.softmax = nn.Softmax(dim=-1) def forward(self, Q, K, V, mask=None, dk=64): w = torch.bmm(Q, K.transpose(1, 2)) if mask is not None: assert w.size() == mask.size() w.masked_fill_(mask, -float('inf')) w = self.softmax(w / dk ** 0.5) c = torch.bmm(w, V) return c class MultiHeadNew(nn.Module): def __init__(self, hidden_size, n_splits): super().__init__() self.hidden_size = hidden_size self.n_splits = n_splits self.Q_linear = nn.Linear(hidden_size, hidden_size, bias=False) self.K_linear = nn.Linear(hidden_size, hidden_size, bias=False) self.V_linear = nn.Linear(hidden_size, hidden_size, bias=False) self.linear = nn.Linear(hidden_size, hidden_size, bias=False) self.attn = Attention() def forward(self, input_0, input_1, input_2): primals_1 = self.Q_linear.weight primals_3 = self.K_linear.weight primals_5 = self.V_linear.weight primals_7 = self.linear.weight primals_2 = input_0 primals_4 = input_1 primals_6 = input_2 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
iml1111/machine-translation
MultiHead
false
6,885
[ "MIT" ]
1
a7dd673efbe8a172c1df49e0d50482dc84008c37
https://github.com/iml1111/machine-translation/tree/a7dd673efbe8a172c1df49e0d50482dc84008c37
IOU
import torch import torch.multiprocessing def _iou(pred, target, size_average=True): b = pred.shape[0] IoU = 0.0 for i in range(0, b): Iand1 = torch.sum(target[i, :, :, :] * pred[i, :, :, :]) Ior1 = torch.sum(target[i, :, :, :]) + torch.sum(pred[i, :, :, :] ) - Iand1 IoU1 = Iand1 / Ior1 IoU = IoU + (1 - IoU1) return IoU / b class IOU(torch.nn.Module): def __init__(self, size_average=True): super(IOU, self).__init__() self.size_average = size_average def forward(self, pred, target): return _iou(pred, target, self.size_average) 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.multiprocessing assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_div_mul_rsub_sub_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 tmp0 = tl.load(in_ptr0 + r0, None) tmp4 = tl.load(in_ptr1 + r0, None) tmp12 = tl.load(in_ptr0 + (64 + r0), None) tmp16 = tl.load(in_ptr1 + (64 + r0), None) tmp24 = tl.load(in_ptr0 + (128 + r0), None) tmp28 = tl.load(in_ptr1 + (128 + r0), None) tmp36 = tl.load(in_ptr0 + (192 + r0), None) tmp40 = tl.load(in_ptr1 + (192 + r0), None) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.sum(tmp1, 1)[:, None] tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK]) tmp7 = tl.sum(tmp5, 1)[:, None] tmp8 = tmp0 * tmp4 tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK]) tmp11 = tl.sum(tmp9, 1)[:, None] tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK]) tmp15 = tl.sum(tmp13, 1)[:, None] tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK]) tmp19 = tl.sum(tmp17, 1)[:, None] tmp20 = tmp12 * tmp16 tmp21 = tl.broadcast_to(tmp20, [XBLOCK, RBLOCK]) tmp23 = tl.sum(tmp21, 1)[:, None] tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK]) tmp27 = tl.sum(tmp25, 1)[:, None] tmp29 = tl.broadcast_to(tmp28, [XBLOCK, RBLOCK]) tmp31 = tl.sum(tmp29, 1)[:, None] tmp32 = tmp24 * tmp28 tmp33 = tl.broadcast_to(tmp32, [XBLOCK, RBLOCK]) tmp35 = tl.sum(tmp33, 1)[:, None] tmp37 = tl.broadcast_to(tmp36, [XBLOCK, RBLOCK]) tmp39 = tl.sum(tmp37, 1)[:, None] tmp41 = tl.broadcast_to(tmp40, [XBLOCK, RBLOCK]) tmp43 = tl.sum(tmp41, 1)[:, None] tmp44 = tmp36 * tmp40 tmp45 = tl.broadcast_to(tmp44, [XBLOCK, RBLOCK]) tmp47 = tl.sum(tmp45, 1)[:, None] tmp48 = tmp3 + tmp7 tmp49 = tmp48 - tmp11 tmp50 = tmp11 / tmp49 tmp51 = 1.0 tmp52 = tmp51 - tmp50 tmp53 = 0.0 tmp54 = tmp52 + tmp53 tmp55 = tmp15 + tmp19 tmp56 = tmp55 - tmp23 tmp57 = tmp23 / tmp56 tmp58 = tmp51 - tmp57 tmp59 = tmp54 + tmp58 tmp60 = tmp27 + tmp31 tmp61 = tmp60 - tmp35 tmp62 = tmp35 / tmp61 tmp63 = tmp51 - tmp62 tmp64 = tmp59 + tmp63 tmp65 = tmp39 + tmp43 tmp66 = tmp65 - tmp47 tmp67 = tmp47 / tmp66 tmp68 = tmp51 - tmp67 tmp69 = tmp64 + tmp68 tmp70 = 0.25 tmp71 = tmp69 * tmp70 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp71, 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) buf12 = buf0 del buf0 buf13 = buf12 del buf12 get_raw_stream(0) triton_per_fused_add_div_mul_rsub_sub_sum_0[grid(1)](buf13, arg1_1, arg0_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf13, def _iou(pred, target, size_average=True): b = pred.shape[0] IoU = 0.0 for i in range(0, b): Iand1 = torch.sum(target[i, :, :, :] * pred[i, :, :, :]) Ior1 = torch.sum(target[i, :, :, :]) + torch.sum(pred[i, :, :, :] ) - Iand1 IoU1 = Iand1 / Ior1 IoU = IoU + (1 - IoU1) return IoU / b class IOUNew(torch.nn.Module): def __init__(self, size_average=True): super(IOUNew, self).__init__() self.size_average = size_average def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
intchous/SmartText
IOU
false
6,886
[ "MIT" ]
1
81abb84ce135a3859c32257d861c9e87b51f8c3f
https://github.com/intchous/SmartText/tree/81abb84ce135a3859c32257d861c9e87b51f8c3f
CecaModule
import math import torch import torch.nn.functional as F import torch.nn as nn class CecaModule(nn.Module): """Constructs a circular ECA module. ECA module where the conv uses circular padding rather than zero padding. Unlike the spatial dimension, the channels do not have inherent ordering nor locality. Although this module in essence, applies such an assumption, it is unnecessary to limit the channels on either "edge" from being circularly adapted to each other. This will fundamentally increase connectivity and possibly increase performance metrics (accuracy, robustness), without significantly impacting resource metrics (parameter size, throughput,latency, etc) Args: channels: Number of channels of the input feature map for use in adaptive kernel sizes for actual calculations according to channel. gamma, beta: when channel is given parameters of mapping function refer to original paper https://arxiv.org/pdf/1910.03151.pdf (default=None. if channel size not given, use k_size given for kernel size.) kernel_size: Adaptive selection of kernel size (default=3) """ def __init__(self, channels=None, kernel_size=3, gamma=2, beta=1): super(CecaModule, self).__init__() assert kernel_size % 2 == 1 if channels is not None: t = int(abs(math.log(channels, 2) + beta) / gamma) kernel_size = max(t if t % 2 else t + 1, 3) self.conv = nn.Conv1d(1, 1, kernel_size=kernel_size, padding=0, bias=False) self.padding = (kernel_size - 1) // 2 def forward(self, x): y = x.mean((2, 3)).view(x.shape[0], 1, -1) y = F.pad(y, (self.padding, self.padding), mode='circular') y = self.conv(y) y = y.view(x.shape[0], -1, 1, 1).sigmoid() return x * y.expand_as(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 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 @triton.jit def triton_per_fused_mean_0(in_ptr0, 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 tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, 0) tmp4 = tl.sum(tmp3, 1)[:, None] tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_poi_fused_copy_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 24 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 6 x1 = xindex // 6 x2 = xindex tmp0 = x0 tmp1 = tl.full([1], 5, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = -4 + x0 tmp4 = tl.full([1], 1, tl.int64) tmp5 = tmp3 < tmp4 tmp6 = tmp5 & tmp2 tmp7 = tmp0 >= tmp4 tmp8 = tmp0 < tmp1 tmp9 = tmp7 & tmp8 tmp10 = tmp9 & tmp6 tmp11 = tl.load(in_ptr0 + (-1 + x0 + 4 * x1), tmp10 & xmask, other=0.0) tmp12 = 16.0 tmp13 = tmp11 / tmp12 tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype) tmp15 = tl.where(tmp10, tmp13, tmp14) tmp16 = float('nan') tmp17 = tl.where(tmp9, tmp15, tmp16) tmp18 = tl.full(tmp17.shape, 0.0, tmp17.dtype) tmp19 = tl.where(tmp6, tmp17, tmp18) tmp20 = tmp3 >= tmp4 tmp21 = tmp3 < tmp1 tmp22 = tmp20 & tmp21 tmp23 = tmp22 & tmp2 tmp24 = tl.load(in_ptr0 + (-5 + x0 + 4 * x1), tmp23 & xmask, other=0.0) tmp25 = tmp24 / tmp12 tmp26 = tl.full(tmp25.shape, 0.0, tmp25.dtype) tmp27 = tl.where(tmp23, tmp25, tmp26) tmp28 = tl.where(tmp22, tmp27, tmp16) tmp29 = tl.where(tmp5, tmp19, tmp28) tmp30 = tl.full(tmp29.shape, 0.0, tmp29.dtype) tmp31 = tl.where(tmp2, tmp29, tmp30) tmp32 = tmp0 < tmp4 tmp33 = 4 + x0 tmp34 = tmp33 >= tmp4 tmp35 = tmp33 < tmp1 tmp36 = tmp34 & tmp35 tmp37 = tmp36 & tmp32 tmp38 = tl.load(in_ptr0 + (3 + x0 + 4 * x1), tmp37 & xmask, other=0.0) tmp39 = tmp38 / tmp12 tmp40 = tl.full(tmp39.shape, 0.0, tmp39.dtype) tmp41 = tl.where(tmp37, tmp39, tmp40) tmp42 = tl.where(tmp36, tmp41, tmp16) tmp43 = tl.full(tmp42.shape, 0.0, tmp42.dtype) tmp44 = tl.where(tmp32, tmp42, tmp43) tmp45 = tl.load(in_ptr0 + (-1 + x0 + 4 * x1), tmp9 & xmask, other=0.0) tmp46 = tmp45 / tmp12 tmp47 = tl.full(tmp46.shape, 0.0, tmp46.dtype) tmp48 = tl.where(tmp9, tmp46, tmp47) tmp49 = tl.where(tmp9, tmp48, tmp16) tmp50 = tl.where(tmp32, tmp44, tmp49) tmp51 = tl.where(tmp2, tmp31, tmp50) tl.store(out_ptr0 + x2, tmp51, xmask) @triton.jit def triton_poi_fused_mul_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 16 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tl.sigmoid(tmp1) tmp3 = tmp0 * tmp2 tl.store(out_ptr0 + x2, tmp3, xmask) def call(args): primals_1, primals_2 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (1, 1, 3), (3, 3, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_per_fused_mean_0[grid(16)](primals_1, buf0, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) buf2 = empty_strided_cuda((4, 1, 6), (6, 6, 1), torch.float32) triton_poi_fused_copy_1[grid(24)](buf0, buf2, 24, XBLOCK=32, num_warps=1, num_stages=1) del buf0 buf3 = extern_kernels.convolution(buf2, primals_2, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf3, (4, 1, 4), (4, 4, 1)) buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_mul_2[grid(256)](primals_1, buf3, buf4, 256, XBLOCK=256, num_warps=4, num_stages=1) return buf4, primals_1, primals_2, buf2, buf3 class CecaModuleNew(nn.Module): """Constructs a circular ECA module. ECA module where the conv uses circular padding rather than zero padding. Unlike the spatial dimension, the channels do not have inherent ordering nor locality. Although this module in essence, applies such an assumption, it is unnecessary to limit the channels on either "edge" from being circularly adapted to each other. This will fundamentally increase connectivity and possibly increase performance metrics (accuracy, robustness), without significantly impacting resource metrics (parameter size, throughput,latency, etc) Args: channels: Number of channels of the input feature map for use in adaptive kernel sizes for actual calculations according to channel. gamma, beta: when channel is given parameters of mapping function refer to original paper https://arxiv.org/pdf/1910.03151.pdf (default=None. if channel size not given, use k_size given for kernel size.) kernel_size: Adaptive selection of kernel size (default=3) """ def __init__(self, channels=None, kernel_size=3, gamma=2, beta=1): super(CecaModuleNew, self).__init__() assert kernel_size % 2 == 1 if channels is not None: t = int(abs(math.log(channels, 2) + beta) / gamma) kernel_size = max(t if t % 2 else t + 1, 3) self.conv = nn.Conv1d(1, 1, kernel_size=kernel_size, padding=0, bias=False) self.padding = (kernel_size - 1) // 2 def forward(self, input_0): primals_2 = self.conv.weight primals_1 = input_0 output = call([primals_1, primals_2]) return output[0]
infomon/meta_nas
CecaModule
false
6,887
[ "Apache-2.0" ]
1
b81b7de86d26ae1ec0d6646b4277f3c918e5e35d
https://github.com/infomon/meta_nas/tree/b81b7de86d26ae1ec0d6646b4277f3c918e5e35d
Binarizer
import torch from abc import ABC from sklearn.preprocessing import Binarizer class BaseOperator(ABC): """ Abstract class defining the basic structure for operator implementations in Hummingbird. """ def __init__(self, regression=False, classification=False, transformer= False, anomaly_detection=False, **kwargs): """ Args: regression: Whether operator is a regression model. classification: Whether the operator is a classification model. transformer: Whether the operator is a feature transformer. anomaly_detection: Whether the operator is an anomaly detection model. kwargs: Other keyword arguments. """ super().__init__() self.regression = regression self.classification = classification self.transformer = transformer self.anomaly_detection = anomaly_detection class Binarizer(BaseOperator, torch.nn.Module): """ Class implementing Binarizer operators in PyTorch. """ def __init__(self, threshold, device): super(Binarizer, self).__init__() self.transformer = True self.threshold = torch.nn.Parameter(torch.FloatTensor([threshold]), requires_grad=False) def forward(self, x): return torch.gt(x, self.threshold).float() def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'threshold': 4, 'device': 0}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from abc import ABC assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused__to_copy_gt_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr1 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp3 = tmp0 > tmp2 tmp4 = tmp3.to(tl.float32) tl.store(out_ptr0 + x0, tmp4, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (1,), (1,)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused__to_copy_gt_0[grid(256)](arg1_1, arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf0, class BaseOperator(ABC): """ Abstract class defining the basic structure for operator implementations in Hummingbird. """ def __init__(self, regression=False, classification=False, transformer= False, anomaly_detection=False, **kwargs): """ Args: regression: Whether operator is a regression model. classification: Whether the operator is a classification model. transformer: Whether the operator is a feature transformer. anomaly_detection: Whether the operator is an anomaly detection model. kwargs: Other keyword arguments. """ super().__init__() self.regression = regression self.classification = classification self.transformer = transformer self.anomaly_detection = anomaly_detection class BinarizerNew(BaseOperator, torch.nn.Module): """ Class implementing Binarizer operators in PyTorch. """ def __init__(self, threshold, device): super(BinarizerNew, self).__init__() self.transformer = True self.threshold = torch.nn.Parameter(torch.FloatTensor([threshold]), requires_grad=False) def forward(self, input_0): arg0_1 = self.threshold arg1_1 = input_0 output = call([arg0_1, arg1_1]) return output[0]
hannahaih/hummingbird
Binarizer
false
6,888
[ "MIT" ]
1
b8ec670b3c90ec7e87d3ae4a2b268075bd5eae65
https://github.com/hannahaih/hummingbird/tree/b8ec670b3c90ec7e87d3ae4a2b268075bd5eae65
ActorNetwork
import torch import torch as T import torch.nn as nn import torch.optim as optim class ActorNetwork(nn.Module): def __init__(self, alpha, state_dim, action_dim, fc1_dim, fc2_dim): super(ActorNetwork, self).__init__() self.fc1 = nn.Linear(state_dim, fc1_dim) self.ln1 = nn.LayerNorm(fc1_dim) self.fc2 = nn.Linear(fc1_dim, fc2_dim) self.ln2 = nn.LayerNorm(fc2_dim) self.action = nn.Linear(fc2_dim, action_dim) self.optimizer = optim.Adam(self.parameters(), lr=alpha) self def forward(self, state): x = T.relu(self.ln1(self.fc1(state))) x = T.relu(self.ln2(self.fc2(x))) action = T.tanh(self.action(x)) return action def save_checkpoint(self, checkpoint_file): T.save(self.state_dict(), checkpoint_file, _use_new_zipfile_serialization=False) def load_checkpoint(self, checkpoint_file): self.load_state_dict(T.load(checkpoint_file)) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'alpha': 4, 'state_dim': 4, 'action_dim': 4, 'fc1_dim': 4, 'fc2_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch as T import torch.nn as nn import torch.optim as optim assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_native_layer_norm_0(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tmp9 = tmp0 - tmp8 tmp10 = tmp9 * tmp9 tmp11 = tmp1 - tmp8 tmp12 = tmp11 * tmp11 tmp13 = tmp10 + tmp12 tmp14 = tmp3 - tmp8 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp8 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp19 / tmp7 tmp21 = 1e-05 tmp22 = tmp20 + tmp21 tmp23 = libdevice.rsqrt(tmp22) tl.store(out_ptr0 + x0, tmp8, xmask) tl.store(out_ptr1 + x0, tmp23, xmask) @triton.jit def triton_poi_fused_native_layer_norm_relu_threshold_backward_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 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 tmp9 = tl.full([1], 0, tl.int32) tmp10 = triton_helpers.maximum(tmp9, tmp8) tmp11 = 0.0 tmp12 = tmp10 <= tmp11 tl.store(out_ptr0 + x2, tmp10, xmask) tl.store(out_ptr1 + x2, tmp12, 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, primals_8, primals_9, primals_10, primals_11) = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (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,), (1,)) assert_size_stride(primals_9, (4,), (1,)) assert_size_stride(primals_10, (4, 4), (4, 1)) assert_size_stride(primals_11, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.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_native_layer_norm_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) buf11 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_native_layer_norm_relu_threshold_backward_1[grid(256) ](buf0, buf1, buf2, primals_4, primals_5, buf3, buf11, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf4) del primals_7 buf5 = buf2 del buf2 buf6 = buf1 del buf1 triton_poi_fused_native_layer_norm_0[grid(64)](buf4, buf5, buf6, 64, XBLOCK=64, num_warps=1, num_stages=1) buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf10 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_native_layer_norm_relu_threshold_backward_1[grid(256) ](buf4, buf5, buf6, primals_8, primals_9, buf7, buf10, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf5 del buf6 del primals_9 buf8 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf7, (64, 4), (4, 1), 0), reinterpret_tensor(primals_10, (4, 4), (1, 4), 0), out=buf8) buf9 = reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf8 triton_poi_fused_tanh_2[grid(256)](buf9, primals_11, 256, XBLOCK= 128, num_warps=4, num_stages=1) del primals_11 return buf9, primals_4, primals_8, reinterpret_tensor(primals_3, (64, 4 ), (4, 1), 0), buf0, reinterpret_tensor(buf3, (64, 4), (4, 1), 0 ), buf4, reinterpret_tensor(buf7, (64, 4), (4, 1), 0 ), buf9, primals_10, buf10, primals_6, buf11 class ActorNetworkNew(nn.Module): def __init__(self, alpha, state_dim, action_dim, fc1_dim, fc2_dim): super(ActorNetworkNew, self).__init__() self.fc1 = nn.Linear(state_dim, fc1_dim) self.ln1 = nn.LayerNorm(fc1_dim) self.fc2 = nn.Linear(fc1_dim, fc2_dim) self.ln2 = nn.LayerNorm(fc2_dim) self.action = nn.Linear(fc2_dim, action_dim) self.optimizer = optim.Adam(self.parameters(), lr=alpha) self def save_checkpoint(self, checkpoint_file): T.save(self.state_dict(), checkpoint_file, _use_new_zipfile_serialization=False) def load_checkpoint(self, checkpoint_file): self.load_state_dict(T.load(checkpoint_file)) def forward(self, input_0): primals_1 = self.fc1.weight primals_2 = self.fc1.bias primals_4 = self.ln1.weight primals_5 = self.ln1.bias primals_6 = self.fc2.weight primals_7 = self.fc2.bias primals_8 = self.ln2.weight primals_9 = self.ln2.bias primals_10 = self.action.weight primals_11 = self.action.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]
indigoLovee/TD3
ActorNetwork
false
6,889
[ "MIT" ]
1
0e86a40c27ec376b52e9f8e0e70db28e7411276b
https://github.com/indigoLovee/TD3/tree/0e86a40c27ec376b52e9f8e0e70db28e7411276b
CriticNetwork
import torch import torch as T import torch.nn as nn import torch.optim as optim class CriticNetwork(nn.Module): def __init__(self, beta, state_dim, action_dim, fc1_dim, fc2_dim): super(CriticNetwork, self).__init__() self.fc1 = nn.Linear(state_dim + action_dim, fc1_dim) self.ln1 = nn.LayerNorm(fc1_dim) self.fc2 = nn.Linear(fc1_dim, fc2_dim) self.ln2 = nn.LayerNorm(fc2_dim) self.q = nn.Linear(fc2_dim, 1) self.optimizer = optim.Adam(self.parameters(), lr=beta) self def forward(self, state, action): x = T.cat([state, action], dim=-1) x = T.relu(self.ln1(self.fc1(x))) x = T.relu(self.ln2(self.fc2(x))) q = self.q(x) return q def save_checkpoint(self, checkpoint_file): T.save(self.state_dict(), checkpoint_file, _use_new_zipfile_serialization=False) def load_checkpoint(self, checkpoint_file): self.load_state_dict(T.load(checkpoint_file)) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'beta': 4, 'state_dim': 4, 'action_dim': 4, 'fc1_dim': 4, 'fc2_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch as T import torch.nn as nn import torch.optim as optim assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_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) @triton.jit def triton_poi_fused_native_layer_norm_1(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel 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_relu_threshold_backward_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 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 tmp9 = tl.full([1], 0, tl.int32) tmp10 = triton_helpers.maximum(tmp9, tmp8) tmp11 = 0.0 tmp12 = tmp10 <= tmp11 tl.store(out_ptr0 + x2, tmp10, xmask) tl.store(out_ptr1 + x2, tmp12, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12 ) = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4, 8), (8, 1)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (4, 4), (4, 1)) assert_size_stride(primals_8, (4,), (1,)) assert_size_stride(primals_9, (4,), (1,)) assert_size_stride(primals_10, (4,), (1,)) assert_size_stride(primals_11, (1, 4), (4, 1)) assert_size_stride(primals_12, (1,), (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)](primals_1, primals_2, buf0, 512, XBLOCK=256, num_warps=4, num_stages=1) del primals_1 del primals_2 buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_4, reinterpret_tensor(buf0, (64, 8), ( 8, 1), 0), reinterpret_tensor(primals_3, (8, 4), (1, 8), 0), alpha=1, beta=1, out=buf1) del primals_3 del primals_4 buf2 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) triton_poi_fused_native_layer_norm_1[grid(64)](buf1, buf2, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf12 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_native_layer_norm_relu_threshold_backward_2[grid(256) ](buf1, buf2, buf3, primals_5, primals_6, buf4, buf12, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_6 buf5 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_8, reinterpret_tensor(buf4, (64, 4), ( 4, 1), 0), reinterpret_tensor(primals_7, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf5) del primals_8 buf6 = buf3 del buf3 buf7 = buf2 del buf2 triton_poi_fused_native_layer_norm_1[grid(64)](buf5, buf6, buf7, 64, XBLOCK=64, num_warps=1, num_stages=1) buf8 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf11 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_native_layer_norm_relu_threshold_backward_2[grid(256) ](buf5, buf6, buf7, primals_9, primals_10, buf8, buf11, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf6 del primals_10 buf10 = reinterpret_tensor(buf7, (64, 1), (1, 1), 0) del buf7 extern_kernels.addmm(primals_12, reinterpret_tensor(buf8, (64, 4), (4, 1), 0), reinterpret_tensor(primals_11, (4, 1), (1, 4), 0), alpha=1, beta=1, out=buf10) del primals_12 return reinterpret_tensor(buf10, (4, 4, 4, 1), (16, 4, 1, 1), 0 ), primals_5, primals_9, reinterpret_tensor(buf0, (64, 8), (8, 1), 0 ), buf1, reinterpret_tensor(buf4, (64, 4), (4, 1), 0 ), buf5, reinterpret_tensor(buf8, (64, 4), (4, 1), 0 ), primals_11, buf11, primals_7, buf12 class CriticNetworkNew(nn.Module): def __init__(self, beta, state_dim, action_dim, fc1_dim, fc2_dim): super(CriticNetworkNew, self).__init__() self.fc1 = nn.Linear(state_dim + action_dim, fc1_dim) self.ln1 = nn.LayerNorm(fc1_dim) self.fc2 = nn.Linear(fc1_dim, fc2_dim) self.ln2 = nn.LayerNorm(fc2_dim) self.q = nn.Linear(fc2_dim, 1) self.optimizer = optim.Adam(self.parameters(), lr=beta) self def save_checkpoint(self, checkpoint_file): T.save(self.state_dict(), checkpoint_file, _use_new_zipfile_serialization=False) def load_checkpoint(self, checkpoint_file): self.load_state_dict(T.load(checkpoint_file)) def forward(self, input_0, input_1): primals_3 = self.fc1.weight primals_4 = self.fc1.bias primals_5 = self.ln1.weight primals_6 = self.ln1.bias primals_7 = self.fc2.weight primals_8 = self.fc2.bias primals_9 = self.ln2.weight primals_10 = self.ln2.bias primals_11 = self.q.weight primals_12 = self.q.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]) return output[0]
indigoLovee/TD3
CriticNetwork
false
6,890
[ "MIT" ]
1
0e86a40c27ec376b52e9f8e0e70db28e7411276b
https://github.com/indigoLovee/TD3/tree/0e86a40c27ec376b52e9f8e0e70db28e7411276b
ChannelSqueeze
import torch import torch.nn as nn def channel_squeeze(x, groups): """ Channel squeeze operation. Parameters: ---------- x : Tensor Input tensor. groups : int Number of groups. Returns ------- Tensor Resulted tensor. """ batch, channels, height, width = x.size() channels_per_group = channels // groups x = x.view(batch, channels_per_group, groups, height, width).sum(dim=2) return x class ChannelSqueeze(nn.Module): """ Channel squeeze layer. This is a wrapper over the same operation. It is designed to save the number of groups. Parameters: ---------- channels : int Number of channels. groups : int Number of groups. """ def __init__(self, channels, groups): super(ChannelSqueeze, self).__init__() if channels % groups != 0: raise ValueError('channels must be divisible by groups') self.groups = groups def forward(self, x): return channel_squeeze(x, self.groups) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'channels': 4, 'groups': 1}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream 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_sum_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tl.store(out_ptr0 + x0, tmp0, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_sum_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return buf0, def channel_squeeze(x, groups): """ Channel squeeze operation. Parameters: ---------- x : Tensor Input tensor. groups : int Number of groups. Returns ------- Tensor Resulted tensor. """ batch, channels, height, width = x.size() channels_per_group = channels // groups x = x.view(batch, channels_per_group, groups, height, width).sum(dim=2) return x class ChannelSqueezeNew(nn.Module): """ Channel squeeze layer. This is a wrapper over the same operation. It is designed to save the number of groups. Parameters: ---------- channels : int Number of channels. groups : int Number of groups. """ def __init__(self, channels, groups): super(ChannelSqueezeNew, self).__init__() if channels % groups != 0: raise ValueError('channels must be divisible by groups') self.groups = groups def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
iofthetiger/pkuad
ChannelSqueeze
false
6,891
[ "Apache-2.0" ]
1
07496d108c614c84be028f344830becc9cac8fe5
https://github.com/iofthetiger/pkuad/tree/07496d108c614c84be028f344830becc9cac8fe5
MP
from torch.nn import Module import torch import torch.utils.data from torch.nn import MaxPool2d class MP(Module): def __init__(self, k=2): super().__init__() self.m = MaxPool2d(kernel_size=k, stride=k) def forward(self, x): return self.m(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.nn import Module import torch.utils.data from torch.nn import MaxPool2d assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_max_pool2d_with_indices_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 2 x1 = xindex // 2 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (4 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (5 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tl.store(out_ptr0 + x2, 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, 2, 2), (16, 4, 2, 1), torch.float32) get_raw_stream(0) triton_poi_fused_max_pool2d_with_indices_0[grid(64)](arg0_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 return buf0, class MPNew(Module): def __init__(self, k=2): super().__init__() self.m = MaxPool2d(kernel_size=k, stride=k) def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
ioangatop/yolo
MP
false
6,892
[ "MIT" ]
1
c65a72337369572bc07090f39123e2bf6ff5f4a3
https://github.com/ioangatop/yolo/tree/c65a72337369572bc07090f39123e2bf6ff5f4a3
BasicBlockWN
import torch import torch as t import torch.nn as nn from abc import ABC from torch.nn.utils.weight_norm import weight_norm def conv1x1(in_planes, out_planes, stride=1): """ Create a 1x1 2d convolution block """ return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False) def conv3x3(in_planes, out_planes, stride=1): """ Create a 3x3 2d convolution block """ return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False) class NeuralNetworkModule(nn.Module, ABC): """ Note: input device and output device are determined by module parameters, your input module / output submodule should not store parameters on more than one device, and you also should not move your output to other devices other than your parameter storage device in forward(). """ def __init__(self): super().__init__() self.input_module = None self.output_module = None def set_input_module(self, input_module: 'nn.Module'): """ Set the input submodule of current module. """ self.input_module = input_module if not isinstance(input_module, NeuralNetworkModule): if isinstance(input_module, nn.Sequential): input_module = self.find_child(input_module, True) if len({p.device for p in input_module.parameters()}) > 1: raise RuntimeError( 'Input module must be another NeuralNetworkModule or locate on one single device.' ) def set_output_module(self, output_module: 'nn.Module'): """ Set the output submodule of current module. """ self.output_module = output_module if not isinstance(output_module, NeuralNetworkModule): if isinstance(output_module, nn.Sequential): output_module = self.find_child(output_module, False) if len({p.device for p in output_module.parameters()}) > 1: raise RuntimeError( 'Output module must be another NeuralNetworkModule or locate on one single device.' ) @property def input_device(self): if self.input_module is None: raise RuntimeError('Input module not set.') elif not isinstance(self.input_module, NeuralNetworkModule): dev_set = {p.device for p in self.input_module.parameters()} if len(dev_set) != 1: raise RuntimeError( 'This input module contains parameters on different devices, please consider about splitting it.' ) else: return list(dev_set)[0] else: return self.input_module.input_device @property def output_device(self): if self.output_module is None and self.input_module is None: raise RuntimeError('Output module not set.') elif self.output_module is not None: if not isinstance(self.output_module, NeuralNetworkModule): dev_set = {p.device for p in self.output_module.parameters()} if len(dev_set) != 1: raise RuntimeError( 'This output module contains parameters on different devices, please consider about splitting it.' ) else: return list(dev_set)[0] else: return self.output_module.output_device else: return self.input_device @staticmethod def find_child(seq, is_first=True): """ Find the first / last leaf child module. """ if isinstance(seq, nn.Sequential): if is_first: return NeuralNetworkModule.find_child(seq[0], is_first) else: return NeuralNetworkModule.find_child(seq[-1], is_first) else: return seq def forward(self, *_): pass class BasicBlockWN(NeuralNetworkModule): """ Basic block with weight normalization """ expansion = 1 def __init__(self, in_planes, out_planes, stride=1, **__): """ Create a basic block of resnet. Args: in_planes: Number of input planes. out_planes: Number of output planes. stride: Stride of convolution. """ super().__init__() self.conv1 = weight_norm(conv3x3(in_planes, out_planes, stride)) self.conv2 = weight_norm(conv3x3(out_planes, self.expansion * out_planes)) self.shortcut = nn.Sequential() self.set_input_module(self.conv1) if stride != 1 or in_planes != self.expansion * out_planes: self.shortcut = nn.Sequential(weight_norm(conv1x1(in_planes, self.expansion * out_planes, stride))) def forward(self, x): out = t.relu(self.conv1(x)) out = self.conv2(out) out += self.shortcut(x) out = t.relu(out) return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_planes': 4, 'out_planes': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn from abc import ABC 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_per_fused__weight_norm_interface_0(in_out_ptr0, in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 rnumel = 36 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, :] rmask = rindex < rnumel r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 36 * x0), rmask & xmask, other=0.0) tmp7 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp1 = tmp0 * tmp0 tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp4 = tl.where(rmask & xmask, tmp2, 0) tmp5 = tl.sum(tmp4, 1)[:, None] tmp6 = libdevice.sqrt(tmp5) tmp8 = tmp7 / tmp6 tmp9 = tmp0 * tmp8 tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp6, xmask) tl.store(out_ptr0 + (r1 + 36 * x0), tmp9, rmask & xmask) @triton.jit def triton_poi_fused_relu_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.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_add_relu_threshold_backward_2(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask) 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 + x0, tmp4, xmask) tl.store(out_ptr0 + x0, tmp6, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 1, 1, 1), (1, 1, 1, 1)) assert_size_stride(primals_2, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 1, 1, 1), (1, 1, 1, 1)) assert_size_stride(primals_5, (4, 4, 3, 3), (36, 9, 3, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 1, 1, 1), (1, 4, 4, 4), torch.float32) buf1 = reinterpret_tensor(buf0, (4, 1, 1, 1), (1, 1, 1, 1), 0) del buf0 buf2 = empty_strided_cuda((4, 4, 3, 3), (36, 9, 3, 1), torch.float32) get_raw_stream(0) triton_per_fused__weight_norm_interface_0[grid(4)](buf1, primals_2, primals_1, buf2, 4, 36, XBLOCK=1, num_warps=2, num_stages=1) buf3 = extern_kernels.convolution(primals_3, buf2, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf3, (4, 4, 4, 4), (64, 16, 4, 1)) buf4 = buf3 del buf3 triton_poi_fused_relu_1[grid(256)](buf4, 256, XBLOCK=128, num_warps =4, num_stages=1) buf5 = empty_strided_cuda((4, 1, 1, 1), (1, 4, 4, 4), torch.float32) buf6 = reinterpret_tensor(buf5, (4, 1, 1, 1), (1, 1, 1, 1), 0) del buf5 buf7 = empty_strided_cuda((4, 4, 3, 3), (36, 9, 3, 1), torch.float32) triton_per_fused__weight_norm_interface_0[grid(4)](buf6, primals_5, primals_4, buf7, 4, 36, XBLOCK=1, num_warps=2, num_stages=1) buf8 = extern_kernels.convolution(buf4, buf7, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 4, 4, 4), (64, 16, 4, 1)) buf9 = buf8 del buf8 buf10 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_add_relu_threshold_backward_2[grid(256)](buf9, primals_3, buf10, 256, XBLOCK=128, num_warps=4, num_stages=1) return (buf9, buf2, buf7, primals_1, primals_2, primals_3, primals_4, primals_5, buf1, buf2, buf4, buf6, buf7, buf10) def conv1x1(in_planes, out_planes, stride=1): """ Create a 1x1 2d convolution block """ return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False) def conv3x3(in_planes, out_planes, stride=1): """ Create a 3x3 2d convolution block """ return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False) class NeuralNetworkModule(nn.Module, ABC): """ Note: input device and output device are determined by module parameters, your input module / output submodule should not store parameters on more than one device, and you also should not move your output to other devices other than your parameter storage device in forward(). """ def __init__(self): super().__init__() self.input_module = None self.output_module = None def set_input_module(self, input_module: 'nn.Module'): """ Set the input submodule of current module. """ self.input_module = input_module if not isinstance(input_module, NeuralNetworkModule): if isinstance(input_module, nn.Sequential): input_module = self.find_child(input_module, True) if len({p.device for p in input_module.parameters()}) > 1: raise RuntimeError( 'Input module must be another NeuralNetworkModule or locate on one single device.' ) def set_output_module(self, output_module: 'nn.Module'): """ Set the output submodule of current module. """ self.output_module = output_module if not isinstance(output_module, NeuralNetworkModule): if isinstance(output_module, nn.Sequential): output_module = self.find_child(output_module, False) if len({p.device for p in output_module.parameters()}) > 1: raise RuntimeError( 'Output module must be another NeuralNetworkModule or locate on one single device.' ) @property def input_device(self): if self.input_module is None: raise RuntimeError('Input module not set.') elif not isinstance(self.input_module, NeuralNetworkModule): dev_set = {p.device for p in self.input_module.parameters()} if len(dev_set) != 1: raise RuntimeError( 'This input module contains parameters on different devices, please consider about splitting it.' ) else: return list(dev_set)[0] else: return self.input_module.input_device @property def output_device(self): if self.output_module is None and self.input_module is None: raise RuntimeError('Output module not set.') elif self.output_module is not None: if not isinstance(self.output_module, NeuralNetworkModule): dev_set = {p.device for p in self.output_module.parameters()} if len(dev_set) != 1: raise RuntimeError( 'This output module contains parameters on different devices, please consider about splitting it.' ) else: return list(dev_set)[0] else: return self.output_module.output_device else: return self.input_device @staticmethod def find_child(seq, is_first=True): """ Find the first / last leaf child module. """ if isinstance(seq, nn.Sequential): if is_first: return NeuralNetworkModule.find_child(seq[0], is_first) else: return NeuralNetworkModule.find_child(seq[-1], is_first) else: return seq def forward(self, *_): pass class BasicBlockWNNew(NeuralNetworkModule): """ Basic block with weight normalization """ expansion = 1 def __init__(self, in_planes, out_planes, stride=1, **__): """ Create a basic block of resnet. Args: in_planes: Number of input planes. out_planes: Number of output planes. stride: Stride of convolution. """ super().__init__() self.conv1 = weight_norm(conv3x3(in_planes, out_planes, stride)) self.conv2 = weight_norm(conv3x3(out_planes, self.expansion * out_planes)) self.shortcut = nn.Sequential() self.set_input_module(self.conv1) if stride != 1 or in_planes != self.expansion * out_planes: self.shortcut = nn.Sequential(weight_norm(conv1x1(in_planes, self.expansion * out_planes, stride))) def forward(self, input_0): primals_1 = self.conv1.weight_g primals_2 = self.conv1.weight_v primals_4 = self.conv2.weight_g primals_5 = self.conv2.weight_v primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
ikamensh/machin
BasicBlockWN
false
6,893
[ "MIT" ]
1
af7b423c47bc1412530cf6c96c11bd3af9b3e239
https://github.com/ikamensh/machin/tree/af7b423c47bc1412530cf6c96c11bd3af9b3e239
Gated_Conv_1d
import torch import torch.nn as nn class Gated_Conv_1d(nn.Module): def __init__(self, channels, kernel_size, stride=1, padding=0, dilation =1, groups=1, bias=True): super(Gated_Conv_1d, self).__init__() self.dilation = dilation self.channels = channels self.conv_dil = nn.Conv1d(in_channels=channels, out_channels=2 * channels, kernel_size=kernel_size, stride=stride, padding= padding, dilation=dilation, groups=groups, bias=bias) self.tan = nn.Tanh() self.sig = nn.Sigmoid() def forward(self, x): x = self.conv_dil(x) tn, sg = torch.split(x, self.channels, 1) return self.tan(tn) * self.sig(sg) def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'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_poi_fused_mul_sigmoid_tanh_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 8 * x1), xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (4 + x0 + 8 * x1), xmask) tmp5 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = libdevice.tanh(tmp2) tmp6 = tmp4 + tmp5 tmp7 = tl.sigmoid(tmp6) tmp8 = tmp3 * tmp7 tl.store(out_ptr0 + x2, tmp3, xmask) tl.store(out_ptr1 + x2, tmp7, xmask) tl.store(out_ptr2 + x2, tmp8, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (8, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (8,), (1,)) assert_size_stride(primals_3, (4, 4, 4), (16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf0, (4, 8, 1), (8, 1, 1)) buf1 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) buf2 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) buf3 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_mul_sigmoid_tanh_0[grid(16)](buf0, primals_2, buf1, buf2, buf3, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf0 del primals_2 return buf3, primals_1, primals_3, buf1, buf2 class Gated_Conv_1dNew(nn.Module): def __init__(self, channels, kernel_size, stride=1, padding=0, dilation =1, groups=1, bias=True): super(Gated_Conv_1dNew, self).__init__() self.dilation = dilation self.channels = channels self.conv_dil = nn.Conv1d(in_channels=channels, out_channels=2 * channels, kernel_size=kernel_size, stride=stride, padding= padding, dilation=dilation, groups=groups, bias=bias) self.tan = nn.Tanh() self.sig = nn.Sigmoid() def forward(self, input_0): primals_1 = self.conv_dil.weight primals_2 = self.conv_dil.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
ioanvl/wavenet_classifier_torch
Gated_Conv_1d
false
6,894
[ "MIT" ]
1
de29bfce59d52ae46143f62c4d7a6158a04edf00
https://github.com/ioanvl/wavenet_classifier_torch/tree/de29bfce59d52ae46143f62c4d7a6158a04edf00
IRevInjectivePad
import torch import torch.nn as nn class IRevInjectivePad(nn.Module): """ i-RevNet channel zero padding block. Parameters: ---------- padding : int Size of the padding. """ def __init__(self, padding): super(IRevInjectivePad, self).__init__() self.padding = padding self.pad = nn.ZeroPad2d(padding=(0, 0, 0, padding)) def forward(self, x): x = x.permute(0, 2, 1, 3) x = self.pad(x) return x.permute(0, 2, 1, 3) def inverse(self, x): return x[:, :x.size(1) - self.padding, :, :] def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'padding': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_constant_pad_nd_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 x1 = xindex // 4 % 8 x0 = xindex % 4 x2 = xindex // 32 % 4 x3 = xindex // 128 x4 = xindex tmp0 = x1 tmp1 = tl.full([1], 4, tl.int64) tmp2 = tmp0 < tmp1 tmp3 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1 + 64 * x3), tmp2 & xmask, other=0.0) tl.store(out_ptr0 + x4, tmp3, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 8, 4), (128, 32, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_constant_pad_nd_0[grid(512)](arg0_1, buf0, 512, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return reinterpret_tensor(buf0, (4, 8, 4, 4), (128, 4, 32, 1), 0), class IRevInjectivePadNew(nn.Module): """ i-RevNet channel zero padding block. Parameters: ---------- padding : int Size of the padding. """ def __init__(self, padding): super(IRevInjectivePadNew, self).__init__() self.padding = padding self.pad = nn.ZeroPad2d(padding=(0, 0, 0, padding)) def inverse(self, x): return x[:, :x.size(1) - self.padding, :, :] def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
iofthetiger/pkuad
IRevInjectivePad
false
6,895
[ "Apache-2.0" ]
1
07496d108c614c84be028f344830becc9cac8fe5
https://github.com/iofthetiger/pkuad/tree/07496d108c614c84be028f344830becc9cac8fe5
BERTIntermediate
from _paritybench_helpers import _mock_config import math import torch import torch.nn as nn def gelu(x): """Implementation of the gelu activation function. For information: OpenAI GPT's gelu is slightly different (and gives slightly different results): 0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3)))) """ return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0))) class BERTIntermediate(nn.Module): def __init__(self, config): super(BERTIntermediate, self).__init__() self.dense = nn.Linear(config.hidden_size, config.intermediate_size) self.config = config self.intermediate_act_fn = gelu def forward(self, hidden_states): hidden_states = self.dense(hidden_states) hidden_states = self.intermediate_act_fn(hidden_states) return hidden_states def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'config': _mock_config(hidden_size=4, 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.triton_helpers import libdevice import math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_div_erf_mul_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.5 tmp2 = tmp0 * tmp1 tmp3 = 0.7071067811865475 tmp4 = tmp0 * tmp3 tmp5 = libdevice.erf(tmp4) tmp6 = 1.0 tmp7 = tmp5 + tmp6 tmp8 = tmp2 * tmp7 tl.store(out_ptr0 + x0, tmp8, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_erf_mul_0[grid(256)](buf0, buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) return buf1, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), buf0 def gelu(x): """Implementation of the gelu activation function. For information: OpenAI GPT's gelu is slightly different (and gives slightly different results): 0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3)))) """ return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0))) class BERTIntermediateNew(nn.Module): def __init__(self, config): super(BERTIntermediateNew, self).__init__() self.dense = nn.Linear(config.hidden_size, config.intermediate_size) self.config = config self.intermediate_act_fn = gelu def forward(self, input_0): primals_1 = self.dense.weight primals_2 = self.dense.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
DAQuestionAnswering/Bert-n-Pals
BERTIntermediate
false
6,896
[ "MIT" ]
1
d5a288b9ac62259e70c249635108ba3906e19f00
https://github.com/DAQuestionAnswering/Bert-n-Pals/tree/d5a288b9ac62259e70c249635108ba3906e19f00
Decoder5
import torch import torch.nn as nn class Decoder5(nn.Module): def __init__(self): super(Decoder5, self).__init__() self.reflecPad15 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv15 = nn.Conv2d(512, 512, 3, 1, 0) self.relu15 = nn.ReLU(inplace=True) self.unpool = nn.UpsamplingNearest2d(scale_factor=2) self.reflecPad16 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv16 = nn.Conv2d(512, 512, 3, 1, 0) self.relu16 = nn.ReLU(inplace=True) self.reflecPad17 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv17 = nn.Conv2d(512, 512, 3, 1, 0) self.relu17 = nn.ReLU(inplace=True) self.reflecPad18 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv18 = nn.Conv2d(512, 512, 3, 1, 0) self.relu18 = nn.ReLU(inplace=True) self.reflecPad19 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv19 = nn.Conv2d(512, 256, 3, 1, 0) self.relu19 = nn.ReLU(inplace=True) self.unpool2 = nn.UpsamplingNearest2d(scale_factor=2) self.reflecPad20 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv20 = nn.Conv2d(256, 256, 3, 1, 0) self.relu20 = nn.ReLU(inplace=True) self.reflecPad21 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv21 = nn.Conv2d(256, 256, 3, 1, 0) self.relu21 = nn.ReLU(inplace=True) self.reflecPad22 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv22 = nn.Conv2d(256, 256, 3, 1, 0) self.relu22 = nn.ReLU(inplace=True) self.reflecPad23 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv23 = nn.Conv2d(256, 128, 3, 1, 0) self.relu23 = nn.ReLU(inplace=True) self.unpool3 = nn.UpsamplingNearest2d(scale_factor=2) self.reflecPad24 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv24 = nn.Conv2d(128, 128, 3, 1, 0) self.relu24 = nn.ReLU(inplace=True) self.reflecPad25 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv25 = nn.Conv2d(128, 64, 3, 1, 0) self.relu25 = nn.ReLU(inplace=True) self.unpool4 = nn.UpsamplingNearest2d(scale_factor=2) self.reflecPad26 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv26 = nn.Conv2d(64, 64, 3, 1, 0) self.relu26 = nn.ReLU(inplace=True) self.reflecPad27 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv27 = nn.Conv2d(64, 3, 3, 1, 0) def forward(self, x): out = self.reflecPad15(x) out = self.conv15(out) out = self.relu15(out) out = self.unpool(out) out = self.reflecPad16(out) out = self.conv16(out) out = self.relu16(out) out = self.reflecPad17(out) out = self.conv17(out) out = self.relu17(out) out = self.reflecPad18(out) out = self.conv18(out) out = self.relu18(out) out = self.reflecPad19(out) out = self.conv19(out) out = self.relu19(out) out = self.unpool2(out) out = self.reflecPad20(out) out = self.conv20(out) out = self.relu20(out) out = self.reflecPad21(out) out = self.conv21(out) out = self.relu21(out) out = self.reflecPad22(out) out = self.conv22(out) out = self.relu22(out) out = self.reflecPad23(out) out = self.conv23(out) out = self.relu23(out) out = self.unpool3(out) out = self.reflecPad24(out) out = self.conv24(out) out = self.relu24(out) out = self.reflecPad25(out) out = self.conv25(out) out = self.relu25(out) out = self.unpool4(out) out = self.reflecPad26(out) out = self.conv26(out) out = self.relu26(out) out = self.reflecPad27(out) out = self.conv27(out) return out def get_inputs(): return [torch.rand([4, 512, 4, 4])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_reflection_pad2d_0(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 % 6 x1 = xindex // 6 % 6 x2 = xindex // 36 x3 = xindex tmp0 = tl.load(in_ptr0 + (15 + -1 * tl_math.abs(-3 + tl_math.abs(-1 + x0)) + -4 * tl_math.abs(-3 + tl_math.abs(-1 + x1)) + 16 * x2), None, eviction_policy='evict_last') tl.store(out_ptr0 + x3, tmp0, None) @triton.jit def triton_poi_fused__to_copy_add_arange_mul_1(out_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 8 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp3.to(tl.int32) tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_poi_fused__unsafe_index_convolution_reflection_pad2d_relu_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 // 10 % 10 x0 = xindex % 10 x4 = xindex // 100 x2 = xindex // 100 % 512 x7 = xindex tmp0 = tl.load(in_ptr0 + (7 + -1 * tl_math.abs(-7 + tl_math.abs(-1 + x1 ))), None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (7 + -1 * tl_math.abs(-7 + tl_math.abs(-1 + x0 ))), None, eviction_policy='evict_last') tmp10 = tl.load(in_ptr2 + x2, None, eviction_policy='evict_last') tmp1 = tl.full([XBLOCK], 4, tl.int32) tmp2 = tmp0 + tmp1 tmp3 = tmp0 < 0 tmp4 = tl.where(tmp3, tmp2, tmp0) tmp6 = tmp5 + tmp1 tmp7 = tmp5 < 0 tmp8 = tl.where(tmp7, tmp6, tmp5) tmp9 = tl.load(in_ptr1 + (tmp8 + 4 * tmp4 + 16 * x4), None, eviction_policy='evict_last') tmp11 = tmp9 + tmp10 tmp12 = tl.full([1], 0, tl.int32) tmp13 = triton_helpers.maximum(tmp12, tmp11) tl.store(out_ptr0 + x7, tmp13, None) @triton.jit def triton_poi_fused_convolution_reflection_pad2d_relu_3(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 % 10 x1 = xindex // 10 % 10 x4 = xindex // 100 x2 = xindex // 100 % 512 x5 = xindex tmp0 = tl.load(in_ptr0 + (63 + -1 * tl_math.abs(-7 + tl_math.abs(-1 + x0)) + -8 * tl_math.abs(-7 + tl_math.abs(-1 + x1)) + 64 * x4), None, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, 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 + x5, tmp4, None) @triton.jit def triton_poi_fused__to_copy_add_arange_mul_4(out_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp3.to(tl.int32) tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_poi_fused__unsafe_index_convolution_reflection_pad2d_relu_5(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x1 = xindex // 18 % 18 x0 = xindex % 18 x4 = xindex // 324 x2 = xindex // 324 % 256 x7 = xindex tmp0 = tl.load(in_ptr0 + (15 + -1 * tl_math.abs(-15 + tl_math.abs(-1 + x1))), None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (15 + -1 * tl_math.abs(-15 + tl_math.abs(-1 + x0))), None, eviction_policy='evict_last') tmp10 = tl.load(in_ptr2 + x2, None, eviction_policy='evict_last') tmp1 = tl.full([XBLOCK], 8, tl.int32) tmp2 = tmp0 + tmp1 tmp3 = tmp0 < 0 tmp4 = tl.where(tmp3, tmp2, tmp0) tmp6 = tmp5 + tmp1 tmp7 = tmp5 < 0 tmp8 = tl.where(tmp7, tmp6, tmp5) tmp9 = tl.load(in_ptr1 + (tmp8 + 8 * tmp4 + 64 * x4), None, eviction_policy='evict_last') tmp11 = tmp9 + tmp10 tmp12 = tl.full([1], 0, tl.int32) tmp13 = triton_helpers.maximum(tmp12, tmp11) tl.store(out_ptr0 + x7, tmp13, None) @triton.jit def triton_poi_fused_convolution_reflection_pad2d_relu_6(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x0 = xindex % 18 x1 = xindex // 18 % 18 x4 = xindex // 324 x2 = xindex // 324 % 256 x5 = xindex tmp0 = tl.load(in_ptr0 + (255 + -1 * tl_math.abs(-15 + tl_math.abs(-1 + x0)) + -16 * tl_math.abs(-15 + tl_math.abs(-1 + x1)) + 256 * x4), None, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, 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 + x5, tmp4, None) @triton.jit def triton_poi_fused__to_copy_add_arange_mul_7(out_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 32 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp3.to(tl.int32) tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_poi_fused__unsafe_index_convolution_reflection_pad2d_relu_8(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 // 34 % 34 x0 = xindex % 34 x4 = xindex // 1156 x2 = xindex // 1156 % 128 x7 = xindex tmp0 = tl.load(in_ptr0 + (31 + -1 * tl_math.abs(-31 + tl_math.abs(-1 + x1))), None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (31 + -1 * tl_math.abs(-31 + tl_math.abs(-1 + x0))), None, eviction_policy='evict_last') tmp10 = tl.load(in_ptr2 + x2, None, eviction_policy='evict_last') tmp1 = tl.full([XBLOCK], 16, tl.int32) tmp2 = tmp0 + tmp1 tmp3 = tmp0 < 0 tmp4 = tl.where(tmp3, tmp2, tmp0) tmp6 = tmp5 + tmp1 tmp7 = tmp5 < 0 tmp8 = tl.where(tmp7, tmp6, tmp5) tmp9 = tl.load(in_ptr1 + (tmp8 + 16 * tmp4 + 256 * x4), None, eviction_policy='evict_last') tmp11 = tmp9 + tmp10 tmp12 = tl.full([1], 0, tl.int32) tmp13 = triton_helpers.maximum(tmp12, tmp11) tl.store(out_ptr0 + x7, tmp13, None) @triton.jit def triton_poi_fused_convolution_reflection_pad2d_relu_9(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 % 34 x1 = xindex // 34 % 34 x4 = xindex // 1156 x2 = xindex // 1156 % 128 x5 = xindex tmp0 = tl.load(in_ptr0 + (1023 + -1 * tl_math.abs(-31 + tl_math.abs(-1 + x0)) + -32 * tl_math.abs(-31 + tl_math.abs(-1 + x1)) + 1024 * x4), None, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, 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 + x5, tmp4, None) @triton.jit def triton_poi_fused_arange_10(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = x0 tl.store(out_ptr0 + x0, tmp0, xmask) @triton.jit def triton_poi_fused__to_copy_add_arange_mul_11(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = x0 tmp1 = tmp0.to(tl.float32) tmp2 = 0.5 tmp3 = tmp1 * tmp2 tmp4 = tmp3.to(tl.int32) tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_poi_fused__unsafe_index_convolution_reflection_pad2d_relu_12(in_ptr0 , in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1115136 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 66 % 66 x0 = xindex % 66 x4 = xindex // 4356 x2 = xindex // 4356 % 64 x7 = xindex tmp0 = tl.load(in_ptr0 + (63 + -1 * tl_math.abs(-63 + tl_math.abs(-1 + x1))), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (63 + -1 * tl_math.abs(-63 + tl_math.abs(-1 + x0))), xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last') tmp1 = tl.full([XBLOCK], 32, tl.int32) tmp2 = tmp0 + tmp1 tmp3 = tmp0 < 0 tmp4 = tl.where(tmp3, tmp2, tmp0) tmp6 = tmp5 + tmp1 tmp7 = tmp5 < 0 tmp8 = tl.where(tmp7, tmp6, tmp5) tmp9 = tl.load(in_ptr1 + (tmp8 + 32 * tmp4 + 1024 * x4), xmask, eviction_policy='evict_last') tmp11 = tmp9 + tmp10 tmp12 = tl.full([1], 0, tl.int32) tmp13 = triton_helpers.maximum(tmp12, tmp11) tl.store(out_ptr0 + x7, tmp13, xmask) @triton.jit def triton_poi_fused_convolution_reflection_pad2d_relu_13(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1115136 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 66 x1 = xindex // 66 % 66 x4 = xindex // 4356 x2 = xindex // 4356 % 64 x5 = xindex tmp0 = tl.load(in_ptr0 + (4095 + -1 * tl_math.abs(-63 + tl_math.abs(-1 + x0)) + -64 * tl_math.abs(-63 + tl_math.abs(-1 + x1)) + 4096 * x4), xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x2, 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 + x5, tmp4, xmask) @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 // 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_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 // 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_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 // 1024 % 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_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 // 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_18(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 256 % 128 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 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_19(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_20(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 64 % 256 tmp0 = tl.load(in_ptr0 + x3, None) tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 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_21(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) @triton.jit def triton_poi_fused_convolution_relu_threshold_backward_22(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x3 = xindex x1 = xindex // 16 % 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) = args args.clear() assert_size_stride(primals_1, (4, 512, 4, 4), (8192, 16, 4, 1)) assert_size_stride(primals_2, (512, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_3, (512,), (1,)) assert_size_stride(primals_4, (512, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_5, (512,), (1,)) assert_size_stride(primals_6, (512, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_7, (512,), (1,)) assert_size_stride(primals_8, (512, 512, 3, 3), (4608, 9, 3, 1)) assert_size_stride(primals_9, (512,), (1,)) assert_size_stride(primals_10, (256, 512, 3, 3), (4608, 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, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_15, (256,), (1,)) assert_size_stride(primals_16, (256, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_17, (256,), (1,)) assert_size_stride(primals_18, (128, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_19, (128,), (1,)) assert_size_stride(primals_20, (128, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_21, (128,), (1,)) assert_size_stride(primals_22, (64, 128, 3, 3), (1152, 9, 3, 1)) assert_size_stride(primals_23, (64,), (1,)) assert_size_stride(primals_24, (64, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_25, (64,), (1,)) assert_size_stride(primals_26, (3, 64, 3, 3), (576, 9, 3, 1)) assert_size_stride(primals_27, (3,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 512, 6, 6), (18432, 36, 6, 1), torch. float32) get_raw_stream(0) triton_poi_fused_reflection_pad2d_0[grid(73728)](primals_1, buf0, 73728, XBLOCK=1024, 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, 512, 4, 4), (8192, 16, 4, 1)) buf2 = empty_strided_cuda((8,), (1,), torch.int64) triton_poi_fused__to_copy_add_arange_mul_1[grid(8)](buf2, 8, XBLOCK =8, num_warps=1, num_stages=1) buf3 = empty_strided_cuda((4, 512, 10, 10), (51200, 100, 10, 1), torch.float32) triton_poi_fused__unsafe_index_convolution_reflection_pad2d_relu_2[grid (204800)](buf2, buf1, primals_3, buf3, 204800, XBLOCK=512, num_warps=8, num_stages=1) buf4 = extern_kernels.convolution(buf3, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 512, 8, 8), (32768, 64, 8, 1)) buf5 = empty_strided_cuda((4, 512, 10, 10), (51200, 100, 10, 1), torch.float32) triton_poi_fused_convolution_reflection_pad2d_relu_3[grid(204800)](buf4 , primals_5, buf5, 204800, XBLOCK=512, num_warps=8, num_stages=1) buf6 = extern_kernels.convolution(buf5, primals_6, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf6, (4, 512, 8, 8), (32768, 64, 8, 1)) buf7 = empty_strided_cuda((4, 512, 10, 10), (51200, 100, 10, 1), torch.float32) triton_poi_fused_convolution_reflection_pad2d_relu_3[grid(204800)](buf6 , primals_7, buf7, 204800, XBLOCK=512, num_warps=8, num_stages=1) buf8 = extern_kernels.convolution(buf7, primals_8, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 512, 8, 8), (32768, 64, 8, 1)) buf9 = empty_strided_cuda((4, 512, 10, 10), (51200, 100, 10, 1), torch.float32) triton_poi_fused_convolution_reflection_pad2d_relu_3[grid(204800)](buf8 , primals_9, buf9, 204800, XBLOCK=512, num_warps=8, num_stages=1) buf10 = extern_kernels.convolution(buf9, primals_10, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf10, (4, 256, 8, 8), (16384, 64, 8, 1)) buf11 = empty_strided_cuda((16,), (1,), torch.int64) triton_poi_fused__to_copy_add_arange_mul_4[grid(16)](buf11, 16, XBLOCK=16, num_warps=1, num_stages=1) buf12 = empty_strided_cuda((4, 256, 18, 18), (82944, 324, 18, 1), torch.float32) triton_poi_fused__unsafe_index_convolution_reflection_pad2d_relu_5[grid (331776)](buf11, buf10, primals_11, buf12, 331776, XBLOCK=512, num_warps=8, num_stages=1) buf13 = extern_kernels.convolution(buf12, primals_12, stride=(1, 1), padding=(0, 0), 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 = empty_strided_cuda((4, 256, 18, 18), (82944, 324, 18, 1), torch.float32) triton_poi_fused_convolution_reflection_pad2d_relu_6[grid(331776)]( buf13, primals_13, buf14, 331776, XBLOCK=512, num_warps=8, num_stages=1) buf15 = extern_kernels.convolution(buf14, primals_14, stride=(1, 1), padding=(0, 0), 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 = empty_strided_cuda((4, 256, 18, 18), (82944, 324, 18, 1), torch.float32) triton_poi_fused_convolution_reflection_pad2d_relu_6[grid(331776)]( buf15, primals_15, buf16, 331776, XBLOCK=512, num_warps=8, num_stages=1) buf17 = extern_kernels.convolution(buf16, primals_16, 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, 16, 16), (65536, 256, 16, 1)) buf18 = empty_strided_cuda((4, 256, 18, 18), (82944, 324, 18, 1), torch.float32) triton_poi_fused_convolution_reflection_pad2d_relu_6[grid(331776)]( buf17, primals_17, buf18, 331776, XBLOCK=512, num_warps=8, num_stages=1) buf19 = extern_kernels.convolution(buf18, primals_18, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf19, (4, 128, 16, 16), (32768, 256, 16, 1)) buf20 = empty_strided_cuda((32,), (1,), torch.int64) triton_poi_fused__to_copy_add_arange_mul_7[grid(32)](buf20, 32, XBLOCK=32, num_warps=1, num_stages=1) buf21 = empty_strided_cuda((4, 128, 34, 34), (147968, 1156, 34, 1), torch.float32) triton_poi_fused__unsafe_index_convolution_reflection_pad2d_relu_8[grid (591872)](buf20, buf19, primals_19, buf21, 591872, XBLOCK=1024, num_warps=4, num_stages=1) buf22 = extern_kernels.convolution(buf21, primals_20, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf22, (4, 128, 32, 32), (131072, 1024, 32, 1)) buf23 = empty_strided_cuda((4, 128, 34, 34), (147968, 1156, 34, 1), torch.float32) triton_poi_fused_convolution_reflection_pad2d_relu_9[grid(591872)]( buf22, primals_21, buf23, 591872, XBLOCK=512, num_warps=8, num_stages=1) buf24 = extern_kernels.convolution(buf23, primals_22, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf24, (4, 64, 32, 32), (65536, 1024, 32, 1)) buf25 = empty_strided_cuda((64,), (1,), torch.int64) triton_poi_fused_arange_10[grid(64)](buf25, 64, XBLOCK=64, num_warps=1, num_stages=1) buf26 = empty_strided_cuda((64,), (1,), torch.int64) triton_poi_fused__to_copy_add_arange_mul_11[grid(64)](buf26, 64, XBLOCK=64, num_warps=1, num_stages=1) buf27 = empty_strided_cuda((4, 64, 66, 66), (278784, 4356, 66, 1), torch.float32) triton_poi_fused__unsafe_index_convolution_reflection_pad2d_relu_12[ grid(1115136)](buf26, buf24, primals_23, buf27, 1115136, XBLOCK =512, num_warps=8, num_stages=1) buf28 = extern_kernels.convolution(buf27, primals_24, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf28, (4, 64, 64, 64), (262144, 4096, 64, 1)) buf29 = empty_strided_cuda((4, 64, 66, 66), (278784, 4356, 66, 1), torch.float32) triton_poi_fused_convolution_reflection_pad2d_relu_13[grid(1115136)]( buf28, primals_25, buf29, 1115136, XBLOCK=512, num_warps=8, num_stages=1) buf30 = extern_kernels.convolution(buf29, primals_26, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf30, (4, 3, 64, 64), (12288, 4096, 64, 1)) buf31 = buf30 del buf30 triton_poi_fused_convolution_14[grid(49152)](buf31, primals_27, 49152, XBLOCK=256, num_warps=4, num_stages=1) del primals_27 buf32 = empty_strided_cuda((4, 64, 64, 64), (262144, 4096, 64, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_15[grid(1048576)]( buf28, primals_25, buf32, 1048576, XBLOCK=1024, num_warps=4, num_stages=1) del buf28 del primals_25 buf33 = empty_strided_cuda((4, 64, 32, 32), (65536, 1024, 32, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_16[grid(262144)]( buf24, primals_23, buf33, 262144, XBLOCK=512, num_warps=8, num_stages=1) del buf24 del primals_23 buf34 = empty_strided_cuda((4, 128, 32, 32), (131072, 1024, 32, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_17[grid(524288)]( buf22, primals_21, buf34, 524288, XBLOCK=1024, num_warps=4, num_stages=1) del buf22 del primals_21 buf35 = empty_strided_cuda((4, 128, 16, 16), (32768, 256, 16, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_18[grid(131072)]( buf19, primals_19, buf35, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del buf19 del primals_19 buf36 = empty_strided_cuda((4, 256, 16, 16), (65536, 256, 16, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_19[grid(262144)]( buf17, primals_17, buf36, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del buf17 del primals_17 buf37 = empty_strided_cuda((4, 256, 16, 16), (65536, 256, 16, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_19[grid(262144)]( buf15, primals_15, buf37, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del buf15 del primals_15 buf38 = empty_strided_cuda((4, 256, 16, 16), (65536, 256, 16, 1), torch.bool) triton_poi_fused_convolution_relu_threshold_backward_19[grid(262144)]( buf13, primals_13, buf38, 262144, XBLOCK=1024, num_warps=4, num_stages=1) del buf13 del primals_13 buf39 = empty_strided_cuda((4, 256, 8, 8), (16384, 64, 8, 1), torch .bool) triton_poi_fused_convolution_relu_threshold_backward_20[grid(65536)]( buf10, primals_11, buf39, 65536, XBLOCK=256, num_warps=4, num_stages=1) del buf10 del primals_11 buf40 = empty_strided_cuda((4, 512, 8, 8), (32768, 64, 8, 1), torch .bool) triton_poi_fused_convolution_relu_threshold_backward_21[grid(131072)]( buf8, primals_9, buf40, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del buf8 del primals_9 buf41 = empty_strided_cuda((4, 512, 8, 8), (32768, 64, 8, 1), torch .bool) triton_poi_fused_convolution_relu_threshold_backward_21[grid(131072)]( buf6, primals_7, buf41, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del buf6 del primals_7 buf42 = empty_strided_cuda((4, 512, 8, 8), (32768, 64, 8, 1), torch .bool) triton_poi_fused_convolution_relu_threshold_backward_21[grid(131072)]( buf4, primals_5, buf42, 131072, XBLOCK=1024, num_warps=4, num_stages=1) del buf4 del primals_5 buf43 = empty_strided_cuda((4, 512, 4, 4), (8192, 16, 4, 1), torch.bool ) triton_poi_fused_convolution_relu_threshold_backward_22[grid(32768)]( buf1, primals_3, buf43, 32768, XBLOCK=256, num_warps=4, num_stages=1) del buf1 del primals_3 return (buf31, 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, buf0, buf2, buf3, buf5, buf7, buf9, buf11, buf12, buf14, buf16, buf18, buf20, buf21, buf23, buf25, buf26, buf27, buf29, buf32, buf33, buf34, buf35, buf36, buf37, buf38, buf39, buf40, buf41, buf42, buf43) class Decoder5New(nn.Module): def __init__(self): super(Decoder5New, self).__init__() self.reflecPad15 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv15 = nn.Conv2d(512, 512, 3, 1, 0) self.relu15 = nn.ReLU(inplace=True) self.unpool = nn.UpsamplingNearest2d(scale_factor=2) self.reflecPad16 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv16 = nn.Conv2d(512, 512, 3, 1, 0) self.relu16 = nn.ReLU(inplace=True) self.reflecPad17 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv17 = nn.Conv2d(512, 512, 3, 1, 0) self.relu17 = nn.ReLU(inplace=True) self.reflecPad18 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv18 = nn.Conv2d(512, 512, 3, 1, 0) self.relu18 = nn.ReLU(inplace=True) self.reflecPad19 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv19 = nn.Conv2d(512, 256, 3, 1, 0) self.relu19 = nn.ReLU(inplace=True) self.unpool2 = nn.UpsamplingNearest2d(scale_factor=2) self.reflecPad20 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv20 = nn.Conv2d(256, 256, 3, 1, 0) self.relu20 = nn.ReLU(inplace=True) self.reflecPad21 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv21 = nn.Conv2d(256, 256, 3, 1, 0) self.relu21 = nn.ReLU(inplace=True) self.reflecPad22 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv22 = nn.Conv2d(256, 256, 3, 1, 0) self.relu22 = nn.ReLU(inplace=True) self.reflecPad23 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv23 = nn.Conv2d(256, 128, 3, 1, 0) self.relu23 = nn.ReLU(inplace=True) self.unpool3 = nn.UpsamplingNearest2d(scale_factor=2) self.reflecPad24 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv24 = nn.Conv2d(128, 128, 3, 1, 0) self.relu24 = nn.ReLU(inplace=True) self.reflecPad25 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv25 = nn.Conv2d(128, 64, 3, 1, 0) self.relu25 = nn.ReLU(inplace=True) self.unpool4 = nn.UpsamplingNearest2d(scale_factor=2) self.reflecPad26 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv26 = nn.Conv2d(64, 64, 3, 1, 0) self.relu26 = nn.ReLU(inplace=True) self.reflecPad27 = nn.ReflectionPad2d((1, 1, 1, 1)) self.conv27 = nn.Conv2d(64, 3, 3, 1, 0) def forward(self, input_0): primals_2 = self.conv15.weight primals_3 = self.conv15.bias primals_4 = self.conv16.weight primals_5 = self.conv16.bias primals_6 = self.conv17.weight primals_7 = self.conv17.bias primals_8 = self.conv18.weight primals_9 = self.conv18.bias primals_10 = self.conv19.weight primals_11 = self.conv19.bias primals_12 = self.conv20.weight primals_13 = self.conv20.bias primals_14 = self.conv21.weight primals_15 = self.conv21.bias primals_16 = self.conv22.weight primals_17 = self.conv22.bias primals_18 = self.conv23.weight primals_19 = self.conv23.bias primals_20 = self.conv24.weight primals_21 = self.conv24.bias primals_22 = self.conv25.weight primals_23 = self.conv25.bias primals_24 = self.conv26.weight primals_25 = self.conv26.bias primals_26 = self.conv27.weight primals_27 = self.conv27.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11, primals_12, primals_13, primals_14, primals_15, primals_16, primals_17, primals_18, primals_19, primals_20, primals_21, primals_22, primals_23, primals_24, primals_25, primals_26, primals_27]) return output[0]
hologerry/wct_experiment
Decoder5
false
6,897
[ "MIT" ]
1
890d885561dc8df8c4ae732aebd902aa838257e6
https://github.com/hologerry/wct_experiment/tree/890d885561dc8df8c4ae732aebd902aa838257e6
FirstLSTMAmp
import torch import torch.nn as nn class FirstLSTMAmp(nn.Module): """ First LSTM amplifier branch. Parameters: ---------- in_features : int Number of input channels. out_features : int Number of output channels. """ def __init__(self, in_features, out_features): super(FirstLSTMAmp, self).__init__() mid_features = in_features // 4 self.fc1 = nn.Linear(in_features=in_features, out_features=mid_features ) self.activ = nn.ReLU(inplace=True) self.fc2 = nn.Linear(in_features=mid_features, out_features= out_features) def forward(self, x): x = self.fc1(x) x = self.activ(x) x = self.fc2(x) 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 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 = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp3 = tmp0 + tmp2 tmp4 = tl.full([1], 0, tl.int32) tmp5 = triton_helpers.maximum(tmp4, tmp3) tmp6 = 0.0 tmp7 = tmp5 <= tmp6 tl.store(in_out_ptr0 + x0, tmp5, xmask) tl.store(out_ptr0 + x0, tmp7, xmask) @triton.jit def triton_poi_fused_view_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 + 4 * (x0 % 4 // 4) + 16 * ((4 * (x0 // 4 % 4) + x0 % 4) // 16)), xmask) tl.store(out_ptr0 + x0, tmp0, 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, (1,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 1), (1, 1)) assert_size_stride(primals_5, (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_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 1), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf0 buf4 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.bool) get_raw_stream(0) triton_poi_fused_relu_threshold_backward_0[grid(64)](buf1, primals_2, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 1), (1, 1), torch.float32) triton_poi_fused_view_1[grid(64)](buf1, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf1 buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_5, buf2, reinterpret_tensor(primals_4, (1, 4), (1, 1), 0), alpha=1, beta=1, out=buf3) del primals_5 return reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf2, primals_4, buf4 class FirstLSTMAmpNew(nn.Module): """ First LSTM amplifier branch. Parameters: ---------- in_features : int Number of input channels. out_features : int Number of output channels. """ def __init__(self, in_features, out_features): super(FirstLSTMAmpNew, self).__init__() mid_features = in_features // 4 self.fc1 = nn.Linear(in_features=in_features, out_features=mid_features ) self.activ = nn.ReLU(inplace=True) self.fc2 = nn.Linear(in_features=mid_features, out_features= out_features) 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]
iofthetiger/pkuad
FirstLSTMAmp
false
6,898
[ "Apache-2.0" ]
1
07496d108c614c84be028f344830becc9cac8fe5
https://github.com/iofthetiger/pkuad/tree/07496d108c614c84be028f344830becc9cac8fe5
AlexConv
import torch import torch.nn as nn import torch.nn.functional as F from inspect import isfunction def get_activation_layer(activation): """ Create activation layer from string/function. Parameters: ---------- activation : function, or str, or nn.Module Activation function or name of activation function. Returns ------- nn.Module Activation layer. """ assert activation is not None if isfunction(activation): return activation() elif isinstance(activation, str): if activation == 'relu': return nn.ReLU(inplace=True) elif activation == 'relu6': return nn.ReLU6(inplace=True) elif activation == 'swish': return Swish() elif activation == 'hswish': return HSwish(inplace=True) elif activation == 'sigmoid': return nn.Sigmoid() elif activation == 'hsigmoid': return HSigmoid() else: raise NotImplementedError() else: assert isinstance(activation, nn.Module) return activation class HSigmoid(nn.Module): """ Approximated sigmoid function, so-called hard-version of sigmoid from 'Searching for MobileNetV3,' https://arxiv.org/abs/1905.02244. """ def forward(self, x): return F.relu6(x + 3.0, inplace=True) / 6.0 class HSwish(nn.Module): """ H-Swish activation function from 'Searching for MobileNetV3,' https://arxiv.org/abs/1905.02244. Parameters: ---------- inplace : bool Whether to use inplace version of the module. """ def __init__(self, inplace=False): super(HSwish, self).__init__() self.inplace = inplace def forward(self, x): return x * F.relu6(x + 3.0, inplace=self.inplace) / 6.0 class Swish(nn.Module): """ Swish activation function from 'Searching for Activation Functions,' https://arxiv.org/abs/1710.05941. """ def forward(self, x): return x * torch.sigmoid(x) class ConvBlock(nn.Module): """ Standard convolution block with Batch normalization and activation. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. kernel_size : int or tuple/list of 2 int Convolution window size. stride : int or tuple/list of 2 int Strides of the convolution. padding : int or tuple/list of 2 int Padding value for convolution layer. dilation : int or tuple/list of 2 int, default 1 Dilation value for convolution layer. groups : int, default 1 Number of groups. bias : bool, default False Whether the layer uses a bias vector. use_bn : bool, default True Whether to use BatchNorm layer. bn_eps : float, default 1e-5 Small float added to variance in Batch norm. activation : function or str or None, default nn.ReLU(inplace=True) Activation function or name of activation function. """ def __init__(self, in_channels, out_channels, kernel_size, stride, padding, dilation=1, groups=1, bias=False, use_bn=True, bn_eps= 1e-05, activation=lambda : nn.ReLU(inplace=True)): super(ConvBlock, self).__init__() self.activate = activation is not None self.use_bn = use_bn self.conv = nn.Conv2d(in_channels=in_channels, out_channels= out_channels, kernel_size=kernel_size, stride=stride, padding= padding, dilation=dilation, groups=groups, bias=bias) if self.use_bn: self.bn = nn.BatchNorm2d(num_features=out_channels, eps=bn_eps) if self.activate: self.activ = get_activation_layer(activation) def forward(self, x): x = self.conv(x) if self.use_bn: x = self.bn(x) if self.activate: x = self.activ(x) return x class AlexConv(ConvBlock): """ AlexNet specific convolution block. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. kernel_size : int or tuple/list of 2 int Convolution window size. stride : int or tuple/list of 2 int Strides of the convolution. padding : int or tuple/list of 2 int Padding value for convolution layer. use_lrn : bool Whether to use LRN layer. """ def __init__(self, in_channels, out_channels, kernel_size, stride, padding, use_lrn): super(AlexConv, self).__init__(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride= stride, padding=padding, bias=True, use_bn=False) self.use_lrn = use_lrn def forward(self, x): x = super(AlexConv, self).forward(x) if self.use_lrn: x = F.local_response_norm(x, size=5, k=2.0) 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, 'stride': 1, 'padding': 4, 'use_lrn': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.nn.functional as F from inspect import isfunction assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_convolution_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 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') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) @triton.jit def triton_poi_fused_constant_pad_nd_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 2592 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 81 % 8 x2 = xindex // 648 x3 = xindex % 648 x4 = xindex tmp0 = -2 + x1 tmp1 = tl.full([1], 0, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tmp2 & tmp4 tmp6 = tl.load(in_ptr0 + (-162 + x3 + 324 * x2), tmp5 & xmask, other=0.0) tmp7 = tl.full([1], 0, tl.int32) tmp8 = triton_helpers.maximum(tmp7, tmp6) tmp9 = tmp8 * tmp8 tmp10 = tl.full(tmp9.shape, 0.0, tmp9.dtype) tmp11 = tl.where(tmp5, tmp9, tmp10) tl.store(out_ptr0 + x4, tmp11, xmask) @triton.jit def triton_poi_fused_add_avg_pool3d_div_mul_pow_relu_2(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 1296 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 324 x1 = xindex // 324 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 648 * x1), xmask) tmp1 = tl.load(in_ptr0 + (81 + x0 + 648 * x1), xmask) tmp3 = tl.load(in_ptr0 + (162 + x0 + 648 * x1), xmask) tmp5 = tl.load(in_ptr0 + (243 + x0 + 648 * x1), xmask) tmp7 = tl.load(in_ptr0 + (324 + x0 + 648 * x1), xmask) tmp11 = tl.load(in_ptr1 + x2, xmask) tmp2 = tmp1 + tmp0 tmp4 = tmp3 + tmp2 tmp6 = tmp5 + tmp4 tmp8 = tmp7 + tmp6 tmp9 = 0.2 tmp10 = tmp8 * tmp9 tmp12 = tl.full([1], 0, tl.int32) tmp13 = triton_helpers.maximum(tmp12, tmp11) tmp14 = 0.0001 tmp15 = tmp10 * tmp14 tmp16 = 2.0 tmp17 = tmp15 + tmp16 tmp18 = 0.75 tmp19 = libdevice.pow(tmp17, tmp18) tmp20 = tmp13 / tmp19 tl.store(out_ptr0 + x2, tmp10, xmask) tl.store(out_ptr1 + x2, tmp20, 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=(4, 4), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 9, 9), (324, 81, 9, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(1296)](buf1, primals_2, 1296, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((4, 1, 8, 9, 9), (648, 648, 81, 9, 1), torch.float32) triton_poi_fused_constant_pad_nd_1[grid(2592)](buf1, buf2, 2592, XBLOCK=128, num_warps=4, num_stages=1) buf3 = empty_strided_cuda((4, 1, 4, 9, 9), (324, 324, 81, 9, 1), torch.float32) buf4 = empty_strided_cuda((4, 4, 9, 9), (324, 81, 9, 1), torch.float32) triton_poi_fused_add_avg_pool3d_div_mul_pow_relu_2[grid(1296)](buf2, buf1, buf3, buf4, 1296, XBLOCK=128, num_warps=4, num_stages=1) return buf4, primals_1, primals_3, buf1, buf2, buf3 def get_activation_layer(activation): """ Create activation layer from string/function. Parameters: ---------- activation : function, or str, or nn.Module Activation function or name of activation function. Returns ------- nn.Module Activation layer. """ assert activation is not None if isfunction(activation): return activation() elif isinstance(activation, str): if activation == 'relu': return nn.ReLU(inplace=True) elif activation == 'relu6': return nn.ReLU6(inplace=True) elif activation == 'swish': return Swish() elif activation == 'hswish': return HSwish(inplace=True) elif activation == 'sigmoid': return nn.Sigmoid() elif activation == 'hsigmoid': return HSigmoid() else: raise NotImplementedError() else: assert isinstance(activation, nn.Module) return activation class HSigmoid(nn.Module): """ Approximated sigmoid function, so-called hard-version of sigmoid from 'Searching for MobileNetV3,' https://arxiv.org/abs/1905.02244. """ def forward(self, x): return F.relu6(x + 3.0, inplace=True) / 6.0 class HSwish(nn.Module): """ H-Swish activation function from 'Searching for MobileNetV3,' https://arxiv.org/abs/1905.02244. Parameters: ---------- inplace : bool Whether to use inplace version of the module. """ def __init__(self, inplace=False): super(HSwish, self).__init__() self.inplace = inplace def forward(self, x): return x * F.relu6(x + 3.0, inplace=self.inplace) / 6.0 class Swish(nn.Module): """ Swish activation function from 'Searching for Activation Functions,' https://arxiv.org/abs/1710.05941. """ def forward(self, x): return x * torch.sigmoid(x) class ConvBlock(nn.Module): """ Standard convolution block with Batch normalization and activation. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. kernel_size : int or tuple/list of 2 int Convolution window size. stride : int or tuple/list of 2 int Strides of the convolution. padding : int or tuple/list of 2 int Padding value for convolution layer. dilation : int or tuple/list of 2 int, default 1 Dilation value for convolution layer. groups : int, default 1 Number of groups. bias : bool, default False Whether the layer uses a bias vector. use_bn : bool, default True Whether to use BatchNorm layer. bn_eps : float, default 1e-5 Small float added to variance in Batch norm. activation : function or str or None, default nn.ReLU(inplace=True) Activation function or name of activation function. """ def __init__(self, in_channels, out_channels, kernel_size, stride, padding, dilation=1, groups=1, bias=False, use_bn=True, bn_eps= 1e-05, activation=lambda : nn.ReLU(inplace=True)): super(ConvBlock, self).__init__() self.activate = activation is not None self.use_bn = use_bn self.conv = nn.Conv2d(in_channels=in_channels, out_channels= out_channels, kernel_size=kernel_size, stride=stride, padding= padding, dilation=dilation, groups=groups, bias=bias) if self.use_bn: self.bn = nn.BatchNorm2d(num_features=out_channels, eps=bn_eps) if self.activate: self.activ = get_activation_layer(activation) def forward(self, x): x = self.conv(x) if self.use_bn: x = self.bn(x) if self.activate: x = self.activ(x) return x class AlexConvNew(ConvBlock): """ AlexNet specific convolution block. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. kernel_size : int or tuple/list of 2 int Convolution window size. stride : int or tuple/list of 2 int Strides of the convolution. padding : int or tuple/list of 2 int Padding value for convolution layer. use_lrn : bool Whether to use LRN layer. """ def __init__(self, in_channels, out_channels, kernel_size, stride, padding, use_lrn): super(AlexConvNew, self).__init__(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride= stride, padding=padding, bias=True, use_bn=False) self.use_lrn = use_lrn 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]
iofthetiger/pkuad
AlexConv
false
6,899
[ "Apache-2.0" ]
1
07496d108c614c84be028f344830becc9cac8fe5
https://github.com/iofthetiger/pkuad/tree/07496d108c614c84be028f344830becc9cac8fe5
Attention
import torch import torch.nn.functional as F class Attention(torch.nn.Module): """Scaled dot product attention.""" def __init__(self, hidden_dim, **kwargs): super(Attention, self).__init__(**kwargs) self.projection_layer = torch.nn.Linear(hidden_dim, 1) def forward(self, atten_post): posts_attention_values = self.projection_layer(atten_post) posts_attention_weights = F.softmax(posts_attention_values.permute( 0, 2, 1), dim=-1) del posts_attention_values torch.cuda.empty_cache() self_atten_output_post = torch.matmul(posts_attention_weights, atten_post).squeeze(dim=1) return self_atten_output_post, posts_attention_weights def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'hidden_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math assert_size_stride = torch._C._dynamo.guards.assert_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 = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = 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 = 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), (16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((16, 1), (1, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (16, 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, 1, 4), (4, 16, 1), torch.float32) get_raw_stream(0) triton_poi_fused__softmax_0[grid(16)](buf1, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1) buf3 = reinterpret_tensor(buf1, (4, 1, 4), (4, 4, 1), 0) del buf1 triton_poi_fused__softmax_1[grid(16)](buf2, buf3, 16, XBLOCK=16, num_warps=1, num_stages=1) buf4 = reinterpret_tensor(buf2, (4, 1, 4), (4, 4, 1), 0) del buf2 extern_kernels.bmm(buf3, primals_3, out=buf4) return reinterpret_tensor(buf4, (4, 4), (4, 1), 0), buf3, primals_3, buf3 class AttentionNew(torch.nn.Module): """Scaled dot product attention.""" def __init__(self, hidden_dim, **kwargs): super(AttentionNew, self).__init__(**kwargs) self.projection_layer = torch.nn.Linear(hidden_dim, 1) def forward(self, input_0): primals_1 = self.projection_layer.weight primals_2 = self.projection_layer.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0], output[1]
isspek/Cross-Lingual-Cyberbullying
Attention
false
6,900
[ "MIT" ]
1
710c136b9233f0be87af72e43e25722e73158c52
https://github.com/isspek/Cross-Lingual-Cyberbullying/tree/710c136b9233f0be87af72e43e25722e73158c52
MobileNetV3Classifier
import torch import torch.nn as nn import torch.nn.functional as F def conv1x1(in_channels, out_channels, stride=1, groups=1, bias=False): """ Convolution 1x1 layer. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. stride : int or tuple/list of 2 int, default 1 Strides of the convolution. groups : int, default 1 Number of groups. bias : bool, default False Whether the layer uses a bias vector. """ return nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=stride, groups=groups, bias=bias) class HSwish(nn.Module): """ H-Swish activation function from 'Searching for MobileNetV3,' https://arxiv.org/abs/1905.02244. Parameters: ---------- inplace : bool Whether to use inplace version of the module. """ def __init__(self, inplace=False): super(HSwish, self).__init__() self.inplace = inplace def forward(self, x): return x * F.relu6(x + 3.0, inplace=self.inplace) / 6.0 class MobileNetV3Classifier(nn.Module): """ MobileNetV3 classifier. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. mid_channels : int Number of middle channels. dropout_rate : float Parameter of Dropout layer. Faction of the input units to drop. """ def __init__(self, in_channels, out_channels, mid_channels, dropout_rate): super(MobileNetV3Classifier, self).__init__() self.use_dropout = dropout_rate != 0.0 self.conv1 = conv1x1(in_channels=in_channels, out_channels=mid_channels ) self.activ = HSwish(inplace=True) if self.use_dropout: self.dropout = nn.Dropout(p=dropout_rate) self.conv2 = conv1x1(in_channels=mid_channels, out_channels= out_channels, bias=True) def forward(self, x): x = self.conv1(x) x = self.activ(x) if self.use_dropout: x = self.dropout(x) x = self.conv2(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4, 'mid_channels': 4, 'dropout_rate': 0.5}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn import torch.nn.functional as F assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_div_hardtanh_mul_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 3.0 tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = 6.0 tmp6 = triton_helpers.minimum(tmp4, tmp5) tmp7 = tmp0 * tmp6 tmp8 = 0.16666666666666666 tmp9 = tmp7 * tmp8 tl.store(out_ptr0 + x0, tmp9, xmask) @triton.jit def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4, 4, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_4, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_2, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1)) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_hardtanh_mul_0[grid(256)](buf0, buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf2 = extern_kernels.convolution(buf1, primals_3, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 4, 4, 4), (64, 16, 4, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_1[grid(256)](buf3, primals_4, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_4 return buf3, primals_1, primals_2, primals_3, buf0, buf1 def conv1x1(in_channels, out_channels, stride=1, groups=1, bias=False): """ Convolution 1x1 layer. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. stride : int or tuple/list of 2 int, default 1 Strides of the convolution. groups : int, default 1 Number of groups. bias : bool, default False Whether the layer uses a bias vector. """ return nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=stride, groups=groups, bias=bias) class HSwish(nn.Module): """ H-Swish activation function from 'Searching for MobileNetV3,' https://arxiv.org/abs/1905.02244. Parameters: ---------- inplace : bool Whether to use inplace version of the module. """ def __init__(self, inplace=False): super(HSwish, self).__init__() self.inplace = inplace def forward(self, x): return x * F.relu6(x + 3.0, inplace=self.inplace) / 6.0 class MobileNetV3ClassifierNew(nn.Module): """ MobileNetV3 classifier. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. mid_channels : int Number of middle channels. dropout_rate : float Parameter of Dropout layer. Faction of the input units to drop. """ def __init__(self, in_channels, out_channels, mid_channels, dropout_rate): super(MobileNetV3ClassifierNew, self).__init__() self.use_dropout = dropout_rate != 0.0 self.conv1 = conv1x1(in_channels=in_channels, out_channels=mid_channels ) self.activ = HSwish(inplace=True) if self.use_dropout: self.dropout = nn.Dropout(p=dropout_rate) self.conv2 = conv1x1(in_channels=mid_channels, out_channels= out_channels, bias=True) def forward(self, input_0): primals_1 = self.conv1.weight primals_3 = self.conv2.weight primals_4 = self.conv2.bias primals_2 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
iofthetiger/pkuad
MobileNetV3Classifier
false
6,901
[ "Apache-2.0" ]
1
07496d108c614c84be028f344830becc9cac8fe5
https://github.com/iofthetiger/pkuad/tree/07496d108c614c84be028f344830becc9cac8fe5
SPHead
import torch import torch.nn as nn import torch.nn.functional as F from inspect import isfunction def conv1x1(in_channels, out_channels, stride=1, groups=1, bias=False): """ Convolution 1x1 layer. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. stride : int or tuple/list of 2 int, default 1 Strides of the convolution. groups : int, default 1 Number of groups. bias : bool, default False Whether the layer uses a bias vector. """ return nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=stride, groups=groups, bias=bias) def get_activation_layer(activation): """ Create activation layer from string/function. Parameters: ---------- activation : function, or str, or nn.Module Activation function or name of activation function. Returns ------- nn.Module Activation layer. """ assert activation is not None if isfunction(activation): return activation() elif isinstance(activation, str): if activation == 'relu': return nn.ReLU(inplace=True) elif activation == 'relu6': return nn.ReLU6(inplace=True) elif activation == 'swish': return Swish() elif activation == 'hswish': return HSwish(inplace=True) elif activation == 'sigmoid': return nn.Sigmoid() elif activation == 'hsigmoid': return HSigmoid() else: raise NotImplementedError() else: assert isinstance(activation, nn.Module) return activation def conv3x3_block(in_channels, out_channels, stride=1, padding=1, dilation= 1, groups=1, bias=False, use_bn=True, bn_eps=1e-05, activation=lambda : nn.ReLU(inplace=True)): """ 3x3 version of the standard convolution block. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. stride : int or tuple/list of 2 int, default 1 Strides of the convolution. padding : int or tuple/list of 2 int, default 1 Padding value for convolution layer. dilation : int or tuple/list of 2 int, default 1 Dilation value for convolution layer. groups : int, default 1 Number of groups. bias : bool, default False Whether the layer uses a bias vector. use_bn : bool, default True Whether to use BatchNorm layer. bn_eps : float, default 1e-5 Small float added to variance in Batch norm. activation : function or str or None, default nn.ReLU(inplace=True) Activation function or name of activation function. """ return ConvBlock(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias, use_bn=use_bn, bn_eps=bn_eps, activation= activation) class HSigmoid(nn.Module): """ Approximated sigmoid function, so-called hard-version of sigmoid from 'Searching for MobileNetV3,' https://arxiv.org/abs/1905.02244. """ def forward(self, x): return F.relu6(x + 3.0, inplace=True) / 6.0 class HSwish(nn.Module): """ H-Swish activation function from 'Searching for MobileNetV3,' https://arxiv.org/abs/1905.02244. Parameters: ---------- inplace : bool Whether to use inplace version of the module. """ def __init__(self, inplace=False): super(HSwish, self).__init__() self.inplace = inplace def forward(self, x): return x * F.relu6(x + 3.0, inplace=self.inplace) / 6.0 class Swish(nn.Module): """ Swish activation function from 'Searching for Activation Functions,' https://arxiv.org/abs/1710.05941. """ def forward(self, x): return x * torch.sigmoid(x) class ConvBlock(nn.Module): """ Standard convolution block with Batch normalization and activation. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. kernel_size : int or tuple/list of 2 int Convolution window size. stride : int or tuple/list of 2 int Strides of the convolution. padding : int or tuple/list of 2 int Padding value for convolution layer. dilation : int or tuple/list of 2 int, default 1 Dilation value for convolution layer. groups : int, default 1 Number of groups. bias : bool, default False Whether the layer uses a bias vector. use_bn : bool, default True Whether to use BatchNorm layer. bn_eps : float, default 1e-5 Small float added to variance in Batch norm. activation : function or str or None, default nn.ReLU(inplace=True) Activation function or name of activation function. """ def __init__(self, in_channels, out_channels, kernel_size, stride, padding, dilation=1, groups=1, bias=False, use_bn=True, bn_eps= 1e-05, activation=lambda : nn.ReLU(inplace=True)): super(ConvBlock, self).__init__() self.activate = activation is not None self.use_bn = use_bn self.conv = nn.Conv2d(in_channels=in_channels, out_channels= out_channels, kernel_size=kernel_size, stride=stride, padding= padding, dilation=dilation, groups=groups, bias=bias) if self.use_bn: self.bn = nn.BatchNorm2d(num_features=out_channels, eps=bn_eps) if self.activate: self.activ = get_activation_layer(activation) def forward(self, x): x = self.conv(x) if self.use_bn: x = self.bn(x) if self.activate: x = self.activ(x) return x class SPHead(nn.Module): """ SuperPointNet head block. Parameters: ---------- in_channels : int Number of input channels. mid_channels : int Number of middle channels. out_channels : int Number of output channels. """ def __init__(self, in_channels, mid_channels, out_channels): super(SPHead, self).__init__() self.conv1 = conv3x3_block(in_channels=in_channels, out_channels= mid_channels, bias=True, use_bn=False) self.conv2 = conv1x1(in_channels=mid_channels, out_channels= out_channels, bias=True) def forward(self, x): x = self.conv1(x) x = self.conv2(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'mid_channels': 4, 'out_channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn import torch.nn.functional as F from inspect import isfunction assert_size_stride = torch._C._dynamo.guards.assert_size_stride @triton.jit def triton_poi_fused_convolution_relu_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, xmask) @triton.jit def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4, 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, 1, 1), (4, 1, 1, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(256)](buf1, primals_2, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = extern_kernels.convolution(buf1, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 4, 4, 4), (64, 16, 4, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_1[grid(256)](buf3, primals_5, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 return buf3, primals_1, primals_3, primals_4, buf1 def conv1x1(in_channels, out_channels, stride=1, groups=1, bias=False): """ Convolution 1x1 layer. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. stride : int or tuple/list of 2 int, default 1 Strides of the convolution. groups : int, default 1 Number of groups. bias : bool, default False Whether the layer uses a bias vector. """ return nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=stride, groups=groups, bias=bias) def get_activation_layer(activation): """ Create activation layer from string/function. Parameters: ---------- activation : function, or str, or nn.Module Activation function or name of activation function. Returns ------- nn.Module Activation layer. """ assert activation is not None if isfunction(activation): return activation() elif isinstance(activation, str): if activation == 'relu': return nn.ReLU(inplace=True) elif activation == 'relu6': return nn.ReLU6(inplace=True) elif activation == 'swish': return Swish() elif activation == 'hswish': return HSwish(inplace=True) elif activation == 'sigmoid': return nn.Sigmoid() elif activation == 'hsigmoid': return HSigmoid() else: raise NotImplementedError() else: assert isinstance(activation, nn.Module) return activation def conv3x3_block(in_channels, out_channels, stride=1, padding=1, dilation= 1, groups=1, bias=False, use_bn=True, bn_eps=1e-05, activation=lambda : nn.ReLU(inplace=True)): """ 3x3 version of the standard convolution block. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. stride : int or tuple/list of 2 int, default 1 Strides of the convolution. padding : int or tuple/list of 2 int, default 1 Padding value for convolution layer. dilation : int or tuple/list of 2 int, default 1 Dilation value for convolution layer. groups : int, default 1 Number of groups. bias : bool, default False Whether the layer uses a bias vector. use_bn : bool, default True Whether to use BatchNorm layer. bn_eps : float, default 1e-5 Small float added to variance in Batch norm. activation : function or str or None, default nn.ReLU(inplace=True) Activation function or name of activation function. """ return ConvBlock(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias, use_bn=use_bn, bn_eps=bn_eps, activation= activation) class HSigmoid(nn.Module): """ Approximated sigmoid function, so-called hard-version of sigmoid from 'Searching for MobileNetV3,' https://arxiv.org/abs/1905.02244. """ def forward(self, x): return F.relu6(x + 3.0, inplace=True) / 6.0 class HSwish(nn.Module): """ H-Swish activation function from 'Searching for MobileNetV3,' https://arxiv.org/abs/1905.02244. Parameters: ---------- inplace : bool Whether to use inplace version of the module. """ def __init__(self, inplace=False): super(HSwish, self).__init__() self.inplace = inplace def forward(self, x): return x * F.relu6(x + 3.0, inplace=self.inplace) / 6.0 class Swish(nn.Module): """ Swish activation function from 'Searching for Activation Functions,' https://arxiv.org/abs/1710.05941. """ def forward(self, x): return x * torch.sigmoid(x) class ConvBlock(nn.Module): """ Standard convolution block with Batch normalization and activation. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. kernel_size : int or tuple/list of 2 int Convolution window size. stride : int or tuple/list of 2 int Strides of the convolution. padding : int or tuple/list of 2 int Padding value for convolution layer. dilation : int or tuple/list of 2 int, default 1 Dilation value for convolution layer. groups : int, default 1 Number of groups. bias : bool, default False Whether the layer uses a bias vector. use_bn : bool, default True Whether to use BatchNorm layer. bn_eps : float, default 1e-5 Small float added to variance in Batch norm. activation : function or str or None, default nn.ReLU(inplace=True) Activation function or name of activation function. """ def __init__(self, in_channels, out_channels, kernel_size, stride, padding, dilation=1, groups=1, bias=False, use_bn=True, bn_eps= 1e-05, activation=lambda : nn.ReLU(inplace=True)): super(ConvBlock, self).__init__() self.activate = activation is not None self.use_bn = use_bn self.conv = nn.Conv2d(in_channels=in_channels, out_channels= out_channels, kernel_size=kernel_size, stride=stride, padding= padding, dilation=dilation, groups=groups, bias=bias) if self.use_bn: self.bn = nn.BatchNorm2d(num_features=out_channels, eps=bn_eps) if self.activate: self.activ = get_activation_layer(activation) def forward(self, x): x = self.conv(x) if self.use_bn: x = self.bn(x) if self.activate: x = self.activ(x) return x class SPHeadNew(nn.Module): """ SuperPointNet head block. Parameters: ---------- in_channels : int Number of input channels. mid_channels : int Number of middle channels. out_channels : int Number of output channels. """ def __init__(self, in_channels, mid_channels, out_channels): super(SPHeadNew, self).__init__() self.conv1 = conv3x3_block(in_channels=in_channels, out_channels= mid_channels, bias=True, use_bn=False) self.conv2 = conv1x1(in_channels=mid_channels, out_channels= out_channels, bias=True) def forward(self, input_0): primals_1 = self.conv1.conv.weight primals_2 = self.conv1.conv.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]
iofthetiger/pkuad
SPHead
false
6,902
[ "Apache-2.0" ]
1
07496d108c614c84be028f344830becc9cac8fe5
https://github.com/iofthetiger/pkuad/tree/07496d108c614c84be028f344830becc9cac8fe5
NTXent
import torch import torch.nn as nn import torch.nn.functional as F class NTXent(nn.Module): def forward(self, z1, z2, t): batch_size = z1.shape[0] device = z1.device z1 = F.normalize(z1, dim=-1) z2 = F.normalize(z2, dim=-1) similarity = torch.matmul(z1, z2.T) similarity = similarity * torch.exp(t) targets = torch.arange(batch_size, device=device) loss = F.cross_entropy(similarity, targets) return loss def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_div_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = libdevice.sqrt(tmp11) tmp13 = 1e-12 tmp14 = triton_helpers.maximum(tmp12, tmp13) tmp15 = tmp0 / tmp14 tl.store(out_ptr0 + x2, tmp15, xmask) @triton.jit def triton_per_fused__log_softmax_arange_exp_mul_nll_loss_forward_1(in_out_ptr0 , in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 4 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + 4 * r0, None, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + 4 * r0, None, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (1 + 4 * r0), None, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + (1 + 4 * r0), None, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (2 + 4 * r0), None, eviction_policy='evict_last') tmp10 = tl.load(in_ptr1 + (2 + 4 * r0), None, eviction_policy='evict_last') tmp14 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last') tmp15 = tl.load(in_ptr1 + (3 + 4 * r0), None, eviction_policy='evict_last') tmp2 = tl_math.exp(tmp1) tmp3 = tmp0 * tmp2 tmp6 = tl_math.exp(tmp5) tmp7 = tmp4 * tmp6 tmp8 = triton_helpers.maximum(tmp3, tmp7) tmp11 = tl_math.exp(tmp10) tmp12 = tmp9 * tmp11 tmp13 = triton_helpers.maximum(tmp8, tmp12) tmp16 = tl_math.exp(tmp15) tmp17 = tmp14 * tmp16 tmp18 = triton_helpers.maximum(tmp13, tmp17) tmp19 = tmp3 - tmp18 tmp20 = tl_math.exp(tmp19) tmp21 = tmp7 - tmp18 tmp22 = tl_math.exp(tmp21) tmp23 = tmp20 + tmp22 tmp24 = tmp12 - tmp18 tmp25 = tl_math.exp(tmp24) tmp26 = tmp23 + tmp25 tmp27 = tmp17 - tmp18 tmp28 = tl_math.exp(tmp27) tmp29 = tmp26 + tmp28 tmp30 = r0 tmp31 = tl.full([1, 1], -100, tl.int64) tmp32 = tmp30 != tmp31 tmp33 = tl.full([1, 1], 0, tl.int64) tmp34 = tl.where(tmp32, tmp30, tmp33) tmp35 = tl.load(in_ptr0 + (tmp34 + 4 * r0), None, eviction_policy= 'evict_last') tmp36 = tl.load(in_ptr1 + (tmp34 + 4 * r0), None, eviction_policy= 'evict_last') tmp37 = tl_math.exp(tmp36) tmp38 = tmp35 * tmp37 tmp39 = tmp38 - tmp18 tmp40 = tl_math.log(tmp29) tmp41 = tmp39 - tmp40 tmp42 = -tmp41 tmp43 = 0.0 tmp44 = tl.where(tmp32, tmp42, tmp43) tmp45 = tl.broadcast_to(tmp44, [XBLOCK, RBLOCK]) tmp47 = tl.sum(tmp45, 1)[:, None] tmp48 = tmp32.to(tl.int64) tmp49 = tl.broadcast_to(tmp48, [XBLOCK, RBLOCK]) tmp51 = tl.sum(tmp49, 1)[:, None] tmp52 = tmp51.to(tl.float32) tmp53 = tmp47 / tmp52 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp53, None) def call(args): arg0_1, arg1_1, arg2_1 = args args.clear() assert_size_stride(arg0_1, (4, 4), (4, 1)) assert_size_stride(arg1_1, (4, 4), (4, 1)) assert_size_stride(arg2_1, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_div_0[grid(16)](arg0_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_div_0[grid(16)](arg1_1, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg1_1 buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf0, reinterpret_tensor(buf1, (4, 4), (1, 4), 0), out=buf2) del buf0 del buf1 buf5 = empty_strided_cuda((), (), torch.float32) buf7 = buf5 del buf5 triton_per_fused__log_softmax_arange_exp_mul_nll_loss_forward_1[grid(1) ](buf7, buf2, arg2_1, 1, 4, XBLOCK=1, num_warps=2, num_stages=1) del arg2_1 del buf2 return buf7, class NTXentNew(nn.Module): def forward(self, input_0, input_1, input_2): arg0_1 = input_0 arg1_1 = input_1 arg2_1 = input_2 output = call([arg0_1, arg1_1, arg2_1]) return output[0]
isaaccorley/contrastive-surface-image-pretraining
NTXent
false
6,903
[ "MIT" ]
1
a918d4fd3b9cc61ec512af978fb4f086d3b46a70
https://github.com/isaaccorley/contrastive-surface-image-pretraining/tree/a918d4fd3b9cc61ec512af978fb4f086d3b46a70
VectorQuantizer
import torch import torch.nn as nn import torch.nn.functional as F class VectorQuantizer(nn.Module): def __init__(self, num_embeddings, embedding_dim, commitment_cost): super(VectorQuantizer, self).__init__() self._embedding_dim = embedding_dim self._num_embeddings = num_embeddings self._embedding = nn.Embedding(self._num_embeddings, self. _embedding_dim) self._embedding.weight.data.uniform_(-1 / self._num_embeddings, 1 / self._num_embeddings) self._commitment_cost = commitment_cost self.emb_indexes = [] def forward(self, inputs): inputs = inputs.permute(0, 2, 3, 1).contiguous() input_shape = inputs.shape flat_input = inputs.view(-1, self._embedding_dim) distances = torch.sum(flat_input ** 2, dim=1, keepdim=True ) + torch.sum(self._embedding.weight ** 2, dim=1 ) - 2 * torch.matmul(flat_input, self._embedding.weight.t()) encoding_indices = torch.argmin(distances, dim=1).unsqueeze(1) self.emb_indexes.extend(encoding_indices.cpu().detach().numpy()[0]) encodings = torch.zeros(encoding_indices.shape[0], self. _num_embeddings, device=inputs.device) encodings.scatter_(1, encoding_indices, 1) quantized = torch.matmul(encodings, self._embedding.weight).view( input_shape) e_latent_loss = F.mse_loss(quantized.detach(), inputs) q_latent_loss = F.mse_loss(quantized, inputs.detach()) loss = q_latent_loss + self._commitment_cost * e_latent_loss quantized = inputs + (quantized - inputs).detach() avg_probs = torch.mean(encodings, dim=0) perplexity = torch.exp(-torch.sum(avg_probs * torch.log(avg_probs + 1e-10))) return loss, quantized.permute(0, 3, 1, 2).contiguous( ), perplexity, encodings def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_embeddings': 4, 'embedding_dim': 4, 'commitment_cost': 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_cpu = torch._C._dynamo.guards._empty_strided_cpu 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_view_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 64 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x1 = xindex y0 = yindex tmp0 = tl.load(in_ptr0 + (16 * x1 + 64 * (y0 // 16) + y0 % 16), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x1 + 4 * y0), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_add_mul_pow_sub_sum_1(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 x0 = xindex % 4 x2 = xindex tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last') tmp13 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp16 = 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' ) tmp23 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tmp0 * tmp0 tmp3 = tmp2 * tmp2 tmp4 = tmp1 + tmp3 tmp6 = tmp5 * tmp5 tmp7 = tmp4 + tmp6 tmp9 = tmp8 * tmp8 tmp10 = tmp7 + tmp9 tmp12 = tmp11 * tmp11 tmp14 = tmp13 * tmp13 tmp15 = tmp12 + tmp14 tmp17 = tmp16 * tmp16 tmp18 = tmp15 + tmp17 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp22 = tmp10 + tmp21 tmp24 = 2.0 tmp25 = tmp23 * tmp24 tmp26 = tmp22 - tmp25 tl.store(in_out_ptr0 + x2, tmp26, xmask) @triton.jit def triton_poi_fused_argmin_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp17 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp32 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 < tmp1 tmp3 = tmp0 == tmp1 tmp4 = tmp0 != tmp0 tmp5 = tmp1 != tmp1 tmp6 = tmp4 > tmp5 tmp7 = tmp2 | tmp6 tmp8 = tmp4 & tmp5 tmp9 = tmp3 | tmp8 tmp10 = tl.full([1], 0, tl.int64) tmp11 = tl.full([1], 1, tl.int64) tmp12 = tmp10 < tmp11 tmp13 = tmp9 & tmp12 tmp14 = tmp7 | tmp13 tmp15 = tl.where(tmp14, tmp0, tmp1) tmp16 = tl.where(tmp14, tmp10, tmp11) tmp18 = tmp15 < tmp17 tmp19 = tmp15 == tmp17 tmp20 = tmp15 != tmp15 tmp21 = tmp17 != tmp17 tmp22 = tmp20 > tmp21 tmp23 = tmp18 | tmp22 tmp24 = tmp20 & tmp21 tmp25 = tmp19 | tmp24 tmp26 = tl.full([1], 2, tl.int64) tmp27 = tmp16 < tmp26 tmp28 = tmp25 & tmp27 tmp29 = tmp23 | tmp28 tmp30 = tl.where(tmp29, tmp15, tmp17) tmp31 = tl.where(tmp29, tmp16, tmp26) tmp33 = tmp30 < tmp32 tmp34 = tmp30 == tmp32 tmp35 = tmp30 != tmp30 tmp36 = tmp32 != tmp32 tmp37 = tmp35 > tmp36 tmp38 = tmp33 | tmp37 tmp39 = tmp35 & tmp36 tmp40 = tmp34 | tmp39 tmp41 = tl.full([1], 3, tl.int64) tmp42 = tmp31 < tmp41 tmp43 = tmp40 & tmp42 tmp44 = tmp38 | tmp43 tl.where(tmp44, tmp30, tmp32) tmp46 = tl.where(tmp44, tmp31, tmp41) tl.store(out_ptr0 + x0, tmp46, xmask) @triton.jit def triton_poi_fused_scatter_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 x1 = xindex // 4 x0 = xindex % 4 x2 = xindex tmp0 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp1 = x0 tmp2 = tmp0 == tmp1 tmp3 = 1.0 tmp4 = 0.0 tmp5 = tl.where(tmp2, tmp3, tmp4) tl.store(out_ptr0 + x2, tmp5, xmask) @triton.jit def triton_per_fused_add_clone_mse_loss_mul_4(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r3 = rindex r0 = rindex % 4 r1 = rindex // 4 % 16 r2 = rindex // 64 tmp0 = tl.load(in_ptr0 + r3, None) tmp1 = tl.load(in_ptr1 + (r1 + 16 * r0 + 64 * r2), None, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp3 = tmp2 * tmp2 tmp4 = tl.broadcast_to(tmp3, [RBLOCK]) tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0)) tmp7 = 256.0 tmp8 = tmp6 / tmp7 tmp9 = 4.0 tmp10 = tmp8 * tmp9 tmp11 = tmp8 + tmp10 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp11, None) @triton.jit def triton_per_fused_mean_5(in_ptr0, out_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 + (x0 + 4 * r1), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, 0) tmp4 = tl.sum(tmp3, 1)[:, None] tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_per_fused_add_exp_log_mean_mul_neg_sum_6(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 4 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = 64.0 tmp2 = tmp0 / tmp1 tmp3 = 1e-10 tmp4 = tmp2 + tmp3 tmp5 = tl_math.log(tmp4) tmp6 = tmp2 * tmp5 tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK]) tmp9 = tl.sum(tmp7, 1)[:, None] tmp10 = -tmp9 tmp11 = tl_math.exp(tmp10) tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp11, None) @triton.jit def triton_poi_fused_clone_7(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 16 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 4 y1 = yindex // 4 tmp0 = tl.load(in_ptr0 + (x2 + 16 * y3), xmask & ymask) tmp1 = tl.load(in_ptr1 + (y0 + 4 * x2 + 64 * y1), xmask & ymask) tmp2 = tmp1 - tmp0 tmp3 = tmp0 + tmp2 tl.store(out_ptr0 + (x2 + 16 * y3), tmp3, xmask & ymask) @triton.jit def triton_poi_fused_clone_mse_loss_mse_loss_backward_8(in_out_ptr0, in_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 64 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 16 y1 = yindex // 16 tmp0 = tl.load(in_out_ptr0 + (x2 + 4 * y3), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (y0 + 16 * x2 + 64 * y1), xmask & ymask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp3 = 0.0078125 tmp4 = tmp2 * tmp3 tl.debug_barrier() tl.store(in_out_ptr0 + (x2 + 4 * y3), tmp4, xmask & ymask) def call(args): primals_1, primals_2 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clone_view_0[grid(64, 4)](primals_1, buf0, 64, 4, XBLOCK=4, YBLOCK=32, num_warps=4, num_stages=1) buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(buf0, reinterpret_tensor(primals_2, (4, 4), (1, 4 ), 0), out=buf1) buf2 = buf1 del buf1 triton_poi_fused_add_mul_pow_sub_sum_1[grid(256)](buf2, buf0, primals_2, 256, XBLOCK=256, num_warps=4, num_stages=1) buf3 = empty_strided_cuda((64,), (1,), torch.int64) triton_poi_fused_argmin_2[grid(64)](buf2, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1) buf4 = empty_strided_cpu((64, 1), (1, 1), torch.int64) buf4.copy_(reinterpret_tensor(buf3, (64, 1), (1, 1), 0)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf5 = buf2 del buf2 triton_poi_fused_scatter_3[grid(256)](buf3, buf5, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf3 buf6 = buf0 del buf0 extern_kernels.mm(buf5, primals_2, out=buf6) del primals_2 buf7 = empty_strided_cuda((), (), torch.float32) buf12 = buf7 del buf7 triton_per_fused_add_clone_mse_loss_mul_4[grid(1)](buf12, buf6, primals_1, 1, 256, num_warps=2, num_stages=1) buf8 = empty_strided_cuda((4,), (1,), torch.float32) triton_per_fused_mean_5[grid(4)](buf5, buf8, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) buf9 = empty_strided_cuda((), (), torch.float32) buf13 = buf9 del buf9 triton_per_fused_add_exp_log_mean_mul_neg_sum_6[grid(1)](buf13, buf8, 1, 4, XBLOCK=1, num_warps=2, num_stages=1) del buf8 buf10 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_clone_7[grid(16, 16)](primals_1, buf6, buf10, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) buf11 = reinterpret_tensor(buf6, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf6 triton_poi_fused_clone_mse_loss_mse_loss_backward_8[grid(64, 4)](buf11, primals_1, 64, 4, XBLOCK=4, YBLOCK=32, num_warps=4, num_stages=1) del primals_1 return buf12, buf10, buf13, buf5, reinterpret_tensor(buf4, (), (), 0 ), buf11, reinterpret_tensor(buf5, (4, 64), (1, 4), 0) class VectorQuantizerNew(nn.Module): def __init__(self, num_embeddings, embedding_dim, commitment_cost): super(VectorQuantizerNew, self).__init__() self._embedding_dim = embedding_dim self._num_embeddings = num_embeddings self._embedding = nn.Embedding(self._num_embeddings, self. _embedding_dim) self._embedding.weight.data.uniform_(-1 / self._num_embeddings, 1 / self._num_embeddings) self._commitment_cost = commitment_cost self.emb_indexes = [] def forward(self, input_0): primals_2 = self._embedding.weight primals_1 = input_0 output = call([primals_1, primals_2]) return output[0], output[1], output[2], output[3]
imatge-upc/pixelcoordEDL
VectorQuantizer
false
6,904
[ "MIT" ]
1
353632feed6ac8c93758c1a2a1b7a477e7ff053c
https://github.com/imatge-upc/pixelcoordEDL/tree/353632feed6ac8c93758c1a2a1b7a477e7ff053c
NavigatorBranch
import torch import torch.nn as nn def conv1x1(in_channels, out_channels, stride=1, groups=1, bias=False): """ Convolution 1x1 layer. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. stride : int or tuple/list of 2 int, default 1 Strides of the convolution. groups : int, default 1 Number of groups. bias : bool, default False Whether the layer uses a bias vector. """ return nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=stride, groups=groups, bias=bias) def conv3x3(in_channels, out_channels, stride=1, padding=1, dilation=1, groups=1, bias=False): """ Convolution 3x3 layer. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. stride : int or tuple/list of 2 int, default 1 Strides of the convolution. padding : int or tuple/list of 2 int, default 1 Padding value for convolution layer. groups : int, default 1 Number of groups. bias : bool, default False Whether the layer uses a bias vector. """ return nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias) class Flatten(nn.Module): """ Simple flatten module. """ def forward(self, x): return x.view(x.size(0), -1) class NavigatorBranch(nn.Module): """ Navigator branch block for Navigator unit. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. stride : int or tuple/list of 2 int Strides of the convolution. """ def __init__(self, in_channels, out_channels, stride): super(NavigatorBranch, self).__init__() mid_channels = 128 self.down_conv = conv3x3(in_channels=in_channels, out_channels= mid_channels, stride=stride, bias=True) self.activ = nn.ReLU(inplace=False) self.tidy_conv = conv1x1(in_channels=mid_channels, out_channels= out_channels, bias=True) self.flatten = Flatten() def forward(self, x): y = self.down_conv(x) y = self.activ(y) z = self.tidy_conv(y) z = self.flatten(z) return z, y def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4, 'stride': 1}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 512 xnumel = 9 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 4 y1 = yindex // 4 tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask & ymask, eviction_policy= 'evict_last') tl.store(out_ptr0 + (y0 + 4 * x2 + 36 * y1), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 16 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y3 = yindex y0 = yindex % 4 y1 = yindex // 4 tmp0 = tl.load(in_ptr0 + (x2 + 16 * y3), xmask & ymask) tl.store(out_ptr0 + (y0 + 4 * x2 + 64 * y1), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_convolution_relu_2(in_ptr0, in_ptr1, out_ptr0, out_ptr1, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 512 xnumel = 16 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 128 y1 = yindex // 128 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 128 * x2 + 2048 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1, 1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(out_ptr0 + (x2 + 16 * y3), tmp4, xmask & ymask) tl.store(out_ptr1 + (y0 + 128 * x2 + 2048 * y1), tmp4, xmask & ymask) @triton.jit def triton_poi_fused_convolution_3(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 16 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 64 * y1), xmask & ymask) tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + (x2 + 16 * y3), tmp2, xmask & ymask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (128, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_2, (128,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 128, 1, 1), (128, 1, 1, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((128, 4, 3, 3), (36, 1, 12, 4), torch.float32 ) get_raw_stream(0) triton_poi_fused_0[grid(512, 9)](primals_1, buf0, 512, 9, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del primals_1 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 1, 16, 4), torch.float32) triton_poi_fused_1[grid(16, 16)](primals_3, buf1, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) del primals_3 buf2 = extern_kernels.convolution(buf1, buf0, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf2, (4, 128, 4, 4), (2048, 1, 512, 128)) buf3 = empty_strided_cuda((4, 128, 4, 4), (2048, 16, 4, 1), torch. float32) buf4 = empty_strided_cuda((4, 128, 4, 4), (2048, 1, 512, 128), torch.float32) triton_poi_fused_convolution_relu_2[grid(512, 16)](buf2, primals_2, buf3, buf4, 512, 16, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1) del buf2 del primals_2 buf5 = extern_kernels.convolution(buf4, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf5, (4, 4, 4, 4), (64, 1, 16, 4)) del buf4 buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_convolution_3[grid(16, 16)](buf5, primals_5, buf6, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) del buf5 del primals_5 return reinterpret_tensor(buf6, (4, 64), (64, 1), 0 ), buf3, buf0, buf1, primals_4, buf3 def conv1x1(in_channels, out_channels, stride=1, groups=1, bias=False): """ Convolution 1x1 layer. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. stride : int or tuple/list of 2 int, default 1 Strides of the convolution. groups : int, default 1 Number of groups. bias : bool, default False Whether the layer uses a bias vector. """ return nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=stride, groups=groups, bias=bias) def conv3x3(in_channels, out_channels, stride=1, padding=1, dilation=1, groups=1, bias=False): """ Convolution 3x3 layer. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. stride : int or tuple/list of 2 int, default 1 Strides of the convolution. padding : int or tuple/list of 2 int, default 1 Padding value for convolution layer. groups : int, default 1 Number of groups. bias : bool, default False Whether the layer uses a bias vector. """ return nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias) class Flatten(nn.Module): """ Simple flatten module. """ def forward(self, x): return x.view(x.size(0), -1) class NavigatorBranchNew(nn.Module): """ Navigator branch block for Navigator unit. Parameters: ---------- in_channels : int Number of input channels. out_channels : int Number of output channels. stride : int or tuple/list of 2 int Strides of the convolution. """ def __init__(self, in_channels, out_channels, stride): super(NavigatorBranchNew, self).__init__() mid_channels = 128 self.down_conv = conv3x3(in_channels=in_channels, out_channels= mid_channels, stride=stride, bias=True) self.activ = nn.ReLU(inplace=False) self.tidy_conv = conv1x1(in_channels=mid_channels, out_channels= out_channels, bias=True) self.flatten = Flatten() def forward(self, input_0): primals_1 = self.down_conv.weight primals_2 = self.down_conv.bias primals_4 = self.tidy_conv.weight primals_5 = self.tidy_conv.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0], output[1]
iofthetiger/pkuad
NavigatorBranch
false
6,905
[ "Apache-2.0" ]
1
07496d108c614c84be028f344830becc9cac8fe5
https://github.com/iofthetiger/pkuad/tree/07496d108c614c84be028f344830becc9cac8fe5
SCLN
import torch import torch.nn as nn class LinearNorm(nn.Module): """ LinearNorm Projection """ def __init__(self, in_features, out_features, bias=False): super(LinearNorm, self).__init__() self.linear = nn.Linear(in_features, out_features, bias) nn.init.xavier_uniform_(self.linear.weight) if bias: nn.init.constant_(self.linear.bias, 0.0) def forward(self, x): x = self.linear(x) return x class SCLN(nn.Module): """ Speaker Condition Layer Normalization """ def __init__(self, s_size, hidden_size, eps=1e-08, bias=False): super(SCLN, self).__init__() self.hidden_size = hidden_size self.affine_layer = LinearNorm(s_size, 2 * hidden_size, bias) self.eps = eps def forward(self, x, s): mu, sigma = torch.mean(x, dim=-1, keepdim=True), torch.std(x, dim=- 1, keepdim=True) y = (x - mu) / (sigma + self.eps) b, g = torch.split(self.affine_layer(s), self.hidden_size, dim=-1) o = g * y + b return o def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'s_size': 4, 'hidden_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.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, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + (4 + x0 + 8 * x1), xmask) 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_ptr0 + (x0 + 8 * x1), xmask) tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp8 = tmp6 + tmp7 tmp9 = 4.0 tmp10 = tmp8 / tmp9 tmp11 = tmp1 - tmp10 tmp12 = tmp2 - tmp10 tmp13 = tmp12 * tmp12 tmp14 = tmp3 - tmp10 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp10 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp7 - tmp10 tmp21 = tmp20 * tmp20 tmp22 = tmp19 + tmp21 tmp23 = 3.0 tmp24 = tmp22 / tmp23 tmp25 = libdevice.sqrt(tmp24) tmp26 = 1e-08 tmp27 = tmp25 + tmp26 tmp28 = tmp11 / tmp27 tmp29 = tmp0 * tmp28 tmp31 = tmp29 + tmp30 tl.store(out_ptr0 + x2, tmp31, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (8, 4), (4, 1)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 8), (8, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 8), (1, 4), 0), out=buf0) del primals_2 buf1 = 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)](buf0, primals_1, buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf0 return buf1, primals_1, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0) class LinearNorm(nn.Module): """ LinearNorm Projection """ def __init__(self, in_features, out_features, bias=False): super(LinearNorm, self).__init__() self.linear = nn.Linear(in_features, out_features, bias) nn.init.xavier_uniform_(self.linear.weight) if bias: nn.init.constant_(self.linear.bias, 0.0) def forward(self, x): x = self.linear(x) return x class SCLNNew(nn.Module): """ Speaker Condition Layer Normalization """ def __init__(self, s_size, hidden_size, eps=1e-08, bias=False): super(SCLNNew, self).__init__() self.hidden_size = hidden_size self.affine_layer = LinearNorm(s_size, 2 * hidden_size, bias) self.eps = eps def forward(self, input_0, input_1): primals_2 = self.affine_layer.linear.weight primals_1 = input_0 primals_3 = input_1 output = call([primals_1, primals_2, primals_3]) return output[0]
ishine/Cross-Speaker-Emotion-Transfer
SCLN
false
6,906
[ "MIT" ]
1
9d38e8058f5abc06167bac244d8ace083e2a6220
https://github.com/ishine/Cross-Speaker-Emotion-Transfer/tree/9d38e8058f5abc06167bac244d8ace083e2a6220
Tile
import torch import torch.nn as nn class Tile(nn.Module): def __init__(self, max_size, dim): super(Tile, self).__init__() self.max_size = max_size self.dim = dim def forward(self, input): return input.repeat(*[(self.max_size if x == self.dim else 1) for x in range(len(input.shape))]) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'max_size': 4, 'dim': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn 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_repeat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tl.store(out_ptr0 + x0, tmp0, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_repeat_0[grid(256)](arg0_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf0, class TileNew(nn.Module): def __init__(self, max_size, dim): super(TileNew, self).__init__() self.max_size = max_size self.dim = dim def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
ixaxaar/pytorch-npi
Tile
false
6,907
[ "MIT" ]
1
50b028840c00f7807fb6490ce6bb0918832dc360
https://github.com/ixaxaar/pytorch-npi/tree/50b028840c00f7807fb6490ce6bb0918832dc360
GlobalAvgPool2d
import torch import torch.nn as nn import torch.utils class GlobalAvgPool2d(nn.Module): def __init__(self): """Global average pooling over the input's spatial dimensions""" super(GlobalAvgPool2d, self).__init__() def forward(self, inputs): in_size = inputs.size() inputs = inputs.view((in_size[0], in_size[1], -1)).mean(dim=2) inputs = inputs.view(in_size[0], in_size[1], 1, 1) return inputs def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn import torch.utils assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_per_fused_mean_0(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 16 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, 0) tmp4 = tl.sum(tmp3, 1)[:, None] tmp5 = 16.0 tmp6 = tmp4 / tmp5 tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp6, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_mean_0[grid(16)](buf1, arg0_1, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) del arg0_1 return reinterpret_tensor(buf1, (4, 4, 1, 1), (4, 1, 1, 1), 0), class GlobalAvgPool2dNew(nn.Module): def __init__(self): """Global average pooling over the input's spatial dimensions""" super(GlobalAvgPool2dNew, self).__init__() def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
jameslong95/FasterSeg
GlobalAvgPool2d
false
6,908
[ "MIT" ]
1
872e04964ea46494a6018d9915cee5476e361c27
https://github.com/jameslong95/FasterSeg/tree/872e04964ea46494a6018d9915cee5476e361c27
Vec2ArousalNet
import torch import torch.utils.data class Vec2ArousalNet(torch.nn.Module): def __init__(self, D_in, H, D_out): super(Vec2ArousalNet, self).__init__() self.layer_1 = torch.nn.Linear(D_in, H) self.layer_2 = torch.nn.Linear(H, D_out) def forward(self, x): h = self.layer_1(x).clamp(min=0) y = self.layer_2(h).clamp(min=0, max=1) return y def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'D_in': 4, 'H': 4, 'D_out': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clamp_ge_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = tmp2 >= tmp3 tl.store(out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr1 + x2, tmp5, xmask) @triton.jit def triton_poi_fused_clamp_ge_le_logical_and_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = triton_helpers.maximum(tmp2, tmp3) tmp5 = 1.0 tmp6 = triton_helpers.minimum(tmp4, tmp5) tmp7 = tmp2 >= tmp3 tmp8 = tmp2 <= tmp5 tmp9 = tmp7 & tmp8 tl.store(out_ptr0 + x2, tmp6, xmask) tl.store(out_ptr1 + x2, tmp9, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) get_raw_stream(0) triton_poi_fused_clamp_ge_0[grid(256)](buf0, primals_2, buf1, buf5, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf2 = buf0 del buf0 extern_kernels.mm(reinterpret_tensor(buf1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf2) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused_clamp_ge_le_logical_and_1[grid(256)](buf2, primals_5, buf3, buf4, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf2 del primals_5 return buf3, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 4), (4, 1), 0), buf4, primals_4, buf5 class Vec2ArousalNetNew(torch.nn.Module): def __init__(self, D_in, H, D_out): super(Vec2ArousalNetNew, self).__init__() self.layer_1 = torch.nn.Linear(D_in, H) self.layer_2 = torch.nn.Linear(H, D_out) def forward(self, input_0): primals_1 = self.layer_1.weight primals_2 = self.layer_1.bias primals_4 = self.layer_2.weight primals_5 = self.layer_2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
jackvandrunen/hackuci18
Vec2ArousalNet
false
6,909
[ "BSD-2-Clause" ]
1
fff3fd7d116a6a83f19229a17377b84922145ebd
https://github.com/jackvandrunen/hackuci18/tree/fff3fd7d116a6a83f19229a17377b84922145ebd
LinearAttention2d
import torch class LinearAttention2d(torch.nn.Module): """ Linear attention based on parametrized compatibility score function with softmax normalization. """ def __init__(self, in_features, out_features): super(LinearAttention2d, self).__init__() self.in_features = in_features self.out_features = out_features self.proj = None if self.in_features != self.out_features: self.proj = torch.nn.Conv2d(self.in_features, self.out_features, kernel_size=(1, 1), padding=(0, 0), bias=False) self.score = torch.nn.Conv2d(self.out_features, out_channels=1, kernel_size=(1, 1), padding=(0, 0), bias=False) def forward(self, x, g): b, _, h, w = x.size() if self.proj is not None: g = self.proj(g) c = self.score(x + g) a = torch.softmax(c.view(b, 1, -1), dim=2).view(b, 1, h, w) g = torch.mul(a.expand_as(x), x) g = g.view(b, self.out_features, -1).sum(dim=2) return a, g def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_features': 4, 'out_features': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream 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_add_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask) tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_per_fused__softmax_1(in_ptr0, 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 + 16 * x0), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, float('-inf')) tmp4 = triton_helpers.max2(tmp3, 1)[:, None] tmp5 = tmp0 - tmp4 tmp6 = tl_math.exp(tmp5) tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK]) tmp9 = tl.where(xmask, tmp7, 0) tmp10 = tl.sum(tmp9, 1)[:, None] tmp11 = tmp6 / tmp10 tl.store(out_ptr2 + (r1 + 16 * x0), tmp11, xmask) @triton.jit def triton_per_fused_sum_2(in_ptr0, in_ptr1, 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) r2 = rindex x1 = xindex // 4 x3 = xindex tmp0 = tl.load(in_ptr0 + (r2 + 16 * x1), xmask, eviction_policy= 'evict_last', other=0.0) tmp1 = tl.load(in_ptr1 + (r2 + 16 * x3), 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] tl.store(out_ptr0 + x3, tmp6, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (1, 4, 1, 1), (4, 1, 1, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_0[grid(256)](primals_1, primals_2, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf1 = extern_kernels.convolution(buf0, primals_3, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf1, (4, 1, 4, 4), (16, 16, 4, 1)) buf4 = empty_strided_cuda((4, 1, 16), (16, 16, 1), torch.float32) triton_per_fused__softmax_1[grid(4)](buf1, buf4, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) del buf1 buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_per_fused_sum_2[grid(16)](buf4, primals_1, buf5, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) return reinterpret_tensor(buf4, (4, 1, 4, 4), (16, 16, 4, 1), 0 ), buf5, primals_1, primals_3, buf0, buf4 class LinearAttention2dNew(torch.nn.Module): """ Linear attention based on parametrized compatibility score function with softmax normalization. """ def __init__(self, in_features, out_features): super(LinearAttention2dNew, self).__init__() self.in_features = in_features self.out_features = out_features self.proj = None if self.in_features != self.out_features: self.proj = torch.nn.Conv2d(self.in_features, self.out_features, kernel_size=(1, 1), padding=(0, 0), bias=False) self.score = torch.nn.Conv2d(self.out_features, out_channels=1, kernel_size=(1, 1), padding=(0, 0), bias=False) def forward(self, input_0, input_1): primals_3 = self.score.weight primals_1 = input_0 primals_2 = input_1 output = call([primals_1, primals_2, primals_3]) return output[0], output[1]
itsfrank98/CT-COVID
LinearAttention2d
false
6,911
[ "MIT" ]
1
3f054000ca0518be2486cf00cfab695b09e39a26
https://github.com/itsfrank98/CT-COVID/tree/3f054000ca0518be2486cf00cfab695b09e39a26
UpConv2x2
import torch import torch.nn as nn import torch.nn.functional as F import torch.utils.data from torch.cuda import * class UpConv2x2(nn.Module): def __init__(self, channels): super(UpConv2x2, self).__init__() self.conv = nn.Conv2d(channels, channels // 2, kernel_size=2, stride=1, padding=0, bias=True) def forward(self, x): x = F.interpolate(x, scale_factor=2, mode='nearest') x = F.pad(x, (0, 1, 0, 1)) x = self.conv(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn import torch.utils.data from torch.cuda import * assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused__unsafe_index_constant_pad_nd_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1296 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 9 % 9 x0 = xindex % 9 x2 = xindex // 81 x4 = xindex tmp0 = x1 tmp1 = tl.full([1], 8, tl.int64) tmp2 = tmp0 < tmp1 tmp3 = x0 tmp4 = tmp3 < tmp1 tmp5 = tmp2 & tmp4 tmp6 = tmp0.to(tl.float32) tmp7 = 0.5 tmp8 = tmp6 * tmp7 tmp9 = tmp8.to(tl.int32) tmp10 = tmp3.to(tl.float32) tmp11 = tmp10 * tmp7 tmp12 = tmp11.to(tl.int32) tmp13 = tl.load(in_ptr0 + (tmp12 + 4 * tmp9 + 16 * x2), tmp5 & xmask, eviction_policy='evict_last', other=0.0) tl.store(out_ptr0 + x4, tmp13, xmask) @triton.jit def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 64 % 2 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, (2, 4, 2, 2), (16, 4, 2, 1)) assert_size_stride(primals_3, (2,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 9, 9), (324, 81, 9, 1), torch.float32) get_raw_stream(0) triton_poi_fused__unsafe_index_constant_pad_nd_0[grid(1296)](primals_1, buf0, 1296, 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, 2, 8, 8), (128, 64, 8, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_1[grid(512)](buf2, primals_3, 512, XBLOCK=256, num_warps=4, num_stages=1) del primals_3 return buf2, primals_2, buf0 class UpConv2x2New(nn.Module): def __init__(self, channels): super(UpConv2x2New, self).__init__() self.conv = nn.Conv2d(channels, channels // 2, kernel_size=2, stride=1, padding=0, bias=True) 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]
jabae/detectEM
UpConv2x2
false
6,912
[ "MIT" ]
1
2d1a5116164d0bed0a8ea767a227d05a8970a448
https://github.com/jabae/detectEM/tree/2d1a5116164d0bed0a8ea767a227d05a8970a448
MultiheadAttention
import torch import numpy as np from typing import Optional import torch.nn as nn class MultiheadAttention(nn.Module): """Multihead scaled dot-product attention. """ def __init__(self, contexts: 'int', queries: 'int', channels: 'int', heads: 'int'): """Initializer. Args: contexts: size of the key, value channels. queries: size of the query channels. channels: size of the hidden channels. heads: the number of the attnetion heads. """ super().__init__() self.channels, self.heads = channels // heads, heads self.proj_key = nn.Linear(contexts, channels) self.proj_value = nn.Linear(contexts, channels) self.proj_query = nn.Linear(queries, channels) self.proj_out = nn.Linear(channels, channels) def forward(self, query: 'torch.Tensor', key: 'torch.Tensor', value: 'torch.Tensor', mask: 'Optional[torch.Tensor]'=None) ->torch.Tensor: """Transform the inputs. Args: query: [torch.float32; [B, S, queries]], query. key: [torch.float32; [B, T, contexts]], key. value: [torch.float32; [B, T, contexts]], value. mask: [torch.float32; [B, S, T]], attention mask. Returns: [torch.float32; [B, S, C]], attended. """ bsize, querylen, _ = query.shape keylen = key.shape[1] key = self.proj_key(key).view(bsize, keylen, self.heads, self.channels) value = self.proj_value(value).view(bsize, keylen, self.heads, self .channels) query = self.proj_query(query).view(bsize, querylen, self.heads, self.channels) score = torch.matmul(query.permute(0, 2, 1, 3), key.permute(0, 2, 3, 1) ) * self.channels ** -0.5 if mask is not None: score.masked_fill_(~mask[:, None, 0:1], -np.inf) weights = torch.softmax(score, dim=-1) out = torch.matmul(weights, value.transpose(1, 2)) out = self.proj_out(out.transpose(1, 2).reshape(bsize, querylen, -1)) if mask is not None: out = out * mask[..., 0:1] return out def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4]), torch.rand([4, 4, 4]) ] def get_init_inputs(): return [[], {'contexts': 4, 'queries': 4, 'channels': 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 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__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp3 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp1 = 1.0 tmp2 = tmp0 * tmp1 tmp4 = tmp3 * tmp1 tmp6 = tmp5 * tmp1 tmp7 = triton_helpers.maximum(tmp4, tmp6) tmp9 = tmp8 * tmp1 tmp10 = triton_helpers.maximum(tmp7, tmp9) tmp12 = tmp11 * tmp1 tmp13 = triton_helpers.maximum(tmp10, tmp12) tmp14 = tmp2 - tmp13 tmp15 = tmp14 * tmp1 tmp16 = tl_math.exp(tmp15) tl.store(out_ptr0 + x2, tmp16, xmask) @triton.jit def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_clone_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_add_4(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 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, primals_11) = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_3, (4, 4), (4, 1)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4, 4), (4, 1)) assert_size_stride(primals_6, (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, 4), (4, 1)) assert_size_stride(primals_11, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_2, (16, 4), (4, 1), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf0) del primals_3 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_7, (16, 4), (4, 1), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf1) del primals_5 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_8, (4, 4), (1, 4), 0), out=buf2) del primals_8 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_9, buf3, 16, 4, XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1) del primals_9 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_4, buf4, 16, 4, XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1) del primals_4 buf5 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf3, (16, 4, 1), (4, 1, 0), 0), reinterpret_tensor(buf4, (16, 1, 4), (4, 0, 1), 0), out=buf5) buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__softmax_1[grid(256)](buf5, buf6, 256, XBLOCK=256, num_warps=4, num_stages=1) buf7 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf5 triton_poi_fused__softmax_2[grid(256)](buf6, buf7, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf6 buf8 = reinterpret_tensor(buf0, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf0 triton_poi_fused_clone_0[grid(16, 4)](buf1, primals_6, buf8, 16, 4, XBLOCK=2, YBLOCK=16, num_warps=1, num_stages=1) del primals_6 buf9 = reinterpret_tensor(buf1, (16, 4, 1), (4, 1, 1), 0) del buf1 extern_kernels.bmm(reinterpret_tensor(buf7, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf8, (16, 4, 1), (4, 1, 0), 0), out=buf9) buf10 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused_clone_3[grid(16, 4)](buf9, buf10, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) buf11 = reinterpret_tensor(buf9, (16, 4), (4, 1), 0) del buf9 extern_kernels.mm(reinterpret_tensor(buf10, (16, 4), (4, 1), 0), reinterpret_tensor(primals_10, (4, 4), (1, 4), 0), out=buf11) buf12 = reinterpret_tensor(buf11, (4, 4, 4), (16, 4, 1), 0) del buf11 triton_poi_fused_add_4[grid(64)](buf12, primals_11, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_11 return buf12, reinterpret_tensor(primals_2, (16, 4), (4, 1), 0 ), reinterpret_tensor(primals_7, (16, 4), (4, 1), 0 ), reinterpret_tensor(primals_1, (16, 4), (4, 1), 0 ), buf7, reinterpret_tensor(buf10, (16, 4), (4, 1), 0 ), primals_10, reinterpret_tensor(buf8, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 4), 0) class MultiheadAttentionNew(nn.Module): """Multihead scaled dot-product attention. """ def __init__(self, contexts: 'int', queries: 'int', channels: 'int', heads: 'int'): """Initializer. Args: contexts: size of the key, value channels. queries: size of the query channels. channels: size of the hidden channels. heads: the number of the attnetion heads. """ super().__init__() self.channels, self.heads = channels // heads, heads self.proj_key = nn.Linear(contexts, channels) self.proj_value = nn.Linear(contexts, channels) self.proj_query = nn.Linear(queries, channels) self.proj_out = nn.Linear(channels, channels) def forward(self, input_0, input_1, input_2): primals_3 = self.proj_key.weight primals_4 = self.proj_key.bias primals_5 = self.proj_value.weight primals_6 = self.proj_value.bias primals_8 = self.proj_query.weight primals_9 = self.proj_query.bias primals_10 = self.proj_out.weight primals_11 = self.proj_out.bias primals_1 = input_0 primals_2 = input_1 primals_7 = input_2 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11]) return output[0]
ishine/torch-retriever-vc
MultiheadAttention
false
6,913
[ "MIT" ]
1
db5119d9d703ea819e2ac9185871ea3db52c14e1
https://github.com/ishine/torch-retriever-vc/tree/db5119d9d703ea819e2ac9185871ea3db52c14e1
Bilinear
import torch import torch.nn as nn class Bilinear(nn.Module): def __init__(self, size): super(Bilinear, self).__init__() self.size = size self.mat = nn.Parameter(torch.FloatTensor(self.size, self.size)) self.reset_parameters() def reset_parameters(self): params = [p for p in self.parameters() if p.requires_grad] for i, param in enumerate(params): param.data.normal_() def forward(self, vector1, vector2): bma = torch.matmul(vector1, self.mat).unsqueeze(1) ba = torch.matmul(bma, vector2.unsqueeze(2)).view(-1, 1) return ba def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clone_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 x0 = xindex % 64 x2 = xindex // 256 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tl.store(out_ptr0 + x3, tmp0, xmask) @triton.jit def triton_poi_fused_clone_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 16 x2 = xindex // 64 x3 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 16 * x2), xmask, eviction_policy= 'evict_last') tl.store(out_ptr0 + x3, tmp0, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_3, (4, 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_2, (64, 4), (4, 1), 0), primals_1, out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clone_0[grid(1024)](buf0, buf1, 1024, XBLOCK=128, num_warps=4, num_stages=1) del buf0 buf2 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1), torch.float32) triton_poi_fused_clone_1[grid(1024)](primals_3, buf2, 1024, XBLOCK= 256, num_warps=4, num_stages=1) del primals_3 buf3 = empty_strided_cuda((64, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf1, (64, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf2, (64, 4, 4), (16, 4, 1), 0), out=buf3) del buf1 return reinterpret_tensor(buf3, (1024, 1), (1, 1), 0), reinterpret_tensor( buf2, (64, 4, 4), (16, 1, 4), 0), reinterpret_tensor(primals_2, (4, 64), (1, 4), 0) class BilinearNew(nn.Module): def __init__(self, size): super(BilinearNew, self).__init__() self.size = size self.mat = nn.Parameter(torch.FloatTensor(self.size, self.size)) self.reset_parameters() def reset_parameters(self): params = [p for p in self.parameters() if p.requires_grad] for i, param in enumerate(params): param.data.normal_() def forward(self, input_0, input_1): primals_1 = self.mat primals_2 = input_0 primals_3 = input_1 output = call([primals_1, primals_2, primals_3]) return output[0]
jalshr21/diora-1
Bilinear
false
6,914
[ "Apache-2.0" ]
1
a9b680fde6a840707340e9e8232643b0f0e637bd
https://github.com/jalshr21/diora-1/tree/a9b680fde6a840707340e9e8232643b0f0e637bd
SigmoidFocalLoss
import torch import torch.nn as nn import torch.utils class SigmoidFocalLoss(nn.Module): def __init__(self, ignore_label, gamma=2.0, alpha=0.25, reduction='mean'): super(SigmoidFocalLoss, self).__init__() self.ignore_label = ignore_label self.gamma = gamma self.alpha = alpha self.reduction = reduction def forward(self, pred, target): b, _h, _w = target.size() pred = pred.view(b, -1, 1) pred_sigmoid = pred.sigmoid() target = target.view(b, -1).float() mask = target.ne(self.ignore_label).float() target = mask * target onehot = target.view(b, -1, 1) max_val = (-pred_sigmoid).clamp(min=0) pos_part = (1 - pred_sigmoid) ** self.gamma * (pred_sigmoid - pred_sigmoid * onehot) neg_part = pred_sigmoid ** self.gamma * (max_val + ((-max_val).exp( ) + (-pred_sigmoid - max_val).exp()).log()) loss = -(self.alpha * pos_part + (1 - self.alpha) * neg_part).sum(dim =-1) * mask if self.reduction == 'mean': loss = loss.mean() return loss def get_inputs(): return [torch.rand([4, 16]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'ignore_label': 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 import torch.utils assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused__to_copy_add_clamp_exp_log_mean_mul_ne_neg_pow_rsub_sigmoid_sub_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 tmp0 = tl.load(in_ptr0 + r0, None) tmp5 = tl.load(in_ptr1 + r0, None) tmp1 = tl.sigmoid(tmp0) tmp2 = 1.0 tmp3 = tmp2 - tmp1 tmp4 = tmp3 * tmp3 tmp6 = 4.0 tmp7 = tmp5 != tmp6 tmp8 = tmp7.to(tl.float32) tmp9 = tmp8 * tmp5 tmp10 = tmp1 * tmp9 tmp11 = tmp1 - tmp10 tmp12 = tmp4 * tmp11 tmp13 = 0.25 tmp14 = tmp12 * tmp13 tmp15 = tmp1 * tmp1 tmp16 = -tmp1 tmp17 = 0.0 tmp18 = triton_helpers.maximum(tmp16, tmp17) tmp19 = -tmp18 tmp20 = tl_math.exp(tmp19) tmp21 = tmp16 - tmp18 tmp22 = tl_math.exp(tmp21) tmp23 = tmp20 + tmp22 tmp24 = tl_math.log(tmp23) tmp25 = tmp18 + tmp24 tmp26 = tmp15 * tmp25 tmp27 = 0.75 tmp28 = tmp26 * tmp27 tmp29 = tmp14 + tmp28 tmp30 = -tmp29 tmp31 = tmp30 * tmp8 tmp32 = tl.broadcast_to(tmp31, [XBLOCK, RBLOCK]) tmp34 = tl.sum(tmp32, 1)[:, None] tmp35 = 64.0 tmp36 = tmp34 / tmp35 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp36, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(arg1_1, (4, 16), (16, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf1 = empty_strided_cuda((), (), torch.float32) buf2 = buf1 del buf1 get_raw_stream(0) triton_per_fused__to_copy_add_clamp_exp_log_mean_mul_ne_neg_pow_rsub_sigmoid_sub_sum_0[ grid(1)](buf2, arg1_1, arg0_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf2, class SigmoidFocalLossNew(nn.Module): def __init__(self, ignore_label, gamma=2.0, alpha=0.25, reduction='mean'): super(SigmoidFocalLossNew, self).__init__() self.ignore_label = ignore_label self.gamma = gamma self.alpha = alpha self.reduction = reduction def forward(self, input_0, input_1): arg1_1 = input_0 arg0_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
jameslong95/FasterSeg
SigmoidFocalLoss
false
6,915
[ "MIT" ]
1
872e04964ea46494a6018d9915cee5476e361c27
https://github.com/jameslong95/FasterSeg/tree/872e04964ea46494a6018d9915cee5476e361c27
ScaledDotProductAttention
import torch import numpy as np import torch.nn as nn class ScaledDotProductAttention(nn.Module): def __init__(self, dropout): super().__init__() self.dropout = nn.Dropout(dropout) def forward(self, q, k, v, mask=None, rpe_q=None, rpe_v=None): """ Args: q: query (*, query_len, dim) k: key (*, key_len, dim) v: value (*, key_len, dim) mask: (*, query_len, key_len), True will be masked out rpe_q : (query_len, key_len, dim) rpe_v : (query_len, key_len, dim) Returns: context: (*, query_len, dim) alignment: (*, query_len, key_len) """ dim = q.shape[-1] q /= dim ** 0.5 energy = q @ k.transpose(-2, -1) if rpe_q is not None: energy += torch.einsum('...qd,qkd->...qk', q, rpe_q) if mask is not None: energy = energy.masked_fill(mask, np.NINF) alignment = torch.softmax(energy, dim=-1) context = self.dropout(alignment) @ v if rpe_v is not None: context += torch.einsum('...qk,qkd->...qd', alignment, rpe_v) return context, alignment 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 [[], {'dropout': 0.5}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_div_0(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.5 tmp2 = tmp0 * tmp1 tl.store(out_ptr0 + x0, tmp2, xmask) tl.store(out_ptr1 + x0, tmp2, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): arg0_1, arg1_1, arg2_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_div_0[grid(256)](arg0_1, buf0, arg0_1, 256, XBLOCK =128, num_warps=4, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf0, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(arg1_1, (16, 4, 4), (16, 1, 4), 0), out=buf1 ) del arg1_1 buf2 = buf0 del buf0 triton_poi_fused__softmax_1[grid(256)](buf1, buf2, 256, XBLOCK=128, num_warps=4, num_stages=1) buf3 = reinterpret_tensor(buf1, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf1 triton_poi_fused__softmax_2[grid(256)](buf2, buf3, 256, XBLOCK=256, num_warps=4, num_stages=1) buf4 = reinterpret_tensor(buf2, (16, 4, 4), (16, 4, 1), 0) del buf2 extern_kernels.bmm(reinterpret_tensor(buf3, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(arg2_1, (16, 4, 4), (16, 4, 1), 0), out=buf4 ) del arg2_1 return reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0), buf3 class ScaledDotProductAttentionNew(nn.Module): def __init__(self, dropout): super().__init__() self.dropout = nn.Dropout(dropout) def forward(self, input_0, input_1, input_2): arg0_1 = input_0 arg1_1 = input_1 arg2_1 = input_2 output = call([arg0_1, arg1_1, arg2_1]) return output[0], output[1]
jahsylla/stochastic-cslr
ScaledDotProductAttention
false
6,916
[ "MIT" ]
1
d12d48ebec34183d939917cda2d54f38593dcddb
https://github.com/jahsylla/stochastic-cslr/tree/d12d48ebec34183d939917cda2d54f38593dcddb
JSloss
import torch import torch.nn as nn import torch.nn.functional as F class JSloss(nn.Module): """ Compute the Jensen-Shannon loss using the torch native kl_div""" def __init__(self, reduction='batchmean'): super().__init__() self.red = reduction def forward(self, input, target): net = (input + target) / 2.0 return 0.5 * (F.kl_div(input, net, reduction=self.red) + F.kl_div( target, net, reduction=self.red)) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_div_mul_sub_sum_xlogy_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.load(in_ptr1 + r0, None) tmp2 = tmp0 + tmp1 tmp3 = 0.5 tmp4 = tmp2 * tmp3 tmp5 = libdevice.isnan(tmp4).to(tl.int1) tmp6 = 0.0 tmp7 = tmp4 == tmp6 tmp8 = tl_math.log(tmp4) tmp9 = tmp4 * tmp8 tmp10 = tl.where(tmp7, tmp6, tmp9) tmp11 = float('nan') tmp12 = tl.where(tmp5, tmp11, tmp10) tmp13 = tmp4 * tmp0 tmp14 = tmp12 - tmp13 tmp15 = tl.broadcast_to(tmp14, [RBLOCK]) tmp17 = triton_helpers.promote_to_tensor(tl.sum(tmp15, 0)) tmp18 = tmp4 * tmp1 tmp19 = tmp12 - tmp18 tmp20 = tl.broadcast_to(tmp19, [RBLOCK]) tmp22 = triton_helpers.promote_to_tensor(tl.sum(tmp20, 0)) tmp23 = 0.25 tmp24 = tmp17 * tmp23 tmp25 = tmp22 * tmp23 tmp26 = tmp24 + tmp25 tmp27 = tmp26 * tmp3 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp27, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf2 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_div_mul_sub_sum_xlogy_0[grid(1)](buf2, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf2, class JSlossNew(nn.Module): """ Compute the Jensen-Shannon loss using the torch native kl_div""" def __init__(self, reduction='batchmean'): super().__init__() self.red = reduction def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
jaredaevans/UltrafastNST
JSloss
false
6,917
[ "MIT" ]
1
6671c6b618ce6bb4920b15f782be962e484a5423
https://github.com/jaredaevans/UltrafastNST/tree/6671c6b618ce6bb4920b15f782be962e484a5423
USConv2d
import torch import torch.nn as nn import torch.utils def make_divisible(v, divisor=8, min_value=1): """ forked from slim: https://github.com/tensorflow/models/blob/ 0344c5503ee55e24f0de7f37336a6e08f10976fd/ research/slim/nets/mobilenet/mobilenet.py#L62-L69 """ if min_value is None: min_value = divisor new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) if new_v < 0.9 * v: new_v += divisor return new_v class USConv2d(nn.Conv2d): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, depthwise=False, bias=True, width_mult_list=[1.0]): super(USConv2d, self).__init__(in_channels, out_channels, kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias) self.depthwise = depthwise self.in_channels_max = in_channels self.out_channels_max = out_channels self.width_mult_list = width_mult_list self.ratio = 1.0, 1.0 def set_ratio(self, ratio): self.ratio = ratio def forward(self, input): assert self.ratio[0] in self.width_mult_list, str(self.ratio[0] ) + ' in? ' + str(self.width_mult_list) self.in_channels = make_divisible(self.in_channels_max * self.ratio[0]) assert self.ratio[1] in self.width_mult_list, str(self.ratio[1] ) + ' in? ' + str(self.width_mult_list) self.out_channels = make_divisible(self.out_channels_max * self. ratio[1]) self.groups = self.in_channels if self.depthwise else 1 weight = self.weight[:self.out_channels, :self.in_channels, :, :] if self.bias is not None: bias = self.bias[:self.out_channels] else: bias = self.bias y = nn.functional.conv2d(input, weight, 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 import torch.nn as nn import torch.utils 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 = 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 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 1, 1), (4, 1, 1, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(16)](buf1, primals_2, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_2 return buf1, primals_3, primals_1 def make_divisible(v, divisor=8, min_value=1): """ forked from slim: https://github.com/tensorflow/models/blob/ 0344c5503ee55e24f0de7f37336a6e08f10976fd/ research/slim/nets/mobilenet/mobilenet.py#L62-L69 """ if min_value is None: min_value = divisor new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) if new_v < 0.9 * v: new_v += divisor return new_v class USConv2dNew(nn.Conv2d): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, depthwise=False, bias=True, width_mult_list=[1.0]): super(USConv2dNew, self).__init__(in_channels, out_channels, kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias) self.depthwise = depthwise self.in_channels_max = in_channels self.out_channels_max = out_channels self.width_mult_list = width_mult_list self.ratio = 1.0, 1.0 def set_ratio(self, ratio): self.ratio = ratio 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]
jameslong95/FasterSeg
USConv2d
false
6,918
[ "MIT" ]
1
872e04964ea46494a6018d9915cee5476e361c27
https://github.com/jameslong95/FasterSeg/tree/872e04964ea46494a6018d9915cee5476e361c27
GetStyleLoss
import torch import torch.nn as nn import torch.nn.functional as F def gram_matrix(input): """ gram matrix for feature assignments """ a, b, c, d = input.size() allG = [] for i in range(a): features = input[i].view(b, c * d) gram = torch.mm(features, features.t()) gram = gram.div(c * d) allG.append(gram) return torch.stack(allG) class GetStyleLoss(nn.Module): """ evaluate the style loss with gram matrix """ def forward(self, input, target): """ forward pass """ gram = gram_matrix(target) return F.mse_loss(gram, input) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_stack_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 x0 = xindex % 4 x2 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 4 * x1), tmp4 & xmask, other=0.0) tmp6 = 0.0625 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], 8, tl.int64) tmp12 = tmp0 < tmp11 tmp13 = tmp10 & tmp12 tmp14 = tl.load(in_ptr1 + (x0 + 4 * (-4 + x1)), tmp13 & xmask, other=0.0) tmp15 = tmp14 * tmp6 tmp16 = tl.full(tmp15.shape, 0.0, tmp15.dtype) tmp17 = tl.where(tmp13, tmp15, tmp16) tmp18 = tmp0 >= tmp11 tmp19 = tl.full([1], 12, tl.int64) tmp20 = tmp0 < tmp19 tmp21 = tmp18 & tmp20 tmp22 = tl.load(in_ptr2 + (x0 + 4 * (-8 + x1)), tmp21 & xmask, other=0.0) tmp23 = tmp22 * tmp6 tmp24 = tl.full(tmp23.shape, 0.0, tmp23.dtype) tmp25 = tl.where(tmp21, tmp23, tmp24) tmp26 = tmp0 >= tmp19 tl.full([1], 16, tl.int64) tmp29 = tl.load(in_ptr3 + (x0 + 4 * (-12 + x1)), tmp26 & xmask, other=0.0) tmp30 = tmp29 * tmp6 tmp31 = tl.full(tmp30.shape, 0.0, tmp30.dtype) tmp32 = tl.where(tmp26, tmp30, tmp31) tmp33 = tl.where(tmp21, tmp25, tmp32) tmp34 = tl.where(tmp13, tmp17, tmp33) tmp35 = tl.where(tmp4, tmp9, tmp34) tl.store(out_ptr0 + x2, tmp35, xmask) @triton.jit def triton_per_fused_mse_loss_1(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex % 64 r2 = rindex tmp0 = tl.load(in_ptr0 + r0, None, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + r2, None) tmp2 = tmp0 - tmp1 tmp3 = tmp2 * tmp2 tmp4 = tl.broadcast_to(tmp3, [RBLOCK]) tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0)) tmp7 = 256.0 tmp8 = tmp6 / tmp7 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp8, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(arg0_1, (4, 16), (16, 1), 0), reinterpret_tensor(arg0_1, (16, 4), (1, 16), 0), out=buf0) buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(arg0_1, (4, 16), (16, 1), 64), reinterpret_tensor(arg0_1, (16, 4), (1, 16), 64), out=buf1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(arg0_1, (4, 16), (16, 1), 128), reinterpret_tensor(arg0_1, (16, 4), (1, 16), 128), out=buf2) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(arg0_1, (4, 16), (16, 1), 192), reinterpret_tensor(arg0_1, (16, 4), (1, 16), 192), out=buf3) del arg0_1 buf4 = empty_strided_cuda((16, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_stack_0[grid(64)](buf0, buf1, buf2, buf3, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf0 del buf1 del buf2 del buf3 buf5 = empty_strided_cuda((), (), torch.float32) buf6 = buf5 del buf5 triton_per_fused_mse_loss_1[grid(1)](buf6, buf4, arg1_1, 1, 256, num_warps=2, num_stages=1) del arg1_1 del buf4 return buf6, def gram_matrix(input): """ gram matrix for feature assignments """ a, b, c, d = input.size() allG = [] for i in range(a): features = input[i].view(b, c * d) gram = torch.mm(features, features.t()) gram = gram.div(c * d) allG.append(gram) return torch.stack(allG) class GetStyleLossNew(nn.Module): """ evaluate the style loss with gram matrix """ def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
jaredaevans/UltrafastNST
GetStyleLoss
false
6,919
[ "MIT" ]
1
6671c6b618ce6bb4920b15f782be962e484a5423
https://github.com/jaredaevans/UltrafastNST/tree/6671c6b618ce6bb4920b15f782be962e484a5423
ConvBlock
import torch import torch.nn as nn import torch.nn.functional as F import torch.utils.data from torch.cuda import * def conv3x3(in_channels, out_channels): return nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=True) class ConvBlock(nn.Module): def __init__(self, in_channels, out_channels): """ Args: in_channels: number of channels in input (1st) feature map out_channels: number of channels in output feature maps """ super(ConvBlock, self).__init__() self.conv1 = conv3x3(in_channels, out_channels) self.conv2 = conv3x3(out_channels, out_channels) self.conv3 = conv3x3(out_channels, out_channels) self.norm = nn.BatchNorm2d(out_channels, track_running_stats=False) def forward(self, x): x = F.relu(self.norm(self.conv1(x))) x = F.relu(self.norm(self.conv2(x))) x = F.relu(self.norm(self.conv3(x))) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.utils.data from torch.cuda import * assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) @triton.jit def triton_per_fused__native_batch_norm_legit_1(in_ptr0, 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 % 16 r2 = rindex // 16 x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0 + 64 * r2), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tl.where(xmask, tmp1, 0) tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.full([XBLOCK, 1], 64, tl.int32) tmp9 = tmp8.to(tl.float32) tmp10 = tmp7 / tmp9 tmp11 = tmp1 - tmp10 tmp12 = tmp11 * tmp11 tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK]) tmp15 = tl.where(xmask, tmp13, 0) tmp16 = tl.sum(tmp15, 1)[:, None] tmp17 = 64.0 tmp18 = tmp16 / tmp17 tmp19 = 1e-05 tmp20 = tmp18 + tmp19 tmp21 = libdevice.rsqrt(tmp20) tl.store(out_ptr2 + x0, tmp21, xmask) tl.store(out_ptr0 + x0, tmp10, xmask) tl.store(out_ptr1 + x0, tmp16, xmask) @triton.jit def triton_poi_fused__native_batch_norm_legit_relu_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = 64.0 tmp5 = tmp3 / tmp4 tmp6 = 1e-05 tmp7 = tmp5 + tmp6 tmp8 = libdevice.rsqrt(tmp7) tmp9 = tmp2 * tmp8 tmp11 = tmp9 * tmp10 tmp13 = tmp11 + tmp12 tmp14 = tl.full([1], 0, tl.int32) tmp15 = triton_helpers.maximum(tmp14, tmp13) tl.store(out_ptr0 + x3, tmp15, xmask) @triton.jit def triton_poi_fused__native_batch_norm_legit_relu_threshold_backward_3(in_ptr0 , in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = 64.0 tmp5 = tmp3 / tmp4 tmp6 = 1e-05 tmp7 = tmp5 + tmp6 tmp8 = libdevice.rsqrt(tmp7) tmp9 = tmp2 * tmp8 tmp11 = tmp9 * tmp10 tmp13 = tmp11 + tmp12 tmp14 = tl.full([1], 0, tl.int32) tmp15 = triton_helpers.maximum(tmp14, tmp13) tmp16 = 0.0 tmp17 = tmp15 <= tmp16 tl.store(out_ptr0 + x3, tmp15, xmask) tl.store(out_ptr1 + x3, tmp17, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9) = args args.clear() assert_size_stride(primals_1, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_7, (4,), (1,)) assert_size_stride(primals_8, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_9, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(256)](buf1, primals_2, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32) buf3 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32) buf5 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32) triton_per_fused__native_batch_norm_legit_1[grid(4)](buf1, buf2, buf3, buf5, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__native_batch_norm_legit_relu_2[grid(256)](buf1, buf2, buf3, primals_4, primals_5, buf6, 256, XBLOCK=128, num_warps=4, 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, 4, 4, 4), (64, 16, 4, 1)) buf8 = buf7 del buf7 triton_poi_fused_convolution_0[grid(256)](buf8, primals_7, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 buf9 = buf3 del buf3 buf10 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32) buf12 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32) triton_per_fused__native_batch_norm_legit_1[grid(4)](buf8, buf9, buf10, buf12, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) buf13 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__native_batch_norm_legit_relu_2[grid(256)](buf8, buf9, buf10, primals_4, primals_5, buf13, 256, XBLOCK=128, num_warps=4, num_stages=1) buf14 = extern_kernels.convolution(buf13, primals_8, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf14, (4, 4, 4, 4), (64, 16, 4, 1)) buf15 = buf14 del buf14 triton_poi_fused_convolution_0[grid(256)](buf15, primals_9, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_9 buf16 = buf10 del buf10 buf17 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32) buf19 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32) triton_per_fused__native_batch_norm_legit_1[grid(4)](buf15, buf16, buf17, buf19, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) buf20 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf21 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) triton_poi_fused__native_batch_norm_legit_relu_threshold_backward_3[ grid(256)](buf15, buf16, buf17, primals_4, primals_5, buf20, buf21, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf17 del primals_5 return (buf20, primals_1, primals_3, primals_4, primals_6, primals_8, buf1, reinterpret_tensor(buf5, (4,), (1,), 0), buf6, buf8, reinterpret_tensor(buf12, (4,), (1,), 0), buf13, buf15, reinterpret_tensor(buf19, (4,), (1,), 0), buf21, reinterpret_tensor (buf16, (1, 4, 1, 1), (4, 1, 1, 1), 0), reinterpret_tensor(buf9, (1, 4, 1, 1), (4, 1, 1, 1), 0), reinterpret_tensor(buf2, (1, 4, 1, 1), (4, 1, 1, 1), 0)) def conv3x3(in_channels, out_channels): return nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=True) class ConvBlockNew(nn.Module): def __init__(self, in_channels, out_channels): """ Args: in_channels: number of channels in input (1st) feature map out_channels: number of channels in output feature maps """ super(ConvBlockNew, self).__init__() self.conv1 = conv3x3(in_channels, out_channels) self.conv2 = conv3x3(out_channels, out_channels) self.conv3 = conv3x3(out_channels, out_channels) self.norm = nn.BatchNorm2d(out_channels, track_running_stats=False) def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_6 = self.conv2.weight primals_4 = self.conv2.bias primals_8 = self.conv3.weight primals_5 = self.conv3.bias primals_7 = self.norm.weight primals_9 = self.norm.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]
jabae/detectEM
ConvBlock
false
6,920
[ "MIT" ]
1
2d1a5116164d0bed0a8ea767a227d05a8970a448
https://github.com/jabae/detectEM/tree/2d1a5116164d0bed0a8ea767a227d05a8970a448
BertAttention
from _paritybench_helpers import _mock_config import math import torch import torch.nn as nn class BertAttention(nn.Module): def __init__(self, config, ctx_dim=None): super().__init__() if config.hidden_size % config.num_attention_heads != 0: raise ValueError( 'The hidden size (%d) is not a multiple of the number of attention heads (%d)' % (config.hidden_size, config.num_attention_heads)) self.num_attention_heads = config.num_attention_heads self.attention_head_size = int(config.hidden_size / config. num_attention_heads) self.all_head_size = (self.num_attention_heads * self. attention_head_size) if ctx_dim is None: ctx_dim = config.hidden_size self.query = nn.Linear(config.hidden_size, self.all_head_size) self.key = nn.Linear(ctx_dim, self.all_head_size) self.value = nn.Linear(ctx_dim, self.all_head_size) self.dropout = nn.Dropout(config.attention_probs_dropout_prob) def transpose_for_scores(self, x): new_x_shape = x.size()[:-1] + (self.num_attention_heads, self. attention_head_size) x = x.view(*new_x_shape) return x.permute(0, 2, 1, 3) def forward(self, hidden_states, context, attention_mask=None): mixed_query_layer = self.query(hidden_states) mixed_key_layer = self.key(context) mixed_value_layer = self.value(context) query_layer = self.transpose_for_scores(mixed_query_layer) key_layer = self.transpose_for_scores(mixed_key_layer) value_layer = self.transpose_for_scores(mixed_value_layer) attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2)) attention_scores = attention_scores / math.sqrt(self. attention_head_size) if attention_mask is not None: attention_scores = attention_scores + attention_mask attention_probs = nn.Softmax(dim=-1)(attention_scores) attention_probs = self.dropout(attention_probs) context_layer = torch.matmul(attention_probs, value_layer) context_layer = context_layer.permute(0, 2, 1, 3).contiguous() new_context_layer_shape = context_layer.size()[:-2] + (self. all_head_size,) context_layer = context_layer.view(*new_context_layer_shape) return context_layer def get_inputs(): return [torch.rand([4, 4, 4]), torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'config': _mock_config(hidden_size=4, num_attention_heads= 4, attention_probs_dropout_prob=0.5)}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_0(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 1.0 tmp4 = tmp2 * tmp3 tl.store(out_ptr0 + (x2 + 4 * y3), tmp4, xmask & ymask) @triton.jit def triton_poi_fused_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 x2 = xindex tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp18 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp25 = tl.load(in_ptr1 + x2, xmask) tmp26 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last') tmp27 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp29 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp31 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp1 = float('-inf') tmp2 = tmp0 == tmp1 tmp3 = tmp2 == 0 tmp4 = tmp3.to(tl.int64) tmp5 = tmp4 != 0 tmp7 = tmp6 == tmp1 tmp8 = tmp7 == 0 tmp9 = tmp8.to(tl.int64) tmp10 = tmp9 != 0 tmp11 = tmp5 | tmp10 tmp13 = tmp12 == tmp1 tmp14 = tmp13 == 0 tmp15 = tmp14.to(tl.int64) tmp16 = tmp15 != 0 tmp17 = tmp11 | tmp16 tmp19 = tmp18 == tmp1 tmp20 = tmp19 == 0 tmp21 = tmp20.to(tl.int64) tmp22 = tmp21 != 0 tmp23 = tmp17 | tmp22 tmp24 = tmp23 == 0 tmp28 = tmp26 + tmp27 tmp30 = tmp28 + tmp29 tmp32 = tmp30 + tmp31 tmp33 = tmp25 / tmp32 tmp34 = 0.0 tmp35 = tl.where(tmp24, tmp34, tmp33) tl.store(out_ptr0 + x2, tmp35, xmask) @triton.jit def triton_poi_fused_3(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask) @triton.jit def triton_poi_fused_clone_4(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8) = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_7, (4, 4), (4, 1)) assert_size_stride(primals_8, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (16, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_6, (16, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf1) del primals_4 buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_6, (16, 4), (4, 1), 0), reinterpret_tensor(primals_7, (4, 4), (1, 4), 0), out=buf2) del primals_7 buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_0[grid(16, 4)](buf0, primals_2, buf3, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_2 buf4 = reinterpret_tensor(buf0, (4, 4, 1, 4), (16, 4, 4, 1), 0) del buf0 triton_poi_fused_0[grid(16, 4)](buf1, primals_5, buf4, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1, num_stages=1) del primals_5 buf5 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf3, (16, 4, 1), (4, 1, 0), 0), reinterpret_tensor(buf4, (16, 1, 4), (4, 0, 1), 0), out=buf5) buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_1[grid(256)](buf5, buf6, 256, XBLOCK=128, num_warps=4, num_stages=1) buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_2[grid(256)](buf5, buf6, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf5 del buf6 buf8 = reinterpret_tensor(buf1, (4, 4, 4, 1), (16, 4, 1, 1), 0) del buf1 triton_poi_fused_3[grid(16, 4)](buf2, primals_8, buf8, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) del primals_8 buf9 = reinterpret_tensor(buf2, (16, 4, 1), (4, 1, 1), 0) del buf2 extern_kernels.bmm(reinterpret_tensor(buf7, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf8, (16, 4, 1), (4, 1, 0), 0), out=buf9) buf10 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32) triton_poi_fused_clone_4[grid(16, 4)](buf9, buf10, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1) del buf9 return reinterpret_tensor(buf10, (4, 4, 4), (16, 4, 1), 0 ), reinterpret_tensor(primals_3, (16, 4), (4, 1), 0 ), reinterpret_tensor(primals_6, (16, 4), (4, 1), 0 ), buf7, reinterpret_tensor(buf8, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf3, (16, 1, 4), (4, 1, 1), 0 ), reinterpret_tensor(buf4, (16, 4, 1), (4, 1, 4), 0) class BertAttentionNew(nn.Module): def __init__(self, config, ctx_dim=None): super().__init__() if config.hidden_size % config.num_attention_heads != 0: raise ValueError( 'The hidden size (%d) is not a multiple of the number of attention heads (%d)' % (config.hidden_size, config.num_attention_heads)) self.num_attention_heads = config.num_attention_heads self.attention_head_size = int(config.hidden_size / config. num_attention_heads) self.all_head_size = (self.num_attention_heads * self. attention_head_size) if ctx_dim is None: ctx_dim = config.hidden_size self.query = nn.Linear(config.hidden_size, self.all_head_size) self.key = nn.Linear(ctx_dim, self.all_head_size) self.value = nn.Linear(ctx_dim, self.all_head_size) self.dropout = nn.Dropout(config.attention_probs_dropout_prob) def transpose_for_scores(self, x): new_x_shape = x.size()[:-1] + (self.num_attention_heads, self. attention_head_size) x = x.view(*new_x_shape) return x.permute(0, 2, 1, 3) def forward(self, input_0, input_1): primals_1 = self.query.weight primals_2 = self.query.bias primals_4 = self.key.weight primals_5 = self.key.bias primals_7 = self.value.weight primals_8 = self.value.bias primals_3 = input_0 primals_6 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8]) return output[0]
AsmitaBhat30/lxmert
BertAttention
false
6,921
[ "MIT" ]
1
90292dc36a25c04c4f76fe9119e3141d5dc05874
https://github.com/AsmitaBhat30/lxmert/tree/90292dc36a25c04c4f76fe9119e3141d5dc05874
TVLoss
import torch import torch.nn as nn import torch.nn.functional as F class TVLoss(nn.Module): """L2 total variation loss, as in Mahendran et al.""" def forward(self, input): input = F.pad(input, (0, 1, 0, 1), 'replicate') x_diff = input[..., :-1, 1:] - input[..., :-1, :-1] y_diff = input[..., 1:, :-1] - input[..., :-1, :-1] return (x_diff ** 2 + y_diff ** 2).mean() def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_mean_pow_sub_0(in_out_ptr0, in_ptr0, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex % 4 r1 = rindex // 4 % 4 r2 = rindex // 16 tmp0 = tl.load(in_ptr0 + (4 * (3 * (3 <= r1) + r1 * (r1 < 3)) + 16 * r2 + (3 * (3 <= 1 + r0) + (1 + r0) * (1 + r0 < 3))), None) tmp1 = tl.load(in_ptr0 + (4 * (3 * (3 <= r1) + r1 * (r1 < 3)) + 16 * r2 + (3 * (3 <= r0) + r0 * (r0 < 3))), None) tmp4 = tl.load(in_ptr0 + (4 * (3 * (3 <= 1 + r1) + (1 + r1) * (1 + r1 < 3)) + 16 * r2 + (3 * (3 <= r0) + r0 * (r0 < 3))), None) tmp2 = tmp0 - tmp1 tmp3 = tmp2 * tmp2 tmp5 = tmp4 - tmp1 tmp6 = tmp5 * tmp5 tmp7 = tmp3 + tmp6 tmp8 = tl.broadcast_to(tmp7, [RBLOCK]) tmp10 = triton_helpers.promote_to_tensor(tl.sum(tmp8, 0)) tmp11 = 256.0 tmp12 = tmp10 / tmp11 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp12, None) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf1 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_mean_pow_sub_0[grid(1)](buf1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 return buf1, class TVLossNew(nn.Module): """L2 total variation loss, as in Mahendran et al.""" def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
jayChung0302/SideProject-sudalchongbo
TVLoss
false
6,922
[ "MIT" ]
1
fb0a3d0aee53ba24d3b8ec2dd8c52d0e8f6c33d7
https://github.com/jayChung0302/SideProject-sudalchongbo/tree/fb0a3d0aee53ba24d3b8ec2dd8c52d0e8f6c33d7
FocalLoss
import torch import torch.nn as nn import torch.nn.functional as F class FocalSigmoidLossFunc(torch.autograd.Function): """ compute backward directly for better numeric stability """ @staticmethod def forward(ctx, logits, label, alpha, gamma): logits = logits.float() coeff = torch.empty_like(logits).fill_(1 - alpha) coeff[label == 1] = alpha probs = torch.sigmoid(logits) log_probs = torch.where(logits >= 0, F.softplus(logits, -1, 50), logits - F.softplus(logits, 1, 50)) log_1_probs = torch.where(logits >= 0, -logits + F.softplus(logits, -1, 50), -F.softplus(logits, 1, 50)) probs_gamma = probs ** gamma probs_1_gamma = (1.0 - probs) ** gamma ctx.coeff = coeff ctx.probs = probs ctx.log_probs = log_probs ctx.log_1_probs = log_1_probs ctx.probs_gamma = probs_gamma ctx.probs_1_gamma = probs_1_gamma ctx.label = label ctx.gamma = gamma term1 = probs_1_gamma * log_probs term2 = probs_gamma * log_1_probs loss = torch.where(label == 1, term1, term2).mul_(coeff).neg_() return loss @staticmethod def backward(ctx, grad_output): """ compute gradient of focal loss """ coeff = ctx.coeff probs = ctx.probs log_probs = ctx.log_probs log_1_probs = ctx.log_1_probs probs_gamma = ctx.probs_gamma probs_1_gamma = ctx.probs_1_gamma label = ctx.label gamma = ctx.gamma term1 = (1.0 - probs - gamma * probs * log_probs).mul_(probs_1_gamma ).neg_() term2 = (probs - gamma * (1.0 - probs) * log_1_probs).mul_(probs_gamma) grads = torch.where(label == 1, term1, term2).mul_(coeff).mul_( grad_output) return grads, None, None, None class FocalLoss(nn.Module): """ This use better formula to compute the gradient, which has better numeric stability """ def __init__(self, alpha=1, gamma=2, reduction='mean'): super().__init__() self.alpha = alpha self.gamma = gamma self.reduction = reduction def forward(self, logits, label): loss = FocalSigmoidLossFunc.apply(logits, label, self.alpha, self.gamma ) if self.reduction == 'mean': loss = loss.mean() if self.reduction == 'sum': loss = loss.sum() return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn import torch.nn.functional as F assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_eq_fill_ge_index_put_lift_fresh_mean_mul_neg_pow_rsub_sigmoid_softplus_sub_where_0( in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp3 = tl.load(in_ptr1 + r0, None) tmp1 = 1.0 tmp2 = tmp0 == tmp1 tmp4 = tl.sigmoid(tmp3) tmp5 = tmp1 - tmp4 tmp6 = tmp5 * tmp5 tmp7 = 0.0 tmp8 = tmp3 >= tmp7 tmp9 = -1.0 tmp10 = tmp3 * tmp9 tmp11 = 50.0 tmp12 = tmp10 > tmp11 tmp13 = tl_math.exp(tmp10) tmp14 = libdevice.log1p(tmp13) tmp15 = tmp14 * tmp9 tmp16 = tl.where(tmp12, tmp3, tmp15) tmp17 = tmp3 * tmp1 tmp18 = tmp17 > tmp11 tmp19 = tl_math.exp(tmp17) tmp20 = libdevice.log1p(tmp19) tmp21 = tmp20 * tmp1 tmp22 = tl.where(tmp18, tmp3, tmp21) tmp23 = tmp3 - tmp22 tmp24 = tl.where(tmp8, tmp16, tmp23) tmp25 = tmp6 * tmp24 tmp26 = tmp4 * tmp4 tmp27 = -tmp3 tmp28 = tmp27 + tmp16 tmp29 = -tmp22 tmp30 = tl.where(tmp8, tmp28, tmp29) tmp31 = tmp26 * tmp30 tmp32 = tl.where(tmp2, tmp25, tmp31) tmp33 = tl.where(tmp2, tmp1, tmp7) tmp34 = tmp32 * tmp33 tmp35 = -tmp34 tmp36 = tl.broadcast_to(tmp35, [RBLOCK]) tmp38 = triton_helpers.promote_to_tensor(tl.sum(tmp36, 0)) tmp39 = 256.0 tmp40 = tmp38 / tmp39 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp40, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf2 = empty_strided_cuda((), (), torch.float32) buf3 = buf2 del buf2 get_raw_stream(0) triton_per_fused_add_eq_fill_ge_index_put_lift_fresh_mean_mul_neg_pow_rsub_sigmoid_softplus_sub_where_0[ grid(1)](buf3, arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1) del arg0_1 del arg1_1 return buf3, class FocalSigmoidLossFunc(torch.autograd.Function): """ compute backward directly for better numeric stability """ @staticmethod def forward(ctx, logits, label, alpha, gamma): logits = logits.float() coeff = torch.empty_like(logits).fill_(1 - alpha) coeff[label == 1] = alpha probs = torch.sigmoid(logits) log_probs = torch.where(logits >= 0, F.softplus(logits, -1, 50), logits - F.softplus(logits, 1, 50)) log_1_probs = torch.where(logits >= 0, -logits + F.softplus(logits, -1, 50), -F.softplus(logits, 1, 50)) probs_gamma = probs ** gamma probs_1_gamma = (1.0 - probs) ** gamma ctx.coeff = coeff ctx.probs = probs ctx.log_probs = log_probs ctx.log_1_probs = log_1_probs ctx.probs_gamma = probs_gamma ctx.probs_1_gamma = probs_1_gamma ctx.label = label ctx.gamma = gamma term1 = probs_1_gamma * log_probs term2 = probs_gamma * log_1_probs loss = torch.where(label == 1, term1, term2).mul_(coeff).neg_() return loss @staticmethod def backward(ctx, grad_output): """ compute gradient of focal loss """ coeff = ctx.coeff probs = ctx.probs log_probs = ctx.log_probs log_1_probs = ctx.log_1_probs probs_gamma = ctx.probs_gamma probs_1_gamma = ctx.probs_1_gamma label = ctx.label gamma = ctx.gamma term1 = (1.0 - probs - gamma * probs * log_probs).mul_(probs_1_gamma ).neg_() term2 = (probs - gamma * (1.0 - probs) * log_1_probs).mul_(probs_gamma) grads = torch.where(label == 1, term1, term2).mul_(coeff).mul_( grad_output) return grads, None, None, None class FocalLossNew(nn.Module): """ This use better formula to compute the gradient, which has better numeric stability """ def __init__(self, alpha=1, gamma=2, reduction='mean'): super().__init__() self.alpha = alpha self.gamma = gamma self.reduction = reduction def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
jaredaevans/UltrafastNST
FocalLoss
false
6,923
[ "MIT" ]
1
6671c6b618ce6bb4920b15f782be962e484a5423
https://github.com/jaredaevans/UltrafastNST/tree/6671c6b618ce6bb4920b15f782be962e484a5423
LogisticRegression
import torch import torch.nn as nn class LogisticRegression(nn.Module): """ A logistic regression model of the form P(y = 1 | x) = 1 / (1 + exp(-(mx + b))) """ def __init__(self, init_m=1.0, init_b=1.0): """ Initialize a logistic regression model by defining its initial parameters. The ``nn.Parameter`` wrapper is needed if we intend to use vanilla ``torch.Tensors`` as parameters of this module, so that this object knows it has trainable parameters (e.g. when calling the ``.parameters()`` method on this object, or moving the model to GPU via ``.cuda``). If we assign parameters which are already subclasses of ``nn.Module`` (e.g. the ``nn.Linear`` layer), ``nn.Parameter`` is not needed. Parameters ---------- init_m : ``float``, optional (defualt: 1.0) Initial estimate for ``m``, the slope of the model init_b : ``float``, optional (default: 1.0) Initial estimate for ``b``, the y-intercept of the model """ super(LogisticRegression, self).__init__() self.m = nn.Parameter(torch.tensor(init_m)) self.b = nn.Parameter(torch.tensor(init_b)) def forward(self, x): """ Forward pass through the model, which produces a prediction ``\\hat{y}`` for each given ``x`` in the batch. Parameters ---------- x : ``torch.Tensor`` of shape ``(batch_size, )`` The single input. Returns ------- scores : ``torch.Tensor`` of shape ``(batch_size, )`` The raw logits of the predicted output """ scores = self.m * x + self.b return scores 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_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) tmp4 = tl.load(in_ptr2 + 0) tmp5 = tl.broadcast_to(tmp4, [XBLOCK]) tmp3 = tmp1 * tmp2 tmp6 = tmp3 + tmp5 tl.store(out_ptr0 + x0, tmp6, xmask) def call(args): primals_1, primals_2, primals_3 = 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, (), ()) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_mul_0[grid(256)](primals_1, primals_2, primals_3, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_1 del primals_3 return buf0, primals_2 class LogisticRegressionNew(nn.Module): """ A logistic regression model of the form P(y = 1 | x) = 1 / (1 + exp(-(mx + b))) """ def __init__(self, init_m=1.0, init_b=1.0): """ Initialize a logistic regression model by defining its initial parameters. The ``nn.Parameter`` wrapper is needed if we intend to use vanilla ``torch.Tensors`` as parameters of this module, so that this object knows it has trainable parameters (e.g. when calling the ``.parameters()`` method on this object, or moving the model to GPU via ``.cuda``). If we assign parameters which are already subclasses of ``nn.Module`` (e.g. the ``nn.Linear`` layer), ``nn.Parameter`` is not needed. Parameters ---------- init_m : ``float``, optional (defualt: 1.0) Initial estimate for ``m``, the slope of the model init_b : ``float``, optional (default: 1.0) Initial estimate for ``b``, the y-intercept of the model """ super(LogisticRegressionNew, self).__init__() self.m = nn.Parameter(torch.tensor(init_m)) self.b = nn.Parameter(torch.tensor(init_b)) def forward(self, input_0): primals_1 = self.m primals_3 = self.b primals_2 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
jayelm/pytorch-project-template
LogisticRegression
false
6,924
[ "MIT" ]
1
30306ce07b21c97c6993432764cbbe0a73092a0c
https://github.com/jayelm/pytorch-project-template/tree/30306ce07b21c97c6993432764cbbe0a73092a0c
DownConvBlock
import torch import torch.nn as nn import torch.nn.functional as F import torch.utils.data from torch.cuda import * def conv3x3(in_channels, out_channels): return nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=True) def maxpool2x2(): return nn.MaxPool2d(kernel_size=2, stride=2, padding=0) class DownConvBlock(nn.Module): def __init__(self, in_channels, out_channels): """ Args: in_channels: number of channels in input (1st) feature map out_channels: number of channels in output feature maps """ super(DownConvBlock, self).__init__() self.maxpool = maxpool2x2() self.conv1 = conv3x3(in_channels, out_channels) self.conv2 = conv3x3(out_channels, out_channels) self.conv3 = conv3x3(out_channels, out_channels) self.norm = nn.BatchNorm2d(out_channels, track_running_stats=False) def forward(self, x): x = self.maxpool(x) x = F.relu(self.norm(self.conv1(x))) x = F.relu(self.norm(self.conv2(x))) x = F.relu(self.norm(self.conv3(x))) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'in_channels': 4, 'out_channels': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.utils.data from torch.cuda import * assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_max_pool2d_with_indices_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 2 x1 = xindex // 2 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (4 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr0 + (5 + 2 * x0 + 8 * x1), xmask, eviction_policy= 'evict_last') tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp4 = triton_helpers.maximum(tmp3, tmp2) tmp6 = triton_helpers.maximum(tmp5, tmp4) tl.store(out_ptr0 + x2, tmp6, xmask) @triton.jit def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 4 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) @triton.jit def triton_per_fused__native_batch_norm_legit_2(in_ptr0, 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 % 4 r2 = rindex // 4 x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 4 * x0 + 16 * r2), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tl.where(xmask, tmp1, 0) tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.full([XBLOCK, 1], 16, tl.int32) tmp9 = tmp8.to(tl.float32) tmp10 = tmp7 / tmp9 tmp11 = tmp1 - tmp10 tmp12 = tmp11 * tmp11 tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK]) tmp15 = tl.where(xmask, tmp13, 0) tmp16 = tl.sum(tmp15, 1)[:, None] tmp17 = 16.0 tmp18 = tmp16 / tmp17 tmp19 = 1e-05 tmp20 = tmp18 + tmp19 tmp21 = libdevice.rsqrt(tmp20) tl.store(out_ptr2 + x0, tmp21, xmask) tl.store(out_ptr0 + x0, tmp10, xmask) tl.store(out_ptr1 + x0, tmp16, xmask) @triton.jit def triton_poi_fused__native_batch_norm_legit_relu_3(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 x3 = xindex x1 = xindex // 4 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = 16.0 tmp5 = tmp3 / tmp4 tmp6 = 1e-05 tmp7 = tmp5 + tmp6 tmp8 = libdevice.rsqrt(tmp7) tmp9 = tmp2 * tmp8 tmp11 = tmp9 * tmp10 tmp13 = tmp11 + tmp12 tmp14 = tl.full([1], 0, tl.int32) tmp15 = triton_helpers.maximum(tmp14, tmp13) tl.store(out_ptr0 + x3, tmp15, xmask) @triton.jit def triton_poi_fused__native_batch_norm_legit_relu_threshold_backward_4(in_ptr0 , in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 4 % 4 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = 16.0 tmp5 = tmp3 / tmp4 tmp6 = 1e-05 tmp7 = tmp5 + tmp6 tmp8 = libdevice.rsqrt(tmp7) tmp9 = tmp2 * tmp8 tmp11 = tmp9 * tmp10 tmp13 = tmp11 + tmp12 tmp14 = tl.full([1], 0, tl.int32) tmp15 = triton_helpers.maximum(tmp14, tmp13) tmp16 = 0.0 tmp17 = tmp15 <= tmp16 tl.store(out_ptr0 + x3, tmp15, xmask) tl.store(out_ptr1 + x3, tmp17, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9) = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (4,), (1,)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_7, (4,), (1,)) assert_size_stride(primals_8, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_9, (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_max_pool2d_with_indices_0[grid(64)](primals_1, buf0, 64, XBLOCK=64, num_warps=1, 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, 4, 2, 2), (16, 4, 2, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_1[grid(64)](buf2, primals_3, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_3 buf3 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32) buf4 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32) buf6 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32) triton_per_fused__native_batch_norm_legit_2[grid(4)](buf2, buf3, buf4, buf6, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) buf7 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32) triton_poi_fused__native_batch_norm_legit_relu_3[grid(64)](buf2, buf3, buf4, primals_4, primals_5, buf7, 64, XBLOCK=64, num_warps=1, num_stages=1) buf8 = extern_kernels.convolution(buf7, primals_6, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf8, (4, 4, 2, 2), (16, 4, 2, 1)) buf9 = buf8 del buf8 triton_poi_fused_convolution_1[grid(64)](buf9, primals_7, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_7 buf10 = buf4 del buf4 buf11 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32) buf13 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32) triton_per_fused__native_batch_norm_legit_2[grid(4)](buf9, buf10, buf11, buf13, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) buf14 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32) triton_poi_fused__native_batch_norm_legit_relu_3[grid(64)](buf9, buf10, buf11, primals_4, primals_5, buf14, 64, XBLOCK=64, num_warps=1, num_stages=1) buf15 = extern_kernels.convolution(buf14, primals_8, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf15, (4, 4, 2, 2), (16, 4, 2, 1)) buf16 = buf15 del buf15 triton_poi_fused_convolution_1[grid(64)](buf16, primals_9, 64, XBLOCK=64, num_warps=1, num_stages=1) del primals_9 buf17 = buf11 del buf11 buf18 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32) buf20 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 4, 4), torch.float32) triton_per_fused__native_batch_norm_legit_2[grid(4)](buf16, buf17, buf18, buf20, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) buf21 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32) buf22 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.bool) triton_poi_fused__native_batch_norm_legit_relu_threshold_backward_4[ grid(64)](buf16, buf17, buf18, primals_4, primals_5, buf21, buf22, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf18 del primals_5 return (buf21, primals_2, primals_4, primals_6, primals_8, buf0, buf2, reinterpret_tensor(buf6, (4,), (1,), 0), buf7, buf9, reinterpret_tensor(buf13, (4,), (1,), 0), buf14, buf16, reinterpret_tensor(buf20, (4,), (1,), 0), buf22, reinterpret_tensor (buf17, (1, 4, 1, 1), (4, 1, 1, 1), 0), reinterpret_tensor(buf10, ( 1, 4, 1, 1), (4, 1, 1, 1), 0), reinterpret_tensor(buf3, (1, 4, 1, 1 ), (4, 1, 1, 1), 0)) def conv3x3(in_channels, out_channels): return nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=True) def maxpool2x2(): return nn.MaxPool2d(kernel_size=2, stride=2, padding=0) class DownConvBlockNew(nn.Module): def __init__(self, in_channels, out_channels): """ Args: in_channels: number of channels in input (1st) feature map out_channels: number of channels in output feature maps """ super(DownConvBlockNew, self).__init__() self.maxpool = maxpool2x2() self.conv1 = conv3x3(in_channels, out_channels) self.conv2 = conv3x3(out_channels, out_channels) self.conv3 = conv3x3(out_channels, out_channels) self.norm = nn.BatchNorm2d(out_channels, track_running_stats=False) def forward(self, input_0): primals_2 = self.conv1.weight primals_3 = self.conv1.bias primals_6 = self.conv2.weight primals_4 = self.conv2.bias primals_8 = self.conv3.weight primals_5 = self.conv3.bias primals_7 = self.norm.weight primals_9 = self.norm.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9]) return output[0]
jabae/detectEM
DownConvBlock
false
6,925
[ "MIT" ]
1
2d1a5116164d0bed0a8ea767a227d05a8970a448
https://github.com/jabae/detectEM/tree/2d1a5116164d0bed0a8ea767a227d05a8970a448
VariationalLoss
import torch import torch.nn as nn import torch.nn.functional as F class VariationalLoss(nn.Module): """ Variational loss to enforce continuity of images """ def forward(self, input): """ forward pass """ self.loss = F.mse_loss(input[:, :, 1:, :], input[:, :, :-1, :] ) + F.mse_loss(input[:, :, :, 1:], input[:, :, :, :-1]) return input def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_mse_loss_0(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): rnumel = 192 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] rmask = rindex < rnumel r0 = rindex % 12 r1 = rindex // 12 r2 = rindex % 3 r3 = rindex // 3 tmp0 = tl.load(in_ptr0 + (4 + r0 + 16 * r1), rmask, other=0.0) tmp1 = tl.load(in_ptr0 + (r0 + 16 * r1), rmask, other=0.0) tmp8 = tl.load(in_ptr0 + (1 + r2 + 4 * r3), rmask, other=0.0) tmp9 = tl.load(in_ptr0 + (r2 + 4 * r3), rmask, other=0.0) tmp2 = tmp0 - tmp1 tmp3 = tmp2 * tmp2 tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.where(rmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp10 = tmp8 - tmp9 tmp11 = tmp10 * tmp10 tmp12 = tl.broadcast_to(tmp11, [XBLOCK, RBLOCK]) tmp14 = tl.where(rmask, tmp12, 0) tmp15 = tl.sum(tmp14, 1)[:, None] tmp16 = 192.0 tmp17 = tmp7 / tmp16 tmp18 = tmp15 / tmp16 tmp19 = tmp17 + tmp18 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp19, None) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((), (), torch.float32) buf2 = buf0 del buf0 get_raw_stream(0) triton_per_fused_add_mse_loss_0[grid(1)](buf2, arg0_1, 1, 192, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 return buf2, class VariationalLossNew(nn.Module): """ Variational loss to enforce continuity of images """ def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
jaredaevans/UltrafastNST
VariationalLoss
false
6,926
[ "MIT" ]
1
6671c6b618ce6bb4920b15f782be962e484a5423
https://github.com/jaredaevans/UltrafastNST/tree/6671c6b618ce6bb4920b15f782be962e484a5423
TwoHiddenLayerFc
import torch import torch.nn as nn import torch.nn.functional as F class TwoHiddenLayerFc(nn.Module): def __init__(self, input_shape, out_dim): super(TwoHiddenLayerFc, self).__init__() self.fc1 = nn.Linear(input_shape, 200) self.fc2 = nn.Linear(200, 200) self.fc3 = nn.Linear(200, out_dim) def forward(self, x): out = F.relu(self.fc1(x)) out = F.relu(self.fc2(out)) out = self.fc3(out) return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_shape': 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 assert_size_stride = torch._C._dynamo.guards.assert_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) 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, (200, 200), (200, 1)) assert_size_stride(primals_5, (200,), (1,)) assert_size_stride(primals_6, (4, 200), (200, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 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 buf6 = 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, buf6, 12800, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf2 = empty_strided_cuda((64, 200), (200, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 200), (200, 1), 0), reinterpret_tensor(primals_4, (200, 200), (1, 200), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 200), (3200, 800, 200, 1), 0) del buf2 buf5 = empty_strided_cuda((4, 4, 4, 200), (3200, 800, 200, 1), torch.bool) triton_poi_fused_relu_threshold_backward_0[grid(12800)](buf3, primals_5, buf5, 12800, XBLOCK=128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 200), (200, 1), 0), reinterpret_tensor(primals_6, (200, 4), (1, 200), 0), alpha=1, beta=1, out=buf4) del primals_7 return reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), reinterpret_tensor(buf1, (64, 200), (200, 1), 0 ), reinterpret_tensor(buf3, (64, 200), (200, 1), 0 ), primals_6, buf5, primals_4, buf6 class TwoHiddenLayerFcNew(nn.Module): def __init__(self, input_shape, out_dim): super(TwoHiddenLayerFcNew, self).__init__() self.fc1 = nn.Linear(input_shape, 200) self.fc2 = nn.Linear(200, 200) self.fc3 = nn.Linear(200, out_dim) def forward(self, input_0): primals_1 = self.fc1.weight primals_2 = self.fc1.bias primals_4 = self.fc2.weight primals_5 = self.fc2.bias primals_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]
jasonyanglu/fedavgpy
TwoHiddenLayerFc
false
6,927
[ "MIT" ]
1
cefbe5854f02d3df1197d849872286439c86e949
https://github.com/jasonyanglu/fedavgpy/tree/cefbe5854f02d3df1197d849872286439c86e949
SoftCrossEntropyLoss2d
import torch import torch.nn as nn import torch.nn.functional as F import torch.utils class SoftCrossEntropyLoss2d(nn.Module): def __init__(self): super(SoftCrossEntropyLoss2d, self).__init__() def forward(self, inputs, targets): loss = 0 inputs = -F.log_softmax(inputs, dim=1) for index in range(inputs.size()[0]): loss += F.conv2d(inputs[range(index, index + 1)], targets[range (index, index + 1)]) / (targets.size()[2] * targets.size()[3]) 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 from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn import torch.utils assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused__log_softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tl.store(out_ptr0 + x3, tmp8, xmask) @triton.jit def triton_poi_fused__log_softmax_neg_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl_math.exp(tmp1) tmp4 = tl_math.exp(tmp3) tmp5 = tmp2 + tmp4 tmp7 = tl_math.exp(tmp6) tmp8 = tmp5 + tmp7 tmp10 = tl_math.exp(tmp9) tmp11 = tmp8 + tmp10 tmp12 = tl_math.log(tmp11) tmp13 = tmp0 - tmp12 tmp14 = -tmp13 tl.store(out_ptr0 + x3, tmp14, xmask) @triton.jit def triton_poi_fused_index_2(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 + (x1 + 16 * y0), xmask & ymask, eviction_policy ='evict_last') tl.store(out_ptr0 + (y0 + 4 * x1), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_index_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl. constexpr, XBLOCK: tl.constexpr): ynumel = 4 xnumel = 16 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x1 = xindex y0 = yindex tmp0 = tl.load(in_ptr0 + (64 + x1 + 16 * y0), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (y0 + 4 * x1), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_index_4(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 + (128 + x1 + 16 * y0), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (y0 + 4 * x1), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_index_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 + (192 + x1 + 16 * y0), xmask & ymask, eviction_policy='evict_last') tl.store(out_ptr0 + (y0 + 4 * x1), tmp0, xmask & ymask) @triton.jit def triton_poi_fused_add_div_6(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) tmp0 = tl.load(in_ptr0 + 0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK]) tmp6 = tl.load(in_ptr1 + 0) tmp7 = tl.broadcast_to(tmp6, [XBLOCK]) tmp10 = tl.load(in_out_ptr0 + 0) tmp11 = tl.broadcast_to(tmp10, [XBLOCK]) tmp14 = tl.load(in_ptr2 + 0) tmp15 = tl.broadcast_to(tmp14, [XBLOCK]) tmp2 = 0.0625 tmp3 = tmp1 * tmp2 tmp4 = 0.0 tmp5 = tmp3 + tmp4 tmp8 = tmp7 * tmp2 tmp9 = tmp5 + tmp8 tmp12 = tmp11 * tmp2 tmp13 = tmp9 + tmp12 tmp16 = tmp15 * tmp2 tmp17 = tmp13 + tmp16 tl.store(in_out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp17, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused__log_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__log_softmax_neg_1[grid(256)](buf0, buf1, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf0 buf2 = empty_strided_cuda((1, 4, 4, 4), (64, 1, 16, 4), torch.float32) triton_poi_fused_index_2[grid(4, 16)](buf1, buf2, 4, 16, XBLOCK=16, YBLOCK=4, num_warps=1, num_stages=1) buf3 = empty_strided_cuda((1, 4, 4, 4), (64, 1, 16, 4), torch.float32) triton_poi_fused_index_2[grid(4, 16)](arg1_1, buf3, 4, 16, XBLOCK= 16, YBLOCK=4, num_warps=1, num_stages=1) buf4 = extern_kernels.convolution(buf2, buf3, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (1, 1, 1, 1), (1, 1, 1, 1)) buf5 = buf3 del buf3 triton_poi_fused_index_3[grid(4, 16)](buf1, buf5, 4, 16, XBLOCK=16, YBLOCK=4, num_warps=1, num_stages=1) buf6 = buf2 del buf2 triton_poi_fused_index_3[grid(4, 16)](arg1_1, buf6, 4, 16, XBLOCK= 16, YBLOCK=4, num_warps=1, num_stages=1) buf7 = extern_kernels.convolution(buf5, buf6, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf7, (1, 1, 1, 1), (1, 1, 1, 1)) buf8 = buf6 del buf6 triton_poi_fused_index_4[grid(4, 16)](buf1, buf8, 4, 16, XBLOCK=16, YBLOCK=4, num_warps=1, num_stages=1) buf9 = buf5 del buf5 triton_poi_fused_index_4[grid(4, 16)](arg1_1, buf9, 4, 16, XBLOCK= 16, YBLOCK=4, num_warps=1, num_stages=1) buf10 = extern_kernels.convolution(buf8, buf9, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf10, (1, 1, 1, 1), (1, 1, 1, 1)) buf11 = buf9 del buf9 triton_poi_fused_index_5[grid(4, 16)](buf1, buf11, 4, 16, XBLOCK=16, YBLOCK=4, num_warps=1, num_stages=1) del buf1 buf12 = buf8 del buf8 triton_poi_fused_index_5[grid(4, 16)](arg1_1, buf12, 4, 16, XBLOCK= 16, YBLOCK=4, num_warps=1, num_stages=1) del arg1_1 buf13 = extern_kernels.convolution(buf11, buf12, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf13, (1, 1, 1, 1), (1, 1, 1, 1)) del buf11 del buf12 buf14 = buf10 del buf10 triton_poi_fused_add_div_6[grid(1)](buf14, buf4, buf7, buf13, 1, XBLOCK=1, num_warps=1, num_stages=1) del buf13 del buf4 del buf7 return buf14, class SoftCrossEntropyLoss2dNew(nn.Module): def __init__(self): super(SoftCrossEntropyLoss2dNew, 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]
jameslong95/FasterSeg
SoftCrossEntropyLoss2d
false
6,928
[ "MIT" ]
1
872e04964ea46494a6018d9915cee5476e361c27
https://github.com/jameslong95/FasterSeg/tree/872e04964ea46494a6018d9915cee5476e361c27
StyleTrack
import torch import torch.nn as nn def gram_matrix(input): """ gram matrix for feature assignments """ a, b, c, d = input.size() allG = [] for i in range(a): features = input[i].view(b, c * d) gram = torch.mm(features, features.t()) gram = gram.div(c * d) allG.append(gram) return torch.stack(allG) class StyleTrack(nn.Module): """ This modules tracks the content image style across many images for loading (partnered with next module). This is useful for e.g. maintaining color scheme of the content images """ def forward(self, input): """ forward pass """ gram = gram_matrix(input) self.value = gram return input 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 import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_stack_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 x0 = xindex % 4 x2 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 4 * x1), tmp4 & xmask, other=0.0) tmp6 = 0.0625 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], 8, tl.int64) tmp12 = tmp0 < tmp11 tmp13 = tmp10 & tmp12 tmp14 = tl.load(in_ptr1 + (x0 + 4 * (-4 + x1)), tmp13 & xmask, other=0.0) tmp15 = tmp14 * tmp6 tmp16 = tl.full(tmp15.shape, 0.0, tmp15.dtype) tmp17 = tl.where(tmp13, tmp15, tmp16) tmp18 = tmp0 >= tmp11 tmp19 = tl.full([1], 12, tl.int64) tmp20 = tmp0 < tmp19 tmp21 = tmp18 & tmp20 tmp22 = tl.load(in_ptr2 + (x0 + 4 * (-8 + x1)), tmp21 & xmask, other=0.0) tmp23 = tmp22 * tmp6 tmp24 = tl.full(tmp23.shape, 0.0, tmp23.dtype) tmp25 = tl.where(tmp21, tmp23, tmp24) tmp26 = tmp0 >= tmp19 tl.full([1], 16, tl.int64) tmp29 = tl.load(in_ptr3 + (x0 + 4 * (-12 + x1)), tmp26 & xmask, other=0.0) tmp30 = tmp29 * tmp6 tmp31 = tl.full(tmp30.shape, 0.0, tmp30.dtype) tmp32 = tl.where(tmp26, tmp30, tmp31) tmp33 = tl.where(tmp21, tmp25, tmp32) tmp34 = tl.where(tmp13, tmp17, tmp33) tmp35 = tl.where(tmp4, tmp9, tmp34) tl.store(out_ptr0 + x2, tmp35, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(arg0_1, (4, 16), (16, 1), 0), reinterpret_tensor(arg0_1, (16, 4), (1, 16), 0), out=buf0) buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(arg0_1, (4, 16), (16, 1), 64), reinterpret_tensor(arg0_1, (16, 4), (1, 16), 64), out=buf1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(arg0_1, (4, 16), (16, 1), 128), reinterpret_tensor(arg0_1, (16, 4), (1, 16), 128), out=buf2) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(arg0_1, (4, 16), (16, 1), 192), reinterpret_tensor(arg0_1, (16, 4), (1, 16), 192), out=buf3) del arg0_1 buf4 = empty_strided_cuda((16, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_stack_0[grid(64)](buf0, buf1, buf2, buf3, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf0 del buf1 del buf2 del buf3 return reinterpret_tensor(buf4, (4, 4, 4), (16, 4, 1), 0), def gram_matrix(input): """ gram matrix for feature assignments """ a, b, c, d = input.size() allG = [] for i in range(a): features = input[i].view(b, c * d) gram = torch.mm(features, features.t()) gram = gram.div(c * d) allG.append(gram) return torch.stack(allG) class StyleTrackNew(nn.Module): """ This modules tracks the content image style across many images for loading (partnered with next module). This is useful for e.g. maintaining color scheme of the content images """ def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
jaredaevans/UltrafastNST
StyleTrack
false
6,929
[ "MIT" ]
1
6671c6b618ce6bb4920b15f782be962e484a5423
https://github.com/jaredaevans/UltrafastNST/tree/6671c6b618ce6bb4920b15f782be962e484a5423
ReflectPad2d
import torch class ReflectPad2d(torch.nn.Module): """ reflectionpad2d that can be transfered across onnx etc size : int (the size of padding) """ def __init__(self, size): super().__init__() self.size = size def forward(self, ins): size = self.size l_list, r_list = [], [] u_list, d_list = [], [] for i in range(size): left = ins[:, :, :, size - i:size - i + 1] l_list.append(left) right = ins[:, :, :, i - size - 1:i - size] r_list.append(right) l_list.append(ins) ins = torch.cat(l_list + r_list[::-1], dim=3) for i in range(size): up = ins[:, :, size - i:size - i + 1, :] u_list.append(up) down = ins[:, :, i - size - 1:i - size, :] d_list.append(down) u_list.append(ins) ins = torch.cat(u_list + d_list[::-1], dim=2) return ins def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'size': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream 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 = 640 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 10 x1 = xindex // 10 x2 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (3 + 4 * x1), 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 + (2 + 4 * x1), 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 + (1 + 4 * x1), tmp14 & xmask, eviction_policy= 'evict_last', other=0.0) tmp16 = tmp0 >= tmp12 tmp17 = tl.full([1], 7, tl.int64) tmp18 = tmp0 < tmp17 tmp19 = tmp16 & tmp18 tmp20 = tl.load(in_ptr0 + (4 * x1 + (-3 + x0)), tmp19 & xmask, eviction_policy='evict_last', other=0.0) tmp21 = tmp0 >= tmp17 tmp22 = tl.full([1], 8, tl.int64) tmp23 = tmp0 < tmp22 tmp24 = tmp21 & tmp23 tmp25 = tl.load(in_ptr0 + (2 + 4 * x1), tmp24 & xmask, eviction_policy= 'evict_last', other=0.0) tmp26 = tmp0 >= tmp22 tmp27 = tl.full([1], 9, tl.int64) tmp28 = tmp0 < tmp27 tmp29 = tmp26 & tmp28 tmp30 = tl.load(in_ptr0 + (1 + 4 * x1), tmp29 & xmask, eviction_policy= 'evict_last', other=0.0) tmp31 = tmp0 >= tmp27 tl.full([1], 10, tl.int64) tmp34 = tl.load(in_ptr0 + 4 * x1, tmp31 & xmask, eviction_policy= 'evict_last', other=0.0) tmp35 = tl.where(tmp29, tmp30, tmp34) tmp36 = tl.where(tmp24, tmp25, tmp35) tmp37 = tl.where(tmp19, tmp20, tmp36) tmp38 = tl.where(tmp14, tmp15, tmp37) tmp39 = tl.where(tmp9, tmp10, tmp38) tmp40 = tl.where(tmp4, tmp5, tmp39) tl.store(out_ptr0 + x2, tmp40, xmask) @triton.jit def triton_poi_fused_cat_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 1600 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 10 % 10 x0 = xindex % 10 x2 = xindex // 100 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 + (30 + x0 + 40 * 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 + (20 + x0 + 40 * 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 + (10 + x0 + 40 * x2), tmp14 & xmask, eviction_policy='evict_last', other=0.0) tmp16 = tmp0 >= tmp12 tmp17 = tl.full([1], 7, tl.int64) tmp18 = tmp0 < tmp17 tmp19 = tmp16 & tmp18 tmp20 = tl.load(in_ptr0 + (x0 + 10 * (-3 + x1) + 40 * x2), tmp19 & xmask, other=0.0) tmp21 = tmp0 >= tmp17 tmp22 = tl.full([1], 8, tl.int64) tmp23 = tmp0 < tmp22 tmp24 = tmp21 & tmp23 tmp25 = tl.load(in_ptr0 + (20 + x0 + 40 * x2), tmp24 & xmask, eviction_policy='evict_last', other=0.0) tmp26 = tmp0 >= tmp22 tmp27 = tl.full([1], 9, tl.int64) tmp28 = tmp0 < tmp27 tmp29 = tmp26 & tmp28 tmp30 = tl.load(in_ptr0 + (10 + x0 + 40 * x2), tmp29 & xmask, eviction_policy='evict_last', other=0.0) tmp31 = tmp0 >= tmp27 tl.full([1], 10, tl.int64) tmp34 = tl.load(in_ptr0 + (x0 + 40 * x2), tmp31 & xmask, eviction_policy='evict_last', other=0.0) tmp35 = tl.where(tmp29, tmp30, tmp34) tmp36 = tl.where(tmp24, tmp25, tmp35) tmp37 = tl.where(tmp19, tmp20, tmp36) tmp38 = tl.where(tmp14, tmp15, tmp37) tmp39 = tl.where(tmp9, tmp10, tmp38) tmp40 = tl.where(tmp4, tmp5, tmp39) tl.store(out_ptr0 + x3, tmp40, 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, 10), (160, 40, 10, 1), torch. float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(640)](arg0_1, buf0, 640, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((4, 4, 10, 10), (400, 100, 10, 1), torch. float32) triton_poi_fused_cat_1[grid(1600)](buf0, buf1, 1600, XBLOCK=128, num_warps=4, num_stages=1) del buf0 return buf1, class ReflectPad2dNew(torch.nn.Module): """ reflectionpad2d that can be transfered across onnx etc size : int (the size of padding) """ def __init__(self, size): super().__init__() self.size = size def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
jaredaevans/UltrafastNST
ReflectPad2d
false
6,930
[ "MIT" ]
1
6671c6b618ce6bb4920b15f782be962e484a5423
https://github.com/jaredaevans/UltrafastNST/tree/6671c6b618ce6bb4920b15f782be962e484a5423
EncoderImagePrecomp
import torch import numpy as np import torch.nn as nn from collections import OrderedDict import torch.nn.init def l2norm(x, dim=-1): return x / x.norm(2, dim=dim, keepdim=True).clamp(min=1e-06) class EncoderImagePrecomp(nn.Module): """ image encoder """ def __init__(self, img_dim, embed_size, no_imgnorm=False): super(EncoderImagePrecomp, self).__init__() self.embed_size = embed_size self.no_imgnorm = no_imgnorm self.fc = nn.Linear(img_dim, embed_size) self.init_weights() def init_weights(self): """ Xavier initialization for the fully connected layer """ r = np.sqrt(6.0) / np.sqrt(self.fc.in_features + self.fc.out_features) self.fc.weight.data.uniform_(-r, r) self.fc.bias.data.fill_(0) def forward(self, images): """ extract image feature vectors """ features = self.fc(images.float()) if not self.no_imgnorm: features = l2norm(features) return features def load_state_dict(self, state_dict): """ copies parameters, overwritting the default one to accept state_dict from Full model """ own_state = self.state_dict() new_state = OrderedDict() for name, param in state_dict.items(): if name in own_state: new_state[name] = param super(EncoderImagePrecomp, self).load_state_dict(new_state) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'img_dim': 4, 'embed_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import numpy as np import torch.nn as nn from collections import OrderedDict import torch.nn.init assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clamp_div_linalg_vector_norm_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = libdevice.sqrt(tmp11) tmp13 = 1e-06 tmp14 = triton_helpers.maximum(tmp12, tmp13) tmp15 = tmp0 / tmp14 tl.store(out_ptr0 + x2, tmp15, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 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.addmm(primals_3, reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_2 del primals_3 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clamp_div_linalg_vector_norm_0[grid(256)](buf0, buf1, 256, XBLOCK=256, num_warps=4, num_stages=1) return buf1, reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), buf0 def l2norm(x, dim=-1): return x / x.norm(2, dim=dim, keepdim=True).clamp(min=1e-06) class EncoderImagePrecompNew(nn.Module): """ image encoder """ def __init__(self, img_dim, embed_size, no_imgnorm=False): super(EncoderImagePrecompNew, self).__init__() self.embed_size = embed_size self.no_imgnorm = no_imgnorm self.fc = nn.Linear(img_dim, embed_size) self.init_weights() def init_weights(self): """ Xavier initialization for the fully connected layer """ r = np.sqrt(6.0) / np.sqrt(self.fc.in_features + self.fc.out_features) self.fc.weight.data.uniform_(-r, r) self.fc.bias.data.fill_(0) def load_state_dict(self, state_dict): """ copies parameters, overwritting the default one to accept state_dict from Full model """ own_state = self.state_dict() new_state = OrderedDict() for name, param in state_dict.items(): if name in own_state: new_state[name] = param super(EncoderImagePrecompNew, self).load_state_dict(new_state) 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]
jefflai108/VGNSL
EncoderImagePrecomp
false
6,931
[ "MIT" ]
1
0edc3db3691abbad2a505b2165bd99e7a62d784f
https://github.com/jefflai108/VGNSL/tree/0edc3db3691abbad2a505b2165bd99e7a62d784f
BehlerAngular
import torch from torch import nn as nn class BehlerAngular(nn.Module): """ Compute Behler type angular contribution of the angle spanned by three atoms: :math:`2^{(1-\\zeta)} (1 + \\lambda \\cos( {\\theta}_{ijk} ) )^\\zeta` Sets of zetas with lambdas of -1 and +1 are generated automatically. Args: zetas (set of int): Set of exponents used to compute angular Behler term (default={1}) """ def __init__(self, zetas={1}): super(BehlerAngular, self).__init__() self.zetas = zetas def forward(self, cos_theta): """ Args: cos_theta (torch.Tensor): Cosines between all pairs of neighbors of the central atom. Returns: torch.Tensor: Tensor containing values of the angular filters. """ angular_pos = [(2 ** (1 - zeta) * ((1.0 - cos_theta) ** zeta). unsqueeze(-1)) for zeta in self.zetas] angular_neg = [(2 ** (1 - zeta) * ((1.0 + cos_theta) ** zeta). unsqueeze(-1)) for zeta in self.zetas] angular_all = angular_pos + angular_neg return torch.cat(angular_all, -1) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch import 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 = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 2 x1 = xindex // 2 x2 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + x1, tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp6 = 1.0 tmp7 = tmp6 - tmp5 tmp8 = tmp7 * tmp6 tmp9 = tl.full(tmp8.shape, 0.0, tmp8.dtype) tmp10 = tl.where(tmp4, tmp8, tmp9) tmp11 = tmp0 >= tmp3 tl.full([1], 2, tl.int64) tmp14 = tl.load(in_ptr0 + x1, tmp11 & xmask, eviction_policy= 'evict_last', other=0.0) tmp15 = tmp14 + tmp6 tmp16 = tmp15 * tmp6 tmp17 = tl.full(tmp16.shape, 0.0, tmp16.dtype) tmp18 = tl.where(tmp11, tmp16, tmp17) tmp19 = tl.where(tmp4, tmp10, tmp18) tl.store(out_ptr0 + x2, tmp19, 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, 2), (128, 32, 8, 2, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(512)](arg0_1, buf0, 512, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf0, class BehlerAngularNew(nn.Module): """ Compute Behler type angular contribution of the angle spanned by three atoms: :math:`2^{(1-\\zeta)} (1 + \\lambda \\cos( {\\theta}_{ijk} ) )^\\zeta` Sets of zetas with lambdas of -1 and +1 are generated automatically. Args: zetas (set of int): Set of exponents used to compute angular Behler term (default={1}) """ def __init__(self, zetas={1}): super(BehlerAngularNew, self).__init__() self.zetas = zetas def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
jduerholt/schnetpack
BehlerAngular
false
6,932
[ "MIT" ]
1
228d50fdeba4592b1de54d3a9570d766757c2ee1
https://github.com/jduerholt/schnetpack/tree/228d50fdeba4592b1de54d3a9570d766757c2ee1
Mult
import torch import torch.utils.data import torch from torch import nn class Mult(nn.Module): def __init__(self, nc): super(Mult, self).__init__() self.register_parameter(name='exp', param=torch.nn.Parameter(torch. diag(torch.ones(nc)).unsqueeze(-1).unsqueeze(-1))) """self.register_parameter(name='weight', param=torch.nn.Parameter(torch.ones(nc).unsqueeze(-1).unsqueeze(-1))) """ self.register_parameter(name='bias', param=torch.nn.Parameter(torch .zeros(nc).unsqueeze(-1).unsqueeze(-1))) self.relu = nn.ReLU() def forward(self, x): x = self.relu(x) + 0.1 return x.unsqueeze(-3).pow(self.exp).prod(1) + self.bias def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'nc': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.utils.data import torch from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_pow_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 1024 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 16 x4 = xindex // 64 x5 = xindex // 16 % 16 x6 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 16 * x4), xmask, eviction_policy= 'evict_last') tmp5 = tl.load(in_ptr1 + x5, xmask, eviction_policy='evict_last') tmp1 = tl.full([1], 0, tl.int32) tmp2 = triton_helpers.maximum(tmp1, tmp0) tmp3 = 0.1 tmp4 = tmp2 + tmp3 tmp6 = libdevice.pow(tmp4, tmp5) tl.store(out_ptr0 + x6, tmp6, xmask) @triton.jit def triton_poi_fused_add_prod_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex // 64 x3 = xindex % 64 x1 = xindex // 16 % 4 x4 = xindex tmp0 = tl.load(in_ptr0 + (x3 + 256 * x2), xmask) tmp1 = tl.load(in_ptr0 + (64 + x3 + 256 * x2), xmask) tmp3 = tl.load(in_ptr0 + (128 + x3 + 256 * x2), xmask) tmp5 = tl.load(in_ptr0 + (192 + x3 + 256 * x2), xmask) tmp7 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 * tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tl.store(out_ptr0 + x4, tmp8, xmask) @triton.jit def triton_poi_fused_ge_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 + x0, xmask) tmp1 = 0.0 tmp2 = tmp0 >= tmp1 tl.store(out_ptr0 + x0, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 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, 1), (1, 1, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_pow_0[grid(1024)](primals_1, primals_2, buf0, 1024, XBLOCK=256, num_warps=4, num_stages=1) buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_add_prod_1[grid(256)](buf0, primals_3, buf1, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_3 buf2 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.bool) triton_poi_fused_ge_2[grid(16)](primals_2, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_2 return buf1, primals_1, buf0, buf2 class MultNew(nn.Module): def __init__(self, nc): super(MultNew, self).__init__() self.register_parameter(name='exp', param=torch.nn.Parameter(torch. diag(torch.ones(nc)).unsqueeze(-1).unsqueeze(-1))) """self.register_parameter(name='weight', param=torch.nn.Parameter(torch.ones(nc).unsqueeze(-1).unsqueeze(-1))) """ self.register_parameter(name='bias', param=torch.nn.Parameter(torch .zeros(nc).unsqueeze(-1).unsqueeze(-1))) self.relu = nn.ReLU() def forward(self, input_0): primals_2 = self.exp primals_3 = self.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
jayin92/vae-pix2pix-terrain-generator
Mult
false
6,933
[ "BSD-3-Clause" ]
1
805ea0b053dc9d9c22301af7f536a8fb7e2118d1
https://github.com/jayin92/vae-pix2pix-terrain-generator/tree/805ea0b053dc9d9c22301af7f536a8fb7e2118d1
VectorQuantizeLayer_GB
import torch from torch import nn import torch.nn.functional as F class VectorQuantizeLayer_GB(nn.Module): def __init__(self, input_dim, vq_size, vq_dim, temp=(1.0, 0.1, 0.99), groups=1, combine_groups=True, time_first=True, activation=nn.GELU( ), weight_proj_depth=1, weight_proj_factor=1): """Vector quantization using gumbel softmax Args: input_dim: input dimension (channels) vq_size: number of quantized vectors per group vq_dim: dimensionality of the resulting quantized vector temp: temperature for training. this should be a tuple of 3 elements: (start, stop, decay factor) groups: number of groups for vector quantization combine_groups: whether to use the vectors for all groups time_first: if true, expect input in BxTxC format, otherwise in BxCxT activation: what activation to use (should be a module). this is only used if weight_proj_depth is > 1 weight_proj_depth: number of layers (with activation in between) to project input before computing logits weight_proj_factor: this is used only if weight_proj_depth is > 1. scales the inner dimensionality of projections by this factor """ super().__init__() self.input_dim = input_dim self.vq_size = vq_size self.groups = groups self.combine_groups = combine_groups self.time_first = time_first self.out_dim = vq_dim assert vq_dim % groups == 0, f'dim {vq_dim} must be divisible by groups {groups} for concatenation' var_dim = vq_dim // groups num_groups = groups if not combine_groups else 1 self.vars = nn.Parameter(torch.FloatTensor(1, num_groups * vq_size, var_dim)) nn.init.uniform_(self.vars) if weight_proj_depth > 1: def block(input_dim, output_dim): return nn.Sequential(nn.Linear(input_dim, output_dim), activation) inner_dim = self.input_dim * weight_proj_factor self.weight_proj = nn.Sequential(*[block(self.input_dim if i == 0 else inner_dim, inner_dim) for i in range( weight_proj_depth - 1)], nn.Linear(inner_dim, groups * vq_size) ) else: self.weight_proj = nn.Linear(self.input_dim, groups * vq_size) nn.init.normal_(self.weight_proj.weight, mean=0, std=1) nn.init.zeros_(self.weight_proj.bias) assert len(temp) == 3, temp self.max_temp, self.min_temp, self.temp_decay = temp self.curr_temp = self.max_temp self.codebook_indices = None def set_num_updates(self, num_updates): self.curr_temp = max(self.max_temp * self.temp_decay ** num_updates, self.min_temp) def get_codebook_indices(self): if self.codebook_indices is None: from itertools import product p = [range(self.vq_size)] * self.groups inds = list(product(*p)) self.codebook_indices = torch.tensor(inds, dtype=torch.long, device=self.vars.device).flatten() if not self.combine_groups: self.codebook_indices = self.codebook_indices.view(self. vq_size ** self.groups, -1) for b in range(1, self.groups): self.codebook_indices[:, b] += self.vq_size * b self.codebook_indices = self.codebook_indices.flatten() return self.codebook_indices def codebook(self): indices = self.get_codebook_indices() return self.vars.squeeze(0).index_select(0, indices).view(self. vq_size ** self.groups, -1) def sample_from_codebook(self, b, n): indices = self.get_codebook_indices() indices = indices.view(-1, self.groups) cb_size = indices.size(0) assert n < cb_size, f'sample size {n} is greater than size of codebook {cb_size}' sample_idx = torch.randint(low=0, high=cb_size, size=(b * n,)) indices = indices[sample_idx] z = self.vars.squeeze(0).index_select(0, indices.flatten()).view(b, n, -1) return z def to_codebook_index(self, indices): res = indices.new_full(indices.shape[:-1], 0) for i in range(self.groups): exponent = self.groups - i - 1 res += indices[..., i] * self.vq_size ** exponent return res def forward(self, x, produce_targets=False): result = {'vq_size': self.vq_size * self.groups} if not self.time_first: x = x.transpose(1, 2) bsz, tsz, fsz = x.shape x = x.reshape(-1, fsz) x = self.weight_proj(x) x = x.view(bsz * tsz * self.groups, -1) _, k = x.max(-1) hard_x = x.new_zeros(*x.shape).scatter_(-1, k.view(-1, 1), 1.0).view( bsz * tsz, self.groups, -1) result['temp'] = self.curr_temp if self.training: x = F.gumbel_softmax(x.float(), tau=self.curr_temp, hard=True ).type_as(x) else: x = hard_x x = x.view(bsz * tsz, -1) vars = self.vars if self.combine_groups: vars = vars.repeat(1, self.groups, 1) if produce_targets: result['targets'] = x.view(bsz * tsz * self.groups, -1).argmax(dim =-1).view(bsz, tsz, self.groups).detach() x = x.unsqueeze(-1) * vars x = x.view(bsz * tsz, self.groups, self.vq_size, -1) x = x.sum(-2) x = x.view(bsz, tsz, -1) if not self.time_first: x = x.transpose(1, 2) return x def get_inputs(): return [torch.rand([4, 4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'vq_size': 4, 'vq_dim': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_max_0(in_ptr0, out_ptr0, 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') tmp17 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp32 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 > tmp1 tmp3 = tmp0 == tmp1 tmp4 = tmp0 != tmp0 tmp5 = tmp1 != tmp1 tmp6 = tmp4 > tmp5 tmp7 = tmp2 | tmp6 tmp8 = tmp4 & tmp5 tmp9 = tmp3 | tmp8 tmp10 = tl.full([1], 0, tl.int64) tmp11 = tl.full([1], 1, tl.int64) tmp12 = tmp10 < tmp11 tmp13 = tmp9 & tmp12 tmp14 = tmp7 | tmp13 tmp15 = tl.where(tmp14, tmp0, tmp1) tmp16 = tl.where(tmp14, tmp10, tmp11) tmp18 = tmp15 > tmp17 tmp19 = tmp15 == tmp17 tmp20 = tmp15 != tmp15 tmp21 = tmp17 != tmp17 tmp22 = tmp20 > tmp21 tmp23 = tmp18 | tmp22 tmp24 = tmp20 & tmp21 tmp25 = tmp19 | tmp24 tmp26 = tl.full([1], 2, tl.int64) tmp27 = tmp16 < tmp26 tmp28 = tmp25 & tmp27 tmp29 = tmp23 | tmp28 tmp30 = tl.where(tmp29, tmp15, tmp17) tmp31 = tl.where(tmp29, tmp16, tmp26) tmp33 = tmp30 > tmp32 tmp34 = tmp30 == tmp32 tmp35 = tmp30 != tmp30 tmp36 = tmp32 != tmp32 tmp37 = tmp35 > tmp36 tmp38 = tmp33 | tmp37 tmp39 = tmp35 & tmp36 tmp40 = tmp34 | tmp39 tmp41 = tl.full([1], 3, tl.int64) tmp42 = tmp31 < tmp41 tmp43 = tmp40 & tmp42 tmp44 = tmp38 | tmp43 tl.where(tmp44, tmp30, tmp32) tmp46 = tl.where(tmp44, tmp31, tmp41) tl.store(out_ptr0 + x0, tmp46, xmask) @triton.jit def triton_poi_fused_scatter_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 x1 = xindex // 4 x0 = xindex % 4 x2 = xindex tmp0 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp1 = x0 tmp2 = tmp0 == tmp1 tmp3 = 1.0 tmp4 = 0.0 tmp5 = tl.where(tmp2, tmp3, tmp4) tl.store(out_ptr0 + x2, tmp5, xmask) @triton.jit def triton_poi_fused_sum_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 x0 = xindex % 4 x2 = xindex tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp8 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last' ) tmp12 = tl.load(in_ptr1 + (12 + 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 tl.store(out_ptr0 + x2, tmp14, xmask) def call(args): primals_1, primals_2, primals_3, primals_4 = args args.clear() assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (4,), (1,)) assert_size_stride(primals_4, (1, 4, 4), (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.addmm(primals_3, reinterpret_tensor(primals_1, (16, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 del primals_3 buf1 = empty_strided_cuda((16,), (1,), torch.int64) get_raw_stream(0) triton_poi_fused_max_0[grid(16)](buf0, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1) buf2 = buf0 del buf0 triton_poi_fused_scatter_1[grid(64)](buf1, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf1 buf3 = empty_strided_cuda((16, 1, 4), (4, 4, 1), torch.float32) triton_poi_fused_sum_2[grid(64)](buf2, primals_4, buf3, 64, XBLOCK= 64, num_warps=1, num_stages=1) del primals_4 return reinterpret_tensor(buf3, (4, 4, 4), (16, 4, 1), 0), buf2 class VectorQuantizeLayer_GBNew(nn.Module): def __init__(self, input_dim, vq_size, vq_dim, temp=(1.0, 0.1, 0.99), groups=1, combine_groups=True, time_first=True, activation=nn.GELU( ), weight_proj_depth=1, weight_proj_factor=1): """Vector quantization using gumbel softmax Args: input_dim: input dimension (channels) vq_size: number of quantized vectors per group vq_dim: dimensionality of the resulting quantized vector temp: temperature for training. this should be a tuple of 3 elements: (start, stop, decay factor) groups: number of groups for vector quantization combine_groups: whether to use the vectors for all groups time_first: if true, expect input in BxTxC format, otherwise in BxCxT activation: what activation to use (should be a module). this is only used if weight_proj_depth is > 1 weight_proj_depth: number of layers (with activation in between) to project input before computing logits weight_proj_factor: this is used only if weight_proj_depth is > 1. scales the inner dimensionality of projections by this factor """ super().__init__() self.input_dim = input_dim self.vq_size = vq_size self.groups = groups self.combine_groups = combine_groups self.time_first = time_first self.out_dim = vq_dim assert vq_dim % groups == 0, f'dim {vq_dim} must be divisible by groups {groups} for concatenation' var_dim = vq_dim // groups num_groups = groups if not combine_groups else 1 self.vars = nn.Parameter(torch.FloatTensor(1, num_groups * vq_size, var_dim)) nn.init.uniform_(self.vars) if weight_proj_depth > 1: def block(input_dim, output_dim): return nn.Sequential(nn.Linear(input_dim, output_dim), activation) inner_dim = self.input_dim * weight_proj_factor self.weight_proj = nn.Sequential(*[block(self.input_dim if i == 0 else inner_dim, inner_dim) for i in range( weight_proj_depth - 1)], nn.Linear(inner_dim, groups * vq_size) ) else: self.weight_proj = nn.Linear(self.input_dim, groups * vq_size) nn.init.normal_(self.weight_proj.weight, mean=0, std=1) nn.init.zeros_(self.weight_proj.bias) assert len(temp) == 3, temp self.max_temp, self.min_temp, self.temp_decay = temp self.curr_temp = self.max_temp self.codebook_indices = None def set_num_updates(self, num_updates): self.curr_temp = max(self.max_temp * self.temp_decay ** num_updates, self.min_temp) def get_codebook_indices(self): if self.codebook_indices is None: from itertools import product p = [range(self.vq_size)] * self.groups inds = list(product(*p)) self.codebook_indices = torch.tensor(inds, dtype=torch.long, device=self.vars.device).flatten() if not self.combine_groups: self.codebook_indices = self.codebook_indices.view(self. vq_size ** self.groups, -1) for b in range(1, self.groups): self.codebook_indices[:, b] += self.vq_size * b self.codebook_indices = self.codebook_indices.flatten() return self.codebook_indices def codebook(self): indices = self.get_codebook_indices() return self.vars.squeeze(0).index_select(0, indices).view(self. vq_size ** self.groups, -1) def sample_from_codebook(self, b, n): indices = self.get_codebook_indices() indices = indices.view(-1, self.groups) cb_size = indices.size(0) assert n < cb_size, f'sample size {n} is greater than size of codebook {cb_size}' sample_idx = torch.randint(low=0, high=cb_size, size=(b * n,)) indices = indices[sample_idx] z = self.vars.squeeze(0).index_select(0, indices.flatten()).view(b, n, -1) return z def to_codebook_index(self, indices): res = indices.new_full(indices.shape[:-1], 0) for i in range(self.groups): exponent = self.groups - i - 1 res += indices[..., i] * self.vq_size ** exponent return res def forward(self, input_0): primals_4 = self.vars primals_2 = self.weight_proj.weight primals_3 = self.weight_proj.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
jefflai108/Self-Supervised-Speech-Pretraining-and-Representation-Learning
VectorQuantizeLayer_GB
false
6,934
[ "MIT" ]
1
bb8df008397d5a0360ab7d4b68e91588ed648270
https://github.com/jefflai108/Self-Supervised-Speech-Pretraining-and-Representation-Learning/tree/bb8df008397d5a0360ab7d4b68e91588ed648270
Accuracy
import torch from torch import nn def accuracy(logits: 'torch.Tensor', labels: 'torch.Tensor', ignore_index: 'int'=-100) ->torch.Tensor: with torch.no_grad(): valid_mask = labels != ignore_index predictions = logits.float().argmax(-1) correct = (predictions == labels) * valid_mask return correct.sum().float() / valid_mask.sum().float() class Accuracy(nn.Module): def __init__(self, ignore_index: 'int'=-100): super().__init__() self.ignore_index = ignore_index def forward(self, inputs, target): return accuracy(inputs, target, self.ignore_index) def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_argmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp17 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp32 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ) tmp2 = tmp0 > tmp1 tmp3 = tmp0 == tmp1 tmp4 = tmp0 != tmp0 tmp5 = tmp1 != tmp1 tmp6 = tmp4 > tmp5 tmp7 = tmp2 | tmp6 tmp8 = tmp4 & tmp5 tmp9 = tmp3 | tmp8 tmp10 = tl.full([1], 0, tl.int64) tmp11 = tl.full([1], 1, tl.int64) tmp12 = tmp10 < tmp11 tmp13 = tmp9 & tmp12 tmp14 = tmp7 | tmp13 tmp15 = tl.where(tmp14, tmp0, tmp1) tmp16 = tl.where(tmp14, tmp10, tmp11) tmp18 = tmp15 > tmp17 tmp19 = tmp15 == tmp17 tmp20 = tmp15 != tmp15 tmp21 = tmp17 != tmp17 tmp22 = tmp20 > tmp21 tmp23 = tmp18 | tmp22 tmp24 = tmp20 & tmp21 tmp25 = tmp19 | tmp24 tmp26 = tl.full([1], 2, tl.int64) tmp27 = tmp16 < tmp26 tmp28 = tmp25 & tmp27 tmp29 = tmp23 | tmp28 tmp30 = tl.where(tmp29, tmp15, tmp17) tmp31 = tl.where(tmp29, tmp16, tmp26) tmp33 = tmp30 > tmp32 tmp34 = tmp30 == tmp32 tmp35 = tmp30 != tmp30 tmp36 = tmp32 != tmp32 tmp37 = tmp35 > tmp36 tmp38 = tmp33 | tmp37 tmp39 = tmp35 & tmp36 tmp40 = tmp34 | tmp39 tmp41 = tl.full([1], 3, tl.int64) tmp42 = tmp31 < tmp41 tmp43 = tmp40 & tmp42 tmp44 = tmp38 | tmp43 tl.where(tmp44, tmp30, tmp32) tmp46 = tl.where(tmp44, tmp31, tmp41) tl.store(out_ptr0 + x0, tmp46, xmask) @triton.jit def triton_per_fused__to_copy_div_eq_mul_ne_sum_1(in_ptr0, in_ptr1, out_ptr2, 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 % 64 r2 = rindex tmp0 = tl.load(in_ptr0 + r0, None, eviction_policy='evict_last') tmp2 = tl.load(in_ptr1 + r2, None) tmp1 = tmp0.to(tl.float32) tmp3 = tmp1 == tmp2 tmp4 = -100.0 tmp5 = tmp2 != tmp4 tmp6 = tmp3 & tmp5 tmp7 = tmp6.to(tl.int64) tmp8 = tl.broadcast_to(tmp7, [RBLOCK]) tmp10 = triton_helpers.promote_to_tensor(tl.sum(tmp8, 0)) tmp11 = tmp5.to(tl.int64) tmp12 = tl.broadcast_to(tmp11, [RBLOCK]) tmp14 = triton_helpers.promote_to_tensor(tl.sum(tmp12, 0)) tmp15 = tmp10.to(tl.float32) tmp16 = tmp14.to(tl.float32) tmp17 = tmp15 / tmp16 tl.store(out_ptr2 + tl.full([1], 0, tl.int32), tmp17, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.int64) get_raw_stream(0) triton_poi_fused_argmax_0[grid(64)](arg0_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 buf3 = empty_strided_cuda((), (), torch.float32) triton_per_fused__to_copy_div_eq_mul_ne_sum_1[grid(1)](buf0, arg1_1, buf3, 1, 256, num_warps=2, num_stages=1) del arg1_1 del buf0 return buf3, def accuracy(logits: 'torch.Tensor', labels: 'torch.Tensor', ignore_index: 'int'=-100) ->torch.Tensor: with torch.no_grad(): valid_mask = labels != ignore_index predictions = logits.float().argmax(-1) correct = (predictions == labels) * valid_mask return correct.sum().float() / valid_mask.sum().float() class AccuracyNew(nn.Module): def __init__(self, ignore_index: 'int'=-100): super().__init__() self.ignore_index = ignore_index def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
jgoodson/TraGeC
Accuracy
false
6,935
[ "BSD-3-Clause" ]
1
3370e29ba0639745055cbee726a40181a4dd61df
https://github.com/jgoodson/TraGeC/tree/3370e29ba0639745055cbee726a40181a4dd61df
ComboLossOnlyPos
import torch import torch.nn as nn class SoftDiceLoss(nn.Module): """Differentiable soft dice loss. Note: Sigmoid is automatically applied here! """ def __init__(self): super(SoftDiceLoss, self).__init__() def forward(self, logits, targets): eps = 1e-09 num = targets.size(0) probs = torch.sigmoid(logits) m1 = probs.view(num, -1) m2 = targets.view(num, -1).float() intersection = torch.sum(m1 * m2, 1) union = torch.sum(m1, dim=1) + torch.sum(m2, dim=1) score = (2 * intersection + eps) / (union + eps) score = (1 - score).mean() return score class MultiLabelDiceLoss(nn.Module): """The average dice across multiple classes. Note: Sigmoid is automatically applied here! """ def __init__(self): super(MultiLabelDiceLoss, self).__init__() self.dice_loss = SoftDiceLoss() def forward(self, logits, targets): loss = 0 num_classes = targets.size(1) for class_nr in range(num_classes): loss += self.dice_loss(logits[:, class_nr, :, :], targets[:, class_nr, :, :]) return loss / num_classes class ComboLoss(nn.Module): """Weighted classification and segmentation loss. Attributes: weights (list): activation: bce: with logits loss dice_loss: soft dice loss (all classes) """ def __init__(self, weights=[0.1, 0, 1], activation=None): """ Args: weights (list): [image_cls, pixel_seg, pixel_cls] activation: One of ['sigmoid', None] """ super(ComboLoss, self).__init__() self.weights = weights self.activation = activation assert self.activation in ['sigmoid', None ], "`activation` must be one of ['sigmoid', None]." self.bce = nn.BCEWithLogitsLoss(reduce=True) self.dice_loss = MultiLabelDiceLoss() def create_fc_tensors(self, logits, targets): """Creates the classification tensors from the segmentation ones. """ batch_size, num_classes, _, _ = targets.shape summed = targets.view(batch_size, num_classes, -1).sum(-1) targets_fc = (summed > 0).float() logits_fc = logits.view(batch_size, num_classes, -1) logits_fc = torch.max(logits_fc, -1)[0] return logits_fc, targets_fc def forward(self, logits, targets): logits_fc, targets_fc = self.create_fc_tensors(logits, targets) p = torch.sigmoid(logits) if self.activation == 'sigmoid' else logits if self.weights[0]: loss_fc = self.weights[0] * self.bce(logits_fc, targets_fc) else: loss_fc = torch.tensor(0) if self.weights[1] or self.weights[2]: loss_seg_dice = self.weights[1] * self.dice_loss(p, targets) loss_seg_bce = self.weights[2] * self.bce(logits, targets) else: loss_seg_dice = torch.tensor(0) loss_seg_bce = torch.tensor(0) loss = loss_fc + loss_seg_bce + loss_seg_dice return loss class ComboLossOnlyPos(ComboLoss): """Weighted classification and segmentation loss. (Considers only positive) This loss is only calculated on labels with ROIs in them to maximize the foreground class prediction capability. Attributes: weights (list): activation: bce: with logits loss dice_loss: soft dice loss (per class) """ def __init__(self, weights=[0.1, 0, 1], activation=None): """ Args: weights (list): [image_cls, pixel_seg, pixel_cls] activation: One of ['sigmoid', None] """ super().__init__(weights=weights, activation=activation) self.dice_loss = SoftDiceLoss() def forward(self, logits, targets): logits_fc, targets_fc = self.create_fc_tensors(logits, targets) n_pos = targets_fc.sum() pos_idx = targets_fc > 0.5 neg_idx = targets_fc < 0.5 if self.weights[0]: loss_fc = self.weights[0] * self.bce(logits_fc[neg_idx], targets_fc[neg_idx]) else: loss_fc = torch.tensor(0) if self.weights[1] or self.weights[2]: logits_pos, targets_pos = logits[pos_idx], targets[pos_idx] if n_pos == 0: loss_seg_dice = torch.tensor(0) else: loss_seg_dice = self.weights[1] * self.dice_loss(logits_pos, targets_pos) loss_seg_bce = self.weights[2] * self.bce(logits_pos, targets_pos) else: loss_seg_dice = torch.tensor(0) loss_seg_bce = torch.tensor(0) loss = loss_fc + loss_seg_bce + loss_seg_dice return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_max_0(in_ptr0, 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 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] tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_per_fused__to_copy_gt_lt_sum_1(in_out_ptr0, in_ptr0, out_ptr0, out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 16 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, 0) tmp4 = tl.sum(tmp3, 1)[:, None] tmp5 = 0.0 tmp6 = tmp4 > tmp5 tmp7 = tmp6.to(tl.float32) tmp8 = 0.5 tmp9 = tmp7 < tmp8 tmp10 = tmp7 > tmp8 tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp7, xmask) tl.store(out_ptr0 + x0, tmp9, xmask) tl.store(out_ptr1 + x0, tmp10, xmask) @triton.jit def triton_per_fused_sum_2(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl. constexpr): RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.sum(tmp1, 1)[:, None] tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp3, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_per_fused_max_0[grid(16)](arg1_1, buf0, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) del arg1_1 buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) buf3 = buf2 del buf2 buf4 = empty_strided_cuda((4, 4), (4, 1), torch.bool) buf6 = empty_strided_cuda((4, 4), (4, 1), torch.bool) triton_per_fused__to_copy_gt_lt_sum_1[grid(16)](buf3, arg0_1, buf4, buf6, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 buf5 = empty_strided_cuda((), (), torch.float32) triton_per_fused_sum_2[grid(1)](buf3, buf5, 1, 16, XBLOCK=1, num_warps=2, num_stages=1) return buf0, buf4, buf3, buf5, buf6 class SoftDiceLoss(nn.Module): """Differentiable soft dice loss. Note: Sigmoid is automatically applied here! """ def __init__(self): super(SoftDiceLoss, self).__init__() def forward(self, logits, targets): eps = 1e-09 num = targets.size(0) probs = torch.sigmoid(logits) m1 = probs.view(num, -1) m2 = targets.view(num, -1).float() intersection = torch.sum(m1 * m2, 1) union = torch.sum(m1, dim=1) + torch.sum(m2, dim=1) score = (2 * intersection + eps) / (union + eps) score = (1 - score).mean() return score class MultiLabelDiceLoss(nn.Module): """The average dice across multiple classes. Note: Sigmoid is automatically applied here! """ def __init__(self): super(MultiLabelDiceLoss, self).__init__() self.dice_loss = SoftDiceLoss() def forward(self, logits, targets): loss = 0 num_classes = targets.size(1) for class_nr in range(num_classes): loss += self.dice_loss(logits[:, class_nr, :, :], targets[:, class_nr, :, :]) return loss / num_classes class ComboLoss(nn.Module): """Weighted classification and segmentation loss. Attributes: weights (list): activation: bce: with logits loss dice_loss: soft dice loss (all classes) """ def __init__(self, weights=[0.1, 0, 1], activation=None): """ Args: weights (list): [image_cls, pixel_seg, pixel_cls] activation: One of ['sigmoid', None] """ super(ComboLoss, self).__init__() self.weights = weights self.activation = activation assert self.activation in ['sigmoid', None ], "`activation` must be one of ['sigmoid', None]." self.bce = nn.BCEWithLogitsLoss(reduce=True) self.dice_loss = MultiLabelDiceLoss() def create_fc_tensors(self, logits, targets): """Creates the classification tensors from the segmentation ones. """ batch_size, num_classes, _, _ = targets.shape summed = targets.view(batch_size, num_classes, -1).sum(-1) targets_fc = (summed > 0).float() logits_fc = logits.view(batch_size, num_classes, -1) logits_fc = torch.max(logits_fc, -1)[0] return logits_fc, targets_fc def forward(self, logits, targets): logits_fc, targets_fc = self.create_fc_tensors(logits, targets) p = torch.sigmoid(logits) if self.activation == 'sigmoid' else logits if self.weights[0]: loss_fc = self.weights[0] * self.bce(logits_fc, targets_fc) else: loss_fc = torch.tensor(0) if self.weights[1] or self.weights[2]: loss_seg_dice = self.weights[1] * self.dice_loss(p, targets) loss_seg_bce = self.weights[2] * self.bce(logits, targets) else: loss_seg_dice = torch.tensor(0) loss_seg_bce = torch.tensor(0) loss = loss_fc + loss_seg_bce + loss_seg_dice return loss class ComboLossOnlyPosNew(ComboLoss): """Weighted classification and segmentation loss. (Considers only positive) This loss is only calculated on labels with ROIs in them to maximize the foreground class prediction capability. Attributes: weights (list): activation: bce: with logits loss dice_loss: soft dice loss (per class) """ def __init__(self, weights=[0.1, 0, 1], activation=None): """ Args: weights (list): [image_cls, pixel_seg, pixel_cls] activation: One of ['sigmoid', None] """ super().__init__(weights=weights, activation=activation) self.dice_loss = SoftDiceLoss() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
jchen42703/reproducing-cloud-3rd-place
ComboLossOnlyPos
false
6,936
[ "Apache-2.0" ]
1
25571f53efd48f68735d7fe2991e3ad783cbd4b1
https://github.com/jchen42703/reproducing-cloud-3rd-place/tree/25571f53efd48f68735d7fe2991e3ad783cbd4b1
MultiLabelDiceLoss
import torch import torch.nn as nn class SoftDiceLoss(nn.Module): """Differentiable soft dice loss. Note: Sigmoid is automatically applied here! """ def __init__(self): super(SoftDiceLoss, self).__init__() def forward(self, logits, targets): eps = 1e-09 num = targets.size(0) probs = torch.sigmoid(logits) m1 = probs.view(num, -1) m2 = targets.view(num, -1).float() intersection = torch.sum(m1 * m2, 1) union = torch.sum(m1, dim=1) + torch.sum(m2, dim=1) score = (2 * intersection + eps) / (union + eps) score = (1 - score).mean() return score class MultiLabelDiceLoss(nn.Module): """The average dice across multiple classes. Note: Sigmoid is automatically applied here! """ def __init__(self): super(MultiLabelDiceLoss, self).__init__() self.dice_loss = SoftDiceLoss() def forward(self, logits, targets): loss = 0 num_classes = targets.size(1) for class_nr in range(num_classes): loss += self.dice_loss(logits[:, class_nr, :, :], targets[:, class_nr, :, :]) return loss / num_classes 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) tmp2 = tl.load(in_ptr1 + (r1 + 64 * x0), xmask, other=0.0) tmp1 = tl.sigmoid(tmp0) tmp3 = tmp1 * tmp2 tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp10 = tl.where(xmask, tmp8, 0) tmp11 = tl.sum(tmp10, 1)[:, None] tmp12 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp14 = tl.where(xmask, tmp12, 0) tmp15 = tl.sum(tmp14, 1)[:, None] tl.store(out_ptr0 + x0, tmp7, xmask) tl.store(out_ptr1 + x0, tmp11, xmask) tl.store(out_ptr2 + x0, tmp15, 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) tmp2 = tl.load(in_ptr1 + (32 + r1 + 64 * x0), xmask, other=0.0) tmp1 = tl.sigmoid(tmp0) tmp3 = tmp1 * tmp2 tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp10 = tl.where(xmask, tmp8, 0) tmp11 = tl.sum(tmp10, 1)[:, None] tmp12 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp14 = tl.where(xmask, tmp12, 0) tmp15 = tl.sum(tmp14, 1)[:, None] tl.store(out_ptr0 + x0, tmp7, xmask) tl.store(out_ptr1 + x0, tmp11, xmask) tl.store(out_ptr2 + x0, tmp15, 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) tmp2 = tl.load(in_ptr1 + (48 + r1 + 64 * x0), xmask, other=0.0) tmp1 = tl.sigmoid(tmp0) tmp3 = tmp1 * tmp2 tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp10 = tl.where(xmask, tmp8, 0) tmp11 = tl.sum(tmp10, 1)[:, None] tmp12 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp14 = tl.where(xmask, tmp12, 0) tmp15 = tl.sum(tmp14, 1)[:, None] tl.store(out_ptr0 + x0, tmp7, xmask) tl.store(out_ptr1 + x0, tmp11, xmask) tl.store(out_ptr2 + x0, tmp15, 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) tmp2 = tl.load(in_ptr1 + (16 + r1 + 64 * x0), xmask, other=0.0) tmp1 = tl.sigmoid(tmp0) tmp3 = tmp1 * tmp2 tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp10 = tl.where(xmask, tmp8, 0) tmp11 = tl.sum(tmp10, 1)[:, None] tmp12 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp14 = tl.where(xmask, tmp12, 0) tmp15 = tl.sum(tmp14, 1)[:, None] tl.store(out_ptr0 + x0, tmp7, xmask) tl.store(out_ptr1 + x0, tmp11, xmask) tl.store(out_ptr2 + x0, tmp15, xmask) @triton.jit def triton_per_fused_add_div_mean_mul_rsub_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) tmp15 = tl.load(in_ptr3 + r0, None) tmp18 = tl.load(in_ptr4 + r0, None) tmp19 = tl.load(in_ptr5 + r0, None) tmp27 = tl.load(in_ptr6 + r0, None) tmp30 = tl.load(in_ptr7 + r0, None) tmp31 = tl.load(in_ptr8 + r0, None) tmp39 = tl.load(in_ptr9 + r0, None) tmp42 = tl.load(in_ptr10 + r0, None) tmp43 = tl.load(in_ptr11 + r0, None) tmp1 = 2.0 tmp2 = tmp0 * tmp1 tmp3 = 1e-09 tmp4 = tmp2 + tmp3 tmp7 = tmp5 + tmp6 tmp8 = tmp7 + tmp3 tmp9 = tmp4 / tmp8 tmp10 = 1.0 tmp11 = tmp10 - tmp9 tmp12 = tl.broadcast_to(tmp11, [XBLOCK, RBLOCK]) tmp14 = tl.sum(tmp12, 1)[:, None] tmp16 = tmp15 * tmp1 tmp17 = tmp16 + tmp3 tmp20 = tmp18 + tmp19 tmp21 = tmp20 + tmp3 tmp22 = tmp17 / tmp21 tmp23 = tmp10 - tmp22 tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK]) tmp26 = tl.sum(tmp24, 1)[:, None] tmp28 = tmp27 * tmp1 tmp29 = tmp28 + tmp3 tmp32 = tmp30 + tmp31 tmp33 = tmp32 + tmp3 tmp34 = tmp29 / tmp33 tmp35 = tmp10 - tmp34 tmp36 = tl.broadcast_to(tmp35, [XBLOCK, RBLOCK]) tmp38 = tl.sum(tmp36, 1)[:, None] tmp40 = tmp39 * tmp1 tmp41 = tmp40 + tmp3 tmp44 = tmp42 + tmp43 tmp45 = tmp44 + tmp3 tmp46 = tmp41 / tmp45 tmp47 = tmp10 - tmp46 tmp48 = tl.broadcast_to(tmp47, [XBLOCK, RBLOCK]) tmp50 = tl.sum(tmp48, 1)[:, None] tmp51 = 4.0 tmp52 = tmp14 / tmp51 tmp53 = 0.0 tmp54 = tmp52 + tmp53 tmp55 = tmp26 / tmp51 tmp56 = tmp54 + tmp55 tmp57 = tmp38 / tmp51 tmp58 = tmp56 + tmp57 tmp59 = tmp50 / tmp51 tmp60 = tmp58 + tmp59 tmp61 = 0.25 tmp62 = tmp60 * tmp61 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp62, 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_mean_mul_rsub_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 SoftDiceLoss(nn.Module): """Differentiable soft dice loss. Note: Sigmoid is automatically applied here! """ def __init__(self): super(SoftDiceLoss, self).__init__() def forward(self, logits, targets): eps = 1e-09 num = targets.size(0) probs = torch.sigmoid(logits) m1 = probs.view(num, -1) m2 = targets.view(num, -1).float() intersection = torch.sum(m1 * m2, 1) union = torch.sum(m1, dim=1) + torch.sum(m2, dim=1) score = (2 * intersection + eps) / (union + eps) score = (1 - score).mean() return score class MultiLabelDiceLossNew(nn.Module): """The average dice across multiple classes. Note: Sigmoid is automatically applied here! """ def __init__(self): super(MultiLabelDiceLossNew, self).__init__() self.dice_loss = SoftDiceLoss() def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
jchen42703/reproducing-cloud-3rd-place
MultiLabelDiceLoss
false
6,937
[ "Apache-2.0" ]
1
25571f53efd48f68735d7fe2991e3ad783cbd4b1
https://github.com/jchen42703/reproducing-cloud-3rd-place/tree/25571f53efd48f68735d7fe2991e3ad783cbd4b1
ConvPlus
import torch import torch.nn as nn import torch.utils.data class ConvPlus(nn.Module): def __init__(self, c1, c2, k=3, s=1, g=1, bias=True): super(ConvPlus, self).__init__() self.cv1 = nn.Conv2d(c1, c2, (k, 1), s, (k // 2, 0), groups=g, bias =bias) self.cv2 = nn.Conv2d(c1, c2, (1, k), s, (0, k // 2), groups=g, bias =bias) def forward(self, x): return self.cv1(x) + self.cv2(x) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'c1': 4, 'c2': 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.utils.data 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, in_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x3, xmask) tmp4 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp5 = tmp3 + tmp4 tmp6 = tmp2 + tmp5 tl.store(in_out_ptr0 + x3, tmp6, xmask) def call(args): primals_1, primals_2, primals_3, primals_4, primals_5 = args args.clear() assert_size_stride(primals_1, (4, 4, 3, 1), (12, 3, 1, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4, 1, 3), (12, 3, 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, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1)) buf1 = extern_kernels.convolution(primals_3, primals_4, stride=(1, 1), padding=(0, 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 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_add_convolution_0[grid(256)](buf2, primals_2, buf1, primals_5, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf1 del primals_2 del primals_5 return buf2, primals_1, primals_3, primals_4 class ConvPlusNew(nn.Module): def __init__(self, c1, c2, k=3, s=1, g=1, bias=True): super(ConvPlusNew, self).__init__() self.cv1 = nn.Conv2d(c1, c2, (k, 1), s, (k // 2, 0), groups=g, bias =bias) self.cv2 = nn.Conv2d(c1, c2, (1, k), s, (0, k // 2), groups=g, bias =bias) def forward(self, input_0): primals_1 = self.cv1.weight primals_2 = self.cv1.bias primals_4 = self.cv2.weight primals_5 = self.cv2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
jiangbestone/detect_rcnn
ConvPlus
false
6,938
[ "MIT" ]
1
41c4f4d3f8409cc146314c41a3d02ceafa9a7477
https://github.com/jiangbestone/detect_rcnn/tree/41c4f4d3f8409cc146314c41a3d02ceafa9a7477
PredictionHeadTransform
import math import torch import typing from torch import nn from torch.nn import LayerNorm def gelu(x: 'torch.Tensor') ->torch.Tensor: return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0))) def swish(x: 'torch.Tensor') ->torch.Tensor: return x * torch.sigmoid(x) def get_activation_fn(name: 'str') ->typing.Callable: if name == 'gelu': return gelu elif name == 'relu': return nn.functional.relu elif name == 'swish': return swish else: raise ValueError(f'Unrecognized activation fn: {name}') class PredictionHeadTransform(nn.Module): def __init__(self, hidden_size: 'int', input_size: 'int', hidden_act: 'typing.Union[str, typing.Callable]'='gelu', layer_norm_eps: 'float'=1e-12): super().__init__() self.dense = nn.Linear(input_size, hidden_size) if isinstance(hidden_act, str): self.transform_act_fn = get_activation_fn(hidden_act) else: self.transform_act_fn = hidden_act self.LayerNorm = LayerNorm(hidden_size, eps=layer_norm_eps) def forward(self, hidden_states: 'torch.Tensor') ->torch.Tensor: hidden_states = self.dense(hidden_states) hidden_states = self.transform_act_fn(hidden_states) hidden_states = self.LayerNorm(hidden_states) return hidden_states def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'hidden_size': 4, 'input_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import math import typing from torch import nn from torch.nn import LayerNorm assert_size_stride = torch._C._dynamo.guards.assert_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_mul_native_layer_norm_0(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') 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_native_layer_norm_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp9 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp16 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp18 = tl.load(in_ptr4 + 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 = tmp8 - tmp9 tmp12 = 1e-12 tmp13 = tmp11 + tmp12 tmp14 = libdevice.rsqrt(tmp13) tmp15 = tmp10 * tmp14 tmp17 = tmp15 * tmp16 tmp19 = tmp17 + tmp18 tl.store(out_ptr0 + x2, tmp19, 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,), (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((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_mul_native_layer_norm_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_native_layer_norm_1[grid(256)](buf0, buf1, buf2, primals_4, primals_5, buf3, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf1 del buf2 del primals_5 return buf3, primals_4, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf0 def gelu(x: 'torch.Tensor') ->torch.Tensor: return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0))) def swish(x: 'torch.Tensor') ->torch.Tensor: return x * torch.sigmoid(x) def get_activation_fn(name: 'str') ->typing.Callable: if name == 'gelu': return gelu elif name == 'relu': return nn.functional.relu elif name == 'swish': return swish else: raise ValueError(f'Unrecognized activation fn: {name}') class PredictionHeadTransformNew(nn.Module): def __init__(self, hidden_size: 'int', input_size: 'int', hidden_act: 'typing.Union[str, typing.Callable]'='gelu', layer_norm_eps: 'float'=1e-12): super().__init__() self.dense = nn.Linear(input_size, hidden_size) if isinstance(hidden_act, str): self.transform_act_fn = get_activation_fn(hidden_act) else: self.transform_act_fn = hidden_act self.LayerNorm = LayerNorm(hidden_size, eps=layer_norm_eps) def forward(self, input_0): primals_1 = self.dense.weight primals_2 = self.dense.bias primals_4 = self.LayerNorm.weight primals_5 = self.LayerNorm.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0]
jgoodson/TraGeC
PredictionHeadTransform
false
6,939
[ "BSD-3-Clause" ]
1
3370e29ba0639745055cbee726a40181a4dd61df
https://github.com/jgoodson/TraGeC/tree/3370e29ba0639745055cbee726a40181a4dd61df
AddCoords
import torch import torch.nn as nn class AddCoords(nn.Module): def __init__(self, with_r=False): super().__init__() self.with_r = with_r def forward(self, input_tensor): """ Args: input_tensor: shape(batch, channel, x_dim, y_dim) """ batch_size, _, x_dim, y_dim = input_tensor.size() xx_channel = torch.arange(x_dim).repeat(1, y_dim, 1) yy_channel = torch.arange(y_dim).repeat(1, x_dim, 1).transpose(1, 2) xx_channel = xx_channel.float() / (x_dim - 1) yy_channel = yy_channel.float() / (y_dim - 1) xx_channel = xx_channel * 2 - 1 yy_channel = yy_channel * 2 - 1 xx_channel = xx_channel.repeat(batch_size, 1, 1, 1).transpose(2, 3) yy_channel = yy_channel.repeat(batch_size, 1, 1, 1).transpose(2, 3) ret = torch.cat([input_tensor, xx_channel.type_as(input_tensor), yy_channel.type_as(input_tensor)], dim=1) if self.with_r: rr = torch.sqrt(torch.pow(xx_channel.type_as(input_tensor) - 0.5, 2) + torch.pow(yy_channel.type_as(input_tensor) - 0.5, 2)) ret = torch.cat([ret, rr], dim=1) return ret 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_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 384 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex // 16 % 6 x3 = xindex // 96 x4 = xindex % 16 x1 = xindex // 4 % 4 x0 = xindex % 4 x5 = xindex tmp0 = x2 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 4, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x4 + 16 * x2 + 64 * x3), tmp4 & xmask, other=0.0) tmp6 = tmp0 >= tmp3 tmp7 = tl.full([1], 5, tl.int64) tmp8 = tmp0 < tmp7 tmp9 = tmp6 & tmp8 tmp10 = x1 tmp11 = tmp10.to(tl.float32) tmp12 = 0.3333333333333333 tmp13 = tmp11 * tmp12 tmp14 = 2.0 tmp15 = tmp13 * tmp14 tmp16 = 1.0 tmp17 = tmp15 - tmp16 tmp18 = tl.full(tmp17.shape, 0.0, tmp17.dtype) tmp19 = tl.where(tmp9, tmp17, tmp18) tmp20 = tmp0 >= tmp7 tl.full([1], 6, tl.int64) tmp23 = x0 tmp24 = tmp23.to(tl.float32) tmp25 = tmp24 * tmp12 tmp26 = tmp25 * tmp14 tmp27 = tmp26 - tmp16 tmp28 = tl.full(tmp27.shape, 0.0, tmp27.dtype) tmp29 = tl.where(tmp20, tmp27, tmp28) tmp30 = tl.where(tmp9, tmp19, tmp29) tmp31 = tl.where(tmp4, tmp5, tmp30) tl.store(out_ptr0 + x5, tmp31, 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, 6, 4, 4), (96, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(384)](arg0_1, buf0, 384, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 return buf0, class AddCoordsNew(nn.Module): def __init__(self, with_r=False): super().__init__() self.with_r = with_r def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
jiangxiluning/TCPN
AddCoords
false
6,940
[ "Apache-2.0" ]
1
916bd8455be5c784068b7bb5bd6226da3f2d95c7
https://github.com/jiangxiluning/TCPN/tree/916bd8455be5c784068b7bb5bd6226da3f2d95c7
NegativeCosineSimilarity
import torch import torch.nn.functional as F class NegativeCosineSimilarity(torch.nn.Module): """Implementation of the Negative Cosine Simililarity used in the SimSiam[0] paper. [0] SimSiam, 2020, https://arxiv.org/abs/2011.10566 Examples: >>> # initialize loss function >>> loss_fn = NegativeCosineSimilarity() >>> >>> # generate two representation tensors >>> # with batch size 10 and dimension 128 >>> x0 = torch.randn(10, 128) >>> x1 = torch.randn(10, 128) >>> >>> # calculate loss >>> loss = loss_fn(x0, x1) """ def __init__(self, dim: 'int'=1, eps: 'float'=1e-08) ->None: """Same parameters as in torch.nn.CosineSimilarity. Args: dim (int, optional): Dimension where cosine similarity is computed. Default: 1 eps (float, optional): Small value to avoid division by zero. Default: 1e-8 """ super().__init__() self.dim = dim self.eps = eps def forward(self, x0: 'torch.Tensor', x1: 'torch.Tensor') ->torch.Tensor: return -F.cosine_similarity(x0, x1, self.dim, self.eps).mean() def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice 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_min_div_linalg_vector_norm_mul_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp16 = tl.load(in_ptr1 + x3, xmask) tmp17 = tl.load(in_ptr1 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp19 = tl.load(in_ptr1 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp22 = tl.load(in_ptr1 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp25 = tl.load(in_ptr1 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = libdevice.sqrt(tmp11) tmp13 = 1e-08 tmp14 = triton_helpers.maximum(tmp12, tmp13) tmp15 = tmp0 / tmp14 tmp18 = tmp17 * tmp17 tmp20 = tmp19 * tmp19 tmp21 = tmp18 + tmp20 tmp23 = tmp22 * tmp22 tmp24 = tmp21 + tmp23 tmp26 = tmp25 * tmp25 tmp27 = tmp24 + tmp26 tmp28 = libdevice.sqrt(tmp27) tmp29 = triton_helpers.maximum(tmp28, tmp13) tmp30 = tmp16 / tmp29 tmp31 = tmp15 * tmp30 tl.store(out_ptr0 + x3, tmp31, xmask) @triton.jit def triton_per_fused_mean_neg_sum_1(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 64 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex % 16 r1 = rindex // 16 tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None) tmp1 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None) tmp3 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None) tmp5 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK]) tmp9 = tl.sum(tmp7, 1)[:, None] tmp10 = 64.0 tmp11 = tmp9 / tmp10 tmp12 = -tmp11 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp12, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_0[grid(256)]( arg1_1, arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 del arg1_1 buf1 = empty_strided_cuda((), (), torch.float32) buf2 = buf1 del buf1 triton_per_fused_mean_neg_sum_1[grid(1)](buf2, buf0, 1, 64, XBLOCK= 1, num_warps=2, num_stages=1) del buf0 return buf2, class NegativeCosineSimilarityNew(torch.nn.Module): """Implementation of the Negative Cosine Simililarity used in the SimSiam[0] paper. [0] SimSiam, 2020, https://arxiv.org/abs/2011.10566 Examples: >>> # initialize loss function >>> loss_fn = NegativeCosineSimilarity() >>> >>> # generate two representation tensors >>> # with batch size 10 and dimension 128 >>> x0 = torch.randn(10, 128) >>> x1 = torch.randn(10, 128) >>> >>> # calculate loss >>> loss = loss_fn(x0, x1) """ def __init__(self, dim: 'int'=1, eps: 'float'=1e-08) ->None: """Same parameters as in torch.nn.CosineSimilarity. Args: dim (int, optional): Dimension where cosine similarity is computed. Default: 1 eps (float, optional): Small value to avoid division by zero. Default: 1e-8 """ super().__init__() self.dim = dim self.eps = eps def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
jianzhnie/self_supervised
NegativeCosineSimilarity
false
6,941
[ "Apache-2.0" ]
1
d1e0f31ab032150ab0ad007c1e19773135a5fb79
https://github.com/jianzhnie/self_supervised/tree/d1e0f31ab032150ab0ad007c1e19773135a5fb79
Net
import torch import torch.utils.data import torch.utils.data.distributed 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, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.conv2_drop = nn.Dropout2d() self.fc1 = nn.Linear(320, 50) self.fc2 = nn.Linear(50, 10) def forward(self, x): x = x.view(x.shape[0], 1, 28, 28) x = F.relu(F.max_pool2d(self.conv1(x), 2)) x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) x = x.view(-1, 320) x = F.relu(self.fc1(x)) x = F.dropout(x, training=self.training) x = self.fc2(x) return F.log_softmax(x, dim=1) def get_inputs(): return [torch.rand([4, 1, 28, 28])] 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.utils.data import torch.utils.data.distributed import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_convolution_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 23040 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 576 % 10 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) @triton.jit def triton_poi_fused_max_pool2d_with_indices_relu_1(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 5760 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 12 x3 = xindex // 12 x2 = xindex // 1440 x4 = xindex % 1440 x5 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 48 * x3), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 48 * x3), xmask, eviction_policy ='evict_last') tmp7 = tl.load(in_ptr0 + (24 + 2 * x0 + 48 * x3), xmask, eviction_policy='evict_last') tmp12 = tl.load(in_ptr0 + (25 + 2 * x0 + 48 * x3), xmask, eviction_policy='evict_last') tmp2 = tmp1 > tmp0 tmp3 = tl.full([1], 1, tl.int8) tmp4 = tl.full([1], 0, tl.int8) tmp5 = tl.where(tmp2, tmp3, tmp4) tmp6 = triton_helpers.maximum(tmp1, tmp0) tmp8 = tmp7 > tmp6 tmp9 = tl.full([1], 2, tl.int8) tmp10 = tl.where(tmp8, tmp9, tmp5) tmp11 = triton_helpers.maximum(tmp7, tmp6) tmp13 = tmp12 > tmp11 tmp14 = tl.full([1], 3, tl.int8) tmp15 = tl.where(tmp13, tmp14, tmp10) tmp16 = triton_helpers.maximum(tmp12, tmp11) tmp17 = tl.full([1], 0, tl.int32) tmp18 = triton_helpers.maximum(tmp17, tmp16) tl.store(out_ptr0 + (x4 + 1536 * x2), tmp15, xmask) tl.store(out_ptr1 + x5, tmp18, xmask) @triton.jit def triton_poi_fused_convolution_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 5120 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 64 % 20 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) @triton.jit def triton_poi_fused_max_pool2d_with_indices_relu_threshold_backward_3(in_ptr0, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr): xnumel = 1280 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 + (2 * x0 + 16 * x1), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 16 * x1), xmask, eviction_policy ='evict_last') tmp7 = tl.load(in_ptr0 + (8 + 2 * x0 + 16 * x1), xmask, eviction_policy ='evict_last') tmp12 = tl.load(in_ptr0 + (9 + 2 * x0 + 16 * x1), xmask, eviction_policy='evict_last') tmp2 = tmp1 > tmp0 tmp3 = tl.full([1], 1, tl.int8) tmp4 = tl.full([1], 0, tl.int8) tmp5 = tl.where(tmp2, tmp3, tmp4) tmp6 = triton_helpers.maximum(tmp1, tmp0) tmp8 = tmp7 > tmp6 tmp9 = tl.full([1], 2, tl.int8) tmp10 = tl.where(tmp8, tmp9, tmp5) tmp11 = triton_helpers.maximum(tmp7, tmp6) tmp13 = tmp12 > tmp11 tmp14 = tl.full([1], 3, tl.int8) tmp15 = tl.where(tmp13, tmp14, tmp10) tmp16 = triton_helpers.maximum(tmp12, tmp11) tmp17 = tl.full([1], 0, tl.int32) tmp18 = triton_helpers.maximum(tmp17, tmp16) tmp19 = 0.0 tmp20 = tmp18 <= tmp19 tl.store(out_ptr0 + x2, tmp15, xmask) tl.store(out_ptr1 + x2, tmp18, xmask) tl.store(out_ptr2 + x2, tmp20, xmask) @triton.jit def triton_poi_fused_relu_4(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 200 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 50 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_per_fused__log_softmax_5(in_ptr0, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 4 rnumel = 10 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] rmask = rindex < rnumel r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 10 * x0), rmask & xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(rmask & xmask, tmp1, float('-inf')) tmp4 = triton_helpers.max2(tmp3, 1)[:, None] tmp5 = tmp0 - tmp4 tmp6 = tl_math.exp(tmp5) tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK]) tmp9 = tl.where(rmask & xmask, tmp7, 0) tmp10 = tl.sum(tmp9, 1)[:, None] tmp11 = tl_math.log(tmp10) tmp12 = tmp5 - tmp11 tl.store(out_ptr2 + (r1 + 10 * x0), tmp12, rmask & xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9) = args args.clear() assert_size_stride(primals_1, (4, 1, 28, 28), (784, 784, 28, 1)) assert_size_stride(primals_2, (10, 1, 5, 5), (25, 25, 5, 1)) assert_size_stride(primals_3, (10,), (1,)) assert_size_stride(primals_4, (20, 10, 5, 5), (250, 25, 5, 1)) assert_size_stride(primals_5, (20,), (1,)) assert_size_stride(primals_6, (50, 320), (320, 1)) assert_size_stride(primals_7, (50,), (1,)) assert_size_stride(primals_8, (10, 50), (50, 1)) assert_size_stride(primals_9, (10,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 10, 24, 24), (5760, 576, 24, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_0[grid(23040)](buf1, primals_3, 23040, XBLOCK=128, num_warps=4, num_stages=1) del primals_3 buf2 = empty_strided_cuda((4, 10, 12, 12), (1536, 144, 12, 1), torch.int8) buf3 = empty_strided_cuda((4, 10, 12, 12), (1440, 144, 12, 1), torch.float32) triton_poi_fused_max_pool2d_with_indices_relu_1[grid(5760)](buf1, buf2, buf3, 5760, XBLOCK=128, num_warps=4, num_stages=1) buf4 = extern_kernels.convolution(buf3, primals_4, stride=(1, 1), padding=(0, 0), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf4, (4, 20, 8, 8), (1280, 64, 8, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_2[grid(5120)](buf5, primals_5, 5120, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf6 = empty_strided_cuda((4, 20, 4, 4), (320, 16, 4, 1), torch.int8) buf7 = empty_strided_cuda((4, 20, 4, 4), (320, 16, 4, 1), torch.float32 ) buf14 = empty_strided_cuda((4, 20, 4, 4), (320, 16, 4, 1), torch.bool) triton_poi_fused_max_pool2d_with_indices_relu_threshold_backward_3[grid (1280)](buf5, buf6, buf7, buf14, 1280, XBLOCK=128, num_warps=4, num_stages=1) buf8 = empty_strided_cuda((4, 50), (50, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf7, (4, 320), (320, 1), 0), reinterpret_tensor(primals_6, (320, 50), (1, 320), 0), out=buf8) buf9 = buf8 del buf8 triton_poi_fused_relu_4[grid(200)](buf9, primals_7, 200, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 buf10 = empty_strided_cuda((4, 10), (10, 1), torch.float32) extern_kernels.addmm(primals_9, buf9, reinterpret_tensor(primals_8, (50, 10), (1, 50), 0), alpha=1, beta=1, out=buf10) del primals_9 buf13 = empty_strided_cuda((4, 10), (10, 1), torch.float32) triton_per_fused__log_softmax_5[grid(4)](buf10, buf13, 4, 10, XBLOCK=1, num_warps=2, num_stages=1) del buf10 return (buf13, primals_1, primals_2, primals_4, buf1, buf2, buf3, buf5, buf6, reinterpret_tensor(buf7, (4, 320), (320, 1), 0), buf9, buf13, primals_8, primals_6, buf14) class NetNew(nn.Module): def __init__(self): super(NetNew, self).__init__() self.conv1 = nn.Conv2d(1, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.conv2_drop = nn.Dropout2d() self.fc1 = nn.Linear(320, 50) self.fc2 = nn.Linear(50, 10) def forward(self, input_0): primals_2 = self.conv1.weight primals_3 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_6 = self.fc1.weight primals_7 = self.fc1.bias primals_8 = self.fc2.weight primals_9 = self.fc2.bias primals_1 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9]) return output[0]
jesterhazy/sagemaker-pytorch-container
Net
false
6,942
[ "Apache-2.0" ]
1
2eb4ba9216e5d72cd4d61eadc173764a41dea6b9
https://github.com/jesterhazy/sagemaker-pytorch-container/tree/2eb4ba9216e5d72cd4d61eadc173764a41dea6b9
GCNModelVAE
import torch import torch.nn as nn from torch.nn.parameter import Parameter class GraphConvolution(nn.Module): def __init__(self, input_dim, output_dim, dropout, bias=False): super(GraphConvolution, self).__init__() self.input_dim = input_dim self.output_dim = output_dim self.weight = Parameter(torch.FloatTensor(input_dim, output_dim)) self.reset_parameters() if bias: self.bias = Parameter(torch.FloatTensor(output_dim)) nn.init.zeros_(self.bias) else: self.register_parameter('bias', None) self.dropout = nn.Dropout(dropout) def reset_parameters(self): nn.init.xavier_uniform_(self.weight) def forward(self, input, adj): input = self.dropout(input) support = torch.mm(input, self.weight) output = torch.spmm(adj, support) if self.bias is not None: output = output + self.bias return output class InnerProductDecoder(nn.Module): """ 内积用来做decoder,用来生成邻接矩阵 """ def __init__(self, dropout): super(InnerProductDecoder, self).__init__() self.dropout = nn.Dropout(dropout) def forward(self, z): z = self.dropout(z) adj = torch.mm(z, z.t()) return adj class GCNModelVAE(nn.Module): def __init__(self, input_feat_dim, hidden_dim1, hidden_dim2, dropout, vae_bool=True): super(GCNModelVAE, self).__init__() self.vae_bool = vae_bool self.gc1 = GraphConvolution(input_feat_dim, hidden_dim1, dropout) self.gc2 = GraphConvolution(hidden_dim1, hidden_dim2, dropout) self.gc3 = GraphConvolution(hidden_dim1, hidden_dim2, dropout) self.ip = InnerProductDecoder(dropout) self.relu = nn.ReLU() def encode(self, input, adj): hidden1 = self.relu(self.gc1(input, adj)) return self.gc2(hidden1, adj), self.gc3(hidden1, adj) def reparameterize(self, mu, logvar): if self.vae_bool: std = torch.exp(logvar) eps = torch.randn_like(std) return eps.mul(std).add_(mu) else: return mu def forward(self, input, adj): mu, logvar = self.encode(input, adj) z = self.reparameterize(mu, logvar) return self.ip(z), mu, logvar def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'input_feat_dim': 4, 'hidden_dim1': 4, 'hidden_dim2': 4, 'dropout': 0.5}]
import torch from torch import device from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math import torch.nn as nn from 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_relu_0(in_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_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_add_exp_mul_1(in_ptr0, in_ptr1, in_ptr2, 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_ptr1 + x0, xmask) tmp4 = tl.load(in_ptr2 + x0, xmask) tmp2 = tl_math.exp(tmp1) tmp3 = tmp0 * tmp2 tmp5 = tmp3 + tmp4 tl.store(out_ptr0 + x0, tmp5, 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, primals_2, out=buf0) del primals_2 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf0, out=buf1) buf2 = buf1 del buf1 get_raw_stream(0) triton_poi_fused_relu_0[grid(16)](buf2, 16, XBLOCK=16, num_warps=1, num_stages=1) buf3 = buf0 del buf0 extern_kernels.mm(buf2, primals_4, out=buf3) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf3, out=buf4) buf5 = buf3 del buf3 extern_kernels.mm(buf2, primals_5, out=buf5) buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_3, buf5, out=buf6) buf7 = torch.ops.aten.randn.default([4, 4], dtype=torch.float32, device=device(type='cuda', index=0), pin_memory=False) buf8 = buf7 del buf7 buf9 = buf5 del buf5 triton_poi_fused_add_exp_mul_1[grid(16)](buf8, buf6, buf4, buf9, 16, XBLOCK=16, num_warps=1, num_stages=1) buf10 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(buf9, reinterpret_tensor(buf9, (4, 4), (1, 4), 0), out=buf10) return buf10, buf4, buf6, buf2, buf6, buf8, buf9, reinterpret_tensor( primals_3, (4, 4), (1, 4), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0 ), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0) class GraphConvolution(nn.Module): def __init__(self, input_dim, output_dim, dropout, bias=False): super(GraphConvolution, self).__init__() self.input_dim = input_dim self.output_dim = output_dim self.weight = Parameter(torch.FloatTensor(input_dim, output_dim)) self.reset_parameters() if bias: self.bias = Parameter(torch.FloatTensor(output_dim)) nn.init.zeros_(self.bias) else: self.register_parameter('bias', None) self.dropout = nn.Dropout(dropout) def reset_parameters(self): nn.init.xavier_uniform_(self.weight) def forward(self, input, adj): input = self.dropout(input) support = torch.mm(input, self.weight) output = torch.spmm(adj, support) if self.bias is not None: output = output + self.bias return output class InnerProductDecoder(nn.Module): """ 内积用来做decoder,用来生成邻接矩阵 """ def __init__(self, dropout): super(InnerProductDecoder, self).__init__() self.dropout = nn.Dropout(dropout) def forward(self, z): z = self.dropout(z) adj = torch.mm(z, z.t()) return adj class GCNModelVAENew(nn.Module): def __init__(self, input_feat_dim, hidden_dim1, hidden_dim2, dropout, vae_bool=True): super(GCNModelVAENew, self).__init__() self.vae_bool = vae_bool self.gc1 = GraphConvolution(input_feat_dim, hidden_dim1, dropout) self.gc2 = GraphConvolution(hidden_dim1, hidden_dim2, dropout) self.gc3 = GraphConvolution(hidden_dim1, hidden_dim2, dropout) self.ip = InnerProductDecoder(dropout) self.relu = nn.ReLU() def encode(self, input, adj): hidden1 = self.relu(self.gc1(input, adj)) return self.gc2(hidden1, adj), self.gc3(hidden1, adj) def reparameterize(self, mu, logvar): if self.vae_bool: std = torch.exp(logvar) eps = torch.randn_like(std) return eps.mul(std).add_(mu) else: return mu def forward(self, input_0, input_1): primals_1 = self.gc1.weight primals_2 = self.gc2.weight primals_3 = self.gc3.weight primals_4 = input_0 primals_5 = input_1 output = call([primals_1, primals_2, primals_3, primals_4, primals_5]) return output[0], output[1], output[2]
jiangnanboy/gcn_for_prediction_of_protein_interactions
GCNModelVAE
false
6,943
[ "Apache-2.0" ]
1
b2a9eb06cdfe0971d0c352299db1075ec4827dd9
https://github.com/jiangnanboy/gcn_for_prediction_of_protein_interactions/tree/b2a9eb06cdfe0971d0c352299db1075ec4827dd9
GraphAttentionLayer
import torch import torch.nn as nn from torch.nn.parameter import Parameter import torch.nn.functional as F class GraphAttentionLayer(nn.Module): def __init__(self, input_dim, output_dim, dropout, alpha): super(GraphAttentionLayer, self).__init__() self.input_dim = input_dim self.output_dim = output_dim self.weight = Parameter(torch.FloatTensor(input_dim, output_dim)) self.a = Parameter(torch.FloatTensor(2 * output_dim, 1)) self.reset_parameters() self.dropout = nn.Dropout(dropout) self.leakyrelu = nn.LeakyReLU(alpha) self.elu = nn.ELU() def reset_parameters(self): nn.init.xavier_uniform_(self.weight) nn.init.xavier_normal_(self.a) def forward(self, input, adj): Wh = torch.mm(input, self.weight) Wh1 = torch.matmul(Wh, self.a[:self.output_dim, :]) Wh2 = torch.matmul(Wh, self.a[self.output_dim:, :]) e = Wh1 + Wh2.T e = self.leakyrelu(e) zero_vec = -9000000000000000.0 * torch.ones_like(e) attention = torch.where(adj > 0, e, zero_vec) attention = F.softmax(attention, dim=-1) attention = self.dropout(attention) attention_wh = torch.matmul(attention, Wh) return self.elu(attention_wh) def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {'input_dim': 4, 'output_dim': 4, 'dropout': 0.5, 'alpha': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn from torch.nn.parameter import Parameter assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_leaky_relu_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 4 x0 = xindex % 4 x2 = xindex tmp0 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tl.store(out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_gt_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.0 tmp2 = tmp0 > tmp1 tl.store(out_ptr0 + x0, tmp2, xmask) @triton.jit def triton_poi_fused__softmax_add_leaky_relu_mul_where_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 4 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last').to(tl .int1) tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last').to(tl .int1) tmp2 = tl.load(in_ptr2 + x0, xmask) tmp3 = tl.load(in_ptr3 + 0) tmp4 = tl.broadcast_to(tmp3, [XBLOCK]) tmp11 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp12 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp13 = tl.load(in_ptr3 + 1) tmp14 = tl.broadcast_to(tmp13, [XBLOCK]) tmp20 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp21 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp22 = tl.load(in_ptr3 + 2) tmp23 = tl.broadcast_to(tmp22, [XBLOCK]) tmp29 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp30 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last' ).to(tl.int1) tmp31 = tl.load(in_ptr3 + 3) tmp32 = tl.broadcast_to(tmp31, [XBLOCK]) tmp5 = tmp2 + tmp4 tmp6 = 4.0 tmp7 = tmp5 * tmp6 tmp8 = tl.where(tmp1, tmp5, tmp7) tmp9 = -8999999815811072.0 tmp10 = tl.where(tmp0, tmp8, tmp9) tmp15 = tmp2 + tmp14 tmp16 = tmp15 * tmp6 tmp17 = tl.where(tmp12, tmp15, tmp16) tmp18 = tl.where(tmp11, tmp17, tmp9) tmp19 = triton_helpers.maximum(tmp10, tmp18) tmp24 = tmp2 + tmp23 tmp25 = tmp24 * tmp6 tmp26 = tl.where(tmp21, tmp24, tmp25) tmp27 = tl.where(tmp20, tmp26, tmp9) tmp28 = triton_helpers.maximum(tmp19, tmp27) tmp33 = tmp2 + tmp32 tmp34 = tmp33 * tmp6 tmp35 = tl.where(tmp30, tmp33, tmp34) tmp36 = tl.where(tmp29, tmp35, tmp9) tmp37 = triton_helpers.maximum(tmp28, tmp36) tl.store(out_ptr0 + x0, tmp37, xmask) @triton.jit def triton_poi_fused__softmax_add_leaky_relu_mul_where_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask).to(tl.int1) tmp1 = tl.load(in_ptr1 + x2, xmask).to(tl.int1) tmp2 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp10 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last') tmp4 = tmp2 + tmp3 tmp5 = 4.0 tmp6 = tmp4 * tmp5 tmp7 = tl.where(tmp1, tmp4, tmp6) tmp8 = -8999999815811072.0 tmp9 = tl.where(tmp0, tmp7, tmp8) tmp11 = tmp9 - tmp10 tmp12 = tl_math.exp(tmp11) tl.store(out_ptr0 + x2, tmp12, xmask) @triton.jit def triton_poi_fused__softmax_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused_elu_5(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.0 tmp2 = tmp0 > tmp1 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 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4, 4), (4, 1)) assert_size_stride(primals_3, (8, 1), (1, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(primals_2, primals_1, out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 1), (1, 1), torch.float32) extern_kernels.mm(buf0, reinterpret_tensor(primals_3, (4, 1), (1, 1 ), 0), out=buf1) buf2 = empty_strided_cuda((4, 1), (1, 1), torch.float32) extern_kernels.mm(buf0, reinterpret_tensor(primals_3, (4, 1), (1, 1 ), 4), out=buf2) buf3 = empty_strided_cuda((4, 4), (4, 1), torch.bool) get_raw_stream(0) triton_poi_fused_add_leaky_relu_0[grid(16)](buf1, buf2, buf3, 16, XBLOCK=16, num_warps=1, num_stages=1) buf4 = empty_strided_cuda((4, 4), (4, 1), torch.bool) triton_poi_fused_gt_1[grid(16)](primals_4, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1) del primals_4 buf5 = empty_strided_cuda((4, 1), (1, 4), torch.float32) triton_poi_fused__softmax_add_leaky_relu_mul_where_2[grid(4)](buf4, buf3, buf1, buf2, buf5, 4, XBLOCK=4, num_warps=1, num_stages=1) buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused__softmax_add_leaky_relu_mul_where_3[grid(16)](buf4, buf3, buf1, buf2, buf5, buf6, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf1 del buf2 del buf5 buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused__softmax_4[grid(16)](buf6, buf7, 16, XBLOCK=16, num_warps=1, num_stages=1) buf8 = buf6 del buf6 extern_kernels.mm(buf7, buf0, out=buf8) buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_elu_5[grid(16)](buf8, buf9, 16, XBLOCK=16, num_warps=1, num_stages=1) return buf9, buf3, buf4, buf7, buf8, reinterpret_tensor(buf0, (4, 4), ( 1, 4), 0), reinterpret_tensor(primals_3, (1, 4), (1, 1), 4 ), reinterpret_tensor(primals_3, (1, 4), (1, 1), 0 ), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0) class GraphAttentionLayerNew(nn.Module): def __init__(self, input_dim, output_dim, dropout, alpha): super(GraphAttentionLayerNew, self).__init__() self.input_dim = input_dim self.output_dim = output_dim self.weight = Parameter(torch.FloatTensor(input_dim, output_dim)) self.a = Parameter(torch.FloatTensor(2 * output_dim, 1)) self.reset_parameters() self.dropout = nn.Dropout(dropout) self.leakyrelu = nn.LeakyReLU(alpha) self.elu = nn.ELU() def reset_parameters(self): nn.init.xavier_uniform_(self.weight) nn.init.xavier_normal_(self.a) def forward(self, input_0, input_1): primals_1 = self.weight primals_3 = self.a primals_2 = input_0 primals_4 = input_1 output = call([primals_1, primals_2, primals_3, primals_4]) return output[0]
jiangnanboy/gcn_for_prediction_of_protein_interactions
GraphAttentionLayer
false
6,944
[ "Apache-2.0" ]
1
b2a9eb06cdfe0971d0c352299db1075ec4827dd9
https://github.com/jiangnanboy/gcn_for_prediction_of_protein_interactions/tree/b2a9eb06cdfe0971d0c352299db1075ec4827dd9
ComboLoss
import torch import torch.nn as nn class SoftDiceLoss(nn.Module): """Differentiable soft dice loss. Note: Sigmoid is automatically applied here! """ def __init__(self): super(SoftDiceLoss, self).__init__() def forward(self, logits, targets): eps = 1e-09 num = targets.size(0) probs = torch.sigmoid(logits) m1 = probs.view(num, -1) m2 = targets.view(num, -1).float() intersection = torch.sum(m1 * m2, 1) union = torch.sum(m1, dim=1) + torch.sum(m2, dim=1) score = (2 * intersection + eps) / (union + eps) score = (1 - score).mean() return score class MultiLabelDiceLoss(nn.Module): """The average dice across multiple classes. Note: Sigmoid is automatically applied here! """ def __init__(self): super(MultiLabelDiceLoss, self).__init__() self.dice_loss = SoftDiceLoss() def forward(self, logits, targets): loss = 0 num_classes = targets.size(1) for class_nr in range(num_classes): loss += self.dice_loss(logits[:, class_nr, :, :], targets[:, class_nr, :, :]) return loss / num_classes class ComboLoss(nn.Module): """Weighted classification and segmentation loss. Attributes: weights (list): activation: bce: with logits loss dice_loss: soft dice loss (all classes) """ def __init__(self, weights=[0.1, 0, 1], activation=None): """ Args: weights (list): [image_cls, pixel_seg, pixel_cls] activation: One of ['sigmoid', None] """ super(ComboLoss, self).__init__() self.weights = weights self.activation = activation assert self.activation in ['sigmoid', None ], "`activation` must be one of ['sigmoid', None]." self.bce = nn.BCEWithLogitsLoss(reduce=True) self.dice_loss = MultiLabelDiceLoss() def create_fc_tensors(self, logits, targets): """Creates the classification tensors from the segmentation ones. """ batch_size, num_classes, _, _ = targets.shape summed = targets.view(batch_size, num_classes, -1).sum(-1) targets_fc = (summed > 0).float() logits_fc = logits.view(batch_size, num_classes, -1) logits_fc = torch.max(logits_fc, -1)[0] return logits_fc, targets_fc def forward(self, logits, targets): logits_fc, targets_fc = self.create_fc_tensors(logits, targets) p = torch.sigmoid(logits) if self.activation == 'sigmoid' else logits if self.weights[0]: loss_fc = self.weights[0] * self.bce(logits_fc, targets_fc) else: loss_fc = torch.tensor(0) if self.weights[1] or self.weights[2]: loss_seg_dice = self.weights[1] * self.dice_loss(p, targets) loss_seg_bce = self.weights[2] * self.bce(logits, targets) else: loss_seg_dice = torch.tensor(0) loss_seg_bce = torch.tensor(0) loss = loss_fc + loss_seg_bce + loss_seg_dice return loss def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.nn as nn 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_max_0(in_ptr0, 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 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] tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_per_fused_sum_1(in_ptr0, 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 tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tmp3 = tl.where(xmask, tmp1, 0) tmp4 = tl.sum(tmp3, 1)[:, None] tl.store(out_ptr0 + x0, tmp4, xmask) @triton.jit def triton_per_fused__to_copy_binary_cross_entropy_with_logits_gt_2(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) tmp6 = tl.load(in_ptr1 + r0, None) tmp1 = 0.0 tmp2 = tmp0 > tmp1 tmp3 = tmp2.to(tl.float32) tmp4 = 1.0 tmp5 = tmp4 - tmp3 tmp7 = tmp5 * tmp6 tmp8 = triton_helpers.minimum(tmp1, tmp6) tmp9 = tl_math.abs(tmp6) tmp10 = -tmp9 tmp11 = tl_math.exp(tmp10) tmp12 = libdevice.log1p(tmp11) tmp13 = tmp8 - tmp12 tmp14 = tmp7 - tmp13 tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK]) tmp17 = tl.sum(tmp15, 1)[:, None] tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp17, None) @triton.jit def triton_per_fused_binary_cross_entropy_with_logits_3(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel): XBLOCK: tl.constexpr = 1 RBLOCK: tl.constexpr = 256 xoffset = tl.program_id(0) * XBLOCK tl.full([1], xoffset, tl.int32) tl.full([RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[:] tl.full([RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + r0, None) 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)) tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp15, None) @triton.jit def triton_per_fused_mul_sum_4(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) tmp2 = tl.load(in_ptr1 + (r1 + 64 * x0), xmask, other=0.0) tmp1 = tl.sigmoid(tmp0) tmp3 = tmp1 * tmp2 tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp10 = tl.where(xmask, tmp8, 0) tmp11 = tl.sum(tmp10, 1)[:, None] tmp12 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp14 = tl.where(xmask, tmp12, 0) tmp15 = tl.sum(tmp14, 1)[:, None] tl.store(out_ptr0 + x0, tmp7, xmask) tl.store(out_ptr1 + x0, tmp11, xmask) tl.store(out_ptr2 + x0, tmp15, xmask) @triton.jit def triton_per_fused_mul_sum_5(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) tmp2 = tl.load(in_ptr1 + (16 + r1 + 64 * x0), xmask, other=0.0) tmp1 = tl.sigmoid(tmp0) tmp3 = tmp1 * tmp2 tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp10 = tl.where(xmask, tmp8, 0) tmp11 = tl.sum(tmp10, 1)[:, None] tmp12 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp14 = tl.where(xmask, tmp12, 0) tmp15 = tl.sum(tmp14, 1)[:, None] tl.store(out_ptr0 + x0, tmp7, xmask) tl.store(out_ptr1 + x0, tmp11, xmask) tl.store(out_ptr2 + x0, tmp15, xmask) @triton.jit def triton_per_fused_mul_sum_6(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) tmp2 = tl.load(in_ptr1 + (32 + r1 + 64 * x0), xmask, other=0.0) tmp1 = tl.sigmoid(tmp0) tmp3 = tmp1 * tmp2 tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp10 = tl.where(xmask, tmp8, 0) tmp11 = tl.sum(tmp10, 1)[:, None] tmp12 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp14 = tl.where(xmask, tmp12, 0) tmp15 = tl.sum(tmp14, 1)[:, None] tl.store(out_ptr0 + x0, tmp7, xmask) tl.store(out_ptr1 + x0, tmp11, xmask) tl.store(out_ptr2 + x0, tmp15, xmask) @triton.jit def triton_per_fused_mul_sum_7(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) tmp2 = tl.load(in_ptr1 + (48 + r1 + 64 * x0), xmask, other=0.0) tmp1 = tl.sigmoid(tmp0) tmp3 = tmp1 * tmp2 tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp10 = tl.where(xmask, tmp8, 0) tmp11 = tl.sum(tmp10, 1)[:, None] tmp12 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK]) tmp14 = tl.where(xmask, tmp12, 0) tmp15 = tl.sum(tmp14, 1)[:, None] tl.store(out_ptr0 + x0, tmp7, xmask) tl.store(out_ptr1 + x0, tmp11, xmask) tl.store(out_ptr2 + x0, tmp15, xmask) @triton.jit def triton_per_fused__to_copy_add_binary_cross_entropy_with_logits_div_gt_mean_mul_rsub_8( 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, in_ptr12, in_ptr13, 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) tmp15 = tl.load(in_ptr3 + r0, None) tmp18 = tl.load(in_ptr4 + r0, None) tmp19 = tl.load(in_ptr5 + r0, None) tmp27 = tl.load(in_ptr6 + r0, None) tmp30 = tl.load(in_ptr7 + r0, None) tmp31 = tl.load(in_ptr8 + r0, None) tmp39 = tl.load(in_ptr9 + r0, None) tmp42 = tl.load(in_ptr10 + r0, None) tmp43 = tl.load(in_ptr11 + r0, None) tmp51 = tl.load(in_ptr12 + 0) tmp52 = tl.broadcast_to(tmp51, [XBLOCK, 1]) tmp57 = tl.load(in_ptr13 + 0) tmp58 = tl.broadcast_to(tmp57, [XBLOCK, 1]) tmp1 = 2.0 tmp2 = tmp0 * tmp1 tmp3 = 1e-09 tmp4 = tmp2 + tmp3 tmp7 = tmp5 + tmp6 tmp8 = tmp7 + tmp3 tmp9 = tmp4 / tmp8 tmp10 = 1.0 tmp11 = tmp10 - tmp9 tmp12 = tl.broadcast_to(tmp11, [XBLOCK, RBLOCK]) tmp14 = tl.sum(tmp12, 1)[:, None] tmp16 = tmp15 * tmp1 tmp17 = tmp16 + tmp3 tmp20 = tmp18 + tmp19 tmp21 = tmp20 + tmp3 tmp22 = tmp17 / tmp21 tmp23 = tmp10 - tmp22 tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK]) tmp26 = tl.sum(tmp24, 1)[:, None] tmp28 = tmp27 * tmp1 tmp29 = tmp28 + tmp3 tmp32 = tmp30 + tmp31 tmp33 = tmp32 + tmp3 tmp34 = tmp29 / tmp33 tmp35 = tmp10 - tmp34 tmp36 = tl.broadcast_to(tmp35, [XBLOCK, RBLOCK]) tmp38 = tl.sum(tmp36, 1)[:, None] tmp40 = tmp39 * tmp1 tmp41 = tmp40 + tmp3 tmp44 = tmp42 + tmp43 tmp45 = tmp44 + tmp3 tmp46 = tmp41 / tmp45 tmp47 = tmp10 - tmp46 tmp48 = tl.broadcast_to(tmp47, [XBLOCK, RBLOCK]) tmp50 = tl.sum(tmp48, 1)[:, None] tmp53 = 16.0 tmp54 = tmp52 / tmp53 tmp55 = 0.1 tmp56 = tmp54 * tmp55 tmp59 = 256.0 tmp60 = tmp58 / tmp59 tmp61 = tmp60 * tmp10 tmp62 = tmp56 + tmp61 tmp63 = 4.0 tmp64 = tmp14 / tmp63 tmp65 = 0.0 tmp66 = tmp64 + tmp65 tmp67 = tmp26 / tmp63 tmp68 = tmp66 + tmp67 tmp69 = tmp38 / tmp63 tmp70 = tmp68 + tmp69 tmp71 = tmp50 / tmp63 tmp72 = tmp70 + tmp71 tmp73 = 0.25 tmp74 = tmp72 * tmp73 tmp75 = tmp74 * tmp65 tmp76 = tmp62 + tmp75 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp76, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_per_fused_max_0[grid(16)](arg1_1, buf0, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_per_fused_sum_1[grid(16)](arg0_1, buf2, 16, 16, XBLOCK=1, num_warps=2, num_stages=1) buf3 = empty_strided_cuda((), (), torch.float32) triton_per_fused__to_copy_binary_cross_entropy_with_logits_gt_2[grid(1) ](buf2, buf0, buf3, 1, 16, XBLOCK=1, num_warps=2, num_stages=1) del buf0 del buf2 buf4 = empty_strided_cuda((), (), torch.float32) triton_per_fused_binary_cross_entropy_with_logits_3[grid(1)](arg0_1, arg1_1, buf4, 1, 256, num_warps=2, num_stages=1) buf5 = empty_strided_cuda((4,), (1,), torch.float32) buf6 = empty_strided_cuda((4,), (1,), torch.float32) buf7 = empty_strided_cuda((4,), (1,), torch.float32) triton_per_fused_mul_sum_4[grid(4)](arg1_1, arg0_1, buf5, buf6, buf7, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) buf9 = empty_strided_cuda((4,), (1,), torch.float32) buf10 = empty_strided_cuda((4,), (1,), torch.float32) buf11 = empty_strided_cuda((4,), (1,), torch.float32) triton_per_fused_mul_sum_5[grid(4)](arg1_1, arg0_1, buf9, buf10, buf11, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) buf13 = empty_strided_cuda((4,), (1,), torch.float32) buf14 = empty_strided_cuda((4,), (1,), torch.float32) buf15 = empty_strided_cuda((4,), (1,), torch.float32) triton_per_fused_mul_sum_6[grid(4)](arg1_1, arg0_1, buf13, buf14, buf15, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) buf17 = empty_strided_cuda((4,), (1,), torch.float32) buf18 = empty_strided_cuda((4,), (1,), torch.float32) buf19 = empty_strided_cuda((4,), (1,), torch.float32) triton_per_fused_mul_sum_7[grid(4)](arg1_1, arg0_1, buf17, buf18, buf19, 4, 16, XBLOCK=1, num_warps=2, num_stages=1) del arg0_1 del arg1_1 buf12 = empty_strided_cuda((), (), torch.float32) buf21 = buf12 del buf12 triton_per_fused__to_copy_add_binary_cross_entropy_with_logits_div_gt_mean_mul_rsub_8[ grid(1)](buf21, buf5, buf6, buf7, buf9, buf10, buf11, buf13, buf14, buf15, buf17, buf18, buf19, buf3, buf4, 1, 4, XBLOCK=1, num_warps=2, num_stages=1) del buf10 del buf11 del buf13 del buf14 del buf15 del buf17 del buf18 del buf19 del buf3 del buf4 del buf5 del buf6 del buf7 del buf9 return buf21, class SoftDiceLoss(nn.Module): """Differentiable soft dice loss. Note: Sigmoid is automatically applied here! """ def __init__(self): super(SoftDiceLoss, self).__init__() def forward(self, logits, targets): eps = 1e-09 num = targets.size(0) probs = torch.sigmoid(logits) m1 = probs.view(num, -1) m2 = targets.view(num, -1).float() intersection = torch.sum(m1 * m2, 1) union = torch.sum(m1, dim=1) + torch.sum(m2, dim=1) score = (2 * intersection + eps) / (union + eps) score = (1 - score).mean() return score class MultiLabelDiceLoss(nn.Module): """The average dice across multiple classes. Note: Sigmoid is automatically applied here! """ def __init__(self): super(MultiLabelDiceLoss, self).__init__() self.dice_loss = SoftDiceLoss() def forward(self, logits, targets): loss = 0 num_classes = targets.size(1) for class_nr in range(num_classes): loss += self.dice_loss(logits[:, class_nr, :, :], targets[:, class_nr, :, :]) return loss / num_classes class ComboLossNew(nn.Module): """Weighted classification and segmentation loss. Attributes: weights (list): activation: bce: with logits loss dice_loss: soft dice loss (all classes) """ def __init__(self, weights=[0.1, 0, 1], activation=None): """ Args: weights (list): [image_cls, pixel_seg, pixel_cls] activation: One of ['sigmoid', None] """ super(ComboLossNew, self).__init__() self.weights = weights self.activation = activation assert self.activation in ['sigmoid', None ], "`activation` must be one of ['sigmoid', None]." self.bce = nn.BCEWithLogitsLoss(reduce=True) self.dice_loss = MultiLabelDiceLoss() def create_fc_tensors(self, logits, targets): """Creates the classification tensors from the segmentation ones. """ batch_size, num_classes, _, _ = targets.shape summed = targets.view(batch_size, num_classes, -1).sum(-1) targets_fc = (summed > 0).float() logits_fc = logits.view(batch_size, num_classes, -1) logits_fc = torch.max(logits_fc, -1)[0] return logits_fc, targets_fc def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
jchen42703/reproducing-cloud-3rd-place
ComboLoss
false
6,945
[ "Apache-2.0" ]
1
25571f53efd48f68735d7fe2991e3ad783cbd4b1
https://github.com/jchen42703/reproducing-cloud-3rd-place/tree/25571f53efd48f68735d7fe2991e3ad783cbd4b1
DomainCNN
import torch from torch.nn import functional as F import torch.utils.data class DomainCNN(torch.nn.Module): def __init__(self, domains): super(DomainCNN, self).__init__() self.conv1 = torch.nn.Conv1d(1, 32, kernel_size=5) self.pool1 = torch.nn.MaxPool1d(kernel_size=2) self.conv2 = torch.nn.Conv1d(32, 16, kernel_size=5, stride=2) self.pool2 = torch.nn.MaxPool1d(kernel_size=2, stride=2) self.conv3 = torch.nn.Conv1d(16, 8, kernel_size=2, stride=2) self.pool3 = torch.nn.MaxPool1d(kernel_size=2, stride=1) self.fc1 = torch.nn.Linear(8 * 2, domains) def forward(self, x): x = F.elu(self.conv1(x)) x = self.pool1(x) x = F.elu(self.conv2(x)) x = self.pool2(x) x = F.elu(self.conv3(x)) x = self.pool3(x) x = x.view(-1, 8 * 2) m = torch.nn.Softmax(1) x = m(self.fc1(x)) return x def get_inputs(): return [torch.rand([4, 1, 64])] def get_init_inputs(): return [[], {'domains': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_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_elu_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 7680 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 60 % 32 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_max_pool2d_with_indices_1(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 3840 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 2 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0), xmask, eviction_policy='evict_last') 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) tl.store(out_ptr0 + x0, tmp5, xmask) tl.store(out_ptr1 + x0, tmp6, xmask) @triton.jit def triton_poi_fused_convolution_elu_2(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 832 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 13 % 16 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_max_pool2d_with_indices_3(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 384 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 6 x1 = xindex // 6 x2 = xindex tmp0 = tl.load(in_ptr0 + (2 * x0 + 13 * x1), xmask, eviction_policy= 'evict_last') tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 13 * x1), xmask, eviction_policy ='evict_last') tmp2 = tmp1 > tmp0 tmp3 = tl.full([1], 1, tl.int8) tmp4 = tl.full([1], 0, tl.int8) tmp5 = tl.where(tmp2, tmp3, tmp4) tmp6 = triton_helpers.maximum(tmp1, tmp0) tl.store(out_ptr0 + x2, tmp5, xmask) tl.store(out_ptr1 + x2, tmp6, xmask) @triton.jit def triton_poi_fused_convolution_elu_4(in_out_ptr0, in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 96 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 3 % 8 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_max_pool2d_with_indices_5(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 2 x1 = xindex // 2 x2 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 3 * x1), xmask) tmp1 = tl.load(in_ptr0 + (1 + x0 + 3 * x1), xmask) 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) tl.store(out_ptr0 + x2, tmp5, xmask) tl.store(out_ptr1 + x2, tmp6, xmask) @triton.jit def triton_poi_fused__softmax_6(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x2, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_7(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9) = args args.clear() assert_size_stride(primals_1, (32, 1, 5), (5, 5, 1)) assert_size_stride(primals_2, (32,), (1,)) assert_size_stride(primals_3, (4, 1, 64), (64, 64, 1)) assert_size_stride(primals_4, (16, 32, 5), (160, 5, 1)) assert_size_stride(primals_5, (16,), (1,)) assert_size_stride(primals_6, (8, 16, 2), (32, 2, 1)) assert_size_stride(primals_7, (8,), (1,)) assert_size_stride(primals_8, (4, 16), (16, 1)) assert_size_stride(primals_9, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=( 0,), groups=1, bias=None) assert_size_stride(buf0, (4, 32, 60), (1920, 60, 1)) buf1 = buf0 del buf0 buf2 = empty_strided_cuda((4, 32, 60), (1920, 60, 1), torch.float32) get_raw_stream(0) triton_poi_fused_convolution_elu_0[grid(7680)](buf1, primals_2, buf2, 7680, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf3 = empty_strided_cuda((4, 32, 1, 30), (960, 30, 30, 1), torch.int8) buf4 = empty_strided_cuda((4, 32, 1, 30), (960, 30, 30, 1), torch. float32) triton_poi_fused_max_pool2d_with_indices_1[grid(3840)](buf2, buf3, buf4, 3840, XBLOCK=256, num_warps=4, num_stages=1) buf5 = extern_kernels.convolution(reinterpret_tensor(buf4, (4, 32, 30), (960, 30, 1), 0), primals_4, stride=(2,), padding=(0,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf5, (4, 16, 13), (208, 13, 1)) buf6 = buf5 del buf5 buf7 = empty_strided_cuda((4, 16, 13), (208, 13, 1), torch.float32) triton_poi_fused_convolution_elu_2[grid(832)](buf6, primals_5, buf7, 832, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf8 = empty_strided_cuda((4, 16, 1, 6), (96, 6, 6, 1), torch.int8) buf9 = empty_strided_cuda((4, 16, 1, 6), (96, 6, 6, 1), torch.float32) triton_poi_fused_max_pool2d_with_indices_3[grid(384)](buf7, buf8, buf9, 384, XBLOCK=128, num_warps=4, num_stages=1) buf10 = extern_kernels.convolution(reinterpret_tensor(buf9, (4, 16, 6), (96, 6, 1), 0), primals_6, stride=(2,), padding=(0,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None) assert_size_stride(buf10, (4, 8, 3), (24, 3, 1)) buf11 = buf10 del buf10 buf12 = empty_strided_cuda((4, 8, 3), (24, 3, 1), torch.float32) triton_poi_fused_convolution_elu_4[grid(96)](buf11, primals_7, buf12, 96, XBLOCK=128, num_warps=4, num_stages=1) del primals_7 buf13 = empty_strided_cuda((4, 8, 1, 2), (16, 2, 2, 1), torch.int8) buf14 = empty_strided_cuda((4, 8, 1, 2), (16, 2, 2, 1), torch.float32) triton_poi_fused_max_pool2d_with_indices_5[grid(64)](buf12, buf13, buf14, 64, XBLOCK=64, num_warps=1, num_stages=1) buf15 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_9, reinterpret_tensor(buf14, (4, 16), (16, 1), 0), reinterpret_tensor(primals_8, (16, 4), (1, 16), 0), alpha=1, beta=1, out=buf15) del primals_9 buf16 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused__softmax_6[grid(16)](buf15, buf16, 16, XBLOCK=16, num_warps=1, num_stages=1) buf17 = buf15 del buf15 triton_poi_fused__softmax_7[grid(16)](buf16, buf17, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf16 return (buf17, primals_1, primals_3, primals_4, primals_6, buf1, reinterpret_tensor(buf2, (4, 32, 1, 60), (1920, 60, 60, 1), 0), buf3, reinterpret_tensor(buf4, (4, 32, 30), (960, 30, 1), 0), buf6, reinterpret_tensor(buf7, (4, 16, 1, 13), (208, 13, 13, 1), 0), buf8, reinterpret_tensor(buf9, (4, 16, 6), (96, 6, 1), 0), buf11, reinterpret_tensor(buf12, (4, 8, 1, 3), (24, 3, 3, 1), 0), buf13, reinterpret_tensor(buf14, (4, 16), (16, 1), 0), buf17, primals_8) class DomainCNNNew(torch.nn.Module): def __init__(self, domains): super(DomainCNNNew, self).__init__() self.conv1 = torch.nn.Conv1d(1, 32, kernel_size=5) self.pool1 = torch.nn.MaxPool1d(kernel_size=2) self.conv2 = torch.nn.Conv1d(32, 16, kernel_size=5, stride=2) self.pool2 = torch.nn.MaxPool1d(kernel_size=2, stride=2) self.conv3 = torch.nn.Conv1d(16, 8, kernel_size=2, stride=2) self.pool3 = torch.nn.MaxPool1d(kernel_size=2, stride=1) self.fc1 = torch.nn.Linear(8 * 2, domains) 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_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]
jenchen1398/artistic-music-style-transfer
DomainCNN
false
6,946
[ "BSD-3-Clause" ]
1
aa02bcf9c27cb6124c6316a756f7fd77d42be11a
https://github.com/jenchen1398/artistic-music-style-transfer/tree/aa02bcf9c27cb6124c6316a756f7fd77d42be11a
GeCEmbeddings
from _paritybench_helpers import _mock_config import torch import typing from torch import nn def create_sinusoidal_embeddings(n_pos, dim, out): out.requires_grad = False positions = torch.arange(0, n_pos)[:, None] dimensions = torch.arange(0, dim) position_enc = positions / torch.pow(10000, 2 * (dimensions // 2) / dim) out[:, 0::2] = torch.sin(position_enc[:, 0::2]) out[:, 1::2] = torch.cos(position_enc[:, 1::2]) class GeCEmbeddings(nn.Module): """Construct the embeddings from gene, (strand and spacing embeddings). """ def __init__(self, config: 'BioConfig', position_embeddings: 'bool'=True): super().__init__() self.generep_embeddings = nn.Linear(config.input_rep_size, config. hidden_size) if position_embeddings: self.position_embeddings: 'nn.Embedding' = nn.Embedding(config. max_position_embeddings, config.hidden_size) if config.sinusoidal_pos_embds: create_sinusoidal_embeddings(n_pos=config. max_position_embeddings, dim=config.hidden_size, out= self.position_embeddings.weight) self.direction_embeddings: 'nn.Embedding' = nn.Embedding(3, config. hidden_size) self.length_embeddings: 'nn.Embedding' = nn.Embedding(config. gene_max_length // config.gene_length_bin_size + 1, config. hidden_size) self.gene_length_bin_size = config.gene_length_bin_size self.LayerNorm = nn.LayerNorm(config.hidden_size, eps=config. layer_norm_eps) self.dropout = nn.Dropout(config.hidden_dropout_prob) def forward(self, gene_reps: 'torch.Tensor', strands: 'typing.Optional[torch.Tensor]'=None, lengths: 'typing.Optional[torch.Tensor]'=None, **kwargs) ->torch.Tensor: if strands is None: strands = torch.zeros_like(gene_reps[:, :, 0], dtype=torch.long) else: strands = strands.long() if lengths is None: lengths = torch.ones_like(gene_reps[:, :, 0], dtype=torch.long) else: lengths = strands.long() generep_embeddings = self.generep_embeddings(gene_reps) direction_embeddings = self.direction_embeddings(strands + 1) length_embeddings = self.length_embeddings(torch.clamp(lengths, 1, self.length_embeddings.num_embeddings) // self.gene_length_bin_size ) embeddings = (generep_embeddings + direction_embeddings + length_embeddings) if hasattr(self, 'position_embeddings'): position_ids = torch.arange(gene_reps.size()[1], dtype=torch. long, device=gene_reps.device) position_ids = position_ids.unsqueeze(0).expand(gene_reps.shape [:-1]) position_embeddings = self.position_embeddings(position_ids) embeddings = embeddings + position_embeddings embeddings = self.LayerNorm(embeddings) embeddings = self.dropout(embeddings) return embeddings def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'config': _mock_config(input_rep_size=4, hidden_size=4, max_position_embeddings=4, sinusoidal_pos_embds=4, gene_max_length= 4, gene_length_bin_size=4, layer_norm_eps=1, hidden_dropout_prob=0.5)}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice from torch import nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_add_0(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.full([1], 1, tl.int64) tl.store(out_ptr0 + x0, tmp0, xmask) @triton.jit def triton_poi_fused_floor_divide_1(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.full([1], 0, tl.int64) tl.store(out_ptr0 + x0, tmp0, xmask) @triton.jit def triton_poi_fused_add_embedding_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 4 x5 = xindex % 16 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr3 + x5, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp8 = tmp6 + tmp7 tl.store(in_out_ptr0 + x3, tmp8, xmask) @triton.jit def triton_poi_fused_native_layer_norm_3(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex 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_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, 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,), (1,)) assert_size_stride(primals_4, (3, 4), (4, 1)) assert_size_stride(primals_5, (2, 4), (4, 1)) assert_size_stride(primals_6, (4, 4), (4, 1)) assert_size_stride(primals_7, (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_1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0) del primals_2 buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.int64) get_raw_stream(0) triton_poi_fused_add_0[grid(64)](buf1, 64, XBLOCK=64, num_warps=1, num_stages=1) buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.int64) triton_poi_fused_floor_divide_1[grid(64)](buf2, 64, XBLOCK=64, num_warps=1, num_stages=1) buf3 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf0 triton_poi_fused_add_embedding_2[grid(256)](buf3, primals_3, primals_4, primals_5, primals_6, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_3 del primals_4 del primals_5 del primals_6 buf4 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf5 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) triton_poi_fused_native_layer_norm_3[grid(64)](buf3, buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_4[grid(256)](buf3, buf4, buf5, primals_7, primals_8, buf6, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf4 del buf5 del primals_8 return buf6, primals_7, reinterpret_tensor(primals_1, (64, 4), (4, 1), 0 ), buf1, buf2, buf3 def create_sinusoidal_embeddings(n_pos, dim, out): out.requires_grad = False positions = torch.arange(0, n_pos)[:, None] dimensions = torch.arange(0, dim) position_enc = positions / torch.pow(10000, 2 * (dimensions // 2) / dim) out[:, 0::2] = torch.sin(position_enc[:, 0::2]) out[:, 1::2] = torch.cos(position_enc[:, 1::2]) class GeCEmbeddingsNew(nn.Module): """Construct the embeddings from gene, (strand and spacing embeddings). """ def __init__(self, config: 'BioConfig', position_embeddings: 'bool'=True): super().__init__() self.generep_embeddings = nn.Linear(config.input_rep_size, config. hidden_size) if position_embeddings: self.position_embeddings: 'nn.Embedding' = nn.Embedding(config. max_position_embeddings, config.hidden_size) if config.sinusoidal_pos_embds: create_sinusoidal_embeddings(n_pos=config. max_position_embeddings, dim=config.hidden_size, out= self.position_embeddings.weight) self.direction_embeddings: 'nn.Embedding' = nn.Embedding(3, config. hidden_size) self.length_embeddings: 'nn.Embedding' = nn.Embedding(config. gene_max_length // config.gene_length_bin_size + 1, config. hidden_size) self.gene_length_bin_size = config.gene_length_bin_size self.LayerNorm = nn.LayerNorm(config.hidden_size, eps=config. layer_norm_eps) self.dropout = nn.Dropout(config.hidden_dropout_prob) def forward(self, input_0): primals_2 = self.generep_embeddings.weight primals_3 = self.generep_embeddings.bias primals_6 = self.position_embeddings.weight primals_4 = self.direction_embeddings.weight primals_5 = self.length_embeddings.weight primals_7 = self.LayerNorm.weight primals_8 = self.LayerNorm.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]
jgoodson/TraGeC
GeCEmbeddings
false
6,947
[ "BSD-3-Clause" ]
1
3370e29ba0639745055cbee726a40181a4dd61df
https://github.com/jgoodson/TraGeC/tree/3370e29ba0639745055cbee726a40181a4dd61df
RoutingBase
import torch from torch.nn import functional as F import torch.nn as nn def cal_normal(v, dim=-1, keepdim=False): """ :return: """ normal = torch.sum(v ** 2, dim=dim, keepdim=keepdim) ** 0.5 return normal def squash(sr, dim=1): """ :param dim: :param sr:(bs, dim) :return: """ sr_normal = cal_normal(sr, keepdim=True, dim=dim) sr_normal2 = sr_normal ** 2 v = sr / sr_normal * (sr_normal2 / (1 + sr_normal2)) return v def dynamic_routing(u, br): """ u: (b, num_size, num_classes, dim) br: (b, num_size, num_classes, 1) :return: """ cr = F.softmax(br, dim=1) sr = torch.sum(cr * u, dim=1) vr = squash(sr, dim=-1) sm = torch.einsum('bncd,bcd->bnc', u, vr).unsqueeze(dim=3) br = br + sm return br, vr class RoutingBase(nn.Module): def __init__(self, num_routing_iterations=1, **kwargs): super(RoutingBase, self).__init__() self.num_routing_iterations = num_routing_iterations def forward(self, inx): """ inx: (b, num_size, num_classes, dim) :return: """ v_h = [] b_h = [] inx_device = inx.device br = torch.zeros(size=(*inx.size()[:-1], 1), requires_grad=False, device=inx_device) for i in range(self.num_routing_iterations): br, vr = dynamic_routing(inx, br) v_h.append(vr.unsqueeze(dim=3)) b_h.append(br) return torch.cat(b_h, dim=-1), torch.cat(v_h, dim=-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.triton_helpers import libdevice, math as tl_math from torch.nn import 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__softmax_mul_pow_sum_zeros_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 x2 = xindex tmp6 = tl.load(in_ptr0 + (4 * x0 + 64 * x1), xmask, eviction_policy= 'evict_last') tmp8 = tl.load(in_ptr0 + (16 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp11 = tl.load(in_ptr0 + (32 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp14 = tl.load(in_ptr0 + (48 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp18 = tl.load(in_ptr0 + (1 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp20 = tl.load(in_ptr0 + (17 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp23 = tl.load(in_ptr0 + (33 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp26 = tl.load(in_ptr0 + (49 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp31 = tl.load(in_ptr0 + (2 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp33 = tl.load(in_ptr0 + (18 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp36 = tl.load(in_ptr0 + (34 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp39 = tl.load(in_ptr0 + (50 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp44 = tl.load(in_ptr0 + (3 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp46 = tl.load(in_ptr0 + (19 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp49 = tl.load(in_ptr0 + (35 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp52 = tl.load(in_ptr0 + (51 + 4 * x0 + 64 * x1), xmask, eviction_policy='evict_last') tmp0 = 0.0 tmp1 = tl_math.exp(tmp0) tmp2 = tmp1 + tmp1 tmp3 = tmp2 + tmp1 tmp4 = tmp3 + tmp1 tmp5 = tmp1 / tmp4 tmp7 = tmp5 * tmp6 tmp9 = tmp5 * tmp8 tmp10 = tmp7 + tmp9 tmp12 = tmp5 * tmp11 tmp13 = tmp10 + tmp12 tmp15 = tmp5 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp16 * tmp16 tmp19 = tmp5 * tmp18 tmp21 = tmp5 * tmp20 tmp22 = tmp19 + tmp21 tmp24 = tmp5 * tmp23 tmp25 = tmp22 + tmp24 tmp27 = tmp5 * tmp26 tmp28 = tmp25 + tmp27 tmp29 = tmp28 * tmp28 tmp30 = tmp17 + tmp29 tmp32 = tmp5 * tmp31 tmp34 = tmp5 * tmp33 tmp35 = tmp32 + tmp34 tmp37 = tmp5 * tmp36 tmp38 = tmp35 + tmp37 tmp40 = tmp5 * tmp39 tmp41 = tmp38 + tmp40 tmp42 = tmp41 * tmp41 tmp43 = tmp30 + tmp42 tmp45 = tmp5 * tmp44 tmp47 = tmp5 * tmp46 tmp48 = tmp45 + tmp47 tmp50 = tmp5 * tmp49 tmp51 = tmp48 + tmp50 tmp53 = tmp5 * tmp52 tmp54 = tmp51 + tmp53 tmp55 = tmp54 * tmp54 tmp56 = tmp43 + tmp55 tl.store(out_ptr0 + x2, tmp56, xmask) @triton.jit def triton_poi_fused__softmax_add_div_mul_pow_sum_zeros_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 x2 = xindex // 16 x3 = xindex % 16 x4 = xindex // 4 x5 = xindex tmp6 = tl.load(in_ptr0 + (x3 + 64 * x2), xmask) tmp8 = tl.load(in_ptr0 + (16 + x3 + 64 * x2), xmask) tmp11 = tl.load(in_ptr0 + (32 + x3 + 64 * x2), xmask) tmp14 = tl.load(in_ptr0 + (48 + x3 + 64 * x2), xmask) tmp17 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last') tmp0 = 0.0 tmp1 = tl_math.exp(tmp0) tmp2 = tmp1 + tmp1 tmp3 = tmp2 + tmp1 tmp4 = tmp3 + tmp1 tmp5 = tmp1 / tmp4 tmp7 = tmp5 * tmp6 tmp9 = tmp5 * tmp8 tmp10 = tmp7 + tmp9 tmp12 = tmp5 * tmp11 tmp13 = tmp10 + tmp12 tmp15 = tmp5 * tmp14 tmp16 = tmp13 + tmp15 tmp18 = libdevice.sqrt(tmp17) tmp19 = tmp16 / tmp18 tmp20 = tmp18 * tmp18 tmp21 = 1.0 tmp22 = tmp20 + tmp21 tmp23 = tmp20 / tmp22 tmp24 = tmp19 * tmp23 tl.store(out_ptr0 + x5, tmp24, xmask) @triton.jit def triton_poi_fused_clone_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 4 x1 = xindex // 4 % 4 x2 = xindex // 16 % 4 x3 = xindex // 64 x4 = xindex tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1 + 64 * x3), xmask) tl.store(out_ptr0 + x4, tmp0, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32) get_raw_stream(0) triton_poi_fused__softmax_mul_pow_sum_zeros_0[grid(16)](arg0_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32) triton_poi_fused__softmax_add_div_mul_pow_sum_zeros_1[grid(64)](arg0_1, buf0, buf1, 64, XBLOCK=64, num_warps=1, num_stages=1) del buf0 buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_clone_2[grid(256)](arg0_1, buf2, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 buf3 = empty_strided_cuda((16, 4, 1), (4, 1, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(buf2, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf1, (16, 4, 1), (4, 1, 1), 0), out=buf3) del buf2 return reinterpret_tensor(buf3, (4, 4, 4, 1), (16, 1, 4, 1), 0 ), reinterpret_tensor(buf1, (4, 4, 4, 1), (16, 4, 1, 1), 0) def cal_normal(v, dim=-1, keepdim=False): """ :return: """ normal = torch.sum(v ** 2, dim=dim, keepdim=keepdim) ** 0.5 return normal def squash(sr, dim=1): """ :param dim: :param sr:(bs, dim) :return: """ sr_normal = cal_normal(sr, keepdim=True, dim=dim) sr_normal2 = sr_normal ** 2 v = sr / sr_normal * (sr_normal2 / (1 + sr_normal2)) return v def dynamic_routing(u, br): """ u: (b, num_size, num_classes, dim) br: (b, num_size, num_classes, 1) :return: """ cr = F.softmax(br, dim=1) sr = torch.sum(cr * u, dim=1) vr = squash(sr, dim=-1) sm = torch.einsum('bncd,bcd->bnc', u, vr).unsqueeze(dim=3) br = br + sm return br, vr class RoutingBaseNew(nn.Module): def __init__(self, num_routing_iterations=1, **kwargs): super(RoutingBaseNew, self).__init__() self.num_routing_iterations = num_routing_iterations def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0], output[1]
jiangzhiwei2018/Pytorch_CapsNet
RoutingBase
false
6,948
[ "Apache-2.0" ]
1
b8931d65d5a99a4ff18fd209c16d3ff7d094d1ad
https://github.com/jiangzhiwei2018/Pytorch_CapsNet/tree/b8931d65d5a99a4ff18fd209c16d3ff7d094d1ad
MCDropout2d
import torch from torch import Tensor import torch.nn as nn from torch.functional import F import torch.nn.functional as F class MCDropout2d(nn.Dropout2d): """2D dropout that stays on during training and testing """ def forward(self, input: 'Tensor') ->Tensor: return F.dropout2d(input, self.p, True, self.inplace) 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_bernoulli_0(out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = float('nan') tl.store(out_ptr0 + x0, tmp0, xmask) @triton.jit def triton_poi_fused_div_mul_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 16 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp2 = 2.0 tmp3 = tmp1 * tmp2 tmp4 = tmp0 * tmp3 tl.store(out_ptr0 + x2, 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) buf1 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_bernoulli_0[grid(16)](buf1, 16, XBLOCK=16, num_warps=1, num_stages=1) torch.ops.aten.bernoulli_.float(buf1, 0.5) buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_div_mul_1[grid(256)](arg0_1, buf1, buf3, 256, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del buf1 return buf3, class MCDropout2dNew(nn.Dropout2d): """2D dropout that stays on during training and testing """ def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
jiwoncpark/ex-con
MCDropout2d
false
6,949
[ "MIT" ]
1
6775d11ec1c3e7005890e58d16dd07b711861cdf
https://github.com/jiwoncpark/ex-con/tree/6775d11ec1c3e7005890e58d16dd07b711861cdf
BarlowTwinLoss
import torch import torch.nn.functional as F def off_diagonal(x): """Return a flattened view of the off-diagonal elements of a square matrix. >>> x = np.array([[1,2,3],[4,5,6],[7,8,9]]) array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> x.flatten() array([1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> y = x.flatten()[:-1] >>> z = y.reshape(2,4) >>> z array([[1, 2, 3, 4], [5, 6, 7, 8]]) >>> z[:, 1:] array([[2, 3, 4], [6, 7, 8]]) """ n, m = x.shape assert n == m, 'x is not a phalanx' return x.flatten()[:-1].view(n - 1, n + 1)[:, 1:].flatten() class BarlowTwinLoss(torch.nn.Module): def __init__(self, lambda_param=0.005) ->None: super().__init__() self.lambda_param = lambda_param def forward(self, x, y): x_norm = F.normalize(x, dim=-1) y_norm = F.normalize(y, dim=-1) N, _D = x.size()[:2] simmlar_mat = torch.mm(x_norm.T, y_norm) / N on_diag = torch.diagonal(simmlar_mat).add(-1).pow(2).sum() off_diag = off_diagonal(simmlar_mat).pow_(2).sum() loss = on_diag + self.lambda_param * off_diag return loss def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_div_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = libdevice.sqrt(tmp11) tmp13 = 1e-12 tmp14 = triton_helpers.maximum(tmp12, tmp13) tmp15 = tmp0 / tmp14 tl.store(out_ptr0 + x2, tmp15, xmask) @triton.jit def triton_per_fused_add_pow_sum_1(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): RBLOCK: tl.constexpr = 4 xoffset = tl.program_id(0) * XBLOCK xoffset + tl.arange(0, XBLOCK)[:, None] tl.full([XBLOCK, RBLOCK], True, tl.int1) rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r0 = rindex tmp0 = tl.load(in_ptr0 + 5 * r0, None, eviction_policy='evict_last') tmp1 = 0.25 tmp2 = tmp0 * tmp1 tmp3 = -1.0 tmp4 = tmp2 + tmp3 tmp5 = tmp4 * tmp4 tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK]) tmp8 = tl.sum(tmp6, 1)[:, None] tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp8, None) @triton.jit def triton_per_fused_add_mul_sum_2(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr): rnumel = 12 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, :] rmask = rindex < rnumel r0 = rindex tmp0 = tl.load(in_ptr0 + (1 + 5 * (r0 // 4) + 5 * (r0 % 4 // 4) + r0 % 4), rmask, other=0.0) tmp8 = tl.load(in_out_ptr0 + 0) tmp9 = tl.broadcast_to(tmp8, [XBLOCK, 1]) tmp1 = 0.25 tmp2 = tmp0 * tmp1 tmp3 = tmp2 * tmp2 tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK]) tmp6 = tl.where(rmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp10 = 0.005 tmp11 = tmp7 * tmp10 tmp12 = tmp9 + tmp11 tl.debug_barrier() tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp12, None) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4), (4, 1)) assert_size_stride(arg1_1, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_div_0[grid(16)](arg0_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused_div_0[grid(16)](arg1_1, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1) del arg1_1 buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf0, (4, 4), (1, 4), 0), buf1, out=buf2) del buf0 del buf1 buf3 = empty_strided_cuda((), (), torch.float32) triton_per_fused_add_pow_sum_1[grid(1)](buf2, buf3, 1, 4, XBLOCK=1, num_warps=2, num_stages=1) buf5 = buf3 del buf3 triton_per_fused_add_mul_sum_2[grid(1)](buf5, buf2, 1, 12, XBLOCK=1, num_warps=2, num_stages=1) del buf2 return buf5, def off_diagonal(x): """Return a flattened view of the off-diagonal elements of a square matrix. >>> x = np.array([[1,2,3],[4,5,6],[7,8,9]]) array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> x.flatten() array([1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> y = x.flatten()[:-1] >>> z = y.reshape(2,4) >>> z array([[1, 2, 3, 4], [5, 6, 7, 8]]) >>> z[:, 1:] array([[2, 3, 4], [6, 7, 8]]) """ n, m = x.shape assert n == m, 'x is not a phalanx' return x.flatten()[:-1].view(n - 1, n + 1)[:, 1:].flatten() class BarlowTwinLossNew(torch.nn.Module): def __init__(self, lambda_param=0.005) ->None: super().__init__() self.lambda_param = lambda_param def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
jianzhnie/self_supervised
BarlowTwinLoss
false
6,950
[ "Apache-2.0" ]
1
d1e0f31ab032150ab0ad007c1e19773135a5fb79
https://github.com/jianzhnie/self_supervised/tree/d1e0f31ab032150ab0ad007c1e19773135a5fb79
SamePadConv2d
import torch from torch.nn import functional as F import torch.nn as nn class SamePadConv2d(nn.Conv2d): """ Conv with TF padding='same' https://github.com/pytorch/pytorch/issues/3867#issuecomment-349279036 """ def __init__(self, in_channels, out_channels, kernel_size, stride=1, dilation=1, groups=1, bias=True, padding_mode='zeros'): super().__init__(in_channels, out_channels, kernel_size, stride, 0, dilation, groups, bias, padding_mode) def get_pad_odd(self, in_, weight, stride, dilation): effective_filter_size_rows = (weight - 1) * dilation + 1 out_rows = (in_ + stride - 1) // stride max(0, (out_rows - 1) * stride + effective_filter_size_rows - in_) padding_rows = max(0, (out_rows - 1) * stride + (weight - 1) * dilation + 1 - in_) rows_odd = padding_rows % 2 != 0 return padding_rows, rows_odd def forward(self, x): padding_rows, rows_odd = self.get_pad_odd(x.shape[2], self.weight. shape[2], self.stride[0], self.dilation[0]) padding_cols, cols_odd = self.get_pad_odd(x.shape[3], self.weight. shape[3], self.stride[1], self.dilation[1]) if rows_odd or cols_odd: x = F.pad(x, [0, int(cols_odd), 0, int(rows_odd)]) return F.conv2d(x, self.weight, self.bias, self.stride, padding=( padding_rows // 2, padding_cols // 2), dilation=self.dilation, groups=self.groups) 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 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_constant_pad_nd_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 400 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 5 % 5 x0 = xindex % 5 x2 = xindex // 25 x3 = xindex tmp0 = x1 tmp1 = tl.full([1], 4, tl.int64) tmp2 = tmp0 < tmp1 tmp3 = x0 tmp4 = tmp3 < tmp1 tmp5 = tmp2 & tmp4 tmp6 = tl.load(in_ptr0 + (x0 + 4 * x1 + 16 * x2), tmp5 & xmask, other=0.0) tl.store(out_ptr0 + x3, tmp6, xmask) @triton.jit def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 16 % 4 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tl.store(in_out_ptr0 + x3, tmp2, xmask) def call(args): primals_1, primals_2, primals_3 = 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, 5, 5), (100, 25, 5, 1), torch.float32) get_raw_stream(0) triton_poi_fused_constant_pad_nd_0[grid(400)](primals_1, buf0, 400, 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, 4, 4, 4), (64, 16, 4, 1)) buf2 = buf1 del buf1 triton_poi_fused_convolution_1[grid(256)](buf2, primals_3, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_3 return buf2, primals_2, buf0 class SamePadConv2dNew(nn.Conv2d): """ Conv with TF padding='same' https://github.com/pytorch/pytorch/issues/3867#issuecomment-349279036 """ def __init__(self, in_channels, out_channels, kernel_size, stride=1, dilation=1, groups=1, bias=True, padding_mode='zeros'): super().__init__(in_channels, out_channels, kernel_size, stride, 0, dilation, groups, bias, padding_mode) def get_pad_odd(self, in_, weight, stride, dilation): effective_filter_size_rows = (weight - 1) * dilation + 1 out_rows = (in_ + stride - 1) // stride max(0, (out_rows - 1) * stride + effective_filter_size_rows - in_) padding_rows = max(0, (out_rows - 1) * stride + (weight - 1) * dilation + 1 - in_) rows_odd = padding_rows % 2 != 0 return padding_rows, rows_odd 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]
jjeamin/obJDetection
SamePadConv2d
false
6,951
[ "MIT" ]
1
eb7fbc410beb00fad1a6477e827e9ce2d8efbac5
https://github.com/jjeamin/obJDetection/tree/eb7fbc410beb00fad1a6477e827e9ce2d8efbac5
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]
jiuney/XAI606-EEGNet
Conv2dWithConstraint
false
6,952
[ "MIT" ]
1
45ff28630ed1b09d0853f2cfb148a5dd2693e5ab
https://github.com/jiuney/XAI606-EEGNet/tree/45ff28630ed1b09d0853f2cfb148a5dd2693e5ab
CrossEntropyLossSoft
import torch class CrossEntropyLossSoft(torch.nn.modules.loss._Loss): """ inplace distillation for image classification """ def forward(self, output, target): output_log_prob = torch.nn.functional.log_softmax(output, dim=1) target = target.unsqueeze(1) output_log_prob = output_log_prob.unsqueeze(2) cross_entropy_loss = -torch.bmm(target, output_log_prob) return cross_entropy_loss def get_inputs(): return [torch.rand([4, 4]), torch.rand([4, 4])] def get_init_inputs(): return [[], {}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import math as tl_math assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused__log_softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) @triton.jit def triton_poi_fused__log_softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp2 = tl_math.exp(tmp1) tmp4 = tl_math.exp(tmp3) tmp5 = tmp2 + tmp4 tmp7 = tl_math.exp(tmp6) tmp8 = tmp5 + tmp7 tmp10 = tl_math.exp(tmp9) tmp11 = tmp8 + tmp10 tmp12 = tl_math.log(tmp11) tmp13 = tmp0 - tmp12 tl.store(out_ptr0 + x2, tmp13, xmask) @triton.jit def triton_poi_fused_neg_2(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 tl.store(in_out_ptr0 + x0, tmp1, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4), (4, 1)) assert_size_stride(arg1_1, (4, 4), (4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused__log_softmax_0[grid(16)](arg0_1, buf0, 16, XBLOCK= 16, num_warps=1, num_stages=1) del arg0_1 buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32) triton_poi_fused__log_softmax_1[grid(16)](buf0, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1) del buf0 buf2 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32) extern_kernels.bmm(reinterpret_tensor(arg1_1, (4, 1, 4), (4, 4, 1), 0), reinterpret_tensor(buf1, (4, 4, 1), (4, 1, 0), 0), out=buf2) del arg1_1 del buf1 buf3 = buf2 del buf2 triton_poi_fused_neg_2[grid(4)](buf3, 4, XBLOCK=4, num_warps=1, num_stages=1) return buf3, class CrossEntropyLossSoftNew(torch.nn.modules.loss._Loss): """ inplace distillation for image classification """ def forward(self, input_0, input_1): arg0_1 = input_0 arg1_1 = input_1 output = call([arg0_1, arg1_1]) return output[0]
jiuyecao/Opt-CoInfer
CrossEntropyLossSoft
false
6,953
[ "MIT" ]
1
60f29a28c34d3bf9b2f23c98bb8e98caf1abc4f0
https://github.com/jiuyecao/Opt-CoInfer/tree/60f29a28c34d3bf9b2f23c98bb8e98caf1abc4f0
Selector
import torch import torch.nn as nn import torch.utils.data class Selector(nn.Module): def __init__(self): super(Selector, self).__init__() self.conv1 = nn.Conv2d(2048 + 256, 256, 3) self.relu1 = nn.ReLU(inplace=True) self.conv2 = nn.Conv2d(256, 16, 3) self.relu2 = nn.ReLU(inplace=True) self.conv3 = nn.Conv2d(16, 1, 3) def forward(self, x): weights = self.relu1(self.conv1(x)) weights = self.relu2(self.conv2(weights)) weights = self.conv3(weights) return weights def get_inputs(): return [torch.rand([4, 2304, 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 import torch.utils.data 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 // 3844 % 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_relu_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 230400 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x1 = xindex // 3600 % 16 tmp0 = tl.load(in_out_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = tl.full([1], 0, tl.int32) tmp4 = triton_helpers.maximum(tmp3, tmp2) tl.store(in_out_ptr0 + x3, tmp4, xmask) @triton.jit def triton_poi_fused_convolution_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 13456 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_out_ptr0 + x0, xmask) tmp1 = tl.load(in_ptr0 + 0) tmp2 = tl.broadcast_to(tmp1, [XBLOCK]) tmp3 = tmp0 + tmp2 tl.store(in_out_ptr0 + x0, tmp3, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (256, 2304, 3, 3), (20736, 9, 3, 1)) assert_size_stride(primals_2, (256,), (1,)) assert_size_stride(primals_3, (4, 2304, 64, 64), (9437184, 4096, 64, 1)) assert_size_stride(primals_4, (16, 256, 3, 3), (2304, 9, 3, 1)) assert_size_stride(primals_5, (16,), (1,)) assert_size_stride(primals_6, (1, 16, 3, 3), (144, 9, 3, 1)) assert_size_stride(primals_7, (1,), (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, 256, 62, 62), (984064, 3844, 62, 1)) buf1 = buf0 del buf0 get_raw_stream(0) triton_poi_fused_convolution_relu_0[grid(3936256)](buf1, primals_2, 3936256, XBLOCK=512, num_warps=8, 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, 16, 60, 60), (57600, 3600, 60, 1)) buf3 = buf2 del buf2 triton_poi_fused_convolution_relu_1[grid(230400)](buf3, primals_5, 230400, XBLOCK=1024, 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, 1, 58, 58), (3364, 3364, 58, 1)) buf5 = buf4 del buf4 triton_poi_fused_convolution_2[grid(13456)](buf5, primals_7, 13456, XBLOCK=256, num_warps=4, num_stages=1) del primals_7 return buf5, primals_1, primals_3, primals_4, primals_6, buf1, buf3 class SelectorNew(nn.Module): def __init__(self): super(SelectorNew, self).__init__() self.conv1 = nn.Conv2d(2048 + 256, 256, 3) self.relu1 = nn.ReLU(inplace=True) self.conv2 = nn.Conv2d(256, 16, 3) self.relu2 = nn.ReLU(inplace=True) self.conv3 = nn.Conv2d(16, 1, 3) def forward(self, input_0): primals_1 = self.conv1.weight primals_2 = self.conv1.bias primals_4 = self.conv2.weight primals_5 = self.conv2.bias primals_6 = self.conv3.weight primals_7 = self.conv3.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
hsuanchuu/maskrcnn-benchmark
Selector
false
6,954
[ "MIT" ]
1
39429eca800fb912418c34d104ff6f3f2ea07bbd
https://github.com/hsuanchuu/maskrcnn-benchmark/tree/39429eca800fb912418c34d104ff6f3f2ea07bbd
ShuffleCatChunk
import torch import torch.nn as nn class ShuffleCatChunk(nn.Module): def forward(self, a, b): assert a.size() == b.size() _n, c, _h, _w = a.size() a = torch.chunk(a, chunks=c, dim=1) b = torch.chunk(b, chunks=c, dim=1) x = [None] * (c * 2) x[::2] = a x[1::2] = b x = torch.cat(x, dim=1) return x def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl. constexpr): xnumel = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 16 % 8 x0 = xindex % 16 x2 = xindex // 128 x3 = xindex tmp0 = x1 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 1, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (x0 + 64 * 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_ptr1 + (x0 + 64 * 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 + (16 + x0 + 64 * x2), tmp14 & xmask, eviction_policy='evict_last', other=0.0) tmp16 = tmp0 >= tmp12 tmp17 = tl.full([1], 4, tl.int64) tmp18 = tmp0 < tmp17 tmp19 = tmp16 & tmp18 tmp20 = tl.load(in_ptr1 + (16 + x0 + 64 * x2), tmp19 & xmask, eviction_policy='evict_last', other=0.0) tmp21 = tmp0 >= tmp17 tmp22 = tl.full([1], 5, tl.int64) tmp23 = tmp0 < tmp22 tmp24 = tmp21 & tmp23 tmp25 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), tmp24 & xmask, eviction_policy='evict_last', other=0.0) tmp26 = tmp0 >= tmp22 tmp27 = tl.full([1], 6, tl.int64) tmp28 = tmp0 < tmp27 tmp29 = tmp26 & tmp28 tmp30 = tl.load(in_ptr1 + (32 + x0 + 64 * x2), tmp29 & xmask, eviction_policy='evict_last', other=0.0) tmp31 = tmp0 >= tmp27 tmp32 = tl.full([1], 7, tl.int64) tmp33 = tmp0 < tmp32 tmp34 = tmp31 & tmp33 tmp35 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), tmp34 & xmask, eviction_policy='evict_last', other=0.0) tmp36 = tmp0 >= tmp32 tl.full([1], 8, tl.int64) tmp39 = tl.load(in_ptr1 + (48 + x0 + 64 * x2), tmp36 & xmask, eviction_policy='evict_last', other=0.0) tmp40 = tl.where(tmp34, tmp35, tmp39) tmp41 = tl.where(tmp29, tmp30, tmp40) tmp42 = tl.where(tmp24, tmp25, tmp41) tmp43 = tl.where(tmp19, tmp20, tmp42) tmp44 = tl.where(tmp14, tmp15, tmp43) tmp45 = tl.where(tmp9, tmp10, tmp44) tmp46 = tl.where(tmp4, tmp5, tmp45) tl.store(out_ptr0 + x3, tmp46, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 8, 4, 4), (128, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_cat_0[grid(512)](arg0_1, arg1_1, buf0, 512, XBLOCK =128, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf0, class ShuffleCatChunkNew(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]
jjkennedy3/PINTO_model_zoo
ShuffleCatChunk
false
6,955
[ "MIT" ]
1
a181c3015a6241873798c4ad3eadd4ce97024f70
https://github.com/jjkennedy3/PINTO_model_zoo/tree/a181c3015a6241873798c4ad3eadd4ce97024f70
ShuffleCat
import torch import torch.nn as nn class ShuffleCat(nn.Module): def forward(self, a, b): assert a.size() == b.size() n, c, h, w = a.size() a = a.permute(0, 2, 3, 1).contiguous().view(-1, c) b = b.permute(0, 2, 3, 1).contiguous().view(-1, c) x = torch.cat((a, b), dim=0).transpose(1, 0).contiguous() x = x.view(c * 2, n, h, w).permute(1, 0, 2, 3) return x 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 reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_clone_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl .constexpr): xnumel = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex % 128 x1 = xindex // 128 x2 = xindex tmp0 = x0 tl.full([1], 0, tl.int64) tmp3 = tl.full([1], 64, tl.int64) tmp4 = tmp0 < tmp3 tmp5 = tl.load(in_ptr0 + (16 * x1 + 64 * (x0 // 16 % 4) + x0 % 16), tmp4 & xmask, eviction_policy='evict_last', other=0.0) tmp6 = tmp0 >= tmp3 tl.full([1], 128, tl.int64) tmp9 = tl.load(in_ptr1 + (16 * x1 + 64 * ((-64 + x0) // 16 % 4) + (-64 + x0) % 16), 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, 128), (128, 1), torch.float32) get_raw_stream(0) triton_poi_fused_clone_0[grid(512)](arg0_1, arg1_1, buf0, 512, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return reinterpret_tensor(buf0, (4, 8, 4, 4), (16, 64, 4, 1), 0), class ShuffleCatNew(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]
jjkennedy3/PINTO_model_zoo
ShuffleCat
false
6,956
[ "MIT" ]
1
a181c3015a6241873798c4ad3eadd4ce97024f70
https://github.com/jjkennedy3/PINTO_model_zoo/tree/a181c3015a6241873798c4ad3eadd4ce97024f70
BatchNormDense
import torch import torch.nn as nn from torch.nn.parameter import Parameter class BatchNormDense(nn.Module): def __init__(self, num_features, eps=1e-08): super().__init__() self.num_features = num_features self.eps = eps self.gamma = Parameter(torch.Tensor(num_features)) self.beta = Parameter(torch.Tensor(num_features)) self.reset_parameters() def reset_parameters(self): nn.init.ones_(self.gamma) nn.init.zeros_(self.beta) def forward(self, x): means = x.mean(dim=0) variances = x.var(dim=0) x = (x - means) / torch.sqrt(variances + self.eps) return self.gamma * x + self.beta 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.nn as nn from torch.nn.parameter import Parameter assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_add_div_mean_mul_sqrt_sub_var_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 x3 = xindex x4 = xindex % 64 tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + x3, xmask) tmp2 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + (64 + x4), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr1 + (128 + x4), xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr1 + (192 + x4), xmask, eviction_policy='evict_last') tmp30 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last') tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp8 = tmp6 + tmp7 tmp9 = 4.0 tmp10 = tmp8 / tmp9 tmp11 = tmp1 - tmp10 tmp12 = tmp2 - tmp10 tmp13 = tmp12 * tmp12 tmp14 = tmp3 - tmp10 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp10 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp7 - tmp10 tmp21 = tmp20 * tmp20 tmp22 = tmp19 + tmp21 tmp23 = 3.0 tmp24 = tmp22 / tmp23 tmp25 = 1e-08 tmp26 = tmp24 + tmp25 tmp27 = libdevice.sqrt(tmp26) tmp28 = tmp11 / tmp27 tmp29 = tmp0 * tmp28 tmp31 = tmp29 + tmp30 tl.store(out_ptr0 + x3, tmp31, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_add_div_mean_mul_sqrt_sub_var_0[grid(256)](primals_2, primals_1, primals_3, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 del primals_3 return buf0, primals_1 class BatchNormDenseNew(nn.Module): def __init__(self, num_features, eps=1e-08): super().__init__() self.num_features = num_features self.eps = eps self.gamma = Parameter(torch.Tensor(num_features)) self.beta = Parameter(torch.Tensor(num_features)) self.reset_parameters() def reset_parameters(self): nn.init.ones_(self.gamma) nn.init.zeros_(self.beta) def forward(self, input_0): primals_2 = self.gamma primals_3 = self.beta primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
jkoscialkowski/dnn-exercises
BatchNormDense
false
6,957
[ "MIT" ]
1
5d1616fce1b461e39858c68279d2fafefab00a56
https://github.com/jkoscialkowski/dnn-exercises/tree/5d1616fce1b461e39858c68279d2fafefab00a56
BasicBlock
import torch import torch.nn as nn import torch.utils.data def conv1x1(in_planes, out_planes, stride=1): """1x1 convolution""" return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False) def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1): """3x3 convolution with padding""" return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=dilation, groups=groups, bias=False, dilation=dilation) class BasicBlock(nn.Module): expansion = 1 __constants__ = ['downsample'] def __init__(self, inplanes, planes, stride=1, downsample=None, groups= 1, base_width=64, dilation=1, norm_model='instance'): super(BasicBlock, self).__init__() if 'instance' == norm_model: norm_layer = nn.InstanceNorm2d else: norm_layer = nn.BatchNorm2d if groups != 1 or base_width != 64: raise ValueError( 'BasicBlock only supports groups=1 and base_width=64') if dilation > 1: raise NotImplementedError( 'Dilation > 1 not supported in BasicBlock') self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = norm_layer(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes) self.bn2 = norm_layer(planes) self.stride = stride if stride != 1 or inplanes != planes: self.downsample = nn.Sequential(conv1x1(inplanes, planes, stride), norm_layer(planes)) else: self.downsample = downsample def forward(self, x): identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if self.downsample is not None: identity = self.downsample(x) out += identity out = self.relu(out) return out def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'inplanes': 4, 'planes': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn import torch.utils.data assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_per_fused__native_batch_norm_legit_relu_0(in_ptr0, out_ptr0, out_ptr2, out_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 16 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tl.where(xmask, tmp1, 0) tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.full([XBLOCK, 1], 16, tl.int32) tmp9 = tmp8.to(tl.float32) tmp10 = tmp7 / tmp9 tmp11 = tmp1 - tmp10 tmp12 = tmp11 * tmp11 tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK]) tmp15 = tl.where(xmask, tmp13, 0) tmp16 = tl.sum(tmp15, 1)[:, None] tmp17 = tmp0 - tmp10 tmp18 = 16.0 tmp19 = tmp16 / tmp18 tmp20 = 1e-05 tmp21 = tmp19 + tmp20 tmp22 = libdevice.rsqrt(tmp21) tmp23 = tmp17 * tmp22 tmp24 = tl.full([1, 1], 0, tl.int32) tmp25 = triton_helpers.maximum(tmp24, tmp23) tl.store(out_ptr2 + (r1 + 16 * x0), tmp25, xmask) tl.store(out_ptr3 + x0, tmp22, xmask) tl.store(out_ptr0 + x0, tmp10, xmask) @triton.jit def triton_per_fused__native_batch_norm_legit_relu_threshold_backward_1(in_ptr0 , in_ptr1, out_ptr0, out_ptr2, out_ptr3, out_ptr4, xnumel, rnumel, XBLOCK: tl.constexpr): xnumel = 16 RBLOCK: tl.constexpr = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rindex = tl.arange(0, RBLOCK)[None, :] tl.full([XBLOCK, RBLOCK], True, tl.int1) r1 = rindex x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0) tmp24 = tl.load(in_ptr1 + (r1 + 16 * x0), xmask, other=0.0) tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK]) tl.where(xmask, tmp1, 0) tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK]) tmp6 = tl.where(xmask, tmp4, 0) tmp7 = tl.sum(tmp6, 1)[:, None] tmp8 = tl.full([XBLOCK, 1], 16, tl.int32) tmp9 = tmp8.to(tl.float32) tmp10 = tmp7 / tmp9 tmp11 = tmp1 - tmp10 tmp12 = tmp11 * tmp11 tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK]) tmp15 = tl.where(xmask, tmp13, 0) tmp16 = tl.sum(tmp15, 1)[:, None] tmp17 = tmp0 - tmp10 tmp18 = 16.0 tmp19 = tmp16 / tmp18 tmp20 = 1e-05 tmp21 = tmp19 + tmp20 tmp22 = libdevice.rsqrt(tmp21) tmp23 = tmp17 * tmp22 tmp25 = tmp23 + tmp24 tmp26 = tl.full([1, 1], 0, tl.int32) tmp27 = triton_helpers.maximum(tmp26, tmp25) tmp28 = 0.0 tmp29 = tmp27 <= tmp28 tl.store(out_ptr2 + (r1 + 16 * x0), tmp27, xmask) tl.store(out_ptr3 + (r1 + 16 * x0), tmp29, xmask) tl.store(out_ptr4 + x0, tmp22, xmask) tl.store(out_ptr0 + x0, tmp10, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4, 4, 3, 3), (36, 9, 3, 1)) assert_size_stride(primals_3, (4, 4, 3, 3), (36, 9, 3, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1)) buf1 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32 ) buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf4 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32 ) get_raw_stream(0) triton_per_fused__native_batch_norm_legit_relu_0[grid(16)](buf0, buf1, buf5, buf4, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) buf6 = extern_kernels.convolution(buf5, primals_3, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None) assert_size_stride(buf6, (4, 4, 4, 4), (64, 16, 4, 1)) buf7 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32 ) buf11 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) buf12 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool) buf10 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch. float32) triton_per_fused__native_batch_norm_legit_relu_threshold_backward_1[ grid(16)](buf6, primals_1, buf7, buf11, buf12, buf10, 16, 16, XBLOCK=8, num_warps=2, num_stages=1) return buf11, primals_1, primals_2, primals_3, buf0, reinterpret_tensor( buf4, (16,), (1,), 0), buf5, buf6, reinterpret_tensor(buf10, (16,), (1,), 0), buf12, reinterpret_tensor(buf7, (1, 16, 1, 1), (16, 1, 1, 1), 0), reinterpret_tensor(buf1, (1, 16, 1, 1), (16, 1, 1, 1), 0) def conv1x1(in_planes, out_planes, stride=1): """1x1 convolution""" return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False) def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1): """3x3 convolution with padding""" return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=dilation, groups=groups, bias=False, dilation=dilation) class BasicBlockNew(nn.Module): expansion = 1 __constants__ = ['downsample'] def __init__(self, inplanes, planes, stride=1, downsample=None, groups= 1, base_width=64, dilation=1, norm_model='instance'): super(BasicBlockNew, self).__init__() if 'instance' == norm_model: norm_layer = nn.InstanceNorm2d else: norm_layer = nn.BatchNorm2d if groups != 1 or base_width != 64: raise ValueError( 'BasicBlock only supports groups=1 and base_width=64') if dilation > 1: raise NotImplementedError( 'Dilation > 1 not supported in BasicBlock') self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = norm_layer(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes) self.bn2 = norm_layer(planes) self.stride = stride if stride != 1 or inplanes != planes: self.downsample = nn.Sequential(conv1x1(inplanes, planes, stride), norm_layer(planes)) else: self.downsample = downsample def forward(self, input_0): primals_2 = self.conv1.weight primals_3 = self.conv2.weight primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
jinwoo1225/MichiGAN-HAiR
BasicBlock
false
6,958
[ "MIT" ]
1
dece2ad2e93de3a7c52b4a657ecc0f1a667ccc7e
https://github.com/jinwoo1225/MichiGAN-HAiR/tree/dece2ad2e93de3a7c52b4a657ecc0f1a667ccc7e
ShuffleCatAlt
import torch import torch.nn as nn class ShuffleCatAlt(nn.Module): def forward(self, a, b): assert a.size() == b.size() n, c, h, w = a.size() x = torch.zeros(n, c * 2, h, w, dtype=a.dtype, device=a.device) x[:, ::2] = a x[:, 1::2] = b return x def get_inputs(): return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_copy_zeros_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 512 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x1 = xindex // 16 % 8 x0 = xindex % 16 x2 = xindex // 128 x3 = xindex tmp0 = x1 tmp1 = tl.full([1], 1, tl.int64) tmp2 = tmp0 >= tmp1 tmp3 = (-1 + x1) % 2 tmp4 = tl.full([1], 0, tl.int64) tmp5 = tmp3 == tmp4 tmp6 = tmp2 & tmp5 tmp7 = tl.load(in_ptr0 + (x0 + 16 * triton_helpers.div_floor_integer(-1 + x1, 2) + 64 * x2), tmp6 & xmask, other=0.0) tmp8 = x3 // 16 % 8 % 2 tmp9 = tmp8 == tmp4 tmp10 = tl.load(in_ptr1 + (x0 + 16 * (x1 // 2) + 64 * x2), tmp9 & xmask, other=0.0) tmp11 = 0.0 tmp12 = tl.where(tmp9, tmp10, tmp11) tmp13 = tl.where(tmp6, tmp7, tmp12) tl.store(out_ptr0 + x3, tmp13, xmask) def call(args): arg0_1, arg1_1 = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 8, 4, 4), (128, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_copy_zeros_0[grid(512)](arg1_1, arg0_1, buf0, 512, XBLOCK=128, num_warps=4, num_stages=1) del arg0_1 del arg1_1 return buf0, class ShuffleCatAltNew(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]
jjkennedy3/PINTO_model_zoo
ShuffleCatAlt
false
6,959
[ "MIT" ]
1
a181c3015a6241873798c4ad3eadd4ce97024f70
https://github.com/jjkennedy3/PINTO_model_zoo/tree/a181c3015a6241873798c4ad3eadd4ce97024f70
DummyMCObjective
from torch.nn import Module import torch from torch import Tensor from abc import ABC from abc import abstractmethod class AcquisitionObjective(Module, ABC): """Abstract base class for objectives.""" ... class MCAcquisitionObjective(AcquisitionObjective): """Abstract base class for MC-based objectives.""" @abstractmethod def forward(self, samples: 'Tensor') ->Tensor: """Evaluate the objective on the samples. Args: samples: A `sample_shape x batch_shape x q x m`-dim Tensors of samples from a model posterior. Returns: Tensor: A `sample_shape x batch_shape x q`-dim Tensor of objective values (assuming maximization). This method is usually not called directly, but via the objectives Example: >>> # `__call__` method: >>> samples = sampler(posterior) >>> outcome = mc_obj(samples) """ pass class DummyMCObjective(MCAcquisitionObjective): def forward(self, samples: 'Tensor') ->Tensor: return samples.sum(-1) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch.nn import Module from torch import Tensor from abc import ABC from abc import abstractmethod assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_poi_fused_sum_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tl.store(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_sum_0[grid(64)](arg0_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1) del arg0_1 return buf0, class AcquisitionObjective(Module, ABC): """Abstract base class for objectives.""" ... class MCAcquisitionObjective(AcquisitionObjective): """Abstract base class for MC-based objectives.""" @abstractmethod def forward(self, samples: 'Tensor') ->Tensor: """Evaluate the objective on the samples. Args: samples: A `sample_shape x batch_shape x q x m`-dim Tensors of samples from a model posterior. Returns: Tensor: A `sample_shape x batch_shape x q`-dim Tensor of objective values (assuming maximization). This method is usually not called directly, but via the objectives Example: >>> # `__call__` method: >>> samples = sampler(posterior) >>> outcome = mc_obj(samples) """ pass class DummyMCObjectiveNew(MCAcquisitionObjective): def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
jmren168/botorch
DummyMCObjective
false
6,960
[ "MIT" ]
1
6c067185f56d3a244c4093393b8a97388fb1c0b3
https://github.com/jmren168/botorch/tree/6c067185f56d3a244c4093393b8a97388fb1c0b3
PolicyNetworkGridworld
import torch import torch.nn as nn import torch.nn.functional as F class PolicyNetworkGridworld(nn.Module): """ Deep neural network which represents policy network. """ def __init__(self, input_size, num_actions): super(PolicyNetworkGridworld, self).__init__() self.linear1 = nn.Linear(input_size, 50) self.linear2 = nn.Linear(50, 50) self.head = nn.Linear(50, num_actions) def forward(self, x): x = F.leaky_relu(self.linear1(x)) x = F.leaky_relu(self.linear2(x)) return F.softmax(self.head(x)) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'num_actions': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime import triton_helpers 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_leaky_relu_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 3200 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 50 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = 0.0 tmp4 = tmp2 > tmp3 tmp5 = 0.01 tmp6 = tmp2 * tmp5 tmp7 = tl.where(tmp4, tmp2, tmp6) tl.store(out_ptr0 + x2, tmp4, xmask) tl.store(out_ptr1 + x2, tmp7, xmask) @triton.jit def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = triton_helpers.maximum(tmp1, tmp2) tmp5 = triton_helpers.maximum(tmp3, tmp4) tmp7 = triton_helpers.maximum(tmp5, tmp6) tmp8 = tmp0 - tmp7 tmp9 = tl_math.exp(tmp8) tl.store(out_ptr0 + x3, tmp9, xmask) @triton.jit def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr ): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tmp1 + tmp2 tmp5 = tmp3 + tmp4 tmp7 = tmp5 + tmp6 tmp8 = tmp0 / tmp7 tl.store(out_ptr0 + x3, tmp8, xmask) 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, (50, 4), (4, 1)) assert_size_stride(primals_2, (50,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (50, 50), (50, 1)) assert_size_stride(primals_5, (50,), (1,)) assert_size_stride(primals_6, (4, 50), (50, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 50), (50, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 50), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.bool) buf2 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch. float32) get_raw_stream(0) triton_poi_fused_leaky_relu_0[grid(3200)](buf0, primals_2, buf1, buf2, 3200, XBLOCK=128, num_warps=4, num_stages=1) del primals_2 buf3 = buf0 del buf0 extern_kernels.mm(reinterpret_tensor(buf2, (64, 50), (50, 1), 0), reinterpret_tensor(primals_4, (50, 50), (1, 50), 0), out=buf3) buf4 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.bool) buf5 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch. float32) triton_poi_fused_leaky_relu_0[grid(3200)](buf3, primals_5, buf4, buf5, 3200, XBLOCK=128, num_warps=4, num_stages=1) del buf3 del primals_5 buf6 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf5, (64, 50), (50, 1), 0), reinterpret_tensor(primals_6, (50, 4), (1, 50), 0), alpha=1, beta=1, out=buf6) del primals_7 buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused__softmax_1[grid(256)](buf6, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1) buf8 = reinterpret_tensor(buf6, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf6 triton_poi_fused__softmax_2[grid(256)](buf7, buf8, 256, XBLOCK=256, num_warps=4, num_stages=1) del buf7 return buf8, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf1, reinterpret_tensor(buf2, (64, 50), (50, 1), 0 ), buf4, reinterpret_tensor(buf5, (64, 50), (50, 1), 0 ), buf8, primals_6, primals_4 class PolicyNetworkGridworldNew(nn.Module): """ Deep neural network which represents policy network. """ def __init__(self, input_size, num_actions): super(PolicyNetworkGridworldNew, self).__init__() self.linear1 = nn.Linear(input_size, 50) self.linear2 = nn.Linear(50, 50) self.head = nn.Linear(50, num_actions) def forward(self, input_0): primals_1 = self.linear1.weight primals_2 = self.linear1.bias primals_4 = self.linear2.weight primals_5 = self.linear2.bias primals_6 = self.head.weight primals_7 = self.head.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
jlebensold/flrl-ddpg
PolicyNetworkGridworld
false
6,961
[ "MIT" ]
1
d91e9f4aedf48d0614e33bd22c7f684ecda089b1
https://github.com/jlebensold/flrl-ddpg/tree/d91e9f4aedf48d0614e33bd22c7f684ecda089b1
DQNGridworld
import torch import torch.nn as nn import torch.nn.functional as F class DQNGridworld(nn.Module): """ Deep neural network with represents an agent. """ def __init__(self, input_size, num_actions): super(DQNGridworld, self).__init__() self.linear1 = nn.Linear(input_size, 50) self.linear2 = nn.Linear(50, 50) self.head = nn.Linear(50, num_actions) def forward(self, x): x = F.leaky_relu(self.linear1(x)) x = F.leaky_relu(self.linear2(x)) return self.head(x) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'input_size': 4, 'num_actions': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream 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 = 3200 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 50 tmp0 = tl.load(in_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, (50, 4), (4, 1)) assert_size_stride(primals_2, (50,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (50, 50), (50, 1)) assert_size_stride(primals_5, (50,), (1,)) assert_size_stride(primals_6, (4, 50), (50, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 50), (50, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 50), (1, 4), 0), out=buf0) del primals_1 buf1 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.bool) buf2 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch. float32) get_raw_stream(0) triton_poi_fused_leaky_relu_0[grid(3200)](buf0, primals_2, buf1, buf2, 3200, XBLOCK=256, num_warps=4, num_stages=1) del primals_2 buf3 = buf0 del buf0 extern_kernels.mm(reinterpret_tensor(buf2, (64, 50), (50, 1), 0), reinterpret_tensor(primals_4, (50, 50), (1, 50), 0), out=buf3) buf4 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch.bool) buf5 = empty_strided_cuda((4, 4, 4, 50), (800, 200, 50, 1), torch. float32) triton_poi_fused_leaky_relu_0[grid(3200)](buf3, primals_5, buf4, buf5, 3200, XBLOCK=256, num_warps=4, num_stages=1) del buf3 del primals_5 buf6 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf5, (64, 50), (50, 1), 0), reinterpret_tensor(primals_6, (50, 4), (1, 50), 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, 50), (50, 1), 0 ), buf4, reinterpret_tensor(buf5, (64, 50), (50, 1), 0 ), primals_6, primals_4 class DQNGridworldNew(nn.Module): """ Deep neural network with represents an agent. """ def __init__(self, input_size, num_actions): super(DQNGridworldNew, self).__init__() self.linear1 = nn.Linear(input_size, 50) self.linear2 = nn.Linear(50, 50) self.head = nn.Linear(50, num_actions) def forward(self, input_0): primals_1 = self.linear1.weight primals_2 = self.linear1.bias primals_4 = self.linear2.weight primals_5 = self.linear2.bias primals_6 = self.head.weight primals_7 = self.head.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
jlebensold/flrl-ddpg
DQNGridworld
false
6,962
[ "MIT" ]
1
d91e9f4aedf48d0614e33bd22c7f684ecda089b1
https://github.com/jlebensold/flrl-ddpg/tree/d91e9f4aedf48d0614e33bd22c7f684ecda089b1
ProjectionHead
import torch from torch import nn as nn class ProjectionHead(nn.Module): def __init__(self, embedding_dim, projection_dim, dropout): super().__init__() self.projection = nn.Linear(embedding_dim, projection_dim) self.gelu = nn.GELU() self.fc = nn.Linear(projection_dim, projection_dim) self.dropout = nn.Dropout(dropout) self.layer_norm = nn.LayerNorm(projection_dim) def forward(self, x): projected = self.projection(x) x = self.gelu(projected) x = self.fc(x) x = self.dropout(x) x = x + projected x = self.layer_norm(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'embedding_dim': 4, 'projection_dim': 4, 'dropout': 0.5}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice from torch import nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_gelu_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + x0, xmask) tmp1 = 0.5 tmp2 = tmp0 * tmp1 tmp3 = 0.7071067811865476 tmp4 = tmp0 * tmp3 tmp5 = libdevice.erf(tmp4) tmp6 = 1.0 tmp7 = tmp5 + tmp6 tmp8 = tmp2 * tmp7 tl.store(out_ptr0 + x0, tmp8, xmask) @triton.jit def triton_poi_fused_add_1(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x0 = xindex % 4 tmp0 = tl.load(in_out_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr1 + x2, xmask) tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tl.store(in_out_ptr0 + x2, tmp4, xmask) @triton.jit def triton_poi_fused_native_layer_norm_2(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr): xnumel = 64 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp4 = tmp2 + tmp3 tmp6 = tmp4 + tmp5 tmp7 = 4.0 tmp8 = tmp6 / tmp7 tmp9 = tmp0 - tmp8 tmp10 = tmp9 * tmp9 tmp11 = tmp1 - tmp8 tmp12 = tmp11 * tmp11 tmp13 = tmp10 + tmp12 tmp14 = tmp3 - tmp8 tmp15 = tmp14 * tmp14 tmp16 = tmp13 + tmp15 tmp17 = tmp5 - tmp8 tmp18 = tmp17 * tmp17 tmp19 = tmp16 + tmp18 tmp20 = tmp19 / tmp7 tmp21 = 1e-05 tmp22 = tmp20 + tmp21 tmp23 = libdevice.rsqrt(tmp22) tl.store(out_ptr0 + x0, tmp8, xmask) tl.store(out_ptr1 + x0, tmp23, xmask) @triton.jit def triton_poi_fused_native_layer_norm_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 x0 = xindex % 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last') tmp5 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last') tmp2 = tmp0 - tmp1 tmp4 = tmp2 * tmp3 tmp6 = tmp4 * tmp5 tmp8 = tmp6 + tmp7 tl.store(out_ptr0 + x2, tmp8, xmask) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (4, 4), (4, 1)) assert_size_stride(primals_5, (4,), (1,)) assert_size_stride(primals_6, (4,), (1,)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_gelu_0[grid(256)](buf0, buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(buf1, (64, 4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0) del buf2 triton_poi_fused_add_1[grid(256)](buf3, primals_5, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) buf5 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32) triton_poi_fused_native_layer_norm_2[grid(64)](buf3, buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1) buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_native_layer_norm_3[grid(256)](buf3, buf4, buf5, primals_6, primals_7, buf6, 256, XBLOCK=128, num_warps=4, num_stages=1) del buf4 del buf5 del primals_7 return buf6, primals_6, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf0, reinterpret_tensor(buf1, (64, 4), (4, 1), 0), buf3, primals_4 class ProjectionHeadNew(nn.Module): def __init__(self, embedding_dim, projection_dim, dropout): super().__init__() self.projection = nn.Linear(embedding_dim, projection_dim) self.gelu = nn.GELU() self.fc = nn.Linear(projection_dim, projection_dim) self.dropout = nn.Dropout(dropout) self.layer_norm = nn.LayerNorm(projection_dim) def forward(self, input_0): primals_1 = self.projection.weight primals_2 = self.projection.bias primals_4 = self.fc.weight primals_5 = self.fc.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]
jianzhnie/MultimodalTransformer
ProjectionHead
false
6,963
[ "Apache-2.0" ]
1
6cd4ca8034a53da361149745aecead68fbe304a0
https://github.com/jianzhnie/MultimodalTransformer/tree/6cd4ca8034a53da361149745aecead68fbe304a0
FFDNN
import torch import torch as tc import torch.nn as nn class FFDNN(nn.Module): def __init__(self, insize, action_space): super(FFDNN, self).__init__() self.input = nn.Linear(insize, 64) self.layer1 = nn.Linear(64, 32) self.layer2 = nn.Linear(32, action_space) def forward(self, x): x = tc.tanh(self.input(x)) x = tc.tanh(self.layer1(x)) x = self.layer2(x) return x def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'insize': 4, 'action_space': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_tanh_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 64 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = libdevice.tanh(tmp2) tl.store(in_out_ptr0 + x2, tmp3, None) @triton.jit def triton_poi_fused_tanh_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr ): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] tl.full([XBLOCK], True, tl.int1) x2 = xindex x0 = xindex % 32 tmp0 = tl.load(in_out_ptr0 + x2, None) tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last') tmp2 = tmp0 + tmp1 tmp3 = libdevice.tanh(tmp2) tl.store(in_out_ptr0 + x2, tmp3, None) def call(args): (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7) = args args.clear() assert_size_stride(primals_1, (64, 4), (4, 1)) assert_size_stride(primals_2, (64,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_4, (32, 64), (64, 1)) assert_size_stride(primals_5, (32,), (1,)) assert_size_stride(primals_6, (4, 32), (32, 1)) assert_size_stride(primals_7, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 64), (64, 1), torch.float32) extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 64), (1, 4), 0), out=buf0) del primals_1 buf1 = reinterpret_tensor(buf0, (4, 4, 4, 64), (1024, 256, 64, 1), 0) del buf0 get_raw_stream(0) triton_poi_fused_tanh_0[grid(4096)](buf1, primals_2, 4096, 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, 64), (64, 1), 0), reinterpret_tensor(primals_4, (64, 32), (1, 64), 0), out=buf2) buf3 = reinterpret_tensor(buf2, (4, 4, 4, 32), (512, 128, 32, 1), 0) del buf2 triton_poi_fused_tanh_1[grid(2048)](buf3, primals_5, 2048, XBLOCK= 128, num_warps=4, num_stages=1) del primals_5 buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (64, 32), (32, 1), 0), reinterpret_tensor(primals_6, (32, 4), (1, 32), 0), alpha=1, beta=1, out=buf4) del primals_7 return reinterpret_tensor(buf4, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0 ), buf1, buf3, primals_6, primals_4 class FFDNNNew(nn.Module): def __init__(self, insize, action_space): super(FFDNNNew, self).__init__() self.input = nn.Linear(insize, 64) self.layer1 = nn.Linear(64, 32) self.layer2 = nn.Linear(32, action_space) def forward(self, input_0): primals_1 = self.input.weight primals_2 = self.input.bias primals_4 = self.layer1.weight primals_5 = self.layer1.bias primals_6 = self.layer2.weight primals_7 = self.layer2.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7]) return output[0]
johan-gras/rl-camb-kaggle-connect-x
FFDNN
false
6,964
[ "Apache-2.0" ]
1
764463e556c5aea6f61390d2fec83f363510d029
https://github.com/johan-gras/rl-camb-kaggle-connect-x/tree/764463e556c5aea6f61390d2fec83f363510d029
EncoderImagePrecomp
import torch import numpy as np from collections import OrderedDict import torch.nn as nn import torch.nn.init def l2norm(X): """L2-normalize columns of X """ norm = torch.pow(X, 2).sum(dim=1).sqrt() X = torch.div(X, norm.unsqueeze(1).expand_as(X)) return X class EncoderImagePrecomp(nn.Module): def __init__(self, img_dim, embed_size, use_abs=False, no_imgnorm=False): super(EncoderImagePrecomp, self).__init__() self.embed_size = embed_size self.no_imgnorm = no_imgnorm self.use_abs = use_abs self.fc = nn.Linear(img_dim, embed_size) self.init_weights() def init_weights(self): """Xavier initialization for the fully connected layer """ r = np.sqrt(6.0) / np.sqrt(self.fc.in_features + self.fc.out_features) self.fc.weight.data.uniform_(-r, r) self.fc.bias.data.fill_(0) def forward(self, images): """Extract image feature vectors.""" features = self.fc(images) if not self.no_imgnorm: features = l2norm(features) if self.use_abs: features = torch.abs(features) return features def load_state_dict(self, state_dict): """Copies parameters. overwritting the default one to accept state_dict from Full model """ own_state = self.state_dict() new_state = OrderedDict() for name, param in state_dict.items(): if name in own_state: new_state[name] = param super(EncoderImagePrecomp, self).load_state_dict(new_state) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'img_dim': 4, 'embed_size': 4}]
import torch from torch._inductor.select_algorithm import extern_kernels import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import numpy as np from collections import OrderedDict import torch.nn as nn import torch.nn.init assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor @triton.jit def triton_poi_fused_div_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex x0 = xindex % 16 x2 = xindex // 64 tmp0 = tl.load(in_ptr0 + x3, xmask) tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp3 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp6 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp9 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy= 'evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = libdevice.sqrt(tmp11) tmp13 = tmp0 / tmp12 tl.store(out_ptr0 + x3, tmp13, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0 ), alpha=1, beta=1, out=buf0) del primals_1 del primals_2 buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_div_0[grid(256)](buf0, buf1, 256, XBLOCK=128, num_warps=4, num_stages=1) return buf1, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), buf0 def l2norm(X): """L2-normalize columns of X """ norm = torch.pow(X, 2).sum(dim=1).sqrt() X = torch.div(X, norm.unsqueeze(1).expand_as(X)) return X class EncoderImagePrecompNew(nn.Module): def __init__(self, img_dim, embed_size, use_abs=False, no_imgnorm=False): super(EncoderImagePrecompNew, self).__init__() self.embed_size = embed_size self.no_imgnorm = no_imgnorm self.use_abs = use_abs self.fc = nn.Linear(img_dim, embed_size) self.init_weights() def init_weights(self): """Xavier initialization for the fully connected layer """ r = np.sqrt(6.0) / np.sqrt(self.fc.in_features + self.fc.out_features) self.fc.weight.data.uniform_(-r, r) self.fc.bias.data.fill_(0) def load_state_dict(self, state_dict): """Copies parameters. overwritting the default one to accept state_dict from Full model """ own_state = self.state_dict() new_state = OrderedDict() for name, param in state_dict.items(): if name in own_state: new_state[name] = param super(EncoderImagePrecompNew, self).load_state_dict(new_state) def forward(self, input_0): primals_1 = self.fc.weight primals_2 = self.fc.bias primals_3 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
joannezhouyi/visual_textual_cross_retrieval
EncoderImagePrecomp
false
6,965
[ "Apache-2.0" ]
1
6d5c55a475af74bba63887fff0774d5597830a2b
https://github.com/joannezhouyi/visual_textual_cross_retrieval/tree/6d5c55a475af74bba63887fff0774d5597830a2b
BatchNormConv
import torch import torch.nn as nn from torch.nn.parameter import Parameter class BatchNormConv(nn.Module): def __init__(self, num_channels, eps=1e-08): super().__init__() self.num_channels = num_channels self.eps = eps self.gamma = Parameter(torch.Tensor(num_channels)) self.beta = Parameter(torch.Tensor(num_channels)) self.reset_parameters() def reset_parameters(self): nn.init.ones_(self.gamma) nn.init.zeros_(self.beta) def forward(self, x): means = x.mean(dim=(0, 2, 3)) variances = x.var(dim=(0, 2, 3)) x = (x.permute(0, 2, 3, 1) - means) / torch.sqrt(variances + self.eps) x = self.gamma * x + self.beta return x.permute(0, 3, 1, 2) def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'num_channels': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream from torch._inductor.runtime.triton_helpers import libdevice import torch.nn as nn from torch.nn.parameter import Parameter assert_size_stride = torch._C._dynamo.guards.assert_size_stride empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda @triton.jit def triton_per_fused_add_mean_sqrt_var_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 % 16 r2 = rindex // 16 x0 = xindex tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0 + 64 * r2), 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 = 1e-08 tmp24 = tmp22 + tmp23 tmp25 = libdevice.sqrt(tmp24) tl.debug_barrier() tl.store(in_out_ptr0 + x0, tmp20, xmask) tl.debug_barrier() tl.store(in_out_ptr1 + x0, tmp25, xmask) @triton.jit def triton_poi_fused_add_div_mul_sub_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl. constexpr): ynumel = 64 xnumel = 4 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 16 y1 = yindex // 16 y3 = yindex tmp0 = tl.load(in_ptr0 + x2, xmask, eviction_policy='evict_last') tmp1 = tl.load(in_ptr1 + (y0 + 16 * x2 + 64 * y1), xmask & ymask, eviction_policy='evict_last') tmp2 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last') tmp4 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last') tmp7 = tl.load(in_ptr4 + x2, xmask, eviction_policy='evict_last') tmp3 = tmp1 - tmp2 tmp5 = tmp3 / tmp4 tmp6 = tmp0 * tmp5 tmp8 = tmp6 + tmp7 tl.store(out_ptr0 + (x2 + 4 * y3), tmp8, xmask & ymask) @triton.jit def triton_poi_fused_add_div_mul_permute_sub_2(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr): ynumel = 16 xnumel = 16 yoffset = tl.program_id(1) * YBLOCK yindex = yoffset + tl.arange(0, YBLOCK)[None, :] ymask = yindex < ynumel xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel x2 = xindex y0 = yindex % 4 y1 = yindex // 4 y3 = yindex tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 64 * y1), xmask & ymask) tl.store(out_ptr0 + (x2 + 16 * y3), tmp0, xmask & ymask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4,), (1,)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4,), (1,), torch.float32) buf3 = empty_strided_cuda((4,), (1,), torch.float32) buf1 = buf0 del buf0 buf5 = buf3 del buf3 get_raw_stream(0) triton_per_fused_add_mean_sqrt_var_0[grid(4)](buf1, buf5, primals_1, 4, 64, XBLOCK=1, num_warps=2, num_stages=1) buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_add_div_mul_sub_1[grid(64, 4)](primals_2, primals_1, buf1, buf5, primals_3, buf6, 64, 4, XBLOCK=4, YBLOCK =32, num_warps=4, num_stages=1) del primals_2 del primals_3 buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32) triton_poi_fused_add_div_mul_permute_sub_2[grid(16, 16)](buf6, buf7, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1) del buf6 return buf7, primals_1, buf1, buf5 class BatchNormConvNew(nn.Module): def __init__(self, num_channels, eps=1e-08): super().__init__() self.num_channels = num_channels self.eps = eps self.gamma = Parameter(torch.Tensor(num_channels)) self.beta = Parameter(torch.Tensor(num_channels)) self.reset_parameters() def reset_parameters(self): nn.init.ones_(self.gamma) nn.init.zeros_(self.beta) def forward(self, input_0): primals_2 = self.gamma primals_3 = self.beta primals_1 = input_0 output = call([primals_1, primals_2, primals_3]) return output[0]
jkoscialkowski/dnn-exercises
BatchNormConv
false
6,966
[ "MIT" ]
1
5d1616fce1b461e39858c68279d2fafefab00a56
https://github.com/jkoscialkowski/dnn-exercises/tree/5d1616fce1b461e39858c68279d2fafefab00a56
LinearWithConstraint
import torch import torch.nn as nn class LinearWithConstraint(nn.Linear): def __init__(self, *args, max_norm=1, **kwargs): self.max_norm = max_norm super(LinearWithConstraint, 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(LinearWithConstraint, self).forward(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 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_renorm_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 16 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x2 = xindex x1 = xindex // 4 tmp0 = tl.load(in_ptr0 + x2, xmask) tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last') tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last') tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last') tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last') tmp2 = tmp1 * tmp1 tmp4 = tmp3 * tmp3 tmp5 = tmp2 + tmp4 tmp7 = tmp6 * tmp6 tmp8 = tmp5 + tmp7 tmp10 = tmp9 * tmp9 tmp11 = tmp8 + tmp10 tmp12 = libdevice.sqrt(tmp11) tmp13 = 1.0 tmp14 = tmp12 > tmp13 tmp15 = 1e-07 tmp16 = tmp12 + tmp15 tmp17 = tl.full([1], 1, tl.int32) tmp18 = tmp17 / tmp16 tmp19 = tmp18 * tmp13 tmp20 = tl.where(tmp14, tmp19, tmp13) tmp21 = tmp0 * tmp20 tl.store(out_ptr0 + x2, tmp21, xmask) def call(args): primals_1, primals_2, primals_3 = args args.clear() assert_size_stride(primals_1, (4, 4), (4, 1)) assert_size_stride(primals_2, (4,), (1,)) assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32) get_raw_stream(0) triton_poi_fused_renorm_0[grid(16)](primals_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1) buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32) extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), reinterpret_tensor(buf0, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf1) del primals_2 buf2 = torch.ops.aten.set_.source_Tensor(primals_1, buf0) assert_size_stride(buf2, (4, 4), (4, 1)) del primals_1 return reinterpret_tensor(buf1, (4, 4, 4, 4), (64, 16, 4, 1), 0 ), reinterpret_tensor(primals_3, (64, 4), (4, 1), 0) class LinearWithConstraintNew(nn.Linear): def __init__(self, *args, max_norm=1, **kwargs): self.max_norm = max_norm super(LinearWithConstraintNew, 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]
jiuney/XAI606-EEGNet
LinearWithConstraint
false
6,967
[ "MIT" ]
1
45ff28630ed1b09d0853f2cfb148a5dd2693e5ab
https://github.com/jiuney/XAI606-EEGNet/tree/45ff28630ed1b09d0853f2cfb148a5dd2693e5ab
patch_extractor
import torch from torch import nn class patch_extractor(nn.Module): """ Module for creating custom patch extractor """ def __init__(self, patch_size, pad=False, center=False, dim=2): super(patch_extractor, self).__init__() self.dim = dim self.im2pat = nn.Unfold(kernel_size=patch_size) self.pad = pad self.padsize = patch_size - 1 self.center = center self.patch_size = patch_size def forward(self, input, batch_size=0, split=[1, 0]): if self.pad and self.dim == 2: input = torch.cat((input, input[:, :, :self.padsize, :]), 2) input = torch.cat((input, input[:, :, :, :self.padsize]), 3) elif self.pad and self.dim == 3: input = torch.cat((input, input[:, :, :self.padsize, :, :]), 2) input = torch.cat((input, input[:, :, :, :self.padsize, :]), 3) input = torch.cat((input, input[:, :, :, :, :self.padsize]), 4) if self.dim == 2: patches = self.im2pat(input).squeeze(0).transpose(1, 0) split_size = patches.size(0) // split[0] if split[1] == split[0] - 1: patches = patches[split_size * split[1]:] else: patches = patches[split_size * split[1]:split_size * (split [1] + 1)] elif self.dim == 3: patches = self.im2pat(input[0]).squeeze(0).transpose(1, 0).reshape( -1, input.shape[2], self.patch_size, self.patch_size) split_size = patches.size(0) // split[0] if split[1] == split[0] - 1: patches = patches[split_size * split[1]:] else: patches = patches[split_size * split[1]:split_size * (split [1] + 1)] patches = patches.unfold(1, self.patch_size, self.stride).permute( 0, 1, 4, 2, 3) patches = patches.reshape(-1, self.patch_size ** 3) if batch_size > 0: idx = torch.randperm(patches.size(0))[:batch_size] patches = patches[idx, :] if self.center: patches = patches - torch.mean(patches, -1).unsqueeze(-1) return patches def get_inputs(): return [torch.rand([4, 4, 4, 4])] def get_init_inputs(): return [[], {'patch_size': 4}]
import torch import triton import triton.language as tl from torch._inductor.runtime.triton_heuristics import grid from torch._C import _cuda_getCurrentRawStream as get_raw_stream 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_im2col_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 256 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x3 = xindex tmp0 = tl.load(in_ptr0 + x3, xmask) tl.store(out_ptr0 + x3, tmp0, xmask) def call(args): arg0_1, = args args.clear() assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1)) with torch.cuda._DeviceGuard(0): torch.cuda.set_device(0) buf0 = empty_strided_cuda((4, 4, 4, 1, 4, 1), (64, 16, 4, 4, 1, 1), torch.float32) get_raw_stream(0) triton_poi_fused_im2col_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1) del arg0_1 return reinterpret_tensor(buf0, (64, 4, 1), (1, 64, 4), 0), class patch_extractorNew(nn.Module): """ Module for creating custom patch extractor """ def __init__(self, patch_size, pad=False, center=False, dim=2): super(patch_extractorNew, self).__init__() self.dim = dim self.im2pat = nn.Unfold(kernel_size=patch_size) self.pad = pad self.padsize = patch_size - 1 self.center = center self.patch_size = patch_size def forward(self, input_0): arg0_1 = input_0 output = call([arg0_1]) return output[0]
johertrich/Wasserstein_Patch_Prior
patch_extractor
false
6,968
[ "MIT" ]
1
70877a6f1031e51b7868984b97027951d1d190d3
https://github.com/johertrich/Wasserstein_Patch_Prior/tree/70877a6f1031e51b7868984b97027951d1d190d3